code string | signature string | docstring string | loss_without_docstring float64 | loss_with_docstring float64 | factor float64 |
|---|---|---|---|---|---|
stats = {stat: "%f|g" % value}
self.send(stats, sample_rate) | def gauge(self, stat, value, sample_rate=1) | Log gauge information for a single stat
>>> statsd_client.gauge('some.gauge',42) | 7.462899 | 11.376714 | 0.65598 |
if not isinstance(stats, list):
stats = [stats]
data = dict((stat, "%s|c" % delta) for stat in stats)
self.send(data, sample_rate) | def update_stats(self, stats, delta, sample_rate=1) | Updates one or more stats counters by arbitrary amounts
>>> statsd_client.update_stats('some.int',10) | 4.886701 | 4.855382 | 1.00645 |
if self.prefix:
data = dict((".".join((self.prefix, stat)), value) for stat, value in data.items())
if sample_rate < 1:
if random.random() > sample_rate:
return
sampled_data = dict((stat, "%s|@%s" % (value, sample_rate))
... | def send(self, data, sample_rate=1) | Squirt the metrics over UDP | 3.436466 | 3.004729 | 1.143686 |
pid = None
if os.path.exists(self.pidfile):
with open(self.pidfile, 'r') as fp:
pid = int(fp.read().strip())
if pid:
msg = 'pidfile (%s) exists. Daemon already running?\n'
sys.stderr.write(msg % self.pidfile)
sys.exit(1)
... | def start(self, *args, **kw) | Start the daemon. | 1.935149 | 1.799428 | 1.075425 |
pid = None
if os.path.exists(self.pidfile):
with open(self.pidfile, 'r') as fp:
pid = int(fp.read().strip())
if not pid:
msg = 'pidfile (%s) does not exist. Daemon not running?\n'
sys.stderr.write(msg % self.pidfile)
retur... | def stop(self) | Stop the daemon. | 1.79214 | 1.680011 | 1.066743 |
self.stop()
self.start(*args, **kw) | def restart(self, *args, **kw) | Restart the daemon. | 3.749464 | 3.694155 | 1.014972 |
url = "https://%s/info/system?null" % (netloc)
response = requests.get(url, verify=self.verify)
if not response.ok or not hasattr(response, "json"):
error_message = '%s Unexpected Error: %s for uri: %s\nText: %r' %\
(response.status_code,
... | def get_auth_providers(self, netloc) | BIG-IQ specific query for auth providers
BIG-IP doesn't really need this because BIG-IP's multiple auth providers
seem to handle fallthrough just fine. BIG-IQ on the other hand, needs to
have its auth provider specified if you're using one of the non-default
ones.
:param netloc... | 4.7082 | 4.7662 | 0.987831 |
login_body = {
'username': self.username,
'password': self.password,
}
if self.auth_provider:
if self.auth_provider == 'local':
login_body['loginProviderName'] = 'local'
elif self.auth_provider == 'tmos':
l... | def get_new_token(self, netloc) | Get a new token from BIG-IP and store it internally.
Throws relevant exception if it fails to get a new token.
This method will be called automatically if a request is attempted
but there is no authentication token, or the authentication token
is expired. It is usually not necessary f... | 2.352367 | 2.301468 | 1.022116 |
'''(str, str, str) --> str
This function checks the supplied elements to see if each conforms to
the specification for the appropriate part of the URI. These validations
are conducted by the helper function _validate_uri_parts.
After validation the parts are assembled into a valid BigIP REST URI
... | def generate_bigip_uri(base_uri, partition, name, sub_path, suffix, **kwargs) | (str, str, str) --> str
This function checks the supplied elements to see if each conforms to
the specification for the appropriate part of the URI. These validations
are conducted by the helper function _validate_uri_parts.
After validation the parts are assembled into a valid BigIP REST URI
strin... | 3.554523 | 1.644887 | 2.160953 |
@functools.wraps(method)
def wrapper(self, RIC_base_uri, **kwargs):
partition = kwargs.pop('partition', '')
sub_path = kwargs.pop('subPath', '')
suffix = kwargs.pop('suffix', '')
identifier, kwargs = _unique_resource_identifier_from_kwargs(**kwargs)
uri_as_parts = kw... | def decorate_HTTP_verb_method(method) | Prepare and Post-Process HTTP VERB method for BigIP-RESTServer request.
This function decorates all of the HTTP VERB methods in the
iControlRESTSession class. It provides the core logic for this module.
If necessary it validates and assembles a uri from parts with a call to
`generate_bigip_uri`.
... | 2.920871 | 2.737305 | 1.067061 |
name = kwargs.pop('name', '')
uuid = kwargs.pop('uuid', '')
id = kwargs.pop('id', '')
if uuid:
return uuid, kwargs
elif id:
# Used for /mgmt/cm/system/authn/providers/tmos on BIG-IP
return id, kwargs
else:
return name, kwargs | def _unique_resource_identifier_from_kwargs(**kwargs) | Chooses an identifier given different choices
The unique identifier in BIG-IP's REST API at the time of this writing
is called 'name'. This is in contrast to the unique identifier that is
used by iWorkflow and BIG-IQ which at some times is 'name' and other
times is 'uuid'.
For example, in iWorkflo... | 5.374598 | 4.740165 | 1.133842 |
args1 = get_request_args(kwargs)
args2 = get_send_args(kwargs)
req = requests.Request('DELETE', uri, **args1)
prepared = self.session.prepare_request(req)
if self.debug:
self._debug_output.append(debug_prepared_request(prepared))
return self.session.s... | def delete(self, uri, **kwargs) | Sends a HTTP DELETE command to the BIGIP REST Server.
Use this method to send a DELETE command to the BIGIP. When calling
this method with the optional arguments ``name`` and ``partition``
as part of ``**kwargs`` they will be added to the ``uri`` passed
in separated by ~ to create a pr... | 4.059111 | 4.423669 | 0.917589 |
old_ua = self.session.headers.get('User-Agent', '')
ua = old_ua + ' ' + user_agent
self.session.headers['User-Agent'] = ua.strip() | def append_user_agent(self, user_agent) | Append text to the User-Agent header for the request.
Use this method to update the User-Agent header by appending the
given string to the session's User-Agent header separated by a space.
:param user_agent: A string to append to the User-Agent header
:type user_agent: str | 2.877076 | 2.636093 | 1.091417 |
is_valid = False
exc = None
for load in (load_pem_public_key, load_ssh_public_key):
if not is_valid:
try:
load(value.encode('utf-8'), default_backend())
is_valid = True
except Exception as e:
exc = e
if not is_valid:
... | def validate_public_key(value) | Check that the given value is a valid RSA Public key in either PEM or OpenSSH format. If it is invalid,
raises ``django.core.exceptions.ValidationError``. | 2.746658 | 2.556129 | 1.074538 |
for fmt in cls.OUTPUT_FORMATS:
clean_fmt = fmt.replace('+', '_')
setattr(cls, clean_fmt, property(
(lambda x, fmt=fmt: cls._output(x, fmt)), # fget
(lambda x, y, fmt=fmt: cls._input(x, y, fmt)))) | def _register_formats(cls) | Adds format properties. | 4.04115 | 3.757275 | 1.075553 |
'''Handles pdf and epub format.
Inpute: output_filename should have the proper extension.
Output: The name of the file created, or an IOError if failed'''
temp_file = NamedTemporaryFile(mode="w", suffix=".md", delete=False)
temp_file.write(self._content)
temp_file.close()... | def to_file(self, output_filename) | Handles pdf and epub format.
Inpute: output_filename should have the proper extension.
Output: The name of the file created, or an IOError if failed | 4.31865 | 2.787893 | 1.549073 |
iat = iat if iat else time.time()
if not generate_nonce:
generate_nonce = lambda username, iat: random.random() # NOQA
token_data = {
'username': username,
'time': iat,
'nonce': generate_nonce(username, iat),
}
token = jwt.encode(token_data, private_key, algor... | def sign(username, private_key, generate_nonce=None, iat=None, algorithm=DEFAULT_ALGORITHM) | Create a signed JWT using the given username and RSA private key.
:param username: Username (string) to authenticate as on the remote system.
:param private_key: Private key to use to sign the JWT claim.
:param generate_nonce: Optional. Callable to use to generate a new nonce. Defaults to
`random.r... | 2.600947 | 2.835175 | 0.917385 |
unverified_data = jwt.decode(token, options={
'verify_signature': False
})
if 'username' not in unverified_data:
return None
return unverified_data['username'] | def get_claimed_username(token) | Given a JWT, get the username that it is claiming to be `without verifying that the signature is valid`.
:param token: JWT claim
:return: Username | 2.950633 | 3.485615 | 0.846517 |
try:
token_data = jwt.decode(token, public_key, algorithms=algorithms)
except jwt.InvalidTokenError:
logger.debug('JWT failed verification')
return False
claimed_username = token_data.get('username')
claimed_time = token_data.get('time', 0)
claimed_nonce = token_data.ge... | def verify(token, public_key, validate_nonce=None, algorithms=[DEFAULT_ALGORITHM]) | Verify the validity of the given JWT using the given public key.
:param token: JWM claim
:param public_key: Public key to use when verifying the claim's signature.
:param validate_nonce: Callable to use to validate the claim's nonce.
:param algorithms: Allowable signing algorithms. Defaults to ['RS512'... | 2.414718 | 2.404042 | 1.004441 |
# TODO: Figure out some way to do this in a thread-safe manner. It'd be better to use
# a Redis Set or something, but we don't necessarily want to be tightly coupled to
# Redis either since not everyone uses it.
key = self.create_nonce_key(username, iat)
used = cache.get... | def log_used_nonce(self, username, iat, nonce) | Log a nonce as being used, and therefore henceforth invalid.
:param username: Username as a string.
:param iat: Unix timestamp float or integer of when the nonce was used.
:param nonce: Nonce value. | 6.827542 | 7.595991 | 0.898835 |
key = self.create_nonce_key(username, iat)
used = cache.get(key, [])
return nonce not in used | def validate_nonce(self, username, iat, nonce) | Confirm that the given nonce hasn't already been used.
:param username: Username as a string.
:param iat: Unix timestamp float or integer of when the nonce was used.
:param nonce: Nonce value.
:return: True if nonce is valid, False if it is invalid. | 6.679399 | 7.004883 | 0.953535 |
if 'HTTP_AUTHORIZATION' not in request.META:
return
try:
method, claim = request.META['HTTP_AUTHORIZATION'].split(' ', 1)
except ValueError:
return
if method.upper() != AUTH_METHOD:
return
username = token.get_claimed_us... | def process_request(self, request) | Process a Django request and authenticate users.
If a JWT authentication header is detected and it is determined to be valid, the user is set as
``request.user`` and CSRF protection is disabled (``request._dont_enforce_csrf_checks = True``) on
the request.
:param request: Django Reques... | 2.887525 | 2.836605 | 1.017951 |
private = rsa.generate_private_key(
public_exponent=public_exponent,
key_size=size,
backend=default_backend()
)
public = private.public_key()
if not as_string:
return private, public
pem_private = private.private_bytes(Encoding.PEM, PrivateFormat.PKCS8, NoEncry... | def generate_key_pair(size=2048, public_exponent=65537, as_string=True) | Generate a public/private key pair.
:param size: Optional. Describes how many bits long the key should be, larger keys provide more security,
currently 1024 and below are considered breakable, and 2048 or 4096 are reasonable default
key sizes for new keys. Defaults to 2048.
:param public_expone... | 1.616136 | 1.785394 | 0.905198 |
key_file = os.path.expanduser(key_file)
key_file = os.path.abspath(key_file)
if not key_password:
with open(key_file, 'r') as key:
return key.read()
with open(key_file, 'rb') as key:
key_bytes = key.read()
return decrypt_key(key_bytes, key_password).decode(ENCODING... | def load_private_key(key_file, key_password=None) | Load a private key from disk.
:param key_file: File path to key file.
:param key_password: Optional. If the key file is encrypted, provide the password to decrypt it. Defaults to None.
:return: PrivateKey<string> | 2.218958 | 2.570938 | 0.863093 |
private = serialization.load_pem_private_key(key, password=password, backend=default_backend())
return private.private_bytes(Encoding.PEM, PrivateFormat.PKCS8, NoEncryption()) | def decrypt_key(key, password) | Decrypt an encrypted private key.
:param key: Encrypted private key as a string.
:param password: Key pass-phrase.
:return: Decrypted private key as a string. | 2.237845 | 2.786617 | 0.803069 |
if not key:
key = load_private_key(key_file, key_password)
claim = token.sign(username, key)
return "%s %s" % (AUTH_METHOD, claim.decode(ENCODING)) | def create_auth_header(username, key=None, key_file="~/.ssh/id_rsa", key_password=None) | Create an HTTP Authorization header using a private key file.
Either a key or a key_file must be provided.
:param username: The username to authenticate as on the remote system.
:param key: Optional. A private key as either a string or an instance of cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivat... | 4.754592 | 5.753986 | 0.826313 |
# print('_config_log', 'from %s' %name if name else '')
FORMAT = '%(message)s'
# maybe better for log files
# FORMAT='[%(levelname)s]:%(message)s',
# Reset handlers
for h in list(logging.root.handlers):
logging.root.removeHandler(h)
global _log_level
if lvl: _log_level = l... | def _config_logs(lvl=None, name=None) | Set up or change logging configuration.
_config_logs() => idempotent setup;
_config_logs(L) => change log level | 4.124968 | 4.496734 | 0.917325 |
columns = tuple(columns)
lens = []
for key in columns:
value_len = max(len(str(each.get(key, ''))) for each in data)
# account for header lengths
lens.append(max(value_len, len(self._get_name(key))))
fmt = self.mk_fmt(*lens)
return f... | def mk_set_headers(self, data, columns) | figure out sizes and create header fmt | 5.453835 | 4.84368 | 1.125969 |
if key in self.display_names:
return self.display_names[key]
return key.capitalize() | def _get_name(self, key) | get display name for a key, or mangle for display | 4.597477 | 3.109519 | 1.478517 |
self.display_section("%s (%d)" % (self._get_name(typ), len(data)))
headers = tuple(map(self._get_name, columns))
fmt = self.mk_set_headers(data, columns)
self.display_headers(fmt, headers)
for each in data:
row = tuple(self._get_val(each, k) for k, v in each... | def display_set(self, typ, data, columns) | display a list of dicts | 4.28463 | 4.342372 | 0.986703 |
string = u" ".join(args) + '\n'
self.fobj.write(string) | def _print(self, *args) | internal print to self.fobj | 7.321458 | 4.336535 | 1.68832 |
if hasattr(self, 'print_' + typ):
getattr(self, 'print_' + typ)(data)
elif not data:
self._print("%s: %s" % (typ, data))
elif isinstance(data, collections.Mapping):
self._print("\n", typ)
for k, v in data.items():
self.pr... | def display(self, typ, data) | display section of typ with data | 3.043042 | 3.048657 | 0.998158 |
"Decorate a command handler"
def _wrapped(*a, **k):
r = func(*a, **k)
if r is None: r = 0
return r
return staticmethod(_wrapped) | def _handler(func) | Decorate a command handler | 6.40087 | 5.888624 | 1.086989 |
"Add commands to a parser"
subps = parser.add_subparsers()
for cmd, cls in commands:
subp = subps.add_parser(cmd, help=cls.__doc__)
add_args = getattr(cls, 'add_arguments', None)
if add_args:
add_args(subp)
handler = getattr(cls, 'handle', None)
if handler... | def add_subcommands(parser, commands) | Add commands to a parser | 2.245683 | 2.190664 | 1.025115 |
names, _, _, values = inspect.getargspec(fn)
@wraps(fn)
def wrapped(*args, **kwargs):
i = 0
backend = args[0]
for name in names[1:]:
value = args[i]
if name == "concrete" and isinstance(value, six.string_types):
args[i] = backend.REFTAG_CO... | def reftag_to_cls(fn) | decorator that checks function arguments for `concrete` and `resource`
and will properly set them to class references if a string (reftag) is
passed as the value | 2.772295 | 2.323461 | 1.193175 |
fetch = lambda: self._fetcher.fetch_latest(res, pk, 1, since=since)
self._update(res, fetch, depth) | def update(self, res, pk, depth=1, since=None) | Try to sync an object to the local database, in case of failure
where a referenced object is not found, attempt to fetch said
object from the REST api | 7.007685 | 8.065427 | 0.868855 |
"Like update() but uses WHERE-style args"
fetch = lambda: self._fetcher.fetch_all_latest(res, 0, kwargs, since=since)
self._update(res, fetch, depth) | def update_where(self, res, depth=0, since=None, **kwargs) | Like update() but uses WHERE-style args | 13.890917 | 8.442216 | 1.645411 |
"Sync all objects for the relations rs (if None, sync all resources)"
self._log.info("Updating resources: %s", ' '.join(r.tag for r in rs))
if rs is None:
rs = resource.all_resources()
ctx = self._ContextClass(self)
for r in rs:
self._atomic_update(lambda... | def update_all(self, rs=None, since=None) | Sync all objects for the relations rs (if None, sync all resources) | 8.831351 | 4.93659 | 1.788958 |
res, pk = key
jobs, lock = self._jobs
with lock:
return jobs[res].get(pk) | def get_task(self, key) | Get a scheduled task, or none | 17.75886 | 18.743645 | 0.94746 |
res, pk = key
jobs, lock = self._jobs
task = _tasks.UpdateTask(func(*args), key)
with lock:
job = jobs[res].get(pk)
had = bool(job)
if not job:
job = task
jobs[res][pk] = job
else:
ta... | def set_job(self, key, func, args) | Get a scheduled task or set if none exists.
Returns:
- task coroutine/continuation | 7.329083 | 7.464737 | 0.981827 |
"Synchronized access to tasks"
jobs, lock = self._jobs
with lock:
return jobs[res].copy() | def pending_tasks(self, res) | Synchronized access to tasks | 18.864149 | 10.65611 | 1.770266 |
"Fetch data with func, return dict indexed by ID"
data, e = fetch_func()
if e: raise e
yield {row['id']: row for row in data} | def fetch_and_index(self, fetch_func) | Fetch data with func, return dict indexed by ID | 11.19716 | 6.817725 | 1.64236 |
try:
clean_func(obj)
except B.validation_error() as e:
# _debug.log_validation_errors(B, e, obj, k)
# Check if it's a uniqueness or missing relation error
fields = B.detect_uniqueness_error(e)
missing = B.detect_missing_relations(obj, e)
return fields, missi... | def clean_helper(B, obj, clean_func) | Clean object, intercepting and collecting any missing-relation or
unique-constraint errors and returning the relevant resource ids/fields.
Returns:
- tuple: (<dict of non-unique fields>, <dict of missing refs>) | 7.319979 | 6.098858 | 1.200221 |
B = get_backend()
field_groups = FieldGroups(B.get_concrete(res))
try:
obj = B.get_object(B.get_concrete(res), row['id'])
except B.object_missing_error(B.get_concrete(res)):
tbl = B.get_concrete(res)
obj = tbl()
# Set attributes, refs
for fname, field in field_grou... | def initialize_object(B, res, row) | Do a shallow initialization of an object
Arguments:
- row<dict>: dict of data like depth=1, i.e. many_refs are only ids | 4.083528 | 4.054594 | 1.007136 |
"Find and read config file for a directory, return None if not found."
conf_path = os.path.expanduser(conf_dir)
if not os.path.exists(conf_path):
# only throw if not default
if conf_dir != DEFAULT_CONFIG_DIR:
raise IOError("Config directory not found at %s" % (conf_path, ))
... | def read_config(conf_dir=DEFAULT_CONFIG_DIR) | Find and read config file for a directory, return None if not found. | 5.34659 | 4.05902 | 1.317212 |
data = default_config(schema)
config = read_config(conf_dir)
if config:
recursive_update(data, config)
return data | def load_config(conf_dir=DEFAULT_CONFIG_DIR, schema=CLIENT_SCHEMA) | Load config files from the specified directory, using defaults for missing values.
Directory should contain a file named config.<ext> where <ext> is a
supported config file format. | 5.025551 | 5.565589 | 0.902968 |
"Check for a config file with old schema"
if not data:
return False
ok, errors, warnings = _schema.validate(_OLD_SCHEMA, data)
return ok and not (errors or warnings) | def detect_old(data) | Check for a config file with old schema | 9.370766 | 6.225241 | 1.505286 |
"Convert config data with old schema to new schema"
ret = default_config()
ret['sync'].update(data.get('peeringdb', {}))
ret['orm']['database'].update(data.get('database', {}))
return ret | def convert_old(data) | Convert config data with old schema to new schema | 8.972816 | 7.260478 | 1.235844 |
if not codec:
codec = 'yaml'
codec = munge.get_codec(codec)()
conf_dir = os.path.expanduser(conf_dir)
if not os.path.exists(conf_dir):
os.mkdir(conf_dir)
# Check for existing file, back up if necessary
outpath = os.path.join(conf_dir, 'config.' + codec.extensions[0])
if... | def write_config(data, conf_dir=DEFAULT_CONFIG_DIR, codec="yaml",
backup_existing=False) | Write config values to a file.
Arguments:
- conf_dir<str>: path to output directory
- codec<str>: output field format
- backup_existing<bool>: if a config file exists,
make a copy before overwriting | 2.462282 | 2.803713 | 0.878222 |
out = {}
for name, attr in sch.attributes():
fullpath = name
if path:
fullpath = '{}.{}'.format(path, name)
if defaults is None:
defaults = {}
default = defaults.get(name)
if isinstance(attr, _schema.Schema):
# recurse on sub-schem... | def prompt_config(sch, defaults=None, path=None) | Utility function to recursively prompt for config values
Arguments:
- defaults<dict>: default values used for empty inputs
- path<str>: path to prepend to config keys (eg. "path.keyname") | 2.773068 | 3.060331 | 0.906133 |
"Request object from API"
d, e = self._fetcher.fetch(R, pk, depth)
if e: raise e
return d | def fetch(self, R, pk, depth=1) | Request object from API | 10.41486 | 6.912074 | 1.506763 |
"Request multiple objects from API"
d, e = self._fetcher.fetch_all(R, depth, kwargs)
if e: raise e
return d | def fetch_all(self, R, depth=1, **kwargs) | Request multiple objects from API | 11.058311 | 8.023828 | 1.378184 |
"Get a resource instance by primary key (id)"
B = get_backend()
return B.get_object(B.get_concrete(res), pk) | def get(self, res, pk) | Get a resource instance by primary key (id) | 10.75935 | 8.311219 | 1.294557 |
"Get resources using a filter condition"
B = get_backend()
return B.get_objects(B.get_concrete(res)) | def all(self, res) | Get resources using a filter condition | 24.627663 | 14.606148 | 1.686116 |
def _wrapped(*a, **k):
gen = func(*a, **k)
return _consume_task(gen)
return _wrapped | def run_task(func) | Decorator to collect and return generator results, returning a list
if there are multiple results | 6.044031 | 5.266199 | 1.147703 |
return self._load(self._request(typ, id=id, params=kwargs)) | def get(self, typ, id, **kwargs) | Load type by id | 7.766023 | 6.699406 | 1.159211 |
backend, backend_version = peeringdb.get_backend_info()
user_agent = 'PeeringDB/{} {}/{}'.format(peeringdb.__version__,
backend, backend_version)
headers = {
"Accept": "application/json",
"User-Agent": user_agent,
... | def _request(self, typ, id=0, method='GET', params=None, data=None,
url=None) | send the request, return response obj | 2.35716 | 2.354776 | 1.001012 |
async def _wrapped(*a, **k):
r, ret = None, []
gen = func(*a, **k)
while True:
try:
item = gen.send(r)
except StopIteration:
break
if inspect.isawaitable(item):
r = await item
else:
... | def wrap_generator(func) | Decorator to convert a generator function to an async function which collects
and returns generator results, returning a list if there are multiple results | 2.633903 | 2.530183 | 1.040993 |
def _wrapped(*a, **k):
loop = asyncio.get_event_loop()
return loop.run_until_complete(func(*a, **k))
return _wrapped | def run_task(func) | Decorator to wrap an async function in an event loop.
Use for main sync interface methods. | 2.909317 | 2.481614 | 1.172349 |
re_tag = re.compile('^(?P<tag>[a-zA-Z]+)[\s-]*(?P<pk>\d+)$')
m = re_tag.search(string)
if not m:
raise ValueError("unable to split string '%s'" % (string, ))
return (m.group('tag').lower(), int(m.group('pk'))) | def split_ref(string) | splits a string into (tag, id) | 3.226119 | 2.883723 | 1.118734 |
"Prompt for input"
if default is not None:
msg = '{} ({})'.format(msg, repr(default))
msg = '{}: '.format(msg)
try:
s = input(msg)
except KeyboardInterrupt:
exit(1)
except EOFError:
s = ''
if not s:
s = default
return s | def prompt(msg, default=None) | Prompt for input | 2.879755 | 2.905655 | 0.991086 |
"Set soft memory limit"
rsrc = resource.RLIMIT_DATA
soft, hard = resource.getrlimit(rsrc)
resource.setrlimit(rsrc, (limit, hard)) # 4GB
softnew, _ = resource.getrlimit(rsrc)
assert softnew == limit
_log = logging.getLogger(__name__)
_log.debug('Set soft memory limit: %s => %s', soft, s... | def limit_mem(limit=(4 * 1024**3)) | Set soft memory limit | 3.602653 | 3.508302 | 1.026894 |
inits = [i for i in inits if len(i) > 0]
output = pytorch_model_template.format(**{
'module_name': pytorch_module_name,
'module_name_lower': pytorch_module_name.lower(),
'inits': '\n'.join(inits),
'inputs': inputs,
'calls': '\n'.join(calls),
'outputs': outp... | def render_module(inits, calls, inputs, outputs, dst_dir, pytorch_dict, pytorch_module_name) | Render model. | 1.849444 | 1.837532 | 1.006483 |
x = [mx.nd.array(np.ones(i)) for i in args]
x = net(*x)
# Get network params
params = net.collect_params()
# Create a symbol to trace net
x = [mx.sym.var('__input__' + str(i)) for i in range(len(args))]
sym = net(*x)
if len(sym) > 1:
group = mx.sym.Group(sym)
else:
... | def gluon2pytorch(net, args, dst_dir, pytorch_module_name, debug=True, keep_names=False) | Function to convert a model. | 3.395504 | 3.387408 | 1.00239 |
if sys.version_info.major != 2:
return
if package.__name__ == 'odoo':
if not hasattr(package, 'release'):
# Since 'release' is not in the odoo package, it means
# odoo/__init__.py did not run, so what we have here is a dummy
# odoo package created by setu... | def hook_odoo(package) | work around Odoo 10 issue
https://github.com/acsone/setuptools-odoo/issues/10
# This hook should runs after all *-nspkg.pth files because it is named
# zzz_ and .pth file run in alphabetical order. | 5.471046 | 4.324039 | 1.265263 |
outputMsg = "Current ftp connections:\n"
counter = 1
for k in self.ftpList:
outputMsg += str(counter) + ". " + k + " "
outputMsg += str(self.ftpList[k]) + "\n"
counter += 1
if self.printOutput:
logger.info(outputMsg)
return... | def getAllFtpConnections(self) | Returns a dictionary containing active ftp connections. | 3.352835 | 3.029798 | 1.10662 |
if connId in self.ftpList:
errMsg = "Connection with ID %s already exist. It should be deleted before this step." % connId
raise FtpLibraryError(errMsg)
else:
newFtp = None
outputMsg = ""
try:
timeout = int(timeout)
... | def ftp_connect(self, host, user='anonymous', password='anonymous@', port=21, timeout=30, connId='default') | Constructs FTP object, opens a connection and login.
Call this function before any other (otherwise raises exception).
Returns server output.
Parameters:
- host - server host address
- user(optional) - FTP user name. If not given, 'anonymous' is used.
- passwo... | 3.267852 | 3.333034 | 0.980444 |
thisConn = self.__getConnection(connId)
outputMsg = ""
try:
outputMsg += thisConn.getwelcome()
except ftplib.all_errors as e:
raise FtpLibraryError(str(e))
if self.printOutput:
logger.info(outputMsg)
return outputMsg | def get_welcome(self, connId='default') | Returns wlecome message of FTP server.
Parameters:
- connId(optional) - connection identifier. By default equals 'default' | 5.037722 | 4.632189 | 1.087547 |
dirList = []
thisConn = self.__getConnection(connId)
outputMsg = ""
try:
thisConn.dir(dirList.append)
for d in dirList:
outputMsg += str(d) + "\n"
except ftplib.all_errors as e:
raise FtpLibraryError(str(e))
if ... | def dir(self, connId='default') | Returns list of raw lines returned as contens of current directory.
Parameters:
- connId(optional) - connection identifier. By default equals 'default' | 3.981489 | 4.207532 | 0.946277 |
files_list = []
thisConn = self.__getConnection(connId)
try:
files_list = thisConn.nlst()
except:
files_list = []
return files_list | def dir_names(self, connId='default') | Returns list of files (and/or directories) of current directory.
Parameters:
- connId(optional) - connection identifier. By default equals 'default' | 4.497691 | 4.380577 | 1.026735 |
thisConn = self.__getConnection(connId)
outputMsg = ""
localPath = ""
if localFilePath == None:
localPath = remoteFileName
else:
localPath = os.path.normpath(localFilePath)
if os.path.isdir(localPath):
localPath = os.pa... | def download_file(self, remoteFileName, localFilePath=None, connId='default') | Downloads file from current directory on FTP server in binary mode. If
localFilePath is not given, file is saved in current local directory (by
default folder containing robot framework project file) with the same name
as source file. Returns server output
Parameters:
- remoteFil... | 2.614836 | 2.780032 | 0.940578 |
thisConn = self.__getConnection(connId)
outputMsg = ""
remoteFileName_ = ""
localFilePath = os.path.normpath(localFileName)
if not os.path.isfile(localFilePath):
raise FtpLibraryError("Valid file path should be provided.")
else:
if remoteF... | def upload_file(self, localFileName, remoteFileName=None, connId='default') | Sends file from local drive to current directory on FTP server in binary mode.
Returns server output.
Parameters:
- localFileName - file name or path to a file on a local drive.
- remoteFileName (optional) - a name or path containing name under which file should be saved.
- connI... | 3.038583 | 3.269925 | 0.929252 |
thisConn = self.__getConnection(connId)
outputMsg = ""
try:
tmpSize = thisConn.size(fileToCheck)
outputMsg += str(tmpSize)
except ftplib.all_errors as e:
raise FtpLibraryError(str(e))
if self.printOutput:
logger.info(output... | def size(self, fileToCheck, connId='default') | Checks size of a file on FTP server. Returns size of a file in bytes (integer).
Parameters:
- fileToCheck - file name or path to a file on FTP server
- connId(optional) - connection identifier. By default equals 'default'
Example:
| ${file1size} = | size | /home/myname/tmp/uu.txt... | 4.21722 | 4.52387 | 0.932215 |
thisConn = self.__getConnection(connId)
outputMsg = ""
try:
outputMsg += str(thisConn.sendcmd(command))
except ftplib.all_errors as e:
raise FtpLibraryError(str(e))
if self.printOutput:
logger.info(outputMsg)
return outputMsg | def send_cmd(self, command, connId='default') | Sends any command to FTP server. Returns server output.
Parameters:
- command - any valid command to be sent (invalid will result in exception).
- connId(optional) - connection identifier. By default equals 'default'
Example:
| send cmd | HELP | | 4.801071 | 4.547279 | 1.055812 |
thisConn = self.__getConnection(connId)
try:
thisConn.quit()
self.__removeConnection(connId)
except Exception as e:
try:
thisConn.close()
self.__removeConnection(connId)
except ftplib.all_errors as x:
... | def ftp_close(self, connId='default') | Closes FTP connection. Returns None.
Parameters:
- connId(optional) - connection identifier. By default equals 'default' | 3.231974 | 3.410187 | 0.947741 |
root_logger = logging.getLogger(name)
root_logger.setLevel(logging.DEBUG if verbose > 0 else logging.INFO)
formatter = ColorFormatter(verbose > 0, colors)
if colors:
colorclass.Windows.enable()
handler_stdout = logging.StreamHandler(sys.stdout)
handler_stdout.setFormatter(formatter... | def setup_logging(verbose=0, colors=False, name=None) | Configure console logging. Info and below go to stdout, others go to stderr.
:param int verbose: Verbosity level. > 0 print debug statements. > 1 passed to sphinx-build.
:param bool colors: Print color text in non-verbose mode.
:param str name: Which logger name to set handlers to. Used for testing. | 2.170155 | 2.334397 | 0.929643 |
formatted = super(ColorFormatter, self).format(record)
if self.verbose or not record.name.startswith(self.SPECIAL_SCOPE):
return formatted
# Arrow.
formatted = '=> ' + formatted
# Colors.
if not self.colors:
return formatted
if r... | def format(self, record) | Apply little arrow and colors to the record.
Arrow and colors are only applied to sphinxcontrib.versioning log statements.
:param logging.LogRecord record: The log record object to log. | 3.328383 | 3.350986 | 0.993255 |
gen = iter(iterator)
while True:
chunked = list()
for i, item in enumerate(gen):
chunked.append(item)
if i >= max_size - 1:
break
if not chunked:
return
yield chunked | def chunk(iterator, max_size) | Chunk a list/set/etc.
:param iter iterator: The iterable object to chunk.
:param int max_size: Max size of each chunk. Remainder chunk may be smaller.
:return: Yield list of items.
:rtype: iter | 2.40764 | 3.293489 | 0.73103 |
log = logging.getLogger(__name__)
# Setup env.
env = os.environ.copy()
if environ:
env.update(environ)
if env_var and not IS_WINDOWS:
env['GIT_DIR'] = os.path.join(local_root, '.git')
else:
env.pop('GIT_DIR', None)
# Run command.
with open(os.devnull) as nu... | def run_command(local_root, command, env_var=True, pipeto=None, retry=0, environ=None) | Run a command and return the output.
:raise CalledProcessError: Command exits non-zero.
:param str local_root: Local path to git root directory.
:param iter command: Command to run.
:param dict environ: Environment variables to set/override in the command.
:param bool env_var: Define GIT_DIR envir... | 2.549651 | 2.374843 | 1.073608 |
command = ['git', 'rev-parse', '--show-toplevel']
try:
output = run_command(directory, command, env_var=False)
except CalledProcessError as exc:
raise GitError('Failed to find local git repository root in {}.'.format(repr(directory)), exc.output)
if IS_WINDOWS:
output = outp... | def get_root(directory) | Get root directory of the local git repo from any subdirectory within it.
:raise GitError: If git command fails (dir not a git repo?).
:param str directory: Subdirectory in the local repo.
:return: Root directory of repository.
:rtype: str | 3.851 | 4.022085 | 0.957464 |
command = ['git', 'ls-remote', '--heads', '--tags']
try:
output = run_command(local_root, command)
except CalledProcessError as exc:
raise GitError('Git failed to list remote refs.', exc.output)
# Dereference annotated tags if any. No need to fetch annotations.
if '^{}' in outp... | def list_remote(local_root) | Get remote branch/tag latest SHAs.
:raise GitError: When git ls-remote fails.
:param str local_root: Local path to git root directory.
:return: List of tuples containing strings. Each tuple is sha, name, kind.
:rtype: list | 3.435597 | 3.408264 | 1.008019 |
dates_paths = dict()
# Filter without docs.
for commit in commits:
if commit in dates_paths:
continue
command = ['git', 'ls-tree', '--name-only', '-r', commit] + conf_rel_paths
try:
output = run_command(local_root, command)
except CalledProcessEr... | def filter_and_date(local_root, conf_rel_paths, commits) | Get commit Unix timestamps and first matching conf.py path. Exclude commits with no conf.py file.
:raise CalledProcessError: Unhandled git command failure.
:raise GitError: A commit SHA has not been fetched.
:param str local_root: Local path to git root directory.
:param iter conf_rel_paths: List of p... | 3.280718 | 3.302346 | 0.993451 |
# Fetch all known branches.
command = ['git', 'fetch', 'origin']
run_command(local_root, command)
# Fetch new branches/tags.
for sha, name, kind in remotes:
try:
run_command(local_root, ['git', 'reflog', sha])
except CalledProcessError:
run_command(local... | def fetch_commits(local_root, remotes) | Fetch from origin.
:raise CalledProcessError: Unhandled git command failure.
:param str local_root: Local path to git root directory.
:param iter remotes: Output of list_remote(). | 3.357169 | 3.666943 | 0.915523 |
log = logging.getLogger(__name__)
target = os.path.realpath(target)
mtimes = list()
# Define extract function.
def extract(stdout):
queued_links = list()
try:
with tarfile.open(fileobj=stdout, mode='r|') as tar:
for info in tar:
... | def export(local_root, commit, target) | Export git commit to directory. "Extracts" all files at the commit to the target directory.
Set mtime of RST files to last commit date.
:raise CalledProcessError: Unhandled git command failure.
:param str local_root: Local path to git root directory.
:param str commit: Git commit SHA to export.
:... | 2.831085 | 2.738592 | 1.033774 |
log = logging.getLogger(__name__)
output = run_command(local_root, ['git', 'remote', '-v'])
remotes = dict()
for match in RE_ALL_REMOTES.findall(output):
remotes.setdefault(match[0], [None, None])
if match[2] == 'fetch':
remotes[match[0]][0] = match[1]
else:
... | def clone(local_root, new_root, remote, branch, rel_dest, exclude) | Clone "local_root" origin into a new directory and check out a specific branch. Optionally run "git rm".
:raise CalledProcessError: Unhandled git command failure.
:raise GitError: Handled git failures.
:param str local_root: Local path to git root directory.
:param str new_root: Local path empty direc... | 2.557867 | 2.525754 | 1.012714 |
log = logging.getLogger(__name__)
current_branch = run_command(local_root, ['git', 'rev-parse', '--abbrev-ref', 'HEAD']).strip()
run_command(local_root, ['git', 'add', '.'])
# Check if there are no changes.
try:
run_command(local_root, ['git', 'diff', 'HEAD', '--no-ext-diff', '--quiet'... | def commit_and_push(local_root, remote, versions) | Commit changed, new, and deleted files in the repo and attempt to push the branch to the remote repository.
:raise CalledProcessError: Unhandled git command failure.
:raise GitError: Conflicting changes made in remote by other client and bad git config for commits.
:param str local_root: Local path to git... | 3.036968 | 2.920043 | 1.040042 |
matches = [(RE_SEMVER.findall(n) or [[]])[0] for n in names]
max_len_ints = 0
max_len_str = 0
# Get max lens for padding.
for match in (m for m in matches if m):
max_len_ints = len(match) # Never changes.
max_len_str = max(max_len_str, len(match[-1]))
if not max_len_ints:
... | def semvers(names) | Parse versions into integers and convert non-integer meta indicators into integers with ord().
Each return list item has an indicator as the first item. 0 for valid versions and 1 for invalid. Can be used to
sort non-version names (e.g. master, feature_branch, etc) after valid versions. No sorting is done in t... | 3.559825 | 3.480126 | 1.022901 |
exploded_alpha = list()
exploded_semver = list()
# Convert name to int if alpha is in sort.
if 'alpha' in sort:
alpha_max_len = max(len(r['name']) for r in remotes)
for name in (r['name'] for r in remotes):
exploded_alpha.append([ord(i) for i in name] + [0] * (alpha_max... | def multi_sort(remotes, sort) | Sort `remotes` in place. Allows sorting by multiple conditions.
This is needed because Python 3 no longer supports sorting lists of multiple types. Sort keys must all be of the
same type.
Problem: the user expects versions to be sorted latest first and timelogical to be most recent first (when viewing
... | 3.019439 | 2.765108 | 1.091979 |
return [(r['name'], self.vpathto(r['name'])) for r in self.remotes if r['kind'] == 'heads'] | def branches(self) | Return list of (name and urls) only branches. | 12.804674 | 8.927918 | 1.434228 |
return [(r['name'], self.vpathto(r['name'])) for r in self.remotes if r['kind'] == 'tags'] | def tags(self) | Return list of (name and urls) only tags. | 12.06612 | 9.195893 | 1.312121 |
if self.context['current_version'] == other_version:
return True
return self.context['pagename'] in self[other_version]['found_docs'] | def vhasdoc(self, other_version) | Return True if the other version has the current document. Like Sphinx's hasdoc().
:raise KeyError: If other_version doesn't exist.
:param str other_version: Version to link to.
:return: If current document is in the other version.
:rtype: bool | 8.036587 | 8.017557 | 1.002373 |
is_root = self.context['scv_is_root']
pagename = self.context['pagename']
if self.context['current_version'] == other_version and not is_root:
return '{}.html'.format(pagename.split('/')[-1])
other_remote = self[other_version]
other_root_dir = other_remote['... | def vpathto(self, other_version) | Return relative path to current document in another version. Like Sphinx's pathto().
If the current document doesn't exist in the other version its master_doc path is returned instead.
:raise KeyError: If other_version doesn't exist.
:param str other_version: Version to link to.
:ret... | 5.336094 | 4.91787 | 1.085042 |
log = logging.getLogger(__name__)
# Attempt to read.
log.info('Reading config from %s...', local_conf)
try:
config = read_config(os.path.dirname(local_conf), '<local>')
except HandledError:
log.warning('Unable to read file, continuing with only CLI args.')
return dict()... | def read_local_conf(local_conf) | Search for conf.py in any rel_source directory in CWD and if found read it and return.
:param str local_conf: Path to conf.py to read.
:return: Loaded conf.py.
:rtype: dict | 4.948019 | 5.250463 | 0.942397 |
log = logging.getLogger(__name__)
# List remote.
log.info('Getting list of all remote branches/tags...')
try:
remotes = list_remote(root)
except GitError as exc:
log.error(exc.message)
log.error(exc.output)
raise HandledError
log.info('Found: %s', ' '.join(i... | def gather_git_info(root, conf_rel_paths, whitelist_branches, whitelist_tags) | Gather info about the remote git repository. Get list of refs.
:raise HandledError: If function fails with a handled error. Will be logged before raising.
:param str root: Root directory of repository.
:param iter conf_rel_paths: List of possible relative paths (to git root) of Sphinx conf.py (e.g. docs/c... | 2.387206 | 2.29793 | 1.038851 |
log = logging.getLogger(__name__)
exported_root = TempDir(True).name
# Extract all.
for sha in {r['sha'] for r in versions.remotes}:
target = os.path.join(exported_root, sha)
log.debug('Exporting %s to temporary directory.', sha)
export(local_root, sha, target)
# Build... | def pre_build(local_root, versions) | Build docs for all versions to determine root directory and master_doc names.
Need to build docs to (a) avoid filename collision with files from root_ref and branch/tag names and (b) determine
master_doc config values for all versions (in case master_doc changes from e.g. contents.rst to index.rst between
... | 3.986949 | 3.719277 | 1.071969 |
log = logging.getLogger(__name__)
while True:
# Build root.
remote = versions[Config.from_context().root_ref]
log.info('Building root: %s', remote['name'])
source = os.path.dirname(os.path.join(exported_root, remote['sha'], remote['conf_rel_path']))
build(source, de... | def build_all(exported_root, destination, versions) | Build all versions.
:param str exported_root: Tempdir path with exported commits as subdirectories.
:param str destination: Destination directory to copy/overwrite built docs to. Does not delete old files.
:param sphinxcontrib.versioning.versions.Versions versions: Versions class instance. | 3.643949 | 3.609935 | 1.009422 |
# Used internally. For rebuilding all pages when one or versions fail.
app.add_config_value('sphinxcontrib_versioning_versions', SC_VERSIONING_VERSIONS, 'html')
# Needed for banner.
app.config.html_static_path.append(STATIC_DIR)
app.add_stylesheet('banner.css')
# Tell Sphinx which config ... | def setup(app) | Called by Sphinx during phase 0 (initialization).
:param sphinx.application.Sphinx app: Sphinx application object.
:returns: Extension version.
:rtype: dict | 4.858236 | 5.189642 | 0.936141 |
# Patch.
application.Config = ConfigInject
if config.show_banner:
EventHandlers.BANNER_GREATEST_TAG = config.banner_greatest_tag
EventHandlers.BANNER_MAIN_VERSION = config.banner_main_ref
EventHandlers.BANNER_RECENT_TAG = config.banner_recent_tag
EventHandlers.SHOW_BANNE... | def _build(argv, config, versions, current_name, is_root) | Build Sphinx docs via multiprocessing for isolation.
:param tuple argv: Arguments to pass to Sphinx.
:param sphinxcontrib.versioning.lib.Config config: Runtime configuration.
:param sphinxcontrib.versioning.versions.Versions versions: Versions class instance.
:param str current_name: The ref name of th... | 6.035353 | 5.945185 | 1.015167 |
# Patch.
EventHandlers.ABORT_AFTER_READ = queue
# Run.
_build(argv, config, Versions(list()), current_name, False) | def _read_config(argv, config, current_name, queue) | Read the Sphinx config via multiprocessing for isolation.
:param tuple argv: Arguments to pass to Sphinx.
:param sphinxcontrib.versioning.lib.Config config: Runtime configuration.
:param str current_name: The ref name of the current version being built.
:param multiprocessing.queues.Queue queue: Commun... | 31.51399 | 42.488602 | 0.741705 |
log = logging.getLogger(__name__)
argv = ('sphinx-build', source, target)
config = Config.from_context()
log.debug('Running sphinx-build for %s with args: %s', current_name, str(argv))
child = multiprocessing.Process(target=_build, args=(argv, config, versions, current_name, is_root))
chil... | def build(source, target, versions, current_name, is_root) | Build Sphinx docs for one version. Includes Versions class instance with names/urls in the HTML context.
:raise HandledError: If sphinx-build fails. Will be logged before raising.
:param str source: Source directory to pass to sphinx-build.
:param str target: Destination directory to write documentation t... | 3.841459 | 3.274193 | 1.173254 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.