Lawrence D’Oliveiro
Forking GitHub Repos

I’ve lost count of the number of times that other GitHub users have forked one of my repositories and then sat on their copy and did nothing.

If you want your own personal copy of a repo, by all means use git clone to mirror it to your PC. Is there a need for your copy to be public on GitHub, when it contains nothing but a (gradually becoming obsolete) snapshot of the original?

If you plan to do some of your own development on it, then my advice is: don’t do the fork until you are ready to push your first change to it. This is because, at the time you fork, the owner of the repo you fork from gets a notification, and that is the time I will go to have a look at your copy, to see if you’ve done anything interesting yet. If I see nothing, I will probably forget to go back at some later point to recheck, so you are unlikely to get a second chance at my attention.

So use GitHub wisely.

The “Equation” That Changed My Life

At school, I was good at maths and science. But from an early age, I was fascinated by these things called “computers” that were portrayed on TV and in books (this was the early 1970s in a South-East-Asian country, so they were not exactly household objects). I read up everything I could about them, but all the popular accounts were frustratingly short on detail. I was very impressed when some older boys built a rudimentary one (just a few lights and switches) for a science fair at school.

Then some school friends and I got membership at the British Council library in town. They had more books than I had ever seen before, including a full set of tne Encyclopedia Britannica. Alone of all the encyclopedias I had seen up to that point, the Britannica article on “Computers” actually had examples of proper program code! (It was in FORTRAN, but, hey, that was still fantastic to me.) In among all the sample statements, there was this:

N = N + 1

As I said, I was good at maths, and I knew what an equation was—enough to realize that this made no sense as a mathematical equation—there was no value of N (at least, no finite value) which would satisfy it!

But the key point was, in FORTRAN, the “=” denotes, not equality, but assignment. The statement means, “take the current value in the location denoted by N, add 1 to it, and put it into the location denoted by N”.

Once I had grasped this concept, I understood a whole lot more about computers than I had before.

Other languages from around the same time, designed by Proper Computer Scientists, used “:=” to denote assignment, leaving “=” to represent something closer to its mathematical meaning of equality. But unfortunately, the later popularity of C, which uses FORTRAN-style “=” for assignment, and invented another operator, “==”, to denote equality comparison, has probably meant that whole new generations of maths-savvy teenagers will have to go through the confusion I did.

I have a slight problem with this, though. The .DS_Store isn’t related to your project, it is related to the system you’re coding on. Yor .gitignore file will be a lot more elegant if it only lists files that are related to the projects. For a Rails app, log files and db/schema.rb is definitely app related. .DS_Store isn’t, though.
August Lilleaas (via papaeye)

Aaahhh, stick it in your bloody .gitignore and be done with it. So what if it’s ignored even on systems which do not create .DS_Store files? Keep It Simple, Stupid!

ldo17:

helenismelon:

why is Javascript such an ugly language

so many fucking brackets

everywhere

i… i think i actually miss python

It’s not the brackets, it’s the stupid semicolon rule. And the peculiar variable scoping. And the stupid semicolon rule. And automatic type…

It’s not that hard to keep track of matching brackets. As soon as I insert the opener, I also insert the closer at the same time, and continue typing between them. Also it helps to keep the braces properly indented, on lines by themselves, e.g.

if (cond)
  {
    for (loopstuff)
      {
        ..
      } /*for*/
    ..
  }
else
  {
    ..
  } /*if*/

Also decent editors like Emacs include bracket-matching functions, so you can see which opener matches with which closer, and spot mismatches.

startcodelabs:

We have decided to use Scratch and Python as the starter programming languages in our Labs. We recently discussed how we arrived at this decision in our blog. Do you agree? If not, what do you think are the best languages or tools to get started?

Java certainly fits the definition of being free…

Agree with choice of Python, don’t know that much about Scratch. Definitely agree with avoiding Java for now—it is often a much more long-winded and less flexible language when trying to express the same things as Python.

Conditional Expressions In Python

The Python syntax for conditional expressions (introduced in Python 2.5) is

trueval if cond else falseval

I think this is bloody awful. Why couldn’t they have adopted the standard C syntax, as used in a whole bunch of other C-derivative languages?

cond ? trueval : falseval

On the bright side, there are some alternative constructs that allow you to avoid that horrible syntax. Here’s one I have used a few times:

(falseval, trueval)[cond]

For example:

NrRows = (3, 5)[LargeTable]

The main disadvantage with this is that it doesn’t do short-circuit evaluation: both falseval and trueval are always evaluated, regardless of the value of cond. For example, this will crash with a “TypeError: ‘NoneType’ object is not subscriptable” if Dict happens to be None:

Value = (None, Dict[Key])[Dict != None]

It also requires that cond evaluate to 0, 1, True or False, not allowing other types of values like standard Python conditionals, but I don’t see this as a disadvantage.

Here is another form I have seen suggested:

cond and trueval or falseval

For example (rewriting the crashing example above):

Value = Dict != None and Dict[Key] or None

This one correctly short-circuits the evaluation of the arms of the conditional, provided that trueval does not return a value that can be interpreted as false. In other words, it can malfunction in mysterious ways if you’re not careful.

Finally, here’s an idea I came up with, which introduces short-circuit evaluation into the first alternative I mentioned above:

(lambda : falseval, lambda : trueval)[cond]()

For example:

Value = (lambda : None, lambda : Dict[Key])[Dict != None]()

Don’t you just love lambda-expressions?

