with-statement

H2DB WITH clause

烂漫一生 提交于 2021-02-10 13:58:07
问题 I'm writing a unit test for a method with the following sql WITH temptab( i__id , i__name, i__effective, i__expires, i__lefttag, i__righttag, hier_id, hier_dim_id, parent_item_id, parent_hier_id, parent_dim_id, ancestor, h__id, h__name, h__level, h__effective, h__expires, rec_lvl) AS ( SELECT item.id as i__id, item.name as i__name, item.effectivets as i__effective, item.expirests as i__expires, item.lefttag as i__lefttag, item.righttag as i__righttag, hier_id, hier_dim_id, parent_item_id,

Use for loop after the With Clause in PL/SQL

泄露秘密 提交于 2021-02-08 15:46:05
问题 Im using PL/SQL. I am trying to have a for loop right after I define my temporary tables in the with clause. However, Im getting an error to have a SELECT query first. For instance WITH TMP1 AS (.....), TMP2 AS (......), TMP3 AS (......) FOR R IN (SELECT DISTINCT ..... FROM TMP1 WHERE .....) LOOP SELECT .... FROM TMP2, TMP2 WHERE TMP2.... = R..... .... How do I go about doing so? Thanks 回答1: You can't access a CTE outside of the whole statement. And you can't access individual parts of a CTE

Recommended way of closing files using pathlib module?

*爱你&永不变心* 提交于 2021-02-08 13:32:07
问题 Historically I have always used the following for reading files in python : with open("file", "r") as f: for line in f: # do thing to line Is this still the recommend approach? Are there any drawbacks to using the following: from pathlib import Path path = Path("file") for line in path.open(): # do thing to line Most of the references I found are using the with keyword for opening files for the convenience of not having to explicitly close the file. Is this applicable for the iterator

manually open context manager

十年热恋 提交于 2021-02-04 18:48:09
问题 My question is, how can I execute any context manager without using with ? Python has the idea of context managers, instead of file = open('some_file', 'w') try: file.write('Hola!') finally: file.close() # end try you can write with open('some_file', 'w') as opened_file: opened_file.write('Hola!') # end with While in most cases the second one is the golden solution, for testing and exploring in the interactive console, the first one can be much better used, as you can write it line by line. >

python: sending a mail, fails when inside a “with” block

可紊 提交于 2021-01-28 04:45:55
问题 I am wondering why this code test = smtplib.SMTP('smtp.gmail.com', 587) test.ehlo() test.starttls() test.ehlo() test.login('address','passw') test.sendmail(sender, recipients, composed) test.close() works, but when written like this with smtplib.SMTP('smtp.gmail.com', 587) as s: s.ehlo() s.starttls() s.ehlo() s.login('address','passw') s.sendmail(sender, recipients, composed) s.close() it fails with the message Unable to send the email. Error: <class 'AttributeError'> Traceback (most recent

Python proper way to catch exceptions on file close

梦想的初衷 提交于 2021-01-27 09:55:42
问题 I'm old at Perl and new to Python. I know in Perl that fd.close() isn't irrelevant. Writing to a full file system, close() will report the error. Also for socket errors, they appear in close(). So how to do with in Python? Some examples show putting the open() and close() in the same try block which would catch IOError on either. But other examples show close() in the finally block to close the file upon exception. However, what if the exception first occurs in close()? Does this cover both

Is using `with` statement with Proxies a bad practice?

China☆狼群 提交于 2021-01-27 02:47:26
问题 First of all, I want to clarify, I know that with is deprecated , and using it is generally a bad practice . However, my question is about a special case: using a special Proxy object as the parameter of with. Background I'm working on a project, where I have to limit access of a piece of code to the global scope. One approach might be to use a loop with eval , that creates constant variables with the value of undefined for each property of the global object, but that seems even worse than

Is using `with` statement with Proxies a bad practice?

限于喜欢 提交于 2021-01-27 02:44:48
问题 First of all, I want to clarify, I know that with is deprecated , and using it is generally a bad practice . However, my question is about a special case: using a special Proxy object as the parameter of with. Background I'm working on a project, where I have to limit access of a piece of code to the global scope. One approach might be to use a loop with eval , that creates constant variables with the value of undefined for each property of the global object, but that seems even worse than

Is using `with` statement with Proxies a bad practice?

痴心易碎 提交于 2021-01-27 02:44:07
问题 First of all, I want to clarify, I know that with is deprecated , and using it is generally a bad practice . However, my question is about a special case: using a special Proxy object as the parameter of with. Background I'm working on a project, where I have to limit access of a piece of code to the global scope. One approach might be to use a loop with eval , that creates constant variables with the value of undefined for each property of the global object, but that seems even worse than

Python __enter__ / __exit__ vs __init__ (or __new__) / __del__

爱⌒轻易说出口 提交于 2021-01-20 17:18:28
问题 I have searched and I'm unable to come up with any good reason to use python's __enter__ / __exit__ rather than __init__ (or __new__ ?) / __del__ . I understand that __enter__ / __exit__ are intended for use with the with statement as context managers, and the with statement is great. But the counterpart to that is that any code in those blocks is only executed in that context. By using these instead of __init__ / __del__ I appear to be creating an implicit contract with callers that they