Skip to content

Instantly share code, notes, and snippets.

View gcrsaldanha's full-sized avatar
🏠
Working from home

Gabriel Saldanha gcrsaldanha

🏠
Working from home
View GitHub Profile
@gcrsaldanha
gcrsaldanha / tmux-cheatsheet.markdown
Created June 25, 2018 21:01 — forked from MohamedAlaa/tmux-cheatsheet.markdown
tmux shortcuts & cheatsheet

tmux shortcuts & cheatsheet

start new:

tmux

start new with session name:

tmux new -s myname
@gcrsaldanha
gcrsaldanha / set_literal_vs_set_method.py
Created June 27, 2018 12:16
This gist demonstrates the usage of `set()` and `{}` for building sets in python and when one is preferred over the other
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():
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello, World!'
if __name__ == '__main__':
app.run()
@gcrsaldanha
gcrsaldanha / find_factors.py
Created December 31, 2018 01:13
Simple method to find the factors of a given number (computationally expensive)
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]
@gcrsaldanha
gcrsaldanha / largest_prime_factor.py
Created December 31, 2018 12:22
Find largest prime factor (optimal)
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 / timing_decorator.py
Created December 31, 2018 12:47
Decorator for measuring a function duration
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 / devto-using-generators-to-avoid-extra-service-calls.py
Last active April 28, 2020 12:39
A simple snippet of coding showing how to use Python generators and any to avoid extra calls
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 / truffle develop
Last active May 12, 2020 00:44
truffle develop output
# 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 / app.py
Created June 15, 2020 13:20
HTTP + Ethereum example
'''
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 / concurrency.py
Last active November 25, 2020 11:00
Python concurrency example
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!