_id
stringlengths 2
7
| title
stringlengths 1
88
| partition
stringclasses 3
values | text
stringlengths 31
13.1k
| language
stringclasses 1
value | meta_information
dict |
---|---|---|---|---|---|
q280300
|
path.read_md5
|
test
|
def read_md5(self):
""" Calculate the md5 hash for this file.
This reads through the entire file.
"""
f = self.open('rb')
try:
m = md5()
while True:
d = f.read(8192)
if not
|
python
|
{
"resource": ""
}
|
q280301
|
Profile.begin
|
test
|
def begin(self):
"""Create profile stats file and load profiler.
"""
|
python
|
{
"resource": ""
}
|
q280302
|
Profile.report
|
test
|
def report(self, stream):
"""Output profiler report.
"""
log.debug('printing profiler report')
self.prof.close()
prof_stats = stats.load(self.pfile)
prof_stats.sort_stats(self.sort)
# 2.5 has completely different stream handling from 2.4 and earlier.
# Before 2.5, stats objects have no stream attribute; in 2.5 and later
# a reference sys.stdout is stored before we can tweak it.
|
python
|
{
"resource": ""
}
|
q280303
|
Profile.finalize
|
test
|
def finalize(self, result):
"""Clean up stats file, if configured to do so.
"""
if not self.available():
return
try:
self.prof.close()
except AttributeError:
# TODO: is this trying to catch just the case where not
# hasattr(self.prof, "close")? If so, the function call should be
# moved out of the try: suite.
pass
if self.clean_stats_file:
|
python
|
{
"resource": ""
}
|
q280304
|
Command.handle
|
test
|
def handle(self, *args, **options):
"""Handle CLI command"""
try:
while True:
Channel(HEARTBEAT_CHANNEL).send({'time':time.time()})
|
python
|
{
"resource": ""
}
|
q280305
|
InputHookManager.enable_wx
|
test
|
def enable_wx(self, app=None):
"""Enable event loop integration with wxPython.
Parameters
----------
app : WX Application, optional.
Running application to use. If not given, we probe WX for an
existing application object, and create a new one if none is found.
Notes
-----
This methods sets the ``PyOS_InputHook`` for wxPython, which allows
the wxPython to integrate with terminal based applications like
IPython.
If ``app`` is not given we probe for an existing one, and return it if
found. If no existing app is found, we create an :class:`wx.App` as
follows::
import wx
app = wx.App(redirect=False, clearSigInt=False)
"""
import wx
wx_version = V(wx.__version__).version
if wx_version < [2, 8]:
raise ValueError("requires wxPython
|
python
|
{
"resource": ""
}
|
q280306
|
InputHookManager.disable_wx
|
test
|
def disable_wx(self):
"""Disable event loop integration with wxPython.
This merely sets PyOS_InputHook to NULL.
"""
|
python
|
{
"resource": ""
}
|
q280307
|
InputHookManager.disable_qt4
|
test
|
def disable_qt4(self):
"""Disable event loop integration with PyQt4.
This merely sets PyOS_InputHook to NULL.
"""
|
python
|
{
"resource": ""
}
|
q280308
|
InputHookManager.enable_gtk
|
test
|
def enable_gtk(self, app=None):
"""Enable event loop integration with PyGTK.
Parameters
----------
app : ignored
Ignored, it's only a placeholder to keep the call signature of all
gui activation methods consistent, which simplifies the logic of
supporting magics.
Notes
-----
This methods sets the PyOS_InputHook for PyGTK, which allows
the PyGTK to integrate with terminal based applications like
IPython.
"""
import gtk
try:
|
python
|
{
"resource": ""
}
|
q280309
|
InputHookManager.enable_tk
|
test
|
def enable_tk(self, app=None):
"""Enable event loop integration with Tk.
Parameters
----------
app : toplevel :class:`Tkinter.Tk` widget, optional.
Running toplevel widget to use. If not given, we probe Tk for an
existing one, and create a new one if none is found.
Notes
-----
If you have already created a :class:`Tkinter.Tk` object, the only
thing done by this method is to register with the
:class:`InputHookManager`, since creating that object automatically
|
python
|
{
"resource": ""
}
|
q280310
|
InputHookManager.enable_pyglet
|
test
|
def enable_pyglet(self, app=None):
"""Enable event loop integration with pyglet.
Parameters
----------
app : ignored
Ignored, it's only a placeholder to keep the call signature of all
gui activation methods consistent, which simplifies the logic of
|
python
|
{
"resource": ""
}
|
q280311
|
wave_saver
|
test
|
def wave_saver(u, x, y, t):
"""save the wave log"""
|
python
|
{
"resource": ""
}
|
q280312
|
HistoryAccessor.init_db
|
test
|
def init_db(self):
"""Connect to the database, and create tables if necessary."""
# use detect_types so that timestamps return datetime objects
self.db = sqlite3.connect(self.hist_file,
detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
self.db.execute("""CREATE TABLE IF NOT EXISTS sessions (session integer
primary key autoincrement, start timestamp,
end timestamp, num_cmds integer, remark text)""")
self.db.execute("""CREATE TABLE IF NOT EXISTS history
(session integer, line integer, source text, source_raw text,
|
python
|
{
"resource": ""
}
|
q280313
|
HistoryAccessor._run_sql
|
test
|
def _run_sql(self, sql, params, raw=True, output=False):
"""Prepares and runs an SQL query for the history database.
Parameters
----------
sql : str
Any filtering expressions to go after SELECT ... FROM ...
params : tuple
Parameters passed to the SQL query (to replace "?")
raw, output : bool
See :meth:`get_range`
Returns
-------
Tuples as :meth:`get_range`
"""
toget = 'source_raw' if raw else 'source'
sqlfrom = "history"
if output:
|
python
|
{
"resource": ""
}
|
q280314
|
HistoryAccessor.get_session_info
|
test
|
def get_session_info(self, session=0):
"""get info about a session
Parameters
----------
session : int
Session number to retrieve. The current session is 0, and negative
numbers count back from current session, so -1 is previous session.
|
python
|
{
"resource": ""
}
|
q280315
|
HistoryAccessor.get_tail
|
test
|
def get_tail(self, n=10, raw=True, output=False, include_latest=False):
"""Get the last n lines from the history database.
Parameters
----------
n : int
The number of lines to get
raw, output : bool
See :meth:`get_range`
include_latest : bool
If False (default), n+1 lines are fetched, and the latest one
is discarded. This is intended to be used where the function
is called by a user command, which it should not return.
Returns
-------
Tuples as
|
python
|
{
"resource": ""
}
|
q280316
|
HistoryAccessor.get_range_by_str
|
test
|
def get_range_by_str(self, rangestr, raw=True, output=False):
"""Get lines of history from a string of ranges, as used by magic
commands %hist, %save, %macro, etc.
Parameters
----------
rangestr : str
A string specifying ranges, e.g. "5
|
python
|
{
"resource": ""
}
|
q280317
|
HistoryManager._get_hist_file_name
|
test
|
def _get_hist_file_name(self, profile=None):
"""Get default history file name based on the Shell's profile.
The profile parameter is ignored, but must exist for compatibility with
the
|
python
|
{
"resource": ""
}
|
q280318
|
HistoryManager.name_session
|
test
|
def name_session(self, name):
"""Give the current session a name in the history database."""
with self.db:
|
python
|
{
"resource": ""
}
|
q280319
|
HistoryManager.reset
|
test
|
def reset(self, new_session=True):
"""Clear the session history, releasing all object references, and
optionally open a new session."""
self.output_hist.clear()
# The directory history can't be completely empty
self.dir_hist[:] = [os.getcwdu()]
if new_session:
|
python
|
{
"resource": ""
}
|
q280320
|
HistoryManager._get_range_session
|
test
|
def _get_range_session(self, start=1, stop=None, raw=True, output=False):
"""Get input and output history from the current session. Called by
get_range, and takes similar parameters."""
input_hist = self.input_hist_raw if raw else self.input_hist_parsed
n = len(input_hist)
if start < 0:
start += n
if not stop or (stop > n):
|
python
|
{
"resource": ""
}
|
q280321
|
HistoryManager.store_output
|
test
|
def store_output(self, line_num):
"""If database output logging is enabled, this saves all the
outputs from the indicated prompt number to the database. It's
called by run_cell after code has been executed.
Parameters
----------
line_num : int
The line number from which to save outputs
"""
if (not self.db_log_output) or (line_num not in self.output_hist_reprs):
return
|
python
|
{
"resource": ""
}
|
q280322
|
HistoryManager.writeout_cache
|
test
|
def writeout_cache(self, conn=None):
"""Write any entries in the cache to the database."""
if conn is None:
conn = self.db
with self.db_input_cache_lock:
try:
self._writeout_input_cache(conn)
except sqlite3.IntegrityError:
self.new_session(conn)
print("ERROR! Session/line number was not unique in",
"database. History logging moved to new session",
self.session_number)
try:
# Try writing to the new session. If this fails, don't
# recurse
self._writeout_input_cache(conn)
|
python
|
{
"resource": ""
}
|
q280323
|
HistorySavingThread.stop
|
test
|
def stop(self):
"""This can be called from the main thread to safely stop this thread.
Note that it does not attempt to write out remaining history before
exiting. That should be done by calling
|
python
|
{
"resource": ""
}
|
q280324
|
_get_num_cpus
|
test
|
def _get_num_cpus():
"""Return the number of CPUs on the system"""
# we try to determine num CPUs by using different approaches.
# SC_NPROCESSORS_ONLN seems to be the safer and it is also
# used by multiprocessing module
try:
return os.sysconf("SC_NPROCESSORS_ONLN")
except ValueError:
# as a second fallback we try to parse /proc/cpuinfo
num = 0
f = open('/proc/cpuinfo', 'r')
try:
lines = f.readlines()
finally:
f.close()
for line in lines:
if line.lower().startswith('processor'):
num += 1
#
|
python
|
{
"resource": ""
}
|
q280325
|
get_system_per_cpu_times
|
test
|
def get_system_per_cpu_times():
"""Return a list of namedtuple representing the CPU times
for every CPU available on the system.
"""
cpus = []
f = open('/proc/stat', 'r')
# get rid of the first line who refers to system wide CPU stats
try:
f.readline()
for line in f.readlines():
if line.startswith('cpu'):
values = line.split()[1:8]
|
python
|
{
"resource": ""
}
|
q280326
|
disk_partitions
|
test
|
def disk_partitions(all=False):
"""Return mounted disk partitions as a list of nameduples"""
phydevs = []
f = open("/proc/filesystems", "r")
try:
for line in f:
if not line.startswith("nodev"):
phydevs.append(line.strip())
finally:
|
python
|
{
"resource": ""
}
|
q280327
|
get_pid_list
|
test
|
def get_pid_list():
"""Returns a list of PIDs currently running on the system."""
pids =
|
python
|
{
"resource": ""
}
|
q280328
|
nice_pair
|
test
|
def nice_pair(pair):
"""Make a nice string representation of a pair of numbers.
If the numbers are equal, just return the number, otherwise return the pair
with a dash between them, indicating the range.
|
python
|
{
"resource": ""
}
|
q280329
|
format_lines
|
test
|
def format_lines(statements, lines):
"""Nicely format a list of line numbers.
Format a list of line numbers for printing by coalescing groups of lines as
long as the lines represent consecutive statements. This will coalesce
even if there are gaps between statements.
For example, if `statements` is [1,2,3,4,5,10,11,12,13,14] and
`lines` is [1,2,5,10,11,13,14] then the result will be "1-2, 5-11, 13-14".
"""
pairs = []
i = 0
j = 0
start = None
statements = sorted(statements)
lines = sorted(lines)
while i < len(statements) and j < len(lines):
|
python
|
{
"resource": ""
}
|
q280330
|
short_stack
|
test
|
def short_stack():
"""Return a string summarizing the call stack."""
stack = inspect.stack()[:0:-1]
|
python
|
{
"resource": ""
}
|
q280331
|
expensive
|
test
|
def expensive(fn):
"""A decorator to cache the result of an expensive operation.
Only applies to methods with no arguments.
"""
attr = "_cache_" +
|
python
|
{
"resource": ""
}
|
q280332
|
join_regex
|
test
|
def join_regex(regexes):
"""Combine a list of regexes into one that matches any of them."""
if len(regexes) > 1:
return "|".join(["(%s)" % r for
|
python
|
{
"resource": ""
}
|
q280333
|
file_be_gone
|
test
|
def file_be_gone(path):
"""Remove a file, and don't get annoyed if it doesn't exist."""
try:
os.remove(path)
except
|
python
|
{
"resource": ""
}
|
q280334
|
Hasher.update
|
test
|
def update(self, v):
"""Add `v` to the hash, recursively if needed."""
self.md5.update(to_bytes(str(type(v))))
if isinstance(v, string_class):
self.md5.update(to_bytes(v))
elif v is None:
pass
elif isinstance(v, (int, float)):
self.md5.update(to_bytes(str(v)))
elif isinstance(v, (tuple, list)):
for e in v:
self.update(e)
elif isinstance(v, dict):
keys = v.keys()
|
python
|
{
"resource": ""
}
|
q280335
|
ClusterManager.update_profiles
|
test
|
def update_profiles(self):
"""List all profiles in the ipython_dir and cwd.
"""
for path in [get_ipython_dir(), os.getcwdu()]:
for profile in list_profiles_in(path):
pd = self.get_profile_dir(profile, path)
|
python
|
{
"resource": ""
}
|
q280336
|
ClusterManager.start_cluster
|
test
|
def start_cluster(self, profile, n=None):
"""Start a cluster for a given profile."""
self.check_profile(profile)
data = self.profiles[profile]
if data['status'] == 'running':
raise web.HTTPError(409, u'cluster already running')
cl, esl, default_n = self.build_launchers(data['profile_dir'])
n = n if n is not None else default_n
def clean_data():
data.pop('controller_launcher',None)
data.pop('engine_set_launcher',None)
data.pop('n',None)
data['status'] = 'stopped'
def engines_stopped(r):
self.log.debug('Engines stopped')
if cl.running:
cl.stop()
clean_data()
esl.on_stop(engines_stopped)
def controller_stopped(r):
self.log.debug('Controller stopped')
if esl.running:
esl.stop()
|
python
|
{
"resource": ""
}
|
q280337
|
ClusterManager.stop_cluster
|
test
|
def stop_cluster(self, profile):
"""Stop a cluster for a given profile."""
self.check_profile(profile)
data = self.profiles[profile]
if data['status'] == 'stopped':
raise web.HTTPError(409, u'cluster not running')
data = self.profiles[profile]
cl = data['controller_launcher']
esl = data['engine_set_launcher']
if cl.running:
cl.stop()
if esl.running:
esl.stop()
|
python
|
{
"resource": ""
}
|
q280338
|
_find_cmd
|
test
|
def _find_cmd(cmd):
"""Find the full path to a .bat or .exe using the win32api module."""
try:
from win32api import SearchPath
except ImportError:
raise ImportError('you need to have pywin32 installed for this to work')
else:
PATH = os.environ['PATH']
extensions = ['.exe', '.com', '.bat', '.py']
path = None
for ext in extensions:
try:
|
python
|
{
"resource": ""
}
|
q280339
|
_system_body
|
test
|
def _system_body(p):
"""Callback for _system."""
enc = DEFAULT_ENCODING
for line in read_no_interrupt(p.stdout).splitlines():
line = line.decode(enc, 'replace')
print(line, file=sys.stdout)
for line in
|
python
|
{
"resource": ""
}
|
q280340
|
Reporter.find_code_units
|
test
|
def find_code_units(self, morfs):
"""Find the code units we'll report on.
`morfs` is a list of modules or filenames.
"""
morfs = morfs or self.coverage.data.measured_files()
file_locator = self.coverage.file_locator
self.code_units = code_unit_factory(morfs, file_locator)
if self.config.include:
patterns = prep_patterns(self.config.include)
filtered = []
for
|
python
|
{
"resource": ""
}
|
q280341
|
Reporter.report_files
|
test
|
def report_files(self, report_fn, morfs, directory=None):
"""Run a reporting function on a number of morfs.
`report_fn` is called for each relative morf in `morfs`. It is called
as::
report_fn(code_unit, analysis)
where `code_unit` is the `CodeUnit` for the morf, and `analysis` is
the `Analysis` for the morf.
"""
self.find_code_units(morfs)
if not self.code_units:
raise CoverageException("No data to report.")
self.directory = directory
if self.directory and not os.path.exists(self.directory):
os.makedirs(self.directory)
for cu in self.code_units:
|
python
|
{
"resource": ""
}
|
q280342
|
raises
|
test
|
def raises(*exceptions):
"""Test must raise one of expected exceptions to pass.
Example use::
@raises(TypeError, ValueError)
def test_raises_type_error():
raise TypeError("This test passes")
@raises(Exception)
def test_that_fails_by_passing():
pass
If you want to test many assertions about exceptions in a single test,
you may want to use `assert_raises` instead.
"""
valid = ' or '.join([e.__name__ for e in exceptions])
def decorate(func):
name = func.__name__
def newfunc(*arg, **kw):
try:
|
python
|
{
"resource": ""
}
|
q280343
|
set_trace
|
test
|
def set_trace():
"""Call pdb.set_trace in the calling frame, first restoring
sys.stdout to the real output stream. Note that sys.stdout is NOT
|
python
|
{
"resource": ""
}
|
q280344
|
timed
|
test
|
def timed(limit):
"""Test must finish within specified time limit to pass.
Example use::
@timed(.1)
def test_that_fails():
time.sleep(.2)
"""
def decorate(func):
def newfunc(*arg, **kw):
|
python
|
{
"resource": ""
}
|
q280345
|
InteractiveShellApp.init_extensions
|
test
|
def init_extensions(self):
"""Load all IPython extensions in IPythonApp.extensions.
This uses the :meth:`ExtensionManager.load_extensions` to load all
the extensions listed in ``self.extensions``.
"""
try:
self.log.debug("Loading IPython extensions...")
extensions = self.default_extensions + self.extensions
for ext in extensions:
try:
self.log.info("Loading IPython extension: %s" % ext)
|
python
|
{
"resource": ""
}
|
q280346
|
InteractiveShellApp.init_code
|
test
|
def init_code(self):
"""run the pre-flight code, specified via exec_lines"""
self._run_startup_files()
self._run_exec_lines()
self._run_exec_files()
self._run_cmd_line_code()
self._run_module()
# flush output, so itwon't be attached to the first
|
python
|
{
"resource": ""
}
|
q280347
|
InteractiveShellApp._run_exec_lines
|
test
|
def _run_exec_lines(self):
"""Run lines of code in IPythonApp.exec_lines in the user's namespace."""
if not self.exec_lines:
return
try:
self.log.debug("Running code from IPythonApp.exec_lines...")
for line in self.exec_lines:
try:
self.log.info("Running code in user namespace: %s" %
line)
self.shell.run_cell(line, store_history=False)
|
python
|
{
"resource": ""
}
|
q280348
|
InteractiveShellApp._run_startup_files
|
test
|
def _run_startup_files(self):
"""Run files from profile startup directory"""
startup_dir = self.profile_dir.startup_dir
startup_files = glob.glob(os.path.join(startup_dir, '*.py'))
startup_files += glob.glob(os.path.join(startup_dir, '*.ipy'))
|
python
|
{
"resource": ""
}
|
q280349
|
InteractiveShellApp._run_exec_files
|
test
|
def _run_exec_files(self):
"""Run files from IPythonApp.exec_files"""
if not self.exec_files:
return
self.log.debug("Running files in IPythonApp.exec_files...")
try:
for fname in self.exec_files:
|
python
|
{
"resource": ""
}
|
q280350
|
InteractiveShellApp._run_cmd_line_code
|
test
|
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()
|
python
|
{
"resource": ""
}
|
q280351
|
InteractiveShellApp._run_module
|
test
|
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`.
|
python
|
{
"resource": ""
}
|
q280352
|
generic
|
test
|
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,
|
python
|
{
"resource": ""
}
|
q280353
|
data_filename
|
test
|
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.
|
python
|
{
"resource": ""
}
|
q280354
|
data
|
test
|
def data(fname):
"""Return the contents of a data file of ours."""
data_file =
|
python
|
{
"resource": ""
}
|
q280355
|
escape
|
test
|
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("
|
python
|
{
"resource": ""
}
|
q280356
|
HtmlReporter.report
|
test
|
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:
|
python
|
{
"resource": ""
}
|
q280357
|
HtmlReporter.make_local_static_report_files
|
test
|
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(
|
python
|
{
"resource": ""
}
|
q280358
|
HtmlReporter.write_html
|
test
|
def write_html(self, fname, html):
"""Write `html` to `fname`, properly encoded."""
fout = open(fname, "wb")
try:
|
python
|
{
"resource": ""
}
|
q280359
|
HtmlReporter.file_hash
|
test
|
def file_hash(self, source, cu):
"""Compute a hash that changes if the file needs to be re-reported."""
m = Hasher()
|
python
|
{
"resource": ""
}
|
q280360
|
HtmlReporter.index_file
|
test
|
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):
|
python
|
{
"resource": ""
}
|
q280361
|
HtmlStatus.read
|
test
|
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
|
python
|
{
"resource": ""
}
|
q280362
|
HtmlStatus.write
|
test
|
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,
|
python
|
{
"resource": ""
}
|
q280363
|
sort_compare
|
test
|
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)."""
|
python
|
{
"resource": ""
}
|
q280364
|
get_slice
|
test
|
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:
|
python
|
{
"resource": ""
}
|
q280365
|
chop
|
test
|
def chop(seq, size):
"""Chop a sequence into chunks of the given size."""
chunk = lambda i:
|
python
|
{
"resource": ""
}
|
q280366
|
read_config
|
test
|
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')
|
python
|
{
"resource": ""
}
|
q280367
|
read_manifest
|
test
|
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 =
|
python
|
{
"resource": ""
}
|
q280368
|
_glob_to_regexp
|
test
|
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
|
python
|
{
"resource": ""
}
|
q280369
|
file_matches
|
test
|
def file_matches(filename, patterns):
"""Does this filename match any of the patterns?"""
return
|
python
|
{
"resource": ""
}
|
q280370
|
Git.get_versioned_files
|
test
|
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.
|
python
|
{
"resource": ""
}
|
q280371
|
MultiKernelManager.start_kernel
|
test
|
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(
|
python
|
{
"resource": ""
}
|
q280372
|
MultiKernelManager.shutdown_kernel
|
test
|
def shutdown_kernel(self, kernel_id):
"""Shutdown a kernel by its kernel uuid.
Parameters
==========
kernel_id : uuid
The id of the kernel to shutdown.
|
python
|
{
"resource": ""
}
|
q280373
|
MultiKernelManager.kill_kernel
|
test
|
def kill_kernel(self, kernel_id):
"""Kill a kernel by its kernel uuid.
Parameters
==========
kernel_id : uuid
The id of the kernel to kill.
|
python
|
{
"resource": ""
}
|
q280374
|
MultiKernelManager.get_kernel
|
test
|
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.
"""
|
python
|
{
"resource": ""
}
|
q280375
|
MultiKernelManager.get_kernel_ports
|
test
|
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:
|
python
|
{
"resource": ""
}
|
q280376
|
MappingKernelManager.notebook_for_kernel
|
test
|
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]
|
python
|
{
"resource": ""
}
|
q280377
|
MappingKernelManager.start_kernel
|
test
|
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)
|
python
|
{
"resource": ""
}
|
q280378
|
MappingKernelManager.shutdown_kernel
|
test
|
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)
|
python
|
{
"resource": ""
}
|
q280379
|
MappingKernelManager.interrupt_kernel
|
test
|
def interrupt_kernel(self, kernel_id):
"""Interrupt a kernel."""
self._check_kernel_id(kernel_id)
super(MappingKernelManager,
|
python
|
{
"resource": ""
}
|
q280380
|
MappingKernelManager.restart_kernel
|
test
|
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.
|
python
|
{
"resource": ""
}
|
q280381
|
MappingKernelManager.create_iopub_stream
|
test
|
def create_iopub_stream(self, kernel_id):
"""Create a new iopub stream."""
self._check_kernel_id(kernel_id)
return
|
python
|
{
"resource": ""
}
|
q280382
|
MappingKernelManager.create_shell_stream
|
test
|
def create_shell_stream(self, kernel_id):
"""Create a new shell stream."""
self._check_kernel_id(kernel_id)
return
|
python
|
{
"resource": ""
}
|
q280383
|
MappingKernelManager.create_hb_stream
|
test
|
def create_hb_stream(self, kernel_id):
"""Create a new hb stream."""
|
python
|
{
"resource": ""
}
|
q280384
|
ResetMixin.reset
|
test
|
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
|
python
|
{
"resource": ""
}
|
q280385
|
export_html
|
test
|
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
|
python
|
{
"resource": ""
}
|
q280386
|
export_xhtml
|
test
|
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
|
python
|
{
"resource": ""
}
|
q280387
|
ensure_utf8
|
test
|
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
|
python
|
{
"resource": ""
}
|
q280388
|
fix_html
|
test
|
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>')
|
python
|
{
"resource": ""
}
|
q280389
|
HtmlExporter.export
|
test
|
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')
|
python
|
{
"resource": ""
}
|
q280390
|
get_unique_or_none
|
test
|
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:
|
python
|
{
"resource": ""
}
|
q280391
|
get_query_includes
|
test
|
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:
|
python
|
{
"resource": ""
}
|
q280392
|
get_text_query
|
test
|
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
|
python
|
{
"resource": ""
}
|
q280393
|
get_date_greater_query
|
test
|
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)
|
python
|
{
"resource": ""
}
|
q280394
|
get_date_less_query
|
test
|
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)
|
python
|
{
"resource": ""
}
|
q280395
|
get_null_or_blank_query
|
test
|
def get_null_or_blank_query(field=None):
"""
Query for null or blank field.
"""
if not field:
return field
|
python
|
{
"resource": ""
}
|
q280396
|
CaseInsensitiveQuerySet.case_insensitive
|
test
|
def case_insensitive(self, fields_dict):
"""
Converts queries to case insensitive for special fields.
"""
if hasattr(self.model, 'CASE_INSENSITIVE_FIELDS'):
|
python
|
{
"resource": ""
}
|
q280397
|
AttributeSelector.options
|
test
|
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",
|
python
|
{
"resource": ""
}
|
q280398
|
AttributeSelector.validateAttrib
|
test
|
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()
|
python
|
{
"resource": ""
}
|
q280399
|
AttributeSelector.wantMethod
|
test
|
def wantMethod(self, method):
"""Accept the method if its attributes match.
"""
try:
|
python
|
{
"resource": ""
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.