def multiple_submit_sms(self, pn, driver): print "Thread:" + str(pn) + "started." for num in range( safe_eval(ReadConfig().get_num_submissions_per_process)): driver.get("http://" + safe_eval(ReadConfig().get_ip_address) + "/smstester") to_number_ele = driver.find_element_by_css_selector( self.TO_NUM_LOC) from_number_ele = driver.find_element_by_css_selector( self.FROM_NUM_LOC) sms_ele = driver.find_element_by_css_selector(self.SMS_MESSAGE_LOC) sms_send_btn = driver.find_element_by_css_selector( self.SEND_SMS_LOC) to_number_ele.clear() to_number_ele.send_keys(safe_eval(ReadConfig().get_to_num_data)) from_number_ele.clear() from_number_ele.send_keys(safe_eval( ReadConfig().get_from_num_data)) sms_ele.clear() sms_ele.send_keys(SMSBuilder().get_sms_submission()) sms_send_btn.click() print "Thread:" + str(pn) + "ended."
def save(file, iarray, metafile=None, version=(1, 0)): """Save a info array to a .npy file and a metadata file. Similar to the numpy.save function. Parameters ---------- file: file handle or str File or file name to write the array to in .npy format. iarray: InfoArray object or array with similar interface Array to be written to file with meta data. metafile: str File name for the meta data. The `info` attribute of `iarray` will be written here. Default is None, where the it is assumed to be the file name associated with `file` with ".meta" appended. """ # Restrict to version (1,0) because we've only written write_header for # this version. if version != (1, 0): raise ValueError("Only version (1,0) is safe from this function.") # Make sure that the meta data will be representable as a string. infostring = repr(iarray.info) try: safe_eval(infostring) except SyntaxError: raise ValueError # Save the array in .npy format. if isinstance(file, basestring): fid = open(file, "wb") else: fid = file npfor.write_array(fid, iarray, version=version) # Figure out what the filename for the meta data should be. if metafile is None: try: fname = file.name except AttributeError: fname = file metafile = fname + ".meta" # Save the meta data. info_fid = open(metafile, 'w') try: info_fid.write(infostring) finally: info_fid.close()
def load_h5(h5obj, path): """Load an info array from an hdf5 file. Parameters ---------- h5obj: h5py File or Group object File from which the info array will be read from. path: string Path within `h5obj` to read the array. Returns ------- iarray: InfoArray Array loaded from file. """ # TODO: Allow `h5obj` to be a string with a path to a file to be opened # and then closed. data = h5obj[path] iarray = np.empty(data.shape, data.dtype) iarray[:] = data[:] info = {} for key, value in data.attrs.iteritems(): info[key] = safe_eval(value) iarray = InfoArray(iarray, info) return iarray
def _read_array_header(fp, version): """ see read_array_header_1_0 """ # Read an unsigned, little-endian short int which has the length of the # header. import struct if version == (1, 0): hlength_type = '<H' elif version == (2, 0): hlength_type = '<I' else: raise ValueError("Invalid version %r" % version) hlength_str = _read_bytes(fp, struct.calcsize(hlength_type), "array header length") header_length = struct.unpack(hlength_type, hlength_str)[0] header = _read_bytes(fp, header_length, "array header") # The header is a pretty-printed string representation of a literal # Python dictionary with trailing newlines padded to a ARRAY_ALIGN byte # boundary. The keys are strings. # "shape" : tuple of int # "fortran_order" : bool # "descr" : dtype.descr header = _filter_header(header) try: old_header = header # replace paddings like ('', '|V4') header = re.sub(r"\s*,*\s*\(\s*''\s*,\s*'\|*V\d'\s*\)", '', header) aligned = header != old_header d = safe_eval(header) except SyntaxError as e: msg = "Cannot parse header: %r\nException: %r" raise ValueError(msg % (header, e)) if not isinstance(d, dict): msg = "Header is not a dictionary: %r" raise ValueError(msg % d) keys = sorted(d.keys()) if keys != ['descr', 'fortran_order', 'shape']: msg = "Header does not contain the correct keys: %r" raise ValueError(msg % (keys, )) # Sanity-check the values. if (not isinstance(d['shape'], tuple) or not numpy.all([isinstance(x, (int, long)) for x in d['shape']])): msg = "shape is not valid: %r" raise ValueError(msg % (d['shape'], )) if not isinstance(d['fortran_order'], bool): msg = "fortran_order is not a valid bool: %r" raise ValueError(msg % (d['fortran_order'], )) try: dtype = numpy.dtype(d['descr'], align=aligned) except TypeError as e: msg = "descr is not a valid dtype descriptor: %r" raise ValueError(msg % (d['descr'], )) return d['shape'], d['fortran_order'], dtype
def open_memmap(filename, mode='r+', dtype=None, shape=None, fortran_order=False, version=(1, 0), metafile=None): """Open a file and memory map it to an InfoMemmap object. This is similar to the numpy.lib.format.openmemmap() function but also deals with the meta data dictionary, which is read and written from a meta data file. The only extra argument over the numpy version is the meta data file name `metafile`. Parameters ---------- metafile: str File name for which the `info` attribute of the returned InfoMemmap will be read from and written to. Default is None, where the it is assumed to be `filename` + ".meta". Returns ------- marray: InfoMemmap The `info` is intialized as an empty dictionary if `mode` is 'w' or if the file corresponding to `metafile` does not exist. The `metafile` attribute of marray is set to the `metafile` parameter unless `mode` is 'r' or 'c' in which case it is set to None. """ # Restrict to version (1,0) because we've only written write_header for # this version. if version != (1, 0): raise ValueError("Only version (1,0) is safe from this function.") # Memory map the data part. marray = npfor.open_memmap(filename, mode, dtype, shape, fortran_order, version) # Get the file name for the meta data. if metafile is None: metafile = filename + '.meta' # Read the meta data if need be. if ('r' in mode or mode is 'c') and os.path.isfile(metafile): info_fid = open(metafile, 'r') try: infostring = info_fid.readline() finally: info_fid.close() info = safe_eval(infostring) else: info = {} # In read mode don't pass a metafile to protect the meta data. if mode is 'r' or mode is 'c': metafile = None marray = info_header.InfoMemmap(marray, info, metafile) return marray
def _read_array_header(fp, version): """ see read_array_header_1_0 """ # Read an unsigned, little-endian short int which has the length of the # header. import struct hinfo = _header_size_info.get(version) if hinfo is None: raise ValueError("Invalid version {!r}".format(version)) hlength_type, encoding = hinfo hlength_str = _read_bytes(fp, struct.calcsize(hlength_type), "array header length") header_length = struct.unpack(hlength_type, hlength_str)[0] header = _read_bytes(fp, header_length, "array header") header = header.decode(encoding) # The header is a pretty-printed string representation of a literal # Python dictionary with trailing newlines padded to a ARRAY_ALIGN byte # boundary. The keys are strings. # "shape" : tuple of int # "fortran_order" : bool # "descr" : dtype.descr # Versions (2, 0) and (1, 0) could have been created by a Python 2 # implementation before header filtering was implemented. if version <= (2, 0): header = _filter_header(header) try: d = safe_eval(header) except SyntaxError as e: msg = "Cannot parse header: {!r}" raise ValueError(msg.format(header)) from e if not isinstance(d, dict): msg = "Header is not a dictionary: {!r}" raise ValueError(msg.format(d)) if EXPECTED_KEYS != d.keys(): keys = sorted(d.keys()) msg = "Header does not contain the correct keys: {!r}" raise ValueError(msg.format(keys)) # Sanity-check the values. if (not isinstance(d['shape'], tuple) or not all(isinstance(x, int) for x in d['shape'])): msg = "shape is not valid: {!r}" raise ValueError(msg.format(d['shape'])) if not isinstance(d['fortran_order'], bool): msg = "fortran_order is not a valid bool: {!r}" raise ValueError(msg.format(d['fortran_order'])) try: dtype = descr_to_dtype(d['descr']) except TypeError as e: msg = "descr is not a valid dtype descriptor: {!r}" raise ValueError(msg.format(d['descr'])) from e return d['shape'], d['fortran_order'], dtype
def read_localarray_header(fp, version): """ Read an array header from a filelike object using the 1.0 file format version. This will leave the file object located just after the header. Parameters ---------- fp : filelike object A file object or something with a `.read()` method like a file. version : tuple of int Returns ------- __version__ : str Version of the Distributed Array Protocol used. dim_data : tuple A tuple containing a dictionary for each dimension of the underlying array, as described in the Distributed Array Protocol. Raises ------ ValueError If the data is invalid. """ # Read an unsigned, little-endian short int which has the length of the # header. import struct if version == (1, 0): hlength_str = _read_bytes(fp, 2, "Array header length") header_length = struct.unpack("<H", hlength_str)[0] header = _read_bytes(fp, header_length, "Array header") else: raise ValueError("Invalid version %r" % version) # The header is a pretty-printed string representation of a literal Python # dictionary with trailing newlines padded to a 16-byte boundary. The keys # are strings. try: d = safe_eval(header) except SyntaxError as e: msg = "Cannot parse header: %r\nException: %r" raise ValueError(msg % (header, e)) if not isinstance(d, dict): msg = "Header is not a dictionary: %r" raise ValueError(msg % d) keys = sorted(d.keys()) if keys != ["__version__", "dim_data"]: msg = "Header does not contain the correct keys: %r" raise ValueError(msg % (keys,)) # TODO: Sanity check with the DAP validator return d["__version__"], d["dim_data"]
def read_localarray_header(fp, version): """ Read an array header from a filelike object using the 1.0 file format version. This will leave the file object located just after the header. Parameters ---------- fp : filelike object A file object or something with a `.read()` method like a file. version : tuple of int Returns ------- __version__ : str Version of the Distributed Array Protocol used. dim_data : tuple A tuple containing a dictionary for each dimension of the underlying array, as described in the Distributed Array Protocol. Raises ------ ValueError If the data is invalid. """ # Read an unsigned, little-endian short int which has the length of the # header. import struct if version == (1, 0): hlength_str = _read_bytes(fp, 2, "Array header length") header_length = struct.unpack('<H', hlength_str)[0] header = _read_bytes(fp, header_length, "Array header") else: raise ValueError("Invalid version %r" % version) # The header is a pretty-printed string representation of a literal Python # dictionary with trailing newlines padded to a 16-byte boundary. The keys # are strings. try: d = safe_eval(header) except SyntaxError as e: msg = "Cannot parse header: %r\nException: %r" raise ValueError(msg % (header, e)) if not isinstance(d, dict): msg = "Header is not a dictionary: %r" raise ValueError(msg % d) keys = sorted(d.keys()) if keys != ['__version__', 'dim_data']: msg = "Header does not contain the correct keys: %r" raise ValueError(msg % (keys, )) # TODO: Sanity check with the DAP validator return d['__version__'], d['dim_data']
def _read_array_header(fp, version): """ see read_array_header_1_0 """ # Read an unsigned, little-endian short int which has the length of the # header. import struct hinfo = _header_size_info.get(version) if hinfo is None: raise ValueError("Invalid version {!r}".format(version)) hlength_type, encoding = hinfo hlength_str = _read_bytes(fp, struct.calcsize(hlength_type), "array header length") header_length = struct.unpack(hlength_type, hlength_str)[0] header = _read_bytes(fp, header_length, "array header") header = header.decode(encoding) # The header is a pretty-printed string representation of a literal # Python dictionary with trailing newlines padded to a ARRAY_ALIGN byte # boundary. The keys are strings. # "shape" : tuple of int # "fortran_order" : bool # "descr" : dtype.descr header = _filter_header(header) try: d = safe_eval(header) except SyntaxError as e: msg = "Cannot parse header: {!r}\nException: {!r}" raise ValueError(msg.format(header, e)) if not isinstance(d, dict): msg = "Header is not a dictionary: {!r}" raise ValueError(msg.format(d)) keys = sorted(d.keys()) if keys != ["descr", "fortran_order", "shape"]: msg = "Header does not contain the correct keys: {!r}" raise ValueError(msg.format(keys)) # Sanity-check the values. if not isinstance(d["shape"], tuple) or not numpy.all( [isinstance(x, (int, long)) for x in d["shape"]] ): msg = "shape is not valid: {!r}" raise ValueError(msg.format(d["shape"])) if not isinstance(d["fortran_order"], bool): msg = "fortran_order is not a valid bool: {!r}" raise ValueError(msg.format(d["fortran_order"])) try: dtype = descr_to_dtype(d["descr"]) except TypeError as e: msg = "descr is not a valid dtype descriptor: {!r}" raise ValueError(msg.format(d["descr"])) return d["shape"], d["fortran_order"], dtype
def _info_flush(self): """Write the info array to disk only.""" # Prior to numpy 1.5, we can't get the mode, so just assume we are # allowed to write mode = getattr(self, "mode", "r+") if ("+" in mode or "w" in mode) and self.metafile is not None: # Convert info dictionary to a pretty string. infostring = repr(self.info) try: safe_eval(infostring) except SyntaxError: raise ce.DataError("Array info not representable as a string.") # Save the meta data. info_fid = open(self.metafile, "w") try: info_fid.write(infostring) finally: info_fid.close()
def _read_array_header(fp, version): """ see read_array_header_1_0 """ # Read an unsigned, little-endian short int which has the length of the # header. import struct if version == (1, 0): hlength_type = '<H' elif version == (2, 0): hlength_type = '<I' else: raise ValueError("Invalid version %r" % version) hlength_str = _read_bytes(fp, struct.calcsize(hlength_type), "array header length") header_length = struct.unpack(hlength_type, hlength_str)[0] header = _read_bytes(fp, header_length, "array header") # The header is a pretty-printed string representation of a literal # Python dictionary with trailing newlines padded to a ARRAY_ALIGN byte # boundary. The keys are strings. # "shape" : tuple of int # "fortran_order" : bool # "descr" : dtype.descr header = _filter_header(header) try: d = safe_eval(header) except SyntaxError as e: msg = "Cannot parse header: %r\nException: %r" raise ValueError(msg % (header, e)) if not isinstance(d, dict): msg = "Header is not a dictionary: %r" raise ValueError(msg % d) keys = sorted(d.keys()) if keys != ['descr', 'fortran_order', 'shape']: msg = "Header does not contain the correct keys: %r" raise ValueError(msg % (keys,)) # Sanity-check the values. if (not isinstance(d['shape'], tuple) or not numpy.all([isinstance(x, (int, long)) for x in d['shape']])): msg = "shape is not valid: %r" raise ValueError(msg % (d['shape'],)) if not isinstance(d['fortran_order'], bool): msg = "fortran_order is not a valid bool: %r" raise ValueError(msg % (d['fortran_order'],)) try: dtype = descr_to_dtype(d['descr']) except TypeError as e: msg = "descr is not a valid dtype descriptor: %r" raise ValueError(msg % (d['descr'],)) return d['shape'], d['fortran_order'], dtype
def _read_array_header(fp, version): """ see read_array_header_1_0 """ # Read an unsigned, little-endian short int which has the length of the # header. import struct if version == (1, 0): hlength_str = _read_bytes(fp, 2, "array header length") header_length = struct.unpack('<H', hlength_str)[0] header = _read_bytes(fp, header_length, "array header") elif version == (2, 0): hlength_str = _read_bytes(fp, 4, "array header length") header_length = struct.unpack('<I', hlength_str)[0] header = _read_bytes(fp, header_length, "array header") else: raise ValueError("Invalid version %r" % version) # The header is a pretty-printed string representation of a literal Python # dictionary with trailing newlines padded to a 16-byte boundary. The keys # are strings. # "shape" : tuple of int # "fortran_order" : bool # "descr" : dtype.descr try: d = safe_eval(header) except SyntaxError as e: msg = "Cannot parse header: %r\nException: %r" raise ValueError(msg % (header, e)) if not isinstance(d, dict): msg = "Header is not a dictionary: %r" raise ValueError(msg % d) keys = sorted(d.keys()) if keys != ['descr', 'fortran_order', 'shape']: msg = "Header does not contain the correct keys: %r" raise ValueError(msg % (keys, )) # Sanity-check the values. if (not isinstance(d['shape'], tuple) or not numpy.all([isinstance(x, (int, long)) for x in d['shape']])): msg = "shape is not valid: %r" raise ValueError(msg % (d['shape'], )) if not isinstance(d['fortran_order'], bool): msg = "fortran_order is not a valid bool: %r" raise ValueError(msg % (d['fortran_order'], )) try: dtype = numpy.dtype(d['descr']) except TypeError as e: msg = "descr is not a valid dtype descriptor: %r" raise ValueError(msg % (d['descr'], )) return d['shape'], d['fortran_order'], dtype
def _info_flush(self): """Write the info array to disk only.""" # Prior to numpy 1.5, we can't get the mode, so just assume we are # allowed to write mode = getattr(self, 'mode', 'r+') if ('+' in mode or 'w' in mode) and self.metafile is not None: # Convert info dictionary to a pretty string. infostring = repr(self.info) try: safe_eval(infostring) except SyntaxError: raise ce.DataError("Array info not representable as a string.") # Save the meta data. info_fid = open(self.metafile, 'w') try: info_fid.write(infostring) finally: info_fid.close()
def read_array_header_1_0(fp): """ Read an array header from a filelike object using the 1.0 file format version. This will leave the file object located just after the header. Parameters ---------- fp : filelike object A file object or something with a `.read()` method like a file. Returns ------- shape : tuple of int The shape of the array. fortran_order : bool The array data will be written out directly if it is either C-contiguous or Fortran-contiguous. Otherwise, it will be made contiguous before writing it out. dtype : dtype The dtype of the file's data. Raises ------ ValueError : If the data is invalid. """ # Read an unsigned, little-endian short int which has the length of the # header. import struct hlength_str = fp.read(2) if len(hlength_str) != 2: msg = "EOF at %s before reading array header length" raise ValueError(msg % fp.tell()) header_length = struct.unpack('<H', hlength_str)[0] header = fp.read(header_length) if len(header) != header_length: raise ValueError("EOF at %s before reading array header" % fp.tell()) # The header is a pretty-printed string representation of a literal Python # dictionary with trailing newlines padded to a 16-byte boundary. The keys # are strings. # "shape" : tuple of int # "fortran_order" : bool # "descr" : dtype.descr try: d = safe_eval(header) except SyntaxError, e: msg = "Cannot parse header: %r\nException: %r" raise ValueError(msg % (header, e))
def _dtype_unpack(self, s): # pulled from np.lib.format.read_array_header_1_0 # The header is a pretty-printed string representation of a literal Python # dictionary with trailing newlines padded to a 16-byte boundary. The keys # are strings. # "shape" : tuple of int # "fortran_order" : bool # "descr" : dtype.descr try: d = safe_eval(s) except SyntaxError, e: msg = "Cannot parse descriptor: %r\nException: %r" raise ValueError(msg % (s, e))
def load(file, metafile=None): """Open a .npy file and load it into memory as an info_aray. Similar to the numpy.load function. Does not support memory mapping (use open_memmap). Parameters ---------- file: file handle or str .npy file or file name to read the array from. metafile: str File name for which the `info` attribute of the returned InfoArray will be read from. Default is None, where the it is assumed to be the file name associated with `file` with ".meta" appended. If the file does not exist, the info attribute is initialized to an empty dictionary. Returns ------- iarray: InfoArray object """ # Load the data from .npy format. array = sp.load(file) # Figure out what the filename for the meta data should be. if metafile is None: try: fname = file.name except AttributeError: fname = file metafile = fname + ".meta" # Read the meta data. if os.path.isfile(metafile): info_fid = open(metafile, 'r') try: infostring = info_fid.readline() finally: info_fid.close() info = safe_eval(infostring) else: info = {} # Construct the infor array. array = info_header.InfoArray(array, info) return array
def _read_header(self): self.file = None try: magic_str = magic(1, 0) self.file = MPI.File.Open(self.comm, self.fn, MPI.MODE_RDONLY) # magic_str = mpi_read_bytes(self.file, len(magic_str)) if magic_str[:-2] != MAGIC_PREFIX: raise MPIFileTypeError( "MAGIC_PREFIX missing at the beginning of file {}".format( self.fn)) version = magic_str[-2:] if version == b'\x01\x00': hlength_type = '<H' elif version == b'\x02\x00': hlength_type = '<I' else: raise MPIFileTypeError("Invalid version %r" % version) hlength_str = mpi_read_bytes(self.file, struct.calcsize(hlength_type)) header_length = struct.unpack(hlength_type, hlength_str)[0] header = mpi_read_bytes(self.file, header_length) header = _filter_header(asstr(header)) d = safe_eval( header ) # TODO: Copy from _read_array_header with all the assertions self.dtype = np.dtype(d['descr']) self.fortran_order = d['fortran_order'] self.nb_grid_pts = d['shape'] except Exception as err: # FIXME! This should be handled through a resource manager if self.file is not None: self.file.Close() raise err
def get_gps_answer(self): return choice(safe_eval(ReadConfig().get_gps_data)) + "," + choice( safe_eval(ReadConfig().get_gps_data))
for num in range( safe_eval(ReadConfig().get_num_submissions_per_process)): driver.get("http://" + safe_eval(ReadConfig().get_ip_address) + "/smstester") to_number_ele = driver.find_element_by_css_selector( self.TO_NUM_LOC) from_number_ele = driver.find_element_by_css_selector( self.FROM_NUM_LOC) sms_ele = driver.find_element_by_css_selector(self.SMS_MESSAGE_LOC) sms_send_btn = driver.find_element_by_css_selector( self.SEND_SMS_LOC) to_number_ele.clear() to_number_ele.send_keys(safe_eval(ReadConfig().get_to_num_data)) from_number_ele.clear() from_number_ele.send_keys(safe_eval( ReadConfig().get_from_num_data)) sms_ele.clear() sms_ele.send_keys(SMSBuilder().get_sms_submission()) sms_send_btn.click() print "Thread:" + str(pn) + "ended." if __name__ == "__main__": num_procs = safe_eval(ReadConfig().get_num_procs) for num in range(num_procs): Process(target=SMSSubmission().multiple_submit_sms, args=(num, webdriver.Firefox())).start()
def read_array_header_1_0(fp): """ Read an array header from a filelike object using the 1.0 file format version. This will leave the file object located just after the header. Parameters ---------- fp : filelike object A file object or something with a `.read()` method like a file. Returns ------- shape : tuple of int The shape of the array. fortran_order : bool The array data will be written out directly if it is either C-contiguous or Fortran-contiguous. Otherwise, it will be made contiguous before writing it out. dtype : dtype The dtype of the file's data. Raises ------ ValueError If the data is invalid. """ # Read an unsigned, little-endian short int which has the length of the # header. import struct hlength_str = fp.read(2) if len(hlength_str) != 2: msg = "EOF at %s before reading array header length" raise ValueError(msg % fp.tell()) header_length = struct.unpack('<H', hlength_str)[0] header = fp.read(header_length) if len(header) != header_length: raise ValueError("EOF at %s before reading array header" % fp.tell()) # The header is a pretty-printed string representation of a literal Python # dictionary with trailing newlines padded to a 16-byte boundary. The keys # are strings. # "shape" : tuple of int # "fortran_order" : bool # "descr" : dtype.descr try: d = safe_eval(header) except SyntaxError as e: msg = "Cannot parse header: %r\nException: %r" raise ValueError(msg % (header, e)) if not isinstance(d, dict): msg = "Header is not a dictionary: %r" raise ValueError(msg % d) keys = sorted(d.keys()) if keys != ['descr', 'fortran_order', 'shape']: msg = "Header does not contain the correct keys: %r" raise ValueError(msg % (keys,)) # Sanity-check the values. if (not isinstance(d['shape'], tuple) or not numpy.all([isinstance(x, (int,long)) for x in d['shape']])): msg = "shape is not valid: %r" raise ValueError(msg % (d['shape'],)) if not isinstance(d['fortran_order'], bool): msg = "fortran_order is not a valid bool: %r" raise ValueError(msg % (d['fortran_order'],)) try: dtype = numpy.dtype(d['descr']) except TypeError as e: msg = "descr is not a valid dtype descriptor: %r" raise ValueError(msg % (d['descr'],)) return d['shape'], d['fortran_order'], dtype
def get_word_answer(self): return choice(safe_eval(ReadConfig().get_word_data))
def test_safe_eval_nameconstant(): # Test if safe_eval supports Python 3.4 _ast.NameConstant utils.safe_eval('None')
def get_number_answer(self): return choice(safe_eval(ReadConfig().get_number_data))
def read_array_header_1_0(fp): """ Read an array header from a filelike object using the 1.0 file format version. This will leave the file object located just after the header. Parameters ---------- fp : filelike object A file object or something with a `.read()` method like a file. Returns ------- shape : tuple of int The shape of the array. fortran_order : bool The array data will be written out directly if it is either C-contiguous or Fortran-contiguous. Otherwise, it will be made contiguous before writing it out. dtype : dtype The dtype of the file's data. Raises ------ ValueError If the data is invalid. """ # Read an unsigned, little-endian short int which has the length of the # header. import struct hlength_str = fp.read(2) if len(hlength_str) != 2: msg = "EOF at %s before reading array header length" raise ValueError(msg % fp.tell()) header_length = struct.unpack('<H', hlength_str)[0] header = fp.read(header_length) if len(header) != header_length: raise ValueError("EOF at %s before reading array header" % fp.tell()) # The header is a pretty-printed string representation of a literal Python # dictionary with trailing newlines padded to a 16-byte boundary. The keys # are strings. # "shape" : tuple of int # "fortran_order" : bool # "descr" : dtype.descr try: d = safe_eval(header) except SyntaxError as e: msg = "Cannot parse header: %r\nException: %r" raise ValueError(msg % (header, e)) if not isinstance(d, dict): msg = "Header is not a dictionary: %r" raise ValueError(msg % d) keys = sorted(d.keys()) if keys != ['descr', 'fortran_order', 'shape']: msg = "Header does not contain the correct keys: %r" raise ValueError(msg % (keys,)) # Sanity-check the values. if (not isinstance(d['shape'], tuple) or not numpy.all([isinstance(x, (int, long)) for x in d['shape']])): msg = "shape is not valid: %r" raise ValueError(msg % (d['shape'],)) if not isinstance(d['fortran_order'], bool): msg = "fortran_order is not a valid bool: %r" raise ValueError(msg % (d['fortran_order'],)) try: dtype = numpy.dtype(d['descr']) except TypeError as e: msg = "descr is not a valid dtype descriptor: %r" raise ValueError(msg % (d['descr'],)) return d['shape'], d['fortran_order'], dtype
def get_subject(self): return choice(safe_eval(ReadConfig().get_subject_data))