Last active
May 2, 2018 21:03
-
-
Save aunyks/ba2ac171955317998e08060821ec78a0 to your computer and use it in GitHub Desktop.
Simple utility functions used throughout my research.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import json | |
import requests | |
def unix_to_date(unix_int): | |
if type(unix_int) == type(''): | |
return datetime.datetime.fromtimestamp(int(unix_int)).strftime('%Y-%m-%d %H:%M:%S') | |
elif type(unix_int) == type(0): | |
return datetime.datetime.fromtimestamp(unix_int).strftime('%Y-%m-%d %H:%M:%S') | |
else: | |
raise Exception('Provided Unix time was not of type int or string') | |
def file_to_str(filepath): | |
if type(filepath) == type(''): | |
with open(filepath, 'r') as this_file: | |
return this_file.read() | |
else: | |
raise Exception('Provided filepath not a string') | |
def str_to_file(str, filepath): | |
if type(filepath) == type(''): | |
with open(filepath, "w") as text_file: | |
text_file.write(str) | |
else: | |
raise Exception('Provided filepath not a string') | |
def json_to_dict(str): | |
return json.loads(str) | |
def dict_to_json(dict): | |
return json.dumps(dict) | |
def urlopen(url): | |
return requests.get(url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment