Package: tricky
tricky - that's not about python.This Python module named "tricky" is a wonderful collection of handy and versatile utilities designed to assist you in your routine Python programming tasks. With a wide range of convenient functions that operate mostly on iterables and types, your Python experience will become smoother and more efficient.OverviewThetrickymodule is mainly composed of:Iterables Module(tricky.iterables): This sub-module is designed to handle operations and manipulations on iterable objects cleverly.Typing(tricky.typing): This sub-module provides a more expressive typing system, to deal with items of specific types.Examples:IterablesExample ofiterables.filter_itemThis function will iterate through a collection of items
and return the first one that meets the specified condition.
If no such item is found, it will return a default value.importtypingastfromtricky.typingimportIntegerfromtricky.iterablesimportfilter_itemnumbers=range(1000)result:t.Optional[Integer]=filter_item(numbers,# the iterablelambdanumber:number==342,# condition to get your itemNone,# the default value to return, if condition not met)print(result)# 342Example ofiterables.remove_values_from_iterableThis function will create a new list from 'initial_values', excluding any that are in 'values_to_remove'.importtypingastfromtricky.iterablesimportremove_values_from_iterablefromtricky.typingimportIntegerinitial:t.List[Integer]=[1,2,3,4,5,6,7,8,9,10]remove:t.List[Integer]=[2,5,7]# We want to remove the numbers 2, 5, and 7 from our initial list.result=remove_values_from_iterable(initial,remove)print(result)# Output: [1, 3, 4, 6, 8, 9, 10]Example ofiterables.are_there_duplicatesThis function are_there_duplicates checks within a sequence if there are any duplicate items.
It iterates over all elements in the given sequence and for each element,
it checks if the same element exists in the remaining sequence or not.
If the element exists, it immediately returns True indicating there are duplicates,
else if it traversed the entire sequence and found no duplicates, it returns False.importtypingastfromtricky.iterablesimportare_there_duplicatesfromtricky.typingimportIntegerhas_no_duplicates:t.List[Integer]=[1,2,3,4,5]has_duplicates:t.List[Integer]=[1,2,1,4,5]print(are_there_duplicates(has_no_duplicates))# Output: False, as there are no duplicates.print(are_there_duplicates(has_duplicates))# Output: True, as 1 is repeated.Example ofiterables.unpack_valuesThe function unpacks the value of a given attribute from a collection of objects.
It checks each object if it has the said attribute through hasattr method, and if it does,
it retrieves the value via getattr method.
The results are then accumulated in a list, which is the returned output.importtypingastfromtricky.iterablesimportunpack_valuesclassSomeClass:def__init__(self,a,b):self.a=aself.b=biterable:t.List[SomeClass]=[SomeClass(1,2),SomeClass(3,4),SomeClass(5,6)]attribute='a'# Attribute to extractprint(unpack_values(iterable,attribute))# Outputs [1, 3, 5]Example ofiterables.unpack_values_by_conditionThe function returns a list of values.
Each value comes from an object from the iterable parameter,
and specifically from the attribute of that object corresponding to the attribute parameter.importtypingastfromtricky.iterablesimportunpack_values_by_conditionfromtricky.typingimportIntegerclassSimple:def__init__(self,value):self.some_attr=value# Create a list of Simple objects, where each object has a value attribute from 1 to 5objects:t.List[Simple]=[Simple(i)foriinrange(1,6)]# Lookup attributelookup_attribute='some_attr'# Condition that checks if the value attribute of the object is greater than 3condition=(lambdaobj:obj.attribute>3)# Use the function to get values greater than 3values:t.List[Integer]=unpack_values_by_condition(objects,lookup_attribute,condition)print(values)# It should output: [4, 5]Example ofiterables.unzipThe unzip function in your code is essentially a flatten function.
Specifically, it takes in an arbitrary number of iterable collections (list, tuple, etc.)
as arguments, and consolidates all the items in these collections into a single, flat list.importtypingastfromtricky.iterablesimportunzipfromtricky.typingimportInteger,Stringnumbers:t.List[Integer]=[1,2,3,4]string:t.List[String]=['apple','banana','cherry']print(unzip(numbers))# Output: [1, 2, 3, 4, 'apple', 'banana', 'cherry']Typing | tricky.typingAn example of a simple use of aTypedList:fromtricky.typingimportTypedList,Integernumbers=TypedList[Integer](1,2,3,4,5)assertisinstance(numbers,(list,TypedList))# TrueBut if an element with a different type is passed to the list, an exception will be thrown:fromtricky.typingimportTypedList,Integernumbers=TypedList[Integer](1,2,3,'string',5)# ValueError: Passed item "string" of sequence has type <class 'str'>, but annotated type is <class 'int'>An example of a simple use of aAnnotatedStringfromtricky.typingimportAnnotatedStringexpecting_value='example'annotated_string=AnnotatedString['example'](expecting_value)assertisinstance(AnnotatedString['example'](expecting_value),(str,AnnotatedString))But if the annotated value does not match the one passed, an exception will be thrownfromtricky.typingimportAnnotatedStringbad_value='bad_value'annotated_string=AnnotatedString['example'](bad_value)# ValueError: Annotated and passed values are not equal 'bad_value' != 'example'