Replit Python Module Documentation¶
replit.database module¶
Interface with the Replit Database.
-
class
replit.database.
AsyncDatabase
(db_url)¶ Bases:
object
Async interface for Repl.it Database.
-
db_url
¶
-
async
delete
(key)¶ Delete a key from the database.
- Parameters
key (str) – The key to delete
- Return type
None
-
async
get
(key)¶ Get the value of an item from the database.
- Parameters
key (str) – The key to retreive
- Raises
KeyError – Key is not set
- Returns
The value of the key
- Return type
str
-
async
items
()¶ Convert the database to a dict and return the dict’s items method.
- Returns
The items
- Return type
Tuple[Tuple[str]]
-
async
keys
()¶ Get all keys in the database.
- Returns
The keys in the database.
- Return type
Tuple[str]
-
async
list
(prefix)¶ List keys in the database which start with prefix.
- Parameters
prefix (str) – The prefix keys must start with, blank not not check.
- Returns
The keys found.
- Return type
Tuple[str]
-
sess
¶
-
async
set
(key, value)¶ Set a key in the database to value.
- Parameters
key (str) – The key to set
value (str) – The value to set it to
- Return type
None
-
async
to_dict
(prefix='')¶ Dump all data in the database into a dictionary.
- Parameters
prefix (str) – The prefix the keys must start with, blank means anything. Defaults to “”.
- Returns
All keys in the database.
- Return type
Dict[str, str]
-
async
values
()¶ Get every value in the database.
- Returns
The values in the database.
- Return type
Tuple[str]
-
-
class
replit.database.
AsyncJSONKey
(db, key, dtype, get_default=None, discard_bad_data=False, do_raise=False)¶ Bases:
object
Represents an key in the async database that holds a JSON value. db.jsonkey() will initialize an instance for you, you don’t have to do it manually.
-
db
¶
-
discard_bad_data
¶
-
do_raise
¶
-
dtype
¶
-
async
get
()¶ Get the value of the key. If an invalid JSON value is read or the type does not match, it will show a
prompt asking the user what to do unless discard_bad_data is set.
- Raises
KeyError – If do_raise is true and the key does not exist.
json.JSONDecodeError – If do_raise is true and invalid JSON data is read from the key.
- Returns
The value read from the database
- Return type
JSON_TYPE
-
get_default
¶
-
key
¶
-
async
set
(data)¶ Set the value of the jsonkey. :type data:
Union
[str
,int
,float
,bool
,dict
,list
,None
] :param data: The value to set it to. :type data: JSON_TYPE- Raises
TypeError – The type of the value set does not match the datatype.
- Return type
None
-
-
class
replit.database.
Database
(db_url)¶ Bases:
collections.abc.MutableMapping
Dictionary-like interface for Repl.it Database.
This interface will coerce all values everything to and from JSON. If you don’t want this, use AsyncDatabase instead.
-
db_url
¶
-
prefix
(prefix)¶ Return all of the keys in the database that begin with the prefix.
- Parameters
prefix (str) – The prefix the keys must start with, blank means anything.
- Returns
The keys found.
- Return type
Tuple[str]
-
sess
¶
-
-
class
replit.database.
JSONKey
(db, key, dtype, get_default=None, discard_bad_data=False, do_raise=False)¶ Bases:
replit.database.jsonkey.AsyncJSONKey
Represents a key in the database that holds a JSON value.
db.jsonkey() will initialize an instance for you, you don’t have to do it manually.
-
append
(item)¶ Append to the JSONKey’s value if it is a list.
- Parameters
item (JSON_TYPE) – The item to append.
- Return type
None
-
db
¶
-
discard_bad_data
¶
-
do_raise
¶
-
dtype
¶
-
get
()¶ Get the value of the key.
- If an invalid JSON value is read or the type does not match, it will show a
prompt asking the user what to do unless discard_bad_data is set.
- Returns
The value read from the database
- Return type
JSON_TYPE
-
get_default
¶
-
key
¶
-
keys
(*keys)¶ Reads multiple keys from the key’s value and allows setting.
- Parameters
*keys (str) – The keys to read from the data.
- Returns
The value accessed from self.get()[k1][k2][kn]
- Return type
Any
-
read
(key, default=None)¶ Shorthand for self.get().get(name, default) if datatype is dict.
- Parameters
key (str) – The name to get.
default (Any) – The default if the key doesn’t exist. Defaults to None.
- Returns
The value read or the default.
- Return type
Any
-
set
(data)¶ Set the value of the jsonkey.
- Parameters
data (JSON_TYPE) – The value to set it to.
- Raises
TypeError – The type of the value set does not match the datatype.
- Return type
None
-
replit.audio module¶
A library to play audio in a repl.
-
class
replit.audio.
Audio
¶ Bases:
object
The basic audio manager.
Notes
This is not intended to be called directly, instead use
audio
.Using this in addition to audio can cause major issues.
-
get_paused
()¶ Get a list of paused sources.
- Returns
A list of sources that are paused.
- Return type
List[Source]
-
get_playing
()¶ Get a list of playing sources.
- Returns
A list of sources that aren’t paused.
- Return type
List[Source]
-
get_source
(source_id)¶ Get a source by it’s ID.
- Parameters
source_id (int) – The ID for the source that should be found.
- Raises
NoSuchSourceException – If the source isnt found or there isn’t any sources known to the audio manager.
- Returns
The source with the ID provided.
- Return type
-
get_sources
()¶ Gets all sources.
- Returns
Every source known to the audio manager, paused or playing.
- Return type
List[Source]
-
play_file
(file_path, volume=1, does_loop=False, loop_count=0, name=None)¶ Sends a request to play a file, assuming the file is valid.
- Parameters
file_path (str) – The path to the file that should be played. Can be absolute or relative.
volume (float) – The volume the source should be played at. (1 being 100%)
does_loop (bool) – Wether the source should repeat itself or not. Note, if you set this you should also set loop_count.
loop_count (int) – How many times the source should repeat itself. Set to 0 to have the source play only once, or set to a negative value for the source to repeat forever.
name (str) – The name of the file. Default value is a unique name for the source.
- Returns
The source created with the provided data.
- Return type
- Raises
FileNotFoundError – If the file is not found.
InvalidFileType – If the file type is not valid.
-
play_tone
(duration, pitch, wave_type, does_loop=False, loop_count=0, volume=1, name=None)¶ Play a tone from a frequency and wave type.
- Parameters
duration (float) – How long the tone should be played (in seconds).
pitch (int) – The frequency the tone should be played at.
wave_type (WaveType) – The wave shape used to generate the tone.
does_loop (bool) – Wether the source should repeat itself or not. Note, if you set this you should also set loop_count.
loop_count (int) – How many times the source should repeat itself. Set to 0 to have the source play only once, or set to a negative value for the source to repeat forever.
volume (float) – The volume the tone should be played at (1 being 100%).
name (str) – The name of the file. Default value is a unique name for the source.
- Returns
The source for the tone.
- Return type
-
read_status
()¶ Get the raw data for what’s playing.
This is an api call, and shouldn’t be needed for general usage.
- Returns
The contents of /tmp/audioStatus.json
- Return type
AudioStatus
-
-
exception
replit.audio.
InvalidFileType
¶ Bases:
Exception
Exception for when a requested file’s type isnt valid.
-
exception
replit.audio.
NoSuchSourceException
¶ Bases:
Exception
Exception used when a source doesn’t exist.
-
class
replit.audio.
Source
(payload, loops)¶ Bases:
object
A Source is used to get audio that is sent to the user.
-
property
does_loop
¶ Whether the source repeats itself or not.
- Return type
bool
-
property
duration
¶ The duration of the source.
- Return type
timedelta
-
property
end_time
¶ Property wrapper for
get_end_time()
- Return type
Optional
[datetime
]
-
get_end_time
()¶ The estimated time when the source will be done playing.
- Returns
- The estimated time when the source will be done playing
or None if it is already finished.
- Return type
Optional[datetime]
-
get_loops_remaining
()¶ The remaining amount of times the file will restart.
- Returns
- The number of loops remaining or None if the source can’t be
found, either because it has finished playing or an error occured.
- Return type
Optional[int]
-
get_paused
()¶ Whether the source is paused.
- Return type
bool
-
get_remaining
()¶ The estimated time remaining in the source’s current loop.
- Return type
timedelta
-
get_start_time
()¶ When the source started plaing.
- Return type
datetime
-
get_volume
()¶ The volume the source is set to.
- Return type
float
-
property
id
¶ The ID of the source.
- Return type
int
-
property
loops_remaining
¶ Property wrapper for
get_loops_remaining()
- Return type
Optional
[int
]
-
property
name
¶ The name of the source.
- Return type
str
-
property
path
¶ The path to the source, if available.
- Return type
str
-
property
paused
¶ Property wrapper for replit.Source.get_paused and replit.Source.set_paused
- Return type
bool
-
property
remaining
¶ Property wrapper for
get_remaining()
- Return type
timedelta
-
set_loop
(loop_count)¶ Set the remaining amount of loops for the source.
- Parameters
loop_count (int) – How many times the source should repeat itself. Set to a negative value for infinite.
- Return type
None
-
set_paused
(paused)¶ Change if the source is paused.
- Parameters
paused (bool) – Whether the source should be paused.
- Return type
None
-
set_volume
(volume)¶ Set the volume.
- Parameters
volume (float) – The volume the source should be set to.
- Return type
None
-
property
start_time
¶ Property wrapper for
get_start_time()
- Return type
datetime
-
toggle_playing
()¶ Play/pause the source.
- Return type
None
-
property
volume
¶ Property wrapper for replit.Source.get_volume and replit.Source.set_volume
- Return type
float
-
property