partition stringclasses 3 values | func_name stringlengths 1 134 | docstring stringlengths 1 46.9k | path stringlengths 4 223 | original_string stringlengths 75 104k | code stringlengths 75 104k | docstring_tokens listlengths 1 1.97k | repo stringlengths 7 55 | language stringclasses 1 value | url stringlengths 87 315 | code_tokens listlengths 19 28.4k | sha stringlengths 40 40 |
|---|---|---|---|---|---|---|---|---|---|---|---|
test | InteractiveShellApp._run_cmd_line_code | Run code or file specified at the command-line | environment/lib/python2.7/site-packages/IPython/core/shellapp.py | def _run_cmd_line_code(self):
"""Run code or file specified at the command-line"""
if self.code_to_run:
line = self.code_to_run
try:
self.log.info("Running code given at command line (c=): %s" %
line)
self.shell.run_cell(line, store_history=False)
except:
self.log.warn("Error in executing line in user namespace: %s" %
line)
self.shell.showtraceback()
# Like Python itself, ignore the second if the first of these is present
elif self.file_to_run:
fname = self.file_to_run
try:
self._exec_file(fname)
except:
self.log.warn("Error in executing file in user namespace: %s" %
fname)
self.shell.showtraceback() | def _run_cmd_line_code(self):
"""Run code or file specified at the command-line"""
if self.code_to_run:
line = self.code_to_run
try:
self.log.info("Running code given at command line (c=): %s" %
line)
self.shell.run_cell(line, store_history=False)
except:
self.log.warn("Error in executing line in user namespace: %s" %
line)
self.shell.showtraceback()
# Like Python itself, ignore the second if the first of these is present
elif self.file_to_run:
fname = self.file_to_run
try:
self._exec_file(fname)
except:
self.log.warn("Error in executing file in user namespace: %s" %
fname)
self.shell.showtraceback() | [
"Run",
"code",
"or",
"file",
"specified",
"at",
"the",
"command",
"-",
"line"
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/core/shellapp.py#L326-L347 | [
"def",
"_run_cmd_line_code",
"(",
"self",
")",
":",
"if",
"self",
".",
"code_to_run",
":",
"line",
"=",
"self",
".",
"code_to_run",
"try",
":",
"self",
".",
"log",
".",
"info",
"(",
"\"Running code given at command line (c=): %s\"",
"%",
"line",
")",
"self",
".",
"shell",
".",
"run_cell",
"(",
"line",
",",
"store_history",
"=",
"False",
")",
"except",
":",
"self",
".",
"log",
".",
"warn",
"(",
"\"Error in executing line in user namespace: %s\"",
"%",
"line",
")",
"self",
".",
"shell",
".",
"showtraceback",
"(",
")",
"# Like Python itself, ignore the second if the first of these is present",
"elif",
"self",
".",
"file_to_run",
":",
"fname",
"=",
"self",
".",
"file_to_run",
"try",
":",
"self",
".",
"_exec_file",
"(",
"fname",
")",
"except",
":",
"self",
".",
"log",
".",
"warn",
"(",
"\"Error in executing file in user namespace: %s\"",
"%",
"fname",
")",
"self",
".",
"shell",
".",
"showtraceback",
"(",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | InteractiveShellApp._run_module | Run module specified at the command-line. | environment/lib/python2.7/site-packages/IPython/core/shellapp.py | def _run_module(self):
"""Run module specified at the command-line."""
if self.module_to_run:
# Make sure that the module gets a proper sys.argv as if it were
# run using `python -m`.
save_argv = sys.argv
sys.argv = [sys.executable] + self.extra_args
try:
self.shell.safe_run_module(self.module_to_run,
self.shell.user_ns)
finally:
sys.argv = save_argv | def _run_module(self):
"""Run module specified at the command-line."""
if self.module_to_run:
# Make sure that the module gets a proper sys.argv as if it were
# run using `python -m`.
save_argv = sys.argv
sys.argv = [sys.executable] + self.extra_args
try:
self.shell.safe_run_module(self.module_to_run,
self.shell.user_ns)
finally:
sys.argv = save_argv | [
"Run",
"module",
"specified",
"at",
"the",
"command",
"-",
"line",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/core/shellapp.py#L349-L360 | [
"def",
"_run_module",
"(",
"self",
")",
":",
"if",
"self",
".",
"module_to_run",
":",
"# Make sure that the module gets a proper sys.argv as if it were",
"# run using `python -m`.",
"save_argv",
"=",
"sys",
".",
"argv",
"sys",
".",
"argv",
"=",
"[",
"sys",
".",
"executable",
"]",
"+",
"self",
".",
"extra_args",
"try",
":",
"self",
".",
"shell",
".",
"safe_run_module",
"(",
"self",
".",
"module_to_run",
",",
"self",
".",
"shell",
".",
"user_ns",
")",
"finally",
":",
"sys",
".",
"argv",
"=",
"save_argv"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | generic | Create a simple generic function | environment/lib/python2.7/site-packages/IPython/external/simplegeneric/_simplegeneric.py | def generic(func):
"""Create a simple generic function"""
_sentinel = object()
def _by_class(*args, **kw):
cls = args[0].__class__
for t in type(cls.__name__, (cls,object), {}).__mro__:
f = _gbt(t, _sentinel)
if f is not _sentinel:
return f(*args, **kw)
else:
return func(*args, **kw)
_by_type = {object: func}
try:
_by_type[InstanceType] = _by_class
except NameError: # Python 3
pass
_gbt = _by_type.get
def when_type(*types):
"""Decorator to add a method that will be called for the given types"""
for t in types:
if not isinstance(t, classtypes):
raise TypeError(
"%r is not a type or class" % (t,)
)
def decorate(f):
for t in types:
if _by_type.setdefault(t,f) is not f:
raise TypeError(
"%r already has method for type %r" % (func, t)
)
return f
return decorate
_by_object = {}
_gbo = _by_object.get
def when_object(*obs):
"""Decorator to add a method to be called for the given object(s)"""
def decorate(f):
for o in obs:
if _by_object.setdefault(id(o), (o,f))[1] is not f:
raise TypeError(
"%r already has method for object %r" % (func, o)
)
return f
return decorate
def dispatch(*args, **kw):
f = _gbo(id(args[0]), _sentinel)
if f is _sentinel:
for t in type(args[0]).__mro__:
f = _gbt(t, _sentinel)
if f is not _sentinel:
return f(*args, **kw)
else:
return func(*args, **kw)
else:
return f[1](*args, **kw)
dispatch.__name__ = func.__name__
dispatch.__dict__ = func.__dict__.copy()
dispatch.__doc__ = func.__doc__
dispatch.__module__ = func.__module__
dispatch.when_type = when_type
dispatch.when_object = when_object
dispatch.default = func
dispatch.has_object = lambda o: id(o) in _by_object
dispatch.has_type = lambda t: t in _by_type
return dispatch | def generic(func):
"""Create a simple generic function"""
_sentinel = object()
def _by_class(*args, **kw):
cls = args[0].__class__
for t in type(cls.__name__, (cls,object), {}).__mro__:
f = _gbt(t, _sentinel)
if f is not _sentinel:
return f(*args, **kw)
else:
return func(*args, **kw)
_by_type = {object: func}
try:
_by_type[InstanceType] = _by_class
except NameError: # Python 3
pass
_gbt = _by_type.get
def when_type(*types):
"""Decorator to add a method that will be called for the given types"""
for t in types:
if not isinstance(t, classtypes):
raise TypeError(
"%r is not a type or class" % (t,)
)
def decorate(f):
for t in types:
if _by_type.setdefault(t,f) is not f:
raise TypeError(
"%r already has method for type %r" % (func, t)
)
return f
return decorate
_by_object = {}
_gbo = _by_object.get
def when_object(*obs):
"""Decorator to add a method to be called for the given object(s)"""
def decorate(f):
for o in obs:
if _by_object.setdefault(id(o), (o,f))[1] is not f:
raise TypeError(
"%r already has method for object %r" % (func, o)
)
return f
return decorate
def dispatch(*args, **kw):
f = _gbo(id(args[0]), _sentinel)
if f is _sentinel:
for t in type(args[0]).__mro__:
f = _gbt(t, _sentinel)
if f is not _sentinel:
return f(*args, **kw)
else:
return func(*args, **kw)
else:
return f[1](*args, **kw)
dispatch.__name__ = func.__name__
dispatch.__dict__ = func.__dict__.copy()
dispatch.__doc__ = func.__doc__
dispatch.__module__ = func.__module__
dispatch.when_type = when_type
dispatch.when_object = when_object
dispatch.default = func
dispatch.has_object = lambda o: id(o) in _by_object
dispatch.has_type = lambda t: t in _by_type
return dispatch | [
"Create",
"a",
"simple",
"generic",
"function"
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/external/simplegeneric/_simplegeneric.py#L23-L101 | [
"def",
"generic",
"(",
"func",
")",
":",
"_sentinel",
"=",
"object",
"(",
")",
"def",
"_by_class",
"(",
"*",
"args",
",",
"*",
"*",
"kw",
")",
":",
"cls",
"=",
"args",
"[",
"0",
"]",
".",
"__class__",
"for",
"t",
"in",
"type",
"(",
"cls",
".",
"__name__",
",",
"(",
"cls",
",",
"object",
")",
",",
"{",
"}",
")",
".",
"__mro__",
":",
"f",
"=",
"_gbt",
"(",
"t",
",",
"_sentinel",
")",
"if",
"f",
"is",
"not",
"_sentinel",
":",
"return",
"f",
"(",
"*",
"args",
",",
"*",
"*",
"kw",
")",
"else",
":",
"return",
"func",
"(",
"*",
"args",
",",
"*",
"*",
"kw",
")",
"_by_type",
"=",
"{",
"object",
":",
"func",
"}",
"try",
":",
"_by_type",
"[",
"InstanceType",
"]",
"=",
"_by_class",
"except",
"NameError",
":",
"# Python 3",
"pass",
"_gbt",
"=",
"_by_type",
".",
"get",
"def",
"when_type",
"(",
"*",
"types",
")",
":",
"\"\"\"Decorator to add a method that will be called for the given types\"\"\"",
"for",
"t",
"in",
"types",
":",
"if",
"not",
"isinstance",
"(",
"t",
",",
"classtypes",
")",
":",
"raise",
"TypeError",
"(",
"\"%r is not a type or class\"",
"%",
"(",
"t",
",",
")",
")",
"def",
"decorate",
"(",
"f",
")",
":",
"for",
"t",
"in",
"types",
":",
"if",
"_by_type",
".",
"setdefault",
"(",
"t",
",",
"f",
")",
"is",
"not",
"f",
":",
"raise",
"TypeError",
"(",
"\"%r already has method for type %r\"",
"%",
"(",
"func",
",",
"t",
")",
")",
"return",
"f",
"return",
"decorate",
"_by_object",
"=",
"{",
"}",
"_gbo",
"=",
"_by_object",
".",
"get",
"def",
"when_object",
"(",
"*",
"obs",
")",
":",
"\"\"\"Decorator to add a method to be called for the given object(s)\"\"\"",
"def",
"decorate",
"(",
"f",
")",
":",
"for",
"o",
"in",
"obs",
":",
"if",
"_by_object",
".",
"setdefault",
"(",
"id",
"(",
"o",
")",
",",
"(",
"o",
",",
"f",
")",
")",
"[",
"1",
"]",
"is",
"not",
"f",
":",
"raise",
"TypeError",
"(",
"\"%r already has method for object %r\"",
"%",
"(",
"func",
",",
"o",
")",
")",
"return",
"f",
"return",
"decorate",
"def",
"dispatch",
"(",
"*",
"args",
",",
"*",
"*",
"kw",
")",
":",
"f",
"=",
"_gbo",
"(",
"id",
"(",
"args",
"[",
"0",
"]",
")",
",",
"_sentinel",
")",
"if",
"f",
"is",
"_sentinel",
":",
"for",
"t",
"in",
"type",
"(",
"args",
"[",
"0",
"]",
")",
".",
"__mro__",
":",
"f",
"=",
"_gbt",
"(",
"t",
",",
"_sentinel",
")",
"if",
"f",
"is",
"not",
"_sentinel",
":",
"return",
"f",
"(",
"*",
"args",
",",
"*",
"*",
"kw",
")",
"else",
":",
"return",
"func",
"(",
"*",
"args",
",",
"*",
"*",
"kw",
")",
"else",
":",
"return",
"f",
"[",
"1",
"]",
"(",
"*",
"args",
",",
"*",
"*",
"kw",
")",
"dispatch",
".",
"__name__",
"=",
"func",
".",
"__name__",
"dispatch",
".",
"__dict__",
"=",
"func",
".",
"__dict__",
".",
"copy",
"(",
")",
"dispatch",
".",
"__doc__",
"=",
"func",
".",
"__doc__",
"dispatch",
".",
"__module__",
"=",
"func",
".",
"__module__",
"dispatch",
".",
"when_type",
"=",
"when_type",
"dispatch",
".",
"when_object",
"=",
"when_object",
"dispatch",
".",
"default",
"=",
"func",
"dispatch",
".",
"has_object",
"=",
"lambda",
"o",
":",
"id",
"(",
"o",
")",
"in",
"_by_object",
"dispatch",
".",
"has_type",
"=",
"lambda",
"t",
":",
"t",
"in",
"_by_type",
"return",
"dispatch"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | data_filename | Return the path to a data file of ours.
The file is searched for on `STATIC_PATH`, and the first place it's found,
is returned.
Each directory in `STATIC_PATH` is searched as-is, and also, if `pkgdir`
is provided, at that subdirectory. | virtualEnvironment/lib/python2.7/site-packages/coverage/html.py | def data_filename(fname, pkgdir=""):
"""Return the path to a data file of ours.
The file is searched for on `STATIC_PATH`, and the first place it's found,
is returned.
Each directory in `STATIC_PATH` is searched as-is, and also, if `pkgdir`
is provided, at that subdirectory.
"""
for static_dir in STATIC_PATH:
static_filename = os.path.join(static_dir, fname)
if os.path.exists(static_filename):
return static_filename
if pkgdir:
static_filename = os.path.join(static_dir, pkgdir, fname)
if os.path.exists(static_filename):
return static_filename
raise CoverageException("Couldn't find static file %r" % fname) | def data_filename(fname, pkgdir=""):
"""Return the path to a data file of ours.
The file is searched for on `STATIC_PATH`, and the first place it's found,
is returned.
Each directory in `STATIC_PATH` is searched as-is, and also, if `pkgdir`
is provided, at that subdirectory.
"""
for static_dir in STATIC_PATH:
static_filename = os.path.join(static_dir, fname)
if os.path.exists(static_filename):
return static_filename
if pkgdir:
static_filename = os.path.join(static_dir, pkgdir, fname)
if os.path.exists(static_filename):
return static_filename
raise CoverageException("Couldn't find static file %r" % fname) | [
"Return",
"the",
"path",
"to",
"a",
"data",
"file",
"of",
"ours",
"."
] | tnkteja/myhelp | python | https://github.com/tnkteja/myhelp/blob/fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb/virtualEnvironment/lib/python2.7/site-packages/coverage/html.py#L23-L41 | [
"def",
"data_filename",
"(",
"fname",
",",
"pkgdir",
"=",
"\"\"",
")",
":",
"for",
"static_dir",
"in",
"STATIC_PATH",
":",
"static_filename",
"=",
"os",
".",
"path",
".",
"join",
"(",
"static_dir",
",",
"fname",
")",
"if",
"os",
".",
"path",
".",
"exists",
"(",
"static_filename",
")",
":",
"return",
"static_filename",
"if",
"pkgdir",
":",
"static_filename",
"=",
"os",
".",
"path",
".",
"join",
"(",
"static_dir",
",",
"pkgdir",
",",
"fname",
")",
"if",
"os",
".",
"path",
".",
"exists",
"(",
"static_filename",
")",
":",
"return",
"static_filename",
"raise",
"CoverageException",
"(",
"\"Couldn't find static file %r\"",
"%",
"fname",
")"
] | fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb |
test | data | Return the contents of a data file of ours. | virtualEnvironment/lib/python2.7/site-packages/coverage/html.py | def data(fname):
"""Return the contents of a data file of ours."""
data_file = open(data_filename(fname))
try:
return data_file.read()
finally:
data_file.close() | def data(fname):
"""Return the contents of a data file of ours."""
data_file = open(data_filename(fname))
try:
return data_file.read()
finally:
data_file.close() | [
"Return",
"the",
"contents",
"of",
"a",
"data",
"file",
"of",
"ours",
"."
] | tnkteja/myhelp | python | https://github.com/tnkteja/myhelp/blob/fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb/virtualEnvironment/lib/python2.7/site-packages/coverage/html.py#L44-L50 | [
"def",
"data",
"(",
"fname",
")",
":",
"data_file",
"=",
"open",
"(",
"data_filename",
"(",
"fname",
")",
")",
"try",
":",
"return",
"data_file",
".",
"read",
"(",
")",
"finally",
":",
"data_file",
".",
"close",
"(",
")"
] | fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb |
test | escape | HTML-escape the text in `t`. | virtualEnvironment/lib/python2.7/site-packages/coverage/html.py | def escape(t):
"""HTML-escape the text in `t`."""
return (t
# Convert HTML special chars into HTML entities.
.replace("&", "&").replace("<", "<").replace(">", ">")
.replace("'", "'").replace('"', """)
# Convert runs of spaces: "......" -> " . . ."
.replace(" ", " ")
# To deal with odd-length runs, convert the final pair of spaces
# so that "....." -> " . ."
.replace(" ", " ")
) | def escape(t):
"""HTML-escape the text in `t`."""
return (t
# Convert HTML special chars into HTML entities.
.replace("&", "&").replace("<", "<").replace(">", ">")
.replace("'", "'").replace('"', """)
# Convert runs of spaces: "......" -> " . . ."
.replace(" ", " ")
# To deal with odd-length runs, convert the final pair of spaces
# so that "....." -> " . ."
.replace(" ", " ")
) | [
"HTML",
"-",
"escape",
"the",
"text",
"in",
"t",
"."
] | tnkteja/myhelp | python | https://github.com/tnkteja/myhelp/blob/fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb/virtualEnvironment/lib/python2.7/site-packages/coverage/html.py#L375-L386 | [
"def",
"escape",
"(",
"t",
")",
":",
"return",
"(",
"t",
"# Convert HTML special chars into HTML entities.",
".",
"replace",
"(",
"\"&\"",
",",
"\"&\"",
")",
".",
"replace",
"(",
"\"<\"",
",",
"\"<\"",
")",
".",
"replace",
"(",
"\">\"",
",",
"\">\"",
")",
".",
"replace",
"(",
"\"'\"",
",",
"\"'\"",
")",
".",
"replace",
"(",
"'\"'",
",",
"\""\"",
")",
"# Convert runs of spaces: \"......\" -> \" . . .\"",
".",
"replace",
"(",
"\" \"",
",",
"\" \"",
")",
"# To deal with odd-length runs, convert the final pair of spaces",
"# so that \".....\" -> \" . .\"",
".",
"replace",
"(",
"\" \"",
",",
"\" \"",
")",
")"
] | fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb |
test | HtmlReporter.report | Generate an HTML report for `morfs`.
`morfs` is a list of modules or filenames. | virtualEnvironment/lib/python2.7/site-packages/coverage/html.py | def report(self, morfs):
"""Generate an HTML report for `morfs`.
`morfs` is a list of modules or filenames.
"""
assert self.config.html_dir, "must give a directory for html reporting"
# Read the status data.
self.status.read(self.config.html_dir)
# Check that this run used the same settings as the last run.
m = Hasher()
m.update(self.config)
these_settings = m.digest()
if self.status.settings_hash() != these_settings:
self.status.reset()
self.status.set_settings_hash(these_settings)
# The user may have extra CSS they want copied.
if self.config.extra_css:
self.extra_css = os.path.basename(self.config.extra_css)
# Process all the files.
self.report_files(self.html_file, morfs, self.config.html_dir)
if not self.files:
raise CoverageException("No data to report.")
# Write the index file.
self.index_file()
self.make_local_static_report_files()
return self.totals.pc_covered | def report(self, morfs):
"""Generate an HTML report for `morfs`.
`morfs` is a list of modules or filenames.
"""
assert self.config.html_dir, "must give a directory for html reporting"
# Read the status data.
self.status.read(self.config.html_dir)
# Check that this run used the same settings as the last run.
m = Hasher()
m.update(self.config)
these_settings = m.digest()
if self.status.settings_hash() != these_settings:
self.status.reset()
self.status.set_settings_hash(these_settings)
# The user may have extra CSS they want copied.
if self.config.extra_css:
self.extra_css = os.path.basename(self.config.extra_css)
# Process all the files.
self.report_files(self.html_file, morfs, self.config.html_dir)
if not self.files:
raise CoverageException("No data to report.")
# Write the index file.
self.index_file()
self.make_local_static_report_files()
return self.totals.pc_covered | [
"Generate",
"an",
"HTML",
"report",
"for",
"morfs",
"."
] | tnkteja/myhelp | python | https://github.com/tnkteja/myhelp/blob/fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb/virtualEnvironment/lib/python2.7/site-packages/coverage/html.py#L89-L123 | [
"def",
"report",
"(",
"self",
",",
"morfs",
")",
":",
"assert",
"self",
".",
"config",
".",
"html_dir",
",",
"\"must give a directory for html reporting\"",
"# Read the status data.",
"self",
".",
"status",
".",
"read",
"(",
"self",
".",
"config",
".",
"html_dir",
")",
"# Check that this run used the same settings as the last run.",
"m",
"=",
"Hasher",
"(",
")",
"m",
".",
"update",
"(",
"self",
".",
"config",
")",
"these_settings",
"=",
"m",
".",
"digest",
"(",
")",
"if",
"self",
".",
"status",
".",
"settings_hash",
"(",
")",
"!=",
"these_settings",
":",
"self",
".",
"status",
".",
"reset",
"(",
")",
"self",
".",
"status",
".",
"set_settings_hash",
"(",
"these_settings",
")",
"# The user may have extra CSS they want copied.",
"if",
"self",
".",
"config",
".",
"extra_css",
":",
"self",
".",
"extra_css",
"=",
"os",
".",
"path",
".",
"basename",
"(",
"self",
".",
"config",
".",
"extra_css",
")",
"# Process all the files.",
"self",
".",
"report_files",
"(",
"self",
".",
"html_file",
",",
"morfs",
",",
"self",
".",
"config",
".",
"html_dir",
")",
"if",
"not",
"self",
".",
"files",
":",
"raise",
"CoverageException",
"(",
"\"No data to report.\"",
")",
"# Write the index file.",
"self",
".",
"index_file",
"(",
")",
"self",
".",
"make_local_static_report_files",
"(",
")",
"return",
"self",
".",
"totals",
".",
"pc_covered"
] | fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb |
test | HtmlReporter.make_local_static_report_files | Make local instances of static files for HTML report. | virtualEnvironment/lib/python2.7/site-packages/coverage/html.py | def make_local_static_report_files(self):
"""Make local instances of static files for HTML report."""
# The files we provide must always be copied.
for static, pkgdir in self.STATIC_FILES:
shutil.copyfile(
data_filename(static, pkgdir),
os.path.join(self.directory, static)
)
# The user may have extra CSS they want copied.
if self.extra_css:
shutil.copyfile(
self.config.extra_css,
os.path.join(self.directory, self.extra_css)
) | def make_local_static_report_files(self):
"""Make local instances of static files for HTML report."""
# The files we provide must always be copied.
for static, pkgdir in self.STATIC_FILES:
shutil.copyfile(
data_filename(static, pkgdir),
os.path.join(self.directory, static)
)
# The user may have extra CSS they want copied.
if self.extra_css:
shutil.copyfile(
self.config.extra_css,
os.path.join(self.directory, self.extra_css)
) | [
"Make",
"local",
"instances",
"of",
"static",
"files",
"for",
"HTML",
"report",
"."
] | tnkteja/myhelp | python | https://github.com/tnkteja/myhelp/blob/fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb/virtualEnvironment/lib/python2.7/site-packages/coverage/html.py#L125-L139 | [
"def",
"make_local_static_report_files",
"(",
"self",
")",
":",
"# The files we provide must always be copied.",
"for",
"static",
",",
"pkgdir",
"in",
"self",
".",
"STATIC_FILES",
":",
"shutil",
".",
"copyfile",
"(",
"data_filename",
"(",
"static",
",",
"pkgdir",
")",
",",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"directory",
",",
"static",
")",
")",
"# The user may have extra CSS they want copied.",
"if",
"self",
".",
"extra_css",
":",
"shutil",
".",
"copyfile",
"(",
"self",
".",
"config",
".",
"extra_css",
",",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"directory",
",",
"self",
".",
"extra_css",
")",
")"
] | fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb |
test | HtmlReporter.write_html | Write `html` to `fname`, properly encoded. | virtualEnvironment/lib/python2.7/site-packages/coverage/html.py | def write_html(self, fname, html):
"""Write `html` to `fname`, properly encoded."""
fout = open(fname, "wb")
try:
fout.write(html.encode('ascii', 'xmlcharrefreplace'))
finally:
fout.close() | def write_html(self, fname, html):
"""Write `html` to `fname`, properly encoded."""
fout = open(fname, "wb")
try:
fout.write(html.encode('ascii', 'xmlcharrefreplace'))
finally:
fout.close() | [
"Write",
"html",
"to",
"fname",
"properly",
"encoded",
"."
] | tnkteja/myhelp | python | https://github.com/tnkteja/myhelp/blob/fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb/virtualEnvironment/lib/python2.7/site-packages/coverage/html.py#L141-L147 | [
"def",
"write_html",
"(",
"self",
",",
"fname",
",",
"html",
")",
":",
"fout",
"=",
"open",
"(",
"fname",
",",
"\"wb\"",
")",
"try",
":",
"fout",
".",
"write",
"(",
"html",
".",
"encode",
"(",
"'ascii'",
",",
"'xmlcharrefreplace'",
")",
")",
"finally",
":",
"fout",
".",
"close",
"(",
")"
] | fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb |
test | HtmlReporter.file_hash | Compute a hash that changes if the file needs to be re-reported. | virtualEnvironment/lib/python2.7/site-packages/coverage/html.py | def file_hash(self, source, cu):
"""Compute a hash that changes if the file needs to be re-reported."""
m = Hasher()
m.update(source)
self.coverage.data.add_to_hash(cu.filename, m)
return m.digest() | def file_hash(self, source, cu):
"""Compute a hash that changes if the file needs to be re-reported."""
m = Hasher()
m.update(source)
self.coverage.data.add_to_hash(cu.filename, m)
return m.digest() | [
"Compute",
"a",
"hash",
"that",
"changes",
"if",
"the",
"file",
"needs",
"to",
"be",
"re",
"-",
"reported",
"."
] | tnkteja/myhelp | python | https://github.com/tnkteja/myhelp/blob/fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb/virtualEnvironment/lib/python2.7/site-packages/coverage/html.py#L149-L154 | [
"def",
"file_hash",
"(",
"self",
",",
"source",
",",
"cu",
")",
":",
"m",
"=",
"Hasher",
"(",
")",
"m",
".",
"update",
"(",
"source",
")",
"self",
".",
"coverage",
".",
"data",
".",
"add_to_hash",
"(",
"cu",
".",
"filename",
",",
"m",
")",
"return",
"m",
".",
"digest",
"(",
")"
] | fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb |
test | HtmlReporter.html_file | Generate an HTML file for one source file. | virtualEnvironment/lib/python2.7/site-packages/coverage/html.py | def html_file(self, cu, analysis):
"""Generate an HTML file for one source file."""
source_file = cu.source_file()
try:
source = source_file.read()
finally:
source_file.close()
# Find out if the file on disk is already correct.
flat_rootname = cu.flat_rootname()
this_hash = self.file_hash(source, cu)
that_hash = self.status.file_hash(flat_rootname)
if this_hash == that_hash:
# Nothing has changed to require the file to be reported again.
self.files.append(self.status.index_info(flat_rootname))
return
self.status.set_file_hash(flat_rootname, this_hash)
# If need be, determine the encoding of the source file. We use it
# later to properly write the HTML.
if sys.version_info < (3, 0):
encoding = source_encoding(source)
# Some UTF8 files have the dreaded UTF8 BOM. If so, junk it.
if encoding.startswith("utf-8") and source[:3] == "\xef\xbb\xbf":
source = source[3:]
encoding = "utf-8"
# Get the numbers for this file.
nums = analysis.numbers
if self.arcs:
missing_branch_arcs = analysis.missing_branch_arcs()
# These classes determine which lines are highlighted by default.
c_run = "run hide_run"
c_exc = "exc"
c_mis = "mis"
c_par = "par " + c_run
lines = []
for lineno, line in enumerate(source_token_lines(source)):
lineno += 1 # 1-based line numbers.
# Figure out how to mark this line.
line_class = []
annotate_html = ""
annotate_title = ""
if lineno in analysis.statements:
line_class.append("stm")
if lineno in analysis.excluded:
line_class.append(c_exc)
elif lineno in analysis.missing:
line_class.append(c_mis)
elif self.arcs and lineno in missing_branch_arcs:
line_class.append(c_par)
annlines = []
for b in missing_branch_arcs[lineno]:
if b < 0:
annlines.append("exit")
else:
annlines.append(str(b))
annotate_html = " ".join(annlines)
if len(annlines) > 1:
annotate_title = "no jumps to these line numbers"
elif len(annlines) == 1:
annotate_title = "no jump to this line number"
elif lineno in analysis.statements:
line_class.append(c_run)
# Build the HTML for the line
html = []
for tok_type, tok_text in line:
if tok_type == "ws":
html.append(escape(tok_text))
else:
tok_html = escape(tok_text) or ' '
html.append(
"<span class='%s'>%s</span>" % (tok_type, tok_html)
)
lines.append({
'html': ''.join(html),
'number': lineno,
'class': ' '.join(line_class) or "pln",
'annotate': annotate_html,
'annotate_title': annotate_title,
})
# Write the HTML page for this file.
html = spaceless(self.source_tmpl.render({
'c_exc': c_exc, 'c_mis': c_mis, 'c_par': c_par, 'c_run': c_run,
'arcs': self.arcs, 'extra_css': self.extra_css,
'cu': cu, 'nums': nums, 'lines': lines,
}))
if sys.version_info < (3, 0):
html = html.decode(encoding)
html_filename = flat_rootname + ".html"
html_path = os.path.join(self.directory, html_filename)
self.write_html(html_path, html)
# Save this file's information for the index file.
index_info = {
'nums': nums,
'html_filename': html_filename,
'name': cu.name,
}
self.files.append(index_info)
self.status.set_index_info(flat_rootname, index_info) | def html_file(self, cu, analysis):
"""Generate an HTML file for one source file."""
source_file = cu.source_file()
try:
source = source_file.read()
finally:
source_file.close()
# Find out if the file on disk is already correct.
flat_rootname = cu.flat_rootname()
this_hash = self.file_hash(source, cu)
that_hash = self.status.file_hash(flat_rootname)
if this_hash == that_hash:
# Nothing has changed to require the file to be reported again.
self.files.append(self.status.index_info(flat_rootname))
return
self.status.set_file_hash(flat_rootname, this_hash)
# If need be, determine the encoding of the source file. We use it
# later to properly write the HTML.
if sys.version_info < (3, 0):
encoding = source_encoding(source)
# Some UTF8 files have the dreaded UTF8 BOM. If so, junk it.
if encoding.startswith("utf-8") and source[:3] == "\xef\xbb\xbf":
source = source[3:]
encoding = "utf-8"
# Get the numbers for this file.
nums = analysis.numbers
if self.arcs:
missing_branch_arcs = analysis.missing_branch_arcs()
# These classes determine which lines are highlighted by default.
c_run = "run hide_run"
c_exc = "exc"
c_mis = "mis"
c_par = "par " + c_run
lines = []
for lineno, line in enumerate(source_token_lines(source)):
lineno += 1 # 1-based line numbers.
# Figure out how to mark this line.
line_class = []
annotate_html = ""
annotate_title = ""
if lineno in analysis.statements:
line_class.append("stm")
if lineno in analysis.excluded:
line_class.append(c_exc)
elif lineno in analysis.missing:
line_class.append(c_mis)
elif self.arcs and lineno in missing_branch_arcs:
line_class.append(c_par)
annlines = []
for b in missing_branch_arcs[lineno]:
if b < 0:
annlines.append("exit")
else:
annlines.append(str(b))
annotate_html = " ".join(annlines)
if len(annlines) > 1:
annotate_title = "no jumps to these line numbers"
elif len(annlines) == 1:
annotate_title = "no jump to this line number"
elif lineno in analysis.statements:
line_class.append(c_run)
# Build the HTML for the line
html = []
for tok_type, tok_text in line:
if tok_type == "ws":
html.append(escape(tok_text))
else:
tok_html = escape(tok_text) or ' '
html.append(
"<span class='%s'>%s</span>" % (tok_type, tok_html)
)
lines.append({
'html': ''.join(html),
'number': lineno,
'class': ' '.join(line_class) or "pln",
'annotate': annotate_html,
'annotate_title': annotate_title,
})
# Write the HTML page for this file.
html = spaceless(self.source_tmpl.render({
'c_exc': c_exc, 'c_mis': c_mis, 'c_par': c_par, 'c_run': c_run,
'arcs': self.arcs, 'extra_css': self.extra_css,
'cu': cu, 'nums': nums, 'lines': lines,
}))
if sys.version_info < (3, 0):
html = html.decode(encoding)
html_filename = flat_rootname + ".html"
html_path = os.path.join(self.directory, html_filename)
self.write_html(html_path, html)
# Save this file's information for the index file.
index_info = {
'nums': nums,
'html_filename': html_filename,
'name': cu.name,
}
self.files.append(index_info)
self.status.set_index_info(flat_rootname, index_info) | [
"Generate",
"an",
"HTML",
"file",
"for",
"one",
"source",
"file",
"."
] | tnkteja/myhelp | python | https://github.com/tnkteja/myhelp/blob/fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb/virtualEnvironment/lib/python2.7/site-packages/coverage/html.py#L156-L266 | [
"def",
"html_file",
"(",
"self",
",",
"cu",
",",
"analysis",
")",
":",
"source_file",
"=",
"cu",
".",
"source_file",
"(",
")",
"try",
":",
"source",
"=",
"source_file",
".",
"read",
"(",
")",
"finally",
":",
"source_file",
".",
"close",
"(",
")",
"# Find out if the file on disk is already correct.",
"flat_rootname",
"=",
"cu",
".",
"flat_rootname",
"(",
")",
"this_hash",
"=",
"self",
".",
"file_hash",
"(",
"source",
",",
"cu",
")",
"that_hash",
"=",
"self",
".",
"status",
".",
"file_hash",
"(",
"flat_rootname",
")",
"if",
"this_hash",
"==",
"that_hash",
":",
"# Nothing has changed to require the file to be reported again.",
"self",
".",
"files",
".",
"append",
"(",
"self",
".",
"status",
".",
"index_info",
"(",
"flat_rootname",
")",
")",
"return",
"self",
".",
"status",
".",
"set_file_hash",
"(",
"flat_rootname",
",",
"this_hash",
")",
"# If need be, determine the encoding of the source file. We use it",
"# later to properly write the HTML.",
"if",
"sys",
".",
"version_info",
"<",
"(",
"3",
",",
"0",
")",
":",
"encoding",
"=",
"source_encoding",
"(",
"source",
")",
"# Some UTF8 files have the dreaded UTF8 BOM. If so, junk it.",
"if",
"encoding",
".",
"startswith",
"(",
"\"utf-8\"",
")",
"and",
"source",
"[",
":",
"3",
"]",
"==",
"\"\\xef\\xbb\\xbf\"",
":",
"source",
"=",
"source",
"[",
"3",
":",
"]",
"encoding",
"=",
"\"utf-8\"",
"# Get the numbers for this file.",
"nums",
"=",
"analysis",
".",
"numbers",
"if",
"self",
".",
"arcs",
":",
"missing_branch_arcs",
"=",
"analysis",
".",
"missing_branch_arcs",
"(",
")",
"# These classes determine which lines are highlighted by default.",
"c_run",
"=",
"\"run hide_run\"",
"c_exc",
"=",
"\"exc\"",
"c_mis",
"=",
"\"mis\"",
"c_par",
"=",
"\"par \"",
"+",
"c_run",
"lines",
"=",
"[",
"]",
"for",
"lineno",
",",
"line",
"in",
"enumerate",
"(",
"source_token_lines",
"(",
"source",
")",
")",
":",
"lineno",
"+=",
"1",
"# 1-based line numbers.",
"# Figure out how to mark this line.",
"line_class",
"=",
"[",
"]",
"annotate_html",
"=",
"\"\"",
"annotate_title",
"=",
"\"\"",
"if",
"lineno",
"in",
"analysis",
".",
"statements",
":",
"line_class",
".",
"append",
"(",
"\"stm\"",
")",
"if",
"lineno",
"in",
"analysis",
".",
"excluded",
":",
"line_class",
".",
"append",
"(",
"c_exc",
")",
"elif",
"lineno",
"in",
"analysis",
".",
"missing",
":",
"line_class",
".",
"append",
"(",
"c_mis",
")",
"elif",
"self",
".",
"arcs",
"and",
"lineno",
"in",
"missing_branch_arcs",
":",
"line_class",
".",
"append",
"(",
"c_par",
")",
"annlines",
"=",
"[",
"]",
"for",
"b",
"in",
"missing_branch_arcs",
"[",
"lineno",
"]",
":",
"if",
"b",
"<",
"0",
":",
"annlines",
".",
"append",
"(",
"\"exit\"",
")",
"else",
":",
"annlines",
".",
"append",
"(",
"str",
"(",
"b",
")",
")",
"annotate_html",
"=",
"\" \"",
".",
"join",
"(",
"annlines",
")",
"if",
"len",
"(",
"annlines",
")",
">",
"1",
":",
"annotate_title",
"=",
"\"no jumps to these line numbers\"",
"elif",
"len",
"(",
"annlines",
")",
"==",
"1",
":",
"annotate_title",
"=",
"\"no jump to this line number\"",
"elif",
"lineno",
"in",
"analysis",
".",
"statements",
":",
"line_class",
".",
"append",
"(",
"c_run",
")",
"# Build the HTML for the line",
"html",
"=",
"[",
"]",
"for",
"tok_type",
",",
"tok_text",
"in",
"line",
":",
"if",
"tok_type",
"==",
"\"ws\"",
":",
"html",
".",
"append",
"(",
"escape",
"(",
"tok_text",
")",
")",
"else",
":",
"tok_html",
"=",
"escape",
"(",
"tok_text",
")",
"or",
"' '",
"html",
".",
"append",
"(",
"\"<span class='%s'>%s</span>\"",
"%",
"(",
"tok_type",
",",
"tok_html",
")",
")",
"lines",
".",
"append",
"(",
"{",
"'html'",
":",
"''",
".",
"join",
"(",
"html",
")",
",",
"'number'",
":",
"lineno",
",",
"'class'",
":",
"' '",
".",
"join",
"(",
"line_class",
")",
"or",
"\"pln\"",
",",
"'annotate'",
":",
"annotate_html",
",",
"'annotate_title'",
":",
"annotate_title",
",",
"}",
")",
"# Write the HTML page for this file.",
"html",
"=",
"spaceless",
"(",
"self",
".",
"source_tmpl",
".",
"render",
"(",
"{",
"'c_exc'",
":",
"c_exc",
",",
"'c_mis'",
":",
"c_mis",
",",
"'c_par'",
":",
"c_par",
",",
"'c_run'",
":",
"c_run",
",",
"'arcs'",
":",
"self",
".",
"arcs",
",",
"'extra_css'",
":",
"self",
".",
"extra_css",
",",
"'cu'",
":",
"cu",
",",
"'nums'",
":",
"nums",
",",
"'lines'",
":",
"lines",
",",
"}",
")",
")",
"if",
"sys",
".",
"version_info",
"<",
"(",
"3",
",",
"0",
")",
":",
"html",
"=",
"html",
".",
"decode",
"(",
"encoding",
")",
"html_filename",
"=",
"flat_rootname",
"+",
"\".html\"",
"html_path",
"=",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"directory",
",",
"html_filename",
")",
"self",
".",
"write_html",
"(",
"html_path",
",",
"html",
")",
"# Save this file's information for the index file.",
"index_info",
"=",
"{",
"'nums'",
":",
"nums",
",",
"'html_filename'",
":",
"html_filename",
",",
"'name'",
":",
"cu",
".",
"name",
",",
"}",
"self",
".",
"files",
".",
"append",
"(",
"index_info",
")",
"self",
".",
"status",
".",
"set_index_info",
"(",
"flat_rootname",
",",
"index_info",
")"
] | fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb |
test | HtmlReporter.index_file | Write the index.html file for this report. | virtualEnvironment/lib/python2.7/site-packages/coverage/html.py | def index_file(self):
"""Write the index.html file for this report."""
index_tmpl = Templite(
data("index.html"), self.template_globals
)
self.totals = sum([f['nums'] for f in self.files])
html = index_tmpl.render({
'arcs': self.arcs,
'extra_css': self.extra_css,
'files': self.files,
'totals': self.totals,
})
if sys.version_info < (3, 0):
html = html.decode("utf-8")
self.write_html(
os.path.join(self.directory, "index.html"),
html
)
# Write the latest hashes for next time.
self.status.write(self.directory) | def index_file(self):
"""Write the index.html file for this report."""
index_tmpl = Templite(
data("index.html"), self.template_globals
)
self.totals = sum([f['nums'] for f in self.files])
html = index_tmpl.render({
'arcs': self.arcs,
'extra_css': self.extra_css,
'files': self.files,
'totals': self.totals,
})
if sys.version_info < (3, 0):
html = html.decode("utf-8")
self.write_html(
os.path.join(self.directory, "index.html"),
html
)
# Write the latest hashes for next time.
self.status.write(self.directory) | [
"Write",
"the",
"index",
".",
"html",
"file",
"for",
"this",
"report",
"."
] | tnkteja/myhelp | python | https://github.com/tnkteja/myhelp/blob/fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb/virtualEnvironment/lib/python2.7/site-packages/coverage/html.py#L268-L291 | [
"def",
"index_file",
"(",
"self",
")",
":",
"index_tmpl",
"=",
"Templite",
"(",
"data",
"(",
"\"index.html\"",
")",
",",
"self",
".",
"template_globals",
")",
"self",
".",
"totals",
"=",
"sum",
"(",
"[",
"f",
"[",
"'nums'",
"]",
"for",
"f",
"in",
"self",
".",
"files",
"]",
")",
"html",
"=",
"index_tmpl",
".",
"render",
"(",
"{",
"'arcs'",
":",
"self",
".",
"arcs",
",",
"'extra_css'",
":",
"self",
".",
"extra_css",
",",
"'files'",
":",
"self",
".",
"files",
",",
"'totals'",
":",
"self",
".",
"totals",
",",
"}",
")",
"if",
"sys",
".",
"version_info",
"<",
"(",
"3",
",",
"0",
")",
":",
"html",
"=",
"html",
".",
"decode",
"(",
"\"utf-8\"",
")",
"self",
".",
"write_html",
"(",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"directory",
",",
"\"index.html\"",
")",
",",
"html",
")",
"# Write the latest hashes for next time.",
"self",
".",
"status",
".",
"write",
"(",
"self",
".",
"directory",
")"
] | fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb |
test | HtmlStatus.read | Read the last status in `directory`. | virtualEnvironment/lib/python2.7/site-packages/coverage/html.py | def read(self, directory):
"""Read the last status in `directory`."""
usable = False
try:
status_file = os.path.join(directory, self.STATUS_FILE)
fstatus = open(status_file, "rb")
try:
status = pickle.load(fstatus)
finally:
fstatus.close()
except (IOError, ValueError):
usable = False
else:
usable = True
if status['format'] != self.STATUS_FORMAT:
usable = False
elif status['version'] != coverage.__version__:
usable = False
if usable:
self.files = status['files']
self.settings = status['settings']
else:
self.reset() | def read(self, directory):
"""Read the last status in `directory`."""
usable = False
try:
status_file = os.path.join(directory, self.STATUS_FILE)
fstatus = open(status_file, "rb")
try:
status = pickle.load(fstatus)
finally:
fstatus.close()
except (IOError, ValueError):
usable = False
else:
usable = True
if status['format'] != self.STATUS_FORMAT:
usable = False
elif status['version'] != coverage.__version__:
usable = False
if usable:
self.files = status['files']
self.settings = status['settings']
else:
self.reset() | [
"Read",
"the",
"last",
"status",
"in",
"directory",
"."
] | tnkteja/myhelp | python | https://github.com/tnkteja/myhelp/blob/fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb/virtualEnvironment/lib/python2.7/site-packages/coverage/html.py#L308-L331 | [
"def",
"read",
"(",
"self",
",",
"directory",
")",
":",
"usable",
"=",
"False",
"try",
":",
"status_file",
"=",
"os",
".",
"path",
".",
"join",
"(",
"directory",
",",
"self",
".",
"STATUS_FILE",
")",
"fstatus",
"=",
"open",
"(",
"status_file",
",",
"\"rb\"",
")",
"try",
":",
"status",
"=",
"pickle",
".",
"load",
"(",
"fstatus",
")",
"finally",
":",
"fstatus",
".",
"close",
"(",
")",
"except",
"(",
"IOError",
",",
"ValueError",
")",
":",
"usable",
"=",
"False",
"else",
":",
"usable",
"=",
"True",
"if",
"status",
"[",
"'format'",
"]",
"!=",
"self",
".",
"STATUS_FORMAT",
":",
"usable",
"=",
"False",
"elif",
"status",
"[",
"'version'",
"]",
"!=",
"coverage",
".",
"__version__",
":",
"usable",
"=",
"False",
"if",
"usable",
":",
"self",
".",
"files",
"=",
"status",
"[",
"'files'",
"]",
"self",
".",
"settings",
"=",
"status",
"[",
"'settings'",
"]",
"else",
":",
"self",
".",
"reset",
"(",
")"
] | fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb |
test | HtmlStatus.write | Write the current status to `directory`. | virtualEnvironment/lib/python2.7/site-packages/coverage/html.py | def write(self, directory):
"""Write the current status to `directory`."""
status_file = os.path.join(directory, self.STATUS_FILE)
status = {
'format': self.STATUS_FORMAT,
'version': coverage.__version__,
'settings': self.settings,
'files': self.files,
}
fout = open(status_file, "wb")
try:
pickle.dump(status, fout)
finally:
fout.close() | def write(self, directory):
"""Write the current status to `directory`."""
status_file = os.path.join(directory, self.STATUS_FILE)
status = {
'format': self.STATUS_FORMAT,
'version': coverage.__version__,
'settings': self.settings,
'files': self.files,
}
fout = open(status_file, "wb")
try:
pickle.dump(status, fout)
finally:
fout.close() | [
"Write",
"the",
"current",
"status",
"to",
"directory",
"."
] | tnkteja/myhelp | python | https://github.com/tnkteja/myhelp/blob/fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb/virtualEnvironment/lib/python2.7/site-packages/coverage/html.py#L333-L346 | [
"def",
"write",
"(",
"self",
",",
"directory",
")",
":",
"status_file",
"=",
"os",
".",
"path",
".",
"join",
"(",
"directory",
",",
"self",
".",
"STATUS_FILE",
")",
"status",
"=",
"{",
"'format'",
":",
"self",
".",
"STATUS_FORMAT",
",",
"'version'",
":",
"coverage",
".",
"__version__",
",",
"'settings'",
":",
"self",
".",
"settings",
",",
"'files'",
":",
"self",
".",
"files",
",",
"}",
"fout",
"=",
"open",
"(",
"status_file",
",",
"\"wb\"",
")",
"try",
":",
"pickle",
".",
"dump",
"(",
"status",
",",
"fout",
")",
"finally",
":",
"fout",
".",
"close",
"(",
")"
] | fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb |
test | uniq_stable | uniq_stable(elems) -> list
Return from an iterable, a list of all the unique elements in the input,
but maintaining the order in which they first appear.
A naive solution to this problem which just makes a dictionary with the
elements as keys fails to respect the stability condition, since
dictionaries are unsorted by nature.
Note: All elements in the input must be valid dictionary keys for this
routine to work, as it internally uses a dictionary for efficiency
reasons. | environment/lib/python2.7/site-packages/IPython/utils/data.py | def uniq_stable(elems):
"""uniq_stable(elems) -> list
Return from an iterable, a list of all the unique elements in the input,
but maintaining the order in which they first appear.
A naive solution to this problem which just makes a dictionary with the
elements as keys fails to respect the stability condition, since
dictionaries are unsorted by nature.
Note: All elements in the input must be valid dictionary keys for this
routine to work, as it internally uses a dictionary for efficiency
reasons."""
unique = []
unique_dict = {}
for nn in elems:
if nn not in unique_dict:
unique.append(nn)
unique_dict[nn] = None
return unique | def uniq_stable(elems):
"""uniq_stable(elems) -> list
Return from an iterable, a list of all the unique elements in the input,
but maintaining the order in which they first appear.
A naive solution to this problem which just makes a dictionary with the
elements as keys fails to respect the stability condition, since
dictionaries are unsorted by nature.
Note: All elements in the input must be valid dictionary keys for this
routine to work, as it internally uses a dictionary for efficiency
reasons."""
unique = []
unique_dict = {}
for nn in elems:
if nn not in unique_dict:
unique.append(nn)
unique_dict[nn] = None
return unique | [
"uniq_stable",
"(",
"elems",
")",
"-",
">",
"list"
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/utils/data.py#L22-L42 | [
"def",
"uniq_stable",
"(",
"elems",
")",
":",
"unique",
"=",
"[",
"]",
"unique_dict",
"=",
"{",
"}",
"for",
"nn",
"in",
"elems",
":",
"if",
"nn",
"not",
"in",
"unique_dict",
":",
"unique",
".",
"append",
"(",
"nn",
")",
"unique_dict",
"[",
"nn",
"]",
"=",
"None",
"return",
"unique"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | sort_compare | Sort and compare two lists.
By default it does it in place, thus modifying the lists. Use inplace = 0
to avoid that (at the cost of temporary copy creation). | environment/lib/python2.7/site-packages/IPython/utils/data.py | def sort_compare(lst1, lst2, inplace=1):
"""Sort and compare two lists.
By default it does it in place, thus modifying the lists. Use inplace = 0
to avoid that (at the cost of temporary copy creation)."""
if not inplace:
lst1 = lst1[:]
lst2 = lst2[:]
lst1.sort(); lst2.sort()
return lst1 == lst2 | def sort_compare(lst1, lst2, inplace=1):
"""Sort and compare two lists.
By default it does it in place, thus modifying the lists. Use inplace = 0
to avoid that (at the cost of temporary copy creation)."""
if not inplace:
lst1 = lst1[:]
lst2 = lst2[:]
lst1.sort(); lst2.sort()
return lst1 == lst2 | [
"Sort",
"and",
"compare",
"two",
"lists",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/utils/data.py#L45-L54 | [
"def",
"sort_compare",
"(",
"lst1",
",",
"lst2",
",",
"inplace",
"=",
"1",
")",
":",
"if",
"not",
"inplace",
":",
"lst1",
"=",
"lst1",
"[",
":",
"]",
"lst2",
"=",
"lst2",
"[",
":",
"]",
"lst1",
".",
"sort",
"(",
")",
"lst2",
".",
"sort",
"(",
")",
"return",
"lst1",
"==",
"lst2"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | list2dict | Takes a list of (key,value) pairs and turns it into a dict. | environment/lib/python2.7/site-packages/IPython/utils/data.py | def list2dict(lst):
"""Takes a list of (key,value) pairs and turns it into a dict."""
dic = {}
for k,v in lst: dic[k] = v
return dic | def list2dict(lst):
"""Takes a list of (key,value) pairs and turns it into a dict."""
dic = {}
for k,v in lst: dic[k] = v
return dic | [
"Takes",
"a",
"list",
"of",
"(",
"key",
"value",
")",
"pairs",
"and",
"turns",
"it",
"into",
"a",
"dict",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/utils/data.py#L57-L62 | [
"def",
"list2dict",
"(",
"lst",
")",
":",
"dic",
"=",
"{",
"}",
"for",
"k",
",",
"v",
"in",
"lst",
":",
"dic",
"[",
"k",
"]",
"=",
"v",
"return",
"dic"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | list2dict2 | Takes a list and turns it into a dict.
Much slower than list2dict, but more versatile. This version can take
lists with sublists of arbitrary length (including sclars). | environment/lib/python2.7/site-packages/IPython/utils/data.py | def list2dict2(lst, default=''):
"""Takes a list and turns it into a dict.
Much slower than list2dict, but more versatile. This version can take
lists with sublists of arbitrary length (including sclars)."""
dic = {}
for elem in lst:
if type(elem) in (types.ListType,types.TupleType):
size = len(elem)
if size == 0:
pass
elif size == 1:
dic[elem] = default
else:
k,v = elem[0], elem[1:]
if len(v) == 1: v = v[0]
dic[k] = v
else:
dic[elem] = default
return dic | def list2dict2(lst, default=''):
"""Takes a list and turns it into a dict.
Much slower than list2dict, but more versatile. This version can take
lists with sublists of arbitrary length (including sclars)."""
dic = {}
for elem in lst:
if type(elem) in (types.ListType,types.TupleType):
size = len(elem)
if size == 0:
pass
elif size == 1:
dic[elem] = default
else:
k,v = elem[0], elem[1:]
if len(v) == 1: v = v[0]
dic[k] = v
else:
dic[elem] = default
return dic | [
"Takes",
"a",
"list",
"and",
"turns",
"it",
"into",
"a",
"dict",
".",
"Much",
"slower",
"than",
"list2dict",
"but",
"more",
"versatile",
".",
"This",
"version",
"can",
"take",
"lists",
"with",
"sublists",
"of",
"arbitrary",
"length",
"(",
"including",
"sclars",
")",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/utils/data.py#L65-L84 | [
"def",
"list2dict2",
"(",
"lst",
",",
"default",
"=",
"''",
")",
":",
"dic",
"=",
"{",
"}",
"for",
"elem",
"in",
"lst",
":",
"if",
"type",
"(",
"elem",
")",
"in",
"(",
"types",
".",
"ListType",
",",
"types",
".",
"TupleType",
")",
":",
"size",
"=",
"len",
"(",
"elem",
")",
"if",
"size",
"==",
"0",
":",
"pass",
"elif",
"size",
"==",
"1",
":",
"dic",
"[",
"elem",
"]",
"=",
"default",
"else",
":",
"k",
",",
"v",
"=",
"elem",
"[",
"0",
"]",
",",
"elem",
"[",
"1",
":",
"]",
"if",
"len",
"(",
"v",
")",
"==",
"1",
":",
"v",
"=",
"v",
"[",
"0",
"]",
"dic",
"[",
"k",
"]",
"=",
"v",
"else",
":",
"dic",
"[",
"elem",
"]",
"=",
"default",
"return",
"dic"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | get_slice | Get a slice of a sequence with variable step. Specify start,stop,step. | environment/lib/python2.7/site-packages/IPython/utils/data.py | def get_slice(seq, start=0, stop=None, step=1):
"""Get a slice of a sequence with variable step. Specify start,stop,step."""
if stop == None:
stop = len(seq)
item = lambda i: seq[i]
return map(item,xrange(start,stop,step)) | def get_slice(seq, start=0, stop=None, step=1):
"""Get a slice of a sequence with variable step. Specify start,stop,step."""
if stop == None:
stop = len(seq)
item = lambda i: seq[i]
return map(item,xrange(start,stop,step)) | [
"Get",
"a",
"slice",
"of",
"a",
"sequence",
"with",
"variable",
"step",
".",
"Specify",
"start",
"stop",
"step",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/utils/data.py#L93-L98 | [
"def",
"get_slice",
"(",
"seq",
",",
"start",
"=",
"0",
",",
"stop",
"=",
"None",
",",
"step",
"=",
"1",
")",
":",
"if",
"stop",
"==",
"None",
":",
"stop",
"=",
"len",
"(",
"seq",
")",
"item",
"=",
"lambda",
"i",
":",
"seq",
"[",
"i",
"]",
"return",
"map",
"(",
"item",
",",
"xrange",
"(",
"start",
",",
"stop",
",",
"step",
")",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | chop | Chop a sequence into chunks of the given size. | environment/lib/python2.7/site-packages/IPython/utils/data.py | def chop(seq, size):
"""Chop a sequence into chunks of the given size."""
chunk = lambda i: seq[i:i+size]
return map(chunk,xrange(0,len(seq),size)) | def chop(seq, size):
"""Chop a sequence into chunks of the given size."""
chunk = lambda i: seq[i:i+size]
return map(chunk,xrange(0,len(seq),size)) | [
"Chop",
"a",
"sequence",
"into",
"chunks",
"of",
"the",
"given",
"size",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/utils/data.py#L101-L104 | [
"def",
"chop",
"(",
"seq",
",",
"size",
")",
":",
"chunk",
"=",
"lambda",
"i",
":",
"seq",
"[",
"i",
":",
"i",
"+",
"size",
"]",
"return",
"map",
"(",
"chunk",
",",
"xrange",
"(",
"0",
",",
"len",
"(",
"seq",
")",
",",
"size",
")",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | read_config | Read configuration from setup.cfg. | virtualEnvironment/lib/python2.7/site-packages/check_manifest.py | def read_config():
"""Read configuration from setup.cfg."""
# XXX modifies global state, which is kind of evil
config = ConfigParser.ConfigParser()
config.read(['setup.cfg'])
if not config.has_section('check-manifest'):
return
if (config.has_option('check-manifest', 'ignore-default-rules')
and config.getboolean('check-manifest', 'ignore-default-rules')):
del IGNORE[:]
if config.has_option('check-manifest', 'ignore'):
patterns = [p.strip() for p in config.get('check-manifest',
'ignore').splitlines()]
IGNORE.extend(p for p in patterns if p) | def read_config():
"""Read configuration from setup.cfg."""
# XXX modifies global state, which is kind of evil
config = ConfigParser.ConfigParser()
config.read(['setup.cfg'])
if not config.has_section('check-manifest'):
return
if (config.has_option('check-manifest', 'ignore-default-rules')
and config.getboolean('check-manifest', 'ignore-default-rules')):
del IGNORE[:]
if config.has_option('check-manifest', 'ignore'):
patterns = [p.strip() for p in config.get('check-manifest',
'ignore').splitlines()]
IGNORE.extend(p for p in patterns if p) | [
"Read",
"configuration",
"from",
"setup",
".",
"cfg",
"."
] | tnkteja/myhelp | python | https://github.com/tnkteja/myhelp/blob/fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb/virtualEnvironment/lib/python2.7/site-packages/check_manifest.py#L462-L475 | [
"def",
"read_config",
"(",
")",
":",
"# XXX modifies global state, which is kind of evil",
"config",
"=",
"ConfigParser",
".",
"ConfigParser",
"(",
")",
"config",
".",
"read",
"(",
"[",
"'setup.cfg'",
"]",
")",
"if",
"not",
"config",
".",
"has_section",
"(",
"'check-manifest'",
")",
":",
"return",
"if",
"(",
"config",
".",
"has_option",
"(",
"'check-manifest'",
",",
"'ignore-default-rules'",
")",
"and",
"config",
".",
"getboolean",
"(",
"'check-manifest'",
",",
"'ignore-default-rules'",
")",
")",
":",
"del",
"IGNORE",
"[",
":",
"]",
"if",
"config",
".",
"has_option",
"(",
"'check-manifest'",
",",
"'ignore'",
")",
":",
"patterns",
"=",
"[",
"p",
".",
"strip",
"(",
")",
"for",
"p",
"in",
"config",
".",
"get",
"(",
"'check-manifest'",
",",
"'ignore'",
")",
".",
"splitlines",
"(",
")",
"]",
"IGNORE",
".",
"extend",
"(",
"p",
"for",
"p",
"in",
"patterns",
"if",
"p",
")"
] | fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb |
test | read_manifest | Read existing configuration from MANIFEST.in.
We use that to ignore anything the MANIFEST.in ignores. | virtualEnvironment/lib/python2.7/site-packages/check_manifest.py | def read_manifest():
"""Read existing configuration from MANIFEST.in.
We use that to ignore anything the MANIFEST.in ignores.
"""
# XXX modifies global state, which is kind of evil
if not os.path.isfile('MANIFEST.in'):
return
with open('MANIFEST.in') as manifest:
contents = manifest.read()
ignore, ignore_regexps = _get_ignore_from_manifest(contents)
IGNORE.extend(ignore)
IGNORE_REGEXPS.extend(ignore_regexps) | def read_manifest():
"""Read existing configuration from MANIFEST.in.
We use that to ignore anything the MANIFEST.in ignores.
"""
# XXX modifies global state, which is kind of evil
if not os.path.isfile('MANIFEST.in'):
return
with open('MANIFEST.in') as manifest:
contents = manifest.read()
ignore, ignore_regexps = _get_ignore_from_manifest(contents)
IGNORE.extend(ignore)
IGNORE_REGEXPS.extend(ignore_regexps) | [
"Read",
"existing",
"configuration",
"from",
"MANIFEST",
".",
"in",
"."
] | tnkteja/myhelp | python | https://github.com/tnkteja/myhelp/blob/fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb/virtualEnvironment/lib/python2.7/site-packages/check_manifest.py#L478-L490 | [
"def",
"read_manifest",
"(",
")",
":",
"# XXX modifies global state, which is kind of evil",
"if",
"not",
"os",
".",
"path",
".",
"isfile",
"(",
"'MANIFEST.in'",
")",
":",
"return",
"with",
"open",
"(",
"'MANIFEST.in'",
")",
"as",
"manifest",
":",
"contents",
"=",
"manifest",
".",
"read",
"(",
")",
"ignore",
",",
"ignore_regexps",
"=",
"_get_ignore_from_manifest",
"(",
"contents",
")",
"IGNORE",
".",
"extend",
"(",
"ignore",
")",
"IGNORE_REGEXPS",
".",
"extend",
"(",
"ignore_regexps",
")"
] | fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb |
test | _glob_to_regexp | Compile a glob pattern into a regexp.
We need to do this because fnmatch allows * to match /, which we
don't want. E.g. an MANIFEST.in exclude of 'dirname/*css' should
match 'dirname/foo.css' but not 'dirname/subdir/bar.css'. | virtualEnvironment/lib/python2.7/site-packages/check_manifest.py | def _glob_to_regexp(pat):
"""Compile a glob pattern into a regexp.
We need to do this because fnmatch allows * to match /, which we
don't want. E.g. an MANIFEST.in exclude of 'dirname/*css' should
match 'dirname/foo.css' but not 'dirname/subdir/bar.css'.
"""
pat = fnmatch.translate(pat)
# Note that distutils in Python 2.6 has a buggy glob_to_re in
# distutils.filelist -- it converts '*.cfg' to '[^/]*cfg' instead
# of '[^\\]*cfg' on Windows.
sep = r'\\\\' if os.path.sep == '\\' else os.path.sep
return re.sub(r'((?<!\\)(\\\\)*)\.', r'\1[^%s]' % sep, pat) | def _glob_to_regexp(pat):
"""Compile a glob pattern into a regexp.
We need to do this because fnmatch allows * to match /, which we
don't want. E.g. an MANIFEST.in exclude of 'dirname/*css' should
match 'dirname/foo.css' but not 'dirname/subdir/bar.css'.
"""
pat = fnmatch.translate(pat)
# Note that distutils in Python 2.6 has a buggy glob_to_re in
# distutils.filelist -- it converts '*.cfg' to '[^/]*cfg' instead
# of '[^\\]*cfg' on Windows.
sep = r'\\\\' if os.path.sep == '\\' else os.path.sep
return re.sub(r'((?<!\\)(\\\\)*)\.', r'\1[^%s]' % sep, pat) | [
"Compile",
"a",
"glob",
"pattern",
"into",
"a",
"regexp",
"."
] | tnkteja/myhelp | python | https://github.com/tnkteja/myhelp/blob/fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb/virtualEnvironment/lib/python2.7/site-packages/check_manifest.py#L493-L505 | [
"def",
"_glob_to_regexp",
"(",
"pat",
")",
":",
"pat",
"=",
"fnmatch",
".",
"translate",
"(",
"pat",
")",
"# Note that distutils in Python 2.6 has a buggy glob_to_re in",
"# distutils.filelist -- it converts '*.cfg' to '[^/]*cfg' instead",
"# of '[^\\\\]*cfg' on Windows.",
"sep",
"=",
"r'\\\\\\\\'",
"if",
"os",
".",
"path",
".",
"sep",
"==",
"'\\\\'",
"else",
"os",
".",
"path",
".",
"sep",
"return",
"re",
".",
"sub",
"(",
"r'((?<!\\\\)(\\\\\\\\)*)\\.'",
",",
"r'\\1[^%s]'",
"%",
"sep",
",",
"pat",
")"
] | fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb |
test | file_matches | Does this filename match any of the patterns? | virtualEnvironment/lib/python2.7/site-packages/check_manifest.py | def file_matches(filename, patterns):
"""Does this filename match any of the patterns?"""
return any(fnmatch.fnmatch(filename, pat) for pat in patterns) | def file_matches(filename, patterns):
"""Does this filename match any of the patterns?"""
return any(fnmatch.fnmatch(filename, pat) for pat in patterns) | [
"Does",
"this",
"filename",
"match",
"any",
"of",
"the",
"patterns?"
] | tnkteja/myhelp | python | https://github.com/tnkteja/myhelp/blob/fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb/virtualEnvironment/lib/python2.7/site-packages/check_manifest.py#L580-L582 | [
"def",
"file_matches",
"(",
"filename",
",",
"patterns",
")",
":",
"return",
"any",
"(",
"fnmatch",
".",
"fnmatch",
"(",
"filename",
",",
"pat",
")",
"for",
"pat",
"in",
"patterns",
")"
] | fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb |
test | Git.get_versioned_files | List all files versioned by git in the current directory. | virtualEnvironment/lib/python2.7/site-packages/check_manifest.py | def get_versioned_files():
"""List all files versioned by git in the current directory."""
# Git for Windows uses UTF-8 instead of the locale encoding.
# Regular Git on sane POSIX systems uses the locale encoding
encoding = 'UTF-8' if sys.platform == 'win32' else None
output = run(['git', 'ls-files', '-z'], encoding=encoding)
return add_directories(output.split('\0')[:-1]) | def get_versioned_files():
"""List all files versioned by git in the current directory."""
# Git for Windows uses UTF-8 instead of the locale encoding.
# Regular Git on sane POSIX systems uses the locale encoding
encoding = 'UTF-8' if sys.platform == 'win32' else None
output = run(['git', 'ls-files', '-z'], encoding=encoding)
return add_directories(output.split('\0')[:-1]) | [
"List",
"all",
"files",
"versioned",
"by",
"git",
"in",
"the",
"current",
"directory",
"."
] | tnkteja/myhelp | python | https://github.com/tnkteja/myhelp/blob/fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb/virtualEnvironment/lib/python2.7/site-packages/check_manifest.py#L276-L282 | [
"def",
"get_versioned_files",
"(",
")",
":",
"# Git for Windows uses UTF-8 instead of the locale encoding.",
"# Regular Git on sane POSIX systems uses the locale encoding",
"encoding",
"=",
"'UTF-8'",
"if",
"sys",
".",
"platform",
"==",
"'win32'",
"else",
"None",
"output",
"=",
"run",
"(",
"[",
"'git'",
",",
"'ls-files'",
",",
"'-z'",
"]",
",",
"encoding",
"=",
"encoding",
")",
"return",
"add_directories",
"(",
"output",
".",
"split",
"(",
"'\\0'",
")",
"[",
":",
"-",
"1",
"]",
")"
] | fb3a4809d448ad14d5b2e6ddf2e7e89ad52b71cb |
test | MultiKernelManager.start_kernel | Start a new kernel. | environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/kernelmanager.py | def start_kernel(self, **kwargs):
"""Start a new kernel."""
kernel_id = unicode(uuid.uuid4())
# use base KernelManager for each Kernel
km = self.kernel_manager_factory(connection_file=os.path.join(
self.connection_dir, "kernel-%s.json" % kernel_id),
config=self.config,
)
km.start_kernel(**kwargs)
# start just the shell channel, needed for graceful restart
km.start_channels(shell=True, sub=False, stdin=False, hb=False)
self._kernels[kernel_id] = km
return kernel_id | def start_kernel(self, **kwargs):
"""Start a new kernel."""
kernel_id = unicode(uuid.uuid4())
# use base KernelManager for each Kernel
km = self.kernel_manager_factory(connection_file=os.path.join(
self.connection_dir, "kernel-%s.json" % kernel_id),
config=self.config,
)
km.start_kernel(**kwargs)
# start just the shell channel, needed for graceful restart
km.start_channels(shell=True, sub=False, stdin=False, hb=False)
self._kernels[kernel_id] = km
return kernel_id | [
"Start",
"a",
"new",
"kernel",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/kernelmanager.py#L81-L93 | [
"def",
"start_kernel",
"(",
"self",
",",
"*",
"*",
"kwargs",
")",
":",
"kernel_id",
"=",
"unicode",
"(",
"uuid",
".",
"uuid4",
"(",
")",
")",
"# use base KernelManager for each Kernel",
"km",
"=",
"self",
".",
"kernel_manager_factory",
"(",
"connection_file",
"=",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"connection_dir",
",",
"\"kernel-%s.json\"",
"%",
"kernel_id",
")",
",",
"config",
"=",
"self",
".",
"config",
",",
")",
"km",
".",
"start_kernel",
"(",
"*",
"*",
"kwargs",
")",
"# start just the shell channel, needed for graceful restart",
"km",
".",
"start_channels",
"(",
"shell",
"=",
"True",
",",
"sub",
"=",
"False",
",",
"stdin",
"=",
"False",
",",
"hb",
"=",
"False",
")",
"self",
".",
"_kernels",
"[",
"kernel_id",
"]",
"=",
"km",
"return",
"kernel_id"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | MultiKernelManager.shutdown_kernel | Shutdown a kernel by its kernel uuid.
Parameters
==========
kernel_id : uuid
The id of the kernel to shutdown. | environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/kernelmanager.py | def shutdown_kernel(self, kernel_id):
"""Shutdown a kernel by its kernel uuid.
Parameters
==========
kernel_id : uuid
The id of the kernel to shutdown.
"""
self.get_kernel(kernel_id).shutdown_kernel()
del self._kernels[kernel_id] | def shutdown_kernel(self, kernel_id):
"""Shutdown a kernel by its kernel uuid.
Parameters
==========
kernel_id : uuid
The id of the kernel to shutdown.
"""
self.get_kernel(kernel_id).shutdown_kernel()
del self._kernels[kernel_id] | [
"Shutdown",
"a",
"kernel",
"by",
"its",
"kernel",
"uuid",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/kernelmanager.py#L95-L104 | [
"def",
"shutdown_kernel",
"(",
"self",
",",
"kernel_id",
")",
":",
"self",
".",
"get_kernel",
"(",
"kernel_id",
")",
".",
"shutdown_kernel",
"(",
")",
"del",
"self",
".",
"_kernels",
"[",
"kernel_id",
"]"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | MultiKernelManager.kill_kernel | Kill a kernel by its kernel uuid.
Parameters
==========
kernel_id : uuid
The id of the kernel to kill. | environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/kernelmanager.py | def kill_kernel(self, kernel_id):
"""Kill a kernel by its kernel uuid.
Parameters
==========
kernel_id : uuid
The id of the kernel to kill.
"""
self.get_kernel(kernel_id).kill_kernel()
del self._kernels[kernel_id] | def kill_kernel(self, kernel_id):
"""Kill a kernel by its kernel uuid.
Parameters
==========
kernel_id : uuid
The id of the kernel to kill.
"""
self.get_kernel(kernel_id).kill_kernel()
del self._kernels[kernel_id] | [
"Kill",
"a",
"kernel",
"by",
"its",
"kernel",
"uuid",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/kernelmanager.py#L106-L115 | [
"def",
"kill_kernel",
"(",
"self",
",",
"kernel_id",
")",
":",
"self",
".",
"get_kernel",
"(",
"kernel_id",
")",
".",
"kill_kernel",
"(",
")",
"del",
"self",
".",
"_kernels",
"[",
"kernel_id",
"]"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | MultiKernelManager.get_kernel | Get the single KernelManager object for a kernel by its uuid.
Parameters
==========
kernel_id : uuid
The id of the kernel. | environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/kernelmanager.py | def get_kernel(self, kernel_id):
"""Get the single KernelManager object for a kernel by its uuid.
Parameters
==========
kernel_id : uuid
The id of the kernel.
"""
km = self._kernels.get(kernel_id)
if km is not None:
return km
else:
raise KeyError("Kernel with id not found: %s" % kernel_id) | def get_kernel(self, kernel_id):
"""Get the single KernelManager object for a kernel by its uuid.
Parameters
==========
kernel_id : uuid
The id of the kernel.
"""
km = self._kernels.get(kernel_id)
if km is not None:
return km
else:
raise KeyError("Kernel with id not found: %s" % kernel_id) | [
"Get",
"the",
"single",
"KernelManager",
"object",
"for",
"a",
"kernel",
"by",
"its",
"uuid",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/kernelmanager.py#L140-L152 | [
"def",
"get_kernel",
"(",
"self",
",",
"kernel_id",
")",
":",
"km",
"=",
"self",
".",
"_kernels",
".",
"get",
"(",
"kernel_id",
")",
"if",
"km",
"is",
"not",
"None",
":",
"return",
"km",
"else",
":",
"raise",
"KeyError",
"(",
"\"Kernel with id not found: %s\"",
"%",
"kernel_id",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | MultiKernelManager.get_kernel_ports | Return a dictionary of ports for a kernel.
Parameters
==========
kernel_id : uuid
The id of the kernel.
Returns
=======
port_dict : dict
A dict of key, value pairs where the keys are the names
(stdin_port,iopub_port,shell_port) and the values are the
integer port numbers for those channels. | environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/kernelmanager.py | def get_kernel_ports(self, kernel_id):
"""Return a dictionary of ports for a kernel.
Parameters
==========
kernel_id : uuid
The id of the kernel.
Returns
=======
port_dict : dict
A dict of key, value pairs where the keys are the names
(stdin_port,iopub_port,shell_port) and the values are the
integer port numbers for those channels.
"""
# this will raise a KeyError if not found:
km = self.get_kernel(kernel_id)
return dict(shell_port=km.shell_port,
iopub_port=km.iopub_port,
stdin_port=km.stdin_port,
hb_port=km.hb_port,
) | def get_kernel_ports(self, kernel_id):
"""Return a dictionary of ports for a kernel.
Parameters
==========
kernel_id : uuid
The id of the kernel.
Returns
=======
port_dict : dict
A dict of key, value pairs where the keys are the names
(stdin_port,iopub_port,shell_port) and the values are the
integer port numbers for those channels.
"""
# this will raise a KeyError if not found:
km = self.get_kernel(kernel_id)
return dict(shell_port=km.shell_port,
iopub_port=km.iopub_port,
stdin_port=km.stdin_port,
hb_port=km.hb_port,
) | [
"Return",
"a",
"dictionary",
"of",
"ports",
"for",
"a",
"kernel",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/kernelmanager.py#L154-L175 | [
"def",
"get_kernel_ports",
"(",
"self",
",",
"kernel_id",
")",
":",
"# this will raise a KeyError if not found:",
"km",
"=",
"self",
".",
"get_kernel",
"(",
"kernel_id",
")",
"return",
"dict",
"(",
"shell_port",
"=",
"km",
".",
"shell_port",
",",
"iopub_port",
"=",
"km",
".",
"iopub_port",
",",
"stdin_port",
"=",
"km",
".",
"stdin_port",
",",
"hb_port",
"=",
"km",
".",
"hb_port",
",",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | MappingKernelManager.notebook_for_kernel | Return the notebook_id for a kernel_id or None. | environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/kernelmanager.py | def notebook_for_kernel(self, kernel_id):
"""Return the notebook_id for a kernel_id or None."""
notebook_ids = [k for k, v in self._notebook_mapping.iteritems() if v == kernel_id]
if len(notebook_ids) == 1:
return notebook_ids[0]
else:
return None | def notebook_for_kernel(self, kernel_id):
"""Return the notebook_id for a kernel_id or None."""
notebook_ids = [k for k, v in self._notebook_mapping.iteritems() if v == kernel_id]
if len(notebook_ids) == 1:
return notebook_ids[0]
else:
return None | [
"Return",
"the",
"notebook_id",
"for",
"a",
"kernel_id",
"or",
"None",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/kernelmanager.py#L247-L253 | [
"def",
"notebook_for_kernel",
"(",
"self",
",",
"kernel_id",
")",
":",
"notebook_ids",
"=",
"[",
"k",
"for",
"k",
",",
"v",
"in",
"self",
".",
"_notebook_mapping",
".",
"iteritems",
"(",
")",
"if",
"v",
"==",
"kernel_id",
"]",
"if",
"len",
"(",
"notebook_ids",
")",
"==",
"1",
":",
"return",
"notebook_ids",
"[",
"0",
"]",
"else",
":",
"return",
"None"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | MappingKernelManager.delete_mapping_for_kernel | Remove the kernel/notebook mapping for kernel_id. | environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/kernelmanager.py | def delete_mapping_for_kernel(self, kernel_id):
"""Remove the kernel/notebook mapping for kernel_id."""
notebook_id = self.notebook_for_kernel(kernel_id)
if notebook_id is not None:
del self._notebook_mapping[notebook_id] | def delete_mapping_for_kernel(self, kernel_id):
"""Remove the kernel/notebook mapping for kernel_id."""
notebook_id = self.notebook_for_kernel(kernel_id)
if notebook_id is not None:
del self._notebook_mapping[notebook_id] | [
"Remove",
"the",
"kernel",
"/",
"notebook",
"mapping",
"for",
"kernel_id",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/kernelmanager.py#L255-L259 | [
"def",
"delete_mapping_for_kernel",
"(",
"self",
",",
"kernel_id",
")",
":",
"notebook_id",
"=",
"self",
".",
"notebook_for_kernel",
"(",
"kernel_id",
")",
"if",
"notebook_id",
"is",
"not",
"None",
":",
"del",
"self",
".",
"_notebook_mapping",
"[",
"notebook_id",
"]"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | MappingKernelManager.start_kernel | Start a kernel for a notebok an return its kernel_id.
Parameters
----------
notebook_id : uuid
The uuid of the notebook to associate the new kernel with. If this
is not None, this kernel will be persistent whenever the notebook
requests a kernel. | environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/kernelmanager.py | def start_kernel(self, notebook_id=None, **kwargs):
"""Start a kernel for a notebok an return its kernel_id.
Parameters
----------
notebook_id : uuid
The uuid of the notebook to associate the new kernel with. If this
is not None, this kernel will be persistent whenever the notebook
requests a kernel.
"""
kernel_id = self.kernel_for_notebook(notebook_id)
if kernel_id is None:
kwargs['extra_arguments'] = self.kernel_argv
kernel_id = super(MappingKernelManager, self).start_kernel(**kwargs)
self.set_kernel_for_notebook(notebook_id, kernel_id)
self.log.info("Kernel started: %s" % kernel_id)
self.log.debug("Kernel args: %r" % kwargs)
else:
self.log.info("Using existing kernel: %s" % kernel_id)
return kernel_id | def start_kernel(self, notebook_id=None, **kwargs):
"""Start a kernel for a notebok an return its kernel_id.
Parameters
----------
notebook_id : uuid
The uuid of the notebook to associate the new kernel with. If this
is not None, this kernel will be persistent whenever the notebook
requests a kernel.
"""
kernel_id = self.kernel_for_notebook(notebook_id)
if kernel_id is None:
kwargs['extra_arguments'] = self.kernel_argv
kernel_id = super(MappingKernelManager, self).start_kernel(**kwargs)
self.set_kernel_for_notebook(notebook_id, kernel_id)
self.log.info("Kernel started: %s" % kernel_id)
self.log.debug("Kernel args: %r" % kwargs)
else:
self.log.info("Using existing kernel: %s" % kernel_id)
return kernel_id | [
"Start",
"a",
"kernel",
"for",
"a",
"notebok",
"an",
"return",
"its",
"kernel_id",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/kernelmanager.py#L261-L280 | [
"def",
"start_kernel",
"(",
"self",
",",
"notebook_id",
"=",
"None",
",",
"*",
"*",
"kwargs",
")",
":",
"kernel_id",
"=",
"self",
".",
"kernel_for_notebook",
"(",
"notebook_id",
")",
"if",
"kernel_id",
"is",
"None",
":",
"kwargs",
"[",
"'extra_arguments'",
"]",
"=",
"self",
".",
"kernel_argv",
"kernel_id",
"=",
"super",
"(",
"MappingKernelManager",
",",
"self",
")",
".",
"start_kernel",
"(",
"*",
"*",
"kwargs",
")",
"self",
".",
"set_kernel_for_notebook",
"(",
"notebook_id",
",",
"kernel_id",
")",
"self",
".",
"log",
".",
"info",
"(",
"\"Kernel started: %s\"",
"%",
"kernel_id",
")",
"self",
".",
"log",
".",
"debug",
"(",
"\"Kernel args: %r\"",
"%",
"kwargs",
")",
"else",
":",
"self",
".",
"log",
".",
"info",
"(",
"\"Using existing kernel: %s\"",
"%",
"kernel_id",
")",
"return",
"kernel_id"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | MappingKernelManager.shutdown_kernel | Shutdown a kernel and remove its notebook association. | environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/kernelmanager.py | def shutdown_kernel(self, kernel_id):
"""Shutdown a kernel and remove its notebook association."""
self._check_kernel_id(kernel_id)
super(MappingKernelManager, self).shutdown_kernel(kernel_id)
self.delete_mapping_for_kernel(kernel_id)
self.log.info("Kernel shutdown: %s" % kernel_id) | def shutdown_kernel(self, kernel_id):
"""Shutdown a kernel and remove its notebook association."""
self._check_kernel_id(kernel_id)
super(MappingKernelManager, self).shutdown_kernel(kernel_id)
self.delete_mapping_for_kernel(kernel_id)
self.log.info("Kernel shutdown: %s" % kernel_id) | [
"Shutdown",
"a",
"kernel",
"and",
"remove",
"its",
"notebook",
"association",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/kernelmanager.py#L282-L287 | [
"def",
"shutdown_kernel",
"(",
"self",
",",
"kernel_id",
")",
":",
"self",
".",
"_check_kernel_id",
"(",
"kernel_id",
")",
"super",
"(",
"MappingKernelManager",
",",
"self",
")",
".",
"shutdown_kernel",
"(",
"kernel_id",
")",
"self",
".",
"delete_mapping_for_kernel",
"(",
"kernel_id",
")",
"self",
".",
"log",
".",
"info",
"(",
"\"Kernel shutdown: %s\"",
"%",
"kernel_id",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | MappingKernelManager.interrupt_kernel | Interrupt a kernel. | environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/kernelmanager.py | def interrupt_kernel(self, kernel_id):
"""Interrupt a kernel."""
self._check_kernel_id(kernel_id)
super(MappingKernelManager, self).interrupt_kernel(kernel_id)
self.log.info("Kernel interrupted: %s" % kernel_id) | def interrupt_kernel(self, kernel_id):
"""Interrupt a kernel."""
self._check_kernel_id(kernel_id)
super(MappingKernelManager, self).interrupt_kernel(kernel_id)
self.log.info("Kernel interrupted: %s" % kernel_id) | [
"Interrupt",
"a",
"kernel",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/kernelmanager.py#L296-L300 | [
"def",
"interrupt_kernel",
"(",
"self",
",",
"kernel_id",
")",
":",
"self",
".",
"_check_kernel_id",
"(",
"kernel_id",
")",
"super",
"(",
"MappingKernelManager",
",",
"self",
")",
".",
"interrupt_kernel",
"(",
"kernel_id",
")",
"self",
".",
"log",
".",
"info",
"(",
"\"Kernel interrupted: %s\"",
"%",
"kernel_id",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | MappingKernelManager.restart_kernel | Restart a kernel while keeping clients connected. | environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/kernelmanager.py | def restart_kernel(self, kernel_id):
"""Restart a kernel while keeping clients connected."""
self._check_kernel_id(kernel_id)
km = self.get_kernel(kernel_id)
km.restart_kernel()
self.log.info("Kernel restarted: %s" % kernel_id)
return kernel_id
# the following remains, in case the KM restart machinery is
# somehow unacceptable
# Get the notebook_id to preserve the kernel/notebook association.
notebook_id = self.notebook_for_kernel(kernel_id)
# Create the new kernel first so we can move the clients over.
new_kernel_id = self.start_kernel()
# Now kill the old kernel.
self.kill_kernel(kernel_id)
# Now save the new kernel/notebook association. We have to save it
# after the old kernel is killed as that will delete the mapping.
self.set_kernel_for_notebook(notebook_id, new_kernel_id)
self.log.info("Kernel restarted: %s" % new_kernel_id)
return new_kernel_id | def restart_kernel(self, kernel_id):
"""Restart a kernel while keeping clients connected."""
self._check_kernel_id(kernel_id)
km = self.get_kernel(kernel_id)
km.restart_kernel()
self.log.info("Kernel restarted: %s" % kernel_id)
return kernel_id
# the following remains, in case the KM restart machinery is
# somehow unacceptable
# Get the notebook_id to preserve the kernel/notebook association.
notebook_id = self.notebook_for_kernel(kernel_id)
# Create the new kernel first so we can move the clients over.
new_kernel_id = self.start_kernel()
# Now kill the old kernel.
self.kill_kernel(kernel_id)
# Now save the new kernel/notebook association. We have to save it
# after the old kernel is killed as that will delete the mapping.
self.set_kernel_for_notebook(notebook_id, new_kernel_id)
self.log.info("Kernel restarted: %s" % new_kernel_id)
return new_kernel_id | [
"Restart",
"a",
"kernel",
"while",
"keeping",
"clients",
"connected",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/kernelmanager.py#L302-L322 | [
"def",
"restart_kernel",
"(",
"self",
",",
"kernel_id",
")",
":",
"self",
".",
"_check_kernel_id",
"(",
"kernel_id",
")",
"km",
"=",
"self",
".",
"get_kernel",
"(",
"kernel_id",
")",
"km",
".",
"restart_kernel",
"(",
")",
"self",
".",
"log",
".",
"info",
"(",
"\"Kernel restarted: %s\"",
"%",
"kernel_id",
")",
"return",
"kernel_id",
"# the following remains, in case the KM restart machinery is",
"# somehow unacceptable",
"# Get the notebook_id to preserve the kernel/notebook association.",
"notebook_id",
"=",
"self",
".",
"notebook_for_kernel",
"(",
"kernel_id",
")",
"# Create the new kernel first so we can move the clients over.",
"new_kernel_id",
"=",
"self",
".",
"start_kernel",
"(",
")",
"# Now kill the old kernel.",
"self",
".",
"kill_kernel",
"(",
"kernel_id",
")",
"# Now save the new kernel/notebook association. We have to save it",
"# after the old kernel is killed as that will delete the mapping.",
"self",
".",
"set_kernel_for_notebook",
"(",
"notebook_id",
",",
"new_kernel_id",
")",
"self",
".",
"log",
".",
"info",
"(",
"\"Kernel restarted: %s\"",
"%",
"new_kernel_id",
")",
"return",
"new_kernel_id"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | MappingKernelManager.create_iopub_stream | Create a new iopub stream. | environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/kernelmanager.py | def create_iopub_stream(self, kernel_id):
"""Create a new iopub stream."""
self._check_kernel_id(kernel_id)
return super(MappingKernelManager, self).create_iopub_stream(kernel_id) | def create_iopub_stream(self, kernel_id):
"""Create a new iopub stream."""
self._check_kernel_id(kernel_id)
return super(MappingKernelManager, self).create_iopub_stream(kernel_id) | [
"Create",
"a",
"new",
"iopub",
"stream",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/kernelmanager.py#L324-L327 | [
"def",
"create_iopub_stream",
"(",
"self",
",",
"kernel_id",
")",
":",
"self",
".",
"_check_kernel_id",
"(",
"kernel_id",
")",
"return",
"super",
"(",
"MappingKernelManager",
",",
"self",
")",
".",
"create_iopub_stream",
"(",
"kernel_id",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | MappingKernelManager.create_shell_stream | Create a new shell stream. | environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/kernelmanager.py | def create_shell_stream(self, kernel_id):
"""Create a new shell stream."""
self._check_kernel_id(kernel_id)
return super(MappingKernelManager, self).create_shell_stream(kernel_id) | def create_shell_stream(self, kernel_id):
"""Create a new shell stream."""
self._check_kernel_id(kernel_id)
return super(MappingKernelManager, self).create_shell_stream(kernel_id) | [
"Create",
"a",
"new",
"shell",
"stream",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/kernelmanager.py#L329-L332 | [
"def",
"create_shell_stream",
"(",
"self",
",",
"kernel_id",
")",
":",
"self",
".",
"_check_kernel_id",
"(",
"kernel_id",
")",
"return",
"super",
"(",
"MappingKernelManager",
",",
"self",
")",
".",
"create_shell_stream",
"(",
"kernel_id",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | MappingKernelManager.create_hb_stream | Create a new hb stream. | environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/kernelmanager.py | def create_hb_stream(self, kernel_id):
"""Create a new hb stream."""
self._check_kernel_id(kernel_id)
return super(MappingKernelManager, self).create_hb_stream(kernel_id) | def create_hb_stream(self, kernel_id):
"""Create a new hb stream."""
self._check_kernel_id(kernel_id)
return super(MappingKernelManager, self).create_hb_stream(kernel_id) | [
"Create",
"a",
"new",
"hb",
"stream",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/kernelmanager.py#L334-L337 | [
"def",
"create_hb_stream",
"(",
"self",
",",
"kernel_id",
")",
":",
"self",
".",
"_check_kernel_id",
"(",
"kernel_id",
")",
"return",
"super",
"(",
"MappingKernelManager",
",",
"self",
")",
".",
"create_hb_stream",
"(",
"kernel_id",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | Deprecated.configure | Configure plugin. | environment/lib/python2.7/site-packages/nose/plugins/deprecated.py | def configure(self, options, conf):
"""Configure plugin.
"""
if not self.can_configure:
return
self.conf = conf
disable = getattr(options, 'noDeprecated', False)
if disable:
self.enabled = False | def configure(self, options, conf):
"""Configure plugin.
"""
if not self.can_configure:
return
self.conf = conf
disable = getattr(options, 'noDeprecated', False)
if disable:
self.enabled = False | [
"Configure",
"plugin",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/nose/plugins/deprecated.py#L37-L45 | [
"def",
"configure",
"(",
"self",
",",
"options",
",",
"conf",
")",
":",
"if",
"not",
"self",
".",
"can_configure",
":",
"return",
"self",
".",
"conf",
"=",
"conf",
"disable",
"=",
"getattr",
"(",
"options",
",",
"'noDeprecated'",
",",
"False",
")",
"if",
"disable",
":",
"self",
".",
"enabled",
"=",
"False"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | ResetMixin.reset | Reset all OneTimeProperty attributes that may have fired already. | environment/lib/python2.7/site-packages/IPython/utils/autoattr.py | def reset(self):
"""Reset all OneTimeProperty attributes that may have fired already."""
instdict = self.__dict__
classdict = self.__class__.__dict__
# To reset them, we simply remove them from the instance dict. At that
# point, it's as if they had never been computed. On the next access,
# the accessor function from the parent class will be called, simply
# because that's how the python descriptor protocol works.
for mname, mval in classdict.items():
if mname in instdict and isinstance(mval, OneTimeProperty):
delattr(self, mname) | def reset(self):
"""Reset all OneTimeProperty attributes that may have fired already."""
instdict = self.__dict__
classdict = self.__class__.__dict__
# To reset them, we simply remove them from the instance dict. At that
# point, it's as if they had never been computed. On the next access,
# the accessor function from the parent class will be called, simply
# because that's how the python descriptor protocol works.
for mname, mval in classdict.items():
if mname in instdict and isinstance(mval, OneTimeProperty):
delattr(self, mname) | [
"Reset",
"all",
"OneTimeProperty",
"attributes",
"that",
"may",
"have",
"fired",
"already",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/utils/autoattr.py#L90-L100 | [
"def",
"reset",
"(",
"self",
")",
":",
"instdict",
"=",
"self",
".",
"__dict__",
"classdict",
"=",
"self",
".",
"__class__",
".",
"__dict__",
"# To reset them, we simply remove them from the instance dict. At that",
"# point, it's as if they had never been computed. On the next access,",
"# the accessor function from the parent class will be called, simply",
"# because that's how the python descriptor protocol works.",
"for",
"mname",
",",
"mval",
"in",
"classdict",
".",
"items",
"(",
")",
":",
"if",
"mname",
"in",
"instdict",
"and",
"isinstance",
"(",
"mval",
",",
"OneTimeProperty",
")",
":",
"delattr",
"(",
"self",
",",
"mname",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | iseq | Generate integers from start to (and including!) stop,
with increment of inc. Alternative to range/xrange. | environment/share/doc/ipython/examples/parallel/wave2D/wavesolver.py | def iseq(start=0, stop=None, inc=1):
"""
Generate integers from start to (and including!) stop,
with increment of inc. Alternative to range/xrange.
"""
if stop is None: # allow isequence(3) to be 0, 1, 2, 3
# take 1st arg as stop, start as 0, and inc=1
stop = start; start = 0; inc = 1
return arange(start, stop+inc, inc) | def iseq(start=0, stop=None, inc=1):
"""
Generate integers from start to (and including!) stop,
with increment of inc. Alternative to range/xrange.
"""
if stop is None: # allow isequence(3) to be 0, 1, 2, 3
# take 1st arg as stop, start as 0, and inc=1
stop = start; start = 0; inc = 1
return arange(start, stop+inc, inc) | [
"Generate",
"integers",
"from",
"start",
"to",
"(",
"and",
"including!",
")",
"stop",
"with",
"increment",
"of",
"inc",
".",
"Alternative",
"to",
"range",
"/",
"xrange",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/share/doc/ipython/examples/parallel/wave2D/wavesolver.py#L17-L25 | [
"def",
"iseq",
"(",
"start",
"=",
"0",
",",
"stop",
"=",
"None",
",",
"inc",
"=",
"1",
")",
":",
"if",
"stop",
"is",
"None",
":",
"# allow isequence(3) to be 0, 1, 2, 3",
"# take 1st arg as stop, start as 0, and inc=1",
"stop",
"=",
"start",
"start",
"=",
"0",
"inc",
"=",
"1",
"return",
"arange",
"(",
"start",
",",
"stop",
"+",
"inc",
",",
"inc",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | export_html | Export the contents of the ConsoleWidget as HTML.
Parameters:
-----------
html : str,
A utf-8 encoded Python string containing the Qt HTML to export.
filename : str
The file to be saved.
image_tag : callable, optional (default None)
Used to convert images. See ``default_image_tag()`` for information.
inline : bool, optional [default True]
If True, include images as inline PNGs. Otherwise, include them as
links to external PNG files, mimicking web browsers' "Web Page,
Complete" behavior. | environment/lib/python2.7/site-packages/IPython/frontend/qt/rich_text.py | def export_html(html, filename, image_tag = None, inline = True):
""" Export the contents of the ConsoleWidget as HTML.
Parameters:
-----------
html : str,
A utf-8 encoded Python string containing the Qt HTML to export.
filename : str
The file to be saved.
image_tag : callable, optional (default None)
Used to convert images. See ``default_image_tag()`` for information.
inline : bool, optional [default True]
If True, include images as inline PNGs. Otherwise, include them as
links to external PNG files, mimicking web browsers' "Web Page,
Complete" behavior.
"""
if image_tag is None:
image_tag = default_image_tag
else:
image_tag = ensure_utf8(image_tag)
if inline:
path = None
else:
root,ext = os.path.splitext(filename)
path = root + "_files"
if os.path.isfile(path):
raise OSError("%s exists, but is not a directory." % path)
with open(filename, 'w') as f:
html = fix_html(html)
f.write(IMG_RE.sub(lambda x: image_tag(x, path = path, format = "png"),
html)) | def export_html(html, filename, image_tag = None, inline = True):
""" Export the contents of the ConsoleWidget as HTML.
Parameters:
-----------
html : str,
A utf-8 encoded Python string containing the Qt HTML to export.
filename : str
The file to be saved.
image_tag : callable, optional (default None)
Used to convert images. See ``default_image_tag()`` for information.
inline : bool, optional [default True]
If True, include images as inline PNGs. Otherwise, include them as
links to external PNG files, mimicking web browsers' "Web Page,
Complete" behavior.
"""
if image_tag is None:
image_tag = default_image_tag
else:
image_tag = ensure_utf8(image_tag)
if inline:
path = None
else:
root,ext = os.path.splitext(filename)
path = root + "_files"
if os.path.isfile(path):
raise OSError("%s exists, but is not a directory." % path)
with open(filename, 'w') as f:
html = fix_html(html)
f.write(IMG_RE.sub(lambda x: image_tag(x, path = path, format = "png"),
html)) | [
"Export",
"the",
"contents",
"of",
"the",
"ConsoleWidget",
"as",
"HTML",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/frontend/qt/rich_text.py#L125-L160 | [
"def",
"export_html",
"(",
"html",
",",
"filename",
",",
"image_tag",
"=",
"None",
",",
"inline",
"=",
"True",
")",
":",
"if",
"image_tag",
"is",
"None",
":",
"image_tag",
"=",
"default_image_tag",
"else",
":",
"image_tag",
"=",
"ensure_utf8",
"(",
"image_tag",
")",
"if",
"inline",
":",
"path",
"=",
"None",
"else",
":",
"root",
",",
"ext",
"=",
"os",
".",
"path",
".",
"splitext",
"(",
"filename",
")",
"path",
"=",
"root",
"+",
"\"_files\"",
"if",
"os",
".",
"path",
".",
"isfile",
"(",
"path",
")",
":",
"raise",
"OSError",
"(",
"\"%s exists, but is not a directory.\"",
"%",
"path",
")",
"with",
"open",
"(",
"filename",
",",
"'w'",
")",
"as",
"f",
":",
"html",
"=",
"fix_html",
"(",
"html",
")",
"f",
".",
"write",
"(",
"IMG_RE",
".",
"sub",
"(",
"lambda",
"x",
":",
"image_tag",
"(",
"x",
",",
"path",
"=",
"path",
",",
"format",
"=",
"\"png\"",
")",
",",
"html",
")",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | export_xhtml | Export the contents of the ConsoleWidget as XHTML with inline SVGs.
Parameters:
-----------
html : str,
A utf-8 encoded Python string containing the Qt HTML to export.
filename : str
The file to be saved.
image_tag : callable, optional (default None)
Used to convert images. See ``default_image_tag()`` for information. | environment/lib/python2.7/site-packages/IPython/frontend/qt/rich_text.py | def export_xhtml(html, filename, image_tag=None):
""" Export the contents of the ConsoleWidget as XHTML with inline SVGs.
Parameters:
-----------
html : str,
A utf-8 encoded Python string containing the Qt HTML to export.
filename : str
The file to be saved.
image_tag : callable, optional (default None)
Used to convert images. See ``default_image_tag()`` for information.
"""
if image_tag is None:
image_tag = default_image_tag
else:
image_tag = ensure_utf8(image_tag)
with open(filename, 'w') as f:
# Hack to make xhtml header -- note that we are not doing any check for
# valid XML.
offset = html.find("<html>")
assert offset > -1, 'Invalid HTML string: no <html> tag.'
html = ('<html xmlns="http://www.w3.org/1999/xhtml">\n'+
html[offset+6:])
html = fix_html(html)
f.write(IMG_RE.sub(lambda x: image_tag(x, path = None, format = "svg"),
html)) | def export_xhtml(html, filename, image_tag=None):
""" Export the contents of the ConsoleWidget as XHTML with inline SVGs.
Parameters:
-----------
html : str,
A utf-8 encoded Python string containing the Qt HTML to export.
filename : str
The file to be saved.
image_tag : callable, optional (default None)
Used to convert images. See ``default_image_tag()`` for information.
"""
if image_tag is None:
image_tag = default_image_tag
else:
image_tag = ensure_utf8(image_tag)
with open(filename, 'w') as f:
# Hack to make xhtml header -- note that we are not doing any check for
# valid XML.
offset = html.find("<html>")
assert offset > -1, 'Invalid HTML string: no <html> tag.'
html = ('<html xmlns="http://www.w3.org/1999/xhtml">\n'+
html[offset+6:])
html = fix_html(html)
f.write(IMG_RE.sub(lambda x: image_tag(x, path = None, format = "svg"),
html)) | [
"Export",
"the",
"contents",
"of",
"the",
"ConsoleWidget",
"as",
"XHTML",
"with",
"inline",
"SVGs",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/frontend/qt/rich_text.py#L163-L192 | [
"def",
"export_xhtml",
"(",
"html",
",",
"filename",
",",
"image_tag",
"=",
"None",
")",
":",
"if",
"image_tag",
"is",
"None",
":",
"image_tag",
"=",
"default_image_tag",
"else",
":",
"image_tag",
"=",
"ensure_utf8",
"(",
"image_tag",
")",
"with",
"open",
"(",
"filename",
",",
"'w'",
")",
"as",
"f",
":",
"# Hack to make xhtml header -- note that we are not doing any check for",
"# valid XML.",
"offset",
"=",
"html",
".",
"find",
"(",
"\"<html>\"",
")",
"assert",
"offset",
">",
"-",
"1",
",",
"'Invalid HTML string: no <html> tag.'",
"html",
"=",
"(",
"'<html xmlns=\"http://www.w3.org/1999/xhtml\">\\n'",
"+",
"html",
"[",
"offset",
"+",
"6",
":",
"]",
")",
"html",
"=",
"fix_html",
"(",
"html",
")",
"f",
".",
"write",
"(",
"IMG_RE",
".",
"sub",
"(",
"lambda",
"x",
":",
"image_tag",
"(",
"x",
",",
"path",
"=",
"None",
",",
"format",
"=",
"\"svg\"",
")",
",",
"html",
")",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | ensure_utf8 | wrapper for ensuring image_tag returns utf8-encoded str on Python 2 | environment/lib/python2.7/site-packages/IPython/frontend/qt/rich_text.py | def ensure_utf8(image_tag):
"""wrapper for ensuring image_tag returns utf8-encoded str on Python 2"""
if py3compat.PY3:
# nothing to do on Python 3
return image_tag
def utf8_image_tag(*args, **kwargs):
s = image_tag(*args, **kwargs)
if isinstance(s, unicode):
s = s.encode('utf8')
return s
return utf8_image_tag | def ensure_utf8(image_tag):
"""wrapper for ensuring image_tag returns utf8-encoded str on Python 2"""
if py3compat.PY3:
# nothing to do on Python 3
return image_tag
def utf8_image_tag(*args, **kwargs):
s = image_tag(*args, **kwargs)
if isinstance(s, unicode):
s = s.encode('utf8')
return s
return utf8_image_tag | [
"wrapper",
"for",
"ensuring",
"image_tag",
"returns",
"utf8",
"-",
"encoded",
"str",
"on",
"Python",
"2"
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/frontend/qt/rich_text.py#L219-L230 | [
"def",
"ensure_utf8",
"(",
"image_tag",
")",
":",
"if",
"py3compat",
".",
"PY3",
":",
"# nothing to do on Python 3",
"return",
"image_tag",
"def",
"utf8_image_tag",
"(",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"s",
"=",
"image_tag",
"(",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
"if",
"isinstance",
"(",
"s",
",",
"unicode",
")",
":",
"s",
"=",
"s",
".",
"encode",
"(",
"'utf8'",
")",
"return",
"s",
"return",
"utf8_image_tag"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | fix_html | Transforms a Qt-generated HTML string into a standards-compliant one.
Parameters:
-----------
html : str,
A utf-8 encoded Python string containing the Qt HTML. | environment/lib/python2.7/site-packages/IPython/frontend/qt/rich_text.py | def fix_html(html):
""" Transforms a Qt-generated HTML string into a standards-compliant one.
Parameters:
-----------
html : str,
A utf-8 encoded Python string containing the Qt HTML.
"""
# A UTF-8 declaration is needed for proper rendering of some characters
# (e.g., indented commands) when viewing exported HTML on a local system
# (i.e., without seeing an encoding declaration in an HTTP header).
# C.f. http://www.w3.org/International/O-charset for details.
offset = html.find('<head>')
if offset > -1:
html = (html[:offset+6]+
'\n<meta http-equiv="Content-Type" '+
'content="text/html; charset=utf-8" />\n'+
html[offset+6:])
# Replace empty paragraphs tags with line breaks.
html = re.sub(EMPTY_P_RE, '<br/>', html)
return html | def fix_html(html):
""" Transforms a Qt-generated HTML string into a standards-compliant one.
Parameters:
-----------
html : str,
A utf-8 encoded Python string containing the Qt HTML.
"""
# A UTF-8 declaration is needed for proper rendering of some characters
# (e.g., indented commands) when viewing exported HTML on a local system
# (i.e., without seeing an encoding declaration in an HTTP header).
# C.f. http://www.w3.org/International/O-charset for details.
offset = html.find('<head>')
if offset > -1:
html = (html[:offset+6]+
'\n<meta http-equiv="Content-Type" '+
'content="text/html; charset=utf-8" />\n'+
html[offset+6:])
# Replace empty paragraphs tags with line breaks.
html = re.sub(EMPTY_P_RE, '<br/>', html)
return html | [
"Transforms",
"a",
"Qt",
"-",
"generated",
"HTML",
"string",
"into",
"a",
"standards",
"-",
"compliant",
"one",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/frontend/qt/rich_text.py#L233-L255 | [
"def",
"fix_html",
"(",
"html",
")",
":",
"# A UTF-8 declaration is needed for proper rendering of some characters",
"# (e.g., indented commands) when viewing exported HTML on a local system",
"# (i.e., without seeing an encoding declaration in an HTTP header).",
"# C.f. http://www.w3.org/International/O-charset for details.",
"offset",
"=",
"html",
".",
"find",
"(",
"'<head>'",
")",
"if",
"offset",
">",
"-",
"1",
":",
"html",
"=",
"(",
"html",
"[",
":",
"offset",
"+",
"6",
"]",
"+",
"'\\n<meta http-equiv=\"Content-Type\" '",
"+",
"'content=\"text/html; charset=utf-8\" />\\n'",
"+",
"html",
"[",
"offset",
"+",
"6",
":",
"]",
")",
"# Replace empty paragraphs tags with line breaks.",
"html",
"=",
"re",
".",
"sub",
"(",
"EMPTY_P_RE",
",",
"'<br/>'",
",",
"html",
")",
"return",
"html"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | HtmlExporter.export | Displays a dialog for exporting HTML generated by Qt's rich text
system.
Returns
-------
The name of the file that was saved, or None if no file was saved. | environment/lib/python2.7/site-packages/IPython/frontend/qt/rich_text.py | def export(self):
""" Displays a dialog for exporting HTML generated by Qt's rich text
system.
Returns
-------
The name of the file that was saved, or None if no file was saved.
"""
parent = self.control.window()
dialog = QtGui.QFileDialog(parent, 'Save as...')
dialog.setAcceptMode(QtGui.QFileDialog.AcceptSave)
filters = [
'HTML with PNG figures (*.html *.htm)',
'XHTML with inline SVG figures (*.xhtml *.xml)'
]
dialog.setNameFilters(filters)
if self.filename:
dialog.selectFile(self.filename)
root,ext = os.path.splitext(self.filename)
if ext.lower() in ('.xml', '.xhtml'):
dialog.selectNameFilter(filters[-1])
if dialog.exec_():
self.filename = dialog.selectedFiles()[0]
choice = dialog.selectedNameFilter()
html = self.control.document().toHtml().encode('utf-8')
# Configure the exporter.
if choice.startswith('XHTML'):
exporter = export_xhtml
else:
# If there are PNGs, decide how to export them.
inline = self.inline_png
if inline is None and IMG_RE.search(html):
dialog = QtGui.QDialog(parent)
dialog.setWindowTitle('Save as...')
layout = QtGui.QVBoxLayout(dialog)
msg = "Exporting HTML with PNGs"
info = "Would you like inline PNGs (single large html " \
"file) or external image files?"
checkbox = QtGui.QCheckBox("&Don't ask again")
checkbox.setShortcut('D')
ib = QtGui.QPushButton("&Inline")
ib.setShortcut('I')
eb = QtGui.QPushButton("&External")
eb.setShortcut('E')
box = QtGui.QMessageBox(QtGui.QMessageBox.Question,
dialog.windowTitle(), msg)
box.setInformativeText(info)
box.addButton(ib, QtGui.QMessageBox.NoRole)
box.addButton(eb, QtGui.QMessageBox.YesRole)
layout.setSpacing(0)
layout.addWidget(box)
layout.addWidget(checkbox)
dialog.setLayout(layout)
dialog.show()
reply = box.exec_()
dialog.hide()
inline = (reply == 0)
if checkbox.checkState():
# Don't ask anymore; always use this choice.
self.inline_png = inline
exporter = lambda h, f, i: export_html(h, f, i, inline)
# Perform the export!
try:
return exporter(html, self.filename, self.image_tag)
except Exception, e:
msg = "Error exporting HTML to %s\n" % self.filename + str(e)
reply = QtGui.QMessageBox.warning(parent, 'Error', msg,
QtGui.QMessageBox.Ok, QtGui.QMessageBox.Ok)
return None | def export(self):
""" Displays a dialog for exporting HTML generated by Qt's rich text
system.
Returns
-------
The name of the file that was saved, or None if no file was saved.
"""
parent = self.control.window()
dialog = QtGui.QFileDialog(parent, 'Save as...')
dialog.setAcceptMode(QtGui.QFileDialog.AcceptSave)
filters = [
'HTML with PNG figures (*.html *.htm)',
'XHTML with inline SVG figures (*.xhtml *.xml)'
]
dialog.setNameFilters(filters)
if self.filename:
dialog.selectFile(self.filename)
root,ext = os.path.splitext(self.filename)
if ext.lower() in ('.xml', '.xhtml'):
dialog.selectNameFilter(filters[-1])
if dialog.exec_():
self.filename = dialog.selectedFiles()[0]
choice = dialog.selectedNameFilter()
html = self.control.document().toHtml().encode('utf-8')
# Configure the exporter.
if choice.startswith('XHTML'):
exporter = export_xhtml
else:
# If there are PNGs, decide how to export them.
inline = self.inline_png
if inline is None and IMG_RE.search(html):
dialog = QtGui.QDialog(parent)
dialog.setWindowTitle('Save as...')
layout = QtGui.QVBoxLayout(dialog)
msg = "Exporting HTML with PNGs"
info = "Would you like inline PNGs (single large html " \
"file) or external image files?"
checkbox = QtGui.QCheckBox("&Don't ask again")
checkbox.setShortcut('D')
ib = QtGui.QPushButton("&Inline")
ib.setShortcut('I')
eb = QtGui.QPushButton("&External")
eb.setShortcut('E')
box = QtGui.QMessageBox(QtGui.QMessageBox.Question,
dialog.windowTitle(), msg)
box.setInformativeText(info)
box.addButton(ib, QtGui.QMessageBox.NoRole)
box.addButton(eb, QtGui.QMessageBox.YesRole)
layout.setSpacing(0)
layout.addWidget(box)
layout.addWidget(checkbox)
dialog.setLayout(layout)
dialog.show()
reply = box.exec_()
dialog.hide()
inline = (reply == 0)
if checkbox.checkState():
# Don't ask anymore; always use this choice.
self.inline_png = inline
exporter = lambda h, f, i: export_html(h, f, i, inline)
# Perform the export!
try:
return exporter(html, self.filename, self.image_tag)
except Exception, e:
msg = "Error exporting HTML to %s\n" % self.filename + str(e)
reply = QtGui.QMessageBox.warning(parent, 'Error', msg,
QtGui.QMessageBox.Ok, QtGui.QMessageBox.Ok)
return None | [
"Displays",
"a",
"dialog",
"for",
"exporting",
"HTML",
"generated",
"by",
"Qt",
"s",
"rich",
"text",
"system",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/frontend/qt/rich_text.py#L47-L119 | [
"def",
"export",
"(",
"self",
")",
":",
"parent",
"=",
"self",
".",
"control",
".",
"window",
"(",
")",
"dialog",
"=",
"QtGui",
".",
"QFileDialog",
"(",
"parent",
",",
"'Save as...'",
")",
"dialog",
".",
"setAcceptMode",
"(",
"QtGui",
".",
"QFileDialog",
".",
"AcceptSave",
")",
"filters",
"=",
"[",
"'HTML with PNG figures (*.html *.htm)'",
",",
"'XHTML with inline SVG figures (*.xhtml *.xml)'",
"]",
"dialog",
".",
"setNameFilters",
"(",
"filters",
")",
"if",
"self",
".",
"filename",
":",
"dialog",
".",
"selectFile",
"(",
"self",
".",
"filename",
")",
"root",
",",
"ext",
"=",
"os",
".",
"path",
".",
"splitext",
"(",
"self",
".",
"filename",
")",
"if",
"ext",
".",
"lower",
"(",
")",
"in",
"(",
"'.xml'",
",",
"'.xhtml'",
")",
":",
"dialog",
".",
"selectNameFilter",
"(",
"filters",
"[",
"-",
"1",
"]",
")",
"if",
"dialog",
".",
"exec_",
"(",
")",
":",
"self",
".",
"filename",
"=",
"dialog",
".",
"selectedFiles",
"(",
")",
"[",
"0",
"]",
"choice",
"=",
"dialog",
".",
"selectedNameFilter",
"(",
")",
"html",
"=",
"self",
".",
"control",
".",
"document",
"(",
")",
".",
"toHtml",
"(",
")",
".",
"encode",
"(",
"'utf-8'",
")",
"# Configure the exporter.",
"if",
"choice",
".",
"startswith",
"(",
"'XHTML'",
")",
":",
"exporter",
"=",
"export_xhtml",
"else",
":",
"# If there are PNGs, decide how to export them.",
"inline",
"=",
"self",
".",
"inline_png",
"if",
"inline",
"is",
"None",
"and",
"IMG_RE",
".",
"search",
"(",
"html",
")",
":",
"dialog",
"=",
"QtGui",
".",
"QDialog",
"(",
"parent",
")",
"dialog",
".",
"setWindowTitle",
"(",
"'Save as...'",
")",
"layout",
"=",
"QtGui",
".",
"QVBoxLayout",
"(",
"dialog",
")",
"msg",
"=",
"\"Exporting HTML with PNGs\"",
"info",
"=",
"\"Would you like inline PNGs (single large html \"",
"\"file) or external image files?\"",
"checkbox",
"=",
"QtGui",
".",
"QCheckBox",
"(",
"\"&Don't ask again\"",
")",
"checkbox",
".",
"setShortcut",
"(",
"'D'",
")",
"ib",
"=",
"QtGui",
".",
"QPushButton",
"(",
"\"&Inline\"",
")",
"ib",
".",
"setShortcut",
"(",
"'I'",
")",
"eb",
"=",
"QtGui",
".",
"QPushButton",
"(",
"\"&External\"",
")",
"eb",
".",
"setShortcut",
"(",
"'E'",
")",
"box",
"=",
"QtGui",
".",
"QMessageBox",
"(",
"QtGui",
".",
"QMessageBox",
".",
"Question",
",",
"dialog",
".",
"windowTitle",
"(",
")",
",",
"msg",
")",
"box",
".",
"setInformativeText",
"(",
"info",
")",
"box",
".",
"addButton",
"(",
"ib",
",",
"QtGui",
".",
"QMessageBox",
".",
"NoRole",
")",
"box",
".",
"addButton",
"(",
"eb",
",",
"QtGui",
".",
"QMessageBox",
".",
"YesRole",
")",
"layout",
".",
"setSpacing",
"(",
"0",
")",
"layout",
".",
"addWidget",
"(",
"box",
")",
"layout",
".",
"addWidget",
"(",
"checkbox",
")",
"dialog",
".",
"setLayout",
"(",
"layout",
")",
"dialog",
".",
"show",
"(",
")",
"reply",
"=",
"box",
".",
"exec_",
"(",
")",
"dialog",
".",
"hide",
"(",
")",
"inline",
"=",
"(",
"reply",
"==",
"0",
")",
"if",
"checkbox",
".",
"checkState",
"(",
")",
":",
"# Don't ask anymore; always use this choice.",
"self",
".",
"inline_png",
"=",
"inline",
"exporter",
"=",
"lambda",
"h",
",",
"f",
",",
"i",
":",
"export_html",
"(",
"h",
",",
"f",
",",
"i",
",",
"inline",
")",
"# Perform the export!",
"try",
":",
"return",
"exporter",
"(",
"html",
",",
"self",
".",
"filename",
",",
"self",
".",
"image_tag",
")",
"except",
"Exception",
",",
"e",
":",
"msg",
"=",
"\"Error exporting HTML to %s\\n\"",
"%",
"self",
".",
"filename",
"+",
"str",
"(",
"e",
")",
"reply",
"=",
"QtGui",
".",
"QMessageBox",
".",
"warning",
"(",
"parent",
",",
"'Error'",
",",
"msg",
",",
"QtGui",
".",
"QMessageBox",
".",
"Ok",
",",
"QtGui",
".",
"QMessageBox",
".",
"Ok",
")",
"return",
"None"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | get_unique_or_none | Returns a unique instance of `klass` or None | toolware/utils/query.py | def get_unique_or_none(klass, *args, **kwargs):
""" Returns a unique instance of `klass` or None """
try:
return klass.objects.get(*args, **kwargs)
except klass.DoesNotExist:
return None
except klass.MultipleObjectsReturned:
return None
return None | def get_unique_or_none(klass, *args, **kwargs):
""" Returns a unique instance of `klass` or None """
try:
return klass.objects.get(*args, **kwargs)
except klass.DoesNotExist:
return None
except klass.MultipleObjectsReturned:
return None
return None | [
"Returns",
"a",
"unique",
"instance",
"of",
"klass",
"or",
"None"
] | un33k/django-toolware | python | https://github.com/un33k/django-toolware/blob/973f3e003dc38b812897dab88455bee37dcaf931/toolware/utils/query.py#L12-L20 | [
"def",
"get_unique_or_none",
"(",
"klass",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"try",
":",
"return",
"klass",
".",
"objects",
".",
"get",
"(",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
"except",
"klass",
".",
"DoesNotExist",
":",
"return",
"None",
"except",
"klass",
".",
"MultipleObjectsReturned",
":",
"return",
"None",
"return",
"None"
] | 973f3e003dc38b812897dab88455bee37dcaf931 |
test | get_or_create_unique | Returns a tuple of (instance, created), where `instance` is the retrieved
or created instance of `klass` and `created` is a boolean specifying whether
a new object was created. The value for the unique fields must be present in
the defaults dictionary. | toolware/utils/query.py | def get_or_create_unique(klass, defaults, unique_fields):
"""
Returns a tuple of (instance, created), where `instance` is the retrieved
or created instance of `klass` and `created` is a boolean specifying whether
a new object was created. The value for the unique fields must be present in
the defaults dictionary.
"""
if not unique_fields or not defaults:
return (None, False)
uniqueness_query = {k: v for k, v in defaults.items() if k in unique_fields}
try:
with transaction.atomic():
instance, created = klass.objects.get_or_create(defaults=defaults, **uniqueness_query)
except IntegrityError:
try:
instance, created = klass(**defaults).save(), True
except Exception as err:
return (None, False)
except Exception as err:
return (None, False)
if instance and not created:
for attr, value in defaults.items():
if getattr(instance, attr):
setattr(instance, attr, value)
instance.save()
return (instance, created) | def get_or_create_unique(klass, defaults, unique_fields):
"""
Returns a tuple of (instance, created), where `instance` is the retrieved
or created instance of `klass` and `created` is a boolean specifying whether
a new object was created. The value for the unique fields must be present in
the defaults dictionary.
"""
if not unique_fields or not defaults:
return (None, False)
uniqueness_query = {k: v for k, v in defaults.items() if k in unique_fields}
try:
with transaction.atomic():
instance, created = klass.objects.get_or_create(defaults=defaults, **uniqueness_query)
except IntegrityError:
try:
instance, created = klass(**defaults).save(), True
except Exception as err:
return (None, False)
except Exception as err:
return (None, False)
if instance and not created:
for attr, value in defaults.items():
if getattr(instance, attr):
setattr(instance, attr, value)
instance.save()
return (instance, created) | [
"Returns",
"a",
"tuple",
"of",
"(",
"instance",
"created",
")",
"where",
"instance",
"is",
"the",
"retrieved",
"or",
"created",
"instance",
"of",
"klass",
"and",
"created",
"is",
"a",
"boolean",
"specifying",
"whether",
"a",
"new",
"object",
"was",
"created",
".",
"The",
"value",
"for",
"the",
"unique",
"fields",
"must",
"be",
"present",
"in",
"the",
"defaults",
"dictionary",
"."
] | un33k/django-toolware | python | https://github.com/un33k/django-toolware/blob/973f3e003dc38b812897dab88455bee37dcaf931/toolware/utils/query.py#L23-L52 | [
"def",
"get_or_create_unique",
"(",
"klass",
",",
"defaults",
",",
"unique_fields",
")",
":",
"if",
"not",
"unique_fields",
"or",
"not",
"defaults",
":",
"return",
"(",
"None",
",",
"False",
")",
"uniqueness_query",
"=",
"{",
"k",
":",
"v",
"for",
"k",
",",
"v",
"in",
"defaults",
".",
"items",
"(",
")",
"if",
"k",
"in",
"unique_fields",
"}",
"try",
":",
"with",
"transaction",
".",
"atomic",
"(",
")",
":",
"instance",
",",
"created",
"=",
"klass",
".",
"objects",
".",
"get_or_create",
"(",
"defaults",
"=",
"defaults",
",",
"*",
"*",
"uniqueness_query",
")",
"except",
"IntegrityError",
":",
"try",
":",
"instance",
",",
"created",
"=",
"klass",
"(",
"*",
"*",
"defaults",
")",
".",
"save",
"(",
")",
",",
"True",
"except",
"Exception",
"as",
"err",
":",
"return",
"(",
"None",
",",
"False",
")",
"except",
"Exception",
"as",
"err",
":",
"return",
"(",
"None",
",",
"False",
")",
"if",
"instance",
"and",
"not",
"created",
":",
"for",
"attr",
",",
"value",
"in",
"defaults",
".",
"items",
"(",
")",
":",
"if",
"getattr",
"(",
"instance",
",",
"attr",
")",
":",
"setattr",
"(",
"instance",
",",
"attr",
",",
"value",
")",
"instance",
".",
"save",
"(",
")",
"return",
"(",
"instance",
",",
"created",
")"
] | 973f3e003dc38b812897dab88455bee37dcaf931 |
test | get_text_tokenizer | Tokenize the input string and return two lists, exclude list is for words that
start with a dash (ex: -word) and include list is for all other words | toolware/utils/query.py | def get_text_tokenizer(query_string):
"""
Tokenize the input string and return two lists, exclude list is for words that
start with a dash (ex: -word) and include list is for all other words
"""
# Regex to split on double-quotes, single-quotes, and continuous non-whitespace characters.
split_pattern = re.compile('("[^"]+"|\'[^\']+\'|\S+)')
# Pattern to remove more than one inter white-spaces and more than one "-"
space_cleanup_pattern = re.compile('[\s]{2,}')
dash_cleanup_pattern = re.compile('^[-]{2,}')
# Return the list of keywords.
keywords = [dash_cleanup_pattern.sub('-', space_cleanup_pattern.sub(' ', t.strip(' "\'')))
for t in split_pattern.findall(query_string) if len(t.strip(' "\'')) > 0]
include = [word for word in keywords if not word.startswith('-')]
exclude = [word.lstrip('-') for word in keywords if word.startswith('-')]
return include, exclude | def get_text_tokenizer(query_string):
"""
Tokenize the input string and return two lists, exclude list is for words that
start with a dash (ex: -word) and include list is for all other words
"""
# Regex to split on double-quotes, single-quotes, and continuous non-whitespace characters.
split_pattern = re.compile('("[^"]+"|\'[^\']+\'|\S+)')
# Pattern to remove more than one inter white-spaces and more than one "-"
space_cleanup_pattern = re.compile('[\s]{2,}')
dash_cleanup_pattern = re.compile('^[-]{2,}')
# Return the list of keywords.
keywords = [dash_cleanup_pattern.sub('-', space_cleanup_pattern.sub(' ', t.strip(' "\'')))
for t in split_pattern.findall(query_string) if len(t.strip(' "\'')) > 0]
include = [word for word in keywords if not word.startswith('-')]
exclude = [word.lstrip('-') for word in keywords if word.startswith('-')]
return include, exclude | [
"Tokenize",
"the",
"input",
"string",
"and",
"return",
"two",
"lists",
"exclude",
"list",
"is",
"for",
"words",
"that",
"start",
"with",
"a",
"dash",
"(",
"ex",
":",
"-",
"word",
")",
"and",
"include",
"list",
"is",
"for",
"all",
"other",
"words"
] | un33k/django-toolware | python | https://github.com/un33k/django-toolware/blob/973f3e003dc38b812897dab88455bee37dcaf931/toolware/utils/query.py#L119-L136 | [
"def",
"get_text_tokenizer",
"(",
"query_string",
")",
":",
"# Regex to split on double-quotes, single-quotes, and continuous non-whitespace characters.",
"split_pattern",
"=",
"re",
".",
"compile",
"(",
"'(\"[^\"]+\"|\\'[^\\']+\\'|\\S+)'",
")",
"# Pattern to remove more than one inter white-spaces and more than one \"-\"",
"space_cleanup_pattern",
"=",
"re",
".",
"compile",
"(",
"'[\\s]{2,}'",
")",
"dash_cleanup_pattern",
"=",
"re",
".",
"compile",
"(",
"'^[-]{2,}'",
")",
"# Return the list of keywords.",
"keywords",
"=",
"[",
"dash_cleanup_pattern",
".",
"sub",
"(",
"'-'",
",",
"space_cleanup_pattern",
".",
"sub",
"(",
"' '",
",",
"t",
".",
"strip",
"(",
"' \"\\''",
")",
")",
")",
"for",
"t",
"in",
"split_pattern",
".",
"findall",
"(",
"query_string",
")",
"if",
"len",
"(",
"t",
".",
"strip",
"(",
"' \"\\''",
")",
")",
">",
"0",
"]",
"include",
"=",
"[",
"word",
"for",
"word",
"in",
"keywords",
"if",
"not",
"word",
".",
"startswith",
"(",
"'-'",
")",
"]",
"exclude",
"=",
"[",
"word",
".",
"lstrip",
"(",
"'-'",
")",
"for",
"word",
"in",
"keywords",
"if",
"word",
".",
"startswith",
"(",
"'-'",
")",
"]",
"return",
"include",
",",
"exclude"
] | 973f3e003dc38b812897dab88455bee37dcaf931 |
test | get_query_includes | Builds a query for included terms in a text search. | toolware/utils/query.py | def get_query_includes(tokenized_terms, search_fields):
"""
Builds a query for included terms in a text search.
"""
query = None
for term in tokenized_terms:
or_query = None
for field_name in search_fields:
q = Q(**{"%s__icontains" % field_name: term})
if or_query is None:
or_query = q
else:
or_query = or_query | q
if query is None:
query = or_query
else:
query = query & or_query
return query | def get_query_includes(tokenized_terms, search_fields):
"""
Builds a query for included terms in a text search.
"""
query = None
for term in tokenized_terms:
or_query = None
for field_name in search_fields:
q = Q(**{"%s__icontains" % field_name: term})
if or_query is None:
or_query = q
else:
or_query = or_query | q
if query is None:
query = or_query
else:
query = query & or_query
return query | [
"Builds",
"a",
"query",
"for",
"included",
"terms",
"in",
"a",
"text",
"search",
"."
] | un33k/django-toolware | python | https://github.com/un33k/django-toolware/blob/973f3e003dc38b812897dab88455bee37dcaf931/toolware/utils/query.py#L139-L156 | [
"def",
"get_query_includes",
"(",
"tokenized_terms",
",",
"search_fields",
")",
":",
"query",
"=",
"None",
"for",
"term",
"in",
"tokenized_terms",
":",
"or_query",
"=",
"None",
"for",
"field_name",
"in",
"search_fields",
":",
"q",
"=",
"Q",
"(",
"*",
"*",
"{",
"\"%s__icontains\"",
"%",
"field_name",
":",
"term",
"}",
")",
"if",
"or_query",
"is",
"None",
":",
"or_query",
"=",
"q",
"else",
":",
"or_query",
"=",
"or_query",
"|",
"q",
"if",
"query",
"is",
"None",
":",
"query",
"=",
"or_query",
"else",
":",
"query",
"=",
"query",
"&",
"or_query",
"return",
"query"
] | 973f3e003dc38b812897dab88455bee37dcaf931 |
test | get_text_query | Builds a query for both included & excluded terms in a text search. | toolware/utils/query.py | def get_text_query(query_string, search_fields):
"""
Builds a query for both included & excluded terms in a text search.
"""
include_terms, exclude_terms = get_text_tokenizer(query_string)
include_q = get_query_includes(include_terms, search_fields)
exclude_q = get_query_excludes(exclude_terms, search_fields)
query = None
if include_q and exclude_q:
query = include_q & ~exclude_q
elif not exclude_q:
query = include_q
else:
query = ~exclude_q
return query | def get_text_query(query_string, search_fields):
"""
Builds a query for both included & excluded terms in a text search.
"""
include_terms, exclude_terms = get_text_tokenizer(query_string)
include_q = get_query_includes(include_terms, search_fields)
exclude_q = get_query_excludes(exclude_terms, search_fields)
query = None
if include_q and exclude_q:
query = include_q & ~exclude_q
elif not exclude_q:
query = include_q
else:
query = ~exclude_q
return query | [
"Builds",
"a",
"query",
"for",
"both",
"included",
"&",
"excluded",
"terms",
"in",
"a",
"text",
"search",
"."
] | un33k/django-toolware | python | https://github.com/un33k/django-toolware/blob/973f3e003dc38b812897dab88455bee37dcaf931/toolware/utils/query.py#L179-L193 | [
"def",
"get_text_query",
"(",
"query_string",
",",
"search_fields",
")",
":",
"include_terms",
",",
"exclude_terms",
"=",
"get_text_tokenizer",
"(",
"query_string",
")",
"include_q",
"=",
"get_query_includes",
"(",
"include_terms",
",",
"search_fields",
")",
"exclude_q",
"=",
"get_query_excludes",
"(",
"exclude_terms",
",",
"search_fields",
")",
"query",
"=",
"None",
"if",
"include_q",
"and",
"exclude_q",
":",
"query",
"=",
"include_q",
"&",
"~",
"exclude_q",
"elif",
"not",
"exclude_q",
":",
"query",
"=",
"include_q",
"else",
":",
"query",
"=",
"~",
"exclude_q",
"return",
"query"
] | 973f3e003dc38b812897dab88455bee37dcaf931 |
test | get_date_greater_query | Query for if date_field is within number of "days" ago. | toolware/utils/query.py | def get_date_greater_query(days, date_field):
"""
Query for if date_field is within number of "days" ago.
"""
query = None
days = get_integer(days)
if days:
past = get_days_ago(days)
query = Q(**{"%s__gte" % date_field: past.isoformat()})
return query | def get_date_greater_query(days, date_field):
"""
Query for if date_field is within number of "days" ago.
"""
query = None
days = get_integer(days)
if days:
past = get_days_ago(days)
query = Q(**{"%s__gte" % date_field: past.isoformat()})
return query | [
"Query",
"for",
"if",
"date_field",
"is",
"within",
"number",
"of",
"days",
"ago",
"."
] | un33k/django-toolware | python | https://github.com/un33k/django-toolware/blob/973f3e003dc38b812897dab88455bee37dcaf931/toolware/utils/query.py#L196-L205 | [
"def",
"get_date_greater_query",
"(",
"days",
",",
"date_field",
")",
":",
"query",
"=",
"None",
"days",
"=",
"get_integer",
"(",
"days",
")",
"if",
"days",
":",
"past",
"=",
"get_days_ago",
"(",
"days",
")",
"query",
"=",
"Q",
"(",
"*",
"*",
"{",
"\"%s__gte\"",
"%",
"date_field",
":",
"past",
".",
"isoformat",
"(",
")",
"}",
")",
"return",
"query"
] | 973f3e003dc38b812897dab88455bee37dcaf931 |
test | get_date_less_query | Query for if date_field is within number of "days" from now. | toolware/utils/query.py | def get_date_less_query(days, date_field):
"""
Query for if date_field is within number of "days" from now.
"""
query = None
days = get_integer(days)
if days:
future = get_days_from_now(days)
query = Q(**{"%s__lte" % date_field: future.isoformat()})
return query | def get_date_less_query(days, date_field):
"""
Query for if date_field is within number of "days" from now.
"""
query = None
days = get_integer(days)
if days:
future = get_days_from_now(days)
query = Q(**{"%s__lte" % date_field: future.isoformat()})
return query | [
"Query",
"for",
"if",
"date_field",
"is",
"within",
"number",
"of",
"days",
"from",
"now",
"."
] | un33k/django-toolware | python | https://github.com/un33k/django-toolware/blob/973f3e003dc38b812897dab88455bee37dcaf931/toolware/utils/query.py#L208-L217 | [
"def",
"get_date_less_query",
"(",
"days",
",",
"date_field",
")",
":",
"query",
"=",
"None",
"days",
"=",
"get_integer",
"(",
"days",
")",
"if",
"days",
":",
"future",
"=",
"get_days_from_now",
"(",
"days",
")",
"query",
"=",
"Q",
"(",
"*",
"*",
"{",
"\"%s__lte\"",
"%",
"date_field",
":",
"future",
".",
"isoformat",
"(",
")",
"}",
")",
"return",
"query"
] | 973f3e003dc38b812897dab88455bee37dcaf931 |
test | get_null_or_blank_query | Query for null or blank field. | toolware/utils/query.py | def get_null_or_blank_query(field=None):
"""
Query for null or blank field.
"""
if not field:
return field
null_q = get_null_query(field)
blank_q = get_blank_query(field)
return (null_q | blank_q) | def get_null_or_blank_query(field=None):
"""
Query for null or blank field.
"""
if not field:
return field
null_q = get_null_query(field)
blank_q = get_blank_query(field)
return (null_q | blank_q) | [
"Query",
"for",
"null",
"or",
"blank",
"field",
"."
] | un33k/django-toolware | python | https://github.com/un33k/django-toolware/blob/973f3e003dc38b812897dab88455bee37dcaf931/toolware/utils/query.py#L240-L248 | [
"def",
"get_null_or_blank_query",
"(",
"field",
"=",
"None",
")",
":",
"if",
"not",
"field",
":",
"return",
"field",
"null_q",
"=",
"get_null_query",
"(",
"field",
")",
"blank_q",
"=",
"get_blank_query",
"(",
"field",
")",
"return",
"(",
"null_q",
"|",
"blank_q",
")"
] | 973f3e003dc38b812897dab88455bee37dcaf931 |
test | CaseInsensitiveQuerySet.case_insensitive | Converts queries to case insensitive for special fields. | toolware/utils/query.py | def case_insensitive(self, fields_dict):
"""
Converts queries to case insensitive for special fields.
"""
if hasattr(self.model, 'CASE_INSENSITIVE_FIELDS'):
for field in self.model.CASE_INSENSITIVE_FIELDS:
if field in fields_dict:
fields_dict[field + '__iexact'] = fields_dict[field]
del fields_dict[field] | def case_insensitive(self, fields_dict):
"""
Converts queries to case insensitive for special fields.
"""
if hasattr(self.model, 'CASE_INSENSITIVE_FIELDS'):
for field in self.model.CASE_INSENSITIVE_FIELDS:
if field in fields_dict:
fields_dict[field + '__iexact'] = fields_dict[field]
del fields_dict[field] | [
"Converts",
"queries",
"to",
"case",
"insensitive",
"for",
"special",
"fields",
"."
] | un33k/django-toolware | python | https://github.com/un33k/django-toolware/blob/973f3e003dc38b812897dab88455bee37dcaf931/toolware/utils/query.py#L62-L70 | [
"def",
"case_insensitive",
"(",
"self",
",",
"fields_dict",
")",
":",
"if",
"hasattr",
"(",
"self",
".",
"model",
",",
"'CASE_INSENSITIVE_FIELDS'",
")",
":",
"for",
"field",
"in",
"self",
".",
"model",
".",
"CASE_INSENSITIVE_FIELDS",
":",
"if",
"field",
"in",
"fields_dict",
":",
"fields_dict",
"[",
"field",
"+",
"'__iexact'",
"]",
"=",
"fields_dict",
"[",
"field",
"]",
"del",
"fields_dict",
"[",
"field",
"]"
] | 973f3e003dc38b812897dab88455bee37dcaf931 |
test | attr | Decorator that adds attributes to classes or functions
for use with the Attribute (-a) plugin. | environment/lib/python2.7/site-packages/nose/plugins/attrib.py | def attr(*args, **kwargs):
"""Decorator that adds attributes to classes or functions
for use with the Attribute (-a) plugin.
"""
def wrap_ob(ob):
for name in args:
setattr(ob, name, True)
for name, value in kwargs.iteritems():
setattr(ob, name, value)
return ob
return wrap_ob | def attr(*args, **kwargs):
"""Decorator that adds attributes to classes or functions
for use with the Attribute (-a) plugin.
"""
def wrap_ob(ob):
for name in args:
setattr(ob, name, True)
for name, value in kwargs.iteritems():
setattr(ob, name, value)
return ob
return wrap_ob | [
"Decorator",
"that",
"adds",
"attributes",
"to",
"classes",
"or",
"functions",
"for",
"use",
"with",
"the",
"Attribute",
"(",
"-",
"a",
")",
"plugin",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/nose/plugins/attrib.py#L114-L124 | [
"def",
"attr",
"(",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"def",
"wrap_ob",
"(",
"ob",
")",
":",
"for",
"name",
"in",
"args",
":",
"setattr",
"(",
"ob",
",",
"name",
",",
"True",
")",
"for",
"name",
",",
"value",
"in",
"kwargs",
".",
"iteritems",
"(",
")",
":",
"setattr",
"(",
"ob",
",",
"name",
",",
"value",
")",
"return",
"ob",
"return",
"wrap_ob"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | get_method_attr | Look up an attribute on a method/ function.
If the attribute isn't found there, looking it up in the
method's class, if any. | environment/lib/python2.7/site-packages/nose/plugins/attrib.py | def get_method_attr(method, cls, attr_name, default = False):
"""Look up an attribute on a method/ function.
If the attribute isn't found there, looking it up in the
method's class, if any.
"""
Missing = object()
value = getattr(method, attr_name, Missing)
if value is Missing and cls is not None:
value = getattr(cls, attr_name, Missing)
if value is Missing:
return default
return value | def get_method_attr(method, cls, attr_name, default = False):
"""Look up an attribute on a method/ function.
If the attribute isn't found there, looking it up in the
method's class, if any.
"""
Missing = object()
value = getattr(method, attr_name, Missing)
if value is Missing and cls is not None:
value = getattr(cls, attr_name, Missing)
if value is Missing:
return default
return value | [
"Look",
"up",
"an",
"attribute",
"on",
"a",
"method",
"/",
"function",
".",
"If",
"the",
"attribute",
"isn",
"t",
"found",
"there",
"looking",
"it",
"up",
"in",
"the",
"method",
"s",
"class",
"if",
"any",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/nose/plugins/attrib.py#L126-L137 | [
"def",
"get_method_attr",
"(",
"method",
",",
"cls",
",",
"attr_name",
",",
"default",
"=",
"False",
")",
":",
"Missing",
"=",
"object",
"(",
")",
"value",
"=",
"getattr",
"(",
"method",
",",
"attr_name",
",",
"Missing",
")",
"if",
"value",
"is",
"Missing",
"and",
"cls",
"is",
"not",
"None",
":",
"value",
"=",
"getattr",
"(",
"cls",
",",
"attr_name",
",",
"Missing",
")",
"if",
"value",
"is",
"Missing",
":",
"return",
"default",
"return",
"value"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | AttributeSelector.options | Register command line options | environment/lib/python2.7/site-packages/nose/plugins/attrib.py | def options(self, parser, env):
"""Register command line options"""
parser.add_option("-a", "--attr",
dest="attr", action="append",
default=env.get('NOSE_ATTR'),
metavar="ATTR",
help="Run only tests that have attributes "
"specified by ATTR [NOSE_ATTR]")
# disable in < 2.4: eval can't take needed args
if compat_24:
parser.add_option("-A", "--eval-attr",
dest="eval_attr", metavar="EXPR", action="append",
default=env.get('NOSE_EVAL_ATTR'),
help="Run only tests for whose attributes "
"the Python expression EXPR evaluates "
"to True [NOSE_EVAL_ATTR]") | def options(self, parser, env):
"""Register command line options"""
parser.add_option("-a", "--attr",
dest="attr", action="append",
default=env.get('NOSE_ATTR'),
metavar="ATTR",
help="Run only tests that have attributes "
"specified by ATTR [NOSE_ATTR]")
# disable in < 2.4: eval can't take needed args
if compat_24:
parser.add_option("-A", "--eval-attr",
dest="eval_attr", metavar="EXPR", action="append",
default=env.get('NOSE_EVAL_ATTR'),
help="Run only tests for whose attributes "
"the Python expression EXPR evaluates "
"to True [NOSE_EVAL_ATTR]") | [
"Register",
"command",
"line",
"options"
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/nose/plugins/attrib.py#L160-L175 | [
"def",
"options",
"(",
"self",
",",
"parser",
",",
"env",
")",
":",
"parser",
".",
"add_option",
"(",
"\"-a\"",
",",
"\"--attr\"",
",",
"dest",
"=",
"\"attr\"",
",",
"action",
"=",
"\"append\"",
",",
"default",
"=",
"env",
".",
"get",
"(",
"'NOSE_ATTR'",
")",
",",
"metavar",
"=",
"\"ATTR\"",
",",
"help",
"=",
"\"Run only tests that have attributes \"",
"\"specified by ATTR [NOSE_ATTR]\"",
")",
"# disable in < 2.4: eval can't take needed args",
"if",
"compat_24",
":",
"parser",
".",
"add_option",
"(",
"\"-A\"",
",",
"\"--eval-attr\"",
",",
"dest",
"=",
"\"eval_attr\"",
",",
"metavar",
"=",
"\"EXPR\"",
",",
"action",
"=",
"\"append\"",
",",
"default",
"=",
"env",
".",
"get",
"(",
"'NOSE_EVAL_ATTR'",
")",
",",
"help",
"=",
"\"Run only tests for whose attributes \"",
"\"the Python expression EXPR evaluates \"",
"\"to True [NOSE_EVAL_ATTR]\"",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | AttributeSelector.configure | Configure the plugin and system, based on selected options.
attr and eval_attr may each be lists.
self.attribs will be a list of lists of tuples. In that list, each
list is a group of attributes, all of which must match for the rule to
match. | environment/lib/python2.7/site-packages/nose/plugins/attrib.py | def configure(self, options, config):
"""Configure the plugin and system, based on selected options.
attr and eval_attr may each be lists.
self.attribs will be a list of lists of tuples. In that list, each
list is a group of attributes, all of which must match for the rule to
match.
"""
self.attribs = []
# handle python eval-expression parameter
if compat_24 and options.eval_attr:
eval_attr = tolist(options.eval_attr)
for attr in eval_attr:
# "<python expression>"
# -> eval(expr) in attribute context must be True
def eval_in_context(expr, obj, cls):
return eval(expr, None, ContextHelper(obj, cls))
self.attribs.append([(attr, eval_in_context)])
# attribute requirements are a comma separated list of
# 'key=value' pairs
if options.attr:
std_attr = tolist(options.attr)
for attr in std_attr:
# all attributes within an attribute group must match
attr_group = []
for attrib in attr.strip().split(","):
# don't die on trailing comma
if not attrib:
continue
items = attrib.split("=", 1)
if len(items) > 1:
# "name=value"
# -> 'str(obj.name) == value' must be True
key, value = items
else:
key = items[0]
if key[0] == "!":
# "!name"
# 'bool(obj.name)' must be False
key = key[1:]
value = False
else:
# "name"
# -> 'bool(obj.name)' must be True
value = True
attr_group.append((key, value))
self.attribs.append(attr_group)
if self.attribs:
self.enabled = True | def configure(self, options, config):
"""Configure the plugin and system, based on selected options.
attr and eval_attr may each be lists.
self.attribs will be a list of lists of tuples. In that list, each
list is a group of attributes, all of which must match for the rule to
match.
"""
self.attribs = []
# handle python eval-expression parameter
if compat_24 and options.eval_attr:
eval_attr = tolist(options.eval_attr)
for attr in eval_attr:
# "<python expression>"
# -> eval(expr) in attribute context must be True
def eval_in_context(expr, obj, cls):
return eval(expr, None, ContextHelper(obj, cls))
self.attribs.append([(attr, eval_in_context)])
# attribute requirements are a comma separated list of
# 'key=value' pairs
if options.attr:
std_attr = tolist(options.attr)
for attr in std_attr:
# all attributes within an attribute group must match
attr_group = []
for attrib in attr.strip().split(","):
# don't die on trailing comma
if not attrib:
continue
items = attrib.split("=", 1)
if len(items) > 1:
# "name=value"
# -> 'str(obj.name) == value' must be True
key, value = items
else:
key = items[0]
if key[0] == "!":
# "!name"
# 'bool(obj.name)' must be False
key = key[1:]
value = False
else:
# "name"
# -> 'bool(obj.name)' must be True
value = True
attr_group.append((key, value))
self.attribs.append(attr_group)
if self.attribs:
self.enabled = True | [
"Configure",
"the",
"plugin",
"and",
"system",
"based",
"on",
"selected",
"options",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/nose/plugins/attrib.py#L177-L228 | [
"def",
"configure",
"(",
"self",
",",
"options",
",",
"config",
")",
":",
"self",
".",
"attribs",
"=",
"[",
"]",
"# handle python eval-expression parameter",
"if",
"compat_24",
"and",
"options",
".",
"eval_attr",
":",
"eval_attr",
"=",
"tolist",
"(",
"options",
".",
"eval_attr",
")",
"for",
"attr",
"in",
"eval_attr",
":",
"# \"<python expression>\"",
"# -> eval(expr) in attribute context must be True",
"def",
"eval_in_context",
"(",
"expr",
",",
"obj",
",",
"cls",
")",
":",
"return",
"eval",
"(",
"expr",
",",
"None",
",",
"ContextHelper",
"(",
"obj",
",",
"cls",
")",
")",
"self",
".",
"attribs",
".",
"append",
"(",
"[",
"(",
"attr",
",",
"eval_in_context",
")",
"]",
")",
"# attribute requirements are a comma separated list of",
"# 'key=value' pairs",
"if",
"options",
".",
"attr",
":",
"std_attr",
"=",
"tolist",
"(",
"options",
".",
"attr",
")",
"for",
"attr",
"in",
"std_attr",
":",
"# all attributes within an attribute group must match",
"attr_group",
"=",
"[",
"]",
"for",
"attrib",
"in",
"attr",
".",
"strip",
"(",
")",
".",
"split",
"(",
"\",\"",
")",
":",
"# don't die on trailing comma",
"if",
"not",
"attrib",
":",
"continue",
"items",
"=",
"attrib",
".",
"split",
"(",
"\"=\"",
",",
"1",
")",
"if",
"len",
"(",
"items",
")",
">",
"1",
":",
"# \"name=value\"",
"# -> 'str(obj.name) == value' must be True",
"key",
",",
"value",
"=",
"items",
"else",
":",
"key",
"=",
"items",
"[",
"0",
"]",
"if",
"key",
"[",
"0",
"]",
"==",
"\"!\"",
":",
"# \"!name\"",
"# 'bool(obj.name)' must be False",
"key",
"=",
"key",
"[",
"1",
":",
"]",
"value",
"=",
"False",
"else",
":",
"# \"name\"",
"# -> 'bool(obj.name)' must be True",
"value",
"=",
"True",
"attr_group",
".",
"append",
"(",
"(",
"key",
",",
"value",
")",
")",
"self",
".",
"attribs",
".",
"append",
"(",
"attr_group",
")",
"if",
"self",
".",
"attribs",
":",
"self",
".",
"enabled",
"=",
"True"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | AttributeSelector.validateAttrib | Verify whether a method has the required attributes
The method is considered a match if it matches all attributes
for any attribute group.
. | environment/lib/python2.7/site-packages/nose/plugins/attrib.py | def validateAttrib(self, method, cls = None):
"""Verify whether a method has the required attributes
The method is considered a match if it matches all attributes
for any attribute group.
."""
# TODO: is there a need for case-sensitive value comparison?
any = False
for group in self.attribs:
match = True
for key, value in group:
attr = get_method_attr(method, cls, key)
if callable(value):
if not value(key, method, cls):
match = False
break
elif value is True:
# value must exist and be True
if not bool(attr):
match = False
break
elif value is False:
# value must not exist or be False
if bool(attr):
match = False
break
elif type(attr) in (list, tuple):
# value must be found in the list attribute
if not str(value).lower() in [str(x).lower()
for x in attr]:
match = False
break
else:
# value must match, convert to string and compare
if (value != attr
and str(value).lower() != str(attr).lower()):
match = False
break
any = any or match
if any:
# not True because we don't want to FORCE the selection of the
# item, only say that it is acceptable
return None
return False | def validateAttrib(self, method, cls = None):
"""Verify whether a method has the required attributes
The method is considered a match if it matches all attributes
for any attribute group.
."""
# TODO: is there a need for case-sensitive value comparison?
any = False
for group in self.attribs:
match = True
for key, value in group:
attr = get_method_attr(method, cls, key)
if callable(value):
if not value(key, method, cls):
match = False
break
elif value is True:
# value must exist and be True
if not bool(attr):
match = False
break
elif value is False:
# value must not exist or be False
if bool(attr):
match = False
break
elif type(attr) in (list, tuple):
# value must be found in the list attribute
if not str(value).lower() in [str(x).lower()
for x in attr]:
match = False
break
else:
# value must match, convert to string and compare
if (value != attr
and str(value).lower() != str(attr).lower()):
match = False
break
any = any or match
if any:
# not True because we don't want to FORCE the selection of the
# item, only say that it is acceptable
return None
return False | [
"Verify",
"whether",
"a",
"method",
"has",
"the",
"required",
"attributes",
"The",
"method",
"is",
"considered",
"a",
"match",
"if",
"it",
"matches",
"all",
"attributes",
"for",
"any",
"attribute",
"group",
".",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/nose/plugins/attrib.py#L230-L272 | [
"def",
"validateAttrib",
"(",
"self",
",",
"method",
",",
"cls",
"=",
"None",
")",
":",
"# TODO: is there a need for case-sensitive value comparison?",
"any",
"=",
"False",
"for",
"group",
"in",
"self",
".",
"attribs",
":",
"match",
"=",
"True",
"for",
"key",
",",
"value",
"in",
"group",
":",
"attr",
"=",
"get_method_attr",
"(",
"method",
",",
"cls",
",",
"key",
")",
"if",
"callable",
"(",
"value",
")",
":",
"if",
"not",
"value",
"(",
"key",
",",
"method",
",",
"cls",
")",
":",
"match",
"=",
"False",
"break",
"elif",
"value",
"is",
"True",
":",
"# value must exist and be True",
"if",
"not",
"bool",
"(",
"attr",
")",
":",
"match",
"=",
"False",
"break",
"elif",
"value",
"is",
"False",
":",
"# value must not exist or be False",
"if",
"bool",
"(",
"attr",
")",
":",
"match",
"=",
"False",
"break",
"elif",
"type",
"(",
"attr",
")",
"in",
"(",
"list",
",",
"tuple",
")",
":",
"# value must be found in the list attribute",
"if",
"not",
"str",
"(",
"value",
")",
".",
"lower",
"(",
")",
"in",
"[",
"str",
"(",
"x",
")",
".",
"lower",
"(",
")",
"for",
"x",
"in",
"attr",
"]",
":",
"match",
"=",
"False",
"break",
"else",
":",
"# value must match, convert to string and compare",
"if",
"(",
"value",
"!=",
"attr",
"and",
"str",
"(",
"value",
")",
".",
"lower",
"(",
")",
"!=",
"str",
"(",
"attr",
")",
".",
"lower",
"(",
")",
")",
":",
"match",
"=",
"False",
"break",
"any",
"=",
"any",
"or",
"match",
"if",
"any",
":",
"# not True because we don't want to FORCE the selection of the",
"# item, only say that it is acceptable",
"return",
"None",
"return",
"False"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | AttributeSelector.wantMethod | Accept the method if its attributes match. | environment/lib/python2.7/site-packages/nose/plugins/attrib.py | def wantMethod(self, method):
"""Accept the method if its attributes match.
"""
try:
cls = method.im_class
except AttributeError:
return False
return self.validateAttrib(method, cls) | def wantMethod(self, method):
"""Accept the method if its attributes match.
"""
try:
cls = method.im_class
except AttributeError:
return False
return self.validateAttrib(method, cls) | [
"Accept",
"the",
"method",
"if",
"its",
"attributes",
"match",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/nose/plugins/attrib.py#L279-L286 | [
"def",
"wantMethod",
"(",
"self",
",",
"method",
")",
":",
"try",
":",
"cls",
"=",
"method",
".",
"im_class",
"except",
"AttributeError",
":",
"return",
"False",
"return",
"self",
".",
"validateAttrib",
"(",
"method",
",",
"cls",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | QtKillRing.rotate | Rotate the kill ring, then yank back the new top. | environment/lib/python2.7/site-packages/IPython/frontend/qt/console/kill_ring.py | def rotate(self):
""" Rotate the kill ring, then yank back the new top.
"""
if self._prev_yank:
text = self._ring.rotate()
if text:
self._skip_cursor = True
cursor = self._text_edit.textCursor()
cursor.movePosition(QtGui.QTextCursor.Left,
QtGui.QTextCursor.KeepAnchor,
n = len(self._prev_yank))
cursor.insertText(text)
self._prev_yank = text | def rotate(self):
""" Rotate the kill ring, then yank back the new top.
"""
if self._prev_yank:
text = self._ring.rotate()
if text:
self._skip_cursor = True
cursor = self._text_edit.textCursor()
cursor.movePosition(QtGui.QTextCursor.Left,
QtGui.QTextCursor.KeepAnchor,
n = len(self._prev_yank))
cursor.insertText(text)
self._prev_yank = text | [
"Rotate",
"the",
"kill",
"ring",
"then",
"yank",
"back",
"the",
"new",
"top",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/frontend/qt/console/kill_ring.py#L104-L116 | [
"def",
"rotate",
"(",
"self",
")",
":",
"if",
"self",
".",
"_prev_yank",
":",
"text",
"=",
"self",
".",
"_ring",
".",
"rotate",
"(",
")",
"if",
"text",
":",
"self",
".",
"_skip_cursor",
"=",
"True",
"cursor",
"=",
"self",
".",
"_text_edit",
".",
"textCursor",
"(",
")",
"cursor",
".",
"movePosition",
"(",
"QtGui",
".",
"QTextCursor",
".",
"Left",
",",
"QtGui",
".",
"QTextCursor",
".",
"KeepAnchor",
",",
"n",
"=",
"len",
"(",
"self",
".",
"_prev_yank",
")",
")",
"cursor",
".",
"insertText",
"(",
"text",
")",
"self",
".",
"_prev_yank",
"=",
"text"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | patch_pyzmq | backport a few patches from newer pyzmq
These can be removed as we bump our minimum pyzmq version | environment/lib/python2.7/site-packages/IPython/zmq/__init__.py | def patch_pyzmq():
"""backport a few patches from newer pyzmq
These can be removed as we bump our minimum pyzmq version
"""
import zmq
# ioloop.install, introduced in pyzmq 2.1.7
from zmq.eventloop import ioloop
def install():
import tornado.ioloop
tornado.ioloop.IOLoop = ioloop.IOLoop
if not hasattr(ioloop, 'install'):
ioloop.install = install
# fix missing DEALER/ROUTER aliases in pyzmq < 2.1.9
if not hasattr(zmq, 'DEALER'):
zmq.DEALER = zmq.XREQ
if not hasattr(zmq, 'ROUTER'):
zmq.ROUTER = zmq.XREP
# fallback on stdlib json if jsonlib is selected, because jsonlib breaks things.
# jsonlib support is removed from pyzmq >= 2.2.0
from zmq.utils import jsonapi
if jsonapi.jsonmod.__name__ == 'jsonlib':
import json
jsonapi.jsonmod = json | def patch_pyzmq():
"""backport a few patches from newer pyzmq
These can be removed as we bump our minimum pyzmq version
"""
import zmq
# ioloop.install, introduced in pyzmq 2.1.7
from zmq.eventloop import ioloop
def install():
import tornado.ioloop
tornado.ioloop.IOLoop = ioloop.IOLoop
if not hasattr(ioloop, 'install'):
ioloop.install = install
# fix missing DEALER/ROUTER aliases in pyzmq < 2.1.9
if not hasattr(zmq, 'DEALER'):
zmq.DEALER = zmq.XREQ
if not hasattr(zmq, 'ROUTER'):
zmq.ROUTER = zmq.XREP
# fallback on stdlib json if jsonlib is selected, because jsonlib breaks things.
# jsonlib support is removed from pyzmq >= 2.2.0
from zmq.utils import jsonapi
if jsonapi.jsonmod.__name__ == 'jsonlib':
import json
jsonapi.jsonmod = json | [
"backport",
"a",
"few",
"patches",
"from",
"newer",
"pyzmq",
"These",
"can",
"be",
"removed",
"as",
"we",
"bump",
"our",
"minimum",
"pyzmq",
"version"
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/zmq/__init__.py#L16-L46 | [
"def",
"patch_pyzmq",
"(",
")",
":",
"import",
"zmq",
"# ioloop.install, introduced in pyzmq 2.1.7",
"from",
"zmq",
".",
"eventloop",
"import",
"ioloop",
"def",
"install",
"(",
")",
":",
"import",
"tornado",
".",
"ioloop",
"tornado",
".",
"ioloop",
".",
"IOLoop",
"=",
"ioloop",
".",
"IOLoop",
"if",
"not",
"hasattr",
"(",
"ioloop",
",",
"'install'",
")",
":",
"ioloop",
".",
"install",
"=",
"install",
"# fix missing DEALER/ROUTER aliases in pyzmq < 2.1.9",
"if",
"not",
"hasattr",
"(",
"zmq",
",",
"'DEALER'",
")",
":",
"zmq",
".",
"DEALER",
"=",
"zmq",
".",
"XREQ",
"if",
"not",
"hasattr",
"(",
"zmq",
",",
"'ROUTER'",
")",
":",
"zmq",
".",
"ROUTER",
"=",
"zmq",
".",
"XREP",
"# fallback on stdlib json if jsonlib is selected, because jsonlib breaks things.",
"# jsonlib support is removed from pyzmq >= 2.2.0",
"from",
"zmq",
".",
"utils",
"import",
"jsonapi",
"if",
"jsonapi",
".",
"jsonmod",
".",
"__name__",
"==",
"'jsonlib'",
":",
"import",
"json",
"jsonapi",
".",
"jsonmod",
"=",
"json"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | version_from_schema | returns: API version number <str>
raises: <VersionNotFound>
NOTE:
relies on presence of comment tags in the XSD, which are currently present
for both ebaySvc.xsd (TradingAPI) and ShoppingService.xsd (ShoppingAPI) | dirtyebay/utils.py | def version_from_schema(schema_el):
"""
returns: API version number <str>
raises: <VersionNotFound>
NOTE:
relies on presence of comment tags in the XSD, which are currently present
for both ebaySvc.xsd (TradingAPI) and ShoppingService.xsd (ShoppingAPI)
"""
vc_el = schema_el
while True:
vc_el = vc_el.getprevious()
if vc_el is None:
break
if vc_el.tag is etree.Comment:
match = VERSION_COMMENT.search(vc_el.text)
if match:
try:
return match.group(1)
except IndexError:
pass
raise VersionNotFound('Version comment not found preceeding schema node') | def version_from_schema(schema_el):
"""
returns: API version number <str>
raises: <VersionNotFound>
NOTE:
relies on presence of comment tags in the XSD, which are currently present
for both ebaySvc.xsd (TradingAPI) and ShoppingService.xsd (ShoppingAPI)
"""
vc_el = schema_el
while True:
vc_el = vc_el.getprevious()
if vc_el is None:
break
if vc_el.tag is etree.Comment:
match = VERSION_COMMENT.search(vc_el.text)
if match:
try:
return match.group(1)
except IndexError:
pass
raise VersionNotFound('Version comment not found preceeding schema node') | [
"returns",
":",
"API",
"version",
"number",
"<str",
">",
"raises",
":",
"<VersionNotFound",
">"
] | anentropic/dirtyebay | python | https://github.com/anentropic/dirtyebay/blob/c3194932360445563cfe0c7e034e41f09358c7eb/dirtyebay/utils.py#L36-L57 | [
"def",
"version_from_schema",
"(",
"schema_el",
")",
":",
"vc_el",
"=",
"schema_el",
"while",
"True",
":",
"vc_el",
"=",
"vc_el",
".",
"getprevious",
"(",
")",
"if",
"vc_el",
"is",
"None",
":",
"break",
"if",
"vc_el",
".",
"tag",
"is",
"etree",
".",
"Comment",
":",
"match",
"=",
"VERSION_COMMENT",
".",
"search",
"(",
"vc_el",
".",
"text",
")",
"if",
"match",
":",
"try",
":",
"return",
"match",
".",
"group",
"(",
"1",
")",
"except",
"IndexError",
":",
"pass",
"raise",
"VersionNotFound",
"(",
"'Version comment not found preceeding schema node'",
")"
] | c3194932360445563cfe0c7e034e41f09358c7eb |
test | _default_ns_prefix | XML doc may have several prefix:namespace_url pairs, can also specify
a namespace_url as default, tags in that namespace don't need a prefix
NOTE:
we rely on default namespace also present in prefixed form, I'm not sure if
this is an XML certainty or a quirk of the eBay WSDLs
in our case the WSDL contains:
<wsdl:documentation>
<Version>1.0.0</Version>
</wsdl:documentation>
...but our query needs to give a prefix to the path of `Version` so we need
to determine the default namespace of the doc, find the matching prefix and
return it | dirtyebay/utils.py | def _default_ns_prefix(nsmap):
"""
XML doc may have several prefix:namespace_url pairs, can also specify
a namespace_url as default, tags in that namespace don't need a prefix
NOTE:
we rely on default namespace also present in prefixed form, I'm not sure if
this is an XML certainty or a quirk of the eBay WSDLs
in our case the WSDL contains:
<wsdl:documentation>
<Version>1.0.0</Version>
</wsdl:documentation>
...but our query needs to give a prefix to the path of `Version` so we need
to determine the default namespace of the doc, find the matching prefix and
return it
"""
if None in nsmap:
default_url = nsmap[None]
prefix = None
for key, val in nsmap.iteritems():
if val == default_url and key is not None:
prefix = key
break
else:
raise ValueError(
"Default namespace {url} not found as a prefix".format(
url=default_url
)
)
return prefix
raise ValueError("No default namespace found in map") | def _default_ns_prefix(nsmap):
"""
XML doc may have several prefix:namespace_url pairs, can also specify
a namespace_url as default, tags in that namespace don't need a prefix
NOTE:
we rely on default namespace also present in prefixed form, I'm not sure if
this is an XML certainty or a quirk of the eBay WSDLs
in our case the WSDL contains:
<wsdl:documentation>
<Version>1.0.0</Version>
</wsdl:documentation>
...but our query needs to give a prefix to the path of `Version` so we need
to determine the default namespace of the doc, find the matching prefix and
return it
"""
if None in nsmap:
default_url = nsmap[None]
prefix = None
for key, val in nsmap.iteritems():
if val == default_url and key is not None:
prefix = key
break
else:
raise ValueError(
"Default namespace {url} not found as a prefix".format(
url=default_url
)
)
return prefix
raise ValueError("No default namespace found in map") | [
"XML",
"doc",
"may",
"have",
"several",
"prefix",
":",
"namespace_url",
"pairs",
"can",
"also",
"specify",
"a",
"namespace_url",
"as",
"default",
"tags",
"in",
"that",
"namespace",
"don",
"t",
"need",
"a",
"prefix",
"NOTE",
":",
"we",
"rely",
"on",
"default",
"namespace",
"also",
"present",
"in",
"prefixed",
"form",
"I",
"m",
"not",
"sure",
"if",
"this",
"is",
"an",
"XML",
"certainty",
"or",
"a",
"quirk",
"of",
"the",
"eBay",
"WSDLs"
] | anentropic/dirtyebay | python | https://github.com/anentropic/dirtyebay/blob/c3194932360445563cfe0c7e034e41f09358c7eb/dirtyebay/utils.py#L60-L90 | [
"def",
"_default_ns_prefix",
"(",
"nsmap",
")",
":",
"if",
"None",
"in",
"nsmap",
":",
"default_url",
"=",
"nsmap",
"[",
"None",
"]",
"prefix",
"=",
"None",
"for",
"key",
",",
"val",
"in",
"nsmap",
".",
"iteritems",
"(",
")",
":",
"if",
"val",
"==",
"default_url",
"and",
"key",
"is",
"not",
"None",
":",
"prefix",
"=",
"key",
"break",
"else",
":",
"raise",
"ValueError",
"(",
"\"Default namespace {url} not found as a prefix\"",
".",
"format",
"(",
"url",
"=",
"default_url",
")",
")",
"return",
"prefix",
"raise",
"ValueError",
"(",
"\"No default namespace found in map\"",
")"
] | c3194932360445563cfe0c7e034e41f09358c7eb |
test | version_from_wsdl | returns: API version number <str>
raises: <VersionNotFound>
NOTE:
relies on presence of documentation node in the WSDLs:
<wsdl:documentation>
<Version>1.0.0</Version>
</wsdl:documentation> | dirtyebay/utils.py | def version_from_wsdl(wsdl_tree):
"""
returns: API version number <str>
raises: <VersionNotFound>
NOTE:
relies on presence of documentation node in the WSDLs:
<wsdl:documentation>
<Version>1.0.0</Version>
</wsdl:documentation>
"""
prefix = _default_ns_prefix(wsdl_tree.nsmap)
# XPath doesn't allow empty prefix:
safe_map = wsdl_tree.nsmap.copy()
try:
del safe_map[None]
except KeyError:
pass
try:
# various eBay WSDLs are inconsistent - need case-insensitive matching
version_el = wsdl_tree.xpath(
'wsdl:service/wsdl:documentation/'
'*[self::{p}:version or self::{p}:Version]'.format(p=prefix),
namespaces=safe_map
)[0]
except IndexError:
raise VersionNotFound(
'Version not found in WSDL service documentation'
)
else:
return version_el.text | def version_from_wsdl(wsdl_tree):
"""
returns: API version number <str>
raises: <VersionNotFound>
NOTE:
relies on presence of documentation node in the WSDLs:
<wsdl:documentation>
<Version>1.0.0</Version>
</wsdl:documentation>
"""
prefix = _default_ns_prefix(wsdl_tree.nsmap)
# XPath doesn't allow empty prefix:
safe_map = wsdl_tree.nsmap.copy()
try:
del safe_map[None]
except KeyError:
pass
try:
# various eBay WSDLs are inconsistent - need case-insensitive matching
version_el = wsdl_tree.xpath(
'wsdl:service/wsdl:documentation/'
'*[self::{p}:version or self::{p}:Version]'.format(p=prefix),
namespaces=safe_map
)[0]
except IndexError:
raise VersionNotFound(
'Version not found in WSDL service documentation'
)
else:
return version_el.text | [
"returns",
":",
"API",
"version",
"number",
"<str",
">",
"raises",
":",
"<VersionNotFound",
">"
] | anentropic/dirtyebay | python | https://github.com/anentropic/dirtyebay/blob/c3194932360445563cfe0c7e034e41f09358c7eb/dirtyebay/utils.py#L93-L125 | [
"def",
"version_from_wsdl",
"(",
"wsdl_tree",
")",
":",
"prefix",
"=",
"_default_ns_prefix",
"(",
"wsdl_tree",
".",
"nsmap",
")",
"# XPath doesn't allow empty prefix:",
"safe_map",
"=",
"wsdl_tree",
".",
"nsmap",
".",
"copy",
"(",
")",
"try",
":",
"del",
"safe_map",
"[",
"None",
"]",
"except",
"KeyError",
":",
"pass",
"try",
":",
"# various eBay WSDLs are inconsistent - need case-insensitive matching",
"version_el",
"=",
"wsdl_tree",
".",
"xpath",
"(",
"'wsdl:service/wsdl:documentation/'",
"'*[self::{p}:version or self::{p}:Version]'",
".",
"format",
"(",
"p",
"=",
"prefix",
")",
",",
"namespaces",
"=",
"safe_map",
")",
"[",
"0",
"]",
"except",
"IndexError",
":",
"raise",
"VersionNotFound",
"(",
"'Version not found in WSDL service documentation'",
")",
"else",
":",
"return",
"version_el",
".",
"text"
] | c3194932360445563cfe0c7e034e41f09358c7eb |
test | parser_from_schema | Returns an XSD-schema-enabled lxml parser from a WSDL or XSD
`schema_url` can of course be local path via file:// url | dirtyebay/utils.py | def parser_from_schema(schema_url, require_version=True):
"""
Returns an XSD-schema-enabled lxml parser from a WSDL or XSD
`schema_url` can of course be local path via file:// url
"""
schema_tree = etree.parse(schema_url)
def get_version(element, getter):
try:
return getter(element)
except VersionNotFound:
if require_version:
raise
else:
return None
root = schema_tree.getroot()
if root.tag == '{%s}definitions' % namespaces.WSDL:
# wsdl should contain an embedded schema
schema_el = schema_tree.find('wsdl:types/xs:schema', namespaces=NS_MAP)
version = get_version(root, version_from_wsdl)
else:
schema_el = root
version = get_version(schema_el, version_from_schema)
schema = etree.XMLSchema(schema_el)
return objectify.makeparser(schema=schema), version | def parser_from_schema(schema_url, require_version=True):
"""
Returns an XSD-schema-enabled lxml parser from a WSDL or XSD
`schema_url` can of course be local path via file:// url
"""
schema_tree = etree.parse(schema_url)
def get_version(element, getter):
try:
return getter(element)
except VersionNotFound:
if require_version:
raise
else:
return None
root = schema_tree.getroot()
if root.tag == '{%s}definitions' % namespaces.WSDL:
# wsdl should contain an embedded schema
schema_el = schema_tree.find('wsdl:types/xs:schema', namespaces=NS_MAP)
version = get_version(root, version_from_wsdl)
else:
schema_el = root
version = get_version(schema_el, version_from_schema)
schema = etree.XMLSchema(schema_el)
return objectify.makeparser(schema=schema), version | [
"Returns",
"an",
"XSD",
"-",
"schema",
"-",
"enabled",
"lxml",
"parser",
"from",
"a",
"WSDL",
"or",
"XSD"
] | anentropic/dirtyebay | python | https://github.com/anentropic/dirtyebay/blob/c3194932360445563cfe0c7e034e41f09358c7eb/dirtyebay/utils.py#L129-L156 | [
"def",
"parser_from_schema",
"(",
"schema_url",
",",
"require_version",
"=",
"True",
")",
":",
"schema_tree",
"=",
"etree",
".",
"parse",
"(",
"schema_url",
")",
"def",
"get_version",
"(",
"element",
",",
"getter",
")",
":",
"try",
":",
"return",
"getter",
"(",
"element",
")",
"except",
"VersionNotFound",
":",
"if",
"require_version",
":",
"raise",
"else",
":",
"return",
"None",
"root",
"=",
"schema_tree",
".",
"getroot",
"(",
")",
"if",
"root",
".",
"tag",
"==",
"'{%s}definitions'",
"%",
"namespaces",
".",
"WSDL",
":",
"# wsdl should contain an embedded schema",
"schema_el",
"=",
"schema_tree",
".",
"find",
"(",
"'wsdl:types/xs:schema'",
",",
"namespaces",
"=",
"NS_MAP",
")",
"version",
"=",
"get_version",
"(",
"root",
",",
"version_from_wsdl",
")",
"else",
":",
"schema_el",
"=",
"root",
"version",
"=",
"get_version",
"(",
"schema_el",
",",
"version_from_schema",
")",
"schema",
"=",
"etree",
".",
"XMLSchema",
"(",
"schema_el",
")",
"return",
"objectify",
".",
"makeparser",
"(",
"schema",
"=",
"schema",
")",
",",
"version"
] | c3194932360445563cfe0c7e034e41f09358c7eb |
test | askQuestion | Ask a question to STDOUT and return answer from STDIN
Args:
question: A string question that will be printed to stdout
Kwargs:
required: True indicates the question must be answered
answers: A sequence of dicts with key value pairs where the key will
be a user-selectable choice menu item with the value as its related
description.
tries: Integer value of maximum amount of attempts users may exercise
to select a valid answer
Returns:
A user specified string when required is False and answers was not
provided. A user selected key from answers when required is True and
answers was provided. None if required was false and user did not
enter a answer. None if required was True and user reached reached
maximum limit of tries. | sparc/common/cli.py | def askQuestion(question, required = True, answers = dict(), tries = 3):
"""Ask a question to STDOUT and return answer from STDIN
Args:
question: A string question that will be printed to stdout
Kwargs:
required: True indicates the question must be answered
answers: A sequence of dicts with key value pairs where the key will
be a user-selectable choice menu item with the value as its related
description.
tries: Integer value of maximum amount of attempts users may exercise
to select a valid answer
Returns:
A user specified string when required is False and answers was not
provided. A user selected key from answers when required is True and
answers was provided. None if required was false and user did not
enter a answer. None if required was True and user reached reached
maximum limit of tries.
"""
print question
if not answers:
# we get the user's answer via a named utility because direct calls to
# raw_input() are hard to test (this way, tests can provide their own
# implementation of ISelectedChoice without calls to raw_input())
answer = getUtility(ISelectedChoice, 'sparc.common.cli_selected_choice').selection()
_attempts = 0
while True:
if tries and _attempts > tries:
print (u"Too many attempts")
return None
if not required or answer:
return answer if answer else None
print (u"Invalid input, please try again.")
answer = getUtility(ISelectedChoice, 'sparc.common.cli_selected_choice').selection()
_attempts += 1
for selection_pair in answers:
for key, potential_answer in selection_pair.iteritems():
print "(" + key + ") " + potential_answer
break # only process the first dict entry for the sequence
_attempts = 0
while True:
answer = getUtility(ISelectedChoice, 'sparc.common.cli_selected_choice').selection()
if tries and _attempts > tries:
print _(u"Too many attempts")
return None
if not answer and not required:
return None
for selection_pair in answers:
if answer in selection_pair:
return answer
print (u"Invalid selection: {}, please try again.".format(answer))
answer = getUtility(ISelectedChoice, 'sparc.common.cli_selected_choice').selection()
_attempts += 1 | def askQuestion(question, required = True, answers = dict(), tries = 3):
"""Ask a question to STDOUT and return answer from STDIN
Args:
question: A string question that will be printed to stdout
Kwargs:
required: True indicates the question must be answered
answers: A sequence of dicts with key value pairs where the key will
be a user-selectable choice menu item with the value as its related
description.
tries: Integer value of maximum amount of attempts users may exercise
to select a valid answer
Returns:
A user specified string when required is False and answers was not
provided. A user selected key from answers when required is True and
answers was provided. None if required was false and user did not
enter a answer. None if required was True and user reached reached
maximum limit of tries.
"""
print question
if not answers:
# we get the user's answer via a named utility because direct calls to
# raw_input() are hard to test (this way, tests can provide their own
# implementation of ISelectedChoice without calls to raw_input())
answer = getUtility(ISelectedChoice, 'sparc.common.cli_selected_choice').selection()
_attempts = 0
while True:
if tries and _attempts > tries:
print (u"Too many attempts")
return None
if not required or answer:
return answer if answer else None
print (u"Invalid input, please try again.")
answer = getUtility(ISelectedChoice, 'sparc.common.cli_selected_choice').selection()
_attempts += 1
for selection_pair in answers:
for key, potential_answer in selection_pair.iteritems():
print "(" + key + ") " + potential_answer
break # only process the first dict entry for the sequence
_attempts = 0
while True:
answer = getUtility(ISelectedChoice, 'sparc.common.cli_selected_choice').selection()
if tries and _attempts > tries:
print _(u"Too many attempts")
return None
if not answer and not required:
return None
for selection_pair in answers:
if answer in selection_pair:
return answer
print (u"Invalid selection: {}, please try again.".format(answer))
answer = getUtility(ISelectedChoice, 'sparc.common.cli_selected_choice').selection()
_attempts += 1 | [
"Ask",
"a",
"question",
"to",
"STDOUT",
"and",
"return",
"answer",
"from",
"STDIN",
"Args",
":",
"question",
":",
"A",
"string",
"question",
"that",
"will",
"be",
"printed",
"to",
"stdout",
"Kwargs",
":",
"required",
":",
"True",
"indicates",
"the",
"question",
"must",
"be",
"answered",
"answers",
":",
"A",
"sequence",
"of",
"dicts",
"with",
"key",
"value",
"pairs",
"where",
"the",
"key",
"will",
"be",
"a",
"user",
"-",
"selectable",
"choice",
"menu",
"item",
"with",
"the",
"value",
"as",
"its",
"related",
"description",
".",
"tries",
":",
"Integer",
"value",
"of",
"maximum",
"amount",
"of",
"attempts",
"users",
"may",
"exercise",
"to",
"select",
"a",
"valid",
"answer",
"Returns",
":",
"A",
"user",
"specified",
"string",
"when",
"required",
"is",
"False",
"and",
"answers",
"was",
"not",
"provided",
".",
"A",
"user",
"selected",
"key",
"from",
"answers",
"when",
"required",
"is",
"True",
"and",
"answers",
"was",
"provided",
".",
"None",
"if",
"required",
"was",
"false",
"and",
"user",
"did",
"not",
"enter",
"a",
"answer",
".",
"None",
"if",
"required",
"was",
"True",
"and",
"user",
"reached",
"reached",
"maximum",
"limit",
"of",
"tries",
"."
] | davisd50/sparc.common | python | https://github.com/davisd50/sparc.common/blob/50fb47ff6f42042c363f27bd13cb3919a560e235/sparc/common/cli.py#L15-L70 | [
"def",
"askQuestion",
"(",
"question",
",",
"required",
"=",
"True",
",",
"answers",
"=",
"dict",
"(",
")",
",",
"tries",
"=",
"3",
")",
":",
"print",
"question",
"if",
"not",
"answers",
":",
"# we get the user's answer via a named utility because direct calls to",
"# raw_input() are hard to test (this way, tests can provide their own",
"# implementation of ISelectedChoice without calls to raw_input())",
"answer",
"=",
"getUtility",
"(",
"ISelectedChoice",
",",
"'sparc.common.cli_selected_choice'",
")",
".",
"selection",
"(",
")",
"_attempts",
"=",
"0",
"while",
"True",
":",
"if",
"tries",
"and",
"_attempts",
">",
"tries",
":",
"print",
"(",
"u\"Too many attempts\"",
")",
"return",
"None",
"if",
"not",
"required",
"or",
"answer",
":",
"return",
"answer",
"if",
"answer",
"else",
"None",
"print",
"(",
"u\"Invalid input, please try again.\"",
")",
"answer",
"=",
"getUtility",
"(",
"ISelectedChoice",
",",
"'sparc.common.cli_selected_choice'",
")",
".",
"selection",
"(",
")",
"_attempts",
"+=",
"1",
"for",
"selection_pair",
"in",
"answers",
":",
"for",
"key",
",",
"potential_answer",
"in",
"selection_pair",
".",
"iteritems",
"(",
")",
":",
"print",
"\"(\"",
"+",
"key",
"+",
"\") \"",
"+",
"potential_answer",
"break",
"# only process the first dict entry for the sequence",
"_attempts",
"=",
"0",
"while",
"True",
":",
"answer",
"=",
"getUtility",
"(",
"ISelectedChoice",
",",
"'sparc.common.cli_selected_choice'",
")",
".",
"selection",
"(",
")",
"if",
"tries",
"and",
"_attempts",
">",
"tries",
":",
"print",
"_",
"(",
"u\"Too many attempts\"",
")",
"return",
"None",
"if",
"not",
"answer",
"and",
"not",
"required",
":",
"return",
"None",
"for",
"selection_pair",
"in",
"answers",
":",
"if",
"answer",
"in",
"selection_pair",
":",
"return",
"answer",
"print",
"(",
"u\"Invalid selection: {}, please try again.\"",
".",
"format",
"(",
"answer",
")",
")",
"answer",
"=",
"getUtility",
"(",
"ISelectedChoice",
",",
"'sparc.common.cli_selected_choice'",
")",
".",
"selection",
"(",
")",
"_attempts",
"+=",
"1"
] | 50fb47ff6f42042c363f27bd13cb3919a560e235 |
test | authenticate_unless_readonly | authenticate this page *unless* readonly view is active.
In read-only mode, the notebook list and print view should
be accessible without authentication. | environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/handlers.py | def authenticate_unless_readonly(f, self, *args, **kwargs):
"""authenticate this page *unless* readonly view is active.
In read-only mode, the notebook list and print view should
be accessible without authentication.
"""
@web.authenticated
def auth_f(self, *args, **kwargs):
return f(self, *args, **kwargs)
if self.application.read_only:
return f(self, *args, **kwargs)
else:
return auth_f(self, *args, **kwargs) | def authenticate_unless_readonly(f, self, *args, **kwargs):
"""authenticate this page *unless* readonly view is active.
In read-only mode, the notebook list and print view should
be accessible without authentication.
"""
@web.authenticated
def auth_f(self, *args, **kwargs):
return f(self, *args, **kwargs)
if self.application.read_only:
return f(self, *args, **kwargs)
else:
return auth_f(self, *args, **kwargs) | [
"authenticate",
"this",
"page",
"*",
"unless",
"*",
"readonly",
"view",
"is",
"active",
".",
"In",
"read",
"-",
"only",
"mode",
"the",
"notebook",
"list",
"and",
"print",
"view",
"should",
"be",
"accessible",
"without",
"authentication",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/handlers.py#L102-L116 | [
"def",
"authenticate_unless_readonly",
"(",
"f",
",",
"self",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"@",
"web",
".",
"authenticated",
"def",
"auth_f",
"(",
"self",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"return",
"f",
"(",
"self",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
"if",
"self",
".",
"application",
".",
"read_only",
":",
"return",
"f",
"(",
"self",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
"else",
":",
"return",
"auth_f",
"(",
"self",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | AuthenticatedHandler.ws_url | websocket url matching the current request
turns http[s]://host[:port] into
ws[s]://host[:port] | environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/handlers.py | def ws_url(self):
"""websocket url matching the current request
turns http[s]://host[:port] into
ws[s]://host[:port]
"""
proto = self.request.protocol.replace('http', 'ws')
host = self.application.ipython_app.websocket_host # default to config value
if host == '':
host = self.request.host # get from request
return "%s://%s" % (proto, host) | def ws_url(self):
"""websocket url matching the current request
turns http[s]://host[:port] into
ws[s]://host[:port]
"""
proto = self.request.protocol.replace('http', 'ws')
host = self.application.ipython_app.websocket_host # default to config value
if host == '':
host = self.request.host # get from request
return "%s://%s" % (proto, host) | [
"websocket",
"url",
"matching",
"the",
"current",
"request"
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/handlers.py#L170-L180 | [
"def",
"ws_url",
"(",
"self",
")",
":",
"proto",
"=",
"self",
".",
"request",
".",
"protocol",
".",
"replace",
"(",
"'http'",
",",
"'ws'",
")",
"host",
"=",
"self",
".",
"application",
".",
"ipython_app",
".",
"websocket_host",
"# default to config value",
"if",
"host",
"==",
"''",
":",
"host",
"=",
"self",
".",
"request",
".",
"host",
"# get from request",
"return",
"\"%s://%s\"",
"%",
"(",
"proto",
",",
"host",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | ZMQStreamHandler._reserialize_reply | Reserialize a reply message using JSON.
This takes the msg list from the ZMQ socket, unserializes it using
self.session and then serializes the result using JSON. This method
should be used by self._on_zmq_reply to build messages that can
be sent back to the browser. | environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/handlers.py | def _reserialize_reply(self, msg_list):
"""Reserialize a reply message using JSON.
This takes the msg list from the ZMQ socket, unserializes it using
self.session and then serializes the result using JSON. This method
should be used by self._on_zmq_reply to build messages that can
be sent back to the browser.
"""
idents, msg_list = self.session.feed_identities(msg_list)
msg = self.session.unserialize(msg_list)
try:
msg['header'].pop('date')
except KeyError:
pass
try:
msg['parent_header'].pop('date')
except KeyError:
pass
msg.pop('buffers')
return jsonapi.dumps(msg, default=date_default) | def _reserialize_reply(self, msg_list):
"""Reserialize a reply message using JSON.
This takes the msg list from the ZMQ socket, unserializes it using
self.session and then serializes the result using JSON. This method
should be used by self._on_zmq_reply to build messages that can
be sent back to the browser.
"""
idents, msg_list = self.session.feed_identities(msg_list)
msg = self.session.unserialize(msg_list)
try:
msg['header'].pop('date')
except KeyError:
pass
try:
msg['parent_header'].pop('date')
except KeyError:
pass
msg.pop('buffers')
return jsonapi.dumps(msg, default=date_default) | [
"Reserialize",
"a",
"reply",
"message",
"using",
"JSON",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/handlers.py#L371-L390 | [
"def",
"_reserialize_reply",
"(",
"self",
",",
"msg_list",
")",
":",
"idents",
",",
"msg_list",
"=",
"self",
".",
"session",
".",
"feed_identities",
"(",
"msg_list",
")",
"msg",
"=",
"self",
".",
"session",
".",
"unserialize",
"(",
"msg_list",
")",
"try",
":",
"msg",
"[",
"'header'",
"]",
".",
"pop",
"(",
"'date'",
")",
"except",
"KeyError",
":",
"pass",
"try",
":",
"msg",
"[",
"'parent_header'",
"]",
".",
"pop",
"(",
"'date'",
")",
"except",
"KeyError",
":",
"pass",
"msg",
".",
"pop",
"(",
"'buffers'",
")",
"return",
"jsonapi",
".",
"dumps",
"(",
"msg",
",",
"default",
"=",
"date_default",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | AuthenticatedZMQStreamHandler._inject_cookie_message | Inject the first message, which is the document cookie,
for authentication. | environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/handlers.py | def _inject_cookie_message(self, msg):
"""Inject the first message, which is the document cookie,
for authentication."""
if isinstance(msg, unicode):
# Cookie can't constructor doesn't accept unicode strings for some reason
msg = msg.encode('utf8', 'replace')
try:
self.request._cookies = Cookie.SimpleCookie(msg)
except:
logging.warn("couldn't parse cookie string: %s",msg, exc_info=True) | def _inject_cookie_message(self, msg):
"""Inject the first message, which is the document cookie,
for authentication."""
if isinstance(msg, unicode):
# Cookie can't constructor doesn't accept unicode strings for some reason
msg = msg.encode('utf8', 'replace')
try:
self.request._cookies = Cookie.SimpleCookie(msg)
except:
logging.warn("couldn't parse cookie string: %s",msg, exc_info=True) | [
"Inject",
"the",
"first",
"message",
"which",
"is",
"the",
"document",
"cookie",
"for",
"authentication",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/handlers.py#L429-L438 | [
"def",
"_inject_cookie_message",
"(",
"self",
",",
"msg",
")",
":",
"if",
"isinstance",
"(",
"msg",
",",
"unicode",
")",
":",
"# Cookie can't constructor doesn't accept unicode strings for some reason",
"msg",
"=",
"msg",
".",
"encode",
"(",
"'utf8'",
",",
"'replace'",
")",
"try",
":",
"self",
".",
"request",
".",
"_cookies",
"=",
"Cookie",
".",
"SimpleCookie",
"(",
"msg",
")",
"except",
":",
"logging",
".",
"warn",
"(",
"\"couldn't parse cookie string: %s\"",
",",
"msg",
",",
"exc_info",
"=",
"True",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | IOPubHandler.start_hb | Start the heartbeating and call the callback if the kernel dies. | environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/handlers.py | def start_hb(self, callback):
"""Start the heartbeating and call the callback if the kernel dies."""
if not self._beating:
self._kernel_alive = True
def ping_or_dead():
self.hb_stream.flush()
if self._kernel_alive:
self._kernel_alive = False
self.hb_stream.send(b'ping')
# flush stream to force immediate socket send
self.hb_stream.flush()
else:
try:
callback()
except:
pass
finally:
self.stop_hb()
def beat_received(msg):
self._kernel_alive = True
self.hb_stream.on_recv(beat_received)
loop = ioloop.IOLoop.instance()
self._hb_periodic_callback = ioloop.PeriodicCallback(ping_or_dead, self.time_to_dead*1000, loop)
loop.add_timeout(time.time()+self.first_beat, self._really_start_hb)
self._beating= True | def start_hb(self, callback):
"""Start the heartbeating and call the callback if the kernel dies."""
if not self._beating:
self._kernel_alive = True
def ping_or_dead():
self.hb_stream.flush()
if self._kernel_alive:
self._kernel_alive = False
self.hb_stream.send(b'ping')
# flush stream to force immediate socket send
self.hb_stream.flush()
else:
try:
callback()
except:
pass
finally:
self.stop_hb()
def beat_received(msg):
self._kernel_alive = True
self.hb_stream.on_recv(beat_received)
loop = ioloop.IOLoop.instance()
self._hb_periodic_callback = ioloop.PeriodicCallback(ping_or_dead, self.time_to_dead*1000, loop)
loop.add_timeout(time.time()+self.first_beat, self._really_start_hb)
self._beating= True | [
"Start",
"the",
"heartbeating",
"and",
"call",
"the",
"callback",
"if",
"the",
"kernel",
"dies",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/handlers.py#L493-L520 | [
"def",
"start_hb",
"(",
"self",
",",
"callback",
")",
":",
"if",
"not",
"self",
".",
"_beating",
":",
"self",
".",
"_kernel_alive",
"=",
"True",
"def",
"ping_or_dead",
"(",
")",
":",
"self",
".",
"hb_stream",
".",
"flush",
"(",
")",
"if",
"self",
".",
"_kernel_alive",
":",
"self",
".",
"_kernel_alive",
"=",
"False",
"self",
".",
"hb_stream",
".",
"send",
"(",
"b'ping'",
")",
"# flush stream to force immediate socket send",
"self",
".",
"hb_stream",
".",
"flush",
"(",
")",
"else",
":",
"try",
":",
"callback",
"(",
")",
"except",
":",
"pass",
"finally",
":",
"self",
".",
"stop_hb",
"(",
")",
"def",
"beat_received",
"(",
"msg",
")",
":",
"self",
".",
"_kernel_alive",
"=",
"True",
"self",
".",
"hb_stream",
".",
"on_recv",
"(",
"beat_received",
")",
"loop",
"=",
"ioloop",
".",
"IOLoop",
".",
"instance",
"(",
")",
"self",
".",
"_hb_periodic_callback",
"=",
"ioloop",
".",
"PeriodicCallback",
"(",
"ping_or_dead",
",",
"self",
".",
"time_to_dead",
"*",
"1000",
",",
"loop",
")",
"loop",
".",
"add_timeout",
"(",
"time",
".",
"time",
"(",
")",
"+",
"self",
".",
"first_beat",
",",
"self",
".",
"_really_start_hb",
")",
"self",
".",
"_beating",
"=",
"True"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | IOPubHandler._really_start_hb | callback for delayed heartbeat start
Only start the hb loop if we haven't been closed during the wait. | environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/handlers.py | def _really_start_hb(self):
"""callback for delayed heartbeat start
Only start the hb loop if we haven't been closed during the wait.
"""
if self._beating and not self.hb_stream.closed():
self._hb_periodic_callback.start() | def _really_start_hb(self):
"""callback for delayed heartbeat start
Only start the hb loop if we haven't been closed during the wait.
"""
if self._beating and not self.hb_stream.closed():
self._hb_periodic_callback.start() | [
"callback",
"for",
"delayed",
"heartbeat",
"start",
"Only",
"start",
"the",
"hb",
"loop",
"if",
"we",
"haven",
"t",
"been",
"closed",
"during",
"the",
"wait",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/handlers.py#L522-L528 | [
"def",
"_really_start_hb",
"(",
"self",
")",
":",
"if",
"self",
".",
"_beating",
"and",
"not",
"self",
".",
"hb_stream",
".",
"closed",
"(",
")",
":",
"self",
".",
"_hb_periodic_callback",
".",
"start",
"(",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | IOPubHandler.stop_hb | Stop the heartbeating and cancel all related callbacks. | environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/handlers.py | def stop_hb(self):
"""Stop the heartbeating and cancel all related callbacks."""
if self._beating:
self._beating = False
self._hb_periodic_callback.stop()
if not self.hb_stream.closed():
self.hb_stream.on_recv(None) | def stop_hb(self):
"""Stop the heartbeating and cancel all related callbacks."""
if self._beating:
self._beating = False
self._hb_periodic_callback.stop()
if not self.hb_stream.closed():
self.hb_stream.on_recv(None) | [
"Stop",
"the",
"heartbeating",
"and",
"cancel",
"all",
"related",
"callbacks",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/frontend/html/notebook/handlers.py#L530-L536 | [
"def",
"stop_hb",
"(",
"self",
")",
":",
"if",
"self",
".",
"_beating",
":",
"self",
".",
"_beating",
"=",
"False",
"self",
".",
"_hb_periodic_callback",
".",
"stop",
"(",
")",
"if",
"not",
"self",
".",
"hb_stream",
".",
"closed",
"(",
")",
":",
"self",
".",
"hb_stream",
".",
"on_recv",
"(",
"None",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | Demo.fload | Load file object. | environment/lib/python2.7/site-packages/IPython/lib/demo.py | def fload(self):
"""Load file object."""
# read data and parse into blocks
if hasattr(self, 'fobj') and self.fobj is not None:
self.fobj.close()
if hasattr(self.src, "read"):
# It seems to be a file or a file-like object
self.fobj = self.src
else:
# Assume it's a string or something that can be converted to one
self.fobj = open(self.fname) | def fload(self):
"""Load file object."""
# read data and parse into blocks
if hasattr(self, 'fobj') and self.fobj is not None:
self.fobj.close()
if hasattr(self.src, "read"):
# It seems to be a file or a file-like object
self.fobj = self.src
else:
# Assume it's a string or something that can be converted to one
self.fobj = open(self.fname) | [
"Load",
"file",
"object",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/lib/demo.py#L256-L266 | [
"def",
"fload",
"(",
"self",
")",
":",
"# read data and parse into blocks",
"if",
"hasattr",
"(",
"self",
",",
"'fobj'",
")",
"and",
"self",
".",
"fobj",
"is",
"not",
"None",
":",
"self",
".",
"fobj",
".",
"close",
"(",
")",
"if",
"hasattr",
"(",
"self",
".",
"src",
",",
"\"read\"",
")",
":",
"# It seems to be a file or a file-like object",
"self",
".",
"fobj",
"=",
"self",
".",
"src",
"else",
":",
"# Assume it's a string or something that can be converted to one",
"self",
".",
"fobj",
"=",
"open",
"(",
"self",
".",
"fname",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | Demo.reload | Reload source from disk and initialize state. | environment/lib/python2.7/site-packages/IPython/lib/demo.py | def reload(self):
"""Reload source from disk and initialize state."""
self.fload()
self.src = self.fobj.read()
src_b = [b.strip() for b in self.re_stop.split(self.src) if b]
self._silent = [bool(self.re_silent.findall(b)) for b in src_b]
self._auto = [bool(self.re_auto.findall(b)) for b in src_b]
# if auto_all is not given (def. None), we read it from the file
if self.auto_all is None:
self.auto_all = bool(self.re_auto_all.findall(src_b[0]))
else:
self.auto_all = bool(self.auto_all)
# Clean the sources from all markup so it doesn't get displayed when
# running the demo
src_blocks = []
auto_strip = lambda s: self.re_auto.sub('',s)
for i,b in enumerate(src_b):
if self._auto[i]:
src_blocks.append(auto_strip(b))
else:
src_blocks.append(b)
# remove the auto_all marker
src_blocks[0] = self.re_auto_all.sub('',src_blocks[0])
self.nblocks = len(src_blocks)
self.src_blocks = src_blocks
# also build syntax-highlighted source
self.src_blocks_colored = map(self.ip_colorize,self.src_blocks)
# ensure clean namespace and seek offset
self.reset() | def reload(self):
"""Reload source from disk and initialize state."""
self.fload()
self.src = self.fobj.read()
src_b = [b.strip() for b in self.re_stop.split(self.src) if b]
self._silent = [bool(self.re_silent.findall(b)) for b in src_b]
self._auto = [bool(self.re_auto.findall(b)) for b in src_b]
# if auto_all is not given (def. None), we read it from the file
if self.auto_all is None:
self.auto_all = bool(self.re_auto_all.findall(src_b[0]))
else:
self.auto_all = bool(self.auto_all)
# Clean the sources from all markup so it doesn't get displayed when
# running the demo
src_blocks = []
auto_strip = lambda s: self.re_auto.sub('',s)
for i,b in enumerate(src_b):
if self._auto[i]:
src_blocks.append(auto_strip(b))
else:
src_blocks.append(b)
# remove the auto_all marker
src_blocks[0] = self.re_auto_all.sub('',src_blocks[0])
self.nblocks = len(src_blocks)
self.src_blocks = src_blocks
# also build syntax-highlighted source
self.src_blocks_colored = map(self.ip_colorize,self.src_blocks)
# ensure clean namespace and seek offset
self.reset() | [
"Reload",
"source",
"from",
"disk",
"and",
"initialize",
"state",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/lib/demo.py#L268-L302 | [
"def",
"reload",
"(",
"self",
")",
":",
"self",
".",
"fload",
"(",
")",
"self",
".",
"src",
"=",
"self",
".",
"fobj",
".",
"read",
"(",
")",
"src_b",
"=",
"[",
"b",
".",
"strip",
"(",
")",
"for",
"b",
"in",
"self",
".",
"re_stop",
".",
"split",
"(",
"self",
".",
"src",
")",
"if",
"b",
"]",
"self",
".",
"_silent",
"=",
"[",
"bool",
"(",
"self",
".",
"re_silent",
".",
"findall",
"(",
"b",
")",
")",
"for",
"b",
"in",
"src_b",
"]",
"self",
".",
"_auto",
"=",
"[",
"bool",
"(",
"self",
".",
"re_auto",
".",
"findall",
"(",
"b",
")",
")",
"for",
"b",
"in",
"src_b",
"]",
"# if auto_all is not given (def. None), we read it from the file",
"if",
"self",
".",
"auto_all",
"is",
"None",
":",
"self",
".",
"auto_all",
"=",
"bool",
"(",
"self",
".",
"re_auto_all",
".",
"findall",
"(",
"src_b",
"[",
"0",
"]",
")",
")",
"else",
":",
"self",
".",
"auto_all",
"=",
"bool",
"(",
"self",
".",
"auto_all",
")",
"# Clean the sources from all markup so it doesn't get displayed when",
"# running the demo",
"src_blocks",
"=",
"[",
"]",
"auto_strip",
"=",
"lambda",
"s",
":",
"self",
".",
"re_auto",
".",
"sub",
"(",
"''",
",",
"s",
")",
"for",
"i",
",",
"b",
"in",
"enumerate",
"(",
"src_b",
")",
":",
"if",
"self",
".",
"_auto",
"[",
"i",
"]",
":",
"src_blocks",
".",
"append",
"(",
"auto_strip",
"(",
"b",
")",
")",
"else",
":",
"src_blocks",
".",
"append",
"(",
"b",
")",
"# remove the auto_all marker",
"src_blocks",
"[",
"0",
"]",
"=",
"self",
".",
"re_auto_all",
".",
"sub",
"(",
"''",
",",
"src_blocks",
"[",
"0",
"]",
")",
"self",
".",
"nblocks",
"=",
"len",
"(",
"src_blocks",
")",
"self",
".",
"src_blocks",
"=",
"src_blocks",
"# also build syntax-highlighted source",
"self",
".",
"src_blocks_colored",
"=",
"map",
"(",
"self",
".",
"ip_colorize",
",",
"self",
".",
"src_blocks",
")",
"# ensure clean namespace and seek offset",
"self",
".",
"reset",
"(",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | Demo._get_index | Get the current block index, validating and checking status.
Returns None if the demo is finished | environment/lib/python2.7/site-packages/IPython/lib/demo.py | def _get_index(self,index):
"""Get the current block index, validating and checking status.
Returns None if the demo is finished"""
if index is None:
if self.finished:
print >>io.stdout, 'Demo finished. Use <demo_name>.reset() if you want to rerun it.'
return None
index = self.block_index
else:
self._validate_index(index)
return index | def _get_index(self,index):
"""Get the current block index, validating and checking status.
Returns None if the demo is finished"""
if index is None:
if self.finished:
print >>io.stdout, 'Demo finished. Use <demo_name>.reset() if you want to rerun it.'
return None
index = self.block_index
else:
self._validate_index(index)
return index | [
"Get",
"the",
"current",
"block",
"index",
"validating",
"and",
"checking",
"status",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/lib/demo.py#L314-L326 | [
"def",
"_get_index",
"(",
"self",
",",
"index",
")",
":",
"if",
"index",
"is",
"None",
":",
"if",
"self",
".",
"finished",
":",
"print",
">>",
"io",
".",
"stdout",
",",
"'Demo finished. Use <demo_name>.reset() if you want to rerun it.'",
"return",
"None",
"index",
"=",
"self",
".",
"block_index",
"else",
":",
"self",
".",
"_validate_index",
"(",
"index",
")",
"return",
"index"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | Demo.seek | Move the current seek pointer to the given block.
You can use negative indices to seek from the end, with identical
semantics to those of Python lists. | environment/lib/python2.7/site-packages/IPython/lib/demo.py | def seek(self,index):
"""Move the current seek pointer to the given block.
You can use negative indices to seek from the end, with identical
semantics to those of Python lists."""
if index<0:
index = self.nblocks + index
self._validate_index(index)
self.block_index = index
self.finished = False | def seek(self,index):
"""Move the current seek pointer to the given block.
You can use negative indices to seek from the end, with identical
semantics to those of Python lists."""
if index<0:
index = self.nblocks + index
self._validate_index(index)
self.block_index = index
self.finished = False | [
"Move",
"the",
"current",
"seek",
"pointer",
"to",
"the",
"given",
"block",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/lib/demo.py#L328-L337 | [
"def",
"seek",
"(",
"self",
",",
"index",
")",
":",
"if",
"index",
"<",
"0",
":",
"index",
"=",
"self",
".",
"nblocks",
"+",
"index",
"self",
".",
"_validate_index",
"(",
"index",
")",
"self",
".",
"block_index",
"=",
"index",
"self",
".",
"finished",
"=",
"False"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | Demo.edit | Edit a block.
If no number is given, use the last block executed.
This edits the in-memory copy of the demo, it does NOT modify the
original source file. If you want to do that, simply open the file in
an editor and use reload() when you make changes to the file. This
method is meant to let you change a block during a demonstration for
explanatory purposes, without damaging your original script. | environment/lib/python2.7/site-packages/IPython/lib/demo.py | def edit(self,index=None):
"""Edit a block.
If no number is given, use the last block executed.
This edits the in-memory copy of the demo, it does NOT modify the
original source file. If you want to do that, simply open the file in
an editor and use reload() when you make changes to the file. This
method is meant to let you change a block during a demonstration for
explanatory purposes, without damaging your original script."""
index = self._get_index(index)
if index is None:
return
# decrease the index by one (unless we're at the very beginning), so
# that the default demo.edit() call opens up the sblock we've last run
if index>0:
index -= 1
filename = self.shell.mktempfile(self.src_blocks[index])
self.shell.hooks.editor(filename,1)
new_block = file_read(filename)
# update the source and colored block
self.src_blocks[index] = new_block
self.src_blocks_colored[index] = self.ip_colorize(new_block)
self.block_index = index
# call to run with the newly edited index
self() | def edit(self,index=None):
"""Edit a block.
If no number is given, use the last block executed.
This edits the in-memory copy of the demo, it does NOT modify the
original source file. If you want to do that, simply open the file in
an editor and use reload() when you make changes to the file. This
method is meant to let you change a block during a demonstration for
explanatory purposes, without damaging your original script."""
index = self._get_index(index)
if index is None:
return
# decrease the index by one (unless we're at the very beginning), so
# that the default demo.edit() call opens up the sblock we've last run
if index>0:
index -= 1
filename = self.shell.mktempfile(self.src_blocks[index])
self.shell.hooks.editor(filename,1)
new_block = file_read(filename)
# update the source and colored block
self.src_blocks[index] = new_block
self.src_blocks_colored[index] = self.ip_colorize(new_block)
self.block_index = index
# call to run with the newly edited index
self() | [
"Edit",
"a",
"block",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/lib/demo.py#L354-L381 | [
"def",
"edit",
"(",
"self",
",",
"index",
"=",
"None",
")",
":",
"index",
"=",
"self",
".",
"_get_index",
"(",
"index",
")",
"if",
"index",
"is",
"None",
":",
"return",
"# decrease the index by one (unless we're at the very beginning), so",
"# that the default demo.edit() call opens up the sblock we've last run",
"if",
"index",
">",
"0",
":",
"index",
"-=",
"1",
"filename",
"=",
"self",
".",
"shell",
".",
"mktempfile",
"(",
"self",
".",
"src_blocks",
"[",
"index",
"]",
")",
"self",
".",
"shell",
".",
"hooks",
".",
"editor",
"(",
"filename",
",",
"1",
")",
"new_block",
"=",
"file_read",
"(",
"filename",
")",
"# update the source and colored block",
"self",
".",
"src_blocks",
"[",
"index",
"]",
"=",
"new_block",
"self",
".",
"src_blocks_colored",
"[",
"index",
"]",
"=",
"self",
".",
"ip_colorize",
"(",
"new_block",
")",
"self",
".",
"block_index",
"=",
"index",
"# call to run with the newly edited index",
"self",
"(",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | Demo.show | Show a single block on screen | environment/lib/python2.7/site-packages/IPython/lib/demo.py | def show(self,index=None):
"""Show a single block on screen"""
index = self._get_index(index)
if index is None:
return
print >>io.stdout, self.marquee('<%s> block # %s (%s remaining)' %
(self.title,index,self.nblocks-index-1))
print >>io.stdout,(self.src_blocks_colored[index])
sys.stdout.flush() | def show(self,index=None):
"""Show a single block on screen"""
index = self._get_index(index)
if index is None:
return
print >>io.stdout, self.marquee('<%s> block # %s (%s remaining)' %
(self.title,index,self.nblocks-index-1))
print >>io.stdout,(self.src_blocks_colored[index])
sys.stdout.flush() | [
"Show",
"a",
"single",
"block",
"on",
"screen"
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/lib/demo.py#L383-L393 | [
"def",
"show",
"(",
"self",
",",
"index",
"=",
"None",
")",
":",
"index",
"=",
"self",
".",
"_get_index",
"(",
"index",
")",
"if",
"index",
"is",
"None",
":",
"return",
"print",
">>",
"io",
".",
"stdout",
",",
"self",
".",
"marquee",
"(",
"'<%s> block # %s (%s remaining)'",
"%",
"(",
"self",
".",
"title",
",",
"index",
",",
"self",
".",
"nblocks",
"-",
"index",
"-",
"1",
")",
")",
"print",
">>",
"io",
".",
"stdout",
",",
"(",
"self",
".",
"src_blocks_colored",
"[",
"index",
"]",
")",
"sys",
".",
"stdout",
".",
"flush",
"(",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | Demo.show_all | Show entire demo on screen, block by block | environment/lib/python2.7/site-packages/IPython/lib/demo.py | def show_all(self):
"""Show entire demo on screen, block by block"""
fname = self.title
title = self.title
nblocks = self.nblocks
silent = self._silent
marquee = self.marquee
for index,block in enumerate(self.src_blocks_colored):
if silent[index]:
print >>io.stdout, marquee('<%s> SILENT block # %s (%s remaining)' %
(title,index,nblocks-index-1))
else:
print >>io.stdout, marquee('<%s> block # %s (%s remaining)' %
(title,index,nblocks-index-1))
print >>io.stdout, block,
sys.stdout.flush() | def show_all(self):
"""Show entire demo on screen, block by block"""
fname = self.title
title = self.title
nblocks = self.nblocks
silent = self._silent
marquee = self.marquee
for index,block in enumerate(self.src_blocks_colored):
if silent[index]:
print >>io.stdout, marquee('<%s> SILENT block # %s (%s remaining)' %
(title,index,nblocks-index-1))
else:
print >>io.stdout, marquee('<%s> block # %s (%s remaining)' %
(title,index,nblocks-index-1))
print >>io.stdout, block,
sys.stdout.flush() | [
"Show",
"entire",
"demo",
"on",
"screen",
"block",
"by",
"block"
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/lib/demo.py#L395-L411 | [
"def",
"show_all",
"(",
"self",
")",
":",
"fname",
"=",
"self",
".",
"title",
"title",
"=",
"self",
".",
"title",
"nblocks",
"=",
"self",
".",
"nblocks",
"silent",
"=",
"self",
".",
"_silent",
"marquee",
"=",
"self",
".",
"marquee",
"for",
"index",
",",
"block",
"in",
"enumerate",
"(",
"self",
".",
"src_blocks_colored",
")",
":",
"if",
"silent",
"[",
"index",
"]",
":",
"print",
">>",
"io",
".",
"stdout",
",",
"marquee",
"(",
"'<%s> SILENT block # %s (%s remaining)'",
"%",
"(",
"title",
",",
"index",
",",
"nblocks",
"-",
"index",
"-",
"1",
")",
")",
"else",
":",
"print",
">>",
"io",
".",
"stdout",
",",
"marquee",
"(",
"'<%s> block # %s (%s remaining)'",
"%",
"(",
"title",
",",
"index",
",",
"nblocks",
"-",
"index",
"-",
"1",
")",
")",
"print",
">>",
"io",
".",
"stdout",
",",
"block",
",",
"sys",
".",
"stdout",
".",
"flush",
"(",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | LineDemo.reload | Reload source from disk and initialize state. | environment/lib/python2.7/site-packages/IPython/lib/demo.py | def reload(self):
"""Reload source from disk and initialize state."""
# read data and parse into blocks
self.fload()
lines = self.fobj.readlines()
src_b = [l for l in lines if l.strip()]
nblocks = len(src_b)
self.src = ''.join(lines)
self._silent = [False]*nblocks
self._auto = [True]*nblocks
self.auto_all = True
self.nblocks = nblocks
self.src_blocks = src_b
# also build syntax-highlighted source
self.src_blocks_colored = map(self.ip_colorize,self.src_blocks)
# ensure clean namespace and seek offset
self.reset() | def reload(self):
"""Reload source from disk and initialize state."""
# read data and parse into blocks
self.fload()
lines = self.fobj.readlines()
src_b = [l for l in lines if l.strip()]
nblocks = len(src_b)
self.src = ''.join(lines)
self._silent = [False]*nblocks
self._auto = [True]*nblocks
self.auto_all = True
self.nblocks = nblocks
self.src_blocks = src_b
# also build syntax-highlighted source
self.src_blocks_colored = map(self.ip_colorize,self.src_blocks)
# ensure clean namespace and seek offset
self.reset() | [
"Reload",
"source",
"from",
"disk",
"and",
"initialize",
"state",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/lib/demo.py#L517-L535 | [
"def",
"reload",
"(",
"self",
")",
":",
"# read data and parse into blocks",
"self",
".",
"fload",
"(",
")",
"lines",
"=",
"self",
".",
"fobj",
".",
"readlines",
"(",
")",
"src_b",
"=",
"[",
"l",
"for",
"l",
"in",
"lines",
"if",
"l",
".",
"strip",
"(",
")",
"]",
"nblocks",
"=",
"len",
"(",
"src_b",
")",
"self",
".",
"src",
"=",
"''",
".",
"join",
"(",
"lines",
")",
"self",
".",
"_silent",
"=",
"[",
"False",
"]",
"*",
"nblocks",
"self",
".",
"_auto",
"=",
"[",
"True",
"]",
"*",
"nblocks",
"self",
".",
"auto_all",
"=",
"True",
"self",
".",
"nblocks",
"=",
"nblocks",
"self",
".",
"src_blocks",
"=",
"src_b",
"# also build syntax-highlighted source",
"self",
".",
"src_blocks_colored",
"=",
"map",
"(",
"self",
".",
"ip_colorize",
",",
"self",
".",
"src_blocks",
")",
"# ensure clean namespace and seek offset",
"self",
".",
"reset",
"(",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | series | Processes a collection in series
Parameters
----------
collection : list
list of Record objects
method : method to call on each Record
prints : int
number of timer prints to the screen
Returns
-------
collection : list
list of Record objects after going through method called
If more than one collection is given, the function is called with an argument list
consisting of the corresponding item of each collection, substituting None for
missing values when not all collection have the same length.
If the function is None, return the original collection (or a list of tuples if multiple collections).
Example
-------
adding 2 to every number in a range
>>> import turntable
>>> collection = range(100)
>>> method = lambda x: x + 2
>>> collection = turntable.spin.series(collection, method) | turntable/spin.py | def series(collection, method, prints = 15, *args, **kwargs):
'''
Processes a collection in series
Parameters
----------
collection : list
list of Record objects
method : method to call on each Record
prints : int
number of timer prints to the screen
Returns
-------
collection : list
list of Record objects after going through method called
If more than one collection is given, the function is called with an argument list
consisting of the corresponding item of each collection, substituting None for
missing values when not all collection have the same length.
If the function is None, return the original collection (or a list of tuples if multiple collections).
Example
-------
adding 2 to every number in a range
>>> import turntable
>>> collection = range(100)
>>> method = lambda x: x + 2
>>> collection = turntable.spin.series(collection, method)
'''
if 'verbose' in kwargs.keys():
verbose = kwargs['verbose']
else:
verbose = True
results = []
timer = turntable.utils.Timer(nLoops=len(collection), numPrints=prints, verbose=verbose)
for subject in collection:
results.append(method(subject, *args, **kwargs))
timer.loop()
timer.fin()
return results | def series(collection, method, prints = 15, *args, **kwargs):
'''
Processes a collection in series
Parameters
----------
collection : list
list of Record objects
method : method to call on each Record
prints : int
number of timer prints to the screen
Returns
-------
collection : list
list of Record objects after going through method called
If more than one collection is given, the function is called with an argument list
consisting of the corresponding item of each collection, substituting None for
missing values when not all collection have the same length.
If the function is None, return the original collection (or a list of tuples if multiple collections).
Example
-------
adding 2 to every number in a range
>>> import turntable
>>> collection = range(100)
>>> method = lambda x: x + 2
>>> collection = turntable.spin.series(collection, method)
'''
if 'verbose' in kwargs.keys():
verbose = kwargs['verbose']
else:
verbose = True
results = []
timer = turntable.utils.Timer(nLoops=len(collection), numPrints=prints, verbose=verbose)
for subject in collection:
results.append(method(subject, *args, **kwargs))
timer.loop()
timer.fin()
return results | [
"Processes",
"a",
"collection",
"in",
"series"
] | jshiv/turntable | python | https://github.com/jshiv/turntable/blob/c095a93df14d672ba54db164a7ab7373444d1829/turntable/spin.py#L18-L62 | [
"def",
"series",
"(",
"collection",
",",
"method",
",",
"prints",
"=",
"15",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"if",
"'verbose'",
"in",
"kwargs",
".",
"keys",
"(",
")",
":",
"verbose",
"=",
"kwargs",
"[",
"'verbose'",
"]",
"else",
":",
"verbose",
"=",
"True",
"results",
"=",
"[",
"]",
"timer",
"=",
"turntable",
".",
"utils",
".",
"Timer",
"(",
"nLoops",
"=",
"len",
"(",
"collection",
")",
",",
"numPrints",
"=",
"prints",
",",
"verbose",
"=",
"verbose",
")",
"for",
"subject",
"in",
"collection",
":",
"results",
".",
"append",
"(",
"method",
"(",
"subject",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
")",
"timer",
".",
"loop",
"(",
")",
"timer",
".",
"fin",
"(",
")",
"return",
"results"
] | c095a93df14d672ba54db164a7ab7373444d1829 |
test | batch | Processes a collection in parallel batches,
each batch processes in series on a single process.
Running batches in parallel can be more effficient that splitting a list across cores as in spin.parallel
because of parallel processing has high IO requirements.
Parameters
----------
collection : list
i.e. list of Record objects
method : method to call on each Record
processes : int
number of processes to run on [defaults to number of cores on machine]
batch_size : int
lenght of each batch [defaults to number of elements / number of processes]
Returns
-------
collection : list
list of Record objects after going through method called
Example
-------
adding 2 to every number in a range
>>> import turntable
>>> collection = range(100)
>>> def jam(record):
>>> return record + 2
>>> collection = turntable.spin.batch(collection, jam)
Note
----
lambda functions do not work in parallel | turntable/spin.py | def batch(collection, method, processes=None, batch_size=None, quiet=False,
kwargs_to_dump=None, args=None, **kwargs):
'''Processes a collection in parallel batches,
each batch processes in series on a single process.
Running batches in parallel can be more effficient that splitting a list across cores as in spin.parallel
because of parallel processing has high IO requirements.
Parameters
----------
collection : list
i.e. list of Record objects
method : method to call on each Record
processes : int
number of processes to run on [defaults to number of cores on machine]
batch_size : int
lenght of each batch [defaults to number of elements / number of processes]
Returns
-------
collection : list
list of Record objects after going through method called
Example
-------
adding 2 to every number in a range
>>> import turntable
>>> collection = range(100)
>>> def jam(record):
>>> return record + 2
>>> collection = turntable.spin.batch(collection, jam)
Note
----
lambda functions do not work in parallel
'''
if processes is None:
# default to the number of processes, not exceeding 20 or the number of
# subjects
processes = min(mp.cpu_count(), 20, len(collection))
if batch_size is None:
# floor divide rounds down to nearest int
batch_size = max(len(collection) // processes, 1)
print 'size of each batch =', batch_size
mod = len(collection) % processes
# batch_list is a list of cars broken in to batch size chunks
batch_list = [collection[x:x + batch_size]
for x in xrange(0, len(collection) - mod, batch_size)]
# remainder handling
if mod != 0:
batch_list[len(batch_list) - 1] += collection[-mod:]
print 'number of batches =', len(batch_list)
# New args
if args is None:
args = method
else:
if isinstance(args, tuple) == False:
args = (args,)
args = (method,) + args
# Applying the mp method w/ or w/o dumping using the custom operator
# method
if kwargs_to_dump is None:
res = parallel(
batch_list,
new_function_batch,
processes=processes,
args=args,
**kwargs)
else:
res = process_dump(
batch_list,
new_function_batch,
kwargs_to_dump,
processes=processes,
args=args,
**kwargs)
returnList = []
for l in res:
returnList += l
# toc = time.time()
# elapsed = toc-tic
# if quiet is False:
# if processes is None:
# print "Total Elapsed time: %s :-)" % str(elapsed)
# else:
# print "Total Elapsed time: %s on %s processes :-)" %
# (str(elapsed),str(processes))
return returnList | def batch(collection, method, processes=None, batch_size=None, quiet=False,
kwargs_to_dump=None, args=None, **kwargs):
'''Processes a collection in parallel batches,
each batch processes in series on a single process.
Running batches in parallel can be more effficient that splitting a list across cores as in spin.parallel
because of parallel processing has high IO requirements.
Parameters
----------
collection : list
i.e. list of Record objects
method : method to call on each Record
processes : int
number of processes to run on [defaults to number of cores on machine]
batch_size : int
lenght of each batch [defaults to number of elements / number of processes]
Returns
-------
collection : list
list of Record objects after going through method called
Example
-------
adding 2 to every number in a range
>>> import turntable
>>> collection = range(100)
>>> def jam(record):
>>> return record + 2
>>> collection = turntable.spin.batch(collection, jam)
Note
----
lambda functions do not work in parallel
'''
if processes is None:
# default to the number of processes, not exceeding 20 or the number of
# subjects
processes = min(mp.cpu_count(), 20, len(collection))
if batch_size is None:
# floor divide rounds down to nearest int
batch_size = max(len(collection) // processes, 1)
print 'size of each batch =', batch_size
mod = len(collection) % processes
# batch_list is a list of cars broken in to batch size chunks
batch_list = [collection[x:x + batch_size]
for x in xrange(0, len(collection) - mod, batch_size)]
# remainder handling
if mod != 0:
batch_list[len(batch_list) - 1] += collection[-mod:]
print 'number of batches =', len(batch_list)
# New args
if args is None:
args = method
else:
if isinstance(args, tuple) == False:
args = (args,)
args = (method,) + args
# Applying the mp method w/ or w/o dumping using the custom operator
# method
if kwargs_to_dump is None:
res = parallel(
batch_list,
new_function_batch,
processes=processes,
args=args,
**kwargs)
else:
res = process_dump(
batch_list,
new_function_batch,
kwargs_to_dump,
processes=processes,
args=args,
**kwargs)
returnList = []
for l in res:
returnList += l
# toc = time.time()
# elapsed = toc-tic
# if quiet is False:
# if processes is None:
# print "Total Elapsed time: %s :-)" % str(elapsed)
# else:
# print "Total Elapsed time: %s on %s processes :-)" %
# (str(elapsed),str(processes))
return returnList | [
"Processes",
"a",
"collection",
"in",
"parallel",
"batches",
"each",
"batch",
"processes",
"in",
"series",
"on",
"a",
"single",
"process",
".",
"Running",
"batches",
"in",
"parallel",
"can",
"be",
"more",
"effficient",
"that",
"splitting",
"a",
"list",
"across",
"cores",
"as",
"in",
"spin",
".",
"parallel",
"because",
"of",
"parallel",
"processing",
"has",
"high",
"IO",
"requirements",
"."
] | jshiv/turntable | python | https://github.com/jshiv/turntable/blob/c095a93df14d672ba54db164a7ab7373444d1829/turntable/spin.py#L64-L161 | [
"def",
"batch",
"(",
"collection",
",",
"method",
",",
"processes",
"=",
"None",
",",
"batch_size",
"=",
"None",
",",
"quiet",
"=",
"False",
",",
"kwargs_to_dump",
"=",
"None",
",",
"args",
"=",
"None",
",",
"*",
"*",
"kwargs",
")",
":",
"if",
"processes",
"is",
"None",
":",
"# default to the number of processes, not exceeding 20 or the number of",
"# subjects",
"processes",
"=",
"min",
"(",
"mp",
".",
"cpu_count",
"(",
")",
",",
"20",
",",
"len",
"(",
"collection",
")",
")",
"if",
"batch_size",
"is",
"None",
":",
"# floor divide rounds down to nearest int",
"batch_size",
"=",
"max",
"(",
"len",
"(",
"collection",
")",
"//",
"processes",
",",
"1",
")",
"print",
"'size of each batch ='",
",",
"batch_size",
"mod",
"=",
"len",
"(",
"collection",
")",
"%",
"processes",
"# batch_list is a list of cars broken in to batch size chunks",
"batch_list",
"=",
"[",
"collection",
"[",
"x",
":",
"x",
"+",
"batch_size",
"]",
"for",
"x",
"in",
"xrange",
"(",
"0",
",",
"len",
"(",
"collection",
")",
"-",
"mod",
",",
"batch_size",
")",
"]",
"# remainder handling",
"if",
"mod",
"!=",
"0",
":",
"batch_list",
"[",
"len",
"(",
"batch_list",
")",
"-",
"1",
"]",
"+=",
"collection",
"[",
"-",
"mod",
":",
"]",
"print",
"'number of batches ='",
",",
"len",
"(",
"batch_list",
")",
"# New args",
"if",
"args",
"is",
"None",
":",
"args",
"=",
"method",
"else",
":",
"if",
"isinstance",
"(",
"args",
",",
"tuple",
")",
"==",
"False",
":",
"args",
"=",
"(",
"args",
",",
")",
"args",
"=",
"(",
"method",
",",
")",
"+",
"args",
"# Applying the mp method w/ or w/o dumping using the custom operator",
"# method",
"if",
"kwargs_to_dump",
"is",
"None",
":",
"res",
"=",
"parallel",
"(",
"batch_list",
",",
"new_function_batch",
",",
"processes",
"=",
"processes",
",",
"args",
"=",
"args",
",",
"*",
"*",
"kwargs",
")",
"else",
":",
"res",
"=",
"process_dump",
"(",
"batch_list",
",",
"new_function_batch",
",",
"kwargs_to_dump",
",",
"processes",
"=",
"processes",
",",
"args",
"=",
"args",
",",
"*",
"*",
"kwargs",
")",
"returnList",
"=",
"[",
"]",
"for",
"l",
"in",
"res",
":",
"returnList",
"+=",
"l",
"# toc = time.time()",
"# elapsed = toc-tic",
"# if quiet is False:",
"# \tif processes is None:",
"# \t\tprint \"Total Elapsed time: %s :-)\" % str(elapsed)",
"# \telse:",
"# print \"Total Elapsed time: %s on %s processes :-)\" %",
"# (str(elapsed),str(processes))",
"return",
"returnList"
] | c095a93df14d672ba54db164a7ab7373444d1829 |
test | thread | sets up the threadpool with map for parallel processing | turntable/spin.py | def thread(function, sequence, cores=None, runSeries=False, quiet=False):
'''sets up the threadpool with map for parallel processing'''
# Make the Pool of workes
if cores is None:
pool = ThreadPool()
else:
pool = ThreadPool(cores)
# Operate on the list of subjects with the requested function
# in the split threads
tic = time.time()
if runSeries is False:
try:
results = pool.map(function, sequence)
# close the pool and wiat for teh work to finish
pool.close()
pool.join()
except:
print 'thread Failed... running in series :-('
results = series(sequence, function)
else:
results = series(sequence, function)
toc = time.time()
elapsed = toc - tic
if quiet is False:
if cores is None:
print "Elapsed time: %s :-)\n" % str(elapsed)
else:
print "Elapsed time: %s on %s threads :-)\n" % (str(elapsed), str(cores))
# Noes:
# import functools
# abc = map(functools.partial(sb.dist, distName = 'weibull'), wbldfList)
return results | def thread(function, sequence, cores=None, runSeries=False, quiet=False):
'''sets up the threadpool with map for parallel processing'''
# Make the Pool of workes
if cores is None:
pool = ThreadPool()
else:
pool = ThreadPool(cores)
# Operate on the list of subjects with the requested function
# in the split threads
tic = time.time()
if runSeries is False:
try:
results = pool.map(function, sequence)
# close the pool and wiat for teh work to finish
pool.close()
pool.join()
except:
print 'thread Failed... running in series :-('
results = series(sequence, function)
else:
results = series(sequence, function)
toc = time.time()
elapsed = toc - tic
if quiet is False:
if cores is None:
print "Elapsed time: %s :-)\n" % str(elapsed)
else:
print "Elapsed time: %s on %s threads :-)\n" % (str(elapsed), str(cores))
# Noes:
# import functools
# abc = map(functools.partial(sb.dist, distName = 'weibull'), wbldfList)
return results | [
"sets",
"up",
"the",
"threadpool",
"with",
"map",
"for",
"parallel",
"processing"
] | jshiv/turntable | python | https://github.com/jshiv/turntable/blob/c095a93df14d672ba54db164a7ab7373444d1829/turntable/spin.py#L188-L223 | [
"def",
"thread",
"(",
"function",
",",
"sequence",
",",
"cores",
"=",
"None",
",",
"runSeries",
"=",
"False",
",",
"quiet",
"=",
"False",
")",
":",
"# Make the Pool of workes",
"if",
"cores",
"is",
"None",
":",
"pool",
"=",
"ThreadPool",
"(",
")",
"else",
":",
"pool",
"=",
"ThreadPool",
"(",
"cores",
")",
"# Operate on the list of subjects with the requested function",
"# in the split threads",
"tic",
"=",
"time",
".",
"time",
"(",
")",
"if",
"runSeries",
"is",
"False",
":",
"try",
":",
"results",
"=",
"pool",
".",
"map",
"(",
"function",
",",
"sequence",
")",
"# close the pool and wiat for teh work to finish",
"pool",
".",
"close",
"(",
")",
"pool",
".",
"join",
"(",
")",
"except",
":",
"print",
"'thread Failed... running in series :-('",
"results",
"=",
"series",
"(",
"sequence",
",",
"function",
")",
"else",
":",
"results",
"=",
"series",
"(",
"sequence",
",",
"function",
")",
"toc",
"=",
"time",
".",
"time",
"(",
")",
"elapsed",
"=",
"toc",
"-",
"tic",
"if",
"quiet",
"is",
"False",
":",
"if",
"cores",
"is",
"None",
":",
"print",
"\"Elapsed time: %s :-)\\n\"",
"%",
"str",
"(",
"elapsed",
")",
"else",
":",
"print",
"\"Elapsed time: %s on %s threads :-)\\n\"",
"%",
"(",
"str",
"(",
"elapsed",
")",
",",
"str",
"(",
"cores",
")",
")",
"# Noes:",
"# import functools",
"# abc = map(functools.partial(sb.dist, distName = 'weibull'), wbldfList)",
"return",
"results"
] | c095a93df14d672ba54db164a7ab7373444d1829 |
test | parallel | Processes a collection in parallel.
Parameters
----------
collection : list
i.e. list of Record objects
method : method to call on each Record
processes : int
number of processes to run on [defaults to number of cores on machine]
batch_size : int
lenght of each batch [defaults to number of elements / number of processes]
Returns
-------
collection : list
list of Record objects after going through method called
Example
-------
adding 2 to every number in a range
>>> import turntable
>>> collection = range(100)
>>> def jam(record):
>>> return record + 2
>>> collection = turntable.spin.parallel(collection, jam)
Note
----
lambda functions do not work in parallel | turntable/spin.py | def parallel(collection, method, processes=None, args=None, **kwargs):
'''Processes a collection in parallel.
Parameters
----------
collection : list
i.e. list of Record objects
method : method to call on each Record
processes : int
number of processes to run on [defaults to number of cores on machine]
batch_size : int
lenght of each batch [defaults to number of elements / number of processes]
Returns
-------
collection : list
list of Record objects after going through method called
Example
-------
adding 2 to every number in a range
>>> import turntable
>>> collection = range(100)
>>> def jam(record):
>>> return record + 2
>>> collection = turntable.spin.parallel(collection, jam)
Note
----
lambda functions do not work in parallel
'''
if processes is None:
# default to the number of cores, not exceeding 20
processes = min(mp.cpu_count(), 20)
print "Running parallel process on " + str(processes) + " cores. :-)"
pool = mp.Pool(processes=processes)
PROC = []
tic = time.time()
for main_arg in collection:
if args is None:
ARGS = (main_arg,)
else:
if isinstance(args, tuple) == False:
args = (args,)
ARGS = (main_arg,) + args
PROC.append(pool.apply_async(method, args=ARGS, kwds=kwargs))
#RES = [p.get() for p in PROC]
RES = []
for p in PROC:
try:
RES.append(p.get())
except Exception as e:
print "shit happens..."
print e
RES.append(None)
pool.close()
pool.join()
toc = time.time()
elapsed = toc - tic
print "Elapsed time: %s on %s processes :-)\n" % (str(elapsed), str(processes))
return RES | def parallel(collection, method, processes=None, args=None, **kwargs):
'''Processes a collection in parallel.
Parameters
----------
collection : list
i.e. list of Record objects
method : method to call on each Record
processes : int
number of processes to run on [defaults to number of cores on machine]
batch_size : int
lenght of each batch [defaults to number of elements / number of processes]
Returns
-------
collection : list
list of Record objects after going through method called
Example
-------
adding 2 to every number in a range
>>> import turntable
>>> collection = range(100)
>>> def jam(record):
>>> return record + 2
>>> collection = turntable.spin.parallel(collection, jam)
Note
----
lambda functions do not work in parallel
'''
if processes is None:
# default to the number of cores, not exceeding 20
processes = min(mp.cpu_count(), 20)
print "Running parallel process on " + str(processes) + " cores. :-)"
pool = mp.Pool(processes=processes)
PROC = []
tic = time.time()
for main_arg in collection:
if args is None:
ARGS = (main_arg,)
else:
if isinstance(args, tuple) == False:
args = (args,)
ARGS = (main_arg,) + args
PROC.append(pool.apply_async(method, args=ARGS, kwds=kwargs))
#RES = [p.get() for p in PROC]
RES = []
for p in PROC:
try:
RES.append(p.get())
except Exception as e:
print "shit happens..."
print e
RES.append(None)
pool.close()
pool.join()
toc = time.time()
elapsed = toc - tic
print "Elapsed time: %s on %s processes :-)\n" % (str(elapsed), str(processes))
return RES | [
"Processes",
"a",
"collection",
"in",
"parallel",
"."
] | jshiv/turntable | python | https://github.com/jshiv/turntable/blob/c095a93df14d672ba54db164a7ab7373444d1829/turntable/spin.py#L225-L293 | [
"def",
"parallel",
"(",
"collection",
",",
"method",
",",
"processes",
"=",
"None",
",",
"args",
"=",
"None",
",",
"*",
"*",
"kwargs",
")",
":",
"if",
"processes",
"is",
"None",
":",
"# default to the number of cores, not exceeding 20",
"processes",
"=",
"min",
"(",
"mp",
".",
"cpu_count",
"(",
")",
",",
"20",
")",
"print",
"\"Running parallel process on \"",
"+",
"str",
"(",
"processes",
")",
"+",
"\" cores. :-)\"",
"pool",
"=",
"mp",
".",
"Pool",
"(",
"processes",
"=",
"processes",
")",
"PROC",
"=",
"[",
"]",
"tic",
"=",
"time",
".",
"time",
"(",
")",
"for",
"main_arg",
"in",
"collection",
":",
"if",
"args",
"is",
"None",
":",
"ARGS",
"=",
"(",
"main_arg",
",",
")",
"else",
":",
"if",
"isinstance",
"(",
"args",
",",
"tuple",
")",
"==",
"False",
":",
"args",
"=",
"(",
"args",
",",
")",
"ARGS",
"=",
"(",
"main_arg",
",",
")",
"+",
"args",
"PROC",
".",
"append",
"(",
"pool",
".",
"apply_async",
"(",
"method",
",",
"args",
"=",
"ARGS",
",",
"kwds",
"=",
"kwargs",
")",
")",
"#RES = [p.get() for p in PROC]",
"RES",
"=",
"[",
"]",
"for",
"p",
"in",
"PROC",
":",
"try",
":",
"RES",
".",
"append",
"(",
"p",
".",
"get",
"(",
")",
")",
"except",
"Exception",
"as",
"e",
":",
"print",
"\"shit happens...\"",
"print",
"e",
"RES",
".",
"append",
"(",
"None",
")",
"pool",
".",
"close",
"(",
")",
"pool",
".",
"join",
"(",
")",
"toc",
"=",
"time",
".",
"time",
"(",
")",
"elapsed",
"=",
"toc",
"-",
"tic",
"print",
"\"Elapsed time: %s on %s processes :-)\\n\"",
"%",
"(",
"str",
"(",
"elapsed",
")",
",",
"str",
"(",
"processes",
")",
")",
"return",
"RES"
] | c095a93df14d672ba54db164a7ab7373444d1829 |
test | install_mathjax | Download and install MathJax for offline use.
This will install mathjax to the 'static' dir in the IPython notebook
package, so it will fail if the caller does not have write access
to that location.
MathJax is a ~15MB download, and ~150MB installed.
Parameters
----------
replace : bool [False]
Whether to remove and replace an existing install.
tag : str ['v1.1']
Which tag to download. Default is 'v1.1', the current stable release,
but alternatives include 'v1.1a' and 'master'. | environment/lib/python2.7/site-packages/IPython/external/mathjax.py | def install_mathjax(tag='v1.1', replace=False):
"""Download and install MathJax for offline use.
This will install mathjax to the 'static' dir in the IPython notebook
package, so it will fail if the caller does not have write access
to that location.
MathJax is a ~15MB download, and ~150MB installed.
Parameters
----------
replace : bool [False]
Whether to remove and replace an existing install.
tag : str ['v1.1']
Which tag to download. Default is 'v1.1', the current stable release,
but alternatives include 'v1.1a' and 'master'.
"""
mathjax_url = "https://github.com/mathjax/MathJax/tarball/%s"%tag
nbdir = os.path.dirname(os.path.abspath(nbmod.__file__))
static = os.path.join(nbdir, 'static')
dest = os.path.join(static, 'mathjax')
# check for existence and permissions
if not os.access(static, os.W_OK):
raise IOError("Need have write access to %s"%static)
if os.path.exists(dest):
if replace:
if not os.access(dest, os.W_OK):
raise IOError("Need have write access to %s"%dest)
print "removing previous MathJax install"
shutil.rmtree(dest)
else:
print "offline MathJax apparently already installed"
return
# download mathjax
print "Downloading mathjax source..."
response = urllib2.urlopen(mathjax_url)
print "done"
# use 'r|gz' stream mode, because socket file-like objects can't seek:
tar = tarfile.open(fileobj=response.fp, mode='r|gz')
topdir = tar.firstmember.path
print "Extracting to %s"%dest
tar.extractall(static)
# it will be mathjax-MathJax-<sha>, rename to just mathjax
os.rename(os.path.join(static, topdir), dest) | def install_mathjax(tag='v1.1', replace=False):
"""Download and install MathJax for offline use.
This will install mathjax to the 'static' dir in the IPython notebook
package, so it will fail if the caller does not have write access
to that location.
MathJax is a ~15MB download, and ~150MB installed.
Parameters
----------
replace : bool [False]
Whether to remove and replace an existing install.
tag : str ['v1.1']
Which tag to download. Default is 'v1.1', the current stable release,
but alternatives include 'v1.1a' and 'master'.
"""
mathjax_url = "https://github.com/mathjax/MathJax/tarball/%s"%tag
nbdir = os.path.dirname(os.path.abspath(nbmod.__file__))
static = os.path.join(nbdir, 'static')
dest = os.path.join(static, 'mathjax')
# check for existence and permissions
if not os.access(static, os.W_OK):
raise IOError("Need have write access to %s"%static)
if os.path.exists(dest):
if replace:
if not os.access(dest, os.W_OK):
raise IOError("Need have write access to %s"%dest)
print "removing previous MathJax install"
shutil.rmtree(dest)
else:
print "offline MathJax apparently already installed"
return
# download mathjax
print "Downloading mathjax source..."
response = urllib2.urlopen(mathjax_url)
print "done"
# use 'r|gz' stream mode, because socket file-like objects can't seek:
tar = tarfile.open(fileobj=response.fp, mode='r|gz')
topdir = tar.firstmember.path
print "Extracting to %s"%dest
tar.extractall(static)
# it will be mathjax-MathJax-<sha>, rename to just mathjax
os.rename(os.path.join(static, topdir), dest) | [
"Download",
"and",
"install",
"MathJax",
"for",
"offline",
"use",
".",
"This",
"will",
"install",
"mathjax",
"to",
"the",
"static",
"dir",
"in",
"the",
"IPython",
"notebook",
"package",
"so",
"it",
"will",
"fail",
"if",
"the",
"caller",
"does",
"not",
"have",
"write",
"access",
"to",
"that",
"location",
".",
"MathJax",
"is",
"a",
"~15MB",
"download",
"and",
"~150MB",
"installed",
".",
"Parameters",
"----------",
"replace",
":",
"bool",
"[",
"False",
"]",
"Whether",
"to",
"remove",
"and",
"replace",
"an",
"existing",
"install",
".",
"tag",
":",
"str",
"[",
"v1",
".",
"1",
"]",
"Which",
"tag",
"to",
"download",
".",
"Default",
"is",
"v1",
".",
"1",
"the",
"current",
"stable",
"release",
"but",
"alternatives",
"include",
"v1",
".",
"1a",
"and",
"master",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/external/mathjax.py#L32-L79 | [
"def",
"install_mathjax",
"(",
"tag",
"=",
"'v1.1'",
",",
"replace",
"=",
"False",
")",
":",
"mathjax_url",
"=",
"\"https://github.com/mathjax/MathJax/tarball/%s\"",
"%",
"tag",
"nbdir",
"=",
"os",
".",
"path",
".",
"dirname",
"(",
"os",
".",
"path",
".",
"abspath",
"(",
"nbmod",
".",
"__file__",
")",
")",
"static",
"=",
"os",
".",
"path",
".",
"join",
"(",
"nbdir",
",",
"'static'",
")",
"dest",
"=",
"os",
".",
"path",
".",
"join",
"(",
"static",
",",
"'mathjax'",
")",
"# check for existence and permissions",
"if",
"not",
"os",
".",
"access",
"(",
"static",
",",
"os",
".",
"W_OK",
")",
":",
"raise",
"IOError",
"(",
"\"Need have write access to %s\"",
"%",
"static",
")",
"if",
"os",
".",
"path",
".",
"exists",
"(",
"dest",
")",
":",
"if",
"replace",
":",
"if",
"not",
"os",
".",
"access",
"(",
"dest",
",",
"os",
".",
"W_OK",
")",
":",
"raise",
"IOError",
"(",
"\"Need have write access to %s\"",
"%",
"dest",
")",
"print",
"\"removing previous MathJax install\"",
"shutil",
".",
"rmtree",
"(",
"dest",
")",
"else",
":",
"print",
"\"offline MathJax apparently already installed\"",
"return",
"# download mathjax",
"print",
"\"Downloading mathjax source...\"",
"response",
"=",
"urllib2",
".",
"urlopen",
"(",
"mathjax_url",
")",
"print",
"\"done\"",
"# use 'r|gz' stream mode, because socket file-like objects can't seek:",
"tar",
"=",
"tarfile",
".",
"open",
"(",
"fileobj",
"=",
"response",
".",
"fp",
",",
"mode",
"=",
"'r|gz'",
")",
"topdir",
"=",
"tar",
".",
"firstmember",
".",
"path",
"print",
"\"Extracting to %s\"",
"%",
"dest",
"tar",
".",
"extractall",
"(",
"static",
")",
"# it will be mathjax-MathJax-<sha>, rename to just mathjax",
"os",
".",
"rename",
"(",
"os",
".",
"path",
".",
"join",
"(",
"static",
",",
"topdir",
")",
",",
"dest",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | with_it | wrap `with obj` out of func.
example:
``` py
@with_it(Lock())
def func(): pass
``` | jasily/lang/with_any.py | def with_it(obj):
'''
wrap `with obj` out of func.
example:
``` py
@with_it(Lock())
def func(): pass
```
'''
def _wrap(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
with obj:
return func(*args, **kwargs)
return wrapper
return _wrap | def with_it(obj):
'''
wrap `with obj` out of func.
example:
``` py
@with_it(Lock())
def func(): pass
```
'''
def _wrap(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
with obj:
return func(*args, **kwargs)
return wrapper
return _wrap | [
"wrap",
"with",
"obj",
"out",
"of",
"func",
"."
] | Jasily/jasily-python | python | https://github.com/Jasily/jasily-python/blob/1c821a120ebbbbc3c5761f5f1e8a73588059242a/jasily/lang/with_any.py#L12-L33 | [
"def",
"with_it",
"(",
"obj",
")",
":",
"def",
"_wrap",
"(",
"func",
")",
":",
"@",
"functools",
".",
"wraps",
"(",
"func",
")",
"def",
"wrapper",
"(",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"with",
"obj",
":",
"return",
"func",
"(",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
"return",
"wrapper",
"return",
"_wrap"
] | 1c821a120ebbbbc3c5761f5f1e8a73588059242a |
test | with_objattr | wrap `with getattr(self, name)` out of func.
usage:
``` py
class A:
def __init__(self):
self._lock = RLock()
@with_objattr('_lock') # so easy to make a sync instance method !
def func():
pass
``` | jasily/lang/with_any.py | def with_objattr(name):
'''
wrap `with getattr(self, name)` out of func.
usage:
``` py
class A:
def __init__(self):
self._lock = RLock()
@with_objattr('_lock') # so easy to make a sync instance method !
def func():
pass
```
'''
def _wrap(func):
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
with getattr(self, name):
return func(self, *args, **kwargs)
return wrapper
return _wrap | def with_objattr(name):
'''
wrap `with getattr(self, name)` out of func.
usage:
``` py
class A:
def __init__(self):
self._lock = RLock()
@with_objattr('_lock') # so easy to make a sync instance method !
def func():
pass
```
'''
def _wrap(func):
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
with getattr(self, name):
return func(self, *args, **kwargs)
return wrapper
return _wrap | [
"wrap",
"with",
"getattr",
"(",
"self",
"name",
")",
"out",
"of",
"func",
"."
] | Jasily/jasily-python | python | https://github.com/Jasily/jasily-python/blob/1c821a120ebbbbc3c5761f5f1e8a73588059242a/jasily/lang/with_any.py#L35-L61 | [
"def",
"with_objattr",
"(",
"name",
")",
":",
"def",
"_wrap",
"(",
"func",
")",
":",
"@",
"functools",
".",
"wraps",
"(",
"func",
")",
"def",
"wrapper",
"(",
"self",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"with",
"getattr",
"(",
"self",
",",
"name",
")",
":",
"return",
"func",
"(",
"self",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
"return",
"wrapper",
"return",
"_wrap"
] | 1c821a120ebbbbc3c5761f5f1e8a73588059242a |
test | with_objattrs | like `with_objattr` but enter context one by one. | jasily/lang/with_any.py | def with_objattrs(*names):
'''
like `with_objattr` but enter context one by one.
'''
def _wrap(func):
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
with contextlib.ExitStack() as stack:
for name in names:
stack.enter_context(getattr(self, name))
return func(self, *args, **kwargs)
return wrapper
return _wrap | def with_objattrs(*names):
'''
like `with_objattr` but enter context one by one.
'''
def _wrap(func):
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
with contextlib.ExitStack() as stack:
for name in names:
stack.enter_context(getattr(self, name))
return func(self, *args, **kwargs)
return wrapper
return _wrap | [
"like",
"with_objattr",
"but",
"enter",
"context",
"one",
"by",
"one",
"."
] | Jasily/jasily-python | python | https://github.com/Jasily/jasily-python/blob/1c821a120ebbbbc3c5761f5f1e8a73588059242a/jasily/lang/with_any.py#L63-L79 | [
"def",
"with_objattrs",
"(",
"*",
"names",
")",
":",
"def",
"_wrap",
"(",
"func",
")",
":",
"@",
"functools",
".",
"wraps",
"(",
"func",
")",
"def",
"wrapper",
"(",
"self",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"with",
"contextlib",
".",
"ExitStack",
"(",
")",
"as",
"stack",
":",
"for",
"name",
"in",
"names",
":",
"stack",
".",
"enter_context",
"(",
"getattr",
"(",
"self",
",",
"name",
")",
")",
"return",
"func",
"(",
"self",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
"return",
"wrapper",
"return",
"_wrap"
] | 1c821a120ebbbbc3c5761f5f1e8a73588059242a |
test | inspect_traceback | Inspect a traceback and its frame, returning source for the expression
where the exception was raised, with simple variable replacement performed
and the line on which the exception was raised marked with '>>' | environment/lib/python2.7/site-packages/nose/inspector.py | def inspect_traceback(tb):
"""Inspect a traceback and its frame, returning source for the expression
where the exception was raised, with simple variable replacement performed
and the line on which the exception was raised marked with '>>'
"""
log.debug('inspect traceback %s', tb)
# we only want the innermost frame, where the exception was raised
while tb.tb_next:
tb = tb.tb_next
frame = tb.tb_frame
lines, exc_line = tbsource(tb)
# figure out the set of lines to grab.
inspect_lines, mark_line = find_inspectable_lines(lines, exc_line)
src = StringIO(textwrap.dedent(''.join(inspect_lines)))
exp = Expander(frame.f_locals, frame.f_globals)
while inspect_lines:
try:
for tok in tokenize.generate_tokens(src.readline):
exp(*tok)
except tokenize.TokenError, e:
# this can happen if our inspectable region happens to butt up
# against the end of a construct like a docstring with the closing
# """ on separate line
log.debug("Tokenizer error: %s", e)
inspect_lines.pop(0)
mark_line -= 1
src = StringIO(textwrap.dedent(''.join(inspect_lines)))
exp = Expander(frame.f_locals, frame.f_globals)
continue
break
padded = []
if exp.expanded_source:
exp_lines = exp.expanded_source.split('\n')
ep = 0
for line in exp_lines:
if ep == mark_line:
padded.append('>> ' + line)
else:
padded.append(' ' + line)
ep += 1
return '\n'.join(padded) | def inspect_traceback(tb):
"""Inspect a traceback and its frame, returning source for the expression
where the exception was raised, with simple variable replacement performed
and the line on which the exception was raised marked with '>>'
"""
log.debug('inspect traceback %s', tb)
# we only want the innermost frame, where the exception was raised
while tb.tb_next:
tb = tb.tb_next
frame = tb.tb_frame
lines, exc_line = tbsource(tb)
# figure out the set of lines to grab.
inspect_lines, mark_line = find_inspectable_lines(lines, exc_line)
src = StringIO(textwrap.dedent(''.join(inspect_lines)))
exp = Expander(frame.f_locals, frame.f_globals)
while inspect_lines:
try:
for tok in tokenize.generate_tokens(src.readline):
exp(*tok)
except tokenize.TokenError, e:
# this can happen if our inspectable region happens to butt up
# against the end of a construct like a docstring with the closing
# """ on separate line
log.debug("Tokenizer error: %s", e)
inspect_lines.pop(0)
mark_line -= 1
src = StringIO(textwrap.dedent(''.join(inspect_lines)))
exp = Expander(frame.f_locals, frame.f_globals)
continue
break
padded = []
if exp.expanded_source:
exp_lines = exp.expanded_source.split('\n')
ep = 0
for line in exp_lines:
if ep == mark_line:
padded.append('>> ' + line)
else:
padded.append(' ' + line)
ep += 1
return '\n'.join(padded) | [
"Inspect",
"a",
"traceback",
"and",
"its",
"frame",
"returning",
"source",
"for",
"the",
"expression",
"where",
"the",
"exception",
"was",
"raised",
"with",
"simple",
"variable",
"replacement",
"performed",
"and",
"the",
"line",
"on",
"which",
"the",
"exception",
"was",
"raised",
"marked",
"with",
">>"
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/nose/inspector.py#L18-L62 | [
"def",
"inspect_traceback",
"(",
"tb",
")",
":",
"log",
".",
"debug",
"(",
"'inspect traceback %s'",
",",
"tb",
")",
"# we only want the innermost frame, where the exception was raised",
"while",
"tb",
".",
"tb_next",
":",
"tb",
"=",
"tb",
".",
"tb_next",
"frame",
"=",
"tb",
".",
"tb_frame",
"lines",
",",
"exc_line",
"=",
"tbsource",
"(",
"tb",
")",
"# figure out the set of lines to grab.",
"inspect_lines",
",",
"mark_line",
"=",
"find_inspectable_lines",
"(",
"lines",
",",
"exc_line",
")",
"src",
"=",
"StringIO",
"(",
"textwrap",
".",
"dedent",
"(",
"''",
".",
"join",
"(",
"inspect_lines",
")",
")",
")",
"exp",
"=",
"Expander",
"(",
"frame",
".",
"f_locals",
",",
"frame",
".",
"f_globals",
")",
"while",
"inspect_lines",
":",
"try",
":",
"for",
"tok",
"in",
"tokenize",
".",
"generate_tokens",
"(",
"src",
".",
"readline",
")",
":",
"exp",
"(",
"*",
"tok",
")",
"except",
"tokenize",
".",
"TokenError",
",",
"e",
":",
"# this can happen if our inspectable region happens to butt up",
"# against the end of a construct like a docstring with the closing",
"# \"\"\" on separate line",
"log",
".",
"debug",
"(",
"\"Tokenizer error: %s\"",
",",
"e",
")",
"inspect_lines",
".",
"pop",
"(",
"0",
")",
"mark_line",
"-=",
"1",
"src",
"=",
"StringIO",
"(",
"textwrap",
".",
"dedent",
"(",
"''",
".",
"join",
"(",
"inspect_lines",
")",
")",
")",
"exp",
"=",
"Expander",
"(",
"frame",
".",
"f_locals",
",",
"frame",
".",
"f_globals",
")",
"continue",
"break",
"padded",
"=",
"[",
"]",
"if",
"exp",
".",
"expanded_source",
":",
"exp_lines",
"=",
"exp",
".",
"expanded_source",
".",
"split",
"(",
"'\\n'",
")",
"ep",
"=",
"0",
"for",
"line",
"in",
"exp_lines",
":",
"if",
"ep",
"==",
"mark_line",
":",
"padded",
".",
"append",
"(",
"'>> '",
"+",
"line",
")",
"else",
":",
"padded",
".",
"append",
"(",
"' '",
"+",
"line",
")",
"ep",
"+=",
"1",
"return",
"'\\n'",
".",
"join",
"(",
"padded",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | tbsource | Get source from a traceback object.
A tuple of two things is returned: a list of lines of context from
the source code, and the index of the current line within that list.
The optional second argument specifies the number of lines of context
to return, which are centered around the current line.
.. Note ::
This is adapted from inspect.py in the python 2.4 standard library,
since a bug in the 2.3 version of inspect prevents it from correctly
locating source lines in a traceback frame. | environment/lib/python2.7/site-packages/nose/inspector.py | def tbsource(tb, context=6):
"""Get source from a traceback object.
A tuple of two things is returned: a list of lines of context from
the source code, and the index of the current line within that list.
The optional second argument specifies the number of lines of context
to return, which are centered around the current line.
.. Note ::
This is adapted from inspect.py in the python 2.4 standard library,
since a bug in the 2.3 version of inspect prevents it from correctly
locating source lines in a traceback frame.
"""
lineno = tb.tb_lineno
frame = tb.tb_frame
if context > 0:
start = lineno - 1 - context//2
log.debug("lineno: %s start: %s", lineno, start)
try:
lines, dummy = inspect.findsource(frame)
except IOError:
lines, index = [''], 0
else:
all_lines = lines
start = max(start, 1)
start = max(0, min(start, len(lines) - context))
lines = lines[start:start+context]
index = lineno - 1 - start
# python 2.5 compat: if previous line ends in a continuation,
# decrement start by 1 to match 2.4 behavior
if sys.version_info >= (2, 5) and index > 0:
while lines[index-1].strip().endswith('\\'):
start -= 1
lines = all_lines[start:start+context]
else:
lines, index = [''], 0
log.debug("tbsource lines '''%s''' around index %s", lines, index)
return (lines, index) | def tbsource(tb, context=6):
"""Get source from a traceback object.
A tuple of two things is returned: a list of lines of context from
the source code, and the index of the current line within that list.
The optional second argument specifies the number of lines of context
to return, which are centered around the current line.
.. Note ::
This is adapted from inspect.py in the python 2.4 standard library,
since a bug in the 2.3 version of inspect prevents it from correctly
locating source lines in a traceback frame.
"""
lineno = tb.tb_lineno
frame = tb.tb_frame
if context > 0:
start = lineno - 1 - context//2
log.debug("lineno: %s start: %s", lineno, start)
try:
lines, dummy = inspect.findsource(frame)
except IOError:
lines, index = [''], 0
else:
all_lines = lines
start = max(start, 1)
start = max(0, min(start, len(lines) - context))
lines = lines[start:start+context]
index = lineno - 1 - start
# python 2.5 compat: if previous line ends in a continuation,
# decrement start by 1 to match 2.4 behavior
if sys.version_info >= (2, 5) and index > 0:
while lines[index-1].strip().endswith('\\'):
start -= 1
lines = all_lines[start:start+context]
else:
lines, index = [''], 0
log.debug("tbsource lines '''%s''' around index %s", lines, index)
return (lines, index) | [
"Get",
"source",
"from",
"a",
"traceback",
"object",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/nose/inspector.py#L65-L106 | [
"def",
"tbsource",
"(",
"tb",
",",
"context",
"=",
"6",
")",
":",
"lineno",
"=",
"tb",
".",
"tb_lineno",
"frame",
"=",
"tb",
".",
"tb_frame",
"if",
"context",
">",
"0",
":",
"start",
"=",
"lineno",
"-",
"1",
"-",
"context",
"//",
"2",
"log",
".",
"debug",
"(",
"\"lineno: %s start: %s\"",
",",
"lineno",
",",
"start",
")",
"try",
":",
"lines",
",",
"dummy",
"=",
"inspect",
".",
"findsource",
"(",
"frame",
")",
"except",
"IOError",
":",
"lines",
",",
"index",
"=",
"[",
"''",
"]",
",",
"0",
"else",
":",
"all_lines",
"=",
"lines",
"start",
"=",
"max",
"(",
"start",
",",
"1",
")",
"start",
"=",
"max",
"(",
"0",
",",
"min",
"(",
"start",
",",
"len",
"(",
"lines",
")",
"-",
"context",
")",
")",
"lines",
"=",
"lines",
"[",
"start",
":",
"start",
"+",
"context",
"]",
"index",
"=",
"lineno",
"-",
"1",
"-",
"start",
"# python 2.5 compat: if previous line ends in a continuation,",
"# decrement start by 1 to match 2.4 behavior ",
"if",
"sys",
".",
"version_info",
">=",
"(",
"2",
",",
"5",
")",
"and",
"index",
">",
"0",
":",
"while",
"lines",
"[",
"index",
"-",
"1",
"]",
".",
"strip",
"(",
")",
".",
"endswith",
"(",
"'\\\\'",
")",
":",
"start",
"-=",
"1",
"lines",
"=",
"all_lines",
"[",
"start",
":",
"start",
"+",
"context",
"]",
"else",
":",
"lines",
",",
"index",
"=",
"[",
"''",
"]",
",",
"0",
"log",
".",
"debug",
"(",
"\"tbsource lines '''%s''' around index %s\"",
",",
"lines",
",",
"index",
")",
"return",
"(",
"lines",
",",
"index",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | find_inspectable_lines | Find lines in home that are inspectable.
Walk back from the err line up to 3 lines, but don't walk back over
changes in indent level.
Walk forward up to 3 lines, counting \ separated lines as 1. Don't walk
over changes in indent level (unless part of an extended line) | environment/lib/python2.7/site-packages/nose/inspector.py | def find_inspectable_lines(lines, pos):
"""Find lines in home that are inspectable.
Walk back from the err line up to 3 lines, but don't walk back over
changes in indent level.
Walk forward up to 3 lines, counting \ separated lines as 1. Don't walk
over changes in indent level (unless part of an extended line)
"""
cnt = re.compile(r'\\[\s\n]*$')
df = re.compile(r':[\s\n]*$')
ind = re.compile(r'^(\s*)')
toinspect = []
home = lines[pos]
home_indent = ind.match(home).groups()[0]
before = lines[max(pos-3, 0):pos]
before.reverse()
after = lines[pos+1:min(pos+4, len(lines))]
for line in before:
if ind.match(line).groups()[0] == home_indent:
toinspect.append(line)
else:
break
toinspect.reverse()
toinspect.append(home)
home_pos = len(toinspect)-1
continued = cnt.search(home)
for line in after:
if ((continued or ind.match(line).groups()[0] == home_indent)
and not df.search(line)):
toinspect.append(line)
continued = cnt.search(line)
else:
break
log.debug("Inspecting lines '''%s''' around %s", toinspect, home_pos)
return toinspect, home_pos | def find_inspectable_lines(lines, pos):
"""Find lines in home that are inspectable.
Walk back from the err line up to 3 lines, but don't walk back over
changes in indent level.
Walk forward up to 3 lines, counting \ separated lines as 1. Don't walk
over changes in indent level (unless part of an extended line)
"""
cnt = re.compile(r'\\[\s\n]*$')
df = re.compile(r':[\s\n]*$')
ind = re.compile(r'^(\s*)')
toinspect = []
home = lines[pos]
home_indent = ind.match(home).groups()[0]
before = lines[max(pos-3, 0):pos]
before.reverse()
after = lines[pos+1:min(pos+4, len(lines))]
for line in before:
if ind.match(line).groups()[0] == home_indent:
toinspect.append(line)
else:
break
toinspect.reverse()
toinspect.append(home)
home_pos = len(toinspect)-1
continued = cnt.search(home)
for line in after:
if ((continued or ind.match(line).groups()[0] == home_indent)
and not df.search(line)):
toinspect.append(line)
continued = cnt.search(line)
else:
break
log.debug("Inspecting lines '''%s''' around %s", toinspect, home_pos)
return toinspect, home_pos | [
"Find",
"lines",
"in",
"home",
"that",
"are",
"inspectable",
".",
"Walk",
"back",
"from",
"the",
"err",
"line",
"up",
"to",
"3",
"lines",
"but",
"don",
"t",
"walk",
"back",
"over",
"changes",
"in",
"indent",
"level",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/nose/inspector.py#L109-L146 | [
"def",
"find_inspectable_lines",
"(",
"lines",
",",
"pos",
")",
":",
"cnt",
"=",
"re",
".",
"compile",
"(",
"r'\\\\[\\s\\n]*$'",
")",
"df",
"=",
"re",
".",
"compile",
"(",
"r':[\\s\\n]*$'",
")",
"ind",
"=",
"re",
".",
"compile",
"(",
"r'^(\\s*)'",
")",
"toinspect",
"=",
"[",
"]",
"home",
"=",
"lines",
"[",
"pos",
"]",
"home_indent",
"=",
"ind",
".",
"match",
"(",
"home",
")",
".",
"groups",
"(",
")",
"[",
"0",
"]",
"before",
"=",
"lines",
"[",
"max",
"(",
"pos",
"-",
"3",
",",
"0",
")",
":",
"pos",
"]",
"before",
".",
"reverse",
"(",
")",
"after",
"=",
"lines",
"[",
"pos",
"+",
"1",
":",
"min",
"(",
"pos",
"+",
"4",
",",
"len",
"(",
"lines",
")",
")",
"]",
"for",
"line",
"in",
"before",
":",
"if",
"ind",
".",
"match",
"(",
"line",
")",
".",
"groups",
"(",
")",
"[",
"0",
"]",
"==",
"home_indent",
":",
"toinspect",
".",
"append",
"(",
"line",
")",
"else",
":",
"break",
"toinspect",
".",
"reverse",
"(",
")",
"toinspect",
".",
"append",
"(",
"home",
")",
"home_pos",
"=",
"len",
"(",
"toinspect",
")",
"-",
"1",
"continued",
"=",
"cnt",
".",
"search",
"(",
"home",
")",
"for",
"line",
"in",
"after",
":",
"if",
"(",
"(",
"continued",
"or",
"ind",
".",
"match",
"(",
"line",
")",
".",
"groups",
"(",
")",
"[",
"0",
"]",
"==",
"home_indent",
")",
"and",
"not",
"df",
".",
"search",
"(",
"line",
")",
")",
":",
"toinspect",
".",
"append",
"(",
"line",
")",
"continued",
"=",
"cnt",
".",
"search",
"(",
"line",
")",
"else",
":",
"break",
"log",
".",
"debug",
"(",
"\"Inspecting lines '''%s''' around %s\"",
",",
"toinspect",
",",
"home_pos",
")",
"return",
"toinspect",
",",
"home_pos"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | countdown | Create a countdown. | django_baseline/templatetags/countdownbox.py | def countdown(name, date, description='', id='', granularity='sec', start=None,
progressbar=False, progressbar_inversed=False, showpct=False):
'''
Create a countdown.
'''
end_date = dateparse.parse_datetime(date)
end = dateformat.format(end_date, 'U')
content = '<div class="name">' + name + '</div>'
content += '<div class="description">' + description + '</div>'
if progressbar:
if not end: raise Exception('For progressbar, start date is requried.')
parsed_date = datetime.datetime.combine(
dateparse.parse_date(start), datetime.time())
start_date = dateparse.parse_datetime(start) or parsed_date
now = datetime.datetime.now()
pct = (now - start_date).total_seconds() /\
(end_date - start_date).total_seconds()
pct = int(pct * 100)
if progressbar_inversed:
pct = 100 - pct
# Note: the output is for bootstrap!
bar = '<div class="progress progress-striped active">'
bar += '<div class="progress-bar" role="progressbar" aria-valuenow="{pct}" aria-valuemin="0" aria-valuemax="100" style="width: {pct}%">'
bar += '<span class="sr-only">{pct}% Complete</span>'
bar += '</div>'
bar += '</div>'
if showpct:
bar += '<div class="percentage">{pct}%</div>'
bar = bar.format(pct=pct)
content += bar
content += '<div class="counter"></div>'
attr = {
'class': 'countdownbox',
'data-datetime': end,
'data-granularity': granularity
}
if id:
attr['id'] = id
return html.tag('div', content, attr) | def countdown(name, date, description='', id='', granularity='sec', start=None,
progressbar=False, progressbar_inversed=False, showpct=False):
'''
Create a countdown.
'''
end_date = dateparse.parse_datetime(date)
end = dateformat.format(end_date, 'U')
content = '<div class="name">' + name + '</div>'
content += '<div class="description">' + description + '</div>'
if progressbar:
if not end: raise Exception('For progressbar, start date is requried.')
parsed_date = datetime.datetime.combine(
dateparse.parse_date(start), datetime.time())
start_date = dateparse.parse_datetime(start) or parsed_date
now = datetime.datetime.now()
pct = (now - start_date).total_seconds() /\
(end_date - start_date).total_seconds()
pct = int(pct * 100)
if progressbar_inversed:
pct = 100 - pct
# Note: the output is for bootstrap!
bar = '<div class="progress progress-striped active">'
bar += '<div class="progress-bar" role="progressbar" aria-valuenow="{pct}" aria-valuemin="0" aria-valuemax="100" style="width: {pct}%">'
bar += '<span class="sr-only">{pct}% Complete</span>'
bar += '</div>'
bar += '</div>'
if showpct:
bar += '<div class="percentage">{pct}%</div>'
bar = bar.format(pct=pct)
content += bar
content += '<div class="counter"></div>'
attr = {
'class': 'countdownbox',
'data-datetime': end,
'data-granularity': granularity
}
if id:
attr['id'] = id
return html.tag('div', content, attr) | [
"Create",
"a",
"countdown",
"."
] | theduke/django-baseline | python | https://github.com/theduke/django-baseline/blob/7be8b956e53c70b35f34e1783a8fe8f716955afb/django_baseline/templatetags/countdownbox.py#L14-L64 | [
"def",
"countdown",
"(",
"name",
",",
"date",
",",
"description",
"=",
"''",
",",
"id",
"=",
"''",
",",
"granularity",
"=",
"'sec'",
",",
"start",
"=",
"None",
",",
"progressbar",
"=",
"False",
",",
"progressbar_inversed",
"=",
"False",
",",
"showpct",
"=",
"False",
")",
":",
"end_date",
"=",
"dateparse",
".",
"parse_datetime",
"(",
"date",
")",
"end",
"=",
"dateformat",
".",
"format",
"(",
"end_date",
",",
"'U'",
")",
"content",
"=",
"'<div class=\"name\">'",
"+",
"name",
"+",
"'</div>'",
"content",
"+=",
"'<div class=\"description\">'",
"+",
"description",
"+",
"'</div>'",
"if",
"progressbar",
":",
"if",
"not",
"end",
":",
"raise",
"Exception",
"(",
"'For progressbar, start date is requried.'",
")",
"parsed_date",
"=",
"datetime",
".",
"datetime",
".",
"combine",
"(",
"dateparse",
".",
"parse_date",
"(",
"start",
")",
",",
"datetime",
".",
"time",
"(",
")",
")",
"start_date",
"=",
"dateparse",
".",
"parse_datetime",
"(",
"start",
")",
"or",
"parsed_date",
"now",
"=",
"datetime",
".",
"datetime",
".",
"now",
"(",
")",
"pct",
"=",
"(",
"now",
"-",
"start_date",
")",
".",
"total_seconds",
"(",
")",
"/",
"(",
"end_date",
"-",
"start_date",
")",
".",
"total_seconds",
"(",
")",
"pct",
"=",
"int",
"(",
"pct",
"*",
"100",
")",
"if",
"progressbar_inversed",
":",
"pct",
"=",
"100",
"-",
"pct",
"# Note: the output is for bootstrap!",
"bar",
"=",
"'<div class=\"progress progress-striped active\">'",
"bar",
"+=",
"'<div class=\"progress-bar\" role=\"progressbar\" aria-valuenow=\"{pct}\" aria-valuemin=\"0\" aria-valuemax=\"100\" style=\"width: {pct}%\">'",
"bar",
"+=",
"'<span class=\"sr-only\">{pct}% Complete</span>'",
"bar",
"+=",
"'</div>'",
"bar",
"+=",
"'</div>'",
"if",
"showpct",
":",
"bar",
"+=",
"'<div class=\"percentage\">{pct}%</div>'",
"bar",
"=",
"bar",
".",
"format",
"(",
"pct",
"=",
"pct",
")",
"content",
"+=",
"bar",
"content",
"+=",
"'<div class=\"counter\"></div>'",
"attr",
"=",
"{",
"'class'",
":",
"'countdownbox'",
",",
"'data-datetime'",
":",
"end",
",",
"'data-granularity'",
":",
"granularity",
"}",
"if",
"id",
":",
"attr",
"[",
"'id'",
"]",
"=",
"id",
"return",
"html",
".",
"tag",
"(",
"'div'",
",",
"content",
",",
"attr",
")"
] | 7be8b956e53c70b35f34e1783a8fe8f716955afb |
test | cleanup | Cleanup routine to shut down all subprocesses we opened. | environment/share/doc/ipython/examples/parallel/workflow/wmanager.py | def cleanup(controller, engines):
"""Cleanup routine to shut down all subprocesses we opened."""
import signal, time
print('Starting cleanup')
print('Stopping engines...')
for e in engines:
e.send_signal(signal.SIGINT)
print('Stopping controller...')
# so it can shut down its queues
controller.send_signal(signal.SIGINT)
time.sleep(0.1)
print('Killing controller...')
controller.kill()
print('Cleanup done') | def cleanup(controller, engines):
"""Cleanup routine to shut down all subprocesses we opened."""
import signal, time
print('Starting cleanup')
print('Stopping engines...')
for e in engines:
e.send_signal(signal.SIGINT)
print('Stopping controller...')
# so it can shut down its queues
controller.send_signal(signal.SIGINT)
time.sleep(0.1)
print('Killing controller...')
controller.kill()
print('Cleanup done') | [
"Cleanup",
"routine",
"to",
"shut",
"down",
"all",
"subprocesses",
"we",
"opened",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/share/doc/ipython/examples/parallel/workflow/wmanager.py#L15-L29 | [
"def",
"cleanup",
"(",
"controller",
",",
"engines",
")",
":",
"import",
"signal",
",",
"time",
"print",
"(",
"'Starting cleanup'",
")",
"print",
"(",
"'Stopping engines...'",
")",
"for",
"e",
"in",
"engines",
":",
"e",
".",
"send_signal",
"(",
"signal",
".",
"SIGINT",
")",
"print",
"(",
"'Stopping controller...'",
")",
"# so it can shut down its queues",
"controller",
".",
"send_signal",
"(",
"signal",
".",
"SIGINT",
")",
"time",
".",
"sleep",
"(",
"0.1",
")",
"print",
"(",
"'Killing controller...'",
")",
"controller",
".",
"kill",
"(",
")",
"print",
"(",
"'Cleanup done'",
")"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
test | ConditionalModifier.pre_call | A modifier hook function. This is called in priority order prior
to invoking the ``Action`` for the step. This allows a
modifier to alter the context, or to take over subsequent
action invocation.
:param ctxt: The context object.
:param pre_mod: A list of the modifiers preceding this
modifier in the list of modifiers that is
applicable to the action. This list is in
priority order.
:param post_mod: A list of the modifiers following this
modifier in the list of modifiers that is
applicable to the action. This list is in
priority order.
:param action: The action that will be performed.
:returns: A ``None`` return value indicates that the modifier
is taking no action. A non-``None`` return value
should consist of a ``StepResult`` object; this will
suspend further ``pre_call()`` processing and
proceed to the ``post_call()`` processing. This
implementation returns a ``StepResult`` with state
``SKIPPED`` if the condition does not evaluate to
``True``. | timid/modifiers.py | def pre_call(self, ctxt, pre_mod, post_mod, action):
"""
A modifier hook function. This is called in priority order prior
to invoking the ``Action`` for the step. This allows a
modifier to alter the context, or to take over subsequent
action invocation.
:param ctxt: The context object.
:param pre_mod: A list of the modifiers preceding this
modifier in the list of modifiers that is
applicable to the action. This list is in
priority order.
:param post_mod: A list of the modifiers following this
modifier in the list of modifiers that is
applicable to the action. This list is in
priority order.
:param action: The action that will be performed.
:returns: A ``None`` return value indicates that the modifier
is taking no action. A non-``None`` return value
should consist of a ``StepResult`` object; this will
suspend further ``pre_call()`` processing and
proceed to the ``post_call()`` processing. This
implementation returns a ``StepResult`` with state
``SKIPPED`` if the condition does not evaluate to
``True``.
"""
# Check the condition
if not self.condition(ctxt):
return steps.StepResult(state=steps.SKIPPED)
return None | def pre_call(self, ctxt, pre_mod, post_mod, action):
"""
A modifier hook function. This is called in priority order prior
to invoking the ``Action`` for the step. This allows a
modifier to alter the context, or to take over subsequent
action invocation.
:param ctxt: The context object.
:param pre_mod: A list of the modifiers preceding this
modifier in the list of modifiers that is
applicable to the action. This list is in
priority order.
:param post_mod: A list of the modifiers following this
modifier in the list of modifiers that is
applicable to the action. This list is in
priority order.
:param action: The action that will be performed.
:returns: A ``None`` return value indicates that the modifier
is taking no action. A non-``None`` return value
should consist of a ``StepResult`` object; this will
suspend further ``pre_call()`` processing and
proceed to the ``post_call()`` processing. This
implementation returns a ``StepResult`` with state
``SKIPPED`` if the condition does not evaluate to
``True``.
"""
# Check the condition
if not self.condition(ctxt):
return steps.StepResult(state=steps.SKIPPED)
return None | [
"A",
"modifier",
"hook",
"function",
".",
"This",
"is",
"called",
"in",
"priority",
"order",
"prior",
"to",
"invoking",
"the",
"Action",
"for",
"the",
"step",
".",
"This",
"allows",
"a",
"modifier",
"to",
"alter",
"the",
"context",
"or",
"to",
"take",
"over",
"subsequent",
"action",
"invocation",
"."
] | rackerlabs/timid | python | https://github.com/rackerlabs/timid/blob/b1c6aa159ab380a033740f4aa392cf0d125e0ac6/timid/modifiers.py#L61-L93 | [
"def",
"pre_call",
"(",
"self",
",",
"ctxt",
",",
"pre_mod",
",",
"post_mod",
",",
"action",
")",
":",
"# Check the condition",
"if",
"not",
"self",
".",
"condition",
"(",
"ctxt",
")",
":",
"return",
"steps",
".",
"StepResult",
"(",
"state",
"=",
"steps",
".",
"SKIPPED",
")",
"return",
"None"
] | b1c6aa159ab380a033740f4aa392cf0d125e0ac6 |
test | IgnoreErrorsModifier.post_call | A modifier hook function. This is called in reverse-priority
order after invoking the ``Action`` for the step. This allows
a modifier to inspect or alter the result of the step.
:param ctxt: The context object.
:param result: The result of the action. This will be a
``StepResult`` object.
:param action: The action that was performed.
:param post_mod: A list of modifiers following this modifier
in the list of modifiers that is applicable
to the action. This list is in priority
order.
:param pre_mod: A list of modifiers preceding this modifier in
the list of modifiers that is applicable to
the action. This list is in priority order.
:returns: The result for the action, optionally modified. If
the result is not modified, ``result`` must be
returned unchanged. This implementation alters the
``ignore`` property of the ``result`` object to
match the configured value. | timid/modifiers.py | def post_call(self, ctxt, result, action, post_mod, pre_mod):
"""
A modifier hook function. This is called in reverse-priority
order after invoking the ``Action`` for the step. This allows
a modifier to inspect or alter the result of the step.
:param ctxt: The context object.
:param result: The result of the action. This will be a
``StepResult`` object.
:param action: The action that was performed.
:param post_mod: A list of modifiers following this modifier
in the list of modifiers that is applicable
to the action. This list is in priority
order.
:param pre_mod: A list of modifiers preceding this modifier in
the list of modifiers that is applicable to
the action. This list is in priority order.
:returns: The result for the action, optionally modified. If
the result is not modified, ``result`` must be
returned unchanged. This implementation alters the
``ignore`` property of the ``result`` object to
match the configured value.
"""
# Set the ignore state
result.ignore = self.config
return result | def post_call(self, ctxt, result, action, post_mod, pre_mod):
"""
A modifier hook function. This is called in reverse-priority
order after invoking the ``Action`` for the step. This allows
a modifier to inspect or alter the result of the step.
:param ctxt: The context object.
:param result: The result of the action. This will be a
``StepResult`` object.
:param action: The action that was performed.
:param post_mod: A list of modifiers following this modifier
in the list of modifiers that is applicable
to the action. This list is in priority
order.
:param pre_mod: A list of modifiers preceding this modifier in
the list of modifiers that is applicable to
the action. This list is in priority order.
:returns: The result for the action, optionally modified. If
the result is not modified, ``result`` must be
returned unchanged. This implementation alters the
``ignore`` property of the ``result`` object to
match the configured value.
"""
# Set the ignore state
result.ignore = self.config
return result | [
"A",
"modifier",
"hook",
"function",
".",
"This",
"is",
"called",
"in",
"reverse",
"-",
"priority",
"order",
"after",
"invoking",
"the",
"Action",
"for",
"the",
"step",
".",
"This",
"allows",
"a",
"modifier",
"to",
"inspect",
"or",
"alter",
"the",
"result",
"of",
"the",
"step",
"."
] | rackerlabs/timid | python | https://github.com/rackerlabs/timid/blob/b1c6aa159ab380a033740f4aa392cf0d125e0ac6/timid/modifiers.py#L111-L139 | [
"def",
"post_call",
"(",
"self",
",",
"ctxt",
",",
"result",
",",
"action",
",",
"post_mod",
",",
"pre_mod",
")",
":",
"# Set the ignore state",
"result",
".",
"ignore",
"=",
"self",
".",
"config",
"return",
"result"
] | b1c6aa159ab380a033740f4aa392cf0d125e0ac6 |
test | save_ids | Keep our history and outstanding attributes up to date after a method call. | environment/lib/python2.7/site-packages/IPython/parallel/client/view.py | def save_ids(f, self, *args, **kwargs):
"""Keep our history and outstanding attributes up to date after a method call."""
n_previous = len(self.client.history)
try:
ret = f(self, *args, **kwargs)
finally:
nmsgs = len(self.client.history) - n_previous
msg_ids = self.client.history[-nmsgs:]
self.history.extend(msg_ids)
map(self.outstanding.add, msg_ids)
return ret | def save_ids(f, self, *args, **kwargs):
"""Keep our history and outstanding attributes up to date after a method call."""
n_previous = len(self.client.history)
try:
ret = f(self, *args, **kwargs)
finally:
nmsgs = len(self.client.history) - n_previous
msg_ids = self.client.history[-nmsgs:]
self.history.extend(msg_ids)
map(self.outstanding.add, msg_ids)
return ret | [
"Keep",
"our",
"history",
"and",
"outstanding",
"attributes",
"up",
"to",
"date",
"after",
"a",
"method",
"call",
"."
] | cloud9ers/gurumate | python | https://github.com/cloud9ers/gurumate/blob/075dc74d1ee62a8c6b7a8bf2b271364f01629d1e/environment/lib/python2.7/site-packages/IPython/parallel/client/view.py#L44-L54 | [
"def",
"save_ids",
"(",
"f",
",",
"self",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"n_previous",
"=",
"len",
"(",
"self",
".",
"client",
".",
"history",
")",
"try",
":",
"ret",
"=",
"f",
"(",
"self",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
"finally",
":",
"nmsgs",
"=",
"len",
"(",
"self",
".",
"client",
".",
"history",
")",
"-",
"n_previous",
"msg_ids",
"=",
"self",
".",
"client",
".",
"history",
"[",
"-",
"nmsgs",
":",
"]",
"self",
".",
"history",
".",
"extend",
"(",
"msg_ids",
")",
"map",
"(",
"self",
".",
"outstanding",
".",
"add",
",",
"msg_ids",
")",
"return",
"ret"
] | 075dc74d1ee62a8c6b7a8bf2b271364f01629d1e |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.