repo stringlengths 7 55 | path stringlengths 4 223 | func_name stringlengths 1 134 | original_string stringlengths 75 104k | language stringclasses 1 value | code stringlengths 75 104k | code_tokens listlengths 19 28.4k | docstring stringlengths 1 46.9k | docstring_tokens listlengths 1 1.97k | sha stringlengths 40 40 | url stringlengths 87 315 | partition stringclasses 1 value |
|---|---|---|---|---|---|---|---|---|---|---|---|
gak/pygooglechart | pygooglechart.py | Chart.set_legend | def set_legend(self, legend):
"""legend needs to be a list, tuple or None"""
assert(isinstance(legend, list) or isinstance(legend, tuple) or
legend is None)
if legend:
self.legend = [quote(a) for a in legend]
else:
self.legend = None | python | def set_legend(self, legend):
"""legend needs to be a list, tuple or None"""
assert(isinstance(legend, list) or isinstance(legend, tuple) or
legend is None)
if legend:
self.legend = [quote(a) for a in legend]
else:
self.legend = None | [
"def",
"set_legend",
"(",
"self",
",",
"legend",
")",
":",
"assert",
"(",
"isinstance",
"(",
"legend",
",",
"list",
")",
"or",
"isinstance",
"(",
"legend",
",",
"tuple",
")",
"or",
"legend",
"is",
"None",
")",
"if",
"legend",
":",
"self",
".",
"legend",
"=",
"[",
"quote",
"(",
"a",
")",
"for",
"a",
"in",
"legend",
"]",
"else",
":",
"self",
".",
"legend",
"=",
"None"
] | legend needs to be a list, tuple or None | [
"legend",
"needs",
"to",
"be",
"a",
"list",
"tuple",
"or",
"None"
] | 25234bb63127a7e5e057c0b98ab841f3f1d93b21 | https://github.com/gak/pygooglechart/blob/25234bb63127a7e5e057c0b98ab841f3f1d93b21/pygooglechart.py#L434-L441 | train |
gak/pygooglechart | pygooglechart.py | Chart.set_legend_position | def set_legend_position(self, legend_position):
"""Sets legend position. Default is 'r'.
b - At the bottom of the chart, legend entries in a horizontal row.
bv - At the bottom of the chart, legend entries in a vertical column.
t - At the top of the chart, legend entries in a horizontal row.
tv - At the top of the chart, legend entries in a vertical column.
r - To the right of the chart, legend entries in a vertical column.
l - To the left of the chart, legend entries in a vertical column.
"""
if legend_position:
self.legend_position = quote(legend_position)
else:
self.legend_position = None | python | def set_legend_position(self, legend_position):
"""Sets legend position. Default is 'r'.
b - At the bottom of the chart, legend entries in a horizontal row.
bv - At the bottom of the chart, legend entries in a vertical column.
t - At the top of the chart, legend entries in a horizontal row.
tv - At the top of the chart, legend entries in a vertical column.
r - To the right of the chart, legend entries in a vertical column.
l - To the left of the chart, legend entries in a vertical column.
"""
if legend_position:
self.legend_position = quote(legend_position)
else:
self.legend_position = None | [
"def",
"set_legend_position",
"(",
"self",
",",
"legend_position",
")",
":",
"if",
"legend_position",
":",
"self",
".",
"legend_position",
"=",
"quote",
"(",
"legend_position",
")",
"else",
":",
"self",
".",
"legend_position",
"=",
"None"
] | Sets legend position. Default is 'r'.
b - At the bottom of the chart, legend entries in a horizontal row.
bv - At the bottom of the chart, legend entries in a vertical column.
t - At the top of the chart, legend entries in a horizontal row.
tv - At the top of the chart, legend entries in a vertical column.
r - To the right of the chart, legend entries in a vertical column.
l - To the left of the chart, legend entries in a vertical column. | [
"Sets",
"legend",
"position",
".",
"Default",
"is",
"r",
"."
] | 25234bb63127a7e5e057c0b98ab841f3f1d93b21 | https://github.com/gak/pygooglechart/blob/25234bb63127a7e5e057c0b98ab841f3f1d93b21/pygooglechart.py#L443-L456 | train |
gak/pygooglechart | pygooglechart.py | Chart.data_class_detection | def data_class_detection(self, data):
"""Determines the appropriate data encoding type to give satisfactory
resolution (http://code.google.com/apis/chart/#chart_data).
"""
assert(isinstance(data, list) or isinstance(data, tuple))
if not isinstance(self, (LineChart, BarChart, ScatterChart)):
# From the link above:
# Simple encoding is suitable for all other types of chart
# regardless of size.
return SimpleData
elif self.height < 100:
# The link above indicates that line and bar charts less
# than 300px in size can be suitably represented with the
# simple encoding. I've found that this isn't sufficient,
# e.g. examples/line-xy-circle.png. Let's try 100px.
return SimpleData
else:
return ExtendedData | python | def data_class_detection(self, data):
"""Determines the appropriate data encoding type to give satisfactory
resolution (http://code.google.com/apis/chart/#chart_data).
"""
assert(isinstance(data, list) or isinstance(data, tuple))
if not isinstance(self, (LineChart, BarChart, ScatterChart)):
# From the link above:
# Simple encoding is suitable for all other types of chart
# regardless of size.
return SimpleData
elif self.height < 100:
# The link above indicates that line and bar charts less
# than 300px in size can be suitably represented with the
# simple encoding. I've found that this isn't sufficient,
# e.g. examples/line-xy-circle.png. Let's try 100px.
return SimpleData
else:
return ExtendedData | [
"def",
"data_class_detection",
"(",
"self",
",",
"data",
")",
":",
"assert",
"(",
"isinstance",
"(",
"data",
",",
"list",
")",
"or",
"isinstance",
"(",
"data",
",",
"tuple",
")",
")",
"if",
"not",
"isinstance",
"(",
"self",
",",
"(",
"LineChart",
",",
"BarChart",
",",
"ScatterChart",
")",
")",
":",
"# From the link above:",
"# Simple encoding is suitable for all other types of chart",
"# regardless of size.",
"return",
"SimpleData",
"elif",
"self",
".",
"height",
"<",
"100",
":",
"# The link above indicates that line and bar charts less",
"# than 300px in size can be suitably represented with the",
"# simple encoding. I've found that this isn't sufficient,",
"# e.g. examples/line-xy-circle.png. Let's try 100px.",
"return",
"SimpleData",
"else",
":",
"return",
"ExtendedData"
] | Determines the appropriate data encoding type to give satisfactory
resolution (http://code.google.com/apis/chart/#chart_data). | [
"Determines",
"the",
"appropriate",
"data",
"encoding",
"type",
"to",
"give",
"satisfactory",
"resolution",
"(",
"http",
":",
"//",
"code",
".",
"google",
".",
"com",
"/",
"apis",
"/",
"chart",
"/",
"#chart_data",
")",
"."
] | 25234bb63127a7e5e057c0b98ab841f3f1d93b21 | https://github.com/gak/pygooglechart/blob/25234bb63127a7e5e057c0b98ab841f3f1d93b21/pygooglechart.py#L527-L544 | train |
gak/pygooglechart | pygooglechart.py | Chart.data_x_range | def data_x_range(self):
"""Return a 2-tuple giving the minimum and maximum x-axis
data range.
"""
try:
lower = min([min(self._filter_none(s))
for type, s in self.annotated_data()
if type == 'x'])
upper = max([max(self._filter_none(s))
for type, s in self.annotated_data()
if type == 'x'])
return (lower, upper)
except ValueError:
return None | python | def data_x_range(self):
"""Return a 2-tuple giving the minimum and maximum x-axis
data range.
"""
try:
lower = min([min(self._filter_none(s))
for type, s in self.annotated_data()
if type == 'x'])
upper = max([max(self._filter_none(s))
for type, s in self.annotated_data()
if type == 'x'])
return (lower, upper)
except ValueError:
return None | [
"def",
"data_x_range",
"(",
"self",
")",
":",
"try",
":",
"lower",
"=",
"min",
"(",
"[",
"min",
"(",
"self",
".",
"_filter_none",
"(",
"s",
")",
")",
"for",
"type",
",",
"s",
"in",
"self",
".",
"annotated_data",
"(",
")",
"if",
"type",
"==",
"'x'",
"]",
")",
"upper",
"=",
"max",
"(",
"[",
"max",
"(",
"self",
".",
"_filter_none",
"(",
"s",
")",
")",
"for",
"type",
",",
"s",
"in",
"self",
".",
"annotated_data",
"(",
")",
"if",
"type",
"==",
"'x'",
"]",
")",
"return",
"(",
"lower",
",",
"upper",
")",
"except",
"ValueError",
":",
"return",
"None"
] | Return a 2-tuple giving the minimum and maximum x-axis
data range. | [
"Return",
"a",
"2",
"-",
"tuple",
"giving",
"the",
"minimum",
"and",
"maximum",
"x",
"-",
"axis",
"data",
"range",
"."
] | 25234bb63127a7e5e057c0b98ab841f3f1d93b21 | https://github.com/gak/pygooglechart/blob/25234bb63127a7e5e057c0b98ab841f3f1d93b21/pygooglechart.py#L549-L562 | train |
gak/pygooglechart | pygooglechart.py | Chart.scaled_data | def scaled_data(self, data_class, x_range=None, y_range=None):
"""Scale `self.data` as appropriate for the given data encoding
(data_class) and return it.
An optional `y_range` -- a 2-tuple (lower, upper) -- can be
given to specify the y-axis bounds. If not given, the range is
inferred from the data: (0, <max-value>) presuming no negative
values, or (<min-value>, <max-value>) if there are negative
values. `self.scaled_y_range` is set to the actual lower and
upper scaling range.
Ditto for `x_range`. Note that some chart types don't have x-axis
data.
"""
self.scaled_data_class = data_class
# Determine the x-axis range for scaling.
if x_range is None:
x_range = self.data_x_range()
if x_range and x_range[0] > 0:
x_range = (x_range[0], x_range[1])
self.scaled_x_range = x_range
# Determine the y-axis range for scaling.
if y_range is None:
y_range = self.data_y_range()
if y_range and y_range[0] > 0:
y_range = (y_range[0], y_range[1])
self.scaled_y_range = y_range
scaled_data = []
for type, dataset in self.annotated_data():
if type == 'x':
scale_range = x_range
elif type == 'y':
scale_range = y_range
elif type == 'marker-size':
scale_range = (0, max(dataset))
scaled_dataset = []
for v in dataset:
if v is None:
scaled_dataset.append(None)
else:
scaled_dataset.append(
data_class.scale_value(v, scale_range))
scaled_data.append(scaled_dataset)
return scaled_data | python | def scaled_data(self, data_class, x_range=None, y_range=None):
"""Scale `self.data` as appropriate for the given data encoding
(data_class) and return it.
An optional `y_range` -- a 2-tuple (lower, upper) -- can be
given to specify the y-axis bounds. If not given, the range is
inferred from the data: (0, <max-value>) presuming no negative
values, or (<min-value>, <max-value>) if there are negative
values. `self.scaled_y_range` is set to the actual lower and
upper scaling range.
Ditto for `x_range`. Note that some chart types don't have x-axis
data.
"""
self.scaled_data_class = data_class
# Determine the x-axis range for scaling.
if x_range is None:
x_range = self.data_x_range()
if x_range and x_range[0] > 0:
x_range = (x_range[0], x_range[1])
self.scaled_x_range = x_range
# Determine the y-axis range for scaling.
if y_range is None:
y_range = self.data_y_range()
if y_range and y_range[0] > 0:
y_range = (y_range[0], y_range[1])
self.scaled_y_range = y_range
scaled_data = []
for type, dataset in self.annotated_data():
if type == 'x':
scale_range = x_range
elif type == 'y':
scale_range = y_range
elif type == 'marker-size':
scale_range = (0, max(dataset))
scaled_dataset = []
for v in dataset:
if v is None:
scaled_dataset.append(None)
else:
scaled_dataset.append(
data_class.scale_value(v, scale_range))
scaled_data.append(scaled_dataset)
return scaled_data | [
"def",
"scaled_data",
"(",
"self",
",",
"data_class",
",",
"x_range",
"=",
"None",
",",
"y_range",
"=",
"None",
")",
":",
"self",
".",
"scaled_data_class",
"=",
"data_class",
"# Determine the x-axis range for scaling.",
"if",
"x_range",
"is",
"None",
":",
"x_range",
"=",
"self",
".",
"data_x_range",
"(",
")",
"if",
"x_range",
"and",
"x_range",
"[",
"0",
"]",
">",
"0",
":",
"x_range",
"=",
"(",
"x_range",
"[",
"0",
"]",
",",
"x_range",
"[",
"1",
"]",
")",
"self",
".",
"scaled_x_range",
"=",
"x_range",
"# Determine the y-axis range for scaling.",
"if",
"y_range",
"is",
"None",
":",
"y_range",
"=",
"self",
".",
"data_y_range",
"(",
")",
"if",
"y_range",
"and",
"y_range",
"[",
"0",
"]",
">",
"0",
":",
"y_range",
"=",
"(",
"y_range",
"[",
"0",
"]",
",",
"y_range",
"[",
"1",
"]",
")",
"self",
".",
"scaled_y_range",
"=",
"y_range",
"scaled_data",
"=",
"[",
"]",
"for",
"type",
",",
"dataset",
"in",
"self",
".",
"annotated_data",
"(",
")",
":",
"if",
"type",
"==",
"'x'",
":",
"scale_range",
"=",
"x_range",
"elif",
"type",
"==",
"'y'",
":",
"scale_range",
"=",
"y_range",
"elif",
"type",
"==",
"'marker-size'",
":",
"scale_range",
"=",
"(",
"0",
",",
"max",
"(",
"dataset",
")",
")",
"scaled_dataset",
"=",
"[",
"]",
"for",
"v",
"in",
"dataset",
":",
"if",
"v",
"is",
"None",
":",
"scaled_dataset",
".",
"append",
"(",
"None",
")",
"else",
":",
"scaled_dataset",
".",
"append",
"(",
"data_class",
".",
"scale_value",
"(",
"v",
",",
"scale_range",
")",
")",
"scaled_data",
".",
"append",
"(",
"scaled_dataset",
")",
"return",
"scaled_data"
] | Scale `self.data` as appropriate for the given data encoding
(data_class) and return it.
An optional `y_range` -- a 2-tuple (lower, upper) -- can be
given to specify the y-axis bounds. If not given, the range is
inferred from the data: (0, <max-value>) presuming no negative
values, or (<min-value>, <max-value>) if there are negative
values. `self.scaled_y_range` is set to the actual lower and
upper scaling range.
Ditto for `x_range`. Note that some chart types don't have x-axis
data. | [
"Scale",
"self",
".",
"data",
"as",
"appropriate",
"for",
"the",
"given",
"data",
"encoding",
"(",
"data_class",
")",
"and",
"return",
"it",
"."
] | 25234bb63127a7e5e057c0b98ab841f3f1d93b21 | https://github.com/gak/pygooglechart/blob/25234bb63127a7e5e057c0b98ab841f3f1d93b21/pygooglechart.py#L579-L625 | train |
gak/pygooglechart | pygooglechart.py | MapChart.set_codes | def set_codes(self, codes):
'''Set the country code map for the data.
Codes given in a list.
i.e. DE - Germany
AT - Austria
US - United States
'''
codemap = ''
for cc in codes:
cc = cc.upper()
if cc in self.__ccodes:
codemap += cc
else:
raise UnknownCountryCodeException(cc)
self.codes = codemap | python | def set_codes(self, codes):
'''Set the country code map for the data.
Codes given in a list.
i.e. DE - Germany
AT - Austria
US - United States
'''
codemap = ''
for cc in codes:
cc = cc.upper()
if cc in self.__ccodes:
codemap += cc
else:
raise UnknownCountryCodeException(cc)
self.codes = codemap | [
"def",
"set_codes",
"(",
"self",
",",
"codes",
")",
":",
"codemap",
"=",
"''",
"for",
"cc",
"in",
"codes",
":",
"cc",
"=",
"cc",
".",
"upper",
"(",
")",
"if",
"cc",
"in",
"self",
".",
"__ccodes",
":",
"codemap",
"+=",
"cc",
"else",
":",
"raise",
"UnknownCountryCodeException",
"(",
"cc",
")",
"self",
".",
"codes",
"=",
"codemap"
] | Set the country code map for the data.
Codes given in a list.
i.e. DE - Germany
AT - Austria
US - United States | [
"Set",
"the",
"country",
"code",
"map",
"for",
"the",
"data",
".",
"Codes",
"given",
"in",
"a",
"list",
"."
] | 25234bb63127a7e5e057c0b98ab841f3f1d93b21 | https://github.com/gak/pygooglechart/blob/25234bb63127a7e5e057c0b98ab841f3f1d93b21/pygooglechart.py#L1021-L1039 | train |
gak/pygooglechart | pygooglechart.py | MapChart.set_geo_area | def set_geo_area(self, area):
'''Sets the geo area for the map.
* africa
* asia
* europe
* middle_east
* south_america
* usa
* world
'''
if area in self.__areas:
self.geo_area = area
else:
raise UnknownChartType('Unknown chart type for maps: %s' %area) | python | def set_geo_area(self, area):
'''Sets the geo area for the map.
* africa
* asia
* europe
* middle_east
* south_america
* usa
* world
'''
if area in self.__areas:
self.geo_area = area
else:
raise UnknownChartType('Unknown chart type for maps: %s' %area) | [
"def",
"set_geo_area",
"(",
"self",
",",
"area",
")",
":",
"if",
"area",
"in",
"self",
".",
"__areas",
":",
"self",
".",
"geo_area",
"=",
"area",
"else",
":",
"raise",
"UnknownChartType",
"(",
"'Unknown chart type for maps: %s'",
"%",
"area",
")"
] | Sets the geo area for the map.
* africa
* asia
* europe
* middle_east
* south_america
* usa
* world | [
"Sets",
"the",
"geo",
"area",
"for",
"the",
"map",
"."
] | 25234bb63127a7e5e057c0b98ab841f3f1d93b21 | https://github.com/gak/pygooglechart/blob/25234bb63127a7e5e057c0b98ab841f3f1d93b21/pygooglechart.py#L1041-L1056 | train |
gak/pygooglechart | pygooglechart.py | MapChart.add_data_dict | def add_data_dict(self, datadict):
'''Sets the data and country codes via a dictionary.
i.e. {'DE': 50, 'GB': 30, 'AT': 70}
'''
self.set_codes(list(datadict.keys()))
self.add_data(list(datadict.values())) | python | def add_data_dict(self, datadict):
'''Sets the data and country codes via a dictionary.
i.e. {'DE': 50, 'GB': 30, 'AT': 70}
'''
self.set_codes(list(datadict.keys()))
self.add_data(list(datadict.values())) | [
"def",
"add_data_dict",
"(",
"self",
",",
"datadict",
")",
":",
"self",
".",
"set_codes",
"(",
"list",
"(",
"datadict",
".",
"keys",
"(",
")",
")",
")",
"self",
".",
"add_data",
"(",
"list",
"(",
"datadict",
".",
"values",
"(",
")",
")",
")"
] | Sets the data and country codes via a dictionary.
i.e. {'DE': 50, 'GB': 30, 'AT': 70} | [
"Sets",
"the",
"data",
"and",
"country",
"codes",
"via",
"a",
"dictionary",
"."
] | 25234bb63127a7e5e057c0b98ab841f3f1d93b21 | https://github.com/gak/pygooglechart/blob/25234bb63127a7e5e057c0b98ab841f3f1d93b21/pygooglechart.py#L1065-L1072 | train |
hsolbrig/PyShEx | pyshex/utils/datatype_utils.py | can_cast_to | def can_cast_to(v: Literal, dt: str) -> bool:
""" 5.4.3 Datatype Constraints
Determine whether "a value of the lexical form of n can be cast to the target type v per
XPath Functions 3.1 section 19 Casting[xpath-functions]."
"""
# TODO: rdflib doesn't appear to pay any attention to lengths (e.g. 257 is a valid XSD.byte)
return v.value is not None and Literal(str(v), datatype=dt).value is not None | python | def can_cast_to(v: Literal, dt: str) -> bool:
""" 5.4.3 Datatype Constraints
Determine whether "a value of the lexical form of n can be cast to the target type v per
XPath Functions 3.1 section 19 Casting[xpath-functions]."
"""
# TODO: rdflib doesn't appear to pay any attention to lengths (e.g. 257 is a valid XSD.byte)
return v.value is not None and Literal(str(v), datatype=dt).value is not None | [
"def",
"can_cast_to",
"(",
"v",
":",
"Literal",
",",
"dt",
":",
"str",
")",
"->",
"bool",
":",
"# TODO: rdflib doesn't appear to pay any attention to lengths (e.g. 257 is a valid XSD.byte)",
"return",
"v",
".",
"value",
"is",
"not",
"None",
"and",
"Literal",
"(",
"str",
"(",
"v",
")",
",",
"datatype",
"=",
"dt",
")",
".",
"value",
"is",
"not",
"None"
] | 5.4.3 Datatype Constraints
Determine whether "a value of the lexical form of n can be cast to the target type v per
XPath Functions 3.1 section 19 Casting[xpath-functions]." | [
"5",
".",
"4",
".",
"3",
"Datatype",
"Constraints"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/utils/datatype_utils.py#L12-L19 | train |
hsolbrig/PyShEx | pyshex/utils/datatype_utils.py | total_digits | def total_digits(n: Literal) -> Optional[int]:
""" 5.4.5 XML Schema Numberic Facet Constraints
totaldigits and fractiondigits constraints on values not derived from xsd:decimal fail.
"""
return len(str(abs(int(n.value)))) + fraction_digits(n) if is_numeric(n) and n.value is not None else None | python | def total_digits(n: Literal) -> Optional[int]:
""" 5.4.5 XML Schema Numberic Facet Constraints
totaldigits and fractiondigits constraints on values not derived from xsd:decimal fail.
"""
return len(str(abs(int(n.value)))) + fraction_digits(n) if is_numeric(n) and n.value is not None else None | [
"def",
"total_digits",
"(",
"n",
":",
"Literal",
")",
"->",
"Optional",
"[",
"int",
"]",
":",
"return",
"len",
"(",
"str",
"(",
"abs",
"(",
"int",
"(",
"n",
".",
"value",
")",
")",
")",
")",
"+",
"fraction_digits",
"(",
"n",
")",
"if",
"is_numeric",
"(",
"n",
")",
"and",
"n",
".",
"value",
"is",
"not",
"None",
"else",
"None"
] | 5.4.5 XML Schema Numberic Facet Constraints
totaldigits and fractiondigits constraints on values not derived from xsd:decimal fail. | [
"5",
".",
"4",
".",
"5",
"XML",
"Schema",
"Numberic",
"Facet",
"Constraints"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/utils/datatype_utils.py#L22-L27 | train |
hsolbrig/PyShEx | pyshex/utils/datatype_utils.py | fraction_digits | def fraction_digits(n: Literal) -> Optional[int]:
""" 5.4.5 XML Schema Numeric Facet Constraints
for "fractiondigits" constraints, v is less than or equals the number of digits to the right of the decimal place
in the XML Schema canonical form[xmlschema-2] of the value of n, ignoring trailing zeros.
"""
# Note - the last expression below isolates the fractional portion, reverses it (e.g. 017320 --> 023710) and
# converts it to an integer and back to a string
return None if not is_numeric(n) or n.value is None \
else 0 if is_integer(n) or '.' not in str(n.value) or str(n.value).split('.')[1] == '0' \
else len(str(int(str(n.value).split('.')[1][::-1]))) | python | def fraction_digits(n: Literal) -> Optional[int]:
""" 5.4.5 XML Schema Numeric Facet Constraints
for "fractiondigits" constraints, v is less than or equals the number of digits to the right of the decimal place
in the XML Schema canonical form[xmlschema-2] of the value of n, ignoring trailing zeros.
"""
# Note - the last expression below isolates the fractional portion, reverses it (e.g. 017320 --> 023710) and
# converts it to an integer and back to a string
return None if not is_numeric(n) or n.value is None \
else 0 if is_integer(n) or '.' not in str(n.value) or str(n.value).split('.')[1] == '0' \
else len(str(int(str(n.value).split('.')[1][::-1]))) | [
"def",
"fraction_digits",
"(",
"n",
":",
"Literal",
")",
"->",
"Optional",
"[",
"int",
"]",
":",
"# Note - the last expression below isolates the fractional portion, reverses it (e.g. 017320 --> 023710) and",
"# converts it to an integer and back to a string",
"return",
"None",
"if",
"not",
"is_numeric",
"(",
"n",
")",
"or",
"n",
".",
"value",
"is",
"None",
"else",
"0",
"if",
"is_integer",
"(",
"n",
")",
"or",
"'.'",
"not",
"in",
"str",
"(",
"n",
".",
"value",
")",
"or",
"str",
"(",
"n",
".",
"value",
")",
".",
"split",
"(",
"'.'",
")",
"[",
"1",
"]",
"==",
"'0'",
"else",
"len",
"(",
"str",
"(",
"int",
"(",
"str",
"(",
"n",
".",
"value",
")",
".",
"split",
"(",
"'.'",
")",
"[",
"1",
"]",
"[",
":",
":",
"-",
"1",
"]",
")",
")",
")"
] | 5.4.5 XML Schema Numeric Facet Constraints
for "fractiondigits" constraints, v is less than or equals the number of digits to the right of the decimal place
in the XML Schema canonical form[xmlschema-2] of the value of n, ignoring trailing zeros. | [
"5",
".",
"4",
".",
"5",
"XML",
"Schema",
"Numeric",
"Facet",
"Constraints"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/utils/datatype_utils.py#L30-L40 | train |
hsolbrig/PyShEx | pyshex/utils/datatype_utils.py | _map_xpath_flags_to_re | def _map_xpath_flags_to_re(expr: str, xpath_flags: str) -> Tuple[int, str]:
""" Map `5.6.2 Flags <https://www.w3.org/TR/xpath-functions-31/#flags>`_ to python
:param expr: match pattern
:param xpath_flags: xpath flags
:returns: python flags / modified match pattern
"""
python_flags: int = 0
modified_expr = expr
if xpath_flags is None:
xpath_flags = ""
if 's' in xpath_flags:
python_flags |= re.DOTALL
if 'm' in xpath_flags:
python_flags |= re.MULTILINE
if 'i' in xpath_flags:
python_flags |= re.IGNORECASE
if 'x' in xpath_flags:
modified_expr = re.sub(r'[\t\n\r ]|\[[^\]]*\]', _char_class_escape, modified_expr)
if 'q' in xpath_flags:
modified_expr = re.escape(modified_expr)
return python_flags, modified_expr | python | def _map_xpath_flags_to_re(expr: str, xpath_flags: str) -> Tuple[int, str]:
""" Map `5.6.2 Flags <https://www.w3.org/TR/xpath-functions-31/#flags>`_ to python
:param expr: match pattern
:param xpath_flags: xpath flags
:returns: python flags / modified match pattern
"""
python_flags: int = 0
modified_expr = expr
if xpath_flags is None:
xpath_flags = ""
if 's' in xpath_flags:
python_flags |= re.DOTALL
if 'm' in xpath_flags:
python_flags |= re.MULTILINE
if 'i' in xpath_flags:
python_flags |= re.IGNORECASE
if 'x' in xpath_flags:
modified_expr = re.sub(r'[\t\n\r ]|\[[^\]]*\]', _char_class_escape, modified_expr)
if 'q' in xpath_flags:
modified_expr = re.escape(modified_expr)
return python_flags, modified_expr | [
"def",
"_map_xpath_flags_to_re",
"(",
"expr",
":",
"str",
",",
"xpath_flags",
":",
"str",
")",
"->",
"Tuple",
"[",
"int",
",",
"str",
"]",
":",
"python_flags",
":",
"int",
"=",
"0",
"modified_expr",
"=",
"expr",
"if",
"xpath_flags",
"is",
"None",
":",
"xpath_flags",
"=",
"\"\"",
"if",
"'s'",
"in",
"xpath_flags",
":",
"python_flags",
"|=",
"re",
".",
"DOTALL",
"if",
"'m'",
"in",
"xpath_flags",
":",
"python_flags",
"|=",
"re",
".",
"MULTILINE",
"if",
"'i'",
"in",
"xpath_flags",
":",
"python_flags",
"|=",
"re",
".",
"IGNORECASE",
"if",
"'x'",
"in",
"xpath_flags",
":",
"modified_expr",
"=",
"re",
".",
"sub",
"(",
"r'[\\t\\n\\r ]|\\[[^\\]]*\\]'",
",",
"_char_class_escape",
",",
"modified_expr",
")",
"if",
"'q'",
"in",
"xpath_flags",
":",
"modified_expr",
"=",
"re",
".",
"escape",
"(",
"modified_expr",
")",
"return",
"python_flags",
",",
"modified_expr"
] | Map `5.6.2 Flags <https://www.w3.org/TR/xpath-functions-31/#flags>`_ to python
:param expr: match pattern
:param xpath_flags: xpath flags
:returns: python flags / modified match pattern | [
"Map",
"5",
".",
"6",
".",
"2",
"Flags",
"<https",
":",
"//",
"www",
".",
"w3",
".",
"org",
"/",
"TR",
"/",
"xpath",
"-",
"functions",
"-",
"31",
"/",
"#flags",
">",
"_",
"to",
"python"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/utils/datatype_utils.py#L61-L84 | train |
hsolbrig/PyShEx | pyshex/utils/datatype_utils.py | map_object_literal | def map_object_literal(v: Union[str, jsonasobj.JsonObj]) -> ShExJ.ObjectLiteral:
""" `PyShEx.jsg <https://github.com/hsolbrig/ShExJSG/ShExJSG/ShExJ.jsg>`_ does not add identifying
types to ObjectLiterals. This routine re-identifies the types
"""
# TODO: isinstance(v, JSGString) should work here, but it doesn't with IRIREF(http://a.example/v1)
return v if issubclass(type(v), JSGString) or (isinstance(v, JSGObject) and 'type' in v) else \
ShExJ.IRIREF(v) if isinstance(v, str) else ShExJ.ObjectLiteral(**v._as_dict) | python | def map_object_literal(v: Union[str, jsonasobj.JsonObj]) -> ShExJ.ObjectLiteral:
""" `PyShEx.jsg <https://github.com/hsolbrig/ShExJSG/ShExJSG/ShExJ.jsg>`_ does not add identifying
types to ObjectLiterals. This routine re-identifies the types
"""
# TODO: isinstance(v, JSGString) should work here, but it doesn't with IRIREF(http://a.example/v1)
return v if issubclass(type(v), JSGString) or (isinstance(v, JSGObject) and 'type' in v) else \
ShExJ.IRIREF(v) if isinstance(v, str) else ShExJ.ObjectLiteral(**v._as_dict) | [
"def",
"map_object_literal",
"(",
"v",
":",
"Union",
"[",
"str",
",",
"jsonasobj",
".",
"JsonObj",
"]",
")",
"->",
"ShExJ",
".",
"ObjectLiteral",
":",
"# TODO: isinstance(v, JSGString) should work here, but it doesn't with IRIREF(http://a.example/v1)",
"return",
"v",
"if",
"issubclass",
"(",
"type",
"(",
"v",
")",
",",
"JSGString",
")",
"or",
"(",
"isinstance",
"(",
"v",
",",
"JSGObject",
")",
"and",
"'type'",
"in",
"v",
")",
"else",
"ShExJ",
".",
"IRIREF",
"(",
"v",
")",
"if",
"isinstance",
"(",
"v",
",",
"str",
")",
"else",
"ShExJ",
".",
"ObjectLiteral",
"(",
"*",
"*",
"v",
".",
"_as_dict",
")"
] | `PyShEx.jsg <https://github.com/hsolbrig/ShExJSG/ShExJSG/ShExJ.jsg>`_ does not add identifying
types to ObjectLiterals. This routine re-identifies the types | [
"PyShEx",
".",
"jsg",
"<https",
":",
"//",
"github",
".",
"com",
"/",
"hsolbrig",
"/",
"ShExJSG",
"/",
"ShExJSG",
"/",
"ShExJ",
".",
"jsg",
">",
"_",
"does",
"not",
"add",
"identifying",
"types",
"to",
"ObjectLiterals",
".",
"This",
"routine",
"re",
"-",
"identifies",
"the",
"types"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/utils/datatype_utils.py#L95-L101 | train |
stuaxo/vext | vext/cmdline/__init__.py | do_enable | def do_enable():
"""
Uncomment any lines that start with #import in the .pth file
"""
try:
_lines = []
with open(vext_pth, mode='r') as f:
for line in f.readlines():
if line.startswith('#') and line[1:].lstrip().startswith('import '):
_lines.append(line[1:].lstrip())
else:
_lines.append(line)
try:
os.unlink('%s.tmp' % vext_pth)
except:
pass
with open('%s.tmp' % vext_pth, mode='w+') as f:
f.writelines(_lines)
try:
os.unlink('%s~' % vext_pth)
except:
pass
os.rename(vext_pth, '%s~' % vext_pth)
os.rename('%s.tmp' % vext_pth, vext_pth)
except IOError as e:
if e.errno == 2:
# vext file doesn't exist, recreate it.
create_pth() | python | def do_enable():
"""
Uncomment any lines that start with #import in the .pth file
"""
try:
_lines = []
with open(vext_pth, mode='r') as f:
for line in f.readlines():
if line.startswith('#') and line[1:].lstrip().startswith('import '):
_lines.append(line[1:].lstrip())
else:
_lines.append(line)
try:
os.unlink('%s.tmp' % vext_pth)
except:
pass
with open('%s.tmp' % vext_pth, mode='w+') as f:
f.writelines(_lines)
try:
os.unlink('%s~' % vext_pth)
except:
pass
os.rename(vext_pth, '%s~' % vext_pth)
os.rename('%s.tmp' % vext_pth, vext_pth)
except IOError as e:
if e.errno == 2:
# vext file doesn't exist, recreate it.
create_pth() | [
"def",
"do_enable",
"(",
")",
":",
"try",
":",
"_lines",
"=",
"[",
"]",
"with",
"open",
"(",
"vext_pth",
",",
"mode",
"=",
"'r'",
")",
"as",
"f",
":",
"for",
"line",
"in",
"f",
".",
"readlines",
"(",
")",
":",
"if",
"line",
".",
"startswith",
"(",
"'#'",
")",
"and",
"line",
"[",
"1",
":",
"]",
".",
"lstrip",
"(",
")",
".",
"startswith",
"(",
"'import '",
")",
":",
"_lines",
".",
"append",
"(",
"line",
"[",
"1",
":",
"]",
".",
"lstrip",
"(",
")",
")",
"else",
":",
"_lines",
".",
"append",
"(",
"line",
")",
"try",
":",
"os",
".",
"unlink",
"(",
"'%s.tmp'",
"%",
"vext_pth",
")",
"except",
":",
"pass",
"with",
"open",
"(",
"'%s.tmp'",
"%",
"vext_pth",
",",
"mode",
"=",
"'w+'",
")",
"as",
"f",
":",
"f",
".",
"writelines",
"(",
"_lines",
")",
"try",
":",
"os",
".",
"unlink",
"(",
"'%s~'",
"%",
"vext_pth",
")",
"except",
":",
"pass",
"os",
".",
"rename",
"(",
"vext_pth",
",",
"'%s~'",
"%",
"vext_pth",
")",
"os",
".",
"rename",
"(",
"'%s.tmp'",
"%",
"vext_pth",
",",
"vext_pth",
")",
"except",
"IOError",
"as",
"e",
":",
"if",
"e",
".",
"errno",
"==",
"2",
":",
"# vext file doesn't exist, recreate it.",
"create_pth",
"(",
")"
] | Uncomment any lines that start with #import in the .pth file | [
"Uncomment",
"any",
"lines",
"that",
"start",
"with",
"#import",
"in",
"the",
".",
"pth",
"file"
] | fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69 | https://github.com/stuaxo/vext/blob/fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69/vext/cmdline/__init__.py#L32-L63 | train |
stuaxo/vext | vext/cmdline/__init__.py | do_disable | def do_disable():
"""
Comment any lines that start with import in the .pth file
"""
from vext import vext_pth
try:
_lines = []
with open(vext_pth, mode='r') as f:
for line in f.readlines():
if not line.startswith('#') and line.startswith('import '):
_lines.append('# %s' % line)
else:
_lines.append(line)
try:
os.unlink('%s.tmp' % vext_pth)
except:
pass
with open('%s.tmp' % vext_pth, mode='w+') as f:
f.writelines(_lines)
try:
os.unlink('%s~' % vext_pth)
except:
pass
os.rename(vext_pth, '%s~' % vext_pth)
os.rename('%s.tmp' % vext_pth, vext_pth)
except IOError as e:
if e.errno == 2: # file didn't exist == disabled
return | python | def do_disable():
"""
Comment any lines that start with import in the .pth file
"""
from vext import vext_pth
try:
_lines = []
with open(vext_pth, mode='r') as f:
for line in f.readlines():
if not line.startswith('#') and line.startswith('import '):
_lines.append('# %s' % line)
else:
_lines.append(line)
try:
os.unlink('%s.tmp' % vext_pth)
except:
pass
with open('%s.tmp' % vext_pth, mode='w+') as f:
f.writelines(_lines)
try:
os.unlink('%s~' % vext_pth)
except:
pass
os.rename(vext_pth, '%s~' % vext_pth)
os.rename('%s.tmp' % vext_pth, vext_pth)
except IOError as e:
if e.errno == 2: # file didn't exist == disabled
return | [
"def",
"do_disable",
"(",
")",
":",
"from",
"vext",
"import",
"vext_pth",
"try",
":",
"_lines",
"=",
"[",
"]",
"with",
"open",
"(",
"vext_pth",
",",
"mode",
"=",
"'r'",
")",
"as",
"f",
":",
"for",
"line",
"in",
"f",
".",
"readlines",
"(",
")",
":",
"if",
"not",
"line",
".",
"startswith",
"(",
"'#'",
")",
"and",
"line",
".",
"startswith",
"(",
"'import '",
")",
":",
"_lines",
".",
"append",
"(",
"'# %s'",
"%",
"line",
")",
"else",
":",
"_lines",
".",
"append",
"(",
"line",
")",
"try",
":",
"os",
".",
"unlink",
"(",
"'%s.tmp'",
"%",
"vext_pth",
")",
"except",
":",
"pass",
"with",
"open",
"(",
"'%s.tmp'",
"%",
"vext_pth",
",",
"mode",
"=",
"'w+'",
")",
"as",
"f",
":",
"f",
".",
"writelines",
"(",
"_lines",
")",
"try",
":",
"os",
".",
"unlink",
"(",
"'%s~'",
"%",
"vext_pth",
")",
"except",
":",
"pass",
"os",
".",
"rename",
"(",
"vext_pth",
",",
"'%s~'",
"%",
"vext_pth",
")",
"os",
".",
"rename",
"(",
"'%s.tmp'",
"%",
"vext_pth",
",",
"vext_pth",
")",
"except",
"IOError",
"as",
"e",
":",
"if",
"e",
".",
"errno",
"==",
"2",
":",
"# file didn't exist == disabled",
"return"
] | Comment any lines that start with import in the .pth file | [
"Comment",
"any",
"lines",
"that",
"start",
"with",
"import",
"in",
"the",
".",
"pth",
"file"
] | fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69 | https://github.com/stuaxo/vext/blob/fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69/vext/cmdline/__init__.py#L67-L99 | train |
stuaxo/vext | vext/cmdline/__init__.py | do_check | def do_check(vext_files):
"""
Attempt to import everything in the 'test-imports' section of specified
vext_files
:param: list of vext filenames (without paths), '*' matches all.
:return: True if test_imports was successful from all files
"""
import vext
# not efficient ... but then there shouldn't be many of these
all_specs = set(vext.gatekeeper.spec_files_flat())
if vext_files == ['*']:
vext_files = all_specs
unknown_specs = set(vext_files) - all_specs
for fn in unknown_specs:
print("%s is not an installed vext file." % fn, file=sys.stderr)
if unknown_specs:
return False
check_passed = True
for fn in [join(vext.gatekeeper.spec_dir(), fn) for fn in vext_files]:
f = open_spec(open(fn))
modules = f.get('test_import', [])
for success, module in vext.gatekeeper.test_imports(modules):
if not success:
check_passed = False
line = "import %s: %s" % (module, '[success]' if success else '[failed]')
print(line)
print('')
return check_passed | python | def do_check(vext_files):
"""
Attempt to import everything in the 'test-imports' section of specified
vext_files
:param: list of vext filenames (without paths), '*' matches all.
:return: True if test_imports was successful from all files
"""
import vext
# not efficient ... but then there shouldn't be many of these
all_specs = set(vext.gatekeeper.spec_files_flat())
if vext_files == ['*']:
vext_files = all_specs
unknown_specs = set(vext_files) - all_specs
for fn in unknown_specs:
print("%s is not an installed vext file." % fn, file=sys.stderr)
if unknown_specs:
return False
check_passed = True
for fn in [join(vext.gatekeeper.spec_dir(), fn) for fn in vext_files]:
f = open_spec(open(fn))
modules = f.get('test_import', [])
for success, module in vext.gatekeeper.test_imports(modules):
if not success:
check_passed = False
line = "import %s: %s" % (module, '[success]' if success else '[failed]')
print(line)
print('')
return check_passed | [
"def",
"do_check",
"(",
"vext_files",
")",
":",
"import",
"vext",
"# not efficient ... but then there shouldn't be many of these",
"all_specs",
"=",
"set",
"(",
"vext",
".",
"gatekeeper",
".",
"spec_files_flat",
"(",
")",
")",
"if",
"vext_files",
"==",
"[",
"'*'",
"]",
":",
"vext_files",
"=",
"all_specs",
"unknown_specs",
"=",
"set",
"(",
"vext_files",
")",
"-",
"all_specs",
"for",
"fn",
"in",
"unknown_specs",
":",
"print",
"(",
"\"%s is not an installed vext file.\"",
"%",
"fn",
",",
"file",
"=",
"sys",
".",
"stderr",
")",
"if",
"unknown_specs",
":",
"return",
"False",
"check_passed",
"=",
"True",
"for",
"fn",
"in",
"[",
"join",
"(",
"vext",
".",
"gatekeeper",
".",
"spec_dir",
"(",
")",
",",
"fn",
")",
"for",
"fn",
"in",
"vext_files",
"]",
":",
"f",
"=",
"open_spec",
"(",
"open",
"(",
"fn",
")",
")",
"modules",
"=",
"f",
".",
"get",
"(",
"'test_import'",
",",
"[",
"]",
")",
"for",
"success",
",",
"module",
"in",
"vext",
".",
"gatekeeper",
".",
"test_imports",
"(",
"modules",
")",
":",
"if",
"not",
"success",
":",
"check_passed",
"=",
"False",
"line",
"=",
"\"import %s: %s\"",
"%",
"(",
"module",
",",
"'[success]'",
"if",
"success",
"else",
"'[failed]'",
")",
"print",
"(",
"line",
")",
"print",
"(",
"''",
")",
"return",
"check_passed"
] | Attempt to import everything in the 'test-imports' section of specified
vext_files
:param: list of vext filenames (without paths), '*' matches all.
:return: True if test_imports was successful from all files | [
"Attempt",
"to",
"import",
"everything",
"in",
"the",
"test",
"-",
"imports",
"section",
"of",
"specified",
"vext_files"
] | fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69 | https://github.com/stuaxo/vext/blob/fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69/vext/cmdline/__init__.py#L116-L148 | train |
stuaxo/vext | vext/gatekeeper/__init__.py | fix_path | def fix_path(p):
"""
Convert path pointing subdirectory of virtualenv site-packages
to system site-packages.
Destination directory must exist for this to work.
>>> fix_path('C:\\some-venv\\Lib\\site-packages\\gnome')
'C:\\Python27\\lib\\site-packages\\gnome'
"""
venv_lib = get_python_lib()
if p.startswith(venv_lib):
subdir = p[len(venv_lib) + 1:]
for sitedir in getsyssitepackages():
fixed_path = join(sitedir, subdir)
if isdir(fixed_path):
return fixed_path
return p | python | def fix_path(p):
"""
Convert path pointing subdirectory of virtualenv site-packages
to system site-packages.
Destination directory must exist for this to work.
>>> fix_path('C:\\some-venv\\Lib\\site-packages\\gnome')
'C:\\Python27\\lib\\site-packages\\gnome'
"""
venv_lib = get_python_lib()
if p.startswith(venv_lib):
subdir = p[len(venv_lib) + 1:]
for sitedir in getsyssitepackages():
fixed_path = join(sitedir, subdir)
if isdir(fixed_path):
return fixed_path
return p | [
"def",
"fix_path",
"(",
"p",
")",
":",
"venv_lib",
"=",
"get_python_lib",
"(",
")",
"if",
"p",
".",
"startswith",
"(",
"venv_lib",
")",
":",
"subdir",
"=",
"p",
"[",
"len",
"(",
"venv_lib",
")",
"+",
"1",
":",
"]",
"for",
"sitedir",
"in",
"getsyssitepackages",
"(",
")",
":",
"fixed_path",
"=",
"join",
"(",
"sitedir",
",",
"subdir",
")",
"if",
"isdir",
"(",
"fixed_path",
")",
":",
"return",
"fixed_path",
"return",
"p"
] | Convert path pointing subdirectory of virtualenv site-packages
to system site-packages.
Destination directory must exist for this to work.
>>> fix_path('C:\\some-venv\\Lib\\site-packages\\gnome')
'C:\\Python27\\lib\\site-packages\\gnome' | [
"Convert",
"path",
"pointing",
"subdirectory",
"of",
"virtualenv",
"site",
"-",
"packages",
"to",
"system",
"site",
"-",
"packages",
"."
] | fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69 | https://github.com/stuaxo/vext/blob/fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69/vext/gatekeeper/__init__.py#L56-L76 | train |
stuaxo/vext | vext/gatekeeper/__init__.py | fixup_paths | def fixup_paths():
"""
Fixup paths added in .pth file that point to the virtualenv
instead of the system site packages.
In depth: .PTH can execute arbitrary code, which might
manipulate the PATH or sys.path
:return:
"""
original_paths = os.environ.get('PATH', "").split(os.path.pathsep)
original_dirs = set(added_dirs)
yield
# Fix PATH environment variable
current_paths = os.environ.get('PATH', "").split(os.path.pathsep)
if original_paths != current_paths:
changed_paths = set(current_paths).difference(set(original_paths))
# rebuild PATH env var
fixed_paths = []
for path in current_paths:
if path in changed_paths:
fixed_paths.append(env_t(fix_path(path)))
else:
fixed_paths.append(env_t(path))
os.environ['PATH'] = os.pathsep.join(fixed_paths)
# Fix added_dirs
if added_dirs != original_dirs:
for path in set(added_dirs.difference(original_dirs)):
fixed_path = fix_path(path)
if fixed_path != path:
print("Fix %s >> %s" % (path, fixed_path))
added_dirs.remove(path)
added_dirs.add(fixed_path)
i = sys.path.index(path) # not efficient... but shouldn't happen often
sys.path[i] = fixed_path
if env_t(fixed_path) not in os.environ['PATH']:
os.environ['PATH'].append(os.pathsep + env_t(fixed_path)) | python | def fixup_paths():
"""
Fixup paths added in .pth file that point to the virtualenv
instead of the system site packages.
In depth: .PTH can execute arbitrary code, which might
manipulate the PATH or sys.path
:return:
"""
original_paths = os.environ.get('PATH', "").split(os.path.pathsep)
original_dirs = set(added_dirs)
yield
# Fix PATH environment variable
current_paths = os.environ.get('PATH', "").split(os.path.pathsep)
if original_paths != current_paths:
changed_paths = set(current_paths).difference(set(original_paths))
# rebuild PATH env var
fixed_paths = []
for path in current_paths:
if path in changed_paths:
fixed_paths.append(env_t(fix_path(path)))
else:
fixed_paths.append(env_t(path))
os.environ['PATH'] = os.pathsep.join(fixed_paths)
# Fix added_dirs
if added_dirs != original_dirs:
for path in set(added_dirs.difference(original_dirs)):
fixed_path = fix_path(path)
if fixed_path != path:
print("Fix %s >> %s" % (path, fixed_path))
added_dirs.remove(path)
added_dirs.add(fixed_path)
i = sys.path.index(path) # not efficient... but shouldn't happen often
sys.path[i] = fixed_path
if env_t(fixed_path) not in os.environ['PATH']:
os.environ['PATH'].append(os.pathsep + env_t(fixed_path)) | [
"def",
"fixup_paths",
"(",
")",
":",
"original_paths",
"=",
"os",
".",
"environ",
".",
"get",
"(",
"'PATH'",
",",
"\"\"",
")",
".",
"split",
"(",
"os",
".",
"path",
".",
"pathsep",
")",
"original_dirs",
"=",
"set",
"(",
"added_dirs",
")",
"yield",
"# Fix PATH environment variable",
"current_paths",
"=",
"os",
".",
"environ",
".",
"get",
"(",
"'PATH'",
",",
"\"\"",
")",
".",
"split",
"(",
"os",
".",
"path",
".",
"pathsep",
")",
"if",
"original_paths",
"!=",
"current_paths",
":",
"changed_paths",
"=",
"set",
"(",
"current_paths",
")",
".",
"difference",
"(",
"set",
"(",
"original_paths",
")",
")",
"# rebuild PATH env var",
"fixed_paths",
"=",
"[",
"]",
"for",
"path",
"in",
"current_paths",
":",
"if",
"path",
"in",
"changed_paths",
":",
"fixed_paths",
".",
"append",
"(",
"env_t",
"(",
"fix_path",
"(",
"path",
")",
")",
")",
"else",
":",
"fixed_paths",
".",
"append",
"(",
"env_t",
"(",
"path",
")",
")",
"os",
".",
"environ",
"[",
"'PATH'",
"]",
"=",
"os",
".",
"pathsep",
".",
"join",
"(",
"fixed_paths",
")",
"# Fix added_dirs",
"if",
"added_dirs",
"!=",
"original_dirs",
":",
"for",
"path",
"in",
"set",
"(",
"added_dirs",
".",
"difference",
"(",
"original_dirs",
")",
")",
":",
"fixed_path",
"=",
"fix_path",
"(",
"path",
")",
"if",
"fixed_path",
"!=",
"path",
":",
"print",
"(",
"\"Fix %s >> %s\"",
"%",
"(",
"path",
",",
"fixed_path",
")",
")",
"added_dirs",
".",
"remove",
"(",
"path",
")",
"added_dirs",
".",
"add",
"(",
"fixed_path",
")",
"i",
"=",
"sys",
".",
"path",
".",
"index",
"(",
"path",
")",
"# not efficient... but shouldn't happen often",
"sys",
".",
"path",
"[",
"i",
"]",
"=",
"fixed_path",
"if",
"env_t",
"(",
"fixed_path",
")",
"not",
"in",
"os",
".",
"environ",
"[",
"'PATH'",
"]",
":",
"os",
".",
"environ",
"[",
"'PATH'",
"]",
".",
"append",
"(",
"os",
".",
"pathsep",
"+",
"env_t",
"(",
"fixed_path",
")",
")"
] | Fixup paths added in .pth file that point to the virtualenv
instead of the system site packages.
In depth: .PTH can execute arbitrary code, which might
manipulate the PATH or sys.path
:return: | [
"Fixup",
"paths",
"added",
"in",
".",
"pth",
"file",
"that",
"point",
"to",
"the",
"virtualenv",
"instead",
"of",
"the",
"system",
"site",
"packages",
"."
] | fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69 | https://github.com/stuaxo/vext/blob/fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69/vext/gatekeeper/__init__.py#L80-L120 | train |
stuaxo/vext | vext/gatekeeper/__init__.py | addpackage | def addpackage(sys_sitedir, pthfile, known_dirs):
"""
Wrapper for site.addpackage
Try and work out which directories are added by
the .pth and add them to the known_dirs set
:param sys_sitedir: system site-packages directory
:param pthfile: path file to add
:param known_dirs: set of known directories
"""
with open(join(sys_sitedir, pthfile)) as f:
for n, line in enumerate(f):
if line.startswith("#"):
continue
line = line.rstrip()
if line:
if line.startswith(("import ", "import\t")):
exec (line, globals(), locals())
continue
else:
p_rel = join(sys_sitedir, line)
p_abs = abspath(line)
if isdir(p_rel):
os.environ['PATH'] += env_t(os.pathsep + p_rel)
sys.path.append(p_rel)
added_dirs.add(p_rel)
elif isdir(p_abs):
os.environ['PATH'] += env_t(os.pathsep + p_abs)
sys.path.append(p_abs)
added_dirs.add(p_abs)
if isfile(pthfile):
site.addpackage(sys_sitedir, pthfile, known_dirs)
else:
logging.debug("pth file '%s' not found") | python | def addpackage(sys_sitedir, pthfile, known_dirs):
"""
Wrapper for site.addpackage
Try and work out which directories are added by
the .pth and add them to the known_dirs set
:param sys_sitedir: system site-packages directory
:param pthfile: path file to add
:param known_dirs: set of known directories
"""
with open(join(sys_sitedir, pthfile)) as f:
for n, line in enumerate(f):
if line.startswith("#"):
continue
line = line.rstrip()
if line:
if line.startswith(("import ", "import\t")):
exec (line, globals(), locals())
continue
else:
p_rel = join(sys_sitedir, line)
p_abs = abspath(line)
if isdir(p_rel):
os.environ['PATH'] += env_t(os.pathsep + p_rel)
sys.path.append(p_rel)
added_dirs.add(p_rel)
elif isdir(p_abs):
os.environ['PATH'] += env_t(os.pathsep + p_abs)
sys.path.append(p_abs)
added_dirs.add(p_abs)
if isfile(pthfile):
site.addpackage(sys_sitedir, pthfile, known_dirs)
else:
logging.debug("pth file '%s' not found") | [
"def",
"addpackage",
"(",
"sys_sitedir",
",",
"pthfile",
",",
"known_dirs",
")",
":",
"with",
"open",
"(",
"join",
"(",
"sys_sitedir",
",",
"pthfile",
")",
")",
"as",
"f",
":",
"for",
"n",
",",
"line",
"in",
"enumerate",
"(",
"f",
")",
":",
"if",
"line",
".",
"startswith",
"(",
"\"#\"",
")",
":",
"continue",
"line",
"=",
"line",
".",
"rstrip",
"(",
")",
"if",
"line",
":",
"if",
"line",
".",
"startswith",
"(",
"(",
"\"import \"",
",",
"\"import\\t\"",
")",
")",
":",
"exec",
"(",
"line",
",",
"globals",
"(",
")",
",",
"locals",
"(",
")",
")",
"continue",
"else",
":",
"p_rel",
"=",
"join",
"(",
"sys_sitedir",
",",
"line",
")",
"p_abs",
"=",
"abspath",
"(",
"line",
")",
"if",
"isdir",
"(",
"p_rel",
")",
":",
"os",
".",
"environ",
"[",
"'PATH'",
"]",
"+=",
"env_t",
"(",
"os",
".",
"pathsep",
"+",
"p_rel",
")",
"sys",
".",
"path",
".",
"append",
"(",
"p_rel",
")",
"added_dirs",
".",
"add",
"(",
"p_rel",
")",
"elif",
"isdir",
"(",
"p_abs",
")",
":",
"os",
".",
"environ",
"[",
"'PATH'",
"]",
"+=",
"env_t",
"(",
"os",
".",
"pathsep",
"+",
"p_abs",
")",
"sys",
".",
"path",
".",
"append",
"(",
"p_abs",
")",
"added_dirs",
".",
"add",
"(",
"p_abs",
")",
"if",
"isfile",
"(",
"pthfile",
")",
":",
"site",
".",
"addpackage",
"(",
"sys_sitedir",
",",
"pthfile",
",",
"known_dirs",
")",
"else",
":",
"logging",
".",
"debug",
"(",
"\"pth file '%s' not found\"",
")"
] | Wrapper for site.addpackage
Try and work out which directories are added by
the .pth and add them to the known_dirs set
:param sys_sitedir: system site-packages directory
:param pthfile: path file to add
:param known_dirs: set of known directories | [
"Wrapper",
"for",
"site",
".",
"addpackage"
] | fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69 | https://github.com/stuaxo/vext/blob/fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69/vext/gatekeeper/__init__.py#L123-L158 | train |
stuaxo/vext | vext/gatekeeper/__init__.py | filename_to_module | def filename_to_module(filename):
"""
convert a filename like html5lib-0.999.egg-info to html5lib
"""
find = re.compile(r"^[^.|-]*")
name = re.search(find, filename).group(0)
return name | python | def filename_to_module(filename):
"""
convert a filename like html5lib-0.999.egg-info to html5lib
"""
find = re.compile(r"^[^.|-]*")
name = re.search(find, filename).group(0)
return name | [
"def",
"filename_to_module",
"(",
"filename",
")",
":",
"find",
"=",
"re",
".",
"compile",
"(",
"r\"^[^.|-]*\"",
")",
"name",
"=",
"re",
".",
"search",
"(",
"find",
",",
"filename",
")",
".",
"group",
"(",
"0",
")",
"return",
"name"
] | convert a filename like html5lib-0.999.egg-info to html5lib | [
"convert",
"a",
"filename",
"like",
"html5lib",
"-",
"0",
".",
"999",
".",
"egg",
"-",
"info",
"to",
"html5lib"
] | fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69 | https://github.com/stuaxo/vext/blob/fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69/vext/gatekeeper/__init__.py#L166-L172 | train |
stuaxo/vext | vext/gatekeeper/__init__.py | init_path | def init_path():
"""
Add any new modules that are directories to the PATH
"""
sitedirs = getsyssitepackages()
for sitedir in sitedirs:
env_path = os.environ['PATH'].split(os.pathsep)
for module in allowed_modules:
p = join(sitedir, module)
if isdir(p) and not p in env_path:
os.environ['PATH'] += env_t(os.pathsep + p) | python | def init_path():
"""
Add any new modules that are directories to the PATH
"""
sitedirs = getsyssitepackages()
for sitedir in sitedirs:
env_path = os.environ['PATH'].split(os.pathsep)
for module in allowed_modules:
p = join(sitedir, module)
if isdir(p) and not p in env_path:
os.environ['PATH'] += env_t(os.pathsep + p) | [
"def",
"init_path",
"(",
")",
":",
"sitedirs",
"=",
"getsyssitepackages",
"(",
")",
"for",
"sitedir",
"in",
"sitedirs",
":",
"env_path",
"=",
"os",
".",
"environ",
"[",
"'PATH'",
"]",
".",
"split",
"(",
"os",
".",
"pathsep",
")",
"for",
"module",
"in",
"allowed_modules",
":",
"p",
"=",
"join",
"(",
"sitedir",
",",
"module",
")",
"if",
"isdir",
"(",
"p",
")",
"and",
"not",
"p",
"in",
"env_path",
":",
"os",
".",
"environ",
"[",
"'PATH'",
"]",
"+=",
"env_t",
"(",
"os",
".",
"pathsep",
"+",
"p",
")"
] | Add any new modules that are directories to the PATH | [
"Add",
"any",
"new",
"modules",
"that",
"are",
"directories",
"to",
"the",
"PATH"
] | fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69 | https://github.com/stuaxo/vext/blob/fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69/vext/gatekeeper/__init__.py#L275-L285 | train |
stuaxo/vext | vext/gatekeeper/__init__.py | install_importer | def install_importer():
"""
If in a virtualenv then load spec files to decide which
modules can be imported from system site-packages and
install path hook.
"""
logging.debug('install_importer')
if not in_venv():
logging.debug('No virtualenv active py:[%s]', sys.executable)
return False
if disable_vext:
logging.debug('Vext disabled by environment variable')
return False
if GatekeeperFinder.PATH_TRIGGER not in sys.path:
try:
load_specs()
sys.path.append(GatekeeperFinder.PATH_TRIGGER)
sys.path_hooks.append(GatekeeperFinder)
except Exception as e:
"""
Dont kill other programmes because of a vext error
"""
logger.info(str(e))
if logger.getEffectiveLevel() == logging.DEBUG:
raise
logging.debug("importer installed")
return True | python | def install_importer():
"""
If in a virtualenv then load spec files to decide which
modules can be imported from system site-packages and
install path hook.
"""
logging.debug('install_importer')
if not in_venv():
logging.debug('No virtualenv active py:[%s]', sys.executable)
return False
if disable_vext:
logging.debug('Vext disabled by environment variable')
return False
if GatekeeperFinder.PATH_TRIGGER not in sys.path:
try:
load_specs()
sys.path.append(GatekeeperFinder.PATH_TRIGGER)
sys.path_hooks.append(GatekeeperFinder)
except Exception as e:
"""
Dont kill other programmes because of a vext error
"""
logger.info(str(e))
if logger.getEffectiveLevel() == logging.DEBUG:
raise
logging.debug("importer installed")
return True | [
"def",
"install_importer",
"(",
")",
":",
"logging",
".",
"debug",
"(",
"'install_importer'",
")",
"if",
"not",
"in_venv",
"(",
")",
":",
"logging",
".",
"debug",
"(",
"'No virtualenv active py:[%s]'",
",",
"sys",
".",
"executable",
")",
"return",
"False",
"if",
"disable_vext",
":",
"logging",
".",
"debug",
"(",
"'Vext disabled by environment variable'",
")",
"return",
"False",
"if",
"GatekeeperFinder",
".",
"PATH_TRIGGER",
"not",
"in",
"sys",
".",
"path",
":",
"try",
":",
"load_specs",
"(",
")",
"sys",
".",
"path",
".",
"append",
"(",
"GatekeeperFinder",
".",
"PATH_TRIGGER",
")",
"sys",
".",
"path_hooks",
".",
"append",
"(",
"GatekeeperFinder",
")",
"except",
"Exception",
"as",
"e",
":",
"\"\"\"\n Dont kill other programmes because of a vext error\n \"\"\"",
"logger",
".",
"info",
"(",
"str",
"(",
"e",
")",
")",
"if",
"logger",
".",
"getEffectiveLevel",
"(",
")",
"==",
"logging",
".",
"DEBUG",
":",
"raise",
"logging",
".",
"debug",
"(",
"\"importer installed\"",
")",
"return",
"True"
] | If in a virtualenv then load spec files to decide which
modules can be imported from system site-packages and
install path hook. | [
"If",
"in",
"a",
"virtualenv",
"then",
"load",
"spec",
"files",
"to",
"decide",
"which",
"modules",
"can",
"be",
"imported",
"from",
"system",
"site",
"-",
"packages",
"and",
"install",
"path",
"hook",
"."
] | fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69 | https://github.com/stuaxo/vext/blob/fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69/vext/gatekeeper/__init__.py#L377-L406 | train |
stuaxo/vext | vext/gatekeeper/__init__.py | GateKeeperLoader.load_module | def load_module(self, name):
"""
Only lets modules in allowed_modules be loaded, others
will get an ImportError
"""
# Get the name relative to SITEDIR ..
filepath = self.module_info[1]
fullname = splitext( \
relpath(filepath, self.sitedir) \
)[0].replace(os.sep, '.')
modulename = filename_to_module(fullname)
if modulename not in allowed_modules:
if remember_blocks:
blocked_imports.add(fullname)
if log_blocks:
raise ImportError("Vext blocked import of '%s'" % modulename)
else:
# Standard error message
raise ImportError("No module named %s" % modulename)
if name not in sys.modules:
try:
logger.debug("load_module %s %s", name, self.module_info)
module = imp.load_module(name, *self.module_info)
except Exception as e:
logger.debug(e)
raise
sys.modules[fullname] = module
return sys.modules[fullname] | python | def load_module(self, name):
"""
Only lets modules in allowed_modules be loaded, others
will get an ImportError
"""
# Get the name relative to SITEDIR ..
filepath = self.module_info[1]
fullname = splitext( \
relpath(filepath, self.sitedir) \
)[0].replace(os.sep, '.')
modulename = filename_to_module(fullname)
if modulename not in allowed_modules:
if remember_blocks:
blocked_imports.add(fullname)
if log_blocks:
raise ImportError("Vext blocked import of '%s'" % modulename)
else:
# Standard error message
raise ImportError("No module named %s" % modulename)
if name not in sys.modules:
try:
logger.debug("load_module %s %s", name, self.module_info)
module = imp.load_module(name, *self.module_info)
except Exception as e:
logger.debug(e)
raise
sys.modules[fullname] = module
return sys.modules[fullname] | [
"def",
"load_module",
"(",
"self",
",",
"name",
")",
":",
"# Get the name relative to SITEDIR ..",
"filepath",
"=",
"self",
".",
"module_info",
"[",
"1",
"]",
"fullname",
"=",
"splitext",
"(",
"relpath",
"(",
"filepath",
",",
"self",
".",
"sitedir",
")",
")",
"[",
"0",
"]",
".",
"replace",
"(",
"os",
".",
"sep",
",",
"'.'",
")",
"modulename",
"=",
"filename_to_module",
"(",
"fullname",
")",
"if",
"modulename",
"not",
"in",
"allowed_modules",
":",
"if",
"remember_blocks",
":",
"blocked_imports",
".",
"add",
"(",
"fullname",
")",
"if",
"log_blocks",
":",
"raise",
"ImportError",
"(",
"\"Vext blocked import of '%s'\"",
"%",
"modulename",
")",
"else",
":",
"# Standard error message",
"raise",
"ImportError",
"(",
"\"No module named %s\"",
"%",
"modulename",
")",
"if",
"name",
"not",
"in",
"sys",
".",
"modules",
":",
"try",
":",
"logger",
".",
"debug",
"(",
"\"load_module %s %s\"",
",",
"name",
",",
"self",
".",
"module_info",
")",
"module",
"=",
"imp",
".",
"load_module",
"(",
"name",
",",
"*",
"self",
".",
"module_info",
")",
"except",
"Exception",
"as",
"e",
":",
"logger",
".",
"debug",
"(",
"e",
")",
"raise",
"sys",
".",
"modules",
"[",
"fullname",
"]",
"=",
"module",
"return",
"sys",
".",
"modules",
"[",
"fullname",
"]"
] | Only lets modules in allowed_modules be loaded, others
will get an ImportError | [
"Only",
"lets",
"modules",
"in",
"allowed_modules",
"be",
"loaded",
"others",
"will",
"get",
"an",
"ImportError"
] | fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69 | https://github.com/stuaxo/vext/blob/fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69/vext/gatekeeper/__init__.py#L184-L215 | train |
stuaxo/vext | vext/helpers/sip.py | extra_paths | def extra_paths():
"""
:return: extra paths
"""
# TODO - this is only tested on Ubuntu for now
# there must be a better way of getting
# the sip directory.
dirs = {}
try:
@vext.env.run_in_syspy
def run(*args):
import sipconfig
config = sipconfig.Configuration()
dirs = {
"sip.default_sip_dir": config.default_sip_dir,
}
return dirs
dirs = run()
return dirs
except ImportError:
return dirs | python | def extra_paths():
"""
:return: extra paths
"""
# TODO - this is only tested on Ubuntu for now
# there must be a better way of getting
# the sip directory.
dirs = {}
try:
@vext.env.run_in_syspy
def run(*args):
import sipconfig
config = sipconfig.Configuration()
dirs = {
"sip.default_sip_dir": config.default_sip_dir,
}
return dirs
dirs = run()
return dirs
except ImportError:
return dirs | [
"def",
"extra_paths",
"(",
")",
":",
"# TODO - this is only tested on Ubuntu for now",
"# there must be a better way of getting",
"# the sip directory.",
"dirs",
"=",
"{",
"}",
"try",
":",
"@",
"vext",
".",
"env",
".",
"run_in_syspy",
"def",
"run",
"(",
"*",
"args",
")",
":",
"import",
"sipconfig",
"config",
"=",
"sipconfig",
".",
"Configuration",
"(",
")",
"dirs",
"=",
"{",
"\"sip.default_sip_dir\"",
":",
"config",
".",
"default_sip_dir",
",",
"}",
"return",
"dirs",
"dirs",
"=",
"run",
"(",
")",
"return",
"dirs",
"except",
"ImportError",
":",
"return",
"dirs"
] | :return: extra paths | [
":",
"return",
":",
"extra",
"paths"
] | fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69 | https://github.com/stuaxo/vext/blob/fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69/vext/helpers/sip.py#L10-L30 | train |
jaysonsantos/python-binary-memcached | bmemcached/utils.py | str_to_bytes | def str_to_bytes(value):
"""
Simply convert a string type to bytes if the value is a string
and is an instance of six.string_types but not of six.binary_type
in python2 struct.pack("<Q") is both string_types and binary_type but
in python3 struct.pack("<Q") is binary_type but not a string_types
:param value:
:param binary:
:return:
"""
if not isinstance(value, six.binary_type) and isinstance(value, six.string_types):
return value.encode()
return value | python | def str_to_bytes(value):
"""
Simply convert a string type to bytes if the value is a string
and is an instance of six.string_types but not of six.binary_type
in python2 struct.pack("<Q") is both string_types and binary_type but
in python3 struct.pack("<Q") is binary_type but not a string_types
:param value:
:param binary:
:return:
"""
if not isinstance(value, six.binary_type) and isinstance(value, six.string_types):
return value.encode()
return value | [
"def",
"str_to_bytes",
"(",
"value",
")",
":",
"if",
"not",
"isinstance",
"(",
"value",
",",
"six",
".",
"binary_type",
")",
"and",
"isinstance",
"(",
"value",
",",
"six",
".",
"string_types",
")",
":",
"return",
"value",
".",
"encode",
"(",
")",
"return",
"value"
] | Simply convert a string type to bytes if the value is a string
and is an instance of six.string_types but not of six.binary_type
in python2 struct.pack("<Q") is both string_types and binary_type but
in python3 struct.pack("<Q") is binary_type but not a string_types
:param value:
:param binary:
:return: | [
"Simply",
"convert",
"a",
"string",
"type",
"to",
"bytes",
"if",
"the",
"value",
"is",
"a",
"string",
"and",
"is",
"an",
"instance",
"of",
"six",
".",
"string_types",
"but",
"not",
"of",
"six",
".",
"binary_type",
"in",
"python2",
"struct",
".",
"pack",
"(",
"<Q",
")",
"is",
"both",
"string_types",
"and",
"binary_type",
"but",
"in",
"python3",
"struct",
".",
"pack",
"(",
"<Q",
")",
"is",
"binary_type",
"but",
"not",
"a",
"string_types",
":",
"param",
"value",
":",
":",
"param",
"binary",
":",
":",
"return",
":"
] | 6a792829349c69204d9c5045e5c34b4231216dd6 | https://github.com/jaysonsantos/python-binary-memcached/blob/6a792829349c69204d9c5045e5c34b4231216dd6/bmemcached/utils.py#L6-L18 | train |
hsolbrig/PyShEx | pyshex/evaluate.py | evaluate | def evaluate(g: Graph,
schema: Union[str, ShExJ.Schema],
focus: Optional[Union[str, URIRef, IRIREF]],
start: Optional[Union[str, URIRef, IRIREF, START, START_TYPE]]=None,
debug_trace: bool = False) -> Tuple[bool, Optional[str]]:
""" Evaluate focus node `focus` in graph `g` against shape `shape` in ShEx schema `schema`
:param g: Graph containing RDF
:param schema: ShEx Schema -- if str, it will be parsed
:param focus: focus node in g. If not specified, all URI subjects in G will be evaluated.
:param start: Starting shape. If omitted, the Schema start shape is used
:param debug_trace: Turn on debug tracing
:return: None if success or failure reason if failure
"""
if isinstance(schema, str):
schema = SchemaLoader().loads(schema)
if schema is None:
return False, "Error parsing schema"
if not isinstance(focus, URIRef):
focus = URIRef(str(focus))
if start is None:
start = str(schema.start) if schema.start else None
if start is None:
return False, "No starting shape"
if not isinstance(start, IRIREF) and start is not START and start is not START_TYPE:
start = IRIREF(str(start))
cntxt = Context(g, schema)
cntxt.debug_context.debug = debug_trace
map_ = FixedShapeMap()
map_.add(ShapeAssociation(focus, start))
test_result, reasons = isValid(cntxt, map_)
return test_result, '\n'.join(reasons) | python | def evaluate(g: Graph,
schema: Union[str, ShExJ.Schema],
focus: Optional[Union[str, URIRef, IRIREF]],
start: Optional[Union[str, URIRef, IRIREF, START, START_TYPE]]=None,
debug_trace: bool = False) -> Tuple[bool, Optional[str]]:
""" Evaluate focus node `focus` in graph `g` against shape `shape` in ShEx schema `schema`
:param g: Graph containing RDF
:param schema: ShEx Schema -- if str, it will be parsed
:param focus: focus node in g. If not specified, all URI subjects in G will be evaluated.
:param start: Starting shape. If omitted, the Schema start shape is used
:param debug_trace: Turn on debug tracing
:return: None if success or failure reason if failure
"""
if isinstance(schema, str):
schema = SchemaLoader().loads(schema)
if schema is None:
return False, "Error parsing schema"
if not isinstance(focus, URIRef):
focus = URIRef(str(focus))
if start is None:
start = str(schema.start) if schema.start else None
if start is None:
return False, "No starting shape"
if not isinstance(start, IRIREF) and start is not START and start is not START_TYPE:
start = IRIREF(str(start))
cntxt = Context(g, schema)
cntxt.debug_context.debug = debug_trace
map_ = FixedShapeMap()
map_.add(ShapeAssociation(focus, start))
test_result, reasons = isValid(cntxt, map_)
return test_result, '\n'.join(reasons) | [
"def",
"evaluate",
"(",
"g",
":",
"Graph",
",",
"schema",
":",
"Union",
"[",
"str",
",",
"ShExJ",
".",
"Schema",
"]",
",",
"focus",
":",
"Optional",
"[",
"Union",
"[",
"str",
",",
"URIRef",
",",
"IRIREF",
"]",
"]",
",",
"start",
":",
"Optional",
"[",
"Union",
"[",
"str",
",",
"URIRef",
",",
"IRIREF",
",",
"START",
",",
"START_TYPE",
"]",
"]",
"=",
"None",
",",
"debug_trace",
":",
"bool",
"=",
"False",
")",
"->",
"Tuple",
"[",
"bool",
",",
"Optional",
"[",
"str",
"]",
"]",
":",
"if",
"isinstance",
"(",
"schema",
",",
"str",
")",
":",
"schema",
"=",
"SchemaLoader",
"(",
")",
".",
"loads",
"(",
"schema",
")",
"if",
"schema",
"is",
"None",
":",
"return",
"False",
",",
"\"Error parsing schema\"",
"if",
"not",
"isinstance",
"(",
"focus",
",",
"URIRef",
")",
":",
"focus",
"=",
"URIRef",
"(",
"str",
"(",
"focus",
")",
")",
"if",
"start",
"is",
"None",
":",
"start",
"=",
"str",
"(",
"schema",
".",
"start",
")",
"if",
"schema",
".",
"start",
"else",
"None",
"if",
"start",
"is",
"None",
":",
"return",
"False",
",",
"\"No starting shape\"",
"if",
"not",
"isinstance",
"(",
"start",
",",
"IRIREF",
")",
"and",
"start",
"is",
"not",
"START",
"and",
"start",
"is",
"not",
"START_TYPE",
":",
"start",
"=",
"IRIREF",
"(",
"str",
"(",
"start",
")",
")",
"cntxt",
"=",
"Context",
"(",
"g",
",",
"schema",
")",
"cntxt",
".",
"debug_context",
".",
"debug",
"=",
"debug_trace",
"map_",
"=",
"FixedShapeMap",
"(",
")",
"map_",
".",
"add",
"(",
"ShapeAssociation",
"(",
"focus",
",",
"start",
")",
")",
"test_result",
",",
"reasons",
"=",
"isValid",
"(",
"cntxt",
",",
"map_",
")",
"return",
"test_result",
",",
"'\\n'",
".",
"join",
"(",
"reasons",
")"
] | Evaluate focus node `focus` in graph `g` against shape `shape` in ShEx schema `schema`
:param g: Graph containing RDF
:param schema: ShEx Schema -- if str, it will be parsed
:param focus: focus node in g. If not specified, all URI subjects in G will be evaluated.
:param start: Starting shape. If omitted, the Schema start shape is used
:param debug_trace: Turn on debug tracing
:return: None if success or failure reason if failure | [
"Evaluate",
"focus",
"node",
"focus",
"in",
"graph",
"g",
"against",
"shape",
"shape",
"in",
"ShEx",
"schema",
"schema"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/evaluate.py#L14-L45 | train |
hsolbrig/PyShEx | pyshex/shape_expressions_language/p5_2_validation_definition.py | isValid | def isValid(cntxt: Context, m: FixedShapeMap) -> Tuple[bool, List[str]]:
"""`5.2 Validation Definition <http://shex.io/shex-semantics/#validation>`_
The expression isValid(G, m) indicates that for every nodeSelector/shapeLabel pair (n, s) in m, s has a
corresponding shape expression se and satisfies(n, se, G, m). satisfies is defined below for each form
of shape expression
:param cntxt: evaluation context - includes graph and schema
:param m: list of NodeShape pairs to test
:return: Success/failure indicator and, if fail, a list of failure reasons
"""
if not cntxt.is_valid:
return False, cntxt.error_list
parse_nodes = []
for nodeshapepair in m:
n = nodeshapepair.nodeSelector
if not isinstance_(n, Node):
return False, [f"{n}: Triple patterns are not implemented"]
# The third test below is because the spec asserts that completely empty graphs pass in certain circumstances
elif not (next(cntxt.graph.predicate_objects(nodeshapepair.nodeSelector), None) or
next(cntxt.graph.subject_predicates(nodeshapepair.nodeSelector), None) or
not next(cntxt.graph.triples((None, None, None)), None)):
return False, [f"Focus: {nodeshapepair.nodeSelector} not in graph"]
else:
s = cntxt.shapeExprFor(START if nodeshapepair.shapeLabel is None or nodeshapepair.shapeLabel is START
else nodeshapepair.shapeLabel)
cntxt.current_node = ParseNode(satisfies, s, n, cntxt)
if not s:
if nodeshapepair.shapeLabel is START or nodeshapepair.shapeLabel is None:
cntxt.fail_reason = "START node is not specified or is invalid"
else:
cntxt.fail_reason = f"Shape: {nodeshapepair.shapeLabel} not found in Schema"
return False, cntxt.process_reasons()
parse_nodes.append(cntxt.current_node)
if not satisfies(cntxt, n, s):
cntxt.current_node.result = False
return False, cntxt.process_reasons()
else:
cntxt.current_node.result = True
return True, [] | python | def isValid(cntxt: Context, m: FixedShapeMap) -> Tuple[bool, List[str]]:
"""`5.2 Validation Definition <http://shex.io/shex-semantics/#validation>`_
The expression isValid(G, m) indicates that for every nodeSelector/shapeLabel pair (n, s) in m, s has a
corresponding shape expression se and satisfies(n, se, G, m). satisfies is defined below for each form
of shape expression
:param cntxt: evaluation context - includes graph and schema
:param m: list of NodeShape pairs to test
:return: Success/failure indicator and, if fail, a list of failure reasons
"""
if not cntxt.is_valid:
return False, cntxt.error_list
parse_nodes = []
for nodeshapepair in m:
n = nodeshapepair.nodeSelector
if not isinstance_(n, Node):
return False, [f"{n}: Triple patterns are not implemented"]
# The third test below is because the spec asserts that completely empty graphs pass in certain circumstances
elif not (next(cntxt.graph.predicate_objects(nodeshapepair.nodeSelector), None) or
next(cntxt.graph.subject_predicates(nodeshapepair.nodeSelector), None) or
not next(cntxt.graph.triples((None, None, None)), None)):
return False, [f"Focus: {nodeshapepair.nodeSelector} not in graph"]
else:
s = cntxt.shapeExprFor(START if nodeshapepair.shapeLabel is None or nodeshapepair.shapeLabel is START
else nodeshapepair.shapeLabel)
cntxt.current_node = ParseNode(satisfies, s, n, cntxt)
if not s:
if nodeshapepair.shapeLabel is START or nodeshapepair.shapeLabel is None:
cntxt.fail_reason = "START node is not specified or is invalid"
else:
cntxt.fail_reason = f"Shape: {nodeshapepair.shapeLabel} not found in Schema"
return False, cntxt.process_reasons()
parse_nodes.append(cntxt.current_node)
if not satisfies(cntxt, n, s):
cntxt.current_node.result = False
return False, cntxt.process_reasons()
else:
cntxt.current_node.result = True
return True, [] | [
"def",
"isValid",
"(",
"cntxt",
":",
"Context",
",",
"m",
":",
"FixedShapeMap",
")",
"->",
"Tuple",
"[",
"bool",
",",
"List",
"[",
"str",
"]",
"]",
":",
"if",
"not",
"cntxt",
".",
"is_valid",
":",
"return",
"False",
",",
"cntxt",
".",
"error_list",
"parse_nodes",
"=",
"[",
"]",
"for",
"nodeshapepair",
"in",
"m",
":",
"n",
"=",
"nodeshapepair",
".",
"nodeSelector",
"if",
"not",
"isinstance_",
"(",
"n",
",",
"Node",
")",
":",
"return",
"False",
",",
"[",
"f\"{n}: Triple patterns are not implemented\"",
"]",
"# The third test below is because the spec asserts that completely empty graphs pass in certain circumstances",
"elif",
"not",
"(",
"next",
"(",
"cntxt",
".",
"graph",
".",
"predicate_objects",
"(",
"nodeshapepair",
".",
"nodeSelector",
")",
",",
"None",
")",
"or",
"next",
"(",
"cntxt",
".",
"graph",
".",
"subject_predicates",
"(",
"nodeshapepair",
".",
"nodeSelector",
")",
",",
"None",
")",
"or",
"not",
"next",
"(",
"cntxt",
".",
"graph",
".",
"triples",
"(",
"(",
"None",
",",
"None",
",",
"None",
")",
")",
",",
"None",
")",
")",
":",
"return",
"False",
",",
"[",
"f\"Focus: {nodeshapepair.nodeSelector} not in graph\"",
"]",
"else",
":",
"s",
"=",
"cntxt",
".",
"shapeExprFor",
"(",
"START",
"if",
"nodeshapepair",
".",
"shapeLabel",
"is",
"None",
"or",
"nodeshapepair",
".",
"shapeLabel",
"is",
"START",
"else",
"nodeshapepair",
".",
"shapeLabel",
")",
"cntxt",
".",
"current_node",
"=",
"ParseNode",
"(",
"satisfies",
",",
"s",
",",
"n",
",",
"cntxt",
")",
"if",
"not",
"s",
":",
"if",
"nodeshapepair",
".",
"shapeLabel",
"is",
"START",
"or",
"nodeshapepair",
".",
"shapeLabel",
"is",
"None",
":",
"cntxt",
".",
"fail_reason",
"=",
"\"START node is not specified or is invalid\"",
"else",
":",
"cntxt",
".",
"fail_reason",
"=",
"f\"Shape: {nodeshapepair.shapeLabel} not found in Schema\"",
"return",
"False",
",",
"cntxt",
".",
"process_reasons",
"(",
")",
"parse_nodes",
".",
"append",
"(",
"cntxt",
".",
"current_node",
")",
"if",
"not",
"satisfies",
"(",
"cntxt",
",",
"n",
",",
"s",
")",
":",
"cntxt",
".",
"current_node",
".",
"result",
"=",
"False",
"return",
"False",
",",
"cntxt",
".",
"process_reasons",
"(",
")",
"else",
":",
"cntxt",
".",
"current_node",
".",
"result",
"=",
"True",
"return",
"True",
",",
"[",
"]"
] | `5.2 Validation Definition <http://shex.io/shex-semantics/#validation>`_
The expression isValid(G, m) indicates that for every nodeSelector/shapeLabel pair (n, s) in m, s has a
corresponding shape expression se and satisfies(n, se, G, m). satisfies is defined below for each form
of shape expression
:param cntxt: evaluation context - includes graph and schema
:param m: list of NodeShape pairs to test
:return: Success/failure indicator and, if fail, a list of failure reasons | [
"5",
".",
"2",
"Validation",
"Definition",
"<http",
":",
"//",
"shex",
".",
"io",
"/",
"shex",
"-",
"semantics",
"/",
"#validation",
">",
"_"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/shape_expressions_language/p5_2_validation_definition.py#L14-L53 | train |
stuaxo/vext | vext/install/__init__.py | check_sysdeps | def check_sysdeps(vext_files):
"""
Check that imports in 'test_imports' succeed
otherwise display message in 'install_hints'
"""
@run_in_syspy
def run(*modules):
result = {}
for m in modules:
if m:
try:
__import__(m)
result[m] = True
except ImportError:
result[m] = False
return result
success = True
for vext_file in vext_files:
with open(vext_file) as f:
vext = open_spec(f)
install_hint = " ".join(vext.get('install_hints', ['System dependencies not found']))
modules = vext.get('test_import', '')
logger.debug("%s test imports of: %s", vext_file, modules)
result = run(*modules)
if logging.getLogger().getEffectiveLevel() == logging.DEBUG:
for k, v in result.items():
logger.debug("%s: %s", k, v)
if not all(result.values()):
success = False
print(install_hint)
return success | python | def check_sysdeps(vext_files):
"""
Check that imports in 'test_imports' succeed
otherwise display message in 'install_hints'
"""
@run_in_syspy
def run(*modules):
result = {}
for m in modules:
if m:
try:
__import__(m)
result[m] = True
except ImportError:
result[m] = False
return result
success = True
for vext_file in vext_files:
with open(vext_file) as f:
vext = open_spec(f)
install_hint = " ".join(vext.get('install_hints', ['System dependencies not found']))
modules = vext.get('test_import', '')
logger.debug("%s test imports of: %s", vext_file, modules)
result = run(*modules)
if logging.getLogger().getEffectiveLevel() == logging.DEBUG:
for k, v in result.items():
logger.debug("%s: %s", k, v)
if not all(result.values()):
success = False
print(install_hint)
return success | [
"def",
"check_sysdeps",
"(",
"vext_files",
")",
":",
"@",
"run_in_syspy",
"def",
"run",
"(",
"*",
"modules",
")",
":",
"result",
"=",
"{",
"}",
"for",
"m",
"in",
"modules",
":",
"if",
"m",
":",
"try",
":",
"__import__",
"(",
"m",
")",
"result",
"[",
"m",
"]",
"=",
"True",
"except",
"ImportError",
":",
"result",
"[",
"m",
"]",
"=",
"False",
"return",
"result",
"success",
"=",
"True",
"for",
"vext_file",
"in",
"vext_files",
":",
"with",
"open",
"(",
"vext_file",
")",
"as",
"f",
":",
"vext",
"=",
"open_spec",
"(",
"f",
")",
"install_hint",
"=",
"\" \"",
".",
"join",
"(",
"vext",
".",
"get",
"(",
"'install_hints'",
",",
"[",
"'System dependencies not found'",
"]",
")",
")",
"modules",
"=",
"vext",
".",
"get",
"(",
"'test_import'",
",",
"''",
")",
"logger",
".",
"debug",
"(",
"\"%s test imports of: %s\"",
",",
"vext_file",
",",
"modules",
")",
"result",
"=",
"run",
"(",
"*",
"modules",
")",
"if",
"logging",
".",
"getLogger",
"(",
")",
".",
"getEffectiveLevel",
"(",
")",
"==",
"logging",
".",
"DEBUG",
":",
"for",
"k",
",",
"v",
"in",
"result",
".",
"items",
"(",
")",
":",
"logger",
".",
"debug",
"(",
"\"%s: %s\"",
",",
"k",
",",
"v",
")",
"if",
"not",
"all",
"(",
"result",
".",
"values",
"(",
")",
")",
":",
"success",
"=",
"False",
"print",
"(",
"install_hint",
")",
"return",
"success"
] | Check that imports in 'test_imports' succeed
otherwise display message in 'install_hints' | [
"Check",
"that",
"imports",
"in",
"test_imports",
"succeed",
"otherwise",
"display",
"message",
"in",
"install_hints"
] | fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69 | https://github.com/stuaxo/vext/blob/fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69/vext/install/__init__.py#L24-L57 | train |
stuaxo/vext | vext/install/__init__.py | install_vexts | def install_vexts(vext_files, verify=True):
"""
copy vext_file to sys.prefix + '/share/vext/specs'
(PIP7 seems to remove data_files so we recreate something similar here)
"""
if verify and not check_sysdeps(vext_files):
return
spec_dir = join(prefix, 'share/vext/specs')
try:
makedirs(spec_dir)
except OSError as e:
if not isdir(spec_dir):
logger.error("Error making spec directory [%s]: %r" % (spec_dir, e))
for vext_file in vext_files:
dest = normpath(join(spec_dir, basename(vext_file)))
try:
logger.debug("%s > %s" % (vext_file, dest))
copyfile(vext_file, dest)
yield vext_file, dest
except IOError as e:
logger.error("Could not copy %s %r" % (vext_file, e)) | python | def install_vexts(vext_files, verify=True):
"""
copy vext_file to sys.prefix + '/share/vext/specs'
(PIP7 seems to remove data_files so we recreate something similar here)
"""
if verify and not check_sysdeps(vext_files):
return
spec_dir = join(prefix, 'share/vext/specs')
try:
makedirs(spec_dir)
except OSError as e:
if not isdir(spec_dir):
logger.error("Error making spec directory [%s]: %r" % (spec_dir, e))
for vext_file in vext_files:
dest = normpath(join(spec_dir, basename(vext_file)))
try:
logger.debug("%s > %s" % (vext_file, dest))
copyfile(vext_file, dest)
yield vext_file, dest
except IOError as e:
logger.error("Could not copy %s %r" % (vext_file, e)) | [
"def",
"install_vexts",
"(",
"vext_files",
",",
"verify",
"=",
"True",
")",
":",
"if",
"verify",
"and",
"not",
"check_sysdeps",
"(",
"vext_files",
")",
":",
"return",
"spec_dir",
"=",
"join",
"(",
"prefix",
",",
"'share/vext/specs'",
")",
"try",
":",
"makedirs",
"(",
"spec_dir",
")",
"except",
"OSError",
"as",
"e",
":",
"if",
"not",
"isdir",
"(",
"spec_dir",
")",
":",
"logger",
".",
"error",
"(",
"\"Error making spec directory [%s]: %r\"",
"%",
"(",
"spec_dir",
",",
"e",
")",
")",
"for",
"vext_file",
"in",
"vext_files",
":",
"dest",
"=",
"normpath",
"(",
"join",
"(",
"spec_dir",
",",
"basename",
"(",
"vext_file",
")",
")",
")",
"try",
":",
"logger",
".",
"debug",
"(",
"\"%s > %s\"",
"%",
"(",
"vext_file",
",",
"dest",
")",
")",
"copyfile",
"(",
"vext_file",
",",
"dest",
")",
"yield",
"vext_file",
",",
"dest",
"except",
"IOError",
"as",
"e",
":",
"logger",
".",
"error",
"(",
"\"Could not copy %s %r\"",
"%",
"(",
"vext_file",
",",
"e",
")",
")"
] | copy vext_file to sys.prefix + '/share/vext/specs'
(PIP7 seems to remove data_files so we recreate something similar here) | [
"copy",
"vext_file",
"to",
"sys",
".",
"prefix",
"+",
"/",
"share",
"/",
"vext",
"/",
"specs"
] | fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69 | https://github.com/stuaxo/vext/blob/fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69/vext/install/__init__.py#L60-L83 | train |
stuaxo/vext | vext/install/__init__.py | create_pth | def create_pth():
"""
Create the default PTH file
:return:
"""
if prefix == '/usr':
print("Not creating PTH in real prefix: %s" % prefix)
return False
with open(vext_pth, 'w') as f:
f.write(DEFAULT_PTH_CONTENT)
return True | python | def create_pth():
"""
Create the default PTH file
:return:
"""
if prefix == '/usr':
print("Not creating PTH in real prefix: %s" % prefix)
return False
with open(vext_pth, 'w') as f:
f.write(DEFAULT_PTH_CONTENT)
return True | [
"def",
"create_pth",
"(",
")",
":",
"if",
"prefix",
"==",
"'/usr'",
":",
"print",
"(",
"\"Not creating PTH in real prefix: %s\"",
"%",
"prefix",
")",
"return",
"False",
"with",
"open",
"(",
"vext_pth",
",",
"'w'",
")",
"as",
"f",
":",
"f",
".",
"write",
"(",
"DEFAULT_PTH_CONTENT",
")",
"return",
"True"
] | Create the default PTH file
:return: | [
"Create",
"the",
"default",
"PTH",
"file",
":",
"return",
":"
] | fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69 | https://github.com/stuaxo/vext/blob/fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69/vext/install/__init__.py#L86-L96 | train |
hsolbrig/PyShEx | pyshex/utils/collection_utils.py | format_collection | def format_collection(g: Graph, subj: Union[URIRef, BNode], max_entries: int = None, nentries: int = 0) -> Optional[List[str]]:
"""
Return the turtle representation of subj as a collection
:param g: Graph containing subj
:param subj: subject of list
:param max_entries: maximum number of list elements to return, None means all
:param nentries: used for recursion
:return: List of formatted entries if subj heads a well formed collection else None
"""
if subj == RDF.nil:
return [')']
if max_entries is not None and nentries >= max_entries:
return [' ...', ')']
cadr = cdr = None
for p, o in g.predicate_objects(subj):
if p == RDF.first and cadr is None:
cadr = o
elif p == RDF.rest and cdr is None:
cdr = o
else:
return None
# technically this can't happen but it doesn't hurt to address it
if cadr == RDF.nil and cdr is None:
return []
elif cadr is not None and cdr is not None:
return [(' ' if nentries else '(') + cadr.n3(g.namespace_manager)] + format_collection(g, cdr, max_entries,
nentries+1)
else:
return None | python | def format_collection(g: Graph, subj: Union[URIRef, BNode], max_entries: int = None, nentries: int = 0) -> Optional[List[str]]:
"""
Return the turtle representation of subj as a collection
:param g: Graph containing subj
:param subj: subject of list
:param max_entries: maximum number of list elements to return, None means all
:param nentries: used for recursion
:return: List of formatted entries if subj heads a well formed collection else None
"""
if subj == RDF.nil:
return [')']
if max_entries is not None and nentries >= max_entries:
return [' ...', ')']
cadr = cdr = None
for p, o in g.predicate_objects(subj):
if p == RDF.first and cadr is None:
cadr = o
elif p == RDF.rest and cdr is None:
cdr = o
else:
return None
# technically this can't happen but it doesn't hurt to address it
if cadr == RDF.nil and cdr is None:
return []
elif cadr is not None and cdr is not None:
return [(' ' if nentries else '(') + cadr.n3(g.namespace_manager)] + format_collection(g, cdr, max_entries,
nentries+1)
else:
return None | [
"def",
"format_collection",
"(",
"g",
":",
"Graph",
",",
"subj",
":",
"Union",
"[",
"URIRef",
",",
"BNode",
"]",
",",
"max_entries",
":",
"int",
"=",
"None",
",",
"nentries",
":",
"int",
"=",
"0",
")",
"->",
"Optional",
"[",
"List",
"[",
"str",
"]",
"]",
":",
"if",
"subj",
"==",
"RDF",
".",
"nil",
":",
"return",
"[",
"')'",
"]",
"if",
"max_entries",
"is",
"not",
"None",
"and",
"nentries",
">=",
"max_entries",
":",
"return",
"[",
"' ...'",
",",
"')'",
"]",
"cadr",
"=",
"cdr",
"=",
"None",
"for",
"p",
",",
"o",
"in",
"g",
".",
"predicate_objects",
"(",
"subj",
")",
":",
"if",
"p",
"==",
"RDF",
".",
"first",
"and",
"cadr",
"is",
"None",
":",
"cadr",
"=",
"o",
"elif",
"p",
"==",
"RDF",
".",
"rest",
"and",
"cdr",
"is",
"None",
":",
"cdr",
"=",
"o",
"else",
":",
"return",
"None",
"# technically this can't happen but it doesn't hurt to address it",
"if",
"cadr",
"==",
"RDF",
".",
"nil",
"and",
"cdr",
"is",
"None",
":",
"return",
"[",
"]",
"elif",
"cadr",
"is",
"not",
"None",
"and",
"cdr",
"is",
"not",
"None",
":",
"return",
"[",
"(",
"' '",
"if",
"nentries",
"else",
"'('",
")",
"+",
"cadr",
".",
"n3",
"(",
"g",
".",
"namespace_manager",
")",
"]",
"+",
"format_collection",
"(",
"g",
",",
"cdr",
",",
"max_entries",
",",
"nentries",
"+",
"1",
")",
"else",
":",
"return",
"None"
] | Return the turtle representation of subj as a collection
:param g: Graph containing subj
:param subj: subject of list
:param max_entries: maximum number of list elements to return, None means all
:param nentries: used for recursion
:return: List of formatted entries if subj heads a well formed collection else None | [
"Return",
"the",
"turtle",
"representation",
"of",
"subj",
"as",
"a",
"collection"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/utils/collection_utils.py#L6-L36 | train |
stuaxo/vext | vext/helpers/__init__.py | get_extra_path | def get_extra_path(name):
"""
:param name: name in format helper.path_name
sip.default_sip_dir
"""
# Paths are cached in path_cache
helper_name, _, key = name.partition(".")
helper = path_helpers.get(helper_name)
if not helper:
raise ValueError("Helper '{0}' not found.".format(helper))
if name not in path_cache:
extra_paths = helper.extra_paths()
path_cache.update(extra_paths)
extra_path = path_cache.get(name)
if not extra_path:
raise ValueError("Helper '{0}' has no path called {1}".format(helper_name, name))
return extra_path | python | def get_extra_path(name):
"""
:param name: name in format helper.path_name
sip.default_sip_dir
"""
# Paths are cached in path_cache
helper_name, _, key = name.partition(".")
helper = path_helpers.get(helper_name)
if not helper:
raise ValueError("Helper '{0}' not found.".format(helper))
if name not in path_cache:
extra_paths = helper.extra_paths()
path_cache.update(extra_paths)
extra_path = path_cache.get(name)
if not extra_path:
raise ValueError("Helper '{0}' has no path called {1}".format(helper_name, name))
return extra_path | [
"def",
"get_extra_path",
"(",
"name",
")",
":",
"# Paths are cached in path_cache",
"helper_name",
",",
"_",
",",
"key",
"=",
"name",
".",
"partition",
"(",
"\".\"",
")",
"helper",
"=",
"path_helpers",
".",
"get",
"(",
"helper_name",
")",
"if",
"not",
"helper",
":",
"raise",
"ValueError",
"(",
"\"Helper '{0}' not found.\"",
".",
"format",
"(",
"helper",
")",
")",
"if",
"name",
"not",
"in",
"path_cache",
":",
"extra_paths",
"=",
"helper",
".",
"extra_paths",
"(",
")",
"path_cache",
".",
"update",
"(",
"extra_paths",
")",
"extra_path",
"=",
"path_cache",
".",
"get",
"(",
"name",
")",
"if",
"not",
"extra_path",
":",
"raise",
"ValueError",
"(",
"\"Helper '{0}' has no path called {1}\"",
".",
"format",
"(",
"helper_name",
",",
"name",
")",
")",
"return",
"extra_path"
] | :param name: name in format helper.path_name
sip.default_sip_dir | [
":",
"param",
"name",
":",
"name",
"in",
"format",
"helper",
".",
"path_name"
] | fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69 | https://github.com/stuaxo/vext/blob/fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69/vext/helpers/__init__.py#L12-L29 | train |
stuaxo/vext | setup.py | upgrade_setuptools | def upgrade_setuptools():
"""
setuptools 12.2 can trigger a really nasty bug
that eats all memory, so upgrade it to
18.8, which is known to be good.
"""
# Note - I tried including the higher version in
# setup_requires, but was still able to trigger
# the bug. - stu.axon
global MIN_SETUPTOOLS
r = None
try:
r = pkg_resources.require(["setuptools"])[0]
except DistributionNotFound:
# ok, setuptools will be installed later
return
if StrictVersion(r.version) >= StrictVersion(MIN_SETUPTOOLS):
return
else:
print("Upgrading setuptools...")
subprocess.call("%s -mpip install 'setuptools>=%s'" % (sys.executable, MIN_SETUPTOOLS), shell=True) | python | def upgrade_setuptools():
"""
setuptools 12.2 can trigger a really nasty bug
that eats all memory, so upgrade it to
18.8, which is known to be good.
"""
# Note - I tried including the higher version in
# setup_requires, but was still able to trigger
# the bug. - stu.axon
global MIN_SETUPTOOLS
r = None
try:
r = pkg_resources.require(["setuptools"])[0]
except DistributionNotFound:
# ok, setuptools will be installed later
return
if StrictVersion(r.version) >= StrictVersion(MIN_SETUPTOOLS):
return
else:
print("Upgrading setuptools...")
subprocess.call("%s -mpip install 'setuptools>=%s'" % (sys.executable, MIN_SETUPTOOLS), shell=True) | [
"def",
"upgrade_setuptools",
"(",
")",
":",
"# Note - I tried including the higher version in",
"# setup_requires, but was still able to trigger",
"# the bug. - stu.axon",
"global",
"MIN_SETUPTOOLS",
"r",
"=",
"None",
"try",
":",
"r",
"=",
"pkg_resources",
".",
"require",
"(",
"[",
"\"setuptools\"",
"]",
")",
"[",
"0",
"]",
"except",
"DistributionNotFound",
":",
"# ok, setuptools will be installed later",
"return",
"if",
"StrictVersion",
"(",
"r",
".",
"version",
")",
">=",
"StrictVersion",
"(",
"MIN_SETUPTOOLS",
")",
":",
"return",
"else",
":",
"print",
"(",
"\"Upgrading setuptools...\"",
")",
"subprocess",
".",
"call",
"(",
"\"%s -mpip install 'setuptools>=%s'\"",
"%",
"(",
"sys",
".",
"executable",
",",
"MIN_SETUPTOOLS",
")",
",",
"shell",
"=",
"True",
")"
] | setuptools 12.2 can trigger a really nasty bug
that eats all memory, so upgrade it to
18.8, which is known to be good. | [
"setuptools",
"12",
".",
"2",
"can",
"trigger",
"a",
"really",
"nasty",
"bug",
"that",
"eats",
"all",
"memory",
"so",
"upgrade",
"it",
"to",
"18",
".",
"8",
"which",
"is",
"known",
"to",
"be",
"good",
"."
] | fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69 | https://github.com/stuaxo/vext/blob/fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69/setup.py#L28-L49 | train |
stuaxo/vext | setup.py | InstallLib.installed_packages | def installed_packages(self):
""" :return: list of installed packages """
packages = []
CMDLINE = [sys.executable, "-mpip", "freeze"]
try:
for package in subprocess.check_output(CMDLINE) \
.decode('utf-8'). \
splitlines():
for comparator in ["==", ">=", "<=", "<", ">"]:
if comparator in package:
# installed package names usually look like Pillow==2.8.1
# ignore others, like external packages that pip show
# won't understand
name = package.partition(comparator)[0]
packages.append(name)
except RuntimeError as e:
if logger.isEnabledFor(logging.DEBUG):
logger.debug("Exception checking existing packages.")
logger.debug("cmdline: %s", CMDLINE)
ex_type, ex, tb = sys.exc_info()
traceback.print_tb(tb)
logger.debug()
return packages | python | def installed_packages(self):
""" :return: list of installed packages """
packages = []
CMDLINE = [sys.executable, "-mpip", "freeze"]
try:
for package in subprocess.check_output(CMDLINE) \
.decode('utf-8'). \
splitlines():
for comparator in ["==", ">=", "<=", "<", ">"]:
if comparator in package:
# installed package names usually look like Pillow==2.8.1
# ignore others, like external packages that pip show
# won't understand
name = package.partition(comparator)[0]
packages.append(name)
except RuntimeError as e:
if logger.isEnabledFor(logging.DEBUG):
logger.debug("Exception checking existing packages.")
logger.debug("cmdline: %s", CMDLINE)
ex_type, ex, tb = sys.exc_info()
traceback.print_tb(tb)
logger.debug()
return packages | [
"def",
"installed_packages",
"(",
"self",
")",
":",
"packages",
"=",
"[",
"]",
"CMDLINE",
"=",
"[",
"sys",
".",
"executable",
",",
"\"-mpip\"",
",",
"\"freeze\"",
"]",
"try",
":",
"for",
"package",
"in",
"subprocess",
".",
"check_output",
"(",
"CMDLINE",
")",
".",
"decode",
"(",
"'utf-8'",
")",
".",
"splitlines",
"(",
")",
":",
"for",
"comparator",
"in",
"[",
"\"==\"",
",",
"\">=\"",
",",
"\"<=\"",
",",
"\"<\"",
",",
"\">\"",
"]",
":",
"if",
"comparator",
"in",
"package",
":",
"# installed package names usually look like Pillow==2.8.1",
"# ignore others, like external packages that pip show",
"# won't understand",
"name",
"=",
"package",
".",
"partition",
"(",
"comparator",
")",
"[",
"0",
"]",
"packages",
".",
"append",
"(",
"name",
")",
"except",
"RuntimeError",
"as",
"e",
":",
"if",
"logger",
".",
"isEnabledFor",
"(",
"logging",
".",
"DEBUG",
")",
":",
"logger",
".",
"debug",
"(",
"\"Exception checking existing packages.\"",
")",
"logger",
".",
"debug",
"(",
"\"cmdline: %s\"",
",",
"CMDLINE",
")",
"ex_type",
",",
"ex",
",",
"tb",
"=",
"sys",
".",
"exc_info",
"(",
")",
"traceback",
".",
"print_tb",
"(",
"tb",
")",
"logger",
".",
"debug",
"(",
")",
"return",
"packages"
] | :return: list of installed packages | [
":",
"return",
":",
"list",
"of",
"installed",
"packages"
] | fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69 | https://github.com/stuaxo/vext/blob/fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69/setup.py#L127-L149 | train |
stuaxo/vext | setup.py | InstallLib.package_info | def package_info(self):
"""
:return: list of package info on installed packages
"""
import subprocess
# create a commandline like pip show Pillow show
package_names = self.installed_packages()
if not package_names:
# No installed packages yet, so nothign to do here...
return []
cmdline = [sys.executable, "-mpip"]
for name in package_names:
cmdline.extend(["show", name])
output = subprocess.check_output(cmdline)
# Python 3 fix
if not isinstance(output, str):
# Some package info is encoded in Latin-1 or something other than
# UTF8. Replace non-UTF characters with '?' instead of crashing.
output = str(output, encoding='UTF-8', errors='replace')
# parse output that looks like this example
"""
---
Name: Pillow
Version: 2.8.1
Location: /mnt/data/home/stu/.virtualenvs/shoebot-setup/lib/python2.7/site-packages/Pillow-2.8.1-py2.7-linux-x86_64.egg
Requires:
---
Name: vext.gi
Version: 0.5.6.25
Location: /mnt/data/home/stu/.virtualenvs/shoebot-setup/lib/python2.7/site-packages/vext.gi-0.5.6.25-py2.7.egg
Requires: vext
"""
results = []
for info in output[3:].split("---"):
d = {}
for line in info[1:].splitlines():
arg, _, value = line.partition(': ')
arg = arg.lower()
if arg == 'requires':
value = value.split(', ')
d[arg] = value
results.append(d)
return results | python | def package_info(self):
"""
:return: list of package info on installed packages
"""
import subprocess
# create a commandline like pip show Pillow show
package_names = self.installed_packages()
if not package_names:
# No installed packages yet, so nothign to do here...
return []
cmdline = [sys.executable, "-mpip"]
for name in package_names:
cmdline.extend(["show", name])
output = subprocess.check_output(cmdline)
# Python 3 fix
if not isinstance(output, str):
# Some package info is encoded in Latin-1 or something other than
# UTF8. Replace non-UTF characters with '?' instead of crashing.
output = str(output, encoding='UTF-8', errors='replace')
# parse output that looks like this example
"""
---
Name: Pillow
Version: 2.8.1
Location: /mnt/data/home/stu/.virtualenvs/shoebot-setup/lib/python2.7/site-packages/Pillow-2.8.1-py2.7-linux-x86_64.egg
Requires:
---
Name: vext.gi
Version: 0.5.6.25
Location: /mnt/data/home/stu/.virtualenvs/shoebot-setup/lib/python2.7/site-packages/vext.gi-0.5.6.25-py2.7.egg
Requires: vext
"""
results = []
for info in output[3:].split("---"):
d = {}
for line in info[1:].splitlines():
arg, _, value = line.partition(': ')
arg = arg.lower()
if arg == 'requires':
value = value.split(', ')
d[arg] = value
results.append(d)
return results | [
"def",
"package_info",
"(",
"self",
")",
":",
"import",
"subprocess",
"# create a commandline like pip show Pillow show",
"package_names",
"=",
"self",
".",
"installed_packages",
"(",
")",
"if",
"not",
"package_names",
":",
"# No installed packages yet, so nothign to do here...",
"return",
"[",
"]",
"cmdline",
"=",
"[",
"sys",
".",
"executable",
",",
"\"-mpip\"",
"]",
"for",
"name",
"in",
"package_names",
":",
"cmdline",
".",
"extend",
"(",
"[",
"\"show\"",
",",
"name",
"]",
")",
"output",
"=",
"subprocess",
".",
"check_output",
"(",
"cmdline",
")",
"# Python 3 fix",
"if",
"not",
"isinstance",
"(",
"output",
",",
"str",
")",
":",
"# Some package info is encoded in Latin-1 or something other than",
"# UTF8. Replace non-UTF characters with '?' instead of crashing.",
"output",
"=",
"str",
"(",
"output",
",",
"encoding",
"=",
"'UTF-8'",
",",
"errors",
"=",
"'replace'",
")",
"# parse output that looks like this example",
"\"\"\"\n ---\n Name: Pillow\n Version: 2.8.1\n Location: /mnt/data/home/stu/.virtualenvs/shoebot-setup/lib/python2.7/site-packages/Pillow-2.8.1-py2.7-linux-x86_64.egg\n Requires:\n ---\n Name: vext.gi\n Version: 0.5.6.25\n Location: /mnt/data/home/stu/.virtualenvs/shoebot-setup/lib/python2.7/site-packages/vext.gi-0.5.6.25-py2.7.egg\n Requires: vext\n\n \"\"\"",
"results",
"=",
"[",
"]",
"for",
"info",
"in",
"output",
"[",
"3",
":",
"]",
".",
"split",
"(",
"\"---\"",
")",
":",
"d",
"=",
"{",
"}",
"for",
"line",
"in",
"info",
"[",
"1",
":",
"]",
".",
"splitlines",
"(",
")",
":",
"arg",
",",
"_",
",",
"value",
"=",
"line",
".",
"partition",
"(",
"': '",
")",
"arg",
"=",
"arg",
".",
"lower",
"(",
")",
"if",
"arg",
"==",
"'requires'",
":",
"value",
"=",
"value",
".",
"split",
"(",
"', '",
")",
"d",
"[",
"arg",
"]",
"=",
"value",
"results",
".",
"append",
"(",
"d",
")",
"return",
"results"
] | :return: list of package info on installed packages | [
":",
"return",
":",
"list",
"of",
"package",
"info",
"on",
"installed",
"packages"
] | fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69 | https://github.com/stuaxo/vext/blob/fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69/setup.py#L151-L197 | train |
stuaxo/vext | setup.py | InstallLib.depends_on | def depends_on(self, dependency):
"""
List of packages that depend on dependency
:param dependency: package name, e.g. 'vext' or 'Pillow'
"""
packages = self.package_info()
return [package for package in packages if dependency in package.get("requires", "")] | python | def depends_on(self, dependency):
"""
List of packages that depend on dependency
:param dependency: package name, e.g. 'vext' or 'Pillow'
"""
packages = self.package_info()
return [package for package in packages if dependency in package.get("requires", "")] | [
"def",
"depends_on",
"(",
"self",
",",
"dependency",
")",
":",
"packages",
"=",
"self",
".",
"package_info",
"(",
")",
"return",
"[",
"package",
"for",
"package",
"in",
"packages",
"if",
"dependency",
"in",
"package",
".",
"get",
"(",
"\"requires\"",
",",
"\"\"",
")",
"]"
] | List of packages that depend on dependency
:param dependency: package name, e.g. 'vext' or 'Pillow' | [
"List",
"of",
"packages",
"that",
"depend",
"on",
"dependency",
":",
"param",
"dependency",
":",
"package",
"name",
"e",
".",
"g",
".",
"vext",
"or",
"Pillow"
] | fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69 | https://github.com/stuaxo/vext/blob/fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69/setup.py#L199-L205 | train |
stuaxo/vext | setup.py | InstallLib.find_vext_files | def find_vext_files(self):
"""
:return: Absolute paths to any provided vext files
"""
packages = self.depends_on("vext")
vext_files = []
for location in [package.get("location") for package in packages]:
if not location:
continue
vext_files.extend(glob(join(location, "*.vext")))
return vext_files | python | def find_vext_files(self):
"""
:return: Absolute paths to any provided vext files
"""
packages = self.depends_on("vext")
vext_files = []
for location in [package.get("location") for package in packages]:
if not location:
continue
vext_files.extend(glob(join(location, "*.vext")))
return vext_files | [
"def",
"find_vext_files",
"(",
"self",
")",
":",
"packages",
"=",
"self",
".",
"depends_on",
"(",
"\"vext\"",
")",
"vext_files",
"=",
"[",
"]",
"for",
"location",
"in",
"[",
"package",
".",
"get",
"(",
"\"location\"",
")",
"for",
"package",
"in",
"packages",
"]",
":",
"if",
"not",
"location",
":",
"continue",
"vext_files",
".",
"extend",
"(",
"glob",
"(",
"join",
"(",
"location",
",",
"\"*.vext\"",
")",
")",
")",
"return",
"vext_files"
] | :return: Absolute paths to any provided vext files | [
":",
"return",
":",
"Absolute",
"paths",
"to",
"any",
"provided",
"vext",
"files"
] | fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69 | https://github.com/stuaxo/vext/blob/fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69/setup.py#L207-L217 | train |
stuaxo/vext | setup.py | InstallLib.run | def run(self):
"""
Need to find any pre-existing vext contained in dependent packages
and install them
example:
you create a setup.py with install_requires["vext.gi"]:
- vext.gi gets installed using bdist_egg
- vext itself is now called with bdist_egg and we end up here
Vext now needs to find and install .vext files in vext.gi
[or any other files that depend on vext]
:return:
"""
logger.debug("vext InstallLib [started]")
# Find packages that depend on vext and check for .vext files...
logger.debug("find_vext_files")
vext_files = self.find_vext_files()
logger.debug("manually_install_vext: ", vext_files)
self.manually_install_vext(vext_files)
logger.debug("enable vext")
self.enable_vext()
logger.debug("install_lib.run")
install_lib.run(self)
logger.debug("vext InstallLib [finished]") | python | def run(self):
"""
Need to find any pre-existing vext contained in dependent packages
and install them
example:
you create a setup.py with install_requires["vext.gi"]:
- vext.gi gets installed using bdist_egg
- vext itself is now called with bdist_egg and we end up here
Vext now needs to find and install .vext files in vext.gi
[or any other files that depend on vext]
:return:
"""
logger.debug("vext InstallLib [started]")
# Find packages that depend on vext and check for .vext files...
logger.debug("find_vext_files")
vext_files = self.find_vext_files()
logger.debug("manually_install_vext: ", vext_files)
self.manually_install_vext(vext_files)
logger.debug("enable vext")
self.enable_vext()
logger.debug("install_lib.run")
install_lib.run(self)
logger.debug("vext InstallLib [finished]") | [
"def",
"run",
"(",
"self",
")",
":",
"logger",
".",
"debug",
"(",
"\"vext InstallLib [started]\"",
")",
"# Find packages that depend on vext and check for .vext files...",
"logger",
".",
"debug",
"(",
"\"find_vext_files\"",
")",
"vext_files",
"=",
"self",
".",
"find_vext_files",
"(",
")",
"logger",
".",
"debug",
"(",
"\"manually_install_vext: \"",
",",
"vext_files",
")",
"self",
".",
"manually_install_vext",
"(",
"vext_files",
")",
"logger",
".",
"debug",
"(",
"\"enable vext\"",
")",
"self",
".",
"enable_vext",
"(",
")",
"logger",
".",
"debug",
"(",
"\"install_lib.run\"",
")",
"install_lib",
".",
"run",
"(",
"self",
")",
"logger",
".",
"debug",
"(",
"\"vext InstallLib [finished]\"",
")"
] | Need to find any pre-existing vext contained in dependent packages
and install them
example:
you create a setup.py with install_requires["vext.gi"]:
- vext.gi gets installed using bdist_egg
- vext itself is now called with bdist_egg and we end up here
Vext now needs to find and install .vext files in vext.gi
[or any other files that depend on vext]
:return: | [
"Need",
"to",
"find",
"any",
"pre",
"-",
"existing",
"vext",
"contained",
"in",
"dependent",
"packages",
"and",
"install",
"them"
] | fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69 | https://github.com/stuaxo/vext/blob/fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69/setup.py#L244-L273 | train |
jaysonsantos/python-binary-memcached | bmemcached/client/mixin.py | ClientMixin.set_servers | def set_servers(self, servers):
"""
Iter to a list of servers and instantiate Protocol class.
:param servers: A list of servers
:type servers: list
:return: Returns nothing
:rtype: None
"""
if isinstance(servers, six.string_types):
servers = [servers]
assert servers, "No memcached servers supplied"
self._servers = [Protocol(
server=server,
username=self.username,
password=self.password,
compression=self.compression,
socket_timeout=self.socket_timeout,
pickle_protocol=self.pickle_protocol,
pickler=self.pickler,
unpickler=self.unpickler,
) for server in servers] | python | def set_servers(self, servers):
"""
Iter to a list of servers and instantiate Protocol class.
:param servers: A list of servers
:type servers: list
:return: Returns nothing
:rtype: None
"""
if isinstance(servers, six.string_types):
servers = [servers]
assert servers, "No memcached servers supplied"
self._servers = [Protocol(
server=server,
username=self.username,
password=self.password,
compression=self.compression,
socket_timeout=self.socket_timeout,
pickle_protocol=self.pickle_protocol,
pickler=self.pickler,
unpickler=self.unpickler,
) for server in servers] | [
"def",
"set_servers",
"(",
"self",
",",
"servers",
")",
":",
"if",
"isinstance",
"(",
"servers",
",",
"six",
".",
"string_types",
")",
":",
"servers",
"=",
"[",
"servers",
"]",
"assert",
"servers",
",",
"\"No memcached servers supplied\"",
"self",
".",
"_servers",
"=",
"[",
"Protocol",
"(",
"server",
"=",
"server",
",",
"username",
"=",
"self",
".",
"username",
",",
"password",
"=",
"self",
".",
"password",
",",
"compression",
"=",
"self",
".",
"compression",
",",
"socket_timeout",
"=",
"self",
".",
"socket_timeout",
",",
"pickle_protocol",
"=",
"self",
".",
"pickle_protocol",
",",
"pickler",
"=",
"self",
".",
"pickler",
",",
"unpickler",
"=",
"self",
".",
"unpickler",
",",
")",
"for",
"server",
"in",
"servers",
"]"
] | Iter to a list of servers and instantiate Protocol class.
:param servers: A list of servers
:type servers: list
:return: Returns nothing
:rtype: None | [
"Iter",
"to",
"a",
"list",
"of",
"servers",
"and",
"instantiate",
"Protocol",
"class",
"."
] | 6a792829349c69204d9c5045e5c34b4231216dd6 | https://github.com/jaysonsantos/python-binary-memcached/blob/6a792829349c69204d9c5045e5c34b4231216dd6/bmemcached/client/mixin.py#L48-L70 | train |
jaysonsantos/python-binary-memcached | bmemcached/client/mixin.py | ClientMixin.flush_all | def flush_all(self, time=0):
"""
Send a command to server flush|delete all keys.
:param time: Time to wait until flush in seconds.
:type time: int
:return: True in case of success, False in case of failure
:rtype: bool
"""
returns = []
for server in self.servers:
returns.append(server.flush_all(time))
return any(returns) | python | def flush_all(self, time=0):
"""
Send a command to server flush|delete all keys.
:param time: Time to wait until flush in seconds.
:type time: int
:return: True in case of success, False in case of failure
:rtype: bool
"""
returns = []
for server in self.servers:
returns.append(server.flush_all(time))
return any(returns) | [
"def",
"flush_all",
"(",
"self",
",",
"time",
"=",
"0",
")",
":",
"returns",
"=",
"[",
"]",
"for",
"server",
"in",
"self",
".",
"servers",
":",
"returns",
".",
"append",
"(",
"server",
".",
"flush_all",
"(",
"time",
")",
")",
"return",
"any",
"(",
"returns",
")"
] | Send a command to server flush|delete all keys.
:param time: Time to wait until flush in seconds.
:type time: int
:return: True in case of success, False in case of failure
:rtype: bool | [
"Send",
"a",
"command",
"to",
"server",
"flush|delete",
"all",
"keys",
"."
] | 6a792829349c69204d9c5045e5c34b4231216dd6 | https://github.com/jaysonsantos/python-binary-memcached/blob/6a792829349c69204d9c5045e5c34b4231216dd6/bmemcached/client/mixin.py#L72-L85 | train |
jaysonsantos/python-binary-memcached | bmemcached/client/mixin.py | ClientMixin.stats | def stats(self, key=None):
"""
Return server stats.
:param key: Optional if you want status from a key.
:type key: six.string_types
:return: A dict with server stats
:rtype: dict
"""
# TODO: Stats with key is not working.
returns = {}
for server in self.servers:
returns[server.server] = server.stats(key)
return returns | python | def stats(self, key=None):
"""
Return server stats.
:param key: Optional if you want status from a key.
:type key: six.string_types
:return: A dict with server stats
:rtype: dict
"""
# TODO: Stats with key is not working.
returns = {}
for server in self.servers:
returns[server.server] = server.stats(key)
return returns | [
"def",
"stats",
"(",
"self",
",",
"key",
"=",
"None",
")",
":",
"# TODO: Stats with key is not working.",
"returns",
"=",
"{",
"}",
"for",
"server",
"in",
"self",
".",
"servers",
":",
"returns",
"[",
"server",
".",
"server",
"]",
"=",
"server",
".",
"stats",
"(",
"key",
")",
"return",
"returns"
] | Return server stats.
:param key: Optional if you want status from a key.
:type key: six.string_types
:return: A dict with server stats
:rtype: dict | [
"Return",
"server",
"stats",
"."
] | 6a792829349c69204d9c5045e5c34b4231216dd6 | https://github.com/jaysonsantos/python-binary-memcached/blob/6a792829349c69204d9c5045e5c34b4231216dd6/bmemcached/client/mixin.py#L87-L102 | train |
gak/pygooglechart | examples/pie.py | house_explosions | def house_explosions():
"""
Data from http://indexed.blogspot.com/2007/12/meltdown-indeed.html
"""
chart = PieChart2D(int(settings.width * 1.7), settings.height)
chart.add_data([10, 10, 30, 200])
chart.set_pie_labels([
'Budding Chemists',
'Propane issues',
'Meth Labs',
'Attempts to escape morgage',
])
chart.download('pie-house-explosions.png') | python | def house_explosions():
"""
Data from http://indexed.blogspot.com/2007/12/meltdown-indeed.html
"""
chart = PieChart2D(int(settings.width * 1.7), settings.height)
chart.add_data([10, 10, 30, 200])
chart.set_pie_labels([
'Budding Chemists',
'Propane issues',
'Meth Labs',
'Attempts to escape morgage',
])
chart.download('pie-house-explosions.png') | [
"def",
"house_explosions",
"(",
")",
":",
"chart",
"=",
"PieChart2D",
"(",
"int",
"(",
"settings",
".",
"width",
"*",
"1.7",
")",
",",
"settings",
".",
"height",
")",
"chart",
".",
"add_data",
"(",
"[",
"10",
",",
"10",
",",
"30",
",",
"200",
"]",
")",
"chart",
".",
"set_pie_labels",
"(",
"[",
"'Budding Chemists'",
",",
"'Propane issues'",
",",
"'Meth Labs'",
",",
"'Attempts to escape morgage'",
",",
"]",
")",
"chart",
".",
"download",
"(",
"'pie-house-explosions.png'",
")"
] | Data from http://indexed.blogspot.com/2007/12/meltdown-indeed.html | [
"Data",
"from",
"http",
":",
"//",
"indexed",
".",
"blogspot",
".",
"com",
"/",
"2007",
"/",
"12",
"/",
"meltdown",
"-",
"indeed",
".",
"html"
] | 25234bb63127a7e5e057c0b98ab841f3f1d93b21 | https://github.com/gak/pygooglechart/blob/25234bb63127a7e5e057c0b98ab841f3f1d93b21/examples/pie.py#L45-L57 | train |
hsolbrig/PyShEx | pyshex/utils/value_set_utils.py | objectValueMatches | def objectValueMatches(n: Node, vsv: ShExJ.objectValue) -> bool:
""" http://shex.io/shex-semantics/#values
Implements "n = vsv" where vsv is an objectValue and n is a Node
Note that IRIREF is a string pattern, so the matching type is str
"""
return \
(isinstance(vsv, IRIREF) and isinstance(n, URIRef) and uriref_matches_iriref(n, vsv)) or \
(isinstance(vsv, ShExJ.ObjectLiteral) and isinstance(n, Literal) and literal_matches_objectliteral(n, vsv)) | python | def objectValueMatches(n: Node, vsv: ShExJ.objectValue) -> bool:
""" http://shex.io/shex-semantics/#values
Implements "n = vsv" where vsv is an objectValue and n is a Node
Note that IRIREF is a string pattern, so the matching type is str
"""
return \
(isinstance(vsv, IRIREF) and isinstance(n, URIRef) and uriref_matches_iriref(n, vsv)) or \
(isinstance(vsv, ShExJ.ObjectLiteral) and isinstance(n, Literal) and literal_matches_objectliteral(n, vsv)) | [
"def",
"objectValueMatches",
"(",
"n",
":",
"Node",
",",
"vsv",
":",
"ShExJ",
".",
"objectValue",
")",
"->",
"bool",
":",
"return",
"(",
"isinstance",
"(",
"vsv",
",",
"IRIREF",
")",
"and",
"isinstance",
"(",
"n",
",",
"URIRef",
")",
"and",
"uriref_matches_iriref",
"(",
"n",
",",
"vsv",
")",
")",
"or",
"(",
"isinstance",
"(",
"vsv",
",",
"ShExJ",
".",
"ObjectLiteral",
")",
"and",
"isinstance",
"(",
"n",
",",
"Literal",
")",
"and",
"literal_matches_objectliteral",
"(",
"n",
",",
"vsv",
")",
")"
] | http://shex.io/shex-semantics/#values
Implements "n = vsv" where vsv is an objectValue and n is a Node
Note that IRIREF is a string pattern, so the matching type is str | [
"http",
":",
"//",
"shex",
".",
"io",
"/",
"shex",
"-",
"semantics",
"/",
"#values"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/utils/value_set_utils.py#L10-L19 | train |
hsolbrig/PyShEx | pyshex/utils/value_set_utils.py | uriref_matches_iriref | def uriref_matches_iriref(v1: URIRef, v2: Union[str, ShExJ.IRIREF]) -> bool:
""" Compare :py:class:`rdflib.URIRef` value with :py:class:`ShExJ.IRIREF` value """
return str(v1) == str(v2) | python | def uriref_matches_iriref(v1: URIRef, v2: Union[str, ShExJ.IRIREF]) -> bool:
""" Compare :py:class:`rdflib.URIRef` value with :py:class:`ShExJ.IRIREF` value """
return str(v1) == str(v2) | [
"def",
"uriref_matches_iriref",
"(",
"v1",
":",
"URIRef",
",",
"v2",
":",
"Union",
"[",
"str",
",",
"ShExJ",
".",
"IRIREF",
"]",
")",
"->",
"bool",
":",
"return",
"str",
"(",
"v1",
")",
"==",
"str",
"(",
"v2",
")"
] | Compare :py:class:`rdflib.URIRef` value with :py:class:`ShExJ.IRIREF` value | [
"Compare",
":",
"py",
":",
"class",
":",
"rdflib",
".",
"URIRef",
"value",
"with",
":",
"py",
":",
"class",
":",
"ShExJ",
".",
"IRIREF",
"value"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/utils/value_set_utils.py#L22-L24 | train |
hsolbrig/PyShEx | pyshex/utils/value_set_utils.py | uriref_startswith_iriref | def uriref_startswith_iriref(v1: URIRef, v2: Union[str, ShExJ.IRIREF]) -> bool:
""" Determine whether a :py:class:`rdflib.URIRef` value starts with the text of a :py:class:`ShExJ.IRIREF` value """
return str(v1).startswith(str(v2)) | python | def uriref_startswith_iriref(v1: URIRef, v2: Union[str, ShExJ.IRIREF]) -> bool:
""" Determine whether a :py:class:`rdflib.URIRef` value starts with the text of a :py:class:`ShExJ.IRIREF` value """
return str(v1).startswith(str(v2)) | [
"def",
"uriref_startswith_iriref",
"(",
"v1",
":",
"URIRef",
",",
"v2",
":",
"Union",
"[",
"str",
",",
"ShExJ",
".",
"IRIREF",
"]",
")",
"->",
"bool",
":",
"return",
"str",
"(",
"v1",
")",
".",
"startswith",
"(",
"str",
"(",
"v2",
")",
")"
] | Determine whether a :py:class:`rdflib.URIRef` value starts with the text of a :py:class:`ShExJ.IRIREF` value | [
"Determine",
"whether",
"a",
":",
"py",
":",
"class",
":",
"rdflib",
".",
"URIRef",
"value",
"starts",
"with",
"the",
"text",
"of",
"a",
":",
"py",
":",
"class",
":",
"ShExJ",
".",
"IRIREF",
"value"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/utils/value_set_utils.py#L27-L29 | train |
hsolbrig/PyShEx | pyshex/utils/value_set_utils.py | literal_matches_objectliteral | def literal_matches_objectliteral(v1: Literal, v2: ShExJ.ObjectLiteral) -> bool:
""" Compare :py:class:`rdflib.Literal` with :py:class:`ShExJ.objectLiteral` """
v2_lit = Literal(str(v2.value), datatype=iriref_to_uriref(v2.type), lang=str(v2.language) if v2.language else None)
return v1 == v2_lit | python | def literal_matches_objectliteral(v1: Literal, v2: ShExJ.ObjectLiteral) -> bool:
""" Compare :py:class:`rdflib.Literal` with :py:class:`ShExJ.objectLiteral` """
v2_lit = Literal(str(v2.value), datatype=iriref_to_uriref(v2.type), lang=str(v2.language) if v2.language else None)
return v1 == v2_lit | [
"def",
"literal_matches_objectliteral",
"(",
"v1",
":",
"Literal",
",",
"v2",
":",
"ShExJ",
".",
"ObjectLiteral",
")",
"->",
"bool",
":",
"v2_lit",
"=",
"Literal",
"(",
"str",
"(",
"v2",
".",
"value",
")",
",",
"datatype",
"=",
"iriref_to_uriref",
"(",
"v2",
".",
"type",
")",
",",
"lang",
"=",
"str",
"(",
"v2",
".",
"language",
")",
"if",
"v2",
".",
"language",
"else",
"None",
")",
"return",
"v1",
"==",
"v2_lit"
] | Compare :py:class:`rdflib.Literal` with :py:class:`ShExJ.objectLiteral` | [
"Compare",
":",
"py",
":",
"class",
":",
"rdflib",
".",
"Literal",
"with",
":",
"py",
":",
"class",
":",
"ShExJ",
".",
"objectLiteral"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/utils/value_set_utils.py#L32-L35 | train |
hsolbrig/PyShEx | pyshex/shape_expressions_language/p5_4_node_constraints.py | satisfiesNodeConstraint | def satisfiesNodeConstraint(cntxt: Context, n: Node, nc: ShExJ.NodeConstraint, _: DebugContext) -> bool:
""" `5.4.1 Semantics <http://shex.io/shex-semantics/#node-constraint-semantics>`_
For a node n and constraint nc, satisfies2(n, nc) if and only if for every nodeKind, datatype, xsFacet and
values constraint value v present in nc nodeSatisfies(n, v). The following sections define nodeSatisfies for
each of these types of constraints:
"""
return nodeSatisfiesNodeKind(cntxt, n, nc) and nodeSatisfiesDataType(cntxt, n, nc) and \
nodeSatisfiesStringFacet(cntxt, n, nc) and nodeSatisfiesNumericFacet(cntxt, n, nc) and \
nodeSatisfiesValues(cntxt, n, nc) | python | def satisfiesNodeConstraint(cntxt: Context, n: Node, nc: ShExJ.NodeConstraint, _: DebugContext) -> bool:
""" `5.4.1 Semantics <http://shex.io/shex-semantics/#node-constraint-semantics>`_
For a node n and constraint nc, satisfies2(n, nc) if and only if for every nodeKind, datatype, xsFacet and
values constraint value v present in nc nodeSatisfies(n, v). The following sections define nodeSatisfies for
each of these types of constraints:
"""
return nodeSatisfiesNodeKind(cntxt, n, nc) and nodeSatisfiesDataType(cntxt, n, nc) and \
nodeSatisfiesStringFacet(cntxt, n, nc) and nodeSatisfiesNumericFacet(cntxt, n, nc) and \
nodeSatisfiesValues(cntxt, n, nc) | [
"def",
"satisfiesNodeConstraint",
"(",
"cntxt",
":",
"Context",
",",
"n",
":",
"Node",
",",
"nc",
":",
"ShExJ",
".",
"NodeConstraint",
",",
"_",
":",
"DebugContext",
")",
"->",
"bool",
":",
"return",
"nodeSatisfiesNodeKind",
"(",
"cntxt",
",",
"n",
",",
"nc",
")",
"and",
"nodeSatisfiesDataType",
"(",
"cntxt",
",",
"n",
",",
"nc",
")",
"and",
"nodeSatisfiesStringFacet",
"(",
"cntxt",
",",
"n",
",",
"nc",
")",
"and",
"nodeSatisfiesNumericFacet",
"(",
"cntxt",
",",
"n",
",",
"nc",
")",
"and",
"nodeSatisfiesValues",
"(",
"cntxt",
",",
"n",
",",
"nc",
")"
] | `5.4.1 Semantics <http://shex.io/shex-semantics/#node-constraint-semantics>`_
For a node n and constraint nc, satisfies2(n, nc) if and only if for every nodeKind, datatype, xsFacet and
values constraint value v present in nc nodeSatisfies(n, v). The following sections define nodeSatisfies for
each of these types of constraints: | [
"5",
".",
"4",
".",
"1",
"Semantics",
"<http",
":",
"//",
"shex",
".",
"io",
"/",
"shex",
"-",
"semantics",
"/",
"#node",
"-",
"constraint",
"-",
"semantics",
">",
"_"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/shape_expressions_language/p5_4_node_constraints.py#L20-L29 | train |
hsolbrig/PyShEx | pyshex/shape_expressions_language/p5_4_node_constraints.py | nodeSatisfiesNodeKind | def nodeSatisfiesNodeKind(cntxt: Context, n: Node, nc: ShExJ.NodeConstraint, c: DebugContext) -> bool:
""" `5.4.2 Node Kind Constraints <http://shex.io/shex-semantics/#nodeKind>`_
For a node n and constraint value v, nodeSatisfies(n, v) if:
* v = "iri" and n is an IRI.
* v = "bnode" and n is a blank node.
* v = "literal" and n is a Literal.
* v = "nonliteral" and n is an IRI or blank node.
"""
if c.debug and nc.nodeKind is not None:
print(f" Kind: {nc.nodeKind}")
if nc.nodeKind is None or \
(nc.nodeKind == 'iri' and isinstance(n, URIRef)) or \
(nc.nodeKind == 'bnode' and isinstance(n, BNode)) or \
(nc.nodeKind == 'literal' and isinstance(n, Literal)) or \
(nc.nodeKind == 'nonliteral' and isinstance(n, (URIRef, BNode))):
return True
cntxt.fail_reason = f"Node kind mismatch have: {type(n).__name__} expected: {nc.nodeKind}"
return False | python | def nodeSatisfiesNodeKind(cntxt: Context, n: Node, nc: ShExJ.NodeConstraint, c: DebugContext) -> bool:
""" `5.4.2 Node Kind Constraints <http://shex.io/shex-semantics/#nodeKind>`_
For a node n and constraint value v, nodeSatisfies(n, v) if:
* v = "iri" and n is an IRI.
* v = "bnode" and n is a blank node.
* v = "literal" and n is a Literal.
* v = "nonliteral" and n is an IRI or blank node.
"""
if c.debug and nc.nodeKind is not None:
print(f" Kind: {nc.nodeKind}")
if nc.nodeKind is None or \
(nc.nodeKind == 'iri' and isinstance(n, URIRef)) or \
(nc.nodeKind == 'bnode' and isinstance(n, BNode)) or \
(nc.nodeKind == 'literal' and isinstance(n, Literal)) or \
(nc.nodeKind == 'nonliteral' and isinstance(n, (URIRef, BNode))):
return True
cntxt.fail_reason = f"Node kind mismatch have: {type(n).__name__} expected: {nc.nodeKind}"
return False | [
"def",
"nodeSatisfiesNodeKind",
"(",
"cntxt",
":",
"Context",
",",
"n",
":",
"Node",
",",
"nc",
":",
"ShExJ",
".",
"NodeConstraint",
",",
"c",
":",
"DebugContext",
")",
"->",
"bool",
":",
"if",
"c",
".",
"debug",
"and",
"nc",
".",
"nodeKind",
"is",
"not",
"None",
":",
"print",
"(",
"f\" Kind: {nc.nodeKind}\"",
")",
"if",
"nc",
".",
"nodeKind",
"is",
"None",
"or",
"(",
"nc",
".",
"nodeKind",
"==",
"'iri'",
"and",
"isinstance",
"(",
"n",
",",
"URIRef",
")",
")",
"or",
"(",
"nc",
".",
"nodeKind",
"==",
"'bnode'",
"and",
"isinstance",
"(",
"n",
",",
"BNode",
")",
")",
"or",
"(",
"nc",
".",
"nodeKind",
"==",
"'literal'",
"and",
"isinstance",
"(",
"n",
",",
"Literal",
")",
")",
"or",
"(",
"nc",
".",
"nodeKind",
"==",
"'nonliteral'",
"and",
"isinstance",
"(",
"n",
",",
"(",
"URIRef",
",",
"BNode",
")",
")",
")",
":",
"return",
"True",
"cntxt",
".",
"fail_reason",
"=",
"f\"Node kind mismatch have: {type(n).__name__} expected: {nc.nodeKind}\"",
"return",
"False"
] | `5.4.2 Node Kind Constraints <http://shex.io/shex-semantics/#nodeKind>`_
For a node n and constraint value v, nodeSatisfies(n, v) if:
* v = "iri" and n is an IRI.
* v = "bnode" and n is a blank node.
* v = "literal" and n is a Literal.
* v = "nonliteral" and n is an IRI or blank node. | [
"5",
".",
"4",
".",
"2",
"Node",
"Kind",
"Constraints",
"<http",
":",
"//",
"shex",
".",
"io",
"/",
"shex",
"-",
"semantics",
"/",
"#nodeKind",
">",
"_"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/shape_expressions_language/p5_4_node_constraints.py#L33-L52 | train |
hsolbrig/PyShEx | pyshex/shape_expressions_language/p5_4_node_constraints.py | nodeSatisfiesDataType | def nodeSatisfiesDataType(cntxt: Context, n: Node, nc: ShExJ.NodeConstraint, c: DebugContext) -> bool:
""" `5.4.3 Datatype Constraints <http://shex.io/shex-semantics/#datatype>`_
For a node n and constraint value v, nodeSatisfies(n, v) if n is an Literal with the datatype v and, if v is in
the set of SPARQL operand data types[sparql11-query], an XML schema string with a value of the lexical form of
n can be cast to the target type v per XPath Functions 3.1 section 19 Casting[xpath-functions]. Only datatypes
supported by SPARQL MUST be tested but ShEx extensions MAY add support for other datatypes.
"""
if nc.datatype is None:
return True
if c.debug:
print(f" Datatype: {nc.datatype}")
if not isinstance(n, Literal):
cntxt.fail_reason = f"Datatype constraint ({nc.datatype}) " \
f"does not match {type(n).__name__} {cntxt.n3_mapper.n3(n)}"
cntxt.dump_bnode(n)
return False
actual_datatype = _datatype(n)
if actual_datatype == str(nc.datatype) or \
(is_sparql_operand_datatype(nc.datatype) and can_cast_to(n, nc.datatype)):
return True
cntxt.fail_reason = f"Datatype mismatch - expected: {nc.datatype} actual: {actual_datatype}"
return False | python | def nodeSatisfiesDataType(cntxt: Context, n: Node, nc: ShExJ.NodeConstraint, c: DebugContext) -> bool:
""" `5.4.3 Datatype Constraints <http://shex.io/shex-semantics/#datatype>`_
For a node n and constraint value v, nodeSatisfies(n, v) if n is an Literal with the datatype v and, if v is in
the set of SPARQL operand data types[sparql11-query], an XML schema string with a value of the lexical form of
n can be cast to the target type v per XPath Functions 3.1 section 19 Casting[xpath-functions]. Only datatypes
supported by SPARQL MUST be tested but ShEx extensions MAY add support for other datatypes.
"""
if nc.datatype is None:
return True
if c.debug:
print(f" Datatype: {nc.datatype}")
if not isinstance(n, Literal):
cntxt.fail_reason = f"Datatype constraint ({nc.datatype}) " \
f"does not match {type(n).__name__} {cntxt.n3_mapper.n3(n)}"
cntxt.dump_bnode(n)
return False
actual_datatype = _datatype(n)
if actual_datatype == str(nc.datatype) or \
(is_sparql_operand_datatype(nc.datatype) and can_cast_to(n, nc.datatype)):
return True
cntxt.fail_reason = f"Datatype mismatch - expected: {nc.datatype} actual: {actual_datatype}"
return False | [
"def",
"nodeSatisfiesDataType",
"(",
"cntxt",
":",
"Context",
",",
"n",
":",
"Node",
",",
"nc",
":",
"ShExJ",
".",
"NodeConstraint",
",",
"c",
":",
"DebugContext",
")",
"->",
"bool",
":",
"if",
"nc",
".",
"datatype",
"is",
"None",
":",
"return",
"True",
"if",
"c",
".",
"debug",
":",
"print",
"(",
"f\" Datatype: {nc.datatype}\"",
")",
"if",
"not",
"isinstance",
"(",
"n",
",",
"Literal",
")",
":",
"cntxt",
".",
"fail_reason",
"=",
"f\"Datatype constraint ({nc.datatype}) \"",
"f\"does not match {type(n).__name__} {cntxt.n3_mapper.n3(n)}\"",
"cntxt",
".",
"dump_bnode",
"(",
"n",
")",
"return",
"False",
"actual_datatype",
"=",
"_datatype",
"(",
"n",
")",
"if",
"actual_datatype",
"==",
"str",
"(",
"nc",
".",
"datatype",
")",
"or",
"(",
"is_sparql_operand_datatype",
"(",
"nc",
".",
"datatype",
")",
"and",
"can_cast_to",
"(",
"n",
",",
"nc",
".",
"datatype",
")",
")",
":",
"return",
"True",
"cntxt",
".",
"fail_reason",
"=",
"f\"Datatype mismatch - expected: {nc.datatype} actual: {actual_datatype}\"",
"return",
"False"
] | `5.4.3 Datatype Constraints <http://shex.io/shex-semantics/#datatype>`_
For a node n and constraint value v, nodeSatisfies(n, v) if n is an Literal with the datatype v and, if v is in
the set of SPARQL operand data types[sparql11-query], an XML schema string with a value of the lexical form of
n can be cast to the target type v per XPath Functions 3.1 section 19 Casting[xpath-functions]. Only datatypes
supported by SPARQL MUST be tested but ShEx extensions MAY add support for other datatypes. | [
"5",
".",
"4",
".",
"3",
"Datatype",
"Constraints",
"<http",
":",
"//",
"shex",
".",
"io",
"/",
"shex",
"-",
"semantics",
"/",
"#datatype",
">",
"_"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/shape_expressions_language/p5_4_node_constraints.py#L56-L78 | train |
hsolbrig/PyShEx | pyshex/shape_expressions_language/p5_4_node_constraints.py | nodeSatisfiesStringFacet | def nodeSatisfiesStringFacet(cntxt: Context, n: Node, nc: ShExJ.NodeConstraint, _c: DebugContext) -> bool:
""" `5.4.5 XML Schema String Facet Constraints <ttp://shex.io/shex-semantics/#xs-string>`_
String facet constraints apply to the lexical form of the RDF Literals and IRIs and blank node
identifiers (see note below regarding access to blank node identifiers).
"""
# Let lex =
#
# * if the value n is an RDF Literal, the lexical form of the literal (see[rdf11-concepts] section 3.3 Literals).
# * if the value n is an IRI, the IRI string (see[rdf11-concepts] section 3.2 IRIs).
# * if the value n is a blank node, the blank node identifier (see[rdf11-concepts] section 3.4 Blank Nodes).
if nc.length is not None or nc.minlength is not None or nc.maxlength is not None \
or nc.pattern is not None:
lex = str(n)
# Let len = the number of unicode codepoints in lex
# For a node n and constraint value v, nodeSatisfies(n, v):
#
# * for "length" constraints, v = len,
# * for "minlength" constraints, v >= len,
# * for "maxlength" constraints, v <= len,
# * for "pattern" constraints, v is unescaped into a valid XPath 3.1 regular expression[xpath-functions-31]
# re and invoking fn:matches(lex, re) returns fn:true. If the flags parameter is present, it is passed
# as a third argument to fn:matches. The pattern may have XPath 3.1 regular expression escape sequences
# per the modified production [10] in section 5.6.1.1 as well as numeric escape sequences of the
# form 'u' HEX HEX HEX HEX or 'U' HEX HEX HEX HEX HEX HEX HEX HEX. Unescaping replaces numeric escape
# sequences with the corresponding unicode codepoint
# TODO: Figure out whether we need to connect this to the lxml exslt functions
# TODO: Map flags if not
if (nc.length is None or len(lex) == nc.length) and \
(nc.minlength is None or len(lex) >= nc.minlength) and \
(nc.maxlength is None or len(lex) <= nc.maxlength) and \
(nc.pattern is None or pattern_match(nc.pattern, nc.flags, lex)):
return True
elif nc.length is not None and len(lex) != nc.length:
cntxt.fail_reason = f"String length mismatch - expected: {nc.length} actual: {len(lex)}"
elif nc.minlength is not None and len(lex) < nc.minlength:
cntxt.fail_reason = f"String length violation - minimum: {nc.minlength} actual: {len(lex)}"
elif nc.maxlength is not None and len(lex) > nc.maxlength:
cntxt.fail_reason = f"String length violation - maximum: {nc.maxlength} actual: {len(lex)}"
elif nc.pattern is not None and not pattern_match(nc.pattern, nc.flags, lex):
cntxt.fail_reason = f"Pattern match failure - pattern: {nc.pattern} flags:{nc.flags}" \
f" string: {lex}"
else:
cntxt.fail_reason = "Programming error - flame the programmer"
return False
else:
return True | python | def nodeSatisfiesStringFacet(cntxt: Context, n: Node, nc: ShExJ.NodeConstraint, _c: DebugContext) -> bool:
""" `5.4.5 XML Schema String Facet Constraints <ttp://shex.io/shex-semantics/#xs-string>`_
String facet constraints apply to the lexical form of the RDF Literals and IRIs and blank node
identifiers (see note below regarding access to blank node identifiers).
"""
# Let lex =
#
# * if the value n is an RDF Literal, the lexical form of the literal (see[rdf11-concepts] section 3.3 Literals).
# * if the value n is an IRI, the IRI string (see[rdf11-concepts] section 3.2 IRIs).
# * if the value n is a blank node, the blank node identifier (see[rdf11-concepts] section 3.4 Blank Nodes).
if nc.length is not None or nc.minlength is not None or nc.maxlength is not None \
or nc.pattern is not None:
lex = str(n)
# Let len = the number of unicode codepoints in lex
# For a node n and constraint value v, nodeSatisfies(n, v):
#
# * for "length" constraints, v = len,
# * for "minlength" constraints, v >= len,
# * for "maxlength" constraints, v <= len,
# * for "pattern" constraints, v is unescaped into a valid XPath 3.1 regular expression[xpath-functions-31]
# re and invoking fn:matches(lex, re) returns fn:true. If the flags parameter is present, it is passed
# as a third argument to fn:matches. The pattern may have XPath 3.1 regular expression escape sequences
# per the modified production [10] in section 5.6.1.1 as well as numeric escape sequences of the
# form 'u' HEX HEX HEX HEX or 'U' HEX HEX HEX HEX HEX HEX HEX HEX. Unescaping replaces numeric escape
# sequences with the corresponding unicode codepoint
# TODO: Figure out whether we need to connect this to the lxml exslt functions
# TODO: Map flags if not
if (nc.length is None or len(lex) == nc.length) and \
(nc.minlength is None or len(lex) >= nc.minlength) and \
(nc.maxlength is None or len(lex) <= nc.maxlength) and \
(nc.pattern is None or pattern_match(nc.pattern, nc.flags, lex)):
return True
elif nc.length is not None and len(lex) != nc.length:
cntxt.fail_reason = f"String length mismatch - expected: {nc.length} actual: {len(lex)}"
elif nc.minlength is not None and len(lex) < nc.minlength:
cntxt.fail_reason = f"String length violation - minimum: {nc.minlength} actual: {len(lex)}"
elif nc.maxlength is not None and len(lex) > nc.maxlength:
cntxt.fail_reason = f"String length violation - maximum: {nc.maxlength} actual: {len(lex)}"
elif nc.pattern is not None and not pattern_match(nc.pattern, nc.flags, lex):
cntxt.fail_reason = f"Pattern match failure - pattern: {nc.pattern} flags:{nc.flags}" \
f" string: {lex}"
else:
cntxt.fail_reason = "Programming error - flame the programmer"
return False
else:
return True | [
"def",
"nodeSatisfiesStringFacet",
"(",
"cntxt",
":",
"Context",
",",
"n",
":",
"Node",
",",
"nc",
":",
"ShExJ",
".",
"NodeConstraint",
",",
"_c",
":",
"DebugContext",
")",
"->",
"bool",
":",
"# Let lex =",
"#",
"# * if the value n is an RDF Literal, the lexical form of the literal (see[rdf11-concepts] section 3.3 Literals).",
"# * if the value n is an IRI, the IRI string (see[rdf11-concepts] section 3.2 IRIs).",
"# * if the value n is a blank node, the blank node identifier (see[rdf11-concepts] section 3.4 Blank Nodes).",
"if",
"nc",
".",
"length",
"is",
"not",
"None",
"or",
"nc",
".",
"minlength",
"is",
"not",
"None",
"or",
"nc",
".",
"maxlength",
"is",
"not",
"None",
"or",
"nc",
".",
"pattern",
"is",
"not",
"None",
":",
"lex",
"=",
"str",
"(",
"n",
")",
"# Let len = the number of unicode codepoints in lex",
"# For a node n and constraint value v, nodeSatisfies(n, v):",
"#",
"# * for \"length\" constraints, v = len,",
"# * for \"minlength\" constraints, v >= len,",
"# * for \"maxlength\" constraints, v <= len,",
"# * for \"pattern\" constraints, v is unescaped into a valid XPath 3.1 regular expression[xpath-functions-31]",
"# re and invoking fn:matches(lex, re) returns fn:true. If the flags parameter is present, it is passed",
"# as a third argument to fn:matches. The pattern may have XPath 3.1 regular expression escape sequences",
"# per the modified production [10] in section 5.6.1.1 as well as numeric escape sequences of the",
"# form 'u' HEX HEX HEX HEX or 'U' HEX HEX HEX HEX HEX HEX HEX HEX. Unescaping replaces numeric escape",
"# sequences with the corresponding unicode codepoint",
"# TODO: Figure out whether we need to connect this to the lxml exslt functions",
"# TODO: Map flags if not",
"if",
"(",
"nc",
".",
"length",
"is",
"None",
"or",
"len",
"(",
"lex",
")",
"==",
"nc",
".",
"length",
")",
"and",
"(",
"nc",
".",
"minlength",
"is",
"None",
"or",
"len",
"(",
"lex",
")",
">=",
"nc",
".",
"minlength",
")",
"and",
"(",
"nc",
".",
"maxlength",
"is",
"None",
"or",
"len",
"(",
"lex",
")",
"<=",
"nc",
".",
"maxlength",
")",
"and",
"(",
"nc",
".",
"pattern",
"is",
"None",
"or",
"pattern_match",
"(",
"nc",
".",
"pattern",
",",
"nc",
".",
"flags",
",",
"lex",
")",
")",
":",
"return",
"True",
"elif",
"nc",
".",
"length",
"is",
"not",
"None",
"and",
"len",
"(",
"lex",
")",
"!=",
"nc",
".",
"length",
":",
"cntxt",
".",
"fail_reason",
"=",
"f\"String length mismatch - expected: {nc.length} actual: {len(lex)}\"",
"elif",
"nc",
".",
"minlength",
"is",
"not",
"None",
"and",
"len",
"(",
"lex",
")",
"<",
"nc",
".",
"minlength",
":",
"cntxt",
".",
"fail_reason",
"=",
"f\"String length violation - minimum: {nc.minlength} actual: {len(lex)}\"",
"elif",
"nc",
".",
"maxlength",
"is",
"not",
"None",
"and",
"len",
"(",
"lex",
")",
">",
"nc",
".",
"maxlength",
":",
"cntxt",
".",
"fail_reason",
"=",
"f\"String length violation - maximum: {nc.maxlength} actual: {len(lex)}\"",
"elif",
"nc",
".",
"pattern",
"is",
"not",
"None",
"and",
"not",
"pattern_match",
"(",
"nc",
".",
"pattern",
",",
"nc",
".",
"flags",
",",
"lex",
")",
":",
"cntxt",
".",
"fail_reason",
"=",
"f\"Pattern match failure - pattern: {nc.pattern} flags:{nc.flags}\"",
"f\" string: {lex}\"",
"else",
":",
"cntxt",
".",
"fail_reason",
"=",
"\"Programming error - flame the programmer\"",
"return",
"False",
"else",
":",
"return",
"True"
] | `5.4.5 XML Schema String Facet Constraints <ttp://shex.io/shex-semantics/#xs-string>`_
String facet constraints apply to the lexical form of the RDF Literals and IRIs and blank node
identifiers (see note below regarding access to blank node identifiers). | [
"5",
".",
"4",
".",
"5",
"XML",
"Schema",
"String",
"Facet",
"Constraints",
"<ttp",
":",
"//",
"shex",
".",
"io",
"/",
"shex",
"-",
"semantics",
"/",
"#xs",
"-",
"string",
">",
"_"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/shape_expressions_language/p5_4_node_constraints.py#L89-L139 | train |
hsolbrig/PyShEx | pyshex/shape_expressions_language/p5_4_node_constraints.py | nodeSatisfiesNumericFacet | def nodeSatisfiesNumericFacet(cntxt: Context, n: Node, nc: ShExJ.NodeConstraint, _c: DebugContext) -> bool:
""" `5.4.5 XML Schema Numeric Facet Constraints <http://shex.io/shex-semantics/#xs-numeric>`_
Numeric facet constraints apply to the numeric value of RDF Literals with datatypes listed in SPARQL 1.1
Operand Data Types[sparql11-query]. Numeric constraints on non-numeric values fail. totaldigits and
fractiondigits constraints on values not derived from xsd:decimal fail.
"""
if nc.mininclusive is not None or nc.minexclusive is not None or nc.maxinclusive is not None \
or nc.maxexclusive is not None or nc.totaldigits is not None or nc.fractiondigits is not None:
if is_numeric(n):
v = n.value
if isinstance(v, numbers.Number):
if (nc.mininclusive is None or v >= nc.mininclusive) and \
(nc.minexclusive is None or v > nc.minexclusive) and \
(nc.maxinclusive is None or v <= nc.maxinclusive) and \
(nc.maxexclusive is None or v < nc.maxexclusive) and \
(nc.totaldigits is None or (total_digits(n) is not None and
total_digits(n) <= nc.totaldigits)) and \
(nc.fractiondigits is None or (fraction_digits(n) is not None and
fraction_digits(n) <= nc.fractiondigits)):
return True
else:
if nc.mininclusive is not None and v < nc.mininclusive:
cntxt.fail_reason = f"Numeric value volation - minimum inclusive: " \
f"{nc.mininclusive} actual: {v}"
elif nc.minexclusive is not None and v <= nc.minexclusive:
cntxt.fail_reason = f"Numeric value volation - minimum exclusive: " \
f"{nc.minexclusive} actual: {v}"
elif nc.maxinclusive is not None and v > nc.maxinclusive:
cntxt.fail_reason = f"Numeric value volation - maximum inclusive: " \
f"{nc.maxinclusive} actual: {v}"
elif nc.maxexclusive is not None and v >= nc.maxexclusive:
cntxt.fail_reason = f"Numeric value volation - maximum exclusive: " \
f"{nc.maxexclusive} actual: {v}"
elif nc.totaldigits is not None and (total_digits(n) is None or
total_digits(n) > nc.totaldigits):
cntxt.fail_reason = f"Numeric value volation - max total digits: " \
f"{nc.totaldigits} value: {v}"
elif nc.fractiondigits is not None and (fraction_digits(n) is None or
total_digits(n) > nc.fractiondigits):
cntxt.fail_reason = f"Numeric value volation - max fractional digits: " \
f"{nc.fractiondigits} value: {v}"
else:
cntxt.fail_reason = "Impossible error - kick the programmer"
return False
else:
cntxt.fail_reason = "Numeric test on non-number: {v}"
return False
else:
cntxt.fail_reason = "Numeric test on non-number: {n}"
return False
return True | python | def nodeSatisfiesNumericFacet(cntxt: Context, n: Node, nc: ShExJ.NodeConstraint, _c: DebugContext) -> bool:
""" `5.4.5 XML Schema Numeric Facet Constraints <http://shex.io/shex-semantics/#xs-numeric>`_
Numeric facet constraints apply to the numeric value of RDF Literals with datatypes listed in SPARQL 1.1
Operand Data Types[sparql11-query]. Numeric constraints on non-numeric values fail. totaldigits and
fractiondigits constraints on values not derived from xsd:decimal fail.
"""
if nc.mininclusive is not None or nc.minexclusive is not None or nc.maxinclusive is not None \
or nc.maxexclusive is not None or nc.totaldigits is not None or nc.fractiondigits is not None:
if is_numeric(n):
v = n.value
if isinstance(v, numbers.Number):
if (nc.mininclusive is None or v >= nc.mininclusive) and \
(nc.minexclusive is None or v > nc.minexclusive) and \
(nc.maxinclusive is None or v <= nc.maxinclusive) and \
(nc.maxexclusive is None or v < nc.maxexclusive) and \
(nc.totaldigits is None or (total_digits(n) is not None and
total_digits(n) <= nc.totaldigits)) and \
(nc.fractiondigits is None or (fraction_digits(n) is not None and
fraction_digits(n) <= nc.fractiondigits)):
return True
else:
if nc.mininclusive is not None and v < nc.mininclusive:
cntxt.fail_reason = f"Numeric value volation - minimum inclusive: " \
f"{nc.mininclusive} actual: {v}"
elif nc.minexclusive is not None and v <= nc.minexclusive:
cntxt.fail_reason = f"Numeric value volation - minimum exclusive: " \
f"{nc.minexclusive} actual: {v}"
elif nc.maxinclusive is not None and v > nc.maxinclusive:
cntxt.fail_reason = f"Numeric value volation - maximum inclusive: " \
f"{nc.maxinclusive} actual: {v}"
elif nc.maxexclusive is not None and v >= nc.maxexclusive:
cntxt.fail_reason = f"Numeric value volation - maximum exclusive: " \
f"{nc.maxexclusive} actual: {v}"
elif nc.totaldigits is not None and (total_digits(n) is None or
total_digits(n) > nc.totaldigits):
cntxt.fail_reason = f"Numeric value volation - max total digits: " \
f"{nc.totaldigits} value: {v}"
elif nc.fractiondigits is not None and (fraction_digits(n) is None or
total_digits(n) > nc.fractiondigits):
cntxt.fail_reason = f"Numeric value volation - max fractional digits: " \
f"{nc.fractiondigits} value: {v}"
else:
cntxt.fail_reason = "Impossible error - kick the programmer"
return False
else:
cntxt.fail_reason = "Numeric test on non-number: {v}"
return False
else:
cntxt.fail_reason = "Numeric test on non-number: {n}"
return False
return True | [
"def",
"nodeSatisfiesNumericFacet",
"(",
"cntxt",
":",
"Context",
",",
"n",
":",
"Node",
",",
"nc",
":",
"ShExJ",
".",
"NodeConstraint",
",",
"_c",
":",
"DebugContext",
")",
"->",
"bool",
":",
"if",
"nc",
".",
"mininclusive",
"is",
"not",
"None",
"or",
"nc",
".",
"minexclusive",
"is",
"not",
"None",
"or",
"nc",
".",
"maxinclusive",
"is",
"not",
"None",
"or",
"nc",
".",
"maxexclusive",
"is",
"not",
"None",
"or",
"nc",
".",
"totaldigits",
"is",
"not",
"None",
"or",
"nc",
".",
"fractiondigits",
"is",
"not",
"None",
":",
"if",
"is_numeric",
"(",
"n",
")",
":",
"v",
"=",
"n",
".",
"value",
"if",
"isinstance",
"(",
"v",
",",
"numbers",
".",
"Number",
")",
":",
"if",
"(",
"nc",
".",
"mininclusive",
"is",
"None",
"or",
"v",
">=",
"nc",
".",
"mininclusive",
")",
"and",
"(",
"nc",
".",
"minexclusive",
"is",
"None",
"or",
"v",
">",
"nc",
".",
"minexclusive",
")",
"and",
"(",
"nc",
".",
"maxinclusive",
"is",
"None",
"or",
"v",
"<=",
"nc",
".",
"maxinclusive",
")",
"and",
"(",
"nc",
".",
"maxexclusive",
"is",
"None",
"or",
"v",
"<",
"nc",
".",
"maxexclusive",
")",
"and",
"(",
"nc",
".",
"totaldigits",
"is",
"None",
"or",
"(",
"total_digits",
"(",
"n",
")",
"is",
"not",
"None",
"and",
"total_digits",
"(",
"n",
")",
"<=",
"nc",
".",
"totaldigits",
")",
")",
"and",
"(",
"nc",
".",
"fractiondigits",
"is",
"None",
"or",
"(",
"fraction_digits",
"(",
"n",
")",
"is",
"not",
"None",
"and",
"fraction_digits",
"(",
"n",
")",
"<=",
"nc",
".",
"fractiondigits",
")",
")",
":",
"return",
"True",
"else",
":",
"if",
"nc",
".",
"mininclusive",
"is",
"not",
"None",
"and",
"v",
"<",
"nc",
".",
"mininclusive",
":",
"cntxt",
".",
"fail_reason",
"=",
"f\"Numeric value volation - minimum inclusive: \"",
"f\"{nc.mininclusive} actual: {v}\"",
"elif",
"nc",
".",
"minexclusive",
"is",
"not",
"None",
"and",
"v",
"<=",
"nc",
".",
"minexclusive",
":",
"cntxt",
".",
"fail_reason",
"=",
"f\"Numeric value volation - minimum exclusive: \"",
"f\"{nc.minexclusive} actual: {v}\"",
"elif",
"nc",
".",
"maxinclusive",
"is",
"not",
"None",
"and",
"v",
">",
"nc",
".",
"maxinclusive",
":",
"cntxt",
".",
"fail_reason",
"=",
"f\"Numeric value volation - maximum inclusive: \"",
"f\"{nc.maxinclusive} actual: {v}\"",
"elif",
"nc",
".",
"maxexclusive",
"is",
"not",
"None",
"and",
"v",
">=",
"nc",
".",
"maxexclusive",
":",
"cntxt",
".",
"fail_reason",
"=",
"f\"Numeric value volation - maximum exclusive: \"",
"f\"{nc.maxexclusive} actual: {v}\"",
"elif",
"nc",
".",
"totaldigits",
"is",
"not",
"None",
"and",
"(",
"total_digits",
"(",
"n",
")",
"is",
"None",
"or",
"total_digits",
"(",
"n",
")",
">",
"nc",
".",
"totaldigits",
")",
":",
"cntxt",
".",
"fail_reason",
"=",
"f\"Numeric value volation - max total digits: \"",
"f\"{nc.totaldigits} value: {v}\"",
"elif",
"nc",
".",
"fractiondigits",
"is",
"not",
"None",
"and",
"(",
"fraction_digits",
"(",
"n",
")",
"is",
"None",
"or",
"total_digits",
"(",
"n",
")",
">",
"nc",
".",
"fractiondigits",
")",
":",
"cntxt",
".",
"fail_reason",
"=",
"f\"Numeric value volation - max fractional digits: \"",
"f\"{nc.fractiondigits} value: {v}\"",
"else",
":",
"cntxt",
".",
"fail_reason",
"=",
"\"Impossible error - kick the programmer\"",
"return",
"False",
"else",
":",
"cntxt",
".",
"fail_reason",
"=",
"\"Numeric test on non-number: {v}\"",
"return",
"False",
"else",
":",
"cntxt",
".",
"fail_reason",
"=",
"\"Numeric test on non-number: {n}\"",
"return",
"False",
"return",
"True"
] | `5.4.5 XML Schema Numeric Facet Constraints <http://shex.io/shex-semantics/#xs-numeric>`_
Numeric facet constraints apply to the numeric value of RDF Literals with datatypes listed in SPARQL 1.1
Operand Data Types[sparql11-query]. Numeric constraints on non-numeric values fail. totaldigits and
fractiondigits constraints on values not derived from xsd:decimal fail. | [
"5",
".",
"4",
".",
"5",
"XML",
"Schema",
"Numeric",
"Facet",
"Constraints",
"<http",
":",
"//",
"shex",
".",
"io",
"/",
"shex",
"-",
"semantics",
"/",
"#xs",
"-",
"numeric",
">",
"_"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/shape_expressions_language/p5_4_node_constraints.py#L145-L196 | train |
hsolbrig/PyShEx | pyshex/shape_expressions_language/p5_4_node_constraints.py | nodeSatisfiesValues | def nodeSatisfiesValues(cntxt: Context, n: Node, nc: ShExJ.NodeConstraint, _c: DebugContext) -> bool:
""" `5.4.5 Values Constraint <http://shex.io/shex-semantics/#values>`_
For a node n and constraint value v, nodeSatisfies(n, v) if n matches some valueSetValue vsv in v.
"""
if nc.values is None:
return True
else:
if any(_nodeSatisfiesValue(cntxt, n, vsv) for vsv in nc.values):
return True
else:
cntxt.fail_reason = f"Node: {cntxt.n3_mapper.n3(n)} not in value set:\n\t " \
f"{as_json(cntxt.type_last(nc), indent=None)[:60]}..."
return False | python | def nodeSatisfiesValues(cntxt: Context, n: Node, nc: ShExJ.NodeConstraint, _c: DebugContext) -> bool:
""" `5.4.5 Values Constraint <http://shex.io/shex-semantics/#values>`_
For a node n and constraint value v, nodeSatisfies(n, v) if n matches some valueSetValue vsv in v.
"""
if nc.values is None:
return True
else:
if any(_nodeSatisfiesValue(cntxt, n, vsv) for vsv in nc.values):
return True
else:
cntxt.fail_reason = f"Node: {cntxt.n3_mapper.n3(n)} not in value set:\n\t " \
f"{as_json(cntxt.type_last(nc), indent=None)[:60]}..."
return False | [
"def",
"nodeSatisfiesValues",
"(",
"cntxt",
":",
"Context",
",",
"n",
":",
"Node",
",",
"nc",
":",
"ShExJ",
".",
"NodeConstraint",
",",
"_c",
":",
"DebugContext",
")",
"->",
"bool",
":",
"if",
"nc",
".",
"values",
"is",
"None",
":",
"return",
"True",
"else",
":",
"if",
"any",
"(",
"_nodeSatisfiesValue",
"(",
"cntxt",
",",
"n",
",",
"vsv",
")",
"for",
"vsv",
"in",
"nc",
".",
"values",
")",
":",
"return",
"True",
"else",
":",
"cntxt",
".",
"fail_reason",
"=",
"f\"Node: {cntxt.n3_mapper.n3(n)} not in value set:\\n\\t \"",
"f\"{as_json(cntxt.type_last(nc), indent=None)[:60]}...\"",
"return",
"False"
] | `5.4.5 Values Constraint <http://shex.io/shex-semantics/#values>`_
For a node n and constraint value v, nodeSatisfies(n, v) if n matches some valueSetValue vsv in v. | [
"5",
".",
"4",
".",
"5",
"Values",
"Constraint",
"<http",
":",
"//",
"shex",
".",
"io",
"/",
"shex",
"-",
"semantics",
"/",
"#values",
">",
"_"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/shape_expressions_language/p5_4_node_constraints.py#L200-L213 | train |
hsolbrig/PyShEx | pyshex/shape_expressions_language/p5_4_node_constraints.py | _nodeSatisfiesValue | def _nodeSatisfiesValue(cntxt: Context, n: Node, vsv: ShExJ.valueSetValue) -> bool:
"""
A term matches a valueSetValue if:
* vsv is an objectValue and n = vsv.
* vsv is a Language with langTag lt and n is a language-tagged string with a language tag l and l = lt.
* vsv is a IriStem, LiteralStem or LanguageStem with stem st and nodeIn(n, st).
* vsv is a IriStemRange, LiteralStemRange or LanguageStemRange with stem st and exclusions excls and
nodeIn(n, st) and there is no x in excls such that nodeIn(n, excl).
* vsv is a Wildcard with exclusions excls and there is no x in excls such that nodeIn(n, excl).
Note that ObjectLiteral is *not* typed in ShExJ.jsg, so we identify it by a lack of a 'type' variable
.. note:: Mismatch with spec
This won't work correctly if the stem value is passed in to nodeIn, as there will be no way to know whether
we're matching an IRI or other type
... note:: Language issue
The stem range spec shouldn't have the first element in the exclusions
"""
vsv = map_object_literal(vsv)
if isinstance_(vsv, ShExJ.objectValue):
return objectValueMatches(n, vsv)
if isinstance(vsv, ShExJ.Language):
if vsv.languageTag is not None and isinstance(n, Literal) and n.language is not None:
return n.language == vsv.languageTag
else:
return False
if isinstance(vsv, ShExJ.IriStem):
return nodeInIriStem(cntxt, n, vsv.stem)
if isinstance(vsv, ShExJ.IriStemRange):
exclusions = vsv.exclusions if vsv.exclusions is not None else []
return nodeInIriStem(cntxt, n, vsv.stem) and not any(
(uriref_matches_iriref(n, excl) if isinstance(excl, ShExJ.IRIREF) else
uriref_startswith_iriref(n, excl.stem)) for excl in exclusions)
if isinstance(vsv, ShExJ.LiteralStem):
return nodeInLiteralStem(cntxt, n, vsv.stem)
if isinstance(vsv, ShExJ.LiteralStemRange):
exclusions = vsv.exclusions if vsv.exclusions is not None else []
return nodeInLiteralStem(cntxt, n, vsv.stem) and not any(str(n) == excl for excl in exclusions)
if isinstance(vsv, ShExJ.LanguageStem):
return nodeInLanguageStem(cntxt, n, vsv.stem)
if isinstance(vsv, ShExJ.LanguageStemRange):
exclusions = vsv.exclusions if vsv.exclusions is not None else []
return nodeInLanguageStem(cntxt, n, vsv.stem) and not any(str(n) == str(excl) for excl in exclusions)
return False | python | def _nodeSatisfiesValue(cntxt: Context, n: Node, vsv: ShExJ.valueSetValue) -> bool:
"""
A term matches a valueSetValue if:
* vsv is an objectValue and n = vsv.
* vsv is a Language with langTag lt and n is a language-tagged string with a language tag l and l = lt.
* vsv is a IriStem, LiteralStem or LanguageStem with stem st and nodeIn(n, st).
* vsv is a IriStemRange, LiteralStemRange or LanguageStemRange with stem st and exclusions excls and
nodeIn(n, st) and there is no x in excls such that nodeIn(n, excl).
* vsv is a Wildcard with exclusions excls and there is no x in excls such that nodeIn(n, excl).
Note that ObjectLiteral is *not* typed in ShExJ.jsg, so we identify it by a lack of a 'type' variable
.. note:: Mismatch with spec
This won't work correctly if the stem value is passed in to nodeIn, as there will be no way to know whether
we're matching an IRI or other type
... note:: Language issue
The stem range spec shouldn't have the first element in the exclusions
"""
vsv = map_object_literal(vsv)
if isinstance_(vsv, ShExJ.objectValue):
return objectValueMatches(n, vsv)
if isinstance(vsv, ShExJ.Language):
if vsv.languageTag is not None and isinstance(n, Literal) and n.language is not None:
return n.language == vsv.languageTag
else:
return False
if isinstance(vsv, ShExJ.IriStem):
return nodeInIriStem(cntxt, n, vsv.stem)
if isinstance(vsv, ShExJ.IriStemRange):
exclusions = vsv.exclusions if vsv.exclusions is not None else []
return nodeInIriStem(cntxt, n, vsv.stem) and not any(
(uriref_matches_iriref(n, excl) if isinstance(excl, ShExJ.IRIREF) else
uriref_startswith_iriref(n, excl.stem)) for excl in exclusions)
if isinstance(vsv, ShExJ.LiteralStem):
return nodeInLiteralStem(cntxt, n, vsv.stem)
if isinstance(vsv, ShExJ.LiteralStemRange):
exclusions = vsv.exclusions if vsv.exclusions is not None else []
return nodeInLiteralStem(cntxt, n, vsv.stem) and not any(str(n) == excl for excl in exclusions)
if isinstance(vsv, ShExJ.LanguageStem):
return nodeInLanguageStem(cntxt, n, vsv.stem)
if isinstance(vsv, ShExJ.LanguageStemRange):
exclusions = vsv.exclusions if vsv.exclusions is not None else []
return nodeInLanguageStem(cntxt, n, vsv.stem) and not any(str(n) == str(excl) for excl in exclusions)
return False | [
"def",
"_nodeSatisfiesValue",
"(",
"cntxt",
":",
"Context",
",",
"n",
":",
"Node",
",",
"vsv",
":",
"ShExJ",
".",
"valueSetValue",
")",
"->",
"bool",
":",
"vsv",
"=",
"map_object_literal",
"(",
"vsv",
")",
"if",
"isinstance_",
"(",
"vsv",
",",
"ShExJ",
".",
"objectValue",
")",
":",
"return",
"objectValueMatches",
"(",
"n",
",",
"vsv",
")",
"if",
"isinstance",
"(",
"vsv",
",",
"ShExJ",
".",
"Language",
")",
":",
"if",
"vsv",
".",
"languageTag",
"is",
"not",
"None",
"and",
"isinstance",
"(",
"n",
",",
"Literal",
")",
"and",
"n",
".",
"language",
"is",
"not",
"None",
":",
"return",
"n",
".",
"language",
"==",
"vsv",
".",
"languageTag",
"else",
":",
"return",
"False",
"if",
"isinstance",
"(",
"vsv",
",",
"ShExJ",
".",
"IriStem",
")",
":",
"return",
"nodeInIriStem",
"(",
"cntxt",
",",
"n",
",",
"vsv",
".",
"stem",
")",
"if",
"isinstance",
"(",
"vsv",
",",
"ShExJ",
".",
"IriStemRange",
")",
":",
"exclusions",
"=",
"vsv",
".",
"exclusions",
"if",
"vsv",
".",
"exclusions",
"is",
"not",
"None",
"else",
"[",
"]",
"return",
"nodeInIriStem",
"(",
"cntxt",
",",
"n",
",",
"vsv",
".",
"stem",
")",
"and",
"not",
"any",
"(",
"(",
"uriref_matches_iriref",
"(",
"n",
",",
"excl",
")",
"if",
"isinstance",
"(",
"excl",
",",
"ShExJ",
".",
"IRIREF",
")",
"else",
"uriref_startswith_iriref",
"(",
"n",
",",
"excl",
".",
"stem",
")",
")",
"for",
"excl",
"in",
"exclusions",
")",
"if",
"isinstance",
"(",
"vsv",
",",
"ShExJ",
".",
"LiteralStem",
")",
":",
"return",
"nodeInLiteralStem",
"(",
"cntxt",
",",
"n",
",",
"vsv",
".",
"stem",
")",
"if",
"isinstance",
"(",
"vsv",
",",
"ShExJ",
".",
"LiteralStemRange",
")",
":",
"exclusions",
"=",
"vsv",
".",
"exclusions",
"if",
"vsv",
".",
"exclusions",
"is",
"not",
"None",
"else",
"[",
"]",
"return",
"nodeInLiteralStem",
"(",
"cntxt",
",",
"n",
",",
"vsv",
".",
"stem",
")",
"and",
"not",
"any",
"(",
"str",
"(",
"n",
")",
"==",
"excl",
"for",
"excl",
"in",
"exclusions",
")",
"if",
"isinstance",
"(",
"vsv",
",",
"ShExJ",
".",
"LanguageStem",
")",
":",
"return",
"nodeInLanguageStem",
"(",
"cntxt",
",",
"n",
",",
"vsv",
".",
"stem",
")",
"if",
"isinstance",
"(",
"vsv",
",",
"ShExJ",
".",
"LanguageStemRange",
")",
":",
"exclusions",
"=",
"vsv",
".",
"exclusions",
"if",
"vsv",
".",
"exclusions",
"is",
"not",
"None",
"else",
"[",
"]",
"return",
"nodeInLanguageStem",
"(",
"cntxt",
",",
"n",
",",
"vsv",
".",
"stem",
")",
"and",
"not",
"any",
"(",
"str",
"(",
"n",
")",
"==",
"str",
"(",
"excl",
")",
"for",
"excl",
"in",
"exclusions",
")",
"return",
"False"
] | A term matches a valueSetValue if:
* vsv is an objectValue and n = vsv.
* vsv is a Language with langTag lt and n is a language-tagged string with a language tag l and l = lt.
* vsv is a IriStem, LiteralStem or LanguageStem with stem st and nodeIn(n, st).
* vsv is a IriStemRange, LiteralStemRange or LanguageStemRange with stem st and exclusions excls and
nodeIn(n, st) and there is no x in excls such that nodeIn(n, excl).
* vsv is a Wildcard with exclusions excls and there is no x in excls such that nodeIn(n, excl).
Note that ObjectLiteral is *not* typed in ShExJ.jsg, so we identify it by a lack of a 'type' variable
.. note:: Mismatch with spec
This won't work correctly if the stem value is passed in to nodeIn, as there will be no way to know whether
we're matching an IRI or other type
... note:: Language issue
The stem range spec shouldn't have the first element in the exclusions | [
"A",
"term",
"matches",
"a",
"valueSetValue",
"if",
":",
"*",
"vsv",
"is",
"an",
"objectValue",
"and",
"n",
"=",
"vsv",
".",
"*",
"vsv",
"is",
"a",
"Language",
"with",
"langTag",
"lt",
"and",
"n",
"is",
"a",
"language",
"-",
"tagged",
"string",
"with",
"a",
"language",
"tag",
"l",
"and",
"l",
"=",
"lt",
".",
"*",
"vsv",
"is",
"a",
"IriStem",
"LiteralStem",
"or",
"LanguageStem",
"with",
"stem",
"st",
"and",
"nodeIn",
"(",
"n",
"st",
")",
".",
"*",
"vsv",
"is",
"a",
"IriStemRange",
"LiteralStemRange",
"or",
"LanguageStemRange",
"with",
"stem",
"st",
"and",
"exclusions",
"excls",
"and",
"nodeIn",
"(",
"n",
"st",
")",
"and",
"there",
"is",
"no",
"x",
"in",
"excls",
"such",
"that",
"nodeIn",
"(",
"n",
"excl",
")",
".",
"*",
"vsv",
"is",
"a",
"Wildcard",
"with",
"exclusions",
"excls",
"and",
"there",
"is",
"no",
"x",
"in",
"excls",
"such",
"that",
"nodeIn",
"(",
"n",
"excl",
")",
"."
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/shape_expressions_language/p5_4_node_constraints.py#L216-L269 | train |
hsolbrig/PyShEx | pyshex/shape_expressions_language/p5_4_node_constraints.py | nodeInIriStem | def nodeInIriStem(_: Context, n: Node, s: ShExJ.IriStem) -> bool:
"""
**nodeIn**: asserts that an RDF node n is equal to an RDF term s or is in a set defined by a
:py:class:`ShExJ.IriStem`, :py:class:`LiteralStem` or :py:class:`LanguageStem`.
The expression `nodeInIriStem(n, s)` is satisfied iff:
#) `s` is a :py:class:`ShExJ.WildCard` or
#) `n` is an :py:class:`rdflib.URIRef` and fn:starts-with(`n`, `s`)
"""
return isinstance(s, ShExJ.Wildcard) or \
(isinstance(n, URIRef) and uriref_startswith_iriref(n, str(s))) | python | def nodeInIriStem(_: Context, n: Node, s: ShExJ.IriStem) -> bool:
"""
**nodeIn**: asserts that an RDF node n is equal to an RDF term s or is in a set defined by a
:py:class:`ShExJ.IriStem`, :py:class:`LiteralStem` or :py:class:`LanguageStem`.
The expression `nodeInIriStem(n, s)` is satisfied iff:
#) `s` is a :py:class:`ShExJ.WildCard` or
#) `n` is an :py:class:`rdflib.URIRef` and fn:starts-with(`n`, `s`)
"""
return isinstance(s, ShExJ.Wildcard) or \
(isinstance(n, URIRef) and uriref_startswith_iriref(n, str(s))) | [
"def",
"nodeInIriStem",
"(",
"_",
":",
"Context",
",",
"n",
":",
"Node",
",",
"s",
":",
"ShExJ",
".",
"IriStem",
")",
"->",
"bool",
":",
"return",
"isinstance",
"(",
"s",
",",
"ShExJ",
".",
"Wildcard",
")",
"or",
"(",
"isinstance",
"(",
"n",
",",
"URIRef",
")",
"and",
"uriref_startswith_iriref",
"(",
"n",
",",
"str",
"(",
"s",
")",
")",
")"
] | **nodeIn**: asserts that an RDF node n is equal to an RDF term s or is in a set defined by a
:py:class:`ShExJ.IriStem`, :py:class:`LiteralStem` or :py:class:`LanguageStem`.
The expression `nodeInIriStem(n, s)` is satisfied iff:
#) `s` is a :py:class:`ShExJ.WildCard` or
#) `n` is an :py:class:`rdflib.URIRef` and fn:starts-with(`n`, `s`) | [
"**",
"nodeIn",
"**",
":",
"asserts",
"that",
"an",
"RDF",
"node",
"n",
"is",
"equal",
"to",
"an",
"RDF",
"term",
"s",
"or",
"is",
"in",
"a",
"set",
"defined",
"by",
"a",
":",
"py",
":",
"class",
":",
"ShExJ",
".",
"IriStem",
":",
"py",
":",
"class",
":",
"LiteralStem",
"or",
":",
"py",
":",
"class",
":",
"LanguageStem",
"."
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/shape_expressions_language/p5_4_node_constraints.py#L272-L282 | train |
hsolbrig/PyShEx | pyshex/shape_expressions_language/p5_4_node_constraints.py | nodeInLiteralStem | def nodeInLiteralStem(_: Context, n: Node, s: ShExJ.LiteralStem) -> bool:
""" http://shex.io/shex-semantics/#values
**nodeIn**: asserts that an RDF node n is equal to an RDF term s or is in a set defined by a
:py:class:`ShExJ.IriStem`, :py:class:`LiteralStem` or :py:class:`LanguageStem`.
The expression `nodeInLiteralStem(n, s)` is satisfied iff:
#) `s` is a :py:class:`ShExJ.WildCard` or
#) `n` is an :py:class:`rdflib.Literal` and fn:starts-with(`n`, `s`)
"""
return isinstance(s, ShExJ.Wildcard) or \
(isinstance(n, Literal) and str(n.value).startswith(str(s))) | python | def nodeInLiteralStem(_: Context, n: Node, s: ShExJ.LiteralStem) -> bool:
""" http://shex.io/shex-semantics/#values
**nodeIn**: asserts that an RDF node n is equal to an RDF term s or is in a set defined by a
:py:class:`ShExJ.IriStem`, :py:class:`LiteralStem` or :py:class:`LanguageStem`.
The expression `nodeInLiteralStem(n, s)` is satisfied iff:
#) `s` is a :py:class:`ShExJ.WildCard` or
#) `n` is an :py:class:`rdflib.Literal` and fn:starts-with(`n`, `s`)
"""
return isinstance(s, ShExJ.Wildcard) or \
(isinstance(n, Literal) and str(n.value).startswith(str(s))) | [
"def",
"nodeInLiteralStem",
"(",
"_",
":",
"Context",
",",
"n",
":",
"Node",
",",
"s",
":",
"ShExJ",
".",
"LiteralStem",
")",
"->",
"bool",
":",
"return",
"isinstance",
"(",
"s",
",",
"ShExJ",
".",
"Wildcard",
")",
"or",
"(",
"isinstance",
"(",
"n",
",",
"Literal",
")",
"and",
"str",
"(",
"n",
".",
"value",
")",
".",
"startswith",
"(",
"str",
"(",
"s",
")",
")",
")"
] | http://shex.io/shex-semantics/#values
**nodeIn**: asserts that an RDF node n is equal to an RDF term s or is in a set defined by a
:py:class:`ShExJ.IriStem`, :py:class:`LiteralStem` or :py:class:`LanguageStem`.
The expression `nodeInLiteralStem(n, s)` is satisfied iff:
#) `s` is a :py:class:`ShExJ.WildCard` or
#) `n` is an :py:class:`rdflib.Literal` and fn:starts-with(`n`, `s`) | [
"http",
":",
"//",
"shex",
".",
"io",
"/",
"shex",
"-",
"semantics",
"/",
"#values"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/shape_expressions_language/p5_4_node_constraints.py#L285-L296 | train |
hsolbrig/PyShEx | pyshex/shape_expressions_language/p5_4_node_constraints.py | nodeInLanguageStem | def nodeInLanguageStem(_: Context, n: Node, s: ShExJ.LanguageStem) -> bool:
""" http://shex.io/shex-semantics/#values
**nodeIn**: asserts that an RDF node n is equal to an RDF term s or is in a set defined by a
:py:class:`ShExJ.IriStem`, :py:class:`LiteralStem` or :py:class:`LanguageStem`.
The expression `nodeInLanguageStem(n, s)` is satisfied iff:
#) `s` is a :py:class:`ShExJ.WildCard` or
#) `n` is a language-tagged string and fn:starts-with(`n.language`, `s`)
"""
return isinstance(s, ShExJ.Wildcard) or \
(isinstance(n, Literal) and n.language is not None and str(n.language).startswith(str(s))) | python | def nodeInLanguageStem(_: Context, n: Node, s: ShExJ.LanguageStem) -> bool:
""" http://shex.io/shex-semantics/#values
**nodeIn**: asserts that an RDF node n is equal to an RDF term s or is in a set defined by a
:py:class:`ShExJ.IriStem`, :py:class:`LiteralStem` or :py:class:`LanguageStem`.
The expression `nodeInLanguageStem(n, s)` is satisfied iff:
#) `s` is a :py:class:`ShExJ.WildCard` or
#) `n` is a language-tagged string and fn:starts-with(`n.language`, `s`)
"""
return isinstance(s, ShExJ.Wildcard) or \
(isinstance(n, Literal) and n.language is not None and str(n.language).startswith(str(s))) | [
"def",
"nodeInLanguageStem",
"(",
"_",
":",
"Context",
",",
"n",
":",
"Node",
",",
"s",
":",
"ShExJ",
".",
"LanguageStem",
")",
"->",
"bool",
":",
"return",
"isinstance",
"(",
"s",
",",
"ShExJ",
".",
"Wildcard",
")",
"or",
"(",
"isinstance",
"(",
"n",
",",
"Literal",
")",
"and",
"n",
".",
"language",
"is",
"not",
"None",
"and",
"str",
"(",
"n",
".",
"language",
")",
".",
"startswith",
"(",
"str",
"(",
"s",
")",
")",
")"
] | http://shex.io/shex-semantics/#values
**nodeIn**: asserts that an RDF node n is equal to an RDF term s or is in a set defined by a
:py:class:`ShExJ.IriStem`, :py:class:`LiteralStem` or :py:class:`LanguageStem`.
The expression `nodeInLanguageStem(n, s)` is satisfied iff:
#) `s` is a :py:class:`ShExJ.WildCard` or
#) `n` is a language-tagged string and fn:starts-with(`n.language`, `s`) | [
"http",
":",
"//",
"shex",
".",
"io",
"/",
"shex",
"-",
"semantics",
"/",
"#values"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/shape_expressions_language/p5_4_node_constraints.py#L299-L310 | train |
hsolbrig/PyShEx | pyshex/shape_expressions_language/p5_4_node_constraints.py | nodeInBnodeStem | def nodeInBnodeStem(_cntxt: Context, _n: Node, _s: Union[str, ShExJ.Wildcard]) -> bool:
""" http://shex.io/shex-semantics/#values
**nodeIn**: asserts that an RDF node n is equal to an RDF term s or is in a set defined by a
:py:class:`ShExJ.IriStem`, :py:class:`LiteralStem` or :py:class:`LanguageStem`.
The expression `nodeInBnodeStem(n, s)` is satisfied iff:
#) `s` is a :py:class:`ShExJ.WildCard` or
#) `n` is a language-tagged string and fn:starts-with(`n.language`, `s`)
"""
# TODO: resolve issue #79 to figure out how to do this
return False | python | def nodeInBnodeStem(_cntxt: Context, _n: Node, _s: Union[str, ShExJ.Wildcard]) -> bool:
""" http://shex.io/shex-semantics/#values
**nodeIn**: asserts that an RDF node n is equal to an RDF term s or is in a set defined by a
:py:class:`ShExJ.IriStem`, :py:class:`LiteralStem` or :py:class:`LanguageStem`.
The expression `nodeInBnodeStem(n, s)` is satisfied iff:
#) `s` is a :py:class:`ShExJ.WildCard` or
#) `n` is a language-tagged string and fn:starts-with(`n.language`, `s`)
"""
# TODO: resolve issue #79 to figure out how to do this
return False | [
"def",
"nodeInBnodeStem",
"(",
"_cntxt",
":",
"Context",
",",
"_n",
":",
"Node",
",",
"_s",
":",
"Union",
"[",
"str",
",",
"ShExJ",
".",
"Wildcard",
"]",
")",
"->",
"bool",
":",
"# TODO: resolve issue #79 to figure out how to do this",
"return",
"False"
] | http://shex.io/shex-semantics/#values
**nodeIn**: asserts that an RDF node n is equal to an RDF term s or is in a set defined by a
:py:class:`ShExJ.IriStem`, :py:class:`LiteralStem` or :py:class:`LanguageStem`.
The expression `nodeInBnodeStem(n, s)` is satisfied iff:
#) `s` is a :py:class:`ShExJ.WildCard` or
#) `n` is a language-tagged string and fn:starts-with(`n.language`, `s`) | [
"http",
":",
"//",
"shex",
".",
"io",
"/",
"shex",
"-",
"semantics",
"/",
"#values"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/shape_expressions_language/p5_4_node_constraints.py#L313-L325 | train |
stuaxo/vext | vext/env/__init__.py | run_in_syspy | def run_in_syspy(f):
"""
Decorator to run a function in the system python
:param f:
:return:
"""
fname = f.__name__
code_lines = inspect.getsource(f).splitlines()
code = dedent("\n".join(code_lines[1:])) # strip this decorator
# add call to the function and print it's result
code += dedent("""\n
import sys
args = sys.argv[1:]
result = {fname}(*args)
print("%r" % result)
""").format(fname=fname)
env = os.environ
python = findsyspy()
logger.debug("Create function for system python\n%s" % code)
def call_f(*args):
cmd = [python, '-c', code] + list(args)
output = subprocess.check_output(cmd, env=env).decode('utf-8')
result = ast.literal_eval(output)
return result
return call_f | python | def run_in_syspy(f):
"""
Decorator to run a function in the system python
:param f:
:return:
"""
fname = f.__name__
code_lines = inspect.getsource(f).splitlines()
code = dedent("\n".join(code_lines[1:])) # strip this decorator
# add call to the function and print it's result
code += dedent("""\n
import sys
args = sys.argv[1:]
result = {fname}(*args)
print("%r" % result)
""").format(fname=fname)
env = os.environ
python = findsyspy()
logger.debug("Create function for system python\n%s" % code)
def call_f(*args):
cmd = [python, '-c', code] + list(args)
output = subprocess.check_output(cmd, env=env).decode('utf-8')
result = ast.literal_eval(output)
return result
return call_f | [
"def",
"run_in_syspy",
"(",
"f",
")",
":",
"fname",
"=",
"f",
".",
"__name__",
"code_lines",
"=",
"inspect",
".",
"getsource",
"(",
"f",
")",
".",
"splitlines",
"(",
")",
"code",
"=",
"dedent",
"(",
"\"\\n\"",
".",
"join",
"(",
"code_lines",
"[",
"1",
":",
"]",
")",
")",
"# strip this decorator",
"# add call to the function and print it's result",
"code",
"+=",
"dedent",
"(",
"\"\"\"\\n\n import sys\n args = sys.argv[1:]\n result = {fname}(*args)\n print(\"%r\" % result)\n \"\"\"",
")",
".",
"format",
"(",
"fname",
"=",
"fname",
")",
"env",
"=",
"os",
".",
"environ",
"python",
"=",
"findsyspy",
"(",
")",
"logger",
".",
"debug",
"(",
"\"Create function for system python\\n%s\"",
"%",
"code",
")",
"def",
"call_f",
"(",
"*",
"args",
")",
":",
"cmd",
"=",
"[",
"python",
",",
"'-c'",
",",
"code",
"]",
"+",
"list",
"(",
"args",
")",
"output",
"=",
"subprocess",
".",
"check_output",
"(",
"cmd",
",",
"env",
"=",
"env",
")",
".",
"decode",
"(",
"'utf-8'",
")",
"result",
"=",
"ast",
".",
"literal_eval",
"(",
"output",
")",
"return",
"result",
"return",
"call_f"
] | Decorator to run a function in the system python
:param f:
:return: | [
"Decorator",
"to",
"run",
"a",
"function",
"in",
"the",
"system",
"python"
] | fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69 | https://github.com/stuaxo/vext/blob/fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69/vext/env/__init__.py#L16-L46 | train |
stuaxo/vext | vext/env/__init__.py | in_venv | def in_venv():
"""
:return: True if in running from a virtualenv
Has to detect the case where the python binary is run
directly, so VIRTUAL_ENV may not be set
"""
global _in_venv
if _in_venv is not None:
return _in_venv
if not (os.path.isfile(ORIG_PREFIX_TXT) or os.path.isfile(PY_VENV_CFG)):
logger.debug("in_venv no orig_prefix_txt [%s]", ORIG_PREFIX_TXT)
logger.debug("in_venv no py_venv_cfg [%s]", PY_VENV_CFG)
# TODO - check this is actually valid !
_in_venv = False
return _in_venv
if 'VIRTUAL_ENV' in os.environ:
logger.debug("in_venv VIRTUAL_ENV set.")
_in_venv = True
else:
# Find first python in path ... if its not this one,
# ...we are in a different python
python = basename(sys.executable)
for p in os.environ['PATH'].split(os.pathsep):
py_path = join(p, python)
if isfile(py_path):
logger.debug("in_venv py_at [%s] return: %s", (py_path, sys.executable != py_path))
_in_venv = sys.executable != py_path
break
return _in_venv | python | def in_venv():
"""
:return: True if in running from a virtualenv
Has to detect the case where the python binary is run
directly, so VIRTUAL_ENV may not be set
"""
global _in_venv
if _in_venv is not None:
return _in_venv
if not (os.path.isfile(ORIG_PREFIX_TXT) or os.path.isfile(PY_VENV_CFG)):
logger.debug("in_venv no orig_prefix_txt [%s]", ORIG_PREFIX_TXT)
logger.debug("in_venv no py_venv_cfg [%s]", PY_VENV_CFG)
# TODO - check this is actually valid !
_in_venv = False
return _in_venv
if 'VIRTUAL_ENV' in os.environ:
logger.debug("in_venv VIRTUAL_ENV set.")
_in_venv = True
else:
# Find first python in path ... if its not this one,
# ...we are in a different python
python = basename(sys.executable)
for p in os.environ['PATH'].split(os.pathsep):
py_path = join(p, python)
if isfile(py_path):
logger.debug("in_venv py_at [%s] return: %s", (py_path, sys.executable != py_path))
_in_venv = sys.executable != py_path
break
return _in_venv | [
"def",
"in_venv",
"(",
")",
":",
"global",
"_in_venv",
"if",
"_in_venv",
"is",
"not",
"None",
":",
"return",
"_in_venv",
"if",
"not",
"(",
"os",
".",
"path",
".",
"isfile",
"(",
"ORIG_PREFIX_TXT",
")",
"or",
"os",
".",
"path",
".",
"isfile",
"(",
"PY_VENV_CFG",
")",
")",
":",
"logger",
".",
"debug",
"(",
"\"in_venv no orig_prefix_txt [%s]\"",
",",
"ORIG_PREFIX_TXT",
")",
"logger",
".",
"debug",
"(",
"\"in_venv no py_venv_cfg [%s]\"",
",",
"PY_VENV_CFG",
")",
"# TODO - check this is actually valid !",
"_in_venv",
"=",
"False",
"return",
"_in_venv",
"if",
"'VIRTUAL_ENV'",
"in",
"os",
".",
"environ",
":",
"logger",
".",
"debug",
"(",
"\"in_venv VIRTUAL_ENV set.\"",
")",
"_in_venv",
"=",
"True",
"else",
":",
"# Find first python in path ... if its not this one,",
"# ...we are in a different python",
"python",
"=",
"basename",
"(",
"sys",
".",
"executable",
")",
"for",
"p",
"in",
"os",
".",
"environ",
"[",
"'PATH'",
"]",
".",
"split",
"(",
"os",
".",
"pathsep",
")",
":",
"py_path",
"=",
"join",
"(",
"p",
",",
"python",
")",
"if",
"isfile",
"(",
"py_path",
")",
":",
"logger",
".",
"debug",
"(",
"\"in_venv py_at [%s] return: %s\"",
",",
"(",
"py_path",
",",
"sys",
".",
"executable",
"!=",
"py_path",
")",
")",
"_in_venv",
"=",
"sys",
".",
"executable",
"!=",
"py_path",
"break",
"return",
"_in_venv"
] | :return: True if in running from a virtualenv
Has to detect the case where the python binary is run
directly, so VIRTUAL_ENV may not be set | [
":",
"return",
":",
"True",
"if",
"in",
"running",
"from",
"a",
"virtualenv"
] | fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69 | https://github.com/stuaxo/vext/blob/fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69/vext/env/__init__.py#L49-L81 | train |
stuaxo/vext | vext/env/__init__.py | getsyssitepackages | def getsyssitepackages():
"""
:return: list of site-packages from system python
"""
global _syssitepackages
if not _syssitepackages:
if not in_venv():
_syssitepackages = get_python_lib()
return _syssitepackages
@run_in_syspy
def run(*args):
import site
return site.getsitepackages()
output = run()
_syssitepackages = output
logger.debug("system site packages: %s", _syssitepackages)
return _syssitepackages | python | def getsyssitepackages():
"""
:return: list of site-packages from system python
"""
global _syssitepackages
if not _syssitepackages:
if not in_venv():
_syssitepackages = get_python_lib()
return _syssitepackages
@run_in_syspy
def run(*args):
import site
return site.getsitepackages()
output = run()
_syssitepackages = output
logger.debug("system site packages: %s", _syssitepackages)
return _syssitepackages | [
"def",
"getsyssitepackages",
"(",
")",
":",
"global",
"_syssitepackages",
"if",
"not",
"_syssitepackages",
":",
"if",
"not",
"in_venv",
"(",
")",
":",
"_syssitepackages",
"=",
"get_python_lib",
"(",
")",
"return",
"_syssitepackages",
"@",
"run_in_syspy",
"def",
"run",
"(",
"*",
"args",
")",
":",
"import",
"site",
"return",
"site",
".",
"getsitepackages",
"(",
")",
"output",
"=",
"run",
"(",
")",
"_syssitepackages",
"=",
"output",
"logger",
".",
"debug",
"(",
"\"system site packages: %s\"",
",",
"_syssitepackages",
")",
"return",
"_syssitepackages"
] | :return: list of site-packages from system python | [
":",
"return",
":",
"list",
"of",
"site",
"-",
"packages",
"from",
"system",
"python"
] | fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69 | https://github.com/stuaxo/vext/blob/fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69/vext/env/__init__.py#L84-L102 | train |
stuaxo/vext | vext/env/__init__.py | findsyspy | def findsyspy():
"""
:return: system python executable
"""
if not in_venv():
return sys.executable
python = basename(realpath(sys.executable))
prefix = None
if HAS_ORIG_PREFIX_TXT:
with open(ORIG_PREFIX_TXT) as op:
prefix = op.read()
elif HAS_PY_VENV_CFG:
prefix = getattr(sys, "_home")
if not prefix:
return None
for folder in os.environ['PATH'].split(os.pathsep):
if folder and \
normpath(normcase(folder)).startswith(normcase(normpath(prefix))) and \
isfile(join(folder, python)):
return join(folder, python)
# OSX: Homebrew doesn't leave python in the PATH
if isfile(join(prefix, "bin", python)):
return join(prefix, "bin", python) | python | def findsyspy():
"""
:return: system python executable
"""
if not in_venv():
return sys.executable
python = basename(realpath(sys.executable))
prefix = None
if HAS_ORIG_PREFIX_TXT:
with open(ORIG_PREFIX_TXT) as op:
prefix = op.read()
elif HAS_PY_VENV_CFG:
prefix = getattr(sys, "_home")
if not prefix:
return None
for folder in os.environ['PATH'].split(os.pathsep):
if folder and \
normpath(normcase(folder)).startswith(normcase(normpath(prefix))) and \
isfile(join(folder, python)):
return join(folder, python)
# OSX: Homebrew doesn't leave python in the PATH
if isfile(join(prefix, "bin", python)):
return join(prefix, "bin", python) | [
"def",
"findsyspy",
"(",
")",
":",
"if",
"not",
"in_venv",
"(",
")",
":",
"return",
"sys",
".",
"executable",
"python",
"=",
"basename",
"(",
"realpath",
"(",
"sys",
".",
"executable",
")",
")",
"prefix",
"=",
"None",
"if",
"HAS_ORIG_PREFIX_TXT",
":",
"with",
"open",
"(",
"ORIG_PREFIX_TXT",
")",
"as",
"op",
":",
"prefix",
"=",
"op",
".",
"read",
"(",
")",
"elif",
"HAS_PY_VENV_CFG",
":",
"prefix",
"=",
"getattr",
"(",
"sys",
",",
"\"_home\"",
")",
"if",
"not",
"prefix",
":",
"return",
"None",
"for",
"folder",
"in",
"os",
".",
"environ",
"[",
"'PATH'",
"]",
".",
"split",
"(",
"os",
".",
"pathsep",
")",
":",
"if",
"folder",
"and",
"normpath",
"(",
"normcase",
"(",
"folder",
")",
")",
".",
"startswith",
"(",
"normcase",
"(",
"normpath",
"(",
"prefix",
")",
")",
")",
"and",
"isfile",
"(",
"join",
"(",
"folder",
",",
"python",
")",
")",
":",
"return",
"join",
"(",
"folder",
",",
"python",
")",
"# OSX: Homebrew doesn't leave python in the PATH",
"if",
"isfile",
"(",
"join",
"(",
"prefix",
",",
"\"bin\"",
",",
"python",
")",
")",
":",
"return",
"join",
"(",
"prefix",
",",
"\"bin\"",
",",
"python",
")"
] | :return: system python executable | [
":",
"return",
":",
"system",
"python",
"executable"
] | fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69 | https://github.com/stuaxo/vext/blob/fa98a21ecfbbc1c3d1b84085d69ec42defdd2f69/vext/env/__init__.py#L105-L131 | train |
jaysonsantos/python-binary-memcached | bmemcached/client/distributed.py | DistributedClient.delete | def delete(self, key, cas=0):
"""
Delete a key/value from server. If key does not exist, it returns True.
:param key: Key's name to be deleted
:param cas: CAS of the key
:return: True in case o success and False in case of failure.
"""
server = self._get_server(key)
return server.delete(key, cas) | python | def delete(self, key, cas=0):
"""
Delete a key/value from server. If key does not exist, it returns True.
:param key: Key's name to be deleted
:param cas: CAS of the key
:return: True in case o success and False in case of failure.
"""
server = self._get_server(key)
return server.delete(key, cas) | [
"def",
"delete",
"(",
"self",
",",
"key",
",",
"cas",
"=",
"0",
")",
":",
"server",
"=",
"self",
".",
"_get_server",
"(",
"key",
")",
"return",
"server",
".",
"delete",
"(",
"key",
",",
"cas",
")"
] | Delete a key/value from server. If key does not exist, it returns True.
:param key: Key's name to be deleted
:param cas: CAS of the key
:return: True in case o success and False in case of failure. | [
"Delete",
"a",
"key",
"/",
"value",
"from",
"server",
".",
"If",
"key",
"does",
"not",
"exist",
"it",
"returns",
"True",
"."
] | 6a792829349c69204d9c5045e5c34b4231216dd6 | https://github.com/jaysonsantos/python-binary-memcached/blob/6a792829349c69204d9c5045e5c34b4231216dd6/bmemcached/client/distributed.py#L23-L32 | train |
jaysonsantos/python-binary-memcached | bmemcached/client/distributed.py | DistributedClient.set | def set(self, key, value, time=0, compress_level=-1):
"""
Set a value for a key on server.
:param key: Key's name
:type key: str
:param value: A value to be stored on server.
:type value: object
:param time: Time in seconds that your key will expire.
:type time: int
:param compress_level: How much to compress.
0 = no compression, 1 = fastest, 9 = slowest but best,
-1 = default compression level.
:type compress_level: int
:return: True in case of success and False in case of failure
:rtype: bool
"""
server = self._get_server(key)
return server.set(key, value, time, compress_level) | python | def set(self, key, value, time=0, compress_level=-1):
"""
Set a value for a key on server.
:param key: Key's name
:type key: str
:param value: A value to be stored on server.
:type value: object
:param time: Time in seconds that your key will expire.
:type time: int
:param compress_level: How much to compress.
0 = no compression, 1 = fastest, 9 = slowest but best,
-1 = default compression level.
:type compress_level: int
:return: True in case of success and False in case of failure
:rtype: bool
"""
server = self._get_server(key)
return server.set(key, value, time, compress_level) | [
"def",
"set",
"(",
"self",
",",
"key",
",",
"value",
",",
"time",
"=",
"0",
",",
"compress_level",
"=",
"-",
"1",
")",
":",
"server",
"=",
"self",
".",
"_get_server",
"(",
"key",
")",
"return",
"server",
".",
"set",
"(",
"key",
",",
"value",
",",
"time",
",",
"compress_level",
")"
] | Set a value for a key on server.
:param key: Key's name
:type key: str
:param value: A value to be stored on server.
:type value: object
:param time: Time in seconds that your key will expire.
:type time: int
:param compress_level: How much to compress.
0 = no compression, 1 = fastest, 9 = slowest but best,
-1 = default compression level.
:type compress_level: int
:return: True in case of success and False in case of failure
:rtype: bool | [
"Set",
"a",
"value",
"for",
"a",
"key",
"on",
"server",
"."
] | 6a792829349c69204d9c5045e5c34b4231216dd6 | https://github.com/jaysonsantos/python-binary-memcached/blob/6a792829349c69204d9c5045e5c34b4231216dd6/bmemcached/client/distributed.py#L41-L59 | train |
jaysonsantos/python-binary-memcached | bmemcached/client/distributed.py | DistributedClient.set_multi | def set_multi(self, mappings, time=0, compress_level=-1):
"""
Set multiple keys with it's values on server.
:param mappings: A dict with keys/values
:type mappings: dict
:param time: Time in seconds that your key will expire.
:type time: int
:param compress_level: How much to compress.
0 = no compression, 1 = fastest, 9 = slowest but best,
-1 = default compression level.
:type compress_level: int
:return: True in case of success and False in case of failure
:rtype: bool
"""
returns = []
if not mappings:
return False
server_mappings = defaultdict(dict)
for key, value in mappings.items():
server_key = self._get_server(key)
server_mappings[server_key].update([(key, value)])
for server, m in server_mappings.items():
returns.append(server.set_multi(m, time, compress_level))
return all(returns) | python | def set_multi(self, mappings, time=0, compress_level=-1):
"""
Set multiple keys with it's values on server.
:param mappings: A dict with keys/values
:type mappings: dict
:param time: Time in seconds that your key will expire.
:type time: int
:param compress_level: How much to compress.
0 = no compression, 1 = fastest, 9 = slowest but best,
-1 = default compression level.
:type compress_level: int
:return: True in case of success and False in case of failure
:rtype: bool
"""
returns = []
if not mappings:
return False
server_mappings = defaultdict(dict)
for key, value in mappings.items():
server_key = self._get_server(key)
server_mappings[server_key].update([(key, value)])
for server, m in server_mappings.items():
returns.append(server.set_multi(m, time, compress_level))
return all(returns) | [
"def",
"set_multi",
"(",
"self",
",",
"mappings",
",",
"time",
"=",
"0",
",",
"compress_level",
"=",
"-",
"1",
")",
":",
"returns",
"=",
"[",
"]",
"if",
"not",
"mappings",
":",
"return",
"False",
"server_mappings",
"=",
"defaultdict",
"(",
"dict",
")",
"for",
"key",
",",
"value",
"in",
"mappings",
".",
"items",
"(",
")",
":",
"server_key",
"=",
"self",
".",
"_get_server",
"(",
"key",
")",
"server_mappings",
"[",
"server_key",
"]",
".",
"update",
"(",
"[",
"(",
"key",
",",
"value",
")",
"]",
")",
"for",
"server",
",",
"m",
"in",
"server_mappings",
".",
"items",
"(",
")",
":",
"returns",
".",
"append",
"(",
"server",
".",
"set_multi",
"(",
"m",
",",
"time",
",",
"compress_level",
")",
")",
"return",
"all",
"(",
"returns",
")"
] | Set multiple keys with it's values on server.
:param mappings: A dict with keys/values
:type mappings: dict
:param time: Time in seconds that your key will expire.
:type time: int
:param compress_level: How much to compress.
0 = no compression, 1 = fastest, 9 = slowest but best,
-1 = default compression level.
:type compress_level: int
:return: True in case of success and False in case of failure
:rtype: bool | [
"Set",
"multiple",
"keys",
"with",
"it",
"s",
"values",
"on",
"server",
"."
] | 6a792829349c69204d9c5045e5c34b4231216dd6 | https://github.com/jaysonsantos/python-binary-memcached/blob/6a792829349c69204d9c5045e5c34b4231216dd6/bmemcached/client/distributed.py#L61-L86 | train |
jaysonsantos/python-binary-memcached | bmemcached/client/distributed.py | DistributedClient.add | def add(self, key, value, time=0, compress_level=-1):
"""
Add a key/value to server ony if it does not exist.
:param key: Key's name
:type key: six.string_types
:param value: A value to be stored on server.
:type value: object
:param time: Time in seconds that your key will expire.
:type time: int
:param compress_level: How much to compress.
0 = no compression, 1 = fastest, 9 = slowest but best,
-1 = default compression level.
:type compress_level: int
:return: True if key is added False if key already exists
:rtype: bool
"""
server = self._get_server(key)
return server.add(key, value, time, compress_level) | python | def add(self, key, value, time=0, compress_level=-1):
"""
Add a key/value to server ony if it does not exist.
:param key: Key's name
:type key: six.string_types
:param value: A value to be stored on server.
:type value: object
:param time: Time in seconds that your key will expire.
:type time: int
:param compress_level: How much to compress.
0 = no compression, 1 = fastest, 9 = slowest but best,
-1 = default compression level.
:type compress_level: int
:return: True if key is added False if key already exists
:rtype: bool
"""
server = self._get_server(key)
return server.add(key, value, time, compress_level) | [
"def",
"add",
"(",
"self",
",",
"key",
",",
"value",
",",
"time",
"=",
"0",
",",
"compress_level",
"=",
"-",
"1",
")",
":",
"server",
"=",
"self",
".",
"_get_server",
"(",
"key",
")",
"return",
"server",
".",
"add",
"(",
"key",
",",
"value",
",",
"time",
",",
"compress_level",
")"
] | Add a key/value to server ony if it does not exist.
:param key: Key's name
:type key: six.string_types
:param value: A value to be stored on server.
:type value: object
:param time: Time in seconds that your key will expire.
:type time: int
:param compress_level: How much to compress.
0 = no compression, 1 = fastest, 9 = slowest but best,
-1 = default compression level.
:type compress_level: int
:return: True if key is added False if key already exists
:rtype: bool | [
"Add",
"a",
"key",
"/",
"value",
"to",
"server",
"ony",
"if",
"it",
"does",
"not",
"exist",
"."
] | 6a792829349c69204d9c5045e5c34b4231216dd6 | https://github.com/jaysonsantos/python-binary-memcached/blob/6a792829349c69204d9c5045e5c34b4231216dd6/bmemcached/client/distributed.py#L88-L106 | train |
jaysonsantos/python-binary-memcached | bmemcached/client/distributed.py | DistributedClient.replace | def replace(self, key, value, time=0, compress_level=-1):
"""
Replace a key/value to server ony if it does exist.
:param key: Key's name
:type key: six.string_types
:param value: A value to be stored on server.
:type value: object
:param time: Time in seconds that your key will expire.
:type time: int
:param compress_level: How much to compress.
0 = no compression, 1 = fastest, 9 = slowest but best,
-1 = default compression level.
:type compress_level: int
:return: True if key is replace False if key does not exists
:rtype: bool
"""
server = self._get_server(key)
return server.replace(key, value, time, compress_level) | python | def replace(self, key, value, time=0, compress_level=-1):
"""
Replace a key/value to server ony if it does exist.
:param key: Key's name
:type key: six.string_types
:param value: A value to be stored on server.
:type value: object
:param time: Time in seconds that your key will expire.
:type time: int
:param compress_level: How much to compress.
0 = no compression, 1 = fastest, 9 = slowest but best,
-1 = default compression level.
:type compress_level: int
:return: True if key is replace False if key does not exists
:rtype: bool
"""
server = self._get_server(key)
return server.replace(key, value, time, compress_level) | [
"def",
"replace",
"(",
"self",
",",
"key",
",",
"value",
",",
"time",
"=",
"0",
",",
"compress_level",
"=",
"-",
"1",
")",
":",
"server",
"=",
"self",
".",
"_get_server",
"(",
"key",
")",
"return",
"server",
".",
"replace",
"(",
"key",
",",
"value",
",",
"time",
",",
"compress_level",
")"
] | Replace a key/value to server ony if it does exist.
:param key: Key's name
:type key: six.string_types
:param value: A value to be stored on server.
:type value: object
:param time: Time in seconds that your key will expire.
:type time: int
:param compress_level: How much to compress.
0 = no compression, 1 = fastest, 9 = slowest but best,
-1 = default compression level.
:type compress_level: int
:return: True if key is replace False if key does not exists
:rtype: bool | [
"Replace",
"a",
"key",
"/",
"value",
"to",
"server",
"ony",
"if",
"it",
"does",
"exist",
"."
] | 6a792829349c69204d9c5045e5c34b4231216dd6 | https://github.com/jaysonsantos/python-binary-memcached/blob/6a792829349c69204d9c5045e5c34b4231216dd6/bmemcached/client/distributed.py#L108-L126 | train |
jaysonsantos/python-binary-memcached | bmemcached/client/distributed.py | DistributedClient.get_multi | def get_multi(self, keys, get_cas=False):
"""
Get multiple keys from server.
:param keys: A list of keys to from server.
:type keys: list
:param get_cas: If get_cas is true, each value is (data, cas), with each result's CAS value.
:type get_cas: boolean
:return: A dict with all requested keys.
:rtype: dict
"""
servers = defaultdict(list)
d = {}
for key in keys:
server_key = self._get_server(key)
servers[server_key].append(key)
for server, keys in servers.items():
results = server.get_multi(keys)
if not get_cas:
# Remove CAS data
for key, (value, cas) in results.items():
results[key] = value
d.update(results)
return d | python | def get_multi(self, keys, get_cas=False):
"""
Get multiple keys from server.
:param keys: A list of keys to from server.
:type keys: list
:param get_cas: If get_cas is true, each value is (data, cas), with each result's CAS value.
:type get_cas: boolean
:return: A dict with all requested keys.
:rtype: dict
"""
servers = defaultdict(list)
d = {}
for key in keys:
server_key = self._get_server(key)
servers[server_key].append(key)
for server, keys in servers.items():
results = server.get_multi(keys)
if not get_cas:
# Remove CAS data
for key, (value, cas) in results.items():
results[key] = value
d.update(results)
return d | [
"def",
"get_multi",
"(",
"self",
",",
"keys",
",",
"get_cas",
"=",
"False",
")",
":",
"servers",
"=",
"defaultdict",
"(",
"list",
")",
"d",
"=",
"{",
"}",
"for",
"key",
"in",
"keys",
":",
"server_key",
"=",
"self",
".",
"_get_server",
"(",
"key",
")",
"servers",
"[",
"server_key",
"]",
".",
"append",
"(",
"key",
")",
"for",
"server",
",",
"keys",
"in",
"servers",
".",
"items",
"(",
")",
":",
"results",
"=",
"server",
".",
"get_multi",
"(",
"keys",
")",
"if",
"not",
"get_cas",
":",
"# Remove CAS data",
"for",
"key",
",",
"(",
"value",
",",
"cas",
")",
"in",
"results",
".",
"items",
"(",
")",
":",
"results",
"[",
"key",
"]",
"=",
"value",
"d",
".",
"update",
"(",
"results",
")",
"return",
"d"
] | Get multiple keys from server.
:param keys: A list of keys to from server.
:type keys: list
:param get_cas: If get_cas is true, each value is (data, cas), with each result's CAS value.
:type get_cas: boolean
:return: A dict with all requested keys.
:rtype: dict | [
"Get",
"multiple",
"keys",
"from",
"server",
"."
] | 6a792829349c69204d9c5045e5c34b4231216dd6 | https://github.com/jaysonsantos/python-binary-memcached/blob/6a792829349c69204d9c5045e5c34b4231216dd6/bmemcached/client/distributed.py#L149-L172 | train |
jaysonsantos/python-binary-memcached | bmemcached/client/distributed.py | DistributedClient.cas | def cas(self, key, value, cas, time=0, compress_level=-1):
"""
Set a value for a key on server if its CAS value matches cas.
:param key: Key's name
:type key: six.string_types
:param value: A value to be stored on server.
:type value: object
:param cas: The CAS value previously obtained from a call to get*.
:type cas: int
:param time: Time in seconds that your key will expire.
:type time: int
:param compress_level: How much to compress.
0 = no compression, 1 = fastest, 9 = slowest but best,
-1 = default compression level.
:type compress_level: int
:return: True in case of success and False in case of failure
:rtype: bool
"""
server = self._get_server(key)
return server.cas(key, value, cas, time, compress_level) | python | def cas(self, key, value, cas, time=0, compress_level=-1):
"""
Set a value for a key on server if its CAS value matches cas.
:param key: Key's name
:type key: six.string_types
:param value: A value to be stored on server.
:type value: object
:param cas: The CAS value previously obtained from a call to get*.
:type cas: int
:param time: Time in seconds that your key will expire.
:type time: int
:param compress_level: How much to compress.
0 = no compression, 1 = fastest, 9 = slowest but best,
-1 = default compression level.
:type compress_level: int
:return: True in case of success and False in case of failure
:rtype: bool
"""
server = self._get_server(key)
return server.cas(key, value, cas, time, compress_level) | [
"def",
"cas",
"(",
"self",
",",
"key",
",",
"value",
",",
"cas",
",",
"time",
"=",
"0",
",",
"compress_level",
"=",
"-",
"1",
")",
":",
"server",
"=",
"self",
".",
"_get_server",
"(",
"key",
")",
"return",
"server",
".",
"cas",
"(",
"key",
",",
"value",
",",
"cas",
",",
"time",
",",
"compress_level",
")"
] | Set a value for a key on server if its CAS value matches cas.
:param key: Key's name
:type key: six.string_types
:param value: A value to be stored on server.
:type value: object
:param cas: The CAS value previously obtained from a call to get*.
:type cas: int
:param time: Time in seconds that your key will expire.
:type time: int
:param compress_level: How much to compress.
0 = no compression, 1 = fastest, 9 = slowest but best,
-1 = default compression level.
:type compress_level: int
:return: True in case of success and False in case of failure
:rtype: bool | [
"Set",
"a",
"value",
"for",
"a",
"key",
"on",
"server",
"if",
"its",
"CAS",
"value",
"matches",
"cas",
"."
] | 6a792829349c69204d9c5045e5c34b4231216dd6 | https://github.com/jaysonsantos/python-binary-memcached/blob/6a792829349c69204d9c5045e5c34b4231216dd6/bmemcached/client/distributed.py#L178-L198 | train |
jaysonsantos/python-binary-memcached | bmemcached/client/distributed.py | DistributedClient.incr | def incr(self, key, value):
"""
Increment a key, if it exists, returns it's actual value, if it don't, return 0.
:param key: Key's name
:type key: six.string_types
:param value: Number to be incremented
:type value: int
:return: Actual value of the key on server
:rtype: int
"""
server = self._get_server(key)
return server.incr(key, value) | python | def incr(self, key, value):
"""
Increment a key, if it exists, returns it's actual value, if it don't, return 0.
:param key: Key's name
:type key: six.string_types
:param value: Number to be incremented
:type value: int
:return: Actual value of the key on server
:rtype: int
"""
server = self._get_server(key)
return server.incr(key, value) | [
"def",
"incr",
"(",
"self",
",",
"key",
",",
"value",
")",
":",
"server",
"=",
"self",
".",
"_get_server",
"(",
"key",
")",
"return",
"server",
".",
"incr",
"(",
"key",
",",
"value",
")"
] | Increment a key, if it exists, returns it's actual value, if it don't, return 0.
:param key: Key's name
:type key: six.string_types
:param value: Number to be incremented
:type value: int
:return: Actual value of the key on server
:rtype: int | [
"Increment",
"a",
"key",
"if",
"it",
"exists",
"returns",
"it",
"s",
"actual",
"value",
"if",
"it",
"don",
"t",
"return",
"0",
"."
] | 6a792829349c69204d9c5045e5c34b4231216dd6 | https://github.com/jaysonsantos/python-binary-memcached/blob/6a792829349c69204d9c5045e5c34b4231216dd6/bmemcached/client/distributed.py#L200-L212 | train |
jaysonsantos/python-binary-memcached | bmemcached/client/distributed.py | DistributedClient.decr | def decr(self, key, value):
"""
Decrement a key, if it exists, returns it's actual value, if it don't, return 0.
Minimum value of decrement return is 0.
:param key: Key's name
:type key: six.string_types
:param value: Number to be decremented
:type value: int
:return: Actual value of the key on server
:rtype: int
"""
server = self._get_server(key)
return server.decr(key, value) | python | def decr(self, key, value):
"""
Decrement a key, if it exists, returns it's actual value, if it don't, return 0.
Minimum value of decrement return is 0.
:param key: Key's name
:type key: six.string_types
:param value: Number to be decremented
:type value: int
:return: Actual value of the key on server
:rtype: int
"""
server = self._get_server(key)
return server.decr(key, value) | [
"def",
"decr",
"(",
"self",
",",
"key",
",",
"value",
")",
":",
"server",
"=",
"self",
".",
"_get_server",
"(",
"key",
")",
"return",
"server",
".",
"decr",
"(",
"key",
",",
"value",
")"
] | Decrement a key, if it exists, returns it's actual value, if it don't, return 0.
Minimum value of decrement return is 0.
:param key: Key's name
:type key: six.string_types
:param value: Number to be decremented
:type value: int
:return: Actual value of the key on server
:rtype: int | [
"Decrement",
"a",
"key",
"if",
"it",
"exists",
"returns",
"it",
"s",
"actual",
"value",
"if",
"it",
"don",
"t",
"return",
"0",
".",
"Minimum",
"value",
"of",
"decrement",
"return",
"is",
"0",
"."
] | 6a792829349c69204d9c5045e5c34b4231216dd6 | https://github.com/jaysonsantos/python-binary-memcached/blob/6a792829349c69204d9c5045e5c34b4231216dd6/bmemcached/client/distributed.py#L214-L227 | train |
hsolbrig/PyShEx | pyshex/shape_expressions_language/p5_5_shapes_and_triple_expressions.py | satisfiesShape | def satisfiesShape(cntxt: Context, n: Node, S: ShExJ.Shape, c: DebugContext) -> bool:
""" `5.5.2 Semantics <http://shex.io/shex-semantics/#triple-expressions-semantics>`_
For a node `n`, shape `S`, graph `G`, and shapeMap `m`, `satisfies(n, S, G, m)` if and only if:
* `neigh(G, n)` can be partitioned into two sets matched and remainder such that
`matches(matched, expression, m)`. If expression is absent, remainder = `neigh(G, n)`.
:param n: focus node
:param S: Shape to be satisfied
:param cntxt: Evaluation context
:param c: Debug context
:return: true iff `satisfies(n, S, cntxt)`
"""
# Recursion detection. If start_evaluating returns a boolean value, this is the assumed result of the shape
# evaluation. If it returns None, then an initial evaluation is needed
rslt = cntxt.start_evaluating(n, S)
if rslt is None:
cntxt.evaluate_stack.append((n, S.id))
predicates = directed_predicates_in_expression(S, cntxt)
matchables = RDFGraph()
# Note: The code below does an "over-slurp" for the sake of expediency. If you are interested in
# getting EXACTLY the needed triples, set cntxt.over_slurp to false
if isinstance(cntxt.graph, SlurpyGraph) and cntxt.over_slurp:
with slurper(cntxt, n, S) as g:
_ = g.triples((n, None, None))
for predicate, direction in predicates.items():
with slurper(cntxt, n, S) as g:
matchables.add_triples(g.triples((n if direction.is_fwd else None,
iriref_to_uriref(predicate),
n if direction.is_rev else None)))
if c.debug:
print(c.i(1, "predicates:", sorted(cntxt.n3_mapper.n3(p) for p in predicates.keys())))
print(c.i(1, "matchables:", sorted(cntxt.n3_mapper.n3(m) for m in matchables)))
print()
if S.closed:
# TODO: Is this working correctly on reverse items?
non_matchables = RDFGraph([t for t in arcsOut(cntxt.graph, n) if t not in matchables])
if len(non_matchables):
cntxt.fail_reason = "Unmatched triples in CLOSED shape:"
cntxt.fail_reason = '\n'.join(f"\t{t}" for t in non_matchables)
if c.debug:
print(c.i(0,
f"<--- Satisfies shape {c.d()} FAIL - "
f"{len(non_matchables)} non-matching triples on a closed shape"))
print(c.i(1, "", list(non_matchables)))
print()
return False
# Evaluate the actual expression. Start assuming everything matches...
if S.expression:
if matches(cntxt, matchables, S.expression):
rslt = True
else:
extras = {iriref_to_uriref(e) for e in S.extra} if S.extra is not None else {}
if len(extras):
permutable_matchables = RDFGraph([t for t in matchables if t.p in extras])
non_permutable_matchables = RDFGraph([t for t in matchables if t not in permutable_matchables])
if c.debug:
print(c.i(1,
f"Complete match failed -- evaluating extras", list(extras)))
for matched, remainder in partition_2(permutable_matchables):
permutation = non_permutable_matchables.union(matched)
if matches(cntxt, permutation, S.expression):
rslt = True
break
rslt = rslt or False
else:
rslt = True # Empty shape
# If an assumption was made and the result doesn't match the assumption, switch directions and try again
done, consistent = cntxt.done_evaluating(n, S, rslt)
if not done:
rslt = satisfiesShape(cntxt, n, S)
rslt = rslt and consistent
cntxt.evaluate_stack.pop()
return rslt | python | def satisfiesShape(cntxt: Context, n: Node, S: ShExJ.Shape, c: DebugContext) -> bool:
""" `5.5.2 Semantics <http://shex.io/shex-semantics/#triple-expressions-semantics>`_
For a node `n`, shape `S`, graph `G`, and shapeMap `m`, `satisfies(n, S, G, m)` if and only if:
* `neigh(G, n)` can be partitioned into two sets matched and remainder such that
`matches(matched, expression, m)`. If expression is absent, remainder = `neigh(G, n)`.
:param n: focus node
:param S: Shape to be satisfied
:param cntxt: Evaluation context
:param c: Debug context
:return: true iff `satisfies(n, S, cntxt)`
"""
# Recursion detection. If start_evaluating returns a boolean value, this is the assumed result of the shape
# evaluation. If it returns None, then an initial evaluation is needed
rslt = cntxt.start_evaluating(n, S)
if rslt is None:
cntxt.evaluate_stack.append((n, S.id))
predicates = directed_predicates_in_expression(S, cntxt)
matchables = RDFGraph()
# Note: The code below does an "over-slurp" for the sake of expediency. If you are interested in
# getting EXACTLY the needed triples, set cntxt.over_slurp to false
if isinstance(cntxt.graph, SlurpyGraph) and cntxt.over_slurp:
with slurper(cntxt, n, S) as g:
_ = g.triples((n, None, None))
for predicate, direction in predicates.items():
with slurper(cntxt, n, S) as g:
matchables.add_triples(g.triples((n if direction.is_fwd else None,
iriref_to_uriref(predicate),
n if direction.is_rev else None)))
if c.debug:
print(c.i(1, "predicates:", sorted(cntxt.n3_mapper.n3(p) for p in predicates.keys())))
print(c.i(1, "matchables:", sorted(cntxt.n3_mapper.n3(m) for m in matchables)))
print()
if S.closed:
# TODO: Is this working correctly on reverse items?
non_matchables = RDFGraph([t for t in arcsOut(cntxt.graph, n) if t not in matchables])
if len(non_matchables):
cntxt.fail_reason = "Unmatched triples in CLOSED shape:"
cntxt.fail_reason = '\n'.join(f"\t{t}" for t in non_matchables)
if c.debug:
print(c.i(0,
f"<--- Satisfies shape {c.d()} FAIL - "
f"{len(non_matchables)} non-matching triples on a closed shape"))
print(c.i(1, "", list(non_matchables)))
print()
return False
# Evaluate the actual expression. Start assuming everything matches...
if S.expression:
if matches(cntxt, matchables, S.expression):
rslt = True
else:
extras = {iriref_to_uriref(e) for e in S.extra} if S.extra is not None else {}
if len(extras):
permutable_matchables = RDFGraph([t for t in matchables if t.p in extras])
non_permutable_matchables = RDFGraph([t for t in matchables if t not in permutable_matchables])
if c.debug:
print(c.i(1,
f"Complete match failed -- evaluating extras", list(extras)))
for matched, remainder in partition_2(permutable_matchables):
permutation = non_permutable_matchables.union(matched)
if matches(cntxt, permutation, S.expression):
rslt = True
break
rslt = rslt or False
else:
rslt = True # Empty shape
# If an assumption was made and the result doesn't match the assumption, switch directions and try again
done, consistent = cntxt.done_evaluating(n, S, rslt)
if not done:
rslt = satisfiesShape(cntxt, n, S)
rslt = rslt and consistent
cntxt.evaluate_stack.pop()
return rslt | [
"def",
"satisfiesShape",
"(",
"cntxt",
":",
"Context",
",",
"n",
":",
"Node",
",",
"S",
":",
"ShExJ",
".",
"Shape",
",",
"c",
":",
"DebugContext",
")",
"->",
"bool",
":",
"# Recursion detection. If start_evaluating returns a boolean value, this is the assumed result of the shape",
"# evaluation. If it returns None, then an initial evaluation is needed",
"rslt",
"=",
"cntxt",
".",
"start_evaluating",
"(",
"n",
",",
"S",
")",
"if",
"rslt",
"is",
"None",
":",
"cntxt",
".",
"evaluate_stack",
".",
"append",
"(",
"(",
"n",
",",
"S",
".",
"id",
")",
")",
"predicates",
"=",
"directed_predicates_in_expression",
"(",
"S",
",",
"cntxt",
")",
"matchables",
"=",
"RDFGraph",
"(",
")",
"# Note: The code below does an \"over-slurp\" for the sake of expediency. If you are interested in",
"# getting EXACTLY the needed triples, set cntxt.over_slurp to false",
"if",
"isinstance",
"(",
"cntxt",
".",
"graph",
",",
"SlurpyGraph",
")",
"and",
"cntxt",
".",
"over_slurp",
":",
"with",
"slurper",
"(",
"cntxt",
",",
"n",
",",
"S",
")",
"as",
"g",
":",
"_",
"=",
"g",
".",
"triples",
"(",
"(",
"n",
",",
"None",
",",
"None",
")",
")",
"for",
"predicate",
",",
"direction",
"in",
"predicates",
".",
"items",
"(",
")",
":",
"with",
"slurper",
"(",
"cntxt",
",",
"n",
",",
"S",
")",
"as",
"g",
":",
"matchables",
".",
"add_triples",
"(",
"g",
".",
"triples",
"(",
"(",
"n",
"if",
"direction",
".",
"is_fwd",
"else",
"None",
",",
"iriref_to_uriref",
"(",
"predicate",
")",
",",
"n",
"if",
"direction",
".",
"is_rev",
"else",
"None",
")",
")",
")",
"if",
"c",
".",
"debug",
":",
"print",
"(",
"c",
".",
"i",
"(",
"1",
",",
"\"predicates:\"",
",",
"sorted",
"(",
"cntxt",
".",
"n3_mapper",
".",
"n3",
"(",
"p",
")",
"for",
"p",
"in",
"predicates",
".",
"keys",
"(",
")",
")",
")",
")",
"print",
"(",
"c",
".",
"i",
"(",
"1",
",",
"\"matchables:\"",
",",
"sorted",
"(",
"cntxt",
".",
"n3_mapper",
".",
"n3",
"(",
"m",
")",
"for",
"m",
"in",
"matchables",
")",
")",
")",
"print",
"(",
")",
"if",
"S",
".",
"closed",
":",
"# TODO: Is this working correctly on reverse items?",
"non_matchables",
"=",
"RDFGraph",
"(",
"[",
"t",
"for",
"t",
"in",
"arcsOut",
"(",
"cntxt",
".",
"graph",
",",
"n",
")",
"if",
"t",
"not",
"in",
"matchables",
"]",
")",
"if",
"len",
"(",
"non_matchables",
")",
":",
"cntxt",
".",
"fail_reason",
"=",
"\"Unmatched triples in CLOSED shape:\"",
"cntxt",
".",
"fail_reason",
"=",
"'\\n'",
".",
"join",
"(",
"f\"\\t{t}\"",
"for",
"t",
"in",
"non_matchables",
")",
"if",
"c",
".",
"debug",
":",
"print",
"(",
"c",
".",
"i",
"(",
"0",
",",
"f\"<--- Satisfies shape {c.d()} FAIL - \"",
"f\"{len(non_matchables)} non-matching triples on a closed shape\"",
")",
")",
"print",
"(",
"c",
".",
"i",
"(",
"1",
",",
"\"\"",
",",
"list",
"(",
"non_matchables",
")",
")",
")",
"print",
"(",
")",
"return",
"False",
"# Evaluate the actual expression. Start assuming everything matches...",
"if",
"S",
".",
"expression",
":",
"if",
"matches",
"(",
"cntxt",
",",
"matchables",
",",
"S",
".",
"expression",
")",
":",
"rslt",
"=",
"True",
"else",
":",
"extras",
"=",
"{",
"iriref_to_uriref",
"(",
"e",
")",
"for",
"e",
"in",
"S",
".",
"extra",
"}",
"if",
"S",
".",
"extra",
"is",
"not",
"None",
"else",
"{",
"}",
"if",
"len",
"(",
"extras",
")",
":",
"permutable_matchables",
"=",
"RDFGraph",
"(",
"[",
"t",
"for",
"t",
"in",
"matchables",
"if",
"t",
".",
"p",
"in",
"extras",
"]",
")",
"non_permutable_matchables",
"=",
"RDFGraph",
"(",
"[",
"t",
"for",
"t",
"in",
"matchables",
"if",
"t",
"not",
"in",
"permutable_matchables",
"]",
")",
"if",
"c",
".",
"debug",
":",
"print",
"(",
"c",
".",
"i",
"(",
"1",
",",
"f\"Complete match failed -- evaluating extras\"",
",",
"list",
"(",
"extras",
")",
")",
")",
"for",
"matched",
",",
"remainder",
"in",
"partition_2",
"(",
"permutable_matchables",
")",
":",
"permutation",
"=",
"non_permutable_matchables",
".",
"union",
"(",
"matched",
")",
"if",
"matches",
"(",
"cntxt",
",",
"permutation",
",",
"S",
".",
"expression",
")",
":",
"rslt",
"=",
"True",
"break",
"rslt",
"=",
"rslt",
"or",
"False",
"else",
":",
"rslt",
"=",
"True",
"# Empty shape",
"# If an assumption was made and the result doesn't match the assumption, switch directions and try again",
"done",
",",
"consistent",
"=",
"cntxt",
".",
"done_evaluating",
"(",
"n",
",",
"S",
",",
"rslt",
")",
"if",
"not",
"done",
":",
"rslt",
"=",
"satisfiesShape",
"(",
"cntxt",
",",
"n",
",",
"S",
")",
"rslt",
"=",
"rslt",
"and",
"consistent",
"cntxt",
".",
"evaluate_stack",
".",
"pop",
"(",
")",
"return",
"rslt"
] | `5.5.2 Semantics <http://shex.io/shex-semantics/#triple-expressions-semantics>`_
For a node `n`, shape `S`, graph `G`, and shapeMap `m`, `satisfies(n, S, G, m)` if and only if:
* `neigh(G, n)` can be partitioned into two sets matched and remainder such that
`matches(matched, expression, m)`. If expression is absent, remainder = `neigh(G, n)`.
:param n: focus node
:param S: Shape to be satisfied
:param cntxt: Evaluation context
:param c: Debug context
:return: true iff `satisfies(n, S, cntxt)` | [
"5",
".",
"5",
".",
"2",
"Semantics",
"<http",
":",
"//",
"shex",
".",
"io",
"/",
"shex",
"-",
"semantics",
"/",
"#triple",
"-",
"expressions",
"-",
"semantics",
">",
"_"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/shape_expressions_language/p5_5_shapes_and_triple_expressions.py#L23-L107 | train |
hsolbrig/PyShEx | pyshex/shape_expressions_language/p5_5_shapes_and_triple_expressions.py | valid_remainder | def valid_remainder(cntxt: Context, n: Node, matchables: RDFGraph, S: ShExJ.Shape) -> bool:
"""
Let **outs** be the arcsOut in remainder: `outs = remainder ∩ arcsOut(G, n)`.
Let **matchables** be the triples in outs whose predicate appears in a TripleConstraint in `expression`. If
`expression` is absent, matchables = Ø (the empty set).
* There is no triple in **matchables** which matches a TripleConstraint in expression
* There is no triple in **matchables** whose predicate does not appear in extra.
* closed is false or unmatchables is empty
:param cntxt: evaluation context
:param n: focus node
:param matchables: non-matched triples
:param S: Shape being evaluated
:return: True if remainder is valid
"""
# TODO: Update this and satisfies to address the new algorithm
# Let **outs** be the arcsOut in remainder: `outs = remainder ∩ arcsOut(G, n)`.
outs = arcsOut(cntxt.graph, n).intersection(matchables)
# predicates that in a TripleConstraint in `expression`
predicates = predicates_in_expression(S, cntxt)
# Let **matchables** be the triples in outs whose predicate appears in predicates. If
# `expression` is absent, matchables = Ø (the empty set).
matchables = RDFGraph(t for t in outs if str(t.p) in predicates)
# There is no triple in **matchables** which matches a TripleConstraint in expression
if matchables and S.expression is not None:
tes = triple_constraints_in_expression(S.expression, cntxt)
for m in matchables:
if any(matchesTripleConstraint(cntxt, m, te) for te in tes):
return False
# There is no triple in **matchables** whose predicate does not appear in extra.
extras = {iriref_to_uriref(e) for e in S.extra} if S.extra is not None else {}
if any(t.p not in extras for t in matchables):
return False
# closed is false or unmatchables is empty.
return not S.closed.val or not bool(outs - matchables) | python | def valid_remainder(cntxt: Context, n: Node, matchables: RDFGraph, S: ShExJ.Shape) -> bool:
"""
Let **outs** be the arcsOut in remainder: `outs = remainder ∩ arcsOut(G, n)`.
Let **matchables** be the triples in outs whose predicate appears in a TripleConstraint in `expression`. If
`expression` is absent, matchables = Ø (the empty set).
* There is no triple in **matchables** which matches a TripleConstraint in expression
* There is no triple in **matchables** whose predicate does not appear in extra.
* closed is false or unmatchables is empty
:param cntxt: evaluation context
:param n: focus node
:param matchables: non-matched triples
:param S: Shape being evaluated
:return: True if remainder is valid
"""
# TODO: Update this and satisfies to address the new algorithm
# Let **outs** be the arcsOut in remainder: `outs = remainder ∩ arcsOut(G, n)`.
outs = arcsOut(cntxt.graph, n).intersection(matchables)
# predicates that in a TripleConstraint in `expression`
predicates = predicates_in_expression(S, cntxt)
# Let **matchables** be the triples in outs whose predicate appears in predicates. If
# `expression` is absent, matchables = Ø (the empty set).
matchables = RDFGraph(t for t in outs if str(t.p) in predicates)
# There is no triple in **matchables** which matches a TripleConstraint in expression
if matchables and S.expression is not None:
tes = triple_constraints_in_expression(S.expression, cntxt)
for m in matchables:
if any(matchesTripleConstraint(cntxt, m, te) for te in tes):
return False
# There is no triple in **matchables** whose predicate does not appear in extra.
extras = {iriref_to_uriref(e) for e in S.extra} if S.extra is not None else {}
if any(t.p not in extras for t in matchables):
return False
# closed is false or unmatchables is empty.
return not S.closed.val or not bool(outs - matchables) | [
"def",
"valid_remainder",
"(",
"cntxt",
":",
"Context",
",",
"n",
":",
"Node",
",",
"matchables",
":",
"RDFGraph",
",",
"S",
":",
"ShExJ",
".",
"Shape",
")",
"->",
"bool",
":",
"# TODO: Update this and satisfies to address the new algorithm",
"# Let **outs** be the arcsOut in remainder: `outs = remainder ∩ arcsOut(G, n)`.",
"outs",
"=",
"arcsOut",
"(",
"cntxt",
".",
"graph",
",",
"n",
")",
".",
"intersection",
"(",
"matchables",
")",
"# predicates that in a TripleConstraint in `expression`",
"predicates",
"=",
"predicates_in_expression",
"(",
"S",
",",
"cntxt",
")",
"# Let **matchables** be the triples in outs whose predicate appears in predicates. If",
"# `expression` is absent, matchables = Ø (the empty set).",
"matchables",
"=",
"RDFGraph",
"(",
"t",
"for",
"t",
"in",
"outs",
"if",
"str",
"(",
"t",
".",
"p",
")",
"in",
"predicates",
")",
"# There is no triple in **matchables** which matches a TripleConstraint in expression",
"if",
"matchables",
"and",
"S",
".",
"expression",
"is",
"not",
"None",
":",
"tes",
"=",
"triple_constraints_in_expression",
"(",
"S",
".",
"expression",
",",
"cntxt",
")",
"for",
"m",
"in",
"matchables",
":",
"if",
"any",
"(",
"matchesTripleConstraint",
"(",
"cntxt",
",",
"m",
",",
"te",
")",
"for",
"te",
"in",
"tes",
")",
":",
"return",
"False",
"# There is no triple in **matchables** whose predicate does not appear in extra.",
"extras",
"=",
"{",
"iriref_to_uriref",
"(",
"e",
")",
"for",
"e",
"in",
"S",
".",
"extra",
"}",
"if",
"S",
".",
"extra",
"is",
"not",
"None",
"else",
"{",
"}",
"if",
"any",
"(",
"t",
".",
"p",
"not",
"in",
"extras",
"for",
"t",
"in",
"matchables",
")",
":",
"return",
"False",
"# closed is false or unmatchables is empty.",
"return",
"not",
"S",
".",
"closed",
".",
"val",
"or",
"not",
"bool",
"(",
"outs",
"-",
"matchables",
")"
] | Let **outs** be the arcsOut in remainder: `outs = remainder ∩ arcsOut(G, n)`.
Let **matchables** be the triples in outs whose predicate appears in a TripleConstraint in `expression`. If
`expression` is absent, matchables = Ø (the empty set).
* There is no triple in **matchables** which matches a TripleConstraint in expression
* There is no triple in **matchables** whose predicate does not appear in extra.
* closed is false or unmatchables is empty
:param cntxt: evaluation context
:param n: focus node
:param matchables: non-matched triples
:param S: Shape being evaluated
:return: True if remainder is valid | [
"Let",
"**",
"outs",
"**",
"be",
"the",
"arcsOut",
"in",
"remainder",
":",
"outs",
"=",
"remainder",
"∩",
"arcsOut",
"(",
"G",
"n",
")",
"."
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/shape_expressions_language/p5_5_shapes_and_triple_expressions.py#L110-L153 | train |
hsolbrig/PyShEx | pyshex/shape_expressions_language/p5_5_shapes_and_triple_expressions.py | matches | def matches(cntxt: Context, T: RDFGraph, expr: ShExJ.tripleExpr) -> bool:
"""
**matches**: asserts that a triple expression is matched by a set of triples that come from the neighbourhood of a
node in an RDF graph. The expression `matches(T, expr, m)` indicates that a set of triples `T` can satisfy these
rules:
* expr has semActs and `matches(T, expr, m)` by the remaining rules in this list and the evaluation
of semActs succeeds according to the section below on Semantic Actions.
* expr has a cardinality of min and/or max not equal to 1, where a max of -1 is treated as unbounded, and T
can be partitioned into k subsets T1, T2,…Tk such that min ≤ k ≤ max and for each Tn,
`matches(Tn, expr, m)` by the remaining rules in this list.
* expr is a OneOf and there is some shape expression se2 in shapeExprs such that a matches(T, se2, m).
* expr is an EachOf and there is some partition of T into T1, T2,… such that for every expression
expr1, expr2,… in shapeExprs, matches(Tn, exprn, m).
* expr is a TripleConstraint and:
* T is a set of one triple. Let t be the soul triple in T.
* t's predicate equals expr's predicate. Let value be t's subject if inverse is true, else t's object.
* if inverse is true, t is in arcsIn, else t is in `arcsOut`.
* either
* expr has no valueExpr
* or `satisfies(value, valueExpr, G, m).
"""
if isinstance_(expr, ShExJ.tripleExprLabel):
return matchesExpr(cntxt, T, expr)
else:
return matchesCardinality(cntxt, T, expr) and (expr.semActs is None or semActsSatisfied(expr.semActs, cntxt)) | python | def matches(cntxt: Context, T: RDFGraph, expr: ShExJ.tripleExpr) -> bool:
"""
**matches**: asserts that a triple expression is matched by a set of triples that come from the neighbourhood of a
node in an RDF graph. The expression `matches(T, expr, m)` indicates that a set of triples `T` can satisfy these
rules:
* expr has semActs and `matches(T, expr, m)` by the remaining rules in this list and the evaluation
of semActs succeeds according to the section below on Semantic Actions.
* expr has a cardinality of min and/or max not equal to 1, where a max of -1 is treated as unbounded, and T
can be partitioned into k subsets T1, T2,…Tk such that min ≤ k ≤ max and for each Tn,
`matches(Tn, expr, m)` by the remaining rules in this list.
* expr is a OneOf and there is some shape expression se2 in shapeExprs such that a matches(T, se2, m).
* expr is an EachOf and there is some partition of T into T1, T2,… such that for every expression
expr1, expr2,… in shapeExprs, matches(Tn, exprn, m).
* expr is a TripleConstraint and:
* T is a set of one triple. Let t be the soul triple in T.
* t's predicate equals expr's predicate. Let value be t's subject if inverse is true, else t's object.
* if inverse is true, t is in arcsIn, else t is in `arcsOut`.
* either
* expr has no valueExpr
* or `satisfies(value, valueExpr, G, m).
"""
if isinstance_(expr, ShExJ.tripleExprLabel):
return matchesExpr(cntxt, T, expr)
else:
return matchesCardinality(cntxt, T, expr) and (expr.semActs is None or semActsSatisfied(expr.semActs, cntxt)) | [
"def",
"matches",
"(",
"cntxt",
":",
"Context",
",",
"T",
":",
"RDFGraph",
",",
"expr",
":",
"ShExJ",
".",
"tripleExpr",
")",
"->",
"bool",
":",
"if",
"isinstance_",
"(",
"expr",
",",
"ShExJ",
".",
"tripleExprLabel",
")",
":",
"return",
"matchesExpr",
"(",
"cntxt",
",",
"T",
",",
"expr",
")",
"else",
":",
"return",
"matchesCardinality",
"(",
"cntxt",
",",
"T",
",",
"expr",
")",
"and",
"(",
"expr",
".",
"semActs",
"is",
"None",
"or",
"semActsSatisfied",
"(",
"expr",
".",
"semActs",
",",
"cntxt",
")",
")"
] | **matches**: asserts that a triple expression is matched by a set of triples that come from the neighbourhood of a
node in an RDF graph. The expression `matches(T, expr, m)` indicates that a set of triples `T` can satisfy these
rules:
* expr has semActs and `matches(T, expr, m)` by the remaining rules in this list and the evaluation
of semActs succeeds according to the section below on Semantic Actions.
* expr has a cardinality of min and/or max not equal to 1, where a max of -1 is treated as unbounded, and T
can be partitioned into k subsets T1, T2,…Tk such that min ≤ k ≤ max and for each Tn,
`matches(Tn, expr, m)` by the remaining rules in this list.
* expr is a OneOf and there is some shape expression se2 in shapeExprs such that a matches(T, se2, m).
* expr is an EachOf and there is some partition of T into T1, T2,… such that for every expression
expr1, expr2,… in shapeExprs, matches(Tn, exprn, m).
* expr is a TripleConstraint and:
* T is a set of one triple. Let t be the soul triple in T.
* t's predicate equals expr's predicate. Let value be t's subject if inverse is true, else t's object.
* if inverse is true, t is in arcsIn, else t is in `arcsOut`.
* either
* expr has no valueExpr
* or `satisfies(value, valueExpr, G, m). | [
"**",
"matches",
"**",
":",
"asserts",
"that",
"a",
"triple",
"expression",
"is",
"matched",
"by",
"a",
"set",
"of",
"triples",
"that",
"come",
"from",
"the",
"neighbourhood",
"of",
"a",
"node",
"in",
"an",
"RDF",
"graph",
".",
"The",
"expression",
"matches",
"(",
"T",
"expr",
"m",
")",
"indicates",
"that",
"a",
"set",
"of",
"triples",
"T",
"can",
"satisfy",
"these",
"rules",
":"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/shape_expressions_language/p5_5_shapes_and_triple_expressions.py#L156-L181 | train |
hsolbrig/PyShEx | pyshex/shape_expressions_language/p5_5_shapes_and_triple_expressions.py | matchesCardinality | def matchesCardinality(cntxt: Context, T: RDFGraph, expr: Union[ShExJ.tripleExpr, ShExJ.tripleExprLabel],
c: DebugContext) -> bool:
""" Evaluate cardinality expression
expr has a cardinality of min and/or max not equal to 1, where a max of -1 is treated as unbounded, and
T can be partitioned into k subsets T1, T2,…Tk such that min ≤ k ≤ max and for each Tn,
matches(Tn, expr, m) by the remaining rules in this list.
"""
# TODO: Cardinality defaults into spec
min_ = expr.min if expr.min is not None else 1
max_ = expr.max if expr.max is not None else 1
cardinality_text = f"{{{min_},{'*' if max_ == -1 else max_}}}"
if c.debug and (min_ != 0 or len(T) != 0):
print(f"{cardinality_text} matching {len(T)} triples")
if min_ == 0 and len(T) == 0:
return True
if isinstance(expr, ShExJ.TripleConstraint):
if len(T) < min_:
if len(T) > 0:
_fail_triples(cntxt, T)
cntxt.fail_reason = f" {len(T)} triples less than {cardinality_text}"
else:
cntxt.fail_reason = f" No matching triples found for predicate {cntxt.n3_mapper.n3(expr.predicate)}"
return False
elif 0 <= max_ < len(T):
_fail_triples(cntxt, T)
cntxt.fail_reason = f" {len(T)} triples exceeds max {cardinality_text}"
return False
else:
return all(matchesTripleConstraint(cntxt, t, expr) for t in T)
else:
for partition in _partitions(T, min_, max_):
if all(matchesExpr(cntxt, part, expr) for part in partition):
return True
if min_ != 1 or max_ != 1:
_fail_triples(cntxt, T)
cntxt.fail_reason = f" {len(T)} triples cannot be partitioned into {cardinality_text} passing groups"
return False | python | def matchesCardinality(cntxt: Context, T: RDFGraph, expr: Union[ShExJ.tripleExpr, ShExJ.tripleExprLabel],
c: DebugContext) -> bool:
""" Evaluate cardinality expression
expr has a cardinality of min and/or max not equal to 1, where a max of -1 is treated as unbounded, and
T can be partitioned into k subsets T1, T2,…Tk such that min ≤ k ≤ max and for each Tn,
matches(Tn, expr, m) by the remaining rules in this list.
"""
# TODO: Cardinality defaults into spec
min_ = expr.min if expr.min is not None else 1
max_ = expr.max if expr.max is not None else 1
cardinality_text = f"{{{min_},{'*' if max_ == -1 else max_}}}"
if c.debug and (min_ != 0 or len(T) != 0):
print(f"{cardinality_text} matching {len(T)} triples")
if min_ == 0 and len(T) == 0:
return True
if isinstance(expr, ShExJ.TripleConstraint):
if len(T) < min_:
if len(T) > 0:
_fail_triples(cntxt, T)
cntxt.fail_reason = f" {len(T)} triples less than {cardinality_text}"
else:
cntxt.fail_reason = f" No matching triples found for predicate {cntxt.n3_mapper.n3(expr.predicate)}"
return False
elif 0 <= max_ < len(T):
_fail_triples(cntxt, T)
cntxt.fail_reason = f" {len(T)} triples exceeds max {cardinality_text}"
return False
else:
return all(matchesTripleConstraint(cntxt, t, expr) for t in T)
else:
for partition in _partitions(T, min_, max_):
if all(matchesExpr(cntxt, part, expr) for part in partition):
return True
if min_ != 1 or max_ != 1:
_fail_triples(cntxt, T)
cntxt.fail_reason = f" {len(T)} triples cannot be partitioned into {cardinality_text} passing groups"
return False | [
"def",
"matchesCardinality",
"(",
"cntxt",
":",
"Context",
",",
"T",
":",
"RDFGraph",
",",
"expr",
":",
"Union",
"[",
"ShExJ",
".",
"tripleExpr",
",",
"ShExJ",
".",
"tripleExprLabel",
"]",
",",
"c",
":",
"DebugContext",
")",
"->",
"bool",
":",
"# TODO: Cardinality defaults into spec",
"min_",
"=",
"expr",
".",
"min",
"if",
"expr",
".",
"min",
"is",
"not",
"None",
"else",
"1",
"max_",
"=",
"expr",
".",
"max",
"if",
"expr",
".",
"max",
"is",
"not",
"None",
"else",
"1",
"cardinality_text",
"=",
"f\"{{{min_},{'*' if max_ == -1 else max_}}}\"",
"if",
"c",
".",
"debug",
"and",
"(",
"min_",
"!=",
"0",
"or",
"len",
"(",
"T",
")",
"!=",
"0",
")",
":",
"print",
"(",
"f\"{cardinality_text} matching {len(T)} triples\"",
")",
"if",
"min_",
"==",
"0",
"and",
"len",
"(",
"T",
")",
"==",
"0",
":",
"return",
"True",
"if",
"isinstance",
"(",
"expr",
",",
"ShExJ",
".",
"TripleConstraint",
")",
":",
"if",
"len",
"(",
"T",
")",
"<",
"min_",
":",
"if",
"len",
"(",
"T",
")",
">",
"0",
":",
"_fail_triples",
"(",
"cntxt",
",",
"T",
")",
"cntxt",
".",
"fail_reason",
"=",
"f\" {len(T)} triples less than {cardinality_text}\"",
"else",
":",
"cntxt",
".",
"fail_reason",
"=",
"f\" No matching triples found for predicate {cntxt.n3_mapper.n3(expr.predicate)}\"",
"return",
"False",
"elif",
"0",
"<=",
"max_",
"<",
"len",
"(",
"T",
")",
":",
"_fail_triples",
"(",
"cntxt",
",",
"T",
")",
"cntxt",
".",
"fail_reason",
"=",
"f\" {len(T)} triples exceeds max {cardinality_text}\"",
"return",
"False",
"else",
":",
"return",
"all",
"(",
"matchesTripleConstraint",
"(",
"cntxt",
",",
"t",
",",
"expr",
")",
"for",
"t",
"in",
"T",
")",
"else",
":",
"for",
"partition",
"in",
"_partitions",
"(",
"T",
",",
"min_",
",",
"max_",
")",
":",
"if",
"all",
"(",
"matchesExpr",
"(",
"cntxt",
",",
"part",
",",
"expr",
")",
"for",
"part",
"in",
"partition",
")",
":",
"return",
"True",
"if",
"min_",
"!=",
"1",
"or",
"max_",
"!=",
"1",
":",
"_fail_triples",
"(",
"cntxt",
",",
"T",
")",
"cntxt",
".",
"fail_reason",
"=",
"f\" {len(T)} triples cannot be partitioned into {cardinality_text} passing groups\"",
"return",
"False"
] | Evaluate cardinality expression
expr has a cardinality of min and/or max not equal to 1, where a max of -1 is treated as unbounded, and
T can be partitioned into k subsets T1, T2,…Tk such that min ≤ k ≤ max and for each Tn,
matches(Tn, expr, m) by the remaining rules in this list. | [
"Evaluate",
"cardinality",
"expression"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/shape_expressions_language/p5_5_shapes_and_triple_expressions.py#L196-L234 | train |
hsolbrig/PyShEx | pyshex/shape_expressions_language/p5_5_shapes_and_triple_expressions.py | matchesExpr | def matchesExpr(cntxt: Context, T: RDFGraph, expr: ShExJ.tripleExpr, _: DebugContext) -> bool:
""" Evaluate the expression
"""
if isinstance(expr, ShExJ.OneOf):
return matchesOneOf(cntxt, T, expr)
elif isinstance(expr, ShExJ.EachOf):
return matchesEachOf(cntxt, T, expr)
elif isinstance(expr, ShExJ.TripleConstraint):
return matchesCardinality(cntxt, T, expr)
elif isinstance_(expr, ShExJ.tripleExprLabel):
return matchesTripleExprRef(cntxt, T, expr)
else:
raise Exception("Unknown expression") | python | def matchesExpr(cntxt: Context, T: RDFGraph, expr: ShExJ.tripleExpr, _: DebugContext) -> bool:
""" Evaluate the expression
"""
if isinstance(expr, ShExJ.OneOf):
return matchesOneOf(cntxt, T, expr)
elif isinstance(expr, ShExJ.EachOf):
return matchesEachOf(cntxt, T, expr)
elif isinstance(expr, ShExJ.TripleConstraint):
return matchesCardinality(cntxt, T, expr)
elif isinstance_(expr, ShExJ.tripleExprLabel):
return matchesTripleExprRef(cntxt, T, expr)
else:
raise Exception("Unknown expression") | [
"def",
"matchesExpr",
"(",
"cntxt",
":",
"Context",
",",
"T",
":",
"RDFGraph",
",",
"expr",
":",
"ShExJ",
".",
"tripleExpr",
",",
"_",
":",
"DebugContext",
")",
"->",
"bool",
":",
"if",
"isinstance",
"(",
"expr",
",",
"ShExJ",
".",
"OneOf",
")",
":",
"return",
"matchesOneOf",
"(",
"cntxt",
",",
"T",
",",
"expr",
")",
"elif",
"isinstance",
"(",
"expr",
",",
"ShExJ",
".",
"EachOf",
")",
":",
"return",
"matchesEachOf",
"(",
"cntxt",
",",
"T",
",",
"expr",
")",
"elif",
"isinstance",
"(",
"expr",
",",
"ShExJ",
".",
"TripleConstraint",
")",
":",
"return",
"matchesCardinality",
"(",
"cntxt",
",",
"T",
",",
"expr",
")",
"elif",
"isinstance_",
"(",
"expr",
",",
"ShExJ",
".",
"tripleExprLabel",
")",
":",
"return",
"matchesTripleExprRef",
"(",
"cntxt",
",",
"T",
",",
"expr",
")",
"else",
":",
"raise",
"Exception",
"(",
"\"Unknown expression\"",
")"
] | Evaluate the expression | [
"Evaluate",
"the",
"expression"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/shape_expressions_language/p5_5_shapes_and_triple_expressions.py#L257-L271 | train |
hsolbrig/PyShEx | pyshex/shape_expressions_language/p5_5_shapes_and_triple_expressions.py | matchesOneOf | def matchesOneOf(cntxt: Context, T: RDFGraph, expr: ShExJ.OneOf, _: DebugContext) -> bool:
"""
expr is a OneOf and there is some shape expression se2 in shapeExprs such that a matches(T, se2, m).
"""
return any(matches(cntxt, T, e) for e in expr.expressions) | python | def matchesOneOf(cntxt: Context, T: RDFGraph, expr: ShExJ.OneOf, _: DebugContext) -> bool:
"""
expr is a OneOf and there is some shape expression se2 in shapeExprs such that a matches(T, se2, m).
"""
return any(matches(cntxt, T, e) for e in expr.expressions) | [
"def",
"matchesOneOf",
"(",
"cntxt",
":",
"Context",
",",
"T",
":",
"RDFGraph",
",",
"expr",
":",
"ShExJ",
".",
"OneOf",
",",
"_",
":",
"DebugContext",
")",
"->",
"bool",
":",
"return",
"any",
"(",
"matches",
"(",
"cntxt",
",",
"T",
",",
"e",
")",
"for",
"e",
"in",
"expr",
".",
"expressions",
")"
] | expr is a OneOf and there is some shape expression se2 in shapeExprs such that a matches(T, se2, m). | [
"expr",
"is",
"a",
"OneOf",
"and",
"there",
"is",
"some",
"shape",
"expression",
"se2",
"in",
"shapeExprs",
"such",
"that",
"a",
"matches",
"(",
"T",
"se2",
"m",
")",
"."
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/shape_expressions_language/p5_5_shapes_and_triple_expressions.py#L275-L279 | train |
hsolbrig/PyShEx | pyshex/shape_expressions_language/p5_5_shapes_and_triple_expressions.py | matchesEachOf | def matchesEachOf(cntxt: Context, T: RDFGraph, expr: ShExJ.EachOf, _: DebugContext) -> bool:
""" expr is an EachOf and there is some partition of T into T1, T2,… such that for every expression
expr1, expr2,… in shapeExprs, matches(Tn, exprn, m).
"""
return EachOfEvaluator(cntxt, T, expr).evaluate(cntxt) | python | def matchesEachOf(cntxt: Context, T: RDFGraph, expr: ShExJ.EachOf, _: DebugContext) -> bool:
""" expr is an EachOf and there is some partition of T into T1, T2,… such that for every expression
expr1, expr2,… in shapeExprs, matches(Tn, exprn, m).
"""
return EachOfEvaluator(cntxt, T, expr).evaluate(cntxt) | [
"def",
"matchesEachOf",
"(",
"cntxt",
":",
"Context",
",",
"T",
":",
"RDFGraph",
",",
"expr",
":",
"ShExJ",
".",
"EachOf",
",",
"_",
":",
"DebugContext",
")",
"->",
"bool",
":",
"return",
"EachOfEvaluator",
"(",
"cntxt",
",",
"T",
",",
"expr",
")",
".",
"evaluate",
"(",
"cntxt",
")"
] | expr is an EachOf and there is some partition of T into T1, T2,… such that for every expression
expr1, expr2,… in shapeExprs, matches(Tn, exprn, m). | [
"expr",
"is",
"an",
"EachOf",
"and",
"there",
"is",
"some",
"partition",
"of",
"T",
"into",
"T1",
"T2",
"…",
"such",
"that",
"for",
"every",
"expression",
"expr1",
"expr2",
"…",
"in",
"shapeExprs",
"matches",
"(",
"Tn",
"exprn",
"m",
")",
"."
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/shape_expressions_language/p5_5_shapes_and_triple_expressions.py#L283-L288 | train |
hsolbrig/PyShEx | pyshex/shape_expressions_language/p5_5_shapes_and_triple_expressions.py | matchesTripleConstraint | def matchesTripleConstraint(cntxt: Context, t: RDFTriple, expr: ShExJ.TripleConstraint, c: DebugContext) -> bool:
"""
expr is a TripleConstraint and:
* t is a triple
* t's predicate equals expr's predicate.
Let value be t's subject if inverse is true, else t's object.
* if inverse is true, t is in arcsIn, else t is in arcsOut.
"""
from pyshex.shape_expressions_language.p5_3_shape_expressions import satisfies
if c.debug:
print(c.i(1, f" triple: {t}"))
print(c.i(1, '', expr._as_json_dumps().split('\n')))
if uriref_matches_iriref(t.p, expr.predicate):
value = t.s if expr.inverse else t.o
return expr.valueExpr is None or satisfies(cntxt, value, expr.valueExpr)
else:
cntxt.fail_reason = f"Predicate mismatch: {t.p} ≠ {expr.predicate}"
return False | python | def matchesTripleConstraint(cntxt: Context, t: RDFTriple, expr: ShExJ.TripleConstraint, c: DebugContext) -> bool:
"""
expr is a TripleConstraint and:
* t is a triple
* t's predicate equals expr's predicate.
Let value be t's subject if inverse is true, else t's object.
* if inverse is true, t is in arcsIn, else t is in arcsOut.
"""
from pyshex.shape_expressions_language.p5_3_shape_expressions import satisfies
if c.debug:
print(c.i(1, f" triple: {t}"))
print(c.i(1, '', expr._as_json_dumps().split('\n')))
if uriref_matches_iriref(t.p, expr.predicate):
value = t.s if expr.inverse else t.o
return expr.valueExpr is None or satisfies(cntxt, value, expr.valueExpr)
else:
cntxt.fail_reason = f"Predicate mismatch: {t.p} ≠ {expr.predicate}"
return False | [
"def",
"matchesTripleConstraint",
"(",
"cntxt",
":",
"Context",
",",
"t",
":",
"RDFTriple",
",",
"expr",
":",
"ShExJ",
".",
"TripleConstraint",
",",
"c",
":",
"DebugContext",
")",
"->",
"bool",
":",
"from",
"pyshex",
".",
"shape_expressions_language",
".",
"p5_3_shape_expressions",
"import",
"satisfies",
"if",
"c",
".",
"debug",
":",
"print",
"(",
"c",
".",
"i",
"(",
"1",
",",
"f\" triple: {t}\"",
")",
")",
"print",
"(",
"c",
".",
"i",
"(",
"1",
",",
"''",
",",
"expr",
".",
"_as_json_dumps",
"(",
")",
".",
"split",
"(",
"'\\n'",
")",
")",
")",
"if",
"uriref_matches_iriref",
"(",
"t",
".",
"p",
",",
"expr",
".",
"predicate",
")",
":",
"value",
"=",
"t",
".",
"s",
"if",
"expr",
".",
"inverse",
"else",
"t",
".",
"o",
"return",
"expr",
".",
"valueExpr",
"is",
"None",
"or",
"satisfies",
"(",
"cntxt",
",",
"value",
",",
"expr",
".",
"valueExpr",
")",
"else",
":",
"cntxt",
".",
"fail_reason",
"=",
"f\"Predicate mismatch: {t.p} ≠ {expr.predicate}\"",
"return",
"False"
] | expr is a TripleConstraint and:
* t is a triple
* t's predicate equals expr's predicate.
Let value be t's subject if inverse is true, else t's object.
* if inverse is true, t is in arcsIn, else t is in arcsOut. | [
"expr",
"is",
"a",
"TripleConstraint",
"and",
":"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/shape_expressions_language/p5_5_shapes_and_triple_expressions.py#L292-L313 | train |
hsolbrig/PyShEx | pyshex/shape_expressions_language/p5_5_shapes_and_triple_expressions.py | matchesTripleExprRef | def matchesTripleExprRef(cntxt: Context, T: RDFGraph, expr: ShExJ.tripleExprLabel, _: DebugContext) -> bool:
"""
expr is an tripleExprRef and satisfies(value, tripleExprWithId(tripleExprRef), G, m).
The tripleExprWithId function is defined in Triple Expression Reference Requirement below.
"""
expr = cntxt.tripleExprFor(expr)
if expr is None:
cntxt.fail_reason = "{expr}: Reference not found"
return False
return matchesExpr(cntxt, T, expr) | python | def matchesTripleExprRef(cntxt: Context, T: RDFGraph, expr: ShExJ.tripleExprLabel, _: DebugContext) -> bool:
"""
expr is an tripleExprRef and satisfies(value, tripleExprWithId(tripleExprRef), G, m).
The tripleExprWithId function is defined in Triple Expression Reference Requirement below.
"""
expr = cntxt.tripleExprFor(expr)
if expr is None:
cntxt.fail_reason = "{expr}: Reference not found"
return False
return matchesExpr(cntxt, T, expr) | [
"def",
"matchesTripleExprRef",
"(",
"cntxt",
":",
"Context",
",",
"T",
":",
"RDFGraph",
",",
"expr",
":",
"ShExJ",
".",
"tripleExprLabel",
",",
"_",
":",
"DebugContext",
")",
"->",
"bool",
":",
"expr",
"=",
"cntxt",
".",
"tripleExprFor",
"(",
"expr",
")",
"if",
"expr",
"is",
"None",
":",
"cntxt",
".",
"fail_reason",
"=",
"\"{expr}: Reference not found\"",
"return",
"False",
"return",
"matchesExpr",
"(",
"cntxt",
",",
"T",
",",
"expr",
")"
] | expr is an tripleExprRef and satisfies(value, tripleExprWithId(tripleExprRef), G, m).
The tripleExprWithId function is defined in Triple Expression Reference Requirement below. | [
"expr",
"is",
"an",
"tripleExprRef",
"and",
"satisfies",
"(",
"value",
"tripleExprWithId",
"(",
"tripleExprRef",
")",
"G",
"m",
")",
".",
"The",
"tripleExprWithId",
"function",
"is",
"defined",
"in",
"Triple",
"Expression",
"Reference",
"Requirement",
"below",
"."
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/shape_expressions_language/p5_5_shapes_and_triple_expressions.py#L317-L326 | train |
jaysonsantos/python-binary-memcached | bmemcached/client/replicating.py | ReplicatingClient.get | def get(self, key, get_cas=False):
"""
Get a key from server.
:param key: Key's name
:type key: six.string_types
:param get_cas: If true, return (value, cas), where cas is the new CAS value.
:type get_cas: boolean
:return: Returns a key data from server.
:rtype: object
"""
for server in self.servers:
value, cas = server.get(key)
if value is not None:
if get_cas:
return value, cas
else:
return value
if get_cas:
return None, None | python | def get(self, key, get_cas=False):
"""
Get a key from server.
:param key: Key's name
:type key: six.string_types
:param get_cas: If true, return (value, cas), where cas is the new CAS value.
:type get_cas: boolean
:return: Returns a key data from server.
:rtype: object
"""
for server in self.servers:
value, cas = server.get(key)
if value is not None:
if get_cas:
return value, cas
else:
return value
if get_cas:
return None, None | [
"def",
"get",
"(",
"self",
",",
"key",
",",
"get_cas",
"=",
"False",
")",
":",
"for",
"server",
"in",
"self",
".",
"servers",
":",
"value",
",",
"cas",
"=",
"server",
".",
"get",
"(",
"key",
")",
"if",
"value",
"is",
"not",
"None",
":",
"if",
"get_cas",
":",
"return",
"value",
",",
"cas",
"else",
":",
"return",
"value",
"if",
"get_cas",
":",
"return",
"None",
",",
"None"
] | Get a key from server.
:param key: Key's name
:type key: six.string_types
:param get_cas: If true, return (value, cas), where cas is the new CAS value.
:type get_cas: boolean
:return: Returns a key data from server.
:rtype: object | [
"Get",
"a",
"key",
"from",
"server",
"."
] | 6a792829349c69204d9c5045e5c34b4231216dd6 | https://github.com/jaysonsantos/python-binary-memcached/blob/6a792829349c69204d9c5045e5c34b4231216dd6/bmemcached/client/replicating.py#L30-L49 | train |
jaysonsantos/python-binary-memcached | bmemcached/client/replicating.py | ReplicatingClient.gets | def gets(self, key):
"""
Get a key from server, returning the value and its CAS key.
This method is for API compatibility with other implementations.
:param key: Key's name
:type key: six.string_types
:return: Returns (key data, value), or (None, None) if the value is not in cache.
:rtype: object
"""
for server in self.servers:
value, cas = server.get(key)
if value is not None:
return value, cas
return None, None | python | def gets(self, key):
"""
Get a key from server, returning the value and its CAS key.
This method is for API compatibility with other implementations.
:param key: Key's name
:type key: six.string_types
:return: Returns (key data, value), or (None, None) if the value is not in cache.
:rtype: object
"""
for server in self.servers:
value, cas = server.get(key)
if value is not None:
return value, cas
return None, None | [
"def",
"gets",
"(",
"self",
",",
"key",
")",
":",
"for",
"server",
"in",
"self",
".",
"servers",
":",
"value",
",",
"cas",
"=",
"server",
".",
"get",
"(",
"key",
")",
"if",
"value",
"is",
"not",
"None",
":",
"return",
"value",
",",
"cas",
"return",
"None",
",",
"None"
] | Get a key from server, returning the value and its CAS key.
This method is for API compatibility with other implementations.
:param key: Key's name
:type key: six.string_types
:return: Returns (key data, value), or (None, None) if the value is not in cache.
:rtype: object | [
"Get",
"a",
"key",
"from",
"server",
"returning",
"the",
"value",
"and",
"its",
"CAS",
"key",
"."
] | 6a792829349c69204d9c5045e5c34b4231216dd6 | https://github.com/jaysonsantos/python-binary-memcached/blob/6a792829349c69204d9c5045e5c34b4231216dd6/bmemcached/client/replicating.py#L51-L66 | train |
jaysonsantos/python-binary-memcached | bmemcached/client/replicating.py | ReplicatingClient.get_multi | def get_multi(self, keys, get_cas=False):
"""
Get multiple keys from server.
:param keys: A list of keys to from server.
:type keys: list
:param get_cas: If get_cas is true, each value is (data, cas), with each result's CAS value.
:type get_cas: boolean
:return: A dict with all requested keys.
:rtype: dict
"""
d = {}
if keys:
for server in self.servers:
results = server.get_multi(keys)
if not get_cas:
# Remove CAS data
for key, (value, cas) in results.items():
results[key] = value
d.update(results)
keys = [_ for _ in keys if _ not in d]
if not keys:
break
return d | python | def get_multi(self, keys, get_cas=False):
"""
Get multiple keys from server.
:param keys: A list of keys to from server.
:type keys: list
:param get_cas: If get_cas is true, each value is (data, cas), with each result's CAS value.
:type get_cas: boolean
:return: A dict with all requested keys.
:rtype: dict
"""
d = {}
if keys:
for server in self.servers:
results = server.get_multi(keys)
if not get_cas:
# Remove CAS data
for key, (value, cas) in results.items():
results[key] = value
d.update(results)
keys = [_ for _ in keys if _ not in d]
if not keys:
break
return d | [
"def",
"get_multi",
"(",
"self",
",",
"keys",
",",
"get_cas",
"=",
"False",
")",
":",
"d",
"=",
"{",
"}",
"if",
"keys",
":",
"for",
"server",
"in",
"self",
".",
"servers",
":",
"results",
"=",
"server",
".",
"get_multi",
"(",
"keys",
")",
"if",
"not",
"get_cas",
":",
"# Remove CAS data",
"for",
"key",
",",
"(",
"value",
",",
"cas",
")",
"in",
"results",
".",
"items",
"(",
")",
":",
"results",
"[",
"key",
"]",
"=",
"value",
"d",
".",
"update",
"(",
"results",
")",
"keys",
"=",
"[",
"_",
"for",
"_",
"in",
"keys",
"if",
"_",
"not",
"in",
"d",
"]",
"if",
"not",
"keys",
":",
"break",
"return",
"d"
] | Get multiple keys from server.
:param keys: A list of keys to from server.
:type keys: list
:param get_cas: If get_cas is true, each value is (data, cas), with each result's CAS value.
:type get_cas: boolean
:return: A dict with all requested keys.
:rtype: dict | [
"Get",
"multiple",
"keys",
"from",
"server",
"."
] | 6a792829349c69204d9c5045e5c34b4231216dd6 | https://github.com/jaysonsantos/python-binary-memcached/blob/6a792829349c69204d9c5045e5c34b4231216dd6/bmemcached/client/replicating.py#L68-L91 | train |
jaysonsantos/python-binary-memcached | bmemcached/client/replicating.py | ReplicatingClient.set | def set(self, key, value, time=0, compress_level=-1):
"""
Set a value for a key on server.
:param key: Key's name
:type key: str
:param value: A value to be stored on server.
:type value: object
:param time: Time in seconds that your key will expire.
:type time: int
:param compress_level: How much to compress.
0 = no compression, 1 = fastest, 9 = slowest but best,
-1 = default compression level.
:type compress_level: int
:return: True in case of success and False in case of failure
:rtype: bool
"""
returns = []
for server in self.servers:
returns.append(server.set(key, value, time, compress_level=compress_level))
return any(returns) | python | def set(self, key, value, time=0, compress_level=-1):
"""
Set a value for a key on server.
:param key: Key's name
:type key: str
:param value: A value to be stored on server.
:type value: object
:param time: Time in seconds that your key will expire.
:type time: int
:param compress_level: How much to compress.
0 = no compression, 1 = fastest, 9 = slowest but best,
-1 = default compression level.
:type compress_level: int
:return: True in case of success and False in case of failure
:rtype: bool
"""
returns = []
for server in self.servers:
returns.append(server.set(key, value, time, compress_level=compress_level))
return any(returns) | [
"def",
"set",
"(",
"self",
",",
"key",
",",
"value",
",",
"time",
"=",
"0",
",",
"compress_level",
"=",
"-",
"1",
")",
":",
"returns",
"=",
"[",
"]",
"for",
"server",
"in",
"self",
".",
"servers",
":",
"returns",
".",
"append",
"(",
"server",
".",
"set",
"(",
"key",
",",
"value",
",",
"time",
",",
"compress_level",
"=",
"compress_level",
")",
")",
"return",
"any",
"(",
"returns",
")"
] | Set a value for a key on server.
:param key: Key's name
:type key: str
:param value: A value to be stored on server.
:type value: object
:param time: Time in seconds that your key will expire.
:type time: int
:param compress_level: How much to compress.
0 = no compression, 1 = fastest, 9 = slowest but best,
-1 = default compression level.
:type compress_level: int
:return: True in case of success and False in case of failure
:rtype: bool | [
"Set",
"a",
"value",
"for",
"a",
"key",
"on",
"server",
"."
] | 6a792829349c69204d9c5045e5c34b4231216dd6 | https://github.com/jaysonsantos/python-binary-memcached/blob/6a792829349c69204d9c5045e5c34b4231216dd6/bmemcached/client/replicating.py#L93-L114 | train |
jaysonsantos/python-binary-memcached | bmemcached/client/replicating.py | ReplicatingClient.set_multi | def set_multi(self, mappings, time=0, compress_level=-1):
"""
Set multiple keys with it's values on server.
:param mappings: A dict with keys/values
:type mappings: dict
:param time: Time in seconds that your key will expire.
:type time: int
:param compress_level: How much to compress.
0 = no compression, 1 = fastest, 9 = slowest but best,
-1 = default compression level.
:type compress_level: int
:return: True in case of success and False in case of failure
:rtype: bool
"""
returns = []
if mappings:
for server in self.servers:
returns.append(server.set_multi(mappings, time, compress_level=compress_level))
return all(returns) | python | def set_multi(self, mappings, time=0, compress_level=-1):
"""
Set multiple keys with it's values on server.
:param mappings: A dict with keys/values
:type mappings: dict
:param time: Time in seconds that your key will expire.
:type time: int
:param compress_level: How much to compress.
0 = no compression, 1 = fastest, 9 = slowest but best,
-1 = default compression level.
:type compress_level: int
:return: True in case of success and False in case of failure
:rtype: bool
"""
returns = []
if mappings:
for server in self.servers:
returns.append(server.set_multi(mappings, time, compress_level=compress_level))
return all(returns) | [
"def",
"set_multi",
"(",
"self",
",",
"mappings",
",",
"time",
"=",
"0",
",",
"compress_level",
"=",
"-",
"1",
")",
":",
"returns",
"=",
"[",
"]",
"if",
"mappings",
":",
"for",
"server",
"in",
"self",
".",
"servers",
":",
"returns",
".",
"append",
"(",
"server",
".",
"set_multi",
"(",
"mappings",
",",
"time",
",",
"compress_level",
"=",
"compress_level",
")",
")",
"return",
"all",
"(",
"returns",
")"
] | Set multiple keys with it's values on server.
:param mappings: A dict with keys/values
:type mappings: dict
:param time: Time in seconds that your key will expire.
:type time: int
:param compress_level: How much to compress.
0 = no compression, 1 = fastest, 9 = slowest but best,
-1 = default compression level.
:type compress_level: int
:return: True in case of success and False in case of failure
:rtype: bool | [
"Set",
"multiple",
"keys",
"with",
"it",
"s",
"values",
"on",
"server",
"."
] | 6a792829349c69204d9c5045e5c34b4231216dd6 | https://github.com/jaysonsantos/python-binary-memcached/blob/6a792829349c69204d9c5045e5c34b4231216dd6/bmemcached/client/replicating.py#L141-L161 | train |
jaysonsantos/python-binary-memcached | bmemcached/client/replicating.py | ReplicatingClient.delete | def delete(self, key, cas=0):
"""
Delete a key/value from server. If key does not exist, it returns True.
:param key: Key's name to be deleted
:param cas: CAS of the key
:return: True in case o success and False in case of failure.
"""
returns = []
for server in self.servers:
returns.append(server.delete(key, cas))
return any(returns) | python | def delete(self, key, cas=0):
"""
Delete a key/value from server. If key does not exist, it returns True.
:param key: Key's name to be deleted
:param cas: CAS of the key
:return: True in case o success and False in case of failure.
"""
returns = []
for server in self.servers:
returns.append(server.delete(key, cas))
return any(returns) | [
"def",
"delete",
"(",
"self",
",",
"key",
",",
"cas",
"=",
"0",
")",
":",
"returns",
"=",
"[",
"]",
"for",
"server",
"in",
"self",
".",
"servers",
":",
"returns",
".",
"append",
"(",
"server",
".",
"delete",
"(",
"key",
",",
"cas",
")",
")",
"return",
"any",
"(",
"returns",
")"
] | Delete a key/value from server. If key does not exist, it returns True.
:param key: Key's name to be deleted
:param cas: CAS of the key
:return: True in case o success and False in case of failure. | [
"Delete",
"a",
"key",
"/",
"value",
"from",
"server",
".",
"If",
"key",
"does",
"not",
"exist",
"it",
"returns",
"True",
"."
] | 6a792829349c69204d9c5045e5c34b4231216dd6 | https://github.com/jaysonsantos/python-binary-memcached/blob/6a792829349c69204d9c5045e5c34b4231216dd6/bmemcached/client/replicating.py#L209-L221 | train |
jaysonsantos/python-binary-memcached | bmemcached/client/replicating.py | ReplicatingClient.incr | def incr(self, key, value):
"""
Increment a key, if it exists, returns it's actual value, if it don't, return 0.
:param key: Key's name
:type key: six.string_types
:param value: Number to be incremented
:type value: int
:return: Actual value of the key on server
:rtype: int
"""
returns = []
for server in self.servers:
returns.append(server.incr(key, value))
return returns[0] | python | def incr(self, key, value):
"""
Increment a key, if it exists, returns it's actual value, if it don't, return 0.
:param key: Key's name
:type key: six.string_types
:param value: Number to be incremented
:type value: int
:return: Actual value of the key on server
:rtype: int
"""
returns = []
for server in self.servers:
returns.append(server.incr(key, value))
return returns[0] | [
"def",
"incr",
"(",
"self",
",",
"key",
",",
"value",
")",
":",
"returns",
"=",
"[",
"]",
"for",
"server",
"in",
"self",
".",
"servers",
":",
"returns",
".",
"append",
"(",
"server",
".",
"incr",
"(",
"key",
",",
"value",
")",
")",
"return",
"returns",
"[",
"0",
"]"
] | Increment a key, if it exists, returns it's actual value, if it don't, return 0.
:param key: Key's name
:type key: six.string_types
:param value: Number to be incremented
:type value: int
:return: Actual value of the key on server
:rtype: int | [
"Increment",
"a",
"key",
"if",
"it",
"exists",
"returns",
"it",
"s",
"actual",
"value",
"if",
"it",
"don",
"t",
"return",
"0",
"."
] | 6a792829349c69204d9c5045e5c34b4231216dd6 | https://github.com/jaysonsantos/python-binary-memcached/blob/6a792829349c69204d9c5045e5c34b4231216dd6/bmemcached/client/replicating.py#L230-L245 | train |
jaysonsantos/python-binary-memcached | bmemcached/client/replicating.py | ReplicatingClient.decr | def decr(self, key, value):
"""
Decrement a key, if it exists, returns it's actual value, if it don't, return 0.
Minimum value of decrement return is 0.
:param key: Key's name
:type key: six.string_types
:param value: Number to be decremented
:type value: int
:return: Actual value of the key on server
:rtype: int
"""
returns = []
for server in self.servers:
returns.append(server.decr(key, value))
return returns[0] | python | def decr(self, key, value):
"""
Decrement a key, if it exists, returns it's actual value, if it don't, return 0.
Minimum value of decrement return is 0.
:param key: Key's name
:type key: six.string_types
:param value: Number to be decremented
:type value: int
:return: Actual value of the key on server
:rtype: int
"""
returns = []
for server in self.servers:
returns.append(server.decr(key, value))
return returns[0] | [
"def",
"decr",
"(",
"self",
",",
"key",
",",
"value",
")",
":",
"returns",
"=",
"[",
"]",
"for",
"server",
"in",
"self",
".",
"servers",
":",
"returns",
".",
"append",
"(",
"server",
".",
"decr",
"(",
"key",
",",
"value",
")",
")",
"return",
"returns",
"[",
"0",
"]"
] | Decrement a key, if it exists, returns it's actual value, if it don't, return 0.
Minimum value of decrement return is 0.
:param key: Key's name
:type key: six.string_types
:param value: Number to be decremented
:type value: int
:return: Actual value of the key on server
:rtype: int | [
"Decrement",
"a",
"key",
"if",
"it",
"exists",
"returns",
"it",
"s",
"actual",
"value",
"if",
"it",
"don",
"t",
"return",
"0",
".",
"Minimum",
"value",
"of",
"decrement",
"return",
"is",
"0",
"."
] | 6a792829349c69204d9c5045e5c34b4231216dd6 | https://github.com/jaysonsantos/python-binary-memcached/blob/6a792829349c69204d9c5045e5c34b4231216dd6/bmemcached/client/replicating.py#L247-L263 | train |
hsolbrig/PyShEx | pyshex/parse_tree/parse_node.py | ParseNode.set_result | def set_result(self, rval: bool) -> None:
""" Set the result of the evaluation. If the result is true, prune all of the children that didn't cut it
:param rval: Result of evaluation
"""
self.result = rval
if self.result:
self.nodes = [pn for pn in self.nodes if pn.result] | python | def set_result(self, rval: bool) -> None:
""" Set the result of the evaluation. If the result is true, prune all of the children that didn't cut it
:param rval: Result of evaluation
"""
self.result = rval
if self.result:
self.nodes = [pn for pn in self.nodes if pn.result] | [
"def",
"set_result",
"(",
"self",
",",
"rval",
":",
"bool",
")",
"->",
"None",
":",
"self",
".",
"result",
"=",
"rval",
"if",
"self",
".",
"result",
":",
"self",
".",
"nodes",
"=",
"[",
"pn",
"for",
"pn",
"in",
"self",
".",
"nodes",
"if",
"pn",
".",
"result",
"]"
] | Set the result of the evaluation. If the result is true, prune all of the children that didn't cut it
:param rval: Result of evaluation | [
"Set",
"the",
"result",
"of",
"the",
"evaluation",
".",
"If",
"the",
"result",
"is",
"true",
"prune",
"all",
"of",
"the",
"children",
"that",
"didn",
"t",
"cut",
"it"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/parse_tree/parse_node.py#L67-L74 | train |
hsolbrig/PyShEx | pyshex/utils/schema_utils.py | reference_of | def reference_of(selector: shapeLabel, cntxt: Union[Context, ShExJ.Schema] ) -> Optional[ShExJ.shapeExpr]:
""" Return the shape expression in the schema referenced by selector, if any
:param cntxt: Context node or ShEx Schema
:param selector: identifier of element to select within the schema
:return:
"""
schema = cntxt.schema if isinstance(cntxt, Context) else cntxt
if selector is START:
return schema.start
for expr in schema.shapes:
if not isinstance(expr, ShExJ.ShapeExternal) and expr.id == selector:
return expr
return schema.start if schema.start is not None and schema.start.id == selector else None | python | def reference_of(selector: shapeLabel, cntxt: Union[Context, ShExJ.Schema] ) -> Optional[ShExJ.shapeExpr]:
""" Return the shape expression in the schema referenced by selector, if any
:param cntxt: Context node or ShEx Schema
:param selector: identifier of element to select within the schema
:return:
"""
schema = cntxt.schema if isinstance(cntxt, Context) else cntxt
if selector is START:
return schema.start
for expr in schema.shapes:
if not isinstance(expr, ShExJ.ShapeExternal) and expr.id == selector:
return expr
return schema.start if schema.start is not None and schema.start.id == selector else None | [
"def",
"reference_of",
"(",
"selector",
":",
"shapeLabel",
",",
"cntxt",
":",
"Union",
"[",
"Context",
",",
"ShExJ",
".",
"Schema",
"]",
")",
"->",
"Optional",
"[",
"ShExJ",
".",
"shapeExpr",
"]",
":",
"schema",
"=",
"cntxt",
".",
"schema",
"if",
"isinstance",
"(",
"cntxt",
",",
"Context",
")",
"else",
"cntxt",
"if",
"selector",
"is",
"START",
":",
"return",
"schema",
".",
"start",
"for",
"expr",
"in",
"schema",
".",
"shapes",
":",
"if",
"not",
"isinstance",
"(",
"expr",
",",
"ShExJ",
".",
"ShapeExternal",
")",
"and",
"expr",
".",
"id",
"==",
"selector",
":",
"return",
"expr",
"return",
"schema",
".",
"start",
"if",
"schema",
".",
"start",
"is",
"not",
"None",
"and",
"schema",
".",
"start",
".",
"id",
"==",
"selector",
"else",
"None"
] | Return the shape expression in the schema referenced by selector, if any
:param cntxt: Context node or ShEx Schema
:param selector: identifier of element to select within the schema
:return: | [
"Return",
"the",
"shape",
"expression",
"in",
"the",
"schema",
"referenced",
"by",
"selector",
"if",
"any"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/utils/schema_utils.py#L10-L23 | train |
hsolbrig/PyShEx | pyshex/utils/schema_utils.py | triple_reference_of | def triple_reference_of(label: ShExJ.tripleExprLabel, cntxt: Context) -> Optional[ShExJ.tripleExpr]:
""" Search for the label in a Schema """
te: Optional[ShExJ.tripleExpr] = None
if cntxt.schema.start is not None:
te = triple_in_shape(cntxt.schema.start, label, cntxt)
if te is None:
for shapeExpr in cntxt.schema.shapes:
te = triple_in_shape(shapeExpr, label, cntxt)
if te:
break
return te | python | def triple_reference_of(label: ShExJ.tripleExprLabel, cntxt: Context) -> Optional[ShExJ.tripleExpr]:
""" Search for the label in a Schema """
te: Optional[ShExJ.tripleExpr] = None
if cntxt.schema.start is not None:
te = triple_in_shape(cntxt.schema.start, label, cntxt)
if te is None:
for shapeExpr in cntxt.schema.shapes:
te = triple_in_shape(shapeExpr, label, cntxt)
if te:
break
return te | [
"def",
"triple_reference_of",
"(",
"label",
":",
"ShExJ",
".",
"tripleExprLabel",
",",
"cntxt",
":",
"Context",
")",
"->",
"Optional",
"[",
"ShExJ",
".",
"tripleExpr",
"]",
":",
"te",
":",
"Optional",
"[",
"ShExJ",
".",
"tripleExpr",
"]",
"=",
"None",
"if",
"cntxt",
".",
"schema",
".",
"start",
"is",
"not",
"None",
":",
"te",
"=",
"triple_in_shape",
"(",
"cntxt",
".",
"schema",
".",
"start",
",",
"label",
",",
"cntxt",
")",
"if",
"te",
"is",
"None",
":",
"for",
"shapeExpr",
"in",
"cntxt",
".",
"schema",
".",
"shapes",
":",
"te",
"=",
"triple_in_shape",
"(",
"shapeExpr",
",",
"label",
",",
"cntxt",
")",
"if",
"te",
":",
"break",
"return",
"te"
] | Search for the label in a Schema | [
"Search",
"for",
"the",
"label",
"in",
"a",
"Schema"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/utils/schema_utils.py#L26-L36 | train |
hsolbrig/PyShEx | pyshex/utils/schema_utils.py | triple_in_shape | def triple_in_shape(expr: ShExJ.shapeExpr, label: ShExJ.tripleExprLabel, cntxt: Context) \
-> Optional[ShExJ.tripleExpr]:
""" Search for the label in a shape expression """
te = None
if isinstance(expr, (ShExJ.ShapeOr, ShExJ.ShapeAnd)):
for expr2 in expr.shapeExprs:
te = triple_in_shape(expr2, label, cntxt)
if te is not None:
break
elif isinstance(expr, ShExJ.ShapeNot):
te = triple_in_shape(expr.shapeExpr, label, cntxt)
elif isinstance(expr, ShExJ.shapeExprLabel):
se = reference_of(expr, cntxt)
if se is not None:
te = triple_in_shape(se, label, cntxt)
return te | python | def triple_in_shape(expr: ShExJ.shapeExpr, label: ShExJ.tripleExprLabel, cntxt: Context) \
-> Optional[ShExJ.tripleExpr]:
""" Search for the label in a shape expression """
te = None
if isinstance(expr, (ShExJ.ShapeOr, ShExJ.ShapeAnd)):
for expr2 in expr.shapeExprs:
te = triple_in_shape(expr2, label, cntxt)
if te is not None:
break
elif isinstance(expr, ShExJ.ShapeNot):
te = triple_in_shape(expr.shapeExpr, label, cntxt)
elif isinstance(expr, ShExJ.shapeExprLabel):
se = reference_of(expr, cntxt)
if se is not None:
te = triple_in_shape(se, label, cntxt)
return te | [
"def",
"triple_in_shape",
"(",
"expr",
":",
"ShExJ",
".",
"shapeExpr",
",",
"label",
":",
"ShExJ",
".",
"tripleExprLabel",
",",
"cntxt",
":",
"Context",
")",
"->",
"Optional",
"[",
"ShExJ",
".",
"tripleExpr",
"]",
":",
"te",
"=",
"None",
"if",
"isinstance",
"(",
"expr",
",",
"(",
"ShExJ",
".",
"ShapeOr",
",",
"ShExJ",
".",
"ShapeAnd",
")",
")",
":",
"for",
"expr2",
"in",
"expr",
".",
"shapeExprs",
":",
"te",
"=",
"triple_in_shape",
"(",
"expr2",
",",
"label",
",",
"cntxt",
")",
"if",
"te",
"is",
"not",
"None",
":",
"break",
"elif",
"isinstance",
"(",
"expr",
",",
"ShExJ",
".",
"ShapeNot",
")",
":",
"te",
"=",
"triple_in_shape",
"(",
"expr",
".",
"shapeExpr",
",",
"label",
",",
"cntxt",
")",
"elif",
"isinstance",
"(",
"expr",
",",
"ShExJ",
".",
"shapeExprLabel",
")",
":",
"se",
"=",
"reference_of",
"(",
"expr",
",",
"cntxt",
")",
"if",
"se",
"is",
"not",
"None",
":",
"te",
"=",
"triple_in_shape",
"(",
"se",
",",
"label",
",",
"cntxt",
")",
"return",
"te"
] | Search for the label in a shape expression | [
"Search",
"for",
"the",
"label",
"in",
"a",
"shape",
"expression"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/utils/schema_utils.py#L39-L54 | train |
hsolbrig/PyShEx | pyshex/utils/schema_utils.py | predicates_in_expression | def predicates_in_expression(expression: ShExJ.shapeExpr, cntxt: Context) -> List[IRIREF]:
""" Return the set of predicates that "appears in a TripleConstraint in an expression
See: `5.5.2 Semantics <http://shex.io/shex-semantics/#triple-expressions-semantics>`_ for details
:param expression: Expression to scan for predicates
:param cntxt: Context of evaluation
:return: List of predicates
"""
return list(directed_predicates_in_expression(expression, cntxt).keys()) | python | def predicates_in_expression(expression: ShExJ.shapeExpr, cntxt: Context) -> List[IRIREF]:
""" Return the set of predicates that "appears in a TripleConstraint in an expression
See: `5.5.2 Semantics <http://shex.io/shex-semantics/#triple-expressions-semantics>`_ for details
:param expression: Expression to scan for predicates
:param cntxt: Context of evaluation
:return: List of predicates
"""
return list(directed_predicates_in_expression(expression, cntxt).keys()) | [
"def",
"predicates_in_expression",
"(",
"expression",
":",
"ShExJ",
".",
"shapeExpr",
",",
"cntxt",
":",
"Context",
")",
"->",
"List",
"[",
"IRIREF",
"]",
":",
"return",
"list",
"(",
"directed_predicates_in_expression",
"(",
"expression",
",",
"cntxt",
")",
".",
"keys",
"(",
")",
")"
] | Return the set of predicates that "appears in a TripleConstraint in an expression
See: `5.5.2 Semantics <http://shex.io/shex-semantics/#triple-expressions-semantics>`_ for details
:param expression: Expression to scan for predicates
:param cntxt: Context of evaluation
:return: List of predicates | [
"Return",
"the",
"set",
"of",
"predicates",
"that",
"appears",
"in",
"a",
"TripleConstraint",
"in",
"an",
"expression",
"See",
":",
"5",
".",
"5",
".",
"2",
"Semantics",
"<http",
":",
"//",
"shex",
".",
"io",
"/",
"shex",
"-",
"semantics",
"/",
"#triple",
"-",
"expressions",
"-",
"semantics",
">",
"_",
"for",
"details"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/utils/schema_utils.py#L68-L77 | train |
hsolbrig/PyShEx | pyshex/utils/schema_utils.py | directed_predicates_in_expression | def directed_predicates_in_expression(expression: ShExJ.shapeExpr, cntxt: Context) -> Dict[IRIREF, PredDirection]:
""" Directed predicates in expression -- return all predicates in shapeExpr along with which direction(s) they
evaluate
:param expression: Expression to scan
:param cntxt:
:return:
"""
dir_predicates: Dict[IRIREF, PredDirection] = {}
def predicate_finder(predicates: Dict[IRIREF, PredDirection], tc: ShExJ.TripleConstraint, _: Context) -> None:
if isinstance(tc, ShExJ.TripleConstraint):
predicates.setdefault(tc.predicate, PredDirection()).dir(tc.inverse is None or not tc.inverse)
def triple_expr_finder(predicates: Dict[IRIREF, PredDirection], expr: ShExJ.shapeExpr, cntxt_: Context) -> None:
if isinstance(expr, ShExJ.Shape) and expr.expression is not None:
cntxt_.visit_triple_expressions(expr.expression, predicate_finder, predicates)
# TODO: follow_inner_shapes as True probably goes too far, but we definitely need to cross shape/triplecons
cntxt.visit_shapes(expression, triple_expr_finder, dir_predicates, follow_inner_shapes=False)
return dir_predicates | python | def directed_predicates_in_expression(expression: ShExJ.shapeExpr, cntxt: Context) -> Dict[IRIREF, PredDirection]:
""" Directed predicates in expression -- return all predicates in shapeExpr along with which direction(s) they
evaluate
:param expression: Expression to scan
:param cntxt:
:return:
"""
dir_predicates: Dict[IRIREF, PredDirection] = {}
def predicate_finder(predicates: Dict[IRIREF, PredDirection], tc: ShExJ.TripleConstraint, _: Context) -> None:
if isinstance(tc, ShExJ.TripleConstraint):
predicates.setdefault(tc.predicate, PredDirection()).dir(tc.inverse is None or not tc.inverse)
def triple_expr_finder(predicates: Dict[IRIREF, PredDirection], expr: ShExJ.shapeExpr, cntxt_: Context) -> None:
if isinstance(expr, ShExJ.Shape) and expr.expression is not None:
cntxt_.visit_triple_expressions(expr.expression, predicate_finder, predicates)
# TODO: follow_inner_shapes as True probably goes too far, but we definitely need to cross shape/triplecons
cntxt.visit_shapes(expression, triple_expr_finder, dir_predicates, follow_inner_shapes=False)
return dir_predicates | [
"def",
"directed_predicates_in_expression",
"(",
"expression",
":",
"ShExJ",
".",
"shapeExpr",
",",
"cntxt",
":",
"Context",
")",
"->",
"Dict",
"[",
"IRIREF",
",",
"PredDirection",
"]",
":",
"dir_predicates",
":",
"Dict",
"[",
"IRIREF",
",",
"PredDirection",
"]",
"=",
"{",
"}",
"def",
"predicate_finder",
"(",
"predicates",
":",
"Dict",
"[",
"IRIREF",
",",
"PredDirection",
"]",
",",
"tc",
":",
"ShExJ",
".",
"TripleConstraint",
",",
"_",
":",
"Context",
")",
"->",
"None",
":",
"if",
"isinstance",
"(",
"tc",
",",
"ShExJ",
".",
"TripleConstraint",
")",
":",
"predicates",
".",
"setdefault",
"(",
"tc",
".",
"predicate",
",",
"PredDirection",
"(",
")",
")",
".",
"dir",
"(",
"tc",
".",
"inverse",
"is",
"None",
"or",
"not",
"tc",
".",
"inverse",
")",
"def",
"triple_expr_finder",
"(",
"predicates",
":",
"Dict",
"[",
"IRIREF",
",",
"PredDirection",
"]",
",",
"expr",
":",
"ShExJ",
".",
"shapeExpr",
",",
"cntxt_",
":",
"Context",
")",
"->",
"None",
":",
"if",
"isinstance",
"(",
"expr",
",",
"ShExJ",
".",
"Shape",
")",
"and",
"expr",
".",
"expression",
"is",
"not",
"None",
":",
"cntxt_",
".",
"visit_triple_expressions",
"(",
"expr",
".",
"expression",
",",
"predicate_finder",
",",
"predicates",
")",
"# TODO: follow_inner_shapes as True probably goes too far, but we definitely need to cross shape/triplecons",
"cntxt",
".",
"visit_shapes",
"(",
"expression",
",",
"triple_expr_finder",
",",
"dir_predicates",
",",
"follow_inner_shapes",
"=",
"False",
")",
"return",
"dir_predicates"
] | Directed predicates in expression -- return all predicates in shapeExpr along with which direction(s) they
evaluate
:param expression: Expression to scan
:param cntxt:
:return: | [
"Directed",
"predicates",
"in",
"expression",
"--",
"return",
"all",
"predicates",
"in",
"shapeExpr",
"along",
"with",
"which",
"direction",
"(",
"s",
")",
"they",
"evaluate"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/utils/schema_utils.py#L92-L112 | train |
hsolbrig/PyShEx | pyshex/utils/partitions.py | algorithm_u | def algorithm_u(ns, m):
"""
taken from `Stack Overflow <https://codereview.stackexchange.com/questions/1526/finding-all-k-subset-partitions>`_
"""
def visit(nv, av):
ps = [[] for _ in range(m)]
for jv in range(nv):
ps[av[jv + 1]].append(ns[jv])
return ps
def f(mu, nu, sigma, n, a):
if mu == 2:
yield visit(n, a)
else:
for v in f(mu - 1, nu - 1, (mu + sigma) % 2, n, a):
yield v
if nu == mu + 1:
a[mu] = mu - 1
yield visit(n, a)
while a[nu] > 0:
a[nu] = a[nu] - 1
yield visit(n, a)
elif nu > mu + 1:
if (mu + sigma) % 2 == 1:
a[nu - 1] = mu - 1
else:
a[mu] = mu - 1
if (a[nu] + sigma) % 2 == 1:
for v in b(mu, nu - 1, 0, n, a):
yield v
else:
for v in f(mu, nu - 1, 0, n, a):
yield v
while a[nu] > 0:
a[nu] = a[nu] - 1
if (a[nu] + sigma) % 2 == 1:
for v in b(mu, nu - 1, 0, n, a):
yield v
else:
for v in f(mu, nu - 1, 0, n, a):
yield v
def b(mu, nu, sigma, n, a):
if nu == mu + 1:
while a[nu] < mu - 1:
yield visit(n, a)
a[nu] = a[nu] + 1
yield visit(n, a)
a[mu] = 0
elif nu > mu + 1:
if (a[nu] + sigma) % 2 == 1:
for v in f(mu, nu - 1, 0, n, a):
yield v
else:
for v in b(mu, nu - 1, 0, n, a):
yield v
while a[nu] < mu - 1:
a[nu] = a[nu] + 1
if (a[nu] + sigma) % 2 == 1:
for v in f(mu, nu - 1, 0, n, a):
yield v
else:
for v in b(mu, nu - 1, 0, n, a):
yield v
if (mu + sigma) % 2 == 1:
a[nu - 1] = 0
else:
a[mu] = 0
if mu == 2:
yield visit(n, a)
else:
for v in b(mu - 1, nu - 1, (mu + sigma) % 2, n, a):
yield v
ng = len(ns)
ag = [0] * (ng + 1)
for j in range(1, m + 1):
ag[ng - m + j] = j - 1
return f(m, ng, 0, ng, ag) if m > 1 else [[ns]] | python | def algorithm_u(ns, m):
"""
taken from `Stack Overflow <https://codereview.stackexchange.com/questions/1526/finding-all-k-subset-partitions>`_
"""
def visit(nv, av):
ps = [[] for _ in range(m)]
for jv in range(nv):
ps[av[jv + 1]].append(ns[jv])
return ps
def f(mu, nu, sigma, n, a):
if mu == 2:
yield visit(n, a)
else:
for v in f(mu - 1, nu - 1, (mu + sigma) % 2, n, a):
yield v
if nu == mu + 1:
a[mu] = mu - 1
yield visit(n, a)
while a[nu] > 0:
a[nu] = a[nu] - 1
yield visit(n, a)
elif nu > mu + 1:
if (mu + sigma) % 2 == 1:
a[nu - 1] = mu - 1
else:
a[mu] = mu - 1
if (a[nu] + sigma) % 2 == 1:
for v in b(mu, nu - 1, 0, n, a):
yield v
else:
for v in f(mu, nu - 1, 0, n, a):
yield v
while a[nu] > 0:
a[nu] = a[nu] - 1
if (a[nu] + sigma) % 2 == 1:
for v in b(mu, nu - 1, 0, n, a):
yield v
else:
for v in f(mu, nu - 1, 0, n, a):
yield v
def b(mu, nu, sigma, n, a):
if nu == mu + 1:
while a[nu] < mu - 1:
yield visit(n, a)
a[nu] = a[nu] + 1
yield visit(n, a)
a[mu] = 0
elif nu > mu + 1:
if (a[nu] + sigma) % 2 == 1:
for v in f(mu, nu - 1, 0, n, a):
yield v
else:
for v in b(mu, nu - 1, 0, n, a):
yield v
while a[nu] < mu - 1:
a[nu] = a[nu] + 1
if (a[nu] + sigma) % 2 == 1:
for v in f(mu, nu - 1, 0, n, a):
yield v
else:
for v in b(mu, nu - 1, 0, n, a):
yield v
if (mu + sigma) % 2 == 1:
a[nu - 1] = 0
else:
a[mu] = 0
if mu == 2:
yield visit(n, a)
else:
for v in b(mu - 1, nu - 1, (mu + sigma) % 2, n, a):
yield v
ng = len(ns)
ag = [0] * (ng + 1)
for j in range(1, m + 1):
ag[ng - m + j] = j - 1
return f(m, ng, 0, ng, ag) if m > 1 else [[ns]] | [
"def",
"algorithm_u",
"(",
"ns",
",",
"m",
")",
":",
"def",
"visit",
"(",
"nv",
",",
"av",
")",
":",
"ps",
"=",
"[",
"[",
"]",
"for",
"_",
"in",
"range",
"(",
"m",
")",
"]",
"for",
"jv",
"in",
"range",
"(",
"nv",
")",
":",
"ps",
"[",
"av",
"[",
"jv",
"+",
"1",
"]",
"]",
".",
"append",
"(",
"ns",
"[",
"jv",
"]",
")",
"return",
"ps",
"def",
"f",
"(",
"mu",
",",
"nu",
",",
"sigma",
",",
"n",
",",
"a",
")",
":",
"if",
"mu",
"==",
"2",
":",
"yield",
"visit",
"(",
"n",
",",
"a",
")",
"else",
":",
"for",
"v",
"in",
"f",
"(",
"mu",
"-",
"1",
",",
"nu",
"-",
"1",
",",
"(",
"mu",
"+",
"sigma",
")",
"%",
"2",
",",
"n",
",",
"a",
")",
":",
"yield",
"v",
"if",
"nu",
"==",
"mu",
"+",
"1",
":",
"a",
"[",
"mu",
"]",
"=",
"mu",
"-",
"1",
"yield",
"visit",
"(",
"n",
",",
"a",
")",
"while",
"a",
"[",
"nu",
"]",
">",
"0",
":",
"a",
"[",
"nu",
"]",
"=",
"a",
"[",
"nu",
"]",
"-",
"1",
"yield",
"visit",
"(",
"n",
",",
"a",
")",
"elif",
"nu",
">",
"mu",
"+",
"1",
":",
"if",
"(",
"mu",
"+",
"sigma",
")",
"%",
"2",
"==",
"1",
":",
"a",
"[",
"nu",
"-",
"1",
"]",
"=",
"mu",
"-",
"1",
"else",
":",
"a",
"[",
"mu",
"]",
"=",
"mu",
"-",
"1",
"if",
"(",
"a",
"[",
"nu",
"]",
"+",
"sigma",
")",
"%",
"2",
"==",
"1",
":",
"for",
"v",
"in",
"b",
"(",
"mu",
",",
"nu",
"-",
"1",
",",
"0",
",",
"n",
",",
"a",
")",
":",
"yield",
"v",
"else",
":",
"for",
"v",
"in",
"f",
"(",
"mu",
",",
"nu",
"-",
"1",
",",
"0",
",",
"n",
",",
"a",
")",
":",
"yield",
"v",
"while",
"a",
"[",
"nu",
"]",
">",
"0",
":",
"a",
"[",
"nu",
"]",
"=",
"a",
"[",
"nu",
"]",
"-",
"1",
"if",
"(",
"a",
"[",
"nu",
"]",
"+",
"sigma",
")",
"%",
"2",
"==",
"1",
":",
"for",
"v",
"in",
"b",
"(",
"mu",
",",
"nu",
"-",
"1",
",",
"0",
",",
"n",
",",
"a",
")",
":",
"yield",
"v",
"else",
":",
"for",
"v",
"in",
"f",
"(",
"mu",
",",
"nu",
"-",
"1",
",",
"0",
",",
"n",
",",
"a",
")",
":",
"yield",
"v",
"def",
"b",
"(",
"mu",
",",
"nu",
",",
"sigma",
",",
"n",
",",
"a",
")",
":",
"if",
"nu",
"==",
"mu",
"+",
"1",
":",
"while",
"a",
"[",
"nu",
"]",
"<",
"mu",
"-",
"1",
":",
"yield",
"visit",
"(",
"n",
",",
"a",
")",
"a",
"[",
"nu",
"]",
"=",
"a",
"[",
"nu",
"]",
"+",
"1",
"yield",
"visit",
"(",
"n",
",",
"a",
")",
"a",
"[",
"mu",
"]",
"=",
"0",
"elif",
"nu",
">",
"mu",
"+",
"1",
":",
"if",
"(",
"a",
"[",
"nu",
"]",
"+",
"sigma",
")",
"%",
"2",
"==",
"1",
":",
"for",
"v",
"in",
"f",
"(",
"mu",
",",
"nu",
"-",
"1",
",",
"0",
",",
"n",
",",
"a",
")",
":",
"yield",
"v",
"else",
":",
"for",
"v",
"in",
"b",
"(",
"mu",
",",
"nu",
"-",
"1",
",",
"0",
",",
"n",
",",
"a",
")",
":",
"yield",
"v",
"while",
"a",
"[",
"nu",
"]",
"<",
"mu",
"-",
"1",
":",
"a",
"[",
"nu",
"]",
"=",
"a",
"[",
"nu",
"]",
"+",
"1",
"if",
"(",
"a",
"[",
"nu",
"]",
"+",
"sigma",
")",
"%",
"2",
"==",
"1",
":",
"for",
"v",
"in",
"f",
"(",
"mu",
",",
"nu",
"-",
"1",
",",
"0",
",",
"n",
",",
"a",
")",
":",
"yield",
"v",
"else",
":",
"for",
"v",
"in",
"b",
"(",
"mu",
",",
"nu",
"-",
"1",
",",
"0",
",",
"n",
",",
"a",
")",
":",
"yield",
"v",
"if",
"(",
"mu",
"+",
"sigma",
")",
"%",
"2",
"==",
"1",
":",
"a",
"[",
"nu",
"-",
"1",
"]",
"=",
"0",
"else",
":",
"a",
"[",
"mu",
"]",
"=",
"0",
"if",
"mu",
"==",
"2",
":",
"yield",
"visit",
"(",
"n",
",",
"a",
")",
"else",
":",
"for",
"v",
"in",
"b",
"(",
"mu",
"-",
"1",
",",
"nu",
"-",
"1",
",",
"(",
"mu",
"+",
"sigma",
")",
"%",
"2",
",",
"n",
",",
"a",
")",
":",
"yield",
"v",
"ng",
"=",
"len",
"(",
"ns",
")",
"ag",
"=",
"[",
"0",
"]",
"*",
"(",
"ng",
"+",
"1",
")",
"for",
"j",
"in",
"range",
"(",
"1",
",",
"m",
"+",
"1",
")",
":",
"ag",
"[",
"ng",
"-",
"m",
"+",
"j",
"]",
"=",
"j",
"-",
"1",
"return",
"f",
"(",
"m",
",",
"ng",
",",
"0",
",",
"ng",
",",
"ag",
")",
"if",
"m",
">",
"1",
"else",
"[",
"[",
"ns",
"]",
"]"
] | taken from `Stack Overflow <https://codereview.stackexchange.com/questions/1526/finding-all-k-subset-partitions>`_ | [
"taken",
"from",
"Stack",
"Overflow",
"<https",
":",
"//",
"codereview",
".",
"stackexchange",
".",
"com",
"/",
"questions",
"/",
"1526",
"/",
"finding",
"-",
"all",
"-",
"k",
"-",
"subset",
"-",
"partitions",
">",
"_"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/utils/partitions.py#L11-L90 | train |
hsolbrig/PyShEx | pyshex/utils/partitions.py | integer_partition | def integer_partition(size: int, nparts: int) -> Iterator[List[List[int]]]:
""" Partition a list of integers into a list of partitions """
for part in algorithm_u(range(size), nparts):
yield part | python | def integer_partition(size: int, nparts: int) -> Iterator[List[List[int]]]:
""" Partition a list of integers into a list of partitions """
for part in algorithm_u(range(size), nparts):
yield part | [
"def",
"integer_partition",
"(",
"size",
":",
"int",
",",
"nparts",
":",
"int",
")",
"->",
"Iterator",
"[",
"List",
"[",
"List",
"[",
"int",
"]",
"]",
"]",
":",
"for",
"part",
"in",
"algorithm_u",
"(",
"range",
"(",
"size",
")",
",",
"nparts",
")",
":",
"yield",
"part"
] | Partition a list of integers into a list of partitions | [
"Partition",
"a",
"list",
"of",
"integers",
"into",
"a",
"list",
"of",
"partitions"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/utils/partitions.py#L93-L96 | train |
hsolbrig/PyShEx | pyshex/utils/partitions.py | partition_t | def partition_t(T: RDFGraph, nparts: int) -> Iterator[Tuple[RDFGraph, ...]]:
"""
Partition T into all possible partitions of T of size nparts
:param T: Set of RDF triples to be partitioned
:param nparts: number of partitions (e.g. 2 means return all possible 2 set partitions
:return: Iterator that returns partitions
We don't actually partition the triples directly -- instead, we partition a set of integers that
reference elements in the (ordered) set and return those
"""
def partition_map(partition: List[List[int]]) -> Tuple[RDFGraph, ...]:
rval: List[RDFGraph, ...] = []
for part in partition:
if len(part) == 1 and part[0] >= t_list_len:
rval.append(RDFGraph())
else:
rval.append(RDFGraph([t_list[e] for e in part if e < t_list_len]))
return tuple(rval)
t_list = sorted(list(T)) # Sorted not strictly necessary, but aids testing
t_list_len = len(t_list)
return map(lambda partition: partition_map(partition), filtered_integer_partition(t_list_len, nparts)) | python | def partition_t(T: RDFGraph, nparts: int) -> Iterator[Tuple[RDFGraph, ...]]:
"""
Partition T into all possible partitions of T of size nparts
:param T: Set of RDF triples to be partitioned
:param nparts: number of partitions (e.g. 2 means return all possible 2 set partitions
:return: Iterator that returns partitions
We don't actually partition the triples directly -- instead, we partition a set of integers that
reference elements in the (ordered) set and return those
"""
def partition_map(partition: List[List[int]]) -> Tuple[RDFGraph, ...]:
rval: List[RDFGraph, ...] = []
for part in partition:
if len(part) == 1 and part[0] >= t_list_len:
rval.append(RDFGraph())
else:
rval.append(RDFGraph([t_list[e] for e in part if e < t_list_len]))
return tuple(rval)
t_list = sorted(list(T)) # Sorted not strictly necessary, but aids testing
t_list_len = len(t_list)
return map(lambda partition: partition_map(partition), filtered_integer_partition(t_list_len, nparts)) | [
"def",
"partition_t",
"(",
"T",
":",
"RDFGraph",
",",
"nparts",
":",
"int",
")",
"->",
"Iterator",
"[",
"Tuple",
"[",
"RDFGraph",
",",
"...",
"]",
"]",
":",
"def",
"partition_map",
"(",
"partition",
":",
"List",
"[",
"List",
"[",
"int",
"]",
"]",
")",
"->",
"Tuple",
"[",
"RDFGraph",
",",
"...",
"]",
":",
"rval",
":",
"List",
"[",
"RDFGraph",
",",
"...",
"]",
"=",
"[",
"]",
"for",
"part",
"in",
"partition",
":",
"if",
"len",
"(",
"part",
")",
"==",
"1",
"and",
"part",
"[",
"0",
"]",
">=",
"t_list_len",
":",
"rval",
".",
"append",
"(",
"RDFGraph",
"(",
")",
")",
"else",
":",
"rval",
".",
"append",
"(",
"RDFGraph",
"(",
"[",
"t_list",
"[",
"e",
"]",
"for",
"e",
"in",
"part",
"if",
"e",
"<",
"t_list_len",
"]",
")",
")",
"return",
"tuple",
"(",
"rval",
")",
"t_list",
"=",
"sorted",
"(",
"list",
"(",
"T",
")",
")",
"# Sorted not strictly necessary, but aids testing",
"t_list_len",
"=",
"len",
"(",
"t_list",
")",
"return",
"map",
"(",
"lambda",
"partition",
":",
"partition_map",
"(",
"partition",
")",
",",
"filtered_integer_partition",
"(",
"t_list_len",
",",
"nparts",
")",
")"
] | Partition T into all possible partitions of T of size nparts
:param T: Set of RDF triples to be partitioned
:param nparts: number of partitions (e.g. 2 means return all possible 2 set partitions
:return: Iterator that returns partitions
We don't actually partition the triples directly -- instead, we partition a set of integers that
reference elements in the (ordered) set and return those | [
"Partition",
"T",
"into",
"all",
"possible",
"partitions",
"of",
"T",
"of",
"size",
"nparts",
":",
"param",
"T",
":",
"Set",
"of",
"RDF",
"triples",
"to",
"be",
"partitioned",
":",
"param",
"nparts",
":",
"number",
"of",
"partitions",
"(",
"e",
".",
"g",
".",
"2",
"means",
"return",
"all",
"possible",
"2",
"set",
"partitions",
":",
"return",
":",
"Iterator",
"that",
"returns",
"partitions"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/utils/partitions.py#L143-L164 | train |
hsolbrig/PyShEx | pyshex/utils/partitions.py | partition_2 | def partition_2(T: RDFGraph) -> List[Tuple[RDFGraph, RDFGraph]]:
"""
Partition T into all possible combinations of two subsets
:param T: RDF Graph to partition
:return:
"""
for p in partition_t(T, 2):
yield p | python | def partition_2(T: RDFGraph) -> List[Tuple[RDFGraph, RDFGraph]]:
"""
Partition T into all possible combinations of two subsets
:param T: RDF Graph to partition
:return:
"""
for p in partition_t(T, 2):
yield p | [
"def",
"partition_2",
"(",
"T",
":",
"RDFGraph",
")",
"->",
"List",
"[",
"Tuple",
"[",
"RDFGraph",
",",
"RDFGraph",
"]",
"]",
":",
"for",
"p",
"in",
"partition_t",
"(",
"T",
",",
"2",
")",
":",
"yield",
"p"
] | Partition T into all possible combinations of two subsets
:param T: RDF Graph to partition
:return: | [
"Partition",
"T",
"into",
"all",
"possible",
"combinations",
"of",
"two",
"subsets",
":",
"param",
"T",
":",
"RDF",
"Graph",
"to",
"partition",
":",
"return",
":"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/utils/partitions.py#L167-L174 | train |
hsolbrig/PyShEx | pyshex/sparql11_query/p17_1_operand_data_types.py | is_strict_numeric | def is_strict_numeric(n: Node) -> bool:
""" numeric denotes typed literals with datatypes xsd:integer, xsd:decimal, xsd:float, and xsd:double. """
return is_typed_literal(n) and cast(Literal, n).datatype in [XSD.integer, XSD.decimal, XSD.float, XSD.double] | python | def is_strict_numeric(n: Node) -> bool:
""" numeric denotes typed literals with datatypes xsd:integer, xsd:decimal, xsd:float, and xsd:double. """
return is_typed_literal(n) and cast(Literal, n).datatype in [XSD.integer, XSD.decimal, XSD.float, XSD.double] | [
"def",
"is_strict_numeric",
"(",
"n",
":",
"Node",
")",
"->",
"bool",
":",
"return",
"is_typed_literal",
"(",
"n",
")",
"and",
"cast",
"(",
"Literal",
",",
"n",
")",
".",
"datatype",
"in",
"[",
"XSD",
".",
"integer",
",",
"XSD",
".",
"decimal",
",",
"XSD",
".",
"float",
",",
"XSD",
".",
"double",
"]"
] | numeric denotes typed literals with datatypes xsd:integer, xsd:decimal, xsd:float, and xsd:double. | [
"numeric",
"denotes",
"typed",
"literals",
"with",
"datatypes",
"xsd",
":",
"integer",
"xsd",
":",
"decimal",
"xsd",
":",
"float",
"and",
"xsd",
":",
"double",
"."
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/sparql11_query/p17_1_operand_data_types.py#L15-L17 | train |
hsolbrig/PyShEx | pyshex/sparql11_query/p17_1_operand_data_types.py | is_simple_literal | def is_simple_literal(n: Node) -> bool:
""" simple literal denotes a plain literal with no language tag. """
return is_typed_literal(n) and cast(Literal, n).datatype is None and cast(Literal, n).language is None | python | def is_simple_literal(n: Node) -> bool:
""" simple literal denotes a plain literal with no language tag. """
return is_typed_literal(n) and cast(Literal, n).datatype is None and cast(Literal, n).language is None | [
"def",
"is_simple_literal",
"(",
"n",
":",
"Node",
")",
"->",
"bool",
":",
"return",
"is_typed_literal",
"(",
"n",
")",
"and",
"cast",
"(",
"Literal",
",",
"n",
")",
".",
"datatype",
"is",
"None",
"and",
"cast",
"(",
"Literal",
",",
"n",
")",
".",
"language",
"is",
"None"
] | simple literal denotes a plain literal with no language tag. | [
"simple",
"literal",
"denotes",
"a",
"plain",
"literal",
"with",
"no",
"language",
"tag",
"."
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/sparql11_query/p17_1_operand_data_types.py#L20-L22 | train |
hsolbrig/PyShEx | pyshex/shape_expressions_language/p5_7_semantic_actions.py | semActsSatisfied | def semActsSatisfied(acts: Optional[List[ShExJ.SemAct]], cntxt: Context) -> bool:
""" `5.7.1 Semantic Actions Semantics <http://shex.io/shex-semantics/#semantic-actions-semantics>`_
The evaluation semActsSatisfied on a list of SemActs returns success or failure. The evaluation of an individual
SemAct is implementation-dependent.
"""
return True | python | def semActsSatisfied(acts: Optional[List[ShExJ.SemAct]], cntxt: Context) -> bool:
""" `5.7.1 Semantic Actions Semantics <http://shex.io/shex-semantics/#semantic-actions-semantics>`_
The evaluation semActsSatisfied on a list of SemActs returns success or failure. The evaluation of an individual
SemAct is implementation-dependent.
"""
return True | [
"def",
"semActsSatisfied",
"(",
"acts",
":",
"Optional",
"[",
"List",
"[",
"ShExJ",
".",
"SemAct",
"]",
"]",
",",
"cntxt",
":",
"Context",
")",
"->",
"bool",
":",
"return",
"True"
] | `5.7.1 Semantic Actions Semantics <http://shex.io/shex-semantics/#semantic-actions-semantics>`_
The evaluation semActsSatisfied on a list of SemActs returns success or failure. The evaluation of an individual
SemAct is implementation-dependent. | [
"5",
".",
"7",
".",
"1",
"Semantic",
"Actions",
"Semantics",
"<http",
":",
"//",
"shex",
".",
"io",
"/",
"shex",
"-",
"semantics",
"/",
"#semantic",
"-",
"actions",
"-",
"semantics",
">",
"_"
] | 9d659cc36e808afd66d4a6d60e8ea21cb12eb744 | https://github.com/hsolbrig/PyShEx/blob/9d659cc36e808afd66d4a6d60e8ea21cb12eb744/pyshex/shape_expressions_language/p5_7_semantic_actions.py#L13-L19 | train |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.