Python tempfile.TemporaryDirectory() Examples

The following are 30 code examples of tempfile.TemporaryDirectory(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tempfile , or try the search function .
Example #1
Source File: pregenerate_training_data.py    From tpu_pretrain with Apache License 2.0 7 votes vote down vote up
def __init__(self, reduce_memory=False):
        if reduce_memory:
            self.temp_dir = TemporaryDirectory()
            self.working_dir = Path(self.temp_dir.name)
            self.document_shelf_filepath = self.working_dir / 'shelf.db'
            self.document_shelf = shelve.open(str(self.document_shelf_filepath),
                                              flag='n', protocol=-1)
            self.documents = None
        else:
            self.documents = []
            self.document_shelf = None
            self.document_shelf_filepath = None
            self.temp_dir = None
        self.doc_lengths = []
        self.doc_cumsum = None
        self.cumsum_max = None
        self.reduce_memory = reduce_memory 
Example #2
Source File: testTutorials.py    From pyGSTi with Apache License 2.0 6 votes vote down vote up
def test_tutorials():
    with TemporaryDirectory() as tmp:
        tmp_path = Path(tmp)

        # Copy tutorial file resources
        for f_path in _TUTORIAL_FILES:
            src = _TUTORIALS_ROOT / f_path
            dest = tmp_path / f_path
            dest.parent.mkdir(parents=True, exist_ok=True)
            if src.is_dir():
                shutil.copytree(src, dest)
            else:
                shutil.copy(src, dest)

        # Emit a test for each notebook
        for nb_path in notebooks_in_path(_TUTORIALS_ROOT):
            rel_path = nb_path.relative_to(_TUTORIALS_ROOT)
            workdir = tmp_path / rel_path.parent
            workdir.mkdir(parents=True, exist_ok=True)
            description = "Running notebook {}".format(rel_path)
            yield attr(description=description)(run_notebook), nb_path, workdir 
Example #3
Source File: deepq.py    From HardRLWithYoutube with MIT License 6 votes vote down vote up
def save_act(self, path=None):
        """Save model to a pickle located at `path`"""
        if path is None:
            path = os.path.join(logger.get_dir(), "model.pkl")

        with tempfile.TemporaryDirectory() as td:
            save_state(os.path.join(td, "model"))
            arc_name = os.path.join(td, "packed.zip")
            with zipfile.ZipFile(arc_name, 'w') as zipf:
                for root, dirs, files in os.walk(td):
                    for fname in files:
                        file_path = os.path.join(root, fname)
                        if file_path != arc_name:
                            zipf.write(file_path, os.path.relpath(file_path, td))
            with open(arc_name, "rb") as f:
                model_data = f.read()
        with open(path, "wb") as f:
            cloudpickle.dump((model_data, self._act_params), f) 
Example #4
Source File: __main__.py    From Rebaler with GNU General Public License v3.0 6 votes vote down vote up
def main():
    random.seed(0)
    args = get_arguments()
    log.logger = log.Log()

    reference, ref_names, circularity, ref_seqs = load_reference(args.reference)
    if args.direct:
        unpolished_sequences = ref_seqs
    else:
        unpolished_sequences = build_unpolished_assembly(args, reference, ref_names, ref_seqs)
    with tempfile.TemporaryDirectory() as polish_dir:
        polishing_rounds(ref_names, unpolished_sequences, circularity, args.reads, args.threads,
                         polish_dir)
        final_assembly = final_shred_and_polish(ref_names, circularity, polish_dir, args.threads)
        output_result(final_assembly, circularity)

    log.log('') 
Example #5
Source File: simple.py    From lirpg with MIT License 6 votes vote down vote up
def save(self, path=None):
        """Save model to a pickle located at `path`"""
        if path is None:
            path = os.path.join(logger.get_dir(), "model.pkl")

        with tempfile.TemporaryDirectory() as td:
            save_state(os.path.join(td, "model"))
            arc_name = os.path.join(td, "packed.zip")
            with zipfile.ZipFile(arc_name, 'w') as zipf:
                for root, dirs, files in os.walk(td):
                    for fname in files:
                        file_path = os.path.join(root, fname)
                        if file_path != arc_name:
                            zipf.write(file_path, os.path.relpath(file_path, td))
            with open(arc_name, "rb") as f:
                model_data = f.read()
        with open(path, "wb") as f:
            cloudpickle.dump((model_data, self._act_params), f) 
Example #6
Source File: test_reloader.py    From sanic with MIT License 6 votes vote down vote up
def test_reloader_live(runargs, mode):
    with TemporaryDirectory() as tmpdir:
        filename = os.path.join(tmpdir, "reloader.py")
        text = write_app(filename, **runargs)
        proc = Popen(argv[mode], cwd=tmpdir, stdout=PIPE, creationflags=flags)
        try:
            timeout = Timer(5, terminate, [proc])
            timeout.start()
            # Python apparently keeps using the old source sometimes if
            # we don't sleep before rewrite (pycache timestamp problem?)
            sleep(1)
            line = scanner(proc)
            assert text in next(line)
            # Edit source code and try again
            text = write_app(filename, **runargs)
            assert text in next(line)
        finally:
            timeout.cancel()
            terminate(proc)
            with suppress(TimeoutExpired):
                proc.wait(timeout=3) 
Example #7
Source File: algorithms_tests.py    From neural-style-docker with MIT License 6 votes vote down vote up
def test_styletransfer_size():
    """Style transfer works for varying image sizes, producing correctly scaled images"""
    for alg in ALGORITHMS.keys():
        for size in [50, 100, 200]:
            for img in ["docker.png", "obama.jpg"]:
                originalshape = shape(CONTENTS + img)
                tmpdir = TemporaryDirectory()
                styletransfer([CONTENTS + img], [STYLES + "cubism.jpg"], tmpdir.name, alg=alg, size=size)
                files = glob(tmpdir.name + "/" + filename(img) + "*cubism*")
                resultshape = shape(files[0])
                rescalefactor = size / originalshape[0]
                expectedshape = [size, int(rescalefactor * originalshape[1])]
                print("Expected shape", expectedshape)
                print("Actual shape", resultshape)
                assert len(files) == 1
                assert expectedshape == resultshape 
Example #8
Source File: base.py    From vault with MIT License 6 votes vote down vote up
def setUpClass(cls):
        # Set db location
        file_ = tempfile.NamedTemporaryFile(delete=False)
        global_scope['db_file'] = file_.name

        # Create a user key
        cls.secret_key = str(uuid.uuid4())
        cls.enc = global_scope['enc'] = Encryption(cls.secret_key.encode())

        # Load config
        cls.conf_path = tempfile.TemporaryDirectory()
        cls.config = Config(cls.conf_path.name + '/config')
        global_scope['conf'] = cls.config

        # Create engine
        engine = get_engine()

        # Create tables and set database session
        Base.metadata.create_all(engine)
        Session = sessionmaker(bind=engine)
        cls.session = Session()

        # Populate db
        cls.populate_base() 
Example #9
Source File: serialize_test.py    From dataflow with Apache License 2.0 5 votes vote down vote up
def test_lmdb(self):
        with tempfile.TemporaryDirectory() as f:
            self.run_write_read_test(
                os.path.join(f, 'test.lmdb'),
                LMDBSerializer,
                {}, {},
                {}, {'shuffle': False},
                'Skip test_lmdb, no lmdb available') 
Example #10
Source File: __init__.py    From pyGSTi with Apache License 2.0 5 votes vote down vote up
def temp_path(self, filename=None):
        """Provides a context with the path of a temporary file.

        This is distinct from the contexts provided by tempfile in
        that this method yields the path of the temporary file, so the
        underlying file may be opened or closed inside the context as
        the caller pleases.

        Under the hood, this actually creates the file in a temporary
        directory. This directory will be cleaned up when the context
        closes, including the returned file and any other siblings.

        Parameters
        ----------
        filename: str, optional
            Optionally, the name of the file. By default, one will be
            randomly generated.

        Yields
        ------
        str
            The path of the temporary file.

        See Also
        --------
        ``with_temp_file`` : decorator version
        """

        filename = filename or "temp_file"  # yeah looks random to me
        with TemporaryDirectory() as tmpdir:
            tmp_path = Path(tmpdir) / filename
            # Yield to context with temporary path
            yield str(tmp_path)
            # TemporaryDirectory will be cleaned up on close 
Example #11
Source File: test_misc.py    From vault with MIT License 5 votes vote down vote up
def test_create_directory_if_missing_2(self):
        # When the folder is missing
        with tempfile.TemporaryDirectory() as dir_:
            self.assertTrue(misc.create_directory_if_missing(
                dir_ + '/some/dir')) 
Example #12
Source File: test_misc.py    From vault with MIT License 5 votes vote down vote up
def test_create_directory_if_missing(self):
        # When the folder exists
        with tempfile.TemporaryDirectory() as dir_:
            self.assertFalse(misc.create_directory_if_missing(
                dir_)) 
Example #13
Source File: test_import_export.py    From vault with MIT License 5 votes vote down vote up
def test_save_file_2(self):
        # Create a temporary directory
        dir_ = tempfile.TemporaryDirectory()

        self.assertFalse(import_export.save_file(
            dir_.name + '/non/existent/file', 'some content'))

        # Cleanup dir
        dir_.cleanup() 
Example #14
Source File: test_import_export.py    From vault with MIT License 5 votes vote down vote up
def test_save_file(self):
        # Create a temporary directory
        dir_ = tempfile.TemporaryDirectory()

        self.assertTrue(import_export.save_file(
            dir_.name + '/file', 'some content'))

        # Cleanup dir
        dir_.cleanup() 
Example #15
Source File: test_vault.py    From vault with MIT License 5 votes vote down vote up
def test_initialize_6(self, patched, patched2):
        # Test export

        patched.return_value = 'patched'
        patched2.return_value = 'patched'

        # Set temporary files
        dir_ = tempfile.TemporaryDirectory()

        self.assertIsNone(vault.initialize(dir_.name + '/some/new/file',
                                           self.conf_path.name + '/config')) 
Example #16
Source File: package.py    From apsconnect-cli with Apache License 2.0 5 votes vote down vote up
def __init__(self, source, instance_only=False):
        self.instance_only = instance_only
        with TemporaryDirectory() as tempdir:
            self.source = source
            self.tempdir = tempdir
            if self.is_http:
                self.filename = Package._download(self.source, self.tempdir)
            else:
                self.filename = os.path.basename(self.source)
                copyfile(os.path.expanduser(self.source), os.path.join(self.tempdir, self.filename))
            self.path = os.path.join(self.tempdir, self.filename)
            with open(self.path, 'rb') as f:
                self.body = xmlrpclib.Binary(f.read())
            self._extract_files()
            self._parse_metadata() 
Example #17
Source File: serialize_test.py    From dataflow with Apache License 2.0 5 votes vote down vote up
def test_hdf5(self):
        args = [['label', 'image']]
        with tempfile.TemporaryDirectory() as f:
            self.run_write_read_test(
                os.path.join(f, 'test.h5'),
                HDF5Serializer,
                args, {},
                args, {'shuffle': False},
                'Skip test_hdf5, no h5py available') 
Example #18
Source File: serialize_test.py    From dataflow with Apache License 2.0 5 votes vote down vote up
def test_numpy(self):
        with tempfile.TemporaryDirectory() as f:
            self.run_write_read_test(
                os.path.join(f, 'test.npz'),
                NumpySerializer,
                {}, {},
                {}, {'shuffle': False},
                'Skip test_numpy, no numpy available') 
Example #19
Source File: algorithms_tests.py    From neural-style-docker with MIT License 5 votes vote down vote up
def test_alpha():
    """Transformation of images with an alpha channel preserve transparency"""
    tmpdir = TemporaryDirectory()
    # Transform image with alpha
    styletransfer([CONTENTS + "dockersmallalpha.png"], [STYLES + "cubism.jpg"], tmpdir.name, alg="chen-schmidt-inverse")
    assert len(glob(tmpdir.name + "/*dockersmallalpha_cubism*")) == 1
    # Transform image without alpha
    styletransfer([CONTENTS + "dockersmall.png"], [STYLES + "cubism.jpg"], tmpdir.name, alg="chen-schmidt-inverse")
    assert len(glob(tmpdir.name + "/*dockersmall_cubism*")) == 1
    # Check correct that generated image are different
    assertalldifferent(tmpdir.name + "/*cubism*") 
Example #20
Source File: serialize_test.py    From dataflow with Apache License 2.0 5 votes vote down vote up
def test_tfrecord(self):
        with tempfile.TemporaryDirectory() as f:
            self.run_write_read_test(
                os.path.join(f, 'test.tfrecord'),
                TFRecordSerializer,
                {}, {},
                {}, {'size': 32},
                'Skip test_tfrecord, no tensorflow available') 
Example #21
Source File: conftest.py    From interpret-text with MIT License 5 votes vote down vote up
def tmp(tmp_path_factory):
    td = TemporaryDirectory(dir=tmp_path_factory.getbasetemp())
    try:
        yield td.name
    finally:
        td.cleanup() 
Example #22
Source File: test_config.py    From sanic with MIT License 5 votes vote down vote up
def temp_path():
    """ a simple cross platform replacement for NamedTemporaryFile """
    with TemporaryDirectory() as td:
        yield Path(td, "file") 
Example #23
Source File: algorithms_tests.py    From neural-style-docker with MIT License 5 votes vote down vote up
def test_neuraltile():
    """The neural tiling procedure can be run without issues"""
    tmpdir = TemporaryDirectory()
    content = CONTENTS + "avila-walls.jpg"
    outfile = tmpdir.name + "/tiled.png"
    neuraltile(content, STYLES + "cubism.jpg", outfile, alg="chen-schmidt-inverse", overlap=100)
    assert shape(outfile) == shape(content) 
Example #24
Source File: imagemagick_tests.py    From neural-style-docker with MIT License 5 votes vote down vote up
def test_equalimages():
    """Image comparison works"""
    tmpdir = TemporaryDirectory()
    # Compare different images
    assert equalimages(CONTENTS + "docker.png", CONTENTS + "goldengate.jpg") is False
    # Compare equal images
    copied = tmpdir.name + "/docker.png"
    copyfile(CONTENTS + "docker.png", copied)
    assert equalimages(CONTENTS + "docker.png", copied) is True 
Example #25
Source File: imagemagick_tests.py    From neural-style-docker with MIT License 5 votes vote down vote up
def test_convert_nolayers():
    """Convert a single image with no layers works as expected"""
    for content in [CONTENTS + f for f in ["docker.png", "goldengate.jpg"]]:
        for ext in [".png", ".jpg", ".psd", ".tga"]:
            tmpdir = TemporaryDirectory()
            outname = tmpdir.name + "/" + "output" + ext
            convert(content, outname)
            assert len(glob(tmpdir.name + "/" + filename(outname) + ext)) == 1
            assert shape(outname) == shape(content) 
Example #26
Source File: imagemagick_tests.py    From neural-style-docker with MIT License 5 votes vote down vote up
def test_mergealpha():
    """Extracting the alpha channel from an image, then merging it back, produces the same image"""
    contents = [CONTENTS + imname for imname in ["alphasample.png", "docker.png"]]
    for content in contents:
        tmpdir = TemporaryDirectory()
        alphafile = tmpdir.name + "/alpha.png"
        rgbfile = tmpdir.name + "/rgb.png"
        extractalpha(content, rgbfile, alphafile)
        recfile = tmpdir.name + "/reconstructed.png"
        mergealpha(rgbfile, alphafile, recfile)
        assert equalimages(content, recfile) 
Example #27
Source File: imagemagick_tests.py    From neural-style-docker with MIT License 5 votes vote down vote up
def test_extractalpha():
    """Extracting the alpha channel from an image works as expected"""
    tmpdir = TemporaryDirectory()
    img = CONTENTS + "/alphasample.png"
    alphafile = tmpdir.name + "/alpha.png"
    rgbfile = tmpdir.name + "/rgb.png"
    extractalpha(img, rgbfile, alphafile)
    assert shape(rgbfile) == shape(img)
    assert shape(alphafile) == shape(img)
    assert not equalimages(img, rgbfile)
    assert not equalimages(img, alphafile)
    assert not equalimages(rgbfile, alphafile) 
Example #28
Source File: imagemagick_tests.py    From neural-style-docker with MIT License 5 votes vote down vote up
def test_choptiles():
    """Chopping an image into tiles works as expected"""
    tmpdir = TemporaryDirectory()
    content = CONTENTS + "/goldengate.jpg"
    tiles = choptiles(content, xtiles=2, ytiles=3, overlap=50, outname=tmpdir.name + "/tiles")
    print("Generated tiles", tiles)
    assert len(tiles) == 6
    for i, tile in enumerate(tiles):
        assert int(filename(tile).split("_")[-1]) == i 
Example #29
Source File: imagemagick_tests.py    From neural-style-docker with MIT License 5 votes vote down vote up
def test_resize_changeproportions():
    """Resizing an image changing proportions works correctly"""
    tmpdir = TemporaryDirectory()
    fname = tmpdir.name + "/docker.png"
    copyfile(CONTENTS + "docker.png", fname)
    resize(fname, [700, 300])
    assert shape(fname) == [700, 300] 
Example #30
Source File: imagemagick_tests.py    From neural-style-docker with MIT License 5 votes vote down vote up
def test_resize_keepproportions():
    """Resizing an image without changing proportions works correctly"""
    tmpdir = TemporaryDirectory()
    fname = tmpdir.name + "/docker.png"
    copyfile(CONTENTS + "docker.png", fname)
    resize(fname, 1016)
    assert shape(fname) == [1016, 886]