shield (invalidate) assert in python [duplicate] - python

This question already has answers here:
Disable assertions in Python
(6 answers)
Closed 4 years ago.
I want to invalidate the "assert" in my Python codes when I run it as a release edition. Consider that a lot of "assert" may be used in my files in the developing procedure, but I want to skip the compiling of "assert" to enhance the efficiency. Is there some simple method like a pre_define of "NDEBUG" in c++ ?

Use command line option -O. As described in the docs:
In the current implementation, the built-in variable __debug__ is True
under normal circumstances, False when optimization is requested
(command line option -O). The current code generator emits no code for
an assert statement when optimization is requested at compile time.

Related

How do I execute a string containing Python, without creating a new file or using "os" module? [duplicate]

This question already has answers here:
How do I execute a string containing Python code in Python?
(14 answers)
Closed 5 months ago.
This post was edited and submitted for review 5 months ago and failed to reopen the post:
Original close reason(s) were not resolved
How do I execute a string containing Python, without creating a new file, using excec() or using "os" module?
I've seen the similiar question, but the answeres always kind of created some "new file"(for example created something like this), or went out of the current code, but i want it to stay inside the code and execute it.
Example:
string_base64 = "eCA9ICJoZWxsbyBmZWxsb3ciCgoKcHJpbnQoeCkKeCA9ICJoZWxsbyBmZWxsb3ciCgoKcHJpbnQoeCkKeCA9ICJoZWxsbyBmZWxsb3ciCgoKcHJpbnQoeCkKeCA9ICJoZWxsbyBmZWxsb3ciCgoKcHJpbnQoeCk="
This b64 code has inside it strings and commands (+ For example imports) and it needs to run in the current file, without using exec() or modules
This Thread and the answers, didn't answer the question!
Buildin exec function seems to be doing exactly what you want:
https://docs.python.org/3/library/functions.html#exec

Python REPL in Django [duplicate]

This question already has answers here:
How do I execute a string containing Python code in Python?
(14 answers)
Closed 7 years ago.
I want to execute python code in my browser. Now I enter code in text-field in template, took it to view, where doing following:
source = request.POST.get('source', '').replace('"', r'\"')
result = commands.getoutput('python -c "%s"' % source)
I used python's module command for this, but I think it's don't correct way.
Correct way is using code module, but I don't understand how to get result of execution code and organise REPL. Can you give a little tip how to do it, please?
UPD: I want to start interactive shell in my browser with some variables. User can write some functions to manipulate this vars in browser and see that comes out of it. I understand the danger of this, but now it is not relevant.
You need to take a look at eval and exec also it is potentially very dangerous from security point of view.

Pylint ignore specific names [duplicate]

This question already has answers here:
How to disable pylint 'Undefined variable' error for a specific variable in a file?
(8 answers)
Closed 9 years ago.
I have a problem with pylint, i.e. sometimes it repeats the same message for some variable/class/module etc. and I can't find a workaround for that. What I want is to say pylint
"don't check [message XXX|any message] for variable YYY in [this module|module "ZZZ"]" with some option or rcfile directive.
There's good-names=YYY for this, or for some advanced stuff you can modify the regex via variable-rgx.
According to the docs you enable and disable messages using lines like:
# pylint: disable=W0631
in the python code.
What you are asking for is not supported in the current version of Pylint.
You may want to get in touch with the maintainers and propose them a feature request and an implementation.

How to monitor a directory for changes in Python? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Monitoring files/directories with python
I am making an API server that allows hot-deploying of code. And yes, there are dozens of questions about re-importing modules. I just want to monitor a directory, and when a change is detected, perform an action.
path_to_monitor="c:\\app\\package"
def action():
do_something()
dm=dir_monitor(path_to_monitor,action)
dm.start()
I need a solution that is mature and cross-platform.
You can use a Python thread which uses os.listdir() to list files. If you want to monitor other file properties than the file name, os.stat might help you.

Only allow 1 instance of a python script [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Python: single instance of program
What is the best way to insure that only 1 copy of a python script is running? I am having trouble with python zombies. I tired creating a write lock using open("lock","w"), but python doesn't notify me if the file already has a write lock, it just seems to wait.
Try:
import os
os.open("lock", os.O_CREAT|os.O_EXCL)
The documentation for os.open and its flags.
Your question is similar to this one: What is the best way to open a file for exclusive access in Python?. The answers given there should help you with your issue.
(Use the flag combination portalocker.LOCK_EX!|portalocker.LOCK_NB to return quickly. If the file is locked by another process, your script should get an exception.)

Categories

Resources