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  writes:

Ian> On Thu, Oct 12, 2017 at 5:07 PM, Alberto Berti 
 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  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 t

Re: Question about asyncio and blocking operations

2016-01-26 Thread Alberto Berti

>>>>> "Alberto" == Alberto Berti  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: 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: 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
> "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: 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  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: 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 

  - 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