Skip to content

Instantly share code, notes, and snippets.

View rbotzer's full-sized avatar
🦄
Productizing

Ronen Botzer rbotzer

🦄
Productizing
View GitHub Profile

Query Parsing in Ruby

While looking for a Ruby equivalent of PHP's parse_query(), or Python's urlparse.parse_qs (i.e. the reverse of ActiveSupport's Object#to_query extension), I ran into several limited/failed implementations. Addressable is the only one that seems to do it right.

Examples for common datatypes

>> require 'active_support'
>> str_ex = "bar".to_query('foo')
=> "foo=bar"
>> array_ex = [1, 2, 3].to_query('foo')
=> "foo%5B%5D=1&foo%5B%5D=2&foo%5B%5D=3"
@rbotzer
rbotzer / multiple-php-versions-os-x.md
Last active July 31, 2017 09:26
Installing PHP versions on Mac OS X Mavericks (10.9)

Installing PHP versions on Mac OS X Mavericks (10.9+) and Higher

Using Homebrew, Homebrew-PHP and the RVM-like brew-php-switcher you can easily switch between multiple installed versions of PHP on your OS X machine (Mavericks 10.9 to El-Capitan 10.11).

Install the command line tools

    xcode-select --install # install the command line tools, if missing

Install Homebrew

    ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
    brew update && brew doctor
@rbotzer
rbotzer / hi-scores-data-structure.py
Created October 31, 2019 23:51
High scores data structure
key = (namespace, set, "pacman")
scores = {
"CPU": [9800, {"dt": "2017-12-05 01:01:11", "ts": 1512435671573}],
"ETC": [9200, {"dt": "2018-05-01 13:47:26", "ts": 1525182446891}],
"SOS": [24700, {"dt": "2018-01-05 01:01:11", "ts": 1515114071923}],
"ACE": [34500, {"dt": "1979-04-01 09:46:28", "ts": 291807988156}],
"EIR": [18400, {"dt": "2018-03-18 18:44:12", "ts": 1521398652483}],
"CFO": [17400, {"dt": "2017-11-19 15:22:38", "ts": 1511104958197}],
}
pp = pprint.PrettyPrinter(indent=2)
print("\nGet all Pac-Man top scores, sorted by score (rank)")
ops = [mh.map_get_by_rank_range("scores", 0, -1, aerospike.MAP_RETURN_KEY_VALUE)]
k, m, b = client.operate(key, ops)
by_rank = b["scores"]
pp.pprint(by_rank)
@rbotzer
rbotzer / give-award-once.py
Last active November 2, 2019 01:22
Give an award once and only once
# add an award icon to a specific player ("CFO") where awards have a type
# and a count, some awards can be given once, and some more than once
ctx = [
ctxh.cdt_ctx_map_key("CFO"),
# the attribute map is the second element of the tuple
ctxh.cdt_ctx_list_index(1),
]
ops = [
# give the unicorn award exactly once
mh.map_put(
# assuming it's given once a day, grant the 'top score' award to the
# current top score
ctx = [
ctxh.cdt_ctx_map_rank(-1),
# the attribute map is the second element of the tuple
ctxh.cdt_ctx_list_index(1),
]
ctx2 = ctx + [ctxh.cdt_ctx_map_key("awards")]
ops = [
# create the top score award if it doesn't exist
# grant the award once more
client.operate(key, ops)
# get the top three scores
ops = [mh.map_get_by_rank_range("scores", -3, 3, aerospike.MAP_RETURN_KEY_VALUE)]
k, m, b = client.operate(key, ops)
pp.pprint(b["scores"])
@rbotzer
rbotzer / one-sensor-one-day.py
Last active December 4, 2019 03:58
One day's data for a single sensor
print("\nGet sensor{} data for April 2nd".format(sensor_id))
key = (namespace, set, "sensor{}-04-02".format(sensor_id))
if options.interactive:
pause()
_, _, b = client.get(key)
pp.pprint(b["t"])
@rbotzer
rbotzer / one-sensor-three-hours.py
Created December 4, 2019 04:10
Three hours of data from one sensor
key = (namespace, set, "sensor{}-12-31".format(sensor_id))
print("\nRetrieve sensor{} data for 8-11am, December 31st".format(sensor_id))
if options.interactive:
pause()
starts = 8 * 60
ends = 11 * 60
ops = [
lh.list_get_by_value_range(
"t",
aerospike.LIST_RETURN_VALUE,
@rbotzer
rbotzer / one-sensor-one-year.py
Created December 4, 2019 04:42
One year of data for one sensor
print("\nGet a year's data for sensor{}".format(sensor_id))
if options.interactive:
pause()
dt = datetime.datetime(2018, 1, 1, 0, 0, 0)
keys = []
for i in range(1, 366):
keys.append((namespace, set,"sensor{}-{:02d}-{:02d}".format(sensor_id, dt.month, dt.day)))
dt = dt + timedelta(days=1)
sensor_year = client.get_many(keys)
for rec in sensor_year: