content stringlengths 39 14.9k | sha1 stringlengths 40 40 | id int64 0 710k |
|---|---|---|
def sign(x: float) -> float:
"""
Função sinal: retorna 1.0 se x é maior ou igual a zero, -1.0 caso contrário.
Args:
x (float): valor a ser comparado com zero.
Returns:
float: 1.0 ou -1.0, a depender do valor de x.
"""
if x >= 0.0:
return 1.0
else:
r... | 7f38d062344660f0d5acd2defe76e0f974f20ffa | 48,082 |
def get_version(opts, app):
"""Get version of a specific app
Args:
opts (dict): Nephos options dict.
app (str): Helm application name.
Returns:
str: Desired version of Helm app, if specified. Defaults to None.
"""
if "versions" in opts and app in opts["versions"]:
r... | bf3517644904f26482dac56c598326c2bbcf5b44 | 48,087 |
def tagged_struct_columns(typegraph, struct_id):
""" Return a dict mapping the struct column names to versions tagged with the id.
"""
struct_id = str(struct_id)
typedef = 'Struct' + struct_id
colheader = ('struct_id', *typegraph.fields[typedef])
return { header:"t{:s}_{:s}".format(struct_id, he... | 022a33a32dfc9bbc964fd2294f1393a796597469 | 48,091 |
def maybe_tuple(value):
"""Return `value` as a tuple. If it is already a tuple, return it
unchanged. Otherwise return a 1-element tuple containing `value`."""
if isinstance(value, tuple):
return value
return (value,) | 6279c9a96d5362b7cdaf4b022ac3f41a40a26e28 | 48,093 |
def config_identifier(converter, model_name):
"""Create identifier of configuration based on data `converter` and `model_name`"""
return model_name.lower().replace('-', '_') + '_' + converter | 50633c10a240823e4cb7e664d480cfe459fdf8a8 | 48,094 |
def _get_id_field_from_input_field_name(input_field_name: str) -> str:
"""
Map plural input fields like children to the appropriate field child_ids in this
case.
"""
if input_field_name == "children":
return "child_ids"
return input_field_name.rstrip("s") + "_ids" | 7ce6368f6d9462718d0acf6b46099c8ae80e0e54 | 48,098 |
def valid_benefit_data(promotion):
""" Valid JSON data for creating a new Benefit object """
return {
'promotion': promotion.pk
} | 8e6ec6adb25930623ecc45bf04cadac14ad8087c | 48,106 |
def convert_string_to_pymath(strin):
""" Convert a string to a 'math' format """
if strin.strip() is not '':
return '$' + strin.replace(' ', '\ ') + '$'
else:
return strin | 74f2d99e5bcb324c3284c0e06d4af343c9c320ac | 48,109 |
def conj(pary):
"""
Returns the conjugation of 2D PitchArray
same as PitchArray.conj(), but create a new copy
Parameter:
----------
pary: PitchArray
"""
return pary.conj(inplace = False) | 36f12094dba0c4e0cf28ffb8d27e2b35c87d85b2 | 48,114 |
def to_iso639_part1(language_code):
"""
Convert codes like "en-us" to "en"
"""
return language_code.split('-', 1)[0] | 53192b1a7b0263cca5bb9c714fc01be4b4b3b1ae | 48,116 |
import types
def good_decorator_accepting_args(decorator):
"""This decorator makes decorators behave well wrt to decorated
functions names, doc, etc.
Differently from good_decorator, this accepts decorators possibly
receiving arguments and keyword arguments.
This decorato can be used indifferen... | 898d8bdcb71d47d8a083841d57122e9e16436905 | 48,125 |
def is_step(cur, prev):
"""
Checks whether pairs cur (current) and prev (previous) are consecutive tracks.
Works if disc_num or track_num is a single letter
:param cur: [disc_num, track_num]
:param prev: [disc_num, track_num]
:return: bool. True if cur comes after prev, False otherwise
"""
... | 4c5238fb21faa18c44fca2ea457da29f42dbfba3 | 48,126 |
import six
def check(pkt, pktType, keyMatches=None, **valMatches):
"""This function takes an object that was expected to come from a packet (after it has been JSONized)
and compares it against the arg requirements so you don't have to have 10 if() statements to look for keys in a dict, etc..
Args... | a2e9819cf25ed2d919da74e2ff3141537303cb0e | 48,127 |
def calc_kl_div(a,b):
"""
Calculates the KL-divergence with ref tensor a, comparing to a new tensor b.
source of formula: https://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence
"""
return (a * (a / b).log()).sum() | 9c2c545a15d863561604304661d1071f5b88de90 | 48,128 |
def _get_instance_list(mig, field='name', filter_list=None):
"""
Helper to grab field from instances response.
:param mig: Managed Instance Group Object from libcloud.
:type mig: :class: `GCEInstanceGroupManager`
:param field: Field name in list_managed_instances response. Defaults
... | efcb7c948583e7433ff30030cd934903f9953632 | 48,133 |
def comp_nthoctave_axis(noct, freqmin, freqmax):
"""Computes the frequency vector between freqmin and freqmax for the 1/n octave
Parameters
----------
noct: int
kind of octave band (1/3, etc)
freqmin: float
minimum frequency
freqmax: float
maximum frequency
Returns
... | ab7ef798021f8ee2872e5b2e0040860c80639647 | 48,134 |
def get_clauses(id_dict, db):
"""Get a list of clauses to be passed to a db query.
Note that an empty condition will be returned if id_dict has no ids in it
(either the dict is empty or all the lists within the dict are empty),
which will in general have the unexpected effect of selecting everything,
... | 86effb5e1f035f3ba30bd7e305a2c71a933a3c33 | 48,136 |
def frontiers_from_bar_to_time(seq, bars):
"""
Converts the frontiers (or a sequence of integers) from bar indexes to absolute times of the bars.
The frontier is considered as the end of the bar.
Parameters
----------
seq : list of integers
The frontiers, in bar indexes.
bars : list... | ea86d14725a6761ba90ba4b62b7f78d687e6a269 | 48,145 |
def bin2string (arr):
""" Converts the binary number array 'arr' to string format """
bin_string = ''
for bits in arr:
bin_string += str(int(bits))
return bin_string | 1d0ae72bb93732562c51a1380bb519ccfdc1db31 | 48,148 |
def is_app_code(code: int) -> bool:
"""
Checks whether a code is part of the app range.
:param code: Code to check.
:return: Boolean indicating whether `code` is within the app range.
"""
return 0 < code < 0x10 | d0bd6485875500418b5ddd60ae5fb3c9b965b71f | 48,154 |
def get_group_lists(self, sym_grouping):
"""Gets the index list for left and right groups."""
left_idx = [k for k in range(sym_grouping[0])]
right_list = [k + sym_grouping[0] for k in range(sym_grouping[1])]
return left_idx, right_list | b3adf2e12c7fa2cb4ac5ecdd47c8f9b18a77c49b | 48,160 |
def actions(board):
"""
Returns set of all possible actions (i, j) available on the board.
"""
available_moves = set()
for x, row in enumerate(board):
for y, column in enumerate(row):
if column is None:
available_moves.add((x, y))
return available_moves | b6d4057e3c5c369a8cadd4c10d7175e5003824bd | 48,163 |
def chromatic_induction_factors(n):
"""
Returns the chromatic induction factors :math:`N_{bb}` and :math:`N_{cb}`.
Parameters
----------
n : numeric
Function of the luminance factor of the background :math:`n`.
Returns
-------
tuple
Chromatic induction factors :math:`N_... | d05e9243d3ee4a7255f59435bb02131638142f9f | 48,164 |
import torch
def distance2bbox(points, distance, max_shape=None):
"""Decode distance prediction to bounding box.
Args:
points (Tensor): Shape (n, 2), [x, y].
distance (Tensor): Distance from the given point to 4
boundaries (left, top, right, bottom).
max_shape (tuple): Shap... | 1a7021e32398625b87a847fc82f7316137dbe5cf | 48,165 |
def can_self_enroll_in_course(course_key):
"""
Returns True if the user can enroll themselves in a course.
Note: an example of a course that a user cannot enroll in directly
is a CCX course. For such courses, a user can only be enrolled by
a CCX coach.
"""
if hasattr(course_key, 'ccx'):
... | e5b9b66bb2885a17afbd947093cfbb4088095e7d | 48,166 |
def depth_to_col_name(depth):
"""
Derives the proper name of the column for locations given a depth.
"""
if depth == 0:
return "location"
else:
return "sub_" * depth + "lctn" | e6ae7a4e3563bf762dfe0a82d514314a849da0a6 | 48,176 |
def _determine_sentence_id(sentence, new_id, id_name, old_id):
"""
Determine the appropriate id for this sentence.
Ids here means doc id or par id.
Args:
sentence: The sentence whose ids to check.
new_id: The key that the id can come up as without the id key word.
id_name: The id in the co... | 95a5f1987a9037df1fb7e73b2b47c8da2b265e1c | 48,179 |
def string_slice(strvar,slicevar):
""" slice a string with |string_slice:'[first]:[last]'
"""
first,last= slicevar.partition(':')[::2]
if first=='':
return strvar[:int(last)]
elif last=='':
return strvar[int(first):]
else:
return strvar[int(first):int(last)] | 1a52d6a7d71a6c1c116dcd9c1eef6d5d5962cd07 | 48,180 |
def get_cumulative_rewards(rewards, gamma=0.99):
"""
rewards: rewards at each step
gamma: discount for reward
"""
discounted_rewards = []
for rew in range(len(rewards)):
Gt = 0
pw = 0
for r in rewards[rew:]:
Gt = Gt + gamma ** pw * r
pw = pw + 1
... | bd8716f6cfb1261048ebcd99dfeac023eef78e49 | 48,182 |
from typing import Any
def is_number(val: Any) -> bool:
"""
Check if a value is a number.
"""
return isinstance(val, (int, float, complex)) | 37a62e0a846a22ee58c9bdc96cf63bfeb34bc539 | 48,191 |
def increment_ipv4_segments(segments):
"""
Increment an ip address given in quad segments based on ipv4 rules
:param segments: IPv4 segments to increment.
:type segments: ``list`` or ``tuple``
:return: Incremented segments.
:rtype: ``list``
"""
segments = [int(segment) for segment in s... | fd2f9ea2c74fa3546815b53e20407b63cee6b1a4 | 48,192 |
def _calculate_num_runs_failures(list_of_results):
"""Caculate number of runs and failures for a particular test.
Args:
list_of_results: (List) of JobResult object.
Returns:
A tuple of total number of runs and failures.
"""
num_runs = len(list_of_results) # By default, there is 1 run per JobResu... | f67a6b1fa6a5aefc19bfe7e99ed77473801e6b83 | 48,193 |
def test_request_response(connection, receiver):
"""
Test request/response messaging pattern.
"""
def endpoint_callback(message):
return message.payload + "-pong"
connection.register_async_endpoint(endpoint_callback, "test.request")
connection.call_async(receiver.create_cbf(), "test.re... | 200dd1c9dcf752130b7748323ea9d269a9d53eab | 48,196 |
from datetime import datetime
import logging
def get_now_time(format="%Y-%m-%d %H:%M:%S", show=False):
"""
Gets current time.
"""
now = datetime.now()
now = now.strftime(format)
if show == True:
logging.info("Current time: {}".format(now))
return now | 1cb1cc85c102601890319a7fc057484b3c79360a | 48,197 |
def warning_formatter(msg, category, filename, lineno, line=None):
"""Format warning to only print filename, linenumber and message.
Parameters
----------
msg
warning message
category
warning category
filename
filename of file where warning was raised
lineno
... | 8b0c35077ca5c1872eefa00616e36ea67d40c93d | 48,198 |
def parse_etraveler_response(rsp, validate):
""" Convert the response from an eTraveler clientAPI query to a
key,value pair
Parameters
----------
rsp : return type from
eTraveler.clientAPI.connection.Connection.getHardwareHierarchy
which is an array of dicts information about the 'c... | b8f8d0cb395889dd2266e926abbf323c6ea7ae84 | 48,201 |
def hex_dist(x1, y1, z1, x2, y2, z2):
"""Returns how many steps one hex is from another"""
return (abs(x1 - x2) + abs(y1 - y2) + abs(z1 - z2)) // 2 | a9a49fb956292ff91cfb582be0c0a43b227d6285 | 48,205 |
def _get_repo_path(repo_url):
"""
Extracts the username/reponame from the given github URL
:param repo_url: (str) Full https path to the github repository
:return: (str) <username>/<reponame>
"""
position = repo_url.find("github.com")
name = ""
if position >= 0:
name = repo_url[... | 108b395c596364a66675c217965bebe57df506e9 | 48,211 |
def nombre_joueurs_annee(annee,liste):
"""
Fonction qui en paramètre prend une année (int)
et la liste des joueurs sous forme de liste de dictionnaires
et qui retourne le nombre de joueurs ayant joués l'année donnée
"""
compteur = 0
for enreg in liste :
if int(enreg['Année']) == anne... | 75ae36a2cac32bd6b02a4a3ade1cfab82af8f575 | 48,220 |
def longest_line_length(code):
"""Return length of longest line."""
if len(code) == 0:
return 0
return max(len(line) for line in code.splitlines()) | cdf83015969293e5b6a0645bc9a99878e230696e | 48,227 |
def count_valid(passports : list[dict[str, str]], fields : list[str]) -> int:
"""Counts the passports that have the right fields"""
return sum(
all(field in passport for field in fields)
for passport in passports
) | aa17d08fb77bc0d1b5346db45e8f72c8e4ad9094 | 48,228 |
def top_sentences(query, sentences, idfs, n):
"""
Given a `query` (a set of words), `sentences` (a dictionary mapping
sentences to a list of their words), and `idfs` (a dictionary mapping words
to their IDF values), return a list of the `n` top sentences that match
the query, ranked according to idf... | 0440d2bec742f88728c294e9e359e18d38a106a7 | 48,232 |
def clean_up_feature_sets(*feature_sets, earliest_date: dict, last_date: dict) -> list:
"""Leave only features from inside the observation window."""
results = []
for feats in feature_sets:
results.append(feats[(feats.DATE < feats.SUBJECT_ID.map(last_date))
& (feats.DATE... | 06e4ac3713dce63a4237694fa6b6b0ed850216f6 | 48,233 |
from datetime import datetime
def str2timestamp(str_timestamp: str, date_format: str = '%d.%m.%Y %H:%M:%S') -> datetime:
"""
convert a string into a datetime object
:param str_timestamp:
:param date_format:
:return:
"""
return datetime.strptime(str_timestamp, date_format) | fc66d55fdd7004d8ff1299cdc6adf39412701bd7 | 48,236 |
def str2bool(stuff):
"""Converts a string to a Boolean as a human would expect."""
return stuff.lower() in ("yes", "true", "y", "1") | f48b8c5062ecf072c06903bfd1f9638926606811 | 48,237 |
def _check_if_any_list_value_in_string(listx, line):
"""
Internal method to test if any of the list value present in a line.
"""
flag = False
for value in listx:
if value in line:
flag = True
break
return flag | c5461fa063c0148e4017a94c2f3540dbc3bd2b7e | 48,248 |
def flatten_lists_to_csv(data):
"""Converts the passed in data to csv.
Assuming:
x = [
["v1", 98, 23],
["v2", 0.25, 0.56],
]
then flatten_lists_to_csv(data) will return the following string:
v1,v2
98,0.25
23,0.56
:param list data: A list of lists ho... | d02297c9829dac0fda4629e69db09cceae402a7a | 48,249 |
import re
def re_search(pattern, text, plural=False):
"""Regex helper to find strings in a body of text"""
match = [m.group(1) for m in re.finditer(pattern, text)]
if plural:
return match
else:
if match:
return match[0] | 66998eb3b29978260eb60603cf440f95a16eb532 | 48,252 |
import copy
def merge_rows(row1, row2):
"""Merge two rows of the table of CVE data"""
output = copy.deepcopy(row1)
for key in row2:
if key not in output:
output[key] = row2[key]
elif output[key] == row2[key]:
continue
elif key == 'References':
ou... | 62e206636f4775efc2173ea6a835ec7a7c1c3d1f | 48,254 |
def _year_to_decade(yr):
"""
A simple function so I don't mess this up later, this constructs the *redistricting*
decade of a district. This is offset from the regular decade a year is in by two.
"""
return (yr - 2) - (yr - 2) % 10 | 69c971f422801d260bdd5151ce47165785f9a46b | 48,255 |
def positive(number: int) -> int:
"""
:return: Number, or 1 if number is negative or 0
"""
return max(1, number) | 82380388554c5f42096e33509424c5f67167c463 | 48,256 |
import click
def _cb_key_val(ctx, param, value):
"""
from: https://github.com/mapbox/rasterio/blob/69305c72b58b15a96330d371ad90ef31c209e981/rasterio/rio/options.py
click callback to validate `--opt KEY1=VAL1 --opt KEY2=VAL2` and collect
in a dictionary like the one below, which is what the CLI funct... | ed06b49232921fb63f2e99269ea1d32fb1bdaa84 | 48,260 |
def get_path(obj, path):
"""Fetch data from an object with nested, or return None. Avoids raising."""
for each in path:
try:
obj = obj[each]
except Exception:
return None
return obj | 799c3a68ecdc6662ad5d2747501ff05ae93fe39b | 48,266 |
def convert_collection_to_path_param(collection):
"""
Convert a list of elements to a valid path parameter by concatenating
with ",".
This is used when calling some endpoints that require a list of instance ids.
If the collection parameter is not of type ``list``, ``tuple``, ``str`` or ``int``, ``N... | 3fa52bbc39c32c5e088338b6e8f1f84ec39a3630 | 48,269 |
def find_letter(letters, l, index):
"""
Find the first occurrence of a letter in a word after a given index.
Searches forward in the word after index-th letter. If no matching letter is found,
search backwards for the latest occurrence before the index-th letter.
:return: index of the found occurr... | 5e681a2734996055eb6d117af14ff8b1f11e9ab3 | 48,275 |
def seat (bpass):
"""Returns the seat ID for the given boarding pass (`bpass`)."""
row = sum(2**(6-n) for n, s in enumerate(bpass[0:7]) if s == 'B')
col = sum(2**(2-n) for n, s in enumerate(bpass[7:] ) if s == 'R')
return (row * 8) + col | 29b6e929798ccbd6a2c3aaf916940df75844d176 | 48,279 |
from typing import List
def _break_line_in_two(line: str, max_chars_per_line: int) -> List[str]:
"""Breaks a line into the first line lower than max_char_per_line and the remaining string"""
if len(line) <= max_chars_per_line:
return [line]
position = 0
while position < max_chars_per_line:
... | d7997ee8e5033ebf5b109c126181b9978a622a0e | 48,281 |
def has_access(user, workflow):
"""Calculate if user has access to workflow.
:param user: User object
:param workflow: Workflow object
:return: True if it is owner or in the shared list
"""
return workflow.user == user or user in workflow.shared.all() | d42ec8b9f7ca8dc7842b8a9950c1374ae7afcfbd | 48,290 |
def abs_value_equal(x, y):
"""Return whether or not the absolute value of both numbers is the same.
Please refrain from using libraries (abs)
>>> abs_value_equal(-2, -2)
True
>>> abs_value_equal(-3, 3)
True
>>> abs_value_equal(1, 2)
False
>>> abs_value_equal(3, 3)
True
>>> ... | 5047caa23e0e1fd78e8696f706c5aa8ad43b0e22 | 48,291 |
def ListVersions(client, messages, pkg, version_view):
"""Lists all versions under a package."""
list_vers_req = messages.ArtifactregistryProjectsLocationsRepositoriesPackagesVersionsListRequest(
parent=pkg, view=version_view)
list_vers_res = client.projects_locations_repositories_packages_versions.List(
... | ea7b3c320817094df3bc23b05cf9cb9f18c2a7d1 | 48,292 |
import typing
def decode_byte_array(as_bytes: typing.List[int]) -> bytes:
"""Decodes a byte array.
"""
return bytes(as_bytes) | a029272fc8ab76e169d1cc15ae7b248fac9ea719 | 48,297 |
import struct
def hex_to_32bit_IEEE754_float(hex_str):
"""Transform a string containing a hexidecimal representation of a 32-bits
IEEE754-formatted float value to a float
"""
return (struct.unpack("f", struct.pack("i", int(hex_str, 16))))[0] | 961b87e7774268bcb3e55459097397d7cfd22b4c | 48,299 |
def subsample(y, limit=256, factor=2):
"""
If a given Series is longer than `limit`, returns subsampled sequence by the specified integer factor
"""
if len(y) > limit:
return y[::factor].reset_index(drop=True)
return y | dda6aa9ec00276e94be1db7354cbe3ad830f728b | 48,301 |
from typing import Union
import torch
def AvgPoolNd(
dim: int,
kernel: int,
stride: Union[None, int] = None,
dtype=None,
):
"""Average pooling layer.
Args:
dim (int): Dimensionality.
kernel (int): Kernel size.
stride (int, optional): Stride.
dtype (dtype): Data... | 5488f87b9f371bba9bd5de4f29a2e04e799825db | 48,309 |
def logout_user(_):
"""Log user out."""
return {}, 200 | edb75ddc32f905c62600ef7706ccd060d02f466c | 48,310 |
from typing import Any
def default_serialize(value: Any) -> str:
"""Default value serializer.
``None`` -> ``""``
``value: Union[bool, int, float, str]`` -> ``str(value)``
``value: Any`` -> ``repr(value)``
"""
if isinstance(value, str):
return value
if isinstance(value, (bool, int,... | fdb96224b765cd2eef8bb33bd04b8bc6f9a6510e | 48,315 |
def tuple_(*args):
""":yaql:tuple
Returns tuple of args.
:signature: tuple([args])
:arg [args]: chain of values for tuple
:argType [args]: chain of any types
:returnType: tuple
.. code::
yaql> tuple(0, [], "a")
[0, [], "a"]
"""
return args | 54474e0230e3821903d60f441c698cb73beec596 | 48,316 |
import re
def get_web_element_attribute_names(web_element):
"""Get all attribute names of a web element"""
# get element html
html = web_element.get_attribute("outerHTML")
# find all with regex
pattern = """([a-z]+-?[a-z]+_?)='?"?"""
return re.findall(pattern, html) | 33aa9d0ab94525e6c68581aa4ce45c457e269bc4 | 48,320 |
import torch
def sort_rows(m, n_rows):
"""sort N*M matrix by row
Args:
m (torch.Tensor): N*M matrix to be sorted
n_rows (int): no of rows to be sorted
Returns:
sorted (torch.Tensor): N*M matrix with sorted row
"""
m_T = m.transpose(1, 0)
sorted_m = torch.to... | c8c27cc05302d55750e54600bbebe1726939dd83 | 48,321 |
from typing import Any
def _isstr(value: Any) -> bool:
"""
Check to see if this is a stringlike or a (nested) iterable of stringlikes
"""
if isinstance(value, (str, bytes)):
return True
if hasattr(value, "__iter__"):
return all(_isstr(v) for v in value)
return False | a1232c628f3a174297e4db03f239673c8b11bd16 | 48,322 |
def c_to_f(temperature):
"""Converts temperature from celcius to fahrenheit
Args:
temperature: floating point representing the temperature in celcius
Returns: temperature in fahrenheit
"""
if temperature is None:
return None
return (temperature * 9 / 5) + 32 | 1606e0f64fb5ebe8146c63a3a78d3b47fbaf9871 | 48,323 |
import torch
def adversarial_loss(prob, label):
"""Compute adversarial losses in GAN networks.
Note:
As a reminder, prob is either D(x) or D(G(z)), and label is
either 0 (fake) or 1 (real). With BCELoss(), this means that
- l(D(x), 1) = -log(D(x)): "Real" discriminator loss
-... | 1eb7eb8e5d82a354e4272fa5de57d67f30ceca92 | 48,328 |
def create_vocab(docs):
"""Create a vocabulary for a given set of documents"""
words = set()
for d in docs:
for w in d:
words.add(w)
vocab = {}
for i, w in enumerate(list(words)):
vocab[w] = i
return vocab | e02a5b6d1c8eb18ef6fbd279b3ecd7f0b62d1107 | 48,335 |
def partition(length, parts):
"""Partitions 'length' into (approximately) equal 'parts'."""
sublengths = [length//parts] * parts
for i in range(length % parts): # treatment of remainder
sublengths[i] += 1
return sublengths | d398b104ed434d076244b00ad4dc8876821e3e75 | 48,339 |
import re
def parse_checksum_row(row):
""" Args:
row: a line of text from pt-table-checksum
Returns: An array of elements, if the regex matches
[ts, errors, diffs, rows, chunks, chunks_skipped,
elapsed_time, db, tbl]
Ex: [ '08-30T06:25:33', '0', '0', '28598',... | 4f239ba582c07a7135d00e7078ec578dcd13de83 | 48,342 |
def get_list_of_multiple_or_one_or_empty_from_dict(input, name, vtype=None):
"""
Extracts objects by 'name' from the 'input' and returns as a list.
Tries both plural and singular names from the input.
If vtype is specified, tries to convert each of the elements in the result to this type.
:param in... | b038bd1c2ff390d32bd058344f48175ca8071b27 | 48,345 |
def lower_keys(obj: dict) -> dict:
"""
Lowercase first level keys of a dictionary
:param obj: object to be lowercased
:return: same object with lowercase keys in first level
"""
nu_obj = dict()
for key in obj:
nu_obj[key.lower()] = obj[key]
return nu_obj | e3043d3b850128bb94b9c9388ab59e279a87bc40 | 48,351 |
def CSVWriter (iterable, outLoc, header="", ):
"""
Writes an iterable to a CSV file.
:param iterable: List of list
:param outLoc: file location. Where to place it.
:param header: header of the CSV file
:return: 1
"""
if not iterable:
print ("nothing to write")
return 0
... | 25a800d84b5ad4733fa6a4205f49823191a8523f | 48,353 |
def fibonacci(n):
"""
Returns a list of fibonacci numbers of length n
Parameters
----------
n : int
Number in fibonacci suite desired
Returns
-------
fib_list : list[ints]
List of integers
"""
memo = [0, 1]
for i in range(2, n):
memo += [memo[i - 2]... | 5c2e29732c383b92a7a56fd618418d4301bbcd3c | 48,362 |
import math
def get_number_elements_per_split(total_elements, num_splits):
# type: (int, int) -> int
"""Compute the number of elements in each split"""
return math.ceil(total_elements / float(num_splits)) | 9f80c0db1042291afe7fa9513464ff186946feac | 48,365 |
def fit_in_range(value, min_value, max_value):
"""Make the given value fit into the range min_value..max_value"""
if value < min_value:
value = min_value
elif value > max_value:
value = max_value
return value | e12de739061ce1c6b8ca72502a15cafd58b56555 | 48,369 |
def transpose_list(list_of_dicts):
"""Transpose a list of dicts to a dict of lists.
:param list_of_dicts: to transpose, as in the output from a parse call
:return: Dict of lists
"""
res = {}
for d in list_of_dicts:
for k, v in d.items():
if k in res:
res[k].a... | 48c0213992f1c0a3d542f5631bad1522a096ab14 | 48,373 |
def get_cond_latents_at_level(cond_latents, level, hparams):
"""Returns a single or list of conditional latents at level 'level'."""
if cond_latents:
if hparams.latent_dist_encoder in ["conv_net", "conv3d_net"]:
return [cond_latent[level] for cond_latent in cond_latents]
elif hparams.latent_dist_encod... | 6a2184d059753b8a946ec24bf4e1bbff6efe9910 | 48,375 |
def prepare_fetch_incidents_query(fetch_timestamp: str,
fetch_severity: list,
fetch_table: str,
fetch_subtype: list,
fetch_fields: str,
fetch_limit: s... | a68e0c9bac3d6ef451d772bffbc0fd43e23b7360 | 48,376 |
def fancy_retrieve(broker, num_messages=1):
"""
Fancy wrapper that combines several operations to retrieve a message from the message broker.
:param broker:
The Broker instance
:param num_messages:
The number of messages to retrieve from the queue
:rtype:
list
:return:
... | 24668471841a3bb71691d2dcd3d715bd97626e53 | 48,377 |
def HasLib(parsed, name, version=None):
"""Check if the parsed yaml has specified the given library.
Args:
parsed: parsed from yaml to python object
name: str, Name of the library
version: str, If specified, also matches against the version of the library.
Returns:
True if library with optionall... | 67b1fbf0823eeaaaf666fe6f08af817f622e19b4 | 48,381 |
def find_biggest_value_per_day(day_data):
"""
Take pressure data per day and find
biggest value.
If some systolic and other systolic equal,
compare by diastolic
"""
values = [(data[2], data[3]) for data in day_data]
systolic, diastolic = max(values)
return systolic, diastolic | f46a866809278b95851c8583a1682de58749a64f | 48,384 |
def db_row_count(cursor, schema_table):
"""
:param cursor: Postgres formatted database connection string
:type cursor: psycopg2.cursor
:param schema_table: schema.table
:type schema_table: str|unicode
:return: number of rows in the specified schema.table
:rtype: int
"""
query = "... | 7c8547940233fb9b6bde894badf8096938ed7892 | 48,385 |
def datasetsListIntoTree(dList):
"""Transforms list of datasets into a neested dict where keys are year and quartal"""
d = {}
for rec in dList:
if rec.quartal:
if rec.year not in d:
d[rec.year] = [None] * 4
d[rec.year][rec.quartal - 1] = rec
else:
d[rec.year] = rec
return d | 37a9ea05d919d02ff5bcf3844ee9df78ea2ba040 | 48,387 |
import hashlib
def sha256_hex(hash_input):
""" Return 64-character hex representation of SHA256 of input.
Args:
hash_input (obj): a python object having a string representation.
Returns:
length-64 hexadecimal string representing result of applying
SHA256 to utf-8 encoding of stri... | 1261bbe0b9a208653ab7b069b4d6a64c24c9b57c | 48,390 |
import shutil
def tpl_repo(request, tmp_path, tpl_path):
"""
Creates a temporary folder containing a set of test templates.
The templates to copy can be passed in by using the tpl_repo_contents mark.
Used for testing installation of template directories.
"""
repo_dir = tmp_path / "template_r... | 7299c86555595a063d9d043fcf1c5ebf7c5c4c6f | 48,392 |
def get_base_name(full_name: str) -> str:
"""Extract the base name of a package.
Args:
full_name: Full name of the package of interest, e.g., pandas.testing
Returns:
Base name of the provided package, e.g., pandas
"""
return full_name.split('.')[0] | 5a9fb854021fd878afe729dd938a4415b4ad39b5 | 48,395 |
def project_directory(packaged_scene, package_root, source_scene):
"""
Project directory to use in scene settings
Returns:
Project directory str
"""
return " project_directory \"[python nuke.script_directory()]\"\n" | 21e6ea43cacadd9adaa0ef760b34384be770562a | 48,397 |
def color_str_yellow(s):
"""Color string YELLOW for writing to STDIN."""
return "\033[93m{}\033[00m".format(s) | 4c02d3a111da7213dd887bb168f7f77b0b733f41 | 48,399 |
def cyclic_fetch_elements_in_array(array, start_index, searchable_size):
"""
Fetch elements without worrying about reaching the end of the array
Args:
array (list of Any): anything in the form of array, can be an array of ADT
start_index (int): the starting index to slice from
searc... | 663b2fbbd576d926853e805715a7164b015e6c05 | 48,405 |
def value_for_key(data, keypath, default=None, exception_on_miss=False):
"""Returns the value at the given *keypath* within :attr:`values`.
A key path is a list of components delimited by dots (periods). The components are interpreted
as dictionary keys within the structure.
For example, the key p... | f563fd845d6138d8651caf8780a1089b2b2d99f9 | 48,407 |
def is_json(input_file):
"""
Check if the file is in JSON format.
The function reads the first character of the file, and if it is "{" then returns True.
:param input_file: file name (string)
:return: Boolean.
"""
with open(input_file) as unknown_file:
c = unknown_file.read(1)
... | 9213688ec0fc6d0b659360ba37dc5c740886e571 | 48,408 |
from pathlib import Path
from datetime import datetime
def detect_new_day(lock_file: Path = Path('.daylock')):
"""
Return true when executed on a different day than the last time.
It uses a file (lock_file) to keep track of the last day
"""
day_of_the_year = datetime.now().timetuple().tm_yday
... | a56ec8cca97ee423b163d0587d0cb1d90371b17a | 48,414 |
from typing import Iterable
import pathlib
from typing import Sequence
def get_modified_files(
files: Iterable[pathlib.Path], *, since: float
) -> Sequence[pathlib.Path]:
"""Return the files that were modified since the given timestamp."""
return [f for f in files if f.stat().st_mtime > since] | 1576da300654d6f4aa9dbc331fc23740bc25dd7c | 48,416 |
def share_percentage(df, columns):
"""
This function shows the balance in each column of dataframe.
For each column, it returns the list of unique values together with
the percentange of each value in the dataset.
"""
shares_cols = dict()
nrowss = len(df)
for col in columns:
... | b95ace6ae03d29b5bfaf0958719586013f435b2d | 48,423 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.