{"id":481,"date":"2012-06-20T00:49:26","date_gmt":"2012-06-19T23:49:26","guid":{"rendered":"http:\/\/nemisj.com\/?p=481"},"modified":"2020-05-15T18:48:33","modified_gmt":"2020-05-15T17:48:33","slug":"python-api-javascript","status":"publish","type":"post","link":"https:\/\/nemisj.com\/python-api-javascript\/","title":{"rendered":"Javascript to Python API reference guide"},"content":{"rendered":"<div id=\"bsf_rt_marker\"><\/div><blockquote><p>Do you also keep searching for JS terms to find out what Python API to use? I use this article as a reference guide of the javascript to python API.<\/p><\/blockquote>\n<h2>Prologue<\/h2>\n<p>After constantly working with javascript for more than seven years I&#8217;ve decided to explore another programming language. Python became my language of choice.<\/p>\n<p>Since I&#8217;m not doing python coding fulltime, I keep diving into the same docs of python again and again, in order to recall how to do simple and basic stuff. For example: looping through the different data types or searching in a string for a pattern.<\/p>\n<p>My long-running relationship with JavaScript only makes the process of learning even more difficult. Every time I&#8217;m trying to do something in python i&#8217;m searching for the equivalent of javascript API in python. However it can take a long time before the answer is found. For example, how would you do <code>charCodeAt<\/code> in python? I have to say it took me a while to find an answer to this question, but then, time passes by and I forget it again.<\/p>\n<p>For this reason I wrote this short reference guide. Though it only contains stuff which differs from the javascript perspective. This means I haven&#8217;t done a mapping for methods which are the same in both languages.<\/p>\n<p>One piece of advice. In the python REPL you can execute the <code>help<\/code> function and pass any object to it. The <code>help<\/code> function will show you documentation regarding the passed object e.g.,<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">    help([])\n<\/pre>\n<p>will return documentation about arrays.<\/p>\n<h3>Global stuff<\/h3>\n<h4>escape<\/h4>\n<p><em>Syntax: escape(x)<\/em><\/p>\n<pre><code>import urllib; \nurllib.quote(x)\n<\/code><\/pre>\n<h4>unescape<\/h4>\n<p><em>Syntax: unescape(x)<\/em><\/p>\n<pre><code>import urllib\nurllib.unquote(x)\n<\/code><\/pre>\n<h4>parseFloat<\/h4>\n<p><em>Syntax: parseFloat(x)<\/em><\/p>\n<pre><code>float(x)\n<\/code><\/pre>\n<h4>parseInt<\/h4>\n<p><em>Syntax: parseInt(x)<\/em><\/p>\n<pre><code>int(x)\n<\/code><\/pre>\n<p>Note: Be carefull with this one, since it might other thing than you expect. Often parseInt is used to parse any string ( with int or float in it) to an integer. In case you try to parse a float or non-valid number it will throw an error. This means that, stuff like:<\/p>\n<pre><code>int(\"123.456\")\n<\/code><\/pre>\n<p>will break in python, but is still valid in javascript.<\/p>\n<h3>String<\/h3>\n<ul>\n<li><a href=\"http:\/\/docs.python.org\/library\/string.html\">http:\/\/docs.python.org\/library\/string.html<\/a><\/li>\n<li><a href=\"http:\/\/www.skymind.com\/~ocrow\/python_string\/\">http:\/\/www.skymind.com\/~ocrow\/python_string<\/a><\/li>\n<\/ul>\n<h4>charAt<\/h4>\n<p><em>Syntax: &#8220;string&#8221;.charAt(index)<\/em><\/p>\n<pre><code>\"string\"[index]\n<\/code><\/pre>\n<h4>charCadeAt<\/h4>\n<p><em>Syntax: &#8220;string&#8221;.charCodeAt(index)<\/em><\/p>\n<pre><code>ord(\"string\"[index])\n<\/code><\/pre>\n<h4>indexOf<\/h4>\n<p><em>Syntax: &#8220;string&#8221;.indexOf(&#8220;searchValue&#8221;,[fromIndex])<\/em><\/p>\n<pre><code>\"string\".find(\"searchValue\", [fromIndex])\n<\/code><\/pre>\n<h4>fromCharCode<\/h4>\n<p>Syntax: String.fromCharCode(charCode)<\/p>\n<pre><code>chr(charCode)\n<\/code><\/pre>\n<h4>lastIndexOf<\/h4>\n<p><em>Syntax: &#8220;string&#8221;.lastIndexOf(&#8220;searchValue&#8221;,[fromIndex])<\/em><\/p>\n<pre><code>\"string\".rfind(\"searchValue\", [fromIndex])\n<\/code><\/pre>\n<h4>match<\/h4>\n<p>Placed inside RegExp object<\/p>\n<h4>replace<\/h4>\n<p>Placed inside RegExp object<\/p>\n<h4>slice<\/h4>\n<p><em>Syntax: &#8220;string&#8221;.slice(startIndex)<\/em><\/p>\n<pre><code>\"string\"[startIndex:]\n<\/code><\/pre>\n<p><em>Syntax: &#8220;string&#8221;.slice(startIndex, endIndex)<\/em><\/p>\n<pre><code>\"string\"[startIndex:endIndex]\n<\/code><\/pre>\n<h4>split<\/h4>\n<p>Syntax: &#8220;string&#8221;.split(delimiter)<\/p>\n<pre><code>\"str\".split(delimiter)\n<\/code><\/pre>\n<h4>substr<\/h4>\n<p><em>Syntax: &#8220;string&#8221;.substr(start)<\/em><\/p>\n<pre><code>\"string\"[start:]\n<\/code><\/pre>\n<p><em>Syntax: &#8220;string&#8221;.substr(start, [length])<\/em><\/p>\n<pre><code>\"string\"[start:start+length]\n<\/code><\/pre>\n<p>Note: In javascript substring expects length and in Python an endIndex. The comparable function in javascript is <code>slice<\/code><\/p>\n<h4>substring<\/h4>\n<p><em>Syntax: &#8220;string&#8221;.substring(start, end)<\/em><\/p>\n<pre><code>\"string\"[start:end]\n<\/code><\/pre>\n<h4>toLowerCase<\/h4>\n<p><em>Syntax: &#8220;string&#8221;.toLowerCase()<\/em><\/p>\n<pre><code>\"string\".lower()\n<\/code><\/pre>\n<h4>toUpperCase<\/h4>\n<p><em>Syntax: &#8220;string&#8221;.toUpperCase()<\/em><\/p>\n<pre><code>\"string\".upper()\n<\/code><\/pre>\n<h3>RegExp<\/h3>\n<ul>\n<li><a href=\"http:\/\/docs.python.org\/release\/2.5.2\/lib\/match-objects.html\">http:\/\/docs.python.org\/release\/2.5.2\/lib\/match-objects.html<\/a><\/li>\n<li><a href=\"http:\/\/docs.python.org\/dev\/howto\/regex.html\">http:\/\/docs.python.org\/dev\/howto\/regex.html<\/a><\/li>\n<li><a href=\"http:\/\/docs.python.org\/library\/re.html\">http:\/\/docs.python.org\/library\/re.html<\/a><\/li>\n<li><a href=\"http:\/\/www.tutorialspoint.com\/python\/python_reg_expressions.htm\">http:\/\/www.tutorialspoint.com\/python\/python_reg_expressions.htm<\/a><\/li>\n<\/ul>\n<p>RegExp support in python is pretty <em>BIG<\/em> and is much more extended than in javascript ( IMHO: mostly like anything in Python ). That&#8217;s why I have dedicated a <a href=\"http:\/\/nemisj.com\/python-regexp-javascript\/\">separate article<\/a> to this subject. So if you are going to use regexps intensively I recommend you read that article as well.<\/p>\n<p>Also <code>match<\/code> and <code>replace<\/code> are part of the String object, I specify it here in order to emphasize that searching and replacing in Python is not the same as it is in JavaScript.<\/p>\n<h4>test<\/h4>\n<p><em>Syntax: var r = \/regexp\/.test(&#8220;string&#8221;)<\/em><\/p>\n<pre><code>import re\nr = (re.search(r\"regexp\", \"someString\") != None)\n<\/code><\/pre>\n<p>Note: search doesn&#8217;t return boolean value, but None if no match is found.<\/p>\n<h4>replace<\/h4>\n<p><em>Syntax: &#8220;string&#8221;.replace(\/regexp\/, &#8220;newSubStr&#8221;)<\/em><\/p>\n<pre><code>import re\nre.sub(r\"regepx\", \"originalString\", \"newSubStr\")\n<\/code><\/pre>\n<p>*Syntax: &#8220;string&#8221;.replace(\/regexp\/, function(str, p1&#8230;p2, offset, s){})<\/p>\n<pre><code>import re\n<\/code><\/pre>\n<h4>match<\/h4>\n<p><em>Syntax: &#8220;string&#8221;.match(\/regexp\/g)<\/em><\/p>\n<pre><code>import re\nre.findall(r\"regexp\", \"import\u201d)\n<\/code><\/pre>\n<p>Note: Please note that only <code>match<\/code> with the GLOBAL flag is presented here. The reason for that is explained in <a href=\"http:\/\/nemisj.com\/python-regexp-javascript\/\">&#8220;Introduction into Python Regexp&#8221;<\/a> as a follow up article.<\/p>\n<h3>Date<\/h3>\n<ul>\n<li><a href=\"http:\/\/docs.python.org\/release\/2.5.2\/lib\/datetime-date.html\">http:\/\/docs.python.org\/release\/2.5.2\/lib\/datetime-date.html<\/a><\/li>\n<\/ul>\n<p>One of the difference between Python and JavaScript is that Python works with seconds while JavaScript with milliseconds.<\/p>\n<h4>Getting Date object of now<\/h4>\n<p><em>Syntax: var now = new Date()<\/em><\/p>\n<pre><code>import datetime\nnow = datetime.datetime.now()\n<\/code><\/pre>\n<h4>Creating date object based on year, month, etc<\/h4>\n<p><em>Syntax: var d = new Date(year, month, date)<\/em><\/p>\n<pre><code>import datetime\ndatetime.datetime(year, month+1, date)\n<\/code><\/pre>\n<p>Note: The month in python is from 1&lt;=12 and not from 0 like in javascript<\/p>\n<h4>Creating date object based on epoch<\/h4>\n<p><em>Syntax: var d = new Date(epoch)<\/em><\/p>\n<pre><code>import datetime\ndatetime.fromtimestamp(epoch)\n<\/code><\/pre>\n<h4>getDate<\/h4>\n<p><em>Syntax: dateObject.getDate( )<\/em><\/p>\n<pre><code>dateObject.day\n<\/code><\/pre>\n<h4>getDay<\/h4>\n<p><em>Syntax: d.getDay()<\/em><\/p>\n<pre><code>d.weekday()\n<\/code><\/pre>\n<h4>getFullYear<\/h4>\n<p><em>Syntax: d.getFullYear()<\/em><\/p>\n<pre><code>d.year\n<\/code><\/pre>\n<h4>getMonth<\/h4>\n<p><em>Syntax: d.getMonth()<\/em><\/p>\n<pre><code>(d.month - 1)\n<\/code><\/pre>\n<p>Note: That month in python is from 1&lt;=12 and not from 0 like in JavaScript<\/p>\n<h4>getHours<\/h4>\n<p><em>Syntax: d.getHours()<\/em><\/p>\n<pre><code>d.hour\n<\/code><\/pre>\n<h4>getMilliseconds<\/h4>\n<p><em>Syntax: d.getMilliseconds()<\/em><\/p>\n<pre><code>(d.microsecond * 0.001)\n<\/code><\/pre>\n<h4>getMinutes<\/h4>\n<p><em>Syntax: d.getMinutes()<\/em><\/p>\n<pre><code>d.minute\n<\/code><\/pre>\n<h4>getSeconds<\/h4>\n<p><em>Syntax: d.getSeconds()<\/em><\/p>\n<pre><code>d.second\n<\/code><\/pre>\n<h4>getTime<\/h4>\n<p><em>Syntax: d.getTime()<\/em><\/p>\n<pre><code>import time\ntime.mktime(d.timetuple())\n<\/code><\/pre>\n<h3>Math<\/h3>\n<ul>\n<li><a href=\"http:\/\/docs.python.org\/library\/math.html\">http:\/\/docs.python.org\/library\/math.html<\/a><\/li>\n<\/ul>\n<h4>random<\/h4>\n<p><em>Syntax: Math.random()<\/em><\/p>\n<pre><code>import random\nrandom.random()\n<\/code><\/pre>\n<h4>round<\/h4>\n<p><em>Syntax: Math.round(number)<\/em><\/p>\n<pre><code>int(round(number))\n<\/code><\/pre>\n<p>Note: round in Python always returns a floating number, while in JavaScript an integer and keep that in mind.<\/p>\n<h4>abs, ceil, floor<\/h4>\n<pre><code># All the other methods of math are mostly equiualent to Math in javascript  \nimport math \nmath.abs()\nmath.fabs(x)\nmath.ceil(x)\nmath.floor(x)\n<\/code><\/pre>\n<h2>Array<\/h2>\n<ul>\n<li><a href=\"http:\/\/effbot.org\/zone\/python-list.htm\">http:\/\/effbot.org\/zone\/python-list.htm<\/a><\/li>\n<\/ul>\n<h4>push<\/h4>\n<p><em>Syntax: arr.push(item)<\/em><\/p>\n<pre><code>arr.append(item)\n<\/code><\/pre>\n<p>Note: Python has a different return value, so if you need the length of the array wrap it in a <code>len<\/code> function<\/p>\n<pre><code>len(arr.append(item))\n<\/code><\/pre>\n<h4>shift<\/h4>\n<p><em>Syntax: arr.shift(item)<\/em><\/p>\n<pre><code>arr.insert(0, item)\n<\/code><\/pre>\n<h4>unshift<\/h4>\n<p><em>Syntax: arr.unshift()<\/em><\/p>\n<pre><code>arr.pop(0) # different return\n<\/code><\/pre>\n<h4>length<\/h4>\n<p>Syntax: arr.length<\/p>\n<pre><code>len(one)\n<\/code><\/pre>\n<h4>slice<\/h4>\n<p><em>Syntax: arr.slice(begin)<\/em><\/p>\n<pre><code>arr[begin:]\n<\/code><\/pre>\n<p>*Syntax: arr.slice(begin, end)<\/p>\n<pre><code>arr[begin:end]\n<\/code><\/pre>\n<h4>concat<\/h4>\n<p><em>Syntax: arr.concat(secondArray)<\/em><\/p>\n<pre><code>arr + secondArray\n<\/code><\/pre>\n<p>Note: concat is making a shallow copy of an array<\/p>\n<p>When doing a new copy of an array in javascript you do this:<\/p>\n<pre><code>[].concat(x)\n<\/code><\/pre>\n<p>But in python you can use this syntax:<\/p>\n<pre><code>x[:]\n<\/code><\/pre>\n<h4>join<\/h4>\n<p><em>Syntax: arr.join(delimiter)<\/em><\/p>\n<pre><code>delimiter.join(arr)\n<\/code><\/pre>\n<p>Note: As you can see, the method for joining an array is sitting inside the string class and not in the array class as in javascript<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Do you also keep searching for JS terms to find out what Python API to use? I use this article as a reference guide of&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/nemisj.com\/python-api-javascript\/\">Continue reading<span class=\"screen-reader-text\">Javascript to Python API reference guide<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-481","post","type-post","status-publish","format-standard","hentry","category-javascript","entry"],"_links":{"self":[{"href":"https:\/\/nemisj.com\/wp-json\/wp\/v2\/posts\/481","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nemisj.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nemisj.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nemisj.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nemisj.com\/wp-json\/wp\/v2\/comments?post=481"}],"version-history":[{"count":79,"href":"https:\/\/nemisj.com\/wp-json\/wp\/v2\/posts\/481\/revisions"}],"predecessor-version":[{"id":1835,"href":"https:\/\/nemisj.com\/wp-json\/wp\/v2\/posts\/481\/revisions\/1835"}],"wp:attachment":[{"href":"https:\/\/nemisj.com\/wp-json\/wp\/v2\/media?parent=481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nemisj.com\/wp-json\/wp\/v2\/categories?post=481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nemisj.com\/wp-json\/wp\/v2\/tags?post=481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}