Skip to content

Instantly share code, notes, and snippets.

@victorwyee
victorwyee / 01_greeting_class.py
Created February 27, 2021 03:49
Stop Writing Classes (Jack Diederich, PyCon US 2012)
class Greeting:
def __init__(self, greeting='hello'):
self.greeting = greeting
def greet(self, name):
return f'{self.greeting}! {name}'
greeting = Greeting('hola')
print(greeting.greet('bob'))
class BankAccount
attr_reader :balance
def initialize
@balance = 0
end
def deposit amount
@balance += amount
end
# Don't do this:
for i in [0, 1, 2, 3, 4, 5]:
print(i**2)
# Do this:
# In Python 2, range returns a list.
# In Python 3, xrange is deprecated. range now returns a generator.
for i in range(6):
print(i**2)
@victorwyee
victorwyee / cleanest.sh
Last active October 28, 2025 14:02
Looping over tuples in Bash
declare -a elems=(
"a 1"
"b 2"
"c 3 word"
"d 4"
)
for elem in "${elems[@]}"; do
read -a strarr <<< "$elem" # uses default whitespace IFS
echo ${strarr[0]} ${strarr[1]} ${strarr[2]}
@victorwyee
victorwyee / singular.php
Created April 19, 2020 21:45
Setting a viewport height without stretching a featured image. #wordpress
<?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>
<div class="featured-image" style='background-image: url(" <?= get_the_post_thumbnail_url(get_the_ID(), 'full'); ?>");'></div>
<?php endif; ?>
@victorwyee
victorwyee / styles.css
Last active April 19, 2020 07:46
Prevent iframe embed from automatic inline resizing. #wordpress
/* https://github.com/WordPress/gutenberg/issues/8383 */
figure.is-embed {
position: relative;
width: 100%;
height: 0;
padding-bottom: 56.25%; /*16:9*/
}
figure.is-embed iframe {
@victorwyee
victorwyee / Main.js
Created April 15, 2020 17:15
Move a row of data from one worksheet to another based on cell value, with Google Apps Script #googledocs
/**
* Moves row of data to another spreadsheet based on criteria in column 10.
* Each spreadsheet can be a "source" spreadsheet, or the "target" spreadsheet.
* Assumes there as many spreadsheets as there are criteria values, e.g.
* if the criteria are "R", "P", and "D", then there are three spreadsheets
* named "R", "P" and "D".
* Assumes that each spreadsheet has the same structure.
*/
function onEdit(e) {
@victorwyee
victorwyee / numpy_random.ipynb
Created April 12, 2020 19:27
Generate random numbers with numpy. #numpy #synthetic #testdata
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@victorwyee
victorwyee / singular.php
Created April 12, 2020 04:33
Output shortcode inside a theme. #wordpress
<?php echo do_shortcode('[shortcode]'); ?>
@victorwyee
victorwyee / functions.php
Created April 12, 2020 04:32
PHP print to console.
function print_to_console($data) {
$output = $data;
if (is_array($output))
$output = implode(',', $output);
echo "<script>console.log('Debug Objects: " . $output . "' );</script>";
}