Skip to content

Commit

Permalink
Merge pull request #1 from brython-dev/master
Browse files Browse the repository at this point in the history
update
  • Loading branch information
YukkuriC authored Apr 10, 2019
2 parents 558e1a6 + 5c6ded2 commit 745eea3
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 89 deletions.
107 changes: 65 additions & 42 deletions www/gallery/sort_table_template.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!doctype html>
<html>
<head>
<meta charset="iso-8859-1">
<meta charset="utf-8">
<style>
body,td,th{
font-family:sans-serif;
Expand All @@ -26,10 +26,12 @@
background-color: #61D7A4;
padding: 4px;
}

.arrows{
cursor: default;
}
</style>

<script type="text/javascript" src="../src/brython.js"></script>
<script type="text/javascript" src="/src/brython.js"></script>
<script type="text/javascript" src="../src/brython_stdlib.js"></script>

<script type="text/python" src="show_source.py"></script>
Expand All @@ -42,14 +44,15 @@ <h2>Pays d'Europe</h2>
<thead>
<tr>
<th b-code="for field in fields:">
{field} <span b-on="click:sort">&uparrow;&downarrow;</span>
{field} <span class="arrows" b-on="click:sort">&uparrow;&downarrow;</span>
</th>
</tr>
</thead>
<tbody>
<tr b-code="for country in countries">
<td b-code="for i, item in enumerate(country):"
<td b-code="for i, item in enumerate(country[:-1]):"
class="{'num' if i >= 2 else False}">{item}</td>
<td class="num">{format(country[-1], ".2f")}</td>
</tr>
</tbody>
</table>
Expand All @@ -74,10 +77,7 @@ <h2>Pays d'Europe</h2>

order = elt.data.order[column]

if True:
key = lambda x: x[column]
else:
key = lambda x: float(x[column])
key = lambda x: x[column]

# Changing the value of attribute "countries" will trigger re-rendering
# the template
Expand All @@ -86,43 +86,66 @@ <h2>Pays d'Europe</h2>
# next time, sort this column the other way
elt.data.order[column] = 1 - order

fields = ["Nom", "Capitale", "Superficie", "Population"]

