code string | signature string | docstring string | loss_without_docstring float64 | loss_with_docstring float64 | factor float64 |
|---|---|---|---|---|---|
parts = domain.split('.')
if len(parts) == 1:
yield parts[0]
else:
for i in range(len(parts), 1, -1):
yield ".".join(parts[-i:]) | def _domain_variants(domain) | >>> list(_domain_variants("foo.bar.example.com"))
['foo.bar.example.com', 'bar.example.com', 'example.com']
>>> list(_domain_variants("example.com"))
['example.com']
>>> list(_domain_variants("localhost"))
['localhost'] | 2.309054 | 2.603086 | 0.887045 |
joined_regexes = "|".join(r for r in regexes if r)
if not joined_regexes:
return None
if use_re2:
import re2
return re2.compile(joined_regexes, flags=flags, max_mem=max_mem)
return re.compile(joined_regexes, flags=flags) | def _combined_regex(regexes, flags=re.IGNORECASE, use_re2=False, max_mem=None) | Return a compiled regex combined (using OR) from a list of ``regexes``.
If there is nothing to combine, None is returned.
re2 library (https://github.com/axiak/pyre2) often can match and compile
large regexes much faster than stdlib re module (10x is not uncommon),
but there are some gotchas:
* in... | 2.111137 | 2.444226 | 0.863724 |
options = options or {}
for optname in self.options:
if optname == 'match-case': # TODO
continue
if optname not in options:
raise ValueError("Rule requires option %s" % optname)
if optname == 'domain':
if not... | def match_url(self, url, options=None) | Return if this rule matches the URL.
What to do if rule is matched is up to developer. Most likely
``.is_exception`` attribute should be taken in account. | 3.611305 | 3.639492 | 0.992255 |
if self.is_comment:
return False
if self.is_html_rule: # HTML rules are not supported yet
return False
options = options or {}
keys = set(options.keys())
if not keys.issuperset(self._options_keys):
# some of the required options are... | def matching_supported(self, options=None) | Return whether this rule can return meaningful result,
given the `options` dict. If some options are missing,
then rule shouldn't be matched against, and this function
returns False.
No options:
>>> rule = AdblockRule("swf|")
>>> rule.matching_supported({})
True
... | 5.465715 | 5.075259 | 1.076933 |
if not rule:
return rule
# Check if the rule isn't already regexp
if rule.startswith('/') and rule.endswith('/'):
if len(rule) > 1:
rule = rule[1:-1]
else:
raise AdblockParsingError('Invalid rule')
return r... | def rule_to_regex(cls, rule) | Convert AdBlock rule to a regular expression. | 6.214052 | 6.007249 | 1.034425 |
if general_re and general_re.search(url):
return True
rules = []
if 'domain' in options and domain_required_rules:
src_domain = options['domain']
for domain in _domain_variants(src_domain):
if domain in domain_required_rules:
... | def _matches(self, url, options,
general_re, domain_required_rules, rules_with_options) | Return if ``url``/``options`` are matched by rules defined by
``general_re``, ``domain_required_rules`` and ``rules_with_options``.
``general_re`` is a compiled regex for rules without options.
``domain_required_rules`` is a {domain: [rules_which_require_it]}
mapping.
``rules... | 2.956396 | 3.360999 | 0.879618 |
# check directives are correct
if _format == "%S":
def _fromatter(seconds):
return "{:.2f}".format(seconds)
elif _format == "%I":
def _fromatter(seconds):
return "{0}".format(int(seconds * 1000))
else:
_format = _format.replace("%h", "... | def seconds_to_str_fromatter(_format) | Accepted format directives: %i %s %m %h | 2.175119 | 2.141628 | 1.015638 |
if not mode in [self.STRICT_MIN_LENGTH, self.DROP_TRAILING_SILENCE,
self.STRICT_MIN_LENGTH | self.DROP_TRAILING_SILENCE, 0]:
raise ValueError("Wrong value for mode")
self._mode = mode
self._strict_min_length = (mode & self.STRICT_MIN_LENGTH) != 0
... | def set_mode(self, mode) | :Parameters:
`mode` : *(int)*
New mode, must be one of:
- `StreamTokenizer.STRICT_MIN_LENGTH`
- `StreamTokenizer.DROP_TRAILING_SILENCE`
- `StreamTokenizer.STRICT_MIN_LENGTH | StreamTokenizer.DROP_TRAILING_SILENCE`
- `0`
See `Stre... | 3.322514 | 1.999731 | 1.661481 |
self._reinitialize()
if callback is not None:
self._deliver = callback
while True:
frame = data_source.read()
if frame is None:
break
self._current_frame += 1
self._process(frame)
self._post_process(... | def tokenize(self, data_source, callback=None) | Read data from `data_source`, one frame a time, and process the read frames in
order to detect sequences of frames that make up valid tokens.
:Parameters:
`data_source` : instance of the :class:`DataSource` class that implements a `read` method.
'read' should return a slice of... | 4.499291 | 4.212724 | 1.068024 |
if self._current >= len(self._data):
return None
self._current += 1
return self._data[self._current - 1] | def read(self) | Read one character from buffer.
:Returns:
Current character or None if end of buffer is reached | 3.873673 | 3.670472 | 1.055361 |
if not isinstance(data, basestring):
raise ValueError("data must an instance of basestring")
self._data = data
self._current = 0 | def set_data(self, data) | Set a new data buffer.
:Parameters:
`data` : a basestring object
New data buffer. | 5.4413 | 4.459911 | 1.220047 |
signal = AudioEnergyValidator._convert(data, self.sample_width)
return AudioEnergyValidator._signal_log_energy(signal) >= self._energy_threshold | def is_valid(self, data) | Check if data is valid. Audio data will be converted into an array (of
signed values) of which the log energy is computed. Log energy is computed
as follows:
.. code:: python
arr = AudioEnergyValidator._convert(signal, sample_width)
energy = float(numpy.dot(arr, arr)) /... | 16.494259 | 5.058117 | 3.260949 |
if len(data_buffer) % (self.sample_width * self.channels) != 0:
raise ValueError("length of data_buffer must be a multiple of (sample_width * channels)")
self._buffer = data_buffer
self._index = 0
self._left = 0 if self._buffer is None else len(self._buffer) | def set_data(self, data_buffer) | Set new data for this audio stream.
:Parameters:
`data_buffer` : str, basestring, Bytes
a string buffer with a length multiple of (sample_width * channels) | 2.934616 | 2.557859 | 1.147294 |
if len(data_buffer) % (self.sample_width * self.channels) != 0:
raise ValueError("length of data_buffer must be a multiple of (sample_width * channels)")
self._buffer += data_buffer
self._left += len(data_buffer) | def append_data(self, data_buffer) | Append data to this audio stream
:Parameters:
`data_buffer` : str, basestring, Bytes
a buffer with a length multiple of (sample_width * channels) | 3.385754 | 2.497061 | 1.355896 |
# Disable post_save during manage.py loaddata
if kwargs.get("raw", False):
return False
user, created = kwargs["instance"], kwargs["created"]
disabled = getattr(user, "_disable_account_creation", not settings.ACCOUNT_CREATE_ON_SAVE)
if created and not disabled:
Account.create(... | def user_post_save(sender, **kwargs) | After User.save is called we check to see if it was a created user. If so,
we check if the User object wants account creation. If all passes we
create an Account object.
We only run on user creation to avoid having to check for existence on
each call to User.save. | 6.326437 | 5.236073 | 1.208241 |
kwargs = {"user": self.request.user, "initial": self.get_initial()}
if self.request.method in ["POST", "PUT"]:
kwargs.update({
"data": self.request.POST,
"files": self.request.FILES,
})
return kwargs | def get_form_kwargs(self) | Returns the keyword arguments for instantiating the form. | 2.008101 | 1.812596 | 1.107859 |
if not settings.ACCOUNT_PASSWORD_USE_HISTORY:
return False
if hasattr(user, "password_expiry"):
# user-specific value
expiry = user.password_expiry.expiry
else:
# use global value
expiry = settings.ACCOUNT_PASSWORD_EXPIRY
if expiry == 0: # zero indicates n... | def check_password_expired(user) | Return True if password is expired and system is using
password expiration, False otherwise. | 3.573053 | 3.48214 | 1.026108 |
def decorator(view_func):
@functools.wraps(view_func, assigned=available_attrs(view_func))
def _wrapped_view(request, *args, **kwargs):
if is_authenticated(request.user):
return view_func(request, *args, **kwargs)
return handle_redirect_to_login(
... | def login_required(func=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None) | Decorator for views that checks that the user is logged in, redirecting
to the log in page if necessary. | 1.672116 | 1.739295 | 0.961376 |
bits = token.split_contents()
if len(bits) == 2:
user = bits[1]
as_var = None
elif len(bits) == 4:
user = bits[1]
as_var = bits[3]
else:
raise template.TemplateSyntaxError("'{0}' takes either two or four arguments".format(bits[0]))
return UserDisplayNode(... | def do_user_display(parser, token) | Example usage::
{% user_display user %}
or if you need to use in a {% blocktrans %}::
{% user_display user as user_display}
{% blocktrans %}{{ user_display }} has sent you a gift.{% endblocktrans %} | 1.906242 | 2.030379 | 0.93886 |
bits = token.split_contents()
if len(bits) < 2:
raise template.TemplateSyntaxError(
"'%s' takes at least one argument"
" (path to a view)" % bits[0]
)
viewname = parser.compile_filter(bits[1])
args = []
kwargs = {}
asvar = None
bits = bits[2:]
... | def urlnext(parser, token) | {% url %} copied from Django 1.7. | 1.794623 | 1.652182 | 1.086214 |
if all([key in context for key in ["redirect_field_name", "redirect_field_value"]]):
if context["redirect_field_value"]:
url += "?" + urlencode({
context["redirect_field_name"]: context["redirect_field_value"],
})
return url | def add_next(self, url, context) | With both `redirect_field_name` and `redirect_field_value` available in
the context, add on a querystring to handle "next" redirecting. | 3.025425 | 2.194296 | 1.378768 |
if "permakey" in request.headers:
# Extract the permakey from the headers
permakey = request.headers.get("permakey")
# In production, probably should have some exception handling Here
# in case the permakey is an empty string or some other bad value
... | def _verify(
self,
request,
return_payload=False,
verify=True,
raise_missing=False,
request_args=None,
request_kwargs=None,
*args,
**kwargs
) | If there is a "permakey", then we will verify the token by checking the
database. Otherwise, just do the normal verification.
Typically, any method that begins with an underscore in sanic-jwt should
not be touched. In this case, we are trying to break the rules a bit to handle
a unique ... | 4.419889 | 4.027415 | 1.097451 |
if item in self: # noqa
item = getattr(self, item)
return item() | def get(self, item) | Helper method to avoid calling getattr | 9.370337 | 7.731609 | 1.211952 |
return {
x.lower()[10:]: app_config.get(x)
for x in filter(lambda x: x.startswith("SANIC_JWT"), app_config)
} | def extract_presets(app_config) | Pull the application's configurations for Sanic JWT | 7.207795 | 4.142883 | 1.739802 |
if len(args) > 1:
kwargs.update({"authenticate": args[1]})
return Initialize(args[0], **kwargs) | def initialize(*args, **kwargs) | Functional approach to initializing Sanic JWT. This was the original
method, but was replaced by the Initialize class. It is recommended to use
the class because it is more flexible. There is no current plan to remove
this method, but it may be depracated in the future. | 6.538343 | 4.601611 | 1.420881 |
# Depracation notices
if "SANIC_JWT_HANDLER_PAYLOAD_SCOPES" in self.app.config:
raise exceptions.InvalidConfiguration(
"SANIC_JWT_HANDLER_PAYLOAD_SCOPES has been deprecated. "
"Instead, pass your handler method (not an import path) as "
... | def __check_deprecated(self) | Checks for deprecated configuration keys | 3.914055 | 3.805525 | 1.028519 |
for mapping in endpoint_mappings:
if all(map(self.config.get, mapping.keys)):
self.__add_single_endpoint(
mapping.cls, mapping.endpoint, mapping.is_protected
)
self.bp.exception(exceptions.SanicJWTException)(
self.resp... | def __add_endpoints(self) | Initialize the Sanic JWT Blueprint and add to the instance initialized | 7.261592 | 5.891119 | 1.232634 |
config = self.config
if "class_views" in self.kwargs:
class_views = self.kwargs.pop("class_views")
for route, view in class_views:
if issubclass(view, endpoints.BaseEndpoint) and isinstance(
route, str
):
... | def __add_class_views(self) | Include any custom class views on the Sanic JWT Blueprint | 3.952794 | 3.364558 | 1.174833 |
# msg took from BaseAuthentication
msg = (
"Sanic JWT was not initialized properly. It did not received "
"an instance of {}"
)
if not issubclass(self.authentication_class, Authentication):
raise exceptions.InitializationFailure(
... | def __check_classes(self) | Check if any of the default classes (`Authentication`, `Configuration`
and / or `Responses`) have been overwitten and if they're still valid | 4.09703 | 3.260998 | 1.256373 |
config = self.config
# Initialize instance of the Authentication class
self.instance.auth = self.authentication_class(self.app, config=config)
init_handlers = (
handlers if config.auth_mode() else auth_mode_agnostic_handlers
)
for handler in init_h... | def __initialize_instance(self) | Take any predefined methods/handlers and insert them into Sanic JWT | 4.823294 | 4.303688 | 1.120735 |
handler_to_enable = filter(lambda h: h.keys is not None, handlers)
for handler in handler_to_enable:
if handler.name in self.kwargs:
for k in handler.keys:
self.kwargs.update({k: True})
self.config = self.configuration_class(self.app.conf... | def __load_configuration(self) | Configure settings for the instance in the following order:
1. Sanic JWT's defaults
2. Custom Configuration class
3. Key word arguments passed to Initialize | 5.406297 | 5.608552 | 0.963938 |
uid = self.config.user_id()
if isinstance(user, dict):
user_id = user.get(uid)
elif hasattr(user, "to_dict"):
_to_dict = await utils.call(user.to_dict)
user_id = _to_dict.get(uid)
else:
raise exceptions.InvalidRetrieveUserObject()
... | async def _get_user_id(self, user, *, asdict=False) | Get a user_id from a user object. If `asdict` is True, will return
it as a dict with `config.user_id` as key. The `asdict` keyword
defaults to `False`. | 3.852259 | 3.447374 | 1.117447 |
delta = timedelta(seconds=self.config.expiration_delta())
exp = datetime.utcnow() + delta
additional = {"exp": exp}
for option in ["iss", "iat", "nbf", "aud"]:
setting = "claim_{}".format(option.lower())
if setting in self.config: # noqa
... | async def add_claims(self, payload, user, *args, **kwargs) | Injects standard claims into the payload for: exp, iss, iat, nbf, aud.
And, custom claims, if they exist | 3.976106 | 3.691758 | 1.077022 |
try:
is_valid, status, reasons = self._verify(
request,
request_args=request_args,
request_kwargs=request_kwargs,
)
except Exception as e:
logger.debug(e.args)
if self.config.debug():
... | def _check_authentication(self, request, request_args, request_kwargs) | Checks a request object to determine if that request contains a valid,
and authenticated JWT.
It returns a tuple:
1. Boolean whether the request is authenticated with a valid JWT
2. HTTP status code
3. Reasons (if any) for a potential authentication failure | 4.584579 | 4.126944 | 1.110889 |
secret = self._get_secret()
algorithm = self._get_algorithm()
kwargs = {}
for claim in self.claims:
if claim != "exp":
setting = "claim_{}".format(claim.lower())
if setting in self.config: # noqa
value = self.conf... | def _decode(self, token, verify=True) | Take a JWT and return a decoded payload. Optionally, will verify
the claims on the token. | 2.746236 | 2.631017 | 1.043793 |
payload = await utils.call(self.build_payload, user)
if (
not isinstance(payload, dict)
or self.config.user_id() not in payload
):
raise exceptions.InvalidPayload
payload = await utils.call(self.add_claims, payload, user)
extend_pay... | async def _get_payload(self, user) | Given a user object, create a payload and extend it as configured. | 3.361009 | 3.15175 | 1.066394 |
if refresh_token:
cookie_token_name_key = "cookie_refresh_token_name"
else:
cookie_token_name_key = "cookie_access_token_name"
cookie_token_name = getattr(self.config, cookie_token_name_key)
return request.cookies.get(cookie_token_name(), None) | def _get_token_from_cookies(self, request, refresh_token) | Extract the token if present inside the request cookies. | 2.877366 | 2.591135 | 1.110466 |
header = request.headers.get(self.config.authorization_header(), None)
if header is None:
return None
else:
header_prefix_key = "authorization_header_prefix"
header_prefix = getattr(self.config, header_prefix_key)
if header_prefix():
... | def _get_token_from_headers(self, request, refresh_token) | Extract the token if present inside the headers of a request. | 3.402554 | 3.23166 | 1.052881 |
if refresh_token:
query_string_token_name_key = "query_string_refresh_token_name"
else:
query_string_token_name_key = "query_string_access_token_name"
query_string_token_name = getattr(
self.config, query_string_token_name_key
)
return... | def _get_token_from_query_string(self, request, refresh_token) | Extract the token if present from the request args. | 2.774218 | 2.480403 | 1.118454 |
if self.config.cookie_set():
token = self._get_token_from_cookies(request, refresh_token)
if token:
return token
else:
if self.config.cookie_strict():
raise exceptions.MissingAuthorizationCookie()
if self.... | def _get_token(self, request, refresh_token=False) | Extract a token from a request object. | 2.386865 | 2.251217 | 1.060255 |
try:
token = self._get_token(request)
is_valid = True
reason = None
except (
exceptions.MissingAuthorizationCookie,
exceptions.MissingAuthorizationQueryArg,
exceptions.MissingAuthorizationHeader,
) as e:
... | def _verify(
self,
request,
return_payload=False,
verify=True,
raise_missing=False,
request_args=None,
request_kwargs=None,
*args,
**kwargs
) | Verify that a request object is authenticated. | 2.762938 | 2.726427 | 1.013392 |
payload = self._verify(
request, return_payload=True, verify=verify, *args, **kwargs
)
return payload | def extract_payload(self, request, verify=True, *args, **kwargs) | Extract a payload from a request object. | 4.681256 | 4.164984 | 1.123955 |
payload = self.extract_payload(request)
if not payload:
return None
scopes_attribute = self.config.scopes_name()
return payload.get(scopes_attribute, None) | def extract_scopes(self, request) | Extract scopes from a request object. | 4.746887 | 4.130601 | 1.1492 |
payload = self.extract_payload(request)
user_id_attribute = self.config.user_id()
return payload.get(user_id_attribute, None) | def extract_user_id(self, request) | Extract a user id from a request object. | 4.15063 | 3.710526 | 1.11861 |
payload = await self._get_payload(user)
secret = self._get_secret(True)
algorithm = self._get_algorithm()
return jwt.encode(payload, secret, algorithm=algorithm).decode("utf-8") | async def generate_access_token(self, user) | Generate an access token for a given user. | 3.551738 | 3.167809 | 1.121197 |
refresh_token = await utils.call(self.config.generate_refresh_token())
user_id = await self._get_user_id(user)
await utils.call(
self.store_refresh_token,
user_id=user_id,
refresh_token=refresh_token,
request=request,
)
ret... | async def generate_refresh_token(self, request, user) | Generate a refresh token for a given user. | 3.368185 | 3.193872 | 1.054577 |
if isinstance(df, (pd.DataFrame, pd.Series)):
return df.iloc[0:shape], df.iloc[shape:]
else:
return df[0:shape], df[shape:] | def tsplit(df, shape) | Split array into two parts. | 2.427065 | 2.225804 | 1.090422 |
if all([isinstance(df, (pd.DataFrame, pd.Series)) for df in [x, y]]):
return pd.concat([x, y], axis=axis)
else:
if axis == 0:
return np.concatenate([x, y])
else:
return np.column_stack([x, y]) | def concat(x, y, axis=0) | Concatenate a sequence of pandas or numpy objects into one entity. | 2.240561 | 2.067863 | 1.083516 |
shape = df.shape
if len(shape) == 1:
return df.reshape(shape[0], 1)
else:
return df | def reshape_1d(df) | If parameter is 1D row vector then convert it into 2D matrix. | 2.465646 | 2.037216 | 1.210302 |
if isinstance(df, (pd.DataFrame, pd.Series)):
return df.iloc[index]
else:
return df[index, :] | def idx(df, index) | Universal indexing for numpy and pandas objects. | 2.949727 | 2.749841 | 1.07269 |
pbar = tqdm(total=rounds)
def callback(_, ):
pbar.update(1)
return callback | def xgb_progressbar(rounds=1000) | Progressbar for xgboost using tqdm library.
Examples
--------
>>> model = xgb.train(params, X_train, 1000, callbacks=[xgb_progress(1000), ]) | 6.633271 | 13.236678 | 0.501128 |
if isinstance(model, (Regressor, Classifier)):
self.models.append(model)
else:
raise ValueError('Unrecognized estimator.') | def add(self, model) | Adds a single model.
Parameters
----------
model : `Estimator` | 5.977561 | 5.627124 | 1.062276 |
result_train = []
result_test = []
y = None
for model in self.models:
result = model.stack(k=k, stratify=stratify, shuffle=shuffle, seed=seed, full_test=full_test)
train_df = pd.DataFrame(result.X_train, columns=generate_columns(result.X_train, model.nam... | def stack(self, k=5, stratify=False, shuffle=True, seed=100, full_test=True, add_diff=False) | Stacks sequence of models.
Parameters
----------
k : int, default 5
Number of folds.
stratify : bool, default False
shuffle : bool, default True
seed : int, default 100
full_test : bool, default True
If True then evaluate test dataset on ... | 1.934144 | 2.078803 | 0.930412 |
result_train = []
result_test = []
y = None
for model in self.models:
result = model.blend(proportion=proportion, stratify=stratify, seed=seed, indices=indices)
train_df = pd.DataFrame(result.X_train, columns=generate_columns(result.X_train, model.name))... | def blend(self, proportion=0.2, stratify=False, seed=100, indices=None, add_diff=False) | Blends sequence of models.
Parameters
----------
proportion : float, default 0.2
stratify : bool, default False
seed : int, default False
indices : list(np.ndarray,np.ndarray), default None
Two numpy arrays that contain indices for train/test slicing.
... | 1.892546 | 2.078174 | 0.910677 |
p = Optimizer(self.models, test_size=test_size, scorer=scorer)
return p.minimize(method) | def find_weights(self, scorer, test_size=0.2, method='SLSQP') | Finds optimal weights for weighted average of models.
Parameters
----------
scorer : function
Scikit-learn like metric.
test_size : float, default 0.2
method : str
Type of solver. Should be one of:
- 'Nelder-Mead'
- 'Powell'
... | 5.245756 | 9.535483 | 0.55013 |
return self.apply(lambda x: np.average(x, axis=0, weights=weights)) | def weight(self, weights) | Applies weighted mean to models.
Parameters
----------
weights : list
Returns
-------
np.ndarray
Examples
----------
>>> pipeline = ModelsPipeline(model_rf,model_lr)
>>> pipeline.weight([0.8,0.2]) | 4.977151 | 6.738085 | 0.73866 |
features = [f for f in features if f in train.columns]
for column in features:
if full:
categories = pd.concat([train[column], test[column]]).dropna().unique()
else:
categories = train[column].dropna().unique()
train[column] = train[column].astype('category... | def onehot_features(train, test, features, full=False, sparse=False, dummy_na=True) | Encode categorical features using a one-hot scheme.
Parameters
----------
train : pd.DataFrame
test : pd.DataFrame
features : list
Column names in the DataFrame to be encoded.
full : bool, default False
Whether use all columns from train/test or only from train.
sparse : boo... | 1.807366 | 1.908769 | 0.946875 |
for column in features:
if full:
vs = pd.concat([train[column], test[column]])
labels, indexer = pd.factorize(vs, sort=sort)
else:
labels, indexer = pd.factorize(train[column], sort=sort)
train[column] = indexer.get_indexer(train[column])
te... | def factorize(train, test, features, na_value=-9999, full=False, sort=True) | Factorize categorical features.
Parameters
----------
train : pd.DataFrame
test : pd.DataFrame
features : list
Column names in the DataFrame to be encoded.
na_value : int, default -9999
full : bool, default False
Whether use all columns from train/test or only from train.... | 1.928013 | 2.054681 | 0.938352 |
def group_woe(group):
event = float(group.sum())
non_event = group.shape[0] - event
rel_event = event / event_total
rel_non_event = non_event / non_event_total
return np.log(rel_non_event / rel_event) * 100
if df[target_name].nunique() > 2:
raise ValueErr... | def woe(df, feature_name, target_name) | Calculate weight of evidence.
Parameters
----------
df: Dataframe
feature_name: str
Column name to encode.
target_name: str
Target column name.
Returns
-------
Series | 2.559036 | 2.652428 | 0.96479 |
def group_mean(group):
group_size = float(group.shape[0])
if C is None:
return (group.mean() * group_size + global_mean) / group_size
else:
return (group.mean() * group_size + global_mean * C) / (group_size + C)
global_mean = df[target_name].mean()
retu... | def mean_target(df, feature_name, target_name, C=None) | Mean target.
Original idea: Stanislav Semenov
Parameters
----------
C : float, default None
Regularization coefficient. The higher, the more conservative result.
The optimal value lies between 10 and 50 depending on the data.
feature_name : str
target_name : str
df: DataFram... | 2.602562 | 2.595631 | 1.00267 |
import xgboost as xgb
f_train = model.predict(xgb.DMatrix(X_train), pred_leaf=True)
f_test = model.predict(xgb.DMatrix(X_test), pred_leaf=True)
enc = OneHotEncoder()
enc.fit(f_train)
return enc.transform(f_train), enc.transform(f_test) | def xgb_to_features(model, X_train, X_test) | Converts xgboost model into categorical features.
Reference:
"Practical Lessons from Predicting Clicks on Ads at Facebook"
https://research.fb.com/publications/practical-lessons-from-predicting-clicks-on-ads-at-facebook/ | 2.067023 | 1.997741 | 1.03468 |
if not self.loaded:
self.load()
if stratify:
stratify = self.y_train
else:
stratify = None
if indices is None:
X_train, X_test, y_train, y_test = train_test_split(self.X_train, self._y_train,
... | def split(self, test_size=0.1, stratify=False, inplace=False, seed=33, indices=None) | Splits train set into two parts (train/test).
Parameters
----------
test_size : float, default 0.1
stratify : bool, default False
inplace : bool, default False
If `True` then dataset's train/test sets will be replaced with new data.
seed : int, default 33
... | 1.678186 | 1.818038 | 0.923075 |
if stratify:
kf = StratifiedKFold(n_splits=k, random_state=seed, shuffle=shuffle)
else:
kf = KFold(n_splits=k, random_state=seed, shuffle=shuffle)
for train_index, test_index in kf.split(self.X_train, self.y_train):
X_train, y_train = idx(self.X_trai... | def kfold(self, k=5, stratify=False, shuffle=True, seed=33) | K-Folds cross validation iterator.
Parameters
----------
k : int, default 5
stratify : bool, default False
shuffle : bool, default True
seed : int, default 33
Yields
-------
X_train, y_train, X_test, y_test, train_index, test_index | 1.446228 | 1.407865 | 1.027249 |
if self._hash is None:
m = hashlib.new('md5')
if self._preprocessor is None:
# generate hash from numpy array
m.update(numpy_buffer(self._X_train))
m.update(numpy_buffer(self._y_train))
if self._X_test is not None:
... | def hash(self) | Return md5 hash for current dataset. | 2.631541 | 2.405409 | 1.09401 |
if not isinstance(ds, Dataset):
raise ValueError('Expected `Dataset`, got %s.' % ds)
X_train = concat(ds.X_train, self.X_train, axis=axis)
y_train = concat(ds.y_train, self.y_train, axis=axis)
if ds.X_test is not None:
X_test = concat(ds.X_test, self.X_... | def merge(self, ds, inplace=False, axis=1) | Merge two datasets.
Parameters
----------
axis : {0,1}
ds : `Dataset`
inplace : bool, default False
Returns
-------
`Dataset` | 1.592796 | 1.624605 | 0.980421 |
self._X_train = csc_matrix(self._X_train)
self._X_test = csc_matrix(self._X_test) | def to_csc(self) | Convert Dataset to scipy's Compressed Sparse Column matrix. | 2.801785 | 2.220721 | 1.261656 |
self._X_train = csr_matrix(self._X_train)
self._X_test = csr_matrix(self._X_test) | def to_csr(self) | Convert Dataset to scipy's Compressed Sparse Row matrix. | 2.633983 | 2.184759 | 1.205617 |
if hasattr(self._X_train, 'todense'):
self._X_train = self._X_train.todense()
self._X_test = self._X_test.todense() | def to_dense(self) | Convert sparse Dataset to dense matrix. | 2.581041 | 2.098183 | 1.230131 |
m = hashlib.new('md5')
m.update(self.hash.encode('utf-8'))
for key in sorted(params.keys()):
h_string = ('%s-%s' % (key, params[key])).encode('utf-8')
m.update(h_string)
return m.hexdigest() | def _dhash(self, params) | Generate hash of the dictionary object. | 2.691644 | 2.570881 | 1.046973 |
if self.use_cache:
pdict = {'k': k, 'stratify': stratify, 'shuffle': shuffle, 'seed': seed, 'test_size': test_size}
if indices is not None:
pdict['train_index'] = np_hash(indices[0])
pdict['test_index'] = np_hash(indices[1])
dhash = ... | def validate(self, scorer=None, k=1, test_size=0.1, stratify=False, shuffle=True, seed=100, indices=None) | Evaluate score by cross-validation.
Parameters
----------
scorer : function(y_true,y_pred), default None
Scikit-learn like metric that returns a score.
k : int, default 1
The number of folds for validation.
If k=1 then randomly split X_train into two... | 2.101317 | 2.155689 | 0.974777 |
train = None
test = []
if self.use_cache:
pdict = {'k': k, 'stratify': stratify, 'shuffle': shuffle, 'seed': seed, 'full_test': full_test}
dhash = self._dhash(pdict)
c = Cache(dhash, prefix='s')
if c.available:
logger.info... | def stack(self, k=5, stratify=False, shuffle=True, seed=100, full_test=True) | Stack a single model. You should rarely be using this method. Use `ModelsPipeline.stack` instead.
Parameters
----------
k : int, default 5
stratify : bool, default False
shuffle : bool, default True
seed : int, default 100
full_test : bool, default True
... | 2.444574 | 2.447735 | 0.998709 |
if self.use_cache:
pdict = {'proportion': proportion, 'stratify': stratify, 'seed': seed, 'indices': indices}
if indices is not None:
pdict['train_index'] = np_hash(indices[0])
pdict['test_index'] = np_hash(indices[1])
dhash = self.... | def blend(self, proportion=0.2, stratify=False, seed=100, indices=None) | Blend a single model.
You should rarely be using this method. Use `ModelsPipeline.blend` instead.
Parameters
----------
proportion : float, default 0.2
Test size holdout.
stratify : bool, default False
seed : int, default 100
indices : list(np.ndarray... | 3.132591 | 3.051775 | 1.026482 |
# Credits to: https://github.com/joblib/joblib/blob/04b001861e1dd03a857b7b419c336de64e05714c/joblib/hashing.py
if isinstance(ndarray, (pd.Series, pd.DataFrame)):
ndarray = ndarray.values
if ndarray.flags.c_contiguous:
obj_c_contiguous = ndarray
elif ndarray.flags.f_contiguous:
... | def numpy_buffer(ndarray) | Creates a buffer from c_contiguous numpy ndarray. | 3.549568 | 3.448649 | 1.029263 |
if not os.path.exists(self._hash_dir):
os.makedirs(self._hash_dir)
if isinstance(data, pd.DataFrame):
columns = data.columns.tolist()
np.save(os.path.join(self._hash_dir, key), data.values)
json.dump(columns, open(os.path.join(self._hash_dir, '%s... | def store(self, key, data) | Takes an array and stores it in the cache. | 1.961933 | 1.917922 | 1.022947 |
column_file = os.path.join(self._hash_dir, '%s.json' % key)
cache_file = os.path.join(self._hash_dir, '%s.npy' % key)
if os.path.exists(cache_file):
data = np.load(cache_file)
if os.path.exists(column_file):
with open(column_file, 'r') as json_fi... | def retrieve(self, key) | Retrieves a cached array if possible. | 2.351834 | 2.20527 | 1.06646 |
if hasattr(sys, 'pypy_version_info'):
return (sys.version_info[0], sys.pypy_version_info.major,
sys.pypy_version_info.minor)
else:
return (sys.version_info[0], sys.version_info[1]) | def _pep425_version() | :return:
A tuple of integers representing the Python version number | 2.046251 | 1.87512 | 1.091264 |
try:
import _manylinux
return bool(_manylinux.manylinux1_compatible)
except (ImportError, AttributeError):
pass
# Check for glibc 2.5
try:
proc = ctypes.CDLL(None)
gnu_get_libc_version = proc.gnu_get_libc_version
gnu_get_libc_version.restype = ctype... | def _pep425_supports_manylinux() | :return:
A boolean indicating if the machine can use manylinux1 packages | 2.689173 | 2.577302 | 1.043406 |
try:
soabi = sysconfig.get_config_var('SOABI')
if soabi:
if soabi.startswith('cpython-'):
return 'cp%s' % soabi.split('-')[1]
return soabi.replace('.', '_').replace('-', '_')
except (IOError, NameError):
pass
impl = _pep425_implementatio... | def _pep425_get_abi() | :return:
A unicode string of the system abi. Will be something like: "cp27m",
"cp33m", etc. | 3.023723 | 2.931614 | 1.031419 |
tags = []
versions = []
version_info = _pep425_version()
major = version_info[:-1]
for minor in range(version_info[-1], -1, -1):
versions.append(''.join(map(str, major + (minor,))))
impl = _pep425_implementation()
abis = []
abi = _pep425_get_abi()
if abi:
abi... | def _pep425tags() | :return:
A list of 3-element tuples with unicode strings or None:
[0] implementation tag - cp33, pp27, cp26, py2, py2.py3
[1] abi tag - cp26m, None
[2] arch tag - linux_x86_64, macosx_10_10_x85_64, etc | 2.220657 | 2.195899 | 1.011275 |
x_bytes = int(math.ceil(math.log(x, 2) / 8.0))
y_bytes = int(math.ceil(math.log(y, 2) / 8.0))
num_bytes = max(x_bytes, y_bytes)
byte_string = b'\x04'
byte_string += int_to_bytes(x, width=num_bytes)
byte_string += int_to_bytes(y, width=num_bytes)
retur... | def from_coords(cls, x, y) | Creates an ECPoint object from the X and Y integer coordinates of the
point
:param x:
The X coordinate, as an integer
:param y:
The Y coordinate, as an integer
:return:
An ECPoint object | 2.197099 | 2.062053 | 1.065491 |
data = self.native
first_byte = data[0:1]
# Uncompressed
if first_byte == b'\x04':
remaining = data[1:]
field_len = len(remaining) // 2
x = int_from_bytes(remaining[0:field_len])
y = int_from_bytes(remaining[field_len:])
... | def to_coords(self) | Returns the X and Y coordinates for this EC point, as native Python
integers
:return:
A 2-element tuple containing integers (X, Y) | 5.177841 | 4.457948 | 1.161485 |
if not isinstance(private_key, byte_cls) and not isinstance(private_key, Asn1Value):
raise TypeError(unwrap(
'''
private_key must be a byte string or Asn1Value, not %s
''',
type_name(private_key)
))
if alg... | def wrap(cls, private_key, algorithm) | Wraps a private key in a PrivateKeyInfo structure
:param private_key:
A byte string or Asn1Value object of the private key
:param algorithm:
A unicode string of "rsa", "dsa" or "ec"
:return:
A PrivateKeyInfo object | 2.180042 | 2.036631 | 1.070416 |
if self.algorithm == 'dsa':
params = self['private_key_algorithm']['parameters']
return Integer(pow(
params['g'].native,
self['private_key'].parsed.native,
params['p'].native
))
if self.algorithm == 'rsa':
... | def _compute_public_key(self) | Computes the public key corresponding to the current private key.
:return:
For RSA keys, an RSAPublicKey object. For DSA keys, an Integer
object. For EC keys, an ECPointBitString. | 2.738171 | 2.596045 | 1.054747 |
if self.algorithm == 'rsa':
return self['private_key'].parsed
if self.algorithm == 'dsa':
params = self['private_key_algorithm']['parameters']
return DSAPrivateKey({
'version': 0,
'p': params['p'],
'q': params... | def unwrap(self) | Unwraps the private key into an RSAPrivateKey, DSAPrivateKey or
ECPrivateKey object
:return:
An RSAPrivateKey, DSAPrivateKey or ECPrivateKey object | 2.678163 | 2.341287 | 1.143885 |
if self._algorithm is None:
self._algorithm = self['private_key_algorithm']['algorithm'].native
return self._algorithm | def algorithm(self) | :return:
A unicode string of "rsa", "dsa" or "ec" | 9.06269 | 5.777138 | 1.568716 |
if self._bit_size is None:
if self.algorithm == 'rsa':
prime = self['private_key'].parsed['modulus'].native
elif self.algorithm == 'dsa':
prime = self['private_key_algorithm']['parameters']['p'].native
elif self.algorithm == 'ec':
... | def bit_size(self) | :return:
The bit size of the private key, as an integer | 3.089659 | 2.822469 | 1.094665 |
if self._public_key is None:
if self.algorithm == 'ec':
key = self['private_key'].parsed
if key['public_key']:
self._public_key = key['public_key'].untag()
else:
self._public_key = self._compute_public_... | def public_key(self) | :return:
If an RSA key, an RSAPublicKey object. If a DSA key, an Integer
object. If an EC key, an ECPointBitString object. | 3.428432 | 3.25128 | 1.054487 |
if self._fingerprint is None:
params = self['private_key_algorithm']['parameters']
key = self['private_key'].parsed
if self.algorithm == 'rsa':
to_hash = '%d:%d' % (
key['modulus'].native,
key['public_exponent... | def fingerprint(self) | Creates a fingerprint that can be compared with a public key to see if
the two form a pair.
This fingerprint is not compatible with fingerprints generated by any
other software.
:return:
A byte string that is a sha256 hash of selected components (based
on the ke... | 2.6072 | 2.510405 | 1.038557 |
if not isinstance(public_key, byte_cls) and not isinstance(public_key, Asn1Value):
raise TypeError(unwrap(
'''
public_key must be a byte string or Asn1Value, not %s
''',
type_name(public_key)
))
if algorit... | def wrap(cls, public_key, algorithm) | Wraps a public key in a PublicKeyInfo structure
:param public_key:
A byte string or Asn1Value object of the public key
:param algorithm:
A unicode string of "rsa"
:return:
A PublicKeyInfo object | 3.13083 | 2.634705 | 1.188304 |
if self.algorithm == 'rsa':
return self['public_key'].parsed
key_type = self.algorithm.upper()
a_an = 'an' if key_type == 'EC' else 'a'
raise ValueError(unwrap(
'''
Only RSA public keys may be unwrapped - this key is %s %s public
... | def unwrap(self) | Unwraps an RSA public key into an RSAPublicKey object. Does not support
DSA or EC public keys since they do not have an unwrapped form.
:return:
An RSAPublicKey object | 7.818736 | 6.677591 | 1.170892 |
if self.algorithm != 'ec':
raise ValueError(unwrap(
'''
Only EC keys have a curve, this key is %s
''',
self.algorithm.upper()
))
params = self['algorithm']['parameters']
chosen = params.chosen
... | def curve(self) | Returns information about the curve used for an EC key
:raises:
ValueError - when the key is not an EC key
:return:
A two-element tuple, with the first element being a unicode string
of "implicit_ca", "specified" or "named". If the first element is
"impl... | 9.116862 | 5.079272 | 1.794915 |
if self.algorithm != 'dsa':
raise ValueError(unwrap(
'''
Only DSA keys are generated using a hash algorithm, this key is
%s
''',
self.algorithm.upper()
))
parameters = self['algorithm']['pa... | def hash_algo(self) | Returns the name of the family of hash algorithms used to generate a
DSA key
:raises:
ValueError - when the key is not a DSA key
:return:
A unicode string of "sha1" or "sha2" or None if no parameters are
present | 6.209733 | 4.743741 | 1.309037 |
if self._sha1 is None:
self._sha1 = hashlib.sha1(byte_cls(self['public_key'])).digest()
return self._sha1 | def sha1(self) | :return:
The SHA1 hash of the DER-encoded bytes of this public key info | 5.531743 | 4.834927 | 1.144121 |
if self._sha256 is None:
self._sha256 = hashlib.sha256(byte_cls(self['public_key'])).digest()
return self._sha256 | def sha256(self) | :return:
The SHA-256 hash of the DER-encoded bytes of this public key info | 4.160009 | 3.748952 | 1.109646 |
setup_file = os.path.join(package_root, 'setup.py')
git_wc_proc = subprocess.Popen(
['git', 'status', '--porcelain', '-uno'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=package_root
)
git_wc_status, _ = git_wc_proc.communicate()
if len(git_wc_status... | def run() | Creates a sdist .tar.gz and a bdist_wheel --univeral .whl and uploads
them to pypi
:return:
A bool - if the packaging and upload process was successful | 2.251318 | 2.188574 | 1.028669 |
xml_report_path = os.path.join(package_root, 'coverage.xml')
if os.path.exists(xml_report_path):
os.unlink(xml_report_path)
cov = coverage.Coverage(include='%s/*.py' % package_name)
cov.start()
from .tests import run as run_tests
result = run_tests()
print()
if ci:
... | def run(ci=False) | Runs the tests while measuring coverage
:param ci:
If coverage is being run in a CI environment - this triggers trying to
run the tests for the rest of modularcrypto and uploading coverage data
:return:
A bool - if the tests ran successfully | 2.978148 | 2.745761 | 1.084635 |
proc = subprocess.Popen(
['git'] + params,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=cwd
)
stdout, stderr = proc.communicate()
code = proc.wait()
if code != 0:
e = OSError('git exit code was non-zero')
e.stdout = stdout
raise ... | def _git_command(params, cwd) | Executes a git command, returning the output
:param params:
A list of the parameters to pass to git
:param cwd:
The working directory to execute git in
:return:
A 2-element tuple of (stdout, stderr) | 2.497513 | 2.405366 | 1.038309 |
output = {}
for line in data.splitlines():
line = line.strip()
if not line or '=' not in line:
continue
parts = line.split('=')
if len(parts) != 2:
continue
name = parts[0]
value = parts[1]
if len(value) > 1:
if va... | def _parse_env_var_file(data) | Parses a basic VAR="value data" file contents into a dict
:param data:
A unicode string of the file data
:return:
A dict of parsed name/value data | 1.970241 | 1.987518 | 0.991307 |
if sys.platform == 'darwin':
version = _plat.mac_ver()[0]
_plat_ver_info = tuple(map(int, version.split('.')))
if _plat_ver_info < (10, 12):
name = 'OS X'
else:
name = 'macOS'
return '%s %s' % (name, version)
elif sys.platform == 'win32':
... | def _platform_name() | Returns information about the current operating system and version
:return:
A unicode string containing the OS name and version | 1.876466 | 1.828416 | 1.02628 |
dir_patterns, file_patterns = _gitignore(root)
paths = []
prefix = os.path.abspath(root) + os.sep
for base, dirs, files in os.walk(root):
for d in dirs:
for dir_pattern in dir_patterns:
if fnmatch(d, dir_pattern):
dirs.remove(d)
... | def _list_files(root) | Lists all of the files in a directory, taking into account any .gitignore
file that is present
:param root:
A unicode filesystem path
:return:
A list of unicode strings, containing paths of all files not ignored
by .gitignore with root, using relative paths | 2.095335 | 2.095963 | 0.999701 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.