ANN: JavaScripthon 0.9 has been released

2018-04-20 Thread Alberto Berti
I'm pleased to announce the release of version 0.9 of JavaScripthon!

JavaScripthon in a small and unobtrusive yet powerful
Python-to-JavaScript compiler for Python 3.5+ that targets ES6+
syntax.

Changes since the previous version:

- add a --source-name options to be used together with
  --inline-map when using -s;
- move main repository to gitlab.com/metapensiero;
- add support for default export and import;
- add documentation for the JS() marker function;
- refactor of the JS AST nodes;
- fix path splitting and joining on Windows (thanks to Roman Yakubuk);

There are some new contributions in this release:

- BrainBacon has made a JavaScripthon loader for WebPack;
- icarito has contributed support for JavaScripthon to the
  python-webpack-loader for WebPack;
- icarito has also integrated JavaScripthon with Nuxt.js and
  Vue.js;
- chfw has integrated JavaScripthon into pyecharts to allow
  Python function translation.

For more informations se the project homepage at 
https://gitlab.com/metapensiero/metapensiero.pj

It's also mirrored on github at https://github.com/metapensiero/metapensiero.pj

License: GPL3

--
Alberto Berti (alberto at metapensiero.it)

"gutta cavat lapidem"
-- 
https://mail.python.org/mailman/listinfo/python-announce-list

Support the Python Software Foundation:
http://www.python.org/psf/donations/


Re: A use-case for for...else with no break

2017-11-02 Thread Alberto Berti
> "Wolfgang" == Wolfgang Maier  
> writes:

   Wolfgang> Try running it interactively and you'll see,
   Wolfgang> wolfgang

I've tried but my muscolar memory failed me... i've issued a C-c C-c
that usually would have sent the region of text to the interpreter
session (when done from python-mode opened files in emacs)  but instead
i was in gnus and it sent the half prepared email :-)

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A use-case for for...else with no break

2017-11-02 Thread Alberto Berti
> "Steve" == Steve D'Aprano  writes:

py> for x in "abcdefgh":
Steve> ... print(x, end='')
Steve> ...
py> efghpy>


Steve> "For ... else" to the rescue!

py> for char in "abcdefgh":
Steve> ... print(char, end='')
Steve> ... else:
Steve> ... print()
Steve> ...
Steve> abcdefgh
py> 

else doesn't seem to bring any advantage over:

for char in "abcdefgh":
print(char, end='')
print()

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Cooperative class tree filtering

2017-10-31 Thread Alberto Berti
Thanks Ian,

>>>>> "Ian" == Ian Kelly <ian.g.ke...@gmail.com> writes:

Ian> On Thu, Oct 12, 2017 at 5:07 PM, Alberto Berti 
<albe...@metapensiero.it> wrote:

Ian> My initial reaction is: is this really worth it? This seems like an
Ian> awful lot of code and added complexity in order to do away with two
Ian> lines. It's a lot easier to reason about "return
Ian> super().filter(element)" and verify that it does the right thing than
Ian> for the complicated descriptor above.


yes this was also my conclusion, that descriptor is sitting on a branch
of its own and I've yet to decide on it.  The fact is that i've many
cases where I need a coperative superclass-subclass method execution and
I was trying to find some other pattern that would fit in ;-)


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Invoking return through a function?

2017-10-31 Thread Alberto Berti
> "Lele" == Lele Gaifax  writes:

Lele> r...@zedat.fu-berlin.de (Stefan Ram) writes:
Stefan> There are many macro processors available, like the C macro
Stefan> preprocessor, gpp, M4, or funnelweb. You can always use them
Stefan> for your Python source (which, in this case, isn't exactly Python
Stefan> source anymore).

Lele> Even more to the point, MacroPy! See 
https://github.com/azazel75/macropy for a
Lele> 3.5+ version.

I share the opinion of Stefan and others, it's a bad practice. But just
to have some fun I implemented  it with MacroPy...

This is the module with the test code::

  # test.py
  from test_macros import macros, check_macro

  def checkKey(k, m):
  return k in m

  @check_macro
  def test():

  m = {1: 'a', 2: 'b'}
  print('before')
  checkKey(3, m)
  print('after')


here the test function is wrapped by  the macro, that is defined as:

  # test_macros.py
  from macropy.core.macros import Macros
  from macropy.core.quotes import macros, q, u, ast_literal
  from macropy.core.walkers import Walker
  from macropy.experimental.pattern import macros, switch, _matching, 
