{"id":43,"date":"2013-09-05T14:27:00","date_gmt":"2013-09-05T14:27:00","guid":{"rendered":"https:\/\/sandrock.co.za\/carl\/2013\/09\/05\/printing-offset-page-ranges\/"},"modified":"2013-09-05T14:27:00","modified_gmt":"2013-09-05T14:27:00","slug":"printing-offset-page-ranges","status":"publish","type":"post","link":"https:\/\/sandrock.co.za\/carl\/2013\/09\/printing-offset-page-ranges\/","title":{"rendered":"Printing offset page ranges"},"content":{"rendered":"<p>I was printing a document the other day that contained some colour pages. Being a cheapskate, I printed the whole document to the black-and-white laser printer down the hall, then printed the colour pages on the more expensive colour printer.<\/p>\n<p>Unfortunately, I had just written down the ranges of the pages to print using the page numbers on the document, but I had forgotten that the page number had started again from page 12 in the actual document. This meant I had printed all the ranges 12 pages shifted on the colour document. It wasn&#8217;t a complete loss, as some of the pages overlapped. How to print only the pages which hadn&#8217;t been printed already? Unix shell to the rescue!<\/p>\n<p>I came up with the following one-liner which saved me literally <i>minutes<\/i> of menial counting:<\/p>\n<pre>echo {46..50} {52..56} {59..68} 70 \n| tr \" \" \"n\" \n| awk '{printed[$1+12]++; printed[$i]--} END {for (i in printed) if (printed[i]==1) print i}' \n| sort | tr \"n\" \",\" <\/pre>\n<p>\nThis yields a nice comma separated list of pages that I can print:<\/p>\n<pre>58,71,72,73,74,75,76,77,78,79,80,82<\/pre>\n<p>I had also previously written a range parsing class in Python, which looks like this:<br \/>\n<\/p>\n<pre>class Range(object):\n    \"\"\" class containing the idea of non-contiguous ranges \"\"\"\n    def __init__(self, s):\n        \"\"\" Parse range from string \"\"\"\n        self.string = s\n        self.items = []\n        for group in s.split(','): # 1-2,5-7,9\n            if '-' in group:\n                [start, stop] = [int(e) for e in group.split('-')]\n            else:\n                start = int(group)\n                stop = start\n            items = range(start, stop+1)\n            self.items = self.items + items\n    def __iter__(self):\n        return iter(self.items)\n       \n    def __str__(self):\n        return self.string\n        \n    def __repr__(self):\n        return \"Range('\" + str(self) + \"')\" \n<\/pre>\n<p>With that defined, I could also solve the problem like this in a python console:<\/p>\n<p><\/p>\n<pre>wantedtoprint = set(Range('46-50,52-56,59-68,70').items)\nprinted = set(i+12 for i in wantedtoprint)\nsorted(printed.difference(wantedtoprint))\n\n=&gt; [58, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 82]\n<\/pre>\n<p>This is why I always have a terminal open.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was printing a document the other day that contained some colour pages. Being a cheapskate, I printed the whole document to the black-and-white laser printer down the hall, then printed the colour pages on the more expensive colour printer. Unfortunately, I had just written down the ranges of the pages to print using the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[1],"tags":[],"class_list":["post-43","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/sandrock.co.za\/carl\/wp-json\/wp\/v2\/posts\/43","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sandrock.co.za\/carl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sandrock.co.za\/carl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sandrock.co.za\/carl\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sandrock.co.za\/carl\/wp-json\/wp\/v2\/comments?post=43"}],"version-history":[{"count":0,"href":"https:\/\/sandrock.co.za\/carl\/wp-json\/wp\/v2\/posts\/43\/revisions"}],"wp:attachment":[{"href":"https:\/\/sandrock.co.za\/carl\/wp-json\/wp\/v2\/media?parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sandrock.co.za\/carl\/wp-json\/wp\/v2\/categories?post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sandrock.co.za\/carl\/wp-json\/wp\/v2\/tags?post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}