def test_retval(self):
# _get_candidate_names returns a _RandomNameSequence object
obj = tempfile._get_candidate_names()
self.assertIsInstance(obj, tempfile._RandomNameSequence)
python类_get_candidate_names()的实例源码
def test_same_thing(self):
# _get_candidate_names always returns the same object
a = tempfile._get_candidate_names()
b = tempfile._get_candidate_names()
self.assertTrue(a is b)
def _mock_candidate_names(*names):
return support.swap_attr(tempfile,
'_get_candidate_names',
lambda: iter(names))
def test_retval(self):
# _get_candidate_names returns a _RandomNameSequence object
obj = tempfile._get_candidate_names()
self.assertIsInstance(obj, tempfile._RandomNameSequence)
def test_same_thing(self):
# _get_candidate_names always returns the same object
a = tempfile._get_candidate_names()
b = tempfile._get_candidate_names()
self.assertTrue(a is b)
def _mock_candidate_names(*names):
return support.swap_attr(tempfile,
'_get_candidate_names',
lambda: iter(names))
def test_retval(self):
# _get_candidate_names returns a _RandomNameSequence object
obj = tempfile._get_candidate_names()
self.assertIsInstance(obj, tempfile._RandomNameSequence)
def test_same_thing(self):
# _get_candidate_names always returns the same object
a = tempfile._get_candidate_names()
b = tempfile._get_candidate_names()
self.assertTrue(a is b)
def temp_filename(directory, suffix):
temp_name = next(tempfile._get_candidate_names())
filename = os.path.join(directory, temp_name + suffix)
return filename
# run a 2-ary function on two things -- loop over elements pairwise if the
# things are lists
def setUp(self):
# Get a random temporary file to use for testing
self.tmp_status_file = next(tempfile._get_candidate_names())
self.app.config['HEALTHCHECK_STATUS_FILE'] = self.tmp_status_file
super(HealthViewTestCaseMixin, self).setUp()
def test_retval(self):
# _get_candidate_names returns a _RandomNameSequence object
obj = tempfile._get_candidate_names()
self.assertIsInstance(obj, tempfile._RandomNameSequence)
def test_same_thing(self):
# _get_candidate_names always returns the same object
a = tempfile._get_candidate_names()
b = tempfile._get_candidate_names()
self.assertTrue(a is b)
def _mock_candidate_names(*names):
return support.swap_attr(tempfile,
'_get_candidate_names',
lambda: iter(names))
def get_new_file_name():
# return tempfile._get_candidate_names()
global ci
while True:
ci += 1
ci = ci % max_file_i
yield str(ci)
def get_images_urls(soup):
a = soup.find_all("a",{"class":"gallery__item carousel__item"})
if len(a) == 0:
return []
images_urls = []
for img in a:
h = img.get("href")
parsed = urlparse.urlparse(h)
image = urlparse.parse_qs(parsed.query)['img_url'][0]
images_urls.append(image)
return images_urls
#temp_name = next(tempfile._get_candidate_names())
def getCaptchaImage(self, soup):
i = soup.find("img", { "class" : "form__captcha" })
url= i.get("src")
print soup.find_all("input")
self.last_key = soup.find("input",{"name":"key"})["value"]
self.last_retpath = soup.find("input",{"name":"retpath"})["value"]
fname = "./files/"+next(tempfile._get_candidate_names())+".jpg"
download_image(url, fname)
return fname
def _copy_outside_keys(self):
"""Copy key from out of the workspace into one"""
paths_map = {}
real_inv = os.path.join(self.path, os.readlink(self.inventory))
for line in fileinput.input(real_inv, inplace=True):
key_defs = re.findall(r"ansible_ssh_private_key_file=\/\S+", line)
for key_def in key_defs:
path = key_def.split("=")[-1]
paths_map.setdefault(path, path)
new_line = line.strip()
for mapped_orig, mapped_new in paths_map.iteritems():
if mapped_orig == mapped_new:
keyfilename = os.path.basename(mapped_orig)
rand_part = next(tempfile._get_candidate_names())
new_fname = "{}-{}".format(keyfilename, rand_part)
shutil.copy2(mapped_orig, os.path.join(
self.path, new_fname))
paths_map[mapped_orig] = os.path.join(
self.path_placeholder, new_fname)
new_fname = paths_map[mapped_orig]
else:
new_fname = mapped_new
new_line = re.sub(mapped_orig, new_fname, new_line)
print(new_line)
def generate_tmp_filename(extension):
return tempfile._get_default_tempdir() + "/" + next(tempfile._get_candidate_names()) + "." + extension
def video(self, tensor=None, videofile=None, win=None, env=None, opts=None):
"""
This function plays a video. It takes as input the filename of the video
or a `LxHxWxC` tensor containing all the frames of the video. The function
does not support any plot-specific `options`.
"""
opts = {} if opts is None else opts
opts['fps'] = opts.get('fps', 25)
_assert_opts(opts)
assert tensor is not None or videofile is not None, \
'should specify video tensor or file'
if tensor is not None:
import cv2
import tempfile
assert tensor.ndim == 4, 'video should be in 4D tensor'
videofile = '/tmp/%s.ogv' % next(tempfile._get_candidate_names())
if cv2.__version__.startswith('2'): # OpenCV 2
fourcc = cv2.cv.CV_FOURCC(
chr(ord('T')),
chr(ord('H')),
chr(ord('E')),
chr(ord('O'))
)
elif cv2.__version__.startswith('3'): # OpenCV 3
fourcc = cv2.VideoWriter_fourcc(
chr(ord('T')),
chr(ord('H')),
chr(ord('E')),
chr(ord('O'))
)
writer = cv2.VideoWriter(
videofile,
fourcc,
opts.get('fps'),
(tensor.shape[1], tensor.shape[2])
)
assert writer.isOpened(), 'video writer could not be opened'
for i in range(tensor.shape[0]):
writer.write(tensor[i, :, :, :])
writer.release()
writer = None
extension = videofile.split(".")[-1].lower()
mimetypes = dict(mp4='mp4', ogv='ogg', avi='avi', webm='webm')
mimetype = mimetypes.get(extension)
assert mimetype is not None, 'unknown video type: %s' % extension
bytestr = loadfile(videofile)
videodata = """
<video controls>
<source type="video/%s" src="data:video/%s;base64,%s">
Your browser does not support the video tag.
</video>
""" % (mimetype, mimetype, base64.b64encode(bytestr).decode('utf-8'))
return self.text(text=videodata, win=win, env=env, opts=opts)