_id stringlengths 2 7 | title stringlengths 1 88 | partition stringclasses 3
values | text stringlengths 31 13.1k | language stringclasses 1
value | meta_information dict |
|---|---|---|---|---|---|
q13100 | get_is_property | train | def get_is_property(value, is_bytes=False):
"""Get shortcut for `SC` or `Binary` property."""
if value.startswith('^'):
prefix = value[1:3]
temp = value[3:]
negate = '^'
else:
prefix = value[:2]
temp = value[2:]
negate = ''
if prefix != 'is':
raise ValueError("Does not | python | {
"resource": ""
} |
q13101 | get_in_property | train | def get_in_property(value, is_bytes=False):
"""Get shortcut for `Block` property."""
if value.startswith('^'):
prefix = value[1:3]
temp = value[3:]
negate = '^'
else:
prefix = value[:2]
temp = value[2:]
negate = ''
if | python | {
"resource": ""
} |
q13102 | get_unicode_property | train | def get_unicode_property(value, prop=None, is_bytes=False):
"""Retrieve the Unicode category from the table."""
if prop is not None:
prop = unidata.unicode_alias['_'].get(prop, prop)
try:
if prop == 'generalcategory':
return get_gc_property(value, is_bytes)
elif prop == 'script':
return get_script_property(value, is_bytes)
elif prop == 'scriptextensions':
return get_script_extension_property(value, is_bytes)
elif prop == 'block':
return get_block_property(value, is_bytes)
elif prop == 'binary':
return get_binary_property(value, is_bytes)
elif prop == 'bidiclass':
return get_bidi_property(value, is_bytes)
elif prop == 'bidipairedbrackettype':
return get_bidi_paired_bracket_type_property(value, is_bytes)
elif prop == 'age':
return get_age_property(value, is_bytes)
elif prop == 'eastasianwidth':
return get_east_asian_width_property(value, is_bytes)
elif PY35 and prop == 'indicpositionalcategory':
return get_indic_positional_category_property(value, is_bytes)
elif not PY35 and prop == 'indicmatracategory':
return get_indic_positional_category_property(value, is_bytes)
elif prop == 'indicsyllabiccategory':
return get_indic_syllabic_category_property(value, is_bytes)
elif prop == 'hangulsyllabletype':
return get_hangul_syllable_type_property(value, is_bytes)
elif prop == 'decompositiontype':
return get_decomposition_type_property(value, is_bytes)
elif prop == 'canonicalcombiningclass':
return get_canonical_combining_class_property(value, is_bytes)
elif prop == 'numerictype':
return get_numeric_type_property(value, is_bytes)
elif prop == 'numericvalue':
return get_numeric_value_property(value, is_bytes)
elif prop == 'joiningtype':
return get_joining_type_property(value, is_bytes)
elif prop == 'joininggroup':
return get_joining_group_property(value, is_bytes)
elif prop == 'graphemeclusterbreak':
return get_grapheme_cluster_break_property(value, is_bytes)
elif prop == 'linebreak':
return get_line_break_property(value, is_bytes)
| python | {
"resource": ""
} |
q13103 | compile_replace | train | def compile_replace(pattern, repl, flags=0):
"""Construct a method that can be used as a replace method for `sub`, `subn`, etc."""
call = None
if pattern is not None and isinstance(pattern, _RE_TYPE):
if isinstance(repl, (str, bytes)):
if not (pattern.flags & DEBUG):
call = _cached_replace_compile(pattern, repl, flags, type(repl))
else: # pragma: no cover
call = _bre_parse._ReplaceParser().parse(pattern, repl, bool(flags & FORMAT))
elif isinstance(repl, ReplaceTemplate):
if flags:
raise ValueError("Cannot process flags argument | python | {
"resource": ""
} |
q13104 | findall | train | def findall(pattern, string, *args, **kwargs):
"""Apply `findall` after applying backrefs."""
| python | {
"resource": ""
} |
q13105 | sub | train | def sub(pattern, repl, string, *args, **kwargs):
"""Apply `sub` after applying backrefs."""
flags = args[4] if len(args) > 4 else kwargs.get('flags', 0)
is_replace = _is_replace(repl)
is_string = isinstance(repl, (str, bytes)) | python | {
"resource": ""
} |
q13106 | get_version | train | def get_version():
""" Extract the version number from the code. """
here = os.path.abspath(os.path.dirname(__file__))
jbxapi_file = os.path.join(here, "jbxapi.py")
| python | {
"resource": ""
} |
q13107 | JoeSandbox.analysis_list | train | def analysis_list(self):
"""
Fetch a list of all analyses.
"""
| python | {
"resource": ""
} |
q13108 | JoeSandbox.submit_sample | train | def submit_sample(self, sample, cookbook=None, params={}, _extra_params={}):
"""
Submit a sample and returns the submission id.
Parameters:
sample: The sample to submit. Needs to be a file-like object or a tuple in
the shape (filename, file-like object).
cookbook: Uploads a cookbook together with the sample. Needs to be a file-like object or a
tuple in the shape (filename, file-like object)
params: Customize the sandbox parameters. They are described in more detail
in the default submission parameters.
Example:
import jbxapi
joe = jbxapi.JoeSandbox()
with open("sample.exe", "rb") as f:
joe.submit_sample(f, params={"systems": ["w7"]})
Example:
import io, jbxapi
| python | {
"resource": ""
} |
q13109 | JoeSandbox.submit_sample_url | train | def submit_sample_url(self, url, params={}, _extra_params={}):
"""
Submit a sample at a given URL for analysis.
| python | {
"resource": ""
} |
q13110 | JoeSandbox.submit_url | train | def submit_url(self, url, params={}, _extra_params={}):
"""
Submit a website for analysis.
"""
self._check_user_parameters(params)
params | python | {
"resource": ""
} |
q13111 | JoeSandbox.submit_cookbook | train | def submit_cookbook(self, cookbook, params={}, _extra_params={}):
"""
Submit a cookbook.
"""
self._check_user_parameters(params)
| python | {
"resource": ""
} |
q13112 | JoeSandbox.submission_delete | train | def submission_delete(self, submission_id):
"""
Delete a submission.
"""
response = self._post(self.apiurl + '/v2/submission/delete', | python | {
"resource": ""
} |
q13113 | JoeSandbox.server_online | train | def server_online(self):
"""
Returns True if the Joe Sandbox servers are running or False if they are in maintenance mode.
"""
| python | {
"resource": ""
} |
q13114 | JoeSandbox.analysis_info | train | def analysis_info(self, webid):
"""
Show the status and most important attributes of an analysis.
"""
response = | python | {
"resource": ""
} |
q13115 | JoeSandbox.analysis_download | train | def analysis_download(self, webid, type, run=None, file=None):
"""
Download a resource for an analysis. E.g. the full report, binaries, screenshots.
The full list of resources can be found in our API documentation.
When `file` is given, the return value is the filename specified by the server,
otherwise it's a tuple of (filename, bytes).
Parameters:
webid: the webid of the analysis
type: the report type, e.g. 'html', 'bins'
run: specify the run. If it is None, let Joe Sandbox pick one
file: a writeable file-like object (When obmitted, the method returns
the data as a bytes object.)
Example:
json_report, name = joe.analysis_download(123456, 'jsonfixed')
Example:
with open("full_report.html", "wb") as f:
name = joe.analysis_download(123456, "html", file=f)
"""
# when no file is specified, we create our own
if file is None:
_file = io.BytesIO()
else:
_file = file
data = {
'apikey': self.apikey,
'webid': webid,
| python | {
"resource": ""
} |
q13116 | JoeSandbox.analysis_search | train | def analysis_search(self, query):
"""
Lists the webids of the analyses that match the given query.
Searches in MD5, SHA1, SHA256, filename, cookbook name, comment, url and report id.
"""
| python | {
"resource": ""
} |
q13117 | JoeSandbox.server_systems | train | def server_systems(self):
"""
Retrieve a list of available systems.
"""
response = | python | {
"resource": ""
} |
q13118 | JoeSandbox.account_info | train | def account_info(self):
"""
Only available on Joe Sandbox Cloud
Show information about the account.
"""
| python | {
"resource": ""
} |
q13119 | JoeSandbox.server_info | train | def server_info(self):
"""
Query information about the server.
"""
response = self._post(self.apiurl | python | {
"resource": ""
} |
q13120 | JoeSandbox.server_lia_countries | train | def server_lia_countries(self):
"""
Show the available localized internet anonymization countries.
"""
response | python | {
"resource": ""
} |
q13121 | JoeSandbox.server_languages_and_locales | train | def server_languages_and_locales(self):
"""
Show the available languages and locales
"""
| python | {
"resource": ""
} |
q13122 | JoeSandbox._post | train | def _post(self, url, data=None, **kwargs):
"""
Wrapper around requests.post which
(a) always inserts a timeout
(b) converts errors to ConnectionError
(c) re-tries a few times
(d) converts file names to ASCII
"""
# Remove non-ASCII characters from filenames due to a limitation of the combination of
# urllib3 (via python-requests) and our server
# https://github.com/requests/requests/issues/2117
# Internal Ticket #3090
if "files" in kwargs and kwargs["files"] is not None:
acceptable_chars = "0123456789" + "abcdefghijklmnopqrstuvwxyz" + \
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" + " _-.,()[]{}"
for param_name, fp in kwargs["files"].items():
if isinstance(fp, (tuple, list)):
filename, fp = fp
else:
filename = requests.utils.guess_filename(fp) or param_name
def encode(char):
try:
if char in acceptable_chars:
return char
except UnicodeDecodeError:
pass
return "x{:02x}".format(ord(char))
filename = "".join(encode(x) for x in filename)
| python | {
"resource": ""
} |
q13123 | JoeSandbox._check_user_parameters | train | def _check_user_parameters(self, user_parameters):
"""
Verifies that the parameter dict given by the user only contains
known keys. This ensures that the user detects typos faster.
"""
if not user_parameters:
return
| python | {
"resource": ""
} |
q13124 | JoeSandbox._raise_or_extract | train | def _raise_or_extract(self, response):
"""
Raises an exception if the response indicates an API error.
Otherwise returns the object at the 'data' key of the API response.
"""
try:
data = response.json()
except ValueError:
raise JoeException("The server responded with | python | {
"resource": ""
} |
q13125 | LinuxDistribution._parse_os_release_content | train | def _parse_os_release_content(lines):
"""
Parse the lines of an os-release file.
Parameters:
* lines: Iterable through the lines in the os-release file.
Each line must be a unicode string or a UTF-8 encoded byte
string.
Returns:
A dictionary containing all information items.
"""
props = {}
lexer = shlex.shlex(lines, posix=True)
lexer.whitespace_split = True
# The shlex module defines its `wordchars` variable using literals,
# making it dependent on the encoding of the Python source file.
# In Python 2.6 and 2.7, the shlex source file is encoded in
# 'iso-8859-1', and the `wordchars` variable is defined as a byte
# string. This causes a UnicodeDecodeError to be raised when the
# parsed content is a unicode object. The following fix resolves that
# (... but it should be fixed in shlex...):
if sys.version_info[0] == 2 and isinstance(lexer.wordchars, bytes):
lexer.wordchars = lexer.wordchars.decode('iso-8859-1')
tokens = list(lexer)
for token in tokens:
# At this point, all shell-like parsing has been done (i.e.
# comments processed, quotes and backslash escape sequences
# processed, multi-line values assembled, trailing newlines
# stripped, etc.), so the tokens are now either:
# * variable assignments: var=value
# * commands or their arguments (not allowed in os-release)
if '=' in token:
k, v = token.split('=', 1)
if isinstance(v, bytes):
v = v.decode('utf-8')
props[k.lower()] = v
else:
# Ignore any tokens that are not variable assignments
pass
if 'version_codename' in props:
| python | {
"resource": ""
} |
q13126 | pretty_print | train | def pretty_print(n):
"""Pretty print function for very big integers"""
if type(n) != int:
return n
ret = []
n = str(n)
for i in range(len(n) - 1, -1, -1):
ret.append(n[i])
if (len(n) - i) % 3 == | python | {
"resource": ""
} |
q13127 | A10DeviceInstancePlugin.create_a10_device_instance | train | def create_a10_device_instance(self, context, a10_device_instance):
"""Attempt to create instance using neutron context"""
LOG.debug("A10DeviceInstancePlugin.create(): a10_device_instance=%s", a10_device_instance)
config = a10_config.A10Config()
vthunder_defaults = config.get_vthunder_config()
imgr = instance_manager.InstanceManager.from_config(config, context)
dev_instance = common_resources.remove_attributes_not_specified(
a10_device_instance.get(resources.RESOURCE))
# Create the instance with specified defaults.
vthunder_config = vthunder_defaults.copy()
vthunder_config.update(_convert(dev_instance, _API, _VTHUNDER_CONFIG))
instance = imgr.create_device_instance(vthunder_config, dev_instance.get("name"))
db_record = {}
db_record.update(_convert(vthunder_config, _VTHUNDER_CONFIG, _DB))
| python | {
"resource": ""
} |
q13128 | VipHandler.vport_meta | train | def vport_meta(self, vip):
"""Get the vport meta, no matter which name was used"""
vport_meta = self.meta(vip, 'vport', None)
if vport_meta is None:
| python | {
"resource": ""
} |
q13129 | apply_template | train | def apply_template(template, *args, **kw):
"""Applies every callable in any Mapping or Iterable"""
if six.callable(template):
return template(*args, **kw)
if isinstance(template, six.string_types):
| python | {
"resource": ""
} |
q13130 | initialize_vthunder | train | def initialize_vthunder(a10_cfg, device_cfg, client):
"""Perform initialization of system-wide settings"""
vth = a10_cfg.get_vthunder_config()
initialize_interfaces(vth, device_cfg, client)
| python | {
"resource": ""
} |
q13131 | magic_session | train | def magic_session(db_session=None, url=None):
"""Either does nothing with the session you already have or
makes one that commits and closes no matter what happens
"""
if db_session is not None:
yield db_session
else:
session = get_session(url, expire_on_commit=False)
| python | {
"resource": ""
} |
q13132 | InstanceManager._plumb_port | train | def _plumb_port(self, server, network_id, wrong_ips):
"""Look for an existing port on the network
Add one if it doesn't exist
"""
for attached_interface in server.interface_list():
if attached_interface.net_id == network_id:
if any(map(lambda x: x['ip_address'] | python | {
"resource": ""
} |
q13133 | NeutronDbWrapper.allocate_ip_for_subnet | train | def allocate_ip_for_subnet(self, subnet_id, mac, port_id):
"""Allocates an IP from the specified subnet and creates a port"""
# Get an available IP and mark it as used before someone else does
# If there's no IP, , log it and return an error
# If we successfully get an IP, create a port with the specified MAC and device data
| python | {
"resource": ""
} |
q13134 | NeutronDbWrapper.a10_allocate_ip_from_dhcp_range | train | def a10_allocate_ip_from_dhcp_range(self, subnet, interface_id, mac, port_id):
"""Search for an available IP.addr from unallocated nmodels.IPAllocationPool range.
If no addresses are available then an error is raised. Returns the address as a string.
This search is conducted by a difference of the nmodels.IPAllocationPool set_a
and the current IP allocations.
"""
subnet_id = subnet["id"]
network_id = subnet["network_id"]
iprange_result = self.get_ipallocationpool_by_subnet_id(subnet_id)
ip_in_use_list = [x.ip_address for x in self.get_ipallocations_by_subnet_id(subnet_id)]
range_begin, range_end = iprange_result.first_ip, iprange_result.last_ip
ip_address = IPHelpers.find_unused_ip(range_begin, range_end, ip_in_use_list)
if not ip_address:
msg = "Cannot allocate from subnet {0}".format(subnet) | python | {
"resource": ""
} |
q13135 | HealthMonitorHandler._dissociate | train | def _dissociate(self, c, context, hm, pool_id):
"""Remove a pool association"""
pool_name = self._pool_name(context, pool_id)
| python | {
"resource": ""
} |
q13136 | HealthMonitorHandler.dissociate | train | def dissociate(self, c, context, hm, pool_id):
"""Remove a pool association, and the healthmonitor if its the last one"""
| python | {
"resource": ""
} |
q13137 | HealthMonitorHandler._delete | train | def _delete(self, c, context, hm):
"""Delete a healthmonitor and ALL its pool associations"""
pools = hm.get("pools", [])
for pool in pools:
pool_id = pool.get("pool_id")
| python | {
"resource": ""
} |
q13138 | Bartlett1932.add_node_to_network | train | def add_node_to_network(self, node, network):
"""Add node to the chain and receive transmissions."""
network.add_node(node)
parents = node.neighbors(direction="from")
| python | {
"resource": ""
} |
q13139 | Bartlett1932.recruit | train | def recruit(self):
"""Recruit one participant at a time until all networks are full."""
if self.networks(full=False):
| python | {
"resource": ""
} |
q13140 | Source.create_information | train | def create_information(self):
"""Create new infos on demand."""
info | python | {
"resource": ""
} |
q13141 | RogersExperiment.recruit | train | def recruit(self):
"""Recruit participants if necessary."""
num_approved = len(Participant.query.filter_by(status="approved").all())
end_of_generation = num_approved % self.generation_size == 0
complete = num_approved >= (self.generations * self.generation_size)
if complete:
self.log("All networks full: closing recruitment", "-----")
| python | {
"resource": ""
} |
q13142 | size_on_copy | train | def size_on_copy(root="."):
"""Return the size of the experiment directory in bytes, excluding any
files and directories which would be excluded on copy.
"""
total_size = 0
exclusions = exclusion_policy()
for dirpath, dirnames, filenames in os.walk(root, topdown=True):
current_exclusions = exclusions(dirpath, os.listdir(dirpath))
| python | {
"resource": ""
} |
q13143 | setup_experiment | train | def setup_experiment(log, debug=True, verbose=False, app=None, exp_config=None):
"""Checks the experiment's python dependencies, then prepares a temp directory
with files merged from the custom experiment and Dallinger.
The resulting directory includes all the files necessary to deploy to
Heroku.
"""
# Verify that the Postgres server is running.
try:
db.check_connection()
except Exception:
log("There was a problem connecting to the Postgres database!")
raise
# Check that the demo-specific requirements are satisfied.
try:
with open("requirements.txt", "r") as f:
dependencies = [r for r in f.readlines() if r[:3] != "-e "]
except (OSError, IOError): | python | {
"resource": ""
} |
q13144 | HerokuLocalDeployment.notify | train | def notify(self, message):
"""Callback function which checks lines of output, tries to match
against regex defined in subclass's "dispatch" dict, and passes through
to a handler on match.
"""
for regex, handler in self.dispatch.items(): | python | {
"resource": ""
} |
q13145 | DebugDeployment.recruitment_closed | train | def recruitment_closed(self, match):
"""Recruitment is closed.
Start a thread to check the experiment summary.
| python | {
"resource": ""
} |
q13146 | DebugDeployment.check_status | train | def check_status(self):
"""Check the output of the summary route until
the experiment is complete, then we can stop monitoring Heroku
subprocess output.
"""
self.out.log("Recruitment is complete. Waiting for experiment completion...")
base_url = get_base_url()
status_url = base_url + "/summary"
while not self.complete:
time.sleep(10)
try:
resp = requests.get(status_url)
exp_data = resp.json()
| python | {
"resource": ""
} |
q13147 | DebugDeployment.notify | train | def notify(self, message):
"""Monitor output from heroku process.
This overrides the base class's `notify`
to make sure that we stop if the status-monitoring thread
has determined that the experiment is complete.
"""
| python | {
"resource": ""
} |
q13148 | StandaloneServer.load | train | def load(self):
"""Return our application to be run."""
app = util.import_app("dallinger.experiment_server.sockets:app")
if | python | {
"resource": ""
} |
q13149 | AnimalInfo.perturbed_contents | train | def perturbed_contents(self):
"""Perturb the given animal."""
animal = json.loads(self.contents)
for prop, prop_range in self.properties.items():
| python | {
"resource": ""
} |
q13150 | FullyConnected.add_node | train | def add_node(self, node):
"""Add a node, connecting it to everyone and back."""
other_nodes = [n for n in self.nodes() if n.id != node.id]
for n in other_nodes:
if isinstance(n, Source):
| python | {
"resource": ""
} |
q13151 | Empty.add_source | train | def add_source(self, source):
"""Connect the source to all existing other nodes.""" | python | {
"resource": ""
} |
q13152 | Star.add_node | train | def add_node(self, node):
"""Add a node and connect it to the center."""
nodes = self.nodes()
if len(nodes) > 1:
| python | {
"resource": ""
} |
q13153 | DiscreteGenerational.add_node | train | def add_node(self, node):
"""Link to the agent from a parent based on the parent's fitness"""
num_agents = len(self.nodes(type=Agent))
curr_generation = int((num_agents - 1) / float(self.generation_size))
node.generation = curr_generation
if curr_generation == 0 and self.initial_source:
parent = self._select_oldest_source()
| python | {
"resource": ""
} |
q13154 | CoordinationChatroom.create_network | train | def create_network(self):
"""Create a new network by reading the configuration file."""
| python | {
"resource": ""
} |
q13155 | CoordinationChatroom.info_post_request | train | def info_post_request(self, node, info):
"""Run when a request to create an | python | {
"resource": ""
} |
q13156 | nocache | train | def nocache(func):
"""Stop caching for pages wrapped in nocache decorator."""
def new_func(*args, **kwargs):
"""No cache Wrapper."""
resp = make_response(func(*args, **kwargs)) | python | {
"resource": ""
} |
q13157 | ValidatesBrowser.exclusions | train | def exclusions(self):
"""Return list of browser exclusion rules defined in the Configuration.
"""
exclusion_rules = [
r.strip()
| python | {
"resource": ""
} |
q13158 | ValidatesBrowser.is_supported | train | def is_supported(self, user_agent_string):
"""Check user agent against configured exclusions.
"""
user_agent_obj = user_agents.parse(user_agent_string)
browser_ok = True
for rule in self.exclusions:
if rule in ["mobile", "tablet", "touchcapable", "pc", "bot"]:
if (
(rule == "mobile" and user_agent_obj.is_mobile)
or (rule == "tablet" and user_agent_obj.is_tablet)
or (rule == "touchcapable" and user_agent_obj.is_touch_capable)
| python | {
"resource": ""
} |
q13159 | MCMCP.create_node | train | def create_node(self, network, participant):
"""Create a node for a participant."""
| python | {
"resource": ""
} |
q13160 | MCMCP.data_check | train | def data_check(self, participant):
"""Make sure each trial contains exactly one chosen info."""
infos = participant.infos()
| python | {
"resource": ""
} |
q13161 | find_experiment_export | train | def find_experiment_export(app_id):
"""Attempt to find a zipped export of an experiment with the ID provided
and return its path. Returns None if not found.
Search order:
1. local "data" subdirectory
2. user S3 bucket
3. Dallinger S3 bucket
"""
# Check locally first
cwd = os.getcwd()
data_filename = "{}-data.zip".format(app_id)
path_to_data = os.path.join(cwd, "data", data_filename)
if os.path.exists(path_to_data):
try:
Data(path_to_data)
except IOError:
from dallinger import logger
logger.exception(
"Error reading local data file {}, checking remote.".format(
path_to_data
)
)
else: | python | {
"resource": ""
} |
q13162 | load | train | def load(app_id):
"""Load the data from wherever it is found."""
path_to_data = find_experiment_export(app_id)
| python | {
"resource": ""
} |
q13163 | dump_database | train | def dump_database(id):
"""Dump the database to a temporary directory."""
tmp_dir = tempfile.mkdtemp()
current_dir = os.getcwd()
os.chdir(tmp_dir)
FNULL = open(os.devnull, "w")
heroku_app = HerokuApp(dallinger_uid=id, output=FNULL)
heroku_app.backup_capture()
heroku_app.backup_download()
for | python | {
"resource": ""
} |
q13164 | backup | train | def backup(id):
"""Backup the database to S3."""
filename = dump_database(id)
key = "{}.dump".format(id)
bucket = | python | {
"resource": ""
} |
q13165 | register | train | def register(id, url=None):
"""Register a UUID key in the global S3 bucket."""
bucket = registration_s3_bucket()
key = registration_key(id)
| python | {
"resource": ""
} |
q13166 | is_registered | train | def is_registered(id):
"""Check if a UUID is already registered"""
bucket = registration_s3_bucket()
key = registration_key(id)
| python | {
"resource": ""
} |
q13167 | copy_heroku_to_local | train | def copy_heroku_to_local(id):
"""Copy a Heroku database locally."""
heroku_app = HerokuApp(dallinger_uid=id)
try:
| python | {
"resource": ""
} |
q13168 | copy_db_to_csv | train | def copy_db_to_csv(dsn, path, scrub_pii=False):
"""Copy a local database to a set of CSV files."""
if "postgresql://" in dsn or "postgres://" in dsn:
conn = psycopg2.connect(dsn=dsn)
else:
conn = psycopg2.connect(database=dsn, user="dallinger")
cur = conn.cursor()
for table in table_names:
csv_path = os.path.join(path, "{}.csv".format(table))
| python | {
"resource": ""
} |
q13169 | _scrub_participant_table | train | def _scrub_participant_table(path_to_data):
"""Scrub PII from the given participant table."""
path = os.path.join(path_to_data, "participant.csv")
with open_for_csv(path, "r") as input, open("{}.0".format(path), "w") as output:
reader = csv.reader(input)
writer = csv.writer(output)
headers = next(reader)
writer.writerow(headers)
for i, row in enumerate(reader):
| python | {
"resource": ""
} |
q13170 | export | train | def export(id, local=False, scrub_pii=False):
"""Export data from an experiment."""
print("Preparing to export the data...")
if local:
db_uri = db.db_url
else:
db_uri = HerokuApp(id).db_uri
# Create the data package if it doesn't already exist.
subdata_path = os.path.join("data", id, "data")
try:
os.makedirs(subdata_path)
except OSError as e:
if e.errno != errno.EEXIST or not os.path.isdir(subdata_path):
raise
# Copy in the data.
copy_db_to_csv(db_uri, subdata_path, scrub_pii=scrub_pii)
# Copy the experiment code into a code/ subdirectory.
try:
| python | {
"resource": ""
} |
q13171 | ingest_to_model | train | def ingest_to_model(file, model, engine=None):
"""Load data from a CSV file handle into storage for a
SQLAlchemy model class.
"""
if engine is None:
engine = db.engine
reader = csv.reader(file)
columns = tuple('"{}"'.format(n) for n | python | {
"resource": ""
} |
q13172 | _get_or_create_s3_bucket | train | def _get_or_create_s3_bucket(s3, name):
"""Get an S3 bucket resource after making sure it exists"""
exists = True
try:
s3.meta.client.head_bucket(Bucket=name)
except botocore.exceptions.ClientError | python | {
"resource": ""
} |
q13173 | user_s3_bucket | train | def user_s3_bucket(canonical_user_id=None):
"""Get the user's S3 bucket."""
s3 = _s3_resource()
if not canonical_user_id:
canonical_user_id = _get_canonical_aws_user_id(s3)
s3_bucket_name = "dallinger-{}".format(
| python | {
"resource": ""
} |
q13174 | _s3_resource | train | def _s3_resource(dallinger_region=False):
"""A boto3 S3 resource using the AWS keys in the config."""
config = get_config()
if not config.ready:
config.load()
region | python | {
"resource": ""
} |
q13175 | transmit_by_fitness | train | def transmit_by_fitness(from_whom, to_whom=None, what=None):
"""Choose a parent with probability proportional to their fitness."""
parents = from_whom
parent_fs = [p.fitness for p in parents]
parent_probs | python | {
"resource": ""
} |
q13176 | get_messenger | train | def get_messenger(config):
"""Return an appropriate Messenger.
If we're in debug mode, or email settings aren't set, return a debug
version which logs the message instead of attempting to send a real
email.
"""
email_settings = EmailConfig(config)
if config.get("mode") == "debug":
return DebugMessenger(email_settings)
problems = email_settings.validate()
if | python | {
"resource": ""
} |
q13177 | EmailConfig.validate | train | def validate(self):
"""Could this config be used to send a real email?"""
missing = []
for k, v in self._map.items():
attr = getattr(self, k, False)
if not attr or attr == CONFIG_PLACEHOLDER:
| python | {
"resource": ""
} |
q13178 | scoped_session_decorator | train | def scoped_session_decorator(func):
"""Manage contexts and add debugging to db sessions."""
@wraps(func)
def wrapper(*args, **kwargs):
with sessions_scope(session):
# The session used in func comes from the funcs globals, but
# it will be a proxied thread local var from the session
| python | {
"resource": ""
} |
q13179 | serialized | train | def serialized(func):
"""Run a function within a db transaction using SERIALIZABLE isolation.
With this isolation level, committing will fail if this transaction
read data that was since modified by another transaction. So we need
to handle that case and retry the transaction.
"""
@wraps(func)
def wrapper(*args, **kw):
attempts = 100
session.remove()
while attempts > 0:
try:
session.connection(
execution_options={"isolation_level": "SERIALIZABLE"}
)
result = func(*args, **kw)
session.commit()
return result
except OperationalError as exc:
session.rollback()
if isinstance(exc.orig, TransactionRollbackError):
if | python | {
"resource": ""
} |
q13180 | chat | train | def chat(ws):
"""Relay chat messages to and from clients.
"""
lag_tolerance_secs = float(request.args.get("tolerance", 0.1))
client = Client(ws, lag_tolerance_secs=lag_tolerance_secs) | python | {
"resource": ""
} |
q13181 | Channel.subscribe | train | def subscribe(self, client):
"""Subscribe a client to the channel."""
| python | {
"resource": ""
} |
q13182 | Channel.unsubscribe | train | def unsubscribe(self, client):
"""Unsubscribe a client from the channel."""
if client in self.clients:
self.clients.remove(client)
| python | {
"resource": ""
} |
q13183 | Channel.listen | train | def listen(self):
"""Relay messages from a redis pubsub to all subscribed clients.
This is run continuously in a separate greenlet.
"""
pubsub = redis_conn.pubsub()
name = self.name
if isinstance(name, six.text_type):
name = name.encode("utf-8")
try:
pubsub.subscribe([name])
except ConnectionError:
app.logger.exception("Could not connect to redis.")
log("Listening on channel {}".format(self.name))
for message in pubsub.listen():
data = message.get("data")
if message["type"] == "message" | python | {
"resource": ""
} |
q13184 | ChatBackend.subscribe | train | def subscribe(self, client, channel_name):
"""Register a new client to receive messages on a channel."""
if channel_name not in self.channels:
self.channels[channel_name] = channel | python | {
"resource": ""
} |
q13185 | ChatBackend.unsubscribe | train | def unsubscribe(self, client):
"""Unsubscribe a client from all channels."""
| python | {
"resource": ""
} |
q13186 | Client.send | train | def send(self, message):
"""Send a single message to the websocket."""
if isinstance(message, bytes):
message = message.decode("utf8")
with self.send_lock:
try:
| python | {
"resource": ""
} |
q13187 | Client.heartbeat | train | def heartbeat(self):
"""Send a ping to the websocket periodically.
This is needed so that Heroku won't close the | python | {
"resource": ""
} |
q13188 | Client.publish | train | def publish(self):
"""Relay messages from client to redis."""
while not self.ws.closed:
# Sleep to prevent *constant* context-switches.
gevent.sleep(self.lag_tolerance_secs)
message = self.ws.receive()
| python | {
"resource": ""
} |
q13189 | BotBase.driver | train | def driver(self):
"""Returns a Selenium WebDriver instance of the type requested in the
configuration."""
from dallinger.config import get_config
config = get_config()
if not config.ready:
config.load()
driver_url = config.get("webdriver_url", None)
driver_type = config.get("webdriver_type")
driver = None
if driver_url:
capabilities = CAPABILITY_MAP.get(driver_type.lower())
| python | {
"resource": ""
} |
q13190 | BotBase.sign_up | train | def sign_up(self):
"""Accept HIT, give consent and start experiment.
This uses Selenium to click through buttons on the ad,
consent, and instruction pages.
"""
try:
self.driver.get(self.URL)
logger.info("Loaded ad page.")
begin = WebDriverWait(self.driver, 10).until(
EC.element_to_be_clickable((By.CLASS_NAME, "btn-primary"))
)
begin.click()
logger.info("Clicked begin experiment button.")
WebDriverWait(self.driver, 10).until(lambda d: len(d.window_handles) == 2)
self.driver.switch_to_window(self.driver.window_handles[-1])
self.driver.set_window_size(1024, 768)
logger.info("Switched to experiment popup.")
consent = WebDriverWait(self.driver, 10).until(
| python | {
"resource": ""
} |
q13191 | BotBase.sign_off | train | def sign_off(self):
"""Submit questionnaire and finish.
This uses Selenium to click the submit button on the questionnaire
and return to the original window.
"""
try:
logger.info("Bot player signing off.")
feedback = WebDriverWait(self.driver, 20).until(
EC.presence_of_element_located((By.ID, "submit-questionnaire"))
| python | {
"resource": ""
} |
q13192 | BotBase.run_experiment | train | def run_experiment(self):
"""Sign up, run the ``participate`` method, then sign off and close
the driver."""
try:
self.sign_up()
self.participate()
if self.sign_off():
| python | {
"resource": ""
} |
q13193 | HighPerformanceBotBase.run_experiment | train | def run_experiment(self):
"""Runs the phases of interacting with the experiment
including signup, participation, signoff, and recording completion.
"""
self.sign_up()
self.participate()
| python | {
"resource": ""
} |
q13194 | HighPerformanceBotBase.sign_up | train | def sign_up(self):
"""Signs up a participant for the experiment.
This is done using a POST request to the /participant/ endpoint.
"""
self.log("Bot player signing up.")
self.subscribe_to_quorum_channel()
while True:
url = (
"{host}/participant/{self.worker_id}/"
"{self.hit_id}/{self.assignment_id}/"
"debug?fingerprint_hash={hash}&recruiter=bots:{bot_name}".format(
host=self.host,
self=self,
hash=uuid.uuid4().hex,
bot_name=self.__class__.__name__,
)
)
try:
result = requests.post(url) | python | {
"resource": ""
} |
q13195 | HighPerformanceBotBase.complete_experiment | train | def complete_experiment(self, status):
"""Record worker completion status to the experiment server.
This is done using a GET request to the /worker_complete
or /worker_failed endpoints.
"""
self.log("Bot player completing experiment. Status: {}".format(status))
| python | {
"resource": ""
} |
q13196 | HighPerformanceBotBase.subscribe_to_quorum_channel | train | def subscribe_to_quorum_channel(self):
"""In case the experiment enforces a quorum, listen for notifications
before creating Partipant objects.
"""
| python | {
"resource": ""
} |
q13197 | MTurkService.set_rest_notification | train | def set_rest_notification(self, url, hit_type_id):
"""Set a REST endpoint to recieve notifications about the HIT
The newer AWS MTurk API does not support this feature, which means we
cannot use boto3 here. Instead, we make the call manually after
assembling a properly signed request.
"""
ISO8601 = "%Y-%m-%dT%H:%M:%SZ"
notification_version = "2006-05-05"
API_version = "2014-08-15"
data = {
"AWSAccessKeyId": self.aws_key,
"HITTypeId": hit_type_id,
"Notification.1.Active": "True",
"Notification.1.Destination": url,
"Notification.1.EventType.1": "AssignmentAccepted",
"Notification.1.EventType.2": "AssignmentAbandoned",
| python | {
"resource": ""
} |
q13198 | MTurkService.register_hit_type | train | def register_hit_type(
self, title, description, reward, duration_hours, keywords, qualifications
):
"""Register HIT Type for this HIT and return the type's ID, which
is required for creating a HIT.
"""
reward = str(reward)
duration_secs = int(datetime.timedelta(hours=duration_hours).total_seconds())
| python | {
"resource": ""
} |
q13199 | MTurkService.create_qualification_type | train | def create_qualification_type(self, name, description, status="Active"):
"""Create a new qualification Workers can be scored for.
"""
try:
response = self.mturk.create_qualification_type(
Name=name, Description=description, QualificationTypeStatus=status
)
| python | {
"resource": ""
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.