| Conditions | 4 | 
| Total Lines | 66 | 
| Code Lines | 18 | 
| Lines | 66 | 
| Ratio | 100 % | 
| 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 | ||
| 6 | View Code Duplication | def create_tables(): | |
|  | |||
| 7 | """create tables in postgresql database""" | ||
| 8 | commands = ( | ||
| 9 | """ | ||
| 10 | DROP TABLE IF EXISTS tb_users; | ||
| 11 | CREATE TABLE tb_users( | ||
| 12 | user_id SERIAL PRIMARY KEY, | ||
| 13 | username VARCHAR(100) UNIQUE NOT NULL, | ||
| 14 | firstname VARCHAR(50) NOT NULL, | ||
| 15 | lastname VARCHAR(50) NOT NULL, | ||
| 16 | password VARCHAR(255) NOT NULL, | ||
| 17 | created_on TIMESTAMP NOT NULL, | ||
| 18 | last_login TIMESTAMP | ||
| 19 | ) | ||
| 20 | """ | ||
| 21 | , | ||
| 22 | """ | ||
| 23 | DROP TABLE IF EXISTS tb_request; | ||
| 24 | CREATE TABLE tb_request( | ||
| 25 | request_id SERIAL PRIMARY KEY, | ||
| 26 | requestor VARCHAR(255) NOT NULL, | ||
| 27 | email VARCHAR(100) NOT NULL, | ||
| 28 | type VARCHAR(50) NOT NULL, | ||
| 29 | status VARCHAR(50) NOT NULL, | ||
| 30 | description TEXT, | ||
| 31 | created_on TIMESTAMP NOT NULL | ||
| 32 | ) | ||
| 33 | """ | ||
| 34 | , | ||
| 35 | """ | ||
| 36 | DROP TABLE IF EXISTS tb_admin; | ||
| 37 | CREATE TABLE tb_admin( | ||
| 38 | admin_id SERIAL PRIMARY KEY, | ||
| 39 | username VARCHAR(100) UNIQUE NOT NULL, | ||
| 40 | fullname VARCHAR(100) NOT NULL, | ||
| 41 | password VARCHAR(255) NOT NULL, | ||
| 42 | created_on TIMESTAMP NOT NULL, | ||
| 43 | last_login TIMESTAMP | ||
| 44 | ) | ||
| 45 | """ | ||
| 46 | ) | ||
| 47 | |||
| 48 | conn = None | ||
| 49 | try: | ||
| 50 | |||
| 51 | conn = psycopg2.connect(dbname='test_db',user="antonio", password="pass.123") | ||
| 52 | |||
| 53 | #create cursor | ||
| 54 | cur = conn.cursor() | ||
| 55 | |||
| 56 | #execute statement | ||
| 57 | for command in commands: | ||
| 58 | cur.execute(command) | ||
| 59 | |||
| 60 | cur.close() | ||
| 61 | |||
| 62 | #commit the changes | ||
| 63 | conn.commit() | ||
| 64 | |||
| 65 | except (Exception, psycopg2.DatabaseError) as error: | ||
| 66 | print(error) | ||
| 67 | finally: | ||
| 68 | if conn is not None: | ||
| 69 | conn.close() | ||
| 70 | |||
| 71 | return "Tables user, request and admin created succesfully" | ||
| 72 | |||
| 75 | create_tables() |