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)
python类ascii_letters()的实例源码
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
post_gen_project.py 文件源码
项目:cookiecutter-django-reactjs
作者: genomics-geek
项目源码
文件源码
阅读 33
收藏 0
点赞 0
评论 0
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 random_string(length):
pool = string.ascii_letters + string.digits
return ''.join(random.choice(pool) for i in range(length))
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 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 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 rand_name(size=8, chars=string.ascii_letters):
return ''.join(random.choice(chars) for x in xrange(1, size))