Package: programlib
Programlib: programs as objectsProgramlib 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.InstallationProgramlib can be installed withpip install programlibHowever, 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.python3orjavac.Standard usageCreate a program object withfromprogramlibimportProgramprogram=Program(source_code,language='C++')This object hasasavemethod that will save the source code to a file at the specified path.arunmethod that runs the program and returns a list of strings it printed tostdout. You can optionally provide a list of input strings as well.aspawnmethod 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.atestmethod 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, dooutput_lines=program.run(input_lines)To use the program as an agent in a Reinforcement Learning environment, doenv=gym.make('Env-vX')agent=program.spawn().rl(env.action_space,env.obs_space)obs,info=env.reset()terminated=Falsetruncated=Falsewhilenot(terminatedortruncated):action=agent.predict(obs,deterministic=True)obs,reward,terminated,truncated,info=env.step(action)See alsoexamples.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 usageLanguage configurationWhen you create a program object with a language name likelanguage='C++',programlibretrieves 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:fromprogramlibimportProgram,Languagelanguage=Language(build_cmd='g++{name}.cpp -o{name}',run_cmd='./{name}',source='{name}.cpp',artefacts=['{name}'])program=Program(source_code,language=language)sourceparameter describes the naming convention for the source file (usually{name}.extension). Make sure that this parameter contains a{name}placeholder, so thatprogramlibcan keep track of several source files at the same time.build_cmdandrun_cmdrespectively instructprogramlibwhich commands to use to compile and run the program in this language.artefactsis a list of all the files produced bybuild_cmdcommand.
It is needed to clean up the artefacts when the program object is destroyed.Error handlingBy default, any errors at build time or run time will lead to an exception being raised, with 2 exceptions:testfunction that catches exceptions during test cases execution and marks these tests as failed.Settingprogram.run(force=True)orprogram.test(force=True)will makeprogramlibignore all errors.You can checkprogram.stdoutandprogram.exitstatusto check the output during the last run (or, if in was never run, during build).