
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
An implementation of most of Python's built-in functions in JavaScript.
These functions can be attached to a certain namespace -- including the global namespace (globalThis, global / window).
npm install --save pybi
# or
yarn add pybi
import {install} from 'pybi'
// or
// const {install} = require('pybi')
// The global namespace is polluted by default:
install()
// Now you can do something like
print(list(zip([1,2,3], [4,5,6], [7,8,9,10])))
// Optionally install into a certain namespace:
install(MyApp)
// or just have all functions in one place:
const py3funcs = install({})
// Now you could also use the functions without pollution:
(function({print, list, zip}) {
print(list(zip([1,2,3], [4,5,6], [7,8,9,10])))
})(py3funcs)
// or shorter:
(function({print, list, zip}) {
print(list(zip([1,2,3], [4,5,6], [7,8,9,10])))
})(install({}))
The following literals are (or can) be used:
True, FalseNoner`\n` , f-strings are currently not explicitly supported
because you can just use JavaScript's template strings.b`\a` , br`\a` , rb`\a` __import__()
abs()all()any()ascii()
bin()bool()breakpoint() :triangular_flag_on_post:
debugger.
Thus when used for debugging 1 up-step is necessary.bytearray() :triangular_flag_on_post:
process variable.errors argument.bytes()callable()chr()classmethod()
Caveats for details.compile()
complex()
delattr()dict()dir() :triangular_flag_on_post:
NotImplementedError).divmod()enumerate() :triangular_flag_on_post:
Array instead of an enumerate instance.eval() :triangular_flag_on_post:
eval function in JS. :earth_africa:
But this lib's version wraps the passed expression string in parentheses
so that its value is returned.exec()filter()float()format() :triangular_flag_on_post:
frozenset()getattr()globals()
globalThis (or window/global respectively) please. :wink:hasattr()hash()
hash-sum
if the dependant project has it installed.help()hex()id()input() :triangular_flag_on_post:
process variable.async (default true).
If false, busy waiting is used but system-sleep can be used to relax the busy waiting.int()isinstance()issubclass()iter(object, sentinel=undefined, equality=(x, y) => x === y) :triangular_flag_on_post:
equality that is used for comparing the
sentinel to each iteration's value (i.e. object()) because equality
is not defined as well as in Python.len()list()locals()
map()max() :triangular_flag_on_post:
memoryview()
min()next()object() :triangular_flag_on_post:
oct()open()
open('/path', 'rb', {encoding: 'utf8'}) and
open('/path', 'rb', undefined, 'utf8')mode allows more than in Python.
That way it may be more convenient for people used to the fs.open interface.
E.g. 'ax' is not allowed in Python but it is in fs.ord()pow()print()
kwargs can be passed by passing an object with the following shape as
the last argument, for example:
{__kwargs__, end='-------'}.
__kwargs__ is a named export of pybi.
Note that the end keyword argument is prepended to the default (unavoidable?) line break.
This means it behaves differently than in Python.property()
Proxy didn't work the way I wanted and making this function an alias
for Object.defineProperty is pointless IMHO.
The built-in getter
and setter
are easy to use and save using property as a decorator.
The only added value would be reacting to delete
(which could indeed be accomplished with a Proxy).
This is as far as I got. :wink:range()repr()
evalable representation by their .toString method.
Otherwise <${object.constructor.name} object> is returned.reversed()round()set()setattr()slice()
Slice but is currently not really usable,
because it can't be used on any built-in functionality of JavaScript.
I guess, there could be an Array Proxy that intercepts the array
accessor (see this question)
and uses the Slice class.sorted(iterable, key=undefined, reversed=false)staticmethod()str()sum()super() (keyword)tuple() :triangular_flag_on_post:
Array which is mutable!type()
classmethod decorator works when using type in Python.
The created class has the __name__, __bases__ and __dict__ attributes like in Python.vars() :triangular_flag_on_post:
__dict__.zip()There are some configuration flags for some of the functions.
config is just an object and can be reset using the reset function:
const {config, reset} = require('pybi')
// or
import {config, reset} from 'pybi'
console.log(config) // {
// classmethod_firstArgClass: true,
// hash_useHashSum: true,
// hash_warnNoHashSum: true,
// type_warnArrow: true,
// }
*/
config.classmethod_firstArgClass = false
reset('classmethod_firstArgClass')
console.log(config.classmethod_firstArgClass) // true
// or: reset everything
reset()
classmethodThere are 3 different ways to use this function and 3 according behaviors which can be slightly different.
Since you're probably using babel (for either babel-plugin-proposal-class-properties or babel-plugin-proposal-decorators) the most powerful and most pythonic way is using actual decorators in legacy mode, because
@ syntax,This is the most robust and flexible implementation: It behaves like in Python (I think :wink:).
Internally, the same function as for as wrapper function (below) is used,
thus see that section.
When using classmethod beware that the returned functions cannot be used with multiple/different classes like you could do in Python.
This is due to the fact that in JavaScript we cannot determine the class that contains the according method definition (without additional effort like additional class decorators).
Thus, the 1st class, the method is called on, is cached (in order to support "standalone-calls" (see below)).
In particular, this means that the following is invalid:
const f = classmethod(cls => cls)
class A {
static m1 = f
}
class B {
static m2 = f
}
// still no errors thrown
A.m1()
// all good
const m1 = A.m1
m1()
// still all good
B.m2()
// THROWING UP!
Additionally, decorators in JavaScript can only result in a single descriptor which means, that the classmethod can only be defined either on the class or in the prototype with a single call/assignment (unlike in Python where classmethods can also be accessed called from instances).
There is another slight difference to Python:
classmethod returns functions, so f can be called but in Python classmethod objects are not callable.
FAQs
An implementation of most of Python's built-in functions in JavaScript.
We found that pybi demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.