Skip to content

Instantly share code, notes, and snippets.

@arcnavier
Last active April 25, 2018 20:47
Show Gist options
  • Save arcnavier/103f9073f8b14e686123bca9c14c9b7d to your computer and use it in GitHub Desktop.
Save arcnavier/103f9073f8b14e686123bca9c14c9b7d to your computer and use it in GitHub Desktop.

Python: functional programming

all(iterable)
any(iterable)
map(function, iterable, ...)
zip(*iterables)
filter(function, iterable)
itertools.izip(*iterables)
itertools.takewhile(predicate, iterable)
functools.reduce(function, iterable[, initializer])
>>> map(lambda x : x * x, [1, 2, 3, 4, 5])
[1, 4, 9, 16, 25]
>>> reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
15
>>> filter(lambda x : x % 2 == 0, [1, 2, 3, 4, 5])
[2, 4]

Python: Duck Typing

"It requires that type checking be deferred to runtime, and is implemented by means of dynamic typing or reflection"

"If it looks like a duck and quacks like a duck, it’s a duck"

class Parrot:
    def fly(self):
        print("Parrot flying")

class Airplane:
    def fly(self):
        print("Airplane flying")

class Whale:
    def swim(self):
        print("Whale swimming")

def lift_off(entity):
    entity.fly()

parrot = Parrot()
airplane = Airplane()
whale = Whale()

lift_off(parrot) # prints `Parrot flying`
lift_off(airplane) # prints `Airplane flying`
lift_off(whale) # Throws the error `'Whale' object has no attribute 'fly'`

From: Wikipedia

Dependency Management

Reuseable code from anyone, easy to install.

npm
$ npm install express

npm notice created a lockfile as package-lock.json. You should commit this file.

+ express@4.16.3
added 50 packages from 47 contributors in 2.669s
var express = require('express');
var app = express();

app.get('/', function(req, res) {
  res.send('Hello World!');
});

app.listen(3000, function() {
  console.log('Example app listening on port 3000!');
});
alt
$ pip install requests
Collecting requests
  Downloading https://files.pythonhosted.org/packages/49/df/50aa1999ab9bde74656c2919d9c0c085fd2b3775fd3eca826012bef76d8c/requests-2.18.4-py2.py3-none-any.whl (88kB)
    100% |████████████████████████████████| 92kB 1.1MB/s
Collecting urllib3<1.23,>=1.21.1 (from requests)
  Downloading https://files.pythonhosted.org/packages/63/cb/6965947c13a94236f6d4b8223e21beb4d576dc72e8130bd7880f600839b8/urllib3-1.22-py2.py3-none-any.whl (132kB)
    100% |████████████████████████████████| 133kB 6.2MB/s
Collecting idna<2.7,>=2.5 (from requests)
  Downloading https://files.pythonhosted.org/packages/27/cc/6dd9a3869f15c2edfab863b992838277279ce92663d334df9ecf5106f5c6/idna-2.6-py2.py3-none-any.whl (56kB)
    100% |████████████████████████████████| 61kB 6.9MB/s
Collecting chardet<3.1.0,>=3.0.2 (from requests)
  Downloading https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)
    100% |████████████████████████████████| 143kB 6.1MB/s
Collecting certifi>=2017.4.17 (from requests)
  Downloading https://files.pythonhosted.org/packages/7c/e6/92ad559b7192d846975fc916b65f667c7b8c3a32bea7372340bfe9a15fa5/certifi-2018.4.16-py2.py3-none-any.whl (150kB)
    100% |████████████████████████████████| 153kB 7.9MB/s
Installing collected packages: urllib3, idna, chardet, certifi, requests
Successfully installed certifi-2018.4.16 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22
>>> import requests
>>> r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
>>> r.status_code
200
>>> r.headers['content-type']
'application/json; charset=utf8'
>>> r.encoding
'utf-8'
>>> r.text
u'{"type":"User"...'
>>> r.json()
{u'private_gists': 419, u'total_private_repos': 77, ...}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment