signature
stringlengths
8
3.44k
body
stringlengths
0
1.41M
docstring
stringlengths
1
122k
id
stringlengths
5
17
def monthabbr(self):
return PERSIAN_MONTH_ABBRS[self.month]<EOL>
:rtype: unicode :return: The corresponding persian month abbreviation: [فر, ار, خر, تی, مر, شه, مه, آب, آذ, دی, به, اس]
f9577:c0:m26
def monthabbr_ascii(self):
return PERSIAN_MONTH_ABBRS_ASCII[self.month]<EOL>
:rtype: unicode :return: The corresponding persian month abbreviation in ASCII: [F, O , Kh ... E].
f9577:c0:m27
def monthnameascii(self):
return PERSIAN_MONTH_NAMES_ASCII[self.month]<EOL>
:rtype: unicode :return: The corresponding persian month name in ASCII: [Farvardin - Esfand]
f9577:c0:m28
def localdateformat(self):
return self.strftime('<STR_LIT>')<EOL>
It's equivalent to: .. testsetup:: api-date-localdateformat from __future__ import print_function from khayyam import JalaliDate .. doctest:: api-date-localdateformat >>> print(JalaliDate(1361, 6, 15).strftime('%A %D %B %N')) دوشنبه ۱۵ شهریور ۱۳۶۱ For example: .. doctest:: api-date-localdateformat...
f9577:c0:m29
def firstdayofyear(self):
return JalaliDate(self.year, <NUM_LIT:1>, <NUM_LIT:1>)<EOL>
As it's name says: it's referring to a :py:class:`JalaliDate` representing the first day of current instance's year. :return: First day of corresponding year. :rtype: :py:class:`JalaliDate`
f9577:c0:m30
def dayofyear(self):
return (self - self.firstdayofyear()).days + <NUM_LIT:1><EOL>
:return: Day of year az integer: 1-35[5,6] :rtype: int
f9577:c0:m31
def weekofyear(self, first_day_of_week=SATURDAY):
first_day_of_year = self.firstdayofyear()<EOL>days = (self - first_day_of_year).days<EOL>offset = first_day_of_week - first_day_of_year.weekday()<EOL>if offset < <NUM_LIT:0>:<EOL><INDENT>offset += <NUM_LIT:7><EOL><DEDENT>if days < offset:<EOL><INDENT>return <NUM_LIT:0><EOL><DEDENT>return int((days - offset) / <NUM_LIT:...
weekofyear(first_day_of_week=SATURDAY) :param first_day_of_week: One of the :py:data:`khayyam.SATURDAY`, :py:data:`khayyam.SUNDAY`, :py:data:`khayyam.MONDAY`, :py:data:`khayyam.TUESDAY`, :py:data:`khayyam.WEDNESDAY`, ...
f9577:c0:m32
def __init__(self, status, reason='<STR_LIT>'):
self.status = status<EOL>self.reason = reason<EOL>
Args: :param int status: Integer HTTP response status
f9584:c0:m0
def get_client(project_id=None, credentials=None,<EOL>service_url=None, service_account=None,<EOL>private_key=None, private_key_file=None,<EOL>json_key=None, json_key_file=None,<EOL>readonly=True, swallow_results=True,<EOL>num_retries=<NUM_LIT:0>):
if not credentials:<EOL><INDENT>assert (service_account and (private_key or private_key_file)) or (<EOL>json_key or json_key_file),'<STR_LIT>'<EOL><DEDENT>if not project_id:<EOL><INDENT>assert json_key or json_key_file,'<STR_LIT>'<EOL><DEDENT>if service_url is None:<EOL><INDENT>service_url = DISCOVERY_URI<EOL><DEDENT>s...
Return a singleton instance of BigQueryClient. Either AssertionCredentials or a service account and private key combination need to be provided in order to authenticate requests to BigQuery. Parameters ---------- project_id : str, optional The BigQuery project id, required unless json_key o...
f9586:m0
def get_projects(bq_service):
projects_request = bq_service.projects().list().execute()<EOL>projects = []<EOL>for project in projects_request.get('<STR_LIT>', []):<EOL><INDENT>project_data = {<EOL>'<STR_LIT:id>': project['<STR_LIT:id>'],<EOL>'<STR_LIT:name>': project['<STR_LIT>']<EOL>}<EOL>projects.append(project_data)<EOL><DEDENT>return projects<E...
Given the BigQuery service, return data about all projects.
f9586:m1
def _get_bq_service(credentials=None, service_url=None):
assert credentials, '<STR_LIT>'<EOL>http = credentials.authorize(Http())<EOL>service = build(<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>http=http,<EOL>discoveryServiceUrl=service_url,<EOL>cache_discovery=False<EOL>)<EOL>return service<EOL>
Construct an authorized BigQuery service object.
f9586:m2
def _credentials():
from oauth2client.service_account import ServiceAccountCredentials<EOL>return ServiceAccountCredentials<EOL>
Import and return SignedJwtAssertionCredentials class
f9586:m3
def _get_project_id(self, project_id=None):
if project_id is None:<EOL><INDENT>project_id = self.project_id<EOL><DEDENT>return project_id<EOL>
Get new project_id Default is self.project_id, which is the project client authenticate to. A new project_id is specified when client wants to authenticate to 1 project, but run jobs in a different project. Parameters ---------- project_id : str ...
f9586:c0:m1
def _submit_query_job(self, query_data):
logger.debug('<STR_LIT>' % query_data)<EOL>job_collection = self.bigquery.jobs()<EOL>try:<EOL><INDENT>query_reply = job_collection.query(<EOL>projectId=self.project_id, body=query_data).execute(<EOL>num_retries=self.num_retries)<EOL><DEDENT>except HttpError as e:<EOL><INDENT>if query_data.get("<STR_LIT>", False):<EOL><...
Submit a query job to BigQuery. This is similar to BigQueryClient.query, but gives the user direct access to the query method on the offical BigQuery python client. For fine-grained control over a query job, see: https://google-api-client-libraries.appspot.c...
f9586:c0:m2
def _get_job_reference(self, job_id):
job_reference = {<EOL>"<STR_LIT>": self.project_id,<EOL>"<STR_LIT>": job_id<EOL>}<EOL>return job_reference<EOL>
Get job reference from job_id For more details, see: https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#resource Parameters ---------- job_id: Id of the job Returns ------- job_reference: json of job_reference
f9586:c0:m3
def _insert_job(self, body_object):
logger.debug('<STR_LIT>' % body_object)<EOL>job_collection = self.bigquery.jobs()<EOL>return job_collection.insert(<EOL>projectId=self.project_id,<EOL>body=body_object<EOL>).execute(num_retries=self.num_retries)<EOL>
Submit a job to BigQuery Direct proxy to the insert() method of the offical BigQuery python client. Able to submit load, link, query, copy, or extract jobs. For more details, see: https://google-api-client-libraries.appspot.com/documentation/bigquery/v2/pyt...
f9586:c0:m4
def query(self, query, max_results=None, timeout=<NUM_LIT:0>, dry_run=False, use_legacy_sql=None, external_udf_uris=None):
logger.debug('<STR_LIT>' % query)<EOL>query_data = {<EOL>'<STR_LIT>': query,<EOL>'<STR_LIT>': timeout * <NUM_LIT:1000>,<EOL>'<STR_LIT>': dry_run,<EOL>'<STR_LIT>': max_results<EOL>}<EOL>if use_legacy_sql is not None:<EOL><INDENT>query_data['<STR_LIT>'] = use_legacy_sql<EOL><DEDENT>if external_udf_uris:<EOL><INDENT>query...
Submit a query to BigQuery. Parameters ---------- query : str BigQuery query string max_results : int, optional The maximum number of rows to return per page of results. timeout : float, optional How long to wait for the query to complete, in ...
f9586:c0:m5
def get_query_schema(self, job_id):
query_reply = self.get_query_results(job_id, offset=<NUM_LIT:0>, limit=<NUM_LIT:0>)<EOL>if not query_reply['<STR_LIT>']:<EOL><INDENT>logger.warning('<STR_LIT>' % job_id)<EOL>raise UnfinishedQueryException()<EOL><DEDENT>return query_reply['<STR_LIT>']['<STR_LIT>']<EOL>
Retrieve the schema of a query by job id. Parameters ---------- job_id : str The job_id that references a BigQuery query Returns ------- list A ``list`` of ``dict`` objects that represent the schema.
f9586:c0:m6
def get_table_schema(self, dataset, table, project_id=None):
project_id = self._get_project_id(project_id)<EOL>try: <EOL><INDENT>result = self.bigquery.tables().get(<EOL>projectId=project_id,<EOL>tableId=table,<EOL>datasetId=dataset).execute(num_retries=self.num_retries)<EOL><DEDENT>except HttpError as e:<EOL><INDENT>if int(e.resp['<STR_LIT:status>']) == <NUM_LIT>:<EOL><INDEN...
Return the table schema. Parameters ---------- dataset : str The dataset containing the `table`. table : str The table to get the schema for project_id: str, optional The project of the dataset. Returns ------- list ...
f9586:c0:m7
def check_job(self, job_id):
query_reply = self.get_query_results(job_id, offset=<NUM_LIT:0>, limit=<NUM_LIT:0>)<EOL>return (query_reply.get('<STR_LIT>', False),<EOL>int(query_reply.get('<STR_LIT>', <NUM_LIT:0>)))<EOL>
Return the state and number of results of a query by job id. Parameters ---------- job_id : str The job id of the query to check. Returns ------- tuple (``bool``, ``int``) Whether or not the query has completed and the total number of...
f9586:c0:m8
def get_query_rows(self, job_id, offset=None, limit=None, timeout=<NUM_LIT:0>):
<EOL>query_reply = self.get_query_results(job_id, offset=offset,<EOL>limit=limit, timeout=timeout)<EOL>if not query_reply['<STR_LIT>']:<EOL><INDENT>logger.warning('<STR_LIT>' % job_id)<EOL>raise UnfinishedQueryException()<EOL><DEDENT>schema = query_reply["<STR_LIT>"]["<STR_LIT>"]<EOL>rows = query_reply.get('<STR_LIT>',...
Retrieve a list of rows from a query table by job id. This method will append results from multiple pages together. If you want to manually page through results, you can use `get_query_results` method directly. Parameters ---------- job_id : str The job id th...
f9586:c0:m9
def check_dataset(self, dataset_id, project_id=None):
dataset = self.get_dataset(dataset_id, project_id)<EOL>return bool(dataset)<EOL>
Check to see if a dataset exists. Parameters ---------- dataset_id : str Dataset unique id project_id: str, optional The project the dataset is in Returns ------- bool True if dataset at `dataset_id` exists, else Fasle
f9586:c0:m10
def get_dataset(self, dataset_id, project_id=None):
project_id = self._get_project_id(project_id)<EOL>try: <EOL><INDENT>dataset = self.bigquery.datasets().get(<EOL>projectId=project_id, datasetId=dataset_id).execute(<EOL>num_retries=self.num_retries)<EOL><DEDENT>except HttpError:<EOL><INDENT>dataset = {}<EOL><DEDENT>return dataset<EOL>
Retrieve a dataset if it exists, otherwise return an empty dict. Parameters ---------- dataset_id : str Dataset unique id project_id: str, optional The project the dataset is in Returns ------- dict Contains dataset object if ...
f9586:c0:m11
def check_table(self, dataset, table, project_id=None):
table = self.get_table(dataset, table, project_id)<EOL>return bool(table)<EOL>
Check to see if a table exists. Parameters ---------- dataset : str The dataset to check table : str The name of the table project_id: str, optional The project the table is in Returns ------- bool True if ...
f9586:c0:m12
def get_table(self, dataset, table, project_id=None):
project_id = self._get_project_id(project_id)<EOL>try: <EOL><INDENT>table = self.bigquery.tables().get(<EOL>projectId=project_id, datasetId=dataset,<EOL>tableId=table).execute(num_retries=self.num_retries)<EOL><DEDENT>except HttpError:<EOL><INDENT>table = {}<EOL><DEDENT>return table<EOL>
Retrieve a table if it exists, otherwise return an empty dict. Parameters ---------- dataset : str The dataset that the table is in table : str The name of the table project_id: str, optional The project that the table is in Returns ...
f9586:c0:m13
def create_table(self, dataset, table, schema,<EOL>expiration_time=None, time_partitioning=False,<EOL>project_id=None):
project_id = self._get_project_id(project_id)<EOL>body = {<EOL>'<STR_LIT>': {'<STR_LIT>': schema},<EOL>'<STR_LIT>': {<EOL>'<STR_LIT>': table,<EOL>'<STR_LIT>': project_id,<EOL>'<STR_LIT>': dataset<EOL>}<EOL>}<EOL>if expiration_time is not None:<EOL><INDENT>body['<STR_LIT>'] = expiration_time<EOL><DEDENT>if time_partitio...
Create a new table in the dataset. Parameters ---------- dataset : str The dataset to create the table in table : str The name of the table to create schema : dict The table schema expiration_time : int or double, optional ...
f9586:c0:m14
def update_table(self, dataset, table, schema, project_id=None):
project_id = self._get_project_id(project_id)<EOL>body = {<EOL>'<STR_LIT>': {'<STR_LIT>': schema},<EOL>'<STR_LIT>': {<EOL>'<STR_LIT>': table,<EOL>'<STR_LIT>': project_id,<EOL>'<STR_LIT>': dataset<EOL>}<EOL>}<EOL>try:<EOL><INDENT>result = self.bigquery.tables().update(<EOL>projectId=project_id,<EOL>tableId= table,<EOL>d...
Update an existing table in the dataset. Parameters ---------- dataset : str The dataset to update the table in table : str The name of the table to update schema : dict Table schema project_id: str, optional The project to...
f9586:c0:m15
def patch_table(self, dataset, table, schema, project_id=None):
project_id = self._get_project_id(project_id)<EOL>body = {<EOL>'<STR_LIT>': {'<STR_LIT>': schema},<EOL>'<STR_LIT>': {<EOL>'<STR_LIT>': table,<EOL>'<STR_LIT>': project_id,<EOL>'<STR_LIT>': dataset<EOL>}<EOL>}<EOL>try:<EOL><INDENT>result = self.bigquery.tables().patch(<EOL>projectId=project_id,<EOL>datasetId=dataset,<EOL...
Patch an existing table in the dataset. Parameters ---------- dataset : str The dataset to patch the table in table : str The name of the table to patch schema : dict The table schema project_id: str, optional The project t...
f9586:c0:m16
def create_view(self, dataset, view, query, use_legacy_sql=None, project_id=None):
project_id = self._get_project_id(project_id)<EOL>body = {<EOL>'<STR_LIT>': {<EOL>'<STR_LIT>': view,<EOL>'<STR_LIT>': project_id,<EOL>'<STR_LIT>': dataset<EOL>},<EOL>'<STR_LIT>': {<EOL>'<STR_LIT>': query<EOL>}<EOL>}<EOL>if use_legacy_sql is not None:<EOL><INDENT>body['<STR_LIT>']['<STR_LIT>'] = use_legacy_sql<EOL><DEDE...
Create a new view in the dataset. Parameters ---------- dataset : str The dataset to create the view in view : str The name of the view to create query : dict A query that BigQuery executes when the view is referenced. use_lega...
f9586:c0:m17
def delete_table(self, dataset, table, project_id=None):
project_id = self._get_project_id(project_id)<EOL>try: <EOL><INDENT>response = self.bigquery.tables().delete(<EOL>projectId=project_id,<EOL>datasetId=dataset,<EOL>tableId=table<EOL>).execute(num_retries=self.num_retries)<EOL>if self.swallow_results:<EOL><INDENT>return True<EOL><DEDENT>else:<EOL><INDENT>retur...
Delete a table from the dataset. Parameters ---------- dataset : str The dataset to delete the table from. table : str The name of the table to delete project_id: str, optional String id of the project Returns ------- ...
f9586:c0:m18
def get_tables(self, dataset_id, app_id, start_time, end_time, project_id=None):
if isinstance(start_time, datetime):<EOL><INDENT>start_time = calendar.timegm(start_time.utctimetuple())<EOL><DEDENT>if isinstance(end_time, datetime):<EOL><INDENT>end_time = calendar.timegm(end_time.utctimetuple())<EOL><DEDENT>every_table = self._get_all_tables(dataset_id, project_id)<EOL>app_tables = every_table.get(...
Retrieve a list of tables that are related to the given app id and are inside the range of start and end times. Parameters ---------- dataset_id : str The BigQuery dataset id to consider. app_id : str The appspot name start_time : Union[datetime, ...
f9586:c0:m19
def import_data_from_uris(<EOL>self,<EOL>source_uris,<EOL>dataset,<EOL>table,<EOL>schema=None, <EOL>job=None,<EOL>source_format=None,<EOL>create_disposition=None,<EOL>write_disposition=None,<EOL>encoding=None,<EOL>ignore_unknown_values=None,<EOL>max_bad_records=None,<EOL>allow_jagged_rows=None,<EOL>allow_quo...
source_uris = source_uris if isinstance(source_uris, list)else [source_uris]<EOL>project_id = self._get_project_id(project_id)<EOL>configuration = {<EOL>"<STR_LIT>": {<EOL>"<STR_LIT>": project_id,<EOL>"<STR_LIT>": table,<EOL>"<STR_LIT>": dataset<EOL>},<EOL>"<STR_LIT>": source_uris,<EOL>}<EOL>if max_bad_records:<EOL><IN...
Imports data into a BigQuery table from cloud storage. Optional arguments that are not specified are determined by BigQuery as described: https://developers.google.com/bigquery/docs/reference/v2/jobs Parameters ---------- source_urls : list A ``list`` of ``str`` objects representing the urls on cloud stora...
f9586:c0:m20
def export_data_to_uris(<EOL>self,<EOL>destination_uris,<EOL>dataset,<EOL>table, <EOL>job=None,<EOL>compression=None,<EOL>destination_format=None,<EOL>print_header=None,<EOL>field_delimiter=None,<EOL>project_id=None,<EOL>):
destination_uris = destination_urisif isinstance(destination_uris, list) else [destination_uris]<EOL>project_id = self._get_project_id(project_id)<EOL>configuration = {<EOL>"<STR_LIT>": {<EOL>"<STR_LIT>": project_id,<EOL>"<STR_LIT>": table,<EOL>"<STR_LIT>": dataset<EOL>},<EOL>"<STR_LIT>": destination_uris,<EOL>}<EOL>if...
Export data from a BigQuery table to cloud storage. Optional arguments that are not specified are determined by BigQuery as described: https://developers.google.com/bigquery/docs/reference/v2/jobs Parameters ---------- destination_uris : Union[str, list] ``str`` or ``list`` of ``str`` objects representing the URIs...
f9586:c0:m21
def write_to_table(<EOL>self,<EOL>query,<EOL>dataset=None,<EOL>table=None, <EOL>external_udf_uris=None,<EOL>allow_large_results=None,<EOL>use_query_cache=None,<EOL>priority=None,<EOL>create_disposition=None,<EOL>write_disposition=None,<EOL>use_legacy_sql=None,<EOL>maximum_billing_tier=None,<EOL>flatten=None,...
configuration = {<EOL>"<STR_LIT>": query,<EOL>}<EOL>project_id = self._get_project_id(project_id)<EOL>if dataset and table:<EOL><INDENT>configuration['<STR_LIT>'] = {<EOL>"<STR_LIT>": project_id,<EOL>"<STR_LIT>": table,<EOL>"<STR_LIT>": dataset<EOL>}<EOL><DEDENT>if allow_large_results is not None:<EOL><INDENT>configura...
Write query result to table. If dataset or table is not provided, Bigquery will write the result to temporary table. Optional arguments that are not specified are determined by BigQuery as described: https://developers.google.com/bigquery/docs/reference/v2/jobs Parameters ---------- query : str BigQuery query stri...
f9586:c0:m22
def wait_for_job(self, job, interval=<NUM_LIT:5>, timeout=<NUM_LIT>):
complete = False<EOL>job_id = str(job if isinstance(job,<EOL>(six.binary_type, six.text_type, int))<EOL>else job['<STR_LIT>']['<STR_LIT>'])<EOL>job_resource = None<EOL>start_time = time()<EOL>elapsed_time = <NUM_LIT:0><EOL>while not (complete or elapsed_time > timeout):<EOL><INDENT>sleep(interval)<EOL>request = self.bi...
Waits until the job indicated by job_resource is done or has failed Parameters ---------- job : Union[dict, str] ``dict`` representing a BigQuery job resource, or a ``str`` representing the BigQuery job id interval : float, optional Polling interval in seconds, default = 5 timeout : float, optional Tim...
f9586:c0:m23
def push_rows(self, dataset, table, rows, insert_id_key=None,<EOL>skip_invalid_rows=None, ignore_unknown_values=None,<EOL>template_suffix=None, project_id=None):
project_id = self._get_project_id(project_id)<EOL>table_data = self.bigquery.tabledata()<EOL>rows_data = []<EOL>for row in rows:<EOL><INDENT>each_row = {}<EOL>each_row["<STR_LIT>"] = row<EOL>if insert_id_key is not None:<EOL><INDENT>keys = insert_id_key.split('<STR_LIT:.>')<EOL>val = reduce(lambda d, key: d.get(key) if...
Upload rows to BigQuery table. Parameters ---------- dataset : str The dataset to upload to table : str The name of the table to insert rows into rows : list A ``list`` of rows (``dict`` objects) to add to the table insert_id_k...
f9586:c0:m24
def get_all_tables(self, dataset_id, project_id=None):
tables_data = self._get_all_tables_for_dataset(dataset_id, project_id)<EOL>tables = []<EOL>for table in tables_data.get('<STR_LIT>', []):<EOL><INDENT>table_name = table.get('<STR_LIT>', {}).get('<STR_LIT>')<EOL>if table_name:<EOL><INDENT>tables.append(table_name)<EOL><DEDENT><DEDENT>return tables<EOL>
Retrieve a list of tables for the dataset. Parameters ---------- dataset_id : str The dataset to retrieve table data for. project_id: str Unique ``str`` identifying the BigQuery project contains the dataset Returns ------- A ``list`` with...
f9586:c0:m25
def _get_all_tables(self, dataset_id, cache=False, project_id=None):
do_fetch = True<EOL>if cache and self.cache.get(dataset_id):<EOL><INDENT>time, result = self.cache.get(dataset_id)<EOL>if datetime.now() - time < CACHE_TIMEOUT:<EOL><INDENT>do_fetch = False<EOL><DEDENT><DEDENT>if do_fetch:<EOL><INDENT>result = self._get_all_tables_for_dataset(dataset_id, project_id)<EOL>self.cache[data...
Retrieve the list of tables for dataset, that respect the formats: * appid_YYYY_MM * YYYY_MM_appid Parameters ---------- dataset_id : str The dataset to retrieve table names for cache : bool, optional To use cached value or not (de...
f9586:c0:m26
def _get_all_tables_for_dataset(self, dataset_id, project_id=None):
project_id = self._get_project_id(project_id)<EOL>result = self.bigquery.tables().list(<EOL>projectId=project_id,<EOL>datasetId=dataset_id).execute(num_retries=self.num_retries)<EOL>page_token = result.get('<STR_LIT>')<EOL>while page_token:<EOL><INDENT>res = self.bigquery.tables().list(<EOL>projectId=project_id,<EOL>da...
Retrieve a list of all tables for the dataset. Parameters ---------- dataset_id : str The dataset to retrieve table names for project_id: str Unique ``str`` identifying the BigQuery project contains the dataset Returns ------- dict ...
f9586:c0:m27
def _parse_table_list_response(self, list_response):
tables = defaultdict(dict)<EOL>for table in list_response.get('<STR_LIT>', []):<EOL><INDENT>table_ref = table.get('<STR_LIT>')<EOL>if not table_ref:<EOL><INDENT>continue<EOL><DEDENT>table_id = table_ref.get('<STR_LIT>', '<STR_LIT>')<EOL>year_month, app_id = self._parse_table_name(table_id)<EOL>if not year_month:<EOL><I...
Parse the response received from calling list on tables. Parameters ---------- list_response The response found by calling list on a BigQuery table object. Returns ------- dict Dates referenced by table names
f9586:c0:m28
def _parse_table_name(self, table_id):
<EOL>attributes = table_id.split('<STR_LIT:_>')<EOL>year_month = "<STR_LIT:->".join(attributes[:<NUM_LIT:2>])<EOL>app_id = "<STR_LIT:->".join(attributes[<NUM_LIT:2>:])<EOL>if year_month.count("<STR_LIT:->") == <NUM_LIT:1> and all(<EOL>[num.isdigit() for num in year_month.split('<STR_LIT:->')]):<EOL><INDENT>return year_...
Parse a table name in the form of appid_YYYY_MM or YYYY_MM_appid and return a tuple consisting of YYYY-MM and the app id. Returns (None, None) in the event of a name like <desc>_YYYYMMDD_<int> Parameters ---------- table_id : str The table id as listed by BigQuery ...
f9586:c0:m29
def _filter_tables_by_time(self, tables, start_time, end_time):
return [table_name for (table_name, unix_seconds) in tables.items()<EOL>if self._in_range(start_time, end_time, unix_seconds)]<EOL>
Filter a table dictionary and return table names based on the range of start and end times in unix seconds. Parameters ---------- tables : dict Dates referenced by table names start_time : int The unix time after which records will be fetched end_...
f9586:c0:m30
def _in_range(self, start_time, end_time, time):
ONE_MONTH = <NUM_LIT> <EOL>return start_time <= time <= end_time ortime <= start_time <= time + ONE_MONTH ortime <= end_time <= time + ONE_MONTH<EOL>
Indicate if the given time falls inside of the given range. Parameters ---------- start_time : int The unix time for the start of the range end_time : int The unix time for the end of the range time : int The unix time to check Return...
f9586:c0:m31
def get_query_results(self, job_id, offset=None, limit=None,<EOL>page_token=None, timeout=<NUM_LIT:0>):
job_collection = self.bigquery.jobs()<EOL>return job_collection.getQueryResults(<EOL>projectId=self.project_id,<EOL>jobId=job_id,<EOL>startIndex=offset,<EOL>maxResults=limit,<EOL>pageToken=page_token,<EOL>timeoutMs=timeout * <NUM_LIT:1000>).execute(num_retries=self.num_retries)<EOL>
Execute the query job indicated by the given job id. This is direct mapping to bigquery api https://cloud.google.com/bigquery/docs/reference/v2/jobs/getQueryResults Parameters ---------- job_id : str The job id of the query to check offset : optional ...
f9586:c0:m32
def _transform_row(self, row, schema):
log = {}<EOL>for index, col_dict in enumerate(schema):<EOL><INDENT>col_name = col_dict['<STR_LIT:name>']<EOL>row_value = row['<STR_LIT:f>'][index]['<STR_LIT:v>']<EOL>if row_value is None:<EOL><INDENT>log[col_name] = None<EOL>continue<EOL><DEDENT>if col_dict['<STR_LIT:type>'] == '<STR_LIT>':<EOL><INDENT>row_value = self...
Apply the given schema to the given BigQuery data row. Parameters ---------- row A single BigQuery row to transform schema : list The BigQuery table schema to apply to the row, specifically the list of field dicts. Returns ------- ...
f9586:c0:m33
def _recurse_on_row(self, col_dict, nested_value):
row_value = None<EOL>if col_dict['<STR_LIT>'] == '<STR_LIT>' and isinstance(nested_value, list):<EOL><INDENT>row_value = [self._transform_row(record['<STR_LIT:v>'], col_dict['<STR_LIT>'])<EOL>for record in nested_value]<EOL><DEDENT>else:<EOL><INDENT>row_value = self._transform_row(nested_value, col_dict['<STR_LIT>'])<E...
Apply the schema specified by the given dict to the nested value by recursing on it. Parameters ---------- col_dict : dict The schema to apply to the nested value. nested_value : A value nested in a BigQuery row. Returns ------- Union[dict, l...
f9586:c0:m34
def _generate_hex_for_uris(self, uris):
return sha256(("<STR_LIT::>".join(uris) + str(time())).encode()).hexdigest()<EOL>
Given uris, generate and return hex version of it Parameters ---------- uris : list Containing all uris Returns ------- str Hexed uris
f9586:c0:m35
def create_dataset(self, dataset_id, friendly_name=None, description=None,<EOL>access=None, location=None, project_id=None):
project_id = self._get_project_id(project_id)<EOL>try: <EOL><INDENT>datasets = self.bigquery.datasets()<EOL>dataset_data = self.dataset_resource(dataset_id, <EOL>project_id=project_id,<EOL>friendly_name=friendly_name,<EOL>description=description,<EOL>access=acc...
Create a new BigQuery dataset. Parameters ---------- dataset_id : str Unique ``str`` identifying the dataset with the project (the referenceID of the dataset, not the integer id of the dataset) friendly_name: str, optional A human readable nam...
f9586:c0:m38
def get_datasets(self, project_id=None):
project_id = self._get_project_id(project_id)<EOL>try: <EOL><INDENT>datasets = self.bigquery.datasets()<EOL>request = datasets.list(projectId=project_id)<EOL>result = request.execute(num_retries=self.num_retries)<EOL>return result.get('<STR_LIT>', [])<EOL><DEDENT>except HttpError as e:<EOL><INDENT>logger.err...
List all datasets in the project. Parameters ---------- project_id: str Unique ``str`` identifying the BigQuery project contains the dataset Returns ------- list Dataset resources
f9586:c0:m39
def delete_dataset(self, dataset_id, delete_contents=False, project_id=None):
project_id = self._get_project_id(project_id)<EOL>try: <EOL><INDENT>datasets = self.bigquery.datasets()<EOL>request = datasets.delete(projectId=project_id,<EOL>datasetId=dataset_id,<EOL>deleteContents=delete_contents)<EOL>response = request.execute(num_retries=self.num_retries)<EOL>if self.swallow_results:<E...
Delete a BigQuery dataset. Parameters ---------- dataset_id : str Unique ``str`` identifying the dataset with the project (the referenceId of the dataset) Unique ``str`` identifying the BigQuery project contains the dataset delete_contents : b...
f9586:c0:m40
def update_dataset(self, dataset_id, friendly_name=None, description=None,<EOL>access=None, project_id=None):
project_id = self._get_project_id(project_id)<EOL>try: <EOL><INDENT>datasets = self.bigquery.datasets()<EOL>body = self.dataset_resource(dataset_id, <EOL>friendly_name=friendly_name,<EOL>description=description, <EOL>access=access,<EOL>project_id=project_id)<EOL>request = datasets.update(projectId=project_id...
Updates information in an existing dataset. The update method replaces the entire dataset resource, whereas the patch method only replaces fields that are provided in the submitted dataset resource. Parameters ---------- dataset_id : str Unique ``str`` identifying th...
f9586:c0:m41
def patch_dataset(self, dataset_id, friendly_name=None, description=None,<EOL>access=None, project_id=None):
project_id = self._get_project_id(project_id)<EOL>try: <EOL><INDENT>datasets = self.bigquery.datasets()<EOL>body = self.dataset_resource(dataset_id, <EOL>friendly_name=friendly_name,<EOL>description=description, <EOL>access=access,<EOL>project_id=project_id)<EOL>request = datasets.patch(projectId=project_id,...
Updates information in an existing dataset. The update method replaces the entire dataset resource, whereas the patch method only replaces fields that are provided in the submitted dataset resource. Parameters ---------- dataset_id : str Unique string idenfitying the...
f9586:c0:m42
def dataset_resource(self, ref_id, friendly_name=None, description=None,<EOL>access=None, location=None, project_id=None):
project_id = self._get_project_id(project_id)<EOL>data = {<EOL>"<STR_LIT>": {<EOL>"<STR_LIT>": ref_id,<EOL>"<STR_LIT>": project_id<EOL>}<EOL>}<EOL>if friendly_name:<EOL><INDENT>data["<STR_LIT>"] = friendly_name<EOL><DEDENT>if description:<EOL><INDENT>data["<STR_LIT:description>"] = description<EOL><DEDENT>if access:<EO...
See https://developers.google.com/bigquery/docs/reference/v2/datasets#resource Parameters ---------- ref_id : str Dataset id (the reference id, not the integer id) friendly_name : str, optional An optional descriptive name for the dataset ...
f9586:c0:m43
@classmethod<EOL><INDENT>def schema_from_record(cls, record):<DEDENT>
from bigquery.schema_builder import schema_from_record<EOL>return schema_from_record(record)<EOL>
Given a dict representing a record instance to be inserted into BigQuery, calculate the schema. Parameters ---------- record : dict representing a record to be inserted into big query, where all keys are ``str`` objects (representing column names in t...
f9586:c0:m44
def render_query(dataset, tables, select=None, conditions=None,<EOL>groupings=None, having=None, order_by=None, limit=None):
if None in (dataset, tables):<EOL><INDENT>return None<EOL><DEDENT>query = "<STR_LIT>" % (<EOL>_render_select(select),<EOL>_render_sources(dataset, tables),<EOL>_render_conditions(conditions),<EOL>_render_groupings(groupings),<EOL>_render_having(having),<EOL>_render_order(order_by),<EOL>_render_limit(limit)<EOL>)<EOL>re...
Render a query that will run over the given tables using the specified parameters. Parameters ---------- dataset : str The BigQuery dataset to query data from tables : Union[dict, list] The table in `dataset` to query. select : dict, optional The keys function as column ...
f9587:m0
def _render_select(selections):
if not selections:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>rendered_selections = []<EOL>for name, options in selections.items():<EOL><INDENT>if not isinstance(options, list):<EOL><INDENT>options = [options]<EOL><DEDENT>original_name = name<EOL>for options_dict in options:<EOL><INDENT>name = original_name<EOL>alias =...
Render the selection part of a query. Parameters ---------- selections : dict Selections for a table Returns ------- str A string for the "select" part of a query See Also -------- render_query : Further clarification of `selections` dict formatting
f9587:m1
def _format_select(formatter, name):
for caster in formatter.split('<STR_LIT:->'):<EOL><INDENT>if caster == '<STR_LIT>':<EOL><INDENT>name = "<STR_LIT>" % name<EOL><DEDENT>elif '<STR_LIT::>' in caster:<EOL><INDENT>caster, args = caster.split('<STR_LIT::>')<EOL>name = "<STR_LIT>" % (caster, name, args)<EOL><DEDENT>else:<EOL><INDENT>name = "<STR_LIT>" % (cas...
Modify the query selector by applying any formatters to it. Parameters ---------- formatter : str Hyphen-delimited formatter string where formatters are applied inside-out, e.g. the formatter string SEC_TO_MICRO-INTEGER-FORMAT_UTC_USEC applied to the selector foo would result in...
f9587:m2
def _render_sources(dataset, tables):
if isinstance(tables, dict):<EOL><INDENT>if tables.get('<STR_LIT>', False):<EOL><INDENT>try:<EOL><INDENT>dataset_table = '<STR_LIT:.>'.join([dataset, tables['<STR_LIT>']])<EOL>return "<STR_LIT>""<STR_LIT>".format(dataset_table,<EOL>tables['<STR_LIT>'],<EOL>tables['<STR_LIT>'])<EOL><DEDENT>except KeyError as exp:<EOL><I...
Render the source part of a query. Parameters ---------- dataset : str The data set to fetch log data from. tables : Union[dict, list] The tables to fetch log data from Returns ------- str A string that represents the "from" part of a query.
f9587:m3
def _render_conditions(conditions):
if not conditions:<EOL><INDENT>return "<STR_LIT>"<EOL><DEDENT>rendered_conditions = []<EOL>for condition in conditions:<EOL><INDENT>field = condition.get('<STR_LIT>')<EOL>field_type = condition.get('<STR_LIT:type>')<EOL>comparators = condition.get('<STR_LIT>')<EOL>if None in (field, field_type, comparators) or not comp...
Render the conditions part of a query. Parameters ---------- conditions : list A list of dictionary items to filter a table. Returns ------- str A string that represents the "where" part of a query See Also -------- render_query : Further clarification of `conditio...
f9587:m4
def _render_condition(field, field_type, comparators):
field_type = field_type.upper()<EOL>negated_conditions, normal_conditions = [], []<EOL>for comparator in comparators:<EOL><INDENT>condition = comparator.get("<STR_LIT>").upper()<EOL>negated = "<STR_LIT>" if comparator.get("<STR_LIT>") else "<STR_LIT>"<EOL>value = comparator.get("<STR_LIT:value>")<EOL>if condition == "<...
Render a single query condition. Parameters ---------- field : str The field the condition applies to field_type : str The data type of the field. comparators : array_like An iterable of logic operators to use. Returns ------- str a condition string.
f9587:m5
def _render_condition_value(value, field_type):
<EOL>if field_type == "<STR_LIT>":<EOL><INDENT>value = <NUM_LIT:1> if value else <NUM_LIT:0><EOL><DEDENT>elif field_type in ("<STR_LIT>", "<STR_LIT>", "<STR_LIT>"):<EOL><INDENT>value = "<STR_LIT>" % (value)<EOL><DEDENT>elif field_type in ("<STR_LIT>"):<EOL><INDENT>value = "<STR_LIT>" % (str(value))<EOL><DEDENT>return "...
Render a query condition value. Parameters ---------- value : Union[bool, int, float, str, datetime] The value of the condition field_type : str The data type of the field Returns ------- str A value string.
f9587:m6
def _render_groupings(fields):
if not fields:<EOL><INDENT>return "<STR_LIT>"<EOL><DEDENT>return "<STR_LIT>" + "<STR_LIT:U+002CU+0020>".join(fields)<EOL>
Render the group by part of a query. Parameters ---------- fields : list A list of fields to group by. Returns ------- str A string that represents the "group by" part of a query.
f9587:m7
def _render_having(having_conditions):
if not having_conditions:<EOL><INDENT>return "<STR_LIT>"<EOL><DEDENT>rendered_conditions = []<EOL>for condition in having_conditions:<EOL><INDENT>field = condition.get('<STR_LIT>')<EOL>field_type = condition.get('<STR_LIT:type>')<EOL>comparators = condition.get('<STR_LIT>')<EOL>if None in (field, field_type, comparator...
Render the having part of a query. Parameters ---------- having_conditions : list A ``list`` of ``dict``s to filter the rows Returns ------- str A string that represents the "having" part of a query. See Also -------- render_query : Further clarification of `condit...
f9587:m8
def _render_order(order):
if not order or '<STR_LIT>' not in order or '<STR_LIT>' not in order:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>return "<STR_LIT>" % ("<STR_LIT:U+002CU+0020>".join(order['<STR_LIT>']), order['<STR_LIT>'])<EOL>
Render the order by part of a query. Parameters ---------- order : dict A dictionary with two keys, fields and direction. Such that the dictionary should be formatted as {'fields': ['TimeStamp'], 'direction':'desc'}. Returns ------- str A string that represents ...
f9587:m9
def _render_limit(limit):
if not limit:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>return "<STR_LIT>" % limit<EOL>
Render the limit part of a query. Parameters ---------- limit : int, optional Limit the amount of data needed to be returned. Returns ------- str A string that represents the "limit" part of a query.
f9587:m10
def schema_from_record(record, timestamp_parser=default_timestamp_parser):
return [describe_field(k, v, timestamp_parser=timestamp_parser)<EOL>for k, v in list(record.items())]<EOL>
Generate a BigQuery schema given an example of a record that is to be inserted into BigQuery. Parameters ---------- record : dict Example of a record that is to be inserted into BigQuery timestamp_parser : function, optional Unary function taking a ``str`` and returning and ``bool``...
f9589:m1
def describe_field(k, v, timestamp_parser=default_timestamp_parser):
def bq_schema_field(name, bq_type, mode):<EOL><INDENT>return {"<STR_LIT:name>": name, "<STR_LIT:type>": bq_type, "<STR_LIT>": mode}<EOL><DEDENT>if isinstance(v, list):<EOL><INDENT>if len(v) == <NUM_LIT:0>:<EOL><INDENT>raise Exception(<EOL>"<STR_LIT>".format(k))<EOL><DEDENT>v = v[<NUM_LIT:0>]<EOL>mode = "<STR_LIT>"<EOL>...
Given a key representing a column name and value representing the value stored in the column, return a representation of the BigQuery schema element describing that field. Raise errors if invalid value types are provided. Parameters ---------- k : Union[str, unicode] Key representing th...
f9589:m2
def bigquery_type(o, timestamp_parser=default_timestamp_parser):
t = type(o)<EOL>if t in six.integer_types:<EOL><INDENT>return "<STR_LIT>"<EOL><DEDENT>elif (t == six.binary_type and six.PY2) or t == six.text_type:<EOL><INDENT>if timestamp_parser and timestamp_parser(o):<EOL><INDENT>return "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>return "<STR_LIT:string>"<EOL><DEDENT><DEDENT>elif t ...
Given a value, return the matching BigQuery type of that value. Must be one of str/unicode/int/float/datetime/record, where record is a dict containing value which have matching BigQuery types. Parameters ---------- o : object A Python object time_stamp_parser : function, optional ...
f9589:m3
def confirm(text, default=True):
if default:<EOL><INDENT>legend = "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>legend = "<STR_LIT>"<EOL><DEDENT>res = "<STR_LIT>"<EOL>while (res != "<STR_LIT:y>") and (res != "<STR_LIT:n>"):<EOL><INDENT>res = input(text + "<STR_LIT>".format(legend)).lower()<EOL>if not res and default:<EOL><INDENT>res = "<STR_LIT:y>"<EOL><D...
Console confirmation dialog based on raw_input.
f9593:m0
def ensure_dir(d):
if not os.path.exists(d):<EOL><INDENT>os.makedirs(d)<EOL><DEDENT>
Check does directory exist, and create an empty one if not.
f9593:m1
def read_file(fname):
res = []<EOL>try:<EOL><INDENT>with open(fname, '<STR_LIT:r>') as f:<EOL><INDENT>for line in f:<EOL><INDENT>line = line.rstrip('<STR_LIT:\n>').rstrip('<STR_LIT:\r>')<EOL>if line and (line[<NUM_LIT:0>] != '<STR_LIT:#>'):<EOL><INDENT>regexline = "<STR_LIT>" + re.sub("<STR_LIT>", "<STR_LIT>", line) + "<STR_LIT>"<EOL>res.ap...
Read file, convert wildcards into regular expressions, skip empty lines and comments.
f9593:m2
def drop_it(title, filters, blacklist):
title = title.lower()<EOL>matched = False<EOL>for f in filters:<EOL><INDENT>if re.match(f, title):<EOL><INDENT>matched = True<EOL><DEDENT><DEDENT>if not matched:<EOL><INDENT>return True<EOL><DEDENT>for b in blacklist:<EOL><INDENT>if re.match(b, title):<EOL><INDENT>return True<EOL><DEDENT><DEDENT>return False<EOL>
The found torrents should be in filters list and shouldn't be in blacklist.
f9593:m3
def do_list():
dirs = os.walk(CONFIG_ROOT).next()[<NUM_LIT:1>]<EOL>if dirs:<EOL><INDENT>print("<STR_LIT>")<EOL>for d in dirs:<EOL><INDENT>print("<STR_LIT>".format(d))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT>
CLI action "list configurations".
f9593:m4
def do_create(config, config_dir):
if os.path.exists(config_dir):<EOL><INDENT>print("<STR_LIT>".format(config))<EOL>exit(<NUM_LIT:1>)<EOL><DEDENT>os.makedirs(config_dir)<EOL>print("<STR_LIT>")<EOL>url = input("<STR_LIT>")<EOL>torrent_dir = input("<STR_LIT>".format(DEFAULT_TORRRENT_DIR)) or DEFAULT_TORRRENT_DIR<EOL>update_interval = input("<STR_LIT>".for...
CLI action "create new configuration".
f9593:m5
def do_update(config, config_dir):
if not os.path.exists(config_dir):<EOL><INDENT>print("<STR_LIT>".format(config))<EOL>exit(<NUM_LIT:1>)<EOL><DEDENT>config_file = os.path.join(config_dir, '<STR_LIT>')<EOL>with open(config_file, '<STR_LIT:r>') as f:<EOL><INDENT>old_config_data = json.load(f)<EOL><DEDENT>old_url = old_config_data['<STR_LIT:url>']<EOL>old...
CLI action "update new configuration".
f9593:m6
def do_remove(config, config_dir):
if not os.path.exists(config_dir):<EOL><INDENT>print("<STR_LIT>".format(config))<EOL>exit(<NUM_LIT:1>)<EOL><DEDENT>if confirm("<STR_LIT>".format(config)):<EOL><INDENT>shutil.rmtree(config_dir)<EOL>print("<STR_LIT>".format(config))<EOL><DEDENT>else:<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT>
CLI action "remove configuration".
f9593:m7
def do_exec(config, config_dir):
if not os.path.exists(config_dir):<EOL><INDENT>print("<STR_LIT>".format(config))<EOL>exit(<NUM_LIT:1>)<EOL><DEDENT>print("<STR_LIT>".format(config))<EOL>config_file = os.path.join(config_dir, '<STR_LIT>')<EOL>with open(config_file, '<STR_LIT:r>') as f:<EOL><INDENT>config_data = json.load(f)<EOL><DEDENT>url = config_dat...
CLI action "process the feed from specified configuration".
f9593:m8
def do_filter(config, config_dir):
if not os.path.exists(config_dir):<EOL><INDENT>print("<STR_LIT>".format(config))<EOL>exit(<NUM_LIT:1>) <EOL><DEDENT>editor = os.environ["<STR_LIT>"]<EOL>config_filter = os.path.join(config_dir, '<STR_LIT>')<EOL>call([editor, config_filter])<EOL>print("<STR_LIT>")<EOL>
CLI action "run editor for filters list".
f9593:m9
def do_blacklist(config, config_dir):
if not os.path.exists(config_dir):<EOL><INDENT>print("<STR_LIT>".format(config))<EOL>exit(<NUM_LIT:1>) <EOL><DEDENT>editor = os.environ["<STR_LIT>"]<EOL>config_blacklist = os.path.join(config_dir, '<STR_LIT>')<EOL>call([editor, config_blacklist])<EOL>print("<STR_LIT>")<EOL>
CLI action "run editor for blacklist".
f9593:m10
def action(act, config):
if not config:<EOL><INDENT>pass<EOL><DEDENT>elif act is "<STR_LIT:list>":<EOL><INDENT>do_list()<EOL><DEDENT>else:<EOL><INDENT>config_dir = os.path.join(CONFIG_ROOT, config)<EOL>globals()["<STR_LIT>" + act](config, config_dir)<EOL><DEDENT>
CLI action preprocessor
f9593:m11
def register(opener_function):
openers.append(opener_function)<EOL>return opener_function<EOL>
Decorator that adds decorated opener function to the list of openers. :param opener_function: Opener function. :return: Opener function.
f9595:m0
def filehandles(path, openers_list=openers, pattern='<STR_LIT>', verbose=False):
if not verbose:<EOL><INDENT>logging.disable(logging.VERBOSE)<EOL><DEDENT>for opener in openers_list:<EOL><INDENT>try:<EOL><INDENT>for filehandle in opener(path=path, pattern=pattern, verbose=verbose):<EOL><INDENT>with closing(filehandle):<EOL><INDENT>yield filehandle<EOL><DEDENT><DEDENT>break <EOL><DEDENT>except (zipf...
Main function that iterates over list of openers and decides which opener to use. :param str path: Path. :param list openers_list: List of openers. :param str pattern: Regular expression pattern. :param verbose: Print additional information. :type verbose: :py:obj:`True` or :py:obj:`False` :ret...
f9595:m1
@register<EOL>def directory_opener(path, pattern='<STR_LIT>', verbose=False):
if not os.path.isdir(path):<EOL><INDENT>raise NotADirectoryError<EOL><DEDENT>else:<EOL><INDENT>openers_list = [opener for opener in openers if not opener.__name__.startswith('<STR_LIT>')] <EOL>for root, dirlist, filelist in os.walk(path):<EOL><INDENT>for filename in filelist:<EOL><INDENT>if pattern and not re.match(pa...
Directory opener. :param str path: Path. :param str pattern: Regular expression pattern. :return: Filehandle(s).
f9595:m2
@register<EOL>def ziparchive_opener(path, pattern='<STR_LIT>', verbose=False):
with zipfile.ZipFile(io.BytesIO(urlopen(path).read()), '<STR_LIT:r>') if is_url(path) else zipfile.ZipFile(path, '<STR_LIT:r>') as ziparchive:<EOL><INDENT>for zipinfo in ziparchive.infolist():<EOL><INDENT>if not zipinfo.filename.endswith('<STR_LIT:/>'):<EOL><INDENT>source = os.path.join(path, zipinfo.filename)<EOL>if p...
Opener that opens files from zip archive.. :param str path: Path. :param str pattern: Regular expression pattern. :return: Filehandle(s).
f9595:m3
@register<EOL>def tararchive_opener(path, pattern='<STR_LIT>', verbose=False):
with tarfile.open(fileobj=io.BytesIO(urlopen(path).read())) if is_url(path) else tarfile.open(path) as tararchive:<EOL><INDENT>for tarinfo in tararchive:<EOL><INDENT>if tarinfo.isfile():<EOL><INDENT>source = os.path.join(path, tarinfo.name)<EOL>if pattern and not re.match(pattern, tarinfo.name):<EOL><INDENT>logger.verb...
Opener that opens files from tar archive. :param str path: Path. :param str pattern: Regular expression pattern. :return: Filehandle(s).
f9595:m4
@register<EOL>def gzip_opener(path, pattern='<STR_LIT>', verbose=False):
source = path if is_url(path) else os.path.abspath(path)<EOL>filename = os.path.basename(path)<EOL>if pattern and not re.match(pattern, filename):<EOL><INDENT>logger.verbose('<STR_LIT>'.format(os.path.abspath(filename), pattern))<EOL>return<EOL><DEDENT>try:<EOL><INDENT>filehandle = gzip.GzipFile(fileobj=io.BytesIO(urlo...
Opener that opens single gzip compressed file. :param str path: Path. :param str pattern: Regular expression pattern. :return: Filehandle(s).
f9595:m5
@register<EOL>def bz2_opener(path, pattern='<STR_LIT>', verbose=False):
source = path if is_url(path) else os.path.abspath(path)<EOL>filename = os.path.basename(path)<EOL>if pattern and not re.match(pattern, filename):<EOL><INDENT>logger.verbose('<STR_LIT>'.format(os.path.abspath(path), pattern))<EOL>return<EOL><DEDENT>try:<EOL><INDENT>filehandle = bz2.open(io.BytesIO(urlopen(path).read())...
Opener that opens single bz2 compressed file. :param str path: Path. :param str pattern: Regular expression pattern. :return: Filehandle(s).
f9595:m6
@register<EOL>def text_opener(path, pattern='<STR_LIT>', verbose=False):
source = path if is_url(path) else os.path.abspath(path)<EOL>filename = os.path.basename(path)<EOL>if pattern and not re.match(pattern, filename):<EOL><INDENT>logger.verbose('<STR_LIT>'.format(os.path.abspath(path), pattern))<EOL>return<EOL><DEDENT>filehandle = urlopen(path) if is_url(path) else open(path)<EOL>logger.v...
Opener that opens single text file. :param str path: Path. :param str pattern: Regular expression pattern. :return: Filehandle(s).
f9595:m7
def is_url(path):
try:<EOL><INDENT>parse_result = urlparse(path)<EOL>return all((parse_result.scheme, parse_result.netloc, parse_result.path))<EOL><DEDENT>except ValueError:<EOL><INDENT>return False<EOL><DEDENT>
Test if path represents a valid URL string. :param str path: Path to file. :return: True if path is valid url string, False otherwise. :rtype: :py:obj:`True` or :py:obj:`False`
f9595:m8
@register.inclusion_tag('<STR_LIT>')<EOL>def render_honeypot_field(field_name=None):
if not field_name:<EOL><INDENT>field_name = settings.HONEYPOT_FIELD_NAME<EOL><DEDENT>value = getattr(settings, '<STR_LIT>', '<STR_LIT>')<EOL>if callable(value):<EOL><INDENT>value = value()<EOL><DEDENT>return {'<STR_LIT>': field_name, '<STR_LIT:value>': value}<EOL>
Renders honeypot field named field_name (defaults to HONEYPOT_FIELD_NAME).
f9598:m0
def honeypot_equals(val):
expected = getattr(settings, '<STR_LIT>', '<STR_LIT>')<EOL>if callable(expected):<EOL><INDENT>expected = expected()<EOL><DEDENT>return val == expected<EOL>
Default verifier used if HONEYPOT_VERIFIER is not specified. Ensures val == HONEYPOT_VALUE or HONEYPOT_VALUE() if it's a callable.
f9602:m0
def verify_honeypot_value(request, field_name):
verifier = getattr(settings, '<STR_LIT>', honeypot_equals)<EOL>if request.method == '<STR_LIT:POST>':<EOL><INDENT>field = field_name or settings.HONEYPOT_FIELD_NAME<EOL>if field not in request.POST or not verifier(request.POST[field]):<EOL><INDENT>resp = render_to_string('<STR_LIT>',<EOL>{'<STR_LIT>': field})<EOL>retur...
Verify that request.POST[field_name] is a valid honeypot. Ensures that the field exists and passes verification according to HONEYPOT_VERIFIER.
f9602:m1
def check_honeypot(func=None, field_name=None):
<EOL>if isinstance(func, six.string_types):<EOL><INDENT>func, field_name = field_name, func<EOL><DEDENT>def decorated(func):<EOL><INDENT>def inner(request, *args, **kwargs):<EOL><INDENT>response = verify_honeypot_value(request, field_name)<EOL>if response:<EOL><INDENT>return response<EOL><DEDENT>else:<EOL><INDENT>retur...
Check request.POST for valid honeypot field. Takes an optional field_name that defaults to HONEYPOT_FIELD_NAME if not specified.
f9602:m2
def honeypot_exempt(view_func):
<EOL>def wrapped(*args, **kwargs):<EOL><INDENT>return view_func(*args, **kwargs)<EOL><DEDENT>wrapped.honeypot_exempt = True<EOL>return wraps(view_func, assigned=available_attrs(view_func))(wrapped)<EOL>
Mark view as exempt from honeypot validation
f9602:m3
def u2handlers(self):
return []<EOL>
Override suds HTTP Transport as it does not properly honor local system configuration for proxy settings Derived from https://gist.github.com/rbarrois/3721801
f9605:c0:m0
def __init__(self, wsdl=None, api_key=None, timeout=<NUM_LIT:5>):
if not wsdl:<EOL><INDENT>wsdl = os.environ['<STR_LIT>']<EOL><DEDENT>if not api_key:<EOL><INDENT>api_key = os.environ['<STR_LIT>']<EOL><DEDENT>self._soap_client = Client(wsdl, transport=WellBehavedHttpTransport())<EOL>self._soap_client.set_options(timeout=timeout)<EOL>token3 = Element('<STR_LIT>', ns=DARWIN_WEBSERVICE_N...
Constructor Keyword arguments: wsdl -- the URL of the Darwin LDB WSDL document. Will fall back to using the DARWIN_WEBSERVICE_WSDL environment variable if not supplied api_key -- a valid API key for the Darwin LDB webservice. Will fall back to the DARWIN_WEBSERVICE_API_KEY if not supplied timeout -- a timeout in secon...
f9605:c1:m0
def get_station_board(<EOL>self,<EOL>crs,<EOL>rows=<NUM_LIT>,<EOL>include_departures=True,<EOL>include_arrivals=False,<EOL>destination_crs=None,<EOL>origin_crs=None<EOL>):
<EOL>if include_departures and include_arrivals:<EOL><INDENT>query_type = '<STR_LIT>'<EOL><DEDENT>elif include_departures:<EOL><INDENT>query_type = '<STR_LIT>'<EOL><DEDENT>elif include_arrivals:<EOL><INDENT>query_type = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>q = pa...
Query the darwin webservice to obtain a board for a particular station and return a StationBoard instance Positional arguments: crs -- the three letter CRS code of a UK station Keyword arguments: rows -- the number of rows to retrieve (default 10) include_departures -- include departing services in the departure boar...
f9605:c1:m2
def get_service_details(self, service_id):
service_query =self._soap_client.service['<STR_LIT>']['<STR_LIT>']<EOL>try:<EOL><INDENT>soap_response = service_query(serviceID=service_id)<EOL><DEDENT>except WebFault:<EOL><INDENT>raise WebServiceError<EOL><DEDENT>return ServiceDetails(soap_response)<EOL>
Get the details of an individual service and return a ServiceDetails instance. Positional arguments: service_id: A Darwin LDB service id
f9605:c1:m3
@property<EOL><INDENT>def generated_at(self):<DEDENT>
return self._generated_at<EOL>
The time at which the station board was generated.
f9605:c3:m1
@property<EOL><INDENT>def crs(self):<DEDENT>
return self._crs<EOL>
The CRS code for the station.
f9605:c3:m2