Perspective.py

Latest version: v0.3.4

Safety actively analyzes 623126 Python packages for vulnerabilities to keep your Python projects secure.

Scan your dependencies

Page 1 of 2

0.3.4

Here is a release which includes yet some other ideas I've added and some bugs that I've noticed while implementing my ideas and I've fixed. You can see my ideas below.

New features
- Added yet another example code, but this time it introduces an easy-to-use GUI (Graphical User Interface) written with tkinter library. However, understanding the code requires some object-oriented programming knowledge and experience. Despite the source code is available in the repository, I actually designed the GUI for end-users therefore it's more like a demo for the API.
- Added `sort_response` function to the `perspective.utils` class which can be used to sort the keys of the dictionary that the `analyze` function returned according to their values or in alphabetical in both ascending and descending order.
- Added `UnknownSorting` exception type to the `perspective.errors` class, however, unlike its name suggests, this exception exists frankly to raise when an unknown string is provided to `order` or `sort_by` arguments of `sort_response` function.
- Added `export_json` function to the `perspective.utils` class which can be used to export the dictionary that the `analyze` function returned to a file in JSON format as the name of the function suggests. The file name can be anything.

Bug fixes
- Fixed `EmptyResponse` exception when none of the requested attributes supported the language of the text provided and when the `skip_on_lang` keyword argument of `analyze` function was set to True therefore the response was (supposed to be) an empty dictionary. The exception was caused by the logging line of the function.

0.3.3

Whilst trying to create a GUI (Graphical User Interface) for the API, I noticed that passing `None` to the `logging_level` argument while creating the Client object raises an exception. In this release, I only fixed that issue.

Bug fixes
- Fixed an exception when `None` was passed to the `logging_level` argument of `perspective.Client.__init__` function.

0.3.2

A random idea came to my mind whilst I was in school and I decided to add it to the library. While I was adding the feature, I noticed some bugs and fixed them too at the mean time. You can see what the idea was and the bugs that I've fixed below. Other than that, I literally have no idea to add to the library so I would appreciate if you give me some ideas in the Issues tab of the repository.

New features
- Added `save_data` function which can be used to save the response that the `analyze` function returned to a SQLite3 database file. I know it's already pretty easy to do this by yourself but here is an easier way.
- Added `EmptyResponse` exception type to `perspective.errors` class which gets raised when there is no attribute in the dictionary that was passed to the `response` argument in any of the functions of `perspective.utils` class.
- Added `EmptyFileName` exception type to `perspective.errors` class which gets raised when an empty string was provided as the `filename` argument of `save_graph` and/or `save_data` functions of `perspective.utils` class.

Changes
- Changed the way the `analyze` function of `perspective.Client` object and all other functions in `perspective.utils` class (only if raw response was provided) fetch score values of attributes from the raw response that was directly returned from the API. They now get the score values from `summaryScore` nested-dictionary which contains the average score value instead of `spanScores` which shall contain score values of each sentence of the text requested to the API in the future.

Bug fixes
- Fixed `TypeError` exception when attempting to use `format_response` or `save_data` in `perspective.utils` class by providing raw response to the `response` argument and setting `sort_by` argument to something other than `None`.
- Fixed TOXICITY attribute appearing as `toxicity` instead of `Toxicity` (first letter is not capital) when `True` is passed to the `align_right` argument of `format_response` function in `perspective.utils` class.
- Fixed `ModuleNotFoundError` exception when importing the package (perspective.py) on a machine with no `matplotlib` library installed, as the library was missing in `install_requires` list of `setup.py` file.

Code changes
- Fixed some typos in function comments.
- Added function annotations to every single function in the library.

0.3.1

I just noticed that the `logging_level` property of `perpective.Client` object was not working properly so I checked the code out and realized my mistake, thereupon I released a quick fix to it. Now you can change the logging level anytime you want by setting a new value to `logging_level` property of your `perspective.Client` instance. Literally the exact same thing I said above is available below.

Bug fixes
- Fixed `logging_level` property not working properly (setting a value to it didn't change anything).

0.3.0

Although this release does not include as much interesting new features as previous release does, it still can be considered as a big release. I spent my whole weekend looking for ways to improve ease of usage and find bugs, and here is the result. All changes are available below:
New features
- Support for `logging` module is here! Now you can set the logging level in `logging_level` argument of `__init__` function of Client object; in other words, when creating the Client object, you can pass the logging level limitation you want to the `logging_level` argument. Information logs such as completion of the request to the API will be logged as INFO and other logs such as skipping attributes which does not support the language of the text will be logged as DEBUG.
- Added `change_token` function to Client object which can be used to change the token without recreating the Client object.
- Added `sort_by` argument to `perspective.utils.format_response` which takes whether `ascending` or `descending` as value (literal value), which can be used to sort the attributes according to their score values ascending or descending.
- Added `return_raw` argument to `analyze` function of Client object which can be used to make the function return the raw response instead of a simplified response.
- Added support for processing raw responses in `response` argument of every single function in `perspective.utils` class.
- Added new exception types for specification of errors that occur whilst making a request to the API or whilst checking validity of arguments locally.
Changes
- Exceptions no more include the parent exception with a message "During handling of the above exception, another exception occurred:".
- Removed "Error" suffix from all exception names, for example, `UnknownAttributeError` is now `UnknownAttribute`.
- Made `filename` attribute of `save_graph` function in `perspective.utils` class non-required and default to "chart.png".
Bug fixes
- Fixed `UnknownAttribute` error when multiple attribute groups (such as `perspective.Attributes.All` and/or `perspective.Attributes.Production`) used in `attributes` argument of `analyze` function in Client object.
- Fixed `show_graph` and `save_graph` functions drawing grid lines onto the bar chart no matter what value `grid_lines` argument gets unless `False` is given to `grid_lines` argument of those functions.
Code changes
- Moved all exception classes from `main.py` to a new file named `error.py`.
- Fixed spelling, grammar and argument name mistakes in function comments.
- Included license information in every single *.py file except `__init__.py` and updated the copyright year in license text.
- Moved variables which hold lists of all attributes and/or all attribute groups to `attributes.py` as a global variable outside the Attributes class, for simplification of main code.

0.2.0

This is one of biggest updates so far, which includes lots of new features and some bug fixes. I've also added some examples to the examples folder. All changes are listed below:

New features
- Added `format_response`, `get_highest`, `get_lowest`, `show_graph` and `save_graph` functions to `perspective/utils.py`.
- `format_response` formats the dictionary that the `analyze` function returns to make the response easier to read by a human.
- `get_highest` returns the attribute with the highest score value from the dictionary that the `analyze` function returns.
- `get_lowest` returns the attribute with the lowest score value.
- `show_graph` uses matplotlib library to create a bar chart, descending according to the score values and shows the graph by popping up an interactive window.
- `save_graph` does the same `show_graph` does but saves the graph as an image instead of showing it by popping up a window.
- Added `perspective.Attributes.All`, `perspective.Attributes.Production`, `perspective.Attributes.Experimental` and `perspective.Attributes.NewYorkTimes` which include their related attributes (such as TOXICITY goes to Production and ATTACK_ON_AUTHOR goes to NewYorkTimes). Information about which attribute belongs to which category can be found in [this article](https://developers.perspectiveapi.com/s/about-the-api-attributes-and-languages). Those can be used on `attributes` argument of `analyze` function.
- Added `skip_on_lang` and `skip_on_unknown` arguments to `analyze` function.
- `skip_on_lang` tells the function to skip an attribute if the language of the text is not supported by that attribute. The function checks whether the attribute supports that language or not by making a request to the API instead of checking locally, so it may exceed the ratelimit for the API if there are lots of attributes which are expected to not support the language of the text.
- `skip_on_unknown` tells the function to skip an attribute if that attribute is unknown. Unlike `skip_on_lang`, function checks whether the attribute is known or not locally so usage of this wouldn't exceed the ratelimit in case there are lots of unknown attributes requested.
Changes
- Renamed `requestedAttributes` argument to `attributes`.
- Made `attributes` argument non-required, and it now defaults to all production-ready attributes (`perspective.Attributes.Production`).
Bug fixes
- Fixed `UnknownAttributeError` exception when `perspective.Attributes.IDENTITY_ATTACK` and/or `perspective.Attributes.THREAT` is used in `attributes` argument of `analyze` function (there were minor spelling mistakes in those attributes' names).
Code changes
- Moved attribute classes from `main.py` to a new file named `attributes.py`. This didn't affect the functionality of the classes.

Page 1 of 2

© 2024 Safety CLI Cybersecurity Inc. All Rights Reserved.