本文整理匯總了Python中doctest.Example方法的典型用法代碼示例。如果您正苦於以下問題:Python doctest.Example方法的具體用法?Python doctest.Example怎麽用?Python doctest.Example使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類doctest
的用法示例。
在下文中一共展示了doctest.Example方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: old_test2
# 需要導入模塊: import doctest [as 別名]
# 或者: from doctest import Example [as 別名]
def old_test2(): r"""
>>> from doctest import Tester
>>> t = Tester(globs={}, verbose=1)
>>> test = r'''
... # just an example
... >>> x = 1 + 2
... >>> x
... 3
... '''
>>> t.runstring(test, "Example")
Running string Example
Trying:
x = 1 + 2
Expecting nothing
ok
Trying:
x
Expecting:
3
ok
0 of 2 examples failed in string Example
TestResults(failed=0, attempted=2)
"""
示例2: old_test2
# 需要導入模塊: import doctest [as 別名]
# 或者: from doctest import Example [as 別名]
def old_test2(): r"""
>>> from doctest import Tester
>>> t = Tester(globs={}, verbose=1)
>>> test = r'''
... # just an example
... >>> x = 1 + 2
... >>> x
... 3
... '''
>>> t.runstring(test, "Example")
Running string Example
Trying:
x = 1 + 2
Expecting nothing
ok
Trying:
x
Expecting:
3
ok
0 of 2 examples failed in string Example
(0, 2)
"""
示例3: pad
# 需要導入模塊: import doctest [as 別名]
# 或者: from doctest import Example [as 別名]
def pad(self, example: Example, max_sl: int, max_conv: int, field: Field, target_roles: Optional[Roles] = None) -> \
Tuple[Conversations, Lengths, Roles]:
"""Pad a hierarchical example to the max sequence length and max conv length provided. Optionally if
target_roles parameter is provided every sentence whose role, found from example.roles,
is not matching the target_roles will be padded completely.
"""
indices = [0] + np.cumsum(example.sl).tolist()
minibatch = self.get_minibatch_text(example, indices, backwards=self.backwards)
field.fix_length = max_sl
field.include_lengths = True
padded, lens = field.pad(minibatch=minibatch)
padded_roles = list(example.roles)
padded_sentence = [field.pad_token for _ in range(max_sl)]
if target_roles is not None:
padded = [p if r in target_roles else padded_sentence for p, r in zip(padded, padded_roles)]
for _ in range(max_conv - len(padded)):
padded.append(padded_sentence)
lens.append(0)
padded_roles.append(field.pad_token)
return padded, lens, padded_roles
示例4: get_suite_examples
# 需要導入模塊: import doctest [as 別名]
# 或者: from doctest import Example [as 別名]
def get_suite_examples(self, suite, case):
# suite/case specified, so only parse relevant text into Examples
exs = collections.OrderedDict()
case_ex = collections.OrderedDict()
# get the shared lines that should impact all the cases in the suite.
shrd_txt = self.shared_case_data[suite]
if shrd_txt:
parse_shared = self.parser.parse(shrd_txt.group(0), self.tstfile_name)
shrd_ex = [i for i in parse_shared if isinstance(i, Example)]
if shrd_ex:
case_ex['shared'] = shrd_ex
if case:
if str(case[0]) not in self.data[suite]:
raise KeyError
parsed_temp_examples = self.parser.parse(self.data[suite][case[0]], self.tstfile_name)
case_examples = [i for i in parsed_temp_examples if isinstance(i, Example)]
case_ex[str(case[0])] = case_examples
else:
for itemcase in self.data[suite].keys():
parsed_temp_examples = self.parser.parse(self.data[suite][itemcase], self.tstfile_name)
case_examples = [i for i in parsed_temp_examples if isinstance(i, Example)]
case_ex[itemcase] = case_examples
exs[suite] = case_ex
return exs
示例5: get_all_examples
# 需要導入模塊: import doctest [as 別名]
# 或者: from doctest import Example [as 別名]
def get_all_examples(self):
# no suite/case flag, so parses all text into Example objects
exs = collections.OrderedDict()
for sui in self.data.keys():
case_ex = collections.OrderedDict()
# get the shared lines that should impact all the cases in the suite.
shrd_txt = self.shared_case_data[sui]
if shrd_txt:
parse_shared = self.parser.parse(shrd_txt.group(0), self.tstfile_name)
shrd_ex = [i for i in parse_shared if isinstance(i, Example)]
if shrd_ex:
case_ex['shared'] = shrd_ex
for itemcase in self.data[sui].keys():
parsed_temp_examples = self.parser.parse(self.data[sui][itemcase], self.tstfile_name)
case_examples = [i for i in parsed_temp_examples if isinstance(i, Example)]
case_ex[itemcase] = case_examples
exs[sui] = case_ex
return exs
# catch inf loops/ recur err
示例6: run_examples
# 需要導入模塊: import doctest [as 別名]
# 或者: from doctest import Example [as 別名]
def run_examples(self, exs):
# runs the Example objects, keeps track of right/wrong etc
total_failed = 0
total_attempted = 0
case = 'shared'
for sui in exs.keys():
if not total_failed:
final_env = dict(self.good_env)
if 'shared' in exs[sui].keys():
dtest = DocTest(exs[sui]['shared'], self.good_env, 'shared', None, None, None)
result = self.runner.run(dtest, clear_globs=False)
# take the env from shared dtest and save it for other exs
final_env = dict(self.good_env, **dtest.globs)
total_failed += result.failed
total_attempted += result.attempted
for case in exs[sui].keys():
if case != 'shared':
if not total_failed:
example_name = "Suite {}, Case {}".format(sui, case)
dtest = DocTest(exs[sui][case], final_env, example_name, None, None, None)
result = self.runner.run(dtest)
total_failed += result.failed
total_attempted += result.attempted
return total_failed, total_attempted
示例7: parse
# 需要導入模塊: import doctest [as 別名]
# 或者: from doctest import Example [as 別名]
def parse(self, string, name='<string>'):
"""
Divide the given string into examples and intervening text,
and return them as a list of alternating Examples and strings.
Line numbers for the Examples are 0-based. The optional
argument `name` is a name identifying this string, and is only
used for error messages.
"""
string = string.expandtabs()
# If all lines begin with the same indentation, then strip it.
min_indent = self._min_indent(string)
if min_indent > 0:
string = '\n'.join([l[min_indent:] for l in string.split('\n')])
output = []
charno, lineno = 0, 0
# Find all doctest examples in the string:
for m in self._EXAMPLE_RE.finditer(string):
# Add the pre-example text to `output`.
output.append(string[charno:m.start()])
# Update lineno (lines before this example)
lineno += string.count('\n', charno, m.start())
# Extract info from the regexp match.
(source, options, want, exc_msg) = \
self._parse_example(m, name, lineno)
# Create an Example, and add it to the list.
if not self._IS_BLANK_OR_COMMENT(source):
# @@: Erg, this is the only line I need to change...
output.append(doctest.Example(
source, want, exc_msg,
lineno=lineno,
indent=min_indent+len(m.group('indent') or m.group('runindent')),
options=options))
# Update lineno (lines inside this example)
lineno += string.count('\n', m.start(), m.end())
# Update charno.
charno = m.end()
# Add any remaining post-example text to `output`.
output.append(string[charno:])
return output
示例8: modify_example
# 需要導入模塊: import doctest [as 別名]
# 或者: from doctest import Example [as 別名]
def modify_example(example):
new_want = Python2DocTestParser.add_u_before_strings(example.want)
example = doctest.Example(
source=example.source,
want=new_want,
exc_msg=example.exc_msg,
lineno=example.lineno,
indent=example.indent,
options=example.options
)
return example
示例9: modify_example
# 需要導入模塊: import doctest [as 別名]
# 或者: from doctest import Example [as 別名]
def modify_example(example):
new_source = example.source
new_want = example.want
# README is formatted without "..." before multi-line input to make code easy to copy-paste
if new_source.endswith('"""\n'):
new_source += new_want + '\n"""'
new_want = ""
# doctest sometimes incorrectly includes markdown in returned example
if new_want.endswith("```\n"):
new_want = new_want[:new_want.index("```")]
# README's serialize() has spaces instead of tabs to make output easier to read
if new_want.startswith("# text"):
new_want = re.sub(r" {2,}", "\t", new_want)
new_want = new_want.rstrip() + "\n\n"
# README cheats and prints return value without quotes
new_want = repr(new_want)
# README has examples with lists formatted in multiple lines to make them easier to read
if new_want.startswith(("[", "Token([", "Metadata([")):
new_want = ReadmeTestParser.normalize_whitespace(new_want)
example = doctest.Example(
source=new_source,
want=new_want,
exc_msg=example.exc_msg,
lineno=example.lineno,
indent=example.indent,
options=example.options
)
return example
示例10: parse
# 需要導入模塊: import doctest [as 別名]
# 或者: from doctest import Example [as 別名]
def parse(self, *args, **kwargs):
examples = DocTestParser.parse(self, *args, **kwargs)
for example in examples:
if not isinstance(example, Example):
continue
if any(flag in example.options for flag in skipflags):
example.options[SKIP] = True
return examples
示例11: __init__
# 需要導入模塊: import doctest [as 別名]
# 或者: from doctest import Example [as 別名]
def __init__(self, source, want, exc_msg=None, lineno=0, indent=0,
options=None):
# Parent constructor
doctest.Example.__init__(self,source,want,exc_msg,lineno,indent,options)
# An EXTRA newline is needed to prevent pexpect hangs
self.source += '\n'
示例12: parse
# 需要導入模塊: import doctest [as 別名]
# 或者: from doctest import Example [as 別名]
def parse(self, string, name='<string>'):
r = super().parse(string, name)
for s in r:
if isinstance(s, doctest.Example):
s.source = transform(s.source)
return r
示例13: process_minibatch
# 需要導入模塊: import doctest [as 別名]
# 或者: from doctest import Example [as 別名]
def process_minibatch(self, minibatch: List[Example]) -> Tuple[LT, LT, LT]:
max_sl = max([max(ex.sl) for ex in minibatch])
max_conv = max([len(ex.roles) for ex in minibatch])
padded_examples, padded_targets, padded_lengths, padded_roles = [], [], [], []
for example in minibatch:
examples, lens, roles = self.pad(example, max_sl=max_sl, max_conv=max_conv, field=self.text_field)
padded_examples.extend(examples)
padded_lengths.extend(lens)
padded_roles.append(roles)
# if self.target_roles is not None we will pad the roles we do not want to train on
# this allows for learning only the responses we are interested in
targets, *_ = self.pad(example, max_sl=max_sl, max_conv=max_conv, field=self.text_field,
target_roles=self.target_roles)
padded_targets.extend(targets)
self.text_field.include_lengths = False
data = self.text_field.numericalize(padded_examples, device=self.device, train=self.train)
batch_size = len(minibatch)
assert_dims(data, [max_sl, max_conv * batch_size])
data = data.view(max_sl, batch_size, max_conv).transpose(2, 0).transpose(2, 1).contiguous()
source = data[:-1] # we remove the extra padding sentence added here
targets = self.text_field.numericalize(padded_targets, device=self.device, train=self.train)
targets = targets.view(max_sl, batch_size, max_conv).transpose(2, 0).transpose(2, 1).contiguous()
# shapes will be max_conv -1 , max_sl, batch_size
assert_dims(source, [max_conv - 1, max_sl, batch_size])
assert_dims(targets, [max_conv, max_sl, batch_size])
return source, targets[1:], targets[1:, 1:]
示例14: get_minibatch_text
# 需要導入模塊: import doctest [as 別名]
# 或者: from doctest import Example [as 別名]
def get_minibatch_text(self, example: Example, indices: List[int], backwards: bool = False) -> List[List[str]]:
minibatch = [example.text[indices[index]:indices[index + 1]] for index in range(len(indices) - 1)]
if backwards:
minibatch = [i[::-1] for i in minibatch]
return minibatch
示例15: run_doctest
# 需要導入模塊: import doctest [as 別名]
# 或者: from doctest import Example [as 別名]
def run_doctest(name, doctest_string, global_environment):
"""
Run a single test with given global_environment.
Returns (True, '') if the doctest passes.
Returns (False, failure_message) if the doctest fails.
"""
examples = doctest.DocTestParser().parse(
doctest_string,
name
)
test = doctest.DocTest(
[e for e in examples if isinstance(e, doctest.Example)],
global_environment,
name,
None,
None,
doctest_string
)
doctestrunner = doctest.DocTestRunner(verbose=True)
runresults = io.StringIO()
with redirect_stdout(runresults), redirect_stderr(runresults), hide_outputs():
doctestrunner.run(test, clear_globs=False)
with open(os.devnull, 'w') as f, redirect_stderr(f), redirect_stdout(f):
result = doctestrunner.summarize(verbose=True)
# An individual test can only pass or fail
if result.failed == 0:
return (True, '')
else:
return False, runresults.getvalue()