« first day (3753 days earlier)      last day (1178 days later) » 

1:15 AM
@aneroid It is reopened.
 
thanks @alkasm!
I feel bad for the OP, putting in the work to edit, even before it was closed the first time and add their solution just after
 
Yeah I was surprised to see that it was closed after their edit. It's not a perfect question by any means but they did attempt to address the feedback.
 
yeah, the original v1 was quite bad, missing all code. but i think it was the 'close vote' snowball already rolling. so didn't matter that he'd done the work before it got closed
 
@Minsky you can use operator.itemgetter:
>>> from operator import itemgetter
>>> W1, W2, b1, b2 = itemgetter('W1', 'W2', 'b1', 'b2')(parameters)
>>> W1
'W1_value'
not quite as compact as the equivalent JS. although 99 times out of 100 I'd use Andras' suggestion.
 
 
1 hour later…
2:39 AM
cbg all, is there a way I can make line 6 in the code also "auto commit"? the sqlite3 docs mention connections auto commit when used in a context manager but using with contextlib.closing doesnt seem to trigger auto connect, MCVE : pastebin.com/yg95EmRT
do I need to write my own class for doing this? or is there an alternative?
 
Can you point to the relevant docs?
From what you said I'd expect with sqlite3.connect(...) as conn: ...
(but I don't do databases either)
 
thanks
OK, what you linked seems clear enough. Why are you trying to shoehorn contextlib.closing into it?
 
close a connection?
 
Let me rephrase it. Which part of the linked example doesn't satisfy your use case?
 
2:45 AM
line 7 in my linked code, works the way the docs tell, meaning it auto commits, but as per some SO answers (stackoverflow.com/a/19522634/12502959) without using contextlib.closing the connection is not closed, unlike files were .close is called on exit
 
# Connection object used as context manager only commits or rollbacks transactions,
# so the connection object should be closed manually
con.close()
 
yeah which is why I wanted to use contextlib.close
but doing so stops the auto commit mechanism they have or so it would seem in my case
is what I ask making sense? I can rephrase it better if needed
 
Just to be clear you could close the connection manually, right?
just like in the sqlite3 example
 
I could, but that defats the whole purpose of contextlib.closing correct?
 
Not necessarily. Imagine a use case where your API is given a file that someone else has already opened.
 
2:50 AM
context mangers in general, I can even do .close on files and not use with
 
@python_user you'd have to write some boilerplate to handle exceptions correctly
 
I could, which was my initial though process when I said chat.stackoverflow.com/transcript/message/51443750#51443750
@AndrasDeak this makes sense
 
As I said I don't know databases so I'll spare you my guesswork
 
thanks for the suggestion though, I would still expect the auto commit to work the way I wanted it but maybe there were some design choices made for the better
 
You could probably trace out how the suggested context manager works, and how wrapping it in closing() works, considering what a context manager is syntactical sugar for.
It's possible you'd have to do something weird like using one context manager to define the connection, and another explicitly saying closing(conn) or similar
 
2:54 AM
@AndrasDeak I will have to this then, OTOH stackoverflow.com/a/50645518/12502959 seems to do what I want
is it ok to ask this on main? provided I give a work around suggested by the answer above?
the question here is "not how to auto commit" but "why it doest auto commit" if I dont use the answer above
 
3:12 AM
19 mins ago, by Andras Deak
You could probably trace out how the suggested context manager works, and how wrapping it in closing() works, considering what a context manager is syntactical sugar for.
 
@AndrasDeak ha, thanks for the reminder
 
@AndrasDeak thats what I am trying to do now
@AndrasDeak I guess I figured out why, docs.python.org/3/library/contextlib.html#contextlib.closing I guess its because its just yielding my connection and not using a context manger internally
 
3:56 AM
I wonder if there's a smart way to do what you want
what I had in mind about using a compound with statement wouldn't work, because you need the closing() inside the scope of the conn context, but you'd want the closing() to terminate after the conn does (to commit before you close)
Martijn's post on the subject might help but it's too late here for me to try and figure it out
my suspicion is that sane solutions would keep your context managers nested
 
4:11 AM
@AndrasDeak I tried something like with sqlite3.connect('temp.db') as conn_, closing(conn_) as conn and it didnt work, got sqlite3.ProgrammingError: Cannot operate on a closed database.
@AndrasDeak this looks useful I will see if I can make use of this
 
@python_user my point exactly. Close before commit.
 
I am going to settle for a manual close for now, and once I understand context managers better I will start using / modifying Maritin's answer
 
 
10 hours later…
2:15 PM
@python_user Your issue is that you can have only one thing work as a context manager at a time. So either the sqlite3.connect is the context manager, or the wrapping closing. The closing does not know you have fed it a context manager that needs to be handled as such.
You can define your own easily, though.
import contextlib

@contextlib.contextmanager
def cm_closing(context_manager: contextlib.AbstractContextManager):
    with context_manager as context:
        with contextlib.closing(context):
            yield context
@python_user ^ this should work for your case as a replacement of closing.
 
@MisterMiyagi ah, nice
Is that double with not the same as with context_manager as context, contextlib.closing(context):?
Wait, why won't that close before the enclosing context manager commits?
 
ah, damn, good point...
must be the other way around
@contextlib.contextmanager
def cm_closing(context_manager: contextlib.AbstractContextManager):
    with contextlib.closing(context_manager):
        with context_manager as context:
            yield context
 
NameError?
I'm glad I got this right, then :D
ah
Could you just write that as with contextlib.closing(context_manager), context_manager: yield context_manager?
Now I see that you can only do this inline with an asspression
with contextlib.closing(conn:=sqlite3.connect('foo.db')), conn: ...
 
2:31 PM
it's a bit tricky that the thing to close is created by a context manager, but must be closed after the context. :/
seeing how @python_user has several transaction, I would handle the closing and transactions separately.
with closing(sqlite3.connect('temp.db')) as conn:
    with conn as transaction1, closing(transaction1.cursor(...)):
        ...
    with conn as transaction2, ...:
        ...
 
Is there a significance to renaming the managers with "as"? There's probably a subtlety I'm missing.
 
2:49 PM
thanks @MisterMiyagi, let me understand what you have suggested before I get back
 
3:00 PM
@MisterMiyagi in with contextlib.closing(context_manager) why is there no as? I guess this is Andras' question as well?
 
well I'm asking why there's an as elsewhere, but yeah
 
I am guessing since only the inner with is returning something that the context manager yields, a name is needed and the outer with will probably close whatever it yields it as such when it enters __exit__?
 
I guess we could read docs.python.org/3/reference/compound_stmts.html#with in detail to learn what's up
 
I have always skipped understanding how to read these with_item ::= expression ["as" target] I know there is page in doc that explains how to read these, guess its time I checked
 
@AndrasDeak the result of entering a context is not necessarily the context manager. For example, closing returns its argument instead.
 
3:06 PM
@MisterMiyagi so when you do with a as b you get b from what a yields?
@python_user that part just describes the syntax, saying that "as target" is optional
 
@MisterMiyagi I feel this might be related to that, not sure though, I still dont fully understand the answer linked
^ that is a reply to an old question I asked in here
 
@MisterMiyagi OK, I think I get it, thanks
> 1.The context expression (the expression given in the with_item) is evaluated to obtain a context manager.
2. The context manager’s __enter__() is loaded for later use.
5. If a target was included in the with statement, the return value from __enter__() is assigned to it.
so transaction1 is the result of conn.__enter__() more or less
give or take generators or something
apparently only using with open(...) as f: only reveals a subset of the functionality :P
 
thanks for the help guys, I have a lot to understand with context managers before I start writing my own custom versions
 
AAB
4:13 PM
Hi all,
 
4:35 PM
cbg
 Is unpacking a range/map using * faster than using a list? any opinions why?
    %%timeit
    [*range(10)]
    #322 ns ± 14 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
    %%timeit
    list(range(10))
    #437 ns ± 32.2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
 
One fewer function call. The real question is how much time it takes until you start noticing the 0.1 microsecond difference.
You can look at the respective bytecodes
 
you can say its a general question, not hampering anything as such yet :) but thanks for the direction.
 
