-
-
Save Phuket2/89535a038f5d954d5c6a to your computer and use it in GitHub Desktop.
# coding: utf-8 | |
# *** DISCLAIMER PLEASE READ *** | |
# please understand, i am a newbie. being a newbie, | |
# my code may be crap or worse buggy | |
# i still like to share. it could be useful code. | |
# or parts of could be useful | |
# but i like the feedback | |
''' | |
*** iOS, Pythonista Script 1.5/1.6 *** | |
Pythonista by omz:software | |
https://appsto.re/au/P0xGF.i | |
NOTES: | |
meant to be run from the tools menu.... | |
the idea is to create a .py file | |
fast in a known location. | |
You can use **my_std_script** var to write some code to the | |
file if you like. | |
LOL , it would be so nice if markdown worked | |
inside python comments :) | |
REASON: | |
So often i want to test something in a new .py, | |
i often create a new file in the project dir i am | |
wirking on, called crap.py or test.py or something. | |
Pollutes my project directory. | |
This is only for those .py files you want to create, | |
use and forget. | |
Some point you should go back into the target directory all delete | |
its contents. the temp file created will not be automactially | |
deleted! | |
''' | |
import os, sys | |
import tempfile | |
import editor, console | |
# assumes this directory will reside in | |
# the site_packages directory. | |
_temp_dir_name = 'TestFiles' | |
# the base dir where the temp_dir_name is | |
# created if it does exist... | |
# you will be prompted, before a dir is created | |
_base_dir = '{}/Documents/{}'.format(os.path.expanduser('~'), | |
'site-packages') | |
# my_std_script, is written to the new file | |
# can also set my_std_script = '', nothing will be | |
# written to the file | |
my_std_script =''' | |
# coding: utf-8 | |
import ui | |
class MyClass(object): | |
def __init__(self): | |
pass | |
if __name__ == '__main__': | |
print 'in main...' | |
''' | |
# uncomment the line below, if you want nothing written | |
# to the temp file... | |
#my_std_script = '' | |
_target_dir = '''{0}/{1}/'''.format(_base_dir, _temp_dir_name) | |
# make sure the target dir exists | |
if not os.path.isdir(_target_dir): | |
result = console.alert(title = 'Directory Does Not Exist', | |
message = 'the target_dir, does not exist!', | |
button1 = 'Make Dir') | |
# Cancel button exits, so no check required. | |
try: | |
os.mkdir(_target_dir) | |
except OSError as err: | |
raise err | |
try: | |
with tempfile.NamedTemporaryFile(mode = 'w', | |
dir = _target_dir, | |
suffix = '.py', | |
delete = False) as f: | |
f.write(my_std_script) | |
name = f.name | |
except Exception as err: | |
raise Exception(err.message, err.args) | |
editor.open_file(name) | |
I do agree with the explicit rather than implicit. However I believe it unnecessary to capture an error if there is nothing to be done with the captured error especially if it is to be raised anyway. I see why you would put in the try except
to show that there could be an error there, however I personally believe that it would be better commenting that there is an error over putting a middle man
so to say into the equation when nothing is being done.
In others words what is the point of capturing an error to raise it anyway?
I apologise if I seem blunt or harsh that is not my intention. I just like knowing the reasons behind actions and tend to not 'sugar coat' (for want of a better word) any question I ask.
Useful links:
https://docs.python.org/2/tutorial/errors.html
https://realpython.com/blog/python/the-most-diabolical-python-antipattern/
https://docs.python.org/2/howto/doanddont.html#except
Cethric, don't worry about being blunt. It's the way I like it. I am trying to learn, I am happy you have taken the time out to and give feedback as well as some resources. I really appreciate it.
I have been using gists because I really struggle with repo's. I still do. But to get serious about people's comments and input I just have to bite the bullet and get over it. I have moved this gist to https://github.com/Phuket2/Pythonista/tree/master/Tools%20Menu
I am writing something else at the moment for paths, and have stopped manually raising the error
But again, thanks for taking the time. Feel free to blunt with me anytime, I love the feedback
I have moved this gist into https://github.com/Phuket2/Pythonista/tree/master/Tools%20Menu
Will stop updating this version....
Cethric, I am still learning. I prefer your 2nd suggestion over the first. Just so you can see there is acknowledgement of the possibility of an error. I have read about the different ideas on this from Python programmers. I am more in the camp of being explicit rather than implicit. But as I say, I am still learning, maybe my views will change along the way. In actual fact, I think the error should not just raise an error. It should be handled better than what I did.