I trying a lot of methods, but any method work strange. Some return error, some return value, but not what I need to be returned ("0" rather than string).
So, I try to use Everything in python code. I make example tool, but when I start using API I stacked. Simply I can't call function or call it wrong.
My code you may see on Github (with comments).
To run it need dll, that you can also find on Github, Everything itself and python 3.5.
Run it in cmd by using command: fs [path] (i.e. C:\, but not used right now as I can't call function)
Code started at line 73 and ended at 126.
Rather than struggling with calling "Everything" I would suggest that you take a look at the python built-in library functions os.walk, fnmatch.fnmatch and glob.glob - they work very well to do what everything does with no external dependencies and a simple pythonic user calling interface.
Additionally, if you write your code using them, you code will work on every platform that supports python.
Related
I'm following this tutorial on creating a vim plugin from python. I was trying to use the knowledge I got to access the first line of the current buffer, so I wrote:
let s:plugin_root_dir = fnamemodify(resolve(expand('<sfile>:p')), ':h')
python3 << EOF
import sys
from os.path import normpath, join
import vim
plugin_root_dir = vim.eval('s:plugin_root_dir')
python_root_dir = normpath(join(plugin_root_dir, '..', 'python'))
sys.path.insert(0, python_root_dir)
import sample
EOF
and in my python file I wrote:
import vim
vim.command("sign define gdlinterror text=✕")
vim.command("sign define gdlintwarning text=")
print(vim.current.buffer[0])
However I get the following error:
Error detected while processing function provider#python3#Call:
line 18:
Error invoking 'python_execute' on channel 4 (python3-script-host):
Traceback (most recent call last):
File "<string>", line 7, in <module>
File "/home/chrismg/Projects/Python/sampleplugin/python/sample.py", line 5, in <module>
print(vim.current.buffer[0])
File "/home/chrismg/.local/lib/python3.6/site-packages/pynvim/api/buffer.py", line 45, in __ge
titem__
return self.request('nvim_buf_get_lines', i, i + 1, True)[0]
IndexError: list index out of range
So when I tried the same commands within vim using :python3 it surprised me that it worked very well, but not in the python script. So when I checked the len of the vim.current.buffer I got 0. I think this is the amount of lines in the file, which is false in my case, so I started wondering, is this python script being executed before the buffer is being loaded? This would explain why there is an index out of bounds error. However, I don't know how to run the vim plugin after the buffer is finished loading.
Any help in finding out what to do is greatly appreciated. Thanks!
The thing is, even when you're writing plug-in code in Python, you'll still be handling Vimscript events, accessing Vimscript variables, using Vimscript for mapping keys and so on. Writing plug-ins in Python essentially means writing them in Python and Vimscript.
You should use your main Python module (sample.py) to define functions rather than execute code at the top-level. Then you can use mappings or auto-commands to trigger those functions on specific events.
For example, to trigger compilation on pressing F5:
nnoremap <silent> <F5> :python3 sample.compile()<CR>
Or to add an auto-gemerated license comment to the top with <Leader>L
nnoremap <silent> <Leader>L :0put =py3eval('sample.license_text()')<CR>
Or to check syntax of Ruby files when they're loaded:
autocmd FileType ruby python3 sample.check_syntax('ruby')
As you can see, while you're being able to use Python code here, you're mostly doing so by calling the :python3 command or using the py3eval() function, but still using quite a bit of Vimscript in the process. So, in a way, you end up having to understand really well how Vimscript works to be able to write Vim plug-ins effectively.
The same is also quite true for using the vim module from Python. Since you're mainly using it to access Vimscript variables and the way you have access to Vim objects closely matches what Vim functions offer you, knowing Vimscript well will help you there.
Being able to write Vim plug-ins in Python can be quite handy when you're using it to accomplish something that would be impossible or very hard to do in pure Vimscript, such as accessing web resources (for example, online dictionary lookups), parsing complex file formats such as XML or CSV with full fidelity, accessing databases or network servers, or evaluating syntax of Python code (since you can easily use Python to parse and run Python snippets.)
But even in those cases, it makes most sense to implement most of the plug-in using Vimscript and use Python only as necessary to implement the parts you can't implement directly with Vimscript.
If you'd like to learn more Vimscript and how to write plug-ins with it, I strongly recommend Learn Vimscript the Hard Way by Steve Losh. It's an excellent resource if you're interested in creating Vim plug-ins.
I am aware that there are multiple libraries for both languages (R/Python) to call modules from the other one. I am looking for a way to have the backend of my code running in python mainly because of .pyc and speed, and also the front end running in R so I can have a Shiny app. I couldn't find a way to make python machine for the backend. If anyone knows how to do it in R/Rstudio please respond.
I don't have any good benchmarks on it's speed, but the reticulate package is the best way I know of to pass data to and from a python script without using a webserver. It lets you import python objects into R where they will act like R objects, accepting arguments and returning values.
There were a few wonky problems that I had when I just wanted to run functions from a single file. It ran into problems with import statements and multiple functions that called on each other. What worked well was to run the import statements separately (see the sapply() statement below) and to merge all the code in my python script into a single object. This worked nicely and seemed about as fast as running it in python normally (though I haven't done any real benchmarking)
library(reticulate)
use_python(python = '/usr/bin/python') # optionally specify python location
# Import statements are here, not in the file
sapply(c("import mysql.connector", "import re"), py_run_string)
# File contains only the definition of class MismatchFinder
source_python("python_script.py")
# Now we can call on that python object from R
result <- MismatchFinder()$find_mismatch(arg1, arg2)
My impression is that it might be simpler if you make your python code into a module and load it with: py_module <- import_from_path('my_python_module', path = 'PATH') but I didn't try that.
Hope this helps!
I believe what you are looking for is the code below. It will run a python script in R.
system('python3 file_name.py')
I have Sage 4.7.1 installed and have run into an odd problem. Many of my older scripts that use functions like deepcopy() and uniq() no longer recognize them as global names. I have been able to fix this by importing the python modules one by one, but this is quite tedious. But when I start the command-line Sage interface, I can type "list2=deepcopy(list1)" without importing the copy module, and this works fine. How is it possible that the command line Sage can recognize global name 'deepcopy' but if I load my script that uses the same name it doesn't recognize it?
oops, sorry, not familiar with stackoverflow yet. I type: 'sage_4.7.1/sage" to start the command line interface; then, I type "load jbom.py" to load up all the functions I defined in a python script. When I use one of the functions from the script, it runs for a few seconds (complex function) then hits a spot where I use some function that Sage normally has as a global name (deepcopy, uniq, etc) but for some reason the script I loaded does not know what the function is. And to reiterate, my script jbom.py used to work the last time I was working on this particular research, just as I described.
It also makes no difference if I use 'load jbom.py' or 'import jbom'. Both methods get the functions I defined in my script (but I have to use jbom. in the second case) and both get the same error about 'deepcopy' not being a global name.
REPLY TO DSM: I have been sloppy about describing the problem, for which I am sorry. I have created a new script 'experiment.py' that has "import jbom" as its first line. Executing the function in experiment.py recognizes the functions in jbom.py but deepcopy is not recognized. I tried loading jbom.py as "load jbom.py" and I can use the functions just like I did months ago. So, is this all just a problem of layering scripts without proper usage of import/load etc?
SOLVED: I added "from sage.all import *" to the beginning of jbom.py and now I can load experiment.py and execute the functions calling jbom.py functions without any problems. From the Sage doc on import/load I can't really tell what I was doing wrong exactly.
Okay, here's what's going on:
You can only import files ending with .py (ignoring .py[co]) These are standard Python files and aren't preparsed, so 1/3 == int(0), not QQ(1)/QQ(3), and you don't have the equivalent of a from sage.all import * to play with.
You can load and attach both .py and .sage files (as well as .pyx and .spyx and .m). Both have access to Sage definitions but the .py files aren't preparsed (so y=17 makes y a Python int) while the .sage files are (so y=17 makes y a Sage Integer).
So import jbom here works just like it would in Python, and you don't get the access to what Sage has put in scope. load etc. are handy but they don't scale up to larger programs so well. I've proposed improving this in the past and making .sage scripts less second-class citizens, but there hasn't yet been the mix of agreement on what to do and energy to do it. In the meantime your best bet is to import from sage.all.
I am about to get a bunch of python scripts from an untrusted source.
I'd like to be sure that no part of the code can hurt my system, meaning:
(1) the code is not allowed to import ANY MODULE
(2) the code is not allowed to read or write any data, connect to the network etc
(the purpose of each script is to loop through a list, compute some data from input given to it and return the computed value)
before I execute such code, I'd like to have a script 'examine' it and make sure that there's nothing dangerous there that could hurt my system.
I thought of using the following approach: check that the word 'import' is not used (so we are guaranteed that no modules are imported)
yet, it would still be possible for the user (if desired) to write code to read/write files etc (say, using open).
Then here comes the question:
(1) where can I get a 'global' list of python methods (like open)?
(2) Is there some code that I could add to each script that is sent to me (at the top) that would make some 'global' methods invalid for that script (for example, any use of the keyword open would lead to an exception)?
I know that there are some solutions of python sandboxing. but please try to answer this question as I feel this is the more relevant approach for my needs.
EDIT: suppose that I make sure that no import is in the file, and that no possible hurtful methods (such as open, eval, etc) are in it. can I conclude that the file is SAFE? (can you think of any other 'dangerous' ways that built-in methods can be run?)
This point hasn't been made yet, and should be:
You are not going to be able to secure arbitrary Python code.
A VM is the way to go unless you want security issues up the wazoo.
You can still obfuscate import without using eval:
s = '__imp'
s += 'ort__'
f = globals()['__builtins__'].__dict__[s]
** BOOM **
Built-in functions.
Keywords.
Note that you'll need to do things like look for both "file" and "open", as both can open files.
Also, as others have noted, this isn't 100% certain to stop someone determined to insert malacious code.
An approach that should work better than string matching us to use module ast, parse the python code, do your whitelist filtering on the tree (e.g. allow only basic operations), then compile and run the tree.
See this nice example by Andrew Dalke on manipulating ASTs.
built in functions/keywords:
eval
exec
__import__
open
file
input
execfile
print can be dangerous if you have one of those dumb shells that execute code on seeing certain output
stdin
__builtins__
globals() and locals() must be blocked otherwise they can be used to bypass your rules
There's probably tons of others that I didn't think about.
Unfortunately, crap like this is possible...
object().__reduce__()[0].__globals__["__builtins__"]["eval"]("open('/tmp/l0l0l0l0l0l0l','w').write('pwnd')")
So it turns out keywords, import restrictions, and in-scope by default symbols alone are not enough to cover, you need to verify the entire graph...
Use a Virtual Machine instead of running it on a system that you are concerned about.
Without a sandboxed environment, it is impossible to prevent a Python file from doing harm to your system aside from not running it.
It is easy to create a Cryptominer, delete/encrypt/overwrite files, run shell commands, and do general harm to your system.
If you are on Linux, you should be able to use docker to sandbox your code.
For more information, see this GitHub issue: https://github.com/raxod502/python-in-a-box/issues/2.
I did come across this on GitHub, so something like it could be used, but that has a lot of limits.
Another approach would be to create another Python file which parses the original one, removes the bad code, and runs the file. However, that would still be hit-and-miss.
I'm developing my own Python code interpreter using the Python C API, as described in the Python documentation. I've taken a look on the Python source code and I tried to follow the same steps that are carried out in the standard interpreter when executing a py file. These steps (sequence of C API function calls) are basically:
PyRun_AnyFileExFlags()
PyRun_SimpleFileExFlags()
PyRun_FileExFlags()
PyArena_New()
PyParser_ASTFromFile()
run_mod()
PyAST_Compile()
PyEval_EvalCode()
PyEval_EvalCodeEx()
PyThreadState_GET()
PyFrame_New()
PyEval_EvalFrameEx()
The only difference in my code is that I do manually the AST compilation, frame creation, etc. and then I call PyEval_EvalFrame.
With this, I am able to execute an arbitrary .py file with my program, as if it were the normal Python interpreter. My problem comes when the code that my program is executing makes use of the time module: all time module operations get blocked in the GIL! For example, if the Python code calls time.sleep(1), this call is blocked and never gets executed.
Obviously I am doing something wrong that blocks the GIL (and therefore blocks the time module) but I dont know how to correct it. The last statement in my code where I have control is in PyEval_EvalFrameEx, and from that point on, everything runs "as in regular Python interpreter", I think.
Anybody had a similar problem? What am I doing wrong, so that I block the time module?
Hope somebody can help me...
Thanks for your time. Best regards,
R.
You need to provide more detail.
How does your interpreter's behavior differ from the standard interpreter?
If you just want to run arbitrary source files, why are you not calling one of the higher level interfaces, like PyRun_SimpleFile? Did your code call Py_Initialize?