Skip to content

Instantly share code, notes, and snippets.

@neila
Created March 9, 2021 04:09
Show Gist options
  • Save neila/0184545e7a643ff7c506673b15ac3c2b to your computer and use it in GitHub Desktop.
Save neila/0184545e7a643ff7c506673b15ac3c2b to your computer and use it in GitHub Desktop.
import matplotlib.pyplot as plt
import requests
from ratelimit import limits, sleep_and_retry
class SecAPI(object):
SEC_CALL_LIMIT = {'calls': 10, 'seconds': 1}
@staticmethod
@sleep_and_retry
# Dividing the call limit by half to avoid coming close to the limit
@limits(calls=SEC_CALL_LIMIT['calls'] / 2, period=SEC_CALL_LIMIT['seconds'])
def _call_sec(url):
return requests.get(url)
def get(self, url):
return self._call_sec(url).text
def print_ten_k_data(ten_k_data, fields, field_length_limit=50):
indentation = ' '
print('[')
for ten_k in ten_k_data:
print_statement = '{}{{'.format(indentation)
for field in fields:
value = str(ten_k[field])
# Show return lines in output
if isinstance(value, str):
value_str = '\'{}\''.format(value.replace('\n', '\\n'))
else:
value_str = str(value)
# Cut off the string if it gets too long
if len(value_str) > field_length_limit:
value_str = value_str[:field_length_limit] + '...'
print_statement += '\n{}{}: {}'.format(indentation * 2, field, value_str)
print_statement += '},'
print(print_statement)
print(']')
def plot_similarities(similarities_list, dates, title, labels):
assert len(similarities_list) == len(labels)
plt.figure(1, figsize=(10, 7))
for similarities, label in zip(similarities_list, labels):
plt.title(title)
plt.plot(dates, similarities, label=label)
plt.legend()
plt.xticks(rotation=90)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment