def bump(self, filter_requirements, required=False, show_summary=True, show_detail=False, **kwargs):

found_targets = [target for target in self.targets if os.path.exists(target)]<EOL>if not found_targets:<EOL><INDENT>raise BumpAccident('<STR_LIT>' % '<STR_LIT:U+002CU+0020>'.join(self.targets))<EOL><DEDENT>bump_reqs = RequirementsManager()<EOL>if filter_requirements:<EOL><INDENT>requirements = parse_requirements(filter_requirements)<EOL>bump_reqs.add(requirements, required=required)<EOL><DEDENT>try:<EOL><INDENT>for target in found_targets:<EOL><INDENT>log.debug('<STR_LIT>', target)<EOL>target_bumpers = []<EOL>target_bump_reqs = RequirementsManager(bump_reqs)<EOL>loops = <NUM_LIT:0><EOL>while True:<EOL><INDENT>loops += <NUM_LIT:1><EOL>if loops > <NUM_LIT:5>:<EOL><INDENT>log.debug('<STR_LIT>')<EOL>break<EOL><DEDENT>if not target_bumpers:<EOL><INDENT>target_bumpers = [model(target, detail=self.detail, test_drive=self.test_drive)<EOL>for model in self.bumper_models if model.likes(target)]<EOL>if not target_bumpers:<EOL><INDENT>log.debug('<STR_LIT>', target, self.default_model)<EOL>target_bumpers = [self.default_model(target, detail=self.detail,<EOL>test_drive=self.test_drive)]<EOL><DEDENT>self.bumpers.extend(target_bumpers)<EOL><DEDENT>new_target_bump_reqs = RequirementsManager()<EOL>for bumper in target_bumpers:<EOL><INDENT>target_bumps = bumper.bump(target_bump_reqs)<EOL>self.bumps.update(dict((b.name, b) for b in target_bumps))<EOL>for bump in target_bumps:<EOL><INDENT>for new_req in bump.requirements:<EOL><INDENT>if not (bump_reqs.satisfied_by_checked(new_req) or<EOL>target_bump_reqs.satisfied_by_checked(new_req)):<EOL><INDENT>new_target_bump_reqs.add(new_req)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>bump_reqs.matched_name |= target_bump_reqs.matched_name<EOL>bump_reqs.checked.extend(target_bump_reqs.checked)<EOL>if new_target_bump_reqs:<EOL><INDENT>bump_reqs.add(new_target_bump_reqs)<EOL><DEDENT>target_bump_reqs = RequirementsManager(list(<EOL>r for r in new_target_bump_reqs if r.project_name not in self.bumps))<EOL>if not target_bump_reqs:<EOL><INDENT>break<EOL><DEDENT><DEDENT><DEDENT>if not self.bumpers:<EOL><INDENT>raise BumpAccident('<STR_LIT>' % '<STR_LIT:U+002CU+0020>'.join(found_targets))<EOL><DEDENT>if bump_reqs and not bump_reqs.matched_name:<EOL><INDENT>raise BumpAccident('<STR_LIT>' % '<STR_LIT:U+002CU+0020>'.join(found_targets))<EOL><DEDENT>if self.bumps:<EOL><INDENT>for bump in self.bumps.values():<EOL><INDENT>bump_reqs.check(bump)<EOL><DEDENT>for reqs in bump_reqs.required_requirements().values():<EOL><INDENT>for req in reqs:<EOL><INDENT>if not self.full_throttle:<EOL><INDENT>use_force = '<STR_LIT>' if req.required_by else '<STR_LIT>'<EOL>raise BumpAccident('<STR_LIT>'<EOL>'<STR_LIT>' % (req, use_force))<EOL><DEDENT><DEDENT><DEDENT>if self.test_drive:<EOL><INDENT>log.info("<STR_LIT>")<EOL><DEDENT>messages = {}<EOL>for bumper in self.bumpers:<EOL><INDENT>if bumper.bumps:<EOL><INDENT>if not self.test_drive:<EOL><INDENT>bumper.update_requirements()<EOL><DEDENT>if self.test_drive or show_summary:<EOL><INDENT>msg = bumper.bump_message(self.test_drive or show_detail)<EOL>if self.test_drive:<EOL><INDENT>print(msg)<EOL><DEDENT>else:<EOL><INDENT>rewords = [('<STR_LIT>', '<STR_LIT>'), ('<STR_LIT>', '<STR_LIT>'),<EOL>('<STR_LIT>', '<STR_LIT>')]<EOL>for word, new_word in rewords:<EOL><INDENT>if msg.startswith(word):<EOL><INDENT>msg = msg.replace(word, new_word, <NUM_LIT:1>)<EOL>break<EOL><DEDENT><DEDENT>log.info(msg)<EOL><DEDENT><DEDENT>messages[bumper.target] = bumper.bump_message(True)<EOL><DEDENT><DEDENT>return messages, self.bumps<EOL><DEDENT>else:<EOL><INDENT>log.info('<STR_LIT>')<EOL>return {}, []<EOL><DEDENT><DEDENT>except Exception:<EOL><INDENT>if not self.test_drive and self.bumps:<EOL><INDENT>map(lambda b: b.reverse(), self.bumpers)<EOL><DEDENT>raise<EOL><DEDENT>

Bump dependency requirements using filter.

:param list filter_requirements: List of dependency filter requirements.
:param bool required: Require the filter_requirements to be met (by adding if possible).
:param bool show_summary: Show summary for each bump made.
:param bool show_detail: Show detail for each bump made if available.
:return: Tuple with two elements: Dict of target file to bump message, List of :class:`Bump`
:raise BumpAccident: for any bump errors


if not self.test_drive and self.bumps:<EOL><INDENT>map(lambda b: b.reverse(), self.bumpers)<EOL><DEDENT>


def _expand_targets(self, targets, base_dir=None):

all_targets = []<EOL>for target in targets:<EOL><INDENT>target_dirs = [p for p in [base_dir, os.path.dirname(target)] if p]<EOL>target_dir = target_dirs and os.path.join(*target_dirs) or '<STR_LIT>'<EOL>target = os.path.basename(target)<EOL>target_path = os.path.join(target_dir, target)<EOL>if os.path.exists(target_path):<EOL><INDENT>all_targets.append(target_path)<EOL>with open(target_path) as fp:<EOL><INDENT>for line in fp:<EOL><INDENT>if line.startswith('<STR_LIT>'):<EOL><INDENT>_, new_target = line.split('<STR_LIT:U+0020>', <NUM_LIT:1>)<EOL>all_targets.extend(self._expand_targets([new_target.strip()], base_dir=target_dir))<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>return all_targets<EOL>

Expand targets by looking for '-r' in targets.


try:<EOL><INDENT>return list(pkg_resources.parse_requirements(requirements))<EOL><DEDENT>except Exception as e:<EOL><INDENT>in_file = '<STR_LIT>' % in_file if in_file else '<STR_LIT>'<EOL>raise ValueError('<STR_LIT>'.format(e, in_file))<EOL><DEDENT>

Parse string requirements into list of :class:`pkg_resources.Requirement` instances

:param str requirements: Requirements text to parse
:param str in_file: File the requirements came from
:return: List of requirements
:raises ValueError: if failed to parse


if package not in cls.package_info_cache:<EOL><INDENT>package_json_url = '<STR_LIT>' % package<EOL>try:<EOL><INDENT>logging.getLogger('<STR_LIT>').setLevel(logging.WARN)<EOL>response = requests.get(package_json_url)<EOL>response.raise_for_status()<EOL>cls.package_info_cache[package] = simplejson.loads(response.text)<EOL><DEDENT>except Exception as e:<EOL><INDENT>log.debug('<STR_LIT>', package_json_url, e)<EOL>cls.package_info_cache[package] = None<EOL><DEDENT><DEDENT>return cls.package_info_cache[package]<EOL>


info = PyPI.package_info(package)<EOL>return info and sorted(info['<STR_LIT>'].keys(), key=lambda x: x.split(), reverse=True) or []<EOL>


