Github link: shared.python.utils
Welcome to the second post on python utils. Lets get to it, shall we?
empty_list()
To make an empty list with a specific number of items looks like this:
# generates list full of None values: empty_list = [] * number_of_items
While its not terrible, its not great either. I made this function so I could fill the list with something specific. Like so:
empty_list = pyutils.empty_list(number_of_items=0, default_item=True)
time_it() and count_it()
These two are useful decorators. What is a decorator you ask? Think of it as writing a generic wrapper for a function, but you leave the function variable. If you want a step by step on how to make decorators, please visit my decorator discussion page.
get_sorted_by_most_common()
I suppose this is where we get specific to problems I’ve had. I needed to sort all items in a list by how many items there are. for example:
data = get_sorted_by_most_common( [ "Mouth", "Corner", "Up", "Mouth", "Up", "Mouth", "Up", "Mouth", "Up", "Mouth", "Corner", "Up" ] ) # result: # ["Mouth", "Up", "Corner"]
I’m aware this is not a common thing, but hey, if you ever have a similar issue, here it is.
That’s mostly it. Please ask questions if you have any. Leave a comment. Let me know how I’m doing. Thank you all.
//Isoparm