“Konwertuj kod online JavaScript na Python” Kod odpowiedzi

Konwertuj JS na Python Online

>>> import js2py
>>> f = js2py.eval_js('function f(x) {return x + x}')
>>> f(2)
4
>>> f()
nan
>>> f(f)
function f(x) { [python code] }function f(x) { [python code] }
mathiasgodwin

Konwertuj kod online JavaScript na Python

var bigint = require( 'big-integer' )

var lower = 'abcdefghijklmnopqrstuvwxyz';
var upper = lower.toUpperCase();
var numbers = '0123456789'
var ig_alphabet =  upper + lower + numbers + '-_'
var bigint_alphabet = numbers + lower

function toShortcode( longid )
{
	var o = bigint( longid ).toString( 64 )
	return o.replace(/<(\d+)>|(\w)/g, (m,m1,m2) =>
	{
		return ig_alphabet.charAt( ( m1 )
		  ? parseInt( m1 ) 
		  : bigint_alphabet.indexOf( m2 ) )
	});
}

function fromShortcode( shortcode )
{
	var o = shortcode.replace( /\S/g, m =>
	{
		var c = ig_alphabet.indexOf( m )
		var b = bigint_alphabet.charAt( c ) 
		return ( b != "" ) ? b : `<${c}>`
	} )	
	return bigint( o, 64 ).toString( 10 )
}

toShortcode(  '908540701891980503' ) // s.b. 'ybyPRoQWzX'
fromShortcode( 'ybyPRoQWzX' ) // s.b. '908540701891980503'
Foolish Fish

Konwertuj kod online JavaScript na Python

var bigint = require( 'big-integer' )

var lower = 'abcdefghijklmnopqrstuvwxyz';
var upper = lower.toUpperCase();
var numbers = '0123456789'
var ig_alphabet =  upper + lower + numbers + '-_'
var bigint_alphabet = numbers + lower

function toShortcode( longid )
{
	var o = bigint( longid ).toString( 64 )
	return o.replace(/<(\d+)>|(\w)/g, (m,m1,m2) =>
	{
		return ig_alphabet.charAt( ( m1 )
		  ? parseInt( m1 ) 
		  : bigint_alphabet.indexOf( m2 ) )
	});
}

function fromShortcode( shortcode )
{
	var o = shortcode.replace( /\S/g, m =>
	{
		var c = ig_alphabet.indexOf( m )
		var b = bigint_alphabet.charAt( c ) 
		return ( b != "" ) ? b : `<${c}>`
	} )	
	return bigint( o, 64 ).toString( 10 )
}

toShortcode(  '908540701891980503' ) // s.b. 'ybyPRoQWzX'
fromShortcode( 'ybyPRoQWzX' ) // s.b. '908540701891980503'
Foolish Fish

Odpowiedzi podobne do “Konwertuj kod online JavaScript na Python”

Pytania podobne do “Konwertuj kod online JavaScript na Python”

Więcej pokrewnych odpowiedzi na “Konwertuj kod online JavaScript na Python” w JavaScript

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu