Skip to content

Instantly share code, notes, and snippets.

@lmzach09
lmzach09 / foreach-with-timeout-example.js
Created August 21, 2019 00:05
An example of a JavaScript for-each loop that has a timeout inside. The results are not what you'd expect if you came to JS from Python.
const array = [1, 2, 3, 4, 5];
array.forEach((el, i) => {
setTimeout(() => {
console.log(el);
}, 1000);
});
@lmzach09
lmzach09 / foreach-with-callback.js
Created August 21, 2019 00:09
Give your JavaScript Arrays a built-in method for looping over members in a synchronous fashion.
function forEachWithCallback(callback) {
const arrayCopy = this;
let index = 0;
const next = () => {
index++;
if (arrayCopy.length > 0) {
callback(arrayCopy.shift(), index, next);
}
}
next();
@lmzach09
lmzach09 / 1-for-each-with-promise.js
Last active September 28, 2022 16:29
Implementation of a for-each loop in JavaScript that works with callbacks. The next iteration in the loop does not start until the previous iteration's callback gets called.
const array = [1, 2, 3, 4, 5];
array.forEachWithCallback((el, i, next) => {
request({
method: 'GET',
hostname: 'httpbin.org',
path: '/get?myArg=' + el
}).then((res) => {
const responseBody = JSON.parse(res.body);
console.log(responseBody.args.myArg);
@lmzach09
lmzach09 / for-each-with-promise.js
Last active September 2, 2019 17:42
Implementation of a for-each loop in JavaScript that works with callbacks. The next iteration in the loop does not start until the previous iteration's callback gets called.
// request method implementation is in a GitHub Gist linked below
const array = [1, 2, 3, 4, 5];
array.forEachWithCallback((el, i, next) => {
request({
method: 'GET',
hostname: 'httpbin.org',
path: '/get?myArg=' + el
}).then((res) => {
const responseBody = JSON.parse(res.body);
@lmzach09
lmzach09 / google_search.py
Created September 1, 2019 04:53
Do a Google Search in Python
from googlesearch import search
query = 'how old is samuel l jackson'
## Google Search query results as a Python List of URLs
search_result_list = list(search(query, tld="co.in", num=10, stop=3, pause=1))
@lmzach09
lmzach09 / get_url.py
Created September 1, 2019 04:59
Do a Google search and get the page in the first result with Python 3
import requests
from lxml import html
from googlesearch import search
from bs4 import BeautifulSoup
query = 'how old is samuel l jackson'
## Google Search query results as a Python List of URLs
search_result_list = list(search(query, tld="co.in", num=10, stop=3, pause=1))
@lmzach09
lmzach09 / google_search.py
Created September 1, 2019 05:08
A method for doing a ChatBot query to Google Search. Returns the first sentence in the first <p> of the first URL of the search result.
import requests
import string
from lxml import html
from googlesearch import search
from bs4 import BeautifulSoup
# to search
# print(chatbot_query('how old is samuel l jackson'))
def chatbot_query(query, index=0):
@lmzach09
lmzach09 / server.py
Created September 1, 2019 05:15
beginning of a web server in python 3
import http.server
import socketserver
from google_search import chatbot_query
PORT = 8080
DIRECTORY = 'public'
@lmzach09
lmzach09 / server.py
Created September 1, 2019 05:18
snippet from web server in python 3
class Handler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=DIRECTORY, **kwargs)
def do_POST(self):
self.send_response(200)
content_length = int(self.headers['Content-Length'])
post_body = self.rfile.read(content_length)
self.end_headers()
print('user query', post_body)
@lmzach09
lmzach09 / server.py
Created September 1, 2019 05:22
HTTP server for a ChatBot API that queries Google for its ChatBot answers
import http.server
import socketserver
from google_search import chatbot_query
PORT = 8080
DIRECTORY = 'public'
class Handler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=DIRECTORY, **kwargs)