Programlib: programs as objects
Programlib is a tool that turns programs in any programming language into convenient Python objects, letting you run any string as a C++/Python/Clojure/etc program from a Python script. This project is aimed to help develop automatic programming and genetic software improvement systems, though many other applications are possible.
Installation
Programlib can be installed with
pip install programlib
However, you have to also make sure that the programming languages you want to use are installed.
By default, programlib uses command line tools that come with the programming languages, i.e. python3 or javac.
Standard usage
Create a program object with
from programlib import Program
program = Program(source_code, language='C++')
This object has
- a
savemethod that will save the source code to a file at the specified path. - a
runmethod that runs the program and returns a list of strings it printed tostdout. You can optionally provide a list of input strings as well. - a
spawnmethod that launches the program in an interactive mode. It returns anAgentobject with andactmethod that takes a sequence of input strings and returns a list of strings printed tostdoutin response. - a
testmethod that takes a list of test cases. A test case is a tuple of 2 lists: the first list is the input strings, the second is the expected output strings. The method returns a full log of all test runs and updatesprogram.avg_scoreandprogram.test_pass_rateattributes.
To use the program to process some input lines, do
output_lines = program.run(input_lines)
To use the program as an agent in a Reinforcement Learning environment, do
env = gym.make('Env-vX')
agent = program.spawn().rl(env.action_space, env.obs_space)
obs, info = env.reset()
terminated = False
truncated = False
while not (terminated or truncated):
action = agent.predict(obs, deterministic=True)
obs, reward, terminated, truncated, info = env.step(action)
See also examples.
Currently supported programming languages out of the box are C++, Python, Java, Clojure, Ruby, Rust, Go, Haskell, Scala, Kotlin, PHP, C#, Swift, D, Julia, Clojure, Elixir and Erlang. See "Advanced usage" below for instructions on how to add other languages.
Advanced usage
Language configuration
When you create a program object with a language name like language='C++', programlib retrieves an appropriate language configuration from it's database.
If you have a different opinion on how to compile or run in this language or want to use a language that is not supported out of the box, you can create your own language configuration object:
from programlib import Program, Language
language = Language(
build_cmd='g++ {name}.cpp -o {name}',
run_cmd='./{name}',
source='{name}.cpp',
artefacts=['{name}']
)
program = Program(source_code, language=language)
source parameter describes the naming convention for the source file (usually {name}.extension). Make sure that this parameter contains a {name} placeholder, so that programlib can keep track of several source files at the same time.
build_cmd and run_cmd respectively instruct programlib which commands to use to compile and run the program in this language.
artefacts is a list of all the files produced by build_cmd command.
It is needed to clean up the artefacts when the program object is destroyed.
Error handling
By default, any errors at build time or run time will lead to RuntimeError being raised, with 2 exceptions:
testfunction that catches exceptions during test cases execution and marks these tests as failed.- Setting
program.run(force=True)orprogram.test(force=True)will makeprogramlibignore all errors.
You can check program.stdout and program.exitstatus to check the output during the last run (or, if in was never run, during build).
Metadata
Release files for programlib 12.0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| programlib-12.0.5.tar.gz | 9.1 kB | Without provenance | Details for programlib-12.0.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| programlib-12.0.5-py3-none-any.whl | Python 3 | none | any | Details for programlib-12.0.5-py3-none-any.whl |
Total release size: 18.5 kB
Release files / programlib-12.0.5.tar.gz
| Download URL | programlib-12.0.5.tar.gz |
|---|---|
| Size | 9.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7bab30e02d6552a545a7abbd0a7be0122de273d820e7a5764ffba967b215cdaa
|
|
BLAKE2b-256 checksum How to use checksums |
993d3ef1ef3fde7dd9eb6fc6347b5e6278af60213efce89fb86e13bd7a041f3f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.11.5 Darwin/23.5.0
|
Release files / programlib-12.0.5-py3-none-any.whl
| Download URL | programlib-12.0.5-py3-none-any.whl |
|---|---|
| Size | 9.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1791c6d851e0c9f191d1ff17130cecbb85b952ffe673cb75e49299c9cb42fc5a
|
|
BLAKE2b-256 checksum How to use checksums |
68745433abfa96df861248c9604018a6aa77a78317c0aacab8532e8cd5b55e47
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.11.5 Darwin/23.5.0
|
Security for programlib 12.0.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.