https://github.com/dainfloop/jspy
https://github.com/dainfloop/jspy
Last synced: 7 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/dainfloop/jspy
- Owner: DaInfLoop
- Created: 2023-03-04T18:36:48.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-03-04T18:45:09.000Z (over 3 years ago)
- Last Synced: 2024-12-26T13:13:52.579Z (over 1 year ago)
- Language: Python
- Size: 11.9 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# jspy
The definition of: I got bored so I decided to make Javascript in Python.
## Usage:
1) First install the library
2) Import it within your code:
```py
from lib import *
# All set!
```
Note that jspy is not complete yet, so contributions on the [GitHub repo](https://github.com/DaInfLoop/jspy) are welcome!
## Documentation
Core
### class Object
Class you can use to create an object. This is not the recommended way to create an object, and you should use `createObject()` instead.
### lib#createObject
Create an object from a dictionary.
Usage:
```py
myObj = createObject({"hello": "world"})
```
### lib#require
Import a module/JSON file.
Usage:
```py
# Imagine I have a file in the same directory called "coolFile.py"
coolFile = require('coolFile')
```
### class Console
The console class. This does nothing special being called manually, so just use the `console` variable.
### true/false/null
Self-explanatory. `true == True`, `false == False`, `null == None`.
JSON
Already pre-imported, but you can run require('jspy:json') to re-import.
### JSON#parse -> lib.core.Object
Parse a JSON object and make it into an Object.
Usage:
```py
parsed = JSON.parse('{"hello":"world"}')
console.log(parsed) # {"hello": "world"}
```
### JSON#stringify -> str
Stringify a dict. You can specify how many spaces you want for the indent.
Usage:
```py
myDict = {"hello": "world"}
console.log(JSON.stringify(myDict, null, 2))
"""
{
"hello": "world"
}
"""
```
Events
Import via require('jspy:events').
### class EventEmitter
Create an EventEmitter.
Usage:
```py
emitter = EventEmitter()
```
### EventEmitter#on -> null
Register an event.
Usage:
```py
emitter.on('event', handler)
```
### EventEmitter#once -> null
Register an event that will delete itself when emitted.
Usage:
```py
emitter.once('event', handler)
# After `handler` is run, the event will no longer run `handler`.
```
### @EventEmitter#event -> Callable
A decorator that you can add to a function to register it as an event.
Usage:
```py
# emitter.on
@emitter.event('event')
def onHandler(args):
console.log(args)
# emitter.once
@emitter.event('event', once=True)
def onceHandler(args):
console.log(args)
```
### EventEmitter#emit -> bool
Emit an event. Runs all event handlers. Returns `True` if an event handler was found, or `False` if one wasn't found.
Usage:
```py
@emitter.event('event')
def onHandler(args):
console.log(args)
emitter.emit('event', "hello!") # prints "hello!"
```