Friday, 26 November 2021

typing.Callable

 

Callable type; Callable[[int], str] is a function of (int) -> str.

The subscription syntax must always be used with exactly two values: the argument list and the return type. The argument list must be a list of types or an ellipsis; the return type must be a single type.

There is no syntax to indicate optional or keyword arguments; such function types are rarely used as callback types. Callable[..., ReturnType] (literal ellipsis) can be used to type hint a callable taking any number of arguments and returning ReturnType. A plain Callable is equivalent to Callable[..., Any], and in turn to collections.abc.Callable.

Callables which take other callables as arguments may indicate that their parameter types are dependent on each other using ParamSpec. Additionally, if that callable adds or removes arguments from other callables, the Concatenate operator may be used. They take the form Callable[ParamSpecVariable, ReturnType] and Callable[Concatenate[Arg1Type, Arg2Type, ..., ParamSpecVariable], ReturnType] respectively.

Deprecated since version 3.9: collections.abc.Callable now supports []. See PEP 585 and Generic Alias Type.

Changed in version 3.10: Callable now supports ParamSpec and Concatenate. See PEP 612 for more information.

 

from:  https://docs.python.org/3/library/typing.html#typing.Callable

Tuesday, 23 November 2021

Python - object MagicMock can't be used in 'await' expression

 You can get mocks to return objects that can be awaited by using a Future. The following is a pytest test case, but something similar should be possible with unittest.

async def test_that_mock_can_be_awaited():
    mock = MagicMock(return_value=Future())
    mock.return_value.set_result(123)
    result = await mock()
    assert result == 123

In your case, since you're patching Service (which gets passed in as mock), mock.return_value = Future() should do the trick.


from: https://stackoverflow.com/questions/51394411/python-object-magicmock-cant-be-used-in-await-expression

Friday, 15 October 2021

partial string formatting

 s = '{foo} {bar}'

s.format(foo='FOO', bar='{bar}')


from: https://stackoverflow.com/questions/11283961/partial-string-formatting

Saturday, 9 October 2021

How to check if one dictionary is a subset of another larger dictionary?

 

In Python 3, you can use dict.items() to get a set-like view of the dict items. You can then use the <= operator to test if one view is a "subset" of the other:

d1.items() <= d2.items()

In Python 2.7, use the dict.viewitems() to do the same:

d1.viewitems() <= d2.viewitems()

In Python 2.6 and below you will need a different solution, such as using all():

all(key in d2 and d2[key] == d1[key] for key in d1)

 

from:  https://stackoverflow.com/questions/9323749/how-to-check-if-one-dictionary-is-a-subset-of-another-larger-dictionary

Friday, 1 October 2021

Sort JSON object using json.dumps()

 # Import the JSON module

import json

# Array of JSON Objects
products = [{"name": "HDD", "brand": "Samsung", "price": 100},
            {"name": "Monitor", "brand": "Dell", "price": 120},
            {"name": "Mouse", "brand": "Logitech", "price": 10}]

# Read and print the original data
print("The original data:\n{0}".format(products))
# Convert into the JSON object after sorting
sorted_json_data = json.dumps(products, sort_keys=True)
# Print the sorted JSON data
print("The sorted JSON data based on the keys:\n{0}".format(sorted_json_data))


from: https://linuxhint.com/sort-json-objects-python/

Thursday, 16 September 2021

How to convert Nonetype to int or string?

 int(value or 0)


from: https://stackoverflow.com/questions/3930188/how-to-convert-nonetype-to-int-or-string

how to dynamically create an instance of a class in python?

 all you will need to do is

klass = globals()["class_name"]
instance = klass()


from: https://stackoverflow.com/questions/3451779/how-to-dynamically-create-an-instance-of-a-class-in-python