Dive into Code

Discover code snippets, tutorials, and programming insights

Python

Python keyword generator function

<p>To create an SEO keyword generator function in Python, we&#39;ll need to use some natural language processing techniques to extract relevant keywords from the input text. In this case, we&#39;ll …

python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# keyword_generator/utils.py
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import PorterStemmer

nltk.download('punkt')
nltk.download('stopwords')

def generate_keywords(text):
    # Tokenize the text into words
    words = word_tokenize(text)

    # Remove stopwords (common words like "the", "is", "and", etc.)
    stop_words = set(stopwords.words('english'))
    words = [word.lower() for word in words if word.lower() not in stop_words]

    # Stemming the words (converting words to their root form)
    stemmer = PorterStemmer()
    stemmed_words = [stemmer.stem(word) for word in words]

    # Count word frequencies
    word_freq = {}
    for word in stemmed_words:
        word_freq[word] = word_freq.get(word, 0) + 1

    # Sort the words by frequency in descending order
    sorted_words = sorted(word_freq.items(), key=lambda x: x[1], reverse=True)

    # Return the top 10 keywords (you can adjust this number as needed)
    return [word for word, _ in sorted_words[:10]]
Python

Python requests check status code

<p>You can use the Python <code>requests</code> library to check if the status code of a URL is 200 without actually downloading the file content. When you make a request using …

python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import requests

def check_url_status(url):
    try:
        response = requests.get(url, stream=True)
        if response.status_code == 200:
            print("URL is accessible (status code 200).")
        else:
            print(f"URL is not accessible. Status code: {response.status_code}")
    except requests.exceptions.RequestException as e:
        print(f"Error: {e}")

# Example usage:
url_to_check = "https://example.com/somefile.txt"
check_url_status(url_to_check)
Postgresql

Hotel Management postgresql basic schema

<p>It is a basic&nbsp; database schema for managing information related to companies, hotels, rooms, features, attributes, customers, and reservations. The SQL script defines the table structure for each entity along …

python
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
create table company(
    id serial primary key not null,
    name varchar(200),
    image varchar(200) null,
    created_at timestamp not null default now(),
    updated_at timestamp not null default now(),
    "order" int not null default 0,
    is_published BOOLEAN NOT NULL default False,
    is_active BOOLEAN NOT NULL default True
);

create index  idx_company_order company("order");


create table hotel(
    id serial primary key not null,
    company_id int references company(id),
    name varchar(200),
    image varchar(200) null,
    created_at timestamp not null default now(),
    updated_at timestamp not null default now(),
    "order" int not null default 0,
    is_published BOOLEAN NOT NULL default False,
    is_active BOOLEAN NOT NULL default True
);

create index  idx_hotel_company_id hotel(company_id);
create index  idx_hotel_order hotel("order");


create table hotelimage(
    id serial primary key not null,
    hotel_id int references hotel(id),
    image varchar(200) null,
    created_at timestamp not null default now(),
    updated_at timestamp not null default now(),
    "order" int not null default 0,
    is_published BOOLEAN NOT NULL default False,
    is_active BOOLEAN NOT NULL default True
);

create index  idx_hotelimage_hotel_id hotel(hotel_id);
create index  idx_hotelimage_order hotelimage("order");


create table room(
    id serial primary key not null,
    hotel_id int references hotel(id),
    name varchar(200),
    image varchar(200) null,
    created_at timestamp not null default now(),
    updated_at timestamp not null default now(),
    "order" int not null default 0,
    is_published BOOLEAN NOT NULL default False,
    is_active BOOLEAN NOT NULL default True
);

create index  idx_room_hotel_id room(hotel_id);
create index  idx_room_order room("order");


create table roomimage(
    id serial primary key not null,
    room_id int references room(id),
    image varchar(200) null,
    created_at timestamp not null default now(),
    updated_at timestamp not null default now(),
    "order" int not null default 0,
    is_published BOOLEAN NOT NULL default False,
    is_active BOOLEAN NOT NULL default True
);

create index  idx_roomimage_room_id room(room_id);
create index  idx_roomimage_order roomimage("order");



create table feature(
    id serial primary key not null,
    company_id int references company(id),
    name varchar(200),
    image varchar(200) null,
    created_at timestamp not null default now(),
    updated_at timestamp not null default now(),
    "order" int not null default 0,
    is_published BOOLEAN NOT NULL default False,
    is_active BOOLEAN NOT NULL default True
);

create index  idx_feature_company_id feature(company_id);
create index  idx_feature_order feature("order");


create table attribute(
    id serial primary key not null,
    feature_id int references feature(id),
    name text,
    created_at timestamp not null default now(),
    updated_at timestamp not null default now(),
    "order" int not null default 0
);


create index  idx_attribute_feature_id attribute(feature_id);
create index  idx_attribute_order attribute("order");



create table roomfeature(
    id serial primary key not null,
    room_id int references room(id),
    feature_id int references feature(id),
    created_at timestamp not null default now(),
    updated_at timestamp not null default now(),
    "order" int not null default 0,
);

create index  idx_roomfeature_feature_id roomfeature(feature_id);
create index  idx_roomfeature_room_id roomfeature(room_id);
create unique index idx_roomfeature_room_feature roomfeature(feature_id,room_id)
create index  idx_roomfeature_order roomfeature("order");



create table roomattribute(
    id serial primary key not null,
    room_id int references room(id),
    attribute_id int references attribute(id),
    created_at timestamp not null default now(),
    updated_at timestamp not null default now(),
    "order" int not null default 0,
);

create index  idx_roomattribute_attribute_id roomattribute(attribute_id);
create index  idx_roomattribute_room_id roomattribute(room_id);
create unique index idx_roomattribute_room_attribute roomattribute(attribute_id,room_id)
create index  idx_roomattribute_order roomattribute("order");



create table customer(
    id serial primary key not null,
    name varchar(200),
    surname varchar(200),
    email varchar(200),
    created_at timestamp not null default now(),
    updated_at timestamp not null default now(),
);


create table reservation(
    id serial primary key not null,
    room_id int references room(id),
    customer_id int references customer(id),
    start_date DATE NOT NULL,
    end_date DATE NOT NULL,
    created_at timestamp not null default now(),
    updated_at timestamp not null default now(),

);

create index  idx_reservation_customer_id reservation(customer_id);
create index  idx_reservation_room_id reservation(room_id);
create index  idx_reservation_start_date reservation(start_date);
create index  idx_reservation_end_date reservation(end_date);