Make your first Polyglot project using MetaCall!
MetaCall is a cross-platform library that facilitates polyglot programming, or the use of multiple programming languages simultaneously, making use of a multilingual interpreter. Moreover, MetaCall uses Function Mesh as a Function as a Service (FaaS).
In this article, we will learn how to install MetaCall and make our first project using Python and Javascript. The project will be a simple Random Password Generator.
Currently, it supports a wide range of languages, including C/C++, JavaScript, Ruby, Python, and C#, but it intends to extend support for more.
Installation
You can install MetaCall in multiple ways. The easiest one is using their installation script.
- install.sh, for Linux and MacOS.
- install.ps1, for Windows.
You can download and run them using one of the following commands:
- CURL:
curl -sL https://raw.githubusercontent.com/metacall/install/master/install.sh | sh
- Wget:
wget -O — https://raw.githubusercontent.com/metacall/install/master/install.sh | sh
- Powershell:
powershell -NoProfile -ExecutionPolicy unrestricted -Command “[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; &([scriptblock]::Create((Invoke-WebRequest -UseBasicParsing ‘https://raw.githubusercontent.com/metacall/install/master/install.ps1')))"
Python Part
The core function will be written in Python. Create an app.py
and insert the following code in it.
import random
def shuffle(string):
tempList = list(string) # Convert the string to a list
random.shuffle(tempList) # Shuffle the list
return ''.join(tempList) # Convert the list to a string again
def getRandomPassword(length: int) -> str:
numUpper = length // 4 # Get the number of uppercase letters
numLower = length // 4 # Get the number of lowercase letters
numSpecial = length // 4 # Get the number of special characters
numNumbers = length - numUpper - numLower - numSpecial # Get the number of numbers
# Get lists of random characters
upperCaseLetters = [chr(random.randint(65,90)) for _ in range(numUpper)] # Generate a list of 2 random Uppercase letters (based on ASCII code)
lowerCaseLetters = [chr(random.randint(97,122)) for _ in range(numLower)] # Generate a list of 2 random Lowercase letters (based on ASCII code)
numbers = [str(random.randint(0,9)) for _ in range(numNumbers)] # Generate a list of 2 random numbers (based on ASCII code)
specialLetters = [chr(random.randint(33,47)) for _ in range(numSpecial)] # Generate a list of 2 random special characters (based on ASCII code)
#Generate password using all the characters, in random order
password = ''.join(upperCaseLetters + lowerCaseLetters + specialLetters + numbers)
password = shuffle(password)
return password
Of course, you can edit the function as much as you like. Making it more simple or more complex.
Don’t forget to hit the Clap and Follow buttons to help me write more articles like this.
Javascript Part
In this part, we will call app.py
in the same way we call a normal javascript file. Create main.js
and embed the following code in it.
#!/usr/bin/env node
// import
const { getRandomPassword } = require('./app.py');
// test that it works
console.log(getRandomPassword(length=10));
// warp it in a function
function getPassword(length=10) {
return getRandomPassword(length);}
// export
module.exports = {
getPassword
};
The Magic Part
That is the part you were waiting for, running the code!
You can simply run metacall main.js
in your terminal, wait for MetaCall to do its magic, and check the output!
Try using Ruby for the shuffle
function and share your work with us!
Conclusion
MetaCall implementation uses a higher-level protocol (QUIC / HTTP3) to reduce RPC overheads. It uses a high-performance multi-core asynchronous I/O model. Compared to the conventional REST API-based architecture, it is more effective.
The MetaCall FaaS can construct an API gateway, import the functions automatically, and use your API at scale with a metacall.json
file.
In upcoming sections, we’ll examine how to build an application from the ground up and deploy it to the MetaCall FaaS.
You can find the Github Repository here. You can even make your own example and it here ✨
Don’t forget to hit the Clap and Follow buttons to help me write more articles like this.