Last active
January 5, 2017 18:25
-
-
Save janetriley/10501289 to your computer and use it in GitHub Desktop.
My first decorator! Translate text to Swedish Chef.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
""" | |
I wrote my first decorator following Colin Myer's PyCon2014 'Intro to Decorators' talk. Wheee! | |
""" | |
def chefalize(phrase): | |
"""convert HTML to Swedish Chef-speak | |
Cribbed from Mark Pilgrim's "Dive Into Python" - http://www.diveintopython.net/html_processing/index.html - | |
code at http://www.siafoo.net/snippet/133 | |
which was based on the classic chef.x, copyright (c) 1992, 1993 John Hagerman | |
""" | |
subs = ((r'a([nu])', r'u\1'), | |
(r'A([nu])', r'U\1'), | |
(r'a\B', r'e'), | |
(r'A\B', r'E'), | |
(r'en\b', r'ee'), | |
(r'\Bew', r'oo'), | |
(r'\Be\b', r'e-a'), | |
(r'\be', r'i'), | |
(r'\bE', r'I'), | |
(r'\Bf', r'ff'), | |
(r'\Bir', r'ur'), | |
(r'(\w*?)i(\w*?)$', r'\1ee\2'), | |
(r'\bow', r'oo'), | |
(r'\bo', r'oo'), | |
(r'\bO', r'Oo'), | |
(r'the', r'zee'), | |
(r'The', r'Zee'), | |
(r'th\b', r't'), | |
(r'\Btion', r'shun'), | |
(r'\Bu', r'oo'), | |
(r'\BU', r'Oo'), | |
(r'v', r'f'), | |
(r'V', r'F'), | |
(r'w', r'w'), | |
(r'W', r'W'), | |
(r'([a-z])[.]', r'\1. Bork Bork Bork!')) | |
def inner(phrase, *args, **kwargs): | |
for fromPattern, toPattern in subs: | |
phrase = re.sub(fromPattern, toPattern, phrase) | |
return phrase | |
return inner | |
@chefalize | |
def my_plain_function(phrase): | |
return phrase | |
if __name__ == "__main__": | |
conference_talk = """Friday 11:30 a.m.-noon | |
Decorators: A Powerful Weapon in your Python Arsenal | |
Colton Myers | |
Audience level: Intermediate | |
Category: Python Core (language, stdlib, etc.) | |
Description: | |
Decorators are an invaluable addition to anyone's arsenal of python tools and tricks. We will learn what a decorator is, how decorators are created, and then explore some of the cooler applications for decorators in our everyday projects. | |
Abstract: | |
Decorators are one of the coolest constructs in Python. Although they aren't necessary in many python scripts/applications, | |
there are certain problems that decorators solve beautifully. The average python programmer may have heard of decorators, | |
may have even used them from time to time. But many python programmers don't know how decorators work, or often even | |
how to construct them! | |
This session will be a fairly in-depth exploration of decorators: how to construct them, how they work, and | |
how to use them in your code. By the end of the session, you should have a moderate understanding of how decorators work, | |
and should be able to use them in your own projects.""" | |
print my_plain_function(conference_talk) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment