code
string
signature
string
docstring
string
loss_without_docstring
float64
loss_with_docstring
float64
factor
float64
if (phone is None) and (email is None): raise ParamsError() if password is None: raise ParamsError() r = NCloudBot() # r.username = phone or email md5 = hashlib.md5() md5.update(password) password = md5.hexdigest() print password r.data = {'password': password, ...
def login(password, phone=None, email=None, rememberLogin=True)
登录接口,返回 :class:'Response' 对象 :param password: 网易云音乐的密码 :param phone: (optional) 手机登录 :param email: (optional) 邮箱登录 :param rememberLogin: (optional) 是否记住密码,默认 True
3.393271
3.379711
1.004012
if uid is None: raise ParamsError() r = NCloudBot() r.method = 'USER_PLAY_LIST' r.data = {'offset': offset, 'uid': uid, 'limit': limit, 'csrf_token': ''} r.send() return r.response
def user_play_list(uid, offset=0, limit=1000)
获取用户歌单,包含收藏的歌单 :param uid: 用户的ID,可通过登录或者其他接口获取 :param offset: (optional) 分段起始位置,默认 0 :param limit: (optional) 数据上限多少行,默认 1000
5.784021
6.747431
0.857218
if uid is None: raise ParamsError() r = NCloudBot() r.method = 'USER_DJ' r.data = {'offset': offset, 'limit': limit, "csrf_token": ""} r.params = {'uid': uid} r.send() return r.response
def user_dj(uid, offset=0, limit=30)
获取用户电台数据 :param uid: 用户的ID,可通过登录或者其他接口获取 :param offset: (optional) 分段起始位置,默认 0 :param limit: (optional) 数据上限多少行,默认 30
6.452674
7.678125
0.840397
if keyword is None: raise ParamsError() r = NCloudBot() r.method = 'SEARCH' r.data = { 's': keyword, 'limit': str(limit), 'type': str(type), 'offset': str(offset) } r.send() return r.response
def search(keyword, type=1, offset=0, limit=30)
搜索歌曲,支持搜索歌曲、歌手、专辑等 :param keyword: 关键词 :param type: (optional) 搜索类型,1: 单曲, 100: 歌手, 1000: 歌单, 1002: 用户 :param offset: (optional) 分段起始位置,默认 0 :param limit: (optional) 数据上限多少行,默认 30
4.862638
5.656178
0.859704
if uid is None: raise ParamsError() r = NCloudBot() r.method = 'USER_FOLLOWS' r.params = {'uid': uid} r.data = {'offset': offset, 'limit': limit, 'order': True} r.send() return r.response
def user_follows(uid, offset='0', limit=30)
获取用户关注列表 :param uid: 用户的ID,可通过登录或者其他接口获取 :param offset: (optional) 分段起始位置,默认 0 :param limit: (optional) 数据上限多少行,默认 30
5.808166
6.995786
0.830238
if uid is None: raise ParamsError() r = NCloudBot() r.method = 'USER_EVENT' r.params = {'uid': uid} r.data = {'time': -1, 'getcounts': True, "csrf_token": ""} r.send() return r.response
def user_event(uid)
获取用户动态 :param uid: 用户的ID,可通过登录或者其他接口获取
10.012704
11.090661
0.902805
if uid is None: raise ParamsError() r = NCloudBot() r.method = 'USER_RECORD' r.data = {'type': type, 'uid': uid, "csrf_token": ""} r.send() return r.response
def user_record(uid, type=0)
获取用户的播放列表,必须登录 :param uid: 用户的ID,可通过登录或者其他接口获取 :param type: (optional) 数据类型,0:获取所有记录,1:获取 weekData
8.505954
9.941768
0.855578
r = NCloudBot() r.method = 'EVENT' r.data = {"csrf_token": ""} r.send() return r.response
def event()
获取好友的动态,包括分享视频、音乐、动态等
15.788038
15.358591
1.027961
r = NCloudBot() r.method = 'TOP_PLAYLIST_HIGHQUALITY' r.data = {'cat': cat, 'offset': offset, 'limit': limit} r.send() return r.response
def top_playlist_highquality(cat='全部', offset=0, limit=20)
获取网易云音乐的精品歌单 :param cat: (optional) 歌单类型,默认 ‘全部’,比如 华语、欧美等 :param offset: (optional) 分段起始位置,默认 0 :param limit: (optional) 数据上限多少行,默认 20
5.045775
6.227322
0.810264
if id is None: raise ParamsError() r = NCloudBot() r.method = 'PLAY_LIST_DETAIL' r.data = {'id': id, 'limit': limit, "csrf_token": ""} r.send() return r.response
def play_list_detail(id, limit=20)
获取歌单中的所有音乐。由于获取精品中,只能看到歌单名字和 ID 并没有歌单的音乐,因此增加该接口传入歌单 ID 获取歌单中的所有音乐. :param id: 歌单的ID :param limit: (optional) 数据上限多少行,默认 20
7.085087
7.985322
0.887264
if not isinstance(ids, list): raise ParamsError() r = NCloudBot() r.method = 'MUSIC_URL' r.data = {'ids': ids, 'br': 999000, "csrf_token": ""} r.send() return r.response
def music_url(ids=[])
通过歌曲 ID 获取歌曲下载地址 :param ids: 歌曲 ID 的 list
6.262943
6.687058
0.936577
if id is None: raise ParamsError() r = NCloudBot() r.method = 'LYRIC' r.params = {'id': id} r.send() return r.response
def lyric(id)
通过歌曲 ID 获取歌曲歌词地址 :param id: 歌曲ID
7.959171
9.683273
0.82195
if id is None: raise ParamsError() r = NCloudBot() r.method = 'MUSIC_COMMENT' r.params = {'id': id} r.data = {'offset': offset, 'limit': limit, 'rid': id, "csrf_token": ""} r.send() return r.response
def music_comment(id, offset=0, limit=20)
获取歌曲的评论列表 :param id: 歌曲 ID :param offset: (optional) 分段起始位置,默认 0 :param limit: (optional) 数据上限多少行,默认 20
5.906661
6.879433
0.858597
if not isinstance(ids, list): raise ParamsError() c = [] for id in ids: c.append({'id': id}) r = NCloudBot() r.method = 'SONG_DETAIL' r.data = {'c': json.dumps(c), 'ids': c, "csrf_token": ""} r.send() return r.response
def song_detail(ids)
通过歌曲 ID 获取歌曲的详细信息 :param ids: 歌曲 ID 的 list
6.382484
6.685088
0.954735
r = NCloudBot() r.method = 'PERSONAL_FM' r.data = {"csrf_token": ""} r.send() return r.response
def personal_fm()
个人的 FM ,必须在登录之后调用,即 login 之后调用
12.472488
11.590472
1.076098
headers = { 'Accept': '*/*', 'Accept-Language': 'zh-CN,zh;q=0.8,gl;q=0.6,zh-TW;q=0.4', 'Connection': 'keep-alive', 'Content-Type': 'application/x-www-form-urlencoded', 'Referer': 'ht...
def _get_webapi_requests(self)
Update headers of webapi for Requests.
1.929775
1.792944
1.076316
# rememberLogin # if self.method is 'LOGIN' and resp.json().get('code') == 200: # cookiesJar.save_cookies(resp, NCloudBot.username) self.response.content = resp.content self.response.status_code = resp.status_code self.response.headers = resp.headers
def _build_response(self, resp)
Build internal Response object from given response.
7.686454
7.637975
1.006347
success = False if self.method is None: raise ParamsError() try: if self.method == 'SEARCH': req = self._get_requests() _url = self.__NETEAST_HOST + self._METHODS[self.method] resp = req.post(_url, data=self.data) ...
def send(self)
Sens the request.
3.888766
3.86138
1.007092
if not self.headers and len(self.content) > 3: encoding = get_encoding_from_headers(self.headers) if encoding is not None: return json.loads(self.content.decode(encoding)) return json.loads(self.content)
def json(self)
Returns the json-encoded content of a response, if any.
3.653063
3.030901
1.205273
old = get_option(name) globals()[name] = value return old
def set_option(name, value)
Set plydata option Parameters ---------- name : str Name of the option value : object New value of the option Returns ------- old : object Old value of the option See also -------- :class:`options`
5.131089
10.648097
0.481878
data = pd.merge(verb.x, verb.y, **verb.kwargs) # Preserve x groups if isinstance(verb.x, GroupedDataFrame): data.plydata_groups = list(verb.x.plydata_groups) return data
def _join(verb)
Join helper
10.115255
9.568386
1.057154
if by is None: by = self.plydata_groups # Turn off sorting by groups messes with some verbs if 'sort' not in kwargs: kwargs['sort'] = False return super().groupby(by, **kwargs)
def groupby(self, by=None, **kwargs)
Group by and do not sort (unless specified) For plydata use cases, there is no need to specify group columns.
9.153531
7.242239
1.263909
# No groups if not self.plydata_groups: return np.ones(len(self), dtype=int) grouper = self.groupby() indices = np.empty(len(self), dtype=int) for i, (_, idx) in enumerate(sorted(grouper.indices.items())): indices[idx] = i return indices
def group_indices(self)
Return group indices
4.330039
4.100807
1.055899
@wraps(verb_func) def _verb_func(verb): verb.expressions, new_columns = build_expressions(verb) if add_groups: verb.groups = new_columns return verb_func(verb) return _verb_func
def _make_verb_helper(verb_func, add_groups=False)
Create function that prepares verb for the verb function The functions created add expressions to be evaluated to the verb, then call the core verb function Parameters ---------- verb_func : function Core verb function. This is the function called after expressions created and adde...
4.09805
3.572335
1.147163
if isinstance(df, GroupedDataFrame): base_df = GroupedDataFrame( df.loc[:, df.plydata_groups], df.plydata_groups, copy=True) else: base_df = pd.DataFrame(index=df.index) return base_df
def _get_base_dataframe(df)
Remove all columns other than those grouped on
5.131686
4.669199
1.09905
n = len(data) if isinstance(gdf, GroupedDataFrame): for i, col in enumerate(gdf.plydata_groups): if col not in data: group_values = [gdf[col].iloc[0]] * n # Need to be careful and maintain the dtypes # of the group columns ...
def _add_group_columns(data, gdf)
Add group columns to data with a value from the grouped dataframe It is assumed that the grouped dataframe contains a single group >>> data = pd.DataFrame({ ... 'x': [5, 6, 7]}) >>> gdf = GroupedDataFrame({ ... 'g': list('aaa'), ... 'x': range(3)}, groups=['g']) >>> _add_group_...
3.214729
3.555518
0.904152
with suppress(AttributeError): # If the index of a series and the dataframe # in which the series will be assigned to a # column do not match, missing values/NaNs # are created. We do not want that. if not value.index.equals(data.index): if len(value) == len(...
def _create_column(data, col, value)
Create column in dataframe Helper method meant to deal with problematic column values. e.g When the series index does not match that of the data. Parameters ---------- data : pandas.DataFrame dataframe in which to insert value col : column label Column name value : obje...
4.837102
5.252002
0.921002
def partial(func, col, *args, **kwargs): def new_func(gdf): return func(gdf[col], *args, **kwargs) return new_func def make_statement(func, col): if isinstance(func, str): expr = '{}({})'.format(func, col) elif callable(func): ...
def build_expressions(verb)
Build expressions for helper verbs Parameters ---------- verb : verb A verb with a *functions* attribute. Returns ------- out : tuple (List of Expressions, New columns). The expressions and the new columns in which the results of those expressions will be stored...
3.490683
3.391462
1.029256
# Short cut if self._all_expressions_evaluated(): if self.drop: # Drop extra columns. They do not correspond to # any expressions. columns = [expr.column for expr in self.expressions] self.data = self.data.loc[:, column...
def process(self)
Run the expressions Returns ------- out : pandas.DataFrame Resulting data
9.026324
8.600218
1.049546
def present(expr): return expr.stmt == expr.column and expr.column in self.data return all(present(expr) for expr in self.expressions)
def _all_expressions_evaluated(self)
Return True all expressions match with the columns Saves some processor cycles
9.616929
9.335732
1.03012
if isinstance(self.data, GroupedDataFrame): grouper = self.data.groupby() # groupby on categorical columns uses the categories # even if they are not present in the data. This # leads to empty groups. We exclude them. return (gdf for _, gdf in...
def _get_group_dataframes(self)
Get group dataframes Returns ------- out : tuple or generator Group dataframes
6.054814
6.041039
1.00228
gdf._is_copy = None result_index = gdf.index if self.keep_index else [] data = pd.DataFrame(index=result_index) for expr in self.expressions: value = expr.evaluate(gdf, self.env) if isinstance(value, pd.DataFrame): data = value ...
def _evaluate_group_dataframe(self, gdf)
Evaluate a single group dataframe Parameters ---------- gdf : pandas.DataFrame Input group dataframe Returns ------- out : pandas.DataFrame Result data
4.171311
4.832239
0.863225
egdfs = list(egdfs) edata = pd.concat(egdfs, axis=0, ignore_index=False, copy=False) # groupby can mixup the rows. We try to maintain the original # order, but we can only do that if the result has a one to # one relationship with the original one2one = ( ...
def _concat(self, egdfs)
Concatenate evaluated group dataframes Parameters ---------- egdfs : iterable Evaluated dataframes Returns ------- edata : pandas.DataFrame Evaluated data
4.730981
4.86245
0.972963
def _get_slice_cols(sc): # Just like pandas.DataFrame.loc the stop # column is included idx_start = data_columns.get_loc(sc.start) idx_stop = data_columns.get_loc(sc.stop) + 1 return data_columns[idx_start:idx_stop:sc.step] ...
def _resolve_slices(data_columns, names)
Convert any slices into column names Parameters ---------- data_columns : pandas.Index Dataframe columns names : tuple Names (including slices) of columns in the dataframe. Returns ------- out : tuple Names of colu...
3.222194
3.016267
1.068272
columns = verb.data.columns contains = verb.contains matches = verb.matches groups = _get_groups(verb) names = cls._resolve_slices(columns, verb.names) names_set = set(names) groups_set = set(groups) lst = [[]] if names or groups: ...
def select(cls, verb)
Return selected columns for the select verb Parameters ---------- verb : object verb with the column selection attributes: - names - startswith - endswith - contains - matches
2.716984
2.597303
1.046079
groups = set(_get_groups(verb)) return [col for col in verb.data if col not in groups]
def _all(cls, verb)
A verb
11.7691
11.830774
0.994787
# Named (listed) columns are always included columns = cls.select(verb) final_columns_set = set(cls.select(verb)) groups_set = set(_get_groups(verb)) final_columns_set -= groups_set - set(verb.names) def pred(col): if col not in verb.data: ...
def _at(cls, verb)
A verb with a select text match
6.512822
6.07697
1.071722
pred = verb.predicate data = verb.data groups = set(_get_groups(verb)) # force predicate if isinstance(pred, str): if not pred.endswith('_dtype'): pred = '{}_dtype'.format(pred) pred = getattr(pdtypes, pred) elif pdtypes.i...
def _if(cls, verb)
A verb with a predicate function
6.137596
5.739845
1.069297
try: module = type_lookup[type(data)] except KeyError: # Some guess work for subclasses for type_, mod in type_lookup.items(): if isinstance(data, type_): module = mod break try: return getattr(module, verb) except (NameErr...
def get_verb_function(data, verb)
Return function that implements the verb for given data type
4.492496
4.275864
1.050664
# dispatch if not hasattr(args[0], '_Expression'): return BaseExpression(*args, *kwargs) else: return args[0]._Expression(*args, **kwargs)
def Expression(*args, **kwargs)
Return an appropriate Expression given the arguments Parameters ---------- args : tuple Positional arguments passed to the Expression class kwargs : dict Keyword arguments passed to the Expression class
7.044723
6.09853
1.155151
def n(): return len(data) if isinstance(self.stmt, str): # Add function n() that computes the # size of the group data to the inner namespace. if self._has_n_func: namespace = dict(data, n=n) else: ...
def evaluate(self, data, env)
Evaluate statement Parameters ---------- data : pandas.DataFrame Data in whose namespace the statement will be evaluated. Typically, this is a group dataframe. Returns ------- out : object Result of the evaluation.pandas.DataFrame
8.204559
7.580857
1.082273
# For each predicate-value, we keep track of the positions # that have been copied to the result, so that the later # more general values do not overwrite the previous ones. result = np.repeat(None, len(data)) copied = np.repeat(False, len(data)) for pred_expr, v...
def evaluate(self, data, env)
Evaluate the predicates and values
5.348423
5.221165
1.024374
bool_idx = self.predicate_expr.evaluate(data, env) true_value = self.true_value_expr.evaluate(data, env) false_value = self.false_value_expr.evaluate(data, env) true_idx = np.where(bool_idx)[0] false_idx = np.where(~bool_idx)[0] result = np.repeat(None, len(data)...
def evaluate(self, data, env)
Evaluate the predicates and values
2.509588
2.482395
1.010954
return self.__class__(self._namespaces + [outer_namespace], self.flags)
def with_outer_namespace(self, outer_namespace)
Return a new EvalEnvironment with an extra namespace added. This namespace will be used only for variables that are not found in any existing namespace, i.e., it is "outside" them all.
10.739295
8.754438
1.226726
code = compile(expr, source_name, "eval", self.flags, False) return eval(code, {}, VarLookupDict([inner_namespace] + self._namespaces))
def eval(self, expr, source_name="<string>", inner_namespace={})
Evaluate some Python code in the encapsulated environment. :arg expr: A string containing a Python expression. :arg source_name: A name for this string, for use in tracebacks. :arg inner_namespace: A dict-like object that will be checked first when `expr` attempts to access any variabl...
8.80812
12.233155
0.72002
if isinstance(eval_env, cls): return eval_env elif isinstance(eval_env, numbers.Integral): depth = eval_env + reference else: raise TypeError("Parameter 'eval_env' must be either an integer " "or an instance of patsy.EvalEn...
def capture(cls, eval_env=0, reference=0)
Capture an execution environment from the stack. If `eval_env` is already an :class:`EvalEnvironment`, it is returned unchanged. Otherwise, we walk up the stack by ``eval_env + reference`` steps and capture that function's evaluation environment. For ``eval_env=0`` and ``reference=0``, t...
5.104238
4.97391
1.026202
vld = VarLookupDict(self._namespaces) new_ns = dict((name, vld[name]) for name in names) return EvalEnvironment([new_ns], self.flags)
def subset(self, names)
Creates a new, flat EvalEnvironment that contains only the variables specified.
12.554305
7.501159
1.673649
d[key] = value try: yield d finally: del d[key]
def temporary_key(d, key, value)
Context manager that removes key from dictionary on closing The dictionary will hold the key for the duration of the context. Parameters ---------- d : dict-like Dictionary in which to insert a temporary key. key : hashable Location at which to insert ``value``. value : obj...
3.285757
5.072637
0.647741
setattr(obj, name, value) try: yield obj finally: delattr(obj, name)
def temporary_attr(obj, name, value)
Context manager that removes key from dictionary on closing The dictionary will hold the key for the duration of the context. Parameters ---------- obj : object Object onto which to add a temporary attribute. name : str Name of attribute to add to ``obj``. value : object ...
2.818514
3.793048
0.743074
env = EvalEnvironment.capture(1) try: return env.namespace[name] except KeyError: raise NameError("No data named {!r} found".format(name))
def Q(name)
Quote a variable name A way to 'quote' variable names, especially ones that do not otherwise meet Python's variable name rules. Parameters ---------- name : str Name of variable Returns ------- value : object Value of variable Examples -------- >>> import ...
8.118253
12.806493
0.633917
original_index = [df.index for df in dfs] have_bad_index = [not isinstance(df.index, pd.RangeIndex) for df in dfs] for df, bad in zip(dfs, have_bad_index): if bad: df.reset_index(drop=True, inplace=True) try: yield dfs finally: for df,...
def regular_index(*dfs)
Change & restore the indices of dataframes Dataframe with duplicate values can be hard to work with. When split and recombined, you cannot restore the row order. This can be the case even if the index has unique but irregular/unordered. This contextmanager resets the unordered indices of any datafr...
2.488878
2.839272
0.87659
seen = set() def make_seen(x): seen.add(x) return x return [make_seen(x) for x in lst if x not in seen]
def unique(lst)
Return unique elements :class:`pandas.unique` and :class:`numpy.unique` cast mixed type lists to the same type. They are faster, but some times we want to maintain the type. Parameters ---------- lst : list-like List of items Returns ------- out : list Unique items...
3.058544
4.655399
0.656989
try: return arr.iloc[n] except (KeyError, IndexError): return np.nan
def _nth(arr, n)
Return the nth value of array If it is missing return NaN
4.036141
3.467903
1.163856
if frames is None and fps is None: return times_to_ms(h, m, s, ms) elif frames is not None and fps is not None: return frames_to_ms(frames, fps) else: raise ValueError("Both fps and frames must be specified")
def make_time(h=0, m=0, s=0, ms=0, frames=None, fps=None)
Convert time to milliseconds. See :func:`pysubs2.time.times_to_ms()`. When both frames and fps are specified, :func:`pysubs2.time.frames_to_ms()` is called instead. Raises: ValueError: Invalid fps, or one of frames/fps is missing. Example: >>> make_time(s=1.5) 1500 >>>...
2.482312
2.663802
0.931868
h, m, s, frac = map(int, groups) ms = frac * 10**(3 - len(groups[-1])) ms += s * 1000 ms += m * 60000 ms += h * 3600000 return ms
def timestamp_to_ms(groups)
Convert groups from :data:`pysubs2.time.TIMESTAMP` match to milliseconds. Example: >>> timestamp_to_ms(TIMESTAMP.match("0:00:00.42").groups()) 420
2.483495
2.911049
0.853127
ms += s * 1000 ms += m * 60000 ms += h * 3600000 return int(round(ms))
def times_to_ms(h=0, m=0, s=0, ms=0)
Convert hours, minutes, seconds to milliseconds. Arguments may be positive or negative, int or float, need not be normalized (``s=120`` is okay). Returns: Number of milliseconds (rounded to int).
2.146832
2.737198
0.784317
if fps <= 0: raise ValueError("Framerate must be positive number (%f)." % fps) return int(round(frames * (1000 / fps)))
def frames_to_ms(frames, fps)
Convert frame-based duration to milliseconds. Arguments: frames: Number of frames (should be int). fps: Framerate (must be a positive number, eg. 23.976). Returns: Number of milliseconds (rounded to int). Raises: ValueError: fps was negative or zero.
4.528205
4.666367
0.970392
if fps <= 0: raise ValueError("Framerate must be positive number (%f)." % fps) return int(round((ms / 1000) * fps))
def ms_to_frames(ms, fps)
Convert milliseconds to number of frames. Arguments: ms: Number of milliseconds (may be int, float or other numeric class). fps: Framerate (must be a positive number, eg. 23.976). Returns: Number of frames (int). Raises: ValueError: fps was negative or zero...
4.12071
4.430006
0.930182
ms = int(round(ms)) h, ms = divmod(ms, 3600000) m, ms = divmod(ms, 60000) s, ms = divmod(ms, 1000) return Times(h, m, s, ms)
def ms_to_times(ms)
Convert milliseconds to normalized tuple (h, m, s, ms). Arguments: ms: Number of milliseconds (may be int, float or other numeric class). Should be non-negative. Returns: Named tuple (h, m, s, ms) of ints. Invariants: ``ms in range(1000) and s in range(60) and m in ...
2.013486
2.057446
0.978634
sgn = "-" if ms < 0 else "" h, m, s, ms = ms_to_times(abs(ms)) if fractions: return sgn + "{:01d}:{:02d}:{:02d}.{:03d}".format(h, m, s, ms) else: return sgn + "{:01d}:{:02d}:{:02d}".format(h, m, s)
def ms_to_str(ms, fractions=False)
Prettyprint milliseconds to [-]H:MM:SS[.mmm] Handles huge and/or negative times. Non-negative times with ``fractions=True`` are matched by :data:`pysubs2.time.TIMESTAMP`. Arguments: ms: Number of milliseconds (int, float or other numeric class). fractions: Whether to print up to mi...
1.984356
2.231961
0.889064
# XXX throw on overflow/underflow? if ms < 0: ms = 0 if ms > MAX_REPRESENTABLE_TIME: ms = MAX_REPRESENTABLE_TIME h, m, s, ms = ms_to_times(ms) return "%01d:%02d:%02d.%02d" % (h, m, s, ms//10)
def ms_to_timestamp(ms)
Convert ms to 'H:MM:SS.cc
3.643121
3.636626
1.001786
fragments = SSAEvent.OVERRIDE_SEQUENCE.split(text) if len(fragments) == 1: return [(text, style)] def apply_overrides(all_overrides): s = style.copy() for tag in re.findall(r"\\[ibus][10]|\\r[a-zA-Z_0-9 ]*", all_overrides): if tag == r"\r": ...
def parse_tags(text, style=SSAStyle.DEFAULT_STYLE, styles={})
Split text into fragments with computed SSAStyles. Returns list of tuples (fragment, style), where fragment is a part of text between two brace-delimited override sequences, and style is the computed styling of the fragment, ie. the original style modified by all override sequences before the fragm...
3.771954
3.289493
1.146667
text = self.text text = self.OVERRIDE_SEQUENCE.sub("", text) text = text.replace(r"\h", " ") text = text.replace(r"\n", "\n") text = text.replace(r"\N", "\n") return text
def plaintext(self)
Subtitle text as multi-line string with no tags (read/write property). Writing to this property replaces :attr:`SSAEvent.text` with given plain text. Newlines are converted to ``\\N`` tags.
4.522697
4.334288
1.043469
delta = make_time(h=h, m=m, s=s, ms=ms, frames=frames, fps=fps) self.start += delta self.end += delta
def shift(self, h=0, m=0, s=0, ms=0, frames=None, fps=None)
Shift start and end times. See :meth:`SSAFile.shift()` for full description.
2.891431
3.192194
0.905782
if isinstance(other, SSAEvent): return self.as_dict() == other.as_dict() else: raise TypeError("Cannot compare to non-SSAEvent object")
def equals(self, other)
Field-based equality for SSAEvents.
4.458555
2.835731
1.572277
with open(path, encoding=encoding) as fp: return cls.from_file(fp, format_, fps=fps, **kwargs)
def load(cls, path, encoding="utf-8", format_=None, fps=None, **kwargs)
Load subtitle file from given path. Arguments: path (str): Path to subtitle file. encoding (str): Character encoding of input file. Defaults to UTF-8, you may need to change this. format_ (str): Optional, forces use of specific parser (eg. `"s...
2.821504
6.601945
0.427375
fp = io.StringIO(string) return cls.from_file(fp, format_, fps=fps, **kwargs)
def from_string(cls, string, format_=None, fps=None, **kwargs)
Load subtitle file from string. See :meth:`SSAFile.load()` for full description. Arguments: string (str): Subtitle file in a string. Note that the string must be Unicode (in Python 2). Returns: SSAFile Example: >>> text = ''' ...
3.452757
5.655931
0.610467
if format_ is None: # Autodetect subtitle format, then read again using correct parser. # The file might be a pipe and we need to read it twice, # so just buffer everything. text = fp.read() fragment = text[:10000] format_ = autode...
def from_file(cls, fp, format_=None, fps=None, **kwargs)
Read subtitle file from file object. See :meth:`SSAFile.load()` for full description. Note: This is a low-level method. Usually, one of :meth:`SSAFile.load()` or :meth:`SSAFile.from_string()` is preferable. Arguments: fp (file object): A file object, ie. :c...
5.906202
6.110672
0.966539
if format_ is None: ext = os.path.splitext(path)[1].lower() format_ = get_format_identifier(ext) with open(path, "w", encoding=encoding) as fp: self.to_file(fp, format_, fps=fps, **kwargs)
def save(self, path, encoding="utf-8", format_=None, fps=None, **kwargs)
Save subtitle file to given path. Arguments: path (str): Path to subtitle file. encoding (str): Character encoding of output file. Defaults to UTF-8, which should be fine for most purposes. format_ (str): Optional, specifies desired subtitle format ...
2.648534
2.713807
0.975948
fp = io.StringIO() self.to_file(fp, format_, fps=fps, **kwargs) return fp.getvalue()
def to_string(self, format_, fps=None, **kwargs)
Get subtitle file as a string. See :meth:`SSAFile.save()` for full description. Returns: str
2.903417
4.49524
0.645887
impl = get_format_class(format_) impl.to_file(self, fp, format_, fps=fps, **kwargs)
def to_file(self, fp, format_, fps=None, **kwargs)
Write subtitle file to file object. See :meth:`SSAFile.save()` for full description. Note: This is a low-level method. Usually, one of :meth:`SSAFile.save()` or :meth:`SSAFile.to_string()` is preferable. Arguments: fp (file object): A file object, ie. :clas...
4.253644
6.884232
0.617882
if in_fps <= 0 or out_fps <= 0: raise ValueError("Framerates must be positive, cannot transform %f -> %f" % (in_fps, out_fps)) ratio = in_fps / out_fps for line in self: line.start = int(round(line.start * ratio)) line.end = int(round(line.end * rati...
def transform_framerate(self, in_fps, out_fps)
Rescale all timestamps by ratio of in_fps/out_fps. Can be used to fix files converted from frame-based to time-based with wrongly assumed framerate. Arguments: in_fps (float) out_fps (float) Raises: ValueError: Non-positive framerate given.
2.525565
2.763763
0.913814
if old_name not in self.styles: raise KeyError("Style %r not found" % old_name) if new_name in self.styles: raise ValueError("There is already a style called %r" % new_name) if not is_valid_field_content(new_name): raise ValueError("%r is not a valid ...
def rename_style(self, old_name, new_name)
Rename a style, including references to it. Arguments: old_name (str): Style to be renamed. new_name (str): New name for the style (must be unused). Raises: KeyError: No style named old_name. ValueError: new_name is not a legal name (cannot use commas) ...
2.825773
2.962086
0.95398
if not isinstance(subs, SSAFile): raise TypeError("Must supply an SSAFile.") for name, style in subs.styles.items(): if name not in self.styles or overwrite: self.styles[name] = style
def import_styles(self, subs, overwrite=True)
Merge in styles from other SSAFile. Arguments: subs (SSAFile): Subtitle file imported from. overwrite (bool): On name conflict, use style from the other file (default: True).
3.330021
2.928171
1.137236
if isinstance(other, SSAFile): for key in set(chain(self.info.keys(), other.info.keys())) - {"ScriptType"}: sv, ov = self.info.get(key), other.info.get(key) if sv is None: logging.debug("%r missing in self.info", key) ...
def equals(self, other)
Equality of two SSAFiles. Compares :attr:`SSAFile.info`, :attr:`SSAFile.styles` and :attr:`SSAFile.events`. Order of entries in OrderedDicts does not matter. "ScriptType" key in info is considered an implementation detail and thus ignored. Useful mostly in unit tests. Differences are l...
1.982447
1.730492
1.145597
if format_ not in FORMAT_IDENTIFIER_TO_FORMAT_CLASS: raise UnknownFormatIdentifierError(format_) for ext, f in FILE_EXTENSION_TO_FORMAT_IDENTIFIER.items(): if f == format_: return ext raise RuntimeError("No file extension for format %r" % format_)
def get_file_extension(format_)
Format identifier -> file extension
4.21907
4.00032
1.054683
formats = set() for impl in FORMAT_IDENTIFIER_TO_FORMAT_CLASS.values(): guess = impl.guess_format(content) if guess is not None: formats.add(guess) if len(formats) == 1: return formats.pop() elif not formats: raise FormatAutodetectionError("No suitable f...
def autodetect_format(content)
Return format identifier for given fragment or raise FormatAutodetectionError.
3.059107
2.591321
1.18052
app._debug = True dft_logger.debug('livereload enabled: %s', '✓' if config.livereload else '✖') def get_host(request): if config.infer_host: return request.headers.get('host', 'localhost').split(':', 1)[0] else: return config.host if config.livereload: ...
def modify_main_app(app, config: Config)
Modify the app we're serving to make development easier, eg. * modify responses to add the livereload snippet * set ``static_root_url`` on the app * setup the debug toolbar
3.527499
3.340269
1.056053
cli_count = len(app[WS]) if cli_count == 0: return 0 is_html = None if path: path = str(Path(app['static_url']) / Path(path).relative_to(app['static_path'])) is_html = mimetypes.guess_type(path)[0] == 'text/html' reloads = 0 aux_logger.debug('prompting source reloa...
async def src_reload(app, path: str = None)
prompt each connected browser to reload by sending websocket message. :param path: if supplied this must be a path relative to app['static_path'], eg. reload of a single file is only supported for static resources. :return: number of sources reloaded
3.741135
3.410756
1.096864
filename = URL.build(path=request.match_info['filename'], encoded=True).path raw_path = self._directory.joinpath(filename) try: filepath = raw_path.resolve() if not filepath.exists(): # simulate strict=True for python 3.6 which is not permitted wi...
def modify_request(self, request)
Apply common path conventions eg. / > /index.html, /foobar > /foobar.html
3.584155
3.362305
1.065981
for attr_name in dir(self): if attr_name.startswith('_') or attr_name.upper() != attr_name: continue orig_value = getattr(self, attr_name) is_required = isinstance(orig_value, Required) orig_type = orig_value.v_type if is_required else ty...
def substitute_environ(self)
Substitute environment variables into settings.
3.046323
2.941597
1.035602
settings = Settings() conn = psycopg2.connect( password=settings.DB_PASSWORD, host=settings.DB_HOST, port=settings.DB_PORT, user=settings.DB_USER, ) conn.autocommit = True cur = conn.cursor() db_name = settings.DB_NAME cur.execute('SELECT EXISTS (SELECT ...
def prepare_database(delete_existing: bool) -> bool
(Re)create a fresh database and run migrations. :param delete_existing: whether or not to drop an existing database if it exists :return: whether or not a database has been (re)created
2.302294
2.275527
1.011763
# {% if database.is_none and example.is_message_board %} # app.router allows us to generate urls based on their names, # see http://aiohttp.readthedocs.io/en/stable/web.html#reverse-url-constructing-using-named-resources message_url = request.app.router['messages'].url_for() ctx = dict( ...
async def index(request)
This is the view handler for the "/" url. **Note: returning html without a template engine like jinja2 is ugly, no way around that.** :param request: the request object see http://aiohttp.readthedocs.io/en/stable/web_reference.html#request :return: aiohttp.web.Response object
5.454116
5.43157
1.004151
messages = [] # {% if database.is_none %} if request.app['settings'].MESSAGE_FILE.exists(): # read the message file, process it and populate the "messages" list with request.app['settings'].MESSAGE_FILE.open() as msg_file: for line in msg_file: if not line: ...
async def message_data(request)
As an example of aiohttp providing a non-html response, we load the actual messages for the "messages" view above via ajax using this endpoint to get data. see static/message_display.js for details of rendering.
3.277727
3.136764
1.044939
return str(URL( database=settings.DB_NAME, password=settings.DB_PASSWORD, host=settings.DB_HOST, port=settings.DB_PORT, username=settings.DB_USER, drivername='postgres', ))
def pg_dsn(settings: Settings) -> str
:param settings: settings including connection settings :return: DSN url suitable for sqlalchemy and aiopg.
2.994002
2.516082
1.189946
setup_logging(verbose) run_app(*serve_static(static_path=path, livereload=livereload, port=port))
def serve(path, livereload, port, verbose)
Serve static files from a directory.
6.48475
6.474376
1.001602
active_config = {k: v for k, v in config.items() if v is not None} setup_logging(config['verbose']) try: run_app(*_runserver(**active_config)) except AiohttpDevException as e: if config['verbose']: tb = click.style(traceback.format_exc().strip('\n'), fg='white', dim=True...
def runserver(**config)
Run a development server for an aiohttp apps. Takes one argument "app-path" which should be a path to either a directory containing a recognized default file ("app.py" or "main.py") or to a specific file. Defaults to the environment variable "AIO_APP_PATH" or ".". The app path is run directly, see the "--...
4.193582
4.497646
0.932395
setup_logging(verbose) try: check_dir_clean(Path(path)) if name is None: name = Path(path).name for kwarg_name, choice_enum in DECISIONS: docs = dedent(choice_enum.__doc__).split('\n') title, *help_text = filter(bool, docs) click.sech...
def start(*, path, name, verbose, **kwargs)
Create a new aiohttp app.
4.366308
4.261542
1.024584
rel_py_file = self.py_file.relative_to(self.python_path) module_path = '.'.join(rel_py_file.with_suffix('').parts) sys.path.append(str(self.python_path)) try: module = import_module(module_path) except ImportError as e: raise AdevConfigError('err...
def import_app_factory(self)
Import attribute/class from from a python module. Raise AdevConfigError if the import failed. :return: (attribute, Path object for directory of file)
2.813577
2.618619
1.07445
# force a full reload in sub processes so they load an updated version of code, this must be called only once set_start_method('spawn') config = Config(**config_kwargs) config.import_app_factory() loop = asyncio.get_event_loop() loop.run_until_complete(check_port_open(config.main_port, lo...
def runserver(**config_kwargs)
Prepare app ready to run development server. :param config_kwargs: see config.Config for more details :return: tuple (auxiliary app, auxiliary app port, event loop)
4.040883
3.900947
1.035872
log_level = 'DEBUG' if verbose else 'INFO' return { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'default': { 'format': '[%(asctime)s] %(message)s', 'datefmt': '%H:%M:%S', 'class': 'aiohttp_devtools.logs...
def log_config(verbose: bool) -> dict
Setup default config. for dictConfig. :param verbose: level: DEBUG if True, INFO if False :return: dict suitable for ``logging.config.dictConfig``
1.612044
1.612017
1.000016
def _scenario(func, *args, **kw): _check_coroutine(func) if weight > 0: sname = name or func.__name__ data = {'name': sname, 'weight': weight, 'delay': delay, 'func': func, 'args': args, 'kw': kw} _SCENARIO[sname] = dat...
def scenario(weight=1, delay=0.0, name=None)
Decorator to register a function as a Molotov test. Options: - **weight** used by Molotov when the scenarii are randomly picked. The functions with the highest values are more likely to be picked. Integer, defaults to 1. This value is ignored when the *scenario_picker* decorator is used. ...
2.643283
3.377786
0.782549
req = functools.partial(_request, endpoint, verb, session_options, **options) return _run_in_fresh_loop(req)
def request(endpoint, verb='GET', session_options=None, **options)
Performs a synchronous request. Uses a dedicated event loop and aiohttp.ClientSession object. Options: - endpoint: the endpoint to call - verb: the HTTP verb to use (defaults: GET) - session_options: a dict containing options to initialize the session (defaults: None) - options: extra o...
7.09925
11.811403
0.601051
req = functools.partial(_request, endpoint, verb, session_options, json=True, **options) return _run_in_fresh_loop(req)
def json_request(endpoint, verb='GET', session_options=None, **options)
Like :func:`molotov.request` but extracts json from the response.
6.508356
5.265905
1.235942
if name not in _VARS and factory is not None: _VARS[name] = factory() return _VARS.get(name)
def get_var(name, factory=None)
Gets a global variable given its name. If factory is not None and the variable is not set, factory is a callable that will set the variable. If not set, returns None.
3.204886
4.024689
0.796307
if scenario is None: scenario = pick_scenario(self.wid, step_id) try: await self.send_event('scenario_start', scenario=scenario) await scenario['func'](session, *scenario['args'], **scenario['kw']) await self.s...
async def step(self, step_id, session, scenario=None)
single scenario call. When it returns 1, it works. -1 the script failed, 0 the test is stopping or needs to stop.
4.013882
3.922611
1.023268
parser = argparse.ArgumentParser(description='Github-based load test') parser.add_argument('--version', action='store_true', default=False, help='Displays version and exits.') parser.add_argument('--virtualenv', type=str, default='virtualenv', help='Vir...
def main()
Moloslave clones a git repo and runs a molotov test
2.386853
2.243498
1.063898
errors = [] def onerror(function, path, excinfo): if show_warnings: print 'Cannot delete %s: %s' % (os.path.relpath(directory), excinfo[1]) errors.append((function, path, excinfo)) if os.path.exists(directory): if not os.path.isdir(directory): raise Not...
def remove_directory(directory, show_warnings=True)
Deletes a directory and its contents. Returns a list of errors in form (function, path, excinfo).
2.522339
2.152481
1.171828
try: os.makedirs(target_directory) except: # TODO: specific exception? pass for f in source_files: source = os.path.join(source_directory, f) if source_directory else f target = os.path.join(target_directory, f) shutil.copy2(source, target)
def copy_files(source_files, target_directory, source_directory=None)
Copies a list of files to the specified directory. If source_directory is provided, it will be prepended to each source file.
2.284193
2.317869
0.985471
while True: print message, '(yes/no)', line = raw_input() if line is None: return None line = line.lower() if line == 'y' or line == 'ye' or line == 'yes': return True if line == 'n' or line == 'no': return False
def yes_or_no(message)
Gets user input and returns True for yes and False for no.
2.455924
2.402636
1.022179