Skip to content

Python Snippets#

Debug#

  • debug module imports and show what is importing the current module

    Python
    import inspect
    print(inspect.getframeinfo(inspect.getouterframes(inspect.currentframe())[1][0])[0])
    

  • if it says <importlib frozen>, then it means the import came from importlib.reload(..)

Enums#

  • declare

    Python
    from enum import Enum, auto
    class DbgCmdType(Enum):
      Standalone = auto()
      Toggle     = auto()
      IntRange   = auto()
      FloatRange = auto()
      String     = auto()
      ComboBox   = auto()
    

  • iterate over enums

    Python
    ((targetTypeEnum.name,targetTypeEnum.value) for targetTypeEnum in UE4TargetType)
    

Type Checking#

  • exact: type(py_obj) is list
  • inheritance Checking: isinstance(py_obj, SWidget)

Dictionary#

  • `dict.get(key\[, default\])\: get with optional default fallback
  • merge dictionary
    Python
    x = dict(a=1, b=2)
    y = dict(b=3, d=4)
    z = {**x, **y}
    # z := {'a': 1, 'b': 3, 'd': 4}, note that value for `b` is taken from the latter dict.
    

Lists#

  • unzip or transpose list of tuples

    Python
    original = [('a', 1), ('b', 2), ('c', 3), ('d', 4)]
    zip(*original) => (['a', 'b', 'c', 'd'], [1, 2, 3, 4])
    

  • unzip list of Tuples:

    Python
    zipper_list = [(1, 'a'), (2, 'b'), (3, 'c')]
    list_a, list_b = zip(*zipper_list)
    

  • flatten list

    Python
    a_list = [[1, 2], [3, 4], [5, 6]]
    print(list(itertools.chain.from_iterable(a_list))) # Output: [1, 2, 3, 4, 5, 6]
    print(list(itertools.chain(*a_list))) # Output: [1, 2, 3, 4, 5, 6]
    [*[1,2],*[3]] # Output: [1,2,3]
    

List Comprehensions#

  • nested list comprehensions
    Python
    # Just think in for-loops syntax. So, If I used for loops for the previous flattening, I’d do something like:
    [y for x in non_flat for y in x]
    # equiv to:
    for x in non_flat:
      for y in x:
        y
    

Environment#

  • Registry updating
Python
from winregistry import WinRegistry
reg = WinRegistry()
regPath = r"HKLM\System\CurrentControlSet\Control\Session Manager\Environment"
return reg.read_value(regPath, envVarName)['data']
regPath = r"HKCU\Environment"
reg.write_value(regPath, envVarName, value, valTypeStr)['data']
  • get or set environment variables on windows
Python
klcommon.getSysEnvVar(envVarName)
klcommon.setSysEnvVar(envVarName, value, valTypeStr='REG_EXPAND_SZ')
klcommon.getUserEnvVar(envVarName)
klcommon.setUserEnvVar(envVarName, value, valTypeStr='REG_EXPAND_SZ')
  • Python interpreter version number
Python
import sys
sys.version_info
print(sys.version)
print(sys.executable)
import os
print(os.__file__)

Path Manipulations#

  • convert to relative

    Python
    ue4_asset_dir = '/Game/' + Path(os.path.relpath(capself.animAssetDirPicker.get_directory(), ue.get_content_dir()))
    

  • normalize path

    Python
    Path('../mydir').absolute()
    

  • convert to forward slashes only

    Python
    Path(...).as_posix()
    

  • join paths together

    Python
    Path('/engine')  / Path('/content')
    

Directory#


Last update: 2022-09-07