Last active
July 18, 2018 03:37
-
-
Save BlogBlocks/bb549704359b7d70ab504f424ca559af to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## Post To Gist --- PostToGist.py AND Post in SQLite " | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 32, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "https://gist.github.com/5d86ae97f513a505db5c28320dd178b1 \n", | |
| "9 # USAGE example:\n", | |
| "# import UnCamel\n", | |
| "# x = \"TimeToUncamelCaseTextUsingFunctions\"\n", | |
| "# UnCamel.uncamel(x)\n", | |
| "def uncamel(x): \n", | |
| " return reduce(lambda a,b: a + ((b.upper() == b and (len(a) and a[-1].upper() != a[-1])) \n", | |
| " and (' ' + b) or b), x, '')\n", | |
| "def uncam(x):\n", | |
| " import re\n", | |
| " return re.sub(\"([a-z])([A-Z])\",\"\\g<1> \\g<2>\",x) https://gist.github.com/76663365e1c77ad6ba19c25009001c03 UnCamel.py\n", | |
| "https://gist.github.com/5d86ae97f513a505db5c28320dd178b1 \n", | |
| "10 Index https://gist.github.com/76663365e1c77ad6ba19c25009001c03 UnCamel.py\n", | |
| "https://gist.github.com/5d86ae97f513a505db5c28320dd178b1 \n", | |
| "11 {\n", | |
| " \"cells\": [\n", | |
| " {\n", | |
| " \"cell_type\": \"markdown\",\n", | |
| " \"metadata\": {},\n", | |
| " \"source\": [\n", | |
| " \"## Post To Gist --- PostToGist.py AND Post in SQLite \"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 13,\n", | |
| " \"metadata\": {},\n", | |
| " \"outputs\": [\n", | |
| " {\n", | |
| " \"name\": \"stdout\",\n", | |
| " \"output_type\": \"stream\",\n", | |
| " \"text\": [\n", | |
| " \"https://gist.github.com/76663365e1c77ad6ba19c25009001c03 \\n\",\n", | |
| " \"9 # USAGE example:\\n\",\n", | |
| " \"# import UnCamel\\n\",\n", | |
| " \"# x = \\\"TimeToUncamelCaseTextUsingFunctions\\\"\\n\",\n", | |
| " \"# UnCamel.uncamel(x)\\n\",\n", | |
| " \"def uncamel(x): \\n\",\n", | |
| " \" return reduce(lambda a,b: a + ((b.upper() == b and (len(a) and a[-1].upper() != a[-1])) \\n\",\n", | |
| " \" and (' ' + b) or b), x, '')\\n\",\n", | |
| " \"def uncam(x):\\n\",\n", | |
| " \" import re\\n\",\n", | |
| " \" return re.sub(\\\"([a-z])([A-Z])\\\",\\\"\\\\g<1> \\\\g<2>\\\",x) https://gist.github.com/76663365e1c77ad6ba19c25009001c03 UnCamel.py\\n\",\n", | |
| " \"https://gist.github.com/76663365e1c77ad6ba19c25009001c03 \\n\",\n", | |
| " \"10 Index https://gist.github.com/76663365e1c77ad6ba19c25009001c03 UnCamel.py\\n\"\n", | |
| " ]\n", | |
| " }\n", | |
| " ],\n", | |
| " \"source\": [\n", | |
| " \"#%%writefile GISTstore/post2gist.py\\n\",\n", | |
| " \"import os\\n\",\n", | |
| " \"from time import sleep\\n\",\n", | |
| " \"import sqlite3\\n\",\n", | |
| " \"import os, requests, sys, json\\n\",\n", | |
| " \"import GISTkey\\n\",\n", | |
| " \"username=GISTkey.gistkey()[0]\\n\",\n", | |
| " \"password=GISTkey.gistkey()[1]\\n\",\n", | |
| " \"database =\\\"GISTstore/gist.db\\\"\\n\",\n", | |
| " \"conn = sqlite3.connect(database)\\n\",\n", | |
| " \"c = conn.cursor()\\n\",\n", | |
| " \"c.execute(\\\"\\\"\\\"\\n\",\n", | |
| " \"CREATE VIRTUAL TABLE IF NOT EXISTS gist \\n\",\n", | |
| " \"USING FTS4(content, description, filename);\\n\",\n", | |
| " \"\\\"\\\"\\\")\\n\",\n", | |
| " \"c.close()\\n\",\n", | |
| " \"conn.close()\\n\",\n", | |
| " \"conn = sqlite3.connect(database)\\n\",\n", | |
| " \"conn.text_factory=str \\n\",\n", | |
| " \"c = conn.cursor()\\n\",\n", | |
| " \"if not os.path.exists('GISTstore'):\\n\",\n", | |
| " \" os.makedirs('GISTstore') \\n\",\n", | |
| " \"PATH = \\\"GISTstore/\\\"\\n\",\n", | |
| " \"filename = \\\"UnCamel.py\\\"\\n\",\n", | |
| " \"content=open(PATH+filename, 'r').read()\\n\",\n", | |
| " \"post = 'https://api.github.com/gists'\\n\",\n", | |
| " \"r = requests.post(post,json.dumps({'files':{filename:{\\\"content\\\":content}}}),\\n\",\n", | |
| " \" auth=requests.auth.HTTPBasicAuth(username, password)) \\n\",\n", | |
| " \"sleep(2)\\n\",\n", | |
| " \"Url= (r.json()['html_url'])\\n\",\n", | |
| " \"c.execute(\\\"INSERT INTO gist VALUES (?,?,?)\\\", (content, Url, filename)) \\n\",\n", | |
| " \"conn.commit()\\n\",\n", | |
| " \"title = \\\"Index\\\"\\n\",\n", | |
| " \"c.execute(\\\"INSERT INTO gist VALUES (?,?,?)\\\", (title, Url, filename)) \\n\",\n", | |
| " \"conn.commit()\\n\",\n", | |
| " \"for row in c.execute('SELECT rowid, content, description, filename FROM gist \\\\\\n\",\n", | |
| " \" WHERE filename MATCH ?', (filename,)):\\n\",\n", | |
| " \" print Url,\\\"\\\\n\\\",row[0],row[1],row[2],row[3]\\n\",\n", | |
| " \"c.close()\\n\",\n", | |
| " \"conn.close()\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"markdown\",\n", | |
| " \"metadata\": {},\n", | |
| " \"source\": [\n", | |
| " \"# SQLite database\\n\",\n", | |
| " \"## View Gists published to https://gist.github.com\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 30,\n", | |
| " \"metadata\": {},\n", | |
| " \"outputs\": [\n", | |
| " {\n", | |
| " \"name\": \"stdout\",\n", | |
| " \"output_type\": \"stream\",\n", | |
| " \"text\": [\n", | |
| " \"Overwriting GISTstore/GistIndex.py\\n\"\n", | |
| " ]\n", | |
| " }\n", | |
| " ],\n", | |
| " \"source\": [\n", | |
| " \"%%writefile GISTstore/GistIndex.py\\n\",\n", | |
| " \"import sqlite3\\n\",\n", | |
| " \"def Index():\\n\",\n", | |
| " \" database =\\\"GISTstore/gist.db\\\"\\n\",\n", | |
| " \" conn = sqlite3.connect(database)\\n\",\n", | |
| " \" c = conn.cursor()\\n\",\n", | |
| " \" for row in c.execute('SELECT rowid, content, description, filename FROM gist \\\\\\n\",\n", | |
| " \" WHERE content = ?', (\\\"Index\\\",)):\\n\",\n", | |
| " \" print row[0],row[1],row[2],row[3]\\n\",\n", | |
| " \" \\n\",\n", | |
| " \"\\n\",\n", | |
| " \"if __name__ == \\\"__main__\\\":\\n\",\n", | |
| " \" Index() \"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 31,\n", | |
| " \"metadata\": {},\n", | |
| " \"outputs\": [\n", | |
| " {\n", | |
| " \"name\": \"stdout\",\n", | |
| " \"output_type\": \"stream\",\n", | |
| " \"text\": [\n", | |
| " \"2 Index https://gist.github.com/f67b8ecf4589b531f408d798a4e650eb post2gist.py\\r\\n\",\n", | |
| " \"4 Index https://gist.github.com/d640c0100e5535b608e2fe0f4e8fe925 GISTkey.py\\r\\n\",\n", | |
| " \"6 Index https://gist.github.com/71cd9394db91fd089765861ebf121b0e SimpleGistPost.py\\r\\n\",\n", | |
| " \"8 Index https://gist.github.com/8426999e17ef19b9ea7e2a27a3223773 Pmp3.py\\r\\n\",\n", | |
| " \"10 Index https://gist.github.com/76663365e1c77ad6ba19c25009001c03 UnCamel.py\\r\\n\"\n", | |
| " ]\n", | |
| " }\n", | |
| " ],\n", | |
| " \"source\": [\n", | |
| " \"!python GISTstore/GistIndex.py\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 24,\n", | |
| " \"metadata\": {},\n", | |
| " \"outputs\": [\n", | |
| " {\n", | |
| " \"name\": \"stdout\",\n", | |
| " \"output_type\": \"stream\",\n", | |
| " \"text\": [\n", | |
| " \"Writing PostToGist.py\\n\"\n", | |
| " ]\n", | |
| " }\n", | |
| " ],\n", | |
| " \"source\": [\n", | |
| " \"%%writefile PostToGist.py\\n\",\n", | |
| " \"#!/usr/bin/python\\n\",\n", | |
| " \"import os, requests, sys, json\\n\",\n", | |
| " \"import GISTkey\\n\",\n", | |
| " \"username=GISTkey.gist()[0]\\n\",\n", | |
| " \"password=GISTkey.gist()[1]\\n\",\n", | |
| " \"filename = os.path.basename(sys.argv[1])\\n\",\n", | |
| " \"content=open(filename, 'r').read()\\n\",\n", | |
| " \"r = requests.post('https://api.github.com/gists',json.dumps({'files':{filename:{\\\"content\\\":content}}}),auth=requests.auth.HTTPBasicAuth(username, password)) \\n\",\n", | |
| " \"print(r.json()['html_url'])\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 35,\n", | |
| " \"metadata\": {\n", | |
| " \"collapsed\": true\n", | |
| " },\n", | |
| " \"outputs\": [],\n", | |
| " \"source\": [\n", | |
| " \"!mkdir GISTstore\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 53,\n", | |
| " \"metadata\": {},\n", | |
| " \"outputs\": [\n", | |
| " {\n", | |
| " \"name\": \"stdout\",\n", | |
| " \"output_type\": \"stream\",\n", | |
| " \"text\": [\n", | |
| " \"Writing GISTstore/__init__.py\\n\"\n", | |
| " ]\n", | |
| " }\n", | |
| " ],\n", | |
| " \"source\": [\n", | |
| " \"%%writefile GISTstore/__init__.py\\n\",\n", | |
| " \"print \\\"Ver 1.06\\\"\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 43,\n", | |
| " \"metadata\": {},\n", | |
| " \"outputs\": [\n", | |
| " {\n", | |
| " \"name\": \"stdout\",\n", | |
| " \"output_type\": \"stream\",\n", | |
| " \"text\": [\n", | |
| " \"Overwriting GISTstore/GISTkey.py\\n\"\n", | |
| " ]\n", | |
| " }\n", | |
| " ],\n", | |
| " \"source\": [\n", | |
| " \"%%writefile GISTstore/GISTkey.py\\n\",\n", | |
| " \"# USE:\\n\",\n", | |
| " \"#import GISTkey\\n\",\n", | |
| " \"#username=GISTkey.gist()[0]\\n\",\n", | |
| " \"#password=GISTkey.gist()[1]\\n\",\n", | |
| " \"def gist():\\n\",\n", | |
| " \" username='username'\\n\",\n", | |
| " \" password=\\\"password\\\"\\n\",\n", | |
| " \" GistKEY = (username, password)\\n\",\n", | |
| " \" return GistKEY\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 57,\n", | |
| " \"metadata\": {},\n", | |
| " \"outputs\": [\n", | |
| " {\n", | |
| " \"name\": \"stdout\",\n", | |
| " \"output_type\": \"stream\",\n", | |
| " \"text\": [\n", | |
| " \"username password\\n\"\n", | |
| " ]\n", | |
| " }\n", | |
| " ],\n", | |
| " \"source\": [\n", | |
| " \"from GISTstore import GISTkey\\n\",\n", | |
| " \"username=GISTkey.gistkey()[0]\\n\",\n", | |
| " \"password=GISTkey.gistkey()[1]\\n\",\n", | |
| " \"print username,password\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 51,\n", | |
| " \"metadata\": {},\n", | |
| " \"outputs\": [\n", | |
| " {\n", | |
| " \"name\": \"stdout\",\n", | |
| " \"output_type\": \"stream\",\n", | |
| " \"text\": [\n", | |
| " \"Overwriting GISTstore/GISTkey.py\\n\"\n", | |
| " ]\n", | |
| " }\n", | |
| " ],\n", | |
| " \"source\": [\n", | |
| " \"%%writefile GISTstore/GISTkey.py\\n\",\n", | |
| " \"# USE:\\n\",\n", | |
| " \"#import GISTkey\\n\",\n", | |
| " \"#username=GISTkey.gistkey()[0]\\n\",\n", | |
| " \"#password=GISTkey.gistkey()[1]\\n\",\n", | |
| " \"def gistkey():\\n\",\n", | |
| " \" username='username'\\n\",\n", | |
| " \" password=\\\"password\\\"\\n\",\n", | |
| " \" GistAPI = (username, password)\\n\",\n", | |
| " \" return GistAPI\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 66,\n", | |
| " \"metadata\": {},\n", | |
| " \"outputs\": [\n", | |
| " {\n", | |
| " \"name\": \"stdout\",\n", | |
| " \"output_type\": \"stream\",\n", | |
| " \"text\": [\n", | |
| " \"Overwriting GISTstore/SimpleGistPost.py\\n\"\n", | |
| " ]\n", | |
| " }\n", | |
| " ],\n", | |
| " \"source\": [\n", | |
| " \"%%writefile GISTstore/SimpleGistPost.py\\n\",\n", | |
| " \"#!/usr/bin/python\\n\",\n", | |
| " \"#USE: python SimpleGistPost.py FileToPost.py\\n\",\n", | |
| " \"import os, requests, sys, json\\n\",\n", | |
| " \"import GISTkey\\n\",\n", | |
| " \"from time import sleep\\n\",\n", | |
| " \"username=GISTkey.gistkey()[0]\\n\",\n", | |
| " \"password=GISTkey.gistkey()[1]\\n\",\n", | |
| " \"filename = os.path.basename(sys.argv[1])\\n\",\n", | |
| " \"content=open(filename, 'r').read()\\n\",\n", | |
| " \"r = requests.post('https://api.github.com/gists',json.dumps({'files':{filename:{\\\"content\\\":content}}}),auth=requests.auth.HTTPBasicAuth(username, password)) \\n\",\n", | |
| " \"sleep(2)\\n\",\n", | |
| " \"print(r.json()['html_url'])\\n\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 68,\n", | |
| " \"metadata\": {},\n", | |
| " \"outputs\": [\n", | |
| " {\n", | |
| " \"name\": \"stdout\",\n", | |
| " \"output_type\": \"stream\",\n", | |
| " \"text\": [\n", | |
| " \"gist.db GISTkey.pyc __init__.pyc SimpleGistPost.py\\r\\n\",\n", | |
| " \"GISTkey.py __init__.py post2gist.py\\r\\n\"\n", | |
| " ]\n", | |
| " }\n", | |
| " ],\n", | |
| " \"source\": [\n", | |
| " \"!ls GISTstore\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 31,\n", | |
| " \"metadata\": {},\n", | |
| " \"outputs\": [\n", | |
| " {\n", | |
| " \"name\": \"stdout\",\n", | |
| " \"output_type\": \"stream\",\n", | |
| " \"text\": [\n", | |
| " \"blogblocks Shair2u2\\n\"\n", | |
| " ]\n", | |
| " }\n", | |
| " ],\n", | |
| " \"source\": [\n", | |
| " \"import GISTkey\\n\",\n", | |
| " \"username=GISTkey.gistkey()[0]\\n\",\n", | |
| " \"password=GISTkey.gistkey()[1]\\n\",\n", | |
| " \"print username, password\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 49,\n", | |
| " \"metadata\": {\n", | |
| " \"collapsed\": true\n", | |
| " },\n", | |
| " \"outputs\": [],\n", | |
| " \"source\": [\n", | |
| " \"!rm GISTstore/gist.db\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"markdown\",\n", | |
| " \"metadata\": {},\n", | |
| " \"source\": [\n", | |
| " \"# POST TO https://gist.github.com via Python\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": null,\n", | |
| " \"metadata\": {\n", | |
| " \"collapsed\": true\n", | |
| " },\n", | |
| " \"outputs\": [],\n", | |
| " \"source\": [\n", | |
| " \"#%%writefile GISTstore/post2gist.py\\n\",\n", | |
| " \"import os\\n\",\n", | |
| " \"from time import sleep\\n\",\n", | |
| " \"import sqlite3\\n\",\n", | |
| " \"import os, requests, sys, json\\n\",\n", | |
| " \"import GISTkey\\n\",\n", | |
| " \"username=GISTkey.gistkey()[0]\\n\",\n", | |
| " \"password=GISTkey.gistkey()[1]\\n\",\n", | |
| " \"database =\\\"GISTstore/gist.db\\\"\\n\",\n", | |
| " \"conn = sqlite3.connect(database)\\n\",\n", | |
| " \"c = conn.cursor()\\n\",\n", | |
| " \"c.execute(\\\"\\\"\\\"\\n\",\n", | |
| " \"CREATE VIRTUAL TABLE IF NOT EXISTS gist \\n\",\n", | |
| " \"USING FTS4(content, description, filename);\\n\",\n", | |
| " \"\\\"\\\"\\\")\\n\",\n", | |
| " \"c.close()\\n\",\n", | |
| " \"conn.close()\\n\",\n", | |
| " \"conn = sqlite3.connect(database)\\n\",\n", | |
| " \"conn.text_factory=str \\n\",\n", | |
| " \"c = conn.cursor()\\n\",\n", | |
| " \"PATH = \\\"GISTstore/\\\"\\n\",\n", | |
| " \"filename = \\\"post2gist.py\\\"\\n\",\n", | |
| " \"content=open(PATH+filename, 'r').read()\\n\",\n", | |
| " \"r = requests.post('https://api.github.com/gists',json.dumps({'files':{filename:{\\\"content\\\":content}}}),auth=requests.auth.HTTPBasicAuth(username, password)) \\n\",\n", | |
| " \"sleep(2)\\n\",\n", | |
| " \"Url= (r.json()['html_url'])\\n\",\n", | |
| " \"c.execute(\\\"INSERT INTO gist VALUES (?,?,?)\\\", (content, Url, filename)) \\n\",\n", | |
| " \"conn.commit()\\n\",\n", | |
| " \"title = \\\"Index\\\"\\n\",\n", | |
| " \"c.execute(\\\"INSERT INTO gist VALUES (?,?,?)\\\", (title, Url, filename)) \\n\",\n", | |
| " \"conn.commit()\\n\",\n", | |
| " \"for row in c.execute('SELECT rowid, content, description, filename FROM gist \\\\\\n\",\n", | |
| " \" WHERE filename MATCH ?', (filename,)):\\n\",\n", | |
| " \" print Url,\\\"\\\\n\\\",row[0],row[1],row[2],row[3]\\n\",\n", | |
| " \"c.close()\\n\",\n", | |
| " \"conn.close()\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 69,\n", | |
| " \"metadata\": {},\n", | |
| " \"outputs\": [\n", | |
| " {\n", | |
| " \"name\": \"stdout\",\n", | |
| " \"output_type\": \"stream\",\n", | |
| " \"text\": [\n", | |
| " \"Writing TestText.txt\\n\"\n", | |
| " ]\n", | |
| " }\n", | |
| " ],\n", | |
| " \"source\": [\n", | |
| " \"%%writefile TestText.txt\\n\",\n", | |
| " \"This file is created in order to test my text to speech scripts.\\n\",\n", | |
| " \"I am wanting to use text to speech and the Jupyter Notebook to create youtube video.\\n\",\n", | |
| " \"Converting text to mp3 is quite easy, however to directly convert to sound is not as simple.\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 75,\n", | |
| " \"metadata\": {\n", | |
| " \"collapsed\": true\n", | |
| " },\n", | |
| " \"outputs\": [],\n", | |
| " \"source\": [\n", | |
| " \"from gtts import gTTS\\n\",\n", | |
| " \"import os\\n\",\n", | |
| " \"filename = 'TestText.txt'\\n\",\n", | |
| " \"content=open(filename, 'r').read().decode(\\\"utf-8\\\", \\\"ignore\\\")\\n\",\n", | |
| " \"tts = gTTS(text=content, lang='en')\\n\",\n", | |
| " \"tts.save(filename+\\\".mp3\\\")\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": null,\n", | |
| " \"metadata\": {\n", | |
| " \"collapsed\": true\n", | |
| " },\n", | |
| " \"outputs\": [],\n", | |
| " \"source\": [\n", | |
| " \"from gtts import gTTS\\n\",\n", | |
| " \"import os\\n\",\n", | |
| " \"sd =open('/home/jack/Desktop/text_stuff/Wiki_HistoryofEarth.txt', 'r')\\n\",\n", | |
| " \"rd = sd.read().decode(\\\"utf-8\\\", \\\"ignore\\\")\\n\",\n", | |
| " \"tts = gTTS(text=rd, lang='en')\\n\",\n", | |
| " \"tts.save(\\\"good.mp3\\\")\\n\",\n", | |
| " \"os.system(\\\"mpg321 good.mp3\\\")\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 76,\n", | |
| " \"metadata\": {},\n", | |
| " \"outputs\": [\n", | |
| " {\n", | |
| " \"name\": \"stdout\",\n", | |
| " \"output_type\": \"stream\",\n", | |
| " \"text\": [\n", | |
| " \"audio.mp3 good.mp3 hek.mp3 hello.mp3 play.mp3 test.mp3 TestText.txt.mp3\\r\\n\"\n", | |
| " ]\n", | |
| " }\n", | |
| " ],\n", | |
| " \"source\": [\n", | |
| " \"!ls *.mp3\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 4,\n", | |
| " \"metadata\": {},\n", | |
| " \"outputs\": [\n", | |
| " {\n", | |
| " \"name\": \"stdout\",\n", | |
| " \"output_type\": \"stream\",\n", | |
| " \"text\": [\n", | |
| " \"Overwriting GISTstore/Pmp3.py\\n\"\n", | |
| " ]\n", | |
| " }\n", | |
| " ],\n", | |
| " \"source\": [\n", | |
| " \"%%writefile GISTstore/Pmp3.py\\n\",\n", | |
| " \"import pyglet\\n\",\n", | |
| " \"def exit_callback(dt):\\n\",\n", | |
| " \" pyglet.app.exit()\\n\",\n", | |
| " \" return\\n\",\n", | |
| " \"def pmp3(mp3File):\\n\",\n", | |
| " \" music = pyglet.media.load(mp3File)\\n\",\n", | |
| " \" music.play()\\n\",\n", | |
| " \" pyglet.clock.schedule_once(exit_callback , music.duration)\\n\",\n", | |
| " \" pyglet.app.run()\\n\",\n", | |
| " \" return\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 1,\n", | |
| " \"metadata\": {},\n", | |
| " \"outputs\": [\n", | |
| " {\n", | |
| " \"name\": \"stdout\",\n", | |
| " \"output_type\": \"stream\",\n", | |
| " \"text\": [\n", | |
| " \"Ver 1.06\\n\"\n", | |
| " ]\n", | |
| " }\n", | |
| " ],\n", | |
| " \"source\": [\n", | |
| " \"from GISTstore import Pmp3\\n\",\n", | |
| " \"mp3File = 'audio.mp3'\\n\",\n", | |
| " \"Pmp3.pmp3(mp3File)\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 11,\n", | |
| " \"metadata\": {},\n", | |
| " \"outputs\": [\n", | |
| " {\n", | |
| " \"name\": \"stdout\",\n", | |
| " \"output_type\": \"stream\",\n", | |
| " \"text\": [\n", | |
| " \"Help on function schedule_once in module pyglet.clock:\\n\",\n", | |
| " \"\\n\",\n", | |
| " \"schedule_once(func, delay, *args, **kwargs)\\n\",\n", | |
| " \" Schedule 'func' to be called once after 'delay' seconds (can be\\n\",\n", | |
| " \" a float) on the default clock. The arguments passed to 'func' are\\n\",\n", | |
| " \" 'dt' (time since last function call), followed by any ``*args`` and\\n\",\n", | |
| " \" ``**kwargs`` given here.\\n\",\n", | |
| " \" \\n\",\n", | |
| " \" If no default clock is set, the func is queued and will be scheduled\\n\",\n", | |
| " \" on the default clock as soon as it is created.\\n\",\n", | |
| " \" \\n\",\n", | |
| " \" :Parameters:\\n\",\n", | |
| " \" `func` : function\\n\",\n", | |
| " \" The function to call when the timer lapses.\\n\",\n", | |
| " \" `delay` : float\\n\",\n", | |
| " \" The number of seconds to wait before the timer lapses.\\n\",\n", | |
| " \"\\n\"\n", | |
| " ]\n", | |
| " }\n", | |
| " ],\n", | |
| " \"source\": [\n", | |
| " \"help(pyglet.clock.schedule_once)\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"markdown\",\n", | |
| " \"metadata\": {},\n", | |
| " \"source\": [\n", | |
| " \"# get mp3 length\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 24,\n", | |
| " \"metadata\": {},\n", | |
| " \"outputs\": [\n", | |
| " {\n", | |
| " \"name\": \"stdout\",\n", | |
| " \"output_type\": \"stream\",\n", | |
| " \"text\": [\n", | |
| " \"17.424\\n\"\n", | |
| " ]\n", | |
| " }\n", | |
| " ],\n", | |
| " \"source\": [\n", | |
| " \"from mutagen.mp3 import MP3\\n\",\n", | |
| " \"audio = MP3(\\\"TestText.txt.mp3\\\")\\n\",\n", | |
| " \"print audio.info.length\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 2,\n", | |
| " \"metadata\": {},\n", | |
| " \"outputs\": [],\n", | |
| " \"source\": [\n", | |
| " \"from GISTstore import Pmp3\\n\",\n", | |
| " \"mp3File = 'audio.mp3'\\n\",\n", | |
| " \"Pmp3.pmp3(mp3File)\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 3,\n", | |
| " \"metadata\": {},\n", | |
| " \"outputs\": [],\n", | |
| " \"source\": [\n", | |
| " \"from GISTstore import Pmp3\\n\",\n", | |
| " \"mp3File = 'TestText.txt.mp3'\\n\",\n", | |
| " \"Pmp3.pmp3(mp3File)\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 6,\n", | |
| " \"metadata\": {},\n", | |
| " \"outputs\": [],\n", | |
| " \"source\": [\n", | |
| " \"import pyglet\\n\",\n", | |
| " \"music = pyglet.media.load('TestText.txt.mp3')\\n\",\n", | |
| " \"music.play()\\n\",\n", | |
| " \"def exit_callback(dt):\\n\",\n", | |
| " \" pyglet.app.exit()\\n\",\n", | |
| " \"pyglet.clock.schedule_once(exit_callback , music.duration)\\n\",\n", | |
| " \"pyglet.app.run()\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": null,\n", | |
| " \"metadata\": {\n", | |
| " \"collapsed\": true\n", | |
| " },\n", | |
| " \"outputs\": [],\n", | |
| " \"source\": [\n", | |
| " \"from gtts import gTTS\\n\",\n", | |
| " \"import os\\n\",\n", | |
| " \"sd =open('/home/jack/Desktop/text_stuff/Wiki_HistoryofEarth.txt', 'r')\\n\",\n", | |
| " \"rd = sd.read().decode(\\\"utf-8\\\", \\\"ignore\\\")\\n\",\n", | |
| " \"tts = gTTS(text=rd, lang='en')\\n\",\n", | |
| " \"tts.save(\\\"good.mp3\\\")\\n\",\n", | |
| " \"os.system(\\\"mpg321 good.mp3\\\")\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 19,\n", | |
| " \"metadata\": {\n", | |
| " \"collapsed\": true\n", | |
| " },\n", | |
| " \"outputs\": [],\n", | |
| " \"source\": [\n", | |
| " \"from gtts import gTTS\\n\",\n", | |
| " \"sd =open('/home/jack/Desktop/text_stuff/hek.txt', 'r')\\n\",\n", | |
| " \"rd = sd.read().decode(\\\"utf-8\\\", \\\"ignore\\\")\\n\",\n", | |
| " \"tts=gTTS(rd ,lang='en')\\n\",\n", | |
| " \"tts.save(\\\"hek.mp3\\\")\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 9,\n", | |
| " \"metadata\": {\n", | |
| " \"collapsed\": true\n", | |
| " },\n", | |
| " \"outputs\": [],\n", | |
| " \"source\": [\n", | |
| " \"import pyttsx\\n\",\n", | |
| " \"engine = pyttsx.init()\\n\",\n", | |
| " \"voices = engine.getProperty('voices')\\n\",\n", | |
| " \"engine.setProperty('voice', 'en-scottish')\\n\",\n", | |
| " \"engine.say('The quick brown fox jumped over the lazy dog.')\\n\",\n", | |
| " \"engine.runAndWait()\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 10,\n", | |
| " \"metadata\": {},\n", | |
| " \"outputs\": [\n", | |
| " {\n", | |
| " \"name\": \"stdout\",\n", | |
| " \"output_type\": \"stream\",\n", | |
| " \"text\": [\n", | |
| " \"cantonese\\n\"\n", | |
| " ]\n", | |
| " }\n", | |
| " ],\n", | |
| " \"source\": [\n", | |
| " \"import pyttsx\\n\",\n", | |
| " \"engine = pyttsx.init()\\n\",\n", | |
| " \"voices = engine.getProperty('voices')\\n\",\n", | |
| " \"engine.setProperty('voice', 'english_rp')\\n\",\n", | |
| " \"print voice.id\\n\",\n", | |
| " \"engine.say('The quick brown fox jumped over the lazy dog.')\\n\",\n", | |
| " \"engine.runAndWait()\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 11,\n", | |
| " \"metadata\": {\n", | |
| " \"collapsed\": true\n", | |
| " },\n", | |
| " \"outputs\": [],\n", | |
| " \"source\": [\n", | |
| " \"import pyttsx\\n\",\n", | |
| " \"engine = pyttsx.init()\\n\",\n", | |
| " \"#voices = engine.getProperty('voices')\\n\",\n", | |
| " \"#engine.setProperty('voice', 'english_rp')\\n\",\n", | |
| " \"#print voice.id\\n\",\n", | |
| " \"engine.say('The quick brown fox jumped over the lazy dog.')\\n\",\n", | |
| " \"engine.runAndWait()\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": null,\n", | |
| " \"metadata\": {\n", | |
| " \"collapsed\": true\n", | |
| " },\n", | |
| " \"outputs\": [],\n", | |
| " \"source\": [\n", | |
| " \"import pyttsx\\n\",\n", | |
| " \"engine = pyttsx.init()\\n\",\n", | |
| " \"voices = engine.getProperty('voices')\\n\",\n", | |
| " \"for voice in voices:\\n\",\n", | |
| " \" engine.setProperty('voice', voice.id)\\n\",\n", | |
| " \" print voice.id\\n\",\n", | |
| " \" engine.say('The quick brown fox jumped over the lazy dog.')\\n\",\n", | |
| " \"engine.runAndWait()\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 17,\n", | |
| " \"metadata\": {\n", | |
| " \"collapsed\": true\n", | |
| " },\n", | |
| " \"outputs\": [],\n", | |
| " \"source\": [\n", | |
| " \"import pyttsx\\n\",\n", | |
| " \"engine = pyttsx.init()\\n\",\n", | |
| " \"engine.say('Sally sells seashells by the seashore.')\\n\",\n", | |
| " \"engine.say('The quick brown fox jumped over the lazy dog.')\\n\",\n", | |
| " \"engine.runAndWait()\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 16,\n", | |
| " \"metadata\": {\n", | |
| " \"collapsed\": true\n", | |
| " },\n", | |
| " \"outputs\": [],\n", | |
| " \"source\": [\n", | |
| " \"import pyttsx\\n\",\n", | |
| " \"engine = pyttsx.init()\\n\",\n", | |
| " \"engine.say('Sally sells seashells by the seashore.')\\n\",\n", | |
| " \"engine.say('The quick brown fox jumped over the lazy dog.')\\n\",\n", | |
| " \"engine.runAndWait()\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 14,\n", | |
| " \"metadata\": {\n", | |
| " \"collapsed\": true\n", | |
| " },\n", | |
| " \"outputs\": [],\n", | |
| " \"source\": [\n", | |
| " \"import pyttsx\\n\",\n", | |
| " \"engine = pyttsx.init()\\n\",\n", | |
| " \"rate = engine.getProperty('rate')\\n\",\n", | |
| " \"engine.setProperty('rate', rate-30)\\n\",\n", | |
| " \"engine.say('The quick brown fox jumped over the lazy dog.')\\n\",\n", | |
| " \"engine.runAndWait()\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": null,\n", | |
| " \"metadata\": {\n", | |
| " \"collapsed\": true\n", | |
| " },\n", | |
| " \"outputs\": [],\n", | |
| " \"source\": [\n", | |
| " \"engine = pyttsx.init()\\n\",\n", | |
| " \"voices = engine.getProperty('voices')\\n\",\n", | |
| " \"for voice in voices:\\n\",\n", | |
| " \" engine.setProperty('voice', voice.id)\\n\",\n", | |
| " \" engine.say('The quick brown fox jumped over the lazy dog.')\\n\",\n", | |
| " \"engine.runAndWait()\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 1,\n", | |
| " \"metadata\": {},\n", | |
| " \"outputs\": [\n", | |
| " {\n", | |
| " \"name\": \"stdout\",\n", | |
| " \"output_type\": \"stream\",\n", | |
| " \"text\": [\n", | |
| " \"Help on module pyttsx.voice in pyttsx:\\n\",\n", | |
| " \"\\n\",\n", | |
| " \"NAME\\n\",\n", | |
| " \" pyttsx.voice - Voice metadata definition.\\n\",\n", | |
| " \"\\n\",\n", | |
| " \"FILE\\n\",\n", | |
| " \" /home/jack/Desktop/Processing/Processing/pyttsx/voice.py\\n\",\n", | |
| " \"\\n\",\n", | |
| " \"DESCRIPTION\\n\",\n", | |
| " \" Copyright (c) 2009, 2013 Peter Parente\\n\",\n", | |
| " \" \\n\",\n", | |
| " \" Permission to use, copy, modify, and distribute this software for any\\n\",\n", | |
| " \" purpose with or without fee is hereby granted, provided that the above\\n\",\n", | |
| " \" copyright notice and this permission notice appear in all copies.\\n\",\n", | |
| " \" \\n\",\n", | |
| " \" THE SOFTWARE IS PROVIDED \\\"AS IS\\\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES\\n\",\n", | |
| " \" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF\\n\",\n", | |
| " \" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR\\n\",\n", | |
| " \" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES\\n\",\n", | |
| " \" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN\\n\",\n", | |
| " \" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF\\n\",\n", | |
| " \" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\\n\",\n", | |
| " \"\\n\",\n", | |
| " \"CLASSES\\n\",\n", | |
| " \" __builtin__.object\\n\",\n", | |
| " \" Voice\\n\",\n", | |
| " \" \\n\",\n", | |
| " \" class Voice(__builtin__.object)\\n\",\n", | |
| " \" | Methods defined here:\\n\",\n", | |
| " \" | \\n\",\n", | |
| " \" | __init__(self, id, name=None, languages=[], gender=None, age=None)\\n\",\n", | |
| " \" | \\n\",\n", | |
| " \" | __str__(self)\\n\",\n", | |
| " \" | \\n\",\n", | |
| " \" | ----------------------------------------------------------------------\\n\",\n", | |
| " \" | Data descriptors defined here:\\n\",\n", | |
| " \" | \\n\",\n", | |
| " \" | __dict__\\n\",\n", | |
| " \" | dictionary for instance variables (if defined)\\n\",\n", | |
| " \" | \\n\",\n", | |
| " \" | __weakref__\\n\",\n", | |
| " \" | list of weak references to the object (if defined)\\n\",\n", | |
| " \"\\n\",\n", | |
| " \"\\n\"\n", | |
| " ]\n", | |
| " }\n", | |
| " ],\n", | |
| " \"source\": [\n", | |
| " \"from pyttsx import voice\\n\",\n", | |
| " \"help(voice)\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 5,\n", | |
| " \"metadata\": {},\n", | |
| " \"outputs\": [\n", | |
| " {\n", | |
| " \"name\": \"stdout\",\n", | |
| " \"output_type\": \"stream\",\n", | |
| " \"text\": [\n", | |
| " \"Help on method getProperty in module pyttsx.engine:\\n\",\n", | |
| " \"\\n\",\n", | |
| " \"getProperty(self, name) method of pyttsx.engine.Engine instance\\n\",\n", | |
| " \" Gets the current value of a property. Valid names and values include:\\n\",\n", | |
| " \" \\n\",\n", | |
| " \" voices: List of L{voice.Voice} objects supported by the driver\\n\",\n", | |
| " \" voice: String ID of the current voice\\n\",\n", | |
| " \" rate: Integer speech rate in words per minute\\n\",\n", | |
| " \" volume: Floating point volume of speech in the range [0.0, 1.0]\\n\",\n", | |
| " \" \\n\",\n", | |
| " \" Numeric values outside the valid range supported by the driver are\\n\",\n", | |
| " \" clipped.\\n\",\n", | |
| " \" \\n\",\n", | |
| " \" @param name: Name of the property to fetch\\n\",\n", | |
| " \" @type name: str\\n\",\n", | |
| " \" @return: Value associated with the property\\n\",\n", | |
| " \" @rtype: object\\n\",\n", | |
| " \" @raise KeyError: When the property name is unknown\\n\",\n", | |
| " \"\\n\"\n", | |
| " ]\n", | |
| " }\n", | |
| " ],\n", | |
| " \"source\": [\n", | |
| " \"import pyttsx\\n\",\n", | |
| " \"engine = pyttsx.init()\\n\",\n", | |
| " \"voices = engine.getProperty('voices')\\n\",\n", | |
| " \"help(engine.getProperty)\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 7,\n", | |
| " \"metadata\": {},\n", | |
| " \"outputs\": [],\n", | |
| " \"source\": [\n", | |
| " \"import pyttsx\\n\",\n", | |
| " \"engine = pyttsx.init()\\n\",\n", | |
| " \"voices = engine.getProperty('voices')\\n\",\n", | |
| " \"for voice in voices:\\n\",\n", | |
| " \" engine.setProperty('voice', voice.id)\\n\",\n", | |
| " \" engine.say('The quick brown fox jumped over the lazy dog.')\\n\",\n", | |
| " \"engine.runAndWait()\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 23,\n", | |
| " \"metadata\": {\n", | |
| " \"scrolled\": true\n", | |
| " },\n", | |
| " \"outputs\": [\n", | |
| " {\n", | |
| " \"name\": \"stdout\",\n", | |
| " \"output_type\": \"stream\",\n", | |
| " \"text\": [\n", | |
| " \"index-PDE2db.txt indexPDE2db.txt index.txt nov04.txt readthis.txt\\r\\n\"\n", | |
| " ]\n", | |
| " }\n", | |
| " ],\n", | |
| " \"source\": [\n", | |
| " \"!ls *.txt\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 12,\n", | |
| " \"metadata\": {},\n", | |
| " \"outputs\": [\n", | |
| " {\n", | |
| " \"name\": \"stdout\",\n", | |
| " \"output_type\": \"stream\",\n", | |
| " \"text\": [\n", | |
| " \"Help on class gTTS in module gtts.tts:\\n\",\n", | |
| " \"\\n\",\n", | |
| " \"class gTTS\\n\",\n", | |
| " \" | gTTS (Google Text to Speech): an interface to Google's Text to Speech API\\n\",\n", | |
| " \" | \\n\",\n", | |
| " \" | Methods defined here:\\n\",\n", | |
| " \" | \\n\",\n", | |
| " \" | __init__(self, text, lang='en', slow=False, debug=False)\\n\",\n", | |
| " \" | \\n\",\n", | |
| " \" | save(self, savefile)\\n\",\n", | |
| " \" | Do the Web request and save to `savefile`\\n\",\n", | |
| " \" | \\n\",\n", | |
| " \" | write_to_fp(self, fp)\\n\",\n", | |
| " \" | Do the Web request and save to a file-like object\\n\",\n", | |
| " \" | \\n\",\n", | |
| " \" | ----------------------------------------------------------------------\\n\",\n", | |
| " \" | Data and other attributes defined here:\\n\",\n", | |
| " \" | \\n\",\n", | |
| " \" | GOOGLE_TTS_URL = 'https://translate.google.com/translate_tts'\\n\",\n", | |
| " \" | \\n\",\n", | |
| " \" | LANGUAGES = {'af': 'Afrikaans', 'ar': 'Arabic', 'bn': 'Bengali', 'ca':...\\n\",\n", | |
| " \" | \\n\",\n", | |
| " \" | MAX_CHARS = 100\\n\",\n", | |
| " \" | \\n\",\n", | |
| " \" | Speed = <class gtts.tts.Speed>\\n\",\n", | |
| " \"\\n\"\n", | |
| " ]\n", | |
| " }\n", | |
| " ],\n", | |
| " \"source\": [\n", | |
| " \"from gtts import gTTS\\n\",\n", | |
| " \"gTTS()\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"markdown\",\n", | |
| " \"metadata\": {},\n", | |
| " \"source\": [\n", | |
| " \"http://pyttsx.readthedocs.io/en/latest/engine.html#changing-voices\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 10,\n", | |
| " \"metadata\": {},\n", | |
| " \"outputs\": [\n", | |
| " {\n", | |
| " \"name\": \"stdout\",\n", | |
| " \"output_type\": \"stream\",\n", | |
| " \"text\": [\n", | |
| " \"Help on module UnCamel:\\n\",\n", | |
| " \"\\n\",\n", | |
| " \"NAME\\n\",\n", | |
| " \" UnCamel\\n\",\n", | |
| " \"\\n\",\n", | |
| " \"FILE\\n\",\n", | |
| " \" /home/jack/Desktop/Processing/Processing/UnCamel.py\\n\",\n", | |
| " \"\\n\",\n", | |
| " \"DESCRIPTION\\n\",\n", | |
| " \" # USAGE example:\\n\",\n", | |
| " \" # import UnCamel\\n\",\n", | |
| " \" # x = \\\"TimeToUncamelCaseTextUsingFunctions\\\"\\n\",\n", | |
| " \" # UnCamel.uncamel(x)\\n\",\n", | |
| " \"\\n\",\n", | |
| " \"FUNCTIONS\\n\",\n", | |
| " \" uncam(x)\\n\",\n", | |
| " \" \\n\",\n", | |
| " \" uncamel(x)\\n\",\n", | |
| " \" # USAGE example:\\n\",\n", | |
| " \" # import UnCamel\\n\",\n", | |
| " \" # x = \\\"TimeToUncamelCaseTextUsingFunctions\\\"\\n\",\n", | |
| " \" # UnCamel.uncamel(x)\\n\",\n", | |
| " \"\\n\",\n", | |
| " \"\\n\"\n", | |
| " ]\n", | |
| " }\n", | |
| " ],\n", | |
| " \"source\": [\n", | |
| " \"import UnCamel\\n\",\n", | |
| " \"help(UnCamel)\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 12,\n", | |
| " \"metadata\": {},\n", | |
| " \"outputs\": [\n", | |
| " {\n", | |
| " \"name\": \"stdout\",\n", | |
| " \"output_type\": \"stream\",\n", | |
| " \"text\": [\n", | |
| " \"Writing GISTstore/UnCamel.py\\n\"\n", | |
| " ]\n", | |
| " }\n", | |
| " ],\n", | |
| " \"source\": [\n", | |
| " \"%%writefile GISTstore/UnCamel.py\\n\",\n", | |
| " \"# USAGE example:\\n\",\n", | |
| " \"# import UnCamel\\n\",\n", | |
| " \"# x = \\\"TimeToUncamelCaseTextUsingFunctions\\\"\\n\",\n", | |
| " \"# UnCamel.uncamel(x)\\n\",\n", | |
| " \"def uncamel(x): \\n\",\n", | |
| " \" return reduce(lambda a,b: a + ((b.upper() == b and (len(a) and a[-1].upper() != a[-1])) \\n\",\n", | |
| " \" and (' ' + b) or b), x, '')\\n\",\n", | |
| " \"def uncam(x):\\n\",\n", | |
| " \" import re\\n\",\n", | |
| " \" return re.sub(\\\"([a-z])([A-Z])\\\",\\\"\\\\g<1> \\\\g<2>\\\",x)\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": 4,\n", | |
| " \"metadata\": {},\n", | |
| " \"outputs\": [\n", | |
| " {\n", | |
| " \"data\": {\n", | |
| " \"text/plain\": [\n", | |
| " \"'Let Us Try To Uncamel Case Text Using Functions'\"\n", | |
| " ]\n", | |
| " },\n", | |
| " \"execution_count\": 4,\n", | |
| " \"metadata\": {},\n", | |
| " \"output_type\": \"execute_result\"\n", | |
| " }\n", | |
| " ],\n", | |
| " \"source\": [\n", | |
| " \"def uncamel(x): \\n\",\n", | |
| " \" return reduce(lambda a,b: a + ((b.upper() == b and \\n\",\n", | |
| " \" (len(a) and a[-1].upper() != a[-1])) and \\n\",\n", | |
| " \" (' ' + b) or b), x, '')\\n\",\n", | |
| " \"x = \\\"LetUsTryToUncamelCaseTextUsingFunctions\\\"\\n\",\n", | |
| " \"uncamel(x)\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"markdown\",\n", | |
| " \"metadata\": {},\n", | |
| " \"source\": [\n", | |
| " \"# Github API\"\n", | |
| " ]\n", | |
| " },\n", | |
| " {\n", | |
| " \"cell_type\": \"code\",\n", | |
| " \"execution_count\": null,\n", | |
| " \"metadata\": {\n", | |
| " \"collapsed\": true\n", | |
| " },\n", | |
| " \"outputs\": [],\n", | |
| " \"source\": [\n", | |
| " \"{\\n\",\n", | |
| " \" \\\"current_user_url\\\": \\\"https://api.github.com/user\\\",\\n\",\n", | |
| " \" \\\"current_user_authorizations_html_url\\\": \\\"https://github.com/settings/connections/applications{/client_id}\\\",\\n\",\n", | |
| " \" \\\"authorizations_url\\\": \\\"https://api.github.com/authorizations\\\",\\n\",\n", | |
| " \" \\\"code_search_url\\\": \\\"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}\\\",\\n\",\n", | |
| " \" \\\"commit_search_url\\\": \\\"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}\\\",\\n\",\n", | |
| " \" \\\"emails_url\\\": \\\"https://api.github.com/user/emails\\\",\\n\",\n", | |
| " \" \\\"emojis_url\\\": \\\"https://api.github.com/emojis\\\",\\n\",\n", | |
| " \" \\\"events_url\\\": \\\"https://api.github.com/events\\\",\\n\",\n", | |
| " \" \\\"feeds_url\\\": \\\"https://api.github.com/feeds\\\",\\n\",\n", | |
| " \" \\\"followers_url\\\": \\\"https://api.github.com/user/followers\\\",\\n\",\n", | |
| " \" \\\"following_url\\\": \\\"https://api.github.com/user/following{/target}\\\",\\n\",\n", | |
| " \" \\\"gists_url\\\": \\\"https://api.github.com/gists{/gist_id}\\\",\\n\",\n", | |
| " \" \\\"hub_url\\\": \\\"https://api.github.com/hub\\\",\\n\",\n", | |
| " \" \\\"issue_search_url\\\": \\\"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}\\\",\\n\",\n", | |
| " \" \\\"issues_url\\\": \\\"https://api.github.com/issues\\\",\\n\",\n", | |
| " \" \\\"keys_url\\\": \\\"https://api.github.com/user/keys\\\",\\n\",\n", | |
| " \" \\\"notifications_url\\\": \\\"https://api.github.com/notifications\\\",\\n\",\n", | |
| " \" \\\"organization_repositories_url\\\": \\\"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}\\\",\\n\",\n", | |
| " \" \\\"organization_url\\\": \\\"https://api.github.com/orgs/{org}\\\",\\n\",\n", | |
| " \" \\\"public_gists_url\\\": \\\"https://api.github.com/gists/public\\\",\\n\",\n", | |
| " \" \\\"rate_limit_url\\\": \\\"https://api.github.com/rate_limit\\\",\\n\",\n", | |
| " \" \\\"repository_url\\\": \\\"https://api.github.com/repos/{owner}/{repo}\\\",\\n\",\n", | |
| " \" \\\"repository_search_url\\\": \\\"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}\\\",\\n\",\n", | |
| " \" \\\"current_user_repositories_url\\\": \\\"https://api.github.com/user/repos{?type,page,per_page,sort}\\\",\\n\",\n", | |
| " \" \\\"starred_url\\\": \\\"https://api.github.com/user/starred{/owner}{/repo}\\\",\\n\",\n", | |
| " \" \\\"starred_gists_url\\\": \\\"https://api.github.com/gists/starred\\\",\\n\",\n", | |
| " \" \\\"team_url\\\": \\\"https://api.github.com/teams\\\",\\n\",\n", | |
| " \" \\\"user_url\\\": \\\"https://api.github.com/users/{user}\\\",\\n\",\n", | |
| " \" \\\"user_organizations_url\\\": \\\"https://api.github.com/user/orgs\\\",\\n\",\n", | |
| " \" \\\"user_repositories_url\\\": \\\"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}\\\",\\n\",\n", | |
| " \" \\\"user_search_url\\\": \\\"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}\\\"\\n\",\n", | |
| " \"}\\n\"\n", | |
| " ]\n", | |
| " }\n", | |
| " ],\n", | |
| " \"metadata\": {\n", | |
| " \"kernelspec\": {\n", | |
| " \"display_name\": \"Python 2\",\n", | |
| " \"language\": \"python\",\n", | |
| " \"name\": \"python2\"\n", | |
| " },\n", | |
| " \"language_info\": {\n", | |
| " \"codemirror_mode\": {\n", | |
| " \"name\": \"ipython\",\n", | |
| " \"version\": 2\n", | |
| " },\n", | |
| " \"file_extension\": \".py\",\n", | |
| " \"mimetype\": \"text/x-python\",\n", | |
| " \"name\": \"python\",\n", | |
| " \"nbconvert_exporter\": \"python\",\n", | |
| " \"pygments_lexer\": \"ipython2\",\n", | |
| " \"version\": \"2.7.13\"\n", | |
| " }\n", | |
| " },\n", | |
| " \"nbformat\": 4,\n", | |
| " \"nbformat_minor\": 2\n", | |
| "}\n", | |
| "https://gist.github.com/5d86ae97f513a505db5c28320dd178b1 UnCamel.py\n", | |
| "https://gist.github.com/5d86ae97f513a505db5c28320dd178b1 \n", | |
| "12 Index https://gist.github.com/5d86ae97f513a505db5c28320dd178b1 UnCamel.py\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "#%%writefile GISTstore/post2gist.py\n", | |
| "import os\n", | |
| "from time import sleep\n", | |
| "import sqlite3\n", | |
| "import os, requests, sys, json\n", | |
| "import GISTkey\n", | |
| "username=GISTkey.gistkey()[0]\n", | |
| "password=GISTkey.gistkey()[1]\n", | |
| "database =\"GISTstore/gist.db\"\n", | |
| "conn = sqlite3.connect(database)\n", | |
| "c = conn.cursor()\n", | |
| "c.execute(\"\"\"\n", | |
| "CREATE VIRTUAL TABLE IF NOT EXISTS gist \n", | |
| "USING FTS4(content, description, filename);\n", | |
| "\"\"\")\n", | |
| "c.close()\n", | |
| "conn.close()\n", | |
| "conn = sqlite3.connect(database)\n", | |
| "conn.text_factory=str \n", | |
| "c = conn.cursor()\n", | |
| "if not os.path.exists('GISTstore'):\n", | |
| " os.makedirs('GISTstore') \n", | |
| "PATH = \"GISTstore/\"\n", | |
| "filename = \"UnCamel.py\"\n", | |
| "content=open(PATH+filename, 'r').read()\n", | |
| "#content=open('Post_To_Gist _PostToGist.py.ipynb', 'r').read()\n", | |
| "post = 'https://api.github.com/gists'\n", | |
| "r = requests.post(post,json.dumps({'files':{filename:{\"content\":content}}}),\n", | |
| " auth=requests.auth.HTTPBasicAuth(username, password)) \n", | |
| "sleep(2)\n", | |
| "Url= (r.json()['html_url'])\n", | |
| "c.execute(\"INSERT INTO gist VALUES (?,?,?)\", (content, Url, filename)) \n", | |
| "conn.commit()\n", | |
| "title = \"Index\"\n", | |
| "c.execute(\"INSERT INTO gist VALUES (?,?,?)\", (title, Url, filename)) \n", | |
| "conn.commit()\n", | |
| "for row in c.execute('SELECT rowid, content, description, filename FROM gist \\\n", | |
| " WHERE filename MATCH ?', (filename,)):\n", | |
| " print Url,\"\\n\",row[0],row[1],row[2],row[3]\n", | |
| "c.close()\n", | |
| "conn.close()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# SQLite database\n", | |
| "## View Gists published to https://gist.github.com" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 30, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Overwriting GISTstore/GistIndex.py\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%%writefile GISTstore/GistIndex.py\n", | |
| "import sqlite3\n", | |
| "def Index():\n", | |
| " database =\"GISTstore/gist.db\"\n", | |
| " conn = sqlite3.connect(database)\n", | |
| " c = conn.cursor()\n", | |
| " for row in c.execute('SELECT rowid, content, description, filename FROM gist \\\n", | |
| " WHERE content = ?', (\"Index\",)):\n", | |
| " print row[0],row[1],row[2],row[3]\n", | |
| " \n", | |
| "\n", | |
| "if __name__ == \"__main__\":\n", | |
| " Index() " | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 31, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "2 Index https://gist.github.com/f67b8ecf4589b531f408d798a4e650eb post2gist.py\r\n", | |
| "4 Index https://gist.github.com/d640c0100e5535b608e2fe0f4e8fe925 GISTkey.py\r\n", | |
| "6 Index https://gist.github.com/71cd9394db91fd089765861ebf121b0e SimpleGistPost.py\r\n", | |
| "8 Index https://gist.github.com/8426999e17ef19b9ea7e2a27a3223773 Pmp3.py\r\n", | |
| "10 Index https://gist.github.com/76663365e1c77ad6ba19c25009001c03 UnCamel.py\r\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "!python GISTstore/GistIndex.py" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 24, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Writing PostToGist.py\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%%writefile PostToGist.py\n", | |
| "#!/usr/bin/python\n", | |
| "import os, requests, sys, json\n", | |
| "import GISTkey\n", | |
| "username=GISTkey.gist()[0]\n", | |
| "password=GISTkey.gist()[1]\n", | |
| "filename = os.path.basename(sys.argv[1])\n", | |
| "content=open(filename, 'r').read()\n", | |
| "r = requests.post('https://api.github.com/gists',json.dumps({'files':{filename:{\"content\":content}}}),auth=requests.auth.HTTPBasicAuth(username, password)) \n", | |
| "print(r.json()['html_url'])" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 35, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "!mkdir GISTstore" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 53, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Writing GISTstore/__init__.py\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%%writefile GISTstore/__init__.py\n", | |
| "print \"Ver 1.06\"" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 43, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Overwriting GISTstore/GISTkey.py\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%%writefile GISTstore/GISTkey.py\n", | |
| "# USE:\n", | |
| "#import GISTkey\n", | |
| "#username=GISTkey.gist()[0]\n", | |
| "#password=GISTkey.gist()[1]\n", | |
| "def gist():\n", | |
| " username='username'\n", | |
| " password=\"password\"\n", | |
| " GistKEY = (username, password)\n", | |
| " return GistKEY" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 57, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "username password\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "from GISTstore import GISTkey\n", | |
| "username=GISTkey.gistkey()[0]\n", | |
| "password=GISTkey.gistkey()[1]\n", | |
| "print username,password" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 51, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Overwriting GISTstore/GISTkey.py\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%%writefile GISTstore/GISTkey.py\n", | |
| "# USE:\n", | |
| "#import GISTkey\n", | |
| "#username=GISTkey.gistkey()[0]\n", | |
| "#password=GISTkey.gistkey()[1]\n", | |
| "def gistkey():\n", | |
| " username='username'\n", | |
| " password=\"password\"\n", | |
| " GistAPI = (username, password)\n", | |
| " return GistAPI" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 66, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Overwriting GISTstore/SimpleGistPost.py\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%%writefile GISTstore/SimpleGistPost.py\n", | |
| "#!/usr/bin/python\n", | |
| "#USE: python SimpleGistPost.py FileToPost.py\n", | |
| "import os, requests, sys, json\n", | |
| "import GISTkey\n", | |
| "from time import sleep\n", | |
| "username=GISTkey.gistkey()[0]\n", | |
| "password=GISTkey.gistkey()[1]\n", | |
| "filename = os.path.basename(sys.argv[1])\n", | |
| "content=open(filename, 'r').read()\n", | |
| "r = requests.post('https://api.github.com/gists',json.dumps({'files':{filename:{\"content\":content}}}),auth=requests.auth.HTTPBasicAuth(username, password)) \n", | |
| "sleep(2)\n", | |
| "print(r.json()['html_url'])\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 68, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "gist.db GISTkey.pyc __init__.pyc SimpleGistPost.py\r\n", | |
| "GISTkey.py __init__.py post2gist.py\r\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "!ls GISTstore" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 31, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "blogblocks Shair2u2\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "import GISTkey\n", | |
| "username=GISTkey.gistkey()[0]\n", | |
| "password=GISTkey.gistkey()[1]\n", | |
| "print username, password" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 49, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "!rm GISTstore/gist.db" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# POST TO https://gist.github.com via Python" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "#%%writefile GISTstore/post2gist.py\n", | |
| "import os\n", | |
| "from time import sleep\n", | |
| "import sqlite3\n", | |
| "import os, requests, sys, json\n", | |
| "import GISTkey\n", | |
| "username=GISTkey.gistkey()[0]\n", | |
| "password=GISTkey.gistkey()[1]\n", | |
| "database =\"GISTstore/gist.db\"\n", | |
| "conn = sqlite3.connect(database)\n", | |
| "c = conn.cursor()\n", | |
| "c.execute(\"\"\"\n", | |
| "CREATE VIRTUAL TABLE IF NOT EXISTS gist \n", | |
| "USING FTS4(content, description, filename);\n", | |
| "\"\"\")\n", | |
| "c.close()\n", | |
| "conn.close()\n", | |
| "conn = sqlite3.connect(database)\n", | |
| "conn.text_factory=str \n", | |
| "c = conn.cursor()\n", | |
| "PATH = \"GISTstore/\"\n", | |
| "filename = \"post2gist.py\"\n", | |
| "content=open(PATH+filename, 'r').read()\n", | |
| "r = requests.post('https://api.github.com/gists',json.dumps({'files':{filename:{\"content\":content}}}),auth=requests.auth.HTTPBasicAuth(username, password)) \n", | |
| "sleep(2)\n", | |
| "Url= (r.json()['html_url'])\n", | |
| "c.execute(\"INSERT INTO gist VALUES (?,?,?)\", (content, Url, filename)) \n", | |
| "conn.commit()\n", | |
| "title = \"Index\"\n", | |
| "c.execute(\"INSERT INTO gist VALUES (?,?,?)\", (title, Url, filename)) \n", | |
| "conn.commit()\n", | |
| "for row in c.execute('SELECT rowid, content, description, filename FROM gist \\\n", | |
| " WHERE filename MATCH ?', (filename,)):\n", | |
| " print Url,\"\\n\",row[0],row[1],row[2],row[3]\n", | |
| "c.close()\n", | |
| "conn.close()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 69, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Writing TestText.txt\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%%writefile TestText.txt\n", | |
| "This file is created in order to test my text to speech scripts.\n", | |
| "I am wanting to use text to speech and the Jupyter Notebook to create youtube video.\n", | |
| "Converting text to mp3 is quite easy, however to directly convert to sound is not as simple." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 75, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "from gtts import gTTS\n", | |
| "import os\n", | |
| "filename = 'TestText.txt'\n", | |
| "content=open(filename, 'r').read().decode(\"utf-8\", \"ignore\")\n", | |
| "tts = gTTS(text=content, lang='en')\n", | |
| "tts.save(filename+\".mp3\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "from gtts import gTTS\n", | |
| "import os\n", | |
| "sd =open('/home/jack/Desktop/text_stuff/Wiki_HistoryofEarth.txt', 'r')\n", | |
| "rd = sd.read().decode(\"utf-8\", \"ignore\")\n", | |
| "tts = gTTS(text=rd, lang='en')\n", | |
| "tts.save(\"good.mp3\")\n", | |
| "os.system(\"mpg321 good.mp3\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 76, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "audio.mp3 good.mp3 hek.mp3 hello.mp3 play.mp3 test.mp3 TestText.txt.mp3\r\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "!ls *.mp3" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Overwriting GISTstore/Pmp3.py\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%%writefile GISTstore/Pmp3.py\n", | |
| "import pyglet\n", | |
| "def exit_callback(dt):\n", | |
| " pyglet.app.exit()\n", | |
| " return\n", | |
| "def pmp3(mp3File):\n", | |
| " music = pyglet.media.load(mp3File)\n", | |
| " music.play()\n", | |
| " pyglet.clock.schedule_once(exit_callback , music.duration)\n", | |
| " pyglet.app.run()\n", | |
| " return" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Ver 1.06\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "from GISTstore import Pmp3\n", | |
| "mp3File = 'audio.mp3'\n", | |
| "Pmp3.pmp3(mp3File)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 11, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Help on function schedule_once in module pyglet.clock:\n", | |
| "\n", | |
| "schedule_once(func, delay, *args, **kwargs)\n", | |
| " Schedule 'func' to be called once after 'delay' seconds (can be\n", | |
| " a float) on the default clock. The arguments passed to 'func' are\n", | |
| " 'dt' (time since last function call), followed by any ``*args`` and\n", | |
| " ``**kwargs`` given here.\n", | |
| " \n", | |
| " If no default clock is set, the func is queued and will be scheduled\n", | |
| " on the default clock as soon as it is created.\n", | |
| " \n", | |
| " :Parameters:\n", | |
| " `func` : function\n", | |
| " The function to call when the timer lapses.\n", | |
| " `delay` : float\n", | |
| " The number of seconds to wait before the timer lapses.\n", | |
| "\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "help(pyglet.clock.schedule_once)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# get mp3 length" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 24, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "17.424\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "from mutagen.mp3 import MP3\n", | |
| "audio = MP3(\"TestText.txt.mp3\")\n", | |
| "print audio.info.length" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "from GISTstore import Pmp3\n", | |
| "mp3File = 'audio.mp3'\n", | |
| "Pmp3.pmp3(mp3File)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "from GISTstore import Pmp3\n", | |
| "mp3File = 'TestText.txt.mp3'\n", | |
| "Pmp3.pmp3(mp3File)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import pyglet\n", | |
| "music = pyglet.media.load('TestText.txt.mp3')\n", | |
| "music.play()\n", | |
| "def exit_callback(dt):\n", | |
| " pyglet.app.exit()\n", | |
| "pyglet.clock.schedule_once(exit_callback , music.duration)\n", | |
| "pyglet.app.run()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "from gtts import gTTS\n", | |
| "import os\n", | |
| "sd =open('/home/jack/Desktop/text_stuff/Wiki_HistoryofEarth.txt', 'r')\n", | |
| "rd = sd.read().decode(\"utf-8\", \"ignore\")\n", | |
| "tts = gTTS(text=rd, lang='en')\n", | |
| "tts.save(\"good.mp3\")\n", | |
| "os.system(\"mpg321 good.mp3\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 19, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "from gtts import gTTS\n", | |
| "sd =open('/home/jack/Desktop/text_stuff/hek.txt', 'r')\n", | |
| "rd = sd.read().decode(\"utf-8\", \"ignore\")\n", | |
| "tts=gTTS(rd ,lang='en')\n", | |
| "tts.save(\"hek.mp3\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 9, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import pyttsx\n", | |
| "engine = pyttsx.init()\n", | |
| "voices = engine.getProperty('voices')\n", | |
| "engine.setProperty('voice', 'en-scottish')\n", | |
| "engine.say('The quick brown fox jumped over the lazy dog.')\n", | |
| "engine.runAndWait()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 10, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "cantonese\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "import pyttsx\n", | |
| "engine = pyttsx.init()\n", | |
| "voices = engine.getProperty('voices')\n", | |
| "engine.setProperty('voice', 'english_rp')\n", | |
| "print voice.id\n", | |
| "engine.say('The quick brown fox jumped over the lazy dog.')\n", | |
| "engine.runAndWait()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 11, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import pyttsx\n", | |
| "engine = pyttsx.init()\n", | |
| "#voices = engine.getProperty('voices')\n", | |
| "#engine.setProperty('voice', 'english_rp')\n", | |
| "#print voice.id\n", | |
| "engine.say('The quick brown fox jumped over the lazy dog.')\n", | |
| "engine.runAndWait()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import pyttsx\n", | |
| "engine = pyttsx.init()\n", | |
| "voices = engine.getProperty('voices')\n", | |
| "for voice in voices:\n", | |
| " engine.setProperty('voice', voice.id)\n", | |
| " print voice.id\n", | |
| " engine.say('The quick brown fox jumped over the lazy dog.')\n", | |
| "engine.runAndWait()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 17, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import pyttsx\n", | |
| "engine = pyttsx.init()\n", | |
| "engine.say('Sally sells seashells by the seashore.')\n", | |
| "engine.say('The quick brown fox jumped over the lazy dog.')\n", | |
| "engine.runAndWait()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 16, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import pyttsx\n", | |
| "engine = pyttsx.init()\n", | |
| "engine.say('Sally sells seashells by the seashore.')\n", | |
| "engine.say('The quick brown fox jumped over the lazy dog.')\n", | |
| "engine.runAndWait()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 14, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import pyttsx\n", | |
| "engine = pyttsx.init()\n", | |
| "rate = engine.getProperty('rate')\n", | |
| "engine.setProperty('rate', rate-30)\n", | |
| "engine.say('The quick brown fox jumped over the lazy dog.')\n", | |
| "engine.runAndWait()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "engine = pyttsx.init()\n", | |
| "voices = engine.getProperty('voices')\n", | |
| "for voice in voices:\n", | |
| " engine.setProperty('voice', voice.id)\n", | |
| " engine.say('The quick brown fox jumped over the lazy dog.')\n", | |
| "engine.runAndWait()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Help on module pyttsx.voice in pyttsx:\n", | |
| "\n", | |
| "NAME\n", | |
| " pyttsx.voice - Voice metadata definition.\n", | |
| "\n", | |
| "FILE\n", | |
| " /home/jack/Desktop/Processing/Processing/pyttsx/voice.py\n", | |
| "\n", | |
| "DESCRIPTION\n", | |
| " Copyright (c) 2009, 2013 Peter Parente\n", | |
| " \n", | |
| " Permission to use, copy, modify, and distribute this software for any\n", | |
| " purpose with or without fee is hereby granted, provided that the above\n", | |
| " copyright notice and this permission notice appear in all copies.\n", | |
| " \n", | |
| " THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES\n", | |
| " WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF\n", | |
| " MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR\n", | |
| " ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES\n", | |
| " WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN\n", | |
| " ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF\n", | |
| " OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\n", | |
| "\n", | |
| "CLASSES\n", | |
| " __builtin__.object\n", | |
| " Voice\n", | |
| " \n", | |
| " class Voice(__builtin__.object)\n", | |
| " | Methods defined here:\n", | |
| " | \n", | |
| " | __init__(self, id, name=None, languages=[], gender=None, age=None)\n", | |
| " | \n", | |
| " | __str__(self)\n", | |
| " | \n", | |
| " | ----------------------------------------------------------------------\n", | |
| " | Data descriptors defined here:\n", | |
| " | \n", | |
| " | __dict__\n", | |
| " | dictionary for instance variables (if defined)\n", | |
| " | \n", | |
| " | __weakref__\n", | |
| " | list of weak references to the object (if defined)\n", | |
| "\n", | |
| "\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "from pyttsx import voice\n", | |
| "help(voice)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Help on method getProperty in module pyttsx.engine:\n", | |
| "\n", | |
| "getProperty(self, name) method of pyttsx.engine.Engine instance\n", | |
| " Gets the current value of a property. Valid names and values include:\n", | |
| " \n", | |
| " voices: List of L{voice.Voice} objects supported by the driver\n", | |
| " voice: String ID of the current voice\n", | |
| " rate: Integer speech rate in words per minute\n", | |
| " volume: Floating point volume of speech in the range [0.0, 1.0]\n", | |
| " \n", | |
| " Numeric values outside the valid range supported by the driver are\n", | |
| " clipped.\n", | |
| " \n", | |
| " @param name: Name of the property to fetch\n", | |
| " @type name: str\n", | |
| " @return: Value associated with the property\n", | |
| " @rtype: object\n", | |
| " @raise KeyError: When the property name is unknown\n", | |
| "\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "import pyttsx\n", | |
| "engine = pyttsx.init()\n", | |
| "voices = engine.getProperty('voices')\n", | |
| "help(engine.getProperty)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 7, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import pyttsx\n", | |
| "engine = pyttsx.init()\n", | |
| "voices = engine.getProperty('voices')\n", | |
| "for voice in voices:\n", | |
| " engine.setProperty('voice', voice.id)\n", | |
| " engine.say('The quick brown fox jumped over the lazy dog.')\n", | |
| "engine.runAndWait()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 23, | |
| "metadata": { | |
| "scrolled": true | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "index-PDE2db.txt indexPDE2db.txt index.txt nov04.txt readthis.txt\r\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "!ls *.txt" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 12, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Help on class gTTS in module gtts.tts:\n", | |
| "\n", | |
| "class gTTS\n", | |
| " | gTTS (Google Text to Speech): an interface to Google's Text to Speech API\n", | |
| " | \n", | |
| " | Methods defined here:\n", | |
| " | \n", | |
| " | __init__(self, text, lang='en', slow=False, debug=False)\n", | |
| " | \n", | |
| " | save(self, savefile)\n", | |
| " | Do the Web request and save to `savefile`\n", | |
| " | \n", | |
| " | write_to_fp(self, fp)\n", | |
| " | Do the Web request and save to a file-like object\n", | |
| " | \n", | |
| " | ----------------------------------------------------------------------\n", | |
| " | Data and other attributes defined here:\n", | |
| " | \n", | |
| " | GOOGLE_TTS_URL = 'https://translate.google.com/translate_tts'\n", | |
| " | \n", | |
| " | LANGUAGES = {'af': 'Afrikaans', 'ar': 'Arabic', 'bn': 'Bengali', 'ca':...\n", | |
| " | \n", | |
| " | MAX_CHARS = 100\n", | |
| " | \n", | |
| " | Speed = <class gtts.tts.Speed>\n", | |
| "\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "from gtts import gTTS\n", | |
| "gTTS()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "http://pyttsx.readthedocs.io/en/latest/engine.html#changing-voices" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 10, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Help on module UnCamel:\n", | |
| "\n", | |
| "NAME\n", | |
| " UnCamel\n", | |
| "\n", | |
| "FILE\n", | |
| " /home/jack/Desktop/Processing/Processing/UnCamel.py\n", | |
| "\n", | |
| "DESCRIPTION\n", | |
| " # USAGE example:\n", | |
| " # import UnCamel\n", | |
| " # x = \"TimeToUncamelCaseTextUsingFunctions\"\n", | |
| " # UnCamel.uncamel(x)\n", | |
| "\n", | |
| "FUNCTIONS\n", | |
| " uncam(x)\n", | |
| " \n", | |
| " uncamel(x)\n", | |
| " # USAGE example:\n", | |
| " # import UnCamel\n", | |
| " # x = \"TimeToUncamelCaseTextUsingFunctions\"\n", | |
| " # UnCamel.uncamel(x)\n", | |
| "\n", | |
| "\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "import UnCamel\n", | |
| "help(UnCamel)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 12, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Writing GISTstore/UnCamel.py\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%%writefile GISTstore/UnCamel.py\n", | |
| "# USAGE example:\n", | |
| "# import UnCamel\n", | |
| "# x = \"TimeToUncamelCaseTextUsingFunctions\"\n", | |
| "# UnCamel.uncamel(x)\n", | |
| "def uncamel(x): \n", | |
| " return reduce(lambda a,b: a + ((b.upper() == b and (len(a) and a[-1].upper() != a[-1])) \n", | |
| " and (' ' + b) or b), x, '')\n", | |
| "def uncam(x):\n", | |
| " import re\n", | |
| " return re.sub(\"([a-z])([A-Z])\",\"\\g<1> \\g<2>\",x)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'Let Us Try To Uncamel Case Text Using Functions'" | |
| ] | |
| }, | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "def uncamel(x): \n", | |
| " return reduce(lambda a,b: a + ((b.upper() == b and \n", | |
| " (len(a) and a[-1].upper() != a[-1])) and \n", | |
| " (' ' + b) or b), x, '')\n", | |
| "x = \"LetUsTryToUncamelCaseTextUsingFunctions\"\n", | |
| "uncamel(x)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# Github API" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "{\n", | |
| " \"current_user_url\": \"https://api.github.com/user\",\n", | |
| " \"current_user_authorizations_html_url\": \"https://github.com/settings/connections/applications{/client_id}\",\n", | |
| " \"authorizations_url\": \"https://api.github.com/authorizations\",\n", | |
| " \"code_search_url\": \"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}\",\n", | |
| " \"commit_search_url\": \"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}\",\n", | |
| " \"emails_url\": \"https://api.github.com/user/emails\",\n", | |
| " \"emojis_url\": \"https://api.github.com/emojis\",\n", | |
| " \"events_url\": \"https://api.github.com/events\",\n", | |
| " \"feeds_url\": \"https://api.github.com/feeds\",\n", | |
| " \"followers_url\": \"https://api.github.com/user/followers\",\n", | |
| " \"following_url\": \"https://api.github.com/user/following{/target}\",\n", | |
| " \"gists_url\": \"https://api.github.com/gists{/gist_id}\",\n", | |
| " \"hub_url\": \"https://api.github.com/hub\",\n", | |
| " \"issue_search_url\": \"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}\",\n", | |
| " \"issues_url\": \"https://api.github.com/issues\",\n", | |
| " \"keys_url\": \"https://api.github.com/user/keys\",\n", | |
| " \"notifications_url\": \"https://api.github.com/notifications\",\n", | |
| " \"organization_repositories_url\": \"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}\",\n", | |
| " \"organization_url\": \"https://api.github.com/orgs/{org}\",\n", | |
| " \"public_gists_url\": \"https://api.github.com/gists/public\",\n", | |
| " \"rate_limit_url\": \"https://api.github.com/rate_limit\",\n", | |
| " \"repository_url\": \"https://api.github.com/repos/{owner}/{repo}\",\n", | |
| " \"repository_search_url\": \"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}\",\n", | |
| " \"current_user_repositories_url\": \"https://api.github.com/user/repos{?type,page,per_page,sort}\",\n", | |
| " \"starred_url\": \"https://api.github.com/user/starred{/owner}{/repo}\",\n", | |
| " \"starred_gists_url\": \"https://api.github.com/gists/starred\",\n", | |
| " \"team_url\": \"https://api.github.com/teams\",\n", | |
| " \"user_url\": \"https://api.github.com/users/{user}\",\n", | |
| " \"user_organizations_url\": \"https://api.github.com/user/orgs\",\n", | |
| " \"user_repositories_url\": \"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}\",\n", | |
| " \"user_search_url\": \"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}\"\n", | |
| "}\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 34, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "/home/jack/Desktop/jupnote/snippet.db\n", | |
| "/home/jack/Desktop/text_stuff/snippet.db\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "!locate snippet.db" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 39, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "notebooks.db pde2db.db pdefts4.db snippet.db\r\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "!ls *.db" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 40, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Search : \n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "import sqlite3\n", | |
| "import sys\n", | |
| "conn = sqlite3.connect('/home/jack/Desktop/text_stuff/snippet.db')\n", | |
| "conn.text_factory = str\n", | |
| "c = conn.cursor()\n", | |
| "count=0;req=200\n", | |
| "search = raw_input(\"Search : \")\n", | |
| "for row in c.execute('SELECT rowid, * FROM snippet WHERE snippet MATCH ?', (search,)): \n", | |
| " count=count+1\n", | |
| " print \"ID : \",(row)[0],(row)[2],\" -- KEYWORDS\",(row)[3],\"\\n\"\n", | |
| " if count > req:\n", | |
| " conn.close()\n", | |
| " sys.exit()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 41, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Search : \n", | |
| "ID : 1 \n", | |
| "delimiters = ['\\n', ' ', ',', '.', '?', '!', ':', 'and_what_else_you_need']\n", | |
| "words = content\n", | |
| "for delimiter in delimiters:\n", | |
| " new_words = []\n", | |
| " for word in words:\n", | |
| " new_words += word.split(delimiter)\n", | |
| " words = new_words\n", | |
| " -- KEYWORDS words, delimiter, split \n", | |
| "\n", | |
| "ID : 2 \n", | |
| "def insert_info(store):\n", | |
| " with sqlite3.connect(\"misc.db\") as db:\n", | |
| " #use a text_factory that can interpret 8-bit bytestrings \n", | |
| " db.text_factory = str\n", | |
| " cursor = db.cursor()\n", | |
| " #db.text_factory = str\n", | |
| " sql = \"insert into storeit (data0, data1, data2) values (?, ?, ?)\"\n", | |
| " cursor.execute(sql, store)\n", | |
| " db.commit()\n", | |
| " \n", | |
| " OR\n", | |
| " conn.text_factory = str\n", | |
| " -- KEYWORDS text_factory, 8-bit bytestrings, 8-bit \n", | |
| "\n", | |
| "ID : 3 \n", | |
| "import sqlite3\n", | |
| "import sys\n", | |
| "conn = sqlite3.connect('snippet.db')\n", | |
| "conn.text_factory = str\n", | |
| "c = conn.cursor()\n", | |
| "count=0\n", | |
| "req=200\n", | |
| "search = raw_input(\"Search\")\n", | |
| "#for row in c.execute('SELECT rowid,* FROM tweets WHERE text MATCH %s' % search):\n", | |
| "for row in c.execute('SELECT * FROM snippet WHERE keywords MATCH ?', (search,)): \n", | |
| " count=count+1\n", | |
| " #print count,\"by\",(row)[2],\"\n", | |
| "\",(row)[1],\"\n", | |
| "\"\n", | |
| " print count,\"-\",(row)[1],\" -- by\",(row)[2],\"\n", | |
| "\"\n", | |
| " if count > req:\n", | |
| " conn.close()\n", | |
| " sys.exit()\n", | |
| " -- KEYWORDS text_factory, 8-bit bytestrings, 8-bit \n", | |
| "\n", | |
| "ID : 4 \n", | |
| "with open(\"Use.txt\",'r') as f:\n", | |
| " get_all=f.readlines()\n", | |
| "\n", | |
| "----------------\n", | |
| "with open(\"file.txt\",'w') as f:\n", | |
| " for i,line in enumerate(get_all,1): ## STARTS THE NUMBERING FROM 1 (by default it begins with 0) \n", | |
| " if i == 2: ## OVERWRITES line:2\n", | |
| " f.writelines(\"XXXXXXXXXXXXXXXXXXXXXXX\n", | |
| "\")\n", | |
| " else:\n", | |
| " f.writelines(line)\n", | |
| " -- KEYWORDS enumerate, edit line, edit, edit line number \n", | |
| "\n", | |
| "ID : 5 \n", | |
| "%%writefile Key.py\n", | |
| "def twiter():\n", | |
| " CONSUMER_KEY = 'WWWWWWWWWWWWWWWW'\n", | |
| " CONSUMER_SECRET = 'XXXXXXXXXXXXXXXXX'\n", | |
| " ACCESS_KEY = 'YYYYYYYYYYYYYYYYYYY'\n", | |
| " ACCESS_SECRET = 'ZZZZZZZZZZZZZZZZ'\n", | |
| " twir = (CONSUMER_KEY, CONSUMER_SECRET, ACCESS_KEY, ACCESS_SECRET)\n", | |
| " return twir\n", | |
| " -- KEYWORDS Key, Key.py, twitter api, Twitter API \n", | |
| "\n", | |
| "ID : 6 \n", | |
| "from itertools import tee\n", | |
| "count=0\n", | |
| "with open(\"symmetrymag_tweets.csv\") as inf:\n", | |
| " for line in inf:\n", | |
| " lines = line[39:]\n", | |
| " outf = open(\"symmetrymag.txt\", \"a\") \n", | |
| " outf.write(lines)\n", | |
| "outf.close() \n", | |
| " -- KEYWORDS CSV, clean csv, edit csv, prepare for database \n", | |
| "\n", | |
| "ID : 7 \n", | |
| "search = raw_input(\"find : \")\n", | |
| "file = open(\"symmetrymag.txt\")\n", | |
| "lines = file.readlines()\n", | |
| "for line in lines:\n", | |
| " if search in line:print line\n", | |
| " if search == True:\n", | |
| " file.close()\n", | |
| " exit()\n", | |
| "file.close()\n", | |
| "\n", | |
| " -- KEYWORDS search, search text file, textfile, edit textfile \n", | |
| "\n", | |
| "ID : 8 \n", | |
| "import sqlite3\n", | |
| "import time\n", | |
| "#account = \"TEDTalks.txt\"\n", | |
| "account = \"symmetrymag.txt\"\n", | |
| "#account = \"elonmusk.txt\"\n", | |
| "#account = \"realDonaldTrump.txt\"\n", | |
| "user = account[:-4]\n", | |
| "lines = open(account,\"r\")\n", | |
| "line = lines.readline()\n", | |
| "for line in lines:\n", | |
| " conn = sqlite3.connect('collection.db')\n", | |
| " conn.text_factory = str\n", | |
| " c = conn.cursor()\n", | |
| " c.execute(\"INSERT INTO tweets VALUES (?,?)\", (line, user)) \n", | |
| " conn.commit()\n", | |
| " conn.close() \n", | |
| " \n", | |
| " #print line \n", | |
| "\n", | |
| "conn.commit()\n", | |
| "conn.close() \n", | |
| " -- KEYWORDS insert data, text file to database, text to data \n", | |
| "\n", | |
| "ID : 9 \n", | |
| "import Key\n", | |
| "from random import randint\n", | |
| "\n", | |
| "#Twitter API credentials\n", | |
| "consumer_key = Key.twiter()[0]\n", | |
| "consumer_secret = Key.twiter()[1]\n", | |
| "access_key = Key.twiter()[2]\n", | |
| "access_secret = Key.twiter()[3]\n", | |
| "\n", | |
| "\n", | |
| "def get_all_tweets(screen_name):\n", | |
| " #Twitter only allows access to a users most recent 3240 tweets with this method\n", | |
| "\n", | |
| " #authorize twitter, initialize tweepy\n", | |
| " auth = tweepy.OAuthHandler(consumer_key, consumer_secret)\n", | |
| " auth.set_access_token(access_key, access_secret)\n", | |
| " api = tweepy.API(auth)\n", | |
| "\n", | |
| " -- KEYWORDS Key , using Key.py, using Key, API key \n", | |
| "\n", | |
| "ID : 10 \n", | |
| "conn = GetSqliteConnection(db_path)\n", | |
| "conn.text_factory = lambda x: unicode(x, 'utf-8', 'ignore')\n", | |
| " -- KEYWORDS text_factory , utf-8, unicode, lambda \n", | |
| "\n", | |
| "ID : 11 \n", | |
| "%%writefile /home/jack/hidden/Authorize.py\n", | |
| "def keys():\n", | |
| " ftp = \"ftp.MYsite.com\"\n", | |
| " username = \"Josephine\"\n", | |
| " password = \"WhaWah2525\"\n", | |
| " login = (ftp,username,password)\n", | |
| " return login\n", | |
| " ------------\n", | |
| "import sys\n", | |
| "sys.path.insert(0, \"/home/jack/hidden\"\n", | |
| "import Authorize\n", | |
| "ftp = Authorize.keys()[0]\n", | |
| "username = Authorize.keys()[1]\n", | |
| "password = Authorize.keys()[2]\n", | |
| "\n", | |
| "print ftp, username, password \n", | |
| " \n", | |
| " -- KEYWORDS Authorize, ftp, hidden passwords, Authorize.keys \n", | |
| "\n", | |
| "ID : 12 \n", | |
| "!sqlite3 ipydb.db \"pragma integrity_check;\"\n", | |
| " -- KEYWORDS verify database, pragma, integrity_check, check database \n", | |
| "\n", | |
| "ID : 13 \n", | |
| "import base64\n", | |
| "string=\"This is the text above Encoded Base64\"\n", | |
| "encodedlistvalue=base64.b64encode(string)\n", | |
| "\n", | |
| "string = encodedlistvalue\n", | |
| "print encodedlistvalue,\"\n", | |
| "\",base64.b64decode(string)\n", | |
| " -- KEYWORDS base64, encoding base64, decoding base64, Encoded Base64 \n", | |
| "\n", | |
| "ID : 14 \n", | |
| "import base64 #encode muliple lines and keep the format\n", | |
| "string = \n", | |
| "╲╲╭━━━━━━━╮╱╱\n", | |
| "╲╭╯╭━╮┈╭━╮╰╮╱\n", | |
| "╲┃┈┃┈▊┈┃┈▊┈┃╱\n", | |
| "╲┃┈┗━┛┈┗━┛┈┃╱\n", | |
| "╱┃┈┏━━━━━┓┈┃╲\n", | |
| "╱┃┈┃┈┈╭━╮┃┈┃╲\n", | |
| "╱╰╮╰━━┻━┻╯╭╯╲\n", | |
| "╱╱╰━━━━━━━╯╲╲ FROM: http://copy.r74n.com/ascii-art\n", | |
| "EncodedStringValue=base64.b64encode(string)\n", | |
| "string2 = EncodedStringValue\n", | |
| "print \"Decoded String2 : \",base64.b64decode(string2)\n", | |
| " -- KEYWORDS base64, encoding base64, decoding base64, Encoded Base64 \n", | |
| "\n", | |
| "ID : 15 \n", | |
| "import sqlite3\n", | |
| "conn = sqlite3.connect('ipydb.db')\n", | |
| "c = conn.cursor()# Never \n", | |
| "for row in c.execute('SELECT * FROM python ORDER BY code'):\n", | |
| " print\"entry :\",(row[0]).encode('ascii'),\"\n", | |
| "\"\n", | |
| " print\"keywords :\",(row[1]).encode('ascii'),\"\n", | |
| "-----\n", | |
| "\",\"\n", | |
| "\" \n", | |
| " #data = c.fetchall()\n", | |
| " #print data\n", | |
| " -- KEYWORDS ascii, encode('ascii'), fetchall, sqlite3 \n", | |
| "\n", | |
| "ID : 16 \n", | |
| "import sqlite3\n", | |
| "import feedparser\n", | |
| "import time\n", | |
| "import sqlite3\n", | |
| "Dbase = 'bigfeedfts.db'\n", | |
| "conn = sqlite3.connect(Dbase)\n", | |
| "c = conn.cursor()\n", | |
| "c.execute(\n", | |
| "CREATE VIRTUAL TABLE IF NOT EXISTS bbctech \n", | |
| "USING FTS3(head, feed);\n", | |
| ")\n", | |
| "count=0\n", | |
| "while count<35:\n", | |
| " count=count+1\n", | |
| " if count==1:feed='http://feeds.bbci.co.uk/news/technology/rss.xml'\n", | |
| " if count==2:feed='http://www.cbn.com/cbnnews/us/feed/'\n", | |
| " d = feedparser.parse(feed)\n", | |
| " for post in d.entries:\n", | |
| " aa = `d['feed']['title'],d['feed']['link'],d.entries[0]['link']`\n", | |
| " bb = `post.title + \": \" + post.link + \"\"`\n", | |
| " conn = sqlite3.connect(Dbase)\n", | |
| " c = conn.cursor()\n", | |
| " c.execute(\"INSERT INTO bbctech VALUES (?,?)\", (aa,bb))\n", | |
| " conn.commit()\n", | |
| " conn.close()\n", | |
| " \n", | |
| " \n", | |
| "conn = sqlite3.connect(Dbase)\n", | |
| "c = conn.cursor()# Never\n", | |
| "count=0\n", | |
| "for row in c.execute('SELECT * FROM bbctech ORDER BY rowid DESC'):\n", | |
| " row=str(row)\n", | |
| " row=row.replace(\"(u\",\"\");row=row.replace('\", u\"u',\"\n", | |
| "\")\n", | |
| " row=row.replace(\"/', u'\",\" \");row=row.replace('\"',\"\")\n", | |
| " row=row.replace(\"', u'\",\" \");row=row.replace(\"')\",\" \")\n", | |
| " row=row.replace(\"'\",\"\");row=row.replace(\" , uu\",\"\n", | |
| "\")\n", | |
| " count=count+1\n", | |
| " print\"\n", | |
| "Number :\",count,\" -----\n", | |
| "\",(row)\n", | |
| " -- KEYWORDS rss, RSS, rss feeds, sqlite3 \n", | |
| "\n", | |
| "ID : 17 \n", | |
| "import sqlite3\n", | |
| "import sys\n", | |
| "conn = sqlite3.connect('bigfeedfts.db')\n", | |
| "c = conn.cursor()\n", | |
| "count=0\n", | |
| "# Limited Amount of Results to 100\n", | |
| "req=100\n", | |
| "term = raw_input(\"Search Term : \")\n", | |
| "for row in c.execute(\"SELECT * FROM bbctech WHERE feed MATCH ?\", (term,)):\n", | |
| " row=str(row)\n", | |
| " row=row.replace(\"(u\"(u\",\"\");row=row.replace(\"', u'\",\" \");\n", | |
| " row=row.replace(\"u'\",\" \");row=row.replace(')\", u\" ', \"\n", | |
| "\");\n", | |
| " row=row.replace(\" http://\",\"\n", | |
| "http://\");row=row.replace('\")','')\n", | |
| " row=row.replace(\"'\",\"\");row=row.replace(\"#tk.rss_all\", \"\")\n", | |
| " count=count+1\n", | |
| " print \"\n", | |
| "\",count,\"-----\n", | |
| "\",(row)\n", | |
| " if count > req:\n", | |
| " conn.close()\n", | |
| " sys.exit()\n", | |
| " -- KEYWORDS search rss, RSS, rss feeds, search sqlite3 \n", | |
| "\n", | |
| "ID : 18 \n", | |
| "#This is a great search\n", | |
| "#Much better that a regular browser search. \n", | |
| "#This search keeps a google page that may be scrapedor gleaned or just used as a hyperlink documant. \n", | |
| "#Results are fantastic.\n", | |
| "from bs4 import BeautifulSoup\n", | |
| "import requests\n", | |
| "url = u'https://www.google.com/search?num=30&newwindow=1&client=ubuntu&channel=fs&btnG=Search&q='\n", | |
| "query = u'Trump, idiot '\n", | |
| "r = requests.get(url+query)\n", | |
| "soup = BeautifulSoup(r.text, 'html.parser')\n", | |
| "for list in soup:\n", | |
| " print list\n", | |
| "text = str(list)\n", | |
| "html_str = text\n", | |
| "Html_file= open(\"filename4.html\",\"w\")\n", | |
| "Html_file.write(html_str)\n", | |
| "Html_file.close()#This is a great search\n", | |
| "#Much better that a regular browser search. \n", | |
| "#This search keeps a google page that may be scrapedor gleaned or just used as a hyperlink documant. \n", | |
| "#Results are fantastic.\n", | |
| "from bs4 import BeautifulSoup\n", | |
| "import requests\n", | |
| "url = u'https://www.google.com/search?num=30&newwindow=1&client=ubuntu&channel=fs&btnG=Search&q='\n", | |
| "query = u'Trump, idiot '\n", | |
| "r = requests.get(url+query)\n", | |
| "soup = BeautifulSoup(r.text, 'html.parser')\n", | |
| "for list in soup:\n", | |
| " print list\n", | |
| "text = str(list)\n", | |
| "html_str = text\n", | |
| "Html_file= open(\"filename4.html\",\"w\")\n", | |
| "Html_file.write(html_str)\n", | |
| "Html_file.close()\n", | |
| "-----\n", | |
| "READ THE HTML\n", | |
| "from lxml import html\n", | |
| "print html.parse('filename4.html').xpath('//body')[0].text_content()\n", | |
| " -- KEYWORDS google search, parse html, html, read html \n", | |
| "\n", | |
| "ID : 19 \n", | |
| "from PIL import Image, ImageFont\n", | |
| "import GenIm\n", | |
| "\n", | |
| "img = Image.open('tmmpp/HURRICANE_01.png')\n", | |
| "position = (340,600)\n", | |
| "text= \"JackNorthrup_ImageBot\"\n", | |
| "font = ImageFont.truetype(\"/home/jack/.fonts/Exo-Black.ttf\", 25)\n", | |
| "col = (255,255,255,150)\n", | |
| "halo_col = (0,0,0)\n", | |
| "newim = GenIm.Draw_text_with_halo(img, position, text, font, col, halo_col)\n", | |
| "newim.save(\"tmmpp/HURRICANE_02.png\")\n", | |
| "!showme tmmpp/HURRICANE_02.png\n", | |
| " -- KEYWORDS text on an image, image, text image, text, words on image \n", | |
| "\n", | |
| "ID : 20 \n", | |
| "from itertools import tee\n", | |
| "count=0\n", | |
| "with open(\"realDonaldTrump_tweets.csv\") as inf:\n", | |
| " # set up iterators\n", | |
| " cfg,res = tee(inf)\n", | |
| " # advance cfg by four lines\n", | |
| " for i in range(4):\n", | |
| " next(cfg)\n", | |
| "\n", | |
| " for c,r in zip(cfg, res):\n", | |
| " count=count+1\n", | |
| " if \"campaign\" in c:\n", | |
| " #print \"Date :\",c[21:]\n", | |
| " print c[39:]\n", | |
| " -- KEYWORDS cvs, remove characters, text edit, clean, clean text \n", | |
| "\n", | |
| "ID : 21 \n", | |
| "import tweepy #https://github.com/tweepy/tweepy\n", | |
| "import csv\n", | |
| "import sys\n", | |
| "sys.path.insert(0,\"/home/jack/anaconda2/envs/py27/lib/python2.7/site-packages\")\n", | |
| "import Key\n", | |
| "from random import randint\n", | |
| "consumer_key = Key.twiter()[0]\n", | |
| "consumer_secret = Key.twiter()[1]\n", | |
| "access_key = Key.twiter()[2]\n", | |
| "access_secret = Key.twiter()[3]\n", | |
| "\n", | |
| "def get_all_tweets(screen_name):\n", | |
| " auth = tweepy.OAuthHandler(consumer_key, consumer_secret)\n", | |
| " auth.set_access_token(access_key, access_secret)\n", | |
| " api = tweepy.API(auth)\n", | |
| " alltweets = []\t\n", | |
| " new_tweets = api.user_timeline(screen_name = screen_name,count=200)\n", | |
| " alltweets.extend(new_tweets)\n", | |
| " oldest = alltweets[-1].id - 1\n", | |
| " while len(new_tweets) > 0:\n", | |
| " print \"getting tweets before %s\" % (oldest)\n", | |
| " new_tweets = api.user_timeline(screen_name = screen_name,count=200,max_id=oldest)\n", | |
| " alltweets.extend(new_tweets)\n", | |
| " oldest = alltweets[-1].id - 1\n", | |
| " print (len(alltweets))\n", | |
| " if (len(alltweets)) >200:\n", | |
| " outtweets = [[tweet.id_str, tweet.created_at, tweet.text.encode(\"utf-8\")] for tweet in alltweets]\n", | |
| " with open('%s_tweets.csv' % screen_name, 'wb') as f:\n", | |
| " writer = csv.writer(f)\n", | |
| " writer.writerow([\"id\",\"created_at\",\"text\"])\n", | |
| " writer.writerows(outtweets)\n", | |
| " pass\n", | |
| "if __name__ == '__main__':\n", | |
| " USER = raw_input(\"User : \") or \"CNN\"\n", | |
| " get_all_tweets(USER)\n", | |
| "--- \n", | |
| "get all tweets, csv, get many tweets, tweets to csv, tweets.csv \n", | |
| " -- KEYWORDS get all tweets, csv, get many tweets, tweets to csv, tweets.csv \n", | |
| "\n", | |
| "ID : 22 \n", | |
| "# TOP ONE IS BEST\n", | |
| "textin= open('hashtag.txt', 'r')\n", | |
| "lines = textin.readlines()\n", | |
| "for line in lines:\n", | |
| " time.sleep(1)\n", | |
| " print line\n", | |
| "---\n", | |
| "import time\n", | |
| "textin= open('hashtag.txt', 'r')\n", | |
| "lines = textin.read()\n", | |
| "time.sleep(1)\n", | |
| "print lines,\n", | |
| "---\n", | |
| "textin= open('hashtag.txt', 'r')\n", | |
| "lines = textin.read().splitlines()\n", | |
| "time.sleep(1)\n", | |
| "print lines,\n", | |
| "---\n", | |
| "read line by line, read(), splitlines(), read().splitlines(), read files \n", | |
| " -- KEYWORDS read line by line, read(), splitlines(), read().splitlines(), read files \n", | |
| "\n", | |
| "ID : 23 \n", | |
| "import markovify\n", | |
| "f = open(\"grimm.txt\")\n", | |
| "text = f.read()\n", | |
| "text_model_a = markovify.Text(text)\n", | |
| "\n", | |
| "\n", | |
| "ebook_b =open('hekel.txt')\n", | |
| "text0 = ebook_b.read()\n", | |
| "text_model_b = markovify.Text(text0)\n", | |
| "for i in range(5):\n", | |
| " print(text_model_b.make_short_sentence(140))\n", | |
| " STR0 = (text_model_b.make_short_sentence(140))\n", | |
| " savE = open('savE.txt', 'a')\n", | |
| " savE.write(STR0)\n", | |
| " savE.close()\n", | |
| "\n", | |
| "# 2. Print five randomly-generated sentences\n", | |
| "for i in range(5):\n", | |
| " print(text_model_a.make_short_sentence(140))\n", | |
| " STR = (text_model_a.make_short_sentence(140))\n", | |
| " savE = open('savE.txt', 'a')\n", | |
| " savE.write(STR)\n", | |
| " savE.close()\n", | |
| "# 3. Print three randomly-generated sentences of no more than 140 characters\n", | |
| "for i in range(5):\n", | |
| " print(text_model_a.make_short_sentence(140))\n", | |
| " STR2 = (text_model_a.make_short_sentence(140))\n", | |
| " savE = open('savE.txt', 'a')\n", | |
| " savE.write(STR2)\n", | |
| " savE.close()\n", | |
| "# Combine the models into a single one\n", | |
| "both_models = markovify.combine([text_model_a,text_model_b])\n", | |
| "for i in range(5):\n", | |
| " print(both_models.make_short_sentence(140)) \n", | |
| " STR3 = (both_models.make_short_sentence(140)) \n", | |
| " savE = open('savE.txt', 'a')\n", | |
| " savE.write(STR3)\n", | |
| " savE.close() \n", | |
| "---\n", | |
| "markovify , combine , text_model_a ,\n", | |
| " -- KEYWORDS markovify, combine, text_model_a, markovify, generated sentences \n", | |
| "\n", | |
| "ID : 24 \n", | |
| "Using shutil\n", | |
| "\n", | |
| "from shutil import copyfile\n", | |
| "copyfile(src, dst)\n", | |
| "----\n", | |
| "# Python Copy File - Sample Code\n", | |
| "from shutil import copyfile\n", | |
| "from sys import exit\n", | |
| "source = input(\"Enter source file with full path: \")\n", | |
| "target = input(\"Enter target file with full path: \")\n", | |
| "# adding exception handling\n", | |
| "try:\n", | |
| " copyfile(source, target)\n", | |
| "except IOError as e:\n", | |
| " print(\"Unable to copy file. %s\" % e)\n", | |
| " exit(1)\n", | |
| "except:\n", | |
| " print(\"Unexpected error:\", sys.exc_info())\n", | |
| " exit(1)\n", | |
| "print(\"\n", | |
| "File copy done!\n", | |
| "\")\n", | |
| "while True:\n", | |
| " print(\"Do you like to print the file ? (y/n): \")\n", | |
| " check = input()\n", | |
| " if check == 'n':\n", | |
| " break\n", | |
| " elif check == 'y':\n", | |
| " file = open(target, \"r\")\n", | |
| " print(\"\n", | |
| "Here follows the file content:\n", | |
| "\")\n", | |
| " print(file.read())\n", | |
| " file.close()\n", | |
| " print()\n", | |
| " break\n", | |
| " else:\n", | |
| " continue\n", | |
| " -- KEYWORDS copy, copy file, copy a file, from shutil import copyfile, shutil, copyfile \n", | |
| "\n", | |
| "ID : 25 \n", | |
| "how to turn on the keyboard backlight\n", | |
| "\n", | |
| "USE: xset led on\n", | |
| "\n", | |
| "keyboard backlight\n", | |
| " -- KEYWORDS how to turn on backlight, turn on backlight, keyboard backlight, keyboard, backlight \n", | |
| "\n", | |
| "ID : 26 \n", | |
| "import sqlite3\n", | |
| "import os.path\n", | |
| "from os import listdir, getcwd\n", | |
| "from IPython.core.display import Image \n", | |
| "\n", | |
| "def get_picture_list(rel_path):\n", | |
| " abs_path = os.path.join(os.getcwd(),rel_path)\n", | |
| " print 'abs_path =', abs_path\n", | |
| " dir_files = os.listdir(abs_path)\n", | |
| " #print dir_files\n", | |
| " return dir_files\n", | |
| "\n", | |
| "picture_list = get_picture_list('snippets')\n", | |
| "print picture_list\n", | |
| "------------\n", | |
| "get_picture_list, get list of files, get a file list in memory\n", | |
| "files in memeory , file list memory \n", | |
| " -- KEYWORDS get_picture_list, get list of files, get a file list in memory \n", | |
| "\n", | |
| "ID : 27 \n", | |
| "import sqlite3\n", | |
| "import sys\n", | |
| "conn = sqlite3.connect('pdfs.db')\n", | |
| "conn.text_factory = str\n", | |
| "c = conn.cursor()\n", | |
| "count=0;req=200\n", | |
| "search = raw_input(\"Search : \")\n", | |
| "for row in c.execute('SELECT rowid, text FROM pdfs WHERE text MATCH ?', (search,)): \n", | |
| " count=count+1\n", | |
| " print (row)[0],\"-\",(row)[1],\n", | |
| " if count > req:\n", | |
| " conn.close()\n", | |
| " sys.exit()\n", | |
| " \n", | |
| " --------\n", | |
| " search database, search db, raw_input, SQLite search\n", | |
| " \n", | |
| " -- KEYWORDS search database, search db, raw_input, SQLite search \n", | |
| "\n", | |
| "ID : 28 \n", | |
| "Creating a MySQL database with Python\n", | |
| "If you don't have it you may need:\n", | |
| "sudo apt-get install libmysqlclient-dev\n", | |
| "\n", | |
| "# Works\n", | |
| "import MySQLdb as db\n", | |
| "con = db.connect(\"localhost\",\"root\",\"\")\n", | |
| "cur = con.cursor()\n", | |
| "cur.execute('CREATE DATABASE searchdb01;')\n", | |
| "\n", | |
| " -- KEYWORDS MySQL, Creating a MySQL database, libmysqlclient-dev, searchdb01 \n", | |
| "\n", | |
| "ID : 29 \n", | |
| "# works \n", | |
| "import MySQLdb as db\n", | |
| "import json \n", | |
| "import base64\n", | |
| " \n", | |
| "con = db.connect(\"localhost\",\"root\",\"ThinkPadT$#\", \"searchdb01\")\n", | |
| "file = \"\"[mylist.jsn\n", | |
| "while 1:\n", | |
| " line = file.readline()\n", | |
| " if not line:\n", | |
| " break\n", | |
| " pass # do something \n", | |
| "#listname='mylist.json'\n", | |
| "#stringlistvalue=json.dumps(listname)\n", | |
| "\"\"\n", | |
| "keywords = \"\"\n", | |
| "database, code, python, lesson 1, Oh234\n", | |
| "\"\"\n", | |
| "encodedlistvalue=base64.b64encode(file)\n", | |
| "with con:\n", | |
| " cur = con.cursor()\n", | |
| " cur.execute(\"CREATE TABLE Code(Id INT PRIMARY KEY AUTO_INCREMENT, Name VARCHAR(2500), Keywords VARCHAR(500))\")\n", | |
| " #cur.execute(\"INSERT INTO Code(Name) VALUES('%s')\" % (encodedlistvalue))\n", | |
| " cur.execute(\"INSERT INTO Code(Name, Keywords) VALUES('%s','%s')\" % (encodedlistvalue, keywords))\n", | |
| " -- KEYWORDS MySQL, Creating a MySQL database, libmysqlclient-dev, searchdb01 \n", | |
| "\n", | |
| "ID : 30 \n", | |
| "#!/usr/bin/python\n", | |
| "import MySQLdb\n", | |
| "import sys\n", | |
| "import base64\n", | |
| "con = db.connect(\"localhost\",\"root\",\" \", \"searchdb01\")\n", | |
| "cur = con.cursor()\n", | |
| "# execute the SQL query using execute() method.\n", | |
| "cur.execute (\"select Id, Name, Keywords from Code\")\n", | |
| "\n", | |
| "data = cur.fetchall ()\n", | |
| "# print the rows\n", | |
| "for row in data :\n", | |
| " encodedlistvalue=base64.b64decode(row[1])\n", | |
| " print row[0], encodedlistvalue, '\n", | |
| "', 'Keywords:', row[2], '\n", | |
| " -----------------------------\n", | |
| "'\n", | |
| "# close the cursor object\n", | |
| "cur.close ()\n", | |
| "# close the connection\n", | |
| "con.close ()\n", | |
| "# exit the program\n", | |
| "sys.exit()\n", | |
| " -- KEYWORDS MySQL, base64 , b64decode, libmysqlclient-dev, searchdb01 \n", | |
| "\n", | |
| "ID : 31 \n", | |
| "import MySQLdb\n", | |
| "con = db.connect(\"localhost\",\"root\",\"ThinkPadT$#\", \"searchdb01\")\n", | |
| "\n", | |
| "param = \"lesson\"\n", | |
| "c = con.cursor()\n", | |
| "c.execute(\"SELECT * FROM Code WHERE Keywords LIKE %s LIMIT 1\", (\"%\" + param + \"%\",))\n", | |
| "\n", | |
| "data = c.fetchall()\n", | |
| "for row in data :\n", | |
| " encodedlistvalue=base64.b64decode(row[1])\n", | |
| " print row[0], encodedlistvalue, '\n", | |
| "', 'Keywords:', row[2], '\n", | |
| " -----------------------------\n", | |
| "'\n", | |
| "c.close()\n", | |
| " -- KEYWORDS MySQL, base64 , b64decode, libmysqlclient-dev, searchdb01 \n", | |
| "\n", | |
| "ID : 32 \n", | |
| "def createdb(dbnew):\n", | |
| " import sqlite3\n", | |
| " conn = sqlite3.connect(dbnew)\n", | |
| " c = conn.cursor()\n", | |
| " \n", | |
| " query1 = \"DROP TABLE IF EXISTS Junk\"\n", | |
| " query2 = \"\"CREATE TABLE IF NOT EXISTS Junk(\n", | |
| " \"language\" VARCHAR(32) NOT NULL,\n", | |
| " \"keywords\" VARCHAR(500) default NULL,\n", | |
| " \"script\" VARCHAR(2500) default NULL\n", | |
| " )\n", | |
| " \"\"\n", | |
| " c.execute(query1)\n", | |
| " c.execute(query2)\n", | |
| " \n", | |
| "dbnew = \"newdb.db\" \n", | |
| "createdb(dbnew) \n", | |
| " -- KEYWORDS function to create a database, function, database function \n", | |
| "\n", | |
| "ID : 33 \n", | |
| "import sqlite3\n", | |
| "import os.path\n", | |
| "from os import listdir, getcwd\n", | |
| "from IPython.core.display import Image \n", | |
| "\n", | |
| "def get_picture_list(rel_path):\n", | |
| " abs_path = os.path.join(os.getcwd(),rel_path)\n", | |
| " print 'abs_path =', abs_path\n", | |
| " dir_files = os.listdir(abs_path)\n", | |
| " #print dir_files\n", | |
| " return dir_files\n", | |
| "\n", | |
| "picture_list = get_picture_list('snippets')\n", | |
| "print picture_list\n", | |
| "import sqlite3\n", | |
| "import os.path\n", | |
| "from os import listdir, getcwd\n", | |
| "from IPython.core.display import Image \n", | |
| "\n", | |
| "def create_or_open_db(db_file):\n", | |
| " db_is_new = not os.path.exists(db_file)\n", | |
| " conn = sqlite3.connect(db_file)\n", | |
| " if db_is_new:\n", | |
| " print 'Creating schema'\n", | |
| " sql = '''create table if not exists PICTURES(\n", | |
| " ID INTEGER PRIMARY KEY AUTOINCREMENT,\n", | |
| " PICTURE BLOB,\n", | |
| " TYPE TEXT,\n", | |
| " FILE_NAME TEXT);'''\n", | |
| " conn.execute(sql) # shortcut for conn.cursor().execute(sql)\n", | |
| " else:\n", | |
| " print 'Schema exists\n", | |
| "'\n", | |
| " return conn\n", | |
| "\n", | |
| "def insert_picture(picture_file):\n", | |
| " with open(picture_file, 'rb') as input_file:\n", | |
| " conn = sqlite3.connect(dbname)\n", | |
| " c = conn.cursor()\n", | |
| " ablob = input_file.read()\n", | |
| " base=os.path.basename(picture_file)\n", | |
| " afile, ext = os.path.splitext(base)\n", | |
| " sql = '''INSERT INTO PICTURES\n", | |
| " (PICTURE, TYPE, FILE_NAME)\n", | |
| " VALUES(?, ?, ?);'''\n", | |
| " c.execute(sql,[sqlite3.Binary(ablob), ext, afile]) \n", | |
| " conn.commit()\n", | |
| "\n", | |
| "def loadimages(dbname, path):\n", | |
| " conn = sqlite3.connect(dbname)\n", | |
| " c = conn.cursor()\n", | |
| " #conn.execute(\"DELETE FROM PICTURES\")\n", | |
| " for fn in picture_list:\n", | |
| " picture_file = path+\"/\"+fn\n", | |
| " insert_picture(picture_file)\n", | |
| "\n", | |
| " for r in c.execute(\"SELECT rowid, FILE_NAME FROM PICTURES\"):\n", | |
| " print r[0],r[1]\n", | |
| " \n", | |
| " conn.commit()\n", | |
| "\n", | |
| "\n", | |
| "def get_image(picture_id):\n", | |
| " conn = sqlite3.connect(dbname)\n", | |
| " c = conn.cursor()\n", | |
| " c.execute(\"SELECT PICTURE, TYPE, FILE_NAME FROM PICTURES WHERE id = ?;\",(picture_id,))\n", | |
| " #sql = \"SELECT PICTURE, TYPE, FILE_NAME FROM PICTURES WHERE id = 19\"\n", | |
| " param = {'id': picture_id}\n", | |
| " #c.execute(sql, param)\n", | |
| " ablob, ext, afile = c.fetchone()\n", | |
| " filename = afile + ext\n", | |
| " with open(filename, 'wb') as output_file:\n", | |
| " output_file.write(ablob)\n", | |
| " return filename\n", | |
| "\n", | |
| "\n", | |
| "dbname = \"ImageC.db\"\n", | |
| "db_file = create_or_open_db(dbname)\n", | |
| "path = \"snippets/\"\n", | |
| "loadimages(dbname, path)\n", | |
| "filename = get_image(16)\n", | |
| "print filename\n", | |
| "Image(filename=filename)\n", | |
| "\n", | |
| "-----------------\n", | |
| "store, retrieve images,SQLite Databasestore,\n", | |
| "retrieve images, from SQLite , Database\n", | |
| " -- KEYWORDS store, retrieve images, from SQLite Database \n", | |
| "\n", | |
| "ID : 34 \n", | |
| "try:\n", | |
| " mercury = wikipedia.summary(\"Mercury\")\n", | |
| "except wikipedia.exceptions.DisambiguationError as e:\n", | |
| " lines = str(e.options)\n", | |
| " lines=lines.replace(\"u'\",\"\");lines=lines.replace(\"', \",\"\n", | |
| "\")\n", | |
| " lines=lines.replace(\"[\",\"\");lines=lines.replace(\"]\",\"\")\n", | |
| " lines=lines.replace('u\"','');lines=lines.replace('\"','')\n", | |
| " with open(\"textwik.txt\", \"w\")as f:\n", | |
| " f.write(lines) \n", | |
| "f.close()\n", | |
| "bad_words = ['(disambiguation)', 'All pages']\n", | |
| "with open('textwik.txt') as oldfile, open('usewik.txt', 'w') as newfile:\n", | |
| " for line in oldfile:\n", | |
| " if not any(bad_word in line for bad_word in bad_words):\n", | |
| " newfile.write(line)\n", | |
| " \n", | |
| "---------------\n", | |
| "wikipedia , bad_words, bad words, disambiguation, remove lines from text\n", | |
| " -- KEYWORDS wikipedia , bad_words, bad words, disambiguation, remove lines from text \n", | |
| "\n", | |
| "ID : 35 \n", | |
| "%%writefile Image2SQLite.py\n", | |
| "import sqlite3\n", | |
| "import os.path\n", | |
| "from os import listdir, getcwd\n", | |
| "from IPython.core.display import Image \n", | |
| "\n", | |
| "def getImage_list(rel_path):\n", | |
| " abs_path = os.path.join(os.getcwd(),rel_path)\n", | |
| " print 'abs_path =', abs_path\n", | |
| " dir_files = os.listdir(abs_path)\n", | |
| " return dir_files\n", | |
| "\n", | |
| "def create_or_open_db(dbname):\n", | |
| " db_is_new = not os.path.exists(db_file)\n", | |
| " conn = sqlite3.connect(db_file)\n", | |
| " if db_is_new:\n", | |
| " print 'Creating schema'\n", | |
| " sql = '''create table if not exists images(\n", | |
| " ID INTEGER PRIMARY KEY AUTOINCREMENT,\n", | |
| " image BLOB,\n", | |
| " TYPE TEXT,\n", | |
| " imagE TEXT);'''\n", | |
| " conn.execute(sql) # shortcut for conn.cursor().execute(sql)\n", | |
| " else:\n", | |
| " print 'Schema exists\n", | |
| "'\n", | |
| " conn.commit()\n", | |
| " conn.close()\n", | |
| " return conn\n", | |
| "\n", | |
| "def insertImage(dbname, imageFile):\n", | |
| " with open(imageFile, 'rb') as input_file:\n", | |
| " conn = sqlite3.connect(dbname)\n", | |
| " c = conn.cursor()\n", | |
| " ablob = input_file.read()\n", | |
| " base=os.path.basename(imageFile)\n", | |
| " afile, ext = os.path.splitext(base)\n", | |
| " sql = '''INSERT INTO images\n", | |
| " (image, TYPE, imagE)\n", | |
| " VALUES(?, ?, ?);'''\n", | |
| " c.execute(sql,[sqlite3.Binary(ablob), ext, afile]) \n", | |
| " conn.commit()\n", | |
| "\n", | |
| "def loadimagE(dbname, path):\n", | |
| " conn = sqlite3.connect(dbname)\n", | |
| " c = conn.cursor()\n", | |
| " #conn.execute(\"DELETE FROM images\")\n", | |
| " for fn in image_list:\n", | |
| " imageFile = path+\"/\"+fn\n", | |
| " insertImage(imageFile)\n", | |
| "\n", | |
| " for r in c.execute(\"SELECT rowid, imagE FROM images\"):\n", | |
| " print r[0],r[1]\n", | |
| " \n", | |
| " conn.commit()\n", | |
| " conn.close()\n", | |
| "\n", | |
| "def image_id(dbname):\n", | |
| " conn = sqlite3.connect(dbname)\n", | |
| " c = conn.cursor()\n", | |
| " rows = c.execute(\"SELECT rowid, TYPE, imagE FROM images\")\n", | |
| " for row in rows:\n", | |
| " print row[0],row[2]+row[1] \n", | |
| " return\n", | |
| " \n", | |
| "def get_image(dbname,image_id):\n", | |
| " conn = sqlite3.connect(dbname)\n", | |
| " c = conn.cursor()\n", | |
| " c.execute(\"SELECT image, TYPE, imagE FROM images WHERE id = ?;\",(image_id,))\n", | |
| " #sql = \"SELECT image, TYPE, imagE FROM images WHERE id = 19\"\n", | |
| " param = {'id': image_id}\n", | |
| " #c.execute(sql, param)\n", | |
| " ablob, ext, afile = c.fetchone()\n", | |
| " filename = afile + ext\n", | |
| " with open(filename, 'wb') as output_file:\n", | |
| " output_file.write(ablob)\n", | |
| " return filename\n", | |
| "---------------\n", | |
| "USAGE:\n", | |
| "import Image2Data\n", | |
| "picture_list = Image2Data.get_picture_list('snippets')\n", | |
| "print picture_list\n", | |
| "\n", | |
| "import Image2Data\n", | |
| "dbname = \"ImageE.db\"\n", | |
| "Image2Data.create_or_open_db(dbname)\n", | |
| "\n", | |
| "#insert one image\n", | |
| "import Image2Data\n", | |
| "dbname = \"ImageD.db\"\n", | |
| "picture_file = \"01.jpg\"\n", | |
| "Image2Data.insert_picture(dbname, picture_file)\n", | |
| "\n", | |
| "import Image2Data\n", | |
| "dbname = \"ImageD.db\"\n", | |
| "path = \"snippets\"\n", | |
| "loadimages(dbname, path)\n", | |
| "\n", | |
| "def image_id(dbname):\n", | |
| " conn = sqlite3.connect(dbname)\n", | |
| " c = conn.cursor()\n", | |
| " rows = c.execute(\"SELECT rowid, TYPE, FILE_NAME FROM PICTURES\")\n", | |
| " for row in rows:\n", | |
| " print row[0],row[2]+row[1]\n", | |
| " \n", | |
| "#list images by id\n", | |
| "dbname = \"ImageD.db\"\n", | |
| "image_id(dbname)\n", | |
| "\n", | |
| "#retrieve image by id\n", | |
| "filename = get_image(dbname,1)\n", | |
| "print filename\n", | |
| "Image(filename=filename)\n", | |
| "\n", | |
| "\n", | |
| " -- KEYWORDS wikipedia , bad_words, bad words, disambiguation, remove lines from text \n", | |
| "\n", | |
| "ID : 36 \n", | |
| "%%writefile Image2SQLite.py\n", | |
| "import sqlite3\n", | |
| "import os.path\n", | |
| "from os import listdir, getcwd\n", | |
| "from IPython.core.display import Image \n", | |
| "\n", | |
| "def getImage_list(rel_path):\n", | |
| " abs_path = os.path.join(os.getcwd(),rel_path)\n", | |
| " print 'abs_path =', abs_path\n", | |
| " dir_files = os.listdir(abs_path)\n", | |
| " return dir_files\n", | |
| "\n", | |
| "def create_or_open_db(dbname):\n", | |
| " db_is_new = not os.path.exists(db_file)\n", | |
| " conn = sqlite3.connect(db_file)\n", | |
| " if db_is_new:\n", | |
| " print 'Creating schema'\n", | |
| " sql = '''create table if not exists images(\n", | |
| " ID INTEGER PRIMARY KEY AUTOINCREMENT,\n", | |
| " image BLOB,\n", | |
| " TYPE TEXT,\n", | |
| " imagE TEXT);'''\n", | |
| " conn.execute(sql) # shortcut for conn.cursor().execute(sql)\n", | |
| " else:\n", | |
| " print 'Schema exists\n", | |
| "'\n", | |
| " conn.commit()\n", | |
| " conn.close()\n", | |
| " return conn\n", | |
| "\n", | |
| "def insertImage(dbname, imageFile):\n", | |
| " with open(imageFile, 'rb') as input_file:\n", | |
| " conn = sqlite3.connect(dbname)\n", | |
| " c = conn.cursor()\n", | |
| " ablob = input_file.read()\n", | |
| " base=os.path.basename(imageFile)\n", | |
| " afile, ext = os.path.splitext(base)\n", | |
| " sql = '''INSERT INTO images\n", | |
| " (image, TYPE, imagE)\n", | |
| " VALUES(?, ?, ?);'''\n", | |
| " c.execute(sql,[sqlite3.Binary(ablob), ext, afile]) \n", | |
| " conn.commit()\n", | |
| "\n", | |
| "def loadimagE(dbname, path):\n", | |
| " conn = sqlite3.connect(dbname)\n", | |
| " c = conn.cursor()\n", | |
| " #conn.execute(\"DELETE FROM images\")\n", | |
| " for fn in image_list:\n", | |
| " imageFile = path+\"/\"+fn\n", | |
| " insertImage(imageFile)\n", | |
| "\n", | |
| " for r in c.execute(\"SELECT rowid, imagE FROM images\"):\n", | |
| " print r[0],r[1]\n", | |
| " \n", | |
| " conn.commit()\n", | |
| " conn.close()\n", | |
| "\n", | |
| "def image_id(dbname):\n", | |
| " conn = sqlite3.connect(dbname)\n", | |
| " c = conn.cursor()\n", | |
| " rows = c.execute(\"SELECT rowid, TYPE, imagE FROM images\")\n", | |
| " for row in rows:\n", | |
| " print row[0],row[2]+row[1] \n", | |
| " return\n", | |
| " \n", | |
| "def get_image(dbname,image_id):\n", | |
| " conn = sqlite3.connect(dbname)\n", | |
| " c = conn.cursor()\n", | |
| " c.execute(\"SELECT image, TYPE, imagE FROM images WHERE id = ?;\",(image_id,))\n", | |
| " #sql = \"SELECT image, TYPE, imagE FROM images WHERE id = 19\"\n", | |
| " param = {'id': image_id}\n", | |
| " #c.execute(sql, param)\n", | |
| " ablob, ext, afile = c.fetchone()\n", | |
| " filename = afile + ext\n", | |
| " with open(filename, 'wb') as output_file:\n", | |
| " output_file.write(ablob)\n", | |
| " return filename\n", | |
| "---------------\n", | |
| "USAGE:\n", | |
| "import Image2Data\n", | |
| "picture_list = Image2Data.get_picture_list('snippets')\n", | |
| "print picture_list\n", | |
| "\n", | |
| "import Image2Data\n", | |
| "dbname = \"ImageE.db\"\n", | |
| "Image2Data.create_or_open_db(dbname)\n", | |
| "\n", | |
| "#insert one image\n", | |
| "import Image2Data\n", | |
| "dbname = \"ImageD.db\"\n", | |
| "picture_file = \"01.jpg\"\n", | |
| "Image2Data.insert_picture(dbname, picture_file)\n", | |
| "\n", | |
| "import Image2Data\n", | |
| "dbname = \"ImageD.db\"\n", | |
| "path = \"snippets\"\n", | |
| "loadimages(dbname, path)\n", | |
| "\n", | |
| "def image_id(dbname):\n", | |
| " conn = sqlite3.connect(dbname)\n", | |
| " c = conn.cursor()\n", | |
| " rows = c.execute(\"SELECT rowid, TYPE, FILE_NAME FROM PICTURES\")\n", | |
| " for row in rows:\n", | |
| " print row[0],row[2]+row[1]\n", | |
| " \n", | |
| "#list images by id\n", | |
| "dbname = \"ImageD.db\"\n", | |
| "image_id(dbname)\n", | |
| "\n", | |
| "#retrieve image by id\n", | |
| "filename = get_image(dbname,1)\n", | |
| "print filename\n", | |
| "Image(filename=filename)\n", | |
| "------------\n", | |
| "images to database , image2data , store images, store images as data, SQLite images\n", | |
| "\n", | |
| " -- KEYWORDS images to database , image2data , store images, store images as data, SQLite images \n", | |
| "\n", | |
| "ID : 37 \n", | |
| "# Get number of words in a file\n", | |
| "fname = raw_input(\"Enter file name: \")\n", | |
| "num_words = 0\n", | |
| "with open(fname, 'r') as f:\n", | |
| " for line in f:\n", | |
| " words = line.split()\n", | |
| " num_words += len(words)\n", | |
| "print \"Number of words: \",num_words\n", | |
| " \n", | |
| " -- KEYWORDS Get words file number words file 'Get number of words in a file' \n", | |
| "\n", | |
| "ID : 38 \n", | |
| "from time import sleep\n", | |
| "topic = raw_input(\"Research : \")\n", | |
| "fname = topic.replace(\" \", \"\")+\".txt\"\n", | |
| "fname = \"Wiki_\"+fname\n", | |
| "f =open(fname, \"w\")\n", | |
| "f.close()\n", | |
| "import wikipedia\n", | |
| "rows = wikipedia.search(topic)\n", | |
| "for row in rows:\n", | |
| " sleep(1)\n", | |
| " para = wikipedia.summary(row)\n", | |
| " para = '\n", | |
| "\n", | |
| "'.join((row, para)).encode('utf-8').strip()\n", | |
| " enter = open(fname, \"a\")\n", | |
| " enter.write(para)\n", | |
| " enter.close()\n", | |
| " print para\n", | |
| "enter.close()\n", | |
| " -- KEYWORDS search summary wikipedia text file \n", | |
| "\n", | |
| "ID : 39 \n", | |
| "# %load SearchFilename.py\n", | |
| "'''\n", | |
| "Search a filename for a phrase and how many following lines to display\n", | |
| "USAGE:\n", | |
| "import SearchFilename\n", | |
| "filename = \"hek.txt\"\n", | |
| "length = 4\n", | |
| "SearchFilename.searchfilename(filename, length)\n", | |
| "'''\n", | |
| "def searchfilename(filename, length):\n", | |
| " f = open(filename, \"r\")\n", | |
| " searchlines = f.readlines()\n", | |
| " f.close()\n", | |
| " search = str(raw_input(\"Search Phrase : \"))\n", | |
| " for i, line in enumerate(searchlines):\n", | |
| " if search in line: \n", | |
| " for l in searchlines[i:i+length]: print l,\n", | |
| " print\n", | |
| " \n", | |
| "USAGE:\n", | |
| "import SearchFilename\n", | |
| "filename = \"Automate-the-Boring-Stuff.txt\"\n", | |
| "# length = how many lines after\n", | |
| "length = 4\n", | |
| "SearchFilename.searchfilename(filename, length) \n", | |
| "\n", | |
| "------\n", | |
| "search text file search file module import searchfile\n", | |
| "\n", | |
| "\n", | |
| " -- KEYWORDS search text file search file module import searchfile \n", | |
| "\n", | |
| "ID : 40 \n", | |
| "import os\n", | |
| "import timeit\n", | |
| "def txsearch():\n", | |
| " # Ask to enter string to search\n", | |
| " Sstring = raw_input(\"Search Phrase\")\n", | |
| " for fname in os.listdir('./'):\n", | |
| " # Apply file type filter \n", | |
| " if fname.endswith(\".txt\"):\n", | |
| " # Open file for reading\n", | |
| " fo = open(fname)\n", | |
| " # Read the first line from the file\n", | |
| " line = fo.readline()\n", | |
| " # Initialize counter for line number\n", | |
| " line_no = 1\n", | |
| " # Loop until EOF\n", | |
| " while line != '' :\n", | |
| " index = line.find(Sstring)\n", | |
| " if ( index != -1) :\n", | |
| " # Set some parameters no lines longer than 240 characters \n", | |
| " # or less than search phrase +30 characters \n", | |
| " if len(line)< 240 and len(line)> len(Sstring)+20 :\n", | |
| " #print(fname, \"[\", line_no, \",\", index, \"] \", line)\n", | |
| " #print fname,line[1:-8],\" \"\n", | |
| " print fname,line_no,line\n", | |
| " # Read next line\n", | |
| " line = fo.readline() \n", | |
| " # Increment line counter\n", | |
| " line_no += 1\n", | |
| " # Close the files\n", | |
| " fo.close()\n", | |
| " \n", | |
| "\n", | |
| "------\n", | |
| "search text file search file module import searchfile\n", | |
| "\n", | |
| "\n", | |
| " -- KEYWORDS search text file search file module import searchfile \n", | |
| "\n", | |
| "ID : 41 \n", | |
| "import sys\n", | |
| "import tweepy\n", | |
| "import csv\n", | |
| "import Key\n", | |
| "\n", | |
| "#pass security information to variables\n", | |
| "consumer_key = Key.twiter()[0]\n", | |
| "consumer_secret = Key.twiter()[1]\n", | |
| "access_key = Key.twiter()[2]\n", | |
| "access_secret = Key.twiter()[3]\n", | |
| "\n", | |
| "#use variables to access twitter\n", | |
| "auth = tweepy.OAuthHandler(consumer_key, consumer_secret)\n", | |
| "auth.set_access_token(access_key, access_secret)\n", | |
| "api = tweepy.API(auth)\n", | |
| "\n", | |
| "#create an object called 'customStreamListener'\n", | |
| "class CustomStreamListener(tweepy.StreamListener):\n", | |
| "\n", | |
| " def on_status(self, status):\n", | |
| " print (status.author.screen_name, status.created_at, status.text)\n", | |
| " # Writing status data\n", | |
| " with open('OutputStreaming.txt', 'a') as f:\n", | |
| " writer = csv.writer(f)\n", | |
| " #status.author.screen_name = status.author.screen_name.encode('UTF-8')\n", | |
| " #status.text.encode = status.text.encode('UTF-8') \n", | |
| " writer.writerow([status.author.screen_name.encode('UTF-8'), status.created_at, status.text.encode('utf8')])\n", | |
| "\n", | |
| "\n", | |
| " def on_error(self, status_code):\n", | |
| " print >> sys.stderr, 'Encountered error with status code:', status_code\n", | |
| " return True # Don't kill the stream\n", | |
| "\n", | |
| " def on_timeout(self):\n", | |
| " print >> sys.stderr, 'Timeout...'\n", | |
| " return True # Don't kill the stream\n", | |
| "\n", | |
| "\n", | |
| " def titles():\n", | |
| " # Writing csv titles\n", | |
| " with open('OutputStreaming.txt', 'a') as f:\n", | |
| " writer = csv.writer(f)\n", | |
| " writer.writerow(['Author', 'Date', 'Text'])\n", | |
| " \n", | |
| "def main():\n", | |
| " streamingAPI = tweepy.streaming.Stream(auth, CustomStreamListener())\n", | |
| " #with open(\"tokens.txt\", \"r\") as f:\n", | |
| " with open(\"tokens2.txt\", \"r\") as f: \n", | |
| " tokens = f.readlines()\n", | |
| "\n", | |
| " streamingAPI.filter(track=tokens)\n", | |
| " #streamingAPI.filter(track=['Dallas', 'NewYork'])\n", | |
| "\n", | |
| "\n", | |
| " #status.text.encode('utf-8')\n", | |
| " #writer.writerow([unicode(s).encode(\"utf-8\") for s in row])\n", | |
| " #writer.writerow([unicode('Author', 'Date', 'Text').encode(\"utf-8\") for 'Author', 'Date', 'Text' in row])\n", | |
| "\n", | |
| "\n", | |
| " -- KEYWORDS csv to text tweepy to text tweepy tweepy streaming twitter import tweepy \n", | |
| "\n", | |
| "ID : 42 \n", | |
| "def tidyt(in_string): \n", | |
| " texout = in_string.replace(\"', u'\", \" \");texout = texout.replace(\"\\u2018\", \"\")\n", | |
| " texout = texout.replace(\"\\u2019 \", \"\");texout = texout.replace(\"%\", \"\")\n", | |
| " texout = texout.replace(\"[u'\", \"\");texout = texout.replace(\" u'\", \"\")\n", | |
| " texout = texout.replace(', u\"', ' ');texout = texout.replace('\",', ' ')\n", | |
| " texout = texout.replace(\"'\", '');texout = texout.replace(\"Mr. \", 'Mr. ')\n", | |
| " texout = texout.replace(\",\", '');texout = texout.replace(\".\", '')\n", | |
| " ftexout =texout.replace(\"']\", \"\")\n", | |
| " return ftexout\n", | |
| "\n", | |
| " -- KEYWORDS clean text tidy text textout replace string.replace \n", | |
| "\n", | |
| "ID : 43 \n", | |
| "from textblob import TextBlob\n", | |
| "import random\n", | |
| "import sys\n", | |
| "\n", | |
| "# stdin's read() method just reads in all of standard input as a string;\n", | |
| "# use the decode method to convert to ascii (textblob prefers ascii)\n", | |
| "text = sys.stdin.read().decode('ascii', errors=\"replace\")\n", | |
| "blob = TextBlob(text)\n", | |
| "\n", | |
| "short_sentences = list()\n", | |
| "for sentence in blob.sentences:\n", | |
| " if len(sentence.words) <= 5:\n", | |
| " short_sentences.append(sentence.replace(\"\n", | |
| "\", \" \"))\n", | |
| "\n", | |
| "for item in random.sample(short_sentences, 10):\n", | |
| "\tprint item\n", | |
| " -- KEYWORDS blob text blob TextBlog stdin Short Sentences \n", | |
| "\n", | |
| "ID : 44 \n", | |
| "Datetime objects ephem\n", | |
| "\n", | |
| " When creating an ephem.Date, you can specify the date as either a date or datetime object from the datetime standard Python module. You can also ask a PyEphem date to convert itself the other direction by calling its datetime() method.\n", | |
| "\n", | |
| " >>> from datetime import date, datetime\n", | |
| " >>> print(ephem.Date(datetime(2005, 4, 18, 22, 15)))\n", | |
| " 2005/4/18 22:15:00\n", | |
| "\n", | |
| " >>> d = ephem.Date('2000/12/25 12:41:16')\n", | |
| " >>> d.datetime()\n", | |
| " datetime.datetime(2000, 12, 25, 12, 41, 15, 999999)\n", | |
| "\n", | |
| " In those last two commands, note that slight round-off error has converted sixteen seconds to 15.999999 seconds! The inevitability of such errors is why PyEphem exposes its own date type instead of returning Python datetime objects automatically.\n", | |
| "Tuples\n", | |
| "\n", | |
| " PyEphem can return a date as a six-element tuple giving the year, month, day, hour, minute, and seconds, where the seconds include any fractions of a second. You can also provide such a tuple when creating a PyEphem date.\n", | |
| "\n", | |
| " >>> timetuple = (1984, 5, 30, 12, 23, 45)\n", | |
| " >>> print(ephem.Date(timetuple))\n", | |
| " 1984/5/30 12:23:45\n", | |
| "\n", | |
| " >>> d = ephem.Date('2001/12/14 16:07:57')\n", | |
| " >>> print(d.tuple())\n", | |
| " (2001, 12, 14, 16, 7, 57.00000002514571)\n", | |
| "\n", | |
| " Several functions in the Python standard module time will accept the time formatted as one of these six-element tuples. This feature was used in the Time Zones section, above, to convert a PyEphem date into local time.\n", | |
| "Triples\n", | |
| "\n", | |
| " There may be occasions where you need to manipulate the year and month but do not need to break the day into hours and minutes. In these cases, you can provide a three-item tuple (a “triple” of values) when creating a PyEphem date, and receive one back by calling the triple() method.\n", | |
| "\n", | |
| " >>> timetriple = (1998, 2, 26.691458333334594)\n", | |
| " >>> print(ephem.Date(timetriple))\n", | |
| " 1998/2/26 16:35:42\n", | |
| "\n", | |
| " >>> d = ephem.Date('1996/4/17 22:37:11.5')\n", | |
| " >>> print(d.triple())\n", | |
| " (1996, 4, 17.94249421296263)\n", | |
| "\n", | |
| "Floats\n", | |
| "\n", | |
| " Finally, since a PyEphem date is really just a floating-point number, so you can manually supply the value you want it to have.\n", | |
| "\n", | |
| " >>> print(ephem.Date(37238.1721875))\n", | |
| " 2001/12/14 16:07:57\n", | |
| "\n", | |
| " >>> d = ephem.Date('2000/12/25 12:41:16')\n", | |
| " >>> print('%.6f' % d)\n", | |
| " 36884.028657\n", | |
| "\n", | |
| "\n", | |
| " -- KEYWORDS store, retrieve images, from SQLite Database \n", | |
| "\n", | |
| "ID : 45 \n", | |
| "import numpy as np\n", | |
| "import matplotlib.pyplot as plt\n", | |
| "from jplephem.spk import SPK\n", | |
| "import time\n", | |
| "\n", | |
| "ephem = 'de421.bsp'\n", | |
| "ephem = 'de405.bsp'\n", | |
| "\n", | |
| "kernel = SPK.open(ephem)\n", | |
| "\n", | |
| "jd_1900_01_01 = 2415020.5004882407\n", | |
| "\n", | |
| "ntimes = [i*10**n for n in range(5) for i in [1, 2, 5]]\n", | |
| "\n", | |
| "years = [np.zeros(1)] + [np.linspace(0, 100, n) for n in ntimes[1:]] # 100 years\n", | |
| "\n", | |
| "barytup = (0, 3)\n", | |
| "earthtup = (3, 399)\n", | |
| "# moontup = (3, 301)\n", | |
| "\n", | |
| "microsecs = []\n", | |
| "for y in years:\n", | |
| " mics = []\n", | |
| " #for thing in things:\n", | |
| "\n", | |
| " jd = jd_1900_01_01 + y * 365.25 # roughly, it doesn't matter here\n", | |
| "\n", | |
| " tstart = time.clock()\n", | |
| " answer = kernel[earthtup].compute(jd) + kernel[barytup].compute(jd)\n", | |
| " mics.append(1E+06 * (time.clock() - tstart))\n", | |
| "\n", | |
| " microsecs.append(mics)\n", | |
| "\n", | |
| "microsecs = np.array(microsecs)\n", | |
| "\n", | |
| "many = [len(y) for y in years]\n", | |
| "\n", | |
| "fig = plt.figure()\n", | |
| "ax = plt.subplot(111, xlabel='length of JD object',\n", | |
| " ylabel='microseconds',\n", | |
| " title='time for jplephem [0,3] and [3,399] with ' + ephem )\n", | |
| "\n", | |
| "# from here: http://stackoverflow.com/a/14971193/3904031\n", | |
| "for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] +\n", | |
| " ax.get_xticklabels() + ax.get_yticklabels()):\n", | |
| " item.set_fontsize(item.get_fontsize() + 4)\n", | |
| "\n", | |
| "#for name, mics in zip(names, microsecs):\n", | |
| "ax.plot(many, microsecs, lw=2, label='earth')\n", | |
| "plt.legend(loc='upper left', shadow=False, fontsize='x-large')\n", | |
| "plt.xscale('log')\n", | |
| "plt.yscale('log')\n", | |
| "plt.ylim(1E+02, 1E+06)\n", | |
| "\n", | |
| "plt.savefig(\"jplephem speed test \" + ephem.split('.')[0])\n", | |
| "\n", | |
| "plt.show()\n", | |
| "\n", | |
| "\n", | |
| " -- KEYWORDS store, retrieve images, from SQLite Database \n", | |
| "\n", | |
| "ID : 46 \n", | |
| "import numpy as np\n", | |
| "import matplotlib.pyplot as plt\n", | |
| "from skyfield.api import load, JulianDate\n", | |
| "import time\n", | |
| "\n", | |
| "ephem = 'de421.bsp'\n", | |
| "ephem = 'de405.bsp'\n", | |
| "\n", | |
| "de = load(ephem) \n", | |
| "\n", | |
| "earth = de['earth']\n", | |
| "moon = de['moon']\n", | |
| "earth_barycenter = de['earth barycenter']\n", | |
| "mercury = de['mercury']\n", | |
| "jupiter = de['jupiter barycenter']\n", | |
| "pluto = de['pluto barycenter']\n", | |
| "\n", | |
| "things = [ earth, moon, earth_barycenter, mercury, jupiter, pluto ]\n", | |
| "names = ['earth', 'moon', 'earth barycenter', 'mercury', 'jupiter', 'pluto']\n", | |
| "\n", | |
| "ntimes = [i*10**n for n in range(5) for i in [1, 2, 5]]\n", | |
| "\n", | |
| "years = [np.zeros(1)] + [np.linspace(0, 100, n) for n in ntimes[1:]] # 100 years\n", | |
| "\n", | |
| "microsecs = []\n", | |
| "for y in years:\n", | |
| "\n", | |
| " from skyfield.api import load\n", | |
| " ts = load.timescale()\n", | |
| " t = ts.utc(1980, 4, 20) # the new way\n", | |
| "\n", | |
| " jd = ts.tt(jd=2444349.500592) # jd is also supported for tai, tt, tdb\n", | |
| " \n", | |
| " # Depreciated\n", | |
| " #jd = JulianDate(utc=(1900 + y, 1, 1))\n", | |
| " mics = []\n", | |
| " for thing in things:\n", | |
| "\n", | |
| " tstart = time.clock()\n", | |
| " answer = thing.at(jd).position.km\n", | |
| " mics.append(1E+06 * (time.clock() - tstart))\n", | |
| "\n", | |
| " microsecs.append(mics)\n", | |
| "\n", | |
| "microsecs = np.array(microsecs).T\n", | |
| "\n", | |
| "many = [len(y) for y in years]\n", | |
| "\n", | |
| "\n", | |
| "fig = plt.figure(figsize=(10, 8))\n", | |
| "ax = plt.subplot(111, xlabel='length of JD object',\n", | |
| " ylabel='microseconds',\n", | |
| " title='time for thing.at(jd).position.km with ' + ephem )\n", | |
| "\n", | |
| "for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] +\n", | |
| " ax.get_xticklabels() + ax.get_yticklabels()):\n", | |
| " item.set_fontsize(item.get_fontsize() + 4) # http://stackoverflow.com/a/14971193/3904031\n", | |
| "\n", | |
| "for name, mics in zip(names, microsecs):\n", | |
| " ax.plot(many, mics, lw=2, label=name)\n", | |
| "plt.legend(loc='upper left', shadow=False, fontsize='x-large')\n", | |
| "plt.xscale('log')\n", | |
| "plt.yscale('log')\n", | |
| "plt.savefig(\"skyfield speed test \" + ephem.split('.')[0])\n", | |
| "plt.show()\n", | |
| " -- KEYWORDS store, retrieve images, from SQLite Database \n", | |
| "\n", | |
| "ID : 47 \n", | |
| "# Some helper functions\n", | |
| "def norm(x, x0, sigma):\n", | |
| " return np.exp(-0.5 * (x - x0) ** 2 / sigma ** 2)\n", | |
| "\n", | |
| "def sigmoid(x, x0, alpha):\n", | |
| " return 1. / (1. + np.exp(- (x - x0) / alpha))\n", | |
| " \n", | |
| "# define the curves\n", | |
| "x = np.linspace(0, 1, 100)\n", | |
| "y1 = np.sqrt(norm(x, 0.7, 0.05)) + 0.2 * (1.5 - sigmoid(x, 0.8, 0.05))\n", | |
| "\n", | |
| "y2 = 0.2 * norm(x, 0.5, 0.2) + np.sqrt(norm(x, 0.6, 0.05)) + 0.1 * (1 - sigmoid(x, 0.75, 0.05))\n", | |
| "\n", | |
| "y3 = 0.05 + 1.4 * norm(x, 0.85, 0.08)\n", | |
| "y3[x > 0.85] = 0.05 + 1.4 * norm(x[x > 0.85], 0.85, 0.3)\n", | |
| "\n", | |
| "# draw the curves\n", | |
| "ax = pl.axes()\n", | |
| "ax.plot(x, y1, c='gray')\n", | |
| "ax.plot(x, y2, c='blue')\n", | |
| "ax.plot(x, y3, c='red')\n", | |
| "\n", | |
| "ax.text(0.3, -0.1, \"Yard\")\n", | |
| "ax.text(0.5, -0.1, \"Steps\")\n", | |
| "ax.text(0.7, -0.1, \"Door\")\n", | |
| "ax.text(0.9, -0.1, \"Inside\")\n", | |
| "\n", | |
| "ax.text(0.05, 1.1, \"fear that\n", | |
| "there's\n", | |
| "something\n", | |
| "behind me\")\n", | |
| "ax.plot([0.15, 0.2], [1.0, 0.2], '-k', lw=0.5)\n", | |
| "\n", | |
| "ax.text(0.25, 0.8, \"forward\n", | |
| "speed\")\n", | |
| "ax.plot([0.32, 0.35], [0.75, 0.35], '-k', lw=0.5)\n", | |
| "\n", | |
| "ax.text(0.9, 0.4, \"embarrassment\")\n", | |
| "ax.plot([1.0, 0.8], [0.55, 1.05], '-k', lw=0.5)\n", | |
| "\n", | |
| "ax.set_title(\"Walking back to my\n", | |
| "front door at night:\")\n", | |
| "\n", | |
| "ax.set_xlim(0, 1)\n", | |
| "ax.set_ylim(0, 1.5)\n", | |
| "\n", | |
| "# modify all the axes elements in-place\n", | |
| "XKCDify(ax, expand_axes=True)\n", | |
| "\n", | |
| " -- KEYWORDS store, retrieve images, from SQLite Database \n", | |
| "\n", | |
| "ID : 48 \n", | |
| "np.random.seed(0)\n", | |
| "\n", | |
| "ax = pylab.axes()\n", | |
| "\n", | |
| "x = np.linspace(0, 10, 100)\n", | |
| "ax.plot(x, np.sin(x) * np.exp(-0.1 * (x - 5) ** 2), 'b', lw=1, label='damped sine')\n", | |
| "ax.plot(x, -np.cos(x) * np.exp(-0.1 * (x - 5) ** 2), 'r', lw=1, label='damped cosine')\n", | |
| "\n", | |
| "ax.set_title('check it out!')\n", | |
| "ax.set_xlabel('x label')\n", | |
| "ax.set_ylabel('y label')\n", | |
| "\n", | |
| "ax.legend(loc='lower right')\n", | |
| "\n", | |
| "ax.set_xlim(0, 10)\n", | |
| "ax.set_ylim(-1.0, 1.0)\n", | |
| "\n", | |
| "#XKCDify the axes -- this operates in-place\n", | |
| "XKCDify(ax, xaxis_loc=0.0, yaxis_loc=1.0,\n", | |
| " xaxis_arrow='+-', yaxis_arrow='+-',\n", | |
| " expand_axes=True)\n", | |
| " -- KEYWORDS store, retrieve images, from SQLite Database \n", | |
| "\n", | |
| "ID : 49 \n", | |
| "\"\"\n", | |
| "XKCD plot generator\n", | |
| "-------------------\n", | |
| "Author: Jake Vanderplas\n", | |
| "\n", | |
| "This is a script that will take any matplotlib line diagram, and convert it\n", | |
| "to an XKCD-style plot. It will work for plots with line & text elements,\n", | |
| "including axes labels and titles (but not axes tick labels).\n", | |
| "\n", | |
| "The idea for this comes from work by Damon McDougall\n", | |
| " http://www.mail-archive.com/matplotlib-users@lists.sourceforge.net/msg25499.html\n", | |
| "\"\"\n", | |
| "import numpy as np\n", | |
| "import pylab as pl\n", | |
| "from scipy import interpolate, signal\n", | |
| "import matplotlib.font_manager as fm\n", | |
| "\n", | |
| "\n", | |
| "# We need a special font for the code below. It can be downloaded this way:\n", | |
| "import os\n", | |
| "import urllib2\n", | |
| "if not os.path.exists('Humor-Sans.ttf'):\n", | |
| " fhandle = urllib2.urlopen('http://antiyawn.com/uploads/Humor-Sans-1.0.ttf')\n", | |
| " open('Humor-Sans.ttf', 'wb').write(fhandle.read())\n", | |
| "\n", | |
| " \n", | |
| "def xkcd_line(x, y, xlim=None, ylim=None,\n", | |
| " mag=1.0, f1=30, f2=0.05, f3=15):\n", | |
| " \"\"\n", | |
| " Mimic a hand-drawn line from (x, y) data\n", | |
| "\n", | |
| " Parameters\n", | |
| " ----------\n", | |
| " x, y : array_like\n", | |
| " arrays to be modified\n", | |
| " xlim, ylim : data range\n", | |
| " the assumed plot range for the modification. If not specified,\n", | |
| " they will be guessed from the data\n", | |
| " mag : float\n", | |
| " magnitude of distortions\n", | |
| " f1, f2, f3 : int, float, int\n", | |
| " filtering parameters. f1 gives the size of the window, f2 gives\n", | |
| " the high-frequency cutoff, f3 gives the size of the filter\n", | |
| " \n", | |
| " Returns\n", | |
| " -------\n", | |
| " x, y : ndarrays\n", | |
| " The modified lines\n", | |
| " \"\"\n", | |
| " x = np.asarray(x)\n", | |
| " y = np.asarray(y)\n", | |
| " \n", | |
| " # get limits for rescaling\n", | |
| " if xlim is None:\n", | |
| " xlim = (x.min(), x.max())\n", | |
| " if ylim is None:\n", | |
| " ylim = (y.min(), y.max())\n", | |
| "\n", | |
| " if xlim[1] == xlim[0]:\n", | |
| " xlim = ylim\n", | |
| " \n", | |
| " if ylim[1] == ylim[0]:\n", | |
| " ylim = xlim\n", | |
| "\n", | |
| " # scale the data\n", | |
| " x_scaled = (x - xlim[0]) * 1. / (xlim[1] - xlim[0])\n", | |
| " y_scaled = (y - ylim[0]) * 1. / (ylim[1] - ylim[0])\n", | |
| "\n", | |
| " # compute the total distance along the path\n", | |
| " dx = x_scaled[1:] - x_scaled[:-1]\n", | |
| " dy = y_scaled[1:] - y_scaled[:-1]\n", | |
| " dist_tot = np.sum(np.sqrt(dx * dx + dy * dy))\n", | |
| "\n", | |
| " # number of interpolated points is proportional to the distance\n", | |
| " Nu = int(200 * dist_tot)\n", | |
| " u = np.arange(-1, Nu + 1) * 1. / (Nu - 1)\n", | |
| "\n", | |
| " # interpolate curve at sampled points\n", | |
| " k = min(3, len(x) - 1)\n", | |
| " res = interpolate.splprep([x_scaled, y_scaled], s=0, k=k)\n", | |
| " x_int, y_int = interpolate.splev(u, res[0]) \n", | |
| "\n", | |
| " # we'll perturb perpendicular to the drawn line\n", | |
| " dx = x_int[2:] - x_int[:-2]\n", | |
| " dy = y_int[2:] - y_int[:-2]\n", | |
| " dist = np.sqrt(dx * dx + dy * dy)\n", | |
| "\n", | |
| " # create a filtered perturbation\n", | |
| " coeffs = mag * np.random.normal(0, 0.01, len(x_int) - 2)\n", | |
| " b = signal.firwin(f1, f2 * dist_tot, window=('kaiser', f3))\n", | |
| " response = signal.lfilter(b, 1, coeffs)\n", | |
| "\n", | |
| " x_int[1:-1] += response * dy / dist\n", | |
| " y_int[1:-1] += response * dx / dist\n", | |
| "\n", | |
| " # un-scale data\n", | |
| " x_int = x_int[1:-1] * (xlim[1] - xlim[0]) + xlim[0]\n", | |
| " y_int = y_int[1:-1] * (ylim[1] - ylim[0]) + ylim[0]\n", | |
| " \n", | |
| " return x_int, y_int\n", | |
| "\n", | |
| "\n", | |
| "def XKCDify(ax, mag=1.0,\n", | |
| " f1=50, f2=0.01, f3=15,\n", | |
| " bgcolor='w',\n", | |
| " xaxis_loc=None,\n", | |
| " yaxis_loc=None,\n", | |
| " xaxis_arrow='+',\n", | |
| " yaxis_arrow='+',\n", | |
| " ax_extend=0.1,\n", | |
| " expand_axes=False):\n", | |
| " \"\"Make axis look hand-drawn\n", | |
| "\n", | |
| " This adjusts all lines, text, legends, and axes in the figure to look\n", | |
| " like xkcd plots. Other plot elements are not modified.\n", | |
| " \n", | |
| " Parameters\n", | |
| " ----------\n", | |
| " ax : Axes instance\n", | |
| " the axes to be modified.\n", | |
| " mag : float\n", | |
| " the magnitude of the distortion\n", | |
| " f1, f2, f3 : int, float, int\n", | |
| " filtering parameters. f1 gives the size of the window, f2 gives\n", | |
| " the high-frequency cutoff, f3 gives the size of the filter\n", | |
| " xaxis_loc, yaxis_log : float\n", | |
| " The locations to draw the x and y axes. If not specified, they\n", | |
| " will be drawn from the bottom left of the plot\n", | |
| " xaxis_arrow, yaxis_arrow : str\n", | |
| " where to draw arrows on the x/y axes. Options are '+', '-', '+-', or ''\n", | |
| " ax_extend : float\n", | |
| " How far (fractionally) to extend the drawn axes beyond the original\n", | |
| " axes limits\n", | |
| " expand_axes : bool\n", | |
| " if True, then expand axes to fill the figure (useful if there is only\n", | |
| " a single axes in the figure)\n", | |
| " \"\"\n", | |
| " # Get axes aspect\n", | |
| " ext = ax.get_window_extent().extents\n", | |
| " aspect = (ext[3] - ext[1]) / (ext[2] - ext[0])\n", | |
| "\n", | |
| " xlim = ax.get_xlim()\n", | |
| " ylim = ax.get_ylim()\n", | |
| "\n", | |
| " xspan = xlim[1] - xlim[0]\n", | |
| " yspan = ylim[1] - xlim[0]\n", | |
| "\n", | |
| " xax_lim = (xlim[0] - ax_extend * xspan,\n", | |
| " xlim[1] + ax_extend * xspan)\n", | |
| " yax_lim = (ylim[0] - ax_extend * yspan,\n", | |
| " ylim[1] + ax_extend * yspan)\n", | |
| "\n", | |
| " if xaxis_loc is None:\n", | |
| " xaxis_loc = ylim[0]\n", | |
| "\n", | |
| " if yaxis_loc is None:\n", | |
| " yaxis_loc = xlim[0]\n", | |
| "\n", | |
| " # Draw axes\n", | |
| " xaxis = pl.Line2D([xax_lim[0], xax_lim[1]], [xaxis_loc, xaxis_loc],\n", | |
| " linestyle='-', color='k')\n", | |
| " yaxis = pl.Line2D([yaxis_loc, yaxis_loc], [yax_lim[0], yax_lim[1]],\n", | |
| " linestyle='-', color='k')\n", | |
| "\n", | |
| " # Label axes3, 0.5, 'hello', fontsize=14)\n", | |
| " ax.text(xax_lim[1], xaxis_loc - 0.02 * yspan, ax.get_xlabel(),\n", | |
| " fontsize=14, ha='right', va='top', rotation=12)\n", | |
| " ax.text(yaxis_loc - 0.02 * xspan, yax_lim[1], ax.get_ylabel(),\n", | |
| " fontsize=14, ha='right', va='top', rotation=78)\n", | |
| " ax.set_xlabel('')\n", | |
| " ax.set_ylabel('')\n", | |
| "\n", | |
| " # Add title\n", | |
| " ax.text(0.5 * (xax_lim[1] + xax_lim[0]), yax_lim[1],\n", | |
| " ax.get_title(),\n", | |
| " ha='center', va='bottom', fontsize=16)\n", | |
| " ax.set_title('')\n", | |
| "\n", | |
| " Nlines = len(ax.lines)\n", | |
| " lines = [xaxis, yaxis] + [ax.lines.pop(0) for i in range(Nlines)]\n", | |
| "\n", | |
| " for line in lines:\n", | |
| " x, y = line.get_data()\n", | |
| "\n", | |
| " x_int, y_int = xkcd_line(x, y, xlim, ylim,\n", | |
| " mag, f1, f2, f3)\n", | |
| "\n", | |
| " # create foreground and background line\n", | |
| " lw = line.get_linewidth()\n", | |
| " line.set_linewidth(2 * lw)\n", | |
| " line.set_data(x_int, y_int)\n", | |
| "\n", | |
| " # don't add background line for axes\n", | |
| " if (line is not xaxis) and (line is not yaxis):\n", | |
| " line_bg = pl.Line2D(x_int, y_int, color=bgcolor,\n", | |
| " linewidth=8 * lw)\n", | |
| "\n", | |
| " ax.add_line(line_bg)\n", | |
| " ax.add_line(line)\n", | |
| "\n", | |
| " # Draw arrow-heads at the end of axes lines\n", | |
| " arr1 = 0.03 * np.array([-1, 0, -1])\n", | |
| " arr2 = 0.02 * np.array([-1, 0, 1])\n", | |
| "\n", | |
| " arr1[::2] += np.random.normal(0, 0.005, 2)\n", | |
| " arr2[::2] += np.random.normal(0, 0.005, 2)\n", | |
| "\n", | |
| " x, y = xaxis.get_data()\n", | |
| " if '+' in str(xaxis_arrow):\n", | |
| " ax.plot(x[-1] + arr1 * xspan * aspect,\n", | |
| " y[-1] + arr2 * yspan,\n", | |
| " color='k', lw=2)\n", | |
| " if '-' in str(xaxis_arrow):\n", | |
| " ax.plot(x[0] - arr1 * xspan * aspect,\n", | |
| " y[0] - arr2 * yspan,\n", | |
| " color='k', lw=2)\n", | |
| "\n", | |
| " x, y = yaxis.get_data()\n", | |
| " if '+' in str(yaxis_arrow):\n", | |
| " ax.plot(x[-1] + arr2 * xspan * aspect,\n", | |
| " y[-1] + arr1 * yspan,\n", | |
| " color='k', lw=2)\n", | |
| " if '-' in str(yaxis_arrow):\n", | |
| " ax.plot(x[0] - arr2 * xspan * aspect,\n", | |
| " y[0] - arr1 * yspan,\n", | |
| " color='k', lw=2)\n", | |
| "\n", | |
| " # Change all the fonts to humor-sans.\n", | |
| " prop = fm.FontProperties(fname='Humor-Sans.ttf', size=16)\n", | |
| " for text in ax.texts:\n", | |
| " text.set_fontproperties(prop)\n", | |
| " \n", | |
| " # modify legend\n", | |
| " leg = ax.get_legend()\n", | |
| " if leg is not None:\n", | |
| " leg.set_frame_on(False)\n", | |
| " \n", | |
| " for child in leg.get_children():\n", | |
| " if isinstance(child, pl.Line2D):\n", | |
| " x, y = child.get_data()\n", | |
| " child.set_data(xkcd_line(x, y, mag=10, f1=100, f2=0.001))\n", | |
| " child.set_linewidth(2 * child.get_linewidth())\n", | |
| " if isinstance(child, pl.Text):\n", | |
| " child.set_fontproperties(prop)\n", | |
| " \n", | |
| " # Set the axis limits\n", | |
| " ax.set_xlim(xax_lim[0] - 0.1 * xspan,\n", | |
| " xax_lim[1] + 0.1 * xspan)\n", | |
| " ax.set_ylim(yax_lim[0] - 0.1 * yspan,\n", | |
| " yax_lim[1] + 0.1 * yspan)\n", | |
| "\n", | |
| " # adjust the axes\n", | |
| " ax.set_xticks([])\n", | |
| " ax.set_yticks([]) \n", | |
| "\n", | |
| " if expand_axes:\n", | |
| " ax.figure.set_facecolor(bgcolor)\n", | |
| " ax.set_axis_off()\n", | |
| " ax.set_position([0, 0, 1, 1])\n", | |
| " \n", | |
| " return ax\n", | |
| "\n", | |
| " -- KEYWORDS store, retrieve images, from SQLite Database \n", | |
| "\n", | |
| "ID : 50 \n", | |
| "%%writefile SatInfo.py\n", | |
| "from itertools import izip, islice\n", | |
| "from time import sleep\n", | |
| "def satinfo():\n", | |
| " search_string = raw_input(\"Load : \")\n", | |
| " with open('visual.txt', 'r') as infile, open('visual.tmp', 'w') as outfile:\n", | |
| " for line in infile:\n", | |
| " if search_string in line:\n", | |
| " outfile.writelines([line, next(infile), next(infile)])\n", | |
| "\n", | |
| " from time import sleep\n", | |
| " with open('visual.tmp', 'r') as f:\n", | |
| " while True:\n", | |
| " name = f.readline()\n", | |
| " one = f.readline()\n", | |
| " two = f.readline()\n", | |
| " sleep(1)\n", | |
| " return name, one, two \n", | |
| " \n", | |
| "def prntlist(): \n", | |
| " from time import sleep\n", | |
| " with open('visual.txt', 'r') as f:\n", | |
| " while True:\n", | |
| " next_n_lines = list(islice(f, 3))\n", | |
| " if not next_n_lines:\n", | |
| " break\n", | |
| " sleep(.5)\n", | |
| " print next_n_lines[0],\n", | |
| " \n", | |
| "def reuse():\n", | |
| " from time import sleep\n", | |
| " with open('visual.tmp', 'r') as f:\n", | |
| " while True:\n", | |
| " name = f.readline()\n", | |
| " one = f.readline()\n", | |
| " two = f.readline()\n", | |
| " sleep(1)\n", | |
| " #print name, one, two \n", | |
| " f.close()\n", | |
| " return name, one, two \n", | |
| " \n", | |
| " \n", | |
| " -- KEYWORDS store, retrieve images, from SQLite Database \n", | |
| "\n", | |
| "ID : 51 \n", | |
| "Join All Files Starting With Wiki_\n", | |
| "import glob\n", | |
| "import os\n", | |
| "for f in glob.glob(\"Wiki_*.txt\"):\n", | |
| " os.system(\"cat \"+f+\" >> ALL_WIKI.txt\")\n", | |
| "-----------\n", | |
| "Count All Words in a File\n", | |
| "fname = raw_input(\"Enter file name: \")\n", | |
| "num_words = 0\n", | |
| "with open(fname, 'r') as f:\n", | |
| " for line in f:\n", | |
| " words = line.split()\n", | |
| " num_words += len(words)\n", | |
| "print \"Number of words: \",num_words\n", | |
| "\n", | |
| "------------\n", | |
| "Remove All Blank Lines\n", | |
| "from Txmanip import RemoveBlank\n", | |
| "origFile = \"ALL_WIKI.txt\"\n", | |
| "saveAS = \"ALL_WIKI_GOOD.txt\"\n", | |
| "RemoveBlank.removeblank(origFile, saveAS)\n", | |
| "------------\n", | |
| " -- KEYWORDS store, retrieve images, from SQLite Database \n", | |
| "\n", | |
| "ID : 52 \n", | |
| "# %load SearchFilename.py\n", | |
| "'''\n", | |
| "Search a filename for a phrase and how many following lines to display\n", | |
| "USAGE:\n", | |
| "import SearchFilename\n", | |
| "filename = \"hek.txt\"\n", | |
| "length = 4\n", | |
| "SearchFilename.searchfilename(filename, length)\n", | |
| "'''\n", | |
| "def searchfilename(filename, length):\n", | |
| " f = open(filename, \"r\")\n", | |
| " searchlines = f.readlines()\n", | |
| " f.close()\n", | |
| " search = str(raw_input(\"Search Phrase : \"))\n", | |
| " for i, line in enumerate(searchlines):\n", | |
| " if search in line: \n", | |
| " for l in searchlines[i:i+length]: print l,\n", | |
| " print\n", | |
| " \n", | |
| "#USAGE:\n", | |
| "import SearchFilename\n", | |
| "filename = \"ALL_WIKI_GOOD.txt\"\n", | |
| "# length = how many lines after\n", | |
| "length = 7\n", | |
| "SearchFilename.searchfilename(filename, length) \n", | |
| " -- KEYWORDS store, retrieve images, from SQLite Database \n", | |
| "\n", | |
| "ID : 53 \n", | |
| "import json\n", | |
| "import sys\n", | |
| "from time import sleep\n", | |
| "import sqlite3\n", | |
| "import csv\n", | |
| "title = \"ftp2.ipynb\"\n", | |
| "f= open(title,\"w\")\n", | |
| "f.close()\n", | |
| "\n", | |
| "conn = sqlite3.connect('ftp.db')\n", | |
| "conn.text_factory=str \n", | |
| "c = conn.cursor()\n", | |
| "with open(title, 'a') as outfile:\n", | |
| " for row in c.execute('SELECT text, title FROM ipynb'):\n", | |
| " row0 = row[1]\n", | |
| " #row0.write(outfile)\n", | |
| " #outfile = csv.writer(row0)\n", | |
| " outfile.write(row0)\n", | |
| "conn.close()\n", | |
| "f.close()\n", | |
| "\n", | |
| "jupyter to database from database to jupyter notebook\n", | |
| " \n", | |
| " -- KEYWORDS store, retrieve images, from SQLite Database \n", | |
| "\n", | |
| "ID : 54 \n", | |
| "import json\n", | |
| "import sys\n", | |
| "from time import sleep\n", | |
| "import sqlite3\n", | |
| "conn = sqlite3.connect('ftp.db')\n", | |
| "conn.text_factory=str \n", | |
| "c = conn.cursor()\n", | |
| "for row in c.execute('SELECT text, title FROM ipynb'):\n", | |
| " sleep(.5)\n", | |
| " #row0 = base64.b64decode(row[0])\n", | |
| " #print row0, row[1]\n", | |
| " print row[1] \n", | |
| " \n", | |
| "jupyter to view database from database to jupyter notebook\n", | |
| " \n", | |
| " -- KEYWORDS store, retrieve images, from SQLite Database \n", | |
| "\n", | |
| "ID : 55 \n", | |
| "import base64\n", | |
| "from time import sleep\n", | |
| "title = \"FTP_and_Editor.ipynb\"\n", | |
| "import sqlite3\n", | |
| "conn = sqlite3.connect('ftp.db')\n", | |
| "conn.text_factory=str \n", | |
| "c = conn.cursor()\n", | |
| "with open(title, 'r') as f:\n", | |
| " lines = f.readlines()\n", | |
| " for line in lines:\n", | |
| " encodedlistvalue=base64.b64encode(line)\n", | |
| " c.execute(\"INSERT INTO ipynb VALUES (?,?)\", (encodedlistvalue, line)) \n", | |
| " conn.commit()\n", | |
| " #print encodedlistvalue\n", | |
| "\n", | |
| "conn.close()\n", | |
| "f.close()\n", | |
| " \n", | |
| "jupyter to view database from database to jupyter notebook\n", | |
| " \n", | |
| " -- KEYWORDS store, retrieve images, from SQLite Database \n", | |
| "\n", | |
| "ID : 56 \n", | |
| "import sqlite3\n", | |
| "conn = sqlite3.connect('ftp.db')\n", | |
| "c = conn.cursor()\n", | |
| "conn.text_factory=str \n", | |
| "c.execute(\"\"\n", | |
| "CREATE TABLE ipynb (text, title);\n", | |
| "\"\")\n", | |
| "conn.commit()\n", | |
| "conn.close()\n", | |
| " \n", | |
| "jupyter to view database from database to jupyter notebook\n", | |
| " \n", | |
| " -- KEYWORDS store, retrieve images, from SQLite Database \n", | |
| "\n", | |
| "ID : 57 \n", | |
| "import sqlite3\n", | |
| "import sys\n", | |
| "conn = sqlite3.connect('notebooks.db')\n", | |
| "conn.text_factory = str\n", | |
| "c = conn.cursor()\n", | |
| "count=0;req=200\n", | |
| "id1 = raw_input(\"Starting ID : \")\n", | |
| "id2 = raw_input(\"How Many Rows : \")\n", | |
| "# id1 is start id2 is how many lines\n", | |
| "for row in c.execute('SELECT rowid, * from ipynb LIMIT ? OFFSET ?', (id2, id1)):\n", | |
| " count=count+1\n", | |
| " #print row[0],row[1],\"\n", | |
| "\",row[2]\n", | |
| " print row[2]\n", | |
| " if count > req:\n", | |
| " conn.close()\n", | |
| " sys.exit()\n", | |
| " -- KEYWORDS print span of rows, line numbers print by id \n", | |
| "\n", | |
| "ID : 58 \n", | |
| "import base64\n", | |
| "from time import sleep\n", | |
| "import sqlite3\n", | |
| "conn = sqlite3.connect('notebooks.db')\n", | |
| "conn.text_factory=str \n", | |
| "c = conn.cursor()\n", | |
| "line =\" \"\n", | |
| "title = \"index\"\n", | |
| "c.execute(\"INSERT INTO ipynb VALUES (?,?)\", (title, line)) \n", | |
| "conn.commit()\n", | |
| " #print encodedlistvalue\n", | |
| "\n", | |
| "conn.close()\n", | |
| "f.close()\n", | |
| " -- KEYWORDS create index column \n", | |
| "\n", | |
| "ID : 59 \n", | |
| "import json\n", | |
| "import sys\n", | |
| "from time import sleep\n", | |
| "import sqlite3\n", | |
| "import csv\n", | |
| "conn = sqlite3.connect('notebooks.db')\n", | |
| "conn.text_factory=str \n", | |
| "c = conn.cursor()\n", | |
| "#c.execute(\"DELETE title, line FROM ipynb where rowid MATCH '362113' \"):\n", | |
| "#c.execute(\"delete from ipynb where rowid=362113;\"):\n", | |
| "c.execute(\"DELETE FROM ipynb WHERE rowid = ?\", (362113,))\n", | |
| "conn.commit()\n", | |
| "conn.close()\n", | |
| " \n", | |
| " -- KEYWORDS delete sqlite by rowid delete id rowid ROWID \n", | |
| "\n", | |
| "ID : 60 \n", | |
| "import sqlite3\n", | |
| "connection = sqlite3.connect('notebooks.db')\n", | |
| "connection.row_factory = sqlite3.Row\n", | |
| "cursor = connection.execute('select * from ipynb')\n", | |
| "# instead of cursor.description:\n", | |
| "row = cursor.fetchone()\n", | |
| "names = row.keys()\n", | |
| "print names\n", | |
| " -- KEYWORDS get column names, lost column names, unknown colums \n", | |
| "\n", | |
| "ID : 61 \n", | |
| "# clustering dataset\n", | |
| "from sklearn.cluster import KMeans\n", | |
| "from sklearn import metrics\n", | |
| "import numpy as np\n", | |
| "import matplotlib.pyplot as plt\n", | |
| " \n", | |
| "x1 = np.random.randint(0,20, size=50)\n", | |
| "x2 = np.random.randint(0,20, size=50)\n", | |
| "# create new plot and data\n", | |
| "plt.plot()\n", | |
| "X = np.array(list(zip(x1, x2))).reshape(len(x1), 2)\n", | |
| "colors = ['b', 'g', 'c']\n", | |
| "markers = ['o', 'v', 's']\n", | |
| " \n", | |
| "# KMeans algorithm \n", | |
| "K = 3\n", | |
| "kmeans_model = KMeans(n_clusters=K).fit(X)\n", | |
| " \n", | |
| "print(kmeans_model.cluster_centers_)\n", | |
| "centers = np.array(kmeans_model.cluster_centers_)\n", | |
| " \n", | |
| "plt.plot()\n", | |
| "plt.title('k means centroids')\n", | |
| " \n", | |
| "for i, l in enumerate(kmeans_model.labels_):\n", | |
| " plt.plot(x1[i], x2[i], color=colors[l], marker=markers[l],ls='None')\n", | |
| " plt.xlim([-1, 21])\n", | |
| " plt.ylim([-1, 21])\n", | |
| "\n", | |
| "plt.scatter(centers[:,0], centers[:,1], marker=\"X\", color='r')\n", | |
| "plt.show()\n", | |
| " -- KEYWORDS clusters, centers of clusters, find centers clusters, find cluster centers \n", | |
| "\n", | |
| "ID : 62 \n", | |
| "# clustering dataset\n", | |
| "from sklearn.cluster import KMeans\n", | |
| "from sklearn import metrics\n", | |
| "import numpy as np\n", | |
| "import matplotlib.pyplot as plt\n", | |
| " \n", | |
| "x1 = np.random.randint(1,9, size=30)\n", | |
| "x2 = np.random.randint(1,9, size=30)\n", | |
| " \n", | |
| "plt.plot()\n", | |
| "plt.xlim([0, 10])\n", | |
| "plt.ylim([0, 10])\n", | |
| "plt.title('Dataset')\n", | |
| "plt.scatter(x1, x2)\n", | |
| "plt.show()\n", | |
| " \n", | |
| "# create new plot and data\n", | |
| "plt.plot()\n", | |
| "X = np.array(list(zip(x1, x2))).reshape(len(x1), 2)\n", | |
| "colors = ['b', 'g', 'r']\n", | |
| "markers = ['o', 'v', 's']\n", | |
| " \n", | |
| "# KMeans algorithm \n", | |
| "K = 3\n", | |
| "kmeans_model = KMeans(n_clusters=K).fit(X)\n", | |
| " \n", | |
| "plt.plot()\n", | |
| "for i, l in enumerate(kmeans_model.labels_):\n", | |
| " plt.plot(x1[i], x2[i], color=colors[l], marker=markers[l],ls='None')\n", | |
| " plt.xlim([0, 10])\n", | |
| " plt.ylim([0, 10])\n", | |
| "\n", | |
| "plt.show()\n", | |
| "\n", | |
| " -- KEYWORDS clusters, locating clusters, finding clusters, cluster \n", | |
| "\n", | |
| "ID : 63 \n", | |
| "from gtts import gTTS\n", | |
| "import os\n", | |
| "tts = gTTS(text='Good morning. Good morning', lang='en')\n", | |
| "tts.save(\"good.mp3\")\n", | |
| "os.system(\"mpg321 good.mp3\")\n", | |
| " -- KEYWORDS text to speach, gTTS, system read mp3, txt to mp3 \n", | |
| "\n", | |
| "ID : 64 \n", | |
| "import os\n", | |
| "import re\n", | |
| "from pygame import mixer\n", | |
| "import datetime\n", | |
| "import time\n", | |
| "from gtts import gTTS\n", | |
| "\n", | |
| "mp3_nameold='111'\n", | |
| "mp3_name = \"1.mp3\"\n", | |
| "\n", | |
| "mixer.init()\n", | |
| "\n", | |
| "\n", | |
| "f = open(\"test001.txt\",\"r\")\n", | |
| "ss = f.readline()\n", | |
| "while ss:\n", | |
| " \n", | |
| " split_regex = re.compile(r'[.|!|?|…]')\n", | |
| " sentences = filter(lambda t: t, [t.strip() for t in split_regex.split(ss)])\n", | |
| "\n", | |
| " for x in sentences:\n", | |
| " if(x!=\"\"):\n", | |
| " print(x)\n", | |
| " tts=gTTS(text=x, lang='en')\n", | |
| " tts.save(mp3_name)\n", | |
| " mixer.music.load(mp3_name)\n", | |
| " mixer.music.play()\n", | |
| " while mixer.music.get_busy():\n", | |
| " time.sleep(0.1)\n", | |
| " mp3_nameold=mp3_name\n", | |
| " now_time = datetime.datetime.now()\n", | |
| " mp3_name = now_time.strftime(\"%d%m%Y%I%M%S\")+\".mp3\"\n", | |
| " ss = f.readline()\n", | |
| "f.close\n", | |
| "mixer.music.load('1.mp3')\n", | |
| "mixer.stop\n", | |
| "mixer.quit\n", | |
| "\n", | |
| "if(os.path.exists(mp3_nameold)):\n", | |
| " os.remove(mp3_nameold)\n", | |
| "\n", | |
| " -- KEYWORDS text to speech, gTTS, system read mp3, txt to mp3 \n", | |
| "\n", | |
| "ID : 65 \n", | |
| "# USAGE example:\n", | |
| "# import UnCamel\n", | |
| "# x = \"TimeToUncamelCaseTextUsingFunctions\"\n", | |
| "# UnCamel.uncamel(x)\n", | |
| "def uncamel(x): \n", | |
| " return reduce(lambda a,b: a + ((b.upper() == b and (len(a) and a[-1].upper() != a[-1])) \n", | |
| " and (' ' + b) or b), x, '')\n", | |
| "def uncam(x):\n", | |
| " import re\n", | |
| " return re.sub(\"([a-z])([A-Z])\",\"\\g<1> \\g<2>\",x)\n", | |
| " -------\n", | |
| "def uncamel(x): \n", | |
| " return reduce(lambda a,b: a + ((b.upper() == b and \n", | |
| " (len(a) and a[-1].upper() != a[-1])) and \n", | |
| " (' ' + b) or b), x, '')\n", | |
| "x = \"LetUsTryToUncamelCaseTextUsingFunctions\"\n", | |
| "uncamel(x) \n", | |
| " -- KEYWORDS UnCamel, import UnCamel, uncamel(x) \n", | |
| "\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "import sqlite3\n", | |
| "import sys\n", | |
| "conn = sqlite3.connect('/home/jack/Desktop/text_stuff/snippet.db')\n", | |
| "conn.text_factory = str\n", | |
| "c = conn.cursor()\n", | |
| "count=0;req=200\n", | |
| "search = raw_input(\"Search : \")\n", | |
| "for row in c.execute('SELECT rowid, * FROM snippet'): \n", | |
| " count=count+1\n", | |
| " print \"ID : \",(row)[0],(row)[2],\" -- KEYWORDS\",(row)[3],\"\\n\"\n", | |
| " if count > req:\n", | |
| " conn.close()\n", | |
| " sys.exit()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 37, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import sqlite3\n", | |
| "import base64\n", | |
| "conn = sqlite3.connect('/home/jack/Desktop/text_stuff/snippet.db') \n", | |
| "c = conn.cursor()\n", | |
| "conn.text_factory = str\n", | |
| "file = \"\"\"\n", | |
| "# USAGE example:\n", | |
| "# import UnCamel\n", | |
| "# x = \"TimeToUncamelCaseTextUsingFunctions\"\n", | |
| "# UnCamel.uncamel(x)\n", | |
| "def uncamel(x): \n", | |
| " return reduce(lambda a,b: a + ((b.upper() == b and (len(a) and a[-1].upper() != a[-1])) \n", | |
| " and (' ' + b) or b), x, '')\n", | |
| "def uncam(x):\n", | |
| " import re\n", | |
| " return re.sub(\"([a-z])([A-Z])\",\"\\g<1> \\g<2>\",x)\n", | |
| " -------\n", | |
| "def uncamel(x): \n", | |
| " return reduce(lambda a,b: a + ((b.upper() == b and \n", | |
| " (len(a) and a[-1].upper() != a[-1])) and \n", | |
| " (' ' + b) or b), x, '')\n", | |
| "x = \"LetUsTryToUncamelCaseTextUsingFunctions\"\n", | |
| "uncamel(x) \n", | |
| "\"\"\"\n", | |
| "keywords = \"UnCamel, import UnCamel, uncamel(x) \"\n", | |
| "encodedlistvalue=base64.b64encode(file)\n", | |
| "c.execute(\"INSERT INTO snippet VALUES (?,?,?)\", (encodedlistvalue, file, keywords))\n", | |
| "conn.commit()\n", | |
| "conn.close()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 2", | |
| "language": "python", | |
| "name": "python2" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 2 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython2", | |
| "version": "2.7.13" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment