ShijiaD/CodeXGLUE-Code-Docstring-Validate
13
1[
2 {
3 "code": "def set_keyring(keyring):\n global _keyring_backend\n if not isinstance(keyring, backend.KeyringBackend):\n raise TypeError(\"The keyring must be a subclass of KeyringBackend\")\n _keyring_backend = keyring",
4 "docstring": "Set current keyring backend."
5 },
6 {
7 "code": "def run_program(prog_list, debug, shell):\n try:\n if not shell:\n process = Popen(prog_list, stdout=PIPE, stderr=PIPE)\n stdout, stderr = process.communicate()\n retcode = process.returncode\n if debug >= 1:\n print(\"Program : \", \" \".join(prog_list))\n print(\"Return Code: \", retcode)\n print(\"Stdout: \", stdout)\n print(\"Stderr: \", stderr)\n return bool(retcode)\n else:\n command = \" \".join(prog_list)\n os.system(command)\n return True\n except:\n return False",
8 "docstring": "Run a program and check program return code Note that some commands don't work\n well with Popen. So if this function is specifically called with 'shell=True',\n then it will run the old 'os.system'. In which case, there is no program output"
9 },
10 {
11 "code": "def fetch_repo_creation_date(self):\n gh = self.github\n user = self.options.user\n repo = self.options.project\n rc, data = gh.repos[user][repo].get()\n if rc == 200:\n return REPO_CREATED_TAG_NAME, data[\"created_at\"]\n else:\n self.raise_GitHubError(rc, data, gh.getheaders())\n return None, None",
12 "docstring": "Get the creation date of the repository from GitHub.\n\n :rtype: str, str\n :return: special tag name, creation date as ISO date string"
13 },
14 {
15 "code": "def inverse_dynamics(self, angles, start=0, end=1e100, states=None, max_force=100):\n if states is not None:\n self.skeleton.set_body_states(states)\n for frame_no, frame in enumerate(angles):\n if frame_no < start:\n continue\n if frame_no >= end:\n break\n self.ode_space.collide(None, self.on_collision)\n states = self.skeleton.get_body_states()\n self.skeleton.set_body_states(states)\n self.skeleton.enable_motors(max_force)\n self.skeleton.set_target_angles(angles[frame_no])\n self.ode_world.step(self.dt)\n torques = self.skeleton.joint_torques\n self.skeleton.disable_motors()\n self.skeleton.set_body_states(states)\n self.skeleton.add_torques(torques)\n yield torques\n self.ode_world.step(self.dt)\n self.ode_contactgroup.empty()",
16 "docstring": "Follow a set of angle data, yielding dynamic joint torques.\n\n Parameters\n ----------\n angles : ndarray (num-frames x num-dofs)\n Follow angle data provided by this array of angle values.\n start : int, optional\n Start following angle data after this frame. Defaults to the start\n of the angle data.\n end : int, optional\n Stop following angle data after this frame. Defaults to the end of\n the angle data.\n states : list of body states, optional\n If given, set the states of the skeleton bodies to these values\n before starting to follow the marker data.\n max_force : float, optional\n Allow each degree of freedom in the skeleton to exert at most this\n force when attempting to follow the given joint angles. Defaults to\n 100N. Setting this value to be large results in more accurate\n following but can cause oscillations in the PID controllers,\n resulting in noisy torques.\n\n Returns\n -------\n torques : sequence of torque frames\n Returns a generator of joint torque data for the skeleton. One set\n of joint torques will be generated for each frame of angle data\n between `start` and `end`."
17 },
18 {
19 "code": "def recursive_glob(base_directory, regex=''):\n files = glob(op.join(base_directory, regex))\n for path, dirlist, filelist in os.walk(base_directory):\n for dir_name in dirlist:\n files.extend(glob(op.join(path, dir_name, regex)))\n return files",
20 "docstring": "Uses glob to find all files or folders that match the regex\n starting from the base_directory.\n\n Parameters\n ----------\n base_directory: str\n\n regex: str\n\n Returns\n -------\n files: list"
21 },
22 {
23 "code": "def process_point_value(cls, command_type, command, index, op_type):\n _log.debug('Processing received point value for index {}: {}'.format(index, command))",
24 "docstring": "A PointValue was received from the Master. Process its payload.\n\n :param command_type: (string) Either 'Select' or 'Operate'.\n :param command: A ControlRelayOutputBlock or else a wrapped data value (AnalogOutputInt16, etc.).\n :param index: (integer) DNP3 index of the payload's data definition.\n :param op_type: An OperateType, or None if command_type == 'Select'."
25 },
26 {
27 "code": "def add_color_to_scheme(scheme, name, foreground, background, palette_colors):\n if foreground is None and background is None:\n return scheme\n new_scheme = []\n for item in scheme:\n if item[0] == name:\n if foreground is None:\n foreground = item[1]\n if background is None:\n background = item[2]\n if palette_colors > 16:\n new_scheme.append((name, '', '', '', foreground, background))\n else:\n new_scheme.append((name, foreground, background))\n else:\n new_scheme.append(item)\n return new_scheme",
28 "docstring": "Add foreground and background colours to a color scheme"
29 },
30 {
31 "code": "def array_2d_from_array_1d(self, array_1d):\n return mapping_util.map_masked_1d_array_to_2d_array_from_array_1d_shape_and_one_to_two(\n array_1d=array_1d, shape=self.mask.shape, one_to_two=self.mask.masked_grid_index_to_pixel)",
32 "docstring": "Map a 1D array the same dimension as the grid to its original masked 2D array.\n\n Parameters\n -----------\n array_1d : ndarray\n The 1D array which is mapped to its masked 2D array."
33 },
34 {
35 "code": "def connect():\n ftp_class = ftplib.FTP if not SSL else ftplib.FTP_TLS\n ftp = ftp_class(timeout=TIMEOUT)\n ftp.connect(HOST, PORT)\n ftp.login(USER, PASSWORD)\n if SSL:\n ftp.prot_p()\n return ftp",
36 "docstring": "Connect to FTP server, login and return an ftplib.FTP instance."
37 },
38 {
39 "code": "def to_table(components, topo_info):\n inputs, outputs = defaultdict(list), defaultdict(list)\n for ctype, component in components.items():\n if ctype == 'bolts':\n for component_name, component_info in component.items():\n for input_stream in component_info['inputs']:\n input_name = input_stream['component_name']\n inputs[component_name].append(input_name)\n outputs[input_name].append(component_name)\n info = []\n spouts_instance = topo_info['physical_plan']['spouts']\n bolts_instance = topo_info['physical_plan']['bolts']\n for ctype, component in components.items():\n if ctype == \"stages\":\n continue\n for component_name, component_info in component.items():\n row = [ctype[:-1], component_name]\n if ctype == 'spouts':\n row.append(len(spouts_instance[component_name]))\n else:\n row.append(len(bolts_instance[component_name]))\n row.append(','.join(inputs.get(component_name, ['-'])))\n row.append(','.join(outputs.get(component_name, ['-'])))\n info.append(row)\n header = ['type', 'name', 'parallelism', 'input', 'output']\n return info, header",
40 "docstring": "normalize raw logical plan info to table"
41 },
42 {
43 "code": "def pickle_load(path, compression=False):\n if compression:\n with zipfile.ZipFile(path, \"r\", compression=zipfile.ZIP_DEFLATED) as myzip:\n with myzip.open(\"data\") as f:\n return pickle.load(f)\n else:\n with open(path, \"rb\") as f:\n return pickle.load(f)",
44 "docstring": "Unpickle a possible compressed pickle.\n\n Parameters\n ----------\n path: str\n path to the output file\n compression: bool\n if true assumes that pickle was compressed when created and attempts decompression.\n\n Returns\n -------\n obj: object\n the unpickled object"
45 },
46 {
47 "code": "def get_stdout(self, workflow_id, task_id):\n url = '%(wf_url)s/%(wf_id)s/tasks/%(task_id)s/stdout' % {\n 'wf_url': self.workflows_url, 'wf_id': workflow_id, 'task_id': task_id\n }\n r = self.gbdx_connection.get(url)\n r.raise_for_status()\n return r.text",
48 "docstring": "Get stdout for a particular task.\n\n Args:\n workflow_id (str): Workflow id.\n task_id (str): Task id.\n\n Returns:\n Stdout of the task (string)."
49 },
50 {
51 "code": "def _getMatchingRowsNoRetries(self, tableInfo, conn, fieldsToMatch,\n selectFieldNames, maxRows=None):\n assert fieldsToMatch, repr(fieldsToMatch)\n assert all(k in tableInfo.dbFieldNames\n for k in fieldsToMatch.iterkeys()), repr(fieldsToMatch)\n assert selectFieldNames, repr(selectFieldNames)\n assert all(f in tableInfo.dbFieldNames for f in selectFieldNames), repr(\n selectFieldNames)\n matchPairs = fieldsToMatch.items()\n matchExpressionGen = (\n p[0] +\n (' IS ' + {True:'TRUE', False:'FALSE'}[p[1]] if isinstance(p[1], bool)\n else ' IS NULL' if p[1] is None\n else ' IN %s' if isinstance(p[1], self._SEQUENCE_TYPES)\n else '=%s')\n for p in matchPairs)\n matchFieldValues = [p[1] for p in matchPairs\n if (not isinstance(p[1], (bool)) and p[1] is not None)]\n query = 'SELECT %s FROM %s WHERE (%s)' % (\n ','.join(selectFieldNames), tableInfo.tableName,\n ' AND '.join(matchExpressionGen))\n sqlParams = matchFieldValues\n if maxRows is not None:\n query += ' LIMIT %s'\n sqlParams.append(maxRows)\n conn.cursor.execute(query, sqlParams)\n rows = conn.cursor.fetchall()\n if rows:\n assert maxRows is None or len(rows) <= maxRows, \"%d !<= %d\" % (\n len(rows), maxRows)\n assert len(rows[0]) == len(selectFieldNames), \"%d != %d\" % (\n len(rows[0]), len(selectFieldNames))\n else:\n rows = tuple()\n return rows",
52 "docstring": "Return a sequence of matching rows with the requested field values from\n a table or empty sequence if nothing matched.\n\n tableInfo: Table information: a ClientJobsDAO._TableInfoBase instance\n conn: Owned connection acquired from ConnectionFactory.get()\n fieldsToMatch: Dictionary of internal fieldName/value mappings that\n identify the desired rows. If a value is an instance of\n ClientJobsDAO._SEQUENCE_TYPES (list/set/tuple), then the\n operator 'IN' will be used in the corresponding SQL\n predicate; if the value is bool: \"IS TRUE/FALSE\"; if the\n value is None: \"IS NULL\"; '=' will be used for all other\n cases.\n selectFieldNames:\n list of fields to return, using internal field names\n maxRows: maximum number of rows to return; unlimited if maxRows\n is None\n\n retval: A sequence of matching rows, each row consisting of field\n values in the order of the requested field names. Empty\n sequence is returned when not match exists."
53 },
54 {
55 "code": "def clone(self):\n result = copy.copy(self)\n result._compound_mfrs = copy.deepcopy(self._compound_mfrs)\n return result",
56 "docstring": "Create a complete copy of the stream.\n\n :returns: A new MaterialStream object."
57 },
58 {
59 "code": "def makedirs(path):\n if not os.path.isdir(path):\n os.makedirs(path)\n return path",
60 "docstring": "Create directories if they do not exist, otherwise do nothing.\n\n Return path for convenience"
61 },
62 {
63 "code": "def get_element_mfr(self, element):\n result = 0.0\n for compound in self.material.compounds:\n formula = compound.split('[')[0]\n result += self.get_compound_mfr(compound) *\\\n stoich.element_mass_fraction(formula, element)\n return result",
64 "docstring": "Determine the mass flow rate of the specified elements in the stream.\n\n :returns: Mass flow rates. [kg/h]"
65 },
66 {
67 "code": "def characters(quantity=10):\n line = map(_to_lower_alpha_only,\n ''.join(random.sample(get_dictionary('lorem_ipsum'), quantity)))\n return ''.join(line)[:quantity]",
68 "docstring": "Return random characters."
69 },
70 {
71 "code": "def create_log_config(verbose, quiet):\n if verbose and quiet:\n raise ValueError(\n \"Supplying both --quiet and --verbose makes no sense.\"\n )\n elif verbose:\n level = logging.DEBUG\n elif quiet:\n level = logging.ERROR\n else:\n level = logging.INFO\n logger_cfg = {\"handlers\": [\"click_handler\"], \"level\": level}\n return {\n \"version\": 1,\n \"formatters\": {\"click_formatter\": {\"format\": \"%(message)s\"}},\n \"handlers\": {\n \"click_handler\": {\n \"level\": level,\n \"class\": \"doc2dash.__main__.ClickEchoHandler\",\n \"formatter\": \"click_formatter\",\n }\n },\n \"loggers\": {\"doc2dash\": logger_cfg, \"__main__\": logger_cfg},\n }",
72 "docstring": "We use logging's levels as an easy-to-use verbosity controller."
73 },
74 {
75 "code": "def _is_ndb(self):\n if isinstance(self._model, type):\n if _NDB_MODEL is not None and issubclass(self._model, _NDB_MODEL):\n return True\n elif issubclass(self._model, db.Model):\n return False\n raise TypeError(\n 'Model class not an NDB or DB model: {0}.'.format(self._model))",
76 "docstring": "Determine whether the model of the instance is an NDB model.\n\n Returns:\n Boolean indicating whether or not the model is an NDB or DB model."
77 },
78 {
79 "code": "def convert_clip(params, w_name, scope_name, inputs, layers, weights, names):\n print('Converting clip ...')\n if params['min'] == 0:\n print(\"using ReLU({0})\".format(params['max']))\n layer = keras.layers.ReLU(max_value=params['max'])\n else:\n def target_layer(x, vmin=params['min'], vmax=params['max']):\n import tensorflow as tf\n return tf.clip_by_value(x, vmin, vmax)\n layer = keras.layers.Lambda(target_layer)\n layers[scope_name] = layer(layers[inputs[0]])",
80 "docstring": "Convert clip operation.\n\n Args:\n params: dictionary with layer parameters\n w_name: name prefix in state_dict\n scope_name: pytorch scope name\n inputs: pytorch node inputs\n layers: dictionary with keras tensors\n weights: pytorch state_dict\n names: use short names for keras layers"
81 },
82 {
83 "code": "def add_newlines(f, output, char):\n line_count = get_line_count(f)\n f = open(f, 'r+')\n output = open(output, 'r+')\n for line in range(line_count):\n string = f.readline()\n string = re.sub(char, char + '\\n', string)\n output.write(string)",
84 "docstring": "Adds line breaks after every occurance of a given character in a file.\n\n Args:\n f: string, path to input file.\n\n output: string, path to output file.\n\n Returns:\n None."
85 },
86 {
87 "code": "def set_training(model, mode):\n if mode is None:\n yield\n return\n old_mode = model.training\n if old_mode != mode:\n model.train(mode)\n try:\n yield\n finally:\n if old_mode != mode:\n model.train(old_mode)",
88 "docstring": "A context manager to temporarily set the training mode of 'model'\n to 'mode', resetting it when we exit the with-block. A no-op if\n mode is None."
89 },
90 {
91 "code": "def fit(self, X, y):\n word_vector_transformer = WordVectorTransformer(padding='max')\n X = word_vector_transformer.fit_transform(X)\n X = LongTensor(X)\n self.word_vector_transformer = word_vector_transformer\n y_transformer = LabelEncoder()\n y = y_transformer.fit_transform(y)\n y = torch.from_numpy(y)\n self.y_transformer = y_transformer\n dataset = CategorizedDataset(X, y)\n dataloader = DataLoader(dataset,\n batch_size=self.batch_size,\n shuffle=True,\n num_workers=4)\n KERNEL_SIZES = self.kernel_sizes\n NUM_KERNEL = self.num_kernel\n EMBEDDING_DIM = self.embedding_dim\n model = TextCNN(\n vocab_size=word_vector_transformer.get_vocab_size(),\n embedding_dim=EMBEDDING_DIM,\n output_size=len(self.y_transformer.classes_),\n kernel_sizes=KERNEL_SIZES,\n num_kernel=NUM_KERNEL)\n if USE_CUDA:\n model = model.cuda()\n EPOCH = self.epoch\n LR = self.lr\n loss_function = nn.CrossEntropyLoss()\n optimizer = optim.Adam(model.parameters(), lr=LR)\n for epoch in range(EPOCH):\n losses = []\n for i, data in enumerate(dataloader):\n X, y = data\n X, y = Variable(X), Variable(y)\n optimizer.zero_grad()\n model.train()\n output = model(X)\n loss = loss_function(output, y)\n losses.append(loss.data.tolist()[0])\n loss.backward()\n optimizer.step()\n if i % 100 == 0:\n print(\"[%d/%d] mean_loss : %0.2f\" % (\n epoch, EPOCH, np.mean(losses)))\n losses = []\n self.model = model",
92 "docstring": "Fit KimCNNClassifier according to X, y\n\n Parameters\n ----------\n X : list of string\n each item is a raw text\n y : list of string\n each item is a label"
93 },
94 {
95 "code": "def copy_file(self, path, prefixed_path, source_storage):\n if prefixed_path in self.copied_files:\n return self.log(\"Skipping '%s' (already copied earlier)\" % path)\n if not self.delete_file(path, prefixed_path, source_storage):\n return\n source_path = source_storage.path(path)\n if self.dry_run:\n self.log(\"Pretending to copy '%s'\" % source_path, level=1)\n else:\n self.log(\"Copying '%s'\" % source_path, level=1)\n with source_storage.open(path) as source_file:\n self.storage.save(prefixed_path, source_file)\n self.copied_files.append(prefixed_path)",
96 "docstring": "Attempt to copy ``path`` with storage"
97 },
98 {
99 "code": "def calculate_average_scores_on_graph(\n graph: BELGraph,\n key: Optional[str] = None,\n tag: Optional[str] = None,\n default_score: Optional[float] = None,\n runs: Optional[int] = None,\n use_tqdm: bool = False,\n):\n subgraphs = generate_bioprocess_mechanisms(graph, key=key)\n scores = calculate_average_scores_on_subgraphs(\n subgraphs,\n key=key,\n tag=tag,\n default_score=default_score,\n runs=runs,\n use_tqdm=use_tqdm\n )\n return scores",
100 "docstring": "Calculate the scores over all biological processes in the sub-graph.\n\n As an implementation, it simply computes the sub-graphs then calls :func:`calculate_average_scores_on_subgraphs` as\n described in that function's documentation.\n\n :param graph: A BEL graph with heats already on the nodes\n :param key: The key in the node data dictionary representing the experimental data. Defaults to\n :data:`pybel_tools.constants.WEIGHT`.\n :param tag: The key for the nodes' data dictionaries where the scores will be put. Defaults to 'score'\n :param default_score: The initial score for all nodes. This number can go up or down.\n :param runs: The number of times to run the heat diffusion workflow. Defaults to 100.\n :param use_tqdm: Should there be a progress bar for runners?\n :return: A dictionary of {pybel node tuple: results tuple}\n :rtype: dict[tuple, tuple]\n\n Suggested usage with :mod:`pandas`:\n\n >>> import pandas as pd\n >>> from pybel_tools.analysis.heat import calculate_average_scores_on_graph\n >>> graph = ... # load graph and data\n >>> scores = calculate_average_scores_on_graph(graph)\n >>> pd.DataFrame.from_items(scores.items(), orient='index', columns=RESULT_LABELS)"
101 },
102 {
103 "code": "def _readxml(self):\n block = re.sub(r'<(/?)s>', r'<\\1s>', self._readblock())\n try:\n xml = XML(block)\n except ParseError:\n xml = None\n return xml",
104 "docstring": "Read a block and return the result as XML\n\n :return: block as xml\n :rtype: xml.etree.ElementTree"
105 },
106 {
107 "code": "def merge(*args):\n ret = []\n for arg in args:\n if isinstance(arg, list) or isinstance(arg, tuple):\n ret += list(arg)\n else:\n ret.append(arg)\n return ret",
108 "docstring": "Implements the 'merge' operator for merging lists."
109 },
110 {
111 "code": "def update_query_params(uri, params):\n parts = urllib.parse.urlparse(uri)\n query_params = parse_unique_urlencoded(parts.query)\n query_params.update(params)\n new_query = urllib.parse.urlencode(query_params)\n new_parts = parts._replace(query=new_query)\n return urllib.parse.urlunparse(new_parts)",
112 "docstring": "Updates a URI with new query parameters.\n\n If a given key from ``params`` is repeated in the ``uri``, then\n the URI will be considered invalid and an error will occur.\n\n If the URI is valid, then each value from ``params`` will\n replace the corresponding value in the query parameters (if\n it exists).\n\n Args:\n uri: string, A valid URI, with potential existing query parameters.\n params: dict, A dictionary of query parameters.\n\n Returns:\n The same URI but with the new query parameters added."
113 },
114 {
115 "code": "def posteriori_covariance(self):\n r\n K = GLMM.covariance(self)\n tau = self._ep._posterior.tau\n return pinv(pinv(K) + diag(1 / tau))",
116 "docstring": "r\"\"\" Covariance of the estimated posteriori."
117 },
118 {
119 "code": "def image(self, well_row, well_column, field_row, field_column):\n return next((i for i in self.images\n if attribute(i, 'u') == well_column and\n attribute(i, 'v') == well_row and\n attribute(i, 'x') == field_column and\n attribute(i, 'y') == field_row), '')",
120 "docstring": "Get path of specified image.\n\n Parameters\n ----------\n well_row : int\n Starts at 0. Same as --U in files.\n well_column : int\n Starts at 0. Same as --V in files.\n field_row : int\n Starts at 0. Same as --Y in files.\n field_column : int\n Starts at 0. Same as --X in files.\n\n Returns\n -------\n string\n Path to image or empty string if image is not found."
121 },
122 {
123 "code": "def j2(x):\n to_return = 2./(x+1e-15)*j1(x) - j0(x)\n to_return[x==0] = 0\n return to_return",
124 "docstring": "A fast j2 defined in terms of other special functions"
125 },
126 {
127 "code": "def getInstanceJstack(self, topology_info, instance_id):\n pid_response = yield getInstancePid(topology_info, instance_id)\n try:\n http_client = tornado.httpclient.AsyncHTTPClient()\n pid_json = json.loads(pid_response)\n pid = pid_json['stdout'].strip()\n if pid == '':\n raise Exception('Failed to get pid')\n endpoint = utils.make_shell_endpoint(topology_info, instance_id)\n url = \"%s/jstack/%s\" % (endpoint, pid)\n response = yield http_client.fetch(url)\n Log.debug(\"HTTP call for url: %s\", url)\n raise tornado.gen.Return(response.body)\n except tornado.httpclient.HTTPError as e:\n raise Exception(str(e))",
128 "docstring": "Fetches Instance jstack from heron-shell."
129 },
130 {
131 "code": "def tsms(when, tz=None):\n if not when:\n return None\n when = totz(when, tz)\n return calendar.timegm(when.timetuple()) * 1000 + int(round(when.microsecond / 1000.0))",
132 "docstring": "Return a Unix timestamp in milliseconds for the provided datetime. The `totz` function is\n called on the datetime to convert it to the provided timezone. It will be converted to UTC if\n no timezone is provided."
133 },
134 {
135 "code": "def bitsToString(arr):\n s = array('c','.'*len(arr))\n for i in xrange(len(arr)):\n if arr[i] == 1:\n s[i]='*'\n return s",
136 "docstring": "Returns a string representing a numpy array of 0's and 1's"
137 },
138 {
139 "code": "def assign_params(sess, params, network):\n ops = []\n for idx, param in enumerate(params):\n ops.append(network.all_params[idx].assign(param))\n if sess is not None:\n sess.run(ops)\n return ops",
140 "docstring": "Assign the given parameters to the TensorLayer network.\n\n Parameters\n ----------\n sess : Session\n TensorFlow Session.\n params : list of array\n A list of parameters (array) in order.\n network : :class:`Layer`\n The network to be assigned.\n\n Returns\n --------\n list of operations\n A list of tf ops in order that assign params. Support sess.run(ops) manually.\n\n Examples\n --------\n - See ``tl.files.save_npz``\n\n References\n ----------\n - `Assign value to a TensorFlow variable <http://stackoverflow.com/questions/34220532/how-to-assign-value-to-a-tensorflow-variable>`__"
141 },
142 {
143 "code": "def count(self):\n xml = get_changeset(self.id)\n actions = [action.tag for action in xml.getchildren()]\n self.create = actions.count('create')\n self.modify = actions.count('modify')\n self.delete = actions.count('delete')\n self.verify_editor()\n try:\n if (self.create / len(actions) > self.percentage and\n self.create > self.create_threshold and\n (self.powerfull_editor or self.create > self.top_threshold)):\n self.label_suspicious('possible import')\n elif (self.modify / len(actions) > self.percentage and\n self.modify > self.modify_threshold):\n self.label_suspicious('mass modification')\n elif ((self.delete / len(actions) > self.percentage and\n self.delete > self.delete_threshold) or\n self.delete > self.top_threshold):\n self.label_suspicious('mass deletion')\n except ZeroDivisionError:\n print('It seems this changeset was redacted')",
144 "docstring": "Count the number of elements created, modified and deleted by the\n changeset and analyses if it is a possible import, mass modification or\n a mass deletion."
145 },
146 {
147 "code": "def bounding_box(self):\n min_x, min_y, max_x, max_y = zip(*list(self.walk_rows(\n lambda row: row.bounding_box)))\n return min(min_x), min(min_y), max(max_x), max(max_y)",
148 "docstring": "The minimum and maximum bounds of this layout.\n\n :return: ``(min_x, min_y, max_x, max_y)`` the bounding box\n of this layout\n :rtype: tuple"
149 },
150 {
151 "code": "def swish(x, name='swish'):\n with tf.name_scope(name):\n x = tf.nn.sigmoid(x) * x\n return x",
152 "docstring": "Swish function.\n\n See `Swish: a Self-Gated Activation Function <https://arxiv.org/abs/1710.05941>`__.\n\n Parameters\n ----------\n x : Tensor\n input.\n name: str\n function name (optional).\n\n Returns\n -------\n Tensor\n A ``Tensor`` in the same type as ``x``."
153 },
154 {
155 "code": "def transform_title(self, content_metadata_item):\n title_with_locales = []\n for locale in self.enterprise_configuration.get_locales():\n title_with_locales.append({\n 'locale': locale,\n 'value': content_metadata_item.get('title', '')\n })\n return title_with_locales",
156 "docstring": "Return the title of the content item."
157 },
158 {
159 "code": "def annotate_metadata_data(repo, task, patterns=[\"*\"], size=0):\n mgr = plugins_get_mgr() \n keys = mgr.search('representation')['representation']\n representations = [mgr.get_by_key('representation', k) for k in keys]\n matching_files = repo.find_matching_files(patterns)\n package = repo.package\n rootdir = repo.rootdir\n files = package['resources']\n for f in files:\n relativepath = f['relativepath']\n if relativepath in matching_files:\n path = os.path.join(rootdir, relativepath)\n if task == 'preview':\n print(\"Adding preview for \", relativepath)\n f['content'] = open(path).read()[:size]\n elif task == 'schema':\n for r in representations: \n if r.can_process(path): \n print(\"Adding schema for \", path)\n f['schema'] = r.get_schema(path)\n break",
160 "docstring": "Update metadata with the content of the files"
161 },
162 {
163 "code": "def ast2str(expr, level=0, names=None):\n if isinstance(expr, Expression):\n return ast2str(expr.body, 0, names) \\\n if hasattr(expr, \"body\") else \"\"\n elif isinstance(expr, Name):\n return names.get(expr.id, expr.id) if names else expr.id\n elif isinstance(expr, BoolOp):\n op = expr.op\n if isinstance(op, Or):\n str_exp = \" or \".join(ast2str(i, level + 1, names)\n for i in expr.values)\n elif isinstance(op, And):\n str_exp = \" and \".join(ast2str(i, level + 1, names)\n for i in expr.values)\n else:\n raise TypeError(\"unsupported operation \" + op.__class__.__name)\n return \"(\" + str_exp + \")\" if level else str_exp\n elif expr is None:\n return \"\"\n else:\n raise TypeError(\"unsupported operation \" + repr(expr))",
164 "docstring": "convert compiled ast to gene_reaction_rule str\n\n Parameters\n ----------\n expr : str\n string for a gene reaction rule, e.g \"a and b\"\n level : int\n internal use only\n names : dict\n Dict where each element id a gene identifier and the value is the\n gene name. Use this to get a rule str which uses names instead. This\n should be done for display purposes only. All gene_reaction_rule\n strings which are computed with should use the id.\n\n Returns\n ------\n string\n The gene reaction rule"
165 },
166 {
167 "code": "def multitype_sort(a):\n types = defaultdict(list)\n numbers = {int, float, complex}\n for x in a:\n t = type(x)\n if t in numbers:\n types['number'].append(x)\n else:\n types[t].append(x)\n for t in types:\n types[t] = np.sort(types[t])\n return list(chain(*(types[t] for t in types)))",
168 "docstring": "Sort elements of multiple types\n\n x is assumed to contain elements of different types, such that\n plain sort would raise a `TypeError`.\n\n Parameters\n ----------\n a : array-like\n Array of items to be sorted\n\n Returns\n -------\n out : list\n Items sorted within their type groups."
169 },
170 {
171 "code": "def dist(self, src, tar):\n if src == tar:\n return 0.0\n src_comp = self._rle.encode(self._bwt.encode(src))\n tar_comp = self._rle.encode(self._bwt.encode(tar))\n concat_comp = self._rle.encode(self._bwt.encode(src + tar))\n concat_comp2 = self._rle.encode(self._bwt.encode(tar + src))\n return (\n min(len(concat_comp), len(concat_comp2))\n - min(len(src_comp), len(tar_comp))\n ) / max(len(src_comp), len(tar_comp))",
172 "docstring": "Return the NCD between two strings using BWT plus RLE.\n\n Parameters\n ----------\n src : str\n Source string for comparison\n tar : str\n Target string for comparison\n\n Returns\n -------\n float\n Compression distance\n\n Examples\n --------\n >>> cmp = NCDbwtrle()\n >>> cmp.dist('cat', 'hat')\n 0.75\n >>> cmp.dist('Niall', 'Neil')\n 0.8333333333333334\n >>> cmp.dist('aluminum', 'Catalan')\n 1.0\n >>> cmp.dist('ATCG', 'TAGC')\n 0.8"
173 },
174 {
175 "code": "def _ensure_managed_repos_dir_exists():\n if not os.path.exists(constants.REPOS_DIR):\n os.makedirs(constants.REPOS_DIR)",
176 "docstring": "Our exports file will be invalid if this folder doesn't exist, and the NFS server\n will not run correctly."
177 },
178 {
179 "code": "def convert_lsstdoc_tex(\n content, to_fmt, deparagraph=False, mathjax=False,\n smart=True, extra_args=None):\n augmented_content = '\\n'.join((LSSTDOC_MACROS, content))\n return convert_text(\n augmented_content, 'latex', to_fmt,\n deparagraph=deparagraph, mathjax=mathjax,\n smart=smart, extra_args=extra_args)",
180 "docstring": "Convert lsstdoc-class LaTeX to another markup format.\n\n This function is a thin wrapper around `convert_text` that automatically\n includes common lsstdoc LaTeX macros.\n\n Parameters\n ----------\n content : `str`\n Original content.\n\n to_fmt : `str`\n Output format for the content (see https://pandoc.org/MANUAL.html).\n For example, 'html5'.\n\n deparagraph : `bool`, optional\n If `True`, then the\n `lsstprojectmeta.pandoc.filters.deparagraph.deparagraph` filter is\n used to remove paragraph (``<p>``, for example) tags around a single\n paragraph of content. That filter does not affect content that\n consists of multiple blocks (several paragraphs, or lists, for\n example). Default is `False`.\n\n For example, **without** this filter Pandoc will convert\n the string ``\"Title text\"`` to ``\"<p>Title text</p>\"`` in HTML. The\n paragraph tags aren't useful if you intend to wrap the converted\n content in different tags, like ``<h1>``, using your own templating\n system.\n\n **With** this filter, Pandoc will convert the string ``\"Title text\"``\n to ``\"Title text\"`` in HTML.\n\n mathjax : `bool`, optional\n If `True` then Pandoc will markup output content to work with MathJax.\n Default is False.\n\n smart : `bool`, optional\n If `True` (default) then ascii characters will be converted to unicode\n characters like smart quotes and em dashes.\n\n extra_args : `list`, optional\n Sequence of Pandoc arguments command line arguments (such as\n ``'--normalize'``). The ``deparagraph``, ``mathjax``, and ``smart``\n arguments are convenience arguments that are equivalent to items\n in ``extra_args``.\n\n Returns\n -------\n output : `str`\n Content in the output (``to_fmt``) format.\n\n Notes\n -----\n This function will automatically install Pandoc if it is not available.\n See `ensure_pandoc`."
181 },
182 {
183 "code": "async def manage(self):\n cm = _ContextManager(self.database)\n if isinstance(self.database.obj, AIODatabase):\n cm.connection = await self.database.async_connect()\n else:\n cm.connection = self.database.connect()\n return cm",
184 "docstring": "Manage a database connection."
185 },
186 {
187 "code": "def extract_features_and_generate_model(essays, algorithm=util_functions.AlgorithmTypes.regression):\n f = feature_extractor.FeatureExtractor()\n f.initialize_dictionaries(essays)\n train_feats = f.gen_feats(essays)\n set_score = numpy.asarray(essays._score, dtype=numpy.int)\n if len(util_functions.f7(list(set_score)))>5:\n algorithm = util_functions.AlgorithmTypes.regression\n else:\n algorithm = util_functions.AlgorithmTypes.classification\n clf,clf2 = get_algorithms(algorithm)\n cv_error_results=get_cv_error(clf2,train_feats,essays._score)\n try:\n clf.fit(train_feats, set_score)\n except ValueError:\n log.exception(\"Not enough classes (0,1,etc) in sample.\")\n set_score[0]=1\n set_score[1]=0\n clf.fit(train_feats, set_score)\n return f, clf, cv_error_results",
188 "docstring": "Feed in an essay set to get feature vector and classifier\n essays must be an essay set object\n additional array is an optional argument that can specify\n a numpy array of values to add in\n returns a trained FeatureExtractor object and a trained classifier"
189 },
190 {
191 "code": "def pick_coda_from_decimal(decimal):\n decimal = Decimal(decimal)\n __, digits, exp = decimal.as_tuple()\n if exp < 0:\n return DIGIT_CODAS[digits[-1]]\n __, digits, exp = decimal.normalize().as_tuple()\n index = bisect_right(EXP_INDICES, exp) - 1\n if index < 0:\n return DIGIT_CODAS[digits[-1]]\n else:\n return EXP_CODAS[EXP_INDICES[index]]",
192 "docstring": "Picks only a coda from a decimal."
193 },
194 {
195 "code": "def tool_factory(clsname, name, driver, base=GromacsCommand):\n clsdict = {\n 'command_name': name,\n 'driver': driver,\n '__doc__': property(base._get_gmx_docs)\n }\n return type(clsname, (base,), clsdict)",
196 "docstring": "Factory for GromacsCommand derived types."
197 },
198 {
199 "code": "def get_extr_lics_xref(self, extr_lic):\n xrefs = list(self.graph.triples((extr_lic, RDFS.seeAlso, None)))\n return map(lambda xref_triple: xref_triple[2], xrefs)",
200 "docstring": "Return a list of cross references."
201 },
202 {
203 "code": "def norm_remote_path(path):\n path = os.path.normpath(path)\n if path.startswith(os.path.sep):\n return path[1:]\n else:\n return path",
204 "docstring": "Normalize `path`.\n\n All remote paths are absolute."
205 },
206 {
207 "code": "def save_component(self, component_name, save_path):\n component = self.get_component(component_name=component_name)\n self._validate_savable(component=component, component_name=component_name)\n return component.save(sess=self.session, save_path=save_path)",
208 "docstring": "Saves a component of this model to the designated location.\n\n Args:\n component_name: The component to save.\n save_path: The location to save to.\n Returns:\n Checkpoint path where the component was saved."
209 },
210 {
211 "code": "def dsa_sign(private_key, data, hash_algorithm):\n if private_key.algorithm != 'dsa':\n raise ValueError('The key specified is not a DSA private key')\n return _sign(private_key, data, hash_algorithm)",
212 "docstring": "Generates a DSA signature\n\n :param private_key:\n The PrivateKey to generate the signature with\n\n :param data:\n A byte string of the data the signature is for\n\n :param hash_algorithm:\n A unicode string of \"md5\", \"sha1\", \"sha256\", \"sha384\" or \"sha512\"\n\n :raises:\n ValueError - when any of the parameters contain an invalid value\n TypeError - when any of the parameters are of the wrong type\n OSError - when an error is returned by the OS crypto library\n\n :return:\n A byte string of the signature"
213 },
214 {
215 "code": "def generateStats(filename, maxSamples = None,):\n statsCollectorMapping = {'float': FloatStatsCollector,\n 'int': IntStatsCollector,\n 'string': StringStatsCollector,\n 'datetime': DateTimeStatsCollector,\n 'bool': BoolStatsCollector,\n }\n filename = resource_filename(\"nupic.datafiles\", filename)\n print \"*\"*40\n print \"Collecting statistics for file:'%s'\" % (filename,)\n dataFile = FileRecordStream(filename)\n statsCollectors = []\n for fieldName, fieldType, fieldSpecial in dataFile.getFields():\n statsCollector = \\\n statsCollectorMapping[fieldType](fieldName, fieldType, fieldSpecial)\n statsCollectors.append(statsCollector)\n if maxSamples is None:\n maxSamples = 500000\n for i in xrange(maxSamples):\n record = dataFile.getNextRecord()\n if record is None:\n break\n for i, value in enumerate(record):\n statsCollectors[i].addValue(value)\n stats = {}\n for statsCollector in statsCollectors:\n statsCollector.getStats(stats)\n if dataFile.getResetFieldIdx() is not None:\n resetFieldName,_,_ = dataFile.getFields()[dataFile.reset]\n stats.pop(resetFieldName)\n if VERBOSITY > 0:\n pprint.pprint(stats)\n return stats",
216 "docstring": "Collect statistics for each of the fields in the user input data file and\n return a stats dict object.\n\n Parameters:\n ------------------------------------------------------------------------------\n filename: The path and name of the data file.\n maxSamples: Upper bound on the number of rows to be processed\n retval: A dictionary of dictionaries. The top level keys are the\n field names and the corresponding values are the statistics\n collected for the individual file.\n Example:\n {\n 'consumption':{'min':0,'max':90,'mean':50,...},\n 'gym':{'numDistinctCategories':10,...},\n ...\n }"
217 },
218 {
219 "code": "def add_condor_job(self, token, batchmaketaskid, jobdefinitionfilename,\n outputfilename, errorfilename, logfilename,\n postfilename):\n parameters = dict()\n parameters['token'] = token\n parameters['batchmaketaskid'] = batchmaketaskid\n parameters['jobdefinitionfilename'] = jobdefinitionfilename\n parameters['outputfilename'] = outputfilename\n parameters['errorfilename'] = errorfilename\n parameters['logfilename'] = logfilename\n parameters['postfilename'] = postfilename\n response = self.request('midas.batchmake.add.condor.job', parameters)\n return response",
220 "docstring": "Add a Condor DAG job to the Condor DAG associated with this\n Batchmake task\n\n :param token: A valid token for the user in question.\n :type token: string\n :param batchmaketaskid: id of the Batchmake task for this DAG\n :type batchmaketaskid: int | long\n :param jobdefinitionfilename: Filename of the definition file for the\n job\n :type jobdefinitionfilename: string\n :param outputfilename: Filename of the output file for the job\n :type outputfilename: string\n :param errorfilename: Filename of the error file for the job\n :type errorfilename: string\n :param logfilename: Filename of the log file for the job\n :type logfilename: string\n :param postfilename: Filename of the post script log file for the job\n :type postfilename: string\n :return: The created Condor job DAO.\n :rtype: dict"
221 },
222 {
223 "code": "def export(self, model_name, export_folder):\n for transformer in self.transformers:\n if isinstance(transformer, MultiLabelBinarizer):\n joblib.dump(transformer,\n join(export_folder, \"label.transformer.bin\"),\n protocol=2)\n if isinstance(transformer, TfidfVectorizer):\n joblib.dump(transformer,\n join(export_folder, \"tfidf.transformer.bin\"),\n protocol=2)\n if isinstance(transformer, CountVectorizer):\n joblib.dump(transformer,\n join(export_folder, \"count.transformer.bin\"),\n protocol=2)\n if isinstance(transformer, NumberRemover):\n joblib.dump(transformer,\n join(export_folder, \"number.transformer.bin\"),\n protocol=2)\n model = [model for model in self.models if model.name == model_name][0]\n e = Experiment(self.X, self.y, model.estimator, None)\n model_filename = join(export_folder, \"model.bin\")\n e.export(model_filename)",
224 "docstring": "Export model and transformers to export_folder\n\n Parameters\n ----------\n model_name: string\n name of model to export\n export_folder: string\n folder to store exported model and transformers"
225 },
226 {
227 "code": "def clear(self, *args, **kwargs):\n super(Deposit, self).clear(*args, **kwargs)",
228 "docstring": "Clear only drafts.\n\n Status required: ``'draft'``.\n\n Meta information inside `_deposit` are preserved."
229 },
230 {
231 "code": "def pitch_contour(annotation, **kwargs):\n ax = kwargs.pop('ax', None)\n ax = mir_eval.display.__get_axes(ax=ax)[0]\n times, values = annotation.to_interval_values()\n indices = np.unique([v['index'] for v in values])\n for idx in indices:\n rows = [i for (i, v) in enumerate(values) if v['index'] == idx]\n freqs = np.asarray([values[r]['frequency'] for r in rows])\n unvoiced = ~np.asarray([values[r]['voiced'] for r in rows])\n freqs[unvoiced] *= -1\n ax = mir_eval.display.pitch(times[rows, 0], freqs, unvoiced=True,\n ax=ax,\n **kwargs)\n return ax",
232 "docstring": "Plotting wrapper for pitch contours"
233 },
234 {
235 "code": "def encode(self, o):\n if isinstance(o, basestring):\n if isinstance(o, str):\n _encoding = self.encoding\n if (_encoding is not None \n and not (_encoding == 'utf-8')):\n o = o.decode(_encoding)\n if self.ensure_ascii:\n return encode_basestring_ascii(o)\n else:\n return encode_basestring(o)\n chunks = list(self.iterencode(o))\n return ''.join(chunks)",
236 "docstring": "Return a JSON string representation of a Python data structure.\n\n >>> JSONEncoder().encode({\"foo\": [\"bar\", \"baz\"]})\n '{\"foo\": [\"bar\", \"baz\"]}'"
237 },
238 {
239 "code": "def child(self, offset256):\n a = bytes(self.pubkey) + offset256\n s = hashlib.sha256(a).digest()\n return self.derive_from_seed(s)",
240 "docstring": "Derive new private key from this key and a sha256 \"offset\""
241 },
242 {
243 "code": "def pop_data(self):\n data = self.data\n self.data = SortedKeyList(key=self._key)\n return data",
244 "docstring": "Replace this observation's data with a fresh container.\n\n Returns\n -------\n annotation_data : SortedKeyList\n The original annotation data container"
245 },
246 {
247 "code": "def _isCheckpointDir(checkpointDir):\n lastSegment = os.path.split(checkpointDir)[1]\n if lastSegment[0] == '.':\n return False\n if not checkpointDir.endswith(g_defaultCheckpointExtension):\n return False\n if not os.path.isdir(checkpointDir):\n return False\n return True",
248 "docstring": "Return true iff checkpointDir appears to be a checkpoint directory."
249 },
250 {
251 "code": "def from_participantid(participant_id):\n return user.UserID(\n chat_id=participant_id.chat_id,\n gaia_id=participant_id.gaia_id\n )",
252 "docstring": "Convert hangouts_pb2.ParticipantId to UserID."
253 },
254 {
255 "code": "def draw_freehand(self):\n if _ctx._ns[\"mousedown\"]:\n x, y = mouse()\n if self.show_grid:\n x, y = self.grid.snap(x, y)\n if self.freehand_move == True:\n cmd = MOVETO\n self.freehand_move = False\n else:\n cmd = LINETO\n pt = PathElement()\n if cmd != MOVETO:\n pt.freehand = True\n else:\n pt.freehand = False\n pt.cmd = cmd\n pt.x = x\n pt.y = y\n pt.ctrl1 = Point(x,y)\n pt.ctrl2 = Point(x,y)\n self._points.append(pt)\n r = 4\n _ctx.nofill()\n _ctx.stroke(self.handle_color)\n _ctx.oval(pt.x-r, pt.y-r, r*2, r*2)\n _ctx.fontsize(9)\n _ctx.fill(self.handle_color)\n _ctx.text(\" (\"+str(int(pt.x))+\", \"+str(int(pt.y))+\")\", pt.x+r, pt.y)\n self._dirty = True\n else:\n self.freehand_move = True\n if self._dirty:\n self._points[-1].freehand = False\n self.export_svg()\n self._dirty = False",
256 "docstring": "Freehand sketching."
257 },
258 {
259 "code": "def get_user_by_email(self, email):\n parameters = dict()\n parameters['email'] = email\n response = self.request('midas.user.get', parameters)\n return response",
260 "docstring": "Get a user by the email of that user.\n\n :param email: The email of the desired user.\n :type email: string\n :returns: The user requested.\n :rtype: dict"
261 },
262 {
263 "code": "def _stack_positions(positions, pos_in_dollars=True):\n if pos_in_dollars:\n positions = get_percent_alloc(positions)\n positions = positions.drop('cash', axis='columns')\n positions = positions.stack()\n positions.index = positions.index.set_names(['dt', 'ticker'])\n return positions",
264 "docstring": "Convert positions to percentages if necessary, and change them\n to long format.\n\n Parameters\n ----------\n positions: pd.DataFrame\n Daily holdings (in dollars or percentages), indexed by date.\n Will be converted to percentages if positions are in dollars.\n Short positions show up as cash in the 'cash' column.\n\n pos_in_dollars : bool\n Flag indicating whether `positions` are in dollars or percentages\n If True, positions are in dollars."
265 },
266 {
267 "code": "def app_class():\n try:\n pkg_resources.get_distribution('invenio-files-rest')\n from invenio_files_rest.app import Flask as FlaskBase\n except pkg_resources.DistributionNotFound:\n from flask import Flask as FlaskBase\n class Request(TrustedHostsMixin, FlaskBase.request_class):\n pass\n class Flask(FlaskBase):\n request_class = Request\n return Flask",
268 "docstring": "Create Flask application class.\n\n Invenio-Files-REST needs to patch the Werkzeug form parsing in order to\n support streaming large file uploads. This is done by subclassing the Flask\n application class."
269 },
270 {
271 "code": "def write_json(self, fh, pretty=True):\n sjson = json.JSONEncoder().encode(self.json())\n if pretty:\n json.dump(json.loads(sjson), fh, sort_keys=True, indent=4)\n else:\n json.dump(json.loads(sjson), fh)\n return",
272 "docstring": "Write composite object to file handle in JSON format.\n\n Args:\n fh (file): File handle to write to.\n pretty (bool): Sort keys and indent in output."
273 },
274 {
275 "code": "def graph_background(s):\n if s.background == None:\n s._ctx.background(None)\n else:\n s._ctx.background(s.background) \n if s.depth:\n try:\n clr = colors.color(s.background).darker(0.2)\n p = s._ctx.rect(0, 0, s._ctx.WIDTH, s._ctx.HEIGHT, draw=False)\n colors.gradientfill(p, clr, clr.lighter(0.35))\n colors.shadow(dx=0, dy=0, blur=2, alpha=0.935, clr=s.background)\n except:\n pass",
276 "docstring": "Graph background color."
277 },
278 {
279 "code": "def md_dimension_info(name, node):\n def _get_value(child_name):\n return getattr(node.find(child_name), 'text', None)\n resolution = _get_value('resolution')\n defaultValue = node.find(\"defaultValue\")\n strategy = defaultValue.find(\"strategy\") if defaultValue is not None else None\n strategy = strategy.text if strategy is not None else None\n return DimensionInfo(\n name,\n _get_value('enabled') == 'true',\n _get_value('presentation'),\n int(resolution) if resolution else None,\n _get_value('units'),\n _get_value('unitSymbol'),\n strategy,\n _get_value('attribute'),\n _get_value('endAttribute'),\n _get_value('referenceValue'),\n _get_value('nearestMatchEnabled')\n )",
280 "docstring": "Extract metadata Dimension Info from an xml node"
281 },
282 {
283 "code": "def fulladder_gate(variables, vartype=dimod.BINARY, name='FULL_ADDER'):\n variables = tuple(variables)\n if vartype is dimod.BINARY:\n configs = frozenset([(0, 0, 0, 0, 0),\n (0, 0, 1, 1, 0),\n (0, 1, 0, 1, 0),\n (0, 1, 1, 0, 1),\n (1, 0, 0, 1, 0),\n (1, 0, 1, 0, 1),\n (1, 1, 0, 0, 1),\n (1, 1, 1, 1, 1)])\n else:\n configs = frozenset([(-1, -1, -1, -1, -1),\n (-1, -1, +1, +1, -1),\n (-1, +1, -1, +1, -1),\n (-1, +1, +1, -1, +1),\n (+1, -1, -1, +1, -1),\n (+1, -1, +1, -1, +1),\n (+1, +1, -1, -1, +1),\n (+1, +1, +1, +1, +1)])\n def func(in1, in2, in3, sum_, carry):\n total = (in1 > 0) + (in2 > 0) + (in3 > 0)\n if total == 0:\n return (sum_ <= 0) and (carry <= 0)\n elif total == 1:\n return (sum_ > 0) and (carry <= 0)\n elif total == 2:\n return (sum_ <= 0) and (carry > 0)\n elif total == 3:\n return (sum_ > 0) and (carry > 0)\n else:\n raise ValueError(\"func recieved unexpected values\")\n return Constraint(func, configs, variables, vartype=vartype, name=name)",
284 "docstring": "Full adder.\n\n Args:\n variables (list): Variable labels for the and gate as `[in1, in2, in3, sum, carry]`,\n where `in1, in2, in3` are inputs to be added and `sum` and 'carry' the resultant\n outputs.\n vartype (Vartype, optional, default='BINARY'): Variable type. Accepted\n input values:\n\n * Vartype.SPIN, 'SPIN', {-1, 1}\n * Vartype.BINARY, 'BINARY', {0, 1}\n name (str, optional, default='FULL_ADDER'): Name for the constraint.\n\n Returns:\n Constraint(:obj:`.Constraint`): Constraint that is satisfied when its variables are\n assigned values that match the valid states of a Boolean full adder.\n\n Examples:\n >>> import dwavebinarycsp\n >>> import dwavebinarycsp.factories.constraint.gates as gates\n >>> csp = dwavebinarycsp.ConstraintSatisfactionProblem(dwavebinarycsp.BINARY)\n >>> csp.add_constraint(gates.fulladder_gate(['a', 'b', 'c_in', 'total', 'c_out'], name='FA1'))\n >>> csp.check({'a': 1, 'b': 0, 'c_in': 1, 'total': 0, 'c_out': 1})\n True"
285 },
286 {
287 "code": "def plot_grid(grid_arcsec, array, units, kpc_per_arcsec, pointsize, zoom_offset_arcsec):\n if grid_arcsec is not None:\n if zoom_offset_arcsec is not None:\n grid_arcsec -= zoom_offset_arcsec\n grid_units = convert_grid_units(grid_arcsec=grid_arcsec, array=array, units=units,\n kpc_per_arcsec=kpc_per_arcsec)\n plt.scatter(y=np.asarray(grid_units[:, 0]), x=np.asarray(grid_units[:, 1]), s=pointsize, c='k')",
288 "docstring": "Plot a grid of points over the array of data on the figure.\n\n Parameters\n -----------.\n grid_arcsec : ndarray or data.array.grids.RegularGrid\n A grid of (y,x) coordinates in arc-seconds which may be plotted over the array.\n array : data.array.scaled_array.ScaledArray\n The 2D array of data which is plotted.\n units : str\n The units of the y / x axis of the plots, in arc-seconds ('arcsec') or kiloparsecs ('kpc').\n kpc_per_arcsec : float or None\n The conversion factor between arc-seconds and kiloparsecs, required to plot the units in kpc.\n grid_pointsize : int\n The size of the points plotted to show the grid."
289 },
290 {
291 "code": "def _handleModelRunnerException(jobID, modelID, jobsDAO, experimentDir, logger,\n e):\n msg = StringIO.StringIO()\n print >>msg, \"Exception occurred while running model %s: %r (%s)\" % (\n modelID, e, type(e))\n traceback.print_exc(None, msg)\n completionReason = jobsDAO.CMPL_REASON_ERROR\n completionMsg = msg.getvalue()\n logger.error(completionMsg)\n if type(e) is not InvalidConnectionException:\n jobsDAO.modelUpdateResults(modelID, results=None, numRecords=0)\n if type(e) == JobFailException:\n workerCmpReason = jobsDAO.jobGetFields(jobID,\n ['workerCompletionReason'])[0]\n if workerCmpReason == ClientJobsDAO.CMPL_REASON_SUCCESS:\n jobsDAO.jobSetFields(jobID, fields=dict(\n cancel=True,\n workerCompletionReason = ClientJobsDAO.CMPL_REASON_ERROR,\n workerCompletionMsg = \": \".join(str(i) for i in e.args)),\n useConnectionID=False,\n ignoreUnchanged=True)\n return (completionReason, completionMsg)",
292 "docstring": "Perform standard handling of an exception that occurs while running\n a model.\n\n Parameters:\n -------------------------------------------------------------------------\n jobID: ID for this hypersearch job in the jobs table\n modelID: model ID\n jobsDAO: ClientJobsDAO instance\n experimentDir: directory containing the experiment\n logger: the logger to use\n e: the exception that occurred\n retval: (completionReason, completionMsg)"
293 },
294 {
295 "code": "def included(self, path, is_dir=False):\n inclusive = None\n for pattern in self.patterns:\n if pattern.is_dir == is_dir and pattern.matches(path):\n inclusive = pattern.inclusive\n return inclusive",
296 "docstring": "Check patterns in order, last match that includes or excludes `path` wins. Return `None` on undecided."
297 },
298 {
299 "code": "def upload(self, filename, location=''):\n current_folder = self._ftp.pwd()\n self.mkdir(location)\n self.cd(location)\n fl = open(filename, 'rb')\n filename = filename.split('/')[-1]\n self._ftp.storbinary('STOR %s' % filename, fl)\n fl.close()\n self.cd(current_folder)",
300 "docstring": "Uploads a file on the server to the desired location\n\n :param filename: the name of the file to be uploaded.\n :type filename: string\n :param location: the directory in which the file will\n be stored.\n :type location: string"
301 },
302 {
303 "code": "def get_configured_dns():\n ips = []\n try:\n output = subprocess.check_output(['nmcli', 'device', 'show'])\n output = output.decode('utf-8')\n for line in output.split('\\n'):\n if 'DNS' in line:\n pattern = r\"\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\"\n for hit in re.findall(pattern, line):\n ips.append(hit)\n except FileNotFoundError:\n pass\n return ips",
304 "docstring": "Returns the configured DNS servers with the use f nmcli."
305 },
306 {
307 "code": "def calc_intent(self, query):\n matches = self.calc_intents(query)\n if len(matches) == 0:\n return MatchData('', '')\n best_match = max(matches, key=lambda x: x.conf)\n best_matches = (match for match in matches if match.conf == best_match.conf)\n return min(best_matches, key=lambda x: sum(map(len, x.matches.values())))",
308 "docstring": "Tests all the intents against the query and returns\n match data of the best intent\n\n Args:\n query (str): Input sentence to test against intents\n Returns:\n MatchData: Best intent match"
309 },
310 {
311 "code": "def FlagCxx14Features(filename, clean_lines, linenum, error):\n line = clean_lines.elided[linenum]\n include = Match(r'\\s*\n if include and include.group(1) in ('scoped_allocator', 'shared_mutex'):\n error(filename, linenum, 'build/c++14', 5,\n ('<%s> is an unapproved C++14 header.') % include.group(1))",
312 "docstring": "Flag those C++14 features that we restrict.\n\n Args:\n filename: The name of the current file.\n clean_lines: A CleansedLines instance containing the file.\n linenum: The number of the line to check.\n error: The function to call with any errors found."
313 },
314 {
315 "code": "def _cleanup(self):\r\n self.exit()\r\n workspace = osp.join(os.getcwd(), 'octave-workspace')\r\n if osp.exists(workspace):\r\n os.remove(workspace)",
316 "docstring": "Clean up resources used by the session."
317 },
318 {
319 "code": "def _GetNextLogCountPerToken(token):\n global _log_counter_per_token\n _log_counter_per_token[token] = 1 + _log_counter_per_token.get(token, -1)\n return _log_counter_per_token[token]",
320 "docstring": "Wrapper for _log_counter_per_token.\n\n Args:\n token: The token for which to look up the count.\n\n Returns:\n The number of times this function has been called with\n *token* as an argument (starting at 0)"
321 },
322 {
323 "code": "def write(self, chunk, serialize=False, format=None):\n self.require_not_closed()\n if chunk is None:\n return\n if serialize or format is not None:\n self.serialize(chunk, format=format)\n return\n if type(chunk) is six.binary_type:\n self._length += len(chunk)\n self._stream.write(chunk)\n elif isinstance(chunk, six.string_types):\n encoding = self.encoding\n if encoding is not None:\n chunk = chunk.encode(encoding)\n else:\n raise exceptions.InvalidOperation(\n 'Attempting to write textual data without an encoding.')\n self._length += len(chunk)\n self._stream.write(chunk)\n elif isinstance(chunk, collections.Iterable):\n for section in chunk:\n self.write(section)\n else:\n raise exceptions.InvalidOperation(\n 'Attempting to write something not recognized.')",
324 "docstring": "Writes the given chunk to the output buffer.\n\n @param[in] chunk\n Either a byte array, a unicode string, or a generator. If `chunk`\n is a generator then calling `self.write(<generator>)` is\n equivalent to:\n\n @code\n for x in <generator>:\n self.write(x)\n self.flush()\n @endcode\n\n @param[in] serialize\n True to serialize the lines in a determined serializer.\n\n @param[in] format\n A specific format to serialize in; if provided, no detection is\n done. If not provided, the accept header (as well as the URL\n extension) is looked at to determine an appropriate serializer."
325 },
326 {
327 "code": "def grompp_qtot(*args, **kwargs):\n qtot_pattern = re.compile('System has non-zero total charge: *(?P<qtot>[-+]?\\d*\\.\\d+([eE][-+]\\d+)?)')\n kwargs['stdout'] = False\n kwargs['stderr'] = False\n rc, output, error = grompp_warnonly(*args, **kwargs)\n gmxoutput = \"\\n\".join([x for x in [output, error] if x is not None])\n if rc != 0:\n msg = \"grompp_qtot() failed. See warning and screen output for clues.\"\n logger.error(msg)\n import sys\n sys.stderr.write(\"=========== grompp (stdout/stderr) ============\\n\")\n sys.stderr.write(gmxoutput)\n sys.stderr.write(\"===============================================\\n\")\n sys.stderr.flush()\n raise GromacsError(rc, msg)\n qtot = 0\n for line in gmxoutput.split('\\n'):\n m = qtot_pattern.search(line)\n if m:\n qtot = float(m.group('qtot'))\n break\n logger.info(\"system total charge qtot = {qtot!r}\".format(**vars()))\n return qtot",
328 "docstring": "Run ``gromacs.grompp`` and return the total charge of the system.\n\n :Arguments:\n The arguments are the ones one would pass to :func:`gromacs.grompp`.\n :Returns:\n The total charge as reported\n\n Some things to keep in mind:\n\n * The stdout output of grompp is only shown when an error occurs. For\n debugging, look at the log file or screen output and try running the\n normal :func:`gromacs.grompp` command and analyze the output if the\n debugging messages are not sufficient.\n\n * Check that ``qtot`` is correct. Because the function is based on pattern\n matching of the informative output of :program:`grompp` it can break when\n the output format changes. This version recognizes lines like ::\n\n ' System has non-zero total charge: -4.000001e+00'\n\n using the regular expression\n :regexp:`System has non-zero total charge: *(?P<qtot>[-+]?\\d*\\.\\d+([eE][-+]\\d+)?)`."
329 },
330 {
331 "code": "def parse_query_string(self, query):\n if not query:\n return None\n if query[0] == '(':\n index = self.find_closing_braces(query)\n if index != len(query) - 1:\n raise Exception(\"Invalid syntax\")\n else:\n return self.parse_query_string(query[1:-1])\n start_index = query.find(\"(\")\n if start_index < 0:\n try:\n constant = float(query)\n return constant\n except ValueError:\n raise Exception(\"Invalid syntax\")\n token = query[:start_index]\n if token not in self.operators:\n raise Exception(\"Invalid token: \" + token)\n rest_of_the_query = query[start_index:]\n braces_end_index = self.find_closing_braces(rest_of_the_query)\n if braces_end_index != len(rest_of_the_query) - 1:\n raise Exception(\"Invalid syntax\")\n parts = self.get_sub_parts(rest_of_the_query[1:-1])\n if token == \"TS\":\n return self.operators[token](parts)\n children = []\n for part in parts:\n children.append(self.parse_query_string(part))\n node = self.operators[token](children)\n return node",
332 "docstring": "Returns a parse tree for the query, each of the node is a\n subclass of Operator. This is both a lexical as well as syntax analyzer step."
333 },
334 {
335 "code": "def solve_prop(self, goal, reset_method=True):\n r\n if self.Tmin is None or self.Tmax is None:\n raise Exception('Both a minimum and a maximum value are not present indicating there is not enough data for temperature dependency.')\n if not self.test_property_validity(goal):\n raise Exception('Input property is not considered plausible; no method would calculate it.')\n def error(T):\n if reset_method:\n self.method = None\n return self.T_dependent_property(T) - goal\n try:\n return brenth(error, self.Tmin, self.Tmax)\n except ValueError:\n raise Exception('To within the implemented temperature range, it is not possible to calculate the desired value.')",
336 "docstring": "r'''Method to solve for the temperature at which a property is at a\n specified value. `T_dependent_property` is used to calculate the value\n of the property as a function of temperature; if `reset_method` is True,\n the best method is used at each temperature as the solver seeks a\n solution. This slows the solution moderately.\n\n Checks the given property value with `test_property_validity` first\n and raises an exception if it is not valid. Requires that Tmin and\n Tmax have been set to know what range to search within.\n\n Search is performed with the brenth solver from SciPy.\n\n Parameters\n ----------\n goal : float\n Propoerty value desired, [`units`]\n reset_method : bool\n Whether or not to reset the method as the solver searches\n\n Returns\n -------\n T : float\n Temperature at which the property is the specified value [K]"
337 },
338 {
339 "code": "def _make_prefixed(self, name, is_element, declared_prefixes, declarations):\n namespace, name = self._split_qname(name, is_element)\n if namespace is None:\n prefix = None\n elif namespace in declared_prefixes:\n prefix = declared_prefixes[namespace]\n elif namespace in self._prefixes:\n prefix = self._prefixes[namespace]\n declarations[namespace] = prefix\n declared_prefixes[namespace] = prefix\n else:\n if is_element:\n prefix = None\n else:\n prefix = self._make_prefix(declared_prefixes)\n declarations[namespace] = prefix\n declared_prefixes[namespace] = prefix\n if prefix:\n return prefix + u\":\" + name\n else:\n return name",
340 "docstring": "Return namespace-prefixed tag or attribute name.\n\n Add appropriate declaration to `declarations` when neccessary.\n\n If no prefix for an element namespace is defined, make the elements\n namespace default (no prefix). For attributes, make up a prefix in such\n case.\n\n :Parameters:\n - `name`: QName ('{namespace-uri}local-name')\n to convert\n - `is_element`: `True` for element, `False` for an attribute\n - `declared_prefixes`: mapping of prefixes already declared\n at this scope\n - `declarations`: XMLNS declarations on the current element.\n :Types:\n - `name`: `unicode`\n - `is_element`: `bool`\n - `declared_prefixes`: `unicode` to `unicode` dictionary\n - `declarations`: `unicode` to `unicode` dictionary\n\n :Returntype: `unicode`"
341 },
342 {
343 "code": "def processed_shape(self, shape):\n for processor in self.preprocessors:\n shape = processor.processed_shape(shape=shape)\n return shape",
344 "docstring": "Shape of preprocessed state given original shape.\n\n Args:\n shape: original state shape\n\n Returns: processed state shape"
345 },
346 {
347 "code": "def transaction(func):\n @wraps(func)\n def wrapper(cls, *args, **kwargs):\n with (yield from cls.get_cursor(_CursorType.NAMEDTUPLE)) as c:\n try:\n yield from c.execute('BEGIN')\n result = (yield from func(cls, c, *args, **kwargs))\n except Exception:\n yield from c.execute('ROLLBACK')\n else:\n yield from c.execute('COMMIT')\n return result\n return wrapper",
348 "docstring": "Provides a transacted cursor which will run in autocommit=false mode\n\n For any exception the transaction will be rolled back.\n Requires that the function being decorated is an instance of a class or object\n that yields a cursor from a get_cursor(cursor_type=CursorType.NAMEDTUPLE) coroutine or provides such an object\n as the first argument in its signature\n\n Yields:\n A client-side transacted named cursor"
349 },
350 {
351 "code": "def IsBlockInNameSpace(nesting_state, is_forward_declaration):\n if is_forward_declaration:\n return len(nesting_state.stack) >= 1 and (\n isinstance(nesting_state.stack[-1], _NamespaceInfo))\n return (len(nesting_state.stack) > 1 and\n nesting_state.stack[-1].check_namespace_indentation and\n isinstance(nesting_state.stack[-2], _NamespaceInfo))",
352 "docstring": "Checks that the new block is directly in a namespace.\n\n Args:\n nesting_state: The _NestingState object that contains info about our state.\n is_forward_declaration: If the class is a forward declared class.\n Returns:\n Whether or not the new block is directly in a namespace."
353 },
354 {
355 "code": "def getAccountFromPrivateKey(self, wif):\n pub = self.publickey_from_wif(wif)\n return self.getAccountFromPublicKey(pub)",
356 "docstring": "Obtain account name from private key"
357 },
358 {
359 "code": "def _make_session(connection: Optional[str] = None) -> Session:\n if connection is None:\n connection = get_global_connection()\n engine = create_engine(connection)\n create_all(engine)\n session_cls = sessionmaker(bind=engine)\n session = session_cls()\n return session",
360 "docstring": "Make a session."
361 },
362 {
363 "code": "def less_or_equal(a, b, *args):\n return (\n less(a, b) or soft_equals(a, b)\n ) and (not args or less_or_equal(b, *args))",
364 "docstring": "Implements the '<=' operator with JS-style type coertion."
365 },
366 {
367 "code": "def find_external_compartment(model):\n if model.boundary:\n counts = pd.Series(tuple(r.compartments)[0] for r in model.boundary)\n most = counts.value_counts()\n most = most.index[most == most.max()].to_series()\n else:\n most = None\n like_external = compartment_shortlist[\"e\"] + [\"e\"]\n matches = pd.Series([co in like_external for co in model.compartments],\n index=model.compartments)\n if matches.sum() == 1:\n compartment = matches.index[matches][0]\n LOGGER.info(\"Compartment `%s` sounds like an external compartment. \"\n \"Using this one without counting boundary reactions\" %\n compartment)\n return compartment\n elif most is not None and matches.sum() > 1 and matches[most].sum() == 1:\n compartment = most[matches[most]][0]\n LOGGER.warning(\"There are several compartments that look like an \"\n \"external compartment but `%s` has the most boundary \"\n \"reactions, so using that as the external \"\n \"compartment.\" % compartment)\n return compartment\n elif matches.sum() > 1:\n raise RuntimeError(\"There are several compartments (%s) that look \"\n \"like external compartments but we can't tell \"\n \"which one to use. Consider renaming your \"\n \"compartments please.\")\n if most is not None:\n return most[0]\n LOGGER.warning(\"Could not identify an external compartment by name and\"\n \" choosing one with the most boundary reactions. That \"\n \"might be complete nonsense or change suddenly. \"\n \"Consider renaming your compartments using \"\n \"`Model.compartments` to fix this.\")\n raise RuntimeError(\"The heuristic for discovering an external compartment \"\n \"relies on names and boundary reactions. Yet, there \"\n \"are neither compartments with recognized names nor \"\n \"boundary reactions in the model.\")",
368 "docstring": "Find the external compartment in the model.\n\n Uses a simple heuristic where the external compartment should be the one\n with the most exchange reactions.\n\n Arguments\n ---------\n model : cobra.Model\n A cobra model.\n\n Returns\n -------\n str\n The putative external compartment."
369 },
370 {
371 "code": "def _nginx_location_spec(port_spec, bridge_ip):\n location_string_spec = \"\\t \\t location / { \\n\"\n for location_setting in ['proxy_http_version 1.1;',\n 'proxy_set_header Upgrade $http_upgrade;',\n 'proxy_set_header Connection \"upgrade\";',\n 'proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;',\n 'proxy_set_header Host $http_host;',\n _nginx_proxy_string(port_spec, bridge_ip)]:\n location_string_spec += \"\\t \\t \\t {} \\n\".format(location_setting)\n location_string_spec += \"\\t \\t } \\n\"\n return location_string_spec",
372 "docstring": "This will output the nginx location config string for specific port spec"
373 },
374 {
375 "code": "def update_extend(dst, src):\n for k, v in src.items():\n existing = dst.setdefault(k, [])\n for x in v:\n if x not in existing:\n existing.append(x)",
376 "docstring": "Update the `dst` with the `src`, extending values where lists.\n\n Primiarily useful for integrating results from `get_library_config`."
377 },
378 {
379 "code": "def _fixupRandomEncoderParams(params, minVal, maxVal, minResolution):\n encodersDict = (\n params[\"modelConfig\"][\"modelParams\"][\"sensorParams\"][\"encoders\"]\n )\n for encoder in encodersDict.itervalues():\n if encoder is not None:\n if encoder[\"type\"] == \"RandomDistributedScalarEncoder\":\n resolution = max(minResolution,\n (maxVal - minVal) / encoder.pop(\"numBuckets\")\n )\n encodersDict[\"c1\"][\"resolution\"] = resolution",
380 "docstring": "Given model params, figure out the correct parameters for the\n RandomDistributed encoder. Modifies params in place."
381 },
382 {
383 "code": "def set_stream(self,stream):\n _unused = stream\n if self.joined and self.handler:\n self.handler.user_left(self.me,None)\n self.joined=False",
384 "docstring": "Called when current stream changes.\n\n Mark the room not joined and inform `self.handler` that it was left.\n\n :Parameters:\n - `stream`: the new stream.\n :Types:\n - `stream`: `pyxmpp.stream.Stream`"
385 },
386 {
387 "code": "def set_state(_id, body):\n url = DEVICE_URL % _id\n if \"mode\" in body:\n url = MODES_URL % _id\n arequest = requests.put(url, headers=HEADERS, data=json.dumps(body))\n status_code = str(arequest.status_code)\n if status_code != '202':\n _LOGGER.error(\"State not accepted. \" + status_code)\n return False",
388 "docstring": "Set a devices state."
389 },
390 {
391 "code": "def upload_s3(cfg, path_to_zip_file, *use_s3):\n print('Uploading your new Lambda function')\n profile_name = cfg.get('profile')\n aws_access_key_id = cfg.get('aws_access_key_id')\n aws_secret_access_key = cfg.get('aws_secret_access_key')\n client = get_client(\n 's3', profile_name, aws_access_key_id, aws_secret_access_key,\n cfg.get('region'),\n )\n byte_stream = b''\n with open(path_to_zip_file, mode='rb') as fh:\n byte_stream = fh.read()\n s3_key_prefix = cfg.get('s3_key_prefix', '/dist')\n checksum = hashlib.new('md5', byte_stream).hexdigest()\n timestamp = str(time.time())\n filename = '{prefix}{checksum}-{ts}.zip'.format(\n prefix=s3_key_prefix, checksum=checksum, ts=timestamp,\n )\n buck_name = (\n os.environ.get('S3_BUCKET_NAME') or cfg.get('bucket_name')\n )\n func_name = (\n os.environ.get('LAMBDA_FUNCTION_NAME') or cfg.get('function_name')\n )\n kwargs = {\n 'Bucket': '{}'.format(buck_name),\n 'Key': '{}'.format(filename),\n 'Body': byte_stream,\n }\n client.put_object(**kwargs)\n print('Finished uploading {} to S3 bucket {}'.format(func_name, buck_name))\n if use_s3:\n return filename",
392 "docstring": "Upload a function to AWS S3."
393 },
394 {
395 "code": "def log_file(self, url=None):\n if url is None:\n url = self.url\n f = re.sub(\"file://\", \"\", url)\n try:\n with open(f, \"a\") as of:\n of.write(str(self.store.get_json_tuples(True)))\n except IOError as e:\n print(e)\n print(\"Could not write the content to the file..\")",
396 "docstring": "Write to a local log file"
397 },
398 {
399 "code": "def build_dag(data, samples):\n snames = [i.name for i in samples]\n dag = nx.DiGraph()\n joborder = JOBORDER[data.paramsdict[\"assembly_method\"]]\n for sname in snames:\n for func in joborder:\n dag.add_node(\"{}-{}-{}\".format(func, 0, sname))\n for chunk in xrange(10):\n dag.add_node(\"{}-{}-{}\".format(\"muscle_align\", chunk, sname))\n dag.add_node(\"{}-{}-{}\".format(\"reconcat\", 0, sname))\n for sname in snames:\n for sname2 in snames:\n dag.add_edge(\"{}-{}-{}\".format(joborder[0], 0, sname2),\n \"{}-{}-{}\".format(joborder[1], 0, sname))\n for idx in xrange(2, len(joborder)):\n dag.add_edge(\"{}-{}-{}\".format(joborder[idx-1], 0, sname),\n \"{}-{}-{}\".format(joborder[idx], 0, sname))\n for sname2 in snames:\n for chunk in range(10):\n dag.add_edge(\"{}-{}-{}\".format(\"muscle_chunker\", 0, sname2),\n \"{}-{}-{}\".format(\"muscle_align\", chunk, sname))\n dag.add_edge(\"{}-{}-{}\".format(\"muscle_align\", chunk, sname),\n \"{}-{}-{}\".format(\"reconcat\", 0, sname))\n return dag, joborder",
400 "docstring": "build a directed acyclic graph describing jobs to be run in order."
401 },
402 {
403 "code": "def save_intraday(data: pd.DataFrame, ticker: str, dt, typ='TRADE'):\n cur_dt = pd.Timestamp(dt).strftime('%Y-%m-%d')\n logger = logs.get_logger(save_intraday, level='debug')\n info = f'{ticker} / {cur_dt} / {typ}'\n data_file = hist_file(ticker=ticker, dt=dt, typ=typ)\n if not data_file: return\n if data.empty:\n logger.warning(f'data is empty for {info} ...')\n return\n exch = const.exch_info(ticker=ticker)\n if exch.empty: return\n end_time = pd.Timestamp(\n const.market_timing(ticker=ticker, dt=dt, timing='FINISHED')\n ).tz_localize(exch.tz)\n now = pd.Timestamp('now', tz=exch.tz) - pd.Timedelta('1H')\n if end_time > now:\n logger.debug(f'skip saving cause market close ({end_time}) < now - 1H ({now}) ...')\n return\n logger.info(f'saving data to {data_file} ...')\n files.create_folder(data_file, is_file=True)\n data.to_parquet(data_file)",
404 "docstring": "Check whether data is done for the day and save\n\n Args:\n data: data\n ticker: ticker\n dt: date\n typ: [TRADE, BID, ASK, BID_BEST, ASK_BEST, BEST_BID, BEST_ASK]\n\n Examples:\n >>> os.environ['BBG_ROOT'] = 'xbbg/tests/data'\n >>> sample = pd.read_parquet('xbbg/tests/data/aapl.parq')\n >>> save_intraday(sample, 'AAPL US Equity', '2018-11-02')\n >>> # Invalid exchange\n >>> save_intraday(sample, 'AAPL XX Equity', '2018-11-02')\n >>> # Invalid empty data\n >>> save_intraday(pd.DataFrame(), 'AAPL US Equity', '2018-11-02')\n >>> # Invalid date - too close\n >>> cur_dt = utils.cur_time()\n >>> save_intraday(sample, 'AAPL US Equity', cur_dt)"
405 },
406 {
407 "code": "def fetch_and_filter_tags(self):\n self.all_tags = self.fetcher.get_all_tags()\n self.filtered_tags = self.get_filtered_tags(self.all_tags)\n self.fetch_tags_dates()",
408 "docstring": "Fetch and filter tags, fetch dates and sort them in time order."
409 },
410 {
411 "code": "def __load_jams_schema():\n schema_file = os.path.join(SCHEMA_DIR, 'jams_schema.json')\n jams_schema = None\n with open(resource_filename(__name__, schema_file), mode='r') as fdesc:\n jams_schema = json.load(fdesc)\n if jams_schema is None:\n raise JamsError('Unable to load JAMS schema')\n return jams_schema",
412 "docstring": "Load the schema file from the package."
413 },
414 {
415 "code": "def MoveToCenter(self) -> bool:\n if self.IsTopLevel():\n rect = self.BoundingRectangle\n screenWidth, screenHeight = GetScreenSize()\n x, y = (screenWidth - rect.width()) // 2, (screenHeight - rect.height()) // 2\n if x < 0: x = 0\n if y < 0: y = 0\n return SetWindowPos(self.NativeWindowHandle, SWP.HWND_Top, x, y, 0, 0, SWP.SWP_NoSize)\n return False",
416 "docstring": "Move window to screen center."
417 },
418 {
419 "code": "def delete_tenant_quota(context, tenant_id):\n tenant_quotas = context.session.query(Quota)\n tenant_quotas = tenant_quotas.filter_by(tenant_id=tenant_id)\n tenant_quotas.delete()",
420 "docstring": "Delete the quota entries for a given tenant_id.\n\n Atfer deletion, this tenant will use default quota values in conf."
421 },
422 {
423 "code": "def transform(self, mode=None):\n if mode:\n self._canvas.mode = mode\n return self._canvas.mode",
424 "docstring": "Set the current transform mode.\n\n :param mode: CENTER or CORNER"
425 },
426 {
427 "code": "def make_clean_figure(figsize, remove_tooltips=False, remove_keybindings=False):\n tooltip = mpl.rcParams['toolbar']\n if remove_tooltips:\n mpl.rcParams['toolbar'] = 'None'\n fig = pl.figure(figsize=figsize)\n mpl.rcParams['toolbar'] = tooltip\n if remove_keybindings:\n fig.canvas.mpl_disconnect(fig.canvas.manager.key_press_handler_id)\n return fig",
428 "docstring": "Makes a `matplotlib.pyplot.Figure` without tooltips or keybindings\n\n Parameters\n ----------\n figsize : tuple\n Figsize as passed to `matplotlib.pyplot.figure`\n remove_tooltips, remove_keybindings : bool\n Set to True to remove the tooltips bar or any key bindings,\n respectively. Default is False\n\n Returns\n -------\n fig : `matplotlib.pyplot.Figure`"
429 },
430 {
431 "code": "def _get_valid_formats():\n if NO_SOX:\n return []\n so = subprocess.check_output(['sox', '-h'])\n if type(so) is not str:\n so = str(so, encoding='UTF-8')\n so = so.split('\\n')\n idx = [i for i in range(len(so)) if 'AUDIO FILE FORMATS:' in so[i]][0]\n formats = so[idx].split(' ')[3:]\n return formats",
432 "docstring": "Calls SoX help for a lists of audio formats available with the current\n install of SoX.\n\n Returns:\n --------\n formats : list\n List of audio file extensions that SoX can process."
433 },
434 {
435 "code": "def contains(self, clr):\n if not isinstance(clr, Color):\n return False\n if not isinstance(clr, _list):\n clr = [clr]\n for clr in clr:\n if clr.is_grey and not self.grayscale:\n return (self.black.contains(clr) or \\\n self.white.contains(clr))\n for r, v in [(self.h, clr.h), (self.s, clr.s), (self.b, clr.brightness), (self.a, clr.a)]:\n if isinstance(r, _list):\n pass\n elif isinstance(r, tuple):\n r = [r]\n else:\n r = [(r, r)]\n for min, max in r:\n if not (min <= v <= max):\n return False\n return True",
436 "docstring": "Returns True if the given color is part of this color range.\n\n Check whether each h, s, b, a component of the color\n falls within the defined range for that component.\n\n If the given color is grayscale,\n checks against the definitions for black and white."
437 },
438 {
439 "code": "def append(self, item):\n if self.meta_type == 'dict':\n raise AssertionError('Cannot append to object of `dict` base type!')\n if self.meta_type == 'list':\n self._list.append(item)\n return",
440 "docstring": "Append to object, if object is list."
441 },
442 {
443 "code": "def CheckBracesSpacing(filename, clean_lines, linenum, nesting_state, error):\n line = clean_lines.elided[linenum]\n match = Match(r'^(.*[^ ({>]){', line)\n if match:\n leading_text = match.group(1)\n (endline, endlinenum, endpos) = CloseExpression(\n clean_lines, linenum, len(match.group(1)))\n trailing_text = ''\n if endpos > -1:\n trailing_text = endline[endpos:]\n for offset in xrange(endlinenum + 1,\n min(endlinenum + 3, clean_lines.NumLines() - 1)):\n trailing_text += clean_lines.elided[offset]\n if (not Match(r'^[\\s}]*[{.;,)<>\\]:]', trailing_text)\n and not _IsType(clean_lines, nesting_state, leading_text)):\n error(filename, linenum, 'whitespace/braces', 5,\n 'Missing space before {')\n if Search(r'}else', line):\n error(filename, linenum, 'whitespace/braces', 5,\n 'Missing space before else')\n if Search(r':\\s*;\\s*$', line):\n error(filename, linenum, 'whitespace/semicolon', 5,\n 'Semicolon defining empty statement. Use {} instead.')\n elif Search(r'^\\s*;\\s*$', line):\n error(filename, linenum, 'whitespace/semicolon', 5,\n 'Line contains only semicolon. If this should be an empty statement, '\n 'use {} instead.')\n elif (Search(r'\\s+;\\s*$', line) and\n not Search(r'\\bfor\\b', line)):\n error(filename, linenum, 'whitespace/semicolon', 5,\n 'Extra space before last semicolon. If this should be an empty '\n 'statement, use {} instead.')",
444 "docstring": "Checks for horizontal spacing near commas.\n\n Args:\n filename: The name of the current file.\n clean_lines: A CleansedLines instance containing the file.\n linenum: The number of the line to check.\n nesting_state: A NestingState instance which maintains information about\n the current stack of nested blocks being parsed.\n error: The function to call with any errors found."
445 },
446 {
447 "code": "def as_unicode(self):\n result = self.domain\n if self.local:\n result = self.local + u'@' + result\n if self.resource:\n result = result + u'/' + self.resource\n if not JID.cache.has_key(result):\n JID.cache[result] = self\n return result",
448 "docstring": "Unicode string JID representation.\n\n :return: JID as Unicode string."
449 },
450 {
451 "code": "def _insert_metadata(cursor, model, publisher, message):\n params = model.metadata.copy()\n params['publisher'] = publisher\n params['publication_message'] = message\n params['_portal_type'] = _model_to_portaltype(model)\n params['summary'] = str(cnxepub.DocumentSummaryFormatter(model))\n for person_field in ATTRIBUTED_ROLE_KEYS:\n params[person_field] = [parse_user_uri(x['id'])\n for x in params.get(person_field, [])]\n params['parent_ident_hash'] = parse_parent_ident_hash(model)\n if model.ident_hash is not None:\n uuid, version = split_ident_hash(model.ident_hash,\n split_version=True)\n params['_uuid'] = uuid\n params['_major_version'], params['_minor_version'] = version\n cursor.execute(\"SELECT moduleid FROM latest_modules WHERE uuid = %s\",\n (uuid,))\n try:\n moduleid = cursor.fetchone()[0]\n except TypeError:\n moduleid = None\n params['_moduleid'] = moduleid\n cursor.execute(\"SELECT * from document_controls where uuid = %s\",\n (uuid,))\n try:\n cursor.fetchone()[0]\n except TypeError:\n cursor.execute(\"INSERT INTO document_controls (uuid) VALUES (%s)\",\n (uuid,))\n created = model.metadata.get('created', None)\n stmt = MODULE_INSERTION_TEMPLATE.format(**{\n '__uuid__': \"%(_uuid)s::uuid\",\n '__major_version__': \"%(_major_version)s\",\n '__minor_version__': \"%(_minor_version)s\",\n '__moduleid__': moduleid is None and \"DEFAULT\" or \"%(_moduleid)s\",\n '__created__': created is None and \"DEFAULT\" or \"%(created)s\",\n })\n else:\n created = model.metadata.get('created', None)\n stmt = MODULE_INSERTION_TEMPLATE.format(**{\n '__uuid__': \"DEFAULT\",\n '__major_version__': \"DEFAULT\",\n '__minor_version__': \"DEFAULT\",\n '__moduleid__': \"DEFAULT\",\n '__created__': created is None and \"DEFAULT\" or \"%(created)s\",\n })\n cursor.execute(stmt, params)\n module_ident, ident_hash = cursor.fetchone()\n _insert_optional_roles(cursor, model, module_ident)\n return module_ident, ident_hash",
452 "docstring": "Insert a module with the given ``metadata``."
453 },
454 {
455 "code": "def _validate_search_query(self, returning_query):\n start_index = returning_query.from_index or 0\n size = returning_query.size or 0\n if start_index < 0:\n raise CitrinationClientError(\n \"start_index cannot be negative. Please enter a value greater than or equal to zero\")\n if size < 0:\n raise CitrinationClientError(\"Size cannot be negative. Please enter a value greater than or equal to zero\")\n if start_index + size > MAX_QUERY_DEPTH:\n raise CitrinationClientError(\n \"Citrination does not support pagination past the {0}th result. Please reduce either the from_index and/or size such that their sum is below {0}\".format(\n MAX_QUERY_DEPTH))",
456 "docstring": "Checks to see that the query will not exceed the max query depth\n\n :param returning_query: The PIF system or Dataset query to execute.\n :type returning_query: :class:`PifSystemReturningQuery` or :class: `DatasetReturningQuery`"
457 },
458 {
459 "code": "def ControlFromPoint2(x: int, y: int) -> Control:\n return Control.CreateControlFromElement(_AutomationClient.instance().IUIAutomation.ElementFromHandle(WindowFromPoint(x, y)))",
460 "docstring": "Get a native handle from point x,y and call IUIAutomation.ElementFromHandle.\n Return `Control` subclass."
461 },
462 {
463 "code": "def fmt(lbaf=3):\n if env():\n cij.err(\"cij.nvme.exists: Invalid NVMe ENV.\")\n return 1\n nvme = cij.env_to_dict(PREFIX, EXPORTED + REQUIRED)\n cmd = [\"nvme\", \"format\", nvme[\"DEV_PATH\"], \"-l\", str(lbaf)]\n rcode, _, _ = cij.ssh.command(cmd, shell=True)\n return rcode",
464 "docstring": "Do format for NVMe device"
465 },
466 {
467 "code": "def toBigInt(byteArray):\n array = byteArray[::-1]\n out = 0\n for key, value in enumerate(array):\n decoded = struct.unpack(\"B\", bytes([value]))[0]\n out = out | decoded << key * 8\n return out",
468 "docstring": "Convert the byte array to a BigInteger"
469 },
470 {
471 "code": "def serialize_groups(self, groups):\n rules = []\n for group in groups:\n rules.extend(self.serialize_rules(group.rules))\n return rules",
472 "docstring": "Creates a payload for the redis server\n\n The rule schema is the following:\n\n REDIS KEY - port_device_id.port_mac_address/sg\n REDIS VALUE - A JSON dump of the following:\n\n port_mac_address must be lower-cased and stripped of non-alphanumeric\n characters\n\n {\"id\": \"<arbitrary uuid>\",\n \"rules\": [\n {\"ethertype\": <hexademical integer>,\n \"protocol\": <integer>,\n \"port start\": <integer>, # optional\n \"port end\": <integer>, # optional\n \"icmp type\": <integer>, # optional\n \"icmp code\": <integer>, # optional\n \"source network\": <string>,\n \"destination network\": <string>,\n \"action\": <string>,\n \"direction\": <string>},\n ],\n \"security groups ack\": <boolean>\n }\n\n Example:\n {\"id\": \"004c6369-9f3d-4d33-b8f5-9416bf3567dd\",\n \"rules\": [\n {\"ethertype\": 0x800,\n \"protocol\": \"tcp\",\n \"port start\": 1000,\n \"port end\": 1999,\n \"source network\": \"10.10.10.0/24\",\n \"destination network\": \"\",\n \"action\": \"allow\",\n \"direction\": \"ingress\"},\n ],\n \"security groups ack\": \"true\"\n }\n\n port start/end and icmp type/code are mutually exclusive pairs."
473 },
474 {
475 "code": "def custom_showtraceback(\n self,\n exc_tuple=None,\n filename=None,\n tb_offset=None,\n exception_only=False,\n running_compiled_code=False,\n):\n self.default_showtraceback(\n exc_tuple,\n filename,\n tb_offset,\n exception_only=True,\n running_compiled_code=running_compiled_code,\n )",
476 "docstring": "Custom showtraceback for monkey-patching IPython's InteractiveShell\n\n https://stackoverflow.com/questions/1261668/cannot-override-sys-excepthook"
477 },
478 {
479 "code": "def GetTopLevelControl(self) -> 'Control':\n handle = self.NativeWindowHandle\n if handle:\n topHandle = GetAncestor(handle, GAFlag.Root)\n if topHandle:\n if topHandle == handle:\n return self\n else:\n return ControlFromHandle(topHandle)\n else:\n pass\n else:\n control = self\n while True:\n control = control.GetParentControl()\n handle = control.NativeWindowHandle\n if handle:\n topHandle = GetAncestor(handle, GAFlag.Root)\n return ControlFromHandle(topHandle)",
480 "docstring": "Get the top level control which current control lays.\n If current control is top level, return self.\n If current control is root control, return None.\n Return `PaneControl` or `WindowControl` or None."
481 },
482 {
483 "code": "def handle_package_has_file_helper(self, pkg_file):\n nodes = list(self.graph.triples((None, self.spdx_namespace.fileName, Literal(pkg_file.name))))\n if len(nodes) == 1:\n return nodes[0][0]\n else:\n raise InvalidDocumentError('handle_package_has_file_helper could not' +\n ' find file node for file: {0}'.format(pkg_file.name))",
484 "docstring": "Return node representing pkg_file\n pkg_file should be instance of spdx.file."
485 },
486 {
487 "code": "def get_wildcard(self):\n return _convert(self._ip, notation=NM_WILDCARD,\n inotation=IP_DOT, _check=False, _isnm=self._isnm)",
488 "docstring": "Return the wildcard bits notation of the netmask."
489 },
490 {
491 "code": "def upload_backend(index='dev', user=None):\n get_vars()\n use_devpi(index=index)\n with fab.lcd('../application'):\n fab.local('make upload')",
492 "docstring": "Build the backend and upload it to the remote server at the given index"
493 },
494 {
495 "code": "def refresh(self, token, timeout):\n assert token in self._dict, \"Lock must exist\"\n assert timeout == -1 or timeout > 0\n if timeout < 0 or timeout > LockStorageDict.LOCK_TIME_OUT_MAX:\n timeout = LockStorageDict.LOCK_TIME_OUT_MAX\n self._lock.acquire_write()\n try:\n lock = self._dict[token]\n lock[\"timeout\"] = timeout\n lock[\"expire\"] = time.time() + timeout\n self._dict[token] = lock\n self._flush()\n finally:\n self._lock.release()\n return lock",
496 "docstring": "Modify an existing lock's timeout.\n\n token:\n Valid lock token.\n timeout:\n Suggested lifetime in seconds (-1 for infinite).\n The real expiration time may be shorter than requested!\n Returns:\n Lock dictionary.\n Raises ValueError, if token is invalid."
497 },
498 {
499 "code": "def analyze(fname=False,save=True,show=None):\n if fname and os.path.exists(fname.replace(\".abf\",\".rst\")):\n print(\"SKIPPING DUE TO RST FILE\")\n return\n swhlab.plotting.core.IMAGE_SAVE=save\n if show is None:\n if cm.isIpython():\n swhlab.plotting.core.IMAGE_SHOW=True\n else:\n swhlab.plotting.core.IMAGE_SHOW=False\n abf=ABF(fname)\n print(\">>>>> PROTOCOL >>>>>\",abf.protocomment)\n runFunction=\"proto_unknown\"\n if \"proto_\"+abf.protocomment in globals():\n runFunction=\"proto_\"+abf.protocomment\n abf.log.debug(\"running %s()\"%(runFunction))\n plt.close('all')\n globals()[runFunction](abf)\n try:\n globals()[runFunction](abf)\n except:\n abf.log.error(\"EXCEPTION DURING PROTOCOL FUNCTION\")\n abf.log.error(sys.exc_info()[0])\n return \"ERROR\"\n plt.close('all')\n return \"SUCCESS\"",
500 "docstring": "given a filename or ABF object, try to analyze it."
501 },
502 {
503 "code": "def from_entity(entity, self_user_id):\n user_id = UserID(chat_id=entity.id.chat_id,\n gaia_id=entity.id.gaia_id)\n return User(user_id, entity.properties.display_name,\n entity.properties.first_name,\n entity.properties.photo_url,\n entity.properties.email,\n (self_user_id == user_id) or (self_user_id is None))",
504 "docstring": "Construct user from ``Entity`` message.\n\n Args:\n entity: ``Entity`` message.\n self_user_id (~hangups.user.UserID or None): The ID of the current\n user. If ``None``, assume ``entity`` is the current user.\n\n Returns:\n :class:`~hangups.user.User` object."
505 },
506 {
507 "code": "def write_sbml_model(cobra_model, filename, f_replace=F_REPLACE, **kwargs):\n doc = _model_to_sbml(cobra_model, f_replace=f_replace, **kwargs)\n if isinstance(filename, string_types):\n libsbml.writeSBMLToFile(doc, filename)\n elif hasattr(filename, \"write\"):\n sbml_str = libsbml.writeSBMLToString(doc)\n filename.write(sbml_str)",
508 "docstring": "Writes cobra model to filename.\n\n The created model is SBML level 3 version 1 (L1V3) with\n fbc package v2 (fbc-v2).\n\n If the given filename ends with the suffix \".gz\" (for example,\n \"myfile.xml.gz\"), libSBML assumes the caller wants the file to be\n written compressed in gzip format. Similarly, if the given filename\n ends with \".zip\" or \".bz2\", libSBML assumes the caller wants the\n file to be compressed in zip or bzip2 format (respectively). Files\n whose names lack these suffixes will be written uncompressed. Special\n considerations for the zip format: If the given filename ends with\n \".zip\", the file placed in the zip archive will have the suffix\n \".xml\" or \".sbml\". For example, the file in the zip archive will\n be named \"test.xml\" if the given filename is \"test.xml.zip\" or\n \"test.zip\". Similarly, the filename in the archive will be\n \"test.sbml\" if the given filename is \"test.sbml.zip\".\n\n Parameters\n ----------\n cobra_model : cobra.core.Model\n Model instance which is written to SBML\n filename : string\n path to which the model is written\n use_fbc_package : boolean {True, False}\n should the fbc package be used\n f_replace: dict of replacement functions for id replacement"
509 },
510 {
511 "code": "def get_filtered_normalized_events(self):\n user_image = google_v2_operations.get_action_image(self._op,\n _ACTION_USER_COMMAND)\n need_ok = google_v2_operations.is_success(self._op)\n events = {}\n for event in google_v2_operations.get_events(self._op):\n if self._filter(event):\n continue\n mapped, match = self._map(event)\n name = mapped['name']\n if name == 'ok':\n if not need_ok or 'ok' in events:\n continue\n if name == 'pulling-image':\n if match.group(1) != user_image:\n continue\n events[name] = mapped\n return sorted(events.values(), key=operator.itemgetter('start-time'))",
512 "docstring": "Filter the granular v2 events down to events of interest.\n\n Filter through the large number of granular events returned by the\n pipelines API, and extract only those that are interesting to a user. This\n is implemented by filtering out events which are known to be uninteresting\n (i.e. the default actions run for every job) and by explicitly matching\n specific events which are interesting and mapping those to v1 style naming.\n\n Events which are not whitelisted or blacklisted will still be output,\n meaning any events which are added in the future won't be masked.\n We don't want to suppress display of events that we don't recognize.\n They may be important.\n\n Returns:\n A list of maps containing the normalized, filtered events."
513 },
514 {
515 "code": "def platform_config_dir():\n if LINUX:\n dpath_ = os.environ.get('XDG_CONFIG_HOME', '~/.config')\n elif DARWIN:\n dpath_ = '~/Library/Application Support'\n elif WIN32:\n dpath_ = os.environ.get('APPDATA', '~/AppData/Roaming')\n else:\n raise NotImplementedError('Unknown Platform %r' % (sys.platform,))\n dpath = normpath(expanduser(dpath_))\n return dpath",
516 "docstring": "Returns a directory which should be writable for any application\n This should be used for persistent configuration files.\n\n Returns:\n PathLike : path to the cahce dir used by the current operating system"
517 },
518 {
519 "code": "def _GetNativeEolStyle(platform=sys.platform):\n _NATIVE_EOL_STYLE_MAP = {\n 'win32' : EOL_STYLE_WINDOWS,\n 'linux2' : EOL_STYLE_UNIX,\n 'linux' : EOL_STYLE_UNIX,\n 'darwin' : EOL_STYLE_MAC,\n }\n result = _NATIVE_EOL_STYLE_MAP.get(platform)\n if result is None:\n from ._exceptions import UnknownPlatformError\n raise UnknownPlatformError(platform)\n return result",
520 "docstring": "Internal function that determines EOL_STYLE_NATIVE constant with the proper value for the\n current platform."
521 },
522 {
523 "code": "def get_socket(self, sessid=''):\n socket = self.sockets.get(sessid)\n if sessid and not socket:\n return None\n if socket is None:\n socket = Socket(self, self.config)\n self.sockets[socket.sessid] = socket\n else:\n socket.incr_hits()\n return socket",
524 "docstring": "Return an existing or new client Socket."
525 },
526 {
527 "code": "def item(proto_dataset_uri, input_file, relpath_in_dataset):\n proto_dataset = dtoolcore.ProtoDataSet.from_uri(\n proto_dataset_uri,\n config_path=CONFIG_PATH)\n if relpath_in_dataset == \"\":\n relpath_in_dataset = os.path.basename(input_file)\n proto_dataset.put_item(input_file, relpath_in_dataset)",
528 "docstring": "Add a file to the proto dataset."
529 },
530 {
531 "code": "def _in_gce_environment():\n if SETTINGS.env_name is not None:\n return SETTINGS.env_name == 'GCE_PRODUCTION'\n if NO_GCE_CHECK != 'True' and _detect_gce_environment():\n SETTINGS.env_name = 'GCE_PRODUCTION'\n return True\n return False",
532 "docstring": "Detect if the code is running in the Compute Engine environment.\n\n Returns:\n True if running in the GCE environment, False otherwise."
533 },
534 {
535 "code": "def index(self, i, length=None):\n if self.begin <= i <= self.end:\n index = i - self.BEGIN - self.offset\n if length is None:\n length = self.full_range()\n else:\n length = min(length, self.full_range())\n if 0 <= index < length:\n return index",
536 "docstring": "Return an integer index or None"
537 },
538 {
539 "code": "def clippedObj(obj, maxElementSize=64):\n if hasattr(obj, '_asdict'):\n obj = obj._asdict()\n if isinstance(obj, dict):\n objOut = dict()\n for key,val in obj.iteritems():\n objOut[key] = clippedObj(val)\n elif hasattr(obj, '__iter__'):\n objOut = []\n for val in obj:\n objOut.append(clippedObj(val))\n else:\n objOut = str(obj)\n if len(objOut) > maxElementSize:\n objOut = objOut[0:maxElementSize] + '...'\n return objOut",
540 "docstring": "Return a clipped version of obj suitable for printing, This\n is useful when generating log messages by printing data structures, but\n don't want the message to be too long.\n\n If passed in a dict, list, or namedtuple, each element of the structure's\n string representation will be limited to 'maxElementSize' characters. This\n will return a new object where the string representation of each element\n has been truncated to fit within maxElementSize."
541 },
542 {
543 "code": "def sorted(list, cmp=None, reversed=False):\n list = [x for x in list]\n list.sort(cmp)\n if reversed: list.reverse()\n return list",
544 "docstring": "Returns a sorted copy of the list."
545 },
546 {
547 "code": "def reposition(self, frame_no):\n for label, j in self.channels.items():\n body = self.bodies[label]\n body.position = self.positions[frame_no, j]\n body.linear_velocity = self.velocities[frame_no, j]",
548 "docstring": "Reposition markers to a specific frame of data.\n\n Parameters\n ----------\n frame_no : int\n The frame of data where we should reposition marker bodies. Markers\n will be positioned in the appropriate places in world coordinates.\n In addition, linear velocities of the markers will be set according\n to the data as long as there are no dropouts in neighboring frames."
549 },
550 {
551 "code": "def draw_on_image(self, image, color=(0, 255, 0), alpha=1.0, size=1,\n copy=True, raise_if_out_of_image=False, thickness=None):\n image = np.copy(image) if copy else image\n for bb in self.bounding_boxes:\n image = bb.draw_on_image(\n image,\n color=color,\n alpha=alpha,\n size=size,\n copy=False,\n raise_if_out_of_image=raise_if_out_of_image,\n thickness=thickness\n )\n return image",
552 "docstring": "Draw all bounding boxes onto a given image.\n\n Parameters\n ----------\n image : (H,W,3) ndarray\n The image onto which to draw the bounding boxes.\n This image should usually have the same shape as\n set in BoundingBoxesOnImage.shape.\n\n color : int or list of int or tuple of int or (3,) ndarray, optional\n The RGB color of all bounding boxes. If a single int ``C``, then\n that is equivalent to ``(C,C,C)``.\n\n alpha : float, optional\n Alpha/transparency of the bounding box.\n\n size : int, optional\n Thickness in pixels.\n\n copy : bool, optional\n Whether to copy the image before drawing the bounding boxes.\n\n raise_if_out_of_image : bool, optional\n Whether to raise an exception if any bounding box is outside of the\n image.\n\n thickness : None or int, optional\n Deprecated.\n\n Returns\n -------\n image : (H,W,3) ndarray\n Image with drawn bounding boxes."
553 },
554 {
555 "code": "def list(self,\n key_name=None,\n max_suggestions=100,\n cutoff=0.5,\n locked_only=False,\n key_type=None):\n self._assert_valid_stash()\n key_list = [k for k in self._storage.list()\n if k['name'] != 'stored_passphrase' and\n (k.get('lock') if locked_only else True)]\n if key_type:\n types = ('secret', None) if key_type == 'secret' else [key_type]\n key_list = [k for k in key_list if k.get('type') in types]\n key_list = [k['name'] for k in key_list]\n if key_name:\n if key_name.startswith('~'):\n key_list = difflib.get_close_matches(\n key_name.lstrip('~'), key_list, max_suggestions, cutoff)\n else:\n key_list = [k for k in key_list if key_name in k]\n audit(\n storage=self._storage.db_path,\n action='LIST' + ('[LOCKED]' if locked_only else ''),\n message=json.dumps(dict()))\n return key_list",
556 "docstring": "Return a list of all keys."
557 },
558 {
559 "code": "def refresh_maps(self):\n for robot in self.robots:\n resp2 = (\n requests.get(urljoin(self.ENDPOINT, 'users/me/robots/{}/maps'.format(robot.serial)),\n headers=self._headers))\n resp2.raise_for_status()\n self._maps.update({robot.serial: resp2.json()})",
560 "docstring": "Get information about maps of the robots.\n\n :return:"
561 },
562 {
563 "code": "def lint_file(in_file, out_file=None):\n for line in in_file:\n print(line.strip(), file=out_file)",
564 "docstring": "Helps remove extraneous whitespace from the lines of a file\n\n :param file in_file: A readable file or file-like\n :param file out_file: A writable file or file-like"
565 },
566 {
567 "code": "def codestr2rst(codestr, lang='python'):\n code_directive = \"\\n.. code-block:: {0}\\n\\n\".format(lang)\n indented_block = indent(codestr, ' ' * 4)\n return code_directive + indented_block",
568 "docstring": "Return reStructuredText code block from code string"
569 },
570 {
571 "code": "def send(self, command, timeout=5):\n logger.info(u'Sending %s' % command)\n _, writable, __ = select.select([], [self.sock], [], timeout)\n if not writable:\n raise SendTimeoutError()\n writable[0].sendall(command + '\\n')",
572 "docstring": "Send a command to the server\n\n :param string command: command to send"
573 },
574 {
575 "code": "async def get_entity_by_id(self, get_entity_by_id_request):\n response = hangouts_pb2.GetEntityByIdResponse()\n await self._pb_request('contacts/getentitybyid',\n get_entity_by_id_request, response)\n return response",
576 "docstring": "Return one or more user entities.\n\n Searching by phone number only finds entities when their phone number\n is in your contacts (and not always even then), and can't be used to\n find Google Voice contacts."
577 },
578 {
579 "code": "def next(self, data):\n self.__length += 1\n result = self.__kdf.calculate(self.__key, data, 64)\n self.__key = result[:32]\n return result[32:]",
580 "docstring": "Derive a new set of internal and output data from given input data and the data\n stored internally.\n\n Use the key derivation function to derive new data. The kdf gets supplied with the\n current key and the data passed to this method.\n\n :param data: A bytes-like object encoding the data to pass to the key derivation\n function.\n :returns: A bytes-like object encoding the output material."
581 },
582 {
583 "code": "def auth_finish(self, _unused):\n self.lock.acquire()\n try:\n self.__logger.debug(\"Authenticated\")\n self.authenticated=True\n self.state_change(\"authorized\",self.my_jid)\n self._post_auth()\n finally:\n self.lock.release()",
584 "docstring": "Handle success of the legacy authentication."
585 },
586 {
587 "code": "def convert_gemm(params, w_name, scope_name, inputs, layers, weights, names):\n print('Converting Linear ...')\n if names == 'short':\n tf_name = 'FC' + random_string(6)\n elif names == 'keep':\n tf_name = w_name\n else:\n tf_name = w_name + str(random.random())\n bias_name = '{0}.bias'.format(w_name)\n weights_name = '{0}.weight'.format(w_name)\n W = weights[weights_name].numpy().transpose()\n input_channels, output_channels = W.shape\n keras_weights = [W]\n has_bias = False\n if bias_name in weights:\n bias = weights[bias_name].numpy()\n keras_weights = [W, bias]\n has_bias = True\n dense = keras.layers.Dense(\n output_channels,\n weights=keras_weights, use_bias=has_bias, name=tf_name, bias_initializer='zeros', kernel_initializer='zeros',\n )\n layers[scope_name] = dense(layers[inputs[0]])",
588 "docstring": "Convert Linear.\n\n Args:\n params: dictionary with layer parameters\n w_name: name prefix in state_dict\n scope_name: pytorch scope name\n inputs: pytorch node inputs\n layers: dictionary with keras tensors\n weights: pytorch state_dict\n names: use short names for keras layers"
589 },
590 {
591 "code": "def find_duplicates(items, k=2, key=None):\n duplicates = defaultdict(list)\n if key is None:\n for count, item in enumerate(items):\n duplicates[item].append(count)\n else:\n for count, item in enumerate(items):\n duplicates[key(item)].append(count)\n for key in list(duplicates.keys()):\n if len(duplicates[key]) < k:\n del duplicates[key]\n duplicates = dict(duplicates)\n return duplicates",
592 "docstring": "Find all duplicate items in a list.\n\n Search for all items that appear more than `k` times and return a mapping\n from each (k)-duplicate item to the positions it appeared in.\n\n Args:\n items (Iterable): hashable items possibly containing duplicates\n k (int): only return items that appear at least `k` times (default=2)\n key (Callable, optional): Returns indices where `key(items[i])`\n maps to a particular value at least k times.\n\n Returns:\n dict: maps each duplicate item to the indices at which it appears\n\n CommandLine:\n python -m ubelt.util_dict find_duplicates\n\n Example:\n >>> import ubelt as ub\n >>> items = [0, 0, 1, 2, 3, 3, 0, 12, 2, 9]\n >>> duplicates = ub.find_duplicates(items)\n >>> print('items = %r' % (items,))\n >>> print('duplicates = %r' % (duplicates,))\n >>> assert duplicates == {0: [0, 1, 6], 2: [3, 8], 3: [4, 5]}\n >>> assert ub.find_duplicates(items, 3) == {0: [0, 1, 6]}\n\n Example:\n >>> import ubelt as ub\n >>> items = [0, 0, 1, 2, 3, 3, 0, 12, 2, 9]\n >>> # note: k can be 0\n >>> duplicates = ub.find_duplicates(items, k=0)\n >>> print(ub.repr2(duplicates, nl=0))\n {0: [0, 1, 6], 1: [2], 2: [3, 8], 3: [4, 5], 9: [9], 12: [7]}\n\n Example:\n >>> import ubelt as ub\n >>> items = [10, 11, 12, 13, 14, 15, 16]\n >>> duplicates = ub.find_duplicates(items, key=lambda x: x // 2)\n >>> print(ub.repr2(duplicates, nl=0))\n {5: [0, 1], 6: [2, 3], 7: [4, 5]}"
593 },
594 {
595 "code": "def parse_line(line, document=None):\n result = re.match(line_pattern, line)\n if result:\n _, lineno, offset, severity, msg = result.groups()\n lineno = int(lineno or 1)\n offset = int(offset or 0)\n errno = 2\n if severity == 'error':\n errno = 1\n diag = {\n 'source': 'mypy',\n 'range': {\n 'start': {'line': lineno - 1, 'character': offset},\n 'end': {'line': lineno - 1, 'character': offset + 1}\n },\n 'message': msg,\n 'severity': errno\n }\n if document:\n word = document.word_at_position(diag['range']['start'])\n if word:\n diag['range']['end']['character'] = (\n diag['range']['start']['character'] + len(word))\n return diag",
596 "docstring": "Return a language-server diagnostic from a line of the Mypy error report;\n optionally, use the whole document to provide more context on it."
597 },
598 {
599 "code": "def detail(self, *args, **kwargs):\n prefix = kwargs.pop(\"prefix\", default_prefix)\n kwargs[\"votes\"] = list(set(kwargs[\"votes\"]))\n return OrderedDict(\n [\n (\"memo_key\", PublicKey(kwargs[\"memo_key\"], prefix=prefix)),\n (\"voting_account\", ObjectId(kwargs[\"voting_account\"], \"account\")),\n (\"num_witness\", Uint16(kwargs[\"num_witness\"])),\n (\"num_committee\", Uint16(kwargs[\"num_committee\"])),\n (\"votes\", Array([VoteId(o) for o in kwargs[\"votes\"]])),\n (\"extensions\", Set([])),\n ]\n )",
600 "docstring": "This is an example how to sort votes prior to using them in the\n Object"
601 },
602 {
603 "code": "def update_collaboration(self):\n for field in record_get_field_instances(self.record, '710'):\n subs = field_get_subfield_instances(field)\n for idx, (key, value) in enumerate(subs[:]):\n if key == '5':\n subs.pop(idx)\n elif value.startswith('CERN. Geneva'):\n subs.pop(idx)\n if len(subs) == 0:\n record_delete_field(self.record,\n tag='710',\n field_position_global=field[4])",
604 "docstring": "710 Collaboration."
605 },
606 {
607 "code": "def _get_result_msg_and_payload(result, stream):\n groups = _GDB_MI_RESULT_RE.match(result).groups()\n token = int(groups[0]) if groups[0] != \"\" else None\n message = groups[1]\n if groups[2] is None:\n payload = None\n else:\n stream.advance_past_chars([\",\"])\n payload = _parse_dict(stream)\n return token, message, payload",
608 "docstring": "Get result message and payload dict"
609 },
610 {
611 "code": "def login(email=None, password=None, api_key=None, application='Default',\n url=None, verify_ssl_certificate=True):\n try:\n input_ = raw_input\n except NameError:\n input_ = input\n if url is None:\n url = input_('Server URL: ')\n url = url.rstrip('/')\n if session.communicator is None:\n session.communicator = Communicator(url)\n else:\n session.communicator.url = url\n session.communicator.verify_ssl_certificate = verify_ssl_certificate\n if email is None:\n email = input_('Email: ')\n session.email = email\n if api_key is None:\n if password is None:\n password = getpass.getpass()\n session.api_key = session.communicator.get_default_api_key(\n session.email, password)\n session.application = 'Default'\n else:\n session.api_key = api_key\n session.application = application\n return renew_token()",
612 "docstring": "Do the legwork of logging into the Midas Server instance, storing the API\n key and token.\n\n :param email: (optional) Email address to login with. If not set, the\n console will be prompted.\n :type email: None | string\n :param password: (optional) User password to login with. If not set and no\n 'api_key' is set, the console will be prompted.\n :type password: None | string\n :param api_key: (optional) API key to login with. If not set, password\n login with be used.\n :type api_key: None | string\n :param application: (optional) Application name to be used with 'api_key'.\n :type application: string\n :param url: (optional) URL address of the Midas Server instance to login\n to. If not set, the console will be prompted.\n :type url: None | string\n :param verify_ssl_certificate: (optional) If True, the SSL certificate will\n be verified\n :type verify_ssl_certificate: bool\n :returns: API token.\n :rtype: string"
613 },
614 {
615 "code": "def delete(self, wait=True):\n resp = self.parent.delete(self.id)\n if wait:\n self.wait()\n return resp",
616 "docstring": "Delete this droplet\n\n Parameters\n ----------\n wait: bool, default True\n Whether to block until the pending action is completed"
617 },
618 {
619 "code": "def create(self, list_id, data):\n return self._mc_client._post(url=self._build_path(list_id, 'segments'), data=data)",
620 "docstring": "adds a new segment to the list."
621 },
622 {
623 "code": "def upgrade(safe=True):\n manager = MANAGER\n if safe:\n cmd = 'upgrade'\n else:\n cmd = 'dist-upgrade'\n run_as_root(\"%(manager)s --assume-yes %(cmd)s\" % locals(), pty=False)",
624 "docstring": "Upgrade all packages."
625 },
626 {
627 "code": "def http_exception_error_handler(\n exception):\n assert issubclass(type(exception), HTTPException), type(exception)\n assert hasattr(exception, \"code\")\n assert hasattr(exception, \"description\")\n return response(exception.code, exception.description)",
628 "docstring": "Handle HTTP exception\n\n :param werkzeug.exceptions.HTTPException exception: Raised exception\n\n A response is returned, as formatted by the :py:func:`response` function."
629 },
630 {
631 "code": "def id_to_word(self, word_id):\n if word_id >= len(self.reverse_vocab):\n return self.reverse_vocab[self.unk_id]\n else:\n return self.reverse_vocab[word_id]",
632 "docstring": "Returns the word string of an integer word id."
633 },
634 {
635 "code": "def new_knitting_pattern_set_loader(specification=DefaultSpecification()):\n parser = specification.new_parser(specification)\n loader = specification.new_loader(parser.knitting_pattern_set)\n return loader",
636 "docstring": "Create a loader for a knitting pattern set.\n\n :param specification: a :class:`specification\n <knittingpattern.ParsingSpecification.ParsingSpecification>`\n for the knitting pattern set, default\n :class:`DefaultSpecification`"
637 },
638 {
639 "code": "def currency(self):\n try:\n current_subscription = self.info[\"viewer\"][\"home\"][\"currentSubscription\"]\n return current_subscription[\"priceInfo\"][\"current\"][\"currency\"]\n except (KeyError, TypeError, IndexError):\n _LOGGER.error(\"Could not find currency.\")\n return \"\"",
640 "docstring": "Return the currency."
641 },
642 {
643 "code": "def run(self, cmd, *args):\n if self.manager is None:\n raise Exception(\"Fatal internal error: Missing repository manager\")\n if cmd not in dir(self.manager):\n raise Exception(\"Fatal internal error: Invalid command {} being run\".format(cmd))\n func = getattr(self.manager, cmd)\n repo = self\n return func(repo, *args)",
644 "docstring": "Run a specific command using the manager"
645 },
646 {
647 "code": "def copy_file(src, dest):\n dir_path = os.path.dirname(dest)\n if not os.path.exists(dir_path):\n os.makedirs(dir_path)\n shutil.copy2(src, dest)",
648 "docstring": "Copy file helper method\n\n :type src: str|unicode\n :type dest: str|unicode"
649 },
650 {
651 "code": "def _call_api(self, verb, url, **request_kwargs):\n api = 'https://api.github.com{}'.format(url)\n auth_headers = {'Authorization': 'token {}'.format(self.api_token)}\n headers = {**auth_headers, **request_kwargs.pop('headers', {})}\n return getattr(requests, verb)(api, headers=headers, **request_kwargs)",
652 "docstring": "Perform a github API call\n\n Args:\n verb (str): Can be \"post\", \"put\", or \"get\"\n url (str): The base URL with a leading slash for Github API (v3)\n auth (str or HTTPBasicAuth): A Github API token or a HTTPBasicAuth object"
653 },
654 {
655 "code": "def error_message(self, message, fh=None, prefix=\"[error]:\",\n suffix=\"...\"):\n msg = prefix + message + suffix\n fh = fh or sys.stderr\n if fh is sys.stderr:\n termcolor.cprint(msg, color=\"red\")\n else:\n fh.write(msg)\n pass",
656 "docstring": "print error type message\n if file handle is `sys.stderr`, print color message\n\n :param str message: message to print\n :param file fh: file handle, default is `sys.stdout`\n :param str prefix: message prefix,default is `[error]`\n :param str suffix: message suffix ,default is '...'\n :return: None"
657 },
658 {
659 "code": "def gather(obj):\n if hasattr(obj, '__distob_gather__'):\n return obj.__distob_gather__()\n elif (isinstance(obj, collections.Sequence) and \n not isinstance(obj, string_types)):\n return [gather(subobj) for subobj in obj]\n else:\n return obj",
660 "docstring": "Retrieve objects that have been distributed, making them local again"
661 },
662 {
663 "code": "def get_chat_id(self, message):\n if message.chat.type == 'private':\n return message.user.id\n return message.chat.id",
664 "docstring": "Telegram chat type can be either \"private\", \"group\", \"supergroup\" or\n \"channel\".\n Return user ID if it is of type \"private\", chat ID otherwise."
665 },
666 {
667 "code": "def publish(self, registry=None):\n if (registry is None) or (registry == registry_access.Registry_Base_URL):\n if 'private' in self.description and self.description['private']:\n return \"this %s is private and cannot be published\" % (self.description_filename.split('.')[0])\n upload_archive = os.path.join(self.path, 'upload.tar.gz')\n fsutils.rmF(upload_archive)\n fd = os.open(upload_archive, os.O_CREAT | os.O_EXCL | os.O_RDWR | getattr(os, \"O_BINARY\", 0))\n with os.fdopen(fd, 'rb+') as tar_file:\n tar_file.truncate()\n self.generateTarball(tar_file)\n logger.debug('generated tar file of length %s', tar_file.tell())\n tar_file.seek(0)\n shasum = hashlib.sha256()\n while True:\n chunk = tar_file.read(1000)\n if not chunk:\n break\n shasum.update(chunk)\n logger.debug('generated tar file has hash %s', shasum.hexdigest())\n tar_file.seek(0)\n with self.findAndOpenReadme() as readme_file_wrapper:\n if not readme_file_wrapper:\n logger.warning(\"no readme.md file detected\")\n with open(self.getDescriptionFile(), 'r') as description_file:\n return registry_access.publish(\n self.getRegistryNamespace(),\n self.getName(),\n self.getVersion(),\n description_file,\n tar_file,\n readme_file_wrapper.file,\n readme_file_wrapper.extension().lower(),\n registry=registry\n )",
668 "docstring": "Publish to the appropriate registry, return a description of any\n errors that occured, or None if successful.\n No VCS tagging is performed."
669 },
670 {
671 "code": "def get_resources(self, names=None, stores=None, workspaces=None):\n stores = self.get_stores(\n names = stores,\n workspaces = workspaces\n )\n resources = []\n for s in stores:\n try:\n resources.extend(s.get_resources())\n except FailedRequestError:\n continue\n if names is None:\n names = []\n elif isinstance(names, basestring):\n names = [s.strip() for s in names.split(',') if s.strip()]\n if resources and names:\n return ([resource for resource in resources if resource.name in names])\n return resources",
672 "docstring": "Resources include feature stores, coverage stores and WMS stores, however does not include layer groups.\n names, stores and workspaces can be provided as a comma delimited strings or as arrays, and are used for filtering.\n Will always return an array."
673 },
674 {
675 "code": "def plot_3(data, ss, *args):\n if len(data) <= 1:\n warnings.warn(\"Only one datapoint. Could not compute t-SNE embedding.\")\n return None\n scores = np.array([d['mean_test_score'] for d in data])\n warped = np.array([ss.point_to_unit(d['parameters']) for d in data])\n X = TSNE(n_components=2).fit_transform(warped)\n e_scores = np.exp(scores)\n mine, maxe = np.min(e_scores), np.max(e_scores)\n color = (e_scores - mine) / (maxe - mine)\n mapped_colors = list(map(rgb2hex, cm.get_cmap('RdBu_r')(color)))\n p = bk.figure(title='t-SNE (unsupervised)', tools=TOOLS)\n df_params = nonconstant_parameters(data)\n df_params['score'] = scores\n df_params['x'] = X[:, 0]\n df_params['y'] = X[:, 1]\n df_params['color'] = mapped_colors\n df_params['radius'] = 1\n p.circle(\n x='x', y='y', color='color', radius='radius',\n source=ColumnDataSource(data=df_params), fill_alpha=0.6,\n line_color=None)\n cp = p\n hover = cp.select(dict(type=HoverTool))\n format_tt = [(s, '@%s' % s) for s in df_params.columns]\n hover.tooltips = OrderedDict([(\"index\", \"$index\")] + format_tt)\n xax, yax = p.axis\n xax.axis_label = 't-SNE coord 1'\n yax.axis_label = 't-SNE coord 2'\n return p",
676 "docstring": "t-SNE embedding of the parameters, colored by score"
677 },
678 {
679 "code": "def set_concluded_license(self, doc, lic):\n if self.has_package(doc) and self.has_file(doc):\n if not self.file_conc_lics_set:\n self.file_conc_lics_set = True\n if validations.validate_lics_conc(lic):\n self.file(doc).conc_lics = lic\n return True\n else:\n raise SPDXValueError('File::ConcludedLicense')\n else:\n raise CardinalityError('File::ConcludedLicense')\n else:\n raise OrderError('File::ConcludedLicense')",
680 "docstring": "Raises OrderError if no package or file defined.\n Raises CardinalityError if already set.\n Raises SPDXValueError if malformed."
681 },
682 {
683 "code": "def get_argument_starttime(self):\n try:\n starttime = self.get_argument(constants.PARAM_STARTTIME)\n return starttime\n except tornado.web.MissingArgumentError as e:\n raise Exception(e.log_message)",
684 "docstring": "Helper function to get starttime argument.\n Raises exception if argument is missing.\n Returns the starttime argument."
685 },
686 {
687 "code": "def render_to_message(self, extra_context=None, *args, **kwargs):\n message = super(TemplatedHTMLEmailMessageView, self)\\\n .render_to_message(extra_context, *args, **kwargs)\n if extra_context is None:\n extra_context = {}\n context = self.get_context_data(**extra_context)\n content = self.render_html_body(context)\n message.attach_alternative(content, mimetype='text/html')\n return message",
688 "docstring": "Renders and returns an unsent message with the given context.\n\n Any extra keyword arguments passed will be passed through as keyword\n arguments to the message constructor.\n\n :param extra_context: Any additional context to use when rendering\n templated content.\n :type extra_context: :class:`dict`\n :returns: A message instance.\n :rtype: :attr:`.message_class`"
689 },
690 {
691 "code": "def _load_credentials_file(credentials_file):\n try:\n credentials_file.seek(0)\n data = json.load(credentials_file)\n except Exception:\n logger.warning(\n 'Credentials file could not be loaded, will ignore and '\n 'overwrite.')\n return {}\n if data.get('file_version') != 2:\n logger.warning(\n 'Credentials file is not version 2, will ignore and '\n 'overwrite.')\n return {}\n credentials = {}\n for key, encoded_credential in iteritems(data.get('credentials', {})):\n try:\n credential_json = base64.b64decode(encoded_credential)\n credential = client.Credentials.new_from_json(credential_json)\n credentials[key] = credential\n except:\n logger.warning(\n 'Invalid credential {0} in file, ignoring.'.format(key))\n return credentials",
692 "docstring": "Load credentials from the given file handle.\n\n The file is expected to be in this format:\n\n {\n \"file_version\": 2,\n \"credentials\": {\n \"key\": \"base64 encoded json representation of credentials.\"\n }\n }\n\n This function will warn and return empty credentials instead of raising\n exceptions.\n\n Args:\n credentials_file: An open file handle.\n\n Returns:\n A dictionary mapping user-defined keys to an instance of\n :class:`oauth2client.client.Credentials`."
693 },
694 {
695 "code": "def padnames(names):\n longname_len = max(len(i) for i in names)\n padding = 5\n pnames = [name + \" \" * (longname_len - len(name)+ padding) \\\n for name in names]\n snppad = \"//\" + \" \" * (longname_len - 2 + padding)\n return np.array(pnames), snppad",
696 "docstring": "pads names for loci output"
697 },
698 {
699 "code": "def stop(self):\n with self.lock:\n for dummy in self.threads:\n self.queue.put(None)",
700 "docstring": "Stop the resolver threads."
701 },
702 {
703 "code": "def sys_rename(self, oldnamep, newnamep):\n oldname = self.current.read_string(oldnamep)\n newname = self.current.read_string(newnamep)\n ret = 0\n try:\n os.rename(oldname, newname)\n except OSError as e:\n ret = -e.errno\n return ret",
704 "docstring": "Rename filename `oldnamep` to `newnamep`.\n\n :param int oldnamep: pointer to oldname\n :param int newnamep: pointer to newname"
705 },
706 {
707 "code": "def remove_members(self, to_remove):\n if isinstance(to_remove, string_types) or \\\n hasattr(to_remove, \"id\"):\n warn(\"need to pass in a list\")\n to_remove = [to_remove]\n self._members.difference_update(to_remove)",
708 "docstring": "Remove objects from the group.\n\n Parameters\n ----------\n to_remove : list\n A list of cobra objects to remove from the group"
709 },
710 {
711 "code": "def create_input_peptides_files(\n peptides,\n max_peptides_per_file=None,\n group_by_length=False):\n if group_by_length:\n peptide_lengths = {len(p) for p in peptides}\n peptide_groups = {l: [] for l in peptide_lengths}\n for p in peptides:\n peptide_groups[len(p)].append(p)\n else:\n peptide_groups = {\"\": peptides}\n file_names = []\n for key, group in peptide_groups.items():\n n_peptides = len(group)\n if not max_peptides_per_file:\n max_peptides_per_file = n_peptides\n input_file = None\n for i, p in enumerate(group):\n if i % max_peptides_per_file == 0:\n if input_file is not None:\n file_names.append(input_file.name)\n input_file.close()\n input_file = make_writable_tempfile(\n prefix_number=i // max_peptides_per_file,\n prefix_name=key,\n suffix=\".txt\")\n input_file.write(\"%s\\n\" % p)\n if input_file is not None:\n file_names.append(input_file.name)\n input_file.close()\n return file_names",
712 "docstring": "Creates one or more files containing one peptide per line,\n returns names of files."
713 },
714 {
715 "code": "def unlink_f(path):\n try:\n os.unlink(path)\n except OSError as err:\n if err.errno != errno.ENOENT:\n raise",
716 "docstring": "Unlink path but do not complain if file does not exist."
717 },
718 {
719 "code": "def draw_lines_heatmap_array(self, image_shape, alpha=1.0,\n size=1, antialiased=True,\n raise_if_out_of_image=False):\n assert len(image_shape) == 2 or (\n len(image_shape) == 3 and image_shape[-1] == 1), (\n \"Expected (H,W) or (H,W,1) as image_shape, got %s.\" % (\n image_shape,))\n arr = self.draw_lines_on_image(\n np.zeros(image_shape, dtype=np.uint8),\n color=255, alpha=alpha, size=size,\n antialiased=antialiased,\n raise_if_out_of_image=raise_if_out_of_image\n )\n return arr.astype(np.float32) / 255.0",
720 "docstring": "Draw the line segments of the line string as a heatmap array.\n\n Parameters\n ----------\n image_shape : tuple of int\n The shape of the image onto which to draw the line mask.\n\n alpha : float, optional\n Opacity of the line string. Higher values denote a more visible\n line string.\n\n size : int, optional\n Thickness of the line segments.\n\n antialiased : bool, optional\n Whether to draw the line with anti-aliasing activated.\n\n raise_if_out_of_image : bool, optional\n Whether to raise an error if the line string is fully\n outside of the image. If set to False, no error will be raised and\n only the parts inside the image will be drawn.\n\n Returns\n -------\n ndarray\n Float array of shape `image_shape` (no channel axis) with drawn\n line string. All values are in the interval ``[0.0, 1.0]``."
721 },
722 {
723 "code": "def import_pyfile(filepath, mod_name=None):\n import sys\n if sys.version_info.major == 3:\n import importlib.machinery\n loader = importlib.machinery.SourceFileLoader('', filepath)\n mod = loader.load_module(mod_name)\n else:\n import imp\n mod = imp.load_source(mod_name, filepath)\n return mod",
724 "docstring": "Imports the contents of filepath as a Python module.\n\n :param filepath: string\n\n :param mod_name: string\n Name of the module when imported\n\n :return: module\n Imported module"
725 },
726 {
727 "code": "def labels(self):\n return sorted(self.channels, key=lambda c: self.channels[c])",
728 "docstring": "Return the names of our marker labels in canonical order."
729 },
730 {
731 "code": "def get_devpi_url(ctx):\n cmd = 'devpi use --urls'\n lines = ctx.run(cmd, hide='out', echo=False).stdout.splitlines()\n for line in lines:\n try:\n line, base_url = line.split(':', 1)\n except ValueError:\n notify.warning('Ignoring \"{}\"!'.format(line))\n else:\n if line.split()[-1].strip() == 'simpleindex':\n return base_url.split('\\x1b')[0].strip().rstrip('/')\n raise LookupError(\"Cannot find simpleindex URL in '{}' output:\\n {}\".format(\n cmd, '\\n '.join(lines),\n ))",
732 "docstring": "Get currently used 'devpi' base URL."
733 },
734 {
735 "code": "async def get_oauth_verifier(oauth_token):\n url = \"https://api.twitter.com/oauth/authorize?oauth_token=\" + oauth_token\n try:\n browser = webbrowser.open(url)\n await asyncio.sleep(2)\n if not browser:\n raise RuntimeError\n except RuntimeError:\n print(\"could not open a browser\\ngo here to enter your PIN: \" + url)\n verifier = input(\"\\nEnter your PIN: \")\n return verifier",
736 "docstring": "Open authorize page in a browser,\n print the url if it didn't work\n\n Arguments\n ---------\n oauth_token : str\n The oauth token received in :func:`get_oauth_token`\n\n Returns\n -------\n str\n The PIN entered by the user"
737 },
738 {
739 "code": "def layers(self):\n layers = [self._layer_def(style) for style in self.styles]\n return layers",
740 "docstring": "Renders the list of layers to add to the map.\n\n Returns:\n layers (list): list of layer entries suitable for use in mapbox-gl 'map.addLayer()' call"
741 },
742 {
743 "code": "def objHasUnsavedChanges(self):\n\t\tif not self.obj:\n\t\t\treturn False\n\t\treturn self.obj.hasUnsavedChanges(cascadeObjects=True)",
744 "docstring": "objHasUnsavedChanges - Check if any object has unsaved changes, cascading."
745 },
746 {
747 "code": "def parse_list_line_windows(self, b):\n line = b.decode(encoding=self.encoding).rstrip(\"\\r\\n\")\n date_time_end = line.index(\"M\")\n date_time_str = line[:date_time_end + 1].strip().split(\" \")\n date_time_str = \" \".join([x for x in date_time_str if len(x) > 0])\n line = line[date_time_end + 1:].lstrip()\n with setlocale(\"C\"):\n strptime = datetime.datetime.strptime\n date_time = strptime(date_time_str, \"%m/%d/%Y %I:%M %p\")\n info = {}\n info[\"modify\"] = self.format_date_time(date_time)\n next_space = line.index(\" \")\n if line.startswith(\"<DIR>\"):\n info[\"type\"] = \"dir\"\n else:\n info[\"type\"] = \"file\"\n info[\"size\"] = line[:next_space].replace(\",\", \"\")\n if not info[\"size\"].isdigit():\n raise ValueError\n filename = line[next_space:].lstrip()\n if filename == \".\" or filename == \"..\":\n raise ValueError\n return pathlib.PurePosixPath(filename), info",
748 "docstring": "Parsing Microsoft Windows `dir` output\n\n :param b: response line\n :type b: :py:class:`bytes` or :py:class:`str`\n\n :return: (path, info)\n :rtype: (:py:class:`pathlib.PurePosixPath`, :py:class:`dict`)"
749 },
750 {
751 "code": "def get_incorrect_names_by_namespace(graph: BELGraph, namespace: str) -> Set[str]:\n return {\n exc.name\n for _, exc, _ in graph.warnings\n if isinstance(exc, (MissingNamespaceNameWarning, MissingNamespaceRegexWarning)) and exc.namespace == namespace\n }",
752 "docstring": "Return the set of all incorrect names from the given namespace in the graph.\n\n :return: The set of all incorrect names from the given namespace in the graph"
753 },
754 {
755 "code": "def _sample_with_priority(self, p):\n parent = 0\n while True:\n left = 2 * parent + 1\n if left >= len(self._memory):\n return parent\n left_p = self._memory[left] if left < self._capacity - 1 \\\n else (self._memory[left].priority or 0)\n if p <= left_p:\n parent = left\n else:\n if left + 1 >= len(self._memory):\n raise RuntimeError('Right child is expected to exist.')\n p -= left_p\n parent = left + 1",
756 "docstring": "Sample random element with priority greater than p."
757 },
758 {
759 "code": "def get_fsapi_endpoint(self):\n endpoint = yield from self.__session.get(self.fsapi_device_url, timeout = self.timeout)\n text = yield from endpoint.text(encoding='utf-8')\n doc = objectify.fromstring(text)\n return doc.webfsapi.text",
760 "docstring": "Parse the fsapi endpoint from the device url."
761 },
762 {
763 "code": "def fit_transform(self, X, y=None, **params):\n X = as_features(X, stack=True)\n X_new = self.transformer.fit_transform(X.stacked_features, y, **params)\n return self._gather_outputs(X, X_new)",
764 "docstring": "Fit and transform the stacked points.\n\n Parameters\n ----------\n X : :class:`Features` or list of bag feature arrays\n Data to train on and transform.\n\n any other keyword argument :\n Passed on as keyword arguments to the transformer's ``transform()``.\n\n Returns\n -------\n X_new : :class:`Features`\n Transformed features."
765 },
766 {
767 "code": "def to_representation(self, instance):\n request = self.context['request']\n enterprise_customer = instance.enterprise_customer\n representation = super(EnterpriseCustomerCatalogDetailSerializer, self).to_representation(instance)\n paginated_content = instance.get_paginated_content(request.GET)\n count = paginated_content['count']\n search_results = paginated_content['results']\n for item in search_results:\n content_type = item['content_type']\n marketing_url = item.get('marketing_url')\n if marketing_url:\n item['marketing_url'] = utils.update_query_parameters(\n marketing_url, utils.get_enterprise_utm_context(enterprise_customer)\n )\n if content_type == 'course':\n item['enrollment_url'] = instance.get_course_enrollment_url(item['key'])\n if content_type == 'courserun':\n item['enrollment_url'] = instance.get_course_run_enrollment_url(item['key'])\n if content_type == 'program':\n item['enrollment_url'] = instance.get_program_enrollment_url(item['uuid'])\n previous_url = None\n next_url = None\n page = int(request.GET.get('page', '1'))\n request_uri = request.build_absolute_uri()\n if paginated_content['previous']:\n previous_url = utils.update_query_parameters(request_uri, {'page': page - 1})\n if paginated_content['next']:\n next_url = utils.update_query_parameters(request_uri, {'page': page + 1})\n representation['count'] = count\n representation['previous'] = previous_url\n representation['next'] = next_url\n representation['results'] = search_results\n return representation",
768 "docstring": "Serialize the EnterpriseCustomerCatalog object.\n\n Arguments:\n instance (EnterpriseCustomerCatalog): The EnterpriseCustomerCatalog to serialize.\n\n Returns:\n dict: The EnterpriseCustomerCatalog converted to a dict."
769 },
770 {
771 "code": "def encode(self, word, max_length=8):\n word = ''.join(\n char for char in word.lower() if char in self._initial_phones\n )\n if not word:\n word = 'รท'\n values = [self._initial_phones[word[0]]]\n values += [self._trailing_phones[char] for char in word[1:]]\n shifted_values = [_ >> 1 for _ in values]\n condensed_values = [values[0]]\n for n in range(1, len(shifted_values)):\n if shifted_values[n] != shifted_values[n - 1]:\n condensed_values.append(values[n])\n values = (\n [condensed_values[0]]\n + [0] * max(0, max_length - len(condensed_values))\n + condensed_values[1:max_length]\n )\n hash_value = 0\n for val in values:\n hash_value = (hash_value << 8) | val\n return hash_value",
772 "docstring": "Return the eudex phonetic hash of a word.\n\n Parameters\n ----------\n word : str\n The word to transform\n max_length : int\n The length in bits of the code returned (default 8)\n\n Returns\n -------\n int\n The eudex hash\n\n Examples\n --------\n >>> pe = Eudex()\n >>> pe.encode('Colin')\n 432345564238053650\n >>> pe.encode('Christopher')\n 433648490138894409\n >>> pe.encode('Niall')\n 648518346341351840\n >>> pe.encode('Smith')\n 720575940412906756\n >>> pe.encode('Schmidt')\n 720589151732307997"
773 },
774 {
775 "code": "def create_archive(directory, filename, config={}, ignore_predicate=None, ignored_files=['.git', '.svn']):\n with zipfile.ZipFile(filename, 'w', compression=zipfile.ZIP_DEFLATED) as zip_file:\n root_len = len(os.path.abspath(directory))\n out(\"Creating archive: \" + str(filename))\n for root, dirs, files in os.walk(directory, followlinks=True):\n archive_root = os.path.abspath(root)[root_len + 1:]\n for f in files:\n fullpath = os.path.join(root, f)\n archive_name = os.path.join(archive_root, f)\n if filename in fullpath:\n continue\n if ignored_files is not None:\n for name in ignored_files:\n if fullpath.endswith(name):\n out(\"Skipping: \" + str(name))\n continue\n if ignore_predicate is not None:\n if not ignore_predicate(archive_name):\n out(\"Skipping: \" + str(archive_name))\n continue\n out(\"Adding: \" + str(archive_name))\n zip_file.write(fullpath, archive_name, zipfile.ZIP_DEFLATED)\n return filename",
776 "docstring": "Creates an archive from a directory and returns\n the file that was created."
777 },
778 {
779 "code": "def all_childnodes_to_nifti1img(h5group):\n child_nodes = []\n def append_parent_if_dataset(name, obj):\n if isinstance(obj, h5py.Dataset):\n if name.split('/')[-1] == 'data':\n child_nodes.append(obj.parent)\n vols = []\n h5group.visititems(append_parent_if_dataset)\n for c in child_nodes:\n vols.append(hdfgroup_to_nifti1image(c))\n return vols",
780 "docstring": "Returns in a list all images found under h5group.\n\n Parameters\n ----------\n h5group: h5py.Group\n HDF group\n\n Returns\n -------\n list of nifti1Image"
781 },
782 {
783 "code": "def t_t_isopen(self, t):\n r'\"|\\''\n if t.value[0] == '\"':\n t.lexer.push_state('istringquotes')\n elif t.value[0] == '\\'':\n t.lexer.push_state('istringapostrophe')\n return t",
784 "docstring": "r'\"|\\"
785 },
786 {
787 "code": "def expandvars_dict(settings):\n return dict(\n (key, os.path.expandvars(value))\n for key, value in settings.iteritems()\n )",
788 "docstring": "Expands all environment variables in a settings dictionary."
789 },
790 {
791 "code": "def check_pre_requirements(pre_requirements):\n pre_requirements = set(pre_requirements or [])\n pre_requirements.add('virtualenv')\n for requirement in pre_requirements:\n if not which(requirement):\n print_error('Requirement {0!r} is not found in system'.\n format(requirement))\n return False\n return True",
792 "docstring": "Check all necessary system requirements to exist.\n\n :param pre_requirements:\n Sequence of pre-requirements to check by running\n ``where <pre_requirement>`` on Windows and ``which ...`` elsewhere."
793 },
794 {
795 "code": "def get_images_bytesize_match(self, images):\n cnt = 0\n max_bytes_size = 15728640\n good_images = []\n for image in images:\n if cnt > 30:\n return good_images\n src = self.parser.getAttribute(image, attr='src')\n src = self.build_image_path(src)\n src = self.add_schema_if_none(src)\n local_image = self.get_local_image(src)\n if local_image:\n filesize = local_image.bytes\n if (filesize == 0 or filesize > self.images_min_bytes) and filesize < max_bytes_size:\n good_images.append(image)\n else:\n images.remove(image)\n cnt += 1\n return good_images if len(good_images) > 0 else None",
796 "docstring": "\\\n loop through all the images and find the ones\n that have the best bytez to even make them a candidate"
797 },
798 {
799 "code": "def _parse_revision_date(self):\n r\n doc_datetime = None\n if not self.is_draft:\n date_command = LatexCommand(\n 'date',\n {'name': 'content', 'required': True, 'bracket': '{'})\n try:\n parsed = next(date_command.parse(self._tex))\n command_content = parsed['content'].strip()\n except StopIteration:\n command_content = None\n self._logger.warning('lsstdoc has no date command')\n if command_content is not None and command_content != r'\\today':\n try:\n doc_datetime = datetime.datetime.strptime(command_content,\n '%Y-%m-%d')\n project_tz = timezone('US/Pacific')\n localized_datetime = project_tz.localize(doc_datetime)\n doc_datetime = localized_datetime.astimezone(pytz.utc)\n self._revision_datetime_source = 'tex'\n except ValueError:\n self._logger.warning('Could not parse a datetime from '\n 'lsstdoc date command: %r',\n command_content)\n if doc_datetime is None:\n content_extensions = ('tex', 'bib', 'pdf', 'png', 'jpg')\n try:\n doc_datetime = get_content_commit_date(\n content_extensions,\n root_dir=self._root_dir)\n self._revision_datetime_source = 'git'\n except RuntimeError:\n self._logger.warning('Could not get a datetime from the Git '\n 'repository at %r',\n self._root_dir)\n if doc_datetime is None:\n doc_datetime = pytz.utc.localize(datetime.datetime.now())\n self._revision_datetime_source = 'now'\n self._datetime = doc_datetime",
800 "docstring": "r\"\"\"Parse the ``\\date`` command, falling back to getting the\n most recent Git commit date and the current datetime.\n\n Result is available from the `revision_datetime` attribute."
801 },
802 {
803 "code": "def get_single_header(headers, key):\n raw_headers = headers.getRawHeaders(key)\n if raw_headers is None:\n return None\n header, _ = cgi.parse_header(raw_headers[-1])\n return header",
804 "docstring": "Get a single value for the given key out of the given set of headers.\n\n :param twisted.web.http_headers.Headers headers:\n The set of headers in which to look for the header value\n :param str key:\n The header key"
805 },
806 {
807 "code": "def update(self, *args, **kwargs):\n super(Deposit, self).update(*args, **kwargs)",
808 "docstring": "Update only drafts.\n\n Status required: ``'draft'``.\n\n Meta information inside `_deposit` are preserved."
809 },
810 {
811 "code": "def aggregate_tree(l_tree):\n def _aggregate_phase1(tree):\n n_tree = radix.Radix()\n for prefix in tree.prefixes():\n if tree.search_worst(prefix).prefix == prefix:\n n_tree.add(prefix)\n return n_tree\n def _aggregate_phase2(tree):\n n_tree = radix.Radix()\n for rnode in tree:\n p = text(ip_network(text(rnode.prefix)).supernet())\n r = tree.search_covered(p)\n if len(r) == 2:\n if r[0].prefixlen == r[1].prefixlen == rnode.prefixlen:\n n_tree.add(p)\n else:\n n_tree.add(rnode.prefix)\n else:\n n_tree.add(rnode.prefix)\n return n_tree\n l_tree = _aggregate_phase1(l_tree)\n if len(l_tree.prefixes()) == 1:\n return l_tree\n while True:\n r_tree = _aggregate_phase2(l_tree)\n if l_tree.prefixes() == r_tree.prefixes():\n break\n else:\n l_tree = r_tree\n del r_tree\n return l_tree",
812 "docstring": "Walk a py-radix tree and aggregate it.\n\n Arguments\n l_tree -- radix.Radix() object"
813 },
814 {
815 "code": "def switch(template, version):\n temple.update.update(new_template=template, new_version=version)",
816 "docstring": "Switch a project's template to a different template."
817 },
818 {
819 "code": "def send_status_response(environ, start_response, e, add_headers=None, is_head=False):\n status = get_http_status_string(e)\n headers = []\n if add_headers:\n headers.extend(add_headers)\n if e in (HTTP_NOT_MODIFIED, HTTP_NO_CONTENT):\n start_response(\n status, [(\"Content-Length\", \"0\"), (\"Date\", get_rfc1123_time())] + headers\n )\n return [b\"\"]\n if e in (HTTP_OK, HTTP_CREATED):\n e = DAVError(e)\n assert isinstance(e, DAVError)\n content_type, body = e.get_response_page()\n if is_head:\n body = compat.b_empty\n assert compat.is_bytes(body), body\n start_response(\n status,\n [\n (\"Content-Type\", content_type),\n (\"Date\", get_rfc1123_time()),\n (\"Content-Length\", str(len(body))),\n ]\n + headers,\n )\n return [body]",
820 "docstring": "Start a WSGI response for a DAVError or status code."
821 },
822 {
823 "code": "def removeTopology(self, topology_name, state_manager_name):\n topologies = []\n for top in self.topologies:\n if (top.name == topology_name and\n top.state_manager_name == state_manager_name):\n if (topology_name, state_manager_name) in self.topologyInfos:\n self.topologyInfos.pop((topology_name, state_manager_name))\n else:\n topologies.append(top)\n self.topologies = topologies",
824 "docstring": "Removes the topology from the local cache."
825 },
826 {
827 "code": "def encoder_data(self, data):\n prev_val = self.digital_response_table[data[self.RESPONSE_TABLE_MODE]][self.RESPONSE_TABLE_PIN_DATA_VALUE]\n val = int((data[self.MSB] << 7) + data[self.LSB])\n if val > 8192:\n val -= 16384\n pin = data[0]\n with self.pymata.data_lock:\n self.digital_response_table[data[self.RESPONSE_TABLE_MODE]][self.RESPONSE_TABLE_PIN_DATA_VALUE] = val\n if prev_val != val:\n callback = self.digital_response_table[pin][self.RESPONSE_TABLE_CALLBACK]\n if callback is not None:\n callback([self.pymata.ENCODER, pin,\n self.digital_response_table[pin][self.RESPONSE_TABLE_PIN_DATA_VALUE]])",
828 "docstring": "This method handles the incoming encoder data message and stores\n the data in the digital response table.\n\n :param data: Message data from Firmata\n\n :return: No return value."
829 },
830 {
831 "code": "def _dot_to_dec(ip, check=True):\n if check and not is_dot(ip):\n raise ValueError('_dot_to_dec: invalid IP: \"%s\"' % ip)\n octets = str(ip).split('.')\n dec = 0\n dec |= int(octets[0]) << 24\n dec |= int(octets[1]) << 16\n dec |= int(octets[2]) << 8\n dec |= int(octets[3])\n return dec",
832 "docstring": "Dotted decimal notation to decimal conversion."
833 },
834 {
835 "code": "def initialize(self, containers):\n self._containers = deepcopy(containers)\n self.__write(containers, initialize=True)",
836 "docstring": "Initialize a new state file with the given contents.\n This function fails in case the state file already exists."
837 },
838 {
839 "code": "def is_quiet(self):\n level = self._conversation.self_conversation_state.notification_level\n return level == hangouts_pb2.NOTIFICATION_LEVEL_QUIET",
840 "docstring": "``True`` if notification level for this conversation is quiet."
841 },
842 {
843 "code": "def create_dashboard(self, panel_file, data_sources=None, strict=True):\n es_enrich = self.conf['es_enrichment']['url']\n kibana_url = self.conf['panels']['kibiter_url']\n mboxes_sources = set(['pipermail', 'hyperkitty', 'groupsio', 'nntp'])\n if data_sources and any(x in data_sources for x in mboxes_sources):\n data_sources = list(data_sources)\n data_sources.append('mbox')\n if data_sources and ('supybot' in data_sources):\n data_sources = list(data_sources)\n data_sources.append('irc')\n if data_sources and 'google_hits' in data_sources:\n data_sources = list(data_sources)\n data_sources.append('googlehits')\n if data_sources and 'stackexchange' in data_sources:\n data_sources = list(data_sources)\n data_sources.append('stackoverflow')\n if data_sources and 'phabricator' in data_sources:\n data_sources = list(data_sources)\n data_sources.append('maniphest')\n try:\n import_dashboard(es_enrich, kibana_url, panel_file, data_sources=data_sources, strict=strict)\n except ValueError:\n logger.error(\"%s does not include release field. Not loading the panel.\", panel_file)\n except RuntimeError:\n logger.error(\"Can not load the panel %s\", panel_file)",
844 "docstring": "Upload a panel to Elasticsearch if it does not exist yet.\n\n If a list of data sources is specified, upload only those\n elements (visualizations, searches) that match that data source.\n\n :param panel_file: file name of panel (dashobard) to upload\n :param data_sources: list of data sources\n :param strict: only upload a dashboard if it is newer than the one already existing"
845 },
846 {
847 "code": "def get_constructor_arguments(self) -> str:\n item = self._constructor_abi_item\n return '()' if item is None else self.tuple_signature_for_components(item['inputs'])",
848 "docstring": "Returns the tuple type signature for the arguments of the contract constructor."
849 },
850 {
851 "code": "def _get_operation_input_field_values(self, metadata, file_input):\n input_args = metadata['request']['ephemeralPipeline']['inputParameters']\n vals_dict = metadata['request']['pipelineArgs']['inputs']\n names = [\n arg['name'] for arg in input_args if ('localCopy' in arg) == file_input\n ]\n return {name: vals_dict[name] for name in names if name in vals_dict}",
852 "docstring": "Returns a dictionary of envs or file inputs for an operation.\n\n Args:\n metadata: operation metadata field\n file_input: True to return a dict of file inputs, False to return envs.\n\n Returns:\n A dictionary of input field name value pairs"
853 },
854 {
855 "code": "def view_maker(self, name, instance=None):\n if instance is None:\n instance = self\n sig = \"lang\" in [\n parameter.name\n for parameter in inspect.signature(getattr(instance, name)).parameters.values()\n ]\n def route(**kwargs):\n if sig and \"lang\" not in kwargs:\n kwargs[\"lang\"] = self.get_locale()\n if \"semantic\" in kwargs:\n del kwargs[\"semantic\"]\n return self.route(getattr(instance, name), **kwargs)\n return route",
856 "docstring": "Create a view\n\n :param name: Name of the route function to use for the view.\n :type name: str\n :return: Route function which makes use of Nemo context (such as menu informations)\n :rtype: function"
857 },
858 {
859 "code": "def _get_container_port_mappings(app):\n container = app['container']\n port_mappings = container.get('portMappings')\n if port_mappings is None and 'docker' in container:\n port_mappings = container['docker'].get('portMappings')\n return port_mappings",
860 "docstring": "Get the ``portMappings`` field for the app container."
861 },
862 {
863 "code": "def clear(self):\n self._compound_masses = self._compound_masses * 0.0\n self._P = 1.0\n self._T = 25.0\n self._H = 0.0",
864 "docstring": "Set all the compound masses in the package to zero.\n Set the pressure to 1, the temperature to 25 and the enthalpy to zero."
865 },
866 {
867 "code": "def MessageToJson(message, including_default_value_fields=False):\n js = _MessageToJsonObject(message, including_default_value_fields)\n return json.dumps(js, indent=2)",
868 "docstring": "Converts protobuf message to JSON format.\n\n Args:\n message: The protocol buffers message instance to serialize.\n including_default_value_fields: If True, singular primitive fields,\n repeated fields, and map fields will always be serialized. If\n False, only serialize non-empty fields. Singular message fields\n and oneof fields are not affected by this option.\n\n Returns:\n A string containing the JSON formatted protocol buffer message."
869 },
870 {
871 "code": "def _keep_alive_thread(self):\n while True:\n with self._lock:\n if self.connected():\n self._ws.ping()\n else:\n self.disconnect()\n self._thread = None\n return\n sleep(30)",
872 "docstring": "Used exclusively as a thread which keeps the WebSocket alive."
873 },
874 {
875 "code": "def scaffolds_to_contigs(infile, outfile, number_contigs=False):\n seq_reader = sequences.file_reader(infile)\n fout = utils.open_file_write(outfile)\n for seq in seq_reader:\n contigs = seq.contig_coords()\n counter = 1\n for contig in contigs:\n if number_contigs:\n name = seq.id + '.' + str(counter)\n counter += 1\n else:\n name = '.'.join([seq.id, str(contig.start + 1), str(contig.end + 1)])\n print(sequences.Fasta(name, seq[contig.start:contig.end+1]), file=fout)\n utils.close(fout)",
876 "docstring": "Makes a file of contigs from scaffolds by splitting at every N.\n Use number_contigs=True to add .1, .2, etc onto end of each\n contig, instead of default to append coordinates."
877 },
878 {
879 "code": "def parseStringList(s):\n assert isinstance(s, basestring)\n return [int(i) for i in s.split()]",
880 "docstring": "Parse a string of space-separated numbers, returning a Python list.\n\n :param s: (string) to parse\n :returns: (list) binary SDR"
881 },
882 {
883 "code": "def _camelcase_to_underscore(url):\n def upper2underscore(text):\n for char in text:\n if char.islower():\n yield char\n else:\n yield '_'\n if char.isalpha():\n yield char.lower()\n return ''.join(upper2underscore(url))",
884 "docstring": "Translate camelCase into underscore format.\n\n >>> _camelcase_to_underscore('minutesBetweenSummaries')\n 'minutes_between_summaries'"
885 },
886 {
887 "code": "def add_loopless(model, zero_cutoff=None):\n zero_cutoff = normalize_cutoff(model, zero_cutoff)\n internal = [i for i, r in enumerate(model.reactions) if not r.boundary]\n s_int = create_stoichiometric_matrix(model)[:, numpy.array(internal)]\n n_int = nullspace(s_int).T\n max_bound = max(max(abs(b) for b in r.bounds) for r in model.reactions)\n prob = model.problem\n to_add = []\n for i in internal:\n rxn = model.reactions[i]\n indicator = prob.Variable(\"indicator_\" + rxn.id, type=\"binary\")\n on_off_constraint = prob.Constraint(\n rxn.flux_expression - max_bound * indicator,\n lb=-max_bound, ub=0, name=\"on_off_\" + rxn.id)\n delta_g = prob.Variable(\"delta_g_\" + rxn.id)\n delta_g_range = prob.Constraint(\n delta_g + (max_bound + 1) * indicator,\n lb=1, ub=max_bound, name=\"delta_g_range_\" + rxn.id)\n to_add.extend([indicator, on_off_constraint, delta_g, delta_g_range])\n model.add_cons_vars(to_add)\n for i, row in enumerate(n_int):\n name = \"nullspace_constraint_\" + str(i)\n nullspace_constraint = prob.Constraint(Zero, lb=0, ub=0, name=name)\n model.add_cons_vars([nullspace_constraint])\n coefs = {model.variables[\n \"delta_g_\" + model.reactions[ridx].id]: row[i]\n for i, ridx in enumerate(internal) if\n abs(row[i]) > zero_cutoff}\n model.constraints[name].set_linear_coefficients(coefs)",
888 "docstring": "Modify a model so all feasible flux distributions are loopless.\n\n In most cases you probably want to use the much faster `loopless_solution`.\n May be used in cases where you want to add complex constraints and\n objecives (for instance quadratic objectives) to the model afterwards\n or use an approximation of Gibbs free energy directions in you model.\n Adds variables and constraints to a model which will disallow flux\n distributions with loops. The used formulation is described in [1]_.\n This function *will* modify your model.\n\n Parameters\n ----------\n model : cobra.Model\n The model to which to add the constraints.\n zero_cutoff : positive float, optional\n Cutoff used for null space. Coefficients with an absolute value smaller\n than `zero_cutoff` are considered to be zero (default model.tolerance).\n\n Returns\n -------\n Nothing\n\n References\n ----------\n .. [1] Elimination of thermodynamically infeasible loops in steady-state\n metabolic models. Schellenberger J, Lewis NE, Palsson BO. Biophys J.\n 2011 Feb 2;100(3):544-53. doi: 10.1016/j.bpj.2010.12.3707. Erratum\n in: Biophys J. 2011 Mar 2;100(5):1381."
889 },
890 {
891 "code": "def many_to_one(clsname, **kw):\n @declared_attr\n def m2o(cls):\n cls._references((cls.__name__, clsname))\n return relationship(clsname, **kw)\n return m2o",
892 "docstring": "Use an event to build a many-to-one relationship on a class.\n\n This makes use of the :meth:`.References._reference_table` method\n to generate a full foreign key relationship to the remote table."
893 },
894 {
895 "code": "def SyntheticRestaurant(n=20):\n \"Generate a DataSet with n examples.\"\n def gen():\n example = map(random.choice, restaurant.values)\n example[restaurant.target] = Fig[18,2](example)\n return example\n return RestaurantDataSet([gen() for i in range(n)])",
896 "docstring": "Generate a DataSet with n examples."
897 },
898 {
899 "code": "def merge_to_one_seq(infile, outfile, seqname='union'):\n seq_reader = sequences.file_reader(infile)\n seqs = []\n for seq in seq_reader:\n seqs.append(copy.copy(seq))\n new_seq = ''.join([seq.seq for seq in seqs])\n if type(seqs[0]) == sequences.Fastq:\n new_qual = ''.join([seq.qual for seq in seqs])\n seqs[:] = []\n merged = sequences.Fastq(seqname, new_seq, new_qual)\n else:\n merged = sequences.Fasta(seqname, new_seq)\n seqs[:] = []\n f = utils.open_file_write(outfile)\n print(merged, file=f)\n utils.close(f)",
900 "docstring": "Takes a multi fasta or fastq file and writes a new file that contains just one sequence, with the original sequences catted together, preserving their order"
901 },
902 {
903 "code": "def decorator(decorator_func):\n assert callable(decorator_func), type(decorator_func)\n def _decorator(func=None, **kwargs):\n assert func is None or callable(func), type(func)\n if func:\n return decorator_func(func, **kwargs)\n else:\n def _decorator_helper(func):\n return decorator_func(func, **kwargs)\n return _decorator_helper\n return _decorator",
904 "docstring": "Allows a decorator to be called with or without keyword arguments."
905 },
906 {
907 "code": "def _matrix2dict(matrix, etype=False):\n n = len(matrix)\n adj = {k: {} for k in range(n)}\n for k in range(n):\n for j in range(n):\n if matrix[k, j] != 0:\n adj[k][j] = {} if not etype else matrix[k, j]\n return adj",
908 "docstring": "Takes an adjacency matrix and returns an adjacency list."
909 },
910 {
911 "code": "def get_or_default(func=None, default=None):\n def decorator(func):\n @wraps(func)\n def wrapper(*args, **kwargs):\n try:\n return func(*args, **kwargs)\n except ObjectDoesNotExist:\n if callable(default):\n return default()\n else:\n return default\n return wrapper\n if func is None:\n return decorator\n else:\n return decorator(func)",
912 "docstring": "Wrapper around Django's ORM `get` functionality.\n Wrap anything that raises ObjectDoesNotExist exception\n and provide the default value if necessary.\n `default` by default is None. `default` can be any callable,\n if it is callable it will be called when ObjectDoesNotExist\n exception will be raised."
913 },
914 {
915 "code": "def _expand_consumed_mesh(self, mesh, mesh_index, row_position, passed):\n if not mesh.is_produced():\n return\n row = mesh.producing_row\n position = Point(\n row_position.x + mesh.index_in_producing_row - mesh_index,\n row_position.y - INSTRUCTION_HEIGHT\n )\n self._expand(row, position, passed)",
916 "docstring": "expand the consumed meshes"
917 },
918 {
919 "code": "def register_timer_task_in_sec(self, task, second):\n second_in_float = float(second)\n expiration = time.time() + second_in_float\n heappush(self.timer_tasks, (expiration, task))",
920 "docstring": "Registers a new timer task\n\n :param task: function to be run at a specified second from now\n :param second: how many seconds to wait before the timer is triggered"
921 },
922 {
923 "code": "def fmt(a, b):\n return 100 * np.min([a, b], axis=0).sum() / np.max([a, b], axis=0).sum()",
924 "docstring": "Figure of merit in time"
925 },
926 {
927 "code": "def submit(course, tid=None, pastebin=False, review=False):\n if tid is not None:\n return submit_exercise(Exercise.byid(tid),\n pastebin=pastebin,\n request_review=review)\n else:\n sel = Exercise.get_selected()\n if not sel:\n raise NoExerciseSelected()\n return submit_exercise(sel, pastebin=pastebin, request_review=review)",
928 "docstring": "Submit the selected exercise to the server."
929 },
930 {
931 "code": "def _get_warped_array(\n input_file=None,\n indexes=None,\n dst_bounds=None,\n dst_shape=None,\n dst_crs=None,\n resampling=None,\n src_nodata=None,\n dst_nodata=None\n):\n try:\n return _rasterio_read(\n input_file=input_file,\n indexes=indexes,\n dst_bounds=dst_bounds,\n dst_shape=dst_shape,\n dst_crs=dst_crs,\n resampling=resampling,\n src_nodata=src_nodata,\n dst_nodata=dst_nodata\n )\n except Exception as e:\n logger.exception(\"error while reading file %s: %s\", input_file, e)\n raise",
932 "docstring": "Extract a numpy array from a raster file."
933 },
934 {
935 "code": "def fromordinal(cls, n):\n y, m, d = _ord2ymd(n)\n return cls(y, m, d)",
936 "docstring": "Contruct a date from a proleptic Gregorian ordinal.\n\n January 1 of year 1 is day 1. Only the year, month and day are\n non-zero in the result."
937 },
938 {
939 "code": "def get_own_ip():\n own_ip = None\n interfaces = psutil.net_if_addrs()\n for _, details in interfaces.items():\n for detail in details:\n if detail.family == socket.AF_INET:\n ip_address = ipaddress.ip_address(detail.address)\n if not (ip_address.is_link_local or ip_address.is_loopback):\n own_ip = str(ip_address)\n break\n return own_ip",
940 "docstring": "Gets the IP from the inet interfaces."
941 },
942 {
943 "code": "def dist_abs(\n self, src, tar, weights='exponential', max_length=8, normalized=False\n ):\n xored = eudex(src, max_length=max_length) ^ eudex(\n tar, max_length=max_length\n )\n if not weights:\n binary = bin(xored)\n distance = binary.count('1')\n if normalized:\n return distance / (len(binary) - 2)\n return distance\n if callable(weights):\n weights = weights()\n elif weights == 'exponential':\n weights = Eudex.gen_exponential()\n elif weights == 'fibonacci':\n weights = Eudex.gen_fibonacci()\n if isinstance(weights, GeneratorType):\n weights = [next(weights) for _ in range(max_length)][::-1]\n distance = 0\n max_distance = 0\n while (xored or normalized) and weights:\n max_distance += 8 * weights[-1]\n distance += bin(xored & 0xFF).count('1') * weights.pop()\n xored >>= 8\n if normalized:\n distance /= max_distance\n return distance",
944 "docstring": "Calculate the distance between the Eudex hashes of two terms.\n\n Parameters\n ----------\n src : str\n Source string for comparison\n tar : str\n Target string for comparison\n weights : str, iterable, or generator function\n The weights or weights generator function\n\n - If set to ``None``, a simple Hamming distance is calculated.\n - If set to ``exponential``, weight decays by powers of 2, as\n proposed in the eudex specification:\n https://github.com/ticki/eudex.\n - If set to ``fibonacci``, weight decays through the Fibonacci\n series, as in the eudex reference implementation.\n - If set to a callable function, this assumes it creates a\n generator and the generator is used to populate a series of\n weights.\n - If set to an iterable, the iterable's values should be\n integers and will be used as the weights.\n\n max_length : int\n The number of characters to encode as a eudex hash\n normalized : bool\n Normalizes to [0, 1] if True\n\n Returns\n -------\n int\n The Eudex Hamming distance\n\n Examples\n --------\n >>> cmp = Eudex()\n >>> cmp.dist_abs('cat', 'hat')\n 128\n >>> cmp.dist_abs('Niall', 'Neil')\n 2\n >>> cmp.dist_abs('Colin', 'Cuilen')\n 10\n >>> cmp.dist_abs('ATCG', 'TAGC')\n 403\n\n >>> cmp.dist_abs('cat', 'hat', weights='fibonacci')\n 34\n >>> cmp.dist_abs('Niall', 'Neil', weights='fibonacci')\n 2\n >>> cmp.dist_abs('Colin', 'Cuilen', weights='fibonacci')\n 7\n >>> cmp.dist_abs('ATCG', 'TAGC', weights='fibonacci')\n 117\n\n >>> cmp.dist_abs('cat', 'hat', weights=None)\n 1\n >>> cmp.dist_abs('Niall', 'Neil', weights=None)\n 1\n >>> cmp.dist_abs('Colin', 'Cuilen', weights=None)\n 2\n >>> cmp.dist_abs('ATCG', 'TAGC', weights=None)\n 9\n\n >>> # Using the OEIS A000142:\n >>> cmp.dist_abs('cat', 'hat', [1, 1, 2, 6, 24, 120, 720, 5040])\n 1\n >>> cmp.dist_abs('Niall', 'Neil', [1, 1, 2, 6, 24, 120, 720, 5040])\n 720\n >>> cmp.dist_abs('Colin', 'Cuilen',\n ... [1, 1, 2, 6, 24, 120, 720, 5040])\n 744\n >>> cmp.dist_abs('ATCG', 'TAGC', [1, 1, 2, 6, 24, 120, 720, 5040])\n 6243"
945 },
946 {
947 "code": "def tradepile(self):\n method = 'GET'\n url = 'tradepile'\n rc = self.__request__(method, url)\n events = [self.pin.event('page_view', 'Hub - Transfers'), self.pin.event('page_view', 'Transfer List - List View')]\n if rc.get('auctionInfo'):\n events.append(self.pin.event('page_view', 'Item - Detail View'))\n self.pin.send(events)\n return [itemParse(i) for i in rc.get('auctionInfo', ())]",
948 "docstring": "Return items in tradepile."
949 },
950 {
951 "code": "def get_all_certificates(self):\n data = self.get_data(\"certificates\")\n certificates = list()\n for jsoned in data['certificates']:\n cert = Certificate(**jsoned)\n cert.token = self.token\n certificates.append(cert)\n return certificates",
952 "docstring": "This function returns a list of Certificate objects."
953 },
954 {
955 "code": "def create_jwt(integration_id, private_key_path):\n integration_id = int(integration_id)\n with open(private_key_path, 'rb') as f:\n cert_bytes = f.read()\n now = datetime.datetime.now()\n expiration_time = now + datetime.timedelta(minutes=9)\n payload = {\n 'iat': int(now.timestamp()),\n 'exp': int(expiration_time.timestamp()),\n 'iss': integration_id\n }\n return jwt.encode(payload, cert_bytes, algorithm='RS256')",
956 "docstring": "Create a JSON Web Token to authenticate a GitHub Integration or\n installation.\n\n Parameters\n ----------\n integration_id : `int`\n Integration ID. This is available from the GitHub integration's\n homepage.\n private_key_path : `str`\n Path to the integration's private key (a ``.pem`` file).\n\n Returns\n -------\n jwt : `bytes`\n JSON Web Token that is good for 9 minutes.\n\n Notes\n -----\n The JWT is encoded with the RS256 algorithm. It includes a payload with\n fields:\n\n - ``'iat'``: The current time, as an `int` timestamp.\n - ``'exp'``: Expiration time, as an `int timestamp. The expiration\n time is set of 9 minutes in the future (maximum allowance is 10 minutes).\n - ``'iss'``: The integration ID (`int`).\n\n For more information, see\n https://developer.github.com/early-access/integrations/authentication/."
957 },
958 {
959 "code": "def allow_request(self, request, view):\n service_users = get_service_usernames()\n if request.user.username in service_users:\n self.update_throttle_scope()\n return super(ServiceUserThrottle, self).allow_request(request, view)",
960 "docstring": "Modify throttling for service users.\n\n Updates throttling rate if the request is coming from the service user, and\n defaults to UserRateThrottle's configured setting otherwise.\n\n Updated throttling rate comes from `DEFAULT_THROTTLE_RATES` key in `REST_FRAMEWORK`\n setting. service user throttling is specified in `DEFAULT_THROTTLE_RATES` by `service_user` key\n\n Example Setting:\n ```\n REST_FRAMEWORK = {\n ...\n 'DEFAULT_THROTTLE_RATES': {\n ...\n 'service_user': '50/day'\n }\n }\n ```"
961 },
962 {
963 "code": "def map(self, map_function):\n from heronpy.streamlet.impl.mapbolt import MapStreamlet\n map_streamlet = MapStreamlet(map_function, self)\n self._add_child(map_streamlet)\n return map_streamlet",
964 "docstring": "Return a new Streamlet by applying map_function to each element of this Streamlet."
965 },
966 {
967 "code": "def connect(self):\n try:\n logger.info(u'Connecting %s:%d' % (self.host, self.port))\n self.sock.connect((self.host, self.port))\n except socket.error:\n raise ConnectionError()\n self.state = CONNECTED",
968 "docstring": "Connect to the server\n\n :raise ConnectionError: If socket cannot establish a connection"
969 },
970 {
971 "code": "def _get_algorithm_info(self, algorithm_info):\n if algorithm_info['algorithm'] not in self.ALGORITHMS:\n raise Exception('Algorithm not supported: %s'\n % algorithm_info['algorithm'])\n algorithm = self.ALGORITHMS[algorithm_info['algorithm']]\n algorithm_info.update(algorithm)\n return algorithm_info",
972 "docstring": "Get algorithm info"
973 },
974 {
975 "code": "def path_to_node(tree, path):\n if path is None:\n return None\n node = tree\n for key in path:\n node = child_by_key(node, key)\n return node",
976 "docstring": "FST node located at the given path"
977 },
978 {
979 "code": "def INC(cpu, dest):\n arg0 = dest.read()\n res = dest.write(arg0 + 1)\n res &= (1 << dest.size) - 1\n SIGN_MASK = 1 << (dest.size - 1)\n cpu.AF = ((arg0 ^ 1) ^ res) & 0x10 != 0\n cpu.ZF = res == 0\n cpu.SF = (res & SIGN_MASK) != 0\n cpu.OF = res == SIGN_MASK\n cpu.PF = cpu._calculate_parity_flag(res)",
980 "docstring": "Increments by 1.\n\n Adds 1 to the destination operand, while preserving the state of the\n CF flag. The destination operand can be a register or a memory location.\n This instruction allows a loop counter to be updated without disturbing\n the CF flag. (Use a ADD instruction with an immediate operand of 1 to\n perform an increment operation that does updates the CF flag.)::\n\n DEST = DEST +1;\n\n :param cpu: current CPU.\n :param dest: destination operand."
981 },
982 {
983 "code": "def i2c_stop_reading(self, address):\n data = [address, self.I2C_STOP_READING]\n self._command_handler.send_sysex(self._command_handler.I2C_REQUEST, data)",
984 "docstring": "This method stops an I2C_READ_CONTINUOUSLY operation for the i2c device address specified.\n\n :param address: address of i2c device"
985 },
986 {
987 "code": "def get_last_commit(git_path=None):\n if git_path is None: git_path = GIT_PATH\n line = get_last_commit_line(git_path)\n revision_id = line.split()[1]\n return revision_id",
988 "docstring": "Get the HEAD commit SHA1 of repository in current dir."
989 },
990 {
991 "code": "def file_serializer(obj):\n return {\n \"id\": str(obj.file_id),\n \"filename\": obj.key,\n \"filesize\": obj.file.size,\n \"checksum\": obj.file.checksum,\n }",
992 "docstring": "Serialize a object.\n\n :param obj: A :class:`invenio_files_rest.models.ObjectVersion` instance.\n :returns: A dictionary with the fields to serialize."
993 },
994 {
995 "code": "def _keywords(self):\n meta = self.find(\"meta\", {\"name\":\"keywords\"})\n if isinstance(meta, dict) and \\\n meta.has_key(\"content\"):\n keywords = [k.strip() for k in meta[\"content\"].split(\",\")]\n else:\n keywords = []\n return keywords",
996 "docstring": "Returns the meta keywords in the page."
997 },
998 {
999 "code": "def update_throttle_scope(self):\n self.scope = SERVICE_USER_SCOPE\n self.rate = self.get_rate()\n self.num_requests, self.duration = self.parse_rate(self.rate)",
1000 "docstring": "Update throttle scope so that service user throttle rates are applied."
1001 },
1002 {
1003 "code": "def predict_proba(self, X):\n check_is_fitted(self, ['inverse_influence_matrix'])\n X = check_array(X)\n return self.__find_leverages(X, self.inverse_influence_matrix)",
1004 "docstring": "Predict the distances for X to center of the training set.\n\n Parameters\n ----------\n X : array-like or sparse matrix, shape (n_samples, n_features)\n The input samples. Internally, it will be converted to\n ``dtype=np.float32`` and if a sparse matrix is provided\n to a sparse ``csr_matrix``.\n\n Returns\n -------\n leverages: array of shape = [n_samples]\n The objects distances to center of the training set."
1005 },
1006 {
1007 "code": "def getch():\n try:\n termios.tcsetattr(_fd, termios.TCSANOW, _new_settings)\n ch = sys.stdin.read(1)\n finally:\n termios.tcsetattr(_fd, termios.TCSADRAIN, _old_settings)\n return ch",
1008 "docstring": "get character. waiting for key"
1009 },
1010 {
1011 "code": "def _in_git_repo():\n ret = temple.utils.shell('git rev-parse', stderr=subprocess.DEVNULL, check=False)\n return ret.returncode == 0",
1012 "docstring": "Returns True if inside a git repo, False otherwise"
1013 },
1014 {
1015 "code": "def smooth_fwhm(self, fwhm):\n if fwhm != self._smooth_fwhm:\n self._is_data_smooth = False\n self._smooth_fwhm = fwhm",
1016 "docstring": "Set a smoothing Gaussian kernel given its FWHM in mm."
1017 },
1018 {
1019 "code": "def record_delete_subfield(rec, tag, subfield_code, ind1=' ', ind2=' '):\n ind1, ind2 = _wash_indicators(ind1, ind2)\n for field in rec.get(tag, []):\n if field[1] == ind1 and field[2] == ind2:\n field[0][:] = [subfield for subfield in field[0]\n if subfield_code != subfield[0]]",
1020 "docstring": "Delete all subfields with subfield_code in the record."
1021 },
1022 {
1023 "code": "def sendCommands(comPort, commands):\n mutex.acquire()\n try:\n try:\n port = serial.Serial(port=comPort)\n header = '11010101 10101010'\n footer = '10101101'\n for command in _translateCommands(commands):\n _sendBinaryData(port, header + command + footer)\n except serial.SerialException:\n print('Unable to open serial port %s' % comPort)\n print('')\n raise\n finally:\n mutex.release()",
1024 "docstring": "Send X10 commands using the FireCracker on comPort\n\n comPort should be the name of a serial port on the host platform. On\n Windows, for example, 'com1'.\n\n commands should be a string consisting of X10 commands separated by\n commas. For example. 'A1 On, A Dim, A Dim, A Dim, A Lamps Off'. The\n letter is a house code (A-P) and the number is the device number (1-16).\n Possible commands for a house code / device number combination are\n 'On' and 'Off'. The commands 'Bright' and 'Dim' should be used with a\n house code alone after sending an On command to a specific device. The\n 'All On', 'All Off', 'Lamps On', and 'Lamps Off' commands should also\n be used with a house code alone.\n\n # Turn on module A1\n >>> sendCommands('com1', 'A1 On')\n\n # Turn all modules with house code A off\n >>> sendCommands('com1', 'A All Off')\n\n # Turn all lamp modules with house code B on\n >>> sendCommands('com1', 'B Lamps On')\n\n # Turn on module A1 and dim it 3 steps, then brighten it 1 step\n >>> sendCommands('com1', 'A1 On, A Dim, A Dim, A Dim, A Bright')"
1025 },
1026 {
1027 "code": "def save_traceback(err):\n dirname = safe_path(os.path.expanduser(\n os.path.join('~', '.{0}'.format(__script__))\n ))\n if not os.path.isdir(dirname):\n os.mkdir(dirname)\n filename = os.path.join(dirname, '{0}.log'.format(__script__))\n with open(filename, 'a+') as handler:\n traceback.print_exc(file=handler)\n message = ('User aborted workflow'\n if isinstance(err, KeyboardInterrupt)\n else 'Unexpected error catched')\n print_error(message)\n print_error('Full log stored to {0}'.format(filename), False)\n return True",
1028 "docstring": "Save error traceback to bootstrapper log file.\n\n :param err: Catched exception."
1029 },
1030 {
1031 "code": "def configure_modrpaf(self):\n r = self.local_renderer\n if r.env.modrpaf_enabled:\n self.install_packages()\n self.enable_mod('rpaf')\n else:\n if self.last_manifest.modrpaf_enabled:\n self.disable_mod('mod_rpaf')",
1032 "docstring": "Installs the mod-rpaf Apache module.\n\n https://github.com/gnif/mod_rpaf"
1033 },
1034 {
1035 "code": "def _plugin_endpoint_rename(fn_name, instance):\n if instance and instance.namespaced:\n fn_name = \"r_{0}_{1}\".format(instance.name, fn_name[2:])\n return fn_name",
1036 "docstring": "Rename endpoint function name to avoid conflict when namespacing is set to true\n\n :param fn_name: Name of the route function\n :param instance: Instance bound to the function\n :return: Name of the new namespaced function name"
1037 },
1038 {
1039 "code": "def get_directory(self, path_to_directory, timeout=30, backoff=0.4, max_wait=4):\n response = None\n started_at = None\n time_elapsed = 0\n i = 0\n while time_elapsed < timeout:\n response = self._get('{0}.zip'.format(path_to_directory))\n if response:\n break\n else:\n if started_at is None:\n started_at = time.time()\n time.sleep(min(backoff * (2 ** i), max_wait))\n i += 1\n time_elapsed = time.time() - started_at\n return response",
1040 "docstring": "Gets an artifact directory by its path.\n\n See the `Go artifact directory documentation`__ for example responses.\n\n .. __: http://api.go.cd/current/#get-artifact-directory\n\n .. note::\n Getting a directory relies on Go creating a zip file of the\n directory in question. Because of this Go will zip the file in\n the background and return a 202 Accepted response. It's then up\n to the client to check again later and get the final file.\n\n To work with normal assumptions this :meth:`get_directory` will\n retry itself up to ``timeout`` seconds to get a 200 response to\n return. At that point it will then return the response as is, no\n matter whether it's still 202 or 200. The retry is done with an\n exponential backoff with a max value between retries. See the\n ``backoff`` and ``max_wait`` variables.\n\n If you want to handle the retry logic yourself then use :meth:`get`\n and add '.zip' as a suffix on the directory.\n\n Args:\n path_to_directory (str): The path to the directory to get.\n It can be nested eg ``target/dist.zip``\n timeout (int): How many seconds we will wait in total for a\n successful response from Go when we're receiving 202\n backoff (float): The initial value used for backoff, raises\n exponentially until it reaches ``max_wait``\n max_wait (int): The max time between retries\n\n Returns:\n Response: :class:`gocd.api.response.Response` object\n A successful response is a zip-file."
1041 },
1042 {
1043 "code": "def update(dst, src):\n stack = [(dst, src)]\n def isdict(o):\n return hasattr(o, 'keys')\n while stack:\n current_dst, current_src = stack.pop()\n for key in current_src:\n if key not in current_dst:\n current_dst[key] = current_src[key]\n else:\n if isdict(current_src[key]) and isdict(current_dst[key]):\n stack.append((current_dst[key], current_src[key]))\n else:\n current_dst[key] = current_src[key]\n return dst",
1044 "docstring": "Recursively update the destination dict-like object with the source dict-like object.\n\n Useful for merging options and Bunches together!\n\n Based on:\n http://code.activestate.com/recipes/499335-recursively-update-a-dictionary-without-hitting-py/#c1"
1045 },
1046 {
1047 "code": "def remove(name_or_path):\n click.echo()\n try:\n r = cpenv.resolve(name_or_path)\n except cpenv.ResolveError as e:\n click.echo(e)\n return\n obj = r.resolved[0]\n if not isinstance(obj, cpenv.VirtualEnvironment):\n click.echo('{} is a module. Use `cpenv module remove` instead.')\n return\n click.echo(format_objects([obj]))\n click.echo()\n user_confirmed = click.confirm(\n red('Are you sure you want to remove this environment?')\n )\n if user_confirmed:\n click.echo('Attempting to remove...', nl=False)\n try:\n obj.remove()\n except Exception as e:\n click.echo(bold_red('FAIL'))\n click.echo(e)\n else:\n click.echo(bold_green('OK!'))",
1048 "docstring": "Remove an environment"
1049 },
1050 {
1051 "code": "def retrieve_descriptor(descriptor):\n the_descriptor = descriptor\n if the_descriptor is None:\n the_descriptor = {}\n if isinstance(the_descriptor, six.string_types):\n try:\n if os.path.isfile(the_descriptor):\n with open(the_descriptor, 'r') as f:\n the_descriptor = json.load(f)\n else:\n req = requests.get(the_descriptor)\n req.raise_for_status()\n req.encoding = 'utf8'\n the_descriptor = req.json()\n except (IOError, requests.exceptions.RequestException) as error:\n message = 'Unable to load JSON at \"%s\"' % descriptor\n six.raise_from(exceptions.DataPackageException(message), error)\n except ValueError as error:\n message = 'Unable to parse JSON at \"%s\". %s' % (descriptor, error)\n six.raise_from(exceptions.DataPackageException(message), error)\n if hasattr(the_descriptor, 'read'):\n try:\n the_descriptor = json.load(the_descriptor)\n except ValueError as e:\n six.raise_from(exceptions.DataPackageException(str(e)), e)\n if not isinstance(the_descriptor, dict):\n msg = 'Data must be a \\'dict\\', but was a \\'{0}\\''\n raise exceptions.DataPackageException(msg.format(type(the_descriptor).__name__))\n return the_descriptor",
1052 "docstring": "Retrieve descriptor."
1053 },
1054 {
1055 "code": "def decode(geohash):\n lat, lon, lat_err, lon_err = decode_exactly(geohash)\n lats = \"%.*f\" % (max(1, int(round(-log10(lat_err)))) - 1, lat)\n lons = \"%.*f\" % (max(1, int(round(-log10(lon_err)))) - 1, lon)\n if '.' in lats: lats = lats.rstrip('0')\n if '.' in lons: lons = lons.rstrip('0')\n return lats, lons",
1056 "docstring": "Decode geohash, returning two strings with latitude and longitude\n containing only relevant digits and with trailing zeroes removed."
1057 },
1058 {
1059 "code": "def validate(self, strict=True):\n valid = True\n try:\n jsonschema.validate(self.__json__, self.__schema__)\n except jsonschema.ValidationError as invalid:\n if strict:\n raise SchemaError(str(invalid))\n else:\n warnings.warn(str(invalid))\n valid = False\n return valid",
1060 "docstring": "Validate a JObject against its schema\n\n Parameters\n ----------\n strict : bool\n Enforce strict schema validation\n\n Returns\n -------\n valid : bool\n True if the jam validates\n False if not, and `strict==False`\n\n Raises\n ------\n SchemaError\n If `strict==True` and `jam` fails validation"
1061 },
1062 {
1063 "code": "def add_tag(self, tag):\n self.tags = list(set(self.tags or []) | set([tag]))",
1064 "docstring": "Adds a tag to the list of tags and makes sure the result list contains only unique results."
1065 },
1066 {
1067 "code": "def list_(args):\n osf = _setup_osf(args)\n project = osf.project(args.project)\n for store in project.storages:\n prefix = store.name\n for file_ in store.files:\n path = file_.path\n if path.startswith('/'):\n path = path[1:]\n print(os.path.join(prefix, path))",
1068 "docstring": "List all files from all storages for project.\n\n If the project is private you need to specify a username."
1069 },
1070 {
1071 "code": "def filter_config(config, deploy_config):\n if not os.path.isfile(deploy_config):\n return DotDict()\n config_module = get_config_module(deploy_config)\n return config_module.filter(config)",
1072 "docstring": "Return a config subset using the filter defined in the deploy config."
1073 },
1074 {
1075 "code": "def create_session(self):\n session = None\n if self.key_file is not None:\n credfile = os.path.expandvars(os.path.expanduser(self.key_file))\n try:\n with open(credfile, 'r') as f:\n creds = json.load(f)\n except json.JSONDecodeError as e:\n logger.error(\n \"EC2Provider '{}': json decode error in credential file {}\".format(self.label, credfile)\n )\n raise e\n except Exception as e:\n logger.debug(\n \"EC2Provider '{0}' caught exception while reading credential file: {1}\".format(\n self.label, credfile\n )\n )\n raise e\n logger.debug(\"EC2Provider '{}': Using credential file to create session\".format(self.label))\n session = boto3.session.Session(region_name=self.region, **creds)\n elif self.profile is not None:\n logger.debug(\"EC2Provider '{}': Using profile name to create session\".format(self.label))\n session = boto3.session.Session(\n profile_name=self.profile, region_name=self.region\n )\n else:\n logger.debug(\"EC2Provider '{}': Using environment variables to create session\".format(self.label))\n session = boto3.session.Session(region_name=self.region)\n return session",
1076 "docstring": "Create a session.\n\n First we look in self.key_file for a path to a json file with the\n credentials. The key file should have 'AWSAccessKeyId' and 'AWSSecretKey'.\n\n Next we look at self.profile for a profile name and try\n to use the Session call to automatically pick up the keys for the profile from\n the user default keys file ~/.aws/config.\n\n Finally, boto3 will look for the keys in environment variables:\n AWS_ACCESS_KEY_ID: The access key for your AWS account.\n AWS_SECRET_ACCESS_KEY: The secret key for your AWS account.\n AWS_SESSION_TOKEN: The session key for your AWS account.\n This is only needed when you are using temporary credentials.\n The AWS_SECURITY_TOKEN environment variable can also be used,\n but is only supported for backwards compatibility purposes.\n AWS_SESSION_TOKEN is supported by multiple AWS SDKs besides python."
1077 },
1078 {
1079 "code": "async def dispatch_downstream(self, message, steam_name):\n handler = getattr(self, get_handler_name(message), None)\n if handler:\n await handler(message, stream_name=steam_name)\n else:\n await self.base_send(message)",
1080 "docstring": "Handle a downstream message coming from an upstream steam.\n\n if there is not handling method set for this method type it will propagate the message further downstream.\n\n This is called as part of the co-routine of an upstream steam, not the same loop as used for upstream messages\n in the de-multiplexer."
1081 },
1082 {
1083 "code": "def clicky(parser, token):\n bits = token.split_contents()\n if len(bits) > 1:\n raise TemplateSyntaxError(\"'%s' takes no arguments\" % bits[0])\n return ClickyNode()",
1084 "docstring": "Clicky tracking template tag.\n\n Renders Javascript code to track page visits. You must supply\n your Clicky Site ID (as a string) in the ``CLICKY_SITE_ID``\n setting."
1085 },
1086 {
1087 "code": "def call_dcm2nii(work_dir, arguments=''):\n if not op.exists(work_dir):\n raise IOError('Folder {} not found.'.format(work_dir))\n cmd_line = 'dcm2nii {0} \"{1}\"'.format(arguments, work_dir)\n log.info(cmd_line)\n return subprocess.check_call(cmd_line, shell=True)",
1088 "docstring": "Converts all DICOM files within `work_dir` into one or more\n NifTi files by calling dcm2nii on this folder.\n\n Parameters\n ----------\n work_dir: str\n Path to the folder that contain the DICOM files\n\n arguments: str\n String containing all the flag arguments for `dcm2nii` CLI.\n\n Returns\n -------\n sys_code: int\n dcm2nii execution return code"
1089 },
1090 {
1091 "code": "def to_float(option,value):\n if type(value) is str:\n try:\n value=float(value)\n except ValueError:\n pass\n return (option,value)",
1092 "docstring": "Converts string values to floats when appropriate"
1093 },
1094 {
1095 "code": "def print_boggle(board):\n \"Print the board in a 2-d array.\"\n n2 = len(board); n = exact_sqrt(n2)\n for i in range(n2):\n if i % n == 0 and i > 0: print\n if board[i] == 'Q': print 'Qu',\n else: print str(board[i]) + ' ',\n print",
1096 "docstring": "Print the board in a 2-d array."
1097 },
1098 {
1099 "code": "def backend_version(backend, childprocess=None):\n if childprocess is None:\n childprocess = childprocess_default_value()\n if not childprocess:\n return _backend_version(backend)\n else:\n return run_in_childprocess(_backend_version, None, backend)",
1100 "docstring": "Back-end version.\n\n :param backend: back-end (examples:scrot, wx,..)\n :param childprocess: see :py:func:`grab`\n :return: version as string"
1101 },
1102 {
1103 "code": "def make_present_participles(verbs):\n res = []\n for verb in verbs:\n parts = verb.split()\n if parts[0].endswith(\"e\"):\n parts[0] = parts[0][:-1] + \"ing\"\n else:\n parts[0] = parts[0] + \"ing\"\n res.append(\" \".join(parts))\n return res",
1104 "docstring": "Make the list of verbs into present participles\n\n E.g.:\n\n empower -> empowering\n drive -> driving"
1105 },
1106 {
1107 "code": "def active_env_module_resolver(resolver, path):\n from .api import get_active_env\n env = get_active_env()\n if not env:\n raise ResolveError\n mod = env.get_module(path)\n if not mod:\n raise ResolveError\n return mod",
1108 "docstring": "Resolves modules in currently active environment."
1109 },
1110 {
1111 "code": "def execute_sync(self, message):\n info(\"synchronizing message: {message}\")\n with self.world._unlock_temporarily():\n message._sync(self.world)\n self.world._react_to_sync_response(message)\n for actor in self.actors:\n actor._react_to_sync_response(message)",
1112 "docstring": "Respond when the server indicates that the client is out of sync.\n\n The server can request a sync when this client sends a message that \n fails the check() on the server. If the reason for the failure isn't \n very serious, then the server can decide to send it as usual in the \n interest of a smooth gameplay experience. When this happens, the \n server sends out an extra response providing the clients with the\n information they need to resync themselves."
1113 },
1114 {
1115 "code": "def _concat_queries(queries, operators='__and__'):\n if not queries:\n raise ValueError('Expected some `queries`, got {}.'.format(queries))\n if len(queries) == 1:\n return queries[0]\n if isinstance(operators, str):\n operators = [operators] * (len(queries) - 1)\n if len(queries) - 1 != len(operators):\n raise ValueError('Expected `operators` to be a string or a list with the same'\n ' length as `field_names` ({}), got {}.'.format(len(queries),\n operators))\n first, rest, end = queries[0], queries[1:-1], queries[-1:][0]\n bigop = getattr(first, operators[0])\n for i, q in enumerate(rest):\n bigop = getattr(bigop(q), operators[i])\n return bigop(end)",
1116 "docstring": "Create a tinyDB Query object that is the concatenation of each query in `queries`.\n The concatenation operator is taken from `operators`.\n\n Parameters\n ----------\n queries: list of tinydb.Query\n The list of tinydb.Query to be joined.\n\n operators: str or list of str\n List of binary operators to join `queries` into one query.\n Check TinyDB.Query class for possible choices.\n\n Returns\n -------\n query: tinydb.database.Query"
1117 },
1118 {
1119 "code": "def file_supported(cls, filename):\n if not isinstance(filename, str):\n return False\n (_, ext) = os.path.splitext(filename)\n if ext not in cls.extensions:\n return False\n else:\n return True",
1120 "docstring": "Returns a boolean indicating whether the filename has an\n appropriate extension for this class."
1121 },
1122 {
1123 "code": "def invoke_hook_spout_fail(self, message_id, fail_latency_ns):\n if len(self.task_hooks) > 0:\n spout_fail_info = SpoutFailInfo(message_id=message_id,\n spout_task_id=self.get_task_id(),\n fail_latency_ms=fail_latency_ns * system_constants.NS_TO_MS)\n for task_hook in self.task_hooks:\n task_hook.spout_fail(spout_fail_info)",
1124 "docstring": "invoke task hooks for every time spout fails a tuple\n\n :type message_id: str\n :param message_id: message id to which a failed tuple was anchored\n :type fail_latency_ns: float\n :param fail_latency_ns: fail latency in nano seconds"
1125 },
1126 {
1127 "code": "def write_extracted_licenses(lics, out):\n write_value('LicenseID', lics.identifier, out)\n if lics.full_name is not None:\n write_value('LicenseName', lics.full_name, out)\n if lics.comment is not None:\n write_text_value('LicenseComment', lics.comment, out)\n for xref in sorted(lics.cross_ref):\n write_value('LicenseCrossReference', xref, out)\n write_text_value('ExtractedText', lics.text, out)",
1128 "docstring": "Write extracted licenses fields to out."
1129 },
1130 {
1131 "code": "def run_splitted_processing(max_simultaneous_processes, process_name,\n filenames):\n pids = []\n while len(filenames) > 0:\n while len(filenames) > 0 and len(pids) < max_simultaneous_processes:\n filename = filenames.pop()\n pids.append(service_start(service=process_name,\n param=['-f', filename, '-d',\n imported_day]))\n while len(pids) == max_simultaneous_processes:\n time.sleep(sleep_timer)\n pids = update_running_pids(pids)\n while len(pids) > 0:\n time.sleep(sleep_timer)\n pids = update_running_pids(pids)",
1132 "docstring": "Run processes which push the routing dump of the RIPE in a redis\n database.\n The dump has been splitted in multiple files and each process run\n on one of this files."
1133 },
1134 {
1135 "code": "def bdp(tickers, flds, **kwargs):\n logger = logs.get_logger(bdp, level=kwargs.pop('log', logs.LOG_LEVEL))\n con, _ = create_connection()\n ovrds = assist.proc_ovrds(**kwargs)\n logger.info(\n f'loading reference data from Bloomberg:\\n'\n f'{assist.info_qry(tickers=tickers, flds=flds)}'\n )\n data = con.ref(tickers=tickers, flds=flds, ovrds=ovrds)\n if not kwargs.get('cache', False): return [data]\n qry_data = []\n for r, snap in data.iterrows():\n subset = [r]\n data_file = storage.ref_file(\n ticker=snap.ticker, fld=snap.field, ext='pkl', **kwargs\n )\n if data_file:\n if not files.exists(data_file): qry_data.append(data.iloc[subset])\n files.create_folder(data_file, is_file=True)\n data.iloc[subset].to_pickle(data_file)\n return qry_data",
1136 "docstring": "Bloomberg reference data\n\n Args:\n tickers: tickers\n flds: fields to query\n **kwargs: bbg overrides\n\n Returns:\n pd.DataFrame\n\n Examples:\n >>> bdp('IQ US Equity', 'Crncy', raw=True)\n ticker field value\n 0 IQ US Equity Crncy USD\n >>> bdp('IQ US Equity', 'Crncy').reset_index()\n ticker crncy\n 0 IQ US Equity USD"
1137 },
1138 {
1139 "code": "def next_item(self):\n queue = self.queue\n try:\n item = queue.get(block=True, timeout=5)\n return item\n except Exception:\n return None",
1140 "docstring": "Get a single item from the queue."
1141 },
1142 {
1143 "code": "def _run_gevent(app, config, mode):\n import gevent\n import gevent.monkey\n gevent.monkey.patch_all()\n from gevent.pywsgi import WSGIServer\n server_args = {\n \"bind_addr\": (config[\"host\"], config[\"port\"]),\n \"wsgi_app\": app,\n \"keyfile\": None,\n \"certfile\": None,\n }\n protocol = \"http\"\n server_args.update(config.get(\"server_args\", {}))\n dav_server = WSGIServer(server_args[\"bind_addr\"], app)\n _logger.info(\"Running {}\".format(dav_server))\n _logger.info(\n \"Serving on {}://{}:{} ...\".format(protocol, config[\"host\"], config[\"port\"])\n )\n try:\n gevent.spawn(dav_server.serve_forever())\n except KeyboardInterrupt:\n _logger.warning(\"Caught Ctrl-C, shutting down...\")\n return",
1144 "docstring": "Run WsgiDAV using gevent if gevent is installed.\n\n See\n https://github.com/gevent/gevent/blob/master/src/gevent/pywsgi.py#L1356\n https://github.com/gevent/gevent/blob/master/src/gevent/server.py#L38\n for more options"
1145 },
1146 {
1147 "code": "def on_exit_stage(self):\n self.forum.on_finish_game()\n for actor in self.actors:\n actor.on_finish_game()\n with self.world._unlock_temporarily():\n self.world.on_finish_game()",
1148 "docstring": "Give the actors, the world, and the messaging system a chance to react \n to the end of the game."
1149 },
1150 {
1151 "code": "def spin_up_instance(self, command, job_name):\n command = Template(template_string).substitute(jobname=job_name,\n user_script=command,\n linger=str(self.linger).lower(),\n worker_init=self.worker_init)\n instance_type = self.instance_type\n subnet = self.sn_ids[0]\n ami_id = self.image_id\n total_instances = len(self.instances)\n if float(self.spot_max_bid) > 0:\n spot_options = {\n 'MarketType': 'spot',\n 'SpotOptions': {\n 'MaxPrice': str(self.spot_max_bid),\n 'SpotInstanceType': 'one-time',\n 'InstanceInterruptionBehavior': 'terminate'\n }\n }\n else:\n spot_options = {}\n if total_instances > self.max_nodes:\n logger.warn(\"Exceeded instance limit ({}). Cannot continue\\n\".format(self.max_nodes))\n return [None]\n try:\n tag_spec = [{\"ResourceType\": \"instance\", \"Tags\": [{'Key': 'Name', 'Value': job_name}]}]\n instance = self.ec2.create_instances(\n MinCount=1,\n MaxCount=1,\n InstanceType=instance_type,\n ImageId=ami_id,\n KeyName=self.key_name,\n SubnetId=subnet,\n SecurityGroupIds=[self.sg_id],\n TagSpecifications=tag_spec,\n InstanceMarketOptions=spot_options,\n InstanceInitiatedShutdownBehavior='terminate',\n IamInstanceProfile={'Arn': self.iam_instance_profile_arn},\n UserData=command\n )\n except ClientError as e:\n print(e)\n logger.error(e.response)\n return [None]\n except Exception as e:\n logger.error(\"Request for EC2 resources failed : {0}\".format(e))\n return [None]\n self.instances.append(instance[0].id)\n logger.info(\n \"Started up 1 instance {} . Instance type:{}\".format(instance[0].id, instance_type)\n )\n return instance",
1152 "docstring": "Start an instance in the VPC in the first available subnet.\n\n N instances will be started if nodes_per_block > 1.\n Not supported. We only do 1 node per block.\n\n Parameters\n ----------\n command : str\n Command string to execute on the node.\n job_name : str\n Name associated with the instances."
1153 },
1154 {
1155 "code": "def sort_by_modified(files_or_folders: list) -> list:\n return sorted(files_or_folders, key=os.path.getmtime, reverse=True)",
1156 "docstring": "Sort files or folders by modified time\n\n Args:\n files_or_folders: list of files or folders\n\n Returns:\n list"
1157 },
1158 {
1159 "code": "def contains_ignoring_case(self, *items):\n if len(items) == 0:\n raise ValueError('one or more args must be given')\n if isinstance(self.val, str_types):\n if len(items) == 1:\n if not isinstance(items[0], str_types):\n raise TypeError('given arg must be a string')\n if items[0].lower() not in self.val.lower():\n self._err('Expected <%s> to case-insensitive contain item <%s>, but did not.' % (self.val, items[0]))\n else:\n missing = []\n for i in items:\n if not isinstance(i, str_types):\n raise TypeError('given args must all be strings')\n if i.lower() not in self.val.lower():\n missing.append(i)\n if missing:\n self._err('Expected <%s> to case-insensitive contain items %s, but did not contain %s.' % (self.val, self._fmt_items(items), self._fmt_items(missing)))\n elif isinstance(self.val, Iterable):\n missing = []\n for i in items:\n if not isinstance(i, str_types):\n raise TypeError('given args must all be strings')\n found = False\n for v in self.val:\n if not isinstance(v, str_types):\n raise TypeError('val items must all be strings')\n if i.lower() == v.lower():\n found = True\n break\n if not found:\n missing.append(i)\n if missing:\n self._err('Expected <%s> to case-insensitive contain items %s, but did not contain %s.' % (self.val, self._fmt_items(items), self._fmt_items(missing)))\n else:\n raise TypeError('val is not a string or iterable')\n return self",
1160 "docstring": "Asserts that val is string and contains the given item or items."
1161 },
1162 {
1163 "code": "def expose_ancestors_or_children(self, member, collection, lang=None):\n x = {\n \"id\": member.id,\n \"label\": str(member.get_label(lang)),\n \"model\": str(member.model),\n \"type\": str(member.type),\n \"size\": member.size,\n \"semantic\": self.semantic(member, parent=collection)\n }\n if isinstance(member, ResourceCollection):\n x[\"lang\"] = str(member.lang)\n return x",
1164 "docstring": "Build an ancestor or descendant dict view based on selected information\n\n :param member: Current Member to build for\n :param collection: Collection from which we retrieved it\n :param lang: Language to express data in\n :return:"
1165 },
1166 {
1167 "code": "def get_mongoadmins(self):\n apps = []\n for app_name in settings.INSTALLED_APPS:\n mongoadmin = \"{0}.mongoadmin\".format(app_name)\n try:\n module = import_module(mongoadmin)\n except ImportError as e:\n if str(e).startswith(\"No module named\"):\n continue\n raise e\n app_store = AppStore(module)\n apps.append(dict(\n app_name=app_name,\n obj=app_store\n ))\n return apps",
1168 "docstring": "Returns a list of all mongoadmin implementations for the site"
1169 },
1170 {
1171 "code": "def move_next_to(self, body_a, body_b, offset_a, offset_b):\n ba = self.get_body(body_a)\n bb = self.get_body(body_b)\n if ba is None:\n return bb.relative_offset_to_world(offset_b)\n if bb is None:\n return ba.relative_offset_to_world(offset_a)\n anchor = ba.relative_offset_to_world(offset_a)\n offset = bb.relative_offset_to_world(offset_b)\n bb.position = bb.position + anchor - offset\n return anchor",
1172 "docstring": "Move one body to be near another one.\n\n After moving, the location described by ``offset_a`` on ``body_a`` will\n be coincident with the location described by ``offset_b`` on ``body_b``.\n\n Parameters\n ----------\n body_a : str or :class:`Body`\n The body to use as a reference for moving the other body. If this is\n a string, it is treated as the name of a body to look up in the\n world.\n body_b : str or :class:`Body`\n The body to move next to ``body_a``. If this is a string, it is\n treated as the name of a body to look up in the world.\n offset_a : 3-tuple of float\n The offset of the anchor point, given as a relative fraction of the\n size of ``body_a``. See :func:`Body.relative_offset_to_world`.\n offset_b : 3-tuple of float\n The offset of the anchor point, given as a relative fraction of the\n size of ``body_b``.\n\n Returns\n -------\n anchor : 3-tuple of float\n The location of the shared point, which is often useful to use as a\n joint anchor."
1173 },
1174 {
1175 "code": "def get_exception_from_status_and_error_codes(status_code, error_code, value):\n if status_code == requests.codes.bad_request:\n exception = BadRequest(value)\n elif status_code == requests.codes.unauthorized:\n exception = Unauthorized(value)\n elif status_code == requests.codes.forbidden:\n exception = Unauthorized(value)\n elif status_code in [requests.codes.not_found, requests.codes.gone]:\n exception = NotFound(value)\n elif status_code == requests.codes.method_not_allowed:\n exception = MethodNotAllowed(value)\n elif status_code >= requests.codes.bad_request:\n exception = HTTPError(value)\n else:\n exception = ResponseError(value)\n if error_code == -100:\n exception = InternalError(value)\n elif error_code == -101:\n exception = InvalidToken(value)\n elif error_code == -105:\n exception = UploadFailed(value)\n elif error_code == -140:\n exception = UploadTokenGenerationFailed(value)\n elif error_code == -141:\n exception = InvalidUploadToken(value)\n elif error_code == -150:\n exception = InvalidParameter(value)\n elif error_code == -151:\n exception = InvalidPolicy(value)\n return exception",
1176 "docstring": "Return an exception given status and error codes.\n\n :param status_code: HTTP status code.\n :type status_code: None | int\n :param error_code: Midas Server error code.\n :type error_code: None | int\n :param value: Message to display.\n :type value: string\n :returns: Exception.\n :rtype : pydas.exceptions.ResponseError"
1177 },
1178 {
1179 "code": "def add_example(self, example):\n \"Add an example to the list of examples, checking it first.\"\n self.check_example(example)\n self.examples.append(example)",
1180 "docstring": "Add an example to the list of examples, checking it first."
1181 },
1182 {
1183 "code": "def removeAllRecords(self):\n for field in self.fields:\n field.encodings, field.values=[], []\n field.numRecords, field.numEncodings= (0, 0)",
1184 "docstring": "Deletes all the values in the dataset"
1185 },
1186 {
1187 "code": "async def get_size(media):\n if hasattr(media, 'seek'):\n await execute(media.seek(0, os.SEEK_END))\n size = await execute(media.tell())\n await execute(media.seek(0))\n elif hasattr(media, 'headers'):\n size = int(media.headers['Content-Length'])\n elif isinstance(media, bytes):\n size = len(media)\n else:\n raise TypeError(\"Can't get size of media of type:\",\n type(media).__name__)\n _logger.info(\"media size: %dB\" % size)\n return size",
1188 "docstring": "Get the size of a file\n\n Parameters\n ----------\n media : file object\n The file object of the media\n\n Returns\n -------\n int\n The size of the file"
1189 },
1190 {
1191 "code": "def set_color_list(self, color_list, offset=0):\n if not len(color_list):\n return\n color_list = make.colors(color_list)\n size = len(self._colors) - offset\n if len(color_list) > size:\n color_list = color_list[:size]\n self._colors[offset:offset + len(color_list)] = color_list",
1192 "docstring": "Set the internal colors starting at an optional offset.\n\n If `color_list` is a list or other 1-dimensional array, it is reshaped\n into an N x 3 list.\n\n If `color_list` too long it is truncated; if it is too short then only\n the initial colors are set."
1193 },
1194 {
1195 "code": "def find(self, y):\n node = self.root\n while True:\n edge = self._edgeLabel(node, node.parent)\n if edge.startswith(y):\n return node.idx\n i = 0\n while(i < len(edge) and edge[i] == y[0]):\n y = y[1:]\n i += 1\n if i != 0:\n if i == len(edge) and y != '':\n pass\n else:\n return -1\n node = node._get_transition_link(y[0])\n if not node:\n return -1",
1196 "docstring": "Returns starting position of the substring y in the string used for\n building the Suffix tree.\n\n :param y: String\n :return: Index of the starting position of string y in the string used for building the Suffix tree\n -1 if y is not a substring."
1197 },
1198 {
1199 "code": "def add_item(self,jid,node=None,name=None,action=None):\n return DiscoItem(self,jid,node,name,action)",
1200 "docstring": "Add a new item to the `DiscoItems` object.\n\n :Parameters:\n - `jid`: item JID.\n - `node`: item node name.\n - `name`: item name.\n - `action`: action for a \"disco push\".\n :Types:\n - `jid`: `pyxmpp.JID`\n - `node`: `unicode`\n - `name`: `unicode`\n - `action`: `unicode`\n\n :returns: the item created.\n :returntype: `DiscoItem`."
