Neither one nor Many

 
September 22 2017

Launch debug shell in some arbitrary piece of code

import code
code.interact(local=locals())

Or using IPython:

import IPython
IPython.embed()

regex

name params with ?P<X>,'

In [18]: output = "x:3372 y:1280 screen:0 window:29360137"
In [19]: reg = re.compile(r'x:(?P<X>\d*) y:(?P<Y>\d*)')
In [20]: reg.match(output).group('X')
Out[20]: '3372'

match non-greedy by adding ? suffix

read json from file

source

import json
from pprint import pprint

with open('data.json') as f:
    data = json.load(f)

    pprint(data)

Edit for Python3:

with open('data.json', encoding='utf-8') as data_file:
    data = json.loads(data_file.read())

yapf formatter

There is a plugin for pycharm. Installed it, and also pip install yapf.

Then mv /usr/local/bin/yapf /usr/local/bin/yapf.x and created this bash script to tweak some values:

#!/bin/bash
/usr/local/bin/yapf.x --style='{based_on_style: google, column_limit: 100}' "$@" 

file writing

file = open("testfile.txt","w") 
file.write("Hello World") 
file.close() 

temp file name writing

import tempfile
tf = tempfile.NamedTemporaryFile()
temp_file_name = tf.name
tf.close()

or better:

import tempfile
with tempfile.NamedTemporaryFile(dir='/tmp', delete=False) as config:
    # temp_file_name = config.name
    config.write('sdfkjskldf')

subprocess

with exec_helpers:

with exec_helpers.Subprocess() as executor:
    apiserver = executor.check_call('module load kubernetes && kubectl config view -o ' \
                                    'jsonpath="{.clusters[0].cluster.server}"')
    if apiserver:
        return apiserver.stdout_str
raise AbortException("Could not retrieve the Kubernetes API server address")

# ret.exit_code == 0

without:

subprocess.check_output(["echo", "Hello World!"])
subprocess.check_output("exit 1", shell=True)
subprocess.check_output("ls non_existent_file; exit 0", stderr=subprocess.STDOUT, shell=True)

abstract method

For in base class:

import abc

@abc.abstractmethod
def init(self):
    return

chmod +x

import os
import stat

st = os.stat('somefile')
os.chmod('somefile', st.st_mode | stat.S_IEXEC)

dedent

import textwrap
textwrap.dedent('''\
    #!/bin/bash
    set -ex
    sleep 10
    echo Failure is the mother of success.
''')
Cheatsheets Comments (0)


Leave a Reply

Comment may not be visible immediately, because I process everything manually.**

**) I plan to automate this.., but it's on my ToDo since for ever..


Author:
Ray Burgemeestre
february 23th, 1984

Topics:
C++, Linux, Webdev

Other interests:
Music, Art, Zen