text
stringlengths 48
9.16k
|
|---|
def test_name(self):
table = AddressTable(self.block_id)
size = 100
step = 3
for i in range(0, size):
if i % step == 0:
table.create("res1", self.block_id + (1000 + i), i)
for i in range(0, size):
id_ = table.get_id('res1', i)
self.assertEqual(id_, self.block_id + (1000 + (i / step) * step))
|
def get_access_token(self, code):
payload = {'client_id': BII_GITHUB_OAUTH_CLIENT_ID,
'client_secret': BII_GITHUB_OAUTH_CLIENT_SECRET,
'code': code}
headers = {'Accept': 'application/json'}
res = requests.post('https://github.com/login/oauth/access_token', params=payload,
headers=headers)
json = res.json()
if "error" in json:
raise BiiException(json["error"])
if json.get("scope", None) != self.scope:
return BiiException(json["Biicode needs your email and login"])
return json["access_token"]
|
def _check_multiple_choice(self):
if self.type == "multiple_choice":
if self.mchoices is None:
raise OptionError(
"must supply a list of mchoices for type '%s'" % self.type, self)
elif type(self.mchoices) not in (tuple, list):
raise OptionError(
"choices must be a list of strings ('%s' supplied)"
% str(type(self.mchoices)).split("'")[1], self)
if self.split_char is None:
self.split_char = ','
elif self.mchoices is not None:
raise OptionError(
"must not supply mchoices for type %r" % self.type, self)
|
def working_on(self, job):
setproctitle('pyres_minion:%s: working on job: %s' % (os.getppid(), job._payload))
self.logger.debug('marking as working on')
data = {
'queue': job._queue,
'run_at': int(time.mktime(datetime.datetime.now().timetuple())),
'payload': job._payload
}
data = json.dumps(data)
self.resq.redis["resque:minion:%s" % str(self)] = data
self.logger.debug("minion:%s" % str(self))
|
def build_sig(type_signature, var_dict=None):
"""
Parse a TypeSignature object and convert it to the internal type system
language.
Args:
type_signature: an instance of TypeSignature
var_dict: a dictionary of already-bound type variables, or None
Returns: A list of TypeVariable/TypeOperator objects, representing the
function type corresponding to the type signature
"""
args = type_signature.args
cons = type_signature.constraints
var_dict = {} if var_dict is None else var_dict
return [build_sig_arg(i, cons, var_dict) for i in args]
|
def test_list_of_dicts_with_missing_to_numpy():
data = [{'name': 'Alice', 'amount': 100},
{'name': 'Bob'},
{'amount': 200}]
result = convert(np.ndarray, data)
assert result.dtype.names == ('amount', 'name')
expected = np.array([(100.0, 'Alice'),
(np.nan, 'Bob'),
(200.0, None)],
dtype=[('amount', 'float64'), ('name', 'O')])
assert np.all((result == expected) |
((result != result) & (expected != expected)))
|
def test_frame_to_redshift(temp_tb):
tb = into(temp_tb, df)
assert into(set, tb) == into(set, df)
|
def process_response(self, request, response):
if request.is_ajax():
if request.REQUEST.get('ajax_redirect_passthrough'):
return response
if type(response) == HttpResponseRedirect:
response.status_code = 278
return response
|
@staticmethod
def repr_or_None(value):
if value is not None:
return "%x" % id(value)
else:
return "None"
|
def set_dict(prop, d):
'''Helper to set values from json recursively'''
for key, value in d.iteritems():
if isinstance(value, dict):
if not prop.__data__.has_key(key) or not isinstance(prop[key], Properties):
prop[key] = Properties()
set_dict(prop[key], value)
else:
prop[key] = value
|
def subpaths(path):
'''List of all recursive parents of `path` in distance order'''
def append_deeper(acc, name):
return acc + [acc[-1] + os.sep + name]
drive, dirs = os.path.splitdrive(path)
dirs = dirs.split(os.sep)
if os.path.isfile(path):
dirs = dirs[:-1]
paths = reduce(append_deeper, dirs, [''])[1:]
paths = [d[1:] if d.startswith(os.sep+os.sep) else d for d in paths]
paths = [drive + d for d in paths]
paths.reverse()
return paths
|
def __init__(self, rev=None, user=UNKNOWN_USER, message=EMPTY_MESSAGE, items=None, changelist=None, time=None):
self.rev = rev
self.user = user
self.message = message
self.items = items if items else []
self.time = time or current_repo_time()
self.changelist = changelist
|
def do_GET(self):
auth = self.headers.getheader('Authorization')
if auth is None:
self.send_response(401)
self.send_header('WWW-Authenticate', 'Basic realm=\"Test\"')
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write('Authentication required')
else:
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.end_headers()
self.wfile.write(auth)
|
def parse_timezone(tz_str):
'''Parse a timezone suffix, as it appears on a date, returning offset in minutes.'''
try:
tz_str = tz_str.lower()
if tz_str[0] in "+-" and len(tz_str) == 5 and tz_str[1:].isdigit():
sign = 1 if (tz_str[0] == "+") else -1
hours = int(tz_str[1:3])
minutes = int(tz_str[3:5])
return -sign * (60 * hours + minutes)
elif tz_str == "gmt" or tz_str == "utc":
return 0
else:
return None
except (ValueError, IndexError):
return None
|
def test_moments(self):
"""
Test the moments of binomial nodes.
"""
# Simple test
X = Binomial(1, 0.7)
u = X._message_to_child()
self.assertEqual(len(u), 1)
self.assertAllClose(u[0],
0.7)
# Test n
X = Binomial(10, 0.7)
u = X._message_to_child()
self.assertAllClose(u[0],
10*0.7)
# Test plates in p
n = np.random.randint(1, 10)
p = np.random.rand(3)
X = Binomial(n, p)
u = X._message_to_child()
self.assertAllClose(u[0],
p*n)
# Test plates in n
n = np.random.randint(1, 10, size=(3,))
p = np.random.rand()
X = Binomial(n, p)
u = X._message_to_child()
self.assertAllClose(u[0],
p*n)
# Test plates in p and n
n = np.random.randint(1, 10, size=(4,1))
p = np.random.rand(3)
X = Binomial(n, p)
u = X._message_to_child()
self.assertAllClose(u[0],
p*n)
# Test with beta prior
P = Beta([7, 3])
logp = P._message_to_child()[0]
p0 = np.exp(logp[0]) / (np.exp(logp[0]) + np.exp(logp[1]))
X = Binomial(1, P)
u = X._message_to_child()
self.assertAllClose(u[0],
p0)
# Test with broadcasted plates
P = Beta([7, 3], plates=(10,))
X = Binomial(5, P)
u = X._message_to_child()
self.assertAllClose(u[0] * np.ones(X.get_shape(0)),
5*p0*np.ones(10))
pass
|
def __repr__(self):
return "<Way id='%s' n_nds=%d n_tags=%d>"%(self.id, len(self.nd_ids), len(self.tags))
|
def ifort_conf(ctx):
import waflib
import os
ctx.env.FC=[]
ctx.load('ifort')
if sys.platform.lower()=="darwin":
ctx.env.LINKFLAGS_fcshlib = ['-dynamiclib']
ctx.env.append_value('FCFLAGS',ctx.env.mopt.split())
ctx.env["FCFLAGS_fc_omp"]=[]
ctx.env.append_value("FCFLAGS_fc_omp","-openmp")
ctx.env.FCSHLIB_MARKER = [""]
ctx.env.FCSTLIB_MARKER = [""]
ctx.check_cc(
errmsg="failed",msg='Compile a test code with ifort',
mandatory=1,fragment = "program test\n WRITE(*,*) 'hello world'\n end program test\n",compile_filename='test.f90',features='fc fcprogram')
if not ctx.options.fortran_flagline:
ctx.start_msg("retrieve ifort link line")
try:
#print "%s %s -dryrun -dynamiclib -shared-intel -no-cxxlib dummy.f90"%(ctx.env.FC," ".join(ctx.env.FCFLAGS))
llgo,llge = ctx.cmd_and_log("%s %s -dryrun -dynamiclib -shared-intel -no-cxxlib dummy.f90"%(ctx.env.FC," ".join(ctx.env.FCFLAGS+ctx.env.FCFLAGS_fc_omp)), output=waflib.Context.BOTH)
#print "RET",llgo,llge
L = set([ll.strip() for ll in re.findall("-L(.+)\s*\\\\", llge.split("ld ")[1]) if ("ifort" in ll.lower()) or ("intel" in ll.lower())])
l = set([ll.strip() for ll in re.findall("-l(.+)\s*\\\\", llge.split("ld ")[1])])
rL = set()
rl = set()
for Li in L:
oli = os.listdir(Li)
for li in l:
if ctx.env.cshlib_PATTERN%li in oli:
rl.add(li)
rL.add(Li)
except:
ctx.end_msg(False)
raise
for pth in list(rL) + ["/lib","/lib64"]:
if osp.exists(pth):
ctx.env.append_value("LIBPATH_fc_runtime",pth)
ctx.env.append_value("RPATH_fc_runtime",pth)
ctx.env.append_value("LIB_fc_runtime",list(rl)+["pthread"])
ctx.end_msg(True)
show_linkline(ctx)
ctx.env.has_ifort = True
|
def add_libdoc_file(self, filename):
'''add all keywords from a libdoc-generated xml file'''
tree = ET.parse(filename)
root = tree.getroot()
if root.tag != "keywordspec":
raise Exception("expect root tag 'keywordspec', got '%s'" % root.tag)
collection_id = self.add_collection(root.get("name"), root.get("type"),
root.get("doc"), root.get("version"),
root.get("scope"), root.get("namedargs"))
for kw in tree.findall("kw"):
kw_name = kw.get("name")
kw_doc = _get_text(kw, "doc")
args_element = kw.find("arguments")
kw_args = []
if args_element is not None:
for arg_element in args_element.findall("arg"):
kw_args.append(arg_element.text)
self._add_keyword(collection_id, kw_name, kw_doc, kw_args)
|
def update(self):
'''
Update the form in background
'''
# get the information
try:
disk_info = self.statistics['Disk']['text']['/']
swap_info = self.statistics['Memory']['text']['swap_memory']
memory_info = self.statistics['Memory']['text']['memory']
processes_info = self.statistics['Process']['text']
system_info = self.statistics['System']['text']
cpu_info = self.statistics['CPU']['graph']
# overview
row1 = "Disk Usage (/) {4}{0: <6}/{1: >6} MB{4}{2: >2} %{5}Processes{4}{3: <8}".format(disk_info["used"],
disk_info["total"],
disk_info["percentage"],
processes_info["running_processes"],
" "*int(4*self.X_SCALING_FACTOR),
" "*int(9*self.X_SCALING_FACTOR))
row2 = "Swap Memory {4}{0: <6}/{1: >6} MB{4}{2: >2} %{5}Threads {4}{3: <8}".format(swap_info["active"],
swap_info["total"],
swap_info["percentage"],
processes_info["running_threads"],
" "*int(4*self.X_SCALING_FACTOR),
" "*int(9*self.X_SCALING_FACTOR))
row3 = "Main Memory {4}{0: <6}/{1: >6} MB{4}{2: >2} %{5}Boot Time{4}{3: <8}".format(memory_info["active"],
memory_info["total"],
memory_info["percentage"],
system_info['running_time'],
" "*int(4*self.X_SCALING_FACTOR),
" "*int(9*self.X_SCALING_FACTOR))
self.basic_stats.value = row1 + '\n' + row2 + '\n' + row3
self.basic_stats.display()
### cpu_usage chart
cpu_canvas = Canvas()
next_peak_height = int(math.ceil((float(cpu_info['percentage'])/100)*self.CHART_HEIGHT))
self.cpu_chart.value = (self.draw_chart(cpu_canvas,next_peak_height,'cpu'))
self.cpu_chart.display()
### memory_usage chart
memory_canvas = Canvas()
next_peak_height = int(math.ceil((float(memory_info['percentage'])/100)*self.CHART_HEIGHT))
self.memory_chart.value = self.draw_chart(memory_canvas,next_peak_height,'memory')
self.memory_chart.display()
### processes_table
processes_table = self.statistics['Process']['table']
# check sorting flags
if MEMORY_SORT:
sorted_table = sorted(processes_table,key=lambda k:k['memory'],reverse=True)
elif TIME_SORT:
sorted_table = sorted(processes_table,key=lambda k:k['rawtime'],reverse=True)
else:
sorted_table = processes_table
# to keep things pre computed
temp_list = []
for proc in sorted_table:
if proc['user'] == system_info['user']:
temp_list.append("{0: <30} {1: >5}{5}{2: <10}{5}{3}{5}{4: >6.2f} % \
".format( (proc['name'][:25] + '...') if len(proc['name']) > 25 else proc['name'],
proc['id'],
proc['user'],
proc['time'],
proc['memory'],
" "*int(5*self.X_SCALING_FACTOR))
)
self.processes_table.entry_widget.values = temp_list
self.processes_table.display()
# catch the fucking KeyError caused to c
# cumbersome point of reading the stats data structures
except KeyError:
pass
|
def filter_instances(instances, filter_dict):
"""Takes a list of instances and returns the subset of that list that meets the filter_dict's criteria."""
filter_function = lambda instance: \
("id" not in filter_dict or filter_dict["id"] == instance.id) and \
("state" not in filter_dict or filter_dict["state"] == instance.state)
return filter(filter_function, instances)
|
def _initialise(self, options=None):
if options is None:
options = OPTION_DEFAULTS
# initialise a few variables
self.filename = None
self._errors = []
self.raise_errors = options['raise_errors']
self.interpolation = options['interpolation']
self.list_values = options['list_values']
self.create_empty = options['create_empty']
self.file_error = options['file_error']
self.stringify = options['stringify']
self.indent_type = options['indent_type']
self.encoding = options['encoding']
self.default_encoding = options['default_encoding']
self.BOM = False
self.newlines = None
self.write_empty_values = options['write_empty_values']
self.unrepr = options['unrepr']
self.initial_comment = []
self.final_comment = []
self.configspec = None
if self._inspec:
self.list_values = False
# Clear section attributes as well
Section._initialise(self)
|
@staticmethod
def work_dirs_for_path(path, require=False):
'''
Given a path, return all enclosing Zinc working directories. This should be
zero or one except in erroneous situations. Returns a list of (work_dir,
rel_path) pairs, where rel_path is the relative path within the working
directory.
'''
out = []
all_dot_dirs = []
# Use absolute path, so we walk all the way up to the root.
abs_path = os.path.abspath(path)
# We will simplify returned paths to be relative, for readability, but only if the input is relative.
return_abs_paths = os.path.isabs(path)
def simplify_path(p):
return p if return_abs_paths else os.path.relpath(p)
for (parent, rel_path) in parent_dirs(abs_path):
dot_dir = WorkingDir._path_for_dot_dir(parent)
if os.path.isdir(dot_dir):
log.debug("found working dir '%s' (with relative path '%s') for path '%s'", parent, rel_path, path)
out.append((simplify_path(parent), rel_path))
all_dot_dirs.append(dot_dir)
if require and not out:
raise InvalidOperation("path is not within a Zinc working directory: %s" % path)
if len(all_dot_dirs) > 1:
log.error("found nested Zinc woking directories, which should never happen: %s", ", ".join(all_dot_dirs))
return out
|
def compute_index(self, st, n):
"""Compute a 1D array representing the axis index.
Parameters
----------
st : tuple
A tuple of ``(scale, translate)`` parameters.
n : int
The number of bins along the dimension.
Returns
-------
index : ndarray
"""
px = np.arange(n)
s, t = st
return self.inverse_mapper((px - t)/s)
|
def __init__(self, master_url, main_executable=None):
self._master_url = master_url
self._main_executable = main_executable or Configuration['main_executable_path']
self._logger = get_logger(__name__)
|
def _listen(self, cmd, *args):
self.event_id += 1
for listener in self.listeners:
listener.listen(self.event_id, cmd, args)
if cmd == "pid":
# our signal that a new test is starting
self.reset()
self.set_running_state()
if cmd == "ready":
self.set_running_state()
if cmd == "log_message":
attrs = args[0]
if attrs["level"] == "DEBUG":
if attrs["message"].strip().startswith(":break:"):
# this is a signal from the 'breakpoint' keyword
self.remote_port = attrs["message"].split(":")[2]
self.log.debug("remote host=%s port=%s" % (self.remote_host, self.remote_port))
self.set_break_state()
self.proxy("ready")
self.refresh_vars()
elif attrs["message"].strip() == ":continue:":
self.set_running_state()
if cmd in ("start_test", "start_suite", "start_keyword"):
name = args[0]
cmd_type = cmd.split("_")[1]
self.stack.append((cmd_type, name))
self.update_display()
elif cmd in ("end_test", "end_suite", "end_keyword"):
cmd_type = cmd.split("_")[1]
self.stack.pop()
self.update_display()
elif cmd == "close":
self.set_idle_state()
|
def get_host_id(host):
if host in host_name_to_uid:
return host_name_to_uid[host]
else:
return str(uuid.uuid4())
|
def add_row(self, row):
if len(row) != len(self.field_names):
raise ValueError('row has incorrect number of values '
'({0} given, {1} expected)'
.format(len(row), len(self.field_names)))
self._rows.append(_filter_row_values(row, self.__empty))
|
@register.tag
def get_unread_message_count_for(parser, token):
"""
Returns the unread message count for a user.
Syntax::
{% get_unread_message_count_for [user] as [var_name] %}
Example usage::
{% get_unread_message_count_for pero as message_count %}
"""
try:
tag_name, arg = token.contents.split(None, 1)
except ValueError:
raise template.TemplateSyntaxError("%s tag requires arguments" % token.contents.split()[0])
m = re.search(r'(.*?) as (\w+)', arg)
if not m:
raise template.TemplateSyntaxError("%s tag had invalid arguments" % tag_name)
user, var_name = m.groups()
return MessageCount(user, var_name)
|
def async_run(self, port, master_url, num_executors, log_level, eventlog_file):
"""
Run a ClusterRunner slave service.
:param port: the port on which to run the slave service
:type port: int | None
:param master_url: the url of the master to which this slave should attach
:type master_url: string | None
:param num_executors: the number of executors the slave service should use
:type num_executors: int | None
:param log_level: the log level at which to do application logging (or None for default log level)
:type log_level: str | None
:param eventlog_file: an optional alternate file in which to write event logs
:type eventlog_file: str | None
"""
num_executors = num_executors or Configuration['num_executors']
master_url = master_url or '{}:{}'.format(Configuration['master_hostname'], Configuration['master_port'])
port = port or Configuration['port']
log_level = log_level or Configuration['log_level']
eventlog_file = eventlog_file or Configuration['eventlog_file']
log.configure_logging(log_level=log_level, log_file=Configuration['log_file'].format(port))
analytics.initialize(eventlog_file)
analytics.record_event(analytics.SERVICE_STARTED, service='slave')
cluster_slave = ClusterSlave(
port=port,
num_executors=num_executors,
host=Configuration['hostname'],
)
application = ClusterSlaveApplication(cluster_slave)
ioloop = self._start_application(application, port)
self._write_pid_file(Configuration['slave_pid_file'])
# connect to master once tornado ioloop is running
connect_slave_to_master = functools.partial(cluster_slave.connect_to_master, master_url=master_url)
ioloop.add_callback(connect_slave_to_master)
ioloop.start() # this call blocks until the server is stopped
ioloop.close(all_fds=True) # all_fds=True is necessary here to make sure connections don't hang
self._logger.notice('Slave server was stopped.')
|
def parse_sat_output(stdout):
"""Parse a solver's standard competition-format output."""
match = re.search(r"^s +(.+)$", stdout, re.M)
if match:
(answer_type,) = map(str.upper, match.groups())
if answer_type == "SATISFIABLE":
answer = []
for line in re.findall(r"^v ([ \-0-9]*)$", stdout, re.M):
answer.extend(map(int, line.split()))
if answer[-1] == 0:
return answer[:-1]
elif answer_type == "UNSATISFIABLE":
return False
return None
|
def currentAbove(requestContext, seriesList, n):
"""
Takes one metric or a wildcard seriesList followed by an integer N.
Out of all metrics passed, draws only the metrics whose value is above N
at the end of the time period specified.
Example::
&target=currentAbove(server*.instance*.threads.busy,50)
Draws the servers with more than 50 busy threads.
"""
return [series for series in seriesList if safeLast(series) >= n]
|
def remote_create_folder(dst_ssh, dst_path):
"""Create folder remotely by using ssh
:param dst_ssh: str -- user name and host name of destination path
just like: user@host
:param dst_path: str -- destination path
:return: None
"""
dst_command = "\"mkdir -p {}\"".format(dst_path)
command = "ssh " + dst_ssh + " " + dst_command
os.popen(command)
|
def get_code_dir(self):
#Rationale for the default code directory location:
# PEP 3147
# http://www.python.org/dev/peps/pep-3147/
#
# Which standardizes the __pycache__ directory as a place to put
# compilation artifacts for python programs
source_dir, source_file = os.path.split(inspect.getsourcefile(self.fn))
candidate = os.path.join(source_dir, '__pycache__', source_file, self.__name__)
if os.path.exists(candidate):
return candidate
try:
os.makedirs(candidate)
return candidate
except OSError:
#Fallback!
#Can't create a directory where the source file lives
#(Maybe the source file is in a system directory)
#Let's put it in a tempdir which we know will be writable
candidate = os.path.join(tempfile.gettempdir(),
'copperhead-cache-uid%s' % os.getuid(),
source_file, self.__name__)
if os.path.exists(candidate):
return candidate
#No check here to ensure this succeeds - fatal error if it fails
os.makedirs(candidate)
return candidate
|
def test30_10(self):
"""Tests the RS(30,10) code"""
coder = rs.RSCoder(30,10)
m = "Hello, wor"
code = coder.encode(m)
self.assertTrue( coder.verify(code) )
self.assertEqual(m, coder.decode(code) )
self.assertEqual(30, len(code))
# Change 10 bytes. This code should tolerate up to 10 bytes changed
changes = [0, 1, 2, 4, 7,
10, 14, 18, 22, 27]
c = list(ord(x) for x in code)
for pos in changes:
c[pos] = (c[pos] + 50) % 255
c = "".join(chr(x) for x in c)
decode = coder.decode(c)
self.assertEqual(m, decode)
|
def _fetchWithBootstrap(requestContext, seriesList, **delta_kwargs):
"""
Request the same data but with a bootstrap period at the beginning.
"""
from .app import evaluateTarget, pathsFromTarget
bootstrapContext = requestContext.copy()
bootstrapContext['startTime'] = (
requestContext['startTime'] - timedelta(**delta_kwargs))
bootstrapContext['endTime'] = requestContext['startTime']
bootstrapList = []
# Get all paths to fetch
paths = []
for series in seriesList:
if series.pathExpression in [b.pathExpression for b in bootstrapList]:
continue
paths.extend(pathsFromTarget(series.pathExpression))
# Fetch all paths
data_store = fetchData(bootstrapContext, paths)
for series in seriesList:
if series.pathExpression in [b.pathExpression for b in bootstrapList]:
# This pathExpression returns multiple series and we already
# fetched it
continue
bootstraps = evaluateTarget(bootstrapContext,
series.pathExpression,
data_store)
found = dict(((s.name, s) for s in bootstraps))
for s in seriesList:
if s.name not in found:
# bootstrap interval too large for the range available in
# storage. Fill with nulls.
start = epoch(bootstrapContext['startTime'])
end = epoch(bootstrapContext['endTime'])
delta = (end - start) % s.step
values = [None] * delta
found[s.name] = TimeSeries(s.name, start, end, s.step, values)
found[s.name].pathExpression = s.pathExpression
bootstrapList.append(found[s.name])
newSeriesList = []
for bootstrap, original in zip_longest(bootstrapList, seriesList):
newValues = []
if bootstrap.step != original.step:
ratio = bootstrap.step / original.step
for value in bootstrap:
# XXX For series with aggregationMethod = sum this should also
# divide by the ratio to bring counts to the same time unit
# ...but we have no way of knowing whether that's the case
newValues.extend([value] * ratio)
else:
newValues.extend(bootstrap)
newValues.extend(original)
newSeries = TimeSeries(original.name, bootstrap.start, original.end,
original.step, newValues)
newSeries.pathExpression = series.pathExpression
newSeriesList.append(newSeries)
return newSeriesList
|
def insertion_sort(seq):
''' sort a sequence using the insertion sort alg '''
for i in range(1, len(seq)):
j = i
while j > 0 and seq[j-1] > seq[j]:
seq[j-1], seq[j] = seq[j], seq[j-1]
j -= 1
return seq
|
def stream_decode_gzip(iterator):
"""Stream decodes a gzip-encoded iterator"""
try:
dec = zlib.decompressobj(16 + zlib.MAX_WBITS)
for chunk in iterator:
rv = dec.decompress(chunk)
if rv:
yield rv
buf = dec.decompress('')
rv = buf + dec.flush()
if rv:
yield rv
except zlib.error:
pass
|
def ada_predict(self, X=[]):
''' adaboost predicting '''
if X == None: return
X = np.array(X)
N, d = X.shape
Y = np.zeros(N)
score = []
# T iterations
for t in range(self.T):
weak_learner = self.weak_classifier_ens[t]
Y += self.alpha[t]*weak_learner.stump_predict(X)
score.append(np.sign(Y))
return score
|
def _emit_test():
"write out a test"
if test_name is None:
return
subunit.write("test %s\n" % test_name)
if not log:
subunit.write("%s %s\n" % (result, test_name))
else:
subunit.write("%s %s [\n" % (result, test_name))
if log:
for line in log:
subunit.write("%s\n" % line)
subunit.write("]\n")
del log[:]
|
def heartbeat(queue_name, task_id, owner, message, index):
"""Sets the heartbeat status of the task and extends its lease.
The task's lease is extended by the same amount as its last lease to
ensure that any operations following the heartbeat will still hold the
lock for the original lock period.
Args:
queue_name: Name of the queue the work item is on.
task_id: ID of the task that is finished.
owner: Who or what has the current lease on the task.
message: Message to report as the task's current status.
index: Number of this message in the sequence of messages from the
current task owner, starting at zero. This lets the API receive
heartbeats out of order, yet ensure that the most recent message
is actually saved to the database. This requires the owner issuing
heartbeat messages to issue heartbeat indexes sequentially.
Returns:
True if the heartbeat message was set, False if it is lower than the
current heartbeat index.
Raises:
TaskDoesNotExistError if the task does not exist.
LeaseExpiredError if the lease is no longer active.
NotOwnerError if the specified owner no longer owns the task.
"""
task = _get_task_with_policy(queue_name, task_id, owner)
if task.heartbeat_number > index:
return False
task.heartbeat = message
task.heartbeat_number = index
# Extend the lease by the time of the last lease.
now = datetime.datetime.utcnow()
timeout_delta = task.eta - task.last_lease
task.eta = now + timeout_delta
task.last_lease = now
db.session.add(task)
signals.task_updated.send(app, task=task)
return True
|
@staticmethod
def _char_to_base(chr_int, target_base):
if chr_int == 0:
return [0]
return MarkovKeyState._char_to_base(chr_int / target_base, target_base) + [chr_int % target_base]
|
def is_override_notify_default(self):
"""Returns True if NTDS Connection should override notify default
"""
if self.options & dsdb.NTDSCONN_OPT_OVERRIDE_NOTIFY_DEFAULT == 0:
return False
return True
|
def is_column(k, columns):
sanitized = strip_suffixes(k, ['__lt', '__gt', '__lte', '__gte'])
if sanitized in columns:
return True
else:
return False
|
def _iter_data(self, data):
for tupl in self.iter_data(data):
if len(tupl) != 2:
raise Exception(
'The iter_data method must yield pair tuples containing '
'the node and its body (empty if not available)')
yield tupl
|
def test_people(self):
self.cl.stub_request("clients/%s/people.json" % self.cl.client_id, "people.json")
people = self.cl.people()
self.assertEquals(2, len(people))
self.assertEquals('person1@blackhole.com', people[0].EmailAddress)
self.assertEquals('Person One', people[0].Name)
self.assertEquals('Active', people[0].Status)
|
def run(self, address, credopts=None, sambaopts=None, versionopts=None):
lp = sambaopts.get_loadparm()
try:
res = netcmd_get_domain_infos_via_cldap(lp, None, address)
except RuntimeError:
raise CommandError("Invalid IP address '" + address + "'!")
self.outf.write("Forest : %s\n" % res.forest)
self.outf.write("Domain : %s\n" % res.dns_domain)
self.outf.write("Netbios domain : %s\n" % res.domain_name)
self.outf.write("DC name : %s\n" % res.pdc_dns_name)
self.outf.write("DC netbios name : %s\n" % res.pdc_name)
self.outf.write("Server site : %s\n" % res.server_site)
self.outf.write("Client site : %s\n" % res.client_site)
|
def get_db_prep_value(self, value, connection=None, prepared=False):
"""
Pickle and b64encode the object, optionally compressing it.
The pickling protocol is specified explicitly (by default 2),
rather than as -1 or HIGHEST_PROTOCOL, because we don't want the
protocol to change over time. If it did, ``exact`` and ``in``
lookups would likely fail, since pickle would now be generating
a different string.
"""
if value is not None and not isinstance(value, PickledObject):
# We call force_unicode here explicitly, so that the encoded string
# isn't rejected by the postgresql_psycopg2 backend. Alternatively,
# we could have just registered PickledObject with the psycopg
# marshaller (telling it to store it like it would a string), but
# since both of these methods result in the same value being stored,
# doing things this way is much easier.
value = force_unicode(dbsafe_encode(value, self.compress, self.protocol))
return value
|
def build_results(self):
self.header("Scraping election candidates")
url = urlparse.urljoin(
self.base_url,
'/Campaign/Candidates/list.aspx?view=certified&electNav=93'
)
soup = self.get(url)
# Get all the links out
links = soup.findAll('a', href=re.compile(r'^.*&electNav=\d+'))
# Drop the link that says "prior elections" because it's a duplicate
links = [
l for l in links
if l.find_next_sibling('span').text != 'Prior Elections'
]
# Loop through the links...
results = []
for i, link in enumerate(links):
# .. go and get each page and its data
url = urlparse.urljoin(self.base_url, link["href"])
data = self.scrape_page(url)
# Parse out the name and year
data['raw_name'] = link.find_next_sibling('span').text.strip()
data['election_type'] = self.parse_election_name(data['raw_name'])
data['year'] = int(data['raw_name'][:4])
# The index value is used to preserve sorting of elections,
# since multiple elections may occur in a year.
# BeautifulSoup goes from top to bottom,
# but the top most election is the most recent so it should
# have the highest id.
data['sort_index'] = len(links) - i
# Add it to the list
results.append(data)
# Take a rest
sleep(0.5)
# Pass out the data
return results
|
def test_render_upload_template_filter_options(self):
tpl = template.Template('{% load adminfiles_tags %}'
'{{ img|render_upload:"alt=blah" }}')
html = tpl.render(template.Context({'img': self.animage}))
self.assertTrue('alt="blah"' in html)
|
def test_doesnt_contain_python_attr(self):
self.assertFalse('PUBLISHED' in self.STATUS)
|
def test_bounces(self):
min_date = "2010-01-01"
self.campaign.stub_request("campaigns/%s/bounces.json?date=%s&orderfield=date&page=1&pagesize=1000&orderdirection=asc" % (self.campaign_id, urllib.quote(min_date, '')), "campaign_bounces.json")
bounces = self.campaign.bounces(min_date)
self.assertEquals(len(bounces.Results), 2)
self.assertEquals(bounces.Results[0].EmailAddress, "asdf@softbouncemyemail.com")
self.assertEquals(bounces.Results[0].ListID, "654523a5855b4a440bae3fb295641546")
self.assertEquals(bounces.Results[0].BounceType, "Soft")
self.assertEquals(bounces.Results[0].Date, "2010-07-02 16:46:00")
self.assertEquals(bounces.Results[0].Reason, "Bounce - But No Email Address Returned ")
self.assertEquals(bounces.ResultsOrderedBy, "date")
self.assertEquals(bounces.OrderDirection, "asc")
self.assertEquals(bounces.PageNumber, 1)
self.assertEquals(bounces.PageSize, 1000)
self.assertEquals(bounces.RecordsOnThisPage, 2)
self.assertEquals(bounces.TotalNumberOfRecords, 2)
self.assertEquals(bounces.NumberOfPages, 1)
|
def test_404(self):
resp = self.app.get('/nope', follow_redirects=True)
assert 'Page Not Found' in resp.data
|
def _format(self,*a):
a = (self.element,)+a
self.commands.append('LV%s.%s(%s);'%a)
|
def draw(self, gl=pyglet.gl):
if not self.relative:
gl.glLoadIdentity()
if self.position is not None:
px, py = self.position
gl.glTranslatef(px, py, 0)
if self.angle is not None:
gl.glRotatef(self.angle, 0, 0, 1)
if self.zoom is not None:
sx, sy = self.zoom
gl.glScalef(sx, sy ,0)
|
@property
def is_bound(self):
"""Flag set if the channel is bound."""
return self._is_bound and self._channel is not None
|
@with_in_memory_connection
def test_gather(self, conn):
def collect_replies():
yield 1
yield 2
yield 3
ticket = uuid()
actor = Actor(conn)
actor._collect_replies = Mock(return_value=collect_replies())
ares = AsyncResult(ticket, actor)
ares.to_python = Mock()
all = ares.gather()
list(all)
actor._collect_replies.assert_caleld_once_with(conn, ANY, ticket)
self.assertEqual(ares.to_python.call_count,
len(list(collect_replies())))
# test that the to_python is applied to all results
actor._collect_replies.reset_mock()
actor._collect_replies = Mock(return_value=collect_replies())
prev_to_python = ares.to_python
new_to_python = lambda x, propagate = True: 'called_%s' % x
ares.to_python = new_to_python
all = ares.gather()
vals = list(all)
expected_vals = [new_to_python(i) for i in collect_replies()]
actor._collect_replies.assert_caleld_once_with(conn, ANY, ticket)
self.assertEqual(vals, expected_vals)
ares.to_python = prev_to_python
|
@register.simple_tag
def clear_search_url(request):
getvars = request.GET.copy()
if 'search' in getvars:
del getvars['search']
if len(getvars.keys()) > 0:
return "%s?%s" % (request.path, getvars.urlencode())
else:
return request.path
|
def test_disk_activate_help(self, capsys):
with pytest.raises(SystemExit):
self.parser.parse_args('disk activate --help'.split())
out, err = capsys.readouterr()
assert 'usage: ceph-deploy disk activate' in out
|
def on_stderr_received(self, data):
"""
:type data: encoded bytes
"""
data, self.intermediate_stderr_buffer = self._split(self.intermediate_stderr_buffer + data)
self.stderr_buffer += data
self.interleaved_buffer += data
self.log_file.write(data)
# Get the decoded Python str
decoded_data = self._decode(data)
# Emit event with decoded Python str
self.event_queue.put(ExecutionEvent(
'STDERR',
job_id=self.job_id,
stage_label=self.stage_label,
data=decoded_data))
|
def relative_svg_path_to_absolute_coord_list(path, bezier_steps=100, segment_length=0.05):
"""
return a list of absolute coordinates from an SVG *relative* path
"""
# get SVG path grammar
look_for = svg_grammar()
# parse the input based on this grammar
pd = look_for.parseString(path)
# absolute position
ap = Point()
# path origin
po = Point()
points = []
p = []
last_bezier_control_point = Point()
for i in range(0, len(pd)):
cmd = pd[i][0]
# 'move to' command
if re.match('m', cmd):
if i == 0:
coord = Point(pd[i][1][0], pd[i][1][1])
ap.assign(coord.x, coord.y)
p.append(ap)
po.assign(coord.x, coord.y)
else:
coord_tmp = Point(pd[i][1][0], pd[i][1][1])
ap += coord_tmp
# a marker that a new path is starting after a previous one closed
points.append(p)
p = []
p.append(ap)
po = ap
for coord_tmp in pd[i][2:]:
coord = Point(coord_tmp[0], coord_tmp[1])
ap += coord
p.append(ap)
# cubic (two control points) Bezier curve command
elif re.match('c', cmd):
bezier_curve_path = []
for n in range(1, len(pd[i])-1, 3):
bezier_curve_path.append(ap)
for m in range(0, 3):
coord = pd[i][n+m]
point = Point(coord[0], coord[1])
bezier_curve_path.append(ap + point)
new_point = Point(pd[i][n+m][0], pd[i][n+m][1])
ap += new_point
for n in range(0, len(bezier_curve_path), 4):
# clear bezier point arrays
bezier_points_x = []
bezier_points_y = []
# split points of bezier into 'x' and 'y' coordinate arrays
# as this is what the point array function expects
for m in range(0, 4):
bezier_points_x.append(bezier_curve_path[n+m].x)
bezier_points_y.append(bezier_curve_path[n+m].y)
# caluclate the individual points along the bezier curve for 'x'
# and 'y'
points_x = calculate_points_of_cubic_bezier(bezier_points_x, bezier_steps)
points_y = calculate_points_of_cubic_bezier(bezier_points_y, bezier_steps)
path_length = calculate_cubic_bezier_length(points_x, points_y)
if path_length == 0:
steps = 1
else:
steps = ceil(path_length / segment_length)
skip = int(ceil(bezier_steps / steps))
bezier_point_array = []
# put thos points back into a Point type array
for n in range(0, len(points_x), skip):
bezier_point_array.append(Point(points_x[n], points_y[n]))
bezier_point_array.append(Point(points_x[len(points_x)-1], points_y[len(points_x)-1]))
p += bezier_point_array
# quadratic (single control point) Bezier curve command
elif re.match('q', cmd):
bezier_curve_path = []
for n in range(1, len(pd[i])-1, 2):
bezier_curve_path.append(ap)
for m in range(0, 2):
coord = pd[i][n+m]
point = Point(coord[0], coord[1])
bezier_curve_path.append(ap + point)
# inject a second, identical control point so this quadratic
# bezier looks like a cubic one
if m == 1:
bezier_curve_path.append(ap+point)
if m == 0:
last_bezier_control_point = ap + point
new_point = Point(pd[i][n+m][0], pd[i][n+m][1])
ap += new_point
for n in range(0, len(bezier_curve_path), 4):
# clear bezier point arrays
bezier_points_x = []
bezier_points_y = []
# split points of bezier into 'x' and 'y' coordinate arrays
# as this is what the point array function expects
for m in range(0, 4):
bezier_points_x.append(bezier_curve_path[n+m].x)
bezier_points_y.append(bezier_curve_path[n+m].y)
# caluclate the individual points along the bezier curve for 'x'
# and 'y'
points_x = calculate_points_of_cubic_bezier(bezier_points_x, bezier_steps)
points_y = calculate_points_of_cubic_bezier(bezier_points_y, bezier_steps)
path_length = calculate_cubic_bezier_length(points_x, points_y)
skip = int(ceil(bezier_steps / (path_length / segment_length)))
bezier_point_array = []
# put those points back into a Point type array
for n in range(0, len(points_x), skip):
bezier_point_array.append(Point(points_x[n], points_y[n]))
bezier_point_array.append(Point(points_x[len(points_x)-1], points_y[len(points_x)-1]))
p += bezier_point_array
# simple cubic Bezier curve command
elif re.match('t', cmd):
bezier_curve_path = []
for n in range(1, len(pd[i])):
bezier_curve_path.append(ap)
coord = pd[i][n]
point = Point(coord[0], coord[1])
end_point = ap + point
diff = Point(ap.x - last_bezier_control_point.x, ap.y - last_bezier_control_point.y)
control_point = ap + diff
bezier_curve_path.append(control_point)
bezier_curve_path.append(end_point)
bezier_curve_path.append(end_point)
last_bezier_control_point = control_point
new_point = Point(pd[i][n][0], pd[i][n][1])
ap += new_point
for n in range(0, len(bezier_curve_path), 4):
# clear bezier point arrays
bezier_points_x = []
bezier_points_y = []
# split points of bezier into 'x' and 'y' coordinate arrays
# as this is what the point array function expects
for m in range(0, 4):
bezier_points_x.append(bezier_curve_path[n+m].x)
bezier_points_y.append(bezier_curve_path[n+m].y)
# caluclate the individual points along the bezier curve for 'x'
# and 'y'
points_x = calculate_points_of_cubic_bezier(bezier_points_x, bezier_steps)
points_y = calculate_points_of_cubic_bezier(bezier_points_y, bezier_steps)
path_length = calculate_cubic_bezier_length(points_x, points_y)
skip = int(ceil(bezier_steps / (path_length / segment_length)))
bezier_point_array = []
# put those points back into a Point type array
for m in range(0, len(points_x), skip):
bezier_point_array.append(Point(points_x[m], points_y[m]))
bezier_point_array.append(Point(points_x[len(points_x)-1], points_y[len(points_x)-1]))
p += bezier_point_array
# elif re.match('s', cmd):
# pass
# 'line to' command
elif re.match('l', cmd):
for coord_tmp in pd[i][1:]:
coord = Point(coord_tmp[0], coord_tmp[1])
ap += coord
p.append(ap)
# 'horizontal line' command
elif re.match('h', cmd):
for coord_tmp in pd[i][1:]:
coord = Point(coord_tmp[0], 0)
ap += coord
p.append(ap)
# 'vertical line' command
elif re.match('v', cmd):
for coord_tmp in pd[i][1:]:
coord = Point(0, coord_tmp[0])
ap += coord
p.append(ap)
# 'close shape' command
elif re.match('z', cmd):
ap = ap + (po - ap)
else:
print("ERROR: found an unsupported SVG path command "+ str(cmd))
points.append(p)
return points
|
def test_remove_entity(self):
from grease import World, Entity
world = World()
comp1 = world.components.one = TestComponent()
comp2 = world.components.two = TestComponent()
comp3 = world.components.three = TestComponent()
entity = Entity(world)
comp1.add(entity)
comp2.add(entity)
self.assertTrue(entity in world.entities)
self.assertTrue(entity in comp1)
self.assertTrue(entity in comp2)
self.assertFalse(entity in comp3)
world.entities.remove(entity)
self.assertFalse(entity in world.entities)
self.assertFalse(entity in comp1)
self.assertFalse(entity in comp2)
self.assertFalse(entity in comp3)
self.assertRaises(KeyError, world.entities.remove, entity)
|
def disable_insecure_serializers(allowed=['json']):
"""Disable untrusted serializers.
Will disable all serializers except ``json``
or you can specify a list of deserializers to allow.
.. note::
Producers will still be able to serialize data
in these formats, but consumers will not accept
incoming data using the untrusted content types.
"""
for name in registry._decoders:
registry.disable(name)
if allowed is not None:
for name in allowed:
registry.enable(name)
|
def __call__(self, request):
url = self.creds.get('opsmgr').get('url') + '/uaa/oauth/token'
username = self.creds.get('opsmgr').get('username')
password = self.creds.get('opsmgr').get('password')
headers = { 'Accept': 'application/json' }
data = {
'grant_type': 'password',
'client_id': 'opsman',
'client_secret': '',
'username': username,
'password': password,
'response_type': 'token',
}
response = requests.post(url, data=data, verify=False, headers=headers)
if response.status_code != requests.codes.ok:
return requests.auth.HTTPBasicAuth(username, password)(request)
response = response.json()
access_token = response.get('access_token')
token_type = response.get('token_type')
request.headers['Authorization'] = token_type + ' ' + access_token
return request
|
def test_isdir_on_non_existing_directory():
assert fs.isdir(os.path.join(TEST_DIR, "foo")) is False
|
def s3_has_uptodate_file(bucket, transfer_file, s3_key_name):
"""Check if S3 has an existing, up to date version of this file.
"""
s3_key = bucket.get_key(s3_key_name)
if s3_key:
s3_size = s3_key.size
local_size = os.path.getsize(transfer_file)
s3_time = rfc822.mktime_tz(rfc822.parsedate_tz(s3_key.last_modified))
local_time = os.path.getmtime(transfer_file)
return s3_size == local_size and s3_time >= local_time
return False
|
def create_mds(distro, name, cluster, init):
conn = distro.conn
path = '/var/lib/ceph/mds/{cluster}-{name}'.format(
cluster=cluster,
name=name
)
conn.remote_module.safe_mkdir(path)
bootstrap_keyring = '/var/lib/ceph/bootstrap-mds/{cluster}.keyring'.format(
cluster=cluster
)
keypath = os.path.join(path, 'keyring')
stdout, stderr, returncode = remoto.process.check(
conn,
[
'ceph',
'--cluster', cluster,
'--name', 'client.bootstrap-mds',
'--keyring', bootstrap_keyring,
'auth', 'get-or-create', 'mds.{name}'.format(name=name),
'osd', 'allow rwx',
'mds', 'allow',
'mon', 'allow profile mds',
'-o',
os.path.join(keypath),
]
)
if returncode > 0 and returncode != errno.EACCES:
for line in stderr:
conn.logger.error(line)
for line in stdout:
# yes stdout as err because this is an error
conn.logger.error(line)
conn.logger.error('exit code from command was: %s' % returncode)
raise RuntimeError('could not create mds')
remoto.process.check(
conn,
[
'ceph',
'--cluster', cluster,
'--name', 'client.bootstrap-mds',
'--keyring', bootstrap_keyring,
'auth', 'get-or-create', 'mds.{name}'.format(name=name),
'osd', 'allow *',
'mds', 'allow',
'mon', 'allow rwx',
'-o',
os.path.join(keypath),
]
)
conn.remote_module.touch_file(os.path.join(path, 'done'))
conn.remote_module.touch_file(os.path.join(path, init))
if init == 'upstart':
remoto.process.run(
conn,
[
'initctl',
'emit',
'ceph-mds',
'cluster={cluster}'.format(cluster=cluster),
'id={name}'.format(name=name),
],
timeout=7
)
elif init == 'sysvinit':
remoto.process.run(
conn,
[
'service',
'ceph',
'start',
'mds.{name}'.format(name=name),
],
timeout=7
)
if distro.is_el:
system.enable_service(distro.conn)
elif init == 'systemd':
remoto.process.run(
conn,
[
'systemctl',
'enable',
'ceph-mds@{name}'.format(name=name),
],
timeout=7
)
remoto.process.run(
conn,
[
'systemctl',
'start',
'ceph-mds@{name}'.format(name=name),
],
timeout=7
)
remoto.process.run(
conn,
[
'systemctl',
'enable',
'ceph.target',
],
timeout=7
)
|
def release(self, jid, priority=DEFAULT_PRIORITY, delay=0):
"""Release a reserved job back into the ready queue."""
self._interact('release %d %d %d\r\n' % (jid, priority, delay),
['RELEASED', 'BURIED'],
['NOT_FOUND'])
|
@classmethod
def from_doc(cls, doc):
"""
Convert a dictionary (from to_doc) back to its
native ObjectModel type
@param doc: dict
"""
params = {}
for _key, _value in doc.items():
if _key == '_metadata':
continue
elif _value:
params[_key] = TargetingCriterion.from_doc(_value)
return cls(**params)
|
@_if_not_installed("macs14")
def install_macs(env):
"""Model-based Analysis for ChIP-Seq.
http://liulab.dfci.harvard.edu/MACS/
"""
default_version = "1.4.2"
version = env.get("tool_version", default_version)
url = "https://github.com/downloads/taoliu/MACS/" \
"MACS-%s.tar.gz" % version
_get_install(url, env, _python_make)
|
def pop(self, count=1):
if len(self.segments) < 1 + count:
raise Exception('Cannot pop() from path')
newSegments = [segment.copy() for segment in self.segments[:-count]]
return TFSPath(self.closed, *newSegments)
|
def get_priority(self, level):
"""Naive implementation - does not consider duplicate priority levels"""
for k, el in enumerate(self.items):
if el['priority'] == level:
return self.items.pop(k)
return None
|
def keygen(get_keyring=get_keyring):
"""Generate a public/private key pair."""
WheelKeys, keyring = get_keyring()
ed25519ll = signatures.get_ed25519ll()
wk = WheelKeys().load()
keypair = ed25519ll.crypto_sign_keypair()
vk = native(urlsafe_b64encode(keypair.vk))
sk = native(urlsafe_b64encode(keypair.sk))
kr = keyring.get_keyring()
kr.set_password("wheel", vk, sk)
sys.stdout.write("Created Ed25519 keypair with vk={0}\n".format(vk))
if isinstance(kr, keyring.backends.file.BaseKeyring):
sys.stdout.write("in {0}\n".format(kr.file_path))
else:
sys.stdout.write("in %r\n" % kr.__class__)
sk2 = kr.get_password('wheel', vk)
if sk2 != sk:
raise WheelError("Keyring is broken. Could not retrieve secret key.")
sys.stdout.write("Trusting {0} to sign and verify all packages.\n".format(vk))
wk.add_signer('+', vk)
wk.trust('+', vk)
wk.save()
|
@wraps(func)
def inner(*args, **kwargs):
x, y = args
assert type(x) == type(y) and x is not y
return func(*args, **kwargs)
|
def make_padded_chars(words, seperator=' '):
"""Call `_make_padding_char` on a list of words.
For example, to create a new format string to pad a list of values.
(e.g. {:<3} {<:6} {<:9}"""
fmt_string = ''
for word in words:
fmt_string += _make_padded_char(word) + seperator
return fmt_string
|
def _check_eq(self, other, not_equal_func):
if type(self) != type(other):
return not_equal_func(other, "types", type(self), type(other))
for attr in self.attrs():
name = attr.name
if (getattr(self, name) != getattr(other, name)):
return not_equal_func(other, "{!r} attribute".format(name), getattr(self, name),
getattr(other, name))
return True
|
def __eq__(self, other):
return self.person['lname'] == other.person['lname']
|
def getReward(self):
# -1 reward for falling over
# 0.01 reward for close to goal
# return reward inversely proportional to heading error otherwise
r_factor = 0.0001
if np.abs(self.env.getTilt()) > self.max_tilt:
return -1.0
else:
temp = self.calc_dist_to_goal()
heading = self.calc_angle_to_goal()
if (temp < 1e-3):
return 0.01
else:
return (0.95 - heading**2) * r_factor
|
def _fetch(self):
"""
Internal helper that fetches the ring from Redis, including only active
nodes/replicas. Returns a list of tuples (start, replica) (see
_fetch_all docs for more details).
"""
now = time.time()
expiry_time = now - NODE_TIMEOUT
data = self.conn.zrangebyscore(self.key, expiry_time, 'INF')
ring = []
for node_data in data:
start, replica = node_data.split(':', 1)
ring.append((int(start), replica))
ring = sorted(ring, key=operator.itemgetter(0))
return ring
|
def _encode_key_name(key):
key = bytes(key, "utf8")
key_len = len(key)
pbuf, pend, buf = _create_buffer(key_len + 2)
librtmp.AMF_EncodeInt16(pbuf, pend, key_len)
buf[2:key_len + 2] = key
return buf[:]
|
def __init__(self, key, secret=None, secure=True, host=None, path=None, port=None):
super(EucNodeDriver, self).__init__(key, secret, secure, host, port)
if path is None:
path = "/services/Eucalyptus"
self.path = path
|
def find_by_value(self, value):
return self.find_by_xpath('//*[@value="%s"]' % value, original_find="value", original_selector=value)
|
def generate_itemSimOnTypeSet():
prefs = {}
result = {}
try:
with open(os.getcwd() + '//ml-100k' + '/u.item') as item:
for line in item:
typeVector = line.split('|')[5:24]
itemId = line.split('|')[0]
prefs[itemId] = typeVector
result.setdefault(itemId, {})
except IOError as err:
print('File error: ' + str(err))
#print similarity.sim_itemType(prefs['1677'],prefs['1678'],19)
for key1, value1 in prefs.items():
for key2, value2 in prefs.items():
if key1 != key2:
s = similarity.sim_itemType(value1, value2, 19)
print
key1, key2, s
result[key1][key2] = s
dumpPickle(result, '/itemSimOnType.pkl')
|
def Analysis():
test_size = 1000
X, y = Build_Data_Set()
print(len(X))
clf = svm.SVC(kernel="linear", C=1.0)
clf.fit(X[:-test_size],y[:-test_size]) # train data
correct_count = 0
for x in range(1, test_size+1):
if clf.predict(X[-x])[0] == y[-x]:
correct_count += 1
print("correct_count=%s"%float(correct_count))
print("test_size=%s"%float(test_size))
# on OS X with 64-bit python 2.7.6 had to add float(), otherwise result was zero:
print("Accuracy:", (float(correct_count) / float(test_size)) * 100.00)
|
def recv_heartbeat(self, from_uid, proposal_id):
if proposal_id > self.leader_proposal_id:
# Change of leadership
self._acquiring = False
old_leader_uid = self.leader_uid
self.leader_uid = from_uid
self.leader_proposal_id = proposal_id
if self.leader and from_uid != self.node_uid:
self.leader = False
self.messenger.on_leadership_lost()
self.observe_proposal( from_uid, proposal_id )
self.messenger.on_leadership_change( old_leader_uid, from_uid )
if self.leader_proposal_id == proposal_id:
self._tlast_hb = self.timestamp()
|
def request(self, host, handler, request_body, verbose=0):
self.verbose = 0
method = ET.XML(request_body).find('methodName').text
mock = SoftLayerMockHttp(host, 80)
mock.request('POST', "%s/%s" % (handler, method))
resp = mock.getresponse()
return self._parse_response(resp.body, None)
|
def _parse_version_parts(s):
parts = []
for part in _split_version_components(s):
part = _replace_p(part,part)
if not part or part=='.':
continue
if part[:1] in '0123456789':
parts.append(zfill(part,8)) # pad for numeric comparison
else:
parts.append('*'+part)
parts.append('*final') # ensure that alpha/beta/candidate are before final
return parts
|
def testNextMonthPlusOneWeek10am(self):
self.assertEqual(self.today +
relativedelta(months=+1, weeks=+1, hour=10),
datetime(2003, 10, 24, 10, 0))
|
def make_key(self, *parts):
"""Generate a namespaced key for the given path."""
separator = getattr(self.model_class, 'index_separator', '.')
parts = map(decode, parts)
return '%s%s' % (self._base_key, separator.join(map(str, parts)))
|
def _SetEntryFormDate(self, year, month, day, id=-1):
"""Set the data on the entry form."""
if self._RefuseUnsavedModifications():
return False
date = self._MakeDateTime(year, month, day)
self.cal.SetDate(date)
firstid = self.entries.get_first_id(year, month, day)
if id == -1:
id = firstid
self.entry_form_key = TKEntryKey(year, month, day, id)
label = date.Format("%A, %B %d, %Y")
if firstid is not None and (id is None or id > firstid):
label += " (%d)" % self.entries.get_id_pos(year, month, day, id)
self.frame.FindWindowById(self.prev_id).Enable(True)
else:
self.frame.FindWindowById(self.prev_id).Enable(False)
if id is not None:
self.frame.FindWindowById(self.next_id).Enable(True)
else:
self.frame.FindWindowById(self.next_id).Enable(False)
self.frame.FindWindowById(self.date_id).SetLabel(label)
text = subject = author = tags = ''
entry = self.entries.get_entry(year, month, day, id)
if entry is not None:
text = entry.get_text()
author = entry.get_author()
subject = entry.get_subject()
tags = ', '.join(entry.get_tags() or [])
self.frame.FindWindowById(self.author_id).SetValue(author)
self.frame.FindWindowById(self.subject_id).SetValue(subject)
self.frame.FindWindowById(self.text_id).SetValue(text)
self.frame.FindWindowById(self.tags_id).SetValue(tags)
self._NotifyEntryLoaded(entry and True or False)
|
def prepare_query(self):
clone = self.query.clone()
select = []
joined = set()
def ensure_join(query, m, p):
if m not in joined:
if '__' not in p:
next_model = query.model_class
else:
next, _ = p.rsplit('__', 1)
next_model = self.alias_to_model[next]
query = ensure_join(query, next_model, next)
joined.add(m)
return query.switch(next_model).join(m)
else:
return query
for lookup in self.fields:
# lookup may be something like "content" or "user__user_name"
if '__' in lookup:
path, column = lookup.rsplit('__', 1)
model = self.alias_to_model[path]
clone = ensure_join(clone, model, path)
else:
model = self.query.model_class
column = lookup
field = model._meta.fields[column]
select.append(field)
clone._select = select
return clone
|
def get_page(self):
curr_page = request.args.get(self.page_var)
if curr_page and curr_page.isdigit():
return int(curr_page)
return 1
|
def key_table(keys):
return TABLE(
TR(TD(B(T('Key'))), TD(B(T('Time in Cache (h:m:s)')))),
*[TR(TD(k[0]), TD('%02d:%02d:%02d' % k[1])) for k in keys],
**dict(_class='cache-keys',
_style="border-collapse: separate; border-spacing: .5em;"))
|
def test_number_to_string(self):
''' Numbers are turned into strings.
'''
cleaner = Cleaners()
in_int = 85
in_float = 82.12
in_string = "big frame, small spirit!"
in_list = ["hands", "by", "the", "halyards"]
in_none = None
assert cleaner.number_to_string(in_int) == str(in_int)
assert cleaner.number_to_string(in_float) == str(in_float)
assert cleaner.number_to_string(in_string) == in_string
assert cleaner.number_to_string(in_list) == in_list
assert cleaner.number_to_string(in_none) is None
|
def close(self):
if self.fp is not None:
libc.fclose(self.fp)
self.fp = None
super(SecureStringPipe,self).close()
|
def get_tm_time_id_column(column, schema_name):
name = 'tm_%s_id' % column.name
populates = 'label.time.second.of.day.%s' % column.schemaReference
return {'populates': [populates], 'columnName': name, 'mode': 'FULL', 'referenceKey': 1}
|
def do_test( dump_vcd, delay, ModelType ):
# Test messages
test_msgs = [
0x0000,
0x0a0a,
0x0b0b,
0x0c0c,
0x0d0d,
0xf0f0,
0xe0e0,
0xd0d0,
]
# Instantiate and elaborate the model
model = ModelType( 16, test_msgs, delay )
model.vcd_file = dump_vcd
model.elaborate()
# Create a simulator using the simulation tool
sim = SimulationTool( model )
# Run the simulation
print()
sim.reset()
while not model.done() and sim.ncycles < 1000:
sim.print_line_trace()
sim.cycle()
assert model.done()
# Add a couple extra ticks so that the VCD dump is nicer
sim.cycle()
sim.cycle()
sim.cycle()
|
def filter_stop_words(self, words):
"""Remove any stop-words from the collection of words."""
return [w for w in words if w not in self._stopwords]
|
def translate( model, o=sys.stdout, enable_blackbox=False, verilator_xinit='zeros' ):
# List of models to translate
translation_queue = collections.OrderedDict()
# FIXME: Additional source to append to end of translation
append_queue = []
# Utility function to recursively collect all submodels in design
def collect_all_models( m ):
# Add the model to the queue
translation_queue[ m.class_name ] = m
for subm in m.get_submodules():
collect_all_models( subm )
# Collect all submodels in design and translate them
collect_all_models( model )
for k, v in translation_queue.items():
if isinstance( v, verilog.VerilogModel ):
x = verilog.import_module( v, o )
if x not in append_queue:
append_queue.append( x )
else:
translate_module( v, o, enable_blackbox, verilator_xinit )
# Append source code for imported modules and dependecies
verilog.import_sources( append_queue, o )
|
def xtick( s ):
if s.out.rdy and s.out.val:
s.data.popleft()
if len( s.data ) != 0:
s.out.msg.next = s.data[0]
s.out.val.next = ( len( s.data ) != 0 )
|
def __init__( s, nbits, nports=3 ):
assert nports == 3
s.in_ = [ InPort( nbits ) for x in range( nports ) ]
s.out = OutPort( nbits )
s.sel = InPort ( clog2( nbits ) )
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.