Python tempfile._RandomNameSequence() Examples

The following are code examples for showing how to use tempfile._RandomNameSequence(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: geobricks_qgis_plugin_trmm   Author: geobricks   File: gdal_dataset.py    GNU General Public License v2.0 6 votes vote down vote up
def __init__(self,cols,rows,bands,datatype,srs='',gt=[],nodata=[]):
        use_exceptions=gdal.GetUseExceptions()
        gdal.UseExceptions()

        #print cols,rows,bands,datatype,srs,gt,nodata
        if Env.tempdir == '/vsimem':
            #Test to see if enough memory
            tmpdriver=gdal.GetDriverByName('MEM')
            tmpds=tmpdriver.Create('',cols,rows,bands,datatype)
            tmpds=None
            del tmpds

            self._filedescriptor=-1
            self._filename='/vsimem/%s.tif'%tempfile._RandomNameSequence().next()

        else:
            self._filedescriptor,self._filename=tempfile.mkstemp(suffix='.tif')

        NewDataset.__init__(self,self._filename,'GTIFF',
                            cols,rows,bands,datatype,srs,gt,
                            options=Env.tempoptions) 
Example 2
Project: geobricks_qgis_plugin_trmm   Author: geobricks   File: gdal_dataset.py    GNU General Public License v2.0 6 votes vote down vote up
def __init__(self, filepaths, band=0):
        self._datasets=[]#So they don't go out of scope and get GC'd

        #Get a reference dataset so can apply env setting to all datasets
        reference_ds=Dataset(filepaths[0])
        for f in filepaths[1:]:
            d=Dataset(f)
            reference_ds,d=reference_ds.apply_environment(d)

        vrtxml=self.buildvrt(reference_ds, filepaths, band)

        #Temp in memory VRT file
        self._filename='/vsimem/%s.vrt'%tempfile._RandomNameSequence().next()
        self.__write_vsimem__(self._filename,vrtxml)
        self._dataset=gdal.Open(self._filename)

        Dataset.__init__(self) 
Example 3
Project: pyblish-win   Author: pyblish   File: test_tempfile.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def setUp(self):
        self.r = tempfile._RandomNameSequence() 
Example 4
Project: pyblish-win   Author: pyblish   File: test_tempfile.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_get_six_char_str(self):
        # _RandomNameSequence returns a six-character string
        s = self.r.next()
        self.nameCheck(s, '', '', '') 
Example 5
Project: pyblish-win   Author: pyblish   File: test_tempfile.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_many(self):
        # _RandomNameSequence returns no duplicate strings (stochastic)

        dict = {}
        r = self.r
        for i in xrange(TEST_FILES):
            s = r.next()
            self.nameCheck(s, '', '', '')
            self.assertNotIn(s, dict)
            dict[s] = 1 
Example 6
Project: pyblish-win   Author: pyblish   File: test_tempfile.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_supports_iter(self):
        # _RandomNameSequence supports the iterator protocol

        i = 0
        r = self.r
        try:
            for s in r:
                i += 1
                if i == 20:
                    break
        except:
            self.failOnException("iteration") 
Example 7
Project: pyblish-win   Author: pyblish   File: test_tempfile.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_retval(self):
        # _get_candidate_names returns a _RandomNameSequence object
        obj = tempfile._get_candidate_names()
        self.assertIsInstance(obj, tempfile._RandomNameSequence) 
Example 8
Project: NiujiaoDebugger   Author: MrSrc   File: test_tempfile.py    GNU General Public License v3.0 5 votes vote down vote up
def setUp(self):
        self.r = tempfile._RandomNameSequence()
        super().setUp() 
Example 9
Project: NiujiaoDebugger   Author: MrSrc   File: test_tempfile.py    GNU General Public License v3.0 5 votes vote down vote up
def test_get_six_char_str(self):
        # _RandomNameSequence returns a six-character string
        s = next(self.r)
        self.nameCheck(s, '', '', '') 
Example 10
Project: NiujiaoDebugger   Author: MrSrc   File: test_tempfile.py    GNU General Public License v3.0 5 votes vote down vote up
def test_many(self):
        # _RandomNameSequence returns no duplicate strings (stochastic)

        dict = {}
        r = self.r
        for i in range(TEST_FILES):
            s = next(r)
            self.nameCheck(s, '', '', '')
            self.assertNotIn(s, dict)
            dict[s] = 1 
Example 11
Project: NiujiaoDebugger   Author: MrSrc   File: test_tempfile.py    GNU General Public License v3.0 5 votes vote down vote up
def supports_iter(self):
        # _RandomNameSequence supports the iterator protocol

        i = 0
        r = self.r
        for s in r:
            i += 1
            if i == 20:
                break 
Example 12
Project: NiujiaoDebugger   Author: MrSrc   File: test_tempfile.py    GNU General Public License v3.0 5 votes vote down vote up
def test_retval(self):
        # _get_candidate_names returns a _RandomNameSequence object
        obj = tempfile._get_candidate_names()
        self.assertIsInstance(obj, tempfile._RandomNameSequence) 
Example 13
Project: ironpython2   Author: IronLanguages   File: test_tempfile.py    Apache License 2.0 5 votes vote down vote up
def setUp(self):
        self.r = tempfile._RandomNameSequence() 
Example 14
Project: ironpython2   Author: IronLanguages   File: test_tempfile.py    Apache License 2.0 5 votes vote down vote up
def test_get_six_char_str(self):
        # _RandomNameSequence returns a six-character string
        s = self.r.next()
        self.nameCheck(s, '', '', '') 
Example 15
Project: ironpython2   Author: IronLanguages   File: test_tempfile.py    Apache License 2.0 5 votes vote down vote up
def test_many(self):
        # _RandomNameSequence returns no duplicate strings (stochastic)

        dict = {}
        r = self.r
        for i in xrange(TEST_FILES):
            s = r.next()
            self.nameCheck(s, '', '', '')
            self.assertNotIn(s, dict)
            dict[s] = 1 
Example 16
Project: ironpython2   Author: IronLanguages   File: test_tempfile.py    Apache License 2.0 5 votes vote down vote up
def test_supports_iter(self):
        # _RandomNameSequence supports the iterator protocol

        i = 0
        r = self.r
        try:
            for s in r:
                i += 1
                if i == 20:
                    break
        except:
            self.failOnException("iteration") 
Example 17
Project: ironpython2   Author: IronLanguages   File: test_tempfile.py    Apache License 2.0 5 votes vote down vote up
def test_retval(self):
        # _get_candidate_names returns a _RandomNameSequence object
        obj = tempfile._get_candidate_names()
        self.assertIsInstance(obj, tempfile._RandomNameSequence) 
Example 18
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_tempfile.py    GNU General Public License v2.0 5 votes vote down vote up
def setUp(self):
        self.r = tempfile._RandomNameSequence()
        super().setUp() 
Example 19
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_tempfile.py    GNU General Public License v2.0 5 votes vote down vote up
def test_get_six_char_str(self):
        # _RandomNameSequence returns a six-character string
        s = next(self.r)
        self.nameCheck(s, '', '', '') 
Example 20
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_tempfile.py    GNU General Public License v2.0 5 votes vote down vote up
def test_many(self):
        # _RandomNameSequence returns no duplicate strings (stochastic)

        dict = {}
        r = self.r
        for i in range(TEST_FILES):
            s = next(r)
            self.nameCheck(s, '', '', '')
            self.assertNotIn(s, dict)
            dict[s] = 1 
Example 21
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_tempfile.py    GNU General Public License v2.0 5 votes vote down vote up
def supports_iter(self):
        # _RandomNameSequence supports the iterator protocol

        i = 0
        r = self.r
        for s in r:
            i += 1
            if i == 20:
                break 
Example 22
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_tempfile.py    GNU General Public License v2.0 5 votes vote down vote up
def test_retval(self):
        # _get_candidate_names returns a _RandomNameSequence object
        obj = tempfile._get_candidate_names()
        self.assertIsInstance(obj, tempfile._RandomNameSequence) 
Example 23
Project: oss-ftp   Author: aliyun   File: test_tempfile.py    MIT License 5 votes vote down vote up
def setUp(self):
        self.r = tempfile._RandomNameSequence() 
Example 24
Project: oss-ftp   Author: aliyun   File: test_tempfile.py    MIT License 5 votes vote down vote up
def test_get_six_char_str(self):
        # _RandomNameSequence returns a six-character string
        s = self.r.next()
        self.nameCheck(s, '', '', '') 
Example 25
Project: oss-ftp   Author: aliyun   File: test_tempfile.py    MIT License 5 votes vote down vote up
def test_many(self):
        # _RandomNameSequence returns no duplicate strings (stochastic)

        dict = {}
        r = self.r
        for i in xrange(TEST_FILES):
            s = r.next()
            self.nameCheck(s, '', '', '')
            self.assertNotIn(s, dict)
            dict[s] = 1 
Example 26
Project: oss-ftp   Author: aliyun   File: test_tempfile.py    MIT License 5 votes vote down vote up
def test_supports_iter(self):
        # _RandomNameSequence supports the iterator protocol

        i = 0
        r = self.r
        try:
            for s in r:
                i += 1
                if i == 20:
                    break
        except:
            self.failOnException("iteration") 
Example 27
Project: oss-ftp   Author: aliyun   File: test_tempfile.py    MIT License 5 votes vote down vote up
def test_retval(self):
        # _get_candidate_names returns a _RandomNameSequence object
        obj = tempfile._get_candidate_names()
        self.assertIsInstance(obj, tempfile._RandomNameSequence) 
Example 28
Project: mugen   Author: scherroman   File: paths.py    MIT License 5 votes vote down vote up
def generate_temp_file_path(extension: str) -> str:
    return TEMP_PATH_BASE + next(tempfile._RandomNameSequence()) + extension 
Example 29
Project: Hot   Author: dsolimando   File: test_tempfile.py    GNU General Public License v3.0 5 votes vote down vote up
def setUp(self):
        self.r = tempfile._RandomNameSequence() 
Example 30
Project: Hot   Author: dsolimando   File: test_tempfile.py    GNU General Public License v3.0 5 votes vote down vote up
def test_get_six_char_str(self):
        # _RandomNameSequence returns a six-character string
        s = self.r.next()
        self.nameCheck(s, '', '', '') 
Example 31
Project: Hot   Author: dsolimando   File: test_tempfile.py    GNU General Public License v3.0 5 votes vote down vote up
def test_many(self):
        # _RandomNameSequence returns no duplicate strings (stochastic)

        dict = {}
        r = self.r
        for i in xrange(TEST_FILES):
            s = r.next()
            self.nameCheck(s, '', '', '')
            self.failIf(s in dict)
            dict[s] = 1 
Example 32
Project: Hot   Author: dsolimando   File: test_tempfile.py    GNU General Public License v3.0 5 votes vote down vote up
def test_supports_iter(self):
        # _RandomNameSequence supports the iterator protocol

        i = 0
        r = self.r
        try:
            for s in r:
                i += 1
                if i == 20:
                    break
        except:
            failOnException("iteration") 
Example 33
Project: Hot   Author: dsolimando   File: test_tempfile.py    GNU General Public License v3.0 5 votes vote down vote up
def test_retval(self):
        # _get_candidate_names returns a _RandomNameSequence object
        obj = tempfile._get_candidate_names()
        self.assert_(isinstance(obj, tempfile._RandomNameSequence)) 
Example 34
Project: geobricks_qgis_plugin_trmm   Author: geobricks   File: gdal_dataset.py    GNU General Public License v2.0 5 votes vote down vote up
def __init__(self,filepath_or_dataset=None,*args):
        gdal.UseExceptions()

        fp=filepath_or_dataset

        if type(fp) is gdal.Dataset:
            self._dataset = fp
        elif fp is not None:
            if os.path.exists(fp):
                self._dataset = gdal.Open(os.path.abspath(fp),*args)
            else:
                self._dataset = gdal.Open(fp,*args)

        #Issue 8
        self._gt=self.GetGeoTransform()
        if self._gt[5] > 0: #positive NS pixel res.
            tmp_ds = gdal.AutoCreateWarpedVRT(self._dataset)
            tmp_fn = '/vsimem/%s.vrt'%tempfile._RandomNameSequence().next()
            self._dataset = gdal.GetDriverByName('VRT').CreateCopy(tmp_fn,tmp_ds)
            self._gt = self.GetGeoTransform()

        self._x_size=self.RasterXSize
        self._y_size=self.RasterYSize
        self._nbands=self.RasterCount
        self._bands=range(self.RasterCount)
        self._data_type=self.GetRasterBand(1).DataType
        self._srs=self.GetProjectionRef()
        self._block_size=self.GetRasterBand(1).GetBlockSize()
        self._nodata=[b.GetNoDataValue() for b in self]
        
        self.extent=self.__get_extent__() 
Example 35
Project: geobricks_qgis_plugin_trmm   Author: geobricks   File: gdal_dataset.py    GNU General Public License v2.0 5 votes vote down vote up
def __init__(self,dataset_or_band, wkt_srs, snap_ds=None, snap_cellsize=None):

        use_exceptions=gdal.GetUseExceptions()
        gdal.UseExceptions()

        self._simple_fn='/vsimem/%s.vrt'%tempfile._RandomNameSequence().next()
        self._warped_fn='/vsimem/%s.vrt'%tempfile._RandomNameSequence().next()

        try:                   #Is it a Band
            orig_ds=dataset_or_band.dataset._dataset
        except AttributeError: #No, it's a Dataset
            orig_ds=dataset_or_band._dataset

        try: #Generate a warped VRT
            warped_ds=gdal.AutoCreateWarpedVRT(orig_ds,orig_ds.GetProjection(),wkt_srs, Env.resampling)
            #AutoCreateWarpedVRT doesn't create a vsimem filename and we need one
            warped_ds=gdal.GetDriverByName('VRT').CreateCopy(self._warped_fn,warped_ds)

        except Exception as e:
            raise RuntimeError('Unable to project on the fly. '+e.message)

        #Disable the following check as this will allow us to use a WarpedDataset to
        #resample Datasets and creating an AutoCreateWarpedVRT where input srs==output srs
        #will allways fail the test below...
        #if warped_ds.GetGeoTransform()==orig_ds.GetGeoTransform():
        #    raise RuntimeError('Unable to project on the fly. Make sure all input datasets have projections set.')

        if snap_ds:warped_ds=self._modify_vrt(warped_ds, orig_ds, snap_ds, snap_cellsize)
        self._dataset=self._create_simple_VRT(warped_ds,dataset_or_band)

        if not use_exceptions:gdal.DontUseExceptions()
        Dataset.__init__(self) 
Example 36
Project: pony-standup-bot   Author: alexanderad   File: test_storage.py    MIT License 5 votes vote down vote up
def temp_file(self):
        temp_file = os.path.join(
            tempfile.gettempdir(),
            tempfile._RandomNameSequence().next()
        )
        try:
            yield temp_file
        finally:
            os.remove(temp_file) 
Example 37
Project: Project-New-Reign---Nemesis-Main   Author: ShikyoKira   File: test_tempfile.py    GNU General Public License v3.0 5 votes vote down vote up
def setUp(self):
        self.r = tempfile._RandomNameSequence()
        super().setUp() 
Example 38
Project: Project-New-Reign---Nemesis-Main   Author: ShikyoKira   File: test_tempfile.py    GNU General Public License v3.0 5 votes vote down vote up
def test_get_six_char_str(self):
        # _RandomNameSequence returns a six-character string
        s = next(self.r)
        self.nameCheck(s, '', '', '') 
Example 39
Project: Project-New-Reign---Nemesis-Main   Author: ShikyoKira   File: test_tempfile.py    GNU General Public License v3.0 5 votes vote down vote up
def test_many(self):
        # _RandomNameSequence returns no duplicate strings (stochastic)

        dict = {}
        r = self.r
        for i in range(TEST_FILES):
            s = next(r)
            self.nameCheck(s, '', '', '')
            self.assertNotIn(s, dict)
            dict[s] = 1 
Example 40
Project: Project-New-Reign---Nemesis-Main   Author: ShikyoKira   File: test_tempfile.py    GNU General Public License v3.0 5 votes vote down vote up
def supports_iter(self):
        # _RandomNameSequence supports the iterator protocol

        i = 0
        r = self.r
        for s in r:
            i += 1
            if i == 20:
                break 
Example 41
Project: Project-New-Reign---Nemesis-Main   Author: ShikyoKira   File: test_tempfile.py    GNU General Public License v3.0 5 votes vote down vote up
def test_retval(self):
        # _get_candidate_names returns a _RandomNameSequence object
        obj = tempfile._get_candidate_names()
        self.assertIsInstance(obj, tempfile._RandomNameSequence) 
Example 42
Project: cells   Author: AlesTsurko   File: test_tempfile.py    MIT License 5 votes vote down vote up
def setUp(self):
        self.r = tempfile._RandomNameSequence()
        super().setUp() 
Example 43
Project: cells   Author: AlesTsurko   File: test_tempfile.py    MIT License 5 votes vote down vote up
def test_get_six_char_str(self):
        # _RandomNameSequence returns a six-character string
        s = next(self.r)
        self.nameCheck(s, '', '', '') 
Example 44
Project: cells   Author: AlesTsurko   File: test_tempfile.py    MIT License 5 votes vote down vote up
def test_many(self):
        # _RandomNameSequence returns no duplicate strings (stochastic)

        dict = {}
        r = self.r
        for i in range(TEST_FILES):
            s = next(r)
            self.nameCheck(s, '', '', '')
            self.assertNotIn(s, dict)
            dict[s] = 1 
Example 45
Project: cells   Author: AlesTsurko   File: test_tempfile.py    MIT License 5 votes vote down vote up
def supports_iter(self):
        # _RandomNameSequence supports the iterator protocol

        i = 0
        r = self.r
        for s in r:
            i += 1
            if i == 20:
                break 
Example 46
Project: cells   Author: AlesTsurko   File: test_tempfile.py    MIT License 5 votes vote down vote up
def test_retval(self):
        # _get_candidate_names returns a _RandomNameSequence object
        obj = tempfile._get_candidate_names()
        self.assertIsInstance(obj, tempfile._RandomNameSequence) 
Example 47
Project: DockerCli   Author: TMiguelT   File: __init__.py    GNU General Public License v3.0 4 votes vote down vote up
def formulate_command(arguments, no_rm=False, random_tempdir=True, mount_parent=False, cwd=os.getcwd()):
    """
    Generates a docker command, with automatic bind mounting
    """
    # Build the docker command up as two lists
    docker_command = ['docker', 'run']
    container_command = []

    if not no_rm:
        docker_command += ['--rm']

    # We will use this for generating mount locations
    name_gen = tempfile._RandomNameSequence()

    # Convert paths into bind mounts
    for arg in arguments:

        # If it's a file or directory, we need to mount it
        if os.path.isdir(arg) or os.path.isfile(arg):

            arg_path = Path(arg)

            # Make all relative paths absolute
            if not arg_path.is_absolute():
                arg_path = Path(cwd) / arg_path

            # Generate a random temporary directory by default
            if random_tempdir:
                temp_dir = Path(tempfile.gettempdir()) / next(name_gen)
            else:
                temp_dir = Path(tempfile.gettempdir())

            if mount_parent:
                mount_dest = temp_dir / arg_path.parent.name
                docker_command += ['-v', f'{arg_path.parent}:{mount_dest}']
                container_command.append(mount_dest / arg_path.name)
            else:
                mount_dest = temp_dir / arg_path.name
                docker_command += ['-v', f'{arg_path}:{mount_dest}']
                container_command.append(mount_dest)
        else:
            container_command.append(arg)

    # Convert everything back to strings
    return [str(arg) for arg in [*docker_command, *container_command]]