<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Passing Curiosity: Posts tagged notes</title>
    <link href="https://passingcuriosity.com/tags/notes/notes.xml" rel="self" />
    <link href="https://passingcuriosity.com" />
    <id>https://passingcuriosity.com/tags/notes/notes.xml</id>
    <author>
        <name>Thomas Sutton</name>
        
        <email>me@thomas-sutton.id.au</email>
        
    </author>
    <updated>2016-06-22T00:00:00Z</updated>
    <entry>
    <title>Rambling about time zones</title>
    <link href="https://passingcuriosity.com/2016/rambling-about-timezones/" />
    <id>https://passingcuriosity.com/2016/rambling-about-timezones/</id>
    <published>2016-06-22T00:00:00Z</published>
    <updated>2016-06-22T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>I wrote this as a reply to a private discussion about ways to handle
date and time values in data processing systems. Rather than continue
a discussion that really wasn’t going to lead anywhere interesting I
saved the draft and now I’m posting it here. It’s probably not very
interesting or useful but it’s been a while since I last posted.</p>
<p>If you are interested in time in programs go read
<a href="http://infiniteundo.com/post/25326999628/falsehoods-programmers-believe-about-time">falsehoods programmers believe about time</a>. If you are interested
in historical times make sure you’re aware of travesties like
<a href="https://github.com/eggert/tz/blob/master/australasia#L187">Sydney’s pre-standard time offset</a> of 10:04:52.</p>
<hr />
<p>Future times can’t, in general, be specified in UTC timestamp and
offset: the definitions of timezones can and do change and can change
with little notice. For dates this doesn’t make too much of a
difference (a date, of course, denotes the whole 23, 24, or 25 hour
period and not some particular instant within the day) but values with
higher precision it’s possible (though in Australian fairly unlikely)
to convert a future date/time to UTC with a time zone definition which
is <em>not</em> the one which actually applies when the time occurs.</p>
<p>There’s no particular reason to expect any polity to not change it’s
time zone over any particular period (New South Wales did it for the
Olympic and Commonwealth Games; Fiji changed it’s DST start/end dates
in 2011, 2012, 2013, and 2014; Israel negotiated the dates in
parliament in every year but now has a regular schedule - except where
it conflicts Jewish new year).</p>
<p>You need to represent a local date and time in the target time zone
and, at some point, commit to using the time zone definitions you
happen to have to interpret the local time into UTC. Ideally this
would be safe for instants in the upcoming 12 months , say, but who
knows? For some polities it might make sense to commit with the time
zone definitions we have now (DST in Australia’s eastern states has
settled down in the last few years and there are still a few more
years before they can attempt another referendum to introduce it to
Western Australia) but not in others.</p>
<p>PS: As a person who lives in the world and interacts with people and
organisations around me, I’m using “time” to mean “the numbers which
are supposed to correspond to the the numbers on my clock” and not
“absolute time”, “UTC”, etc. I generally try to use words like
“instant” and “timestamp” (respectively) to refer to those latter
things.</p>
<hr />
<p><strong>Update</strong> On Monday, July 4 2016 a meeting of Egypt’s cabinet
<a href="http://www.sis.gov.eg/En/Templates/Articles/tmpArticleNews.aspx?ArtID=105572">cancelled daylight savings time</a> which was due to start on
Thursday, July 7. Three day’s notice is enough to upgrade the TZ
database on all your computer systems, right?</p>]]></summary>
</entry>
<entry>
    <title>Attribute Oriented Programming in PHP</title>
    <link href="https://passingcuriosity.com/2010/oplm2010-4-aop-in-php/" />
    <id>https://passingcuriosity.com/2010/oplm2010-4-aop-in-php/</id>
    <published>2010-01-18T00:00:00Z</published>
    <updated>2010-01-18T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>The fourth talk at the <a href="http://blogs.tucs.org.au/oplm/">Open Programming Language Miniconf 2010</a> was
