Skip to content

Instantly share code, notes, and snippets.

Avatar

Gabriel Fair gabefair

View GitHub Profile
@otayeby
otayeby / redirect-stdout-inside-loop.py
Last active Apr 4, 2018
Redirecting the stdout to a variable while iterating inside a loop.
View redirect-stdout-inside-loop.py
"""
Reason for publishing this Gist and the use case:
In many cases we use functions in libraries that print the variable we want instead of returning it. This Gist shows
how to get hold of the printed values from these functions and store in a variable. I found the solution in
couple of posts on different forums:
* https://stackoverflow.com/questions/1218933/can-i-redirect-the-stdout-in-python-into-some-sort-of-string-buffer
* https://wrongsideofmemphis.wordpress.com/2010/03/01/store-standard-output-on-a-variable-in-python/
* https://groups.google.com/forum/#!topic/comp.lang.python/tkK6n1oVKhM
* https://bytes.com/topic/python/answers/849106-redirection-standard-output-python-command-python-variable
In addition to the trick demonstrated in these posts, I found a case where I would need to store multiple printouts
@joshtch
joshtch / gdrive-dl.py
Created Jan 4, 2018
Google Drive file downloader for Python, with progress bar with tqdm. Based on this SO answer https://stackoverflow.com/a/39225039/3175094
View gdrive-dl.py
#!/usr/bin/env python2
import requests
from tqdm import tqdm
import re
import os
def download_file_from_google_drive(id, destination):
URL = 'https://docs.google.com/uc?export=download'
session = requests.Session()
View generate_data.py
import numpy as np
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.utils import shuffle
# get shuffled iris data
X, y = load_iris(return_X_y=True)
X, y = shuffle(X, y, random_state=0)
@vene
vene / sparsemax_loss_theano.ipynb
Created Jul 8, 2016
sparsemax loss for Theano
View sparsemax_loss_theano.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@vene
vene / coordinate_descent.ipynb
Created Jun 30, 2016
simple cd solver for l2-regularized linear models
View coordinate_descent.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View word_movers_distance.py
from collections import Counter
import numpy as np
from sklearn.metrics import euclidean_distances
from pyemd import emd as pyemd
def word_movers_distance(a, b, embeddings):
"""Word Mover's Distance.
A measure of text similarity: earth mover's distance in embedding metric space.
@niksumeiko
niksumeiko / git.migrate
Last active Oct 24, 2020
Moving git repository and all its branches, tags to a new remote repository keeping commits history
View git.migrate
#!/bin/bash
# Sometimes you need to move your existing git repository
# to a new remote repository (/new remote origin).
# Here are a simple and quick steps that does exactly this.
#
# Let's assume we call "old repo" the repository you wish
# to move, and "new repo" the one you wish to move to.
#
### Step 1. Make sure you have a local copy of all "old repo"
### branches and tags.
@vene
vene / nls_solvers.ipynb
Last active Aug 4, 2018
Non-negative least squares in Python
View nls_solvers.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@vene
vene / nmf_faces.ipynb
Created Oct 29, 2013
Comparison of NMF solvers
View nmf_faces.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@jtriley
jtriley / terminalsize.py
Created Jul 26, 2011
Get current terminal size on Linux, Mac, and Windows
View terminalsize.py
#!/usr/bin/env python
import os
import shlex
import struct
import platform
import subprocess
def get_terminal_size():
""" getTerminalSize()
You can’t perform that action at this time.