Re: [Tutor] path

2019-06-30 Thread Mats Wichmann
On 6/30/19 12:01 AM, ingo wrote:
> 
> 
> On 29-6-2019 15:42, Mats Wichmann wrote:
>>
>> Most people don't use pathlib, and that's kind of sad, since it tries to
>> mitigate the kinds of questions you just asked. Kudos for trying.
> 
> In the end, it works,

Sounds good.  One suggestion - a sort of general programming suggestion
really - whenever you take user input, do some kind of validation on it
before using it.  The context may not be one where anything malicious
could happen, but still, just to catch errors.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] path

2019-06-30 Thread ingo



On 29-6-2019 15:42, Mats Wichmann wrote:
> 
> Most people don't use pathlib, and that's kind of sad, since it tries to
> mitigate the kinds of questions you just asked. Kudos for trying.

In the end, it works,

Ingo

---%<--%<--%<---
# set up some default directories and files
# for starting a new project with SQLite
# using Sublime + SQLTools.
#
# /---fullpath
# |
# /--- data
# /--- db
# |+--- basename.db3
# |+--- basename.read
# /--- ddl
# /--- doc
# /--- sql
# +--- basename.sublime-project
# +--- _FOSSIL_

import pathlib
import sys

# the last bit of the full path is used as the name for the database
# c:\newdatabase\test will create the databse
# c:\newdatabase\test\db\test.db3
print('enter full path for new db:')
fp = pathlib.Path(input())
fn = fp.name  # = os.path.basename()

dirs = {}
for sub in ['', 'data', 'db', 'ddl', 'doc', 'sql']:
dirs[sub] = fp / sub
try:
dirs[sub].mkdir(parents=False, exist_ok=False)
except FileExistsError:
print(f'Directory already exists: {dirs[sub]}')
sys.exit(1)

fdb = dirs['db'] / (fn+'.db3')
fdb.touch()

fr = dirs['db'] / (fn+'.read')
fr.write_text(f"""
-- template to build db from tables etc
-- using dot commands

PRAGMA foreign_keys = OFF;
--DROP TABLE IF EXISTS sometable;

BEGIN TRANSACTION;
--.read {(dirs['ddl'] / 'someddl.ddl').as_posix()}
COMMIT;

PRAGMA temp_store   = 2;
PRAGMA foreign_keys = ON;
PRAGMA journal_mode = WAL;

BEGIN TRANSACTION;
--.read {(dirs['sql'] / 'somequery.sql').as_posix()}
COMMIT;
"""
)

fsub = dirs[''] / (fn+'.sublime-project')
fsub.write_text(f'''
{{
  "folders":[
{{
  "path": "."
}}
  ],
  "connections":{{
"Connection SQLite":{{
  "database": "{fdb.as_posix()}",
  "encoding": "utf-8",
  "type": "sqlite"
}}
  }},
  "default": "Connection SQLite"
}}'''
)

# TODO set up fossil in the fp dir


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor