{"id":6686,"date":"2017-04-11T02:12:03","date_gmt":"2017-04-11T01:12:03","guid":{"rendered":"http:\/\/emilkirkegaard.dk\/en\/?p=6686"},"modified":"2017-04-11T02:13:47","modified_gmt":"2017-04-11T01:13:47","slug":"do-you-hate-pythons-range-and-want-to-use-rs-seq","status":"publish","type":"post","link":"https:\/\/emilkirkegaard.dk\/en\/2017\/04\/do-you-hate-pythons-range-and-want-to-use-rs-seq\/","title":{"rendered":"Do you hate Python&#8217;s range() and want to use R&#8217;s seq()?"},"content":{"rendered":"<p>I do. So I implemented R&#8217;s function in Python.<\/p>\n<p>I wrote a bunch of tests for this for most uses I could think of and they all work. Except the length param, which I did not implement so far. Did not need it. It can be implemented by simply generating the desired number of natural numbers beginning from 1, and then rescaling this to the desired range.<\/p>\n<p>&nbsp;<\/p>\n<pre>#R style seq()\r\ndef seq(from_ = None, to = None, by = None, length = None, along = None):\r\n    #init\r\n    falling = False\r\n\r\n    #length\r\n    if not length is None:\r\n        #TODO: implement when needed\r\n        raise ValueError(\"`length` is not implemented yet\")\r\n\r\n    #along object\r\n    if not along is None:\r\n        return list(range(1, len(along) + 1))\r\n\r\n    #if all numeric None, set defaults\r\n    if from_ is None and to is None and by is None:\r\n        to = 1\r\n        from_ = 1\r\n\r\n    #1 argument\r\n    if not from_ is None and to is None and by is None:\r\n        #this is actually to\r\n        to = from_\r\n        from_ = 1\r\n\r\n    #determine by and adjustment\r\n    if from_ &gt; to:\r\n        adjustment = -1\r\n\r\n        #set by\r\n        if by is None:\r\n            by = -1\r\n    else:\r\n        adjustment = 1\r\n\r\n        #set by\r\n        if by is None:\r\n            by = 1\r\n\r\n    #impossible by\r\n    if from_ &gt; to and by &gt; 0:\r\n        raise ValueError(\"`by` cannot be positive when falling direction\")\r\n    if from_ &lt; to and by &lt; 0:\r\n        raise ValueError(\"`by` cannot be negative when increasing direction\")\r\n    if by is 0:\r\n        raise ValueError(\"`by` cannot be zero\")\r\n\r\n    #from, to\r\n    if not from_ is None and not to is None:\r\n\r\n        return list(range(from_, to + adjustment, by))\r\n\r\n\r\n    #if you get here, something is wrong!\r\n    raise ValueError(\"Unknown error. Bad input?\")\r\n\r\n\r\n#convenience wrapper\r\ndef seq_along(x):\r\n    return(seq(along = x))<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I do. So I implemented R&#8217;s function in Python. I wrote a bunch of tests for this for most uses I could think of and they all work. Except the length param, which I did not implement so far. Did not need it. It can be implemented by simply generating the desired number of natural [&hellip;]<\/p>\n","protected":false},"author":17,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2383,2382],"tags":[],"class_list":["post-6686","post","type-post","status-publish","format-standard","hentry","category-python","category-r","entry"],"_links":{"self":[{"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/posts\/6686","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/comments?post=6686"}],"version-history":[{"count":3,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/posts\/6686\/revisions"}],"predecessor-version":[{"id":6689,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/posts\/6686\/revisions\/6689"}],"wp:attachment":[{"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/media?parent=6686"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/categories?post=6686"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/tags?post=6686"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}