Skip to content

Latest commit

 

History

History
173 lines (139 loc) · 3.84 KB

py-snippets.md

File metadata and controls

173 lines (139 loc) · 3.84 KB

Python Snippets

Debug

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

    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

    from enum import Enum, auto
    class DbgCmdType(Enum):
      Standalone = auto()
      Toggle     = auto()
      IntRange   = auto()
      FloatRange = auto()
      String     = auto()
      ComboBox   = auto()
  • iterate over enums

    ((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
    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

    original = [('a', 1), ('b', 2), ('c', 3), ('d', 4)]
    zip(*original) => (['a', 'b', 'c', 'd'], [1, 2, 3, 4])
  • unzip list of Tuples:

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

    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
    # 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

    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

    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

    import sys
    sys.version_info
    print(sys.version)
    print(sys.executable)
    import os
    print(os.__file__)

Path Manipulations

  • convert to relative

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

    Path('../mydir').absolute()
  • convert to forward slashes only

    Path(...).as_posix()
  • join paths together

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

Directory