Skip to content

Instantly share code, notes, and snippets.

@allenday
allenday / canopy.py
Created February 5, 2024 05:55 — forked from gdbassett/canopy.py
Efficient python implementation of canopy clustering. (A method for efficiently generating centroids and clusters, most commonly as input to a more robust clustering algorithm.)
from sklearn.metrics.pairwise import pairwise_distances
import numpy as np
# X shoudl be a numpy matrix, very likely sparse matrix: http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.sparse.csr_matrix.html#scipy.sparse.csr_matrix
# T1 > T2 for overlapping clusters
# T1 = Distance to centroid point to not include in other clusters
# T2 = Distance to centroid point to include in cluster
# T1 > T2 for overlapping clusters
# T1 < T2 will have points which reside in no clusters
# T1 == T2 will cause all points to reside in mutually exclusive clusters
@allenday
allenday / declare-winner.js
Last active March 1, 2023 22:42
Chainlink + Google Analytics - code snippet to declare winner
function declareWinner() public onlyOwner {
if (charity1Votes == charity2Votes) {
winner = 'Charity #1 and #2 tied!';
}
if (charity1Votes > charity2Votes) {
winner = 'Charity #1 won!';
}
if (charity1Votes > charity2Votes) {
@allenday
allenday / update-contract.js
Last active March 1, 2023 22:42
Chainlink + Google Analytics - code snippet to write votes to smart contract
let item1Votes
try {
item1Votes = parseInt(responses[0].data.rows[0].metricValues[0].value)
} catch {
item1Votes = 0
}
let item2Votes
try {
item2Votes = parseInt(responses[1].data.rows[0].metricValues[0].value)
@allenday
allenday / fetch-data.js
Last active June 13, 2023 05:33
Chainlink + Google Analytics - code snippet to fetch dog and cat votes from BigQuery
const requestConfig = {
method: 'post',
headers: {
"Authorization": `Bearer ${accessToken}`,
"Accept": 'application/json',
"Content-Type": 'application/json'
},
data: {
"metrics": [{"name":"activeUsers"}],
"minuteRanges":[{"startMinutesAgo":29,"endMinutesAgo":0}]
@allenday
allenday / auth.js
Last active June 13, 2023 05:33
Chainlink + Google Analytics - code snippet for Google auth
const tokenResponse = await Functions.makeHttpRequest({
url: 'https://oauth2.googleapis.com/token',
method: 'post',
data: jwtRequestString
})
const accessToken = tokenResponse.data.access_token
CREATE TEMP FUNCTION
KECCAK256(data STRING)
RETURNS STRING
LANGUAGE js AS """
var utf8 = unescape(encodeURIComponent(data));
var arr = [];
for (var i = 0; i < utf8.length; i++) {
arr.push(utf8.charCodeAt(i));
}
return ethers.utils.keccak256(arr)
@allenday
allenday / query.sql
Created July 7, 2022 05:07
balancer new pool bigquery example with ethers.js and ethjs-abi.js
CREATE TEMP FUNCTION
KECCAK256(data STRING)
RETURNS STRING
LANGUAGE js AS """
var utf8 = unescape(encodeURIComponent(data));
var arr = [];
for (var i = 0; i < utf8.length; i++) {
arr.push(utf8.charCodeAt(i));
}
return ethers.utils.keccak256(arr)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
CREATE TEMP FUNCTION
hex64ToInt64(hex STRING)
RETURNS INT64 AS (
IF(hex < "8000000000000000",
cast(concat("0x", hex) AS INT64),
(SELECT (((ms32 & 0x7fffffff) << 32) | ls32) - 0x7fffffffffffffff - 1
FROM (SELECT cast(concat("0x", substr(hex, 1, 8)) AS INT64) AS ms32,
cast(concat("0x", substr(hex, 9, 8)) AS INT64) AS ls32))));
CREATE TEMP FUNCTION
KECCAK(data STRING)
CREATE TEMP FUNCTION term_years(term STRING)
RETURNS ARRAY<STRUCT<year INT64, term_frequency INT64>> AS (
(
SELECT years
FROM `gcp-pdp-words-dev.sandbox.eng_bert_preprocessed_year_term_frequencies`
WHERE preprocessed_term = term
)
-- SELECT ARRAY_AGG(STRUCT<year INT64, term_frequency INT64>(z.year, z.term_frequency + IFNULL(t.term_frequency, 0)))
-- FROM
-- (