Locate the Containing Package or Module **Before** Importing - python

I often find myself needing to import something, but not quite sure of its fully qualified name. I usually end up opening a browser, performing an internet search like python [target_of_import], and scanning a page or two until I find it.
This works, but causes a relatively long break in my workflow, especially if I have to search for a few in a row. How do other people address this?
Is there something like Haskell's Hoogle for Python?
[Note: I currently use vim, in case anyone suggests an IDE-based solution.]
EDIT: For answers concerning autocomplete, please specify this. In general, autocomplete is probably a non-starter solution since in the particular case I am asking about the leftmost characters of the string to be autocompleted are not known.
EDIT 2: While I will not categorically rule out suggestions concerning switching to/learning a new IDE, I'm pretty unlikely to completely change the way I work to accomplish this (e.g., switching from vim on the command line to something like Eclipse + plugins).

You can do this in vim using the Unite.vim
Enable fuzzy file searching by adding the following to your .vimrc:
call unite#filters#matcher_default#use(['matcher_fuzzy'])
Search for file:
:UniteWithInput file_rec/async:/base/path:!<cr>
Search within files:
:UniteWithInut grep:/base/path<cr>
Search file names and within files
:UniteWithInput file_rec/async:/base/path:! grep:/base/path<cr>
(Use to change between sources)
See also :h :UniteWithCursorWord
This will open a buffer with the file matches. You can open the file by pressing enter but since you only want copy the file name simply use y$ to yank the line, q to close the buffer and the p to paste the yanked line.

Related

Python module to keep a file location variable updated?

I have a variable in my python script that holds the path to a file as a string. Is there a way, through a python module, etc, to keep the file path updated if the file were to be moved to another destination?
Short answer: no, not with a string.
Long answer: If you want to use only a string to record the location of this file, you're probably out of luck unless you use other tools to find the location of the file, or record whenever it moves - I don't know what those tools are.
You don't give a lot of info in your question about what you want this variable for; as #DeepSpace says in his comment, if you're trying to make this String follow the file between different runs of this program, then you'd be better off making an argument for the script. If, however, you expect the file to move sometime during the execution of your program, you might be able to use a file object to keep track of it. Or, rather - instead of keeping the filepath in memory, keep a file descriptor in memory instead (the kind you would generate by using the open() function, and just never close that file until the program terminates. You can use seek to return to the start of the file if you needed to read it multiple times. Problems with this include that it's not memory-safe, and it's absolutely not a best practice.
TL;DR
Your best bet is probably to go with a solution like #DeepSpace mentioned where you could go and call your script with a parameter in command-line which will then force the user to input a valid path.
This is actually a really good question, but unfortunately purely Pythonly speaking, this is impossible.
No python module will be able to dynamically linked a variable to a path on the file-system. You will need an external script or routine which will update any kind of data structure that will hold the path value.
Even then, the name of the file could change, but not it's location. here is what I mean by that.
Let's say you were to wrapped that file in a folder only containing that specific file. Since you now know that it's location is fixed (theoretically speaking), you can have another python script/routine that will read the filename and store it in a textfile. Your other script could go and get that file name (considering your routine would sync that file on a regular basis). But, as soon as the location of the file changes, how can you possibly know where it is now. It has to be manually hard coded somewhere to have something close to the behavior your expecting.
Note that my example is not in any way a solution to go-to for your problem. I'm actually trying to underline the shortcomings of such a feature.

adding one more extension to syntax highlighting in gedit

I am creating some python files with extensions .pyt. Every thing is working fine except syntax highlighting.
My new .pyt files are not syntax highlighted as normal .py files do.
Ofcourse, I can change in
view>Highlight Mode> Script > python
manually for every file, but there are around 1200 files and they keep growing. I really dont want to do this manually for each file for the the first opening.
Is there any way to apply default python syntax highlighting to my new .pyt files.
Any Help will be highly appriciated.
P.S. I have read how to do this for gedit 2.0 in many pages like page1, page2 and many more pages but couldn't find any good working processes for gedit 1.0.
Although you (the OP) wrote that you found pages that were meant for gedit 3, I'm going to suggest that you look at this question.
Basically, even if gedit 1.0 used an older version of gtksourceview, as long as you can find the python.lang file in your system and change it, you can probably make it work. After all, it must have some way of specifying on which files it's used.
For new (Gtksourceview 2.0) lang files, this is done by adding either the mimetypes or globs property within the language's tag. So when you find the python.lang file, look for the *.py extension, and see if you can add *.pyt there, as is detailed in the question I linked above.

text formatting using programming

Is there a way to change the text-formatting of a .odt file using a programming software? I'm trying to make some text editions which is time consuming to be done manually in lib.office (text is big). Using R or python I can edit spaces and carriage breaks using brute force, but, to do edits like left, right-justify etc., I have to finally go back to the editor to do it. I can see there are functions in python to include tabs, change case etc., but is it possible to do right-left-centre justification in a .odt text file indirectly using a programming software?
This is a great job for a scripting language like Python. I think you want string methods like str.ljust. That method left-justifies strings.
to open the file.
Alternately, you might try defining a macro in OpenOffice (if they exist and your task is simple) - or investing some time in learning emacs or something and using that (this link shows at least some degree of emacs support for .odt files). Or learn vim, that is the one true way!
Edit: after some research I found this. It seems you could unzip the .odt file, read files within it, and manually edit the text nodes of the XML there. However, it seems like it might be easier to use a library - here are two:
https://pypi.python.org/pypi/odfpy
http://ooopy.sourceforge.net/
Edit 2: in terms of actually justifying the text, presuming you have extracted it, this:
def justify(string, left=True):
if left:
return "\n".join(line.lstrip() for line in string.splitlines())
else:
lines = string.splitlines()
longest_line = len(max(lines, key=len))
return "\n".join(line.rjust(longest_line) for line in lines)
should work.

Dangerous Python Keywords?

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.

Python file read problem

file_read = open("/var/www/rajaneesh/file/_config.php", "r")
contents = file_read.read()
print contents
file_read.close()
The output is empty, but in that file all contents are there. Please help me how to do read and replace a string in __conifg.php.
Usually, when there is such kind of issues, it is very useful to start the interactive shell and analyze all commands.
For instance, it could be that the file does not exists (see comment from freiksenet) or you do not have privileges to it, or it is locked by another process.
If you execute the script in some system (like a web server, as the path could suggest), the exception could go to a log - or simply be swallowed by other components in the system.
On the contrary, if you execute it in the interactive shell, you can immediately see what the problem was, and eventually inspect the object (by using help(), dir() or the module inspect). By the way, this is also a good method for developing a script - just by tinkering around with the concept in the shell, then putting altogether.
While we are here, I strongly suggest you usage of IPython. It is an evolution of the standard shell, with powerful aids for introspection (just press tab, or a put a question mark after an object). Unfortunately in the latest weeks the site is not often not available, but there are good chances you already have it installed on your system.
I copied your code onto my own system, and changed the filename so that it works on my system. Also, I changed the indenting (putting everything at the same level) from what shows in your question. With those changes, the code worked fine.
Thus, I think it's something else specific to your system that we probably cannot solve here (easily).
Would it be possible that you don't have read access to the file you are trying to open?

Categories

Resources