I have a Python console program, which uses input() from builtin to read data from the user. The program has a configuration option, which requires the user to input several paths. I would like to ease these inputs e.g. by a tab-completion.
Is it possible to implement an auto completion e.g. for path names? I see currently no way to hook into the input function to catch tab events/key presses...
Does the Python standard library provide such a feature?
The solution must work on Windows, Linux and Mac OS :).
Please note: I'm not looking for auto completion in Pythons interactive shell/console.
CLI Autocompletion is not included in Python standard library.
You can try:
urwid (http://urwid.org/tutorial/index.html) for a rather low level CLI library
Cement for a full CLI framework (http://cement.readthedocs.io and http://cement.readthedocs.io/en/latest/examples/bash_auto_completion/)
In all cases it's probably quite a bit of work :-/
Related
I'm trying to wrangle my Gnome desktop windows and I would like to automate their placement using Python. What is a good approach for this?
You have 2 options:
If you only need it to work in an X session, then this is relatively easy to do using xdotool (short summary in this SO answer). You can also use it from Python, as explained in another question on SO.
The Wayland protocol does not allow this by default for security reasons. If you want to be able to support both X and Wayland, you'll have to get to the inner working of GNOME Shell by writing an extension (in JS). This extension then either does the moving, or exposes an API to move windows, which you can then call from a Python script.
I am making a notepad using Tkinter and I want to be able to print to the printer the notes taken.I am using a Text object in order to allow the user to write his notes.I couldn't find anything on to how to print to printer with Tkinter and I want to avoid using PyQT or win32api that I know have printer support(due to complexity and lack of experience).I am planning on releasing the application only on windows so I donnot need the it to be cross-platform.
Fact to Accept: Cross-Platform Printing is hard.
Depending on the system you use, the commands to send text / files to a printer will be very different.
For Windows, you should probably use the Python win32print module, even if you say you don't want to use it because of complexity etc.
I honestly think that trying to solve it any other way would be a lot more complicated in the end.
For Linux, Mac, Unix, you can send commands much more directly using LPR to the system printer through the built-in os.popen() or the new subprocess module in Python, but for Windows, my bet is you're better off using the win32print module.
Providing cross-platform printing functionality will always be a challenge because of the differences in the underlying sub-systems on different operating systems.
The basic approach
You'll need to separate the logic of your code, so that depending on the underlying OS, your program will choose the right method for executing the printing functionality you need.
I don't know of any way around this.
Using the Python win32 modules needn't be as complex as you might think.
For Windows
This can be done with the module win32print,
Documented nicely here
For Linux, macOS, Unix
Check out the use of LPR commands, and combine this with basic Python os.popen calls or using the new Python subprocess module
I know you probably wanted a more "copy/paste friendly" way, but that would be very hard without having the rest of your code and not knowing the exact requirements / specs for your app.
Bottom line is that you'd probably end up writing custom code for each platform for printing anyway, so might as well jump in head first.
When I have a some crash report including some MacOSX library (in this case, I'm mostly interested in Python), how can I get more info about it? The library does not contain the debugging information, so the crash report lacks line numbers and other useful stuff. Can I get the debugging information elsewhere and reconstruct the line numbers?
You may want to look at the lldb debugger. It is scriptable in python and pretty easy to do things like symbolicating a crash report. There is even an included example python script that can symbolicate a standard Mac OS X crash report (assuming you have dSYMs for some of the frameworks) and provide file name & line number information.
See http://lldb.llvm.org/symbolication.html for more information about using this, or it is easy to write your own python scripts with lldb. You can make a python method that is called from an lldb session (like lldb.macosx.crashlog does), or you can write a standalone python script that loads lldb and does whatever you want. lldb is structured like a library (a framework on Mac OS X), the lldb command-line command is one possible client of LLDB.framework.
I want to develop a small Python app that interacts with the user via the console/command line. Are there any good libraries I can use to get user input and display the output in a nice-looking way? I tried searching but most of what I found was command-line argument processing, didn't see anything about user interaction in the shell.
It should be crossplatform (Windows and Linux)
A really excellent library is cmd which is part of the python standard library. It is cross platform Windows, Linux, Mac. You just have to implement one class and it offers so many great features:
provides list of supported commands(they end up being implemented as methods)
help command that can be called to explain one of your commands
handles the whole entering a command, checking syntax, and calling your method command loop cycle.
users can save the commands they have run in an interactive session and run them as a script. check out the example below.
If you take the turtle shell example and save it as turtleshell.py and save the below turtle script file as circles.txt
circle 20
circle 50
circle 100
bye
then you could run the turtle script with the following command:
cat circles.txt | ./turtleshell.py
so in the simple example shown in the docs the developer has essentially made a simple mini-language that can be used as an easier interface to to the turtle module making it even easier to introduce programming to kids. The examples above have been taken from the python3 docs because they have included a detailed example in their docs which wasn't there in the 2.7 docs, but cmd is available and fully functional in python 2.3 and later.
You can control the Unix terminal with the curses library. The library essentially lets you build a simple terminal GUI.
If you need more, take a look at Urwid as well. Urwid offers more complex GUI widgets for the discerning terminal GUI developer. :-)
Curses is the most widely used in the Unix environment according to the doc. For Windows you could look at Windows Console Driver, WConio - Windows CONsole I/O for Python or Wcurses. I couldn't find much on cross platform console libraries unfortuntaly.
If your Windows users are CLI users they probably have cygwin which has ncurse support so curses is still the best option if you ask me.
I have a python modules question:
I need to write a script that will take the output from various pieces of hardware and then store them in a database. I know about the current hardware but I do not know about any additional hardware that will be added in the future. Sorry let me be more specific.
The script will retrieve power information from hardware modules connected to power distribution boards. These modules take the raw sensor information and make it available over tcp/ip. I have written functions into the test script that interrogates the hardware modules and gathers the info. The problem is that this will need to be deployed where different hardware modules will be used, not just the ones that I know about. All the hardware modules will make the same info available but they will all do it different ways. Some use telnet or ssh, some provide a web page, some provide and xml output, some will use snmp. I want to create a mechanism that a python module can be written for a new device that pulls out the info and provides it to the script in a standard way so that the script can populate the database and life goes on as normal.
So Instead of adding a new function to the script each time a new hardware module type is encountred and then wrapping it in 'if' statements etc... is there a mechanism to offboard that function into a module that can be dynamically added to the script. A plugin type mechanism comes to mind I suppose. But I don't think that quite fits.
Any suggestions or directions to places where something like this has been used or implemented would be greatly appreciated.
You could use import function in your code. Depending on your Python version, try importlib (Added in Python 2.7) or __import__ function.
You could create plugins for each hardware device you're going to support. Each such plugin could then register itself somewhere (main module path put in Environment variable or known path in Windows registry). Your code would then check that location (periodically, on startup - depending on your needs) and load each plugin found. You could also use distutils and have plugins packed as egg packages.
Please note that while it works quite well when you're running from Python sources, it's getting more complex if you plan to build executables with PyInstaller or Py2exe.