Today I Learned

hashrocket A Hashrocket project

10 posts about #computer-science surprise

ISO 8601 Durations

Today I learned that ISO 8601 includes a spec for durations as well as dates and times.

Normally when I think of ISO 8601 I think of dates (2024-05-30) or times (2024-05-30T19:54:14Z).

But it also includes a spec for durations! Super convenient to have a standardized way to represent this, and rails and javascript libraries are able to parse it.

Some examples:

  • 8 hours => PT8H
  • 3.5 hours => PT3H30M
  • 2 months, 1 day => P2M1D
  • 6 years, 5 months, 4 days, 3 hours, 2 minutes, 1 second => P6Y5M4DT3H2M1S

Files Have Extended Attributes in OSX

I was looking into OSX's quarantine mechanism, and learned how downloaded executables are prevented from running. OSX uses extended attributes to prevent these from running, aka quarantine.

For example, if you install chromedriver with homebrew, then try to open it, you'll see that all too familiar alert that "the developer cannot be verified". How does OSX know to do this? Well, when the file is downloaded, it writes an extended attribute (com.apple.quarantine) key-value to the file (see Gatekeeper). These extended attributes hold additional metadata about each file.

In the terminal, you can view these attributes on a file with the xattr command.

> xattr -l /usr/local/bin/chromedriver
com.apple.quarantine: xxxxxxxxxxxxxxxxxxxxxxxx

You can manually remove an attribute with this utility using the -d flag -

> xattr -d com.apple.quarantine

https://ss64.com/osx/xattr.html

Debugging csound with `printks`

Csound operates uniquely. You can specify certain operations to happen at a certain rate based on the variable name.

kval random 0, 10

This line of code produces a random value between 0 and 10, 1000 times a second because it populates a k-rate (control rate) variable.

But what if I want to debug this? Or see in the console what values I'm producing? You can use the printks opcode.

kval random 0, 10
printks "kval: %f\n", 1, kval

Clearly, 1000 prints a second would eat up the console quickly. printks's second argument is how many seconds between prints. It's debouncing printing automatically.

It's output looks like this:

kval: 9.635761
kval: 6.579237
kval: 6.564805
kval: 7.650385
kval: 3.684652
kval: 2.481188
kval: 3.298008
kval: 5.385859
kval: 0.559652
kval: 5.215418

Read more about it here.

Access System Information On OS X

On machines running OS X, there is an Apple icon in the upper left corner as part of the menu bar. Clicking on this icon reveals a number of options. The first reads About This Mac.

If you hold down the option key, however, that first option will instead read System information...

Select that option to access the System Information panel which can tell you details about your hardware, software, and network.

h/t Dillon Hafer

Run A Hardware Check On A Mac

If your Mac is behaving in an odd way, there may be an issue with some piece of the hardware -- such as the RAM.

You can perform a hardware check in order to chase down a diagnosis.

  • Shutdown your machine
  • Boot your machine
  • While it is booting, hold down the d key

At this point, the machine should have booted into a special hardware check mode. Select your preferred language, the hardware check will be performed, and any issues will be reported.

h/t Dillon Hafer

Pure Data's netreceive uses special protocol

Pure Data is graphical programming language specialising in audio applications. It has an object called netreceive that is a socket (either TCP or UDP) that accepts a connection and outputs messages sent to it.

There are 2 gotcha's that I experienced in working with it.

The first. It requires a special protocol, called FUDI. Each message must be terminated by a semicolon ;. The message can contain any number of 'atoms' (strings) separated by whitespace but the semicolon is essential.

The second. The documentation of pure data contains running examples, if you open the documentation of netreceive before running the object in your patch, you will get a bind: Address already in use (48) error. Close the documentation and try again!

Casting graphql types with inline fragments

Graphql works with a type system. You ask for fruit and it returns fruit, but that fruit could be a pear or an orange.

(contrived example apologies)

interface Fruit {
  seedless: Boolean
}

type Pear implements Fruit {
  color: String
}

type Apple implements Fruit {
  size: Int
}

So when you ask for fruit, and you want the size of the apple, you have to let graphql know that you want an Apple and are expecting an Apple and while I'm thinking about this as casting the graphql docs call it inline fragments.

  query {
    fruit {
      seedless
      ... on Pear {
        color
      }
      ... on Apple {
        size
      }
    }
  }

The ... is a syntactic element not a writing convention.

Size of RSA encrypted content

RSA encryption (publickey/privatekey) is powerful encryption but its really only meant to encrypt small things like other encryption keys. The maximum size of the amount of data an rsa public key can encrypt is:

(key_size/8).floor - 11

So in the case of a key thats 1024 bits it can only encrypt data up to 117 bytes long. Not long enough for documents of any size or variable length.