{"id":8,"date":"2005-11-19T10:16:10","date_gmt":"2005-11-19T14:16:10","guid":{"rendered":"http:\/\/thesmithfam.org\/blog\/?p=8"},"modified":"2019-08-12T07:16:43","modified_gmt":"2019-08-12T13:16:43","slug":"python-uptime-script","status":"publish","type":"post","link":"https:\/\/thesmithfam.org\/blog\/2005\/11\/19\/python-uptime-script\/","title":{"rendered":"Python: Uptime script"},"content":{"rendered":"<p>I&#8217;ve been writing Python for a grand total of 2 days. I recently wrote a little Python script to print a Linx or UNIX system&#8217;s uptime. This took about 10 minutes to write, so I was pretty pleased. It even runs well on a 75MHz <a href=\"http:\/\/www.busybox.net\/\">Busybox<\/a> embedded Linux system. Check it out:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#!\/usr\/bin\/python\r\nimport os\r\n\r\n#----------------------------------------\r\n# Gives a human-readable uptime string\r\ndef uptime():\r\n\r\n     try:\r\n         f = open( &quot;\/proc\/uptime&quot; )\r\n         contents = f.read().split()\r\n         f.close()\r\n     except:\r\n        return &quot;Cannot open uptime file: \/proc\/uptime&quot;\r\n\r\n     total_seconds = float(contents&#x5B;0])\r\n\r\n     # Helper vars:\r\n     MINUTE  = 60\r\n     HOUR    = MINUTE * 60\r\n     DAY     = HOUR * 24\r\n\r\n     # Get the days, hours, etc:\r\n     days    = int( total_seconds \/ DAY )\r\n     hours   = int( ( total_seconds % DAY ) \/ HOUR )\r\n     minutes = int( ( total_seconds % HOUR ) \/ MINUTE )\r\n     seconds = int( total_seconds % MINUTE )\r\n\r\n     # Build up the pretty string (like this: &quot;N days, N hours, N minutes, N seconds&quot;)\r\n     string = &quot;&quot;\r\n     if days &gt; 0:\r\n         string += str(days) + &quot; &quot; + (days == 1 and &quot;day&quot; or &quot;days&quot; ) + &quot;, &quot;\r\n     if len(string) &gt; 0 or hours &gt; 0:\r\n         string += str(hours) + &quot; &quot; + (hours == 1 and &quot;hour&quot; or &quot;hours&quot; ) + &quot;, &quot;\r\n     if len(string) &gt; 0 or minutes &gt; 0:\r\n         string += str(minutes) + &quot; &quot; + (minutes == 1 and &quot;minute&quot; or &quot;minutes&quot; ) + &quot;, &quot;\r\n     string += str(seconds) + &quot; &quot; + (seconds == 1 and &quot;second&quot; or &quot;seconds&quot; )\r\n\r\n     return string;\r\n\r\nprint &quot;The system uptime is:&quot;, uptime()\r\n<\/pre>\n<p>One interesting thing about this script is its emulation of the C ternary operator using Python&#8217;s short-circuited &#8220;and&#8221; operator. Notice this bit:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">(days == 1 and &quot;day&quot; or &quot;days&quot; ) <\/pre>\n<p>Python doesn&#8217;t have the C-style ternary operator: <\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">(days == 1 ? &quot;day&quot; : &quot;days&quot;)<\/pre>\n<p>So the short-circuited &#8220;and&#8221; is a decent alternative. Instead of ignorantly printing &#8220;1 days&#8221;, this code will actually print &#8220;1 day&#8221; and &#8220;2 days&#8221; like it should.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been writing Python for a grand total of 2 days. I recently wrote a little Python script to print a Linx or UNIX system&#8217;s uptime. This took about 10 minutes to write, so I was pretty pleased. It even runs well on a 75MHz Busybox embedded Linux system. Check it out: #!\/usr\/bin\/python import os [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-8","post","type-post","status-publish","format-standard","hentry","category-code-and-cruft"],"_links":{"self":[{"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/posts\/8","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/comments?post=8"}],"version-history":[{"count":1,"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/posts\/8\/revisions"}],"predecessor-version":[{"id":1625,"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/posts\/8\/revisions\/1625"}],"wp:attachment":[{"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/media?parent=8"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/categories?post=8"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/tags?post=8"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}