-
This is a numbered list.
-
I'm going to include a fenced code block as part of this bullet:
Code More Code
/** | |
* 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): |
-
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:-
This version generates innocuous, ignorable errors about the cache directory:
COMPOSER_CACHE_DIR=/dev/null composer update
-
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"
}
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:
- lodash.js' (successor to underscore.js)
_.uniqBy()
or_.uniqWith()
: https://lodash.com/docs - "Standard" JS' own
Array.reduce()
method, as explained by: http://elijahmanor.com/reducing-filter-and-map-down-to-reduce/ - angular-filter's
uniq
orunique
filter: https://github.com/a8m/angular-filter
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
These are the examples from the Wikipedia article about reStructuredText, https://en.wikipedia.org/wiki/ReStructuredText .
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.
import pprint; logger.debug(pprint.pformat((vars(self), dir(self)), depth=5)) |