Re: [Python-ideas] Context manager to temporarily set signal handlers

2017-01-22 Thread Giampaolo Rodola'
I don't know if this is related (regarding functions registered in C) but
one problem I often have is to always execute exit functions. I have come
up with this:
http://grodola.blogspot.com/2016/02/how-to-always-execute-exit-functions-in-py.html


On Fri, Jan 20, 2017 at 1:46 PM, Thomas Kluyver 
wrote:

> Not uncommonly, I want to do something like this in code:
>
> import signal
>
> # Install my own signal handler
> prev_hup = signal.signal(signal.SIGHUP, my_handler)
> prev_term = signal.signal(signal.SIGTERM, my_handler)
> try:
> do_something_else()
> finally:
> # Restore previous signal handlers
> signal.signal(signal.SIGHUP, prev_hup)
> signal.signal(signal.SIGTERM, prev_term)
>
> This works if the existing signal handler is a Python function, or the
> special values SIG_IGN (ignore) or SIG_DFL (default). However, it breaks
> if code has set a signal handler in C: this is not returned, and there
> is no way in Python to reinstate a C-level signal handler once we've
> replaced it from Python.
>
> I propose two possible solutions:
>
> 1. The high-level approach: a context manager which can temporarily set
> one or more signal handlers. If this was implemented in C, it could
> restore C-level as well as Python-level signal handlers.
>
> 2. A lower level approach: signal() and getsignal() would gain the
> ability to return an opaque object which refers to a C-level signal
> handler. The only use for this would be to pass it back to
> signal.signal() to set it as a signal handler again. The context manager
> from (1) could then be implemented in Python.
>
> Crosslinking http://bugs.python.org/issue13285
>
> Thomas
> ___
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>



-- 
Giampaolo - http://grodola.blogspot.com
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

[Python-ideas] "Immutable Builder" Pattern and Operator

2017-01-22 Thread Soni L.
I've been thinking of an Immutable Builder pattern and an operator to go 
with it. Since the builder would be immutable, this wouldn't work:


long_name = mkbuilder()
long_name.seta(a)
long_name.setb(b)
y = long_name.build()

Instead, you'd need something more like this:

long_name = mkbuilder()
long_name = long_name.seta(a)
long_name = long_name.setb(b)
y = long_name.build()

Or we could add an operator to simplify it:

long_name = mkbuilder()
long_name .= seta(a)
long_name .= setb(b)
y = long_name.build()

(Yes, I'm aware you can x = mkbuilder().seta(a).setb(b), then y = 
x.build(). But that doesn't work if you wanna "fork" the builder. Some 
builders, like a builder for network connections of some sort, would 
work best if they were immutable/forkable.)

___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] "Immutable Builder" Pattern and Operator

2017-01-22 Thread Serhiy Storchaka

On 23.01.17 00:45, Soni L. wrote:

I've been thinking of an Immutable Builder pattern and an operator to go
with it. Since the builder would be immutable, this wouldn't work:

long_name = mkbuilder()
long_name.seta(a)
long_name.setb(b)
y = long_name.build()


I think the more pythonic way is:

y = build(a=a, b=b)

A Builder pattern is less used in Python due to the support of keyword 
arguments.



___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] "Immutable Builder" Pattern and Operator

2017-01-22 Thread Joao S. O. Bueno
This is easy to do in Python, and we already have NamedTuples and
other things.


If you need such a builder anyway, this snippet can work - no need for
special syntax:

https://gist.github.com/jsbueno/b2b5f5c06caa915c451253bb4f171ee9



On 22 January 2017 at 20:54, Serhiy Storchaka  wrote:
> On 23.01.17 00:45, Soni L. wrote:
>>
>> I've been thinking of an Immutable Builder pattern and an operator to go
>> with it. Since the builder would be immutable, this wouldn't work:
>>
>> long_name = mkbuilder()
>> long_name.seta(a)
>> long_name.setb(b)
>> y = long_name.build()
>
>
> I think the more pythonic way is:
>
> y = build(a=a, b=b)
>
> A Builder pattern is less used in Python due to the support of keyword
> arguments.
>
>
>
> ___
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] "Immutable Builder" Pattern and Operator

2017-01-22 Thread Soni L.

On 22/01/17 08:54 PM, Serhiy Storchaka wrote:

On 23.01.17 00:45, Soni L. wrote:

I've been thinking of an Immutable Builder pattern and an operator to go
with it. Since the builder would be immutable, this wouldn't work:

long_name = mkbuilder()
long_name.seta(a)
long_name.setb(b)
y = long_name.build()


I think the more pythonic way is:

y = build(a=a, b=b)

A Builder pattern is less used in Python due to the support of keyword 
arguments.


I guess you could do something like this, for an IRC bot builder:

fnircbotbuilder = mkircbotbuilder(network="irc.freenode.net", port=6697, 
ssl=true)

mainbot = mkircbotbuilder(parent=fnircbotbuilder,  # ???
  channels=["#bots"]).build()
