Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kumarjitpathakbangalore/042392958c562ceefc11049ab160a74f to your computer and use it in GitHub Desktop.
Save kumarjitpathakbangalore/042392958c562ceefc11049ab160a74f to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Working with Colab to use google drive to store the output file"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"tf.test.gpu_device_name()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"! pip install psutil\n",
"import psutil"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install -U -q PyDrive\n",
"from pydrive.auth import GoogleAuth\n",
"from pydrive.drive import GoogleDrive\n",
"from google.colab import auth\n",
"from oauth2client.client import GoogleCredentials\n",
"\n",
"auth.authenticate_user()\n",
"gauth = GoogleAuth()\n",
"gauth.credentials = GoogleCredentials.get_application_default()\n",
"drive = GoogleDrive(gauth)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"file1 = drive.CreateFile({'id':'1ZWP7H71JWGjofIZXXXXXXXXX'}) \n",
"file1.GetContentFile('example_file')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"file = open('example_file', 'r')\n",
"content = file.readlines()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"avira= [ x.split() for x in content]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"avira1= [ x[1:-1] for x in avira] # we are applying elimination of first and last element of each row(element) in the list"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def getvalue(x,a):\n",
" if a in x.keys():\n",
" return x[a]\n",
" else:\n",
" return 0\n",
"\n",
"def to_getValue(x):\n",
" return getvalue(x,1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"kp_mean = {}\n",
"kp_var = {}\n",
"\n",
"for i in range(max_col):\n",
" a=list(map(lambda x : getvalue(x,i+1),avira2))\n",
" kp_mean[i+1] = mean(a)\n",
" kp_var[i+1] = variance(a)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create GoogleDriveFile instance with title '.\n",
"file2 = drive.CreateFile({'title': 'kp'}) # note this will create a new file on google drive \n",
"file2.Upload() # Upload the file.\n",
"print('title: %s, id: %s' % (file2['title'], file2['id']))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"file2 = drive.CreateFile({'id':'1xtKWAHQ1gf6Lw9wrAXXXXX'}) \n",
"file2.GetContentFile('kp')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"file = open('kp', 'w')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for k in kp_mean.keys():\n",
" file.write('-1 key:{} mean:{} varience:{} \\n'.format(k,kp_mean[k],kp_var[k]))\n",
"file2.Upload() # uploading or writing on the empty file we created in the google drive\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from google.colab import files"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"files.download('kp') # finally you can download the file from here to your local computer / else go to Drive and download the same"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# If you want to make the google drive work as a local drive follow the below"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Install a Drive FUSE wrapper.\n",
"# https://github.com/astrada/google-drive-ocamlfuse\n",
"!apt-get install -y -qq software-properties-common python-software-properties module-init-tools\n",
"!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null\n",
"!apt-get update -qq 2>&1 > /dev/null\n",
"!apt-get -y install -qq google-drive-ocamlfuse fuse\n",
"\n",
"\n",
"\n",
"# Generate auth tokens for Colab\n",
"from google.colab import auth\n",
"auth.authenticate_user()\n",
"\n",
"\n",
"# Generate creds for the Drive FUSE library.\n",
"from oauth2client.client import GoogleCredentials\n",
"creds = GoogleCredentials.get_application_default()\n",
"import getpass\n",
"!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL\n",
"vcode = getpass.getpass()\n",
"!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}\n",
"\n",
"\n",
"# Create a directory and mount Google Drive using that directory.\n",
"!mkdir -p MyDrive\n",
"!google-drive-ocamlfuse MyDrive\n",
"\n",
"\n",
"!ls MyDrive/"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create a directory and mount Google Drive using that directory.\n",
"!mkdir -p MyDrive\n",
"!google-drive-ocamlfuse MyDrive\n",
"\n",
"\n",
"!ls MyDrive/"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!chmod 777 /content/MyDrive/test_dir_2/ # for changing the directory permission Read write "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!echo \"\" > /content/MyDrive/test_dir_2/test # creating a new file at this location a\n",
"location = '/content/MyDrive/test_dir_2/test'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fo = open(location, \"w\")\n",
"for k in kp_mean.keys():\n",
" fo.write('-1 key:{} mean:{} varience:{} \\n'.format(k,kp_mean[k],kp_var[k]))\n",
"\n",
"fo.close()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment