python-3.5

How to specify “nullable” return type with type hints

为君一笑 提交于 2019-12-03 03:23:23
问题 Suppose I have a function: def get_some_date(some_argument: int=None) -> %datetime_or_None%: if some_argument is not None and some_argument == 1: return datetime.utcnow() else: return None How do I specify the return type for something that can be None ? 回答1: You're looking for Optional. Since your return type can either be datetime (as returned from datetime.utcnow() ) or None you should use Optional[datetime] : from typing import Optional def get_some_date(some_argument: int=None) ->

Python 3.5 async/await with real code example

丶灬走出姿态 提交于 2019-12-03 03:10:36
问题 I've read tons of articles and tutorial about Python's 3.5 async/await thing. I have to say I'm pretty confused, because some use get_event_loop() and run_until_complete(), some use ensure_future(), some use asyncio.wait(), and some use call_soon(). It seems like I have a lot choices, but I have no idea if they are completely identical or there are cases where you use loops and there are cases where you use wait(). But the thing is all examples work with asyncio.sleep() as simulation of real

How to calculate AUC with tensorflow?

こ雲淡風輕ζ 提交于 2019-12-03 03:09:21
I've built a binary classifier using Tensorflow and now I would like to evaluate the classifier using AUC and accuracy. As far as accuracy is concerned, I can easily do like this: X = tf.placeholder('float', [None, n_input]) y = tf.placeholder('float', [None, n_classes]) pred = mlp(X, weights, biases, dropout_keep_prob) correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) When calculating AUC I use the following: print(tf.argmax(pred, 1).dtype.name) print(tf.argmax(pred, 1).dtype.name) a = tf.cast(tf.argmax(pred, 1)

Convert .py to .exe [duplicate]

一个人想着一个人 提交于 2019-12-02 21:42:38
问题 This question already has answers here : How can I convert a .py to .exe for Python? (6 answers) Closed last month . How to convert python Python1.py created in Visual Studio 2015 to Python1.exe, with PyInstaller I gut error, so need to find some other tool to convert my PythonConsole.py to PythonConsole.exe 回答1: You don't have module named pefile install the module pip install pefile then try again 回答2: Import errors can be raised if that package is already installed. check whether that

How to specify “nullable” return type with type hints

允我心安 提交于 2019-12-02 18:56:06
Suppose I have a function: def get_some_date(some_argument: int=None) -> %datetime_or_None%: if some_argument is not None and some_argument == 1: return datetime.utcnow() else: return None How do I specify the return type for something that can be None ? You're looking for Optional . Since your return type can either be datetime (as returned from datetime.utcnow() ) or None you should use Optional[datetime] : from typing import Optional def get_some_date(some_argument: int=None) -> Optional[datetime]: # as defined From the documentation on typing, Optional is shorthand for: Optional[X] is

Python 3 TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

删除回忆录丶 提交于 2019-12-02 18:13:48
问题 #Import the module from math import sqrt #Using while loop statement to make the program not finish before the user close the program. while True: #Print out the introduction message, and get the input value to solve the quadratic equation. print("ax^2+bx+c=0의 꼴로 된 방정식을 풀 수 있습니다. a, b, c의 값을 차례대로 입력하세요.") a = input("a를 입력하세요 : ") b = input("b를 입력하세요 : ") c = input("c를 입력하세요 : ") #Define function that checks whether the input values are natural number or negative number def func_num(n): if n[0

Python 3.5 async/await with real code example

↘锁芯ラ 提交于 2019-12-02 16:40:34
I've read tons of articles and tutorial about Python's 3.5 async/await thing. I have to say I'm pretty confused, because some use get_event_loop() and run_until_complete(), some use ensure_future(), some use asyncio.wait(), and some use call_soon(). It seems like I have a lot choices, but I have no idea if they are completely identical or there are cases where you use loops and there are cases where you use wait(). But the thing is all examples work with asyncio.sleep() as simulation of real slow operation which returns an awaitable object. Once I try to swap this line for some real code the

Difference between coroutine and future/task in Python 3.5?

依然范特西╮ 提交于 2019-12-02 15:42:18
Let's say we have a dummy function: async def foo(arg): result = await some_remote_call(arg) return result.upper() What's the difference between: coros = [] for i in range(5): coros.append(foo(i)) loop = get_event_loop() loop.run_until_complete(wait(coros)) And: from asyncio import ensure_future futures = [] for i in range(5): futures.append(ensure_future(foo(i))) loop = get_event_loop() loop.run_until_complete(wait(futures)) Note : The example returns a result, but this isn't the focus of the question. When return value matters, use gather() instead of wait() . Regardless of return value, I'm

Delete duplicate tuples independent of order with same elements in generator Python 3.5

喜你入骨 提交于 2019-12-02 13:30:51
I have a generator of tuples and I need to delete tuples containing same elements. I need this output for iterating. Input = ((1, 1), (1, 2), (1, 3), (3, 1), (3, 2), (3, 3)) Output= ((1, 1), (1, 2), (1, 3)) Order of output doesn't matter. I have checked this question but it is about lists: Delete duplicate tuples with same elements in nested list Python I use generators to achieve fastest results as the data is very large. You can normalize the data by sorting it, then add it to a set to remove duplicates >>> Input = ((1, 1), (1, 2), (1, 3), (3, 1), (3, 2), (3, 3)) >>> Output = set(tuple

Replace entire line in a text file based on search using Python

两盒软妹~` 提交于 2019-12-02 13:15:54
I'm trying to replace a string which will be in the form of path='/users/username/folder' in a text file. I'm reading that text file and searching the line which starts from 'path ='. Here I've two problems, I'm unable to replace that line using following code If that string starts in between then this code may not work as I'm checking line.startswith(). Please help. f = open('/Volumes/Personal/example.text','r+') for line in f: print(line, end='') if (line.startswith("path = ")): # You need to include a newline if you're replacing the whole line line = CurrentFilePath + "\n" f.write(line)