with-statement

Javascript Sandbox

烂漫一生 提交于 2019-12-06 01:49:14
问题 I want to have developers write some custom apps for a site in Javascript but I want to sandbox it so they can't do anything naughty like redirect the user, set the body display to none etc etc. I have a namespace in Javascript where all the functions they'll ever need exist in there so I was thinking to create a sandbox would be a matter of: with(Namespace) { //App code goes here where they can only access Namespace.* } How is easy is it to get around this and what other methods can be done?

Calculate a series of weighted means in R for groups with different weightings

*爱你&永不变心* 提交于 2019-12-05 15:36:52
I have the following dataset (simple version of my actual data), 'data', and would like to calculate weighted means for variables x1 and x2, using weightings w1 and w2 respectively, split up into two groups (groups determined by the variable n). data <- data.frame(n = c(1,1,1,2,2,2), x1 = c(4,5,4,7,5,5), x2 = c(7,10,9,NaN,11,12), w1 = c(0,1,1,1,1,1), w2 = c(1,1,1,0,0,1)) I'm trying to do it using with() but get an error when I run this: with(data, aggregate(x = list(x1=x1, x2=x2), by = list(n = n), FUN = weighted.mean, w = list(w1 = w1,w2 = w2))) On the otherhand, if weights aren't specified

Other builtin or practical examples of python `with` statement usage?

给你一囗甜甜゛ 提交于 2019-12-05 15:30:28
Does anyone have a real world example outside of python's file object implementation of an __enter__ and __exit__ use case? Preferably your own, since what I'm trying to achieve is a better way to conceptualize the cases where it would be used. I've already read this . And, here's a link to the python documentation. There are many uses. Just in the standard library we have: sqlite3 ; using the connection as a context manager translates to committing or aborting the transaction. unittest ; using assertRaises as a context manager lets you assert an exception is raised, then test aspects of the

Python 'with' command

╄→гoц情女王★ 提交于 2019-12-05 15:17:31
Is this code with open(myfile) as f: data = f.read() process(data) equivalent to this one try: f = open(myfile) data = f.read() process(f) finally: f.close() or the following one? f = open(myfile) try: data = f.read() process(f) finally: f.close() This article: http://effbot.org/zone/python-with-statement.htm suggests (if I understand it correctly) that the latter is true. However, the former would make more sense to me. If I am wrong, what am I missing? It's equivalent to the latter one, because until open() successfully returns, f has no value, and should not be closed. According to the

Is it possible to have an optional with/as statement in python?

柔情痞子 提交于 2019-12-05 13:36:38
问题 Instead of this: FILE = open(f) do_something(FILE) FILE.close() it's better to use this: with open(f) as FILE: do_something(FILE) What if I have something like this? if f is not None: FILE = open(f) else: FILE = None do_something(FILE) if FILE is not None: FILE.close() Where do_something also has an "if FILE is None" clause, and still does something useful in that case - I don't want to just skip do_something if FILE is None. Is there a sensible way of converting this to with/as form? Or am I

How to use a python context manager inside a generator

北城余情 提交于 2019-12-05 09:53:34
问题 In python, should with-statements be used inside a generator? To be clear, I am not asking about using a decorator to create a context manager from a generator function. I am asking whether there is an inherent issue using a with-statement as a context manager inside a generator as it will catch StopIteration and GeneratorExit exceptions in at least some cases. Two examples follow. A good example of the issue is raised by Beazley's example (page 106). I have modified it to use a with

With statement in php

独自空忆成欢 提交于 2019-12-05 08:04:16
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 Not exactly the with statement, but you can use references in your example: $r = &$array[index]; $r->attr1 = val1; $r->attr2 = val2; $r->attr3 = val3; If needed with arrays as in your example, you can mimic the with statement by using the array

line-by-line file processing, for-loop vs with

佐手、 提交于 2019-12-04 23:36:00
I am trying to understand the trade offs/differences between these to ways of opening files for line-by-line processing with open('data.txt') as inf: for line in inf: #etc vs for line in open('data.txt'): # etc I understand that using with ensures the file is closed when the "with-block" (suite?) is exited (or an exception is countered). So I have been using with ever since I learned about it here. Re for -loop: From searching around the net and SO, it seems that whether the file is closed when the for -loop is exited is implementation dependent? And I couldn't find anything about how this

PostgreSQL WITH RECURSIVE performance

大憨熊 提交于 2019-12-04 16:39:52
问题 I have a simple question. Somehow I was unable to find a definitive answer. How much is WITH RECURSIVE syntax optimized in PostgreSQL? By that I mean: is it merely a syntactic sugar for a series of non recursive queries, OR is it more of a single statement that despite its complicated semantics has been optimized as a whole. A follow-up question - just about how much is it possible to optimize this kind of syntax? Of course some concrete data on the matter is most welcome. 回答1: My experience

multiprocessing returns “too many open files” but using `with…as` fixes it. Why?

混江龙づ霸主 提交于 2019-12-04 16:32:00
问题 I was using this answer in order to run parallel commands with multiprocessing in Python on a Linux box. My code did something like: import multiprocessing import logging def cycle(offset): # Do stuff def run(): for nprocess in process_per_cycle: logger.info("Start cycle with %d processes", nprocess) offsets = list(range(nprocess)) pool = multiprocessing.Pool(nprocess) pool.map(cycle, offsets) But I was getting this error: OSError: [Errno 24] Too many open files So, the code was opening too