Peter Serwylo’s presentation about <em><a href="http://blogs.tucs.org.au/oplm/programme/#aopphp">Attribute Oriented Programming in
PHP</a></em>. His <a href="http://lca2010.serwylo.com/">notes</a> are available online.</p>
<h2 id="attributes">Attributes</h2>
<p>Attributes are “extra” bits embedded in source code that doesn’t <em>necessarily</em>
mean anything in the language. Common examples include the documentation
annotations in many comment systems (such as the tags used in Javadoc
comments), JVM annotations, and .Net attributes.</p>
<p>A testing framework, for example, might use a <code>TestFunction</code> attribute to
identify which methods should be called.</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode java"><code class="sourceCode java"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="at">@TestFunction</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="kw">public</span> function <span class="fu">testValidation</span><span class="op">()</span> <span class="op">{</span><span class="kw">...</span><span class="op">}</span></span></code></pre></div>
<div class="sourceCode" id="cb2"><pre class="sourceCode cs"><code class="sourceCode cs"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="op">[</span>TestFunction<span class="op">]</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="kw">public</span> function <span class="fu">testValidation</span><span class="op">()</span> <span class="op">{...}</span></span></code></pre></div>
<h2 id="use-cases">Use cases</h2>
<p>In Java EE you have “beans” which encapsulate business logic. Each bean
requires a bunch of wrappers for remote access, “home” access (whatever that
means), XML files, etc. Keeping all this relatively “boiler-plate” code in
synch is painful. Annotating the bean classes and methods with attributes
makes it possible to generate most of this automatically. No more manual
synching XML files.</p>
<p>Java, though, (as of Java 5) has built in support for <a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html">annotations</a> and
is a compiled language. This makes it relatively simple to do this analysis
and generation at build time. PHP, on the other hand, doesn’t have an
annotation mechanism and doesn’t <em>have</em> a built time.</p>
<p>Instead, Peter’s approach is to use PHP comments to add the attributes. At
load time, these comments are processed using the reflection API and this is
used to build a data structure to use for access control. See the
<a href="http://lca2010.serwylo.com/06_ivt_example.html">permissions example</a>.</p>
<h2 id="a-more-complex-case">A more complex case</h2>
<p>To take this a little further, Peter <a href="http://lca2010.serwylo.com/07_php_zend_example.html">wrote a Zend
extension</a> that makes it
possible to [sort of] do aspect oriented programming (on function points) in
PHP. The <code>add_function_hook()</code> function allows you to register an attribute
and a callback and the extension will call your callback before a function
with that attribute is executed:</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode php"><code class="sourceCode php"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="kw">&lt;?php</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="kw">class</span> RequiresLogging</span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a>{</span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a>	<span class="co">/**</span></span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a><span class="co">	 * </span><span class="an">@log</span><span class="co"> notice &quot;Secret action performed by user [user]</span></span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a><span class="co">	 */</span></span>
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a>	<span class="kw">public</span> <span class="kw">function</span> secretAction()</span>
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a>	{</span>
<span id="cb3-10"><a href="#cb3-10" aria-hidden="true" tabindex="-1"></a>		<span class="co">// perform secret action...</span></span>
<span id="cb3-11"><a href="#cb3-11" aria-hidden="true" tabindex="-1"></a>	}</span>
<span id="cb3-12"><a href="#cb3-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-13"><a href="#cb3-13" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb3-14"><a href="#cb3-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-15"><a href="#cb3-15" aria-hidden="true" tabindex="-1"></a><span class="co">// </span></span>
<span id="cb3-16"><a href="#cb3-16" aria-hidden="true" tabindex="-1"></a><span class="co">// The callback function</span></span>
<span id="cb3-17"><a href="#cb3-17" aria-hidden="true" tabindex="-1"></a><span class="co">//</span></span>
<span id="cb3-18"><a href="#cb3-18" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> performLog( <span class="va">$message</span> )</span>
<span id="cb3-19"><a href="#cb3-19" aria-hidden="true" tabindex="-1"></a>{</span>
<span id="cb3-20"><a href="#cb3-20" aria-hidden="true" tabindex="-1"></a>	<span class="va">$parts</span> <span class="op">=</span> <span class="cn">split</span>( <span class="st">&quot; &quot;</span><span class="ot">,</span> <span class="va">$message</span><span class="ot">,</span> <span class="dv">2</span> )<span class="ot">;</span></span>
<span id="cb3-21"><a href="#cb3-21" aria-hidden="true" tabindex="-1"></a>	<span class="va">$level</span> <span class="op">=</span> <span class="va">$parts</span>[<span class="dv">0</span>]<span class="ot">;</span></span>
<span id="cb3-22"><a href="#cb3-22" aria-hidden="true" tabindex="-1"></a>	<span class="va">$message</span> <span class="op">=</span> <span class="va">$parts</span>[<span class="dv">1</span>]<span class="ot">;</span></span>
<span id="cb3-23"><a href="#cb3-23" aria-hidden="true" tabindex="-1"></a>	Logger::<span class="fu">log</span>( <span class="va">$level</span><span class="ot">,</span> <span class="va">$message</span> )<span class="ot">;</span></span>
<span id="cb3-24"><a href="#cb3-24" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb3-25"><a href="#cb3-25" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-26"><a href="#cb3-26" aria-hidden="true" tabindex="-1"></a><span class="co">// </span></span>
<span id="cb3-27"><a href="#cb3-27" aria-hidden="true" tabindex="-1"></a><span class="co">// Register the callback</span></span>
<span id="cb3-28"><a href="#cb3-28" aria-hidden="true" tabindex="-1"></a><span class="co">// </span></span>
<span id="cb3-29"><a href="#cb3-29" aria-hidden="true" tabindex="-1"></a>add_function_hook( <span class="st">&quot;log&quot;</span><span class="ot">,</span> <span class="st">&quot;performLog&quot;</span> )<span class="ot">;</span></span>
<span id="cb3-30"><a href="#cb3-30" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-31"><a href="#cb3-31" aria-hidden="true" tabindex="-1"></a><span class="co">// Now code like this will result in a log message being recorded</span></span>
<span id="cb3-32"><a href="#cb3-32" aria-hidden="true" tabindex="-1"></a><span class="va">$object</span> <span class="op">=</span> <span class="kw">new</span> RequiresLogging()<span class="ot">;</span></span>
<span id="cb3-33"><a href="#cb3-33" aria-hidden="true" tabindex="-1"></a><span class="va">$object</span>-&gt;secretAction()<span class="ot">;</span></span></code></pre></div>
<p>This type approach is flexible and extremely powerful. You can inject
permission checking code and raise an exception before the real function is
even called.</p>
<p>There are a bunch of memory leaks and possible bugs in the Zend extension, but
it is working and in production use!</p>]]></summary>
</entry>
<entry>
    <title>Notes on Introducing Gearman</title>
    <link href="https://passingcuriosity.com/2010/oplm2010-2-gearman/" />
    <id>https://passingcuriosity.com/2010/oplm2010-2-gearman/</id>
    <published>2010-01-18T00:00:00Z</published>
    <updated>2010-01-18T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>The second talk at the <a href="http://blogs.tucs.org.au/oplm/">Open Programming Language Miniconf 2010</a> was
