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.
This question already has answers here:
How do I script a "yes" response for installing programs?
(6 answers)
Closed 5 years ago.
I've come across a program run by Python2, which asks many questions of yes/no type.
I want to answer "yes" to all of them, but it's really difficult because there are literally hundreds of them (it's basically a parser of code, which asks about every found variable).
So, is there any possibility, how to force Python answer "yes" automatically?
I think about something similar to apt-get -y install. And I'm interested in answers about both Python2 and Python3.
I'm using Lubuntu 16.10 and my default Shell is GNU bash, version 4.3.48.
Try using yes:
yes | python ./script.py
If you have a more complex state to manage during interaction, there is also expect.
yes emits y by default, but you can customize it by providing an argument (e.g. yes yes), thanks #tobias_k. If you need a portable way (in Python), go with the suggestion from Jean-François Fabre (or just hack the script).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I'm not sure if this question is appropriate for here, so if it isn't, I apologize, however I'm not sure where else to ask.
I've been learning python on codecademy however when I downloaded python myself it is nothing like what I've become used to. I can't for the life of me figure out where to define fucntions and I don't know where to find the console.
I would like basically the same that codecademy has with the editor / console 2-in-1 combo. Where can I find this?
FWIW I have python 3.3.5 that I downloaded from www.python.org
Thank you.
The version used by CodeAcademy is Python 2.
Download Python 2 from here: https://www.python.org/download/releases/2.7.6/
Once you are done downloading and installing it, open "IDLE (Python GUI)" and press <CTRL + N>.
This will open the python text editor. Enter your code, functions, and etc. in here.
Then press <F5> to run it.
This is what you should be seeing:
Python Shell (when you first run Python IDLE): http://gyazo.com/a3eca0fb88139c3a526b6f8fdfa6e4c0
And then when you press , you should see a blank file: http://gyazo.com/1d4426ac0e9fb49a50ea33fce881fcb6
As Amit pointed out, CodeAcademy is currently using Python 2.7.3. (I don't for the life of me see why that got downvoted, it's entirely relevant to the question, and "Python 2" covers an awful lot of ground.)
I can't recommend Python 3.x for working on CodeAcademy problems; the syntax and module changes from 2.x to 3.x will give you unending problems (with any luck they will update their course soon).
You can download the latest Python 2.7 (currently 2.7.6) from Python.org (https://www.python.org/download/releases/2.7.6); it is essentially identical to the CodeAcademy version with minor bug fixes.
For an interactive shell, I recommend Dreampie (http://www.dreampie.org/download.html); it has decent live code editing with code completion and documentation.
I also use the Notepad++ text editor (http://notepad-plus-plus.org/download/v6.5.5.html) quite a bit; it has decent code formatting and coloring (although the Python indentation is broken out-of-the-box; you have to go into Settings / Preferences..., Tab Settings, Python, uncheck Use default value, and check Replace by space to make it PEP-8 compliant). There is also a plugin, PyNPP, which will let you launch Python code directly from NotePad++ (no more cut-and-paste!) - you can get it through Notepad++'s built-in Plugin Manager - then hit Alt-Shift-F5 to launch the current file in an interactive Python shell window.
import sys
print sys.version_info
# sys.version_info(major=2, minor=7, micro=3, releaselevel='final', serial=0)
so you are looking for the 2.7.3 version from python.org
If you are very new to python, you may be experiencing the differences between python2 and python3. One main difference that a beginner will run into
print "this works" # in python 2
print("this works") # in python 3
print becomes a method, rather than whatever it was before... Also division is floating type by default, to get integer division in python 3 use 4//3 # output: 1 instead.
It sounds like you are looking for the interactive prompt, and a text editor.
If you want the interactive Python prompt:
If you are on Windows, look for a directory called C:\Python? and find a bin\python.exe.
If you are on Linux or OSX, typing python in the Terminal should get you the interactive prompt.
For a text editor to define functions, Notepad or gedit should help you.
Like I suspect most people reading this, I have no experience with Codecademy, but they seem to be running their own web front-end http://repl.it/ which is available for download -- if you really wanted to (and had the skills to set up a web server to host it on) you could even run that yourself.
The user interface looks like your run-of-the-mill programmers' editor; the abysmal color scheme is probably not worth copying anyway, and the interactive evaluation of your code (rather than the traditional save, quit, run, repeat cycle) is available via plug-ins in many modern editors. Or, as others have already suggested, you could use IDLE, which is a part of the standard Python distribution.
If Python's default interactive mode feels clumsy but otherwise right, you might also want to look at a popular souped-up replacement iPython
When I want to write a Python program, I open Windows explorer and go to the directory where I keep my .py files. When I right click a .py file, I am given the choice to "Edit with IDLE." I like IDLE really well. When I "Run" a program a Python Shell window opens. I work between these two windows.
(I think I used Notepad to create the first .py file because if you open IDLE you get a Python Shell.) (Another oddity - when I want to create a new program file I "Save As" to a new name.) In spite of these oddities IDLE is my favorite programming environment.
I think Komodo Edit is the editor codeacademy used to teach us.
This question already has answers here:
How do I protect Python code from being read by users?
(29 answers)
Closed 9 years ago.
I am writing code (Python and wxpython for GUI) which will run on Debian OS on Raspberry PI. I want to protect/hide the source code. Is there any way to do it? Probably py2exe, or converting it to a library or something else?
The compiled code (.pyc files) can be used if you wish for others to be able to execute but not to read or modify the source code (.py, .pyw).
Simply:
run your application
then copy all the relevant .pyc files into another folder and you should be able to
run it all from the new location
So long as all the appropriate modules are still able to be loaded, everything will work. This will require the version of python to be the same (can't run .pyc files from python 2.4 with python 2.7 and vice-versa)
The other thing to know is that strings will be preserved. You should open them up in a good text editor (I use vim) and inspect the content if you are worried about what others can see.
py2exe is of course another example, but you lose the ability to have cross-platform code at that point -- and if your application is for the Raspberry Pi -- that won't work.
Since you provided no other information about how you intend to run the code, it's not clear if the source will be a module or intended to be run directly. You should read this post to learn more.
This question already has answers here:
How do I watch a file for changes?
(28 answers)
Closed 5 years ago.
I'm trying to detect when a new file is created a directory or when an existing file is modified in a directory.
I tried searching for a script that would do this (preferably in python or bash) but came up short. My environment is linux with Python 2.6
Related Question
You can use gio which is the Filesystem part of GLib (In GLib's python bindings)
import gio
def directory_changed(monitor, file1, file2, evt_type):
if (evt_type in (gio.FILE_MONITOR_EVENT_CREATED,
gio.FILE_MONITOR_EVENT_DELETED)):
print "Changed:", file1, file2, evt_type
gfile = gio.File(".")
monitor = gfile.monitor_directory(gio.FILE_MONITOR_NONE, None)
monitor.connect("changed", directory_changed)
however, your program must be running a GLib mainloop for the events to arrive. One quick way to test that is by using:
import glib
ml = glib.MainLoop()
ml.run()
GLib is a high-level library which is well suited for Applications. You don't have to care about which underlying system it uses for the file monitoring.
I now see you use Fedora Core 2. Really version 2? That might be too old to use GIO in GLib. Pyinotify that has been mentioned might be a better solution, although less portable.
If you're using Linux, you may try pyinotify that acts like an object-oriented wrapper around the inotify(7) system calls. The project website contains quite straightforward tutorials and examples.
If you can use PyQt, there's QFileSystemWatcher that does just this.
There are also Python bindings for gamin.