Skip to content

Commit

Permalink
Python examples (#143)
Browse files Browse the repository at this point in the history
* Added python examples
  • Loading branch information
Razikus authored Apr 19, 2022
1 parent 1d4cf72 commit 9a6cc72
Show file tree
Hide file tree
Showing 8 changed files with 834 additions and 43 deletions.
33 changes: 30 additions & 3 deletions src/master/develop/connection.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Connection and authentication

The immudb server runs on port 3323 as the default. The code examples below illustrate how to connect your client to the server and authenticate using default options and the default username and password.
The immudb server runs on port 3322 as the default. The code examples below illustrate how to connect your client to the server and authenticate using default options and the default username and password.
You can modify defaults on the immudb server in `immudb.toml` in the config folder.
:::: tabs

Expand Down Expand Up @@ -69,8 +69,35 @@ immuClient.login("immudb", "immudb");
:::

::: tab Python
This feature is not yet supported or not documented.
Do you want to make a feature request or help out? Open an issue on [Python sdk github project](https://github.com/codenotary/immudb-py/issues/new)

```python
from grpc import RpcError
from immudb import ImmudbClient

URL = "localhost:3322" # immudb running on your machine
LOGIN = "immudb" # Default username
PASSWORD = "immudb" # Default password
DB = b"defaultdb" # Default database name (must be in bytes)


def main():
client = ImmudbClient(URL)
# database parameter is optional
client.login(LOGIN, PASSWORD, database=DB)
client.logout()

# Bad login
try:
client.login("verybadlogin", "verybadpassword")
except RpcError as exception:
print(exception.debug_error_string())
print(exception.details())


if __name__ == "__main__":
main()

```
:::

::: tab Node.js
Expand Down
166 changes: 158 additions & 8 deletions src/master/develop/history.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,39 @@ Note that, similar with many other methods, `history` method is overloaded to al
:::

::: tab Python
This feature is not yet supported or not documented.
Do you want to make a feature request or help out? Open an issue on [Python sdk github project](https://github.com/codenotary/immudb-py/issues/new)

Python immudb sdk currently doesn't support `SinceTx` parameter

```python
from immudb import ImmudbClient

URL = "localhost:3322" # immudb running on your machine
LOGIN = "immudb" # Default username
PASSWORD = "immudb" # Default password
DB = b"defaultdb" # Default database name (must be in bytes)

def main():
client = ImmudbClient(URL)
client.login(LOGIN, PASSWORD, database = DB)

client.set(b'test', b'1')
client.set(b'test', b'2')
client.set(b'test', b'3')

history = client.history(b'test', 0, 100, True) # List[immudb.datatypes.historyResponseItem]
responseItemFirst = history[0]
print(responseItemFirst.key) # Entry key (b'test')
print(responseItemFirst.value) # Entry value (b'3')
print(responseItemFirst.tx) # Transaction id

responseItemThird = history[2]
print(responseItemThird.key) # Entry key (b'test')
print(responseItemThird.value) # Entry value (b'1')
print(responseItemThird.tx) # Transaction id

if __name__ == "__main__":
main()
```
:::

::: tab Node.js
Expand Down Expand Up @@ -218,8 +249,45 @@ List<KV> scanResult = immuClient.scan("scan", 1, 5, false);
:::

::: tab Python
This feature is not yet supported or not documented.
Do you want to make a feature request or help out? Open an issue on [Python sdk github project](https://github.com/codenotary/immudb-py/issues/new)
```python
from immudb import ImmudbClient

URL = "localhost:3322" # immudb running on your machine
LOGIN = "immudb" # Default username
PASSWORD = "immudb" # Default password
DB = b"defaultdb" # Default database name (must be in bytes)

def main():
client = ImmudbClient(URL)
client.login(LOGIN, PASSWORD, database = DB)
toSet = {
b"aaa": b'1',
b'bbb': b'2',
b'ccc': b'3',
b'acc': b'1',
b'aac': b'2',
b'aac:test1': b'3',
b'aac:test2': b'1',
b'aac:xxx:test': b'2'
}
client.setAll(toSet)

result = client.scan(b'', b'', True, 100) # All entries
print(result)
result = client.scan(b'', b'aac', True, 100) # All entries with prefix 'aac' including 'aac'
print(result)

# Seek key example (allows retrieve entries in proper chunks):
result = client.scan(b'', b'', False, 3)
while result:
for item, value in result.items():
print("SEEK", item, value)
lastKey = list(result.keys())[-1]
result = client.scan(lastKey, b'', False, 3)

if __name__ == "__main__":
main()
```
:::

::: tab Node.js
Expand Down Expand Up @@ -410,8 +478,49 @@ try {
:::

::: tab Python
This feature is not yet supported or not documented.
Do you want to make a feature request or help out? Open an issue on [Python sdk github project](https://github.com/codenotary/immudb-py/issues/new)
```python
from immudb import ImmudbClient

URL = "localhost:3322" # immudb running on your machine
LOGIN = "immudb" # Default username
PASSWORD = "immudb" # Default password
DB = b"defaultdb" # Default database name (must be in bytes)

def main():
client = ImmudbClient(URL)
client.login(LOGIN, PASSWORD, database = DB)
client.verifiedSet(b'x', b'1')
client.verifiedSet(b'y', b'1')
retrieved = client.verifiedGet(b'x')
print(retrieved.refkey) # Entry reference key (None)

client.verifiedSetReference(b'x', b'reference1')
client.setReference(b'x', b'reference2')
client.setReference(b'y', b'reference2')
client.verifiedSet(b'y', b'2')

retrieved = client.verifiedGet(b'reference1')
print(retrieved.key) # Entry key (b'x')
print(retrieved.refkey) # Entry reference key (b'reference1')
print(retrieved.verified) # Entry verification status (True)

retrieved = client.verifiedGet(b'reference2')
print(retrieved.key) # Entry key (b'y')
print(retrieved.refkey) # Entry reference key (b'reference2')
print(retrieved.verified) # Entry verification status (True)
print(retrieved.value) # Entry value (b'3')

retrieved = client.verifiedGet(b'x')
print(retrieved.key) # Entry key (b'x')
print(retrieved.refkey) # Entry reference key (None)
print(retrieved.verified) # Entry verification status (True)

retrieved = client.get(b'reference2')
print(retrieved.key) # Entry key (b'y')

if __name__ == "__main__":
main()
```
:::

::: tab Node.js
Expand Down Expand Up @@ -538,8 +647,49 @@ Do you want to make a feature request or help out? Open an issue on [Java sdk gi
:::

::: tab Python
This feature is not yet supported or not documented.
Do you want to make a feature request or help out? Open an issue on [Python sdk github project](https://github.com/codenotary/immudb-py/issues/new)
```python
from immudb import ImmudbClient

URL = "localhost:3322" # immudb running on your machine
LOGIN = "immudb" # Default username
PASSWORD = "immudb" # Default password
DB = b"defaultdb" # Default database name (must be in bytes)

def main():
client = ImmudbClient(URL)
client.login(LOGIN, PASSWORD, database = DB)
client.verifiedSet(b'x', b'1')
client.verifiedSet(b'y', b'1')
retrieved = client.verifiedGet(b'x')
print(retrieved.refkey) # Entry reference key (None)

client.verifiedSetReference(b'x', b'reference1')
client.setReference(b'x', b'reference2')
client.setReference(b'y', b'reference2')
client.verifiedSet(b'y', b'2')

retrieved = client.verifiedGet(b'reference1')
print(retrieved.key) # Entry key (b'x')
print(retrieved.refkey) # Entry reference key (b'reference1')
print(retrieved.verified) # Entry verification status (True)

retrieved = client.verifiedGet(b'reference2')
print(retrieved.key) # Entry key (b'y')
print(retrieved.refkey) # Entry reference key (b'reference2')
print(retrieved.verified) # Entry verification status (True)
print(retrieved.value) # Entry value (b'3')

retrieved = client.verifiedGet(b'x')
print(retrieved.key) # Entry key (b'x')
print(retrieved.refkey) # Entry reference key (None)
print(retrieved.verified) # Entry verification status (True)

retrieved = client.get(b'reference2')
print(retrieved.key) # Entry key (b'y')

if __name__ == "__main__":
main()
```
:::

::: tab Node.js
Expand Down
31 changes: 29 additions & 2 deletions src/master/develop/indexes.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,35 @@ List<KV> zScan2 = immuClient.zScan("set2", 5, false);
:::

::: tab Python
This feature is not yet supported or not documented.
Do you want to make a feature request or help out? Open an issue on [Python sdk github project](https://github.com/codenotary/immudb-py/issues/new)
```python
from immudb import ImmudbClient

URL = "localhost:3322" # immudb running on your machine
LOGIN = "immudb" # Default username
PASSWORD = "immudb" # Default password
DB = b"defaultdb" # Default database name (must be in bytes)

def main():
client = ImmudbClient(URL)
client.login(LOGIN, PASSWORD, database = DB)
client.set(b"user1", b"[email protected]")
client.set(b"user2", b"[email protected]")
client.set(b"user3", b"[email protected]")
client.set(b"user4", b"[email protected]")

client.zAdd(b"age", 100, b"user1")
client.zAdd(b"age", 101, b"user2")
client.zAdd(b"age", 99, b"user3")
client.zAdd(b"age", 100, b"user4")

scanResult = client.zScan(b"age", b"", 0, 0, True, 50, False, 100, 101)
print(scanResult) # Shows records with 'age' 100 <= score < 101
# with descending order and limit = 50


if __name__ == "__main__":
main()
```
:::

::: tab Node.js
Expand Down
Loading

0 comments on commit 9a6cc72

Please sign in to comment.