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 get_messages_from_stream(data):
"""Extract complete messages from stream and cut out them from stream. @data - stream binary data @return - [list of messages, choped stream data] """ |
messages = []
iterator = HEADER_RE.finditer(data)
last_pos = 0
for match in iterator:
pos = match.span()[0]
header = read_machine_header(data[pos:])
h_len = __get_machine_header_length(header)
cur_last_pos = pos + h_len + header['meta_len'] + header['data_len']
if cur_last_pos > len(data):
break
header, meta, bin_data = parse_message(data[pos:])
messages.append({'header': header, 'meta': meta, 'data': bin_data})
last_pos = cur_last_pos
data = data[last_pos:]
return messages, data |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def clone(self, options=None, attribute_options=None):
""" Returns a clone of this mapping that is configured with the given option and attribute option dictionaries. :param dict options: Maps representer options to their values. :param dict attribute_options: Maps attribute names to dictionaries mapping attribute options to their values. """ |
copied_cfg = self.__configurations[-1].copy()
upd_cfg = type(copied_cfg)(options=options,
attribute_options=attribute_options)
copied_cfg.update(upd_cfg)
return self.__class__(self.__mp_reg, self.__mapped_cls,
self.__de_cls, copied_cfg) |
<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(self, options=None, attribute_options=None):
""" Updates this mapping with the given option and attribute option maps. :param dict options: Maps representer options to their values. :param dict attribute_options: Maps attribute names to dictionaries mapping attribute options to their values. """ |
attr_map = self.__get_attribute_map(self.__mapped_cls, None, 0)
for attributes in attribute_options:
for attr_name in attributes:
if not attr_name in attr_map:
raise AttributeError('Trying to configure non-existing '
'resource attribute "%s"'
% (attr_name))
cfg = RepresenterConfiguration(options=options,
attribute_options=attribute_options)
self.configuration.update(cfg) |
<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_attribute_map(self, mapped_class=None, key=None):
""" Returns an ordered map of the mapped attributes for the given mapped class and attribute key. :param key: Tuple of attribute names specifying a path to a nested attribute in a resource tree. If this is not given, all attributes in this mapping will be returned. """ |
if mapped_class is None:
mapped_class = self.__mapped_cls
if key is None:
key = MappedAttributeKey(())
return OrderedDict([(attr.resource_attr, attr)
for attr in self._attribute_iterator(mapped_class,
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 create_data_element(self, mapped_class=None):
""" Returns a new data element for the given mapped class. :returns: object implementing :class:`IResourceDataElement`. """ |
if not mapped_class is None and mapped_class != self.__mapped_cls:
mp = self.__mp_reg.find_or_create_mapping(mapped_class)
data_el = mp.create_data_element()
else:
data_el = self.__de_cls.create()
return data_el |
<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_linked_data_element(self, url, kind, id=None, # pylint: disable=W0622 relation=None, title=None):
""" Returns a new linked data element for the given url and kind. :param str url: URL to assign to the linked data element. :param str kind: kind of the resource that is linked. One of the constantes defined by :class:`everest.constants.RESOURCE_KINDS`. :returns: object implementing :class:`ILinkedDataElement`. """ |
mp = self.__mp_reg.find_or_create_mapping(Link)
return mp.data_element_class.create(url, kind, id=id,
relation=relation, title=title) |
<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_data_element_from_resource(self, resource):
""" Returns a new data element for the given resource object. :returns: object implementing :class:`IResourceDataElement`. """ |
mp = self.__mp_reg.find_or_create_mapping(type(resource))
return mp.data_element_class.create_from_resource(resource) |
<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_linked_data_element_from_resource(self, resource):
""" Returns a new linked data element for the given resource object. :returns: object implementing :class:`ILinkedDataElement`. """ |
mp = self.__mp_reg.find_or_create_mapping(Link)
return mp.data_element_class.create_from_resource(resource) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def map_to_resource(self, data_element, resource=None):
""" Maps the given data element to a new resource or updates the given resource. :raises ValueError: If :param:`data_element` does not provide :class:`everest.representers.interfaces.IDataElement`. """ |
if not IDataElement.providedBy(data_element): # pylint:disable=E1101
raise ValueError('Expected data element, got %s.' % data_element)
if resource is None:
coll = \
create_staging_collection(data_element.mapping.mapped_class)
agg = coll.get_aggregate()
agg.add(data_element)
if IMemberDataElement.providedBy(data_element): # pylint: disable=E1101
ent = next(iter(agg))
resource = \
data_element.mapping.mapped_class.create_from_entity(ent)
else:
resource = coll
else:
resource.update(data_element)
return resource |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def map_to_data_element(self, resource):
""" Maps the given resource to a data element tree. """ |
trv = ResourceTreeTraverser(resource, self.as_pruning())
visitor = DataElementBuilderResourceTreeVisitor(self)
trv.run(visitor)
return visitor.data_element |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def push_configuration(self, configuration):
""" Pushes the given configuration object on the stack of configurations managed by this mapping and makes it the active configuration. """ |
self.__mapped_attr_cache.clear()
self.__configurations.append(configuration) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def pop_configuration(self):
""" Pushes the currently active configuration from the stack of configurations managed by this mapping. :raises IndexError: If there is only one configuration in the stack. """ |
if len(self.__configurations) == 1:
raise IndexError('Can not pop the last configuration from the '
'stack of configurations.')
self.__configurations.pop()
self.__mapped_attr_cache.clear() |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def with_updated_configuration(self, options=None, attribute_options=None):
""" Returns a context in which this mapping is updated with the given options and attribute options. """ |
new_cfg = self.__configurations[-1].copy()
if not options is None:
for o_name, o_value in iteritems_(options):
new_cfg.set_option(o_name, o_value)
if not attribute_options is None:
for attr_name, ao_opts in iteritems_(attribute_options):
for ao_name, ao_value in iteritems_(ao_opts):
new_cfg.set_attribute_option(attr_name, ao_name, ao_value)
# upd_cfg = type(new_cfg)(options=options,
# attribute_options=attribute_options)
# new_cfg.update(upd_cfg)
return MappingConfigurationContext(self, new_cfg) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _attribute_iterator(self, mapped_class, key):
""" Returns an iterator over the attributes in this mapping for the given mapped class and attribute key. If this is a pruning mapping, attributes that are ignored because of a custom configuration or because of the default ignore rules are skipped. """ |
for attr in \
itervalues_(self.__get_attribute_map(mapped_class, key, 0)):
if self.is_pruning:
do_ignore = attr.should_ignore(key)
else:
do_ignore = False
if not do_ignore:
yield attr |
<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_mapping(self, mapped_class, configuration=None):
""" Creates a new mapping for the given mapped class and representer configuration. :param configuration: configuration for the new data element class. :type configuration: :class:`RepresenterConfiguration` :returns: newly created instance of :class:`Mapping` """ |
cfg = self.__configuration.copy()
if not configuration is None:
cfg.update(configuration)
provided_ifcs = provided_by(object.__new__(mapped_class))
if IMemberResource in provided_ifcs:
base_data_element_class = self.member_data_element_base_class
elif ICollectionResource in provided_ifcs:
base_data_element_class = self.collection_data_element_base_class
elif IResourceLink in provided_ifcs:
base_data_element_class = self.linked_data_element_base_class
else:
raise ValueError('Mapped class for data element class does not '
'implement one of the required interfaces.')
name = "%s%s" % (mapped_class.__name__,
base_data_element_class.__name__)
de_cls = type(name, (base_data_element_class,), {})
mp = self.mapping_class(self, mapped_class, de_cls, cfg)
# Set the data element class' mapping.
# FIXME: This looks like a hack.
de_cls.mapping = mp
return mp |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def find_mapping(self, mapped_class):
""" Returns the mapping registered for the given mapped class or any of its base classes. Returns `None` if no mapping can be found. :param mapped_class: mapped type :type mapped_class: type :returns: instance of :class:`Mapping` or `None` """ |
if not self.__is_initialized:
self.__is_initialized = True
self._initialize()
mapping = None
for base_cls in mapped_class.__mro__:
try:
mapping = self.__mappings[base_cls]
except KeyError:
continue
else:
break
return mapping |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def eachMethod(decorator, methodFilter=lambda fName: True):
""" Class decorator that wraps every single method in its own method decorator methodFilter: a function which accepts a function name and should return True if the method is one which we want to decorate, False if we want to leave this method alone. methodFilter can also be simply a string prefix. If it is a string, it is assumed to be the prefix we're looking for. """ |
if isinstance(methodFilter, basestring):
# Is it a string? If it is, change it into a function that takes a string.
prefix = methodFilter
methodFilter = lambda fName: fName.startswith(prefix)
ismethod = lambda fn: inspect.ismethod(fn) or inspect.isfunction(fn)
def innerDeco(cls):
assert inspect.isclass(cls), "eachMethod is designed to be used only on classes"
for fName, fn in inspect.getmembers(cls):
if methodFilter(fName):
if ismethod(fn):
# We attempt to avoid decorating staticmethods by looking for an arg named cls
# or self; this is a kludge, but there's no other way to tell, and
# staticmethods do not work correctly with eachMethod
if getargspec(fn).args[0] not in ['cls', 'self']:
continue
setattr(cls, fName, decorator(fn))
return cls
return innerDeco |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _sibpath(path, sibling):
""" Return the path to a sibling of a file in the filesystem. This is useful in conjunction with the special C{__file__} attribute that Python provides for modules, so modules can load associated resource files. (Stolen from twisted.python.util) """ |
return os.path.join(os.path.dirname(os.path.abspath(path)), sibling) |
<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(cls, func):
""" Global cache decorator :param func: the function to be decorated :return: the decorator """ |
@functools.wraps(func)
def func_wrapper(*args, **kwargs):
func_key = cls.get_key(func)
val_cache = cls.get_cache(func_key)
lock = cls.get_cache_lock(func_key)
return cls._get_value_from_cache(
func, val_cache, lock, *args, **kwargs)
return func_wrapper |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def instance_cache(cls, func):
""" Save the cache to `self` This decorator take it for granted that the decorated function is a method. The first argument of the function is `self`. :param func: function to decorate :return: the decorator """ |
@functools.wraps(func)
def func_wrapper(*args, **kwargs):
if not args:
raise ValueError('`self` is not available.')
else:
the_self = args[0]
func_key = cls.get_key(func)
val_cache = cls.get_self_cache(the_self, func_key)
lock = cls.get_self_cache_lock(the_self, func_key)
return cls._get_value_from_cache(
func, val_cache, lock, *args, **kwargs)
return func_wrapper |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def clear_instance_cache(cls, func):
""" clear the instance cache Decorate a method of a class, the first parameter is supposed to be `self`. It clear all items cached by the `instance_cache` decorator. :param func: function to decorate """ |
@functools.wraps(func)
def func_wrapper(*args, **kwargs):
if not args:
raise ValueError('`self` is not available.')
else:
the_self = args[0]
cls.clear_self_cache(the_self)
return func(*args, **kwargs)
return func_wrapper |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def persisted(cls, seconds=0, minutes=0, hours=0, days=0, weeks=0):
""" Cache the return of the function for given time. Default to 1 day. :param weeks: as name :param seconds: as name :param minutes: as name :param hours: as name :param days: as name :return: return of the function decorated """ |
days += weeks * 7
hours += days * 24
minutes += hours * 60
seconds += minutes * 60
if seconds == 0:
# default to 1 day
seconds = 24 * 60 * 60
def get_persisted_file(hash_number):
folder = cls.get_persist_folder()
if not os.path.exists(folder):
os.makedirs(folder)
return os.path.join(folder, '{}.pickle'.format(hash_number))
def is_expired(filename):
if os.path.exists(filename):
file_age = cls.get_file_age(filename)
if file_age > seconds:
log.debug('persisted cache expired: {}'.format(filename))
ret = True
else:
ret = False
else:
ret = True
return ret
def decorator(func):
def func_wrapper(*args, **kwargs):
def _key_gen():
string = '{}-{}-{}-{}'.format(
func.__module__,
func.__name__,
args,
kwargs.items()
)
return hashlib.sha256(string.encode('utf-8')).hexdigest()
key = _key_gen()
persisted_file = get_persisted_file(key)
if is_expired(persisted_file):
ret = func(*args, **kwargs)
with open(persisted_file, 'wb') as f:
pickle.dump(ret, f)
else:
with open(persisted_file, 'rb') as f:
ret = pickle.load(f)
return ret
return func_wrapper
return decorator |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def getEvents(self, repo_user, repo_name, until_id=None):
"""Get all repository events, following paging, until the end or until UNTIL_ID is seen. Returns a Deferred.""" |
done = False
page = 0
events = []
while not done:
new_events = yield self.api.makeRequest(
['repos', repo_user, repo_name, 'events'],
page)
# terminate if we find a matching ID
if new_events:
for event in new_events:
if event['id'] == until_id:
done = True
break
events.append(event)
else:
done = True
page += 1
defer.returnValue(events) |
<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_project_path(cls, path):
"""Utility for finding a virtualenv location based on a project path""" |
path = vistir.compat.Path(path)
if path.name == 'Pipfile':
pipfile_path = path
path = path.parent
else:
pipfile_path = path / 'Pipfile'
pipfile_location = cls.normalize_path(pipfile_path)
venv_path = path / '.venv'
if venv_path.exists():
if not venv_path.is_dir():
possible_path = vistir.compat.Path(venv_path.read_text().strip())
if possible_path.exists():
return cls(possible_path.as_posix())
else:
if venv_path.joinpath('lib').exists():
return cls(venv_path.as_posix())
sanitized = re.sub(r'[ $`!*@"\\\r\n\t]', "_", path.name)[0:42]
hash_ = hashlib.sha256(pipfile_location.encode()).digest()[:6]
encoded_hash = base64.urlsafe_b64encode(hash_).decode()
hash_fragment = encoded_hash[:8]
venv_name = "{0}-{1}".format(sanitized, hash_fragment)
return cls(cls.get_workon_home().joinpath(venv_name).as_posix()) |
<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_setup_install_args(self, pkgname, setup_py, develop=False):
"""Get setup.py install args for installing the supplied package in the virtualenv :param str pkgname: The name of the package to install :param str setup_py: The path to the setup file of the package :param bool develop: Whether the package is in development mode :return: The installation arguments to pass to the interpreter when installing :rtype: list """ |
headers = self.base_paths["headers"]
headers = headers / "python{0}".format(self.python_version) / pkgname
install_arg = "install" if not develop else "develop"
return [
self.python, "-u", "-c", SETUPTOOLS_SHIM % setup_py, install_arg,
"--single-version-externally-managed",
"--install-headers={0}".format(self.base_paths["headers"]),
"--install-purelib={0}".format(self.base_paths["purelib"]),
"--install-platlib={0}".format(self.base_paths["platlib"]),
"--install-scripts={0}".format(self.base_paths["scripts"]),
"--install-data={0}".format(self.base_paths["data"]),
] |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def setuptools_install(self, chdir_to, pkg_name, setup_py_path=None, editable=False):
"""Install an sdist or an editable package into the virtualenv :param str chdir_to: The location to change to :param str setup_py_path: The path to the setup.py, if applicable defaults to None :param bool editable: Whether the package is editable, defaults to False """ |
install_options = ["--prefix={0}".format(self.prefix.as_posix()),]
with vistir.contextmanagers.cd(chdir_to):
c = self.run(
self.get_setup_install_args(pkg_name, setup_py_path, develop=editable) +
install_options, cwd=chdir_to
)
return c.returncode |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def install(self, req, editable=False, sources=[]):
"""Install a package into the virtualenv :param req: A requirement to install :type req: :class:`requirementslib.models.requirement.Requirement` :param bool editable: Whether the requirement is editable, defaults to False :param list sources: A list of pip sources to consult, defaults to [] :return: A return code, 0 if successful :rtype: int """ |
try:
packagebuilder = self.safe_import("packagebuilder")
except ImportError:
packagebuilder = None
with self.activated(include_extras=False):
if not packagebuilder:
return 2
ireq = req.as_ireq()
sources = self.filter_sources(req, sources)
cache_dir = os.environ.get('PASSA_CACHE_DIR',
os.environ.get(
'PIPENV_CACHE_DIR',
vistir.path.create_tracked_tempdir(prefix="passabuild")
)
)
built = packagebuilder.build.build(ireq, sources, cache_dir)
if isinstance(built, distlib.wheel.Wheel):
maker = distlib.scripts.ScriptMaker(None, None)
built.install(self.paths, maker)
else:
path = vistir.compat.Path(built.path)
cd_path = path.parent
setup_py = cd_path.joinpath("setup.py")
return self.setuptools_install(
cd_path.as_posix(), req.name, setup_py.as_posix(),
editable=req.editable
)
return 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 activated(self, include_extras=True, extra_dists=[]):
"""A context manager which activates the virtualenv. :param list extra_dists: Paths added to the context after the virtualenv is activated. This context manager sets the following environment variables: * `PYTHONUSERBASE` * `VIRTUAL_ENV` * `PYTHONIOENCODING` * `PYTHONDONTWRITEBYTECODE` In addition, it activates the virtualenv inline by calling `activate_this.py`. """ |
original_path = sys.path
original_prefix = sys.prefix
original_user_base = os.environ.get("PYTHONUSERBASE", None)
original_venv = os.environ.get("VIRTUAL_ENV", None)
parent_path = vistir.compat.Path(__file__).absolute().parent.parent.as_posix()
prefix = self.prefix.as_posix()
with vistir.contextmanagers.temp_environ(), vistir.contextmanagers.temp_path():
os.environ["PATH"] = os.pathsep.join([
vistir.compat.fs_str(self.scripts_dir),
vistir.compat.fs_str(self.prefix.as_posix()),
os.environ.get("PATH", "")
])
os.environ["PYTHONIOENCODING"] = vistir.compat.fs_str("utf-8")
os.environ["PYTHONDONTWRITEBYTECODE"] = vistir.compat.fs_str("1")
os.environ["PATH"] = self.base_paths["PATH"]
os.environ["PYTHONPATH"] = self.base_paths["PYTHONPATH"]
if self.is_venv:
os.environ["VIRTUAL_ENV"] = vistir.compat.fs_str(prefix)
sys.path = self.sys_path
sys.prefix = self.sys_prefix
site.addsitedir(self.base_paths["purelib"])
if include_extras:
site.addsitedir(parent_path)
extra_dists = list(self.extra_dists) + extra_dists
for extra_dist in extra_dists:
if extra_dist not in self.get_working_set():
extra_dist.activate(self.sys_path)
sys.modules["recursive_monkey_patch"] = self.recursive_monkey_patch
try:
yield
finally:
del os.environ["VIRTUAL_ENV"]
if original_user_base:
os.environ["PYTHONUSERBASE"] = original_user_base
if original_venv:
os.environ["VIRTUAL_ENV"] = original_venv
sys.path = original_path
sys.prefix = original_prefix
six.moves.reload_module(pkg_resources) |
<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_monkeypatched_pathset(self):
"""Returns a monkeypatched `UninstallPathset` for using to uninstall packages from the virtualenv :return: A patched `UninstallPathset` which enables uninstallation of venv packages :rtype: :class:`pip._internal.req.req_uninstall.UninstallPathset` """ |
from pip_shims.shims import InstallRequirement
# Determine the path to the uninstall module name based on the install module name
uninstall_path = InstallRequirement.__module__.replace(
"req_install", "req_uninstall"
)
req_uninstall = self.safe_import(uninstall_path)
self.recursive_monkey_patch.monkey_patch(
PatchedUninstaller, req_uninstall.UninstallPathSet
)
return req_uninstall.UninstallPathSet |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def uninstall(self, pkgname, *args, **kwargs):
"""A context manager which allows uninstallation of packages from the virtualenv :param str pkgname: The name of a package to uninstall cleaned = uninstaller.paths print("uninstalled packages: %s" % cleaned) """ |
auto_confirm = kwargs.pop("auto_confirm", True)
verbose = kwargs.pop("verbose", False)
with self.activated():
pathset_base = self.get_monkeypatched_pathset()
dist = next(
iter(filter(lambda d: d.project_name == pkgname, self.get_working_set())),
None
)
pathset = pathset_base.from_dist(dist)
if pathset is not None:
pathset.remove(auto_confirm=auto_confirm, verbose=verbose)
try:
yield pathset
except Exception as e:
if pathset is not None:
pathset.rollback()
else:
if pathset is not None:
pathset.commit()
if pathset is None:
return |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def getRootNode(nodes):
'''Return the node with the most children'''
max = 0
root = None
for i in nodes:
if len(i.children) > max:
max = len(i.children)
root = i
return root |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def getNextNode(nodes,usednodes,parent):
'''Get next node in a breadth-first traversal of nodes that have not been used yet'''
for e in edges:
if e.source==parent:
if e.target in usednodes:
x = e.target
break
elif e.target==parent:
if e.source in usednoes:
x = e.source
break
return x |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def calcPosition(self,parent_circle):
''' Position the circle tangent to the parent circle with the line connecting the centers of the two circles meeting the x axis at angle theta. '''
if r not in self:
raise AttributeError("radius must be calculated before position.")
if theta not in self:
raise AttributeError("theta must be set before position can be calculated.")
x_offset = math.cos(t_radians) * (parent_circle.r + self.r)
y_offset = math.sin(t_radians) * (parent_circle.r + self.r)
self.x = parent_circle.x + x_offset
self.y = parent_circle.y + y_offset |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def upload_progress(request):
""" Used by Ajax calls Return the upload progress and total length values """ |
if 'X-Progress-ID' in request.GET:
progress_id = request.GET['X-Progress-ID']
elif 'X-Progress-ID' in request.META:
progress_id = request.META['X-Progress-ID']
if progress_id:
cache_key = "%s_%s" % (request.META['REMOTE_ADDR'], progress_id)
data = cache.get(cache_key)
return HttpResponse(simplejson.dumps(data)) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def pre_save(self, model_instance, add):
"""Updates username created on ADD only.""" |
value = super(UserField, self).pre_save(model_instance, add)
if not value and not add:
# fall back to OS user if not accessing through browser
# better than nothing ...
value = self.get_os_username()
setattr(model_instance, self.attname, value)
return value
return 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 sys_toolbox_dir():
""" Returns this site-package esri toolbox directory. """ |
return os.path.join(os.path.dirname(os.path.abspath(__file__)), 'esri', 'toolboxes') |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def appdata_roaming_dir():
"""Returns the roaming AppData directory for the installed ArcGIS Desktop.""" |
install = arcpy.GetInstallInfo('desktop')
app_data = arcpy.GetSystemEnvironment("APPDATA")
product_dir = ''.join((install['ProductName'], major_version()))
return os.path.join(app_data, 'ESRI', product_dir) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def add_route(self, handler, uri, methods=frozenset({'GET'}), host=None,
strict_slashes=False):
'''A helper method to register class instance or
functions as a handler to the application url
routes.
:param handler: function or class instance
:param uri: path of the URL
:param methods: list or tuple of methods allowed, these are overridden
if using a HTTPMethodView
:param host:
:return: function or class instance
'''
stream = False
# Handle HTTPMethodView differently
if hasattr(handler, 'view_class'):
http_methods = (
'GET', 'POST', 'PUT', 'HEAD', 'OPTIONS', 'PATCH', 'DELETE')
methods = set()
for method in http_methods:
_handler = getattr(handler.view_class, method.lower(), None)
if _handler:
methods.add(method)
if hasattr(_handler, 'is_stream'):
stream = True
# handle composition view differently
if isinstance(handler, self.composition_view_class):
methods = handler.handlers.keys()
for _handler in handler.handlers.values():
if hasattr(_handler, 'is_stream'):
stream = True
break
self.route(uri=uri, methods=methods, host=host,
strict_slashes=strict_slashes, stream=stream)(handler)
return handler |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def middleware(self, middleware_or_request):
'''Decorate and register middleware to be called before a request.
Can either be called as @app.middleware or @app.middleware('request')
'''
def register_middleware(middleware, attach_to='request'):
if attach_to == 'request':
self.request_middleware.append(middleware)
if attach_to == 'response':
self.response_middleware.appendleft(middleware)
return middleware
# Detect which way this was called, @middleware or @middleware('AT')
if callable(middleware_or_request):
return register_middleware(middleware_or_request)
else:
return partial(register_middleware,
attach_to=middleware_or_request) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def static(self, uri, file_or_directory, pattern=r'/?.+',
use_modified_since=True, use_content_range=False):
'''Register a root to serve files from. The input can either be a
file or a directory. See
'''
static_register(self, uri, file_or_directory, pattern,
use_modified_since, use_content_range) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def url_for(self, view_name: str, **kwargs):
'''Build a URL based on a view name and the values provided.
In order to build a URL, all request parameters must be supplied as
keyword arguments, and each parameter must pass the test for the
specified parameter type. If these conditions are not met, a
`URLBuildError` will be thrown.
Keyword arguments that are not request parameters will be included in
the output URL's query string.
:param view_name: string referencing the view name
:param \*\*kwargs: keys and values that are used to build request
parameters and query string arguments.
:return: the built URL
Raises:
URLBuildError
'''
# find the route by the supplied view name
uri, route = self.router.find_route_by_view_name(view_name)
if not uri or not route:
raise URLBuildError(
'Endpoint with name `{}` was not found'.format(
view_name))
if uri != '/' and uri.endswith('/'):
uri = uri[:-1]
out = uri
# find all the parameters we will need to build in the URL
matched_params = re.findall(
self.router.parameter_pattern, uri)
# _method is only a placeholder now, don't know how to support it
kwargs.pop('_method', None)
anchor = kwargs.pop('_anchor', '')
# _external need SERVER_NAME in config or pass _server arg
external = kwargs.pop('_external', False)
scheme = kwargs.pop('_scheme', '')
if scheme and not external:
raise ValueError('When specifying _scheme, _external must be True')
netloc = kwargs.pop('_server', None)
if netloc is None and external:
netloc = self.config.get('SERVER_NAME', '')
for match in matched_params:
name, _type, pattern = self.router.parse_parameter_string(
match)
# we only want to match against each individual parameter
specific_pattern = '^{}$'.format(pattern)
supplied_param = None
if kwargs.get(name):
supplied_param = kwargs.get(name)
del kwargs[name]
else:
raise URLBuildError(
'Required parameter `{}` was not passed to url_for'.format(
name))
supplied_param = str(supplied_param)
# determine if the parameter supplied by the caller passes the test
# in the URL
passes_pattern = re.match(specific_pattern, supplied_param)
if not passes_pattern:
if _type != str:
msg = (
'Value "{}" for parameter `{}` does not '
'match pattern for type `{}`: {}'.format(
supplied_param, name, _type.__name__, pattern))
else:
msg = (
'Value "{}" for parameter `{}` '
'does not satisfy pattern {}'.format(
supplied_param, name, pattern))
raise URLBuildError(msg)
# replace the parameter in the URL with the supplied value
replacement_regex = '(<{}.*?>)'.format(name)
out = re.sub(
replacement_regex, supplied_param, out)
# parse the remainder of the keyword arguments into a querystring
query_string = urlencode(kwargs, doseq=True) if kwargs else ''
# scheme://netloc/path;parameters?query#fragment
out = urlunparse((scheme, netloc, out, '', query_string, anchor))
return out |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def i2osp(self, long_integer, block_size):
'Convert a long integer into an octet string.'
hex_string = '%X' % long_integer
if len(hex_string) > 2 * block_size:
raise ValueError('integer %i too large to encode in %i octets' % (long_integer, block_size))
return a2b_hex(hex_string.zfill(2 * block_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 random_output(self, max=100):
""" Generate a list of elements from the markov chain. The `max` value is in place in order to prevent excessive iteration. """ |
output = []
item1 = item2 = MarkovChain.START
for i in range(max-3):
item3 = self[(item1, item2)].roll()
if item3 is MarkovChain.END:
break
output.append(item3)
item1 = item2
item2 = item3
return output |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def start(self):
""" Start the periodic runner """ |
if self._isRunning:
return
if self._cease.is_set():
self._cease.clear() # restart
class Runner(threading.Thread):
@classmethod
def run(cls):
nextRunAt = cls.setNextRun()
while not self._cease.is_set():
if datetime.now() >= nextRunAt:
self._run()
nextRunAt = cls.setNextRun()
@classmethod
def setNextRun(cls):
# return datetime.now() + timedelta(seconds=self._interval)
return self._interval.nextRunAt()
runner = Runner()
runner.setDaemon(True)
runner.start()
self._isRunning = 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 useThis(self, *args, **kwargs):
""" Change parameter of the callback function. :param *args, **kwargs: parameter(s) to use when executing the callback function. """ |
self._callback = functools.partial(self._callback, *args, **kwargs) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def stop(self):
""" Stop the periodic runner """ |
self._cease.set()
time.sleep(0.1) # let the thread closing correctly.
self._isRunning = False |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def condition(self) -> bool:
""" check JWT, then check session for validity """ |
jwt = JWT()
if jwt.verify_http_auth_token():
if not current_app.config['AUTH']['FAST_SESSIONS']:
session = SessionModel.where_session_id(
jwt.data['session_id'])
if session is None:
return False
Session.set_current_session(jwt.data['session_id'])
return True
return False |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def render_hidden(name, value):
""" render as hidden widget """ |
if isinstance(value, list):
return MultipleHiddenInput().render(name, value)
return HiddenInput().render(name, 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 next_task(self, item, raise_exceptions=None, **kwargs):
"""Deserializes all transactions for this batch and archives the file. """ |
filename = os.path.basename(item)
batch = self.get_batch(filename)
tx_deserializer = self.tx_deserializer_cls(
allow_self=self.allow_self, override_role=self.override_role
)
try:
tx_deserializer.deserialize_transactions(
transactions=batch.saved_transactions
)
except (DeserializationError, TransactionDeserializerError) as e:
raise TransactionsFileQueueError(e) from e
else:
batch.close()
self.archive(filename) |
<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_batch(self, filename=None):
"""Returns a batch instance given the filename. """ |
try:
history = self.history_model.objects.get(filename=filename)
except self.history_model.DoesNotExist as e:
raise TransactionsFileQueueError(
f"Batch history not found for '{filename}'."
) from e
if history.consumed:
raise TransactionsFileQueueError(
f"Batch closed for '{filename}'. Got consumed=True"
)
batch = self.batch_cls()
batch.batch_id = history.batch_id
batch.filename = history.filename
return batch |
<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_items(self, html):
""" Get state, county, zipcode, address code from lists page. Example: target url: http://www.zillow.com/browse/homes/md/ <<<<<<< HEAD ======= data: [(href, name)] """ |
captcha_patterns = [
"https://www.google.com/recaptcha/api.js", "I'm not a robot"]
for captcha_pattern in captcha_patterns:
if captcha_pattern in html:
raise exc.CaptchaError("Found %r in html!" % captcha_pattern)
data = list()
soup = self.to_soup(html)
div = soup.find("div", class_="zsg-lg-1-2 zsg-sm-1-1")
for li in div.find_all("li"):
a = li.find_all("a")[0]
href = a["href"]
name = a.text.strip()
data.append((href, name))
return data |
<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_time(self, loc4d=None):
""" Based on a Location4D object and this Diel object, calculate the time at which this Diel migration is actually happening """ |
if loc4d is None:
raise ValueError("Location4D object can not be None")
if self.pattern == self.PATTERN_CYCLE:
c = SunCycles.cycles(loc=loc4d)
if self.cycle == self.CYCLE_SUNRISE:
r = c[SunCycles.RISING]
elif self.cycle == self.CYCLE_SUNSET:
r = c[SunCycles.SETTING]
td = timedelta(hours=self.time_delta)
if self.plus_or_minus == self.HOURS_PLUS:
r = r + td
elif self.plus_or_minus == self.HOURS_MINUS:
r = r - td
return r
elif self.pattern == self.PATTERN_SPECIFICTIME:
return self._time.replace(year=loc4d.time.year, month=loc4d.time.month, day=loc4d.time.day) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def move(self, particle, u, v, w, modelTimestep, **kwargs):
# If the particle is settled, don't move it anywhere if particle.settled: return { 'u': 0, 'v': 0, 'w': 0 } # If the particle is halted (but not settled), don't move it anywhere if particle.halted: return { 'u': 0, 'v': 0, 'w': 0 } # How far could I move? We don't want to overshoot our desired depth. vertical_potential = w * modelTimestep """ This only works if min is less than max. No checks are done here, so it should be done before calling this function. """ |
""" I'm below my desired max depth, so i need to go down
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-------------------------------------- min
-------------------------------------- max
x me
______________________________________
"""
if particle.location.depth < self.max_depth:
logger.debug("DIEL: %s - Moving UP to desired depth from %f" % (self.logstring(), particle.location.depth))
# If we are going to overshoot the desired minimum depth,
# calculate a new w to land in the middle of the range.
overshoot_distance = abs(particle.location.depth - self.min_depth)
if overshoot_distance < abs(vertical_potential):
halfway_distance = abs((self.max_depth - self.min_depth) / 2)
w = ((overshoot_distance - halfway_distance) / modelTimestep)
return { 'u': u, 'v': v, 'w': w }
""" I'm above my desired min depth, so i need to go down
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
x me
-------------------------------------- min
-------------------------------------- max
______________________________________
"""
if particle.location.depth > self.min_depth:
logger.debug("DIEL: %s - Moving DOWN to desired depth from %f" % (self.logstring(), particle.location.depth))
# If we are going to overshoot the desired maximum depth,
# calculate a new w to land in the middle of the range.
overshoot_distance = abs(particle.location.depth - self.max_depth)
if overshoot_distance < abs(vertical_potential):
halfway_distance = abs((self.max_depth - self.min_depth) / 2)
w = ((overshoot_distance - halfway_distance) / modelTimestep)
return { 'u': u, 'v': v, 'w': -w }
""" I'm in my desired depth, so I'm just gonna chill here
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-------------------------------------- min
x me
-------------------------------------- max
______________________________________
"""
return { 'u': u, 'v': v, 'w': 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 make_relationship_aggregate(self, relationship):
""" Returns a new relationship aggregate for the given relationship. :param relationship: Instance of :class:`everest.entities.relationship.DomainRelationship`. """ |
if not self._session.IS_MANAGING_BACKREFERENCES:
relationship.direction &= ~RELATIONSHIP_DIRECTIONS.REVERSE
return RelationshipAggregate(self, relationship) |
<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_new(self, entity_class, entity):
""" Registers the given entity for the given class as NEW. :raises ValueError: If the given entity already holds state that was created by another Unit Of Work. """ |
EntityState.manage(entity, self)
EntityState.get_state(entity).status = ENTITY_STATUS.NEW
self.__entity_set_map[entity_class].add(entity) |
<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_clean(self, entity_class, entity):
""" Registers the given entity for the given class as CLEAN. :returns: Cloned entity. """ |
EntityState.manage(entity, self)
EntityState.get_state(entity).status = ENTITY_STATUS.CLEAN
self.__entity_set_map[entity_class].add(entity) |
<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_deleted(self, entity_class, entity):
""" Registers the given entity for the given class as DELETED. :raises ValueError: If the given entity already holds state that was created by another Unit Of Work. """ |
EntityState.manage(entity, self)
EntityState.get_state(entity).status = ENTITY_STATUS.DELETED
self.__entity_set_map[entity_class].add(entity) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def unregister(self, entity_class, entity):
""" Unregisters the given entity for the given class and discards its state information. """ |
EntityState.release(entity, self)
self.__entity_set_map[entity_class].remove(entity) |
<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_marked_new(self, entity):
""" Checks if the given entity is marked with status NEW. Returns `False` if the entity has no state information. """ |
try:
result = EntityState.get_state(entity).status == ENTITY_STATUS.NEW
except ValueError:
result = False
return result |
<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_marked_deleted(self, entity):
""" Checks if the given entity is marked with status DELETED. Returns `False` if the entity has no state information. """ |
try:
result = EntityState.get_state(entity).status \
== ENTITY_STATUS.DELETED
except ValueError:
result = False
return result |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def mark_clean(self, entity):
""" Marks the given entity as CLEAN. This is done when an entity is loaded fresh from the repository or after a commit. """ |
state = EntityState.get_state(entity)
state.status = ENTITY_STATUS.CLEAN
state.is_persisted = 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 iterator(self):
""" Returns an iterator over all entity states held by this Unit Of Work. """ |
# FIXME: There is no dependency tracking; objects are iterated in
# random order.
for ent_cls in list(self.__entity_set_map.keys()):
for ent in self.__entity_set_map[ent_cls]:
yield EntityState.get_state(ent) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def file_save(self, name, filename=None, folder="", keep_ext=True) -> bool:
""" Easy save of a file """ |
if name in self.files:
file_object = self.files[name]
clean_filename = secure_filename(file_object.filename)
if filename is not None and keep_ext:
clean_filename = filename + ".%s" % \
(clean_filename.rsplit('.', 1)[1].lower())
elif filename is not None and not keep_ext:
clean_filename = filename
file_object.save(os.path.join(
current_app.config['UPLOADS']['FOLDER'],
folder, clean_filename))
return 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 parse(self, fail_callback=None):
""" Parse text fields and file fields for values and files """ |
# get text fields
for field in self.field_arguments:
self.values[field['name']] = self.__get_value(field['name'])
if self.values[field['name']] is None and field['required']:
if fail_callback is not None:
fail_callback()
self.__invalid_request(field['error'])
# get file fields
for file in self.file_arguments:
self.files[file['name']] = self.__get_file(file)
if self.files[file['name']] is None and file['required']:
if fail_callback is not None:
fail_callback()
self.__invalid_request(file['error']) |
<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, field_name):
""" Get request Json value by field name """ |
value = request.values.get(field_name)
if value is None:
if self.json_form_data is None:
value = None
elif field_name in self.json_form_data:
value = self.json_form_data[field_name]
return 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_file(self, file):
""" Get request file and do a security check """ |
file_object = None
if file['name'] in request.files:
file_object = request.files[file['name']]
clean_filename = secure_filename(file_object.filename)
if clean_filename == '':
return file_object
if file_object and self.__allowed_extension(
clean_filename, file['extensions']):
return file_object
elif file['name'] not in request.files and file['required']:
return file_object
return file_object |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def __allowed_extension(self, filename, extensions):
""" Check allowed file extensions """ |
allowed_extensions = current_app.config['UPLOADS']['EXTENSIONS']
if extensions is not None:
allowed_extensions = extensions
return '.' in filename and filename.rsplit('.', 1)[1].lower() in \
allowed_extensions |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def __invalid_request(self, error):
""" Error response on failure """ |
# TODO: make this modifiable
error = {
'error': {
'message': error
}
}
abort(JsonResponse(status_code=400, data=error)) |
<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_field(self, offset, length, format):
"""Returns unpacked Python struct array. Args: offset (int):
offset to byte array within structure length (int):
how many bytes to unpack format (str):
Python struct format string for unpacking See Also: https://docs.python.org/2/library/struct.html#format-characters """ |
return struct.unpack(format, self.data[offset:offset + length])[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 export(self, filename, offset=0, length=None):
"""Exports byte array to specified destination Args: filename (str):
destination to output file offset (int):
byte offset (default: 0) """ |
self.__validate_offset(filename=filename, offset=offset, length=length)
with open(filename, 'w') as f:
if length is None:
length = len(self.data) - offset
if offset > 0:
output = self.data[offset:length]
else:
output = self.data[:length]
f.write(output) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def tmpdir():
""" Create a tempdir context for the cwd and remove it after. """ |
target = None
try:
with _tmpdir_extant() as target:
yield target
finally:
if target is not None:
shutil.rmtree(target, ignore_errors=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 run(command, verbose=False):
""" Run a shell command. Capture the stdout and stderr as a single stream. Capture the status code. If verbose=True, then print command and the output to the terminal as it comes in. """ |
def do_nothing(*args, **kwargs):
return None
v_print = print if verbose else do_nothing
p = subprocess.Popen(
command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
)
v_print("run:", command)
def log_and_yield(line):
if six.PY2:
# If not unicode, try to decode it first
if isinstance(line, str):
line = line.decode('utf8', 'replace')
v_print(line)
return line
output = ''.join(map(log_and_yield, p.stdout))
status_code = p.wait()
return CommandResult(command, output, status_code) |
<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_lxc_version():
""" Asks the current host what version of LXC it has. Returns it as a string. If LXC is not installed, raises subprocess.CalledProcessError""" |
runner = functools.partial(
subprocess.check_output,
stderr=subprocess.STDOUT,
universal_newlines=True,
)
# Old LXC had an lxc-version executable, and prefixed its result with
# "lxc version: "
try:
result = runner(['lxc-version']).rstrip()
return parse_version(result.replace("lxc version: ", ""))
except (OSError, subprocess.CalledProcessError):
pass
# New LXC instead has a --version option on most installed executables.
return parse_version(runner(['lxc-start', '--version']).rstrip()) |
<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_qpimage(self, idx=0):
"""Return background-corrected QPImage""" |
if self._bgdata:
# The user has explicitly chosen different background data
# using `get_qpimage_raw`.
qpi = super(SingleHdf5Qpimage, self).get_qpimage()
else:
# We can use the background data stored in the qpimage hdf5 file
qpi = qpimage.QPImage(h5file=self.path,
h5mode="r",
h5dtype=self.as_type,
).copy()
# Force meta data
for key in self.meta_data:
qpi[key] = self.meta_data[key]
# set identifier
qpi["identifier"] = self.get_identifier(idx)
return qpi |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def simulate(s0, transmat, steps=1):
"""Simulate the next state Parameters s0 : ndarray Vector with state variables at t=0 transmat : ndarray The estimated transition/stochastic matrix. steps : int (Default: 1) The number of steps to simulate model outputs ahead. If steps>1 the a Mult-Step Simulation is triggered. Returns ------- out : ndarray (steps=1) Vector with simulated state variables (). (steps>1) Matrix with out[:,step] columns (Fortran order) from a Multi-Step Simulation. The first column is the initial state vector out[:,0]=s0 for algorithmic reasons. """ |
# Single-Step simulation
if steps == 1:
return np.dot(s0, transmat)
# Multi-Step simulation
out = np.zeros(shape=(steps + 1, len(s0)), order='C')
out[0, :] = s0
for i in range(1, steps + 1):
out[i, :] = np.dot(out[i - 1, :], transmat)
return out |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def verify(path):
"""Verify that `path` has a supported numpy file format""" |
path = pathlib.Path(path)
valid = False
if path.suffix == ".npy":
try:
nf = np.load(str(path), mmap_mode="r", allow_pickle=False)
except (OSError, ValueError, IsADirectoryError):
pass
else:
if len(nf.shape) == 2:
valid = True
return valid |
<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_stem(self):
""" Return the name of the file without it's extension. """ |
filename = os.path.basename(self.src_path)
stem, ext = os.path.splitext(filename)
return "index" if stem in ("index", "README", "__init__") else stem |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def type_check(thetype, data, bindings = None):
""" Checks that a given bit of data conforms to the type provided """ |
if not bindings: bindings = Bindings()
if isinstance(thetype, core.RecordType):
for name,child in zip(thetype.child_names, thetype.child_types):
value = data[name]
type_check(child, value, bindings)
elif isinstance(thetype, core.TupleType):
assert isinstance(data, tuple)
assert len(data) == len(thetype.child_types)
for value,child_type in zip(data, thetype.child_types):
type_check(child_type, value, bindings)
elif isinstance(thetype, core.UnionType):
assert isinstance(thetype, dict)
children = [(name,child) for name,child in zip(thetype.child_names, thetype.child_types) if name in data]
assert len(fields) == 1, "0 or more than 1 entry in Union"
child_name,child_type = children[0]
type_check(child_type, data[child_name], bindings)
elif isinstance(thetype, core.TypeApp):
# Type applications are tricky. These will "affect" bindings
bindings.push()
for k,v in thetype.param_values.items():
bindings[k] = v
type_check(thetype.root_type, data, bindings)
bindings.pop()
elif isinstance(thetype, core.TypeVar):
# Find the binding for this type variable
bound_type = bindings[thetype.name]
if bound_type is None:
raise errors.ValidationError("TypeVar(%s) is not bound to a type." % thetype.name)
type_check(bound_type, data, bindings)
elif isinstance(thetype, core.NativeType):
# Native types are interesting - these can be plain types such as Int, Float etc
# or they can be generic types like Array<T>, Map<K,V>
# While plain types are fine, generic types (ie native types with args) pose a problem.
# How do we perform type checking on "contents" of the data given native types.
# We need native types to be able to apply mapper functions on data as they see fit.
# So to deal with custom validations on native types we need
# native types to expose mapper functors for us!!!
if thetype.args and thetype.mapper_functor:
def type_check_functor(*values):
for arg, value in zip(thetype.args, values):
bound_type = bindings[arg]
if bound_type is None:
raise errors.ValidationError("Arg(%s) is not bound to a type." % arg)
type_check(bound_type, value)
thetype.mapper_functor(type_check_functor, data)
# Finally apply any other validators that were nominated
# specifically for that particular type
if thetype.validator:
thetype.validator(thetype, data, bindings) |
<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_setup(filepath):
'''Get the kwargs from the setup function in setup.py'''
# TODO: Need to parse setup.cfg and merge with the data from below
# Monkey patch setuptools.setup to capture keyword arguments
setup_kwargs = {}
def setup_interceptor(**kwargs):
setup_kwargs.update(kwargs)
import setuptools
setuptools_setup = setuptools.setup
setuptools.setup = setup_interceptor
# Manually compile setup.py
with open(filepath, 'r') as f:
code = compile(f.read(), '', 'exec')
setup = ModuleType('setup')
exec(code, setup.__dict__)
# Remove monkey patch
setuptools.setup = setuptools_setup
return setup_kwargs |
<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_console_scripts(setup_data):
'''Parse and return a list of console_scripts from setup_data'''
# TODO: support ini format of entry_points
# TODO: support setup.cfg entry_points as available in pbr
if 'entry_points' not in setup_data:
return []
console_scripts = setup_data['entry_points'].get('console_scripts', [])
return [script.split('=')[0].strip() for script in console_scripts] |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def install_programmer(programmer_id, programmer_options, replace_existing=False):
"""install programmer in programmers.txt. :param programmer_id: string identifier :param programmer_options: dict like :param replace_existing: bool :rtype: None """ |
doaction = 0
if programmer_id in programmers().keys():
log.debug('programmer already exists: %s', programmer_id)
if replace_existing:
log.debug('remove programmer: %s', programmer_id)
remove_programmer(programmer_id)
doaction = 1
else:
doaction = 1
if doaction:
lines = bunch2properties(programmer_id, programmer_options)
programmers_txt().write_lines([''] + lines, append=1) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def schedule(self, job, when):
""" Schedule job to run at when nanoseconds since the UNIX epoch.""" |
pjob = pickle.dumps(job)
self._redis.zadd('ss:scheduled', when, pjob) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def schedule_in(self, job, timedelta):
""" Schedule job to run at datetime.timedelta from now.""" |
now = long(self._now() * 1e6)
when = now + timedelta.total_seconds() * 1e6
self.schedule(job, when) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def schedule_now(self, job):
""" Schedule job to run as soon as possible.""" |
now = long(self._now() * 1e6)
self.schedule(job, now) |
<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_aria_autocomplete(self, field):
""" Returns the appropriate value for attribute aria-autocomplete of field. :param field: The field. :type field: hatemile.util.html.htmldomelement.HTMLDOMElement :return: The ARIA value of field. :rtype: str """ |
tag_name = field.get_tag_name()
input_type = None
if field.has_attribute('type'):
input_type = field.get_attribute('type').lower()
if (
(tag_name == 'TEXTAREA')
or (
(tag_name == 'INPUT')
and (not (
(input_type == 'button')
or (input_type == 'submit')
or (input_type == 'reset')
or (input_type == 'image')
or (input_type == 'file')
or (input_type == 'checkbox')
or (input_type == 'radio')
or (input_type == 'hidden')
))
)
):
value = None
if field.has_attribute('autocomplete'):
value = field.get_attribute('autocomplete').lower()
else:
form = self.parser.find(field).find_ancestors(
'form'
).first_result()
if (form is None) and (field.has_attribute('form')):
form = self.parser.find(
'#' + field.get_attribute('form')
).first_result()
if (form is not None) and (form.has_attribute('autocomplete')):
value = form.get_attribute('autocomplete').lower()
if value == 'on':
return 'both'
elif (
(field.has_attribute('list'))
and (self.parser.find(
'datalist[id="' + field.get_attribute('list') + '"]'
).first_result() is not None)
):
return 'list'
elif value == 'off':
return 'none'
return 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 _validate(self, field, list_attribute):
""" Validate the field when its value change. :param field: The field. :param list_attribute: The list attribute of field with validation. """ |
if not self.scripts_added:
self._generate_validation_scripts()
self.id_generator.generate_id(field)
self.script_list_fields_with_validation.append_text(
'hatemileValidationList.'
+ list_attribute
+ '.push("'
+ field.get_attribute('id')
+ '");'
) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def remove(item):
""" Delete item, whether it's a file, a folder, or a folder full of other files and folders. """ |
if os.path.isdir(item):
shutil.rmtree(item)
else:
# Assume it's a file. error if not.
os.remove(item) |
<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_slugignores(root, fname='.slugignore'):
""" Given a root path, read any .slugignore file inside and return a list of patterns that should be removed prior to slug compilation. Return empty list if file does not exist. """ |
try:
with open(os.path.join(root, fname)) as f:
return [l.rstrip('\n') for l in f]
except IOError:
return [] |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def clean_slug_dir(root):
""" Given a path, delete anything specified in .slugignore. """ |
if not root.endswith('/'):
root += '/'
for pattern in get_slugignores(root):
print("pattern", pattern)
remove_pattern(root, pattern) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def export(self, folder_path, format=None):
""" General purpose export method, gets file type from filepath extension Valid output formats currently are: Trackline: trackline or trkl or *.trkl Shapefile: shapefile or shape or shp or *.shp NetCDF: netcdf or nc or *.nc """ |
if format is None:
raise ValueError("Must export to a specific format, no format specified.")
format = format.lower()
if format == "trackline" or format[-4:] == "trkl":
ex.Trackline.export(folder=folder_path, particles=self.particles, datetimes=self.datetimes)
elif format == "shape" or format == "shapefile" or format[-3:] == "shp":
ex.GDALShapefile.export(folder=folder_path, particles=self.particles, datetimes=self.datetimes)
elif format == "netcdf" or format[-2:] == "nc":
ex.NetCDF.export(folder=folder_path, particles=self.particles, datetimes=self.datetimes, summary=str(self))
elif format == "pickle" or format[-3:] == "pkl" or format[-6:] == "pickle":
ex.Pickle.export(folder=folder_path, particles=self.particles, datetimes=self.datetimes) |
<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(args):
"""Parse passed arguments from shell.""" |
ordered = []
opt_full = dict()
opt_abbrev = dict()
args = args + [''] # Avoid out of range
i = 0
while i < len(args) - 1:
arg = args[i]
arg_next = args[i+1]
if arg.startswith('--'):
if arg_next.startswith('-'):
raise ValueError('{} lacks value'.format(arg))
else:
opt_full[arg[2:]] = arg_next
i += 2
elif arg.startswith('-'):
if arg_next.startswith('-'):
raise ValueError('{} lacks value'.format(arg))
else:
opt_abbrev[arg[1:]] = arg_next
i += 2
else:
ordered.append(arg)
i += 1
return ordered, opt_full, opt_abbrev |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _construct_optional(params):
"""Construct optional args' key and abbreviated key from signature.""" |
args = []
filtered = {key: arg.default for key, arg in params.items() if arg.default != inspect._empty}
for key, default in filtered.items():
arg = OptionalArg(full=key, abbrev=key[0].lower(), default=default)
args.append(arg)
args_full, args_abbrev = dict(), dict()
# Resolve conflicts
known_count = defaultdict(int)
for arg in args:
args_full[arg.full] = arg
if known_count[arg.abbrev] == 0:
args_abbrev[arg.abbrev] = arg
elif known_count[arg.abbrev] == 1:
new_abbrev = arg.abbrev.upper()
args_full[arg.full] = OptionalArg(full=arg.full, abbrev=new_abbrev, default=arg.default)
args_abbrev[new_abbrev] = args_full[arg.full]
else:
new_abbrev = arg.abbrev.upper() + str(known_count[arg.abbrev])
args_full[arg.full] = OptionalArg(full=arg.full, abbrev=new_abbrev, default=arg.default)
args_abbrev[new_abbrev] = args_full[arg.full]
known_count[arg.abbrev] += 1
return args_full, args_abbrev |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _keyboard_access(self, element):
""" Provide keyboard access for element, if it not has. :param element: The element. :type element: hatemile.util.html.htmldomelement.HTMLDOMElement """ |
# pylint: disable=no-self-use
if not element.has_attribute('tabindex'):
tag = element.get_tag_name()
if (tag == 'A') and (not element.has_attribute('href')):
element.set_attribute('tabindex', '0')
elif (
(tag != 'A')
and (tag != 'INPUT')
and (tag != 'BUTTON')
and (tag != 'SELECT')
and (tag != 'TEXTAREA')
):
element.set_attribute('tabindex', '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 _add_event_in_element(self, element, event):
""" Add a type of event in element. :param element: The element. :type element: hatemile.util.html.htmldomelement.HTMLDOMElement :param event: The type of event. :type event: str """ |
if not self.main_script_added:
self._generate_main_scripts()
if self.script_list is not None:
self.id_generator.generate_id(element)
self.script_list.append_text(
event
+ "Elements.push('"
+ element.get_attribute('id')
+ "');"
) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def tee(process, filter):
"""Read lines from process.stdout and echo them to sys.stdout. Returns a list of lines read. Lines are not newline terminated. The 'filter' is a callable which is invoked for every line, receiving the line as argument. If the filter returns True, the line is echoed to sys.stdout. """ |
lines = []
while True:
line = process.stdout.readline()
if line:
if sys.version_info[0] >= 3:
line = decode(line)
stripped_line = line.rstrip()
if filter(stripped_line):
sys.stdout.write(line)
lines.append(stripped_line)
elif process.poll() is not None:
process.stdout.close()
break
return lines |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def tee2(process, filter):
"""Read lines from process.stderr and echo them to sys.stderr. The 'filter' is a callable which is invoked for every line, receiving the line as argument. If the filter returns True, the line is echoed to sys.stderr. """ |
while True:
line = process.stderr.readline()
if line:
if sys.version_info[0] >= 3:
line = decode(line)
stripped_line = line.rstrip()
if filter(stripped_line):
sys.stderr.write(line)
elif process.returncode is not None:
process.stderr.close()
break |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def run(args, echo=True, echo2=True, shell=False, cwd=None, env=None):
"""Run 'args' and return a two-tuple of exit code and lines read. If 'echo' is True, the stdout stream is echoed to sys.stdout. If 'echo2' is True, the stderr stream is echoed to sys.stderr. The 'echo' and 'echo2' arguments may be callables, in which case they are used as tee filters. If 'shell' is True, args are executed via the shell. The 'cwd' argument causes the child process to be executed in cwd. The 'env' argument allows to pass a dict replacing os.environ. """ |
if not callable(echo):
echo = On() if echo else Off()
if not callable(echo2):
echo2 = On() if echo2 else Off()
process = Popen(
args,
stdout=PIPE,
stderr=PIPE,
shell=shell,
cwd=cwd,
env=env
)
with background_thread(tee2, (process, echo2)):
lines = tee(process, echo)
return process.returncode, lines |
<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_representer_class(self, representer_class):
""" Registers the given representer class with this registry, using its MIME content type as the key. """ |
if representer_class in self.__rpr_classes.values():
raise ValueError('The representer class "%s" has already been '
'registered.' % representer_class)
self.__rpr_classes[representer_class.content_type] = representer_class
if issubclass(representer_class, MappingResourceRepresenter):
# Create and hold a mapping registry for the registered resource
# representer class.
mp_reg = representer_class.make_mapping_registry()
self.__mp_regs[representer_class.content_type] = mp_reg |
<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(self, resource_class, content_type, configuration=None):
""" Registers a representer factory for the given combination of resource class and content type. :param configuration: representer configuration. A default instance will be created if this is not given. :type configuration: :class:`everest.representers.config.RepresenterConfiguration` """ |
if not issubclass(resource_class, Resource):
raise ValueError('Representers can only be registered for '
'resource classes (got: %s).' % resource_class)
if not content_type in self.__rpr_classes:
raise ValueError('No representer class has been registered for '
'content type "%s".' % content_type)
# Register a factory resource -> representer for the given combination
# of resource class and content type.
rpr_cls = self.__rpr_classes[content_type]
self.__rpr_factories[(resource_class, content_type)] = \
rpr_cls.create_from_resource_class
if issubclass(rpr_cls, MappingResourceRepresenter):
# Create or update an attribute mapping.
mp_reg = self.__mp_regs[content_type]
mp = mp_reg.find_mapping(resource_class)
if mp is None:
# No mapping was registered yet for this resource class or any
# of its base classes; create a new one on the fly.
new_mp = mp_reg.create_mapping(resource_class, configuration)
elif not configuration is None:
if resource_class is mp.mapped_class:
# We have additional configuration for an existing mapping.
mp.configuration.update(configuration)
new_mp = mp
else:
# We have a derived class with additional configuration.
new_mp = mp_reg.create_mapping(
resource_class,
configuration=mp.configuration)
new_mp.configuration.update(configuration)
elif not resource_class is mp.mapped_class:
# We have a derived class without additional configuration.
new_mp = mp_reg.create_mapping(resource_class,
configuration=mp.configuration)
else:
# We found a dynamically created mapping for the right class
# without additional configuration; do not create a new one.
new_mp = None
if not new_mp is None:
# Store the new (or updated) mapping.
mp_reg.set_mapping(new_mp) |
<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(self, resource_class, content_type):
""" Creates a representer for the given combination of resource and content type. This will also find representer factories that were registered for a base class of the given resource. """ |
rpr_fac = self.__find_representer_factory(resource_class,
content_type)
if rpr_fac is None:
# Register a representer with default configuration on the fly
# and look again.
self.register(resource_class, content_type)
rpr_fac = self.__find_representer_factory(resource_class,
content_type)
return rpr_fac(resource_class) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.