code
string
signature
string
docstring
string
loss_without_docstring
float64
loss_with_docstring
float64
factor
float64
urls = [urljoin(self.item_url, F"{i}.json") for i in item_ids] result = self._run_async(urls=urls) items = [Item(r) for r in result if r] if item_type: return [item for item in items if item.item_type == item_type] else: return items
def get_items_by_ids(self, item_ids, item_type=None)
Given a list of item ids, return all the Item objects Args: item_ids (obj): List of item IDs to query item_type (str): (optional) Item type to filter results with Returns: List of `Item` objects for given item IDs and given item type
3.33202
3.821178
0.871988
url = urljoin(self.user_url, F"{user_id}.json") response = self._get_sync(url) if not response: raise InvalidUserID user = User(response) if expand and user.submitted: items = self.get_items_by_ids(user.submitted) user_opt = { ...
def get_user(self, user_id, expand=False)
Returns Hacker News `User` object. Fetches data from the url: https://hacker-news.firebaseio.com/v0/user/<user_id>.json e.g. https://hacker-news.firebaseio.com/v0/user/pg.json Args: user_id (string): unique user id of a Hacker News user. expand (bool): Flag...
3.913018
3.44861
1.134665
urls = [urljoin(self.user_url, F"{i}.json") for i in user_ids] result = self._run_async(urls=urls) return [User(r) for r in result if r]
def get_users_by_ids(self, user_ids)
Given a list of user ids, return all the User objects
5.734159
5.541481
1.03477
top_stories = self._get_stories('topstories', limit) if raw: top_stories = [story.raw for story in top_stories] return top_stories
def top_stories(self, raw=False, limit=None)
Returns list of item ids of current top stories Args: limit (int): specifies the number of stories to be returned. raw (bool): Flag to indicate whether to represent all objects in raw json. Returns: `list` object containing ids of top stories.
2.952041
4.544875
0.649532
new_stories = self._get_stories('newstories', limit) if raw: new_stories = [story.raw for story in new_stories] return new_stories
def new_stories(self, raw=False, limit=None)
Returns list of item ids of current new stories Args: limit (int): specifies the number of stories to be returned. raw (bool): Flag to indicate whether to transform all objects into raw json. Returns: `list` object containing ids of new stories.
3.197838
4.409503
0.725215
ask_stories = self._get_stories('askstories', limit) if raw: ask_stories = [story.raw for story in ask_stories] return ask_stories
def ask_stories(self, raw=False, limit=None)
Returns list of item ids of latest Ask HN stories Args: limit (int): specifies the number of stories to be returned. raw (bool): Flag to indicate whether to transform all objects into raw json. Returns: `list` object containing ids of Ask HN stories.
3.338027
5.124004
0.651449
show_stories = self._get_stories('showstories', limit) if raw: show_stories = [story.raw for story in show_stories] return show_stories
def show_stories(self, raw=False, limit=None)
Returns list of item ids of latest Show HN stories Args: limit (int): specifies the number of stories to be returned. raw (bool): Flag to indicate whether to transform all objects into raw json. Returns: `list` object containing ids of Show HN storie...
3.400489
4.411388
0.770843
job_stories = self._get_stories('jobstories', limit) if raw: job_stories = [story.raw for story in job_stories] return job_stories
def job_stories(self, raw=False, limit=None)
Returns list of item ids of latest Job stories Args: limit (int): specifies the number of stories to be returned. raw (bool): Flag to indicate whether to transform all objects into raw json. Returns: `list` object containing ids of Job stories.
3.000584
4.271363
0.702489
url = urljoin(self.base_url, 'updates.json') response = self._get_sync(url) return { 'items': self.get_items_by_ids(item_ids=response['items']), 'profiles': self.get_users_by_ids(user_ids=response['profiles']) }
def updates(self)
Returns list of item ids and user ids that have been changed/updated recently. Fetches data from URL: https://hacker-news.firebaseio.com/v0/updates.json Returns: `dict` with two keys whose values are `list` objects
3.953593
3.610363
1.095068
url = urljoin(self.base_url, 'maxitem.json') response = self._get_sync(url) if expand: return self.get_item(response) else: return response
def get_max_item(self, expand=False)
The current largest item id Fetches data from URL: https://hacker-news.firebaseio.com/v0/maxitem.json Args: expand (bool): Flag to indicate whether to transform all IDs into objects. Returns: `int` if successful.
4.122583
4.063109
1.014638
max_item = self.get_max_item() urls = [urljoin(self.item_url, F"{i}.json") for i in range( max_item - num + 1, max_item + 1)] result = self._run_async(urls=urls) return [Item(r) for r in result if r]
def get_last(self, num=10)
Returns last `num` of HN stories Downloads all the HN articles and returns them as Item objects Returns: `list` object containing ids of HN stories.
5.083527
4.54809
1.117728
self.dct = wrapper.Dictionary.load(path) return self
def load(self, path)
Loads DAWG from a file.
22.423473
21.405012
1.047581
return self._similar_keys("", key, self.dct.ROOT, replaces)
def similar_keys(self, key, replaces)
Returns all variants of ``key`` in this DAWG according to ``replaces``. ``replaces`` is an object obtained from ``DAWG.compile_replaces(mapping)`` where mapping is a dict that maps single-char unicode sitrings to another single-char unicode strings. This may be useful e...
31.499485
35.557346
0.885878
''' Returns a list with keys of this DAWG that are prefixes of the ``key``. ''' res = [] index = self.dct.ROOT if not isinstance(key, bytes): key = key.encode('utf8') pos = 1 for ch in key: index = self.dct.follow_char(int_from_by...
def prefixes(self, key)
Returns a list with keys of this DAWG that are prefixes of the ``key``.
5.311822
3.843552
1.382008
self.dct = wrapper.Dictionary() self.guide = wrapper.Guide() with open(path, 'rb') as f: self.dct.read(f) self.guide.read(f) return self
def load(self, path)
Loads DAWG from a file.
4.510033
4.294731
1.050132
if not isinstance(key, bytes): key = key.encode('utf8') return self.b_get_value(key) or default
def get(self, key, default=None)
Returns a list of payloads (as byte objects) for a given key or ``default`` if the key is not found.
5.878466
5.505333
1.067776
return self._similar_items("", key, self.dct.ROOT, replaces)
def similar_items(self, key, replaces)
Returns a list of (key, value) tuples for all variants of ``key`` in this DAWG according to ``replaces``. ``replaces`` is an object obtained from ``DAWG.compile_replaces(mapping)`` where mapping is a dict that maps single-char unicode sitrings to another single-char unicode stri...
31.400381
41.940853
0.748682
return self._similar_item_values(0, key, self.dct.ROOT, replaces)
def similar_item_values(self, key, replaces)
Returns a list of values for all variants of the ``key`` in this DAWG according to ``replaces``. ``replaces`` is an object obtained from ``DAWG.compile_replaces(mapping)`` where mapping is a dict that maps single-char unicode sitrings to another single-char unicode strings.
14.258682
17.181906
0.829866
"Gets a value from a given index." offset = units.offset(self._units[index]) value_index = (index ^ offset) & units.PRECISION_MASK return units.value(self._units[value_index])
def value(self, index)
Gets a value from a given index.
8.029502
7.750914
1.035943
"Reads a dictionary from an input stream." base_size = struct.unpack(str("=I"), fp.read(4))[0] self._units.fromfile(fp, base_size)
def read(self, fp)
Reads a dictionary from an input stream.
8.230103
7.70448
1.068223
"Exact matching." index = self.follow_bytes(key, self.ROOT) if index is None: return False return self.has_value(index)
def contains(self, key)
Exact matching.
11.572659
8.984918
1.28801
"Exact matching (returns value)" index = self.follow_bytes(key, self.ROOT) if index is None: return -1 if not self.has_value(index): return -1 return self.value(index)
def find(self, key)
Exact matching (returns value)
8.196682
5.518026
1.485437
"Follows a transition" offset = units.offset(self._units[index]) next_index = (index ^ offset ^ label) & units.PRECISION_MASK if units.label(self._units[next_index]) != label: return None return next_index
def follow_char(self, label, index)
Follows a transition
9.50117
8.441636
1.125513
"Follows transitions." for ch in s: index = self.follow_char(int_from_byte(ch), index) if index is None: return None return index
def follow_bytes(self, s, index)
Follows transitions.
8.453337
6.009402
1.406685
"Gets the next key" if not self._index_stack: return False index = self._index_stack[-1] if self._last_index != self._dic.ROOT: child_label = self._guide.child(index) # UCharType if child_label: # Follows a transition to the first...
def next(self)
Gets the next key
4.416802
4.482716
0.985296
if not check_response or not check_response.checkErrors: return _IS_OK # only check the first error for now, as per ESP theError = check_response.checkErrors[0] error_tuple = _CHECK_ERROR_CONVERSION.get(theError.code, _IS_UNKNOWN) if error_tuple[1].find(u'{') == -1: # no replacements ...
def convert_response(check_response, project_id)
Computes a http status code and message `CheckResponse` The return value a tuple (code, message, api_key_is_bad) where code: is the http status code message: is the message to return api_key_is_bad: indicates that a given api_key is bad Args: check_response (:class:`endpoints_management.ge...
5.984479
5.646531
1.059851
if not isinstance(check_request, sc_messages.CheckRequest): raise ValueError(u'Invalid request') op = check_request.operation if op is None or op.operationName is None or op.consumerId is None: logging.error(u'Bad %s: not initialized => not signed', check_request) raise ValueErr...
def sign(check_request)
Obtains a signature for an operation in a `CheckRequest` Args: op (:class:`endpoints_management.gen.servicecontrol_v1_messages.Operation`): an operation used in a `CheckRequest` Returns: string: a secure hash generated from the operation
4.009116
3.669513
1.092547
if not self.service_name: raise ValueError(u'the service name must be set') if not self.operation_id: raise ValueError(u'the operation id must be set') if not self.operation_name: raise ValueError(u'the operation name must be set') op = super(...
def as_check_request(self, timer=datetime.utcnow)
Makes a `ServicecontrolServicesCheckRequest` from this instance Returns: a ``ServicecontrolServicesCheckRequest`` Raises: ValueError: if the fields in this instance are insufficient to to create a valid ``ServicecontrolServicesCheckRequest``
2.843292
2.592455
1.096757
if self._cache is None: return [] with self._cache as c: flushed_items = list(c.out_deque) c.out_deque.clear() cached_reqs = [item.extract_request() for item in flushed_items] cached_reqs = [req for req in cached_reqs if req is not Non...
def flush(self)
Flushes this instance's cache. The driver of this instance should call this method every `flush_interval`. Returns: list['CheckRequest']: corresponding to CheckRequests that were pending
4.471954
4.293978
1.041448
if self._cache is not None: with self._cache as c: c.clear() c.out_deque.clear()
def clear(self)
Clears this instance's cache.
9.211993
7.651115
1.204007
if self._cache is None: return signature = sign(req.checkRequest) with self._cache as c: now = self._timer() quota_scale = 0 # WIP item = c.get(signature) if item is None: c[signature] = CachedItem( ...
def add_response(self, req, resp)
Adds the response from sending to `req` to this instance's cache. Args: req (`ServicecontrolServicesCheckRequest`): the request resp (CheckResponse): the response from sending the request
6.173326
5.625138
1.097453
if self._cache is None: return None # no cache, send request now if not isinstance(req, sc_messages.ServicecontrolServicesCheckRequest): raise ValueError(u'Invalid request') if req.serviceName != self.service_name: _logger.error(u'bad check(): servic...
def check(self, req)
Determine if ``req`` is in this instances cache. Determine if there are cache hits for the request in this aggregator instance. Not in the cache If req is not in the cache, it returns ``None`` to indicate that the caller should send the request. Cache Hit; response ha...
3.797728
3.250549
1.168334
a_is_text = isinstance(a, basestring) b_is_text = isinstance(b, basestring) if type(a) != type(b) and not (a_is_text and b_is_text): _logger.error(u'Cannot compare %s to %s, types differ %s!=%s', a, b, type(a), type(b)) raise ValueError(u'cannot compare inputs of d...
def compare(a, b)
Compares two timestamps. ``a`` and ``b`` must be the same type, in addition to normal representations of timestamps that order naturally, they can be rfc3339 formatted strings. Args: a (string|object): a timestamp b (string|object): another timestamp Returns: int: -1 if a < b, 0...
2.41423
2.373975
1.016957
if isinstance(timestamp, datetime.datetime): timestamp = timestamp - _EPOCH_START if not isinstance(timestamp, datetime.timedelta): _logger.error(u'Could not convert %s to a rfc3339 time,', timestamp) raise ValueError(u'Invalid timestamp type') return strict_rfc3339.timestamp_to...
def to_rfc3339(timestamp)
Converts ``timestamp`` to an RFC 3339 date string format. ``timestamp`` can be either a ``datetime.datetime`` or a ``datetime.timedelta``. Instances of the later are assumed to be a delta with the beginining of the unix epoch, 1st of January, 1970 The returned string is always Z-normalized. Examples...
4.299332
4.34935
0.9885
timestamp = strict_rfc3339.rfc3339_to_timestamp(rfc3339_text) result = datetime.datetime.utcfromtimestamp(timestamp) if with_nanos: return (result, int((timestamp - int(timestamp)) * 1e9)) else: return result
def from_rfc3339(rfc3339_text, with_nanos=False)
Parse a RFC 3339 date string format to datetime.date. Example of accepted format: '1972-01-01T10:00:20.021-05:00' - By default, the result is a datetime.datetime - If with_nanos is true, the result is a 2-tuple, (datetime.datetime, nanos), where the second field represents the possible nanosecond ...
2.184905
2.681624
0.814769
now = timer() op = sc_messages.Operation( endTime=timestamp.to_rfc3339(now), startTime=timestamp.to_rfc3339(now), importance=sc_messages.Operation.ImportanceValueValuesEnum.LOW) if self.operation_id: op.operationId = self.operation_id ...
def as_operation(self, timer=datetime.utcnow)
Makes an ``Operation`` from this instance. Returns: an ``Operation``
2.913354
3.069722
0.949061
result = encoding.CopyProtoMessage(self._op) names = sorted(self._metric_values_by_name_then_sign.keys()) for name in names: mvs = self._metric_values_by_name_then_sign[name] result.metricValueSets.append( sc_messages.MetricValueSet( ...
def as_operation(self)
Obtains a single `Operation` representing this instances contents. Returns: :class:`endpoints_management.gen.servicecontrol_v1_messages.Operation`
5.913153
5.510218
1.073125
self._op.logEntries.extend(other_op.logEntries) self._merge_timestamps(other_op) self._merge_metric_values(other_op)
def add(self, other_op)
Combines `other_op` with the operation held by this aggregator. N.B. It merges the operations log entries and metric values, but makes the assumption the operation is consistent. It's the callers responsibility to ensure consistency Args: other_op ( class:`endp...
6.172532
3.286728
1.878017
if num_finite_buckets <= 0: raise ValueError(_BAD_NUM_FINITE_BUCKETS) if growth_factor <= 1.0: raise ValueError(_BAD_FLOAT_ARG % (u'growth factor', 1.0)) if scale <= 0.0: raise ValueError(_BAD_FLOAT_ARG % (u'scale', 0.0)) return sc_messages.Distribution( bucketCounts...
def create_exponential(num_finite_buckets, growth_factor, scale)
Creates a new instance of distribution with exponential buckets Args: num_finite_buckets (int): initializes number of finite buckets growth_factor (float): initializes the growth factor scale (float): initializes the scale Return: :class:`endpoints_management.gen.servicecontrol_v1_...
2.7224
2.365363
1.150944
if num_finite_buckets <= 0: raise ValueError(_BAD_NUM_FINITE_BUCKETS) if width <= 0.0: raise ValueError(_BAD_FLOAT_ARG % (u'width', 0.0)) return sc_messages.Distribution( bucketCounts=[0] * (num_finite_buckets + 2), linearBuckets=sc_messages.LinearBuckets( nu...
def create_linear(num_finite_buckets, width, offset)
Creates a new instance of distribution with linear buckets. Args: num_finite_buckets (int): initializes number of finite buckets width (float): initializes the width of each bucket offset (float): initializes the offset Return: :class:`endpoints_management.gen.servicecontrol_v1_mes...
3.642112
3.033941
1.200456
safe_bounds = sorted(float(x) for x in bounds) if len(safe_bounds) != len(set(safe_bounds)): raise ValueError(u'Detected two elements of bounds that are the same') return sc_messages.Distribution( bucketCounts=[0] * (len(safe_bounds) + 1), explicitBuckets=sc_messages.ExplicitBuc...
def create_explicit(bounds)
Creates a new instance of distribution with explicit buckets. bounds is an iterable of ordered floats that define the explicit buckets Args: bounds (iterable[float]): initializes the bounds Return: :class:`endpoints_management.gen.servicecontrol_v1_messages.Distribution` Raises: ...
5.674819
3.964565
1.431385
dist_type, _ = _detect_bucket_option(dist) if dist_type == u'exponentialBuckets': _update_general_statistics(a_float, dist) _update_exponential_bucket_count(a_float, dist) elif dist_type == u'linearBuckets': _update_general_statistics(a_float, dist) _update_linear_bucket...
def add_sample(a_float, dist)
Adds `a_float` to `dist`, updating its existing buckets. Args: a_float (float): a new value dist (:class:`endpoints_management.gen.servicecontrol_v1_messages.Distribution`): the Distribution being updated Raises: ValueError: if `dist` does not have known bucket options defined ...
2.754155
2.445215
1.126345
if not _buckets_nearly_equal(prior, latest): _logger.error(u'Bucket options do not match. From %s To: %s', prior, latest) raise ValueError(u'Bucket options do not match') if len(prior.bucketCounts) != len(latest.bucketCounts): _logger.erro...
def merge(prior, latest)
Merge `prior` into `latest`. N.B, this mutates latest. It ensures that the statistics and histogram are updated to correctly include the original values from both instances. Args: prior (:class:`endpoints_management.gen.servicecontrol_v1_messages.Distribution`): an instance latest (:cl...
2.698155
2.406461
1.121213
a_type, a_buckets = _detect_bucket_option(a_dist) b_type, b_buckets = _detect_bucket_option(b_dist) if a_type != b_type: return False elif a_type == u'linearBuckets': return _linear_buckets_nearly_equal(a_buckets, b_buckets) elif a_type == u'exponentialBuckets': return _...
def _buckets_nearly_equal(a_dist, b_dist)
Determines whether two `Distributions` are nearly equal. Args: a_dist (:class:`Distribution`): an instance b_dist (:class:`Distribution`): another instance Return: boolean: `True` if the two instances are approximately equal, otherwise False
1.909178
2.11302
0.903531
if not dist.count: dist.count = 1 dist.maximum = a_float dist.minimum = a_float dist.mean = a_float dist.sumOfSquaredDeviation = 0 else: old_count = dist.count old_mean = dist.mean new_mean = ((old_count * old_mean) + a_float) / (old_count + 1...
def _update_general_statistics(a_float, dist)
Adds a_float to distribution, updating the statistics fields. Args: a_float (float): a new value dist (:class:`endpoints_management.gen.servicecontrol_v1_messages.Distribution`): the Distribution being updated
1.76693
1.735378
1.018181
buckets = dist.exponentialBuckets if buckets is None: raise ValueError(_BAD_UNSET_BUCKETS % (u'exponential buckets')) bucket_counts = dist.bucketCounts num_finite_buckets = buckets.numFiniteBuckets if len(bucket_counts) < num_finite_buckets + 2: raise ValueError(_BAD_LOW_BUCKET_...
def _update_exponential_bucket_count(a_float, dist)
Adds `a_float` to `dist`, updating its exponential buckets. Args: a_float (float): a new value dist (:class:`endpoints_management.gen.servicecontrol_v1_messages.Distribution`): the Distribution being updated Raises: ValueError: if `dist` does not already have exponential buckets defi...
3.526914
3.600309
0.979614
buckets = dist.linearBuckets if buckets is None: raise ValueError(_BAD_UNSET_BUCKETS % (u'linear buckets')) bucket_counts = dist.bucketCounts num_finite_buckets = buckets.numFiniteBuckets if len(bucket_counts) < num_finite_buckets + 2: raise ValueError(_BAD_LOW_BUCKET_COUNT) ...
def _update_linear_bucket_count(a_float, dist)
Adds `a_float` to `dist`, updating the its linear buckets. Args: a_float (float): a new value dist (:class:`endpoints_management.gen.servicecontrol_v1_messages.Distribution`): the Distribution being updated Raises: ValueError: if `dist` does not already have linear buckets defined ...
3.151316
3.228153
0.976198
buckets = dist.explicitBuckets if buckets is None: raise ValueError(_BAD_UNSET_BUCKETS % (u'explicit buckets')) bucket_counts = dist.bucketCounts bounds = buckets.bounds if len(bucket_counts) < len(bounds) + 1: raise ValueError(_BAD_LOW_BUCKET_COUNT) bucket_counts[bisect.bis...
def _update_explicit_bucket_count(a_float, dist)
Adds `a_float` to `dist`, updating its explicit buckets. Args: a_float (float): a new value dist (:class:`endpoints_management.gen.servicecontrol_v1_messages.Distribution`): the Distribution being updated Raises: ValueError: if `dist` does not already have explict buckets defined ...
4.8174
4.897379
0.983669
if kwargs is _sentinel: kwargs = {} event = Event(time, priority, action, argument, kwargs) with self._lock: heapq.heappush(self._queue, event) return event
def enterabs(self, time, priority, action, argument=(), kwargs=_sentinel)
Enter a new event in the queue at an absolute time. Returns an ID for the event which can be used to remove it, if necessary.
2.500317
2.795483
0.894413
time = self.timefunc() + delay return self.enterabs(time, priority, action, argument, kwargs)
def enter(self, delay, priority, action, argument=(), kwargs=_sentinel)
A variant that specifies the time as a relative time. This is actually the more commonly used interface.
7.001029
5.546553
1.262231
with self._lock: self._queue.remove(event) heapq.heapify(self._queue)
def cancel(self, event)
Remove an event from the queue. This must be presented the ID as returned by enter(). If the event is not in the queue, this raises ValueError.
5.811367
4.664476
1.245878
# localize variable access to minimize overhead # and to improve thread safety lock = self._lock q = self._queue delayfunc = self.delayfunc timefunc = self.timefunc pop = heapq.heappop while True: with lock: if not q: ...
def run(self, blocking=True)
Execute events until the queue is empty. If blocking is False executes the scheduled events due to expire soonest (if any) and then return the deadline of the next scheduled call in the scheduler. When there is a positive delay until the first event, the delay function is called...
4.574835
4.260461
1.073789
if not isinstance(money, sc_messages.Money): raise ValueError(u'Inputs should be of type %s' % (sc_messages.Money,)) currency = money.currencyCode if not currency or len(currency) != 3: raise ValueError(_MSG_3_LETTERS_LONG) units = money.units nanos = money.nanos if ((units ...
def check_valid(money)
Determine if an instance of `Money` is valid. Args: money (:class:`endpoints_management.gen.servicecontrol_v1_messages.Money`): the instance to test Raises: ValueError: if the money instance is invalid
3.444168
3.367918
1.02264
for m in (a, b): if not isinstance(m, sc_messages.Money): raise ValueError(u'Inputs should be of type %s' % (sc_messages.Money,)) if a.currencyCode != b.currencyCode: raise ValueError(u'Money values need the same currency to be summed') nano_carry, nanos_sum = _sum_nanos(a, ...
def add(a, b, allow_overflow=False)
Adds two instances of `Money`. Args: a (:class:`endpoints_management.gen.servicecontrol_v1_messages.Money`): one money value b (:class:`endpoints_management.gen.servicecontrol_v1_messages.Money`): another money value allow_overflow: determines if the addition is allowed to overflo...
2.47276
2.348853
1.052752
units = money.units nanos = money.nanos if units: if units > 0: return 1 elif units < 0: return -1 if nanos: if nanos > 0: return 1 elif nanos < 0: return -1 return 0
def _sign_of(money)
Determines the amount sign of a money instance Args: money (:class:`endpoints_management.gen.servicecontrol_v1_messages.Money`): the instance to test Return: int: 1, 0 or -1
2.143447
2.157937
0.993286
current_time = time.time() expiration = jwt_claims[u"exp"] if not isinstance(expiration, INT_TYPES): raise suppliers.UnauthenticatedException(u'Malformed claim: "exp" must be an integer') if current_time >= expiration: raise suppliers.UnauthenticatedException(u"The auth token has a...
def _check_jwt_claims(jwt_claims)
Checks whether the JWT claims should be accepted. Specifically, this method checks the "exp" claim and the "nbf" claim (if present), and raises UnauthenticatedException if 1) the current time is before the time identified by the "nbf" claim, or 2) the current time is equal to or after the time identifi...
2.57835
2.488885
1.035946
for claim_name in [u"aud", u"exp", u"iss", u"sub"]: if claim_name not in jwt_claims: raise suppliers.UnauthenticatedException(u'Missing "%s" claim' % claim_name)
def _verify_required_claims_exist(jwt_claims)
Verifies that the required claims exist. Args: jwt_claims: the JWT claims to be verified. Raises: UnauthenticatedException: if some claim doesn't exist.
4.314309
4.681463
0.921573
try: jwt_claims = self.get_jwt_claims(auth_token) except Exception as error: raise suppliers.UnauthenticatedException(u"Cannot decode the auth token", error) _check_jwt_claims(jwt_claims) user_info = U...
def authenticate(self, auth_token, auth_info, service_name)
Authenticates the current auth token. Args: auth_token: the auth token. auth_info: the auth configurations of the API method being called. service_name: the name of this service. Returns: A constructed UserInfo object representing the identity of the caller. ...
3.083824
2.905311
1.061444
def _decode_and_verify(): jwt_claims = jwt.JWT().unpack(auth_token).payload() _verify_required_claims_exist(jwt_claims) issuer = jwt_claims[u"iss"] keys = self._jwks_supplier.supply(issuer) try: return jws.JWS().verify_compac...
def get_jwt_claims(self, auth_token)
Decodes the auth_token into JWT claims represented as a JSON object. This method first tries to look up the cache and returns the result immediately in case of a cache hit. When cache misses, the method tries to decode the given auth token, verify its signature, and check the existence ...
5.607223
5.106103
1.098141
if options is None: # no options, don't create cache return None if not isinstance(options, (CheckOptions, QuotaOptions, ReportOptions)): _logger.error(u'make_cache(): bad options %s', options) raise ValueError(u'Invalid options') if (options.num_entries <= 0): _logge...
def create(options, timer=None, use_deque=True)
Create a cache specified by ``options`` ``options`` is an instance of either :class:`endpoints_management.control.caches.CheckOptions` or :class:`endpoints_management.control.caches.ReportOptions` The returned cache is wrapped in a :class:`LockedObject`, requiring it to be accessed in a with state...
5.06389
4.181276
1.211087
if datetime_func is None: datetime_func = datetime.utcnow def _timer(): return (datetime_func() - datetime(1970, 1, 1)).total_seconds() return _timer
def to_cache_timer(datetime_func)
Converts a datetime_func to a timestamp_func. Args: datetime_func (callable[[datatime]]): a func that returns the current time Returns: time_func (callable[[timestamp]): a func that returns the timestamp from the epoch
3.313181
4.274142
0.775169
self.expire() expired = {k: v for (k, v) in self._tracking.items() if self.get(k) is None} for k, v in expired.items(): del self._tracking[k] self._out_deque.append(v) return self._out_deque
def out_deque(self)
The :class:`collections.deque` to which expired items are added.
3.690129
3.134109
1.177409
build.packages.install("wheel") build.packages.install("twine") build.executables.run([ "python", "setup.py", "sdist", "bdist_wheel", "--universal", "upload", ]) build.executables.run([ "twine", "upload", "dist/*" ])
def distribute(build)
distribute the uranium package
2.988497
3.135026
0.953261
global _THREAD_CLASS # pylint: disable=global-statement try: from google.appengine.api.background_thread import background_thread _THREAD_CLASS = background_thread.BackgroundThread except ImportError: _logger.error( u'Could not install appengine background threads!'...
def use_gae_thread()
Makes ``Client``s started after this use the appengine thread class.
6.519082
5.576786
1.168967
with self._lock: if self._running: return self._stopped = False self._running = True self._start_idle_timer() _logger.debug(u'starting thread of type %s to run the scheduler', _THREAD_CLASS) ...
def start(self)
Starts processing. Calling this method - starts the thread that regularly flushes all enabled caches. - enables the other methods on the instance to be called successfully
5.843136
6.067248
0.963062
with self._lock: if self._stopped: _logger.debug(u'%s is already stopped', self) return self._flush_all_reports() self._stopped = True if self._run_scheduler_directly: self._cleanup_if_stopped() ...
def stop(self)
Halts processing This will lead to the reports being flushed, the caches being cleared and a stop to the current processing thread.
8.310855
7.629501
1.089305
self.start() res = self._check_aggregator.check(check_req) if res: _logger.debug(u'using cached check response for %s: %s', check_request, res) return res # Application code should not fail because check request's don't ...
def check(self, check_req)
Process a check_request. The req is first passed to the check_aggregator. If there is a valid cached response, that is returned, otherwise a response is obtained from the transport. Args: check_req (``ServicecontrolServicesCheckRequest``): to be sent to the servi...
8.476797
6.568201
1.290581
self.start() # no thread running, run the scheduler to ensure any pending # flush tasks are executed. if self._run_scheduler_directly: self._scheduler.run(blocking=False) if not self._report_aggregator.report(report_req): _logger.debug(u'need to...
def report(self, report_req)
Processes a report request. It will aggregate it with prior report_requests to be send later or it will send it immediately if that's appropriate.
9.845939
9.102946
1.081621
if labels is not None: kw[u'labels'] = encoding.PyValueToMessage(MetricValue.LabelsValue, labels) return MetricValue(**kw)
def create(labels=None, **kw)
Constructs a new metric value. This acts as an alternate to MetricValue constructor which simplifies specification of labels. Rather than having to create a MetricValue.Labels instance, all that's necessary to specify the required string. Args: labels (dict([string, [string]]): **kw: ...
10.958944
8.826053
1.241659
prior_type, _ = _detect_value(prior) latest_type, _ = _detect_value(latest) if prior_type != latest_type: _logger.warn(u'Metric values are not compatible: %s, %s', prior, latest) raise ValueError(u'Incompatible delta metric values') if prior_type is None: ...
def merge(metric_kind, prior, latest)
Merges `prior` and `latest` Args: metric_kind (:class:`MetricKind`): indicates the kind of metrics being merged prior (:class:`MetricValue`): an prior instance of the metric latest (:class:`MetricValue`: the latest instance of the metric
3.407441
3.503196
0.972666
if mv.labels: signing.add_dict_to_hash(a_hash, encoding.MessageToPyValue(mv.labels)) money_value = mv.get_assigned_value(u'moneyValue') if money_value is not None: a_hash.update(b'\x00') a_hash.update(money_value.currencyCode.encode('utf-8'))
def update_hash(a_hash, mv)
Adds ``mv`` to ``a_hash`` Args: a_hash (`Hash`): the secure hash, e.g created by hashlib.md5 mv (:class:`MetricValue`): the instance to add to the hash
6.577706
6.196712
1.061483
md5 = hashlib.md5() update_hash(md5, mv) return md5.digest()
def sign(mv)
Obtains a signature for a `MetricValue` Args: mv (:class:`endpoints_management.gen.servicecontrol_v1_messages.MetricValue`): a MetricValue that's part of an operation Returns: string: a unique signature for that operation
5.91347
7.871776
0.751224
issuer_uri_config = self._issuer_uri_configs.get(issuer) if not issuer_uri_config: # The issuer is unknown. return jwks_uri = issuer_uri_config.jwks_uri if jwks_uri: # When jwks_uri is set, return it directly. return jwks_uri ...
def supply(self, issuer)
Supplies the `jwks_uri` for the given issuer. Args: issuer: the issuer. Returns: The `jwks_uri` that is either statically configured or retrieved via OpenId discovery. None is returned when the issuer is unknown or the OpenId discovery fails.
3.1966
2.743029
1.165354
def _retrieve_jwks(): jwks_uri = self._key_uri_supplier.supply(issuer) if not jwks_uri: raise UnauthenticatedException(u"Cannot find the `jwks_uri` for issuer " u"%s: either the issuer is unknown or " ...
def supply(self, issuer)
Supplies the `Json Web Key Set` for the given issuer. Args: issuer: the issuer. Returns: The successfully retrieved Json Web Key Set. None is returned if the issuer is unknown or the retrieval process fails. Raises: UnauthenticatedException: When this...
4.351367
3.939902
1.104435
desc_value_type = desc.valueType or ValueType.STRING # default not parsed return (self.label_name == desc.key and self.value_type == desc_value_type)
def matches(self, desc)
Determines if a given label descriptor matches this enum instance Args: desc (:class:`endpoints_management.gen.servicemanagement_v1_messages.LabelDescriptor`): the instance to test Return: `True` if desc is supported, otherwise `False`
10.678733
13.656669
0.781943
if self.update_label_func: self.update_label_func(self.label_name, info, labels)
def do_labels_update(self, info, labels)
Updates a dictionary of labels using the assigned update_op_func Args: info (:class:`endpoints_management.control.report_request.Info`): the info instance to update labels (dict[string[string]]): the labels dictionary Return: `True` if desc is supported, ...
4.676391
5.334557
0.876622
for l in cls: if l.matches(desc): return True return False
def is_supported(cls, desc)
Determines if the given label descriptor is supported. Args: desc (:class:`endpoints_management.gen.servicemanagement_v1_messages.LabelDescriptor`): the label descriptor to test Return: `True` if desc is supported, otherwise `False`
6.059226
6.542096
0.92619
resource_descs = service.monitoredResources labels_dict = {} logs = set() if service.logging: logs = _add_logging_destinations( service.logging.producerDestinations, resource_descs, service.logs, labels_dict, label_is_supported ...
def extract_report_spec( service, label_is_supported=label_descriptor.KnownLabels.is_supported, metric_is_supported=metric_descriptor.KnownMetrics.is_supported)
Obtains the used logs, metrics and labels from a service. label_is_supported and metric_is_supported are filter functions used to determine if label_descriptors or metric_descriptors found in the service are supported. Args: service (:class:`endpoints_management.gen.servicecontrol_v1_messages.S...
3.419569
3.054293
1.119595
service = self._service if not service.authentication: return {} auth_infos = {} for auth_rule in service.authentication.rules: selector = auth_rule.selector provider_ids_to_audiences = {} for requirement in auth_rule.requirement...
def _extract_auth_config(self)
Obtains the authentication configurations.
3.074576
2.786576
1.103353
service = self._service all_urls = set() urls_with_options = set() if not service.http: return for rule in service.http.rules: http_method, url = _detect_pattern_option(rule) if not url or not http_method or not rule.selector: ...
def _extract_methods(self)
Obtains the methods used in the service.
5.368587
5.101057
1.052446
res = left * right return "{left}*{right}={res}".format(left=left, right=right, res=res)
async def get_blueprint_params(request, left: int, right: int) -> str
API Description: Multiply, left * right. This will show in the swagger page (localhost:8000/api/v1/).
5.196681
3.83588
1.354756
if not service_name: service_name = _get_env_var_or_raise(_SERVICE_NAME_ENV_KEY) if not service_version: service_version = _get_service_version(_SERVICE_VERSION_ENV_KEY, service_name) _logger.debug(u'Contacting Service Management API for s...
def fetch_service_config(service_name=None, service_version=None)
Fetches the service config from Google Service Management API. Args: service_name: the service name. When this argument is unspecified, this method uses the value of the "SERVICE_NAME" environment variable as the service name, and raises ValueError if the environment variable is unset. ...
3.259498
3.061342
1.064728
return ConfigFetchWrapper(application, project_id, control_client, loader)
def add_all(application, project_id, control_client, loader=service.Loaders.FROM_SERVICE_MANAGEMENT)
Adds all endpoints middleware to a wsgi application. Sets up application to use all default endpoints middleware. Example: >>> application = MyWsgiApp() # an existing WSGI application >>> >>> # the name of the controlled service >>> service_name = 'my-service-name' >>> >>...
8.772241
26.542553
0.330497
if not isinstance(a_service, sm_messages.Service): raise ValueError(u"service is None or not an instance of Service") authentication = a_service.authentication if not authentication: _logger.info(u"authentication is not configured in service, " u"authentication che...
def _create_authenticator(a_service)
Create an instance of :class:`google.auth.tokens.Authenticator`. Args: a_service (:class:`endpoints_management.gen.servicemanagement_v1_messages.Service`): a service instance
3.618599
3.434093
1.053728
md5 = hashlib.md5() md5.update(op.consumerId.encode('utf-8')) md5.update(b'\x00') md5.update(op.operationName.encode('utf-8')) if op.labels: signing.add_dict_to_hash(md5, encoding.MessageToPyValue(op.labels)) return md5.digest()
def _sign_operation(op)
Obtains a signature for an operation in a ReportRequest. Args: op (:class:`endpoints_management.gen.servicecontrol_v1_messages.Operation`): an operation used in a `ReportRequest` Returns: string: a unique signature for that operation
3.948114
3.41882
1.154818
if not metric_names: metric_names = () if not label_names: label_names = () known_labels = [] known_metrics = [] # pylint: disable=no-member # pylint is not aware of the __members__ attributes for l in label_descriptor.KnownLabels....
def from_known_inputs(cls, logs=None, metric_names=None, label_names=None)
An alternate constructor that assumes known metrics and labels. This differs from the default constructor in that the metrics and labels are iterables of names of 'known' metrics and labels respectively. The names are used to obtain the metrics and labels from :class:`endpoints_manageme...
2.906454
2.700958
1.076083
# initialize the struct with fields that are always present d = { u'http_response_code': self.response_code, u'timestamp': time.mktime(now.timetuple()) } # compute the severity severity = _SEVERITY.INFO if self.response_code >= 400: ...
def _as_log_entry(self, name, now)
Makes a `LogEntry` from this instance for the given log_name. Args: rules (:class:`ReportingRules`): determines what labels, metrics and logs to include in the report request. now (:class:`datetime.DateTime`): the current time Return: a ``LogEntry`` generated ...
2.992644
3.033021
0.986688
if not self.service_name: raise ValueError(u'the service name must be set') op = super(Info, self).as_operation(timer=timer) # Populate metrics and labels if they can be associated with a # method/operation if op.operationId and op.operationName: ...
def as_report_request(self, rules, timer=datetime.utcnow)
Makes a `ServicecontrolServicesReportRequest` from this instance Args: rules (:class:`ReportingRules`): determines what labels, metrics and logs to include in the report request. timer: a function that determines the current time Return: a ``ServicecontrolServ...
5.58925
4.853089
1.151689
if self._cache is None: return _NO_RESULTS with self._cache as c: flushed_ops = [x.as_operation() for x in c.out_deque] c.out_deque.clear() reqs = [] max_ops = self.MAX_OPERATION_COUNT for x in range(0, len(flushed_ops), ma...
def flush(self)
Flushes this instance's cache. The driver of this instance should call this method every `flush_interval`. Returns: list[``ServicecontrolServicesReportRequest``]: corresponding to the pending cached operations
5.420157
3.833242
1.413988
if self._cache is None: return _NO_RESULTS if self._cache is not None: with self._cache as k: res = [x.as_operation() for x in k.values()] k.clear() k.out_deque.clear() return res
def clear(self)
Clears the cache.
8.545139
7.727908
1.105751
if self._cache is None: return None # no cache, send request now if not isinstance(req, sc_messages.ServicecontrolServicesReportRequest): raise ValueError(u'Invalid request') if req.serviceName != self.service_name: _logger.error(u'bad report(): serv...
def report(self, req)
Adds a report request to the cache. Returns ``None`` if it could not be aggregated, and callers need to send the request to the server, otherwise it returns ``CACHED_OK``. Args: req (:class:`sc_messages.ReportRequest`): the request to be aggregated Result: ...
5.677157
5.299903
1.071181
if not allocate_quota_response or not allocate_quota_response.allocateErrors: return _IS_OK # only allocate_quota the first error for now, as per ESP theError = allocate_quota_response.allocateErrors[0] error_tuple = _QUOTA_ERROR_CONVERSION.get(theError.code, _IS_UNKNOWN) if error_tupl...
def convert_response(allocate_quota_response, project_id)
Computes a http status code and message `AllocateQuotaResponse` The return value a tuple (code, message) where code: is the http status code message: is the message to return Args: allocate_quota_response (:class:`endpoints_management.gen.servicecontrol_v1_messages.AllocateQuotaResponse`): ...
6.574203
6.706091
0.980333
if not isinstance(allocate_quota_request, sc_messages.AllocateQuotaRequest): raise ValueError(u'Invalid request') op = allocate_quota_request.allocateOperation if op is None or op.methodName is None or op.consumerId is None: logging.error(u'Bad %s: not initialized => not signed', alloca...
def sign(allocate_quota_request)
Obtains a signature for an operation in a `AllocateQuotaRequest` Args: op (:class:`endpoints_management.gen.servicecontrol_v1_messages.Operation`): an operation used in a `AllocateQuotaRequest` Returns: string: a secure hash generated from the operation
3.573026
3.384763
1.055621
if not self.service_name: raise ValueError(u'the service name must be set') if not self.operation_id: raise ValueError(u'the operation id must be set') if not self.operation_name: raise ValueError(u'the operation name must be set') op = super(...
def as_allocate_quota_request(self, timer=datetime.utcnow)
Makes a `ServicecontrolServicesAllocateQuotaRequest` from this instance Returns: a ``ServicecontrolServicesAllocateQuotaRequest`` Raises: ValueError: if the fields in this instance are insufficient to to create a valid ``ServicecontrolServicesAllocateQuotaRequest``
3.098443
2.854879
1.085315
if self._cache is None: return [] with self._cache as c, self._out as out: c.expire() now = self._timer() for item in c.values(): if (not self._in_flush_all) and (not self._should_expire(item)): if (not item.is_...
def flush(self)
Flushes this instance's cache. The driver of this instance should call this method every `flush_interval`. Returns: list['ServicecontrolServicesAllocateQuotaRequest']: corresponding to AllocateQuotaRequests that were pending
6.798751
5.254384
1.29392
if self._cache is not None: with self._cache as c, self._out as out: self.in_flush_all = True c.clear() out.clear() # pylint: disable=no-member self.in_flush_all = False
def clear(self)
Clears this instance's cache.
5.537055
5.027883
1.10127
if self._cache is None: return signature = sign(req.allocateQuotaRequest) with self._cache as c: now = self._timer() item = c.get(signature) if item is None: c[signature] = CachedItem( req, resp, self.se...
def add_response(self, req, resp)
Adds the response from sending to `req` to this instance's cache. Args: req (`ServicecontrolServicesAllocateQuotaRequest`): the request resp (AllocateQuotaResponse): the response from sending the request
5.666236
5.099594
1.111115
transmute_func = TransmuteFunction( fn, args_not_from_request=["request"] ) handler = create_handler(transmute_func, context=context) get_swagger_spec(app_or_blueprint).add_func(transmute_func, context) for p in transmute_func.paths: sanic_path = _convert_to_sanic_path(p...
def add_route(app_or_blueprint, fn, context=default_context)
a decorator that adds a transmute route to the application
4.563713
4.29898
1.06158
return (self.metric_name == desc.name and self.kind == desc.metricKind and self.value_type == desc.valueType)
def matches(self, desc)
Determines if a given metric descriptor matches this enum instance Args: desc (:class:`endpoints_management.gen.servicecontrol_v1_messages.MetricDescriptor`): the instance to test Return: `True` if desc is supported, otherwise `False`
6.020209
5.317451
1.132161
self.update_op_func(self.metric_name, info, an_op)
def do_operation_update(self, info, an_op)
Updates an operation using the assigned update_op_func Args: info: (:class:`endpoints_management.control.report_request.Info`): the info instance to update an_op: (:class:`endpoints_management.control.report_request.Info`): the info instance to update ...
10.419102
6.842448
1.522716