class AtomicFileWriter(object): def __init__(self, path, tmp_prefix=None, encoding='utf-8'): self.name = path output_dir, base = os.path.split(path) if tmp_prefix is None: tmp_prefix = base + '.' self.tmpf = NamedTemporaryFile(dir=output_dir, prefix=tmp_prefix, mode='w', encoding=encoding, delete=False) def __enter__(self): self.tmpf.__enter__() return self def __exit__(self, exc_type, exc_value, exc_traceback): tmp_name = self.tmpf.name result = self.tmpf.__exit__(exc_type, exc_value, exc_traceback) if result or exc_type is None: os.rename(tmp_name, self.name) else: os.unlink(tmp_name) return result def write(self, data): return self.tmpf.write(data)
def apicalls(target, **kwargs): """ """ if not target: raise Exception("Invalid target for apicalls()") output_file = NamedTemporaryFile() kwargs.update({"output_file" : output_file}) cmd = _dtrace_command_line(target, **kwargs) # Generate dtrace probes for analysis definitions = os.path.abspath(os.path.join(__file__, "../../core/data/signatures.yml")) probes_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "probes.d") generate_probes(definitions, probes_file, overwrite=True) # The dtrace script will take care of timeout itself, so we just launch # it asynchronously with open(os.devnull, "w") as null: _ = Popen(cmd, stdout=null, stderr=null, cwd=current_directory()) with open('/Users/cloudmark/yield.txt', 'w+') as f: for entry in filelines(output_file): value = entry.strip() if "## apicalls.d done ##" in value: break if len(value) == 0: continue f.write(str(_parse_entry(value))) f.flush() import time time.sleep(1) yield _parse_entry(value) output_file.close() os.remove(probes_file)
def run(self): active_view = self.window.active_view() text = "\n\n".join(getSelectedText(active_view)).strip() tf = NamedTemporaryFile(mode="w", delete=False) try: tf.write(text) tf.close() res = subprocess.check_output(["m4", tf.name], stderr=subprocess.STDOUT, cwd=os.path.dirname(os.path.abspath(active_view.file_name()))) res = res.decode('utf-8').replace('\r', '').strip() panel_name = "m4expand.results" panel = self.window.create_output_panel(panel_name) self.window.run_command("show_panel", {"panel": "output." + panel_name}) panel.set_read_only(False) panel.set_syntax_file(active_view.settings().get("syntax")) panel.run_command("append", {"characters": res}) panel.set_read_only(True) except Exception as e: print("M4Expand - An error occurred: ", e) finally: os.unlink(tf.name)
def tmpfile(stream, mode=None): """Context manager that writes a :class:`Stream` object to a named temporary file and yield it's filename. Cleanup deletes from the temporary file from disk. Args: stream (Stream): Stream object to write to disk as temporary file. mode (int, optional): File mode to set on temporary file. Returns: str: Temporoary file name """ tmp = NamedTemporaryFile(delete=False) if mode is not None: oldmask = os.umask(0) try: os.chmod(tmp.name, mode) finally: os.umask(oldmask) for data in stream: tmp.write(to_bytes(data)) tmp.close() yield tmp.name os.remove(tmp.name)
def testLogfile(self): """Test logging into a logfile""" f = NamedTemporaryFile(delete=False) filename = f.name try: set_log_level("error") # avoid using the console logger f.write(":-P\n") f.close() start_logfile(f.name, "devinfo") log = getLogger("prosoda.test.integration.test_logger") log.debug("Should not be in logfile! :-( ") log.info("Should be in logfile :-) ") log.devinfo("Should be in logfile :-) ") log.warning("Should really be in logfile :-D ") stop_logfile(f.name) contents = file(f.name).read() self.assertNotIn(":-(", contents) self.assertNotIn(":-P", contents) self.assertIn(":-)", contents) self.assertIn(":-D", contents) # Make sure no colour codes are leaked into the logfile self.assertNotIn("\033", contents) finally: set_log_level("debug") unlink(filename)
def __enter__(self): # Ensure that we have not re-entered if self.temp_path != None or self.service != None: raise Exception('Cannot use multiple nested with blocks on same Youtube object!') flow = flow_from_clientsecrets( self.client_secrets_path, scope=YOUTUBE_UPLOAD_SCOPE, message=MISSING_CLIENT_SECRETS_MESSAGE) temp_file = NamedTemporaryFile(delete=False) self.temp_path = temp_file.name temp_file.close() storage = Storage(self.temp_path) credentials = storage.get() if credentials is None or credentials.invalid: credentials = run_flow( flow, storage, argparser.parse_args(list()) ) self.service = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, http=credentials.authorize(httplib2.Http())) return self
def run_solver(self, conflicts, election, deletion_handler, outfile=None): if not conflicts: return [], 0 self.deletion_handler = deletion_handler instance = self.generate_instance(conflicts, election) f = NamedTemporaryFile(delete=False) f.write(instance.encode(code)) f.close() process = Popen([self.cmd, f.name], stdout=PIPE) out, err = process.communicate() conflict_variables, optimum = self.parse_instance(out) if outfile: candidates = election[0] votes = election[1] votecounts = election[2] votemap = self.delete_votes(votes, votecounts, conflict_variables) votesum = sum(votemap.values()) write_map(candidates, votesum, votemap, open(outfile, "w")) remove(f.name) return conflict_variables, optimum
def test_toy_corpus(): keats = ('She dwells with Beauty - Beauty that must die;\n\n' 'And Joy, whose hand is ever at his lips\n\n' 'Bidding adieu; and aching Pleasure nigh,\n\n' 'Turning to poison while the bee-mouth sips:\n\n' 'Ay, in the very temple of Delight\n\n' 'Veil\'d Melancholy has her sovran shrine,\n\n' 'Though seen of none save him whose strenuous tongue\n\n' 'Can burst Joy\'s grape against his palate fine;\n\n' 'His soul shall taste the sadness of her might,\n\n' 'And be among her cloudy trophies hung.') assert toy_corpus(keats) assert toy_corpus(keats, nltk_stop=True) assert toy_corpus(keats, stop_freq=1) assert toy_corpus(keats, add_stop=['and', 'with']) assert toy_corpus(keats, nltk_stop=True, stop_freq=1, add_stop=['ay']) import os from tempfile import NamedTemporaryFile as NFT tmp = NFT(delete=False) tmp.write(keats) tmp.close() c = toy_corpus(tmp.name, is_filename=True, nltk_stop=True, add_stop=['ay']) assert c os.remove(tmp.name) return c
def command_update(args): def write_to(out): config.output(out) library = KeyLibrary(args.key_directory) with open(args.config_file) as fd: config = SedgeEngine(library, fd, not args.no_verify, url=args.config_file) if args.output_file == '-': write_to(ConfigOutput(sys.stdout)) return if not check_or_confirm_overwrite(args.output_file): print("Aborting.", file=sys.stderr) sys.exit(1) tmpf = NamedTemporaryFile(mode='w', dir=os.path.dirname(args.output_file), delete=False) try: tmpf.file.write('''\ # :sedge: # # this configuration generated from `sedge' file: # %s # # do not edit this file manually, edit the source file and re-run `sedge' # ''' % (args.config_file)) write_to(ConfigOutput(tmpf.file)) tmpf.close() if args.verbose: diff_config_changes(args.output_file, tmpf.name) os.rename(tmpf.name, args.output_file) except: os.unlink(tmpf.name) raise
def execute(self, context): hive = HiveServer2Hook(hiveserver2_conn_id=self.hiveserver2_conn_id) logging.info("Extracting data from Hive") logging.info(self.sql) if self.bulk_load: tmpfile = NamedTemporaryFile() hive.to_csv(self.sql, tmpfile.name, delimiter='\t', lineterminator='\n', output_header=False) else: results = hive.get_records(self.sql) mysql = MySqlHook(mysql_conn_id=self.mysql_conn_id) if self.mysql_preoperator: logging.info("Running MySQL preoperator") mysql.run(self.mysql_preoperator) logging.info("Inserting rows into MySQL") if self.bulk_load: mysql.bulk_load(table=self.mysql_table, tmp_file=tmpfile.name) tmpfile.close() else: mysql.insert_rows(table=self.mysql_table, rows=results) if self.mysql_postoperator: logging.info("Running MySQL postoperator") mysql.run(self.mysql_postoperator) logging.info("Done.")
def write_temp_file(data): # create a temp file for use as a config file. This should get cleaned # up magically at the end of the run. fid = NamedTemporaryFile(mode='w+b', suffix='.tmp') fid.write(data) fid.seek(0) return fid
def _build_and_catch_errors(self, build_func, options_bytes, source=None): try: return build_func() except _cl.RuntimeError as e: msg = e.what if options_bytes: msg = msg + "\n(options: %s)" % options_bytes.decode("utf-8") if source is not None: from tempfile import NamedTemporaryFile srcfile = NamedTemporaryFile(mode="wt", delete=False, suffix=".cl") try: srcfile.write(source) finally: srcfile.close() msg = msg + "\n(source saved as %s)" % srcfile.name code = e.code routine = e.routine err = _cl.RuntimeError( _cl.Error._ErrorRecord( msg=msg, code=code, routine=routine)) # Python 3.2 outputs the whole list of currently active exceptions # This serves to remove one (redundant) level from that nesting. raise err
def roundtrip(self, dtype, x, suffix): f = NamedTemporaryFile(suffix='.' + suffix) fname = f.name f.close() sio.imsave(fname, x) y = sio.imread(fname) assert_array_equal(y, x)
def _write_local_schema_file(self, cursor): """ Takes a cursor, and writes the BigQuery schema for the results to a local file system. :return: A dictionary where key is a filename to be used as an object name in GCS, and values are file handles to local files that contains the BigQuery schema fields in .json format. """ schema = [] for field in cursor.description: # See PEP 249 for details about the description tuple. field_name = field[0] field_type = self.type_map(field[1]) # Always allow TIMESTAMP to be nullable. MySQLdb returns None types # for required fields because some MySQL timestamps can't be # represented by Python's datetime (e.g. 0000-00-00 00:00:00). field_mode = 'NULLABLE' if field[6] or field_type == 'TIMESTAMP' else 'REQUIRED' schema.append({ 'name': field_name, 'type': field_type, 'mode': field_mode, }) self.log.info('Using schema for %s: %s', self.schema_filename, schema) tmp_schema_file_handle = NamedTemporaryFile(delete=True) s = json.dumps(schema, tmp_schema_file_handle) if PY3: s = s.encode('utf-8') tmp_schema_file_handle.write(s) return {self.schema_filename: tmp_schema_file_handle}
def run(self, uid, aid, publish=True): aid = int(aid) audiobook = Audiobook.objects.get(id=aid) self.set_status(aid, status.ENCODING) user = User.objects.get(id=uid) try: os.makedirs(BUILD_PATH) except OSError as e: if e.errno == errno.EEXIST: pass else: raise out_file = NamedTemporaryFile(delete=False, prefix='%d-' % aid, suffix='.%s' % self.ext, dir=BUILD_PATH) out_file.close() self.encode(audiobook.source_file.path, out_file.name) self.set_status(aid, status.TAGGING) self.set_tags(audiobook, out_file.name) self.set_status(aid, status.SENDING) if publish: self.put(user, audiobook, out_file.name) self.published(aid) else: self.set_status(aid, None) self.save(audiobook, out_file.name)
def reg_code(): img, code =generate_code_image((80,30),5) session["code"] = code tp = NamedTemporaryFile() img.save(tp.name,format="png") tp.seek(0) return send_file(tp.name,mimetype='image/png')
def _write_local_schema_file(self, cursor): """ Takes a cursor, and writes the BigQuery schema for the results to a local file system. :return: A dictionary where key is a filename to be used as an object name in GCS, and values are file handles to local files that contains the BigQuery schema fields in .json format. """ schema = [] for field in cursor.description: # See PEP 249 for details about the description tuple. field_name = field[0] field_type = self.type_map(field[1]) field_mode = 'REPEATED' if field[1] in (1009, 1005, 1007, 1016) else 'NULLABLE' schema.append({ 'name': field_name, 'type': field_type, 'mode': field_mode, }) self.log.info('Using schema for %s: %s', self.schema_filename, schema) tmp_schema_file_handle = NamedTemporaryFile(delete=True) s = json.dumps(schema, sort_keys=True) if PY3: s = s.encode('utf-8') tmp_schema_file_handle.write(s) return {self.schema_filename: tmp_schema_file_handle}
def test_seq_pipeline_run(): 'It tests that the pipeline runs ok' pipeline = 'sanger_with_qual' fhand_adaptors = NamedTemporaryFile() fhand_adaptors.write(ADAPTORS) fhand_adaptors.flush() arabidopsis_genes = 'arabidopsis_genes+' univec = os.path.join(TEST_DATA_DIR, 'blast', arabidopsis_genes) configuration = {'remove_vectors_blastdb': {'vectors': univec}, 'remove_adaptors': {'adaptors': fhand_adaptors.name}} in_fhands = {} in_fhands['in_seq'] = open(os.path.join(TEST_DATA_DIR, 'seq.fasta'), 'r') in_fhands['in_qual'] = open(os.path.join(TEST_DATA_DIR, 'qual.fasta'), 'r') out_seq_fhand = NamedTemporaryFile() out_qual_fhand = NamedTemporaryFile() writer = SequenceWriter(out_seq_fhand, qual_fhand=out_qual_fhand, file_format='fasta') seq_pipeline_runner(pipeline, configuration, in_fhands, writers={'seq': writer}) result_seq = open(out_seq_fhand.name).read() assert result_seq.count('>') == 6 #are we keeping the description? assert 'mdust' in result_seq
def test_cmyk(): ref = imread(os.path.join(data_dir, 'color.png')) img = Image.open(os.path.join(data_dir, 'color.png')) img = img.convert('CMYK') f = NamedTemporaryFile(suffix='.jpg') fname = f.name f.close() img.save(fname) try: img.close() except AttributeError: # `close` not available on PIL pass new = imread(fname) ref_lab = rgb2lab(ref) new_lab = rgb2lab(new) for i in range(3): newi = np.ascontiguousarray(new_lab[:, :, i]) refi = np.ascontiguousarray(ref_lab[:, :, i]) sim = ssim(refi, newi, dynamic_range=refi.max() - refi.min()) assert sim > 0.99
def test_seq_pipeline_parallel_run_with_fasta_qual(self): 'The pipeline runs in parallel with fasta and qual' pipeline = 'sanger_with_qual' fhand_adaptors = NamedTemporaryFile() fhand_adaptors.write(ADAPTORS) fhand_adaptors.flush() arabidopsis_genes = 'arabidopsis_genes+' univec = os.path.join(TEST_DATA_DIR, 'blast', arabidopsis_genes) configuration = {'remove_vectors': {'vectors': univec}, 'remove_adaptors': {'adaptors': fhand_adaptors.name}} seq1 = create_random_seqwithquality(500, qual_range=50) seq2 = create_random_seqwithquality(500, qual_range=51) seq3 = create_random_seqwithquality(500, qual_range=52) seqs = [seq1, seq2, seq3] inseq_fhand, inqual_fhand = create_temp_seq_file(seqs, format='qual') in_fhands = {} in_fhands['in_seq'] = open(inseq_fhand.name) in_fhands['in_qual'] = open(inqual_fhand.name) outseq_fhand = NamedTemporaryFile() outqual_fhand = NamedTemporaryFile() writer = SequenceWriter(outseq_fhand, qual_fhand=outqual_fhand, file_format='fasta') writers = {'seq': writer} seq_pipeline_runner(pipeline, configuration, in_fhands, processes=4, writers=writers) out_fhand = open(outseq_fhand.name, 'r') result_seq = out_fhand.read() assert result_seq.count('>') == 3
def odt_subreport(name=None, obj=None): if not aeroo_ooo: return _("Error! Subreports not available!") report_xml_ids = ir_obj.search(cr, uid, [('report_name', '=', name)], context=context) if report_xml_ids: service = netsvc.Service._services['report.%s' % name] report_xml = ir_obj.browse(cr, uid, report_xml_ids[0], context=context) data = {'model': obj._table_name, 'id': obj.id, 'report_type': 'aeroo', 'in_format': 'oo-odt'} ### Get new printing object ### sub_aeroo_print = AerooPrint() service.active_prints[sub_aeroo_print.id] = sub_aeroo_print context['print_id'] = sub_aeroo_print.id ############################### sub_aeroo_print.start_time = time.time() report, output = service.create_aeroo_report(cr, uid, \ [obj.id], data, report_xml, context=context, output='odt') # change for OpenERP 6.0 - Service class usage ### Delete printing object ### AerooPrint.print_ids.remove(sub_aeroo_print.id) del service.active_prints[sub_aeroo_print.id] ############################## temp_file = NamedTemporaryFile(suffix='.odt', prefix='aeroo-report-', delete=False) try: temp_file.write(report) finally: temp_file.close() # self.oo_subreports[print_id].append(temp_file.name) # aeroo_print.subreports.append(temp_file.name) self.active_prints[aeroo_print.id].subreports.append(temp_file.name) return "<insert_doc('%s')>" % temp_file.name return None
def test_pipeline_run(): 'It tests that the pipeline runs ok' pipeline = 'sanger_with_qual' fhand_adaptors = NamedTemporaryFile() fhand_adaptors.write(ADAPTORS) fhand_adaptors.flush() arabidopsis_genes = 'arabidopsis_genes+' univec = os.path.join(TEST_DATA_DIR, 'blast', arabidopsis_genes) configuration = {'remove_vectors_blastdb': {'vectors': univec}, 'remove_adaptors': {'adaptors': fhand_adaptors.name}} seq_fhand = open(os.path.join(TEST_DATA_DIR, 'seq.fasta'), 'r') qual_fhand = open(os.path.join(TEST_DATA_DIR, 'qual.fasta'), 'r') seq_iter = seqs_in_file(seq_fhand, qual_fhand) filtered_seq_iter = _pipeline_builder(pipeline, seq_iter, configuration) seq_list = list(filtered_seq_iter) assert 'CGAtcgggggg' in str(seq_list[0].seq) assert len(seq_list) == 6
def test_safe_md5(self): """Make sure we have the expected md5 with varied input types This method is ported from PyCogent (http://www.pycogent.org). PyCogent is a GPL project, but we obtained permission from the authors of this method to port it to the BIOM Format project (and keep it under BIOM's BSD license). """ exp = 'd3b07384d113edec49eaa6238ad5ff00' tmp_f = NamedTemporaryFile( mode='w', prefix='test_safe_md5', suffix='txt') tmp_f.write('foo\n') tmp_f.flush() obs = safe_md5(open(tmp_f.name, 'U')) self.assertEqual(obs, exp) obs = safe_md5(['foo\n']) self.assertEqual(obs, exp) # unsupported type raises TypeError self.assertRaises(TypeError, safe_md5, 42)
def _generate_training_files(self): """Returns a tuple of file objects suitable for passing to the RdpTrainer application controller. """ tmp_dir = get_qiime_temp_dir() training_set = RdpTrainingSet() reference_seqs_file = open(self.Params['reference_sequences_fp'], 'U') id_to_taxonomy_file = open(self.Params['id_to_taxonomy_fp'], 'U') for seq_id, seq in MinimalFastaParser(reference_seqs_file): training_set.add_sequence(seq_id, seq) for line in id_to_taxonomy_file: seq_id, lineage_str = map(strip, line.split('\t')) training_set.add_lineage(seq_id, lineage_str) training_set.dereplicate_taxa() rdp_taxonomy_file = NamedTemporaryFile( prefix='RdpTaxonAssigner_taxonomy_', suffix='.txt', dir=tmp_dir) rdp_taxonomy_file.write(training_set.get_rdp_taxonomy()) rdp_taxonomy_file.seek(0) rdp_training_seqs_file = NamedTemporaryFile( prefix='RdpTaxonAssigner_training_seqs_', suffix='.fasta', dir=tmp_dir) for rdp_id, seq in training_set.get_training_seqs(): rdp_training_seqs_file.write('>%s\n%s\n' % (rdp_id, seq)) rdp_training_seqs_file.seek(0) self._training_set = training_set return rdp_taxonomy_file, rdp_training_seqs_file
def ipconnections(target, **kwargs): """Returns a list of ip connections made by the target. A connection is a named tuple with the following properties: host (string), host_port (int), remote_port (string), protocol (string), timestamp(int). """ if not target: raise Exception("Invalid target for ipconnections()") output_file = NamedTemporaryFile() cmd = ["sudo", "/usr/sbin/dtrace", "-C"] if "timeout" in kwargs: cmd += ["-DANALYSIS_TIMEOUT=%d" % kwargs["timeout"]] cmd += ["-s", path_for_script("ipconnections.d")] cmd += ["-o", output_file.name] if "args" in kwargs: line = "%s %s" % (sanitize_path(target), " ".join(kwargs["args"])) cmd += ["-c", line] else: cmd += ["-c", sanitize_path(target)] # The dtrace script will take care of timeout itself, so we just launch # it asynchronously with open(os.devnull, "w") as f: handler = Popen(cmd, stdout=f, stderr=f) for entry in filelines(output_file): if "## ipconnections.d done ##" in entry.strip(): break yield _parse_single_entry(entry.strip()) output_file.close()
def parallelize(self, c, numSlices=None): """ Distribute a local Python collection to form an RDD. >>> sc.parallelize(range(5), 5).glom().collect() [[0], [1], [2], [3], [4]] """ numSlices = numSlices or self.defaultParallelism # Calling the Java parallelize() method with an ArrayList is too slow, # because it sends O(n) Py4J commands. As an alternative, serialized # objects are written to a file and loaded through textFile(). tempFile = NamedTemporaryFile(delete=False, dir=self._temp_dir) # Make sure we distribute data evenly if it's smaller than self.batchSize if "__len__" not in dir(c): c = list(c) # Make it a list so we can compute its length batchSize = min(len(c) // numSlices, self._batchSize) if batchSize > 1: serializer = BatchedSerializer(self._unbatched_serializer, batchSize) else: serializer = self._unbatched_serializer serializer.dump_stream(c, tempFile) tempFile.close() readRDDFromFile = self._jvm.PythonRDD.readRDDFromFile jrdd = readRDDFromFile(self._jsc, tempFile.name, numSlices) return RDD(jrdd, self, serializer)
def main(): logging.basicConfig(format='%(message)s', level=logging.INFO) args = parse_args() from_date = args.from_date to_date = args.to_date s3_client = S3Client.connect_with_profile(args.aws_profile) s3_bucket = args.s3_bucket s3_key = "{}_{}.logs.json".format(from_date.isoformat(), to_date.isoformat()) sumo_access_id = args.sumo_access_id or os.environ["SUMOLOGIC_ACCESS_ID"] sumo_access_key = args.sumo_access_key or os.environ["SUMOLOGIC_ACCESS_KEY"] sumo_logic_client = SumoLogicClient(sumo_access_id, sumo_access_key) logging.info( "Searching for request logs in {} in range {} to {}".format( args.environment, from_date.isoformat(), to_date.isoformat())) request_logs = sumo_logic_client.search( sumo_logic_query(args.environment), start_dt=from_date, end_dt=to_date, fields=REQUEST_FIELDS) tmp_file = NamedTemporaryFile(mode='w', encoding='utf-8', delete=False) logging.debug("Writing out logs to temporary file {}".format(tmp_file.name)) try: for request in request_logs: tmp_file.write(json.dumps(request, default=json_serialize) + '\n') s3_client.upload_from_file(s3_bucket, s3_key, tmp_file.name) finally: logging.debug("Removing temporary file {}".format(tmp_file.name)) os.remove(tmp_file.name)
def _create_empty_image(self, image_width, image_height): # Check pycairo capabilities if not (cairo.HAS_IMAGE_SURFACE and cairo.HAS_PNG_FUNCTIONS): raise HTTPBadRequest("cairo was not compiled with ImageSurface and PNG support") # Create a new cairo surface surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(image_width), int(image_height)) ctx = cairo.Context(surface) text = "No imagery available for requested coordinates." x_bearing, y_bearing, width, height, x_advance, y_advance = ctx.text_extents(text) ctx.move_to((image_width / 2) - (width / 2), (image_height / 2) + (height / 2)) ctx.set_source_rgba(0, 0, 0, 0.85) ctx.show_text(text) temp_datadir = self.config.get("main", "temp.datadir") temp_url = self.config.get("main", "temp.url") file = NamedTemporaryFile(suffix=".png", dir=temp_datadir, delete=False) surface.write_to_png(file) file.close() return {"file": "%s/%s" % (temp_url, file.name.split("/")[-1])}
def _get_tmp_file(self): ''' Creates a tmp file with the file data ''' data = self.request.files['xml_file'][0]['body'] tmp_file = NamedTemporaryFile(delete=False) tmp_file.write(data) tmp_file.close() return tmp_file.name
def test(): d = {} for i in range( 10000 ): d[ 'foo' + str( i ) ] = 'bar' + str( i ) # Open temporary file and get name file = NamedTemporaryFile() file_name = file.name # Write cdb to file FileCDBDict.to_file( d, file ) file.flush() # Open on disk file2 = open( file_name ) cdb = FileCDBDict( file2 ) for key, value in d.iteritems(): assert cdb[key] == value try: cdb['notin'] assert False, "KeyError was not raised" except KeyError, e: pass