Created
January 18, 2011 02:32
-
-
Save darkhelmet/783894 to your computer and use it in GitHub Desktop.
My talk about python compared to ruby for YEGRB Jan 18 2011.
This file contains 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
from colorama import init, Fore, Back | |
import textwrap | |
import sys | |
import re | |
import ctypes | |
init() | |
def pause(): | |
sys.stdin.readline() | |
def section(name, extra = None): | |
print Fore.GREEN + name + Fore.RESET | |
if extra: | |
print Fore.RED + "\t" + "\n\t".join(textwrap.wrap(extra, 80)) + Fore.RESET | |
pause() | |
def point(text): | |
print "\t- " + text | |
pause() | |
def examples(*args): | |
for a in args: | |
ex = "\t%s" % a | |
print(Fore.CYAN + ex + Fore.RESET) | |
print('') | |
def code(text): | |
stmts = text.split(';') | |
outputs = [] | |
for stmt in stmts: | |
match = re.match("\s*(?P<variable>\w+)\s*=\s*(?P<rest>.*)", stmt) | |
if match: | |
setattr(sys.modules[__name__], match.group('variable'), eval(match.group('rest'))) | |
else: | |
outputs.append(str(eval(stmt))) | |
code = "\t%s # => %s\n" % (text, '; '.join(outputs)) | |
print(Fore.CYAN + code + Fore.RESET) | |
section('Python, from a ruby perspective', 'Daniel Huckstep, January 18, 2011, YEGRB') | |
section('Introduction', 'What is python?') | |
point("Been around since 1991 (vs ruby's 1995')") | |
point('Dynamic, scripting language') | |
point('Object oriented') | |
point('Functional') | |
point('Reflective') | |
point('Influenced by ALGOL and ABC (ruby influenced by Smalltalk and Perl)') | |
section('Ruby stuff', 'Some things like ruby.') | |
point('Array slicing') | |
code('a = [1,2,3]; a[1:2]') | |
point('Duck typing') | |
point('Metaclasses') | |
point('Complex numbers...BUILT IN!') | |
code('c = 1+3j; c.__class__') | |
point('Sets...BUILT IN!') | |
code('s = {1,2,3}; str(s); s.__class__; s.add(2); str(s); s.add(4); str(s)') | |
point('Arrays (called lists)') | |
point('C Extensions') | |
point('FFI (ctypes)') | |
code("libc = ctypes.cdll.LoadLibrary('libc.dylib'); libc.printf; libc.time(None)") | |
point('Default arguments') | |
point('nil (None)') | |
point('IronPython (IronRuby)') | |
section('Cool stuff', 'Python has some pretty sweet things.') | |
point('lambda') | |
code('a = [2,6,1,3,5,9]; sorted(a, key=lambda i: -i)') | |
point('Generators') | |
code('g = (x ** 2 for x in range(10)); g.next(); g.next(); g.next()') | |
point('List comprehensions') | |
code('[x ** 2 for x in range(10) if x % 2 == 0]') | |
point('Modules (import)') | |
examples('import foo', 'import foo as bar', 'from foo import baz', 'from foo import *') | |
point('Crazy introspection: http://code.activestate.com/recipes/474088-tail-call-optimization-decorator/') | |
point('Raw strings') | |
code("'foo\\tbar'; r'foo\\tbar'") | |
point('Tuples') | |
code('t = (1,2,3); t.__class__; t[1]') | |
point('Method decorators') | |
examples('@require_login', 'def get_resource():', ' ....') | |
point('One way of doing things') | |
point('PEPs (Python Enhancement Proposal)') | |
point('PEP8: http://www.python.org/dev/peps/pep-0008/') | |
point('help() in REPL') | |
point('Massive standard library') | |
point('NumPy/SciPy') | |
point('import this') | |
point('Docstrings') | |
examples('def foo():', ' """Calculate the fizzbit of the widget"""') | |
section('Not so cool stuff', 'Just wrong...maybe') | |
point('lambdas are single expression. NO BLOCKS!') | |
point('Some weird syntax and API choices') | |
code("a = ['a','b','c']; ', '.join(a)") | |
point('Silly string interpolation...BUT: http://code.activestate.com/recipes/502257-yet-another-string-interpolation-function/') | |
point('Passing self everywhere') | |
examples('def foo(self, a, b, c):') | |
point('@staticmethod') | |
examples('@staticmethod', 'def foo(a, b, c):') | |
point('Whitespace aware') | |
point('Regex not builtin') | |
examples('import re') |
This file contains 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
Python, from a ruby perspective | |
Daniel Huckstep, January 18, 2011, YEGRB | |
Introduction | |
What is python? | |
- Been around since 1991 (vs ruby's 1995') | |
- Dynamic, scripting language | |
- Object oriented | |
- Functional | |
- Reflective | |
- Influenced by ALGOL and ABC (ruby influenced by Smalltalk and Perl) | |
Ruby stuff | |
Some things like ruby. | |
- Array slicing | |
a = [1,2,3]; a[1:2] # => [2] | |
- Duck typing | |
- Metaclasses | |
- Complex numbers...BUILT IN! | |
c = 1+3j; c.__class__ # => <type 'complex'> | |
- Sets...BUILT IN! | |
s = {1,2,3}; str(s); s.__class__; s.add(2); str(s); s.add(4); str(s) # => set([1, 2, 3]); <type 'set'>; None; set([1, 2, 3]); None; set([1, 2, 3, 4]) | |
- Arrays (called lists) | |
- C Extensions | |
- FFI (ctypes) | |
libc = ctypes.cdll.LoadLibrary('libc.dylib'); libc.printf; libc.time(None) # => <_FuncPtr object at 0x1006091f0>; 1295403908 | |
- Default arguments | |
- nil (None) | |
- IronPython (IronRuby) | |
Cool stuff | |
Python has some pretty sweet things. | |
- lambda | |
a = [2,6,1,3,5,9]; sorted(a, key=lambda i: -i) # => [9, 6, 5, 3, 2, 1] | |
- Generators | |
g = (x ** 2 for x in range(10)); g.next(); g.next(); g.next() # => 0; 1; 4 | |
- List comprehensions | |
[x ** 2 for x in range(10) if x % 2 == 0] # => [0, 4, 16, 36, 64] | |
- Modules (import) | |
import foo | |
import foo as bar | |
from foo import baz | |
from foo import * | |
- Crazy introspection: http://code.activestate.com/recipes/474088-tail-call-optimization-decorator/ | |
- Raw strings | |
'foo\tbar'; r'foo\tbar' # => foo bar; foo\tbar | |
- Tuples | |
t = (1,2,3); t.__class__; t[1] # => <type 'tuple'>; 2 | |
- Method decorators | |
@require_login | |
def get_resource(): | |
.... | |
- One way of doing things | |
- PEPs (Python Enhancement Proposal) | |
- PEP8: http://www.python.org/dev/peps/pep-0008/ | |
- help() in REPL | |
- Massive standard library | |
- NumPy/SciPy | |
- import this | |
- Docstrings | |
def foo(): | |
"""Calculate the fizzbit of the widget""" | |
Not so cool stuff | |
Just wrong...maybe | |
- lambdas are single expression. NO BLOCKS! | |
- Some weird syntax and API choices | |
a = ['a','b','c']; ', '.join(a) # => a, b, c | |
- Silly string interpolation...BUT: http://code.activestate.com/recipes/502257-yet-another-string-interpolation-function/ | |
- Passing self everywhere | |
def foo(self, a, b, c): | |
- @staticmethod | |
@staticmethod | |
def foo(a, b, c): | |
- Whitespace aware | |
- Regex not builtin | |
import re |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment