Skip to content

Instantly share code, notes, and snippets.

View hgupta's full-sized avatar

Harsh Gupta hgupta

View GitHub Profile
@hgupta
hgupta / TrueColour.md
Created September 24, 2018 02:57 — forked from XVilka/TrueColour.md
True Colour (16 million colours) support in various terminal applications and terminals

Colours in terminal

It's a common confusion about terminal colours... Actually we have this:

  • plain ascii
  • ansi escape codes (16 colour codes with bold/italic and background)
  • 256 colour palette (216 colours + 16 ansi + 24 gray) (colors are 24bit)
  • 24bit true colour ("888" colours (aka 16 milion))
printf "\x1b[${bg};2;${red};${green};${blue}m\n"
@hgupta
hgupta / gevent_task_queue.py
Created April 22, 2018 17:40 — forked from ls0f/gevent_task_queue.py
a simple gevent task queue
#coding:utf-8
from gevent import monkey
monkey.patch_all()
import logging
import gevent
logging.basicConfig(level=logging.DEBUG)
LOGGER = logging.getLogger("GeventTaskQueue")
from gevent.queue import Queue, Empty
# http://wiki.apache.org/solr/FAQ#How_can_I_delete_all_documents_from_my_index.3F
# http://wiki.apache.org/solr/UpdateXmlMessages#Updating_a_Data_Record_via_curl

curl "http://index.websolr.com/solr/a0b1c2d3/update?commit=true" -H "Content-Type: text/xml" --data-binary '<delete><query>*:*</query></delete>'

I'm amused at the traction this little gist is getting on Google! I would be remiss not to point out that six+ years later I'm still helping thousands of companies on a daily basis with their search index management, by providing managed Solr as a service over at Websolr, and hosted Elasticsearch at Bonsai. Check us out if you'd like an expert helping hand at Solr and Elasticsearch hosting, ops and support!

@hgupta
hgupta / convert_str_to_class.py
Last active November 25, 2016 09:44
Convert string to Python class object
# SO Link - http://stackoverflow.com/questions/1176136/convert-string-to-python-class-object
import sys
import types
def str_to_class(field):
try:
identifier = getattr(sys.modules[__name__], field)
except AttributeError:
raise NameError("%s doesn't exist." % field)
@hgupta
hgupta / remove_nodejs.sh
Created May 27, 2016 13:06
Completely remove NodeJS from machine
lsbom -f -l -s -pf /var/db/receipts/org.nodejs.pkg.bom | while read f; do sudo rm /usr/local/${f}; done
sudo rm -rf /usr/local/{lib/node{,/.npm,_modules},bin,share/man}/{npm*,node*,man1/node*}
sudo rm -rf /var/db/receipts/org.nodejs.*
@hgupta
hgupta / js_array_flatten.js
Created December 31, 2015 06:11
Pure JavaScript (JS) Array Flatten method (like Ruby Array#flatten)
Array.prototype.flatten = function(limit) {
var level = 0;
if(limit === undefined) limit = Number.POSITIVE_INFINITY;
if(arguments.length > 1) level = arguments[1];
return this.reduce(function(a, b) {
if((b instanceof Array) && level < limit)
a = a.concat(b.flatten(limit, level + 1));
else
a.push(b);
@hgupta
hgupta / inverse_fizzbuzz.rb
Last active August 29, 2015 14:25 — forked from s-yano/inverse_fizzbuzz.rb
Inverse FizzBuzz
def fizzbuzz(x)
x%3==0 && x%5==0 ? "fizzbuzz" : x%3==0 ? "fizz" : x%5==0 ? "buzz" : nil
end
def accept?(a, n)
return n if a.empty?
case fizzbuzz(n)
when nil
accept?(a, n+1)
when a.first
@hgupta
hgupta / ar_stored_procedure.rb
Last active August 29, 2015 14:11
ActiveRecord Stored Procedure Execute (Disconnection issue hack/fix in place)
module ActiveRecord
class Base
# Establishes a connection to the database that's used by all Active Record objects.
def self.mysql2_connection(config)
config[:username] = 'root' if config[:username].nil?
if Mysql2::Client.const_defined? :FOUND_ROWS
config[:flags] = config[:flags] ? config[:flags].to_i | Mysql2::Client::FOUND_ROWS : Mysql2::Client::FOUND_ROWS
end
<tr>
<td><%=@key.split(".").drop(1) %></td>
<td><%= @value%></td>
</tr>
@hgupta
hgupta / varbinary_migration_columns_initializer.rb
Last active August 29, 2015 14:02
Adding MySQL data type like varbinary support for Rails Migrations
# Provide varbinary columns in a Migration.
# Probably a better way to do this?
#
# Usage:
# => t.varbinary :column, :limit => 20, ....[options]
#
ActiveRecord::ConnectionAdapters::SchemaStatements.module_eval do
def type_to_sql_with_varbinary(type, limit = nil, precision = nil, scale = nil)
return type_to_sql_without_varbinary(type, limit, precision, scale) unless :varbinary == type.to_sym