Skip to content

Generates a piece of messy code displaying a given string

License

Notifications You must be signed in to change notification settings

vil02/string_to_code_proj

Repository files navigation

About string_to_code

python_test Quality Gate Status codecov CodeFactor Codacy Badge Open in Gitpod

Basic functionality of string_to_code is to generate a piece of messy code printing a given string. For example

def f_1():
    print('H', end='')
    print('e', end='')


def f_2():
    print('l', end='')
    print('l', end='')


def f_5():
    print(',', end='')
    print(' ', end='')


def f_4():
    print('o', end='')
    f_5()
    print('W', end='')


def f_6():
    print('o', end='')
    print('r', end='')
    print('l', end='')


def f_3():
    f_4()
    f_6()


def f_7():
    print('d', end='')
    print('!', end='')


def f_0():
    f_1()
    f_2()
    f_3()
    f_7()


f_0()

is a python program generated with string_to_code.to_python3 displaying Hello, World!.

Getting started

This package is available at PyPI. It can be installed using the command

pip install string-to-code

In order to generate a code in your_favourite_language just call the function string_to_code.to_your_favourite_language.proc with the string which you want to display. examples show some basic usage.

Information for developers

The project is setup using poetry. In order to create a develompent enviroment, after cloning this repository, run the command:

poetry install --with dev

Have a look at Running tests locally section.

If you just want to see the examples in action, it is enough to clone this repository and run the command:

poetry install

Afterwards you can execute the commands like:

poetry run python3 examples/example_to_cpp.py

Adding a new language

If you think that string_to_code is missing some language, feel free to add it - it is simple. If you want to add support of let's say turbo_snake, there are few things which you need to do:

  • create to_turbo_snake.py in string_to_code. This module should define two function proc and proc_printer_program. The proc function returns a turbo_snake program displaying the input string. The proc_printer_program returns a turbo_snake representation of the given core.PrinterProgram object.

    Have a look at the existing modules.

  • create setup_turbo_snake.py in tests. This file is used for tests. It should contain a function get_test_data() returning a Language object having fields like:

    • tool_names: a list of program names (compilers, interpreters, linters etc.) used for executing and analysing the generated code,
    • string_to_code being the function string_to_code.to_turbo_snake.proc,
    • printer_program_to_code being the function string_to_code.to_turbo_snake.proc_printer_program,
    • run_code: the function, which executes the generated code and returns the standard output,
    • id: the id of the language, most likely turbo_snake,
    • source_code_file_extension: in case of turbo_snake something like ts.
  • create setup_for_turbo_snake.sh or setup_no_sudo_for_turbo_snake.sh in system_setup_scripts. These scripts are used by the workflows to install programs needed to execute corresponding tests - cf. step Install language dependencies in python_test.yml and generate_and_upload_coverage_data.yml, and .gitpod.dockerfile.

  • create and populate the turbo_snake directory in tests/example_data. These files should contain the turbo_snake sourcecode of some basic programs. Please have a look at the already existing examples.

Running tests locally

tests use pytest. If you do not want to run the tests for all of the languages (because it takes too long or you do not want to install all of the required tools) you can skip some of them by using the --skip option. For example, in order to skip bash, C++ and Lisp run the command

poetry run pytest --skip=bash --skip=cpp --skip=lisp

You can also specify the languages which you want to test by using --select option. For example to run tests for C++ and COBOL exclusively execute

poetry run pytest --select=cpp --select=cobol

Without installing any language dependencies from system_setup_scripts you should be able to successfully execute the command

poetry run pytest --select=python3

Existing development environment

This project is setup to be used with gitpod.

Open in Gitpod