fndccbotbuilder = mkircbotbuilder(parent=fnircbotbuilder, dcc=true, 
channeldcc=true)
dccbot = mkircbotbuilder(parent=fndccbotbuilder, 
channels=["#ctcp-s"]).build()
otherircbotbuilder = mkircbotbuilder(parent=fndccbotbuilder, 
network="irc.subluminal.net")  # because we want this whole network
otherbot = mkircbotbuilder(parent=otherircbotbuilder, 
channels=["#programming"]).build()# to use DCC and channel DCC


But this would be cleaner:

botbuilder = 
mkircbotbuilder().network("irc.freenode.net").port(6697).ssl(true)

mainbot = botbuilder.channels(["#bots"]).build()
botbuilder .= dcc(true).channeldcc(true)
dccbot = botbuilder.channels(["#ctcp-s"]).build()
botbuilder .= network("irc.subluminal.net")
otherbot = botbuilder.channels(["#programming"]).build()

(I mean, "channels" could/should be a per-bot property instead of a 
per-builder property... but that doesn't affect the examples much.)

___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] "Immutable Builder" Pattern and Operator

2017-01-22 Thread Ethan Furman

On 01/22/2017 03:30 PM, Soni L. wrote:

On 22/01/17 08:54 PM, Serhiy Storchaka wrote:

On 23.01.17 00:45, Soni L. wrote:



I've been thinking of an Immutable Builder pattern and an operator to go
with it. Since the builder would be immutable, this wouldn't work:

long_name = mkbuilder()
long_name.seta(a)
long_name.setb(b)
y = long_name.build()


I think the more pythonic way is:

y = build(a=a, b=b)

A Builder pattern is less used in Python due to the support of keyword 
arguments.


I guess you could do something like this, for an IRC bot builder:

fnircbotbuilder = mkircbotbuilder(network="irc.freenode.net", port=6697, 
ssl=true)
mainbot = mkircbotbuilder(parent=fnircbotbuilder,  # ???
   channels=["#bots"]).build()
fndccbotbuilder = mkircbotbuilder(parent=fnircbotbuilder, dcc=true, 
channeldcc=true)
dccbot = mkircbotbuilder(parent=fndccbotbuilder, channels=["#ctcp-s"]).build()
otherircbotbuilder = mkircbotbuilder(parent=fndccbotbuilder, 
network="irc.subluminal.net")  # because we want this whole network
otherbot = mkircbotbuilder(parent=otherircbotbuilder, 
channels=["#programming"]).build()# to use DCC and channel DCC


The following is just fine:

   fnircbotbuilder = mkircbotbuilder(
network="irc.freenode.net",
port=6697,
ssl=true,
)
mainbot = fnircbotbuilder(channels=["#bots"]).build()

fndccbotbuilder = fnircbotbuilder(dcc=true, channeldcc=true)
dccbot = fndccbotbuilder(channels=["#ctcp-s"]).build()

otherircbotbuilder = fndccbotbuilder(network="irc.subluminal.net")
otherbot = otherircbotbuilder(channels=["#programming"]).build()

--
~Ethan~
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] "Immutable Builder" Pattern and Operator

2017-01-22 Thread Wes Turner
Have you looked at pyrsistent for
immutable/functional/persistent/copy-on-write data structures in Python?

https://github.com/tobgu/pyrsistent/

(freeze() / thaw())

... e.g. List and Dict NamedTuple values are not immutable (because
append() and update() still work)

On Sunday, January 22, 2017, Soni L.  wrote:

> I've been thinking of an Immutable Builder pattern and an operator to go
> with it. Since the builder would be immutable, this wouldn't work:
>
> long_name = mkbuilder()
> long_name.seta(a)
> long_name.setb(b)
> y = long_name.build()
>
> Instead, you'd need something more like this:
>
> long_name = mkbuilder()
> long_name = long_name.seta(a)
> long_name = long_name.setb(b)
> y = long_name.build()
>
> Or we could add an operator to simplify it:
>
> long_name = mkbuilder()
> long_name .= seta(a)
> long_name .= setb(b)
> y = long_name.build()
>
> (Yes, I'm aware you can x = mkbuilder().seta(a).setb(b), then y =
> x.build(). But that doesn't work if you wanna "fork" the builder. Some
> builders, like a builder for network connections of some sort, would work
> best if they were immutable/forkable.)
> ___
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Re: [Python-ideas] "Immutable Builder" Pattern and Operator

2017-01-22 Thread Wes Turner
On Sunday, January 22, 2017, Wes Turner  wrote:

> Have you looked at pyrsistent for 
> immutable/functional/persistent/copy-on-write
> data structures in Python?
>
> https://github.com/tobgu/pyrsistent/
>
> (freeze() / thaw())
>
> ... e.g. List and Dict NamedTuple values are not immutable (because
> append() and update() still work)
>

fn.py also has immutables:
https://github.com/kachayev/fn.py/blob/master/README.rst#persistent-data-structures


