E
E
Esterbit2021-10-24 12:52:41
Python
Esterbit, 2021-10-24 12:52:41

How to make automatic code dialing?

I want to make a program that will generate random but syntactically correct python code. However, as I understand it, metaprogramming in Python only allows you to create a class and its content. I need to write all the code myself, and I would have the opportunity to observe this.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vindicar, 2021-10-24
@Esterbit

Use the methods of formal grammars (Google term).
Let's represent the generated text as a sequence of characters (character != letter).
We have terminal characters - these are the lines that will occur in the final text. For example: "if", "for", ":"
We have non-terminal symbols - these are the designations of entities in the text. For example: ASSIGNMENT, BRANCH, OPERATOR, CLASS
We have an axiom - a non-terminal symbol, from which we start generation. For example, PROGRAM.
And, most importantly, we have rules for replacing some characters with others.
For example:
ASSIGNMENT = VARIABLE "=" EXPRESSION
One character can be replaced according to several rules - the choice is random (perhaps with some weight?)
BRANCH = "if" EXPRESSION ":\n" INDITION+ OPERATORS INDITION-
BRANCH = "if" EXPRESSION ":\n" INDITION+ OPERATORS INDITION- "else:\n" INDITION+ OPERATORS INDITION-
To generate strings of characters, use recursion . The first rule continues the chain, the second breaks it.
OPERATORS = OPERATORS "\n" OPERATOR
OPERATORS = OPERATOR
You also need to store some global state - used function and variable names, current indentation, and so on. That is, your replacement of a character may require additional logic, as for indent + and indent -.
The substitution rules are applied as long as there is at least one non-terminal left in the character string.

D
dmshar, 2021-10-24
@dmshar

I know that there are applications like Serenade or Talon that allow voice typing, I would like to know if it is possible to do this in Python and if so, what is required for this.
It seems to me that the answer is quite obvious to anyone who is actually a qualified developer.
1. Understand and write - perhaps on your own, or perhaps by opening / studying at least the indicated packages - how it all should look like.
2. Draw up a TOR for development with - preferably - the most detailed description of the algorithms that will be involved in this.
3. On the way to understand, not just "what is required", but experts in what areas will be required to develop such software, and then - to find and hire such specialists to work.
4. Organize the development process.
If you say that all this is obvious, then I completely agree, but all this is completely obvious, but what a question - this is the answer.
Along the way, a few notes.
1. Almost everything that is done using other programming languages ​​can be implemented in Python. The only question is the convenience and speed of development and the effectiveness of the resulting software products.
2. "For this" - quite obviously - requires a good knowledge of the possibilities of Python.
3. The task itself - to create code "from the voice" - is a task of very dubious benefit. Who needs it? Handicapped without hands? Or someone who wants to dictate (by the way - what exactly? The program itself, previously written on a piece of paper?) And at the same time chew a sandwich, "observing" the process of composing the code? Or a schoolboy who has not mastered the study of a programming language and dreams that the program would do it for him?
4. I am absolutely sure that even if such a program is created, neither the speed of development nor even the quality of the final product will be improved compared to the tools that are available today.
5. To understand what it would be like to work with such a product - try to work with a much simpler application - any voice assistant, evaluate its "conveniences and advantages" compared to the direct one. traditional operation of the system. And for one thing, read which development teams worked to create such a product.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question