Last active
December 9, 2015 08:03
-
-
Save Phuket2/89535a038f5d954d5c6a to your computer and use it in GitHub Desktop.
CreateTestFile.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have moved this gist into https://github.com/Phuket2/Pythonista/tree/master/Tools%20Menu
Will stop updating this version....