ClassMatcher

  from ast import Call, Name, Expr

  macros = Macros()

  @macros.decorator
  def check_macro(tree, **kw):

  @Walker
  def transform_checkKey(tree, stop, **kw):
  with switch(tree):
  if Expr(value=Call(func=Name(id='checkKey'))):
  with q as result:
  if not ast_literal[tree.value]:
  print('exiting!')
  return
  stop()
  else:
  result = tree
  return result

  return transform_checkKey.recurse(tree)

The macro is run with all the decorated code of the ``test()`` method
passed as the ``tree`` parameter in the form of ast tree. A tree walker
is then run to navigate the tree and augment the occurrence of checkKey
with the conditional return

And finally, a ``main`` module is needed to activate macro parsing and
substitution:

  # test_main.py
  import macropy.activate

  import test

  test.test()

cheers,

Alberto

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Cooperative class tree filtering

2017-10-12 Thread Alberto Berti

Sorry, i've made a mistake in  the second C body, it's written like: 
>>>>> "me" == Alberto Berti <albe...@metapensiero.it> writes:

me> I've tried some variants of the 'super()' trick, that sometimes seems to
me> change behavior if it's written like that or like super(type(self),
me> self) in no clear (to me, and I failed to find extensive doc on
me> super()'s behavior) way, with things that stop working if mixins are
me> involved (even if the mixins do not reimplement the methods involved
me> here). Eventually i ended implementing a decorator:

me>   from functools import partial, wraps


me>   class delegate_super:
me>   """A method decorator that delegates part of the computation to 
the same
me>   method on the ancestor class."""

me>   _name = None
me>   _owner = None

me>   def __init__(self, meth):
me>   self._meth = meth
me>   @wraps(meth)
me>   def wrapper(*args, **kwargs):
me>   return self.delegator(*args, **kwargs)
me>   self.wrapper = wrapper

me>   def __get__(self, instance, owner):
me>   return partial(self.wrapper, instance)

me>   def __set_name__(self, owner, name):
me>   self._owner = owner
me>   self._name = name

me>   def delegator(self, instance, *args, **kwargs):
me>   result = self._meth(instance, *args, **kwargs)
me>   if result is None:
me>   result = getattr(super(self._owner, instance), 
self._name)(
me>   *args, **kwargs)
me>   return result

me>   class A:
me>   def filter(self, element):
me>   # the default implementation always returns True
me>   return True


me>   class B(A):
me>   @delegate_super
me>   def filter(self, element):
me>   if element == 'foo':
me>   return True
me>   elif element == 'bar':
me>   return False


me>   class C(B):
me>   @delegate_super
me>   def filter(self, element):
me>   if element == 'bar':
me>   return True
me>   else:
me>   return super().filter(element)

The correct version is:

  class C(B):
  @delegate_super
  def filter(self, element):
  if element == 'bar':
  return True


-- 
https://mail.python.org/mailman/listinfo/python-list


Cooperative class tree filtering

2017-10-12 Thread Alberto Berti
Hello,

suppose you have you have three classes A, B, C where C is a subclass of
B and B of A. They provide a method with signature `filter(element)`. It is
called by an external function that collects the elements and then to an
instance of the classes A, B and C is given a chance to filter these
elements, one at a time. So the `filter(element)` method returns `True`  if
the element has to be included in the final set and `False` if it
doesn't, easy peasy.
(This is a simplified example of a real use case).

What I want to have is that if an instance of class C has doesn't know
what to return about an element, it calls `super()` to return the value
from the same method implemented in its superclass, and so on up on the
tree. To show it in code:

  class A:
  def filter(self, element):
  # the default implementation always returns True
  return True


  class B(A):
  def filter(self, element):
  if element == 'foo':
  return True
  elif element == 'bar':
  return False
  else:
  return super().filter(element)


  class C(B):
  def filter(self, element):
  if element == 'bar':
  return True
  else:
  return super().filter(element)


  def collect_elements(instance):
  "A semplified element collect function"
  all_elts = {'foo', 'bar', 'baz', 'zoo'}
  filtered_elts = set(el for el in all_elts if instance.filter(el))
  return filtered_elts

  b = B()
  c = C()

  print(collect_elements(b))
  print(collect_elements(c))

which run gives the following result:

  >>> {'foo', 'zoo', 'baz'}
  {'bar', 'foo', 'zoo', 'baz'}


Now, what i ideally want is to get rid of that super() call at the end of
the methods in classes B and c and to code that "fallback to what my
superclass says" coded into A's implementation, where it kicks in if the
method in the target instance returns None. So to write it in code, I
would like some like:


  class A:
  def _filter_impl(self, element):
  # the default implementation always returns True
  return True

  def filter(self, element):
  # here a logic that if self._filter_impl(element) returns
  # None, it defaults to super(type(self), self)._filter_impl(element)
  # until A._filter_impl() is reached
  pass


  class B(A):
  def _filter_impl(self, element):
  if element == 'foo':
  return True
  elif element == 'bar':
  return False


  class C(B):
  def filter(self, element):
  if element == 'bar':
  return True


I've tried some variants of the 'super()' trick, that sometimes seems to
change behavior if it's written like that or like super(type(self),
self) in no clear (to me, and I failed to find extensive doc on
super()'s behavior) way, with things that stop working if mixins are
involved (even if the mixins do not reimplement the methods involved
here). Eventually i ended implementing a decorator:

  from functools import partial, wraps


  class delegate_super:
  """A method decorator that delegates part of the computation to the same
  method on the ancestor class."""

  _name = None
  _owner = None

  def __init__(self, meth):
  self._meth = meth
  @wraps(meth)
  def wrapper(*args, **kwargs):
  return self.delegator(*args, **kwargs)
  self.wrapper = wrapper

  def __get__(self, instance, owner):
  return partial(self.wrapper, instance)

  def __set_name__(self, owner, name):
  self._owner = owner
  self._name = name

  def delegator(self, instance, *args, **kwargs):
  result = self._meth(instance, *args, **kwargs)
  if result is None:
  result = getattr(super(self._owner, instance), self._name)(
  *args, **kwargs)
  return result

  class A:
  def filter(self, element):
  # the default implementation always returns True
  return True


  class B(A):
  @delegate_super
  def filter(self, element):
  if element == 'foo':
  return True
  elif element == 'bar':
  return False


  class C(B):
  @delegate_super
  def filter(self, element):
  if element == 'bar':
  return True
  else:
  return super().filter(element)


  def collect_elements(instance):
  "A semplified element collect function"
  all_elts = {'foo', 'bar', 'baz', 'zoo'}
  filtered_elts = set(el for el in all_elts if instance.filter(el))
  return filtered_elts

  b = B()
  c = C()

  print(collect_elements(b))
  print(collect_elements(c))

which has the same result as before:

  >>> {'foo', 'zoo', 'baz'}
  {'bar', 'foo', 'zoo', 'baz'}


I would really like to find a way to do this that doesn't involve
decorating the methods in A subclasses to free the final developer to
remember 

[issue29271] Task.current_task(None) returns unexpected result

2017-01-13 Thread Alberto Berti

Changes by Alberto Berti <albe...@metapensiero.it>:


--
nosy: +azazel

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue29271>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



ANN: JavaScrypthon 0.5, now with embedded evaluation of transpiled code

2016-11-26 Thread Alberto Berti
Hi all,

i'm pleased to announce that JavaScripthon 0.5 has been released to
PyPI. JavaScrypthon can translate a subset of Python 3.5 code to  ES6
JavaScript producing beautiful and lean  code, while supporting some of
the latest Python features.

Changelog
(https://github.com/azazel75/metapensiero.pj/blob/master/CHANGES.rst):

- translate ``tmpl("A string with js ${interpolation}")`` to ES6 template
  literals;
- preliminary support to translate names like ``d_foo`` and ``dd_bar`` to
  ``$foo`` and ``$$bar``;
- addded translation of the ``assert`` statement;
- fixed a bug in ``try...except...finally`` statement when there's no
  ``except`` section;
- added translation for ``foo is not bar`` that seems to have dedicated ast
  node;
- if the function is defined in a method but starts with ``fn_`` do not convert
  it to an arrow function. Useful to *not* maintain ``this``;
- added translation for ``callable`` and ``hasattr/getattr/setattr``;
- updated for loops to support more than one target, so now its possible to
  write loops like ``for k, v in iterable(a_map):``;
- updated documentation;
- added a new cli option ``-s`` to translate source from the command line or
  the standard input;
- fixed a pair of bugs on sourcemaps;
- added a new cli option ``--eval`` to also evaluate the produced JavaScript
  using the embedded interpreter;
- added a new cli option ``--dump-ast`` to print out the ast tree of the
  passed in string;
- added sorting to the rendered snippets/decorators/assignments so that their
  order does not change at every ricompilation;
- do not re-declare variables declare in outer scopes;

- updated BabelJS to version 6.18.1;
- allow to import modules with dashes inside by using dunder-inside-words
  notation (``foo__bar`` becomes ``foo-bar``);
- reuse JavaScript interpreter context to speedup translation;
- update ``in`` operator to support ES6 collections;
- added support for method and class decorators;
- added support for class properties and descriptors;
- add ``for`` loop over JS iterables;
- allow to loop over inherited properties;
- fix a bug on ``type()`` translation;
- support for ``range()`` steps;
- add support for generator functions and ``yield`` and ``yield from``
  expressions;
- optionally load babel-polyfill before evaluating code;
- fix a bug on sourcemaps having wrong references when there are documentation
  elements;
- translate ``__get__()`` and ``__set__()`` to to JS equivalents;
- implement ``dict(foo).update(bar)`` and ``dict(foo).copy``;
- documentation improvements;
-- 
https://mail.python.org/mailman/listinfo/python-announce-list

Support the Python Software Foundation:
http://www.python.org/psf/donations/


Re: Question about asyncio and blocking operations

2016-01-26 Thread Alberto Berti
> "Frank" == Frank Millman  writes:

Frank> Now I have another problem. I have some classes which retrieve some
Frank> data from the database during their __init__() method. I find that it
Frank> is not allowed to call a coroutine from __init__(), and it is not
Frank> allowed to turn __init__() into a coroutine.

IMHO this is semantically correct for a method tha should really
initialize that instance an await in the __init__ means having a
suspension point that makes the initialization
somewhat... unpredictable :-).

To cover the cases when you need to call a coroutine from a non
coroutine function like __init__ I have developed a small package that
helps maintaining your code almost clean, where you can be sure that
after some point in your code flow, the coroutines scheduled by the
normal function have been executed. With that you can write code like
this:

from metapensiero.asyncio import transaction

class MyObject():

def __init__(self):
tran = transaction.get()
tran.add(get_db_object('company'), cback=self._init) # 
get_db_object is a coroutine

def _init(self, fut):
self.company = fut.result()

async external_coro(): # this is the calling context, which is a coro
async with transction.begin():
o = MyObject
# maybe other stuff

# start using your db object
o.company...

This way the management of the "inner" coroutine is simpler, and from
your code it's clear it suspends to wait and after that all the
"stashed" coroutines are guaranteed to be executed.

Hope it helps,

Alberto

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question about asyncio and blocking operations

2016-01-26 Thread Alberto Berti

>>>>> "Alberto" == Alberto Berti <albe...@metapensiero.it> writes:

Alberto> async external_coro(): # this is the calling context, which is 
a coro
Alberto> async with transction.begin():
Alberto> o = MyObject
Alberto> # maybe other stuff

ops... here it is "o = MyObject()" ;-)

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: securing a python execution environment...

2007-11-19 Thread Alberto Berti

maybe using import hooks?

http://www.python.org/dev/peps/pep-0302/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: why there is no pythonscript insine web browsers?

2007-11-15 Thread Alberto Berti
 Piet == Piet van Oostrum [EMAIL PROTECTED] writes:

Piet CGI is server-side. The OP was asking for client-side
Piet embedding of Python.

FireFox 3 aka Gran Paradiso can be compiled to have python
scripting but for security reasons it can be used only on crome://
urls, which load  local content.

there is an xpi package that can be installed on Gran Paradiso alphas:

http://vamposdecampos.googlepages.com/pyxpcom

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Populating a dictionary, fast

2007-11-11 Thread Alberto Berti
 Steven == Steven D'Aprano [EMAIL PROTECTED] writes:

Steven $ time ./slurp_dict.py Starting at Sun Nov 11 14:26:51
Steven 2007 Line 0 Line 100 Line 200 Line 300 Line
Steven 400 Line 500 Line 600 Line 700 Line
Steven 800 Items in dict: 8191180 Completed import at Sun Nov
Steven 11 14:29:31 2007 Starting to delete dict...


Steven Traceback (most recent call last): File ./slurp_dict.py,
Steven line 20, in module del id2name KeyboardInterrupt

Steven real 35m52.334s user 1m17.663s sys 0m16.758s


