Neither one nor Many
Software engineering blog about my projects, geometry, visualization and music.
import code
code.interact(local=locals())
Or using IPython:
import IPython
IPython.embed()
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
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())
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 = open("testfile.txt","w")
file.write("Hello World")
file.close()
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')
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)
For in base class:
import abc
@abc.abstractmethod
def init(self):
return
import os
import stat
st = os.stat('somefile')
os.chmod('somefile', st.st_mode | stat.S_IEXEC)
import textwrap
textwrap.dedent('''\
#!/bin/bash
set -ex
sleep 10
echo Failure is the mother of success.
''')
str()
-> !s
repr
-> !r
f"kubectl still returned data for namespace: {json!s}, expected NotFoundException"
f"kubectl still returned data for namespace: {json!r}, expected NotFoundException"
pip install pyyaml
1 import yaml 2 3 with open("/etc/kubernetes/kubelet.kubeconfig") as f: 4 p = yaml.load(f) 5 print(p['clusters'][0]['name'])
from datetime import datetime
datetime.strftime(datetime.now(), "%Y-%m-%d %H:%M:%S")