Skip to content

Instantly share code, notes, and snippets.

@aunyks
Last active May 2, 2018 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aunyks/ba2ac171955317998e08060821ec78a0 to your computer and use it in GitHub Desktop.
Save aunyks/ba2ac171955317998e08060821ec78a0 to your computer and use it in GitHub Desktop.
Simple utility functions used throughout my research.
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