7 minutes
ES6 pour développeurs Python
Pour que le code React, et JavaScript plus généralement, ne soit pas trop moche; il faut s’inspirer de Python ahahah…
Python
1. Variables dans des strings
name = 'olivier'
value = f"""Hello, {name}!
Welcome!"""
price = 7.5
value = f"Prix: {price:.2f} €"
print(value)
# Prix: 7.50 €
2. Iterator
for item in ['A', 'B', 'C']:
print(item)
3. Sets
mon_set = set(['titi'])
mon_set.add('tata')
mon_set.add('toto')
'titi' in mon_set
len(mon_set) == 3
for elem in mon_set:
print(elem)
mon_set.remove('C')
4. Generators
def countdown(counter):
while counter > 0:
yield counter
counter -= 1
for counter in countdown(10):
print(counter)
5. Unpacking
a = 1
b = 2
a, b = b, a
first, second, *the_rest = [1, 2, 3, 4]
6. Lambda functions
sum = lambda x, y: x + y
square = lambda x: x ** 2
7. Function arguments
from pprint import pprint
def create_post(**options):
pprint(options)
def report(post_id, reason='not-relevant'):
pprint({'post_id': post_id, 'reason': reason})
def add_tags(post_id, *tags):
pprint({'post_id': post_id, 'tags': tags})
create_post(title='Hello, World!', content='')
report(42)
report(post_id=24, reason='spam')
add_tags(42, 'python', 'javascript', 'django')
8. Classes et héritage
class Post:
def __init__(self, id, title):
self.id = id
self.title = title
def __str__(self):
return self.title
class Article(Post):
def __init__(self, id, title, content):
super(Article, self).__init__(id, title)
self.content = content
class Link(Post):
def __init__(self, id, title, url):
super(Link, self).__init__(id, title)
self.url = url
def __str__(self):
return '{} ({})'.format(
super(Link, self).__str__(),
self.url,
)
article = Article(1, 'Hello, World!',
'This is my first article.'
)
link = Link(2, 'The Example', 'http://example.com')
# isinstance(article, Post) == True
# isinstance(link, Post) == True
print(link)
9. Class properties
class Post(object):
def __init__(self, id, title):
self.id = id
self.title = title
self._slug = ''
@property
def slug(self):
return self._slug
@slug.setter
def slug(self, value):
self._slug = value
post = Post(1, 'Hello, World!')
post.slug = 'hello-world'
print(post.slug)
10. Liste - tous les éléments vrais
items = [1, 2, 3]
all_truthy = all(items)
11. Liste - au moins un élément vrai
items = [0, 1, 2, 3]
some_truthy = any(items)
12. Liste - itérer sur tous les éléments
items = ['a', 'b', 'c', 'd']
for index, element in enumerate(items):
print(f'{index}: {element};')
13. Liste - Map
items = [0, 1, 2, 3]
all_doubled = list(
map(lambda x: 2 * x, items)
)
# [0, 2, 4, 6]
14. Liste - Filtrer les éléments avec une fonction
items = [0, 1, 2, 3]
only_even = list(
filter(lambda x: x % 2 == 0, items)
)
# [0, 2]
15. Liste - Reduce avec une fonction
from functools import reduce
items = [1, 2, 3, 4]
total = reduce(
lambda total, current: total + current,
items, )
# 10
16. Fusionner des dicts
d1 = {'a': 'A', 'b': 'B'}
d2 = {'a': 'AAA', 'c': 'CCC'}
merged = {**d1, **d2} # since Python 3.5
# {'a': 'AAA', 'b': 'B', 'c': 'CCC'}
17. Parse int
number = int(text)
18. One liner/ Opérateur ternaire
value = 'ADULT' if age >= 18 else 'CHILD'
19. Object attribute
attribute = 'color'
value = getattr(obj, attribute, 'GREEN')
setattr(obj, attribute, value)
20. Dictionnaire value by key
key = 'color'
value = dictionary.get(key, 'GREEN')
dictionary[key] = value
21. Slice
items = [1, 2, 3, 4, 5]
first_two = items[:2]
# [1, 2]
last_two = items[-2:]
# [4, 5]
middle_three = items[1:4]
# [2, 3, 4]
22. Opération sur les listes
items1 = ['A']
items2 = ['B']
items = items1 + items2
items.append('C')
items.insert(0, 'D')
first = items.pop(0)
last = items.pop()
items.delete(0)
23. Joining lists of strings
items = ['A', 'B', 'C']
text = ', '.join(items) # 'A, B, C'
24. JSON
import json
json_data = json.dumps(dictionary, indent=4)
dictionary = json.loads(json_data)
25. Error handling
class CustomException(Exception):
def __init__(self, message):
self.message = message
def __str__(self):
return self.message
def proceed():
raise CustomException('Error happened!')
try:
proceed()
except CustomException as err:
print(f'Sorry! {err}')
finally:
print('Finishing')
26. Import
import math
print(math.log(42))
from math import log
print(log(42))
from math import *
print(log(42))
27. Range
print(range(5))
28. Comprehensions
names = [c.name for c in friends if c.friendly]
29. Dict creation
x = 42
y = 43
mon_dict = {"x": x, "y": y}
30. String in list
my_list = ["titi", "tata", "toto"]
if "titi" in my_list:
print("ok")
ES6
1. Variables dans des strings
name = 'olivier';
value = `Hello, ${name}!
Welcome!`;
price = 7.5;
value = `Prix ${price.toFixed(2)} €`;
console.log(value);
// 'Price: 7.50
2. Iterator
for (let item of ['A', 'B', 'C']) {
console.log(item);
}
3. Sets
mon_set = new Set(['titi']);
mon_set.add('tata').add('toto');
mon_set.has('titi') === true;
mon_set.size === 3;
for (let elem of mon_set.values()) {
console.log(elem);
}
mon_set.delete('C');
4. Generators
function* countdown(counter) {
while (counter > 0) {
yield counter;
counter--;
}
}
for (let counter of countdown(10)) {
console.log(counter);
}
5. Unpacking
a = 1;
b = 2;
[a, b] = [b, a];
[first, second, ...the_rest] = [1, 2, 3, 4];
6. Lambda functions
sum = (x, y) => x + y;
square = x => Math.pow(x, 2);
7. Function arguments
function create_post(options) {
console.log(options);
}
function report(post_id, reason='not-relevant') {
console.log({post_id: post_id, reason: reason});
}
function add_tags(post_id, ...tags) {
console.log({post_id: post_id, tags: tags});
}
create_post({title: 'Hello, World!', content': ''});
report(42);
report(post_id=24, reason='spam');
add_tags(42, 'python', 'javascript', 'django');
8. Classes et héritage
class Post {
constructor (id, title) {
this.id = id;
this.title = title;
}
toString() {
return this.title;
}
}
class Article extends Post {
constructor (id, title, content) {
super(id, title);
this.content = content;
}
}
class Link extends Post {
constructor (id, title, url) {
super(id, title);
this.url = url;
}
toString() {
return super.toString() + ' (' + this.url + ')';
}
}
article = new Article(1, 'Hello, World!',
'This is my first article.'
);
link = new Link(2, 'The Example', 'http://example.com');
// article instanceof Post === true
// link instanceof Post === true
9. Class properties
class Post {
constructor (id, title) {
this.id = id;
this.title = title;
this._slug = '';
}
set slug(value) {
this._slug = value;
}
get slug() {
return this._slug;
}
}
post = new Post(1, 'Hello, World!');
post.slug = 'hello-world';
console.log(post.slug);
10. Liste - tous les éléments vrais
items = [1, 2, 3];
all_truthy = items.every(Boolean);
11. Liste - au moins un élément vrai
items = [0, 1, 2, 3];
some_truthy = items.some(Boolean);
12. Liste - itérer sur tous les éléments
items = ['a', 'b', 'c', 'd'];
items.forEach(function(element, index) {
console.log(`${index}: ${element};`);
});
13. Liste - Map
items = [0, 1, 2, 3];
all_doubled = items.map(
x => 2 * x
);
// [0, 2, 4, 6]
14. Liste - Filtrer les éléments avec une fonction
items = [0, 1, 2, 3];
only_even = items.filter(
x => x % 2 === 0
);
// [0, 2]
15. Liste - Reduce avec une fonction
items = [1, 2, 3, 4];
total = items.reduce(
(total, current) => total + current
);
// 10
16. Fusionner des dicts
d1 = {a: 'A', b: 'B'}
d2 = {a: 'AAA', c: 'CCC'}
merged = {...d1, ...d2};
// {a: 'AAA', b: 'B', c: 'CCC'}
17. Parse int
number = parseInt(text, 10);
18. One liner/ Opérateur ternaire
value = age >= 18? 'ADULT': 'CHILD';
19. Object attribute
attribute = 'color';
value = obj[attribute] || 'GREEN';
obj[attribute] = value;
20. Dictionnaire value by key
key = 'color';
value = dictionary[key] || 'GREEN';
dictionary[key] = value;
21. Slice
items = [1, 2, 3, 4, 5];
first_two = items.slice(0, 2);
// [1, 2]
last_two = items.slice(-2);
// [4, 5]
middle_three = items.slice(1, 4);
// [2, 3, 4]
22. Opération sur les listes
items1 = ['A'];
items2 = ['B'];
items = items1.concat(items2);
items.push('C');
items.unshift('D');
first = items.shift();
last = items.pop();
items.splice(0, 1);
23. Joining lists of strings
items = ['A', 'B', 'C'];
text = items.join(', '); // 'A, B, C'
24. JSON
json_data = JSON.stringify(dictionary, null, 4);
dictionary = JSON.parse(json_data);
25. Error handling
function CustomException(message) {
this.message = message;
this.toString = function() {
return this.message;
}
}
function proceed() {
throw new CustomException('Error happened!');
}
try {
proceed();
} catch (err) {
if (err instanceof CustomException) {
console.log('Sorry! ' + err);
}
} finally {
console.log('Finishing');
}
26. Import
import math from math;
console.log(math.log(42));
import { log } from math;
console.log(log(42));
import * from math;
console.log(log(42));
27. Range
console.log(Array.from(new Array(5), (x,i) => i));
28. Comprehensions
//To double check with Babel
let names = [for (c of friends) if (c.friendly) c.name]
29. Dict creation
let x = 42, y = 43
let mon_dict = {x, y}
30. String in list
let my_list = ["titi", "tata", toto"];
if (my_list.includes('toto')) {
console.log("ok");
}