code
string
signature
string
docstring
string
loss_without_docstring
float64
loss_with_docstring
float64
factor
float64
seed = None result_lambda = identity if len(args) == 1: func = args[0] elif len(args) == 2: seed = args[0] func = args[1] elif len(args) == 3: seed = args[0] func = args[1] result_lambda = args[2] ...
def aggregate(self, *args)
Aggregates the sequence by specified arguments. Its behavior varies depending on if one, two, or three arguments are passed. Assuming the type of the sequence is A: One Argument: argument specifies a function of the type f(current: B, next: A => result: B. current represents results computed so...
2.422601
2.305687
1.050707
result = zero_value for element in self: result = func(result, element) return _wrap(result)
def fold_left(self, zero_value, func)
Assuming that the sequence elements are of type A, folds from left to right starting with the seed value given by zero_value (of type A) using a function of type func(current: B, next: A) => B. current represents the folded value so far and next is the next element from the sequence to fold into...
4.157572
6.06688
0.68529
result = zero_value for element in self.reverse(): result = func(element, result) return _wrap(result)
def fold_right(self, zero_value, func)
Assuming that the sequence elements are of type A, folds from right to left starting with the seed value given by zero_value (of type A) using a function of type func(next: A, current: B) => B. current represents the folded value so far and next is the next element from the sequence to fold into...
4.9834
6.862059
0.726225
return self._transform(transformations.join_t(other, join_type))
def join(self, other, join_type="inner")
Sequence and other must be composed of (Key, Value) pairs. If self.sequence contains (K, V) pairs and other contains (K, W) pairs, the return result is a sequence of (K, (V, W)) pairs. If join_type is "left", V values will always be present, W values may be present or None. If join_type is "righ...
13.386565
27.007122
0.495668
return self._transform(transformations.sliding_t(_wrap, size, step))
def sliding(self, size, step=1)
Groups elements in fixed size blocks by passing a sliding window over them. The last window has at least one element but may have less than size elements :param size: size of sliding window :param step: step size between windows :return: sequence of sliding windows
23.199642
37.212627
0.623435
return self._transform(transformations.sorted_t(key=key, reverse=reverse))
def sorted(self, key=None, reverse=False)
Uses python sort and its passed arguments to sort the input. >>> seq([2, 1, 4, 3]).sorted() [1, 2, 3, 4] :param key: sort using key function :param reverse: return list reversed or not :return: sorted sequence
10.662026
18.46723
0.577348
return self._transform(transformations.slice_t(start, until))
def slice(self, start, until)
Takes a slice of the sequence starting at start and until but not including until. >>> seq([1, 2, 3, 4]).slice(1, 2) [2] >>> seq([1, 2, 3, 4]).slice(1, 3) [2, 3] :param start: starting index :param until: ending index :return: slice including start until but not...
16.876434
29.917587
0.564097
if n is None: self.cache() return self._base_sequence else: return self.cache().take(n).list()
def to_list(self, n=None)
Converts sequence to list of elements. >>> type(seq([]).to_list()) list >>> type(seq([])) functional.pipeline.Sequence >>> seq([1, 2, 3]).to_list() [1, 2, 3] :param n: Take n elements of sequence if not None :return: list of elements in sequence
8.30476
8.162253
1.017459
dictionary = {} for e in self.sequence: dictionary[e[0]] = e[1] if default is None: return dictionary else: if hasattr(default, '__call__'): return collections.defaultdict(default, dictionary) else: ...
def to_dict(self, default=None)
Converts sequence of (Key, Value) pairs to a dictionary. >>> type(seq([('a', 1)]).to_dict()) dict >>> seq([('a', 1), ('b', 2)]).to_dict() {'a': 1, 'b': 2} :param default: Can be a callable zero argument function. When not None, the returned dictionary is a collecti...
2.760586
2.452229
1.125746
with universal_write_open(path, mode=mode, buffering=buffering, encoding=encoding, errors=errors, newline=newline, compression=compression, compresslevel=compresslevel, format=format, check=check, pres...
def to_file(self, path, delimiter=None, mode='wt', buffering=-1, encoding=None, errors=None, newline=None, compresslevel=9, format=None, check=-1, preset=None, filters=None, compression=None)
Saves the sequence to a file by executing str(self) which becomes str(self.to_list()). If delimiter is defined will instead execute self.make_string(delimiter) :param path: path to write file :param delimiter: if defined, will call make_string(delimiter) and save that to file. :param mo...
2.607097
2.355893
1.106628
with universal_write_open(path, mode=mode, compression=compression) as output: output.write((self.map(json.dumps).make_string('\n') + '\n').encode('utf-8'))
def to_jsonl(self, path, mode='wb', compression=None)
Saves the sequence to a jsonl file. Each element is mapped using json.dumps then written with a newline separating each element. :param path: path to write file :param mode: mode to write in, defaults to 'w' to overwrite contents :param compression: compression format
5.743804
7.070858
0.812321
with universal_write_open(path, mode=mode, compression=compression) as output: if root_array: json.dump(self.to_list(), output) else: json.dump(self.to_dict(), output)
def to_json(self, path, root_array=True, mode=WRITE_MODE, compression=None)
Saves the sequence to a json file. If root_array is True, then the sequence will be written to json with an array at the root. If it is False, then the sequence will be converted from a sequence of (Key, Value) pairs to a dictionary so that the json root is a dictionary. :param path: path to wr...
2.751717
3.907638
0.704189
if 'b' in mode: newline = None with universal_write_open(path, mode=mode, compression=compression, newline=newline) as output: csv_writer = csv.writer(output, dialect=dialect, **fmtparams) for row in self: c...
def to_csv(self, path, mode=WRITE_MODE, dialect='excel', compression=None, newline='', **fmtparams)
Saves the sequence to a csv file. Each element should be an iterable which will be expanded to the elements of each row. :param path: path to write file :param mode: file open mode :param dialect: passed to csv.writer :param fmtparams: passed to csv.writer
3.338326
3.494295
0.955365
def _insert_item(item): if isinstance(item, dict): cols = ', '.join(item.keys()) placeholders = ', '.join('?' * len(item)) sql = 'INSERT INTO {} ({}) VALUES ({})'.format(table_name, cols, placeholders) conn.execute(sql, tuple(i...
def _to_sqlite3_by_table(self, conn, table_name)
Saves the sequence to the specified table of sqlite3 database. Each element can be a dictionary, namedtuple, tuple or list. Target table must be created in advance. :param conn: path or sqlite connection, cursor :param table_name: table name string
1.850116
1.753103
1.055338
# pylint: disable=no-member insert_regex = re.compile(r'(insert|update)\s+into', flags=re.IGNORECASE) if insert_regex.match(target): insert_f = self._to_sqlite3_by_query else: insert_f = self._to_sqlite3_by_table if isinstance(conn, (sqlite3.Conn...
def to_sqlite3(self, conn, target, *args, **kwargs)
Saves the sequence to sqlite3 database. Target table must be created in advance. The table schema is inferred from the elements in the sequence if only target table name is supplied. >>> seq([(1, 'Tom'), (2, 'Jack')])\ .to_sqlite3('users.db', 'INSERT INTO user (id, name)...
2.806195
2.84591
0.986045
# pylint: disable=import-error import pandas return pandas.DataFrame.from_records(self.to_list(), columns=columns)
def to_pandas(self, columns=None)
Converts sequence to a pandas DataFrame using pandas.DataFrame.from_records :param columns: columns for pandas to use :return: DataFrame of sequence
5.235932
5.616266
0.93228
formatted_seq = self.tabulate(n=n, headers=headers, tablefmt=tablefmt, floatfmt=floatfmt, numalign=numalign, stralign=stralign, missingval=missingval) print(formatted_seq)
def show(self, n=10, headers=(), tablefmt="simple", floatfmt="g", numalign="decimal", stralign="left", missingval="")
Pretty print first n rows of sequence as a table. See https://bitbucket.org/astanin/python-tabulate for details on tabulate parameters :param n: Number of rows to show :param headers: Passed to tabulate :param tablefmt: Passed to tabulate :param floatfmt: Passed to tabulate ...
2.441915
2.500402
0.976609
self.cache() length = self.len() if length == 0 or not is_tabulatable(self[0]): return None if n is None or n >= length: rows = self.list() message = '' else: rows = self.take(n).list() if tablefmt == 'simple':...
def tabulate(self, n=None, headers=(), tablefmt="simple", floatfmt="g", numalign="decimal", stralign="left", missingval="")
Return pretty string table of first n rows of sequence or everything if n is None. See https://bitbucket.org/astanin/python-tabulate for details on tabulate parameters :param n: Number of rows to show, if set to None return all rows :param headers: Passed to tabulate :param tablefmt: Pa...
2.740083
2.728037
1.004415
if not re.match('^[rbt]{1,3}$', mode): raise ValueError('mode argument must be only have r, b, and t') file_open = get_read_function(path, self.disable_compression) file = file_open(path, mode=mode, buffering=buffering, encoding=encoding, errors=errors, ...
def open(self, path, delimiter=None, mode='r', buffering=-1, encoding=None, errors=None, newline=None)
Reads and parses input files as defined. If delimiter is not None, then the file is read in bulk then split on it. If it is None (the default), then the file is parsed as sequence of lines. The rest of the options are passed directly to builtins.open with the exception that write/append file mo...
4.472818
4.743728
0.942891
if isinstance(csv_file, str): file_open = get_read_function(csv_file, self.disable_compression) input_file = file_open(csv_file) elif hasattr(csv_file, 'next') or hasattr(csv_file, '__next__'): input_file = csv_file else: raise ValueError(...
def csv(self, csv_file, dialect='excel', **fmt_params)
Reads and parses the input of a csv stream or file. csv_file can be a filepath or an object that implements the iterator interface (defines next() or __next__() depending on python version). >>> seq.csv('examples/camping_purchases.csv').take(2) [['1', 'tent', '300'], ['2', 'food', '100...
3.983573
3.722121
1.070243
if isinstance(jsonl_file, str): file_open = get_read_function(jsonl_file, self.disable_compression) input_file = file_open(jsonl_file) else: input_file = jsonl_file return self(input_file).map(jsonapi.loads).cache(delete_lineage=True)
def jsonl(self, jsonl_file)
Reads and parses the input of a jsonl file stream or file. Jsonl formatted files must have a single valid json value on each line which is parsed by the python json module. >>> seq.jsonl('examples/chat_logs.jsonl').first() {u'date': u'10/09', u'message': u'hello anyone there?', u'user'...
5.463872
5.703357
0.95801
if isinstance(json_file, str): file_open = get_read_function(json_file, self.disable_compression) input_file = file_open(json_file) json_input = jsonapi.load(input_file) elif hasattr(json_file, 'read'): json_input = jsonapi.load(json_file) ...
def json(self, json_file)
Reads and parses the input of a json file handler or file. Json files are parsed differently depending on if the root is a dictionary or an array. 1) If the json's root is a dictionary, these are parsed into a sequence of (Key, Value) pairs 2) If the json's root is an array, these are...
3.371081
3.499129
0.963406
if parameters is None: parameters = () if isinstance(conn, (sqlite3api.Connection, sqlite3api.Cursor)): return self(conn.execute(sql, parameters)) elif isinstance(conn, str): with sqlite3api.connect(conn, *args, **kwargs) as input_conn: ...
def sqlite3(self, conn, sql, parameters=None, *args, **kwargs)
Reads input by querying from a sqlite database. >>> seq.sqlite3('examples/users.db', 'select id, name from users where id = 1;').first() [(1, 'Tom')] :param conn: path or sqlite connection, cursor :param sql: SQL query string :param parameters: Parameters for sql query ...
3.109305
3.17429
0.979528
'''Indicate that a formerly enqueued task is complete. Used by Queue consumer threads. For each get() used to fetch a task, a subsequent call to task_done() tells the queue that the processing on the task is complete. If a join() is currently blocking, it will resume when all ...
def task_done(self)
Indicate that a formerly enqueued task is complete. Used by Queue consumer threads. For each get() used to fetch a task, a subsequent call to task_done() tells the queue that the processing on the task is complete. If a join() is currently blocking, it will resume when all items ...
2.347194
1.710323
1.372369
'''Blocks until all items in the Queue have been gotten and processed. The count of unfinished tasks goes up whenever an item is added to the queue. The count goes down whenever a consumer thread calls task_done() to indicate the item was retrieved and all work on it is complete. ...
def join(self)
Blocks until all items in the Queue have been gotten and processed. The count of unfinished tasks goes up whenever an item is added to the queue. The count goes down whenever a consumer thread calls task_done() to indicate the item was retrieved and all work on it is complete. When the...
4.400999
1.915472
2.297606
'''Put an item into the queue. If optional args 'block' is true and 'timeout' is None (the default), block if necessary until a free slot is available. If 'timeout' is a non-negative number, it blocks at most 'timeout' seconds and raises the Full exception if no free slot was av...
def put(self, item, block=True, timeout=None)
Put an item into the queue. If optional args 'block' is true and 'timeout' is None (the default), block if necessary until a free slot is available. If 'timeout' is a non-negative number, it blocks at most 'timeout' seconds and raises the Full exception if no free slot was available wit...
1.886962
1.788867
1.054836
'''Remove and return an item from the queue. If optional args 'block' is true and 'timeout' is None (the default), block if necessary until an item is available. If 'timeout' is a non-negative number, it blocks at most 'timeout' seconds and raises the Empty exception if no item ...
def get(self, block=True, timeout=None)
Remove and return an item from the queue. If optional args 'block' is true and 'timeout' is None (the default), block if necessary until an item is available. If 'timeout' is a non-negative number, it blocks at most 'timeout' seconds and raises the Empty exception if no item was availab...
1.963896
1.868838
1.050865
if self._parent._maxsize <= 0: return False else: return self.qsize() >= self._parent._maxsize
def full(self)
Return True if there are maxsize items in the queue. Note: if the Queue was initialized with maxsize=0 (the default), then full() is never True.
6.116776
4.052619
1.509339
self._parent._check_closing() async with self._parent._async_not_full: self._parent._sync_mutex.acquire() locked = True try: if self._parent._maxsize > 0: do_wait = True while do_wait: ...
async def put(self, item)
Put an item into the queue. Put an item into the queue. If the queue is full, wait until a free slot is available before adding item. This method is a coroutine.
2.875744
2.758241
1.042601
self._parent._check_closing() with self._parent._sync_mutex: if self._parent._maxsize > 0: if self._parent._qsize() >= self._parent._maxsize: raise AsyncQueueFull self._parent._put_internal(item) self._parent._notify_async...
def put_nowait(self, item)
Put an item into the queue without blocking. If no free slot is immediately available, raise QueueFull.
4.301486
4.350421
0.988752
self._parent._check_closing() async with self._parent._async_not_empty: self._parent._sync_mutex.acquire() locked = True try: do_wait = True while do_wait: do_wait = self._parent._qsize() == 0 ...
async def get(self)
Remove and return an item from the queue. If queue is empty, wait until an item is available. This method is a coroutine.
2.917941
2.783173
1.048422
self._parent._check_closing() with self._parent._sync_mutex: if self._parent._qsize() == 0: raise AsyncQueueEmpty item = self._parent._get() self._parent._notify_async_not_full(threadsafe=False) self._parent._notify_sync_not_full(...
def get_nowait(self)
Remove and return an item from the queue. Return an item if one is immediately available, else raise QueueEmpty.
5.507442
5.224546
1.054147
self._parent._check_closing() with self._parent._all_tasks_done: if self._parent._unfinished_tasks <= 0: raise ValueError('task_done() called too many times') self._parent._unfinished_tasks -= 1 if self._parent._unfinished_tasks == 0: ...
def task_done(self)
Indicate that a formerly enqueued task is complete. Used by queue consumers. For each get() used to fetch a task, a subsequent call to task_done() tells the queue that the processing on the task is complete. If a join() is currently blocking, it will resume when all items have ...
2.82287
2.642725
1.068166
while True: with self._parent._sync_mutex: if self._parent._unfinished_tasks == 0: break await self._parent._finished.wait()
async def join(self)
Block until all items in the queue have been gotten and processed. The count of unfinished tasks goes up whenever an item is added to the queue. The count goes down whenever a consumer calls task_done() to indicate that the item was retrieved and all work on it is complete. When the cou...
6.401218
6.847601
0.934812
u if os.path.exists(to_dir) and not os.path.isdir(to_dir): raise Exception('Not a directory : %s' % to_dir) elif not os.path.exists(to_dir): os.makedirs(to_dir, mode=int('0755', 8)) _save(os.path.join(to_dir, FILE_USER_FST_DATA), self.compiledFST[0], compressionle...
def save(self, to_dir, compressionlevel=9)
u""" Save compressed compiled dictionary data. :param to_dir: directory to save dictionary data :compressionlevel: (Optional) gzip compression level. default is 9
3.146994
3.117983
1.009305
u for cfilter in self.char_filters: text = cfilter.filter(text) tokens = self.tokenizer.tokenize(text, stream=True, wakati=False) for tfilter in self.token_filters: tokens = tfilter.filter(tokens) return tokens
def analyze(self, text)
u""" Analyze the input text with custom CharFilters, Tokenizer and TokenFilters. :param text: unicode string to be tokenized :return: token generator. emitted element type depends on the output of the last TokenFilter. (e.g., ExtractAttributeFilter emits strings.)
4.586222
3.916214
1.171086
u arcs = [] address = {} pos = 0 for (num, s) in enumerate(fst.dictionary.values()): for i, (c, v) in enumerate(sorted(s.trans_map.items(), reverse=True)): bary = bytearray() flag = 0 output_size, output = 0, bytes() if i == 0: ...
def compileFST(fst)
u""" convert FST to byte array representing arcs
2.981872
2.892785
1.030796
u if self.wakati: wakati = True if stream: return self.__tokenize_stream(text, wakati, baseform_unk, '') elif dotfile and len(text) < Tokenizer.MAX_CHUNK_SIZE: return list(self.__tokenize_stream(text, wakati, baseform_unk, dotfile)) else: ...
def tokenize(self, text, stream=False, wakati=False, baseform_unk=True, dotfile='')
u""" Tokenize the input text. :param text: unicode string to be tokenized :param stream: (Optional) if given True use stream mode. default is False. :param wakati: (Optinal) if given True returns surface forms only. default is False. :param baseform_unk: (Optional) if given True...
3.122544
2.869808
1.088067
best_matched_ip = None warnings.warn('get_ip is deprecated and will be removed in 3.0.', DeprecationWarning) for key in defs.IPWARE_META_PRECEDENCE_ORDER: value = request.META.get(key, request.META.get(key.replace('_', '-'), '')).strip() if value is not None and value != '': ...
def get_ip(request, real_ip_only=False, right_most_proxy=False)
Returns client's best-matched ip-address, or None @deprecated - Do not edit
2.821451
2.69591
1.046567
warnings.warn('get_real_ip is deprecated and will be removed in 3.0.', DeprecationWarning) return get_ip(request, real_ip_only=True, right_most_proxy=right_most_proxy)
def get_real_ip(request, right_most_proxy=False)
Returns client's best-matched `real` `externally-routable` ip-address, or None @deprecated - Do not edit
3.05225
2.877373
1.060776
warnings.warn('get_trusted_ip is deprecated and will be removed in 3.0.', DeprecationWarning) if trusted_proxies: meta_keys = ['HTTP_X_FORWARDED_FOR', 'X_FORWARDED_FOR'] for key in meta_keys: value = request.META.get(key, request.META.get(key.replace('_', '-'), '')).strip() ...
def get_trusted_ip(request, right_most_proxy=False, trusted_proxies=TRUSTED_PROXY_LIST)
Returns client's ip-address from `trusted` proxy server(s) or None @deprecated - Do not edit
2.493304
2.41139
1.03397
try: socket.inet_pton(socket.AF_INET6, ip_str) except socket.error: return False return True
def is_valid_ipv6(ip_str)
Check the validity of an IPv6 address
1.804377
1.840107
0.980583
value = request.META.get(key, request.META.get(key.replace('_', '-'), '')).strip() if value == '': return None return value
def get_request_meta(request, key)
Given a key, it returns a cleaned up version of the value from request.META, or None
3.352666
3.425511
0.978735
ip_list = [] for ip in ip_str.split(','): clean_ip = ip.strip().lower() if clean_ip: ip_list.append(clean_ip) ip_count = len(ip_list) if ip_count > 0: if is_valid_ip(ip_list[0]) and is_valid_ip(ip_list[-1]): return ip_list, ip_count return [], ...
def get_ips_from_string(ip_str)
Given a string, it returns a list of one or more valid IP addresses
2.131921
2.128448
1.001632
ip = None is_routable_ip = False if is_valid_ip(ip_str): ip = ip_str is_routable_ip = is_public_ip(ip) return ip, is_routable_ip
def get_ip_info(ip_str)
Given a string, it returns a tuple of (IP, Routable).
2.710945
2.285733
1.186029
if last_ip is None: return next_ip if is_public_ip(last_ip) and not is_public_ip(next_ip): return last_ip if is_private_ip(last_ip) and is_loopback_ip(next_ip): return last_ip return next_ip
def get_best_ip(last_ip, next_ip)
Given two IP addresses, it returns the the best match ip. Order of precedence is (Public, Private, Loopback, None) Right-most IP is returned
2.025761
1.927176
1.051155
args = sys_argv[1:] parser = OptionParser(usage=usage) options, args = parser.parse_args(args) template, context = args return template, context
def parse_args(sys_argv, usage)
Return an OptionParser for the script.
3.790668
3.900427
0.97186
renderer = Renderer() return renderer.render(template, context, **kwargs)
def render(template, context=None, **kwargs)
Return the given template string rendered using the given context.
3.964927
3.694518
1.073192
if isinstance(context, dict): # Then we consider the argument a "hash" for the purposes of the spec. # # We do a membership test to avoid using exceptions for flow control # (e.g. catching KeyError). if key in context: return context[key] elif type(contex...
def _get_value(context, key)
Retrieve a key's value from a context item. Returns _NOT_FOUND if the key does not exist. The ContextStack.get() docstring documents this function's intended behavior.
5.719513
5.520692
1.036014
items = context context = ContextStack() for item in items: if item is None: continue if isinstance(item, ContextStack): context._stack.extend(item._stack) else: context.push(item) if kwargs: ...
def create(*context, **kwargs)
Build a ContextStack instance from a sequence of context-like items. This factory-style method is more general than the ContextStack class's constructor in that, unlike the constructor, the argument list can itself contain ContextStack instances. Here is an example illustrating various...
3.70861
4.129776
0.898017
if name == '.': try: return self.top() except IndexError: raise KeyNotFoundError(".", "empty context stack") parts = name.split('.') try: result = self._get_simple(parts[0]) except KeyNotFoundError: ...
def get(self, name)
Resolve a dotted name against the current context stack. This function follows the rules outlined in the section of the spec regarding tag interpolation. This function returns the value as is and does not coerce the return value to a string. Arguments: name: a dotted or non...
7.043404
7.197634
0.978572
for item in reversed(self._stack): result = _get_value(item, name) if result is not _NOT_FOUND: return result raise KeyNotFoundError(name, "part missing")
def _get_simple(self, name)
Query the stack for a non-dotted name.
8.058268
6.500413
1.239655
# We type-check to avoid "TypeError: decoding Unicode is not supported". # We avoid the Python ternary operator for Python 2.4 support. if isinstance(s, unicode): return s return self.unicode(s)
def _to_unicode_soft(self, s)
Convert a basestring to unicode, preserving any unicode subclass.
10.879354
9.483279
1.147214
if encoding is None: encoding = self.string_encoding # TODO: Wrap UnicodeDecodeErrors with a message about setting # the string_encoding and decode_errors attributes. return unicode(b, encoding, self.decode_errors)
def unicode(self, b, encoding=None)
Convert a byte string to unicode, using string_encoding and decode_errors. Arguments: b: a byte string. encoding: the name of an encoding. Defaults to the string_encoding attribute for this instance. Raises: TypeError: Because this method calls Python's bu...
7.652196
6.191299
1.23596
return Loader(file_encoding=self.file_encoding, extension=self.file_extension, to_unicode=self.unicode, search_dirs=self.search_dirs)
def _make_loader(self)
Create a Loader instance using current attributes.
7.296574
5.497069
1.327357
loader = self._make_loader() def load_template(template_name): return loader.load_name(template_name) return load_template
def _make_load_template(self)
Return a function that loads a template by name.
4.347422
3.311553
1.312804
if self.partials is None: return self._make_load_template() # Otherwise, create a function from the custom partial loader. partials = self.partials def load_partial(name): # TODO: consider using EAFP here instead. # http://docs.python.or...
def _make_load_partial(self)
Return a function that loads a partial by name.
6.062488
5.427688
1.116956
val = self.missing_tags if val == MissingTags.strict: return True elif val == MissingTags.ignore: return False raise Exception("Unsupported 'missing_tags' value: %s" % repr(val))
def _is_missing_tags_strict(self)
Return whether missing_tags is set to strict.
4.1665
3.353028
1.242608
load_partial = self._make_load_partial() if self._is_missing_tags_strict(): return load_partial # Otherwise, ignore missing tags. def resolve_partial(name): try: return load_partial(name) except TemplateNotFoundError: ...
def _make_resolve_partial(self)
Return the resolve_partial function to pass to RenderEngine.__init__().
5.503611
4.903673
1.122345
if self._is_missing_tags_strict(): return context_get # Otherwise, ignore missing tags. def resolve_context(stack, name): try: return context_get(stack, name) except KeyNotFoundError: return u'' return resolve...
def _make_resolve_context(self)
Return the resolve_context function to pass to RenderEngine.__init__().
9.186647
7.682029
1.195862
resolve_context = self._make_resolve_context() resolve_partial = self._make_resolve_partial() engine = RenderEngine(literal=self._to_unicode_hard, escape=self._escape_to_unicode, resolve_context=resolve_context, ...
def _make_render_engine(self)
Return a RenderEngine instance for rendering.
5.40573
4.973101
1.086994
loader = self._make_loader() # TODO: consider an approach that does not require using an if # block here. For example, perhaps this class's loader can be # a SpecLoader in all cases, and the SpecLoader instance can # check the object's type. Or perhaps Loader an...
def _render_object(self, obj, *context, **kwargs)
Render the template associated with the given object.
6.234416
5.822412
1.070762
loader = self._make_loader() template = loader.load_name(template_name) return self._render_string(template, *context, **kwargs)
def render_name(self, template_name, *context, **kwargs)
Render the template with the given name using the given context. See the render() docstring for more information.
3.940881
4.161937
0.946886
loader = self._make_loader() template = loader.read(template_path) return self._render_string(template, *context, **kwargs)
def render_path(self, template_path, *context, **kwargs)
Render the template at the given path using the given context. Read the render() docstring for more information.
4.486486
4.384651
1.023225
# RenderEngine.render() requires that the template string be unicode. template = self._to_unicode_hard(template) render_func = lambda engine, stack: engine.render(template, stack) return self._render_final(render_func, *context, **kwargs)
def _render_string(self, template, *context, **kwargs)
Render the given template string using the given context.
7.666327
7.052666
1.087011
stack = ContextStack.create(*context, **kwargs) self._context = stack engine = self._make_render_engine() return render_func(engine, stack)
def _render_final(self, render_func, *context, **kwargs)
Arguments: render_func: a function that accepts a RenderEngine and ContextStack instance and returns a template rendering as a unicode string.
7.756476
5.02832
1.542558
if is_string(template): return self._render_string(template, *context, **kwargs) if isinstance(template, ParsedTemplate): render_func = lambda engine, stack: template.render(engine, stack) return self._render_final(render_func, *context, **kwargs) # O...
def render(self, template, *context, **kwargs)
Render the given template string, view template, or parsed template. Returns a unicode string. Prior to rendering, this method will convert a template that is a byte string (type str in Python 2) to unicode using the string_encoding and decode_errors attributes. See the constructor do...
3.634031
3.81696
0.952074
# This function implementation was chosen to be compatible across Python 2/3. f = open(path, 'rb') # We avoid use of the with keyword for Python 2.4 support. try: b = f.read() finally: f.close() return b.decode(FILE_ENCODING)
def read(path)
Read and return the contents of a text file as a unicode string.
6.928566
5.8624
1.181865
print("writing to: %s" % path) # This function implementation was chosen to be compatible across Python 2/3. f = open(path, "wb") try: b = u.encode(FILE_ENCODING) f.write(b) finally: f.close()
def write(u, path)
Write a unicode string to a file (as utf-8).
4.930712
4.635118
1.063773
root, ext = os.path.splitext(path) if new_ext is None: new_ext = ext temp_path = root + TEMP_EXTENSION + new_ext return temp_path
def make_temp_path(path, new_ext=None)
Arguments: new_ext: the new file extension, including the leading dot. Defaults to preserving the existing file extension.
2.301749
2.842042
0.809893
lines = text.splitlines(True) # preserve line endings. # Remove HTML comments (which we only allow to take a special form). new_lines = filter(lambda line: not line.startswith("<!--"), lines) return "".join(new_lines)
def strip_html_comments(text)
Strip HTML comments from a unicode string.
6.068148
5.906783
1.027319
# Pandoc uses the UTF-8 character encoding for both input and output. command = "pandoc --write=rst --output=%s %s" % (rst_temp_path, md_path) print("converting with pandoc: %s to %s\n-->%s" % (md_path, rst_temp_path, command)) if os.path.exis...
def convert_md_to_rst(md_path, rst_temp_path)
Convert the contents of a file from Markdown to reStructuredText. Returns the converted text as a Unicode string. Arguments: md_path: a path to a UTF-8 encoded Markdown file to convert. rst_temp_path: a temporary path to which to write the converted contents.
3.46933
3.683694
0.941807
readme_path = README_PATH # Remove our HTML comments because PyPI does not allow it. # See the setup.py docstring for more info on this. readme_md = strip_html_comments(read(readme_path)) history_md = strip_html_comments(read(HISTORY_PATH)) license_md = + read(LICENSE_PATH) sections ...
def make_long_description()
Generate the reST long_description for setup() from source files. Returns the generated long_description as a unicode string.
4.000436
4.161361
0.961329
long_description = make_long_description() if long_description != read(RST_DESCRIPTION_PATH): print( % (RST_DESCRIPTION_PATH, PREP_COMMAND)) sys.exit() print("Description up-to-date: %s" % RST_DESCRIPTION_PATH) answer = raw_input("Are you sure you want to publish to PyPI (yes/no)...
def publish()
Publish this package to PyPI (aka "the Cheeseshop").
6.306851
6.279538
1.00435
if not hasattr(obj, '__module__'): return None module = sys.modules[obj.__module__] if not hasattr(module, '__file__'): # TODO: add a unit test for this case. return None path = module.__file__ return os.path.dirname(path)
def get_object_directory(self, obj)
Return the directory containing an object's defining class. Returns None if there is no such directory, for example if the class was defined in an interactive Python session, or in a doctest that appears in a text file (rather than a Python file).
3.04364
2.892536
1.052239
template_name = obj.__class__.__name__ def repl(match): return '_' + match.group(0).lower() return re.sub('[A-Z]', repl, template_name)[1:]
def make_template_name(self, obj)
Return the canonical template name for an object instance. This method converts Python-style class names (PEP 8's recommended CamelCase, aka CapWords) to lower_case_with_underscords. Here is an example with code: >>> class HelloWorld(object): ... pass >>> hi = Hell...
3.818283
4.847586
0.787667
file_name = template_name if template_extension is None: template_extension = self.template_extension if template_extension is not False: file_name += os.path.extsep + template_extension return file_name
def make_file_name(self, template_name, template_extension=None)
Generate and return the file name for the given template name. Arguments: template_extension: defaults to the instance's extension.
2.366947
3.370998
0.70215
for dir_path in search_dirs: file_path = os.path.join(dir_path, file_name) if os.path.exists(file_path): return file_path return None
def _find_path(self, search_dirs, file_name)
Search for the given file, and return the path. Returns None if the file is not found.
1.813865
1.881785
0.963907
path = self._find_path(search_dirs, file_name) if path is None: raise TemplateNotFoundError('File %s not found in dirs: %s' % (repr(file_name), repr(search_dirs))) return path
def _find_path_required(self, search_dirs, file_name)
Return the path to a template with the given file name.
2.999815
2.648753
1.132539
file_name = self.make_file_name(template_name) return self._find_path_required(search_dirs, file_name)
def find_name(self, template_name, search_dirs)
Return the path to a template with the given name. Arguments: template_name: the name of the template. search_dirs: the list of directories in which to search.
5.695819
7.726462
0.737183
if file_name is None: # TODO: should we define a make_file_name() method? template_name = self.make_template_name(obj) file_name = self.make_file_name(template_name) dir_path = self.get_object_directory(obj) if dir_path is not None: sear...
def find_object(self, obj, search_dirs, file_name=None)
Return the path to a template associated with the given object.
3.358929
2.961937
1.134031
if isinstance(s, unicode): return unicode(s) return self.to_unicode(s, encoding)
def unicode(self, s, encoding=None)
Convert a string to unicode using the given encoding, and return it. This function uses the underlying to_unicode attribute. Arguments: s: a basestring instance to convert to unicode. Unlike Python's built-in unicode() function, it is okay to pass unicode strings to...
4.361946
4.724074
0.923344
b = common.read(path) if encoding is None: encoding = self.file_encoding return self.unicode(b, encoding)
def read(self, path, encoding=None)
Read the template at the given path, and return it as a unicode string.
6.179633
6.014132
1.027519
locator = self._make_locator() path = locator.find_file(file_name, self.search_dirs) return self.read(path)
def load_file(self, file_name)
Find and return the template with the given file name. Arguments: file_name: the file name of the template.
8.240549
8.947068
0.921033
locator = self._make_locator() path = locator.find_name(name, self.search_dirs) return self.read(path)
def load_name(self, name)
Find and return the template with the given template name. Arguments: name: the name of the template.
9.662214
12.015843
0.804123
locator = self._make_locator() path = locator.find_object(obj, self.search_dirs) return self.read(path)
def load_object(self, obj)
Find and return the template associated to the given object. Arguments: obj: an instance of a user-defined class. search_dirs: the list of directories in which to search.
9.493796
9.149124
1.037673
# We avoid use of the ternary operator for Python 2.4 support. def get_unicode(node): if type(node) is unicode: return node return node.render(engine, context) parts = map(get_unicode, self._parse_tree) s = ''.join(parts) return u...
def render(self, engine, context)
Returns: a string of type unicode.
6.338279
5.4782
1.157
if type(template) is not unicode: raise Exception("Template is not unicode: %s" % type(template)) parser = _Parser(delimiters) return parser.parse(template)
def parse(template, delimiters=None)
Parse a unicode template string and return a ParsedTemplate instance. Arguments: template: a unicode template string. delimiters: a 2-tuple of delimiters. Defaults to the package default. Examples: >>> parsed = parse(u"Hey {{#who}}{{name}}!{{/who}}") >>> print str(parsed).replace('u', ...
3.35604
4.930418
0.680681
# The possible tag type characters following the opening tag, # excluding "=" and "{". tag_types = "!>&/#^" # TODO: are we following this in the spec? # # The tag's content MUST be a non-whitespace character sequence # NOT containing the current closing delimiter. # tag = r...
def _compile_template_re(delimiters)
Return a regular expression object (re.RegexObject) instance.
9.811007
10.129847
0.968525
self._compile_delimiters() start_index = 0 content_end_index, parsed_section, section_key = None, None, None parsed_template = ParsedTemplate() states = [] while True: match = self._template_re.search(template, start_index) if match is...
def parse(self, template)
Parse a template string starting at some index. This method uses the current tag delimiter. Arguments: template: a unicode string that is the template to parse. index: the index at which to start parsing. Returns: a ParsedTemplate instance.
3.010822
3.029631
0.993792
# TODO: switch to using a dictionary instead of a bunch of ifs and elifs. if tag_type == '!': return _CommentNode() if tag_type == '=': delimiters = tag_key.split() self._change_delimiters(delimiters) return _ChangeNode(delimiters) ...
def _make_interpolation_node(self, tag_type, tag_key, leading_whitespace)
Create and return a non-section node for the parse tree.
4.07027
3.978452
1.023079
if tag_type == '#': return _SectionNode(tag_key, parsed_section, self._delimiters, template, section_start_index, section_end_index) if tag_type == '^': return _InvertedNode(tag_key, parsed_section) raise Exception("Invalid symbol...
def _make_section_node(self, template, tag_type, tag_key, parsed_section, section_start_index, section_end_index)
Create and return a section node for the parse tree.
3.680814
3.686269
0.99852
if spec.template_rel_path is not None: return os.path.split(spec.template_rel_path) # Otherwise, determine the file name separately. locator = self.loader._make_locator() # We do not use the ternary operator for Python 2.4 support. if spec.template_name is ...
def _find_relative(self, spec)
Return the path to the template as a relative (dir, file_name) pair. The directory returned is relative to the directory containing the class definition of the given object. The method returns None for this directory if the directory is unknown without first searching the search direct...
4.370255
3.985864
1.096438
if spec.template_path is not None: return spec.template_path dir_path, file_name = self._find_relative(spec) locator = self.loader._make_locator() if dir_path is None: # Then we need to search for the path. path = locator.find_object(spec, ...
def _find(self, spec)
Find and return the path to the template associated to the instance.
4.722676
4.157545
1.135929
if spec.template is not None: return self.loader.unicode(spec.template, spec.template_encoding) path = self._find(spec) return self.loader.read(path, spec.template_encoding)
def load(self, spec)
Find and return the template associated to a TemplateSpec instance. Returns the template as a unicode string. Arguments: spec: a TemplateSpec instance.
6.095084
5.084846
1.198676
val = self.resolve_context(context, name) if callable(val): # Return because _render_value() is already a string. return self._render_value(val(), context) if not is_string(val): return self.to_str(val) return val
def fetch_string(self, context, name)
Get a value from the given context as a basestring instance.
6.5194
6.128743
1.063742
data = self.resolve_context(context, name) # From the spec: # # If the data is not of a list type, it is coerced into a list # as follows: if the data is truthy (e.g. `!!data == true`), # use a single-element list containing the data, otherwise use ...
def fetch_section_data(self, context, name)
Fetch the value of a section as a list.
4.764754
4.653904
1.023819
if not is_string(val): # In case the template is an integer, for example. val = self.to_str(val) if type(val) is not unicode: val = self.literal(val) return self.render(val, context, delimiters)
def _render_value(self, val, context, delimiters=None)
Render an arbitrary value.
5.16392
4.989949
1.034864
parsed_template = parse(template, delimiters) return parsed_template.render(self, context_stack)
def render(self, template, context_stack, delimiters=None)
Render a unicode template string, and return as unicode. Arguments: template: a template string of type unicode (but not a proper subclass of unicode). context_stack: a ContextStack instance.
4.838707
7.493419
0.645728
if WEBENGINE: return self.dom.runJavaScript("{}".format(script)) else: return self.dom.evaluateJavaScript("{}".format(script))
def evaluate(self, script)
Evaluate script in page frame. :param script: The script to evaluate.
6.681581
9.564892
0.698553