I have a date as a string in the form „dd/mm/yyyy“ and need to convert it to the form „dd. MMMM yyyy“ so that the verbose month is printed out in German.
Now, because we still have the locale bug in Pythonista ...
>>> import locale >>> locale.setlocale(locale.LC_ALL, 'de') Traceback (most recent call last): File "<string>", line 1, in <module> File "/var/containers/Bundle/Application/4D653312-79ED-4473-B63A-522E9EA8C250/Pythonista3.app/Frameworks/Py3Kit.framework/pylib/locale.py", line 599, in setlocale return _setlocale(category, locale) locale.Error: unsupported locale setting >>> locale.setlocale(locale.LC_ALL, 'de_DE') Traceback (most recent call last): File "<string>", line 1, in <module> File "/var/containers/Bundle/Application/4D653312-79ED-4473-B63A-522E9EA8C250/Pythonista3.app/Frameworks/Py3Kit.framework/pylib/locale.py", line 599, in setlocale return _setlocale(category, locale) locale.Error: unsupported locale setting
... I guess I‘m forced to find a workaround. So I opted for the
objc_utilmodule and tried this (variable
gebDatumholds the initial date string in the above mentioned format):
from objc_util import * NSDateFormatter = ObjCClass('NSDateFormatter') dateFormatter = NSDateFormatter.alloc().init() NSLocale = ObjCClass('NSLocale') deLocale = NSLocale.alloc().initWithLocaleIdentifier_(ns('de')) dateFormatter.setLocale_(deLocale) NSDate = ObjCClass('NSDate') date = NSDate.alloc().init() dateFormatter.setDateFormat_('dd/MM/yyyy') date = dateFormatter.dateFromString_(gebDatum) dateFormatter.setDateFormat_('dd. MMMM yyyy') gebDatumVerbose = dateFormatter.stringFromDate_(date)
Now when I want to print out the variable
gebDatumVerboseI get the error
must be str, not ObjCInstance.
Any help / hint / explanation / other (shorter) solution for my problem?
Thanks a lot!
Never ever would I've been able to write such a line like you did in the
trystatment! I have no idea why & how this syntax works, that remains as a brain teaser for the weekend! 😉
Thanks so much for this quick help, Ole!! SO much appreciated! (Just a mini error: the parameter in the
create_image_asset()call has to be
However, I discovered a time stamp problem: I just saved an image to an album. When I did this the time was 03.03.2017, 20:07. When I launched the Photos app and opened my album I saw the one image in it but Photos said the image was dated from yesterday (02.03.2017) and from 22:12 ... 🤔
Anyone any idea why this might be the case (and how to fix it)?
I'd like to kindly ask for a few lines of sample code. I want to achieve this:
I have a custom ui.View where I draw things to.
I want to save the contents of this view as a PNG to my own, new album inside the device's Photos library.
The next time the user wants to save an image it should go into the – now existing – album.
So I assume it goes like this:
- Check if the album "my album" exists
- If it does not exist, create it (photos.create_album())
- Create the PNG image somehow from the ui.View contents (found this, works, writes a file local to Pythonista's sandboxed file system)
- Save this file into the album "my album" using AssetCollection.add_assets() somehow
- Do I need to create this temporary PNG file?
- How do I check if an album exists? get_albums() only gives all albums. 😒
- How, exactly, do I add an image into "my album"?
Being still a Python starter it would be great if someone could help!
Thanks a lot!
Got a ui View.
It contains a TableView and nothing else.
The view gets presented via
Immediately after presenting it I wait via
Now the user should tap on one item in the TableView and the view should close.
The closing of the view works (
tableview.superview.close()) but I don't know how to transport the information about the tapped row back to the main thread so that I can work with it after the
This is my delegate (with debug output):
class MyTableViewDelegate (object): def tableview_did_select(self, tableview, section, row): # Called when a row was selected. print("tableview_did_select: row=", row) print(tableview.data_source.items[row]) tableview.superview.close() pass
Any help regarding how to get the
tableview.data_source.items[row]information out of this delegate method back to my main program would be awesome! 😀
still a beginner at Python, so please be patient.
I'm using the twitter.request function for a Twitter API request.
The result is a tuple consisting of a return code and the actual JSON result.
I want to get hold of that JSON result like this:
if status == 200: result = json.loads(data)
However, I get this error:
Traceback (most recent call last):
File "/private/var/mobile/Containers/Shared/AppGroup/9D2704E0-434A-4879-8FB0-30594FA3A28D/Pythonista3/Documents/addToCollectionIK.py", line 17, in <module>
result = json.loads(data)
File "/var/containers/Bundle/Application/B09EF8B7-CE0F-4572-9748-EC15615DEA7A/Pythonista3.app/Frameworks/PythonistaKit3.framework/pylib/json/init.py", line 312, in loads
TypeError: the JSON object must be str, not 'bytes'
Why is that and what can I do about it? From what I understand the twitter.request function doesn't seem to return the JSON object as a string but as 'bytes' (what's that?). So I need to make a string out of that bytes, right? But how?
(btw, the json.load(data) function doesn't work, either ...)
Thanks a lot,