docstring stringlengths 52 499 | function stringlengths 67 35.2k | __index_level_0__ int64 52.6k 1.16M |
|---|---|---|
Asynchronously get data from Chunked transfer encoding of https://smartcity.rbccps.org/api/0.1.0/subscribe.
(Only this function requires Python 3. Rest of the functions can be run in python2.
Args:
url (string): url to subscribe | async def asynchronously_get_data(self, url):
headers = {"apikey": self.entity_api_key}
try:
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(verify_ssl=False)) as session:
async with session.get(url, headers=headers, timeout=3000) as response:
... | 907,744 |
Copies a file from its location on the web to a designated
place on the local machine.
Args:
file_path: Complete url of the file to copy, string (e.g. http://fool.com/input.css).
target_path: Path and name of file on the local machine, string. (e.g. /directory/output.css)
Returns:
... | def copy_web_file_to_local(file_path, target_path):
response = urllib.request.urlopen(file_path)
f = open(target_path, 'w')
f.write(response.read())
f.close() | 907,842 |
Counts the number of lines in a file.
Args:
fname: string, name of the file.
Returns:
integer, the number of lines in the file. | def get_line_count(fname):
i = 0
with open(fname) as f:
for i, l in enumerate(f):
pass
return i + 1 | 907,843 |
Indentes css that has not been indented and saves it to a new file.
A new file is created if the output destination does not already exist.
Args:
f: string, path to file.
output: string, path/name of the output file (e.g. /directory/output.css).
print type(response.read())
Returns:
... | def indent_css(f, output):
line_count = get_line_count(f)
f = open(f, 'r+')
output = open(output, 'r+')
for line in range(line_count):
string = f.readline().rstrip()
if len(string) > 0:
if string[-1] == ";":
output.write(" " + string + "\n")
... | 907,844 |
Adds line breaks after every occurance of a given character in a file.
Args:
f: string, path to input file.
output: string, path to output file.
Returns:
None. | def add_newlines(f, output, char):
line_count = get_line_count(f)
f = open(f, 'r+')
output = open(output, 'r+')
for line in range(line_count):
string = f.readline()
string = re.sub(char, char + '\n', string)
output.write(string) | 907,845 |
Adds a space before a character if there's isn't one already.
Args:
char: string, character that needs a space before it.
input_file: string, path to file to parse.
output_file: string, path to destination file.
Returns:
None. | def add_whitespace_before(char, input_file, output_file):
line_count = get_line_count(input_file)
input_file = open(input_file, 'r')
output_file = open(output_file, 'r+')
for line in range(line_count):
string = input_file.readline()
# If there's not already a space before the charac... | 907,846 |
Reformats poorly written css. This function does not validate or fix errors in the code.
It only gives code the proper indentation.
Args:
input_file: string, path to the input file.
output_file: string, path to where the reformatted css should be saved. If the target file
doesn't exis... | def reformat_css(input_file, output_file):
# Number of lines in the file.
line_count = get_line_count(input_file)
# Open source and target files.
f = open(input_file, 'r+')
output = open(output_file, 'w')
# Loop over every line in the file.
for line in range(line_count):
# Eli... | 907,847 |
Checks if a string is an integer. If the string value is an integer
return True, otherwise return False.
Args:
string: a string to test.
Returns:
boolean | def is_int(string):
try:
a = float(string)
b = int(a)
except ValueError:
return False
else:
return a == b | 907,848 |
Take a list of strings and clear whitespace
on each one. If a value in the list is not a
string pass it through untouched.
Args:
iterable: mixed list
Returns:
mixed list | def clean_strings(iterable):
retval = []
for val in iterable:
try:
retval.append(val.strip())
except(AttributeError):
retval.append(val)
return retval | 907,850 |
Uses Heron's formula to find the area of a triangle
based on the coordinates of three points.
Args:
point1: list or tuple, the x y coordinate of point one.
point2: list or tuple, the x y coordinate of point two.
point3: list or tuple, the x y coordinate of point three.
Returns:
... | def triangle_area(point1, point2, point3):
a = point_distance(point1, point2)
b = point_distance(point1, point3)
c = point_distance(point2, point3)
s = (a + b + c) / 2.0
return math.sqrt(s * (s - a) * (s - b) * (s - c)) | 907,853 |
Calculates the area of a regular polygon (with sides of equal length).
Args:
number_of_sides: Integer, the number of sides of the polygon
length_of_sides: Integer or floating point number, the length of the sides
Returns:
The area of a regular polygon as an integer or floating point n... | def regular_polygon_area(number_of_sides, length_of_sides):
return (0.25 * number_of_sides * length_of_sides ** 2) / math.tan(
math.pi / number_of_sides
) | 907,854 |
Calculates the median of a list of integers or floating point numbers.
Args:
data: A list of integers or floating point numbers
Returns:
Sorts the list numerically and returns the middle number if the list has an odd number
of items. If the list contains an even number of items the me... | def median(data):
ordered = sorted(data)
length = len(ordered)
if length % 2 == 0:
return (
ordered[math.floor(length / 2) - 1] + ordered[math.floor(length / 2)]
) / 2.0
elif length % 2 != 0:
return ordered[math.floor(length / 2)] | 907,855 |
Calculates the average or mean of a list of numbers
Args:
numbers: a list of integers or floating point numbers.
numtype: string, 'decimal' or 'float'; the type of number to return.
Returns:
The average (mean) of the numbers as a floating point number
or a Decimal object.
... | def average(numbers, numtype='float'):
if type == 'decimal':
return Decimal(sum(numbers)) / len(numbers)
else:
return float(sum(numbers)) / len(numbers) | 907,856 |
Calculate net take-home pay including employer retirement savings match
using the formula laid out by Mr. Money Mustache:
http://www.mrmoneymustache.com/2015/01/26/calculating-net-worth/
Args:
take_home_pay: float or int, monthly take-home pay
spending: float or int, monthly spending
... | def savings_rate(take_home_pay, spending, numtype='float'):
if numtype == 'decimal':
try:
return (
(Decimal(take_home_pay) - Decimal(spending)) / (Decimal(take_home_pay))
) * Decimal(100.0)
# Leave InvalidOperation for backwards compatibility
exc... | 907,860 |
Send a reply message of the given type
Args:
- message: the message to publish
- message_type: the type of message being sent | def reply(self,message,message_type):
if message_type == MULTIPART:
raise Exception("Unsupported reply type")
super(Replier,self).send(message,message_type) | 908,283 |
Parse a GPX file into a GpxModel.
Args:
gpx_element: The root <gpx> element of an XML document containing a
version attribute. GPX versions 1.0 and 1.1 are supported.
gpxns: The XML namespace for GPX in Clarke notation (i.e. delimited
by curly braces).
Returns:
... | def parse_gpx(gpx_element, gpxns=None):
gpxns = gpxns if gpxns is not None else determine_gpx_namespace(gpx_element)
if gpx_element.tag != gpxns+'gpx':
raise ValueError("No gpx root element")
version = gpx_element.attrib['version']
if version == '1.0':
return parse_gpx_1_0(gpx_el... | 908,441 |
Add the args
Args:
args (namespace): The commandline args | def add_args(self, args):
for key, value in vars(args).items():
if value is not None:
setattr(self, key.upper(), value) | 908,644 |
Load the contents from the ini file
Args:
ini_file (str): The file from which the settings should be loaded | def load_ini(self, ini_file):
if ini_file and not os.path.exists(ini_file):
self.log.critical(f"Settings file specified but not found. {ini_file}")
sys.exit(1)
if not ini_file:
ini_file = f"{self.cwd}/settings.ini"
if os.path.exists(ini_file):
... | 908,645 |
Initialization Function
Args:
epsilon (str): The epsilon symbol
alphabet (list): The DFA Alphabet
Returns:
None | def __init__(
self,
epsilon,
alphabet=None):
self.bookeeping = None
self.groups = None
self.epsilon = epsilon
if alphabet is None:
alphabet = createalphabet()
self.alphabet = alphabet | 908,766 |
Find state access strings (DFA shortest paths for every state)
using BFS
Args:
graph (DFA): The DFA states
start (int): The DFA initial state
Return:
list: A list of all the DFA shortest paths for every state | def _bfs_path_states(self, graph, start):
pathstates = {}
# maintain a queue of paths
queue = []
visited = []
# push the first path into the queue
queue.append([['', start]])
while queue:
# get the first path from the queue
path = ... | 908,767 |
Find the accepted states
Args:
graph (DFA): The DFA states
Return:
list: Returns the list of the accepted states | def _get_accepted(self, graph):
accepted = []
for state in graph.states:
if state.final != TropicalWeight(float('inf')):
accepted.append(state)
return accepted | 908,768 |
Send a reply message of the given type
Args:
- message: the message to publish
- message_type: the type of message being sent | def push(self,message,message_type):
super(Producer,self).send(message,message_type) | 908,803 |
Iter over a config and raise if a required option is still not set.
Args:
config (confpy.core.config.Configuration): The configuration object
to validate.
Raises:
MissingRequiredOption: If any required options are not set in the
configuration object.
Required optio... | def check_for_missing_options(config):
for section_name, section in config:
for option_name, option in section:
if option.required and option.value is None:
raise exc.MissingRequiredOption(
"Option {0} in namespace {1} is required.".format(
... | 909,354 |
maintain a map of states distance using BFS
Args:
start (fst state): The initial DFA state
Returns:
list: An ordered list of DFA states
using path distance | def _bfs_sort(self, start):
pathstates = {}
# maintain a queue of nodes to be visited. Both current and previous
# node must be included.
queue = []
# push the first path into the queue
queue.append([0, start])
pathstates[start.stateid] = 0
while ... | 909,487 |
Kleene star operation
Args:
input_string (str): The string that the kleene star will be made
Returns:
str: The applied Kleene star operation on the input string | def star(self, input_string):
if input_string != self.epsilon and input_string != self.empty:
return "(" + input_string + ")*"
else:
return "" | 909,488 |
# - Remove all the POP (type - 2) transitions to state 0,non DFA accepted
# for symbol @closing
# - Generate the accepted transitions
- Replace DFA accepted States with a push - pop symbol and two extra states
Args:
statediag (list): The states of the PDA
dfaaccep... | def get(self, statediag, dfaaccepted):
newstatediag = {}
newstate = PDAState()
newstate.id = 'AI,I' # BECAREFUL WHEN SIMPLIFYING...
newstate.type = 1
newstate.sym = '@wrapping'
transitions = {}
transitions[(0, 0)] = [0]
newstate.trans = transit... | 909,549 |
Performs BFS operation for eliminating useless loop transitions
Args:
graph (PDA): the PDA object
start (PDA state): The PDA initial state
Returns:
list: A cleaned, smaller list of DFA states | def bfs(self, graph, start):
newstatediag = {}
# maintain a queue of paths
queue = []
visited = []
# push the first path into the queue
queue.append(start)
while queue:
# get the first path from the queue
state = queue.pop(0)
... | 909,550 |
Replaces complex state IDs as generated from the product operation,
into simple sequencial numbers. A dictionaty is maintained in order
to map the existed IDs.
Args:
statediag (list): The states of the PDA
accepted (list): the list of DFA accepted states
Returns:
... | def get(self, statediag, accepted=None):
count = 0
statesmap = {}
newstatediag = {}
for state in statediag:
# Simplify state IDs
if statediag[state].id not in statesmap:
statesmap[statediag[state].id] = count
mapped = coun... | 909,552 |
Find the biggest State ID
Args:
statediag (list): The states of the PDA
thebiggestid (int): The binggest state identifier
Returns:
None | def __init__(self, statediag=[], thebiggestid=None):
self.statediag = []
self.quickresponse = {}
self.quickresponse_types = {}
self.toadd = []
self.biggestid = 0
if thebiggestid is None:
for state in statediag:
if statediag[state].id ... | 909,553 |
Creates a new POP state (type - 2) with the same transitions.
The POPed symbol is the unique number of the state.
Args:
trans (dict): Transition dictionary
Returns:
Int: The state identifier | def _generate_state(self, trans):
state = PDAState()
state.id = self.nextstate()
state.type = 2
state.sym = state.id
state.trans = trans.copy()
self.toadd.append(state)
return state.id | 909,554 |
For each state qi of the PDA, we add the rule Aii -> e
For each triplet of states qi, qj and qk, we add the rule Aij -> Aik Akj.
Args:
optimized (bool): Enable or Disable optimization - Do not produce O(n^3) | def insert_self_to_empty_and_insert_all_intemediate(self, optimized):
for state_a in self.statediag:
self.rules.append('A' +repr(state_a.id) +',' + repr(state_a.id) + ': @empty_set')
# If CFG is not requested, avoid the following O(n^3) rule.
# It can be solved and ... | 909,556 |
Generates a new random string from the start symbol
Args:
None
Returns:
str: The generated string | def generate(self):
result = self._gen(self.optimized, self.splitstring)
if self.splitstring and result is not None:
result = result[1:]
return result | 909,810 |
Because of the optimization, the rule for empty states is missing
A check takes place live
Args:
stateid (int): The state identifier
Returns:
bool: A true or false response | def _check_self_to_empty(self, stateid):
x_term = stateid.rfind('@')
y_term = stateid.rfind('A')
if y_term > x_term:
x_term = y_term
ids = stateid[x_term + 1:].split(',')
if len(ids) < 2:
return 0
if ids[0] == ids[1]:
# prin... | 909,812 |
Generates a new random object generated from the nonterminal
Args:
optimized (bool): mode of operation - if enabled not all
CNF rules are included (mitigate O(n^3))
splitstring (bool): A boolean for enabling or disabling
Returns:
str: The g... | def _gen(self, optimized, splitstring):
# Define Dictionary that holds resolved rules
# (only in form A -> terminals sequence)
self.resolved = {}
# First update Resolved dictionary by adding rules
# that contain only terminals (resolved rules)
for nt in self.gram... | 909,816 |
Currently this compiler simply returns an interpreter instead of compiling
TODO: Write this compiler to increase LPProg run speed and to prevent exceeding maximum recursion depth
Args:
prog (str): A string containing the program.
features (FeatureSet): The set of features to ena... | def compile(self, prog, features=Features.ALL):
return LPProg(Parser(Tokenizer(prog, features), features).program(), features) | 910,218 |
Convert any value into a string value.
Args:
value (any): The value to coerce.
Returns:
str: The string representation of the value. | def coerce(self, value):
if isinstance(value, compat.basestring):
return value
return str(value) | 910,511 |
Initialize the option with a regex pattern.
Args:
pattern (str): The regex pattern to match against.
*args: Any position arguments required by base classes.
**kwargs: Any keyword arguments required by base classes.
Raises:
ValueError: If a patter... | def __init__(self, pattern=None, *args, **kwargs):
super(PatternOption, self).__init__(*args, **kwargs)
if pattern is None:
raise ValueError("The pattern cannot be None.")
self._pattern = pattern
self._re = re.compile(pattern) | 910,512 |
Convert a value into a pattern matched string value.
All string values are matched against a regex before they are
considered acceptable values.
Args:
value (any): The value to coerce.
Raises:
ValueError: If the value is not an acceptable value.
... | def coerce(self, value):
if not isinstance(value, compat.basestring):
value = str(value)
if not self._re.match(value):
raise ValueError(
"The value {0} does not match the pattern {1}".format(
value,
se... | 910,513 |
Verify some json.
Args:
schema - the description of a general-case 'valid' json object.
data - the json data to verify.
Returns:
bool: True if data matches the schema, False otherwise.
Raises:
TypeError:
If the schema is of an unknown data type.
... | def check(schema, data, trace=False):
if trace == True:
trace = 1
else:
trace = None
return _check(schema, data, trace=trace) | 910,591 |
Generate s3 application bucket name.
Args:
include_region (bool): Include region in the name generation. | def s3_app_bucket(self, include_region=False):
if include_region:
s3_app_bucket = self.format['s3_app_region_bucket'].format(**self.data)
else:
s3_app_bucket = self.format['s3_app_bucket'].format(**self.data)
return s3_app_bucket | 911,131 |
Generate shared s3 application bucket name.
Args:
include_region (bool): Include region in the name generation. | def shared_s3_app_bucket(self, include_region=False):
if include_region:
shared_s3_app_bucket = self.format['shared_s3_app_region_bucket'].format(**self.data)
else:
shared_s3_app_bucket = self.format['shared_s3_app_bucket'].format(**self.data)
return shared_s3_ap... | 911,132 |
Initialization function
Args:
sid (int): The state identifier
Returns:
None | def __init__(self, sid=None):
self.final = False
self.initial = False
self.stateid = sid
self.arcs = [] | 911,407 |
The initialization function
Args:
srcstate_id (int): The source state identifier
nextstate_id (int): The destination state identifier
ilabel (str): The symbol corresponding to character for the transition | def __init__(self, srcstate_id, nextstate_id, ilabel=None):
self.srcstate = srcstate_id
self.nextstate = nextstate_id
self.ilabel = ilabel | 911,408 |
Sets a symbol
Args:
char (str): The symbol character
num (int): The symbol identifier
Returns:
None | def __setitem__(self, char, num):
self.symbols[num] = char
self.reversesymbols[char] = num | 911,409 |
Adds a new Arc
Args:
src (int): The source state identifier
dst (int): The destination state identifier
char (str): The character for the transition
Returns:
None | def add_arc(self, src, dst, char):
# assert type(src) == type(int()) and type(dst) == type(int()), \
# "State type should be integer."
# assert char in self.I
#
#print self.states
#print src
for s_idx in [src, dst]:
if s_idx >= len(self.st... | 911,413 |
Returns the complement of DFA
Args:
alphabet (list): The input alphabet
Returns:
None | def complement(self, alphabet):
states = sorted(self.states, key=attrgetter('initial'), reverse=True)
for state in states:
if state.final:
state.final = False
else:
state.final = True | 911,414 |
Adds a sink state
Args:
alphabet (list): The input alphabet
Returns:
None | def init_from_acceptor(self, acceptor):
self.states = copy.deepcopy(acceptor.states)
self.alphabet = copy.deepcopy(acceptor.alphabet)
self.osyms = copy.deepcopy(acceptor.osyms)
self.isyms = copy.deepcopy(acceptor.isyms) | 911,415 |
Save the transducer in the text file format of OpenFST.
The format is specified as follows:
arc format: src dest ilabel olabel [weight]
final state format: state [weight]
lines may occur in any order except initial state must be first line
Args:
txt_fst_file_n... | def load(self, txt_fst_file_name):
with open(txt_fst_file_name, 'r') as input_filename:
for line in input_filename:
line = line.strip()
split_line = line.split()
if len(split_line) == 1:
self[int(split_line[0])].final = Tru... | 911,416 |
Constructs an unminimized DFA recognizing
the intersection of the languages of two given DFAs.
Args:
other (DFA): The other DFA that will be used
for the intersect operation
Returns:
Returns:
DFA: The resulting DFA | def intersect(self, other):
operation = bool.__and__
self.cross_product(other, operation)
return self | 911,417 |
Constructs an unminimized DFA recognizing
the symmetric difference of the languages of two given DFAs.
Args:
other (DFA): The other DFA that will be used
for the symmetric difference operation
Returns:
DFA: The resulting DFA | def symmetric_difference(self, other):
operation = bool.__xor__
self.cross_product(other, operation)
return self | 911,418 |
Constructs an unminimized DFA recognizing the union of the languages of two given DFAs.
Args:
other (DFA): The other DFA that will be used
for the union operation
Returns:
DFA: The resulting DFA | def union(self, other):
operation = bool.__or__
self.cross_product(other, operation)
return self | 911,419 |
Transforms a Non Deterministic DFA into a Deterministic
Args:
None
Returns:
DFA: The resulting DFA
Creating an equivalent DFA is done using the standard algorithm.
A nice description can be found in the book:
Harry R. Lewis and Christos H. Papadimitriou. ... | def determinize(self):
# Compute the \epsilon-closure for all states and save it in a diagram
epsilon_closure = {}
for state in self.states:
sid = state.stateid
epsilon_closure[sid] = self._epsilon_closure(state)
# Get a transition diagram to speed up c... | 911,421 |
Performs the Hopcroft minimization algorithm
Args:
None
Returns:
DFA: The minimized input DFA | def hopcroft(self):
def _getset(testset, partition):
for part in partition:
if set(testset) == set(part):
return True
return None
def _create_transitions_representation(graph):
return {x.stateid:... | 911,423 |
A generalized cross-product constructor over two DFAs.
The third argument is a binary boolean function f; a state (q1, q2) in the final
DFA accepts if f(A[q1],A[q2]), where A indicates the acceptance-value of the state.
Args:
dfa_2: The second dfa
accept_method: The boole... | def cross_product(self, dfa_2, accept_method):
dfa_1states = copy.deepcopy(self.states)
dfa_2states = dfa_2.states
self.states = []
states = {}
def _create_transitions_representation(graph, state):
return {self.isyms.find(arc.ilabel): graph[arc... | 911,424 |
Copy files between diferent directories.
Copy one or more files to an existing directory. This function is
recursive, if the source is a directory, all its subdirectories are created
in the destination. Existing files in destination are overwrited without
any warning.
Args:
source (str): F... | def copy_rec(source, dest):
if os.path.isdir(source):
for child in os.listdir(source):
new_dest = os.path.join(dest, child)
os.makedirs(new_dest, exist_ok=True)
copy_rec(os.path.join(source, child), new_dest)
elif os.path.isfile(source):
logging.info(' ... | 911,439 |
Initialization function
Args:
sid (int): The state identifier
Returns:
None | def __init__(self, cur_fst, cur_node):
self.cur_node = cur_node
self.cur_fst = cur_fst | 911,646 |
Adds a new Arc
Args:
src (int): The source state identifier
dst (int): The destination state identifier
char (str): The character for the transition
Returns:
None | def add_arc(self, src, dst, char):
if src not in self.automaton.states():
self.add_state()
arc = fst.Arc(self.isyms[char], self.osyms[char], fst.Weight.One(self.automaton.weight_type()), dst)
self.automaton.add_arc(src, arc) | 911,651 |
After pyfst minimization,
all unused arcs are removed,
and all sink states are removed.
However this may break compatibility.
Args:
alphabet (list): The input alphabet
Returns:
None | def fixminimized(self, alphabet):
insymbols = fst.SymbolTable()
outsymbols = fst.SymbolTable()
num = 1
for char in self.alphabet:
self.isyms.__setitem__(char, num)
self.osyms.__setitem__(char, num)
insymbols.add_symbol(char, num)
... | 911,652 |
Returns the complement of DFA
Args:
alphabet (list): The input alphabet
Returns:
None | def complement(self, alphabet):
self._addsink(alphabet)
for state in self.automaton.states():
if self.automaton.final(state) == fst.Weight.One(self.automaton.weight_type()):
self.automaton.set_final(state, fst.Weight.Zero(self.automaton.weight_type()))
el... | 911,653 |
Adds a sink state
Args:
alphabet (list): The input alphabet
Returns:
None | def init_from_acceptor_bycopying(self, acceptor):
for state in acceptor.states:
for arc in state.arcs:
self.add_arc(state.stateid, arc.nextstate, acceptor.isyms.find(arc.ilabel))
if state.final:
print state.stateid,' is final'
self... | 911,654 |
Constructs an unminimized DFA recognizing
the intersection of the languages of two given DFAs.
Args:
other (DFA): The other DFA that will be used
for the intersect operation
Returns:
Returns:
DFA: The resulting DFA | def intersect(self, other):
self.automaton = fst.intersect(self.automaton, other.automaton)
return self | 911,655 |
Convert text values into boolean values.
True values are (case insensitive): 'yes', 'true', '1'. False values
are (case insensitive): 'no', 'false', '0'.
Args:
value (str or bool): The value to coerce.
Raises:
TypeError: If the value is not a bool or s... | def coerce(self, value):
if isinstance(value, bool):
return value
if not hasattr(value, 'lower'):
raise TypeError('Value is not bool or string.')
if value.lower() in ('yes', 'true', '1'):
return True
if value.lower() in ('n... | 911,721 |
Returns a string from the Diff resutl.
Depending on the method, either the string will
be generated directly from the PDA using the state
removal method, or the PDA will be first translated to
a CFG and then a string will be generated from the CFG
Args:
None
... | def get_string(self):
return_string = None
if not self.mmc:
return ""
method = 'PDASTRING'
if method == 'PDASTRING':
stringgen = PdaString()
print '* Reduce PDA using DFA BFS (remove unreachable states):'
newpda = self.mmc.s
... | 911,728 |
find an instance
Create a new instance and populate it with data stored if it exists.
Args:
binding_id (string): UUID of the binding
instance (AtlasServiceInstance.Instance): instance
Returns:
AtlasServiceBinding: A binding | def find(self, binding_id, instance):
binding = AtlasServiceBinding.Binding(binding_id, instance)
self.backend.storage.populate(binding)
return binding | 911,750 |
Create the binding
Args:
binding (AtlasServiceBinding.Binding): Existing or New binding
parameters (dict): Parameters for the binding
Returns:
Binding: Status
Raises:
ErrBindingAlreadyExists: If binding exists but... | def bind(self, binding, parameters):
if not binding.isProvisioned():
# Update binding parameters
binding.parameters = parameters
# Credentials
creds = self.backend.config.generate_binding_credentials(binding)
... | 911,751 |
Unbind the instance
Args:
binding (AtlasServiceBinding.Binding): Existing or New binding | def unbind(self, binding):
username = self.backend.config.generate_binding_username(binding)
try:
self.backend.atlas.DatabaseUsers.delete_a_database_user(username)
except ErrAtlasNotFound:
# The user does not exist. This is not an issue because ... | 911,752 |
Find
Args:
_id (str): instance id or binding Id
Keyword Arguments:
instance (AtlasServiceInstance.Instance): Existing instance
Returns:
AtlasServiceInstance.Instance or AtlasServiceBinding.Binding: An instance or binding. | def find(self, _id, instance = None):
if instance is None:
# We are looking for an instance
return self.service_instance.find(_id)
else:
# We are looking for a binding
return self.service_binding.find(_id, instance) | 911,862 |
Create an instance
Args:
instance (AtlasServiceInstance.Instance): Existing or New instance
parameters (dict): Parameters for the instance
Keyword Arguments:
existing (bool): True (use an existing cluster), False (create a new cluster)
... | def create(self, instance, parameters, existing=True):
return self.service_instance.create(instance, parameters, existing) | 911,863 |
Constructor.
Args:
version_string (str): The string that gave too many types.
first_matched_type (str): The name of the first detected type.
second_matched_type (str): The name of the second detected type | def __init__(self, version_string, first_matched_type, second_matched_type):
super(TooManyTypesError, self).__init__(
'Release "{}" cannot match types "{}" and "{}"'.format(
version_string, first_matched_type, second_matched_type
)
) | 911,958 |
Pad the left side of a bitarray with 0s to align its length with byte boundaries.
Args:
bits: A bitarray to be padded and aligned.
Returns:
A newly aligned bitarray. | def build_byte_align_buff(bits):
bitmod = len(bits)%8
if bitmod == 0:
rdiff = bitarray()
else:
#KEEP bitarray
rdiff = bitarray(8-bitmod)
rdiff.setall(False)
return rdiff+bits | 911,977 |
Initialize the option with an option type.
Args:
option (option.Option): The option which is used to validate all
list options.
Raises:
TypeError: If the given option is not an instance of option.Option.
TypeError: If the default value is set... | def __init__(self, option=None, default=None, *args, **kwargs):
super(ListOption, self).__init__(*args, **kwargs)
if not isinstance(option, opt.Option):
raise TypeError("Option must be an option type.")
self._option = option
self._default = default
... | 911,987 |
Initalize the Namespace with options
Args:
description (str, optional): A human readable description of what
the Namespace contains.
**options: Each keyword should be an Option object which will be
added to the Namespace.
Raises:
... | def __init__(self, description=None, **options):
self.__doc__ = description
self._options = {}
for name, option in compat.iteritems(options):
self.register(name, option)
super(Namespace, self).__init__() | 911,997 |
Fetch an option from the dictionary.
Args:
name (str): The name of the option.
default: The value to return if the name is missing.
Returns:
any: The value stored by the option.
This method resolves the option to its value rather than returning
... | def get(self, name, default=None):
option = self._options.get(name, None)
if option is None:
return default
return option.__get__(self) | 911,998 |
Set an option value.
Args:
name (str): The name of the option.
value: The value to set the option to.
Raises:
AttributeError: If the name is not registered.
TypeError: If the value is not a string or appropriate native type.
ValueErr... | def set(self, name, value):
if name not in self._options:
raise AttributeError("Option {0} does not exist.".format(name))
return self._options[name].__set__(self, value) | 911,999 |
Register a new option with the namespace.
Args:
name (str): The name to register the option under.
option (option.Option): The option object to register.
Raises:
TypeError: If the option is not an option.Option object.
ValueError: If the name is ... | def register(self, name, option):
if name in self._options:
raise ValueError("Option {0} already exists.".format(name))
if not isinstance(option, opt.Option):
raise TypeError("Options must be of type Option.")
self._options[name] = option | 912,000 |
Set an option value.
Args:
name (str): The name of the option.
value: The value to set the option to.
Raises:
TypeError: If the value is not a string or appropriate native type.
ValueError: If the value is a string but cannot be coerced.
... | def set(self, name, value):
if name not in self._options:
self.register(name, self._generator())
return self._options[name].__set__(self, value) | 912,003 |
Initialize general controller driver values with defaults.
Args:
dev (usb1.USBDevice) - Device entry the driver will control. | def __init__(self, dev):
self._dev = dev
self._dev_handle = None
self._scanchain = None
self._jtagon = False
self._speed = None | 912,135 |
Run a list of executable primitives on this controller, and distribute the returned data to the associated TDOPromises.
Args:
commands: A list of Executable Primitives to be run in order. | def _execute_primitives(self, commands):
for p in commands:
if self._scanchain and self._scanchain._debug:
print(" Executing", p)#pragma: no cover
p.execute(self) | 912,136 |
Add a promise to the promise collection at an optional offset.
Args:
promise: A TDOPromise to add to this collection.
bitoffset: An integer offset for this new promise in the collection.
_offsetideal: An integer offset for this new promise in the collection if the associated... | def add(self, promise, bitoffset, *, _offsetideal=None):
#This Assumes that things are added in order.
#Sorting or checking should likely be added.
if _offsetideal is None:
_offsetideal = bitoffset
if isinstance(promise, TDOPromise):
newpromise = promise.... | 912,162 |
Finds the shortest string using BFS
Args:
graph (DFA): The DFA states
start (DFA state): The DFA initial state
Returns:
str: The shortest string | def bfs(graph, start):
# maintain a queue of paths
queue = []
visited = []
# maintain a queue of nodes
# push the first path into the queue
queue.append([['', start]])
while queue:
# get the first path from the queue
path = queue.pop(0)
# get the last node from t... | 912,337 |
!DEMO!
Simple file parsing generator
Args:
filename: absolute or relative path to file on disk
encoding: encoding string that is passed to open function | def parse(filename, encoding=None):
with open(filename, encoding=encoding) as source:
for line in source:
for word in line.split():
yield word | 912,394 |
Add chain to current shelve file
Args:
name: chain name
order: markov chain order | def add_chain(self, name, order):
if name not in self.chains:
setattr(self.chains, name, MarkovChain(order=order))
else:
raise ValueError("Chain with this name already exists") | 912,403 |
Remove chain from current shelve file
Args:
name: chain name | def remove_chain(self, name):
if name in self.chains:
delattr(self.chains, name)
else:
raise ValueError("Chain with this name not found") | 912,404 |
Build markov chain from source on top of existin chain
Args:
source: iterable which will be used to build chain
chain: MarkovChain in currently loaded shelve file that
will be extended by source | def build_chain(self, source, chain):
for group in WalkByGroup(source, chain.order+1):
pre = group[:-1]
res = group[-1]
if pre not in chain.content:
chain.content[pre] = {res: 1}
else:
if res not in chain.content[pre]:
... | 912,405 |
!DEMO!
Demo function that shows how to generate a simple sentence starting with
uppercase letter without lenght limit.
Args:
chain: MarkovChain that will be used to generate sentence | def generate_sentence(self, chain):
def weighted_choice(choices):
total_weight = sum(weight for val, weight in choices)
rand = random.uniform(0, total_weight)
upto = 0
for val, weight in choices:
if upto + weight >= rand:
... | 912,406 |
Read a value from the configuration, with a default.
Args:
section_name (str): name of the section in the configuration from which
the option should be found.
option (str): name of the configuration option.
default_option (str): name of the default configurat... | def get_config_value(self, section_name, option, default_option="default"):
if self.config is None:
self.config = configparser.ConfigParser()
self.config.read(self.ini_file_name)
if option:
try:
return self.config.get(section_name, option)
... | 912,586 |
Consumes an input and validates if it is accepted
Args:
mystr (str): the input string to be consumes
stack (list): the stack of symbols
state (int): the current state of the PDA
curchar (int): the index of the consumed character
depth (int): the depth ... | def consume_input(self, mystr, stack=[], state=1, curchar=0, depth=0):
mystrsplit = mystr.split(' ')
if self.s[state].type == 1:
stack.append(self.s[state].sym)
if len(self.s[state].trans) > 0:
state = self.s[state].trans[0]
if self.parse(... | 912,830 |
After pyfst minimization,
all unused arcs are removed,
and all sink states are removed.
However this may break compatibility.
Args:
alphabet (list): The input alphabet
Returns:
None | def fixminimized(self, alphabet):
endstate = len(list(self.states))
for state in self.states:
for char in alphabet:
found = 0
for arc in state.arcs:
if self.isyms.find(arc.ilabel) == char:
found = 1
... | 912,943 |
Convert a path to the string representing the path
Args:
path (tuple): A tuple of arcs
Returns:
inp (str): The path concatenated as as string | def _path_to_str(self, path):
inp = ''
for arc in path:
i = self.isyms.find(arc.ilabel)
# Ignore \epsilon transitions both on input
if i != fst.EPSILON:
inp += i
return inp | 912,944 |
Adds a sink state
Args:
alphabet (list): The input alphabet
Returns:
None | def init_from_acceptor(self, acceptor):
states = sorted(
acceptor.states,
key=attrgetter('initial'),
reverse=True)
for state in states:
for arc in state.arcs:
itext = acceptor.isyms.find(arc.ilabel)
if itext in self... | 912,945 |
Return True/False if the machine accepts/reject the input.
Args:
inp (str): input string to be consumed
Returns:
bool: A true or false value depending on if the DFA
accepts the provided input | def consume_input(self, inp):
cur_state = sorted(
self.states,
key=attrgetter('initial'),
reverse=True)[0]
while len(inp) > 0:
found = False
for arc in cur_state.arcs:
if self.isyms.find(arc.ilabel) == inp[0]:
... | 912,946 |
Generate string_length random strings that belong to the automaton.
Args:
string_length (integer): The size of the random string
Returns:
str: The generated string | def random_strings(self, string_length=1):
str_list = []
for path in self.uniform_generate(string_length):
str_list.append(self._path_to_str(path))
return str_list | 912,947 |
Save the machine in the openFST format in the file denoted by
txt_fst_filename.
Args:
txt_fst_filename (str): The name of the file
Returns:
None | def save(self, txt_fst_filename):
txt_fst = open(txt_fst_filename, 'w+')
states = sorted(self.states, key=attrgetter('initial'), reverse=True)
for state in states:
for arc in state.arcs:
itext = self.isyms.find(arc.ilabel)
otext = self.osyms.f... | 912,948 |
Save the transducer in the text file format of OpenFST.
The format is specified as follows:
arc format: src dest ilabel olabel [weight]
final state format: state [weight]
lines may occur in any order except initial state must be first line
Args:
txt_fst_filena... | def load(self, txt_fst_filename):
with open(txt_fst_filename, 'r') as txt_fst:
for line in txt_fst:
line = line.strip()
splitted_line = line.split()
if len(splitted_line) == 1:
self[int(splitted_line[0])].final = True
... | 912,949 |
Initialization function for Arc's guardgen structure
Args:
src_state_id (int): The source state identifier
dst_state_id (int): The destination state identifier
guard_p: The input character
term: The input term
Returns:
None | def __init__(self, src_state_id, dst_state_id, guard_p, term=None):
self.src_state = src_state_id
self.dst_state = dst_state_id
self.guard = guard_p
self.term = None | 912,955 |
Initialization of the SFA oject
Args:
alphabet (list): The input alphabet
Returns:
None | def __init__(self, alphabet=None):
self.states = []
self.arcs = []
self.alphabet = alphabet | 912,956 |
This function adds a new arc in a SFA state
Args:
src (int): The source state identifier
dst (int): The destination state identifier
char (str): The transition symbol
Returns:
None | def add_arc(self, src, dst, char):
assert type(src) == type(int()) and type(dst) == type(int()), \
"State type should be integer."
while src >= len(self.states) or dst >= len(self.states):
self.add_state()
self.states[src].arcs.append(SFAArc(src, dst, char)) | 912,958 |
Return True/False if the machine accepts/reject the input.
Args:
inp (str): input string to be consumed
Retunrs:
bool: A true or false value depending on if the DFA
accepts the provided input | def consume_input(self, inp):
cur_state = self.states[0]
for character in inp:
found = False
for arc in cur_state.arcs:
if arc.guard.is_sat(character):
cur_state = self.states[arc.dst_state]
found = True
... | 912,959 |
Transforms the SFA into a DFA
Args:
None
Returns:
DFA: The generated DFA | def concretize(self):
dfa = DFA(self.alphabet)
for state in self.states:
for arc in state.arcs:
for char in arc.guard:
dfa.add_arc(arc.src_state, arc.dst_state, char)
for i in xrange(len(self.states)):
if self.states[i].final:... | 912,960 |
Read DFA transitions from flex compiled file
Args:
None
Returns:
list: The list of states and the destination for a character | def _read_transitions(self):
states = []
i = 0
regex = re.compile('[ \t\n\r:,]+')
found = 0 # For maintaining the state of yy_nxt declaration
state = 0 # For maintaining the state of opening and closing tag of yy_nxt
substate = 0 # For maintaining the state of... | 913,101 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.