-
roosterboy197
I don't understand what's going on here so hopefully one of you can enlighten me.
I subscribe to the RSS feed for http://kupu.maori.nz in MrReader and have a service set up to send a post to a Pythonista script to extract the word of the day and append it to a text file in my Dropbox. But every time it runs, the file shrinks by a number of bytes, usually between 2 and 25. I can't see any correlation between the size of the appended string and the size of the shrinkage.
What am I doing wrong? I'm betting it's something simple I've missed but I dunno.
#coding: utf-8 import sys import dropboxlogin from dropbox import rest import console import locale import webbrowser dropboxlogin.app_key = '...' dropboxlogin.app_secret = '...' DB_FOLDER = '/flashcards/' DB_FILE = 'Māori.cards.txt' def main(): locale.setlocale(locale.LC_ALL, '') #extract the word of the day from the RSS text rss = sys.argv[1] new_word = rss.split('.')[0] new_word = new_word.replace(': ', ' :: ') #this gives us text like "ngeru :: cat" try: db = dropboxlogin.get_client() ff, md = db.get_file_and_metadata(DB_FOLDER + DB_FILE) wordlist = ff.read().decode('utf-8').splitlines() ff.close() print(md) #to check our starting size wordlist.append(new_word) wordlist = list(set(wordlist)) wordlist.sort(cmp=locale.strcoll) md = db.put_file(DB_FOLDER + DB_FILE, u'\n'.join(wordlist), overwrite=True) print(md) #to see how much we've shrunk console.hud_alert('added {}'.format(new_word)) except rest.ErrorResponse as e: console.alert('Error - add_maori.py', message='{}\n'.format(e)) webbrowser.open('mrreader://') if __name__ == '__main__': main()
-
roosterboy197
And that's why I shouldn't code first thing out of bed in the morning. Of course it's doing that; I'm reusing the same input each time so changing it to a set strips out the dupe. Fixing that gives me this output:
# "12hgtjfj :: 34hytgfrd" ('bytes - before', 1048) ('before', 56, 999) ('after', 56, 999) ('sorted', 56, 999) ('bytes - after', 1054) # 21 characters, 6 bytes
So it's not the sort.
But I just noticed that my byte count increased this time!
-
roosterboy197
Yeah, now I'm really confused. I woke up this morning thinking "hmm, maybe it's the sort", added in just one print statement and now my output looks like this:
# "hgtjfj :: hytgfrd" ('bytes - before', 1122) ('before', 59, 1062) ('after', 58, 1045) ('sorted', 58, 1045) ('bytes - after', 1102) # 17 characters, -20 bytes # "hgtjfj :: hytgfrd" ('bytes - before', 1102) ('before', 58, 1044) ('after', 57, 1027) ('sorted', 57, 1027) ('bytes - after', 1083) # 17 characters, -19 bytes # "hgtjfj :: hytgfrd" ('bytes - before', 1083) ('before', 57, 1027) ('after', 56, 1010) ('sorted', 56, 1010) ('bytes - after', 1065) # 17 characters, -18 bytes # "hgtjfj :: hytgfrd" ('bytes - before', 1065) ('before', 57, 1010) ('after', 56, 993) ('sorted', 56, 993) ('bytes - after', 1048) # 17 characters, -17 bytes
Now the
wordlist = list(set(wordlist))
line is removing data! And it looks like the byte count of the lost data is dropping by one each time. I just don't understand. -
roosterboy197
Here are the results of running that several times, with the string I added and the bytes returned from the Dropbox metadata.
# "whatitoka :: door" ('bytes - before', 1157) ('before', 60, 1091) ('after', 60, 1091) ('bytes - after', 1150) # 17 characters, -7 bytes # "awa :: river" ('bytes - before', 1150) ('before', 60, 1083) ('after', 60, 1083) ('bytes - after', 1142) # 12 characters, -8 bytes # "hapa :: dinner" ('bytes - before', 1142) ('before', 60, 1078) ('after', 60, 1078) ('bytes - after', 1137) # 14 characters, -5 bytes # "āporo :: apple" ('bytes - before', 1137) ('before', 60, 1073) ('after', 60, 1073) ('bytes - after', 1132) # 14 characters, -5 bytes # "hgtj :: gfrd" - random characters ('bytes - before', 1132) ('before', 60, 1066) ('after', 60, 1066) ('bytes - after', 1125) # 12 characters, -7 bytes # "hgtjfj :: hytgfrd" - random characters ('bytes - before', 1125) ('before', 59, 1064) ('after', 59, 1064) ('bytes - after', 1122) # 17 characters, -3 bytes
I can't really see any pattern here. After seeing the byte difference was -5 for both 14-character strings, I tested using same length strings as earlier examples (17 and 12) but got different results.
-
roosterboy197
I find it strange that there's no workflow action to list files in a local or dropbox folder and really wish a future version of Editorial would get one.
Also, the ability to specify a directory in the Sync with Dropbox action so that the whole thing isn't synced seems like a good thing to have.
In the meantime, any suggestions for how to accomplish at least the first of these two things? I've managed to hack together a solution that works but it makes me nervous relying on something like this:
import os import editor import glob import workflow db_folder = workflow.get_variable('foldername') db_path = os.path.split(editor.get_workflows_path())[0] + '/Dropbox/{}/*.*'.format(db_folder) files = glob.glob(db_path) workflow.set_variable('filelist', '\n'.join(files))
Has anyone figured out a better way to list all the files in a Dropbox folder (and a local folder, too, while we're at it)?
-
roosterboy197
Trying to add a fancy title to a UI button using Unicode characters.
vw['btn_underline'].title = u'U\u0332'
works and gives me a button with an underlined capital U.vw['btn_italic'].title = u'\u1D644'
doesn't work. It should give the character MATHEMATICAL SANS-SERIF BOLD ITALIC CAPITAL I. Instead, it shows Unicode character \u1D64 (LATIN SUBSCRIPT SMALL LETTER U) followed by the number 4.What's up with that?
Hmm, picture doesn't show? Here's the link: https://www.dropbox.com/s/9plukw5hloyzz25/unicode_buttons.jpg
-
roosterboy197
Oh, of course! I don't know why I didn't think of that myself. That'll work just fine. Thanks!
-
roosterboy197
Well, that's disappointing. Though I guess it is too much to ask for every single obscure Unicode character to be available in the system font. How often are some of them going to be used?
So, any suggestion for a workaround?
-
roosterboy197
Yeah, the actual listing of the files isn't so much the issue as it is getting the path to the folder. Using
workflow.get_workflows_path()
and then figuring out the path to the Dropbox folders by manipulating the path string seems really hacky to me and fraught with danger.For the purposes of this post, I just kept it to files only, non-recursive. I've since created a custom action that looks at either a Dropbox or Local folder, recursively walks the hierarchy and returns the resulting list as a JSON-formatted string. But the initial issue still remains.
-
roosterboy197
I have a custom UI I'm presenting in a Python script action. My custom view class has a
will_close()
handler where I want to save off some values to two workflow variables I've set up in previous steps. Here's my handler:def will_close(self): clist = u'\n'.join(u'{} :: {}'.format(_, self.wordlist[_]) for _ in self.correct_words) wlist = u'\n'.join(u'{} :: {}'.format(_, self.wordlist[_]) for _ in self.wrong_words) workflow.set_variable('correct_words', clist) workflow.set_variable('wrong_words', wlist)
The values of
correct_words
andwrong_words
are output using Set File Contents actions to two files in Dropbox, creating them if they don't already exist. The files are being created as they should, but they are empty!If I add some
print
statements to mywill_close()
handler, I can see in the console thatclist
andwlist
contain exactly what they should contain.If I pause the workflow at the first Set File Contents action, I can see that the variable
correct_words
contains no value.Any suggestions?