code string | signature string | docstring string | loss_without_docstring float64 | loss_with_docstring float64 | factor float64 |
|---|---|---|---|---|---|
with open('src/vegas/__init__.py', 'a') as vfile:
vfile.write("\n__version__ = '%s'\n" % VEGAS_VERSION)
_build_py.run(self) | def run(self) | Append version number to vegas/__init__.py | 6.11985 | 3.347983 | 1.827921 |
ny = 2000
y = numpy.random.uniform(0., 1., (ny,1))
limit = numpy.arctan(limit)
m = AdaptiveMap([[-limit, limit]], ninc=100)
theta = numpy.empty(y.shape, float)
jac = numpy.empty(y.shape[0], float)
for itn in range(10):
m.map(y, theta, jac)
... | def _make_map(self, limit) | Make vegas grid that is adapted to the pdf. | 5.929698 | 5.65065 | 1.049383 |
def ff(theta, nopdf=nopdf):
tan_theta = numpy.tan(theta)
x = self.scale * tan_theta
jac = self.scale * (tan_theta ** 2 + 1.)
if nopdf:
pdf = jac * self.pdf.pjac[None, :]
else:
pdf = jac * numpy.exp(-(x ** 2) / 2... | def _expval(self, f, nopdf) | Return integrand using the tan mapping. | 3.317309 | 3.321979 | 0.998594 |
# Ensure we open the file in binary mode
try:
with open(filename, 'rb') as f:
chunk = f.read(length)
return chunk
except IOError as e:
print(e) | def get_starting_chunk(filename, length=1024) | :param filename: File to open and get the first little chunk of.
:param length: Number of bytes to read, default 1024.
:returns: Starting chunk of bytes. | 3.000275 | 3.195936 | 0.938778 |
assert isinstance(function_name, str)
self.stdout.write(function_name)
self.stdout.write("\n")
self.stdout.write("-" * len(function_name))
self.stdout.write("\n\n")
byte_code = self.interpreter.compiled_functions[function_name]
self.stdout.write(byte_co... | def dump(self, function_name) | Pretty-dump the bytecode for the function with the given name. | 2.863655 | 2.511884 | 1.140043 |
template = os.path.dirname(os.path.abspath(__file__)) + "/app_template"
name = options.pop("name")
call_command("startapp", name, template=template, **options) | def handle(self, **options) | Call "startapp" to generate app with custom user model. | 5.069888 | 3.480939 | 1.456471 |
if not email:
raise ValueError("The given email must be set")
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user | def _create_user(self, email, password, **extra_fields) | Create and save a User with the given email and password. | 1.520572 | 1.397515 | 1.088054 |
iters = itertools.tee(iterable, n)
iters = (itertools.islice(it, i, None) for i, it in enumerate(iters))
return itertools.izip(*iters) | def nwise(iterable, n) | Iterate through a sequence with a defined length window
>>> list(nwise(range(8), 3))
[(0, 1, 2), (1, 2, 3), (2, 3, 4), (3, 4, 5), (5, 6, 7)]
>>> list(nwise(range(3), 5))
[]
Parameters
----------
iterable
n : length of each sequence
Yields
------
Tuples of length n | 2.416039 | 3.361118 | 0.71882 |
c = [g.node[n]['coordinate'] for n in nodes]
return c | def coordinates(g, nodes) | Extract (lon, lat) coordinate pairs from nodes in an osmgraph
osmgraph nodes have a 'coordinate' property on each node. This is
a shortcut for extracting a coordinate list from an iterable of nodes
>>> g = osmgraph.parse_file(filename)
>>> node_ids = g.nodes()[:3] # Grab 3 nodes
[61341696, 613416... | 4.376397 | 9.945977 | 0.440017 |
forw = g.successors
back = g.predecessors
if backward:
back, forw = forw, back
nodes = forw(n2)
if inbound:
nodes = set(nodes + back(n2))
candidates = [n for n in nodes if n != n1]
if len(candidates) == 1:
result = candidates[0]
elif continue_fn:
re... | def step(g, n1, n2, inbound=False, backward=False, continue_fn=None) | Step along a path through a directed graph unless there is an intersection
Example graph:
Note that edge (1, 2) and (2, 3) are bidirectional, i.e., (2, 1) and
(3, 2) are also edges
1 -- 2 -- 3 -->-- 5 -->-- 7
| |
^ v
| |
4 ... | 3.367884 | 3.607502 | 0.933578 |
prev = n1
curr = n2
_next = step(g, prev, curr, **kwargs)
yield prev
yield curr
visited_nodes = set([prev, curr])
while _next:
yield _next
if _next in visited_nodes:
return
visited_nodes.add(_next)
prev = curr
curr = _next
_n... | def move(g, n1, n2, **kwargs) | Step along a graph until it ends or reach an intersection
Example graph:
Note that edge (1, 2) and (2, 3) are bidirectional, i.e., (2, 1) and
(3, 2) are also edges
1 -- 2 -- 3 -->-- 5 -->-- 7
| |
^ v
| |
4 6
>>> list(... | 2.753869 | 3.160662 | 0.871295 |
return len(set(g.predecessors(n) + g.successors(n))) > 2 | def is_intersection(g, n) | Determine if a node is an intersection
graph: 1 -->-- 2 -->-- 3
>>> is_intersection(g, 2)
False
graph:
1 -- 2 -- 3
|
4
>>> is_intersection(g, 2)
True
Parameters
----------
g : networkx DiGraph
n : node id
Returns
-------
bool | 4.325426 | 6.022377 | 0.718226 |
if not self._is_valid:
self.validate()
from .converters import to_dict
return to_dict(self) | def as_dict(self) | Returns the model as a dict | 7.803244 | 6.012196 | 1.297902 |
for node_id, lon, lat in data:
self.coords[node_id] = (lon, lat) | def coords_callback(self, data) | Callback for nodes that have no tags | 4.968142 | 4.204343 | 1.181669 |
for node_id, tags, coords in data:
# Discard the coords because they go into add_coords
self.nodes[node_id] = tags | def nodes_callback(self, data) | Callback for nodes with tags | 15.450578 | 14.013352 | 1.102561 |
for way_id, tags, nodes in data:
# Imposm passes all ways through regardless of whether the tags
# have been filtered or not. It needs to do this in order to
# handle relations, but we don't care about relations at the
# moment.
if tags:
... | def ways_callback(self, data) | Callback for all ways | 7.745967 | 7.754332 | 0.998921 |
g = nx.DiGraph()
for way_id, (tags, nodes) in self.ways.items():
# If oneway is '-1', reverse the way and treat as a normal oneway
if tags.get('oneway') == '-1':
nodes = reversed(nodes)
tags['oneway'] = 'yes'
oneway = tags.get... | def get_graph(self, parse_direction=False) | Return the networkx directed graph of received data | 2.563941 | 2.605727 | 0.983964 |
importer, parser = make_importer_parser(OSMParser, **kwargs)
parser.parse(filename)
return importer.get_graph(parse_direction=parse_direction) | def parse_file(filename, parse_direction=False, **kwargs) | Return an OSM networkx graph from the input OSM file
Only works with OSM xml, xml.bz2 and pbf files. This function cannot take
OSM QA tile files. Use parse_qa_tile() for QA tiles.
>>> graph = parse_file(filename) | 6.901945 | 8.243294 | 0.83728 |
suffixes = {
'xml': '.osm',
'pbf': '.pbf',
}
try:
suffix = suffixes[type]
except KeyError:
raise ValueError('Unknown data type "%s"' % type)
fd, filename = tempfile.mkstemp(suffix=suffix)
try:
os.write(fd, data)
os.close(fd)
return pa... | def parse_data(data, type, **kwargs) | Return an OSM networkx graph from the input OSM data
Parameters
----------
data : string
type : string ('xml' or 'pbf')
>>> graph = parse_data(data, 'xml') | 2.339536 | 2.201402 | 1.062748 |
import osmqa
importer, parser = make_importer_parser(osmqa.QATileParser, **kwargs)
parser.parse_data(x, y, zoom, data)
return importer.get_graph(parse_direction=parse_direction) | def parse_qa_tile(x, y, zoom, data, parse_direction=False, **kwargs) | Return an OSM networkx graph from the input OSM QA tile data
Parameters
----------
data : string
x : int
tile's x coordinate
y : int
tile's y coordinate
zoom : int
tile's zoom level
>>> graph = parse_qa_tile(data, 1239, 1514, 12) | 5.438076 | 7.074016 | 0.76874 |
if not isinstance(fname, Path):
fname = Path(fname)
path, name, ext = fname.parent, fname.stem, fname.suffix
return path, name, ext | def _basename(fname) | Return file name without path. | 2.838567 | 2.794134 | 1.015902 |
df = pd.read_csv(
fname,
skiprows=2,
parse_dates=[1],
index_col=0,
names=["bottle_number", "time", "startscan", "endscan"],
)
df._metadata = {
"time_of_reset": pd.to_datetime(
linecache.getline(str(fname), 2)[6:-1]
).to_pydatetime()
... | def from_bl(fname) | Read Seabird bottle-trip (bl) file
Example
-------
>>> from pathlib import Path
>>> import ctd
>>> data_path = Path(__file__).parents[1].joinpath("tests", "data")
>>> df = ctd.from_bl(str(data_path.joinpath('bl', 'bottletest.bl')))
>>> df._metadata["time_of_reset"]
datetime.datetime(201... | 4.642642 | 4.147286 | 1.119441 |
f = _read_file(fname)
metadata = _parse_seabird(f.readlines(), ftype="btl")
f.seek(0)
df = pd.read_fwf(
f,
header=None,
index_col=False,
names=metadata["names"],
parse_dates=False,
skiprows=metadata["skiprows"],
)
f.close()
# At this po... | def from_btl(fname) | DataFrame constructor to open Seabird CTD BTL-ASCII format.
Examples
--------
>>> from pathlib import Path
>>> import ctd
>>> data_path = Path(__file__).parents[1].joinpath("tests", "data")
>>> bottles = ctd.from_btl(data_path.joinpath('btl', 'bottletest.btl')) | 3.93871 | 4.164494 | 0.945784 |
f = _read_file(fname)
header, names = [], []
for k, line in enumerate(f.readlines()):
line = line.strip()
if line.startswith("Serial Number"):
serial = line.strip().split(":")[1].strip()
elif line.startswith("Latitude"):
try:
hemisphere = ... | def from_edf(fname) | DataFrame constructor to open XBT EDF ASCII format.
Examples
--------
>>> from pathlib import Path
>>> import ctd
>>> data_path = Path(__file__).parents[1].joinpath("tests", "data")
>>> cast = ctd.from_edf(data_path.joinpath('XBT.EDF.gz'))
>>> ax = cast['temperature'].plot_cast() | 2.177611 | 2.329546 | 0.934779 |
f = _read_file(fname)
metadata = _parse_seabird(f.readlines(), ftype="cnv")
f.seek(0)
df = pd.read_fwf(
f,
header=None,
index_col=None,
names=metadata["names"],
skiprows=metadata["skiprows"],
delim_whitespace=True,
widths=[11] * len(metadata[... | def from_cnv(fname) | DataFrame constructor to open Seabird CTD CNV-ASCII format.
Examples
--------
>>> from pathlib import Path
>>> import ctd
>>> data_path = Path(__file__).parents[1].joinpath("tests", "data")
>>> cast = ctd.from_cnv(data_path.joinpath('CTD_big.cnv.bz2'))
>>> downcast, upcast = cast.split()
... | 3.574754 | 4.069109 | 0.87851 |
f = _read_file(fname)
df = pd.read_csv(
f,
header="infer",
index_col=None,
skiprows=skiprows,
dtype=float,
delim_whitespace=True,
)
f.close()
df.set_index("PRES", drop=True, inplace=True)
df.index.name = "Pressure [dbar]"
metadata = {"nam... | def from_fsi(fname, skiprows=9) | DataFrame constructor to open Falmouth Scientific, Inc. (FSI) CTD
ASCII format.
Examples
--------
>>> from pathlib import Path
>>> import ctd
>>> data_path = Path(__file__).parents[1].joinpath("tests", "data")
>>> cast = ctd.from_fsi(data_path.joinpath('FSI.txt.gz'))
>>> downcast, upcas... | 3.235551 | 4.535585 | 0.71337 |
ros = from_cnv(fname)
ros["pressure"] = ros.index.values.astype(float)
ros["nbf"] = ros["nbf"].astype(int)
ros.set_index("nbf", drop=True, inplace=True, verify_integrity=False)
return ros | def rosette_summary(fname) | Make a BTL (bottle) file from a ROS (bottle log) file.
More control for the averaging process and at which step we want to
perform this averaging eliminating the need to read the data into SBE
Software again after pre-processing.
NOTE: Do not run LoopEdit on the upcast!
Examples
--------
>... | 5.037399 | 5.425026 | 0.928548 |
xs, ys = interpolator.x, interpolator.y
def pointwise(x):
if x < xs[0]:
return ys[0] + (x - xs[0]) * (ys[1] - ys[0]) / (xs[1] - xs[0])
elif x > xs[-1]:
return ys[-1] + (x - xs[-1]) * (ys[-1] - ys[-2]) / (
xs[-1] - xs[-2]
)
else:
... | def _extrap1d(interpolator) | http://stackoverflow.com/questions/2745329/
How to make scipy.interpolate return an extrapolated result beyond the
input range. | 1.744368 | 1.706508 | 1.022186 |
from scipy.interpolate import interp1d
new_data1 = []
for row in data:
mask = ~np.isnan(row)
if mask.any():
y = row[mask]
if y.size == 1:
row = np.repeat(y, len(mask))
else:
x = dist[mask]
f_i = interp1... | def extrap_sec(data, dist, depth, w1=1.0, w2=0) | Extrapolates `data` to zones where the shallow stations are shadowed by
the deep stations. The shadow region usually cannot be extrapolates via
linear interpolation.
The extrapolation is applied using the gradients of the `data` at a certain
level.
Parameters
----------
data : array_like
... | 1.877222 | 2.049606 | 0.915894 |
import gsw
from scipy.interpolate import interp1d
h, lon, lat = list(map(np.asanyarray, (h, lon, lat)))
# Distance in km.
x = np.append(0, np.cumsum(gsw.distance(lon, lat)[0] / 1e3))
h = -gsw.z_from_p(h, lat.mean())
Ih = interp1d(x, h, kind=kind, bounds_error=False, fill_value=h[-1])
... | def gen_topomask(h, lon, lat, dx=1.0, kind="linear", plot=False) | Generates a topography mask from an oceanographic transect taking the
deepest CTD scan as the depth of each station.
Inputs
------
h : array
Pressure of the deepest CTD scan for each station [dbar].
lons : array
Longitude of each station [decimal degrees east].
lat : Latitude... | 3.17381 | 3.131004 | 1.013672 |
alpha = 0.03 # Thermal anomaly amplitude.
beta = 1.0 / 7 # Thermal anomaly time constant (1/beta).
sample_interval = 1 / 15.0
a = 2 * alpha / (sample_interval * beta + 2)
b = 1 - (2 * a / alpha)
dCodT = 0.1 * (1 + 0.006 * [temperature - 20])
dT = np.diff(temperature)
ctm = -1.0 ... | def cell_thermal_mass(temperature, conductivity) | Sample interval is measured in seconds.
Temperature in degrees.
CTM is calculated in S/m. | 7.758798 | 6.516246 | 1.190685 |
import gsw
sigma_theta = gsw.sigma0(SA, CT)
mask = mixed_layer_depth(CT)
mld = np.where(mask)[0][-1]
sig_surface = sigma_theta[0]
sig_bottom_mld = gsw.sigma0(SA[0], CT[mld])
d_sig_t = sig_surface - sig_bottom_mld
d_sig = sigma_theta - sig_bottom_mld
mask = d_sig < d_sig_t # Ba... | def barrier_layer_thickness(SA, CT) | Compute the thickness of water separating the mixed surface layer from the
thermocline. A more precise definition would be the difference between
mixed layer depth (MLD) calculated from temperature minus the mixed layer
depth calculated using density. | 5.593716 | 5.134703 | 1.089394 |
ax = kwargs.pop("ax", None)
fignums = plt.get_fignums()
if ax is None and not fignums:
ax = plt.axes()
fig = ax.get_figure()
fig.set_size_inches((5.25, 6.75))
else:
ax = plt.gca()
fig = plt.gcf()
figsize = kwargs.pop("figsize", fig.get_size_inches())
... | def plot_cast(df, secondary_y=False, label=None, *args, **kwargs) | Plot a CTD variable with the index in the y-axis instead of x-axis. | 2.406472 | 2.337776 | 1.029385 |
shape = data.shape[:-1] + (data.shape[-1] - block + 1, block)
strides = data.strides + (data.strides[-1],)
return np.lib.stride_tricks.as_strided(data, shape=shape, strides=strides) | def _rolling_window(data, block) | http://stackoverflow.com/questions/4936620/
Using strides for an efficient moving average filter. | 1.542286 | 1.483514 | 1.039617 |
idx = df.index.argmax() + 1
down = df.iloc[:idx]
# Reverse index to orient it as a CTD cast.
up = df.iloc[idx:][::-1]
return down, up | def split(df) | Returns a tuple with down/up-cast. | 9.936947 | 7.339111 | 1.353971 |
from scipy import signal
# Butter is closer to what SBE is doing with their cosine filter.
Wn = (1.0 / time_constant) / (sample_rate * 2.0)
b, a = signal.butter(2, Wn, "low")
new_df = df.copy()
new_df.index = signal.filtfilt(b, a, df.index.values)
return new_df | def lp_filter(df, sample_rate=24.0, time_constant=0.15) | Filter a series with `time_constant` (use 0.15 s for pressure), and for
a signal of `sample_rate` in Hertz (24 Hz for 911+).
NOTE: 911+ systems do not require filter for temperature nor salinity.
Examples
--------
>>> from pathlib import Path
>>> import matplotlib.pyplot as plt
>>> import c... | 4.508939 | 4.812274 | 0.936966 |
new_df = df.copy()
press = new_df.copy().index.values
ref = press[0]
inversions = np.diff(np.r_[press, press[-1]]) < 0
mask = np.zeros_like(inversions)
for k, p in enumerate(inversions):
if p:
ref = press[k]
cut = press[k + 1 :] < ref
mask[k + 1 ... | def press_check(df) | Remove pressure reversals from the index. | 4.046026 | 3.726924 | 1.085621 |
start = np.floor(df.index[0])
stop = np.ceil(df.index[-1])
new_index = np.arange(start, stop, delta)
binned = pd.cut(df.index, bins=new_index)
if method == "average":
new_df = df.groupby(binned).mean()
new_df.index = new_index[:-1]
elif method == "interpolate":
raise... | def bindata(df, delta=1.0, method="average") | Bin average the index (usually pressure) to a given interval (default
delta = 1). | 2.70209 | 2.554425 | 1.057807 |
data = series.values.astype(float).copy()
roll = _rolling_window(data, block)
roll = ma.masked_invalid(roll)
std = n1 * roll.std(axis=1)
mean = roll.mean(axis=1)
# Use the last value to fill-up.
std = np.r_[std, np.tile(std[-1], block - 1)]
mean = np.r_[mean, np.tile(mean[-1], bloc... | def _despike(series, n1, n2, block, keep) | Wild Edit Seabird-like function. Passes with Standard deviation
`n1` and `n2` with window size `block`. | 2.488353 | 2.416451 | 1.029755 |
if isinstance(df, pd.Series):
new_df = _despike(df, n1=n1, n2=n2, block=block, keep=keep)
else:
new_df = df.apply(_despike, n1=n1, n2=n2, block=block, keep=keep)
return new_df | def despike(df, n1=2, n2=20, block=100, keep=0) | Wild Edit Seabird-like function. Passes with Standard deviation
`n1` and `n2` with window size `block`. | 1.641966 | 1.762097 | 0.931825 |
windows = {
"flat": np.ones,
"hanning": np.hanning,
"hamming": np.hamming,
"bartlett": np.bartlett,
"blackman": np.blackman,
}
data = series.values.copy()
if window_len < 3:
return pd.Series(data, index=series.index, name=series.name)
if window... | def _smooth(series, window_len, window) | Smooth the data using a window with requested size. | 1.794884 | 1.725495 | 1.040214 |
if isinstance(df, pd.Series):
new_df = _smooth(df, window_len=window_len, window=window)
else:
new_df = df.apply(_smooth, window_len=window_len, window=window)
return new_df | def smooth(df, window_len=11, window="hanning") | Smooth the data using a window with requested size. | 1.977433 | 1.997176 | 0.990114 |
if isinstance(body, str):
if self.encoding is None:
raise TypeError("Unable to encode string with no encoding set")
body = body.encode(self.encoding)
cmd = b'put %d %d %d %d\r\n%b' % (priority, delay, ttr, len(body), body)
return self._int_cmd(cmd... | def put(self,
body: Body,
priority: int = DEFAULT_PRIORITY,
delay: int = DEFAULT_DELAY,
ttr: int = DEFAULT_TTR) -> int | Inserts a job into the currently used tube and returns the job ID.
:param body: The data representing the job.
:param priority: An integer between 0 and 4,294,967,295 where 0 is the
most urgent.
:param delay: The number of seconds to delay the job for.
:param tt... | 3.815668 | 3.739923 | 1.020253 |
self._send_cmd(b'use %b' % tube.encode('ascii'), b'USING') | def use(self, tube: str) -> None | Changes the currently used tube.
:param tube: The tube to use. | 10.389589 | 12.353234 | 0.841042 |
if timeout is None:
cmd = b'reserve'
else:
cmd = b'reserve-with-timeout %d' % timeout
return self._job_cmd(cmd, b'RESERVED') | def reserve(self, timeout: Optional[int] = None) -> Job | Reserves a job from a tube on the watch list, giving this client
exclusive access to it for the TTR. Returns the reserved job.
This blocks until a job is reserved unless a ``timeout`` is given,
which will raise a :class:`TimedOutError <greenstalk.TimedOutError>` if
a job cannot be reser... | 4.663896 | 6.042356 | 0.771867 |
self._send_cmd(b'delete %d' % _to_id(job), b'DELETED') | def delete(self, job: JobOrID) -> None | Deletes a job.
:param job: The job or job ID to delete. | 12.048194 | 12.858903 | 0.936953 |
self._send_cmd(b'release %d %d %d' % (job.id, priority, delay), b'RELEASED') | def release(self,
job: Job,
priority: int = DEFAULT_PRIORITY,
delay: int = DEFAULT_DELAY) -> None | Releases a reserved job.
:param job: The job to release.
:param priority: An integer between 0 and 4,294,967,295 where 0 is the
most urgent.
:param delay: The number of seconds to delay the job for. | 7.144092 | 6.662315 | 1.072314 |
self._send_cmd(b'bury %d %d' % (job.id, priority), b'BURIED') | def bury(self, job: Job, priority: int = DEFAULT_PRIORITY) -> None | Buries a reserved job.
:param job: The job to bury.
:param priority: An integer between 0 and 4,294,967,295 where 0 is the
most urgent. | 7.545692 | 7.492363 | 1.007118 |
self._send_cmd(b'touch %d' % job.id, b'TOUCHED') | def touch(self, job: Job) -> None | Refreshes the TTR of a reserved job.
:param job: The job to touch. | 12.248182 | 12.276585 | 0.997686 |
return self._int_cmd(b'watch %b' % tube.encode('ascii'), b'WATCHING') | def watch(self, tube: str) -> int | Adds a tube to the watch list. Returns the number of tubes this
client is watching.
:param tube: The tube to watch. | 9.334963 | 12.727768 | 0.733433 |
return self._int_cmd(b'ignore %b' % tube.encode('ascii'), b'WATCHING') | def ignore(self, tube: str) -> int | Removes a tube from the watch list. Returns the number of tubes this
client is watching.
:param tube: The tube to ignore. | 13.673149 | 14.878831 | 0.918967 |
return self._int_cmd(b'kick %d' % bound, b'KICKED') | def kick(self, bound: int) -> int | Moves delayed and buried jobs into the ready queue and returns the
number of jobs effected.
Only jobs from the currently used tube are moved.
A kick will only move jobs in a single state. If there are any buried
jobs, only those will be moved. Otherwise delayed jobs will be moved.
... | 12.083825 | 16.610004 | 0.727503 |
self._send_cmd(b'kick-job %d' % _to_id(job), b'KICKED') | def kick_job(self, job: JobOrID) -> None | Moves a delayed or buried job into the ready queue.
:param job: The job or job ID to kick. | 9.900994 | 10.328084 | 0.958648 |
return self._stats_cmd(b'stats-job %d' % _to_id(job)) | def stats_job(self, job: JobOrID) -> Stats | Returns job statistics.
:param job: The job or job ID to return statistics for. | 10.427576 | 14.711091 | 0.708824 |
return self._stats_cmd(b'stats-tube %b' % tube.encode('ascii')) | def stats_tube(self, tube: str) -> Stats | Returns tube statistics.
:param tube: The tube to return statistics for. | 8.995075 | 12.218058 | 0.736212 |
self._send_cmd(b'pause-tube %b %d' % (tube.encode('ascii'), delay), b'PAUSED') | def pause_tube(self, tube: str, delay: int) -> None | Prevents jobs from being reserved from a tube for a period of time.
:param tube: The tube to pause.
:param delay: The number of seconds to pause the tube for. | 5.831149 | 5.750381 | 1.014046 |
if conf:
if not os.path.isfile(conf):
raise click.exceptions.BadParameter("{} is not a file".format(conf))
try:
config.conf.load_config(config_path=conf)
except exceptions.ConfigurationException as e:
raise click.exceptions.BadParameter(str(e))
tw... | def cli(conf) | The fedora-messaging command line interface. | 4.167215 | 4.215594 | 0.988524 |
# The configuration validates these are not null and contain all required keys
# when it is loaded.
bindings = config.conf["bindings"]
queues = config.conf["queues"]
# The CLI and config.DEFAULTS have different defaults for the queue
# settings at the moment. We should select a universal... | def consume(exchange, queue_name, routing_key, callback, app_name) | Consume messages from an AMQP queue using a Python callback. | 3.727818 | 3.698035 | 1.008054 |
global _exit_code
if failure.check(exceptions.BadDeclaration):
_log.error(
"Unable to declare the %s object on the AMQP broker. The "
"broker responded with %s. Check permissions for your user.",
failure.value.obj_type,
failure.value.reason,
)... | def _consume_errback(failure) | Handle any errors that occur during consumer registration. | 3.904442 | 3.794833 | 1.028884 |
for consumer in consumers:
def errback(failure):
global _exit_code
if failure.check(exceptions.HaltConsumer):
_exit_code = failure.value.exit_code
if _exit_code:
_log.error(
"Consumer halted with non-ze... | def _consume_callback(consumers) | Callback when consumers are successfully registered.
This simply registers callbacks for consumer.result deferred object which
fires when the consumer stops.
Args
consumers (list of fedora_messaging.api.Consumer):
The list of consumers that were successfully created. | 3.605334 | 3.501864 | 1.029547 |
try:
deferred.addTimeout(timeout, reactor)
except AttributeError:
# Twisted 12.2 (in EL7) does not have the addTimeout API, so make do with
# the slightly more annoying approach of scheduling a call to cancel which
# is then canceled if the deferred succeeds before the timeo... | def _add_timeout(deferred, timeout) | Add a timeout to the given deferred. This is designed to work with both old
Twisted and versions of Twisted with the addTimeout API. This is
exclusively to support EL7.
The deferred will errback with a :class:`defer.CancelledError` if the
version of Twisted being used doesn't have the
``defer.Defer... | 5.609558 | 5.302524 | 1.057903 |
try:
channel = yield self.channel()
except pika.exceptions.NoFreeChannels:
raise NoFreeChannels()
_std_log.debug("Created AMQP channel id %d", channel.channel_number)
if self._confirms:
yield channel.confirm_delivery()
defer.returnValu... | def _allocate_channel(self) | Allocate a new AMQP channel.
Raises:
NoFreeChannels: If this connection has reached its maximum number of channels. | 5.519207 | 4.577753 | 1.205659 |
self._channel = yield self._allocate_channel()
if _pika_version < pkg_resources.parse_version("1.0.0b1"):
extra_args = dict(all_channels=True)
else:
extra_args = dict(global_qos=True)
yield self._channel.basic_qos(
prefetch_count=config.conf["... | def connectionReady(self, res=None) | Callback invoked when the AMQP connection is ready (when self.ready fires).
This API is not meant for users.
Args:
res: This is an unused argument that provides compatibility with Pika
versions lower than 1.0.0. | 3.475587 | 3.55155 | 0.978611 |
while consumer._running:
try:
deferred_get = queue_object.get()
_add_timeout(deferred_get, 1)
channel, delivery_frame, properties, body = yield deferred_get
except (defer.TimeoutError, defer.CancelledError):
continu... | def _read(self, queue_object, consumer) | The loop that reads from the message queue and calls the consumer callback
wrapper.
Serialized Processing
---------------------
This loop processes messages serially. This is because a second
``queue_object.get()`` operation can only occur after the Deferred from
``self.... | 2.434689 | 2.389339 | 1.01898 |
message.validate()
try:
yield self._channel.basic_publish(
exchange=exchange,
routing_key=message._encoded_routing_key,
body=message._encoded_body,
properties=message._properties,
)
except (pika.exce... | def publish(self, message, exchange) | Publish a :class:`fedora_messaging.message.Message` to an `exchange`_
on the message broker.
Args:
message (message.Message): The message to publish.
exchange (str): The name of the AMQP exchange to publish to
Raises:
NoFreeChannels: If there are no availabl... | 3.348531 | 3.078358 | 1.087765 |
channel = yield self._allocate_channel()
try:
for exchange in exchanges:
args = exchange.copy()
args.setdefault("passive", config.conf["passive_declares"])
try:
yield channel.exchange_declare(**args)
... | def declare_exchanges(self, exchanges) | Declare a number of exchanges at once.
This simply wraps the :meth:`pika.channel.Channel.exchange_declare`
method and deals with error handling and channel allocation.
Args:
exchanges (list of dict): A list of dictionaries, where each dictionary
represents an exchan... | 4.539551 | 3.325902 | 1.364908 |
channel = yield self._allocate_channel()
try:
for queue in queues:
args = queue.copy()
args.setdefault("passive", config.conf["passive_declares"])
try:
yield channel.queue_declare(**args)
except pika... | def declare_queues(self, queues) | Declare a list of queues.
Args:
queues (list of dict): A list of dictionaries, where each dictionary
represents an exchange. Each dictionary can have the following keys:
* queue (str): The name of the queue
* passive (bool): If true, this will ju... | 4.767859 | 3.577562 | 1.332712 |
channel = yield self._allocate_channel()
try:
for binding in bindings:
try:
yield channel.queue_bind(**binding)
except pika.exceptions.ChannelClosed as e:
raise BadDeclaration("binding", binding, e)
fina... | def bind_queues(self, bindings) | Declare a set of bindings between queues and exchanges.
Args:
bindings (list of dict): A list of binding definitions. Each dictionary
must contain the "queue" key whose value is the name of the queue
to create the binding on, as well as the "exchange" key whose value... | 3.963278 | 3.479452 | 1.139052 |
if self.is_closed:
# We were asked to stop because the connection is already gone.
# There's no graceful way to stop because we can't acknowledge
# messages in the middle of being processed.
_std_log.info("Disconnect requested, but AMQP connection already... | def halt(self) | Signal to consumers they should stop after finishing any messages
currently being processed, then close the connection.
Returns:
defer.Deferred: fired when all consumers have successfully stopped
and the connection is closed. | 4.869051 | 4.43021 | 1.099056 |
while self._running:
try:
_channel, delivery_frame, properties, body = yield queue_object.get()
except (error.ConnectionDone, ChannelClosedByClient):
# This is deliberate.
_legacy_twisted_log.msg(
"Stopping the ... | def _read(self, queue_object, consumer) | The loop that reads from the message queue and calls the consumer callback
wrapper.
queue_object (pika.adapters.twisted_connection.ClosableDeferredQueue):
The AMQP queue the consumer is bound to.
consumer (dict): A dictionary describing the consumer for the given
queue_o... | 3.021487 | 2.918944 | 1.03513 |
_legacy_twisted_log.msg(
"Message arrived with delivery tag {tag} for {consumer}",
tag=delivery_frame.delivery_tag,
consumer=consumer,
logLevel=logging.DEBUG,
)
try:
message = get_message(delivery_frame.routing_key, properties,... | def _on_message(self, delivery_frame, properties, body, consumer) | Callback when a message is received from the server.
This method wraps a user-registered callback for message delivery. It
decodes the message body, determines the message schema to validate the
message with, and validates the message before passing it on to the
user callback.
... | 2.200636 | 2.272509 | 0.968373 |
if queue in self._consumers and self._consumers[queue].channel.is_open:
consumer = Consumer(
tag=self._consumers[queue].tag,
queue=queue,
callback=callback,
channel=self._consumers[queue].channel,
)
self... | def consume(self, callback, queue) | Register a message consumer that executes the provided callback when
messages are received.
The queue must exist prior to calling this method. If a consumer
already exists for the given queue, the callback is simply updated and
any new messages for that consumer use the new callback.
... | 3.411941 | 3.058083 | 1.115713 |
try:
consumer = self._consumers[queue]
yield consumer.channel.basic_cancel(consumer_tag=consumer.tag)
except pika.exceptions.AMQPChannelError:
# Consumers are tied to channels, so if this channel is dead the
# consumer should already be canceled (... | def cancel(self, queue) | Cancel the consumer for a queue.
Args:
queue (str): The name of the queue the consumer is subscribed to.
Returns:
defer.Deferred: A Deferred that fires when the consumer
is canceled, or None if the consumer was already canceled. Wrap
the call in ... | 4.050325 | 3.975084 | 1.018928 |
# Start consuming
self._running = True
for consumer in self._consumers.values():
queue_object, _ = yield consumer.channel.basic_consume(
queue=consumer.queue, consumer_tag=consumer.tag
)
deferred = self._read(queue_object, consumer)
... | def resumeProducing(self) | Starts or resumes the retrieval of messages from the server queue.
This method starts receiving messages from the server, they will be
passed to the consumer callback.
.. note:: This is called automatically when :meth:`.consume` is called,
so users should not need to call this unle... | 6.310614 | 6.670784 | 0.946008 |
if not self._running:
return
# Exit the read loop and cancel the consumer on the server.
self._running = False
for consumer in self._consumers.values():
yield consumer.channel.basic_cancel(consumer_tag=consumer.tag)
_legacy_twisted_log.msg("Paused... | def pauseProducing(self) | Pause the reception of messages by canceling all existing consumers.
This does not disconnect from the server.
Message reception can be resumed with :meth:`resumeProducing`.
Returns:
Deferred: fired when the production is paused. | 10.459252 | 9.892774 | 1.057262 |
_legacy_twisted_log.msg("Disconnecting from the AMQP broker")
yield self.pauseProducing()
yield self.close()
self._consumers = {}
self._channel = None | def stopProducing(self) | Stop producing messages and disconnect from the server.
Returns:
Deferred: fired when the production is stopped. | 10.251801 | 10.595655 | 0.967548 |
global _registry_loaded
if not _registry_loaded:
load_message_classes()
try:
return _schema_name_to_class[schema_name]
except KeyError:
_log.warning(
'The schema "%s" is not in the schema registry! Either install '
"the package with its schema defini... | def get_class(schema_name) | Retrieve the message class associated with the schema name.
If no match is found, the default schema is returned and a warning is logged.
Args:
schema_name (six.text_type): The name of the :class:`Message` sub-class;
this is typically the Python path.
Returns:
Message: A sub-c... | 5.493711 | 5.27237 | 1.041981 |
global _registry_loaded
if not _registry_loaded:
load_message_classes()
try:
return _class_to_schema_name[cls]
except KeyError:
raise TypeError(
"The class {} is not in the message registry, which indicates it is"
' not in the current list of entry p... | def get_name(cls) | Retrieve the schema name associated with a message class.
Returns:
str: The schema name.
Raises:
TypeError: If the message class isn't registered. Check your entry point
for correctness. | 6.961821 | 5.789557 | 1.202479 |
for message in pkg_resources.iter_entry_points("fedora.messages"):
cls = message.load()
_log.info(
"Registering the '%s' key as the '%r' class in the Message "
"class registry",
message.name,
cls,
)
_schema_name_to_class[message.na... | def load_message_classes() | Load the 'fedora.messages' entry points and register the message classes. | 4.392367 | 3.559053 | 1.234139 |
if properties.headers is None:
_log.error(
"Message (body=%r) arrived without headers. " "A publisher is misbehaving!",
body,
)
properties.headers = {}
try:
MessageClass = get_class(properties.headers["fedora_messaging_schema"])
except KeyError:
... | def get_message(routing_key, properties, body) | Construct a Message instance given the routing key, the properties and the
body received from the AMQP broker.
Args:
routing_key (str): The AMQP routing key (will become the message topic)
properties (pika.BasicProperties): the AMQP properties
body (bytes): The encoded message body
... | 2.409034 | 2.413628 | 0.998097 |
serialized_messages = []
try:
for message in messages:
message_dict = message._dump()
serialized_messages.append(message_dict)
except AttributeError:
_log.error("Improper object for messages serialization.")
raise TypeError("Message have to be instance of... | def dumps(messages) | Serialize messages to a JSON formatted str
Args:
messages (list): The list of messages to serialize. Each message in
the messages is subclass of Messge.
Returns:
str: Serialized messages.
Raises:
TypeError: If at least one message is not instance of Message class or su... | 4.213396 | 4.410065 | 0.955404 |
try:
messages_dicts = json.loads(serialized_messages)
except ValueError:
_log.error("Loading serialized messages failed.")
raise
messages = []
for message_dict in messages_dicts:
try:
headers = message_dict["headers"]
except KeyError:
... | def loads(serialized_messages) | Deserialize messages from a JSON formatted str
Args:
serialized_messages (JSON str):
Returns:
list: Deserialized message objects.
Raises:
ValidationError: If deserialized message validation failed.
KeyError: If serialized_messages aren't properly serialized.
ValueE... | 2.265333 | 2.303822 | 0.983294 |
headers = {}
for user in self.usernames:
headers["fedora_messaging_user_{}".format(user)] = True
for package in self.packages:
headers["fedora_messaging_rpm_{}".format(package)] = True
for container in self.containers:
headers["fedora_messagin... | def _filter_headers(self) | Add headers designed for filtering messages based on objects.
Returns:
dict: Filter-related headers to be combined with the existing headers | 2.599826 | 2.636818 | 0.985971 |
topic = self.topic
if config.conf["topic_prefix"]:
topic = ".".join((config.conf["topic_prefix"].rstrip("."), topic))
return topic.encode("utf-8") | def _encoded_routing_key(self) | The encoded routing key used to publish the message on the broker. | 4.980049 | 4.057231 | 1.22745 |
for schema in (self.headers_schema, Message.headers_schema):
_log.debug(
'Validating message headers "%r" with schema "%r"',
self._headers,
schema,
)
jsonschema.validate(self._headers, schema)
for schema in (sel... | def validate(self) | Validate the headers and body with the message schema, if any.
In addition to the user-provided schema, all messages are checked against
the base schema which requires certain message headers and the that body
be a JSON object.
.. warning:: This method should not be overridden by sub-c... | 2.74684 | 2.446898 | 1.122581 |
return {
"topic": self.topic,
"headers": self._headers,
"id": self.id,
"body": self.body,
"queue": self.queue,
} | def _dump(self) | Dump message attributes.
Returns:
dict: A dictionary of message attributes. | 4.103252 | 3.984909 | 1.029698 |
# If the callback is a class, create an instance of it first
if inspect.isclass(callback):
callback_object = callback()
if not callable(callback_object):
raise ValueError(
"Callback must be a class that implements __call__ or a function."
)
elif c... | def _check_callback(callback) | Turns a callback that is potentially a class into a callable object.
Args:
callback (object): An object that might be a class, method, or function.
if the object is a class, this creates an instance of it.
Raises:
ValueError: If an instance can't be created or it isn't a callable objec... | 2.477363 | 2.535154 | 0.977204 |
if isinstance(bindings, dict):
bindings = [bindings]
callback = _check_callback(callback)
global _twisted_service
if _twisted_service is None:
_twisted_service = service.FedoraMessagingServiceV2(config.conf["amqp_url"])
reactor.callWhenRunning(_twisted_service.startService)... | def twisted_consume(callback, bindings=None, queues=None) | Start a consumer using the provided callback and run it using the Twisted
event loop (reactor).
.. note:: Callbacks run in a Twisted-managed thread pool using the
:func:`twisted.internet.threads.deferToThread` API to avoid them blocking
the event loop. If you wish to use Twisted APIs in your ca... | 5.36974 | 5.366867 | 1.000535 |
if isinstance(bindings, dict):
bindings = [bindings]
if bindings is None:
bindings = config.conf["bindings"]
else:
try:
config.validate_bindings(bindings)
except exceptions.ConfigurationException as e:
raise ValueError(e.message)
if queues i... | def consume(callback, bindings=None, queues=None) | Start a message consumer that executes the provided callback when messages are
received.
This API is blocking and will not return until the process receives a signal
from the operating system.
.. warning:: This API is runs the callback in the IO loop thread. This means
if your callback could r... | 2.51424 | 2.392047 | 1.051083 |
pre_publish_signal.send(publish, message=message)
if exchange is None:
exchange = config.conf["publish_exchange"]
global _session_cache
if not hasattr(_session_cache, "session"):
_session_cache.session = _session.PublisherSession()
try:
_session_cache.session.publish(... | def publish(message, exchange=None) | Publish a message to an exchange.
This is a synchronous call, meaning that when this function returns, an
acknowledgment has been received from the message broker and you can be
certain the message was published successfully.
There are some cases where an error occurs despite your message being
su... | 3.446228 | 3.972722 | 0.867473 |
self.resetDelay()
self.client = self.protocol(self._parameters)
self.client.factory = self
self.client.ready.addCallback(lambda _: self._on_client_ready())
return self.client | def buildProtocol(self, addr) | Create the Protocol instance.
See the documentation of
`twisted.internet.protocol.ReconnectingClientFactory` for details. | 5.601241 | 5.038573 | 1.111672 |
_legacy_twisted_log.msg("Successfully connected to the AMQP broker.")
yield self.client.resumeProducing()
yield self.client.declare_exchanges(self.exchanges)
yield self.client.declare_queues(self.queues)
yield self.client.bind_queues(self.bindings)
for queue, ca... | def _on_client_ready(self) | Called when the client is ready to send and receive messages. | 4.365515 | 4.264399 | 1.023712 |
if not isinstance(reason.value, error.ConnectionDone):
_legacy_twisted_log.msg(
"Lost connection to the AMQP broker ({reason})",
reason=reason.value,
logLevel=logging.WARNING,
)
if self._client_ready.called:
# R... | def clientConnectionLost(self, connector, reason) | Called when the connection to the broker has been lost.
See the documentation of
`twisted.internet.protocol.ReconnectingClientFactory` for details. | 5.444028 | 5.380101 | 1.011882 |
_legacy_twisted_log.msg(
"Connection to the AMQP broker failed ({reason})",
reason=reason.value,
logLevel=logging.WARNING,
)
protocol.ReconnectingClientFactory.clientConnectionFailed(
self, connector, reason
) | def clientConnectionFailed(self, connector, reason) | Called when the client has failed to connect to the broker.
See the documentation of
`twisted.internet.protocol.ReconnectingClientFactory` for details. | 6.408596 | 6.413316 | 0.999264 |
protocol.ReconnectingClientFactory.stopTrying(self)
if not self._client_ready.called:
self._client_ready.errback(
pika.exceptions.AMQPConnectionError(
u"Could not connect, reconnection cancelled."
)
) | def stopTrying(self) | Stop trying to reconnect to the broker.
See the documentation of
`twisted.internet.protocol.ReconnectingClientFactory` for details. | 5.415424 | 5.364187 | 1.009552 |
if self.client:
yield self.client.stopProducing()
protocol.ReconnectingClientFactory.stopFactory(self) | def stopFactory(self) | Stop the factory.
See the documentation of
`twisted.internet.protocol.ReconnectingClientFactory` for details. | 7.339528 | 5.325611 | 1.378157 |
self.consumers[queue] = callback
if self._client_ready.called:
return self.client.consume(callback, queue) | def consume(self, callback, queue) | Register a new consumer.
This consumer will be configured for every protocol this factory
produces so it will be reconfigured on network failures. If a connection
is already active, the consumer will be added to it.
Args:
callback (callable): The callback to invoke when a m... | 7.695776 | 9.911072 | 0.776483 |
try:
del self.consumers[queue]
except KeyError:
pass
if self.client:
return self.client.cancel(queue) | def cancel(self, queue) | Cancel the consumer for a queue.
This removes the consumer from the list of consumers to be configured for
every connection.
Args:
queue (str): The name of the queue the consumer is subscribed to.
Returns:
defer.Deferred or None: Either a Deferred that fires whe... | 4.004139 | 3.94019 | 1.01623 |
exchange = exchange or config.conf["publish_exchange"]
while True:
client = yield self.whenConnected()
try:
yield client.publish(message, exchange)
break
except ConnectionException:
continue | def publish(self, message, exchange=None) | Publish a :class:`fedora_messaging.message.Message` to an `exchange`_
on the message broker. This call will survive connection failures and try
until it succeeds or is canceled.
Args:
message (message.Message): The message to publish.
exchange (str): The name of the AMQP... | 7.292766 | 5.960085 | 1.223601 |
self._client = self.protocol(self._parameters, confirms=self.confirms)
self._client.factory = self
@defer.inlineCallbacks
def on_ready(unused_param=None):
_std_log.debug("AMQP handshake completed; connection ready for use")
self.resetDelay()... | def buildProtocol(self, addr) | Create the Protocol instance.
See the documentation of
`twisted.internet.protocol.ReconnectingClientFactory` for details. | 6.130347 | 5.991855 | 1.023113 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.