text_prompt
stringlengths
157
13.1k
code_prompt
stringlengths
7
19.8k
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def thread_raise(thread, exctype): ''' Raises or queues the exception `exctype` for the thread `thread`. See the documentation on the function `thread_exception_gate()` for more information. Adapted from http://tomerfiliba.com/recipes/Thread2/ which explains: "The exception will be raised only when executing python bytecode. If your thread calls a native/built-in blocking function, the exception will be raised only when execution returns to the python code." Raises: TypeError if `exctype` is not a class ValueError, SystemError in case of unexpected problems ''' import ctypes, inspect, threading, logging if not inspect.isclass(exctype): raise TypeError( 'cannot raise %s, only exception types can be raised (not ' 'instances)' % exctype) gate = thread_exception_gate(thread) with gate.lock: if gate.ok_to_raise.is_set() and thread.is_alive(): gate.ok_to_raise.clear() logging.info('raising %s in thread %s', exctype, thread) res = ctypes.pythonapi.PyThreadState_SetAsyncExc( ctypes.c_long(thread.ident), ctypes.py_object(exctype)) if res == 0: raise ValueError( 'invalid thread id? thread.ident=%s' % thread.ident) elif res != 1: # if it returns a number greater than one, you're in trouble, # and you should call it again with exc=NULL to revert the effect ctypes.pythonapi.PyThreadState_SetAsyncExc(thread.ident, 0) raise SystemError('PyThreadState_SetAsyncExc failed') else: logging.info('queueing %s for thread %s', exctype, thread) gate.queue_exception(exctype)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def sleep(duration): ''' Sleeps for duration seconds in increments of 0.5 seconds. Use this so that the sleep can be interrupted by thread_raise(). ''' import time start = time.time() while True: elapsed = time.time() - start if elapsed >= duration: break time.sleep(min(duration - elapsed, 0.5))
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def acquire_multi(self, n=1): ''' Returns a list of up to `n` browsers. Raises: NoBrowsersAvailable if none available ''' browsers = [] with self._lock: if len(self._in_use) >= self.size: raise NoBrowsersAvailable while len(self._in_use) < self.size and len(browsers) < n: browser = self._fresh_browser() browsers.append(browser) self._in_use.add(browser) return browsers
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def acquire(self): ''' Returns an available instance. Returns: browser from pool, if available Raises: NoBrowsersAvailable if none available ''' with self._lock: if len(self._in_use) >= self.size: raise NoBrowsersAvailable browser = self._fresh_browser() self._in_use.add(browser) return browser
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def _on_error(self, websock, e): ''' Raises BrowsingException in the thread that created this instance. ''' if isinstance(e, ( websocket.WebSocketConnectionClosedException, ConnectionResetError)): self.logger.error('websocket closed, did chrome die?') else: self.logger.error( 'exception from websocket receiver thread', exc_info=1) brozzler.thread_raise(self.calling_thread, BrowsingException)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def start(self, **kwargs): ''' Starts chrome if it's not running. Args: **kwargs: arguments for self.chrome.start(...) ''' if not self.is_running(): self.websock_url = self.chrome.start(**kwargs) self.websock = websocket.WebSocketApp(self.websock_url) self.websock_thread = WebsockReceiverThread( self.websock, name='WebsockThread:%s' % self.chrome.port) self.websock_thread.start() self._wait_for(lambda: self.websock_thread.is_open, timeout=30) # tell browser to send us messages we're interested in self.send_to_chrome(method='Network.enable') self.send_to_chrome(method='Page.enable') self.send_to_chrome(method='Console.enable') self.send_to_chrome(method='Runtime.enable') self.send_to_chrome(method='ServiceWorker.enable') self.send_to_chrome(method='ServiceWorker.setForceUpdateOnPageLoad') # disable google analytics self.send_to_chrome( method='Network.setBlockedURLs', params={'urls': ['*google-analytics.com/analytics.js', '*google-analytics.com/ga.js']})
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def stop(self): ''' Stops chrome if it's running. ''' try: if (self.websock and self.websock.sock and self.websock.sock.connected): self.logger.info('shutting down websocket connection') try: self.websock.close() except BaseException as e: self.logger.error( 'exception closing websocket %s - %s', self.websock, e) self.chrome.stop() if self.websock_thread and ( self.websock_thread != threading.current_thread()): self.websock_thread.join(timeout=30) if self.websock_thread.is_alive(): self.logger.error( '%s still alive 30 seconds after closing %s, will ' 'forcefully nudge it again', self.websock_thread, self.websock) self.websock.keep_running = False self.websock_thread.join(timeout=30) if self.websock_thread.is_alive(): self.logger.critical( '%s still alive 60 seconds after closing %s', self.websock_thread, self.websock) self.websock_url = None except: self.logger.error('problem stopping', exc_info=True)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def browse_page( self, page_url, extra_headers=None, user_agent=None, behavior_parameters=None, behaviors_dir=None, on_request=None, on_response=None, on_service_worker_version_updated=None, on_screenshot=None, username=None, password=None, hashtags=None, skip_extract_outlinks=False, skip_visit_hashtags=False, skip_youtube_dl=False, page_timeout=300, behavior_timeout=900): ''' Browses page in browser. Browser should already be running, i.e. start() should have been called. Opens the page_url in the browser, runs behaviors, takes a screenshot, extracts outlinks. Args: page_url: url of the page to browse extra_headers: dict of extra http headers to configure the browser to send with every request (default None) user_agent: user agent string, replaces browser default if supplied (default None) behavior_parameters: dict of parameters for populating the javascript behavior template (default None) behaviors_dir: Directory containing behaviors.yaml and JS templates (default None loads Brozzler default JS behaviors) on_request: callback to invoke on every Network.requestWillBeSent event, takes one argument, the json-decoded message (default None) on_response: callback to invoke on every Network.responseReceived event, takes one argument, the json-decoded message (default None) on_service_worker_version_updated: callback to invoke on every ServiceWorker.workerVersionUpdated event, takes one argument, the json-decoded message (default None) on_screenshot: callback to invoke when screenshot is obtained, takes one argument, the the raw jpeg bytes (default None) # XXX takes two arguments, the url of the page at the time the # screenshot was taken, and the raw jpeg bytes (default None) username: username string to use to try logging in if a login form is found in the page (default None) password: password string to use to try logging in if a login form is found in the page (default None) ... (there are more) Returns: A tuple (final_page_url, outlinks). final_page_url: the url in the location bar at the end of the browse_page cycle, which could be different from the original page url if the page redirects, javascript has changed the url in the location bar, etc outlinks: a list of navigational links extracted from the page Raises: brozzler.ProxyError: in case of proxy connection error BrowsingException: if browsing the page fails in some other way ''' if not self.is_running(): raise BrowsingException('browser has not been started') if self.is_browsing: raise BrowsingException('browser is already busy browsing a page') self.is_browsing = True if on_request: self.websock_thread.on_request = on_request if on_response: self.websock_thread.on_response = on_response if on_service_worker_version_updated: self.websock_thread.on_service_worker_version_updated = \ on_service_worker_version_updated try: with brozzler.thread_accept_exceptions(): self.configure_browser( extra_headers=extra_headers, user_agent=user_agent) self.navigate_to_page(page_url, timeout=page_timeout) if password: self.try_login(username, password, timeout=page_timeout) # if login redirected us, return to page_url if page_url != self.url().split('#')[0]: self.logger.debug( 'login navigated away from %s; returning!', page_url) self.navigate_to_page(page_url, timeout=page_timeout) if on_screenshot: self._try_screenshot(on_screenshot) behavior_script = brozzler.behavior_script( page_url, behavior_parameters, behaviors_dir=behaviors_dir) self.run_behavior(behavior_script, timeout=behavior_timeout) if skip_extract_outlinks: outlinks = [] else: outlinks = self.extract_outlinks() if not skip_visit_hashtags: self.visit_hashtags(self.url(), hashtags, outlinks) final_page_url = self.url() return final_page_url, outlinks except brozzler.ReachedLimit: # websock_thread has stashed the ReachedLimit exception with # more information, raise that one raise self.websock_thread.reached_limit except websocket.WebSocketConnectionClosedException as e: self.logger.error('websocket closed, did chrome die?') raise BrowsingException(e) finally: self.is_browsing = False self.websock_thread.on_request = None self.websock_thread.on_response = None
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def url(self, timeout=30): ''' Returns value of document.URL from the browser. ''' self.websock_thread.expect_result(self._command_id.peek()) msg_id = self.send_to_chrome( method='Runtime.evaluate', params={'expression': 'document.URL'}) self._wait_for( lambda: self.websock_thread.received_result(msg_id), timeout=timeout) message = self.websock_thread.pop_result(msg_id) return message['result']['result']['value']
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def brozzler_new_job(argv=None): ''' Command line utility entry point for queuing a new brozzler job. Takes a yaml brozzler job configuration file, creates job, sites, and pages objects in rethinkdb, which brozzler-workers will look at and start crawling. ''' argv = argv or sys.argv arg_parser = argparse.ArgumentParser( prog=os.path.basename(argv[0]), description='brozzler-new-job - queue new job with brozzler', formatter_class=BetterArgumentDefaultsHelpFormatter) arg_parser.add_argument( 'job_conf_file', metavar='JOB_CONF_FILE', help='brozzler job configuration file in yaml') add_rethinkdb_options(arg_parser) add_common_options(arg_parser, argv) args = arg_parser.parse_args(args=argv[1:]) configure_logging(args) rr = rethinker(args) frontier = brozzler.RethinkDbFrontier(rr) try: brozzler.new_job_file(frontier, args.job_conf_file) except brozzler.InvalidJobConf as e: print('brozzler-new-job: invalid job file:', args.job_conf_file, file=sys.stderr) print(' ' + yaml.dump(e.errors).rstrip().replace('\n', '\n '), file=sys.stderr) sys.exit(1)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def brozzler_new_site(argv=None): ''' Command line utility entry point for queuing a new brozzler site. Takes a seed url and creates a site and page object in rethinkdb, which brozzler-workers will look at and start crawling. ''' argv = argv or sys.argv arg_parser = argparse.ArgumentParser( prog=os.path.basename(argv[0]), description='brozzler-new-site - register site to brozzle', formatter_class=BetterArgumentDefaultsHelpFormatter) arg_parser.add_argument('seed', metavar='SEED', help='seed url') add_rethinkdb_options(arg_parser) arg_parser.add_argument( '--time-limit', dest='time_limit', default=None, help='time limit in seconds for this site') arg_parser.add_argument( '--ignore-robots', dest='ignore_robots', action='store_true', help='ignore robots.txt for this site') arg_parser.add_argument( '--warcprox-meta', dest='warcprox_meta', help=( 'Warcprox-Meta http request header to send with each request; ' 'must be a json blob, ignored unless warcprox features are ' 'enabled')) arg_parser.add_argument( '--behavior-parameters', dest='behavior_parameters', default=None, help=( 'json blob of parameters to populate the javascript behavior ' 'template, e.g. {"parameter_username":"x",' '"parameter_password":"y"}')) arg_parser.add_argument( '--username', dest='username', default=None, help='use this username to try to log in if a login form is found') arg_parser.add_argument( '--password', dest='password', default=None, help='use this password to try to log in if a login form is found') add_common_options(arg_parser, argv) args = arg_parser.parse_args(args=argv[1:]) configure_logging(args) rr = rethinker(args) site = brozzler.Site(rr, { 'seed': args.seed, 'time_limit': int(args.time_limit) if args.time_limit else None, 'ignore_robots': args.ignore_robots, 'warcprox_meta': json.loads( args.warcprox_meta) if args.warcprox_meta else None, 'behavior_parameters': json.loads( args.behavior_parameters) if args.behavior_parameters else None, 'username': args.username, 'password': args.password}) frontier = brozzler.RethinkDbFrontier(rr) brozzler.new_site(frontier, site)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def brozzler_list_captures(argv=None): ''' Handy utility for looking up entries in the rethinkdb "captures" table by url or sha1. ''' import urlcanon argv = argv or sys.argv arg_parser = argparse.ArgumentParser( prog=os.path.basename(argv[0]), formatter_class=BetterArgumentDefaultsHelpFormatter) arg_parser.add_argument( '-p', '--prefix', dest='prefix', action='store_true', help=( 'use prefix match for url (n.b. may not work as expected if ' 'searching key has query string because canonicalization can ' 'reorder query parameters)')) arg_parser.add_argument( '--yaml', dest='yaml', action='store_true', help=( 'yaml output (default is json)')) add_rethinkdb_options(arg_parser) add_common_options(arg_parser, argv) arg_parser.add_argument( 'url_or_sha1', metavar='URL_or_SHA1', help='url or sha1 to look up in captures table') args = arg_parser.parse_args(args=argv[1:]) configure_logging(args) rr = rethinker(args) if args.url_or_sha1[:5] == 'sha1:': if args.prefix: logging.warn( 'ignoring supplied --prefix option which does not apply ' 'to lookup by sha1') # assumes it's already base32 (XXX could detect if hex and convert) sha1base32 = args.url_or_sha1[5:].upper() reql = rr.table('captures').between( [sha1base32, r.minval, r.minval], [sha1base32, r.maxval, r.maxval], index='sha1_warc_type') logging.debug('querying rethinkdb: %s', reql) results = reql.run() else: key = urlcanon.semantic(args.url_or_sha1).surt().decode('ascii') abbr_start_key = key[:150] if args.prefix: # surt is necessarily ascii and \x7f is the last ascii character abbr_end_key = key[:150] + '\x7f' end_key = key + '\x7f' else: abbr_end_key = key[:150] end_key = key reql = rr.table('captures').between( [abbr_start_key, r.minval], [abbr_end_key, r.maxval], index='abbr_canon_surt_timestamp', right_bound='closed') reql = reql.order_by(index='abbr_canon_surt_timestamp') reql = reql.filter( lambda capture: (capture['canon_surt'] >= key) & (capture['canon_surt'] <= end_key)) logging.debug('querying rethinkdb: %s', reql) results = reql.run() if args.yaml: yaml.dump_all( results, stream=sys.stdout, explicit_start=True, default_flow_style=False) else: for result in results: print(json.dumps(result, cls=Jsonner, indent=2))
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def _warcprox_opts(self, args): ''' Takes args as produced by the argument parser built by _build_arg_parser and builds warcprox arguments object suitable to pass to warcprox.main.init_controller. Copies some arguments, renames some, populates some with defaults appropriate for brozzler-easy, etc. ''' warcprox_opts = warcprox.Options() warcprox_opts.address = 'localhost' # let the OS choose an available port; discover it later using # sock.getsockname()[1] warcprox_opts.port = 0 warcprox_opts.cacert = args.cacert warcprox_opts.certs_dir = args.certs_dir warcprox_opts.directory = args.warcs_dir warcprox_opts.gzip = True warcprox_opts.prefix = 'brozzler' warcprox_opts.size = 1000 * 1000* 1000 warcprox_opts.rollover_idle_time = 3 * 60 warcprox_opts.digest_algorithm = 'sha1' warcprox_opts.base32 = True warcprox_opts.stats_db_file = None warcprox_opts.playback_port = None warcprox_opts.playback_index_db_file = None warcprox_opts.rethinkdb_big_table_url = ( 'rethinkdb://%s/%s/captures' % ( args.rethinkdb_servers, args.rethinkdb_db)) warcprox_opts.queue_size = 500 warcprox_opts.max_threads = None warcprox_opts.profile = False warcprox_opts.onion_tor_socks_proxy = args.onion_tor_socks_proxy return warcprox_opts
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def _reppy_rules_getitem(self, agent): ''' Find the user-agent token matching the supplied full user-agent, using a case-insensitive substring search. ''' lc_agent = agent.lower() for s in self.agents: if s in lc_agent: return self.agents[s] return self.agents.get('*')
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def is_permitted_by_robots(site, url, proxy=None): ''' Checks if `url` is permitted by robots.txt. Treats any kind of error fetching robots.txt as "allow all". See http://builds.archive.org/javadoc/heritrix-3.x-snapshot/org/archive/modules/net/CrawlServer.html#updateRobots(org.archive.modules.CrawlURI) for some background on that policy. Returns: bool: `True` if `site.ignore_robots` is set, or if `url` is permitted by robots.txt, `False` otherwise Raises: brozzler.ReachedLimit: if warcprox responded with 420 Reached Limit requests.exceptions.ProxyError: if the proxy is down ''' if site.ignore_robots: return True try: result = _robots_cache(site, proxy).allowed( url, site.user_agent or "brozzler") return result except Exception as e: if isinstance(e, reppy.exceptions.ServerError) and isinstance( e.args[0], brozzler.ReachedLimit): raise e.args[0] elif hasattr(e, 'args') and isinstance( e.args[0], requests.exceptions.ProxyError): # reppy has wrapped an exception that we want to bubble up raise brozzler.ProxyError(e) else: logging.warn( "returning true (permitted) after problem fetching " "robots.txt for %r: %r", url, e) return True
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def final_bounces(fetches, url): """ Resolves redirect chains in `fetches` and returns a list of fetches representing the final redirect destinations of the given url. There could be more than one if for example youtube-dl hit the same url with HEAD and then GET requests. """
redirects = {} for fetch in fetches: # XXX check http status 301,302,303,307? check for "uri" header # as well as "location"? see urllib.request.HTTPRedirectHandler if 'location' in fetch['response_headers']: redirects[fetch['url']] = fetch final_url = url while final_url in redirects: fetch = redirects.pop(final_url) final_url = urllib.parse.urljoin( fetch['url'], fetch['response_headers']['location']) final_bounces = [] for fetch in fetches: if fetch['url'] == final_url: final_bounces.append(fetch) return final_bounces
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def _remember_videos(page, fetches, stitch_ups=None): ''' Saves info about videos captured by youtube-dl in `page.videos`. ''' if not 'videos' in page: page.videos = [] for fetch in fetches or []: content_type = fetch['response_headers'].get_content_type() if (content_type.startswith('video/') # skip manifests of DASH segmented video - # see https://github.com/internetarchive/brozzler/pull/70 and content_type != 'video/vnd.mpeg.dash.mpd' and fetch['method'] == 'GET' and fetch['response_code'] in (200, 206)): video = { 'blame': 'youtube-dl', 'url': fetch['url'], 'response_code': fetch['response_code'], 'content-type': content_type, } if 'content-length' in fetch['response_headers']: video['content-length'] = int( fetch['response_headers']['content-length']) if 'content-range' in fetch['response_headers']: video['content-range'] = fetch[ 'response_headers']['content-range'] logging.debug('embedded video %s', video) page.videos.append(video) for stitch_up in stitch_ups or []: if stitch_up['content-type'].startswith('video/'): video = { 'blame': 'youtube-dl', 'url': stitch_up['url'], 'response_code': stitch_up['response_code'], 'content-type': stitch_up['content-type'], 'content-length': stitch_up['content-length'], } logging.debug('embedded video %s', video) page.videos.append(video)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def do_youtube_dl(worker, site, page): ''' Runs youtube-dl configured for `worker` and `site` to download videos from `page`. Args: worker (brozzler.BrozzlerWorker): the calling brozzler worker site (brozzler.Site): the site we are brozzling page (brozzler.Page): the page we are brozzling Returns: tuple with two entries: `list` of `dict`: with info about urls fetched: [{ 'url': ..., 'method': ..., 'response_code': ..., 'response_headers': ..., }, ...] `list` of `str`: outlink urls ''' with tempfile.TemporaryDirectory(prefix='brzl-ydl-') as tempdir: ydl = _build_youtube_dl(worker, tempdir, site) ie_result = _try_youtube_dl(worker, ydl, site, page) outlinks = set() if ie_result and ie_result.get('extractor') == 'youtube:playlist': # youtube watch pages as outlinks outlinks = {'https://www.youtube.com/watch?v=%s' % e['id'] for e in ie_result.get('entries_no_dl', [])} # any outlinks for other cases? return ydl.fetch_spy.fetches, outlinks
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def pages(site_id): """Pages already crawled."""
start = int(flask.request.args.get("start", 0)) end = int(flask.request.args.get("end", start + 90)) reql = rr.table("pages").between( [site_id, 1, r.minval], [site_id, r.maxval, r.maxval], index="least_hops").order_by(index="least_hops")[start:end] logging.debug("querying rethinkdb: %s", reql) pages_ = reql.run() return flask.jsonify(pages=list(pages_))
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def check_version(chrome_exe): ''' Raises SystemExit if `chrome_exe` is not a supported browser version. Must run in the main thread to have the desired effect. ''' # mac$ /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version # Google Chrome 64.0.3282.140 # mac$ /Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary --version # Google Chrome 66.0.3341.0 canary # linux$ chromium-browser --version # Using PPAPI flash. # --ppapi-flash-path=/usr/lib/adobe-flashplugin/libpepflashplayer.so --ppapi-flash-version= # Chromium 61.0.3163.100 Built on Ubuntu , running on Ubuntu 16.04 cmd = [chrome_exe, '--version'] out = subprocess.check_output(cmd, timeout=60) m = re.search(br'(Chromium|Google Chrome) ([\d.]+)', out) if not m: sys.exit( 'unable to parse browser version from output of ' '%r: %r' % (subprocess.list2cmdline(cmd), out)) version_str = m.group(2).decode() major_version = int(version_str.split('.')[0]) if major_version < 64: sys.exit('brozzler requires chrome/chromium version 64 or ' 'later but %s reports version %s' % ( chrome_exe, version_str))
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def _service_heartbeat_if_due(self): '''Sends service registry heartbeat if due''' due = False if self._service_registry: if not hasattr(self, "status_info"): due = True else: d = doublethink.utcnow() - self.status_info["last_heartbeat"] due = d.total_seconds() > self.HEARTBEAT_INTERVAL if due: self._service_heartbeat()
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def _start_browsing_some_sites(self): ''' Starts browsing some sites. Raises: NoBrowsersAvailable if none available ''' # acquire_multi() raises NoBrowsersAvailable if none available browsers = self._browser_pool.acquire_multi( (self._browser_pool.num_available() + 1) // 2) try: sites = self._frontier.claim_sites(len(browsers)) except: self._browser_pool.release_all(browsers) raise for i in range(len(browsers)): if i < len(sites): th = threading.Thread( target=self._brozzle_site_thread_target, args=(browsers[i], sites[i]), name="BrozzlingThread:%s" % browsers[i].chrome.port, daemon=True) with self._browsing_threads_lock: self._browsing_threads.add(th) th.start() else: self._browser_pool.release(browsers[i])
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def _createtoken(self, type_, value, flags=None): '''create a token with position information''' pos = None assert len(self._positions) >= 2, (type_, value) p2 = self._positions.pop() p1 = self._positions.pop() pos = [p1, p2] return token(type_, value, pos, flags)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def parse(s, strictmode=True, expansionlimit=None, convertpos=False): '''parse the input string, returning a list of nodes top level node kinds are: - command - a simple command - pipeline - a series of simple commands - list - a series of one or more pipelines - compound - contains constructs for { list; }, (list), if, for.. leafs are word nodes (which in turn can also contain any of the aforementioned nodes due to command substitutions). when strictmode is set to False, we will: - skip reading a heredoc if we're at the end of the input expansionlimit is used to limit the amount of recursive parsing done due to command substitutions found during word expansion. ''' p = _parser(s, strictmode=strictmode, expansionlimit=expansionlimit) parts = [p.parse()] class endfinder(ast.nodevisitor): def __init__(self): self.end = -1 def visitheredoc(self, node, value): self.end = node.pos[1] # find the 'real' end incase we have a heredoc in there ef = _endfinder() ef.visit(parts[-1]) index = max(parts[-1].pos[1], ef.end) + 1 while index < len(s): part = _parser(s[index:], strictmode=strictmode).parse() if not isinstance(part, ast.node): break ast.posshifter(index).visit(part) parts.append(part) ef = _endfinder() ef.visit(parts[-1]) index = max(parts[-1].pos[1], ef.end) + 1 if convertpos: for tree in parts: ast.posconverter(s).visit(tree) return parts
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def split(s): '''a utility function that mimics shlex.split but handles more complex shell constructs such as command substitutions inside words >>> list(split('a b"c"\\'d\\'')) ['a', 'bcd'] >>> list(split('a "b $(c)" $(d) \\'$(e)\\'')) ['a', 'b $(c)', '$(d)', '$(e)'] >>> list(split('a b\\n')) ['a', 'b', '\\n'] ''' p = _parser(s) for t in p.tok: if t.ttype == tokenizer.tokentype.WORD: quoted = bool(t.flags & flags.word.QUOTED) doublequoted = quoted and t.value[0] == '"' parts, expandedword = subst._expandwordinternal(p, t, 0, doublequoted, 0, 0) yield expandedword else: yield s[t.lexpos:t.endlexpos]
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def sleep_and_retry(func): ''' Return a wrapped function that rescues rate limit exceptions, sleeping the current thread until rate limit resets. :param function func: The function to decorate. :return: Decorated function. :rtype: function ''' @wraps(func) def wrapper(*args, **kargs): ''' Call the rate limited function. If the function raises a rate limit exception sleep for the remaing time period and retry the function. :param args: non-keyword variable length argument list to the decorated function. :param kargs: keyworded variable length argument list to the decorated function. ''' while True: try: return func(*args, **kargs) except RateLimitException as exception: time.sleep(exception.period_remaining) return wrapper
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def __period_remaining(self): ''' Return the period remaining for the current rate limit window. :return: The remaing period. :rtype: float ''' elapsed = self.clock() - self.last_reset return self.period - elapsed
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def filter_lines_from_comments(lines): """ Filter the lines from comments and non code lines. """
for line_nb, raw_line in enumerate(lines): clean_line = remove_comments_from_line(raw_line) if clean_line == '': continue yield line_nb, clean_line, raw_line
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _check_no_current_table(new_obj, current_table): """ Raises exception if we try to add a relation or a column with no current table. """
if current_table is None: msg = 'Cannot add {} before adding table' if isinstance(new_obj, Relation): raise NoCurrentTableException(msg.format('relation')) if isinstance(new_obj, Column): raise NoCurrentTableException(msg.format('column'))
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def update_models(new_obj, current_table, tables, relations): """ Update the state of the parsing. """
_update_check_inputs(current_table, tables, relations) _check_no_current_table(new_obj, current_table) if isinstance(new_obj, Table): tables_names = [t.name for t in tables] _check_not_creating_duplicates(new_obj.name, tables_names, 'table', DuplicateTableException) return new_obj, tables + [new_obj], relations if isinstance(new_obj, Relation): tables_names = [t.name for t in tables] _check_colname_in_lst(new_obj.right_col, tables_names) _check_colname_in_lst(new_obj.left_col, tables_names) return current_table, tables, relations + [new_obj] if isinstance(new_obj, Column): columns_names = [c.name for c in current_table.columns] _check_not_creating_duplicates(new_obj.name, columns_names, 'column', DuplicateColumnException) current_table.columns.append(new_obj) return current_table, tables, relations msg = "new_obj cannot be of type {}" raise ValueError(msg.format(new_obj.__class__.__name__))
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def markdown_file_to_intermediary(filename): """ Parse a file and return to intermediary syntax. """
with open(filename) as f: lines = f.readlines() return line_iterator_to_intermediary(lines)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def check_args(args): """Checks that the args are coherent."""
check_args_has_attributes(args) if args.v: non_version_attrs = [v for k, v in args.__dict__.items() if k != 'v'] print('non_version_attrs', non_version_attrs) if len([v for v in non_version_attrs if v is not None]) != 0: fail('Cannot show the version number with another command.') return if args.i is None: fail('Cannot draw ER diagram of no database.') if args.o is None: fail('Cannot draw ER diagram with no output file.')
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def relation_to_intermediary(fk): """Transform an SQLAlchemy ForeignKey object to it's intermediary representation. """
return Relation( right_col=format_name(fk.parent.table.fullname), left_col=format_name(fk._column_tokens[1]), right_cardinality='?', left_cardinality='*', )
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def column_to_intermediary(col, type_formatter=format_type): """Transform an SQLAlchemy Column object to it's intermediary representation. """
return Column( name=col.name, type=type_formatter(col.type), is_key=col.primary_key, )
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def table_to_intermediary(table): """Transform an SQLAlchemy Table object to it's intermediary representation. """
return Table( name=table.fullname, columns=[column_to_intermediary(col) for col in table.c._data.values()] )
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def metadata_to_intermediary(metadata): """ Transforms SQLAlchemy metadata to the intermediary representation. """
tables = [table_to_intermediary(table) for table in metadata.tables.values()] relationships = [relation_to_intermediary(fk) for table in metadata.tables.values() for fk in table.foreign_keys] return tables, relationships
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def name_for_scalar_relationship(base, local_cls, referred_cls, constraint): """ Overriding naming schemes. """
name = referred_cls.__name__.lower() + "_ref" return name
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def intermediary_to_markdown(tables, relationships, output): """ Saves the intermediary representation to markdown. """
er_markup = _intermediary_to_markdown(tables, relationships) with open(output, "w") as file_out: file_out.write(er_markup)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def intermediary_to_dot(tables, relationships, output): """ Save the intermediary representation to dot format. """
dot_file = _intermediary_to_dot(tables, relationships) with open(output, "w") as file_out: file_out.write(dot_file)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def intermediary_to_schema(tables, relationships, output): """ Transforms and save the intermediary representation to the file chosen. """
dot_file = _intermediary_to_dot(tables, relationships) graph = AGraph() graph = graph.from_string(dot_file) extension = output.split('.')[-1] graph.draw(path=output, prog='dot', format=extension)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _intermediary_to_markdown(tables, relationships): """ Returns the er markup source in a string. """
t = '\n'.join(t.to_markdown() for t in tables) r = '\n'.join(r.to_markdown() for r in relationships) return '{}\n{}'.format(t, r)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _intermediary_to_dot(tables, relationships): """ Returns the dot source representing the database in a string. """
t = '\n'.join(t.to_dot() for t in tables) r = '\n'.join(r.to_dot() for r in relationships) return '{}\n{}\n{}\n}}'.format(GRAPH_BEGINNING, t, r)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def all_to_intermediary(filename_or_input, schema=None): """ Dispatch the filename_or_input to the different function to produce the intermediary syntax. All the supported classes names are in `swich_input_class_to_method`. The input can also be a list of strings in markdown format or a filename finishing by '.er' containing markdown format. """
# Try to convert from the name of the class input_class_name = filename_or_input.__class__.__name__ try: this_to_intermediary = switch_input_class_to_method[input_class_name] tables, relationships = this_to_intermediary(filename_or_input) return tables, relationships except KeyError: pass # try to read markdown file. if isinstance(filename_or_input, basestring): if filename_or_input.split('.')[-1] == 'er': return markdown_file_to_intermediary(filename_or_input) # try to read a markdown in a string if not isinstance(filename_or_input, basestring): if all(isinstance(e, basestring) for e in filename_or_input): return line_iterator_to_intermediary(filename_or_input) # try to read DB URI. try: make_url(filename_or_input) return database_to_intermediary(filename_or_input, schema=schema) except ArgumentError: pass msg = 'Cannot process filename_or_input {}'.format(input_class_name) raise ValueError(msg)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_output_mode(output, mode): """ From the output name and the mode returns a the function that will transform the intermediary representation to the output. """
if mode != 'auto': try: return switch_output_mode_auto[mode] except KeyError: raise ValueError('Mode "{}" is not supported.') extension = output.split('.')[-1] try: return switch_output_mode[extension] except KeyError: return intermediary_to_schema
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def handle_oneof(oneof_schema: list) -> tuple: """ Custom handle of `oneOf` JSON schema validator. Tried to match primitive type and see if it should be allowed to be passed multiple timns into a command :param oneof_schema: `oneOf` JSON schema :return: Tuple of :class:`click.ParamType`, ``multiple`` flag and ``description`` of option """
oneof_dict = {schema["type"]: schema for schema in oneof_schema} click_type = None multiple = False description = None for key, value in oneof_dict.items(): if key == "array": continue elif key in SCHEMA_BASE_MAP: if oneof_dict.get("array") and oneof_dict["array"]["items"]["type"] == key: multiple = True # Found a match to a primitive type click_type = SCHEMA_BASE_MAP[key] description = value.get("title") break return click_type, multiple, description
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def clean_data(data: dict) -> dict: """Removes all empty values and converts tuples into lists"""
new_data = {} for key, value in data.items(): # Verify that only explicitly passed args get passed on if not isinstance(value, bool) and not value: continue # Multiple choice command are passed as tuples, convert to list to match schema if isinstance(value, tuple): value = list(value) new_data[key] = value return new_data
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def schema(self) -> dict: """ A property method that'll return the constructed provider schema. Schema MUST be an object and this method must be overridden :return: JSON schema of the provider """
if not self._merged_schema: log.debug("merging required dict into schema for %s", self.name) self._merged_schema = self._schema.copy() self._merged_schema.update(self._required) return self._merged_schema
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _process_data(self, **data) -> dict: """ The main method that process all resources data. Validates schema, gets environs, validates data, prepares it via provider requirements, merges defaults and check for data dependencies :param data: The raw data passed by the notifiers client :return: Processed data """
env_prefix = data.pop("env_prefix", None) environs = self._get_environs(env_prefix) if environs: data = merge_dicts(data, environs) data = self._merge_defaults(data) self._validate_data(data) data = self._validate_data_dependencies(data) data = self._prepare_data(data) return data
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def is_iso8601(instance: str): """Validates ISO8601 format"""
if not isinstance(instance, str): return True return ISO8601.match(instance) is not None
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def is_rfc2822(instance: str): """Validates RFC2822 format"""
if not isinstance(instance, str): return True return email.utils.parsedate(instance) is not None
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def is_valid_port(instance: int): """Validates data is a valid port"""
if not isinstance(instance, (int, str)): return True return int(instance) in range(65535)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def is_timestamp(instance): """Validates data is a timestamp"""
if not isinstance(instance, (int, str)): return True return datetime.fromtimestamp(int(instance))
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def func_factory(p, method: str) -> callable: """ Dynamically generates callback commands to correlate to provider public methods :param p: A :class:`notifiers.core.Provider` object :param method: A string correlating to a provider method :return: A callback func """
def callback(pretty: bool = False): res = getattr(p, method) dump = partial(json.dumps, indent=4) if pretty else partial(json.dumps) click.echo(dump(res)) return callback
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _notify(p, **data): """The callback func that will be hooked to the ``notify`` command"""
message = data.get("message") if not message and not sys.stdin.isatty(): message = click.get_text_stream("stdin").read() data["message"] = message data = clean_data(data) ctx = click.get_current_context() if ctx.obj.get("env_prefix"): data["env_prefix"] = ctx.obj["env_prefix"] rsp = p.notify(**data) rsp.raise_on_errors() click.secho(f"Succesfully sent a notification to {p.name}!", fg="green")
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _resource(resource, pretty: bool = None, **data): """The callback func that will be hooked to the generic resource commands"""
data = clean_data(data) ctx = click.get_current_context() if ctx.obj.get("env_prefix"): data["env_prefix"] = ctx.obj["env_prefix"] rsp = resource(**data) dump = partial(json.dumps, indent=4) if pretty else partial(json.dumps) click.echo(dump(rsp))
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _resources(p): """Callback func to display provider resources"""
if p.resources: click.echo(",".join(p.resources)) else: click.echo(f"Provider '{p.name}' does not have resource helpers")
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def one_or_more( schema: dict, unique_items: bool = True, min: int = 1, max: int = None ) -> dict: """ Helper function to construct a schema that validates items matching `schema` or an array containing items matching `schema`. :param schema: The schema to use :param unique_items: Flag if array items should be unique :param min: Correlates to ``minLength`` attribute of JSON Schema array :param max: Correlates to ``maxLength`` attribute of JSON Schema array """
multi_schema = { "type": "array", "items": schema, "minItems": min, "uniqueItems": unique_items, } if max: multi_schema["maxItems"] = max return {"oneOf": [multi_schema, schema]}
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def text_to_bool(value: str) -> bool: """ Tries to convert a text value to a bool. If unsuccessful returns if value is None or not :param value: Value to check """
try: return bool(strtobool(value)) except (ValueError, AttributeError): return value is not None
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def merge_dicts(target_dict: dict, merge_dict: dict) -> dict: """ Merges ``merge_dict`` into ``target_dict`` if the latter does not already contain a value for each of the key names in ``merge_dict``. Used to cleanly merge default and environ data into notification payload. :param target_dict: The target dict to merge into and return, the user provided data for example :param merge_dict: The data that should be merged into the target data :return: A dict of merged data """
log.debug("merging dict %s into %s", merge_dict, target_dict) for key, value in merge_dict.items(): if key not in target_dict: target_dict[key] = value return target_dict
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def snake_to_camel_case(value: str) -> str: """ Convert a snake case param to CamelCase :param value: The value to convert :return: A CamelCase value """
log.debug("trying to convert %s to camel case", value) return "".join(word.capitalize() for word in value.split("_"))
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def valid_file(path: str) -> bool: """ Verifies that a string path actually exists and is a file :param path: The path to verify :return: **True** if path exist and is a file """
path = Path(path).expanduser() log.debug("checking if %s is a valid file", path) return path.exists() and path.is_file()
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def init_providers(self, provider, kwargs): """ Inits main and fallback provider if relevant :param provider: Provider name to use :param kwargs: Additional kwargs :raises ValueError: If provider name or fallback names are not valid providers, a :exc:`ValueError` will be raised """
self.provider = notifiers.get_notifier(provider, strict=True) if kwargs.get("fallback"): self.fallback = notifiers.get_notifier(kwargs.pop("fallback"), strict=True) self.fallback_defaults = kwargs.pop("fallback_defaults", {})
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def provider_group_factory(): """Dynamically generate provider groups for all providers, and add all basic command to it"""
for provider in all_providers(): p = get_notifier(provider) provider_name = p.name help = f"Options for '{provider_name}'" group = click.Group(name=provider_name, help=help) # Notify command notify = partial(_notify, p=p) group.add_command(schema_to_command(p, "notify", notify, add_message=True)) # Resources command resources_callback = partial(_resources, p=p) resources_cmd = click.Command( "resources", callback=resources_callback, help="Show provider resources list", ) group.add_command(resources_cmd) pretty_opt = click.Option( ["--pretty/--not-pretty"], help="Output a pretty version of the JSON" ) # Add any provider resources for resource in p.resources: rsc = getattr(p, resource) rsrc_callback = partial(_resource, rsc) rsrc_command = schema_to_command( rsc, resource, rsrc_callback, add_message=False ) rsrc_command.params.append(pretty_opt) group.add_command(rsrc_command) for name, description in CORE_COMMANDS.items(): callback = func_factory(p, name) params = [pretty_opt] command = click.Command( name, callback=callback, help=description.format(provider_name), params=params, ) group.add_command(command) notifiers_cli.add_command(group)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def entry_point(): """The entry that CLI is executed from"""
try: provider_group_factory() notifiers_cli(obj={}) except NotifierException as e: click.secho(f"ERROR: {e.message}", bold=True, fg="red") exit(1)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def request( self, method, endpoint=None, headers=None, use_auth=True, _url=None, _kwargs=None, **kwargs): """ Make a request to the Canvas API and return the response. :param method: The HTTP method for the request. :type method: str :param endpoint: The endpoint to call. :type endpoint: str :param headers: Optional HTTP headers to be sent with the request. :type headers: dict :param use_auth: Optional flag to remove the authentication header from the request. :type use_auth: bool :param _url: Optional argument to send a request to a URL outside of the Canvas API. If this is selected and an endpoint is provided, the endpoint will be ignored and only the _url argument will be used. :type _url: str :param _kwargs: A list of 2-tuples representing processed keyword arguments to be sent to Canvas as params or data. :type _kwargs: `list` :rtype: str """
full_url = _url if _url else "{}{}".format(self.base_url, endpoint) if not headers: headers = {} if use_auth: auth_header = {'Authorization': 'Bearer {}'.format(self.access_token)} headers.update(auth_header) # Convert kwargs into list of 2-tuples and combine with _kwargs. _kwargs = _kwargs or [] _kwargs.extend(kwargs.items()) # Do any final argument processing before sending to request method. for i, kwarg in enumerate(_kwargs): kw, arg = kwarg # Convert boolean objects to a lowercase string. if isinstance(arg, bool): _kwargs[i] = (kw, str(arg).lower()) # Convert any datetime objects into ISO 8601 formatted strings. elif isinstance(arg, datetime): _kwargs[i] = (kw, arg.isoformat()) # Determine the appropriate request method. if method == 'GET': req_method = self._get_request elif method == 'POST': req_method = self._post_request elif method == 'DELETE': req_method = self._delete_request elif method == 'PUT': req_method = self._put_request # Call the request method response = req_method(full_url, headers, _kwargs) # Add response to internal cache if len(self._cache) > 4: self._cache.pop() self._cache.insert(0, response) # Raise for status codes if response.status_code == 400: raise BadRequest(response.text) elif response.status_code == 401: if 'WWW-Authenticate' in response.headers: raise InvalidAccessToken(response.json()) else: raise Unauthorized(response.json()) elif response.status_code == 403: raise Forbidden(response.text) elif response.status_code == 404: raise ResourceDoesNotExist('Not Found') elif response.status_code == 409: raise Conflict(response.text) elif response.status_code == 500: raise CanvasException("API encountered an error processing your request") return response
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _get_request(self, url, headers, params=None): """ Issue a GET request to the specified endpoint with the data provided. :param url: str :pararm headers: dict :param params: dict """
return self._session.get(url, headers=headers, params=params)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _post_request(self, url, headers, data=None): """ Issue a POST request to the specified endpoint with the data provided. :param url: str :pararm headers: dict :param data: dict """
# Grab file from data. files = None for field, value in data: if field == 'file': if isinstance(value, dict): files = value else: files = {'file': value} break # Remove file entry from data. data[:] = [tup for tup in data if tup[0] != 'file'] return self._session.post(url, headers=headers, data=data, files=files)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _delete_request(self, url, headers, data=None): """ Issue a DELETE request to the specified endpoint with the data provided. :param url: str :pararm headers: dict :param data: dict """
return self._session.delete(url, headers=headers, data=data)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _put_request(self, url, headers, data=None): """ Issue a PUT request to the specified endpoint with the data provided. :param url: str :pararm headers: dict :param data: dict """
return self._session.put(url, headers=headers, data=data)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def request_upload_token(self, file): """ Request an upload token. :param file: A file handler pointing to the file to upload. :returns: True if the file uploaded successfully, False otherwise, \ and the JSON response from the API. :rtype: tuple """
self.kwargs['name'] = os.path.basename(file.name) self.kwargs['size'] = os.fstat(file.fileno()).st_size response = self._requester.request( 'POST', self.url, _kwargs=combine_kwargs(**self.kwargs) ) return self.upload(response, file)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def upload(self, response, file): """ Upload the file. :param response: The response from the upload request. :type response: dict :param file: A file handler pointing to the file to upload. :returns: True if the file uploaded successfully, False otherwise, \ and the JSON response from the API. :rtype: tuple """
response = response.json() if not response.get('upload_url'): raise ValueError('Bad API response. No upload_url.') if not response.get('upload_params'): raise ValueError('Bad API response. No upload_params.') kwargs = response.get('upload_params') response = self._requester.request( 'POST', use_auth=False, _url=response.get('upload_url'), file=file, _kwargs=combine_kwargs(**kwargs) ) # remove `while(1);` that may appear at the top of a response response_json = json.loads(response.text.lstrip('while(1);')) return ('url' in response_json, response_json)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def set_attributes(self, attributes): """ Load this object with attributes. This method attempts to detect special types based on the field's content and will create an additional attribute of that type. Consider a JSON response with the following fields:: { "name": "New course name", "course_code": "COURSE-001", "start_at": "2012-05-05T00:00:00Z", "end_at": "2012-08-05T23:59:59Z", "sis_course_id": "12345" } The `start_at` and `end_at` fields match a date in ISO8601 format, so two additional datetime attributes are created, `start_at_date` and `end_at_date`. :param attributes: The JSON object to build this object with. :type attributes: dict """
self.attributes = attributes for attribute, value in attributes.items(): self.__setattr__(attribute, value) # datetime field if DATE_PATTERN.match(text_type(value)): naive = datetime.strptime(value, '%Y-%m-%dT%H:%M:%SZ') aware = naive.replace(tzinfo=pytz.utc) self.__setattr__(attribute + '_date', aware)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def create_account(self, **kwargs): """ Create a new root account. :calls: `POST /api/v1/accounts \ <https://canvas.instructure.com/doc/api/accounts.html#method.accounts.create>`_ :rtype: :class:`canvasapi.account.Account` """
response = self.__requester.request( 'POST', 'accounts', _kwargs=combine_kwargs(**kwargs) ) return Account(self.__requester, response.json())
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_account(self, account, use_sis_id=False, **kwargs): """ Retrieve information on an individual account. :calls: `GET /api/v1/accounts/:id \ <https://canvas.instructure.com/doc/api/accounts.html#method.accounts.show>`_ :param account: The object or ID of the account to retrieve. :type account: int, str or :class:`canvasapi.account.Account` :param use_sis_id: Whether or not account_id is an sis ID. Defaults to `False`. :type use_sis_id: bool :rtype: :class:`canvasapi.account.Account` """
if use_sis_id: account_id = account uri_str = 'accounts/sis_account_id:{}' else: account_id = obj_or_id(account, "account", (Account,)) uri_str = 'accounts/{}' response = self.__requester.request( 'GET', uri_str.format(account_id), _kwargs=combine_kwargs(**kwargs) ) return Account(self.__requester, response.json())
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_accounts(self, **kwargs): """ List accounts that the current user can view or manage. Typically, students and teachers will get an empty list in response. Only account admins can view the accounts that they are in. :calls: `GET /api/v1/accounts \ <https://canvas.instructure.com/doc/api/accounts.html#method.accounts.index>`_ :rtype: :class:`canvasapi.paginated_list.PaginatedList` of :class:`canvasapi.account.Account` """
return PaginatedList( Account, self.__requester, 'GET', 'accounts', _kwargs=combine_kwargs(**kwargs) )
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_course(self, course, use_sis_id=False, **kwargs): """ Retrieve a course by its ID. :calls: `GET /api/v1/courses/:id \ <https://canvas.instructure.com/doc/api/courses.html#method.courses.show>`_ :param course: The object or ID of the course to retrieve. :type course: int, str or :class:`canvasapi.course.Course` :param use_sis_id: Whether or not course_id is an sis ID. Defaults to `False`. :type use_sis_id: bool :rtype: :class:`canvasapi.course.Course` """
if use_sis_id: course_id = course uri_str = 'courses/sis_course_id:{}' else: course_id = obj_or_id(course, "course", (Course,)) uri_str = 'courses/{}' response = self.__requester.request( 'GET', uri_str.format(course_id), _kwargs=combine_kwargs(**kwargs) ) return Course(self.__requester, response.json())
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_user(self, user, id_type=None): """ Retrieve a user by their ID. `id_type` denotes which endpoint to try as there are several different IDs that can pull the same user record from Canvas. Refer to API documentation's `User <https://canvas.instructure.com/doc/api/users.html#User>`_ example to see the ID types a user can be retrieved with. :calls: `GET /api/v1/users/:id \ <https://canvas.instructure.com/doc/api/users.html#method.users.api_show>`_ :param user: The user's object or ID. :type user: :class:`canvasapi.user.User` or int :param id_type: The ID type. :type id_type: str :rtype: :class:`canvasapi.user.User` """
if id_type: uri = 'users/{}:{}'.format(id_type, user) elif user == 'self': uri = 'users/self' else: user_id = obj_or_id(user, "user", (User,)) uri = 'users/{}'.format(user_id) response = self.__requester.request( 'GET', uri ) return User(self.__requester, response.json())
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_courses(self, **kwargs): """ Return a list of active courses for the current user. :calls: `GET /api/v1/courses \ <https://canvas.instructure.com/doc/api/courses.html#method.courses.index>`_ :rtype: :class:`canvasapi.paginated_list.PaginatedList` of :class:`canvasapi.course.Course` """
return PaginatedList( Course, self.__requester, 'GET', 'courses', _kwargs=combine_kwargs(**kwargs) )
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_section(self, section, use_sis_id=False, **kwargs): """ Get details about a specific section. :calls: `GET /api/v1/sections/:id \ <https://canvas.instructure.com/doc/api/sections.html#method.sections.show>`_ :param section: The object or ID of the section to get. :type section: :class:`canvasapi.section.Section` or int :param use_sis_id: Whether or not section_id is an sis ID. Defaults to `False`. :type use_sis_id: bool :rtype: :class:`canvasapi.section.Section` """
if use_sis_id: section_id = section uri_str = 'sections/sis_section_id:{}' else: section_id = obj_or_id(section, "section", (Section,)) uri_str = 'sections/{}' response = self.__requester.request( 'GET', uri_str.format(section_id), _kwargs=combine_kwargs(**kwargs) ) return Section(self.__requester, response.json())
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def set_course_nickname(self, course, nickname): """ Set a nickname for the given course. This will replace the course's name in the output of subsequent API calls, as well as in selected places in the Canvas web user interface. :calls: `PUT /api/v1/users/self/course_nicknames/:course_id \ <https://canvas.instructure.com/doc/api/users.html#method.course_nicknames.update>`_ :param course: The ID of the course. :type course: :class:`canvasapi.course.Course` or int :param nickname: The nickname for the course. :type nickname: str :rtype: :class:`canvasapi.course.CourseNickname` """
from canvasapi.course import CourseNickname course_id = obj_or_id(course, "course", (Course,)) response = self.__requester.request( 'PUT', 'users/self/course_nicknames/{}'.format(course_id), nickname=nickname ) return CourseNickname(self.__requester, response.json())
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def search_accounts(self, **kwargs): """ Return a list of up to 5 matching account domains. Partial matches on name and domain are supported. :calls: `GET /api/v1/accounts/search \ <https://canvas.instructure.com/doc/api/account_domain_lookups.html#method.account_domain_lookups.search>`_ :rtype: dict """
response = self.__requester.request( 'GET', 'accounts/search', _kwargs=combine_kwargs(**kwargs) ) return response.json()
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_group(self, group, use_sis_id=False, **kwargs): """ Return the data for a single group. If the caller does not have permission to view the group a 401 will be returned. :calls: `GET /api/v1/groups/:group_id \ <https://canvas.instructure.com/doc/api/groups.html#method.groups.show>`_ :param group: The object or ID of the group to get. :type group: :class:`canvasapi.group.Group` or int :param use_sis_id: Whether or not group_id is an sis ID. Defaults to `False`. :type use_sis_id: bool :rtype: :class:`canvasapi.group.Group` """
if use_sis_id: group_id = group uri_str = 'groups/sis_group_id:{}' else: group_id = obj_or_id(group, "group", (Group,)) uri_str = 'groups/{}' response = self.__requester.request( 'GET', uri_str.format(group_id), _kwargs=combine_kwargs(**kwargs) ) return Group(self.__requester, response.json())
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_group_category(self, category): """ Get a single group category. :calls: `GET /api/v1/group_categories/:group_category_id \ <https://canvas.instructure.com/doc/api/group_categories.html#method.group_categories.show>`_ :param category: The object or ID of the category. :type category: :class:`canvasapi.group.GroupCategory` or int :rtype: :class:`canvasapi.group.GroupCategory` """
category_id = obj_or_id(category, "category", (GroupCategory,)) response = self.__requester.request( 'GET', 'group_categories/{}'.format(category_id) ) return GroupCategory(self.__requester, response.json())
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def create_conversation(self, recipients, body, **kwargs): """ Create a new Conversation. :calls: `POST /api/v1/conversations \ <https://canvas.instructure.com/doc/api/conversations.html#method.conversations.create>`_ :param recipients: An array of recipient ids. These may be user ids or course/group ids prefixed with 'course\\_' or 'group\\_' respectively, e.g. recipients=['1', '2', 'course_3'] :type recipients: `list` of `str` :param body: The body of the message being added. :type body: `str` :rtype: list of :class:`canvasapi.conversation.Conversation` """
from canvasapi.conversation import Conversation kwargs['recipients'] = recipients kwargs['body'] = body response = self.__requester.request( 'POST', 'conversations', _kwargs=combine_kwargs(**kwargs) ) return [Conversation(self.__requester, convo) for convo in response.json()]
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_conversation(self, conversation, **kwargs): """ Return single Conversation :calls: `GET /api/v1/conversations/:id \ <https://canvas.instructure.com/doc/api/conversations.html#method.conversations.show>`_ :param conversation: The object or ID of the conversation. :type conversation: :class:`canvasapi.conversation.Conversation` or int :rtype: :class:`canvasapi.conversation.Conversation` """
from canvasapi.conversation import Conversation conversation_id = obj_or_id(conversation, "conversation", (Conversation,)) response = self.__requester.request( 'GET', 'conversations/{}'.format(conversation_id), _kwargs=combine_kwargs(**kwargs) ) return Conversation(self.__requester, response.json())
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_conversations(self, **kwargs): """ Return list of conversations for the current user, most resent ones first. :calls: `GET /api/v1/conversations \ <https://canvas.instructure.com/doc/api/conversations.html#method.conversations.index>`_ :rtype: :class:`canvasapi.paginated_list.PaginatedList` of \ :class:`canvasapi.conversation.Conversation` """
from canvasapi.conversation import Conversation return PaginatedList( Conversation, self.__requester, 'GET', 'conversations', _kwargs=combine_kwargs(**kwargs) )
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def create_calendar_event(self, calendar_event, **kwargs): """ Create a new Calendar Event. :calls: `POST /api/v1/calendar_events \ <https://canvas.instructure.com/doc/api/calendar_events.html#method.calendar_events_api.create>`_ :param calendar_event: The attributes of the calendar event. :type calendar_event: `dict` :rtype: :class:`canvasapi.calendar_event.CalendarEvent` """
from canvasapi.calendar_event import CalendarEvent if isinstance(calendar_event, dict) and 'context_code' in calendar_event: kwargs['calendar_event'] = calendar_event else: raise RequiredFieldMissing( "Dictionary with key 'context_codes' is required." ) response = self.__requester.request( 'POST', 'calendar_events', _kwargs=combine_kwargs(**kwargs) ) return CalendarEvent(self.__requester, response.json())
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_calendar_events(self, **kwargs): """ List calendar events. :calls: `GET /api/v1/calendar_events \ <https://canvas.instructure.com/doc/api/calendar_events.html#method.calendar_events_api.index>`_ :rtype: :class:`canvasapi.paginated_list.PaginatedList` of :class:`canvasapi.calendar_event.CalendarEvent` """
from canvasapi.calendar_event import CalendarEvent return PaginatedList( CalendarEvent, self.__requester, 'GET', 'calendar_events', _kwargs=combine_kwargs(**kwargs) )
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_appointment_groups(self, **kwargs): """ List appointment groups. :calls: `GET /api/v1/appointment_groups \ <https://canvas.instructure.com/doc/api/appointment_groups.html#method.appointment_groups.index>`_ :rtype: :class:`canvasapi.paginated_list.PaginatedList` of :class:`canvasapi.appointment_group.AppointmentGroup` """
from canvasapi.appointment_group import AppointmentGroup return PaginatedList( AppointmentGroup, self.__requester, 'GET', 'appointment_groups', _kwargs=combine_kwargs(**kwargs) )
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_appointment_group(self, appointment_group): """ Return single Appointment Group by id :calls: `GET /api/v1/appointment_groups/:id \ <https://canvas.instructure.com/doc/api/appointment_groups.html#method.appointment_groups.show>`_ :param appointment_group: The ID of the appointment group. :type appointment_group: :class:`canvasapi.appointment_group.AppointmentGroup` or int :rtype: :class:`canvasapi.appointment_group.AppointmentGroup` """
from canvasapi.appointment_group import AppointmentGroup appointment_group_id = obj_or_id( appointment_group, "appointment_group", (AppointmentGroup,) ) response = self.__requester.request( 'GET', 'appointment_groups/{}'.format(appointment_group_id) ) return AppointmentGroup(self.__requester, response.json())
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def create_appointment_group(self, appointment_group, **kwargs): """ Create a new Appointment Group. :calls: `POST /api/v1/appointment_groups \ <https://canvas.instructure.com/doc/api/appointment_groups.html#method.appointment_groups.create>`_ :param appointment_group: The attributes of the appointment group. :type appointment_group: `dict` :param title: The title of the appointment group. :type title: `str` :rtype: :class:`canvasapi.appointment_group.AppointmentGroup` """
from canvasapi.appointment_group import AppointmentGroup if ( isinstance(appointment_group, dict) and 'context_codes' in appointment_group and 'title' in appointment_group ): kwargs['appointment_group'] = appointment_group elif ( isinstance(appointment_group, dict) and 'context_codes' not in appointment_group ): raise RequiredFieldMissing( "Dictionary with key 'context_codes' is missing." ) elif isinstance(appointment_group, dict) and 'title' not in appointment_group: raise RequiredFieldMissing("Dictionary with key 'title' is missing.") response = self.__requester.request( 'POST', 'appointment_groups', _kwargs=combine_kwargs(**kwargs) ) return AppointmentGroup(self.__requester, response.json())
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_file(self, file, **kwargs): """ Return the standard attachment json object for a file. :calls: `GET /api/v1/files/:id \ <https://canvas.instructure.com/doc/api/files.html#method.files.api_show>`_ :param file: The object or ID of the file to retrieve. :type file: :class:`canvasapi.file.File` or int :rtype: :class:`canvasapi.file.File` """
file_id = obj_or_id(file, "file", (File,)) response = self.__requester.request( 'GET', 'files/{}'.format(file_id), _kwargs=combine_kwargs(**kwargs) ) return File(self.__requester, response.json())
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_folder(self, folder): """ Return the details for a folder :calls: `GET /api/v1/folders/:id \ <https://canvas.instructure.com/doc/api/files.html#method.folders.show>`_ :param folder: The object or ID of the folder to retrieve. :type folder: :class:`canvasapi.folder.Folder` or int :rtype: :class:`canvasapi.folder.Folder` """
folder_id = obj_or_id(folder, "folder", (Folder,)) response = self.__requester.request( 'GET', 'folders/{}'.format(folder_id) ) return Folder(self.__requester, response.json())
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_outcome(self, outcome): """ Returns the details of the outcome with the given id. :calls: `GET /api/v1/outcomes/:id \ <https://canvas.instructure.com/doc/api/outcomes.html#method.outcomes_api.show>`_ :param outcome: The outcome object or ID to return. :type outcome: :class:`canvasapi.outcome.Outcome` or int :returns: An Outcome object. :rtype: :class:`canvasapi.outcome.Outcome` """
from canvasapi.outcome import Outcome outcome_id = obj_or_id(outcome, "outcome", (Outcome,)) response = self.__requester.request( 'GET', 'outcomes/{}'.format(outcome_id) ) return Outcome(self.__requester, response.json())
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_root_outcome_group(self): """ Redirect to root outcome group for context :calls: `GET /api/v1/global/root_outcome_group \ <https://canvas.instructure.com/doc/api/outcome_groups.html#method.outcome_groups_api.redirect>`_ :returns: The OutcomeGroup of the context. :rtype: :class:`canvasapi.outcome.OutcomeGroup` """
from canvasapi.outcome import OutcomeGroup response = self.__requester.request( 'GET', 'global/root_outcome_group' ) return OutcomeGroup(self.__requester, response.json())
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_outcome_group(self, group): """ Returns the details of the Outcome Group with the given id. :calls: `GET /api/v1/global/outcome_groups/:id \ <https://canvas.instructure.com/doc/api/outcome_groups.html#method.outcome_groups_api.show>`_ :param group: The outcome group object or ID to return. :type group: :class:`canvasapi.outcome.OutcomeGroup` or int :returns: An outcome group object. :rtype: :class:`canvasapi.outcome.OutcomeGroup` """
from canvasapi.outcome import OutcomeGroup outcome_group_id = obj_or_id(group, "group", (OutcomeGroup,)) response = self.__requester.request( 'GET', 'global/outcome_groups/{}'.format(outcome_group_id) ) return OutcomeGroup(self.__requester, response.json())
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_progress(self, progress, **kwargs): """ Get a specific progress. :calls: `GET /api/v1/progress/:id <https://canvas.instructure.com/doc/api/progress.html#method.progress.show>`_ :param progress: The object or ID of the progress to retrieve. :type progress: int, str or :class:`canvasapi.progress.Progress` :rtype: :class:`canvasapi.progress.Progress` """
from canvasapi.progress import Progress progress_id = obj_or_id(progress, "progress", (Progress,)) response = self.__requester.request( 'GET', 'progress/{}'.format(progress_id), _kwargs=combine_kwargs(**kwargs) ) return Progress(self.__requester, response.json())
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def is_multivalued(value): """ Determine whether the given value should be treated as a sequence of multiple values when used as a request parameter. In general anything that is iterable is multivalued. For example, `list` and `tuple` instances are multivalued. Generators are multivalued, as are the iterable objects returned by `zip`, `itertools.chain`, etc. However, a simple `int` is single-valued. `str` and `bytes` are special cases: although these are iterable, we treat each as a single value rather than as a sequence of isolated characters or bytes. """
# special cases: iterable, but not multivalued if isinstance(value, (string_types, binary_type)): return False # general rule: multivalued if iterable return isinstance(value, Iterable)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def combine_kwargs(**kwargs): """ Flatten a series of keyword arguments from complex combinations of dictionaries and lists into a list of tuples representing properly-formatted parameters to pass to the Requester object. :param kwargs: A dictionary containing keyword arguments to be flattened into properly-formatted parameters. :type kwargs: dict :returns: A list of tuples that represent flattened kwargs. The first element is a string representing the key. The second element is the value. :rtype: `list` of `tuple` """
combined_kwargs = [] # Loop through all kwargs provided for kw, arg in kwargs.items(): if isinstance(arg, dict): for k, v in arg.items(): for tup in flatten_kwarg(k, v): combined_kwargs.append(('{}{}'.format(kw, tup[0]), tup[1])) elif is_multivalued(arg): for i in arg: for tup in flatten_kwarg('', i): combined_kwargs.append(('{}{}'.format(kw, tup[0]), tup[1])) else: combined_kwargs.append((text_type(kw), arg)) return combined_kwargs
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def flatten_kwarg(key, obj): """ Recursive call to flatten sections of a kwarg to be combined :param key: The partial keyword to add to the full keyword :type key: str :param obj: The object to translate into a kwarg. If the type is `dict`, the key parameter will be added to the keyword between square brackets and recursively call this function. If the type is `list`, or `tuple`, a set of empty brackets will be appended to the keyword and recursively call this function. Otherwise, the function returns with the final keyword and value. :returns: A list of tuples that represent flattened kwargs. The first element is a string representing the key. The second element is the value. :rtype: `list` of `tuple` """
if isinstance(obj, dict): # Add the word (e.g. "[key]") new_list = [] for k, v in obj.items(): for tup in flatten_kwarg(k, v): new_list.append(('[{}]{}'.format(key, tup[0]), tup[1])) return new_list elif is_multivalued(obj): # Add empty brackets (i.e. "[]") new_list = [] for i in obj: for tup in flatten_kwarg(key + '][', i): new_list.append((tup[0], tup[1])) return new_list else: # Base case. Return list with tuple containing the value return [('[{}]'.format(text_type(key)), obj)]