Skip to content

Instantly share code, notes, and snippets.

View lsloan's full-sized avatar

Mr. Lance E Sloan «UMich» lsloan

  • Teaching and Learning (@tl-its-umich-edu) at University of Michigan: Information and Technology Services
  • Ann Arbor, Michigan, USA
  • 04:08 (UTC -04:00)
  • X @lsloan_umich
View GitHub Profile
@lsloan
lsloan / InsertDate.gs
Created November 17, 2015 16:46 — forked from thomxc/InsertDate.gs
Google Docs Script Macro: Insert Date
/**
* The onOpen function runs automatically when the Google Docs document is
* opened. Use it to add custom menus to Google Docs that allow the user to run
* custom scripts. For more information, please consult the following two
* resources.
*
* Extending Google Docs developer guide:
* https://developers.google.com/apps-script/guides/docs
*
* Document service reference documentation:
'Demonstrate effective use of super() -- Python 3.2 version'
import collections
import logging
logging.basicConfig(level='INFO')
class LoggingDict(dict):
# Simple example of extending a builtin class
def __setitem__(self, key, value):
@lsloan
lsloan / composer_notebook.md
Last active December 11, 2015 16:52
My Composer notebook

Composer Notebook

  1. Ignoring the cache: Composer doesn't have an option to make it ignore its cache while running a command. The Composer developers do not believe this is an important feature and they refused an issue opened to request it. However, it can be helpful for debugging dependency installation. To make a single Composer command (e.g., composer update) ignore the cache, use one of the following:

    1. This version generates innocuous, ignorable errors about the cache directory:

      COMPOSER_CACHE_DIR=/dev/null composer update
@lsloan
lsloan / Toggle Character Viewer.workflow
Created June 12, 2015 18:12
When saved as an Automator workflow service in OS X, this Applescript will toggle the visibility of the character viewer palette. This is useful in older versions of OS X, which didn't provide easy access to special characters like emoji, etc. I bound this workflow to the Command-Option-Control-C key press for easy access.
on isRunning(applicationName)
tell application "System Events"
set appNameIsRunning to exists ¬
(processes where name is applicationName)
end tell
return appNameIsRunning
end isRunning
on run {input, parameters}
@lsloan
lsloan / markdown_list_codeblock.md
Last active December 15, 2015 15:56 — forked from clintel/gist:1155906
GitHub Markdown lists with embedded code blocks

Fenced code blocks inside ordered and unordered lists

  1. This is a numbered list.

  2. I'm going to include a fenced code block as part of this bullet:

    Code
    More Code
    

I've noticed when using DefiantJS's XPath features to search a list of Caliper event JSON from OpenLRS that it's necessary to use some special node names that aren't explcitly shown in the data. For example, if the JSON were:

[
    {
        "edApp": {
            "@id": "https://example.com/super-media-tool",
            "@type": "http://purl.imsglobal.org/caliper/v1/SoftwareApplication",
            "name": "Super Media Tool"
 }
@lsloan
lsloan / README.md
Last active February 17, 2016 19:22
A very simple filter intended for use with AngularJS's `ng-repeat`, etc. to use only unique array items.

I wrote this for a project that didn't want to import additional libraries just for this functionality.
It could probably be more efficient and it only supports direct attributes of the item in question.
It would be nice to add support for attr.subattr.etc at some point. (Maybe using AngularJS's $parse()?)

Other similar functions:

@lsloan
lsloan / README.md
Last active March 10, 2016 15:58
PHP DateTime Posix timestamp handling examples

This set of examples was inspired by a novice colleague wrote the code shown in example 1. The code was intended to convert a string representation of a Posix timestamp into a DateTime object, but does it very inefficiently. It converts the timestamp to a formatted date string, then to a timestamp integer, and finally to a DateTime object, with the call to the DateTime constructor converting the integer into a string prefixed with "@".

Examples 2 and 3 show the conversion can be done in fewer steps.

Because the original code wrapped these conversions in a test for is_string($startTime) test, example 4 demonstrates what would happen if the string doesn't contain what was expected. For example, a human-readable date format. Exceptions are thrown.

Example 5 shows how the code could deal with the Posix vs. human-readable timestamps by attempting to use the first format, then falling back to use the other. I'm still unsure whether it's proper to just drop the first exception if the first attem

@lsloan
lsloan / README.md
Last active March 18, 2016 15:28
PHP default values for unset variables

In PHP, I wanted to do something like $myArray->get('key', 'default'), but that doesn't work in PHP for a number of reasons. (Namely, arrays aren't objects and there isn't a get() function for arrays.) I found it worked with @$myArray['key'] || false, but only because I was using a Boolean value, like false. The || operator always causes the interpreter to treat expressions on each side of it as Booleans. If I tried a different default value, like @$myArray['key'] || 'default', it didn't work as I expected, because it treats 'default' as a Boolean true.

However, since version 5.3, PHP's ternary operator allows an empty "then" result, which returns the initial condition if it's true, otherwise, it returns the required "else" result. The following code illustrates the difference between using || and ?:.

Update: I've noticed that if the initial value used with ?: is an empty, non-null string (i.e. ''), this breaks down. It's being converted to a Boolean false, apparently.