text_prompt stringlengths 157 13.1k | code_prompt stringlengths 7 19.8k ⌀ |
|---|---|
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _handle_func_def(self, node, scope, ctxt, stream):
"""Handle FuncDef nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO """ |
self._dlog("handling function definition")
func = self._handle_node(node.decl, scope, ctxt, stream)
func.body = node.body |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _handle_param_list(self, node, scope, ctxt, stream):
"""Handle ParamList nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO """ |
self._dlog("handling param list")
# params should be a list of tuples:
# [(<name>, <field_class>), ...]
params = []
for param in node.params:
self._mark_id_as_lazy(param)
param_info = self._handle_node(param, scope, ctxt, stream)
params.append... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _handle_func_decl(self, node, scope, ctxt, stream):
"""Handle FuncDecl nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO """ |
self._dlog("handling func decl")
if node.args is not None:
# could just call _handle_param_list directly...
for param in node.args.params:
# see the check in _handle_decl for how this is kept from
# being added to the local context/scope
... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _handle_func_call(self, node, scope, ctxt, stream):
"""Handle FuncCall nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO """ |
self._dlog("handling function call to '{}'".format(node.name.name))
if node.args is None:
func_args = []
else:
func_args = self._handle_node(node.args, scope, ctxt, stream)
func = self._handle_node(node.name, scope, ctxt, stream)
return func.call(func_arg... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _handle_expr_list(self, node, scope, ctxt, stream):
"""Handle ExprList nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO """ |
self._dlog("handling expression list")
exprs = [
self._handle_node(expr, scope, ctxt, stream) for expr in node.exprs
]
return exprs |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _handle_compound(self, node, scope, ctxt, stream):
"""Handle Compound nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO """ |
self._dlog("handling compound statement")
#scope.push()
try:
for child in node.children():
self._handle_node(child, scope, ctxt, stream)
# in case a return occurs, be sure to pop the scope
# (returns are implemented by raising an exception)
... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _handle_return(self, node, scope, ctxt, stream):
"""Handle Return nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO """ |
self._dlog("handling return")
if node.expr is None:
ret_val = None
else:
ret_val = self._handle_node(node.expr, scope, ctxt, stream)
self._dlog("return value = {}".format(ret_val))
raise errors.InterpReturn(ret_val) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _handle_enum(self, node, scope, ctxt, stream):
"""Handle enum nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO """ |
self._dlog("handling enum")
if node.type is None:
enum_cls = fields.Int
else:
enum_cls = self._handle_node(node.type, scope, ctxt, stream)
enum_vals = {}
curr_val = enum_cls()
curr_val._pfp__value = -1
for enumerator in node.values.enumer... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _handle_array_decl(self, node, scope, ctxt, stream):
"""Handle ArrayDecl nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO """ |
self._dlog("handling array declaration '{}'".format(node.type.declname))
if node.dim is None:
# will be used
array_size = None
else:
array_size = self._handle_node(node.dim, scope, ctxt, stream)
self._dlog("array size = {}".format(array_size))
... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _handle_array_ref(self, node, scope, ctxt, stream):
"""Handle ArrayRef nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO """ |
ary = self._handle_node(node.name, scope, ctxt, stream)
subscript = self._handle_node(node.subscript, scope, ctxt, stream)
return ary[fields.PYVAL(subscript)] |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _handle_if(self, node, scope, ctxt, stream):
"""Handle If nodes :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO """ |
self._dlog("handling if/ternary_op")
cond = self._handle_node(node.cond, scope, ctxt, stream)
if cond:
# there should always be an iftrue
return self._handle_node(node.iftrue, scope, ctxt, stream)
else:
if node.iffalse is not None:
ret... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _handle_continue(self, node, scope, ctxt, stream):
"""Handle continue node :node: TODO :scope: TODO :ctxt: TODO :stream: TODO :returns: TODO """ |
self._dlog("handling continue")
raise errors.InterpContinue() |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _get_value(self, node, scope, ctxt, stream):
"""Return the value of the node. It is expected to be either an AST.ID instance or a constant :node: TODO :retur... |
res = self._handle_node(node, scope, ctxt, stream)
if isinstance(res, fields.Field):
return res._pfp__value
# assume it's a constant
else:
return res |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _resolve_to_field_class(self, names, scope):
"""Resolve the names to a class in fields.py, resolving past typedefs, etc :names: TODO :scope: TODO :ctxt: TODO... |
switch = {
"char" : "Char",
"int" : "Int",
"long" : "Int",
"int64" : "Int64",
"uint64" : "UInt64",
"short" : "Short",
"double" : "Double",
"float" : "Float",
"void" : "Void",
... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def bytes_to_bits(bytes_):
"""Convert bytes to a list of bits """ |
res = []
for x in bytes_:
if not isinstance(x, int):
x = ord(x)
res += byte_to_bits(x)
return res |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def is_eof(self):
"""Return if the stream has reached EOF or not without discarding any unflushed bits :returns: True/False """ |
pos = self._stream.tell()
byte = self._stream.read(1)
self._stream.seek(pos, 0)
return utils.binary(byte) == utils.binary("") |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def close(self):
"""Close the stream """ |
self.closed = True
self._flush_bits_to_stream()
self._stream.close() |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def read_bits(self, num):
"""Read ``num`` number of bits from the stream :num: number of bits to read :returns: a list of ``num`` bits, or an empty list if EOF h... |
if num > len(self._bits):
needed = num - len(self._bits)
num_bytes = int(math.ceil(needed / 8.0))
read_bytes = self._stream.read(num_bytes)
for bit in bytes_to_bits(read_bytes):
self._bits.append(bit)
res = []
while len(res) < nu... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def write(self, data):
"""Write data to the stream :data: the data to write to the stream :returns: None """ |
if self.padded:
# flush out any remaining bits first
if len(self._bits) > 0:
self._flush_bits_to_stream()
self._stream.write(data)
else:
# nothing to do here
if len(data) == 0:
return
bits = bytes_t... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def write_bits(self, bits):
"""Write the bits to the stream. Add the bits to the existing unflushed bits and write complete bytes to the stream. """ |
for bit in bits:
self._bits.append(bit)
while len(self._bits) >= 8:
byte_bits = [self._bits.popleft() for x in six.moves.range(8)]
byte = bits_to_bytes(byte_bits)
self._stream.write(byte) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def seek(self, pos, seek_type=0):
"""Seek to the specified position in the stream with seek_type. Unflushed bits will be discarded in the case of a seek. The str... |
self._bits.clear()
return self._stream.seek(pos, seek_type) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def size(self):
"""Return the size of the stream, or -1 if it cannot be determined. """ |
pos = self._stream.tell()
# seek to the end of the stream
self._stream.seek(0,2)
size = self._stream.tell()
self._stream.seek(pos, 0)
return size |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def unconsumed_ranges(self):
"""Return an IntervalTree of unconsumed ranges, of the format (start, end] with the end value not being included """ |
res = IntervalTree()
prev = None
# normal iteration is not in a predictable order
ranges = sorted([x for x in self.range_set], key=lambda x: x.begin)
for rng in ranges:
if prev is None:
prev = rng
continue
res.add(Interv... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _update_consumed_ranges(self, start_pos, end_pos):
"""Update the ``self.consumed_ranges`` array with which byte ranges have been consecutively consumed. """ |
self.range_set.add(Interval(start_pos, end_pos+1))
self.range_set.merge_overlaps() |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def _validate_markdown(self, expfile):
'''ensure that fields are present in markdown file'''
try:
import yaml
except:
bot.error('Python yaml is required for testing yml/markdown files.')
sys.exit(1)
self.metadata = {}
uid = os.path.basename(e... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def perform_checks(template,
do_redirect=False,
context=None,
next=None,
quiet=False):
'''return all checks for required variables before returning to
desired view
Parameters
==========
template: the html temp... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def pack_gzip(params, ctxt, scope, stream, coord):
"""``PackGZip`` - Concats the build output of all params and gzips the resulting data, returning a char array.... |
if len(params) == 0:
raise errors.InvalidArguments(coord, "{} args".format(len(params)), "at least one argument")
built = utils.binary("")
for param in params:
if isinstance(param, pfp.fields.Field):
built += param._pfp__build()
else:
built += param
... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def _validate_folder(self, folder=None):
''' validate folder takes a cloned github repo, ensures
the existence of the config.json, and validates it.
'''
from expfactory.experiment import load_experiment
if folder is None:
folder=os.path.abspath(os.getcwd())
... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def _validate_config(self, folder, validate_folder=True):
''' validate config is the primary validation function that checks
for presence and format of required fields.
Parameters
==========
:folder: full path to folder with config.json
:name: if provided, the folder... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def _read_runtime_vars(variable_file, sep=','):
'''read the entire runtime variable file, and return a list of lists,
each corresponding to a row. We also check the header, and exit
if anything is missing or malformed.
Parameters
==========
variable_file: full path to the tabula... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def _parse_row(row, sep=','):
'''parse row is a helper function to simply clean up a string, and parse
into a row based on a delimiter. If a required length is provided,
we check for this too.
'''
parsed = row.split(sep)
parsed = [x for x in parsed if x.strip()]
return parsed |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def superuser_required(view_func):
""" Decorator for views that checks that the user is logged in and is a staff member, displaying the login page if necessary. ... |
@wraps(view_func)
def _checklogin(request, *args, **kwargs):
if request.user.is_active and request.user.is_superuser:
# The user is valid. Continue to the admin page.
return view_func(request, *args, **kwargs)
assert hasattr(request, 'session'), "The Django admin requir... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def from_lines(cls, pattern_factory, lines):
""" Compiles the pattern lines. *pattern_factory* can be either the name of a registered pattern factory (:c... |
if isinstance(pattern_factory, string_types):
pattern_factory = util.lookup_pattern(pattern_factory)
if not callable(pattern_factory):
raise TypeError("pattern_factory:{!r} is not callable.".format(pattern_factory))
if isinstance(lines, (bytes, unicode)):
raise TypeError("lines:{!r} is not an iterable.... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def match_file(self, file, separators=None):
""" Matches the file to this path-spec. *file* (:class:`str`) is the file path to be matched against :attr:`... |
norm_file = util.normalize_file(file, separators=separators)
return util.match_file(self.patterns, norm_file) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def match_files(self, files, separators=None):
""" Matches the files to this path-spec. *files* (:class:`~collections.abc.Iterable` of :class:`str`) contai... |
if isinstance(files, (bytes, unicode)):
raise TypeError("files:{!r} is not an iterable.".format(files))
file_map = util.normalize_files(files, separators=separators)
matched_files = util.match_files(self.patterns, iterkeys(file_map))
for path in matched_files:
yield file_map[path] |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def match_tree(self, root, on_error=None, follow_links=None):
""" Walks the specified root path for all files and matches them to this path-spec. *root* ... |
files = util.iter_tree(root, on_error=on_error, follow_links=follow_links)
return self.match_files(files) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def pattern_to_regex(cls, pattern):
""" Convert the pattern into a regular expression. *pattern* (:class:`unicode` or :class:`bytes`) is the pattern to c... |
if isinstance(pattern, unicode):
return_type = unicode
elif isinstance(pattern, bytes):
return_type = bytes
pattern = pattern.decode(_BYTES_ENCODING)
else:
raise TypeError("pattern:{!r} is not a unicode or byte string.".format(pattern))
pattern = pattern.strip()
if pattern.startswith('#'):
#... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _translate_segment_glob(pattern):
""" Translates the glob pattern to a regular expression. This is used in the constructor to translate a path segment ... |
# NOTE: This is derived from `fnmatch.translate()` and is similar to
# the POSIX function `fnmatch()` with the `FNM_PATHNAME` flag set.
escape = False
regex = ''
i, end = 0, len(pattern)
while i < end:
# Get next character.
char = pattern[i]
i += 1
if escape:
# Escape the character.
e... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def pattern_to_regex(cls, *args, **kw):
""" Warn about deprecation. """ |
cls._deprecated()
return super(GitIgnorePattern, cls).pattern_to_regex(*args, **kw) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def iter_tree(root, on_error=None, follow_links=None):
""" Walks the specified directory for all files. *root* (:class:`str`) is the root directory to search ... |
if on_error is not None and not callable(on_error):
raise TypeError("on_error:{!r} is not callable.".format(on_error))
if follow_links is None:
follow_links = True
for file_rel in _iter_tree_next(os.path.abspath(root), '', {}, on_error, follow_links):
yield file_rel |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _iter_tree_next(root_full, dir_rel, memo, on_error, follow_links):
""" Scan the directory for all descendant files. *root_full* (:class:`str`) the absolut... |
dir_full = os.path.join(root_full, dir_rel)
dir_real = os.path.realpath(dir_full)
# Remember each encountered ancestor directory and its canonical
# (real) path. If a canonical path is encountered more than once,
# recursion has occurred.
if dir_real not in memo:
memo[dir_real] = dir_rel
else:
raise Recurs... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def match_file(patterns, file):
""" Matches the file to the patterns. *patterns* (:class:`~collections.abc.Iterable` of :class:`~pathspec.pattern.Pattern`) c... |
matched = False
for pattern in patterns:
if pattern.include is not None:
if file in pattern.match((file,)):
matched = pattern.include
return matched |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def match_files(patterns, files):
""" Matches the files to the patterns. *patterns* (:class:`~collections.abc.Iterable` of :class:`~pathspec.pattern.Pattern`)... |
all_files = files if isinstance(files, collection_type) else list(files)
return_files = set()
for pattern in patterns:
if pattern.include is not None:
result_files = pattern.match(all_files)
if pattern.include:
return_files.update(result_files)
else:
return_files.difference_update(result_files)
... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def normalize_files(files, separators=None):
""" Normalizes the file paths to use the POSIX path separator. *files* (:class:`~collections.abc.Iterable` of :cl... |
norm_files = {}
for path in files:
norm_files[normalize_file(path, separators=separators)] = path
return norm_files |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def register_pattern(name, pattern_factory, override=None):
""" Registers the specified pattern factory. *name* (:class:`str`) is the name to register the pat... |
if not isinstance(name, string_types):
raise TypeError("name:{!r} is not a string.".format(name))
if not callable(pattern_factory):
raise TypeError("pattern_factory:{!r} is not callable.".format(pattern_factory))
if name in _registered_patterns and not override:
raise AlreadyRegisteredError(name, _registered_... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def user_default_serializer(self, obj):
"""Convert a User to a cached instance representation.""" |
if not obj:
return None
self.user_default_add_related_pks(obj)
return dict((
('id', obj.id),
('username', obj.username),
self.field_to_json('DateTime', 'date_joined', obj.date_joined),
self.field_to_json(
'PKList', 'vot... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def user_default_loader(self, pk):
"""Load a User from the database.""" |
try:
obj = User.objects.get(pk=pk)
except User.DoesNotExist:
return None
else:
self.user_default_add_related_pks(obj)
return obj |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def user_default_add_related_pks(self, obj):
"""Add related primary keys to a User instance.""" |
if not hasattr(obj, '_votes_pks'):
obj._votes_pks = list(obj.votes.values_list('pk', flat=True)) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def group_default_invalidator(self, obj):
"""Invalidated cached items when the Group changes.""" |
user_pks = User.objects.values_list('pk', flat=True)
return [('User', pk, False) for pk in user_pks] |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def question_default_serializer(self, obj):
"""Convert a Question to a cached instance representation.""" |
if not obj:
return None
self.question_default_add_related_pks(obj)
return dict((
('id', obj.id),
('question_text', obj.question_text),
self.field_to_json('DateTime', 'pub_date', obj.pub_date),
self.field_to_json(
'PKLis... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def question_default_loader(self, pk):
"""Load a Question from the database.""" |
try:
obj = Question.objects.get(pk=pk)
except Question.DoesNotExist:
return None
else:
self.question_default_add_related_pks(obj)
return obj |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def question_default_add_related_pks(self, obj):
"""Add related primary keys to a Question instance.""" |
if not hasattr(obj, '_choice_pks'):
obj._choice_pks = list(obj.choices.values_list('pk', flat=True)) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def choice_default_serializer(self, obj):
"""Convert a Choice to a cached instance representation.""" |
if not obj:
return None
self.choice_default_add_related_pks(obj)
return dict((
('id', obj.id),
('choice_text', obj.choice_text),
self.field_to_json(
'PK', 'question', model=Question, pk=obj.question_id),
self.field_to_j... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def choice_default_loader(self, pk):
"""Load a Choice from the database.""" |
try:
obj = Choice.objects.get(pk=pk)
except Choice.DoesNotExist:
return None
else:
self.choice_default_add_related_pks(obj)
return obj |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def choice_default_add_related_pks(self, obj):
"""Add related primary keys to a Choice instance.""" |
if not hasattr(obj, '_voter_pks'):
obj._voter_pks = obj.voters.values_list('pk', flat=True) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def choice_default_invalidator(self, obj):
"""Invalidated cached items when the Choice changes.""" |
invalid = [('Question', obj.question_id, True)]
for pk in obj.voters.values_list('pk', flat=True):
invalid.append(('User', pk, False))
return invalid |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def cache(self):
"""Get the Django cache interface. This allows disabling the cache with settings.USE_DRF_INSTANCE_CACHE=False. It also delays import so that Dja... |
if not self._cache:
use_cache = getattr(settings, 'USE_DRF_INSTANCE_CACHE', True)
if use_cache:
from django.core.cache import cache
self._cache = cache
return self._cache |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def delete_all_versions(self, model_name, obj_pk):
"""Delete all versions of a cached instance.""" |
if self.cache:
for version in self.versions:
key = self.key_for(version, model_name, obj_pk)
self.cache.delete(key) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def model_function(self, model_name, version, func_name):
"""Return the model-specific caching function.""" |
assert func_name in ('serializer', 'loader', 'invalidator')
name = "%s_%s_%s" % (model_name.lower(), version, func_name)
return getattr(self, name) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def field_function(self, type_code, func_name):
"""Return the field function.""" |
assert func_name in ('to_json', 'from_json')
name = "field_%s_%s" % (type_code.lower(), func_name)
return getattr(self, name) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def field_to_json(self, type_code, key, *args, **kwargs):
"""Convert a field to a JSON-serializable representation.""" |
assert ':' not in key
to_json = self.field_function(type_code, 'to_json')
key_and_type = "%s:%s" % (key, type_code)
json_value = to_json(*args, **kwargs)
return key_and_type, json_value |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def field_from_json(self, key_and_type, json_value):
"""Convert a JSON-serializable representation back to a field.""" |
assert ':' in key_and_type
key, type_code = key_and_type.split(':', 1)
from_json = self.field_function(type_code, 'from_json')
value = from_json(json_value)
return key, value |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def get_instances(self, object_specs, version=None):
"""Get the cached native representation for one or more objects. Keyword arguments: object_specs - A sequenc... |
ret = dict()
spec_keys = set()
cache_keys = []
version = version or self.default_version
# Construct all the cache keys to fetch
for model_name, obj_pk, obj in object_specs:
assert model_name
assert obj_pk
# Get cache keys to fetch
... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def update_instance( self, model_name, pk, instance=None, version=None, update_only=False):
"""Create or update a cached instance. Keyword arguments are: model_n... |
versions = [version] if version else self.versions
invalid = []
for version in versions:
serializer = self.model_function(model_name, version, 'serializer')
loader = self.model_function(model_name, version, 'loader')
invalidator = self.model_function(
... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def field_date_to_json(self, day):
"""Convert a date to a date triple.""" |
if isinstance(day, six.string_types):
day = parse_date(day)
return [day.year, day.month, day.day] if day else None |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def field_datetime_from_json(self, json_val):
"""Convert a UTC timestamp to a UTC datetime.""" |
if type(json_val) == int:
seconds = int(json_val)
dt = datetime.fromtimestamp(seconds, utc)
elif json_val is None:
dt = None
else:
seconds, microseconds = [int(x) for x in json_val.split('.')]
dt = datetime.fromtimestamp(seconds, utc)
... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def field_timedelta_from_json(self, json_val):
"""Convert json_val to a timedelta object. json_val contains total number of seconds in the timedelta. If json_val... |
if isinstance(json_val, str):
return timedelta(seconds=float(json_val))
elif json_val is None:
return None
else:
return timedelta(seconds=json_val) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def field_timedelta_to_json(self, td):
"""Convert timedelta to value containing total number of seconds. If there are fractions of a second the return value will... |
if isinstance(td, six.string_types):
td = parse_duration(td)
if not td:
return None
if td.microseconds > 0:
return str(td.total_seconds())
else:
return int(td.total_seconds()) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def field_pklist_from_json(self, data):
"""Load a PkOnlyQueryset from a JSON dict. This uses the same format as cached_queryset_from_json """ |
model = get_model(data['app'], data['model'])
return PkOnlyQueryset(self, model, data['pks']) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def field_pklist_to_json(self, model, pks):
"""Convert a list of primary keys to a JSON dict. This uses the same format as cached_queryset_to_json """ |
app_label = model._meta.app_label
model_name = model._meta.model_name
return {
'app': app_label,
'model': model_name,
'pks': list(pks),
} |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def field_pk_from_json(self, data):
"""Load a PkOnlyModel from a JSON dict.""" |
model = get_model(data['app'], data['model'])
return PkOnlyModel(self, model, data['pk']) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def field_pk_to_json(self, model, pk):
"""Convert a primary key to a JSON dict.""" |
app_label = model._meta.app_label
model_name = model._meta.model_name
return {
'app': app_label,
'model': model_name,
'pk': pk,
} |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def choice_voters_changed_update_cache( sender, instance, action, reverse, model, pk_set, **kwargs):
"""Update cache when choice.voters changes.""" |
if action not in ('post_add', 'post_remove', 'post_clear'):
# post_clear is not handled, because clear is called in
# django.db.models.fields.related.ReverseManyRelatedObjects.__set__
# before setting the new order
return
if model == User:
assert type(instance) == Choic... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def post_delete_update_cache(sender, instance, **kwargs):
"""Update the cache when an instance is deleted.""" |
name = sender.__name__
if name in cached_model_names:
from .tasks import update_cache_for_instance
update_cache_for_instance(name, instance.pk, instance) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def post_save_update_cache(sender, instance, created, raw, **kwargs):
"""Update the cache when an instance is created or modified.""" |
if raw:
return
name = sender.__name__
if name in cached_model_names:
delay_cache = getattr(instance, '_delay_cache', False)
if not delay_cache:
from .tasks import update_cache_for_instance
update_cache_for_instance(name, instance.pk, instance) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def get_queryset(self):
"""Get the queryset for the action. If action is read action, return a CachedQueryset Otherwise, return a Django queryset """ |
queryset = super(CachedViewMixin, self).get_queryset()
if self.action in ('list', 'retrieve'):
return CachedQueryset(self.get_queryset_cache(), queryset=queryset)
else:
return queryset |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def get_object(self, queryset=None):
""" Return the object the view is displaying. Same as rest_framework.generics.GenericAPIView, but: - Failed assertions inste... |
# Determine the base queryset to use.
assert queryset is None, "Passing a queryset is disabled"
queryset = self.filter_queryset(self.get_queryset())
# Perform the lookup filtering.
lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
lookup = self.kwargs.get(lo... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def get_object_or_404(self, queryset, *filter_args, **filter_kwargs):
"""Return an object or raise a 404. Same as Django's standard shortcut, but make sure to ra... |
if isinstance(queryset, CachedQueryset):
try:
return queryset.get(*filter_args, **filter_kwargs)
except queryset.model.DoesNotExist:
raise Http404(
'No %s matches the given query.' % queryset.model)
else:
return get... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def update_cache_for_instance( model_name, instance_pk, instance=None, version=None):
"""Update the cache for an instance, with cascading updates.""" |
cache = SampleCache()
invalid = cache.update_instance(model_name, instance_pk, instance, version)
for invalid_name, invalid_pk, invalid_version in invalid:
update_cache_for_instance.delay(
invalid_name, invalid_pk, version=invalid_version) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def values_list(self, *args, **kwargs):
"""Return the primary keys as a list. The only valid call is values_list('pk', flat=True) """ |
flat = kwargs.pop('flat', False)
assert flat is True
assert len(args) == 1
assert args[0] == self.model._meta.pk.name
return self.pks |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def pks(self):
"""Lazy-load the primary keys.""" |
if self._primary_keys is None:
self._primary_keys = list(
self.queryset.values_list('pk', flat=True))
return self._primary_keys |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def count(self):
"""Return a count of instances.""" |
if self._primary_keys is None:
return self.queryset.count()
else:
return len(self.pks) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def filter(self, **kwargs):
"""Filter the base queryset.""" |
assert not self._primary_keys
self.queryset = self.queryset.filter(**kwargs)
return self |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def get(self, *args, **kwargs):
"""Return the single item from the filtered queryset.""" |
assert not args
assert list(kwargs.keys()) == ['pk']
pk = kwargs['pk']
model_name = self.model.__name__
object_spec = (model_name, pk, None)
instances = self.cache.get_instances((object_spec,))
try:
model_data = instances[(model_name, pk)][0]
... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def collect(cls):
""" Load all constant generators from settings.WEBPACK_CONSTANT_PROCESSORS and concat their values. """ |
constants = {}
for method_path in WebpackConstants.get_constant_processors():
method = import_string(method_path)
if not callable(method):
raise ImproperlyConfigured('Constant processor "%s" is not callable' % method_path)
result = method(constants... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def get_catalog(self, locale):
"""Create Django translation catalogue for `locale`.""" |
with translation.override(locale):
translation_engine = DjangoTranslation(locale, domain=self.domain, localedirs=self.paths)
trans_cat = translation_engine._catalog
trans_fallback_cat = translation_engine._fallback._catalog if translation_engine._fallback else {}
... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def get_paths(cls, packages):
"""Create list of matching packages for translation engine.""" |
allowable_packages = dict((app_config.name, app_config) for app_config in apps.get_app_configs())
app_configs = [allowable_packages[p] for p in packages if p in allowable_packages]
# paths of requested packages
return [os.path.join(app.path, 'locale') for app in app_configs] |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def get_catalogue_header_value(cls, catalog, key):
"""Get `.po` header value.""" |
header_value = None
if '' in catalog:
for line in catalog[''].split('\n'):
if line.startswith('%s:' % key):
header_value = line.split(':', 1)[1].strip()
return header_value |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _num_plurals(self, catalogue):
""" Return the number of plurals for this catalog language, or 2 if no plural string is available. """ |
match = re.search(r'nplurals=\s*(\d+)', self.get_plural(catalogue) or '')
if match:
return int(match.groups()[0])
return 2 |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def make_header(self, locale, catalog):
"""Populate header with correct data from top-most locale file.""" |
return {
"po-revision-date": self.get_catalogue_header_value(catalog, 'PO-Revision-Date'),
"mime-version": self.get_catalogue_header_value(catalog, 'MIME-Version'),
"last-translator": 'Automatic <hi@thorgate.eu>',
"x-generator": "Python",
"language": ... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def get_endpoint_obj(client, endpoint, object_id):
''' Tiny helper function that gets used all over the place to join the object ID to the endpoint and run a GET request, returning the result '''
endpoint = '/'.join([endpoint, str(object_id)])
return client.authenticated_request(endpoint).json() |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def _validate_response(self, method, response):
''' Helper method to validate the given to a Wunderlist API request is as expected '''
# TODO Fill this out using the error codes here: https://developer.wunderlist.com/documentation/concepts/formats
# The expected results can change based on API v... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def request(self, endpoint, method='GET', headers=None, params=None, data=None):
'''
Send a request to the given Wunderlist API endpoint
Params:
endpoint -- API endpoint to send request to
Keyword Args:
headers -- headers to add to the request
method -- GET, PUT... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def get_access_token(self, code, client_id, client_secret):
'''
Exchange a temporary code for an access token allowing access to a user's account
See https://developer.wunderlist.com/documentation/concepts/authorization for more info
'''
headers = {
'Content-Typ... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def authenticated_request(self, endpoint, method='GET', params=None, data=None):
'''
Send a request to the given Wunderlist API with 'X-Access-Token' and 'X-Client-ID' headers and ensure the response code is as expected given the request type
Params:
endpoint -- API endpoint to send req... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def update_list(self, list_id, revision, title=None, public=None):
''' Updates the list with the given ID to have the given title and public flag '''
return lists_endpoint.update_list(self, list_id, revision, title=title, public=public) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def get_tasks(self, list_id, completed=False):
''' Gets tasks for the list with the given ID, filtered by the given completion flag '''
return tasks_endpoint.get_tasks(self, list_id, completed=completed) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def create_task(self, list_id, title, assignee_id=None, completed=None, recurrence_type=None, recurrence_count=None, due_date=None, starred=None):
''' Creates a new task with the given information in the list with the given ID '''
return tasks_endpoint.create_task(self, list_id, title, assignee_id=assig... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def update_note(self, note_id, revision, content):
''' Updates the note with the given ID to have the given content '''
return notes_endpoint.update_note(self, note_id, revision, content) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def update_task_positions_obj(self, positions_obj_id, revision, values):
'''
Updates the ordering of tasks in the positions object with the given ID to the ordering in the given values.
See https://developer.wunderlist.com/documentation/endpoints/positions for more info
Return:
... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.