python-3.4

difference between python2 and python3 - int() and input()

梦想的初衷 提交于 2019-12-04 03:17:57
问题 I wrote below python code. And I found that python2 and python3 has totally difference running result for input of 1.1. Why is there such difference between python2 and python3? For me, int(1.1) should be 1, then position is valid index 1 within range 0,1,2. So can you please explain why python3 has such result? s=[1,2,3] while True: value=input() print('value:',value) try: position=int(value) print('position',position) print('result',s[position]) except IndexError as err: print('out of index

What unit of time does timeit return?

拈花ヽ惹草 提交于 2019-12-04 02:46:47
问题 I don't know how to interpret the output from Python's timeit.timeit() function. My code is as follows: import timeit setup = """ import pydash list_of_objs = [ {}, {'a': 1, 'b': 2, 0: 0}, {'a': 1, 'c': 1, 'p': lambda x: x} ] """ print(timeit.timeit("pydash.filter_(list_of_objs, {'a': 1})", setup=setup)) The output from this is 11.85382745500101 . How do I interpret this number? 回答1: The return value is seconds as a float . It is the total time taken to run the test (not counting the setup),

Why can't I catch SIGINT when asyncio event loop is running?

半世苍凉 提交于 2019-12-04 02:10:39
Using Python 3.4.1 on Windows, I've found that while executing an asyncio event loop , my program can't be interrupted (i.e. by pressing Ctrl+C in the terminal). More to the point, the SIGINT signal is ignored. Conversely, I've determined that SIGINT is handled when not in an event loop. Why is it that SIGINT is ignored when executing an asyncio event loop? The below program should demonstrate the problem - run it in the terminal and try to stop it by pressing Ctrl+C, it should keep running: import asyncio import signal # Never gets called after entering event loop def handler(*args): print(

What File Descriptor object does Python AsyncIO's loop.add_reader() expect?

大城市里の小女人 提交于 2019-12-03 20:01:52
问题 I'm trying to understand how to use the new AsyncIO functionality in Python 3.4 and I'm struggling with how to use the event_loop.add_reader(). From the limited discussions that I've found it looks like its for reading the standard out of a separate process as opposed to the contents of an open file. Is that true? If so it appears that there's no AsyncIO specific way to integrate standard file IO, is this also true? I've been playing with the following code. The output of the following gives

Python type hinting without cyclic imports

时光毁灭记忆、已成空白 提交于 2019-12-03 18:27:44
问题 I'm trying to split my huge class into two; well, basically into the "main" class and a mixin with additional functions, like so: main.py file: import mymixin.py class Main(object, MyMixin): def func1(self, xxx): ... mymixin.py file: class MyMixin(object): def func2(self: Main, xxx): # <--- note the type hint ... Now, while this works just fine, the type hint in MyMixin.func2 of course can't work. I can't import main.py , because I'd get a cyclic import and without the hint, my editor

How do I connect asyncio.coroutines that continually produce and consume data?

倾然丶 夕夏残阳落幕 提交于 2019-12-03 15:21:42
I am trying to learn how to (idiomatically) use Python 3.4's asyncio . My biggest stumbling block is how to "chain" coroutines that continually consume data, update state with it, and allow that state to be used by another coroutine. The observable behaviour I expect from this example program is simply to periodically report on the sum of numbers received from a subprocess. The reporting should happen at roughly the same rate the Source object recieves numbers from the subprocess. IO blocking in the reporting function should not block reading from the subprocess. If the reporting function

PyQt5 draggable frameless window

别说谁变了你拦得住时间么 提交于 2019-12-03 14:06:43
问题 I found an example to set borders on a frameless window, however it's not draggable. How can I make a frameless window draggable? Especially if I can see an example it'll be awesome. Here is my example code(normally the code is longer, that's why there are much libraries just don't mind them); from PyQt5.QtWidgets import (QMessageBox,QApplication, QWidget, QToolTip, QPushButton, QDesktopWidget, QMainWindow, QAction, qApp, QToolBar, QVBoxLayout, QComboBox,QLabel,QLineEdit,QGridLayout,QMenuBar

String performance - Python 2.7 vs Python 3.4 under Windows 10 vs. Ubuntu

非 Y 不嫁゛ 提交于 2019-12-03 14:01:16
Use case A simple function which checks if a specific string is in another string at a position which is a multiple of 3 (see here for a real world example , finding stop codons in a DNA sequence). Functions sliding_window : takes a string of length 3 compares it to the search string, if they are identical moves 3 characters forward. incremental_start : tries to find the search string, if the found position is not a multiple of 3, it tries to find the next position after the found position. Please note: The sample data is just to make sure that each function has to go through the complete

Get system information(CPU speed-Total RAM-Graphic Card model etc.) under Windows

放肆的年华 提交于 2019-12-03 13:23:19
问题 I searched a lot but couldn't find anything useful. Is this possible to get system information like; CPU: Intel Core i7-3770K CPU @3.5Ghz RAM: 8GB Graphic Card: NVIDIA GeForce GTX 680 under Windows? How can I reach this output? Edit: platform.processor() is not giving the output that I want. So that is useless for me. 回答1: I've been wondering how to do this myself for a while now, so I dug around a bit and came up with this solution using wmi (which requires pywin32 ). Of course, it goes

python3: singledispatch in class, how to dispatch self type

天大地大妈咪最大 提交于 2019-12-03 12:39:07
Using python3.4. Here I want use singledispatch to dispatch different type in __mul__ method . The code like this : class Vector(object): ## some code not paste @functools.singledispatch def __mul__(self, other): raise NotImplementedError("can't mul these type") @__mul__.register(int) @__mul__.register(object) # Becasue can't use Vector , I have to use object def _(self, other): result = Vector(len(self)) # start with vector of zeros for j in range(len(self)): result[j] = self[j]*other return result @__mul__.register(Vector) # how can I use the self't type @__mul__.register(object) # def _