code string | signature string | docstring string | loss_without_docstring float64 | loss_with_docstring float64 | factor float64 |
|---|---|---|---|---|---|
ret = {}
for scc in tarjan(g):
ws = set()
ews = set()
for v in scc:
ws.update(g[v])
for w in ws:
assert w in ret or w in scc
ews.add(w)
... | def tc(g) | Given a graph @g, returns the transitive closure of @g | 3.538766 | 3.45786 | 1.023398 |
feeds = configparser.ConfigParser()
feeds.read(self.data_filename)
return feeds.sections() | def list_feeds(self) | Output a list of all feed names | 6.400015 | 5.612605 | 1.140293 |
try:
if self.args["configfile"]:
return self.args["configfile"]
except KeyError:
pass
return os.path.expanduser('~/.config/greg/greg.conf') | def retrieve_config_file(self) | Retrieve config file | 4.657415 | 4.262559 | 1.092634 |
args = self.args
try:
if args['datadirectory']:
aux.ensure_dir(args['datadirectory'])
return args['datadirectory']
except KeyError:
pass
config = configparser.ConfigParser()
config.read([config_filename_global, self... | def retrieve_data_directory(self) | Retrieve the data directory
Look first into config_filename_global
then into config_filename_user. The latter takes preeminence. | 3.599406 | 3.105933 | 1.158881 |
args = self.args
name = self.name
try:
if args[value]:
return args[value]
except KeyError:
pass
section = name if self.config.has_section(
name) else self.config.default_section
answer = self.config.get(section,... | def retrieve_config(self, value, default) | Retrieves a value (with a certain fallback) from the config files
(looks first into config_filename_global then into
config_filename_user. The latest takes preeminence) if the command line
flag for the value is used, that overrides everything else | 3.78509 | 3.462694 | 1.093106 |
section = self.name if self.config.has_section(
self.name) else self.config.default_section
download_path = self.config.get(
section, 'Download directory', fallback='~/Podcasts')
subdirectory = self.config.get(
section, 'Create subdirectories', fallba... | def retrieve_download_path(self) | Retrieves the download path (looks first into config_filename_global
then into the [DEFAULT], then the [feed], section of
config_filename_user. The latest takes preeminence) | 4.47022 | 3.920224 | 1.140297 |
wanttags = self.retrieve_config('Tag', 'no')
if wanttags == 'yes':
if aux.staggerexists:
willtag = True
else:
willtag = False
print(("You want me to tag {0}, but you have not installed "
"the Stagger ... | def will_tag(self) | Check whether the feed should be tagged | 7.134449 | 6.777534 | 1.052662 |
if self.linkdates != []:
# What follows is a quick sanity check: if the entry date is in the
# future, this is probably a mistake, and we just count the entry
# date as right now.
if max(self.linkdates) <= list(time.localtime()):
currentda... | def how_many(self) | Ascertain where to start downloading, and how many entries. | 5.686975 | 5.295127 | 1.074002 |
if self.sync_by_date:
try:
entry.linkdate = list(entry.published_parsed)
self.linkdate = list(entry.published_parsed)
except (AttributeError, TypeError):
try:
entry.linkdate = list(entry.updated_parsed)
... | def fix_linkdate(self, entry) | Give a date for the entry, depending on feed.sync_by_date
Save it as feed.linkdate | 2.599968 | 2.129679 | 1.220826 |
mime = self.retrieve_config('mime', 'audio')
mimedict = {"number": mime}
# the input that parse_for_download expects
return aux.parse_for_download(mimedict) | def retrieve_mime(self) | Check the mime-type to download | 23.486034 | 19.710093 | 1.191574 |
downloadlinks = {}
downloaded = False
ignoreenclosures = self.retrieve_config('ignoreenclosures', 'no')
notype = self.retrieve_config('notype', 'no')
if ignoreenclosures == 'no':
for enclosure in entry.enclosures:
if notype == 'yes':
... | def download_entry(self, entry) | Find entry link and download entry | 4.74614 | 4.674875 | 1.015244 |
args = parser.parse_args()
try:
function = args.func
except AttributeError:
parser.print_usage()
parser.exit(1)
function(vars(args)) | def main() | Parse the args and call whatever function was selected | 3.486479 | 2.685604 | 1.29821 |
session = c.Session(args)
if args["name"] in session.feeds.sections():
sys.exit("You already have a feed with that name.")
if args["name"] in ["all", "DEFAULT"]:
sys.exit(
("greg uses ""{}"" for a special purpose."
"Please choose another name for your feed.").fo... | def add(args) | Add a new feed | 5.360404 | 5.011939 | 1.069527 |
session = c.Session(args)
if not args["name"] in session.feeds:
sys.exit("You don't have a feed with that name.")
inputtext = ("Are you sure you want to remove the {} "
" feed? (y/N) ").format(args["name"])
reply = input(inputtext)
if reply != "y" and reply != "Y":
... | def remove(args) | Remove the feed given in <args> | 3.404809 | 3.174767 | 1.072459 |
session = c.Session(args)
if "all" in args["names"]:
feeds = session.list_feeds()
else:
feeds = args["names"]
for feed in feeds:
aux.pretty_print(session, feed) | def info(args) | Provide information of a number of feeds | 6.439721 | 5.465878 | 1.178168 |
import operator
session = c.Session(args)
if "all" in args["names"]:
targetfeeds = session.list_feeds()
else:
targetfeeds = []
for name in args["names"]:
if name not in session.feeds:
print("You don't have a feed called {}."
... | def sync(args) | Implement the 'greg sync' command | 4.274459 | 4.233695 | 1.009629 |
session = c.Session(args)
if str(args["url"]) != 'None':
url = args["url"]
name = "DEFAULT"
else:
try:
url = session.feeds[args["feed"]]["url"]
name = args["feed"]
except KeyError:
sys.exit("You don't appear to have a feed with that na... | def check(args) | Implement the 'greg check' command | 3.755402 | 3.819617 | 0.983188 |
session = c.Session(args)
issues = aux.parse_for_download(args)
if issues == ['']:
sys.exit(
"You need to give a list of issues, of the form ""a, b-c, d...""")
dumpfilename = os.path.join(session.data_dir, 'feeddump')
if not os.path.isfile(dumpfilename):
sys.exit(
... | def download(args) | Implement the 'greg download' command | 7.535344 | 7.089897 | 1.062828 |
try:
podcast = feedparser.parse(url)
wentwrong = "urlopen" in str(podcast["bozo_exception"])
except KeyError:
wentwrong = False
if wentwrong:
print("Error: ", url, ": ", str(podcast["bozo_exception"]))
return podcast | def parse_podcast(url) | Try to parse podcast | 4.828053 | 4.768523 | 1.012484 |
feed = placeholders.feed
args = feed.args
placeholders.directory = "This very directory" # wink, wink
placeholders.fullpath = os.path.join(
placeholders.directory, placeholders.filename)
try:
if args["downloaddirectory"]:
ensure_dir(args["downloaddirectory"])
... | def check_directory(placeholders) | Find out, and create if needed,
the directory in which the feed will be downloaded | 4.391221 | 4.220084 | 1.040553 |
single_arg = ""
# in the first bit we put all arguments
# together and take out any extra spaces
list_of_feeds = []
for arg in args["number"]:
single_arg = ''.join([single_arg, " ", arg])
single_arg = single_arg.translate({32: None}) # eliminates spaces
for group in single_arg.... | def parse_for_download(args) | Turn an argument such as 4, 6-8, 10 into a list such as [4,6,7,8,10] | 4.611574 | 4.150566 | 1.111071 |
# We first recover the name of the file to be tagged...
template = placeholders.feed.retrieve_config("file_to_tag", "{filename}")
filename = substitute_placeholders(template, placeholders)
podpath = os.path.join(placeholders.directory, filename)
# ... and this is it
# now we create a dicti... | def tag(placeholders) | Tag the file at podpath with the information in podcast and entry | 7.724276 | 7.294119 | 1.058973 |
import shlex
value = feed.retrieve_config('downloadhandler', 'greg')
if value == 'greg':
while os.path.isfile(placeholders.fullpath):
placeholders.fullpath = placeholders.fullpath + '_'
placeholders.filename = placeholders.filename + '_'
urlretrieve(placeholders.... | def download_handler(feed, placeholders) | Parse and execute the download handler | 6.383 | 6.178995 | 1.033016 |
entrylinks = []
linkdates = []
try:
with open(infofile, 'r') as previous:
for line in previous:
entrylinks.append(line.split(sep=' ')[0])
# This is the list of already downloaded entry links
linkdates.append(eval(line.split(sep=' ', ma... | def parse_feed_info(infofile) | Take a feed file in .local/share/greg/data and return a list of links and
of dates | 4.768693 | 4.422469 | 1.078288 |
if feed in session.feeds:
print()
feed_info = os.path.join(session.data_dir, feed)
entrylinks, linkdates = parse_feed_info(feed_info)
print(feed)
print("-"*len(feed))
print(''.join([" url: ", session.feeds[feed]["url"]]))
if linkdates != []:
... | def pretty_print(session, feed) | Print the dictionary entry of a feed in a nice way. | 5.234652 | 5.047662 | 1.037045 |
newst = inputstring.format(link=placeholders.link,
filename=placeholders.filename,
directory=placeholders.directory,
fullpath=placeholders.fullpath,
title=placeholders.title,
... | def substitute_placeholders(inputstring, placeholders) | Take a string with placeholders, and return the strings with substitutions. | 4.431804 | 4.632111 | 0.956757 |
ctx.index[v] = len(ctx.index)
ctx.lowlink[v] = ctx.index[v]
ctx.S.append(v)
ctx.S_set.add(v)
it = iter(ctx.g.get(v, ()))
ctx.T.append((it,False,v,None)) | def _tarjan_head(ctx, v) | Used by @tarjan and @tarjan_iter. This is the head of the
main iteration | 4.273627 | 4.355569 | 0.981187 |
for w in it:
if w not in ctx.index:
ctx.T.append((it,True,v,w))
_tarjan_head(ctx, w)
return
if w in ctx.S_set:
ctx.lowlink[v] = min(ctx.lowlink[v], ctx.index[w])
if ct... | def _tarjan_body(ctx, it, v) | Used by @tarjan and @tarjan_iter. This is the body of the
main iteration | 3.267499 | 3.512084 | 0.930359 |
ctx = TarjanContext(
g = g,
S = [],
S_set = set(),
index = {},
lowlink = {},
T = [],
ret = [])
main_iter = iter(g)
while True:
try:
v =... | def tarjan_iter(g) | Returns the strongly connected components of the graph @g
in a topological order.
@g is the graph represented as a dictionary
{ <vertex> : <successors of vertex> }.
This function does not recurse. It returns an iterator. | 3.874801 | 4.171292 | 0.928921 |
S = []
S_set = set()
index = {}
lowlink = {}
ret = []
def visit(v):
index[v] = len(index)
lowlink[v] = index[v]
S.append(v)
S_set.add(v)
for w in g.get(v,()):
... | def tarjan_recursive(g) | Returns the strongly connected components of the graph @g
in a topological order.
@g is the graph represented as a dictionary
{ <vertex> : <successors of vertex> }.
This function recurses --- large graphs may cause a stack
... | 1.914935 | 1.924737 | 0.994907 |
T, D, _ = H_diag.shape
assert H_diag.ndim == 3 and H_diag.shape[2] == D
assert H_upper_diag.shape == (T-1, D, D)
assert v.shape == (T, D)
out = np.matmul(H_diag, v[:, :, None])[:, :, 0]
out[:-1] += np.matmul(H_upper_diag, v[1:][:, :, None])[:, :, 0]
out[1:] += np.matmul(np.swapaxes(H_u... | def symm_block_tridiag_matmul(H_diag, H_upper_diag, v) | Compute matrix-vector product with a symmetric block
tridiagonal matrix H and vector v.
:param H_diag: block diagonal terms of H
:param H_upper_diag: upper block diagonal terms of H
:param v: vector to multiple
:return: H * v | 2.065769 | 2.290086 | 0.902049 |
T, D, _ = H_diag.shape
assert H_diag.ndim == 3 and H_diag.shape[2] == D
assert H_upper_diag.shape == (T - 1, D, D)
H_lower_diag = np.swapaxes(H_upper_diag, -2, -1)
ab = np.zeros((2 * D, T * D))
# Fill in blocks along the diagonal
for d in range(D):
# Get indices of (-d)-th dia... | def convert_block_tridiag_to_banded(H_diag, H_upper_diag, lower=True) | convert blocks to banded matrix representation required for scipy.
we are using the "lower form."
see https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.solveh_banded.html | 2.20881 | 2.2193 | 0.995273 |
from scipy.linalg import solveh_banded
ab = convert_block_tridiag_to_banded(H_diag, H_upper_diag) \
if ab is None else ab
x = solveh_banded(ab, v.ravel(), lower=True)
return x.reshape(v.shape) | def scipy_solve_symm_block_tridiag(H_diag, H_upper_diag, v, ab=None) | use scipy.linalg.solve_banded to solve a symmetric block tridiagonal system
see https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.solveh_banded.html | 2.490966 | 2.529546 | 0.984748 |
T, D, _ = H_diag.shape
assert H_diag.ndim == 3 and H_diag.shape[2] == D
assert H_upper_diag.shape == (T - 1, D, D)
J_init = J_11 = J_22 = np.zeros((D, D))
h_init = h_1 = h_2 = np.zeros((D,))
J_21 = np.swapaxes(H_upper_diag, -1, -2)
J_node = H_diag
h_node = np.zeros((T,D))
y =... | def sample_block_tridiag(H_diag, H_upper_diag) | helper function for sampling block tridiag gaussians.
this is only for speed comparison with the solve approach. | 2.930783 | 2.945758 | 0.994916 |
T, D, _ = H_diag.shape
assert H_diag.ndim == 3 and H_diag.shape[2] == D
assert H_upper_diag.shape == (T - 1, D, D)
J_init = J_11 = J_22 = np.zeros((D, D))
h_init = h_1 = h_2 = np.zeros((D,))
log_Z_init = 0
J_21 = np.swapaxes(H_upper_diag, -1, -2)
log_Z_pair = 0
J_node = H_dia... | def logdet_symm_block_tridiag(H_diag, H_upper_diag) | compute the log determinant of a positive definite,
symmetric block tridiag matrix. Use the Kalman
info filter to do so. Specifically, the KF computes
the normalizer:
log Z = 1/2 h^T J^{-1} h -1/2 log |J| +n/2 log 2 \pi
We set h=0 to get -1/2 log |J| + n/2 log 2 \pi and from
this we solv... | 3.011415 | 2.642969 | 1.139406 |
T, D, _ = H_diag.shape
assert H_diag.ndim == 3 and H_diag.shape[2] == D
assert H_upper_diag.shape == (T - 1, D, D)
J_init = J_11 = J_22 = np.zeros((D, D))
h_init = h_1 = h_2 = np.zeros((D,))
J_21 = np.swapaxes(H_upper_diag, -1, -2)
J_node = H_diag
h_node = np.zeros((T, D))
_,... | def compute_symm_block_tridiag_covariances(H_diag, H_upper_diag) | use the info smoother to solve a symmetric block tridiagonal system | 3.28748 | 3.282142 | 1.001626 |
# TODO: move this to cython?
T, N, C, D, b = self.T, self.D_emission, self.C, self.D, self.emission_distn.b
indptr = [0]
indices = []
vals = []
offset = 0
X = np.hstack((self.gaussian_states, self.inputs))
for t in range(T):
# Evaluate... | def resample_zeroinflation_variables(self) | There's no way around the fact that we have to look at every
data point, even the zeros here. | 3.349323 | 3.308909 | 1.012213 |
# Flatten the covariance
T = mus.shape[0]
D = self.D_in
sigs_vec = sigmas.reshape((T, D ** 2))
# Compute the log likelihood of each column
ll = np.zeros((T, self.D_out))
for n in range(self.D_out):
an = self.A[n]
E_loglmbda = n... | def expected_log_likelihood(self, mus, sigmas, y) | Compute the expected log likelihood for a mean and
covariance of x and an observed value of y. | 4.596652 | 4.766949 | 0.964276 |
if isinstance(data, list):
x = np.vstack([d[0] for d in data])
y = np.vstack([d[1] for d in data])
elif isinstance(data, tuple):
assert len(data) == 2
elif isinstance(data, np.ndarray):
x, y = data[:,:self.D_in], data[:, self.D_in:]
... | def max_likelihood(self, data, weights=None, stats=None) | Maximize the likelihood for given data
:param data:
:param weights:
:param stats:
:return: | 2.466755 | 2.531273 | 0.974512 |
T, D = self.T, self.D_latent
assert x.shape == (T, D)
ll = 0
for t in range(self.T):
ll += self.local_log_likelihood(x[t], self.data[t], self.inputs[t])
return ll | def log_conditional_likelihood(self, x) | likelihood \sum_t log p(y_t | x_t)
Optionally override this in base classes | 3.690728 | 4.239261 | 0.870606 |
T, D = self.T, self.D_latent
assert x.shape == (T, D)
gfun = grad(self.local_log_likelihood)
g = np.zeros((T, D))
for t in range(T):
g[t] += gfun(x[t], self.data[t], self.inputs[t])
return g | def grad_local_log_likelihood(self, x) | return d/dxt log p(yt | xt) evaluated at xt
Optionally override this in base classes | 3.652872 | 4.029223 | 0.906595 |
T, D = self.T, self.D_latent
assert x.shape == (T, D)
hfun = hessian(self.local_log_likelihood)
H_diag = np.zeros((T, D, D))
for t in range(T):
H_diag[t] = hfun(x[t], self.data[t], self.inputs[t])
return H_diag | def hessian_local_log_likelihood(self, x) | return d^2/dxt^2 log p(y | x) for each time bin
Optionally override this in base classes | 3.438576 | 3.842897 | 0.894787 |
T, D = self.T, self.D_latent
assert x.shape == (T, D)
# prior log p(x) -- quadratic terms
J_diag, J_upper_diag = self.sparse_J_prior
lp = -0.5 * np.sum(x * symm_block_tridiag_matmul(J_diag, J_upper_diag, x))
# prior log p(x) -- linear terms
_, h_init, l... | def log_joint(self, x) | Compute the log joint probability p(x, y) | 4.398901 | 4.427735 | 0.993488 |
T, D = self.T, self.D_latent
assert x.shape == (T, D)
# Collect the Gaussian LDS prior terms
J_diag, J_upper_diag = self.sparse_J_prior
H_diag, H_upper_diag = -J_diag, -J_upper_diag
# Collect the likelihood terms
H_diag += self.hessian_local_log_likelih... | def sparse_hessian_log_joint(self, x) | The Hessian includes the quadratic terms of the Gaussian LDS prior
as well as the Hessian of the local log likelihood. | 5.526696 | 4.456088 | 1.240257 |
T, D = self.T, self.D_latent
assert x.shape == (T, D)
# Collect the Gaussian LDS prior terms
_, h_init, _ = self.info_init_params
_, _, _, h1, h2, _ = self.info_dynamics_params
H_diag, H_upper_diag = self.sparse_J_prior
# Compute the gradient from the p... | def gradient_log_joint(self, x) | The gradient of the log joint probability.
For the Gaussian terms, this is
d/dx [-1/2 x^T J x + h^T x] = -Jx + h.
For the likelihood terms, we have for each time t
d/dx log p(yt | xt) | 6.321738 | 6.788263 | 0.931275 |
from pylds.util import solve_symm_block_tridiag, scipy_solve_symm_block_tridiag
scale = self.T * self.D_emission
def newton_step(x, stepsz):
assert 0 <= stepsz <= 1
g = self.gradient_log_joint(x)
H_diag, H_upper_diag = self.sparse_hessian_log_joint(x... | def _laplace_approximation_newton(self, tol=1e-6, stepsz=0.9, verbose=False) | Solve a block tridiagonal system with message passing. | 3.658657 | 3.655121 | 1.000967 |
# Observation likelihoods
lmbda = np.exp(np.dot(x, self.C.T) + np.dot(self.inputs, self.D.T))
return (self.data - lmbda).dot(self.C) | def grad_local_log_likelihood(self, x) | d/dx y^T Cx + y^T d - exp(Cx+d)
= y^T C - exp(Cx+d)^T C
= (y - lmbda)^T C | 5.086603 | 4.356154 | 1.167682 |
# Observation likelihoods
lmbda = np.exp(np.dot(x, self.C.T) + np.dot(self.inputs, self.D.T))
return np.einsum('tn, ni, nj ->tij', -lmbda, self.C, self.C) | def hessian_local_log_likelihood(self, x) | d/dx (y - lmbda)^T C = d/dx -exp(Cx + d)^T C
= -C^T exp(Cx + d)^T C | 5.43904 | 4.393054 | 1.2381 |
C, D, u, y = self.C, self.D, self.inputs, self.data
psi = x.dot(C.T) + u.dot(D.T)
p = 1. / (1 + np.exp(-psi))
return (y - p).dot(C) | def grad_local_log_likelihood(self, x) | d/d \psi y \psi - log (1 + exp(\psi))
= y - exp(\psi) / (1 + exp(\psi))
= y - sigma(psi)
= y - p
d \psi / dx = C
d / dx = (y - sigma(psi)) * C | 4.238722 | 4.100004 | 1.033834 |
C, D, u, y = self.C, self.D, self.inputs, self.data
psi = x.dot(C.T) + u.dot(D.T)
p = 1. / (1 + np.exp(-psi))
dp_dpsi = p * (1 - p)
return np.einsum('tn, ni, nj ->tij', -dp_dpsi, self.C, self.C) | def hessian_local_log_likelihood(self, x) | d/dx (y - p) * C
= -dpsi/dx (dp/d\psi) C
= -C p (1-p) C | 4.47473 | 3.950068 | 1.132824 |
masked_datas = [s.masked_data.tocsc() for s in self.states_list]
xs = [np.hstack((s.gaussian_states, s.inputs))for s in self.states_list]
for n in range(self.D_obs):
# Get the nonzero values of the nth column
rowns = [md.indices[md.indptr[n]:md.indptr[n+1]] for ... | def resample_emission_distn(self) | Now for the expensive part... the data is stored in a sparse row
format, which is good for updating the latent states (since we
primarily rely on dot products with the data, which can be
efficiently performed for CSR matrices).
However, in order to update the n-th row of the emission ma... | 3.88808 | 3.498064 | 1.111495 |
@conv_func
def my_conv(n_messages, messages, p_response, app_data):
# Create an array of n_messages response objects
addr = calloc(n_messages, sizeof(PamResponse))
response = cast(addr, POINTER(PamResponse))
p_response[0] = response
... | def authenticate(self, username, password, service='login', encoding='utf-8', resetcreds=True) | username and password authentication for the given service.
Returns True for success, or False for failure.
self.code (integer) and self.reason (string) are always stored and may
be referenced for the reason why authentication failed. 0/'Success' will
be stored for success.... | 3.776961 | 3.539263 | 1.06716 |
found_names = []
with open(self.filename) as f:
content = f.readlines()
for line in content:
line = line.strip()
if " " in line:
attribute, value = line.split(" ", 1)
attribute = attribute.strip()
value ... | def names(self) | The names defined in ~/.ssh/config
:return: the names | 2.776166 | 2.592266 | 1.070942 |
with open(self.filename) as f:
content = f.readlines()
content = [" ".join(x.split()).strip('\n').lstrip().split(' ', 1) for x in content]
# removes duplicated spaces, and splits in two fields, removes leading spaces
hosts = {}
host = "NA"
for line i... | def load(self) | list the hosts defined in the ssh config file | 5.875459 | 5.392901 | 1.08948 |
if name in ["localhost"]:
r = '\n'.join(Shell.sh("-c", command).split()[-1:])
else:
r = '\n'.join(Shell.ssh(name, command).split()[-1:])
return r | def execute(self, name, command) | execute the command on the named host
:param name: the name of the host in config
:param command: the command to be executed
:return: | 5.50182 | 5.95015 | 0.924652 |
data = {
"host": host,
"key": key,
"username": username
}
if verbose and key in self.names():
Console.error("{key} already in ~/.ssh/config".format(**data), traceflag=False)
return ""
else:
entry = dedent(.f... | def generate(self,
key="india",
host="india.futuresystems.org",
username=None,
force=False,
verbose=False) | adds a host to the config file with given parameters. #TODO: make sure this is better documented
:param key: the key
:param host: the host
:param username: the username
:param force: not used
:param verbose: prints debug messages
:return: | 4.162916 | 4.03613 | 1.031413 |
pwd = os.getcwd()
name = filename.replace(pwd, "$PWD")
try:
(first, name) = name.split("site-packages")
name += "... site"
except:
pass
loglevel = logging.CRITICAL
try:
level = grep("loglevel:", config_file(
"/cloudmesh_debug.yaml")).strip().spli... | def LOGGER(filename) | creates a logger with the given name.
You can use it as follows::
log = cloudmesh.common.LOGGER(__file__)
log.error("this is an error")
log.info("this is an info")
log.warning("this is a warning") | 3.267518 | 3.216836 | 1.015755 |
items = []
for k, v in d.items():
new_key = k
for p in prefix:
new_key = new_key.replace(p, new_prefix, 1)
items.append((new_key, v))
return dict(items) | def key_prefix_replace(d, prefix, new_prefix="") | replaces the list of prefix in keys of a flattened dict
:param d: the flattened dict
:param prefix: a list of prefixes that are replaced with a new prefix.
Typically this will be ""
:type prefix: list of str
:param new_prefix: The new prefix. By default it is set to ""
:return: t... | 1.964287 | 2.223795 | 0.883304 |
# http://stackoverflow.com/questions/6027558/flatten-nested-python-dictionaries-compressing-keys
if type(d) == list:
flat = []
for entry in d:
flat.append(flatten(entry, parent_key=parent_key, sep=sep))
return flat
else:
items = []
for k, v in d.items... | def flatten(d, parent_key='', sep='__') | flattens the dict into a one dimensional dictionary
:param d: multidimensional dict
:param parent_key: replaces from the parent key
:param sep: the separation character used when fattening. the default is __
:return: the flattened dict | 1.747977 | 1.769336 | 0.987928 |
dict_result = cls.object_to_dict(obj)
if flatten:
dict_result = FlatDict(dict_result)
return dict_result | def convert(cls, obj, flatten=True) | This function converts object into a Dict optionally Flattening it
:param obj: Object to be converted
:param flatten: boolean to specify if the dict has to be flattened
:return dict: the dict of the object (Flattened or Un-flattened) | 3.35487 | 4.167583 | 0.804992 |
dict_obj = dict()
if obj is not None:
if type(obj) == list:
dict_list = []
for inst in obj:
dict_list.append(cls.object_to_dict(inst))
dict_obj["list"] = dict_list
elif not cls.is_primitive(obj):
... | def object_to_dict(cls, obj) | This function converts Objects into Dictionary | 1.83619 | 1.820173 | 1.0088 |
if debug and error is not None:
print(error)
# TODO: BUG: trace should only be printed if debug is true
if trace:
print(traceback.format_exc()) | def msg(cls, error=None, debug=True, trace=True) | prints the error message
:param error: the error message
:param debug: only prints it if debug is set to true
:param trace: if true prints the trace
:return: | 5.046156 | 5.729718 | 0.880699 |
# TODO: if debug:
Error.msg(error=error, debug=debug, trace=trace) | def traceback(cls, error=None, debug=True, trace=True) | prints the trace
:param error: a message preceding the trace
:param debug: prints it if debug is set to true
:param trace:
:return: | 9.943754 | 9.983443 | 0.996025 |
return "\n".join(
textwrap.wrap(text,
width=width,
initial_indent=" " * indent,
subsequent_indent=" " * indent)) | def indent(text, indent=2, width=128) | indents the given text by the indent specified and wrapping to the given width
:param text: the text to print
:param indent: indent characters
:param width: the width of the text
:return: | 2.387379 | 3.388829 | 0.704485 |
if color:
Console.theme = Console.theme_color
else:
Console.theme = Console.theme_bw
Console.color = color | def set_theme(color=True) | defines if the console messages are printed in color
:param color: if True its printed in color
:return: | 4.760948 | 5.375376 | 0.885696 |
# print (message, prefix)
message = message or ""
if prefix:
text = "ERROR: "
else:
text = ""
if cls.color:
cls.cprint('FAIL', text, str(message))
else:
print(cls.txt_msg(text + str(message)))
if traceflag ... | def error(cls, message, prefix=True, traceflag=False) | prints an error message
:param message: the message
:param prefix: a prefix for the message
:param traceflag: if true the stack trace is retrieved and printed
:return: | 4.43711 | 4.724435 | 0.939183 |
message = message or ""
if prefix:
text = "TODO: "
else:
text = ""
if Console.color:
Console.cprint('FAIL', text, str(message))
else:
print(Console.msg(text + str(message)))
trace = traceback.format_exc().strip()
... | def TODO(message, prefix=True, traceflag=True) | prints an TODO message
:param message: the message
:param prefix: if set to true it prints TODO: as prefix
:param traceflag: if true the stack trace is retrieved and printed
:return: | 4.427874 | 4.918327 | 0.90028 |
message = message or ""
if Console.color:
Console.cprint('RED', 'DEBUG: ', message)
else:
print(Console.msg('DEBUG: ' + message)) | def debug_msg(message) | print a debug message
:param message: the message
:return: | 8.234778 | 9.71784 | 0.847388 |
message = message or ""
if Console.color:
Console.cprint('OKBLUE', "INFO: ", message)
else:
print(Console.msg("INFO: " + message)) | def info(message) | prints an informational message
:param message: the message
:return: | 8.16234 | 9.449832 | 0.863755 |
message = message or ""
if Console.color:
Console.cprint('WARNING', "WARNING: ", message)
else:
print(Console.msg("WARNING: " + message)) | def warning(message) | prints a warning
:param message: the message
:return: | 7.834002 | 8.774437 | 0.892821 |
message = message or ""
if Console.color:
Console.cprint('OKGREEN', "", message)
else:
print(Console.msg(message)) | def ok(message) | prints an ok message
:param message: the message<
:return: | 11.049846 | 12.710223 | 0.869367 |
message = message or ""
prefix = prefix or ""
print((Console.theme[color] +
prefix +
message +
Console.theme['ENDC'])) | def cprint(color, prefix, message) | prints a message in a given color
:param color: the color as defined in the theme
:param prefix: the prefix (a string)
:param message: the message
:return: | 7.370386 | 7.179491 | 1.026589 |
if cls.debug:
print("Timer", name, "started ...")
cls.timer_start[name] = time.time() | def start(cls, name) | starts a timer with the given name.
:param name: the name of the timer
:type name: string | 5.664026 | 6.02048 | 0.940793 |
cls.timer_end[name] = time.time()
if cls.debug:
print("Timer", name, "stopped ...") | def stop(cls, name) | stops the timer with a given name.
:param name: the name of the timer
:type name: string | 6.890841 | 7.906579 | 0.871533 |
if name in cls.timer_end:
cls.timer_elapsed[name] = cls.timer_end[name] - \
cls.timer_start[name]
return cls.timer_elapsed[name]
else:
return "undefined" | def get(cls, name) | returns the time of the timer.
:param name: the name of the timer
:type name: string
:rtype: the elapsed time | 4.049394 | 3.642614 | 1.111673 |
if cls.verbose:
if len(args) == 2:
print(args[0], str("{0:.2f}".format(cls.get(args[1]))), "s")
else:
raise Exception("StopWatch: wrong number of arguments") | def print(cls, *args) | prints a timer. The first argument is the label if it exists, the last is the timer
:param args: label, name
:return: | 5.215424 | 4.461234 | 1.169054 |
#
# PRINT PLATFORM
#
if sysinfo:
data_platform = systeminfo()
print(Printer.attribute(data_platform,
["Machine Arribute", "Time/s"]))
#
# PRINT TIMERS
#
timers = StopWatch.keys()
... | def benchmark(cls, sysinfo=True) | prints out all timers in a convenient benchmark tabble
:return:
:rtype: | 4.054155 | 3.869802 | 1.047639 |
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
def fstr(s):
return s if is_number(s) else '"%s"' % s
if mode != 'dict':
kv_tpl = '("%s", %s)'
ST = 'OrderedDict([\n'
END ... | def dprint(OD, mode='dict', s="", indent=' ' * 4, level=0) | a recursive dict printer method that adds indentations
TODO: needs better explanation and test example
:param OD: the ordered dict
:param mode: the mode is dict
:param s: TODO
:param indent: the indentation characters. default is 4
:param level: the level
:return: | 2.65048 | 2.624744 | 1.009805 |
d = ConfigDict("cloudmesh.yaml")
print(d, end='')
d.info()
print(d["meta"])
print(d["meta.kind"])
print(d["meta"]["kind"])
# this does not yet work
d.data["cloudmesh"]["profile"]["firstname"] = 'ABC'
print(d)
d.save()
import os
os.system("cat cmd3.yaml")
pri... | def main() | TODO: A test which should actually be moved into a nosetest
:return: | 6.896792 | 6.949791 | 0.992374 |
filename = path_expand(filename)
file_contains_tabs = False
with open(filename, 'r') as f:
lines = f.read().split("\n")
line_no = 1
for line in lines:
if "\t" in line:
file_contains_tabs = True
location = [
... | def check_file_for_tabs(cls, filename, verbose=True) | identifies if the file contains tabs and returns True if it
does. It also prints the location of the lines and columns. If
verbose is set to False, the location is not printed.
:param verbose: if true prints issues
:param filename: the filename
:type filename: str
:rtype... | 3.030993 | 3.254096 | 0.93144 |
current_dir = "." + os.path.sep
if path.startswith(current_dir):
cwd = str(os.getcwd())
path = path.replace(current_dir, cwd, 1)
location = os.path.expandvars(os.path.expanduser(path))
return location | def path_expand(cls, path) | expands the path while replacing environment variables, ./, and ~/
:param path: the path to be expanded
:type path: string
:return:the new path
:rtype: string | 2.982096 | 2.971663 | 1.003511 |
if load_order is None:
load_order = [".", os.path.join("~", ".cloudmesh")]
for path in load_order:
name = Config.path_expand(path + os.path.sep + filename)
if verbose:
print("try finding file", name)
if os.path.isfile(name):
... | def find_file(cls, filename, load_order=None, verbose=False) | find the specified file in the list of directories that are given in the
array load_order
:param filename: the file name
:type filename: str
:param load_order: an array with path names in with the filename is looked for.
:type load_order: list of str
:param verbose:
... | 3.281407 | 3.495209 | 0.93883 |
# print ("LOAD CONFIGDICT", filename)
self.data = BaseConfigDict(filename=Config.path_expand(filename))
try:
version = str(self.data["meta"]["version"])
if version not in self.versions:
Console.error("The yaml file version must be {}".format(
... | def load(self, filename) | loads the configuration from the yaml filename
:param filename:
:type filename: string
:return: | 5.436296 | 5.286393 | 1.028356 |
if filename is not None:
location = path_expand(filename)
else:
location = self['meta']['location']
# with open('data.yml', 'w') as outfile:
# outfile.write( yaml.dump(data, default_flow_style=True) )
# Make a backup
self.make... | def write(self, filename=None, output="dict", attribute_indent=4) | This method writes the dict into various output formats. This includes a dict,
json, and yaml
:param filename: the file in which the dict is written
:param output: is a string that is either "dict", "json", "yaml"
:param attribute_indent: character indentation of nested attributes in | 3.005613 | 3.202969 | 0.938383 |
import shutil
destination = backup_name(location)
shutil.copyfile(location, destination) | def make_a_copy(self, location=None) | Creates a backup of the file specified in the location. The backup
filename appends a .bak.NO where number is a number that is not yet
used in the backup directory.
TODO: This function should be moved to another file maybe XShell
:param location: the location of the file to be backed up | 8.803351 | 8.178579 | 1.076391 |
content = self.data.yaml()
with open(Config.path_expand(ConfigDict.filename), 'w') as f:
f.write(content) | def save(self, filename=None) | saves the configuration in the given filename,
if it is none the filename at load time is used.
:param filename: the file name
:type filename: string
:return: | 12.149126 | 11.809415 | 1.028766 |
if start is not None:
data = self.data[start]
return json.dumps(self.data, indent=4) | def json(self, start=None) | :param start: start key in dot notation
returns the dict in json format
:return: json string version
:rtype: string | 4.272286 | 3.598769 | 1.187152 |
try:
config = d = ConfigDict("cloudmesh.yaml")
d = ConfigDict("cloudmesh.yaml")
#
# bug: cloud is none when adding a group
#
config = d["cloudmesh"]["clouds"][cloud]
credentials = config["credentials"]
cl... | def getUser(cls, cloud) | gets the username for a specified cloud.
TODO: works currently only for opensatck.
:param cloud: the name of the cloud
:return: | 6.289949 | 6.089598 | 1.032901 |
path = os.path.dirname(filename)
if not os.path.isdir(path):
Shell.mkdir(path) | def config_dir_setup(filename) | sets the config file and makes sure the directory exists if it has not yet been created.
:param filename:
:return: | 3.283219 | 3.734871 | 0.879072 |
shell = Shell()
print(shell.terminal_type())
r = shell.execute('pwd') # copy line replace
print(r)
# shell.list()
# print json.dumps(shell.command, indent=4)
# test some commands without args
r = shell.execute('ls', ["-l", "-a"])
print(r)
r = shell.execute('ls', ... | def main() | a test that should actually be added into a nosetest
:return: | 5.313158 | 5.418611 | 0.980539 |
python_version = sys.version_info[:3]
v_string = [str(i) for i in python_version]
python_version_s = '.'.join(v_string)
# pip_version = pip.__version__
pip_version = Shell.pip("--version").split()[1]
return python_version_s, pip_version | def get_python(cls) | returns the python and pip version
:return: python version, pip version | 4.090477 | 3.470997 | 1.178473 |
python_version = sys.version_info[:3]
v_string = [str(i) for i in python_version]
if python_version[0] == 2:
python_version_s = '.'.join(v_string)
if (python_version[0] == 2) and (python_version[1] >= 7) and (
python_version[2] >= 9):
... | def check_python(cls) | checks if the python version is supported
:return: True if it is supported | 2.1294 | 2.116966 | 1.005874 |
if platform.system().lower() == "darwin":
command = "/Applications/VirtualBox.app/Contents/MacOS/VBoxManage"
else:
command = 'VBoxManage'
return cls.execute(command, args) | def VBoxManage(cls, *args) | executes VboxManage with the given arguments
:param args:
:return: | 3.193145 | 2.926686 | 1.091044 |
option = '-n' if platform.system().lower() == 'windows' else '-c'
return cls.execute('ping',
"{option} {count} {host}".format(option=option,
count=count,
... | def ping(cls, host=None, count=1) | execute ping
:param host: the host to ping
:param count: the number of pings
:return: | 3.017302 | 3.626808 | 0.831944 |
result = []
for line in lines:
if what not in line:
result = result + [line]
return result | def remove_line_with(cls, lines, what) | returns all lines that do not contain what
:param lines:
:param what:
:return: | 3.205172 | 2.999191 | 1.068679 |
result = []
for line in lines:
if what in line:
result = result + [line]
return result | def find_lines_with(cls, lines, what) | returns all lines that contain what
:param lines:
:param what:
:return: | 3.060723 | 3.016303 | 1.014727 |
exe_paths = glob.glob(cls.cygwin_path + r'\*.exe')
# print cls.cygwin_path
# list all *.exe in cygwin path, use glob
for c in exe_paths:
exe = c.split('\\')
name = exe[1].split('.')[0]
# command['windows'][name] = "{:}\{:}.exe".format(cygwin_... | def find_cygwin_executables(cls) | find the executables in cygwin | 5.493496 | 5.308055 | 1.034936 |
what = sys.platform
kind = 'UNDEFINED_TERMINAL_TYPE'
if 'linux' in what:
kind = 'linux'
elif 'darwin' in what:
kind = 'darwin'
elif 'cygwin' in what:
kind = 'cygwin'
elif 'windows' in what:
kind = 'windows'
... | def terminal_type(cls) | returns darwin, cygwin, cmd, or linux | 2.942514 | 2.401763 | 1.225147 |
# print "--------------"
result = None
terminal = cls.terminal_type()
# print cls.command
os_command = [cmd]
if terminal in ['linux', 'windows']:
os_command = [cmd]
elif 'cygwin' in terminal:
if not cls.command_exists(cmd):
... | def execute(cls,
cmd,
arguments="",
shell=False,
cwd=None,
traceflag=True,
witherror=True) | Run Shell command
:param witherror: if set to False the error will not be printed
:param traceflag: if set to true the trace is printed in case of an error
:param cwd: the current working directory in whcih the command is supposed to be executed.
:param shell: if set to true the subproc... | 2.742003 | 2.823976 | 0.970973 |
directory = path_expand(directory)
try:
os.makedirs(directory)
except OSError as e:
# EEXIST (errno 17) occurs under two conditions when the path exists:
# - it is a file
# - it is a directory
#
# if it is a file, ... | def mkdir(cls, directory) | creates a directory with all its parents in ots name
:param directory: the path of the directory
:return: | 4.20015 | 4.92029 | 0.853639 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.