But Wait, There’s More

This idea can be carried further. Some languages have case-expressions, which are the expression equivalent of switch/case statements. The above lambda-using template can be extended to implement these, either as simple indexing into an array of alternatives:

(
    lambda : case_0_expr,
    lambda : case_1_expr,
    lambda : case_2_expr,
    
)[index]()

which evaluates to case_0_expr if index equals 0, case_1_expr if index equals 1 and so on; or by a more general table lookup:

{
    key1 : lambda : case_key1_expr,
    key2 : lambda : case_key2_expr
    key3 : lambda : case_key3_expr,
    
}[selector]()

which evaluates to case_key1_expr if selector equals key1, case_key2_expr if selector equals key2 and so on. This last form can also be extended to include a default case, if selector matches none of the keyed alternatives:

{
    key1 : lambda : case_key1_expr,
    key2 : lambda : case_key2_expr
    key3 : lambda : case_key3_expr,
    
}.get(selector, lambda : default_expr)()
Why Can’t Java Be More Like Python?

Sure, I’m spoiled by programming-language choice. And I appreciate that a lot of the complexities of Java arise from

  • the desire to root out potential programming errors as early as possible—at compile time, if possible, rather than run time;
  • the need for backward compatibility with code written for older versions of Java.

But I still can’t help considering the fact that the Java Language Specification, 3rd Edition, is well over 600 pages, while the equivalent Python language reference, even adding in the built-in stuff from the Library Reference, only works out to about 120 printable pages. And this in spite of all the extra cool features that Python provides, such as:

  • Overloading of built-in operators;
  • Much simpler and more flexible namespace management;
  • More powerful functional constructs: dictionary expressions, list comprehensions, generator functions;
  • Object-orientation is optional: you can declare a function all by itself, outside of any class, and functions are first-class objects. The closest Java can come is to let you create a Runnable object, which is just a little bit unwieldy;
  • Functionality of ctypes that offers direct access to native libraries more simply than anything you can do in Java with JNI or anything like that;
  • A much more rationally-designed standard library generally: compare how much easier it is to use POSIX select/poll/epoll calls with the select module versus the baroque, convoluted nightmare that is the java.nio.channels API.
Symbolic Signal Names

Python’s signal module defines names for the standard POSIX signals. It would be nice to be able to use these in reverse: that is, if a piece of code gets a signal, to show the symbolic name of the signal, rather than a number, to the user.

Here’s how to create a dictionary of such code-to-name mappings:

import signal

SignalName = {}
for Name in dir(signal) :
  # collect symbolic names for all signals
    Value = getattr(signal, Name)
    if Name.startswith("SIG") and type(Value) == int :
        SignalName[Value] = Name
    #end if
#end for

And here’s an example of how to use it. Assume you have a child process termination value ChildStatus, for example set by

ChildStatus = os.waitpid(ChildPid, 0)[1]

Then you can do the usual check for whether the child process was terminated by a signal, with added display of a symbolic name for the signal if one can be found:

if os.WIFSIGNALED(ChildStatus) :
    SigValue = os.WTERMSIG(ChildStatus)
    SigName = SignalName.get(SigValue, "?")
    sys.stderr.write("Child died from signal %d (%s)\n" % (SigValue, SigName))
#end if

which might produce an output message like

Child died from signal 15 (SIGTERM)

A similar trick can be used, for instance, to get symbolic names for error codes from the errno module.

Fuck Time Zones

underwhelmed:

Everyone should just use GMT

Programming for “US Eastern Standard Time” vs “Eastern Standard Time” including all the fucking little changes that have happened along the way, AND building in the flexibility to change it when lawmakers decide to move across time zones is a bag of dicks.

I’ll see you at zero one hundred, buddy

Unfortunately, people’s daily rhythms get quite upset when they don’t bear some relationship to sunlight. We need local times. But the problem you complain about has already been solved: the solution is called the Olson database, available on all Linux-type systems as /usr/share/zoneinfo. There are standard POSIX routines for getting the info from these, and if those aren’t enough, it’s not hard to parse the files yourself.

The key point is that the computer should always store and process dates/times as UTC, but display them to the user (and accept date/time specifications from the user) in the user’s choice of local timezone. That way, there is never any ambiguity about what moment in time a particular date/timestamp is referring to.

After all, technology is here to serve people, not the other way round. Why shouldn’t our machines adapt to our way of doing things? And it really is not that hard to do, when you use the solution that others have already provided.

Showing Local Time On A Web Page

It always annoys me when websites show times in their time zone rather than mine. Some sites let you configure what time zone they use when showing pages to you, but why bother? The user’s browser is running on the user’s own system, which already has a time zone configured, why not simply use that? Here’s what should happen:

  • The server stores all dates/times in UTC.
  • All date/time information passed between the server and the user’s browser is in UTC.
  • The user’s browser automatically takes care of displaying all dates/times in local time, and where necessary, accepting user input in local time.

How to do this? It’s so simple: JavaScript includes all the necessary functionality to perform the conversions, all you have to do is generate web pages including the necessary script sequences. For example, for example, suppose the date/time you want to show is 1979 Mar 15 21:30:00 UTC. A common convention (which originated on Unix systems) is to represent this as some number of seconds since 1970 Jan 1 00:00:00 UTC. JavaScript wants the offset in milliseconds instead, but it’s easy to convert:

<script>
TheTime = 290381400
document.write(new Date(TheTime * 1000).toLocaleString())
</script>