with-statement

How to check if an object is created with `with` statement?

99封情书 提交于 2019-11-30 03:31:09
问题 I would like to ensure that the class is only instantiated within a "with" statement. i.e. this one is ok: with X() as x: ... and this is not: x = X() How can I ensure such functionality? 回答1: All answers so far do not provide what (I think) OP wants directly . (I think) OP wants something like this: >>> with X() as x: ... # ok >>> x = X() # ERROR Traceback (most recent call last): File "run.py", line 18, in <module> x = X() File "run.py", line 9, in __init__ raise Exception("Should only be

Converting sql statement that contains 'with' cte to linq

随声附和 提交于 2019-11-29 14:16:14
Hey, I have this piece of code here, been battling with it for hours. basically what this sql statement does is gets ALL subfolders of a specified folder (@compositeId). WITH auto_table (id, Name, ParentID) AS ( SELECT C.ID, C.Name, C.ParentID FROM Composite_Table AS C WHERE C.ID = @compositeId UNION ALL SELECT C.ID, C.Name, C.ParentID FROM Composite_Table AS C INNER JOIN auto_table AS a_t ON C.ParentID = a_t.ID ) SELECT * FROM auto_table This query would return something like this: Id | Name | ParentId 1 | StartFolder| NULL 2 | Folder2 | 1 4 | Folder3 | 1 5 | Folder4 | 4 Now i want to convert

tempfile.TemporaryDirectory context manager in Python 2.7

瘦欲@ 提交于 2019-11-29 02:49:16
Is there a way to create a temporary directory in a context manager with Python 2.7? with tempfile.TemporaryDirectory() as temp_dir: # modify files in this dir # here the temporary diretory does not exist any more. Nicholas Bishop Another option is the "backports.tempfile" package on pypi: https://pypi.python.org/pypi/backports.tempfile Quoting the project's description: "This package provides backports of new features in Python’s tempfile module under the backports namespace." Install with: pip install backports.tempfile Then use it in your script: from backports import tempfile with tempfile

Is Python *with* statement exactly equivalent to a try - (except) - finally block?

邮差的信 提交于 2019-11-29 02:15:06
问题 I know this was widely discussed, but I still can't find an answer to confirm this: is the with statement identical to calling the same code in a try - (except) -finally block, where whatever one defines in the __exit__ function of the context manager is placed in the finally block? For example -- are these 2 code snippets doing exactly the same thing? import sys from contextlib import contextmanager @contextmanager def open_input(fpath): fd = open(fpath) if fpath else sys.stdin try: yield fd

StringIO and compatibility with 'with' statement (context manager)

核能气质少年 提交于 2019-11-28 18:28:09
问题 I have some legacy code with a legacy function that takes a filename as an argument and processes the file contents. A working facsimile of the code is below. What I want to do is not have to write to disk with some content that I generate in order to use this legacy function, so I though I could use StringIO to create an object in place of the physical filename. However, this does not work, as you can see below. I thought StringIO was the way to go with this. Can anyone tell me if there is a

In Python, if I return inside a “with” block, will the file still close?

我与影子孤独终老i 提交于 2019-11-28 15:49:57
Consider the following: with open(path, mode) as f: return [line for line in f if condition] Will the file be closed properly, or does using return somehow bypass the context manager ? Yes, it acts like the finally block after a try block, i.e. it always executes (unless the python process terminates in an unusual way of course). It is also mentioned in one of the examples of PEP-343 which is the specification for the with statement: with locked(myLock): # Code here executes with myLock held. The lock is # guaranteed to be released when the block is left (even # if via return or by an uncaught

Variable not being stored in the code when sheet changes

萝らか妹 提交于 2019-11-28 14:08:51
I'm quite new to coding and have encountered a probably very trivial issue. When I run the code line by line in the 'Trade_Sheet' tab, the variable Date_range is picked up correctly as a date previously copied, and updated for 7 days later. However the problem comes when I run the macro in the main tab 'Share_Calc_Tab' where the macro is situated. If I do so, it seems like the variable Date_range sets to 0, and while the rest of the operation is performed, the date will be missing. The code is below: Sub Audit_Trade() Dim Trade_Sheet As Worksheet Dim Share_Calc_Tab As Worksheet Dim lastrow As

How can I open multiple files (number of files unknown beforehand) using “with open” statement?

大城市里の小女人 提交于 2019-11-28 13:02:35
I specifically need to use with open statement for opening the files, because I need to open a few hundred files together and merge them using K-way merge. I understand, ideally I should have kept K low, but I did not foresee this problem. Starting from scratch is not an option now as I have a deadline to meet. So at this point, I need very fast I/O that does not store the whole/huge portion of file in memory (because there are hundreds of files, each of ~10MB). I just need to read one line at a time for K-way merge. Reducing memory usage is my primary focus right now. I learned that with open

Extracting the collection of unique values from a filter in VBA

倖福魔咒の 提交于 2019-11-28 12:40:11
I have a file which has rows extending to tens of thousands across 8 columns. One particular column contains the weekend date. I have to count the number of weekends present in this file. Is there a way to extract the data as shown in the image below? If we can extract and get the count of this collection, then the problem is solved. Please help. Thanks in advance! The following will take a series of three randomized upper-case letters from column A (25K values), put them into a dictionary as unique keys (13,382 values) and dump them back into column C on the same worksheet before sorting them

C# equivalent for Visual Basic keyword: 'With' … 'End With'?

时间秒杀一切 提交于 2019-11-28 12:13:59
In Visual Basic, if you are going to change multiple properties of a single object, there's a With/End With statement: Dim myObject as Object // ' Rather than writing: myObject.property1 = something myObject.property2 = something2 // ' You can write: with myObject .property1 = something .property2 = something2 ... End With I know C# can do it when creating a new object: Object myObject = new Object { property1 = something, property2 = something2, ...}; But how do I do that if myOject is already created (like what Visual Basic is doing)? You cannot do this in C#. This feature is specific to VB