Skip to content

Commit 66ecb06

Browse files
author
Piotr Chromiec
committed
ENH: query price presentation (uses pyfiglet fonts)
1 parent dd53b5c commit 66ecb06

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

pandas_gbq/gbq.py

+32-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import sys
88

99
import numpy as np
10+
from pyfiglet import Figlet
11+
from collections import OrderedDict
12+
1013

1114
from distutils.version import StrictVersion
1215
from pandas import compat, DataFrame, concat
@@ -201,6 +204,21 @@ def __init__(self, project_id, reauth=False, verbose=False,
201204
self.dialect = dialect
202205
self.credentials = self.get_credentials()
203206
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
204222

205223
def get_credentials(self):
206224
if self.private_key:
@@ -421,6 +439,17 @@ def sizeof_fmt(num, suffix='B'):
421439
num /= 1024.0
422440
return fmt % (num, 'Y', suffix)
423441

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+
424453
def get_service(self):
425454
import httplib2
426455
from google_auth_httplib2 import AuthorizedHttp
@@ -543,8 +572,9 @@ def run_query(self, query, **kwargs):
543572
else:
544573
bytes_processed = int(query_reply.get(
545574
'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)))
548578

549579
self._print('Retrieving results...')
550580

0 commit comments

Comments
 (0)