Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View pykong's full-sized avatar
🎯
Focusing

Ben Felder pykong

🎯
Focusing
View GitHub Profile
import shlex
from dataclasses import dataclass
from pathlib import Path
from subprocess import check_call
from typing import Final, Optional, Sequence
IMG_EXTS: Final[Sequence[str]] = (
".png",
".jpg",
".svg",

Transforming Code into Beautiful, Idiomatic Python 3

Notes from Raymond Hettinger's talk at pycon US 2013 video, slides.

The code examples and direct quotes are all from Raymond's talk. I've reproduced them here for my own edification and the hopes that others will find them as handy as I have!

Adapted from Alen K's original transcript to Python 3.6+ syntax by myself.

Looping over a range of numbers

@pykong
pykong / open_in_sublime_merge.nemo_action
Last active December 22, 2019 16:20
Put under ~/.local/share/nemo/actions/
[Nemo Action]
Name=Open in Sublime Merge
Comment=Open in Sublime Merge
Exec=/opt/sublime_merge/sublime_merge -n %F
Icon-Name=sublime-merge
Selection=any
Extensions=dir
Quote=double
@pykong
pykong / open_in_sublime_text.nemo_action
Last active December 22, 2019 16:20
Put under ~/.local/share/nemo/actions/
[Nemo Action]
Name=Open in Sublime Text 3
Comment=Open in Sublime Text 3
Exec=subl -n %F
Icon-Name=sublime-text
Selection=any
Extensions=dir
Quote=double
Mimetypes=text/plain
Dependencies=subl
@pykong
pykong / faker.cs
Created April 27, 2018 09:58
https://github.com/diogolmenezes/Faker PM> Install-Package FakerTest
public class User
{
public string Name { get; set; }
public string Email { get; set; }
public int Age { get; set; }
public DateTime CreatedAt { get; set; }
}
// creating one fake user
var user = new Faker<User>().Create();
// create new object
contact_existing = Mapper.Map<ContactDTO, Contact>(contact_dto);
// update existing object
contact_existing = Mapper.Map<ContactDTO, Contact>(contact_dto, contact_existing);
public async Task DoWithRetryAsync(Func<Task> action, TimeSpan sleepPeriod, int tryCount = 3)
{
if (tryCount <= 0)
throw new ArgumentOutOfRangeException(nameof(tryCount));
while (true) {
try {
await action();
return; // success!
} catch {
arr = [
{"x":"8/11/2009","y":0.026572007},
{"x":"8/12/2009","y":0.025057454},
{"x":"8/13/2009","y":0.024530916},
{"x":"8/14/2009","y":0.031004457}
]
Math.max.apply(Math, arr.map(function(o) {return o.y;}))
System.Diagnostics.Debug.WriteLine("_exchangeEmitter.Start(args);");
internal static class MailExtractor
{
public static List<string> ExtractEmails(string data)
{
List<string> ExtractedEmailAddresses = new List<string>();
//instantiate with this pattern
string EmailPattern = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
Regex emailRegex = new Regex(EmailPattern, RegexOptions.IgnoreCase);
//find items that matches with our pattern