Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Best Place for Generic Functions

I am writing a flask application, and I have found I have have ton of generic utility functions.

Here are the examples of the type of functions that I consider generic utility functions:

def make_hash():
  return defaultdict(make_hash)

def file_read(filename):
  with open(file_name_, 'r') as f:
    return f.read()

def file_write(filename, data):
  with open(filename, 'w') as f:
    f.write(data)

I was thinking of tossing these functions into a separate module all together. However, I am curious if I have the following concerns:

  • There are two few unique functions to warrant a separate module all together. i.e. the file_read, and file_write functions above could go into a file.py module, however since its two functions, I feel like this might be overkill.
  • In my application I use these functions 2-3 times per function so I am moving under the guise they creating these utility functions should help me save some lines of code, and hopefully is making me more efficient.

Question: - What would be the pythonic way of grouping generic utility functions? Should I create a separate module? Curious what others are doing for organizing this type of code.

like image 904
fr00z1 Avatar asked Apr 29 '13 09:04

fr00z1


People also ask

Can functions be generic in Python?

There is a term for this. It is called “Generic Function”. Python recently added support for generic function in Python 3.4 (PEP 443). They did this to the functools module by adding @singledispatch decorator.

What is Singledispatch in Python?

In python 3.8, there is another decorator for methods called singledispatchmethod . This decorator will transform your regular function into a single dispatch generic function. A generic function is composed of multiple functions implementing the same operation for different types.

What is the purpose of a generic function?

A generic function is simply a function that performs a common task by dispatching its input to a particular method-function that is selected on the basis of the class of the input to the generic function. Languages that use generic functions are said to support generic-function OO.


1 Answers

I don't think it has much relation to Python, it's more a design decision.

For only these lines I would not make a separate module; however, if you use it 2 to 3 times I would not copy the code. If you later want to change something only one change is needed, keeping functionality consistent.

Also the methods seem to be very generic, so you could easily use them in other projects later.

And I assume you want to make them static (@static_method).

What I mostly do is group generic utility classes by type, i.e. in your case one file for dictionaries (having 1 method) and one for file (having 2 methods). Later possibly more methods will be added but the functionality is grouped by type/usage.

like image 117
Michel Keijzers Avatar answered Nov 15 '22 19:11

Michel Keijzers