python-2.x

Equivalent of __func__ (from C) in Python

折月煮酒 提交于 2019-12-20 10:47:37
问题 I want to create some kind of debugging output for python and want to pass the functionname to another function for output. The only reasonable way I found to do so was: def foobar(): print 'hello world' print foobar.__name__ is there something that does the same thing for the current function to improve copy and pasting of that line? like the C equivalent printf("%s", __func__) . 回答1: sys._getframe can do that: import sys def foobar(): print sys._getframe().f_code.co_name 回答2: One thing you

Convert unicode with utf-8 string as content to str

≯℡__Kan透↙ 提交于 2019-12-20 10:04:43
问题 I'm using pyquery to parse a page: dom = PyQuery('http://zh.wikipedia.org/w/index.php', {'title': 'CSS', 'printable': 'yes', 'variant': 'zh-cn'}) content = dom('#mw-content-text > p').eq(0).text() but what I get in content is a unicode string with utf-8 encoded content: u'\xe5\xb1\x82\xe5\x8f\xa0\xe6\xa0\xb7\xe5\xbc\x8f\xe8\xa1\xa8...' how could I convert it to str without lost the content? to make it clear: I want conent == '\xe5\xb1\x82\xe5\x8f\xa0\xe6\xa0\xb7\xe5\xbc\x8f\xe8\xa1\xa8' not

Can't get raw_input to return a number

喜你入骨 提交于 2019-12-20 07:47:43
问题 print "How old are you?", age = raw_input() print "How tall are you in inches?", height = raw_input() print "How much do you weigh in pounds", weight = raw_input() print "So, you are %r years old, %r inches tall, and %d kilograms." % ( age, height, weight / 2.2) So I am new to code and this is my code. When I use terminal to compile it, I get this: How old are you? 1 How tall are you in inches? 1 How much do you weigh in pounds 1 Traceback (most recent call last): File "ex11.py", line 9, in

How to pass a list as a function's arguments

别说谁变了你拦得住时间么 提交于 2019-12-20 07:19:23
问题 If I have a function: def run(time, message, time_span_pattern): ... And a list like: run_args = ['1s', '1 second alarm', <_sre.SRE_Pattern object at 0x100435680>] How can I pass the list, as separate arguments, to run? Is there a builtin way to do this, or am I forced to reference each element individually and by index? 回答1: You're looking for: run(*run_args) This is explained in more detail in this StackOverflow answer about the star and double star operator It's also covered in the python

“Unexpected EOF while parsing” after a “try” statement

青春壹個敷衍的年華 提交于 2019-12-20 06:39:54
问题 I'm really, really new to Python and was making a small test program. Here is my code: def start (): print ("This is where text would be") prompt_sta () def prompt_sta (): prompt_0=raw_input("Input a Command: ") try: if prompt_0 == 'Okay': next_screen () else: print ('Type Okay.') prompt_sta () when I try to run it I get the "Unexpected EOF while parsing" error. 回答1: For the EOF error, you can just get rid of that try: like so def start (): print ("This is where text would be") prompt_sta ()

can a function be static and non-static in python 2

痞子三分冷 提交于 2019-12-20 06:13:44
问题 Lets say I have this class: class Test(object): def __init__(self, a): self.a = a def test(self, b): if isinstance(self, Test): return self.a + b else: return self + b This would ideally in my world do this: >>> Test.test(1,2) 3 >>> Test(1).test(2) 3 Now this doesn't work because you get this error: TypeError: unbound method test() must be called with Test instance as first argument (got int instance instead) In python3 this works fine, and I have the sneaking suspicion this is possible with

What does this error message mean in appengine?

最后都变了- 提交于 2019-12-20 04:23:25
问题 Search failed Traceback (most recent call last): File "/base/data/home/apps/s~montaoproject/2013e.368508855356793432/search_demo.py", line 87, in find_documents return index.search(query) File "/python27_runtime/python27_lib/versions/1/google/appengine/api/search/search.py", line 2732, in search _CheckStatus(response.status()) File "/python27_runtime/python27_lib/versions/1/google/appengine/api/search/search.py", line 413, in _CheckStatus raise _ERROR_MAP[status.code()](status.error_detail())

In Python 2, can I pass a list to the percent-format operator?

血红的双手。 提交于 2019-12-20 02:49:35
问题 I have a list of things I'd like to print using a format string. In Python 3-style, using "a string".format(arg,arg,arg) , this is easy. I can just replace with arguments with *mylist , like mylist = [1,2,3] "{} {} {}".format(*mylist) I can't seem to make this work with the older percent-formatting. I've tried stuff like "%i %i %i" % mylist , %i %i %i" % (mylist) , and %i %i %i" % (*mylist) but I just keep getting syntax errors or "not enough arguments". Is this impossible in 2.x style? ETA:

Subpackages and relative imports in PyCharm

南楼画角 提交于 2019-12-20 02:09:22
问题 I am using python 2: python --version Python 2.7.13 :: Continuum Analytics, Inc. I have the following project structure: . └── foo ├── bar1 │ ├── __init__.py │ └── mod1.py ├── bar2 │ ├── __init__.py │ └── mod2.py ├── __init__.py └── start.py start.py from foo.bar2.mod2 import mod2_f mod2_f() mod1.py def mod1_f(): print "mod1_f" mod2.py from foo.bar1.mod1 import mod1_f def mod2_f(): mod1_f() print "mod2_f" If I run start.py from an IDE things work ok. However using something like this: python

Subpackages and relative imports in PyCharm

妖精的绣舞 提交于 2019-12-20 02:07:10
问题 I am using python 2: python --version Python 2.7.13 :: Continuum Analytics, Inc. I have the following project structure: . └── foo ├── bar1 │ ├── __init__.py │ └── mod1.py ├── bar2 │ ├── __init__.py │ └── mod2.py ├── __init__.py └── start.py start.py from foo.bar2.mod2 import mod2_f mod2_f() mod1.py def mod1_f(): print "mod1_f" mod2.py from foo.bar1.mod1 import mod1_f def mod2_f(): mod1_f() print "mod2_f" If I run start.py from an IDE things work ok. However using something like this: python