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
############################################################################
python类ascii_letters()的实例源码
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 rword(n=10):
return "".join(np.random.choice(list(string.ascii_letters), n))
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