Dynamically Set Deep Dictionary Values

If you have ever needed to set some values of a dictionary in JavaScript or PHP, then you know that you have to do something like myArray[a][b][c] = 0. But what if we don’t know that the subkeys will be “a->b->c” until runtime? You would need a way to index into the dictionary dynamically and return the resulting modified dictionary. Below are code snippets in JavaScript and PHP that do exactly that.

Continue reading → Dynamically Set Deep Dictionary Values

Regex to Make Text Links HTML Links

Regex used to select and replace URLs in plain text with clickable links

(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.%-=#~\@!]*(\?\S+)?)?)?)

PHP

$regex = '(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.%-=#~\@!]*(\?\S+)?)?)?)';
$text = mb_ereg_replace($regex, '<a href="\\1">\\1</a>', $text);

Python Multiprocessing

The following code will enable you to implement multiprocessing in any Python project that contains a for loop. Take your for loop that has a long-running function in it and replace “for i in iterable” with your for loop conditions. Change “function_name” to the name of the function that will be executed. Change the list of function arguments to what you need.

import multiprocessing
number_cores = multiprocessing.cpu_count()
pool = multiprocessing.Pool(processes=number_cores)
r = [pool.apply_async(function_name, args=(function_arg1, function_arg2)) for i in iterable]
output = [p.get() for p in r]
pool.terminate()

The above code would be the multiprocessing implementation of the following.

output = []
for i in iterable:
    output += function_name(function_arg1, function_arg2)

Regex Sentence Splitter

Regex to match the end of sentences in order to split a block of text into sentences.

([\.\?!][\'\"\u2018\u2019\u201c\u201d\)\]]*\s*(?<!\w\.\w.)(?<![A-Z][a-z][a-z]\.)(?<![A-Z][a-z]\.)(?<![A-Z]\.)\s+)

Python Code

sentence_regex = ur'([\.\?!][\'\"\u2018\u2019\u201c\u201d\)\]]*\s*(?<!\w\.\w.)(?<![A-Z][a-z][a-z]\.)(?<![A-Z][a-z]\.)(?<![A-Z]\.)\s+)'
regex = re.compile(sentence_regex, flags=re.UNICODE)
sentences = regex.split(TEXT_BLOCK)

Using Synology NAS as CrashPlan Backup Destination

When I first got my Synology DS1815+ to replace my existing file server that was using Windows Storage Spaces on Windows 10, I was excited to continue using CrashPlan to backup my family’s computers. Unfortunately, I soon found out that while the Synology can run CrashPlan after reading the tutorial from PCLoadLetter, it was not anything like I was used to for administration. Using CrashPlan installed on the Synology required modifying configuration files on all of the computers that I wanted to backup, instead of simply logging into my CrashPlan account as I had done before. So, I promptly gave up and used Synology’s Cloud Station Backup for 6 months.

Continue reading → Using Synology NAS as CrashPlan Backup Destination