python-2.x

How can I add a Python version to the Python Launcher?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 11:29:28
问题 On Windows, how can I add a Python version to the Python Launcher? I have python 3.7 installed alongside 2.7. I've made sure that the PATH variable and the registry keys are correct. One thing I will say is that I can't see any other Python environment variables on my machine (such as PYTHONPATH ), or either of the py.ini files descibed here. If possible, I would like the solution to also work with python distribition, such as Anaconda. This is the same question as py launcher does not find

serialwin32.py and serialutil.py error when run Invensense demo python client

跟風遠走 提交于 2019-12-24 07:51:15
问题 Good days, I'm new to python and trying to run a demo provided by Invensense.(9-axis MPU9250 connects a STM32F407G discovery board, I used code and python client in motion_driver_6.12 which downloaded from Invensense website.) the whole python part are python2.7, pysearil, pygame. I searched my issues in Stackoverflow, but the specific situations are a little different, and most of the solutions are useless for me. First, I show my issues. UART connects the PC, run Invensense's python client

Why doesn't Python's filter(predicate, set) return a set?

与世无争的帅哥 提交于 2019-12-24 04:32:09
问题 Why was Python's filter designed such that if you run filter(my_predicate, some_set) , I get back a list object return than a set object? Are there practical cases where you would not want the result to be a set ...? 回答1: You can do a set comprehension. {my_predicate(x) for x in some_set} # mapping {x for x in some_set if my_predicate(x)} # filtering such as In [1]: s = set([1,2,3]) In [2]: {x%2 for x in s} Out[2]: {0, 1} Many of the "functional" functions in Python 2 are standardized on

Why doesn't Python's filter(predicate, set) return a set?

北战南征 提交于 2019-12-24 04:32:07
问题 Why was Python's filter designed such that if you run filter(my_predicate, some_set) , I get back a list object return than a set object? Are there practical cases where you would not want the result to be a set ...? 回答1: You can do a set comprehension. {my_predicate(x) for x in some_set} # mapping {x for x in some_set if my_predicate(x)} # filtering such as In [1]: s = set([1,2,3]) In [2]: {x%2 for x in s} Out[2]: {0, 1} Many of the "functional" functions in Python 2 are standardized on

Python2 and Python3: __init__ and __new__

…衆ロ難τιáo~ 提交于 2019-12-24 03:47:15
问题 I have read other questions which explain the difference between __init__ and __new__ but I just do not understand why in the following code with python 2 out: init and Python3: new init The sample code: class ExampleClass(): def __new__(cls): print ("new") return super().__new__(cls) def __init__(self): print ("init") example = ExampleClass() 回答1: To use __new__ in Python 2.x, the class should be new-style class (class derived from object ). And call to super() is different from that of

Towers of Hanoi with “counter” in python

喜夏-厌秋 提交于 2019-12-24 01:54:21
问题 I have written a code for "Towers of Hanoi" in python and I am trying to add a counter to show how many times it has run. I tried several things like a while loop and for loops etc. but it doesn't work. I am sure that the answer is pretty easy but my brain is running on the lowest setting right now. My code looks like this: def Hanoi(n, src, dst, tmp): if n > 0: Hanoi(n - 1, src, tmp, dst) print "Move disc", chr(64 + n), "From tower", src, "to tower", dst Hanoi(n - 1, tmp, dst, src) Hanoi(4,0

wait function that uses while

廉价感情. 提交于 2019-12-24 00:44:27
问题 I just wrote a function that looked like this: def block_for(seconds): """Wait at least seconds, this function should not be affected by the computer sleeping.""" end_time = datetime.datetime.now() + datetime.timedelta(seconds) while datetime.datetime.now() < end_time: pass Can anything bad come of this? Should there ideally be something inside the while loop? 回答1: maybe putting time.sleep(1) in the while loop will require less cycles? Or def block_for(seconds): """Wait at least seconds, this

Decorate class-method with class-decorator

≡放荡痞女 提交于 2019-12-24 00:29:12
问题 I'm trying to decorate class method using class-decorator. For example class MyDecorator(object): def __init__(self,param1): self.param1 = param1 # do some action with param1 def __call__(self,func): def wrapped(*args,**kwargs): print "in wrapper:" func(*args,**kwargs) return wrapped and my some class: class A: @MyDecorator("Blablabla") def func1(arg1,arg2,arg3): print (arg1,arg2,arg3) but when I do the next action: a = A() a.func(1,2,3) I get the following error: TypeError: func1() takes

What is the best way to use OR in python regex

↘锁芯ラ 提交于 2019-12-23 23:13:27
问题 I'm doing an homework on regex and having some difficulties with OR . Given the following strings: avc7fsrd5vcc12vfscsrwt1qw7eetrs&fsrsy should return t1 s fdjhads jhf&5672t3zcxvb,m654godjhfjdyeuyr123jfjjdjfjdfj77djsfhdjhfdsf99 should return t3 go 123 77 The first part is to extract t with some number and then extract s or go depending on what comes first. If its go, then we need to extract two numbers afterward, otherwise stop. This is the regex I'm using '(t[0-9]).*?(go).*?([0-9]+).*?([0-9]

What type of line breaks does a Python script normally have?

自闭症网瘾萝莉.ら 提交于 2019-12-23 20:43:09
问题 My boss keeps getting annoyed at me for having Windows line breaks in my Python scripts, but I can't for the life of me work out how they are causing him a problem. Is '\r\n' the normal line-break for a Python script? Or does that only happen on IDLE for PC? PS: OK, it seems when I write the script on a Mac that it has '\n's, but is there any way that '\r\n' will cause a problem? Edit: OK... now I'm totally confused. When I interpret files written in Windows in Python they all spit out when I