|
7 | 7 | import sys
|
8 | 8 |
|
9 | 9 | import numpy as np
|
| 10 | +from pyfiglet import Figlet |
| 11 | +from collections import OrderedDict |
| 12 | + |
10 | 13 |
|
11 | 14 | from distutils.version import StrictVersion
|
12 | 15 | from pandas import compat, DataFrame, concat
|
@@ -201,6 +204,21 @@ def __init__(self, project_id, reauth=False, verbose=False,
|
201 | 204 | self.dialect = dialect
|
202 | 205 | self.credentials = self.get_credentials()
|
203 | 206 | self.service = self.get_service()
|
| 207 | + # BQ Queries costs $5 per TB. First 1 TB per month is free |
| 208 | + # see here for more: https://cloud.google.com/bigquery/pricing |
| 209 | + self.query_price_for_TB = 5. # USD |
| 210 | + |
| 211 | + # pyfiglet stuff for price output |
| 212 | + self.__figlets = OrderedDict([ |
| 213 | + (0.1, Figlet(font='straight')), |
| 214 | + (0.5, Figlet(font='starwars')), |
| 215 | + (1.0, Figlet(font='blocks')), |
| 216 | + (2.0, Figlet(font='fraktur')), |
| 217 | + (5.0, Figlet(font='doh'))]) |
| 218 | + |
| 219 | + |
| 220 | + for k in self.__figlets: |
| 221 | + self.__figlets[k].width=120 |
204 | 222 |
|
205 | 223 | def get_credentials(self):
|
206 | 224 | if self.private_key:
|
@@ -421,6 +439,17 @@ def sizeof_fmt(num, suffix='B'):
|
421 | 439 | num /= 1024.0
|
422 | 440 | return fmt % (num, 'Y', suffix)
|
423 | 441 |
|
| 442 | + def query_price_for(self, bytes_num): |
| 443 | + figlet = None |
| 444 | + price = bytes_num * self.query_price_for_TB / 2**40 |
| 445 | + |
| 446 | + for (k,v) in self.__figlets.items(): |
| 447 | + if price > k: |
| 448 | + figlet = v |
| 449 | + |
| 450 | + text_price = '$ {0:.2f}'.format(price) |
| 451 | + return figlet and '\n' + figlet.renderText(text_price) or text_price |
| 452 | + |
424 | 453 | def get_service(self):
|
425 | 454 | import httplib2
|
426 | 455 | from google_auth_httplib2 import AuthorizedHttp
|
@@ -543,8 +572,9 @@ def run_query(self, query, **kwargs):
|
543 | 572 | else:
|
544 | 573 | bytes_processed = int(query_reply.get(
|
545 | 574 | 'totalBytesProcessed', '0'))
|
546 |
| - self._print('Query done.\nProcessed: {}\n'.format( |
547 |
| - self.sizeof_fmt(bytes_processed))) |
| 575 | + self._print('Query done.\nProcessed: {}\nPrice: {}'.format( |
| 576 | + self.sizeof_fmt(bytes_processed), |
| 577 | + self.query_price_for(bytes_processed))) |
548 | 578 |
|
549 | 579 | self._print('Retrieving results...')
|
550 | 580 |
|
|
0 commit comments