python-3.5

Optimizing multiprocessing.Pool with expensive initialization

自古美人都是妖i 提交于 2019-12-04 09:19:12
Here is a complete simple working example import multiprocessing as mp import time import random class Foo: def __init__(self): # some expensive set up function in the real code self.x = 2 print('initializing') def run(self, y): time.sleep(random.random() / 10.) return self.x + y def f(y): foo = Foo() return foo.run(y) def main(): pool = mp.Pool(4) for result in pool.map(f, range(10)): print(result) pool.close() pool.join() if __name__ == '__main__': main() How can I modify it so Foo is only initialized once by each worker, not every task? Basically I want the init called 4 times, not 10. I am

difference between LinearRegression and svm.SVR(kernel=“linear”)

旧巷老猫 提交于 2019-12-04 08:18:16
First there are questions on this forum very similar to this one but trust me none matches so no duplicating please. I have encountered two methods of linear regression using scikit's sklearn and I am failing to understand the difference between the two, especially where in first code there's a method train_test_split() called while in the other one directly fit method is called. I am studying with multiple resources and this single issue is very confusing to me. First which uses SVR X = np.array(df.drop(['label'], 1)) X = preprocessing.scale(X) y = np.array(df['label']) X_train, X_test, y

Difference between coroutine and future/task in Python 3.5?

大城市里の小女人 提交于 2019-12-04 07:25:19
问题 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

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

ⅰ亾dé卋堺 提交于 2019-12-04 06:57:45
问题 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

How to definitely install virtualenvwrapper with Python3.5 on Mac OS Sierra?

三世轮回 提交于 2019-12-04 06:23:00
Well, looking around here there are tons of questions about how to correctly installing virtualenvwrapper on Mac. I really tried all of them but for no avail. I've noticed the previous questions don't have the configuration like my computer, so here it is: Mac OS Sierra 10.12 Python3.5.2 installed from python.org installer for Mac OS This installation puts Python3.5.2 in this path: /Library/Frameworks/Python.framework/Versions/3.5/bin/python3 I've run python3 get-pip.py command to install pip correctly, as I think sudo pip3 virtualenvwrapper to be sure all settings will be done as they should

PermissionError: pip upgrade from 8.1.1 to 8.1.2

99封情书 提交于 2019-12-04 06:14:30
问题 I am trying to upgrading pip from 8.1.1 to 8.1.2 . But it showing following 'PermissionError: [WinError 5] Access is denied : How to upgrade pip? C:\>python -m pip install --upgrade pip Collecting pip Using cached pip-8.1.2-py2.py3-none-any.whl Installing collected packages: pip Found existing installation: pip 8.1.1 Uninstalling pip-8.1.1: Exception: Traceback (most recent call last): File "C:\Program Files\Python35\lib\shutil.py", line 538, in move os.rename(src, real_dst) PermissionError:

Python code, not able to write into xls

做~自己de王妃 提交于 2019-12-04 05:41:36
问题 When the below code is executed in Python 3.5 and xlwt 1.2.0, following error is generated: "Cannot convert byte objects to str implicitly" The code works fine for python 2.7. Anyone please let me know what could be the problem. Thanks in Advance!! import xlwt import re import os wb = xlwt.Workbook() ws = wb.add_sheet('A Test Sheet') ws_1 = wb.add_sheet('A Test Sheet_B') cnt_row = 0 cnt_col_1 = 0 cnt_col_2 = 0 path = "E:\Python_Scripts" files = os.listdir("E:\Python_Scripts") for filename in

How to check if a file is already opened (in the same process)

寵の児 提交于 2019-12-04 05:01:45
And I'd like to specifically achieve that with the try catch construct. This related question suggests that I can do: try: open(fileName, 'wb+') except: print("File already opened!") raise However, it doesn't work me. I can open the same file multiple times without any problem: fileObj1 = open(fileName, 'wb+') fileObj2 = open(fileName, 'wb+') Is it because I have Python 3.5? Or because I'm using Raspbian ? Thanks for the help! atayenel You open the same file but assign them to different variables. What you should do is: fileobj=open(filename,"wb+") if not fileobj.closed: print("file is already

Python 3 UnicodeEncodeError: 'ascii' codec can't encode characters

一笑奈何 提交于 2019-12-04 04:51:24
I've just started to learn Python but I already ran into troubles. I have a simple script with just one command: #!/usr/bin/env python3 print("Příliš žluťoučký kůň úpěl ďábelské ódy.") # Text in Czech When I try to run this script: python3 hello.py I get this message: Traceback (most recent call last): File "hello.py", line 2, in <module> print("P\u0159\xedli\u0161 \u017elu\u0165ou\u010dk\xfd k\u016fn \xfap\u011bl \u010f\xe1belsk\xe9 \xf3dy.") UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-2: ordinal not in range(128) I am using Kubuntu 16.04 and Python 3.5.2. When I

python 3.5 in statsmodels ImportError: cannot import name '_representation'

霸气de小男生 提交于 2019-12-04 04:23:31
I cannot manage to import statsmodels.api correctly when i do that I have this error: File "/home/mlv/.local/lib/python3.5/site-packages/statsmodels/tsa/statespace/tools.py", line 59, in set_mode from . import (_representation, _kalman_filter, _kalman_smoother, ImportError: cannot import name '_representation' I already try to re-install or update it, that does not change. plese i need help =) Please see the github report for more detail. It turns out that statsmodels is dependent upon several packages being installed before it so that it can key on them to compile its own modules. I don't