by <a href="http://datacharmer.blogspot.com">Giuseppe Maxia</a>’s <em><a href="http://blogs.tucs.org.au/oplm/programme/#gearman">Introducing Gearman – Distributed server for all
languages</a></em>. Though there were a few issues – the power dropped out,
taking the projector, audio, and recording gear out – it was an interesting
introduction to a system that I want to learn more about.</p>
<p>After a bit of background on the mainframe, mini, server, desktop progression,
Guiseppe introduced the space that Gearman operates in: distributed
heterogenous system (like the web). Amongst Gearman’s peers in the space
(not to say that they fill the same role 2at all) is <code>memcached</code>.</p>
<p>Gearman is a client/server system for distributing jobs from clients to a
number of workers. Guiseppe drew an analogy to a manager in a company: the
manager gets a request from a customer, delegates it to one or more employees,
and hands the result back to the client (monitoring progress along the way and
assigning the job to new employee/s if the current ones are, e.g., hit by a
bus).</p>
<p>The Gearman server fills the role of the manager: it accepts connections from
clients and one or more workers (which can be local to the server or on other
hosts). The Gearman server is responsible for then farming the jobs submitted
by the clients out to appropriate worker/s for execution.</p>
<p>There are a numerous ways that this can be useful. It can help to decouple
applications and features from each other. Rather than re-writing existing
components for use in projects in another language or on a different platform,
you can use Gearman to mediate the invocation of the existing code from the
new system. Similarly, you can use it to offload computationally intensive
tasks to more capable systems.</p>
<p>Using Gearman is reasonably simple: the <code>gearmand</code> server is written in C and
there are client libraries available for, according to Guiseppe, “everything
that counts”: C/C++, PHP, Python, etc. You just start a server, start one or
more workers, and then use one (or more) of the available client libraries to
submit jobs.</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Start a server</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="va">PORT</span><span class="op">=</span>1234</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="ex">gearmand</span> <span class="at">-d</span> <span class="at">-v</span> <span class="at">-p</span> <span class="va">$PORT</span></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="co"># Start a worker</span></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="ex">gearman</span> <span class="at">-p</span> <span class="va">$PORT</span> <span class="at">-f</span> count_lines <span class="at">--</span> wc <span class="at">-l</span></span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a><span class="co"># Submit a job</span></span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a><span class="ex">gearman</span> <span class="at">-p</span> <span class="va">$PORT</span> <span class="at">-h</span> localhost <span class="at">-f</span> count_lines <span class="op">&lt;</span> /etc/passwd</span></code></pre></div>
<p>The above code starts a Gearman server, starts a worker that can compute the
<code>count_lines</code> function (by running <code>wc -l</code> on its input), and submits a job.</p>
<p>There are some rather more complex uses. One of those that Guiseppe mentioned
is using the client in user defined functions in a relational database server
(he used <a href="http://forge.mysql.com/tools/tool.php?id=235">MySQL</a>, but PostgreSQL is also supported). With appropriate
jobs and workers this can be used to retrieve status information from, e.g.,
MySQL slave servers and store it in a database on the master (which will then
be replicated to the slaves giving all nodes knowledge of the health of the
whole cluster).</p>
<p>To help avoid introducing a single point of failure, you can deploy multiple
servers and use all of them: if one fails, then clients and workers will try
the next. It wasn’t clear, though, if the client libraries do this sensibly
(re-submitting jobs, etc.)</p>
<p>For more information, your first stop should probably be the official
<a href="http://gearman.org/">Gearman</a> web-site. There are also a number of posts on <a href="http://datacharmer.blogspot.com">Guiseppe’s
blog</a>.</p>]]></summary>
</entry>
<entry>
    <title>Notes on Haskell, and all the wonderful things it doesn’t let you do</title>
    <link href="https://passingcuriosity.com/2010/oplm2010-1-haskell/" />
    <id>https://passingcuriosity.com/2010/oplm2010-1-haskell/</id>
    <published>2010-01-18T00:00:00Z</published>
    <updated>2010-01-18T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>The first talk at the <a href="http://blogs.tucs.org.au/oplm/">Open Programming Language Miniconf 2010</a> was
