repository_name
stringlengths 7
55
| func_path_in_repository
stringlengths 4
223
| func_name
stringlengths 1
134
| whole_func_string
stringlengths 75
104k
| language
stringclasses 1
value | func_code_string
stringlengths 75
104k
| func_code_tokens
listlengths 19
28.4k
| func_documentation_string
stringlengths 1
46.9k
| func_documentation_tokens
listlengths 1
1.97k
| split_name
stringclasses 1
value | func_code_url
stringlengths 87
315
|
---|---|---|---|---|---|---|---|---|---|---|
Parsely/birding
|
src/birding/follow.py
|
follow_topic
|
def follow_topic(kafka_class, name, retry_interval=1, **kafka_init):
"""Dump each message from kafka topic to stdio."""
while True:
try:
client = kafka_class(**kafka_init)
topic = client.topics[name]
consumer = topic.get_simple_consumer(reset_offset_on_start=True)
except Exception as e:
if not should_try_kafka_again(e):
raise
with flushing(sys.stderr):
print(
'Failed attempt to connect to Kafka. Will retry ...',
file=sys.stderr)
sleep(retry_interval)
else:
with flushing(sys.stdout):
print('Connected to Kafka.')
break
dump = Dump()
for message in consumer:
with flushing(sys.stdout, sys.stderr):
status = load(message.value)
if status:
dump(status)
|
python
|
def follow_topic(kafka_class, name, retry_interval=1, **kafka_init):
"""Dump each message from kafka topic to stdio."""
while True:
try:
client = kafka_class(**kafka_init)
topic = client.topics[name]
consumer = topic.get_simple_consumer(reset_offset_on_start=True)
except Exception as e:
if not should_try_kafka_again(e):
raise
with flushing(sys.stderr):
print(
'Failed attempt to connect to Kafka. Will retry ...',
file=sys.stderr)
sleep(retry_interval)
else:
with flushing(sys.stdout):
print('Connected to Kafka.')
break
dump = Dump()
for message in consumer:
with flushing(sys.stdout, sys.stderr):
status = load(message.value)
if status:
dump(status)
|
[
"def",
"follow_topic",
"(",
"kafka_class",
",",
"name",
",",
"retry_interval",
"=",
"1",
",",
"*",
"*",
"kafka_init",
")",
":",
"while",
"True",
":",
"try",
":",
"client",
"=",
"kafka_class",
"(",
"*",
"*",
"kafka_init",
")",
"topic",
"=",
"client",
".",
"topics",
"[",
"name",
"]",
"consumer",
"=",
"topic",
".",
"get_simple_consumer",
"(",
"reset_offset_on_start",
"=",
"True",
")",
"except",
"Exception",
"as",
"e",
":",
"if",
"not",
"should_try_kafka_again",
"(",
"e",
")",
":",
"raise",
"with",
"flushing",
"(",
"sys",
".",
"stderr",
")",
":",
"print",
"(",
"'Failed attempt to connect to Kafka. Will retry ...'",
",",
"file",
"=",
"sys",
".",
"stderr",
")",
"sleep",
"(",
"retry_interval",
")",
"else",
":",
"with",
"flushing",
"(",
"sys",
".",
"stdout",
")",
":",
"print",
"(",
"'Connected to Kafka.'",
")",
"break",
"dump",
"=",
"Dump",
"(",
")",
"for",
"message",
"in",
"consumer",
":",
"with",
"flushing",
"(",
"sys",
".",
"stdout",
",",
"sys",
".",
"stderr",
")",
":",
"status",
"=",
"load",
"(",
"message",
".",
"value",
")",
"if",
"status",
":",
"dump",
"(",
"status",
")"
] |
Dump each message from kafka topic to stdio.
|
[
"Dump",
"each",
"message",
"from",
"kafka",
"topic",
"to",
"stdio",
"."
] |
train
|
https://github.com/Parsely/birding/blob/c7f6eee56424234e361b1a455595de202e744dac/src/birding/follow.py#L30-L55
|
Parsely/birding
|
src/birding/follow.py
|
follow_fd
|
def follow_fd(fd):
"""Dump each line of input to stdio."""
dump = Dump()
for line in fd:
if not line.strip():
continue
with flushing(sys.stdout, sys.stderr):
status = load(line)
if status:
dump(status)
|
python
|
def follow_fd(fd):
"""Dump each line of input to stdio."""
dump = Dump()
for line in fd:
if not line.strip():
continue
with flushing(sys.stdout, sys.stderr):
status = load(line)
if status:
dump(status)
|
[
"def",
"follow_fd",
"(",
"fd",
")",
":",
"dump",
"=",
"Dump",
"(",
")",
"for",
"line",
"in",
"fd",
":",
"if",
"not",
"line",
".",
"strip",
"(",
")",
":",
"continue",
"with",
"flushing",
"(",
"sys",
".",
"stdout",
",",
"sys",
".",
"stderr",
")",
":",
"status",
"=",
"load",
"(",
"line",
")",
"if",
"status",
":",
"dump",
"(",
"status",
")"
] |
Dump each line of input to stdio.
|
[
"Dump",
"each",
"line",
"of",
"input",
"to",
"stdio",
"."
] |
train
|
https://github.com/Parsely/birding/blob/c7f6eee56424234e361b1a455595de202e744dac/src/birding/follow.py#L58-L68
|
Parsely/birding
|
src/birding/follow.py
|
should_try_kafka_again
|
def should_try_kafka_again(error):
"""Determine if the error means to retry or fail, True to retry."""
msg = 'Unable to retrieve'
return isinstance(error, KafkaException) and str(error).startswith(msg)
|
python
|
def should_try_kafka_again(error):
"""Determine if the error means to retry or fail, True to retry."""
msg = 'Unable to retrieve'
return isinstance(error, KafkaException) and str(error).startswith(msg)
|
[
"def",
"should_try_kafka_again",
"(",
"error",
")",
":",
"msg",
"=",
"'Unable to retrieve'",
"return",
"isinstance",
"(",
"error",
",",
"KafkaException",
")",
"and",
"str",
"(",
"error",
")",
".",
"startswith",
"(",
"msg",
")"
] |
Determine if the error means to retry or fail, True to retry.
|
[
"Determine",
"if",
"the",
"error",
"means",
"to",
"retry",
"or",
"fail",
"True",
"to",
"retry",
"."
] |
train
|
https://github.com/Parsely/birding/blob/c7f6eee56424234e361b1a455595de202e744dac/src/birding/follow.py#L97-L100
|
koreyou/word_embedding_loader
|
word_embedding_loader/loader/glove.py
|
check_valid
|
def check_valid(line0, line1):
"""
Check if a file is valid Glove format.
Args:
line0 (bytes): First line of the file
line1 (bytes): Second line of the file
Returns:
boo: ``True`` if it is valid. ``False`` if it is invalid.
"""
data = line0.strip().split(b' ')
if len(data) <= 2:
return False
# check if data[2:] is float values
try:
map(float, data[2:])
except:
return False
return True
|
python
|
def check_valid(line0, line1):
"""
Check if a file is valid Glove format.
Args:
line0 (bytes): First line of the file
line1 (bytes): Second line of the file
Returns:
boo: ``True`` if it is valid. ``False`` if it is invalid.
"""
data = line0.strip().split(b' ')
if len(data) <= 2:
return False
# check if data[2:] is float values
try:
map(float, data[2:])
except:
return False
return True
|
[
"def",
"check_valid",
"(",
"line0",
",",
"line1",
")",
":",
"data",
"=",
"line0",
".",
"strip",
"(",
")",
".",
"split",
"(",
"b' '",
")",
"if",
"len",
"(",
"data",
")",
"<=",
"2",
":",
"return",
"False",
"# check if data[2:] is float values",
"try",
":",
"map",
"(",
"float",
",",
"data",
"[",
"2",
":",
"]",
")",
"except",
":",
"return",
"False",
"return",
"True"
] |
Check if a file is valid Glove format.
Args:
line0 (bytes): First line of the file
line1 (bytes): Second line of the file
Returns:
boo: ``True`` if it is valid. ``False`` if it is invalid.
|
[
"Check",
"if",
"a",
"file",
"is",
"valid",
"Glove",
"format",
"."
] |
train
|
https://github.com/koreyou/word_embedding_loader/blob/1bc123f1a8bea12646576dcd768dae3ecea39c06/word_embedding_loader/loader/glove.py#L17-L37
|
koreyou/word_embedding_loader
|
word_embedding_loader/loader/glove.py
|
load_with_vocab
|
def load_with_vocab(fin, vocab, dtype=np.float32):
"""
Load word embedding file with predefined vocabulary
Args:
fin (File): File object to read. File should be open for reading ascii.
vocab (dict): Mapping from words (``bytes``) to vector indices
(``int``).
dtype (numpy.dtype): Element data type to use for the array.
Returns:
numpy.ndarray: Word embedding representation vectors
"""
arr = None
for line in fin:
try:
token, v = _parse_line(line, dtype)
except (ValueError, IndexError):
raise ParseError(b'Parsing error in line: ' + line)
if token in vocab:
if arr is None:
arr = np.empty((len(vocab), len(v)), dtype=dtype)
arr.fill(np.NaN)
elif arr.shape[1] != len(v):
raise ParseError(b'Vector size did not match in line: ' + line)
arr[vocab[token], :] = np.array(v, dtype=dtype).reshape(1, -1)
return arr
|
python
|
def load_with_vocab(fin, vocab, dtype=np.float32):
"""
Load word embedding file with predefined vocabulary
Args:
fin (File): File object to read. File should be open for reading ascii.
vocab (dict): Mapping from words (``bytes``) to vector indices
(``int``).
dtype (numpy.dtype): Element data type to use for the array.
Returns:
numpy.ndarray: Word embedding representation vectors
"""
arr = None
for line in fin:
try:
token, v = _parse_line(line, dtype)
except (ValueError, IndexError):
raise ParseError(b'Parsing error in line: ' + line)
if token in vocab:
if arr is None:
arr = np.empty((len(vocab), len(v)), dtype=dtype)
arr.fill(np.NaN)
elif arr.shape[1] != len(v):
raise ParseError(b'Vector size did not match in line: ' + line)
arr[vocab[token], :] = np.array(v, dtype=dtype).reshape(1, -1)
return arr
|
[
"def",
"load_with_vocab",
"(",
"fin",
",",
"vocab",
",",
"dtype",
"=",
"np",
".",
"float32",
")",
":",
"arr",
"=",
"None",
"for",
"line",
"in",
"fin",
":",
"try",
":",
"token",
",",
"v",
"=",
"_parse_line",
"(",
"line",
",",
"dtype",
")",
"except",
"(",
"ValueError",
",",
"IndexError",
")",
":",
"raise",
"ParseError",
"(",
"b'Parsing error in line: '",
"+",
"line",
")",
"if",
"token",
"in",
"vocab",
":",
"if",
"arr",
"is",
"None",
":",
"arr",
"=",
"np",
".",
"empty",
"(",
"(",
"len",
"(",
"vocab",
")",
",",
"len",
"(",
"v",
")",
")",
",",
"dtype",
"=",
"dtype",
")",
"arr",
".",
"fill",
"(",
"np",
".",
"NaN",
")",
"elif",
"arr",
".",
"shape",
"[",
"1",
"]",
"!=",
"len",
"(",
"v",
")",
":",
"raise",
"ParseError",
"(",
"b'Vector size did not match in line: '",
"+",
"line",
")",
"arr",
"[",
"vocab",
"[",
"token",
"]",
",",
":",
"]",
"=",
"np",
".",
"array",
"(",
"v",
",",
"dtype",
"=",
"dtype",
")",
".",
"reshape",
"(",
"1",
",",
"-",
"1",
")",
"return",
"arr"
] |
Load word embedding file with predefined vocabulary
Args:
fin (File): File object to read. File should be open for reading ascii.
vocab (dict): Mapping from words (``bytes``) to vector indices
(``int``).
dtype (numpy.dtype): Element data type to use for the array.
Returns:
numpy.ndarray: Word embedding representation vectors
|
[
"Load",
"word",
"embedding",
"file",
"with",
"predefined",
"vocabulary"
] |
train
|
https://github.com/koreyou/word_embedding_loader/blob/1bc123f1a8bea12646576dcd768dae3ecea39c06/word_embedding_loader/loader/glove.py#L47-L73
|
koreyou/word_embedding_loader
|
word_embedding_loader/loader/glove.py
|
load
|
def load(fin, dtype=np.float32, max_vocab=None):
"""
Load word embedding file.
Args:
fin (File): File object to read. File should be open for reading ascii.
dtype (numpy.dtype): Element data type to use for the array.
max_vocab (int): Number of vocabulary to read.
Returns:
numpy.ndarray: Word embedding representation vectors
dict: Mapping from words to vector indices.
"""
vocab = {}
arr = None
i = 0
for line in fin:
if max_vocab is not None and i >= max_vocab:
break
try:
token, v = _parse_line(line, dtype)
except (ValueError, IndexError):
raise ParseError(b'Parsing error in line: ' + line)
if token in vocab:
parse_warn(b'Duplicated vocabulary ' + token)
continue
if arr is None:
arr = np.array(v, dtype=dtype).reshape(1, -1)
else:
if arr.shape[1] != len(v):
raise ParseError(b'Vector size did not match in line: ' + line)
arr = np.append(arr, [v], axis=0)
vocab[token] = i
i += 1
return arr, vocab
|
python
|
def load(fin, dtype=np.float32, max_vocab=None):
"""
Load word embedding file.
Args:
fin (File): File object to read. File should be open for reading ascii.
dtype (numpy.dtype): Element data type to use for the array.
max_vocab (int): Number of vocabulary to read.
Returns:
numpy.ndarray: Word embedding representation vectors
dict: Mapping from words to vector indices.
"""
vocab = {}
arr = None
i = 0
for line in fin:
if max_vocab is not None and i >= max_vocab:
break
try:
token, v = _parse_line(line, dtype)
except (ValueError, IndexError):
raise ParseError(b'Parsing error in line: ' + line)
if token in vocab:
parse_warn(b'Duplicated vocabulary ' + token)
continue
if arr is None:
arr = np.array(v, dtype=dtype).reshape(1, -1)
else:
if arr.shape[1] != len(v):
raise ParseError(b'Vector size did not match in line: ' + line)
arr = np.append(arr, [v], axis=0)
vocab[token] = i
i += 1
return arr, vocab
|
[
"def",
"load",
"(",
"fin",
",",
"dtype",
"=",
"np",
".",
"float32",
",",
"max_vocab",
"=",
"None",
")",
":",
"vocab",
"=",
"{",
"}",
"arr",
"=",
"None",
"i",
"=",
"0",
"for",
"line",
"in",
"fin",
":",
"if",
"max_vocab",
"is",
"not",
"None",
"and",
"i",
">=",
"max_vocab",
":",
"break",
"try",
":",
"token",
",",
"v",
"=",
"_parse_line",
"(",
"line",
",",
"dtype",
")",
"except",
"(",
"ValueError",
",",
"IndexError",
")",
":",
"raise",
"ParseError",
"(",
"b'Parsing error in line: '",
"+",
"line",
")",
"if",
"token",
"in",
"vocab",
":",
"parse_warn",
"(",
"b'Duplicated vocabulary '",
"+",
"token",
")",
"continue",
"if",
"arr",
"is",
"None",
":",
"arr",
"=",
"np",
".",
"array",
"(",
"v",
",",
"dtype",
"=",
"dtype",
")",
".",
"reshape",
"(",
"1",
",",
"-",
"1",
")",
"else",
":",
"if",
"arr",
".",
"shape",
"[",
"1",
"]",
"!=",
"len",
"(",
"v",
")",
":",
"raise",
"ParseError",
"(",
"b'Vector size did not match in line: '",
"+",
"line",
")",
"arr",
"=",
"np",
".",
"append",
"(",
"arr",
",",
"[",
"v",
"]",
",",
"axis",
"=",
"0",
")",
"vocab",
"[",
"token",
"]",
"=",
"i",
"i",
"+=",
"1",
"return",
"arr",
",",
"vocab"
] |
Load word embedding file.
Args:
fin (File): File object to read. File should be open for reading ascii.
dtype (numpy.dtype): Element data type to use for the array.
max_vocab (int): Number of vocabulary to read.
Returns:
numpy.ndarray: Word embedding representation vectors
dict: Mapping from words to vector indices.
|
[
"Load",
"word",
"embedding",
"file",
"."
] |
train
|
https://github.com/koreyou/word_embedding_loader/blob/1bc123f1a8bea12646576dcd768dae3ecea39c06/word_embedding_loader/loader/glove.py#L76-L111
|
ministryofjustice/money-to-prisoners-common
|
build_tasks.py
|
python_dependencies
|
def python_dependencies(context: Context, extras=None):
"""
Updates python dependencies
"""
with mock.patch('setuptools.setup'):
from setup import install_requires, extras_require
requirements = install_requires.copy()
if extras:
requirements.extend(extras_require[extras])
return context.pip_command('install', *requirements)
|
python
|
def python_dependencies(context: Context, extras=None):
"""
Updates python dependencies
"""
with mock.patch('setuptools.setup'):
from setup import install_requires, extras_require
requirements = install_requires.copy()
if extras:
requirements.extend(extras_require[extras])
return context.pip_command('install', *requirements)
|
[
"def",
"python_dependencies",
"(",
"context",
":",
"Context",
",",
"extras",
"=",
"None",
")",
":",
"with",
"mock",
".",
"patch",
"(",
"'setuptools.setup'",
")",
":",
"from",
"setup",
"import",
"install_requires",
",",
"extras_require",
"requirements",
"=",
"install_requires",
".",
"copy",
"(",
")",
"if",
"extras",
":",
"requirements",
".",
"extend",
"(",
"extras_require",
"[",
"extras",
"]",
")",
"return",
"context",
".",
"pip_command",
"(",
"'install'",
",",
"*",
"requirements",
")"
] |
Updates python dependencies
|
[
"Updates",
"python",
"dependencies"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/build_tasks.py#L107-L117
|
ministryofjustice/money-to-prisoners-common
|
build_tasks.py
|
set_version
|
def set_version(context: Context, version=None, bump=False):
"""
Updates the version of MTP-common
"""
if bump and version:
raise TaskError('You cannot bump and set a specific version')
if bump:
from mtp_common import VERSION
version = list(VERSION)
version[-1] += 1
else:
try:
version = list(map(int, version.split('.')))
assert len(version) == 3
except (AttributeError, ValueError, AssertionError):
raise TaskError('Version must be in the form N.N.N')
dotted_version = '.'.join(map(str, version))
replacements = [
(r'^VERSION =.*$',
'VERSION = (%s)' % ', '.join(map(str, version)),
'mtp_common/__init__.py'),
(r'^ "version":.*$',
' "version": "%s",' % dotted_version,
'package.json'),
]
for search, replacement, path in replacements:
with open(os.path.join(root_path, path)) as f:
content = f.read()
content = re.sub(search, replacement, content, flags=re.MULTILINE)
with open(os.path.join(root_path, path), 'w') as f:
f.write(content)
context.debug('Updated version to %s' % dotted_version)
|
python
|
def set_version(context: Context, version=None, bump=False):
"""
Updates the version of MTP-common
"""
if bump and version:
raise TaskError('You cannot bump and set a specific version')
if bump:
from mtp_common import VERSION
version = list(VERSION)
version[-1] += 1
else:
try:
version = list(map(int, version.split('.')))
assert len(version) == 3
except (AttributeError, ValueError, AssertionError):
raise TaskError('Version must be in the form N.N.N')
dotted_version = '.'.join(map(str, version))
replacements = [
(r'^VERSION =.*$',
'VERSION = (%s)' % ', '.join(map(str, version)),
'mtp_common/__init__.py'),
(r'^ "version":.*$',
' "version": "%s",' % dotted_version,
'package.json'),
]
for search, replacement, path in replacements:
with open(os.path.join(root_path, path)) as f:
content = f.read()
content = re.sub(search, replacement, content, flags=re.MULTILINE)
with open(os.path.join(root_path, path), 'w') as f:
f.write(content)
context.debug('Updated version to %s' % dotted_version)
|
[
"def",
"set_version",
"(",
"context",
":",
"Context",
",",
"version",
"=",
"None",
",",
"bump",
"=",
"False",
")",
":",
"if",
"bump",
"and",
"version",
":",
"raise",
"TaskError",
"(",
"'You cannot bump and set a specific version'",
")",
"if",
"bump",
":",
"from",
"mtp_common",
"import",
"VERSION",
"version",
"=",
"list",
"(",
"VERSION",
")",
"version",
"[",
"-",
"1",
"]",
"+=",
"1",
"else",
":",
"try",
":",
"version",
"=",
"list",
"(",
"map",
"(",
"int",
",",
"version",
".",
"split",
"(",
"'.'",
")",
")",
")",
"assert",
"len",
"(",
"version",
")",
"==",
"3",
"except",
"(",
"AttributeError",
",",
"ValueError",
",",
"AssertionError",
")",
":",
"raise",
"TaskError",
"(",
"'Version must be in the form N.N.N'",
")",
"dotted_version",
"=",
"'.'",
".",
"join",
"(",
"map",
"(",
"str",
",",
"version",
")",
")",
"replacements",
"=",
"[",
"(",
"r'^VERSION =.*$'",
",",
"'VERSION = (%s)'",
"%",
"', '",
".",
"join",
"(",
"map",
"(",
"str",
",",
"version",
")",
")",
",",
"'mtp_common/__init__.py'",
")",
",",
"(",
"r'^ \"version\":.*$'",
",",
"' \"version\": \"%s\",'",
"%",
"dotted_version",
",",
"'package.json'",
")",
",",
"]",
"for",
"search",
",",
"replacement",
",",
"path",
"in",
"replacements",
":",
"with",
"open",
"(",
"os",
".",
"path",
".",
"join",
"(",
"root_path",
",",
"path",
")",
")",
"as",
"f",
":",
"content",
"=",
"f",
".",
"read",
"(",
")",
"content",
"=",
"re",
".",
"sub",
"(",
"search",
",",
"replacement",
",",
"content",
",",
"flags",
"=",
"re",
".",
"MULTILINE",
")",
"with",
"open",
"(",
"os",
".",
"path",
".",
"join",
"(",
"root_path",
",",
"path",
")",
",",
"'w'",
")",
"as",
"f",
":",
"f",
".",
"write",
"(",
"content",
")",
"context",
".",
"debug",
"(",
"'Updated version to %s'",
"%",
"dotted_version",
")"
] |
Updates the version of MTP-common
|
[
"Updates",
"the",
"version",
"of",
"MTP",
"-",
"common"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/build_tasks.py#L140-L173
|
ministryofjustice/money-to-prisoners-common
|
build_tasks.py
|
docs
|
def docs(context: Context):
"""
Generates static documentation
"""
try:
from sphinx.application import Sphinx
except ImportError:
context.pip_command('install', 'Sphinx')
from sphinx.application import Sphinx
context.shell('cp', 'README.rst', 'docs/README.rst')
app = Sphinx('docs', 'docs', 'docs/build', 'docs/build/.doctrees', buildername='html', parallel=True,
verbosity=context.verbosity)
app.build()
|
python
|
def docs(context: Context):
"""
Generates static documentation
"""
try:
from sphinx.application import Sphinx
except ImportError:
context.pip_command('install', 'Sphinx')
from sphinx.application import Sphinx
context.shell('cp', 'README.rst', 'docs/README.rst')
app = Sphinx('docs', 'docs', 'docs/build', 'docs/build/.doctrees', buildername='html', parallel=True,
verbosity=context.verbosity)
app.build()
|
[
"def",
"docs",
"(",
"context",
":",
"Context",
")",
":",
"try",
":",
"from",
"sphinx",
".",
"application",
"import",
"Sphinx",
"except",
"ImportError",
":",
"context",
".",
"pip_command",
"(",
"'install'",
",",
"'Sphinx'",
")",
"from",
"sphinx",
".",
"application",
"import",
"Sphinx",
"context",
".",
"shell",
"(",
"'cp'",
",",
"'README.rst'",
",",
"'docs/README.rst'",
")",
"app",
"=",
"Sphinx",
"(",
"'docs'",
",",
"'docs'",
",",
"'docs/build'",
",",
"'docs/build/.doctrees'",
",",
"buildername",
"=",
"'html'",
",",
"parallel",
"=",
"True",
",",
"verbosity",
"=",
"context",
".",
"verbosity",
")",
"app",
".",
"build",
"(",
")"
] |
Generates static documentation
|
[
"Generates",
"static",
"documentation"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/build_tasks.py#L177-L190
|
ministryofjustice/money-to-prisoners-common
|
build_tasks.py
|
clean
|
def clean(context: Context, delete_dependencies: bool = False):
"""
Deletes build outputs
"""
paths = ['docs/build', 'build', 'dist', '.eggs'] + glob.glob('*.egg-info')
context.shell('rm -rf %s' % paths_for_shell(paths))
context.shell('find %s -name "*.pyc" -or -name __pycache__ -delete' % context.app.django_app_name)
if delete_dependencies:
context.info('Cleaning local %s dependencies' % context.app.name)
paths = ['venv']
context.shell('rm -rf %s' % paths_for_shell(paths))
|
python
|
def clean(context: Context, delete_dependencies: bool = False):
"""
Deletes build outputs
"""
paths = ['docs/build', 'build', 'dist', '.eggs'] + glob.glob('*.egg-info')
context.shell('rm -rf %s' % paths_for_shell(paths))
context.shell('find %s -name "*.pyc" -or -name __pycache__ -delete' % context.app.django_app_name)
if delete_dependencies:
context.info('Cleaning local %s dependencies' % context.app.name)
paths = ['venv']
context.shell('rm -rf %s' % paths_for_shell(paths))
|
[
"def",
"clean",
"(",
"context",
":",
"Context",
",",
"delete_dependencies",
":",
"bool",
"=",
"False",
")",
":",
"paths",
"=",
"[",
"'docs/build'",
",",
"'build'",
",",
"'dist'",
",",
"'.eggs'",
"]",
"+",
"glob",
".",
"glob",
"(",
"'*.egg-info'",
")",
"context",
".",
"shell",
"(",
"'rm -rf %s'",
"%",
"paths_for_shell",
"(",
"paths",
")",
")",
"context",
".",
"shell",
"(",
"'find %s -name \"*.pyc\" -or -name __pycache__ -delete'",
"%",
"context",
".",
"app",
".",
"django_app_name",
")",
"if",
"delete_dependencies",
":",
"context",
".",
"info",
"(",
"'Cleaning local %s dependencies'",
"%",
"context",
".",
"app",
".",
"name",
")",
"paths",
"=",
"[",
"'venv'",
"]",
"context",
".",
"shell",
"(",
"'rm -rf %s'",
"%",
"paths_for_shell",
"(",
"paths",
")",
")"
] |
Deletes build outputs
|
[
"Deletes",
"build",
"outputs"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/build_tasks.py#L202-L213
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/auth/backends.py
|
MojBackend.authenticate
|
def authenticate(self, username=None, password=None):
"""
Returns a valid `MojUser` if the authentication is successful
or None if the credentials were wrong.
"""
data = api_client.authenticate(username, password)
if not data:
return
return User(
data.get('pk'), data.get('token'), data.get('user_data')
)
|
python
|
def authenticate(self, username=None, password=None):
"""
Returns a valid `MojUser` if the authentication is successful
or None if the credentials were wrong.
"""
data = api_client.authenticate(username, password)
if not data:
return
return User(
data.get('pk'), data.get('token'), data.get('user_data')
)
|
[
"def",
"authenticate",
"(",
"self",
",",
"username",
"=",
"None",
",",
"password",
"=",
"None",
")",
":",
"data",
"=",
"api_client",
".",
"authenticate",
"(",
"username",
",",
"password",
")",
"if",
"not",
"data",
":",
"return",
"return",
"User",
"(",
"data",
".",
"get",
"(",
"'pk'",
")",
",",
"data",
".",
"get",
"(",
"'token'",
")",
",",
"data",
".",
"get",
"(",
"'user_data'",
")",
")"
] |
Returns a valid `MojUser` if the authentication is successful
or None if the credentials were wrong.
|
[
"Returns",
"a",
"valid",
"MojUser",
"if",
"the",
"authentication",
"is",
"successful",
"or",
"None",
"if",
"the",
"credentials",
"were",
"wrong",
"."
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/auth/backends.py#L17-L28
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/nomis.py
|
get_client_token
|
def get_client_token():
"""
Requests and stores the NOMIS API client token from mtp-api
"""
if getattr(settings, 'NOMIS_API_CLIENT_TOKEN', ''):
return settings.NOMIS_API_CLIENT_TOKEN
global client_token
if not client_token or client_token['expires'] and client_token['expires'] - now() < datetime.timedelta(days=1):
session = None
try:
session = api_client.get_authenticated_api_session(settings.TOKEN_RETRIEVAL_USERNAME,
settings.TOKEN_RETRIEVAL_PASSWORD)
client_token = session.get('/tokens/nomis/').json()
except (requests.RequestException, HttpNotFoundError, ValueError, AttributeError):
logger.exception('Cannot load NOMIS API client token')
return None
finally:
if session and getattr(session, 'access_token', None):
api_client.revoke_token(session.access_token)
if client_token.get('expires'):
client_token['expires'] = parse_datetime(client_token['expires'])
if client_token['expires'] < now():
logger.error('NOMIS API client token from mtp-api had expired')
return None
return client_token['token']
|
python
|
def get_client_token():
"""
Requests and stores the NOMIS API client token from mtp-api
"""
if getattr(settings, 'NOMIS_API_CLIENT_TOKEN', ''):
return settings.NOMIS_API_CLIENT_TOKEN
global client_token
if not client_token or client_token['expires'] and client_token['expires'] - now() < datetime.timedelta(days=1):
session = None
try:
session = api_client.get_authenticated_api_session(settings.TOKEN_RETRIEVAL_USERNAME,
settings.TOKEN_RETRIEVAL_PASSWORD)
client_token = session.get('/tokens/nomis/').json()
except (requests.RequestException, HttpNotFoundError, ValueError, AttributeError):
logger.exception('Cannot load NOMIS API client token')
return None
finally:
if session and getattr(session, 'access_token', None):
api_client.revoke_token(session.access_token)
if client_token.get('expires'):
client_token['expires'] = parse_datetime(client_token['expires'])
if client_token['expires'] < now():
logger.error('NOMIS API client token from mtp-api had expired')
return None
return client_token['token']
|
[
"def",
"get_client_token",
"(",
")",
":",
"if",
"getattr",
"(",
"settings",
",",
"'NOMIS_API_CLIENT_TOKEN'",
",",
"''",
")",
":",
"return",
"settings",
".",
"NOMIS_API_CLIENT_TOKEN",
"global",
"client_token",
"if",
"not",
"client_token",
"or",
"client_token",
"[",
"'expires'",
"]",
"and",
"client_token",
"[",
"'expires'",
"]",
"-",
"now",
"(",
")",
"<",
"datetime",
".",
"timedelta",
"(",
"days",
"=",
"1",
")",
":",
"session",
"=",
"None",
"try",
":",
"session",
"=",
"api_client",
".",
"get_authenticated_api_session",
"(",
"settings",
".",
"TOKEN_RETRIEVAL_USERNAME",
",",
"settings",
".",
"TOKEN_RETRIEVAL_PASSWORD",
")",
"client_token",
"=",
"session",
".",
"get",
"(",
"'/tokens/nomis/'",
")",
".",
"json",
"(",
")",
"except",
"(",
"requests",
".",
"RequestException",
",",
"HttpNotFoundError",
",",
"ValueError",
",",
"AttributeError",
")",
":",
"logger",
".",
"exception",
"(",
"'Cannot load NOMIS API client token'",
")",
"return",
"None",
"finally",
":",
"if",
"session",
"and",
"getattr",
"(",
"session",
",",
"'access_token'",
",",
"None",
")",
":",
"api_client",
".",
"revoke_token",
"(",
"session",
".",
"access_token",
")",
"if",
"client_token",
".",
"get",
"(",
"'expires'",
")",
":",
"client_token",
"[",
"'expires'",
"]",
"=",
"parse_datetime",
"(",
"client_token",
"[",
"'expires'",
"]",
")",
"if",
"client_token",
"[",
"'expires'",
"]",
"<",
"now",
"(",
")",
":",
"logger",
".",
"error",
"(",
"'NOMIS API client token from mtp-api had expired'",
")",
"return",
"None",
"return",
"client_token",
"[",
"'token'",
"]"
] |
Requests and stores the NOMIS API client token from mtp-api
|
[
"Requests",
"and",
"stores",
"the",
"NOMIS",
"API",
"client",
"token",
"from",
"mtp",
"-",
"api"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/nomis.py#L29-L53
|
chrisseto/django-include
|
include/query.py
|
IncludeQuerySet.include
|
def include(self, *fields, **kwargs):
"""
Return a new QuerySet instance that will include related objects.
If fields are specified, they must be non-hidden relationships.
If select_related(None) is called, clear the list.
"""
clone = self._clone()
# Preserve the stickiness of related querysets
# NOTE: by default _clone will clear this attribute
# .include does not modify the actual query, so we
# should not clear `filter_is_sticky`
if self.query.filter_is_sticky:
clone.query.filter_is_sticky = True
clone._include_limit = kwargs.pop('limit_includes', None)
assert not kwargs, '"limit_includes" is the only accepted kwargs. Eat your heart out 2.7'
# Copy the behavior of .select_related(None)
if fields == (None, ):
for field in clone._includes.keys():
clone.query._annotations.pop('__{}'.format(field.name), None)
clone._includes.clear()
return clone
# Parse everything the way django handles joins/select related
# Including multiple child fields ie .include(field1__field2, field1__field3)
# turns into {field1: {field2: {}, field3: {}}
for name in fields:
ctx, model = clone._includes, clone.model
for spl in name.split('__'):
field = model._meta.get_field(spl)
if isinstance(field, ForeignObjectRel) and field.is_hidden():
raise ValueError('Hidden field "{!r}" has no descriptor and therefore cannot be included'.format(field))
model = field.related_model
ctx = ctx.setdefault(field, OrderedDict())
for field in clone._includes.keys():
clone._include(field)
return clone
|
python
|
def include(self, *fields, **kwargs):
"""
Return a new QuerySet instance that will include related objects.
If fields are specified, they must be non-hidden relationships.
If select_related(None) is called, clear the list.
"""
clone = self._clone()
# Preserve the stickiness of related querysets
# NOTE: by default _clone will clear this attribute
# .include does not modify the actual query, so we
# should not clear `filter_is_sticky`
if self.query.filter_is_sticky:
clone.query.filter_is_sticky = True
clone._include_limit = kwargs.pop('limit_includes', None)
assert not kwargs, '"limit_includes" is the only accepted kwargs. Eat your heart out 2.7'
# Copy the behavior of .select_related(None)
if fields == (None, ):
for field in clone._includes.keys():
clone.query._annotations.pop('__{}'.format(field.name), None)
clone._includes.clear()
return clone
# Parse everything the way django handles joins/select related
# Including multiple child fields ie .include(field1__field2, field1__field3)
# turns into {field1: {field2: {}, field3: {}}
for name in fields:
ctx, model = clone._includes, clone.model
for spl in name.split('__'):
field = model._meta.get_field(spl)
if isinstance(field, ForeignObjectRel) and field.is_hidden():
raise ValueError('Hidden field "{!r}" has no descriptor and therefore cannot be included'.format(field))
model = field.related_model
ctx = ctx.setdefault(field, OrderedDict())
for field in clone._includes.keys():
clone._include(field)
return clone
|
[
"def",
"include",
"(",
"self",
",",
"*",
"fields",
",",
"*",
"*",
"kwargs",
")",
":",
"clone",
"=",
"self",
".",
"_clone",
"(",
")",
"# Preserve the stickiness of related querysets",
"# NOTE: by default _clone will clear this attribute",
"# .include does not modify the actual query, so we",
"# should not clear `filter_is_sticky`",
"if",
"self",
".",
"query",
".",
"filter_is_sticky",
":",
"clone",
".",
"query",
".",
"filter_is_sticky",
"=",
"True",
"clone",
".",
"_include_limit",
"=",
"kwargs",
".",
"pop",
"(",
"'limit_includes'",
",",
"None",
")",
"assert",
"not",
"kwargs",
",",
"'\"limit_includes\" is the only accepted kwargs. Eat your heart out 2.7'",
"# Copy the behavior of .select_related(None)",
"if",
"fields",
"==",
"(",
"None",
",",
")",
":",
"for",
"field",
"in",
"clone",
".",
"_includes",
".",
"keys",
"(",
")",
":",
"clone",
".",
"query",
".",
"_annotations",
".",
"pop",
"(",
"'__{}'",
".",
"format",
"(",
"field",
".",
"name",
")",
",",
"None",
")",
"clone",
".",
"_includes",
".",
"clear",
"(",
")",
"return",
"clone",
"# Parse everything the way django handles joins/select related",
"# Including multiple child fields ie .include(field1__field2, field1__field3)",
"# turns into {field1: {field2: {}, field3: {}}",
"for",
"name",
"in",
"fields",
":",
"ctx",
",",
"model",
"=",
"clone",
".",
"_includes",
",",
"clone",
".",
"model",
"for",
"spl",
"in",
"name",
".",
"split",
"(",
"'__'",
")",
":",
"field",
"=",
"model",
".",
"_meta",
".",
"get_field",
"(",
"spl",
")",
"if",
"isinstance",
"(",
"field",
",",
"ForeignObjectRel",
")",
"and",
"field",
".",
"is_hidden",
"(",
")",
":",
"raise",
"ValueError",
"(",
"'Hidden field \"{!r}\" has no descriptor and therefore cannot be included'",
".",
"format",
"(",
"field",
")",
")",
"model",
"=",
"field",
".",
"related_model",
"ctx",
"=",
"ctx",
".",
"setdefault",
"(",
"field",
",",
"OrderedDict",
"(",
")",
")",
"for",
"field",
"in",
"clone",
".",
"_includes",
".",
"keys",
"(",
")",
":",
"clone",
".",
"_include",
"(",
"field",
")",
"return",
"clone"
] |
Return a new QuerySet instance that will include related objects.
If fields are specified, they must be non-hidden relationships.
If select_related(None) is called, clear the list.
|
[
"Return",
"a",
"new",
"QuerySet",
"instance",
"that",
"will",
"include",
"related",
"objects",
"."
] |
train
|
https://github.com/chrisseto/django-include/blob/0cf095f9e827d8be406c16f2ec0c17b6c4c301ba/include/query.py#L93-L135
|
harvard-nrg/yaxil
|
yaxil/dicom/__init__.py
|
search
|
def search(d, recursive=True, store_meta=True):
'''
Search for DICOM files within a given directory and receive back a
dictionary of {StudyInstanceUID: {SeriesNumber: [files]}}
Example usage::
>>> import yaxil.dicom
>>> yaxil.dicom.search("~/dicoms").keys()
['1.2.340.500067.8.9.10.11012.13000001401516017181900000200']
:param d: Directory name
:type d: str
:param recursive: Search recursively
:type recursive: bool
:param store_meta: Read and store metadata for each file for fast lookups
:type store_meta: bool
:returns: Dictionary of {StudyInstanceUID: {SeriesNumber: [files]}}
:rtype: dict
'''
# say this fast three times
scans = col.defaultdict(lambda: col.defaultdict(lambda: col.defaultdict(list)))
for dirpath,dirnames,filenames in os.walk(os.path.expanduser(d)):
for f in filenames:
fullfile = os.path.join(dirpath, f)
try:
d = pydicom.read_file(fullfile, stop_before_pixels=True)
except pydicom.filereader.InvalidDicomError:
continue
meta = {k: getattr(d, k, None) for k in d.dir()} if store_meta else None
scans[d.StudyInstanceUID][d.SeriesNumber][d.InstanceNumber].append(DicomFile(meta=meta, file=fullfile))
if not recursive:
del dirnames[:]
return scans
|
python
|
def search(d, recursive=True, store_meta=True):
'''
Search for DICOM files within a given directory and receive back a
dictionary of {StudyInstanceUID: {SeriesNumber: [files]}}
Example usage::
>>> import yaxil.dicom
>>> yaxil.dicom.search("~/dicoms").keys()
['1.2.340.500067.8.9.10.11012.13000001401516017181900000200']
:param d: Directory name
:type d: str
:param recursive: Search recursively
:type recursive: bool
:param store_meta: Read and store metadata for each file for fast lookups
:type store_meta: bool
:returns: Dictionary of {StudyInstanceUID: {SeriesNumber: [files]}}
:rtype: dict
'''
# say this fast three times
scans = col.defaultdict(lambda: col.defaultdict(lambda: col.defaultdict(list)))
for dirpath,dirnames,filenames in os.walk(os.path.expanduser(d)):
for f in filenames:
fullfile = os.path.join(dirpath, f)
try:
d = pydicom.read_file(fullfile, stop_before_pixels=True)
except pydicom.filereader.InvalidDicomError:
continue
meta = {k: getattr(d, k, None) for k in d.dir()} if store_meta else None
scans[d.StudyInstanceUID][d.SeriesNumber][d.InstanceNumber].append(DicomFile(meta=meta, file=fullfile))
if not recursive:
del dirnames[:]
return scans
|
[
"def",
"search",
"(",
"d",
",",
"recursive",
"=",
"True",
",",
"store_meta",
"=",
"True",
")",
":",
"# say this fast three times",
"scans",
"=",
"col",
".",
"defaultdict",
"(",
"lambda",
":",
"col",
".",
"defaultdict",
"(",
"lambda",
":",
"col",
".",
"defaultdict",
"(",
"list",
")",
")",
")",
"for",
"dirpath",
",",
"dirnames",
",",
"filenames",
"in",
"os",
".",
"walk",
"(",
"os",
".",
"path",
".",
"expanduser",
"(",
"d",
")",
")",
":",
"for",
"f",
"in",
"filenames",
":",
"fullfile",
"=",
"os",
".",
"path",
".",
"join",
"(",
"dirpath",
",",
"f",
")",
"try",
":",
"d",
"=",
"pydicom",
".",
"read_file",
"(",
"fullfile",
",",
"stop_before_pixels",
"=",
"True",
")",
"except",
"pydicom",
".",
"filereader",
".",
"InvalidDicomError",
":",
"continue",
"meta",
"=",
"{",
"k",
":",
"getattr",
"(",
"d",
",",
"k",
",",
"None",
")",
"for",
"k",
"in",
"d",
".",
"dir",
"(",
")",
"}",
"if",
"store_meta",
"else",
"None",
"scans",
"[",
"d",
".",
"StudyInstanceUID",
"]",
"[",
"d",
".",
"SeriesNumber",
"]",
"[",
"d",
".",
"InstanceNumber",
"]",
".",
"append",
"(",
"DicomFile",
"(",
"meta",
"=",
"meta",
",",
"file",
"=",
"fullfile",
")",
")",
"if",
"not",
"recursive",
":",
"del",
"dirnames",
"[",
":",
"]",
"return",
"scans"
] |
Search for DICOM files within a given directory and receive back a
dictionary of {StudyInstanceUID: {SeriesNumber: [files]}}
Example usage::
>>> import yaxil.dicom
>>> yaxil.dicom.search("~/dicoms").keys()
['1.2.340.500067.8.9.10.11012.13000001401516017181900000200']
:param d: Directory name
:type d: str
:param recursive: Search recursively
:type recursive: bool
:param store_meta: Read and store metadata for each file for fast lookups
:type store_meta: bool
:returns: Dictionary of {StudyInstanceUID: {SeriesNumber: [files]}}
:rtype: dict
|
[
"Search",
"for",
"DICOM",
"files",
"within",
"a",
"given",
"directory",
"and",
"receive",
"back",
"a",
"dictionary",
"of",
"{",
"StudyInstanceUID",
":",
"{",
"SeriesNumber",
":",
"[",
"files",
"]",
"}}",
"Example",
"usage",
"::",
">>>",
"import",
"yaxil",
".",
"dicom",
">>>",
"yaxil",
".",
"dicom",
".",
"search",
"(",
"~",
"/",
"dicoms",
")",
".",
"keys",
"()",
"[",
"1",
".",
"2",
".",
"340",
".",
"500067",
".",
"8",
".",
"9",
".",
"10",
".",
"11012",
".",
"13000001401516017181900000200",
"]",
":",
"param",
"d",
":",
"Directory",
"name",
":",
"type",
"d",
":",
"str",
":",
"param",
"recursive",
":",
"Search",
"recursively",
":",
"type",
"recursive",
":",
"bool",
":",
"param",
"store_meta",
":",
"Read",
"and",
"store",
"metadata",
"for",
"each",
"file",
"for",
"fast",
"lookups",
":",
"type",
"store_meta",
":",
"bool",
":",
"returns",
":",
"Dictionary",
"of",
"{",
"StudyInstanceUID",
":",
"{",
"SeriesNumber",
":",
"[",
"files",
"]",
"}}",
":",
"rtype",
":",
"dict"
] |
train
|
https://github.com/harvard-nrg/yaxil/blob/af594082258e62d1904d6e6841fce0bb5c0bf309/yaxil/dicom/__init__.py#L14-L46
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/forms/__init__.py
|
replace_default_error_messages
|
def replace_default_error_messages():
"""
Replace Django's generic error messages with MTP-specific versions
NB: avoid trailing full stops visually, they are added for screen readers in templates
"""
forms.Field.default_error_messages['required'] = _('This field is required')
forms.CharField.default_error_messages['min_length'] = _('You’ve entered too few characters')
forms.CharField.default_error_messages['max_length'] = _('You’ve entered too many characters')
forms.IntegerField.default_error_messages['invalid'] = _('Enter a whole number')
forms.FloatField.default_error_messages['invalid'] = _('Enter a number')
forms.DecimalField.default_error_messages['invalid'] = _('Enter a number')
forms.DateField.default_error_messages['invalid'] = _('Enter a valid date')
forms.TimeField.default_error_messages['invalid'] = _('Enter a valid time')
forms.DateTimeField.default_error_messages['invalid'] = _('Enter a valid date and time')
forms.FileField.default_error_messages.update({
'invalid': _('No file was submitted'),
'missing': _('No file was submitted'),
'empty': _('The submitted file is empty'),
})
forms.SplitDateTimeField.default_error_messages['invalid_date'] = _('Enter a valid date')
forms.SplitDateTimeField.default_error_messages['invalid_time'] = _('Enter a valid time')
validators.EmailValidator.message = _('Enter a valid email address')
|
python
|
def replace_default_error_messages():
"""
Replace Django's generic error messages with MTP-specific versions
NB: avoid trailing full stops visually, they are added for screen readers in templates
"""
forms.Field.default_error_messages['required'] = _('This field is required')
forms.CharField.default_error_messages['min_length'] = _('You’ve entered too few characters')
forms.CharField.default_error_messages['max_length'] = _('You’ve entered too many characters')
forms.IntegerField.default_error_messages['invalid'] = _('Enter a whole number')
forms.FloatField.default_error_messages['invalid'] = _('Enter a number')
forms.DecimalField.default_error_messages['invalid'] = _('Enter a number')
forms.DateField.default_error_messages['invalid'] = _('Enter a valid date')
forms.TimeField.default_error_messages['invalid'] = _('Enter a valid time')
forms.DateTimeField.default_error_messages['invalid'] = _('Enter a valid date and time')
forms.FileField.default_error_messages.update({
'invalid': _('No file was submitted'),
'missing': _('No file was submitted'),
'empty': _('The submitted file is empty'),
})
forms.SplitDateTimeField.default_error_messages['invalid_date'] = _('Enter a valid date')
forms.SplitDateTimeField.default_error_messages['invalid_time'] = _('Enter a valid time')
validators.EmailValidator.message = _('Enter a valid email address')
|
[
"def",
"replace_default_error_messages",
"(",
")",
":",
"forms",
".",
"Field",
".",
"default_error_messages",
"[",
"'required'",
"]",
"=",
"_",
"(",
"'This field is required'",
")",
"forms",
".",
"CharField",
".",
"default_error_messages",
"[",
"'min_length'",
"]",
"=",
"_",
"(",
"'You’ve entered too few characters')",
"",
"forms",
".",
"CharField",
".",
"default_error_messages",
"[",
"'max_length'",
"]",
"=",
"_",
"(",
"'You’ve entered too many characters')",
"",
"forms",
".",
"IntegerField",
".",
"default_error_messages",
"[",
"'invalid'",
"]",
"=",
"_",
"(",
"'Enter a whole number'",
")",
"forms",
".",
"FloatField",
".",
"default_error_messages",
"[",
"'invalid'",
"]",
"=",
"_",
"(",
"'Enter a number'",
")",
"forms",
".",
"DecimalField",
".",
"default_error_messages",
"[",
"'invalid'",
"]",
"=",
"_",
"(",
"'Enter a number'",
")",
"forms",
".",
"DateField",
".",
"default_error_messages",
"[",
"'invalid'",
"]",
"=",
"_",
"(",
"'Enter a valid date'",
")",
"forms",
".",
"TimeField",
".",
"default_error_messages",
"[",
"'invalid'",
"]",
"=",
"_",
"(",
"'Enter a valid time'",
")",
"forms",
".",
"DateTimeField",
".",
"default_error_messages",
"[",
"'invalid'",
"]",
"=",
"_",
"(",
"'Enter a valid date and time'",
")",
"forms",
".",
"FileField",
".",
"default_error_messages",
".",
"update",
"(",
"{",
"'invalid'",
":",
"_",
"(",
"'No file was submitted'",
")",
",",
"'missing'",
":",
"_",
"(",
"'No file was submitted'",
")",
",",
"'empty'",
":",
"_",
"(",
"'The submitted file is empty'",
")",
",",
"}",
")",
"forms",
".",
"SplitDateTimeField",
".",
"default_error_messages",
"[",
"'invalid_date'",
"]",
"=",
"_",
"(",
"'Enter a valid date'",
")",
"forms",
".",
"SplitDateTimeField",
".",
"default_error_messages",
"[",
"'invalid_time'",
"]",
"=",
"_",
"(",
"'Enter a valid time'",
")",
"validators",
".",
"EmailValidator",
".",
"message",
"=",
"_",
"(",
"'Enter a valid email address'",
")"
] |
Replace Django's generic error messages with MTP-specific versions
NB: avoid trailing full stops visually, they are added for screen readers in templates
|
[
"Replace",
"Django",
"s",
"generic",
"error",
"messages",
"with",
"MTP",
"-",
"specific",
"versions",
"NB",
":",
"avoid",
"trailing",
"full",
"stops",
"visually",
"they",
"are",
"added",
"for",
"screen",
"readers",
"in",
"templates"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/forms/__init__.py#L6-L27
|
koreyou/word_embedding_loader
|
word_embedding_loader/loader/vocab.py
|
load_vocab
|
def load_vocab(fin):
"""
Load vocabulary from vocab file created by word2vec with
``-save-vocab <file>`` option.
Args:
fin (File): File-like object to read from.
encoding (bytes): Encoding of the input file as defined in ``codecs``
module of Python standard library.
errors (bytes): Set the error handling scheme. The default error
handler is 'strict' meaning that encoding errors raise ValueError.
Refer to ``codecs`` module for more information.
Returns:
OrderedDict: Mapping from a word (``bytes``) to the number of
appearance in the original text (``int``). Order are preserved from
the original vocab file.
"""
vocab = OrderedDict()
for line in fin:
v, c = line.strip().split()
vocab[v] = int(c)
return vocab
|
python
|
def load_vocab(fin):
"""
Load vocabulary from vocab file created by word2vec with
``-save-vocab <file>`` option.
Args:
fin (File): File-like object to read from.
encoding (bytes): Encoding of the input file as defined in ``codecs``
module of Python standard library.
errors (bytes): Set the error handling scheme. The default error
handler is 'strict' meaning that encoding errors raise ValueError.
Refer to ``codecs`` module for more information.
Returns:
OrderedDict: Mapping from a word (``bytes``) to the number of
appearance in the original text (``int``). Order are preserved from
the original vocab file.
"""
vocab = OrderedDict()
for line in fin:
v, c = line.strip().split()
vocab[v] = int(c)
return vocab
|
[
"def",
"load_vocab",
"(",
"fin",
")",
":",
"vocab",
"=",
"OrderedDict",
"(",
")",
"for",
"line",
"in",
"fin",
":",
"v",
",",
"c",
"=",
"line",
".",
"strip",
"(",
")",
".",
"split",
"(",
")",
"vocab",
"[",
"v",
"]",
"=",
"int",
"(",
"c",
")",
"return",
"vocab"
] |
Load vocabulary from vocab file created by word2vec with
``-save-vocab <file>`` option.
Args:
fin (File): File-like object to read from.
encoding (bytes): Encoding of the input file as defined in ``codecs``
module of Python standard library.
errors (bytes): Set the error handling scheme. The default error
handler is 'strict' meaning that encoding errors raise ValueError.
Refer to ``codecs`` module for more information.
Returns:
OrderedDict: Mapping from a word (``bytes``) to the number of
appearance in the original text (``int``). Order are preserved from
the original vocab file.
|
[
"Load",
"vocabulary",
"from",
"vocab",
"file",
"created",
"by",
"word2vec",
"with",
"-",
"save",
"-",
"vocab",
"<file",
">",
"option",
"."
] |
train
|
https://github.com/koreyou/word_embedding_loader/blob/1bc123f1a8bea12646576dcd768dae3ecea39c06/word_embedding_loader/loader/vocab.py#L8-L30
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/auth/api_client.py
|
authenticate
|
def authenticate(username, password):
"""
Returns:
a dict with:
pk: the pk of the user
token: dict containing all the data from the api
(access_token, refresh_token, expires_at etc.)
user_data: dict containing user data such as
first_name, last_name etc.
if the authentication succeeds
Raises Unauthorized if the authentication fails
"""
session = MoJOAuth2Session(
client=LegacyApplicationClient(
client_id=settings.API_CLIENT_ID
)
)
token = session.fetch_token(
token_url=get_request_token_url(),
username=username,
password=password,
auth=HTTPBasicAuth(settings.API_CLIENT_ID, settings.API_CLIENT_SECRET),
timeout=15,
encoding='utf-8'
)
user_data = session.get('/users/{username}/'.format(username=username)).json()
return {
'pk': user_data.get('pk'),
'token': token,
'user_data': user_data
}
|
python
|
def authenticate(username, password):
"""
Returns:
a dict with:
pk: the pk of the user
token: dict containing all the data from the api
(access_token, refresh_token, expires_at etc.)
user_data: dict containing user data such as
first_name, last_name etc.
if the authentication succeeds
Raises Unauthorized if the authentication fails
"""
session = MoJOAuth2Session(
client=LegacyApplicationClient(
client_id=settings.API_CLIENT_ID
)
)
token = session.fetch_token(
token_url=get_request_token_url(),
username=username,
password=password,
auth=HTTPBasicAuth(settings.API_CLIENT_ID, settings.API_CLIENT_SECRET),
timeout=15,
encoding='utf-8'
)
user_data = session.get('/users/{username}/'.format(username=username)).json()
return {
'pk': user_data.get('pk'),
'token': token,
'user_data': user_data
}
|
[
"def",
"authenticate",
"(",
"username",
",",
"password",
")",
":",
"session",
"=",
"MoJOAuth2Session",
"(",
"client",
"=",
"LegacyApplicationClient",
"(",
"client_id",
"=",
"settings",
".",
"API_CLIENT_ID",
")",
")",
"token",
"=",
"session",
".",
"fetch_token",
"(",
"token_url",
"=",
"get_request_token_url",
"(",
")",
",",
"username",
"=",
"username",
",",
"password",
"=",
"password",
",",
"auth",
"=",
"HTTPBasicAuth",
"(",
"settings",
".",
"API_CLIENT_ID",
",",
"settings",
".",
"API_CLIENT_SECRET",
")",
",",
"timeout",
"=",
"15",
",",
"encoding",
"=",
"'utf-8'",
")",
"user_data",
"=",
"session",
".",
"get",
"(",
"'/users/{username}/'",
".",
"format",
"(",
"username",
"=",
"username",
")",
")",
".",
"json",
"(",
")",
"return",
"{",
"'pk'",
":",
"user_data",
".",
"get",
"(",
"'pk'",
")",
",",
"'token'",
":",
"token",
",",
"'user_data'",
":",
"user_data",
"}"
] |
Returns:
a dict with:
pk: the pk of the user
token: dict containing all the data from the api
(access_token, refresh_token, expires_at etc.)
user_data: dict containing user data such as
first_name, last_name etc.
if the authentication succeeds
Raises Unauthorized if the authentication fails
|
[
"Returns",
":",
"a",
"dict",
"with",
":",
"pk",
":",
"the",
"pk",
"of",
"the",
"user",
"token",
":",
"dict",
"containing",
"all",
"the",
"data",
"from",
"the",
"api",
"(",
"access_token",
"refresh_token",
"expires_at",
"etc",
".",
")",
"user_data",
":",
"dict",
"containing",
"user",
"data",
"such",
"as",
"first_name",
"last_name",
"etc",
".",
"if",
"the",
"authentication",
"succeeds",
"Raises",
"Unauthorized",
"if",
"the",
"authentication",
"fails"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/auth/api_client.py#L84-L117
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/auth/api_client.py
|
revoke_token
|
def revoke_token(access_token):
"""
Instructs the API to delete this access token and associated refresh token
"""
response = requests.post(
get_revoke_token_url(),
data={
'token': access_token,
'client_id': settings.API_CLIENT_ID,
'client_secret': settings.API_CLIENT_SECRET,
},
timeout=15
)
return response.status_code == 200
|
python
|
def revoke_token(access_token):
"""
Instructs the API to delete this access token and associated refresh token
"""
response = requests.post(
get_revoke_token_url(),
data={
'token': access_token,
'client_id': settings.API_CLIENT_ID,
'client_secret': settings.API_CLIENT_SECRET,
},
timeout=15
)
return response.status_code == 200
|
[
"def",
"revoke_token",
"(",
"access_token",
")",
":",
"response",
"=",
"requests",
".",
"post",
"(",
"get_revoke_token_url",
"(",
")",
",",
"data",
"=",
"{",
"'token'",
":",
"access_token",
",",
"'client_id'",
":",
"settings",
".",
"API_CLIENT_ID",
",",
"'client_secret'",
":",
"settings",
".",
"API_CLIENT_SECRET",
",",
"}",
",",
"timeout",
"=",
"15",
")",
"return",
"response",
".",
"status_code",
"==",
"200"
] |
Instructs the API to delete this access token and associated refresh token
|
[
"Instructs",
"the",
"API",
"to",
"delete",
"this",
"access",
"token",
"and",
"associated",
"refresh",
"token"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/auth/api_client.py#L120-L133
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/auth/api_client.py
|
get_authenticated_api_session
|
def get_authenticated_api_session(username, password):
"""
:return: an authenticated api session
"""
session = MoJOAuth2Session(
client=LegacyApplicationClient(
client_id=settings.API_CLIENT_ID
)
)
session.fetch_token(
token_url=get_request_token_url(),
username=username,
password=password,
auth=HTTPBasicAuth(settings.API_CLIENT_ID, settings.API_CLIENT_SECRET),
timeout=15,
encoding='utf-8'
)
return session
|
python
|
def get_authenticated_api_session(username, password):
"""
:return: an authenticated api session
"""
session = MoJOAuth2Session(
client=LegacyApplicationClient(
client_id=settings.API_CLIENT_ID
)
)
session.fetch_token(
token_url=get_request_token_url(),
username=username,
password=password,
auth=HTTPBasicAuth(settings.API_CLIENT_ID, settings.API_CLIENT_SECRET),
timeout=15,
encoding='utf-8'
)
return session
|
[
"def",
"get_authenticated_api_session",
"(",
"username",
",",
"password",
")",
":",
"session",
"=",
"MoJOAuth2Session",
"(",
"client",
"=",
"LegacyApplicationClient",
"(",
"client_id",
"=",
"settings",
".",
"API_CLIENT_ID",
")",
")",
"session",
".",
"fetch_token",
"(",
"token_url",
"=",
"get_request_token_url",
"(",
")",
",",
"username",
"=",
"username",
",",
"password",
"=",
"password",
",",
"auth",
"=",
"HTTPBasicAuth",
"(",
"settings",
".",
"API_CLIENT_ID",
",",
"settings",
".",
"API_CLIENT_SECRET",
")",
",",
"timeout",
"=",
"15",
",",
"encoding",
"=",
"'utf-8'",
")",
"return",
"session"
] |
:return: an authenticated api session
|
[
":",
"return",
":",
"an",
"authenticated",
"api",
"session"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/auth/api_client.py#L162-L180
|
harvard-nrg/yaxil
|
yaxil/functools/__init__.py
|
lru_cache
|
def lru_cache(fn):
'''
Memoization wrapper that can handle function attributes, mutable arguments,
and can be applied either as a decorator or at runtime.
:param fn: Function
:type fn: function
:returns: Memoized function
:rtype: function
'''
@wraps(fn)
def memoized_fn(*args):
pargs = pickle.dumps(args)
if pargs not in memoized_fn.cache:
memoized_fn.cache[pargs] = fn(*args)
return memoized_fn.cache[pargs]
# propagate function attributes in the event that
# this is applied as a function and not a decorator
for attr, value in iter(fn.__dict__.items()):
setattr(memoized_fn, attr, value)
memoized_fn.cache = {}
return memoized_fn
|
python
|
def lru_cache(fn):
'''
Memoization wrapper that can handle function attributes, mutable arguments,
and can be applied either as a decorator or at runtime.
:param fn: Function
:type fn: function
:returns: Memoized function
:rtype: function
'''
@wraps(fn)
def memoized_fn(*args):
pargs = pickle.dumps(args)
if pargs not in memoized_fn.cache:
memoized_fn.cache[pargs] = fn(*args)
return memoized_fn.cache[pargs]
# propagate function attributes in the event that
# this is applied as a function and not a decorator
for attr, value in iter(fn.__dict__.items()):
setattr(memoized_fn, attr, value)
memoized_fn.cache = {}
return memoized_fn
|
[
"def",
"lru_cache",
"(",
"fn",
")",
":",
"@",
"wraps",
"(",
"fn",
")",
"def",
"memoized_fn",
"(",
"*",
"args",
")",
":",
"pargs",
"=",
"pickle",
".",
"dumps",
"(",
"args",
")",
"if",
"pargs",
"not",
"in",
"memoized_fn",
".",
"cache",
":",
"memoized_fn",
".",
"cache",
"[",
"pargs",
"]",
"=",
"fn",
"(",
"*",
"args",
")",
"return",
"memoized_fn",
".",
"cache",
"[",
"pargs",
"]",
"# propagate function attributes in the event that",
"# this is applied as a function and not a decorator",
"for",
"attr",
",",
"value",
"in",
"iter",
"(",
"fn",
".",
"__dict__",
".",
"items",
"(",
")",
")",
":",
"setattr",
"(",
"memoized_fn",
",",
"attr",
",",
"value",
")",
"memoized_fn",
".",
"cache",
"=",
"{",
"}",
"return",
"memoized_fn"
] |
Memoization wrapper that can handle function attributes, mutable arguments,
and can be applied either as a decorator or at runtime.
:param fn: Function
:type fn: function
:returns: Memoized function
:rtype: function
|
[
"Memoization",
"wrapper",
"that",
"can",
"handle",
"function",
"attributes",
"mutable",
"arguments",
"and",
"can",
"be",
"applied",
"either",
"as",
"a",
"decorator",
"or",
"at",
"runtime",
"."
] |
train
|
https://github.com/harvard-nrg/yaxil/blob/af594082258e62d1904d6e6841fce0bb5c0bf309/yaxil/functools/__init__.py#L4-L25
|
koreyou/word_embedding_loader
|
word_embedding_loader/loader/word2vec_text.py
|
check_valid
|
def check_valid(line0, line1):
"""
Check :func:`word_embedding_loader.loader.glove.check_valid` for the API.
"""
data0 = line0.split(b' ')
if len(data0) != 2:
return False
# check if data0 is int values
try:
map(int, data0)
_parse_line(line1, float)
except:
return False
return True
|
python
|
def check_valid(line0, line1):
"""
Check :func:`word_embedding_loader.loader.glove.check_valid` for the API.
"""
data0 = line0.split(b' ')
if len(data0) != 2:
return False
# check if data0 is int values
try:
map(int, data0)
_parse_line(line1, float)
except:
return False
return True
|
[
"def",
"check_valid",
"(",
"line0",
",",
"line1",
")",
":",
"data0",
"=",
"line0",
".",
"split",
"(",
"b' '",
")",
"if",
"len",
"(",
"data0",
")",
"!=",
"2",
":",
"return",
"False",
"# check if data0 is int values",
"try",
":",
"map",
"(",
"int",
",",
"data0",
")",
"_parse_line",
"(",
"line1",
",",
"float",
")",
"except",
":",
"return",
"False",
"return",
"True"
] |
Check :func:`word_embedding_loader.loader.glove.check_valid` for the API.
|
[
"Check",
":",
"func",
":",
"word_embedding_loader",
".",
"loader",
".",
"glove",
".",
"check_valid",
"for",
"the",
"API",
"."
] |
train
|
https://github.com/koreyou/word_embedding_loader/blob/1bc123f1a8bea12646576dcd768dae3ecea39c06/word_embedding_loader/loader/word2vec_text.py#L17-L30
|
koreyou/word_embedding_loader
|
word_embedding_loader/loader/word2vec_text.py
|
load_with_vocab
|
def load_with_vocab(fin, vocab, dtype=np.float32):
"""
Refer to :func:`word_embedding_loader.loader.glove.load_with_vocab` for the API.
"""
line = next(fin)
data = line.strip().split(b' ')
assert len(data) == 2
size = int(data[1])
arr = np.empty((len(vocab), size), dtype=dtype)
arr.fill(np.NaN)
for line in fin:
token, v = _load_line(line, dtype, size)
if token in vocab:
arr[vocab[token], :] = v
if np.any(np.isnan(arr)):
raise ParseError(b"Some of vocab was not found in word embedding file")
return arr
|
python
|
def load_with_vocab(fin, vocab, dtype=np.float32):
"""
Refer to :func:`word_embedding_loader.loader.glove.load_with_vocab` for the API.
"""
line = next(fin)
data = line.strip().split(b' ')
assert len(data) == 2
size = int(data[1])
arr = np.empty((len(vocab), size), dtype=dtype)
arr.fill(np.NaN)
for line in fin:
token, v = _load_line(line, dtype, size)
if token in vocab:
arr[vocab[token], :] = v
if np.any(np.isnan(arr)):
raise ParseError(b"Some of vocab was not found in word embedding file")
return arr
|
[
"def",
"load_with_vocab",
"(",
"fin",
",",
"vocab",
",",
"dtype",
"=",
"np",
".",
"float32",
")",
":",
"line",
"=",
"next",
"(",
"fin",
")",
"data",
"=",
"line",
".",
"strip",
"(",
")",
".",
"split",
"(",
"b' '",
")",
"assert",
"len",
"(",
"data",
")",
"==",
"2",
"size",
"=",
"int",
"(",
"data",
"[",
"1",
"]",
")",
"arr",
"=",
"np",
".",
"empty",
"(",
"(",
"len",
"(",
"vocab",
")",
",",
"size",
")",
",",
"dtype",
"=",
"dtype",
")",
"arr",
".",
"fill",
"(",
"np",
".",
"NaN",
")",
"for",
"line",
"in",
"fin",
":",
"token",
",",
"v",
"=",
"_load_line",
"(",
"line",
",",
"dtype",
",",
"size",
")",
"if",
"token",
"in",
"vocab",
":",
"arr",
"[",
"vocab",
"[",
"token",
"]",
",",
":",
"]",
"=",
"v",
"if",
"np",
".",
"any",
"(",
"np",
".",
"isnan",
"(",
"arr",
")",
")",
":",
"raise",
"ParseError",
"(",
"b\"Some of vocab was not found in word embedding file\"",
")",
"return",
"arr"
] |
Refer to :func:`word_embedding_loader.loader.glove.load_with_vocab` for the API.
|
[
"Refer",
"to",
":",
"func",
":",
"word_embedding_loader",
".",
"loader",
".",
"glove",
".",
"load_with_vocab",
"for",
"the",
"API",
"."
] |
train
|
https://github.com/koreyou/word_embedding_loader/blob/1bc123f1a8bea12646576dcd768dae3ecea39c06/word_embedding_loader/loader/word2vec_text.py#L52-L68
|
koreyou/word_embedding_loader
|
word_embedding_loader/loader/word2vec_text.py
|
load
|
def load(fin, dtype=np.float32, max_vocab=None):
"""
Refer to :func:`word_embedding_loader.loader.glove.load` for the API.
"""
vocab = {}
line = next(fin)
data = line.strip().split(b' ')
assert len(data) == 2
words = int(data[0])
if max_vocab is not None:
words = min(max_vocab, words)
size = int(data[1])
arr = np.empty((words, size), dtype=dtype)
i = 0
for n_line, line in enumerate(fin):
if i >= words:
break
token, v = _load_line(line, dtype, size)
if token in vocab:
parse_warn(b'Duplicated vocabulary ' + token)
continue
arr[i, :] = v
vocab[token] = i
i += 1
if i != words:
# Use + instead of formatting because python 3.4.* does not allow
# format with bytes
parse_warn(
b'EOF before the defined size (read ' + bytes(i) + b', expected '
+ bytes(words) + b')'
)
arr = arr[:i, :]
return arr, vocab
|
python
|
def load(fin, dtype=np.float32, max_vocab=None):
"""
Refer to :func:`word_embedding_loader.loader.glove.load` for the API.
"""
vocab = {}
line = next(fin)
data = line.strip().split(b' ')
assert len(data) == 2
words = int(data[0])
if max_vocab is not None:
words = min(max_vocab, words)
size = int(data[1])
arr = np.empty((words, size), dtype=dtype)
i = 0
for n_line, line in enumerate(fin):
if i >= words:
break
token, v = _load_line(line, dtype, size)
if token in vocab:
parse_warn(b'Duplicated vocabulary ' + token)
continue
arr[i, :] = v
vocab[token] = i
i += 1
if i != words:
# Use + instead of formatting because python 3.4.* does not allow
# format with bytes
parse_warn(
b'EOF before the defined size (read ' + bytes(i) + b', expected '
+ bytes(words) + b')'
)
arr = arr[:i, :]
return arr, vocab
|
[
"def",
"load",
"(",
"fin",
",",
"dtype",
"=",
"np",
".",
"float32",
",",
"max_vocab",
"=",
"None",
")",
":",
"vocab",
"=",
"{",
"}",
"line",
"=",
"next",
"(",
"fin",
")",
"data",
"=",
"line",
".",
"strip",
"(",
")",
".",
"split",
"(",
"b' '",
")",
"assert",
"len",
"(",
"data",
")",
"==",
"2",
"words",
"=",
"int",
"(",
"data",
"[",
"0",
"]",
")",
"if",
"max_vocab",
"is",
"not",
"None",
":",
"words",
"=",
"min",
"(",
"max_vocab",
",",
"words",
")",
"size",
"=",
"int",
"(",
"data",
"[",
"1",
"]",
")",
"arr",
"=",
"np",
".",
"empty",
"(",
"(",
"words",
",",
"size",
")",
",",
"dtype",
"=",
"dtype",
")",
"i",
"=",
"0",
"for",
"n_line",
",",
"line",
"in",
"enumerate",
"(",
"fin",
")",
":",
"if",
"i",
">=",
"words",
":",
"break",
"token",
",",
"v",
"=",
"_load_line",
"(",
"line",
",",
"dtype",
",",
"size",
")",
"if",
"token",
"in",
"vocab",
":",
"parse_warn",
"(",
"b'Duplicated vocabulary '",
"+",
"token",
")",
"continue",
"arr",
"[",
"i",
",",
":",
"]",
"=",
"v",
"vocab",
"[",
"token",
"]",
"=",
"i",
"i",
"+=",
"1",
"if",
"i",
"!=",
"words",
":",
"# Use + instead of formatting because python 3.4.* does not allow",
"# format with bytes",
"parse_warn",
"(",
"b'EOF before the defined size (read '",
"+",
"bytes",
"(",
"i",
")",
"+",
"b', expected '",
"+",
"bytes",
"(",
"words",
")",
"+",
"b')'",
")",
"arr",
"=",
"arr",
"[",
":",
"i",
",",
":",
"]",
"return",
"arr",
",",
"vocab"
] |
Refer to :func:`word_embedding_loader.loader.glove.load` for the API.
|
[
"Refer",
"to",
":",
"func",
":",
"word_embedding_loader",
".",
"loader",
".",
"glove",
".",
"load",
"for",
"the",
"API",
"."
] |
train
|
https://github.com/koreyou/word_embedding_loader/blob/1bc123f1a8bea12646576dcd768dae3ecea39c06/word_embedding_loader/loader/word2vec_text.py#L71-L103
|
harvard-nrg/yaxil
|
yaxil/__init__.py
|
auth
|
def auth(alias=None, url=None, cfg="~/.xnat_auth"):
'''
Read connection details from an xnat_auth XML file
Example:
>>> import yaxil
>>> auth = yaxil.auth('xnatastic')
>>> auth.url, auth.username, auth.password
('https://www.xnatastic.org/', 'username', '********')
:param alias: XNAT alias
:type alias: str
:param url: XNAT URL
:type url: str
:param cfg: Configuration file
:type cfg: str
:returns: Named tuple of (url, username, password)
:rtype: :mod:`yaxil.XnatAuth`
'''
if not alias and not url:
raise ValueError('you must provide an alias or url argument')
if alias and url:
raise ValueError('cannot provide both alias and url arguments')
# check and parse config file
cfg = os.path.expanduser(cfg)
if not os.path.exists(cfg):
raise AuthError("could not locate auth file %s" % cfg)
tree = etree.parse(os.path.expanduser(cfg))
# search by alias or url
res = None
if alias:
res = tree.findall("./%s" % alias)
if url:
res = tree.findall("./*/[url='%s']" % url)
if not res:
raise AuthError("failed to locate xnat credentials within %s" % cfg)
elif len(res) > 1:
raise AuthError("found too many sets of credentials within %s" % cfg)
res = res.pop()
# get url
url = res.findall("url")
if not url:
raise AuthError("no url for %s in %s" % (alias, cfg))
elif len(url) > 1:
raise AuthError("too many urls for %s in %s" % (alias, cfg))
# get username
username = res.findall("username")
if not username:
raise AuthError("no username for %s in %s" % (alias, cfg))
elif len(username) > 1:
raise AuthError("too many usernames for %s in %s" % (alias, cfg))
# get password
password = res.findall("password")
if not password:
raise AuthError("no password for %s in %s" % (alias, cfg))
elif len(password) > 1:
raise AuthError("too many passwords for %s in %s" % (alias, cfg))
return XnatAuth(url=url.pop().text, username=username.pop().text,
password=password.pop().text)
|
python
|
def auth(alias=None, url=None, cfg="~/.xnat_auth"):
'''
Read connection details from an xnat_auth XML file
Example:
>>> import yaxil
>>> auth = yaxil.auth('xnatastic')
>>> auth.url, auth.username, auth.password
('https://www.xnatastic.org/', 'username', '********')
:param alias: XNAT alias
:type alias: str
:param url: XNAT URL
:type url: str
:param cfg: Configuration file
:type cfg: str
:returns: Named tuple of (url, username, password)
:rtype: :mod:`yaxil.XnatAuth`
'''
if not alias and not url:
raise ValueError('you must provide an alias or url argument')
if alias and url:
raise ValueError('cannot provide both alias and url arguments')
# check and parse config file
cfg = os.path.expanduser(cfg)
if not os.path.exists(cfg):
raise AuthError("could not locate auth file %s" % cfg)
tree = etree.parse(os.path.expanduser(cfg))
# search by alias or url
res = None
if alias:
res = tree.findall("./%s" % alias)
if url:
res = tree.findall("./*/[url='%s']" % url)
if not res:
raise AuthError("failed to locate xnat credentials within %s" % cfg)
elif len(res) > 1:
raise AuthError("found too many sets of credentials within %s" % cfg)
res = res.pop()
# get url
url = res.findall("url")
if not url:
raise AuthError("no url for %s in %s" % (alias, cfg))
elif len(url) > 1:
raise AuthError("too many urls for %s in %s" % (alias, cfg))
# get username
username = res.findall("username")
if not username:
raise AuthError("no username for %s in %s" % (alias, cfg))
elif len(username) > 1:
raise AuthError("too many usernames for %s in %s" % (alias, cfg))
# get password
password = res.findall("password")
if not password:
raise AuthError("no password for %s in %s" % (alias, cfg))
elif len(password) > 1:
raise AuthError("too many passwords for %s in %s" % (alias, cfg))
return XnatAuth(url=url.pop().text, username=username.pop().text,
password=password.pop().text)
|
[
"def",
"auth",
"(",
"alias",
"=",
"None",
",",
"url",
"=",
"None",
",",
"cfg",
"=",
"\"~/.xnat_auth\"",
")",
":",
"if",
"not",
"alias",
"and",
"not",
"url",
":",
"raise",
"ValueError",
"(",
"'you must provide an alias or url argument'",
")",
"if",
"alias",
"and",
"url",
":",
"raise",
"ValueError",
"(",
"'cannot provide both alias and url arguments'",
")",
"# check and parse config file",
"cfg",
"=",
"os",
".",
"path",
".",
"expanduser",
"(",
"cfg",
")",
"if",
"not",
"os",
".",
"path",
".",
"exists",
"(",
"cfg",
")",
":",
"raise",
"AuthError",
"(",
"\"could not locate auth file %s\"",
"%",
"cfg",
")",
"tree",
"=",
"etree",
".",
"parse",
"(",
"os",
".",
"path",
".",
"expanduser",
"(",
"cfg",
")",
")",
"# search by alias or url",
"res",
"=",
"None",
"if",
"alias",
":",
"res",
"=",
"tree",
".",
"findall",
"(",
"\"./%s\"",
"%",
"alias",
")",
"if",
"url",
":",
"res",
"=",
"tree",
".",
"findall",
"(",
"\"./*/[url='%s']\"",
"%",
"url",
")",
"if",
"not",
"res",
":",
"raise",
"AuthError",
"(",
"\"failed to locate xnat credentials within %s\"",
"%",
"cfg",
")",
"elif",
"len",
"(",
"res",
")",
">",
"1",
":",
"raise",
"AuthError",
"(",
"\"found too many sets of credentials within %s\"",
"%",
"cfg",
")",
"res",
"=",
"res",
".",
"pop",
"(",
")",
"# get url",
"url",
"=",
"res",
".",
"findall",
"(",
"\"url\"",
")",
"if",
"not",
"url",
":",
"raise",
"AuthError",
"(",
"\"no url for %s in %s\"",
"%",
"(",
"alias",
",",
"cfg",
")",
")",
"elif",
"len",
"(",
"url",
")",
">",
"1",
":",
"raise",
"AuthError",
"(",
"\"too many urls for %s in %s\"",
"%",
"(",
"alias",
",",
"cfg",
")",
")",
"# get username",
"username",
"=",
"res",
".",
"findall",
"(",
"\"username\"",
")",
"if",
"not",
"username",
":",
"raise",
"AuthError",
"(",
"\"no username for %s in %s\"",
"%",
"(",
"alias",
",",
"cfg",
")",
")",
"elif",
"len",
"(",
"username",
")",
">",
"1",
":",
"raise",
"AuthError",
"(",
"\"too many usernames for %s in %s\"",
"%",
"(",
"alias",
",",
"cfg",
")",
")",
"# get password",
"password",
"=",
"res",
".",
"findall",
"(",
"\"password\"",
")",
"if",
"not",
"password",
":",
"raise",
"AuthError",
"(",
"\"no password for %s in %s\"",
"%",
"(",
"alias",
",",
"cfg",
")",
")",
"elif",
"len",
"(",
"password",
")",
">",
"1",
":",
"raise",
"AuthError",
"(",
"\"too many passwords for %s in %s\"",
"%",
"(",
"alias",
",",
"cfg",
")",
")",
"return",
"XnatAuth",
"(",
"url",
"=",
"url",
".",
"pop",
"(",
")",
".",
"text",
",",
"username",
"=",
"username",
".",
"pop",
"(",
")",
".",
"text",
",",
"password",
"=",
"password",
".",
"pop",
"(",
")",
".",
"text",
")"
] |
Read connection details from an xnat_auth XML file
Example:
>>> import yaxil
>>> auth = yaxil.auth('xnatastic')
>>> auth.url, auth.username, auth.password
('https://www.xnatastic.org/', 'username', '********')
:param alias: XNAT alias
:type alias: str
:param url: XNAT URL
:type url: str
:param cfg: Configuration file
:type cfg: str
:returns: Named tuple of (url, username, password)
:rtype: :mod:`yaxil.XnatAuth`
|
[
"Read",
"connection",
"details",
"from",
"an",
"xnat_auth",
"XML",
"file"
] |
train
|
https://github.com/harvard-nrg/yaxil/blob/af594082258e62d1904d6e6841fce0bb5c0bf309/yaxil/__init__.py#L80-L138
|
harvard-nrg/yaxil
|
yaxil/__init__.py
|
subjects
|
def subjects(auth, label=None, project=None):
'''
Retrieve Subject tuples for subjects returned by this function.
Example:
>>> import yaxil
>>> auth = yaxil.XnatAuth(url='...', username='...', password='...')
>>> yaxil.subjects(auth, 'AB1234C')
Subject(uri=u'/data/experiments/XNAT_S0001', label=u'AB1234C', id=u'XNAT_S0001',
project=u'MyProject')
:param auth: XNAT authentication
:type auth: :mod:`yaxil.XnatAuth`
:param label: XNAT Subject label
:type label: str
:param project: XNAT Subject Project
:type project: str
:returns: Subject objects
:rtype: :mod:`yaxil.Subject`
'''
url = '{0}/data/subjects'.format(auth.url.rstrip('/'))
logger.debug('issuing http request %s', url)
# compile query string
columns = [
'ID',
'label',
'project'
]
payload = {
'columns': ','.join(columns)
}
if label:
payload['label'] = label
if project:
payload['project'] = project
# submit the request
r = requests.get(url, params=payload, auth=(auth.username, auth.password),
verify=CHECK_CERTIFICATE)
# validate response
if r.status_code != requests.codes.ok:
raise AccessionError('response not ok ({0}) from {1}'.format(r.status_code, r.url))
try:
results = r.json()
__quick_validate(results)
except ResultSetError as e:
raise ResultSetError('{0} from {1}'.format(e.message, r.url))
results = results['ResultSet']
if int(results['totalRecords']) == 0:
raise NoSubjectsError('no records returned from {0}'.format(r.url))
# start generating consumable results for the caller
for item in results['Result']:
yield Subject(uri=item['URI'],
id=item['ID'],
project=item['project'],
label=item['label'])
|
python
|
def subjects(auth, label=None, project=None):
'''
Retrieve Subject tuples for subjects returned by this function.
Example:
>>> import yaxil
>>> auth = yaxil.XnatAuth(url='...', username='...', password='...')
>>> yaxil.subjects(auth, 'AB1234C')
Subject(uri=u'/data/experiments/XNAT_S0001', label=u'AB1234C', id=u'XNAT_S0001',
project=u'MyProject')
:param auth: XNAT authentication
:type auth: :mod:`yaxil.XnatAuth`
:param label: XNAT Subject label
:type label: str
:param project: XNAT Subject Project
:type project: str
:returns: Subject objects
:rtype: :mod:`yaxil.Subject`
'''
url = '{0}/data/subjects'.format(auth.url.rstrip('/'))
logger.debug('issuing http request %s', url)
# compile query string
columns = [
'ID',
'label',
'project'
]
payload = {
'columns': ','.join(columns)
}
if label:
payload['label'] = label
if project:
payload['project'] = project
# submit the request
r = requests.get(url, params=payload, auth=(auth.username, auth.password),
verify=CHECK_CERTIFICATE)
# validate response
if r.status_code != requests.codes.ok:
raise AccessionError('response not ok ({0}) from {1}'.format(r.status_code, r.url))
try:
results = r.json()
__quick_validate(results)
except ResultSetError as e:
raise ResultSetError('{0} from {1}'.format(e.message, r.url))
results = results['ResultSet']
if int(results['totalRecords']) == 0:
raise NoSubjectsError('no records returned from {0}'.format(r.url))
# start generating consumable results for the caller
for item in results['Result']:
yield Subject(uri=item['URI'],
id=item['ID'],
project=item['project'],
label=item['label'])
|
[
"def",
"subjects",
"(",
"auth",
",",
"label",
"=",
"None",
",",
"project",
"=",
"None",
")",
":",
"url",
"=",
"'{0}/data/subjects'",
".",
"format",
"(",
"auth",
".",
"url",
".",
"rstrip",
"(",
"'/'",
")",
")",
"logger",
".",
"debug",
"(",
"'issuing http request %s'",
",",
"url",
")",
"# compile query string",
"columns",
"=",
"[",
"'ID'",
",",
"'label'",
",",
"'project'",
"]",
"payload",
"=",
"{",
"'columns'",
":",
"','",
".",
"join",
"(",
"columns",
")",
"}",
"if",
"label",
":",
"payload",
"[",
"'label'",
"]",
"=",
"label",
"if",
"project",
":",
"payload",
"[",
"'project'",
"]",
"=",
"project",
"# submit the request",
"r",
"=",
"requests",
".",
"get",
"(",
"url",
",",
"params",
"=",
"payload",
",",
"auth",
"=",
"(",
"auth",
".",
"username",
",",
"auth",
".",
"password",
")",
",",
"verify",
"=",
"CHECK_CERTIFICATE",
")",
"# validate response",
"if",
"r",
".",
"status_code",
"!=",
"requests",
".",
"codes",
".",
"ok",
":",
"raise",
"AccessionError",
"(",
"'response not ok ({0}) from {1}'",
".",
"format",
"(",
"r",
".",
"status_code",
",",
"r",
".",
"url",
")",
")",
"try",
":",
"results",
"=",
"r",
".",
"json",
"(",
")",
"__quick_validate",
"(",
"results",
")",
"except",
"ResultSetError",
"as",
"e",
":",
"raise",
"ResultSetError",
"(",
"'{0} from {1}'",
".",
"format",
"(",
"e",
".",
"message",
",",
"r",
".",
"url",
")",
")",
"results",
"=",
"results",
"[",
"'ResultSet'",
"]",
"if",
"int",
"(",
"results",
"[",
"'totalRecords'",
"]",
")",
"==",
"0",
":",
"raise",
"NoSubjectsError",
"(",
"'no records returned from {0}'",
".",
"format",
"(",
"r",
".",
"url",
")",
")",
"# start generating consumable results for the caller",
"for",
"item",
"in",
"results",
"[",
"'Result'",
"]",
":",
"yield",
"Subject",
"(",
"uri",
"=",
"item",
"[",
"'URI'",
"]",
",",
"id",
"=",
"item",
"[",
"'ID'",
"]",
",",
"project",
"=",
"item",
"[",
"'project'",
"]",
",",
"label",
"=",
"item",
"[",
"'label'",
"]",
")"
] |
Retrieve Subject tuples for subjects returned by this function.
Example:
>>> import yaxil
>>> auth = yaxil.XnatAuth(url='...', username='...', password='...')
>>> yaxil.subjects(auth, 'AB1234C')
Subject(uri=u'/data/experiments/XNAT_S0001', label=u'AB1234C', id=u'XNAT_S0001',
project=u'MyProject')
:param auth: XNAT authentication
:type auth: :mod:`yaxil.XnatAuth`
:param label: XNAT Subject label
:type label: str
:param project: XNAT Subject Project
:type project: str
:returns: Subject objects
:rtype: :mod:`yaxil.Subject`
|
[
"Retrieve",
"Subject",
"tuples",
"for",
"subjects",
"returned",
"by",
"this",
"function",
".",
"Example",
":",
">>>",
"import",
"yaxil",
">>>",
"auth",
"=",
"yaxil",
".",
"XnatAuth",
"(",
"url",
"=",
"...",
"username",
"=",
"...",
"password",
"=",
"...",
")",
">>>",
"yaxil",
".",
"subjects",
"(",
"auth",
"AB1234C",
")",
"Subject",
"(",
"uri",
"=",
"u",
"/",
"data",
"/",
"experiments",
"/",
"XNAT_S0001",
"label",
"=",
"u",
"AB1234C",
"id",
"=",
"u",
"XNAT_S0001",
"project",
"=",
"u",
"MyProject",
")"
] |
train
|
https://github.com/harvard-nrg/yaxil/blob/af594082258e62d1904d6e6841fce0bb5c0bf309/yaxil/__init__.py#L151-L205
|
harvard-nrg/yaxil
|
yaxil/__init__.py
|
experiments
|
def experiments(auth, label=None, project=None, subject=None):
'''
Retrieve Experiment tuples for experiments returned by this function.
Example:
>>> import yaxil
>>> auth = yaxil.XnatAuth(url='...', username='...', password='...')
>>> yaxil.experiment(auth, 'AB1234C')
Experiment(uri=u'/data/experiments/XNAT_E0001', label=u'AB1234C', id=u'XNAT_E0001',
project=u'MyProject', subject_id=u'XNAT_S0001', subject_label='ABC')
:param auth: XNAT authentication
:type auth: :mod:`yaxil.XnatAuth`
:param label: XNAT Experiment label
:type label: str
:param project: XNAT Experiment Project
:type project: str
:param subject: YAXIL Subject
:type subject: :mod:`yaxil.Subject`
:returns: Experiment object
:rtype: :mod:`yaxil.Experiment`
'''
if subject and (label or project):
raise ValueError('cannot provide subject with label or project')
url = '{0}/data/experiments'.format(auth.url.rstrip('/'))
logger.debug('issuing http request %s', url)
# compile query string
columns = [
'ID',
'label',
'project',
'xnat:subjectassessordata/subject_id',
'subject_label',
'insert_date'
]
payload = {
'columns': ','.join(columns)
}
if label:
payload['label'] = label
if project:
payload['project'] = project
if subject:
payload['project'] = subject.project
payload['xnat:subjectassessordata/subject_id'] = subject.id
# submit request
r = requests.get(url, params=payload, auth=(auth.username, auth.password),
verify=CHECK_CERTIFICATE)
# validate response
if r.status_code != requests.codes.ok:
raise AccessionError('response not ok ({0}) from {1}'.format(r.status_code, r.url))
try:
results = r.json()
__quick_validate(results)
except ResultSetError as e:
raise ResultSetError('{0} from {1}'.format(e.message, r.url))
results = results['ResultSet']
if int(results['totalRecords']) == 0:
raise NoExperimentsError('no records returned for {0}'.format(r.url))
for item in results['Result']:
yield Experiment(uri=item['URI'],
id=item['ID'],
project=item['project'],
label=item['label'],
subject_id=item['subject_ID'],
subject_label=item['subject_label'],
archived_date=item['insert_date'])
|
python
|
def experiments(auth, label=None, project=None, subject=None):
'''
Retrieve Experiment tuples for experiments returned by this function.
Example:
>>> import yaxil
>>> auth = yaxil.XnatAuth(url='...', username='...', password='...')
>>> yaxil.experiment(auth, 'AB1234C')
Experiment(uri=u'/data/experiments/XNAT_E0001', label=u'AB1234C', id=u'XNAT_E0001',
project=u'MyProject', subject_id=u'XNAT_S0001', subject_label='ABC')
:param auth: XNAT authentication
:type auth: :mod:`yaxil.XnatAuth`
:param label: XNAT Experiment label
:type label: str
:param project: XNAT Experiment Project
:type project: str
:param subject: YAXIL Subject
:type subject: :mod:`yaxil.Subject`
:returns: Experiment object
:rtype: :mod:`yaxil.Experiment`
'''
if subject and (label or project):
raise ValueError('cannot provide subject with label or project')
url = '{0}/data/experiments'.format(auth.url.rstrip('/'))
logger.debug('issuing http request %s', url)
# compile query string
columns = [
'ID',
'label',
'project',
'xnat:subjectassessordata/subject_id',
'subject_label',
'insert_date'
]
payload = {
'columns': ','.join(columns)
}
if label:
payload['label'] = label
if project:
payload['project'] = project
if subject:
payload['project'] = subject.project
payload['xnat:subjectassessordata/subject_id'] = subject.id
# submit request
r = requests.get(url, params=payload, auth=(auth.username, auth.password),
verify=CHECK_CERTIFICATE)
# validate response
if r.status_code != requests.codes.ok:
raise AccessionError('response not ok ({0}) from {1}'.format(r.status_code, r.url))
try:
results = r.json()
__quick_validate(results)
except ResultSetError as e:
raise ResultSetError('{0} from {1}'.format(e.message, r.url))
results = results['ResultSet']
if int(results['totalRecords']) == 0:
raise NoExperimentsError('no records returned for {0}'.format(r.url))
for item in results['Result']:
yield Experiment(uri=item['URI'],
id=item['ID'],
project=item['project'],
label=item['label'],
subject_id=item['subject_ID'],
subject_label=item['subject_label'],
archived_date=item['insert_date'])
|
[
"def",
"experiments",
"(",
"auth",
",",
"label",
"=",
"None",
",",
"project",
"=",
"None",
",",
"subject",
"=",
"None",
")",
":",
"if",
"subject",
"and",
"(",
"label",
"or",
"project",
")",
":",
"raise",
"ValueError",
"(",
"'cannot provide subject with label or project'",
")",
"url",
"=",
"'{0}/data/experiments'",
".",
"format",
"(",
"auth",
".",
"url",
".",
"rstrip",
"(",
"'/'",
")",
")",
"logger",
".",
"debug",
"(",
"'issuing http request %s'",
",",
"url",
")",
"# compile query string",
"columns",
"=",
"[",
"'ID'",
",",
"'label'",
",",
"'project'",
",",
"'xnat:subjectassessordata/subject_id'",
",",
"'subject_label'",
",",
"'insert_date'",
"]",
"payload",
"=",
"{",
"'columns'",
":",
"','",
".",
"join",
"(",
"columns",
")",
"}",
"if",
"label",
":",
"payload",
"[",
"'label'",
"]",
"=",
"label",
"if",
"project",
":",
"payload",
"[",
"'project'",
"]",
"=",
"project",
"if",
"subject",
":",
"payload",
"[",
"'project'",
"]",
"=",
"subject",
".",
"project",
"payload",
"[",
"'xnat:subjectassessordata/subject_id'",
"]",
"=",
"subject",
".",
"id",
"# submit request",
"r",
"=",
"requests",
".",
"get",
"(",
"url",
",",
"params",
"=",
"payload",
",",
"auth",
"=",
"(",
"auth",
".",
"username",
",",
"auth",
".",
"password",
")",
",",
"verify",
"=",
"CHECK_CERTIFICATE",
")",
"# validate response",
"if",
"r",
".",
"status_code",
"!=",
"requests",
".",
"codes",
".",
"ok",
":",
"raise",
"AccessionError",
"(",
"'response not ok ({0}) from {1}'",
".",
"format",
"(",
"r",
".",
"status_code",
",",
"r",
".",
"url",
")",
")",
"try",
":",
"results",
"=",
"r",
".",
"json",
"(",
")",
"__quick_validate",
"(",
"results",
")",
"except",
"ResultSetError",
"as",
"e",
":",
"raise",
"ResultSetError",
"(",
"'{0} from {1}'",
".",
"format",
"(",
"e",
".",
"message",
",",
"r",
".",
"url",
")",
")",
"results",
"=",
"results",
"[",
"'ResultSet'",
"]",
"if",
"int",
"(",
"results",
"[",
"'totalRecords'",
"]",
")",
"==",
"0",
":",
"raise",
"NoExperimentsError",
"(",
"'no records returned for {0}'",
".",
"format",
"(",
"r",
".",
"url",
")",
")",
"for",
"item",
"in",
"results",
"[",
"'Result'",
"]",
":",
"yield",
"Experiment",
"(",
"uri",
"=",
"item",
"[",
"'URI'",
"]",
",",
"id",
"=",
"item",
"[",
"'ID'",
"]",
",",
"project",
"=",
"item",
"[",
"'project'",
"]",
",",
"label",
"=",
"item",
"[",
"'label'",
"]",
",",
"subject_id",
"=",
"item",
"[",
"'subject_ID'",
"]",
",",
"subject_label",
"=",
"item",
"[",
"'subject_label'",
"]",
",",
"archived_date",
"=",
"item",
"[",
"'insert_date'",
"]",
")"
] |
Retrieve Experiment tuples for experiments returned by this function.
Example:
>>> import yaxil
>>> auth = yaxil.XnatAuth(url='...', username='...', password='...')
>>> yaxil.experiment(auth, 'AB1234C')
Experiment(uri=u'/data/experiments/XNAT_E0001', label=u'AB1234C', id=u'XNAT_E0001',
project=u'MyProject', subject_id=u'XNAT_S0001', subject_label='ABC')
:param auth: XNAT authentication
:type auth: :mod:`yaxil.XnatAuth`
:param label: XNAT Experiment label
:type label: str
:param project: XNAT Experiment Project
:type project: str
:param subject: YAXIL Subject
:type subject: :mod:`yaxil.Subject`
:returns: Experiment object
:rtype: :mod:`yaxil.Experiment`
|
[
"Retrieve",
"Experiment",
"tuples",
"for",
"experiments",
"returned",
"by",
"this",
"function",
".",
"Example",
":",
">>>",
"import",
"yaxil",
">>>",
"auth",
"=",
"yaxil",
".",
"XnatAuth",
"(",
"url",
"=",
"...",
"username",
"=",
"...",
"password",
"=",
"...",
")",
">>>",
"yaxil",
".",
"experiment",
"(",
"auth",
"AB1234C",
")",
"Experiment",
"(",
"uri",
"=",
"u",
"/",
"data",
"/",
"experiments",
"/",
"XNAT_E0001",
"label",
"=",
"u",
"AB1234C",
"id",
"=",
"u",
"XNAT_E0001",
"project",
"=",
"u",
"MyProject",
"subject_id",
"=",
"u",
"XNAT_S0001",
"subject_label",
"=",
"ABC",
")"
] |
train
|
https://github.com/harvard-nrg/yaxil/blob/af594082258e62d1904d6e6841fce0bb5c0bf309/yaxil/__init__.py#L223-L289
|
harvard-nrg/yaxil
|
yaxil/__init__.py
|
accession
|
def accession(auth, label, project=None):
'''
Get the Accession ID for any Experiment label.
Example:
>>> import yaxil
>>> auth = yaxil.XnatAuth(url='...', username='...', password='...')
>>> yaxil.accession(auth, 'AB1234C')
u'XNAT_E00001'
:param auth: XNAT authentication
:type auth: :mod:`yaxil.XnatAuth`
:param label: XNAT Experiment label
:type label: str
:param project: XNAT Experiment Project
:type project: str
:returns: Accession ID
:rtype: str
'''
return list(experiments(auth, label, project))[0].id
|
python
|
def accession(auth, label, project=None):
'''
Get the Accession ID for any Experiment label.
Example:
>>> import yaxil
>>> auth = yaxil.XnatAuth(url='...', username='...', password='...')
>>> yaxil.accession(auth, 'AB1234C')
u'XNAT_E00001'
:param auth: XNAT authentication
:type auth: :mod:`yaxil.XnatAuth`
:param label: XNAT Experiment label
:type label: str
:param project: XNAT Experiment Project
:type project: str
:returns: Accession ID
:rtype: str
'''
return list(experiments(auth, label, project))[0].id
|
[
"def",
"accession",
"(",
"auth",
",",
"label",
",",
"project",
"=",
"None",
")",
":",
"return",
"list",
"(",
"experiments",
"(",
"auth",
",",
"label",
",",
"project",
")",
")",
"[",
"0",
"]",
".",
"id"
] |
Get the Accession ID for any Experiment label.
Example:
>>> import yaxil
>>> auth = yaxil.XnatAuth(url='...', username='...', password='...')
>>> yaxil.accession(auth, 'AB1234C')
u'XNAT_E00001'
:param auth: XNAT authentication
:type auth: :mod:`yaxil.XnatAuth`
:param label: XNAT Experiment label
:type label: str
:param project: XNAT Experiment Project
:type project: str
:returns: Accession ID
:rtype: str
|
[
"Get",
"the",
"Accession",
"ID",
"for",
"any",
"Experiment",
"label",
"."
] |
train
|
https://github.com/harvard-nrg/yaxil/blob/af594082258e62d1904d6e6841fce0bb5c0bf309/yaxil/__init__.py#L292-L311
|
harvard-nrg/yaxil
|
yaxil/__init__.py
|
download
|
def download(auth, label, scan_ids=None, project=None, aid=None,
out_dir='.', in_mem=True, progress=False, attempts=1):
'''
Download scan data from XNAT.
Example:
>>> import yaxil
>>> auth = yaxil.XnatAuth(url='...', username='...', password='...')
>>> yaxil.download(auth, 'AB1234C', ['1', '2'], out_dir='./data')
:param auth: XNAT authentication object
:type auth: :mod:`yaxil.XnatAuth`
:param label: XNAT MR Session label
:type label: str
:param scan_ids: Scan numbers to return; use None for all
:type scan_ids: list
:param project: XNAT MR Session project
:type project: str
:param aid: XNAT Accession ID
:type aid: str
:param out_dir: Output directory
:type out_dir: str
:param in_mem: Keep download content in memory; faster but uses more memory
:type in_mem: bool
:param progress: Show download progress every N bytes
:type progress: int
:param attempts: Number of download attempts
:type attempts: int
'''
if not scan_ids:
scan_ids = ['ALL']
if not aid:
aid = accession(auth, label, project)
# build the url
url = "%s/data/experiments/%s/scans/%s/files?format=zip" % (auth.url.rstrip('/'),
aid, ','.join([str(x) for x in scan_ids]))
# issue the http request, with exponential backoff retry behavior
backoff = 10
for _ in range(attempts):
logger.debug("issuing http request %s", url)
r = requests.get(url, stream=True, auth=(auth.username, auth.password), verify=CHECK_CERTIFICATE)
logger.debug("response headers %s", r.headers)
if r.status_code == requests.codes.ok:
break
fuzz = random.randint(0, 10)
logger.warn("download unsuccessful (%s), retrying in %s seconds", r.status_code,
backoff + fuzz)
time.sleep(backoff + fuzz)
backoff *= 2
# if we still have a not-ok status at this point, the download failed
if r.status_code != requests.codes.ok:
raise DownloadError("response not ok (%s) from %s" % (r.status_code, r.url))
# create output directory
if not os.path.exists(out_dir):
os.makedirs(out_dir)
# keep response content in memory or write to a file (memory is obviously faster, but limited)
if in_mem:
content = io.BytesIO()
logger.debug("response content will be read into memory")
else:
content = tf.NamedTemporaryFile(dir=out_dir, prefix="xnat", suffix=".zip")
logger.debug("response content will be stored on disk %s", content.name)
# progress indicator setup
if progress:
sys.stdout.write("reading response data: ")
sys.stdout.flush()
# read response content in chunks
meter = 0
chunk_size = 1024
for chunk in r.iter_content(chunk_size=chunk_size):
if progress and meter >= progress:
sys.stdout.write(next(commons.spinner)); sys.stdout.flush()
sys.stdout.write('\b')
meter = 0
content.write(chunk)
meter += chunk_size
# progress indicator shut down
if progress:
sys.stdout.write('done.\n'); sys.stdout.flush()
# load reponse content into a zipfile object
try:
zf = zipfile.ZipFile(content, allowZip64=True)
except zipfile.BadZipfile:
with tf.NamedTemporaryFile(dir=out_dir, prefix="xnat",
suffix=".zip", delete=False) as fo:
content.seek(0)
fo.write(content.read())
raise DownloadError("bad zip file, written to %s" % fo.name)
# finally extract the zipfile (with various nasty edge cases handled)
logger.debug("extracting zip archive to %s", out_dir)
extract(zf, content, out_dir)
|
python
|
def download(auth, label, scan_ids=None, project=None, aid=None,
out_dir='.', in_mem=True, progress=False, attempts=1):
'''
Download scan data from XNAT.
Example:
>>> import yaxil
>>> auth = yaxil.XnatAuth(url='...', username='...', password='...')
>>> yaxil.download(auth, 'AB1234C', ['1', '2'], out_dir='./data')
:param auth: XNAT authentication object
:type auth: :mod:`yaxil.XnatAuth`
:param label: XNAT MR Session label
:type label: str
:param scan_ids: Scan numbers to return; use None for all
:type scan_ids: list
:param project: XNAT MR Session project
:type project: str
:param aid: XNAT Accession ID
:type aid: str
:param out_dir: Output directory
:type out_dir: str
:param in_mem: Keep download content in memory; faster but uses more memory
:type in_mem: bool
:param progress: Show download progress every N bytes
:type progress: int
:param attempts: Number of download attempts
:type attempts: int
'''
if not scan_ids:
scan_ids = ['ALL']
if not aid:
aid = accession(auth, label, project)
# build the url
url = "%s/data/experiments/%s/scans/%s/files?format=zip" % (auth.url.rstrip('/'),
aid, ','.join([str(x) for x in scan_ids]))
# issue the http request, with exponential backoff retry behavior
backoff = 10
for _ in range(attempts):
logger.debug("issuing http request %s", url)
r = requests.get(url, stream=True, auth=(auth.username, auth.password), verify=CHECK_CERTIFICATE)
logger.debug("response headers %s", r.headers)
if r.status_code == requests.codes.ok:
break
fuzz = random.randint(0, 10)
logger.warn("download unsuccessful (%s), retrying in %s seconds", r.status_code,
backoff + fuzz)
time.sleep(backoff + fuzz)
backoff *= 2
# if we still have a not-ok status at this point, the download failed
if r.status_code != requests.codes.ok:
raise DownloadError("response not ok (%s) from %s" % (r.status_code, r.url))
# create output directory
if not os.path.exists(out_dir):
os.makedirs(out_dir)
# keep response content in memory or write to a file (memory is obviously faster, but limited)
if in_mem:
content = io.BytesIO()
logger.debug("response content will be read into memory")
else:
content = tf.NamedTemporaryFile(dir=out_dir, prefix="xnat", suffix=".zip")
logger.debug("response content will be stored on disk %s", content.name)
# progress indicator setup
if progress:
sys.stdout.write("reading response data: ")
sys.stdout.flush()
# read response content in chunks
meter = 0
chunk_size = 1024
for chunk in r.iter_content(chunk_size=chunk_size):
if progress and meter >= progress:
sys.stdout.write(next(commons.spinner)); sys.stdout.flush()
sys.stdout.write('\b')
meter = 0
content.write(chunk)
meter += chunk_size
# progress indicator shut down
if progress:
sys.stdout.write('done.\n'); sys.stdout.flush()
# load reponse content into a zipfile object
try:
zf = zipfile.ZipFile(content, allowZip64=True)
except zipfile.BadZipfile:
with tf.NamedTemporaryFile(dir=out_dir, prefix="xnat",
suffix=".zip", delete=False) as fo:
content.seek(0)
fo.write(content.read())
raise DownloadError("bad zip file, written to %s" % fo.name)
# finally extract the zipfile (with various nasty edge cases handled)
logger.debug("extracting zip archive to %s", out_dir)
extract(zf, content, out_dir)
|
[
"def",
"download",
"(",
"auth",
",",
"label",
",",
"scan_ids",
"=",
"None",
",",
"project",
"=",
"None",
",",
"aid",
"=",
"None",
",",
"out_dir",
"=",
"'.'",
",",
"in_mem",
"=",
"True",
",",
"progress",
"=",
"False",
",",
"attempts",
"=",
"1",
")",
":",
"if",
"not",
"scan_ids",
":",
"scan_ids",
"=",
"[",
"'ALL'",
"]",
"if",
"not",
"aid",
":",
"aid",
"=",
"accession",
"(",
"auth",
",",
"label",
",",
"project",
")",
"# build the url",
"url",
"=",
"\"%s/data/experiments/%s/scans/%s/files?format=zip\"",
"%",
"(",
"auth",
".",
"url",
".",
"rstrip",
"(",
"'/'",
")",
",",
"aid",
",",
"','",
".",
"join",
"(",
"[",
"str",
"(",
"x",
")",
"for",
"x",
"in",
"scan_ids",
"]",
")",
")",
"# issue the http request, with exponential backoff retry behavior",
"backoff",
"=",
"10",
"for",
"_",
"in",
"range",
"(",
"attempts",
")",
":",
"logger",
".",
"debug",
"(",
"\"issuing http request %s\"",
",",
"url",
")",
"r",
"=",
"requests",
".",
"get",
"(",
"url",
",",
"stream",
"=",
"True",
",",
"auth",
"=",
"(",
"auth",
".",
"username",
",",
"auth",
".",
"password",
")",
",",
"verify",
"=",
"CHECK_CERTIFICATE",
")",
"logger",
".",
"debug",
"(",
"\"response headers %s\"",
",",
"r",
".",
"headers",
")",
"if",
"r",
".",
"status_code",
"==",
"requests",
".",
"codes",
".",
"ok",
":",
"break",
"fuzz",
"=",
"random",
".",
"randint",
"(",
"0",
",",
"10",
")",
"logger",
".",
"warn",
"(",
"\"download unsuccessful (%s), retrying in %s seconds\"",
",",
"r",
".",
"status_code",
",",
"backoff",
"+",
"fuzz",
")",
"time",
".",
"sleep",
"(",
"backoff",
"+",
"fuzz",
")",
"backoff",
"*=",
"2",
"# if we still have a not-ok status at this point, the download failed",
"if",
"r",
".",
"status_code",
"!=",
"requests",
".",
"codes",
".",
"ok",
":",
"raise",
"DownloadError",
"(",
"\"response not ok (%s) from %s\"",
"%",
"(",
"r",
".",
"status_code",
",",
"r",
".",
"url",
")",
")",
"# create output directory",
"if",
"not",
"os",
".",
"path",
".",
"exists",
"(",
"out_dir",
")",
":",
"os",
".",
"makedirs",
"(",
"out_dir",
")",
"# keep response content in memory or write to a file (memory is obviously faster, but limited)",
"if",
"in_mem",
":",
"content",
"=",
"io",
".",
"BytesIO",
"(",
")",
"logger",
".",
"debug",
"(",
"\"response content will be read into memory\"",
")",
"else",
":",
"content",
"=",
"tf",
".",
"NamedTemporaryFile",
"(",
"dir",
"=",
"out_dir",
",",
"prefix",
"=",
"\"xnat\"",
",",
"suffix",
"=",
"\".zip\"",
")",
"logger",
".",
"debug",
"(",
"\"response content will be stored on disk %s\"",
",",
"content",
".",
"name",
")",
"# progress indicator setup",
"if",
"progress",
":",
"sys",
".",
"stdout",
".",
"write",
"(",
"\"reading response data: \"",
")",
"sys",
".",
"stdout",
".",
"flush",
"(",
")",
"# read response content in chunks",
"meter",
"=",
"0",
"chunk_size",
"=",
"1024",
"for",
"chunk",
"in",
"r",
".",
"iter_content",
"(",
"chunk_size",
"=",
"chunk_size",
")",
":",
"if",
"progress",
"and",
"meter",
">=",
"progress",
":",
"sys",
".",
"stdout",
".",
"write",
"(",
"next",
"(",
"commons",
".",
"spinner",
")",
")",
"sys",
".",
"stdout",
".",
"flush",
"(",
")",
"sys",
".",
"stdout",
".",
"write",
"(",
"'\\b'",
")",
"meter",
"=",
"0",
"content",
".",
"write",
"(",
"chunk",
")",
"meter",
"+=",
"chunk_size",
"# progress indicator shut down",
"if",
"progress",
":",
"sys",
".",
"stdout",
".",
"write",
"(",
"'done.\\n'",
")",
"sys",
".",
"stdout",
".",
"flush",
"(",
")",
"# load reponse content into a zipfile object",
"try",
":",
"zf",
"=",
"zipfile",
".",
"ZipFile",
"(",
"content",
",",
"allowZip64",
"=",
"True",
")",
"except",
"zipfile",
".",
"BadZipfile",
":",
"with",
"tf",
".",
"NamedTemporaryFile",
"(",
"dir",
"=",
"out_dir",
",",
"prefix",
"=",
"\"xnat\"",
",",
"suffix",
"=",
"\".zip\"",
",",
"delete",
"=",
"False",
")",
"as",
"fo",
":",
"content",
".",
"seek",
"(",
"0",
")",
"fo",
".",
"write",
"(",
"content",
".",
"read",
"(",
")",
")",
"raise",
"DownloadError",
"(",
"\"bad zip file, written to %s\"",
"%",
"fo",
".",
"name",
")",
"# finally extract the zipfile (with various nasty edge cases handled)",
"logger",
".",
"debug",
"(",
"\"extracting zip archive to %s\"",
",",
"out_dir",
")",
"extract",
"(",
"zf",
",",
"content",
",",
"out_dir",
")"
] |
Download scan data from XNAT.
Example:
>>> import yaxil
>>> auth = yaxil.XnatAuth(url='...', username='...', password='...')
>>> yaxil.download(auth, 'AB1234C', ['1', '2'], out_dir='./data')
:param auth: XNAT authentication object
:type auth: :mod:`yaxil.XnatAuth`
:param label: XNAT MR Session label
:type label: str
:param scan_ids: Scan numbers to return; use None for all
:type scan_ids: list
:param project: XNAT MR Session project
:type project: str
:param aid: XNAT Accession ID
:type aid: str
:param out_dir: Output directory
:type out_dir: str
:param in_mem: Keep download content in memory; faster but uses more memory
:type in_mem: bool
:param progress: Show download progress every N bytes
:type progress: int
:param attempts: Number of download attempts
:type attempts: int
|
[
"Download",
"scan",
"data",
"from",
"XNAT",
".",
"Example",
":",
">>>",
"import",
"yaxil",
">>>",
"auth",
"=",
"yaxil",
".",
"XnatAuth",
"(",
"url",
"=",
"...",
"username",
"=",
"...",
"password",
"=",
"...",
")",
">>>",
"yaxil",
".",
"download",
"(",
"auth",
"AB1234C",
"[",
"1",
"2",
"]",
"out_dir",
"=",
".",
"/",
"data",
")"
] |
train
|
https://github.com/harvard-nrg/yaxil/blob/af594082258e62d1904d6e6841fce0bb5c0bf309/yaxil/__init__.py#L322-L412
|
harvard-nrg/yaxil
|
yaxil/__init__.py
|
extract
|
def extract(zf, content, out_dir='.'):
'''
Extracting a Java 1.6 XNAT ZIP archive in Python.
:param zf: ZipFile object
:type zf: zipfile.ZipFile
:param out_dir: Output directory
:type out_dir: str
'''
previous_header_offset = 0
compensation = Namespace(value=2**32, factor=0)
for i,member in enumerate(zf.infolist()):
'''
Right... so when Java 1.6 produces a Zip filesystem that exceeds 2^32
bytes, the Central Directory local file header offsets after the 2^32
byte appear to overflow. The Python zipfile module then adds any
unexpected bytes to each header offset thereafter. This attempts to fix
that. My guess is that this comment might make perfect sense now, but
will make aboslutely no sense in about a year.
'''
# undo concat padding added from zipfile.py:819
if i == 0:
concat = member.header_offset
member.header_offset -= concat
# if a header offset moves backward, add 2^32 bytes * factor
if previous_header_offset > member.header_offset:
compensation.factor += 1
previous_header_offset = member.header_offset
member.header_offset += compensation.value * compensation.factor
# read the archive member into a bytes file-like object
try:
bio = io.BytesIO(zf.read(member.filename))
except zipfile.BadZipfile:
with tf.NamedTemporaryFile(dir=out_dir, prefix="xnat",
suffix=".zip", delete=False) as fo:
content.seek(0)
fo.write(content.read())
raise DownloadError("bad zip file, written to %s" % fo.name)
# xnat archives may contain files that are gzipped without the .gz
if not member.filename.endswith(".gz"):
try:
gz = gzip.GzipFile(fileobj=bio, mode="rb")
gz.read()
bio = gz
except IOError:
pass
# write the file out to the filesystem
bio.seek(0)
f = os.path.join(out_dir, os.path.basename(member.filename))
with open(f, "wb") as fo:
fo.write(bio.read())
|
python
|
def extract(zf, content, out_dir='.'):
'''
Extracting a Java 1.6 XNAT ZIP archive in Python.
:param zf: ZipFile object
:type zf: zipfile.ZipFile
:param out_dir: Output directory
:type out_dir: str
'''
previous_header_offset = 0
compensation = Namespace(value=2**32, factor=0)
for i,member in enumerate(zf.infolist()):
'''
Right... so when Java 1.6 produces a Zip filesystem that exceeds 2^32
bytes, the Central Directory local file header offsets after the 2^32
byte appear to overflow. The Python zipfile module then adds any
unexpected bytes to each header offset thereafter. This attempts to fix
that. My guess is that this comment might make perfect sense now, but
will make aboslutely no sense in about a year.
'''
# undo concat padding added from zipfile.py:819
if i == 0:
concat = member.header_offset
member.header_offset -= concat
# if a header offset moves backward, add 2^32 bytes * factor
if previous_header_offset > member.header_offset:
compensation.factor += 1
previous_header_offset = member.header_offset
member.header_offset += compensation.value * compensation.factor
# read the archive member into a bytes file-like object
try:
bio = io.BytesIO(zf.read(member.filename))
except zipfile.BadZipfile:
with tf.NamedTemporaryFile(dir=out_dir, prefix="xnat",
suffix=".zip", delete=False) as fo:
content.seek(0)
fo.write(content.read())
raise DownloadError("bad zip file, written to %s" % fo.name)
# xnat archives may contain files that are gzipped without the .gz
if not member.filename.endswith(".gz"):
try:
gz = gzip.GzipFile(fileobj=bio, mode="rb")
gz.read()
bio = gz
except IOError:
pass
# write the file out to the filesystem
bio.seek(0)
f = os.path.join(out_dir, os.path.basename(member.filename))
with open(f, "wb") as fo:
fo.write(bio.read())
|
[
"def",
"extract",
"(",
"zf",
",",
"content",
",",
"out_dir",
"=",
"'.'",
")",
":",
"previous_header_offset",
"=",
"0",
"compensation",
"=",
"Namespace",
"(",
"value",
"=",
"2",
"**",
"32",
",",
"factor",
"=",
"0",
")",
"for",
"i",
",",
"member",
"in",
"enumerate",
"(",
"zf",
".",
"infolist",
"(",
")",
")",
":",
"'''\n Right... so when Java 1.6 produces a Zip filesystem that exceeds 2^32\n bytes, the Central Directory local file header offsets after the 2^32 \n byte appear to overflow. The Python zipfile module then adds any \n unexpected bytes to each header offset thereafter. This attempts to fix \n that. My guess is that this comment might make perfect sense now, but \n will make aboslutely no sense in about a year.\n '''",
"# undo concat padding added from zipfile.py:819",
"if",
"i",
"==",
"0",
":",
"concat",
"=",
"member",
".",
"header_offset",
"member",
".",
"header_offset",
"-=",
"concat",
"# if a header offset moves backward, add 2^32 bytes * factor",
"if",
"previous_header_offset",
">",
"member",
".",
"header_offset",
":",
"compensation",
".",
"factor",
"+=",
"1",
"previous_header_offset",
"=",
"member",
".",
"header_offset",
"member",
".",
"header_offset",
"+=",
"compensation",
".",
"value",
"*",
"compensation",
".",
"factor",
"# read the archive member into a bytes file-like object",
"try",
":",
"bio",
"=",
"io",
".",
"BytesIO",
"(",
"zf",
".",
"read",
"(",
"member",
".",
"filename",
")",
")",
"except",
"zipfile",
".",
"BadZipfile",
":",
"with",
"tf",
".",
"NamedTemporaryFile",
"(",
"dir",
"=",
"out_dir",
",",
"prefix",
"=",
"\"xnat\"",
",",
"suffix",
"=",
"\".zip\"",
",",
"delete",
"=",
"False",
")",
"as",
"fo",
":",
"content",
".",
"seek",
"(",
"0",
")",
"fo",
".",
"write",
"(",
"content",
".",
"read",
"(",
")",
")",
"raise",
"DownloadError",
"(",
"\"bad zip file, written to %s\"",
"%",
"fo",
".",
"name",
")",
"# xnat archives may contain files that are gzipped without the .gz",
"if",
"not",
"member",
".",
"filename",
".",
"endswith",
"(",
"\".gz\"",
")",
":",
"try",
":",
"gz",
"=",
"gzip",
".",
"GzipFile",
"(",
"fileobj",
"=",
"bio",
",",
"mode",
"=",
"\"rb\"",
")",
"gz",
".",
"read",
"(",
")",
"bio",
"=",
"gz",
"except",
"IOError",
":",
"pass",
"# write the file out to the filesystem",
"bio",
".",
"seek",
"(",
"0",
")",
"f",
"=",
"os",
".",
"path",
".",
"join",
"(",
"out_dir",
",",
"os",
".",
"path",
".",
"basename",
"(",
"member",
".",
"filename",
")",
")",
"with",
"open",
"(",
"f",
",",
"\"wb\"",
")",
"as",
"fo",
":",
"fo",
".",
"write",
"(",
"bio",
".",
"read",
"(",
")",
")"
] |
Extracting a Java 1.6 XNAT ZIP archive in Python.
:param zf: ZipFile object
:type zf: zipfile.ZipFile
:param out_dir: Output directory
:type out_dir: str
|
[
"Extracting",
"a",
"Java",
"1",
".",
"6",
"XNAT",
"ZIP",
"archive",
"in",
"Python",
"."
] |
train
|
https://github.com/harvard-nrg/yaxil/blob/af594082258e62d1904d6e6841fce0bb5c0bf309/yaxil/__init__.py#L414-L464
|
harvard-nrg/yaxil
|
yaxil/__init__.py
|
__quick_validate
|
def __quick_validate(r, check=('ResultSet', 'Result', 'totalRecords')):
'''
Quick validation of JSON result set returned by XNAT.
:param r: Result set data in JSON format
:type r: dict
:param check: Fields to check
:type check: tuple
:returns: Result set is valid
:rtype: bool
'''
if 'ResultSet' in check and 'ResultSet' not in r:
raise ResultSetError('no ResultSet in server response')
if 'Result' in check and 'Result' not in r['ResultSet']:
raise ResultSetError('no Result in server response')
if 'totalRecords' in check and 'totalRecords' not in r['ResultSet']:
raise ResultSetError('no totalRecords in server response')
return True
|
python
|
def __quick_validate(r, check=('ResultSet', 'Result', 'totalRecords')):
'''
Quick validation of JSON result set returned by XNAT.
:param r: Result set data in JSON format
:type r: dict
:param check: Fields to check
:type check: tuple
:returns: Result set is valid
:rtype: bool
'''
if 'ResultSet' in check and 'ResultSet' not in r:
raise ResultSetError('no ResultSet in server response')
if 'Result' in check and 'Result' not in r['ResultSet']:
raise ResultSetError('no Result in server response')
if 'totalRecords' in check and 'totalRecords' not in r['ResultSet']:
raise ResultSetError('no totalRecords in server response')
return True
|
[
"def",
"__quick_validate",
"(",
"r",
",",
"check",
"=",
"(",
"'ResultSet'",
",",
"'Result'",
",",
"'totalRecords'",
")",
")",
":",
"if",
"'ResultSet'",
"in",
"check",
"and",
"'ResultSet'",
"not",
"in",
"r",
":",
"raise",
"ResultSetError",
"(",
"'no ResultSet in server response'",
")",
"if",
"'Result'",
"in",
"check",
"and",
"'Result'",
"not",
"in",
"r",
"[",
"'ResultSet'",
"]",
":",
"raise",
"ResultSetError",
"(",
"'no Result in server response'",
")",
"if",
"'totalRecords'",
"in",
"check",
"and",
"'totalRecords'",
"not",
"in",
"r",
"[",
"'ResultSet'",
"]",
":",
"raise",
"ResultSetError",
"(",
"'no totalRecords in server response'",
")",
"return",
"True"
] |
Quick validation of JSON result set returned by XNAT.
:param r: Result set data in JSON format
:type r: dict
:param check: Fields to check
:type check: tuple
:returns: Result set is valid
:rtype: bool
|
[
"Quick",
"validation",
"of",
"JSON",
"result",
"set",
"returned",
"by",
"XNAT",
"."
] |
train
|
https://github.com/harvard-nrg/yaxil/blob/af594082258e62d1904d6e6841fce0bb5c0bf309/yaxil/__init__.py#L466-L483
|
harvard-nrg/yaxil
|
yaxil/__init__.py
|
scansearch
|
def scansearch(auth, label, filt, project=None, aid=None):
'''
Search for scans by supplying a set of SQL-based conditionals.
Example:
>>> import yaxil
>>> auth = yaxil.XnatAuth(url='...', username='...', password='...')
>>> query = {
... 'eor1': "note LIKE %EOR1%",
... 'eor2': "note LIKE %EOR2%",
... 'mpr': "series_description='T1_MEMPRAGE RMS' OR note LIKE %ANAT%"
... }
>>> yaxil.scansearch(auth, 'AB1234C', query)
{"mpr": [4], "eor1": [13], "eor2": [14]}
:param auth: XNAT authentication object
:type auth: :mod:`yaxil.XnatAuth`
:param label: XNAT MR Session label
:type label: str
:param filt: Scan search filter/query
:type filt: dict
:param project: XNAT MR Session project
:type project: str
:param aid: XNAT Accession ID
:type aid: str
:returns: Same dictionary that was passed in, but values are now matching scans
:rtype: dict
'''
if not aid:
aid = accession(auth, label, project)
# get scans for accession as a csv
url = "%s/data/experiments/%s/scans?format=csv" % (auth.url.rstrip('/'), aid)
logger.debug("issuing http request %s", url)
r = requests.get(url, auth=(auth.username, auth.password), verify=CHECK_CERTIFICATE)
if r.status_code != requests.codes.ok:
raise ScanSearchError("response not ok (%s) from %s" % (r.status_code, r.url))
if not r.content:
raise ScanSearchError("response is empty from %s" % r.url)
# read the result into a csv reader
reader = csv.reader(io.StringIO(r.content.decode()))
columns = next(reader)
# create an in-memory database
conn = sqlite3.connect(":memory:")
c = conn.cursor()
# create scans table and insert data
c.execute("CREATE TABLE scans (%s)" % ','.join(columns))
query = "INSERT INTO scans VALUES (%s)" % ','.join('?' * len(columns))
for row in reader:
c.execute(query, [x for x in row])
conn.commit()
# run the user supplied filters and return result
result = col.defaultdict(list)
for token,filt in iter(filt.items()):
try:
result[token] = [x[0] for x in c.execute("SELECT ID FROM scans where %s" % filt)]
except sqlite3.OperationalError:
logger.critical("something is wrong with the filter: %s", filt)
raise
return result
|
python
|
def scansearch(auth, label, filt, project=None, aid=None):
'''
Search for scans by supplying a set of SQL-based conditionals.
Example:
>>> import yaxil
>>> auth = yaxil.XnatAuth(url='...', username='...', password='...')
>>> query = {
... 'eor1': "note LIKE %EOR1%",
... 'eor2': "note LIKE %EOR2%",
... 'mpr': "series_description='T1_MEMPRAGE RMS' OR note LIKE %ANAT%"
... }
>>> yaxil.scansearch(auth, 'AB1234C', query)
{"mpr": [4], "eor1": [13], "eor2": [14]}
:param auth: XNAT authentication object
:type auth: :mod:`yaxil.XnatAuth`
:param label: XNAT MR Session label
:type label: str
:param filt: Scan search filter/query
:type filt: dict
:param project: XNAT MR Session project
:type project: str
:param aid: XNAT Accession ID
:type aid: str
:returns: Same dictionary that was passed in, but values are now matching scans
:rtype: dict
'''
if not aid:
aid = accession(auth, label, project)
# get scans for accession as a csv
url = "%s/data/experiments/%s/scans?format=csv" % (auth.url.rstrip('/'), aid)
logger.debug("issuing http request %s", url)
r = requests.get(url, auth=(auth.username, auth.password), verify=CHECK_CERTIFICATE)
if r.status_code != requests.codes.ok:
raise ScanSearchError("response not ok (%s) from %s" % (r.status_code, r.url))
if not r.content:
raise ScanSearchError("response is empty from %s" % r.url)
# read the result into a csv reader
reader = csv.reader(io.StringIO(r.content.decode()))
columns = next(reader)
# create an in-memory database
conn = sqlite3.connect(":memory:")
c = conn.cursor()
# create scans table and insert data
c.execute("CREATE TABLE scans (%s)" % ','.join(columns))
query = "INSERT INTO scans VALUES (%s)" % ','.join('?' * len(columns))
for row in reader:
c.execute(query, [x for x in row])
conn.commit()
# run the user supplied filters and return result
result = col.defaultdict(list)
for token,filt in iter(filt.items()):
try:
result[token] = [x[0] for x in c.execute("SELECT ID FROM scans where %s" % filt)]
except sqlite3.OperationalError:
logger.critical("something is wrong with the filter: %s", filt)
raise
return result
|
[
"def",
"scansearch",
"(",
"auth",
",",
"label",
",",
"filt",
",",
"project",
"=",
"None",
",",
"aid",
"=",
"None",
")",
":",
"if",
"not",
"aid",
":",
"aid",
"=",
"accession",
"(",
"auth",
",",
"label",
",",
"project",
")",
"# get scans for accession as a csv",
"url",
"=",
"\"%s/data/experiments/%s/scans?format=csv\"",
"%",
"(",
"auth",
".",
"url",
".",
"rstrip",
"(",
"'/'",
")",
",",
"aid",
")",
"logger",
".",
"debug",
"(",
"\"issuing http request %s\"",
",",
"url",
")",
"r",
"=",
"requests",
".",
"get",
"(",
"url",
",",
"auth",
"=",
"(",
"auth",
".",
"username",
",",
"auth",
".",
"password",
")",
",",
"verify",
"=",
"CHECK_CERTIFICATE",
")",
"if",
"r",
".",
"status_code",
"!=",
"requests",
".",
"codes",
".",
"ok",
":",
"raise",
"ScanSearchError",
"(",
"\"response not ok (%s) from %s\"",
"%",
"(",
"r",
".",
"status_code",
",",
"r",
".",
"url",
")",
")",
"if",
"not",
"r",
".",
"content",
":",
"raise",
"ScanSearchError",
"(",
"\"response is empty from %s\"",
"%",
"r",
".",
"url",
")",
"# read the result into a csv reader",
"reader",
"=",
"csv",
".",
"reader",
"(",
"io",
".",
"StringIO",
"(",
"r",
".",
"content",
".",
"decode",
"(",
")",
")",
")",
"columns",
"=",
"next",
"(",
"reader",
")",
"# create an in-memory database",
"conn",
"=",
"sqlite3",
".",
"connect",
"(",
"\":memory:\"",
")",
"c",
"=",
"conn",
".",
"cursor",
"(",
")",
"# create scans table and insert data",
"c",
".",
"execute",
"(",
"\"CREATE TABLE scans (%s)\"",
"%",
"','",
".",
"join",
"(",
"columns",
")",
")",
"query",
"=",
"\"INSERT INTO scans VALUES (%s)\"",
"%",
"','",
".",
"join",
"(",
"'?'",
"*",
"len",
"(",
"columns",
")",
")",
"for",
"row",
"in",
"reader",
":",
"c",
".",
"execute",
"(",
"query",
",",
"[",
"x",
"for",
"x",
"in",
"row",
"]",
")",
"conn",
".",
"commit",
"(",
")",
"# run the user supplied filters and return result",
"result",
"=",
"col",
".",
"defaultdict",
"(",
"list",
")",
"for",
"token",
",",
"filt",
"in",
"iter",
"(",
"filt",
".",
"items",
"(",
")",
")",
":",
"try",
":",
"result",
"[",
"token",
"]",
"=",
"[",
"x",
"[",
"0",
"]",
"for",
"x",
"in",
"c",
".",
"execute",
"(",
"\"SELECT ID FROM scans where %s\"",
"%",
"filt",
")",
"]",
"except",
"sqlite3",
".",
"OperationalError",
":",
"logger",
".",
"critical",
"(",
"\"something is wrong with the filter: %s\"",
",",
"filt",
")",
"raise",
"return",
"result"
] |
Search for scans by supplying a set of SQL-based conditionals.
Example:
>>> import yaxil
>>> auth = yaxil.XnatAuth(url='...', username='...', password='...')
>>> query = {
... 'eor1': "note LIKE %EOR1%",
... 'eor2': "note LIKE %EOR2%",
... 'mpr': "series_description='T1_MEMPRAGE RMS' OR note LIKE %ANAT%"
... }
>>> yaxil.scansearch(auth, 'AB1234C', query)
{"mpr": [4], "eor1": [13], "eor2": [14]}
:param auth: XNAT authentication object
:type auth: :mod:`yaxil.XnatAuth`
:param label: XNAT MR Session label
:type label: str
:param filt: Scan search filter/query
:type filt: dict
:param project: XNAT MR Session project
:type project: str
:param aid: XNAT Accession ID
:type aid: str
:returns: Same dictionary that was passed in, but values are now matching scans
:rtype: dict
|
[
"Search",
"for",
"scans",
"by",
"supplying",
"a",
"set",
"of",
"SQL",
"-",
"based",
"conditionals",
"."
] |
train
|
https://github.com/harvard-nrg/yaxil/blob/af594082258e62d1904d6e6841fce0bb5c0bf309/yaxil/__init__.py#L485-L543
|
harvard-nrg/yaxil
|
yaxil/__init__.py
|
scans
|
def scans(auth, label=None, scan_ids=None, project=None, experiment=None):
'''
Get scan information for a MR Session as a sequence of dictionaries.
Example:
>>> import yaxil
>>> import json
>>> auth = yaxil.XnatAuth(url='...', username='...', password='...')
>>> for scan in yaxil.scans2(auth, 'AB1234C'):
... print(json.dumps(scan, indent=2))
:param auth: XNAT authentication object
:type auth: :mod:`yaxil.XnatAuth`
:param label: XNAT MR Session label
:type label: str
:param scan_ids: Scan numbers to return
:type scan_ids: list
:param project: XNAT MR Session project
:type project: str
:param experiment: YAXIL Experiment
:type experiment: :mod:`yaxil.Experiment`
:returns: Generator of scan data dictionaries
:rtype: dict
'''
if experiment and (label or project):
raise ValueError('cannot supply experiment with label or project')
if experiment:
label,project = experiment.label,experiment.project
aid = accession(auth, label, project)
path = '/data/experiments'
params = {
'xsiType': 'xnat:mrSessionData',
'columns': ','.join(scans.columns.keys())
}
params['xnat:mrSessionData/ID'] = aid
_,result = _get(auth, path, 'json', autobox=True, params=params)
for result in result['ResultSet']['Result']:
if scan_ids == None or result['xnat:mrscandata/id'] in scan_ids:
data = dict()
for k,v in iter(scans.columns.items()):
data[v] = result[k]
yield data
|
python
|
def scans(auth, label=None, scan_ids=None, project=None, experiment=None):
'''
Get scan information for a MR Session as a sequence of dictionaries.
Example:
>>> import yaxil
>>> import json
>>> auth = yaxil.XnatAuth(url='...', username='...', password='...')
>>> for scan in yaxil.scans2(auth, 'AB1234C'):
... print(json.dumps(scan, indent=2))
:param auth: XNAT authentication object
:type auth: :mod:`yaxil.XnatAuth`
:param label: XNAT MR Session label
:type label: str
:param scan_ids: Scan numbers to return
:type scan_ids: list
:param project: XNAT MR Session project
:type project: str
:param experiment: YAXIL Experiment
:type experiment: :mod:`yaxil.Experiment`
:returns: Generator of scan data dictionaries
:rtype: dict
'''
if experiment and (label or project):
raise ValueError('cannot supply experiment with label or project')
if experiment:
label,project = experiment.label,experiment.project
aid = accession(auth, label, project)
path = '/data/experiments'
params = {
'xsiType': 'xnat:mrSessionData',
'columns': ','.join(scans.columns.keys())
}
params['xnat:mrSessionData/ID'] = aid
_,result = _get(auth, path, 'json', autobox=True, params=params)
for result in result['ResultSet']['Result']:
if scan_ids == None or result['xnat:mrscandata/id'] in scan_ids:
data = dict()
for k,v in iter(scans.columns.items()):
data[v] = result[k]
yield data
|
[
"def",
"scans",
"(",
"auth",
",",
"label",
"=",
"None",
",",
"scan_ids",
"=",
"None",
",",
"project",
"=",
"None",
",",
"experiment",
"=",
"None",
")",
":",
"if",
"experiment",
"and",
"(",
"label",
"or",
"project",
")",
":",
"raise",
"ValueError",
"(",
"'cannot supply experiment with label or project'",
")",
"if",
"experiment",
":",
"label",
",",
"project",
"=",
"experiment",
".",
"label",
",",
"experiment",
".",
"project",
"aid",
"=",
"accession",
"(",
"auth",
",",
"label",
",",
"project",
")",
"path",
"=",
"'/data/experiments'",
"params",
"=",
"{",
"'xsiType'",
":",
"'xnat:mrSessionData'",
",",
"'columns'",
":",
"','",
".",
"join",
"(",
"scans",
".",
"columns",
".",
"keys",
"(",
")",
")",
"}",
"params",
"[",
"'xnat:mrSessionData/ID'",
"]",
"=",
"aid",
"_",
",",
"result",
"=",
"_get",
"(",
"auth",
",",
"path",
",",
"'json'",
",",
"autobox",
"=",
"True",
",",
"params",
"=",
"params",
")",
"for",
"result",
"in",
"result",
"[",
"'ResultSet'",
"]",
"[",
"'Result'",
"]",
":",
"if",
"scan_ids",
"==",
"None",
"or",
"result",
"[",
"'xnat:mrscandata/id'",
"]",
"in",
"scan_ids",
":",
"data",
"=",
"dict",
"(",
")",
"for",
"k",
",",
"v",
"in",
"iter",
"(",
"scans",
".",
"columns",
".",
"items",
"(",
")",
")",
":",
"data",
"[",
"v",
"]",
"=",
"result",
"[",
"k",
"]",
"yield",
"data"
] |
Get scan information for a MR Session as a sequence of dictionaries.
Example:
>>> import yaxil
>>> import json
>>> auth = yaxil.XnatAuth(url='...', username='...', password='...')
>>> for scan in yaxil.scans2(auth, 'AB1234C'):
... print(json.dumps(scan, indent=2))
:param auth: XNAT authentication object
:type auth: :mod:`yaxil.XnatAuth`
:param label: XNAT MR Session label
:type label: str
:param scan_ids: Scan numbers to return
:type scan_ids: list
:param project: XNAT MR Session project
:type project: str
:param experiment: YAXIL Experiment
:type experiment: :mod:`yaxil.Experiment`
:returns: Generator of scan data dictionaries
:rtype: dict
|
[
"Get",
"scan",
"information",
"for",
"a",
"MR",
"Session",
"as",
"a",
"sequence",
"of",
"dictionaries",
"."
] |
train
|
https://github.com/harvard-nrg/yaxil/blob/af594082258e62d1904d6e6841fce0bb5c0bf309/yaxil/__init__.py#L545-L586
|
harvard-nrg/yaxil
|
yaxil/__init__.py
|
extendedboldqc
|
def extendedboldqc(auth, label, scan_ids=None, project=None, aid=None):
'''
Get ExtendedBOLDQC data as a sequence of dictionaries.
Example:
>>> import yaxil
>>> import json
>>> auth = yaxil.XnatAuth(url='...', username='...', password='...')
>>> for eqc in yaxil.extendedboldqc2(auth, 'AB1234C')
... print(json.dumps(eqc, indent=2))
:param auth: XNAT authentication object
:type auth: :mod:`yaxil.XnatAuth`
:param label: XNAT MR Session label
:type label: str
:param scan_ids: Scan numbers to return
:type scan_ids: list
:param project: XNAT MR Session project
:type project: str
:param aid: XNAT Accession ID
:type aid: str
:returns: Generator of scan data dictionaries
:rtype: :mod:`dict`
'''
if not aid:
aid = accession(auth, label, project)
path = '/data/experiments'
params = {
'xsiType': 'neuroinfo:extendedboldqc',
'columns': ','.join(extendedboldqc.columns.keys())
}
if project:
params['project'] = project
params['xnat:mrSessionData/ID'] = aid
_,result = _get(auth, path, 'json', autobox=True, params=params)
for result in result['ResultSet']['Result']:
if scan_ids == None or result['neuroinfo:extendedboldqc/scan/scan_id'] in scan_ids:
data = dict()
for k,v in iter(extendedboldqc.columns.items()):
data[v] = result[k]
yield data
|
python
|
def extendedboldqc(auth, label, scan_ids=None, project=None, aid=None):
'''
Get ExtendedBOLDQC data as a sequence of dictionaries.
Example:
>>> import yaxil
>>> import json
>>> auth = yaxil.XnatAuth(url='...', username='...', password='...')
>>> for eqc in yaxil.extendedboldqc2(auth, 'AB1234C')
... print(json.dumps(eqc, indent=2))
:param auth: XNAT authentication object
:type auth: :mod:`yaxil.XnatAuth`
:param label: XNAT MR Session label
:type label: str
:param scan_ids: Scan numbers to return
:type scan_ids: list
:param project: XNAT MR Session project
:type project: str
:param aid: XNAT Accession ID
:type aid: str
:returns: Generator of scan data dictionaries
:rtype: :mod:`dict`
'''
if not aid:
aid = accession(auth, label, project)
path = '/data/experiments'
params = {
'xsiType': 'neuroinfo:extendedboldqc',
'columns': ','.join(extendedboldqc.columns.keys())
}
if project:
params['project'] = project
params['xnat:mrSessionData/ID'] = aid
_,result = _get(auth, path, 'json', autobox=True, params=params)
for result in result['ResultSet']['Result']:
if scan_ids == None or result['neuroinfo:extendedboldqc/scan/scan_id'] in scan_ids:
data = dict()
for k,v in iter(extendedboldqc.columns.items()):
data[v] = result[k]
yield data
|
[
"def",
"extendedboldqc",
"(",
"auth",
",",
"label",
",",
"scan_ids",
"=",
"None",
",",
"project",
"=",
"None",
",",
"aid",
"=",
"None",
")",
":",
"if",
"not",
"aid",
":",
"aid",
"=",
"accession",
"(",
"auth",
",",
"label",
",",
"project",
")",
"path",
"=",
"'/data/experiments'",
"params",
"=",
"{",
"'xsiType'",
":",
"'neuroinfo:extendedboldqc'",
",",
"'columns'",
":",
"','",
".",
"join",
"(",
"extendedboldqc",
".",
"columns",
".",
"keys",
"(",
")",
")",
"}",
"if",
"project",
":",
"params",
"[",
"'project'",
"]",
"=",
"project",
"params",
"[",
"'xnat:mrSessionData/ID'",
"]",
"=",
"aid",
"_",
",",
"result",
"=",
"_get",
"(",
"auth",
",",
"path",
",",
"'json'",
",",
"autobox",
"=",
"True",
",",
"params",
"=",
"params",
")",
"for",
"result",
"in",
"result",
"[",
"'ResultSet'",
"]",
"[",
"'Result'",
"]",
":",
"if",
"scan_ids",
"==",
"None",
"or",
"result",
"[",
"'neuroinfo:extendedboldqc/scan/scan_id'",
"]",
"in",
"scan_ids",
":",
"data",
"=",
"dict",
"(",
")",
"for",
"k",
",",
"v",
"in",
"iter",
"(",
"extendedboldqc",
".",
"columns",
".",
"items",
"(",
")",
")",
":",
"data",
"[",
"v",
"]",
"=",
"result",
"[",
"k",
"]",
"yield",
"data"
] |
Get ExtendedBOLDQC data as a sequence of dictionaries.
Example:
>>> import yaxil
>>> import json
>>> auth = yaxil.XnatAuth(url='...', username='...', password='...')
>>> for eqc in yaxil.extendedboldqc2(auth, 'AB1234C')
... print(json.dumps(eqc, indent=2))
:param auth: XNAT authentication object
:type auth: :mod:`yaxil.XnatAuth`
:param label: XNAT MR Session label
:type label: str
:param scan_ids: Scan numbers to return
:type scan_ids: list
:param project: XNAT MR Session project
:type project: str
:param aid: XNAT Accession ID
:type aid: str
:returns: Generator of scan data dictionaries
:rtype: :mod:`dict`
|
[
"Get",
"ExtendedBOLDQC",
"data",
"as",
"a",
"sequence",
"of",
"dictionaries",
"."
] |
train
|
https://github.com/harvard-nrg/yaxil/blob/af594082258e62d1904d6e6841fce0bb5c0bf309/yaxil/__init__.py#L626-L666
|
harvard-nrg/yaxil
|
yaxil/__init__.py
|
_get
|
def _get(auth, path, fmt, autobox=True, params=None):
'''
Issue a GET request to the XNAT REST API and box the response content.
Example:
>>> import yaxil
>>> from yaxil import Format
>>> auth = yaxil.XnatAuth(url='...', username='...', password='...')
>>> yaxil.get(auth, '/data/experiments', Format.JSON)
:param auth: XNAT authentication
:type auth: :mod:`yaxil.XnatAuth`
:param path: API URL path
:type path: str
:param fmt: API result format
:type fmt: :mod:`yaxil.Format`
:param autobox: Autobox response content into an appropriate reader or other data structure
:type autobox: bool
:param params: Additional query parameters
:type params: dict
:returns: Tuple of (URL, :mod:`dict` | :mod:`xml.etree.ElementTree` | :mod:`csv.reader` | :mod:`str`)
:rtype: tuple
'''
if not params:
params = {}
url = "%s/%s" % (auth.url.rstrip('/'), path.lstrip('/'))
params["format"] = fmt
logger.debug("issuing http request %s", url)
logger.debug("query parameters %s", params)
r = requests.get(url, params=params, auth=(auth.username, auth.password), verify=CHECK_CERTIFICATE)
if r.status_code != requests.codes.ok:
raise RestApiError("response not ok (%s) from %s" % (r.status_code, r.url))
if not r.content:
raise RestApiError("response is empty from %s" % r.url)
if autobox:
return r.url,_autobox(r.text, fmt)
else:
return r.url,r.content
|
python
|
def _get(auth, path, fmt, autobox=True, params=None):
'''
Issue a GET request to the XNAT REST API and box the response content.
Example:
>>> import yaxil
>>> from yaxil import Format
>>> auth = yaxil.XnatAuth(url='...', username='...', password='...')
>>> yaxil.get(auth, '/data/experiments', Format.JSON)
:param auth: XNAT authentication
:type auth: :mod:`yaxil.XnatAuth`
:param path: API URL path
:type path: str
:param fmt: API result format
:type fmt: :mod:`yaxil.Format`
:param autobox: Autobox response content into an appropriate reader or other data structure
:type autobox: bool
:param params: Additional query parameters
:type params: dict
:returns: Tuple of (URL, :mod:`dict` | :mod:`xml.etree.ElementTree` | :mod:`csv.reader` | :mod:`str`)
:rtype: tuple
'''
if not params:
params = {}
url = "%s/%s" % (auth.url.rstrip('/'), path.lstrip('/'))
params["format"] = fmt
logger.debug("issuing http request %s", url)
logger.debug("query parameters %s", params)
r = requests.get(url, params=params, auth=(auth.username, auth.password), verify=CHECK_CERTIFICATE)
if r.status_code != requests.codes.ok:
raise RestApiError("response not ok (%s) from %s" % (r.status_code, r.url))
if not r.content:
raise RestApiError("response is empty from %s" % r.url)
if autobox:
return r.url,_autobox(r.text, fmt)
else:
return r.url,r.content
|
[
"def",
"_get",
"(",
"auth",
",",
"path",
",",
"fmt",
",",
"autobox",
"=",
"True",
",",
"params",
"=",
"None",
")",
":",
"if",
"not",
"params",
":",
"params",
"=",
"{",
"}",
"url",
"=",
"\"%s/%s\"",
"%",
"(",
"auth",
".",
"url",
".",
"rstrip",
"(",
"'/'",
")",
",",
"path",
".",
"lstrip",
"(",
"'/'",
")",
")",
"params",
"[",
"\"format\"",
"]",
"=",
"fmt",
"logger",
".",
"debug",
"(",
"\"issuing http request %s\"",
",",
"url",
")",
"logger",
".",
"debug",
"(",
"\"query parameters %s\"",
",",
"params",
")",
"r",
"=",
"requests",
".",
"get",
"(",
"url",
",",
"params",
"=",
"params",
",",
"auth",
"=",
"(",
"auth",
".",
"username",
",",
"auth",
".",
"password",
")",
",",
"verify",
"=",
"CHECK_CERTIFICATE",
")",
"if",
"r",
".",
"status_code",
"!=",
"requests",
".",
"codes",
".",
"ok",
":",
"raise",
"RestApiError",
"(",
"\"response not ok (%s) from %s\"",
"%",
"(",
"r",
".",
"status_code",
",",
"r",
".",
"url",
")",
")",
"if",
"not",
"r",
".",
"content",
":",
"raise",
"RestApiError",
"(",
"\"response is empty from %s\"",
"%",
"r",
".",
"url",
")",
"if",
"autobox",
":",
"return",
"r",
".",
"url",
",",
"_autobox",
"(",
"r",
".",
"text",
",",
"fmt",
")",
"else",
":",
"return",
"r",
".",
"url",
",",
"r",
".",
"content"
] |
Issue a GET request to the XNAT REST API and box the response content.
Example:
>>> import yaxil
>>> from yaxil import Format
>>> auth = yaxil.XnatAuth(url='...', username='...', password='...')
>>> yaxil.get(auth, '/data/experiments', Format.JSON)
:param auth: XNAT authentication
:type auth: :mod:`yaxil.XnatAuth`
:param path: API URL path
:type path: str
:param fmt: API result format
:type fmt: :mod:`yaxil.Format`
:param autobox: Autobox response content into an appropriate reader or other data structure
:type autobox: bool
:param params: Additional query parameters
:type params: dict
:returns: Tuple of (URL, :mod:`dict` | :mod:`xml.etree.ElementTree` | :mod:`csv.reader` | :mod:`str`)
:rtype: tuple
|
[
"Issue",
"a",
"GET",
"request",
"to",
"the",
"XNAT",
"REST",
"API",
"and",
"box",
"the",
"response",
"content",
".",
"Example",
":",
">>>",
"import",
"yaxil",
">>>",
"from",
"yaxil",
"import",
"Format",
">>>",
"auth",
"=",
"yaxil",
".",
"XnatAuth",
"(",
"url",
"=",
"...",
"username",
"=",
"...",
"password",
"=",
"...",
")",
">>>",
"yaxil",
".",
"get",
"(",
"auth",
"/",
"data",
"/",
"experiments",
"Format",
".",
"JSON",
")"
] |
train
|
https://github.com/harvard-nrg/yaxil/blob/af594082258e62d1904d6e6841fce0bb5c0bf309/yaxil/__init__.py#L739-L776
|
harvard-nrg/yaxil
|
yaxil/__init__.py
|
_autobox
|
def _autobox(content, format):
'''
Autobox response content.
:param content: Response content
:type content: str
:param format: Format to return
:type format: `yaxil.Format`
:returns: Autoboxed content
:rtype: dict|xml.etree.ElementTree.Element|csvreader
'''
if format == Format.JSON:
return json.loads(content)
elif format == Format.XML:
return etree.fromstring(content)
elif format == Format.CSV:
try:
return csv.reader(io.BytesIO(content))
except TypeError:
# as per https://docs.python.org/2/library/csv.html#examples
def unicode_csv_reader(unicode_csv_data, dialect=csv.excel, **kwargs):
# csv.py doesn't do Unicode; encode temporarily as UTF-8:
csv_reader = csv.reader(utf_8_encoder(unicode_csv_data),
dialect=dialect, **kwargs)
for row in csv_reader:
# decode UTF-8 back to Unicode, cell by cell:
yield [unicode(cell, 'utf-8') for cell in row]
def utf_8_encoder(unicode_csv_data):
for line in unicode_csv_data:
yield line.encode('utf-8')
return unicode_csv_reader(io.StringIO(content))
else:
raise AutoboxError("unknown autobox format %s" % format)
|
python
|
def _autobox(content, format):
'''
Autobox response content.
:param content: Response content
:type content: str
:param format: Format to return
:type format: `yaxil.Format`
:returns: Autoboxed content
:rtype: dict|xml.etree.ElementTree.Element|csvreader
'''
if format == Format.JSON:
return json.loads(content)
elif format == Format.XML:
return etree.fromstring(content)
elif format == Format.CSV:
try:
return csv.reader(io.BytesIO(content))
except TypeError:
# as per https://docs.python.org/2/library/csv.html#examples
def unicode_csv_reader(unicode_csv_data, dialect=csv.excel, **kwargs):
# csv.py doesn't do Unicode; encode temporarily as UTF-8:
csv_reader = csv.reader(utf_8_encoder(unicode_csv_data),
dialect=dialect, **kwargs)
for row in csv_reader:
# decode UTF-8 back to Unicode, cell by cell:
yield [unicode(cell, 'utf-8') for cell in row]
def utf_8_encoder(unicode_csv_data):
for line in unicode_csv_data:
yield line.encode('utf-8')
return unicode_csv_reader(io.StringIO(content))
else:
raise AutoboxError("unknown autobox format %s" % format)
|
[
"def",
"_autobox",
"(",
"content",
",",
"format",
")",
":",
"if",
"format",
"==",
"Format",
".",
"JSON",
":",
"return",
"json",
".",
"loads",
"(",
"content",
")",
"elif",
"format",
"==",
"Format",
".",
"XML",
":",
"return",
"etree",
".",
"fromstring",
"(",
"content",
")",
"elif",
"format",
"==",
"Format",
".",
"CSV",
":",
"try",
":",
"return",
"csv",
".",
"reader",
"(",
"io",
".",
"BytesIO",
"(",
"content",
")",
")",
"except",
"TypeError",
":",
"# as per https://docs.python.org/2/library/csv.html#examples",
"def",
"unicode_csv_reader",
"(",
"unicode_csv_data",
",",
"dialect",
"=",
"csv",
".",
"excel",
",",
"*",
"*",
"kwargs",
")",
":",
"# csv.py doesn't do Unicode; encode temporarily as UTF-8:",
"csv_reader",
"=",
"csv",
".",
"reader",
"(",
"utf_8_encoder",
"(",
"unicode_csv_data",
")",
",",
"dialect",
"=",
"dialect",
",",
"*",
"*",
"kwargs",
")",
"for",
"row",
"in",
"csv_reader",
":",
"# decode UTF-8 back to Unicode, cell by cell:",
"yield",
"[",
"unicode",
"(",
"cell",
",",
"'utf-8'",
")",
"for",
"cell",
"in",
"row",
"]",
"def",
"utf_8_encoder",
"(",
"unicode_csv_data",
")",
":",
"for",
"line",
"in",
"unicode_csv_data",
":",
"yield",
"line",
".",
"encode",
"(",
"'utf-8'",
")",
"return",
"unicode_csv_reader",
"(",
"io",
".",
"StringIO",
"(",
"content",
")",
")",
"else",
":",
"raise",
"AutoboxError",
"(",
"\"unknown autobox format %s\"",
"%",
"format",
")"
] |
Autobox response content.
:param content: Response content
:type content: str
:param format: Format to return
:type format: `yaxil.Format`
:returns: Autoboxed content
:rtype: dict|xml.etree.ElementTree.Element|csvreader
|
[
"Autobox",
"response",
"content",
"."
] |
train
|
https://github.com/harvard-nrg/yaxil/blob/af594082258e62d1904d6e6841fce0bb5c0bf309/yaxil/__init__.py#L778-L810
|
harvard-nrg/yaxil
|
yaxil/__init__.py
|
has
|
def has(auth, xsitype, project=None):
'''
Test if a project contains any items of a particular xsi:type.
Example:
>>> import yaxil
>>> auth = yaxil.XnatAuth(url='...', username='...', password='...')
>>> yaxil.has(auth, 'neuroinfo:extendedboldqc', project='MyProject')
:param auth: XNAT authentication
:type auth: :mod:`yaxil.XnatAuth`
:param xsitype: XNAT xsi:type
:param xsitype: str
:param project: XNAT Project
:type project: str
:returns: True or False
:rtype: bool
'''
path = "/data/experiments"
params = {
"xsiType": xsitype,
"columns": 'ID'
}
if project:
params["project"] = project
url,result = _get(auth, path, Format.JSON, autobox=True, params=params)
try:
__quick_validate(result)
except ResultSetError as e:
raise ResultSetError("%s in response from %s" % (e.message, url))
if int(result["ResultSet"]["totalRecords"]) == 0:
return False
return True
|
python
|
def has(auth, xsitype, project=None):
'''
Test if a project contains any items of a particular xsi:type.
Example:
>>> import yaxil
>>> auth = yaxil.XnatAuth(url='...', username='...', password='...')
>>> yaxil.has(auth, 'neuroinfo:extendedboldqc', project='MyProject')
:param auth: XNAT authentication
:type auth: :mod:`yaxil.XnatAuth`
:param xsitype: XNAT xsi:type
:param xsitype: str
:param project: XNAT Project
:type project: str
:returns: True or False
:rtype: bool
'''
path = "/data/experiments"
params = {
"xsiType": xsitype,
"columns": 'ID'
}
if project:
params["project"] = project
url,result = _get(auth, path, Format.JSON, autobox=True, params=params)
try:
__quick_validate(result)
except ResultSetError as e:
raise ResultSetError("%s in response from %s" % (e.message, url))
if int(result["ResultSet"]["totalRecords"]) == 0:
return False
return True
|
[
"def",
"has",
"(",
"auth",
",",
"xsitype",
",",
"project",
"=",
"None",
")",
":",
"path",
"=",
"\"/data/experiments\"",
"params",
"=",
"{",
"\"xsiType\"",
":",
"xsitype",
",",
"\"columns\"",
":",
"'ID'",
"}",
"if",
"project",
":",
"params",
"[",
"\"project\"",
"]",
"=",
"project",
"url",
",",
"result",
"=",
"_get",
"(",
"auth",
",",
"path",
",",
"Format",
".",
"JSON",
",",
"autobox",
"=",
"True",
",",
"params",
"=",
"params",
")",
"try",
":",
"__quick_validate",
"(",
"result",
")",
"except",
"ResultSetError",
"as",
"e",
":",
"raise",
"ResultSetError",
"(",
"\"%s in response from %s\"",
"%",
"(",
"e",
".",
"message",
",",
"url",
")",
")",
"if",
"int",
"(",
"result",
"[",
"\"ResultSet\"",
"]",
"[",
"\"totalRecords\"",
"]",
")",
"==",
"0",
":",
"return",
"False",
"return",
"True"
] |
Test if a project contains any items of a particular xsi:type.
Example:
>>> import yaxil
>>> auth = yaxil.XnatAuth(url='...', username='...', password='...')
>>> yaxil.has(auth, 'neuroinfo:extendedboldqc', project='MyProject')
:param auth: XNAT authentication
:type auth: :mod:`yaxil.XnatAuth`
:param xsitype: XNAT xsi:type
:param xsitype: str
:param project: XNAT Project
:type project: str
:returns: True or False
:rtype: bool
|
[
"Test",
"if",
"a",
"project",
"contains",
"any",
"items",
"of",
"a",
"particular",
"xsi",
":",
"type",
"."
] |
train
|
https://github.com/harvard-nrg/yaxil/blob/af594082258e62d1904d6e6841fce0bb5c0bf309/yaxil/__init__.py#L812-L844
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/auth/middleware.py
|
get_user
|
def get_user(request):
"""
Returns a cached copy of the user if it exists or calls `auth_get_user`
otherwise.
"""
if not hasattr(request, '_cached_user'):
request._cached_user = auth_get_user(request)
return request._cached_user
|
python
|
def get_user(request):
"""
Returns a cached copy of the user if it exists or calls `auth_get_user`
otherwise.
"""
if not hasattr(request, '_cached_user'):
request._cached_user = auth_get_user(request)
return request._cached_user
|
[
"def",
"get_user",
"(",
"request",
")",
":",
"if",
"not",
"hasattr",
"(",
"request",
",",
"'_cached_user'",
")",
":",
"request",
".",
"_cached_user",
"=",
"auth_get_user",
"(",
"request",
")",
"return",
"request",
".",
"_cached_user"
] |
Returns a cached copy of the user if it exists or calls `auth_get_user`
otherwise.
|
[
"Returns",
"a",
"cached",
"copy",
"of",
"the",
"user",
"if",
"it",
"exists",
"or",
"calls",
"auth_get_user",
"otherwise",
"."
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/auth/middleware.py#L11-L18
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/user_admin/views.py
|
ensure_compatible_admin
|
def ensure_compatible_admin(view):
"""
Ensures that the user is in exactly one role.
Other checks could be added, such as requiring one prison if in prison-clerk role.
"""
def wrapper(request, *args, **kwargs):
user_roles = request.user.user_data.get('roles', [])
if len(user_roles) != 1:
context = {
'message': 'I need to be able to manage user accounts. '
'My username is %s' % request.user.username
}
return render(request, 'mtp_common/user_admin/incompatible-admin.html', context=context)
return view(request, *args, **kwargs)
return wrapper
|
python
|
def ensure_compatible_admin(view):
"""
Ensures that the user is in exactly one role.
Other checks could be added, such as requiring one prison if in prison-clerk role.
"""
def wrapper(request, *args, **kwargs):
user_roles = request.user.user_data.get('roles', [])
if len(user_roles) != 1:
context = {
'message': 'I need to be able to manage user accounts. '
'My username is %s' % request.user.username
}
return render(request, 'mtp_common/user_admin/incompatible-admin.html', context=context)
return view(request, *args, **kwargs)
return wrapper
|
[
"def",
"ensure_compatible_admin",
"(",
"view",
")",
":",
"def",
"wrapper",
"(",
"request",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"user_roles",
"=",
"request",
".",
"user",
".",
"user_data",
".",
"get",
"(",
"'roles'",
",",
"[",
"]",
")",
"if",
"len",
"(",
"user_roles",
")",
"!=",
"1",
":",
"context",
"=",
"{",
"'message'",
":",
"'I need to be able to manage user accounts. '",
"'My username is %s'",
"%",
"request",
".",
"user",
".",
"username",
"}",
"return",
"render",
"(",
"request",
",",
"'mtp_common/user_admin/incompatible-admin.html'",
",",
"context",
"=",
"context",
")",
"return",
"view",
"(",
"request",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
"return",
"wrapper"
] |
Ensures that the user is in exactly one role.
Other checks could be added, such as requiring one prison if in prison-clerk role.
|
[
"Ensures",
"that",
"the",
"user",
"is",
"in",
"exactly",
"one",
"role",
".",
"Other",
"checks",
"could",
"be",
"added",
"such",
"as",
"requiring",
"one",
"prison",
"if",
"in",
"prison",
"-",
"clerk",
"role",
"."
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/user_admin/views.py#L31-L47
|
Parsely/birding
|
src/birding/bolt.py
|
fault_barrier
|
def fault_barrier(fn):
"""Method decorator to catch and log errors, then send fail message."""
@functools.wraps(fn)
def process(self, tup):
try:
return fn(self, tup)
except Exception as e:
if isinstance(e, KeyboardInterrupt):
return
print(str(e), file=sys.stderr)
self.fail(tup)
return process
|
python
|
def fault_barrier(fn):
"""Method decorator to catch and log errors, then send fail message."""
@functools.wraps(fn)
def process(self, tup):
try:
return fn(self, tup)
except Exception as e:
if isinstance(e, KeyboardInterrupt):
return
print(str(e), file=sys.stderr)
self.fail(tup)
return process
|
[
"def",
"fault_barrier",
"(",
"fn",
")",
":",
"@",
"functools",
".",
"wraps",
"(",
"fn",
")",
"def",
"process",
"(",
"self",
",",
"tup",
")",
":",
"try",
":",
"return",
"fn",
"(",
"self",
",",
"tup",
")",
"except",
"Exception",
"as",
"e",
":",
"if",
"isinstance",
"(",
"e",
",",
"KeyboardInterrupt",
")",
":",
"return",
"print",
"(",
"str",
"(",
"e",
")",
",",
"file",
"=",
"sys",
".",
"stderr",
")",
"self",
".",
"fail",
"(",
"tup",
")",
"return",
"process"
] |
Method decorator to catch and log errors, then send fail message.
|
[
"Method",
"decorator",
"to",
"catch",
"and",
"log",
"errors",
"then",
"send",
"fail",
"message",
"."
] |
train
|
https://github.com/Parsely/birding/blob/c7f6eee56424234e361b1a455595de202e744dac/src/birding/bolt.py#L16-L27
|
Parsely/birding
|
src/birding/bolt.py
|
TwitterSearchBolt.initialize
|
def initialize(self, conf, ctx):
"""Initialization steps:
1. Get :func:`~birding.search.search_manager_from_config`.
2. Prepare to track searched terms as to avoid redundant searches.
"""
self.manager = get_search_manager()
config = get_config()['TwitterSearchBolt']
self.term_shelf = shelf_from_config(config)
|
python
|
def initialize(self, conf, ctx):
"""Initialization steps:
1. Get :func:`~birding.search.search_manager_from_config`.
2. Prepare to track searched terms as to avoid redundant searches.
"""
self.manager = get_search_manager()
config = get_config()['TwitterSearchBolt']
self.term_shelf = shelf_from_config(config)
|
[
"def",
"initialize",
"(",
"self",
",",
"conf",
",",
"ctx",
")",
":",
"self",
".",
"manager",
"=",
"get_search_manager",
"(",
")",
"config",
"=",
"get_config",
"(",
")",
"[",
"'TwitterSearchBolt'",
"]",
"self",
".",
"term_shelf",
"=",
"shelf_from_config",
"(",
"config",
")"
] |
Initialization steps:
1. Get :func:`~birding.search.search_manager_from_config`.
2. Prepare to track searched terms as to avoid redundant searches.
|
[
"Initialization",
"steps",
":"
] |
train
|
https://github.com/Parsely/birding/blob/c7f6eee56424234e361b1a455595de202e744dac/src/birding/bolt.py#L37-L45
|
Parsely/birding
|
src/birding/bolt.py
|
TwitterSearchBolt.process
|
def process(self, tup):
"""Process steps:
1. Stream in (term, timestamp).
2. Perform :meth:`~birding.search.SearchManager.search` on term.
3. Emit (term, timestamp, search_result).
"""
term, timestamp = tup.values
if term not in self.term_shelf:
self.log(
'search: {term}, {timestamp}'
.format(term=term, timestamp=timestamp))
search_result = self.manager.search(q=term)
self.emit([term, timestamp, search_result])
self.term_shelf[term] = timestamp
|
python
|
def process(self, tup):
"""Process steps:
1. Stream in (term, timestamp).
2. Perform :meth:`~birding.search.SearchManager.search` on term.
3. Emit (term, timestamp, search_result).
"""
term, timestamp = tup.values
if term not in self.term_shelf:
self.log(
'search: {term}, {timestamp}'
.format(term=term, timestamp=timestamp))
search_result = self.manager.search(q=term)
self.emit([term, timestamp, search_result])
self.term_shelf[term] = timestamp
|
[
"def",
"process",
"(",
"self",
",",
"tup",
")",
":",
"term",
",",
"timestamp",
"=",
"tup",
".",
"values",
"if",
"term",
"not",
"in",
"self",
".",
"term_shelf",
":",
"self",
".",
"log",
"(",
"'search: {term}, {timestamp}'",
".",
"format",
"(",
"term",
"=",
"term",
",",
"timestamp",
"=",
"timestamp",
")",
")",
"search_result",
"=",
"self",
".",
"manager",
".",
"search",
"(",
"q",
"=",
"term",
")",
"self",
".",
"emit",
"(",
"[",
"term",
",",
"timestamp",
",",
"search_result",
"]",
")",
"self",
".",
"term_shelf",
"[",
"term",
"]",
"=",
"timestamp"
] |
Process steps:
1. Stream in (term, timestamp).
2. Perform :meth:`~birding.search.SearchManager.search` on term.
3. Emit (term, timestamp, search_result).
|
[
"Process",
"steps",
":"
] |
train
|
https://github.com/Parsely/birding/blob/c7f6eee56424234e361b1a455595de202e744dac/src/birding/bolt.py#L48-L62
|
Parsely/birding
|
src/birding/bolt.py
|
TwitterLookupBolt.process
|
def process(self, tup):
"""Process steps:
1. Stream in (term, timestamp, search_result).
2. Perform :meth:`~birding.search.SearchManager.lookup_search_result`.
3. Emit (term, timestamp, lookup_result).
"""
term, timestamp, search_result = tup.values
self.log(
'lookup: {term}, {timestamp}'
.format(term=term, timestamp=timestamp))
lookup_result = self.manager.lookup_search_result(search_result)
self.emit([term, timestamp, lookup_result])
|
python
|
def process(self, tup):
"""Process steps:
1. Stream in (term, timestamp, search_result).
2. Perform :meth:`~birding.search.SearchManager.lookup_search_result`.
3. Emit (term, timestamp, lookup_result).
"""
term, timestamp, search_result = tup.values
self.log(
'lookup: {term}, {timestamp}'
.format(term=term, timestamp=timestamp))
lookup_result = self.manager.lookup_search_result(search_result)
self.emit([term, timestamp, lookup_result])
|
[
"def",
"process",
"(",
"self",
",",
"tup",
")",
":",
"term",
",",
"timestamp",
",",
"search_result",
"=",
"tup",
".",
"values",
"self",
".",
"log",
"(",
"'lookup: {term}, {timestamp}'",
".",
"format",
"(",
"term",
"=",
"term",
",",
"timestamp",
"=",
"timestamp",
")",
")",
"lookup_result",
"=",
"self",
".",
"manager",
".",
"lookup_search_result",
"(",
"search_result",
")",
"self",
".",
"emit",
"(",
"[",
"term",
",",
"timestamp",
",",
"lookup_result",
"]",
")"
] |
Process steps:
1. Stream in (term, timestamp, search_result).
2. Perform :meth:`~birding.search.SearchManager.lookup_search_result`.
3. Emit (term, timestamp, lookup_result).
|
[
"Process",
"steps",
":"
] |
train
|
https://github.com/Parsely/birding/blob/c7f6eee56424234e361b1a455595de202e744dac/src/birding/bolt.py#L74-L86
|
Parsely/birding
|
src/birding/bolt.py
|
ElasticsearchIndexBolt.initialize
|
def initialize(self, conf, ctx):
"""Initialization steps:
1. Prepare elasticsearch connection, including details for indexing.
"""
config = get_config()['ElasticsearchIndexBolt']
elasticsearch_class = import_name(config['elasticsearch_class'])
self.es = elasticsearch_class(**config['elasticsearch_init'])
self.index = config['index']
self.doc_type = config['doc_type']
|
python
|
def initialize(self, conf, ctx):
"""Initialization steps:
1. Prepare elasticsearch connection, including details for indexing.
"""
config = get_config()['ElasticsearchIndexBolt']
elasticsearch_class = import_name(config['elasticsearch_class'])
self.es = elasticsearch_class(**config['elasticsearch_init'])
self.index = config['index']
self.doc_type = config['doc_type']
|
[
"def",
"initialize",
"(",
"self",
",",
"conf",
",",
"ctx",
")",
":",
"config",
"=",
"get_config",
"(",
")",
"[",
"'ElasticsearchIndexBolt'",
"]",
"elasticsearch_class",
"=",
"import_name",
"(",
"config",
"[",
"'elasticsearch_class'",
"]",
")",
"self",
".",
"es",
"=",
"elasticsearch_class",
"(",
"*",
"*",
"config",
"[",
"'elasticsearch_init'",
"]",
")",
"self",
".",
"index",
"=",
"config",
"[",
"'index'",
"]",
"self",
".",
"doc_type",
"=",
"config",
"[",
"'doc_type'",
"]"
] |
Initialization steps:
1. Prepare elasticsearch connection, including details for indexing.
|
[
"Initialization",
"steps",
":"
] |
train
|
https://github.com/Parsely/birding/blob/c7f6eee56424234e361b1a455595de202e744dac/src/birding/bolt.py#L90-L99
|
Parsely/birding
|
src/birding/bolt.py
|
ElasticsearchIndexBolt.process
|
def process(self, tup):
"""Process steps:
1. Index third positional value from input to elasticsearch.
"""
self.es.bulk(
self.generate_bulk_body(tup.values[2]),
index=self.index,
doc_type=self.doc_type)
|
python
|
def process(self, tup):
"""Process steps:
1. Index third positional value from input to elasticsearch.
"""
self.es.bulk(
self.generate_bulk_body(tup.values[2]),
index=self.index,
doc_type=self.doc_type)
|
[
"def",
"process",
"(",
"self",
",",
"tup",
")",
":",
"self",
".",
"es",
".",
"bulk",
"(",
"self",
".",
"generate_bulk_body",
"(",
"tup",
".",
"values",
"[",
"2",
"]",
")",
",",
"index",
"=",
"self",
".",
"index",
",",
"doc_type",
"=",
"self",
".",
"doc_type",
")"
] |
Process steps:
1. Index third positional value from input to elasticsearch.
|
[
"Process",
"steps",
":"
] |
train
|
https://github.com/Parsely/birding/blob/c7f6eee56424234e361b1a455595de202e744dac/src/birding/bolt.py#L102-L110
|
Parsely/birding
|
src/birding/bolt.py
|
ResultTopicBolt.initialize
|
def initialize(self, conf, ctx):
"""Initialization steps:
1. Connect to Kafka.
2. Prepare Kafka producer for `tweet` topic.
3. Prepare to track tweets published to topic, to avoid redundant data.
"""
config = get_config()['ResultTopicBolt']
kafka_class = import_name(config['kafka_class'])
self.client = kafka_class(**config['kafka_init'])
self.topic = self.client.topics[config['topic']]
self.producer = self.topic.get_producer()
# Use own default index value while still allowing user config.
self.tweet_shelf = shelf_from_config(config, index='pre_kafka_shelf')
|
python
|
def initialize(self, conf, ctx):
"""Initialization steps:
1. Connect to Kafka.
2. Prepare Kafka producer for `tweet` topic.
3. Prepare to track tweets published to topic, to avoid redundant data.
"""
config = get_config()['ResultTopicBolt']
kafka_class = import_name(config['kafka_class'])
self.client = kafka_class(**config['kafka_init'])
self.topic = self.client.topics[config['topic']]
self.producer = self.topic.get_producer()
# Use own default index value while still allowing user config.
self.tweet_shelf = shelf_from_config(config, index='pre_kafka_shelf')
|
[
"def",
"initialize",
"(",
"self",
",",
"conf",
",",
"ctx",
")",
":",
"config",
"=",
"get_config",
"(",
")",
"[",
"'ResultTopicBolt'",
"]",
"kafka_class",
"=",
"import_name",
"(",
"config",
"[",
"'kafka_class'",
"]",
")",
"self",
".",
"client",
"=",
"kafka_class",
"(",
"*",
"*",
"config",
"[",
"'kafka_init'",
"]",
")",
"self",
".",
"topic",
"=",
"self",
".",
"client",
".",
"topics",
"[",
"config",
"[",
"'topic'",
"]",
"]",
"self",
".",
"producer",
"=",
"self",
".",
"topic",
".",
"get_producer",
"(",
")",
"# Use own default index value while still allowing user config.",
"self",
".",
"tweet_shelf",
"=",
"shelf_from_config",
"(",
"config",
",",
"index",
"=",
"'pre_kafka_shelf'",
")"
] |
Initialization steps:
1. Connect to Kafka.
2. Prepare Kafka producer for `tweet` topic.
3. Prepare to track tweets published to topic, to avoid redundant data.
|
[
"Initialization",
"steps",
":"
] |
train
|
https://github.com/Parsely/birding/blob/c7f6eee56424234e361b1a455595de202e744dac/src/birding/bolt.py#L120-L134
|
Parsely/birding
|
src/birding/bolt.py
|
ResultTopicBolt.process
|
def process(self, tup):
"""Process steps:
1. Stream third positional value from input into Kafka topic.
"""
status_seq = self.iter_using_shelf(tup.values[2], self.tweet_shelf)
# This could be more efficient by passing the result from twitter
# straight through to the producer, instead of deserializing and
# reserializing json.
self.producer.produce(json.dumps(status) for status in status_seq)
|
python
|
def process(self, tup):
"""Process steps:
1. Stream third positional value from input into Kafka topic.
"""
status_seq = self.iter_using_shelf(tup.values[2], self.tweet_shelf)
# This could be more efficient by passing the result from twitter
# straight through to the producer, instead of deserializing and
# reserializing json.
self.producer.produce(json.dumps(status) for status in status_seq)
|
[
"def",
"process",
"(",
"self",
",",
"tup",
")",
":",
"status_seq",
"=",
"self",
".",
"iter_using_shelf",
"(",
"tup",
".",
"values",
"[",
"2",
"]",
",",
"self",
".",
"tweet_shelf",
")",
"# This could be more efficient by passing the result from twitter",
"# straight through to the producer, instead of deserializing and",
"# reserializing json.",
"self",
".",
"producer",
".",
"produce",
"(",
"json",
".",
"dumps",
"(",
"status",
")",
"for",
"status",
"in",
"status_seq",
")"
] |
Process steps:
1. Stream third positional value from input into Kafka topic.
|
[
"Process",
"steps",
":"
] |
train
|
https://github.com/Parsely/birding/blob/c7f6eee56424234e361b1a455595de202e744dac/src/birding/bolt.py#L137-L146
|
Parsely/birding
|
src/birding/search.py
|
search_manager_from_config
|
def search_manager_from_config(config, **default_init):
"""Get a `SearchManager` instance dynamically based on config.
`config` is a dictionary containing ``class`` and ``init`` keys as defined
in :mod:`birding.config`.
"""
manager_cls = import_name(config['class'], default_ns='birding.search')
init = {}
init.update(default_init)
init.update(config['init'])
manager = manager_cls(**init)
return manager
|
python
|
def search_manager_from_config(config, **default_init):
"""Get a `SearchManager` instance dynamically based on config.
`config` is a dictionary containing ``class`` and ``init`` keys as defined
in :mod:`birding.config`.
"""
manager_cls = import_name(config['class'], default_ns='birding.search')
init = {}
init.update(default_init)
init.update(config['init'])
manager = manager_cls(**init)
return manager
|
[
"def",
"search_manager_from_config",
"(",
"config",
",",
"*",
"*",
"default_init",
")",
":",
"manager_cls",
"=",
"import_name",
"(",
"config",
"[",
"'class'",
"]",
",",
"default_ns",
"=",
"'birding.search'",
")",
"init",
"=",
"{",
"}",
"init",
".",
"update",
"(",
"default_init",
")",
"init",
".",
"update",
"(",
"config",
"[",
"'init'",
"]",
")",
"manager",
"=",
"manager_cls",
"(",
"*",
"*",
"init",
")",
"return",
"manager"
] |
Get a `SearchManager` instance dynamically based on config.
`config` is a dictionary containing ``class`` and ``init`` keys as defined
in :mod:`birding.config`.
|
[
"Get",
"a",
"SearchManager",
"instance",
"dynamically",
"based",
"on",
"config",
"."
] |
train
|
https://github.com/Parsely/birding/blob/c7f6eee56424234e361b1a455595de202e744dac/src/birding/search.py#L8-L19
|
harvard-nrg/yaxil
|
yaxil/bids/__init__.py
|
bids_from_config
|
def bids_from_config(sess, scans_metadata, config, out_base):
'''
Create a BIDS output directory from configuration file
'''
# get session and subject labels from scan metadata
_item = next(iter(scans_metadata))
session,subject = _item['session_label'],_item['subject_label']
# bids and sourcedata base directories
sourcedata_base = os.path.join(
out_base,
'sourcedata',
'sub-{0}'.format(legal.sub('', subject)),
'ses-{0}'.format(legal.sub('', session))
)
bids_base = os.path.join(
out_base,
'sub-{0}'.format(legal.sub('', subject)),
'ses-{0}'.format(legal.sub('', session))
)
# put arguments in a struct
args = commons.struct(
xnat=sess,
subject=subject,
session=session,
bids=bids_base,
sourcedata=sourcedata_base
)
# process func, anat, and fmap
func_refs = proc_func(config, args)
anat_refs = proc_anat(config, args)
fmap_refs = proc_fmap(config, args, func_refs)
|
python
|
def bids_from_config(sess, scans_metadata, config, out_base):
'''
Create a BIDS output directory from configuration file
'''
# get session and subject labels from scan metadata
_item = next(iter(scans_metadata))
session,subject = _item['session_label'],_item['subject_label']
# bids and sourcedata base directories
sourcedata_base = os.path.join(
out_base,
'sourcedata',
'sub-{0}'.format(legal.sub('', subject)),
'ses-{0}'.format(legal.sub('', session))
)
bids_base = os.path.join(
out_base,
'sub-{0}'.format(legal.sub('', subject)),
'ses-{0}'.format(legal.sub('', session))
)
# put arguments in a struct
args = commons.struct(
xnat=sess,
subject=subject,
session=session,
bids=bids_base,
sourcedata=sourcedata_base
)
# process func, anat, and fmap
func_refs = proc_func(config, args)
anat_refs = proc_anat(config, args)
fmap_refs = proc_fmap(config, args, func_refs)
|
[
"def",
"bids_from_config",
"(",
"sess",
",",
"scans_metadata",
",",
"config",
",",
"out_base",
")",
":",
"# get session and subject labels from scan metadata",
"_item",
"=",
"next",
"(",
"iter",
"(",
"scans_metadata",
")",
")",
"session",
",",
"subject",
"=",
"_item",
"[",
"'session_label'",
"]",
",",
"_item",
"[",
"'subject_label'",
"]",
"# bids and sourcedata base directories",
"sourcedata_base",
"=",
"os",
".",
"path",
".",
"join",
"(",
"out_base",
",",
"'sourcedata'",
",",
"'sub-{0}'",
".",
"format",
"(",
"legal",
".",
"sub",
"(",
"''",
",",
"subject",
")",
")",
",",
"'ses-{0}'",
".",
"format",
"(",
"legal",
".",
"sub",
"(",
"''",
",",
"session",
")",
")",
")",
"bids_base",
"=",
"os",
".",
"path",
".",
"join",
"(",
"out_base",
",",
"'sub-{0}'",
".",
"format",
"(",
"legal",
".",
"sub",
"(",
"''",
",",
"subject",
")",
")",
",",
"'ses-{0}'",
".",
"format",
"(",
"legal",
".",
"sub",
"(",
"''",
",",
"session",
")",
")",
")",
"# put arguments in a struct",
"args",
"=",
"commons",
".",
"struct",
"(",
"xnat",
"=",
"sess",
",",
"subject",
"=",
"subject",
",",
"session",
"=",
"session",
",",
"bids",
"=",
"bids_base",
",",
"sourcedata",
"=",
"sourcedata_base",
")",
"# process func, anat, and fmap",
"func_refs",
"=",
"proc_func",
"(",
"config",
",",
"args",
")",
"anat_refs",
"=",
"proc_anat",
"(",
"config",
",",
"args",
")",
"fmap_refs",
"=",
"proc_fmap",
"(",
"config",
",",
"args",
",",
"func_refs",
")"
] |
Create a BIDS output directory from configuration file
|
[
"Create",
"a",
"BIDS",
"output",
"directory",
"from",
"configuration",
"file"
] |
train
|
https://github.com/harvard-nrg/yaxil/blob/af594082258e62d1904d6e6841fce0bb5c0bf309/yaxil/bids/__init__.py#L14-L44
|
harvard-nrg/yaxil
|
yaxil/bids/__init__.py
|
proc_anat
|
def proc_anat(config, args):
'''
Download anatomical data and convert to BIDS
'''
refs = dict()
for scan in iterconfig(config, 'anat'):
ref = scan.get('id', None)
templ = 'sub-${sub}_ses-${ses}'
if 'acquisition' in scan:
templ += '_acq-${acquisition}'
if 'run' in scan:
templ += '_run-${run}'
templ += '_${modality}'
templ = string.Template(templ)
fbase = templ.safe_substitute(
sub=legal.sub('', args.subject),
ses=legal.sub('', args.session),
acquisition=scan.get('acquisition', None),
run=scan.get('run', None),
modality=scan.get('modality', None),
)
# download data to bids sourcedata directory
sourcedata_dir = os.path.join(args.sourcedata, scan['type'])
if not os.path.exists(sourcedata_dir):
os.makedirs(sourcedata_dir)
dicom_dir = os.path.join(sourcedata_dir, '{0}.dicom'.format(fbase))
logger.info('downloading session=%s, scan=%s, loc=%s', args.session, scan['scan'], dicom_dir)
args.xnat.download(args.session, [scan['scan']], out_dir=dicom_dir)
# convert to nifti
fname = '{0}.nii.gz'.format(fbase)
refs[ref] = os.path.join(scan['type'], fname)
fullfile = os.path.join(args.bids, scan['type'], fname)
logger.info('converting %s to %s', dicom_dir, fullfile)
convert(dicom_dir, fullfile)
return refs
|
python
|
def proc_anat(config, args):
'''
Download anatomical data and convert to BIDS
'''
refs = dict()
for scan in iterconfig(config, 'anat'):
ref = scan.get('id', None)
templ = 'sub-${sub}_ses-${ses}'
if 'acquisition' in scan:
templ += '_acq-${acquisition}'
if 'run' in scan:
templ += '_run-${run}'
templ += '_${modality}'
templ = string.Template(templ)
fbase = templ.safe_substitute(
sub=legal.sub('', args.subject),
ses=legal.sub('', args.session),
acquisition=scan.get('acquisition', None),
run=scan.get('run', None),
modality=scan.get('modality', None),
)
# download data to bids sourcedata directory
sourcedata_dir = os.path.join(args.sourcedata, scan['type'])
if not os.path.exists(sourcedata_dir):
os.makedirs(sourcedata_dir)
dicom_dir = os.path.join(sourcedata_dir, '{0}.dicom'.format(fbase))
logger.info('downloading session=%s, scan=%s, loc=%s', args.session, scan['scan'], dicom_dir)
args.xnat.download(args.session, [scan['scan']], out_dir=dicom_dir)
# convert to nifti
fname = '{0}.nii.gz'.format(fbase)
refs[ref] = os.path.join(scan['type'], fname)
fullfile = os.path.join(args.bids, scan['type'], fname)
logger.info('converting %s to %s', dicom_dir, fullfile)
convert(dicom_dir, fullfile)
return refs
|
[
"def",
"proc_anat",
"(",
"config",
",",
"args",
")",
":",
"refs",
"=",
"dict",
"(",
")",
"for",
"scan",
"in",
"iterconfig",
"(",
"config",
",",
"'anat'",
")",
":",
"ref",
"=",
"scan",
".",
"get",
"(",
"'id'",
",",
"None",
")",
"templ",
"=",
"'sub-${sub}_ses-${ses}'",
"if",
"'acquisition'",
"in",
"scan",
":",
"templ",
"+=",
"'_acq-${acquisition}'",
"if",
"'run'",
"in",
"scan",
":",
"templ",
"+=",
"'_run-${run}'",
"templ",
"+=",
"'_${modality}'",
"templ",
"=",
"string",
".",
"Template",
"(",
"templ",
")",
"fbase",
"=",
"templ",
".",
"safe_substitute",
"(",
"sub",
"=",
"legal",
".",
"sub",
"(",
"''",
",",
"args",
".",
"subject",
")",
",",
"ses",
"=",
"legal",
".",
"sub",
"(",
"''",
",",
"args",
".",
"session",
")",
",",
"acquisition",
"=",
"scan",
".",
"get",
"(",
"'acquisition'",
",",
"None",
")",
",",
"run",
"=",
"scan",
".",
"get",
"(",
"'run'",
",",
"None",
")",
",",
"modality",
"=",
"scan",
".",
"get",
"(",
"'modality'",
",",
"None",
")",
",",
")",
"# download data to bids sourcedata directory",
"sourcedata_dir",
"=",
"os",
".",
"path",
".",
"join",
"(",
"args",
".",
"sourcedata",
",",
"scan",
"[",
"'type'",
"]",
")",
"if",
"not",
"os",
".",
"path",
".",
"exists",
"(",
"sourcedata_dir",
")",
":",
"os",
".",
"makedirs",
"(",
"sourcedata_dir",
")",
"dicom_dir",
"=",
"os",
".",
"path",
".",
"join",
"(",
"sourcedata_dir",
",",
"'{0}.dicom'",
".",
"format",
"(",
"fbase",
")",
")",
"logger",
".",
"info",
"(",
"'downloading session=%s, scan=%s, loc=%s'",
",",
"args",
".",
"session",
",",
"scan",
"[",
"'scan'",
"]",
",",
"dicom_dir",
")",
"args",
".",
"xnat",
".",
"download",
"(",
"args",
".",
"session",
",",
"[",
"scan",
"[",
"'scan'",
"]",
"]",
",",
"out_dir",
"=",
"dicom_dir",
")",
"# convert to nifti",
"fname",
"=",
"'{0}.nii.gz'",
".",
"format",
"(",
"fbase",
")",
"refs",
"[",
"ref",
"]",
"=",
"os",
".",
"path",
".",
"join",
"(",
"scan",
"[",
"'type'",
"]",
",",
"fname",
")",
"fullfile",
"=",
"os",
".",
"path",
".",
"join",
"(",
"args",
".",
"bids",
",",
"scan",
"[",
"'type'",
"]",
",",
"fname",
")",
"logger",
".",
"info",
"(",
"'converting %s to %s'",
",",
"dicom_dir",
",",
"fullfile",
")",
"convert",
"(",
"dicom_dir",
",",
"fullfile",
")",
"return",
"refs"
] |
Download anatomical data and convert to BIDS
|
[
"Download",
"anatomical",
"data",
"and",
"convert",
"to",
"BIDS"
] |
train
|
https://github.com/harvard-nrg/yaxil/blob/af594082258e62d1904d6e6841fce0bb5c0bf309/yaxil/bids/__init__.py#L86-L120
|
harvard-nrg/yaxil
|
yaxil/bids/__init__.py
|
iterconfig
|
def iterconfig(config, scan_type):
'''
Iterate over BIDS configuration file
'''
if scan_type in config:
for modality,scans in iter(config[scan_type].items()):
for scan in scans:
scan.update({
'type': scan_type,
'modality': modality
})
yield scan
|
python
|
def iterconfig(config, scan_type):
'''
Iterate over BIDS configuration file
'''
if scan_type in config:
for modality,scans in iter(config[scan_type].items()):
for scan in scans:
scan.update({
'type': scan_type,
'modality': modality
})
yield scan
|
[
"def",
"iterconfig",
"(",
"config",
",",
"scan_type",
")",
":",
"if",
"scan_type",
"in",
"config",
":",
"for",
"modality",
",",
"scans",
"in",
"iter",
"(",
"config",
"[",
"scan_type",
"]",
".",
"items",
"(",
")",
")",
":",
"for",
"scan",
"in",
"scans",
":",
"scan",
".",
"update",
"(",
"{",
"'type'",
":",
"scan_type",
",",
"'modality'",
":",
"modality",
"}",
")",
"yield",
"scan"
] |
Iterate over BIDS configuration file
|
[
"Iterate",
"over",
"BIDS",
"configuration",
"file"
] |
train
|
https://github.com/harvard-nrg/yaxil/blob/af594082258e62d1904d6e6841fce0bb5c0bf309/yaxil/bids/__init__.py#L181-L192
|
harvard-nrg/yaxil
|
yaxil/bids/__init__.py
|
rename_fmapm
|
def rename_fmapm(bids_base, basename):
'''
Rename magnitude fieldmap file to BIDS specification
'''
files = dict()
for ext in ['nii.gz', 'json']:
for echo in [1, 2]:
fname = '{0}_e{1}.{2}'.format(basename, echo, ext)
src = os.path.join(bids_base, 'fmap', fname)
if os.path.exists(src):
dst = src.replace(
'magnitude_e{0}'.format(echo),
'magnitude{0}'.format(echo)
)
logger.debug('renaming %s to %s', src, dst)
os.rename(src, dst)
files[ext] = dst
return files
|
python
|
def rename_fmapm(bids_base, basename):
'''
Rename magnitude fieldmap file to BIDS specification
'''
files = dict()
for ext in ['nii.gz', 'json']:
for echo in [1, 2]:
fname = '{0}_e{1}.{2}'.format(basename, echo, ext)
src = os.path.join(bids_base, 'fmap', fname)
if os.path.exists(src):
dst = src.replace(
'magnitude_e{0}'.format(echo),
'magnitude{0}'.format(echo)
)
logger.debug('renaming %s to %s', src, dst)
os.rename(src, dst)
files[ext] = dst
return files
|
[
"def",
"rename_fmapm",
"(",
"bids_base",
",",
"basename",
")",
":",
"files",
"=",
"dict",
"(",
")",
"for",
"ext",
"in",
"[",
"'nii.gz'",
",",
"'json'",
"]",
":",
"for",
"echo",
"in",
"[",
"1",
",",
"2",
"]",
":",
"fname",
"=",
"'{0}_e{1}.{2}'",
".",
"format",
"(",
"basename",
",",
"echo",
",",
"ext",
")",
"src",
"=",
"os",
".",
"path",
".",
"join",
"(",
"bids_base",
",",
"'fmap'",
",",
"fname",
")",
"if",
"os",
".",
"path",
".",
"exists",
"(",
"src",
")",
":",
"dst",
"=",
"src",
".",
"replace",
"(",
"'magnitude_e{0}'",
".",
"format",
"(",
"echo",
")",
",",
"'magnitude{0}'",
".",
"format",
"(",
"echo",
")",
")",
"logger",
".",
"debug",
"(",
"'renaming %s to %s'",
",",
"src",
",",
"dst",
")",
"os",
".",
"rename",
"(",
"src",
",",
"dst",
")",
"files",
"[",
"ext",
"]",
"=",
"dst",
"return",
"files"
] |
Rename magnitude fieldmap file to BIDS specification
|
[
"Rename",
"magnitude",
"fieldmap",
"file",
"to",
"BIDS",
"specification"
] |
train
|
https://github.com/harvard-nrg/yaxil/blob/af594082258e62d1904d6e6841fce0bb5c0bf309/yaxil/bids/__init__.py#L194-L211
|
harvard-nrg/yaxil
|
yaxil/bids/__init__.py
|
convert
|
def convert(input, output):
'''
Run dcm2niix on input file
'''
dirname = os.path.dirname(output)
if not os.path.exists(dirname):
os.makedirs(dirname)
basename = os.path.basename(output)
basename = re.sub('.nii(.gz)?', '', basename)
dcm2niix = commons.which('dcm2niix')
cmd = [
'dcm2niix',
'-s', 'y',
'-b', 'y',
'-z', 'y',
'-f', basename,
'-o', dirname,
input
]
logger.debug(cmd)
sp.check_output(cmd)
|
python
|
def convert(input, output):
'''
Run dcm2niix on input file
'''
dirname = os.path.dirname(output)
if not os.path.exists(dirname):
os.makedirs(dirname)
basename = os.path.basename(output)
basename = re.sub('.nii(.gz)?', '', basename)
dcm2niix = commons.which('dcm2niix')
cmd = [
'dcm2niix',
'-s', 'y',
'-b', 'y',
'-z', 'y',
'-f', basename,
'-o', dirname,
input
]
logger.debug(cmd)
sp.check_output(cmd)
|
[
"def",
"convert",
"(",
"input",
",",
"output",
")",
":",
"dirname",
"=",
"os",
".",
"path",
".",
"dirname",
"(",
"output",
")",
"if",
"not",
"os",
".",
"path",
".",
"exists",
"(",
"dirname",
")",
":",
"os",
".",
"makedirs",
"(",
"dirname",
")",
"basename",
"=",
"os",
".",
"path",
".",
"basename",
"(",
"output",
")",
"basename",
"=",
"re",
".",
"sub",
"(",
"'.nii(.gz)?'",
",",
"''",
",",
"basename",
")",
"dcm2niix",
"=",
"commons",
".",
"which",
"(",
"'dcm2niix'",
")",
"cmd",
"=",
"[",
"'dcm2niix'",
",",
"'-s'",
",",
"'y'",
",",
"'-b'",
",",
"'y'",
",",
"'-z'",
",",
"'y'",
",",
"'-f'",
",",
"basename",
",",
"'-o'",
",",
"dirname",
",",
"input",
"]",
"logger",
".",
"debug",
"(",
"cmd",
")",
"sp",
".",
"check_output",
"(",
"cmd",
")"
] |
Run dcm2niix on input file
|
[
"Run",
"dcm2niix",
"on",
"input",
"file"
] |
train
|
https://github.com/harvard-nrg/yaxil/blob/af594082258e62d1904d6e6841fce0bb5c0bf309/yaxil/bids/__init__.py#L231-L251
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/api.py
|
api_errors_to_messages
|
def api_errors_to_messages(request, error, fallback_text):
"""
Adds messages for each error returned from the MTP apis
Useful for displaying errors when there's no form on the page
:param request: HttpRequest
:param error: HttpClientError
:param fallback_text: fallback error text
"""
try:
response_body = json.loads(error.content.decode('utf-8'))
for field, errors in response_body.items():
if isinstance(errors, list):
for error in errors:
messages.error(request, error)
else:
messages.error(request, errors)
except (AttributeError, ValueError, KeyError):
messages.error(request, fallback_text)
|
python
|
def api_errors_to_messages(request, error, fallback_text):
"""
Adds messages for each error returned from the MTP apis
Useful for displaying errors when there's no form on the page
:param request: HttpRequest
:param error: HttpClientError
:param fallback_text: fallback error text
"""
try:
response_body = json.loads(error.content.decode('utf-8'))
for field, errors in response_body.items():
if isinstance(errors, list):
for error in errors:
messages.error(request, error)
else:
messages.error(request, errors)
except (AttributeError, ValueError, KeyError):
messages.error(request, fallback_text)
|
[
"def",
"api_errors_to_messages",
"(",
"request",
",",
"error",
",",
"fallback_text",
")",
":",
"try",
":",
"response_body",
"=",
"json",
".",
"loads",
"(",
"error",
".",
"content",
".",
"decode",
"(",
"'utf-8'",
")",
")",
"for",
"field",
",",
"errors",
"in",
"response_body",
".",
"items",
"(",
")",
":",
"if",
"isinstance",
"(",
"errors",
",",
"list",
")",
":",
"for",
"error",
"in",
"errors",
":",
"messages",
".",
"error",
"(",
"request",
",",
"error",
")",
"else",
":",
"messages",
".",
"error",
"(",
"request",
",",
"errors",
")",
"except",
"(",
"AttributeError",
",",
"ValueError",
",",
"KeyError",
")",
":",
"messages",
".",
"error",
"(",
"request",
",",
"fallback_text",
")"
] |
Adds messages for each error returned from the MTP apis
Useful for displaying errors when there's no form on the page
:param request: HttpRequest
:param error: HttpClientError
:param fallback_text: fallback error text
|
[
"Adds",
"messages",
"for",
"each",
"error",
"returned",
"from",
"the",
"MTP",
"apis",
"Useful",
"for",
"displaying",
"errors",
"when",
"there",
"s",
"no",
"form",
"on",
"the",
"page",
":",
"param",
"request",
":",
"HttpRequest",
":",
"param",
"error",
":",
"HttpClientError",
":",
"param",
"fallback_text",
":",
"fallback",
"error",
"text"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/api.py#L14-L31
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/api.py
|
retrieve_all_pages
|
def retrieve_all_pages(api_endpoint, **kwargs):
"""
Some MTP apis are paginated using Django Rest Framework's LimitOffsetPagination paginator,
this method loads all pages into a single results list
:param api_endpoint: slumber callable, e.g. `[api_client].cashbook.transactions.locked.get`
:param kwargs: additional arguments to pass into api callable
"""
page_size = getattr(settings, 'REQUEST_PAGE_SIZE', 20)
loaded_results = []
offset = 0
while True:
response = api_endpoint(limit=page_size, offset=offset,
**kwargs)
count = response.get('count', 0)
loaded_results += response.get('results', [])
if len(loaded_results) >= count:
break
offset += page_size
return loaded_results
|
python
|
def retrieve_all_pages(api_endpoint, **kwargs):
"""
Some MTP apis are paginated using Django Rest Framework's LimitOffsetPagination paginator,
this method loads all pages into a single results list
:param api_endpoint: slumber callable, e.g. `[api_client].cashbook.transactions.locked.get`
:param kwargs: additional arguments to pass into api callable
"""
page_size = getattr(settings, 'REQUEST_PAGE_SIZE', 20)
loaded_results = []
offset = 0
while True:
response = api_endpoint(limit=page_size, offset=offset,
**kwargs)
count = response.get('count', 0)
loaded_results += response.get('results', [])
if len(loaded_results) >= count:
break
offset += page_size
return loaded_results
|
[
"def",
"retrieve_all_pages",
"(",
"api_endpoint",
",",
"*",
"*",
"kwargs",
")",
":",
"page_size",
"=",
"getattr",
"(",
"settings",
",",
"'REQUEST_PAGE_SIZE'",
",",
"20",
")",
"loaded_results",
"=",
"[",
"]",
"offset",
"=",
"0",
"while",
"True",
":",
"response",
"=",
"api_endpoint",
"(",
"limit",
"=",
"page_size",
",",
"offset",
"=",
"offset",
",",
"*",
"*",
"kwargs",
")",
"count",
"=",
"response",
".",
"get",
"(",
"'count'",
",",
"0",
")",
"loaded_results",
"+=",
"response",
".",
"get",
"(",
"'results'",
",",
"[",
"]",
")",
"if",
"len",
"(",
"loaded_results",
")",
">=",
"count",
":",
"break",
"offset",
"+=",
"page_size",
"return",
"loaded_results"
] |
Some MTP apis are paginated using Django Rest Framework's LimitOffsetPagination paginator,
this method loads all pages into a single results list
:param api_endpoint: slumber callable, e.g. `[api_client].cashbook.transactions.locked.get`
:param kwargs: additional arguments to pass into api callable
|
[
"Some",
"MTP",
"apis",
"are",
"paginated",
"using",
"Django",
"Rest",
"Framework",
"s",
"LimitOffsetPagination",
"paginator",
"this",
"method",
"loads",
"all",
"pages",
"into",
"a",
"single",
"results",
"list",
":",
"param",
"api_endpoint",
":",
"slumber",
"callable",
"e",
".",
"g",
".",
"[",
"api_client",
"]",
".",
"cashbook",
".",
"transactions",
".",
"locked",
".",
"get",
":",
"param",
"kwargs",
":",
"additional",
"arguments",
"to",
"pass",
"into",
"api",
"callable"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/api.py#L34-L54
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/api.py
|
retrieve_all_pages_for_path
|
def retrieve_all_pages_for_path(session, path, **params):
"""
Some MTP apis are paginated using Django Rest Framework's LimitOffsetPagination paginator,
this method loads all pages into a single results list
:param session: Requests Session object
:param path: URL path
:param params: additional URL params
"""
page_size = getattr(settings, 'REQUEST_PAGE_SIZE', 20)
loaded_results = []
offset = 0
while True:
response = session.get(
path,
params=dict(limit=page_size, offset=offset, **params)
)
content = response.json()
count = content.get('count', 0)
loaded_results += content.get('results', [])
if len(loaded_results) >= count:
break
offset += page_size
return loaded_results
|
python
|
def retrieve_all_pages_for_path(session, path, **params):
"""
Some MTP apis are paginated using Django Rest Framework's LimitOffsetPagination paginator,
this method loads all pages into a single results list
:param session: Requests Session object
:param path: URL path
:param params: additional URL params
"""
page_size = getattr(settings, 'REQUEST_PAGE_SIZE', 20)
loaded_results = []
offset = 0
while True:
response = session.get(
path,
params=dict(limit=page_size, offset=offset, **params)
)
content = response.json()
count = content.get('count', 0)
loaded_results += content.get('results', [])
if len(loaded_results) >= count:
break
offset += page_size
return loaded_results
|
[
"def",
"retrieve_all_pages_for_path",
"(",
"session",
",",
"path",
",",
"*",
"*",
"params",
")",
":",
"page_size",
"=",
"getattr",
"(",
"settings",
",",
"'REQUEST_PAGE_SIZE'",
",",
"20",
")",
"loaded_results",
"=",
"[",
"]",
"offset",
"=",
"0",
"while",
"True",
":",
"response",
"=",
"session",
".",
"get",
"(",
"path",
",",
"params",
"=",
"dict",
"(",
"limit",
"=",
"page_size",
",",
"offset",
"=",
"offset",
",",
"*",
"*",
"params",
")",
")",
"content",
"=",
"response",
".",
"json",
"(",
")",
"count",
"=",
"content",
".",
"get",
"(",
"'count'",
",",
"0",
")",
"loaded_results",
"+=",
"content",
".",
"get",
"(",
"'results'",
",",
"[",
"]",
")",
"if",
"len",
"(",
"loaded_results",
")",
">=",
"count",
":",
"break",
"offset",
"+=",
"page_size",
"return",
"loaded_results"
] |
Some MTP apis are paginated using Django Rest Framework's LimitOffsetPagination paginator,
this method loads all pages into a single results list
:param session: Requests Session object
:param path: URL path
:param params: additional URL params
|
[
"Some",
"MTP",
"apis",
"are",
"paginated",
"using",
"Django",
"Rest",
"Framework",
"s",
"LimitOffsetPagination",
"paginator",
"this",
"method",
"loads",
"all",
"pages",
"into",
"a",
"single",
"results",
"list",
":",
"param",
"session",
":",
"Requests",
"Session",
"object",
":",
"param",
"path",
":",
"URL",
"path",
":",
"param",
"params",
":",
"additional",
"URL",
"params"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/api.py#L57-L81
|
Parsely/birding
|
src/birding/spout.py
|
DispatchSpout
|
def DispatchSpout(*a, **kw):
"""Factory to dispatch spout class based on config."""
spout_class_name = get_config()['Spout']
spout_class = import_name(spout_class_name, default_ns='birding.spout')
return spout_class(*a, **kw)
|
python
|
def DispatchSpout(*a, **kw):
"""Factory to dispatch spout class based on config."""
spout_class_name = get_config()['Spout']
spout_class = import_name(spout_class_name, default_ns='birding.spout')
return spout_class(*a, **kw)
|
[
"def",
"DispatchSpout",
"(",
"*",
"a",
",",
"*",
"*",
"kw",
")",
":",
"spout_class_name",
"=",
"get_config",
"(",
")",
"[",
"'Spout'",
"]",
"spout_class",
"=",
"import_name",
"(",
"spout_class_name",
",",
"default_ns",
"=",
"'birding.spout'",
")",
"return",
"spout_class",
"(",
"*",
"a",
",",
"*",
"*",
"kw",
")"
] |
Factory to dispatch spout class based on config.
|
[
"Factory",
"to",
"dispatch",
"spout",
"class",
"based",
"on",
"config",
"."
] |
train
|
https://github.com/Parsely/birding/blob/c7f6eee56424234e361b1a455595de202e744dac/src/birding/spout.py#L11-L15
|
Parsely/birding
|
src/birding/spout.py
|
TermCycleSpout.initialize
|
def initialize(self, stormconf, context):
"""Initialization steps:
1. Prepare sequence of terms based on config: TermCycleSpout/terms.
"""
self.terms = get_config()['TermCycleSpout']['terms']
self.term_seq = itertools.cycle(self.terms)
|
python
|
def initialize(self, stormconf, context):
"""Initialization steps:
1. Prepare sequence of terms based on config: TermCycleSpout/terms.
"""
self.terms = get_config()['TermCycleSpout']['terms']
self.term_seq = itertools.cycle(self.terms)
|
[
"def",
"initialize",
"(",
"self",
",",
"stormconf",
",",
"context",
")",
":",
"self",
".",
"terms",
"=",
"get_config",
"(",
")",
"[",
"'TermCycleSpout'",
"]",
"[",
"'terms'",
"]",
"self",
".",
"term_seq",
"=",
"itertools",
".",
"cycle",
"(",
"self",
".",
"terms",
")"
] |
Initialization steps:
1. Prepare sequence of terms based on config: TermCycleSpout/terms.
|
[
"Initialization",
"steps",
":"
] |
train
|
https://github.com/Parsely/birding/blob/c7f6eee56424234e361b1a455595de202e744dac/src/birding/spout.py#L45-L51
|
Parsely/birding
|
src/birding/spout.py
|
TermCycleSpout.next_tuple
|
def next_tuple(self):
"""Next tuple steps:
1. Emit (term, timestamp) for next term in sequence w/current UTC time.
"""
term = next(self.term_seq)
timestamp = datetime.datetime.utcnow().isoformat()
self.emit([term, timestamp], tup_id=self.pack_tup_id(term, timestamp))
|
python
|
def next_tuple(self):
"""Next tuple steps:
1. Emit (term, timestamp) for next term in sequence w/current UTC time.
"""
term = next(self.term_seq)
timestamp = datetime.datetime.utcnow().isoformat()
self.emit([term, timestamp], tup_id=self.pack_tup_id(term, timestamp))
|
[
"def",
"next_tuple",
"(",
"self",
")",
":",
"term",
"=",
"next",
"(",
"self",
".",
"term_seq",
")",
"timestamp",
"=",
"datetime",
".",
"datetime",
".",
"utcnow",
"(",
")",
".",
"isoformat",
"(",
")",
"self",
".",
"emit",
"(",
"[",
"term",
",",
"timestamp",
"]",
",",
"tup_id",
"=",
"self",
".",
"pack_tup_id",
"(",
"term",
",",
"timestamp",
")",
")"
] |
Next tuple steps:
1. Emit (term, timestamp) for next term in sequence w/current UTC time.
|
[
"Next",
"tuple",
"steps",
":"
] |
train
|
https://github.com/Parsely/birding/blob/c7f6eee56424234e361b1a455595de202e744dac/src/birding/spout.py#L53-L60
|
harvard-nrg/yaxil
|
yaxil/commons/__init__.py
|
cast
|
def cast(s):
'''
Cast a basestring to a more appropriate type.
Example::
>>> from yaxil import cast
>>> type(cast("999"))
<type 'int'>
:param s: String
:type s: basestring
:returns: Casted string
:rtype: int|float|str
'''
if not isinstance(s, basestring):
raise TypeError("argument must be a string")
for test in [int, float, str]:
try:
return test(s)
except ValueError:
continue
return str(s)
|
python
|
def cast(s):
'''
Cast a basestring to a more appropriate type.
Example::
>>> from yaxil import cast
>>> type(cast("999"))
<type 'int'>
:param s: String
:type s: basestring
:returns: Casted string
:rtype: int|float|str
'''
if not isinstance(s, basestring):
raise TypeError("argument must be a string")
for test in [int, float, str]:
try:
return test(s)
except ValueError:
continue
return str(s)
|
[
"def",
"cast",
"(",
"s",
")",
":",
"if",
"not",
"isinstance",
"(",
"s",
",",
"basestring",
")",
":",
"raise",
"TypeError",
"(",
"\"argument must be a string\"",
")",
"for",
"test",
"in",
"[",
"int",
",",
"float",
",",
"str",
"]",
":",
"try",
":",
"return",
"test",
"(",
"s",
")",
"except",
"ValueError",
":",
"continue",
"return",
"str",
"(",
"s",
")"
] |
Cast a basestring to a more appropriate type.
Example::
>>> from yaxil import cast
>>> type(cast("999"))
<type 'int'>
:param s: String
:type s: basestring
:returns: Casted string
:rtype: int|float|str
|
[
"Cast",
"a",
"basestring",
"to",
"a",
"more",
"appropriate",
"type",
".",
"Example",
"::",
">>>",
"from",
"yaxil",
"import",
"cast",
">>>",
"type",
"(",
"cast",
"(",
"999",
"))",
"<type",
"int",
">",
":",
"param",
"s",
":",
"String",
":",
"type",
"s",
":",
"basestring",
":",
"returns",
":",
"Casted",
"string",
":",
"rtype",
":",
"int|float|str"
] |
train
|
https://github.com/harvard-nrg/yaxil/blob/af594082258e62d1904d6e6841fce0bb5c0bf309/yaxil/commons/__init__.py#L26-L46
|
harvard-nrg/yaxil
|
yaxil/commons/__init__.py
|
atomic_write
|
def atomic_write(filename, content, overwrite=True, permissions=0o0644, encoding='utf-8'):
'''
Write a file atomically by writing the file content to a
temporary location first, then renaming the file.
TODO: this relies pretty heavily on os.rename to ensure atomicity, but
os.rename does not silently overwrite files that already exist on
Windows natively. For now, the functionality provided here can only be
supported under Windows Subsystem for Linux on Windows 10 version 1607
and later.
:param filename: Filename
:type filename: str
:param content: File content
:type content: str
:param overwrite: Overwrite
:type overwrite: bool
:param permissions: Octal permissions
:type permissions: octal
'''
filename = os.path.expanduser(filename)
if not overwrite and os.path.exists(filename):
raise WriteError('file already exists: {0}'.format(filename))
dirname = os.path.dirname(filename)
with tf.NamedTemporaryFile(dir=dirname, prefix='.', delete=False) as tmp:
if isinstance(content, six.string_types):
tmp.write(content.decode(encoding))
else:
tmp.write(content)
os.chmod(tmp.name, permissions)
os.rename(tmp.name, filename)
|
python
|
def atomic_write(filename, content, overwrite=True, permissions=0o0644, encoding='utf-8'):
'''
Write a file atomically by writing the file content to a
temporary location first, then renaming the file.
TODO: this relies pretty heavily on os.rename to ensure atomicity, but
os.rename does not silently overwrite files that already exist on
Windows natively. For now, the functionality provided here can only be
supported under Windows Subsystem for Linux on Windows 10 version 1607
and later.
:param filename: Filename
:type filename: str
:param content: File content
:type content: str
:param overwrite: Overwrite
:type overwrite: bool
:param permissions: Octal permissions
:type permissions: octal
'''
filename = os.path.expanduser(filename)
if not overwrite and os.path.exists(filename):
raise WriteError('file already exists: {0}'.format(filename))
dirname = os.path.dirname(filename)
with tf.NamedTemporaryFile(dir=dirname, prefix='.', delete=False) as tmp:
if isinstance(content, six.string_types):
tmp.write(content.decode(encoding))
else:
tmp.write(content)
os.chmod(tmp.name, permissions)
os.rename(tmp.name, filename)
|
[
"def",
"atomic_write",
"(",
"filename",
",",
"content",
",",
"overwrite",
"=",
"True",
",",
"permissions",
"=",
"0o0644",
",",
"encoding",
"=",
"'utf-8'",
")",
":",
"filename",
"=",
"os",
".",
"path",
".",
"expanduser",
"(",
"filename",
")",
"if",
"not",
"overwrite",
"and",
"os",
".",
"path",
".",
"exists",
"(",
"filename",
")",
":",
"raise",
"WriteError",
"(",
"'file already exists: {0}'",
".",
"format",
"(",
"filename",
")",
")",
"dirname",
"=",
"os",
".",
"path",
".",
"dirname",
"(",
"filename",
")",
"with",
"tf",
".",
"NamedTemporaryFile",
"(",
"dir",
"=",
"dirname",
",",
"prefix",
"=",
"'.'",
",",
"delete",
"=",
"False",
")",
"as",
"tmp",
":",
"if",
"isinstance",
"(",
"content",
",",
"six",
".",
"string_types",
")",
":",
"tmp",
".",
"write",
"(",
"content",
".",
"decode",
"(",
"encoding",
")",
")",
"else",
":",
"tmp",
".",
"write",
"(",
"content",
")",
"os",
".",
"chmod",
"(",
"tmp",
".",
"name",
",",
"permissions",
")",
"os",
".",
"rename",
"(",
"tmp",
".",
"name",
",",
"filename",
")"
] |
Write a file atomically by writing the file content to a
temporary location first, then renaming the file.
TODO: this relies pretty heavily on os.rename to ensure atomicity, but
os.rename does not silently overwrite files that already exist on
Windows natively. For now, the functionality provided here can only be
supported under Windows Subsystem for Linux on Windows 10 version 1607
and later.
:param filename: Filename
:type filename: str
:param content: File content
:type content: str
:param overwrite: Overwrite
:type overwrite: bool
:param permissions: Octal permissions
:type permissions: octal
|
[
"Write",
"a",
"file",
"atomically",
"by",
"writing",
"the",
"file",
"content",
"to",
"a",
"temporary",
"location",
"first",
"then",
"renaming",
"the",
"file",
".",
"TODO",
":",
"this",
"relies",
"pretty",
"heavily",
"on",
"os",
".",
"rename",
"to",
"ensure",
"atomicity",
"but",
"os",
".",
"rename",
"does",
"not",
"silently",
"overwrite",
"files",
"that",
"already",
"exist",
"on",
"Windows",
"natively",
".",
"For",
"now",
"the",
"functionality",
"provided",
"here",
"can",
"only",
"be",
"supported",
"under",
"Windows",
"Subsystem",
"for",
"Linux",
"on",
"Windows",
"10",
"version",
"1607",
"and",
"later",
"."
] |
train
|
https://github.com/harvard-nrg/yaxil/blob/af594082258e62d1904d6e6841fce0bb5c0bf309/yaxil/commons/__init__.py#L48-L78
|
harvard-nrg/yaxil
|
yaxil/commons/__init__.py
|
which
|
def which(x):
'''
Same as which command on Linux
'''
for p in os.environ.get('PATH').split(os.pathsep):
p = os.path.join(p, x)
if os.path.exists(p):
return os.path.abspath(p)
return None
|
python
|
def which(x):
'''
Same as which command on Linux
'''
for p in os.environ.get('PATH').split(os.pathsep):
p = os.path.join(p, x)
if os.path.exists(p):
return os.path.abspath(p)
return None
|
[
"def",
"which",
"(",
"x",
")",
":",
"for",
"p",
"in",
"os",
".",
"environ",
".",
"get",
"(",
"'PATH'",
")",
".",
"split",
"(",
"os",
".",
"pathsep",
")",
":",
"p",
"=",
"os",
".",
"path",
".",
"join",
"(",
"p",
",",
"x",
")",
"if",
"os",
".",
"path",
".",
"exists",
"(",
"p",
")",
":",
"return",
"os",
".",
"path",
".",
"abspath",
"(",
"p",
")",
"return",
"None"
] |
Same as which command on Linux
|
[
"Same",
"as",
"which",
"command",
"on",
"Linux"
] |
train
|
https://github.com/harvard-nrg/yaxil/blob/af594082258e62d1904d6e6841fce0bb5c0bf309/yaxil/commons/__init__.py#L83-L91
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/build_tasks/tasks.py
|
serve
|
def serve(context: Context, port=8000, browsersync_port=3000, browsersync_ui_port=3030):
"""
Starts a development server with auto-building and live-reload
"""
try:
from watchdog.observers import Observer
except ImportError:
context.pip_command('install', 'watchdog>0.8,<0.9')
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
class RebuildHandler(PatternMatchingEventHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._patterns = ['*.js', '*.scss', '*.html']
self._ignore_directories = True
self.builder = None
self.rebuild_javascript = threading.Event()
self.rebuild_stylesheets = threading.Event()
def on_any_event(self, event):
if self.builder:
self.builder.cancel()
extension = event.src_path.rsplit('.', 1)[-1].lower()
if extension == 'js':
self.rebuild_javascript.set()
elif extension == 'scss':
self.rebuild_stylesheets.set()
self.builder = threading.Timer(3, self.rebuild)
self.builder.start()
def rebuild(self):
if self.rebuild_javascript.is_set():
self.rebuild_javascript.clear()
context.debug('Triggering javascript build')
bundle_javascript(context)
if self.rebuild_stylesheets.is_set():
self.rebuild_stylesheets.clear()
context.debug('Triggering stylesheet build')
bundle_stylesheets(context)
context.debug('Reloading browsers')
context.node_tool('browser-sync', 'reload', '--port=%s' % browsersync_port)
context.info('Watching sources')
observer = Observer()
paths = [
context.app.common_asset_source_path,
context.app.asset_source_path,
context.app.common_templates_path,
context.app.templates_path,
]
handler = RebuildHandler()
for path in paths:
observer.schedule(handler, path, recursive=True)
observer.setDaemon(True)
observer.start()
context.info('Starting browser sync')
browsersync_args = ['start', '--host=localhost', '--no-open',
'--logLevel', {0: 'silent', 1: 'info', 2: 'debug'}[context.verbosity],
'--port=%s' % browsersync_port, '--proxy=localhost:%s' % port,
'--ui-port=%s' % browsersync_ui_port]
browsersync = functools.partial(context.node_tool, 'browser-sync', *browsersync_args)
threading.Thread(target=browsersync, daemon=True).start()
context.info('Starting web server')
return start(context, port=port)
|
python
|
def serve(context: Context, port=8000, browsersync_port=3000, browsersync_ui_port=3030):
"""
Starts a development server with auto-building and live-reload
"""
try:
from watchdog.observers import Observer
except ImportError:
context.pip_command('install', 'watchdog>0.8,<0.9')
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
class RebuildHandler(PatternMatchingEventHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._patterns = ['*.js', '*.scss', '*.html']
self._ignore_directories = True
self.builder = None
self.rebuild_javascript = threading.Event()
self.rebuild_stylesheets = threading.Event()
def on_any_event(self, event):
if self.builder:
self.builder.cancel()
extension = event.src_path.rsplit('.', 1)[-1].lower()
if extension == 'js':
self.rebuild_javascript.set()
elif extension == 'scss':
self.rebuild_stylesheets.set()
self.builder = threading.Timer(3, self.rebuild)
self.builder.start()
def rebuild(self):
if self.rebuild_javascript.is_set():
self.rebuild_javascript.clear()
context.debug('Triggering javascript build')
bundle_javascript(context)
if self.rebuild_stylesheets.is_set():
self.rebuild_stylesheets.clear()
context.debug('Triggering stylesheet build')
bundle_stylesheets(context)
context.debug('Reloading browsers')
context.node_tool('browser-sync', 'reload', '--port=%s' % browsersync_port)
context.info('Watching sources')
observer = Observer()
paths = [
context.app.common_asset_source_path,
context.app.asset_source_path,
context.app.common_templates_path,
context.app.templates_path,
]
handler = RebuildHandler()
for path in paths:
observer.schedule(handler, path, recursive=True)
observer.setDaemon(True)
observer.start()
context.info('Starting browser sync')
browsersync_args = ['start', '--host=localhost', '--no-open',
'--logLevel', {0: 'silent', 1: 'info', 2: 'debug'}[context.verbosity],
'--port=%s' % browsersync_port, '--proxy=localhost:%s' % port,
'--ui-port=%s' % browsersync_ui_port]
browsersync = functools.partial(context.node_tool, 'browser-sync', *browsersync_args)
threading.Thread(target=browsersync, daemon=True).start()
context.info('Starting web server')
return start(context, port=port)
|
[
"def",
"serve",
"(",
"context",
":",
"Context",
",",
"port",
"=",
"8000",
",",
"browsersync_port",
"=",
"3000",
",",
"browsersync_ui_port",
"=",
"3030",
")",
":",
"try",
":",
"from",
"watchdog",
".",
"observers",
"import",
"Observer",
"except",
"ImportError",
":",
"context",
".",
"pip_command",
"(",
"'install'",
",",
"'watchdog>0.8,<0.9'",
")",
"from",
"watchdog",
".",
"observers",
"import",
"Observer",
"from",
"watchdog",
".",
"events",
"import",
"PatternMatchingEventHandler",
"class",
"RebuildHandler",
"(",
"PatternMatchingEventHandler",
")",
":",
"def",
"__init__",
"(",
"self",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"super",
"(",
")",
".",
"__init__",
"(",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
"self",
".",
"_patterns",
"=",
"[",
"'*.js'",
",",
"'*.scss'",
",",
"'*.html'",
"]",
"self",
".",
"_ignore_directories",
"=",
"True",
"self",
".",
"builder",
"=",
"None",
"self",
".",
"rebuild_javascript",
"=",
"threading",
".",
"Event",
"(",
")",
"self",
".",
"rebuild_stylesheets",
"=",
"threading",
".",
"Event",
"(",
")",
"def",
"on_any_event",
"(",
"self",
",",
"event",
")",
":",
"if",
"self",
".",
"builder",
":",
"self",
".",
"builder",
".",
"cancel",
"(",
")",
"extension",
"=",
"event",
".",
"src_path",
".",
"rsplit",
"(",
"'.'",
",",
"1",
")",
"[",
"-",
"1",
"]",
".",
"lower",
"(",
")",
"if",
"extension",
"==",
"'js'",
":",
"self",
".",
"rebuild_javascript",
".",
"set",
"(",
")",
"elif",
"extension",
"==",
"'scss'",
":",
"self",
".",
"rebuild_stylesheets",
".",
"set",
"(",
")",
"self",
".",
"builder",
"=",
"threading",
".",
"Timer",
"(",
"3",
",",
"self",
".",
"rebuild",
")",
"self",
".",
"builder",
".",
"start",
"(",
")",
"def",
"rebuild",
"(",
"self",
")",
":",
"if",
"self",
".",
"rebuild_javascript",
".",
"is_set",
"(",
")",
":",
"self",
".",
"rebuild_javascript",
".",
"clear",
"(",
")",
"context",
".",
"debug",
"(",
"'Triggering javascript build'",
")",
"bundle_javascript",
"(",
"context",
")",
"if",
"self",
".",
"rebuild_stylesheets",
".",
"is_set",
"(",
")",
":",
"self",
".",
"rebuild_stylesheets",
".",
"clear",
"(",
")",
"context",
".",
"debug",
"(",
"'Triggering stylesheet build'",
")",
"bundle_stylesheets",
"(",
"context",
")",
"context",
".",
"debug",
"(",
"'Reloading browsers'",
")",
"context",
".",
"node_tool",
"(",
"'browser-sync'",
",",
"'reload'",
",",
"'--port=%s'",
"%",
"browsersync_port",
")",
"context",
".",
"info",
"(",
"'Watching sources'",
")",
"observer",
"=",
"Observer",
"(",
")",
"paths",
"=",
"[",
"context",
".",
"app",
".",
"common_asset_source_path",
",",
"context",
".",
"app",
".",
"asset_source_path",
",",
"context",
".",
"app",
".",
"common_templates_path",
",",
"context",
".",
"app",
".",
"templates_path",
",",
"]",
"handler",
"=",
"RebuildHandler",
"(",
")",
"for",
"path",
"in",
"paths",
":",
"observer",
".",
"schedule",
"(",
"handler",
",",
"path",
",",
"recursive",
"=",
"True",
")",
"observer",
".",
"setDaemon",
"(",
"True",
")",
"observer",
".",
"start",
"(",
")",
"context",
".",
"info",
"(",
"'Starting browser sync'",
")",
"browsersync_args",
"=",
"[",
"'start'",
",",
"'--host=localhost'",
",",
"'--no-open'",
",",
"'--logLevel'",
",",
"{",
"0",
":",
"'silent'",
",",
"1",
":",
"'info'",
",",
"2",
":",
"'debug'",
"}",
"[",
"context",
".",
"verbosity",
"]",
",",
"'--port=%s'",
"%",
"browsersync_port",
",",
"'--proxy=localhost:%s'",
"%",
"port",
",",
"'--ui-port=%s'",
"%",
"browsersync_ui_port",
"]",
"browsersync",
"=",
"functools",
".",
"partial",
"(",
"context",
".",
"node_tool",
",",
"'browser-sync'",
",",
"*",
"browsersync_args",
")",
"threading",
".",
"Thread",
"(",
"target",
"=",
"browsersync",
",",
"daemon",
"=",
"True",
")",
".",
"start",
"(",
")",
"context",
".",
"info",
"(",
"'Starting web server'",
")",
"return",
"start",
"(",
"context",
",",
"port",
"=",
"port",
")"
] |
Starts a development server with auto-building and live-reload
|
[
"Starts",
"a",
"development",
"server",
"with",
"auto",
"-",
"building",
"and",
"live",
"-",
"reload"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/build_tasks/tasks.py#L34-L101
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/build_tasks/tasks.py
|
create_build_paths
|
def create_build_paths(context: Context):
"""
Creates directories needed for build outputs
"""
paths = [context.app.asset_build_path, context.app.screenshots_build_path, context.app.collected_assets_path]
for path in filter(None, paths):
os.makedirs(path, exist_ok=True)
|
python
|
def create_build_paths(context: Context):
"""
Creates directories needed for build outputs
"""
paths = [context.app.asset_build_path, context.app.screenshots_build_path, context.app.collected_assets_path]
for path in filter(None, paths):
os.makedirs(path, exist_ok=True)
|
[
"def",
"create_build_paths",
"(",
"context",
":",
"Context",
")",
":",
"paths",
"=",
"[",
"context",
".",
"app",
".",
"asset_build_path",
",",
"context",
".",
"app",
".",
"screenshots_build_path",
",",
"context",
".",
"app",
".",
"collected_assets_path",
"]",
"for",
"path",
"in",
"filter",
"(",
"None",
",",
"paths",
")",
":",
"os",
".",
"makedirs",
"(",
"path",
",",
"exist_ok",
"=",
"True",
")"
] |
Creates directories needed for build outputs
|
[
"Creates",
"directories",
"needed",
"for",
"build",
"outputs"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/build_tasks/tasks.py#L121-L127
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/build_tasks/tasks.py
|
python_dependencies
|
def python_dependencies(context: Context, common_path=None):
"""
Updates python dependencies
"""
context.pip_command('install', '-r', context.requirements_file)
if common_path:
context.pip_command('uninstall', '--yes', 'money-to-prisoners-common')
context.pip_command('install', '--force-reinstall', '-e', common_path)
context.shell('rm', '-rf', 'webpack.config.js')
|
python
|
def python_dependencies(context: Context, common_path=None):
"""
Updates python dependencies
"""
context.pip_command('install', '-r', context.requirements_file)
if common_path:
context.pip_command('uninstall', '--yes', 'money-to-prisoners-common')
context.pip_command('install', '--force-reinstall', '-e', common_path)
context.shell('rm', '-rf', 'webpack.config.js')
|
[
"def",
"python_dependencies",
"(",
"context",
":",
"Context",
",",
"common_path",
"=",
"None",
")",
":",
"context",
".",
"pip_command",
"(",
"'install'",
",",
"'-r'",
",",
"context",
".",
"requirements_file",
")",
"if",
"common_path",
":",
"context",
".",
"pip_command",
"(",
"'uninstall'",
",",
"'--yes'",
",",
"'money-to-prisoners-common'",
")",
"context",
".",
"pip_command",
"(",
"'install'",
",",
"'--force-reinstall'",
",",
"'-e'",
",",
"common_path",
")",
"context",
".",
"shell",
"(",
"'rm'",
",",
"'-rf'",
",",
"'webpack.config.js'",
")"
] |
Updates python dependencies
|
[
"Updates",
"python",
"dependencies"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/build_tasks/tasks.py#L131-L139
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/build_tasks/tasks.py
|
node_dependencies
|
def node_dependencies(context: Context):
"""
Updates node.js dependencies
"""
args = ['--loglevel', {0: 'silent', 1: 'warn', 2: 'info'}[context.verbosity]]
if not context.use_colour:
args.append('--color false')
args.append('install')
return context.shell('npm', *args)
|
python
|
def node_dependencies(context: Context):
"""
Updates node.js dependencies
"""
args = ['--loglevel', {0: 'silent', 1: 'warn', 2: 'info'}[context.verbosity]]
if not context.use_colour:
args.append('--color false')
args.append('install')
return context.shell('npm', *args)
|
[
"def",
"node_dependencies",
"(",
"context",
":",
"Context",
")",
":",
"args",
"=",
"[",
"'--loglevel'",
",",
"{",
"0",
":",
"'silent'",
",",
"1",
":",
"'warn'",
",",
"2",
":",
"'info'",
"}",
"[",
"context",
".",
"verbosity",
"]",
"]",
"if",
"not",
"context",
".",
"use_colour",
":",
"args",
".",
"append",
"(",
"'--color false'",
")",
"args",
".",
"append",
"(",
"'install'",
")",
"return",
"context",
".",
"shell",
"(",
"'npm'",
",",
"*",
"args",
")"
] |
Updates node.js dependencies
|
[
"Updates",
"node",
".",
"js",
"dependencies"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/build_tasks/tasks.py#L151-L159
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/build_tasks/tasks.py
|
local_docker
|
def local_docker(context: Context):
"""
Runs the app in a docker container; for local development only!
Once performed, `docker-compose up` can be used directly
"""
output = io.StringIO()
with contextlib.redirect_stdout(output):
context.shell('docker-machine', 'ip', 'default')
host_machine_ip = output.getvalue().strip() or socket.gethostbyname(socket.gethostname())
args = ()
if context.verbosity > 1:
args += ('--verbose',)
args += ('up', '--build', '--remove-orphans')
if not context.use_colour:
args += ('--no-color',)
context.shell('docker-compose', *args, environment={'HOST_MACHINE_IP': host_machine_ip})
|
python
|
def local_docker(context: Context):
"""
Runs the app in a docker container; for local development only!
Once performed, `docker-compose up` can be used directly
"""
output = io.StringIO()
with contextlib.redirect_stdout(output):
context.shell('docker-machine', 'ip', 'default')
host_machine_ip = output.getvalue().strip() or socket.gethostbyname(socket.gethostname())
args = ()
if context.verbosity > 1:
args += ('--verbose',)
args += ('up', '--build', '--remove-orphans')
if not context.use_colour:
args += ('--no-color',)
context.shell('docker-compose', *args, environment={'HOST_MACHINE_IP': host_machine_ip})
|
[
"def",
"local_docker",
"(",
"context",
":",
"Context",
")",
":",
"output",
"=",
"io",
".",
"StringIO",
"(",
")",
"with",
"contextlib",
".",
"redirect_stdout",
"(",
"output",
")",
":",
"context",
".",
"shell",
"(",
"'docker-machine'",
",",
"'ip'",
",",
"'default'",
")",
"host_machine_ip",
"=",
"output",
".",
"getvalue",
"(",
")",
".",
"strip",
"(",
")",
"or",
"socket",
".",
"gethostbyname",
"(",
"socket",
".",
"gethostname",
"(",
")",
")",
"args",
"=",
"(",
")",
"if",
"context",
".",
"verbosity",
">",
"1",
":",
"args",
"+=",
"(",
"'--verbose'",
",",
")",
"args",
"+=",
"(",
"'up'",
",",
"'--build'",
",",
"'--remove-orphans'",
")",
"if",
"not",
"context",
".",
"use_colour",
":",
"args",
"+=",
"(",
"'--no-color'",
",",
")",
"context",
".",
"shell",
"(",
"'docker-compose'",
",",
"*",
"args",
",",
"environment",
"=",
"{",
"'HOST_MACHINE_IP'",
":",
"host_machine_ip",
"}",
")"
] |
Runs the app in a docker container; for local development only!
Once performed, `docker-compose up` can be used directly
|
[
"Runs",
"the",
"app",
"in",
"a",
"docker",
"container",
";",
"for",
"local",
"development",
"only!",
"Once",
"performed",
"docker",
"-",
"compose",
"up",
"can",
"be",
"used",
"directly"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/build_tasks/tasks.py#L180-L195
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/build_tasks/tasks.py
|
bundle_javascript
|
def bundle_javascript(context: Context):
"""
Compiles javascript
"""
args = ['--bail']
if context.verbosity > 0:
args.append('--verbose')
if not context.use_colour:
args.append('--no-colors')
return context.node_tool('webpack', *args)
|
python
|
def bundle_javascript(context: Context):
"""
Compiles javascript
"""
args = ['--bail']
if context.verbosity > 0:
args.append('--verbose')
if not context.use_colour:
args.append('--no-colors')
return context.node_tool('webpack', *args)
|
[
"def",
"bundle_javascript",
"(",
"context",
":",
"Context",
")",
":",
"args",
"=",
"[",
"'--bail'",
"]",
"if",
"context",
".",
"verbosity",
">",
"0",
":",
"args",
".",
"append",
"(",
"'--verbose'",
")",
"if",
"not",
"context",
".",
"use_colour",
":",
"args",
".",
"append",
"(",
"'--no-colors'",
")",
"return",
"context",
".",
"node_tool",
"(",
"'webpack'",
",",
"*",
"args",
")"
] |
Compiles javascript
|
[
"Compiles",
"javascript"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/build_tasks/tasks.py#L207-L216
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/build_tasks/tasks.py
|
bundle_stylesheets
|
def bundle_stylesheets(context: Context):
"""
Compiles stylesheets
"""
args = [
'--output', context.app.scss_build_path,
'--output-style', 'compressed',
]
if context.verbosity == 0:
args.append('--quiet')
if not context.use_colour:
args.append('--no-color')
for path in context.app.scss_include_paths:
args.append('--include-path')
args.append(path)
return_code = 0
for source_file in context.app.scss_source_file_set.paths_for_shell(separator=None):
return_code = context.node_tool('node-sass', *args + [source_file]) or return_code
return return_code
|
python
|
def bundle_stylesheets(context: Context):
"""
Compiles stylesheets
"""
args = [
'--output', context.app.scss_build_path,
'--output-style', 'compressed',
]
if context.verbosity == 0:
args.append('--quiet')
if not context.use_colour:
args.append('--no-color')
for path in context.app.scss_include_paths:
args.append('--include-path')
args.append(path)
return_code = 0
for source_file in context.app.scss_source_file_set.paths_for_shell(separator=None):
return_code = context.node_tool('node-sass', *args + [source_file]) or return_code
return return_code
|
[
"def",
"bundle_stylesheets",
"(",
"context",
":",
"Context",
")",
":",
"args",
"=",
"[",
"'--output'",
",",
"context",
".",
"app",
".",
"scss_build_path",
",",
"'--output-style'",
",",
"'compressed'",
",",
"]",
"if",
"context",
".",
"verbosity",
"==",
"0",
":",
"args",
".",
"append",
"(",
"'--quiet'",
")",
"if",
"not",
"context",
".",
"use_colour",
":",
"args",
".",
"append",
"(",
"'--no-color'",
")",
"for",
"path",
"in",
"context",
".",
"app",
".",
"scss_include_paths",
":",
"args",
".",
"append",
"(",
"'--include-path'",
")",
"args",
".",
"append",
"(",
"path",
")",
"return_code",
"=",
"0",
"for",
"source_file",
"in",
"context",
".",
"app",
".",
"scss_source_file_set",
".",
"paths_for_shell",
"(",
"separator",
"=",
"None",
")",
":",
"return_code",
"=",
"context",
".",
"node_tool",
"(",
"'node-sass'",
",",
"*",
"args",
"+",
"[",
"source_file",
"]",
")",
"or",
"return_code",
"return",
"return_code"
] |
Compiles stylesheets
|
[
"Compiles",
"stylesheets"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/build_tasks/tasks.py#L220-L238
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/build_tasks/tasks.py
|
lint_javascript
|
def lint_javascript(context: Context):
"""
Tests javascript for code and style errors
"""
args = [
'--config', os.path.join(context.app.common_templates_path, 'mtp_common', 'build_tasks', 'eslintrc.json'),
'--format', 'stylish',
]
if context.verbosity == 0:
args.append('--quiet')
if not context.use_colour:
args.append('--no-color')
args.append(context.app.javascript_source_path)
return context.node_tool('eslint', *args)
|
python
|
def lint_javascript(context: Context):
"""
Tests javascript for code and style errors
"""
args = [
'--config', os.path.join(context.app.common_templates_path, 'mtp_common', 'build_tasks', 'eslintrc.json'),
'--format', 'stylish',
]
if context.verbosity == 0:
args.append('--quiet')
if not context.use_colour:
args.append('--no-color')
args.append(context.app.javascript_source_path)
return context.node_tool('eslint', *args)
|
[
"def",
"lint_javascript",
"(",
"context",
":",
"Context",
")",
":",
"args",
"=",
"[",
"'--config'",
",",
"os",
".",
"path",
".",
"join",
"(",
"context",
".",
"app",
".",
"common_templates_path",
",",
"'mtp_common'",
",",
"'build_tasks'",
",",
"'eslintrc.json'",
")",
",",
"'--format'",
",",
"'stylish'",
",",
"]",
"if",
"context",
".",
"verbosity",
"==",
"0",
":",
"args",
".",
"append",
"(",
"'--quiet'",
")",
"if",
"not",
"context",
".",
"use_colour",
":",
"args",
".",
"append",
"(",
"'--no-color'",
")",
"args",
".",
"append",
"(",
"context",
".",
"app",
".",
"javascript_source_path",
")",
"return",
"context",
".",
"node_tool",
"(",
"'eslint'",
",",
"*",
"args",
")"
] |
Tests javascript for code and style errors
|
[
"Tests",
"javascript",
"for",
"code",
"and",
"style",
"errors"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/build_tasks/tasks.py#L249-L262
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/build_tasks/tasks.py
|
lint_stylesheets
|
def lint_stylesheets(context: Context):
"""
Tests stylesheets for code and style errors
"""
args = [
'--config', os.path.join(context.app.common_templates_path, 'mtp_common', 'build_tasks', 'sass-lint.yml'),
'--format', 'stylish',
'--syntax', 'scss',
]
if context.verbosity > 1:
args.append('--verbose')
args.append(os.path.join(context.app.scss_source_path, '**', '*.scss'))
return context.node_tool('sass-lint', *args)
|
python
|
def lint_stylesheets(context: Context):
"""
Tests stylesheets for code and style errors
"""
args = [
'--config', os.path.join(context.app.common_templates_path, 'mtp_common', 'build_tasks', 'sass-lint.yml'),
'--format', 'stylish',
'--syntax', 'scss',
]
if context.verbosity > 1:
args.append('--verbose')
args.append(os.path.join(context.app.scss_source_path, '**', '*.scss'))
return context.node_tool('sass-lint', *args)
|
[
"def",
"lint_stylesheets",
"(",
"context",
":",
"Context",
")",
":",
"args",
"=",
"[",
"'--config'",
",",
"os",
".",
"path",
".",
"join",
"(",
"context",
".",
"app",
".",
"common_templates_path",
",",
"'mtp_common'",
",",
"'build_tasks'",
",",
"'sass-lint.yml'",
")",
",",
"'--format'",
",",
"'stylish'",
",",
"'--syntax'",
",",
"'scss'",
",",
"]",
"if",
"context",
".",
"verbosity",
">",
"1",
":",
"args",
".",
"append",
"(",
"'--verbose'",
")",
"args",
".",
"append",
"(",
"os",
".",
"path",
".",
"join",
"(",
"context",
".",
"app",
".",
"scss_source_path",
",",
"'**'",
",",
"'*.scss'",
")",
")",
"return",
"context",
".",
"node_tool",
"(",
"'sass-lint'",
",",
"*",
"args",
")"
] |
Tests stylesheets for code and style errors
|
[
"Tests",
"stylesheets",
"for",
"code",
"and",
"style",
"errors"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/build_tasks/tasks.py#L266-L278
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/build_tasks/tasks.py
|
govuk_template
|
def govuk_template(context: Context, version='0.23.0', replace_fonts=True):
"""
Installs GOV.UK template
"""
if FileSet(os.path.join(context.app.govuk_templates_path, 'base.html')):
# NB: check is only on main template and not the assets included
return
url = 'https://github.com/alphagov/govuk_template/releases' \
'/download/v{0}/django_govuk_template-{0}.tgz'.format(version)
try:
context.shell('curl --location %(silent)s --output govuk_template.tgz %(url)s' % {
'silent': '--silent' if context.verbosity == 0 else '',
'url': url,
})
context.shell('tar xzf govuk_template.tgz ./govuk_template')
rsync_flags = '-avz' if context.verbosity == 2 else '-az'
context.shell('rsync %s govuk_template/static/ %s/' % (rsync_flags, context.app.asset_build_path))
context.shell('rsync %s govuk_template/templates/ %s/' % (rsync_flags, context.app.templates_path))
finally:
context.shell('rm -rf govuk_template.tgz ./govuk_template')
if replace_fonts:
# govuk_template includes .eot font files for IE, but they load relative to current URL not the stylesheet
# this option removes these files so common's fonts-ie8.css override is used
context.shell('rm -rf %s/stylesheets/fonts-ie8.css'
' %s/stylesheets/fonts/' % (context.app.asset_build_path, context.app.asset_build_path))
|
python
|
def govuk_template(context: Context, version='0.23.0', replace_fonts=True):
"""
Installs GOV.UK template
"""
if FileSet(os.path.join(context.app.govuk_templates_path, 'base.html')):
# NB: check is only on main template and not the assets included
return
url = 'https://github.com/alphagov/govuk_template/releases' \
'/download/v{0}/django_govuk_template-{0}.tgz'.format(version)
try:
context.shell('curl --location %(silent)s --output govuk_template.tgz %(url)s' % {
'silent': '--silent' if context.verbosity == 0 else '',
'url': url,
})
context.shell('tar xzf govuk_template.tgz ./govuk_template')
rsync_flags = '-avz' if context.verbosity == 2 else '-az'
context.shell('rsync %s govuk_template/static/ %s/' % (rsync_flags, context.app.asset_build_path))
context.shell('rsync %s govuk_template/templates/ %s/' % (rsync_flags, context.app.templates_path))
finally:
context.shell('rm -rf govuk_template.tgz ./govuk_template')
if replace_fonts:
# govuk_template includes .eot font files for IE, but they load relative to current URL not the stylesheet
# this option removes these files so common's fonts-ie8.css override is used
context.shell('rm -rf %s/stylesheets/fonts-ie8.css'
' %s/stylesheets/fonts/' % (context.app.asset_build_path, context.app.asset_build_path))
|
[
"def",
"govuk_template",
"(",
"context",
":",
"Context",
",",
"version",
"=",
"'0.23.0'",
",",
"replace_fonts",
"=",
"True",
")",
":",
"if",
"FileSet",
"(",
"os",
".",
"path",
".",
"join",
"(",
"context",
".",
"app",
".",
"govuk_templates_path",
",",
"'base.html'",
")",
")",
":",
"# NB: check is only on main template and not the assets included",
"return",
"url",
"=",
"'https://github.com/alphagov/govuk_template/releases'",
"'/download/v{0}/django_govuk_template-{0}.tgz'",
".",
"format",
"(",
"version",
")",
"try",
":",
"context",
".",
"shell",
"(",
"'curl --location %(silent)s --output govuk_template.tgz %(url)s'",
"%",
"{",
"'silent'",
":",
"'--silent'",
"if",
"context",
".",
"verbosity",
"==",
"0",
"else",
"''",
",",
"'url'",
":",
"url",
",",
"}",
")",
"context",
".",
"shell",
"(",
"'tar xzf govuk_template.tgz ./govuk_template'",
")",
"rsync_flags",
"=",
"'-avz'",
"if",
"context",
".",
"verbosity",
"==",
"2",
"else",
"'-az'",
"context",
".",
"shell",
"(",
"'rsync %s govuk_template/static/ %s/'",
"%",
"(",
"rsync_flags",
",",
"context",
".",
"app",
".",
"asset_build_path",
")",
")",
"context",
".",
"shell",
"(",
"'rsync %s govuk_template/templates/ %s/'",
"%",
"(",
"rsync_flags",
",",
"context",
".",
"app",
".",
"templates_path",
")",
")",
"finally",
":",
"context",
".",
"shell",
"(",
"'rm -rf govuk_template.tgz ./govuk_template'",
")",
"if",
"replace_fonts",
":",
"# govuk_template includes .eot font files for IE, but they load relative to current URL not the stylesheet",
"# this option removes these files so common's fonts-ie8.css override is used",
"context",
".",
"shell",
"(",
"'rm -rf %s/stylesheets/fonts-ie8.css'",
"' %s/stylesheets/fonts/'",
"%",
"(",
"context",
".",
"app",
".",
"asset_build_path",
",",
"context",
".",
"app",
".",
"asset_build_path",
")",
")"
] |
Installs GOV.UK template
|
[
"Installs",
"GOV",
".",
"UK",
"template"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/build_tasks/tasks.py#L289-L313
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/build_tasks/tasks.py
|
additional_assets
|
def additional_assets(context: Context):
"""
Collects assets from GOV.UK frontend toolkit
"""
rsync_flags = '-avz' if context.verbosity == 2 else '-az'
for path in context.app.additional_asset_paths:
context.shell('rsync %s %s %s/' % (rsync_flags, path, context.app.asset_build_path))
|
python
|
def additional_assets(context: Context):
"""
Collects assets from GOV.UK frontend toolkit
"""
rsync_flags = '-avz' if context.verbosity == 2 else '-az'
for path in context.app.additional_asset_paths:
context.shell('rsync %s %s %s/' % (rsync_flags, path, context.app.asset_build_path))
|
[
"def",
"additional_assets",
"(",
"context",
":",
"Context",
")",
":",
"rsync_flags",
"=",
"'-avz'",
"if",
"context",
".",
"verbosity",
"==",
"2",
"else",
"'-az'",
"for",
"path",
"in",
"context",
".",
"app",
".",
"additional_asset_paths",
":",
"context",
".",
"shell",
"(",
"'rsync %s %s %s/'",
"%",
"(",
"rsync_flags",
",",
"path",
",",
"context",
".",
"app",
".",
"asset_build_path",
")",
")"
] |
Collects assets from GOV.UK frontend toolkit
|
[
"Collects",
"assets",
"from",
"GOV",
".",
"UK",
"frontend",
"toolkit"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/build_tasks/tasks.py#L317-L323
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/build_tasks/tasks.py
|
precompile_python_code
|
def precompile_python_code(context: Context):
"""
Pre-compiles python modules
"""
from compileall import compile_dir
kwargs = {}
if context.verbosity < 2:
kwargs['quiet'] = True
compile_dir(context.app.django_app_name, **kwargs)
|
python
|
def precompile_python_code(context: Context):
"""
Pre-compiles python modules
"""
from compileall import compile_dir
kwargs = {}
if context.verbosity < 2:
kwargs['quiet'] = True
compile_dir(context.app.django_app_name, **kwargs)
|
[
"def",
"precompile_python_code",
"(",
"context",
":",
"Context",
")",
":",
"from",
"compileall",
"import",
"compile_dir",
"kwargs",
"=",
"{",
"}",
"if",
"context",
".",
"verbosity",
"<",
"2",
":",
"kwargs",
"[",
"'quiet'",
"]",
"=",
"True",
"compile_dir",
"(",
"context",
".",
"app",
".",
"django_app_name",
",",
"*",
"*",
"kwargs",
")"
] |
Pre-compiles python modules
|
[
"Pre",
"-",
"compiles",
"python",
"modules"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/build_tasks/tasks.py#L344-L353
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/build_tasks/tasks.py
|
make_messages
|
def make_messages(context: Context, javascript=False, fuzzy=False):
"""
Collects text into translation source files
"""
kwargs = {
'all': True,
'keep_pot': True,
'no_wrap': True,
}
if fuzzy:
kwargs['allow_fuzzy'] = True
if javascript:
kwargs.update(domain='djangojs', ignore_patterns=['*.bundle.js'])
with in_dir(context.app.django_app_name):
return context.management_command('makemessages', **kwargs)
|
python
|
def make_messages(context: Context, javascript=False, fuzzy=False):
"""
Collects text into translation source files
"""
kwargs = {
'all': True,
'keep_pot': True,
'no_wrap': True,
}
if fuzzy:
kwargs['allow_fuzzy'] = True
if javascript:
kwargs.update(domain='djangojs', ignore_patterns=['*.bundle.js'])
with in_dir(context.app.django_app_name):
return context.management_command('makemessages', **kwargs)
|
[
"def",
"make_messages",
"(",
"context",
":",
"Context",
",",
"javascript",
"=",
"False",
",",
"fuzzy",
"=",
"False",
")",
":",
"kwargs",
"=",
"{",
"'all'",
":",
"True",
",",
"'keep_pot'",
":",
"True",
",",
"'no_wrap'",
":",
"True",
",",
"}",
"if",
"fuzzy",
":",
"kwargs",
"[",
"'allow_fuzzy'",
"]",
"=",
"True",
"if",
"javascript",
":",
"kwargs",
".",
"update",
"(",
"domain",
"=",
"'djangojs'",
",",
"ignore_patterns",
"=",
"[",
"'*.bundle.js'",
"]",
")",
"with",
"in_dir",
"(",
"context",
".",
"app",
".",
"django_app_name",
")",
":",
"return",
"context",
".",
"management_command",
"(",
"'makemessages'",
",",
"*",
"*",
"kwargs",
")"
] |
Collects text into translation source files
|
[
"Collects",
"text",
"into",
"translation",
"source",
"files"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/build_tasks/tasks.py#L357-L371
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/build_tasks/tasks.py
|
translations
|
def translations(context: Context, pull=False, push=False):
"""
Synchronises translations with transifex.com
"""
if not (pull or push):
raise TaskError('Specify whether to push or pull translations')
if pull:
context.shell('tx', 'pull')
make_messages(context, javascript=False)
make_messages(context, javascript=True)
if push:
context.shell('tx', 'push', '--source', '--no-interactive')
|
python
|
def translations(context: Context, pull=False, push=False):
"""
Synchronises translations with transifex.com
"""
if not (pull or push):
raise TaskError('Specify whether to push or pull translations')
if pull:
context.shell('tx', 'pull')
make_messages(context, javascript=False)
make_messages(context, javascript=True)
if push:
context.shell('tx', 'push', '--source', '--no-interactive')
|
[
"def",
"translations",
"(",
"context",
":",
"Context",
",",
"pull",
"=",
"False",
",",
"push",
"=",
"False",
")",
":",
"if",
"not",
"(",
"pull",
"or",
"push",
")",
":",
"raise",
"TaskError",
"(",
"'Specify whether to push or pull translations'",
")",
"if",
"pull",
":",
"context",
".",
"shell",
"(",
"'tx'",
",",
"'pull'",
")",
"make_messages",
"(",
"context",
",",
"javascript",
"=",
"False",
")",
"make_messages",
"(",
"context",
",",
"javascript",
"=",
"True",
")",
"if",
"push",
":",
"context",
".",
"shell",
"(",
"'tx'",
",",
"'push'",
",",
"'--source'",
",",
"'--no-interactive'",
")"
] |
Synchronises translations with transifex.com
|
[
"Synchronises",
"translations",
"with",
"transifex",
".",
"com"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/build_tasks/tasks.py#L384-L395
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/build_tasks/tasks.py
|
clean
|
def clean(context: Context, delete_dependencies: bool = False):
"""
Deletes build outputs
"""
paths = [context.app.asset_build_path, context.app.collected_assets_path, context.app.govuk_templates_path,
'docker-compose.yml', 'package.json', 'package-lock.json', 'webpack.config.js']
context.shell('rm -rf %s' % paths_for_shell(paths))
context.shell('find %s -name "*.pyc" -or -name __pycache__ -delete' % context.app.django_app_name)
if delete_dependencies:
context.info('Cleaning app %s dependencies' % context.app.name)
paths = ['node_modules', 'venv']
context.shell('rm -rf %s' % paths_for_shell(paths))
|
python
|
def clean(context: Context, delete_dependencies: bool = False):
"""
Deletes build outputs
"""
paths = [context.app.asset_build_path, context.app.collected_assets_path, context.app.govuk_templates_path,
'docker-compose.yml', 'package.json', 'package-lock.json', 'webpack.config.js']
context.shell('rm -rf %s' % paths_for_shell(paths))
context.shell('find %s -name "*.pyc" -or -name __pycache__ -delete' % context.app.django_app_name)
if delete_dependencies:
context.info('Cleaning app %s dependencies' % context.app.name)
paths = ['node_modules', 'venv']
context.shell('rm -rf %s' % paths_for_shell(paths))
|
[
"def",
"clean",
"(",
"context",
":",
"Context",
",",
"delete_dependencies",
":",
"bool",
"=",
"False",
")",
":",
"paths",
"=",
"[",
"context",
".",
"app",
".",
"asset_build_path",
",",
"context",
".",
"app",
".",
"collected_assets_path",
",",
"context",
".",
"app",
".",
"govuk_templates_path",
",",
"'docker-compose.yml'",
",",
"'package.json'",
",",
"'package-lock.json'",
",",
"'webpack.config.js'",
"]",
"context",
".",
"shell",
"(",
"'rm -rf %s'",
"%",
"paths_for_shell",
"(",
"paths",
")",
")",
"context",
".",
"shell",
"(",
"'find %s -name \"*.pyc\" -or -name __pycache__ -delete'",
"%",
"context",
".",
"app",
".",
"django_app_name",
")",
"if",
"delete_dependencies",
":",
"context",
".",
"info",
"(",
"'Cleaning app %s dependencies'",
"%",
"context",
".",
"app",
".",
"name",
")",
"paths",
"=",
"[",
"'node_modules'",
",",
"'venv'",
"]",
"context",
".",
"shell",
"(",
"'rm -rf %s'",
"%",
"paths_for_shell",
"(",
"paths",
")",
")"
] |
Deletes build outputs
|
[
"Deletes",
"build",
"outputs"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/build_tasks/tasks.py#L399-L411
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/auth/csrf.py
|
csrf_failure
|
def csrf_failure(request, reason=''):
"""
CSRF-failure view which converts the failed POST request into a GET
and calls the original view with a sensible error message presented
to the user.
:param request: the HttpRequest
:param reason: non-localised failure description
"""
if _csrf_failed_view.no_moj_csrf:
from django.views.csrf import csrf_failure
return csrf_failure(request, reason=reason)
# present a sensible error message to users
if reason == REASON_NO_CSRF_COOKIE:
reason = _('Please try again.') + ' ' + \
_('Make sure you haven’t disabled cookies.')
elif reason == REASON_NO_REFERER:
reason = _('Please try again.') + ' ' + \
_('Make sure you are using a modern web browser '
'such as Firefox or Google Chrome.')
else:
reason = _('Your browser failed a security check.') + ' ' + \
_('Please try again.')
messages.error(request, reason)
# convert into GET request and show view again
request.method = 'GET'
request.POST = QueryDict()
# call the original view but set response status to forbidden
response = _csrf_failed_view.callback(request, *_csrf_failed_view.args, **_csrf_failed_view.kwargs)
if response.status_code == 200:
response.status_code = 403
return response
|
python
|
def csrf_failure(request, reason=''):
"""
CSRF-failure view which converts the failed POST request into a GET
and calls the original view with a sensible error message presented
to the user.
:param request: the HttpRequest
:param reason: non-localised failure description
"""
if _csrf_failed_view.no_moj_csrf:
from django.views.csrf import csrf_failure
return csrf_failure(request, reason=reason)
# present a sensible error message to users
if reason == REASON_NO_CSRF_COOKIE:
reason = _('Please try again.') + ' ' + \
_('Make sure you haven’t disabled cookies.')
elif reason == REASON_NO_REFERER:
reason = _('Please try again.') + ' ' + \
_('Make sure you are using a modern web browser '
'such as Firefox or Google Chrome.')
else:
reason = _('Your browser failed a security check.') + ' ' + \
_('Please try again.')
messages.error(request, reason)
# convert into GET request and show view again
request.method = 'GET'
request.POST = QueryDict()
# call the original view but set response status to forbidden
response = _csrf_failed_view.callback(request, *_csrf_failed_view.args, **_csrf_failed_view.kwargs)
if response.status_code == 200:
response.status_code = 403
return response
|
[
"def",
"csrf_failure",
"(",
"request",
",",
"reason",
"=",
"''",
")",
":",
"if",
"_csrf_failed_view",
".",
"no_moj_csrf",
":",
"from",
"django",
".",
"views",
".",
"csrf",
"import",
"csrf_failure",
"return",
"csrf_failure",
"(",
"request",
",",
"reason",
"=",
"reason",
")",
"# present a sensible error message to users",
"if",
"reason",
"==",
"REASON_NO_CSRF_COOKIE",
":",
"reason",
"=",
"_",
"(",
"'Please try again.'",
")",
"+",
"' '",
"+",
"_",
"(",
"'Make sure you haven’t disabled cookies.')",
"",
"elif",
"reason",
"==",
"REASON_NO_REFERER",
":",
"reason",
"=",
"_",
"(",
"'Please try again.'",
")",
"+",
"' '",
"+",
"_",
"(",
"'Make sure you are using a modern web browser '",
"'such as Firefox or Google Chrome.'",
")",
"else",
":",
"reason",
"=",
"_",
"(",
"'Your browser failed a security check.'",
")",
"+",
"' '",
"+",
"_",
"(",
"'Please try again.'",
")",
"messages",
".",
"error",
"(",
"request",
",",
"reason",
")",
"# convert into GET request and show view again",
"request",
".",
"method",
"=",
"'GET'",
"request",
".",
"POST",
"=",
"QueryDict",
"(",
")",
"# call the original view but set response status to forbidden",
"response",
"=",
"_csrf_failed_view",
".",
"callback",
"(",
"request",
",",
"*",
"_csrf_failed_view",
".",
"args",
",",
"*",
"*",
"_csrf_failed_view",
".",
"kwargs",
")",
"if",
"response",
".",
"status_code",
"==",
"200",
":",
"response",
".",
"status_code",
"=",
"403",
"return",
"response"
] |
CSRF-failure view which converts the failed POST request into a GET
and calls the original view with a sensible error message presented
to the user.
:param request: the HttpRequest
:param reason: non-localised failure description
|
[
"CSRF",
"-",
"failure",
"view",
"which",
"converts",
"the",
"failed",
"POST",
"request",
"into",
"a",
"GET",
"and",
"calls",
"the",
"original",
"view",
"with",
"a",
"sensible",
"error",
"message",
"presented",
"to",
"the",
"user",
".",
":",
"param",
"request",
":",
"the",
"HttpRequest",
":",
"param",
"reason",
":",
"non",
"-",
"localised",
"failure",
"description"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/auth/csrf.py#L12-L46
|
alejoe91/MEAutility
|
MEAutility/core.py
|
get_positions
|
def get_positions(elinfo, center=True):
'''Computes the positions of the elctrodes based on the elinfo
Parameters
----------
elinfo: dict
Contains electrode information from yaml file (dim, pitch, sortlist, plane, pos)
Returns
-------
positions: np.array
3d points with the centers of the electrodes
'''
electrode_pos = False
# method 1: positions in elinfo
if 'pos' in elinfo.keys():
pos = np.array(elinfo['pos'])
nelec = pos.shape[0]
if len(pos.shape) == 1:
if len(pos) == 2:
pos2d = np.array([pos])
if 'plane' not in elinfo.keys():
# print("'plane' field with 2D dimensions assumed to be 'yz")
plane = 'yz'
else:
plane = elinfo['plane']
if 'offset' not in elinfo.keys():
offset = 0
else:
offset = elinfo['offset']
pos = add_3dim(pos2d, plane, offset)
elif len(pos) == 3:
pos = np.array([pos])
elif len(pos) != 3:
raise AttributeError('pos attribute should be one or a list of 2D or 3D points')
elif len(pos.shape) == 2:
if pos.shape[1] == 2:
pos2d = pos
if 'plane' not in elinfo.keys():
# print("'plane' field with 2D dimensions assumed to be 'yz")
plane = 'yz'
else:
plane = elinfo['plane']
if 'offset' not in elinfo.keys():
offset = 0
else:
offset = elinfo['offset']
pos = add_3dim(pos2d, plane, offset)
elif pos.shape[1] != 3:
raise AttributeError('pos attribute should be a list of 2D or 3D points')
electrode_pos = True
# method 2: dim, pithch, stagger
if 'dim' in elinfo.keys():
dim = elinfo['dim']
if dim == 1:
if 'plane' not in elinfo.keys():
# print("'plane' field with 2D dimensions assumed to be 'yz")
plane = 'yz'
else:
plane = elinfo['plane']
if 'offset' not in elinfo.keys():
offset = 0
else:
offset = elinfo['offset']
pos2d = np.array([[0, 0]])
pos = add_3dim(pos2d, plane, offset)
else:
if 'pitch' not in elinfo.keys():
raise AttributeError("When 'dim' is used, also 'pitch' should be specified.")
else:
pitch = elinfo['pitch']
if isinstance(dim, (int, np.integer)):
dim = [dim, dim]
if isinstance(pitch, (int, np.integer)) or isinstance(pitch, (float, np.float)):
pitch = [pitch, pitch]
if len(dim) == 2:
d1 = np.array([])
d2 = np.array([])
if 'stagger' in elinfo.keys():
stagger = elinfo['stagger']
else:
stagger = None
for d_i in np.arange(dim[1]):
if stagger is not None:
if isinstance(stagger, (int, np.integer)) or isinstance(stagger, (float, np.float)):
if np.mod(d_i, 2):
d1new = np.arange(dim[0]) * pitch[0] + stagger
else:
d1new = np.arange(dim[0]) * pitch[0]
elif len(stagger) == len(dim):
d1new = np.arange(dim[0]) * pitch[0] + stagger[d_i]
else:
d1new = np.arange(dim[0]) * pitch[0]
else:
d1new = np.arange(dim[0]) * pitch[0]
d1 = np.concatenate((d1, d1new))
d2 = np.concatenate((d2, dim[0] * [pitch[1] * d_i]))
pos2d = np.vstack((d2, d1)).T
if 'plane' not in elinfo.keys():
# print("'plane' field with 2D dimensions assumed to be 'yz")
plane = 'yz'
else:
plane = elinfo['plane']
if 'offset' not in elinfo.keys():
offset = 0
else:
offset = elinfo['offset']
pos2d = np.concatenate((np.reshape(d2.T, (d1.size, 1)),
np.reshape(d1.T, (d2.size, 1))), axis=1)
pos = add_3dim(pos2d, plane, offset)
elif len(dim) >= 3:
d1 = np.array([])
d2 = np.array([])
if 'stagger' in elinfo.keys():
stagger = elinfo['stagger']
else:
stagger = None
for d_i, d in enumerate(dim):
if stagger is not None:
if isinstance(stagger, (int, np.integer)) or isinstance(stagger, (float, np.float)):
if np.mod(d_i, 2):
d1new = np.arange(d) * pitch[0] + stagger
else:
d1new = np.arange(d) * pitch[0]
elif len(stagger) == len(dim):
d1new = np.arange(d) * pitch[0] + stagger[d_i]
else:
d1new = np.arange(d) * pitch[0]
else:
d1new = np.arange(d) * pitch[0]
d1 = np.concatenate((d1, d1new))
d2 = np.concatenate((d2, d * [pitch[1] * d_i]))
pos2d = np.vstack((d2, d1)).T
if 'plane' not in elinfo.keys():
# print("'plane' field with 2D dimensions assumed to be 'yz")
plane = 'yz'
else:
plane = elinfo['plane']
if 'offset' not in elinfo.keys():
offset = 0
else:
offset = elinfo['offset']
pos = add_3dim(pos2d, plane, offset)
electrode_pos = True
if electrode_pos and center:
centered_pos = center_mea(pos)
# resort electrodes in case
centered_pos_sorted = copy.deepcopy(centered_pos)
if 'sortlist' in elinfo.keys() and elinfo['sortlist'] is not None:
sortlist = elinfo['sortlist']
for i, si in enumerate(sortlist):
centered_pos_sorted[si] = centered_pos[i]
else:
centered_pos_sorted = centered_pos
return centered_pos_sorted
elif electrode_pos and not center:
# resort electrodes in case
pos_sorted = copy.deepcopy(pos)
if 'sortlist' in elinfo.keys() and elinfo['sortlist'] is not None:
sortlist = elinfo['sortlist']
for i, si in enumerate(sortlist):
pos_sorted[si] = pos[i]
else:
pos_sorted = pos
return pos_sorted
else:
print("Define either a list of positions 'pos' or 'dim' and 'pitch'")
return None
|
python
|
def get_positions(elinfo, center=True):
'''Computes the positions of the elctrodes based on the elinfo
Parameters
----------
elinfo: dict
Contains electrode information from yaml file (dim, pitch, sortlist, plane, pos)
Returns
-------
positions: np.array
3d points with the centers of the electrodes
'''
electrode_pos = False
# method 1: positions in elinfo
if 'pos' in elinfo.keys():
pos = np.array(elinfo['pos'])
nelec = pos.shape[0]
if len(pos.shape) == 1:
if len(pos) == 2:
pos2d = np.array([pos])
if 'plane' not in elinfo.keys():
# print("'plane' field with 2D dimensions assumed to be 'yz")
plane = 'yz'
else:
plane = elinfo['plane']
if 'offset' not in elinfo.keys():
offset = 0
else:
offset = elinfo['offset']
pos = add_3dim(pos2d, plane, offset)
elif len(pos) == 3:
pos = np.array([pos])
elif len(pos) != 3:
raise AttributeError('pos attribute should be one or a list of 2D or 3D points')
elif len(pos.shape) == 2:
if pos.shape[1] == 2:
pos2d = pos
if 'plane' not in elinfo.keys():
# print("'plane' field with 2D dimensions assumed to be 'yz")
plane = 'yz'
else:
plane = elinfo['plane']
if 'offset' not in elinfo.keys():
offset = 0
else:
offset = elinfo['offset']
pos = add_3dim(pos2d, plane, offset)
elif pos.shape[1] != 3:
raise AttributeError('pos attribute should be a list of 2D or 3D points')
electrode_pos = True
# method 2: dim, pithch, stagger
if 'dim' in elinfo.keys():
dim = elinfo['dim']
if dim == 1:
if 'plane' not in elinfo.keys():
# print("'plane' field with 2D dimensions assumed to be 'yz")
plane = 'yz'
else:
plane = elinfo['plane']
if 'offset' not in elinfo.keys():
offset = 0
else:
offset = elinfo['offset']
pos2d = np.array([[0, 0]])
pos = add_3dim(pos2d, plane, offset)
else:
if 'pitch' not in elinfo.keys():
raise AttributeError("When 'dim' is used, also 'pitch' should be specified.")
else:
pitch = elinfo['pitch']
if isinstance(dim, (int, np.integer)):
dim = [dim, dim]
if isinstance(pitch, (int, np.integer)) or isinstance(pitch, (float, np.float)):
pitch = [pitch, pitch]
if len(dim) == 2:
d1 = np.array([])
d2 = np.array([])
if 'stagger' in elinfo.keys():
stagger = elinfo['stagger']
else:
stagger = None
for d_i in np.arange(dim[1]):
if stagger is not None:
if isinstance(stagger, (int, np.integer)) or isinstance(stagger, (float, np.float)):
if np.mod(d_i, 2):
d1new = np.arange(dim[0]) * pitch[0] + stagger
else:
d1new = np.arange(dim[0]) * pitch[0]
elif len(stagger) == len(dim):
d1new = np.arange(dim[0]) * pitch[0] + stagger[d_i]
else:
d1new = np.arange(dim[0]) * pitch[0]
else:
d1new = np.arange(dim[0]) * pitch[0]
d1 = np.concatenate((d1, d1new))
d2 = np.concatenate((d2, dim[0] * [pitch[1] * d_i]))
pos2d = np.vstack((d2, d1)).T
if 'plane' not in elinfo.keys():
# print("'plane' field with 2D dimensions assumed to be 'yz")
plane = 'yz'
else:
plane = elinfo['plane']
if 'offset' not in elinfo.keys():
offset = 0
else:
offset = elinfo['offset']
pos2d = np.concatenate((np.reshape(d2.T, (d1.size, 1)),
np.reshape(d1.T, (d2.size, 1))), axis=1)
pos = add_3dim(pos2d, plane, offset)
elif len(dim) >= 3:
d1 = np.array([])
d2 = np.array([])
if 'stagger' in elinfo.keys():
stagger = elinfo['stagger']
else:
stagger = None
for d_i, d in enumerate(dim):
if stagger is not None:
if isinstance(stagger, (int, np.integer)) or isinstance(stagger, (float, np.float)):
if np.mod(d_i, 2):
d1new = np.arange(d) * pitch[0] + stagger
else:
d1new = np.arange(d) * pitch[0]
elif len(stagger) == len(dim):
d1new = np.arange(d) * pitch[0] + stagger[d_i]
else:
d1new = np.arange(d) * pitch[0]
else:
d1new = np.arange(d) * pitch[0]
d1 = np.concatenate((d1, d1new))
d2 = np.concatenate((d2, d * [pitch[1] * d_i]))
pos2d = np.vstack((d2, d1)).T
if 'plane' not in elinfo.keys():
# print("'plane' field with 2D dimensions assumed to be 'yz")
plane = 'yz'
else:
plane = elinfo['plane']
if 'offset' not in elinfo.keys():
offset = 0
else:
offset = elinfo['offset']
pos = add_3dim(pos2d, plane, offset)
electrode_pos = True
if electrode_pos and center:
centered_pos = center_mea(pos)
# resort electrodes in case
centered_pos_sorted = copy.deepcopy(centered_pos)
if 'sortlist' in elinfo.keys() and elinfo['sortlist'] is not None:
sortlist = elinfo['sortlist']
for i, si in enumerate(sortlist):
centered_pos_sorted[si] = centered_pos[i]
else:
centered_pos_sorted = centered_pos
return centered_pos_sorted
elif electrode_pos and not center:
# resort electrodes in case
pos_sorted = copy.deepcopy(pos)
if 'sortlist' in elinfo.keys() and elinfo['sortlist'] is not None:
sortlist = elinfo['sortlist']
for i, si in enumerate(sortlist):
pos_sorted[si] = pos[i]
else:
pos_sorted = pos
return pos_sorted
else:
print("Define either a list of positions 'pos' or 'dim' and 'pitch'")
return None
|
[
"def",
"get_positions",
"(",
"elinfo",
",",
"center",
"=",
"True",
")",
":",
"electrode_pos",
"=",
"False",
"# method 1: positions in elinfo",
"if",
"'pos'",
"in",
"elinfo",
".",
"keys",
"(",
")",
":",
"pos",
"=",
"np",
".",
"array",
"(",
"elinfo",
"[",
"'pos'",
"]",
")",
"nelec",
"=",
"pos",
".",
"shape",
"[",
"0",
"]",
"if",
"len",
"(",
"pos",
".",
"shape",
")",
"==",
"1",
":",
"if",
"len",
"(",
"pos",
")",
"==",
"2",
":",
"pos2d",
"=",
"np",
".",
"array",
"(",
"[",
"pos",
"]",
")",
"if",
"'plane'",
"not",
"in",
"elinfo",
".",
"keys",
"(",
")",
":",
"# print(\"'plane' field with 2D dimensions assumed to be 'yz\")",
"plane",
"=",
"'yz'",
"else",
":",
"plane",
"=",
"elinfo",
"[",
"'plane'",
"]",
"if",
"'offset'",
"not",
"in",
"elinfo",
".",
"keys",
"(",
")",
":",
"offset",
"=",
"0",
"else",
":",
"offset",
"=",
"elinfo",
"[",
"'offset'",
"]",
"pos",
"=",
"add_3dim",
"(",
"pos2d",
",",
"plane",
",",
"offset",
")",
"elif",
"len",
"(",
"pos",
")",
"==",
"3",
":",
"pos",
"=",
"np",
".",
"array",
"(",
"[",
"pos",
"]",
")",
"elif",
"len",
"(",
"pos",
")",
"!=",
"3",
":",
"raise",
"AttributeError",
"(",
"'pos attribute should be one or a list of 2D or 3D points'",
")",
"elif",
"len",
"(",
"pos",
".",
"shape",
")",
"==",
"2",
":",
"if",
"pos",
".",
"shape",
"[",
"1",
"]",
"==",
"2",
":",
"pos2d",
"=",
"pos",
"if",
"'plane'",
"not",
"in",
"elinfo",
".",
"keys",
"(",
")",
":",
"# print(\"'plane' field with 2D dimensions assumed to be 'yz\")",
"plane",
"=",
"'yz'",
"else",
":",
"plane",
"=",
"elinfo",
"[",
"'plane'",
"]",
"if",
"'offset'",
"not",
"in",
"elinfo",
".",
"keys",
"(",
")",
":",
"offset",
"=",
"0",
"else",
":",
"offset",
"=",
"elinfo",
"[",
"'offset'",
"]",
"pos",
"=",
"add_3dim",
"(",
"pos2d",
",",
"plane",
",",
"offset",
")",
"elif",
"pos",
".",
"shape",
"[",
"1",
"]",
"!=",
"3",
":",
"raise",
"AttributeError",
"(",
"'pos attribute should be a list of 2D or 3D points'",
")",
"electrode_pos",
"=",
"True",
"# method 2: dim, pithch, stagger",
"if",
"'dim'",
"in",
"elinfo",
".",
"keys",
"(",
")",
":",
"dim",
"=",
"elinfo",
"[",
"'dim'",
"]",
"if",
"dim",
"==",
"1",
":",
"if",
"'plane'",
"not",
"in",
"elinfo",
".",
"keys",
"(",
")",
":",
"# print(\"'plane' field with 2D dimensions assumed to be 'yz\")",
"plane",
"=",
"'yz'",
"else",
":",
"plane",
"=",
"elinfo",
"[",
"'plane'",
"]",
"if",
"'offset'",
"not",
"in",
"elinfo",
".",
"keys",
"(",
")",
":",
"offset",
"=",
"0",
"else",
":",
"offset",
"=",
"elinfo",
"[",
"'offset'",
"]",
"pos2d",
"=",
"np",
".",
"array",
"(",
"[",
"[",
"0",
",",
"0",
"]",
"]",
")",
"pos",
"=",
"add_3dim",
"(",
"pos2d",
",",
"plane",
",",
"offset",
")",
"else",
":",
"if",
"'pitch'",
"not",
"in",
"elinfo",
".",
"keys",
"(",
")",
":",
"raise",
"AttributeError",
"(",
"\"When 'dim' is used, also 'pitch' should be specified.\"",
")",
"else",
":",
"pitch",
"=",
"elinfo",
"[",
"'pitch'",
"]",
"if",
"isinstance",
"(",
"dim",
",",
"(",
"int",
",",
"np",
".",
"integer",
")",
")",
":",
"dim",
"=",
"[",
"dim",
",",
"dim",
"]",
"if",
"isinstance",
"(",
"pitch",
",",
"(",
"int",
",",
"np",
".",
"integer",
")",
")",
"or",
"isinstance",
"(",
"pitch",
",",
"(",
"float",
",",
"np",
".",
"float",
")",
")",
":",
"pitch",
"=",
"[",
"pitch",
",",
"pitch",
"]",
"if",
"len",
"(",
"dim",
")",
"==",
"2",
":",
"d1",
"=",
"np",
".",
"array",
"(",
"[",
"]",
")",
"d2",
"=",
"np",
".",
"array",
"(",
"[",
"]",
")",
"if",
"'stagger'",
"in",
"elinfo",
".",
"keys",
"(",
")",
":",
"stagger",
"=",
"elinfo",
"[",
"'stagger'",
"]",
"else",
":",
"stagger",
"=",
"None",
"for",
"d_i",
"in",
"np",
".",
"arange",
"(",
"dim",
"[",
"1",
"]",
")",
":",
"if",
"stagger",
"is",
"not",
"None",
":",
"if",
"isinstance",
"(",
"stagger",
",",
"(",
"int",
",",
"np",
".",
"integer",
")",
")",
"or",
"isinstance",
"(",
"stagger",
",",
"(",
"float",
",",
"np",
".",
"float",
")",
")",
":",
"if",
"np",
".",
"mod",
"(",
"d_i",
",",
"2",
")",
":",
"d1new",
"=",
"np",
".",
"arange",
"(",
"dim",
"[",
"0",
"]",
")",
"*",
"pitch",
"[",
"0",
"]",
"+",
"stagger",
"else",
":",
"d1new",
"=",
"np",
".",
"arange",
"(",
"dim",
"[",
"0",
"]",
")",
"*",
"pitch",
"[",
"0",
"]",
"elif",
"len",
"(",
"stagger",
")",
"==",
"len",
"(",
"dim",
")",
":",
"d1new",
"=",
"np",
".",
"arange",
"(",
"dim",
"[",
"0",
"]",
")",
"*",
"pitch",
"[",
"0",
"]",
"+",
"stagger",
"[",
"d_i",
"]",
"else",
":",
"d1new",
"=",
"np",
".",
"arange",
"(",
"dim",
"[",
"0",
"]",
")",
"*",
"pitch",
"[",
"0",
"]",
"else",
":",
"d1new",
"=",
"np",
".",
"arange",
"(",
"dim",
"[",
"0",
"]",
")",
"*",
"pitch",
"[",
"0",
"]",
"d1",
"=",
"np",
".",
"concatenate",
"(",
"(",
"d1",
",",
"d1new",
")",
")",
"d2",
"=",
"np",
".",
"concatenate",
"(",
"(",
"d2",
",",
"dim",
"[",
"0",
"]",
"*",
"[",
"pitch",
"[",
"1",
"]",
"*",
"d_i",
"]",
")",
")",
"pos2d",
"=",
"np",
".",
"vstack",
"(",
"(",
"d2",
",",
"d1",
")",
")",
".",
"T",
"if",
"'plane'",
"not",
"in",
"elinfo",
".",
"keys",
"(",
")",
":",
"# print(\"'plane' field with 2D dimensions assumed to be 'yz\")",
"plane",
"=",
"'yz'",
"else",
":",
"plane",
"=",
"elinfo",
"[",
"'plane'",
"]",
"if",
"'offset'",
"not",
"in",
"elinfo",
".",
"keys",
"(",
")",
":",
"offset",
"=",
"0",
"else",
":",
"offset",
"=",
"elinfo",
"[",
"'offset'",
"]",
"pos2d",
"=",
"np",
".",
"concatenate",
"(",
"(",
"np",
".",
"reshape",
"(",
"d2",
".",
"T",
",",
"(",
"d1",
".",
"size",
",",
"1",
")",
")",
",",
"np",
".",
"reshape",
"(",
"d1",
".",
"T",
",",
"(",
"d2",
".",
"size",
",",
"1",
")",
")",
")",
",",
"axis",
"=",
"1",
")",
"pos",
"=",
"add_3dim",
"(",
"pos2d",
",",
"plane",
",",
"offset",
")",
"elif",
"len",
"(",
"dim",
")",
">=",
"3",
":",
"d1",
"=",
"np",
".",
"array",
"(",
"[",
"]",
")",
"d2",
"=",
"np",
".",
"array",
"(",
"[",
"]",
")",
"if",
"'stagger'",
"in",
"elinfo",
".",
"keys",
"(",
")",
":",
"stagger",
"=",
"elinfo",
"[",
"'stagger'",
"]",
"else",
":",
"stagger",
"=",
"None",
"for",
"d_i",
",",
"d",
"in",
"enumerate",
"(",
"dim",
")",
":",
"if",
"stagger",
"is",
"not",
"None",
":",
"if",
"isinstance",
"(",
"stagger",
",",
"(",
"int",
",",
"np",
".",
"integer",
")",
")",
"or",
"isinstance",
"(",
"stagger",
",",
"(",
"float",
",",
"np",
".",
"float",
")",
")",
":",
"if",
"np",
".",
"mod",
"(",
"d_i",
",",
"2",
")",
":",
"d1new",
"=",
"np",
".",
"arange",
"(",
"d",
")",
"*",
"pitch",
"[",
"0",
"]",
"+",
"stagger",
"else",
":",
"d1new",
"=",
"np",
".",
"arange",
"(",
"d",
")",
"*",
"pitch",
"[",
"0",
"]",
"elif",
"len",
"(",
"stagger",
")",
"==",
"len",
"(",
"dim",
")",
":",
"d1new",
"=",
"np",
".",
"arange",
"(",
"d",
")",
"*",
"pitch",
"[",
"0",
"]",
"+",
"stagger",
"[",
"d_i",
"]",
"else",
":",
"d1new",
"=",
"np",
".",
"arange",
"(",
"d",
")",
"*",
"pitch",
"[",
"0",
"]",
"else",
":",
"d1new",
"=",
"np",
".",
"arange",
"(",
"d",
")",
"*",
"pitch",
"[",
"0",
"]",
"d1",
"=",
"np",
".",
"concatenate",
"(",
"(",
"d1",
",",
"d1new",
")",
")",
"d2",
"=",
"np",
".",
"concatenate",
"(",
"(",
"d2",
",",
"d",
"*",
"[",
"pitch",
"[",
"1",
"]",
"*",
"d_i",
"]",
")",
")",
"pos2d",
"=",
"np",
".",
"vstack",
"(",
"(",
"d2",
",",
"d1",
")",
")",
".",
"T",
"if",
"'plane'",
"not",
"in",
"elinfo",
".",
"keys",
"(",
")",
":",
"# print(\"'plane' field with 2D dimensions assumed to be 'yz\")",
"plane",
"=",
"'yz'",
"else",
":",
"plane",
"=",
"elinfo",
"[",
"'plane'",
"]",
"if",
"'offset'",
"not",
"in",
"elinfo",
".",
"keys",
"(",
")",
":",
"offset",
"=",
"0",
"else",
":",
"offset",
"=",
"elinfo",
"[",
"'offset'",
"]",
"pos",
"=",
"add_3dim",
"(",
"pos2d",
",",
"plane",
",",
"offset",
")",
"electrode_pos",
"=",
"True",
"if",
"electrode_pos",
"and",
"center",
":",
"centered_pos",
"=",
"center_mea",
"(",
"pos",
")",
"# resort electrodes in case",
"centered_pos_sorted",
"=",
"copy",
".",
"deepcopy",
"(",
"centered_pos",
")",
"if",
"'sortlist'",
"in",
"elinfo",
".",
"keys",
"(",
")",
"and",
"elinfo",
"[",
"'sortlist'",
"]",
"is",
"not",
"None",
":",
"sortlist",
"=",
"elinfo",
"[",
"'sortlist'",
"]",
"for",
"i",
",",
"si",
"in",
"enumerate",
"(",
"sortlist",
")",
":",
"centered_pos_sorted",
"[",
"si",
"]",
"=",
"centered_pos",
"[",
"i",
"]",
"else",
":",
"centered_pos_sorted",
"=",
"centered_pos",
"return",
"centered_pos_sorted",
"elif",
"electrode_pos",
"and",
"not",
"center",
":",
"# resort electrodes in case",
"pos_sorted",
"=",
"copy",
".",
"deepcopy",
"(",
"pos",
")",
"if",
"'sortlist'",
"in",
"elinfo",
".",
"keys",
"(",
")",
"and",
"elinfo",
"[",
"'sortlist'",
"]",
"is",
"not",
"None",
":",
"sortlist",
"=",
"elinfo",
"[",
"'sortlist'",
"]",
"for",
"i",
",",
"si",
"in",
"enumerate",
"(",
"sortlist",
")",
":",
"pos_sorted",
"[",
"si",
"]",
"=",
"pos",
"[",
"i",
"]",
"else",
":",
"pos_sorted",
"=",
"pos",
"return",
"pos_sorted",
"else",
":",
"print",
"(",
"\"Define either a list of positions 'pos' or 'dim' and 'pitch'\"",
")",
"return",
"None"
] |
Computes the positions of the elctrodes based on the elinfo
Parameters
----------
elinfo: dict
Contains electrode information from yaml file (dim, pitch, sortlist, plane, pos)
Returns
-------
positions: np.array
3d points with the centers of the electrodes
|
[
"Computes",
"the",
"positions",
"of",
"the",
"elctrodes",
"based",
"on",
"the",
"elinfo"
] |
train
|
https://github.com/alejoe91/MEAutility/blob/7c2b0da52c2752a3baf04e8e248e26b0769cd088/MEAutility/core.py#L731-L903
|
alejoe91/MEAutility
|
MEAutility/core.py
|
add_mea
|
def add_mea(mea_yaml_path):
'''Adds the mea design defined by the yaml file in the install folder
Parameters
----------
mea_yaml_file
Returns
-------
'''
path = os.path.abspath(mea_yaml_path)
if path.endswith('.yaml') or path.endswith('.yml') and os.path.isfile(path):
with open(path, 'r') as meafile:
if use_loader:
elinfo = yaml.load(meafile, Loader=yaml.FullLoader)
else:
elinfo = yaml.load(meafile)
if 'pos' not in elinfo.keys():
if 'dim' in elinfo.keys():
if elinfo['dim'] != 1 and 'pitch' not in elinfo.keys():
raise AttributeError("The yaml file should contin either a list of 3d or 2d positions 'pos' or "
"intormation about dimension and pitch ('dim' and 'pitch')")
else:
raise AttributeError("The yaml file should contin either a list of 3d or 2d positions 'pos' or "
"intormation about dimension and pitch ('dim' and 'pitch') - unless dim=1")
this_dir, this_filename = os.path.split(__file__)
shutil.copy(path, os.path.join(this_dir, 'electrodes'))
if path.endswith('.yaml'):
electrodes = [f[:-5] for f in os.listdir(os.path.join(this_dir, "electrodes"))]
elif path.endswith('.yml'):
electrodes = [f[:-4] for f in os.listdir(os.path.join(this_dir, "electrodes"))]
print('Available MEA: \n', electrodes)
return
|
python
|
def add_mea(mea_yaml_path):
'''Adds the mea design defined by the yaml file in the install folder
Parameters
----------
mea_yaml_file
Returns
-------
'''
path = os.path.abspath(mea_yaml_path)
if path.endswith('.yaml') or path.endswith('.yml') and os.path.isfile(path):
with open(path, 'r') as meafile:
if use_loader:
elinfo = yaml.load(meafile, Loader=yaml.FullLoader)
else:
elinfo = yaml.load(meafile)
if 'pos' not in elinfo.keys():
if 'dim' in elinfo.keys():
if elinfo['dim'] != 1 and 'pitch' not in elinfo.keys():
raise AttributeError("The yaml file should contin either a list of 3d or 2d positions 'pos' or "
"intormation about dimension and pitch ('dim' and 'pitch')")
else:
raise AttributeError("The yaml file should contin either a list of 3d or 2d positions 'pos' or "
"intormation about dimension and pitch ('dim' and 'pitch') - unless dim=1")
this_dir, this_filename = os.path.split(__file__)
shutil.copy(path, os.path.join(this_dir, 'electrodes'))
if path.endswith('.yaml'):
electrodes = [f[:-5] for f in os.listdir(os.path.join(this_dir, "electrodes"))]
elif path.endswith('.yml'):
electrodes = [f[:-4] for f in os.listdir(os.path.join(this_dir, "electrodes"))]
print('Available MEA: \n', electrodes)
return
|
[
"def",
"add_mea",
"(",
"mea_yaml_path",
")",
":",
"path",
"=",
"os",
".",
"path",
".",
"abspath",
"(",
"mea_yaml_path",
")",
"if",
"path",
".",
"endswith",
"(",
"'.yaml'",
")",
"or",
"path",
".",
"endswith",
"(",
"'.yml'",
")",
"and",
"os",
".",
"path",
".",
"isfile",
"(",
"path",
")",
":",
"with",
"open",
"(",
"path",
",",
"'r'",
")",
"as",
"meafile",
":",
"if",
"use_loader",
":",
"elinfo",
"=",
"yaml",
".",
"load",
"(",
"meafile",
",",
"Loader",
"=",
"yaml",
".",
"FullLoader",
")",
"else",
":",
"elinfo",
"=",
"yaml",
".",
"load",
"(",
"meafile",
")",
"if",
"'pos'",
"not",
"in",
"elinfo",
".",
"keys",
"(",
")",
":",
"if",
"'dim'",
"in",
"elinfo",
".",
"keys",
"(",
")",
":",
"if",
"elinfo",
"[",
"'dim'",
"]",
"!=",
"1",
"and",
"'pitch'",
"not",
"in",
"elinfo",
".",
"keys",
"(",
")",
":",
"raise",
"AttributeError",
"(",
"\"The yaml file should contin either a list of 3d or 2d positions 'pos' or \"",
"\"intormation about dimension and pitch ('dim' and 'pitch')\"",
")",
"else",
":",
"raise",
"AttributeError",
"(",
"\"The yaml file should contin either a list of 3d or 2d positions 'pos' or \"",
"\"intormation about dimension and pitch ('dim' and 'pitch') - unless dim=1\"",
")",
"this_dir",
",",
"this_filename",
"=",
"os",
".",
"path",
".",
"split",
"(",
"__file__",
")",
"shutil",
".",
"copy",
"(",
"path",
",",
"os",
".",
"path",
".",
"join",
"(",
"this_dir",
",",
"'electrodes'",
")",
")",
"if",
"path",
".",
"endswith",
"(",
"'.yaml'",
")",
":",
"electrodes",
"=",
"[",
"f",
"[",
":",
"-",
"5",
"]",
"for",
"f",
"in",
"os",
".",
"listdir",
"(",
"os",
".",
"path",
".",
"join",
"(",
"this_dir",
",",
"\"electrodes\"",
")",
")",
"]",
"elif",
"path",
".",
"endswith",
"(",
"'.yml'",
")",
":",
"electrodes",
"=",
"[",
"f",
"[",
":",
"-",
"4",
"]",
"for",
"f",
"in",
"os",
".",
"listdir",
"(",
"os",
".",
"path",
".",
"join",
"(",
"this_dir",
",",
"\"electrodes\"",
")",
")",
"]",
"print",
"(",
"'Available MEA: \\n'",
",",
"electrodes",
")",
"return"
] |
Adds the mea design defined by the yaml file in the install folder
Parameters
----------
mea_yaml_file
Returns
-------
|
[
"Adds",
"the",
"mea",
"design",
"defined",
"by",
"the",
"yaml",
"file",
"in",
"the",
"install",
"folder"
] |
train
|
https://github.com/alejoe91/MEAutility/blob/7c2b0da52c2752a3baf04e8e248e26b0769cd088/MEAutility/core.py#L1038-L1073
|
alejoe91/MEAutility
|
MEAutility/core.py
|
remove_mea
|
def remove_mea(mea_name):
'''Adds the mea design defined by the yaml file in the install folder
Parameters
----------
mea_yaml_file
Returns
-------
'''
this_dir, this_filename = os.path.split(__file__)
electrodes = [f for f in os.listdir(os.path.join(this_dir, "electrodes"))]
for e in electrodes:
if mea_name in e:
if os.path.isfile(os.path.join(this_dir, "electrodes", mea_name + '.yaml')):
os.remove(os.path.join(this_dir, "electrodes", mea_name + '.yaml'))
print("Removed: ", os.path.join(this_dir, "electrodes", mea_name + '.yaml'))
elif os.path.isfile(os.path.join(this_dir, "electrodes", mea_name + '.yml')):
os.remove(os.path.join(this_dir, "electrodes", mea_name + '.yml'))
print("Removed: ", os.path.join(this_dir, "electrodes", mea_name + '.yml'))
electrodes = [f[:-5] for f in os.listdir(os.path.join(this_dir, "electrodes"))]
print('Available MEA: \n', electrodes)
return
|
python
|
def remove_mea(mea_name):
'''Adds the mea design defined by the yaml file in the install folder
Parameters
----------
mea_yaml_file
Returns
-------
'''
this_dir, this_filename = os.path.split(__file__)
electrodes = [f for f in os.listdir(os.path.join(this_dir, "electrodes"))]
for e in electrodes:
if mea_name in e:
if os.path.isfile(os.path.join(this_dir, "electrodes", mea_name + '.yaml')):
os.remove(os.path.join(this_dir, "electrodes", mea_name + '.yaml'))
print("Removed: ", os.path.join(this_dir, "electrodes", mea_name + '.yaml'))
elif os.path.isfile(os.path.join(this_dir, "electrodes", mea_name + '.yml')):
os.remove(os.path.join(this_dir, "electrodes", mea_name + '.yml'))
print("Removed: ", os.path.join(this_dir, "electrodes", mea_name + '.yml'))
electrodes = [f[:-5] for f in os.listdir(os.path.join(this_dir, "electrodes"))]
print('Available MEA: \n', electrodes)
return
|
[
"def",
"remove_mea",
"(",
"mea_name",
")",
":",
"this_dir",
",",
"this_filename",
"=",
"os",
".",
"path",
".",
"split",
"(",
"__file__",
")",
"electrodes",
"=",
"[",
"f",
"for",
"f",
"in",
"os",
".",
"listdir",
"(",
"os",
".",
"path",
".",
"join",
"(",
"this_dir",
",",
"\"electrodes\"",
")",
")",
"]",
"for",
"e",
"in",
"electrodes",
":",
"if",
"mea_name",
"in",
"e",
":",
"if",
"os",
".",
"path",
".",
"isfile",
"(",
"os",
".",
"path",
".",
"join",
"(",
"this_dir",
",",
"\"electrodes\"",
",",
"mea_name",
"+",
"'.yaml'",
")",
")",
":",
"os",
".",
"remove",
"(",
"os",
".",
"path",
".",
"join",
"(",
"this_dir",
",",
"\"electrodes\"",
",",
"mea_name",
"+",
"'.yaml'",
")",
")",
"print",
"(",
"\"Removed: \"",
",",
"os",
".",
"path",
".",
"join",
"(",
"this_dir",
",",
"\"electrodes\"",
",",
"mea_name",
"+",
"'.yaml'",
")",
")",
"elif",
"os",
".",
"path",
".",
"isfile",
"(",
"os",
".",
"path",
".",
"join",
"(",
"this_dir",
",",
"\"electrodes\"",
",",
"mea_name",
"+",
"'.yml'",
")",
")",
":",
"os",
".",
"remove",
"(",
"os",
".",
"path",
".",
"join",
"(",
"this_dir",
",",
"\"electrodes\"",
",",
"mea_name",
"+",
"'.yml'",
")",
")",
"print",
"(",
"\"Removed: \"",
",",
"os",
".",
"path",
".",
"join",
"(",
"this_dir",
",",
"\"electrodes\"",
",",
"mea_name",
"+",
"'.yml'",
")",
")",
"electrodes",
"=",
"[",
"f",
"[",
":",
"-",
"5",
"]",
"for",
"f",
"in",
"os",
".",
"listdir",
"(",
"os",
".",
"path",
".",
"join",
"(",
"this_dir",
",",
"\"electrodes\"",
")",
")",
"]",
"print",
"(",
"'Available MEA: \\n'",
",",
"electrodes",
")",
"return"
] |
Adds the mea design defined by the yaml file in the install folder
Parameters
----------
mea_yaml_file
Returns
-------
|
[
"Adds",
"the",
"mea",
"design",
"defined",
"by",
"the",
"yaml",
"file",
"in",
"the",
"install",
"folder"
] |
train
|
https://github.com/alejoe91/MEAutility/blob/7c2b0da52c2752a3baf04e8e248e26b0769cd088/MEAutility/core.py#L1076-L1099
|
koreyou/word_embedding_loader
|
word_embedding_loader/word_embedding.py
|
_get_two_lines
|
def _get_two_lines(f):
"""
Get the first and second lines
Args:
f (filelike): File that is opened for ascii.
Returns:
bytes
"""
l0 = f.readline()
l1 = f.readline()
return l0, l1
|
python
|
def _get_two_lines(f):
"""
Get the first and second lines
Args:
f (filelike): File that is opened for ascii.
Returns:
bytes
"""
l0 = f.readline()
l1 = f.readline()
return l0, l1
|
[
"def",
"_get_two_lines",
"(",
"f",
")",
":",
"l0",
"=",
"f",
".",
"readline",
"(",
")",
"l1",
"=",
"f",
".",
"readline",
"(",
")",
"return",
"l0",
",",
"l1"
] |
Get the first and second lines
Args:
f (filelike): File that is opened for ascii.
Returns:
bytes
|
[
"Get",
"the",
"first",
"and",
"second",
"lines",
"Args",
":",
"f",
"(",
"filelike",
")",
":",
"File",
"that",
"is",
"opened",
"for",
"ascii",
"."
] |
train
|
https://github.com/koreyou/word_embedding_loader/blob/1bc123f1a8bea12646576dcd768dae3ecea39c06/word_embedding_loader/word_embedding.py#L48-L60
|
koreyou/word_embedding_loader
|
word_embedding_loader/word_embedding.py
|
classify_format
|
def classify_format(f):
"""
Determine the format of word embedding file by their content. This operation
only looks at the first two lines and does not check the sanity of input
file.
Args:
f (Filelike):
Returns:
class
"""
l0, l1 = _get_two_lines(f)
if loader.glove.check_valid(l0, l1):
return _glove
elif loader.word2vec_text.check_valid(l0, l1):
return _word2vec_text
elif loader.word2vec_bin.check_valid(l0, l1):
return _word2vec_bin
else:
raise OSError(b"Invalid format")
|
python
|
def classify_format(f):
"""
Determine the format of word embedding file by their content. This operation
only looks at the first two lines and does not check the sanity of input
file.
Args:
f (Filelike):
Returns:
class
"""
l0, l1 = _get_two_lines(f)
if loader.glove.check_valid(l0, l1):
return _glove
elif loader.word2vec_text.check_valid(l0, l1):
return _word2vec_text
elif loader.word2vec_bin.check_valid(l0, l1):
return _word2vec_bin
else:
raise OSError(b"Invalid format")
|
[
"def",
"classify_format",
"(",
"f",
")",
":",
"l0",
",",
"l1",
"=",
"_get_two_lines",
"(",
"f",
")",
"if",
"loader",
".",
"glove",
".",
"check_valid",
"(",
"l0",
",",
"l1",
")",
":",
"return",
"_glove",
"elif",
"loader",
".",
"word2vec_text",
".",
"check_valid",
"(",
"l0",
",",
"l1",
")",
":",
"return",
"_word2vec_text",
"elif",
"loader",
".",
"word2vec_bin",
".",
"check_valid",
"(",
"l0",
",",
"l1",
")",
":",
"return",
"_word2vec_bin",
"else",
":",
"raise",
"OSError",
"(",
"b\"Invalid format\"",
")"
] |
Determine the format of word embedding file by their content. This operation
only looks at the first two lines and does not check the sanity of input
file.
Args:
f (Filelike):
Returns:
class
|
[
"Determine",
"the",
"format",
"of",
"word",
"embedding",
"file",
"by",
"their",
"content",
".",
"This",
"operation",
"only",
"looks",
"at",
"the",
"first",
"two",
"lines",
"and",
"does",
"not",
"check",
"the",
"sanity",
"of",
"input",
"file",
"."
] |
train
|
https://github.com/koreyou/word_embedding_loader/blob/1bc123f1a8bea12646576dcd768dae3ecea39c06/word_embedding_loader/word_embedding.py#L63-L84
|
koreyou/word_embedding_loader
|
word_embedding_loader/word_embedding.py
|
WordEmbedding.load
|
def load(cls, path, vocab=None, dtype=np.float32, max_vocab=None,
format=None, binary=False):
"""
Load pretrained word embedding from a file.
Args:
path (str): Path of file to load.
vocab (str or None): Path to vocabulary file created by word2vec
with ``-save-vocab <file>`` option. If vocab is given,
:py:attr:`~vectors` and :py:attr:`~vocab` is ordered in
descending order of frequency.
dtype (numpy.dtype): Element data type to use for the array.
max_vocab (int): Number of vocabulary to read.
format (str or None): Format of the file. ``'word2vec'`` for file
that was implemented in
`word2vec <https://code.google.com/archive/p/word2vec/>`_,
by Mikolov et al.. ``'glove'`` for file that was implemented in
`GloVe <https://nlp.stanford.edu/projects/glove/>`_, Global
Vectors for Word Representation, by Jeffrey Pennington,
Richard Socher, Christopher D. Manning from Stanford NLP group.
If ``None`` is given, the format is guessed from the content.
binary (bool): Load file as binary file as in word embedding file
created by
`word2vec <https://code.google.com/archive/p/word2vec/>`_ with
``-binary 1`` option. If ``format`` is ``'glove'`` or ``None``,
this argument is simply ignored
Returns:
:class:`~word_embedding_loader.word_embedding.WordEmbedding`
"""
freqs = None
if vocab is not None:
with open(vocab, mode='rb') as f:
freqs = loader.vocab.load_vocab(f)
# Create vocab from freqs
# [:None] gives all the list member
vocab = {k: i for i, (k, v) in enumerate(
sorted(six.iteritems(freqs),
key=lambda k_v: k_v[1], reverse=True)[:max_vocab])}
with open(path, mode='rb') as f:
if format is None:
mod = classify_format(f)
else:
mod = _select_module(format, binary)
with open(path, mode='rb') as f:
if vocab is not None:
arr = mod.loader.load_with_vocab(f, vocab, dtype=dtype)
v = vocab
else:
arr, v = mod.loader.load(f, max_vocab=max_vocab, dtype=dtype)
obj = cls(arr, v, freqs)
obj._load_cond = mod
return obj
|
python
|
def load(cls, path, vocab=None, dtype=np.float32, max_vocab=None,
format=None, binary=False):
"""
Load pretrained word embedding from a file.
Args:
path (str): Path of file to load.
vocab (str or None): Path to vocabulary file created by word2vec
with ``-save-vocab <file>`` option. If vocab is given,
:py:attr:`~vectors` and :py:attr:`~vocab` is ordered in
descending order of frequency.
dtype (numpy.dtype): Element data type to use for the array.
max_vocab (int): Number of vocabulary to read.
format (str or None): Format of the file. ``'word2vec'`` for file
that was implemented in
`word2vec <https://code.google.com/archive/p/word2vec/>`_,
by Mikolov et al.. ``'glove'`` for file that was implemented in
`GloVe <https://nlp.stanford.edu/projects/glove/>`_, Global
Vectors for Word Representation, by Jeffrey Pennington,
Richard Socher, Christopher D. Manning from Stanford NLP group.
If ``None`` is given, the format is guessed from the content.
binary (bool): Load file as binary file as in word embedding file
created by
`word2vec <https://code.google.com/archive/p/word2vec/>`_ with
``-binary 1`` option. If ``format`` is ``'glove'`` or ``None``,
this argument is simply ignored
Returns:
:class:`~word_embedding_loader.word_embedding.WordEmbedding`
"""
freqs = None
if vocab is not None:
with open(vocab, mode='rb') as f:
freqs = loader.vocab.load_vocab(f)
# Create vocab from freqs
# [:None] gives all the list member
vocab = {k: i for i, (k, v) in enumerate(
sorted(six.iteritems(freqs),
key=lambda k_v: k_v[1], reverse=True)[:max_vocab])}
with open(path, mode='rb') as f:
if format is None:
mod = classify_format(f)
else:
mod = _select_module(format, binary)
with open(path, mode='rb') as f:
if vocab is not None:
arr = mod.loader.load_with_vocab(f, vocab, dtype=dtype)
v = vocab
else:
arr, v = mod.loader.load(f, max_vocab=max_vocab, dtype=dtype)
obj = cls(arr, v, freqs)
obj._load_cond = mod
return obj
|
[
"def",
"load",
"(",
"cls",
",",
"path",
",",
"vocab",
"=",
"None",
",",
"dtype",
"=",
"np",
".",
"float32",
",",
"max_vocab",
"=",
"None",
",",
"format",
"=",
"None",
",",
"binary",
"=",
"False",
")",
":",
"freqs",
"=",
"None",
"if",
"vocab",
"is",
"not",
"None",
":",
"with",
"open",
"(",
"vocab",
",",
"mode",
"=",
"'rb'",
")",
"as",
"f",
":",
"freqs",
"=",
"loader",
".",
"vocab",
".",
"load_vocab",
"(",
"f",
")",
"# Create vocab from freqs",
"# [:None] gives all the list member",
"vocab",
"=",
"{",
"k",
":",
"i",
"for",
"i",
",",
"(",
"k",
",",
"v",
")",
"in",
"enumerate",
"(",
"sorted",
"(",
"six",
".",
"iteritems",
"(",
"freqs",
")",
",",
"key",
"=",
"lambda",
"k_v",
":",
"k_v",
"[",
"1",
"]",
",",
"reverse",
"=",
"True",
")",
"[",
":",
"max_vocab",
"]",
")",
"}",
"with",
"open",
"(",
"path",
",",
"mode",
"=",
"'rb'",
")",
"as",
"f",
":",
"if",
"format",
"is",
"None",
":",
"mod",
"=",
"classify_format",
"(",
"f",
")",
"else",
":",
"mod",
"=",
"_select_module",
"(",
"format",
",",
"binary",
")",
"with",
"open",
"(",
"path",
",",
"mode",
"=",
"'rb'",
")",
"as",
"f",
":",
"if",
"vocab",
"is",
"not",
"None",
":",
"arr",
"=",
"mod",
".",
"loader",
".",
"load_with_vocab",
"(",
"f",
",",
"vocab",
",",
"dtype",
"=",
"dtype",
")",
"v",
"=",
"vocab",
"else",
":",
"arr",
",",
"v",
"=",
"mod",
".",
"loader",
".",
"load",
"(",
"f",
",",
"max_vocab",
"=",
"max_vocab",
",",
"dtype",
"=",
"dtype",
")",
"obj",
"=",
"cls",
"(",
"arr",
",",
"v",
",",
"freqs",
")",
"obj",
".",
"_load_cond",
"=",
"mod",
"return",
"obj"
] |
Load pretrained word embedding from a file.
Args:
path (str): Path of file to load.
vocab (str or None): Path to vocabulary file created by word2vec
with ``-save-vocab <file>`` option. If vocab is given,
:py:attr:`~vectors` and :py:attr:`~vocab` is ordered in
descending order of frequency.
dtype (numpy.dtype): Element data type to use for the array.
max_vocab (int): Number of vocabulary to read.
format (str or None): Format of the file. ``'word2vec'`` for file
that was implemented in
`word2vec <https://code.google.com/archive/p/word2vec/>`_,
by Mikolov et al.. ``'glove'`` for file that was implemented in
`GloVe <https://nlp.stanford.edu/projects/glove/>`_, Global
Vectors for Word Representation, by Jeffrey Pennington,
Richard Socher, Christopher D. Manning from Stanford NLP group.
If ``None`` is given, the format is guessed from the content.
binary (bool): Load file as binary file as in word embedding file
created by
`word2vec <https://code.google.com/archive/p/word2vec/>`_ with
``-binary 1`` option. If ``format`` is ``'glove'`` or ``None``,
this argument is simply ignored
Returns:
:class:`~word_embedding_loader.word_embedding.WordEmbedding`
|
[
"Load",
"pretrained",
"word",
"embedding",
"from",
"a",
"file",
"."
] |
train
|
https://github.com/koreyou/word_embedding_loader/blob/1bc123f1a8bea12646576dcd768dae3ecea39c06/word_embedding_loader/word_embedding.py#L129-L184
|
koreyou/word_embedding_loader
|
word_embedding_loader/word_embedding.py
|
WordEmbedding.save
|
def save(self, path, format, binary=False, use_load_condition=False):
"""
Save object as word embedding file. For most arguments, you should refer
to :func:`~word_embedding_loader.word_embedding.WordEmbedding.load`.
Args:
use_load_condition (bool): If `True`, options from
:func:`~word_embedding_loader.word_embedding.WordEmbedding.load`
is used.
Raises:
ValueError: ``use_load_condition == True`` but the object is not
initialized via
:func:`~word_embedding_loader.word_embedding.WordEmbedding.load`.
"""
if use_load_condition:
if self._load_cond is None:
raise ValueError(
b"use_load_condition was specified but the object is not "
b"loaded from a file")
# Use load condition
mod = self._load_cond
else:
mod = _select_module(format, binary)
if self.freqs is None:
itr = list(
sorted(six.iteritems(self.vocab), key=lambda k_v: k_v[1]))
else:
itr = list(
sorted(six.iteritems(self.vocab),
key=lambda k_v: self.freqs[k_v[0]], reverse=True)
)
with open(path, mode='wb') as f:
mod.saver.save(f, self.vectors, itr)
|
python
|
def save(self, path, format, binary=False, use_load_condition=False):
"""
Save object as word embedding file. For most arguments, you should refer
to :func:`~word_embedding_loader.word_embedding.WordEmbedding.load`.
Args:
use_load_condition (bool): If `True`, options from
:func:`~word_embedding_loader.word_embedding.WordEmbedding.load`
is used.
Raises:
ValueError: ``use_load_condition == True`` but the object is not
initialized via
:func:`~word_embedding_loader.word_embedding.WordEmbedding.load`.
"""
if use_load_condition:
if self._load_cond is None:
raise ValueError(
b"use_load_condition was specified but the object is not "
b"loaded from a file")
# Use load condition
mod = self._load_cond
else:
mod = _select_module(format, binary)
if self.freqs is None:
itr = list(
sorted(six.iteritems(self.vocab), key=lambda k_v: k_v[1]))
else:
itr = list(
sorted(six.iteritems(self.vocab),
key=lambda k_v: self.freqs[k_v[0]], reverse=True)
)
with open(path, mode='wb') as f:
mod.saver.save(f, self.vectors, itr)
|
[
"def",
"save",
"(",
"self",
",",
"path",
",",
"format",
",",
"binary",
"=",
"False",
",",
"use_load_condition",
"=",
"False",
")",
":",
"if",
"use_load_condition",
":",
"if",
"self",
".",
"_load_cond",
"is",
"None",
":",
"raise",
"ValueError",
"(",
"b\"use_load_condition was specified but the object is not \"",
"b\"loaded from a file\"",
")",
"# Use load condition",
"mod",
"=",
"self",
".",
"_load_cond",
"else",
":",
"mod",
"=",
"_select_module",
"(",
"format",
",",
"binary",
")",
"if",
"self",
".",
"freqs",
"is",
"None",
":",
"itr",
"=",
"list",
"(",
"sorted",
"(",
"six",
".",
"iteritems",
"(",
"self",
".",
"vocab",
")",
",",
"key",
"=",
"lambda",
"k_v",
":",
"k_v",
"[",
"1",
"]",
")",
")",
"else",
":",
"itr",
"=",
"list",
"(",
"sorted",
"(",
"six",
".",
"iteritems",
"(",
"self",
".",
"vocab",
")",
",",
"key",
"=",
"lambda",
"k_v",
":",
"self",
".",
"freqs",
"[",
"k_v",
"[",
"0",
"]",
"]",
",",
"reverse",
"=",
"True",
")",
")",
"with",
"open",
"(",
"path",
",",
"mode",
"=",
"'wb'",
")",
"as",
"f",
":",
"mod",
".",
"saver",
".",
"save",
"(",
"f",
",",
"self",
".",
"vectors",
",",
"itr",
")"
] |
Save object as word embedding file. For most arguments, you should refer
to :func:`~word_embedding_loader.word_embedding.WordEmbedding.load`.
Args:
use_load_condition (bool): If `True`, options from
:func:`~word_embedding_loader.word_embedding.WordEmbedding.load`
is used.
Raises:
ValueError: ``use_load_condition == True`` but the object is not
initialized via
:func:`~word_embedding_loader.word_embedding.WordEmbedding.load`.
|
[
"Save",
"object",
"as",
"word",
"embedding",
"file",
".",
"For",
"most",
"arguments",
"you",
"should",
"refer",
"to",
":",
"func",
":",
"~word_embedding_loader",
".",
"word_embedding",
".",
"WordEmbedding",
".",
"load",
"."
] |
train
|
https://github.com/koreyou/word_embedding_loader/blob/1bc123f1a8bea12646576dcd768dae3ecea39c06/word_embedding_loader/word_embedding.py#L186-L222
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/build_tasks/paths.py
|
paths_for_shell
|
def paths_for_shell(paths, separator=' '):
"""
Converts a list of paths for use in shell commands
"""
paths = filter(None, paths)
paths = map(shlex.quote, paths)
if separator is None:
return paths
return separator.join(paths)
|
python
|
def paths_for_shell(paths, separator=' '):
"""
Converts a list of paths for use in shell commands
"""
paths = filter(None, paths)
paths = map(shlex.quote, paths)
if separator is None:
return paths
return separator.join(paths)
|
[
"def",
"paths_for_shell",
"(",
"paths",
",",
"separator",
"=",
"' '",
")",
":",
"paths",
"=",
"filter",
"(",
"None",
",",
"paths",
")",
"paths",
"=",
"map",
"(",
"shlex",
".",
"quote",
",",
"paths",
")",
"if",
"separator",
"is",
"None",
":",
"return",
"paths",
"return",
"separator",
".",
"join",
"(",
"paths",
")"
] |
Converts a list of paths for use in shell commands
|
[
"Converts",
"a",
"list",
"of",
"paths",
"for",
"use",
"in",
"shell",
"commands"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/build_tasks/paths.py#L19-L27
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/build_tasks/executor.py
|
Tasks.register
|
def register(self, *dependencies, default=False, hidden=False, ignore_return_code=False):
"""
Decorates a callable to turn it into a task
"""
def outer(func):
task = Task(func, *dependencies, default=default, hidden=hidden, ignore_return_code=ignore_return_code)
overidden_task = self._tasks.pop(task.name, None)
if overidden_task:
self._overidden_tasks[task.name].append(overidden_task)
self[task.name] = task
return task
return outer
|
python
|
def register(self, *dependencies, default=False, hidden=False, ignore_return_code=False):
"""
Decorates a callable to turn it into a task
"""
def outer(func):
task = Task(func, *dependencies, default=default, hidden=hidden, ignore_return_code=ignore_return_code)
overidden_task = self._tasks.pop(task.name, None)
if overidden_task:
self._overidden_tasks[task.name].append(overidden_task)
self[task.name] = task
return task
return outer
|
[
"def",
"register",
"(",
"self",
",",
"*",
"dependencies",
",",
"default",
"=",
"False",
",",
"hidden",
"=",
"False",
",",
"ignore_return_code",
"=",
"False",
")",
":",
"def",
"outer",
"(",
"func",
")",
":",
"task",
"=",
"Task",
"(",
"func",
",",
"*",
"dependencies",
",",
"default",
"=",
"default",
",",
"hidden",
"=",
"hidden",
",",
"ignore_return_code",
"=",
"ignore_return_code",
")",
"overidden_task",
"=",
"self",
".",
"_tasks",
".",
"pop",
"(",
"task",
".",
"name",
",",
"None",
")",
"if",
"overidden_task",
":",
"self",
".",
"_overidden_tasks",
"[",
"task",
".",
"name",
"]",
".",
"append",
"(",
"overidden_task",
")",
"self",
"[",
"task",
".",
"name",
"]",
"=",
"task",
"return",
"task",
"return",
"outer"
] |
Decorates a callable to turn it into a task
|
[
"Decorates",
"a",
"callable",
"to",
"turn",
"it",
"into",
"a",
"task"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/build_tasks/executor.py#L56-L69
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/build_tasks/executor.py
|
Tasks.lookup_task
|
def lookup_task(self, task):
"""
Looks up a task by name or by callable
"""
if isinstance(task, str):
try:
return self[task]
except KeyError:
pass
raise TaskError('Unknown task %s' % task)
|
python
|
def lookup_task(self, task):
"""
Looks up a task by name or by callable
"""
if isinstance(task, str):
try:
return self[task]
except KeyError:
pass
raise TaskError('Unknown task %s' % task)
|
[
"def",
"lookup_task",
"(",
"self",
",",
"task",
")",
":",
"if",
"isinstance",
"(",
"task",
",",
"str",
")",
":",
"try",
":",
"return",
"self",
"[",
"task",
"]",
"except",
"KeyError",
":",
"pass",
"raise",
"TaskError",
"(",
"'Unknown task %s'",
"%",
"task",
")"
] |
Looks up a task by name or by callable
|
[
"Looks",
"up",
"a",
"task",
"by",
"name",
"or",
"by",
"callable"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/build_tasks/executor.py#L71-L80
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/build_tasks/executor.py
|
Tasks.get_default_task
|
def get_default_task(self):
"""
Returns the default task if there is only one
"""
default_tasks = list(filter(lambda task: task.default, self.values()))
if len(default_tasks) == 1:
return default_tasks[0]
|
python
|
def get_default_task(self):
"""
Returns the default task if there is only one
"""
default_tasks = list(filter(lambda task: task.default, self.values()))
if len(default_tasks) == 1:
return default_tasks[0]
|
[
"def",
"get_default_task",
"(",
"self",
")",
":",
"default_tasks",
"=",
"list",
"(",
"filter",
"(",
"lambda",
"task",
":",
"task",
".",
"default",
",",
"self",
".",
"values",
"(",
")",
")",
")",
"if",
"len",
"(",
"default_tasks",
")",
"==",
"1",
":",
"return",
"default_tasks",
"[",
"0",
"]"
] |
Returns the default task if there is only one
|
[
"Returns",
"the",
"default",
"task",
"if",
"there",
"is",
"only",
"one"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/build_tasks/executor.py#L82-L88
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/build_tasks/executor.py
|
ParameterGroup.from_callable
|
def from_callable(cls, func, ignored_parameters=set()):
"""
Reads a function or method signature to produce a set of parameters
"""
group = cls()
signature = inspect.signature(func)
for parameter in signature.parameters.values():
if parameter.name.startswith('_') or parameter.name in ignored_parameters:
continue
parameter = Parameter.from_callable_parameter(parameter)
group[parameter.name] = parameter
return group
|
python
|
def from_callable(cls, func, ignored_parameters=set()):
"""
Reads a function or method signature to produce a set of parameters
"""
group = cls()
signature = inspect.signature(func)
for parameter in signature.parameters.values():
if parameter.name.startswith('_') or parameter.name in ignored_parameters:
continue
parameter = Parameter.from_callable_parameter(parameter)
group[parameter.name] = parameter
return group
|
[
"def",
"from_callable",
"(",
"cls",
",",
"func",
",",
"ignored_parameters",
"=",
"set",
"(",
")",
")",
":",
"group",
"=",
"cls",
"(",
")",
"signature",
"=",
"inspect",
".",
"signature",
"(",
"func",
")",
"for",
"parameter",
"in",
"signature",
".",
"parameters",
".",
"values",
"(",
")",
":",
"if",
"parameter",
".",
"name",
".",
"startswith",
"(",
"'_'",
")",
"or",
"parameter",
".",
"name",
"in",
"ignored_parameters",
":",
"continue",
"parameter",
"=",
"Parameter",
".",
"from_callable_parameter",
"(",
"parameter",
")",
"group",
"[",
"parameter",
".",
"name",
"]",
"=",
"parameter",
"return",
"group"
] |
Reads a function or method signature to produce a set of parameters
|
[
"Reads",
"a",
"function",
"or",
"method",
"signature",
"to",
"produce",
"a",
"set",
"of",
"parameters"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/build_tasks/executor.py#L137-L148
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/build_tasks/executor.py
|
ParameterGroup.from_mapping
|
def from_mapping(cls, mapping):
"""
Produces a set of parameters from a mapping
"""
group = cls()
for name, value in mapping.items():
if name.startswith('_'):
continue
group[name] = Parameter(
name=name,
value=value,
constraint=Parameter.constraint_from_type(value),
)
return group
|
python
|
def from_mapping(cls, mapping):
"""
Produces a set of parameters from a mapping
"""
group = cls()
for name, value in mapping.items():
if name.startswith('_'):
continue
group[name] = Parameter(
name=name,
value=value,
constraint=Parameter.constraint_from_type(value),
)
return group
|
[
"def",
"from_mapping",
"(",
"cls",
",",
"mapping",
")",
":",
"group",
"=",
"cls",
"(",
")",
"for",
"name",
",",
"value",
"in",
"mapping",
".",
"items",
"(",
")",
":",
"if",
"name",
".",
"startswith",
"(",
"'_'",
")",
":",
"continue",
"group",
"[",
"name",
"]",
"=",
"Parameter",
"(",
"name",
"=",
"name",
",",
"value",
"=",
"value",
",",
"constraint",
"=",
"Parameter",
".",
"constraint_from_type",
"(",
"value",
")",
",",
")",
"return",
"group"
] |
Produces a set of parameters from a mapping
|
[
"Produces",
"a",
"set",
"of",
"parameters",
"from",
"a",
"mapping"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/build_tasks/executor.py#L151-L164
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/build_tasks/executor.py
|
ParameterGroup.to_dict
|
def to_dict(self):
"""
Converts the set of parameters into a dict
"""
return dict((parameter.name, parameter.value) for parameter in self.values())
|
python
|
def to_dict(self):
"""
Converts the set of parameters into a dict
"""
return dict((parameter.name, parameter.value) for parameter in self.values())
|
[
"def",
"to_dict",
"(",
"self",
")",
":",
"return",
"dict",
"(",
"(",
"parameter",
".",
"name",
",",
"parameter",
".",
"value",
")",
"for",
"parameter",
"in",
"self",
".",
"values",
"(",
")",
")"
] |
Converts the set of parameters into a dict
|
[
"Converts",
"the",
"set",
"of",
"parameters",
"into",
"a",
"dict"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/build_tasks/executor.py#L189-L193
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/build_tasks/executor.py
|
ParameterGroup.consume_arguments
|
def consume_arguments(self, argument_list):
"""
Takes arguments from a list while there are parameters that can accept them
"""
while True:
argument_count = len(argument_list)
for parameter in self.values():
argument_list = parameter.consume_arguments(argument_list)
if len(argument_list) == argument_count:
return argument_list
|
python
|
def consume_arguments(self, argument_list):
"""
Takes arguments from a list while there are parameters that can accept them
"""
while True:
argument_count = len(argument_list)
for parameter in self.values():
argument_list = parameter.consume_arguments(argument_list)
if len(argument_list) == argument_count:
return argument_list
|
[
"def",
"consume_arguments",
"(",
"self",
",",
"argument_list",
")",
":",
"while",
"True",
":",
"argument_count",
"=",
"len",
"(",
"argument_list",
")",
"for",
"parameter",
"in",
"self",
".",
"values",
"(",
")",
":",
"argument_list",
"=",
"parameter",
".",
"consume_arguments",
"(",
"argument_list",
")",
"if",
"len",
"(",
"argument_list",
")",
"==",
"argument_count",
":",
"return",
"argument_list"
] |
Takes arguments from a list while there are parameters that can accept them
|
[
"Takes",
"arguments",
"from",
"a",
"list",
"while",
"there",
"are",
"parameters",
"that",
"can",
"accept",
"them"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/build_tasks/executor.py#L195-L204
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/build_tasks/executor.py
|
ParameterGroup.update_from
|
def update_from(self, mapping):
"""
Updates the set of parameters from a mapping for keys that already exist
"""
for key, value in mapping.items():
if key in self:
if isinstance(value, Parameter):
value = value.value
self[key].value = value
|
python
|
def update_from(self, mapping):
"""
Updates the set of parameters from a mapping for keys that already exist
"""
for key, value in mapping.items():
if key in self:
if isinstance(value, Parameter):
value = value.value
self[key].value = value
|
[
"def",
"update_from",
"(",
"self",
",",
"mapping",
")",
":",
"for",
"key",
",",
"value",
"in",
"mapping",
".",
"items",
"(",
")",
":",
"if",
"key",
"in",
"self",
":",
"if",
"isinstance",
"(",
"value",
",",
"Parameter",
")",
":",
"value",
"=",
"value",
".",
"value",
"self",
"[",
"key",
"]",
".",
"value",
"=",
"value"
] |
Updates the set of parameters from a mapping for keys that already exist
|
[
"Updates",
"the",
"set",
"of",
"parameters",
"from",
"a",
"mapping",
"for",
"keys",
"that",
"already",
"exist"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/build_tasks/executor.py#L206-L214
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/build_tasks/executor.py
|
Parameter.from_callable_parameter
|
def from_callable_parameter(cls, parameter):
"""
Produces a parameter from a function or method
"""
if parameter.kind == parameter.KEYWORD_ONLY or \
parameter.kind == parameter.POSITIONAL_OR_KEYWORD and parameter.default is not parameter.empty:
if parameter.annotation is not parameter.empty:
constraint = parameter.annotation
else:
constraint = Parameter.constraint_from_type(parameter.default)
return cls(
name=parameter.name,
value=parameter.default,
constraint=constraint,
)
else:
raise ParameterError('Only keyword parameters are supported')
|
python
|
def from_callable_parameter(cls, parameter):
"""
Produces a parameter from a function or method
"""
if parameter.kind == parameter.KEYWORD_ONLY or \
parameter.kind == parameter.POSITIONAL_OR_KEYWORD and parameter.default is not parameter.empty:
if parameter.annotation is not parameter.empty:
constraint = parameter.annotation
else:
constraint = Parameter.constraint_from_type(parameter.default)
return cls(
name=parameter.name,
value=parameter.default,
constraint=constraint,
)
else:
raise ParameterError('Only keyword parameters are supported')
|
[
"def",
"from_callable_parameter",
"(",
"cls",
",",
"parameter",
")",
":",
"if",
"parameter",
".",
"kind",
"==",
"parameter",
".",
"KEYWORD_ONLY",
"or",
"parameter",
".",
"kind",
"==",
"parameter",
".",
"POSITIONAL_OR_KEYWORD",
"and",
"parameter",
".",
"default",
"is",
"not",
"parameter",
".",
"empty",
":",
"if",
"parameter",
".",
"annotation",
"is",
"not",
"parameter",
".",
"empty",
":",
"constraint",
"=",
"parameter",
".",
"annotation",
"else",
":",
"constraint",
"=",
"Parameter",
".",
"constraint_from_type",
"(",
"parameter",
".",
"default",
")",
"return",
"cls",
"(",
"name",
"=",
"parameter",
".",
"name",
",",
"value",
"=",
"parameter",
".",
"default",
",",
"constraint",
"=",
"constraint",
",",
")",
"else",
":",
"raise",
"ParameterError",
"(",
"'Only keyword parameters are supported'",
")"
] |
Produces a parameter from a function or method
|
[
"Produces",
"a",
"parameter",
"from",
"a",
"function",
"or",
"method"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/build_tasks/executor.py#L223-L239
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/build_tasks/executor.py
|
Parameter.constraint_from_type
|
def constraint_from_type(cls, value):
"""
Returns the constraint callable given a value
"""
if value is None:
return None
value_type = type(value)
if value_type in (str, int, bool):
return value_type
raise ParameterError('Parameter type cannot be %s' % value_type)
|
python
|
def constraint_from_type(cls, value):
"""
Returns the constraint callable given a value
"""
if value is None:
return None
value_type = type(value)
if value_type in (str, int, bool):
return value_type
raise ParameterError('Parameter type cannot be %s' % value_type)
|
[
"def",
"constraint_from_type",
"(",
"cls",
",",
"value",
")",
":",
"if",
"value",
"is",
"None",
":",
"return",
"None",
"value_type",
"=",
"type",
"(",
"value",
")",
"if",
"value_type",
"in",
"(",
"str",
",",
"int",
",",
"bool",
")",
":",
"return",
"value_type",
"raise",
"ParameterError",
"(",
"'Parameter type cannot be %s'",
"%",
"value_type",
")"
] |
Returns the constraint callable given a value
|
[
"Returns",
"the",
"constraint",
"callable",
"given",
"a",
"value"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/build_tasks/executor.py#L242-L251
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/build_tasks/executor.py
|
Parameter.constraint_from_choices
|
def constraint_from_choices(cls, value_type: type, choices: collections.Sequence):
"""
Returns a constraint callable based on choices of a given type
"""
choices_str = ', '.join(map(str, choices))
def constraint(value):
value = value_type(value)
if value not in choices:
raise ParameterError('Argument must be one of %s' % choices_str)
return value
constraint.__name__ = 'choices_%s' % value_type.__name__
constraint.__doc__ = 'choice of %s' % choices_str
return constraint
|
python
|
def constraint_from_choices(cls, value_type: type, choices: collections.Sequence):
"""
Returns a constraint callable based on choices of a given type
"""
choices_str = ', '.join(map(str, choices))
def constraint(value):
value = value_type(value)
if value not in choices:
raise ParameterError('Argument must be one of %s' % choices_str)
return value
constraint.__name__ = 'choices_%s' % value_type.__name__
constraint.__doc__ = 'choice of %s' % choices_str
return constraint
|
[
"def",
"constraint_from_choices",
"(",
"cls",
",",
"value_type",
":",
"type",
",",
"choices",
":",
"collections",
".",
"Sequence",
")",
":",
"choices_str",
"=",
"', '",
".",
"join",
"(",
"map",
"(",
"str",
",",
"choices",
")",
")",
"def",
"constraint",
"(",
"value",
")",
":",
"value",
"=",
"value_type",
"(",
"value",
")",
"if",
"value",
"not",
"in",
"choices",
":",
"raise",
"ParameterError",
"(",
"'Argument must be one of %s'",
"%",
"choices_str",
")",
"return",
"value",
"constraint",
".",
"__name__",
"=",
"'choices_%s'",
"%",
"value_type",
".",
"__name__",
"constraint",
".",
"__doc__",
"=",
"'choice of %s'",
"%",
"choices_str",
"return",
"constraint"
] |
Returns a constraint callable based on choices of a given type
|
[
"Returns",
"a",
"constraint",
"callable",
"based",
"on",
"choices",
"of",
"a",
"given",
"type"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/build_tasks/executor.py#L254-L268
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/build_tasks/executor.py
|
Parameter.arg_name
|
def arg_name(self):
"""
Returns the name of the parameter as a command line flag
"""
if self.constraint is bool and self.value:
return '--no-%s' % self.name.replace('_', '-')
return '--%s' % self.name.replace('_', '-')
|
python
|
def arg_name(self):
"""
Returns the name of the parameter as a command line flag
"""
if self.constraint is bool and self.value:
return '--no-%s' % self.name.replace('_', '-')
return '--%s' % self.name.replace('_', '-')
|
[
"def",
"arg_name",
"(",
"self",
")",
":",
"if",
"self",
".",
"constraint",
"is",
"bool",
"and",
"self",
".",
"value",
":",
"return",
"'--no-%s'",
"%",
"self",
".",
"name",
".",
"replace",
"(",
"'_'",
",",
"'-'",
")",
"return",
"'--%s'",
"%",
"self",
".",
"name",
".",
"replace",
"(",
"'_'",
",",
"'-'",
")"
] |
Returns the name of the parameter as a command line flag
|
[
"Returns",
"the",
"name",
"of",
"the",
"parameter",
"as",
"a",
"command",
"line",
"flag"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/build_tasks/executor.py#L280-L286
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/build_tasks/executor.py
|
Parameter.consume_arguments
|
def consume_arguments(self, argument_list):
"""
Takes arguments from a list while this parameter can accept them
"""
if len(argument_list) == 0:
return []
if argument_list[0] == self.arg_name:
argument_list = argument_list[1:]
if self.constraint is bool:
self.value = not self.value
else:
try:
value = argument_list.pop(0)
except IndexError:
raise ParameterError('Argument %s expects a value' % self.arg_name)
self.value = value
return argument_list
|
python
|
def consume_arguments(self, argument_list):
"""
Takes arguments from a list while this parameter can accept them
"""
if len(argument_list) == 0:
return []
if argument_list[0] == self.arg_name:
argument_list = argument_list[1:]
if self.constraint is bool:
self.value = not self.value
else:
try:
value = argument_list.pop(0)
except IndexError:
raise ParameterError('Argument %s expects a value' % self.arg_name)
self.value = value
return argument_list
|
[
"def",
"consume_arguments",
"(",
"self",
",",
"argument_list",
")",
":",
"if",
"len",
"(",
"argument_list",
")",
"==",
"0",
":",
"return",
"[",
"]",
"if",
"argument_list",
"[",
"0",
"]",
"==",
"self",
".",
"arg_name",
":",
"argument_list",
"=",
"argument_list",
"[",
"1",
":",
"]",
"if",
"self",
".",
"constraint",
"is",
"bool",
":",
"self",
".",
"value",
"=",
"not",
"self",
".",
"value",
"else",
":",
"try",
":",
"value",
"=",
"argument_list",
".",
"pop",
"(",
"0",
")",
"except",
"IndexError",
":",
"raise",
"ParameterError",
"(",
"'Argument %s expects a value'",
"%",
"self",
".",
"arg_name",
")",
"self",
".",
"value",
"=",
"value",
"return",
"argument_list"
] |
Takes arguments from a list while this parameter can accept them
|
[
"Takes",
"arguments",
"from",
"a",
"list",
"while",
"this",
"parameter",
"can",
"accept",
"them"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/build_tasks/executor.py#L314-L330
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/build_tasks/executor.py
|
Context.pip_command
|
def pip_command(self, command, *args):
"""
Runs a pip command
"""
try:
from pip._internal import main as pip_main
except ImportError:
from pip import main as pip_main
args = [command] + list(args)
if self.verbosity == 0:
args.insert(0, '--quiet')
elif self.verbosity == 2:
args.insert(0, '--verbose')
return pip_main(args)
|
python
|
def pip_command(self, command, *args):
"""
Runs a pip command
"""
try:
from pip._internal import main as pip_main
except ImportError:
from pip import main as pip_main
args = [command] + list(args)
if self.verbosity == 0:
args.insert(0, '--quiet')
elif self.verbosity == 2:
args.insert(0, '--verbose')
return pip_main(args)
|
[
"def",
"pip_command",
"(",
"self",
",",
"command",
",",
"*",
"args",
")",
":",
"try",
":",
"from",
"pip",
".",
"_internal",
"import",
"main",
"as",
"pip_main",
"except",
"ImportError",
":",
"from",
"pip",
"import",
"main",
"as",
"pip_main",
"args",
"=",
"[",
"command",
"]",
"+",
"list",
"(",
"args",
")",
"if",
"self",
".",
"verbosity",
"==",
"0",
":",
"args",
".",
"insert",
"(",
"0",
",",
"'--quiet'",
")",
"elif",
"self",
".",
"verbosity",
"==",
"2",
":",
"args",
".",
"insert",
"(",
"0",
",",
"'--verbose'",
")",
"return",
"pip_main",
"(",
"args",
")"
] |
Runs a pip command
|
[
"Runs",
"a",
"pip",
"command"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/build_tasks/executor.py#L418-L432
|
ministryofjustice/money-to-prisoners-common
|
mtp_common/build_tasks/executor.py
|
Context.shell
|
def shell(self, command, *args, environment=None):
"""
Runs a shell command
"""
command += ' ' + ' '.join(args)
command = command.strip()
self.debug(self.yellow_style('$ %s' % command))
env = self.env.copy()
env.update(environment or {})
return subprocess.call(command, shell=True, env=env)
|
python
|
def shell(self, command, *args, environment=None):
"""
Runs a shell command
"""
command += ' ' + ' '.join(args)
command = command.strip()
self.debug(self.yellow_style('$ %s' % command))
env = self.env.copy()
env.update(environment or {})
return subprocess.call(command, shell=True, env=env)
|
[
"def",
"shell",
"(",
"self",
",",
"command",
",",
"*",
"args",
",",
"environment",
"=",
"None",
")",
":",
"command",
"+=",
"' '",
"+",
"' '",
".",
"join",
"(",
"args",
")",
"command",
"=",
"command",
".",
"strip",
"(",
")",
"self",
".",
"debug",
"(",
"self",
".",
"yellow_style",
"(",
"'$ %s'",
"%",
"command",
")",
")",
"env",
"=",
"self",
".",
"env",
".",
"copy",
"(",
")",
"env",
".",
"update",
"(",
"environment",
"or",
"{",
"}",
")",
"return",
"subprocess",
".",
"call",
"(",
"command",
",",
"shell",
"=",
"True",
",",
"env",
"=",
"env",
")"
] |
Runs a shell command
|
[
"Runs",
"a",
"shell",
"command"
] |
train
|
https://github.com/ministryofjustice/money-to-prisoners-common/blob/33c43a2912cb990d9148da7c8718f480f07d90a1/mtp_common/build_tasks/executor.py#L434-L443
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.