>
> On Sunday, January 22, 2017, Soni L.  > wrote:
>
>> I've been thinking of an Immutable Builder pattern and an operator to go
>> with it. Since the builder would be immutable, this wouldn't work:
>>
>> long_name = mkbuilder()
>> long_name.seta(a)
>> long_name.setb(b)
>> y = long_name.build()
>>
>> Instead, you'd need something more like this:
>>
>> long_name = mkbuilder()
>> long_name = long_name.seta(a)
>> long_name = long_name.setb(b)
>> y = long_name.build()
>>
>> Or we could add an operator to simplify it:
>>
>> long_name = mkbuilder()
>> long_name .= seta(a)
>> long_name .= setb(b)
>> y = long_name.build()
>>
>> (Yes, I'm aware you can x = mkbuilder().seta(a).setb(b), then y =
>> x.build(). But that doesn't work if you wanna "fork" the builder. Some
>> builders, like a builder for network connections of some sort, would work
>> best if they were immutable/forkable.)
>> ___
>> Python-ideas mailing list
>> Python-ideas@python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Re: [Python-ideas] "Immutable Builder" Pattern and Operator

2017-01-22 Thread Soni L.



On 22/01/17 10:03 PM, Wes Turner wrote:



On Sunday, January 22, 2017, Wes Turner > wrote:


Have you looked at pyrsistent for
immutable/functional/persistent/copy-on-write data structures in
Python?

https://github.com/tobgu/pyrsistent/


(freeze() / thaw())

... e.g. List and Dict NamedTuple values are not immutable
(because append() and update() still work)


fn.py also has immutables:
https://github.com/kachayev/fn.py/blob/master/README.rst#persistent-data-structures


You seem to be thinking of "immutable object builder". Not "the builder 
itself is immutable and operations on it create new builders".




On Sunday, January 22, 2017, Soni L. > wrote:

I've been thinking of an Immutable Builder pattern and an
operator to go with it. Since the builder would be immutable,
this wouldn't work:

long_name = mkbuilder()
long_name.seta(a)
long_name.setb(b)
y = long_name.build()

Instead, you'd need something more like this:

long_name = mkbuilder()
long_name = long_name.seta(a)
long_name = long_name.setb(b)
y = long_name.build()

Or we could add an operator to simplify it:

long_name = mkbuilder()
long_name .= seta(a)
long_name .= setb(b)
y = long_name.build()

(Yes, I'm aware you can x = mkbuilder().seta(a).setb(b), then
y = x.build(). But that doesn't work if you wanna "fork" the
builder. Some builders, like a builder for network connections
of some sort, would work best if they were immutable/forkable.)
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas

Code of Conduct: http://python.org/psf/codeofconduct/




___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Re: [Python-ideas] "Immutable Builder" Pattern and Operator

2017-01-22 Thread Wes Turner
On Sunday, January 22, 2017, Soni L.  wrote:

>
>
> On 22/01/17 10:03 PM, Wes Turner wrote:
>
>
>
> On Sunday, January 22, 2017, Wes Turner  > wrote:
>
>> Have you looked at pyrsistent for 
>> immutable/functional/persistent/copy-on-write
>> data structures in Python?
>>
>> https://github.com/tobgu/pyrsistent/
>>
>> (freeze() / thaw())
>>
>> ... e.g. List and Dict NamedTuple values are not immutable (because
>> append() and update() still work)
>>
>
> fn.py also has immutables:
> https://github.com/kachayev/fn.py/blob/master/README.rst#
> persistent-data-structures
>
>
> You seem to be thinking of "immutable object builder". Not "the builder
> itself is immutable and operations on it create new builders".
>

My mistake.
Something like @optionable and/or @curried from fn.py in conjunction with
PClass from pyrsistent may accomplish what you describe?


>
>
>
>
>>
>> On Sunday, January 22, 2017, Soni L.  wrote:
>>
>>> I've been thinking of an Immutable Builder pattern and an operator to go
>>> with it. Since the builder would be immutable, this wouldn't work:
>>>
>>> long_name = mkbuilder()
>>> long_name.seta(a)
>>> long_name.setb(b)
>>> y = long_name.build()
>>>
>>> Instead, you'd need something more like this:
>>>
>>> long_name = mkbuilder()
>>> long_name = long_name.seta(a)
>>> long_name = long_name.setb(b)
>>> y = long_name.build()
>>>
>>> Or we could add an operator to simplify it:
>>>
>>> long_name = mkbuilder()
>>> long_name .= seta(a)
>>> long_name .= setb(b)
>>> y = long_name.build()
>>>
>>> (Yes, I'm aware you can x = mkbuilder().seta(a).setb(b), then y =
>>> x.build(). But that doesn't work if you wanna "fork" the builder. Some
>>> builders, like a builder for network connections of some sort, would work
>>> best if they were immutable/forkable.)
>>> ___
>>> Python-ideas mailing list
>>> Python-ideas@python.org
>>> https://mail.python.org/mailman/listinfo/python-ideas
>>> Code of Conduct: http://python.org/psf/codeofconduct/
>>>
>>
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/