Skip to content

Commit 5c6ecfd

Browse files
committed
Version 0.2.0
1 parent c91b8a3 commit 5c6ecfd

File tree

4 files changed

+117
-160
lines changed

4 files changed

+117
-160
lines changed

README.md

Lines changed: 96 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,115 @@ $ pip install loaf
1313

1414

1515

16-
## Sample Demo
16+
## Examples
17+
18+
### Importing Into Your Project
1719

1820
```python
19-
import loaf
21+
from loaf import Loaf
22+
```
23+
24+
25+
26+
### Setting Up Credentials
2027

28+
```python
2129
# Setup your credentials with a single line.
22-
loaf.bake(port=6969, db="pizzeria")
30+
loaf = Loaf(port=6969, db="pizzeria")
31+
# Or load your credentials from a file.
32+
loaf = Loaf(file="creds.ini")
33+
# Or use a local SQLite file instead.
34+
loaf = Loaf(file="pizzeria.db")
35+
```
36+
37+
38+
39+
### Executing Queries
40+
41+
```python
42+
# Make queries easily.
43+
toppings = loaf.query("SELECT * from toppings")
44+
# Load your quieries directly from files.
45+
clients = loaf.query(file="getHappyClients.sql")
46+
# Prevent disasters by executing multiple queries.
47+
pepperoni_id, client_name = loaf.multi([
48+
"SELECT id FROM toppings WHERE name='Pepperoni'",
49+
"SELECT name FROM clients WHERE id=6"
50+
])
51+
```
52+
53+
54+
55+
### Printing
56+
57+
```python
58+
# Display info using built-in tables!
59+
loaf.print(pepperoni_id)
60+
loaf.print(client_name)
61+
loaf.print(toppings)
62+
```
63+
64+
```powershell
65+
┏━━━━┓
66+
┃ id ┃
67+
┡━━━━┩
68+
│ 1 │
69+
└────┘
70+
┏━━━━━━━━━━━┓
71+
┃ name ┃
72+
┡━━━━━━━━━━━┩
73+
│ 'Alfonso' │
74+
└───────────┘
75+
┏━━━━┳━━━━━━━━━━━━━┳━━━━━━━┓
76+
┃ id ┃ name ┃ price ┃
77+
┡━━━━╇━━━━━━━━━━━━━╇━━━━━━━┩
78+
│ 1 │ 'Pepperoni' │ 1.49 │
79+
│ 2 │ 'Mushrooms' │ 1.99 │
80+
│ 3 │ 'Onions' │ 0.99 │
81+
└────┴─────────────┴───────┘
82+
```
83+
84+
85+
86+
### Data Manipulation
87+
88+
```python
89+
# Manipulate your data with dictionaries, as God intended.
90+
for topping in toppings:
91+
print(topping['name'])
92+
```
93+
94+
````powershell
95+
Pepperoni
96+
Mushrooms
97+
Onions
98+
````
99+
100+
23101

24-
# Make a query easily.
25-
result = loaf.query("SELECT * from toppings")
26-
print(result)
102+
### Utilities
27103

104+
```python
28105
# Not lazy enough? Try some of the pre-built queires.
106+
# Equivalent of: SELECT name FROM client WHERE name='Marco' LIMIT 1
107+
result = loaf.select("name", "clients", "name='Marco'", limit=1)
108+
# Get all values from a table.
29109
result = loaf.all("toppings")
30-
print(result)
31-
32110
# Got stored procedures? No problemo!
33111
result = loaf.call("ProcedureFindClient", 1)
34-
print(result)
35112
```
36113

37114

38115

39116
![](https://github.com/PoshoDev/Loaf/blob/main/loaf.png?raw=true)
40117

118+
119+
120+
```
121+
⚠️ Syntax for the package has changed heavily since version 0.2.0, if your project depends on Loaf and is using an inferior version, I heavily suggest that you use the previous stable version:
122+
```
123+
124+
```
125+
$ pip install loaf==0.1.30
126+
```
127+

loaf/__init__.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
# A Python module for effortless database usage.
2-
31
import pymysql, psycopg2, psycopg2.extras, sqlite3, datetime, socket, configparser
42
from rich.table import Table
53
from rich import print as rprint
64

7-
cursors = ["DEFAULT", "DICTIONARY"]
5+
cursors = ["DICTIONARY", "DEFAULT"]
86
modes = ["MySQL", "PostgreSQL", "SQLite"]
97
defaults = {
108
"host": socket.gethostbyname(socket.gethostname()),
@@ -176,32 +174,25 @@ def call(self, procedure, args=[], rollback_on_error=None):
176174

177175
# Prints the result of a query as a rich table. The 'data' argument can be a tuple or a dictionary.
178176
def print(self, data, title=None):
179-
# If the data is a dictionary, convert it to a tuple.
180177
if type(data) == dict:
181178
data = tuple(data.items())
182-
# If the data is a tuple, convert it to a list. This is done because the Rich library doesn't support tuples.
183179
if type(data) == tuple:
184180
data = list(data)
185-
# If the data is a list, convert it to a table.
186181
if type(data) == list:
187182
table = Table(title=title)
188-
# If the data is a list of tuples, convert it to a table.
189183
if type(data[0]) == tuple:
190184
for i in range(len(data[0])):
191185
table.add_column(data[0][i])
192186
for i in range(1, len(data)):
193187
table.add_row(*data[i])
194-
# If the data is a list of dictionaries, convert it to a table.
195188
elif type(data[0]) == dict:
196189
for i in data[0].keys():
197190
table.add_column(i)
198191
for i in data:
199-
# Parse the values to strings. Then add the rows.
200192
values = []
201193
for j in i.values():
202-
values.append(parse(j))
194+
values.append(tParse(j))
203195
table.add_row(*values)
204-
# Print the table.
205196
rprint(table)
206197
else:
207198
raise Exception("Invalid data type.")
@@ -326,3 +317,13 @@ def parse(value):
326317
if isinstance(value, datetime.date):
327318
return value.strftime("%Y-%m-%d")
328319
return "'" + str(value).replace("'", "''") + "'"
320+
321+
# Parses a value to a colored string to be used in tables.
322+
def tParse(value):
323+
if value in [None, "", "NULL"]:
324+
return "[dim]NULL[/]"
325+
if isinstance(value, int):
326+
return "[magenta]" + str(value) + "[/]"
327+
if isinstance(value, datetime.date):
328+
return "[cyan]" + value.strftime("%Y-%m-%d") + "[/]"
329+
return "[green]'" + str(value).replace("'", "''") + "'[/]"

loaf/__init__OLD.py

Lines changed: 0 additions & 138 deletions
This file was deleted.

setup.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from setuptools import setup, find_packages
22

3-
VERSION = "0.1.30"
3+
VERSION = "0.2.0"
44
DESCRIPTION = "Effortlessly access your SQL servers and procedures, plus " \
55
"some other utilities!"
66
with open('README.md', encoding="utf8") as f:
@@ -16,7 +16,14 @@
1616
long_description_content_type="text/markdown",
1717
long_description=LONG_DESCRIPTION,
1818
packages=find_packages(),
19-
install_requires=["pymysql", "datetime"], # Excludes "socket"
19+
install_requires=[
20+
"pymysql",
21+
"psycopg2",
22+
"sqlite3",
23+
"datetime",
24+
"socket",
25+
"configparser"
26+
],
2027
keywords=['python', 'SQL', 'MySQL', 'MariaDB', 'PostgreSQL', 'database',
2128
'db', 'easy', 'loaf', 'bread'],
2229
classifiers=[

0 commit comments

Comments
 (0)