You are not logged in.
My programming is generally messy (and very procedural), but I've been having to pick up some basic Python for a course that I teach. Since I've always heard that python makes simple utilities dead-simple to write, I decided to try one out on my own.
PROBLEM DESCRIPTION:
I copy some song lyrics/chords from a db-management app (opensong, its in the AUR) to paste it in libreoffice for formatting and eventual distribution/printing. The format is as below, basically lines starting with periods are chord lines, and lyrics may have spaces/underscores added to do alignment (see extra spacing between 'is' and 'to' in third line and the underscores on 4th line). Sections are listed like [C] for chorus or [V1] for verse 1.
[C]
.C G/B Am C/G
Jingle bells, jingle bells
.C F/G
Jingle all the way
.F F/C C C
Oh what fun it is to ride
. G G F/A G/B C
In a one-horse o___pen sleigh
So, its convenient to sometimes have just the lyrics, which means removing all chords lines and the multiple-space/underscore characters. In addition, due to the way libreoffice treats the initial text when I paste it, I get windows line endings (and when saving, reopening I get additional spacing because of that).
So here's what I have, all messy and basically uncommented, but should be simple enough to follow. And it works!:-
import sys
from PyQt4.QtGui import QApplication
app = QApplication(sys.argv)
clipboard = QApplication.clipboard()
text = clipboard.text()
splittext = text.splitlines() #deals with windows newlines if they exist
newtext = ""
for line in splittext:
if line[0] != ".": #remove chord lines
oldchar = " "
newline = ""
for char in line:
if not (char == " " and char == oldchar) and char != "_":
newline += char
oldchar = char
if newtext != "":
newtext += "\n" + newline
else:
newtext += newline
clipboard.setText(newtext)
sys.exit(app.exec_())
My questions:-
1. Am I 'wasting' code at any point? (subjective I know)
2. Without the sys.exit call I get really weird behaviour, where 'setText' would seem to grab the selection and I'd then have to re-run the app again (and get an error on clipboard.text() before I could actually use the mouse to select anything. This was the same behaviour I got from using pyGtk, actually. Does it have something to do with both QT and GTK assuming they're being used in graphical apps?
3. It seemed to me while doing my searching that the documentation for pyGTK is much better than that for pyQT, did I miss something (a good pyQT wiki or example website, for example)?
Allan-Volunteer on the (topic being discussed) mailn lists. You never get the people who matters attention on the forums.
jasonwryan-Installing Arch is a measure of your literacy. Maintaining Arch is a measure of your diligence. Contributing to Arch is a measure of your competence.
Griemak-Bleeding edge, not bleeding flat. Edge denotes falls will occur from time to time. Bring your own parachute.
Offline
1. If you are willing to use a regex and some list comprehension you can get rid of the for loops:
import sys, re
from PyQt4.QtGui import QApplication
# matches spaces proceded by spaces and underscores.
r = re.compile(r'(?<= ) +|_+')
app = QApplication(sys.argv)
clipboard = QApplication.clipboard()
clipboard.setText("\n".join(
r.sub('', line).strip()
for line in clipboard.text().splitlines()
if line and line[0] != "."
))
sys.exit(app.exec_())
2. IIRC, X11 doesn't store the contents of the clipboard. When an application puts data on the clipboard, it must stick around to provide the data when requested by another application. This is probably the source of your problem.
3. Just read the C++ QT documentation. The method calls are approximately the same in python.
Offline
1. If you are willing to use a regex and some list comprehension you can get rid of the for loops:
Thanks, will take a look at this after I'm back from lunch.
2. IIRC, X11 doesn't store the contents of the clipboard. When an application puts data on the clipboard, it must stick around to provide the data when requested by another application. This is probably the source of your problem.
I'm aware that X11 only stores (for want of a better word) a pointer/reference to the app providing the data, but as far as I can tell my python application does seem to hang around. Will test that a bit, if this theory is correct, though, wouldn't that mean that sys.exit would mean what's in the clipboard (and from my small pthon script) is deleted? Because it's still usable even after the script finishes
3. Just read the C++ QT documentation. The method calls are approximately the same in python.
I've come across all the C++ documentation, did not realize it was approximately the same. Unfortunately that means that its missing what (IMHO) is the best part about the pyGtk document, small code fragments which can be tried out...
Allan-Volunteer on the (topic being discussed) mailn lists. You never get the people who matters attention on the forums.
jasonwryan-Installing Arch is a measure of your literacy. Maintaining Arch is a measure of your diligence. Contributing to Arch is a measure of your competence.
Griemak-Bleeding edge, not bleeding flat. Edge denotes falls will occur from time to time. Bring your own parachute.
Offline