built-in

Is it OK to raise a built-in exception, but with a different message, in Python?

梦想的初衷 提交于 2019-12-05 09:22:08
问题 Is it OK to raise a built-in exception with a custom text? or to raise a built-in warning also with custom text? The documentation reads: exception ValueError: Raised when a built-in operation or function receives an argument (…) Is it implied that only built-in operations should raise a ValueError exception? In practice, I understand that it is safe to create an exception class that inherits from ValueError or Exception. But is it OK not to do that, and directly raise a ValueError("custom

“delete” - restore native function not working for changed prototype, how then?

我怕爱的太早我们不能终老 提交于 2019-12-05 09:04:25
if you change native function like this: window.open= function (a,b,c) { alert(2); } then later you can just delete window.open and it would restore original function, BUT: if you change its prototype like this: window.__proto__.open= function (a,b,c) { alert(3); } then delete won't do anything =\ any ideas how to restore it now? When you change window.open to something else, e.g. using window.open = 'something else'; , then you're shadowing the open method from the prototype; // Looking up window.open (in the prototype chain).... window.open; // Found, result == 'something else' window._

Django Template Filter Syntax error

佐手、 提交于 2019-12-05 08:47:45
问题 I am trying to use django's built in 'default' filter using this code {% load sekizai_tags static compress i18n %} [...] <title>{{ title|default:"nothing" }}</title> But it gives me the following exception django.template.base.TemplateSyntaxError: default requires 2 arguments, 1 provided I am using the following settings for my Template Backend TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [ str(APPS_DIR.path('templates')), ], 'OPTIONS': { 'debug':

What is the 2nd argument for the iter function in Python?

帅比萌擦擦* 提交于 2019-12-05 06:54:05
问题 Let's consider a file: $ echo -e """This is a foo bar sentence .\nAnd this is the first txtfile in the corpus .""" > test.txt $ cat test.txt This is a foo bar sentence . And this is the first txtfile in the corpus . And when I want to read the file by character, I can do https://stackoverflow.com/a/25071590/610569: >>> fin = open('test.txt') >>> while fin.read(1): ... fin.seek(-1,1) ... print fin.read(1), ... T h i s i s a f o o b a r s e n t e n c e . A n d t h i s i s t h e f i r s t t x t

How to disable a built-in command in vim

烈酒焚心 提交于 2019-12-05 03:21:30
In vim, when I hit :wq it is almost always an accident that occurred when attempting to input :w . I would like to disable :wq . The closest I found is cmap , but it has some odd behavior. If I do something like :cmap wq w I can no longer even input :wq ; it just remaps the keystroke sequence wq to w in command mode. Now I cannot, for example, input a search/replace command on a string containing wq . I would just like to alias the exact command :wq to :w or a no-op. Is there a way to do this? EDIT: clarified why :cmap is not an option for me A better solution can be: :cabbrev wq w But I'm not

“Assume” clause in gcc

眉间皱痕 提交于 2019-12-05 01:40:03
Does gcc (latest versions: 4.8, 4.9) have an "assume" clause similar to __assume() built-in supported by icc? E.g., __assume( n % 8 == 0 ); In your example you want to inform the compiler that N is a multiple of 8. You can do this simply by inserting the line N = N & 0xFFFFFFF8; in your code (if N is a 32-bit integer). This doesn't change N , because N is a multiple of 8, but since GCC 4.9 the compiler seems to understand that N is a multiple of 8, after this line. This is shown by the next example, in which two float vectors are added: int add_a(float * restrict a, float * restrict b, int N)

Remove all occurrences of several chars from a string

╄→尐↘猪︶ㄣ 提交于 2019-12-04 23:05:13
Is there a pythonic way to do what the str.strip() method does, except for all occurrences, not just those at the beginning and end of a string? Example: >> '::2012-05-14 18:10:20.856000::'.strip(' -.:') >> '2012-05-14 18:10:20.856000' I want >> '::2012-05-14 18:10:20.856000::'.crazy_function(' -.:') >> '20120514181020856000' Does Python provides me a built-in crazy_function ??? I could easily do it programatically, but I want to know if there is a built-in for that. Couldn't find one. Thank you for your help. Use the translate function to delete the unwanted characters: >>> '::2012-05-14 18

Why Is The property Decorator Only Defined For Classes?

梦想的初衷 提交于 2019-12-04 18:39:31
问题 tl;dr: How come property decorators work with class-level function definitions, but not with module-level definitions? I was applying property decorators to some module-level functions, thinking they would allow me to invoke the methods by mere attribute lookup. This was particularly tempting because I was defining a set of configuration functions, like get_port , get_hostname , etc., all of which could have been replaced with their simpler, more terse property counterparts: port , hostname ,

how to overwrite a builtin method of javascript native objects

梦想与她 提交于 2019-12-04 09:31:45
Lets say we have alert method of window object. I would like to enhance it with nice alertbox. Also I want to save the existing alert method so that we can switch back once our application is over. Something like this, but its throwing error in firefox console. window.prototype.alert = function(){ } You can; var base = window.alert; window.alert = function(message) { document.getElementById("myalertwidget").innerHTML = message; return base.apply(this, arguments); }; There is no window.prototype object. window is a global object of javascript context and it is not created from the prototype.

How to stop myself overwriting Python functions when coding?

有些话、适合烂在心里 提交于 2019-12-04 06:35:55
A source of constant headache when tracking down bugs in my Python code are seemingly innocuous snippets like this: list = ['a', 'b', 'c', 'c'] list(set(list)) This fails because I've overwritten the function list() with the variable list. A contrived example obviously, but the point is Python happily lets me overwrite built-in functions with variables. I realise this is a crucial feature in Python but I would quite like it if the interpreter would warn me when I do it in my code as I usually don't mean to do this. Can anyone suggest a solution (other than just being more careful) - as I keep