Skip to content

Instantly share code, notes, and snippets.

View astockwell's full-sized avatar

Alex Stockwell astockwell

View GitHub Profile
@astockwell
astockwell / README.md
Last active April 30, 2019 14:59
Golang Worker (Toy) Example

Golang Worker (Toy) Example

Demonstrates an example of a work queue fanning-out work items to multiple concurrent workers (goroutines).

The work queue source is lines from a log file in this example, but can be anything. The number of workers is variable and can be adjusted from 1 to 100,000's. As of Go 1.5, the runtime uses all visible CPUs (whatever your operating system considers to be a CPU) to schedule concurrent goroutine execution.

In this example, "work" done on each log file line is simulated by the worker sleeping for a randomized # of seconds from 0-5. When running, you can observe that the workers pull "work" items in an unpredictable order (e.g. you never know which worker will get the next item) and they operate in parallel.

This example also includes 2 variables (dateDict and ipsToInvestigate) that multiple goroutines share and write to (potentially simultaneously), and thus must be protected by a mutex/semaphore (mu).

@astockwell
astockwell / non_trie.py
Last active March 28, 2016 22:49
The power of Trie's -- list search: 0.436379 seconds, trie search: 0.0003269999999999662 seconds.
import re
import string
import time
pattern_words_only = re.compile('[^A-Za-z0-9 ]+', re.UNICODE)
dictionary_words = [line.rstrip('\n') for line in open('/usr/share/dict/words')]
# print(len(dictionary_words))
two_cities = 'It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way - in short, the period was so far like the present period, that some of its noisiest authorities insisted on its being received, for good or for evil, in the superlative degree of comparison only. There were a king with a large jaw and a queen with a plain face, on the throne of England; there were a king with a large jaw and a queen with a fair face, on the throne of France. I
@astockwell
astockwell / parse_guid.py
Last active June 8, 2020 10:09
Decoding Oracle Raw(16), in Python 3 and Ruby 2
def split_into_chunks(string, chunk_length=2):
chunks = []
while len(string) > 0:
chunks.append(string[:chunk_length])
string = string[chunk_length:]
return chunks
def to_oracle_raw16(string, strip_dashes=True, dashify_result=False):
oracle_format_indices = [3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15]
if strip_dashes:
@astockwell
astockwell / sortStrings.js
Last active August 29, 2015 14:17
Sort array of string numbers correctly
// JsFiddle: http://jsfiddle.net/astockwell/6jxLeppm/4/
// Array.prototype.filter polyfill for <= IE8
if (!Array.prototype.filter) {Array.prototype.filter = function(fun/*, thisArg*/) {if (this === void 0 || this === null) {throw new TypeError(); } var t = Object(this); var len = t.length >>> 0; if (typeof fun !== 'function') {throw new TypeError(); } var res = []; var thisArg = arguments.length >= 2 ? arguments[1] : void 0; for (var i = 0; i < len; i++) {if (i in t) {var val = t[i]; if (fun.call(thisArg, val, i, t)) {res.push(val); } } } return res; }; }
var containsNoNumbers = function(s) {
return s.indexOf('0') === -1;
};
var containsNumbers = function(s) {
return !containsNoNumbers(s);
@astockwell
astockwell / index.php
Last active July 27, 2016 17:21
Teaching PHP: Basic principles to use PHP in Wordpress (with ACF)
<?php
/*
* Discovering variables:
*/
echo "string";
print_r(expression);
/*
@astockwell
astockwell / page.php
Last active August 29, 2015 14:06
Advanced Custom Fields Pristine Field Uses
<!-- Text Field -->
<?php $field_name_slug = get_field("field_name_slug"); if ( !empty($field_name_slug) ): ?>
<?php echo $field_name_slug; ?>
<?php endif; ?>
<!-- Image Field -->
<?php $image_field_name = get_field("image_field_name"); if ( $image_field_name ): ?>
<img src="<?php echo $image_field_name["url"]; ?>" alt="<?php echo $image_field_name["title"]; ?>" />
<?php endif; ?>
@astockwell
astockwell / Vagrantfile
Created July 20, 2014 16:58
Vagrantfile to extract data table from Microsoft Access to CSV
# -*- mode: ruby -*-
$script = <<SCRIPT
apt-get update
apt-get install -q -y wget make libtool automake autoconf bison flex unixodbc git mdbtools
cd /vagrant
echo "Get a copy of the MDB file and run 'vagrant ssh -c 'mdb-export /vagrant/database.mdb table_name > /vagrant/table_name.csv''"
SCRIPT
Vagrant.configure("2") do |config|
@astockwell
astockwell / functions.php
Last active August 29, 2015 14:01
Better way to get Wordpress post featured image
<?php
/**
* Get everything you could ever want about a featured image
*
* Usage:
* <?php $image = get_featured_image(); ?>
* <img class="thumbnail" src="<?php echo $image['url']; ?>" alt="<?php echo $image['title']; ?>">
*
* @param integer|object $post_id The ID/Object of the post that has the desired post thumbnail image
* @return false|array Null on failure to find the image, wp_prepare_attachment_for_js array on success
@astockwell
astockwell / README.md
Last active June 14, 2024 13:17
PHP Video Url Parser

Youtube/Vimeo Video Url Parser

Parses URLs from major cloud video providers. Capable of extracting keys from various video embed and link urls to manipulate and access videos in various ways.

Usage

VideoUrlParser::identify_service("https://www.youtube.com/watch?v=x_8kFbZf20I&amp;feature=youtu.be");
@astockwell
astockwell / index.html
Last active February 4, 2016 05:23
AddThis implementation (that actually works!) (add your AddThis analytics profile ID!)
<a class="addthis_button_facebook" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=<?php the_permalink(); ?>">Facebook</a>
<a class="addthis_button_twitter" target="_blank" href="https://twitter.com/intent/tweet?url=<?php the_permalink(); ?>">Twitter</a>
<a href="" class="addthis_button">Share this</a>
<script type="text/javascript">
var addthis_config = addthis_config || {};
addthis_config.data_track_clickback = false; //tracking codes added to URLs after a share event
addthis_config.data_track_addressbar = false; //tracking codes added to URL in the address bar using JavaScript
addthis_config.ui_click = true; //open share panel on click (not hover)
</script>
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-XXXXXXXXXXXXX"></script>