with-statement

Return file handles opened with with open?

◇◆丶佛笑我妖孽 提交于 2020-08-04 06:33:38
问题 I'm creating software where I want to accept compressed files. Since files are read/written everywhere, I created a utility function for opening files, that handles opening/closing for me, for some compressed filetypes. Example code: def return_file_handle(input_file, open_mode="r"): """ Handles compressed and uncompressed files. Accepts open modes r/w/w+ """ if input_file.endswith(".gz") with gzip.open(input_file, open_mode) as gzipped_file_handle: return gzipped_file_handle The problem is,

VB.NET Nested With statements from different scopes

拥有回忆 提交于 2020-05-09 07:52:29
问题 I am wondering if this is possible. I have a List Table (lstTable) that is on the same form that I am trying to fill in with information from a public structure (ELEM_DATA). I understand nested with statements will work if it is within the same scope but how can I do this with example 2 below: Example 1: With me.lstTable.Items(RECORD) .SubItems(1).text = ELEM_DATA(RECORD).name .SubItems(2).text = ELEM_DATA(RECORD).number end with Example 2: With me.lstTable.Items(RECORD) With ELEM_DATA(RECORD

Custom 'with open()' statement in Python: generator didn't yield error

守給你的承諾、 提交于 2020-01-15 12:48:08
问题 I have a class for a file, from which you can parse data, write data etc. I want to use it from any application like this: f = MyFileClass() # __init__ method puts a lot of default data in object with f.open() as file: # where f.open() is custom MyFileClass method file.write("foo") # file should close automatically after this I tried this: # it's in MyFileClass() from contextlib import contextmanager @contextmanager def open(self): try: return open(os.path.join(self.directory, self.name),

With statement in php

末鹿安然 提交于 2020-01-02 03:41:12
问题 I am wondering if there is something similar to javascript or VB's with statement but in php The way this works, for example in VB is shown below. The two code snippets do the same effect: array[index].attr1 = val1; array[index].attr2 = val2; array[index].attr3 = val3; is equal to : With(array[index]) .attr1 = val1 .attr2 = val2 .attr3 = val3 End With 回答1: Not exactly the with statement, but you can use references in your example: $r = &$array[index]; $r->attr1 = val1; $r->attr2 = val2; $r-

Equivalent of with(from Pascal) to C/C++

时光毁灭记忆、已成空白 提交于 2019-12-30 10:41:56
问题 What is the equivalent of with from Pascal language in C/C++ language? A with statement is a shorthand for referencing the fields of a record or the fields, properties, and methods of an object. Example With (Object) do begin Width:=200; Height:=300; end; Is Equivalent with: Object.Width=200; Object.Height=200; 回答1: I don't believe that there is any direct equivalent to that statement in c/c++. If your objective is to avoid repeatedly typing "Object", then I suppose you could use a reference

Oracle — WITH CLAUSE => MERGE? (Syntax error, )

拟墨画扇 提交于 2019-12-30 08:03:48
问题 I'm trying to get the WITH clause to work with merge in Oracle, but for some reason I can't get it working. I'm sure it is something obvious, but I just haven't seen it. -- behold, the wonders of fake data WITH X AS ( SELECT 'moo' AS COW, 'woof' AS CAT, (SELECT MAX( DECIBELS ) FROM ANIMALIA WHERE COW = 'moo' ) AS DECIBELS FROM DUAL ) MERGE INTO ANIMALIA D USING X WHEN MATCHED THEN UPDATE SET D.COW = X.COW; EDIT I actually found out how to manage this (before I submitted the question), but I

Re-assign exception from within a python __exit__ block

浪子不回头ぞ 提交于 2019-12-30 02:00:30
问题 From within an __exit__ block in a custom cursor class I want to catch an exception so I can in turn throw a more specific exception. What is the proper way to do this? class Cursor: def __enter__(self): ... def __exit__(self, ex_type, ex_val, tb): if ex_type == VagueThirdPartyError: # get new more specific error based on error code in ex_val and # return that one in its place. return False # ? else: return False Raising the specific exception within the __exit__ block seems like a hack, but

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

本秂侑毒 提交于 2019-12-29 01:28:10
问题 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

How to avoid accidentally implicitly referring to properties on the global object?

∥☆過路亽.° 提交于 2019-12-28 04:08:11
问题 Is it possible to execute a block of code without the implicit with(global) context that all scripts seem to have by default? For example, in a browser, would there be any way to set up a script so that a line such as const foo = location; throws Uncaught ReferenceError: location is not defined instead of accessing window.location , when location has not been declared first? Lacking that, is there a way that such an implicit reference could result in a warning of some sort? It can be a source

Oracle WITH clause returns no data

◇◆丶佛笑我妖孽 提交于 2019-12-25 13:47:53
问题 I am trying to use a WITH clause in Oracle, but it is not returning any data. This is the query I am trying to run... with test as (select count(*) from my_table) select * from test; When I run this code, I get back the count of the records in my_table select count(*) from my_table I am on Oracle 10g so the query should work... select * from v$version; yields Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi PL/SQL Release 10.2.0.4.0 - Production CORE 10.2.0.4.0 Production TNS