data = super(IndexBuilder, self).freeze()<EOL>try:<EOL><INDENT>base_file_names = data['<STR_LIT>']<EOL><DEDENT>except KeyError:<EOL><INDENT>base_file_names = data['<STR_LIT>']<EOL><DEDENT>store = {}<EOL>c = itertools.count()<EOL>for prefix, items in iteritems(data['<STR_LIT>']):<EOL><INDENT>for name, (index, typeindex, _, shortanchor) in iteritems(items):<EOL><INDENT>objtype = data['<STR_LIT>'][typeindex]<EOL>if objtype.startswith('<STR_LIT>'):<EOL><INDENT>split =  name.rsplit('<STR_LIT>', <NUM_LIT:1>)<EOL>if len(split) != <NUM_LIT:2>:<EOL><INDENT>warnings.warn("<STR_LIT>" % str((prefix, name, objtype)))<EOL>continue<EOL><DEDENT>prefix, name = split<EOL>last_prefix = prefix.split('<STR_LIT>')[-<NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>last_prefix = prefix.split('<STR_LIT:.>')[-<NUM_LIT:1>]<EOL><DEDENT>store[next(c)] = {<EOL>'<STR_LIT:filename>': base_file_names[index],<EOL>'<STR_LIT>': objtype,<EOL>'<STR_LIT>': prefix,<EOL>'<STR_LIT>': last_prefix,<EOL>'<STR_LIT:name>': name,<EOL>'<STR_LIT>': shortanchor,<EOL>}<EOL><DEDENT><DEDENT>data.update({'<STR_LIT:store>': store})<EOL>return data<EOL>

Create a usable data structure for serializing.


global TOKENIZER<EOL>if not TOKENIZER:<EOL><INDENT>TOKENIZER = DeepcutTokenizer()<EOL><DEDENT>return TOKENIZER.tokenize(text, custom_dict=custom_dict)<EOL>

Tokenize given Thai text string

Input
=====
text: str, Thai text string
custom_dict: str (or list), path to customized dictionary file
    It allows the function not to tokenize given dictionary wrongly.
    The file should contain custom words separated by line.
    Alternatively, you can provide list of custom words too.

Output
======
tokens: list, list of tokenized words

Example
=======
>> deepcut.tokenize('ตัดคำได้ดีมาก')
>> ['ตัดคำ','ได้','ดี','มาก']


if sp.isspmatrix_csr(X):<EOL><INDENT>return np.bincount(X.indices, minlength=X.shape[<NUM_LIT:1>])<EOL><DEDENT>return np.diff(sp.csc_matrix(X, copy=False).indptr)<EOL>

Count the number of non-zero values for each feature in sparse X.


if stop == "<STR_LIT>":<EOL><INDENT>return THAI_STOP_WORDS<EOL><DEDENT>elif isinstance(stop, six.string_types):<EOL><INDENT>raise ValueError("<STR_LIT>" % stop)<EOL><DEDENT>elif stop is None:<EOL><INDENT>return None<EOL><DEDENT>return frozenset(stop)<EOL>

Check stop words list
ref: https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/feature_extraction/text.py#L87-L95


<EOL>if self.stop_words is not None:<EOL><INDENT>tokens = [w for w in tokens if w not in self.stop_words]<EOL><DEDENT>min_n, max_n = self.ngram_range<EOL>if max_n != <NUM_LIT:1>:<EOL><INDENT>original_tokens = tokens<EOL>if min_n == <NUM_LIT:1>:<EOL><INDENT>tokens = list(original_tokens)<EOL>min_n += <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>tokens = []<EOL><DEDENT>n_original_tokens = len(original_tokens)<EOL>tokens_append = tokens.append<EOL>space_join = "<STR_LIT:U+0020>".join<EOL>for n in range(min_n,<EOL>min(max_n + <NUM_LIT:1>, n_original_tokens + <NUM_LIT:1>)):<EOL><INDENT>for i in range(n_original_tokens - n + <NUM_LIT:1>):<EOL><INDENT>tokens_append(space_join(original_tokens[i: i + n]))<EOL><DEDENT><DEDENT><DEDENT>return tokens<EOL>

Turn tokens into a tokens of n-grams

ref: https://github.com/scikit-learn/scikit-learn/blob/ef5cb84a/sklearn/feature_extraction/text.py#L124-L153


def _limit_features(self, X, vocabulary,<EOL>high=None, low=None, limit=None):

if high is None and low is None and limit is None:<EOL><INDENT>return X, set()<EOL><DEDENT>dfs = _document_frequency(X)<EOL>tfs = np.asarray(X.sum(axis=<NUM_LIT:0>)).ravel()<EOL>mask = np.ones(len(dfs), dtype=bool)<EOL>if high is not None:<EOL><INDENT>mask &= dfs <= high<EOL><DEDENT>if low is not None:<EOL><INDENT>mask &= dfs >= low<EOL><DEDENT>if limit is not None and mask.sum() > limit:<EOL><INDENT>mask_inds = (-tfs[mask]).argsort()[:limit]<EOL>new_mask = np.zeros(len(dfs), dtype=bool)<EOL>new_mask[np.where(mask)[<NUM_LIT:0>][mask_inds]] = True<EOL>mask = new_mask<EOL><DEDENT>new_indices = np.cumsum(mask) - <NUM_LIT:1>  <EOL>removed_terms = set()<EOL>for term, old_index in list(vocabulary.items()):<EOL><INDENT>if mask[old_index]:<EOL><INDENT>vocabulary[term] = new_indices[old_index]<EOL><DEDENT>else:<EOL><INDENT>del vocabulary[term]<EOL>removed_terms.add(term)<EOL><DEDENT><DEDENT>kept_indices = np.where(mask)[<NUM_LIT:0>]<EOL>if len(kept_indices) == <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>return X[:, kept_indices], vocabulary, removed_terms<EOL>

Remove too rare or too common features.

        ref: https://github.com/scikit-learn/scikit-learn/blob/ef5cb84a/sklearn/feature_extraction/text.py#L734-L773


X = self.transform(raw_documents, new_document=True)<EOL>return X<EOL>

Transform given list of raw_documents to document-term matrix in
sparse CSR format (see scipy)


repls = {'<STR_LIT>' : '<STR_LIT>','<STR_LIT>' : '<STR_LIT>','<STR_LIT>': '<STR_LIT>','<STR_LIT>': '<STR_LIT>'}<EOL>words_all = []<EOL>for i, file in enumerate(files):<EOL><INDENT>lines = open(file, '<STR_LIT:r>')<EOL>for line in lines:<EOL><INDENT>line = reduce(lambda a, kv: a.replace(*kv), repls.items(), line)<EOL>words = [word for word in line.split("<STR_LIT:|>") if word is not '<STR_LIT:\n>']<EOL>words_all.extend(words)<EOL><DEDENT><DEDENT>return words_all<EOL>

Transform list of files to list of words,
removing new line character
and replace name entity '<NE>...</NE>' and abbreviation '<AB>...</AB>' symbol


char_dict = []<EOL>for word in words:<EOL><INDENT>for i, char in enumerate(word):<EOL><INDENT>if i == <NUM_LIT:0>:<EOL><INDENT>char_dict.append({'<STR_LIT>': char,<EOL>'<STR_LIT:type>': CHAR_TYPE_FLATTEN.get(char, '<STR_LIT:o>'),<EOL>'<STR_LIT:target>': True})<EOL><DEDENT>else:<EOL><INDENT>char_dict.append({'<STR_LIT>': char,<EOL>'<STR_LIT:type>': CHAR_TYPE_FLATTEN.get(char, '<STR_LIT:o>'),<EOL>'<STR_LIT:target>': False})<EOL><DEDENT><DEDENT><DEDENT>return pd.DataFrame(char_dict)<EOL>

Give list of input tokenized words,
create dataframe of characters where first character of
the word is tagged as 1, otherwise 0

Example
=======
['กิน', 'หมด'] to dataframe of
[{'char': 'ก', 'type': ..., 'target': 1}, ...,
 {'char': 'ด', 'type': ..., 'target': 0}]


def generate_best_dataset(best_path, output_path='<STR_LIT>', create_val=False):

if not os.path.isdir(output_path):<EOL><INDENT>os.mkdir(output_path)<EOL><DEDENT>if not os.path.isdir(os.path.join(output_path, '<STR_LIT:train>')):<EOL><INDENT>os.makedirs(os.path.join(output_path, '<STR_LIT:train>'))<EOL><DEDENT>if not os.path.isdir(os.path.join(output_path, '<STR_LIT:test>')):<EOL><INDENT>os.makedirs(os.path.join(output_path, '<STR_LIT:test>'))<EOL><DEDENT>if not os.path.isdir(os.path.join(output_path, '<STR_LIT>')) and create_val:<EOL><INDENT>os.makedirs(os.path.join(output_path, '<STR_LIT>'))<EOL><DEDENT>for article_type in article_types:<EOL><INDENT>files = glob(os.path.join(best_path, article_type, '<STR_LIT>'))<EOL>files_train, files_test = train_test_split(files, random_state=<NUM_LIT:0>, test_size=<NUM_LIT:0.1>)<EOL>if create_val:<EOL><INDENT>files_train, files_val = train_test_split(files_train, random_state=<NUM_LIT:0>, test_size=<NUM_LIT:0.1>)<EOL>val_words = generate_words(files_val)<EOL>val_df = create_char_dataframe(val_words)<EOL>val_df.to_csv(os.path.join(output_path, '<STR_LIT>', '<STR_LIT>'.format(article_type)), index=False)<EOL><DEDENT>train_words = generate_words(files_train)<EOL>test_words = generate_words(files_test)<EOL>train_df = create_char_dataframe(train_words)<EOL>test_df = create_char_dataframe(test_words)<EOL>train_df.to_csv(os.path.join(output_path, '<STR_LIT:train>', '<STR_LIT>'.format(article_type)), index=False)<EOL>test_df.to_csv(os.path.join(output_path, '<STR_LIT:test>', '<STR_LIT>'.format(article_type)), index=False)<EOL>print("<STR_LIT>".format(article_type))<EOL><DEDENT>

Generate CSV file for training and testing data

Input
=====
best_path: str, path to BEST folder which contains unzipped subfolder
    'article', 'encyclopedia', 'news', 'novel'

cleaned_data: str, path to output folder, the cleaned data will be saved
    in the given folder name where training set will be stored in `train` folder
    and testing set will be stored on `test` folder

create_val: boolean, True or False, if True, divide training set into training set and
    validation set in `val` folder


<EOL>n_pad = <NUM_LIT><EOL>n_pad_2 = int((n_pad - <NUM_LIT:1>)/<NUM_LIT:2>)<EOL>pad = [{'<STR_LIT>': '<STR_LIT:U+0020>', '<STR_LIT:type>': '<STR_LIT:p>', '<STR_LIT:target>': True}]<EOL>df_pad = pd.DataFrame(pad * n_pad_2)<EOL>df = []<EOL>for article_type in article_types:<EOL><INDENT>df.append(pd.read_csv(os.path.join(best_processed_path, option, '<STR_LIT>'.format(article_type, option))))<EOL><DEDENT>df = pd.concat(df)<EOL>df = pd.concat((df_pad, df, df_pad)) <EOL>df['<STR_LIT>'] = df['<STR_LIT>'].map(lambda x: CHARS_MAP.get(x, <NUM_LIT>))<EOL>df['<STR_LIT:type>'] = df['<STR_LIT:type>'].map(lambda x: CHAR_TYPES_MAP.get(x, <NUM_LIT:4>))<EOL>df_pad = create_n_gram_df(df, n_pad=n_pad)<EOL>char_row = ['<STR_LIT>' + str(i + <NUM_LIT:1>) for i in range(n_pad_2)] +['<STR_LIT>' + str(i + <NUM_LIT:1>) for i in range(n_pad_2)] + ['<STR_LIT>']<EOL>type_row = ['<STR_LIT:type>' + str(i + <NUM_LIT:1>) for i in range(n_pad_2)] +['<STR_LIT>' + str(i + <NUM_LIT:1>) for i in range(n_pad_2)] + ['<STR_LIT:type>']<EOL>x_char = df_pad[char_row].as_matrix()<EOL>x_type = df_pad[type_row].as_matrix()<EOL>y = df_pad['<STR_LIT:target>'].astype(int).as_matrix()<EOL>return x_char, x_type, y<EOL>

Transform processed path into feature matrix and output array

Input
=====
best_processed_path: str, path to processed BEST dataset

option: str, 'train' or 'test'


def train_model(best_processed_path, weight_path='<STR_LIT>', verbose=<NUM_LIT:2>):

x_train_char, x_train_type, y_train = prepare_feature(best_processed_path, option='<STR_LIT:train>')<EOL>x_test_char, x_test_type, y_test = prepare_feature(best_processed_path, option='<STR_LIT:test>')<EOL>validation_set = False<EOL>if os.path.isdir(os.path.join(best_processed_path, '<STR_LIT>')):<EOL><INDENT>validation_set = True<EOL>x_val_char, x_val_type, y_val = prepare_feature(best_processed_path, option='<STR_LIT>')<EOL><DEDENT>if not os.path.isdir(os.path.dirname(weight_path)):<EOL><INDENT>os.makedirs(os.path.dirname(weight_path)) <EOL><DEDENT>callbacks_list = [<EOL>ReduceLROnPlateau(),<EOL>ModelCheckpoint(<EOL>weight_path,<EOL>save_best_only=True,<EOL>save_weights_only=True,<EOL>monitor='<STR_LIT>',<EOL>mode='<STR_LIT>',<EOL>verbose=<NUM_LIT:1><EOL>)<EOL>]<EOL>model = get_convo_nn2()<EOL>train_params = [(<NUM_LIT:10>, <NUM_LIT>), (<NUM_LIT:3>, <NUM_LIT>), (<NUM_LIT:3>, <NUM_LIT>), (<NUM_LIT:3>, <NUM_LIT>), (<NUM_LIT:3>, <NUM_LIT>)]<EOL>for (epochs, batch_size) in train_params:<EOL><INDENT>print("<STR_LIT>".format(epochs, batch_size))<EOL>if validation_set:<EOL><INDENT>model.fit([x_train_char, x_train_type], y_train,<EOL>epochs=epochs, batch_size=batch_size,<EOL>verbose=verbose,<EOL>callbacks=callbacks_list,<EOL>validation_data=([x_val_char, x_val_type], y_val))<EOL><DEDENT>else:<EOL><INDENT>model.fit([x_train_char, x_train_type], y_train,<EOL>epochs=epochs, batch_size=batch_size,<EOL>verbose=verbose,<EOL>callbacks=callbacks_list)<EOL><DEDENT><DEDENT>return model<EOL>

Given path to processed BEST dataset,
train CNN model for words beginning alongside with
character label encoder and character type label encoder

Input
=====
best_processed_path: str, path to processed BEST dataset
weight_path: str, path to weight path file
verbose: int, verbost option for training Keras model

Output
======
model: keras model, keras model for tokenize prediction


x_test_char, x_test_type, y_test = prepare_feature(best_processed_path, option='<STR_LIT:test>')<EOL>y_predict = model.predict([x_test_char, x_test_type])<EOL>y_predict = (y_predict.ravel() > <NUM_LIT:0.5>).astype(int)<EOL>f1score = f1_score(y_test, y_predict)<EOL>precision = precision_score(y_test, y_predict)<EOL>recall = recall_score(y_test, y_predict)<EOL>return f1score, precision, recall<EOL>

Evaluate model on splitted 10 percent testing set


n = len(text)<EOL>n_pad_2 = int((n_pad - <NUM_LIT:1>)/<NUM_LIT:2>)<EOL>text_pad = ['<STR_LIT:U+0020>'] * n_pad_2  + [t for t in text] + ['<STR_LIT:U+0020>'] * n_pad_2<EOL>x_char, x_type = [], []<EOL>for i in range(n_pad_2, n_pad_2 + n):<EOL><INDENT>char_list = text_pad[i + <NUM_LIT:1>: i + n_pad_2 + <NUM_LIT:1>] +list(reversed(text_pad[i - n_pad_2: i])) +[text_pad[i]]<EOL>char_map = [CHARS_MAP.get(c, <NUM_LIT>) for c in char_list]<EOL>char_type = [CHAR_TYPES_MAP.get(CHAR_TYPE_FLATTEN.get(c, '<STR_LIT:o>'), <NUM_LIT:4>)<EOL>for c in char_list]<EOL>x_char.append(char_map)<EOL>x_type.append(char_type)<EOL><DEDENT>x_char = np.array(x_char).astype(float)<EOL>x_type = np.array(x_type).astype(float)<EOL>return x_char, x_type<EOL>

Create feature array of character and surrounding characters


n_pad_2 = int((n_pad - <NUM_LIT:1>)/<NUM_LIT:2>)<EOL>for i in range(n_pad_2):<EOL><INDENT>df['<STR_LIT>'.format(i+<NUM_LIT:1>)] = df['<STR_LIT>'].shift(i + <NUM_LIT:1>)<EOL>df['<STR_LIT>'.format(i+<NUM_LIT:1>)] = df['<STR_LIT:type>'].shift(i + <NUM_LIT:1>)<EOL>df['<STR_LIT>'.format(i+<NUM_LIT:1>)] = df['<STR_LIT>'].shift(-i - <NUM_LIT:1>)<EOL>df['<STR_LIT>'.format(i+<NUM_LIT:1>)] = df['<STR_LIT:type>'].shift(-i - <NUM_LIT:1>)<EOL><DEDENT>return df[n_pad_2: -n_pad_2]<EOL>

Given input dataframe, create feature dataframe of shifted characters


config = context.obj<EOL>header('<STR_LIT>')<EOL>article_filename = find_last_article(config['<STR_LIT>'])<EOL>if not article_filename:<EOL><INDENT>return click.secho('<STR_LIT>', fg='<STR_LIT>')<EOL><DEDENT>click.echo(os.path.basename(article_filename))<EOL>header('<STR_LIT>')<EOL>images = list(sorted(find_images(path)))<EOL>if not images:<EOL><INDENT>return click.secho('<STR_LIT>', fg='<STR_LIT>')<EOL><DEDENT>for filename in images:<EOL><INDENT>click.secho(filename, fg='<STR_LIT>')<EOL><DEDENT>if not click.confirm('<STR_LIT>'):<EOL><INDENT>abort(config)<EOL><DEDENT>url_prefix = os.path.join('<STR_LIT>', IMAGES_PATH)<EOL>images_dir = os.path.join(config['<STR_LIT>'], IMAGES_PATH)<EOL>os.makedirs(images_dir, exist_ok=True)<EOL>header('<STR_LIT>')<EOL>urls = []<EOL>for filename in images:<EOL><INDENT>image_basename = os.path.basename(filename).replace('<STR_LIT:U+0020>', '<STR_LIT:->').lower()<EOL>urls.append(os.path.join(url_prefix, image_basename))<EOL>image_filename = os.path.join(images_dir, image_basename)<EOL>print(filename, image_filename)<EOL>import_image(filename, image_filename)<EOL><DEDENT>content = '<STR_LIT:\n>'<EOL>for url in urls:<EOL><INDENT>url = url.replace('<STR_LIT:\\>', '<STR_LIT:/>')<EOL>content += '<STR_LIT>'.format(url)<EOL><DEDENT>header('<STR_LIT>'.format(article_filename))<EOL>with click.open_file(article_filename, '<STR_LIT:a>') as f:<EOL><INDENT>f.write(content)<EOL><DEDENT>click.launch(article_filename)<EOL>


config = context.obj<EOL>header('<STR_LIT>')<EOL>pelican(config, '<STR_LIT>', production=True)<EOL>header('<STR_LIT>')<EOL>unnecessary_paths = [<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>]<EOL>for path in unnecessary_paths:<EOL><INDENT>remove_path(os.path.join(config['<STR_LIT>'], path))<EOL><DEDENT>if os.environ.get('<STR_LIT>'):  <EOL><INDENT>header('<STR_LIT>')<EOL>run(<EOL>'<STR_LIT>' +<EOL>run('<STR_LIT>', capture=True)<EOL>)<EOL>run(<EOL>'<STR_LIT>' +<EOL>run('<STR_LIT>', capture=True)<EOL>)<EOL>github_token = os.environ.get('<STR_LIT>')<EOL>repo_slug = os.environ.get('<STR_LIT>')<EOL>origin = '<STR_LIT>'.format(github_token, repo_slug)<EOL>run('<STR_LIT>' + origin)<EOL><DEDENT>header('<STR_LIT>')<EOL>run('<STR_LIT>'.format(<EOL>message='<STR_LIT>'.format(choose_commit_emoji()),<EOL>dir=config['<STR_LIT>'],<EOL>))<EOL>header('<STR_LIT>')<EOL>run('<STR_LIT>')<EOL>


run('<STR_LIT>')<EOL>run('<STR_LIT>')<EOL>


Looks for errors in source code of your blog


config = context.obj<EOL>pelican(config, '<STR_LIT>', '<STR_LIT>')<EOL>server_proc = None<EOL>os.chdir(config['<STR_LIT>'])<EOL>try:<EOL><INDENT>try:<EOL><INDENT>command = '<STR_LIT>' + str(PORT)<EOL>server_proc = run(command, bg=True)<EOL>time.sleep(<NUM_LIT:3>)<EOL>click.launch('<STR_LIT>')<EOL>time.sleep(<NUM_LIT:5>)<EOL>pelican(config, '<STR_LIT>')<EOL><DEDENT>except Exception:<EOL><INDENT>if server_proc is not None:<EOL><INDENT>server_proc.kill()<EOL><DEDENT>raise<EOL><DEDENT><DEDENT>except KeyboardInterrupt:<EOL><INDENT>abort(context)<EOL><DEDENT>

Opens local preview of your blog website


config = context.obj<EOL>title = click.prompt('<STR_LIT>')<EOL>author = click.prompt('<STR_LIT>', default=config.get('<STR_LIT>'))<EOL>slug = slugify(title)<EOL>creation_date = datetime.now()<EOL>basename = '<STR_LIT>'.format(creation_date, slug)<EOL>meta = (<EOL>('<STR_LIT>', title),<EOL>('<STR_LIT>', '<STR_LIT>'.format(creation_date)),<EOL>('<STR_LIT>', '<STR_LIT>'.format(creation_date)),<EOL>('<STR_LIT>', author),<EOL>)<EOL>file_content = '<STR_LIT>'<EOL>for key, value in meta:<EOL><INDENT>file_content += '<STR_LIT>'.format(key, value)<EOL><DEDENT>file_content += '<STR_LIT>'<EOL>file_content += '<STR_LIT>'<EOL>file_content += '<STR_LIT>'<EOL>file_content += '<STR_LIT>'<EOL>os.makedirs(config['<STR_LIT>'], exist_ok=True)<EOL>path = os.path.join(config['<STR_LIT>'], basename)<EOL>with click.open_file(path, '<STR_LIT:w>') as f:<EOL><INDENT>f.write(file_content)<EOL><DEDENT>click.echo(path)<EOL>click.launch(path)<EOL>


header('<STR_LIT>')<EOL>run('<STR_LIT>')<EOL>header('<STR_LIT>')<EOL>run('<STR_LIT>')<EOL>if not click.confirm('<STR_LIT>'):<EOL><INDENT>run('<STR_LIT>')<EOL>abort(context)<EOL><DEDENT>header('<STR_LIT>')<EOL>try:<EOL><INDENT>run('<STR_LIT>'.format(<EOL>message='<STR_LIT>'.format(choose_commit_emoji())<EOL>), capture=True)<EOL><DEDENT>except subprocess.CalledProcessError as e:<EOL><INDENT>if '<STR_LIT>' not in e.stdout:<EOL><INDENT>raise<EOL><DEDENT>else:<EOL><INDENT>click.echo('<STR_LIT>')<EOL><DEDENT><DEDENT>header('<STR_LIT>')<EOL>branch = get_branch()<EOL>run('<STR_LIT>'.format(branch=branch))<EOL>pr_link = get_pr_link(branch)<EOL>if pr_link:<EOL><INDENT>click.launch(pr_link)<EOL><DEDENT>


<EOL>LEN_TYPE = {<EOL><NUM_LIT:7>: '<STR_LIT>',<EOL><NUM_LIT:4>: '<STR_LIT>',<EOL><NUM_LIT:2>: '<STR_LIT>',<EOL>}<EOL>for ja in orm.JednostkaAdministracyjna.objects.all():<EOL><INDENT>ja.typ = LEN_TYPE[len(ja.id)]<EOL>ja.save()<EOL><DEDENT>


def do_filter(qs, keywords, exclude=False):

and_q = Q()<EOL>for keyword, value in iteritems(keywords):<EOL><INDENT>try:<EOL><INDENT>values = value.split("<STR_LIT:U+002C>")<EOL>if len(values) > <NUM_LIT:0>:<EOL><INDENT>or_q = Q()<EOL>for value in values:<EOL><INDENT>or_q |= Q(**{keyword: value})<EOL><DEDENT>and_q &= or_q<EOL><DEDENT><DEDENT>except AttributeError:<EOL><INDENT>and_q &= Q(**{keyword: value})<EOL><DEDENT><DEDENT>if exclude:<EOL><INDENT>qs = qs.exclude(and_q)<EOL><DEDENT>else:<EOL><INDENT>qs = qs.filter(and_q)<EOL><DEDENT>return qs<EOL>

Filter queryset based on keywords.
Support for multiple-selected parent values.


@never_cache<EOL>def filterchain_all(request, app, model, field, foreign_key_app_name,<EOL>foreign_key_model_name, foreign_key_field_name, value):

model_class = get_model(app, model)<EOL>keywords = get_keywords(field, value)<EOL>foreign_model_class = get_model(foreign_key_app_name, foreign_key_model_name)<EOL>if not any([(isinstance(f, ChainedManyToManyField) or<EOL>isinstance(f, ChainedForeignKey))<EOL>for f in foreign_model_class._meta.get_fields()]):<EOL><INDENT>raise PermissionDenied("<STR_LIT>")<EOL><DEDENT>limit_choices_to = get_limit_choices_to(foreign_key_app_name, foreign_key_model_name, foreign_key_field_name)<EOL>queryset = get_queryset(model_class, limit_choices_to=limit_choices_to)<EOL>filtered = list(do_filter(queryset, keywords))<EOL>if not getattr(model_class._meta, '<STR_LIT>', False):<EOL><INDENT>sort_results(list(filtered))<EOL><DEDENT>excluded = list(do_filter(queryset, keywords, exclude=True))<EOL>if not getattr(model_class._meta, '<STR_LIT>', False):<EOL><INDENT>sort_results(list(excluded))<EOL><DEDENT>empty_choice = {'<STR_LIT:value>': "<STR_LIT>", '<STR_LIT>': "<STR_LIT>"}<EOL>serialized_results = (<EOL>serialize_results(filtered) +<EOL>[empty_choice] +<EOL>serialize_results(excluded)<EOL>)<EOL>return JsonResponse(serialized_results, safe=False)<EOL>

Returns filtered results followed by excluded results below.


media = super(JqueryMediaMixin, self).media<EOL>js = []<EOL>if JQUERY_URL:<EOL><INDENT>js.append(JQUERY_URL)<EOL><DEDENT>elif JQUERY_URL is not False:<EOL><INDENT>vendor = '<STR_LIT>' if django.VERSION < (<NUM_LIT:1>, <NUM_LIT:9>, <NUM_LIT:0>) else '<STR_LIT>'<EOL>extra = '<STR_LIT>' if settings.DEBUG else '<STR_LIT>'<EOL>jquery_paths = [<EOL>'<STR_LIT>'.format(vendor, extra),<EOL>'<STR_LIT>',<EOL>]<EOL>if USE_DJANGO_JQUERY:<EOL><INDENT>jquery_paths = ['<STR_LIT>'.format(path) for path in jquery_paths]<EOL><DEDENT>js.extend(jquery_paths)<EOL><DEDENT>media += Media(js=js)<EOL>return media<EOL>

Media defined as a dynamic property instead of an inner class.


media = super(ChainedSelect, self).media<EOL>js = ['<STR_LIT>',<EOL>'<STR_LIT>']<EOL>media += Media(js=js)<EOL>return media<EOL>


def _get_available_choices(self, queryset, value):

item = queryset.filter(pk=value).first()<EOL>if item:<EOL><INDENT>try:<EOL><INDENT>pk = getattr(item, self.chained_model_field + "<STR_LIT>")<EOL>filter = {self.chained_model_field: pk}<EOL><DEDENT>except AttributeError:<EOL><INDENT>try:  <EOL><INDENT>pks = getattr(item, self.chained_model_field).all().values_list('<STR_LIT>', flat=True)<EOL>filter = {self.chained_model_field + "<STR_LIT>": pks}<EOL><DEDENT>except AttributeError:<EOL><INDENT>try:  <EOL><INDENT>pks = getattr(item, self.chained_model_field + "<STR_LIT>").all().values_list('<STR_LIT>', flat=True)<EOL>filter = {self.chained_model_field + "<STR_LIT>": pks}<EOL><DEDENT>except AttributeError:  <EOL><INDENT>filter = {}<EOL><DEDENT><DEDENT><DEDENT>filtered = list(get_model(self.to_app_name, self.to_model_name).objects.filter(**filter).distinct())<EOL>if self.sort:<EOL><INDENT>sort_results(filtered)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>filtered = []<EOL><DEDENT>return filtered<EOL>


media = super(ChainedSelectMultiple, self).media<EOL>js = ['<STR_LIT>',<EOL>'<STR_LIT>']<EOL>if self.horizontal:<EOL><INDENT>js.extend(["<STR_LIT>",<EOL>"<STR_LIT>",<EOL>"<STR_LIT>"])<EOL><DEDENT>media += Media(js=js)<EOL>return media<EOL>


<EOL>key1 = input.lower()<EOL>key1 = key1.replace(u"<STR_LIT>", u"<STR_LIT:a>")<EOL>key1 = key1.replace(u"<STR_LIT>", u"<STR_LIT:o>")<EOL>key1 = key1.replace(u"<STR_LIT>", u"<STR_LIT:u>")<EOL>key1 = key1.replace(u"<STR_LIT>", u"<STR_LIT>")<EOL>return key1<EOL>

This function implements sort keys for the german language according to
    DIN 5007.


results.sort(key=lambda x: unicode_sorter(force_text(x)))<EOL>

Performs in-place sort of filterchain results.


def __init__(self, to, chained_field=None, chained_model_field=None,<EOL>auto_choose=False, horizontal=False, **kwargs):

self.chain_field = chained_field<EOL>self.chained_model_field = chained_model_field<EOL>self.auto_choose = auto_choose<EOL>self.horizontal = horizontal<EOL>self.verbose_name = kwargs.get('<STR_LIT>', '<STR_LIT>')<EOL>super(ChainedManyToManyField, self).__init__(to, **kwargs)<EOL>

examples:

class Publication(models.Model):
    name = models.CharField(max_length=255)

class Writer(models.Model):
    name = models.CharField(max_length=255)
    publications = models.ManyToManyField('Publication', blank=True, null=True)

class Book(models.Model):
    publication = models.ForeignKey(Publication)
    writer = ChainedManyToManyField(
        Writer,
        chained_field="publication",
        chained_model_field="publications",
        )
    name = models.CharField(max_length=255)

``chained_field`` is the name of the ForeignKey field referenced by ChainedManyToManyField of the same Model.
in the examples, chained_field is the name of field publication in Model Book.

``chained_model_field`` is the name of the ManyToMany field referenced in the 'to' Model.
in the examples, chained_model_field is the name of field publications in Model Writer.

``auto_choose`` controls whether auto select the choice when there is only one available choice.


def __init__(self, to, chained_field=None, chained_model_field=None,<EOL>show_all=False, auto_choose=False, sort=True, view_name=None, **kwargs):

self.chained_field = chained_field<EOL>self.chained_model_field = chained_model_field<EOL>self.show_all = show_all<EOL>self.auto_choose = auto_choose<EOL>self.sort = sort<EOL>self.view_name = view_name<EOL>if kwargs:<EOL><INDENT>kwargs['<STR_LIT>'] = kwargs.get('<STR_LIT>', models.CASCADE)<EOL><DEDENT>else:<EOL><INDENT>kwargs = {'<STR_LIT>': models.CASCADE}<EOL><DEDENT>super(ChainedForeignKey, self).__init__(to, **kwargs)<EOL>

examples:

class Continent(models.Model):
    name = models.CharField(max_length=255)

class Country(models.Model):
    continent = models.ForeignKey(Continent)

class Location(models.Model):
    continent = models.ForeignKey(Continent)
    country = ChainedForeignKey(
        Country,
        chained_field="continent",
        chained_model_field="continent",
        show_all=True,
        auto_choose=True,
        sort=True,
        # limit_choices_to={'name':'test'}
    )
``chained_field`` is the name of the ForeignKey field referenced by ChainedForeignKey of the same Model.
in the examples, chained_field is the name of field continent in Model Location.

``chained_model_field`` is the name of the ForeignKey field referenced in the 'to' Model.
in the examples, chained_model_field is the name of field continent in Model Country.

``show_all`` controls whether show other choices below the filtered choices, with separater '----------'.

``auto_choose`` controls whether auto select the choice when there is only one available choice.

``sort`` controls whether or not to sort results lexicographically or not.

``view_name`` controls which view to use, 'chained_filter' or 'chained_filter_all'.


print("<STR_LIT>".format(<EOL>result.headerIndex,<EOL>result.index,<EOL>opendnp3.CommandPointStateToString(result.state),<EOL>opendnp3.CommandStatusToString(result.status)<EOL>))<EOL>


print("<STR_LIT>".format(opendnp3.TaskCompletionToString(result.summary)))<EOL>result.ForeachItem(collection_callback)<EOL>


self.iin_field = dict(<EOL>LSB=iin.LSB,<EOL>MSB=iin.MSB<EOL>)<EOL>

Called when a response or unsolicited response is receive from the outstation.


self.task_id = dict(<EOL>id=id.GetId(),<EOL>defined=id.IsDefined()<EOL>)<EOL>


self.task_info = dict(<EOL>type=info.type,<EOL>result=info.result<EOL>)<EOL>


self.link_status = value<EOL>

Called when a the reset/unreset status of the link layer changes.


pass<EOL>

Returns a UTCTimestamp of the current time.


self.state = state<EOL>


if entry.loggerid == "<STR_LIT>":<EOL><INDENT>self.tcp_client = True<EOL><DEDENT>


logger = asiodnp3.ConsoleLogger().Create()<EOL>manager = asiodnp3.DNP3Manager(<NUM_LIT:1>, asiodnp3.ConsoleLogger().Create())<EOL>channel = manager.AddTCPClient("<STR_LIT>",<EOL>FILTERS,<EOL>asiopal.ChannelRetry(),<EOL>HOST,<EOL>LOCAL,<EOL>PORT,<EOL>asiodnp3.PrintingChannelListener().Create())<EOL>stack_config = asiodnp3.MasterStackConfig()<EOL>stack_config.master.responseTimeout = openpal.TimeDuration().Seconds(<NUM_LIT:2>)<EOL>stack_config.link.RemoteAddr = <NUM_LIT:10><EOL>soe_handler = asiodnp3.PrintingSOEHandler().Create()<EOL>default_master_app = asiodnp3.DefaultMasterApplication().Create()<EOL>master = channel.AddMaster("<STR_LIT>",<EOL>soe_handler,<EOL>default_master_app,<EOL>stack_config)<EOL>master.Enable()<EOL>time.sleep(<NUM_LIT:2>)<EOL>if not hang:<EOL><INDENT>del channel<EOL>del master<EOL><DEDENT>print("<STR_LIT>".format(hang))<EOL>manager.Shutdown()<EOL>

Demonstrate hanging when channel and master are not deleted prior to manager.Shutdown()


self.link_status = value<EOL>


if entry.loggerid == "<STR_LIT>":<EOL><INDENT>self.server = True<EOL><DEDENT>


print('<STR_LIT>')<EOL>self.do_menu('<STR_LIT>')<EOL>self.cmdloop('<STR_LIT>')<EOL>exit()<EOL>

Display the command-line interface's menu and issue a prompt.


Display a menu of command-line options. Command syntax is: menu


self.application.channel.SetLogFilters(openpal.LogFilters(opendnp3.levels.ALL_COMMS))<EOL>print('<STR_LIT>'.format(opendnp3.levels.ALL_COMMS))<EOL>

Set the channel log level to ALL_COMMS. Command syntax is: chan_log_all


self.application.channel.SetLogFilters(openpal.LogFilters(opendnp3.levels.NORMAL))<EOL>print('<STR_LIT>'.format(opendnp3.levels.NORMAL))<EOL>

Set the channel log level to NORMAL. Command syntax is: chan_log_normal


Perform the function DISABLE_UNSOLICITED. Command syntax is: disable_unsol


self.application.master.SetLogFilters(openpal.LogFilters(opendnp3.levels.ALL_COMMS))<EOL>_log.debug('<STR_LIT>'.format(opendnp3.levels.ALL_COMMS))<EOL>

Set the master log level to ALL_COMMS. Command syntax is: mast_log_all


self.application.master.SetLogFilters(openpal.LogFilters(opendnp3.levels.NORMAL))<EOL>_log.debug('<STR_LIT>'.format(opendnp3.levels.NORMAL))<EOL>

Set the master log level to NORMAL. Command syntax is: mast_log_normal


self.application.send_direct_operate_command(opendnp3.ControlRelayOutputBlock(opendnp3.ControlCode.LATCH_ON),<EOL><NUM_LIT:5>,<EOL>command_callback)<EOL>

Send a DirectOperate BinaryOutput (group 12) index 5 LATCH_ON to the Outstation. Command syntax is: o1


self.application.send_direct_operate_command(opendnp3.AnalogOutputInt32(<NUM_LIT:7>),<EOL><NUM_LIT:10>,<EOL>command_callback)<EOL>

Send a DirectOperate AnalogOutput (group 41) index 10 value 7 to the Outstation. Command syntax is: o2


Send a DirectOperate BinaryOutput (group 12) CommandSet to the Outstation. Command syntax is: o3


self.application.master.Restart(opendnp3.RestartType.COLD, restart_callback)<EOL>

Request that the Outstation perform a cold restart. Command syntax is: restart


self.application.send_select_and_operate_command(opendnp3.ControlRelayOutputBlock(opendnp3.ControlCode.LATCH_ON),<EOL><NUM_LIT:8>,<EOL>command_callback)<EOL>

Send a SelectAndOperate BinaryOutput (group 12) index 8 LATCH_ON to the Outstation. Command syntax is: s1


Send a SelectAndOperate BinaryOutput (group 12) CommandSet to the Outstation. Command syntax is: s2


self.application.master.ScanAllObjects(opendnp3.GroupVariationID(<NUM_LIT:2>, <NUM_LIT:1>), opendnp3.TaskConfig().Default())<EOL>

Call ScanAllObjects. Command syntax is: scan_all


self.application.fast_scan.Demand()<EOL>

Demand an immediate fast scan. Command syntax is: scan_fast


self.application.master.ScanRange(opendnp3.GroupVariationID(<NUM_LIT:1>, <NUM_LIT:2>), <NUM_LIT:0>, <NUM_LIT:3>, opendnp3.TaskConfig().Default())<EOL>

Do an ad-hoc scan of a range of points (group 1, variation 2, indexes 0-3). Command syntax is: scan_range


self.application.slow_scan.Demand()<EOL>

Demand an immediate slow scan. Command syntax is: scan_slow


millis_since_epoch = int((datetime.now() - datetime.utcfromtimestamp(<NUM_LIT:0>)).total_seconds() * <NUM_LIT>)<EOL>self.application.master.Write(opendnp3.TimeAndInterval(opendnp3.DNPTime(millis_since_epoch),<EOL><NUM_LIT:100>,<EOL>opendnp3.IntervalUnits.Seconds),<EOL><NUM_LIT:0>,                            <EOL>opendnp3.TaskConfig().Default())<EOL>

Write a TimeAndInterval to the Outstation. Command syntax is: write_time


self.application.shutdown()<EOL>exit()<EOL>

Quit the command-line interface. Command syntax is: quit


print("<STR_LIT>".format(<EOL>result.headerIndex,<EOL>result.index,<EOL>opendnp3.CommandPointStateToString(result.state),<EOL>opendnp3.CommandStatusToString(result.status)<EOL>))<EOL>


print("<STR_LIT>".format(opendnp3.TaskCompletionToString(result.summary)))<EOL>result.ForeachItem(collection_callback)<EOL>


The Master has been started from the command line. Execute ad-hoc tests if desired.


def send_direct_operate_command(self, command, index, callback=asiodnp3.PrintingCommandCallback.Get(),<EOL>config=opendnp3.TaskConfig().Default()):

self.master.DirectOperate(command, index, callback, config)<EOL>

Direct operate a single command

:param command: command to operate
:param index: index of the command
:param callback: callback that will be invoked upon completion or failure
:param config: optional configuration that controls normal callbacks and allows the user to be specified for SA


self.master.DirectOperate(command_set, callback, config)<EOL>

Direct operate a set of commands

:param command_set: set of command headers
:param callback: callback that will be invoked upon completion or failure
:param config: optional configuration that controls normal callbacks and allows the user to be specified for SA


def send_select_and_operate_command(self, command, index, callback=asiodnp3.PrintingCommandCallback.Get(),<EOL>config=opendnp3.TaskConfig().Default()):

self.master.SelectAndOperate(command, index, callback, config)<EOL>

Select and operate a single command

:param command: command to operate
:param index: index of the command
:param callback: callback that will be invoked upon completion or failure
:param config: optional configuration that controls normal callbacks and allows the user to be specified for SA


self.master.SelectAndOperate(command_set, callback, config)<EOL>

Select and operate a set of commands

:param command_set: set of command headers
:param callback: callback that will be invoked upon completion or failure
:param config: optional configuration that controls normal callbacks and allows the user to be specified for SA


visitor_class_types = {<EOL>opendnp3.ICollectionIndexedBinary: VisitorIndexedBinary,<EOL>opendnp3.ICollectionIndexedDoubleBitBinary: VisitorIndexedDoubleBitBinary,<EOL>opendnp3.ICollectionIndexedCounter: VisitorIndexedCounter,<EOL>opendnp3.ICollectionIndexedFrozenCounter: VisitorIndexedFrozenCounter,<EOL>opendnp3.ICollectionIndexedAnalog: VisitorIndexedAnalog,<EOL>opendnp3.ICollectionIndexedBinaryOutputStatus: VisitorIndexedBinaryOutputStatus,<EOL>opendnp3.ICollectionIndexedAnalogOutputStatus: VisitorIndexedAnalogOutputStatus,<EOL>opendnp3.ICollectionIndexedTimeAndInterval: VisitorIndexedTimeAndInterval<EOL>}<EOL>visitor_class = visitor_class_types[type(values)]<EOL>visitor = visitor_class()<EOL>values.Foreach(visitor)<EOL>for index, value in visitor.index_and_value:<EOL><INDENT>log_string = '<STR_LIT>'<EOL>_log.debug(log_string.format(info.gv, info.headerIndex, type(values).__name__, index, value))<EOL><DEDENT>

Process measurement data.

:param info: HeaderInfo
:param values: A collection of values received from the Outstation (various data types are possible).


app = OutstationApplication()<EOL>_log.debug('<STR_LIT>')<EOL>app.shutdown()<EOL>_log.debug('<STR_LIT>')<EOL>exit()<EOL>

The Outstation has been started from the command line. Execute ad-hoc tests if desired.


stack_config = asiodnp3.OutstationStackConfig(opendnp3.DatabaseSizes.AllTypes(<NUM_LIT:10>))<EOL>stack_config.outstation.eventBufferConfig = opendnp3.EventBufferConfig().AllTypes(<NUM_LIT:10>)<EOL>stack_config.outstation.params.allowUnsolicited = True<EOL>stack_config.link.LocalAddr = <NUM_LIT:10><EOL>stack_config.link.RemoteAddr = <NUM_LIT:1><EOL>stack_config.link.KeepAliveTimeout = openpal.TimeDuration().Max()<EOL>return stack_config<EOL>


db_config.analog[<NUM_LIT:1>].clazz = opendnp3.PointClass.Class2<EOL>db_config.analog[<NUM_LIT:1>].svariation = opendnp3.StaticAnalogVariation.Group30Var1<EOL>db_config.analog[<NUM_LIT:1>].evariation = opendnp3.EventAnalogVariation.Group32Var7<EOL>db_config.analog[<NUM_LIT:2>].clazz = opendnp3.PointClass.Class2<EOL>db_config.analog[<NUM_LIT:2>].svariation = opendnp3.StaticAnalogVariation.Group30Var1<EOL>db_config.analog[<NUM_LIT:2>].evariation = opendnp3.EventAnalogVariation.Group32Var7<EOL>db_config.binary[<NUM_LIT:1>].clazz = opendnp3.PointClass.Class2<EOL>db_config.binary[<NUM_LIT:1>].svariation = opendnp3.StaticBinaryVariation.Group1Var2<EOL>db_config.binary[<NUM_LIT:1>].evariation = opendnp3.EventBinaryVariation.Group2Var2<EOL>db_config.binary[<NUM_LIT:2>].clazz = opendnp3.PointClass.Class2<EOL>db_config.binary[<NUM_LIT:2>].svariation = opendnp3.StaticBinaryVariation.Group1Var2<EOL>db_config.binary[<NUM_LIT:2>].evariation = opendnp3.EventBinaryVariation.Group2Var2<EOL>

Configure the Outstation's database of input point definitions.

Configure two Analog points (group/variation 30.1) at indexes 1 and 2.
Configure two Binary points (group/variation 1.2) at indexes 1 and 2.


<EOL>self.manager.Shutdown()<EOL>

Execute an orderly shutdown of the Outstation.

The debug messages may be helpful if errors occur during shutdown.


return cls.outstation<EOL>

Get the singleton instance of IOutstation.


cls.outstation = outstn<EOL>

Set the singleton instance of IOutstation, as returned from the channel's AddOutstation call.

Making IOutstation available as a singleton allows other classes (e.g. the command-line UI)
to send commands to it -- see apply_update().


_log.debug('<STR_LIT>')<EOL>return opendnp3.RestartMode.UNSUPPORTED<EOL>

Return a RestartMode enumerated value indicating whether cold restart is supported.


application_iin = opendnp3.ApplicationIIN()<EOL>application_iin.configCorrupt = False<EOL>application_iin.deviceTrouble = False<EOL>application_iin.localControl = False<EOL>application_iin.needTime = False<EOL>iin_field = application_iin.ToIIN()<EOL>_log.debug('<STR_LIT>'.format(iin_field.LSB,<EOL>iin_field.MSB))<EOL>return application_iin<EOL>

Return the application-controlled IIN field.


Return a RestartMode enumerated value indicating whether a warm restart is supported.


@classmethod<EOL><INDENT>def process_point_value(cls, command_type, command, index, op_type):<DEDENT>

_log.debug('<STR_LIT>'.format(index, command))<EOL>

A PointValue was received from the Master. Process its payload.

:param command_type: (string) Either 'Select' or 'Operate'.
:param command: A ControlRelayOutputBlock or else a wrapped data value (AnalogOutputInt16, etc.).
:param index: (integer) DNP3 index of the payload's data definition.
:param op_type: An OperateType, or None if command_type == 'Select'.


_log.debug('<STR_LIT>'.format(type(value).__name__, index, value.value))<EOL>builder = asiodnp3.UpdateBuilder()<EOL>builder.Update(value, index)<EOL>update = builder.Build()<EOL>OutstationApplication.get_outstation().Apply(update)<EOL>

Record an opendnp3 data value (Analog, Binary, etc.) in the outstation's database.

    The data value gets sent to the Master as a side-effect.

:param value: An instance of Analog, Binary, or another opendnp3 data value.
:param index: (integer) Index of the data definition in the opendnp3 database.


OutstationApplication.process_point_value('<STR_LIT>', command, index, None)<EOL>return opendnp3.CommandStatus.SUCCESS<EOL>

The Master sent a Select command to the Outstation. Handle it.

:param command: ControlRelayOutputBlock,
                AnalogOutputInt16, AnalogOutputInt32, AnalogOutputFloat32, or AnalogOutputDouble64.
:param index: int
:return: CommandStatus


def Operate(self, command, index, op_type):

OutstationApplication.process_point_value('<STR_LIT>', command, index, op_type)<EOL>return opendnp3.CommandStatus.SUCCESS<EOL>

The Master sent an Operate command to the Outstation. Handle it.

:param command: ControlRelayOutputBlock,
                AnalogOutputInt16, AnalogOutputInt32, AnalogOutputFloat32, or AnalogOutputDouble64.
:param index: int
:param op_type: OperateType
:return: CommandStatus


print('<STR_LIT>')<EOL>self.do_menu('<STR_LIT>')<EOL>self.cmdloop('<STR_LIT>')<EOL>exit()<EOL>

Display the command-line interface's menu and issue a prompt.


index, value_string = self.index_and_value_from_line(line)<EOL>if index and value_string:<EOL><INDENT>try:<EOL><INDENT>self.application.apply_update(opendnp3.Analog(float(value_string)), index)<EOL><DEDENT>except ValueError:<EOL><INDENT>print('<STR_LIT>')<EOL><DEDENT><DEDENT>

Send the Master an AnalogInput (group 32) value. Command syntax is: a index value


self.application.apply_update(opendnp3.Analog(<NUM_LIT:2>), index=<NUM_LIT:4>)<EOL>

Send the Master an AnalogInput (group 32) value of 2 at index 4. Command syntax is: a2


index, value_string = self.index_and_value_from_line(line)<EOL>if index and value_string:<EOL><INDENT>if value_string.lower() == '<STR_LIT:true>' or value_string.lower() == '<STR_LIT:false>':<EOL><INDENT>self.application.apply_update(opendnp3.Binary(value_string == '<STR_LIT:true>'), index)<EOL><DEDENT>else:<EOL><INDENT>print('<STR_LIT>')<EOL><DEDENT><DEDENT>

Send the Master a BinaryInput (group 2) value. Command syntax is: 'b index true' or 'b index false


self.application.apply_update(opendnp3.Binary(False), index=<NUM_LIT:6>)<EOL>

Send the Master a BinaryInput (group 2) value of False at index 6. Command syntax is: b0


index, value_string = self.index_and_value_from_line(line)<EOL>if index and value_string:<EOL><INDENT>try:<EOL><INDENT>self.application.apply_update(opendnp3.Counter(int(value_string)), index)<EOL><DEDENT>except ValueError:<EOL><INDENT>print('<STR_LIT>')<EOL><DEDENT><DEDENT>

Send the Master a Counter (group 22) value. Command syntax is: c index value


index = self.index_from_line(line)<EOL>if index:<EOL><INDENT>self.application.apply_update(opendnp3.DoubleBitBinary(opendnp3.DoubleBit.DETERMINED_ON), index)<EOL><DEDENT>

Send the Master a DoubleBitBinaryInput (group 4) value of DETERMINED_ON. Command syntax is: d index


Display a menu of command-line options. Command syntax is: menu


self.application.shutdown()<EOL>exit()<EOL>

Quit the command line interface. Command syntax is: quit


try:<EOL><INDENT>index = int(line.split('<STR_LIT:U+0020>')[<NUM_LIT:0>])<EOL><DEDENT>except (ValueError, IndexError):<EOL><INDENT>print('<STR_LIT>')<EOL>index = None<EOL><DEDENT>try:<EOL><INDENT>value_string = line.split('<STR_LIT:U+0020>')[<NUM_LIT:1>]<EOL><DEDENT>except (ValueError, IndexError):<EOL><INDENT>print('<STR_LIT>')<EOL>value_string = None<EOL><DEDENT>return index, value_string<EOL>

Parse an index (integer) and value (string) from command line args and return them.


try:<EOL><INDENT>index = int(line.split('<STR_LIT:U+0020>')[<NUM_LIT:0>])<EOL><DEDENT>except (ValueError, IndexError):<EOL><INDENT>print('<STR_LIT>')<EOL>index = None<EOL><DEDENT>return index<EOL>

Parse an index (integer) from command line args and return it.


def __init__(self, routing=None, default=None, errors='<STR_LIT>',<EOL>python_path='<STR_LIT>', verbose=False,<EOL>pre_routing=None):

super().__init__()<EOL>self.verbose = verbose<EOL>self.receive = LogErrors(self._receive, errors)<EOL>default = dict(self.DEFAULT, **(default or {}))<EOL>self.pre_routing = ActionList(pre_routing)<EOL>self.routing = Routing(routing or {}, default or {}, python_path)<EOL>

:param Address pre_routing: This Address is set with with the message
    after the message is received and converted, but before it is
    routed.
:param errors: either a number, indicating how many errors to report
   before ignoring them, or one of these strings:
   'raise', meaning to raise an exception
   'ignore', meaning to ignore all errors
   'report', meaning to report all errors


msg = self._convert(msg)<EOL>if msg is None:<EOL><INDENT>return<EOL><DEDENT>str_msg = self.verbose and self._msg_to_str(msg)<EOL>if self.verbose and log.is_debug():<EOL><INDENT>log.debug('<STR_LIT>', str_msg)<EOL><DEDENT>if self.pre_routing:<EOL><INDENT>self.pre_routing.receive(msg)<EOL><DEDENT>receiver, msg = self.routing.receive(msg)<EOL>if receiver:<EOL><INDENT>receiver.receive(msg)<EOL>if self.verbose:<EOL><INDENT>log.info('<STR_LIT>', str_msg[:<NUM_LIT>], msg,<EOL>repr(receiver))<EOL><DEDENT><DEDENT>

Receive a message from the input source and perhaps raise an Exception.


raise NotImplementedError<EOL>

Convert the message to a Control-specific format


pass<EOL>

Returns a new thread to run the loop for this control source.


raise NotImplementedError<EOL>

Should yield a sequence of messages from the input source.


def __init__(self, routing, default, python_path):

def make(x):<EOL><INDENT>if isinstance(x, (list, str)):<EOL><INDENT>return ActionList(x)<EOL><DEDENT>assert isinstance(x, dict)<EOL>if '<STR_LIT>' in x or '<STR_LIT>' in x:<EOL><INDENT>x = dict(default, **x)<EOL>return construct.construct_type(x, python_path)<EOL><DEDENT>return {k: make(v) for k, v in x.items()}<EOL><DEDENT>routing = flatten.unflatten(routing)<EOL>self.routing = make(routing)<EOL>

:param dict routing: `routing` is a dict that maps addresses
   to lists of actions.

   The values in the input dictionary `routing` are recursively visited
   to build the routing table:

   * values that are strings or lists are used to construct ActionLists
   * dictionaries that contain "typename" or "datatype" keys are
     used to construct a class of that type.
   * otherwise, dictionaries are visited recursively
   * all other types are forbidden


def visit(x):<EOL><INDENT>set_project = getattr(x, '<STR_LIT>', None)<EOL>if set_project:<EOL><INDENT>set_project(project)<EOL><DEDENT>values = getattr(x, '<STR_LIT>', lambda: ())<EOL>for v in values():<EOL><INDENT>visit(v)<EOL><DEDENT><DEDENT>visit(self.routing)<EOL>


x = self.routing<EOL>while not isinstance(x, ActionList):<EOL><INDENT>if not x or not msg:<EOL><INDENT>return None, msg<EOL><DEDENT>if not isinstance(x, dict):<EOL><INDENT>raise ValueError('<STR_LIT>' % type(x))<EOL><DEDENT>_, value = msg.popitem(last=False)<EOL>x = x.get(str(value))<EOL><DEDENT>return x, msg<EOL>

Returns a (receiver, msg) pair, where receiver is `None` if no route for
the message was found, or otherwise an object with a `receive` method
that can accept that `msg`.


@functools.wraps(method)<EOL>def single(self, address, value=None):<EOL><INDENT>address = urllib.parse.unquote_plus(address)<EOL>try:<EOL><INDENT>error = NO_PROJECT_ERROR<EOL>if not self.project:<EOL><INDENT>raise ValueError<EOL><DEDENT>error = BAD_ADDRESS_ERROR<EOL>ed = editor.Editor(address, self.project)<EOL>if value is None:<EOL><INDENT>error = BAD_GETTER_ERROR<EOL>result = method(self, ed)<EOL><DEDENT>else:<EOL><INDENT>error = BAD_SETTER_ERROR<EOL>result = method(self, ed, value)<EOL><DEDENT>result = {'<STR_LIT:value>': result}<EOL><DEDENT>except Exception as e:<EOL><INDENT>traceback.print_exc()<EOL>msg = '<STR_LIT>' % (error.format(**locals()), e)<EOL>result = {'<STR_LIT:error>': msg}<EOL><DEDENT>return flask.jsonify(result)<EOL><DEDENT>return single<EOL>

Decorator for RestServer methods that take a single address


@functools.wraps(method)<EOL>def multi(self, address='<STR_LIT>'):<EOL><INDENT>values = flask.request.values<EOL>address = urllib.parse.unquote_plus(address)<EOL>if address and values and not address.endswith('<STR_LIT:.>'):<EOL><INDENT>address += '<STR_LIT:.>'<EOL><DEDENT>result = {}<EOL>for a in values or '<STR_LIT>':<EOL><INDENT>try:<EOL><INDENT>if not self.project:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>ed = editor.Editor(address + a, self.project)<EOL>result[address + a] = {'<STR_LIT:value>': method(self, ed, a)}<EOL><DEDENT>except:<EOL><INDENT>if self.project:<EOL><INDENT>traceback.print_exc()<EOL><DEDENT>result[address + a] = {'<STR_LIT:error>': '<STR_LIT>' % a}<EOL><DEDENT><DEDENT>return flask.jsonify(result)<EOL><DEDENT>return multi<EOL>

Decorator for RestServer methods that take multiple addresses


def __init__(self, segments=(), period=None, **kwds):

self.segments = _segments.Segments(segments)<EOL>period = period or self.segments.total_time<EOL>super().__init__(period=period, **kwds)<EOL>

If period is None, then it is set to be the length of the
segments.

Each segment is either a single number, indicating a level, or a pair of
numbers, indicating a level and a time.  Levels without a time are
assigned the average delay.


def __init__(self, use_note_off=True, **kwds):

super().__init__(**kwds)<EOL>self.use_note_off = use_note_off<EOL>

:param use_note_off:
    If False, map note_offs to note_ons with velocity 0
    If True, map note_ons with velocity 0 to note_offs
    If None, do not change none_ons or note_offs


def __init__(self, omit=None, normalizers=None, keys_by_type=None,<EOL>accept=None, reject=None, auto_omit=True):

def to_set(x):<EOL><INDENT>if x is None:<EOL><INDENT>return set()<EOL><DEDENT>if isinstance(x, (list, tuple)):<EOL><INDENT>return set(x)<EOL><DEDENT>return set([x])<EOL><DEDENT>def make_match(m):<EOL><INDENT>return m and {k: to_set(v) for k, v in m.items()}<EOL><DEDENT>self.accept, self.reject = make_match(accept), make_match(reject)<EOL>self.omit = to_set(omit)<EOL>if auto_omit and self.accept:<EOL><INDENT>self.omit.update(k for k, v in self.accept.items() if len(v) == <NUM_LIT:1>)<EOL><DEDENT>self.normalizers = normalizers or {}<EOL>if keys_by_type is None:<EOL><INDENT>self.keys_by_type = None<EOL><DEDENT>else:<EOL><INDENT>self.keys_by_type = {}<EOL>for k, v in keys_by_type.items():<EOL><INDENT>if isinstance(v, str):<EOL><INDENT>v = [v]<EOL><DEDENT>self.keys_by_type[k] = tuple(i for i in v if i not in self.omit)<EOL><DEDENT><DEDENT>

Arguments

omit -- A list of keys that will not be extracted.

normalizers -- Some keys also need to be "normalized" -
    scaled and offset so they are between 0 and 1, or -1 and 1.
    The `normalizers` table maps key names to a function that
    normalizes the value of that key.

keys_by_type -- `keys_by_type` is a dictionary from the `type` in an
    incoming message to a list of message keys to be extracted

accept -- maps keys to a value or a list of values that are
    accepted for that key.  A message has to match *all* entries in
    `accept` to be accepted.

reject -- map key to a value or a list of values that are not
    accepted for that key.  A message is rejected if it matches *any*
    entry in the reject map.

auto_omit -- if True, omit all keys in `accept` that only have one
    possible value.

    auto_omit=True, the default, is probably more useful: if you
    request data for channel=1, type=note_on then you probably don't
    want to see channel=1, type=note_on with each message.


def normal(key):<EOL><INDENT>v = msg.get(key)<EOL>if v is None:<EOL><INDENT>return v<EOL><DEDENT>normalizer = self.normalizers.get(key, lambda x: x)<EOL>return normalizer(v)<EOL><DEDENT>def odict(keys):<EOL><INDENT>return collections.OrderedDict((k, normal(k)) for k in keys)<EOL><DEDENT>def match(m):<EOL><INDENT>return (msg.get(k) in v for k, v in m.items()) if m else ()<EOL><DEDENT>accept = all(match(self.accept))<EOL>reject = any(match(self.reject))<EOL>if reject or not accept:<EOL><INDENT>keys = ()<EOL><DEDENT>elif self.keys_by_type is None:<EOL><INDENT>keys = [k for k in msg.keys() if k not in self.omit]<EOL><DEDENT>else:<EOL><INDENT>keys = self.keys_by_type.get(msg.get('<STR_LIT:type>'))<EOL><DEDENT>return odict(keys)<EOL>

Yield an ordered dictionary if msg['type'] is in keys_by_type.


if self.edit_queue:<EOL><INDENT>self.edit_queue.put_edit(self._set, msg)<EOL><DEDENT>else:<EOL><INDENT>self._set(msg)<EOL><DEDENT>

Receives a message, and either sets it immediately, or puts it on the
edit queue if there is one.


if threaded is not None:<EOL><INDENT>self.threaded = threaded<EOL><DEDENT>run = {'<STR_LIT>': {'<STR_LIT>': False}}<EOL>self.project = project.project(<EOL>self.desc, run, root_file=self.project_file)<EOL>self._run = self.project.run<EOL>self._runner.start(self.threaded)<EOL>


if not self:<EOL><INDENT>instance = getattr(Runner.instance(), '<STR_LIT>', None)<EOL>self = instance and instance()<EOL>if not self:<EOL><INDENT>return<EOL><DEDENT><DEDENT>self._runner.stop()<EOL>if self.project:<EOL><INDENT>self.project.stop()<EOL>self.project = None<EOL><DEDENT>


self.stop()<EOL>super().clear()<EOL>

Stop the project if it's running and clear the project description


return self._runner.is_running<EOL>

True if the Builder is currently running


return self.desc.run.get('<STR_LIT>', False)<EOL>

True if the Builder is runs in a separate thread, false if the
Builder blocks, waiting for the animation to end.


simpixel_driver.open_browser(new=new, autoraise=autoraise)<EOL>

Open an instance of simpixel in the browser


animations.run(None)<EOL>


old_value = desc.get(name)<EOL>if old_value is None:<EOL><INDENT>raise KeyError('<STR_LIT>' % name)<EOL><DEDENT>if value is None:<EOL><INDENT>value = type(old_value)()<EOL><DEDENT>elif name in CLASS_SECTIONS:<EOL><INDENT>if isinstance(value, str):<EOL><INDENT>value = {'<STR_LIT>': aliases.resolve(value)}<EOL><DEDENT>elif isinstance(value, type):<EOL><INDENT>value = {'<STR_LIT>': class_name.class_name(value)}<EOL><DEDENT>elif not isinstance(value, dict):<EOL><INDENT>raise TypeError('<STR_LIT>' % value)<EOL><DEDENT>typename = value.get('<STR_LIT>')<EOL>if typename:<EOL><INDENT>s = '<STR_LIT:s>' if name == '<STR_LIT>' else '<STR_LIT>'<EOL>path = '<STR_LIT>' + name + s<EOL>importer.import_symbol(typename, path)<EOL><DEDENT><DEDENT>elif name == '<STR_LIT>':<EOL><INDENT>if not isinstance(value, (list, int, tuple, str)):<EOL><INDENT>raise TypeError('<STR_LIT>' % value)<EOL><DEDENT><DEDENT>elif type(old_value) is not type(value):<EOL><INDENT>raise TypeError('<STR_LIT>' %<EOL>(type(old_value), value, type(value)))<EOL><DEDENT>desc[name] = value<EOL>

Set one section in a Project description


other = other and _as_dict(other) or {}<EOL>for i in other, kwds:<EOL><INDENT>for k, v in i.items():<EOL><INDENT>if isinstance(v, dict):<EOL><INDENT>old_v = desc[k]<EOL>for k2, v2 in v.items():<EOL><INDENT>if v2 is None:<EOL><INDENT>old_v.pop(k2, None)<EOL><DEDENT>else:<EOL><INDENT>old_v[k2] = v2<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>set_one(desc, k, v)<EOL><DEDENT><DEDENT><DEDENT>

Update sections in a Project description


self._desc = {}<EOL>for key, value in merge.DEFAULT_PROJECT.items():<EOL><INDENT>if key not in self._HIDDEN:<EOL><INDENT>self._desc[key] = type(value)()<EOL><DEDENT><DEDENT>


return self._desc.items()<EOL>

Return an iterable of (key, section value) pairs


sections.update(self._desc, desc, **kwds)<EOL>

This method updates the description much like dict.update(), *except*:

        1. for description which have dictionary values, it uses update
           to alter the existing value and does not replace them.

        2. `None` is a special value that means "clear section to default" or
           "delete field".


return {k: v for k, v in self.items() if v}<EOL>

Returns a dictionary of non-empty description


index = self._check_index(index)<EOL>return self.layout.get(*index)<EOL>

Returns the r, g, b pixel at a location in the layout.  May only be
called if self.is_running is true.


index = self._check_index(index)<EOL>try:<EOL><INDENT>color = make.color(color)<EOL><DEDENT>except:<EOL><INDENT>log.error('<STR_LIT>', color)<EOL>raise<EOL><DEDENT>index.append(color)<EOL>return self.layout.set(*index)<EOL>

Sets the r, g, b pixel at a location in the layout.  May only be called
if self.is_running is true.


self._request_project_file(project_file)<EOL>self.clear()<EOL>self.desc.update(data_file.load(self._project_file))<EOL>

Load/reload the description from a YML file. Prompt if no file given.


self._request_project_file(project_file)<EOL>data_file.dump(self.desc.as_dict(), self.project_file)<EOL>

Save the description as a YML file. Prompt if no file given.


self.desc.clear()<EOL>


self.stop()<EOL>self.__class__._INSTANCE = weakref.ref(self)<EOL>self.is_running = True<EOL>if threaded:<EOL><INDENT>self.thread = runnable.LoopThread()<EOL>self.thread.run_once = self._target<EOL>self.thread.start()<EOL><DEDENT>else:<EOL><INDENT>self._target()<EOL><DEDENT>


if self.is_running:<EOL><INDENT>log.info('<STR_LIT>')<EOL>self.is_running = False<EOL>self.__class__._INSTANCE = None<EOL>try:<EOL><INDENT>self.thread and self.thread.stop()<EOL><DEDENT>except:<EOL><INDENT>log.error('<STR_LIT>')<EOL>traceback.print_exc()<EOL><DEDENT>self.thread = None<EOL>return True<EOL><DEDENT>

Stop the Runner if it's running.
Called as a classmethod, stop the running instance if any.


return cls._INSTANCE and cls._INSTANCE()<EOL>

Return the unique instance of Runner, if any, or None


def __init__(self, *args, limit=None, **kwds):

super().__init__(*args, **kwds)<EOL>self.limit = Limit(**(limit or {}))<EOL>self._math = color_list.Math(self.color_list)<EOL>

:param dict limit: A construction dictionary for a Limit.


def __init__(self, *args, filename='<STR_LIT>', render=None,<EOL>divide=<NUM_LIT:1>, frames=<NUM_LIT>, time=<NUM_LIT:0>, scale=<NUM_LIT:1.0>, options=None,<EOL>gif_dir=None, **kwds):

super().__init__(*args, **kwds)<EOL>self.movie_writer = _movie_writer.MovieWriter(<EOL>filename, render, divide, frames, time, scale, options, gif_dir)<EOL>

:param str filename: Base filename to write the animated GIF file

:param dict render: Parameters to the renderer function -
    see ``bibliopixel.util.image.render.renderer``

:param int divide: If greater than 1, only rendered one in ``divide``
    frames

:param int frames: Number of frames to write

:param float time: Total time to write.  If non-zero, takes precedence
    over `frames`

:param float speed: the speed of the GIF is scaled up by this factor,
    so if speed=2 then a 2 second animation will become a 1 second GIF.

:param dict options: Options to
    ``bibliopixel.util.image.gif.write_animation``

:param str gif_dir: If set, write individual GIF frame files to this
    directory, and do not delete them when done.  For testing purposes.


@classmethod<EOL><INDENT>def construct(cls, project, *, run=None, name=None, data=None, **desc):<DEDENT>

from . failed import Failed<EOL>exception = desc.pop('<STR_LIT>', None)<EOL>if exception:<EOL><INDENT>a = Failed(project.layout, desc, exception)<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>a = cls(project.layout, **desc)<EOL>a._set_runner(run or {})<EOL><DEDENT>except Exception as e:<EOL><INDENT>if cls.FAIL_ON_EXCEPTION:<EOL><INDENT>raise<EOL><DEDENT>a = Failed(project.layout, desc, e)<EOL><DEDENT><DEDENT>a.name = name<EOL>a.data = data<EOL>return a<EOL>

Construct an animation, set the runner, and add in the two
"reserved fields" `name` and `data`.


def __init__(self, layout, *, preclear=True, fail_on_exception=None, **kwds):

self.palette = legacy_palette.pop_legacy_palette(<EOL>kwds, *self.COLOR_DEFAULTS)<EOL>self.palette.length = layout.numLEDs<EOL>attributes.set_reserved(self, '<STR_LIT>', **kwds)<EOL>self.layout = layout<EOL>assert layout<EOL>self.internal_delay = None<EOL>self.on_completion = None<EOL>self.state = runner.STATE.ready<EOL>self.preclear = preclear<EOL>self.runner = None<EOL>self.time = time.time<EOL>self.preframe_callbacks = []<EOL>self.fail_on_exception = self.FAIL_ON_EXCEPTION if fail_on_exception is None else fail_on_exception<EOL>

Arguments:
  preclear: If True, clear the layout before rendering the frame;
      otherwise, the results of the previous frame are preserved

  fail_on_exception: If False, exceptions thrown in the animation frame are
      caught and reported;
      if True, exceptions are are raised, potentially ending the
      animation cycle and the program;
      if None or not set, the value of Animation.FAIL_ON_EXCEPTION is used


return self.layout<EOL>

Many BiblioPixelAnimations use the "protected" variable _led.


return self.state == runner.STATE.complete<EOL>

Many BiblioPixelAnimations use the old `completed` variable.


self.preframe_callbacks.append(callback)<EOL>

The preframe_callbacks are called right before the start of a
frame rendering pass.

To avoid race conditions when editing values, the ``Project``
adds a callback here for the top-level animation, to drain the
edit_queue at a moment where no rendering is
happening.


def __init__(self, *args, overlay=False, detach=True, **kwds):

super().__init__(*args, **kwds)<EOL>if detach:<EOL><INDENT>self.detach(overlay)<EOL><DEDENT>

If overlay is True, then preclear is set to False for everything
other than the first animation.


def __init__(self, layout, random=False, length=None, **kwds):

super().__init__(layout, **kwds)<EOL>self.random = random<EOL>if isinstance(length, (list, tuple)):<EOL><INDENT>self.length = length<EOL><DEDENT>else:<EOL><INDENT>self.length = length and [length]<EOL><DEDENT>

Arguments

random -- If True, a random animation is selected each step
length -- if length is a number, run all the animations in a loop
    for `length` seconds each.  If `length` is a list of numbers,
    use the numbers successively as times.


def opener(ip_address, port, delay=<NUM_LIT:1>):

global WEBPAGE_OPENED<EOL>if WEBPAGE_OPENED:<EOL><INDENT>return<EOL><DEDENT>WEBPAGE_OPENED = True<EOL>raw_opener(ip_address, port, delay)<EOL>

Wait a little and then open a web browser page for the control panel.


def raw_opener(ip_address, port, delay=<NUM_LIT:1>):

def target():<EOL><INDENT>time.sleep(delay)<EOL>url = '<STR_LIT>' % (ip_address, port)<EOL>webbrowser.open(url, new=<NUM_LIT:0>, autoraise=True)<EOL><DEDENT>threading.Thread(target=target, daemon=True).start()<EOL>


desc = load.load_if_filename(desc) or desc<EOL>if isinstance(desc, str):<EOL><INDENT>animation = {'<STR_LIT>': desc}<EOL><DEDENT>elif not isinstance(desc, dict):<EOL><INDENT>raise TypeError('<STR_LIT>' % type(desc))<EOL><DEDENT>elif '<STR_LIT>' in desc or '<STR_LIT>' not in desc:<EOL><INDENT>animation = desc<EOL><DEDENT>else:<EOL><INDENT>animation = desc.pop('<STR_LIT>', {})<EOL>if isinstance(animation, str):<EOL><INDENT>animation = {'<STR_LIT>': animation}<EOL><DEDENT>animation['<STR_LIT>'] = desc.pop('<STR_LIT>', {})<EOL>if desc:<EOL><INDENT>raise ValueError('<STR_LIT>' + '<STR_LIT:U+002CU+0020>'.join(desc))<EOL><DEDENT><DEDENT>animation.setdefault('<STR_LIT>', DEFAULT_ANIMATION)<EOL>animation = construct.to_type_constructor(animation, ANIMATION_PATH)<EOL>datatype = animation.setdefault('<STR_LIT>', failed.Failed)<EOL>animation.setdefault('<STR_LIT:name>', datatype.__name__)<EOL>run = animation.setdefault('<STR_LIT>', {})<EOL>run_parent = parent.setdefault('<STR_LIT>', {})<EOL>if not ('<STR_LIT>' in run or '<STR_LIT>' in run):<EOL><INDENT>if '<STR_LIT>' in run_parent:<EOL><INDENT>run.update(fps=run_parent['<STR_LIT>'])<EOL><DEDENT>elif '<STR_LIT>' in run_parent:<EOL><INDENT>run.update(sleep_time=run_parent['<STR_LIT>'])<EOL><DEDENT><DEDENT>return animation<EOL>

Cleans up all sorts of special cases that humans want when entering
an animation from a yaml file.

1. Loading it from a file
2. Using just a typename instead of a dict
3. A single dict representing an animation, with a run: section.
4. (Legacy) Having a dict with parallel elements run: and animation:
5. (Legacy) A tuple or list: (animation, run )


counts = {}<EOL>for a in animations:<EOL><INDENT>c = counts.get(a['<STR_LIT:name>'], <NUM_LIT:0>) + <NUM_LIT:1><EOL>counts[a['<STR_LIT:name>']] = c<EOL>if c > <NUM_LIT:1>:<EOL><INDENT>a['<STR_LIT:name>'] += '<STR_LIT:_>' + str(c - <NUM_LIT:1>)<EOL><DEDENT><DEDENT>dupes = set(k for k, v in counts.items() if v > <NUM_LIT:1>)<EOL>for a in animations:<EOL><INDENT>if a['<STR_LIT:name>'] in dupes:<EOL><INDENT>a['<STR_LIT:name>'] += '<STR_LIT>'<EOL><DEDENT><DEDENT>

Given a list of animations, some of which might have duplicate names, rename
the first one to be <duplicate>_0, the second <duplicate>_1,
<duplicate>_2, etc.


<EOL>for i, a in enumerate(self.animations):<EOL><INDENT>a.layout = a.layout.clone()<EOL>if overlay and i:<EOL><INDENT>a.preclear = False<EOL><DEDENT><DEDENT>

Give each animation a unique, mutable layout so they can run
independently.


def __init__(self, *args, size=<NUM_LIT:100>, **kwds):

super().__init__(*args, detach=False, **kwds)<EOL>if not size:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>self.size = size if isinstance(size, list) else [size]<EOL>self.is_numpy = hasattr(self.color_list, '<STR_LIT>')<EOL>for animation, begin, end in self._foreach():<EOL><INDENT>animation.layout = Strip([], color_list=self.color_list[begin:end])<EOL><DEDENT>

Arguments --
    size: a number or a list of numbers representing the size of each
        segment from the original layout.  If there aren't enough sizes
        for each segment, the list of sizes is reused repeatedly.


return self._index<EOL>

:returns int: index of the current animation within the Collection.


if self.animation:<EOL><INDENT>log.debug('<STR_LIT>',<EOL>self.__class__.__name__, self.current_animation.title)<EOL>self.frames = self.animation.generate_frames(False)<EOL><DEDENT>

Override this method to get called right after ``self.index`` is set.

:param int old_index: the previous index, before it was changed.


if <NUM_LIT:0> <= self._index < len(self.animations):<EOL><INDENT>return self.animations[self._index]<EOL><DEDENT>

:returns: the selected animation based on self.index, or None if
    self.index is out of bounds


return self.animation<EOL>


if not self._stop_event.isSet():<EOL><INDENT>self._hold_for_data.wait()<EOL>self._hold_for_data.clear()<EOL><DEDENT>

This may seem silly, but on a Receiver step() need not do anything.
Instead, receive the data on the receive thread and set it on the buffer
then call self._hold_for_data.set()


layout = animation.layout<EOL>required = getattr(animation, '<STR_LIT>', None)<EOL>if not required or isinstance(layout, required):<EOL><INDENT>return<EOL><DEDENT>msg = LAYOUT_WARNING % (<EOL>type(animation).__name__, required.__name__, type(layout).__name__)<EOL>setter = layout.set<EOL>adaptor = None<EOL>if required is strip.Strip:<EOL><INDENT>if isinstance(layout, matrix.Matrix):<EOL><INDENT>width = layout.width<EOL>def adaptor(pixel, color=None):<EOL><INDENT>y, x = divmod(pixel, width)<EOL>setter(x, y, color or BLACK)<EOL><DEDENT><DEDENT>elif isinstance(layout, cube.Cube):<EOL><INDENT>lx, ly = layout.x, layout.y<EOL>def adaptor(pixel, color=None):<EOL><INDENT>yz, x = divmod(pixel, lx)<EOL>z, y = divmod(yz, ly)<EOL>setter(x, y, z, color or BLACK)<EOL><DEDENT><DEDENT>elif isinstance(layout, circle.Circle):<EOL><INDENT>def adaptor(pixel, color=None):<EOL><INDENT>layout._set_base(pixel, color or BLACK)<EOL><DEDENT><DEDENT><DEDENT>elif required is matrix.Matrix:<EOL><INDENT>if isinstance(layout, strip.Strip):<EOL><INDENT>width = animation.width<EOL>def adaptor(x, y, color=None):<EOL><INDENT>setter(x + y * width, color or BLACK)<EOL><DEDENT><DEDENT><DEDENT>if not adaptor:<EOL><INDENT>raise ValueError(msg)<EOL><DEDENT>log.warning(msg)<EOL>animation.layout.set = adaptor<EOL>

Adapt the setter in an animation's layout so that Strip animations can run
on on Matrix, Cube, or Circle layout, and Matrix or Cube animations can run
on a Strip layout.


def __init__(self, offset=<NUM_LIT:0>, begin=None, end=None):

self.begin = self.BEGIN if begin is None else begin<EOL>self.end = self.END if end is None else end<EOL>if not (self.BEGIN <= self.begin <= self.end <= self.END):<EOL><INDENT>raise ValueError('<STR_LIT>' %<EOL>(self.BEGIN, self.begin, self.end, self.END))<EOL><DEDENT>self.offset = offset<EOL>

Unlike a `range`, an OffsetRange includes both its begin *and* its end,
so it's closer to how regular people think of a range - for example
that DMX channels are in the range 1-512.


if self.begin <= i <= self.end:<EOL><INDENT>index = i - self.BEGIN - self.offset<EOL>if length is None:<EOL><INDENT>length = self.full_range()<EOL><DEDENT>else:<EOL><INDENT>length = min(length, self.full_range())<EOL><DEDENT>if <NUM_LIT:0> <= index < length:<EOL><INDENT>return index<EOL><DEDENT><DEDENT>


def read_from(self, data, pad=<NUM_LIT:0>):

for i in range(self.BEGIN, self.END + <NUM_LIT:1>):<EOL><INDENT>index = self.index(i, len(data))<EOL>yield pad if index is None else data[index]<EOL><DEDENT>

Returns a generator with the elements "data" taken by offset, restricted
by self.begin and self.end, and padded on either end by `pad` to get
back to the original length of `data`


def pointOnCircle(cx, cy, radius, angle):

angle = math.radians(angle) - (math.pi / <NUM_LIT:2>)<EOL>x = cx + radius * math.cos(angle)<EOL>if x < cx:<EOL><INDENT>x = math.ceil(x)<EOL><DEDENT>else:<EOL><INDENT>x = math.floor(x)<EOL><DEDENT>y = cy + radius * math.sin(angle)<EOL>if y < cy:<EOL><INDENT>y = math.ceil(y)<EOL><DEDENT>else:<EOL><INDENT>y = math.floor(y)<EOL><DEDENT>return (int(x), int(y))<EOL>

Calculates the coordinates of a point on a circle given the center point,
radius, and angle.


def genVector(width, height, x_mult=<NUM_LIT:1>, y_mult=<NUM_LIT:1>):

center_x = (width - <NUM_LIT:1>) / <NUM_LIT:2><EOL>center_y = (height - <NUM_LIT:1>) / <NUM_LIT:2><EOL>def length(x, y):<EOL><INDENT>dx = math.pow(x - center_x, <NUM_LIT:2> * x_mult)<EOL>dy = math.pow(y - center_y, <NUM_LIT:2> * y_mult)<EOL>return int(math.sqrt(dx + dy))<EOL><DEDENT>return [[length(x, y) for x in range(width)] for y in range(height)]<EOL>

Generates a map of vector lengths from the center point to each coordinate.

width - width of matrix to generate
height - height of matrix to generate
x_mult - value to scale x-axis by
y_mult - value to scale y-axis by


if not isinstance(c, type):<EOL><INDENT>c = type(c)<EOL><DEDENT>return '<STR_LIT>' % (c.__module__, c.__name__)<EOL>

:param c: either an object or a class
:return: the classname as a string


parts = s.replace('<STR_LIT:U+002C>', '<STR_LIT:U+0020>').split()<EOL>if not parts:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>pieces = []<EOL>for part in parts:<EOL><INDENT>m = PART_MATCH(part)<EOL>pieces.extend(m.groups() if m else [part])<EOL><DEDENT>if len(pieces) == <NUM_LIT:1>:<EOL><INDENT>pieces.append('<STR_LIT:s>')<EOL><DEDENT>if len(pieces) % <NUM_LIT:2>:<EOL><INDENT>raise ValueError('<STR_LIT>' % (s, parts, pieces))<EOL><DEDENT>result = <NUM_LIT:0><EOL>for number, units in zip(*[iter(pieces)] * <NUM_LIT:2>):<EOL><INDENT>number = float(number)<EOL>if number < <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>result += number * _get_units(units)<EOL><DEDENT>return result<EOL>

Parse a string representing a time interval or duration into seconds,
or raise an exception

:param str s: a string representation of a time interval
:raises ValueError: if ``s`` can't be interpreted as a duration


self.__filename = filename<EOL>data = data_file.load(filename) if os.path.exists(filename) else {}<EOL>super().__init__(data)<EOL>

:param c: the filename to store the DATA_FILE in


def __init__(self, constructor, **kwds):

self.servers = {}<EOL>self.constructor = constructor<EOL>self.kwds = kwds<EOL>

:param constructor: a function which takes a key and some keywords,
    and returns a new server
:param kwds: keywords to the ``constructor`` function


kwds = dict(self.kwds, **kwds)<EOL>server = self.servers.get(key)<EOL>if server:<EOL><INDENT>server.check_keywords(self.constructor, kwds)<EOL><DEDENT>else:<EOL><INDENT>server = _CachedServer(self.constructor, key, kwds)<EOL>self.servers[key] = server<EOL><DEDENT>return server<EOL>

Get a new or existing server for this key.

:param int key: key for the server to use


def __init__(self, ratio=<NUM_LIT:1>, knee=<NUM_LIT:0>, gain=<NUM_LIT:1>, enable=True):

<EOL>self.ratio = ratio<EOL>self.knee = knee<EOL>self.gain = gain<EOL>self.enable = enable<EOL>

:param float ratio: the compression ratio (1 means no compression).
    ratio should usually between 0 and 1.

:param float knee: the ratio where the compression starts to kick in.
    knee should usually be 0 <= knee <= ratio

:param float gain: post limiter output gain. gain should usually be >= 0


self.event.wait()<EOL>self.task()<EOL>self.event.clear()<EOL>next_task.event.set()<EOL>

Wait for the event, run the task, trigger the next task.


return self.run_event.is_set() and not self.stop_event.is_set()<EOL>

Is this Runnable expected to make any progress from here?

The Runnable might still execute a little code after it has stopped
running.


return self.running<EOL>

Is this Runnable still executing code?

In some cases, such as threads, self.is_alive() might be true for some
time after self.running has turned False.


Cleans up resources after the Runnable.

self.cleanup() may not throw an exception.


pass<EOL>

The target code that is repeatedly executed in the run method


self.start()<EOL>try:<EOL><INDENT>yield self<EOL><DEDENT>finally:<EOL><INDENT>self.stop()<EOL><DEDENT>self.wait()<EOL>

A context manager that starts this Runnable, yields,
and then waits for it to finish.


events = list(events)<EOL>master_event = threading.Event()<EOL>def changed():<EOL><INDENT>if condition(e.is_set() for e in events):<EOL><INDENT>master_event.set()<EOL><DEDENT>else:<EOL><INDENT>master_event.clear()<EOL><DEDENT><DEDENT>def add_changed(f):<EOL><INDENT>@functools.wraps(f)<EOL>def wrapped():<EOL><INDENT>f()<EOL>changed()<EOL><DEDENT>return wrapped<EOL><DEDENT>for e in events:<EOL><INDENT>e.set = add_changed(e.set)<EOL>e.clear = add_changed(e.clear)<EOL><DEDENT>changed()<EOL>return master_event<EOL>

Compose a sequence of events into one event.

Arguments:
    events:    a sequence of objects looking like threading.Event
    condition: a function taking a sequence of bools and returning a bool.


def run(function, *args, use_subprocess=False, daemon=True, **kwds):

if use_subprocess:<EOL><INDENT>Creator, Queue = multiprocessing.Process, multiprocessing.Queue<EOL><DEDENT>else:<EOL><INDENT>Creator, Queue = threading.Thread, queue.Queue<EOL><DEDENT>input, output = Queue(), Queue()<EOL>args = input, output, function, args<EOL>sub = Creator(target=_run_locally, args=args, kwargs=kwds, daemon=daemon)<EOL>sub.start()<EOL>return sub, input, output<EOL>

Create input, output queues, call `function` in a subprocess or a thread.

``function`` is called like this: ``function(input, output, *args, **kwds)``

:param use_subprocess: if true, create a new multiprocess;
                       if false, create a new thread
:param function: the function to call
:param daemon: is the thread or subprocess run as a daemon or not?

:param args: positional arguments to the function
:param kwds: keyword arguments to the function
:returns: a tuple with three elements: the subprocess or thread, an input
          queue, and an output queue.


assert isinstance(errors, int) or errors in (<EOL>'<STR_LIT>', '<STR_LIT:ignore>', '<STR_LIT>')<EOL>self.function = function<EOL>self.errors = errors<EOL>self.error_count = <NUM_LIT:0><EOL>

:param function: the function to wrap
:param errors: either a number, indicating how many errors to report
   before ignoring them, or one of these strings:
   'raise', meaning to raise an exception
   'ignore', meaning to ignore all errors
   'report', meaning to report all errors


try:<EOL><INDENT>return self.function(*args, **kwds)<EOL><DEDENT>except Exception as e:<EOL><INDENT>self.error_count += <NUM_LIT:1><EOL>if self.errors == '<STR_LIT>':<EOL><INDENT>raise<EOL><DEDENT>if self.errors == '<STR_LIT:ignore>':<EOL><INDENT>return<EOL><DEDENT>args = (class_name.class_name(e),) + e.args<EOL><DEDENT>if self.errors == '<STR_LIT>' or self.error_count <= self.errors:<EOL><INDENT>log.error(str(args))<EOL><DEDENT>elif self.error_count == self.errors + <NUM_LIT:1>:<EOL><INDENT>log.error('<STR_LIT>', self.errors)<EOL><DEDENT>

Calls `self.function` with the given arguments and keywords, and
returns its value - or if the call throws an exception, returns None.


def _addLoggingLevel(levelName, levelNum, methodName=None):

if not methodName:<EOL><INDENT>methodName = levelName.lower()<EOL><DEDENT>if hasattr(logging, levelName):<EOL><INDENT>raise AttributeError(<EOL>'<STR_LIT>'.format(levelName))<EOL><DEDENT>if hasattr(logging, methodName):<EOL><INDENT>raise AttributeError(<EOL>'<STR_LIT>'.format(methodName))<EOL><DEDENT>if hasattr(logging.getLoggerClass(), methodName):<EOL><INDENT>raise AttributeError(<EOL>'<STR_LIT>'.format(methodName))<EOL><DEDENT>def logForLevel(self, message, *args, **kwargs):<EOL><INDENT>if self.isEnabledFor(levelNum):<EOL><INDENT>self._log(levelNum, message, args, **kwargs)<EOL><DEDENT><DEDENT>def logToRoot(message, *args, **kwargs):<EOL><INDENT>logging.log(levelNum, message, *args, **kwargs)<EOL><DEDENT>logging.addLevelName(levelNum, levelName)<EOL>setattr(logging, levelName, levelNum)<EOL>setattr(logging.getLoggerClass(), methodName, logForLevel)<EOL>setattr(logging, methodName, logToRoot)<EOL>

Comprehensively adds a new logging level to the `logging` module and the
currently configured logging class.

`levelName` becomes an attribute of the `logging` module with the value
`levelNum`. `methodName` becomes a convenience method for both `logging`
itself and the class returned by `logging.getLoggerClass()` (usually just
`logging.Logger`). If `methodName` is not specified, `levelName.lower()` is
used.

To avoid accidental clobberings of existing attributes, this method will
raise an `AttributeError` if the level name is already an attribute of the
`logging` module or if the method name is already present

Example
-------
>>> addLoggingLevel('TRACE', logging.DEBUG - 5)
>>> logging.getLogger(__name__).setLevel("TRACE")
>>> logging.getLogger(__name__).trace('that worked')
>>> logging.trace('so did this')
>>> logging.TRACE
5


if isinstance(level, str):<EOL><INDENT>level = LOG_NAMES[level.lower()]<EOL><DEDENT>logger.setLevel(level)<EOL>

:param level: the level to set - either a string level name from
              'frame', 'debug', 'info', 'warning', 'error'
              or an integer log level from:
              log.FRAME, log.DEBUG, log.INFO, log.WARNING, log.ERROR


def dumps(data, use_yaml=None, safe=True, **kwds):

if use_yaml is None:<EOL><INDENT>use_yaml = ALWAYS_DUMP_YAML<EOL><DEDENT>if use_yaml:<EOL><INDENT>dumps = yaml.safe_dump if safe else yaml.dump<EOL><DEDENT>else:<EOL><INDENT>dumps = json.dumps<EOL>kwds.update(indent=<NUM_LIT:4>, sort_keys=True)<EOL>if not safe:<EOL><INDENT>kwds.update(default=repr)<EOL><DEDENT><DEDENT>return dumps(data, **kwds)<EOL>

Dumps data into a nicely formatted JSON string.

:param dict data: a dictionary to dump
:param kwds: keywords to pass to json.dumps
:returns: a string with formatted data
:rtype: str


def dump(data, file=sys.stdout, use_yaml=None, **kwds):

if use_yaml is None:<EOL><INDENT>use_yaml = ALWAYS_DUMP_YAML<EOL><DEDENT>def dump(fp):<EOL><INDENT>if use_yaml:<EOL><INDENT>yaml.safe_dump(data, stream=fp, **kwds)<EOL><DEDENT>else:<EOL><INDENT>json.dump(data, fp, indent=<NUM_LIT:4>, sort_keys=True, **kwds)<EOL><DEDENT><DEDENT>if not isinstance(file, str):<EOL><INDENT>return dump(file)<EOL><DEDENT>if os.path.isabs(file):<EOL><INDENT>parent = os.path.dirname(file)<EOL>if not os.path.exists(parent):<EOL><INDENT>os.makedirs(parent, exist_ok=True)<EOL><DEDENT><DEDENT>with open(file, '<STR_LIT:w>') as fp:<EOL><INDENT>return dump(fp)<EOL><DEDENT>

Dumps data as nicely formatted JSON string to a file or file handle

:param dict data: a dictionary to dump
:param file: a filename or file handle to write to
:param kwds: keywords to pass to json.dump


if isinstance(file, str):<EOL><INDENT>fp = open(file)<EOL>filename = file<EOL><DEDENT>else:<EOL><INDENT>fp = file<EOL>filename = getattr(fp, '<STR_LIT:name>', '<STR_LIT>')<EOL><DEDENT>try:<EOL><INDENT>return loads(fp.read(), use_yaml, filename)<EOL><DEDENT>except Exception as e:<EOL><INDENT>e.args = ('<STR_LIT>', filename) + e.args<EOL>raise<EOL><DEDENT>

Loads not only JSON files but also YAML files ending in .yml.

:param file: a filename or file handle to read from
:returns: the data loaded from the JSON or YAML file
:rtype: dict


is_data_file = s.endswith('<STR_LIT>') or s.endswith('<STR_LIT>')<EOL>return load(s) if is_data_file else loads(s)<EOL>

Load either a filename, or a string representation of yml/json.


def advance_permutation(a, increasing=True, forward=True):

if not forward:<EOL><INDENT>a.reverse()<EOL><DEDENT>cmp = operator.lt if increasing else operator.gt<EOL>try:<EOL><INDENT>i = next(i for i in reversed(range(len(a) - <NUM_LIT:1>)) if cmp(a[i], a[i + <NUM_LIT:1>]))<EOL>j = next(j for j in reversed(range(i + <NUM_LIT:1>, len(a))) if cmp(a[i], a[j]))<EOL><DEDENT>except StopIteration:<EOL><INDENT>if forward:<EOL><INDENT>a.reverse()<EOL><DEDENT>return False<EOL><DEDENT>a[i], a[j] = a[j], a[i]<EOL>a[i + <NUM_LIT:1>:] = reversed(a[i + <NUM_LIT:1>:])<EOL>if not forward:<EOL><INDENT>a.reverse()<EOL><DEDENT>return True<EOL>

Advance a list of unique, ordered elements in-place, lexicographically
increasing or backward, by rightmost or leftmost digit.

Returns False if the permutation wrapped around - i.e. went from
lexicographically greatest to least, and True in all other cases.

If the length of the list is N, then this function will repeat values after
N! steps, and will return False exactly once.

See also https://stackoverflow.com/a/34325140/43839


pid_filename = pid_filename or DEFAULT_PID_FILENAME<EOL>if os.path.exists(pid_filename):<EOL><INDENT>contents = open(pid_filename).read(<NUM_LIT:16>)<EOL>log.warning('<STR_LIT>',<EOL>pid_filename, contents)<EOL><DEDENT>with open(pid_filename, '<STR_LIT:w>') as fp:<EOL><INDENT>fp.write(str(os.getpid()))<EOL>fp.write('<STR_LIT:\n>')<EOL><DEDENT>try:<EOL><INDENT>yield<EOL><DEDENT>finally:<EOL><INDENT>try:<EOL><INDENT>os.remove(pid_filename)<EOL><DEDENT>except Exception as e:<EOL><INDENT>log.error('<STR_LIT>',<EOL>e, pid_filename)<EOL><DEDENT><DEDENT>

For the duration of this context manager, put the PID for this process into
`pid_filename`, and then remove the file at the end.


return int(open(pid_filename or DEFAULT_PID_FILENAME).read(<NUM_LIT:16>))<EOL>

Return the integer PID for the current bp process, or raise an exception if
there is no such process or it hasn't registered a PID.


try:<EOL><INDENT>yield<EOL><DEDENT>except Exception as e:<EOL><INDENT>e.args = args + e.args<EOL>raise<EOL><DEDENT>

A context manager that appends arguments to any exception thrown

:param args: Arguments to be appended to the ``.args`` attribute of any
             exception that is thrown while the context manager is active


try:<EOL><INDENT>function(*args, **kwds)<EOL><DEDENT>except Exception:<EOL><INDENT>traceback.print_exc()<EOL><DEDENT>

Run a function, catch, report and discard exceptions


def crop(image, top_offset=<NUM_LIT:0>, left_offset=<NUM_LIT:0>, bottom_offset=<NUM_LIT:0>, right_offset=<NUM_LIT:0>):

if bottom_offset or top_offset or left_offset or right_offset:<EOL><INDENT>width, height = image.size<EOL>box = (left_offset, top_offset,<EOL>width - right_offset, height - bottom_offset)<EOL>image = image.crop(box=box)<EOL><DEDENT>return image<EOL>

Return an image cropped on top, bottom, left or right.


def resize(image, x, y, stretch=False, top=None, left=None, mode='<STR_LIT>',<EOL>resample=None):

if x <= <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if y <= <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>from PIL import Image<EOL>resample = Image.ANTIALIAS if resample is None else resample<EOL>if not isinstance(resample, numbers.Number):<EOL><INDENT>try:<EOL><INDENT>resample = getattr(Image, resample.upper())<EOL><DEDENT>except:<EOL><INDENT>raise ValueError("<STR_LIT>" % resample)<EOL><DEDENT>if not isinstance(resample, numbers.Number):<EOL><INDENT>raise ValueError("<STR_LIT>" % resample)<EOL><DEDENT><DEDENT>size = x, y<EOL>if stretch:<EOL><INDENT>return image.resize(size, resample=resample)<EOL><DEDENT>result = Image.new(mode, size)<EOL>ratios = [d1 / d2 for d1, d2 in zip(size, image.size)]<EOL>if ratios[<NUM_LIT:0>] < ratios[<NUM_LIT:1>]:<EOL><INDENT>new_size = (size[<NUM_LIT:0>], int(image.size[<NUM_LIT:1>] * ratios[<NUM_LIT:0>]))<EOL><DEDENT>else:<EOL><INDENT>new_size = (int(image.size[<NUM_LIT:0>] * ratios[<NUM_LIT:1>]), size[<NUM_LIT:1>])<EOL><DEDENT>image = image.resize(new_size, resample=resample)<EOL>if left is None:<EOL><INDENT>box_x = int((x - new_size[<NUM_LIT:0>]) / <NUM_LIT:2>)<EOL><DEDENT>elif left:<EOL><INDENT>box_x = <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>box_x = x - new_size[<NUM_LIT:0>]<EOL><DEDENT>if top is None:<EOL><INDENT>box_y = int((y - new_size[<NUM_LIT:1>]) / <NUM_LIT:2>)<EOL><DEDENT>elif top:<EOL><INDENT>box_y = <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>box_y = y - new_size[<NUM_LIT:1>]<EOL><DEDENT>result.paste(image, box=(box_x, box_y))<EOL>return result<EOL>


def _write(self, filename, frames, fps, loop=<NUM_LIT:0>, palette=<NUM_LIT>):

from PIL import Image<EOL>images = []<EOL>for f in frames:<EOL><INDENT>data = open(f, '<STR_LIT:rb>').read()<EOL>images.append(Image.open(io.BytesIO(data)))<EOL><DEDENT>duration = round(<NUM_LIT:1> / fps, <NUM_LIT:2>)<EOL>im = images.pop(<NUM_LIT:0>)<EOL>im.save(filename,<EOL>save_all=True,<EOL>append_images=images,<EOL>duration=duration,<EOL>loop=loop,<EOL>palette=palette)<EOL>

Write a series of frames as a single animated GIF.

:param str filename: the name of the GIF file to write

:param list frames: a list of filenames, each of which represents a single
    frame of the animation.  Each frame must have exactly the same
    dimensions, and the code has only been tested with .gif files.

:param float fps:
    The number of frames per second.

:param int loop:
    The number of iterations. Default 0 (meaning loop indefinitely).

:param int palette:
    The number of colors to quantize the image to. Is rounded to
    the nearest power of two. Default 256.


def show_image(setter, width, height,<EOL>image_path='<STR_LIT>', image_obj=None, offset=(<NUM_LIT:0>, <NUM_LIT:0>),<EOL>bgcolor=COLORS.Off, brightness=<NUM_LIT:255>):

bgcolor = color_scale(bgcolor, brightness)<EOL>img = image_obj<EOL>if image_path and not img:<EOL><INDENT>from PIL import Image<EOL>img = Image.open(image_path)<EOL><DEDENT>elif not img:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>w = min(width - offset[<NUM_LIT:0>], img.size[<NUM_LIT:0>])<EOL>h = min(height - offset[<NUM_LIT:1>], img.size[<NUM_LIT:1>])<EOL>ox = offset[<NUM_LIT:0>]<EOL>oy = offset[<NUM_LIT:1>]<EOL>for x in range(ox, w + ox):<EOL><INDENT>for y in range(oy, h + oy):<EOL><INDENT>r, g, b, a = (<NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:255>)<EOL>rgba = img.getpixel((x - ox, y - oy))<EOL>if isinstance(rgba, int):<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if len(rgba) == <NUM_LIT:3>:<EOL><INDENT>r, g, b = rgba<EOL><DEDENT>elif len(rgba) == <NUM_LIT:4>:<EOL><INDENT>r, g, b, a = rgba<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if a == <NUM_LIT:0>:<EOL><INDENT>r, g, b = bgcolor<EOL><DEDENT>else:<EOL><INDENT>r, g, b = color_scale((r, g, b), a)<EOL><DEDENT>if brightness != <NUM_LIT:255>:<EOL><INDENT>r, g, b = color_scale((r, g, b), brightness)<EOL><DEDENT>setter(x, y, (r, g, b))<EOL><DEDENT><DEDENT>


def showImage(layout, imagePath="<STR_LIT>", imageObj=None, offset=(<NUM_LIT:0>, <NUM_LIT:0>),<EOL>bgcolor=COLORS.Off, brightness=<NUM_LIT:255>):

if not isinstance(layout, Matrix):<EOL><INDENT>raise RuntimeError("<STR_LIT>")<EOL><DEDENT>layout.all_off()<EOL>return show_image(layout.set, layout.width, layout.height, imagePath,<EOL>imageObj, offset, bgcolor, brightness)<EOL>


def loadImage(layout, imagePath="<STR_LIT>", imageObj=None, offset=(<NUM_LIT:0>, <NUM_LIT:0>),<EOL>bgcolor=COLORS.Off, brightness=<NUM_LIT:255>):

if not isinstance(layout, Matrix):<EOL><INDENT>raise RuntimeError("<STR_LIT>")<EOL><DEDENT>texture = [[COLORS.Off for x in range(layout.width)]<EOL>for y in range(layout.height)]<EOL>def setter(x, y, pixel):<EOL><INDENT>if y >= <NUM_LIT:0> and x >= <NUM_LIT:0>:<EOL><INDENT>texture[y][x] = pixel<EOL><DEDENT><DEDENT>show_image(setter, layout.width, layout.height, imagePath, imageObj,<EOL>offset, bgcolor, brightness)<EOL>return texture<EOL>


deprecated.deprecated('<STR_LIT>')<EOL>return image if (image.mode == mode) else image.convert(mode=mode)<EOL>


deprecated.deprecated('<STR_LIT>')<EOL>return container(convert_mode(image).getdata())<EOL>

Given a PIL.Image, returns a ColorList of its pixels.


deprecated.deprecated('<STR_LIT>')<EOL>from PIL import ImageSequence<EOL>it = ImageSequence.Iterator(image)<EOL>return [image_to_colorlist(i, container) for i in it]<EOL>

Given an animated GIF, return a list with a colorlist for each frame.


def sender(address, use_queue=True, **kwds):

return QueuedSender(address, **kwds) if use_queue else Sender(address)<EOL>

:param str address: a pair (ip_address, port) to pass to socket.connect
:param bool use_queue: if True, run the connection in a different thread
    with a queue


super().__init__()<EOL>self.address = address<EOL>

:param str address: a pair (ip_address, port) to pass to socket.connect


super().__init__(**kwds)<EOL>self.sender = Sender(address)<EOL>


result = {}<EOL>def add(value, *keys):<EOL><INDENT>if keys in result:<EOL><INDENT>raise ValueError('<STR_LIT>' % keys)<EOL><DEDENT>result[keys] = value<EOL><DEDENT>def recurse(value, *keys):<EOL><INDENT>if isinstance(value, dict):<EOL><INDENT>for k, v in value.items():<EOL><INDENT>recurse(v, k, *keys)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>key = '<STR_LIT:.>'.join(reversed(keys))<EOL>if key in result:<EOL><INDENT>raise ValueError('<STR_LIT>' % str(keys))<EOL><DEDENT>result[key] = value<EOL><DEDENT><DEDENT>recurse(master)<EOL>return result<EOL>

:param dict master: a multilevel dictionary
:return: a flattened dictionary
:rtype: dict

Flattens a multilevel dictionary into a single-level one so that::

    {'foo':
        {'bar':
           {
               'a': 1,
               'b': True,
               'c': 'hello',
            },
        },
    }

would become::

    {'foo.bar.a': 1,
     'foo.bar.b': True,
     'foo.bar.a': 1,
     }

You can mix and match both input (hierarchical) and output (dotted) formats
in the input without problems - and if you call flatten more than once, it
has no effect.


result = {}<EOL>for k, v in master.items():<EOL><INDENT>*first, last = k.split('<STR_LIT:.>')<EOL>r = result<EOL>for i in first:<EOL><INDENT>r = r.setdefault(i, {})<EOL><DEDENT>r[last] = v<EOL><DEDENT>return result<EOL>

:param dict master: a multilevel dictionary
:return: a unflattened dictionary
:rtype: dict

Unflattens a single-level dictionary a multilevel into one so that::

    {'foo.bar.a': 1,
     'foo.bar.b': True,
     'foo.bar.a': 1,
     }

would become::

    {'foo':
        {'bar':
           {
               'a': 1,
               'b': True,
               'c': 'hello',
            },
        },
    }


return unflatten(flatten(master))<EOL>

:param dict master: a multilevel dictionary
:return: a canonicalized dictionary that has been completely flattened
         and then unflattened
:rtype: dict


palette = kwds.pop('<STR_LIT>', None)<EOL>if palette:<EOL><INDENT>legacy = [k for k, _ in color_defaults if k in kwds]<EOL>if legacy:<EOL><INDENT>raise ValueError('<STR_LIT>' + '<STR_LIT:U+002CU+0020>'.join(legacy))<EOL><DEDENT>return palette<EOL><DEDENT>values = [kwds.pop(k, v) for k, v in color_defaults]<EOL>if values and color_defaults[<NUM_LIT:0>][<NUM_LIT:0>] in ('<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>values = values[<NUM_LIT:0>]<EOL><DEDENT>return make.colors(values or None)<EOL>

Older animations in BPA and other areas use all sorts of different names for
what we are now representing with palettes.

This function mutates a kwds dictionary to remove these legacy fields and
extract a palette from it, which it returns.


return _WHEEL[round(position) % len(_WHEEL)]<EOL>

Get color from wheel value (0 - 384).
    Provided for those used to using it from Adafruit libraries


def wheel_helper(pos, length, cycle_step):

return wheel_color((pos * len(_WHEEL) / length) + cycle_step)<EOL>

Helper for wheel_color that distributes colors over length and
    allows shifting position.


yield from _TO_COLOR_USER.items()<EOL>for name, color in _TO_COLOR.items():<EOL><INDENT>if name not in _TO_COLOR_USER:<EOL><INDENT>yield name, color<EOL><DEDENT><DEDENT>

Return an iteration over all name, color pairs in tables


if isinstance(x, str):<EOL><INDENT>x = canonical_name(x)<EOL>return x in _TO_COLOR_USER or x in _TO_COLOR<EOL><DEDENT>else:<EOL><INDENT>x = tuple(x)<EOL>return x in _TO_NAME_USER or x in _TO_NAME<EOL><DEDENT>

Return true if this string or integer tuple appears in tables


raise ValueError("<STR_LIT>" % type(c), _COLORS_USAGE)<EOL>


try:<EOL><INDENT>colors[<NUM_LIT:0>][<NUM_LIT:0>]<EOL>return colors<EOL><DEDENT>except:<EOL><INDENT>pass<EOL><DEDENT>extra = len(colors) % <NUM_LIT:3><EOL>if extra:<EOL><INDENT>colors = colors[:-extra]<EOL><DEDENT>return list(zip(*[iter(colors)] * <NUM_LIT:3>))<EOL>

Coerce a list into a list of triplets.

If `colors` is a list of lists or strings, return it as is.  Otherwise,
divide it into tuplets of length three, silently discarding any extra
elements beyond a multiple of three.


if isinstance(colors, str):<EOL><INDENT>colors = _split_colors(colors)<EOL><DEDENT>else:<EOL><INDENT>colors = to_triplets(colors or ())<EOL><DEDENT>colors = (color(c) for c in colors or ())<EOL>return palette.Palette(colors, **kwds)<EOL>

Return a Palette but don't take into account Pallete Names.