lines = [
"Allemagne", "Berlin", 357026, 81.8,
"Autriche", "Vienne", 83871, 8.40,
"Belgique", "Bruxelles", 30518, 10.91,
"Bulgarie", "Sofia", 110944, 7.50,
"Chypre", "Nicosie", 9251, 0.80,
"Danemark", "Copenhague", 43098, 5.56,
"Espagne", "Madrid", 505997, 46.15,
"Estonie", "Tallinn", 43698, 1.34,
"Finlande", "Helsinki", 338144, 5.37,
"France", "Paris", 544000, 65.07,
"Grèce", "Athènes", 131625, 11.32,
"Hongrie", "Budapest", 93029, 9.98,
"Irlande", "Dublin", 69797, 4.48,
"Italie", "Rome", 301336, 60.62,
"Lettonie", "Riga", 64589, 2.22,
"Lituanie", "Vilnius", 62678, 3.24,
"Luxembourg", "Luxembourg", 2586, 0.51,
"Malte", "La Valette", 315, 0.41,
"Pays-Bas", "Amsterdam", 41528, 16.65,
"Pologne", "Varsovie", 312685, 38.20,
"Portugal", "Lisbonne", 91946, 10.63,
"République tchèque", "Prague", 78867, 10.53,
"Roumanie", "Bucarest", 238391, 21.41,
"Royaume-Uni", "Londres", 243820, 62.43,
"Slovaquie", "Bratislava", 49034, 5.43,
"Slovénie", "Ljubljana", 20273, 2.05,
"Suède", "Stockholm", 441369, 9.41
fields = ["Nom", "Capitale", "Population", "Superficie", "Densité / km²"]

countries = [
['Albanie', 'Tirana', 3020209, 28748],
['Andorre', 'Andorra la Vella', 85458, 468],
['Arménie', 'Yerevan', 3060631, 29743],
['Autriche', 'Vienne', 8223062, 83871],
['Azerbaïdjan*', 'Bakou', 9686210, 86600],
['Biélorussie', 'Minsk', 9608058, 207600],
['Belgique', 'Bruxelles', 11239755, 30528],
['Bosnie-Herzégovine', 'Sarajevo', 3871643, 51197],
['Bulgarie', 'Sofia', 6924716, 110879],
['Croatie', 'Zagreb', 4470534, 56594],
['Chypre', 'Nicosie', 1172458, 9251],
['Tchéquie', 'Prague', 10538275, 78867],
['Danemark', 'Copenhague', 5569077, 43094],
['Estonie', 'Tallinn', 1257921, 45228],
['Finlande', 'Helsinki', 5268799, 338145],
['France', 'Paris', 66259012, 643427],
['Géorgie*', 'Tbilisi', 4935880, 69700],
['Allemagne', 'Berlin', 80996685, 357022],
['Grèce', 'Athènes', 10816286, 131957],
['Hongrie', 'Budapest', 9919128, 93028],
['Islande', 'Reykjavík', 317351, 103000],
['Irlande', 'Dublin', 4832765, 70273],
['Italie', 'Rome', 61680122, 301340],
['Kazakhstan*', 'Astana', 17948816, 2724900],
['Lettonie', 'Riga', 2165165, 64589],
['Liechtenstein', 'Vaduz', 37313, 160],
['Lituanie', 'Vilnius', 2943472, 65300],
['Luxembourg', 'Luxembourg', 520672, 2586],
['Macédoine du Nord', 'Skopje', 2091719, 25713],
['Malte', 'La Valette', 412655, 316],
['Moldavie', 'Chișinău', 3583288, 33851],
['Monaco', 'Monaco', 30508, 2],
['Monténégro', 'Podgorica', 650036, 13812],
['Pays-Bas', 'Amsterdam', 16877351, 41543],
['Norvège', 'Oslo', 5147792, 323802],
['Pologne', 'Varsovie', 38346279, 312685],
['Portugal', 'Lisbonne', 10427301, 92090],
['Roumanie', 'Bucarest', 21729871, 238391],
['Russie*', 'Moscou', 146267288, 17098242],
['Saint-Marin', 'Saint-Marin', 32742, 61],
['Serbie', 'Belgrade', 7209764, 88361],
['Slovaquie', 'Bratislava', 5443583, 49035],
['Slovénie', 'Ljubljana', 1988292, 20273],
['Espagne', 'Madrid', 47737941, 505370],
['Suède', 'Stockholm', 9723809, 450295],
['Suisse', 'Berne', 8061516, 41277],
['Turquie*', 'Ankara', 76667864, 783562],
['Ukraine', 'Kiev', 44291413, 603550],
['Royaume-Uni', 'Londres', 63742977, 243610],
['Vatican', 'Vatican', 842, 1],
]

countries = [[lines[4 * i + j] for j in range(4)]
for i in range(len(lines) // 4)]
for country in countries:
country.append(country[-2] / country[-1])

Template(document["table"], [sort]).render(fields=fields, countries=countries)
</script>

<p>* Pays dont au moins une partie se situe en Europe
</body>
</html>
1 change: 0 additions & 1 deletion www/src/Lib/browser/highlight.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import keyword
import _jsre as re

from browser import html
Expand Down
3 changes: 1 addition & 2 deletions www/src/Lib/browser/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ def parse(self, elt):
if nb_double_braces != nb_braces:
lines = [line for line in elt.text.split("\n")
if line.strip()]
text = " ".join(lines).replace('"', "&quot;")
text = 'f"""' + text + '"""'
text = 'f"""' + " ".join(lines) + '"""'
self.add_indent ("__write__(" + text + ")\n", elt)

elif hasattr(elt, "tagName"):
Expand Down
24 changes: 15 additions & 9 deletions www/src/brython.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ $B.regexIdentifier=/^(?:[\$A-Z_a-z\xAA\xB5\xBA\xC0-\xD6\xD8-\xF6\xF8-\u02C1\u02C
__BRYTHON__.implementation=[3,7,2,'dev',0]
__BRYTHON__.__MAGIC__="3.7.2"
__BRYTHON__.version_info=[3,7,0,'final',0]
__BRYTHON__.compiled_date="2019-04-09 09:22:05.193956"
__BRYTHON__.timestamp=1554794525193
__BRYTHON__.compiled_date="2019-04-09 22:21:21.675481"
__BRYTHON__.timestamp=1554841281675
__BRYTHON__.builtin_module_names=["_aio","_ajax","_base64","_binascii","_jsre","_locale","_multiprocessing","_posixsubprocess","_profile","_sre_utils","_string","_strptime","_svg","_warnings","_webworker","array","builtins","dis","hashlib","json","long_int","marshal","math","modulefinder","posix","random","unicodedata","zlib"]
;

Expand Down Expand Up @@ -2670,7 +2670,9 @@ var res1="$B.builtins.str.format('{0:' + "+
fmt+" + '}', "+expr1+")"
elts.push(res1)}else{if(parsed_fstring[i].conversion===null){expr1='$B.builtins.str.$factory('+expr1+')'}
elts.push(expr1)}}else{var re=new RegExp("'","g")
elts.push("'"+parsed_fstring[i].replace(re,"\\'")+"'")}}
var elt=parsed_fstring[i].replace(re,"\\'")
.replace("\n","\\n")
elts.push("'"+elt+"'")}}
return elts.join(' + ')}
for(var i=0;i < this.tree.length;i++){if(this.tree[i].type=="call"){
var js='(function(){throw TypeError.$factory("'+"'str'"+
Expand Down Expand Up @@ -7567,12 +7569,16 @@ return{start:start,stop:stop,step:step}}
slice.start=function(self){return self.start}
slice.step=function(self){return self.step}
slice.stop=function(self){return self.stop}
slice.indices=function(self,length){var len=$B.$GetInt(length)
if(len < 0){_b_.ValueError.$factory("length should not be negative")}
if(self.step > 0){var _len=_b_.min(len,self.stop)
return _b_.tuple.$factory([self.start,_len,self.step])}else if(self.step==_b_.None){var _len=_b_.min(len,self.stop),_start=self.start
if(_start==_b_.None){_start=0}
return _b_.tuple.$factory([_start,_len,1])}
slice.indices=function(self,length){
var $=$B.args("indices",2,{self:null,length:null},["self","length"],arguments,{},null,null)
var len=$B.$GetInt($.length)
if(len > 0){if(len < 0){_b_.ValueError.$factory("length should not be negative")}
var _start=(self.start==_b_.None)? 0 :_b_.min(len,self.start)
var _stop=(self.stop==_b_.None)? len :_b_.min(len,self.stop)
var _step=(self.step==_b_.None)? 1 :self.step
if(_start < 0){_start=_b_.max(0,_start+len)}
if(_stop < 0){_stop=_b_.max(0,_stop+len)}
return _b_.tuple.$factory([_start,_stop,_step])}
_b_.NotImplementedError.$factory(
"Error! negative step indices not implemented yet")}
slice.$factory=function(){var $=$B.args("slice",3,{start:null,stop:null,step:null},["start","stop","step"],arguments,{stop:null,step:null},null,null),start,stop,step
Expand Down
26 changes: 16 additions & 10 deletions www/src/brython_dist.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion www/src/brython_stdlib.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion www/src/py2js.js
Original file line number Diff line number Diff line change
Expand Up @@ -5644,7 +5644,9 @@ var $StringCtx = $B.parser.$StringCtx = function(context,value){
}
}else{
var re = new RegExp("'", "g")
elts.push("'" + parsed_fstring[i].replace(re, "\\'") + "'")
var elt = parsed_fstring[i].replace(re, "\\'")
.replace("\n", "\\n")
elts.push("'" + elt + "'")
}
}
return elts.join(' + ')
Expand Down
40 changes: 21 additions & 19 deletions www/src/py_range_slice.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,29 +377,31 @@ slice.$conv_for_seq = function(self, len){
return {start: start, stop: stop, step: step}
}

//slice.descriptors = {
//start: function(self){return self.start},
//step: function(self){return self.step},
//stop: function(self){return self.stop}
//}
slice.start = function(self){return self.start}
slice.step = function(self){return self.step}
slice.stop = function(self){return self.stop}

slice.indices = function (self, length) {
var len = $B.$GetInt(length)
if(len < 0){_b_.ValueError.$factory("length should not be negative")}
if(self.step > 0) {
var _len = _b_.min(len, self.stop)
return _b_.tuple.$factory([self.start, _len, self.step])
}else if(self.step == _b_.None){
var _len = _b_.min(len, self.stop),
_start = self.start
if(_start == _b_.None){_start = 0}
return _b_.tuple.$factory([_start, _len, 1])
}
_b_.NotImplementedError.$factory(
"Error! negative step indices not implemented yet")
slice.indices = function(self, length){
// This method takes a single integer argument length and computes
// information about the slice that the slice object would describe if
// applied to a sequence of length items. It returns a tuple of three
// integers; respectively these are the start and stop indices and the
// step or stride length of the slice. Missing or out-of-bounds indices
// are handled in a manner consistent with regular slices.
var $ = $B.args("indices", 2, {self: null, length: null},
["self", "length"], arguments, {}, null, null)
var len = $B.$GetInt($.length)
if(len > 0){
if(len < 0){_b_.ValueError.$factory("length should not be negative")}
var _start = (self.start == _b_.None) ? 0 : _b_.min(len, self.start)
var _stop = (self.stop == _b_.None)? len :_b_.min(len, self.stop)
var _step = (self.step == _b_.None)? 1 : self.step
if(_start < 0){_start = _b_.max(0, _start + len)}
if(_stop < 0){_stop=_b_.max(0, _stop + len)}
return _b_.tuple.$factory([_start, _stop, _step])
}
_b_.NotImplementedError.$factory(
"Error! negative step indices not implemented yet")
}

slice.$factory = function(){
Expand Down
4 changes: 2 additions & 2 deletions www/src/version_info.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
__BRYTHON__.implementation = [3, 7, 2, 'dev', 0]
__BRYTHON__.__MAGIC__ = "3.7.2"
__BRYTHON__.version_info = [3, 7, 0, 'final', 0]
__BRYTHON__.compiled_date = "2019-04-09 09:22:05.193956"
__BRYTHON__.timestamp = 1554794525193
__BRYTHON__.compiled_date = "2019-04-09 22:21:21.675481"
__BRYTHON__.timestamp = 1554841281675
__BRYTHON__.builtin_module_names = ["_aio",
"_ajax",
"_base64",
Expand Down
23 changes: 22 additions & 1 deletion www/tests/issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -2388,7 +2388,28 @@ def f():
raise Exception("should have raised NameError")
except NameError:
pass


# issue 1085
expected = [
(0, 10, 1),
(0, 10, 1),
(0, 1, 1),
(0, 1, 1),
(1, 10, 1),
(1, 10, 1),
(1, 1, 1),
(1, 1, 1)
]

tmp = (None, 1)
for i in range(8):
target = slice(
tmp[bool(i & 4)],
tmp[bool(i & 2)],
tmp[bool(i & 1)]
)
assert target.indices(10) == expected[i]

# ==========================================
# Finally, report that all tests have passed
# ==========================================
Expand Down
10 changes: 9 additions & 1 deletion www/tests/test_fstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,12 @@ def fn(l, incr):

# quote inside fstring
t = 8
assert f"'{t}px'" == "'8px'"
assert f"'{t}px'" == "'8px'"

# issue 1086
d = f'''ddf
u{123}
zz'''
assert d == "ddf\nu123\nzz"

print("passed all tests")

0 comments on commit 745eea3

Please sign in to comment.