Skip to content

Instantly share code, notes, and snippets.

View barce's full-sized avatar

Barce barce

View GitHub Profile
@barce
barce / predict_stats.py
Created May 13, 2020 22:39
polyfit for prediction
def predict_stats(a_stats, i_days):
x = np.array( list(range(0,len(a_stats))) )
y = np.array(a_stats)
z = np.polyfit(x,y,3) # construct x**2 + xy + 3
p_true = np.poly1d(z)
return p_true(len(a_stats) + i_days)
@barce
barce / twosum.rb
Created June 14, 2019 23:21
leetcode problem
#!/usr/bin/env ruby
# Given an array of integers, return indices of the two nums such that they add up to a specific target.
#
# You may assume that each input would have exactly one solution, and you may not use the same element twice.
def two_sum(nums, target)
a_copy = []
@barce
barce / connect.c
Created October 3, 2018 20:51
used for creating a (privoxy and/or tor) and ssh proxy
/***********************************************************************
* connect.c -- Make socket connection using SOCKS4/5 and HTTP tunnel.
*
* Copyright (c) 2000-2004 Shun-ichi Goto
* Copyright (c) 2002, J. Grant (English Corrections)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
<?php
function h($string, $esc_type = 'htmlall')
{
switch ($esc_type) {
case 'css':
$string = str_replace(array('<', '>', '\\'), array('&lt;', '&gt;', '&#47;'), $string);
// get rid of various versions of javascript
$string = preg_replace(
'/j\s*[\\\]*\s*a\s*[\\\]*\s*v\s*[\\\]*\s*a\s*[\\\]*\s*s\s*[\\\]*\s*c\s*[\\\]*\s*r\s*[\\\]*\s*i\s*[\\\]*\s*p\s*[\\\]*\s*t\s*[\\\]*\s*:/i',
@barce
barce / naive_grayscale.rb
Last active May 4, 2018 21:20
Naively check is an image is a grayscale image (very slow)
#!/usr/bin/env ruby
require 'rubygems'
require 'rmagick'
def is_grayscale(file_name)
Magick::Image.read('trix_pushed_2_stops.jpg')[0].each_pixel do |pixel, col, row|
puts "Pixel at: #{col}x#{row}:\tR: #{pixel.red}, G: #{pixel.green}, B: #{pixel.blue}"
unless (pixel.red == pixel.green) && (pixel.green == pixel.blue)
return FALSE
@barce
barce / amazonadapi_example.py
Created November 27, 2017 20:06
example of using Amazon AD API
from amazonadapi import AmazonClient
import os
client = AmazonClient()
client.refresh_token = os.environ['AMZN_REFRESH_TOKEN']
client.auto_refresh_token()
client.set_region()
client.profile_id = '3586026682031981'
orders = client.get_orders(dsp_advertiser_id)
@barce
barce / collections.py
Last active June 19, 2017 17:55
collections in python
import collections
c = collections.Counter('helloworld')
print(c)
# Counter({'l': 3, 'o': 2, 'e': 1, 'd': 1, 'h': 1, 'r': 1, 'w': 1})
print(c.most_common(3))
# [('l', 3), ('o', 2), ('e', 1)]
@barce
barce / dict as a switch in python
Created May 25, 2017 16:20
Using a dict as a switch in Python
def dispatch_if(operator, x, y):
if operator == 'add':
return x + y
elif operator == 'sub':
return x - y
elif operator == 'mul':
return x * y
elif operator == 'div':
return x / y
else:
require 'rest_client'
require 'json'
# RestClient.log=STDOUT # Optionally turn on logging
q = '{
"query" : { "term" : { "user" : "kimchy" } }
}
'
r = JSON.parse \
@barce
barce / faraday_test.rb
Created June 8, 2015 23:00
test if faraday is working
require 'faraday'
conn = Faraday.new(:url => 'https://www.google.com') do |faraday|
faraday.request :url_encoded # form-encode POST params
faraday.response :logger # log requests to STDOUT
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
end
response = conn.get '/'