Skip to content

Instantly share code, notes, and snippets.

@kasir-barati
Last active July 1, 2022 04:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kasir-barati/b3c42c0ac94fa91b9df631dd5abd3979 to your computer and use it in GitHub Desktop.
Save kasir-barati/b3c42c0ac94fa91b9df631dd5abd3979 to your computer and use it in GitHub Desktop.
A simple gist full of syntax translation between two God, JS and Python

comprehensions

const arr = [1,2,3];
arr.map(x => 2*x);
arr.filter(x => x % 2 == 0);
arr = [1,2,3]
[2*x for x in arr]
[x for x in arr if x % 2 == 0]

for .every and .some you can use the builtins all and any with a generator comprehension so that you get early return without evaluating every item:

const arr = [1,2,3];
arr.every(x => x < 3);
arr.some(x => x % 2 === 0);
arr = [1,2,3];
all(x < 3 for x in arr)
any(x % 2 == 0 for x in arr)

Literal string

var1 = 10
`${var1}`
# Formatted string
var1 = 10
f"{var1}"

check null

a = undefined
function bb() { console.log("No way to be called") }
a && bb()
a
def bb():
    print("No way to be called")
a and bb()

Get working directory

These two returns where the python/nodejs got executed. For example if I run my script like this python3 shared/read_env.py it will point to the /home/kasir/anime_die_heart/anime_die_heart in my case.

process.cwd()
import os
os.getcwd()

join multiple path

const path = require('node:path');
const osAgnosticResult = path.join('path', 'to', 'file.txt');
import os
os_agnostic_result = os.path.join('path', 'to', 'file.txt')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment