_id stringlengths 2 7 | title stringlengths 1 88 | partition stringclasses 3
values | text stringlengths 31 13.1k | language stringclasses 1
value | meta_information dict |
|---|---|---|---|---|---|
q11300 | wget | train | def wget(url):
"""
Download the page into a string
"""
import | python | {
"resource": ""
} |
q11301 | autodecode | train | def autodecode(b):
""" Try to decode ``bytes`` to text - try default encoding first, otherwise try to autodetect
Args:
b (bytes): byte string
Returns:
str: decoded text string
"""
import warnings
import chardet
try:
return b.decode()
except UnicodeError:
| python | {
"resource": ""
} |
q11302 | remove_directories | train | def remove_directories(list_of_paths):
"""
Removes non-leafs from a list of directory paths
"""
found_dirs = set('/')
for path in list_of_paths:
dirs = path.strip().split('/')
for i in range(2,len(dirs)):
found_dirs.add( '/'.join(dirs[:i]) )
paths | python | {
"resource": ""
} |
q11303 | Subprocess._check_file_is_under_workingdir | train | def _check_file_is_under_workingdir(filename, wdir):
""" Raise error if input is being staged to a location not underneath the working dir
"""
p = filename
if not os.path.isabs(p):
p = os.path.join(wdir, p)
targetpath = os.path.realpath(p)
wdir = os.path.realpath(wdir)
common = os.path.commonprefix([wdir, targetpath])
| python | {
"resource": ""
} |
q11304 | FileReferenceBase._get_access_type | train | def _get_access_type(self, mode):
""" Make sure mode is appropriate; return 'b' for binary access and 't' for text
"""
access_type = None
for char in mode: # figure out whether it's binary or text access
if char in 'bt':
if access_type is not None:
raise IOError('File mode "%s" contains contradictory flags' % mode)
access_type = char
elif char not in 'rbt':
| python | {
"resource": ""
} |
q11305 | getclosurevars | train | def getclosurevars(func):
"""
Get the mapping of free variables to their current values.
Returns a named tuple of dicts mapping the current nonlocal, global
and builtin references as seen by the body of the function. A final
set of unbound names that could not be resolved is also provided.
Note:
Modified function from the Python 3.5 inspect standard library module
Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
2011, 2012, 2013, 2014, 2015, 2016, 2017 Python Software Foundation; All Rights
Reserved"
See also py-cloud-compute-cannon/NOTICES.
"""
if inspect.ismethod(func):
func = func.__func__
elif not inspect.isroutine(func):
raise TypeError("'{!r}' is not a Python function".format(func))
# AMVMOD: deal with python 2 builtins that don't define these
code = getattr(func, '__code__', None)
closure = getattr(func, '__closure__', None)
co_names = getattr(code, 'co_names', ())
glb = getattr(func, '__globals__', {})
# Nonlocal references are named in co_freevars and resolved
# by looking them up in __closure__ by positional index
if closure is None:
nonlocal_vars = {}
else:
nonlocal_vars = {var: cell.cell_contents
for var, cell in zip(code.co_freevars, func.__closure__)}
# Global and builtin references are named in co_names and resolved
# by looking them up in __globals__ or __builtins__
| python | {
"resource": ""
} |
q11306 | EngineBase.dump_all_outputs | train | def dump_all_outputs(self, job, target, abspaths=None):
""" Default dumping strategy - potentially slow for large numbers of files
Subclasses should offer faster implementations, if available
"""
from pathlib import Path
root = Path(native_str(target))
for outputpath, outputfile in job.get_output().items():
path = Path(native_str(outputpath))
| python | {
"resource": ""
} |
q11307 | EngineBase.launch | train | def launch(self, image, command, **kwargs):
"""
Create a job on this engine
Args:
image (str): name of the docker image to launch
command (str): shell command to run
"""
if | python | {
"resource": ""
} |
q11308 | char_width | train | def char_width(char):
"""
Get the display length of a unicode character.
"""
if ord(char) < 128:
return 1
elif unicodedata.east_asian_width(char) in ('F', 'W'):
| python | {
"resource": ""
} |
q11309 | display_len | train | def display_len(text):
"""
Get the display length of a string. This can differ from the character
length if the string contains wide characters.
"""
| python | {
"resource": ""
} |
q11310 | filter_regex | train | def filter_regex(names, regex):
"""
Return a tuple of strings that match the regular | python | {
"resource": ""
} |
q11311 | filter_wildcard | train | def filter_wildcard(names, pattern):
"""
Return a tuple of strings that match a shell-style | python | {
"resource": ""
} |
q11312 | HelpFeature.match | train | def match(self, obj, attrs):
"""
Only match if the object contains a non-empty docstring.
| python | {
"resource": ""
} |
q11313 | PackagedFunction.prepare_namespace | train | def prepare_namespace(self, func):
"""
Prepares the function to be run after deserializing it.
Re-associates any previously bound variables and modules from the closure
Returns:
callable: ready-to-call function
"""
if self.is_imethod:
| python | {
"resource": ""
} |
q11314 | decimal_format | train | def decimal_format(value, TWOPLACES=Decimal(100) ** -2):
'Format a decimal.Decimal like to | python | {
"resource": ""
} |
q11315 | notification_preference | train | def notification_preference(obj_type, profile):
'''Display two radio buttons for turning notifications on or off.
The default value is is have alerts_on = True.
'''
default_alert_value = True
if not profile:
alerts_on = True
else:
| python | {
"resource": ""
} |
q11316 | OldRole.committee_object | train | def committee_object(self):
'''If the committee id no longer exists in mongo for some reason,
this function returns None.
'''
if 'committee_id' in self:
_id = | python | {
"resource": ""
} |
q11317 | Legislator.context_role | train | def context_role(self, bill=None, vote=None, session=None, term=None):
'''Tell this legislator object which session to use when calculating
the legisator's context_role for a given bill or vote.
'''
# If no hints were given about the context, look for a related bill,
# then for a related vote.
if not any([bill, vote, session, term]):
try:
bill = self.bill
except AttributeError:
# A vote?
try:
vote = self.vote
except AttributeError:
# If we're here, this method was called on a
# Legislator was that doesn't have a related bill or vote.
return ''
# If we still have to historical point of reference, figuring
# out the context role is impossible. Return emtpy string.
if not any([bill, vote, session, term]):
return ''
# First figure out the term.
if bill is not None:
term = bill['_term']
elif vote is not None:
try:
_bill = vote.bill
except AttributeError:
_bill = BillVote(vote).bill
if callable(_bill):
_bill = _bill()
term = _bill['_term']
if term is None and session is not None:
term = term_for_session(self[settings.LEVEL_FIELD], session)
# Use the term to get the related roles. First look in the current
# roles list, then fail over to the old_roles list.
roles = [r for r in self['roles']
if r.get('type') == 'member' and r.get('term') == term]
roles = list(filter(None, roles))
if not roles:
roles = [r for r in self.get('old_roles', {}).get(term, [])
if r.get('type') == 'member']
roles = list(filter(None, roles))
if not roles:
# Legislator had no roles for this term. If there is a related
# bill ro vote, this shouldn't happen, but could if the
# legislator's roles got deleted.
return ''
# If there's only one applicable role, we're done.
if len(roles) == 1:
role = roles.pop()
self['context_role'] = role
return role
# If only one of term or session is given and there are multiple roles:
if not list(filter(None, [bill, vote])):
if term is not None:
role = roles[0]
self['context_role'] = role
return role
# Below, use the date of the related bill or vote to determine
# which (of multiple) roles applies.
# Get the context date.
if session is not None:
# If we're here, we have multiple roles for a single session.
# Try to find the correct one in self.metadata,
# else give up.
session_data = self.metadata['session_details'][session]
for role in roles:
role_start = role.get('start_date')
role_end = role.get('end_date')
# Return the first role that overlaps at all with the
# session.
session_start = session_data.get('start_date')
session_end | python | {
"resource": ""
} |
q11318 | Legislator.old_roles_manager | train | def old_roles_manager(self):
'''Return old roles, grouped first by term, then by chamber,
then by type.'''
wrapper = self._old_role_wrapper
chamber_getter = operator.methodcaller('get', 'chamber')
for term, roles in self.get('old_roles', {}).items():
| python | {
"resource": ""
} |
q11319 | NameMatcher._normalize | train | def _normalize(self, name):
"""
Normalizes a legislator name by stripping titles from the front,
converting to lowercase and removing punctuation.
"""
name = re.sub(
r'^(Senator|Representative|Sen\.?|Rep\.?|'
| python | {
"resource": ""
} |
q11320 | NameMatcher.match | train | def match(self, name, chamber=None):
"""
If this matcher has uniquely seen a matching name, return its
value. Otherwise, return None.
If chamber is set then the search will be limited to legislators
with matching chamber. If chamber is None then the search
will be cross-chamber.
"""
try:
return self._manual[chamber][name]
except KeyError:
pass
if chamber == 'joint':
chamber = None
| python | {
"resource": ""
} |
q11321 | get_scraper | train | def get_scraper(mod_path, scraper_type):
""" import a scraper from the scraper registry """
# act of importing puts it into the registry
try:
module = importlib.import_module(mod_path)
except ImportError as e:
raise ScrapeError("could not import %s" % mod_path, e)
# now find the class within the module
ScraperClass = None
for k, v in module.__dict__.items():
if k.startswith('_'):
continue
if getattr(v, 'scraper_type', None) == scraper_type:
if ScraperClass:
| python | {
"resource": ""
} |
q11322 | Scraper._load_schemas | train | def _load_schemas(self):
""" load all schemas into schema dict """
types = ('bill', 'committee', 'person', 'vote', 'event')
for type in types:
schema_path = os.path.join(os.path.split(__file__)[0],
'../schemas/%s.json' % type)
self._schema[type] = json.load(open(schema_path))
self._schema[type]['properties'][settings.LEVEL_FIELD] = {
'minLength': 2, 'type': 'string'}
# bills & votes
self._schema['bill']['properties']['session']['enum'] = \
self.all_sessions()
self._schema['vote']['properties']['session']['enum'] = \
| python | {
"resource": ""
} |
q11323 | Scraper.validate_session | train | def validate_session(self, session, latest_only=False):
""" Check that a session is present in the metadata dictionary.
raises :exc:`~billy.scrape.NoDataForPeriod` if session is invalid
:param session: string representing session to check
"""
if latest_only:
| python | {
"resource": ""
} |
q11324 | Scraper.validate_term | train | def validate_term(self, term, latest_only=False):
""" Check that a term is present in the metadata dictionary.
raises :exc:`~billy.scrape.NoDataForPeriod` if term is invalid
:param term: string representing term to check
:param latest_only: if True, will raise exception if term is not
the current term (default: False)
"""
if latest_only:
if | python | {
"resource": ""
} |
q11325 | SourcedObject.add_source | train | def add_source(self, url, **kwargs):
"""
Add a source URL from which data related to this object was scraped.
:param url: the location | python | {
"resource": ""
} |
q11326 | PlaintextColumns._get_column_ends | train | def _get_column_ends(self):
'''Guess where the ends of the columns lie.
'''
ends = collections.Counter()
for line in self.text.splitlines():
| python | {
"resource": ""
} |
q11327 | PlaintextColumns._get_column_boundaries | train | def _get_column_boundaries(self):
'''Use the guessed ends to guess the boundaries of the plain
text columns.
'''
# Try to figure out the most common column boundaries.
ends = self._get_column_ends()
if not ends:
# If there aren't even any nontrivial sequences of whitespace
# dividing text, there may be just one column. In which case,
# Return a single span, effectively the whole line.
return [slice(None, None)]
most_common = []
threshold = self.threshold
for k, v in collections.Counter(ends.values()).most_common():
if k >= threshold:
most_common.append(k)
if most_common:
boundaries = []
for k, v in ends.items():
if v in most_common:
boundaries.append(k)
else:
# Here there weren't enough boundaries to guess | python | {
"resource": ""
} |
q11328 | PlaintextColumns.getcells | train | def getcells(self, line):
'''Using self.boundaries, extract cells from the given line.
'''
for boundary in self.boundaries:
cell = line.lstrip()[boundary].strip()
if cell:
| python | {
"resource": ""
} |
q11329 | PlaintextColumns.rows | train | def rows(self):
'''Returns an iterator of row tuples.
| python | {
"resource": ""
} |
q11330 | PlaintextColumns.cells | train | def cells(self):
'''Returns an interator of all cells in the table.
'''
for line in self.text.splitlines():
| python | {
"resource": ""
} |
q11331 | PaginatorBase.range_end | train | def range_end(self):
'''"Showing 40 - 50 of 234 results
^
'''
count = self.count
range_end = self.range_start + | python | {
"resource": ""
} |
q11332 | GenericIDMatcher.learn_ids | train | def learn_ids(self, item_list):
""" read in already set ids on objects """
self._reset_sequence()
for item in item_list:
| python | {
"resource": ""
} |
q11333 | GenericIDMatcher.set_ids | train | def set_ids(self, item_list):
""" set ids on an object, using internal mapping then new ids """
self._reset_sequence()
for item in item_list:
| python | {
"resource": ""
} |
q11334 | import_committees_from_legislators | train | def import_committees_from_legislators(current_term, abbr):
""" create committees from legislators that have committee roles """
# for all current legislators
for legislator in db.legislators.find({'roles': {'$elemMatch': {
'term': current_term, settings.LEVEL_FIELD: abbr}}}):
# for all committee roles
for role in legislator['roles']:
if (role['type'] == 'committee member' and
'committee_id' not in role):
spec = {settings.LEVEL_FIELD: abbr,
'chamber': role['chamber'],
'committee': role['committee']}
if 'subcommittee' in role:
spec['subcommittee'] = role['subcommittee']
committee = db.committees.find_one(spec)
if not committee:
committee = spec | python | {
"resource": ""
} |
q11335 | Bill.add_sponsor | train | def add_sponsor(self, type, name, **kwargs):
"""
Associate a sponsor with this bill.
:param type: the type of sponsorship, e.g. 'primary', 'cosponsor'
:param name: the name of the | python | {
"resource": ""
} |
q11336 | Bill.add_version | train | def add_version(self, name, url, mimetype=None, on_duplicate='error',
**kwargs):
"""
Add a version of the text of this bill.
:param name: a name given to this version of the text, e.g.
'As Introduced', 'Version 2', 'As amended', 'Enrolled'
:param url: the location of this version on the legislative website.
:param mimetype: MIME type of the document
:param on_duplicate: What to do if a duplicate is seen:
error - default option, raises a ValueError
ignore - add the document twice (rarely the right choice)
use_new - use the new name, removing the old document
use_old - use the old name, not adding the new document
If multiple formats are provided, a good rule of thumb is to
prefer text, followed by html, followed by pdf/word/etc.
"""
if not mimetype:
raise ValueError('mimetype parameter to add_version is required')
if on_duplicate != 'ignore':
if url in self._seen_versions:
| python | {
"resource": ""
} |
q11337 | Bill.add_action | train | def add_action(self, actor, action, date, type=None, committees=None,
legislators=None, **kwargs):
"""
Add an action that was performed on this bill.
:param actor: a string representing who performed the action.
If the action is associated with one of the chambers this
should be 'upper' or 'lower'. Alternatively, this could be
the name of a committee, a specific legislator, or an outside
actor such as 'Governor'.
:param action: a string representing the action performed, e.g.
'Introduced', 'Signed by the Governor', 'Amended'
:param date: the date/time this action was performed.
:param type: a type classification for this action
;param committees: a committee or list of committees to associate with
this action
"""
| python | {
"resource": ""
} |
q11338 | Bill.add_companion | train | def add_companion(self, bill_id, session=None, chamber=None):
"""
Associate another bill with this one.
If session isn't set it will be set to self['session'].
"""
companion = {'bill_id': bill_id,
| python | {
"resource": ""
} |
q11339 | metadata | train | def metadata(abbr, __metadata=__metadata):
"""
Grab the metadata for the given two-letter abbreviation.
"""
# This data should change very rarely and | python | {
"resource": ""
} |
q11340 | cd | train | def cd(path):
'''Creates the path if it doesn't exist'''
old_dir = os.getcwd()
try:
os.makedirs(path)
except OSError:
| python | {
"resource": ""
} |
q11341 | _get_property_dict | train | def _get_property_dict(schema):
""" given a schema object produce a nested dictionary of fields """
pdict = {}
for k, v in schema['properties'].items():
pdict[k] = {}
| python | {
"resource": ""
} |
q11342 | update | train | def update(old, new, collection, sneaky_update_filter=None):
"""
update an existing object with a new one, only saving it and
setting updated_at if something has changed
old
old object
new
new object
collection
collection to save changed object to
sneaky_update_filter
a filter for updates to object that should be ignored
format is a dict mapping field names to a comparison function
that returns True iff there is a change
"""
# need_save = something has changed
need_save = False
locked_fields = old.get('_locked_fields', [])
for key, value in new.items():
# don't update locked fields
if key in locked_fields:
continue
if old.get(key) != value:
if sneaky_update_filter and key in sneaky_update_filter:
if sneaky_update_filter[key](old[key], value):
old[key] = value
| python | {
"resource": ""
} |
q11343 | convert_timestamps | train | def convert_timestamps(obj):
"""
Convert unix timestamps in the scraper output to python datetimes
so that they will be saved properly as Mongo datetimes.
"""
for key in ('date', 'when', 'end', 'start_date', 'end_date'):
value = obj.get(key)
if value:
try:
obj[key] = _timestamp_to_dt(value)
except TypeError:
| python | {
"resource": ""
} |
q11344 | _make_plus_helper | train | def _make_plus_helper(obj, fields):
""" add a + prefix to any fields in obj that aren't in fields """
new_obj = {}
for key, value in obj.items():
if key in fields or key.startswith('_'):
# if there's a subschema apply it to a list or subdict
if fields.get(key):
if isinstance(value, list):
value = [_make_plus_helper(item, fields[key])
| python | {
"resource": ""
} |
q11345 | make_plus_fields | train | def make_plus_fields(obj):
"""
Add a '+' to the key of non-standard fields.
dispatch to recursive _make_plus_helper | python | {
"resource": ""
} |
q11346 | Metadata.get_object | train | def get_object(cls, abbr):
'''
This particular model needs its own constructor in order to take
advantage of the metadata cache in billy.util, which would otherwise
return unwrapped objects.
'''
obj = get_metadata(abbr)
| python | {
"resource": ""
} |
q11347 | Metadata.committees_legislators | train | def committees_legislators(self, *args, **kwargs):
'''Return an iterable of committees with all the
legislators cached for reference in the Committee model.
So do a "select_related" operation on committee members.
'''
committees = list(self.committees(*args, **kwargs))
legislators = self.legislators({'active': True},
fields=['full_name',
settings.LEVEL_FIELD])
_legislators = {}
# This will be a | python | {
"resource": ""
} |
q11348 | extract_fields | train | def extract_fields(d, fields, delimiter='|'):
""" get values out of an object ``d`` for saving to a csv """
rd = {}
for f in fields:
v = d.get(f, None)
if isinstance(v, (str, unicode)):
| python | {
"resource": ""
} |
q11349 | region_selection | train | def region_selection(request):
'''Handle submission of the region selection form in the base template. '''
form = get_region_select_form(request.GET) | python | {
"resource": ""
} |
q11350 | Committee.add_member | train | def add_member(self, legislator, role='member', **kwargs):
"""
Add a member to the committee object.
:param legislator: name of the legislator
:param role: role that legislator holds in the committee
(eg. chairman) default: 'member'
| python | {
"resource": ""
} |
q11351 | Action.action_display | train | def action_display(self):
'''The action text, with any hyperlinked related entities.'''
action = self['action']
annotations = []
abbr = self.bill[settings.LEVEL_FIELD]
if 'related_entities' in self:
for entity in self['related_entities']:
name = entity['name']
_id = entity['id']
| python | {
"resource": ""
} |
q11352 | ActionsManager._bytype | train | def _bytype(self, action_type, action_spec=None):
'''Return the most recent date on which action_type occurred.
Action spec is a dictionary of key-value attrs to | python | {
"resource": ""
} |
q11353 | BillVote._legislator_objects | train | def _legislator_objects(self):
'''A cache of dereferenced legislator objects.
'''
kwargs = {}
id_getter = operator.itemgetter('leg_id')
ids = []
for k in ('yes', 'no', 'other'):
ids.extend(map(id_getter, self[k + '_votes']))
objs = db.legislators.find({'_all_ids': {'$in': ids}}, **kwargs)
# Handy to keep a reference to the vote on | python | {
"resource": ""
} |
q11354 | BillVote.legislator_vote_value | train | def legislator_vote_value(self):
'''If this vote was accessed through the legislator.votes_manager,
return the value of this legislator's vote.
'''
if not hasattr(self, 'legislator'):
msg = ('legislator_vote_value can only be called '
'from a vote accessed by legislator.votes_manager.')
| python | {
"resource": ""
} |
q11355 | BillVote.is_probably_a_voice_vote | train | def is_probably_a_voice_vote(self):
'''Guess whether this vote is a "voice vote".'''
if '+voice_vote' in self:
return True
if '+vote_type' in self:
if self['+vote_type'] == 'Voice':
| python | {
"resource": ""
} |
q11356 | Event.host | train | def host(self):
'''Return the host committee.
'''
_id = None
for participant in self['participants']:
if participant['type'] == 'host':
if set(['participant_type', 'id']) < set(participant):
# This event uses the id keyname "id".
if participant['participant_type'] == 'committee':
_id = participant['id'] | python | {
"resource": ""
} |
q11357 | Event.host_chairs | train | def host_chairs(self):
'''Returns a list of members that chair the host committee,
including "co-chair" and "chairperson." This could concievalby
yield a false positive if the person's title is 'dunce chair'.
'''
chairs = []
# Host is guaranteed to be a committe or none.
host = self.host()
if host is None:
| python | {
"resource": ""
} |
q11358 | Event.host_members | train | def host_members(self):
'''Return the members of the host committee.
'''
host = self.host()
if host is None:
return
for | python | {
"resource": ""
} |
q11359 | update_common | train | def update_common(obj, report):
""" do updated_at checks """
# updated checks
if obj['updated_at'] >= yesterday:
report['_updated_today_count'] += 1
if obj['updated_at'] >= last_month:
| python | {
"resource": ""
} |
q11360 | normalize_rank | train | def normalize_rank(rank):
"""Normalize a rank in order to be schema-compliant."""
normalized_ranks = {
'BA': 'UNDERGRADUATE',
'BACHELOR': 'UNDERGRADUATE',
'BS': 'UNDERGRADUATE',
'BSC': 'UNDERGRADUATE',
'JUNIOR': 'JUNIOR',
'MAS': 'MASTER',
'MASTER': 'MASTER',
'MS': 'MASTER',
'MSC': 'MASTER',
'PD': 'POSTDOC',
'PHD': 'PHD',
'POSTDOC': 'POSTDOC',
'SENIOR': 'SENIOR',
'STAFF': 'STAFF',
| python | {
"resource": ""
} |
q11361 | get_recid_from_ref | train | def get_recid_from_ref(ref_obj):
"""Retrieve recid from jsonref reference object.
If no recid can be parsed, returns None.
"""
if not isinstance(ref_obj, dict):
| python | {
"resource": ""
} |
q11362 | absolute_url | train | def absolute_url(relative_url):
"""Returns an absolute URL from a URL relative to the server root.
The base URL is taken from the Flask app config if present, otherwise it
falls back to ``http://inspirehep.net``.
"""
default_server = 'http://inspirehep.net'
| python | {
"resource": ""
} |
q11363 | afs_url | train | def afs_url(file_path):
"""Convert a file path to a URL pointing to its path on AFS.
If ``file_path`` doesn't start with ``/opt/cds-invenio/``, and hence is not on
AFS, it returns it unchanged.
The base AFS path is taken from the Flask app config if present, otherwise
it falls back to ``/afs/cern.ch/project/inspire/PROD``.
"""
default_afs_path = '/afs/cern.ch/project/inspire/PROD'
afs_path = current_app.config.get('LEGACY_AFS_PATH', default_afs_path)
if file_path is None:
| python | {
"resource": ""
} |
q11364 | strip_empty_values | train | def strip_empty_values(obj):
"""Recursively strips empty values."""
if isinstance(obj, dict):
new_obj = {}
for key, val in obj.items():
new_val = strip_empty_values(val)
if new_val is not None:
new_obj[key] = new_val
| python | {
"resource": ""
} |
q11365 | dedupe_all_lists | train | def dedupe_all_lists(obj, exclude_keys=()):
"""Recursively remove duplucates from all lists.
Args:
obj: collection to deduplicate
exclude_keys (Container[str]): key names to ignore for deduplication
"""
squared_dedupe_len = 10
if isinstance(obj, dict):
new_obj = {}
for key, value in obj.items():
if key in exclude_keys:
new_obj[key] = value
else:
new_obj[key] = dedupe_all_lists(value)
| python | {
"resource": ""
} |
q11366 | normalize_date_aggressively | train | def normalize_date_aggressively(date):
"""Normalize date, stripping date parts until a valid date is obtained."""
def _strip_last_part(date):
parts = date.split('-')
return '-'.join(parts[:-1])
fake_dates = {'0000', '9999'}
if date in fake_dates:
return None
try:
| python | {
"resource": ""
} |
q11367 | match_country_name_to_its_code | train | def match_country_name_to_its_code(country_name, city=''):
"""Try to match country name with its code.
Name of the city helps when country_name is "Korea".
"""
if country_name:
country_name = country_name.upper().replace('.', '').strip()
if country_to_iso_code.get(country_name):
return country_to_iso_code.get(country_name)
elif country_name == 'KOREA':
if city.upper() in south_korean_cities:
return 'KR'
else:
| python | {
"resource": ""
} |
q11368 | match_us_state | train | def match_us_state(state_string):
"""Try to match a string with one of the states in the US."""
if state_string:
state_string = state_string.upper().replace('.', '').strip()
if us_state_to_iso_code.get(state_string):
return us_state_to_iso_code.get(state_string)
else:
for | python | {
"resource": ""
} |
q11369 | parse_conference_address | train | def parse_conference_address(address_string):
"""Parse a conference address.
This is a pretty dummy address parser. It only extracts country
and state (for US) and should be replaced with something better,
like Google Geocoding.
"""
geo_elements = address_string.split(',')
city = geo_elements[0]
country_name = geo_elements[-1].upper().replace('.', '').strip()
us_state = None
state = None
country_code = None
# Try to match the country
country_code = match_country_name_to_its_code(country_name, city)
if country_code == 'US' and len(geo_elements) > 1:
us_state = match_us_state(geo_elements[-2].upper().strip()
| python | {
"resource": ""
} |
q11370 | parse_institution_address | train | def parse_institution_address(address, city, state_province,
country, postal_code, country_code):
"""Parse an institution address."""
address_list = force_list(address)
state_province = match_us_state(state_province) or state_province
postal_code = force_list(postal_code)
country = force_list(country)
country_code = match_country_code(country_code)
| python | {
"resource": ""
} |
q11371 | name | train | def name(self, key, value):
"""Populate the ``name`` key.
Also populates the ``status``, ``birth_date`` and ``death_date`` keys through side effects.
"""
def _get_title(value):
c_value = force_single_element(value.get('c', ''))
if c_value != 'title (e.g. Sir)':
return c_value
def _get_value(value):
a_value = force_single_element(value.get('a', ''))
q_value = force_single_element(value.get('q', ''))
return a_value or normalize_name(q_value)
if value.get('d'):
dates = value['d']
try:
self['death_date'] = normalize_date(dates)
except ValueError:
dates = dates.split(' - ') | python | {
"resource": ""
} |
q11372 | name2marc | train | def name2marc(self, key, value):
"""Populates the ``100`` field.
Also populates the ``400``, ``880``, and ``667`` fields through side
effects.
"""
result = self.get('100', {})
result['a'] = value.get('value')
result['b'] = value.get('numeration')
result['c'] = value.get('title')
result['q'] = value.get('preferred_name')
if 'name_variants' in value:
self['400'] = [{'a': el} for el in value['name_variants']]
| python | {
"resource": ""
} |
q11373 | positions | train | def positions(self, key, value):
"""Populate the positions field.
Also populates the email_addresses field by side effect.
"""
email_addresses = self.get("email_addresses", [])
current = None
record = None
recid_or_status = force_list(value.get('z'))
for el in recid_or_status:
if el.lower() == 'current':
current = True if value.get('a') else None
else:
record = get_record_ref(maybe_int(el), 'institutions')
rank = normalize_rank(value.get('r'))
current_email_addresses = force_list(value.get('m'))
non_current_email_addresses = force_list(value.get('o'))
email_addresses.extend({
'value': address,
'current': True,
} for address in current_email_addresses)
email_addresses.extend({
'value': address,
| python | {
"resource": ""
} |
q11374 | email_addresses2marc | train | def email_addresses2marc(self, key, value):
"""Populate the 595 MARCXML field.
Also populates the 371 field as a side effect.
"""
m_or_o = 'm' | python | {
"resource": ""
} |
q11375 | email_addresses595 | train | def email_addresses595(self, key, value):
"""Populates the ``email_addresses`` field using the 595 MARCXML field.
Also populates ``_private_notes`` as a side effect.
"""
emails = self.get('email_addresses', [])
if value.get('o'):
emails.append({
'value': value.get('o'),
'current': False,
'hidden': True,
})
if value.get('m'):
emails.append({
'value': value.get('m'),
'current': True,
| python | {
"resource": ""
} |
q11376 | arxiv_categories | train | def arxiv_categories(self, key, value):
"""Populate the ``arxiv_categories`` key.
Also populates the ``inspire_categories`` key through side effects.
"""
def _is_arxiv(category):
return category in valid_arxiv_categories()
def _is_inspire(category):
schema = load_schema('elements/inspire_field')
valid_inspire_categories = schema['properties']['term']['enum']
return category in valid_inspire_categories
def _normalize(a_value):
for category in valid_arxiv_categories():
if a_value.lower() == category.lower():
return normalize_arxiv_category(category)
schema = load_schema('elements/inspire_field')
valid_inspire_categories = schema['properties']['term']['enum']
for category in valid_inspire_categories:
if a_value.lower() == category.lower():
| python | {
"resource": ""
} |
q11377 | urls | train | def urls(self, key, value):
"""Populate the ``url`` key.
Also populates the ``ids`` key through side effects.
"""
description = force_single_element(value.get('y'))
url = value.get('u')
linkedin_match = LINKEDIN_URL.match(url)
twitter_match = TWITTER_URL.match(url)
wikipedia_match = WIKIPEDIA_URL.match(url)
if linkedin_match:
self.setdefault('ids', []).append(
{
'schema': 'LINKEDIN',
'value': unquote_url(linkedin_match.group('page')),
| python | {
"resource": ""
} |
q11378 | new_record | train | def new_record(self, key, value):
"""Populate the ``new_record`` key.
Also populates the ``ids`` key through side effects.
"""
new_record = self.get('new_record', {})
ids = self.get('ids', [])
for value in force_list(value):
for id_ in force_list(value.get('a')):
ids.append({
'schema': 'SPIRES',
'value': id_,
| python | {
"resource": ""
} |
q11379 | isbns | train | def isbns(self, key, value):
"""Populate the ``isbns`` key."""
def _get_medium(value):
def _normalize(medium):
schema = load_schema('hep')
valid_media = schema['properties']['isbns']['items']['properties']['medium']['enum']
medium = medium.lower().replace('-', '').replace(' ', '')
if medium in valid_media:
return medium
elif medium == 'ebook':
return 'online'
elif medium == 'paperback':
return 'softcover'
| python | {
"resource": ""
} |
q11380 | dois | train | def dois(self, key, value):
"""Populate the ``dois`` key.
Also populates the ``persistent_identifiers`` key through side effects.
"""
def _get_first_non_curator_source(sources):
sources_without_curator = [el for el in sources if el.upper() != 'CURATOR']
return force_single_element(sources_without_curator)
def _get_material(value):
MATERIAL_MAP = {
'ebook': 'publication',
}
q_value = force_single_element(value.get('q', ''))
normalized_q_value = q_value.lower()
return MATERIAL_MAP.get(normalized_q_value, normalized_q_value)
def _is_doi(id_, type_):
return (not type_ or type_.upper() == 'DOI') and is_doi(id_)
def _is_handle(id_, type_):
return (not type_ or type_.upper() == 'HDL') and is_handle(id_)
dois = self.get('dois', [])
persistent_identifiers = self.get('persistent_identifiers', [])
values = force_list(value)
for value in values:
id_ = force_single_element(value.get('a', ''))
| python | {
"resource": ""
} |
q11381 | texkeys | train | def texkeys(self, key, value):
"""Populate the ``texkeys`` key.
Also populates the ``external_system_identifiers`` and ``_desy_bookkeeping`` keys through side effects.
"""
def _is_oai(id_, schema):
return id_.startswith('oai:')
def _is_desy(id_, schema):
return id_ and schema in ('DESY',)
def _is_texkey(id_, schema):
return id_ and schema in ('INSPIRETeX', 'SPIRESTeX')
texkeys = self.get('texkeys', [])
external_system_identifiers = self.get('external_system_identifiers', [])
_desy_bookkeeping = self.get('_desy_bookkeeping', [])
values = force_list(value)
for value in values:
ids = force_list(value.get('a', ''))
other_ids = force_list(value.get('z', ''))
schema = force_single_element(value.get('9', ''))
for id_ in ids:
| python | {
"resource": ""
} |
q11382 | arxiv_eprints | train | def arxiv_eprints(self, key, value):
"""Populate the ``arxiv_eprints`` key.
Also populates the ``report_numbers`` key through side effects.
"""
def _get_clean_arxiv_eprint(id_):
return id_.split(':')[-1]
def _is_arxiv_eprint(id_, source):
return source.lower() == 'arxiv'
def _is_hidden_report_number(other_id, source):
return other_id
def _get_clean_source(source):
if source == 'arXiv:reportnumber':
return 'arXiv'
return source
arxiv_eprints = self.get('arxiv_eprints', [])
report_numbers = self.get('report_numbers', [])
values = force_list(value)
for value in values: | python | {
"resource": ""
} |
q11383 | languages | train | def languages(self, key, value):
"""Populate the ``languages`` key."""
languages = self.get('languages', [])
values = force_list(value.get('a'))
for value in values:
for language in RE_LANGUAGE.split(value):
try:
| python | {
"resource": ""
} |
q11384 | languages2marc | train | def languages2marc(self, key, value):
"""Populate the ``041`` | python | {
"resource": ""
} |
q11385 | record_affiliations | train | def record_affiliations(self, key, value):
"""Populate the ``record_affiliations`` key."""
record = get_record_ref(value.get('z'), 'institutions') | python | {
"resource": ""
} |
q11386 | document_type | train | def document_type(self, key, value):
"""Populate the ``document_type`` key.
Also populates the ``_collections``, ``citeable``, ``core``, ``deleted``,
``refereed``, ``publication_type``, and ``withdrawn`` keys through side
effects.
"""
schema = load_schema('hep')
publication_type_schema = schema['properties']['publication_type']
valid_publication_types = publication_type_schema['items']['enum']
document_type = self.get('document_type', [])
publication_type = self.get('publication_type', [])
a_values = force_list(value.get('a'))
for a_value in a_values:
normalized_a_value = a_value.strip().lower()
if normalized_a_value == 'arxiv':
continue # XXX: ignored.
elif normalized_a_value == 'citeable':
self['citeable'] = True
elif normalized_a_value == 'core':
self['core'] = True
elif normalized_a_value == 'noncore':
self['core'] = False
elif normalized_a_value == 'published':
self['refereed'] = True
elif normalized_a_value == 'withdrawn':
self['withdrawn'] = True
elif normalized_a_value == 'deleted':
| python | {
"resource": ""
} |
q11387 | document_type2marc | train | def document_type2marc(self, key, value):
"""Populate the ``980`` MARC field."""
if value in | python | {
"resource": ""
} |
q11388 | references | train | def references(self, key, value):
"""Populate the ``references`` key."""
def _has_curator_flag(value):
normalized_nine_values = [el.upper() for el in force_list(value.get('9'))]
return 'CURATOR' in normalized_nine_values
def _is_curated(value):
return force_single_element(value.get('z')) == '1' and _has_curator_flag(value)
def _set_record(el):
recid = maybe_int(el)
record = get_record_ref(recid, 'literature')
rb.set_record(record)
rb = ReferenceBuilder()
mapping = [
('0', _set_record),
('a', rb.add_uid),
| python | {
"resource": ""
} |
q11389 | documents | train | def documents(self, key, value):
"""Populate the ``documents`` key.
Also populates the ``figures`` key through side effects.
"""
def _is_hidden(value):
return 'HIDDEN' in [val.upper() for val in value] or None
def _is_figure(value):
figures_extensions = ['.png']
return value.get('f') in figures_extensions
def _is_fulltext(value):
return value.get('d', '').lower() == 'fulltext' or None
def _get_index_and_caption(value):
match = re.compile(r'(^\d{5})?\s*(.*)').match(value)
if match:
return match.group(1), match.group(2)
def _get_key(value):
fname = value.get('n', 'document')
extension = value.get('f', '')
if fname.endswith(extension):
return fname
return fname + extension
def _get_source(value):
source = value.get('t', '')
if source in ('INSPIRE-PUBLIC', 'Main'):
source = None
elif source.lower() == 'arxiv':
return 'arxiv'
return source
figures = self.get('figures', [])
is_context = value.get('f', '').endswith('context')
if is_context:
return
| python | {
"resource": ""
} |
q11390 | marcxml2record | train | def marcxml2record(marcxml):
"""Convert a MARCXML string to a JSON record.
Tries to guess which set of rules to use by inspecting the contents
of the ``980__a`` MARC field, but falls back to HEP in case nothing
matches, because records belonging to special collections logically
belong to the Literature collection but don't have ``980__a:HEP``.
Args:
marcxml(str): a string containing MARCXML.
Returns:
dict: a JSON record converted from the string.
"""
marcjson = create_record(marcxml, keep_singletons=False)
collections = _get_collections(marcjson)
if 'conferences' in collections:
return conferences.do(marcjson)
elif 'data' in collections:
return data.do(marcjson)
elif 'experiment' in collections:
| python | {
"resource": ""
} |
q11391 | record2marcxml | train | def record2marcxml(record):
"""Convert a JSON record to a MARCXML string.
Deduces which set of rules to use by parsing the ``$schema`` key, as
it unequivocally determines which kind of record we have.
Args:
record(dict): a JSON record.
Returns:
str: a MARCXML string converted from the record.
"""
schema_name = _get_schema_name(record)
if schema_name == 'hep':
marcjson = hep2marc.do(record)
elif schema_name == 'authors':
marcjson = hepnames2marc.do(record)
else:
raise NotImplementedError(u'JSON -> MARC rules missing for "{}"'.format(schema_name))
record = RECORD()
for key, values in sorted(iteritems(marcjson)):
tag, ind1, ind2 = _parse_key(key)
if _is_controlfield(tag, ind1, ind2):
value = force_single_element(values)
if not isinstance(value, text_type):
value = text_type(value)
record.append(CONTROLFIELD(_strip_invalid_chars_for_xml(value), {'tag': tag}))
else:
for value in force_list(values):
| python | {
"resource": ""
} |
q11392 | number_of_pages | train | def number_of_pages(self, key, value):
"""Populate the ``number_of_pages`` key."""
| python | {
"resource": ""
} |
q11393 | nonfirst_authors | train | def nonfirst_authors(self, key, value):
"""Populate ``700`` MARC field.
Also populates the ``701`` MARC field through side-effects.
"""
field_700 = self.get('700__', [])
field_701 = self.get('701__', [])
is_supervisor = any(el.lower().startswith('dir') for el in force_list(value.get('e', '')))
| python | {
"resource": ""
} |
q11394 | urls | train | def urls(self, key, value):
"""Populate the ``8564`` MARC field.
Also populate the ``FFT`` field through side effects.
"""
def _is_preprint(value):
return value.get('y', '').lower() == 'preprint'
def _is_fulltext(value):
return value['u'].endswith('.pdf') and value['u'].startswith('http://cds.cern.ch')
def _is_local_copy(value):
return 'local copy' in value.get('y', '')
def _is_ignored_domain(value):
ignored_domains = ['http://cdsweb.cern.ch', 'http://cms.cern.ch',
'http://cmsdoc.cern.ch', 'http://documents.cern.ch',
'http://preprints.cern.ch', 'http://cds.cern.ch',
'http://arxiv.org']
return any(value['u'].startswith(domain) for domain in ignored_domains)
field_8564 = self.get('8564_', [])
field_FFT = self.get('FFT__', [])
if 'u' not in value:
return field_8564
url = escape_url(value['u'])
if _is_fulltext(value) and not _is_preprint(value):
if _is_local_copy(value):
description = value.get('y', '').replace('local copy', 'on CERN Document Server')
field_8564.append({
'u': url,
| python | {
"resource": ""
} |
q11395 | titles | train | def titles(self, key, value):
"""Populate the ``titles`` key."""
if not key.startswith('245'):
return {
'source': value.get('9'),
'subtitle': value.get('b'),
'title': value.get('a'),
}
| python | {
"resource": ""
} |
q11396 | title_translations | train | def title_translations(self, key, value):
"""Populate the ``title_translations`` key."""
return {
| python | {
"resource": ""
} |
q11397 | titles2marc | train | def titles2marc(self, key, values):
"""Populate the ``246`` MARC field.
Also populates the ``245`` MARC field through side effects.
"""
first, rest = values[0], values[1:]
self.setdefault('245', []).append({
'a': first.get('title'),
'b': first.get('subtitle'),
'9': first.get('source'),
}) | python | {
"resource": ""
} |
q11398 | title_translations2marc | train | def title_translations2marc(self, key, value):
"""Populate the ``242`` MARC field."""
return {
'a': value.get('title'),
| python | {
"resource": ""
} |
q11399 | imprints | train | def imprints(self, key, value):
"""Populate the ``imprints`` key."""
return {
| python | {
"resource": ""
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.