Skip to content

Commit

Permalink
Python code cleanup (#2573)
Browse files Browse the repository at this point in the history
* Python code cleanup and test case update

---------

Signed-off-by: Prateek Kumar <prateek.kumar@improving.com>
  • Loading branch information
prateek-kumar-improving authored Nov 7, 2024
1 parent 15d2111 commit 6e04584
Show file tree
Hide file tree
Showing 11 changed files with 614 additions and 744 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#### Changes
* Python code cleanup ([#2573](https://github.com/valkey-io/valkey-glide/pull/2573))
* Python: Python: FT.PROFILE command added ([#2543](https://github.com/valkey-io/valkey-glide/pull/2543))
* Python: Python: FT.AGGREGATE command added([#2530](https://github.com/valkey-io/valkey-glide/pull/2530))
* Python: Add JSON.OBJLEN command ([#2495](https://github.com/valkey-io/valkey-glide/pull/2495))
Expand Down
4 changes: 2 additions & 2 deletions python/python/glide/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
FtProfileOptions,
)
from glide.async_commands.server_modules.ft_options.ft_search_options import (
FtSeachOptions,
FtSearchLimit,
FtSearchOptions,
ReturnField,
)
from glide.async_commands.server_modules.json import (
Expand Down Expand Up @@ -305,7 +305,7 @@
"VectorType",
"FtSearchLimit",
"ReturnField",
"FtSeachOptions",
"FtSearchOptions",
"FtAggregateApply",
"FtAggregateFilter",
"FtAggregateClause",
Expand Down
90 changes: 45 additions & 45 deletions python/python/glide/async_commands/server_modules/ft.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
FtProfileOptions,
)
from glide.async_commands.server_modules.ft_options.ft_search_options import (
FtSeachOptions,
FtSearchOptions,
)
from glide.constants import (
TOK,
Expand All @@ -35,7 +35,7 @@

async def create(
client: TGlideClient,
indexName: TEncodable,
index_name: TEncodable,
schema: List[Field],
options: Optional[FtCreateOptions] = None,
) -> TOK:
Expand All @@ -44,9 +44,9 @@ async def create(
Args:
client (TGlideClient): The client to execute the command.
indexName (TEncodable): The index name.
index_name (TEncodable): The index name.
schema (List[Field]): Fields to populate into the index. Equivalent to `SCHEMA` block in the module API.
options (Optional[FtCreateOptions]): Optional arguments for the FT.CREATE command. See `FtCreateOptions`.
options (Optional[FtCreateOptions]): Optional arguments for the FT.CREATE command.
Returns:
TOK: A simple "OK" response.
Expand All @@ -58,52 +58,52 @@ async def create(
>>> result = await ft.create(glide_client, "my_idx1", schema, FtCreateOptions(DataType.HASH, prefixes))
'OK' # Indicates successful creation of index named 'idx'
"""
args: List[TEncodable] = [CommandNames.FT_CREATE, indexName]
args: List[TEncodable] = [CommandNames.FT_CREATE, index_name]
if options:
args.extend(options.toArgs())
args.extend(options.to_args())
if schema:
args.append(FtCreateKeywords.SCHEMA)
for field in schema:
args.extend(field.toArgs())
args.extend(field.to_args())
return cast(TOK, await client.custom_command(args))


async def dropindex(client: TGlideClient, indexName: TEncodable) -> TOK:
async def dropindex(client: TGlideClient, index_name: TEncodable) -> TOK:
"""
Drops an index. The index definition and associated content are deleted. Keys are unaffected.
Args:
client (TGlideClient): The client to execute the command.
indexName (TEncodable): The index name for the index to be dropped.
index_name (TEncodable): The index name for the index to be dropped.
Returns:
TOK: A simple "OK" response.
Examples:
For the following example to work, an index named 'idx' must be already created. If not created, you will get an error.
>>> from glide import ft
>>> indexName = "idx"
>>> result = await ft.dropindex(glide_client, indexName)
>>> index_name = "idx"
>>> result = await ft.dropindex(glide_client, index_name)
'OK' # Indicates successful deletion/dropping of index named 'idx'
"""
args: List[TEncodable] = [CommandNames.FT_DROPINDEX, indexName]
args: List[TEncodable] = [CommandNames.FT_DROPINDEX, index_name]
return cast(TOK, await client.custom_command(args))


async def search(
client: TGlideClient,
indexName: TEncodable,
index_name: TEncodable,
query: TEncodable,
options: Optional[FtSeachOptions],
options: Optional[FtSearchOptions],
) -> FtSearchResponse:
"""
Uses the provided query expression to locate keys within an index. Once located, the count and/or the content of indexed fields within those keys can be returned.
Args:
client (TGlideClient): The client to execute the command.
indexName (TEncodable): The index name to search into.
index_name (TEncodable): The index name to search into.
query (TEncodable): The text query to search.
options (Optional[FtSeachOptions]): The search options. See `FtSearchOptions`.
options (Optional[FtSearchOptions]): The search options.
Returns:
FtSearchResponse: A two element array, where first element is count of documents in result set, and the second element, which has the format Mapping[TEncodable, Mapping[TEncodable, TEncodable]] is a mapping between document names and map of their attributes.
Expand All @@ -115,25 +115,25 @@ async def search(
- A key named {json:}1 with value {"a":1, "b":2}
>>> from glide import ft
>>> result = await ft.search(glide_client, "idx", "*", options=FtSeachOptions(return_fields=[ReturnField(field_identifier="first"), ReturnField(field_identifier="second")]))
[1, { b'json:1': { b'first': b'42', b'second': b'33' } }] # The first element, 1 is the number of keys returned in the search result. The second element is a map of data queried per key.
>>> result = await ft.search(glide_client, "idx", "*", options=FtSearchOptions(return_fields=[ReturnField(field_identifier="first"), ReturnField(field_identifier="second")]))
[1, { b'json:1': { b'first': b'42', b'second': b'33' } }] # The first element, 1 is the number of keys returned in the search result. The second element is a map of data queried per key.
"""
args: List[TEncodable] = [CommandNames.FT_SEARCH, indexName, query]
args: List[TEncodable] = [CommandNames.FT_SEARCH, index_name, query]
if options:
args.extend(options.toArgs())
args.extend(options.to_args())
return cast(FtSearchResponse, await client.custom_command(args))


async def aliasadd(
client: TGlideClient, alias: TEncodable, indexName: TEncodable
client: TGlideClient, alias: TEncodable, index_name: TEncodable
) -> TOK:
"""
Adds an alias for an index. The new alias name can be used anywhere that an index name is required.
Args:
client (TGlideClient): The client to execute the command.
alias (TEncodable): The alias to be added to an index.
indexName (TEncodable): The index name for which the alias has to be added.
index_name (TEncodable): The index name for which the alias has to be added.
Returns:
TOK: A simple "OK" response.
Expand All @@ -143,7 +143,7 @@ async def aliasadd(
>>> result = await ft.aliasadd(glide_client, "myalias", "myindex")
'OK' # Indicates the successful addition of the alias named "myalias" for the index.
"""
args: List[TEncodable] = [CommandNames.FT_ALIASADD, alias, indexName]
args: List[TEncodable] = [CommandNames.FT_ALIASADD, alias, index_name]
return cast(TOK, await client.custom_command(args))


Expand All @@ -168,15 +168,15 @@ async def aliasdel(client: TGlideClient, alias: TEncodable) -> TOK:


async def aliasupdate(
client: TGlideClient, alias: TEncodable, indexName: TEncodable
client: TGlideClient, alias: TEncodable, index_name: TEncodable
) -> TOK:
"""
Updates an existing alias to point to a different physical index. This command only affects future references to the alias.
Args:
client (TGlideClient): The client to execute the command.
alias (TEncodable): The alias name. This alias will now be pointed to a different index.
indexName (TEncodable): The index name for which an existing alias has to updated.
index_name (TEncodable): The index name for which an existing alias has to updated.
Returns:
TOK: A simple "OK" response.
Expand All @@ -186,20 +186,20 @@ async def aliasupdate(
>>> result = await ft.aliasupdate(glide_client, "myalias", "myindex")
'OK' # Indicates the successful update of the alias to point to the index named "myindex"
"""
args: List[TEncodable] = [CommandNames.FT_ALIASUPDATE, alias, indexName]
args: List[TEncodable] = [CommandNames.FT_ALIASUPDATE, alias, index_name]
return cast(TOK, await client.custom_command(args))


async def info(client: TGlideClient, indexName: TEncodable) -> FtInfoResponse:
async def info(client: TGlideClient, index_name: TEncodable) -> FtInfoResponse:
"""
Returns information about a given index.
Args:
client (TGlideClient): The client to execute the command.
indexName (TEncodable): The index name for which the information has to be returned.
index_name (TEncodable): The index name for which the information has to be returned.
Returns:
FtInfoResponse: Nested maps with info about the index. See example for more details. See `FtInfoResponse`.
FtInfoResponse: Nested maps with info about the index. See example for more details.
Examples:
An index with name 'myIndex', 1 text field and 1 vector field is already created for gettting the output of this example.
Expand Down Expand Up @@ -238,59 +238,59 @@ async def info(client: TGlideClient, indexName: TEncodable) -> FtInfoResponse:
b'index_degradation_percentage', 0
]
"""
args: List[TEncodable] = [CommandNames.FT_INFO, indexName]
args: List[TEncodable] = [CommandNames.FT_INFO, index_name]
return cast(FtInfoResponse, await client.custom_command(args))


async def explain(
client: TGlideClient, indexName: TEncodable, query: TEncodable
client: TGlideClient, index_name: TEncodable, query: TEncodable
) -> TEncodable:
"""
Parse a query and return information about how that query was parsed.
Args:
client (TGlideClient): The client to execute the command.
indexName (TEncodable): The index name for which the query is written.
index_name (TEncodable): The index name for which the query is written.
query (TEncodable): The search query, same as the query passed as an argument to FT.SEARCH.
Returns:
TEncodable: A string containing the parsed results representing the execution plan.
Examples:
>>> from glide import ft
>>> result = await ft.explain(glide_client, indexName="myIndex", query="@price:[0 10]")
>>> result = await ft.explain(glide_client, index_name="myIndex", query="@price:[0 10]")
b'Field {\n price\n 0\n 10\n}\n' # Parsed results.
"""
args: List[TEncodable] = [CommandNames.FT_EXPLAIN, indexName, query]
args: List[TEncodable] = [CommandNames.FT_EXPLAIN, index_name, query]
return cast(TEncodable, await client.custom_command(args))


async def explaincli(
client: TGlideClient, indexName: TEncodable, query: TEncodable
client: TGlideClient, index_name: TEncodable, query: TEncodable
) -> List[TEncodable]:
"""
Same as the FT.EXPLAIN command except that the results are displayed in a different format. More useful with cli.
Args:
client (TGlideClient): The client to execute the command.
indexName (TEncodable): The index name for which the query is written.
index_name (TEncodable): The index name for which the query is written.
query (TEncodable): The search query, same as the query passed as an argument to FT.SEARCH.
Returns:
List[TEncodable]: An array containing the execution plan.
Examples:
>>> from glide import ft
>>> result = await ft.explaincli(glide_client, indexName="myIndex", query="@price:[0 10]")
>>> result = await ft.explaincli(glide_client, index_name="myIndex", query="@price:[0 10]")
[b'Field {', b' price', b' 0', b' 10', b'}', b''] # Parsed results.
"""
args: List[TEncodable] = [CommandNames.FT_EXPLAINCLI, indexName, query]
args: List[TEncodable] = [CommandNames.FT_EXPLAINCLI, index_name, query]
return cast(List[TEncodable], await client.custom_command(args))


async def aggregate(
client: TGlideClient,
indexName: TEncodable,
index_name: TEncodable,
query: TEncodable,
options: Optional[FtAggregateOptions],
) -> FtAggregateResponse:
Expand All @@ -299,7 +299,7 @@ async def aggregate(
Args:
client (TGlideClient): The client to execute the command.
indexName (TEncodable): The index name for which the query is written.
index_name (TEncodable): The index name for which the query is written.
query (TEncodable): The search query, same as the query passed as an argument to FT.SEARCH.
options (Optional[FtAggregateOptions]): The optional arguments for the command.
Expand All @@ -311,28 +311,28 @@ async def aggregate(
>>> result = await ft.aggregate(glide_client, "myIndex", "*", FtAggregateOptions(loadFields=["__key"], clauses=[GroupBy(["@condition"], [Reducer("COUNT", [], "bicycles")])]))
[{b'condition': b'refurbished', b'bicycles': b'1'}, {b'condition': b'new', b'bicycles': b'5'}, {b'condition': b'used', b'bicycles': b'4'}]
"""
args: List[TEncodable] = [CommandNames.FT_AGGREGATE, indexName, query]
args: List[TEncodable] = [CommandNames.FT_AGGREGATE, index_name, query]
if options:
args.extend(options.to_args())
return cast(FtAggregateResponse, await client.custom_command(args))


async def profile(
client: TGlideClient, indexName: TEncodable, options: FtProfileOptions
client: TGlideClient, index_name: TEncodable, options: FtProfileOptions
) -> FtProfileResponse:
"""
Runs a search or aggregation query and collects performance profiling information.
Args:
client (TGlideClient): The client to execute the command.
indexName (TEncodable): The index name
index_name (TEncodable): The index name
options (FtProfileOptions): Options for the command.
Returns:
FtProfileResponse: A two-element array. The first element contains results of query being profiled, the second element stores profiling information.
Examples:
>>> ftSearchOptions = FtSeachOptions(return_fields=[ReturnField(field_identifier="a", alias="a_new"), ReturnField(field_identifier="b", alias="b_new")])
>>> ftSearchOptions = FtSearchOptions(return_fields=[ReturnField(field_identifier="a", alias="a_new"), ReturnField(field_identifier="b", alias="b_new")])
>>> ftProfileResult = await ft.profile(glide_client, "myIndex", FtProfileOptions.from_query_options(query="*", queryOptions=ftSearchOptions))
[
[
Expand All @@ -357,5 +357,5 @@ async def profile(
}
]
"""
args: List[TEncodable] = [CommandNames.FT_PROFILE, indexName] + options.to_args()
args: List[TEncodable] = [CommandNames.FT_PROFILE, index_name] + options.to_args()
return cast(FtProfileResponse, await client.custom_command(args))
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class FtCreateKeywords:
EF_RUNTIME = "EF_RUNTIME"


class FtSeachKeywords:
class FtSearchKeywords:
"""
Keywords used in the FT.SEARCH command.
"""
Expand Down
Loading

0 comments on commit 6e04584

Please sign in to comment.