我们从Python开源项目中,提取了以下49个代码示例,用于说明如何使用string.ascii_letters()。
def hexdump(src, length=16, sep='.'): """ Displays a hex output of the content it is passed. This was based on https://gist.github.com/7h3rAm/5603718 with some minor modifications """ allowed = digits + ascii_letters + punctuation + ' ' print_map = ''.join(((x if x in allowed else '.') for x in map(chr, range(256)))) lines = [] for c in xrange(0, len(src), length): chars = src[c:c + length] hex = ' '.join(["%02x" % ord(x) for x in chars]) if len(hex) > 24: hex = "%s %s" % (hex[:24], hex[24:]) printable = ''.join(["%s" % ( (ord(x) <= 127 and print_map[ord(x)]) or sep) for x in chars]) lines.append("%08x: %-*s |%s|" % (c, length * 3, hex, printable)) return '\n'.join(lines)
def clean_filename(filename): """Return a sanitized filename (replace / strip out illegal characters) :param filename: string used for a filename :type filename: str :return: sanitized filename :rtype: str """ return ''.join([ c for c in unicodedata.normalize( 'NFKD', ''.join([REPLACEMENT_CHAR.get(c, c) for c in filename]) ) if not unicodedata.combining(c) and c in '-_.() {0}{1}'.format(string.ascii_letters, string.digits) ])
def generate_article_url(self, response): as_id = ''.join(random.sample(string.ascii_letters + string.digits, 15)) cp_id = ''.join(random.sample(string.ascii_letters + string.digits, 15)) yield scrapy.Request( "http://www.toutiao.com/api/pc/feed/?category=news_tech&utm_source=toutiao&widen=1&max_behot_time=0" + "max_behot_time_tmp=" + str(int(time.time())) + "tadrequire=true&as=" + as_id + "&cp=" + cp_id + "&t=" + str(time.time()), callback=self.generate_article_url ) article_list = json.loads(response.body) if article_list.get("message") != "success": return for article_detail in article_list.get('data'): # wenda gallery ad ? # news_tech and news_finance tag_url = article_detail.get('tag_url') if article_detail.get('article_genre') == 'article'\ and (tag_url == 'news_tech' or tag_url == 'news_finance'): yield scrapy.Request( self.toutiao_url_pre + article_detail.get('source_url'), callback=self.generate_article_content )
def randompdf (path) : numpdf = (randint(1500,2000)) for i in range(10): name = path + ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(randint(5,15))]) + ".pdf" numwords = (randint(200,1000)) pdf = FPDF() pdf.add_page() pdf.set_font("Arial", size=12) words =[] for i in range(numwords): randomword = ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(randint(5,15))]) words.append(randomword) wordsinstring = ''.join(words) pdf.cell(200, 10, txt=wordsinstring, align="C") pdf.output(name) for i in range(numpdf): dupli = path + ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(randint(5,15))]) + ".pdf" copyfile(name, dupli)
def rword(n=10): return "".join(np.random.choice(list(string.ascii_letters), n))
def transition_function(P): """ The main principle on building the transition function is to think about the fact that every time we scan a new character from the input sequence the suffix should match with the prefix of the pattern. If that is not possible for every length of the suffix, the next state need to be the initial, otherwise the length of the suffix that matches properly will be exactly the next state. """ alphabet = st.ascii_letters+st.punctuation+st.digits+st.whitespace m = len(P) trans = [{c:0 for c in alphabet} for i in range(m)] for s in range(m): for c in alphabet: k = min(m, s+1) while (P[:s]+c)[-k:] != P[:k]: k-=1 trans[s][c]=k return trans
def set_nickname(self, nickname): nickname = nickname.strip() # Do some basic validation of the nickname if len(nickname) > 19: logging.warning("Bad nickname length %d: %s", len(nickname), nickname) return False if not all(c in (string.ascii_letters + string.digits) for c in nickname): logging.warning("Bad nickname characters: %s", nickname) return False # Are we replacing an existing nickname? if self.nickname is not None: if self.nickname != nickname: logging.warning("Replacing nickname %s with %s", self.nickname, nickname) else: logging.debug("Duplicate nickname received %s", nickname) self.nickname = nickname return True
def next(self, agents_input): if self.dot_skipped: self.mistake_done = False self.dot_skipped = False return '.' output = self.correct_learner.next(agents_input) if output == ' ': return ' ' if output == '.': if self.mistake_done: self.mistake_done = False return '.' else: self.dot_skipped = True return random.choice(string.ascii_letters) if random.random() < self.error_rate: self.mistake_done = True return random.choice(string.ascii_letters) return output
def _copy_from(self, curs, nrecs, srec, copykw): f = StringIO() for i, c in zip(range(nrecs), cycle(string.ascii_letters)): l = c * srec f.write("%s\t%s\n" % (i, l)) f.seek(0) curs.copy_from(MinimalRead(f), "tcopy", **copykw) curs.execute("select count(*) from tcopy") self.assertEqual(nrecs, curs.fetchone()[0]) curs.execute("select data from tcopy where id < %s order by id", (len(string.ascii_letters),)) for i, (l,) in enumerate(curs): self.assertEqual(l, string.ascii_letters[i] * srec)
def _copy_from(self, curs, nrecs, srec, copykw): f = StringIO() for i, c in izip(xrange(nrecs), cycle(string.ascii_letters)): l = c * srec f.write("%s\t%s\n" % (i, l)) f.seek(0) curs.copy_from(MinimalRead(f), "tcopy", **copykw) curs.execute("select count(*) from tcopy") self.assertEqual(nrecs, curs.fetchone()[0]) curs.execute("select data from tcopy where id < %s order by id", (len(string.ascii_letters),)) for i, (l,) in enumerate(curs): self.assertEqual(l, string.ascii_letters[i] * srec)
def get_newname(path): def conv_char(ch): safe_char = string.ascii_letters + string.digits + "-_" if ch in safe_char: return ch return "_" # if path == ".": return path dirname, basename = os.path.split(path) base, ext = os.path.splitext(basename) newbase = "".join(map(conv_char, base)) if basename == newbase+ext: return os.path.join(dirname, basename) if os.path.exists("%s/%s%s" % (dirname, newbase, ext)): i = 0 while os.path.exists("%s/%s_%d%s" % (dirname, newbase, i, ext)): i += 1 newbase += "_%d" % i newname = "%s/%s%s" % (dirname, newbase, ext) return newname
def get_random_string(length=50): """ Returns a securely generated random string. The default length of 12 with the a-z, A-Z, 0-9 character set returns a 71-bit value. log_2((26+26+10)^12) =~ 71 bits """ punctuation = string.punctuation.replace('"', '').replace("'", '') punctuation = punctuation.replace('\\', '') if using_sysrandom: return ''.join(random.choice( string.digits + string.ascii_letters + punctuation ) for i in range(length)) print( "Cookiecutter Django couldn't find a secure pseudo-random number generator on your system." " Please change change your SECRET_KEY variables in conf/settings/local.py and env.example" " manually." ) return "CHANGEME!!"
def random_alphabetical_string(self, maxlen = 1024, exact = False): """ Filenames are usually rejected if they contain funky characters, blocking execution """ if exact: string_len = maxlen else: string_len = random.randint(1, maxlen) alphabet = string.ascii_letters + string.digits s = ''.join(random.choice(alphabet) for _ in range(string_len)) return s
def test_send_batch_chunk(self, mock_post): """ Test that MailgunClient.send_batch chunks recipients """ chunk_size = 10 recipient_tuples = [("{0}@example.com".format(letter), None) for letter in string.ascii_letters] chunked_emails_to = [recipient_tuples[i:i + chunk_size] for i in range(0, len(recipient_tuples), chunk_size)] assert len(recipient_tuples) == 52 responses = MailgunClient.send_batch('email subject', 'email body', recipient_tuples, chunk_size=chunk_size) assert mock_post.called assert mock_post.call_count == 6 for call_num, args in enumerate(mock_post.call_args_list): called_args, called_kwargs = args assert list(called_args)[0] == '{}/{}'.format(settings.MAILGUN_URL, 'messages') assert called_kwargs['data']['text'].startswith('email body') assert called_kwargs['data']['subject'] == 'email subject' assert sorted(called_kwargs['data']['to']) == sorted([email for email, _ in chunked_emails_to[call_num]]) assert called_kwargs['data']['recipient-variables'] == json.dumps( {email: context or {} for email, context in chunked_emails_to[call_num]} ) response = responses[call_num] assert response.status_code == HTTP_200_OK
def test_send_batch_400_no_raise(self, mock_post): """ Test that if raise_for_status is False we don't raise an exception for a 400 response """ mock_post.return_value = Mock( spec=Response, status_code=HTTP_400_BAD_REQUEST, json=mocked_json() ) chunk_size = 10 recipient_tuples = [("{0}@example.com".format(letter), None) for letter in string.ascii_letters] assert len(recipient_tuples) == 52 with override_settings( MAILGUN_RECIPIENT_OVERRIDE=None, ): resp_list = MailgunClient.send_batch( 'email subject', 'email body', recipient_tuples, chunk_size=chunk_size, raise_for_status=False ) assert len(resp_list) == 6 for resp in resp_list: assert resp.status_code == HTTP_400_BAD_REQUEST assert mock_post.call_count == 6 assert mock_post.return_value.raise_for_status.called is False
def make_binary_entry_filename(table, key): # Assume the random string has been found, until it's not been found. random_string_found = True while random_string_found: random_string = "".join( [random.choice(string.ascii_letters) for x in range(50)]) for d in os.listdir(pyconfig.get('db_bin_path')): if random_string in d: break else: random_string_found = False primary_key = "_".join(table.table.table.primary_key.columns.keys()) return os.path.join( pyconfig.get('db_bin_path'), "{}_{}_{}_{}.dbbin".format( table.table_name, primary_key, key, random_string))
def add_todo(self, msg): u_id = msg['from']['id'] args = get_args(msg) if args: todo_str = ' '.join(args) todo_list = todo_str.split(';') with self.todo_table as table: for todo in todo_list: r_id = "".join( [ random.choice(string.ascii_letters) for x in range(4) ]) table.insert(dict( uid = u_id, item = todo, rid = r_id)) await self.sender.sendMessage('To-do list updated') await self.get_todo(msg) else: await self.sender.sendMessage("No arguments given.")
def split(p): output = os.path.join(get_data_home(), "kddcup.parq") if not os.path.exists(output): dtype = { 1: 'category', 2: 'category', 3: 'category', 41: 'category', } df = pd.read_csv(p, header=None, dtype=dtype) cat_cols = df.select_dtypes(include=['category']).columns df[cat_cols] = df[cat_cols].apply(lambda col: col.cat.codes) df.columns = list(string.ascii_letters[:len(df.columns)]) ddf = dd.from_pandas(df, npartitions=16) ddf.to_parquet(output) return output
def test_multiple_pagination(self): # Ensure multiple pagination works correctly. letters = string.ascii_letters template = ( '{% $tagname 10,20 objects %}' '{% $tagname 1 items using items_page %}' '{% $tagname 5 entries.all using "entries" as myentries %}' ) _, context = self.render( self.request(page=2, entries=3), template, objects=range(47), entries={'all': letters}, items=['foo', 'bar'], items_page='p') self.assertRangeEqual(range(10, 30), context['objects']) self.assertSequenceEqual(['foo'], context['items']) self.assertSequenceEqual(letters[10:15], context['myentries']) self.assertSequenceEqual(letters, context['entries']['all'])
def shave_marks_latin(txt): """Remove all diacritic marks from Latin base characters""" norm_txt = unicodedata.normalize('NFD', txt) # <1> latin_base = False keepers = [] for c in norm_txt: if unicodedata.combining(c) and latin_base: # <2> continue # ignore diacritic on Latin base char keepers.append(c) # <3> # if it isn't combining char, it's a new base char if not unicodedata.combining(c): # <4> latin_base = c in string.ascii_letters shaved = ''.join(keepers) return unicodedata.normalize('NFC', shaved) # <5> # END SHAVE_MARKS_LATIN # BEGIN ASCIIZE
def Transpose(onnx_node, ng_inputs): # type: (NodeWrapper, List[TensorOp]) -> Op """Transpose the input tensor similar to numpy.transpose. By default, reverse the dimensions, but if `perm` attribute is specified permute the axes according to the values given. """ data = ng_inputs[0] permute_axes = onnx_node.get_attribute_value('perm') if permute_axes: input_template = ''.join([ascii_letters[i] for i in range(len(data.axes))]) output_template = ''.join([ascii_letters[i] for i in permute_axes]) ng_op = reorder_axes(data, input_template, output_template) else: ng_op = ng.Transpose(data) return cast_to_pos_axes(ng_op)
def _call_api(self, path, video_id, note, data=None): base_url = self._API_DOMAIN + '/core/' + path encoded_query = compat_urllib_parse_urlencode({ 'oauth_consumer_key': self._API_PARAMS['oAuthKey'], 'oauth_nonce': ''.join([random.choice(string.ascii_letters) for _ in range(32)]), 'oauth_signature_method': 'HMAC-SHA1', 'oauth_timestamp': int(time.time()), 'oauth_version': '1.0', }) headers = self.geo_verification_headers() if data: data = json.dumps(data).encode() headers['Content-Type'] = 'application/json' method = 'POST' if data else 'GET' base_string = '&'.join([method, compat_urlparse.quote(base_url, ''), compat_urlparse.quote(encoded_query, '')]) oauth_signature = base64.b64encode(hmac.new( (self._API_PARAMS['oAuthSecret'] + '&').encode('ascii'), base_string.encode(), hashlib.sha1).digest()).decode() encoded_query += '&oauth_signature=' + compat_urlparse.quote(oauth_signature, '') return self._download_json( '?'.join([base_url, encoded_query]), video_id, note='Downloading %s JSON metadata' % note, headers=headers, data=data)
def pwgen(length=None): """Generate a random pasword.""" if length is None: # A random length is ok to use a weak PRNG length = random.choice(range(35, 45)) alphanumeric_chars = [ l for l in (string.ascii_letters + string.digits) if l not in 'l0QD1vAEIOUaeiou'] # Use a crypto-friendly PRNG (e.g. /dev/urandom) for making the # actual password random_generator = random.SystemRandom() random_chars = [ random_generator.choice(alphanumeric_chars) for _ in range(length)] return(''.join(random_chars))
def random_string(length): pool = string.ascii_letters + string.digits return ''.join(random.choice(pool) for i in range(length))
def create_rand_string(length): return ''.join(random.choice(string.ascii_letters + string.digits + " ") for _ in range(length))
def rand_name(size=8, chars=string.ascii_letters): return ''.join(random.choice(chars) for x in xrange(1, size))
def dump_flash_page (self, page_num): ''' Print one page of flash contents. ''' with self._start_communication_with_board(): address = 512 * page_num print('Page number: {} ({:#08x})'.format(page_num, address)) flash = self.channel.read_range(address, 512) def chunks(l, n): for i in range(0, len(l), n): yield l[i:i + n] def dump_line (addr, bytes): k = binascii.hexlify(bytes).decode('utf-8') b = ' '.join(list(chunks(k, 2))) if len(b) >= 26: # add middle space b = '{} {}'.format(b[0:24], b[24:]) printable = string.ascii_letters + string.digits + string.punctuation + ' ' t = ''.join([chr(i) if chr(i) in printable else '.' for i in bytes]) print('{:08x} {} |{}|'.format(addr, b, t)) for i,chunk in enumerate(chunks(flash, 16)): dump_line(address+(i*16), chunk) ############################################################################ ## Internal Helper Functions for Communicating with Boards ############################################################################
def url2pathname(url): """OS-specific conversion from a relative URL of the 'file' scheme to a file system path; not recommended for general use.""" # e.g. # ///C|/foo/bar/spam.foo # becomes # C:\foo\bar\spam.foo import string, urllib # Windows itself uses ":" even in URLs. url = url.replace(':', '|') if not '|' in url: # No drive specifier, just convert slashes if url[:4] == '////': # path is something like ////host/path/on/remote/host # convert this to \\host\path\on\remote\host # (notice halving of slashes at the start of the path) url = url[2:] components = url.split('/') # make sure not to convert quoted slashes :-) return urllib.unquote('\\'.join(components)) comp = url.split('|') if len(comp) != 2 or comp[0][-1] not in string.ascii_letters: error = 'Bad URL: ' + url raise IOError, error drive = comp[0][-1].upper() path = drive + ':' components = comp[1].split('/') for comp in components: if comp: path = path + '\\' + urllib.unquote(comp) # Issue #11474: url like '/C|/' should convert into 'C:\\' if path.endswith(':') and url.endswith('/'): path += '\\' return path
def _random_file_name(self): randfile = ''.join([random.choice(string.ascii_letters + string.digits) for _ in range(12)]) if self.os_type == 1: workdir = 'c:\\windows\\temp' else: workdir = '/tmp' return self.path_join(workdir, 'cblr.%s.tmp' % (randfile,))
def get_random_string(length=10): """ Quick-n-dirty random string generation. """ chars = string.ascii_letters + string.digits return ''.join([random.choice(chars) for _ in range(length)])
def GenPassword(length=8,chars=string.ascii_letters+string.digits): return ''.join([choice(chars) for i in range(length)])
def generate_password(): """ Generates the random temporary password. :rtype str: The new temp password. """ return ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(PASSWORD_LENGTH)) # ----------------------------------------------------------------------------------------------------------------------
def _ARM_GetMnemonic(self, insn): fmt = insn['format'] res = '' for c in fmt: if not c in string.ascii_letters+string.digits: break res += c return res
def make_random(self, nchars): alphabet = string.ascii_letters + string.digits return ''.join(random.choice(alphabet) for _ in range(nchars))
def make_random(nchars): alphabet = string.ascii_letters + string.digits return ''.join(random.choice(alphabet) for _ in range(nchars))
def random_string(): length = random.randint(1, 10) return "".join(random.choice(string.ascii_letters) for _ in range(length)) # test lambda functions
def create_temp_dir(self): """ Create a temp directory. :return: Directory name of the created temp directory. """ dir_name = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(16)) dir_name = os.path.join("/tmp", dir_name) self.run_command('rm -rf ' + str(dir_name)) self.run_command('mkdir -p ' + str(dir_name)) return dir_name
def randomxls (path) : numxls = (randint(2000,5000)) for i in range(10): name = path + ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(randint(5,15))]) + ".xlsx" workbook = xlsxwriter.Workbook(name) worksheet = workbook.add_worksheet() numrows = (randint(100,500)) for i in range(numrows): coord = 'A' + str(i) textinrow = ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(randint(5,15))]) worksheet.write(coord , textinrow) workbook.close() for i in range(numxls): dupli = path + ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(randint(5,15))]) + ".xlsx" copyfile(name, dupli) #PDF Files
def is_valid_service_name(name): valid_syms = string.ascii_letters + '-_' + string.digits return set(name).issubset(valid_syms)
def generate_path(start=None): return ''.join(random.choice(ascii_letters) for _ in range(5))
def generate_key(length): return ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(length))
def _get_random(self, length=64): valid_chars = string.ascii_letters + string.digits + '-' + '_' return ''.join(random.SystemRandom().choice(valid_chars) for x in range(length))
def create_process(self, command_string): # process is: # - create a temporary file name # - create the process, writing output to a temporary file # - wait for the process to complete # - get the temporary file from the endpoint # - delete the temporary file randfile = ''.join([random.choice(string.ascii_letters + string.digits) for _ in range(12)]) workdir = 'c:\\windows\\carbonblack' randfilename = '%s\\cblr.%s.tmp' % (workdir, randfile) session_id = self.live_response_session url = "%s/api/v1/cblr/session/%d/command" % (self.cb.server, session_id) data = {"session_id": session_id, "name": "create process", "object": command_string, "wait": True, "working_directory": workdir, "output_file": randfilename} r = requests.post(url, headers=self.cb.token_header, data=json.dumps(data), verify=self.cb.ssl_verify, timeout=120) r.raise_for_status() resp = r.json() command_id = resp.get('id') command_state = 'pending' while command_state != 'complete': time.sleep(.2) resp = self.cb.live_response_session_command_get(session_id, command_id) command_state = resp.get('status') # now the file is ready to be read file_content = self.get_file(randfilename) # delete the file self.cb.live_response_session_command_post(session_id, "delete file", randfilename) return file_content
def no_re_matches(re_pattern, length=5, max_attempts = 100): for count in range(max_attempts): try_string = ''.join(random.choice(string.ascii_letters) for x in range(length)) if re_search_better(re_pattern, try_string) is None: return try_string raise ValueUnitsError('Non-matching pattern cannot be found') # abstracts the sub-formula contained in parentheses to a unique variable name # if there are any variables already in formula they need to be selected by the re pattern variables_re so that duplicate variable names are not created # returns the formula with all the new variables in it and a dictionary with the new variables as keys and the origional expression as the values