Steven Notice that the dict is completely read into memory in
Steven just two and a half minutes. The script then tries to
Steven delete the dict, and 32 minutes later is still
Steven struggling. That's the point I got sick of waiting and
Steven interrupted the script.

Steven Conclusion: it's a memory issue, or maybe a garbage
Steven collection issue, not a problem with dicts.


uh, strange results...

I run your same scripts with and without garbage collection enabled
and those are the results:

with gc enabled:

[EMAIL PROTECTED]:~/wip/zodb_test$ python slurp_dict.py 
Starting at Sun Nov 11 16:35:12 2007
Line 0
Line 100
Line 200
Line 300
Line 400
Line 500
Line 600
Line 700
Line 800
Items in dict: 8191180
Completed import at Sun Nov 11 16:36:03 2007
Starting to delete dict...
Completed deletion at Sun Nov 11 16:36:09 2007
Finishing at Sun Nov 11 16:36:09 2007

and without gc enabled

[EMAIL PROTECTED]:~/wip/zodb_test$ python slurp_dict.py 
Starting at Sun Nov 11 16:39:02 2007
Line 0
Line 100
Line 200
Line 300
Line 400
Line 500
Line 600
Line 700
Line 800
Items in dict: 8191180
Completed import at Sun Nov 11 16:39:49 2007
Starting to delete dict...
Completed deletion at Sun Nov 11 16:39:56 2007
Finishing at Sun Nov 11 16:39:56 2007


