Compare commits

..

No commits in common. "main" and "main" have entirely different histories.
main ... main

5 changed files with 27 additions and 114 deletions

114
README.md
View file

@ -1,47 +1,32 @@
# db-prawo-jazdy
# prawo-jazdy-resources
### Slightly modified version of [prawo-jazdy-resources](https://git.mandarynki.eu/maciej/prawo-jazdy-resources) by [maciej](https://git.mandarynki.eu/maciej), to suit the needs of [nuxt-prawo-jazdy](https://git.mandarynki.eu/netman/nuxt-prawo-jazdy)
### Files summary
`main.py` is an _extremely_ badly-written script that reads files (`pytania.csv` and `punkty.csv`) shared by the Ministry of Infrastructure and imports them into a postgresql database
- `db/pytania.csv` is the `katalog_pytania_egzminacyjne_kandydat__14112024.xlsx` file from [here](https://www.gov.pl/web/infrastruktura/prawo-jazdy)
- `db/punkty.csv` isn't published by the Ministry, so you have to send a public information request to acquire it - we have received an XLSX
both of them are converted to CSV
# Requirements
- postgres server
- python3
- packages from requirements.txt (`pip install -r requirements.txt`)
- files above (either already CSV'd from this repo or converted by yourself from official, most probably fresher sources)
### Database structure
`db/main.py` is an *extremely* badly-written script that reads files (`pytania.csv` and `punkty.csv`) published by the Ministry of Infrastructure and imports them into a database
- `db/pytania.csv` is the `katalog_pytania_egzminacyjne_kandydat__14112024.xlsx` file from [here](https://www.gov.pl/web/infrastruktura/prawo-jazdy), opened in Libreoffice Calc and saved as csv
- `db/punkty.csv` isn't published by the Ministry, so you have to send a freedom of information request to acquire it; it is processed the same way as `pytania.csv`
The database structure is as follows:
```sql
CREATE TABLE IF NOT EXISTS tasks (
CREATE TABLE tasks (
id integer NOT NULL,
correct_answer character(5),
correct_answer boolean,
media_url text,
weight smallint
);
CREATE TABLE IF NOT EXISTS questions (
CREATE TABLE questions (
task_id integer,
lang character(2),
text text
);
CREATE TABLE IF NOT EXISTS tasks_advanced (
CREATE TABLE tasks_advanced (
id integer NOT NULL,
correct_answer character(1),
media_url text,
weight smallint
);
CREATE TABLE IF NOT EXISTS questions_advanced (
CREATE TABLE questions_advanced (
task_id integer,
lang character(2),
text text,
@ -50,91 +35,34 @@ CREATE TABLE IF NOT EXISTS questions_advanced (
answer_c text
);
CREATE TABLE IF NOT EXISTS categories (
CREATE TABLE categories (
name text,
task_id integer
);
```
## Criteria
Categories are as such:
- A
- B
- C
- D
- T
- AM
- A1
- A2
- B1
- C1
- D1
- PT
Languages as such:
- PL
- EN
- UA
- DE
# Basic tasks query
According to the Ministry of Infrastructure, in an exam there are (basic):
- 4 questions for 1 point
- 6 questions for 2 points
- 10 questions for 3 points
so NO_OF_QUESTIONS and WEIGHT_OF_QUESTIONS should be set accordingly to the requirements above (and of course set category & lang to your liking)
The basic tasks can be queried like so:
```sql
SELECT
tasks.id,
tasks.correct_answer,
tasks.media_url,
tasks.weight,
questions.text
SELECT tasks.correct_answer, tasks.media_url, tasks.weight, questions.text
FROM tasks
LEFT JOIN questions ON tasks.id = questions.task_id
LEFT JOIN categories ON tasks.id = categories.task_id
WHERE
categories.name = 'B' AND
questions.lang = 'PL' AND
tasks.weight = WEIGHT_OF_QUESTIONS
questions.lang = 'PL'
ORDER BY random()
LIMIT (NO_OF_QUESTIONS);
LIMIT (20);
```
# Advanced tasks query
According to the Ministry of Infrastructure, in an exam there are (advanced):
- 2 questions for 1 point
- 4 questions for 2 points
- 6 questions for 3 points
so NO_OF_QUESTIONS and WEIGHT_OF_QUESTIONS should be set accordingly to the requirements above (and of course set category & lang to your liking)
And the advanced tasks like this:
```sql
SELECT
tasks_advanced.id,
tasks_advanced.correct_answer,
tasks_advanced.media_url,
tasks_advanced.weight,
questions_advanced.text,
questions_advanced.answer_a,
questions_advanced.answer_b,
questions_advanced.answer_c
SELECT tasks_advanced.correct_answer, tasks_advanced.media_url, tasks_advanced.weight,
questions_advanced.text, questions_advanced.answer_a, questions_advanced.answer_b, questions_advanced.answer_c
FROM tasks_advanced
LEFT JOIN questions_advanced ON tasks_advanced.id = questions_advanced.task_id
LEFT JOIN categories ON tasks_advanced.id = categories.task_id
WHERE
categories.name = 'B' AND
questions_advanced.lang = 'PL' AND
tasks_advanced.weight = WEIGHT_OF_QUESTIONS
questions_advanced.lang = 'PL'
ORDER BY random()
LIMIT (NO_OF_QUESTIONS);
```
LIMIT (20);
```

View file

@ -1,21 +1,10 @@
import csv, psycopg2, pwinput
import csv
from tqdm import tqdm
import psycopg2
host = input('host: ')
user = input('user: ')
password = pwinput.pwinput(prompt='password: ')
dbname = input('dbname: ')
port = int(input('port: '))
conn = psycopg2.connect(host=host, user=user, password=password, dbname=dbname, port=port)
conn = psycopg2.connect('host=10.16.32.107 user=maciej password=maciej dbname=egzamin')
cur = conn.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS tasks (id integer NOT NULL, correct_answer character(5), media_url text, weight smallint);')
cur.execute('CREATE TABLE IF NOT EXISTS questions (task_id integer, lang character(2), text text);')
cur.execute('CREATE TABLE IF NOT EXISTS tasks_advanced (id integer NOT NULL, correct_answer character(1), media_url text, weight smallint);')
cur.execute('CREATE TABLE IF NOT EXISTS questions_advanced (task_id integer, lang character(2), text text, answer_a text, answer_b text, answer_c text);')
cur.execute('CREATE TABLE IF NOT EXISTS categories (name text, task_id integer);')
cur.execute('TRUNCATE TABLE tasks;')
cur.execute('TRUNCATE TABLE questions;')
cur.execute('TRUNCATE TABLE categories;')
@ -55,11 +44,11 @@ with open('pytania.csv') as file:
if numer and pytanie_pl and poprawna_odp and kategorie and not odp_a_pl and not odp_b_pl and not odp_c_pl:
if poprawna_odp == 'Tak':
correct_answer = 'true'
correct_answer = True
elif poprawna_odp == 'Nie':
correct_answer = 'false'
correct_answer = False
else:
raise Exception('Error, an answer to a basic question does not equal "Tak" or "Nie", check the pytania.csv file for anomaly in poprawna_odp section')
raise Exception('zjebało się')
cur.execute('INSERT INTO tasks (id, correct_answer, media_url) VALUES (%s, %s, %s)',
(task_id, correct_answer, media))

View file

Can't render this file because it is too large.

View file

@ -1,4 +0,0 @@
tqdm
psycopg2-binary
pwinput
csv