with-statement

Reference object instance created using “with” in Delphi

北城以北 提交于 2019-11-26 17:04:23
问题 is there a way to reference an object instance that is created using the "with" statement? Example: with TAnObject.Create do begin DoSomething(instance); end; Where DoSomething would use the instance reference as if you were passing an instance from a variable declared reference to the object created. Example: AnObject := TAnObject.Create; Thanks. 回答1: Well, you can use such approach: // implement: type TSimpleMethod = procedure of object; function GetThis(const pr: TSimpleMethod): TObject;

Alternative to contextlib.nested with variable number of context managers

无人久伴 提交于 2019-11-26 16:39:59
问题 We have code that invokes a variable number of context managers depending on runtime parameters: from contextlib import nested, contextmanager @contextmanager def my_context(arg): print("entering", arg) try: yield arg finally: print("exiting", arg) def my_fn(items): with nested(*(my_context(arg) for arg in items)) as managers: print("processing under", managers) my_fn(range(3)) However, contextlib.nested is deprecated since Python 2.7: DeprecationWarning: With-statements now directly support

Count the number of rows in another sheet

允我心安 提交于 2019-11-26 15:33:25
I have looked at the suggested questions to find the answer to my problem. The closest question is called: Count number of rows in a different Excel Sheet Count number of rows in a different Excel Sheet The solution to that problem does not work for me. I am trying to count the number of rows in a range in a different worksheet than the active worksheet. Here is my code: Sub verbflashcards() Dim wordcount As Long With Worksheets("Verbs") wordcount = .Range(Cells(4, 1), Cells(4, 1).End(xlDown)).Rows.Count End With MsgBox (wordcount) End Sub I have a worksheet called Verbs and it is the second

Is Delphi “with” keyword a bad practice?

喜你入骨 提交于 2019-11-26 14:41:55
I been reading bad things about the with keyword in delphi but, in my opinion, if you don't over use it. It can make your code look simple. I often put all my TClientDataSets and TFields in TDataModules. So in my forms I had code like this procedure TMyForm.AddButtonClick(Sender: TObject); begin with LongNameDataModule do begin LongNameTable1.Insert; LongNameTable1_Field1.Value := "some value"; LongNameTable1_Field2.Value := LongNameTable2_LongNameField1.Value; LongNameTable1_Field3.Value := LongNameTable3_LongNameField1.Value; LongNameTable1_Field4.Value := LongNameTable4_LongNameField1.Value

RAII in Python - automatic destruction when leaving a scope

纵饮孤独 提交于 2019-11-26 13:06:41
问题 I\'ve been trying to find RAII in Python. Resource Allocation Is Initialization is a pattern in C++ whereby an object is initialized as it is created. If it fails, then it throws an exception. In this way, the programmer knows that the object will never be left in a half-constructed state. Python can do this much. But RAII also works with the scoping rules of C++ to ensure the prompt destruction of the object. As soon as the variable pops off the stack it is destroyed. This may happen in

The VB.NET 'With' Statement - embrace or avoid?

不羁的心 提交于 2019-11-26 12:25:26
问题 At work, I\'m frequently working on projects where numerous properties of certain objects have to be set during their construction or early during their lifetime. For the sake of convenience and readability, I often use the With statement to set these properties. I find that With Me.Elements .PropertyA = True .PropertyB = \"Inactive\" \' And so on for several more lines End With Looks much better than Me.Elements.PropertyA = True Me.Elements.PropertyB = \"Inactive\" \' And so on for several

Using python “with” statement with try-except block

半世苍凉 提交于 2019-11-26 11:58:08
问题 Is this the right way to use the python \"with\" statement in combination with a try-except block?: try: with open(\"file\", \"r\") as f: line = f.readline() except IOError: <whatever> If it is, then considering the old way of doing things: try: f = open(\"file\", \"r\") line = f.readline() except IOError: <whatever> finally: f.close() Is the primary benefit of the \"with\" statement here that we can get rid of three lines of code? It doesn\'t seem that compelling to me for this use case

How do I mock an open used in a with statement (using the Mock framework in Python)?

北战南征 提交于 2019-11-26 11:04:33
How do I test the following code with mocks (using mocks, the patch decorator and sentinels provided by Michael Foord's Mock framework ): def testme(filepath): with open(filepath, 'r') as f: return f.read() The way to do this has changed in mock 0.7.0 which finally supports mocking the python protocol methods (magic methods), particularly using the MagicMock: http://www.voidspace.org.uk/python/mock/magicmock.html An example of mocking open as a context manager (from the examples page in the mock documentation): >>> open_name = '%s.open' % __name__ >>> with patch(open_name, create=True) as mock

Equivalence of “With…End With” in C#? [duplicate]

荒凉一梦 提交于 2019-11-26 06:48:15
问题 This question already has answers here : With block equivalent in C#? (15 answers) Closed 6 years ago . I know that C# has the using keyword, but using disposes of the object automatically. Is there the equivalence of With...End With in Visual Basic 6.0? 回答1: C# doesn't have an equivalent language construct for that. 回答2: It's not equivalent, but would this syntax work for you? Animal a = new Animal() { SpeciesName = "Lion", IsHairy = true, NumberOfLegs = 4 }; 回答3: There is no equivalent, but

Why should I not use “with” in Delphi?

情到浓时终转凉″ 提交于 2019-11-26 05:39:14
问题 I\'ve heard many programmers, particularly Delphi programmers scorn the use of \'with\'. I thought it made programs run faster (only one reference to parent object) and that it was easier to read the code if used sensibly (less than a dozen lines of code and no nesting). Here\'s an example: procedure TBitmap32.FillRectS(const ARect: TRect; Value: TColor32); begin with ARect do FillRectS(Left, Top, Right, Bottom, Value); end; I like using with . What\'s wrong with me? 回答1: One annoyance with