How to make a calculator discord bot? [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 months ago.
Improve this question
How to make a discord bot so when I input the following it calculates it:
INPUT:
2.2x100 2.8x100-10%
OUTPUT:
output should be the result of the following:
2.8100 - 2.2100 and then minus 10% of that,
(that equation is subject to change)
How would I do this?

This is not an easy task (if you don't want to be unsafe and eval() code).
You need an infix parser that takes into account order of operations (priorities). If there are parenthesis in your operation, it will be even more complex.
You will then need to evaluate the result with a function that executes an abstract syntax tree generated by the parser (AST).
There is probably libraries that make this easier without being unsafe, but make a google search about all the terms I talked about so you can learn more.
EDIT: also take a look at this link. Not beginner stuff.
Infix Calculator Expression Parser
EDIT 2: Just in case you want the easiest solution with eval(), be careful. A skilled attacker can destroy your computer with malicious code. If you just eval() whatever is sent to the bot, they could really destroy / take control of your computer. Be careful.

Related

How to a create a random Python program generator? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Don't ask why, but I want to create a Python program that generates a random Python program that is valid for execution. I found a similar thread Searching for a random python program generator , but it's old and all the links except the demonstration http://www.4geeks.de/cgi-bin/webgen.py are dead. And maybe something has changed in these years and it's easier to do such thing?
One of idea is to use quantum collapse algorithm/technique. This would require to make a kind of database, where I describe what symbols can came after previous one. For example after a can come (,, any letter, etc, but never, for example, ). Still I think this will create very gibberish source and I could easily set wrong conditions.
There are two main ways to achieve this:
Use third parties to generate runnable junk code, for instance using yarpgen to generate a valid C program, and then using things to translate that to python like https://github.com/DAN-329/C_to_Python_translator
Write a python code that writes randomly generated functions with random parameters calling eachother, if you just need a bunch of functions calling eachother doing random math operations that should not be that hard to implement.

How to make a self running file? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a activity in school about how a hacker might hack your pc without knowing, I was assigned to make a self running file, what I'm trying to do is, when a person for eg. Downloads a file, then without he opening it should already run without even the user opening it. Is this possible?
This is not possible, not really actually.
If you want a file to be executed without the user executing it first after download, you will have to do one of such things:
Have an already running program on his machine that looks for your file and then executes it when he finds it in the downloads (MAGIC in the beginning of the file for example or hash validation).
Take advantage of a poorly protected software that executes other files or codes (Or override a standard library) file and make it run your code instead. of course this is not as simple as it sounds and requires you to understand the software that you are attacking pretty good.
Note: Most programs won't just execute some arbitrary code and probably wont just use execv for no reason or without making sure that everything is correct and protected, which makes that solution (Without finding a security breach in the software) pretty difficult.

can someone explain the purpose of the strategy metaclass mentioned here [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Cannot link the example to the title, so here it is: Python strategy design pattern example
First let's look at the basic idea of Strategy Pattern. What it really says is developing some algorithm (function/method/code fragment) which can be switched at run time.
If we are using an OOP supported language (Java, Python), most of the time, we can implement the Strategy pattern with the use of run time polymorphism. In your example also it shows how to achieve this exactly in that way.
No need to get confused with the notion of meta class. It's a python specific terminology, which is used to define the class object of Python. This one is a good answer, if you want to know more about meta classes in python.
And in your example, the notion of meta class doesn't quite related with Strategy pattern implementation. It was just used to mark the Strategy class there as an abstract class. You can develop your program even without that part. And it doesn't do any harm to the idea of Strategy pattern implementation.

How do I properly start creating a programming language in python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
So I've been thinking to make a programming language written in python but I have no idea where to start, and I really don't know the steps creating it.
You would probably start by first planning out your language. That should take a lot of time. Then in Python... you would write a parser that can understand the syntax of your language, and an interpreter that can take the results of the parser and perform the actions of the program.
Your language that is written in Python with Python in-turn being written in C is practically guaranteed to be very slow and will not succeed, but this could be a really fun thing to do as a learning or education experience.
You will likely want to look at Abstract Syntax Trees. This is the underlying structure that python is built on. Take a look here at the documentation: https://docs.python.org/2/library/ast.html
Using ASTs you can at least define the syntax of your language. You will still need to solve the problem of how to interpret it on a platform to get your code to execute.

Is there a limit to how large a function can be? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
New programmer here!
I'm creating my first script on my own, and I have a particular function that is quite large, as in 50 lines.
I understand that theoretically a function can be as large as you need it to be, but etiquette-wise, where is a good place to stay under?
I'm using Python 2.something if that makes a difference.
A good rule of thumb (and it's more a guideline of thumb really) is that you should be able to view the entire function on one screen.
That makes it a lot easier to see the control flow without having to scroll all over the place in whatever editor you're using.
If you can't understand fully what a function does at first glance, it's probably a good idea to refactor chunks of code so that the more detailed steps are placed in their own, well-named, separate function and just called from this one.
However, it's not a hard-and-fast rule, you'll adapt your approach depending on your level of expertise and how complex the code actually is.

Categories