A localized command line application looks strange when some part of the messages are in the user language and some other parts, in English.
I don't know if I messed up anything when I installed Python 3 from source, it seems there are no *.mo files, so argparse (among the whole) is not localization aware.
The API does not seems to offer a way to localize, neither. Or did I missed it?
I could patch argparse.py, but I won't, as I want it to be portable, and I'm not OK with suggesting users to patch their Python installation.
Question in fewer words: how to localize argpase without patching Python standard library?
Related question: is this possible?
By default argparse uses the gettext module to translate the messages so you can easily translate them if you want to.
To generate the *.pot files (which you can convert to *.mo files after translation) you can use the pygettext program which is available in the built-in gettext module in python.
Usage:
python pygettext.py argparse.py
This will generate a messages.pot which you can translate, after that just generate the .mo (many ways to do this, just google).
For more info about pygettext see the Python manual about the subject: Internationalizing your programs and modules.
Related
My project has to be extensible, i have a lot of scripts with the same interface that lookup things online. Before i was using __import__ but that does not let me put my 'plugins' on a dedicated directory:
root/
main.py
plugins/
[...]
So my question is: Is there a way to individually import modules from that subdirectory? I'm guessing importlib, but i'm so lost in how the python module loading process works... What i want to do is something like this:
for pluginname in plugins:
plugin = somekindofimport("plugins/{name}".format(name=pluginname))
plugin.unififedinterface()
Also, as a side question, the way am i trying to achieve extensibility is a good way?
I'm on python3.3
Stop thinking in terms of pathnames and start thinking in terms of packages. Read Packages in the tutorial, and if you want more detail see The import system.
But the basic idea is this:
Create a file name plugins/__init__.py. It can be empty; that's enough to turn plugins into a package. Which means you can import modules from that package with:
import plugins.plugin
So, how do you do this dynamically? That's what importlib is for. (You can also use __import__ here, but it's less flexible, and less readable in non-trivial cases, so unless you need pre-3.3 compatibility, don't.)
plugin = importlib.import_module('plugins.{name}'.format(name=pluginname))
It would probably be cleaner to import plugins to get the package, and then use relative imports from within that package, as shown in the examples in the import_module docs.
This also means Python takes care of the .pyc creation and caching, etc.
And it means that you can later expand plugins to be a "namespace package", which can be split across multiple directories like /usr/share/myapp/plugins for stock plugins, /etc/myapp/plugins for site plugins and ~/myapp/plugins for user-specific plugins.
If you really, really want to import from a directory that isn't a package, you can create a module loader and use it, but that's a whole lot of work for no actual benefit. (It's actually not that hard in 3.3 (SourceLoader and friends will do most of the work for you), but you will find almost no examples out there to guide you; instead, you'll find examples of the 2.6-3.2 way, or the 2.0-2.5 way, both of which are hard.) Plus, it means that if someone creates a plugin named, say, gzip, you can end up blocking the stdlib gzip module with the plugin. (That's especially fun if the gzip plugin tries to use the gzip stdlib module, as it likely will…) If the plugin ends up being named plugins.gzip, there's no problem.
Also, as a side question, the way am i trying to achieve extensibility is a good way?
As long as you only want to support 3.3+, yes, I think this is a great solution.
Before 3.3, using a package for plugins was a lot more problematic. People have come up with a variety of different plugin systems—in one case going so far as to dynamically create module objects and execfile into them. If you need to deal with that, I would suggest looking at existing Python apps with plugins (e.g., MusicBrainz Picard) to get different ideas.
I was looking for a quick way to autoformat/pretty-print JSON in Vim the other day and found this great little command on Stack Overflow: :%!python -m json.tool
That sent me on a search for a list of other Python tools to pretty-print common web files, but I couldn't find much. Is there a good resource/list of Python tools that they find particularly useful for cleaning up poorly formatted web stuff inside Vim (e.g. HTML, XML, JavaScript, etc.)?
Python
Are you just looking for a resource for Python one-liners? You could browse through the Python standard library documentation to find more inspiration.
Or simply google "python one-liners json.tool" to find additional resources. For example, this Reddit post: Suggestion for a Python blogger: figure out what what all the stdlib main functionality is, and document it
Command line
Vim supports more than just Python (e.g. HTML Tidy as Keith suggested). Any tool that can accept pipe/standard input will integrate well with Vim.
The % command just picks a range that contains the entire file, and ! filters that range through an external program.
See :help :% and :help :!
For XHTML and XML files you can use tidy.
:%!tidy -i -asxhtml -utf8
:`<,`>!tidy -i -xml -utf8
The last one works on visual selections.
Vim has a command to do it, = (equal), like in ggvG= will reindent the whole file. Try :help = for more info about how to use functions and external programs with =. The default configuration uses internal indenting rules which works for most file types.
There are loads of good tools that are can convert text between the two formats:
par: for hard line wrapping.
pandoc: for HTML, LaTeX, rst, and Markdown
autopep8: for parsing Python code into an AST and spitting it out as pep8 compliant.
...
Vim is designed to make use of such utilities by the powerful formatprg settings. That by default is mapped to the gq operator. It works well with Vim motions, Vim text objects, selections, etc.
For instance, I use the setting below for on my Python files
au FileType python setlocal formatprg=autopep8\ --indent-size\ 0\ -
John MacFarlne has a good article about creating a specialised script using pandoc which you could stick in your vimrc.
!autopep8 -i % seems to work fine in vim . The -i switch is to over-write the existing file in place. Use more # autopep8 --help.
There is a vim plugin for this if you really are thinking of being a power user. Outside of vim you can test it with autopep8 -diff {filename} or autopep8 {filename}.
For my project I would be using the argparse library. My question is, how do I distribute it with my project. I am asking this because of the technicalities and legalities involved.
Do I just:
Put the argparse.py file along with
my project. That is, in the tar file for my project.
Create a package for it for my
distro?
Tell the user to install it himself?
What's your target Python version? It appears that argparse is included from version 2.7.
If you're building a small library with minimal dependencies, I would consider removing the dependency on an external module and only use facilities offered by the standard Python library. You can access command line parameters with sys.argv and parse them yourself, it's usually not that hard to do. Your users will definitely appreciate not having to install yet another third party module just to use your code.
It would be best for the user to install it so that only one copy is present on the system and so that it can be updated if there are any issues, but including it with your project is a viable option if you abide by all requirements specified in the license.
Try to import it from the public location, and if that fails then resort to using the included module.
You could go with Ignacio's suggestion.
But... For what it is worth, there's another library for argument parsing built into Python, which is quite powerful. Have you tried optparse? It belongs to the base Python distribution and has been there for a while...
Good luck!
I once did a cursory search and found no good CVS bindings for Python. I wanted to be able to write helper scripts to do some fine-grained manipulation of the repository and projects in it. I had to resort to using popen and checking stdout and stderr and then parsing those. It was messy and error-prone.
Are there any good quality modules for CVS integration for Python? Which module do you prefer and why?
While I am at it, is there a good Subversion integration module for Python? My understanding is that Subversion has a great API for such things.
For cvs, pyCVS may be worth a look.
For svn, there is pysvn, which is pretty good.
Tailor, a Python program which lets different version control systems interoperate, simply calls the external programs cvs and svn when working with repositories of those formats. This seems pretty ugly, but reduces Tailor's dependencies from "requires ____ bindings" to "requires working system".
This is something that I think would be very useful. Basically, I'd like there to be a way to edit Python source programmatically without requiring human intervention. There are a couple of things I would like to do with this:
Edit the configuration of Python apps that use source modules for configuration.
Set up a "template" so that I can customize a Python source file on the fly. This way, I can set up a "project" system on an open source app I'm working on and allow certain files to be customized.
I could probably write something that can do this myself, but I can see that opening up a lot of "devil's in the details" type issues. Are there any ways to do this currently, or am I just going to have to bite the bullet and implement it myself?
Python's standard library provides pretty good facilities for working with Python source; note the tokenize and parser modules.
Most of these kinds of things can be determined programatically in Python, using modules like sys, os, and the special _file_ identifier which tells you where you are in the filesystem path.
It's important to keep in mind that when a module is first imported it will execute everything in the file-scope, which is important for developing system-dependent behaviors. For example, the os module basically determines what operating system you're using on import and then adjusts its implementation accordingly (by importing another module corresponding to Linux, OSX, Windows, etc.).
There's a lot of power in this feature and something along these lines is probably what you're looking for. :)
[Edit] I've also used socket.gethostname() in some rare, hackish instances. ;)
I had the same issue and I simply opened the file and did some replace: then reload the file in the Python interpreter. This works fine and is easy to do.
Otherwise AFAIK you have to use some conf objects.