Skip to content

Instantly share code, notes, and snippets.

View tommypratama's full-sized avatar
💭
Doing something useful

Tommy Pratama tommypratama

💭
Doing something useful
View GitHub Profile

Create Repository

Create public repository in current directory and view to the browser.

git init && gh repo create -y --public && gh repo view -w

Create private repository in current directory and view to the browser.

git init && gh repo create -y --private && gh repo view -w

Create Credentials Jupyter Notebook

import json

cred = {
  'GOOGLE_KEY': 'secret',
  'FACEBOOK_KEY': 'secret'
}
@tommypratama
tommypratama / random_sample.md
Created February 2, 2020 08:06
Random Sample

Random Sample

import random 

cars = ['Innova Venturer', 'Nissan Leaf', 'Camry Hybrid', 'Seri 7', 'Porsche Boxter', 'Tesla 3', 
        'Alphard', 'X4', 'Seri 6', 'C200 coupe']

random = random.sample(range(len(cars)), 2)
@tommypratama
tommypratama / stocks-data.md
Created January 21, 2020 14:51
Stocks Data

Stocks Data

import pandas_datareader as pdr
import datetime 

def get(tickers, startdate, enddate):
  def data(ticker):
 return (pdr.get_data_yahoo(ticker, start=startdate, end=enddate))
@tommypratama
tommypratama / mac-command.md
Created December 26, 2019 04:02
Mac Command

Mac Command

  • Membuka hidden files SHIFT+CMD+TITIK

Python Decorators

Sometimes you can just write a function or method or class and be done with it. But sometimes you find that you need to wrap your functions or methods or classes in some sort of new behavior. And since you're a good programmer, you don't want to change every function in the same way over and over again. That's not DRY or a good use of your time! Well, when that happens, you just might need a decorator.

Example

Nested function:

Install Kaggle API

Error

jovyan@3825ba6d1a66:~$ kaggle
Traceback (most recent call last):
  File "/opt/conda/bin/kaggle", line 6, in <module>
    from kaggle.cli import main
@tommypratama
tommypratama / tweet_listener.md
Last active January 26, 2020 21:34
Here I define a Tweet listener that creates a file called 'tweets.txt', collects streaming tweets as .jsons and writes them to the file 'tweets.txt'; once 100 tweets have been streamed, the listener closes the file and stops listening.

Twitter listener code

class MyStreamListener(tweepy.StreamListener):
    def __init__(self, api=None):
        super(MyStreamListener, self).__init__()
        self.num_tweets = 0
        self.file = open("tweets.txt", "w")

Running Startup Scripts

  1. Masuk ke VM Kemudian Pilih Edit
  2. Pada Custom metadata, Klik Add item.
  3. Tambahkan startup script berikut:
    • Pada kolom key input startup-script dan value misalnya docker start container
    • Bisa juga menggunakan Cloud Storage: Pada kolom key input startup-script-url

https://cloud.google.com/compute/docs/startupscript