_id stringlengths 2 7 | title stringlengths 1 88 | partition stringclasses 3
values | text stringlengths 31 13.1k | language stringclasses 1
value | meta_information dict |
|---|---|---|---|---|---|
q7200 | NTEnum.assign | train | def assign(self, V, py):
"""Store python value in Value
"""
if isinstance(py, (bytes, unicode)):
for i,C in enumerate(V['value.choices'] or self._choices):
if py==C:
| python | {
"resource": ""
} |
q7201 | periodic | train | def periodic(period=60.0, file=sys.stderr):
"""Start a daemon thread which will periodically print GC stats
:param period: Update period in seconds | python | {
"resource": ""
} |
q7202 | StatsDelta.collect | train | def collect(self, file=sys.stderr):
"""Collect stats and print results to file
:param file: A writable file-like object
"""
cur = gcstats()
Ncur = len(cur)
if self.stats is not None and file is not None:
prev = self.stats
Nprev = self.ntypes # may be less than len(prev)
if Ncur != Nprev:
print("# Types %d -> %d" % (Nprev, Ncur), file=file)
Scur, Sprev, first = set(cur), set(prev), True
for T in Scur - Sprev: # new types
if first:
print('New Types', file=file)
first = False
print(' ', T, cur[T], file=file)
first = True
for T in Sprev - Scur: # collected types
if first:
print('Cleaned Types', file=file)
| python | {
"resource": ""
} |
q7203 | MembersClient.filter | train | def filter(self, chamber, congress=CURRENT_CONGRESS, **kwargs):
"""
Takes a chamber and Congress,
OR state and district, returning a list of members
"""
check_chamber(chamber)
kwargs.update(chamber=chamber, congress=congress)
if 'state' in kwargs and 'district' in kwargs: | python | {
"resource": ""
} |
q7204 | MembersClient.bills | train | def bills(self, member_id, type='introduced'):
"Same as BillsClient.by_member"
path | python | {
"resource": ""
} |
q7205 | MembersClient.compare | train | def compare(self, first, second, chamber, type='votes', congress=CURRENT_CONGRESS):
"""
See how often two members voted together in a given Congress.
Takes two member IDs, a chamber and a Congress number.
"""
check_chamber(chamber)
path = "members/{first}/{type}/{second}/{congress}/{chamber}.json"
| python | {
"resource": ""
} |
q7206 | BillsClient.upcoming | train | def upcoming(self, chamber, congress=CURRENT_CONGRESS):
"Shortcut for upcoming bills"
| python | {
"resource": ""
} |
q7207 | VotesClient.by_month | train | def by_month(self, chamber, year=None, month=None):
"""
Return votes for a single month, defaulting to the current month.
"""
check_chamber(chamber)
now = datetime.datetime.now()
year = year or now.year
month = month or now.month
| python | {
"resource": ""
} |
q7208 | VotesClient.by_range | train | def by_range(self, chamber, start, end):
"""
Return votes cast in a chamber between two dates,
up to one month apart.
"""
check_chamber(chamber)
start, end = parse_date(start), parse_date(end)
if start > end:
start, end = end, start
| python | {
"resource": ""
} |
q7209 | VotesClient.by_date | train | def by_date(self, chamber, date):
"Return votes cast in a chamber on a single day"
| python | {
"resource": ""
} |
q7210 | VotesClient.today | train | def today(self, chamber):
"Return today's votes in a given chamber"
| python | {
"resource": ""
} |
q7211 | VotesClient.nominations | train | def nominations(self, congress=CURRENT_CONGRESS):
"Return votes on nominations from a given Congress"
| python | {
"resource": ""
} |
q7212 | Client.fetch | train | def fetch(self, path, parse=lambda r: r['results'][0]):
"""
Make an API request, with authentication.
This method can be used directly to fetch new endpoints
or customize parsing.
::
>>> from congress import Congress
>>> client = Congress()
>>> senate = client.fetch('115/senate/members.json')
>>> print(senate['num_results'])
101
"""
url = self.BASE_URI + path
headers = {'X-API-Key': self.apikey}
log.debug(url)
resp, content = self.http.request(url, headers=headers)
content = u(content)
content = json.loads(content)
| python | {
"resource": ""
} |
q7213 | parse_date | train | def parse_date(s):
"""
Parse a date using dateutil.parser.parse if available,
falling back to datetime.datetime.strptime if not
"""
if isinstance(s, (datetime.datetime, datetime.date)):
return s
try: | python | {
"resource": ""
} |
q7214 | GroupBy._prep_spark_sql_groupby | train | def _prep_spark_sql_groupby(self):
"""Used Spark SQL group approach"""
# Strip the index info
non_index_columns = filter(lambda x: x not in self._prdd._index_names,
self._prdd._column_names())
self._grouped_spark_sql = (self._prdd.to_spark_sql()
| python | {
"resource": ""
} |
q7215 | GroupBy._prep_pandas_groupby | train | def _prep_pandas_groupby(self):
"""Prepare the old school pandas group by based approach."""
myargs = self._myargs
mykwargs = self._mykwargs
def extract_keys(groupedFrame):
for key, group in groupedFrame:
yield (key, group)
def group_and_extract(frame):
| python | {
"resource": ""
} |
q7216 | GroupBy._group | train | def _group(self, rdd):
"""Group together the values with the same key."""
| python | {
"resource": ""
} |
q7217 | GroupBy.ngroups | train | def ngroups(self):
"""Number of groups."""
if self._can_use_new_school():
return self._grouped_spark_sql.count()
| python | {
"resource": ""
} |
q7218 | GroupBy.sum | train | def sum(self):
"""Compute the sum for each group."""
if self._can_use_new_school():
self._prep_spark_sql_groupby()
import pyspark.sql.functions as func
return self._use_aggregation(func.sum)
self._prep_pandas_groupby()
myargs = self._myargs
mykwargs = self._mykwargs
def create_combiner(x):
return x.groupby(*myargs, **mykwargs).sum()
def merge_value(x, y):
return pd.concat([x, create_combiner(y)])
def merge_combiner(x, y):
| python | {
"resource": ""
} |
q7219 | GroupBy._create_exprs_using_func | train | def _create_exprs_using_func(self, f, columns):
"""Create aggregate expressions using the provided function
with the result coming back as the original column name."""
| python | {
"resource": ""
} |
q7220 | GroupBy._use_aggregation | train | def _use_aggregation(self, agg, columns=None):
"""Compute the result using the aggregation function provided.
The aggregation name must also be provided so we can strip of the extra
name that Spark SQL adds."""
if not columns:
columns = self._columns
| python | {
"resource": ""
} |
q7221 | GroupBy._regroup_mergedRDD | train | def _regroup_mergedRDD(self):
"""A common pattern is we want to call groupby again on the dataframes
so we can use the groupby functions.
"""
myargs = self._myargs
mykwargs = self._mykwargs
self._prep_pandas_groupby() | python | {
"resource": ""
} |
q7222 | GroupBy.nth | train | def nth(self, n, *args, **kwargs):
"""Take the nth element of each grouby."""
# TODO: Stop collecting the entire frame for each key.
self._prep_pandas_groupby()
myargs = self._myargs
mykwargs = self._mykwargs
nthRDD = self._regroup_mergedRDD().mapValues(
| python | {
"resource": ""
} |
q7223 | GroupBy.apply | train | def apply(self, func, *args, **kwargs):
"""Apply the provided function and combine the results together in the
same way as apply from groupby in pandas.
This returns a DataFrame.
"""
self._prep_pandas_groupby()
def key_by_index(data):
"""Key each row by its index.
"""
# TODO: Is there a better way to do this?
for key, row in data.iterrows():
yield (key, pd.DataFrame.from_dict(
dict([(key, row)]), orient='index'))
myargs = self._myargs
mykwargs = self._mykwargs
| python | {
"resource": ""
} |
q7224 | _create_function | train | def _create_function(name, doc=""):
""" Create a function for aggregator by name"""
def _(col):
spark_ctx = SparkContext._active_spark_context
java_ctx = (getattr(spark_ctx._jvm.com.sparklingpandas.functions,
name)
| python | {
"resource": ""
} |
q7225 | PStatCounter.merge | train | def merge(self, frame):
"""
Add another DataFrame to the PStatCounter.
"""
for column, values in frame.iteritems():
# Temporary hack, fix later
counter = self._counters.get(column)
| python | {
"resource": ""
} |
q7226 | PStatCounter.merge_pstats | train | def merge_pstats(self, other):
"""
Merge all of the stats counters of the other PStatCounter with our
counters.
"""
if not isinstance(other, PStatCounter):
raise Exception("Can only merge PStatcounters!")
for column, counter in self._counters.items():
| python | {
"resource": ""
} |
q7227 | _update_index_on_df | train | def _update_index_on_df(df, index_names):
"""Helper function to restore index information after collection. Doesn't
use self so we can serialize this."""
| python | {
"resource": ""
} |
q7228 | DataFrame._rdd | train | def _rdd(self):
"""Return an RDD of Panda DataFrame objects. This can be expensive
especially if we don't do a narrow transformation after and get it back
to Spark SQL land quickly."""
columns = self._schema_rdd.columns
index_names = self._index_names
def fromRecords(records):
if not records:
return []
else:
| python | {
"resource": ""
} |
q7229 | DataFrame._column_names | train | def _column_names(self):
"""Return the column names"""
index_names = set(_normalize_index_names(self._index_names))
column_names | python | {
"resource": ""
} |
q7230 | DataFrame._evil_apply_with_dataframes | train | def _evil_apply_with_dataframes(self, func, preserves_cols=False):
"""Convert the underlying SchmeaRDD to an RDD of DataFrames.
apply the provide function and convert the result back.
This is hella slow."""
source_rdd = self._rdd()
result_rdd = func(source_rdd)
# By default we don't know what the columns & indexes are so we let
# from_rdd_of_dataframes look at the first partition to determine them.
column_idxs = None
if preserves_cols:
index_names = self._index_names
| python | {
"resource": ""
} |
q7231 | DataFrame._first_as_df | train | def _first_as_df(self):
"""Gets the first row as a Panda's DataFrame. Useful for functions like
dtypes & ftypes"""
columns = self._schema_rdd.columns
| python | {
"resource": ""
} |
q7232 | DataFrame.fromDataFrameRDD | train | def fromDataFrameRDD(cls, rdd, sql_ctx):
"""Construct a DataFrame from an RDD of DataFrames.
No checking or validation occurs."""
| python | {
"resource": ""
} |
q7233 | DataFrame.applymap | train | def applymap(self, f, **kwargs):
"""Return a new DataFrame by applying a function to each element of each
Panda DataFrame."""
def transform_rdd(rdd):
return rdd.map(lambda data: data.applymap(f), | python | {
"resource": ""
} |
q7234 | DataFrame.groupby | train | def groupby(self, by=None, axis=0, level=None, as_index=True, sort=True,
group_keys=True, squeeze=False):
"""Returns a groupby on the schema rdd. This returns a GroupBy object.
Note that grouping by a column name will be faster than most other
options due to implementation."""
from sparklingpandas.groupby import | python | {
"resource": ""
} |
q7235 | DataFrame.collect | train | def collect(self):
"""Collect the elements in an DataFrame
and concatenate the partition."""
local_df = self._schema_rdd.toPandas()
| python | {
"resource": ""
} |
q7236 | PRDD.applymap | train | def applymap(self, func, **kwargs):
"""Return a new PRDD by applying a function to each element of each
pandas DataFrame."""
| python | {
"resource": ""
} |
q7237 | PRDD.collect | train | def collect(self):
"""Collect the elements in an PRDD and concatenate the partition."""
# The order of the frame order appends is based on the implementation
# of reduce which calls our function with
# f(valueToBeAdded, accumulator) so we do our reduce implementation.
| python | {
"resource": ""
} |
q7238 | PRDD._custom_rdd_reduce | train | def _custom_rdd_reduce(self, reduce_func):
"""Provides a custom RDD reduce which preserves ordering if the RDD has
been sorted. This is useful for us because we need this functionality
as many pandas operations support sorting the results. The standard
reduce in PySpark does not have this property. Note that when PySpark
no longer does partition reduces locally this code will also need to
be updated."""
def accumulating_iter(iterator):
acc = None
for obj in iterator: | python | {
"resource": ""
} |
q7239 | PSparkContext.read_csv | train | def read_csv(self, file_path, use_whole_file=False, names=None, skiprows=0,
*args, **kwargs):
"""Read a CSV file in and parse it into Pandas DataFrames. By default,
the first row from the first partition of that data is parsed and used
as the column names for the data from. If no 'names' param is
provided we parse the first row of the first partition of data and
use it for column names.
Parameters
----------
file_path: string
Path to input. Any valid file path in Spark works here, eg:
'file:///my/path/in/local/file/system' or 'hdfs:/user/juliet/'
use_whole_file: boolean
Whether of not to use the whole file.
names: list of strings, optional
skiprows: integer, optional
indicates how many rows of input to skip. This will
only be applied to the first partition of the data (so if
#skiprows > #row in first partition this will not work). Generally
this shouldn't be an issue for small values of skiprows.
No other value of header is supported.
All additional parameters available in pandas.read_csv() are usable
here.
Returns
-------
A SparklingPandas DataFrame that contains the data from the
specified file.
"""
def csv_file(partition_number, files):
# pylint: disable=unexpected-keyword-arg
file_count = 0
for _, contents in files:
# Only skip lines on the first file
if partition_number == 0 and file_count == 0 and _skiprows > 0:
yield pandas.read_csv(
sio(contents), *args,
header=None,
names=mynames,
skiprows=_skiprows,
**kwargs)
else:
file_count += 1
yield pandas.read_csv(
sio(contents), *args,
header=None,
names=mynames,
**kwargs)
def csv_rows(partition_number, rows):
# pylint: disable=unexpected-keyword-arg
in_str = "\n".join(rows)
if partition_number == 0:
return iter([
pandas.read_csv(
sio(in_str), *args, header=None,
names=mynames,
skiprows=_skiprows,
**kwargs)])
| python | {
"resource": ""
} |
q7240 | PSparkContext.read_json | train | def read_json(self, file_path,
*args, **kwargs):
"""Read a json file in and parse it into Pandas DataFrames.
If no names is provided we use the first row for the names.
Currently, it is not possible to skip the first n rows of a file.
Headers are provided in the json file and not specified separately.
Parameters
----------
file_path: string
Path to input. Any valid file path in Spark works here, eg:
'my/path/in/local/file/system' or 'hdfs:/user/juliet/'
Other than skipRows, all additional parameters available in
pandas.read_csv() are usable here.
Returns
-------
| python | {
"resource": ""
} |
q7241 | IIIFManipulatorGen.do_first | train | def do_first(self):
"""Load generator, set size.
We take the generator module name from self.srcfile so that
this manipulator will work with different generators in a
similar way to how the ordinary generators work with
different images
"""
# Load generator module and create instance if we haven't already
if (not self.srcfile):
raise IIIFError(text=("No generator specified"))
if (not self.gen):
try:
(name, ext) = os.path.splitext(self.srcfile)
(pack, mod) = os.path.split(name)
module_name = 'iiif.generators.' + mod
try:
module = sys.modules[module_name]
except KeyError:
self.logger.debug(
"Loading generator | python | {
"resource": ""
} |
q7242 | IIIFManipulatorGen.do_region | train | def do_region(self, x, y, w, h):
"""Record region."""
if (x is None):
self.rx = 0
self.ry = 0
self.rw = self.width
self.rh = self.height
| python | {
"resource": ""
} |
q7243 | IIIFManipulatorGen.do_size | train | def do_size(self, w, h):
"""Record size."""
if (w is None):
self.sw = self.rw
self.sh = self.rh
else:
self.sw = w
self.sh = h
# Now we have region and size, generate the image
image = Image.new("RGB", (self.sw, self.sh), self.gen.background_color)
for y in range(0, self.sh):
for x in range(0, self.sw):
ix = int((x * self.rw) // self.sw + self.rx)
| python | {
"resource": ""
} |
q7244 | IIIFAuthBasic.login_handler | train | def login_handler(self, config=None, prefix=None, **args):
"""HTTP Basic login handler.
Respond with 401 and WWW-Authenticate header if there are no
credentials or bad credentials. If there are credentials then
simply check for username equal to password for validity.
"""
headers = {}
headers['Access-control-allow-origin'] = '*'
headers['Content-type'] = 'text/html'
auth = request.authorization
| python | {
"resource": ""
} |
q7245 | IIIFAuthClickthrough.login_service_description | train | def login_service_description(self):
"""Clickthrough login service description.
The login service description _MUST_ include the token service
description. Additionally, for a | python | {
"resource": ""
} |
q7246 | IIIFRequest.clear | train | def clear(self):
"""Clear all data that might pertain to an individual IIIF URL.
Does not change/reset the baseurl or API version which might be
useful in a sequence of calls.
"""
# API parameters
self.identifier = None
self.region = None
self.size = None
self.rotation = None
self.quality = None
self.format = None
self.info = None
# Derived data and flags
self.region_full = False
self.region_square = False
| python | {
"resource": ""
} |
q7247 | IIIFRequest.api_version | train | def api_version(self, v):
"""Set the api_version and associated configurations."""
self._api_version = v
if (self._api_version >= '2.0'):
self.default_quality = 'default' | python | {
"resource": ""
} |
q7248 | IIIFRequest.url | train | def url(self, **params):
"""Build a URL path for image or info request.
An IIIF Image request with parameterized form is assumed unless
the info parameter is specified, in which case an Image Information
request URI is constructred.
"""
self._setattrs(**params)
path = self.baseurl + self.quote(self.identifier) + "/"
if (self.info):
# info request
path += "info"
format = self.format if self.format else "json"
else:
# region
if self.region:
region = self.region
elif self.region_xywh:
region = "%d,%d,%d,%d" % tuple(self.region_xywh)
else:
| python | {
"resource": ""
} |
q7249 | IIIFRequest.parse_url | train | def parse_url(self, url):
"""Parse an IIIF API URL path and each component.
Will parse a URL or URL path that accords with either the
parametrized or info request forms. Will raise an
IIIFRequestError on failure. A wrapper for the split_url()
and parse_parameters() methods.
Note that behavior of split_url() | python | {
"resource": ""
} |
q7250 | IIIFRequest.split_url | train | def split_url(self, url):
"""Parse an IIIF API URL path into components.
Will parse a URL or URL path that accords with either the
parametrized or info API forms. Will raise an IIIFRequestError on
failure.
If self.identifier is set then url is assumed not to include the
identifier.
"""
# clear data first
identifier = self.identifier
self.clear()
# url must start with baseurl if set (including slash)
if (self.baseurl is not None):
(path, num) = re.subn('^' + self.baseurl, '', url, 1)
if (num != 1):
raise IIIFRequestError(
text="Request URL does not start with base URL")
url = path
# Break up by path segments, count to decide format
segs = url.split('/')
if (identifier is not None):
segs.insert(0, identifier)
elif (self.allow_slashes_in_identifier):
segs = self._allow_slashes_in_identifier_munger(segs)
# Now have segments with identifier as first
if (len(segs) > 5):
raise IIIFRequestPathError(
text="Request URL (%s) has too many path segments" % url)
elif (len(segs) == 5):
self.identifier = urlunquote(segs[0])
self.region = urlunquote(segs[1])
self.size = urlunquote(segs[2])
self.rotation = urlunquote(segs[3])
| python | {
"resource": ""
} |
q7251 | IIIFRequest.strip_format | train | def strip_format(self, str_and_format):
"""Look for optional .fmt at end of URI.
The format must start with letter. Note that we want to catch
the case of a dot and no format (format='') which is different
from no dot (format=None)
| python | {
"resource": ""
} |
q7252 | IIIFRequest.parse_parameters | train | def parse_parameters(self):
"""Parse the parameters of an Image Information request.
Will throw an IIIFRequestError on failure, set attributes on
success. Care is taken not to change any of the artibutes
which store path components. All parsed values are stored
in new attributes.
| python | {
"resource": ""
} |
q7253 | IIIFRequest.parse_region | train | def parse_region(self):
"""Parse the region component of the path.
/full/ -> self.region_full = True (test this first)
/square/ -> self.region_square = True (test this second)
/x,y,w,h/ -> self.region_xywh = (x,y,w,h)
/pct:x,y,w,h/ -> self.region_xywh and self.region_pct = True
Will throw errors if the parameters are illegal according to the
specification but does not know about and thus cannot do any tests
against any image being manipulated.
"""
self.region_full = False
self.region_square = False
self.region_pct = False
if (self.region is None or self.region == 'full'):
self.region_full = True
return
if (self.api_version >= '2.1' and self.region == 'square'):
self.region_square = True
return
xywh = self.region
pct_match = re.match('pct:(.*)$', self.region)
if (pct_match):
xywh = pct_match.group(1)
self.region_pct = True
# Now whether this was pct: or now, we expect 4 values...
str_values = xywh.split(',', 5)
if (len(str_values) != 4):
raise IIIFRequestError(
code=400, parameter="region",
text="Bad number of values in region specification, "
"must be x,y,w,h but got %d value(s) from '%s'" %
(len(str_values), xywh))
values = []
for str_value in str_values:
# Must be either integer (not pct) or interger/float (pct)
if (pct_match):
try:
# This is rather more permissive that the iiif spec
value = float(str_value)
except ValueError:
raise IIIFRequestError(
parameter="region",
| python | {
"resource": ""
} |
q7254 | IIIFRequest.parse_size | train | def parse_size(self, size=None):
"""Parse the size component of the path.
/full/ -> self.size_full = True
/max/ -> self.size_mac = True (2.1 and up)
/w,/ -> self.size_wh = (w,None)
/,h/ -> self.size_wh = (None,h)
/w,h/ -> self.size_wh = (w,h)
/pct:p/ -> self.size_pct = p
/!w,h/ -> self.size_wh = (w,h), self.size_bang = True
Expected use:
(w,h) = iiif.size_to_apply(region_w,region_h)
if (q is None):
# full image
else:
# scale to w by h
Returns (None,None) if no scaling is required.
"""
if (size is not None):
self.size = size
self.size_pct = None
self.size_bang = False
self.size_full = False
self.size_wh = (None, None)
if (self.size is None or self.size == 'full'):
self.size_full = True
return
elif (self.size == 'max' and self.api_version >= '2.1'):
self.size_max = True
return
pct_match = re.match('pct:(.*)$', self.size)
if (pct_match is not None):
pct_str = pct_match.group(1)
try:
self.size_pct = float(pct_str)
except ValueError:
raise IIIFRequestError(
code=400, parameter="size",
text="Percentage size value must be a number, got "
"'%s'." % (pct_str))
# Note that Image API specificaton places no upper limit on
# size so none is implemented here.
if (self.size_pct < 0.0):
raise IIIFRequestError(
code=400, parameter="size",
text="Base size percentage, must be > 0.0, got %f." %
| python | {
"resource": ""
} |
q7255 | IIIFRequest._parse_w_comma_h | train | def _parse_w_comma_h(self, whstr, param):
"""Utility to parse "w,h" "w," or ",h" values.
Returns (w,h) where w,h are either None or ineteger. Will
throw a ValueError if there is a problem with one or both.
"""
try:
(wstr, hstr) = whstr.split(',', 2)
w = self._parse_non_negative_int(wstr, 'w')
h = self._parse_non_negative_int(hstr, 'h')
except ValueError as e:
raise IIIFRequestError(
code=400, parameter=param,
| python | {
"resource": ""
} |
q7256 | IIIFRequest.parse_quality | train | def parse_quality(self):
"""Check quality paramater.
Sets self.quality_val based on simple substitution of
'native' for default. Checks for the three valid values
else throws an IIIFRequestError.
"""
if (self.quality is None):
self.quality_val = self.default_quality
elif (self.quality not in self.allowed_qualities):
| python | {
"resource": ""
} |
q7257 | IIIFRequest.parse_format | train | def parse_format(self):
"""Check format parameter.
All formats values listed in the specification are lowercase
alphanumeric value commonly used as file extensions. To leave
opportunity for extension here just do a limited sanity check
on characters and length.
"""
| python | {
"resource": ""
} |
q7258 | IIIFRequest.is_scaled_full_image | train | def is_scaled_full_image(self):
"""True if this request is for a scaled full image.
To be used to determine whether this request should be used
in the set of `sizes` specificed in the Image Information.
| python | {
"resource": ""
} |
q7259 | create_reference_server_flask_app | train | def create_reference_server_flask_app(cfg):
"""Create referece server Flask application with one or more IIIF handlers."""
# Create Flask app
app = Flask(__name__)
Flask.secret_key = "SECRET_HERE"
app.debug = cfg.debug
# Install request handlers
client_prefixes = dict()
for api_version in cfg.api_versions:
handler_config = Config(cfg)
handler_config.api_version = api_version
handler_config.klass_name = 'pil'
| python | {
"resource": ""
} |
q7260 | IIIFInfo.id | train | def id(self):
"""id property based on server_and_prefix and identifier."""
id = ''
if (self.server_and_prefix is not None and
self.server_and_prefix != ''):
id += | python | {
"resource": ""
} |
q7261 | IIIFInfo.id | train | def id(self, value):
"""Split into server_and_prefix and identifier."""
i = value.rfind('/')
if (i > 0):
self.server_and_prefix = value[:i]
self.identifier = value[(i + 1):]
elif (i == 0):
| python | {
"resource": ""
} |
q7262 | IIIFInfo.set_version_info | train | def set_version_info(self, api_version=None):
"""Set up normal values for given api_version.
Will use current value of self.api_version if a version number
is not specified in the call. Will raise an IIIFInfoError
"""
if (api_version is None):
api_version = self.api_version
if (api_version not in CONF):
raise IIIFInfoError("Unknown API version %s" % (api_version))
self.params = | python | {
"resource": ""
} |
q7263 | IIIFInfo.compliance | train | def compliance(self, value):
"""Set the compliance profile URI."""
if (self.api_version < '2.0'):
self.profile = value
else:
try:
| python | {
"resource": ""
} |
q7264 | IIIFInfo.level | train | def level(self):
"""Extract level number from compliance profile URI.
Returns integer level number or raises IIIFInfoError
"""
m = re.match(
self.compliance_prefix +
| python | {
"resource": ""
} |
q7265 | IIIFInfo.level | train | def level(self, value):
"""Build profile URI from level.
Level should be an integer 0,1,2
"""
| python | {
"resource": ""
} |
q7266 | IIIFInfo.add_service | train | def add_service(self, service):
"""Add a service description.
Handles transition from self.service=None, self.service=dict for a
single service, and then self.service=[dict,dict,...] for multiple
"""
if (self.service is None):
self.service | python | {
"resource": ""
} |
q7267 | IIIFInfo.validate | train | def validate(self):
"""Validate this object as Image API data.
Raise IIIFInfoError with helpful message if not valid.
"""
errors = []
for param in self.required_params:
if (not hasattr(self, param) or getattr(self, param) is None):
| python | {
"resource": ""
} |
q7268 | IIIFInfo.as_json | train | def as_json(self, validate=True):
"""Return JSON serialization.
Will raise IIIFInfoError if insufficient parameters are present to
have a valid info.json response (unless validate is False).
"""
if (validate):
self.validate()
json_dict = {}
if (self.api_version > '1.0'):
json_dict['@context'] = self.context
params_to_write = set(self.params)
params_to_write.discard('identifier')
if (self.identifier):
if (self.api_version == '1.0'):
json_dict['identifier'] = self.identifier # local id
else:
json_dict['@id'] = self.id # URI
params_to_write.discard('profile')
if (self.compliance):
if (self.api_version < '2.0'):
json_dict['profile'] = self.compliance
else:
# FIXME - need to support extra profile features
json_dict['profile'] = [self.compliance]
d = {}
if (self.formats is not None):
| python | {
"resource": ""
} |
q7269 | IIIFInfo.read | train | def read(self, fh, api_version=None):
"""Read info.json from file like object.
Parameters:
fh -- file like object supporting fh.read()
api_version -- IIIF Image API version expected
If api_version is set then the parsing will assume this API version,
else the version will be determined from the incoming data. NOTE that
the value of self.api_version is NOT used in this routine.
If an api_version is specified and there is a @context specified then
an IIIFInfoError will be raised unless these match. If no known
@context is present and no api_version set then an IIIFInfoError
will be raised.
"""
j = json.load(fh)
#
# @context and API version
self.context = None
if (api_version == '1.0'):
# v1.0 did not have a @context so we simply take the version
# passed in
self.api_version = api_version
elif ('@context' in j):
# determine API version from context
self.context = j['@context']
api_version_read = None
for v in CONF:
if (v > '1.0' and self.context == CONF[v]['context']):
api_version_read = v
break
if (api_version_read is None):
raise IIIFInfoError(
"Unknown @context, cannot determine API version (%s)" %
(self.context))
else:
if (api_version is not None and
api_version != api_version_read):
raise IIIFInfoError(
"Expected API version '%s' but got @context for API version '%s'" %
(api_version, api_version_read))
else:
| python | {
"resource": ""
} |
q7270 | IIIFManipulatorPIL.set_max_image_pixels | train | def set_max_image_pixels(self, pixels):
"""Set PIL limit on pixel size of images to load if non-zero.
WARNING: This is a global setting in PIL, it is
not local to this manipulator instance!
Setting a value here will not only set the given limit but
also convert the PIL "DecompressionBombWarning" into an
error. Thus setting a moderate limit sets a hard limit on
| python | {
"resource": ""
} |
q7271 | IIIFManipulatorPIL.do_first | train | def do_first(self):
"""Create PIL object from input image file.
Image location must be in self.srcfile. Will result in
self.width and self.height being set to the image dimensions.
Will raise an IIIFError on failure to load the image
"""
self.logger.debug("do_first: src=%s" % (self.srcfile))
try:
self.image = Image.open(self.srcfile)
except Image.DecompressionBombWarning as e:
# This exception will be raised only if PIL has been
# configured to raise an error in the case of images
# that exceeed | python | {
"resource": ""
} |
q7272 | html_page | train | def html_page(title="Page Title", body=""):
"""Create HTML page as string."""
html = "<html>\n<head><title>%s</title></head>\n<body>\n" | python | {
"resource": ""
} |
q7273 | top_level_index_page | train | def top_level_index_page(config):
"""HTML top-level index page which provides a link to each handler."""
title = "IIIF Test Server on %s" % (config.host)
body = "<ul>\n"
for prefix in sorted(config.prefixes.keys()):
| python | {
"resource": ""
} |
q7274 | identifiers | train | def identifiers(config):
"""Show list of identifiers for this prefix.
Handles both the case of local file based identifiers and
also image generators.
Arguments:
config - configuration object in which:
config.klass_name - 'gen' if a generator function
config.generator_dir - directory for generator code
config.image_dir - directory for images
Returns:
ids - a list of ids
"""
ids = []
if (config.klass_name == 'gen'):
for generator in os.listdir(config.generator_dir):
if (generator == '__init__.py'):
continue
(gid, | python | {
"resource": ""
} |
q7275 | prefix_index_page | train | def prefix_index_page(config):
"""HTML index page for a specific prefix.
The prefix seen by the client is obtained from config.client_prefix
as opposed to the local server prefix in config.prefix. Also uses
the identifiers(config) function to get identifiers available.
Arguments:
config - configuration object in which:
config.client_prefix - URI path prefix seen by client
config.host - URI host seen by client
config.api_version - string for api_version
config.manipulator - string manipulator type
config.auth_type - string for auth type
config.include_osd - whether OSD is included
"""
title = "IIIF Image API services under %s" % (config.client_prefix)
# details of this prefix handler
body = '<p>\n'
body += 'host = %s<br/>\n' % (config.host)
body += 'api_version = %s<br/>\n' % (config.api_version)
body += 'manipulator = %s<br/>\n' % (config.klass_name)
body += 'auth_type = %s\n</p>\n' % (config.auth_type)
# table of identifiers and example requests
ids = identifiers(config)
api_version = config.api_version
default = 'native' if api_version < '2.0' else 'default'
body += '<table border="1">\n<tr><th align="left">Image identifier</th>'
body += '<th> </th><th>full</th>'
if (config.klass_name != 'dummy'):
body += '<th>256,256</th>'
body += '<th>30deg</th>'
if (config.include_osd):
body += '<th> </th>'
body += "</tr>\n"
for identifier in sorted(ids):
| python | {
"resource": ""
} |
q7276 | osd_page_handler | train | def osd_page_handler(config=None, identifier=None, prefix=None, **args):
"""Flask handler to produce HTML response for OpenSeadragon view of identifier.
Arguments:
config - Config object for this IIIF handler
identifier - identifier of image/generator
prefix - path prefix
**args - other aguments ignored
"""
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
| python | {
"resource": ""
} |
q7277 | iiif_info_handler | train | def iiif_info_handler(prefix=None, identifier=None,
config=None, klass=None, auth=None, **args):
"""Handler for IIIF Image Information requests."""
if (not auth or degraded_request(identifier) or auth.info_authz()):
# go ahead with request as made
if (auth):
logging.debug("Authorized for image %s" % identifier)
i = IIIFHandler(prefix, identifier, config, klass, auth)
try:
return i.image_information_response()
except IIIFError as e:
return i.error_response(e)
elif (auth.info_authn()):
| python | {
"resource": ""
} |
q7278 | iiif_image_handler | train | def iiif_image_handler(prefix=None, identifier=None,
path=None, config=None, klass=None, auth=None, **args):
"""Handler for IIIF Image Requests.
Behaviour for case of a non-authn or non-authz case is to
return 403.
"""
if (not auth or degraded_request(identifier) or auth.image_authz()):
# serve image
if (auth):
logging.debug("Authorized for image %s" % identifier)
i = IIIFHandler(prefix, identifier, config, klass, auth)
try:
return i.image_request_response(path)
except IIIFError as e:
return i.error_response(e)
else:
| python | {
"resource": ""
} |
q7279 | parse_accept_header | train | def parse_accept_header(accept):
"""Parse an HTTP Accept header.
Parses *accept*, returning a list with pairs of
(media_type, q_value), ordered by q values.
Adapted from <https://djangosnippets.org/snippets/1042/>
"""
result = []
for media_range in accept.split(","):
parts = media_range.split(";")
media_type = parts.pop(0).strip()
media_params = []
q = 1.0
for part in parts:
(key, value) = part.lstrip().split("=", 1)
| python | {
"resource": ""
} |
q7280 | parse_authorization_header | train | def parse_authorization_header(value):
"""Parse the Authenticate header.
Returns nothing on failure, opts hash on success with type='basic' or 'digest'
and other params.
<http://nullege.com/codes/search/werkzeug.http.parse_authorization_header>
<http://stackoverflow.com/questions/1349367/parse-an-http-request-authorization-header-with-python>
<http://bugs.python.org/file34041/0001-Add-an-authorization-header-to-the-initial-request.patch>
"""
try:
(auth_type, auth_info) = value.split(' ', 1)
auth_type = auth_type.lower()
except ValueError:
return
if (auth_type == 'basic'):
try:
decoded = base64.b64decode(auth_info).decode(
'utf-8') # b64decode gives bytes in python3
(username, password) = decoded.split(':', 1)
except (ValueError, TypeError): # py3, py2
return
return {'type': 'basic', 'username': username, 'password': password}
elif (auth_type == 'digest'):
try:
| python | {
"resource": ""
} |
q7281 | do_conneg | train | def do_conneg(accept, supported):
"""Parse accept header and look for preferred type in supported list.
Arguments:
accept - HTTP Accept header
supported - list of MIME type supported by the server
Returns:
supported MIME type with highest q value in request, else None.
FIXME - Should replace | python | {
"resource": ""
} |
q7282 | setup_auth_paths | train | def setup_auth_paths(app, auth, prefix, params):
"""Add URL rules for auth paths."""
base = urljoin('/', prefix + '/') # Must end in slash
app.add_url_rule(base + 'login', prefix + 'login_handler',
auth.login_handler, defaults=params)
app.add_url_rule(base + 'logout', prefix + 'logout_handler',
| python | {
"resource": ""
} |
q7283 | make_prefix | train | def make_prefix(api_version, manipulator, auth_type):
"""Make prefix string based on configuration parameters."""
| python | {
"resource": ""
} |
q7284 | split_comma_argument | train | def split_comma_argument(comma_sep_str):
"""Split a comma separated option into a list."""
| python | {
"resource": ""
} |
q7285 | write_pid_file | train | def write_pid_file():
"""Write a file with the PID of this server instance.
Call when setting up a command line testserver.
"""
pidfile = os.path.basename(sys.argv[0])[:-3] + '.pid' # strip .py, | python | {
"resource": ""
} |
q7286 | setup_app | train | def setup_app(app, cfg):
"""Setup Flask app and handle reverse proxy setup if configured.
Arguments:
app - Flask application
cfg - configuration data
"""
# Set up app_host and app_port in case that we are running
# under reverse proxy setup, otherwise they default to
# config.host and config.port.
if (cfg.app_host and cfg.app_port):
logging.warning("Reverse proxy for service at http://%s:%d/ ..." % (cfg.host, cfg.port))
app.wsgi_app = ReverseProxied(app.wsgi_app, cfg.host)
| python | {
"resource": ""
} |
q7287 | IIIFHandler.server_and_prefix | train | def server_and_prefix(self):
"""Server and prefix from config."""
| python | {
"resource": ""
} |
q7288 | IIIFHandler.json_mime_type | train | def json_mime_type(self):
"""Return the MIME type for a JSON response.
For version 2.0+ the server must return json-ld MIME type if that
format is requested. Implement for 1.1 also.
http://iiif.io/api/image/2.1/#information-request
| python | {
"resource": ""
} |
q7289 | IIIFHandler.file | train | def file(self):
"""Filename property for the source image for the current identifier."""
file = None
if (self.config.klass_name == 'gen'):
for ext in ['.py']:
file = os.path.join(
self.config.generator_dir, self.identifier + ext)
if (os.path.isfile(file)):
return file
| python | {
"resource": ""
} |
q7290 | IIIFHandler.add_compliance_header | train | def add_compliance_header(self):
"""Add IIIF Compliance level header to response."""
if (self.manipulator.compliance_uri is | python | {
"resource": ""
} |
q7291 | IIIFHandler.make_response | train | def make_response(self, content, code=200, headers=None):
"""Wrapper around Flask.make_response which also adds any local headers."""
if headers:
for header in headers:
| python | {
"resource": ""
} |
q7292 | IIIFHandler.image_information_response | train | def image_information_response(self):
"""Parse image information request and create response."""
dr = degraded_request(self.identifier)
if (dr):
self.logger.info("image_information: degraded %s -> %s" %
(self.identifier, dr))
self.degraded = self.identifier
self.identifier = dr
else:
self.logger.info("image_information: %s" % (self.identifier))
# get size
self.manipulator.srcfile = self.file
self.manipulator.do_first()
# most of info.json comes from config, a few things specific to image
info = {'tile_height': self.config.tile_height,
'tile_width': self.config.tile_width,
'scale_factors': self.config.scale_factors
}
# calculate scale factors if not hard-coded
if ('auto' in self.config.scale_factors):
info['scale_factors'] = self.manipulator.scale_factors(
self.config.tile_width, self.config.tile_height)
i = IIIFInfo(conf=info, api_version=self.api_version)
i.server_and_prefix = self.server_and_prefix
i.identifier = self.iiif.identifier
i.width = self.manipulator.width
| python | {
"resource": ""
} |
q7293 | IIIFHandler.image_request_response | train | def image_request_response(self, path):
"""Parse image request and create response."""
# Parse the request in path
if (len(path) > 1024):
raise IIIFError(code=414,
text="URI Too Long: Max 1024 chars, got %d\n" % len(path))
try:
self.iiif.identifier = self.identifier
self.iiif.parse_url(path)
except IIIFRequestPathError as e:
# Reraise as IIIFError with code=404 because we can't tell
# whether there was an encoded slash in the identifier or
# whether there was a bad number of path segments.
raise IIIFError(code=404, text=e.text)
except IIIFError as e:
# Pass through
raise e
except Exception as e:
# Something completely unexpected => 500
raise IIIFError(code=500,
text="Internal Server Error: unexpected exception parsing request (" + str(e) + ")")
dr = degraded_request(self.identifier)
if (dr):
| python | {
"resource": ""
} |
q7294 | IIIFHandler.error_response | train | def error_response(self, e):
"""Make response for an IIIFError e.
Also add compliance header.
"""
| python | {
"resource": ""
} |
q7295 | IIIFRequestHandler.error_response | train | def error_response(self, code, content=''):
"""Construct and send error response."""
self.send_response(code)
self.send_header('Content-Type', 'text/xml')
| python | {
"resource": ""
} |
q7296 | IIIFRequestHandler.do_GET | train | def do_GET(self):
"""Implement the HTTP GET method.
The bulk of this code is wrapped in a big try block and anywhere
within the code may raise an IIIFError which then results in an
IIIF error response (section 5 of spec).
"""
self.compliance_uri = None
self.iiif = IIIFRequest(baseurl='/')
try:
(of, mime_type) = self.do_GET_body()
if (not of):
raise IIIFError("Unexpected failure to open result image")
self.send_response(200, 'OK')
if (mime_type is not None):
self.send_header('Content-Type', mime_type)
self.add_compliance_header()
self.end_headers()
while (1):
buffer = of.read(8192)
if (not buffer):
break
self.wfile.write(buffer)
# Now cleanup
| python | {
"resource": ""
} |
q7297 | IIIFRequestHandler.do_GET_body | train | def do_GET_body(self):
"""Create body of GET."""
iiif = self.iiif
if (len(self.path) > 1024):
raise IIIFError(code=414,
text="URI Too Long: Max 1024 chars, got %d\n" % len(self.path))
try:
# self.path has leading / then identifier/params...
self.path = self.path.lstrip('/')
sys.stderr.write("path = %s" % (self.path))
iiif.parse_url(self.path)
except Exception as e:
# Something completely unexpected => 500
raise IIIFError(code=500,
text="Internal Server Error: unexpected exception parsing request (" + str(e) + ")")
# Now we have a full iiif request
if (re.match('[\w\.\-]+$', iiif.identifier)):
file = os.path.join(TESTIMAGE_DIR, iiif.identifier)
if (not os.path.isfile(file)):
images_available = ""
for image_file in os.listdir(TESTIMAGE_DIR):
if (os.path.isfile(os.path.join(TESTIMAGE_DIR, image_file))):
images_available += " " + image_file + "\n"
raise IIIFError(code=404, parameter="identifier",
text="Image resource '" + iiif.identifier + "' not found. Local image files available:\n" + images_available)
else:
raise IIIFError(code=404, parameter="identifier", | python | {
"resource": ""
} |
q7298 | IIIFAuth.set_cookie_prefix | train | def set_cookie_prefix(self, cookie_prefix=None):
"""Set a random cookie prefix unless one is specified.
In order to run multiple demonstration auth services on the
same server we need to have different cookie names for each
auth domain. Unless cookie_prefix is set, generate a random
one.
| python | {
"resource": ""
} |
q7299 | IIIFAuth.add_services | train | def add_services(self, info):
"""Add auth service descriptions to an IIIFInfo object.
Login service description is the wrapper for all other
auth service descriptions so we have nothing unless
self.login_uri is specified. If we do then add any other
auth services at children.
Exactly the same structure is used in the authorized
and unauthorized cases (although in the data could be
different).
"""
if (self.login_uri):
svc = self.login_service_description()
svcs = []
if (self.logout_uri):
| python | {
"resource": ""
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.