Conditions | 4 |
Total Lines | 67 |
Code Lines | 20 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import os |
||
7 | def create_tables(): |
||
8 | """create tables in postgresql database""" |
||
9 | commands = ( |
||
10 | """ |
||
11 | DROP TABLE IF EXISTS tb_users; |
||
12 | CREATE TABLE tb_users( |
||
13 | user_id SERIAL PRIMARY KEY, |
||
14 | username VARCHAR(100) UNIQUE NOT NULL, |
||
15 | firstname VARCHAR(50) NOT NULL, |
||
16 | lastname VARCHAR(50) NOT NULL, |
||
17 | password VARCHAR(255) NOT NULL, |
||
18 | created_on TIMESTAMP NOT NULL, |
||
19 | last_login TIMESTAMP |
||
20 | ) |
||
21 | """ |
||
22 | , |
||
23 | """ |
||
24 | DROP TABLE IF EXISTS tb_request; |
||
25 | CREATE TABLE tb_request( |
||
26 | request_id SERIAL PRIMARY KEY, |
||
27 | requestor VARCHAR(255) NOT NULL, |
||
28 | email VARCHAR(100) NOT NULL, |
||
29 | type VARCHAR(50) NOT NULL, |
||
30 | status VARCHAR(50) NOT NULL, |
||
31 | description TEXT, |
||
32 | created_on TIMESTAMP NOT NULL |
||
33 | ) |
||
34 | """ |
||
35 | , |
||
36 | """ |
||
37 | DROP TABLE IF EXISTS tb_admin; |
||
38 | CREATE TABLE tb_admin( |
||
39 | admin_id SERIAL PRIMARY KEY, |
||
40 | username VARCHAR(100) UNIQUE NOT NULL, |
||
41 | fullname VARCHAR(100) NOT NULL, |
||
42 | password VARCHAR(255) NOT NULL, |
||
43 | created_on TIMESTAMP NOT NULL, |
||
44 | last_login TIMESTAMP |
||
45 | ) |
||
46 | """ |
||
47 | ) |
||
48 | |||
49 | conn = None |
||
50 | try: |
||
51 | params = dbconfig(filename, section) |
||
52 | conn = psycopg2.connect(**params) |
||
53 | |||
54 | #create cursor |
||
55 | cur = conn.cursor() |
||
56 | |||
57 | #execute statement |
||
58 | for command in commands: |
||
59 | cur.execute(command) |
||
60 | |||
61 | cur.close() |
||
62 | |||
63 | #commit the changes |
||
64 | conn.commit() |
||
65 | print("Tables Created Succesfully") |
||
66 | |||
67 | except (Exception, psycopg2.DatabaseError) as error: |
||
68 | print(error) |
||
69 | finally: |
||
70 | if conn is not None: |
||
71 | conn.close() |
||
72 | |||
73 | return "Tables user, request and admin created succesfully" |
||
74 | |||
77 | create_tables() |