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_by_name(self, name, style_type = None): """Find style by it's descriptive name. :Returns: Returns found style of type :class:`ooxml.doc.Style`. """
for st in self.styles.values(): if st: if st.name == name: return st if style_type and not st: st = self.styles.get(self.default_styles[style_type], None) return st
<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_by_id(self, style_id, style_type = None): """Find style by it's unique identifier :Returns: Returns found style of type :class:`ooxml.doc.Style`. """
for st in self.styles.values(): if st: if st.style_id == style_id: return st if style_type: return self.styles.get(self.default_styles[style_type], 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 process_affinity(affinity=None): """Get or set the CPU affinity set for the current process. This will affect all future threads spawned by this process. It is implementation-defined whether it will also affect previously-spawned threads. """
if affinity is not None: affinity = CPUSet(affinity) if not affinity.issubset(system_affinity()): raise ValueError("unknown cpus: %s" % affinity) return system_affinity()
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def acquire(self,blocking=True,timeout=None): """Attempt to acquire this lock. If the optional argument "blocking" is True and "timeout" is None, this methods blocks until is successfully acquires the lock. If "blocking" is False, it returns immediately if the lock could not be acquired. Otherwise, it blocks for at most "timeout" seconds trying to acquire the lock. In all cases, this methods returns True if the lock was successfully acquired and False otherwise. """
if timeout is None: return self.__lock.acquire(blocking) else: # Simulated timeout using progressively longer sleeps. # This is the same timeout scheme used in the stdlib Condition # class. If there's lots of contention on the lock then there's # a good chance you won't get it; but then again, Python doesn't # guarantee fairness anyway. We hope that platform-specific # extensions can provide a better mechanism. endtime = _time() + timeout delay = 0.0005 while not self.__lock.acquire(False): remaining = endtime - _time() if remaining <= 0: return False delay = min(delay*2,remaining,0.05) _sleep(delay) return 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 from_thread(cls,thread): """Convert a vanilla thread object into an instance of this class. This method "upgrades" a vanilla thread object to an instance of this extended class. You might need to call this if you obtain a reference to a thread by some means other than (a) creating it, or (b) from the methods of the threading2 module. """
new_classes = [] for new_cls in cls.__mro__: if new_cls not in thread.__class__.__mro__: new_classes.append(new_cls) if isinstance(thread,cls): pass elif issubclass(cls,thread.__class__): thread.__class__ = cls else: class UpgradedThread(thread.__class__,cls): pass thread.__class__ = UpgradedThread for new_cls in new_classes: if hasattr(new_cls,"_upgrade_thread"): new_cls._upgrade_thread(thread) return thread
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def acquire(self,blocking=True,timeout=None,shared=False): """Acquire the lock in shared or exclusive mode."""
with self._lock: if shared: self._acquire_shared(blocking,timeout) else: self._acquire_exclusive(blocking,timeout) assert not (self.is_shared and self.is_exclusive)
<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_font_size(document, style): """Get font size defined for this style. It will try to get font size from it's parent style if it is not defined by original style. :Args: - document (:class:`ooxml.doc.Document`): Document object - style (:class:`ooxml.doc.Style`): Style object :Returns: Returns font size as a number. -1 if it can not get font size. """
font_size = style.get_font_size() if font_size == -1: if style.based_on: based_on = document.styles.get_by_id(style.based_on) if based_on: return _get_font_size(document, based_on) return font_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 _get_numbering(document, numid, ilvl): """Returns type for the list. :Returns: Returns type for the list. Returns "bullet" by default or in case of an error. """
try: abs_num = document.numbering[numid] return document.abstruct_numbering[abs_num][ilvl]['numFmt'] except: return 'bullet'
<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_parent(root): """Returns root element for a list. :Args: root (Element): lxml element of current location :Returns: lxml element representing list """
elem = root while True: elem = elem.getparent() if elem.tag in ['ul', 'ol']: return elem
<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_list(ctx, root): """Close already opened list if needed. This will try to see if it is needed to close already opened list. :Args: - ctx (:class:`Context`): Context object - root (Element): lxml element representing current position. :Returns: lxml element where future content should be placed. """
try: n = len(ctx.in_list) if n <= 0: return root elem = root while n > 0: while True: if elem.tag in ['ul', 'ol', 'td']: elem = elem.getparent() break elem = elem.getparent() n -= 1 ctx.in_list = [] return elem except: 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 open_list(ctx, document, par, root, elem): """Open list if it is needed and place current element as first member of a list. :Args: - ctx (:class:`Context`): Context object - document (:class:`ooxml.doc.Document`): Document object - par (:class:`ooxml.doc.Paragraph`): Paragraph element - root (Element): lxml element of current location - elem (Element): lxml element representing current element we are trying to insert :Returns: lxml element where future content should be placed. """
_ls = None if par.ilvl != ctx.ilvl or par.numid != ctx.numid: # start if ctx.ilvl is not None and (par.ilvl > ctx.ilvl): fmt = _get_numbering(document, par.numid, par.ilvl) if par.ilvl > 0: # get last <li> in <ul> # could be nicer _b = list(root)[-1] _ls = etree.SubElement(_b, _get_numbering_tag(fmt)) root = _ls else: _ls = etree.SubElement(root, _get_numbering_tag(fmt)) root = _ls fire_hooks(ctx, document, par, _ls, ctx.get_hook(_get_numbering_tag(fmt))) ctx.in_list.append((par.numid, par.ilvl)) elif ctx.ilvl is not None and par.ilvl < ctx.ilvl: fmt = _get_numbering(document, ctx.numid, ctx.ilvl) try: while True: numid, ilvl = ctx.in_list[-1] if numid == par.numid and ilvl == par.ilvl: break root = _get_parent(root) ctx.in_list.pop() except: pass # if ctx.numid is not None and par.numid > ctx.numid: # if ctx.numid != None: if par.numid > ctx.numid: fmt = _get_numbering(document, par.numid, par.ilvl) _ls = etree.SubElement(root, _get_numbering_tag(fmt)) fire_hooks(ctx, document, par, _ls, ctx.get_hook(_get_numbering_tag(fmt))) ctx.in_list.append((par.numid, par.ilvl)) root = _ls ctx.ilvl = par.ilvl ctx.numid = par.numid _a = etree.SubElement(root, 'li') _a.text = elem.text for a in list(elem): _a.append(a) fire_hooks(ctx, document, par, _a, ctx.get_hook('li')) 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 serialize_break(ctx, document, elem, root): "Serialize break element." if elem.break_type == u'textWrapping': _div = etree.SubElement(root, 'br') else: _div = etree.SubElement(root, 'span') if ctx.options['embed_styles']: _div.set('style', 'page-break-after: always;') fire_hooks(ctx, document, elem, _div, ctx.get_hook('page_break')) 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 serialize_math(ctx, document, elem, root): """Serialize math element. Math objects are not supported at the moment. This is wht we only show error message. """
_div = etree.SubElement(root, 'span') if ctx.options['embed_styles']: _div.set('style', 'border: 1px solid red') _div.text = 'We do not support Math blocks at the moment.' fire_hooks(ctx, document, elem, _div, ctx.get_hook('math')) 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 serialize_link(ctx, document, elem, root): """Serilaze link element. This works only for external links at the moment. """
_a = etree.SubElement(root, 'a') for el in elem.elements: _ser = ctx.get_serializer(el) if _ser: _td = _ser(ctx, document, el, _a) else: if isinstance(el, doc.Text): children = list(_a) if len(children) == 0: _text = _a.text or u'' _a.text = u'{}{}'.format(_text, el.value()) else: _text = children[-1].tail or u'' children[-1].tail = u'{}{}'.format(_text, el.value()) if elem.rid in document.relationships[ctx.options['relationship']]: _a.set('href', document.relationships[ctx.options['relationship']][elem.rid].get('target', '')) fire_hooks(ctx, document, elem, _a, ctx.get_hook('a')) 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 serialize_image(ctx, document, elem, root): """Serialize image element. This is not abstract enough. """
_img = etree.SubElement(root, 'img') # make path configurable if elem.rid in document.relationships[ctx.options['relationship']]: img_src = document.relationships[ctx.options['relationship']][elem.rid].get('target', '') img_name, img_extension = os.path.splitext(img_src) _img.set('src', 'static/{}{}'.format(elem.rid, img_extension)) fire_hooks(ctx, document, elem, _img, ctx.get_hook('img')) 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 fire_hooks(ctx, document, elem, element, hooks): """Fire hooks on newly created element. For each newly created element we will try to find defined hooks and execute them. :Args: - ctx (:class:`Context`): Context object - document (:class:`ooxml.doc.Document`): Document object - elem (:class:`ooxml.doc.Element`): Element which we serialized - element (Element): lxml element which we created - hooks (list): List of hooks """
if not hooks: return for hook in hooks: hook(ctx, document, elem, 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 has_style(node): """Tells us if node element has defined styling. :Args: - node (:class:`ooxml.doc.Element`): Element :Returns: True or False """
elements = ['b', 'i', 'u', 'strike', 'color', 'jc', 'sz', 'ind', 'superscript', 'subscript', 'small_caps'] return any([True for elem in elements if elem in node.rpr])
<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_style_css(ctx, node, embed=True, fontsize=-1): """Returns as string defined CSS for this node. Defined CSS can be different if it is embeded or no. When it is embeded styling for bold,italic and underline will not be defined with CSS. In that case we use defined tags <b>,<i>,<u> from the content. :Args: - ctx (:class:`Context`): Context object - node (:class:`ooxml.doc.Element`): Node element - embed (book): True by default. :Returns: Returns as string defined CSS for this node """
style = [] if not node: return if fontsize in [-1, 2]: if 'sz' in node.rpr: size = int(node.rpr['sz']) / 2 if ctx.options['embed_fontsize']: if ctx.options['scale_to_size']: multiplier = size-ctx.options['scale_to_size'] scale = 100 + int(math.trunc(8.3*multiplier)) style.append('font-size: {}%'.format(scale)) else: style.append('font-size: {}pt'.format(size)) if fontsize in [-1, 1]: # temporarily if not embed: if 'b' in node.rpr: style.append('font-weight: bold') if 'i' in node.rpr: style.append('font-style: italic') if 'u' in node.rpr: style.append('text-decoration: underline') if 'small_caps' in node.rpr: style.append('font-variant: small-caps') if 'strike' in node.rpr: style.append('text-decoration: line-through') if 'color' in node.rpr: if node.rpr['color'] != '000000': style.append('color: #{}'.format(node.rpr['color'])) if 'jc' in node.ppr: # left right both align = node.ppr['jc'] if align.lower() == 'both': align = 'justify' style.append('text-align: {}'.format(align)) if 'ind' in node.ppr: if 'left' in node.ppr['ind']: size = int(node.ppr['ind']['left']) / 10 style.append('margin-left: {}px'.format(size)) if 'right' in node.ppr['ind']: size = int(node.ppr['ind']['right']) / 10 style.append('margin-right: {}px'.format(size)) if 'first_line' in node.ppr['ind']: size = int(node.ppr['ind']['first_line']) / 10 style.append('text-indent: {}px'.format(size)) if len(style) == 0: return '' return '; '.join(style) + ';'
<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_all_styles(document, style): """Returns list of styles on which specified style is based on. :Args: - document (:class:`ooxml.doc.Document`): Document object - style (:class:`ooxml.doc.Style`): Style object :Returns: List of style objects. """
classes = [] while True: classes.insert(0, get_style_name(style)) if style.based_on: style = document.styles.get_by_id(style.based_on) else: break return classes
<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_css_classes(document, style): """Returns CSS classes for this style. This function will check all the styles specified style is based on and return their CSS classes. :Args: - document (:class:`ooxml.doc.Document`): Document object - style (:class:`ooxml.doc.Style`): Style object :Returns: String representing all the CSS classes for this element. 'header1 normal' """
lst = [st.lower() for st in get_all_styles(document, style)[-1:]] + \ ['{}-fontsize'.format(st.lower()) for st in get_all_styles(document, style)[-1:]] return ' '.join(lst)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def serialize_symbol(ctx, document, el, root): "Serialize special symbols." span = etree.SubElement(root, 'span') span.text = el.value() fire_hooks(ctx, document, el, span, ctx.get_hook('symbol')) 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 serialize_footnote(ctx, document, el, root): "Serializes footnotes." footnote_num = el.rid if el.rid not in ctx.footnote_list: ctx.footnote_id += 1 ctx.footnote_list[el.rid] = ctx.footnote_id footnote_num = ctx.footnote_list[el.rid] note = etree.SubElement(root, 'sup') link = etree.SubElement(note, 'a') link.set('href', '#') link.text = u'{}'.format(footnote_num) fire_hooks(ctx, document, el, note, ctx.get_hook('footnote')) 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 serialize_comment(ctx, document, el, root): "Serializes comment." # Check if option is turned on if el.comment_type == 'end': ctx.opened_comments.remove(el.cid) else: if el.comment_type != 'reference': ctx.opened_comments.append(el.cid) if ctx.options['comment_span']: link = etree.SubElement(root, 'a') link.set('href', '#') link.set('class', 'comment-link') link.set('id', 'comment-id-' + el.cid) link.text = '' fire_hooks(ctx, document, el, link, ctx.get_hook('comment')) 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 serialize_endnote(ctx, document, el, root): "Serializes endnotes." footnote_num = el.rid if el.rid not in ctx.endnote_list: ctx.endnote_id += 1 ctx.endnote_list[el.rid] = ctx.endnote_id footnote_num = ctx.endnote_list[el.rid] note = etree.SubElement(root, 'sup') link = etree.SubElement(note, 'a') link.set('href', '#') link.text = u'{}'.format(footnote_num) fire_hooks(ctx, document, el, note, ctx.get_hook('endnote')) 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 serialize_smarttag(ctx, document, el, root): "Serializes smarttag." if ctx.options['smarttag_span']: _span = etree.SubElement(root, 'span', {'class': 'smarttag', 'data-smarttag-element': el.element}) else: _span = root for elem in el.elements: _ser = ctx.get_serializer(elem) if _ser: _td = _ser(ctx, document, elem, _span) else: if isinstance(elem, doc.Text): children = list(_span) if len(children) == 0: _text = _span.text or u'' _span.text = u'{}{}'.format(_text, elem.text) else: _text = children[-1].tail or u'' children[-1].tail = u'{}{}'.format(_text, elem.text) fire_hooks(ctx, document, el, _span, ctx.get_hook('smarttag')) 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 serialize_table(ctx, document, table, root): """Serializes table element. """
# What we should check really is why do we pass None as root element # There is a good chance some content is missing after the import if root is None: return root if ctx.ilvl != None: root = close_list(ctx, root) ctx.ilvl, ctx.numid = None, None _table = etree.SubElement(root, 'table') _table.set('border', '1') _table.set('width', '100%') style = get_style(document, table) if style: _table.set('class', get_css_classes(document, style)) for rows in table.rows: _tr = etree.SubElement(_table, 'tr') for cell in rows: _td = etree.SubElement(_tr, 'td') if cell.grid_span != 1: _td.set('colspan', str(cell.grid_span)) if cell.row_span != 1: _td.set('rowspan', str(cell.row_span)) for elem in cell.elements: if isinstance(elem, doc.Paragraph): _ser = ctx.get_serializer(elem) _td = _ser(ctx, document, elem, _td, embed=False) if ctx.ilvl != None: # root = close_list(ctx, root) _td = close_list(ctx, _td) ctx.ilvl, ctx.numid = None, None fire_hooks(ctx, document, table, _td, ctx.get_hook('td')) fire_hooks(ctx, document, table, _td, ctx.get_hook('tr')) fire_hooks(ctx, document, table, _table, ctx.get_hook('table')) 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 serialize_textbox(ctx, document, txtbox, root): """Serialize textbox element."""
_div = etree.SubElement(root, 'div') _div.set('class', 'textbox') for elem in txtbox.elements: _ser = ctx.get_serializer(elem) if _ser: _ser(ctx, document, elem, _div) fire_hooks(ctx, document, txtbox, _div, ctx.get_hook('textbox')) 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 serialize_elements(document, elements, options=None): """Serialize list of elements into HTML string. :Args: - document (:class:`ooxml.doc.Document`): Document object - elements (list): List of elements - options (dict): Optional dictionary with :class:`Context` options :Returns: Returns HTML representation of the document. """
ctx = Context(document, options) tree_root = root = etree.Element('div') for elem in elements: _ser = ctx.get_serializer(elem) if _ser: root = _ser(ctx, document, elem, root) # TODO: # - create footnotes now return etree.tostring(tree_root, pretty_print=ctx.options.get('pretty_print', True), encoding="utf-8", xml_declaration=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 is_header(self, elem, font_size, node, style=None): """Used for checking if specific element is a header or not. :Returns: True or False """
# This logic has been disabled for now. Mark this as header if it has # been marked during the parsing or mark. # if hasattr(elem, 'possible_header'): # if elem.possible_header: # return True # if not style: # return False if hasattr(style, 'style_id'): fnt_size = _get_font_size(self.doc, style) from .importer import calculate_weight weight = calculate_weight(self.doc, elem) if weight > 50: return False if fnt_size in self.doc.possible_headers_style: return True return font_size in self.doc.possible_headers else: list_of_sizes = {} for el in elem.elements: try: fs = get_style_fontsize(el) weight = len(el.value()) if el.value() else 0 list_of_sizes[fs] = list_of_sizes.setdefault(fs, 0) + weight except: pass sorted_list_of_sizes = list(collections.OrderedDict(sorted(six.iteritems(list_of_sizes), key=lambda t: t[0]))) font_size_to_check = font_size if len(sorted_list_of_sizes) > 0: if sorted_list_of_sizes[0] != font_size: return sorted_list_of_sizes[0] in self.doc.possible_headers return font_size in self.doc.possible_headers
<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_header(self, elem, style, node): """Returns HTML tag representing specific header for this element. :Returns: String representation of HTML tag. """
font_size = style if hasattr(elem, 'possible_header'): if elem.possible_header: return 'h1' if not style: return 'h6' if hasattr(style, 'style_id'): font_size = _get_font_size(self.doc, style) try: if font_size in self.doc.possible_headers_style: return 'h{}'.format(self.doc.possible_headers_style.index(font_size)+1) return 'h{}'.format(self.doc.possible_headers.index(font_size)+1) except ValueError: return 'h6'
<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_serializer(self, node): """Returns serializer for specific element. :Args: - node (:class:`ooxml.doc.Element`): Element object :Returns: Returns reference to a function which will be used for serialization. """
return self.options['serializers'].get(type(node), None) if type(node) in self.options['serializers']: return self.options['serializers'][type(node)] 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 priority(self,priority): """Set the priority for all threads in this group. If setting priority fails on any thread, the priority of all threads is restored to its previous value. """
with self.__lock: old_priorities = {} try: for thread in self.__threads: old_priorities[thread] = thread.priority thread.priority = priority except Exception: for (thread,old_priority) in old_priorities.iteritems(): try: thread.priority = old_priority except Exception: pass raise else: self.__priority = priority
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def affinity(self,affinity): """Set the affinity for all threads in this group. If setting affinity fails on any thread, the affinity of all threads is restored to its previous value. """
with self.__lock: old_affinities = {} try: for thread in self.__threads: old_affinities[thread] = thread.affinity thread.affinity = affinity except Exception: for (thread,old_affinity) in old_affinities.iteritems(): try: thread.affinity = old_affinity except Exception: pass raise else: self.__affinity = affinity
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def join(self,timeout=None): """Join all threads in this group. If the optional "timeout" argument is given, give up after that many seconds. This method returns True is the threads were successfully joined, False if a timeout occurred. """
if timeout is None: for thread in self.__threads: thread.join() else: deadline = _time() + timeout for thread in self.__threads: delay = deadline - _time() if delay <= 0: return False if not thread.join(delay): return False return 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 text_length(elem): """Returns length of the content in this element. Return value is not correct but it is **good enough***. """
if not elem: return 0 value = elem.value() try: value = len(value) except: value = 0 try: for a in elem.elements: value += len(a.value()) except: pass 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 reset(self): "Resets the values." self.zf = zipfile.ZipFile(self.file_name, 'r') self._doc = 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 build_rrule(count=None, interval=None, bysecond=None, byminute=None, byhour=None, byweekno=None, bymonthday=None, byyearday=None, bymonth=None, until=None, bysetpos=None, wkst=None, byday=None, freq=None): """ Build rrule dictionary for vRecur class. :param count: int :param interval: int :param bysecond: int :param byminute: int :param byhour: int :param byweekno: int :param bymonthday: int :param byyearday: int :param bymonth: int :param until: datetime :param bysetpos: int :param wkst: str, two-letter weekday :param byday: weekday :param freq: str, frequency name ('WEEK', 'MONTH', etc) :return: dict """
result = {} if count is not None: result['COUNT'] = count if interval is not None: result['INTERVAL'] = interval if bysecond is not None: result['BYSECOND'] = bysecond if byminute is not None: result['BYMINUTE'] = byminute if byhour is not None: result['BYHOUR'] = byhour if byweekno is not None: result['BYWEEKNO'] = byweekno if bymonthday is not None: result['BYMONTHDAY'] = bymonthday if byyearday is not None: result['BYYEARDAY'] = byyearday if bymonth is not None: result['BYMONTH'] = bymonth if until is not None: result['UNTIL'] = until if bysetpos is not None: result['BYSETPOS'] = bysetpos if wkst is not None: result['WKST'] = wkst if byday is not None: result['BYDAY'] = byday if freq is not None: if freq not in vRecur.frequencies: raise ValueError('Frequency value should be one of: {0}' .format(vRecur.frequencies)) result['FREQ'] = freq 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 build_rrule_from_recurrences_rrule(rule): """ Build rrule dictionary for vRecur class from a django_recurrences rrule. django_recurrences is a popular implementation for recurrences in django. https://pypi.org/project/django-recurrence/ this is a shortcut to interface between recurrences and icalendar. """
from recurrence import serialize line = serialize(rule) if line.startswith('RRULE:'): line = line[6:] return build_rrule_from_text(line)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def build_rrule_from_dateutil_rrule(rule): """ Build rrule dictionary for vRecur class from a dateutil rrule. Dateutils rrule is a popular implementation of rrule in python. https://pypi.org/project/python-dateutil/ this is a shortcut to interface between dateutil and icalendar. """
lines = str(rule).splitlines() for line in lines: if line.startswith('DTSTART:'): continue if line.startswith('RRULE:'): line = line[6:] return build_rrule_from_text(line)
<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, outfile, encoding): u""" Writes the feed to the specified file in the specified encoding. """
cal = Calendar() cal.add('version', '2.0') cal.add('calscale', 'GREGORIAN') for ifield, efield in FEED_FIELD_MAP: val = self.feed.get(ifield) if val is not None: cal.add(efield, val) self.write_items(cal) to_ical = getattr(cal, 'as_string', None) if not to_ical: to_ical = cal.to_ical outfile.write(to_ical())
<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_items(self, calendar): """ Write all events to the calendar """
for item in self.items: event = Event() for ifield, efield in ITEM_EVENT_FIELD_MAP: val = item.get(ifield) if val is not None: event.add(efield, val) calendar.add_component(event)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def _textlist(self, _addtail=False): '''Returns a list of text strings contained within an element and its sub-elements. Helpful for extracting text from prose-oriented XML (such as XHTML or DocBook). ''' result = [] if (not _addtail) and (self.text is not None): result.append(self.text) for elem in self: result.extend(elem.textlist(True)) if _addtail and self.tail is not None: result.append(self.tail) 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 _reindent(s, indent, reformat=True): """ Remove the existing indentation from each line of a chunk of text, s, and then prefix each line with a new indent string. Also removes trailing whitespace from each line, and leading and trailing blank lines. """
s = textwrap.dedent(s) s = s.split('\n') s = [x.rstrip() for x in s] while s and (not s[0]): s = s[1:] while s and (not s[-1]): s = s[:-1] if reformat: s = '\n'.join(s) s = textwrap.wrap(s, initial_indent=indent, subsequent_indent=indent) else: s = [indent + x for x in s] return '\n'.join(s) + '\n'
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def generate_docstr(element, indent='', wrap=None): """ Generate a Python docstr for a given element in the AMQP XML spec file. The element could be a class or method The 'wrap' parameter is an optional chunk of text that's added to the beginning and end of the resulting docstring. """
result = [] txt = element.text and element.text.rstrip() if txt: result.append(_reindent(txt, indent)) result.append(indent) for d in element.findall('doc') + element.findall('rule'): docval = ''.join(d.textlist()).rstrip() if not docval: continue reformat = True if 'name' in d.attrib: result.append(indent + d.attrib['name'].upper() + ':') result.append(indent) extra_indent = ' ' if d.attrib['name'] == 'grammar': reformat = False # Don't want re-indenting to mess this up elif d.tag == 'rule': result.append(indent + 'RULE:') result.append(indent) extra_indent = ' ' else: extra_indent = '' result.append(_reindent(docval, indent + extra_indent, reformat)) result.append(indent) fields = element.findall('field') if fields: result.append(indent + 'PARAMETERS:') for f in fields: result.append(indent + ' ' + _fixup_field_name(f) + ': ' + _field_type(f)) field_docs = generate_docstr(f, indent + ' ') if field_docs: result.append(indent) result.append(field_docs) result.append(indent) if not result: return None if wrap is not None: result = [wrap] + result + [wrap] return '\n'.join(x.rstrip() for x in result) + '\n'
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def generate_module(spec, out): """ Given an AMQP spec parsed into an xml.etree.ElemenTree, and a file-like 'out' object to write to, generate the skeleton of a Python module. """
# # HACK THE SPEC so that 'access' is handled by 'channel' instead of 'connection' # for amqp_class in spec.findall('class'): if amqp_class.attrib['name'] == 'access': amqp_class.attrib['handler'] = 'channel' # # Build up some helper dictionaries # for domain in spec.findall('domain'): domains[domain.attrib['name']] = domain.attrib['type'] for amqp_class in spec.findall('class'): for amqp_method in amqp_class.findall('method'): method_name_map[(amqp_class.attrib['name'], amqp_method.attrib['name'])] = \ ( amqp_class.attrib['index'], amqp_method.attrib['index'], amqp_class.attrib['handler'].capitalize() + '.' + _fixup_method_name(amqp_class, amqp_method), ) #### Actually generate output for amqp_class in spec.findall('class'): if amqp_class.attrib['handler'] == amqp_class.attrib['name']: generate_class(spec, amqp_class, out) out.write('_METHOD_MAP = {\n') for amqp_class in spec.findall('class'): print amqp_class.attrib # for chassis in amqp_class.findall('chassis'): # print ' ', chassis.attrib for amqp_method in amqp_class.findall('method'): # print ' ', amqp_method.attrib # for chassis in amqp_method.findall('chassis'): # print ' ', chassis.attrib chassis = [x.attrib['name'] for x in amqp_method.findall('chassis')] if 'client' in chassis: out.write(" (%s, %s): (%s, %s._%s),\n" % ( amqp_class.attrib['index'], amqp_method.attrib['index'], amqp_class.attrib['handler'].capitalize(), amqp_class.attrib['handler'].capitalize(), _fixup_method_name(amqp_class, amqp_method))) out.write('}\n\n') out.write('_METHOD_NAME_MAP = {\n') for amqp_class in spec.findall('class'): for amqp_method in amqp_class.findall('method'): out.write(" (%s, %s): '%s.%s',\n" % ( amqp_class.attrib['index'], amqp_method.attrib['index'], amqp_class.attrib['handler'].capitalize(), _fixup_method_name(amqp_class, amqp_method))) out.write('}\n')
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _process_method_frame(self, channel, payload): """ Process Method frames """
method_sig = unpack('>HH', payload[:4]) args = AMQPReader(payload[4:]) if method_sig in _CONTENT_METHODS: # # Save what we've got so far and wait for the content-header # self.partial_messages[channel] = _PartialMessage(method_sig, args) self.expected_types[channel] = 2 else: self.queue.put((channel, method_sig, args, 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 _alert(self, args): """ This method allows the server to send a non-fatal warning to the client. This is used for methods that are normally asynchronous and thus do not have confirmations, and for which the server may detect errors that need to be reported. Fatal errors are handled as channel or connection exceptions; non- fatal errors are sent through this method. PARAMETERS: reply_code: short The reply code. The AMQ reply codes are defined in AMQ RFC 011. reply_text: shortstr The localised reply text. This text can be logged as an aid to resolving issues. details: table detailed information for warning A set of fields that provide more information about the problem. The meaning of these fields are defined on a per-reply-code basis (TO BE DEFINED). """
reply_code = args.read_short() reply_text = args.read_shortstr() details = args.read_table() self.alerts.put((reply_code, reply_text, details))
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def access_request(self, realm, exclusive=False, passive=False, active=False, write=False, read=False): """ request an access ticket This method requests an access ticket for an access realm. The server responds by granting the access ticket. If the client does not have access rights to the requested realm this causes a connection exception. Access tickets are a per-channel resource. RULE: The realm name MUST start with either "/data" (for application resources) or "/admin" (for server administration resources). If the realm starts with any other path, the server MUST raise a connection exception with reply code 403 (access refused). RULE: The server MUST implement the /data realm and MAY implement the /admin realm. The mapping of resources to realms is not defined in the protocol - this is a server- side configuration issue. PARAMETERS: realm: shortstr name of requested realm RULE: If the specified realm is not known to the server, the server must raise a channel exception with reply code 402 (invalid path). exclusive: boolean request exclusive access Request exclusive access to the realm. If the server cannot grant this - because there are other active tickets for the realm - it raises a channel exception. passive: boolean request passive access Request message passive access to the specified access realm. Passive access lets a client get information about resources in the realm but not to make any changes to them. active: boolean request active access Request message active access to the specified access realm. Acvtive access lets a client get create and delete resources in the realm. write: boolean request write access Request write access to the specified access realm. Write access lets a client publish messages to all exchanges in the realm. read: boolean request read access Request read access to the specified access realm. Read access lets a client consume messages from queues in the realm. The most recently requested ticket is used as the channel's default ticket for any method that requires a ticket. """
args = AMQPWriter() args.write_shortstr(realm) args.write_bit(exclusive) args.write_bit(passive) args.write_bit(active) args.write_bit(write) args.write_bit(read) self._send_method((30, 10), args) return self.wait(allowed_methods=[ (30, 11), # Channel.access_request_ok ])
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def exchange_declare(self, exchange, type, passive=False, durable=False, auto_delete=True, internal=False, nowait=False, arguments=None, ticket=None): """ declare exchange, create if needed This method creates an exchange if it does not already exist, and if the exchange exists, verifies that it is of the correct and expected class. RULE: The server SHOULD support a minimum of 16 exchanges per virtual host and ideally, impose no limit except as defined by available resources. PARAMETERS: exchange: shortstr RULE: Exchange names starting with "amq." are reserved for predeclared and standardised exchanges. If the client attempts to create an exchange starting with "amq.", the server MUST raise a channel exception with reply code 403 (access refused). type: shortstr exchange type Each exchange belongs to one of a set of exchange types implemented by the server. The exchange types define the functionality of the exchange - i.e. how messages are routed through it. It is not valid or meaningful to attempt to change the type of an existing exchange. RULE: If the exchange already exists with a different type, the server MUST raise a connection exception with a reply code 507 (not allowed). RULE: If the server does not support the requested exchange type it MUST raise a connection exception with a reply code 503 (command invalid). passive: boolean do not create exchange If set, the server will not create the exchange. The client can use this to check whether an exchange exists without modifying the server state. RULE: If set, and the exchange does not already exist, the server MUST raise a channel exception with reply code 404 (not found). durable: boolean request a durable exchange If set when creating a new exchange, the exchange will be marked as durable. Durable exchanges remain active when a server restarts. Non-durable exchanges (transient exchanges) are purged if/when a server restarts. RULE: The server MUST support both durable and transient exchanges. RULE: The server MUST ignore the durable field if the exchange already exists. auto_delete: boolean auto-delete when unused If set, the exchange is deleted when all queues have finished using it. RULE: The server SHOULD allow for a reasonable delay between the point when it determines that an exchange is not being used (or no longer used), and the point when it deletes the exchange. At the least it must allow a client to create an exchange and then bind a queue to it, with a small but non-zero delay between these two actions. RULE: The server MUST ignore the auto-delete field if the exchange already exists. internal: boolean create internal exchange If set, the exchange may not be used directly by publishers, but only when bound to other exchanges. Internal exchanges are used to construct wiring that is not visible to applications. nowait: boolean do not send a reply method If set, the server will not respond to the method. The client should not wait for a reply method. If the server could not complete the method it will raise a channel or connection exception. arguments: table arguments for declaration A set of arguments for the declaration. The syntax and semantics of these arguments depends on the server implementation. This field is ignored if passive is True. ticket: short When a client defines a new exchange, this belongs to the access realm of the ticket used. All further work done with that exchange must be done with an access ticket for the same realm. RULE: The client MUST provide a valid access ticket giving "active" access to the realm in which the exchange exists or will be created, or "passive" access if the if-exists flag is set. """
if arguments is None: arguments = {} args = AMQPWriter() if ticket is not None: args.write_short(ticket) else: args.write_short(self.default_ticket) args.write_shortstr(exchange) args.write_shortstr(type) args.write_bit(passive) args.write_bit(durable) args.write_bit(auto_delete) args.write_bit(internal) args.write_bit(nowait) args.write_table(arguments) self._send_method((40, 10), args) if not nowait: return self.wait(allowed_methods=[ (40, 11), # Channel.exchange_declare_ok ])
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def exchange_delete(self, exchange, if_unused=False, nowait=False, ticket=None): """ delete an exchange This method deletes an exchange. When an exchange is deleted all queue bindings on the exchange are cancelled. PARAMETERS: exchange: shortstr RULE: The exchange MUST exist. Attempting to delete a non-existing exchange causes a channel exception. if_unused: boolean delete only if unused If set, the server will only delete the exchange if it has no queue bindings. If the exchange has queue bindings the server does not delete it but raises a channel exception instead. RULE: If set, the server SHOULD delete the exchange but only if it has no queue bindings. RULE: If set, the server SHOULD raise a channel exception if the exchange is in use. nowait: boolean do not send a reply method If set, the server will not respond to the method. The client should not wait for a reply method. If the server could not complete the method it will raise a channel or connection exception. ticket: short RULE: The client MUST provide a valid access ticket giving "active" access rights to the exchange's access realm. """
args = AMQPWriter() if ticket is not None: args.write_short(ticket) else: args.write_short(self.default_ticket) args.write_shortstr(exchange) args.write_bit(if_unused) args.write_bit(nowait) self._send_method((40, 20), args) if not nowait: return self.wait(allowed_methods=[ (40, 21), # Channel.exchange_delete_ok ])
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def queue_bind(self, queue, exchange, routing_key='', nowait=False, arguments=None, ticket=None): """ bind queue to an exchange This method binds a queue to an exchange. Until a queue is bound it will not receive any messages. In a classic messaging model, store-and-forward queues are bound to a dest exchange and subscription queues are bound to a dest_wild exchange. RULE: A server MUST allow ignore duplicate bindings - that is, two or more bind methods for a specific queue, with identical arguments - without treating these as an error. RULE: If a bind fails, the server MUST raise a connection exception. RULE: The server MUST NOT allow a durable queue to bind to a transient exchange. If the client attempts this the server MUST raise a channel exception. RULE: Bindings for durable queues are automatically durable and the server SHOULD restore such bindings after a server restart. RULE: If the client attempts to an exchange that was declared as internal, the server MUST raise a connection exception with reply code 530 (not allowed). RULE: The server SHOULD support at least 4 bindings per queue, and ideally, impose no limit except as defined by available resources. PARAMETERS: queue: shortstr Specifies the name of the queue to bind. If the queue name is empty, refers to the current queue for the channel, which is the last declared queue. RULE: If the client did not previously declare a queue, and the queue name in this method is empty, the server MUST raise a connection exception with reply code 530 (not allowed). RULE: If the queue does not exist the server MUST raise a channel exception with reply code 404 (not found). exchange: shortstr The name of the exchange to bind to. RULE: If the exchange does not exist the server MUST raise a channel exception with reply code 404 (not found). routing_key: shortstr message routing key Specifies the routing key for the binding. The routing key is used for routing messages depending on the exchange configuration. Not all exchanges use a routing key - refer to the specific exchange documentation. If the routing key is empty and the queue name is empty, the routing key will be the current queue for the channel, which is the last declared queue. nowait: boolean do not send a reply method If set, the server will not respond to the method. The client should not wait for a reply method. If the server could not complete the method it will raise a channel or connection exception. arguments: table arguments for binding A set of arguments for the binding. The syntax and semantics of these arguments depends on the exchange class. ticket: short The client provides a valid access ticket giving "active" access rights to the queue's access realm. """
if arguments is None: arguments = {} args = AMQPWriter() if ticket is not None: args.write_short(ticket) else: args.write_short(self.default_ticket) args.write_shortstr(queue) args.write_shortstr(exchange) args.write_shortstr(routing_key) args.write_bit(nowait) args.write_table(arguments) self._send_method((50, 20), args) if not nowait: return self.wait(allowed_methods=[ (50, 21), # Channel.queue_bind_ok ])
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _queue_declare_ok(self, args): """ confirms a queue definition This method confirms a Declare method and confirms the name of the queue, essential for automatically-named queues. PARAMETERS: queue: shortstr Reports the name of the queue. If the server generated a queue name, this field contains that name. message_count: long number of messages in queue Reports the number of messages in the queue, which will be zero for newly-created queues. consumer_count: long number of consumers Reports the number of active consumers for the queue. Note that consumers can suspend activity (Channel.Flow) in which case they do not appear in this count. """
queue = args.read_shortstr() message_count = args.read_long() consumer_count = args.read_long() return queue, message_count, consumer_count
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def basic_consume(self, queue='', consumer_tag='', no_local=False, no_ack=False, exclusive=False, nowait=False, callback=None, ticket=None): """ start a queue consumer This method asks the server to start a "consumer", which is a transient request for messages from a specific queue. Consumers last as long as the channel they were created on, or until the client cancels them. RULE: The server SHOULD support at least 16 consumers per queue, unless the queue was declared as private, and ideally, impose no limit except as defined by available resources. PARAMETERS: queue: shortstr Specifies the name of the queue to consume from. If the queue name is null, refers to the current queue for the channel, which is the last declared queue. RULE: If the client did not previously declare a queue, and the queue name in this method is empty, the server MUST raise a connection exception with reply code 530 (not allowed). consumer_tag: shortstr Specifies the identifier for the consumer. The consumer tag is local to a connection, so two clients can use the same consumer tags. If this field is empty the server will generate a unique tag. RULE: The tag MUST NOT refer to an existing consumer. If the client attempts to create two consumers with the same non-empty tag the server MUST raise a connection exception with reply code 530 (not allowed). no_local: boolean do not deliver own messages If the no-local field is set the server will not send messages to the client that published them. no_ack: boolean no acknowledgement needed If this field is set the server does not expect acknowledgments for messages. That is, when a message is delivered to the client the server automatically and silently acknowledges it on behalf of the client. This functionality increases performance but at the cost of reliability. Messages can get lost if a client dies before it can deliver them to the application. exclusive: boolean request exclusive access Request exclusive consumer access, meaning only this consumer can access the queue. RULE: If the server cannot grant exclusive access to the queue when asked, - because there are other consumers active - it MUST raise a channel exception with return code 403 (access refused). nowait: boolean do not send a reply method If set, the server will not respond to the method. The client should not wait for a reply method. If the server could not complete the method it will raise a channel or connection exception. callback: Python callable function/method called with each delivered message For each message delivered by the broker, the callable will be called with a Message object as the single argument. If no callable is specified, messages are quietly discarded, no_ack should probably be set to True in that case. ticket: short RULE: The client MUST provide a valid access ticket giving "read" access rights to the realm for the queue. """
args = AMQPWriter() if ticket is not None: args.write_short(ticket) else: args.write_short(self.default_ticket) args.write_shortstr(queue) args.write_shortstr(consumer_tag) args.write_bit(no_local) args.write_bit(no_ack) args.write_bit(exclusive) args.write_bit(nowait) self._send_method((60, 20), args) if not nowait: consumer_tag = self.wait(allowed_methods=[ (60, 21), # Channel.basic_consume_ok ]) self.callbacks[consumer_tag] = callback return consumer_tag
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _basic_deliver(self, args, msg): """ notify the client of a consumer message This method delivers a message to the client, via a consumer. In the asynchronous message delivery model, the client starts a consumer using the Consume method, then the server responds with Deliver methods as and when messages arrive for that consumer. RULE: The server SHOULD track the number of times a message has been delivered to clients and when a message is redelivered a certain number of times - e.g. 5 times - without being acknowledged, the server SHOULD consider the message to be unprocessable (possibly causing client applications to abort), and move the message to a dead letter queue. PARAMETERS: consumer_tag: shortstr consumer tag Identifier for the consumer, valid within the current connection. RULE: The consumer tag is valid only within the channel from which the consumer was created. I.e. a client MUST NOT create a consumer in one channel and then use it in another. delivery_tag: longlong server-assigned delivery tag The server-assigned and channel-specific delivery tag RULE: The delivery tag is valid only within the channel from which the message was received. I.e. a client MUST NOT receive a message on one channel and then acknowledge it on another. RULE: The server MUST NOT use a zero value for delivery tags. Zero is reserved for client use, meaning "all messages so far received". redelivered: boolean message is being redelivered This indicates that the message has been previously delivered to this or another client. exchange: shortstr Specifies the name of the exchange that the message was originally published to. routing_key: shortstr Message routing key Specifies the routing key name specified when the message was published. """
consumer_tag = args.read_shortstr() delivery_tag = args.read_longlong() redelivered = args.read_bit() exchange = args.read_shortstr() routing_key = args.read_shortstr() msg.delivery_info = { 'channel': self, 'consumer_tag': consumer_tag, 'delivery_tag': delivery_tag, 'redelivered': redelivered, 'exchange': exchange, 'routing_key': routing_key, } func = self.callbacks.get(consumer_tag, None) if func is not None: func(msg)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def basic_get(self, queue='', no_ack=False, ticket=None): """ direct access to a queue This method provides a direct access to the messages in a queue using a synchronous dialogue that is designed for specific types of application where synchronous functionality is more important than performance. PARAMETERS: queue: shortstr Specifies the name of the queue to consume from. If the queue name is null, refers to the current queue for the channel, which is the last declared queue. RULE: If the client did not previously declare a queue, and the queue name in this method is empty, the server MUST raise a connection exception with reply code 530 (not allowed). no_ack: boolean no acknowledgement needed If this field is set the server does not expect acknowledgments for messages. That is, when a message is delivered to the client the server automatically and silently acknowledges it on behalf of the client. This functionality increases performance but at the cost of reliability. Messages can get lost if a client dies before it can deliver them to the application. ticket: short RULE: The client MUST provide a valid access ticket giving "read" access rights to the realm for the queue. Non-blocking, returns a message object, or None. """
args = AMQPWriter() if ticket is not None: args.write_short(ticket) else: args.write_short(self.default_ticket) args.write_shortstr(queue) args.write_bit(no_ack) self._send_method((60, 70), args) return self.wait(allowed_methods=[ (60, 71), # Channel.basic_get_ok (60, 72), # Channel.basic_get_empty ])
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def basic_publish(self, msg, exchange='', routing_key='', mandatory=False, immediate=False, ticket=None): """ publish a message This method publishes a message to a specific exchange. The message will be routed to queues as defined by the exchange configuration and distributed to any active consumers when the transaction, if any, is committed. PARAMETERS: exchange: shortstr Specifies the name of the exchange to publish to. The exchange name can be empty, meaning the default exchange. If the exchange name is specified, and that exchange does not exist, the server will raise a channel exception. RULE: The server MUST accept a blank exchange name to mean the default exchange. RULE: If the exchange was declared as an internal exchange, the server MUST raise a channel exception with a reply code 403 (access refused). RULE: The exchange MAY refuse basic content in which case it MUST raise a channel exception with reply code 540 (not implemented). routing_key: shortstr Message routing key Specifies the routing key for the message. The routing key is used for routing messages depending on the exchange configuration. mandatory: boolean indicate mandatory routing This flag tells the server how to react if the message cannot be routed to a queue. If this flag is True, the server will return an unroutable message with a Return method. If this flag is False, the server silently drops the message. RULE: The server SHOULD implement the mandatory flag. immediate: boolean request immediate delivery This flag tells the server how to react if the message cannot be routed to a queue consumer immediately. If this flag is set, the server will return an undeliverable message with a Return method. If this flag is zero, the server will queue the message, but with no guarantee that it will ever be consumed. RULE: The server SHOULD implement the immediate flag. ticket: short RULE: The client MUST provide a valid access ticket giving "write" access rights to the access realm for the exchange. """
args = AMQPWriter() if ticket is not None: args.write_short(ticket) else: args.write_short(self.default_ticket) args.write_shortstr(exchange) args.write_shortstr(routing_key) args.write_bit(mandatory) args.write_bit(immediate) self._send_method((60, 40), args, msg)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _basic_return(self, args, msg): """ return a failed message This method returns an undeliverable message that was published with the "immediate" flag set, or an unroutable message published with the "mandatory" flag set. The reply code and text provide information about the reason that the message was undeliverable. PARAMETERS: reply_code: short The reply code. The AMQ reply codes are defined in AMQ RFC 011. reply_text: shortstr The localised reply text. This text can be logged as an aid to resolving issues. exchange: shortstr Specifies the name of the exchange that the message was originally published to. routing_key: shortstr Message routing key Specifies the routing key name specified when the message was published. """
reply_code = args.read_short() reply_text = args.read_shortstr() exchange = args.read_shortstr() routing_key = args.read_shortstr() self.returned_messages.put( (reply_code, reply_text, exchange, routing_key, msg) )
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _wait_method(self, channel_id, allowed_methods): """ Wait for a method from the server destined for a particular channel. """
# # Check the channel's deferred methods # method_queue = self.channels[channel_id].method_queue for queued_method in method_queue: method_sig = queued_method[0] if (allowed_methods is None) \ or (method_sig in allowed_methods) \ or (method_sig == (20, 40)): method_queue.remove(queued_method) return queued_method # # Nothing queued, need to wait for a method from the peer # while True: channel, method_sig, args, content = \ self.method_reader.read_method() if (channel == channel_id) \ and ((allowed_methods is None) \ or (method_sig in allowed_methods) \ or (method_sig == (20, 40))): return method_sig, args, content # # Certain methods like basic_return should be dispatched # immediately rather than being queued, even if they're not # one of the 'allowed_methods' we're looking for. # if (channel != 0) and (method_sig in Channel._IMMEDIATE_METHODS): self.channels[channel].dispatch_method(method_sig, args, content) continue # # Not the channel and/or method we were looking for. Queue # this method for later # self.channels[channel].method_queue.append((method_sig, args, content)) # # If we just queued up a method for channel 0 (the Connection # itself) it's probably a close method in reaction to some # error, so deal with it right away. # if channel == 0: self.wait()
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def channel(self, channel_id=None): """ Fetch a Channel object identified by the numeric channel_id, or create that object if it doesn't already exist. """
if channel_id in self.channels: return self.channels[channel_id] return Channel(self, channel_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 _close(self, args): """ request a connection close This method indicates that the sender wants to close the connection. This may be due to internal conditions (e.g. a forced shut-down) or due to an error handling a specific method, i.e. an exception. When a close is due to an exception, the sender provides the class and method id of the method which caused the exception. RULE: After sending this method any received method except the Close-OK method MUST be discarded. RULE: The peer sending this method MAY use a counter or timeout to detect failure of the other peer to respond correctly with the Close-OK method. RULE: When a server receives the Close method from a client it MUST delete all server-side resources associated with the client's context. A client CANNOT reconnect to a context after sending or receiving a Close method. PARAMETERS: reply_code: short The reply code. The AMQ reply codes are defined in AMQ RFC 011. reply_text: shortstr The localised reply text. This text can be logged as an aid to resolving issues. class_id: short failing method class When the close is provoked by a method exception, this is the class of the method. method_id: short failing method ID When the close is provoked by a method exception, this is the ID of the method. """
reply_code = args.read_short() reply_text = args.read_shortstr() class_id = args.read_short() method_id = args.read_short() self._x_close_ok() raise AMQPConnectionException(reply_code, reply_text, (class_id, method_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 _x_open(self, virtual_host, capabilities='', insist=False): """ open connection to virtual host This method opens a connection to a virtual host, which is a collection of resources, and acts to separate multiple application domains within a server. RULE: The client MUST open the context before doing any work on the connection. PARAMETERS: virtual_host: shortstr virtual host name The name of the virtual host to work with. RULE: If the server supports multiple virtual hosts, it MUST enforce a full separation of exchanges, queues, and all associated entities per virtual host. An application, connected to a specific virtual host, MUST NOT be able to access resources of another virtual host. RULE: The server SHOULD verify that the client has permission to access the specified virtual host. RULE: The server MAY configure arbitrary limits per virtual host, such as the number of each type of entity that may be used, per connection and/or in total. capabilities: shortstr required capabilities The client may specify a number of capability names, delimited by spaces. The server can use this string to how to process the client's connection request. insist: boolean insist on connecting to server In a configuration with multiple load-sharing servers, the server may respond to a Connection.Open method with a Connection.Redirect. The insist option tells the server that the client is insisting on a connection to the specified server. RULE: When the client uses the insist option, the server SHOULD accept the client connection unless it is technically unable to do so. """
args = AMQPWriter() args.write_shortstr(virtual_host) args.write_shortstr(capabilities) args.write_bit(insist) self._send_method((10, 40), args) return self.wait(allowed_methods=[ (10, 41), # Connection.open_ok (10, 50), # Connection.redirect ])
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _open_ok(self, args): """ signal that the connection is ready This method signals to the client that the connection is ready for use. PARAMETERS: known_hosts: shortstr """
self.known_hosts = args.read_shortstr() AMQP_LOGGER.debug('Open OK! known_hosts [%s]' % self.known_hosts) 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 _redirect(self, args): """ asks the client to use a different server This method redirects the client to another server, based on the requested virtual host and/or capabilities. RULE: When getting the Connection.Redirect method, the client SHOULD reconnect to the host specified, and if that host is not present, to any of the hosts specified in the known-hosts list. PARAMETERS: host: shortstr server to connect to Specifies the server to connect to. This is an IP address or a DNS name, optionally followed by a colon and a port number. If no port number is specified, the client should use the default port number for the protocol. known_hosts: shortstr """
host = args.read_shortstr() self.known_hosts = args.read_shortstr() AMQP_LOGGER.debug('Redirected to [%s], known_hosts [%s]' % (host, self.known_hosts)) return host
<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, args): """ start connection negotiation This method starts the connection negotiation process by telling the client the protocol version that the server proposes, along with a list of security mechanisms which the client can use for authentication. RULE: If the client cannot handle the protocol version suggested by the server it MUST close the socket connection. RULE: The server MUST provide a protocol version that is lower than or equal to that requested by the client in the protocol header. If the server cannot support the specified protocol it MUST NOT send this method, but MUST close the socket connection. PARAMETERS: version_major: octet protocol major version The protocol major version that the server agrees to use, which cannot be higher than the client's major version. version_minor: octet protocol major version The protocol minor version that the server agrees to use, which cannot be higher than the client's minor version. server_properties: table server properties mechanisms: longstr available security mechanisms A list of the security mechanisms that the server supports, delimited by spaces. Currently ASL supports these mechanisms: PLAIN. locales: longstr available message locales A list of the message locales that the server supports, delimited by spaces. The locale defines the language in which the server will send reply texts. RULE: All servers MUST support at least the en_US locale. """
self.version_major = args.read_octet() self.version_minor = args.read_octet() self.server_properties = args.read_table() self.mechanisms = args.read_longstr().split(' ') self.locales = args.read_longstr().split(' ') AMQP_LOGGER.debug('Start from server, version: %d.%d, properties: %s, mechanisms: %s, locales: %s' % (self.version_major, self.version_minor, str(self.server_properties), self.mechanisms, self.locales))
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _x_start_ok(self, client_properties, mechanism, response, locale): """ select security mechanism and locale This method selects a SASL security mechanism. ASL uses SASL (RFC2222) to negotiate authentication and encryption. PARAMETERS: client_properties: table client properties mechanism: shortstr selected security mechanism A single security mechanisms selected by the client, which must be one of those specified by the server. RULE: The client SHOULD authenticate using the highest- level security profile it can handle from the list provided by the server. RULE: The mechanism field MUST contain one of the security mechanisms proposed by the server in the Start method. If it doesn't, the server MUST close the socket. response: longstr security response data A block of opaque data passed to the security mechanism. The contents of this data are defined by the SASL security mechanism. For the PLAIN security mechanism this is defined as a field table holding two fields, LOGIN and PASSWORD. locale: shortstr selected message locale A single message local selected by the client, which must be one of those specified by the server. """
args = AMQPWriter() args.write_table(client_properties) args.write_shortstr(mechanism) args.write_longstr(response) args.write_shortstr(locale) self._send_method((10, 11), args)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _tune(self, args): """ propose connection tuning parameters This method proposes a set of connection configuration values to the client. The client can accept and/or adjust these. PARAMETERS: channel_max: short proposed maximum channels The maximum total number of channels that the server allows per connection. Zero means that the server does not impose a fixed limit, but the number of allowed channels may be limited by available server resources. frame_max: long proposed maximum frame size The largest frame size that the server proposes for the connection. The client can negotiate a lower value. Zero means that the server does not impose any specific limit but may reject very large frames if it cannot allocate resources for them. RULE: Until the frame-max has been negotiated, both peers MUST accept frames of up to 4096 octets large. The minimum non-zero value for the frame- max field is 4096. heartbeat: short desired heartbeat delay The delay, in seconds, of the connection heartbeat that the server wants. Zero means the server does not want a heartbeat. """
self.channel_max = args.read_short() or self.channel_max self.frame_max = args.read_long() or self.frame_max self.method_writer.frame_max = self.frame_max self.heartbeat = args.read_short() self._x_tune_ok(self.channel_max, self.frame_max, 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 _send_method(self, method_sig, args=bytes(), content=None): """ Send a method for our channel. """
if isinstance(args, AMQPWriter): args = args.getvalue() self.connection.method_writer.write_method(self.channel_id, method_sig, args, 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 read_frame(self): """ Read an AMQP frame. """
frame_type, channel, size = unpack('>BHI', self._read(7)) payload = self._read(size) ch = ord(self._read(1)) if ch == 206: # '\xce' return frame_type, channel, payload else: raise Exception('Framing Error, received 0x%02x while expecting 0xce' % ch)
<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_frame(self, frame_type, channel, payload): """ Write out an AMQP frame. """
size = len(payload) self._write(pack('>BHI%dsB' % size, frame_type, channel, size, payload, 0xce))
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _setup_transport(self): """ Wrap the socket in an SSL object, either the new Python 2.6 version, or the older Python 2.5 and lower version. """
if HAVE_PY26_SSL: if hasattr(self, 'sslopts'): self.sslobj = ssl.wrap_socket(self.sock, **self.sslopts) else: self.sslobj = ssl.wrap_socket(self.sock) self.sslobj.do_handshake() else: self.sslobj = socket.ssl(self.sock)
<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_bit(self, b): """ Write a boolean value. """
if b: b = 1 else: b = 0 shift = self.bitcount % 8 if shift == 0: self.bits.append(0) self.bits[-1] |= (b << shift) self.bitcount += 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 on_unexpected_error(e): # pragma: no cover """Catch-all error handler Unexpected errors will be handled by this function. """
sys.stderr.write('Unexpected error: {} ({})\n'.format( str(e), e.__class__.__name__)) sys.stderr.write('See file slam_error.log for additional details.\n') sys.exit(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 init(name, description, bucket, timeout, memory, stages, requirements, function, runtime, config_file, **kwargs): """Generate a configuration file."""
if os.path.exists(config_file): raise RuntimeError('Please delete the old version {} if you want to ' 'reconfigure your project.'.format(config_file)) module, app = function.split(':') if not name: name = module.replace('_', '-') if not re.match('^[a-zA-Z][-a-zA-Z0-9]*$', name): raise ValueError('The name {} is invalid, only letters, numbers and ' 'dashes are allowed.'.format(name)) if not bucket: random_suffix = ''.join( random.choice(string.ascii_uppercase + string.digits) for n in range(8)) bucket = '{}-{}'.format(name, random_suffix) stages = [s.strip() for s in stages.split(',')] if runtime is None: if sys.version_info[0] == 2: # pragma: no cover runtime = 'python2.7' else: runtime = 'python3.6' # generate slam.yaml template_file = os.path.join(os.path.dirname(__file__), 'templates/slam.yaml') with open(template_file) as f: template = f.read() template = render_template(template, name=name, description=description, module=module, app=app, bucket=bucket, timeout=timeout, memory=memory, requirements=requirements, stages=stages, devstage=stages[0], runtime=runtime) with open(config_file, 'wt') as f: f.write(template) # plugins config = _load_config(config_file) for name, plugin in plugins.items(): # write plugin documentation as a comment in config file with open(config_file, 'at') as f: f.write('\n\n# ' + (plugin.__doc__ or name).replace( '\n', '\n# ') + '\n') if hasattr(plugin, 'init'): arguments = {k: v for k, v in kwargs.items() if k in getattr(plugin.init, '_argnames', [])} plugin_config = plugin.init.func(config=config, **arguments) if plugin_config: with open(config_file, 'at') as f: yaml.dump({name: plugin_config}, f, default_flow_style=False) print('The configuration file for your project has been generated. ' 'Remember to add {} to source control.'.format(config_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 _generate_lambda_handler(config, output='.slam/handler.py'): """Generate a handler.py file for the lambda function start up."""
# Determine what the start up code is. The default is to just run the # function, but it can be overriden by a plugin such as wsgi for a more # elaborated way to run the function. run_function = _run_lambda_function for name, plugin in plugins.items(): if name in config and hasattr(plugin, 'run_lambda_function'): run_function = plugin.run_lambda_function run_code = ''.join(inspect.getsourcelines(run_function)[0][1:]) # generate handler.py with open(os.path.join(os.path.dirname(__file__), 'templates/handler.py.template')) as f: template = f.read() template = render_template(template, module=config['function']['module'], app=config['function']['app'], run_lambda_function=run_code, config_json=json.dumps(config, separators=(',', ':'))) with open(output, 'wt') as f: f.write(template + '\n')
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def build(rebuild_deps, config_file): """Build lambda package."""
config = _load_config(config_file) print("Building lambda package...") package = _build(config, rebuild_deps=rebuild_deps) print("{} has been built successfully.".format(package))
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def deploy(stage, lambda_package, no_lambda, rebuild_deps, config_file): """Deploy the project to the development stage."""
config = _load_config(config_file) if stage is None: stage = config['devstage'] s3 = boto3.client('s3') cfn = boto3.client('cloudformation') region = _get_aws_region() # obtain previous deployment if it exists previous_deployment = None try: previous_deployment = cfn.describe_stacks( StackName=config['name'])['Stacks'][0] except botocore.exceptions.ClientError: pass # build lambda package if required built_package = False new_package = True if lambda_package is None and not no_lambda: print("Building lambda package...") lambda_package = _build(config, rebuild_deps=rebuild_deps) built_package = True elif lambda_package is None: # preserve package from previous deployment new_package = False lambda_package = _get_from_stack(previous_deployment, 'Parameter', 'LambdaS3Key') # create S3 bucket if it doesn't exist yet bucket = config['aws']['s3_bucket'] _ensure_bucket_exists(s3, bucket, region) # upload lambda package to S3 if new_package: s3.upload_file(lambda_package, bucket, lambda_package) if built_package: # we created the package, so now that is on S3 we can delete it os.remove(lambda_package) # prepare cloudformation template template_body = get_cfn_template(config) parameters = [ {'ParameterKey': 'LambdaS3Bucket', 'ParameterValue': bucket}, {'ParameterKey': 'LambdaS3Key', 'ParameterValue': lambda_package}, ] stages = list(config['stage_environments'].keys()) stages.sort() for s in stages: param = s.title() + 'Version' if s != stage: v = _get_from_stack(previous_deployment, 'Parameter', param) \ if previous_deployment else '$LATEST' v = v or '$LATEST' else: v = '$LATEST' parameters.append({'ParameterKey': param, 'ParameterValue': v}) # run the cloudformation template if previous_deployment is None: print('Deploying {}:{}...'.format(config['name'], stage)) cfn.create_stack(StackName=config['name'], TemplateBody=template_body, Parameters=parameters, Capabilities=['CAPABILITY_IAM']) waiter = cfn.get_waiter('stack_create_complete') else: print('Updating {}:{}...'.format(config['name'], stage)) cfn.update_stack(StackName=config['name'], TemplateBody=template_body, Parameters=parameters, Capabilities=['CAPABILITY_IAM']) waiter = cfn.get_waiter('stack_update_complete') # wait for cloudformation to do its thing try: waiter.wait(StackName=config['name']) except botocore.exceptions.ClientError: # the update failed, so we remove the lambda package from S3 if built_package: s3.delete_object(Bucket=bucket, Key=lambda_package) raise else: if previous_deployment and new_package: # the update succeeded, so it is safe to delete the lambda package # used by the previous deployment old_pkg = _get_from_stack(previous_deployment, 'Parameter', 'LambdaS3Key') s3.delete_object(Bucket=bucket, Key=old_pkg) # we are done, show status info and exit _print_status(config)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def publish(version, stage, config_file): """Publish a version of the project to a stage."""
config = _load_config(config_file) cfn = boto3.client('cloudformation') if version is None: version = config['devstage'] elif version not in config['stage_environments'].keys() and \ not version.isdigit(): raise ValueError('Invalid version. Use a stage name or a numeric ' 'version number.') if version == stage: raise ValueError('Cannot deploy a stage into itself.') # obtain previous deployment try: previous_deployment = cfn.describe_stacks( StackName=config['name'])['Stacks'][0] except botocore.exceptions.ClientError: raise RuntimeError('This project has not been deployed yet.') # preserve package from previous deployment bucket = _get_from_stack(previous_deployment, 'Parameter', 'LambdaS3Bucket') lambda_package = _get_from_stack(previous_deployment, 'Parameter', 'LambdaS3Key') # prepare cloudformation template template_body = get_cfn_template(config) parameters = [ {'ParameterKey': 'LambdaS3Bucket', 'ParameterValue': bucket}, {'ParameterKey': 'LambdaS3Key', 'ParameterValue': lambda_package}, ] stages = list(config['stage_environments'].keys()) stages.sort() for s in stages: param = s.title() + 'Version' if s != stage: v = _get_from_stack(previous_deployment, 'Parameter', param) \ if previous_deployment else '$LATEST' v = v or '$LATEST' else: if version.isdigit(): # explicit version number v = version else: # publish version from a stage v = _get_from_stack(previous_deployment, 'Parameter', version.title() + 'Version') if v == '$LATEST': # publish a new version from $LATEST lmb = boto3.client('lambda') v = lmb.publish_version(FunctionName=_get_from_stack( previous_deployment, 'Output', 'FunctionArn'))[ 'Version'] parameters.append({'ParameterKey': param, 'ParameterValue': v}) # run the cloudformation template print('Publishing {}:{} to {}...'.format(config['name'], version, stage)) cfn.update_stack(StackName=config['name'], TemplateBody=template_body, Parameters=parameters, Capabilities=['CAPABILITY_IAM']) waiter = cfn.get_waiter('stack_update_complete') # wait for cloudformation to do its thing try: waiter.wait(StackName=config['name']) except botocore.exceptions.ClientError: raise # we are done, show status info and exit _print_status(config)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def invoke(stage, async, dry_run, config_file, args): """Invoke the lambda function."""
config = _load_config(config_file) if stage is None: stage = config['devstage'] cfn = boto3.client('cloudformation') lmb = boto3.client('lambda') try: stack = cfn.describe_stacks(StackName=config['name'])['Stacks'][0] except botocore.exceptions.ClientError: raise RuntimeError('This project has not been deployed yet.') function = _get_from_stack(stack, 'Output', 'FunctionArn') if dry_run: invocation_type = 'DryRun' elif async: invocation_type = 'Event' else: invocation_type = 'RequestResponse' # parse input arguments data = {} for arg in args: s = arg.split('=', 1) if len(s) != 2: raise ValueError('Invalid argument ' + arg) if s[0][-1] == ':': # JSON argument data[s[0][:-1]] = json.loads(s[1]) else: # string argument data[s[0]] = s[1] rv = lmb.invoke(FunctionName=function, InvocationType=invocation_type, Qualifier=stage, Payload=json.dumps({'kwargs': data}, sort_keys=True)) if rv['StatusCode'] != 200 and rv['StatusCode'] != 202: raise RuntimeError('Unexpected error. Status code = {}.'.format( rv['StatusCode'])) if invocation_type == 'RequestResponse': payload = json.loads(rv['Payload'].read().decode('utf-8')) if 'FunctionError' in rv: if 'stackTrace' in payload: print('Traceback (most recent call last):') for frame in payload['stackTrace']: print(' File "{}", line {}, in {}'.format( frame[0], frame[1], frame[2])) print(' ' + frame[3]) print('{}: {}'.format(payload['errorType'], payload['errorMessage'])) else: raise RuntimeError('Unknown error') else: print(str(payload))
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def template(config_file): """Print the default Cloudformation deployment template."""
config = _load_config(config_file) print(get_cfn_template(config, pretty=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 register_plugins(): """find any installed plugins and register them."""
if pkg_resources: # pragma: no cover for ep in pkg_resources.iter_entry_points('slam_plugins'): plugin = ep.load() # add any init options to the main init command if hasattr(plugin, 'init') and hasattr(plugin.init, '_arguments'): for arg in plugin.init._arguments: init.parser.add_argument(*arg[0], **arg[1]) init._arguments += plugin.init._arguments init._argnames += plugin.init._argnames plugins[ep.name] = plugin
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def synchronise_device_state(self, device_state, authentication_headers): """ Synchronizing the component states with AVS Components state must be synchronised with AVS after establishing the downchannel stream in order to create a persistent connection with AVS. Note that currently this function is paying lip-service synchronising the device state: the device state is hard-coded. """
payload = { 'context': device_state, 'event': { 'header': { 'namespace': 'System', 'name': 'SynchronizeState', 'messageId': '' }, 'payload': {} } } multipart_data = MultipartEncoder( fields=[ ( 'metadata', ( 'metadata', json.dumps(payload), 'application/json', {'Content-Disposition': "form-data; name='metadata'"} ) ), ], boundary='boundary' ) headers = { **authentication_headers, 'Content-Type': multipart_data.content_type } stream_id = self.connection.request( 'GET', '/v20160207/events', body=multipart_data, headers=headers, ) response = self.connection.get_response(stream_id) assert response.status in [http.client.NO_CONTENT, http.client.OK]
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def send_audio_file( self, audio_file, device_state, authentication_headers, dialog_request_id, distance_profile, audio_format ): """ Send audio to AVS The file-like object are steaming uploaded for improved latency. Returns: bytes -- wav audio bytes returned from AVS """
payload = { 'context': device_state, 'event': { 'header': { 'namespace': 'SpeechRecognizer', 'name': 'Recognize', 'messageId': self.generate_message_id(), 'dialogRequestId': dialog_request_id, }, 'payload': { 'profile': distance_profile, 'format': audio_format } } } multipart_data = MultipartEncoder( fields=[ ( 'request', ( 'request', json.dumps(payload), 'application/json;', {'Content-Disposition': "form-data; name='request'"} ), ), ( 'audio', ( 'audio', audio_file, 'application/octet-stream', {'Content-Disposition': "form-data; name='audio'"} ) ), ], boundary='boundary', ) headers = { **authentication_headers, 'Content-Type': multipart_data.content_type } stream_id = self.connection.request( 'POST', '/v20160207/events', headers=headers, body=multipart_data, ) response = self.connection.get_response(stream_id) return self.parse_response(response)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def retrieve_api_token(self): """ Retrieve the access token from AVS. This function is memoized, so the value returned by the function will be remembered and returned by subsequent calls until the memo expires. This is because the access token lasts for one hour, then a new token needs to be requested. Decorators: helpers.expiring_memo Returns: str -- The access token for communicating with AVS """
payload = self.oauth2_manager.get_access_token_params( refresh_token=self.refresh_token ) response = requests.post( self.oauth2_manager.access_token_url, json=payload ) response.raise_for_status() response_json = json.loads(response.text) return response_json['access_token']
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def webpack_template_tag(path_to_config): """ A template tag that will output a webpack bundle. Usage: {% load webpack %} {% webpack 'path/to/webpack.config.js' as bundle %} {{ bundle.render_css|safe }} {{ bundle.render_js|safe }} """
# TODO: allow selection of entries # Django's template system silently fails on some exceptions try: return webpack(path_to_config) except (AttributeError, ValueError) as e: raise six.reraise(BundlingError, BundlingError(*e.args), sys.exc_info()[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 _prepare_key(key, *args, **kwargs): """ if arguments are given, adds a hash of the args to the key. """
if not args and not kwargs: return key items = sorted(kwargs.items()) hashable_args = (args, tuple(items)) args_key = hashlib.md5(pickle.dumps(hashable_args)).hexdigest() return "%s/args:%s" % (key, args_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 setup(logdir='log'): """ Set up dual logging to console and to logfile. When this function is called, it first creates the given directory. It then creates a logfile and passes all log messages to come to it. The logfile name encodes the date and time when it was created, for example "20181115-153559.txt". All messages with a log level of at least "WARNING" are also forwarded to the console. Args: logdir: path of the directory where to store the log files. Both a relative or an absolute path may be specified. If a relative path is specified, it is interpreted relative to the working directory. If no directory is given, the logs are written to a folder called "log" in the working directory. """
# Create the root logger. logger = logging.getLogger() logger.setLevel(logging.DEBUG) # Validate the given directory. logdir = os.path.normpath(logdir) # Create a folder for the logfiles. if not os.path.exists(logdir): os.makedirs(logdir) # Construct the logfile name. t = datetime.datetime.now() logfile = '{year:04d}{mon:02d}{day:02d}-' \ '{hour:02d}{min:02d}{sec:02d}.log'.format( year=t.year, mon=t.month, day=t.day, hour=t.hour, min=t.minute, sec=t.second) logfile = os.path.join(logdir, logfile) # Set up logging to the logfile. filehandler = logging.handlers.RotatingFileHandler( filename=logfile, maxBytes=10*1024*1024, backupCount=100) filehandler.setLevel(logging.DEBUG) fileformatter = logging.Formatter( '%(asctime)s %(levelname)-8s: %(message)s') filehandler.setFormatter(fileformatter) logger.addHandler(filehandler) # Set up logging to the console. streamhandler = logging.StreamHandler() streamhandler.setLevel(logging.WARNING) streamformatter = logging.Formatter('%(levelname)s: %(message)s') streamhandler.setFormatter(streamformatter) logger.addHandler(streamhandler)
<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_versions() -> FileVersionResult: """ Search specific project files and extract versions to check. :return: A FileVersionResult object for reporting. """
version_counter = Counter() versions_match = False version_str = None versions_discovered = OrderedDict() for version_obj in version_objects: discovered = version_obj.get_version() versions_discovered[version_obj.key_name] = discovered version_counter.update([discovered]) if len(version_counter) == 1: versions_match = True version_str = list(version_counter.keys())[0] return FileVersionResult( uniform=versions_match, version_details=versions_discovered, version_result=version_str, )
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def supportedChars(*tests): """ Takes any number of strings, and returns the first one the terminal encoding supports. If none are supported it returns '?' the length of the first string. """
for test in tests: try: test.encode(sys.stdout.encoding) return test except UnicodeEncodeError: pass return '?' * len(tests[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 app_templates_dirs(self): """ Build a cached dict with settings.INSTALLED_APPS as keys and the 'templates' directory of each application as values. """
app_templates_dirs = OrderedDict() for app_config in apps.get_app_configs(): templates_dir = os.path.join( getattr(app_config, 'path', '/'), 'templates') if os.path.isdir(templates_dir): templates_dir = upath(templates_dir) app_templates_dirs[app_config.name] = templates_dir app_templates_dirs[app_config.label] = templates_dir return app_templates_dirs
<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_contents(self, origin): """ Try to load the origin. """
try: path = self.get_app_template_path( origin.app_name, origin.template_name) with io.open(path, encoding=self.engine.file_charset) as fp: return fp.read() except KeyError: raise TemplateDoesNotExist(origin) except IOError as error: if error.errno == errno.ENOENT: raise TemplateDoesNotExist(origin) raise
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def load_template_source(self, *ka): """ Backward compatible method for Django < 2.0. """
template_name = ka[0] for origin in self.get_template_sources(template_name): try: return self.get_contents(origin), origin.name except TemplateDoesNotExist: pass raise TemplateDoesNotExist(template_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 rotateImage(image, angle): """ rotates a 2d array to a multiple of 90 deg. 0 = default 1 = 90 deg. cw 2 = 180 deg. 3 = 90 deg. ccw """
image = [list(row) for row in image] for n in range(angle % 4): image = list(zip(*image[::-1])) return image
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def overlaps(self, canvas, exclude=[]): """ Returns True if sprite is touching any other sprite. """
try: exclude = list(exclude) except TypeError: exclude = [exclude] exclude.append(self) for selfY, row in enumerate(self.image.image()): for selfX, pixel in enumerate(row): canvasPixelOn = canvas.testPixel( (selfX + self.position[0], selfY + self.position[1]), excludedSprites=exclude ) if pixel and canvasPixelOn: 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 onEdge(self, canvas): """ Returns a list of the sides of the sprite which are touching the edge of the canvas. 0 = Bottom 1 = Left 2 = Top 3 = Right """
sides = [] if int(self.position[0]) <= 0: sides.append(1) if (int(self.position[0]) + self.image.width) >= canvas.width: sides.append(3) if int(self.position[1]) <= 0: sides.append(2) if (int(self.position[1]) + self.image.height) >= canvas.height: sides.append(0) return sides
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def remote_property(name, get_command, set_command, field_name, doc=None): """Property decorator that facilitates writing properties for values from a remote device. Arguments: name: The field name to use on the local object to store the cached property. get_command: A function that returns the remote value of the property. set_command: A function that accepts a new value for the property and sets it remotely. field_name: The name of the field to retrieve from the response message to get operations. """
def getter(self): try: return getattr(self, name) except AttributeError: value = getattr(self.sendCommand(get_command()), field_name) setattr(self, name, value) return value def setter(self, value): setattr(self, name, value) self.sendCommand(set_command(value)) return property(getter, setter, doc=doc)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def sendCommand(self, command): """Sends a Command object to the MCP2210 and returns its response. Arguments: A commands.Command instance Returns: A commands.Response instance, or raises a CommandException on error. """
command_data = [ord(x) for x in buffer(command)] self.hid.write(command_data) response_data = ''.join(chr(x) for x in self.hid.read(64)) response = command.RESPONSE.from_buffer_copy(response_data) if response.status != 0: raise CommandException(response.status) return response
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def transfer(self, data): """Transfers data over SPI. Arguments: data: The data to transfer. Returns: The data returned by the SPI device. """
settings = self.transfer_settings settings.spi_tx_size = len(data) self.transfer_settings = settings response = '' for i in range(0, len(data), 60): response += self.sendCommand(commands.SPITransferCommand(data[i:i + 60])).data time.sleep(0.01) while len(response) < len(data): response += self.sendCommand(commands.SPITransferCommand('')).data return ''.join(response)
<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_json_response(self, context_dict, status=200): """ Limited serialization for shipping plain data. Do not use for models or other complex or custom objects. """
json_context = json.dumps( context_dict, cls=DjangoJSONEncoder, **self.get_json_dumps_kwargs() ).encode(u'utf-8') return HttpResponse( json_context, content_type=self.get_content_type(), status=status )
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def form_valid(self, form): """ If the request is ajax, save the form and return a json response. Otherwise return super as expected. """
self.object = form.save(commit=False) self.pre_save() self.object.save() if hasattr(form, 'save_m2m'): form.save_m2m() self.post_save() if self.request.is_ajax(): return self.render_json_response(self.get_success_result()) return HttpResponseRedirect(self.get_success_url())
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def form_invalid(self, form): """ We have errors in the form. If ajax, return them as json. Otherwise, proceed as normal. """
if self.request.is_ajax(): return self.render_json_response(self.get_error_result(form)) return super(AjaxFormMixin, self).form_invalid(form)