python-2.x

why custom-build python 2.7.x in sublime text 3.x doesn't run accordingly

二次信任 提交于 2019-12-13 03:53:45
问题 def a(x): assert x>0,'invalid argument' print 'wow' a(2) a(0) this should first print "wow" and then it should raise an exception but instead it prints.The "wow" is splited as "wo" before "assert x>0" and after "AssertionError" on third last line and it keeps changing unpredictly but not once before "Traceback": Traceback (most recent call last): File "E:\Books\Python\think python\assert.py", line 6, in <module> a(0) File "E:\Books\Python\think python\assert.py", line 2, in a wo assert x>0,

schedule code to execute at some time later

廉价感情. 提交于 2019-12-13 00:16:36
问题 How can I schedule some code to be executed at a certain time later? I tried: import time import datetime time.sleep(420) print(datetime.datetime.now()) But this doesn't work if the Mac goes to sleep. To clarify, I need a script (well, some python function, which I could put into a separate script) to run at a precise time in the future. time.sleep doesn't meet my needs because if the computer sleeps during time.sleep 's timeout then the real-time delay is much longer than the one passed to

Python/Django SMTP DebugServer doesn't catch emails

試著忘記壹切 提交于 2019-12-12 18:28:58
问题 I'm starting the server (on mac) this way: python -m smtpd -n -c DebuggingServer localhost:9999 and i don't get any errors or any other notifications. I guess it means all is ok, correct me if i'm wrong. But when i send emails from django shell or from my app, using send_mail/mail_managers with fail_silently=False, i dont see any output on smtpd debug server. I dont get any SMTPErrors, and send_mail/mail_managers returns 1. I ran: lsof -i | grep LISTEN to see if anyone listens to port 9999,

point from a list into a dicitonary variable

。_饼干妹妹 提交于 2019-12-12 16:36:58
问题 Assume you have a list a = [3,4,1] I want with this information to point to the dictionary: b[3][4][1] Now, what I need is a routine. After I see the value, to read and write a value inside b's position. I don't like to copy the variable. I want to change variable b's content directly. 回答1: Assuming b is a nested dictionary, you could do reduce(dict.get, a, b) to access b[3][4][1] . For more general object types, use reduce(operator.getitem, a, b) Writing the value is a bit more involved:

Django mocking storage on model with update() throws error

巧了我就是萌 提交于 2019-12-12 12:57:41
问题 I have a small project and I'm working on some tests. Recently I asked a question: django-test-mocked-imagefield-prevent-upload-or-clean-after-test. I've made some progress in mocking the storage, but currently I'm running into an issue with mocking storage on a model with an update() in the save() method. Current situation: <..crop..> import mock current_storage = 'django.core.files.storage.default_storage._wrapped' def _mock_storage(): return mock.MagicMock(spec=Storage, name="StorageMock")

Python 2.7 IndentationError [closed]

半腔热情 提交于 2019-12-12 10:32:28
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . I am getting an IndentationError when trying to run my program in a Python Interpreter: line 127 global map ^ IndentationError: expected an indented block I am using python version 2.7 What's wrong with the following code?: def make_map(): global map 回答1: Python expects 4 spaces or a tab to indent and align code

Is “backporting” Python 3's `range` to Python 2 a bad idea?

你说的曾经没有我的故事 提交于 2019-12-12 10:31:46
问题 One of my classes requires assignments to be completed in Python, and as an exercise, I've been making sure my programs work in both Python 2 and Python 3, using a script like this: #!/bin/bash # Run some PyUnit tests python2 test.py python3 test.py One thing I've been doing is making range work the same in both versions with this piece of code: import sys # Backport Python 3's range to Python 2 so that this program will run # identically in both versions. if sys.version_info < (3, 0): range

python _2or3 module?

梦想的初衷 提交于 2019-12-12 09:53:08
问题 I am writing a module to let me write code in python 3, but still run it in 2. It looks surprisingly easy actually... anything else I should add? From my (limited) flailing on the interactive interpreter, the future imports do not affect python 3 and are viewed as redundant. # _2or3.py ''' Common usage: from __future__ import print_function, nested_scopes, division, absolute_import, unicode_literals from _2or3 import * ''' import sys if sys.version[0] == '2': range = xrange input = raw_input

What does python print() function actually do?

怎甘沉沦 提交于 2019-12-12 08:34:59
问题 I was looking at this question and started wondering what does the print actually do. I have never found out how to use string.decode() and string.encode() to get an unicode string "out" in the python interactive shell in the same format as the print does. No matter what I do, I get either UnicodeEncodeError or the escaped string with "\x##" notation... This is python 2.x, but I'm already trying to mend my ways and actually call print() :) Example: >>> import sys >>> a = '\xAA\xBB\xCC' >>>

Where does Python store the name binding of function closure?

天涯浪子 提交于 2019-12-12 07:58:08
问题 So recently I understand the concept of function closure. def outer(): somevar = [] assert "somevar" in locals() and not "somevar" in globals() def inner(): assert "somevar" in locals() and not "somevar" in globals() somevar.append(5) return somevar return inner function = outer() somevar_returned = function() assert id(somevar_returned) == id(function.func_closure[0].cell_contents) As much as I understand, the objective of function closure is to keep an active reference to the object, in