{"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nadd the item to the control associating the given data if not None.", "response": "def append(self, a_string, data=None):\r\n \"Adds the item to the control, associating the given data if not None.\"\r\n self.wx_obj.Append(a_string, data)\r\n # reverse association:\r\n self._items_dict[data] = a_string"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ndeleting the item at the zero - based index n from the control.", "response": "def delete(self, a_position):\r\n \"Deletes the item at the zero-based index 'n' from the control.\"\r\n self.wx_obj.Delete(a_position)\r\n data = self.get_data()\r\n if data in self._items_dict:\r\n del self._items_dict[data]"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef represent(obj, prefix, parent=\"\", indent=0, context=False, max_cols=80):\r\n \"Construct a string representing the object\"\r\n try:\r\n name = getattr(obj, \"name\", \"\")\r\n class_name = \"%s.%s\" % (prefix, obj.__class__.__name__)\r\n padding = len(class_name) + 1 + indent * 4 + (5 if context else 0)\r\n params = []\r\n for (k, spec) in sorted(obj._meta.specs.items(), key=get_sort_key):\r\n if k == \"index\": # index is really defined by creation order\r\n continue # also, avoid infinite recursion\r\n if k == \"parent\" and parent != \"\":\r\n v = parent\r\n else:\r\n v = getattr(obj, k, \"\")\r\n if (not isinstance(spec, InternalSpec) \r\n and v != spec.default\r\n and (k != 'id' or v > 0) \r\n and isinstance(v, \r\n (basestring, int, long, float, bool, dict, list, \r\n decimal.Decimal, \r\n datetime.datetime, datetime.date, datetime.time,\r\n Font, Color)) \r\n and repr(v) != 'None'\r\n ):\r\n v = repr(v)\r\n else:\r\n v = None\r\n if v is not None:\r\n params.append(\"%s=%s\" % (k, v)) \r\n param_lines = []\r\n line = \"\"\r\n for param in params:\r\n if len(line + param) + 3 > max_cols - padding:\r\n param_lines.append(line)\r\n line = \"\"\r\n line += param + \", \"\r\n param_lines.append(line)\r\n param_str = (\"\\n%s\" % (\" \" * padding)).join(param_lines)\r\n return \"%s(%s)\" % (class_name, param_str)\r\n except:\r\n raise\r\n # uninitialized, use standard representation to not break debuggers\r\n return object.__repr__(obj)", "response": "Construct a string representing the object"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nfinds an object already created", "response": "def get(obj_name, init=False):\r\n \"Find an object already created\"\r\n wx_parent = None\r\n # check if new_parent is given as string (useful for designer!)\r\n if isinstance(obj_name, basestring):\r\n # find the object reference in the already created gui2py objects\r\n # TODO: only useful for designer, get a better way\r\n obj_parent = COMPONENTS.get(obj_name)\r\n if not obj_parent:\r\n # try to find window (it can be a plain wx frame/control)\r\n wx_parent = wx.FindWindowByName(obj_name)\r\n if wx_parent:\r\n # store gui object (if any)\r\n obj_parent = getattr(wx_parent, \"obj\") \r\n else:\r\n # fallback using just object name (backward compatibility)\r\n for obj in COMPONENTS.values():\r\n if obj.name==obj_name:\r\n obj_parent = obj \r\n else:\r\n obj_parent = obj_name # use the provided parent (as is)\r\n return obj_parent or wx_parent"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nrecreates ( if needed ) the wx_obj and apply new properties", "response": "def rebuild(self, recreate=True, force=False, **kwargs):\r\n \"Recreate (if needed) the wx_obj and apply new properties\"\r\n # detect if this involves a spec that needs to recreate the wx_obj:\r\n needs_rebuild = any([isinstance(spec, (StyleSpec, InitSpec)) \r\n for spec_name, spec in self._meta.specs.items()\r\n if spec_name in kwargs])\r\n # validate if this gui object needs and support recreation\r\n if needs_rebuild and recreate or force:\r\n if DEBUG: print \"rebuilding window!\"\r\n # recreate the wx_obj! warning: it will call Destroy()\r\n self.__init__(**kwargs) \r\n else:\r\n if DEBUG: print \"just setting attr!\"\r\n for name, value in kwargs.items():\r\n setattr(self, name, value)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef destroy(self):\r\n \"Remove event references and destroy wx object (and children)\"\r\n # unreference the obj from the components map and parent\r\n if self._name:\r\n del COMPONENTS[self._get_fully_qualified_name()]\r\n if DEBUG: print \"deleted from components!\"\r\n if isinstance(self._parent, Component):\r\n del self._parent[self._name]\r\n if DEBUG: print \"deleted from parent!\"\r\n # destroy the wx_obj (only if sure that reference is not needed!)\r\n if self.wx_obj:\r\n self.wx_obj.Destroy()\r\n for child in self:\r\n print \"destroying child\", \r\n child.destroy()\r\n # destroy the designer selection marker (if any)\r\n if hasattr(self, 'sel_marker') and self.sel_marker:\r\n self.sel_marker.destroy()\r\n if hasattr(self, 'facade') and self.facade:\r\n self.facade.destroy()", "response": "Remove event references and destroy wx object and children"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef duplicate(self, new_parent=None):\r\n \"Create a new object exactly similar to self\"\r\n kwargs = {}\r\n for spec_name, spec in self._meta.specs.items():\r\n value = getattr(self, spec_name)\r\n if isinstance(value, Color):\r\n print \"COLOR\", value, value.default\r\n if value.default:\r\n value = None\r\n if value is not None:\r\n kwargs[spec_name] = value\r\n del kwargs['parent'] \r\n new_id = wx.NewId()\r\n kwargs['id'] = new_id\r\n kwargs['name'] = \"%s_%s\" % (kwargs['name'], new_id)\r\n new_obj = self.__class__(new_parent or self.get_parent(), **kwargs)\r\n # recursively create a copy of each child (in the new parent!)\r\n for child in self:\r\n child.duplicate(new_obj)\r\n return new_obj", "response": "Create a new object exactly similar to self"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef reindex(self, z=None):\r\n \"Raises/lower the component in the window hierarchy (Z-order/tab order)\"\r\n # z=0: lowers(first index), z=-1: raises (last)\r\n # actually, only useful in design mode\r\n if isinstance(self._parent, Component):\r\n # get the current index (z-order)\r\n if not self in self._parent._children_list:\r\n return len(self._parent._children_list)\r\n i = self._parent._children_list.index(self)\r\n if z is None:\r\n return i\r\n if not hasattr(self, \"designer\") and not self.designer:\r\n raise RuntimeError(\"reindexing can only be done on design mode\")\r\n # delete the element reference from the list\r\n del self._parent._children_list[i]\r\n # insert as last element\r\n if z < 0:\r\n self._parent._children_list.append(self)\r\n else:\r\n self._parent._children_list.insert(z, self)", "response": "Raises / lower the component in the window hierarchy ( Z - order / tab order )"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nstoring the gui / wx object parent for this component", "response": "def set_parent(self, new_parent, init=False):\r\n \"Store the gui/wx object parent for this component\"\r\n # set init=True if this is called from the constructor\r\n self._parent = get(new_parent, init)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _get_parent_name(self):\r\n \"Return parent window name (used in __repr__ parent spec)\"\r\n parent = self.get_parent()\r\n parent_names = []\r\n while parent:\r\n if isinstance(parent, Component):\r\n parent_name = parent.name\r\n # Top Level Windows has no parent!\r\n if parent_name:\r\n parent_names.insert(0, parent_name)\r\n parent = parent.get_parent()\r\n else:\r\n break\r\n if not parent_names:\r\n return None\r\n else:\r\n return '.'.join(parent_names)", "response": "Return parent window name ( used in __repr__ parent spec )"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn full parents name + self name ( useful as key )", "response": "def _get_fully_qualified_name(self):\r\n \"return full parents name + self name (useful as key)\"\r\n parent_name = self._get_parent_name()\r\n if not parent_name:\r\n return self._name\r\n else:\r\n return \"%s.%s\" % (parent_name, self._name)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef snapshot(self):\r\n \"Capture the screen appearance of the control (to be used as facade)\"\r\n width, height = self.wx_obj.GetSize()\r\n bmp = wx.EmptyBitmap(width, height)\r\n wdc = wx.ClientDC(self.wx_obj)\r\n mdc = wx.MemoryDC(bmp)\r\n mdc.Blit(0, 0, width, height, wdc, 0, 0)\r\n #bmp.SaveFile(\"test.bmp\", wx.BITMAP_TYPE_BMP)\r\n wdc.Destroy()\r\n mdc.Destroy()\r\n return bmp", "response": "Capture the screen appearance of the control ( to be used as facade )"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncall when adding a control to the window", "response": "def _sizer_add(self, child):\r\n \"called when adding a control to the window\"\r\n if self.sizer:\r\n if DEBUG: print \"adding to sizer:\", child.name\r\n border = None\r\n if not border:\r\n border = child.sizer_border\r\n flags = child._sizer_flags\r\n if child.sizer_align:\r\n flags |= child._sizer_align\r\n if child.sizer_expand:\r\n flags |= wx.EXPAND\r\n if 'grid' in self.sizer:\r\n self._sizer.Add(child.wx_obj, flag=flags, border=border, \r\n pos=(child.sizer_row, child.sizer_col), \r\n span=(child.sizer_rowspan, child.sizer_colspan))\r\n else:\r\n self._sizer.Add(child.wx_obj, 0, flags, border)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef set_parent(self, new_parent, init=False):\r\n \"Re-parent a child control with the new wx_obj parent\"\r\n Component.set_parent(self, new_parent, init)\r\n # if not called from constructor, we must also reparent in wx:\r\n if not init:\r\n if DEBUG: print \"reparenting\", ctrl.name\r\n if hasattr(self.wx_obj, \"Reparent\"):\r\n self.wx_obj.Reparent(self._parent.wx_obj)", "response": "Re - parent a child control with the new wx_obj parent"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncalculates final pos and size ( auto absolute in pixels & relativa", "response": "def _calc_dimension(self, dim_val, dim_max, font_dim):\r\n \"Calculate final pos and size (auto, absolute in pixels & relativa)\"\r\n if dim_val is None:\r\n return -1 # let wx automatic pos/size\r\n elif isinstance(dim_val, int):\r\n return dim_val # use fixed pixel value (absolute)\r\n elif isinstance(dim_val, basestring):\r\n if dim_val.endswith(\"%\"):\r\n # percentaje, relative to parent max size:\r\n dim_val = int(dim_val[:-1])\r\n dim_val = dim_val / 100.0 * dim_max\r\n elif dim_val.endswith(\"em\"):\r\n # use current font size (suport fractions):\r\n dim_val = float(dim_val[:-2])\r\n dim_val = dim_val * font_dim\r\n elif dim_val.endswith(\"px\"):\r\n # fixed pixels\r\n dim_val = dim_val[:-2]\r\n elif dim_val == \"\" or dim_val == \"auto\":\r\n dim_val = -1\r\n return int(dim_val)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef __tile_background(self, dc):\r\n \"make several copies of the background bitmap\"\r\n sz = self.wx_obj.GetClientSize()\r\n bmp = self._bitmap.get_bits()\r\n w = bmp.GetWidth()\r\n h = bmp.GetHeight()\r\n\r\n if isinstance(self, wx.ScrolledWindow):\r\n # adjust for scrolled position\r\n spx, spy = self.wx_obj.GetScrollPixelsPerUnit()\r\n vsx, vsy = self.wx_obj.GetViewStart()\r\n dx, dy = (spx * vsx) % w, (spy * vsy) % h\r\n else:\r\n dx, dy = (w, h)\r\n\r\n x = -dx\r\n while x < sz.width:\r\n y = -dy\r\n while y < sz.height:\r\n dc.DrawBitmap(bmp, x, y)\r\n y = y + h\r\n x = x + w", "response": "make several copies of the background bitmap"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ndraw the image as background", "response": "def __on_erase_background(self, evt):\r\n \"Draw the image as background\"\r\n if self._bitmap:\r\n dc = evt.GetDC()\r\n \r\n if not dc:\r\n dc = wx.ClientDC(self)\r\n r = self.wx_obj.GetUpdateRegion().GetBox()\r\n dc.SetClippingRegion(r.x, r.y, r.width, r.height)\r\n \r\n if self._background_tiling:\r\n self.__tile_background(dc)\r\n else:\r\n dc.DrawBitmapPoint(self._bitmap.get_bits(), (0, 0))"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef set_parent(self, new_parent, init=False):\r\n \"Associate the component to the control (it could be recreated)\"\r\n # store gui reference inside of wx object (this will enable rebuild...)\r\n self._parent = get(new_parent, init=False) # store new parent\r\n if init:\r\n self._parent[self._name] = self", "response": "Associate the component to the control ( it could be recreated"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef rebuild(self, **kwargs):\r\n \"Update a property value with (used by the designer)\"\r\n for name, value in kwargs.items():\r\n setattr(self, name, value)", "response": "Update a property value with ( used by the designer )"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef __on_paint(self, event):\r\n \"Custom draws the label when transparent background is needed\"\r\n # use a Device Context that supports anti-aliased drawing \r\n # and semi-transparent colours on all platforms\r\n dc = wx.GCDC(wx.PaintDC(self.wx_obj))\r\n dc.SetFont(self.wx_obj.GetFont())\r\n dc.SetTextForeground(self.wx_obj.GetForegroundColour())\r\n dc.DrawText(self.wx_obj.GetLabel(), 0, 0)", "response": "Custom draws the label when transparent background is needed"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef find_modules(rootpath, skip):\r\n\r\n INITPY = '__init__.py'\r\n\r\n rootpath = os.path.normpath(os.path.abspath(rootpath))\r\n if INITPY in os.listdir(rootpath):\r\n root_package = rootpath.split(os.path.sep)[-1]\r\n print \"Searching modules in\", rootpath\r\n else:\r\n print \"No modules in\", rootpath\r\n return\r\n\r\n def makename(package, module):\r\n \"\"\"Join package and module with a dot.\"\"\"\r\n if package:\r\n name = package\r\n if module:\r\n name += '.' + module\r\n else:\r\n name = module\r\n return name\r\n\r\n skipall = []\r\n for m in skip.keys():\r\n if skip[m] is None: skipall.append(m)\r\n\r\n \r\n\r\n tree = {}\r\n saved = 0\r\n found = 0\r\n def save(module, submodule):\r\n name = module+ \".\"+ submodule\r\n \r\n for s in skipall:\r\n if name.startswith(s):\r\n print \"Skipping \"+name\r\n return False\r\n if skip.has_key(module):\r\n if submodule in skip[module]:\r\n print \"Skipping \"+name\r\n return False\r\n if not tree.has_key(module):\r\n tree[module] = []\r\n tree[module].append(submodule)\r\n return True\r\n \r\n for root, subs, files in os.walk(rootpath):\r\n py_files = sorted([f for f in files if os.path.splitext(f)[1] == '.py'])\r\n \r\n if INITPY in py_files:\r\n subpackage = root[len(rootpath):].lstrip(os.path.sep).\\\r\n replace(os.path.sep, '.')\r\n full = makename(root_package, subpackage)\r\n part = full.rpartition('.')\r\n base_package, submodule = part[0], part[2]\r\n found += 1\r\n if save(base_package, submodule): saved += 1\r\n \r\n py_files.remove(INITPY) \r\n for py_file in py_files:\r\n found += 1\r\n module = os.path.splitext(py_file)[0]\r\n if save(full, module): saved += 1\r\n for item in tree.keys():\r\n tree[item].sort()\r\n print \"%s contains %i submodules, %i skipped\" % \\\r\n (root_package, found, found-saved)\r\n\r\n return tree", "response": "Find all modules in a directory and return a dict of the names of the modules that are found."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a list of children sub - components that are column headings", "response": "def _get_column_headings(self):\r\n \"Return a list of children sub-components that are column headings\"\r\n # return it in the same order as inserted in the Grid\r\n headers = [ctrl for ctrl in self if isinstance(ctrl, GridColumn)]\r\n return sorted(headers, key=lambda ch: ch.index)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _set_row_label(self, value):\r\n \"Set the row label format string (empty to hide)\"\r\n if not value:\r\n self.wx_obj.SetRowLabelSize(0)\r\n else:\r\n self.wx_obj._table._row_label = value", "response": "Set the row label format string ( empty to hide )"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef ResetView(self, grid):\r\n \"Update the grid if rows and columns have been added or deleted\"\r\n grid.BeginBatch()\r\n\r\n for current, new, delmsg, addmsg in [\r\n (self._rows, self.GetNumberRows(), \r\n gridlib.GRIDTABLE_NOTIFY_ROWS_DELETED, \r\n gridlib.GRIDTABLE_NOTIFY_ROWS_APPENDED),\r\n (self._cols, self.GetNumberCols(), \r\n gridlib.GRIDTABLE_NOTIFY_COLS_DELETED,\r\n gridlib.GRIDTABLE_NOTIFY_COLS_APPENDED),\r\n ]:\r\n\r\n if new < current:\r\n msg = gridlib.GridTableMessage(self,delmsg,new,current-new)\r\n grid.ProcessTableMessage(msg)\r\n elif new > current:\r\n msg = gridlib.GridTableMessage(self,addmsg,new-current)\r\n grid.ProcessTableMessage(msg)\r\n self.UpdateValues(grid)\r\n\r\n grid.EndBatch()\r\n\r\n self._rows = self.GetNumberRows()\r\n self._cols = self.GetNumberCols()\r\n # update the column rendering plugins\r\n self._updateColAttrs(grid)\r\n\r\n # update the scrollbars and the displayed part of the grid\r\n grid.AdjustScrollbars()\r\n grid.ForceRefresh()", "response": "Update the grid if rows and columns have been added or deleted"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nupdating all displayed values", "response": "def UpdateValues(self, grid):\r\n \"Update all displayed values\"\r\n # This sends an event to the grid table to update all of the values\r\n msg = gridlib.GridTableMessage(self, \r\n gridlib.GRIDTABLE_REQUEST_VIEW_GET_VALUES)\r\n grid.ProcessTableMessage(msg)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _updateColAttrs(self, grid):\r\n \"update the column attributes to add the appropriate renderer\"\r\n col = 0\r\n\r\n for column in self.columns:\r\n attr = gridlib.GridCellAttr()\r\n if False: # column.readonly\r\n attr.SetReadOnly()\r\n if False: # column.renderer\r\n attr.SetRenderer(renderer)\r\n grid.SetColSize(col, column.width)\r\n grid.SetColAttr(col, attr)\r\n col += 1", "response": "update the column attributes to add the appropriate renderer"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef set_parent(self, new_parent, init=False):\r\n \"Associate the header to the control (it could be recreated)\"\r\n self._created = False\r\n SubComponent.set_parent(self, new_parent, init)\r\n # if index not given, append the column at the last position:\r\n if self.index == -1 or self.index >= len(self._parent.columns):\r\n self.index = len(self._parent.columns) - 1\r\n # insert the column in the listview:\r\n self._parent.wx_obj.AppendCols(1)\r\n #self._parent.wx_obj.SetColLabelValue(self.index, self.text)\r\n #self.SetColLabel(self.index, self.align)\r\n #self._parent.wx_obj.SetColSize(self.index, self.width)\r\n self._created = True", "response": "Associate the header to the control ( it could be recreated )"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef insert(self, pos, values):\r\n \"Insert a number of rows into the grid (and associated table)\"\r\n if isinstance(values, dict):\r\n row = GridRow(self, **values)\r\n else:\r\n row = GridRow(self, *values)\r\n list.insert(self, pos, row)\r\n self._grid_view.wx_obj.InsertRows(pos, numRows=1)", "response": "Insert a number of rows into the grid ( and associated table )"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef append(self, values):\r\n \"Insert a number of rows into the grid (and associated table)\"\r\n if isinstance(values, dict):\r\n row = GridRow(self, **values)\r\n else:\r\n row = GridRow(self, *values)\r\n list.append(self, row)\r\n self._grid_view.wx_obj.AppendRows(numRows=1)", "response": "Insert a number of rows into the grid ( and associated table )"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef clear(self):\r\n \"Remove all rows and reset internal structures\"\r\n ## list has no clear ... remove items in reverse order\r\n for i in range(len(self)-1, -1, -1):\r\n del self[i]\r\n self._key = 0\r\n if hasattr(self._grid_view, \"wx_obj\"):\r\n self._grid_view.wx_obj.ClearGrid()", "response": "Remove all rows and reset internal structures"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncall to create the control which must derive from wxControl. wxComboBox. wxSize", "response": "def Create(self, parent, id, evtHandler):\r\n \"Called to create the control, which must derive from wxControl.\" \r\n self._tc = wx.ComboBox(parent, id, \"\", (100, 50))\r\n self.SetControl(self._tc)\r\n # pushing a different event handler instead evtHandler:\r\n self._tc.PushEventHandler(wx.EvtHandler())\r\n self._tc.Bind(wx.EVT_COMBOBOX, self.OnChange)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef SetSize(self, rect):\r\n \"Called to position/size the edit control within the cell rectangle.\"\r\n self._tc.SetDimensions(rect.x, rect.y, rect.width+2, rect.height+2,\r\n wx.SIZE_ALLOW_MINUS_ONE)", "response": "Called to position / size the edit control within the cell rectangle."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nfetches the value from the table and prepare the edit control", "response": "def BeginEdit(self, row, col, grid):\r\n \"Fetch the value from the table and prepare the edit control\"\r\n self.startValue = grid.GetTable().GetValue(row, col)\r\n choices = grid.GetTable().columns[col]._choices\r\n self._tc.Clear()\r\n self._tc.AppendItems(choices)\r\n self._tc.SetStringSelection(self.startValue)\r\n self._tc.SetFocus()"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef EndEdit(self, row, col, grid, val=None):\r\n \"Complete the editing of the current cell. Returns True if changed\"\r\n changed = False\r\n val = self._tc.GetStringSelection()\r\n print \"val\", val, row, col, self.startValue\r\n if val != self.startValue:\r\n changed = True\r\n grid.GetTable().SetValue(row, col, val) # update the table\r\n self.startValue = ''\r\n self._tc.SetStringSelection('')\r\n return changed", "response": "Complete the editing of the current cell. Returns True if changed"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning True to allow the given key to start editing", "response": "def IsAcceptedKey(self, evt):\r\n \"Return True to allow the given key to start editing\"\r\n ## Oops, there's a bug here, we'll have to do it ourself..\r\n ##return self.base_IsAcceptedKey(evt)\r\n return (not (evt.ControlDown() or evt.AltDown()) and\r\n evt.GetKeyCode() != wx.WXK_SHIFT)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef StartingKey(self, evt):\r\n \"This will be called to let the editor do something with the first key\"\r\n key = evt.GetKeyCode()\r\n ch = None\r\n if key in [wx.WXK_NUMPAD0, wx.WXK_NUMPAD1, wx.WXK_NUMPAD2, wx.WXK_NUMPAD3, wx.WXK_NUMPAD4,\r\n wx.WXK_NUMPAD5, wx.WXK_NUMPAD6, wx.WXK_NUMPAD7, wx.WXK_NUMPAD8, wx.WXK_NUMPAD9]:\r\n ch = ch = chr(ord('0') + key - wx.WXK_NUMPAD0)\r\n elif key < 256 and key >= 0 and chr(key) in string.printable:\r\n ch = chr(key)\r\n if not evt.ShiftDown():\r\n ch = ch.lower()\r\n if ch is not None:\r\n self._tc.SetStringSelection(ch)\r\n else:\r\n evt.Skip()", "response": "This will be called to let the editor do something with the first key"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nenables or disable all menu items", "response": "def Enable(self, value):\r\n \"enable or disable all menu items\"\r\n for i in range(self.GetMenuItemCount()):\r\n it = self.FindItemByPosition(i) \r\n it.Enable(value)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef IsEnabled(self, *args, **kwargs):\r\n \"check if all menu items are enabled\"\r\n for i in range(self.GetMenuItemCount()):\r\n it = self.FindItemByPosition(i) \r\n if not it.IsEnabled():\r\n return False\r\n return True", "response": "check if all menu items are enabled"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef find(self, item_id=None):\r\n \"Recursively find a menu item by its id (useful for event handlers)\"\r\n for it in self:\r\n if it.id == item_id:\r\n return it\r\n elif isinstance(it, Menu):\r\n found = it.find(item_id)\r\n if found:\r\n return found", "response": "Recursively find a menu item by its id ( useful for event handlers )"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef Enable(self, value):\r\n \"enable or disable all top menus\"\r\n for i in range(self.GetMenuCount()):\r\n self.EnableTop(i, value)", "response": "enable or disable all top menus"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef IsEnabled(self, *args, **kwargs):\r\n \"check if all top menus are enabled\"\r\n for i in range(self.GetMenuCount()):\r\n if not self.IsEnabledTop(i):\r\n return False\r\n return True", "response": "check if all top menus are enabled"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef RemoveItem(self, menu):\r\n \"Helper method to remove a menu avoiding using its position\"\r\n menus = self.GetMenus() # get the list of (menu, title)\r\n menus = [submenu for submenu in menus if submenu[0] != menu]\r\n self.SetMenus(menus)", "response": "Helper method to remove a menu avoiding using its position"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef build_data_set(self):\n \"Construct a sequence of name/value pairs from controls\"\n data = {}\n for field in self.fields:\n if field.name:# and field.enabled: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n val = field.get_value()\n if val is None:\n continue\n elif isinstance(val, unicode):\n # web2py string processing\n # requires utf-8 encoded text\n val = val.encode(\"utf-8\") \n data[field.name] = val\n return data", "response": "Construct a sequence of name / value pairs from controls"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef setObjectTag(self, object, tag):\n object._attributes = {}\n object._name = tag.GetName().lower()\n for name in self.attributes:\n object._attributes[\"_%s\" % name] = tag.GetParam(name)\n if object._attributes[\"_%s\" % name] == \"\":\n object._attributes[\"_%s\" % name] = None", "response": "Add a tag attribute to the wx object"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ninserting items described in autosummary:: to the TOC tree but not generate the TOC tree.", "response": "def process_autosummary_toc(app, doctree):\n \"\"\"Insert items described in autosummary:: to the TOC tree, but do\n not generate the toctree:: list.\n \"\"\"\n env = app.builder.env\n crawled = {}\n def crawl_toc(node, depth=1):\n crawled[node] = True\n for j, subnode in enumerate(node):\n try:\n if (isinstance(subnode, autosummary_toc)\n and isinstance(subnode[0], addnodes.toctree)):\n env.note_toctree(env.docname, subnode[0])\n continue\n except IndexError:\n continue\n if not isinstance(subnode, nodes.section):\n continue\n if subnode not in crawled:\n crawl_toc(subnode, depth+1)\n crawl_toc(doctree)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef autosummary_table_visit_html(self, node):\n try:\n tbody = node[0][0][-1]\n for row in tbody:\n col1_entry = row[0]\n par = col1_entry[0]\n for j, subnode in enumerate(list(par)):\n if isinstance(subnode, nodes.Text):\n new_text = unicode(subnode.astext())\n new_text = new_text.replace(u\" \", u\"\\u00a0\")\n par[j] = nodes.Text(new_text)\n except IndexError:\n pass", "response": "Make the first column of the table non - breakinging."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngetting an autodoc. Documenter class suitable for documenting the given obj and parent.", "response": "def get_documenter(obj, parent):\n \"\"\"Get an autodoc.Documenter class suitable for documenting the given\n object.\n\n *obj* is the Python object to be documented, and *parent* is an\n another Python object (e.g. a module or a class) to which *obj*\n belongs to.\n \"\"\"\n from sphinx.ext.autodoc import AutoDirective, DataDocumenter, \\\n ModuleDocumenter\n\n if inspect.ismodule(obj):\n # ModuleDocumenter.can_document_member always returns False\n return ModuleDocumenter\n\n # Construct a fake documenter for *parent*\n if parent is not None:\n parent_doc_cls = get_documenter(parent, None)\n else:\n parent_doc_cls = ModuleDocumenter\n\n if hasattr(parent, '__name__'):\n parent_doc = parent_doc_cls(FakeDirective(), parent.__name__)\n else:\n parent_doc = parent_doc_cls(FakeDirective(), \"\")\n\n # Get the corrent documenter class for *obj*\n classes = [cls for cls in AutoDirective._registry.values()\n if cls.can_document_member(obj, '', False, parent_doc)]\n if classes:\n classes.sort(key=lambda cls: cls.priority)\n return classes[-1]\n else:\n return DataDocumenter"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef mangle_signature(sig, max_chars=30):\n s = re.sub(r\"^\\((.*)\\)$\", r\"\\1\", sig).strip()\n\n # Strip strings (which can contain things that confuse the code below)\n s = re.sub(r\"\\\\\\\\\", \"\", s)\n s = re.sub(r\"\\\\'\", \"\", s)\n s = re.sub(r\"'[^']*'\", \"\", s)\n\n # Parse the signature to arguments + options\n args = []\n opts = []\n\n opt_re = re.compile(r\"^(.*, |)([a-zA-Z0-9_*]+)=\")\n while s:\n m = opt_re.search(s)\n if not m:\n # The rest are arguments\n args = s.split(', ')\n break\n\n opts.insert(0, m.group(2))\n s = m.group(1)[:-2]\n\n # Produce a more compact signature\n sig = limited_join(\", \", args, max_chars=max_chars-2)\n if opts:\n if not sig:\n sig = \"[%s]\" % limited_join(\", \", opts, max_chars=max_chars-4)\n elif len(sig) < max_chars - 4 - 2 - 3:\n sig += \"[, %s]\" % limited_join(\", \", opts,\n max_chars=max_chars-len(sig)-4-2)\n\n return u\"(%s)\" % sig", "response": "Reformat a function signature to a more compact form."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef limited_join(sep, items, max_chars=30, overflow_marker=\"...\"):\n full_str = sep.join(items)\n if len(full_str) < max_chars:\n return full_str\n\n n_chars = 0\n n_items = 0\n for j, item in enumerate(items):\n n_chars += len(item) + len(sep)\n if n_chars < max_chars - len(overflow_marker):\n n_items += 1\n else:\n break\n\n return sep.join(list(items[:n_items]) + [overflow_marker])", "response": "Join a number of strings to one limiting the length to max_chars."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_import_prefixes_from_env(env):\n prefixes = [None]\n\n currmodule = env.temp_data.get('py:module')\n if currmodule:\n prefixes.insert(0, currmodule)\n\n currclass = env.temp_data.get('py:class')\n if currclass:\n if currmodule:\n prefixes.insert(0, currmodule + \".\" + currclass)\n else:\n prefixes.insert(0, currclass)\n\n return prefixes", "response": "Obtain current Python import prefixes from env."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef import_by_name(name, prefixes=[None]):\n tried = []\n for prefix in prefixes:\n try:\n if prefix:\n prefixed_name = '.'.join([prefix, name])\n else:\n prefixed_name = name\n obj, parent = _import_by_name(prefixed_name)\n return prefixed_name, obj, parent\n except ImportError:\n tried.append(prefixed_name)\n raise ImportError('no module named %s' % ' or '.join(tried))", "response": "Import a Python object that has the given name under one of the given prefixes."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _import_by_name(name):\n try:\n name_parts = name.split('.')\n\n # try first interpret `name` as MODNAME.OBJ\n modname = '.'.join(name_parts[:-1])\n if modname:\n try:\n __import__(modname)\n mod = sys.modules[modname]\n return getattr(mod, name_parts[-1]), mod\n except (ImportError, IndexError, AttributeError):\n pass\n\n # ... then as MODNAME, MODNAME.OBJ1, MODNAME.OBJ1.OBJ2, ...\n last_j = 0\n modname = None\n for j in reversed(range(1, len(name_parts)+1)):\n last_j = j\n modname = '.'.join(name_parts[:j])\n try:\n __import__(modname)\n except:# ImportError:\n continue\n if modname in sys.modules:\n break\n\n if last_j < len(name_parts):\n parent = None\n obj = sys.modules[modname]\n for obj_name in name_parts[last_j:]:\n parent = obj\n obj = getattr(obj, obj_name)\n return obj, parent\n else:\n return sys.modules[modname], None\n except (ValueError, ImportError, AttributeError, KeyError), e:\n raise ImportError(*e.args)", "response": "Import a Python object given its full name."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nsmart linking role. Expands to ':obj:`text`' if `text` is an object that can be imported; otherwise expands to '*text*'.", "response": "def autolink_role(typ, rawtext, etext, lineno, inliner,\n options={}, content=[]):\n \"\"\"Smart linking role.\n\n Expands to ':obj:`text`' if `text` is an object that can be imported;\n otherwise expands to '*text*'.\n \"\"\"\n env = inliner.document.settings.env\n r = env.get_domain('py').role('obj')(\n 'obj', rawtext, etext, lineno, inliner, options, content)\n pnode = r[0][0]\n\n prefixes = get_import_prefixes_from_env(env)\n try:\n name, obj, parent = import_by_name(pnode['reftarget'], prefixes)\n except ImportError:\n content = pnode[0]\n r[0][0] = nodes.emphasis(rawtext, content[0].astext(),\n classes=content['classes'])\n return r"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_items(self, names):\n env = self.state.document.settings.env\n\n prefixes = get_import_prefixes_from_env(env)\n\n items = []\n\n max_item_chars = 50\n\n for name in names:\n display_name = name\n if name.startswith('~'):\n name = name[1:]\n display_name = name.split('.')[-1]\n\n try:\n real_name, obj, parent = import_by_name(name, prefixes=prefixes)\n except ImportError:\n self.warn('failed to import %s' % name)\n items.append((name, '', '', name))\n continue\n\n # NB. using real_name here is important, since Documenters\n # handle module prefixes slightly differently\n documenter = get_documenter(obj, parent)(self, real_name)\n if not documenter.parse_name():\n self.warn('failed to parse name %s' % real_name)\n items.append((display_name, '', '', real_name))\n continue\n if not documenter.import_object():\n self.warn('failed to import object %s' % real_name)\n items.append((display_name, '', '', real_name))\n continue\n\n # -- Grab the signature\n\n sig = documenter.format_signature()\n if not sig:\n sig = ''\n else:\n max_chars = max(10, max_item_chars - len(display_name))\n sig = mangle_signature(sig, max_chars=max_chars)\n sig = sig.replace('*', r'\\*')\n\n # -- Grab the summary\n\n doc = list(documenter.process_doc(documenter.get_doc()))\n\n while doc and not doc[0].strip():\n doc.pop(0)\n m = re.search(r\"^([A-Z][^A-Z]*?\\.\\s)\", \" \".join(doc).strip())\n if m:\n summary = m.group(1).strip()\n elif doc:\n summary = doc[0].strip()\n else:\n summary = ''\n\n items.append((display_name, sig, summary, real_name))\n\n return items", "response": "Try to import the given names and return a list of tuples of names signature summary_string real_name."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngenerates a proper list of table nodes for autosummary:: directive.", "response": "def get_table(self, items):\n \"\"\"Generate a proper list of table nodes for autosummary:: directive.\n\n *items* is a list produced by :meth:`get_items`.\n \"\"\"\n table_spec = addnodes.tabular_col_spec()\n table_spec['spec'] = 'll'\n\n table = autosummary_table('')\n real_table = nodes.table('', classes=['longtable'])\n table.append(real_table)\n group = nodes.tgroup('', cols=2)\n real_table.append(group)\n group.append(nodes.colspec('', colwidth=10))\n group.append(nodes.colspec('', colwidth=90))\n body = nodes.tbody('')\n group.append(body)\n\n def append_row(*column_texts):\n row = nodes.row('')\n for text in column_texts:\n node = nodes.paragraph('')\n vl = ViewList()\n vl.append(text, '')\n self.state.nested_parse(vl, 0, node)\n try:\n if isinstance(node[0], nodes.paragraph):\n node = node[0]\n except IndexError:\n pass\n row.append(nodes.entry('', node))\n body.append(row)\n\n for name, sig, summary, real_name in items:\n qualifier = 'obj'\n if 'nosignatures' not in self.options:\n col1 = ':%s:`%s <%s>`\\ %s' % (qualifier, name, real_name, sig)\n else:\n col1 = ':%s:`%s <%s>`' % (qualifier, name, real_name)\n col2 = summary\n append_row(col1, col2)\n\n return [table_spec, table]"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nshow a simple pop - up modal dialog", "response": "def alert(message, title=\"\", parent=None, scrolled=False, icon=\"exclamation\"):\r\n \"Show a simple pop-up modal dialog\"\r\n if not scrolled:\r\n icons = {'exclamation': wx.ICON_EXCLAMATION, 'error': wx.ICON_ERROR,\r\n 'question': wx.ICON_QUESTION, 'info': wx.ICON_INFORMATION}\r\n style = wx.OK | icons[icon]\r\n result = dialogs.messageDialog(parent, message, title, style)\r\n else:\r\n result = dialogs.scrolledMessageDialog(parent, message, title)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef confirm(message=\"\", title=\"\", default=False, ok=False, cancel=False,\r\n parent=None):\r\n \"Ask for confirmation (yes/no or ok and cancel), returns True or False\"\r\n style = wx.CENTRE\r\n if ok:\r\n style |= wx.OK \r\n else:\r\n style |= wx.YES | wx.NO\r\n if default:\r\n style |= wx.YES_DEFAULT\r\n else:\r\n style |= wx.NO_DEFAULT\r\n if cancel:\r\n style |= wx.CANCEL\r\n result = dialogs.messageDialog(parent, message, title, style)\r\n if cancel and result.returned == wx.ID_CANCEL:\r\n return None\r\n return result.accepted", "response": "Ask for confirmation ( yes / no or ok and cancel returns True or False"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef select_font(message=\"\", title=\"\", font=None, parent=None):\r\n \"Show a dialog to select a font\"\r\n if font is not None:\r\n wx_font = font._get_wx_font() # use as default\r\n else:\r\n wx_font = None\r\n font = Font() # create an empty font\r\n result = dialogs.fontDialog(parent, font=wx_font)\r\n if result.accepted:\r\n font_data = result.fontData\r\n result.color = result.fontData.GetColour().Get()\r\n wx_font = result.fontData.GetChosenFont()\r\n font.set_wx_font(wx_font)\r\n wx_font = None\r\n return font", "response": "Show a dialog to select a font"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nshows a dialog to pick a color", "response": "def select_color(message=\"\", title=\"\", color=None, parent=None):\r\n \"Show a dialog to pick a color\"\r\n result = dialogs.colorDialog(parent, color=color)\r\n return result.accepted and result.color"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef open_file(title=\"Open\", directory='', filename='', \r\n wildcard='All Files (*.*)|*.*', multiple=False, parent=None):\r\n \"Show a dialog to select files to open, return path(s) if accepted\"\r\n style = wx.OPEN \r\n if multiple:\r\n style |= wx.MULTIPLE\r\n result = dialogs.fileDialog(parent, title, directory, filename, wildcard, \r\n style)\r\n if result.paths and not multiple:\r\n return result.paths[0]\r\n else:\r\n return result.paths", "response": "Show a dialog to select files to open return path(s ) if accepted"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef save_file(title=\"Save\", directory='', filename='', \r\n wildcard='All Files (*.*)|*.*', overwrite=False, parent=None):\r\n \"Show a dialog to select file to save, return path(s) if accepted\"\r\n style = wx.SAVE \r\n if not overwrite:\r\n style |= wx.OVERWRITE_PROMPT\r\n result = dialogs.fileDialog(parent, title, directory, filename, wildcard, \r\n style)\r\n return result.paths", "response": "Show a dialog to select file to save return path(s ) if accepted"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef choose_directory(message='Choose a directory', path=\"\", parent=None):\r\n \"Show a dialog to choose a directory\"\r\n result = dialogs.directoryDialog(parent, message, path)\r\n return result.path", "response": "Show a dialog to choose a directory"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef find(default='', whole_words=0, case_sensitive=0, parent=None):\r\n \"Shows a find text dialog\"\r\n result = dialogs.findDialog(parent, default, whole_words, case_sensitive)\r\n return {'text': result.searchText, 'whole_words': result.wholeWordsOnly,\r\n 'case_sensitive': result.caseSensitive}", "response": "Shows a find text dialog"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef clear(self):\r\n \"Remove all items and reset internal structures\"\r\n dict.clear(self)\r\n self._key = 0\r\n if hasattr(self._tree_view, \"wx_obj\"):\r\n self._tree_view.wx_obj.DeleteAllItems()", "response": "Remove all items and reset internal structures"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef set_has_children(self, has_children=True):\r\n \"Force appearance of the button next to the item\"\r\n # This is useful to allow the user to expand the items which don't have\r\n # any children now, but instead adding them only when needed, thus \r\n # minimizing memory usage and loading time.\r\n self._tree_model._tree_view.wx_obj.SetItemHasChildren(self.wx_item, \r\n has_children)", "response": "Force appearance of the button next to the item"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the type of an image from the file extension", "response": "def bitmap_type(filename):\r\n \"\"\"\r\n Get the type of an image from the file's extension ( .jpg, etc. )\r\n \"\"\"\r\n\r\n if filename == '':\r\n return None\r\n\r\n name, ext = os.path.splitext(filename)\r\n ext = ext[1:].upper()\r\n if ext == 'BMP':\r\n return wx.BITMAP_TYPE_BMP\r\n elif ext == 'GIF':\r\n return wx.BITMAP_TYPE_GIF\r\n elif ext == 'JPG' or ext == 'JPEG':\r\n return wx.BITMAP_TYPE_JPEG\r\n elif ext == 'PCX':\r\n return wx.BITMAP_TYPE_PCX\r\n elif ext == 'PICT':\r\n return wx.BITMAP_TYPE_PICT\r\n elif ext == 'PNG':\r\n return wx.BITMAP_TYPE_PNG\r\n elif ext == 'PNM':\r\n return wx.BITMAP_TYPE_PNM\r\n elif ext == 'TIF' or ext == 'TIFF':\r\n return wx.BITMAP_TYPE_TIF\r\n elif ext == 'XBM':\r\n return wx.BITMAP_TYPE_XBM\r\n elif ext == 'XPM':\r\n return wx.BITMAP_TYPE_XPM\r\n else:\r\n # KEA 2001-10-10\r\n # rather than throw an exception, we could try and have wxPython figure out the image\r\n # type by returning wxBITMAP_TYPE_ANY\r\n raise RuntimeErro('invalid graphics format')"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _getbitmap_type( self, filename ) :\r\n # KEA 2001-07-27\r\n # was\r\n #name, ext = filename.split( '.' )\r\n #ext = ext.upper()\r\n if filename is None or filename == '':\r\n return None\r\n\r\n name, ext = os.path.splitext(filename)\r\n ext = ext[1:].upper()\r\n if ext == 'BMP':\r\n return wx.BITMAP_TYPE_BMP\r\n elif ext == 'GIF':\r\n return wx.BITMAP_TYPE_GIF\r\n elif ext == 'JPG' or ext == 'JPEG':\r\n return wx.BITMAP_TYPE_JPEG\r\n elif ext == 'PCX':\r\n return wx.BITMAP_TYPE_PCX\r\n #elif ext == 'PICT':\r\n # return wx.BITMAP_TYPE_PICT\r\n elif ext == 'PNG':\r\n return wx.BITMAP_TYPE_PNG\r\n elif ext == 'PNM':\r\n return wx.BITMAP_TYPE_PNM\r\n elif ext == 'TIF' or ext == 'TIFF':\r\n return wx.BITMAP_TYPE_TIF\r\n elif ext == 'XBM':\r\n return wx.BITMAP_TYPE_XBM\r\n elif ext == 'XPM':\r\n return wx.BITMAP_TYPE_XPM\r\n else:\r\n # KEA 2001-10-10\r\n # rather than throw an exception, we could try and have wxPython figure out the image\r\n # type by returning wxBITMAP_TYPE_ANY\r\n raise RuntimeError('invalid graphics format')", "response": "Returns the type of an image from the file extension"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _set_icon(self, icon=None):\r\n if icon is not None:\r\n try:\r\n wx_icon = wx.Icon(icon, wx.BITMAP_TYPE_ICO)\r\n self.wx_obj.SetIcon(wx_icon)\r\n except:\r\n pass", "response": "Set icon based on resource values"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ndisplaying or hide the window optionally disabling all other windows", "response": "def show(self, value=True, modal=None):\r\n \"Display or hide the window, optionally disabling all other windows\"\r\n self.wx_obj.Show(value)\r\n if modal:\r\n # disable all top level windows of this application (MakeModal)\r\n disabler = wx.WindowDisabler(self.wx_obj)\r\n # create an event loop to stop execution \r\n eventloop = wx.EventLoop()\r\n def on_close_modal(evt):\r\n evt.Skip()\r\n eventloop.Exit()\r\n self.wx_obj.Bind(wx.EVT_CLOSE, on_close_modal)\r\n # start the event loop to wait user interaction \r\n eventloop.Run()\r\n # reenable the windows disabled and return control to the caller\r\n del disabler"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef draw_arc(self, x1y1, x2y2, xcyc):\n\n self._buf_image.DrawArcPoint(x1y1, x2y2, xcyc)\n if self.auto_refresh:\n dc = wx.ClientDC(self.wx_obj)\n dc.BlitPointSize((0, 0), (self._size[0], self._size[1]), self._buf_image, (0, 0))", "response": "Draws an arc of a circle centered on x1 y1 and x2 y2."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nopen read and eval the resource from the source file", "response": "def parse(filename=\"\"):\n \"Open, read and eval the resource from the source file\"\n # use the provided resource file:\n s = open(filename).read()\n ##s.decode(\"latin1\").encode(\"utf8\")\n import datetime, decimal\n rsrc = eval(s)\n return rsrc"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nsaving the resource to the source file", "response": "def save(filename, rsrc):\n \"Save the resource to the source file\"\n s = pprint.pformat(rsrc)\n ## s = s.encode(\"utf8\")\n open(filename, \"w\").write(s)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncreating the GUI objects defined in the resource ( filename or python struct )", "response": "def load(controller=None, filename=\"\", name=None, rsrc=None):\n \"Create the GUI objects defined in the resource (filename or python struct)\"\n # if no filename is given, search for the rsrc.py with the same module name:\n if not filename and not rsrc:\n if isinstance(controller, types.ClassType):\n # use the controller class module (to get __file__ for rsrc.py)\n mod_dict = util.get_class_module_dict(controller)\n elif isinstance(controller, types.ModuleType):\n # use the module provided as controller\n mod_dict = controller.__dict__\n elif isinstance(controller, Controller):\n # use the instance provided as controller\n mod_dict = util.get_class_module_dict(controller)\n else:\n # use the caller module (no controller explicitelly provided)\n mod_dict = util.get_caller_module_dict()\n # do not use as controller if it was explicitly False or empty\n if controller is None:\n controller = mod_dict\n if util.main_is_frozen():\n # running standalone\n if '__file__' in mod_dict:\n filename = os.path.split(mod_dict['__file__'])[1]\n else:\n # __main__ has not __file__ under py2exe!\n filename = os.path.split(sys.argv[0])[-1]\n filename = os.path.join(util.get_app_dir(), filename)\n else:\n # figure out the .rsrc.py filename based on the module name\n filename = mod_dict['__file__']\n # chop the .pyc or .pyo from the end\n base, ext = os.path.splitext(filename)\n filename = base + \".rsrc.py\"\n # when rsrc is a file name, open, read and eval it:\n if isinstance(filename, basestring):\n rsrc = parse(filename)\n ret = []\n # search over the resource to create the requested object (or all)\n for win in rsrc:\n if not name or win['name'] == name:\n ret.append(build_window(win))\n # associate event handlers\n if ret and controller:\n connect(ret[0], controller) \n # return the first instance created (if any):\n return ret[0]\n else:\n # return all the instances created -for the designer- (if any):\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef build_window(res):\n \"Create a gui2py window based on the python resource\"\n \n # windows specs (parameters)\n kwargs = dict(res.items())\n wintype = kwargs.pop('type')\n menubar = kwargs.pop('menubar', None)\n components = kwargs.pop('components')\n panel = kwargs.pop('panel', {})\n \n from gui import registry\n import gui\n \n winclass = registry.WINDOWS[wintype]\n win = winclass(**kwargs)\n\n # add an implicit panel by default (as pythoncard had)\n if False and panel is not None:\n panel['name'] = 'panel'\n p = gui.Panel(win, **panel)\n else:\n p = win\n \n if components:\n for comp in components:\n build_component(comp, parent=p)\n\n if menubar:\n mb = gui.MenuBar(name=\"menu\", parent=win)\n for menu in menubar:\n build_component(menu, parent=mb)\n return win", "response": "Create a gui2py window based on the python resource"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef build_component(res, parent=None):\n \"Create a gui2py control based on the python resource\"\n # control specs (parameters)\n kwargs = dict(res.items())\n comtype = kwargs.pop('type')\n if 'components' in res:\n components = kwargs.pop('components')\n elif comtype == 'Menu' and 'items' in res:\n components = kwargs.pop('items')\n else:\n components = []\n\n from gui import registry\n\n if comtype in registry.CONTROLS:\n comclass = registry.CONTROLS[comtype]\n elif comtype in registry.MENU:\n comclass = registry.MENU[comtype]\n elif comtype in registry.MISC:\n comclass = registry.MISC[comtype]\n else:\n raise RuntimeError(\"%s not in registry\" % comtype)\n \n # Instantiate the GUI object\n com = comclass(parent=parent, **kwargs)\n \n for comp in components:\n build_component(comp, parent=com)\n\n return com", "response": "Create a gui2py control based on the python resource"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef dump(obj):\n \"Recursive convert a live GUI object to a resource list/dict\"\n \n from .spec import InitSpec, DimensionSpec, StyleSpec, InternalSpec\n import decimal, datetime\n from .font import Font\n from .graphic import Bitmap, Color\n from . import registry\n\n ret = {'type': obj.__class__.__name__}\n \n for (k, spec) in obj._meta.specs.items():\n if k == \"index\": # index is really defined by creation order\n continue # also, avoid infinite recursion\n v = getattr(obj, k, \"\")\n if (not isinstance(spec, InternalSpec) \n and v != spec.default\n and (k != 'id' or v > 0) \n and isinstance(v, \n (basestring, int, long, float, bool, dict, list, \n decimal.Decimal, \n datetime.datetime, datetime.date, datetime.time,\n Font, Color)) \n and repr(v) != 'None'\n and k != 'parent'\n ):\n ret[k] = v \n \n for ctl in obj:\n if ret['type'] in registry.MENU:\n ret.setdefault('items', []).append(dump(ctl))\n else:\n res = dump(ctl)\n if 'menubar' in res:\n ret.setdefault('menubar', []).append(res.pop('menubar'))\n else:\n ret.setdefault('components', []).append(res)\n \n return ret", "response": "Recursive convert a live GUI object to a resource list / dict"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ntranslate gui2py attribute name from pythoncard legacy code", "response": "def convert(self, name):\n \"translate gui2py attribute name from pythoncard legacy code\"\n new_name = PYTHONCARD_PROPERTY_MAP.get(name)\n if new_name:\n print \"WARNING: property %s should be %s (%s)\" % (name, new_name, self.obj.name)\n return new_name\n else:\n return name"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_data():\n \"Read from the clipboard content, return a suitable object (string or bitmap)\"\n data = None\n try:\n if wx.TheClipboard.Open():\n if wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_TEXT)):\n do = wx.TextDataObject()\n wx.TheClipboard.GetData(do)\n data = do.GetText()\n elif wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_BITMAP)):\n do = wx.BitmapDataObject()\n wx.TheClipboard.GetData(do)\n data = do.GetBitmap()\n wx.TheClipboard.Close()\n except:\n data = None\n return data", "response": "Read from the clipboard content return a suitable object ( string or bitmap )"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nwriting content to the clipboard data can be either a string or a bitmap", "response": "def set_data(data):\n \"Write content to the clipboard, data can be either a string or a bitmap\" \n try:\n if wx.TheClipboard.Open():\n if isinstance(data, (str, unicode)):\n do = wx.TextDataObject()\n do.SetText(data)\n wx.TheClipboard.SetData(do)\n elif isinstance(data, wx.Bitmap):\n do = wx.BitmapDataObject()\n do.SetBitmap(data)\n wx.TheClipboard.SetData(do)\n wx.TheClipboard.Close()\n except:\n pass"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nfind out what items are documented in source. rst. See find_autosummary_in_lines.", "response": "def find_autosummary_in_files(filenames):\n \"\"\"Find out what items are documented in source/*.rst.\n\n See `find_autosummary_in_lines`.\n \"\"\"\n documented = []\n for filename in filenames:\n f = open(filename, 'r')\n lines = f.read().splitlines()\n documented.extend(find_autosummary_in_lines(lines, filename=filename))\n f.close()\n return documented"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef find_autosummary_in_docstring(name, module=None, filename=None):\n try:\n real_name, obj, parent = import_by_name(name)\n lines = pydoc.getdoc(obj).splitlines()\n return find_autosummary_in_lines(lines, module=name, filename=filename)\n except AttributeError:\n pass\n except ImportError, e:\n print \"Failed to import '%s': %s\" % (name, e)\n return []", "response": "Find what items are documented in the given object s docstring."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef find_autosummary_in_lines(lines, module=None, filename=None):\n autosummary_re = re.compile(r'^(\\s*)\\.\\.\\s+autosummary::\\s*')\n automodule_re = re.compile(\n r'^\\s*\\.\\.\\s+automodule::\\s*([A-Za-z0-9_.]+)\\s*$')\n module_re = re.compile(\n r'^\\s*\\.\\.\\s+(current)?module::\\s*([a-zA-Z0-9_.]+)\\s*$')\n autosummary_item_re = re.compile(r'^\\s+(~?[_a-zA-Z][a-zA-Z0-9_.]*)\\s*.*?')\n toctree_arg_re = re.compile(r'^\\s+:toctree:\\s*(.*?)\\s*$')\n template_arg_re = re.compile(r'^\\s+:template:\\s*(.*?)\\s*$')\n\n documented = []\n\n toctree = None\n template = None\n current_module = module\n in_autosummary = False\n base_indent = \"\"\n\n for line in lines:\n if in_autosummary:\n m = toctree_arg_re.match(line)\n if m:\n toctree = m.group(1)\n if filename:\n toctree = os.path.join(os.path.dirname(filename),\n toctree)\n continue\n\n m = template_arg_re.match(line)\n if m:\n template = m.group(1).strip()\n continue\n\n if line.strip().startswith(':'):\n continue # skip options\n\n m = autosummary_item_re.match(line)\n if m:\n name = m.group(1).strip()\n if name.startswith('~'):\n name = name[1:]\n if current_module and \\\n not name.startswith(current_module + '.'):\n name = \"%s.%s\" % (current_module, name)\n documented.append((name, toctree, template))\n continue\n\n if not line.strip() or line.startswith(base_indent + \" \"):\n continue\n\n in_autosummary = False\n\n m = autosummary_re.match(line)\n if m:\n in_autosummary = True\n base_indent = m.group(1)\n toctree = None\n template = None\n continue\n\n m = automodule_re.search(line)\n if m:\n current_module = m.group(1).strip()\n # recurse into the automodule docstring\n documented.extend(find_autosummary_in_docstring(\n current_module, filename=filename))\n continue\n\n m = module_re.match(line)\n if m:\n current_module = m.group(2)\n continue\n\n return documented", "response": "Find out what items appear in the given lines."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nadding the object and all their childs", "response": "def load_object(self, obj=None):\n \"Add the object and all their childs\"\n # if not obj is given, do a full reload using the current root\n if obj:\n self.root_obj = obj\n else:\n obj = self.root_obj\n self.tree.DeleteAllItems()\n self.root = self.tree.AddRoot(\"application\")\n self.tree.SetItemText(self.root, \"App\", 1)\n self.tree.SetItemText(self.root, \"col 2 root\", 2)\n #self.tree.SetItemImage(self.root, fldridx, which = wx.TreeItemIcon_Normal)\n #self.tree.SetItemImage(self.root, fldropenidx, which = wx.TreeItemIcon_Expanded)\n\n self.build_tree(self.root, obj)\n self.tree.Expand(self.root)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nselecting the object and show its properties", "response": "def inspect(self, obj, context_menu=False, edit_prop=False, mouse_pos=None):\n \"Select the object and show its properties\"\n child = self.tree.FindItem(self.root, obj.name)\n if DEBUG: print \"inspect child\", child\n if child:\n self.tree.ScrollTo(child)\n self.tree.SetCurrentItem(child)\n self.tree.SelectItem(child)\n child.Selected = True\n self.activate_item(child, edit_prop)\n if context_menu:\n self.show_context_menu(child, mouse_pos)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nloading the selected item in the property editor", "response": "def activate_item(self, child, edit_prop=False, select=False):\n \"load the selected item in the property editor\"\n d = self.tree.GetItemData(child)\n if d:\n o = d.GetData()\n self.selected_obj = o\n callback = lambda o=o, **kwargs: self.update(o, **kwargs)\n self.propeditor.load_object(o, callback)\n if edit_prop:\n wx.CallAfter(self.propeditor.edit)\n if select and self.designer:\n self.designer.select(o)\n else:\n self.selected_obj = None"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nupdate the tree item when the object name changes", "response": "def update(self, obj, **kwargs):\n \"Update the tree item when the object name changes\"\n # search for the old name:\n child = self.tree.FindItem(self.root, kwargs['name'])\n if DEBUG: print \"update child\", child, kwargs\n if child:\n self.tree.ScrollTo(child)\n self.tree.SetCurrentItem(child)\n self.tree.SelectItem(child)\n child.Selected = True\n # update the new name\n self.tree.SetItemText(child, obj.name, 0)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef show_context_menu(self, item, mouse_pos=None):\n \"Open a popup menu with options regarding the selected object\"\n if item:\n d = self.tree.GetItemData(item)\n if d:\n obj = d.GetData()\n if obj:\n # highligh and store the selected object:\n self.highlight(obj.wx_obj)\n self.obj = obj\n \n # make the context menu\n menu = wx.Menu()\n id_del, id_dup, id_raise, id_lower = [wx.NewId() for i\n in range(4)]\n menu.Append(id_del, \"Delete\")\n menu.Append(id_dup, \"Duplicate\")\n menu.Append(id_raise, \"Bring to Front\")\n menu.Append(id_lower, \"Send to Back\")\n\n # make submenu!\n sm = wx.Menu()\n for ctrl in sorted(obj._meta.valid_children,\n key=lambda c: \n registry.ALL.index(c._meta.name)):\n new_id = wx.NewId()\n sm.Append(new_id, ctrl._meta.name)\n self.Bind(wx.EVT_MENU, \n lambda evt, ctrl=ctrl: self.add_child(ctrl, mouse_pos), \n id=new_id)\n \n menu.AppendMenu(wx.NewId(), \"Add child\", sm)\n\n self.Bind(wx.EVT_MENU, self.delete, id=id_del)\n self.Bind(wx.EVT_MENU, self.duplicate, id=id_dup)\n self.Bind(wx.EVT_MENU, self.bring_to_front, id=id_raise)\n self.Bind(wx.EVT_MENU, self.send_to_back, id=id_lower)\n\n self.PopupMenu(menu)\n menu.Destroy()\n self.load_object(self.root_obj)", "response": "Open a popup menu with options regarding the selected object"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef set_parent(self, new_parent, init=False):\n \"Re-parent a child control with the new wx_obj parent (owner)\"\n ##SubComponent.set_parent(self, new_parent, init)\n self.wx_obj.SetOwner(new_parent.wx_obj.GetEventHandler())", "response": "Re - parent a child control with the new wx_obj parent ( owner"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nperforming the actual serialization.", "response": "def to_representation(self, value):\n \"\"\"\n Perform the actual serialization.\n\n Args:\n value: the image to transform\n Returns:\n a url pointing at a scaled and cached image\n \"\"\"\n if not value:\n return None\n\n image = get_thumbnail(value, self.geometry_string, **self.options)\n\n try:\n request = self.context.get('request', None)\n return request.build_absolute_uri(image.url)\n except:\n try:\n return super(HyperlinkedSorlImageField, self).to_representation(image)\n except AttributeError: # NOQA\n return super(HyperlinkedSorlImageField, self).to_native(image.url) # NOQA"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nbuilding and registers a :class:`Selector` object with the given name and configuration. Args: name (str): The name of the selector. Yields: SelectorFactory: The factory that will build the :class:`Selector`.", "response": "def add_selector(name):\n \"\"\"\n Builds and registers a :class:`Selector` object with the given name and configuration.\n\n Args:\n name (str): The name of the selector.\n\n Yields:\n SelectorFactory: The factory that will build the :class:`Selector`.\n \"\"\"\n\n factory = SelectorFactory(name)\n yield factory\n selectors[name] = factory.build_selector()"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the expression filters for this selector.", "response": "def expression_filters(self):\n \"\"\" Dict[str, ExpressionFilter]: Returns the expression filters for this selector. \"\"\"\n\n return {\n name: filter for name, filter in iter(self.filters.items())\n if isinstance(filter, ExpressionFilter)}"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef node_filters(self):\n\n return {\n name: filter for name, filter in iter(self.filters.items())\n if isinstance(filter, NodeFilter)}", "response": "Returns the node filters for this selector."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef expression_filter(self, name, **kwargs):\n\n def decorator(func):\n self.filters[name] = ExpressionFilter(name, func, **kwargs)\n\n return decorator", "response": "Returns a decorator function for adding an expression filter."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns a function that adds a node filter.", "response": "def node_filter(self, name, **kwargs):\n \"\"\"\n Returns a decorator function for adding a node filter.\n\n Args:\n name (str): The name of the filter.\n **kwargs: Variable keyword arguments for the filter.\n\n Returns:\n Callable[[Callable[[Element, Any], bool]]]: A decorator function for adding a node\n filter.\n \"\"\"\n\n def decorator(func):\n self.filters[name] = NodeFilter(name, func, **kwargs)\n\n return decorator"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nadds filters from a particular global filter set.", "response": "def filter_set(self, name):\n \"\"\"\n Adds filters from a particular global :class:`FilterSet`.\n\n Args:\n name (str): The name of the set whose filters should be added.\n \"\"\"\n\n filter_set = filter_sets[name]\n for name, filter in iter(filter_set.filters.items()):\n self.filters[name] = filter\n self.descriptions += filter_set.descriptions"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nbuild a new Selector instance with the current configuration.", "response": "def build_selector(self):\n \"\"\" Selector: Returns a new :class:`Selector` instance with the current configuration. \"\"\"\n\n kwargs = {\n 'label': self.label,\n 'descriptions': self.descriptions,\n 'filters': self.filters}\n if self.format == \"xpath\":\n kwargs['xpath'] = self.func\n if self.format == \"css\":\n kwargs['css'] = self.func\n\n return Selector(self.name, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nresolves this query relative to the given node.", "response": "def resolves_for(self, node):\n \"\"\"\n Resolves this query relative to the given node.\n\n Args:\n node (node.Base): The node to be evaluated.\n\n Returns:\n int: The number of matches found.\n \"\"\"\n\n self.node = node\n self.actual_styles = node.style(*self.expected_styles.keys())\n\n return all(\n toregex(value).search(self.actual_styles[style])\n for style, value in iter(self.expected_styles.items()))"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nasserts that the page has the given path.", "response": "def assert_current_path(self, path, **kwargs):\n \"\"\"\n Asserts that the page has the given path. By default this will compare against the\n path+query portion of the full URL.\n\n Args:\n path (str | RegexObject): The string or regex that the current \"path\" should match.\n **kwargs: Arbitrary keyword arguments for :class:`CurrentPathQuery`.\n\n Returns:\n True\n\n Raises:\n ExpectationNotMet: If the assertion hasn't succeeded during the wait time.\n \"\"\"\n\n query = CurrentPathQuery(path, **kwargs)\n\n @self.document.synchronize\n def assert_current_path():\n if not query.resolves_for(self):\n raise ExpectationNotMet(query.failure_message)\n assert_current_path()\n\n return True"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nassert that the page doesn t have the given path.", "response": "def assert_no_current_path(self, path, **kwargs):\n \"\"\"\n Asserts that the page doesn't have the given path.\n\n Args:\n path (str | RegexObject): The string or regex that the current \"path\" should match.\n **kwargs: Arbitrary keyword arguments for :class:`CurrentPathQuery`.\n\n Returns:\n True\n\n Raises:\n ExpectationNotMet: If the assertion hasn't succeeded during the wait time.\n \"\"\"\n\n query = CurrentPathQuery(path, **kwargs)\n\n @self.document.synchronize\n def assert_no_current_path():\n if query.resolves_for(self):\n raise ExpectationNotMet(query.negative_failure_message)\n\n assert_no_current_path()\n\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef has_current_path(self, path, **kwargs):\n\n try:\n return self.assert_current_path(path, **kwargs)\n except ExpectationNotMet:\n return False", "response": "Checks if the page has the given path."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef has_no_current_path(self, path, **kwargs):\n\n try:\n return self.assert_no_current_path(path, **kwargs)\n except ExpectationNotMet:\n return False", "response": "Checks if the page doesn t have the given path."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nretrieves the text of the element.", "response": "def text(self):\n \"\"\"\n Retrieve the text of the element. If :data:`capybara.ignore_hidden_elements` is ``True``,\n which it is by default, then this will return only text which is visible. The exact\n semantics of this may differ between drivers, but generally any text within elements with\n ``display: none`` is ignored.\n\n Returns:\n str: The text of the element.\n \"\"\"\n\n if capybara.ignore_hidden_elements or capybara.visible_text_only:\n return self.visible_text\n else:\n return self.all_text"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef select_option(self):\n if self.disabled:\n warn(\"Attempt to select disabled option: {}\".format(self.value or self.text))\n self.base.select_option()", "response": "Select this node if it is an option element inside a select tag."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns the given expression filtered by the given value.", "response": "def apply_filter(self, expr, value):\n \"\"\"\n Returns the given expression filtered by the given value.\n\n Args:\n expr (xpath.expression.AbstractExpression): The expression to filter.\n value (object): The desired value with which the expression should be filtered.\n\n Returns:\n xpath.expression.AbstractExpression: The filtered expression.\n \"\"\"\n\n if self.skip(value):\n return expr\n\n if not self._valid_value(value):\n msg = \"Invalid value {value} passed to filter {name} - \".format(\n value=repr(value),\n name=self.name)\n\n if self.default is not None:\n warn(msg + \"defaulting to {}\".format(self.default))\n value = self.default\n else:\n warn(msg + \"skipping\")\n return expr\n\n return self.func(expr, value)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_browser(browser_name, capabilities=None, **options):\n\n if browser_name == \"chrome\":\n return webdriver.Chrome(desired_capabilities=capabilities, **options)\n if browser_name == \"edge\":\n return webdriver.Edge(capabilities=capabilities, **options)\n if browser_name in [\"ff\", \"firefox\"]:\n return webdriver.Firefox(capabilities=capabilities, **options)\n if browser_name in [\"ie\", \"internet_explorer\"]:\n return webdriver.Ie(capabilities=capabilities, **options)\n if browser_name == \"phantomjs\":\n return webdriver.PhantomJS(desired_capabilities=capabilities, **options)\n if browser_name == \"remote\":\n return webdriver.Remote(desired_capabilities=capabilities, **options)\n if browser_name == \"safari\":\n return webdriver.Safari(desired_capabilities=capabilities, **options)\n\n raise ValueError(\"unsupported browser: {}\".format(repr(browser_name)))", "response": "Returns an instance of the given browser with the given capabilities."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the keyword arguments that this query was initialized.", "response": "def kwargs(self):\n \"\"\" Dict[str, Any]: The keyword arguments with which this query was initialized. \"\"\"\n kwargs = {}\n kwargs.update(self.options)\n kwargs.update(self.filter_options)\n return kwargs"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef description(self):\n\n description = self.label\n\n if self.locator:\n description += \" {}\".format(desc(self.locator))\n if self.options[\"text\"] is not None:\n description += \" with text {}\".format(desc(self.options[\"text\"]))\n\n description += self.selector.description(self.filter_options)\n\n return description", "response": "A long description of this query."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning the XPath query for this selector.", "response": "def xpath(self, exact=None):\n \"\"\"\n Returns the XPath query for this selector.\n\n Args:\n exact (bool, optional): Whether to exactly match text.\n\n Returns:\n str: The XPath query for this selector.\n \"\"\"\n\n exact = exact if exact is not None else self.exact\n\n if isinstance(self.expression, AbstractExpression):\n expression = self._apply_expression_filters(self.expression)\n\n return to_xpath(expression, exact=exact)\n else:\n return str_(self.expression)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nresolves this query relative to the given node.", "response": "def resolve_for(self, node, exact=None):\n \"\"\"\n Resolves this query relative to the given node.\n\n Args:\n node (node.Base): The node relative to which this query should be resolved.\n exact (bool, optional): Whether to exactly match text.\n\n Returns:\n list[Element]: A list of elements matched by this query.\n \"\"\"\n\n from capybara.driver.node import Node\n from capybara.node.element import Element\n from capybara.node.simple import Simple\n\n @node.synchronize\n def resolve():\n if self.selector.format == \"css\":\n children = node._find_css(self.css())\n else:\n children = node._find_xpath(self.xpath(exact))\n\n def wrap(child):\n if isinstance(child, Node):\n return Element(node.session, child, node, self)\n else:\n return Simple(child)\n\n children = [wrap(child) for child in children]\n\n return Result(children, self)\n\n return resolve()"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns whether the given node matches all filters.", "response": "def matches_filters(self, node):\n \"\"\"\n Returns whether the given node matches all filters.\n\n Args:\n node (Element): The node to evaluate.\n\n Returns:\n bool: Whether the given node matches.\n \"\"\"\n\n visible = self.visible\n\n if self.options[\"text\"]:\n if isregex(self.options[\"text\"]):\n regex = self.options[\"text\"]\n elif self.exact_text is True:\n regex = re.compile(r\"\\A{}\\Z\".format(re.escape(self.options[\"text\"])))\n else:\n regex = toregex(self.options[\"text\"])\n\n text = normalize_text(\n node.all_text if visible == \"all\" else node.visible_text)\n\n if not regex.search(text):\n return False\n\n if isinstance(self.exact_text, (bytes_, str_)):\n regex = re.compile(r\"\\A{}\\Z\".format(re.escape(self.exact_text)))\n\n text = normalize_text(\n node.all_text if visible == \"all\" else node.visible_text)\n\n if not regex.search(text):\n return False\n\n if visible == \"visible\":\n if not node.visible:\n return False\n elif visible == \"hidden\":\n if node.visible:\n return False\n\n for name, node_filter in iter(self._node_filters.items()):\n if name in self.filter_options:\n if not node_filter.matches(node, self.filter_options[name]):\n return False\n elif node_filter.has_default:\n if not node_filter.matches(node, node_filter.default):\n return False\n\n if self.options[\"filter\"] and not self.options[\"filter\"](node):\n return False\n\n return True"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef current_scope(self):\n scope = self._scopes[-1]\n if scope in [None, \"frame\"]:\n scope = self.document\n return scope", "response": "Returns the current scope of the current node."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef current_host(self):\n\n if not self.current_url:\n return\n\n result = urlparse(self.current_url)\n scheme, netloc = result.scheme, result.netloc\n host = netloc.split(\":\")[0] if netloc else None\n return \"{0}://{1}\".format(scheme, host) if host else None", "response": "str - Host of the current page."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef visit(self, visit_uri):\n\n self.raise_server_error()\n\n visit_uri = urlparse(visit_uri)\n\n if capybara.app_host:\n uri_base = urlparse(capybara.app_host)\n elif self.server:\n uri_base = urlparse(\"http://{}:{}\".format(self.server.host, self.server.port))\n else:\n uri_base = None\n\n visit_uri = ParseResult(\n scheme=visit_uri.scheme or (uri_base.scheme if uri_base else \"\"),\n netloc=visit_uri.netloc or (uri_base.netloc if uri_base else \"\"),\n path=visit_uri.path,\n params=visit_uri.params,\n query=visit_uri.query,\n fragment=visit_uri.fragment)\n\n self.driver.visit(visit_uri.geturl())", "response": "Navigate to the given URL."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef scope(self, *args, **kwargs):\n\n new_scope = args[0] if isinstance(args[0], Base) else self.find(*args, **kwargs)\n self._scopes.append(new_scope)\n try:\n yield\n finally:\n self._scopes.pop()", "response": "A context manager that finds and returns the elements that are in the given selector."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nexecutes the wrapped code within the given iframe.", "response": "def frame(self, locator=None, *args, **kwargs):\n \"\"\"\n Execute the wrapped code within the given iframe using the given frame or frame name/id.\n May not be supported by all drivers.\n\n Args:\n locator (str | Element, optional): The name/id of the frame or the frame's element.\n Defaults to the only frame in the document.\n \"\"\"\n\n self.switch_to_frame(self._find_frame(locator, *args, **kwargs))\n try:\n yield\n finally:\n self.switch_to_frame(\"parent\")"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef switch_to_frame(self, frame):\n\n if isinstance(frame, Element):\n self.driver.switch_to_frame(frame)\n self._scopes.append(\"frame\")\n elif frame == \"parent\":\n if self._scopes[-1] != \"frame\":\n raise ScopeError(\"`switch_to_frame(\\\"parent\\\")` cannot be called \"\n \"from inside a descendant frame's `scope` context.\")\n self._scopes.pop()\n self.driver.switch_to_frame(\"parent\")\n elif frame == \"top\":\n if \"frame\" in self._scopes:\n idx = self._scopes.index(\"frame\")\n if any([scope not in [\"frame\", None] for scope in self._scopes[idx:]]):\n raise ScopeError(\"`switch_to_frame(\\\"top\\\")` cannot be called \"\n \"from inside a descendant frame's `scope` context.\")\n self._scopes = self._scopes[:idx]\n self.driver.switch_to_frame(\"top\")\n else:\n raise ValueError(\n \"You must provide a frame element, \\\"parent\\\", or \\\"top\\\" \"\n \"when calling switch_to_frame\")", "response": "Switches the current locale to the given frame."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef switch_to_window(self, window, wait=None):\n\n if len(self._scopes) > 1:\n raise ScopeError(\n \"`switch_to_window` is not supposed to be invoked from \"\n \"within `scope`s, `frame`s, or other `window`s.\")\n\n if isinstance(window, Window):\n self.driver.switch_to_window(window.handle)\n return window\n else:\n @self.document.synchronize(errors=(WindowError,), wait=wait)\n def switch_and_get_matching_window():\n original_window_handle = self.driver.current_window_handle\n try:\n for handle in self.driver.window_handles:\n self.driver.switch_to_window(handle)\n result = window()\n if result:\n return Window(self, handle)\n except Exception:\n self.driver.switch_to_window(original_window_handle)\n raise\n\n self.driver.switch_to_window(original_window_handle)\n raise WindowError(\"Could not find a window matching lambda\")\n\n return switch_and_get_matching_window()", "response": "Switches to a window."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef window(self, window):\n\n original = self.current_window\n if window != original:\n self.switch_to_window(window)\n self._scopes.append(None)\n try:\n yield\n finally:\n self._scopes.pop()\n if original != window:\n self.switch_to_window(original)", "response": "Context manager for the given window."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef window_opened_by(self, trigger_func, wait=None):\n\n old_handles = set(self.driver.window_handles)\n trigger_func()\n\n @self.document.synchronize(wait=wait, errors=(WindowError,))\n def get_new_window():\n opened_handles = set(self.driver.window_handles) - old_handles\n if len(opened_handles) != 1:\n raise WindowError(\"lambda passed to `window_opened_by` \"\n \"opened {0} windows instead of 1\".format(len(opened_handles)))\n return Window(self, list(opened_handles)[0])\n\n return get_new_window()", "response": "Get the window that has been opened by the passed lambda."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef execute_script(self, script, *args):\n\n args = [arg.base if isinstance(arg, Base) else arg for arg in args]\n self.driver.execute_script(script, *args)", "response": "Execute a JavaScript script and return a result."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nevaluating the given JavaScript and returns the result.", "response": "def evaluate_script(self, script, *args):\n \"\"\"\n Evaluate the given JavaScript and return the result. Be careful when using this with\n scripts that return complex objects, such as jQuery statements. :meth:`execute_script`\n might be a better alternative.\n\n Args:\n script (str): A string of JavaScript to evaluate.\n *args: Variable length argument list to pass to the executed JavaScript string.\n\n Returns:\n object: The result of the evaluated JavaScript (may be driver specific).\n \"\"\"\n\n args = [arg.base if isinstance(arg, Base) else arg for arg in args]\n result = self.driver.evaluate_script(script, *args)\n return self._wrap_element_script_result(result)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nexecutes the wrapped code accepting an alert.", "response": "def accept_alert(self, text=None, wait=None):\n \"\"\"\n Execute the wrapped code, accepting an alert.\n\n Args:\n text (str | RegexObject, optional): Text to match against the text in the modal.\n wait (int | float, optional): Maximum time to wait for the modal to appear after\n executing the wrapped code.\n\n Raises:\n ModalNotFound: If a modal dialog hasn't been found.\n \"\"\"\n\n wait = wait or capybara.default_max_wait_time\n with self.driver.accept_modal(\"alert\", text=text, wait=wait):\n yield"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef accept_confirm(self, text=None, wait=None):\n\n with self.driver.accept_modal(\"confirm\", text=text, wait=wait):\n yield", "response": "Execute the wrapped code accepting a confirm."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nexecutes the wrapped code dismissing a modal.", "response": "def dismiss_confirm(self, text=None, wait=None):\n \"\"\"\n Execute the wrapped code, dismissing a confirm.\n\n Args:\n text (str | RegexObject, optional): Text to match against the text in the modal.\n wait (int | float, optional): Maximum time to wait for the modal to appear after\n executing the wrapped code.\n\n Raises:\n ModalNotFound: If a modal dialog hasn't been found.\n \"\"\"\n\n with self.driver.dismiss_modal(\"confirm\", text=text, wait=wait):\n yield"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nexecutes the wrapped code accepting a prompt optionally responding to the prompt.", "response": "def accept_prompt(self, text=None, response=None, wait=None):\n \"\"\"\n Execute the wrapped code, accepting a prompt, optionally responding to the prompt.\n\n Args:\n text (str | RegexObject, optional): Text to match against the text in the modal.\n response (str, optional): Response to provide to the prompt.\n wait (int | float, optional): Maximum time to wait for the modal to appear after\n executing the wrapped code.\n\n Raises:\n ModalNotFound: If a modal dialog hasn't been found.\n \"\"\"\n\n with self.driver.accept_modal(\"prompt\", text=text, response=response, wait=wait):\n yield"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef dismiss_prompt(self, text=None, wait=None):\n\n with self.driver.dismiss_modal(\"prompt\", text=text, wait=wait):\n yield", "response": "Execute the wrapped code dismissing a prompt."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef save_page(self, path=None):\n\n path = _prepare_path(path, \"html\")\n\n with open(path, \"wb\") as f:\n f.write(encode_string(self.body))\n\n return path", "response": "Save a snapshot of the page."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nsave a screenshot of the page.", "response": "def save_screenshot(self, path=None, **kwargs):\n \"\"\"\n Save a screenshot of the page.\n\n If invoked without arguments, it will save a file to :data:`capybara.save_path` and the\n file will be given a randomly generated filename. If invoked with a relative path, the path\n will be relative to :data:`capybara.save_path`.\n\n Args:\n path (str, optional): The path to where it should be saved.\n **kwargs: Arbitrary keywords arguments for the driver.\n\n Returns:\n str: The path to which the file was saved.\n \"\"\"\n\n path = _prepare_path(path, \"png\")\n self.driver.save_screenshot(path, **kwargs)\n return path"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nresetting the session and return the current session s cookie.", "response": "def reset(self):\n \"\"\"\n Reset the session (i.e., remove cookies and navigate to a blank page).\n\n This method does not:\n * accept modal dialogs if they are present (the Selenium driver does, but others may not),\n * clear the browser cache/HTML 5 local storage/IndexedDB/Web SQL database/etc., or\n * modify the state of the driver/underlying browser in any other way\n\n as doing so would result in performance downsides and it's not needed to do everything\n from the list above for most apps.\n\n If you want to do anything from the list above on a general basis you can write a test\n teardown method.\n \"\"\"\n\n self.driver.reset()\n if self.server:\n self.server.wait_for_pending_requests()\n self.raise_server_error()"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef raise_server_error(self):\n if self.server and self.server.error:\n try:\n if capybara.raise_server_errors:\n raise self.server.error\n finally:\n self.server.reset_error()", "response": "Raise errors encountered by the server."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn whether the given node matches the filter rule with the given value.", "response": "def matches(self, node, value):\n \"\"\"\n Returns whether the given node matches the filter rule with the given value.\n\n Args:\n node (Element): The node to filter.\n value (object): The desired value with which the node should be evaluated.\n\n Returns:\n bool: Whether the given node matches.\n \"\"\"\n\n if self.skip(value):\n return True\n\n if not self._valid_value(value):\n msg = \"Invalid value {value} passed to filter {name} - \".format(\n value=repr(value),\n name=self.name)\n\n if self.default is not None:\n warn(msg + \"defaulting to {}\".format(self.default))\n value = self.default\n else:\n warn(msg + \"skipping\")\n return True\n\n return self.func(node, value)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_version():\n\n global_vars = {}\n\n # Compile and execute the individual file to prevent\n # the package from being automatically loaded.\n source = read(os.path.join(\"capybara\", \"version.py\"))\n code = compile(source, \"version.py\", \"exec\")\n exec(code, global_vars)\n\n return global_vars['__version__']", "response": "Returns the package version."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nresolves this query relative to the given node.", "response": "def resolves_for(self, node):\n \"\"\"\n Resolves this query relative to the given node.\n\n Args:\n node (node.Document): The node to be evaluated.\n\n Returns:\n bool: Whether the given node matches this query.\n \"\"\"\n\n self.actual_title = normalize_text(node.title)\n return bool(self.search_regexp.search(self.actual_title))"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef frame_title(self):\n elements = self._find_xpath(\"/html/head/title\")\n titles = [element.all_text for element in elements]\n return titles[0] if len(titles) else \"\"", "response": "str - The title for the current frame."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef value(self):\n\n if self.tag_name == \"textarea\":\n return inner_content(self.native)\n elif self.tag_name == \"select\":\n if self[\"multiple\"] == \"multiple\":\n selected_options = self._find_xpath(\".//option[@selected='selected']\")\n return [_get_option_value(option) for option in selected_options]\n else:\n options = (\n self._find_xpath(\".//option[@selected='selected']\") +\n self._find_xpath(\".//option\"))\n return _get_option_value(options[0]) if options else None\n elif self.tag_name == \"input\" and self[\"type\"] in [\"checkbox\", \"radio\"]:\n return self[\"value\"] or \"on\"\n else:\n return self[\"value\"]", "response": "str - The value of the form element."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef add_filter_set(name):\n\n factory = FilterSetFactory(name)\n yield factory\n filter_sets[name] = factory.build_filter_set()", "response": "A context manager that builds and registers a global filter set."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the current session if needed.", "response": "def current_session():\n \"\"\"\n Returns the :class:`Session` for the current driver and app, instantiating one if needed.\n\n Returns:\n Session: The :class:`Session` for the current driver and app.\n \"\"\"\n\n driver = current_driver or default_driver\n\n session_key = \"{driver}:{session}:{app}\".format(\n driver=driver, session=session_name, app=str(id(app)))\n session = _session_pool.get(session_key, None)\n\n if session is None:\n from capybara.session import Session\n session = Session(driver, app)\n _session_pool[session_key] = session\n\n return session"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef has_all_of_selectors(self, selector, *locators, **kwargs):\n\n return self.assert_all_of_selectors(selector, *locators, **kwargs)", "response": "Tests if all of the provided selectors are present on the current page or descendants of the current node."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ntesting if none of the provided selectors are present on the current page or descendants of the current page.", "response": "def has_none_of_selectors(self, selector, *locators, **kwargs):\n \"\"\"\n Checks if none of the provided selectors are present on the given page or descendants of the\n current node. If options are provided, the assertion will check that each locator is present\n with those options as well (other than ``wait``). ::\n\n page.has_none_of_selectors(\"custom\", \"Tom\", \"Joe\", visible=\"all\")\n page.has_none_of_selectors(\"css\", \"#my_div\", \"a.not_clicked\")\n\n It accepts all options that :meth:`find_all` accepts, such as ``text`` and ``visible``.\n\n The ``wait`` option applies to all of the selectors as a group, so none of the locators must\n be present with ``wait`` (defaults to :data:`capybara.default_max_wait_time`) seconds.\n\n If the given selector is not a valid selector, the first argument is assumed to be a locator\n and the default selector will be used.\n\n Args:\n selector (str, optional): The name of the selector to use. Defaults to\n :data:`capybara.default_selector`.\n *locators (str): Variable length list of locators.\n **kwargs: Arbitrary keyword arguments for :class:`SelectorQuery`.\n \"\"\"\n\n return self.assert_none_of_selectors(selector, *locators, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef assert_selector(self, *args, **kwargs):\n\n query = SelectorQuery(*args, **kwargs)\n\n @self.synchronize(wait=query.wait)\n def assert_selector():\n result = query.resolve_for(self)\n\n if not (result.matches_count and\n (len(result) > 0 or expects_none(query.options))):\n raise ExpectationNotMet(result.failure_message)\n\n return True\n\n return assert_selector()", "response": "Assert that a given selector is on the page or a descendant of the current node."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef assert_style(self, styles, **kwargs):\n\n query = StyleQuery(styles, **kwargs)\n\n @self.synchronize(wait=query.wait)\n def assert_style():\n if not query.resolves_for(self):\n raise ExpectationNotMet(query.failure_message)\n\n return True\n\n return assert_style()", "response": "Assert that an element has the specified CSS styles."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nassert that all of the provided selectors are present on the given page or descendants of the current node. If options are provided, the assertion will check that each locator is present with those options as well (other than ``wait``). :: page.assert_all_of_selectors(\"custom\", \"Tom\", \"Joe\", visible=\"all\") page.assert_all_of_selectors(\"css\", \"#my_dif\", \"a.not_clicked\") It accepts all options that :meth:`find_all` accepts, such as ``text`` and ``visible``. The ``wait`` option applies to all of the selectors as a group, so all of the locators must be present within ``wait`` (defaults to :data:`capybara.default_max_wait_time`) seconds. If the given selector is not a valid selector, the first argument is assumed to be a locator and the default selector will be used. Args: selector (str, optional): The name of the selector to use. Defaults to :data:`capybara.default_selector`. *locators (str): Variable length list of locators. **kwargs: Arbitrary keyword arguments for :class:`SelectorQuery`.", "response": "def assert_all_of_selectors(self, selector, *locators, **kwargs):\n \"\"\"\n Asserts that all of the provided selectors are present on the given page or descendants of\n the current node. If options are provided, the assertion will check that each locator is\n present with those options as well (other than ``wait``). ::\n\n page.assert_all_of_selectors(\"custom\", \"Tom\", \"Joe\", visible=\"all\")\n page.assert_all_of_selectors(\"css\", \"#my_dif\", \"a.not_clicked\")\n\n It accepts all options that :meth:`find_all` accepts, such as ``text`` and ``visible``.\n\n The ``wait`` option applies to all of the selectors as a group, so all of the locators must\n be present within ``wait`` (defaults to :data:`capybara.default_max_wait_time`) seconds.\n\n If the given selector is not a valid selector, the first argument is assumed to be a locator\n and the default selector will be used.\n\n Args:\n selector (str, optional): The name of the selector to use. Defaults to\n :data:`capybara.default_selector`.\n *locators (str): Variable length list of locators.\n **kwargs: Arbitrary keyword arguments for :class:`SelectorQuery`.\n \"\"\"\n\n wait = kwargs['wait'] if 'wait' in kwargs else capybara.default_max_wait_time\n\n if not isinstance(selector, Hashable) or selector not in selectors:\n locators = (selector,) + locators\n selector = capybara.default_selector\n\n @self.synchronize(wait=wait)\n def assert_all_of_selectors():\n for locator in locators:\n self.assert_selector(selector, locator, **kwargs)\n\n return True\n\n return assert_all_of_selectors()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nasserting that none of the provided selectors are present on the given page or descendants of the current node. If options are provided, the assertion will check that each locator is present with those options as well (other than ``wait``). :: page.assert_none_of_selectors(\"custom\", \"Tom\", \"Joe\", visible=\"all\") page.assert_none_of_selectors(\"css\", \"#my_div\", \"a.not_clicked\") It accepts all options that :meth:`find_all` accepts, such as ``text`` and ``visible``. The ``wait`` option applies to all of the selectors as a group, so none of the locators must be present with ``wait`` (defaults to :data:`capybara.default_max_wait_time`) seconds. If the given selector is not a valid selector, the first argument is assumed to be a locator and the default selector will be used. Args: selector (str, optional): The name of the selector to use. Defaults to :data:`capybara.default_selector`. *locators (str): Variable length list of locators. **kwargs: Arbitrary keyword arguments for :class:`SelectorQuery`.", "response": "def assert_none_of_selectors(self, selector, *locators, **kwargs):\n \"\"\"\n Asserts that none of the provided selectors are present on the given page or descendants of\n the current node. If options are provided, the assertion will check that each locator is\n present with those options as well (other than ``wait``). ::\n\n page.assert_none_of_selectors(\"custom\", \"Tom\", \"Joe\", visible=\"all\")\n page.assert_none_of_selectors(\"css\", \"#my_div\", \"a.not_clicked\")\n\n It accepts all options that :meth:`find_all` accepts, such as ``text`` and ``visible``.\n\n The ``wait`` option applies to all of the selectors as a group, so none of the locators must\n be present with ``wait`` (defaults to :data:`capybara.default_max_wait_time`) seconds.\n\n If the given selector is not a valid selector, the first argument is assumed to be a locator\n and the default selector will be used.\n\n Args:\n selector (str, optional): The name of the selector to use. Defaults to\n :data:`capybara.default_selector`.\n *locators (str): Variable length list of locators.\n **kwargs: Arbitrary keyword arguments for :class:`SelectorQuery`.\n \"\"\"\n wait = kwargs['wait'] if 'wait' in kwargs else capybara.default_max_wait_time\n\n if not isinstance(selector, Hashable) or selector not in selectors:\n locators = (selector,) + locators\n selector = capybara.default_selector\n\n @self.synchronize(wait=wait)\n def assert_none_of_selectors():\n for locator in locators:\n self.assert_no_selector(selector, locator, **kwargs)\n\n return True\n\n return assert_none_of_selectors()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nasserting that a given selector is not on the page or a descendant of the current node. Usage is identical to :meth:`assert_selector`. Query options such as ``count``, ``minimum``, and ``between`` are considered to be an integral part of the selector. This will return True, for example, if a page contains 4 anchors but the query expects 5:: page.assert_no_selector(\"a\", minimum=1) # Found, raises ExpectationNotMet page.assert_no_selector(\"a\", count=4) # Found, raises ExpectationNotMet page.assert_no_selector(\"a\", count=5) # Not Found, returns True Args: *args: Variable length argument list for :class:`SelectorQuery`. **kwargs: Arbitrary keyword arguments for :class:`SelectorQuery`. Returns: True Raises: ExpectationNotMet: The given selector matched.", "response": "def assert_no_selector(self, *args, **kwargs):\n \"\"\"\n Asserts that a given selector is not on the page or a descendant of the current node. Usage\n is identical to :meth:`assert_selector`.\n\n Query options such as ``count``, ``minimum``, and ``between`` are considered to be an\n integral part of the selector. This will return True, for example, if a page contains 4\n anchors but the query expects 5::\n\n page.assert_no_selector(\"a\", minimum=1) # Found, raises ExpectationNotMet\n page.assert_no_selector(\"a\", count=4) # Found, raises ExpectationNotMet\n page.assert_no_selector(\"a\", count=5) # Not Found, returns True\n\n Args:\n *args: Variable length argument list for :class:`SelectorQuery`.\n **kwargs: Arbitrary keyword arguments for :class:`SelectorQuery`.\n\n Returns:\n True\n\n Raises:\n ExpectationNotMet: The given selector matched.\n \"\"\"\n\n query = SelectorQuery(*args, **kwargs)\n\n @self.synchronize(wait=query.wait)\n def assert_no_selector():\n result = query.resolve_for(self)\n\n if result.matches_count and (\n len(result) > 0 or expects_none(query.options)):\n raise ExpectationNotMet(result.negative_failure_message)\n\n return True\n\n return assert_no_selector()"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nasserts that the current node matches a given selector.", "response": "def assert_matches_selector(self, *args, **kwargs):\n \"\"\"\n Asserts that the current node matches a given selector. ::\n\n node.assert_matches_selector(\"p#foo\")\n node.assert_matches_selector(\"xpath\", \"//p[@id='foo']\")\n\n It also accepts all options that :meth:`find_all` accepts, such as ``text`` and\n ``visible``. ::\n\n node.assert_matches_selector(\"li\", text=\"Horse\", visible=True)\n\n Args:\n *args: Variable length argument list for :class:`SelectorQuery`.\n **kwargs: Arbitrary keyword arguments for :class:`SelectorQuery`.\n\n Returns:\n True\n\n Raises:\n ExpectationNotMet: If the selector does not match.\n \"\"\"\n\n query = SelectorQuery(*args, **kwargs)\n\n @self.synchronize(wait=query.wait)\n def assert_matches_selector():\n result = query.resolve_for(self.find_first(\"xpath\", \"./parent::*\", minimum=0) or self.query_scope)\n\n if self not in result:\n raise ExpectationNotMet(\"Item does not match the provided selector\")\n\n return True\n\n return assert_matches_selector()"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nchecks if the page or current node has a radio button or checkbox with the given label name or id or id that is currently checked.", "response": "def has_checked_field(self, locator, **kwargs):\n \"\"\"\n Checks if the page or current node has a radio button or checkbox with the given label,\n value, or id, that is currently checked.\n\n Args:\n locator (str): The label, name, or id of a checked field.\n **kwargs: Arbitrary keyword arguments for :class:`SelectorQuery`.\n\n Returns:\n bool: Whether it exists.\n \"\"\"\n\n kwargs[\"checked\"] = True\n return self.has_selector(\"field\", locator, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef has_no_checked_field(self, locator, **kwargs):\n\n kwargs[\"checked\"] = True\n return self.has_no_selector(\"field\", locator, **kwargs)", "response": "Checks if the page or current node has no radio button or checkbox with the given label name or id or id that is currently checked."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef has_unchecked_field(self, locator, **kwargs):\n\n kwargs[\"checked\"] = False\n return self.has_selector(\"field\", locator, **kwargs)", "response": "Checks if the page or current node has a radio button or checkbox with the given label name or id or id that is currently unchecked."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef has_no_unchecked_field(self, locator, **kwargs):\n\n kwargs[\"checked\"] = False\n return self.has_no_selector(\"field\", locator, **kwargs)", "response": "Checks if the page or current node has no radio button or checkbox with the given label name or id or id."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef assert_text(self, *args, **kwargs):\n\n query = TextQuery(*args, **kwargs)\n\n @self.synchronize(wait=query.wait)\n def assert_text():\n count = query.resolve_for(self)\n\n if not (matches_count(count, query.options) and\n (count > 0 or expects_none(query.options))):\n raise ExpectationNotMet(query.failure_message)\n\n return True\n\n return assert_text()", "response": "Assert that the page or current node has the given text content ignoring any HTML tags."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nasserts that the page or current node doesn t have the given text content.", "response": "def assert_no_text(self, *args, **kwargs):\n \"\"\"\n Asserts that the page or current node doesn't have the given text content, ignoring any\n HTML tags.\n\n Args:\n *args: Variable length argument list for :class:`TextQuery`.\n **kwargs: Arbitrary keyword arguments for :class:`TextQuery`.\n\n Returns:\n True\n\n Raises:\n ExpectationNotMet: If the assertion hasn't succeeded during the wait time.\n \"\"\"\n\n query = TextQuery(*args, **kwargs)\n\n @self.synchronize(wait=query.wait)\n def assert_no_text():\n count = query.resolve_for(self)\n\n if matches_count(count, query.options) and (\n count > 0 or expects_none(query.options)):\n raise ExpectationNotMet(query.negative_failure_message)\n\n return True\n\n return assert_no_text()"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nasserts that the page has the given title.", "response": "def assert_title(self, title, **kwargs):\n \"\"\"\n Asserts that the page has the given title.\n\n Args:\n title (str | RegexObject): The string or regex that the title should match.\n **kwargs: Arbitrary keyword arguments for :class:`TitleQuery`.\n\n Returns:\n True\n\n Raises:\n ExpectationNotMet: If the assertion hasn't succeeded during the wait time.\n \"\"\"\n\n query = TitleQuery(title, **kwargs)\n\n @self.synchronize(wait=query.wait)\n def assert_title():\n if not query.resolves_for(self):\n raise ExpectationNotMet(query.failure_message)\n\n return True\n\n return assert_title()"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nasserting that the page doesn t have the given title.", "response": "def assert_no_title(self, title, **kwargs):\n \"\"\"\n Asserts that the page doesn't have the given title.\n\n Args:\n title (str | RegexObject): The string that the title should include.\n **kwargs: Arbitrary keyword arguments for :class:`TitleQuery`.\n\n Returns:\n True\n\n Raises:\n ExpectationNotMet: If the assertion hasn't succeeded during the wait time.\n \"\"\"\n\n query = TitleQuery(title, **kwargs)\n\n @self.synchronize(wait=query.wait)\n def assert_no_title():\n if query.resolves_for(self):\n raise ExpectationNotMet(query.negative_failure_message)\n\n return True\n\n return assert_no_title()"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncheck if the page has the given title.", "response": "def has_title(self, title, **kwargs):\n \"\"\"\n Checks if the page has the given title.\n\n Args:\n title (str | RegexObject): The string or regex that the title should match.\n **kwargs: Arbitrary keyword arguments for :class:`TitleQuery`.\n\n Returns:\n bool: Whether it matches.\n \"\"\"\n\n try:\n self.assert_title(title, **kwargs)\n return True\n except ExpectationNotMet:\n return False"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef has_no_title(self, title, **kwargs):\n\n try:\n self.assert_no_title(title, **kwargs)\n return True\n except ExpectationNotMet:\n return False", "response": "Checks if the page doesn t have the given title."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nfinding all elements on the page matching the given selector and options.", "response": "def find_all(self, *args, **kwargs):\n \"\"\"\n Find all elements on the page matching the given selector and options.\n\n Both XPath and CSS expressions are supported, but Capybara does not try to automatically\n distinguish between them. The following statements are equivalent::\n\n page.find_all(\"css\", \"a#person_123\")\n page.find_all(\"xpath\", \"//a[@id='person_123']\")\n\n If the type of selector is left out, Capybara uses :data:`capybara.default_selector`. It's\n set to ``\"css\"`` by default. ::\n\n page.find_all(\"a#person_123\")\n\n capybara.default_selector = \"xpath\"\n page.find_all(\"//a[@id='person_123']\")\n\n The set of found elements can further be restricted by specifying options. It's possible to\n select elements by their text or visibility::\n\n page.find_all(\"a\", text=\"Home\")\n page.find_all(\"#menu li\", visible=True)\n\n By default if no elements are found, an empty list is returned; however, expectations can be\n set on the number of elements to be found which will trigger Capybara's waiting behavior for\n the expectations to match. The expectations can be set using::\n\n page.assert_selector(\"p#foo\", count=4)\n page.assert_selector(\"p#foo\", maximum=10)\n page.assert_selector(\"p#foo\", minimum=1)\n page.assert_selector(\"p#foo\", between=range(1, 11))\n\n See :func:`capybara.result.Result.matches_count` for additional information about count\n matching.\n\n Args:\n *args: Variable length argument list for :class:`SelectorQuery`.\n **kwargs: Arbitrary keyword arguments for :class:`SelectorQuery`.\n\n Returns:\n Result: A collection of found elements.\n\n Raises:\n ExpectationNotMet: The matched results did not meet the expected criteria.\n \"\"\"\n\n query = SelectorQuery(*args, **kwargs)\n\n @self.synchronize(wait=query.wait)\n def find_all():\n result = query.resolve_for(self)\n\n if not result.matches_count:\n raise ExpectationNotMet(result.failure_message)\n\n return result\n\n return find_all()"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nfinds the first element on the page matching the given selector and options.", "response": "def find_first(self, *args, **kwargs):\n \"\"\"\n Find the first element on the page matching the given selector and options, or None if no\n element matches.\n\n By default, no waiting behavior occurs. However, if ``capybara.wait_on_first_by_default``\n is set to true, it will trigger Capybara's waiting behavior for a minimum of 1 matching\n element to be found.\n\n Args:\n *args: Variable length argument list for :class:`SelectorQuery`.\n **kwargs: Arbitrary keyword arguments for :class:`SelectorQuery`.\n\n Returns:\n Element: The found element or None.\n \"\"\"\n\n if capybara.wait_on_first_by_default:\n kwargs.setdefault(\"minimum\", 1)\n\n try:\n result = self.find_all(*args, **kwargs)\n return result[0] if len(result) > 0 else None\n except ExpectationNotMet:\n return None"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef resolve_for(self, node):\n\n self.node = node\n self.actual_text = normalize_text(\n node.visible_text if self.query_type == \"visible\" else node.all_text)\n self.count = len(re.findall(self.search_regexp, self.actual_text))\n\n return self.count", "response": "Resolves this query relative to the given node."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn the inner content of a given XML node including tags.", "response": "def inner_content(node):\n \"\"\"\n Returns the inner content of a given XML node, including tags.\n\n Args:\n node (lxml.etree.Element): The node whose inner content is desired.\n\n Returns:\n str: The inner content of the node.\n \"\"\"\n\n from lxml import etree\n\n # Include text content at the start of the node.\n parts = [node.text]\n\n for child in node.getchildren():\n # Include the child serialized to raw XML.\n parts.append(etree.tostring(child, encoding=\"utf-8\"))\n\n # Include any text following the child.\n parts.append(child.tail)\n\n # Discard any non-existent text parts and return.\n return \"\".join(filter(None, parts))"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns the inner text of a given XML node excluding tags.", "response": "def inner_text(node):\n \"\"\"\n Returns the inner text of a given XML node, excluding tags.\n\n Args:\n node: (lxml.etree.Element): The node whose inner text is desired.\n\n Returns:\n str: The inner text of the node.\n \"\"\"\n\n from lxml import etree\n\n # Include text content at the start of the node.\n parts = [node.text]\n\n for child in node.getchildren():\n # Include the raw text content of the child.\n parts.append(etree.tostring(child, encoding=\"utf-8\", method=\"text\"))\n\n # Include any text following the child.\n parts.append(child.tail)\n\n # Discard any non-existent text parts and return.\n return \"\".join(map(decode_bytes, filter(None, parts)))"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nnormalizing the given URL with all query keys properly escaped.", "response": "def normalize_url(url):\n \"\"\"\n Returns the given URL with all query keys properly escaped.\n\n Args:\n url (str): The URL to normalize.\n\n Returns:\n str: The normalized URL.\n \"\"\"\n\n uri = urlparse(url)\n query = uri.query or \"\"\n\n pairs = parse_qsl(query)\n decoded_pairs = [(unquote(key), value) for key, value in pairs]\n encoded_pairs = [(quote(key), value) for key, value in decoded_pairs]\n normalized_query = urlencode(encoded_pairs)\n\n return ParseResult(\n scheme=uri.scheme,\n netloc=uri.netloc,\n path=uri.path,\n params=uri.params,\n query=normalized_query,\n fragment=uri.fragment).geturl()"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ndefines a write - only property that provides a setter function that provides the given setter function. This is a decorator that provides a setter function that can be used to set the value of the object s _handler property.", "response": "def setter_decorator(fset):\n \"\"\"\n Define a write-only property that, in addition to the given setter function, also\n provides a setter decorator defined as the property's getter function.\n\n This allows one to set the property either through traditional assignment, as a\n method argument, or through decoration::\n\n class Widget(object):\n @setter_decorator\n def handler(self, value):\n self._handler = value\n\n widget = Widget()\n\n # Method 1: Traditional assignment\n widget.handler = lambda input: process(input)\n\n # Method 2: Assignment via method argument\n widget.handler(lambda input: process(input))\n\n # Method 3: Assignment via decoration\n @widget.handler\n def handler(input):\n return process(input)\n\n # Method 3b: Assignment via decoration with extraneous parens\n @widget.handler()\n def handler(input):\n return process(input)\n \"\"\"\n\n def fget(self):\n def inner(value):\n fset(self, value)\n\n def outer(value=None):\n if value:\n # We are being called with the desired value, either directly or\n # as a decorator.\n inner(value)\n else:\n # Assume we are being called as a decorator with extraneous parens,\n # so return the setter as the actual decorator.\n return inner\n\n return outer\n\n fdoc = fset.__doc__\n\n return property(fget, fset, None, fdoc)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn True if the given value is valid.", "response": "def _valid_value(self, value):\n \"\"\" bool: Whether the given value is valid. \"\"\"\n\n if not self.valid_values:\n return True\n\n valid_values = (self.valid_values if isinstance(self.valid_values, list)\n else list(self.valid_values))\n return value in valid_values"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nfinds a file field on the page and attach a file given its path.", "response": "def attach_file(self, locator_or_path, path=None, **kwargs):\n \"\"\"\n Find a file field on the page and attach a file given its path. The file field can be found\n via its name, id, or label text. ::\n\n page.attach_file(locator, \"/path/to/file.png\")\n\n Args:\n locator_or_path (str): Which field to attach the file to, or the path of the file that\n will be attached.\n path (str, optional): The path of the file that will be attached. Defaults to\n ``locator_or_path``.\n **kwargs: Arbitrary keyword arguments for :class:`SelectorQuery`.\n\n Raises:\n FileNotFound: No file exists at the given path.\n \"\"\"\n\n if path is None:\n locator, path = None, locator_or_path\n else:\n locator = locator_or_path\n\n if not os.path.isfile(path):\n raise FileNotFound(\"cannot attach file, {0} does not exist\".format(path))\n\n self.find(\"file_field\", locator, **kwargs).set(path)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncheck the contents of the specified check box.", "response": "def check(self, locator=None, allow_label_click=None, **kwargs):\n \"\"\"\n Find a check box and mark it as checked. The check box can be found via name, id, or label\n text. ::\n\n page.check(\"German\")\n\n Args:\n locator (str, optional): Which check box to check.\n allow_label_click (bool, optional): Attempt to click the label to toggle state if\n element is non-visible. Defaults to :data:`capybara.automatic_label_click`.\n **kwargs: Arbitrary keyword arguments for :class:`SelectorQuery`.\n \"\"\"\n\n self._check_with_label(\n \"checkbox\", True, locator=locator, allow_label_click=allow_label_click, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nfind a radio button and mark it as checked. The radio button can be found via name, id, or label text. :: page.choose(\"Male\") Args: locator (str, optional): Which radio button to choose. allow_label_click (bool, optional): Attempt to click the label to toggle state if element is non-visible. Defaults to :data:`capybara.automatic_label_click`. **kwargs: Arbitrary keyword arguments for :class:`SelectorQuery`.", "response": "def choose(self, locator=None, allow_label_click=None, **kwargs):\n \"\"\"\n Find a radio button and mark it as checked. The radio button can be found via name, id, or\n label text. ::\n\n page.choose(\"Male\")\n\n Args:\n locator (str, optional): Which radio button to choose.\n allow_label_click (bool, optional): Attempt to click the label to toggle state if\n element is non-visible. Defaults to :data:`capybara.automatic_label_click`.\n **kwargs: Arbitrary keyword arguments for :class:`SelectorQuery`.\n \"\"\"\n\n self._check_with_label(\n \"radio_button\", True, locator=locator, allow_label_click=allow_label_click, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef fill_in(self, locator=None, current_value=None, value=None, fill_options=None, **kwargs):\n\n if current_value is not None:\n kwargs[\"value\"] = current_value\n\n fill_options = fill_options or {}\n\n self.find(\"fillable_field\", locator, **kwargs).set(value, **fill_options)", "response": "Fills in the given value in the given field."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef select(self, value=None, field=None, **kwargs):\n\n if field:\n self.find(\"select\", field, **kwargs).find(\"option\", value, **kwargs).select_option()\n else:\n self.find(\"option\", value, **kwargs).select_option()", "response": "Select the option from the current scope."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nfinding a check box and uncheck it. The check box can be found via name, id, or label text. :: page.uncheck(\"German\") Args: locator (str, optional): Which check box to uncheck. allow_label_click (bool, optional): Attempt to click the label to toggle state if element is non-visible. Defaults to :data:`capybara.automatic_label_click`. **kwargs: Arbitrary keyword arguments for :class:`SelectorQuery`.", "response": "def uncheck(self, locator=None, allow_label_click=None, **kwargs):\n \"\"\"\n Find a check box and uncheck it. The check box can be found via name, id, or label text. ::\n\n page.uncheck(\"German\")\n\n Args:\n locator (str, optional): Which check box to uncheck.\n allow_label_click (bool, optional): Attempt to click the label to toggle state if\n element is non-visible. Defaults to :data:`capybara.automatic_label_click`.\n **kwargs: Arbitrary keyword arguments for :class:`SelectorQuery`.\n \"\"\"\n\n self._check_with_label(\n \"checkbox\", False, locator=locator, allow_label_click=allow_label_click, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nfinds a select box on the page and unselect a particular option from it. If the select box is a multiple select, ``unselect`` can be called multiple times to unselect more than one option. The select box can be found via its name, id, or label text. :: page.unselect(\"March\", field=\"Month\") Args: value (str, optional): Which option to unselect. field (str, optional): The id, name, or label of the select box. **kwargs: Arbitrary keyword arguments for :class:`SelectorQuery`.", "response": "def unselect(self, value=None, field=None, **kwargs):\n \"\"\"\n Find a select box on the page and unselect a particular option from it. If the select box is\n a multiple select, ``unselect`` can be called multiple times to unselect more than one\n option. The select box can be found via its name, id, or label text. ::\n\n page.unselect(\"March\", field=\"Month\")\n\n Args:\n value (str, optional): Which option to unselect.\n field (str, optional): The id, name, or label of the select box.\n **kwargs: Arbitrary keyword arguments for :class:`SelectorQuery`.\n \"\"\"\n\n if field:\n self.find(\"select\", field, **kwargs).find(\"option\", value, **kwargs).unselect_option()\n else:\n self.find(\"option\", value, **kwargs).unselect_option()"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _check_with_label(self, selector, checked, locator=None, allow_label_click=None, visible=None, wait=None,\n **kwargs):\n \"\"\"\n Args:\n selector (str): The selector for the type of element that should be checked/unchecked.\n checked (bool): Whether the element should be checked.\n locator (str, optional): Which element to check.\n allow_label_click (bool, optional): Attempt to click the label to toggle state if\n element is non-visible. Defaults to :data:`capybara.automatic_label_click`.\n visible (bool | str, optional): The desired element visibility. Defaults to\n :data:`capybara.ignore_hidden_elements`.\n wait (int | float, optional): The number of seconds to wait to check the element.\n Defaults to :data:`capybara.default_max_wait_time`.\n **kwargs: Arbitrary keyword arguments for :class:`SelectorQuery`.\n \"\"\"\n\n if allow_label_click is None:\n allow_label_click = capybara.automatic_label_click\n\n @self.synchronize(wait=BaseQuery.normalize_wait(wait))\n def check_with_label():\n element = None\n try:\n element = self.find(selector, locator, visible=visible, **kwargs)\n element.set(checked)\n except Exception as e:\n if not allow_label_click or not self._should_catch_error(e):\n raise\n try:\n if not element:\n element = self.find(selector, locator, visible=\"all\", **kwargs)\n label = self.find(\"label\", field=element, visible=True)\n if element.checked != checked:\n label.click()\n except Exception:\n raise e\n\n check_with_label()", "response": "Check the element with the specified label."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef synchronize(self, func=None, wait=None, errors=()):\n\n def decorator(func):\n @wraps(func)\n def outer(*args, **kwargs):\n seconds = wait if wait is not None else capybara.default_max_wait_time\n\n def inner():\n return func(*args, **kwargs)\n\n if self.session.synchronized:\n return inner()\n else:\n timer = Timer(seconds)\n self.session.synchronized = True\n try:\n while True:\n try:\n return inner()\n except Exception as e:\n self.session.raise_server_error()\n\n if not self._should_catch_error(e, errors):\n raise\n if timer.expired:\n raise\n\n sleep(0.05)\n\n if timer.stalled:\n raise FrozenInTime(\n \"time appears to be frozen, Capybara does not work with \"\n \"libraries which freeze time, consider using time \"\n \"traveling instead\")\n if capybara.automatic_reload:\n self.reload()\n finally:\n self.session.synchronized = False\n\n return outer\n\n if func:\n return decorator(func)\n else:\n return decorator", "response": "This method is used to synchronize a function with Capybara s primary defense against asynchronicity problems."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns whether the given error should be caught.", "response": "def _should_catch_error(self, error, errors=()):\n \"\"\"\n Returns whether to catch the given error.\n\n Args:\n error (Exception): The error to consider.\n errors (Tuple[Type[Exception], ...], optional): The exception types that should be\n caught. Defaults to :class:`ElementNotFound` plus any driver-specific invalid\n element errors.\n\n Returns:\n bool: Whether to catch the given error.\n \"\"\"\n\n caught_errors = (\n errors or\n self.session.driver.invalid_element_errors + (ElementNotFound,))\n\n return isinstance(error, caught_errors)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef compare_count(self):\n\n if self.query.options[\"count\"] is not None:\n count_opt = int(self.query.options[\"count\"])\n self._cache_at_least(count_opt + 1)\n return cmp(len(self._result_cache), count_opt)\n\n if self.query.options[\"minimum\"] is not None:\n min_opt = int(self.query.options[\"minimum\"])\n if not self._cache_at_least(min_opt):\n return -1\n\n if self.query.options[\"maximum\"] is not None:\n max_opt = int(self.query.options[\"maximum\"])\n if self._cache_at_least(max_opt + 1):\n return 1\n\n if self.query.options[\"between\"] is not None:\n between = self.query.options[\"between\"]\n min_opt, max_opt = between[0], between[-1]\n if not self._cache_at_least(min_opt):\n return -1\n if self._cache_at_least(max_opt + 1):\n return 1\n return 0\n\n return 0", "response": "Returns how the result count compares to the query options."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef failure_message(self):\n\n message = failure_message(self.query.description, self.query.options)\n\n if len(self) > 0:\n message += \", found {count} {matches}: {results}\".format(\n count=len(self),\n matches=declension(\"match\", \"matches\", len(self)),\n results=\", \".join([desc(node.text) for node in self]))\n else:\n message += \" but there were no matches\"\n\n if self._rest:\n elements = \", \".join([desc(element.text) for element in self._rest])\n message += (\". Also found {}, which matched the selector\"\n \" but not all filters.\".format(elements))\n\n return message", "response": "str - A message describing the query failure."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nattempting to fill the result cache with at least the given size. Returns True if the result cache contains at least the given size.", "response": "def _cache_at_least(self, size):\n \"\"\"\n Attempts to fill the result cache with at least the given number of results.\n\n Returns:\n bool: Whether the cache contains at least the given size.\n \"\"\"\n\n try:\n while len(self._result_cache) < size:\n self._result_cache.append(next(self._result_iter))\n return True\n except StopIteration:\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef desc(value):\n\n def normalize_strings(value):\n if isinstance(value, list):\n value = [normalize_strings(e) for e in value]\n\n if isinstance(value, dict):\n value = {normalize_strings(k): normalize_strings(v) for k, v in iter(value.items())}\n\n if isregex(value):\n value = value.pattern\n\n if isbytes(value):\n value = decode_bytes(value)\n\n if PY2:\n if isstring(value):\n # In Python 2, strings (``unicode`` objects) represent as ``u'...'``, so ensure\n # the string is encoded (as a ``str`` object) for cleaner representation.\n value = encode_string(value)\n\n return value\n\n value = normalize_strings(value)\n\n return repr(value)", "response": "str - A normalized representation for a user - provided value."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef expects_none(options):\n\n if any(options.get(key) is not None for key in [\"count\", \"maximum\", \"minimum\", \"between\"]):\n return matches_count(0, options)\n else:\n return False", "response": "Returns whether the given query options expect a possible count of zero."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef failure_message(description, options):\n\n message = \"expected to find {}\".format(description)\n\n if options[\"count\"] is not None:\n message += \" {count} {times}\".format(\n count=options[\"count\"],\n times=declension(\"time\", \"times\", options[\"count\"]))\n elif options[\"between\"] is not None:\n between = options[\"between\"]\n if between:\n first, last = between[0], between[-1]\n else:\n first, last = None, None\n\n message += \" between {first} and {last} times\".format(\n first=first,\n last=last)\n elif options[\"maximum\"] is not None:\n message += \" at most {maximum} {times}\".format(\n maximum=options[\"maximum\"],\n times=declension(\"time\", \"times\", options[\"maximum\"]))\n elif options[\"minimum\"] is not None:\n message += \" at least {minimum} {times}\".format(\n minimum=options[\"minimum\"],\n times=declension(\"time\", \"times\", options[\"minimum\"]))\n\n return message", "response": "Returns a expectation failure message for the given query description."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef matches_count(count, options):\n\n if options.get(\"count\") is not None:\n return count == int(options[\"count\"])\n if options.get(\"maximum\") is not None and int(options[\"maximum\"]) < count:\n return False\n if options.get(\"minimum\") is not None and int(options[\"minimum\"]) > count:\n return False\n if options.get(\"between\") is not None and count not in options[\"between\"]:\n return False\n return True", "response": "Returns whether the given count matches the given query options."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef normalize_text(value):\n\n if value is None:\n return \"\"\n\n text = decode_bytes(value) if isbytes(value) else str_(value)\n\n return normalize_whitespace(text)", "response": "Normalizes the given value to a string of text with extra whitespace removed."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nnormalizes the given text with outer whitespace removed inner whitespace collapsed.", "response": "def normalize_whitespace(text):\n \"\"\"\n Returns the given text with outer whitespace removed and inner whitespace collapsed.\n\n Args:\n text (str): The text to normalize.\n\n Returns:\n str: The normalized text.\n \"\"\"\n\n return re.sub(r\"\\s+\", \" \", text, flags=re.UNICODE).strip()"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns a compiled regular expression for the given text.", "response": "def toregex(text, exact=False):\n \"\"\"\n Returns a compiled regular expression for the given text.\n\n Args:\n text (str | RegexObject): The text to match.\n exact (bool, optional): Whether the generated regular expression should match exact\n strings. Defaults to False.\n\n Returns:\n RegexObject: A compiled regular expression that will match the text.\n \"\"\"\n\n if isregex(text):\n return text\n\n escaped = re.escape(normalize_text(text))\n if exact:\n escaped = r\"\\A{}\\Z\".format(escaped)\n\n return re.compile(escaped)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef resolves_for(self, session):\n\n if self.url:\n self.actual_path = session.current_url\n else:\n result = urlparse(session.current_url)\n\n if self.only_path:\n self.actual_path = result.path\n else:\n request_uri = result.path\n if result.query:\n request_uri += \"?{0}\".format(result.query)\n\n self.actual_path = request_uri\n\n if isregex(self.expected_path):\n return self.expected_path.search(self.actual_path)\n else:\n return normalize_url(self.actual_path) == normalize_url(self.expected_path)", "response": "Returns whether this query resolves for the given session."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nboots a server for the app.", "response": "def boot(self):\n \"\"\"\n Boots a server for the app, if it isn't already booted.\n\n Returns:\n Server: This server.\n \"\"\"\n\n if not self.responsive:\n # Remember the port so we can reuse it if we try to serve this same app again.\n type(self)._ports[self.port_key] = self.port\n\n init_func = capybara.servers[capybara.server_name]\n init_args = (self.middleware, self.port, self.host)\n\n self.server_thread = Thread(target=init_func, args=init_args)\n\n # Inform Python that it shouldn't wait for this thread to terminate before\n # exiting. (It will still be appropriately terminated when the process exits.)\n self.server_thread.daemon = True\n\n self.server_thread.start()\n\n # Make sure the server actually starts and becomes responsive.\n timer = Timer(60)\n while not self.responsive:\n if timer.expired:\n raise RuntimeError(\"WSGI application timed out during boot\")\n self.server_thread.join(0.1)\n\n return self"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef responsive(self):\n\n if self.server_thread and self.server_thread.join(0):\n return False\n\n try:\n # Try to fetch the endpoint added by the middleware.\n identify_url = \"http://{0}:{1}/__identify__\".format(self.host, self.port)\n\n with closing(urlopen(identify_url)) as response:\n body, status_code = response.read(), response.getcode()\n\n if str(status_code)[0] == \"2\" or str(status_code)[0] == \"3\":\n return decode_bytes(body) == str(id(self.app))\n except URLError:\n pass\n\n return False", "response": "Returns True if the server is up and responsive."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef cgetter(self, fcget: typing.Optional[typing.Callable[[typing.Any], typing.Any]]) -> \"AdvancedProperty\":\n self.__fcget = fcget\n return self", "response": "Descriptor to change the class - wide getter on a property."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nsetting the descriptor to change the instance method.", "response": "def instance_method(self, imeth: typing.Optional[typing.Callable[..., typing.Any]]) -> \"SeparateClassMethod\":\n \"\"\"Descriptor to change instance method.\n\n :param imeth: New instance method.\n :type imeth: typing.Optional[typing.Callable]\n :return: SeparateClassMethod\n :rtype: SeparateClassMethod\n \"\"\"\n self.__instance_method = imeth\n return self"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nset the class method to be used for this class.", "response": "def class_method(self, cmeth: typing.Optional[typing.Callable[..., typing.Any]]) -> \"SeparateClassMethod\":\n \"\"\"Descriptor to change class method.\n\n :param cmeth: New class method.\n :type cmeth: typing.Optional[typing.Callable]\n :return: SeparateClassMethod\n :rtype: SeparateClassMethod\n \"\"\"\n self.__class_method = cmeth\n return self"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget outer traceback text for logging.", "response": "def __traceback(self) -> str:\n \"\"\"Get outer traceback text for logging.\"\"\"\n if not self.log_traceback:\n return \"\"\n exc_info = sys.exc_info()\n stack = traceback.extract_stack()\n exc_tb = traceback.extract_tb(exc_info[2])\n full_tb = stack[:1] + exc_tb # cut decorator and build full traceback\n exc_line: typing.List[str] = traceback.format_exception_only(*exc_info[:2])\n # Make standard traceback string\n tb_text = \"\\nTraceback (most recent call last):\\n\" + \"\".join(traceback.format_list(full_tb)) + \"\".join(exc_line)\n return tb_text"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef __get_obj_source(self, instance: typing.Any, owner: typing.Optional[type] = None) -> str:\n if self.log_object_repr:\n return f\"{instance!r}\"\n return f\"<{owner.__name__ if owner is not None else instance.__class__.__name__}() at 0x{id(instance):X}>\"", "response": "Get object repr block."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _get_logger_for_instance(self, instance: typing.Any) -> logging.Logger:\n if self.logger is not None: # pylint: disable=no-else-return\n return self.logger\n elif hasattr(instance, \"logger\") and isinstance(instance.logger, logging.Logger):\n return instance.logger\n elif hasattr(instance, \"log\") and isinstance(instance.log, logging.Logger):\n return instance.log\n return _LOGGER", "response": "Get the logger for the given instance."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef logger(self, logger: typing.Union[logging.Logger, str, None]) -> None:\n if logger is None or isinstance(logger, logging.Logger):\n self.__logger = logger\n else:\n self.__logger = logging.getLogger(logger)", "response": "Set the logger instance to use as override."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_simple_vars_from_src(src):\n ast_data = (ast.Str, ast.Num, ast.List, ast.Set, ast.Dict, ast.Tuple, ast.Bytes, ast.NameConstant)\n\n tree = ast.parse(src)\n\n result = collections.OrderedDict()\n\n for node in ast.iter_child_nodes(tree):\n if not isinstance(node, ast.Assign): # We parse assigns only\n continue\n try:\n if isinstance(node.value, ast_data):\n value = ast.literal_eval(node.value)\n else:\n continue\n except ValueError:\n continue\n for tgt in node.targets:\n if isinstance(tgt, ast.Name) and isinstance(tgt.ctx, ast.Store):\n result[tgt.id] = value\n return result", "response": "Get simple variable names and values from source code."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef run(self):\n try:\n build_ext.build_ext.run(self)\n\n # Copy __init__.py back to repair package.\n build_dir = os.path.abspath(self.build_lib)\n root_dir = os.path.abspath(os.path.join(__file__, \"..\"))\n target_dir = build_dir if not self.inplace else root_dir\n\n src_file = os.path.join(\"advanced_descriptors\", \"__init__.py\")\n\n src = os.path.join(root_dir, src_file)\n dst = os.path.join(target_dir, src_file)\n\n if src != dst:\n shutil.copyfile(src, dst)\n except (\n distutils.errors.DistutilsPlatformError,\n FileNotFoundError,\n ):\n raise BuildFailed()", "response": "Runs the extension build."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _call_api(self, method, params=None):\n url = self.url.format(method=method)\n if not params:\n params = {'token': self.token}\n else:\n params['token'] = self.token\n logger.debug('Send request to %s', url)\n response = requests.get(url, params=params).json()\n if self.verify:\n if not response['ok']:\n msg = 'For {url} API returned this bad response {response}'\n raise Exception(msg.format(url=url, response=response))\n return response", "response": "Call the Slack API."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a list of all channels in this slack team", "response": "def channels(self):\n \"\"\"\n List of channels of this slack team\n \"\"\"\n if not self._channels:\n self._channels = self._call_api('channels.list')['channels']\n return self._channels"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef users(self):\n if not self._users:\n self._users = self._call_api('users.list')['members']\n return self._users", "response": "Returns a list of users of this slack team"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning the channel dict given by human - readable name", "response": "def channel_from_name(self, name):\n \"\"\"\n Return the channel dict given by human-readable {name}\n \"\"\"\n try:\n channel = [channel for channel in self.channels\n if channel['name'] == name][0]\n except IndexError:\n raise ValueError('Unknown channel for name: \"{}\"'.format(name))\n return channel"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef make_message(self, text, channel):\n try:\n channel_id = self.slack.channel_from_name(channel)['id']\n except ValueError:\n channel_id = channel\n return pack({\n 'text': text,\n 'type': 'message',\n 'channel': channel_id,\n 'id': self.message_id,\n })", "response": "High - level function for creating messages. Return packed bytes."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ntranslate machine identifiers into human - readable ones.", "response": "def translate(self, message):\n \"\"\"\n Translate machine identifiers into human-readable\n \"\"\"\n # translate user\n try:\n user_id = message.pop('user')\n user = self.slack.user_from_id(user_id)\n message[u'user'] = user['name']\n except (KeyError, IndexError, ValueError):\n pass\n # translate channel\n try:\n if type(message['channel']) == str:\n channel_id = message.pop('channel')\n else:\n channel_id = message.pop('channel')['id']\n self.slack.reload_channels()\n channel = self.slack.channel_from_id(channel_id)\n message[u'channel'] = channel['name']\n except (KeyError, IndexError, ValueError):\n pass\n return message"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nsends the payload onto the {slack.[payload['type]'} channel. The message is transalated from IDs to human-readable identifiers. Note: The slack API only sends JSON, isBinary will always be false.", "response": "def onMessage(self, payload, isBinary):\n \"\"\"\n Send the payload onto the {slack.[payload['type]'} channel.\n The message is transalated from IDs to human-readable identifiers.\n\n Note: The slack API only sends JSON, isBinary will always be false.\n \"\"\"\n msg = self.translate(unpack(payload))\n if 'type' in msg:\n channel_name = 'slack.{}'.format(msg['type'])\n print('Sending on {}'.format(channel_name))\n channels.Channel(channel_name).send({'text': pack(msg)})"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nsending a Slack message to Slack", "response": "def sendSlack(self, message):\n \"\"\"\n Send message to Slack\n \"\"\"\n channel = message.get('channel', 'general')\n self.sendMessage(self.make_message(message['text'], channel))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreading the available messages and send them through the protocol.", "response": "def read_channel(self):\n \"\"\"\n Get available messages and send through to the protocol\n \"\"\"\n channel, message = self.protocol.channel_layer.receive_many([u'slack.send'], block=False)\n delay = 0.1\n if channel:\n self.protocols[0].sendSlack(message)\n reactor.callLater(delay, self.read_channel)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nruns the Slack API.", "response": "def run(self, args):\n \"\"\"\n Pass in raw arguments, instantiate Slack API and begin client.\n \"\"\"\n args = self.parser.parse_args(args)\n if not args.token:\n raise ValueError('Supply the slack token through --token or setting DJANGOBOT_TOKEN')\n\n # Import the channel layer\n sys.path.insert(0, \".\")\n module_path, object_path = args.channel_layer.split(':', 1)\n channel_layer = importlib.import_module(module_path)\n for part in object_path.split('.'):\n channel_layer = getattr(channel_layer, part)\n\n # Boot up the client\n Client(\n channel_layer=channel_layer,\n token=args.token,\n ).run()"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _set_affiliation(self, v, load=False):\n if hasattr(v, \"_utype\"):\n v = v._utype(v)\n try:\n t = YANGDynClass(\n v,\n base=RestrictedClassType(\n base_type=unicode,\n restriction_type=\"dict_key\",\n restriction_arg={\n u\"napalm-star-wars:EMPIRE\": {\n \"@namespace\": u\"https://napalm-yang.readthedocs.io/napalm-star-wars\",\n \"@module\": u\"napalm-star-wars\",\n },\n u\"EMPIRE\": {\n \"@namespace\": u\"https://napalm-yang.readthedocs.io/napalm-star-wars\",\n \"@module\": u\"napalm-star-wars\",\n },\n u\"napalm-star-wars:REBEL_ALLIANCE\": {\n \"@namespace\": u\"https://napalm-yang.readthedocs.io/napalm-star-wars\",\n \"@module\": u\"napalm-star-wars\",\n },\n u\"REBEL_ALLIANCE\": {\n \"@namespace\": u\"https://napalm-yang.readthedocs.io/napalm-star-wars\",\n \"@module\": u\"napalm-star-wars\",\n },\n },\n ),\n is_leaf=True,\n yang_name=\"affiliation\",\n parent=self,\n path_helper=self._path_helper,\n extmethods=self._extmethods,\n register_paths=True,\n namespace=\"https://napalm-yang.readthedocs.io/napalm-star-wars\",\n defining_module=\"napalm-star-wars\",\n yang_type=\"identityref\",\n is_config=True,\n )\n except (TypeError, ValueError):\n raise ValueError(\n {\n \"error-string\": \"\"\"affiliation must be of a type compatible with identityref\"\"\",\n \"defined-type\": \"napalm-star-wars:identityref\",\n \"generated-type\": \"\"\"YANGDynClass(base=RestrictedClassType(base_type=unicode, restriction_type=\"dict_key\", restriction_arg={u'napalm-star-wars:EMPIRE': {'@namespace': u'https://napalm-yang.readthedocs.io/napalm-star-wars', '@module': u'napalm-star-wars'}, u'EMPIRE': {'@namespace': u'https://napalm-yang.readthedocs.io/napalm-star-wars', '@module': u'napalm-star-wars'}, u'napalm-star-wars:REBEL_ALLIANCE': {'@namespace': u'https://napalm-yang.readthedocs.io/napalm-star-wars', '@module': u'napalm-star-wars'}, u'REBEL_ALLIANCE': {'@namespace': u'https://napalm-yang.readthedocs.io/napalm-star-wars', '@module': u'napalm-star-wars'}},), is_leaf=True, yang_name=\"affiliation\", parent=self, path_helper=self._path_helper, extmethods=self._extmethods, register_paths=True, namespace='https://napalm-yang.readthedocs.io/napalm-star-wars', defining_module='napalm-star-wars', yang_type='identityref', is_config=True)\"\"\",\n }\n )\n\n self.__affiliation = t\n if hasattr(self, \"_set\"):\n self._set()", "response": "Sets the affiliation of the current object."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef dict_diff(prv, nxt):\n keys = set(prv.keys() + nxt.keys())\n result = {}\n for k in keys:\n if prv.get(k) != nxt.get(k):\n result[k] = (prv.get(k), nxt.get(k))\n return result", "response": "Return a dict of keys that differ with another config object."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ngive a string add necessary codes to format the string.", "response": "def colorize(msg, color):\n \"\"\"Given a string add necessary codes to format the string.\"\"\"\n if DONT_COLORIZE:\n return msg\n else:\n return \"{}{}{}\".format(COLORS[color], msg, COLORS[\"endc\"])"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef v2_playbook_on_task_start(self, task, **kwargs):\n self.last_task_name = task.get_name()\n self.printed_last_task = False", "response": "Run when a task starts."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef v2_runner_on_ok(self, result, **kwargs):\n failed = \"failed\" in result._result\n unreachable = \"unreachable\" in result._result\n\n if (\n \"print_action\" in result._task.tags\n or failed\n or unreachable\n or self._display.verbosity > 1\n ):\n self._print_task()\n self.last_skipped = False\n msg = unicode(result._result.get(\"msg\", \"\")) or unicode(\n result._result.get(\"reason\", \"\")\n ) or unicode(\n result._result.get(\"message\", \"\")\n )\n\n stderr = [\n result._result.get(\"exception\", None),\n result._result.get(\"module_stderr\", None),\n ]\n stderr = \"\\n\".join([e for e in stderr if e]).strip()\n\n self._print_host_or_item(\n result._host,\n result._result.get(\"changed\", False),\n msg,\n result._result.get(\"diff\", None),\n is_host=True,\n error=failed or unreachable,\n stdout=result._result.get(\"module_stdout\", None),\n stderr=stderr.strip(),\n )\n\n if \"results\" in result._result:\n for r in result._result[\"results\"]:\n failed = \"failed\" in r\n\n stderr = [r.get(\"exception\", None), r.get(\"module_stderr\", None)]\n stderr = \"\\n\".join([e for e in stderr if e]).strip()\n\n self._print_host_or_item(\n r[\"item\"],\n r.get(\"changed\", False),\n unicode(r.get(\"msg\", \"\")),\n r.get(\"diff\", None),\n is_host=False,\n error=failed,\n stdout=r.get(\"module_stdout\", None),\n stderr=stderr.strip(),\n )\n else:\n self.last_skipped = True\n print(\".\", end=\"\")", "response": "Run when a task finishes correctly."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ndisplays info about playbook statistics.", "response": "def v2_playbook_on_stats(self, stats):\n \"\"\"Display info about playbook statistics.\"\"\"\n print()\n self.printed_last_task = False\n self._print_task(\"STATS\")\n\n hosts = sorted(stats.processed.keys())\n for host in hosts:\n s = stats.summarize(host)\n\n if s[\"failures\"] or s[\"unreachable\"]:\n color = \"failed\"\n elif s[\"changed\"]:\n color = \"changed\"\n else:\n color = \"ok\"\n\n msg = \"{} : ok={}\\tchanged={}\\tfailed={}\\tunreachable={}\".format(\n host, s[\"ok\"], s[\"changed\"], s[\"failures\"], s[\"unreachable\"]\n )\n print(colorize(msg, color))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nrunning when a task is skipped.", "response": "def v2_runner_on_skipped(self, result, **kwargs):\n \"\"\"Run when a task is skipped.\"\"\"\n if self._display.verbosity > 1:\n self._print_task()\n self.last_skipped = False\n\n line_length = 120\n spaces = \" \" * (31 - len(result._host.name) - 4)\n\n line = \" * {}{}- {}\".format(\n colorize(result._host.name, \"not_so_bold\"),\n spaces,\n colorize(\"skipped\", \"skipped\"),\n )\n\n reason = result._result.get(\"skipped_reason\", \"\") or result._result.get(\n \"skip_reason\", \"\"\n )\n if len(reason) < 50:\n line += \" -- {}\".format(reason)\n print(\"{} {}---------\".format(line, \"-\" * (line_length - len(line))))\n else:\n print(\"{} {}\".format(line, \"-\" * (line_length - len(line))))\n print(self._indent_text(reason, 8))\n print(reason)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef prefix_to_addrmask(value, sep=\" \"):\n prefix = netaddr.IPNetwork(value)\n return \"{}{}{}\".format(prefix.ip, sep, prefix.netmask)", "response": "Converts a CIDR formatted prefix into an address netmask representation."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nsetting the keepalive interval of the current node.", "response": "def _set_keepalive_interval(self, v, load=False):\n \"\"\"\n Setter method for keepalive_interval, mapped from YANG variable /network_instances/network_instance/protocols/protocol/bgp/peer_groups/peer_group/timers/state/keepalive_interval (decimal64)\n If this variable is read-only (config: false) in the\n source YANG file, then _set_keepalive_interval is considered as a private\n method. Backends looking to populate this variable should\n do so via calling thisObj._set_keepalive_interval() directly.\n\n YANG Description: Time interval in seconds between transmission of keepalive\nmessages to the neighbor. Typically set to 1/3 the\nhold-time.\n \"\"\"\n if hasattr(v, \"_utype\"):\n v = v._utype(v)\n try:\n t = YANGDynClass(\n v,\n base=RestrictedPrecisionDecimalType(precision=2),\n default=Decimal(30),\n is_leaf=True,\n yang_name=\"keepalive-interval\",\n parent=self,\n path_helper=self._path_helper,\n extmethods=self._extmethods,\n register_paths=True,\n namespace=\"http://openconfig.net/yang/network-instance\",\n defining_module=\"openconfig-network-instance\",\n yang_type=\"decimal64\",\n is_config=False,\n )\n except (TypeError, ValueError):\n raise ValueError(\n {\n \"error-string\": \"\"\"keepalive_interval must be of a type compatible with decimal64\"\"\",\n \"defined-type\": \"decimal64\",\n \"generated-type\": \"\"\"YANGDynClass(base=RestrictedPrecisionDecimalType(precision=2), default=Decimal(30), is_leaf=True, yang_name=\"keepalive-interval\", parent=self, path_helper=self._path_helper, extmethods=self._extmethods, register_paths=True, namespace='http://openconfig.net/yang/network-instance', defining_module='openconfig-network-instance', yang_type='decimal64', is_config=False)\"\"\",\n }\n )\n\n self.__keepalive_interval = t\n if hasattr(self, \"_set\"):\n self._set()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nadding a model to the class.", "response": "def add_model(self, model, force=False):\n \"\"\"\n Add a model.\n\n The model will be asssigned to a class attribute with the YANG name of the model.\n\n Args:\n model (PybindBase): Model to add.\n force (bool): If not set, verify the model is in SUPPORTED_MODELS\n\n Examples:\n\n >>> import napalm_yang\n >>> config = napalm_yang.base.Root()\n >>> config.add_model(napalm_yang.models.openconfig_interfaces)\n >>> config.interfaces\n \n \"\"\"\n if isinstance(model, str):\n self._load_model(model)\n return\n\n try:\n model = model()\n except Exception:\n pass\n\n if model._yang_name not in [a[0] for a in SUPPORTED_MODELS] and not force:\n raise ValueError(\n \"Only models in SUPPORTED_MODELS can be added without `force=True`\"\n )\n\n for k, v in model:\n self._elements[k] = v\n setattr(self, k, v)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get(self, filter=False):\n result = {}\n\n for k, v in self.elements().items():\n intermediate = v.get(filter=filter)\n if intermediate:\n result[k] = intermediate\n\n return result", "response": "Returns a dictionary with the values of the YANG classes."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nloads a dictionary into the model.", "response": "def load_dict(self, data, overwrite=False, auto_load_model=True):\n \"\"\"\n Load a dictionary into the model.\n\n Args:\n data(dict): Dictionary to load\n overwrite(bool): Whether the data present in the model should be overwritten by the\n data in the dict or not.\n auto_load_model(bool): If set to true models will be loaded as they are needed\n\n Examples:\n\n >>> vlans_dict = {\n >>> \"vlans\": { \"vlan\": { 100: {\n >>> \"config\": {\n >>> \"vlan_id\": 100, \"name\": \"production\"}},\n >>> 200: {\n >>> \"config\": {\n >>> \"vlan_id\": 200, \"name\": \"dev\"}}}}}\n >>> config.load_dict(vlans_dict)\n >>> print(config.vlans.vlan.keys())\n ... [200, 100]\n >>> print(100, config.vlans.vlan[100].config.name)\n ... (100, u'production')\n >>> print(200, config.vlans.vlan[200].config.name)\n ... (200, u'dev')\n \"\"\"\n for k, v in data.items():\n if k not in self._elements.keys() and not auto_load_model:\n raise AttributeError(\"Model {} is not loaded\".format(k))\n\n elif k not in self._elements.keys() and auto_load_model:\n self._load_model(k)\n\n attr = getattr(self, k)\n _load_dict(attr, v)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a dictionary with the values of the model.", "response": "def to_dict(self, filter=True):\n \"\"\"\n Returns a dictionary with the values of the model. Note that the values\n of the leafs are evaluated to python types.\n\n Args:\n filter (bool): If set to ``True``, show only values that have been set.\n\n Returns:\n dict: A dictionary with the values of the model.\n\n Example:\n\n >>> pretty_print(config.to_dict(filter=True))\n >>> {\n >>> \"interfaces\": {\n >>> \"interface\": {\n >>> \"et1\": {\n >>> \"config\": {\n >>> \"description\": \"My description\",\n >>> \"mtu\": 1500\n >>> },\n >>> \"name\": \"et1\"\n >>> },\n >>> \"et2\": {\n >>> \"config\": {\n >>> \"description\": \"Another description\",\n >>> \"mtu\": 9000\n >>> },\n >>> \"name\": \"et2\"\n >>> }\n >>> }\n >>> }\n >>> }\n\n \"\"\"\n result = {}\n for k, v in self:\n r = _to_dict(v, filter)\n if r:\n result[k] = r\n return result"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef parse_config(self, device=None, profile=None, native=None, attrs=None):\n if attrs is None:\n attrs = self.elements().values()\n\n for v in attrs:\n parser = Parser(\n v, device=device, profile=profile, native=native, is_config=True\n )\n parser.parse()", "response": "Parse the configuration file into the corresponding models."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef parse_state(self, device=None, profile=None, native=None, attrs=None):\n if attrs is None:\n attrs = self.elements().values()\n\n for v in attrs:\n parser = Parser(\n v, device=device, profile=profile, native=native, is_config=False\n )\n parser.parse()", "response": "Parse the state of the junos system into the corresponding models."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef translate_config(self, profile, merge=None, replace=None):\n result = []\n for k, v in self:\n other_merge = getattr(merge, k) if merge else None\n other_replace = getattr(replace, k) if replace else None\n translator = Translator(\n v, profile, merge=other_merge, replace=other_replace\n )\n result.append(translator.translate())\n\n return \"\\n\".join(result)", "response": "Translate the object to native configuration."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef load_filters():\n all_filters = {}\n for m in JINJA_FILTERS:\n if hasattr(m, \"filters\"):\n all_filters.update(m.filters())\n return all_filters", "response": "Loads and returns all filters."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _parse_list_nested_recursive(\n cls, data, path, iterators, list_vars, cur_vars=None\n ):\n \"\"\"\n This helps parsing shit like:\n\n \n \n \n my_peers\n \n 192.168.100.2\n adsasd\n 65100\n \n \n 192.168.100.3\n 65100\n \n \n \n my_other_peers\n \n 172.20.0.1\n 65200\n \n \n \n \n\n \"\"\"\n cur_vars = dict(cur_vars) if cur_vars else {}\n if path:\n p = path[0]\n path = path[1:]\n else:\n for _ in data:\n list_vars.append(cur_vars)\n iterators.append(data)\n return data\n\n if p.startswith(\"?\"):\n for x in data:\n key, var_path = p.split(\".\")\n cur_vars.update({key.lstrip(\"?\"): x.xpath(var_path)[0].text})\n cls._parse_list_nested_recursive(\n x, path, iterators, list_vars, cur_vars\n )\n else:\n x = data.xpath(p)\n cls._parse_list_nested_recursive(x, path, iterators, list_vars, cur_vars)", "response": "Recursively parse the xml data for a nested list of objects."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _set_trunk_vlans(self, v, load=False):\n if hasattr(v, \"_utype\"):\n v = v._utype(v)\n try:\n t = YANGDynClass(\n v,\n base=TypedListType(\n allowed_type=[\n RestrictedClassType(\n base_type=RestrictedClassType(\n base_type=int,\n restriction_dict={\"range\": [\"0..65535\"]},\n int_size=16,\n ),\n restriction_dict={\"range\": [\"1..4094\"]},\n ),\n RestrictedClassType(\n base_type=six.text_type,\n restriction_dict={\n \"pattern\": \"(409[0-4]|40[0-8][0-9]|[1-3][0-9]{3}|[1-9][0-9]{1,2}|[1-9])\\\\.\\\\.(409[0-4]|40[0-8][0-9]|[1-3][0-9]{3}|[1-9][0-9]{1,2}|[1-9])\"\n },\n ),\n RestrictedClassType(\n base_type=six.text_type,\n restriction_dict={\n \"pattern\": \"(409[0-4]|40[0-8][0-9]|[1-3][0-9]{3}|[1-9][0-9]{1,2}|[1-9])\\\\.((409[0-4]|40[0-8][0-9]|[1-3][0-9]{3}|[1-9][0-9]{1,2}|[1-9])|\\\\*)\"\n },\n ),\n RestrictedClassType(\n base_type=six.text_type,\n restriction_dict={\n \"pattern\": \"(409[0-4]|40[0-8][0-9]|[1-3][0-9]{3}|[1-9][0-9]{1,2}|[1-9])\\\\.\\\\.(409[0-4]|40[0-8][0-9]|[1-3][0-9]{3}|[1-9][0-9]{1,2}|[1-9])\\\\.((409[0-4]|40[0-8][0-9]|[1-3][0-9]{3}|[1-9][0-9]{1,2}|[1-9])|\\\\*)\"\n },\n ),\n RestrictedClassType(\n base_type=six.text_type,\n restriction_dict={\n \"pattern\": \"(\\\\*|(409[0-4]|40[0-8][0-9]|[1-3][0-9]{3}|[1-9][0-9]{1,2}|[1-9]))\\\\.(409[0-4]|40[0-8][0-9]|[1-3][0-9]{3}|[1-9][0-9]{1,2}|[1-9])\\\\.\\\\.(409[0-4]|40[0-8][0-9]|[1-3][0-9]{3}|[1-9][0-9]{1,2}|[1-9])\"\n },\n ),\n ]\n ),\n is_leaf=False,\n yang_name=\"trunk-vlans\",\n parent=self,\n path_helper=self._path_helper,\n extmethods=self._extmethods,\n register_paths=True,\n namespace=\"http://openconfig.net/yang/vlan\",\n defining_module=\"openconfig-vlan\",\n yang_type=\"union\",\n is_config=False,\n )\n except (TypeError, ValueError):\n raise ValueError(\n {\n \"error-string\": \"\"\"trunk_vlans must be of a type compatible with union\"\"\",\n \"defined-type\": \"openconfig-vlan:union\",\n \"generated-type\": \"\"\"YANGDynClass(base=TypedListType(allowed_type=[RestrictedClassType(base_type=RestrictedClassType(base_type=int, restriction_dict={'range': ['0..65535']},int_size=16), restriction_dict={'range': ['1..4094']}),RestrictedClassType(base_type=six.text_type, restriction_dict={'pattern': '(409[0-4]|40[0-8][0-9]|[1-3][0-9]{3}|[1-9][0-9]{1,2}|[1-9])\\\\.\\\\.(409[0-4]|40[0-8][0-9]|[1-3][0-9]{3}|[1-9][0-9]{1,2}|[1-9])'}),RestrictedClassType(base_type=six.text_type, restriction_dict={'pattern': '(409[0-4]|40[0-8][0-9]|[1-3][0-9]{3}|[1-9][0-9]{1,2}|[1-9])\\\\.((409[0-4]|40[0-8][0-9]|[1-3][0-9]{3}|[1-9][0-9]{1,2}|[1-9])|\\\\*)'}),RestrictedClassType(base_type=six.text_type, restriction_dict={'pattern': '(409[0-4]|40[0-8][0-9]|[1-3][0-9]{3}|[1-9][0-9]{1,2}|[1-9])\\\\.\\\\.(409[0-4]|40[0-8][0-9]|[1-3][0-9]{3}|[1-9][0-9]{1,2}|[1-9])\\\\.((409[0-4]|40[0-8][0-9]|[1-3][0-9]{3}|[1-9][0-9]{1,2}|[1-9])|\\\\*)'}),RestrictedClassType(base_type=six.text_type, restriction_dict={'pattern': '(\\\\*|(409[0-4]|40[0-8][0-9]|[1-3][0-9]{3}|[1-9][0-9]{1,2}|[1-9]))\\\\.(409[0-4]|40[0-8][0-9]|[1-3][0-9]{3}|[1-9][0-9]{1,2}|[1-9])\\\\.\\\\.(409[0-4]|40[0-8][0-9]|[1-3][0-9]{3}|[1-9][0-9]{1,2}|[1-9])'}),]), is_leaf=False, yang_name=\"trunk-vlans\", parent=self, path_helper=self._path_helper, extmethods=self._extmethods, register_paths=True, namespace='http://openconfig.net/yang/vlan', defining_module='openconfig-vlan', yang_type='union', is_config=False)\"\"\",\n }\n )\n\n self.__trunk_vlans = t\n if hasattr(self, \"_set\"):\n self._set()", "response": "Sets the value of the trunk_vlans property of the base node."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nfind the necessary file for the given profile and filename.", "response": "def find_yang_file(profile, filename, path):\n \"\"\"\n Find the necessary file for the given test case.\n\n Args:\n device(napalm device connection): for which device\n filename(str): file to find\n path(str): where to find it relative to where the module is installed\n \"\"\"\n # Find base_dir of submodule\n module_dir = os.path.dirname(__file__)\n full_path = os.path.join(module_dir, \"mappings\", profile, path, filename)\n\n if os.path.exists(full_path):\n return full_path\n else:\n msg = \"Couldn't find parsing file: {}\".format(full_path)\n logger.error(msg)\n raise IOError(msg)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef model_to_dict(model, mode=\"\", show_defaults=False):\n\n def is_mode(obj, mode):\n if mode == \"\":\n return True\n elif mode == \"config\":\n return obj._yang_name == \"config\" or obj._is_config\n elif mode == \"state\":\n return obj._yang_name == \"state\" or not obj._is_config\n else:\n raise ValueError(\n \"mode can only be config, state or ''. Passed: {}\".format(mode)\n )\n\n def get_key(key, model, parent_defining_module, show_defaults):\n if not show_defaults:\n # No need to display rw/ro when showing the defaults.\n key = \"{} {}\".format(key, \"[rw]\" if model._is_config else \"[ro]\")\n if parent_defining_module != model._defining_module:\n key = \"{}:{}\".format(model._defining_module, key)\n return key\n\n if model._yang_type in (\"container\", \"list\"):\n cls = model if model._yang_type in (\"container\",) else model._contained_class()\n result = {}\n for k, v in cls:\n r = model_to_dict(v, mode=mode, show_defaults=show_defaults)\n if r:\n result[get_key(k, v, model._defining_module, show_defaults)] = r\n return result\n else:\n if show_defaults:\n if model._default is False:\n if model._yang_type != \"boolean\":\n # Unless the datatype is bool, when the _default attribute\n # is False, it means there is not default value defined in\n # the YANG model.\n return None\n return model._default\n return model._yang_type if is_mode(model, mode) else None", "response": "Given a model return a dictionary representation of the model."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the difference between two models.", "response": "def diff(f, s):\n \"\"\"\n Given two models, return the difference between them.\n\n Args:\n\n f (Pybindbase): First element.\n s (Pybindbase): Second element.\n\n Returns:\n\n dict: A dictionary highlighting the differences.\n\n Examples:\n\n >>> diff = napalm_yang.utils.diff(candidate, running)\n >>> pretty_print(diff)\n >>> {\n >>> \"interfaces\": {\n >>> \"interface\": {\n >>> \"both\": {\n >>> \"Port-Channel1\": {\n >>> \"config\": {\n >>> \"mtu\": {\n >>> \"first\": \"0\",\n >>> \"second\": \"9000\"\n >>> }\n >>> }\n >>> }\n >>> },\n >>> \"first_only\": [\n >>> \"Loopback0\"\n >>> ],\n >>> \"second_only\": [\n >>> \"Loopback1\"\n >>> ]\n >>> }\n >>> }\n >>> }\n \"\"\"\n if isinstance(f, base.Root) or f._yang_type in (\"container\", None):\n result = _diff_root(f, s)\n elif f._yang_type in (\"list\",):\n result = _diff_list(f, s)\n else:\n result = {}\n first = \"{}\".format(f)\n second = \"{}\".format(s)\n if first != second:\n result = {\"first\": first, \"second\": second}\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\npost to URL and get result as a response object.", "response": "def http_post(self, url, data=None):\n \"\"\"POST to URL and get result as a response object.\n\n :param url: URL to POST.\n :type url: str\n :param data: Data to send in the form body.\n :type data: str\n :rtype: requests.Response\n \"\"\"\n if not url.startswith('https://'):\n raise ValueError('Protocol must be HTTPS, invalid URL: %s' % url)\n return requests.post(url, data, verify=True)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_authorization_code_uri(self, **params):\n if 'response_type' not in params:\n params['response_type'] = self.default_response_type\n params.update({'client_id': self.client_id,\n 'redirect_uri': self.redirect_uri})\n return utils.build_url(self.authorization_uri, params)", "response": "Construct a full URL that can be used to obtain an authorization code from the provider authorization_uri."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngets an access token from the provider token URI.", "response": "def get_token(self, code, **params):\n \"\"\"Get an access token from the provider token URI.\n\n :param code: Authorization code.\n :type code: str\n :return: Dict containing access token, refresh token, etc.\n :rtype: dict\n \"\"\"\n params['code'] = code\n if 'grant_type' not in params:\n params['grant_type'] = self.default_grant_type\n params.update({'client_id': self.client_id,\n 'client_secret': self.client_secret,\n 'redirect_uri': self.redirect_uri})\n response = self.http_post(self.token_uri, params)\n try:\n return response.json()\n except TypeError:\n return response.json"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning the query parameters as a dict from the specified URL.", "response": "def url_query_params(url):\n \"\"\"Return query parameters as a dict from the specified URL.\n\n :param url: URL.\n :type url: str\n :rtype: dict\n \"\"\"\n return dict(urlparse.parse_qsl(urlparse.urlparse(url).query, True))"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef url_dequery(url):\n url = urlparse.urlparse(url)\n return urlparse.urlunparse((url.scheme,\n url.netloc,\n url.path,\n url.params,\n '',\n url.fragment))", "response": "Return a URL with the query component removed."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nconstructing a URL based off of base containing all parameters in the query portion of base plus any additional parameters.", "response": "def build_url(base, additional_params=None):\n \"\"\"Construct a URL based off of base containing all parameters in\n the query portion of base plus any additional parameters.\n\n :param base: Base URL\n :type base: str\n ::param additional_params: Additional query parameters to include.\n :type additional_params: dict\n :rtype: str\n \"\"\"\n url = urlparse.urlparse(base)\n query_params = {}\n query_params.update(urlparse.parse_qsl(url.query, True))\n if additional_params is not None:\n query_params.update(additional_params)\n for k, v in additional_params.iteritems():\n if v is None:\n query_params.pop(k)\n\n return urlparse.urlunparse((url.scheme,\n url.netloc,\n url.path,\n url.params,\n urllib.urlencode(query_params),\n url.fragment))"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nhandling an internal exception that was caught and suppressed.", "response": "def _handle_exception(self, exc):\n \"\"\"Handle an internal exception that was caught and suppressed.\n\n :param exc: Exception to process.\n :type exc: Exception\n \"\"\"\n logger = logging.getLogger(__name__)\n logger.exception(exc)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns a Response object from the given parameters.", "response": "def _make_response(self, body='', headers=None, status_code=200):\n \"\"\"Return a response object from the given parameters.\n\n :param body: Buffer/string containing the response body.\n :type body: str\n :param headers: Dict of headers to include in the requests.\n :type headers: dict\n :param status_code: HTTP status code.\n :type status_code: int\n :rtype: requests.Response\n \"\"\"\n res = Response()\n res.status_code = status_code\n if headers is not None:\n res.headers.update(headers)\n res.raw = StringIO(body)\n return res"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _make_redirect_error_response(self, redirect_uri, err):\n params = {\n 'error': err,\n 'response_type': None,\n 'client_id': None,\n 'redirect_uri': None\n }\n redirect = utils.build_url(redirect_uri, params)\n return self._make_response(headers={'Location': redirect},\n status_code=302)", "response": "Create a HTTP 302 redirect response object containing the error."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _make_json_response(self, data, headers=None, status_code=200):\n response_headers = {}\n if headers is not None:\n response_headers.update(headers)\n response_headers['Content-Type'] = 'application/json;charset=UTF-8'\n response_headers['Cache-Control'] = 'no-store'\n response_headers['Pragma'] = 'no-cache'\n return self._make_response(json.dumps(data),\n response_headers,\n status_code)", "response": "Return a response object from the given JSON data."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ngenerating and return an authorization code HTTP response.", "response": "def get_authorization_code(self,\n response_type,\n client_id,\n redirect_uri,\n **params):\n \"\"\"Generate authorization code HTTP response.\n\n :param response_type: Desired response type. Must be exactly \"code\".\n :type response_type: str\n :param client_id: Client ID.\n :type client_id: str\n :param redirect_uri: Client redirect URI.\n :type redirect_uri: str\n :rtype: requests.Response\n \"\"\"\n\n # Ensure proper response_type\n if response_type != 'code':\n err = 'unsupported_response_type'\n return self._make_redirect_error_response(redirect_uri, err)\n\n # Check redirect URI\n is_valid_redirect_uri = self.validate_redirect_uri(client_id,\n redirect_uri)\n if not is_valid_redirect_uri:\n return self._invalid_redirect_uri_response()\n\n # Check conditions\n is_valid_client_id = self.validate_client_id(client_id)\n is_valid_access = self.validate_access()\n scope = params.get('scope', '')\n is_valid_scope = self.validate_scope(client_id, scope)\n\n # Return proper error responses on invalid conditions\n if not is_valid_client_id:\n err = 'unauthorized_client'\n return self._make_redirect_error_response(redirect_uri, err)\n\n if not is_valid_access:\n err = 'access_denied'\n return self._make_redirect_error_response(redirect_uri, err)\n\n if not is_valid_scope:\n err = 'invalid_scope'\n return self._make_redirect_error_response(redirect_uri, err)\n\n # Generate authorization code\n code = self.generate_authorization_code()\n\n # Save information to be used to validate later requests\n self.persist_authorization_code(client_id=client_id,\n code=code,\n scope=scope)\n\n # Return redirection response\n params.update({\n 'code': code,\n 'response_type': None,\n 'client_id': None,\n 'redirect_uri': None\n })\n redirect = utils.build_url(redirect_uri, params)\n return self._make_response(headers={'Location': redirect},\n status_code=302)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef refresh_token(self,\n grant_type,\n client_id,\n client_secret,\n refresh_token,\n **params):\n \"\"\"Generate access token HTTP response from a refresh token.\n\n :param grant_type: Desired grant type. Must be \"refresh_token\".\n :type grant_type: str\n :param client_id: Client ID.\n :type client_id: str\n :param client_secret: Client secret.\n :type client_secret: str\n :param refresh_token: Refresh token.\n :type refresh_token: str\n :rtype: requests.Response\n \"\"\"\n\n # Ensure proper grant_type\n if grant_type != 'refresh_token':\n return self._make_json_error_response('unsupported_grant_type')\n\n # Check conditions\n is_valid_client_id = self.validate_client_id(client_id)\n is_valid_client_secret = self.validate_client_secret(client_id,\n client_secret)\n scope = params.get('scope', '')\n is_valid_scope = self.validate_scope(client_id, scope)\n data = self.from_refresh_token(client_id, refresh_token, scope)\n is_valid_refresh_token = data is not None\n\n # Return proper error responses on invalid conditions\n if not (is_valid_client_id and is_valid_client_secret):\n return self._make_json_error_response('invalid_client')\n\n if not is_valid_scope:\n return self._make_json_error_response('invalid_scope')\n\n if not is_valid_refresh_token:\n return self._make_json_error_response('invalid_grant')\n\n # Discard original refresh token\n self.discard_refresh_token(client_id, refresh_token)\n\n # Generate access tokens once all conditions have been met\n access_token = self.generate_access_token()\n token_type = self.token_type\n expires_in = self.token_expires_in\n refresh_token = self.generate_refresh_token()\n\n # Save information to be used to validate later requests\n self.persist_token_information(client_id=client_id,\n scope=scope,\n access_token=access_token,\n token_type=token_type,\n expires_in=expires_in,\n refresh_token=refresh_token,\n data=data)\n\n # Return json response\n return self._make_json_response({\n 'access_token': access_token,\n 'token_type': token_type,\n 'expires_in': expires_in,\n 'refresh_token': refresh_token\n })", "response": "Generate an access token HTTP response from a refresh token."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_token(self,\n grant_type,\n client_id,\n client_secret,\n redirect_uri,\n code,\n **params):\n \"\"\"Generate access token HTTP response.\n\n :param grant_type: Desired grant type. Must be \"authorization_code\".\n :type grant_type: str\n :param client_id: Client ID.\n :type client_id: str\n :param client_secret: Client secret.\n :type client_secret: str\n :param redirect_uri: Client redirect URI.\n :type redirect_uri: str\n :param code: Authorization code.\n :type code: str\n :rtype: requests.Response\n \"\"\"\n\n # Ensure proper grant_type\n if grant_type != 'authorization_code':\n return self._make_json_error_response('unsupported_grant_type')\n\n # Check conditions\n is_valid_client_id = self.validate_client_id(client_id)\n is_valid_client_secret = self.validate_client_secret(client_id,\n client_secret)\n is_valid_redirect_uri = self.validate_redirect_uri(client_id,\n redirect_uri)\n\n scope = params.get('scope', '')\n is_valid_scope = self.validate_scope(client_id, scope)\n data = self.from_authorization_code(client_id, code, scope)\n is_valid_grant = data is not None\n\n # Return proper error responses on invalid conditions\n if not (is_valid_client_id and is_valid_client_secret):\n return self._make_json_error_response('invalid_client')\n\n if not is_valid_grant or not is_valid_redirect_uri:\n return self._make_json_error_response('invalid_grant')\n\n if not is_valid_scope:\n return self._make_json_error_response('invalid_scope')\n\n # Discard original authorization code\n self.discard_authorization_code(client_id, code)\n\n # Generate access tokens once all conditions have been met\n access_token = self.generate_access_token()\n token_type = self.token_type\n expires_in = self.token_expires_in\n refresh_token = self.generate_refresh_token()\n\n # Save information to be used to validate later requests\n self.persist_token_information(client_id=client_id,\n scope=scope,\n access_token=access_token,\n token_type=token_type,\n expires_in=expires_in,\n refresh_token=refresh_token,\n data=data)\n\n # Return json response\n return self._make_json_response({\n 'access_token': access_token,\n 'token_type': token_type,\n 'expires_in': expires_in,\n 'refresh_token': refresh_token\n })", "response": "Generate access token HTTP response."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngetting authorization code response from a URI. This method will ignore the domain and path of the request, instead automatically parsing the query string parameters. :param uri: URI to parse for authorization information. :type uri: str :rtype: requests.Response", "response": "def get_authorization_code_from_uri(self, uri):\n \"\"\"Get authorization code response from a URI. This method will\n ignore the domain and path of the request, instead\n automatically parsing the query string parameters.\n\n :param uri: URI to parse for authorization information.\n :type uri: str\n :rtype: requests.Response\n \"\"\"\n params = utils.url_query_params(uri)\n try:\n if 'response_type' not in params:\n raise TypeError('Missing parameter response_type in URL query')\n\n if 'client_id' not in params:\n raise TypeError('Missing parameter client_id in URL query')\n\n if 'redirect_uri' not in params:\n raise TypeError('Missing parameter redirect_uri in URL query')\n\n return self.get_authorization_code(**params)\n except TypeError as exc:\n self._handle_exception(exc)\n\n # Catch missing parameters in request\n err = 'invalid_request'\n if 'redirect_uri' in params:\n u = params['redirect_uri']\n return self._make_redirect_error_response(u, err)\n else:\n return self._invalid_redirect_uri_response()\n except StandardError as exc:\n self._handle_exception(exc)\n\n # Catch all other server errors\n err = 'server_error'\n u = params['redirect_uri']\n return self._make_redirect_error_response(u, err)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_token_from_post_data(self, data):\n try:\n # Verify OAuth 2.0 Parameters\n for x in ['grant_type', 'client_id', 'client_secret']:\n if not data.get(x):\n raise TypeError(\"Missing required OAuth 2.0 POST param: {0}\".format(x))\n \n # Handle get token from refresh_token\n if 'refresh_token' in data:\n return self.refresh_token(**data)\n\n # Handle get token from authorization code\n for x in ['redirect_uri', 'code']:\n if not data.get(x):\n raise TypeError(\"Missing required OAuth 2.0 POST param: {0}\".format(x)) \n return self.get_token(**data)\n except TypeError as exc:\n self._handle_exception(exc)\n\n # Catch missing parameters in request\n return self._make_json_error_response('invalid_request')\n except StandardError as exc:\n self._handle_exception(exc)\n\n # Catch all other server errors\n return self._make_json_error_response('server_error')", "response": "Get a token from POST data."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget authorization object representing status of authentication.", "response": "def get_authorization(self):\n \"\"\"Get authorization object representing status of authentication.\"\"\"\n auth = self.authorization_class()\n header = self.get_authorization_header()\n if not header or not header.split:\n return auth\n header = header.split()\n if len(header) > 1 and header[0] == 'Bearer':\n auth.is_oauth = True\n access_token = header[1]\n self.validate_access_token(access_token, auth)\n if not auth.is_valid:\n auth.error = 'access_denied'\n return auth"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef open(self, bus):\n # Close the device if it's already open.\n if self._device is not None:\n self.close()\n # Try to open the file for the specified bus. Must turn off buffering\n # or else Python 3 fails (see: https://bugs.python.org/issue20074)\n self._device = open('/dev/i2c-{0}'.format(bus), 'r+b', buffering=0)", "response": "Open the smbus interface on the specified bus."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef read_byte(self, addr):\n assert self._device is not None, 'Bus must be opened before operations are made against it!'\n self._select_device(addr)\n return ord(self._device.read(1))", "response": "Read a single byte from the specified device."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef read_bytes(self, addr, number):\n assert self._device is not None, 'Bus must be opened before operations are made against it!'\n self._select_device(addr)\n return self._device.read(number)", "response": "Read many bytes from the specified device."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef read_byte_data(self, addr, cmd):\n assert self._device is not None, 'Bus must be opened before operations are made against it!'\n # Build ctypes values to marshall between ioctl and Python.\n reg = c_uint8(cmd)\n result = c_uint8()\n # Build ioctl request.\n request = make_i2c_rdwr_data([\n (addr, 0, 1, pointer(reg)), # Write cmd register.\n (addr, I2C_M_RD, 1, pointer(result)) # Read 1 byte as result.\n ])\n # Make ioctl call and return result data.\n ioctl(self._device.fileno(), I2C_RDWR, request)\n return result.value", "response": "Read a single byte from the specified cmd register of the device."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef read_word_data(self, addr, cmd):\n assert self._device is not None, 'Bus must be opened before operations are made against it!'\n # Build ctypes values to marshall between ioctl and Python.\n reg = c_uint8(cmd)\n result = c_uint16()\n # Build ioctl request.\n request = make_i2c_rdwr_data([\n (addr, 0, 1, pointer(reg)), # Write cmd register.\n (addr, I2C_M_RD, 2, cast(pointer(result), POINTER(c_uint8))) # Read word (2 bytes).\n ])\n # Make ioctl call and return result data.\n ioctl(self._device.fileno(), I2C_RDWR, request)\n return result.value", "response": "Read a word from the specified cmd register of the device."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nperforming a read from the specified cmd register of device.", "response": "def read_i2c_block_data(self, addr, cmd, length=32):\n \"\"\"Perform a read from the specified cmd register of device. Length number\n of bytes (default of 32) will be read and returned as a bytearray.\n \"\"\"\n assert self._device is not None, 'Bus must be opened before operations are made against it!'\n # Build ctypes values to marshall between ioctl and Python.\n reg = c_uint8(cmd)\n result = create_string_buffer(length)\n # Build ioctl request.\n request = make_i2c_rdwr_data([\n (addr, 0, 1, pointer(reg)), # Write cmd register.\n (addr, I2C_M_RD, length, cast(result, POINTER(c_uint8))) # Read data.\n ])\n # Make ioctl call and return result data.\n ioctl(self._device.fileno(), I2C_RDWR, request)\n return bytearray(result.raw)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nwrite a single byte to the specified device.", "response": "def write_quick(self, addr):\n \"\"\"Write a single byte to the specified device.\"\"\"\n # What a strange function, from the python-smbus source this appears to\n # just write a single byte that initiates a write to the specified device\n # address (but writes no data!). The functionality is duplicated below\n # but the actual use case for this is unknown.\n assert self._device is not None, 'Bus must be opened before operations are made against it!'\n # Build ioctl request.\n request = make_i2c_rdwr_data([\n (addr, 0, 0, None), # Write with no data.\n ])\n # Make ioctl call and return result data.\n ioctl(self._device.fileno(), I2C_RDWR, request)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nwrite a single byte to the specified device.", "response": "def write_byte(self, addr, val):\n \"\"\"Write a single byte to the specified device.\"\"\"\n assert self._device is not None, 'Bus must be opened before operations are made against it!'\n self._select_device(addr)\n data = bytearray(1)\n data[0] = val & 0xFF\n self._device.write(data)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nwriting many bytes to the specified device.", "response": "def write_bytes(self, addr, buf):\n \"\"\"Write many bytes to the specified device. buf is a bytearray\"\"\"\n assert self._device is not None, 'Bus must be opened before operations are made against it!'\n self._select_device(addr)\n self._device.write(buf)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef write_byte_data(self, addr, cmd, val):\n assert self._device is not None, 'Bus must be opened before operations are made against it!'\n # Construct a string of data to send with the command register and byte value.\n data = bytearray(2)\n data[0] = cmd & 0xFF\n data[1] = val & 0xFF\n # Send the data to the device.\n self._select_device(addr)\n self._device.write(data)", "response": "Write a byte to the specified command register of the device."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nwrite a 2 byte word of data to the specified cmd register of the specified device.", "response": "def write_word_data(self, addr, cmd, val):\n \"\"\"Write a word (2 bytes) of data to the specified cmd register of the\n device. Note that this will write the data in the endianness of the\n processor running Python (typically little endian)!\n \"\"\"\n assert self._device is not None, 'Bus must be opened before operations are made against it!'\n # Construct a string of data to send with the command register and word value.\n data = struct.pack('=BH', cmd & 0xFF, val & 0xFFFF)\n # Send the data to the device.\n self._select_device(addr)\n self._device.write(data)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nwrite a block of data to the specified cmd register.", "response": "def write_block_data(self, addr, cmd, vals):\n \"\"\"Write a block of data to the specified cmd register of the device.\n The amount of data to write should be the first byte inside the vals\n string/bytearray and that count of bytes of data to write should follow\n it.\n \"\"\"\n # Just use the I2C block data write to write the provided values and\n # their length as the first byte.\n data = bytearray(len(vals)+1)\n data[0] = len(vals) & 0xFF\n data[1:] = vals[0:]\n self.write_i2c_block_data(addr, cmd, data)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nwriting a buffer of data to the specified command register of the device.", "response": "def write_i2c_block_data(self, addr, cmd, vals):\n \"\"\"Write a buffer of data to the specified cmd register of the device.\n \"\"\"\n assert self._device is not None, 'Bus must be opened before operations are made against it!'\n # Construct a string of data to send, including room for the command register.\n data = bytearray(len(vals)+1)\n data[0] = cmd & 0xFF # Command register at the start.\n data[1:] = vals[0:] # Copy in the block data (ugly but necessary to ensure\n # the entire write happens in one transaction).\n # Send the data to the device.\n self._select_device(addr)\n self._device.write(data)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef process_call(self, addr, cmd, val):\n assert self._device is not None, 'Bus must be opened before operations are made against it!'\n # Build ctypes values to marshall between ioctl and Python.\n data = create_string_buffer(struct.pack('=BH', cmd, val))\n result = c_uint16()\n # Build ioctl request.\n request = make_i2c_rdwr_data([\n (addr, 0, 3, cast(pointer(data), POINTER(c_uint8))), # Write data.\n (addr, I2C_M_RD, 2, cast(pointer(result), POINTER(c_uint8))) # Read word (2 bytes).\n ])\n # Make ioctl call and return result data.\n ioctl(self._device.fileno(), I2C_RDWR, request)\n # Note the python-smbus code appears to have a rather serious bug and\n # does not return the result value! This is fixed below by returning it.\n return result.value", "response": "Perform a smbus process call by writing a word to the specified register of the device and then reading a word of response\n ."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef cdn_url(self):\n return '{cdn_base}{path}'.format(cdn_base=conf.cdn_base,\n path=self.cdn_path(self.default_effects))", "response": "Returns file s CDN url."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn file s store aware datetime in UTC format.", "response": "def datetime_stored(self):\n \"\"\"Returns file's store aware *datetime* in UTC format.\n\n It might do API request once because it depends on ``info()``.\n\n \"\"\"\n if self.info().get('datetime_stored'):\n return dateutil.parser.parse(self.info()['datetime_stored'])"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef datetime_removed(self):\n if self.info().get('datetime_removed'):\n return dateutil.parser.parse(self.info()['datetime_removed'])", "response": "Returns file s remove aware *datetime* in UTC format."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn file s upload aware *datetime* in UTC format.", "response": "def datetime_uploaded(self):\n \"\"\"Returns file's upload aware *datetime* in UTC format.\n\n It might do API request once because it depends on ``info()``.\n\n \"\"\"\n if self.info().get('datetime_uploaded'):\n return dateutil.parser.parse(self.info()['datetime_uploaded'])"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef copy(self, effects=None, target=None):\n warning = \"\"\"File.copy method is deprecated and will be\n removed in 4.0.0.\n Please use `create_local_copy`\n and `create_remote_copy` instead.\n \"\"\"\n logger.warn('API Warning: {0}'.format(warning))\n\n if target is not None:\n return self.create_remote_copy(target, effects)\n else:\n return self.create_local_copy(effects)", "response": "Creates a File Copy on Uploadcare or Custom Storage."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef create_local_copy(self, effects=None, store=None):\n effects = self._build_effects(effects)\n store = store or ''\n data = {\n 'source': self.cdn_path(effects)\n }\n if store:\n data['store'] = store\n return rest_request('POST', 'files/', data=data)", "response": "Creates a Local File Copy on Uploadcare Storage."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ncreates a remote copy of a file to a custom storage.", "response": "def create_remote_copy(self, target, effects=None, make_public=None,\n pattern=None):\n \"\"\"Creates file copy in remote storage.\n\n Args:\n - target:\n Name of a custom storage connected to the project.\n - effects:\n Adds CDN image effects to ``self.default_effects`` if any.\n - make_public:\n To forbid public from accessing your files on the storage set\n ``make_public`` option to be False.\n Default value is None. Files have public access by default.\n - pattern:\n Specify ``pattern`` option to set S3 object key name.\n Takes precedence over pattern set in project settings.\n If neither is specified defaults to\n `${uuid}/${filename}${effects}${ext}`.\n\n For more information on each of the options above please refer to\n REST API docs https://uploadcare.com/docs/api_reference/rest/accessing_files/.\n\n Following example copies a file to custom storage named ``samplefs``:\n\n >>> file = File('e8ebfe20-8c11-4a94-9b40-52ecad7d8d1a')\n >>> file.create_remote_copy(target='samplefs',\n >>> make_public=True,\n >>> pattern='${uuid}/${filename}${ext}')\n\n Now custom storage ``samplefs`` contains publicly available file\n with original filename billmurray.jpg in\n in the directory named ``e8ebfe20-8c11-4a94-9b40-52ecad7d8d1a``.\n\n \"\"\"\n effects = self._build_effects(effects)\n data = {\n 'source': self.cdn_path(effects),\n 'target': target\n }\n\n if make_public is not None:\n data['make_public'] = make_public\n if pattern is not None:\n data['pattern'] = pattern\n return rest_request('POST', 'files/', data=data)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nconstructing a new File instance from file info.", "response": "def construct_from(cls, file_info):\n \"\"\"Constructs ``File`` instance from file information.\n\n For example you have result of\n ``/files/1921953c-5d94-4e47-ba36-c2e1dd165e1a/`` API request::\n\n >>> file_info = {\n # ...\n 'uuid': '1921953c-5d94-4e47-ba36-c2e1dd165e1a',\n # ...\n }\n >>> File.construct_from(file_info)\n \n\n \"\"\"\n file_ = cls(file_info['uuid'])\n file_.default_effects = file_info.get('default_effects')\n file_._info_cache = file_info\n return file_"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nuploading a file to a specific node and returns a File instance.", "response": "def upload(cls, file_obj, store=None):\n \"\"\"Uploads a file and returns ``File`` instance.\n\n Args:\n - file_obj: file object to upload to\n - store (Optional[bool]): Should the file be automatically stored\n upon upload. Defaults to None.\n - False - do not store file\n - True - store file (can result in error if autostore\n is disabled for project)\n - None - use project settings\n\n Returns:\n ``File`` instance\n\n \"\"\"\n if store is None:\n store = 'auto'\n elif store:\n store = '1'\n else:\n store = '0'\n\n data = {\n 'UPLOADCARE_STORE': store,\n }\n\n files = uploading_request('POST', 'base/', data=data,\n files={'file': file_obj})\n file_ = cls(files['file'])\n return file_"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nuploading file from given url and returns FileFromUrl instance.", "response": "def upload_from_url(cls, url, store=None, filename=None):\n \"\"\"Uploads file from given url and returns ``FileFromUrl`` instance.\n\n Args:\n - url (str): URL of file to upload to\n - store (Optional[bool]): Should the file be automatically stored\n upon upload. Defaults to None.\n - False - do not store file\n - True - store file (can result in error if autostore\n is disabled for project)\n - None - use project settings\n - filename (Optional[str]): Name of the uploaded file. If this not\n specified the filename will be obtained from response headers\n or source URL. Defaults to None.\n\n Returns:\n ``FileFromUrl`` instance\n\n \"\"\"\n if store is None:\n store = 'auto'\n elif store:\n store = '1'\n else:\n store = '0'\n\n data = {\n 'source_url': url,\n 'store': store,\n }\n if filename:\n data['filename'] = filename\n\n result = uploading_request('POST', 'from_url/',\n data=data)\n if 'token' not in result:\n raise APIError(\n 'could not find token in result: {0}'.format(result)\n )\n file_from_url = cls.FileFromUrl(result['token'])\n return file_from_url"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef upload_from_url_sync(cls, url, timeout=30, interval=0.3,\n until_ready=False, store=None, filename=None):\n \"\"\"Uploads file from given url and returns ``File`` instance.\n\n Args:\n - url (str): URL of file to upload to\n - store (Optional[bool]): Should the file be automatically stored\n upon upload. Defaults to None.\n - False - do not store file\n - True - store file (can result in error if autostore\n is disabled for project)\n - None - use project settings\n - filename (Optional[str]): Name of the uploaded file. If this not\n specified the filename will be obtained from response headers\n or source URL. Defaults to None.\n - timeout (Optional[int]): seconds to wait for successful upload.\n Defaults to 30.\n - interval (Optional[float]): interval between upload status checks.\n Defaults to 0.3.\n - until_ready (Optional[bool]): should we wait until file is\n available via CDN. Defaults to False.\n\n Returns:\n ``File`` instance\n\n Raises:\n ``TimeoutError`` if file wasn't uploaded in time\n\n \"\"\"\n ffu = cls.upload_from_url(url, store, filename)\n return ffu.wait(timeout=timeout, interval=interval,\n until_ready=until_ready)", "response": "Uploads file from given url and returns File instance."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef file_cdn_urls(self):\n file_cdn_urls = []\n for file_index in six.moves.xrange(len(self)):\n file_cdn_url = '{group_cdn_url}nth/{file_index}/'.format(\n group_cdn_url=self.cdn_url,\n file_index=file_index\n )\n file_cdn_urls.append(file_cdn_url)\n return file_cdn_urls", "response": "Returns a list of all files from group without API requesting."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn file group s create aware *datetime* in UTC format.", "response": "def datetime_created(self):\n \"\"\"Returns file group's create aware *datetime* in UTC format.\"\"\"\n if self.info().get('datetime_created'):\n return dateutil.parser.parse(self.info()['datetime_created'])"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nconstructs a FileGroup instance from group information.", "response": "def construct_from(cls, group_info):\n \"\"\"Constructs ``FileGroup`` instance from group information.\"\"\"\n group = cls(group_info['id'])\n group._info_cache = group_info\n return group"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef create(cls, files):\n data = {}\n for index, file_ in enumerate(files):\n if isinstance(file_, File):\n file_index = 'files[{index}]'.format(index=index)\n data[file_index] = six.text_type(file_)\n else:\n raise InvalidParamError(\n 'all items have to be ``File`` instance'\n )\n if not data:\n raise InvalidParamError('set of files is empty')\n\n group_info = uploading_request('POST', 'group/', data=data)\n\n group = cls.construct_from(group_info)\n return group", "response": "Creates a new file group and returns it."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _base_opration(self, method):\n uuids = self.uuids()\n\n while True:\n chunk = list(islice(uuids, 0, self.chunk_size))\n\n if not chunk:\n return\n\n rest_request(method, self.storage_url, chunk)", "response": "Base method for storage operations."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nextracts uuid from each item of specified sequence.", "response": "def uuids(self):\n \"\"\" Extract uuid from each item of specified ``seq``.\n \"\"\"\n for f in self._seq:\n if isinstance(f, File):\n yield f.uuid\n elif isinstance(f, six.string_types):\n yield f\n else:\n raise ValueError(\n 'Invalid type for sequence item: {0}'.format(type(f)))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _list(api_list_class, arg_namespace, **extra):\n if arg_namespace.starting_point:\n ordering_field = (arg_namespace.ordering or '').lstrip('-')\n if ordering_field in ('', 'datetime_uploaded', 'datetime_created'):\n arg_namespace.starting_point = parser.parse(\n arg_namespace.starting_point)\n\n items = api_list_class(\n starting_point=arg_namespace.starting_point,\n ordering=arg_namespace.ordering,\n limit=arg_namespace.limit,\n request_limit=arg_namespace.request_limit,\n **extra\n )\n items.constructor = lambda x: x\n\n try:\n pprint(list(items))\n except ValueError as e:\n print(e)", "response": "A common function for building methods of the list showing."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef bar(iter_content, parts, title=''):\n parts = max(float(parts), 1.0)\n cells = 10\n progress = 0\n step = cells / parts\n\n draw = lambda progress: sys.stdout.write(\n '\\r[{0:10}] {1:.2f}% {2}'.format(\n '#'*int(progress), progress * cells, title))\n\n for chunk in iter_content:\n yield chunk\n\n progress += step\n draw(progress)\n sys.stdout.flush()\n\n draw(cells)\n print('')", "response": "Draw a progress bar for each element in iter_content."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef rest_request(verb, path, data=None, timeout=conf.DEFAULT,\n retry_throttled=conf.DEFAULT):\n \"\"\"Makes REST API request and returns response as ``dict``.\n\n It provides auth headers as well and takes settings from ``conf`` module.\n\n Make sure that given ``path`` does not contain leading slash.\n\n Usage example::\n\n >>> rest_request('GET', 'files/?limit=10')\n {\n 'next': 'https://api.uploadcare.com/files/?limit=10&page=2',\n 'total': 1241,\n 'page': 1,\n 'pages': 125,\n 'per_page': 10,\n 'previous': None,\n 'results': [\n # ...\n {\n # ...\n 'uuid': 1921953c-5d94-4e47-ba36-c2e1dd165e1a,\n # ...\n },\n # ...\n ]\n }\n\n \"\"\"\n if retry_throttled is conf.DEFAULT:\n retry_throttled = conf.retry_throttled\n\n path = path.lstrip('/')\n url = urljoin(conf.api_base, path)\n url_parts = urlsplit(url)\n\n if url_parts.query:\n path = url_parts.path + '?' + url_parts.query\n else:\n path = url_parts.path\n\n content = ''\n if data is not None:\n content = json.dumps(data)\n\n content_type = 'application/json'\n content_md5 = hashlib.md5(content.encode('utf-8')).hexdigest()\n\n def _request():\n date = email.utils.formatdate(usegmt=True)\n sign_string = '\\n'.join([\n verb,\n content_md5,\n content_type,\n date,\n path,\n ])\n sign_string_as_bytes = sign_string.encode('utf-8')\n\n try:\n secret_as_bytes = conf.secret.encode('utf-8')\n except AttributeError:\n secret_as_bytes = bytes()\n sign = hmac.new(secret_as_bytes, sign_string_as_bytes, hashlib.sha1) \\\n .hexdigest()\n\n headers = {\n 'Authorization': 'Uploadcare {0}:{1}'.format(conf.pub_key, sign),\n 'Date': date,\n 'Content-Type': content_type,\n 'Accept': 'application/vnd.uploadcare-v{0}+json'.format(\n conf.api_version),\n 'User-Agent': _build_user_agent(),\n }\n logger.debug('''sent:\n verb: {0}\n path: {1}\n headers: {2}\n data: {3}'''.format(verb, path, headers, content))\n\n try:\n response = session.request(verb, url, allow_redirects=True,\n verify=conf.verify_api_ssl,\n headers=headers, data=content,\n timeout=_get_timeout(timeout))\n except requests.RequestException as exc:\n raise APIConnectionError(exc.args[0])\n\n logger.debug(\n 'got: {0} {1}'.format(response.status_code, response.text)\n )\n\n if 'warning' in response.headers:\n match = re.search('\"(.+)\"', response.headers['warning'])\n if match:\n for warning in match.group(1).split('; '):\n logger.warn('API Warning: {0}'.format(warning))\n\n # No content.\n if response.status_code == 204:\n return {}\n if verb.lower() == 'options':\n return ''\n\n if 200 <= response.status_code < 300:\n if _content_type_from_response(response).endswith(('/json', '+json')):\n if verb.lower() == 'head':\n return ''\n try:\n return response.json()\n except ValueError as exc:\n raise APIError(exc.args[0])\n\n if response.status_code in (401, 403):\n raise AuthenticationError(response.content)\n\n if response.status_code in (400, 404):\n raise InvalidRequestError(response.content)\n\n if response.status_code == 429:\n raise ThrottledRequestError(response)\n\n # Not json or unknown status code.\n raise APIError(response.content)\n\n while True:\n try:\n return _request()\n except ThrottledRequestError as e:\n if retry_throttled:\n logger.debug('Throttled, retry in {0} seconds'.format(e.wait))\n time.sleep(e.wait)\n retry_throttled -= 1\n continue\n else:\n raise", "response": "Makes REST API request and returns response as dict."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nmake a HTTP request and returns response as dict.", "response": "def uploading_request(verb, path, data=None, files=None, timeout=conf.DEFAULT):\n \"\"\"Makes Uploading API request and returns response as ``dict``.\n\n It takes settings from ``conf`` module.\n\n Make sure that given ``path`` does not contain leading slash.\n\n Usage example::\n\n >>> file_obj = open('photo.jpg', 'rb')\n >>> uploading_request('POST', 'base/', files={'file': file_obj})\n {\n 'file': '9b9f4483-77b8-40ae-a198-272ba6280004'\n }\n >>> File('9b9f4483-77b8-40ae-a198-272ba6280004')\n\n \"\"\"\n path = path.lstrip('/')\n url = urljoin(conf.upload_base, path)\n\n if data is None:\n data = {}\n data['pub_key'] = conf.pub_key\n data['UPLOADCARE_PUB_KEY'] = conf.pub_key\n\n headers = {\n 'User-Agent': _build_user_agent(),\n }\n\n try:\n response = session.request(\n str(verb), url, allow_redirects=True,\n verify=conf.verify_upload_ssl, data=data, files=files,\n headers=headers, timeout=_get_timeout(timeout),\n )\n except requests.RequestException as exc:\n raise APIConnectionError(exc.args[0])\n\n # No content.\n if response.status_code == 204:\n return {}\n\n if 200 <= response.status_code < 300:\n if _content_type_from_response(response).endswith(('/json', '+json')):\n try:\n return response.json()\n except ValueError as exc:\n raise APIError(exc.args[0])\n\n if response.status_code in (400, 404):\n raise InvalidRequestError(response.content)\n\n # Not json or unknown status code.\n raise APIError(response.content)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nsetting the state of Home Mode", "response": "def home_mode_set_state(self, state, **kwargs):\n \"\"\"Set the state of Home Mode\"\"\"\n\n # It appears that surveillance station needs lowercase text\n # true/false for the on switch\n if state not in (HOME_MODE_ON, HOME_MODE_OFF):\n raise ValueError('Invalid home mode state')\n\n api = self._api_info['home_mode']\n payload = dict({\n 'api': api['name'],\n 'method': 'Switch',\n 'version': api['version'],\n 'on': state,\n '_sid': self._sid,\n }, **kwargs)\n response = self._get_json_with_retry(api['url'], payload)\n\n if response['success']:\n return True\n\n return False"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef home_mode_status(self, **kwargs):\n api = self._api_info['home_mode']\n payload = dict({\n 'api': api['name'],\n 'method': 'GetInfo',\n 'version': api['version'],\n '_sid': self._sid\n }, **kwargs)\n response = self._get_json_with_retry(api['url'], payload)\n\n return response['data']['on']", "response": "Returns the status of Home Mode"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a list of cameras.", "response": "def camera_list(self, **kwargs):\n \"\"\"Return a list of cameras.\"\"\"\n api = self._api_info['camera']\n payload = dict({\n '_sid': self._sid,\n 'api': api['name'],\n 'method': 'List',\n 'version': api['version'],\n }, **kwargs)\n response = self._get_json_with_retry(api['url'], payload)\n\n cameras = []\n\n for data in response['data']['cameras']:\n cameras.append(Camera(data, self._video_stream_url))\n\n return cameras"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef camera_info(self, camera_ids, **kwargs):\n api = self._api_info['camera']\n payload = dict({\n '_sid': self._sid,\n 'api': api['name'],\n 'method': 'GetInfo',\n 'version': api['version'],\n 'cameraIds': ', '.join(str(id) for id in camera_ids),\n }, **kwargs)\n response = self._get_json_with_retry(api['url'], payload)\n\n cameras = []\n\n for data in response['data']['cameras']:\n cameras.append(Camera(data, self._video_stream_url))\n\n return cameras", "response": "Return a list of cameras matching camera_ids."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn bytes of camera image.", "response": "def camera_snapshot(self, camera_id, **kwargs):\n \"\"\"Return bytes of camera image.\"\"\"\n api = self._api_info['camera']\n payload = dict({\n '_sid': self._sid,\n 'api': api['name'],\n 'method': 'GetSnapshot',\n 'version': api['version'],\n 'cameraId': camera_id,\n }, **kwargs)\n response = self._get(api['url'], payload)\n\n return response.content"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef camera_event_motion_enum(self, camera_id, **kwargs):\n api = self._api_info['camera_event']\n payload = dict({\n '_sid': self._sid,\n 'api': api['name'],\n 'method': 'MotionEnum',\n 'version': api['version'],\n 'camId': camera_id,\n }, **kwargs)\n response = self._get_json_with_retry(api['url'], payload)\n\n return MotionSetting(camera_id, response['data']['MDParam'])", "response": "Return motion settings matching camera_id."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nupdate motion settings matching camera_id with keyword args.", "response": "def camera_event_md_param_save(self, camera_id, **kwargs):\n \"\"\"Update motion settings matching camera_id with keyword args.\"\"\"\n api = self._api_info['camera_event']\n payload = dict({\n '_sid': self._sid,\n 'api': api['name'],\n 'method': 'MDParamSave',\n 'version': api['version'],\n 'camId': camera_id,\n }, **kwargs)\n response = self._get_json_with_retry(api['url'], payload)\n\n return response['data']['camId']"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef update(self):\n cameras = self._api.camera_list()\n self._cameras_by_id = {v.camera_id: v for i, v in enumerate(cameras)}\n\n motion_settings = []\n for camera_id in self._cameras_by_id.keys():\n motion_setting = self._api.camera_event_motion_enum(camera_id)\n motion_settings.append(motion_setting)\n\n self._motion_settings_by_id = {\n v.camera_id: v for i, v in enumerate(motion_settings)}", "response": "Update cameras and motion settings with latest from API."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nsetting the state of Home Mode", "response": "def set_home_mode(self, state):\n \"\"\"Set the state of Home Mode\"\"\"\n state_parameter = HOME_MODE_OFF\n if state:\n state_parameter = HOME_MODE_ON\n return self._api.home_mode_set_state(state_parameter)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreplaces the extension of a file path with a new one.", "response": "def replace_ext(file_path, new_ext):\n \"\"\"\n >>> replace_ext('one/two/three.four.doc', '.html')\n 'one/two/three.four.html'\n >>> replace_ext('one/two/three.four.DOC', '.html')\n 'one/two/three.four.html'\n >>> replace_ext('one/two/three.four.DOC', 'html')\n 'one/two/three.four.html'\n \"\"\"\n if not new_ext.startswith(os.extsep):\n new_ext = os.extsep + new_ext\n index = file_path.rfind(os.extsep)\n return file_path[:index] + new_ext"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ndetermining if a list item is the last list item for a given list item.", "response": "def is_last_li(li, meta_data, current_numId):\n \"\"\"\n Determine if ``li`` is the last list item for a given list\n \"\"\"\n if not is_li(li, meta_data):\n return False\n w_namespace = get_namespace(li, 'w')\n next_el = li\n while True:\n # If we run out of element this must be the last list item\n if next_el is None:\n return True\n\n next_el = next_el.getnext()\n # Ignore elements that are not a list item\n if not is_li(next_el, meta_data):\n continue\n\n new_numId = get_numId(next_el, w_namespace)\n if current_numId != new_numId:\n return True\n # If we have gotten here then we have found another list item in the\n # current list, so ``li`` is not the last li in the list.\n return False"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nyielding all the data that is in a single list node.", "response": "def get_single_list_nodes_data(li, meta_data):\n \"\"\"\n Find consecutive li tags that have content that have the same list id.\n \"\"\"\n yield li\n w_namespace = get_namespace(li, 'w')\n current_numId = get_numId(li, w_namespace)\n starting_ilvl = get_ilvl(li, w_namespace)\n el = li\n while True:\n el = el.getnext()\n if el is None:\n break\n # If the tag has no content ignore it.\n if not has_text(el):\n continue\n\n # Stop the lists if you come across a list item that should be a\n # heading.\n if _is_top_level_upper_roman(el, meta_data):\n break\n\n if (\n is_li(el, meta_data) and\n (starting_ilvl > get_ilvl(el, w_namespace))):\n break\n\n new_numId = get_numId(el, w_namespace)\n if new_numId is None or new_numId == -1:\n # Not a p tag or a list item\n yield el\n continue\n # If the list id of the next tag is different that the previous that\n # means a new list being made (not nested)\n if current_numId != new_numId:\n # Not a subsequent list.\n break\n if is_last_li(el, meta_data, current_numId):\n yield el\n break\n yield el"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the ilvl of a li tag in the given namespace", "response": "def get_ilvl(li, w_namespace):\n \"\"\"\n The ilvl on an li tag tells the li tag at what level of indentation this\n tag is at. This is used to determine if the li tag needs to be nested or\n not.\n \"\"\"\n ilvls = li.xpath('.//w:ilvl', namespaces=li.nsmap)\n if len(ilvls) == 0:\n return -1\n return int(ilvls[0].get('%sval' % w_namespace))"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ngetting the numId of a list item.", "response": "def get_numId(li, w_namespace):\n \"\"\"\n The numId on an li tag maps to the numbering dictionary along side the ilvl\n to determine what the list should look like (unordered, digits, lower\n alpha, etc)\n \"\"\"\n numIds = li.xpath('.//w:numId', namespaces=li.nsmap)\n if len(numIds) == 0:\n return -1\n return numIds[0].get('%sval' % w_namespace)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef create_list(list_type):\n list_types = {\n 'bullet': 'ul',\n }\n el = etree.Element(list_types.get(list_type, 'ol'))\n # These are the supported list style types and their conversion to css.\n list_type_conversions = {\n 'decimal': DEFAULT_LIST_NUMBERING_STYLE,\n 'decimalZero': 'decimal-leading-zero',\n 'upperRoman': 'upper-roman',\n 'lowerRoman': 'lower-roman',\n 'upperLetter': 'upper-alpha',\n 'lowerLetter': 'lower-alpha',\n 'ordinal': DEFAULT_LIST_NUMBERING_STYLE,\n 'cardinalText': DEFAULT_LIST_NUMBERING_STYLE,\n 'ordinalText': DEFAULT_LIST_NUMBERING_STYLE,\n }\n if list_type != 'bullet':\n el.set(\n 'data-list-type',\n list_type_conversions.get(list_type, DEFAULT_LIST_NUMBERING_STYLE),\n )\n return el", "response": "Create a list object based on the passed in list_type."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngets the vMerge element of the tc element.", "response": "def get_v_merge(tc):\n \"\"\"\n vMerge is what docx uses to denote that a table cell is part of a rowspan.\n The first cell to have a vMerge is the start of the rowspan, and the vMerge\n will be denoted with 'restart'. If it is anything other than restart then\n it is a continuation of another rowspan.\n \"\"\"\n if tc is None:\n return None\n v_merges = tc.xpath('.//w:vMerge', namespaces=tc.nsmap)\n if len(v_merges) != 1:\n return None\n v_merge = v_merges[0]\n return v_merge"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_grid_span(tc):\n w_namespace = get_namespace(tc, 'w')\n grid_spans = tc.xpath('.//w:gridSpan', namespaces=tc.nsmap)\n if len(grid_spans) != 1:\n return 1\n grid_span = grid_spans[0]\n return int(grid_span.get('%sval' % w_namespace))", "response": "Get the number of rows in a table cell."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns True if the style is not set to false.", "response": "def style_is_false(style):\n \"\"\"\n For bold, italics and underline. Simply checking to see if the various tags\n are present will not suffice. If the tag is present and set to False then\n the style should not be present.\n \"\"\"\n if style is None:\n return False\n w_namespace = get_namespace(style, 'w')\n return style.get('%sval' % w_namespace) != 'false'"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning True if the tag passed in is considered bold.", "response": "def is_bold(r):\n \"\"\"\n The function will return True if the r tag passed in is considered bold.\n \"\"\"\n w_namespace = get_namespace(r, 'w')\n rpr = r.find('%srPr' % w_namespace)\n if rpr is None:\n return False\n bold = rpr.find('%sb' % w_namespace)\n return style_is_false(bold)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef is_italics(r):\n w_namespace = get_namespace(r, 'w')\n rpr = r.find('%srPr' % w_namespace)\n if rpr is None:\n return False\n italics = rpr.find('%si' % w_namespace)\n return style_is_false(italics)", "response": "Returns True if the tag passed in is considered\n italicized."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef is_underlined(r):\n w_namespace = get_namespace(r, 'w')\n rpr = r.find('%srPr' % w_namespace)\n if rpr is None:\n return False\n underline = rpr.find('%su' % w_namespace)\n return style_is_false(underline)", "response": "Returns True if the tag passed in is considered\n underlined."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn True if the passed in p tag is considered a title.", "response": "def is_title(p):\n \"\"\"\n Certain p tags are denoted as ``Title`` tags. This function will return\n True if the passed in p tag is considered a title.\n \"\"\"\n w_namespace = get_namespace(p, 'w')\n styles = p.xpath('.//w:pStyle', namespaces=p.nsmap)\n if len(styles) == 0:\n return False\n style = styles[0]\n return style.get('%sval' % w_namespace) == 'Title'"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nget the text run content data.", "response": "def get_text_run_content_data(r):\n \"\"\"\n It turns out that r tags can contain both t tags and drawing tags. Since we\n need both, this function will return them in the order in which they are\n found.\n \"\"\"\n w_namespace = get_namespace(r, 'w')\n valid_elements = (\n '%st' % w_namespace,\n '%sdrawing' % w_namespace,\n '%spict' % w_namespace,\n '%sbr' % w_namespace,\n )\n for el in r:\n if el.tag in valid_elements:\n yield el"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncheck to see if the whole line is bold or italics. Returns a tuple of boolean 1 if the whole line is bold False otherwise.", "response": "def whole_line_styled(p):\n \"\"\"\n Checks to see if the whole p tag will end up being bold or italics. Returns\n a tuple (boolean, boolean). The first boolean will be True if the whole\n line is bold, False otherwise. The second boolean will be True if the whole\n line is italics, False otherwise.\n \"\"\"\n r_tags = p.xpath('.//w:r', namespaces=p.nsmap)\n tags_are_bold = [\n is_bold(r) or is_underlined(r) for r in r_tags\n ]\n tags_are_italics = [\n is_italics(r) for r in r_tags\n ]\n return all(tags_are_bold), all(tags_are_italics)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_numbering_info(tree):\n if tree is None:\n return {}\n w_namespace = get_namespace(tree, 'w')\n num_ids = {}\n result = defaultdict(dict)\n # First find all the list types\n for list_type in tree.findall('%snum' % w_namespace):\n list_id = list_type.get('%snumId' % w_namespace)\n\n # Each list type is assigned an abstractNumber that defines how lists\n # should look.\n abstract_number = list_type.find('%sabstractNumId' % w_namespace)\n num_ids[abstract_number.get('%sval' % w_namespace)] = list_id\n\n # Loop through all the abstractNumbers\n for abstract_number in tree.findall('%sabstractNum' % w_namespace):\n abstract_num_id = abstract_number.get('%sabstractNumId' % w_namespace)\n # If we find an abstractNumber that is not being used in the document\n # then ignore it.\n if abstract_num_id not in num_ids:\n continue\n\n # Get the level of the abstract number.\n for lvl in abstract_number.findall('%slvl' % w_namespace):\n ilvl = int(lvl.get('%silvl' % w_namespace))\n lvl_format = lvl.find('%snumFmt' % w_namespace)\n list_style = lvl_format.get('%sval' % w_namespace)\n # Based on the list type and the ilvl (indentation level) store the\n # needed style.\n result[num_ids[abstract_num_id]][ilvl] = list_style\n return result", "response": "Get the numbering info for a tree."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_style_dict(tree):\n # This is a partial document and actual h1 is the document title, which\n # will be displayed elsewhere.\n headers = {\n 'heading 1': 'h2',\n 'heading 2': 'h3',\n 'heading 3': 'h4',\n 'heading 4': 'h5',\n 'heading 5': 'h6',\n 'heading 6': 'h6',\n 'heading 7': 'h6',\n 'heading 8': 'h6',\n 'heading 9': 'h6',\n 'heading 10': 'h6',\n }\n if tree is None:\n return {}\n w_namespace = get_namespace(tree, 'w')\n result = {}\n for el in tree:\n style_id = el.get('%sstyleId' % w_namespace)\n el_result = {\n 'header': False,\n 'font_size': None,\n 'based_on': None,\n }\n # Get the header info\n name = el.find('%sname' % w_namespace)\n if name is None:\n continue\n value = name.get('%sval' % w_namespace).lower()\n if value in headers:\n el_result['header'] = headers[value]\n\n # Get the size info.\n rpr = el.find('%srPr' % w_namespace)\n if rpr is None:\n continue\n size = rpr.find('%ssz' % w_namespace)\n if size is None:\n el_result['font_size'] = None\n else:\n el_result['font_size'] = size.get('%sval' % w_namespace)\n\n # Get based on info.\n based_on = el.find('%sbasedOn' % w_namespace)\n if based_on is None:\n el_result['based_on'] = None\n else:\n el_result['based_on'] = based_on.get('%sval' % w_namespace)\n result[style_id] = el_result\n return result", "response": "Get the style of the tree."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_relationship_info(tree, media, image_sizes):\n if tree is None:\n return {}\n result = {}\n # Loop through each relationship.\n for el in tree.iter():\n el_id = el.get('Id')\n if el_id is None:\n continue\n # Store the target in the result dict.\n target = el.get('Target')\n if any(\n target.lower().endswith(ext) for\n ext in IMAGE_EXTENSIONS_TO_SKIP):\n continue\n if target in media:\n image_size = image_sizes.get(el_id)\n target = convert_image(media[target], image_size)\n # cgi will replace things like & < > with & < >\n result[el_id] = cgi.escape(target)\n\n return result", "response": "Get the relationship info for a tree."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _get_document_data(f, image_handler=None):\n '''\n ``f`` is a ``ZipFile`` that is open\n Extract out the document data, numbering data and the relationship data.\n '''\n if image_handler is None:\n def image_handler(image_id, relationship_dict):\n return relationship_dict.get(image_id)\n\n document_xml = None\n numbering_xml = None\n relationship_xml = None\n styles_xml = None\n parser = etree.XMLParser(strip_cdata=False)\n path, _ = os.path.split(f.filename)\n media = {}\n image_sizes = {}\n # Loop through the files in the zip file.\n for item in f.infolist():\n # This file holds all the content of the document.\n if item.filename == 'word/document.xml':\n xml = f.read(item.filename)\n document_xml = etree.fromstring(xml, parser)\n # This file tells document.xml how lists should look.\n elif item.filename == 'word/numbering.xml':\n xml = f.read(item.filename)\n numbering_xml = etree.fromstring(xml, parser)\n elif item.filename == 'word/styles.xml':\n xml = f.read(item.filename)\n styles_xml = etree.fromstring(xml, parser)\n # This file holds the targets for hyperlinks and images.\n elif item.filename == 'word/_rels/document.xml.rels':\n xml = f.read(item.filename)\n try:\n relationship_xml = etree.fromstring(xml, parser)\n except XMLSyntaxError:\n relationship_xml = etree.fromstring('', parser)\n if item.filename.startswith('word/media/'):\n # Strip off the leading word/\n media[item.filename[len('word/'):]] = f.extract(\n item.filename,\n path,\n )\n # Close the file pointer.\n f.close()\n\n # Get dictionaries for the numbering and the relationships.\n numbering_dict = get_numbering_info(numbering_xml)\n image_sizes = get_image_sizes(document_xml)\n relationship_dict = get_relationship_info(\n relationship_xml,\n media,\n image_sizes\n )\n styles_dict = get_style_dict(styles_xml)\n font_sizes_dict = defaultdict(int)\n if DETECT_FONT_SIZE:\n font_sizes_dict = get_font_sizes_dict(document_xml, styles_dict)\n meta_data = MetaData(\n numbering_dict=numbering_dict,\n relationship_dict=relationship_dict,\n styles_dict=styles_dict,\n font_sizes_dict=font_sizes_dict,\n image_handler=image_handler,\n image_sizes=image_sizes,\n )\n return document_xml, meta_data", "response": "Extract out the document data numbering data and relationship data."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_ordered_list_type(meta_data, numId, ilvl):\n\n # Early return if numId or ilvl are not valid\n numbering_dict = meta_data.numbering_dict\n if numId not in numbering_dict:\n return DEFAULT_LIST_NUMBERING_STYLE\n if ilvl not in numbering_dict[numId]:\n return DEFAULT_LIST_NUMBERING_STYLE\n return meta_data.numbering_dict[numId][ilvl]", "response": "Return the list type for the given numId and ilvl"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nbuilding the list structure and returns the root list.", "response": "def build_list(li_nodes, meta_data):\n \"\"\"\n Build the list structure and return the root list\n \"\"\"\n # Need to keep track of all incomplete nested lists.\n ol_dict = {}\n\n # Need to keep track of the current indentation level.\n current_ilvl = -1\n\n # Need to keep track of the current list id.\n current_numId = -1\n\n # Need to keep track of list that new li tags should be added too.\n current_ol = None\n\n # Store the first list created (the root list) for the return value.\n root_ol = None\n visited_nodes = []\n list_contents = []\n\n def _build_li(list_contents):\n data = '
'.join(t for t in list_contents if t is not None)\n return etree.XML('
  • %s
  • ' % data)\n\n def _build_non_li_content(el, meta_data):\n w_namespace = get_namespace(el, 'w')\n if el.tag == '%stbl' % w_namespace:\n new_el, visited_nodes = build_table(el, meta_data)\n return etree.tostring(new_el), visited_nodes\n elif el.tag == '%sp' % w_namespace:\n return get_element_content(el, meta_data), [el]\n if has_text(el):\n raise UnintendedTag('Did not expect %s' % el.tag)\n\n def _merge_lists(ilvl, current_ilvl, ol_dict, current_ol):\n for i in reversed(range(ilvl, current_ilvl)):\n # Any list that is more indented that ilvl needs to\n # be merged to the list before it.\n if i not in ol_dict:\n continue\n if ol_dict[i] is not current_ol:\n if ol_dict[i] is current_ol:\n continue\n ol_dict[i][-1].append(current_ol)\n current_ol = ol_dict[i]\n\n # Clean up finished nested lists.\n for key in list(ol_dict):\n if key > ilvl:\n del ol_dict[key]\n return current_ol\n\n for li_node in li_nodes:\n w_namespace = get_namespace(li_node, 'w')\n if not is_li(li_node, meta_data):\n # Get the content and visited nodes\n new_el, el_visited_nodes = _build_non_li_content(\n li_node,\n meta_data,\n )\n list_contents.append(new_el)\n visited_nodes.extend(el_visited_nodes)\n continue\n if list_contents:\n li_el = _build_li(list_contents)\n list_contents = []\n current_ol.append(li_el)\n # Get the data needed to build the current list item\n list_contents.append(get_element_content(\n li_node,\n meta_data,\n ))\n ilvl = get_ilvl(li_node, w_namespace)\n numId = get_numId(li_node, w_namespace)\n list_type = get_ordered_list_type(meta_data, numId, ilvl)\n\n # If the ilvl is greater than the current_ilvl or the list id is\n # changing then we have the first li tag in a nested list. We need to\n # create a new list object and update all of our variables for keeping\n # track.\n if (ilvl > current_ilvl) or (numId != current_numId):\n # Only create a new list\n ol_dict[ilvl] = create_list(list_type)\n current_ol = ol_dict[ilvl]\n current_ilvl = ilvl\n current_numId = numId\n # Both cases above are not True then we need to close all lists greater\n # than ilvl and then remove them from the ol_dict\n else:\n # Merge any nested lists that need to be merged.\n current_ol = _merge_lists(\n ilvl=ilvl,\n current_ilvl=current_ilvl,\n ol_dict=ol_dict,\n current_ol=current_ol,\n )\n\n # Set the root list after the first list is created.\n if root_ol is None:\n root_ol = current_ol\n\n # Set the current list.\n if ilvl in ol_dict:\n current_ol = ol_dict[ilvl]\n else:\n # In some instances the ilvl is not in the ol_dict, if that is the\n # case, create it here (not sure how this happens but it has\n # before.) Only do this if the current_ol is not the root_ol,\n # otherwise etree will crash.\n\n if current_ol is not root_ol:\n\n # Merge the current_ol into the root_ol. _merge_lists is not\n # equipped to handle this situation since the only way to get\n # into this block of code is to have mangled ilvls.\n root_ol[-1].append(current_ol)\n\n # Reset the current_ol\n current_ol = create_list(list_type)\n\n # Create the li element.\n visited_nodes.extend(list(li_node.iter()))\n\n # If a list item is the last thing in a document, then you will need to add\n # it here. Should probably figure out how to get the above logic to deal\n # with it.\n if list_contents:\n li_el = _build_li(list_contents)\n list_contents = []\n current_ol.append(li_el)\n\n # Merge up any nested lists that have not been merged.\n current_ol = _merge_lists(\n ilvl=0,\n current_ilvl=current_ilvl,\n ol_dict=ol_dict,\n current_ol=current_ol,\n )\n\n return root_ol, visited_nodes"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nbuilding a single tr element.", "response": "def build_tr(tr, meta_data, row_spans):\n \"\"\"\n This will return a single tr element, with all tds already populated.\n \"\"\"\n\n # Create a blank tr element.\n tr_el = etree.Element('tr')\n w_namespace = get_namespace(tr, 'w')\n visited_nodes = []\n for el in tr:\n if el in visited_nodes:\n continue\n visited_nodes.append(el)\n # Find the table cells.\n if el.tag == '%stc' % w_namespace:\n v_merge = get_v_merge(el)\n # If there is a v_merge and it is not restart then this cell can be\n # ignored.\n if (\n v_merge is not None and\n v_merge.get('%sval' % w_namespace) != 'restart'):\n continue\n\n # Loop through each and build a list of all the content.\n texts = []\n for td_content in el:\n # Since we are doing look-a-heads in this loop we need to check\n # again to see if we have already visited the node.\n if td_content in visited_nodes:\n continue\n\n # Check to see if it is a list or a regular paragraph.\n if is_li(td_content, meta_data):\n # If it is a list, create the list and update\n # visited_nodes.\n li_nodes = get_single_list_nodes_data(\n td_content,\n meta_data,\n )\n list_el, list_visited_nodes = build_list(\n li_nodes,\n meta_data,\n )\n visited_nodes.extend(list_visited_nodes)\n texts.append(etree.tostring(list_el))\n elif td_content.tag == '%stbl' % w_namespace:\n table_el, table_visited_nodes = build_table(\n td_content,\n meta_data,\n )\n visited_nodes.extend(table_visited_nodes)\n texts.append(etree.tostring(table_el))\n elif td_content.tag == '%stcPr' % w_namespace:\n # Do nothing\n visited_nodes.append(td_content)\n continue\n else:\n text = get_element_content(\n td_content,\n meta_data,\n is_td=True,\n )\n texts.append(text)\n\n data = '
    '.join(t for t in texts if t is not None)\n td_el = etree.XML('%s' % data)\n # if there is a colspan then set it here.\n colspan = get_grid_span(el)\n if colspan > 1:\n td_el.set('colspan', '%d' % colspan)\n v_merge = get_v_merge(el)\n\n # If this td has a v_merge and it is restart then set the rowspan\n # here.\n if (\n v_merge is not None and\n v_merge.get('%sval' % w_namespace) == 'restart'):\n rowspan = next(row_spans)\n td_el.set('rowspan', '%d' % rowspan)\n\n tr_el.append(td_el)\n return tr_el"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef build_table(table, meta_data):\n\n # Create a blank table element.\n table_el = etree.Element('table')\n w_namespace = get_namespace(table, 'w')\n\n # Get the rowspan values for cells that have a rowspan.\n row_spans = get_rowspan_data(table)\n for el in table:\n if el.tag == '%str' % w_namespace:\n # Create the tr element.\n tr_el = build_tr(\n el,\n meta_data,\n row_spans,\n )\n # And append it to the table.\n table_el.append(tr_el)\n\n visited_nodes = list(table.iter())\n return table_el, visited_nodes", "response": "This returns a table object with all rows and cells correctly populated."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ngenerate the string data that for this particular t tag.", "response": "def get_t_tag_content(\n t, parent, remove_bold, remove_italics, meta_data):\n \"\"\"\n Generate the string data that for this particular t tag.\n \"\"\"\n if t is None or t.text is None:\n return ''\n\n # Need to escape the text so that we do not accidentally put in text\n # that is not valid XML.\n # cgi will replace things like & < > with & < >\n text = cgi.escape(t.text)\n\n # Wrap the text with any modifiers it might have (bold, italics or\n # underline)\n el_is_bold = not remove_bold and (\n is_bold(parent) or\n is_underlined(parent)\n )\n el_is_italics = not remove_italics and is_italics(parent)\n if el_is_bold:\n text = '%s' % text\n if el_is_italics:\n text = '%s' % text\n return text"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nremoves all tags with the given tag name from the tree.", "response": "def _strip_tag(tree, tag):\n \"\"\"\n Remove all tags that have the tag name ``tag``\n \"\"\"\n for el in tree.iter():\n if el.tag == tag:\n el.getparent().remove(el)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nconverts a file to html.", "response": "def convert(file_path, image_handler=None, fall_back=None, converter=None):\n \"\"\"\n ``file_path`` is a path to the file on the file system that you want to be\n converted to html.\n ``image_handler`` is a function that takes an image_id and a\n relationship_dict to generate the src attribute for images. (see readme\n for more details)\n ``fall_back`` is a function that takes a ``file_path``. This function will\n only be called if for whatever reason the conversion fails.\n ``converter`` is a function to convert a document that is not docx to docx\n (examples in docx2html.converters)\n\n Returns html extracted from ``file_path``\n \"\"\"\n file_base, extension = os.path.splitext(os.path.basename(file_path))\n\n if extension == '.html' or extension == '.htm':\n return read_html_file(file_path)\n\n # Create the converted file as a file in the same dir with the\n # same name only with a .docx extension\n docx_path = replace_ext(file_path, '.docx')\n if extension == '.docx':\n # If the file is already html, just leave it in place.\n docx_path = file_path\n else:\n if converter is None:\n raise FileNotDocx('The file passed in is not a docx.')\n converter(docx_path, file_path)\n if not os.path.isfile(docx_path):\n if fall_back is None:\n raise ConversionFailed('Conversion to docx failed.')\n else:\n return fall_back(file_path)\n\n try:\n # Docx files are actually just zip files.\n zf = get_zip_file_handler(docx_path)\n except BadZipfile:\n raise MalformedDocx('This file is not a docx')\n\n # Need to populate the xml based on word/document.xml\n tree, meta_data = _get_document_data(zf, image_handler)\n return create_html(tree, meta_data)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef find(dataset, url):\n '''Find the location of a dataset on disk, downloading if needed.'''\n fn = os.path.join(DATASETS, dataset)\n dn = os.path.dirname(fn)\n if not os.path.exists(dn):\n print('creating dataset directory: %s', dn)\n os.makedirs(dn)\n if not os.path.exists(fn):\n if sys.version_info < (3, ):\n urllib.urlretrieve(url, fn)\n else:\n urllib.request.urlretrieve(url, fn)\n return fn", "response": "Find the location of a dataset on disk downloading if needed."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nload the MNIST digits dataset.", "response": "def load_mnist(flatten=True, labels=False):\n '''Load the MNIST digits dataset.'''\n fn = find('mnist.pkl.gz', 'http://deeplearning.net/data/mnist/mnist.pkl.gz')\n h = gzip.open(fn, 'rb')\n if sys.version_info < (3, ):\n (timg, tlab), (vimg, vlab), (simg, slab) = pickle.load(h)\n else:\n (timg, tlab), (vimg, vlab), (simg, slab) = pickle.load(h, encoding='bytes')\n h.close()\n if not flatten:\n timg = timg.reshape((-1, 28, 28, 1))\n vimg = vimg.reshape((-1, 28, 28, 1))\n simg = simg.reshape((-1, 28, 28, 1))\n if labels:\n return ((timg, tlab.astype('i')),\n (vimg, vlab.astype('i')),\n (simg, slab.astype('i')))\n return (timg, ), (vimg, ), (simg, )"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef load_cifar(flatten=True, labels=False):\n '''Load the CIFAR10 image dataset.'''\n def extract(name):\n print('extracting data from {}'.format(name))\n h = tar.extractfile(name)\n if sys.version_info < (3, ):\n d = pickle.load(h)\n else:\n d = pickle.load(h, encoding='bytes')\n for k in list(d):\n d[k.decode('utf8')] = d[k]\n h.close()\n img = d['data'].reshape(\n (-1, 3, 32, 32)).transpose((0, 2, 3, 1)).astype('f') / 128 - 1\n if flatten:\n img = img.reshape((-1, 32 * 32 * 3))\n d['data'] = img\n return d\n\n fn = find('cifar10.tar.gz', 'http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz')\n tar = tarfile.open(fn)\n\n imgs = []\n labs = []\n for i in range(1, 6):\n d = extract('cifar-10-batches-py/data_batch_{}'.format(i))\n imgs.extend(d['data'])\n labs.extend(d['labels'])\n timg = np.asarray(imgs[:40000])\n tlab = np.asarray(labs[:40000], 'i')\n vimg = np.asarray(imgs[40000:])\n vlab = np.asarray(labs[40000:], 'i')\n\n d = extract('cifar-10-batches-py/test_batch')\n simg = d['data']\n slab = d['labels']\n\n tar.close()\n\n if labels:\n return (timg, tlab), (vimg, vlab), (simg, slab)\n return (timg, ), (vimg, ), (simg, )", "response": "Load the CIFAR10 image dataset."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef plot_images(imgs, loc, title=None, channels=1):\n '''Plot an array of images.\n\n We assume that we are given a matrix of data whose shape is (n*n, s*s*c) --\n that is, there are n^2 images along the first axis of the array, and each\n image is c squares measuring s pixels on a side. Each row of the input will\n be plotted as a sub-region within a single image array containing an n x n\n grid of images.\n '''\n n = int(np.sqrt(len(imgs)))\n assert n * n == len(imgs), 'images array must contain a square number of rows!'\n s = int(np.sqrt(len(imgs[0]) / channels))\n assert s * s == len(imgs[0]) / channels, 'images must be square!'\n\n img = np.zeros(((s+1) * n - 1, (s+1) * n - 1, channels), dtype=imgs[0].dtype)\n for i, pix in enumerate(imgs):\n r, c = divmod(i, n)\n img[r * (s+1):(r+1) * (s+1) - 1,\n c * (s+1):(c+1) * (s+1) - 1] = pix.reshape((s, s, channels))\n\n img -= img.min()\n img /= img.max()\n\n ax = plt.gcf().add_subplot(loc)\n ax.xaxis.set_visible(False)\n ax.yaxis.set_visible(False)\n ax.set_frame_on(False)\n ax.imshow(img.squeeze(), cmap=plt.cm.gray)\n if title:\n ax.set_title(title)", "response": "Plot an array of images."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef plot_layers(weights, tied_weights=False, channels=1):\n '''Create a plot of weights, visualized as \"bottom-level\" pixel arrays.'''\n if hasattr(weights[0], 'get_value'):\n weights = [w.get_value() for w in weights]\n k = min(len(weights), 9)\n imgs = np.eye(weights[0].shape[0])\n for i, weight in enumerate(weights[:-1]):\n imgs = np.dot(weight.T, imgs)\n plot_images(imgs,\n 100 + 10 * k + i + 1,\n channels=channels,\n title='Layer {}'.format(i+1))\n weight = weights[-1]\n n = weight.shape[1] / channels\n if int(np.sqrt(n)) ** 2 != n:\n return\n if tied_weights:\n imgs = np.dot(weight.T, imgs)\n plot_images(imgs,\n 100 + 10 * k + k,\n channels=channels,\n title='Layer {}'.format(k))\n else:\n plot_images(weight,\n 100 + 10 * k + k,\n channels=channels,\n title='Decoding weights')", "response": "Create a plot of weights visualized as bottom - level pixel arrays."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef plot_filters(filters):\n '''Create a plot of conv filters, visualized as pixel arrays.'''\n imgs = filters.get_value()\n\n N, channels, x, y = imgs.shape\n n = int(np.sqrt(N))\n assert n * n == N, 'filters must contain a square number of rows!'\n assert channels == 1 or channels == 3, 'can only plot grayscale or rgb filters!'\n\n img = np.zeros(((y+1) * n - 1, (x+1) * n - 1, channels), dtype=imgs[0].dtype)\n for i, pix in enumerate(imgs):\n r, c = divmod(i, n)\n img[r * (y+1):(r+1) * (y+1) - 1,\n c * (x+1):(c+1) * (x+1) - 1] = pix.transpose((1, 2, 0))\n\n img -= img.min()\n img /= img.max()\n\n ax = plt.gcf().add_subplot(111)\n ax.xaxis.set_visible(False)\n ax.yaxis.set_visible(False)\n ax.set_frame_on(False)\n ax.imshow(img.squeeze(), cmap=plt.cm.gray)", "response": "Create a plot of conv filters visualized as pixel arrays."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncreate a callable that generates samples from a dataset in batches of size batch_size.", "response": "def batches(arrays, steps=100, batch_size=64, rng=None):\n '''Create a callable that generates samples from a dataset.\n\n Parameters\n ----------\n arrays : list of ndarray (time-steps, data-dimensions)\n Arrays of data. Rows in these arrays are assumed to correspond to time\n steps, and columns to variables. Multiple arrays can be given; in such\n a case, these arrays usually correspond to [input, output]---for\n example, for a recurrent regression problem---or [input, output,\n weights]---for a weighted regression or classification problem.\n steps : int, optional\n Generate samples of this many time steps. Defaults to 100.\n batch_size : int, optional\n Generate this many samples per call. Defaults to 64. This must match the\n batch_size parameter that was used when creating the recurrent network\n that will process the data.\n rng : :class:`numpy.random.RandomState` or int, optional\n A random number generator, or an integer seed for a random number\n generator. If not provided, the random number generator will be created\n with an automatically chosen seed.\n\n Returns\n -------\n callable :\n A callable that can be used inside a dataset for training a recurrent\n network.\n '''\n assert batch_size >= 2, 'batch_size must be at least 2!'\n assert isinstance(arrays, (tuple, list)), 'arrays must be a tuple or list!'\n\n if rng is None or isinstance(rng, int):\n rng = np.random.RandomState(rng)\n\n def sample():\n xs = [np.zeros((batch_size, steps, a.shape[1]), a.dtype) for a in arrays]\n for i in range(batch_size):\n j = rng.randint(len(arrays[0]) - steps)\n for x, a in zip(xs, arrays):\n x[i] = a[j:j+steps]\n return xs\n\n return sample"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef encode(self, txt):\n '''Encode a text string by replacing characters with alphabet index.\n\n Parameters\n ----------\n txt : str\n A string to encode.\n\n Returns\n -------\n classes : list of int\n A sequence of alphabet index values corresponding to the given text.\n '''\n return list(self._fwd_index.get(c, 0) for c in txt)", "response": "Encode a text string by replacing characters with alphabet index."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ncreates a callable that returns a batch of training data.", "response": "def classifier_batches(self, steps, batch_size, rng=None):\n '''Create a callable that returns a batch of training data.\n\n Parameters\n ----------\n steps : int\n Number of time steps in each batch.\n batch_size : int\n Number of training examples per batch.\n rng : :class:`numpy.random.RandomState` or int, optional\n A random number generator, or an integer seed for a random number\n generator. If not provided, the random number generator will be\n created with an automatically chosen seed.\n\n Returns\n -------\n batch : callable\n A callable that, when called, returns a batch of data that can be\n used to train a classifier model.\n '''\n assert batch_size >= 2, 'batch_size must be at least 2!'\n\n if rng is None or isinstance(rng, int):\n rng = np.random.RandomState(rng)\n\n T = np.arange(steps)\n\n def batch():\n inputs = np.zeros((batch_size, steps, 1 + len(self.alpha)), 'f')\n outputs = np.zeros((batch_size, steps), 'i')\n for b in range(batch_size):\n offset = rng.randint(len(self.text) - steps - 1)\n enc = self.encode(self.text[offset:offset + steps + 1])\n inputs[b, T, enc[:-1]] = 1\n outputs[b, T] = enc[1:]\n return [inputs, outputs]\n\n return batch"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ndraws a sequential sample of class labels from this network.", "response": "def predict_sequence(self, labels, steps, streams=1, rng=None):\n '''Draw a sequential sample of class labels from this network.\n\n Parameters\n ----------\n labels : list of int\n A list of integer class labels to get the classifier started.\n steps : int\n The number of time steps to sample.\n streams : int, optional\n Number of parallel streams to sample from the model. Defaults to 1.\n rng : :class:`numpy.random.RandomState` or int, optional\n A random number generator, or an integer seed for a random number\n generator. If not provided, the random number generator will be\n created with an automatically chosen seed.\n\n Yields\n ------\n label(s) : int or list of int\n Yields at each time step an integer class label sampled sequentially\n from the model. If the number of requested streams is greater than\n 1, this will be a list containing the corresponding number of class\n labels.\n '''\n if rng is None or isinstance(rng, int):\n rng = np.random.RandomState(rng)\n offset = len(labels)\n batch = max(2, streams)\n inputs = np.zeros((batch, offset + steps, self.layers[0].output_size), 'f')\n inputs[:, np.arange(offset), labels] = 1\n for i in range(offset, offset + steps):\n chars = []\n for pdf in self.predict_proba(inputs[:i])[:, -1]:\n try:\n c = rng.multinomial(1, pdf).argmax(axis=-1)\n except ValueError:\n # sometimes the pdf triggers a normalization error. just\n # choose greedily in this case.\n c = pdf.argmax(axis=-1)\n chars.append(int(c))\n inputs[np.arange(batch), i, chars] = 1\n yield chars[0] if streams == 1 else chars"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef add_conv_weights(self, name, mean=0, std=None, sparsity=0):\n '''Add a convolutional weight array to this layer's parameters.\n\n Parameters\n ----------\n name : str\n Name of the parameter to add.\n mean : float, optional\n Mean value for randomly-initialized weights. Defaults to 0.\n std : float, optional\n Standard deviation of initial matrix values. Defaults to\n :math:`1 / sqrt(n_i + n_o)`.\n sparsity : float, optional\n Fraction of weights to set to zero. Defaults to 0.\n '''\n nin = self.input_size\n nout = self.output_size\n mean = self.kwargs.get(\n 'mean_{}'.format(name),\n self.kwargs.get('mean', mean))\n std = self.kwargs.get(\n 'std_{}'.format(name),\n self.kwargs.get('std', std or 1 / np.sqrt(nin + nout)))\n sparsity = self.kwargs.get(\n 'sparsity_{}'.format(name),\n self.kwargs.get('sparsity', sparsity))\n arr = np.zeros((nout, nin) + self.filter_size, util.FLOAT)\n for r in range(self.filter_size[0]):\n for c in range(self.filter_size[1]):\n arr[:, :, r, c] = util.random_matrix(\n nout, nin, mean, std, sparsity=sparsity, rng=self.rng)\n self._params.append(theano.shared(arr, name=self._fmt(name)))", "response": "Add a convolutional weight array to this layer s parameters."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef encode(self, x, layer=None, sample=False, **kwargs):\n '''Encode a dataset using the hidden layer activations of our network.\n\n Parameters\n ----------\n x : ndarray\n A dataset to encode. Rows of this dataset capture individual data\n points, while columns represent the variables in each data point.\n\n layer : str, optional\n The name of the hidden layer output to use. By default, we use\n the \"middle\" hidden layer---for example, for a 4,2,4 or 4,3,2,3,4\n autoencoder, we use the layer with size 2.\n\n sample : bool, optional\n If True, then draw a sample using the hidden activations as\n independent Bernoulli probabilities for the encoded data. This\n assumes the hidden layer has a logistic sigmoid activation function.\n\n Returns\n -------\n ndarray :\n The given dataset, encoded by the appropriate hidden layer\n activation.\n '''\n enc = self.feed_forward(x, **kwargs)[self._find_output(layer)]\n if sample:\n return np.random.binomial(n=1, p=enc).astype(np.uint8)\n return enc", "response": "Encode a dataset using the hidden layer activations of our network."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ndecode an encoded dataset by computing the output layer activation.", "response": "def decode(self, z, layer=None, **kwargs):\n '''Decode an encoded dataset by computing the output layer activation.\n\n Parameters\n ----------\n z : ndarray\n A matrix containing encoded data from this autoencoder.\n layer : int or str or :class:`Layer `, optional\n The index or name of the hidden layer that was used to encode `z`.\n\n Returns\n -------\n decoded : ndarray\n The decoded dataset.\n '''\n key = self._find_output(layer)\n if key not in self._functions:\n regs = regularizers.from_kwargs(self, **kwargs)\n outputs, updates = self.build_graph(regs)\n self._functions[key] = theano.function(\n [outputs[key]],\n [outputs[self.layers[-1].output_name]],\n updates=updates)\n return self._functions[key](z)[0]"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _find_output(self, layer):\n '''Find a layer output name for the given layer specifier.\n\n Parameters\n ----------\n layer : None, int, str, or :class:`theanets.layers.Layer`\n A layer specification. If this is None, the \"middle\" layer in the\n network will be used (i.e., the layer at the middle index in the\n list of network layers). If this is an integer, the corresponding\n layer in the network's layer list will be used. If this is a string,\n the layer with the corresponding name will be returned.\n\n Returns\n -------\n name : str\n The fully-scoped output name for the desired layer.\n '''\n if layer is None:\n layer = len(self.layers) // 2\n if isinstance(layer, int):\n layer = self.layers[layer]\n if isinstance(layer, util.basestring):\n try:\n layer = [l for l in self.layers if l.name == layer][0]\n except IndexError:\n pass\n if isinstance(layer, layers.Layer):\n layer = layer.output_name\n return layer", "response": "Find a layer output name for the given layer specifier."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef score(self, x, w=None, **kwargs):\n '''Compute R^2 coefficient of determination for a given input.\n\n Parameters\n ----------\n x : ndarray (num-examples, num-inputs)\n An array containing data to be fed into the network. Multiple\n examples are arranged as rows in this array, with columns containing\n the variables for each example.\n\n Returns\n -------\n r2 : float\n The R^2 correlation between the prediction of this netork and its\n input. This can serve as one measure of the information loss of the\n autoencoder.\n '''\n return super(Autoencoder, self).score(x, x, w=w, **kwargs)", "response": "Compute R^2 coefficient of determination for a given input."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn expressions that should be computed to monitor training.", "response": "def monitors(self, **kwargs):\n '''Return expressions that should be computed to monitor training.\n\n Returns\n -------\n monitors : list of (name, expression) pairs\n A list of named monitor expressions to compute for this network.\n '''\n monitors = super(Classifier, self).monitors(**kwargs)\n regs = regularizers.from_kwargs(self, **kwargs)\n outputs, _ = self.build_graph(regs)\n return monitors + [('acc', self.losses[0].accuracy(outputs))]"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef predict(self, x, **kwargs):\n '''Compute a greedy classification for the given set of data.\n\n Parameters\n ----------\n x : ndarray (num-examples, num-variables)\n An array containing examples to classify. Examples are given as the\n rows in this array.\n\n Returns\n -------\n k : ndarray (num-examples, )\n A vector of class index values, one per row of input data.\n '''\n outputs = self.feed_forward(x, **kwargs)\n return outputs[self.layers[-1].output_name].argmax(axis=-1)", "response": "Compute a greedy classification for the given set of data."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncomputing class posterior probabilities for the given set of data.", "response": "def predict_proba(self, x, **kwargs):\n '''Compute class posterior probabilities for the given set of data.\n\n Parameters\n ----------\n x : ndarray (num-examples, num-variables)\n An array containing examples to predict. Examples are given as the\n rows in this array.\n\n Returns\n -------\n p : ndarray (num-examples, num-classes)\n An array of class posterior probability values, one per row of input\n data.\n '''\n return self.feed_forward(x, **kwargs)[self.layers[-1].output_name]"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncompute the logit values that underlie the softmax output.", "response": "def predict_logit(self, x, **kwargs):\n '''Compute the logit values that underlie the softmax output.\n\n Parameters\n ----------\n x : ndarray (num-examples, num-variables)\n An array containing examples to classify. Examples are given as the\n rows in this array.\n\n Returns\n -------\n l : ndarray (num-examples, num-classes)\n An array of posterior class logit values, one row of logit values\n per row of input data.\n '''\n return self.feed_forward(x, **kwargs)[self.layers[-1].full_name('pre')]"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef score(self, x, y, w=None, **kwargs):\n '''Compute the mean accuracy on a set of labeled data.\n\n Parameters\n ----------\n x : ndarray (num-examples, num-variables)\n An array containing examples to classify. Examples are given as the\n rows in this array.\n y : ndarray (num-examples, )\n A vector of integer class labels, one for each row of input data.\n w : ndarray (num-examples, )\n A vector of weights, one for each row of input data.\n\n Returns\n -------\n score : float\n The (possibly weighted) mean accuracy of the model on the data.\n '''\n eq = y == self.predict(x, **kwargs)\n if w is not None:\n return (w * eq).sum() / w.sum()\n return eq.mean()", "response": "Compute the mean accuracy on a set of labeled data."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nextracts a single batch of data to pass to the model being trained.", "response": "def batch_at(features, labels, seq_begins, seq_lengths):\n '''Extract a single batch of data to pass to the model being trained.\n\n Parameters\n ----------\n features, labels : ndarray\n Arrays of the input features and target labels.\n seq_begins : ndarray\n Array of the start offsets of the speech segments to include.\n seq_lengths : ndarray\n Array of the lengths of the speech segments to include in the batch.\n\n Returns\n -------\n features, labels, mask : ndarrays\n A triple of arrays for training a network. The first element contains\n input features, the second contains target labels, and the third\n contains a \"mask\" consisting of ones where there is valid data and zeros\n everywhere else.\n '''\n length = seq_lengths.max()\n feat = np.zeros((BATCH_SIZE, length, features.shape[-1]), 'f')\n labl = np.zeros((BATCH_SIZE, length), 'i')\n mask = np.zeros((BATCH_SIZE, length), 'f')\n for b, (begin, length) in enumerate(zip(seq_begins, seq_lengths)):\n feat[b, :length] = features[begin:begin+length]\n labl[b, :length] = labels[begin:begin+length]\n mask[b, :length] = 1\n return [feat, labl, mask]"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef batches(dataset):\n '''Returns a callable that chooses sequences from netcdf data.'''\n seq_lengths = dataset.variables['seqLengths'].data\n seq_begins = np.concatenate(([0], np.cumsum(seq_lengths)[:-1]))\n\n def sample():\n chosen = np.random.choice(\n list(range(len(seq_lengths))), BATCH_SIZE, replace=False)\n return batch_at(dataset.variables['inputs'].data,\n dataset.variables['targetClasses'].data,\n seq_begins[chosen],\n seq_lengths[chosen])\n\n return sample", "response": "Returns a callable that chooses sequences from netcdf data."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef load(self, path):\n '''Load a saved network from a pickle file on disk.\n\n This method sets the ``network`` attribute of the experiment to the\n loaded network model.\n\n Parameters\n ----------\n filename : str\n Load the keyword arguments and parameters of a network from a pickle\n file at the named path. If this name ends in \".gz\" then the input\n will automatically be gunzipped; otherwise the input will be treated\n as a \"raw\" pickle.\n\n Returns\n -------\n network : :class:`Network `\n A newly-constructed network, with topology and parameters loaded\n from the given pickle file.\n '''\n self.network = graph.Network.load(path)\n return self.network", "response": "Load a saved network from a pickle file on disk."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef random_matrix(rows, cols, mean=0, std=1, sparsity=0, radius=0, diagonal=0, rng=None):\n '''Create a matrix of randomly-initialized weights.\n\n Parameters\n ----------\n rows : int\n Number of rows of the weight matrix -- equivalently, the number of\n \"input\" units that the weight matrix connects.\n cols : int\n Number of columns of the weight matrix -- equivalently, the number\n of \"output\" units that the weight matrix connects.\n mean : float, optional\n Draw initial weight values from a normal with this mean. Defaults to 0.\n std : float, optional\n Draw initial weight values from a normal with this standard deviation.\n Defaults to 1.\n sparsity : float in (0, 1), optional\n If given, ensure that the given fraction of the weight matrix is\n set to zero. Defaults to 0, meaning all weights are nonzero.\n radius : float, optional\n If given, rescale the initial weights to have this spectral radius.\n No scaling is performed by default.\n diagonal : float, optional\n If nonzero, create a matrix containing all zeros except for this value\n along the diagonal. If nonzero, other arguments (except for rows and\n cols) will be ignored.\n rng : :class:`numpy.random.RandomState` or int, optional\n A random number generator, or an integer seed for a random number\n generator. If not provided, the random number generator will be created\n with an automatically chosen seed.\n\n Returns\n -------\n matrix : numpy array\n An array containing random values. These often represent the weights\n connecting each \"input\" unit to each \"output\" unit in a layer.\n '''\n if rng is None or isinstance(rng, int):\n rng = np.random.RandomState(rng)\n arr = mean + std * rng.randn(rows, cols)\n if 1 > sparsity > 0:\n k = min(rows, cols)\n mask = rng.binomial(n=1, p=1 - sparsity, size=(rows, cols)).astype(bool)\n mask[:k, :k] |= np.eye(k).astype(bool)\n arr *= mask\n if radius > 0:\n # rescale weights to have the appropriate spectral radius.\n u, s, vT = np.linalg.svd(arr, full_matrices=False)\n arr = np.dot(np.dot(u, np.diag(radius * s / abs(s[0]))), vT)\n if diagonal != 0:\n # generate a diagonal weight matrix. ignore other options.\n arr = diagonal * np.eye(max(rows, cols))[:rows, :cols]\n return arr.astype(FLOAT)", "response": "Create a random matrix of the given size."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncreating a vector of randomly - initialized values.", "response": "def random_vector(size, mean=0, std=1, rng=None):\n '''Create a vector of randomly-initialized values.\n\n Parameters\n ----------\n size : int\n Length of vecctor to create.\n mean : float, optional\n Mean value for initial vector values. Defaults to 0.\n std : float, optional\n Standard deviation for initial vector values. Defaults to 1.\n rng : :class:`numpy.random.RandomState` or int, optional\n A random number generator, or an integer seed for a random number\n generator. If not provided, the random number generator will be created\n with an automatically chosen seed.\n\n Returns\n -------\n vector : numpy array\n An array containing random values. This often represents the bias for a\n layer of computation units.\n '''\n if rng is None or isinstance(rng, int):\n rng = np.random.RandomState(rng)\n return (mean + std * rng.randn(size)).astype(FLOAT)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef outputs_matching(outputs, patterns):\n '''Get the outputs from a network that match a pattern.\n\n Parameters\n ----------\n outputs : dict or sequence of (str, theano expression)\n Output expressions to filter for matches. If this is a dictionary, its\n ``items()`` will be processed for matches.\n patterns : sequence of str\n A sequence of glob-style patterns to match against. Any parameter\n matching any pattern in this sequence will be included in the match.\n\n Yields\n ------\n matches : pair of str, theano expression\n Generates a sequence of (name, expression) pairs. The name is the name\n of the output that matched, and the expression is the symbolic output in\n the network graph.\n '''\n if isinstance(patterns, basestring):\n patterns = (patterns, )\n if isinstance(outputs, dict):\n outputs = outputs.items()\n for name, expr in outputs:\n for pattern in patterns:\n if fnmatch.fnmatch(name, pattern):\n yield name, expr\n break", "response": "Get the outputs from a network that match a pattern."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget the parameters from a network that match a pattern.", "response": "def params_matching(layers, patterns):\n '''Get the parameters from a network that match a pattern.\n\n Parameters\n ----------\n layers : list of :class:`theanets.layers.Layer`\n A list of network layers to retrieve parameters from.\n patterns : sequence of str\n A sequence of glob-style patterns to match against. Any parameter\n matching any pattern in this sequence will be included in the match.\n\n Yields\n ------\n matches : pair of str, theano expression\n Generates a sequence of (name, expression) pairs. The name is the name\n of the parameter that matched, and the expression represents the\n parameter symbolically.\n '''\n if isinstance(patterns, basestring):\n patterns = (patterns, )\n for layer in layers:\n for param in layer.params:\n name = param.name\n for pattern in patterns:\n if fnmatch.fnmatch(name, pattern):\n yield name, param\n break"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nconstruct a common regularizer from a set of keyword arguments.", "response": "def from_kwargs(graph, **kwargs):\n '''Construct common regularizers from a set of keyword arguments.\n\n Keyword arguments not listed below will be passed to\n :func:`Regularizer.build` if they specify the name of a registered\n :class:`Regularizer`.\n\n Parameters\n ----------\n graph : :class:`theanets.graph.Network`\n A network graph to regularize.\n\n regularizers : dict or tuple/list of :class:`Regularizer`, optional\n If this is a list or a tuple, the contents of the list will be returned\n as the regularizers. This is to permit custom lists of regularizers to\n be passed easily.\n\n If this is a dict, its contents will be added to the other keyword\n arguments passed in.\n\n rng : int or theano RandomStreams, optional\n If an integer is provided, it will be used to seed the random number\n generators for the dropout or noise regularizers. If a theano\n RandomStreams object is provided, it will be used directly. Defaults to\n 13.\n\n input_dropout : float, optional\n Apply dropout to input layers in the network graph, with this dropout\n rate. Defaults to 0 (no dropout).\n\n hidden_dropout : float, optional\n Apply dropout to hidden layers in the network graph, with this dropout\n rate. Defaults to 0 (no dropout).\n\n output_dropout : float, optional\n Apply dropout to the output layer in the network graph, with this\n dropout rate. Defaults to 0 (no dropout).\n\n input_noise : float, optional\n Apply noise to input layers in the network graph, with this standard\n deviation. Defaults to 0 (no noise).\n\n hidden_noise : float, optional\n Apply noise to hidden layers in the network graph, with this standard\n deviation. Defaults to 0 (no noise).\n\n output_noise : float, optional\n Apply noise to the output layer in the network graph, with this\n standard deviation. Defaults to 0 (no noise).\n\n Returns\n -------\n regs : list of :class:`Regularizer`\n A list of regularizers to apply to the given network graph.\n '''\n if 'regularizers' in kwargs:\n regs = kwargs['regularizers']\n if isinstance(regs, (tuple, list)):\n return regs\n if isinstance(regs, dict):\n kwargs.update(regs)\n\n regs = []\n\n rng = kwargs.get('rng', 13)\n\n def pattern(ls):\n return tuple(l.output_name for l in ls)\n\n inputs = pattern([l for l in graph.layers if isinstance(l, layers.Input)])\n hiddens = pattern(graph.layers[1:-1])\n outputs = pattern([graph.layers[-1]])\n\n # create regularizers for different types of canned dropout.\n spec = {inputs: kwargs.get('input_dropout', 0),\n hiddens: kwargs.get('hidden_dropout', 0),\n outputs: kwargs.get('output_dropout', 0)}\n spec.update(kwargs.get('dropout', {}))\n for pattern, w in spec.items():\n if w:\n regs.append(BernoulliDropout(pattern=pattern, weight=w, rng=rng))\n\n # create regularizers for different types of canned noise.\n spec = {inputs: kwargs.get('input_noise', 0),\n hiddens: kwargs.get('hidden_noise', 0),\n outputs: kwargs.get('output_noise', 0)}\n spec.update(kwargs.get('noise', {}))\n for pattern, w in spec.items():\n if w:\n regs.append(GaussianNoise(pattern=pattern, weight=w, rng=rng))\n\n # create regularizers based on other keyword arguments.\n for key, value in kwargs.items():\n if Regularizer.is_registered(key):\n if not isinstance(value, dict):\n value = dict(weight=value)\n regs.append(Regularizer.build(key, **value))\n\n return regs"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef variables(self):\n '''A list of Theano variables used in this loss.'''\n result = [self._target]\n if self._weights is not None:\n result.append(self._weights)\n return result", "response": "A list of Theano variables used in this loss."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nbuilds a Theano expression for computing the accuracy of the graph output.", "response": "def accuracy(self, outputs):\n '''Build a Theano expression for computing the accuracy of graph output.\n\n Parameters\n ----------\n outputs : dict of Theano expressions\n A dictionary mapping network output names to Theano expressions\n representing the outputs of a computation graph.\n\n Returns\n -------\n acc : Theano expression\n A Theano expression representing the accuracy of the output compared\n to the target data.\n '''\n output = outputs[self.output_name]\n predict = TT.argmax(output, axis=-1)\n correct = TT.eq(predict, self._target)\n acc = correct.mean()\n if self._weights is not None:\n acc = (self._weights * correct).sum() / self._weights.sum()\n return acc"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _scan(self, inputs, outputs, name='scan', step=None, constants=None):\n '''Helper method for defining a basic loop in theano.\n\n Parameters\n ----------\n inputs : sequence of theano expressions\n Inputs to the scan operation.\n outputs : sequence of output specifiers\n Specifiers for the outputs of the scan operation. This should be a\n sequence containing:\n - None for values that are output by the scan but not tapped as\n inputs,\n - an integer or theano scalar (``ndim == 0``) indicating the batch\n size for initial zero state,\n - a theano tensor variable (``ndim > 0``) containing initial state\n data, or\n - a dictionary containing a full output specifier. See\n ``outputs_info`` in the Theano documentation for ``scan``.\n name : str, optional\n Name of the scan variable to create. Defaults to ``'scan'``.\n step : callable, optional\n The callable to apply in the loop. Defaults to :func:`self._step`.\n constants : sequence of tensor, optional\n A sequence of parameters, if any, needed by the step function.\n\n Returns\n -------\n output(s) : theano expression(s)\n Theano expression(s) representing output(s) from the scan.\n updates : sequence of update tuples\n A sequence of updates to apply inside a theano function.\n '''\n init = []\n for i, x in enumerate(outputs):\n ndim = getattr(x, 'ndim', -1)\n if x is None or isinstance(x, dict) or ndim > 0:\n init.append(x)\n continue\n if isinstance(x, int) or ndim == 0:\n init.append(TT.repeat(theano.shared(\n np.zeros((1, self.output_size), util.FLOAT),\n name=self._fmt('init{}'.format(i))), x, axis=0))\n continue\n raise ValueError('cannot handle input {} for scan!'.format(x))\n return theano.scan(\n step or self._step,\n name=self._fmt(name),\n sequences=inputs,\n outputs_info=init,\n non_sequences=constants,\n go_backwards='back' in self.kwargs.get('direction', '').lower(),\n truncate_gradient=self.kwargs.get('bptt_limit', -1),\n )", "response": "This method is used to define a basic loop in theano."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncreating a rate parameter for a recurrent layer.", "response": "def _create_rates(self, dist='uniform', size=None, eps=1e-4):\n '''Create a rate parameter (usually for a recurrent network layer).\n\n Parameters\n ----------\n dist : {'uniform', 'log'}, optional\n Distribution of rate values. Defaults to ``'uniform'``.\n size : int, optional\n Number of rates to create. Defaults to ``self.output_size``.\n eps : float, optional\n A \"buffer\" preventing rate values from getting too close to 0 or 1.\n Defaults to 1e-4.\n\n Returns\n -------\n rates : theano shared or None\n A vector of rate parameters for certain types of recurrent layers.\n '''\n if size is None:\n size = self.output_size\n if dist == 'uniform':\n z = np.random.uniform(eps, 1 - eps, size=size).astype(util.FLOAT)\n return theano.shared(z, name=self._fmt('rate'))\n if dist == 'log':\n z = np.random.uniform(-6, -eps, size=size).astype(util.FLOAT)\n return theano.shared(np.exp(z), name=self._fmt('rate'))\n return None"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nconstructing an activation function by name.", "response": "def build(name, layer, **kwargs):\n '''Construct an activation function by name.\n\n Parameters\n ----------\n name : str or :class:`Activation`\n The name of the type of activation function to build, or an\n already-created instance of an activation function.\n layer : :class:`theanets.layers.Layer`\n The layer to which this activation will be applied.\n kwargs : dict\n Additional named arguments to pass to the activation constructor.\n\n Returns\n -------\n activation : :class:`Activation`\n A neural network activation function instance.\n '''\n if isinstance(name, Activation):\n return name\n\n if '+' in name:\n return functools.reduce(\n Compose, (build(n, layer, **kwargs) for n in name.split('+')))\n\n act = COMMON.get(name)\n if act is not None:\n act.name = name\n act.params = []\n return act\n\n if name.lower().startswith('maxout') and ':' in name:\n name, pieces = name.split(':', 1)\n kwargs['pieces'] = int(pieces)\n kwargs['name'] = name\n kwargs['layer'] = layer\n return Activation.build(name, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ntraining a model using a training and validation set.", "response": "def itertrain(self, train, valid=None, **kwargs):\n '''Train a model using a training and validation set.\n\n This method yields a series of monitor values to the caller. After every\n iteration, a pair of monitor dictionaries is generated: one evaluated on\n the training dataset, and another evaluated on the validation dataset.\n The validation monitors might not be updated during every training\n iteration; in this case, the most recent validation monitors will be\n yielded along with the training monitors.\n\n Parameters\n ----------\n train : :class:`Dataset `\n A set of training data for computing updates to model parameters.\n valid : :class:`Dataset `\n A set of validation data for computing monitor values and\n determining when the loss has stopped improving.\n\n Yields\n ------\n training : dict\n A dictionary mapping monitor names to values, evaluated on the\n training dataset.\n validation : dict\n A dictionary containing monitor values evaluated on the validation\n dataset.\n '''\n for monitors in downhill.build(\n algo=self.algo,\n loss=self.network.loss(**kwargs),\n updates=self.network.updates(**kwargs),\n monitors=self.network.monitors(**kwargs),\n inputs=self.network.variables,\n params=self.network.params,\n monitor_gradients=kwargs.get('monitor_gradients', False),\n ).iterate(train, valid=valid, **kwargs):\n yield monitors"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef reservoir(xs, n, rng):\n '''Select a random sample of n items from xs.'''\n pool = []\n for i, x in enumerate(xs):\n if len(pool) < n:\n pool.append(x / np.linalg.norm(x))\n continue\n j = rng.randint(i + 1)\n if j < n:\n pool[j] = x / np.linalg.norm(x)\n # if the pool still has fewer than n items, pad with distorted random\n # duplicates from the source data.\n L = len(pool)\n S = np.std(pool, axis=0)\n while len(pool) < n:\n x = pool[rng.randint(L)]\n pool.append(x + S * rng.randn(*x.shape))\n return np.array(pool, dtype=pool[0].dtype)", "response": "Select a random sample of n items from xs."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ntrain a model using a training and validation set.", "response": "def itertrain(self, train, valid=None, **kwargs):\n '''Train a model using a training and validation set.\n\n This method yields a series of monitor values to the caller. After every\n iteration, a pair of monitor dictionaries is generated: one evaluated on\n the training dataset, and another evaluated on the validation dataset.\n The validation monitors might not be updated during every training\n iteration; in this case, the most recent validation monitors will be\n yielded along with the training monitors.\n\n Parameters\n ----------\n train : :class:`Dataset `\n A set of training data for computing updates to model parameters.\n valid : :class:`Dataset `\n A set of validation data for computing monitor values and\n determining when the loss has stopped improving.\n\n Yields\n ------\n training : dict\n A dictionary mapping monitor names to values, evaluated on the\n training dataset.\n validation : dict\n A dictionary containing monitor values evaluated on the validation\n dataset.\n '''\n ifci = itertools.chain.from_iterable\n\n def first(x):\n return x[0] if isinstance(x, (tuple, list)) else x\n\n def last(x):\n return x[-1] if isinstance(x, (tuple, list)) else x\n\n odim = idim = None\n for t in train:\n idim = first(t).shape[-1]\n odim = last(t).shape[-1]\n\n rng = kwargs.get('rng')\n if rng is None or isinstance(rng, int):\n rng = np.random.RandomState(rng)\n\n # set output (decoding) weights on the network.\n samples = ifci(last(t) for t in train)\n for param in self.network.layers[-1].params:\n shape = param.get_value(borrow=True).shape\n if len(shape) == 2 and shape[1] == odim:\n arr = np.vstack(SampleTrainer.reservoir(samples, shape[0], rng))\n util.log('setting {}: {}', param.name, shape)\n param.set_value(arr / np.sqrt((arr * arr).sum(axis=1))[:, None])\n\n # set input (encoding) weights on the network.\n samples = ifci(first(t) for t in train)\n for layer in self.network.layers:\n for param in layer.params:\n shape = param.get_value(borrow=True).shape\n if len(shape) == 2 and shape[0] == idim:\n arr = np.vstack(SampleTrainer.reservoir(samples, shape[1], rng)).T\n util.log('setting {}: {}', param.name, shape)\n param.set_value(arr / np.sqrt((arr * arr).sum(axis=0)))\n samples = ifci(self.network.feed_forward(\n first(t))[i-1] for t in train)\n\n yield dict(loss=0), dict(loss=0)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ntraining a model using a training and validation set.", "response": "def itertrain(self, train, valid=None, **kwargs):\n '''Train a model using a training and validation set.\n\n This method yields a series of monitor values to the caller. After every\n iteration, a pair of monitor dictionaries is generated: one evaluated on\n the training dataset, and another evaluated on the validation dataset.\n The validation monitors might not be updated during every training\n iteration; in this case, the most recent validation monitors will be\n yielded along with the training monitors.\n\n Parameters\n ----------\n train : :class:`Dataset `\n A set of training data for computing updates to model parameters.\n valid : :class:`Dataset `\n A set of validation data for computing monitor values and\n determining when the loss has stopped improving.\n\n Yields\n ------\n training : dict\n A dictionary mapping monitor names to values, evaluated on the\n training dataset.\n validation : dict\n A dictionary containing monitor values evaluated on the validation\n dataset.\n '''\n net = self.network\n original = list(net.layers)\n output_name = original[-1].output_name\n tied = any(isinstance(l, layers.Tied) for l in original)\n L = 1 + len(original) // 2 if tied else len(original) - 1\n for i in range(1, L):\n tail = []\n if i == L - 1:\n net.layers = original\n elif tied:\n net.layers = original[:i+1]\n for j in range(i):\n prev = tail[-1] if tail else net.layers[-1]\n tail.append(layers.Layer.build(\n 'tied', partner=original[i-j].name, inputs=prev.name))\n net.layers = original[:i+1] + tail\n else:\n tail.append(layers.Layer.build(\n 'feedforward',\n name='lwout',\n inputs=original[i].output_name,\n size=original[-1].output_size,\n activation=original[-1].kwargs['activation']))\n net.layers = original[:i+1] + tail\n util.log('layerwise: training {}',\n ' -> '.join(l.name for l in net.layers))\n [l.bind(net, initialize=False) for l in net.layers]\n [l.setup() for l in tail]\n net.losses[0].output_name = net.layers[-1].output_name\n trainer = DownhillTrainer(self.algo, net)\n for monitors in trainer.itertrain(train, valid, **kwargs):\n yield monitors\n net.layers = original\n net.losses[0].output_name = output_name"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ntrain a model using a training and validation set.", "response": "def itertrain(self, train, valid=None, **kwargs):\n '''Train a model using a training and validation set.\n\n This method yields a series of monitor values to the caller. After every\n iteration, a pair of monitor dictionaries is generated: one evaluated on\n the training dataset, and another evaluated on the validation dataset.\n The validation monitors might not be updated during every training\n iteration; in this case, the most recent validation monitors will be\n yielded along with the training monitors.\n\n Parameters\n ----------\n train : :class:`Dataset `\n A set of training data for computing updates to model parameters.\n valid : :class:`Dataset `\n A set of validation data for computing monitor values and\n determining when the loss has stopped improving.\n\n Yields\n ------\n training : dict\n A dictionary mapping monitor names to values, evaluated on the\n training dataset.\n validation : dict\n A dictionary containing monitor values evaluated on the validation\n dataset.\n '''\n from . import feedforward\n\n original_layer_names = set(l.name for l in self.network.layers[:-1])\n\n # construct a \"shadow\" of the input network, using the original\n # network's encoding layers, with tied weights in an autoencoder\n # configuration.\n layers_ = list(l.to_spec() for l in self.network.layers[:-1])\n for i, l in enumerate(layers_[::-1][:-2]):\n layers_.append(dict(\n form='tied', partner=l['name'], activation=l['activation']))\n layers_.append(dict(\n form='tied', partner=layers_[1]['name'], activation='linear'))\n\n util.log('creating shadow network')\n ae = feedforward.Autoencoder(layers=layers_)\n\n # train the autoencoder using the supervised layerwise pretrainer.\n pre = SupervisedPretrainer(self.algo, ae)\n for monitors in pre.itertrain(train, valid, **kwargs):\n yield monitors\n\n # copy trained parameter values back to our original network.\n for param in ae.params:\n l, p = param.name.split('.')\n if l in original_layer_names:\n util.log('copying pretrained parameter {}', param.name)\n self.network.find(l, p).set_value(param.get_value())\n\n util.log('completed unsupervised pretraining')"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef add_layer(self, layer=None, **kwargs):\n '''Add a :ref:`layer ` to our network graph.\n\n Parameters\n ----------\n layer : int, tuple, dict, or :class:`Layer `\n A value specifying the layer to add. For more information, please\n see :ref:`guide-creating-specifying-layers`.\n '''\n # if the given layer is a Layer instance, just add it and move on.\n if isinstance(layer, layers.Layer):\n self.layers.append(layer)\n return\n\n form = kwargs.pop('form', 'ff' if self.layers else 'input').lower()\n\n if isinstance(layer, util.basestring):\n if not layers.Layer.is_registered(layer):\n raise util.ConfigurationError('unknown layer type: {}'.format(layer))\n form = layer\n layer = None\n\n # if layer is a tuple/list of integers, assume it's a shape.\n if isinstance(layer, (tuple, list)) and all(isinstance(x, int) for x in layer):\n kwargs['shape'] = tuple(layer)\n layer = None\n\n # if layer is some other tuple/list, assume it's a list of:\n # - the name of a layers.Layer class (str)\n # - the name of an activation function (str)\n # - the number of units in the layer (int)\n if isinstance(layer, (tuple, list)):\n for el in layer:\n if isinstance(el, util.basestring) and layers.Layer.is_registered(el):\n form = el\n elif isinstance(el, util.basestring):\n kwargs['activation'] = el\n elif isinstance(el, int):\n if 'size' in kwargs:\n raise util.ConfigurationError(\n 'duplicate layer sizes! {}'.format(kwargs))\n kwargs['size'] = el\n layer = None\n\n # if layer is a dictionary, try to extract a form for the layer, and\n # override our default keyword arguments with the rest.\n if isinstance(layer, dict):\n for key, value in layer.items():\n if key == 'form':\n form = value.lower()\n else:\n kwargs[key] = value\n layer = None\n\n # if neither shape nor size have been specified yet, check that the\n # \"layer\" param is an int and use it for \"size\".\n if 'shape' not in kwargs and 'size' not in kwargs and isinstance(layer, int):\n kwargs['size'] = layer\n\n # if it hasn't been provided in some other way yet, set input\n # dimensionality based on the model.\n if form == 'input' and 'shape' not in kwargs:\n kwargs.setdefault('ndim', self.INPUT_NDIM)\n\n # set some default layer parameters.\n if form != 'input':\n kwargs.setdefault('inputs', self.layers[-1].output_name)\n kwargs.setdefault('rng', self._rng)\n\n if form.lower() == 'tied' and 'partner' not in kwargs:\n # we look backward through our list of layers for a partner.\n # any \"tied\" layer that we find increases a counter by one,\n # and any \"untied\" layer decreases the counter by one. our\n # partner is the first layer we find with count zero.\n #\n # this is intended to handle the hopefully common case of a\n # (possibly deep) tied-weights autoencoder.\n tied = 1\n partner = None\n for l in self.layers[::-1]:\n tied += 1 if isinstance(l, layers.Tied) else -1\n if tied == 0:\n partner = l.name\n break\n else:\n raise util.ConfigurationError(\n 'cannot find partner for \"{}\"'.format(kwargs))\n kwargs['partner'] = partner\n\n layer = layers.Layer.build(form, **kwargs)\n\n # check that graph inputs have unique names.\n if isinstance(layer, layers.Input):\n if any(layer.name == i.name for i in self.inputs):\n raise util.ConfigurationError(\n '\"{}\": duplicate input name!'.format(layer.name))\n\n self.layers.append(layer)", "response": "Adds a layer to the internal network."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef add_loss(self, loss=None, **kwargs):\n '''Add a :ref:`loss function ` to the model.\n\n Parameters\n ----------\n loss : str, dict, or :class:`theanets.losses.Loss`\n A loss function to add. If this is a Loss instance, it will be added\n immediately. If this is a string, it names a loss function to build\n and add. If it is a dictionary, it should contain a ``'form'`` key\n whose string value names the loss function to add. Other arguments\n will be passed to :func:`theanets.losses.Loss.build`.\n '''\n if isinstance(loss, losses.Loss):\n self.losses.append(loss)\n return\n\n form = loss or 'mse'\n if 'form' in kwargs:\n form = kwargs.pop('form').lower()\n\n kw = dict(target=self.INPUT_NDIM, output_name=self.layers[-1].output_name)\n kw.update(kwargs)\n\n if isinstance(loss, dict):\n loss = dict(loss)\n if 'form' in loss:\n form = loss.pop('form').lower()\n kw.update(loss)\n\n self.losses.append(losses.Loss.build(form, **kw))", "response": "Adds a loss function to the model."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef set_loss(self, *args, **kwargs):\n '''Clear the current loss functions from the network and add a new one.\n\n All parameters and keyword arguments are passed to :func:`add_loss`\n after clearing the current losses.\n '''\n self.losses = []\n self.add_loss(*args, **kwargs)", "response": "Clear the current loss functions from the network and add a new one."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ntrain our network one batch at a time.", "response": "def itertrain(self, train, valid=None, algo='rmsprop', subalgo='rmsprop',\n save_every=0, save_progress=None, **kwargs):\n '''Train our network, one batch at a time.\n\n This method yields a series of ``(train, valid)`` monitor pairs. The\n ``train`` value is a dictionary mapping names to monitor values\n evaluated on the training dataset. The ``valid`` value is also a\n dictionary mapping names to values, but these values are evaluated on\n the validation dataset.\n\n Because validation might not occur every training iteration, the\n validation monitors might be repeated for multiple training iterations.\n It is probably most helpful to think of the validation monitors as being\n the \"most recent\" values that have been computed.\n\n After training completes, the network attribute of this class will\n contain the trained network parameters.\n\n Parameters\n ----------\n train : :class:`Dataset ` or list\n A dataset to use when training the network. If this is a\n ``downhill.Dataset`` instance, it will be used directly as the\n training datset. If it is a list of numpy arrays or a list of\n callables, it will be converted to a ``downhill.Dataset`` and then\n used as the training set.\n valid : :class:`Dataset ` or list, optional\n If this is provided, it will be used as a validation dataset. If not\n provided, the training set will be used for validation. (This is not\n recommended!)\n algo : str, optional\n An optimization algorithm to use for training our network. If not\n provided, :class:`RMSProp ` will be used.\n subalgo : str, optional\n An optimization algorithm to use for a trainer that requires a\n \"sub-algorithm,\" sugh as an unsupervised pretrainer. Defaults to\n :class:`RMSProp `.\n save_every : int or float, optional\n If this is nonzero and ``save_progress`` is not None, then the model\n being trained will be saved periodically. If this is a float, it is\n treated as a number of minutes to wait between savings. If it is an\n int, it is treated as the number of training epochs to wait between\n savings. Defaults to 0.\n save_progress : str or file handle, optional\n If this is not None, and ``save_progress`` is nonzero, then save the\n model periodically during training. This parameter gives either (a)\n the full path of a file to save the model, or (b) a file-like object\n where the model should be saved. If it is a string and the given\n name contains a \"{}\" format specifier, it will be filled with the\n integer Unix timestamp at the time the model is saved. Defaults to\n None, which does not save models.\n\n Yields\n ------\n training : dict\n A dictionary of monitor values computed using the training dataset,\n at the conclusion of training. This dictionary will at least contain\n a 'loss' key that indicates the value of the loss function. Other\n keys may be available depending on the trainer being used.\n validation : dict\n A dictionary of monitor values computed using the validation\n dataset, at the conclusion of training.\n '''\n if 'rng' not in kwargs:\n kwargs['rng'] = self._rng\n\n def create_dataset(data, **kwargs):\n name = kwargs.get('name', 'dataset')\n s = '{}_batches'.format(name)\n return downhill.Dataset(\n data,\n name=name,\n batch_size=kwargs.get('batch_size', 32),\n iteration_size=kwargs.get('iteration_size', kwargs.get(s)),\n axis=kwargs.get('axis', 0),\n rng=kwargs['rng'])\n\n # set up datasets ...\n if valid is None:\n valid = train\n if not isinstance(valid, downhill.Dataset):\n valid = create_dataset(valid, name='valid', **kwargs)\n if not isinstance(train, downhill.Dataset):\n train = create_dataset(train, name='train', **kwargs)\n\n if 'algorithm' in kwargs:\n warnings.warn(\n 'please use the \"algo\" keyword arg instead of \"algorithm\"',\n DeprecationWarning)\n algo = kwargs.pop('algorithm')\n if isinstance(algo, (list, tuple)):\n algo = algo[0]\n\n # set up trainer ...\n if isinstance(algo, util.basestring):\n algo = algo.lower()\n if algo == 'sample':\n algo = trainer.SampleTrainer(self)\n elif algo.startswith('layer') or algo.startswith('sup'):\n algo = trainer.SupervisedPretrainer(subalgo, self)\n elif algo.startswith('pre') or algo.startswith('unsup'):\n algo = trainer.UnsupervisedPretrainer(subalgo, self)\n else:\n algo = trainer.DownhillTrainer(algo, self)\n\n # set up check to save model ...\n def needs_saving(elapsed, iteration):\n if save_progress is None:\n return False\n if isinstance(save_every, float):\n return elapsed > 60 * save_every\n if isinstance(save_every, int):\n return iteration % save_every == 0\n return False\n\n # train it!\n start = time.time()\n for i, monitors in enumerate(algo.itertrain(train, valid, **kwargs)):\n yield monitors\n now = time.time()\n if i and needs_saving(now - start, i):\n filename_or_handle = save_progress\n if isinstance(filename_or_handle, util.basestring):\n filename_or_handle = save_progress.format(int(now))\n self.save(filename_or_handle)\n start = now"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef train(self, *args, **kwargs):\n '''Train the network until the trainer converges.\n\n All arguments are passed to :func:`itertrain`.\n\n Returns\n -------\n training : dict\n A dictionary of monitor values computed using the training dataset,\n at the conclusion of training. This dictionary will at least contain\n a 'loss' key that indicates the value of the loss function. Other\n keys may be available depending on the trainer being used.\n validation : dict\n A dictionary of monitor values computed using the validation\n dataset, at the conclusion of training.\n '''\n monitors = None\n for monitors in self.itertrain(*args, **kwargs):\n pass\n return monitors", "response": "Train the network until the trainer converges."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _hash(self, regularizers=()):\n '''Construct a string key for representing a computation graph.\n\n This key will be unique for a given (a) network topology, (b) set of\n losses, and (c) set of regularizers.\n\n Returns\n -------\n key : str\n A hash representing the computation graph for the current network.\n '''\n def add(s):\n h.update(str(s).encode('utf-8'))\n h = hashlib.md5()\n for l in self.layers:\n add('{}{}{}'.format(l.__class__.__name__, l.name, l.output_shape))\n for l in self.losses:\n add('{}{}'.format(l.__class__.__name__, l.weight))\n for r in regularizers:\n add('{}{}{}'.format(r.__class__.__name__, r.weight, r.pattern))\n return h.hexdigest()", "response": "Construct a string key for representing a computation graph."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nconnecting the layers in this network to form a computation graph.", "response": "def build_graph(self, regularizers=()):\n '''Connect the layers in this network to form a computation graph.\n\n Parameters\n ----------\n regularizers : list of :class:`theanets.regularizers.Regularizer`\n A list of the regularizers to apply while building the computation\n graph.\n\n Returns\n -------\n outputs : list of Theano variables\n A list of expressions giving the output of each layer in the graph.\n updates : list of update tuples\n A list of updates that should be performed by a Theano function that\n computes something using this graph.\n '''\n key = self._hash(regularizers)\n if key not in self._graphs:\n util.log('building computation graph')\n for loss in self.losses:\n loss.log()\n for reg in regularizers:\n reg.log()\n outputs = {}\n updates = []\n for layer in self.layers:\n out, upd = layer.connect(outputs)\n for reg in regularizers:\n reg.modify_graph(out)\n outputs.update(out)\n updates.extend(upd)\n self._graphs[key] = outputs, updates\n return self._graphs[key]"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef inputs(self):\n '''A list of Theano variables for feedforward computations.'''\n return [l.input for l in self.layers if isinstance(l, layers.Input)]", "response": "A list of Theano variables for feedforward computations."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef find(self, which, param):\n '''Get a parameter from a layer in the network.\n\n Parameters\n ----------\n which : int or str\n The layer that owns the parameter to return.\n\n If this is an integer, then 0 refers to the input layer, 1 refers\n to the first hidden layer, 2 to the second, and so on.\n\n If this is a string, the layer with the corresponding name, if any,\n will be used.\n\n param : int or str\n Name of the parameter to retrieve from the specified layer, or its\n index in the parameter list of the layer.\n\n Raises\n ------\n KeyError\n If there is no such layer, or if there is no such parameter in the\n specified layer.\n\n Returns\n -------\n param : Theano shared variable\n A shared parameter variable from the indicated layer.\n '''\n for i, layer in enumerate(self.layers):\n if which == i or which == layer.name:\n return layer.find(param)\n raise KeyError(which)", "response": "Find a parameter from a layer in the network."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef feed_forward(self, x, **kwargs):\n '''Compute a forward pass of all layers from the given input.\n\n All keyword arguments are passed directly to :func:`build_graph`.\n\n Parameters\n ----------\n x : ndarray (num-examples, num-variables)\n An array containing data to be fed into the network. Multiple\n examples are arranged as rows in this array, with columns containing\n the variables for each example.\n\n Returns\n -------\n layers : list of ndarray (num-examples, num-units)\n The activation values of each layer in the the network when given\n input `x`. For each of the hidden layers, an array is returned\n containing one row per input example; the columns of each array\n correspond to units in the respective layer. The \"output\" of the\n network is the last element of this list.\n '''\n regs = regularizers.from_kwargs(self, **kwargs)\n key = self._hash(regs)\n if key not in self._functions:\n outputs, updates = self.build_graph(regs)\n labels, exprs = list(outputs.keys()), list(outputs.values())\n util.log('compiling feed_forward function')\n self._functions[key] = (labels, theano.function(\n self.inputs, exprs, updates=updates))\n labels, f = self._functions[key]\n return dict(zip(labels, f(x)))", "response": "Compute a forward pass of all layers from the given input."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef predict(self, x, **kwargs):\n '''Compute a forward pass of the inputs, returning the network output.\n\n All keyword arguments end up being passed to :func:`build_graph`.\n\n Parameters\n ----------\n x : ndarray (num-examples, num-variables)\n An array containing data to be fed into the network. Multiple\n examples are arranged as rows in this array, with columns containing\n the variables for each example.\n\n Returns\n -------\n y : ndarray (num-examples, num-variables)\n Returns the values of the network output units when given input `x`.\n Rows in this array correspond to examples, and columns to output\n variables.\n '''\n return self.feed_forward(x, **kwargs)[self.layers[-1].output_name]", "response": "Compute a forward pass of the inputs returning the network output."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef score(self, x, y, w=None, **kwargs):\n '''Compute R^2 coefficient of determination for a given labeled input.\n\n Parameters\n ----------\n x : ndarray (num-examples, num-inputs)\n An array containing data to be fed into the network. Multiple\n examples are arranged as rows in this array, with columns containing\n the variables for each example.\n y : ndarray (num-examples, num-outputs)\n An array containing expected target data for the network. Multiple\n examples are arranged as rows in this array, with columns containing\n the variables for each example.\n\n Returns\n -------\n r2 : float\n The R^2 correlation between the prediction of this netork and its\n target output.\n '''\n u = y - self.predict(x, **kwargs)\n v = y - y.mean()\n if w is None:\n w = np.ones_like(u)\n return 1 - (w * u * u).sum() / (w * v * v).sum()", "response": "Compute R^2 coefficient of determination of determination for a given labeled input."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nsave the state of this network to a pickle file on disk.", "response": "def save(self, filename_or_handle):\n '''Save the state of this network to a pickle file on disk.\n\n Parameters\n ----------\n filename_or_handle : str or file handle\n Save the state of this network to a pickle file. If this parameter\n is a string, it names the file where the pickle will be saved. If it\n is a file-like object, this object will be used for writing the\n pickle. If the filename ends in \".gz\" then the output will\n automatically be gzipped.\n '''\n if isinstance(filename_or_handle, util.basestring):\n opener = gzip.open if filename_or_handle.lower().endswith('.gz') else open\n handle = opener(filename_or_handle, 'wb')\n else:\n handle = filename_or_handle\n pickle.dump(self, handle, -1)\n if isinstance(filename_or_handle, util.basestring):\n handle.close()\n util.log('saved model to {}', filename_or_handle)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nloading a saved network from disk.", "response": "def load(cls, filename_or_handle):\n '''Load a saved network from disk.\n\n Parameters\n ----------\n filename_or_handle : str or file handle\n Load the state of this network from a pickle file. If this parameter\n is a string, it names the file where the pickle will be saved. If it\n is a file-like object, this object will be used for reading the\n pickle. If the filename ends in \".gz\" then the output will\n automatically be gunzipped.\n '''\n assert not isinstance(cls, Network), \\\n 'cannot load an instance! say instead: net = Network.load(source)'\n if isinstance(filename_or_handle, util.basestring):\n opener = gzip.open if filename_or_handle.lower().endswith('.gz') else open\n handle = opener(filename_or_handle, 'rb')\n else:\n handle = filename_or_handle\n model = pickle.load(handle)\n if isinstance(filename_or_handle, util.basestring):\n handle.close()\n util.log('loaded model from {}', filename_or_handle)\n return model"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a variable representing the regularized loss for this network.", "response": "def loss(self, **kwargs):\n '''Return a variable representing the regularized loss for this network.\n\n The regularized loss includes both the :ref:`loss computation `\n for the network as well as any :ref:`regularizers ` that\n are in place.\n\n Keyword arguments are passed directly to\n :func:`theanets.regularizers.from_kwargs`.\n\n Returns\n -------\n loss : Theano expression\n A Theano expression representing the loss of this network.\n '''\n regs = regularizers.from_kwargs(self, **kwargs)\n outputs, _ = self.build_graph(regs)\n return sum(l.weight * l(outputs) for l in self.losses) + \\\n sum(r.weight * r.loss(self.layers, outputs) for r in regs)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn expressions that should be computed to monitor training.", "response": "def monitors(self, **kwargs):\n '''Return expressions that should be computed to monitor training.\n\n Returns\n -------\n monitors : list of (name, expression) pairs\n A list of named monitor expressions to compute for this network.\n '''\n regs = regularizers.from_kwargs(self, **kwargs)\n outputs, _ = self.build_graph(regs)\n monitors = [('err', self.losses[0](outputs))]\n\n def matching(pattern):\n '''Yield all matching outputs or parameters from the graph.'''\n for name, expr in util.outputs_matching(outputs, pattern):\n yield name, expr\n for name, expr in util.params_matching(self.layers, pattern):\n yield name, expr\n\n def parse_levels(levels):\n '''Yield named monitor callables.'''\n if isinstance(levels, dict):\n levels = levels.items()\n if isinstance(levels, (int, float)):\n levels = [levels]\n for level in levels:\n if isinstance(level, (tuple, list)):\n label, call = level\n yield ':{}'.format(label), call\n if isinstance(level, (int, float)):\n def call(expr):\n return (expr < level).mean()\n yield '<{}'.format(level), call\n\n inputs = kwargs.get('monitors', {})\n if isinstance(inputs, dict):\n inputs = inputs.items()\n for pattern, levels in inputs:\n for name, expr in matching(pattern):\n for key, value in parse_levels(levels):\n monitors.append(('{}{}'.format(name, key), value(expr)))\n\n return monitors"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef updates(self, **kwargs):\n '''Return expressions to run as updates during network training.\n\n Returns\n -------\n updates : list of (parameter, expression) pairs\n A list of named parameter update expressions for this network.\n '''\n regs = regularizers.from_kwargs(self, **kwargs)\n _, updates = self.build_graph(regs)\n return updates", "response": "Return expressions to run as updates during training."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nnames of layer with one input.", "response": "def input_name(self):\n '''Name of layer input (for layers with one input).'''\n if len(self._input_shapes) != 1:\n raise util.ConfigurationError(\n 'expected one input for layer \"{}\", got {}'\n .format(self.name, self._input_shapes))\n return list(self._input_shapes)[0]"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nsizes of layer input.", "response": "def input_size(self):\n '''Size of layer input (for layers with one input).'''\n shape = self.input_shape\n if shape is None:\n raise util.ConfigurationError(\n 'undefined input size for layer \"{}\"'.format(self.name))\n return shape[-1]"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef output_size(self):\n '''Number of \"neurons\" in this layer's default output.'''\n shape = self.output_shape\n if shape is None:\n raise util.ConfigurationError(\n 'undefined output size for layer \"{}\"'.format(self.name))\n return shape[-1]", "response": "Number of neurons in this layer s default output."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ncreates Theano variables representing the outputs of this layer.", "response": "def connect(self, inputs):\n '''Create Theano variables representing the outputs of this layer.\n\n Parameters\n ----------\n inputs : dict of Theano expressions\n Symbolic inputs to this layer, given as a dictionary mapping string\n names to Theano expressions. Each string key should be of the form\n \"{layer_name}:{output_name}\" and refers to a specific output from\n a specific layer in the graph.\n\n Returns\n -------\n outputs : dict\n A dictionary mapping names to Theano expressions for the outputs\n from this layer.\n updates : sequence of (parameter, expression) tuples\n Updates that should be performed by a Theano function that computes\n something using this layer.\n '''\n outputs, updates = self.transform(inputs)\n # transform the outputs to be a list of ordered pairs if needed.\n if isinstance(outputs, dict):\n outputs = sorted(outputs.items())\n if isinstance(outputs, (TT.TensorVariable, SS.SparseVariable)):\n outputs = [('out', outputs)]\n outs = {self.full_name(name): expr for name, expr in outputs}\n return outs, updates"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nbinds this layer into a computation graph.", "response": "def bind(self, graph, reset=True, initialize=True):\n '''Bind this layer into a computation graph.\n\n This method is a wrapper for performing common initialization tasks. It\n calls :func:`resolve`, :func:`setup`, and :func:`log`.\n\n Parameters\n ----------\n graph : :class:`Network `\n A computation network in which this layer is to be bound.\n reset : bool, optional\n If ``True`` (the default), reset the resolved layers for this layer.\n initialize : bool, optional\n If ``True`` (the default), initialize the parameters for this layer\n by calling :func:`setup`.\n\n Raises\n ------\n theanets.util.ConfigurationError :\n If an input cannot be resolved.\n '''\n if reset:\n for k in self._input_shapes:\n self._input_shapes[k] = None\n for k in self._output_shapes:\n self._output_shapes[k] = None\n self.resolve_inputs(graph.layers)\n self.resolve_outputs()\n self.activate = activations.build(\n self.kwargs.get('activation', 'relu'), self)\n if initialize:\n self.setup()\n self.log()"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef resolve_inputs(self, layers):\n '''Resolve the names of inputs for this layer into shape tuples.\n\n Parameters\n ----------\n layers : list of :class:`Layer`\n A list of the layers that are available for resolving inputs.\n\n Raises\n ------\n theanets.util.ConfigurationError :\n If an input cannot be resolved.\n '''\n resolved = {}\n for name, shape in self._input_shapes.items():\n if shape is None:\n name, shape = self._resolve_shape(name, layers)\n resolved[name] = shape\n self._input_shapes = resolved", "response": "Resolve the names of inputs for this layer into shape tuples."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef resolve_outputs(self):\n '''Resolve the names of outputs for this layer into shape tuples.'''\n input_shape = None\n for i, shape in enumerate(self._input_shapes.values()):\n if i == 0:\n input_shape = shape\n if len(input_shape) != len(shape) or any(\n a is not None and b is not None and a != b\n for a, b in zip(input_shape[:-1], shape[:-1])):\n raise util.ConfigurationError(\n 'layer \"{}\" incompatible input shapes {}'\n .format(self.name, self._input_shapes))\n size = self.kwargs.get('size')\n shape = self.kwargs.get('shape')\n if shape is not None:\n pass\n elif size is not None:\n shape = tuple(input_shape[:-1]) + (size, )\n else:\n raise util.ConfigurationError(\n 'layer \"{}\" does not specify a size'.format(self.name))\n self._output_shapes['out'] = shape", "response": "Resolve the names of outputs for this layer into shape tuples."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef log(self):\n '''Log some information about this layer.'''\n inputs = ', '.join('\"{0}\" {1}'.format(*ns) for ns in self._input_shapes.items())\n util.log('layer {0.__class__.__name__} \"{0.name}\" {0.output_shape} {1} from {2}',\n self, getattr(self.activate, 'name', self.activate), inputs)\n util.log('learnable parameters: {}', self.log_params())", "response": "Log some information about this layer."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef log_params(self):\n '''Log information about this layer's parameters.'''\n total = 0\n for p in self.params:\n shape = p.get_value().shape\n util.log('parameter \"{}\" {}', p.name, shape)\n total += np.prod(shape)\n return total", "response": "Log information about this layer s parameters."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _resolve_shape(self, name, layers):\n '''Given a list of layers, find the layer output with the given name.\n\n Parameters\n ----------\n name : str\n Name of a layer to resolve.\n layers : list of :class:`theanets.layers.base.Layer`\n A list of layers to search in.\n\n Raises\n ------\n util.ConfigurationError :\n If there is no such layer, or if there are more than one.\n\n Returns\n -------\n name : str\n The fully-scoped name of the desired output.\n shape : tuple of None and/or int\n The shape of the named output.\n '''\n matches = [l for l in layers if name.split(':')[0] == l.name]\n if len(matches) != 1:\n raise util.ConfigurationError(\n 'layer \"{}\" cannot resolve \"{}\" using {}'\n .format(self.name, name, [l.name for l in layers]))\n name = name if ':' in name else matches[0].output_name\n return name, matches[0]._output_shapes[name.split(':')[1]]", "response": "Given a list of layers find the layer output with the given name."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef find(self, key):\n '''Get a shared variable for a parameter by name.\n\n Parameters\n ----------\n key : str or int\n The name of the parameter to look up, or the index of the parameter\n in our parameter list. These are both dependent on the\n implementation of the layer.\n\n Returns\n -------\n param : shared variable\n A shared variable containing values for the given parameter.\n\n Raises\n ------\n KeyError\n If a param with the given name does not exist.\n '''\n name = self._fmt(str(key))\n for i, p in enumerate(self._params):\n if key == i or name == p.name:\n return p\n raise KeyError(key)", "response": "Get a shared variable for a parameter by name."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef add_weights(self, name, nin, nout, mean=0, std=0, sparsity=0, diagonal=0):\n '''Helper method to create a new weight matrix.\n\n Parameters\n ----------\n name : str\n Name of the parameter to add.\n nin : int\n Size of \"input\" for this weight matrix.\n nout : int\n Size of \"output\" for this weight matrix.\n mean : float, optional\n Mean value for randomly-initialized weights. Defaults to 0.\n std : float, optional\n Standard deviation of initial matrix values. Defaults to\n :math:`1 / sqrt(n_i + n_o)`.\n sparsity : float, optional\n Fraction of weights to be set to zero. Defaults to 0.\n diagonal : float, optional\n Initialize weights to a matrix of zeros with this value along the\n diagonal. Defaults to None, which initializes all weights randomly.\n '''\n glorot = 1 / np.sqrt(nin + nout)\n m = self.kwargs.get(\n 'mean_{}'.format(name), self.kwargs.get('mean', mean))\n s = self.kwargs.get(\n 'std_{}'.format(name), self.kwargs.get('std', std or glorot))\n p = self.kwargs.get(\n 'sparsity_{}'.format(name), self.kwargs.get('sparsity', sparsity))\n d = self.kwargs.get(\n 'diagonal_{}'.format(name), self.kwargs.get('diagonal', diagonal))\n self._params.append(theano.shared(\n util.random_matrix(nin, nout, mean=m, std=s, sparsity=p,\n diagonal=d, rng=self.rng),\n name=self._fmt(name)))", "response": "Helper method to create a new weight matrix."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ncreates a specification dictionary for this layer.", "response": "def to_spec(self):\n '''Create a specification dictionary for this layer.\n\n Returns\n -------\n spec : dict\n A dictionary specifying the configuration of this layer.\n '''\n spec = dict(**self.kwargs)\n spec.update(\n form=self.__class__.__name__.lower(),\n name=self.name,\n activation=self.kwargs.get('activation', 'relu'),\n )\n return spec"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the ArgMax from C by returning the ArgMax from C by returning the ArgMax from C by returning the ArgMax from C by returning the ArgMax from C by returning the ArgMax from C by returning the ArgMax from C by returning the ArgMax from C by returning the ArgMax from C by returning the ArgMax from C by returning the ArgMax", "response": "def argmax(self, C):\n \"\"\"\n Returns the ArgMax from C by returning the\n (x_pos, y_pos, theta, scale) tuple\n\n >>> C = np.random.randn(10, 10, 5, 4)\n >>> x_pos, y_pos, theta, scale = mp.argmax(C)\n >>> C[x_pos][y_pos][theta][scale] = C.max()\n\n \"\"\"\n ind = np.absolute(C).argmax()\n return np.unravel_index(ind, C.shape)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef band(self, sf_0, B_sf, force=False):\n if sf_0 == 0.:\n return 1.\n elif self.pe.use_cache and not force:\n tag = str(sf_0) + '_' + str(B_sf)\n try:\n return self.cache['band'][tag]\n except:\n if self.pe.verbose>50: print('doing band cache for tag ', tag)\n self.cache['band'][tag] = self.band(sf_0, B_sf, force=True)\n return self.cache['band'][tag]\n else:\n # see http://en.wikipedia.org/wiki/Log-normal_distribution\n env = 1./self.f*np.exp(-.5*(np.log(self.f/sf_0)**2)/B_sf**2)\n return env", "response": "Returns the radial frequency envelope of a given preferred spatial frequency and bandwidth."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns the orientation of the object in the specified time - domain.", "response": "def orientation(self, theta, B_theta, force=False):\n \"\"\"\n Returns the orientation envelope:\n We use a von-Mises distribution on the orientation:\n - mean orientation is ``theta`` (in radians),\n - ``B_theta`` is the bandwidth (in radians). It is equal to the standard deviation of the Gaussian\n envelope which approximate the distribution for low bandwidths. The Half-Width at Half Height is\n given by approximately np.sqrt(2*B_theta_**2*np.log(2)).\n\n # selecting one direction, theta is the mean direction, B_theta the spread\n # we use a von-mises distribution on the orientation\n # see http://en.wikipedia.org/wiki/Von_Mises_distribution\n \"\"\"\n if B_theta is np.inf: # for large bandwidth, returns a strictly flat envelope\n enveloppe_orientation = 1.\n elif self.pe.use_cache and not force:\n tag = str(theta) + '_' + str(B_theta)\n try:\n return self.cache['orientation'][tag]\n except:\n if self.pe.verbose>50: print('doing orientation cache for tag ', tag)\n self.cache['orientation'][tag] = self.orientation(theta, B_theta, force=True)\n return self.cache['orientation'][tag]\n else: # non pathological case\n # As shown in:\n # http://www.csse.uwa.edu.au/~pk/research/matlabfns/PhaseCongruency/Docs/convexpl.html\n # this single bump allows (without the symmetric) to code both symmetric\n # and anti-symmetric parts in one shot.\n cos_angle = np.cos(self.f_theta-theta)\n enveloppe_orientation = np.exp(cos_angle/B_theta**2)\n return enveloppe_orientation"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef loggabor(self, x_pos, y_pos, sf_0, B_sf, theta, B_theta, preprocess=True):\n\n env = np.multiply(self.band(sf_0, B_sf), self.orientation(theta, B_theta))\n if not(x_pos==0.) and not(y_pos==0.): # bypass translation whenever none is needed\n env = env.astype(np.complex128) * self.trans(x_pos*1., y_pos*1.)\n if preprocess : env *= self.f_mask # retina processing\n # normalizing energy:\n env /= np.sqrt((np.abs(env)**2).mean())\n # in the case a a single bump (see ``orientation``), we should compensate the fact that the distribution gets complex:\n env *= np.sqrt(2.)\n return env", "response": "Returns the envelope of a loggabor with the given parameters"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn the image of a LogGabor with the given parameters", "response": "def loggabor_image(self, x_pos, y_pos, theta, sf_0, phase, B_sf, B_theta):\n \"\"\"\n Returns the image of a LogGabor\n\n Note that the convention for coordinates follows that of matrices:\n the origin is at the top left of the image, and coordinates are first\n the rows (vertical axis, going down) then the columns (horizontal axis,\n going right).\n\n \"\"\"\n\n FT_lg = self.loggabor(x_pos, y_pos, sf_0=sf_0, B_sf=B_sf, theta=theta, B_theta=B_theta)\n FT_lg = FT_lg * np.exp(1j * phase)\n return self.invert(FT_lg, full=False)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef from_file(self, ifile, codec='ascii'):\n if ifile.read(12) == b'ooBinaryFile':\n def bin2str(ifile):\n textlen = struct.unpack('>h', ifile.read(2))[0]\n # Single byte characters\n if textlen >= 0:\n return ifile.read(textlen).decode('ascii')\n # Multi byte characters have initial len -1 and then \\xff bytes\n elif textlen == -1:\n textlen = struct.unpack('>h', ifile.read(2))[0]\n data = ifile.read(textlen*2)\n # Hack to go from number to unicode in python3 and python2\n fun = unichr if 'unichr' in __builtins__ else chr\n charlist = (data[i:i+2] for i in range(0, len(data), 2))\n return u''.join(\n fun(struct.unpack('>h', i)[0]) for i in charlist)\n\n ifile.read(ord(ifile.read(1))) # skip oo type\n self.xmin = struct.unpack('>d', ifile.read(8))[0]\n self.xmax = struct.unpack('>d', ifile.read(8))[0]\n ifile.read(1) # skip \n self.tier_num = struct.unpack('>i', ifile.read(4))[0]\n for i in range(self.tier_num):\n tier_type = ifile.read(ord(ifile.read(1))).decode('ascii')\n name = bin2str(ifile)\n tier = Tier(0, 0, name=name, tier_type=tier_type)\n self.tiers.append(tier)\n tier.xmin = struct.unpack('>d', ifile.read(8))[0]\n tier.xmax = struct.unpack('>d', ifile.read(8))[0]\n nint = struct.unpack('>i', ifile.read(4))[0]\n for i in range(nint):\n x1 = struct.unpack('>d', ifile.read(8))[0]\n if tier.tier_type == 'IntervalTier':\n x2 = struct.unpack('>d', ifile.read(8))[0]\n text = bin2str(ifile)\n if tier.tier_type == 'IntervalTier':\n tier.intervals.append((x1, x2, text))\n elif tier.tier_type == 'TextTier':\n tier.intervals.append((x1, text))\n else:\n raise Exception('Tiertype does not exist.')\n else:\n def nn(ifile, pat):\n line = next(ifile).decode(codec)\n return pat.search(line).group(1)\n\n regfloat = re.compile('([\\d.]+)\\s*$', flags=re.UNICODE)\n regint = re.compile('([\\d]+)\\s*$', flags=re.UNICODE)\n regstr = re.compile('\"(.*)\"\\s*$', flags=re.UNICODE)\n # Skip the Headers and empty line\n next(ifile), next(ifile), next(ifile)\n self.xmin = float(nn(ifile, regfloat))\n self.xmax = float(nn(ifile, regfloat))\n # Skip \n line = next(ifile)\n short = line.strip() == b''\n self.tier_num = int(nn(ifile, regint))\n not short and next(ifile)\n for i in range(self.tier_num):\n not short and next(ifile) # skip item[]: and item[\\d]:\n tier_type = nn(ifile, regstr)\n name = nn(ifile, regstr)\n tier = Tier(0, 0, name=name, tier_type=tier_type)\n self.tiers.append(tier)\n tier.xmin = float(nn(ifile, regfloat))\n tier.xmax = float(nn(ifile, regfloat))\n for i in range(int(nn(ifile, regint))):\n not short and next(ifile) # skip intervals [\\d]\n x1 = float(nn(ifile, regfloat))\n if tier.tier_type == 'IntervalTier':\n x2 = float(nn(ifile, regfloat))\n t = nn(ifile, regstr)\n tier.intervals.append((x1, x2, t))\n elif tier.tier_type == 'TextTier':\n t = nn(ifile, regstr)\n tier.intervals.append((x1, t))", "response": "Read a single or multi - byte text grid from a file."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef sort_tiers(self, key=lambda x: x.name):\n self.tiers.sort(key=key)", "response": "Sort the tiers given the key function."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nadd an interval or text tier to the specified location.", "response": "def add_tier(self, name, tier_type='IntervalTier', number=None):\n \"\"\"Add an IntervalTier or a TextTier on the specified location.\n\n :param str name: Name of the tier, duplicate names is allowed.\n :param str tier_type: Type of the tier.\n :param int number: Place to insert the tier, when ``None`` the number\n is generated and the tier will be placed on the bottom.\n :returns: The created tier.\n :raises ValueError: If the number is out of bounds.\n \"\"\"\n if number is None:\n number = 1 if not self.tiers else len(self.tiers)+1\n elif number < 1 or number > len(self.tiers):\n raise ValueError('Number not in [1..{}]'.format(len(self.tiers)))\n elif tier_type not in Tier.P_TIERS:\n raise ValueError('tier_type has to be in {}'.format(self.P_TIERS))\n self.tiers.insert(number-1,\n Tier(self.xmin, self.xmax, name, tier_type))\n return self.tiers[number-1]"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nremove a tier from the cache.", "response": "def remove_tier(self, name_num):\n \"\"\"Remove a tier, when multiple tiers exist with that name only the\n first is removed.\n\n :param name_num: Name or number of the tier to remove.\n :type name_num: int or str\n :raises IndexError: If there is no tier with that number.\n \"\"\"\n if isinstance(name_num, int):\n del(self.tiers[name_num-1])\n else:\n self.tiers = [i for i in self.tiers if i.name != name_num]"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ngive a tier when multiple tiers exist with that name only the first one is returned.", "response": "def get_tier(self, name_num):\n \"\"\"Gives a tier, when multiple tiers exist with that name only the\n first is returned.\n\n :param name_num: Name or number of the tier to return.\n :type name_num: int or str\n :returns: The tier.\n :raises IndexError: If the tier doesn't exist.\n \"\"\"\n return self.tiers[name_num - 1] if isinstance(name_num, int) else\\\n [i for i in self.tiers if i.name == name_num][0]"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nwrite the object to a file.", "response": "def to_file(self, filepath, codec='utf-8', mode='normal'):\n \"\"\"Write the object to a file.\n\n :param str filepath: Path of the fil.\n :param str codec: Text encoding.\n :param string mode: Flag to for write mode, possible modes:\n 'n'/'normal', 's'/'short' and 'b'/'binary'\n \"\"\"\n self.tier_num = len(self.tiers)\n if mode in ['binary', 'b']:\n with open(filepath, 'wb') as f:\n def writebstr(s):\n try:\n bstr = s.encode('ascii')\n except UnicodeError:\n f.write(b'\\xff\\xff')\n bstr = b''.join(struct.pack('>h', ord(c)) for c in s)\n f.write(struct.pack('>h', len(s)))\n f.write(bstr)\n\n f.write(b'ooBinaryFile\\x08TextGrid')\n f.write(struct.pack('>d', self.xmin))\n f.write(struct.pack('>d', self.xmax))\n f.write(b'\\x01')\n f.write(struct.pack('>i', self.tier_num))\n for tier in self.tiers:\n f.write(chr(len(tier.tier_type)).encode('ascii'))\n f.write(tier.tier_type.encode('ascii'))\n writebstr(tier.name)\n f.write(struct.pack('>d', tier.xmin))\n f.write(struct.pack('>d', tier.xmax))\n ints = tier.get_all_intervals()\n f.write(struct.pack('>i', len(ints)))\n itier = tier.tier_type == 'IntervalTier'\n for c in ints:\n f.write(struct.pack('>d', c[0]))\n itier and f.write(struct.pack('>d', c[1]))\n writebstr(c[2 if itier else 1])\n elif mode in ['normal', 'n', 'short', 's']:\n with codecs.open(filepath, 'w', codec) as f:\n short = mode[0] == 's'\n\n def wrt(indent, prefix, value, ff=''):\n indent = 0 if short else indent\n prefix = '' if short else prefix\n if value is not None or not short:\n s = u'{{}}{{}}{}\\n'.format(ff)\n f.write(s.format(' '*indent, prefix, value))\n\n f.write(u'File type = \"ooTextFile\"\\n'\n u'Object class = \"TextGrid\"\\n\\n')\n wrt(0, u'xmin = ', self.xmin, '{:f}')\n wrt(0, u'xmax = ', self.xmax, '{:f}')\n wrt(0, u'tiers? ', u'', '{}')\n wrt(0, u'size = ', self.tier_num, '{:d}')\n wrt(0, u'item []:', None)\n for tnum, tier in enumerate(self.tiers, 1):\n wrt(4, u'item [{:d}]:'.format(tnum), None)\n wrt(8, u'class = ', tier.tier_type, '\"{}\"')\n wrt(8, u'name = ', tier.name, '\"{}\"')\n wrt(8, u'xmin = ', tier.xmin, '{:f}')\n wrt(8, u'xmax = ', tier.xmax, '{:f}')\n if tier.tier_type == 'IntervalTier':\n ints = tier.get_all_intervals()\n wrt(8, u'intervals: size = ', len(ints), '{:d}')\n for i, c in enumerate(ints):\n wrt(8, 'intervals [{:d}]:'.format(i+1), None)\n wrt(12, 'xmin = ', c[0], '{:f}')\n wrt(12, 'xmax = ', c[1], '{:f}')\n wrt(12, 'text = ', c[2].replace('\"', '\"\"'), '\"{}\"')\n elif tier.tier_type == 'TextTier':\n wrt(8, u'points: size = ', len(tier.intervals), '{:d}')\n for i, c in enumerate(tier.get_intervals()):\n wrt(8, 'points [{:d}]:'.format(i+1), None)\n wrt(12, 'number = ', c[0], '{:f}')\n wrt(12, 'mark = ', c[1].replace('\"', '\"\"'), '\"{}\"')\n else:\n raise Exception('Unknown mode')"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nconverts the object to an Eaf object.", "response": "def to_eaf(self, skipempty=True, pointlength=0.1):\n \"\"\"Convert the object to an pympi.Elan.Eaf object\n\n :param int pointlength: Length of respective interval from points in\n seconds\n :param bool skipempty: Skip the empty annotations\n :returns: :class:`pympi.Elan.Eaf` object\n :raises ImportError: If the Eaf module can't be loaded.\n :raises ValueError: If the pointlength is not strictly positive.\n \"\"\"\n from pympi.Elan import Eaf\n eaf_out = Eaf()\n if pointlength <= 0:\n raise ValueError('Pointlength should be strictly positive')\n for tier in self.get_tiers():\n eaf_out.add_tier(tier.name)\n for ann in tier.get_intervals(True):\n if tier.tier_type == 'TextTier':\n ann = (ann[0], ann[0]+pointlength, ann[1])\n if ann[2].strip() or not skipempty:\n eaf_out.add_annotation(tier.name, int(round(ann[0]*1000)),\n int(round(ann[1]*1000)), ann[2])\n return eaf_out"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nadding a point to the TextTier.", "response": "def add_point(self, point, value, check=True):\n \"\"\"Add a point to the TextTier\n\n :param int point: Time of the point.\n :param str value: Text of the point.\n :param bool check: Flag to check for overlap.\n :raises Exception: If overlap or wrong tiertype.\n \"\"\"\n if self.tier_type != 'TextTier':\n raise Exception('Tiertype must be TextTier.')\n if check and any(i for i in self.intervals if i[0] == point):\n raise Exception('No overlap is allowed')\n self.intervals.append((point, value))"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nadding an interval to the IntervalTier.", "response": "def add_interval(self, begin, end, value, check=True):\n \"\"\"Add an interval to the IntervalTier.\n\n :param float begin: Start time of the interval.\n :param float end: End time of the interval.\n :param str value: Text of the interval.\n :param bool check: Flag to check for overlap.\n :raises Exception: If overlap, begin > end or wrong tiertype.\n \"\"\"\n if self.tier_type != 'IntervalTier':\n raise Exception('Tiertype must be IntervalTier')\n if check:\n if any(i for i in self.intervals if begin < i[1] and end > i[0]):\n raise Exception('No overlap is allowed')\n if begin > end:\n raise Exception('Begin must be smaller then end')\n self.intervals.append((begin, end, value))"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nremoves an interval from the list of intervals.", "response": "def remove_interval(self, time):\n \"\"\"Remove an interval, if no interval is found nothing happens.\n\n :param int time: Time of the interval.\n :raises TierTypeException: If the tier is not a IntervalTier.\n \"\"\"\n if self.tier_type != 'IntervalTier':\n raise Exception('Tiertype must be IntervalTier.')\n self.intervals = [i for i in self.intervals\n if not(i[0] <= time and i[1] >= time)]"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nremove a point from the set of points.", "response": "def remove_point(self, time):\n \"\"\"Remove a point, if no point is found nothing happens.\n\n :param int time: Time of the point.\n :raises TierTypeException: If the tier is not a TextTier.\n \"\"\"\n if self.tier_type != 'TextTier':\n raise Exception('Tiertype must be TextTier.')\n self.intervals = [i for i in self.intervals if i[0] != time]"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ngiving all the intervals or points sorted.", "response": "def get_intervals(self, sort=False):\n \"\"\"Give all the intervals or points.\n\n :param bool sort: Flag for yielding the intervals or points sorted.\n :yields: All the intervals\n \"\"\"\n for i in sorted(self.intervals) if sort else self.intervals:\n yield i"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns the true list of intervals including the empty intervals.", "response": "def get_all_intervals(self):\n \"\"\"Returns the true list of intervals including the empty intervals.\"\"\"\n ints = sorted(self.get_intervals(True))\n if self.tier_type == 'IntervalTier':\n if not ints:\n ints.append((self.xmin, self.xmax, ''))\n else:\n if ints[0][0] > self.xmin:\n ints.insert(0, (self.xmin, ints[0][0], ''))\n if ints[-1][1] < self.xmax:\n ints.append((ints[-1][1], self.xmax, ''))\n p = ints[-1]\n for index, i in reversed(list(enumerate(ints[:-1], 1))):\n if p[0] - i[1] != 0:\n ints.insert(index, (i[1], p[0], ''))\n p = i\n return ints"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreading a. cha file and converts it to an ELAN object.", "response": "def eaf_from_chat(file_path, codec='ascii', extension='wav'):\n \"\"\"Reads a .cha file and converts it to an elan object. The functions tries\n to mimic the CHAT2ELAN program that comes with the CLAN package as close as\n possible. This function however converts to the latest ELAN file format\n since the library is designed for it. All CHAT headers will be added as\n Properties in the object and the headers that have a similar field in an\n Eaf file will be added there too. The file description of chat files can be\n found `here `_.\n\n :param str file_path: The file path of the .cha file.\n :param str codec: The codec, if the @UTF8 header is present it will choose\n utf-8, default is ascii. Older CHAT files don't have their encoding\n embedded in a header so you will probably need to choose some obscure\n ISO charset then.\n :param str extension: The extension of the media file.\n :throws StopIteration: If the file doesn't contain a @End header, thus\n inferring the file is broken.\n \"\"\"\n eafob = Eaf()\n eafob.add_linguistic_type('parent')\n eafob.add_linguistic_type(\n 'child', constraints='Symbolic_Association', timealignable=False)\n participantsdb = {}\n last_annotation = None\n with open(file_path, 'r') as chatin:\n while True:\n line = chatin.readline().strip().decode(codec)\n if line == '@UTF8': # Codec marker\n codec = 'utf8'\n continue\n elif line == '@End': # End of file marker\n break\n elif line.startswith('@') and line != '@Begin': # Header marker\n key, value = line.split(':\\t')\n eafob.add_property('{}:\\t'.format(key), value)\n if key == '@Languages':\n for language in value.split(','):\n eafob.add_language(language)\n elif key == '@Participants':\n for participant in value.split(','):\n splits = participant.strip().split(' ')\n splits = map(lambda x: x.replace('_', ' '), splits)\n if len(splits) == 2:\n participantsdb[splits[0]] = (None, splits[1])\n elif len(splits) == 3:\n participantsdb[splits[0]] = (splits[1], splits[2])\n elif key == '@ID':\n ids = map(lambda x: x.replace('_', ''), value.split('|'))\n eafob.add_tier(ids[2], part=participantsdb[ids[2]][0],\n language=ids[0])\n elif key == '@Media':\n media = value.split(',')\n eafob.add_linked_file(\n 'file://{}.{}'.format(media[0], extension))\n elif key == '@Transcriber:':\n for tier in eafob.get_tier_names():\n eafob.tiers[tier][2]['ANNOTATOR'] = value\n elif line.startswith('*'): # Main tier marker\n while len(line.split('\\x15')) != 3:\n line += chatin.readline().decode(codec).strip()\n for participant in participantsdb.keys():\n if line.startswith('*{}:'.format(participant)):\n splits = ''.join(line.split(':')[1:]).strip()\n utt, time, _ = splits.split('\\x15')\n time = map(int, time.split('_'))\n last_annotation = (participant, time[0], time[1], utt)\n eafob.add_annotation(*last_annotation)\n elif line.startswith('%'): # Dependant tier marker\n splits = line.split(':')\n name = '{}_{}'.format(last_annotation[0], splits[0][1:])\n if name not in eafob.get_tier_names():\n eafob.add_tier(name, 'child', last_annotation[0])\n eafob.add_ref_annotation(\n name, last_annotation[0], sum(last_annotation[1:3])/2,\n ''.join(splits[1:]).strip())\n return eafob"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nparsing an EAF file and return a new EAF object.", "response": "def parse_eaf(file_path, eaf_obj):\n \"\"\"Parse an EAF file\n\n :param str file_path: Path to read from, - for stdin.\n :param pympi.Elan.Eaf eaf_obj: Existing EAF object to put the data in.\n :returns: EAF object.\n \"\"\"\n if file_path == '-':\n file_path = sys.stdin\n # Annotation document\n try:\n tree_root = etree.parse(file_path).getroot()\n except etree.ParseError:\n raise Exception('Unable to parse eaf, can you open it in ELAN?')\n\n if tree_root.attrib['VERSION'] not in ['2.8', '2.7']:\n sys.stdout.write('Parsing unknown version of ELAN spec... '\n 'This could result in errors...\\n')\n eaf_obj.adocument.update(tree_root.attrib)\n del(eaf_obj.adocument['{http://www.w3.org/2001/XMLSchema-instance}noNamesp'\n 'aceSchemaLocation'])\n tier_number = 0\n for elem in tree_root:\n # Licence\n if elem.tag == 'LICENSE':\n eaf_obj.licenses.append((elem.text, elem.attrib['LICENSE_URL']))\n # Header\n if elem.tag == 'HEADER':\n eaf_obj.header.update(elem.attrib)\n for elem1 in elem:\n if elem1.tag == 'MEDIA_DESCRIPTOR':\n eaf_obj.media_descriptors.append(elem1.attrib)\n elif elem1.tag == 'LINKED_FILE_DESCRIPTOR':\n eaf_obj.linked_file_descriptors.append(elem1.attrib)\n elif elem1.tag == 'PROPERTY':\n eaf_obj.properties.append(\n (elem1.attrib['NAME'], elem1.text))\n # Time order\n elif elem.tag == 'TIME_ORDER':\n for elem1 in elem:\n tsid = elem1.attrib['TIME_SLOT_ID']\n tsnum = int(''.join(filter(str.isdigit, tsid)))\n if tsnum and tsnum > eaf_obj.maxts:\n eaf_obj.maxts = tsnum\n ts = elem1.attrib.get('TIME_VALUE', None)\n eaf_obj.timeslots[tsid] = ts if ts is None else int(ts)\n # Tier\n elif elem.tag == 'TIER':\n tier_id = elem.attrib['TIER_ID']\n align = {}\n ref = {}\n for elem1 in elem:\n if elem1.tag == 'ANNOTATION':\n for elem2 in elem1:\n if elem2.tag == 'ALIGNABLE_ANNOTATION':\n annot_id = elem2.attrib['ANNOTATION_ID']\n annot_num = int(''.join(\n filter(str.isdigit, annot_id)))\n if annot_num and annot_num > eaf_obj.maxaid:\n eaf_obj.maxaid = annot_num\n annot_start = elem2.attrib['TIME_SLOT_REF1']\n annot_end = elem2.attrib['TIME_SLOT_REF2']\n svg_ref = elem2.attrib.get('SVG_REF', None)\n align[annot_id] = (annot_start, annot_end,\n '' if not list(elem2)[0].text\n else list(elem2)[0].text,\n svg_ref)\n eaf_obj.annotations[annot_id] = tier_id\n elif elem2.tag == 'REF_ANNOTATION':\n annot_ref = elem2.attrib['ANNOTATION_REF']\n previous = elem2.attrib.get('PREVIOUS_ANNOTATION',\n None)\n annot_id = elem2.attrib['ANNOTATION_ID']\n annot_num = int(''.join(\n filter(str.isdigit, annot_id)))\n if annot_num and annot_num > eaf_obj.maxaid:\n eaf_obj.maxaid = annot_num\n svg_ref = elem2.attrib.get('SVG_REF', None)\n ref[annot_id] = (annot_ref,\n '' if not list(elem2)[0].text else\n list(elem2)[0].text,\n previous, svg_ref)\n eaf_obj.annotations[annot_id] = tier_id\n eaf_obj.tiers[tier_id] = (align, ref, elem.attrib, tier_number)\n tier_number += 1\n # Linguistic type\n elif elem.tag == 'LINGUISTIC_TYPE':\n eaf_obj.linguistic_types[elem.attrib['LINGUISTIC_TYPE_ID']] =\\\n elem.attrib\n # Locale\n elif elem.tag == 'LOCALE':\n eaf_obj.locales[elem.attrib['LANGUAGE_CODE']] =\\\n (elem.attrib.get('COUNTRY_CODE', None),\n elem.attrib.get('VARIANT', None))\n # Language\n elif elem.tag == 'LANGUAGE':\n eaf_obj.languages[elem.attrib['LANG_ID']] =\\\n (elem.attrib.get('LANG_DEF', None),\n elem.attrib.get('LANG_LABEL', None))\n # Constraint\n elif elem.tag == 'CONSTRAINT':\n eaf_obj.constraints[elem.attrib['STEREOTYPE']] =\\\n elem.attrib['DESCRIPTION']\n # Controlled vocabulary\n elif elem.tag == 'CONTROLLED_VOCABULARY':\n cv_id = elem.attrib['CV_ID']\n ext_ref = elem.attrib.get('EXT_REF', None)\n descriptions = []\n\n if 'DESCRIPTION' in elem.attrib:\n eaf_obj.languages['und'] = (\n 'http://cdb.iso.org/lg/CDB-00130975-001',\n 'undetermined (und)')\n descriptions.append(('und', elem.attrib['DESCRIPTION']))\n entries = {}\n for elem1 in elem:\n if elem1.tag == 'DESCRIPTION':\n descriptions.append((elem1.attrib['LANG_REF'], elem1.text))\n elif elem1.tag == 'CV_ENTRY':\n cve_value = (elem1.text, 'und',\n elem1.get('DESCRIPTION', None))\n entries['cveid{}'.format(len(entries))] = \\\n ([cve_value], elem1.attrib.get('EXT_REF', None))\n elif elem1.tag == 'CV_ENTRY_ML':\n cem_ext_ref = elem1.attrib.get('EXT_REF', None)\n cve_id = elem1.attrib['CVE_ID']\n cve_values = []\n for elem2 in elem1:\n if elem2.tag == 'CVE_VALUE':\n cve_values.append((elem2.text,\n elem2.attrib['LANG_REF'],\n elem2.get('DESCRIPTION', None)))\n entries[cve_id] = (cve_values, cem_ext_ref)\n eaf_obj.controlled_vocabularies[cv_id] =\\\n (descriptions, entries, ext_ref)\n # Lexicon ref\n elif elem.tag == 'LEXICON_REF':\n eaf_obj.lexicon_refs[elem.attrib['LEX_REF_ID']] = elem.attrib\n # External ref\n elif elem.tag == 'EXTERNAL_REF':\n eaf_obj.external_refs[elem.attrib['EXT_REF_ID']] = (\n elem.attrib['TYPE'], elem.attrib['VALUE'])"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nfunction to pretty print the xml.", "response": "def indent(el, level=0):\n \"\"\"Function to pretty print the xml, meaning adding tabs and newlines.\n\n :param ElementTree.Element el: Current element.\n :param int level: Current level.\n \"\"\"\n i = '\\n' + level * '\\t'\n if len(el):\n if not el.text or not el.text.strip():\n el.text = i+'\\t'\n if not el.tail or not el.tail.strip():\n el.tail = i\n for elem in el:\n indent(elem, level+1)\n if not el.tail or not el.tail.strip():\n el.tail = i\n else:\n if level and (not el.tail or not el.tail.strip()):\n el.tail = i"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nwriting an Eaf object to file.", "response": "def to_eaf(file_path, eaf_obj, pretty=True):\n \"\"\"Write an Eaf object to file.\n\n :param str file_path: Filepath to write to, - for stdout.\n :param pympi.Elan.Eaf eaf_obj: Object to write.\n :param bool pretty: Flag to set pretty printing.\n \"\"\"\n def rm_none(x):\n try: # Ugly hack to test if s is a string in py3 and py2\n basestring\n\n def isstr(s):\n return isinstance(s, basestring)\n except NameError:\n def isstr(s):\n return isinstance(s, str)\n return {k: v if isstr(v) else str(v) for k, v in x.items()\n if v is not None}\n # Annotation Document\n ADOCUMENT = etree.Element('ANNOTATION_DOCUMENT', eaf_obj.adocument)\n # Licence\n for m in eaf_obj.licenses:\n n = etree.SubElement(ADOCUMENT, 'LICENSE', {'LICENSE_URL': m[1]})\n n.text = m[0]\n # Header\n HEADER = etree.SubElement(ADOCUMENT, 'HEADER', eaf_obj.header)\n # Media descriptiors\n for m in eaf_obj.media_descriptors:\n etree.SubElement(HEADER, 'MEDIA_DESCRIPTOR', rm_none(m))\n # Linked file descriptors\n for m in eaf_obj.linked_file_descriptors:\n etree.SubElement(HEADER, 'LINKED_FILE_DESCRIPTOR', rm_none(m))\n # Properties\n for k, v in eaf_obj.properties:\n etree.SubElement(HEADER, 'PROPERTY', {'NAME': k}).text = str(v)\n # Time order\n TIME_ORDER = etree.SubElement(ADOCUMENT, 'TIME_ORDER')\n for t in sorted(eaf_obj.timeslots.items(), key=lambda x: int(x[0][2:])):\n etree.SubElement(TIME_ORDER, 'TIME_SLOT', rm_none(\n {'TIME_SLOT_ID': t[0], 'TIME_VALUE': t[1]}))\n # Tiers\n for t in sorted(eaf_obj.tiers.items(), key=lambda x: x[1][3]):\n tier = etree.SubElement(ADOCUMENT, 'TIER', rm_none(t[1][2]))\n for a in t[1][0].items():\n ann = etree.SubElement(tier, 'ANNOTATION')\n alan = etree.SubElement(ann, 'ALIGNABLE_ANNOTATION', rm_none(\n {'ANNOTATION_ID': a[0], 'TIME_SLOT_REF1': a[1][0],\n 'TIME_SLOT_REF2': a[1][1], 'SVG_REF': a[1][3]}))\n etree.SubElement(alan, 'ANNOTATION_VALUE').text = a[1][2]\n for a in t[1][1].items():\n ann = etree.SubElement(tier, 'ANNOTATION')\n rean = etree.SubElement(ann, 'REF_ANNOTATION', rm_none(\n {'ANNOTATION_ID': a[0], 'ANNOTATION_REF': a[1][0],\n 'PREVIOUS_ANNOTATION': a[1][2], 'SVG_REF': a[1][3]}))\n etree.SubElement(rean, 'ANNOTATION_VALUE').text = a[1][1]\n # Linguistic types\n for l in eaf_obj.linguistic_types.values():\n etree.SubElement(ADOCUMENT, 'LINGUISTIC_TYPE', rm_none(l))\n # Locales\n for lc, (cc, vr) in eaf_obj.locales.items():\n etree.SubElement(ADOCUMENT, 'LOCALE', rm_none(\n {'LANGUAGE_CODE': lc, 'COUNTRY_CODE': cc, 'VARIANT': vr}))\n # Languages\n for lid, (ldef, label) in eaf_obj.languages.items():\n etree.SubElement(ADOCUMENT, 'LANGUAGE', rm_none(\n {'LANG_ID': lid, 'LANG_DEF': ldef, 'LANG_LABEL': label}))\n # Constraints\n for l in eaf_obj.constraints.items():\n etree.SubElement(ADOCUMENT, 'CONSTRAINT', rm_none(\n {'STEREOTYPE': l[0], 'DESCRIPTION': l[1]}))\n # Controlled vocabularies\n for cvid, (descriptions, cv_entries, ext_ref) in\\\n eaf_obj.controlled_vocabularies.items():\n cv = etree.SubElement(ADOCUMENT, 'CONTROLLED_VOCABULARY',\n rm_none({'CV_ID': cvid, 'EXT_REF': ext_ref}))\n for lang_ref, description in descriptions:\n des = etree.SubElement(cv, 'DESCRIPTION', {'LANG_REF': lang_ref})\n if description:\n des.text = description\n for cveid, (values, ext_ref) in cv_entries.items():\n cem = etree.SubElement(cv, 'CV_ENTRY_ML', rm_none({\n 'CVE_ID': cveid, 'EXT_REF': ext_ref}))\n for value, lang_ref, description in values:\n val = etree.SubElement(cem, 'CVE_VALUE', rm_none({\n 'LANG_REF': lang_ref, 'DESCRIPTION': description}))\n val.text = value\n # Lexicon refs\n for l in eaf_obj.lexicon_refs.values():\n etree.SubElement(ADOCUMENT, 'LEXICON_REF', rm_none(l))\n # Exteral refs\n for eid, (etype, value) in eaf_obj.external_refs.items():\n etree.SubElement(ADOCUMENT, 'EXTERNAL_REF', rm_none(\n {'EXT_REF_ID': eid, 'TYPE': etype, 'VALUE': value}))\n\n if pretty:\n indent(ADOCUMENT)\n if file_path == '-':\n try:\n sys.stdout.write(etree.tostring(ADOCUMENT, encoding='unicode'))\n except LookupError:\n sys.stdout.write(etree.tostring(ADOCUMENT, encoding='UTF-8'))\n else:\n if os.access(file_path, os.F_OK):\n os.rename(file_path, '{}.bak'.format(file_path))\n etree.ElementTree(ADOCUMENT).write(\n file_path, xml_declaration=True, encoding='UTF-8')"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef add_annotation(self, id_tier, start, end, value='', svg_ref=None):\n if self.tiers[id_tier][1]:\n raise ValueError('Tier already contains ref annotations...')\n if start == end:\n raise ValueError('Annotation length is zero...')\n if start > end:\n raise ValueError('Annotation length is negative...')\n if start < 0:\n raise ValueError('Start is negative...')\n start_ts = self.generate_ts_id(start)\n end_ts = self.generate_ts_id(end)\n aid = self.generate_annotation_id()\n self.annotations[aid] = id_tier\n self.tiers[id_tier][0][aid] = (start_ts, end_ts, value, svg_ref)", "response": "Adds an annotation to the metadata."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nadds an entry to a controlled vocabulary.", "response": "def add_cv_entry(self, cv_id, cve_id, values, ext_ref=None):\n \"\"\"Add an entry to a controlled vocabulary.\n\n :param str cv_id: Name of the controlled vocabulary to add an entry.\n :param str cve_id: Name of the entry.\n :param list values: List of values of the form:\n ``(value, lang_ref, description)`` where description can be\n ``None``.\n :param str ext_ref: External reference.\n :throws KeyError: If there is no controlled vocabulary with that id.\n :throws ValueError: If a language in one of the entries doesn't exist.\n \"\"\"\n for value, lang_ref, description in values:\n if lang_ref not in self.languages:\n raise ValueError('Language not present: {}'.format(lang_ref))\n self.controlled_vocabularies[cv_id][1][cve_id] = (values, ext_ref)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef add_cv_description(self, cv_id, lang_ref, description=None):\n if lang_ref not in self.languages:\n raise ValueError('Language not present: {}'.format(lang_ref))\n self.controlled_vocabularies[cv_id][0].append((lang_ref, description))", "response": "Adds a description to a controlled vocabulary."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nadd an external reference.", "response": "def add_external_ref(self, eid, etype, value):\n \"\"\"Add an external reference.\n\n :param str eid: Name of the external reference.\n :param str etype: Type of the external reference, has to be in\n ``['iso12620', 'ecv', 'cve_id', 'lexen_id', 'resource_url']``.\n :param str value: Value of the external reference.\n :throws KeyError: if etype is not in the list of possible types.\n \"\"\"\n if etype not in self.ETYPES:\n raise KeyError('etype not in {}'.format(self.ETYPES))\n self.external_refs[eid] = (etype, value)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nadding a language. :param str lang_id: ID of the language. :param str lang_def: Definition of the language(preferably ISO-639-3). :param str lang_label: Label of the language.", "response": "def add_language(self, lang_id, lang_def=None, lang_label=None):\n \"\"\"Add a language.\n\n :param str lang_id: ID of the language.\n :param str lang_def: Definition of the language(preferably ISO-639-3).\n :param str lang_label: Label of the language.\n \"\"\"\n self.languages[lang_id] = (lang_def, lang_label)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nadds a new lexicon reference.", "response": "def add_lexicon_ref(self, lrid, name, lrtype, url, lexicon_id,\n lexicon_name, datcat_id=None, datcat_name=None):\n \"\"\"Add lexicon reference.\n\n :param str lrid: Lexicon reference internal ID.\n :param str name: Lexicon reference display name.\n :param str lrtype: Lexicon reference service type.\n :param str url: Lexicon reference service location\n :param str lexicon_id: Lexicon reference service id.\n :param str lexicon_name: Lexicon reference service name.\n :param str datacat_id: Lexicon reference identifier of data category.\n :param str datacat_name: Lexicon reference name of data category.\n \"\"\"\n self.lexicon_refs[lrid] = {\n 'LEX_REF_ID': lrid,\n 'NAME': name,\n 'TYPE': lrtype,\n 'URL': url,\n 'LEXICON_ID': lexicon_id,\n 'LEXICON_NAME': lexicon_name,\n 'DATCAT_ID': datcat_id,\n 'DATCAT_NAME': datcat_name\n }"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nadds a linguistic type to the list of linguistic types in the resource.", "response": "def add_linguistic_type(self, lingtype, constraints=None,\n timealignable=True, graphicreferences=False,\n extref=None, param_dict=None):\n \"\"\"Add a linguistic type.\n\n :param str lingtype: Name of the linguistic type.\n :param str constraints: Constraint name.\n :param bool timealignable: Flag for time alignable.\n :param bool graphicreferences: Flag for graphic references.\n :param str extref: External reference.\n :param dict param_dict: TAG attributes, when this is not ``None`` it\n will ignore all other options. Please only use\n dictionaries coming from the\n :func:`get_parameters_for_linguistic_type`\n :raises KeyError: If a constraint is not defined\n \"\"\"\n if param_dict:\n self.linguistic_types[lingtype] = param_dict\n else:\n if constraints:\n self.constraints[constraints]\n self.linguistic_types[lingtype] = {\n 'LINGUISTIC_TYPE_ID': lingtype,\n 'TIME_ALIGNABLE': str(timealignable).lower(),\n 'GRAPHIC_REFERENCES': str(graphicreferences).lower(),\n 'CONSTRAINTS': constraints}\n if extref is not None:\n self.linguistic_types[lingtype]['EXT_REF'] = extref"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef add_linked_file(self, file_path, relpath=None, mimetype=None,\n time_origin=None, ex_from=None):\n \"\"\"Add a linked file.\n\n :param str file_path: Path of the file.\n :param str relpath: Relative path of the file.\n :param str mimetype: Mimetype of the file, if ``None`` it tries to\n guess it according to the file extension which currently only works\n for wav, mpg, mpeg and xml.\n :param int time_origin: Time origin for the media file.\n :param str ex_from: Extracted from field.\n :raises KeyError: If mimetype had to be guessed and a non standard\n extension or an unknown mimetype.\n \"\"\"\n if mimetype is None:\n mimetype = self.MIMES[file_path.split('.')[-1]]\n self.media_descriptors.append({\n 'MEDIA_URL': file_path, 'RELATIVE_MEDIA_URL': relpath,\n 'MIME_TYPE': mimetype, 'TIME_ORIGIN': time_origin,\n 'EXTRACTED_FROM': ex_from})", "response": "Adds a linked file to the media_descriptors list."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef add_locale(self, language_code, country_code=None, variant=None):\n self.locales[language_code] = (country_code, variant)", "response": "Add a locale to the locale table."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef add_ref_annotation(self, id_tier, tier2, time, value='',\n prev=None, svg=None):\n \"\"\"Add a reference annotation.\n .. note:: When a timepoint matches two annotations the new reference\n annotation will reference to the first annotation. To circumvent this\n it's always safer to take the middle of the annotation you want to\n reference to.\n\n :param str id_tier: Name of the tier.\n :param str tier2: Tier of the referenced annotation.\n :param int time: Time of the referenced annotation.\n :param str value: Value of the annotation.\n :param str prev: Id of the previous annotation.\n :param str svg_ref: Svg reference.\n :raises KeyError: If the tier is non existent.\n :raises ValueError: If the tier already contains normal annotations or\n if there is no annotation in the tier on the time to reference to.\n \"\"\"\n if self.tiers[id_tier][0]:\n raise ValueError('This tier already contains normal annotations.')\n ann = None\n for aid, (begin, end, _, _) in self.tiers[tier2][0].items():\n begin = self.timeslots[begin]\n end = self.timeslots[end]\n if begin <= time and end >= time:\n ann = aid\n break\n if not ann:\n raise ValueError('There is no annotation to reference to.')\n aid = self.generate_annotation_id()\n self.annotations[aid] = id_tier\n self.tiers[id_tier][1][aid] = (ann, value, prev, svg)", "response": "Add a reference annotation to the given timepoint."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nadd a secondary linked file.", "response": "def add_secondary_linked_file(self, file_path, relpath=None, mimetype=None,\n time_origin=None, assoc_with=None):\n \"\"\"Add a secondary linked file.\n\n :param str file_path: Path of the file.\n :param str relpath: Relative path of the file.\n :param str mimetype: Mimetype of the file, if ``None`` it tries to\n guess it according to the file extension which currently only works\n for wav, mpg, mpeg and xml.\n :param int time_origin: Time origin for the media file.\n :param str assoc_with: Associated with field.\n :raises KeyError: If mimetype had to be guessed and a non standard\n extension or an unknown mimetype.\n \"\"\"\n if mimetype is None:\n mimetype = self.MIMES[file_path.split('.')[-1]]\n self.linked_file_descriptors.append({\n 'LINK_URL': file_path, 'RELATIVE_LINK_URL': relpath,\n 'MIME_TYPE': mimetype, 'TIME_ORIGIN': time_origin,\n 'ASSOCIATED_WITH': assoc_with})"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef add_tier(self, tier_id, ling='default-lt', parent=None, locale=None,\n part=None, ann=None, language=None, tier_dict=None):\n \"\"\"Add a tier. When no linguistic type is given and the default\n linguistic type is unavailable then the assigned linguistic type will\n be the first in the list.\n\n :param str tier_id: Name of the tier.\n :param str ling: Linguistic type, if the type is not available it will\n warn and pick the first available type.\n :param str parent: Parent tier name.\n :param str locale: Locale, if the locale is not present this option is\n ignored and the locale will not be set.\n :param str part: Participant.\n :param str ann: Annotator.\n :param str language: Language , if the language is not present this\n option is ignored and the language will not be set.\n :param dict tier_dict: TAG attributes, when this is not ``None`` it\n will ignore all other options. Please only use\n dictionaries coming from the\n :func:`get_parameters_for_tier`\n :raises ValueError: If the tier_id is empty\n \"\"\"\n if not tier_id:\n raise ValueError('Tier id is empty...')\n if ling not in self.linguistic_types:\n ling = sorted(self.linguistic_types.keys())[0]\n if locale and locale not in self.locales:\n locale = None\n if language and language not in self.languages:\n language = None\n if tier_dict is None:\n self.tiers[tier_id] = ({}, {}, {\n 'TIER_ID': tier_id,\n 'LINGUISTIC_TYPE_REF': ling,\n 'PARENT_REF': parent,\n 'PARTICIPANT': part,\n 'DEFAULT_LOCALE': locale,\n 'LANG_REF': language,\n 'ANNOTATOR': ann}, len(self.tiers))\n else:\n self.tiers[tier_id] = ({}, {}, tier_dict, len(self.tiers))", "response": "Add a tier to the list of available linguistics."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef clean_time_slots(self):\n ts = ((a[0], a[1]) for t in self.tiers.values() for a in t[0].values())\n for a in {a for b in ts for a in b} ^ set(self.timeslots):\n del(self.timeslots[a])", "response": "Clean up all unused timeslots."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef copy_tier(self, eaf_obj, tier_name):\n if tier_name in eaf_obj.get_tier_names():\n eaf_obj.remove_tier(tier_name)\n eaf_obj.add_tier(tier_name,\n tier_dict=self.get_parameters_for_tier(tier_name))\n for ann in self.get_annotation_data_for_tier(tier_name):\n eaf_obj.insert_annotation(tier_name, ann[0], ann[1], ann[2])", "response": "Copies a tier to another."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef create_gaps_and_overlaps_tier(self, tier1, tier2, tier_name=None,\n maxlen=-1, fast=False):\n \"\"\"Create a tier with the gaps and overlaps of the annotations.\n For types see :func:`get_gaps_and_overlaps`\n\n :param str tier1: Name of the first tier.\n :param str tier2: Name of the second tier.\n :param str tier_name: Name of the new tier, if ``None`` the name will\n be generated.\n :param int maxlen: Maximum length of gaps (skip longer ones), if ``-1``\n no maximum will be used.\n :param bool fast: Flag for using the fast method.\n :returns: List of gaps and overlaps of the form:\n ``[(type, start, end)]``.\n :raises KeyError: If a tier is non existent.\n :raises IndexError: If no annotations are available in the tiers.\n \"\"\"\n if tier_name is None:\n tier_name = '{}_{}_ftos'.format(tier1, tier2)\n self.add_tier(tier_name)\n ftos = []\n ftogen = self.get_gaps_and_overlaps2(tier1, tier2, maxlen) if fast\\\n else self.get_gaps_and_overlaps(tier1, tier2, maxlen)\n for fto in ftogen:\n ftos.append(fto)\n if fto[1]-fto[0] >= 1:\n self.add_annotation(tier_name, fto[0], fto[1], fto[2])\n self.clean_time_slots()\n return ftos", "response": "Create a new tier with the gaps and overlaps of the annotations."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef extract(self, start, end):\n from copy import deepcopy\n eaf_out = deepcopy(self)\n for t in eaf_out.get_tier_names():\n for ab, ae, value in eaf_out.get_annotation_data_for_tier(t):\n if ab > end or ae < start:\n eaf_out.remove_annotation(t, (start-end)//2, False)\n eaf_out.clean_time_slots()\n return eaf_out", "response": "Extracts the selected time frame as a new object."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef filter_annotations(self, tier, tier_name=None, filtin=None,\n filtex=None, regex=False, safe=False):\n \"\"\"Filter annotations in a tier using an exclusive and/or inclusive\n filter.\n\n :param str tier: Name of the tier.\n :param str tier_name: Name of the output tier, when ``None`` the name\n will be generated.\n :param list filtin: List of strings to be included, if None all\n annotations all is included.\n :param list filtex: List of strings to be excluded, if None no strings\n are excluded.\n :param bool regex: If this flag is set, the filters are seen as regex\n matches.\n :param bool safe: Ignore zero length annotations(when working with\n possible malformed data).\n :returns: Name of the created tier.\n :raises KeyError: If the tier is non existent.\n \"\"\"\n if tier_name is None:\n tier_name = '{}_filter'.format(tier)\n self.add_tier(tier_name)\n func = (lambda x, y: re.match(x, y)) if regex else lambda x, y: x == y\n for begin, end, value in self.get_annotation_data_for_tier(tier):\n if (filtin and not any(func(f, value) for f in filtin)) or\\\n (filtex and any(func(f, value) for f in filtex)):\n continue\n if not safe or end > begin:\n self.add_annotation(tier_name, begin, end, value)\n self.clean_time_slots()\n return tier_name", "response": "Filter the annotations in a tier using an exclusive and or inclusive filter."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef generate_annotation_id(self):\n if not self.maxaid:\n valid_anns = [int(''.join(filter(str.isdigit, a)))\n for a in self.timeslots]\n self.maxaid = max(valid_anns + [1])+1\n else:\n self.maxaid += 1\n return 'a{:d}'.format(self.maxaid)", "response": "Generate the next annotation id. This function is mainly used\n internally."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ngenerate the next timeslot id, this function is mainly used internally :param int time: Initial time to assign to the timeslot. :raises ValueError: If the time is negative.", "response": "def generate_ts_id(self, time=None):\n \"\"\"Generate the next timeslot id, this function is mainly used\n internally\n\n :param int time: Initial time to assign to the timeslot.\n :raises ValueError: If the time is negative.\n \"\"\"\n if time and time < 0:\n raise ValueError('Time is negative...')\n if not self.maxts:\n valid_ts = [int(''.join(filter(str.isdigit, a)))\n for a in self.timeslots]\n self.maxts = max(valid_ts + [1])+1\n else:\n self.maxts += 1\n ts = 'ts{:d}'.format(self.maxts)\n self.timeslots[ts] = time\n return ts"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_annotation_data_at_time(self, id_tier, time):\n if self.tiers[id_tier][1]:\n return self.get_ref_annotation_at_time(id_tier, time)\n anns = self.tiers[id_tier][0]\n return sorted([(self.timeslots[m[0]], self.timeslots[m[1]], m[2])\n for m in anns.values() if\n self.timeslots[m[0]] <= time and\n self.timeslots[m[1]] >= time])", "response": "Give the annotations at the given time."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ngive the annotation before a given time.", "response": "def get_annotation_data_after_time(self, id_tier, time):\n \"\"\"Give the annotation before a given time. When the tier contains\n reference annotations this will be returned, check\n :func:`get_ref_annotation_data_before_time` for the format. If an\n annotation overlaps with ``time`` that annotation will be returned.\n\n :param str id_tier: Name of the tier.\n :param int time: Time to get the annotation before.\n :raises KeyError: If the tier is non existent.\n \"\"\"\n if self.tiers[id_tier][1]:\n return self.get_ref_annotation_after_time(id_tier, time)\n befores = self.get_annotation_data_between_times(\n id_tier, time, self.get_full_time_interval()[1])\n if befores:\n return [min(befores, key=lambda x: x[0])]\n else:\n return []"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_annotation_data_before_time(self, id_tier, time):\n if self.tiers[id_tier][1]:\n return self.get_ref_annotation_before_time(id_tier, time)\n befores = self.get_annotation_data_between_times(id_tier, 0, time)\n if befores:\n return [max(befores, key=lambda x: x[0])]\n else:\n return []", "response": "Give the annotation before a given time."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns the list of annotations within the times.", "response": "def get_annotation_data_between_times(self, id_tier, start, end):\n \"\"\"Gives the annotations within the times.\n When the tier contains reference annotations this will be returned,\n check :func:`get_ref_annotation_data_between_times` for the format.\n\n :param str id_tier: Name of the tier.\n :param int start: Start time of the annotation.\n :param int end: End time of the annotation.\n :returns: List of annotations within that time.\n :raises KeyError: If the tier is non existent.\n \"\"\"\n if self.tiers[id_tier][1]:\n return self.get_ref_annotation_data_between_times(\n id_tier, start, end)\n anns = ((self.timeslots[a[0]], self.timeslots[a[1]], a[2])\n for a in self.tiers[id_tier][0].values())\n return sorted(a for a in anns if a[1] >= start and a[0] <= end)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_annotation_data_for_tier(self, id_tier):\n if self.tiers[id_tier][1]:\n return self.get_ref_annotation_data_for_tier(id_tier)\n a = self.tiers[id_tier][0]\n return [(self.timeslots[a[b][0]], self.timeslots[a[b][1]], a[b][2])\n for b in a]", "response": "Returns a list of annotations for the given tier."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_child_tiers_for(self, id_tier):\n self.tiers[id_tier]\n return [m for m in self.tiers if 'PARENT_REF' in self.tiers[m][2] and\n self.tiers[m][2]['PARENT_REF'] == id_tier]", "response": "Give all child tiers for a tier."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngives the full time interval of the file.", "response": "def get_full_time_interval(self):\n \"\"\"Give the full time interval of the file. Note that the real interval\n can be longer because the sound file attached can be longer.\n\n :returns: Tuple of the form: ``(min_time, max_time)``.\n \"\"\"\n return (0, 0) if not self.timeslots else\\\n (min(self.timeslots.values()), max(self.timeslots.values()))"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngives gaps and overlaps.", "response": "def get_gaps_and_overlaps(self, tier1, tier2, maxlen=-1):\n \"\"\"Give gaps and overlaps. The return types are shown in the table\n below. The string will be of the format: ``id_tiername_tiername``.\n\n .. note:: There is also a faster method: :func:`get_gaps_and_overlaps2`\n\n For example when a gap occurs between tier1 and tier2 and they are\n called ``speakerA`` and ``speakerB`` the annotation value of that gap\n will be ``G12_speakerA_speakerB``.\n\n | The gaps and overlaps are calculated using Heldner and Edlunds\n method found in:\n | *Heldner, M., & Edlund, J. (2010). Pauses, gaps and overlaps in\n conversations. Journal of Phonetics, 38(4), 555\u2013568.\n doi:10.1016/j.wocn.2010.08.002*\n\n +-----+---------------------------------------------+\n | id | Description |\n +=====+=============================================+\n | O12 | Overlap from tier1 to tier2 |\n +-----+---------------------------------------------+\n | O21 | Overlap from tier2 to tier1 |\n +-----+---------------------------------------------+\n | G12 | Between speaker gap from tier1 to tier2 |\n +-----+---------------------------------------------+\n | G21 | Between speaker gap from tier2 to tier1 |\n +-----+---------------------------------------------+\n | W12 | Within speaker overlap from tier2 in tier1 |\n +-----+---------------------------------------------+\n | W21 | Within speaker overlap from tier1 in tier2 |\n +-----+---------------------------------------------+\n | P1 | Pause for tier1 |\n +-----+---------------------------------------------+\n | P2 | Pause for tier2 |\n +-----+---------------------------------------------+\n\n :param str tier1: Name of the first tier.\n :param str tier2: Name of the second tier.\n :param int maxlen: Maximum length of gaps (skip longer ones), if ``-1``\n no maximum will be used.\n :yields: Tuples of the form ``[(start, end, type)]``.\n :raises KeyError: If a tier is non existent.\n :raises IndexError: If no annotations are available in the tiers.\n \"\"\"\n spkr1anns = sorted((self.timeslots[a[0]], self.timeslots[a[1]])\n for a in self.tiers[tier1][0].values())\n spkr2anns = sorted((self.timeslots[a[0]], self.timeslots[a[1]])\n for a in self.tiers[tier2][0].values())\n line1 = []\n\n def isin(x, lst):\n return False if\\\n len([i for i in lst if i[0] <= x and i[1] >= x]) == 0 else True\n minmax = (min(spkr1anns[0][0], spkr2anns[0][0]),\n max(spkr1anns[-1][1], spkr2anns[-1][1]))\n last = (1, minmax[0])\n for ts in range(*minmax):\n in1, in2 = isin(ts, spkr1anns), isin(ts, spkr2anns)\n if in1 and in2: # Both speaking\n if last[0] == 'B':\n continue\n ty = 'B'\n elif in1: # Only 1 speaking\n if last[0] == '1':\n continue\n ty = '1'\n elif in2: # Only 2 speaking\n if last[0] == '2':\n continue\n ty = '2'\n else: # None speaking\n if last[0] == 'N':\n continue\n ty = 'N'\n line1.append((last[0], last[1], ts))\n last = (ty, ts)\n line1.append((last[0], last[1], minmax[1]))\n for i in range(len(line1)):\n if line1[i][0] == 'N':\n if i != 0 and i < len(line1) - 1 and\\\n line1[i-1][0] != line1[i+1][0]:\n t = ('G12', tier1, tier2) if line1[i-1][0] == '1' else\\\n ('G21', tier2, tier1)\n if maxlen == -1 or abs(line1[i][1]-line1[i][2]) < maxlen:\n yield (line1[i][1], line1[i][2]-1, '_'.join(t))\n else:\n t = ('P1', tier1) if line1[i-1][0] == '1' else\\\n ('P2', tier2)\n if maxlen == -1 or abs(line1[i][1]-line1[i][2]) < maxlen:\n yield (line1[i][1], line1[i][2]-1, '_'.join(t))\n elif line1[i][0] == 'B':\n if i != 0 and i < len(line1) - 1 and\\\n line1[i-1][0] != line1[i+1][0]:\n t = ('O12', tier1, tier2) if line1[i-1][0] == '1' else\\\n ('O21', tier2, tier1)\n yield (line1[i][1], line1[i][2]-1, '_'.join(t))\n else:\n t = ('W12', tier1, tier2) if line1[i-1][0] == '1' else\\\n ('W21', tier2, tier1)\n yield (line1[i][1], line1[i][2]-1, '_'.join(t))"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_gaps_and_overlaps2(self, tier1, tier2, maxlen=-1):\n ad = sorted(((a, i+1) for i, t in enumerate([tier1, tier2]) for a in\n self.get_annotation_data_for_tier(t)), reverse=True)\n if ad:\n last = (lambda x: (x[0][0], x[0][1], x[1]))(ad.pop())\n\n def thr(x, y):\n return maxlen == -1 or abs(x-y) < maxlen\n while ad:\n (begin, end, _), current = ad.pop()\n if last[2] == current and thr(begin, last[1]):\n yield (last[1], begin, 'P{}'.format(current))\n elif last[0] < begin and last[1] > end:\n yield (begin, end, 'W{}{}'.format(last[2], current))\n continue\n elif last[1] > begin:\n yield (begin, last[1], 'O{}{}'.format(last[2], current))\n elif last[1] < begin and thr(begin, last[1]):\n yield (last[1], begin, 'G{}{}'.format(last[2], current))\n last = (begin, end, current)", "response": "A generator function that yields tuples of the first and last gaps and overlapped gaps."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngives the ref annotations at the given time of the form [ begin end value refvalue ]", "response": "def get_ref_annotation_at_time(self, tier, time):\n \"\"\"Give the ref annotations at the given time of the form\n ``[(start, end, value, refvalue)]``\n\n :param str tier: Name of the tier.\n :param int time: Time of the annotation of the parent.\n :returns: List of annotations at that time.\n :raises KeyError: If the tier is non existent.\n \"\"\"\n bucket = []\n for aid, (ref, value, _, _) in self.tiers[tier][1].items():\n begin, end, rvalue, _ = self.tiers[self.annotations[ref]][0][ref]\n begin = self.timeslots[begin]\n end = self.timeslots[end]\n if begin <= time and end >= time:\n bucket.append((begin, end, value, rvalue))\n return bucket"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ngive the ref annotation after a time.", "response": "def get_ref_annotation_data_after_time(self, id_tier, time):\n \"\"\"Give the ref annotation after a time. If an annotation overlaps\n with `ktime`` that annotation will be returned.\n\n :param str id_tier: Name of the tier.\n :param int time: Time to get the annotation after.\n :returns: Annotation after that time in a list\n :raises KeyError: If the tier is non existent.\n \"\"\"\n befores = self.get_ref_annotation_data_between_times(\n id_tier, time, self.get_full_time_interval())\n if befores:\n return [min(befores, key=lambda x: x[0])]\n else:\n return []"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_ref_annotation_data_before_time(self, id_tier, time):\n befores = self.get_ref_annotation_data_between_times(id_tier, 0, time)\n if befores:\n return [max(befores, key=lambda x: x[0])]\n else:\n return []", "response": "Give the ref annotation before a time."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngiving the ref annotations between times of the form [ start end value refvalue )", "response": "def get_ref_annotation_data_between_times(self, id_tier, start, end):\n \"\"\"Give the ref annotations between times of the form\n ``[(start, end, value, refvalue)]``\n\n :param str tier: Name of the tier.\n :param int start: End time of the annotation of the parent.\n :param int end: Start time of the annotation of the parent.\n :returns: List of annotations at that time.\n :raises KeyError: If the tier is non existent.\n \"\"\"\n bucket = []\n for aid, (ref, value, _, _) in self.tiers[id_tier][1].items():\n begin, end, rvalue, _ = self.tiers[self.annotations[ref]][0][ref]\n begin = self.timeslots[begin]\n end = self.timeslots[end]\n if begin <= end and end >= begin:\n bucket.append((begin, end, value, rvalue))\n return bucket"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ngive a list of all reference annotations of the form : start end value refvalue", "response": "def get_ref_annotation_data_for_tier(self, id_tier):\n \"\"\"\"Give a list of all reference annotations of the form:\n ``[(start, end, value, refvalue)]``\n\n :param str id_tier: Name of the tier.\n :raises KeyError: If the tier is non existent.\n :returns: Reference annotations within that tier.\n \"\"\"\n bucket = []\n for aid, (ref, value, prev, _) in self.tiers[id_tier][1].items():\n refann = self.get_parent_aligned_annotation(ref)\n bucket.append((self.timeslots[refann[0]],\n self.timeslots[refann[1]], value, refann[2]))\n return bucket"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngiving the aligment annotation that a reference annotation belongs to directly or indirectly through other reference annotations.", "response": "def get_parent_aligned_annotation(self, ref_id):\n \"\"\"\" Give the aligment annotation that a reference annotation belongs to directly, or indirectly through other\n reference annotations.\n :param str ref_id: Id of a reference annotation.\n :raises KeyError: If no annotation exists with the id or if it belongs to an alignment annotation.\n :returns: The alignment annotation at the end of the reference chain.\n \"\"\"\n parentTier = self.tiers[self.annotations[ref_id]]\n while \"PARENT_REF\" in parentTier[2] and len(parentTier[2]) > 0:\n ref_id = parentTier[1][ref_id][0]\n parentTier = self.tiers[self.annotations[ref_id]]\n\n return parentTier[0][ref_id]"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_tier_ids_for_linguistic_type(self, ling_type, parent=None):\n return [t for t in self.tiers if\n self.tiers[t][2]['LINGUISTIC_TYPE_REF'] == ling_type and\n (parent is None or self.tiers[t][2]['PARENT_REF'] == parent)]", "response": "Give a list of all tiers matching a linguistic type."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef insert_annotation(self, id_tier, start, end, value='', svg_ref=None):\n return self.add_annotation(id_tier, start, end, value, svg_ref)", "response": "Insert an annotation into the cache."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef insert_ref_annotation(self, id_tier, tier2, time, value='',\n prev=None, svg=None):\n \"\"\".. deprecated:: 1.2\n\n Use :func:`add_ref_annotation` instead.\n \"\"\"\n return self.add_ref_annotation(id_tier, tier2, time, value, prev, svg)", "response": "Insert a new ref annotation into the cache."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef merge_tiers(self, tiers, tiernew=None, gapt=0, sep='_', safe=False):\n if tiernew is None:\n tiernew = u'{}_merged'.format('_'.join(tiers))\n self.add_tier(tiernew)\n aa = [(sys.maxsize, sys.maxsize, None)] + sorted((\n a for t in tiers for a in self.get_annotation_data_for_tier(t)),\n reverse=True)\n l = None\n while aa:\n begin, end, value = aa.pop()\n if l is None:\n l = [begin, end, [value]]\n elif begin - l[1] >= gapt:\n if not safe or l[1] > l[0]:\n self.add_annotation(tiernew, l[0], l[1], sep.join(l[2]))\n l = [begin, end, [value]]\n else:\n if end > l[1]:\n l[1] = end\n l[2].append(value)\n return tiernew", "response": "Merge tiers into a new tier and optionally add annotations to the new tier."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nremove all annotations from a tier", "response": "def remove_all_annotations_from_tier(self, id_tier, clean=True):\n \"\"\"remove all annotations from a tier\n\n :param str id_tier: Name of the tier.\n :raises KeyError: If the tier is non existent.\n \"\"\"\n for aid in self.tiers[id_tier][0]:\n del(self.annotations[aid])\n for aid in self.tiers[id_tier][1]:\n del(self.annotations[aid])\n\n self.tiers[id_tier][0].clear()\n self.tiers[id_tier][1].clear()\n if clean:\n self.clean_time_slots()"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nremove an annotation in a tier.", "response": "def remove_annotation(self, id_tier, time, clean=True):\n \"\"\"Remove an annotation in a tier, if you need speed the best thing is\n to clean the timeslots after the last removal. When the tier contains\n reference annotations :func:`remove_ref_annotation` will be executed\n instead.\n\n :param str id_tier: Name of the tier.\n :param int time: Timepoint within the annotation.\n :param bool clean: Flag to clean the timeslots afterwards.\n :raises KeyError: If the tier is non existent.\n :returns: Number of removed annotations.\n \"\"\"\n if self.tiers[id_tier][1]:\n return self.remove_ref_annotation(id_tier, time, clean)\n removed = 0\n for b in [a for a in self.tiers[id_tier][0].items() if\n self.timeslots[a[1][0]] <= time and\n self.timeslots[a[1][1]] >= time]:\n del(self.tiers[id_tier][0][b[0]])\n del(self.annotations[b[0]])\n removed += 1\n if clean:\n self.clean_time_slots()\n return removed"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef remove_cv_description(self, cv_id, lang_ref):\n for i, (l, d) in reversed(enumerate(\n self.controlled_vocabularies[cv_id][1])):\n if l == lang_ref:\n del(self.controlled_vocabularies[cv_id][1][i])", "response": "Removes a description from a controlled vocabulary."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nremove all licenses matching both key and value.", "response": "def remove_license(self, name=None, url=None):\n \"\"\"Remove all licenses matching both key and value.\n\n :param str name: Name of the license.\n :param str url: URL of the license.\n \"\"\"\n for k, v in self.licenses[:]:\n if (name is None or name == k) and (url is None or url == v):\n del(self.licenses[self.licenses.index((k, v))])"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nremoves all linked files that match all the criteria.", "response": "def remove_linked_files(self, file_path=None, relpath=None, mimetype=None,\n time_origin=None, ex_from=None):\n \"\"\"Remove all linked files that match all the criteria, criterias that\n are ``None`` are ignored.\n\n :param str file_path: Path of the file.\n :param str relpath: Relative filepath.\n :param str mimetype: Mimetype of the file.\n :param int time_origin: Time origin.\n :param str ex_from: Extracted from.\n \"\"\"\n for attrib in self.media_descriptors[:]:\n if file_path is not None and attrib['MEDIA_URL'] != file_path:\n continue\n if relpath is not None and attrib['RELATIVE_MEDIA_URL'] != relpath:\n continue\n if mimetype is not None and attrib['MIME_TYPE'] != mimetype:\n continue\n if time_origin is not None and\\\n attrib['TIME_ORIGIN'] != time_origin:\n continue\n if ex_from is not None and attrib['EXTRACTED_FROM'] != ex_from:\n continue\n del(self.media_descriptors[self.media_descriptors.index(attrib)])"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nremove a property from the resource table.", "response": "def remove_property(self, key=None, value=None):\n \"\"\"Remove all properties matching both key and value.\n\n :param str key: Key of the property.\n :param str value: Value of the property.\n \"\"\"\n for k, v in self.properties[:]:\n if (key is None or key == k) and (value is None or value == v):\n del(self.properties[self.properties.index((k, v))])"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef remove_ref_annotation(self, id_tier, time):\n removed = 0\n bucket = []\n for aid, (ref, value, _, _) in self.tiers[id_tier][1].items():\n begin, end, rvalue, _ = self.tiers[self.annotations[ref]][0][ref]\n begin = self.timeslots[begin]\n end = self.timeslots[end]\n if begin <= time and end >= time:\n removed += 1\n bucket.append(aid)\n for aid in bucket:\n del(self.tiers[id_tier][1][aid])\n return removed", "response": "Removes a reference annotation."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nremove all secondary linked files that match all the criteria.", "response": "def remove_secondary_linked_files(self, file_path=None, relpath=None,\n mimetype=None, time_origin=None,\n assoc_with=None):\n \"\"\"Remove all secondary linked files that match all the criteria,\n criterias that are ``None`` are ignored.\n\n :param str file_path: Path of the file.\n :param str relpath: Relative filepath.\n :param str mimetype: Mimetype of the file.\n :param int time_origin: Time origin.\n :param str ex_from: Extracted from.\n \"\"\"\n for attrib in self.linked_file_descriptors[:]:\n if file_path is not None and attrib['LINK_URL'] != file_path:\n continue\n if relpath is not None and attrib['RELATIVE_LINK_URL'] != relpath:\n continue\n if mimetype is not None and attrib['MIME_TYPE'] != mimetype:\n continue\n if time_origin is not None and\\\n attrib['TIME_ORIGIN'] != time_origin:\n continue\n if assoc_with is not None and\\\n attrib['ASSOCIATED_WITH'] != assoc_with:\n continue\n del(self.linked_file_descriptors[\n self.linked_file_descriptors.index(attrib)])"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nremoves a tier from the cache.", "response": "def remove_tier(self, id_tier, clean=True):\n \"\"\"Remove a tier.\n\n :param str id_tier: Name of the tier.\n :param bool clean: Flag to also clean the timeslots.\n :raises KeyError: If tier is non existent.\n \"\"\"\n del(self.tiers[id_tier])\n if clean:\n self.clean_time_slots()"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nremoves multiple tiers from the cache.", "response": "def remove_tiers(self, tiers):\n \"\"\"Remove multiple tiers, note that this is a lot faster then removing\n them individually because of the delayed cleaning of timeslots.\n\n :param list tiers: Names of the tier to remove.\n :raises KeyError: If a tier is non existent.\n \"\"\"\n for a in tiers:\n self.remove_tier(a, clean=False)\n self.clean_time_slots()"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nrename a tier. Note that this renames also the child tiers that have the tier as a parent. :param str id_from: Original name of the tier. :param str id_to: Target name of the tier. :throws KeyError: If the tier doesnt' exist.", "response": "def rename_tier(self, id_from, id_to):\n \"\"\"Rename a tier. Note that this renames also the child tiers that have\n the tier as a parent.\n\n :param str id_from: Original name of the tier.\n :param str id_to: Target name of the tier.\n :throws KeyError: If the tier doesnt' exist.\n \"\"\"\n childs = self.get_child_tiers_for(id_from)\n self.tiers[id_to] = self.tiers.pop(id_from)\n self.tiers[id_to][2]['TIER_ID'] = id_to\n for child in childs:\n self.tiers[child][2]['PARENT_REF'] = id_to"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nshifts all annotations in time.", "response": "def shift_annotations(self, time):\n \"\"\"Shift all annotations in time. Annotations that are in the beginning\n and a left shift is applied can be squashed or discarded.\n\n :param int time: Time shift width, negative numbers make a left shift.\n :returns: Tuple of a list of squashed annotations and a list of removed\n annotations in the format: ``(tiername, start, end, value)``.\n \"\"\"\n total_re = []\n total_sq = []\n for name, tier in self.tiers.items():\n squashed = []\n for aid, (begin, end, value, _) in tier[0].items():\n if self.timeslots[end]+time <= 0:\n squashed.append((name, aid))\n elif self.timeslots[begin]+time < 0:\n total_sq.append((name, self.timeslots[begin],\n self.timeslots[end], value))\n self.timeslots[begin] = 0\n else:\n self.timeslots[begin] += time\n self.timeslots[end] += time\n for name, aid in squashed:\n start, end, value, _ = self.tiers[name][0][aid]\n del(self.tiers[name][0][aid])\n del(self.annotations[aid])\n total_re.append(\n (name, self.timeslots[start], self.timeslots[end], value))\n return total_sq, total_re"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef to_textgrid(self, filtin=[], filtex=[], regex=False):\n from pympi.Praat import TextGrid\n _, end = self.get_full_time_interval()\n tgout = TextGrid(xmax=end/1000.0)\n func = (lambda x, y: re.match(x, y)) if regex else lambda x, y: x == y\n for tier in self.tiers:\n if (filtin and not any(func(f, tier) for f in filtin)) or\\\n (filtex and any(func(f, tier) for f in filtex)):\n continue\n ctier = tgout.add_tier(tier)\n for intv in self.get_annotation_data_for_tier(tier):\n try:\n ctier.add_interval(intv[0]/1000.0, intv[1]/1000.0, intv[2])\n except:\n pass\n return tgout", "response": "Convert the object to a TextGrid object."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef main():\n\n import optparse\n import sys\n import codecs\n import locale\n import six\n from .algorithm import get_display\n\n parser = optparse.OptionParser()\n\n parser.add_option('-e', '--encoding',\n dest='encoding',\n default='utf-8',\n type='string',\n help='Text encoding (default: utf-8)')\n\n parser.add_option('-u', '--upper-is-rtl',\n dest='upper_is_rtl',\n default=False,\n action='store_true',\n help=\"Treat upper case chars as strong 'R' \"\n 'for debugging (default: False).')\n\n parser.add_option('-d', '--debug',\n dest='debug',\n default=False,\n action='store_true',\n help=\"Output to stderr steps taken with the algorithm\")\n\n parser.add_option('-b', '--base-dir',\n dest='base_dir',\n default=None,\n type='string',\n help=\"Override base direction [L|R]\")\n\n options, rest = parser.parse_args()\n\n if options.base_dir and options.base_dir not in 'LR':\n parser.error('option -b can be L or R')\n\n # allow unicode in sys.stdout.write\n if six.PY2:\n sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout)\n\n if rest:\n lines = rest\n else:\n lines = sys.stdin\n\n for line in lines:\n display = get_display(line, options.encoding, options.upper_is_rtl,\n options.base_dir, options.debug)\n # adjust the encoding as unicode, to match the output encoding\n if not isinstance(display, six.text_type):\n display = display.decode(options.encoding)\n\n six.print_(display, end='')", "response": "This function will be used to create the console script"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ndisplays debug information for the storage", "response": "def debug_storage(storage, base_info=False, chars=True, runs=False):\n \"Display debug information for the storage\"\n\n import codecs\n import locale\n import sys\n\n if six.PY2:\n stderr = codecs.getwriter(locale.getpreferredencoding())(sys.stderr)\n else:\n stderr = sys.stderr\n\n caller = inspect.stack()[1][3]\n stderr.write('in %s\\n' % caller)\n\n if base_info:\n stderr.write(u' base level : %d\\n' % storage['base_level'])\n stderr.write(u' base dir : %s\\n' % storage['base_dir'])\n\n if runs:\n stderr.write(u' runs : %s\\n' % list(storage['runs']))\n\n if chars:\n output = u' Chars : '\n for _ch in storage['chars']:\n if _ch != '\\n':\n output += _ch['ch']\n else:\n output += 'C'\n stderr.write(output + u'\\n')\n\n output = u' Res. levels : %s\\n' % u''.join(\n [six.text_type(_ch['level']) for _ch in storage['chars']])\n stderr.write(output)\n\n _types = [_ch['type'].ljust(3) for _ch in storage['chars']]\n\n for i in range(3):\n if i:\n output = u' %s\\n'\n else:\n output = u' Res. types : %s\\n'\n stderr.write(output % u''.join([_t[i] for _t in _types]))"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_base_level(text, upper_is_rtl=False):\n\n base_level = None\n\n prev_surrogate = False\n # P2\n for _ch in text:\n # surrogate in case of ucs2\n if _IS_UCS2 and (_SURROGATE_MIN <= ord(_ch) <= _SURROGATE_MAX):\n prev_surrogate = _ch\n continue\n elif prev_surrogate:\n _ch = prev_surrogate + _ch\n prev_surrogate = False\n\n # treat upper as RTL ?\n if upper_is_rtl and _ch.isupper():\n base_level = 1\n break\n\n bidi_type = bidirectional(_ch)\n\n if bidi_type in ('AL', 'R'):\n base_level = 1\n break\n\n elif bidi_type == 'L':\n base_level = 0\n break\n\n # P3\n if base_level is None:\n base_level = 0\n\n return base_level", "response": "Get the base embedding level of a text. Returns 0 for LTR 1 for RTL."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_embedding_levels(text, storage, upper_is_rtl=False, debug=False):\n\n prev_surrogate = False\n base_level = storage['base_level']\n\n # preset the storage's chars\n for _ch in text:\n if _IS_UCS2 and (_SURROGATE_MIN <= ord(_ch) <= _SURROGATE_MAX):\n prev_surrogate = _ch\n continue\n elif prev_surrogate:\n _ch = prev_surrogate + _ch\n prev_surrogate = False\n\n if upper_is_rtl and _ch.isupper():\n bidi_type = 'R'\n else:\n bidi_type = bidirectional(_ch)\n\n storage['chars'].append({\n 'ch': _ch,\n 'level': base_level,\n 'type': bidi_type,\n 'orig': bidi_type\n })\n if debug:\n debug_storage(storage, base_info=True)", "response": "Get the base embedding level and direction of the text."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef explicit_embed_and_overrides(storage, debug=False):\n overflow_counter = almost_overflow_counter = 0\n directional_override = 'N'\n levels = deque()\n\n # X1\n embedding_level = storage['base_level']\n\n for _ch in storage['chars']:\n bidi_type = _ch['type']\n\n level_func, override = X2_X5_MAPPINGS.get(bidi_type, (None, None))\n\n if level_func:\n # So this is X2 to X5\n # if we've past EXPLICIT_LEVEL_LIMIT, note it and do nothing\n\n if overflow_counter != 0:\n overflow_counter += 1\n continue\n\n new_level = level_func(embedding_level)\n if new_level < EXPLICIT_LEVEL_LIMIT:\n levels.append((embedding_level, directional_override))\n embedding_level, directional_override = new_level, override\n\n elif embedding_level == EXPLICIT_LEVEL_LIMIT - 2:\n # The new level is invalid, but a valid level can still be\n # achieved if this level is 60 and we encounter an RLE or\n # RLO further on. So record that we 'almost' overflowed.\n almost_overflow_counter += 1\n\n else:\n overflow_counter += 1\n else:\n # X6\n if bidi_type not in X6_IGNORED:\n _ch['level'] = embedding_level\n if directional_override != 'N':\n _ch['type'] = directional_override\n\n # X7\n elif bidi_type == 'PDF':\n if overflow_counter:\n overflow_counter -= 1\n elif almost_overflow_counter and \\\n embedding_level != EXPLICIT_LEVEL_LIMIT - 1:\n almost_overflow_counter -= 1\n elif levels:\n embedding_level, directional_override = levels.pop()\n\n # X8\n elif bidi_type == 'B':\n levels.clear()\n overflow_counter = almost_overflow_counter = 0\n embedding_level = _ch['level'] = storage['base_level']\n directional_override = 'N'\n\n # Removes the explicit embeds and overrides of types\n # RLE, LRE, RLO, LRO, PDF, and BN. Adjusts extended chars\n # next and prev as well\n\n # Applies X9. See http://unicode.org/reports/tr9/#X9\n storage['chars'] = [_ch for _ch in storage['chars']\n if _ch['type'] not in X9_REMOVED]\n\n calc_level_runs(storage)\n\n if debug:\n debug_storage(storage, runs=True)", "response": "Apply X1 to X9 rules of the unicode algorithm."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef calc_level_runs(storage):\n # run level depends on the higher of the two levels on either side of\n # the boundary If the higher level is odd, the type is R; otherwise,\n # it is L\n\n storage['runs'].clear()\n chars = storage['chars']\n\n # empty string ?\n if not chars:\n return\n\n def calc_level_run(b_l, b_r):\n return ['L', 'R'][max(b_l, b_r) % 2]\n\n first_char = chars[0]\n\n sor = calc_level_run(storage['base_level'], first_char['level'])\n eor = None\n\n run_start = run_length = 0\n\n prev_level, prev_type = first_char['level'], first_char['type']\n\n for _ch in chars:\n curr_level, curr_type = _ch['level'], _ch['type']\n\n if curr_level == prev_level:\n run_length += 1\n else:\n eor = calc_level_run(prev_level, curr_level)\n storage['runs'].append({'sor': sor, 'eor': eor, 'start': run_start,\n 'type': prev_type, 'length': run_length})\n sor = eor\n run_start += run_length\n run_length = 1\n\n prev_level, prev_type = curr_level, curr_type\n\n # for the last char/runlevel\n eor = calc_level_run(curr_level, storage['base_level'])\n storage['runs'].append({'sor': sor, 'eor': eor, 'start': run_start,\n 'type': curr_type, 'length': run_length})", "response": "Split the storage to run of char types at the same level."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nresolve weak types of the European numbers and ALs in the given storage.", "response": "def resolve_weak_types(storage, debug=False):\n \"\"\"Reslove weak type rules W1 - W3.\n\n See: http://unicode.org/reports/tr9/#Resolving_Weak_Types\n\n \"\"\"\n\n for run in storage['runs']:\n prev_strong = prev_type = run['sor']\n start, length = run['start'], run['length']\n chars = storage['chars'][start:start+length]\n for _ch in chars:\n # W1. Examine each nonspacing mark (NSM) in the level run, and\n # change the type of the NSM to the type of the previous character.\n # If the NSM is at the start of the level run, it will get the type\n # of sor.\n bidi_type = _ch['type']\n\n if bidi_type == 'NSM':\n _ch['type'] = bidi_type = prev_type\n\n # W2. Search backward from each instance of a European number until\n # the first strong type (R, L, AL, or sor) is found. If an AL is\n # found, change the type of the European number to Arabic number.\n if bidi_type == 'EN' and prev_strong == 'AL':\n _ch['type'] = 'AN'\n\n # update prev_strong if needed\n if bidi_type in ('R', 'L', 'AL'):\n prev_strong = bidi_type\n\n prev_type = _ch['type']\n\n # W3. Change all ALs to R\n for _ch in chars:\n if _ch['type'] == 'AL':\n _ch['type'] = 'R'\n\n # W4. A single European separator between two European numbers changes\n # to a European number. A single common separator between two numbers of\n # the same type changes to that type.\n for idx in range(1, len(chars) - 1):\n bidi_type = chars[idx]['type']\n prev_type = chars[idx-1]['type']\n next_type = chars[idx+1]['type']\n\n if bidi_type == 'ES' and (prev_type == next_type == 'EN'):\n chars[idx]['type'] = 'EN'\n\n if bidi_type == 'CS' and prev_type == next_type and \\\n prev_type in ('AN', 'EN'):\n chars[idx]['type'] = prev_type\n\n # W5. A sequence of European terminators adjacent to European numbers\n # changes to all European numbers.\n for idx in range(len(chars)):\n if chars[idx]['type'] == 'EN':\n for et_idx in range(idx-1, -1, -1):\n if chars[et_idx]['type'] == 'ET':\n chars[et_idx]['type'] = 'EN'\n else:\n break\n for et_idx in range(idx+1, len(chars)):\n if chars[et_idx]['type'] == 'ET':\n chars[et_idx]['type'] = 'EN'\n else:\n break\n\n # W6. Otherwise, separators and terminators change to Other Neutral.\n for _ch in chars:\n if _ch['type'] in ('ET', 'ES', 'CS'):\n _ch['type'] = 'ON'\n\n # W7. Search backward from each instance of a European number until the\n # first strong type (R, L, or sor) is found. If an L is found, then\n # change the type of the European number to L.\n prev_strong = run['sor']\n for _ch in chars:\n if _ch['type'] == 'EN' and prev_strong == 'L':\n _ch['type'] = 'L'\n\n if _ch['type'] in ('L', 'R'):\n prev_strong = _ch['type']\n\n if debug:\n debug_storage(storage, runs=True)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nresolve the neutral types in the given storage.", "response": "def resolve_neutral_types(storage, debug):\n \"\"\"Resolving neutral types. Implements N1 and N2\n\n See: http://unicode.org/reports/tr9/#Resolving_Neutral_Types\n\n \"\"\"\n\n for run in storage['runs']:\n start, length = run['start'], run['length']\n # use sor and eor\n chars = [{'type': run['sor']}] + storage['chars'][start:start+length] +\\\n [{'type': run['eor']}]\n total_chars = len(chars)\n\n seq_start = None\n for idx in range(total_chars):\n _ch = chars[idx]\n if _ch['type'] in ('B', 'S', 'WS', 'ON'):\n # N1. A sequence of neutrals takes the direction of the\n # surrounding strong text if the text on both sides has the same\n # direction. European and Arabic numbers act as if they were R\n # in terms of their influence on neutrals. Start-of-level-run\n # (sor) and end-of-level-run (eor) are used at level run\n # boundaries.\n if seq_start is None:\n seq_start = idx\n prev_bidi_type = chars[idx-1]['type']\n else:\n if seq_start is not None:\n next_bidi_type = chars[idx]['type']\n\n if prev_bidi_type in ('AN', 'EN'):\n prev_bidi_type = 'R'\n\n if next_bidi_type in ('AN', 'EN'):\n next_bidi_type = 'R'\n\n for seq_idx in range(seq_start, idx):\n if prev_bidi_type == next_bidi_type:\n chars[seq_idx]['type'] = prev_bidi_type\n else:\n # N2. Any remaining neutrals take the embedding\n # direction. The embedding direction for the given\n # neutral character is derived from its embedding\n # level: L if the character is set to an even level,\n # and R if the level is odd.\n chars[seq_idx]['type'] = \\\n _embedding_direction(chars[seq_idx]['level'])\n\n seq_start = None\n\n if debug:\n debug_storage(storage)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef resolve_implicit_levels(storage, debug):\n for run in storage['runs']:\n start, length = run['start'], run['length']\n chars = storage['chars'][start:start+length]\n\n for _ch in chars:\n # only those types are allowed at this stage\n assert _ch['type'] in ('L', 'R', 'EN', 'AN'),\\\n '%s not allowed here' % _ch['type']\n\n if _embedding_direction(_ch['level']) == 'L':\n # I1. For all characters with an even (left-to-right) embedding\n # direction, those of type R go up one level and those of type\n # AN or EN go up two levels.\n if _ch['type'] == 'R':\n _ch['level'] += 1\n elif _ch['type'] != 'L':\n _ch['level'] += 2\n else:\n # I2. For all characters with an odd (right-to-left) embedding\n # direction, those of type L, EN or AN go up one level.\n if _ch['type'] != 'R':\n _ch['level'] += 1\n\n if debug:\n debug_storage(storage, runs=True)", "response": "Resolves implicit levels in the given storage."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef reverse_contiguous_sequence(chars, line_start, line_end, highest_level,\n lowest_odd_level):\n \"\"\"L2. From the highest level found in the text to the lowest odd\n level on each line, including intermediate levels not actually\n present in the text, reverse any contiguous sequence of characters\n that are at that level or higher.\n\n \"\"\"\n for level in range(highest_level, lowest_odd_level-1, -1):\n _start = _end = None\n\n for run_idx in range(line_start, line_end+1):\n run_ch = chars[run_idx]\n\n if run_ch['level'] >= level:\n if _start is None:\n _start = _end = run_idx\n else:\n _end = run_idx\n else:\n if _end:\n chars[_start:+_end+1] = \\\n reversed(chars[_start:+_end+1])\n _start = _end = None\n\n # anything remaining ?\n if _start is not None:\n chars[_start:+_end+1] = \\\n reversed(chars[_start:+_end+1])", "response": "L2. From the highest level found in the text to the lowest odd\n level on each line and including intermediate levels not actually necessary."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef reorder_resolved_levels(storage, debug):\n\n # Applies L1.\n\n should_reset = True\n chars = storage['chars']\n\n for _ch in chars[::-1]:\n # L1. On each line, reset the embedding level of the following\n # characters to the paragraph embedding level:\n if _ch['orig'] in ('B', 'S'):\n # 1. Segment separators,\n # 2. Paragraph separators,\n _ch['level'] = storage['base_level']\n should_reset = True\n elif should_reset and _ch['orig'] in ('BN', 'WS'):\n # 3. Any sequence of whitespace characters preceding a segment\n # separator or paragraph separator\n # 4. Any sequence of white space characters at the end of the\n # line.\n _ch['level'] = storage['base_level']\n else:\n should_reset = False\n\n max_len = len(chars)\n\n # L2 should be per line\n # Calculates highest level and loweset odd level on the fly.\n\n line_start = line_end = 0\n highest_level = 0\n lowest_odd_level = EXPLICIT_LEVEL_LIMIT\n\n for idx in range(max_len):\n _ch = chars[idx]\n\n # calc the levels\n char_level = _ch['level']\n if char_level > highest_level:\n highest_level = char_level\n\n if char_level % 2 and char_level < lowest_odd_level:\n lowest_odd_level = char_level\n\n if _ch['orig'] == 'B' or idx == max_len - 1:\n line_end = idx\n # omit line breaks\n if _ch['orig'] == 'B':\n line_end -= 1\n\n reverse_contiguous_sequence(chars, line_start, line_end,\n highest_level, lowest_odd_level)\n\n # reset for next line run\n line_start = idx+1\n highest_level = 0\n lowest_odd_level = EXPLICIT_LEVEL_LIMIT\n\n if debug:\n debug_storage(storage)", "response": "Reorder the resolved levels of the language classes."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef apply_mirroring(storage, debug):\n # L4. A character is depicted by a mirrored glyph if and only if (a) the\n # resolved directionality of that character is R, and (b) the\n # Bidi_Mirrored property value of that character is true.\n for _ch in storage['chars']:\n unichar = _ch['ch']\n if mirrored(unichar) and \\\n _embedding_direction(_ch['level']) == 'R':\n _ch['ch'] = MIRRORED.get(unichar, unichar)\n\n if debug:\n debug_storage(storage)", "response": "Applies mirroring to the base character list."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_display(unicode_or_str, encoding='utf-8', upper_is_rtl=False,\n base_dir=None, debug=False):\n \"\"\"Accepts unicode or string. In case it's a string, `encoding`\n is needed as it works on unicode ones (default:\"utf-8\").\n\n Set `upper_is_rtl` to True to treat upper case chars as strong 'R'\n for debugging (default: False).\n\n Set `base_dir` to 'L' or 'R' to override the calculated base_level.\n\n Set `debug` to True to display (using sys.stderr) the steps taken with the\n algorithm.\n\n Returns the display layout, either as unicode or `encoding` encoded\n string.\n\n \"\"\"\n storage = get_empty_storage()\n\n # utf-8 ? we need unicode\n if isinstance(unicode_or_str, six.text_type):\n text = unicode_or_str\n decoded = False\n else:\n text = unicode_or_str.decode(encoding)\n decoded = True\n\n if base_dir is None:\n base_level = get_base_level(text, upper_is_rtl)\n else:\n base_level = PARAGRAPH_LEVELS[base_dir]\n\n storage['base_level'] = base_level\n storage['base_dir'] = ('L', 'R')[base_level]\n\n get_embedding_levels(text, storage, upper_is_rtl, debug)\n explicit_embed_and_overrides(storage, debug)\n resolve_weak_types(storage, debug)\n resolve_neutral_types(storage, debug)\n resolve_implicit_levels(storage, debug)\n reorder_resolved_levels(storage, debug)\n apply_mirroring(storage, debug)\n\n chars = storage['chars']\n display = u''.join([_ch['ch'] for _ch in chars])\n\n if decoded:\n return display.encode(encoding)\n else:\n return display", "response": "Returns the display layout of the given unicode or string."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef process(self, context):\n import os\n from maya import cmds\n\n \"\"\"Inject the current working file\"\"\"\n current_file = cmds.file(sceneName=True, query=True)\n\n # Maya returns forward-slashes by default\n normalised = os.path.normpath(current_file)\n\n context.set_data('currentFile', value=normalised)\n\n # For backwards compatibility\n context.set_data('current_file', value=normalised)", "response": "Inject the current working file"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef convert(lines):\n\n def parse(line):\n line = line.replace(\"from PySide2 import\", \"from Qt import\")\n line = line.replace(\"QtWidgets.QApplication.translate\",\n \"Qt.QtCompat.translate\")\n return line\n\n parsed = list()\n for line in lines:\n line = parse(line)\n parsed.append(line)\n\n return parsed", "response": "Convert compiled. ui file from PySide2 to Qt. py\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nadding an attribute to the object.", "response": "def _add(object, name, value):\n \"\"\"Append to self, accessible via Qt.QtCompat\"\"\"\n self.__added__.append(name)\n setattr(object, name, value)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef cli(args):\n import argparse\n\n parser = argparse.ArgumentParser()\n parser.add_argument(\"--convert\",\n help=\"Path to compiled Python module, e.g. my_ui.py\")\n parser.add_argument(\"--compile\",\n help=\"Accept raw .ui file and compile with native \"\n \"PySide2 compiler.\")\n parser.add_argument(\"--stdout\",\n help=\"Write to stdout instead of file\",\n action=\"store_true\")\n parser.add_argument(\"--stdin\",\n help=\"Read from stdin instead of file\",\n action=\"store_true\")\n\n args = parser.parse_args(args)\n\n if args.stdout:\n raise NotImplementedError(\"--stdout\")\n\n if args.stdin:\n raise NotImplementedError(\"--stdin\")\n\n if args.compile:\n raise NotImplementedError(\"--compile\")\n\n if args.convert:\n sys.stdout.write(\"#\\n\"\n \"# WARNING: --convert is an ALPHA feature.\\n#\\n\"\n \"# See https://github.com/mottosso/Qt.py/pull/132\\n\"\n \"# for details.\\n\"\n \"#\\n\")\n\n #\n # ------> Read\n #\n with open(args.convert) as f:\n lines = convert(f.readlines())\n\n backup = \"%s_backup%s\" % os.path.splitext(args.convert)\n sys.stdout.write(\"Creating \\\"%s\\\"..\\n\" % backup)\n shutil.copy(args.convert, backup)\n\n #\n # <------ Write\n #\n with open(args.convert, \"w\") as f:\n f.write(\"\".join(lines))\n\n sys.stdout.write(\"Successfully converted \\\"%s\\\"\\n\" % args.convert)", "response": "Qt command - line interface for the Sequence System."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _maintain_backwards_compatibility(binding):\n\n for member in (\"__binding__\",\n \"__binding_version__\",\n \"__qt_version__\",\n \"__added__\",\n \"__remapped__\",\n \"__modified__\",\n \"convert\",\n \"load_ui\",\n \"translate\"):\n setattr(binding, member, getattr(self, member))\n self.__added__.append(member)\n\n setattr(binding, \"__wrapper_version__\", self.__version__)\n self.__added__.append(\"__wrapper_version__\")", "response": "Maintain backwards compatibility for the current version of the current version of the current version of the current version of the current version."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ntry showing the most desirable GUI", "response": "def show():\n \"\"\"Try showing the most desirable GUI\n\n This function cycles through the currently registered\n graphical user interfaces, if any, and presents it to\n the user.\n\n \"\"\"\n\n parent = next(\n o for o in QtWidgets.QApplication.instance().topLevelWidgets()\n if o.objectName() == \"MayaWindow\"\n )\n\n gui = _discover_gui()\n\n if gui is None:\n _show_no_gui()\n else:\n return gui(parent)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _discover_gui():\n\n # Prefer last registered\n guis = reversed(pyblish.api.registered_guis())\n\n for gui in guis:\n try:\n gui = __import__(gui).show\n except (ImportError, AttributeError):\n continue\n else:\n return gui", "response": "Return the most desirable GUI of the currently registered GUIs"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef teardown():\n if not self._has_been_setup:\n return\n\n deregister_plugins()\n deregister_host()\n\n if self._has_menu:\n remove_from_filemenu()\n self._has_menu = False\n\n self._has_been_setup = False\n print(\"pyblish: Integration torn down successfully\")", "response": "Remove integration from file menu and plugins"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nadds Pyblish to file - menu.", "response": "def add_to_filemenu():\n \"\"\"Add Pyblish to file-menu\n\n .. note:: We're going a bit hacky here, probably due to my lack\n of understanding for `evalDeferred` or `executeDeferred`,\n so if you can think of a better solution, feel free to edit.\n\n \"\"\"\n\n if hasattr(cmds, 'about') and not cmds.about(batch=True):\n # As Maya builds its menus dynamically upon being accessed,\n # we force its build here prior to adding our entry using it's\n # native mel function call.\n mel.eval(\"evalDeferred buildFileMenu\")\n\n # Serialise function into string\n script = inspect.getsource(_add_to_filemenu)\n script += \"\\n_add_to_filemenu()\"\n\n # If cmds doesn't have any members, we're most likely in an\n # uninitialized batch-mode. It it does exists, ensure we\n # really aren't in batch mode.\n cmds.evalDeferred(script)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef maintained_selection():\n\n previous_selection = cmds.ls(selection=True)\n try:\n yield\n finally:\n if previous_selection:\n cmds.select(previous_selection,\n replace=True,\n noExpand=True)\n else:\n cmds.select(deselect=True,\n noExpand=True)", "response": "Maintain selection during context\n "} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nmaintain current time during context", "response": "def maintained_time():\n \"\"\"Maintain current time during context\n\n Example:\n >>> with maintained_time():\n ... cmds.playblast()\n >>> # Time restored\n\n \"\"\"\n\n ct = cmds.currentTime(query=True)\n try:\n yield\n finally:\n cmds.currentTime(ct, edit=True)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nshow a popup with no GUI.", "response": "def _show_no_gui():\n \"\"\"Popup with information about how to register a new GUI\n\n In the event of no GUI being registered or available,\n this information dialog will appear to guide the user\n through how to get set up with one.\n\n \"\"\"\n\n messagebox = QtWidgets.QMessageBox()\n messagebox.setIcon(messagebox.Warning)\n messagebox.setWindowIcon(QtGui.QIcon(os.path.join(\n os.path.dirname(pyblish.__file__),\n \"icons\",\n \"logo-32x32.svg\"))\n )\n\n spacer = QtWidgets.QWidget()\n spacer.setMinimumSize(400, 0)\n spacer.setSizePolicy(QtWidgets.QSizePolicy.Minimum,\n QtWidgets.QSizePolicy.Expanding)\n\n layout = messagebox.layout()\n layout.addWidget(spacer, layout.rowCount(), 0, 1, layout.columnCount())\n\n messagebox.setWindowTitle(\"Uh oh\")\n\n text = \"No registered GUI found.\\n\\n\"\n\n if not pyblish.api.registered_guis():\n text += (\n \"In order to show you a GUI, one must first be registered. \"\n \"\\n\"\n \"Pyblish supports one or more graphical user interfaces \"\n \"to be registered at once, the next acting as a fallback to \"\n \"the previous.\"\n \"\\n\"\n \"\\n\"\n \"For example, to use Pyblish Lite, first install it:\"\n \"\\n\"\n \"\\n\"\n \"$ pip install pyblish-lite\"\n \"\\n\"\n \"\\n\"\n \"Then register it, like so:\"\n \"\\n\"\n \"\\n\"\n \">>> import pyblish.api\\n\"\n \">>> pyblish.api.register_gui(\\\"pyblish_lite\\\")\"\n \"\\n\"\n \"\\n\"\n \"The next time you try running this, Lite will appear.\"\n \"\\n\"\n \"See http://api.pyblish.com/register_gui.html for \"\n \"more information.\"\n )\n else:\n text += (\n \"None of the registered graphical user interfaces \"\n \"could be found.\"\n \"\\n\"\n \"These interfaces are currently registered:\"\n \"\\n\"\n \"%s\" % \"\\n\".join(pyblish.api.registered_guis())\n )\n\n messagebox.setText(text)\n messagebox.setStandardButtons(messagebox.Ok)\n messagebox.exec_()"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_cumulative_data(self):\n\t\tsets = map(itemgetter('data'), self.data)\n\t\tif not sets:\n\t\t\treturn\n\t\tsum = sets.pop(0)\n\t\tyield sum\n\t\twhile sets:\n\t\t\tsum = map(add, sets.pop(0))\n\t\t\tyield sum", "response": "Get the data as it will be charted."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_single_axis_values(self, axis, dataset):\n\t\tdata_index = getattr(self, '%s_data_index' % axis)\n\t\treturn [p[data_index] for p in dataset['data']]", "response": "Get the values for a single axis of the data."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef __draw_constant_line(self, value_label_style):\n\t\t\"Draw a constant line on the y-axis with the label\"\n\t\tvalue, label, style = value_label_style\n\t\tstart = self.transform_output_coordinates((0, value))[1]\n\t\tstop = self.graph_width\n\t\tpath = etree.SubElement(self.graph, 'path', {\n\t\t\t'd': 'M 0 %(start)s h%(stop)s' % locals(),\n\t\t\t'class': 'constantLine'})\n\t\tif style:\n\t\t\tpath.set('style', style)\n\t\ttext = etree.SubElement(self.graph, 'text', {\n\t\t\t'x': str(2),\n\t\t\t'y': str(start - 2),\n\t\t\t'class': 'constantLine'})\n\t\ttext.text = label", "response": "Draw a constant line on the y - axis with the label"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncache the parameters necessary to transform x & y coordinates", "response": "def load_transform_parameters(self):\n\t\t\"Cache the parameters necessary to transform x & y coordinates\"\n\t\tx_min, x_max, x_div = self.x_range()\n\t\ty_min, y_max, y_div = self.y_range()\n\t\tx_step = (float(self.graph_width) - self.font_size * 2) / \\\n\t\t\t(x_max - x_min)\n\t\ty_step = (float(self.graph_height) - self.font_size * 2) / \\\n\t\t\t(y_max - y_min)\n\t\tself.__transform_parameters = dict(locals())\n\t\tdel self.__transform_parameters['self']"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef reverse_mapping(mapping):\n\tkeys, values = zip(*mapping.items())\n\treturn dict(zip(values, keys))", "response": "Returns the mapping for every key value pair return the mapping for the\n\tequivalent value key pair\n\t"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef flatten_mapping(mapping):\n\treturn {\n\t\tkey: value\n\t\tfor keys, value in mapping.items()\n\t\tfor key in always_iterable(keys)\n\t}", "response": "For every key that has an __iter__ method, assign the values\n\tto a key for each.\n\n\t>>> flatten_mapping({'ab': 3, ('c','d'): 4}) == {'ab': 3, 'c': 4, 'd': 4}\n\tTrue"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef float_range(start=0, stop=None, step=1):\n\tstart = float(start)\n\twhile start < stop:\n\t\tyield start\n\t\tstart += step", "response": "A generator that yields floats from start to stop."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef add_data(self, data_descriptor):\n\t\tpairs = itertools.zip_longest(self.data, data_descriptor['data'])\n\t\tself.data = list(itertools.starmap(robust_add, pairs))", "response": "Adds a data set to the graph."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef add_data(self, conf):\n\t\tself.validate_data(conf)\n\t\tself.process_data(conf)\n\t\tself.data.append(conf)", "response": "Add data to the graph object."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nprocessing the template with the data and config which has been set and return the resulting SVG. Raises ValueError when no data set has been added to the graph object.", "response": "def burn(self):\n\t\t\"\"\"\n\t\tProcess the template with the data and\n\t\tconfig which has been set and return the resulting SVG.\n\n\t\tRaises ValueError when no data set has\n\t\tbeen added to the graph object.\n\t\t\"\"\"\n\t\tif not self.data:\n\t\t\traise ValueError(\"No data available\")\n\n\t\tif hasattr(self, 'calculations'):\n\t\t\tself.calculations()\n\n\t\tself.start_svg()\n\t\tself.calculate_graph_dimensions()\n\t\tself.foreground = etree.Element(\"g\")\n\t\tself.draw_graph()\n\t\tself.draw_titles()\n\t\tself.draw_legend()\n\t\tself.draw_data()\n\t\tself.graph.append(self.foreground)\n\t\tself.render_inline_styles()\n\n\t\treturn self.render(self.root)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncalculating the margin to the left of the plot area setting border_left.", "response": "def calculate_left_margin(self):\n\t\t\"\"\"\n\t\tCalculates the margin to the left of the plot area, setting\n\t\tborder_left.\n\t\t\"\"\"\n\t\tbl = 7\n\t\t# Check for Y labels\n\t\tif self.rotate_y_labels:\n\t\t\tmax_y_label_height_px = self.y_label_font_size\n\t\telse:\n\t\t\tlabel_lengths = map(len, self.get_y_labels())\n\t\t\tmax_y_label_len = max(label_lengths)\n\t\t\tmax_y_label_height_px = 0.6 * max_y_label_len * self.y_label_font_size\n\t\tif self.show_y_labels:\n\t\t\tbl += max_y_label_height_px\n\t\tif self.stagger_y_labels:\n\t\t\tbl += max_y_label_height_px + 10\n\t\tif self.show_y_title:\n\t\t\tbl += self.y_title_font_size + 5\n\t\tself.border_left = bl"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncalculates the margin in pixels to the right of the plot area.", "response": "def calculate_right_margin(self):\n\t\t\"\"\"\n\t\tCalculate the margin in pixels to the right of the plot area,\n\t\tsetting border_right.\n\t\t\"\"\"\n\t\tbr = 7\n\t\tif self.key and self.key_position == 'right':\n\t\t\tmax_key_len = max(map(len, self.keys()))\n\t\t\tbr += max_key_len * self.key_font_size * 0.6\n\t\t\tbr += self.KEY_BOX_SIZE\n\t\t\tbr += 10\t\t# Some padding around the box\n\t\tself.border_right = br"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef calculate_top_margin(self):\n\t\tself.border_top = 5\n\t\tif self.show_graph_title:\n\t\t\tself.border_top += self.title_font_size\n\t\tself.border_top += 5\n\t\tif self.show_graph_subtitle:\n\t\t\tself.border_top += self.subtitle_font_size", "response": "Calculate the margin in pixels above the plot area setting\n\tborder_top."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef add_popup(self, x, y, label):\n\t\ttxt_width = len(label) * self.font_size * 0.6 + 10\n\t\ttx = x + [5, -5][int(x + txt_width > self.width)]\n\t\tanchor = ['start', 'end'][x + txt_width > self.width]\n\t\tstyle = 'fill: #000; text-anchor: %s;' % anchor\n\t\tid = 'label-%s' % self._w3c_name(label)\n\t\tattrs = {\n\t\t\t'x': str(tx),\n\t\t\t'y': str(y - self.font_size),\n\t\t\t'visibility': 'hidden',\n\t\t\t'style': style,\n\t\t\t'text': label,\n\t\t\t'id': id,\n\t\t}\n\t\tetree.SubElement(self.foreground, 'text', attrs)\n\n\t\t# add the circle element to the foreground\n\t\tvis_tmpl = (\n\t\t\t\"document.getElementById('{id}').setAttribute('visibility', {val})\"\n\t\t)\n\t\tattrs = {\n\t\t\t'cx': str(x),\n\t\t\t'cy': str(y),\n\t\t\t'r': str(10),\n\t\t\t'style': 'opacity: 0;',\n\t\t\t'onmouseover': vis_tmpl.format(val='visible', id=id),\n\t\t\t'onmouseout': vis_tmpl.format(val='hidden', id=id),\n\t\t}\n\t\tetree.SubElement(self.foreground, 'circle', attrs)", "response": "Add pop - up information to a point on the graph."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef calculate_bottom_margin(self):\n\t\tbb = 7\n\t\tif self.key and self.key_position == 'bottom':\n\t\t\tbb += len(self.data) * (self.font_size + 5)\n\t\t\tbb += 10\n\t\tif self.show_x_labels:\n\t\t\tmax_x_label_height_px = self.x_label_font_size\n\t\t\tif self.rotate_x_labels:\n\t\t\t\tlabel_lengths = map(len, self.get_x_labels())\n\t\t\t\tmax_x_label_len = functools.reduce(max, label_lengths)\n\t\t\t\tmax_x_label_height_px *= 0.6 * max_x_label_len\n\t\t\tbb += max_x_label_height_px\n\t\t\tif self.stagger_x_labels:\n\t\t\t\tbb += max_x_label_height_px + 10\n\t\tif self.show_x_title:\n\t\t\tbb += self.x_title_font_size + 5\n\t\tself.border_bottom = bb", "response": "Calculate the bottom margin in pixels below the plot area setting\n\tborder_bottom."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef draw_graph(self):\n\t\ttransform = 'translate (%s %s)' % (self.border_left, self.border_top)\n\t\tself.graph = etree.SubElement(self.root, 'g', transform=transform)\n\n\t\tetree.SubElement(self.graph, 'rect', {\n\t\t\t'x': '0',\n\t\t\t'y': '0',\n\t\t\t'width': str(self.graph_width),\n\t\t\t'height': str(self.graph_height),\n\t\t\t'class': 'graphBackground'\n\t\t})\n\n\t\t# Axis\n\t\tetree.SubElement(self.graph, 'path', {\n\t\t\t'd': 'M 0 0 v%s' % self.graph_height,\n\t\t\t'class': 'axis',\n\t\t\t'id': 'xAxis'\n\t\t})\n\t\tetree.SubElement(self.graph, 'path', {\n\t\t\t'd': 'M 0 %s h%s' % (self.graph_height, self.graph_width),\n\t\t\t'class': 'axis',\n\t\t\t'id': 'yAxis'\n\t\t})\n\n\t\tself.draw_x_labels()\n\t\tself.draw_y_labels()", "response": "This method draws the graph."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef make_datapoint_text(self, x, y, value, style=None):\n\t\tif not self.show_data_values:\n\t\t\t# do nothing\n\t\t\treturn\n\t\t# first lay down the text in a wide white stroke to\n\t\t# differentiate it from the background\n\t\te = etree.SubElement(self.foreground, 'text', {\n\t\t\t'x': str(x),\n\t\t\t'y': str(y),\n\t\t\t'class': 'dataPointLabel',\n\t\t\t'style': '%(style)s stroke: #fff; stroke-width: 2;' % vars(),\n\t\t})\n\t\te.text = str(value)\n\t\t# then lay down the text in the specified style\n\t\te = etree.SubElement(self.foreground, 'text', {\n\t\t\t'x': str(x),\n\t\t\t'y': str(y),\n\t\t\t'class': 'dataPointLabel'})\n\t\te.text = str(value)\n\t\tif style:\n\t\t\te.set('style', style)", "response": "Add text for a datapoint"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef draw_x_labels(self):\n\t\t\"Draw the X axis labels\"\n\t\tif self.show_x_labels:\n\t\t\tlabels = self.get_x_labels()\n\t\t\tcount = len(labels)\n\n\t\t\tlabels = enumerate(iter(labels))\n\t\t\tstart = int(not self.step_include_first_x_label)\n\t\t\tlabels = itertools.islice(labels, start, None, self.step_x_labels)\n\t\t\tlist(map(self.draw_x_label, labels))\n\t\t\tself.draw_x_guidelines(self.field_width(), count)", "response": "Draw the X axis labels"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef draw_y_labels(self):\n\t\t\"Draw the Y axis labels\"\n\t\tif not self.show_y_labels:\n\t\t\t# do nothing\n\t\t\treturn\n\n\t\tlabels = self.get_y_labels()\n\t\tcount = len(labels)\n\n\t\tlabels = enumerate(iter(labels))\n\t\tstart = int(not self.step_include_first_y_label)\n\t\tlabels = itertools.islice(labels, start, None, self.step_y_labels)\n\t\tlist(map(self.draw_y_label, labels))\n\t\tself.draw_y_guidelines(self.field_height(), count)", "response": "Draw the Y axis labels"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef draw_x_guidelines(self, label_height, count):\n\t\t\"Draw the X-axis guidelines\"\n\t\tif not self.show_x_guidelines:\n\t\t\treturn\n\t\t# skip the first one\n\t\tfor count in range(1, count):\n\t\t\tmove = 'M {start} 0 v{stop}'.format(\n\t\t\t\tstart=label_height * count,\n\t\t\t\tstop=self.graph_height,\n\t\t\t)\n\t\t\tpath = {'d': move, 'class': 'guideLines'}\n\t\t\tetree.SubElement(self.graph, 'path', path)", "response": "Draw the X - axis guidelines"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ndraws the Y - axis guidelines", "response": "def draw_y_guidelines(self, label_height, count):\n\t\t\"Draw the Y-axis guidelines\"\n\t\tif not self.show_y_guidelines:\n\t\t\treturn\n\t\tfor count in range(1, count):\n\t\t\tmove = 'M 0 {start} h{stop}'.format(\n\t\t\t\tstart=self.graph_height - label_height * count,\n\t\t\t\tstop=self.graph_width,\n\t\t\t)\n\t\t\tpath = {'d': move, 'class': 'guideLines'}\n\t\t\tetree.SubElement(self.graph, 'path', path)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ndrawing the graph title and subtitle", "response": "def draw_titles(self):\n\t\t\"Draws the graph title and subtitle\"\n\t\tif self.show_graph_title:\n\t\t\tself.draw_graph_title()\n\t\tif self.show_graph_subtitle:\n\t\t\tself.draw_graph_subtitle()\n\t\tif self.show_x_title:\n\t\t\tself.draw_x_title()\n\t\tif self.show_y_title:\n\t\t\tself.draw_y_title()"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef render_inline_styles(self):\n\t\t\"Hard-code the styles into the SVG XML if style sheets are not used.\"\n\t\tif not self.css_inline:\n\t\t\t# do nothing\n\t\t\treturn\n\n\t\tstyles = self.parse_css()\n\t\tfor node in self.root.xpath('//*[@class]'):\n\t\t\tcl = '.' + node.attrib['class']\n\t\t\tif cl not in styles:\n\t\t\t\tcontinue\n\t\t\tstyle = styles[cl]\n\t\t\tif 'style' in node.attrib:\n\t\t\t\tstyle += node.attrib['style']\n\t\t\tnode.attrib['style'] = style", "response": "Hard - code the styles into the SVG XML if style sheets are not used."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef parse_css(self):\n\t\t# todo: save the prefs for use later\n\t\t# orig_prefs = cssutils.ser.prefs\n\t\tcssutils.ser.prefs.useMinified()\n\t\tpairs = (\n\t\t\t(r.selectorText, r.style.cssText)\n\t\t\tfor r in self.get_stylesheet()\n\t\t\tif not isinstance(r, cssutils.css.CSSComment)\n\t\t)\n\t\treturn dict(pairs)", "response": "Take a. css file and parse it into a dictionary containing class and style pairs."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nbasing SVG Document Creation", "response": "def start_svg(self):\n\t\t\"Base SVG Document Creation\"\n\t\tSVG_NAMESPACE = 'http://www.w3.org/2000/svg'\n\t\tSVG = '{%s}' % SVG_NAMESPACE\n\t\tNSMAP = {\n\t\t\tNone: SVG_NAMESPACE,\n\t\t\t'xlink': 'http://www.w3.org/1999/xlink',\n\t\t\t'a3': 'http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/',\n\t\t}\n\t\troot_attrs = self._get_root_attributes()\n\t\tself.root = etree.Element(SVG + \"svg\", attrib=root_attrs, nsmap=NSMAP)\n\t\tif hasattr(self, 'style_sheet_href'):\n\t\t\tpi = etree.ProcessingInstruction(\n\t\t\t\t'xml-stylesheet',\n\t\t\t\t'href=\"%s\" type=\"text/css\"' % self.style_sheet_href\n\t\t\t)\n\t\t\tself.root.addprevious(pi)\n\n\t\tcomment_strings = (\n\t\t\t' Created with SVG.Graph ',\n\t\t\t' SVG.Graph by Jason R. Coombs ',\n\t\t\t' Based on SVG::Graph by Sean E. Russel ',\n\t\t\t' Based on Perl SVG:TT:Graph by Leo Lapworth & Stephan Morgan ',\n\t\t\t' ' + '/' * 66,\n\t\t)\n\t\tlist(map(self.root.append, map(etree.Comment, comment_strings)))\n\n\t\tdefs = etree.SubElement(self.root, 'defs')\n\t\tself.add_defs(defs)\n\n\t\tif not hasattr(self, 'style_sheet_href') and not self.css_inline:\n\t\t\tself.root.append(etree.Comment(\n\t\t\t\t' include default stylesheet if none specified '))\n\t\t\tstyle = etree.SubElement(defs, 'style', type='text/css')\n\t\t\t# TODO: the text was previously escaped in a CDATA declaration... how\n\t\t\t# to do that with etree?\n\t\t\tstyle.text = self.get_stylesheet().cssText\n\n\t\tself.root.append(etree.Comment('SVG Background'))\n\t\tetree.SubElement(self.root, 'rect', {\n\t\t\t'width': str(self.width),\n\t\t\t'height': str(self.height),\n\t\t\t'x': '0',\n\t\t\t'y': '0',\n\t\t\t'class': 'svgBackground'})"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ngetting the stylesheets for this instance", "response": "def get_stylesheet_resources(self):\n\t\t\"Get the stylesheets for this instance\"\n\t\t# allow css to include class variables\n\t\tclass_vars = class_dict(self)\n\t\tloader = functools.partial(\n\t\t\tself.load_resource_stylesheet,\n\t\t\tsubs=class_vars)\n\t\tsheets = list(map(loader, self.stylesheet_names))\n\t\treturn sheets"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef send(self, data, force=False):\n if self._registered or force:\n self._sock_file.write('%s\\r\\n' % data)\n self._sock_file.flush()\n else:\n self._out_buffer.append(data)", "response": "\\ Sends data over the wire if the connection is registered."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nconnect to the IRC server using the nickname", "response": "def connect(self):\n \"\"\"\\\n Connect to the IRC server using the nickname\n \"\"\"\n self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n if self.use_ssl:\n self._sock = ssl.wrap_socket(self._sock)\n try:\n self._sock.connect((self.server, self.port))\n except socket.error:\n self.logger.error('Unable to connect to %s on port %d' % (self.server, self.port), exc_info=1)\n return False\n\n self._sock_file = self._sock.makefile()\n if self.password:\n self.set_password()\n self.register_nick()\n self.register()\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nsends a message to a specific user or channel.", "response": "def respond(self, message, channel=None, nick=None):\n \"\"\"\\\n Multipurpose method for sending responses to channel or via message to\n a single user\n \"\"\"\n if channel:\n if not channel.startswith('#'):\n channel = '#%s' % channel\n self.send('PRIVMSG %s :%s' % (channel, message))\n elif nick:\n self.send('PRIVMSG %s :%s' % (nick, message))"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ndispatches the patterns of the socket data based on the patterns in the patterns in the patterns list.", "response": "def dispatch_patterns(self):\n \"\"\"\\\n Low-level dispatching of socket data based on regex matching, in general\n handles\n\n * In event a nickname is taken, registers under a different one\n * Responds to periodic PING messages from server\n * Dispatches to registered callbacks when\n - any user leaves or enters a room currently connected to\n - a channel message is observed\n - a private message is received\n \"\"\"\n return (\n (self.nick_re, self.new_nick),\n (self.nick_change_re, self.handle_nick_change),\n (self.ping_re, self.handle_ping),\n (self.part_re, self.handle_part),\n (self.join_re, self.handle_join),\n (self.quit_re, self.handle_quit),\n (self.chanmsg_re, self.handle_channel_message),\n (self.privmsg_re, self.handle_private_message),\n (self.registered_re, self.handle_registered),\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef handle_registered(self, server):\n if not self._registered:\n self.logger.info('Registered')\n self._registered = True\n for data in self._out_buffer:\n self.send(data)\n self._out_buffer = []", "response": "Handle a registered connection."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef register_callbacks(self):\n self.conn.register_callbacks((\n (re.compile(pattern), callback) \\\n for pattern, callback in self.command_patterns()\n ))", "response": "Register callbacks with the connection"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nwraps for the conn. respond method.", "response": "def respond(self, message, channel=None, nick=None):\n \"\"\"\\\n Wraps the connection object's respond() method\n \"\"\"\n self.conn.respond(message, channel, nick)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef register_with_boss(self):\n gevent.sleep(10) # wait for things to connect, etc\n \n while not self.registered.is_set():\n self.respond('!register {%s}' % platform.node(), nick=self.boss)\n gevent.sleep(30)", "response": "Register the worker with the boss."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef task_runner(self):\n while 1:\n (task_id, command) = self.task_queue.get()\n \n for pattern, callback in self.task_patterns:\n match = re.match(pattern, command)\n if match:\n # execute the callback\n ret = callback(**match.groupdict()) or ''\n \n # clear the stop flag in the event it was set\n self.stop_flag.clear()\n \n # send output of command to channel\n for line in ret.splitlines():\n self.respond('!task-data %s:%s' % (task_id, line), self.channel)\n gevent.sleep(.34)\n \n # indicate task is complete\n self.respond('!task-finished %s' % task_id, self.channel)", "response": "This function is run in a greenlet and will pull the task queue and execute the task callbacks."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef require_boss(self, callback):\n def inner(nick, message, channel, *args, **kwargs):\n if nick != self.boss:\n return\n \n return callback(nick, message, channel, *args, **kwargs)\n return inner", "response": "Decorator to ensure that the command can come from the boss."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef command_patterns(self):\n return (\n ('!register-success (?P.+)', self.require_boss(self.register_success)),\n ('!worker-execute (?:\\((?P.+?)\\) )?(?P\\d+):(?P.+)', self.require_boss(self.worker_execute)),\n ('!worker-ping', self.require_boss(self.worker_ping_handler)),\n ('!worker-stop', self.require_boss(self.worker_stop)),\n )", "response": "Return a list of command patterns that can be used to dispatch the command to the worker bot."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef register_success(self, nick, message, channel, cmd_channel):\n # the boss will tell what channel to join\n self.channel = cmd_channel\n self.conn.join(self.channel)\n \n # indicate that registered so we'll stop trying\n self.registered.set()", "response": "This method is called when the bot is successfully registered with the command channel."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nindicate that the worker with given nick is performing this task", "response": "def add(self, nick):\n \"\"\"\\\n Indicate that the worker with given nick is performing this task\n \"\"\"\n self.data[nick] = ''\n self.workers.add(nick)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef send_validation_email(self):\n if self.email_verified:\n raise ValueError(_('Cannot validate already active user.'))\n\n site = Site.objects.get_current()\n self.validation_notification(user=self, site=site).notify()", "response": "Send a validation email to the user s email address."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef send_password_reset(self):\n site = Site.objects.get_current()\n self.password_reset_notification(user=self, site=site).notify()", "response": "Send a password reset notification to the user s email address."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef validate_password_strength(value):\n used_chars = set(value)\n good_chars = set(ascii_letters + digits + punctuation + ' ')\n required_sets = (ascii_uppercase, ascii_lowercase, digits)\n\n if not used_chars.issubset(good_chars):\n raise ValidationError(too_fancy)\n\n for required in required_sets:\n if not used_chars.intersection(required):\n raise ValidationError(too_simple)", "response": "Validate that the password strength is correct."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nverifies that the user has a valid token.", "response": "def verify_token(self, request, *args, **kwargs):\n \"\"\"\n Use `token` to allow one-time access to a view.\n\n Set the user as a class attribute or raise an `InvalidExpiredToken`.\n\n Token expiry can be set in `settings` with `VERIFY_ACCOUNT_EXPIRY` and is\n set in seconds.\n \"\"\"\n User = get_user_model()\n\n try:\n max_age = settings.VERIFY_ACCOUNT_EXPIRY\n except AttributeError:\n max_age = self.DEFAULT_VERIFY_ACCOUNT_EXPIRY\n\n try:\n email_data = signing.loads(kwargs['token'], max_age=max_age)\n except signing.BadSignature:\n raise self.invalid_exception_class\n\n email = email_data['email']\n\n try:\n self.user = User.objects.get_by_natural_key(email)\n except User.DoesNotExist:\n raise self.invalid_exception_class\n\n if self.user.email_verified:\n raise self.permission_denied_class"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef delete(self, request, *args, **kwargs):\n user = self.get_object()\n user.avatar = None\n user.save()\n return response.Response(status=HTTP_204_NO_CONTENT)", "response": "Delete the user s avatar."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nallows the given request to be throttled.", "response": "def allow_request(self, request, view):\n \"\"\"\n Throttle POST requests only.\n \"\"\"\n if request.method != 'POST':\n return True\n\n return super(PostRequestThrottleMixin, self).allow_request(request, view)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nsingle global client instance", "response": "def client(self):\n \"\"\"single global client instance\"\"\"\n cls = self.__class__\n\n if cls._client is None:\n kwargs = {}\n if self.tls_config:\n kwargs['tls'] = docker.tls.TLSConfig(**self.tls_config)\n kwargs.update(kwargs_from_env())\n client = docker.APIClient(version='auto', **kwargs)\n\n cls._client = client\n return cls._client"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef tls_client(self):\n if self.tls_cert and self.tls_key:\n return (self.tls_cert, self.tls_key)\n return None", "response": "A tuple consisting of the TLS client certificate and key if they have been provided otherwise None."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn the service name inside the Docker Swarm", "response": "def service_name(self):\n \"\"\"\n Service name inside the Docker Swarm\n\n service_suffix should be a numerical value unique for user\n {service_prefix}-{service_owner}-{service_suffix}\n \"\"\"\n if hasattr(self, \"server_name\") and self.server_name:\n server_name = self.server_name\n else:\n server_name = 1\n\n return \"{}-{}-{}\".format(self.service_prefix,\n self.service_owner,\n server_name\n )"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nwraps for calling docker methods", "response": "def _docker(self, method, *args, **kwargs):\n \"\"\"wrapper for calling docker methods\n\n to be passed to ThreadPoolExecutor\n \"\"\"\n m = getattr(self.client, method)\n return m(*args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncall a docker method in a background thread returns a Future", "response": "def docker(self, method, *args, **kwargs):\n \"\"\"Call a docker method in a background thread\n\n returns a Future\n \"\"\"\n return self.executor.submit(self._docker, method, *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef poll(self):\n service = yield self.get_service()\n if not service:\n self.log.warn(\"Docker service not found\")\n return 0\n\n task_filter = {'service': service['Spec']['Name']}\n\n tasks = yield self.docker(\n 'tasks', task_filter\n )\n\n running_task = None\n for task in tasks:\n task_state = task['Status']['State']\n self.log.debug(\n \"Task %s of Docker service %s status: %s\",\n task['ID'][:7],\n self.service_id[:7],\n pformat(task_state),\n )\n if task_state == 'running':\n # there should be at most one running task\n running_task = task\n\n if running_task is not None:\n return None\n else:\n return 1", "response": "Check for a task state like docker service ps id"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef start(self):\n\n # https://github.com/jupyterhub/jupyterhub/blob/master/jupyterhub/user.py#L202\n # By default jupyterhub calls the spawner passing user_options\n if self.use_user_options:\n user_options = self.user_options\n else:\n user_options = {}\n\n self.log.warn(\"user_options: {}\".format(user_options))\n\n service = yield self.get_service()\n\n if service is None:\n\n if 'name' in user_options:\n self.server_name = user_options['name']\n\n if hasattr(self, 'container_spec') and self.container_spec is not None:\n container_spec = dict(**self.container_spec)\n elif user_options == {}:\n raise(\"A container_spec is needed in to create a service\")\n\n container_spec.update(user_options.get('container_spec', {}))\n\n # iterates over mounts to create\n # a new mounts list of docker.types.Mount\n container_spec['mounts'] = []\n for mount in self.container_spec['mounts']:\n m = dict(**mount)\n\n if 'source' in m:\n m['source'] = m['source'].format(\n username=self.service_owner)\n\n if 'driver_config' in m:\n device = m['driver_config']['options']['device'].format(\n username=self.service_owner\n )\n m['driver_config']['options']['device'] = device\n m['driver_config'] = docker.types.DriverConfig(\n **m['driver_config'])\n\n container_spec['mounts'].append(docker.types.Mount(**m))\n\n # some Envs are required by the single-user-image\n container_spec['env'] = self.get_env()\n\n if hasattr(self, 'resource_spec'):\n resource_spec = self.resource_spec\n resource_spec.update(user_options.get('resource_spec', {}))\n\n if hasattr(self, 'networks'):\n networks = self.networks\n if user_options.get('networks') is not None:\n networks = user_options.get('networks')\n\n if hasattr(self, 'placement'):\n placement = self.placement\n if user_options.get('placement') is not None:\n placement = user_options.get('placement')\n\n image = container_spec['Image']\n del container_spec['Image']\n\n # create the service\n container_spec = docker.types.ContainerSpec(\n image, **container_spec)\n resources = docker.types.Resources(**resource_spec)\n\n task_spec = {'container_spec': container_spec,\n 'resources': resources,\n 'placement': placement\n }\n task_tmpl = docker.types.TaskTemplate(**task_spec)\n\n resp = yield self.docker('create_service',\n task_tmpl,\n name=self.service_name,\n networks=networks)\n\n self.service_id = resp['ID']\n\n self.log.info(\n \"Created Docker service '%s' (id: %s) from image %s\",\n self.service_name, self.service_id[:7], image)\n\n else:\n self.log.info(\n \"Found existing Docker service '%s' (id: %s)\",\n self.service_name, self.service_id[:7])\n # Handle re-using API token.\n # Get the API token from the environment variables\n # of the running service:\n envs = service['Spec']['TaskTemplate']['ContainerSpec']['Env']\n for line in envs:\n if line.startswith('JPY_API_TOKEN='):\n self.api_token = line.split('=', 1)[1]\n break\n\n ip = self.service_name\n port = self.service_port\n\n # we use service_name instead of ip\n # https://docs.docker.com/engine/swarm/networking/#use-swarm-mode-service-discovery\n # service_port is actually equal to 8888\n return (ip, port)", "response": "Start a single - user server in a docker service."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef stop(self, now=False):\n self.log.info(\n \"Stopping and removing Docker service %s (id: %s)\",\n self.service_name, self.service_id[:7])\n yield self.docker('remove_service', self.service_id[:7])\n self.log.info(\n \"Docker service %s (id: %s) removed\",\n self.service_name, self.service_id[:7])\n\n self.clear_state()", "response": "Stop and remove the service from the registry"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nchecks lower - cased email is unique.", "response": "def filter_queryset(self, value, queryset):\n \"\"\"Check lower-cased email is unique.\"\"\"\n return super(UniqueEmailValidator, self).filter_queryset(\n value.lower(),\n queryset,\n )"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef update(self, instance, validated_data):\n if not instance.check_password(validated_data['old_password']):\n msg = _('Invalid password.')\n raise serializers.ValidationError({'old_password': msg})\n\n instance.set_password(validated_data['new_password'])\n instance.save()\n return instance", "response": "Check the old password is valid and set the new password."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nupdating the user s password.", "response": "def update(self, instance, validated_data):\n \"\"\"Set the new password for the user.\"\"\"\n instance.set_password(validated_data['new_password'])\n instance.save()\n return instance"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nvalidating if the given email address exists and requires a verification.", "response": "def validate_email(self, email):\n \"\"\"\n Validate if email exists and requires a verification.\n\n `validate_email` will set a `user` attribute on the instance allowing\n the view to send an email confirmation.\n \"\"\"\n try:\n self.user = User.objects.get_by_natural_key(email)\n except User.DoesNotExist:\n msg = _('A user with this email address does not exist.')\n raise serializers.ValidationError(msg)\n\n if self.user.email_verified:\n msg = _('User email address is already verified.')\n raise serializers.ValidationError(msg)\n return email"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef post(self, request):\n serializer = self.serializer_class(data=request.data)\n if serializer.is_valid():\n user = serializer.validated_data['user']\n signals.user_logged_in.send(type(self), user=user, request=request)\n token = self.model.objects.create(user=user)\n token.update_expiry()\n return response.Response({'token': token.key})\n\n return response.Response(\n serializer.errors, status=status.HTTP_400_BAD_REQUEST)", "response": "Create auth token. Differs from DRF that it always creates new token but not re - using them."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef delete(self, request, *args, **kwargs):\n # Logic repeated from DRF because one cannot easily reuse it\n auth = get_authorization_header(request).split()\n\n if not auth or auth[0].lower() != b'token':\n return response.Response(status=status.HTTP_400_BAD_REQUEST)\n\n if len(auth) == 1:\n msg = 'Invalid token header. No credentials provided.'\n return response.Response(msg, status=status.HTTP_400_BAD_REQUEST)\n elif len(auth) > 2:\n msg = 'Invalid token header. Token string should not contain spaces.'\n return response.Response(msg, status=status.HTTP_400_BAD_REQUEST)\n\n try:\n token = self.model.objects.get(key=auth[1])\n except self.model.DoesNotExist:\n pass\n else:\n token.delete()\n signals.user_logged_out.send(\n type(self),\n user=token.user,\n request=request,\n )\n return response.Response(status=status.HTTP_204_NO_CONTENT)", "response": "Delete auth token when delete request was issued."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ndisallows users other than the user whose email is being reset.", "response": "def initial(self, request, *args, **kwargs):\n \"\"\"Disallow users other than the user whose email is being reset.\"\"\"\n email = request.data.get('email')\n if request.user.is_authenticated() and email != request.user.email:\n raise PermissionDenied()\n\n return super(ResendConfirmationEmail, self).initial(\n request,\n *args,\n **kwargs\n )"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef post(self, request, *args, **kwargs):\n serializer = self.serializer_class(data=request.data)\n\n if not serializer.is_valid():\n return response.Response(\n serializer.errors,\n status=status.HTTP_400_BAD_REQUEST,\n )\n\n serializer.user.send_validation_email()\n msg = _('Email confirmation sent.')\n return response.Response(msg, status=status.HTTP_204_NO_CONTENT)", "response": "Validate email and send a request to confirm it."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nvalidate that the email is unique and return it.", "response": "def clean_email(self):\n \"\"\"\n Since User.email is unique, this check is redundant,\n but it sets a nicer error message than the ORM. See #13147.\n \"\"\"\n email = self.cleaned_data['email']\n try:\n User._default_manager.get(email__iexact=email)\n except User.DoesNotExist:\n return email.lower()\n raise forms.ValidationError(self.error_messages['duplicate_email'])"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef update_expiry(self, commit=True):\n self.expires = update_expiry(self.created)\n if commit:\n self.save()", "response": "Update token s expiration datetime on every auth action."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef password_reset_email_context(notification):\n return {\n 'protocol': 'https',\n 'uid': notification.user.generate_uid(),\n 'token': notification.user.generate_token(),\n 'site': notification.site,\n }", "response": "Generate email context to reset a user password."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef email_handler(notification, email_context):\n incuna_mail.send(\n to=notification.user.email,\n subject=notification.email_subject,\n template_name=notification.text_email_template,\n html_template_name=notification.html_email_template,\n context=email_context(notification),\n headers=getattr(notification, 'headers', {}),\n )", "response": "Send a notification by email."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef password_reset_email_handler(notification):\n base_subject = _('{domain} password reset').format(domain=notification.site.domain)\n subject = getattr(settings, 'DUM_PASSWORD_RESET_SUBJECT', base_subject)\n notification.email_subject = subject\n email_handler(notification, password_reset_email_context)", "response": "Password reset email handler."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef authenticate(self, request):\n try:\n key = request.data['token']\n except KeyError:\n return\n\n try:\n token = AuthToken.objects.get(key=key)\n except AuthToken.DoesNotExist:\n return\n\n return (token.user, token)", "response": "Authenticate a user from a token form field\n\n Returns a tuple of user and AuthToken instance"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef authenticate_credentials(self, key):\n user, token = super(TokenAuthentication, self).authenticate_credentials(key)\n\n if token.expires < timezone.now():\n msg = _('Token has expired.')\n raise exceptions.AuthenticationFailed(msg)\n\n # Update the token's expiration date\n token.update_expiry()\n\n return (user, token)", "response": "Custom authentication to check if auth token has expired."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef notebook_show(obj, doc, comm):\n target = obj.ref['id']\n load_mime = 'application/vnd.holoviews_load.v0+json'\n exec_mime = 'application/vnd.holoviews_exec.v0+json'\n\n # Publish plot HTML\n bokeh_script, bokeh_div, _ = bokeh.embed.notebook.notebook_content(obj, comm.id)\n publish_display_data(data={'text/html': encode_utf8(bokeh_div)})\n\n # Publish comm manager\n JS = '\\n'.join([PYVIZ_PROXY, JupyterCommManager.js_manager])\n publish_display_data(data={load_mime: JS, 'application/javascript': JS})\n\n # Publish bokeh plot JS\n msg_handler = bokeh_msg_handler.format(plot_id=target)\n comm_js = comm.js_template.format(plot_id=target, comm_id=comm.id, msg_handler=msg_handler)\n bokeh_js = '\\n'.join([comm_js, bokeh_script])\n\n # Note: extension should be altered so text/html is not required\n publish_display_data(data={exec_mime: '', 'text/html': '',\n 'application/javascript': bokeh_js},\n metadata={exec_mime: {'id': target}})", "response": "Displays bokeh output inside a notebook."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _get_customjs(self, change, p_name):\n data_template = \"data = {{p_name: '{p_name}', value: cb_obj['{change}']}};\"\n fetch_data = data_template.format(change=change, p_name=p_name)\n self_callback = JS_CALLBACK.format(comm_id=self.comm.id,\n timeout=self.timeout,\n debounce=self.debounce,\n plot_id=self.plot_id)\n js_callback = CustomJS(code='\\n'.join([fetch_data,\n self_callback]))\n return js_callback", "response": "Returns a CustomJS callback that can be attached to send the\n widget state across the notebook comms."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef widget(self, param_name):\n if param_name not in self._widgets:\n self._widgets[param_name] = self._make_widget(param_name)\n return self._widgets[param_name]", "response": "Get widget for param_name"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning name widget boxes for all parameters.", "response": "def widgets(self):\n \"\"\"Return name,widget boxes for all parameters (i.e., a property sheet)\"\"\"\n\n params = self.parameterized.params().items()\n key_fn = lambda x: x[1].precedence if x[1].precedence is not None else self.p.default_precedence\n sorted_precedence = sorted(params, key=key_fn)\n outputs = [k for k, p in sorted_precedence if isinstance(p, _View)]\n filtered = [(k,p) for (k,p) in sorted_precedence\n if ((p.precedence is None) or (p.precedence >= self.p.display_threshold))\n and k not in outputs]\n groups = itertools.groupby(filtered, key=key_fn)\n sorted_groups = [sorted(grp) for (k,grp) in groups]\n ordered_params = [el[0] for group in sorted_groups for el in group]\n\n # Format name specially\n ordered_params.pop(ordered_params.index('name'))\n widgets = [Div(text='{0}'.format(self.parameterized.name))]\n\n def format_name(pname):\n p = self.parameterized.params(pname)\n # omit name for buttons, which already show the name on the button\n name = \"\" if issubclass(type(p),param.Action) else pname\n return Div(text=name)\n\n if self.p.show_labels:\n widgets += [self.widget(pname) for pname in ordered_params]\n else:\n widgets += [self.widget(pname) for pname in ordered_params]\n\n if self.p.button and not (self.p.callback is None and self.p.next_n==0):\n display_button = Button(label=self.p.button_text)\n def click_cb():\n # Execute and clear changes since last button press\n try:\n self.execute(self._changed)\n except Exception as e:\n self._changed.clear()\n raise e\n self._changed.clear()\n display_button.on_click(click_cb)\n widgets.append(display_button)\n\n outputs = [self.widget(pname) for pname in outputs]\n return widgets, outputs"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning a TextInput instance with the given arguments and keyword arguments.", "response": "def TextWidget(*args, **kw):\n \"\"\"Forces a parameter value to be text\"\"\"\n kw['value'] = str(kw['value'])\n kw.pop('options', None)\n return TextInput(*args,**kw)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngiving a list of objects returns a dictionary mapping from string name for the object to the object itself.", "response": "def named_objs(objlist):\n \"\"\"\n Given a list of objects, returns a dictionary mapping from\n string name for the object to the object itself.\n \"\"\"\n objs = []\n for k, obj in objlist:\n if hasattr(k, '__name__'):\n k = k.__name__\n else:\n k = as_unicode(k)\n objs.append((k, obj))\n return objs"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn the instance or class owning the supplied method.", "response": "def get_method_owner(meth):\n \"\"\"\n Returns the instance owning the supplied instancemethod or\n the class owning the supplied classmethod.\n \"\"\"\n if inspect.ismethod(meth):\n if sys.version_info < (3,0):\n return meth.im_class if meth.im_self is None else meth.im_self\n else:\n return meth.__self__"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _assign_auth_values(self, http_auth):\n if not http_auth:\n pass\n elif isinstance(http_auth, (tuple, list)):\n self._auth_user, self._auth_password = http_auth\n elif isinstance(http_auth, str):\n self._auth_user, self._auth_password = http_auth.split(':')\n else:\n raise ValueError('HTTP Auth Credentials should be str or '\n 'tuple, not %s' % type(http_auth))", "response": "Assign the username and password of the attributes that are set in the object."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef ping(self, params=None):\n try:\n self.transport.perform_request('HEAD', '/', params=params)\n except TransportError:\n raise gen.Return(False)\n raise gen.Return(True)", "response": "Checks if the cluster is up."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef info(self, params=None):\n _, data = yield self.transport.perform_request('GET', '/',\n params=params)\n raise gen.Return(data)", "response": "Get basic info from the current cluster."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef create(self, index, doc_type, body, id=None, params=None):\n result = yield self.index(index, doc_type, body, id=id, params=params,\n op_type='create')\n raise gen.Return(result)", "response": "Adds a typed JSON document in a specific index making it searchable."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef index(self, index, doc_type, body, id=None, params=None):\n _, data = yield self.transport.perform_request(\n 'PUT' if id else 'POST', _make_path(index, doc_type, id),\n params=params, body=body)\n raise gen.Return(data)", "response": "Add or update a typed JSON document in a specific index making it searchable."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning a boolean indicating whether or not given document exists in the given index.", "response": "def exists(self, index, id, doc_type='_all', params=None):\n \"\"\"\n Returns a boolean indicating whether or not given document exists in\n Elasticsearch. ``_\n\n :arg index: The name of the index\n :arg id: The document ID\n :arg doc_type: The type of the document (uses `_all` by default to\n fetch the first document matching the ID across all types)\n :arg parent: The ID of the parent document\n :arg preference: Specify the node or shard the operation should be\n performed on (default: random)\n :arg realtime: Specify whether to perform the operation in realtime or\n search mode\n :arg refresh: Refresh the shard containing the document before\n performing the operation\n :arg routing: Specific routing value\n \"\"\"\n try:\n self.transport.perform_request(\n 'HEAD', _make_path(index, doc_type, id), params=params)\n except exceptions.NotFoundError:\n return gen.Return(False)\n raise gen.Return(True)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nretrieving a specified alias.", "response": "def get_alias(self, index=None, name=None, params=None):\n \"\"\"\n Retrieve a specified alias.\n ``_\n :arg index: A comma-separated list of index names to filter aliases\n :arg name: A comma-separated list of alias names to return\n :arg allow_no_indices: Whether to ignore if a wildcard indices\n expression resolves into no concrete indices. (This includes `_all`\n string or when no indices have been specified)\n :arg expand_wildcards: Whether to expand wildcard expression to\n concrete indices that are open, closed or both., default 'all',\n valid choices are: 'open', 'closed', 'none', 'all'\n :arg ignore_unavailable: Whether specified concrete indices should be\n ignored when unavailable (missing or closed)\n :arg local: Return local information, do not retrieve the state from\n master node (default: false)\n \"\"\"\n _, result = yield self.transport.perform_request(\n 'GET', _make_path(index, '_alias', name), params=params)\n raise gen.Return(result)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nexecutes a search query and get back search hits", "response": "def search(self, index=None, doc_type=None, body=None, params=None):\n \"\"\"\n Execute a search query and get back search hits that match the query.\n ``_\n\n :arg index: A comma-separated list of index names to search; use `_all`\n or empty string to perform the operation on all indices\n :arg doc_type: A comma-separated list of document types to search;\n leave empty to perform the operation on all types\n :arg body: The search definition using the Query DSL\n :arg _source: True or false to return the _source field or not, or a\n list of fields to return\n :arg _source_exclude: A list of fields to exclude from the returned\n _source field\n :arg _source_include: A list of fields to extract and return from the\n _source field\n :arg analyze_wildcard: Specify whether wildcard and prefix queries\n should be analyzed (default: false)\n :arg analyzer: The analyzer to use for the query string\n :arg default_operator: The default operator for query string query (AND\n or OR) (default: OR)\n :arg df: The field to use as default where no field prefix is given in\n the query string\n :arg explain: Specify whether to return detailed information about\n score computation as part of a hit\n :arg fields: A comma-separated list of fields to return as part of a hit\n :arg ignore_indices: When performed on multiple indices, allows to\n ignore `missing` ones (default: none)\n :arg indices_boost: Comma-separated list of index boosts\n :arg lenient: Specify whether format-based query failures (such as\n providing text to a numeric field) should be ignored\n :arg lowercase_expanded_terms: Specify whether query terms should be\n lowercased\n :arg from_: Starting offset (default: 0)\n :arg preference: Specify the node or shard the operation should be\n performed on (default: random)\n :arg q: Query in the Lucene query string syntax\n :arg routing: A comma-separated list of specific routing values\n :arg scroll: Specify how long a consistent view of the index should be\n maintained for scrolled search\n :arg search_type: Search operation type\n :arg size: Number of hits to return (default: 10)\n :arg sort: A comma-separated list of : pairs\n :arg source: The URL-encoded request definition using the Query DSL\n (instead of using request body)\n :arg stats: Specific 'tag' of the request for logging and statistical\n purposes\n :arg suggest_field: Specify which field to use for suggestions\n :arg suggest_mode: Specify suggest mode (default: missing)\n :arg suggest_size: How many suggestions to return in response\n :arg suggest_text: The source text for which the suggestions should be\n returned\n :arg timeout: Explicit operation timeout\n :arg version: Specify whether to return document version as part of a\n hit\n \"\"\"\n # from is a reserved word so it cannot be used, use from_ instead\n if 'from_' in params:\n params['from'] = params.pop('from_')\n\n if doc_type and not index:\n index = '_all'\n _, data = yield self.transport.perform_request('GET',\n _make_path(index,\n doc_type,\n '_search'),\n params=params,\n body=body)\n raise gen.Return(data)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nscrolling a search request created by specifying the scroll parameter.", "response": "def scroll(self, scroll_id, scroll, params=None):\n \"\"\"\n Scroll a search request created by specifying the scroll parameter.\n ``_\n\n :arg scroll_id: The scroll ID\n :arg scroll: Specify how long a consistent view of the index should be\n maintained for scrolled search\n \"\"\"\n body = {\n \"scroll\": scroll,\n \"scroll_id\": scroll_id\n }\n\n if params:\n if \"scroll\" in params.keys():\n params.pop(\"scroll\")\n if \"scroll_id\" in params.keys():\n params.pop(\"scroll_id\")\n\n _, data = yield self.transport.perform_request('POST',\n _make_path('_search',\n 'scroll'),\n body=body,\n params=params)\n raise gen.Return(data)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef clear_scroll(self, scroll_id, params=None):\n if not isinstance(scroll_id, list):\n scroll_id = [scroll_id]\n\n body = {\n \"scroll_id\": scroll_id\n }\n\n if params and \"scroll_id\" in params.keys():\n params.pop(\"scroll_id\")\n\n _, data = yield self.transport.perform_request('DELETE',\n _make_path('_search',\n 'scroll'),\n body=body,\n params=params)\n raise gen.Return(data)", "response": "Clear the scroll request created by specifying the scroll ID or a list of scroll IDs."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nretrieving mapping definition of index or type.", "response": "def get_mapping(self, index=None, doc_type=None, params=None):\n \"\"\"\n Retrieve mapping definition of index or index/type.\n ``_\n :arg index: A comma-separated list of index names\n :arg doc_type: A comma-separated list of document types\n :arg allow_no_indices: Whether to ignore if a wildcard indices\n expression resolves into no concrete indices. (This includes `_all`\n string or when no indices have been specified)\n :arg expand_wildcards: Whether to expand wildcard expression to concrete\n indices that are open, closed or both., default 'open', valid\n choices are: 'open', 'closed', 'none', 'all'\n :arg ignore_unavailable: Whether specified concrete indices should be\n ignored when unavailable (missing or closed)\n :arg local: Return local information, do not retrieve the state from\n master node (default: false)\n \"\"\"\n _, data = yield self.transport.perform_request('GET',\n _make_path(index,\n '_mapping',\n doc_type),\n params=params)\n raise gen.Return(data)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef suggest(self, index=None, body=None, params=None):\n _, data = yield self.transport.perform_request('POST',\n _make_path(index,\n '_suggest'),\n params=params, body=body)\n raise gen.Return(data)", "response": "The suggest feature suggests similar looking terms based on a provided index and body."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nconverting a number of bytes to a human readable format", "response": "def bytes_to_readable(num):\r\n \"\"\"Converts bytes to a human readable format\"\"\"\r\n if num < 512:\r\n return \"0 Kb\"\r\n elif num < 1024:\r\n return \"1 Kb\"\r\n\r\n for unit in ['', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb']:\r\n if abs(num) < 1024.0:\r\n return \"%3.1f%s\" % (num, unit)\r\n num /= 1024.0\r\n return \"%.1f%s\" % (num, 'Yb')"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ntotal CPU load for Synology DSM", "response": "def cpu_total_load(self):\r\n \"\"\"Total CPU load for Synology DSM\"\"\"\r\n system_load = self.cpu_system_load\r\n user_load = self.cpu_user_load\r\n other_load = self.cpu_other_load\r\n\r\n if system_load is not None and \\\r\n user_load is not None and \\\r\n other_load is not None:\r\n return system_load + user_load + other_load"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef memory_size(self, human_readable=True):\r\n if self._data is not None:\r\n # Memory is actually returned in KB's so multiply before converting\r\n return_data = int(self._data[\"memory\"][\"memory_size\"]) * 1024\r\n if human_readable:\r\n return SynoFormatHelper.bytes_to_readable(\r\n return_data)\r\n else:\r\n return return_data", "response": "Returns the total memory size of the Synology DSM"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nfunction to get specific network", "response": "def _get_network(self, network_id):\r\n \"\"\"Function to get specific network (eth0, total, etc)\"\"\"\r\n if self._data is not None:\r\n for network in self._data[\"network\"]:\r\n if network[\"device\"] == network_id:\r\n return network"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning the total upload speed being used", "response": "def network_up(self, human_readable=True):\r\n \"\"\"Total upload speed being used\"\"\"\r\n network = self._get_network(\"total\")\r\n if network is not None:\r\n return_data = int(network[\"tx\"])\r\n if human_readable:\r\n return SynoFormatHelper.bytes_to_readable(\r\n return_data)\r\n else:\r\n return return_data"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns a list of all available volumes", "response": "def volumes(self):\r\n \"\"\"Returns all available volumes\"\"\"\r\n if self._data is not None:\r\n volumes = []\r\n for volume in self._data[\"volumes\"]:\r\n volumes.append(volume[\"id\"])\r\n return volumes"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _get_volume(self, volume_id):\r\n if self._data is not None:\r\n for volume in self._data[\"volumes\"]:\r\n if volume[\"id\"] == volume_id:\r\n return volume", "response": "Returns a specific volume"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning the total size of the given volume", "response": "def volume_size_total(self, volume, human_readable=True):\r\n \"\"\"Total size of volume\"\"\"\r\n volume = self._get_volume(volume)\r\n if volume is not None:\r\n return_data = int(volume[\"size\"][\"total\"])\r\n if human_readable:\r\n return SynoFormatHelper.bytes_to_readable(\r\n return_data)\r\n else:\r\n return return_data"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ntotal used size in percentage for volume", "response": "def volume_percentage_used(self, volume):\r\n \"\"\"Total used size in percentage for volume\"\"\"\r\n volume = self._get_volume(volume)\r\n if volume is not None:\r\n total = int(volume[\"size\"][\"total\"])\r\n used = int(volume[\"size\"][\"used\"])\r\n\r\n if used is not None and used > 0 and \\\r\n total is not None and total > 0:\r\n return round((float(used) / float(total)) * 100.0, 1)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn the average temperature of all disks making up the volume", "response": "def volume_disk_temp_avg(self, volume):\r\n \"\"\"Average temperature of all disks making up the volume\"\"\"\r\n volume = self._get_volume(volume)\r\n if volume is not None:\r\n vol_disks = volume[\"disks\"]\r\n if vol_disks is not None:\r\n total_temp = 0\r\n total_disks = 0\r\n\r\n for vol_disk in vol_disks:\r\n disk_temp = self.disk_temp(vol_disk)\r\n if disk_temp is not None:\r\n total_disks += 1\r\n total_temp += disk_temp\r\n\r\n if total_temp > 0 and total_disks > 0:\r\n return round(total_temp / total_disks, 0)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning the maximum temperature of all disks making up the volume", "response": "def volume_disk_temp_max(self, volume):\r\n \"\"\"Maximum temperature of all disks making up the volume\"\"\"\r\n volume = self._get_volume(volume)\r\n if volume is not None:\r\n vol_disks = volume[\"disks\"]\r\n if vol_disks is not None:\r\n max_temp = 0\r\n\r\n for vol_disk in vol_disks:\r\n disk_temp = self.disk_temp(vol_disk)\r\n if disk_temp is not None and disk_temp > max_temp:\r\n max_temp = disk_temp\r\n\r\n return max_temp"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn all available ( internal ) disks", "response": "def disks(self):\r\n \"\"\"Returns all available (internal) disks\"\"\"\r\n if self._data is not None:\r\n disks = []\r\n for disk in self._data[\"disks\"]:\r\n disks.append(disk[\"id\"])\r\n return disks"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _get_disk(self, disk_id):\r\n if self._data is not None:\r\n for disk in self._data[\"disks\"]:\r\n if disk[\"id\"] == disk_id:\r\n return disk", "response": "Returns a specific disk"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nbuilds and execute login request", "response": "def _login(self):\r\n \"\"\"Build and execute login request\"\"\"\r\n api_path = \"%s/auth.cgi?api=SYNO.API.Auth&version=2\" % (\r\n self.base_url,\r\n )\r\n\r\n login_path = \"method=login&%s\" % (self._encode_credentials())\r\n\r\n url = \"%s&%s&session=Core&format=cookie\" % (\r\n api_path,\r\n login_path)\r\n result = self._execute_get_url(url, False)\r\n\r\n # Parse Result if valid\r\n if result is not None:\r\n self.access_token = result[\"data\"][\"sid\"]\r\n self._debuglog(\"Authentication Succesfull, token: \" +\r\n str(self.access_token))\r\n return True\r\n else:\r\n self._debuglog(\"Authentication Failed\")\r\n return False"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nfunctioning to handle GET requests for a given url.", "response": "def _get_url(self, url, retry_on_error=True):\r\n \"\"\"Function to handle sessions for a GET request\"\"\"\r\n # Check if we failed to request the url or need to login\r\n if self.access_token is None or \\\r\n self._session is None or \\\r\n self._session_error:\r\n # Clear Access Token en reset session error\r\n self.access_token = None\r\n self._session_error = False\r\n\r\n # First Reset the session\r\n if self._session is not None:\r\n self._session = None\r\n self._debuglog(\"Creating New Session\")\r\n self._session = requests.Session()\r\n \r\n # disable SSL certificate verification\r\n if self._use_https:\r\n self._session.verify = False\r\n\r\n\r\n # We Created a new Session so login\r\n if self._login() is False:\r\n self._session_error = True\r\n self._debuglog(\"Login Failed, unable to process request\")\r\n return\r\n\r\n # Now request the data\r\n response = self._execute_get_url(url)\r\n if (self._session_error or response is None) and retry_on_error:\r\n self._debuglog(\"Error occured, retrying...\")\r\n self._get_url(url, False)\r\n\r\n return response"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nexecute a GET request and return the response", "response": "def _execute_get_url(self, request_url, append_sid=True):\r\n \"\"\"Function to execute and handle a GET request\"\"\"\r\n # Prepare Request\r\n self._debuglog(\"Requesting URL: '\" + request_url + \"'\")\r\n if append_sid:\r\n self._debuglog(\"Appending access_token (SID: \" +\r\n self.access_token + \") to url\")\r\n request_url = \"%s&_sid=%s\" % (\r\n request_url, self.access_token)\r\n\r\n # Execute Request\r\n try:\r\n resp = self._session.get(request_url)\r\n self._debuglog(\"Request executed: \" + str(resp.status_code))\r\n if resp.status_code == 200:\r\n # We got a response\r\n json_data = json.loads(resp.text)\r\n\r\n if json_data[\"success\"]:\r\n self._debuglog(\"Succesfull returning data\")\r\n self._debuglog(str(json_data))\r\n return json_data\r\n else:\r\n if json_data[\"error\"][\"code\"] in {105, 106, 107, 119}:\r\n self._debuglog(\"Session error: \" +\r\n str(json_data[\"error\"][\"code\"]))\r\n self._session_error = True\r\n else:\r\n self._debuglog(\"Failed: \" + resp.text)\r\n else:\r\n # We got a 404 or 401\r\n return None\r\n #pylint: disable=bare-except\r\n except:\r\n return None"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nupdate the various instanced modules", "response": "def update(self):\r\n \"\"\"Updates the various instanced modules\"\"\"\r\n if self._utilisation is not None:\r\n api = \"SYNO.Core.System.Utilization\"\r\n url = \"%s/entry.cgi?api=%s&version=1&method=get&_sid=%s\" % (\r\n self.base_url,\r\n api,\r\n self.access_token)\r\n self._utilisation.update(self._get_url(url))\r\n if self._storage is not None:\r\n api = \"SYNO.Storage.CGI.Storage\"\r\n url = \"%s/entry.cgi?api=%s&version=1&method=load_info&_sid=%s\" % (\r\n self.base_url,\r\n api,\r\n self.access_token)\r\n self._storage.update(self._get_url(url))"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget the various Utilisation variables from the API.", "response": "def utilisation(self):\r\n \"\"\"Getter for various Utilisation variables\"\"\"\r\n if self._utilisation is None:\r\n api = \"SYNO.Core.System.Utilization\"\r\n url = \"%s/entry.cgi?api=%s&version=1&method=get\" % (\r\n self.base_url,\r\n api)\r\n self._utilisation = SynoUtilization(self._get_url(url))\r\n return self._utilisation"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef storage(self):\r\n if self._storage is None:\r\n api = \"SYNO.Storage.CGI.Storage\"\r\n url = \"%s/entry.cgi?api=%s&version=1&method=load_info\" % (\r\n self.base_url,\r\n api)\r\n self._storage = SynoStorage(self._get_url(url))\r\n return self._storage", "response": "Gets the SynoStorage object for the given tag."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncreate a context for a specific request.", "response": "def for_request(request, body=None):\n \"\"\"Creates the context for a specific request.\"\"\"\n tenant, jwt_data = Tenant.objects.for_request(request, body)\n webhook_sender_id = jwt_data.get('sub')\n sender_data = None\n\n if body and 'item' in body:\n if 'sender' in body['item']:\n sender_data = body['item']['sender']\n elif 'message' in body['item'] and 'from' in body['item']['message']:\n sender_data = body['item']['message']['from']\n\n if sender_data is None:\n if webhook_sender_id is None:\n raise BadTenantError('Cannot identify sender in tenant')\n sender_data = {'id': webhook_sender_id}\n\n return Context(\n tenant=tenant,\n sender=HipchatUser(\n id=sender_data.get('id'),\n name=sender_data.get('name'),\n mention_name=sender_data.get('mention_name'),\n ),\n signed_request=request.GET.get('signed_request'),\n context=jwt_data.get('context') or {},\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef tenant_token(self):\n rv = getattr(self, '_tenant_token', None)\n if rv is None:\n rv = self._tenant_token = self.tenant.get_token()\n return rv", "response": "The cached token of the current tenant."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef build_attrs(self, extra_attrs=None, **kwargs):\n \"Helper function for building an attribute dictionary.\"\n self.attrs = self.widget.build_attrs(extra_attrs=None, **kwargs)\n return self.attrs", "response": "Helper function for building an attribute dictionary."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef with_apps(*apps):\n apps_set = set(settings.INSTALLED_APPS)\n apps_set.update(apps)\n return override_settings(INSTALLED_APPS=list(apps_set))", "response": "A class decorator that makes sure the passed apps are present in the INSTALLED_APPS setting."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nclasses decorator that makes sure the passed apps are not present in INSTALLED_APPS.", "response": "def without_apps(*apps):\n \"\"\"\n Class decorator that makes sure the passed apps are not present in\n INSTALLED_APPS.\n \"\"\"\n apps_list = [a for a in settings.INSTALLED_APPS if a not in apps]\n return override_settings(INSTALLED_APPS=apps_list)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_global_settings(self):\n return dict((key, getattr(global_settings, key)) for key in dir(global_settings)\n if key.isupper())", "response": "Return a dictionary of all global_settings values."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nhandle the retrieval of the code", "response": "def do_GET(self):\n\t\t\"\"\"\n\t\tHandle the retrieval of the code\n\t\t\"\"\"\n\t\tparsed_url = urlparse(self.path)\n\n\t\tif parsed_url[2] == \"/\" + SERVER_REDIRECT_PATH: # 2 = Path\n\t\t\tparsed_query = parse_qs(parsed_url[4]) # 4 = Query\n\n\t\t\tif \"code\" not in parsed_query:\n\t\t\t\tself.send_response(200)\n\t\t\t\tself.send_header(\"Content-Type\", \"text/plain\")\n\t\t\t\tself.end_headers()\n\n\t\t\t\tself.wfile.write(\"No code found, try again!\".encode(\"utf-8\"))\n\t\t\t\treturn\n\n\t\t\tself.server.response_code = parsed_query[\"code\"][0]\n\n\t\t\tself.send_response(200)\n\t\t\tself.send_header(\"Content-Type\", \"text/plain\")\n\t\t\tself.end_headers()\n\n\t\t\tself.wfile.write(\n\t\t\t\t\"Thank you for using OAuth2Util. The authorization was successful, \"\n\t\t\t\t\"you can now close this window.\".encode(\"utf-8\"))\n\t\telif parsed_url[2] == \"/\" + SERVER_LINK_PATH: # 2 = Path\n\t\t\tself.send_response(200)\n\t\t\tself.send_header(\"Content-Type\", \"text/html\")\n\t\t\tself.end_headers()\n\n\t\t\tself.wfile.write(\"Hey there!
    Click here to claim your prize.\"\n\t\t\t\t.format(self.server.authorize_url).encode(\"utf-8\"))\n\t\telse:\n\t\t\tself.send_response(404)\n\t\t\tself.send_header(\"Content-Type\", \"text/plain\")\n\t\t\tself.end_headers()\n\t\t\tself.wfile.write(\"404 not found\".encode(\"utf-8\"))"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nsets the app info read from the config file on the Reddit object self. r", "response": "def _set_app_info(self):\n\t\t\"\"\"\n\t\tSet the app info (id & secret) read from the config file on the Reddit object\n\t\t\"\"\"\n\t\tredirect_url = \"http://{0}:{1}/{2}\".format(SERVER_URL, SERVER_PORT,\n\t\t\t\t\t\t\t\t\t\t\t\t SERVER_REDIRECT_PATH)\n\t\tself.r.set_oauth_app_info(self._get_value(CONFIGKEY_APP_KEY),\n\t\t\t\t\t\t\t\t self._get_value(CONFIGKEY_APP_SECRET),\n\t\t\t\t\t\t\t\t redirect_url)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _get_value(self, key, func=None, split_val=None, as_boolean=False,\n\t\texception_default=None):\n\t\t\"\"\"\n\t\tHelper method to get a value from the config\n\t\t\"\"\"\n\t\ttry:\n\t\t\tif as_boolean:\n\t\t\t\treturn self.config.getboolean(key[0], key[1])\n\t\t\tvalue = self.config.get(key[0], key[1])\n\t\t\tif split_val is not None:\n\t\t\t\tvalue = value.split(split_val)\n\t\t\tif func is not None:\n\t\t\t\treturn func(value)\n\t\t\treturn value\n\t\texcept (KeyError, configparser.NoSectionError, configparser.NoOptionError) as e:\n\t\t\tif exception_default is not None:\n\t\t\t\treturn exception_default\n\t\t\traise KeyError(e)", "response": "Internal method to get a value from the configparser object"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _change_value(self, key, value):\n\t\tif not self.config.has_section(key[0]):\n\t\t\tself.config.add_section(key[0])\n\n\t\tself.config.set(key[0], key[1], str(value))\n\n\t\twith open(self.configfile, \"w\") as f:\n\t\t\tself.config.write(f)", "response": "Change the value of the given key in the given file to the given value."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nmigrates the old config file format to the new one", "response": "def _migrate_config(self, oldname=DEFAULT_CONFIG, newname=DEFAULT_CONFIG):\n\t\t\"\"\"\n\t\tMigrates the old config file format to the new one\n\t\t\"\"\"\n\t\tself._log(\"Your OAuth2Util config file is in an old format and needs \"\n\t\t\t\t\"to be changed. I tried as best as I could to migrate it.\", logging.WARNING)\n\n\t\twith open(oldname, \"r\") as old:\n\t\t\twith open(newname, \"w\") as new:\n\t\t\t\tnew.write(\"[app]\\n\")\n\t\t\t\tnew.write(old.read())"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _start_webserver(self, authorize_url=None):\n\t\tserver_address = (SERVER_URL, SERVER_PORT)\n\t\tself.server = HTTPServer(server_address, OAuth2UtilRequestHandler)\n\t\tself.server.response_code = None\n\t\tself.server.authorize_url = authorize_url\n\t\tt = Thread(target=self.server.serve_forever)\n\t\tt.daemon = True\n\t\tt.start()", "response": "Start the webserver that will receive the code\n\t"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nwait until the server has accepted or rejected the request.", "response": "def _wait_for_response(self):\n\t\t\"\"\"\n\t\tWait until the user accepted or rejected the request\n\t\t\"\"\"\n\t\twhile not self.server.response_code:\n\t\t\ttime.sleep(2)\n\t\ttime.sleep(5)\n\t\tself.server.shutdown()"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _get_new_access_information(self):\n\t\tif not self.r.has_oauth_app_info:\n\t\t\tself._log('Cannot obtain authorize url from PRAW. Please check your configuration.', logging.ERROR)\n\t\t\traise AttributeError('Reddit Session invalid, please check your designated config file.')\n\t\turl = self.r.get_authorize_url('UsingOAuth2Util',\n\t\t\t\t\t\tself._get_value(CONFIGKEY_SCOPE, set, split_val=','),\n\t\t\t\t\t\tself._get_value(CONFIGKEY_REFRESHABLE, as_boolean=True))\n\n\t\tself._start_webserver(url)\n\t\tif not self._get_value(CONFIGKEY_SERVER_MODE, as_boolean=True):\n\t\t\twebbrowser.open(url)\n\t\telse:\n\t\t\tprint(\"Webserver is waiting for you :D. Please open {0}:{1}/{2} \"\n\t\t\t\t\t\"in your browser\"\n\t\t\t\t.format(SERVER_URL, SERVER_PORT, SERVER_LINK_PATH))\n\t\tself._wait_for_response()\n\n\t\ttry:\n\t\t\taccess_information = self.r.get_access_information(\n\t\t\t\tself.server.response_code)\n\t\texcept praw.errors.OAuthException:\n\t\t\tself._log(\"Can not authenticate, maybe the app infos (e.g. secret) are wrong.\", logging.ERROR)\n\t\t\traise\n\n\t\tself._change_value(CONFIGKEY_TOKEN, access_information[\"access_token\"])\n\t\tself._change_value(CONFIGKEY_REFRESH_TOKEN, access_information[\"refresh_token\"])\n\t\tself._change_value(CONFIGKEY_VALID_UNTIL, time.time() + TOKEN_VALID_DURATION)", "response": "Get new access information from the reddit server."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _check_token_present(self):\n\t\ttry:\n\t\t\tself._get_value(CONFIGKEY_TOKEN)\n\t\t\tself._get_value(CONFIGKEY_REFRESH_TOKEN)\n\t\t\tself._get_value(CONFIGKEY_REFRESHABLE)\n\t\texcept KeyError:\n\t\t\tself._log(\"Request new Token (CTP)\")\n\t\t\tself._get_new_access_information()", "response": "Check whether the tokens are set and request new ones if not"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef set_access_credentials(self, _retry=0):\n\t\tif _retry >= 5:\n\t\t\traise ConnectionAbortedError('Reddit is not accessible right now, cannot refresh OAuth2 tokens.')\n\n\t\tself._check_token_present()\n\n\t\ttry:\n\t\t\tself.r.set_access_credentials(self._get_value(CONFIGKEY_SCOPE, set, split_val=\",\"),\n\t\t\t\t\t\t\t\t\t\t self._get_value(CONFIGKEY_TOKEN),\n\t\t\t\t\t\t\t\t\t\t self._get_value(CONFIGKEY_REFRESH_TOKEN))\n\t\texcept (praw.errors.OAuthInvalidToken, praw.errors.HTTPException) as e:\n\t\t\t# todo check e status code\n\t\t\t# self._log('Retrying in 5s.')\n\t\t\t# time.sleep(5)\n\t\t\t# self.set_access_credentials(_retry=_retry + 1)\n\n\t\t\tself._log(\"Request new Token (SAC)\")\n\t\t\tself._get_new_access_information()", "response": "Set the token on the Reddit Object again"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef refresh(self, force=False, _retry=0):\n\t\tif _retry >= 5:\n\t\t\traise ConnectionAbortedError('Reddit is not accessible right now, cannot refresh OAuth2 tokens.')\n\t\tself._check_token_present()\n\n\t\t# We check whether another instance already refreshed the token\n\t\tif time.time() > self._get_value(CONFIGKEY_VALID_UNTIL, float, exception_default=0) - REFRESH_MARGIN:\n\t\t\tself.config.read(self.configfile)\n\n\t\t\tif time.time() < self._get_value(CONFIGKEY_VALID_UNTIL, float, exception_default=0) - REFRESH_MARGIN:\n\t\t\t\tself._log(\"Found new token\")\n\t\t\t\tself.set_access_credentials()\n\n\t\tif force or time.time() > self._get_value(CONFIGKEY_VALID_UNTIL, float, exception_default=0) - REFRESH_MARGIN:\n\t\t\tself._log(\"Refresh Token\")\n\t\t\ttry:\n\t\t\t\tnew_token = self.r.refresh_access_information(self._get_value(CONFIGKEY_REFRESH_TOKEN))\n\t\t\t\tself._change_value(CONFIGKEY_TOKEN, new_token[\"access_token\"])\n\t\t\t\tself._change_value(CONFIGKEY_VALID_UNTIL, time.time() + TOKEN_VALID_DURATION)\n\t\t\t\tself.set_access_credentials()\n\t\t\texcept (praw.errors.OAuthInvalidToken, praw.errors.HTTPException) as e:\n\t\t\t\t# todo check e status code\n\t\t\t\t# self._log('Retrying in 5s.')\n\t\t\t\t# time.sleep(5)\n\t\t\t\t# self.refresh(_retry=_retry + 1)\n\n\t\t\t\tself._log(\"Request new Token (REF)\")\n\t\t\t\tself._get_new_access_information()", "response": "Refresh the token from Reddit"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncreate DynamoDB table for run manifests", "response": "def create_manifest_table(dynamodb_client, table_name):\n \"\"\"Create DynamoDB table for run manifests\n\n Arguments:\n dynamodb_client - boto3 DynamoDB client (not service)\n table_name - string representing existing table name\n \"\"\"\n try:\n dynamodb_client.create_table(\n AttributeDefinitions=[\n {\n 'AttributeName': DYNAMODB_RUNID_ATTRIBUTE,\n 'AttributeType': 'S'\n },\n ],\n TableName=table_name,\n KeySchema=[\n {\n 'AttributeName': DYNAMODB_RUNID_ATTRIBUTE,\n 'KeyType': 'HASH'\n },\n ],\n ProvisionedThroughput={\n 'ReadCapacityUnits': 5,\n 'WriteCapacityUnits': 5\n }\n )\n dynamodb_client.get_waiter('table_exists').wait(TableName=table_name)\n except ClientError as e:\n # Table already exists\n if e.response['Error']['Code'] == 'ResourceInUseException':\n pass\n else:\n raise e"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn list of all run ids inside S3 folder.", "response": "def list_runids(s3_client, full_path):\n \"\"\"Return list of all run ids inside S3 folder. It does not respect\n S3 pagination (`MaxKeys`) and returns **all** keys from bucket\n and won't list any prefixes with object archived to AWS Glacier\n\n Arguments:\n s3_client - boto3 S3 client (not service)\n full_path - full valid S3 path to events (such as enriched-archive)\n example: s3://acme-events-bucket/main-pipeline/enriched-archive\n \"\"\"\n listing_finished = False # last response was not truncated\n run_ids_buffer = []\n last_continuation_token = None\n\n (bucket, prefix) = split_full_path(full_path)\n\n while not listing_finished:\n options = clean_dict({\n 'Bucket': bucket,\n 'Prefix': prefix,\n 'Delimiter': '/',\n 'ContinuationToken': last_continuation_token\n })\n\n response = s3_client.list_objects_v2(**options)\n keys = [extract_run_id(key['Prefix']) for key\n in response.get('CommonPrefixes', [])]\n run_ids_buffer.extend([key for key in keys if key is not None])\n last_continuation_token = response.get('NextContinuationToken', None)\n\n if not response['IsTruncated']:\n listing_finished = True\n\n non_archived_run_ids = [run_id for run_id in run_ids_buffer\n if not is_glacier(s3_client, bucket, run_id)]\n return non_archived_run_ids"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nsplitting a full S3 path into a pair of bucket and path.", "response": "def split_full_path(path):\n \"\"\"Return pair of bucket without protocol and path\n\n Arguments:\n path - valid S3 path, such as s3://somebucket/events\n\n >>> split_full_path('s3://mybucket/path-to-events')\n ('mybucket', 'path-to-events/')\n >>> split_full_path('s3://mybucket')\n ('mybucket', None)\n >>> split_full_path('s3n://snowplow-bucket/some/prefix/')\n ('snowplow-bucket', 'some/prefix/')\n \"\"\"\n if path.startswith('s3://'):\n path = path[5:]\n elif path.startswith('s3n://'):\n path = path[6:]\n elif path.startswith('s3a://'):\n path = path[6:]\n else:\n raise ValueError(\"S3 path should start with s3://, s3n:// or \"\n \"s3a:// prefix\")\n parts = path.split('/')\n bucket = parts[0]\n path = '/'.join(parts[1:])\n return bucket, normalize_prefix(path)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nchecks if prefix is archived in Glacier", "response": "def is_glacier(s3_client, bucket, prefix):\n \"\"\"Check if prefix is archived in Glacier, by checking storage class of\n first object inside that prefix\n\n Arguments:\n s3_client - boto3 S3 client (not service)\n bucket - valid extracted bucket (without protocol and prefix)\n example: sowplow-events-data\n prefix - valid S3 prefix (usually, run_id)\n example: snowplow-archive/enriched/archive/\n \"\"\"\n response = s3_client.list_objects_v2(Bucket=bucket,\n Prefix=prefix,\n MaxKeys=3) # 3 to not fetch _SUCCESS\n\n for key in response['Contents']:\n if key.get('StorageClass', 'STANDARD') == 'GLACIER':\n return True\n return False"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef extract_run_id(key):\n filename = key.split('/')[-2] # -1 element is empty string\n run_id = filename.lstrip('run=')\n try:\n datetime.strptime(run_id, '%Y-%m-%d-%H-%M-%S')\n return key\n except ValueError:\n return None", "response": "Extract date part from run id"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nremove all keys with Nones as values", "response": "def clean_dict(dict):\n \"\"\"Remove all keys with Nones as values\n\n >>> clean_dict({'key': None})\n {}\n >>> clean_dict({'empty_s': ''})\n {'empty_s': ''}\n \"\"\"\n if sys.version_info[0] < 3:\n return {k: v for k, v in dict.iteritems() if v is not None}\n else:\n return {k: v for k, v in dict.items() if v is not None}"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nadd run_id into DynamoDB manifest table", "response": "def add_to_manifest(dynamodb_client, table_name, run_id):\n \"\"\"Add run_id into DynamoDB manifest table\n\n Arguments:\n dynamodb_client - boto3 DynamoDB client (not service)\n table_name - string representing existing table name\n run_id - string representing run_id to store\n \"\"\"\n dynamodb_client.put_item(\n TableName=table_name,\n Item={\n DYNAMODB_RUNID_ATTRIBUTE: {\n 'S': run_id\n }\n }\n )"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nchecks if run_id is stored in DynamoDB table.", "response": "def is_in_manifest(dynamodb_client, table_name, run_id):\n \"\"\"Check if run_id is stored in DynamoDB table.\n Return True if run_id is stored or False otherwise.\n\n Arguments:\n dynamodb_client - boto3 DynamoDB client (not service)\n table_name - string representing existing table name\n run_id - string representing run_id to store\n \"\"\"\n response = dynamodb_client.get_item(\n TableName=table_name,\n Key={\n DYNAMODB_RUNID_ATTRIBUTE: {\n 'S': run_id\n }\n }\n )\n return response.get('Item') is not None"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nextract the schema information from Iglu URI", "response": "def extract_schema(uri):\n \"\"\"\n Extracts Schema information from Iglu URI\n\n >>> extract_schema(\"iglu:com.acme-corporation_underscore/event_name-dash/jsonschema/1-10-1\")['vendor']\n 'com.acme-corporation_underscore'\n \"\"\"\n match = re.match(SCHEMA_URI_REGEX, uri)\n if match:\n return {\n 'vendor': match.group(1),\n 'name': match.group(2),\n 'format': match.group(3),\n 'version': match.group(4)\n\n }\n else:\n raise SnowplowEventTransformationException([\n \"Schema {} does not conform to regular expression {}\".format(uri, SCHEMA_URI)\n ])"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncreating an Elasticsearch field name from a schema string", "response": "def fix_schema(prefix, schema):\n \"\"\"\n Create an Elasticsearch field name from a schema string\n \"\"\"\n schema_dict = extract_schema(schema)\n snake_case_organization = schema_dict['vendor'].replace('.', '_').lower()\n snake_case_name = re.sub('([^A-Z_])([A-Z])', '\\g<1>_\\g<2>', schema_dict['name']).lower()\n model = schema_dict['version'].split('-')[0]\n return \"{}_{}_{}_{}\".format(prefix, snake_case_organization, snake_case_name, model)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef parse_contexts(contexts):\n my_json = json.loads(contexts)\n data = my_json['data']\n distinct_contexts = {}\n for context in data:\n schema = fix_schema(\"contexts\", context['schema'])\n inner_data = context['data']\n if schema not in distinct_contexts:\n distinct_contexts[schema] = [inner_data]\n else:\n distinct_contexts[schema].append(inner_data)\n output = []\n for key in distinct_contexts:\n output.append((key, distinct_contexts[key]))\n return output", "response": "Convert a contexts JSON to an Elasticsearch - compatible list of key - value pairs\n "} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef parse_unstruct(unstruct):\n my_json = json.loads(unstruct)\n data = my_json['data']\n schema = data['schema']\n if 'data' in data:\n inner_data = data['data']\n else:\n raise SnowplowEventTransformationException([\"Could not extract inner data field from unstructured event\"])\n fixed_schema = fix_schema(\"unstruct_event\", schema)\n return [(fixed_schema, inner_data)]", "response": "Convert an unstructured event JSON to a list containing one Elasticsearch - compatible key - value pair\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef transform(line, known_fields=ENRICHED_EVENT_FIELD_TYPES, add_geolocation_data=True):\n return jsonify_good_event(line.split('\\t'), known_fields, add_geolocation_data)", "response": "Convert a Snowplow enriched event TSV into a JSON\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nconverting a Snowplow enriched event into a JSON object.", "response": "def jsonify_good_event(event, known_fields=ENRICHED_EVENT_FIELD_TYPES, add_geolocation_data=True):\n \"\"\"\n Convert a Snowplow enriched event in the form of an array of fields into a JSON\n \"\"\"\n if len(event) != len(known_fields):\n raise SnowplowEventTransformationException(\n [\"Expected {} fields, received {} fields.\".format(len(known_fields), len(event))]\n )\n else:\n output = {}\n errors = []\n if add_geolocation_data and event[LATITUDE_INDEX] != '' and event[LONGITUDE_INDEX] != '':\n output['geo_location'] = event[LATITUDE_INDEX] + ',' + event[LONGITUDE_INDEX]\n for i in range(len(event)):\n key = known_fields[i][0]\n if event[i] != '':\n try:\n kvpairs = known_fields[i][1](key, event[i])\n for kvpair in kvpairs:\n output[kvpair[0]] = kvpair[1]\n except SnowplowEventTransformationException as sete:\n errors += sete.error_messages\n except Exception as e:\n errors += [\"Unexpected exception parsing field with key {} and value {}: {}\".format(\n known_fields[i][0],\n event[i],\n repr(e)\n )]\n if errors:\n raise SnowplowEventTransformationException(errors)\n else:\n return output"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nextracting the used view from the TemplateResponse context", "response": "def _get_view_data(self, context_data):\n \"\"\"\n Extract the used view from the TemplateResponse context (ContextMixin)\n \"\"\"\n view = context_data.get('view')\n if not isinstance(view, View):\n view = None\n\n # Denote interesting objects in the template context\n template_context = []\n for key, obj in context_data.items():\n if isinstance(obj, (BaseForm, BaseFormSet, Model)):\n template_context.append((key, _format_path(obj.__class__)))\n\n return {\n 'model': _get_view_model(view),\n 'form': _get_form_class(view),\n 'template_context': template_context,\n }"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_used_template(response):\n if not hasattr(response, 'template_name'):\n return None, None\n\n template = response.template_name\n if template is None:\n return None, None\n\n if isinstance(template, (list, tuple)):\n # See which template name was really used.\n if len(template) == 1:\n return template[0], None\n else:\n used_name = _get_used_template_name(template)\n return used_name, template\n elif isinstance(template, six.string_types):\n # Single string\n return template, None\n else:\n # Template object.\n filename = _get_template_filename(template)\n template_name = '