Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Gabriel Saldanha gcrsaldanha

🏠
Working from home
View GitHub Profile
@gcrsaldanha
gcrsaldanha / concurrency.py
Last active Nov 25, 2020
Python concurrency example
View concurrency.py
import time # I will use it to simulate latency with time.sleep
from concurrent.futures import ThreadPoolExecutor, as_completed
def has_facebook_account(user_email):
time.sleep(5) # 5 seconds! That is bad.
print("Finished facebook after 5 seconds!")
return True
def has_github_account(user_email):
time.sleep(1) # 1 second. Phew!
@gcrsaldanha
gcrsaldanha / app.py
Created Jun 15, 2020
HTTP + Ethereum example
View app.py
'''
Important docs
https://web3py.readthedocs.io/en/stable/web3.eth.html#web3.eth.Eth.getTransaction
https://web3py.readthedocs.io/en/stable/web3.eth.html#web3.eth.Eth.getTransactionReceipt
'''
import hashlib
import json
import os
@gcrsaldanha
gcrsaldanha / truffle develop
Last active May 12, 2020
truffle develop output
View truffle develop
# truffle develop
Truffle Develop started at http://127.0.0.1:9545/
Accounts:
(0) 0xdfb772fba7631b5bfde93cc3e2b0e488d1a17b2a
(1) 0xf2d6f135c743398e370fb865ea69d3ccfb96f123
(2) 0xe5f0f5700e678bb92c9414773b9a9fad1f730d17
(3) 0x69ffe89931711b15db6d5cd7b181c940dc83cabe
(4) 0x19f802fb5bda9631792e58208baba2e0ce7cc7ef
@gcrsaldanha
gcrsaldanha / devto-using-generators-to-avoid-extra-service-calls.py
Last active Apr 28, 2020
A simple snippet of coding showing how to use Python generators and any to avoid extra calls
View devto-using-generators-to-avoid-extra-service-calls.py
def has_facebook_account(user_email):
print('calling Facebook service')
return False
def has_github_account(user_email):
print('calling Github service')
return True
def has_twitter_account(user_email):
print('calling Twitter service')
@gcrsaldanha
gcrsaldanha / timing_decorator.py
Created Dec 31, 2018
Decorator for measuring a function duration
View timing_decorator.py
import time
from functools import wraps
def timing(decorated):
@wraps(decorated)
def decorator(*args, **kwargs):
print(f'Calling {decorated.__name__} function')
start_time_time = time.time()
start_time_perf = time.perf_counter()
@gcrsaldanha
gcrsaldanha / largest_prime_factor.py
Created Dec 31, 2018
Find largest prime factor (optimal)
View largest_prime_factor.py
def get_largest_prime_factor(number):
prime_factors = []
divisor = 1
i = 1
while(i <= number // divisor): # integer division
if number % i == 0: # if number is divisible by i
divisor = i # found a divisor, is used in line 5 to "short" the stop clause
if is_prime(divisor): # filter only prime divisors (factors)
prime_factors.append(divisor)
i += 1
@gcrsaldanha
gcrsaldanha / find_factors.py
Created Dec 31, 2018
Simple method to find the factors of a given number (computationally expensive)
View find_factors.py
def find_factors(number):
factors = []
for factor in range(1, number + 1): # makes range go from 1 to number (inclusive)
if number % factor == 0:
factors.append(factor)
return factors
# Or by using list comprehensions
def find_factors_comprehesion(number):
return [factor for factor in range(1, number + 1) if number % factor == 0]
View hello-world-flask.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello, World!'
if __name__ == '__main__':
app.run()
@gcrsaldanha
gcrsaldanha / set_literal_vs_set_method.py
Created Jun 27, 2018
This gist demonstrates the usage of `set()` and `{}` for building sets in python and when one is preferred over the other
View set_literal_vs_set_method.py
import dis
def non_empty_set_literal():
return {1, 2, 3} # Build a set {1, 2, 3}
def non_empty_set_method():
return set([1, 2, 3]) # Builds a set {1, 2, 3} from a list
def empty_set_literal():
View tmux-cheatsheet.markdown

tmux shortcuts & cheatsheet

start new:

tmux

start new with session name:

tmux new -s myname