5:09 PM
I am trying to implement a custom docker compose for python using Docker SDK for Python for a simple use like 2-3 docker containers. I don't want it to be for all possible cases a docker compose has. But I want to be flexible to include future cases easily. Such cases are spin up a single container, spin up containers in a network. What's the best way to approach this?
For a case wwith one docker container it is quite easy: give it a URL and then pull/start/stop/remove. When it comes to a network I will have to create a network forst, then services that use containers and are in that network. But what if in the future I'd want to include some other cases?
 
user12582392
5:28 PM
hey guys, any ideas on how to collaborate on a notebook
 
user12582392
we're trying to do machine learning with someone else
 
user12582392
google collab is just disgusting, you can't share files easily, and there are sync problems
 
user12582392
or i just don't get it, which is a reasonable option
 
Sam
Hey guys
 
6:08 PM
Hello!
How is everyone doing today?
I just got access to chats! Cant wait to talk with more of you guys, stackoverflow is great!
 
hey Sect0r
 
Hi!
How are you?
 
yeah I am doing, hope you are too
feel free to read the room rules if you haven't already
 
Hi Sect0r
 
Hi!
Also, reply to python_user.
Where do i find the chatroom rules?
 
Just finished reading them.
Thanks!
 
@Sect0r thank you too :)
 
So, what's everyone up to?
 
6:57 PM
@Minsky does it have to be a jupyter notebook?
For example, distributed programming in VSCode is... not awful. I can't really bring myself to speak more highly than that. But yeah, not awful
@Sect0r Oh, you know. The same thing I do every night; trying to take over the world
You?
 
Haha!
 
7:13 PM
@python_user I don't think I follow this properly. I don't believe I've seen something similar in the wild. Also, I'm not sure what it buys you
Just doubling down on potential database write errors?
 
i finished a small thing i started working on today,
a custom image format :) @roganjosh
 
7:41 PM
cbg
How do I run sync function as async function such that its thread safe?
I have the following template in my code but its not thread safe
def sync_func(one, two, three=None):
    # do blocking stuff
    return some_stuff

async def async_func(whatever):
    # obtain one, two, three from somewhere?
    # supports args & kwargs
    thing = functools.partial(sync_func, one, two, three=3)

    # run_in_executor supports passing args directly, e.g.
    # 'run_in_executor(None, func, one, two, three)' but using
    # partial makes stuff a bit easier to read if you have a
    # large amount of arguments you don't want to stack onto
    # a single line.
 
Can you clarify what you are asking? Whether a function is thread safe or not has nothing to do with how you run it. Either the function is thread safe in itself or not.
 
the way I am running it is currently not thread safe.
 
Sorry, I don't know how to respond to that other than to repeat what I just wrote.
Is your issue to execute several things from concurrent invocations of async_func, without any two things executing concurrently?
 

« first day (3753 days earlier)      last day (1178 days later) »