A "Best of the Best Practices" (BOBP) guide to developing in Python.
- "Build tools for others that you want to be built for you." - Kenneth Reitz
- "Simplicity is alway better than functionality." - Pieter Hintjens
# check if job exists | |
curl -XGET 'http://jenkins/checkJobName?value=yourJobFolderName' --user user.name:YourAPIToken | |
# with folder plugin | |
curl -s -XPOST 'http://jenkins/job/FolderName/createItem?name=yourJobName' --data-binary @config.xml -H "Content-Type:text/xml" --user user.name:YourAPIToken | |
# without folder plugin | |
curl -s -XPOST 'http://jenkins/createItem?name=yourJobName' --data-binary @config.xml -H "Content-Type:text/xml" --user user.name:YourAPIToken | |
# create folder |
#!/bin/bash | |
# bash generate random alphanumeric string | |
# | |
# bash generate random 32 character alphanumeric string (upper and lowercase) and | |
NEW_UUID=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1) | |
# bash generate random 32 character alphanumeric string (lowercase only) | |
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1 |
Beautiful is better than ugly. Explicit is better than implicit.
I frequently deal with collections of things in the programs I write. Collections of droids, jedis, planets, lightsabers, starfighters, etc. When programming in Python, these collections of things are usually represented as lists, sets and dictionaries. Oftentimes, what I want to do with collections is to transform them in various ways. Comprehensions is a powerful syntax for doing just that. I use them extensively, and it's one of the things that keep me coming back to Python. Let me show you a few examples of the incredible usefulness of comprehensions.
All of the tasks presented in the examples can be accomplished with the extensive standard library available in Python. These solutions would arguably be more terse and efficient in some cases. I don't have anything against the standard library. To me there is a certain
heat_template_version: '2013-05-23' | |
outputs: | |
public_ip: | |
description: public IP address of the deployed compute instance | |
value: | |
get_attr: [compute_instance, accessIPv4] | |
parameters: | |
count: {default: 1, type: number} | |
flavor: {default: 512MB Standard Instance, type: string} | |
image: {default: 7fd88211-ce43-41bf-9a85-e73df4066581, type: string} |
Latency Comparison Numbers | |
-------------------------- | |
L1 cache reference 0.5 ns | |
Branch mispredict 5 ns | |
L2 cache reference 7 ns 14x L1 cache | |
Mutex lock/unlock 25 ns | |
Main memory reference 100 ns 20x L2 cache, 200x L1 cache | |
Compress 1K bytes with Zippy 3,000 ns | |
Send 1K bytes over 1 Gbps network 10,000 ns 0.01 ms | |
Read 4K randomly from SSD* 150,000 ns 0.15 ms |
app.filter 'displayBytes', () -> | |
return (bytes, precision) -> | |
if isNaN(parseFloat(bytes)) || !isFinite(bytes)) | |
return '-' | |
if typeof precision == 'undefined' | |
precision = 1 | |
units = ['bytes', 'kB', 'MB', 'GB', 'TB', 'PB'] | |
number = Math.floor(Math.log(bytes) / Math.log(1024)) | |
return (bytes / Math.pow(1024, Math.floor(number))).toFixed(precision) + ' ' + units[number] |
1. Two space soft indents (fake tabs) OR tabs... BUT NEVER BOTH - DO NOT MIX | |
2. Whitespace, Parens, Braces, Linebreaks | |
if/else/for/while/try always have spaces, braces and multiple lines. | |
-------------------------------------------------------------------- | |
ffprobe -v quiet -print_format json -show_format -show_streams "lolwut.mp4" > "lolwut.mp4.json" |
1. Two space soft indents (fake tabs) OR tabs... BUT NEVER BOTH - DO NOT MIX | |
2. Whitespace, Parens, Braces, Linebreaks | |
if/else/for/while/try always have spaces, braces and multiple lines. | |
-------------------------------------------------------------------- | |