Skip to content

Instantly share code, notes, and snippets.

@coderofsalvation
Last active October 20, 2022 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coderofsalvation/5f1d6711b0548448dc4ff6d811bb819a to your computer and use it in GitHub Desktop.
Save coderofsalvation/5f1d6711b0548448dc4ff6d811bb819a to your computer and use it in GitHub Desktop.
python patterns

Useful Python Patterns/Functions NOTE: for 3D or lua JS patterns see my other patterns-gists 3D patterns-page

easy debugging/printing using pdb

import pdb

# somewhere in your code
pdb_settrace()

hint: install pip install pdbpp for extras (tab-completion etc)

python decorators

def decorators(*args, **kwargs):
    def inner(func):
        '''
           do operations with func
        '''
        return func
    return inner #this is the fun_obj mentioned in the above content
 
@decorators(params)
def func():
    """
         function implementation
    """

basically equals:

func = (decorator(params))(func)
"""

defaultdict

from collections import defaultdict
  
  
# Function to return a default
# values for keys that is not
# present
def def_value():
    return "Not Present"
      
# Defining the dict
d = defaultdict(def_value)
d["a"] = 1
d["b"] = 2
  
print(d["a"])
print(d["b"])
print(d["c"])

itertools, permutations, sets (venn,intersection,union)

[format(x, '.2f') for x in accumulate(inputs, logistic_map)]
['0.40', '0.91', '0.30', '0.81', '0.60', '0.92', '0.29', '0.79', '0.63',
 '0.88', '0.39', '0.90', '0.33', '0.84', '0.52', '0.95', '0.18', '0.57',
 '0.93', '0.25', '0.71', '0.79', '0.63', '0.88', '0.39', '0.91', '0.32',
 '0.83', '0.54', '0.95', '0.20', '0.60', '0.91', '0.30', '0.80', '0.60']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment