typing

printing slowly (Simulate typing)

匿名 (未验证) 提交于 2019-12-03 01:52:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I am trying to make a textual game in python. All goes well however, I would like to make a function that will allow me to print something to the terminal, but in a fashion hat looks like typing. Currently I have: def print_slow ( str ): for letter in str : print letter , time . sleep (. 1 ) print_slow ( "junk" ) The output is: j u n k Is there a way to get rid of the spaces between the letters? 回答1: In Python 2.x you can use sys.stdout.write instead of print : for letter in str : sys . stdout . write ( letter ) time . sleep (. 1 )

What are variable annotations in Python 3.6?

匿名 (未验证) 提交于 2019-12-03 01:49:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Python 3.6 is about to be released. PEP 494 -- Python 3.6 Release Schedule mentions the end of December, so I went through What's New in Python 3.6 to see they mention the variable annotations : PEP 484 introduced standard for type annotations of function parameters, a.k.a. type hints. This PEP adds syntax to Python for annotating the types of variables including class variables and instance variables: primes : List [ int ] = [] captain : str # Note: no initial value! class Starship : stats : Dict [ str , int ] = {} Just as for

typing.Any vs object?

匿名 (未验证) 提交于 2019-12-03 01:38:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Is there any difference between using typing.Any as opposed to object in typing? For example: def get_item(L: list, i: int) -> typing.Any: return L[i] Compared to: def get_item(L: list, i: int) -> object: return L[i] 回答1: Yes, there is a difference. Although in Python 3, all objects are instances of object , including object itself, only Any documents that the return value should be disregarded by the typechecker. The Any type docstring states that object is a subclass of Any and vice-versa: >>> import typing >>> print(typing.Any.__doc__)

Any way to get IntelliJ-like autocompletion in Eclipse?

匿名 (未验证) 提交于 2019-12-03 01:29:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I'm stuck with Eclipse in one project and I would like to have autocompletion similar to that of IntelliJ - two major problems are: Start typing and Eclipse would automatically suggest all possibilities When there is item selected in dropdown list you can accept it not only with enter key but with dots, spacebars... 回答1: There is something like that in Eclipse I think, certainly in the Eclipse Helios. When typing you press CTRL+SPACE, or when you typed variable name '.' will start auto-complete options. Also here someone suggests

Use offline voice-to-text in Android 4.1 (Jelly Bean) from my application?

匿名 (未验证) 提交于 2019-12-03 00:56:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Android 4.1 includes offline voice typing. You can see this when you click the microphone on the pop-up keyboard, it still works if you have no network connection. However, it looks like the RecognizerIntent API still ends up requiring a network connection when you want to do speech recognition from your app. Is there an API for accessing new the offline voice typing feature in Android from my app? 回答1: Android uses two different APIs for voice: voice typing (key on the keyboard) and voice recognition (everywhere else). Voice

mypy differences in isinstance and issubclass from python 3.5 to 3.6 in parameterized generics

匿名 (未验证) 提交于 2019-12-03 00:48:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Before I upgraded to python 3.6 from python 3.5 this worked: import typing issubclass(list, typing.List[int]) # returns True isinstance([1, 2 ,3], typing.List[int]) # returns True now in python 3.6 both of these raise the following exception: TypeError: Parameterized generics cannot be used with class or instance checks Is this new intended behavior or a bug? If it is intended how can I perform the checks the code above is doing in python 3.6? 回答1: It is intentional, you shouldn't be mixing classes with types as defined in typing , at least,

Clean and type-safe state machine implementation in a statically typed language?

倖福魔咒の 提交于 2019-12-02 18:06:23
I implemented a simple state machine in Python: import time def a(): print "a()" return b def b(): print "b()" return c def c(): print "c()" return a if __name__ == "__main__": state = a while True: state = state() time.sleep(1) I wanted to port it to C, because it wasn't fast enough. But C doesn't let me make a function that returns a function of the same type. I tried making the function of this type: typedef *fn(fn)() , but it doesn't work, so I had to use a structure instead. Now the code is very ugly! #include <stdio.h> #include <stdlib.h> #include <unistd.h> typedef struct fn { struct fn

Get element type of list type [duplicate]

自闭症网瘾萝莉.ら 提交于 2019-12-02 06:44:01
问题 This question already has answers here : How to access the type arguments of typing.Generic? (5 answers) Closed last year . I have some list types (coming from inspect.signature -> inspect.Parameter ) and I'd like to get to know the type of their elements. My current solution works but is extremely ugly, see minimal example below: from typing import List, Type, TypeVar TypeT = TypeVar('TypeT') IntList = List[int] StrList = List[str] # todo: Solve without string representation and eval def

Get element type of list type [duplicate]

十年热恋 提交于 2019-12-02 06:42:23
This question already has an answer here: How to access the type arguments of typing.Generic? 5 answers I have some list types (coming from inspect.signature -> inspect.Parameter ) and I'd like to get to know the type of their elements. My current solution works but is extremely ugly, see minimal example below: from typing import List, Type, TypeVar TypeT = TypeVar('TypeT') IntList = List[int] StrList = List[str] # todo: Solve without string representation and eval def list_elem_type(list_type: Type[TypeT]) -> Type[TypeT]: assert str(list_type)[:11] == 'typing.List' return eval(str(list_type)

What is the correct type hint for an empty list?

十年热恋 提交于 2019-12-02 00:45:07
问题 What is the correct type hint for x = [] ? The type checker in my PyCharm editor flagged this as an error: labelframes: List[ttk.LabelFrame] = [] 'Optional' is not an option as in: labelframes: List[Optional[ttk.LabelFrame]] = [] because the documentation for typing.Optional states that this is equivalent to: labelframes: List[Union[ttk.LabelFrame, None]] = [] and [None] is not [] . I ought to mention that PyCharm doesn't like this either: labelframes: List[Union[ttk.LabelFrame, None]] =