sorting grocery list

68 views
Skip to first unread message

cedardoc

unread,
Mar 12, 2011, 7:06:18 PM3/12/11
to autokey-users
I've been searching for how to do a simple line sort in python to sort
lines of text and can't find anything that doesn't involve converting
the block of text into a comma separated list, sorting, and then
converting it back.

The closest thing I got is this:

keyboard.send_keys("<ctrl>+c")
contents=clipboard.get_selection()
contents.sort()
clipboard.fill_clipboard(contents)
keyboard.send_keys("<ctrl>+v")

but that gives this error:
Traceback (most recent call last):
File "/usr/lib/python2.6/dist-packages/autokey/service.py", line
403, in execute
exec script.code in self.scope
File "<string>", line 3, in <module>
AttributeError: 'unicode' object has no attribute 'sort'

I realize this is not technically an autokey problem, but I'd think a
functioning sort script would be a nice asset for other amateurs who
like using autokey like me. Can any of you see a simple solution for
this please?

Thanks,
Dave

Chris D

unread,
Mar 13, 2011, 6:04:52 PM3/13/11
to autokey-users
What format is the text in the selection? What your code is trying to
do is sorting a text string, which you can't do in Python. You must
first convert the text into a list object which can then be sorted.

cedardoc

unread,
Mar 13, 2011, 10:57:21 PM3/13/11
to autokey-users
"What format is the text in the selection?"
The format is just lines of text in a text editor (gedit)

"sorting a text string, which you can't do in Python. "
I was afraid of that.

"You must first convert the text into a list object which can then be
sorted. "
Sounds complicated - I'll maybe see if I can do it in a bash script
and just launch that script to do it - I just thought if it could
easily be done in python it might be a faster script. In any even,
I'll post back here if I figure it out.

Thanks

cedardoc

unread,
Mar 13, 2011, 11:44:04 PM3/13/11
to autokey-users
Okay, I got it working (a little slow, however)

keyboard.send_keys("<ctrl>+c")
contents2=[]
contents=clipboard.get_selection()
fullpath = "/home/david/temp.txt"
fullpath2 = "/home/david/temp2.txt"
tempTxt = open( fullpath, "w")
tempTxt.write(contents)
tempTxt.close()
command = "sort /home/david/temp.txt > /home/david/temp2.txt"
system.exec_command(command, False)
f = open(fullpath2, 'r')
contents2= f.read()
f.close()
clipboard.fill_clipboard(contents2)
time.sleep(1)
keyboard.send_keys("<ctrl>+v")


Dave

Chris D

unread,
Mar 14, 2011, 6:45:11 PM3/14/11
to autokey-users
It is in fact much quicker and easier to do in Python. All that is
needed is to split the string on newlines, which Python will do very
simply without any special operations:

contentsList = contents.split()
contentsList.sort()
contents = contentsList.join('\n')

Also, you don't need a control+C if you are using get_selection(), as
the selected text is already in the X selection.

cedardoc

unread,
Mar 15, 2011, 4:50:59 PM3/15/11
to autokey-users
Thank you for your reply,

Here's what I tried next:

#keyboard.send_keys("<ctrl>+c")
contents=clipboard.get_selection()
contentsList = contents.split()
contentsList.sort()
contents = contentsList.join('\n')
clipboard.fill_clipboard(contents)
keyboard.send_keys("<ctrl>+v")

and the error I'm getting is this:
Traceback (most recent call last):
File "/usr/lib/python2.6/dist-packages/autokey/service.py", line
403, in execute
exec script.code in self.scope
File "<string>", line 5, in <module>
AttributeError: 'list' object has no attribute 'join'

I thought maybe the "List" in contentsList.sort() was doing that so I
changed the name of the variable, but that didn't work either

then I tried this after googling around a bit:
contents = '\n'.join(contentsList)

and that worked! yay

Thanks for putting me on the right track!

B

unread,
Mar 15, 2011, 11:52:02 PM3/15/11
to autokey-users
Also note that str.split() splits any whitespace, so if your lines
have spaces in them, those will be split.

If you want to preserve spaces and only split on lines, use
string.split('\n')

cedardoc

unread,
Mar 15, 2011, 4:58:04 PM3/15/11
to autokey-users
addendum:

If you want it to split according to the text lines instead of each
single word you have to change

this:
contentsList = contents.split()

into this:
contentsList = contents.split('\n')
Reply all
Reply to author
Forward
0 new messages