本文整理汇总了Python中textwrap.indent函数的典型用法代码示例。如果您正苦于以下问题:Python indent函数的具体用法?Python indent怎么用?Python indent使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了indent函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: print_scrape_results_http
def print_scrape_results_http(results):
"""Print the results obtained by "http" method.
Args:
results: The results to be printed to stdout.
"""
for t in results:
for result in t:
logger.info('{} links found. The search with the keyword "{}" yielded the result: "{}"'.format(
len(result['results']), result['search_keyword'], result['num_results_for_kw']))
import textwrap
for result_set in ('results', 'ads_main', 'ads_aside'):
if result_set in result.keys():
print('### {} link results for "{}" ###'.format(len(result[result_set]), result_set))
for link_title, link_snippet, link_url, link_position in result[result_set]:
try:
print(' Link: {}'.format(unquote(link_url.geturl())))
except AttributeError as ae:
print(ae)
if Config['GLOBAL'].getint('verbosity') > 1:
print(
' Title: \n{}'.format(textwrap.indent('\n'.join(textwrap.wrap(link_title, 50)), '\t')))
print(
' Description: \n{}\n'.format(
textwrap.indent('\n'.join(textwrap.wrap(link_snippet, 70)), '\t')))
print('*' * 70)
print()
示例2: check_negative
def check_negative(dirname, results, expected):
"""Checks the results with the expected results."""
inputs = (set(results['inputs']), set(expected['!inputs']))
outputs = (set(results['outputs']), set(expected['!outputs']))
if not inputs[1].isdisjoint(inputs[0]):
print(bcolors.ERROR + ': Found inputs that should not exist:')
s = pprint.pformat(inputs[1] & inputs[1], width=1)
print(textwrap.indent(s, ' '))
return False
if not outputs[1].isdisjoint(outputs[0]):
print(bcolors.ERROR + ': Found outputs that should not exist:')
s = pprint.pformat(outputs[1] & outputs[1], width=1)
print(textwrap.indent(s, ' '))
return False
existing = [p for p in (os.path.join(dirname, p) for p in outputs[1])
if os.path.exists(p)]
if existing:
print(bcolors.ERROR + ': Outputs exist on from file system, but should not:')
pprint.pprint(existing)
return False
return True
示例3: write_options_group
def write_options_group(self, write, group):
def is_positional_group(group):
return any(not o.option_strings for o in group._group_actions)
if is_positional_group(group):
for option in group._group_actions:
write(option.metavar)
write(textwrap.indent(option.help or '', ' ' * 4))
return
opts = OrderedDict()
for option in group._group_actions:
if option.metavar:
option_fmt = '%s ' + option.metavar
else:
option_fmt = '%s'
option_str = ', '.join(option_fmt % s for s in option.option_strings)
option_desc = textwrap.dedent((option.help or '') % option.__dict__)
opts[option_str] = textwrap.indent(option_desc, ' ' * 4)
padding = len(max(opts)) + 1
for option, desc in opts.items():
write(option.ljust(padding), desc)
示例4: pretty_print
def pretty_print(self,m=True,f=False):
#Print the files and folders under root, with their memory usage if m(with_memory)=True
#Only print folders is f(folders_only)=True
base=3
width=self.depth*base+50
DFS=[self.root]
while DFS:
cur_node=DFS.pop()
indent_size=cur_node.dist*base
name=os.path.basename(cur_node.name)
star_size=width-indent_size-len(name)
try:
if m:
print(textwrap.indent(name,''.join([' ']*indent_size)),''.join(['.']*star_size),cur_node.memsize)
else:
print(textwrap.indent(name,''.join([' ']*indent_size)))
except UnicodeEncodeError as err:
logging.debug(err)
for k in cur_node.kiddir:
DFS.append(k)
if not f:
for k in cur_node.kidfile:
indent_size=k.dist*base
name=os.path.basename(k.name)
star_size=width-indent_size-len(name)
try:
if m:
print(textwrap.indent(name,''.join([' ']*indent_size)),''.join(['.']*star_size),k.memsize)
else:
print(textwrap.indent(name,''.join([' ']*indent_size)))
except UnicodeEncodeError as err:
print ('Handling unicode encode error',err)
示例5: play
def play(self):
selection = None
current_sleep = self.start
current_round = 0
while True:
selection = choice_unduplicated(available_notes, selection)
octave, note = selection.split('.')
flute = getFlute(notes[selection])
figlet_note = figlet('{} {}'.format(octave, note))
header = ' Interval: {:.2f} | Sleep: {:.2f} | Round {:d}/{:d}'.format(
self.step, current_sleep, current_round, self.rounds)
flute_padded = indent(flute, ' ' * 14)
figlet_width = len(max(figlet_note.splitlines(), key=len))
note_padded = indent(figlet_note, ' ' * (17 - int(figlet_width/2)))
system('clear')
print(header, "\n\n", note_padded, flute_padded, sep='')
sleep(current_sleep)
current_round += 1
if (current_round >= self.rounds):
current_round = 0
current_sleep -= self.step
if (current_sleep <= self.stop):
break
示例6: check_positive
def check_positive(dirname, results, expected):
"""Checks the results with the expected results."""
inputs = (set(results['inputs']), set(expected['inputs']))
outputs = (set(results['outputs']), set(expected['outputs']))
if not inputs[1].issubset(inputs[0]):
print(bcolors.ERROR + ': Expected inputs are not a subset of the results.')
print(' The following were not found in the results:')
s = pprint.pformat(inputs[1] - inputs[0], width=1)
print(textwrap.indent(s, ' '))
print(' Instead, these were found:')
s = pprint.pformat(inputs[0], width=1)
print(textwrap.indent(s, ' '))
return False
if not outputs[1].issubset(outputs[0]):
print(bcolors.ERROR + ': Expected outputs are not a subset of the results')
print(' The following were not found in the results:')
s = pprint.pformat(outputs[1] - outputs[0], width=1)
print(textwrap.indent(s, ' '))
print(' Instead, these were found:')
s = pprint.pformat(outputs[0], width=1)
print(textwrap.indent(s, ' '))
return False
missing = [p for p in (os.path.join(dirname, p) for p in outputs[0])
if not os.path.exists(p)]
if missing:
print(bcolors.ERROR + ': Result outputs missing from file system:')
pprint.pprint(missing)
return False
return True
示例7: training_desc
def training_desc(info, verbosity, precision):
"""
Textifies a single training history record. Returns a list of lines.
"""
desc = [
"Date: {}".format(date_desc(info['date'])),
"Bot: {bot}".format(**info) +
", Trainer: {trainer} {config}".format(**info),
"Dists: {}".format(indent(dists_desc(info['dists']), " " * 7).strip()),
"Seeds: {}".format(seeds_desc(info['seeds'], verbosity)),
"Level: {}".format(level_desc(info['level'])) +
", Runs: {}".format(info['runs']) +
", Time: {}".format(time_desc(info['time'], precision)),
"Output: {output}, PRNGs: {prngs_seed}".format(**info) +
", Scores: {}".format(scores_desc(info['scores'], verbosity,
precision))
]
if info['phases'] is not None:
desc.insert(3, "Phases: {}".format(
phases_desc(info['phases'], precision)))
if info['emphases']:
desc.insert(3, "Emphases: {}".format(emphases_desc(info['emphases'])))
if info['param_scale']:
desc.insert(5, "Scaled params: {}".format(
param_scale_desc(info['param_scale'])))
if info['param_freeze']:
desc.insert(5, "Frozen params: {}".format(
", ".join(info['param_freeze'])))
if info['param_map']:
desc.insert(5, "Params map: {}".format(
indent(param_map_desc(info['param_map']), " " * 12).strip()))
return desc
示例8: _format_loop_exception
def _format_loop_exception(self, context, n):
message = context.get('message', 'Unhandled exception in event loop')
exception = context.get('exception')
if exception is not None:
exc_info = (type(exception), exception, exception.__traceback__)
else:
exc_info = None
lines = []
for key in sorted(context):
if key in {'message', 'exception'}:
continue
value = context[key]
if key == 'source_traceback':
tb = ''.join(traceback.format_list(value))
value = 'Object created at (most recent call last):\n'
value += tb.rstrip()
else:
try:
value = repr(value)
except Exception as ex:
value = ('Exception in __repr__ {!r}; '
'value type: {!r}'.format(ex, type(value)))
lines.append('[{}]: {}\n\n'.format(key, value))
if exc_info is not None:
lines.append('[exception]:\n')
formatted_exc = textwrap.indent(
''.join(traceback.format_exception(*exc_info)), ' ')
lines.append(formatted_exc)
details = textwrap.indent(''.join(lines), ' ')
return '{:02d}. {}:\n{}\n'.format(n, message, details)
示例9: print_scrape_results_http
def print_scrape_results_http(results, verbosity=1, view=False):
"""Print the results obtained by "http" method."""
for t in results:
for result in t:
logger.info('{} links found! The search with the keyword "{}" yielded the result:{}'.format(
len(result['results']), result['search_keyword'], result['num_results_for_kw']))
if view:
import webbrowser
webbrowser.open(result['cache_file'])
import textwrap
for result_set in ('results', 'ads_main', 'ads_aside'):
if result_set in result.keys():
print('### {} link results for "{}" ###'.format(len(result[result_set]), result_set))
for link_title, link_snippet, link_url, link_position in result[result_set]:
try:
print(' Link: {}'.format(urllib.parse.unquote(link_url.geturl())))
except AttributeError as ae:
print(ae)
if verbosity > 1:
print(
' Title: \n{}'.format(textwrap.indent('\n'.join(textwrap.wrap(link_title, 50)), '\t')))
print(
' Description: \n{}\n'.format(
textwrap.indent('\n'.join(textwrap.wrap(link_snippet, 70)), '\t')))
print('*' * 70)
print()
示例10: __str__
def __str__(self):
if self.left or self.right:
return '{}({},\n{},\n{}\n)'.format(type(self).__name__,
self.data,
textwrap.indent(str(self.left), ' '*4),
textwrap.indent(str(self.right), ' '*4))
return repr(self)
示例11: galaxy
def galaxy(_galaxy):
systems = []
debug('showing the galaxy...')
for c, s in _galaxy._systems.items():
sys = indent(system(s), ' ')[2:]
systems.append('{} {}'.format(c, sys))
return 'Galaxy:\n{}'.format(indent('\n'.join(systems), ' '))
示例12: check
def check():
name = f.__name__
message = ""
if f.__doc__:
message = "\n" + textwrap.indent(f.__doc__, ' """ ')
try:
f()
return True
except AssertionError as e:
if e.args:
message = e.args[0].strip()
exception_class, exception, trace = sys.exc_info()
frames = traceback.extract_tb(trace)
last = frames[len(frames) - 1]
message_hr = textwrap.indent(message, " ")
assertion = "{3}".format(*last)
position = "{0}:{1}".format(*last)
report("{} ({}):".format(name, position))
if message_hr:
report(" --------------------------------")
report("{}".format(message_hr))
report(" --------------------------------")
report(" {}".format(assertion))
report("")
return False
except Exception as e:
report_exc("{}:{}".format(name, message), traceback.format_exc())
return False
示例13: __str__
def __str__(self):
o = self.fmt.format(**self.__dict__)
for k, l in self.lines.items():
o += textwrap.indent(str(l), prefix=k + ' ')
for txt in self.texts:
o += 'with string\n'
o += textwrap.indent(str(txt), prefix=' ')
return o
示例14: status
def status(self, workspacePath):
status = ScmStatus()
try:
onCorrectBranch = False
output = self.callGit(workspacePath, 'ls-remote' ,'--get-url')
if output != self.__url:
status.add(ScmTaint.switched,
"> URL: configured: '{}', actual: '{}'".format(self.__url, output))
if self.__commit:
output = self.callGit(workspacePath, 'rev-parse', 'HEAD')
if output != self.__commit:
status.add(ScmTaint.switched,
"> commit: configured: '{}', actual: '{}'".format(self.__commit, output))
elif self.__tag:
output = self.callGit(workspacePath, 'tag', '--points-at', 'HEAD').splitlines()
if self.__tag not in output:
actual = ("'" + ", ".join(output) + "'") if output else "not on any tag"
status.add(ScmTaint.switched,
"> tag: configured: '{}', actual: '{}'".format(self.__tag, actual))
elif self.__branch:
output = self.callGit(workspacePath, 'rev-parse', '--abbrev-ref', 'HEAD')
if output != self.__branch:
status.add(ScmTaint.switched,
"> branch: configured: '{}', actual: '{}'".format(self.__branch, output))
else:
output = self.callGit(workspacePath, 'log', '--oneline',
'refs/remotes/origin/'+self.__branch+'..HEAD')
if output:
status.add(ScmTaint.unpushed_main,
joinLines("> unpushed commits on {}:".format(self.__branch),
indent(output, ' ')))
onCorrectBranch = True
# Check for modifications wrt. checked out commit
output = self.callGit(workspacePath, 'status', '--porcelain')
if output:
status.add(ScmTaint.modified, joinLines("> modified:",
indent(output, ' ')))
# The following shows all unpushed commits reachable by any ref
# (local branches, stash, detached HEAD, etc).
# Exclude HEAD if the configured branch is checked out to not
# double-count them. Does not mark the SCM as dirty.
what = ['--all', '--not', '--remotes']
if onCorrectBranch: what.append('HEAD')
output = self.callGit(workspacePath, 'log', '--oneline', '--decorate',
*what)
if output:
status.add(ScmTaint.unpushed_local,
joinLines("> unpushed local commits:", indent(output, ' ')))
except BuildError as e:
status.add(ScmTaint.error, e.slogan)
return status
示例15: slope_a
def slope_a(a, cell_size=1, kern=None, degrees=True, verb=False, keep=False):
"""Return slope in degrees for an input array using 3rd order
finite difference method for a 3x3 moing window view into the array.
Requires:
---------
- a : an input 2d array. X and Y represent coordinates of the Z values
- cell_size : cell size, must be in the same units as X and Y
- kern : kernel to use
- degrees : True, returns degrees otherwise radians
- verb : True, to print results
- keep : False, to remove/squeeze extra dimensions
- filter :
np.array([[1, 2, 1], [2, 0, 2], [1, 2, 1]]) **current default
Notes:
------
::
dzdx: sum(col2 - col0)/8*cellsize
dzdy: sum(row2 - row0)/8*celsize
Assert the array is ndim=4 even if (1,z,y,x)
general dzdx + dzdy = dxyz
[[a, b, c], [[1, 0, 1], [[1, 2, 1] [[1, 2, 1]
[d, e, f] [2, 0, 2], + [0, 0, 0] = [2, 0, 2],
[g, h, i] [1, 0, 1]] [1, 2, 1]] [1, 2, 1]]
"""
frmt = """\n :----------------------------------------:
:{}\n :input array...\n {}\n :slope values...\n {!r:}
:----------------------------------------:
"""
# ---- stride the data and calculate slope for 3x3 sliding windows ----
np.set_printoptions(edgeitems=10, linewidth=100, precision=1)
a_s = stride(a, win=(3, 3), stepby=(1, 1))
if a_s.ndim < 4:
new_shape = (1,) * (4-len(a_s.shape)) + a_s.shape
a_s = a_s.reshape(new_shape)
#
kern = kernels(kern) # return the kernel if specified
# ---- default filter, apply the filter to the array ----
#
dz_dx, dz_dy = filter_a(a_s, a_filter=kern, cell_size=cell_size)
#
s = np.sqrt(dz_dx**2 + dz_dy**2)
if degrees:
s = np.rad2deg(np.arctan(s))
if not keep:
s = np.squeeze(s)
if verb:
p = " "
args = ["Results for slope_a... ",
indent(str(a), p), indent(str(s), p)]
print(dedent(frmt).format(*args))
return s