all with python2.4 on and i386 Linux

cheers 

Alberto

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Populating a dictionary, fast

2007-11-11 Thread Alberto Berti
 Michael == Michael Bacarella [EMAIL PROTECTED] writes:

  This would seem to implicate the line id2name[id] = name as
 being
Michael  excruciatingly slow.
 
 As others have pointed out there is no way that this takes 45
 minutes.Must be something with your system or setup.
 
 A functionally equivalent code for me runs in about 49 seconds!
 (it ends up using about twice as much ram as the data on disk)

Michael You can download the list of keys from here, it's 43M
Michael gzipped: http://www.sendspace.com/file/9530i7

Michael and see it take about 45 minutes with this:

I've downloaded your keys, run your program and this is the result:

$ du -h keys.txt 
128Mkeys.txt

$ time python cache_keys.py 

real0m55.913s
user0m35.286s
sys 0m0.852s

$ python
Python 2.4.4 (#2, Apr 26 2007, 00:02:45) 
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2

$ uname -a
Linux lizard 2.6.21-1-k7 #1 SMP Sat May 26 16:56:05 UTC 2007 i686 GNU/Linux

cheers

Alberto

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Writing GTK UI for Python apps in XUL

2007-11-02 Thread Alberto Berti
 Devraj == Devraj  [EMAIL PROTECTED] writes:

Devraj Hi everyone, Is it possible to write UI code in XUL for a
Devraj GTK Python application? I found NuFox, which does the
Devraj reverse (lets me write Python to generate XUL)

i wrote such a system as a proof of concept, it's far from being
complete.

you can find the sources here:

http://www.artiemestieri.tn.it/~azazel/darcs/snakegtk

with it you can use python scripting  inside xul files.
Take a look at this 

http://www.artiemestieri.tn.it/~azazel/darcs/snakegtk/doc/site/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XUL Parser?

2004-12-02 Thread Alberto Berti
Hi,
I'm slowly developing a thin client environment that will use xul, or a part
of it as gui language, all done in python and that uses python instead of js as
scripting language and event binding. It builds the gui using pyGTK.

Who is interested can find more info here:

http://artiemestieri.tn.it/~azazel/darcs/snakegtk

The project is currently halted due to time constraints. On the repository there
is a tag 0.1 from wich the demo was generated, the HEAD is not functional
yet and it's current 0.2 code, a release  that will add:
 
  - a test suite;

  - remote loading of xul files and python libraries with support for
file, http, https, ftp url schemes, with remote import of python
modules too;

  - importing of external module via script src=an url/

  - refactoring of the overall environment;

If you are interested, please download the code and contribute, it's GPL.

bye, Alberto

-- 
http://mail.python.org/mailman/listinfo/python-list