by Stephen Blackheath’s <em><a href="http://blogs.tucs.org.au/oplm/programme/#haskell">Haskell, and all the wonderful things it doesn’t let
you do</a></em>. This was cool – given that Haskell is my favourite language
– but I didn’t get much out of it beyond a link to <a href="http://en.wikibooks.org/wiki/Haskell/StephensArrowTutorial">Stephen’s Arrows
Tutorial</a>.</p>
<p>The talk, aimed at newcomers, covered a lot of ground. It started with a
characterisation of Haskell as a pure, functional, non-strict, strongly,
statically typed, programming language and gave all the usual reasons that
those are nice properties for a programming language to have.</p>
<p>After a few examples of the syntax, Stephen talked about recursion and
mentioned that idiomatic Haskell tends to use a higher-order function
implementing a particular recursions scheme, but didn’t really point any out
(other than using <code>map</code> in example).</p>
<p>Next was a brief discussion of the type system: it’s strong and static, but it
also features type inference so you don’t need to supply signatures for
everything. It’s probably important to note though, that it’s still a good
idea for top level definitions for documentation purposes, if for no other
reason. Next was a mention of QuickCheck and automatic testing.</p>
<p>Finally, there was a bit about Haskell’s non-strict semantics and the usual
points about concurrency (being hard in imperative languages and rather easier
in Haskell). Laziness is useful as it makes it possible to represent
[potentially] infinite data structures and looping data structures without
destructive updates, alas it can make it hard (or, at least, harder) to reason
about and predict the time and space behaviour of a program.</p>
<p>It was a pretty good talk, but I though Stephen missed a few opportunities to
make some important points that the audience might have appreciated. In
particular, I’d have mentioned the various higher-order recursion operators
and opportunity they provide for automatic application-specific optimisations
through fusion. This is the sort of thing that is difficult or impossible in a
lot of languages but are seriously cool!</p>]]></summary>
</entry>

</feed>
