Skip to content

Commit

Permalink
Minor fixes for pylint and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kotolex committed Nov 6, 2024
1 parent 4154cc8 commit 6c7ccd7
Show file tree
Hide file tree
Showing 23 changed files with 110 additions and 70 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
# README #
<p align="left">
<a href="https://pypi.org/project/surrealist/"><img src="https://img.shields.io/pypi/status/surrealist?style=flat-square"></a>
&nbsp;
<a href="https://pypi.org/project/surrealist/"><img src="https://img.shields.io/pypi/v/surrealist?style=flat-square"></a>
&nbsp;
<a href="https://pypi.org/project/surrealist/"><img src="https://img.shields.io/pypi/dm/surrealist?style=flat-square"></a>
&nbsp;
<a href="https://pypi.org/project/surrealist/"><img src="https://img.shields.io/pypi/pyversions/surrealist?style=flat-square"></a>
&nbsp;
<a href="https://pypi.org/project/surrealist/"><img src="https://img.shields.io/github/last-commit/kotolex/surrealist/master?style=flat-square"></a>
</p>

Surrealist is a Python tool to work with awesome [SurrealDB](https://docs.surrealdb.com/docs/intro) (support for latest version 2.0.4)

Expand Down
6 changes: 5 additions & 1 deletion ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
## Release Notes ##
**Version 1.0.5 (compatible with SurrealDB version 2.0.4):**
- minor examples fix
- minor pylint and readme fixes

**Version 1.0.4 (compatible with SurrealDB version 2.0.4):**
- fix record ids bug (cause SDB since 2.0 not convert string to record_id)
- Readme [block for RecordId](https://github.com/kotolex/surrealist#Using_recordid)
- Readme [block for RecordId](https://github.com/kotolex/surrealist?tab=readme-ov-file#using-recordid)
- examples [for RecordId](https://github.com/kotolex/surrealist/blob/master/examples/record_id.py)
- create RecordId object to work with string or uid/ulid record_id
- now all methods can use RecordId, but strings can be used too for compatibility
Expand Down
6 changes: 3 additions & 3 deletions examples/record_id.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from surrealist import RecordId, get_uuid

# Please read https://github.com/kotolex/surrealist#Using_recordid
# Please read https://github.com/kotolex/surrealist?tab=readme-ov-file#using-recordid
# Refer to https://surrealdb.com/docs/surrealql/datamodel/ids

# You can create a RecordId from id in full form
Expand All @@ -9,14 +9,14 @@
print(record_id.id_part) # tobie
print(record_id.table_part) # person
print(record_id.to_prefixed_string()) # r'person:tobie'
print(f"{record_id!r}") # RecordId('person:tobie')
print(record_id) # RecordId('person:tobie')
# Same RecordId as above we can create with id and table
record_id = RecordId('tobie', table='person')
print(record_id.naive_id) # person:tobie
print(record_id.id_part) # tobie
print(record_id.table_part) # person
print(record_id.to_prefixed_string()) # r'person:tobie'
print(f"{record_id!r}") # RecordId('person:tobie')
print(record_id) # RecordId('person:tobie')

# Note! An exception will be raised if you try to create a RecordId in the wrong way
# All examples below will raise an exception:
Expand Down
2 changes: 1 addition & 1 deletion examples/surreal_ql/define_user.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from surrealist import Database

with Database("http://127.0.0.1:8000", 'test', 'test', credentials=('user_db', 'user_db'), use_http=True) as db:
with Database("http://127.0.0.1:8000", 'test', 'test', credentials=('user_db', 'user_db')) as db:
# on database object we can DEFINE USER

# DEFINE USER new_user ON DATABASE PASSWORD '123456' ROLES OWNER;
Expand Down
6 changes: 3 additions & 3 deletions examples/surreal_ql/ql_alter_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

with Database("http://127.0.0.1:8000", 'test', 'test', credentials=('user_db', 'user_db')) as db:
# ALTER TABLE user SCHEMAFULL;
print(db.alter_table("user").schema_full())
print(db.alter_table("user").schemafull())
# ALTER TABLE IF NOT EXISTS user SCHEMALESS;
print(db.alter_table("user").if_not_exists().schema_less())
print(db.alter_table("user").if_not_exists().schemaless())
# ALTER TABLE user PERMISSIONS FOR create FULL;
print(db.alter_table("user").permissions_for(create="FULL"))
# ALTER TABLE user PERMISSIONS NONE COMMENT "text_of_comment";
print(db.alter_table("user").permissions_none().comment("text_of_comment"))
# ALTER TABLE IF NOT EXISTS user SCHEMAFULL;
print(db.alter_table("user").if_not_exists().schema_full())
print(db.alter_table("user").if_not_exists().schemafull())



Expand Down
7 changes: 4 additions & 3 deletions examples/surreal_ql/ql_create_examples.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from surrealist import Database
from surrealist import Database, RecordId

# Please read https://docs.surrealdb.com/docs/surrealql/statements/create
# here we represent analogs for string queries
Expand All @@ -8,7 +8,8 @@
with Database("http://127.0.0.1:8000", 'test', 'test', credentials=("user_db", "user_db")) as db:
print(db.person.create()) # CREATE person;
print(db.table("person").create().only()) # CREATE ONLY person;
print(db.table("person").create("tobie").only()) # CREATE ONLY person:tobie;
tobie = RecordId("tobie", table="person") # Pay attention how you should use RecordId
print(db.table("person").create(tobie).only()) # CREATE ONLY person:tobie;

# CREATE person SET name = "Tobie", company = "SurrealDB", skills = ["Rust", "Go", "JavaScript"];
print(db.table("person").create().set(name="Tobie", company="SurrealDB", skills=["Rust", "Go", "JavaScript"]))
Expand All @@ -20,7 +21,7 @@
print(db.table("person").create().set('name = "Tobie"', company="SurrealDB", skills=["Rust", "Go", "JavaScript"]))

# CREATE person:tobie CONTENT {"name": "Tobie", "company": "SurrealDB", "skills": ["Rust"]};
print(db.table("person").create("tobie").content({"name": "Tobie", "company": "SurrealDB", "skills": ["Rust"]}))
print(db.table("person").create(tobie).content({"name": "Tobie", "company": "SurrealDB", "skills": ["Rust"]}))

# CREATE person SET age = 46, username = "john-smith" RETURN NONE;
print(db.person.create().set(age=46, username="john-smith").return_none())
Expand Down
8 changes: 4 additions & 4 deletions examples/surreal_ql/ql_delete_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

# Notice: all queries below not executed, just generate representation.
# To run it against SurrealDB, you need to use run method
with Database("http://127.0.0.1:8000", 'test', 'test', credentials=("root", "root")) as db:
with Database("http://127.0.0.1:8000", 'test', 'test', credentials=("user_db", "user_db")) as db:
print(db.person.delete()) # DELETE person;
print(db.person.delete("tobie")) # DELETE person:tobie;
print(db.person.delete(RecordId("person:tobie"))) # DELETE person:tobie;
print(db.table("person").delete("tobie").only()) # DELETE ONLY person:tobie;
tobie = RecordId("tobie", table="person") # Pay attention how you should use RecordId
print(db.person.delete(tobie)) # DELETE person:tobie;
print(db.table("person").delete(tobie).only()) # DELETE ONLY person:tobie;

# DELETE person WHERE age < 18 RETURN NONE TIMEOUT 10s PARALLEL;
print(db.table("person").delete().where("age < 18").return_none().timeout("10s").parallel())
Expand Down
10 changes: 6 additions & 4 deletions examples/surreal_ql/ql_insert_examples.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
from surrealist import Database
from surrealist import Database, RecordId

# Please read https://docs.surrealdb.com/docs/surrealql/statements/insert
# here we represent analogs for string queries

# Notice: all queries below not executed, just generate representation.
# To run it against SurrealDB, you need to use run method
with Database("http://127.0.0.1:8000", 'test', 'test', credentials=("user_db", "user_db")) as db:
tobie = RecordId("tobie", table="person") # Pay attention how you should use RecordId
jaime = RecordId("jaime", table="person") # Pay attention how you should use RecordId
data = {
'name': 'SurrealDB',
'founded': "2021-09-10",
'founders': ['person:tobie', 'person:jaime'],
'founders': [tobie, jaime],
'tags': ['big data', 'database']
}
# INSERT INTO person {"name": "SurrealDB", "founded": "2021-09-10", "founders": ["person:tobie", "person:jaime"],
# "tags": ["big data", "database"]};
print(db.person.insert(data))

bulk = [{'id': "person:jaime", 'name': "Jaime", 'surname': "Morgan Hitchcock"},
{'id': "person:tobie", 'name': "Tobie", 'surname': "Morgan Hitchcock"}]
bulk = [{'id': jaime, 'name': "Jaime", 'surname': "Morgan Hitchcock"},
{'id': tobie, 'name': "Tobie", 'surname': "Morgan Hitchcock"}]
# INSERT INTO person [{"id": "person:jaime", "name": "Jaime", "surname": "Morgan Hitchcock"},
# {"id": "person:tobie", "name": "Tobie", "surname": "Morgan Hitchcock"}];
print(db.person.insert(bulk))
Expand Down
2 changes: 1 addition & 1 deletion examples/surreal_ql/ql_live_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# Notice: all queries below not executed, just generate representation.
# To run it against SurrealDB, you need to use run method
with Database("http://127.0.0.1:8000", 'test', 'test', credentials=("root", "root")) as db:
with Database("http://127.0.0.1:8000", 'test', 'test', credentials=("user_db", "user_db")) as db:
# print here - is a callback
print(db.person.live(print)) # LIVE SELECT * FROM person;
print(db.person.live(print, use_diff=True)) # LIVE SELECT DIFF FROM person;
Expand Down
2 changes: 1 addition & 1 deletion examples/surreal_ql/ql_relate_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# Notice: all queries below not executed, just generate representation.
# To run it against SurrealDB, you need to use run method
with Database("http://127.0.0.1:8000", 'test', 'test', credentials=("root", "root")) as db:
with Database("http://127.0.0.1:8000", 'test', 'test', credentials=("user_db", "user_db")) as db:
# Pay attention, we should use RELATE on database level, not on specific table

# RELATE person:l19zjikkw1p1h9o6ixrg->wrote->article:8nkk6uj4yprt49z7y3zm;
Expand Down
8 changes: 4 additions & 4 deletions examples/surreal_ql/ql_select_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
with Database("http://127.0.0.1:8000", 'test', 'test', credentials=("user_db", "user_db")) as db:
print(db.table("person").select()) # SELECT * FROM person;
print(db.table("person").select("*")) # SELECT * FROM person;
print(db.table("person").select().by_id("john")) # SELECT * FROM person:john;
print(db.table("person").select().by_id(RecordId("person:john"))) # SELECT * FROM person:john;
john = RecordId("john", table="person") # Pay attention how you should use RecordId
print(db.table("person").select().by_id(john)) # SELECT * FROM person:john;
print(db.table("person").select("name", "age")) # SELECT name, age FROM person;
print(db.table("person").select("name", "age").by_id("john").only()) # SELECT name, age FROM ONLY person:john;
print(db.table("person").select(value="age").by_id("john")) # SELECT VALUE age FROM person:john;
print(db.table("person").select("name", "age").by_id(john).only()) # SELECT name, age FROM ONLY person:john;
print(db.table("person").select(value="age").by_id(john)) # SELECT VALUE age FROM person:john;

# SELECT *, (SELECT * FROM events WHERE type = 'activity' LIMIT 5) AS history FROM person;
print(db.table("person").select("*", alias=[("history", "(SELECT * FROM events WHERE type = 'activity' LIMIT 5)")]))
Expand Down
15 changes: 8 additions & 7 deletions examples/surreal_ql/ql_update_examples.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from surrealist import Database
from surrealist import Database, RecordId

# Please read https://docs.surrealdb.com/docs/surrealql/statements/update
# here we represent analogs for string queries
Expand All @@ -7,7 +7,8 @@
# To run it against SurrealDB, you need to use run method
with Database("http://127.0.0.1:8000", 'test', 'test', credentials=('user_db', 'user_db')) as db:
print(db.table("person").update()) # UPDATE person;
print(db.table("person").update("tobie").only()) # UPDATE ONLY person:tobie;
tobie = RecordId("tobie", table="person") # Pay attention how you should use RecordId
print(db.table("person").update(tobie).only()) # UPDATE ONLY person:tobie;

# UPDATE person SET active = true WHERE age < 18 RETURN NONE TIMEOUT 10s PARALLEL;
print(db.table("person").update().set(active=True).where("age < 18").return_none().timeout("10s").parallel())
Expand All @@ -22,17 +23,17 @@
print(db.city.update().set(rank=4, population=9541000).where("name = 'London'"))

# UPDATE ONLY person:tobie SET name = "Tobie", company = "SurrealDB", skills = ["Rust", "Go", "JavaScript"];
print(db.table("person").update("tobie").only().set(name="Tobie", company="SurrealDB",
skills=['Rust', 'Go', 'JavaScript']))
print(db.table("person").update(tobie).only().set(name="Tobie", company="SurrealDB",
skills=['Rust', 'Go', 'JavaScript']))

data = {'name': 'Tobie', 'company': 'SurrealDB', 'skills': ['Rust', 'Go', 'JavaScript']}
# UPDATE person:tobie CONTENT {"name": "Tobie", "company": "SurrealDB", "skills": ["Rust", "Go", "JavaScript"]};
print(db.person.update("tobie").content(data))
print(db.person.update(tobie).content(data))

data = [{"op": "add", "path": "Engineering", "value": "true"}]
# UPDATE person:tobie PATCH [{"op": "add", "path": "Engineering", "value": "true"}];
print(db.person.update("tobie").patch(data))
print(db.person.update(tobie).patch(data))

data = {'settings': {'marketing': True}}
# UPDATE person:tobie MERGE {"settings": {"marketing": true}};
print(db.person.update("tobie").merge(data))
print(db.person.update(tobie).merge(data))
13 changes: 7 additions & 6 deletions examples/surreal_ql/ql_upsert_examples.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from surrealist import Database
from surrealist import Database, RecordId

# Please read https://docs.surrealdb.com/docs/surrealql/statements/upsert
# here we represent analogs for string queries
Expand All @@ -7,7 +7,8 @@
# To run it against SurrealDB, you need to use run method
with Database("http://127.0.0.1:8000", 'test', 'test', credentials=('user_db', 'user_db')) as db:
print(db.table("person").upsert()) # UPSERT person;
print(db.table("person").upsert("tobie").only()) # UPSERT ONLY person:tobie;
tobie = RecordId("tobie", table="person") # Pay attention how you should use RecordId
print(db.table("person").upsert(tobie).only()) # UPSERT ONLY person:tobie;

# UPSERT person SET active = true WHERE age < 18 RETURN NONE TIMEOUT 10s PARALLEL;
print(db.table("person").upsert().set(active=True).where("age < 18").return_none().timeout("10s").parallel())
Expand All @@ -22,17 +23,17 @@
print(db.city.upsert().set(rank=4, population=9541000).where("name = 'London'"))

# UPSERT ONLY person:tobie SET name = "Tobie", company = "SurrealDB", skills = ["Rust", "Go", "JavaScript"];
print(db.table("person").upsert("tobie").only().set(name="Tobie", company="SurrealDB",
print(db.table("person").upsert(tobie).only().set(name="Tobie", company="SurrealDB",
skills=['Rust', 'Go', 'JavaScript']))

data = {'name': 'Tobie', 'company': 'SurrealDB', 'skills': ['Rust', 'Go', 'JavaScript']}
# UPSERT person:tobie CONTENT {"name": "Tobie", "company": "SurrealDB", "skills": ["Rust", "Go", "JavaScript"]};
print(db.person.upsert("tobie").content(data))
print(db.person.upsert(tobie).content(data))

data = [{"op": "add", "path": "Engineering", "value": "true"}]
# UPSERT person:tobie PATCH [{"op": "add", "path": "Engineering", "value": "true"}];
print(db.person.upsert("tobie").patch(data))
print(db.person.upsert(tobie).patch(data))

data = {'settings': {'marketing': True}}
# UPSERT person:tobie MERGE {"settings": {"marketing": true}};
print(db.person.upsert("tobie").merge(data))
print(db.person.upsert(tobie).merge(data))
23 changes: 12 additions & 11 deletions examples/surreal_ql/transaction.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from surrealist import Database
from surrealist import Database, RecordId

# we connect to a database via Database object
with Database("http://127.0.0.1:8000", 'test', 'test', credentials=("user_db", "user_db")) as db:
Expand All @@ -7,23 +7,24 @@
book = db.table("book")
counter = db.table("counter")
# we create queries here but not run it!
create_author = author.create().content({"name": "john", "id": "john"})
create_book = book.create().content({"title": "Title", "author": "author:john"}) # book will relate to author
counter_inc = counter.update("author_count").set("count +=1") # increment counter
john = RecordId("johnny", table="author") # Pay attention how you should use RecordId
author_count = RecordId("authorcount", table="counter") # Pay attention how you should use RecordId
create_author = author.create().content({"name": "john", "id": john})
create_book = book.create().content({"title": "Title", "author": john}) # book will relate to author
counter_inc = counter.upsert(author_count).set("count +=1") # increment counter

# on transactions see https://docs.surrealdb.com/docs/surrealql/transactions
transaction = db.transaction([create_author, create_book, counter_inc])
print(transaction)
# BEGIN TRANSACTION;
#
# CREATE author CONTENT {"name": "john", "id": "john"};
# CREATE book CONTENT {"title": "Title", "author": "author:john"};
# UPDATE counter:author_count SET count +=1;
# CREATE author CONTENT {"name": "john", "id": author:johnny};
# CREATE book CONTENT {"title": "Title", "author": author:johnny};
# UPDATE counter:authorcount SET count +=1;
#
# COMMIT TRANSACTION;

print(transaction.run().result)
# [{'result': [{'id': 'author:john', 'name': 'john'}], 'status': 'OK', 'time': '278.709µs'},
# {'result': [{'author': 'author:john', 'id': 'book:5tyy73v17xqdk24yvj3c', 'title': 'Title'}],
# 'status': 'OK', 'time': '53.875µs'},
# {'result': [{'count': 1, 'id': 'counter:author_count'}], 'status': 'OK', 'time': '53.667µs'}]
# [{'result': [{'id': 'author:johnny', 'name': 'john'}], 'status': 'OK', 'time': '115.9µs'},
# {'result': [{'author': 'author:johnny', 'id': 'book:0yhz1i69d4ifocips9jk', 'title': 'Title'}], 'status': 'OK', 'time': '312.1µs'},
# {'result': [{'count': 1, 'id': 'counter:authorcount'}], 'status': 'OK', 'time': '263.4µs'}]
2 changes: 1 addition & 1 deletion pylint.toml
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ max-returns = 6
max-statements = 50

# Minimum number of public methods for a class (see R0903).
min-public-methods = 2
min-public-methods = 1

[tool.pylint.exceptions]
# Exceptions that will emit a warning when caught.
Expand Down
16 changes: 12 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,29 @@ build-backend = "setuptools.build_meta"

[project]
name = "surrealist"
version = "1.0.4"
version = "1.0.5"
description = "Python client for SurrealDB, latest SurrealDB version compatible, all features supported"
readme = "README.md"
authors = [{ name = "kotolex", email = "farofwell@gmail.com" }]
license = { file = "LICENSE" }
classifiers = [
"License :: OSI Approved :: MIT License",
"Intended Audience :: Developers",
"Intended Audience :: System Administrators",
"Intended Audience :: Education",
"Topic :: Software Development",
"Topic :: Database",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Topic :: Software Development :: Libraries",
"Development Status :: 5 - Production/Stable",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
]
keywords = ["surreal", "python", "Surreal", "SurrealDB", "surrealist", "database", "surrealdb"]
keywords = ["surreal", "python", "Surreal", "SurrealDB", "surrealist", "database", "surrealdb", "surrealdb.py"]
dependencies = [
"websocket-client",
]
Expand Down
2 changes: 1 addition & 1 deletion src/surrealist/connections/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def use(self, namespace: str, database: Optional[str] = None) -> SurrealResult:
@abstractmethod
def transport(self) -> Transport:
"""
This method returns the current transport
This method returns the transport type for the current connection
Refer to: https://github.com/kotolex/surrealist?tab=readme-ov-file#transports
"""
Expand Down
3 changes: 3 additions & 0 deletions src/surrealist/connections/http_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ def _sign(self, credentials, db_params, url):
f"Refer to https://docs.surrealdb.com/docs/introduction/start")

def transport(self) -> Transport:
"""
Returns the transport type for http connection
"""
return Transport.HTTP

@connected
Expand Down
4 changes: 4 additions & 0 deletions src/surrealist/connections/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ def __init__(self, first_connection: Connection, url: str, namespace: Optional[s
self._start()

def transport(self) -> Transport:
"""
Returns the transport type of the underlying connections
:return: Transport enum member
"""
return Transport.HTTP if self._options["use_http"] else Transport.WEBSOCKET

@property
Expand Down
Loading

0 comments on commit 6c7ccd7

Please sign in to comment.