eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: Python security updates are source only updates. There is no windows installer. For instance the page for python 3.6.12 states:  Security fix releases are produced periodically as needed and are source-only releases; binary installers are not provided.  Could someone explain how I can update/patch a Python installation done by the windows installer, so that latest Python security fixes are applied: eg., going from ython 3.6.6 to Python 3.6.12 Or if not possible how to install from Python source code directly.

Answer: To install security patches after the last full bugfix release, you must build Python from source: Compile the Binaries  Install Visual Studio 2019 Community and select:   the Python development workload, and  the Python native development tools (this is under Optional, but is necessary in order to build python from source)     Download the python source code and unzip it.  Navigate to the folder and install required external dependencies by running PCbuild\get_externals.bat  Build the debug (python_d.exe) and release (python.exe) binaries   > PCbuild\build.bat -p x64 -c Debug > PCbuild\build.bat -p x64 -c Release  you can also build the Profile Guided Optimization (pgo) binary > PCbuild\build.bat -p x64 --pgo  The binaries on python.org are run through PGO by default, so a --pgo binary will be faster than a -c Release binary. The debug binary is necessary for adding breakpoints and debugging your code. All built binaries are placed in PCbuild\amd64. Build the Installer The instructions for building an installer are in Tools\msi\README.txt.  Download the extra build dependencies by running Tools\msi\get_externals.bat   NOTE: This is done in addition to running PCbuild/get_externals.bat. It installs additional binaries to externals\windows-installer that are needed for making installers. Specifically,  WiX (wix.exe), which is a toolset that lets developers create installers for Windows Installer, the Windows installation engine. HTML Help (htmlhelp), which is for building documentation.    Turn on .NET Framework 3.5 Features under Turn Windows features on or off     NOTE: This is required by WiX.   Build the installer by running  > .\Tools\msi\buildrelease.bat -x64  NOTE: Be sure the following environment variables are properly set or left blank so the script can set them: PYTHON=<path to python.exe> SPHINXBUILD=<path to sphinx-build.exe>  The installer will be placed in PCbuild\amd64\en-us. It is a single .exe (the installer entry point). The folder will also have a number of additional CAB and MSI files. Each MSI contains the logic required to install a component or feature of Python, but these should not be run directly. Specify --pack to build an installer that does not require all MSIs to be available alongside. This takes longer, but is easier to share.
Question: When we call an endpoint and a redirect occurs due to a missing trailing slash. As you can see in the image below, when a request is made to https://.../notifications, the FastAPI server responds with a redirect to http://.../notifications/ I suspect that it's an app configuration issue rather than a server configuration issue. Does anyone have an idea of how to resolve this issue? 

Answer: This is because your application isn't trusting the reverse proxy's headers overriding the scheme (the X-Forwarded-Proto header that's passed when it handles a TLS request). There's a few ways we can fix that:  If you're running the application straight from uvicorn server, try using the flag --forwarded-allow-ips '*'.  If you're running gunicorn you can set as well the flag --forwarded-allow-ips="*".  In either application, you can additionally use the FORWARDED_ALLOW_IPS environment variable.   Important: the * should be used only as a test, as it'll lead your application to trust the X-Forwarded-* headers from any source. I suggest you read uvicorn's docs and gunicorn's docs for a deeper knowledge of what to set in this flag and why.
Question: So I am trying to plot curved lines to join points, here is the code I am using:- def hanging_line(point1, point2): a = (point2[1] - point1[1])/(np.cosh(point2[0]) - np.cosh(point1[0])) b = point1[1] - a*np.cosh(point1[0]) x = np.linspace(point1[0], point2[0], 100) y = a*np.cosh(x) + b return (x,y) n_teams = 4 n_weeks = 4 fig, ax = plt.subplots(figsize=(6,6)) t = np.array([ [1, 2, 4, 3], [4, 3, 3, 2], [3, 4, 1, 4], [2, 1, 2, 1] ]) fig.patch.set_facecolor('#1b1b1b') for nw in range(n_weeks): ax.scatter([nw] * n_weeks, t[:, nw], marker='o', color='#4F535C', s=100, zorder=2) ax.axis('off') for team in t: x1, x2 = 0, 1 for rank in range(0, len(team) - 1): y1 = n_weeks - team[rank] + 1 y2 = n_weeks - team[rank + 1] + 1 x, y = hanging_line([x1, y1], [x2, y2]) ax.plot(x, y, color='#4F535C', zorder=1) x1 += 1 x2 += 1  The code is producing the following output:-  But I want the curved lines to look somewhat like this:  What changes should I have to do in my code to get the required result?

Answer: Here is an approach using bezier curves. The sequence [...., i-indent, i, i + 0.8, ...] will put control points at each integer position i and some space before and after. The plot below used indent=0.8; indent=0 would create straight lines; with indent>1 the curves would be intersecting more. Other variations will make the curves more or less "cornered". import matplotlib.pyplot as plt from matplotlib.path import Path import matplotlib.patches as patches import numpy as np n_teams = 4 n_weeks = 4 t = np.array([[1, 2, 4, 3], [4, 3, 3, 2], [3, 4, 1, 4], [2, 1, 2, 1]]) fig, ax = plt.subplots(figsize=(10, 4), facecolor='#1b1b1b') ax.set_facecolor('#1b1b1b') indent = 0.8 for tj in t: ax.scatter(np.arange(len(tj)), tj, marker='o', color='#4F535C', s=100, zorder=3) # create bezier curves verts = [(i + d, tij) for i, tij in enumerate(tj) for d in (-indent, 0, indent)][1:-1] codes = [Path.MOVETO] + [Path.CURVE4] * (len(verts) - 1) path = Path(verts, codes) patch = patches.PathPatch(path, facecolor='none', lw=2, edgecolor='#4F535C') ax.add_patch(patch) ax.set_xticks([]) ax.set_yticks([]) ax.autoscale() # sets the xlim and ylim for the added patches plt.show()   A colored version could look like: colors = ['crimson', 'skyblue', 'lime', 'gold'] for tj, color in zip(t, colors): ax.scatter(np.arange(len(tj)), tj, marker='o', color=color, s=100, zorder=3) verts = [(i + d, tij) for i, tij in enumerate(tj) for d in (-indent, 0, indent)][1:-1] codes = [Path.MOVETO] + [Path.CURVE4] * (len(verts) - 1) path = Path(verts, codes) patch = patches.PathPatch(path, facecolor='none', lw=2, edgecolor=color) ax.add_patch(patch)   The following plot compares different values for indent:
Question: Say I have model class UserDB(BaseModel): first_name: Optional[str] = None last_name: Optional[str] = None  How do I make another model that is constructed from this one and has a field that changes based on the fields in this model? For instance, something like this class User(BaseModel): full_name: str = first_name + ' ' + last_name  Constructed like this maybe User.parse_obj(UserDB)  Thanks!

Answer: If you do not want to keep first_name and last_name in User then you can  customize __init__. use validator for setting full_name.  Both methods do what you want: from typing import Optional from pydantic import BaseModel, validator class UserDB(BaseModel): first_name: Optional[str] = None last_name: Optional[str] = None class User_1(BaseModel): location: str # for a change full_name: Optional[str] = None def __init__(self, user_db: UserDB, **data): super().__init__(full_name=f"{user_db.first_name} {user_db.last_name}", **data) user_db = UserDB(first_name="John", last_name="Stark") user = User_1(user_db, location="Mars") print(user) class User_2(BaseModel): first_name: Optional[str] = None last_name: Optional[str] = None full_name: Optional[str] = None @validator('full_name', always=True) def ab(cls, v, values) -> str: return f"{values['first_name']} {values['last_name']}" user = User_2(**user_db.dict()) print(user)  output location='Mars' full_name='John Stark' first_name='John' last_name='Stark' full_name='John Stark'  UPDATE: For working with response_model you can customize __init__ in such way: class User_1(BaseModel): location: str # for a change full_name: Optional[str] = None # def __init__(self, user_db: UserDB, **data): def __init__(self, first_name, last_name, **data): super().__init__(full_name=f"{first_name} {last_name}", **data) user_db = UserDB(first_name="John", last_name="Stark") user = User_1(**user_db.dict(), location="Mars") print(user)
Question: I have a trading bot that trades multiple pairs (30-40). It uses the previous 5m candle for the price input. Therefore, I get 5m history for ALL pairs one by one. Currently, the full cycle takes about 10 minutes, so the 5m candles get updated once in 10m, which is no good. Any ideas on how to speed things up?

Answer: I think the best option for you will be websocket connection. You cannot recieve kline data once per eg. 5 minutes, but you can recieve every change in candle like you see it in graph. Binance API provide only this, but in compound with websocket connection it will by realy fast, not 10 minutes. After recieve data you only must to specify when candle was closed, you can do it from timestamps that are in json data ('t' and 'T'). [documentation here] You must install websockets library. pip install websockets  And here is some sample code how it can work. import asyncio import websockets async def candle_stick_data(): url = "wss://stream.binance.com:9443/ws/" #steam address first_pair = 'bnbbtc@kline_1m' #first pair async with websockets.connect(url+first_pair) as sock: pairs = '{"method": "SUBSCRIBE", "params": ["xrpbtc@kline_1m","ethbtc@kline_1m" ], "id": 1}' #other pairs await sock.send(pairs) print(f"> {pairs}") while True: resp = await sock.recv() print(f"< {resp}") asyncio.get_event_loop().run_until_complete(candle_stick_data())  Output: < {"e":"kline","E":1599828802835,"s":"XRPBTC","k":{"t":1599828780000,"T":1599828839999,"s":"XRPBTC","i":"1m","f":76140140,"L":76140145,"o":"0.00002346","c":"0.00002346","h":"0.00002346","l":"0.00002345","v":"700.00000000","n":6,"x":false,"q":"0.01641578","V":"78.00000000","Q":"0.00182988","B":"0"}} < {"e":"kline","E":1599828804297,"s":"BNBBTC","k":{"t":1599828780000,"T":1599828839999,"s":"BNBBTC","i":"1m","f":87599856,"L":87599935,"o":"0.00229400","c":"0.00229610","h":"0.00229710","l":"0.00229400","v":"417.88000000","n":80,"x":false,"q":"0.95933156","V":"406.63000000","Q":"0.93351653","B":"0"}} < {"e":"kline","E":1599828804853,"s":"ETHBTC","k":{"t":1599828780000,"T":1599828839999,"s":"ETHBTC","i":"1m","f":193235180,"L":193235214,"o":"0.03551300","c":"0.03551700","h":"0.03551800","l":"0.03551300","v":"21.52300000","n":35,"x":false,"q":"0.76437246","V":"11.53400000","Q":"0.40962829","B":"0"}} < {"e":"kline","E":1599828806303,"s":"BNBBTC","k":{"t":1599828780000,"T":1599828839999,"s":"BNBBTC","i":"1m","f":87599856,"L":87599938,"o":"0.00229400","c":"0.00229620","h":"0.00229710","l":"0.00229400","v":"420.34000000","n":83,"x":false,"q":"0.96497998","V":"406.63000000","Q":"0.93351653","B":"0"}}
Question: I am trying to save the base64 encoded image in the django rest framework. First of all, we make a code to insert the base64 encoded image into the imagefield and test it, and the following error appears.  binascii.Error: Incorrect padding  What I don't understand is that I've used the same code before and there was no such error. Can you help me? Here is my code. serializers.py from rest_framework import serializers from .models import post, comment class Base64ImageField (serializers.ImageField) : def to_internal_value (self, data) : from django.core.files.base import ContentFile import base64 import six import uuid if isinstance(data, six.string_types): if 'data:' in data and ';base64,' in data : header, data = data.split(';base64,') try : decoded_file = base64.b64decode(data) except TypeError : self.fail('invalid_image') file_name = str(uuid.uuid4())[:12] file_extension = self.get_file_extension(file_name, decoded_file) complete_file_name = "%s.%s" % (file_name, file_extension, ) data = ContentFile(decoded_file, name=complete_file_name) return super(Base64ImageField, self).to_internal_value(data) def get_file_extension (self, file_name, decoded_file) : import imghdr extension = imghdr.what(file_name, decoded_file) extension = "jpg" if extension == "jpeg" else extension return extension class commentSerializer (serializers.ModelSerializer) : class Meta : model = comment fields = '__all__' class postSerializer (serializers.ModelSerializer) : author = serializers.CharField(source='author.username', read_only=True) image1 = Base64ImageField(use_url=True) image2 = Base64ImageField(use_url=True) image3 = Base64ImageField(use_url=True) image4 = Base64ImageField(use_url=True) image5 = Base64ImageField(use_url=True) comment = commentSerializer(many=True, read_only=True) class Meta: model = post fields = ['pk', 'author', 'title', 'text', 'image1', 'image2', 'image3', 'image4', 'image5', 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'comment'] 

Answer: I'm not sure this applies to your situation, depending on where you're storing your encoded data. I had the same error, but it related to some encoded session data. I cleared out the session data (cookies, cache etc) in the browser Devtools, and it fixed my issue. Just posting this in case it applies or helps others who come along for the same reason.
Question: I am trying to get started with Tensorflow 2.0 Object Detection API. I have gone through the installation following the official tutorial and I pass all the tests. However, I keep getting an error message that I don't understand when I try to run the main module. This is how I run it: python model_main_tf2.py --model_dir=ssd_resnet50_v1_fpn_640x640_coco17_tpu-8 --pipeline_config_path=ssd_resnet50_v1_fpn_640x640_coco17_tpu-8/pipeline.config  This is the beginning of the error message: Traceback (most recent call last): File "model_main_tf2.py", line 113, in <module> tf.compat.v1.app.run() File "/home/hd/hd_hd/hd_rs239/.conda/envs/jan_tf2/lib/python3.7/site-packages/tensorflow/python/platform/app.py", line 40, in run _run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef) File "/home/hd/hd_hd/hd_rs239/.conda/envs/jan_tf2/lib/python3.7/site-packages/absl/app.py", line 299, in run _run_main(main, args) File "/home/hd/hd_hd/hd_rs239/.conda/envs/jan_tf2/lib/python3.7/site-packages/absl/app.py", line 250, in _run_main sys.exit(main(argv)) File "model_main_tf2.py", line 110, in main record_summaries=FLAGS.record_summaries) File "/home/hd/hd_hd/hd_rs239/.conda/envs/jan_tf2/lib/python3.7/site-packages/object_detection/model_lib_v2.py", line 569, in train_loop unpad_groundtruth_tensors) File "/home/hd/hd_hd/hd_rs239/.conda/envs/jan_tf2/lib/python3.7/site-packages/object_detection/model_lib_v2.py", line 383, in load_fine_tune_checkpoint ckpt.restore(checkpoint_path).assert_existing_objects_matched() File "/home/hd/hd_hd/hd_rs239/.conda/envs/jan_tf2/lib/python3.7/site-packages/tensorflow/python/training/tracking/util.py", line 791, in assert_existing_objects_matched (list(unused_python_objects),)) AssertionError: Some Python objects were not bound to checkpointed values, likely due to changes in the Python program: [SyncOnReadVariable:{ 0: <tf.Variable 'conv2_block1_0_bn/moving_variance:0' shape=(256,) dtype=float32, numpy= array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,  In the pipeline.config, I specify a checkpoint like this:  fine_tune_checkpoint: "ssd_resnet50_v1_fpn_640x640_coco17_tpu-8/checkpoint/ckpt-0"  These are the contents of ssd_resnet50_v1_fpn_640x640_coco17_tpu-8/checkpoint/ : checkpoint ckpt-0.data-00000-of-00001 ckpt-0.index  I have searched Google but couldn't find any answer. In this issue, the suggested solution is outdated (the code they suggest to replace is not there anymore). Question: What is the problem and how can I solve it? I am doing this on a server with CentOS Linux 7. I am using Python 3.7. I am new to Tensorflow so please if I am missing any important information, let me know.

Answer: From the file name you provided (ssd_resnet50_v1_fpn_640x640_coco17_tpu-8), I can see you are trying to work with an object detection task. Therefore, in your pipeline.config file change this line: fine_tune_checkpoint_type: "classification"  To: fine_tune_checkpoint_type: "detection"  This should solve your problem.
Question: So recently my PyCharm is missing its run tool window that usually show the run/debug results. it is now replaced with python console and services, which is really frustrating because It's just showing gibberish and command-prompt-like format. How do I return the run tool window back as my main run/debug window? I have circled the tabs/windows that I meant in this pic with red circle. Note: usually I can access this run tool window by pressing alt + 4. Please see red circle:  This is my run config:  This is my view tab bar, it doesn't show run (alt+4): 

Answer: From what I understand you want the run icon pinned to your lower toolbar. (This corresponds to running whatever your last chosen configuration was.) Two easy steps: 1º View -> Tool Windows -> Run  2º Right-click run icon on lower tool bar -> View Mode -> Dock Pinned  Edit after OP feedback: If your Run (Alt+4) option has disappeared completely, besides trying a PyCharm reinstall it's advisable to manually clean the preference files that might be hidden. Check the following paths C:\Users\user_name\AppData\Local\JetBrains and C:\Users\user_name\AppData\Roaming\JetBrains, C:\Users\user_name\.PyCharmCE2020.x, and C:\Path_to_your_Project\.idea. Some of there directories might be hidden so you'll have to check you've set them to be visible. Even by reinstalling PyCharm some of the above configurations are likely to be kept. There's a strong possibility the state of whatever changes that caused Run to disappear is kept in files inside the above mentioned directories.
Question: I'm adding a list of objects entries to a database. Sometimes it may happen that one of this objects is already in the database (I do not have any control on that). With only one IntegrityError all the transactions will fail, i.e. all the objects in entries will not be inserted into the database. try: session.add_all(entries) session.commit() except: logger.error(f"Error! Rolling back") session.rollback() raise finally: session.close()  My desired behavior would be: if there is a IntegrityError in one of the entries, catch it and do not add that object to the database, otherwise continue normally (do not fail) Edit: I'm usign MySQL as backend.

Answer: I depends on what backend you're using. PostgreSQL has a wonderful INSERT() ON CONFLICT DO NOTHING clause which you can use with SQLAlchemy: from sqlalchemy.dialects.postgresql import insert session.execute(insert(MyTable) .values(my_entries) .on_conflict_do_nothing())  MySQL has the similar INSERT IGNORE clause, but SQLAlchemy has less support for it. Luckily, according to this answer, there is a workaround, using prefix_with: session.execute(MyTable.__table__ .insert() .prefix_with('IGNORE') .values(my_entries))  The only thing is that my_entries needs to be a list of column to value mappings. That means [{ 'id': 1, 'name': 'Ringo' }, { 'id': 2, 'name': 'Paul' }, ...] et cetera.
Question: For JavaScript formatter works fine but not for Python. I have installed autopep8 but it seems that I can't set max line length. I tried this: "python.formatting.autopep8Args": [ "--max-line-length", "79", "--experimental" ]  and my settings.json looks like this: { "workbench.colorTheme": "One Dark Pro", "git.autofetch": true, "workbench.iconTheme": "material-icon-theme", "git.enableSmartCommit": true, "terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\cmd.exe", "[javascript]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, "[html]": { "editor.defaultFormatter": "vscode.html-language-features" }, "javascript.updateImportsOnFileMove.enabled": "always", "[javascriptreact]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, "liveServer.settings.donotShowInfoMsg": true, "editor.formatOnSave": true, "window.zoomLevel": 1, "vscode-w3cvalidation.validator-token": "Fri, 07 Aug 2020 07:35:05 GMT", "python.formatting.provider": "autopep8", "python.formatting.autopep8Args": [ "--max-line-length", "79", "--experimental" ], "python.autoComplete.addBrackets": true, "python.autoComplete.extraPaths": [] }  Any ideas how to fix that?

Answer: From autopep8-usage, the default value of max-line-length is 79, so you can change to other value and have a try. About the effect of autopep8 in vscode, I made a test with the same settings as yours, like the following screenshot shows:  every print sentence line-length is over 79, the first and the second print() parameters are expressions, and the setting works for the first one, not for the second. This is because setting applicable rules are provided by python extension and it has own caculation mechanism. When it comes to print strings, the setting doesn't work, so if you mean this in your question, you can add the following code in user settings.json. "editor.wordWrap": "wordWrapColumn", "editor.wordWrapColumn": 79
Question: I have a df like this: col1 col2 [1,3,4,5] [3,3,6,2] [1,4,5,5] [3,8,4,3] [1,3,4,8] [8,3,7,2]  Trying to divide the elements in the lists in col1 and col2 together to get what's in the result column: col1 col2 result [1,3,4,5] [3,3,6,2] [.33,1,.66,2.5] [1,4,5,5] [3,8,4,3] [.33,.5,1.25,1.66] [1,3,4,8] [8,3,7,2] [.33,1,.57,4]  Tried a lot of different approaches - but always get an error. Attempts: #attempt1 df['col1'].div(df['col2'], axis=0) #attempt2 from operator import truediv for i in df.col1: a = np.array(df['col1']) for t in df.col2: b = np.array(df['col2']) x = a/b print(x) #attempt3 for i in df.index: a = col1 b = col2 x = map(truediv, a, b) #attempt4 a = col1 b = col2 result = [x/y for x, y in zip(a, b)] #then apply to df #attempt5 a = col1 b = col2 result = a/b print(percent_matched) #then #apply to df >>>TypeError: unsupported operand type(s) for /: 'list' and 'list'  Any ideas?

Answer: You can use list comprehension with apply, this is conditional on both the lists being of same length df['result'] = df.apply(lambda x: [np.round(x['col1'][i]/x['col2'][i], 2) for i in range(len(x['col1']))], axis = 1) col1 col2 result 0 [1, 3, 4, 5] [3, 3, 6, 2] [0.33, 1.0, 0.67, 2.5] 1 [1, 4, 5, 5] [3, 8, 4, 3] [0.33, 0.5, 1.25, 1.67] 2 [1, 3, 4, 8] [8, 3, 7, 2] [0.12, 1.0, 0.57, 4.0]  Edit: As @TrentonMcKinney suggested, this can be done without using LC. This solution capitalized on Numpy's vectorized operations, df.apply(lambda x: np.round(np.array(x[0]) / np.array(x[1]), 3), axis=1)
Question: I have the following code import plotly.graph_objs as go layout1= go.Layout(title=go.layout.Title(text="A graph",x=0.5), xaxis={'title':'x[m]'}, yaxis={'title':'y[m]','range':[-10,10]}) point_plot=[ go.Scatter(x=[3,4],y=[1,2],name="V0"), go.Scatter(x=[1,2],y=[1,1],name="V0"), go.Scatter(x=[5,6],y=[2,3],name="GT") ] go.Figure(data=point_plot, layout=layout1).show()  which produces the following plot  However this is not what I want exactly. What I want is that the two sets marked with "V0" must be of the same color and have only one mark in the legend. (In fact I am going to plot much more than two sets, like 20 sets of pairs joined by a line and they all have to be the same color and have only one mark in the legend)

Answer: You can combine two V0 segments in a single scatter and add an extra point with np.nan to split two segments value as follows: import plotly.graph_objs as go import numpy as np layout1= go.Layout(title=go.layout.Title(text="A graph",x=0.5), xaxis={'title':'x[m]'}, yaxis={'title':'y[m]','range':[-10,10]}) point_plot=[ go.Scatter(x=[1,2,3,3,4],y=[1,1,np.nan, 1,2],name="V0"), go.Scatter(x=[5,6],y=[2,3],name="GT") ] go.Figure(data=point_plot, layout=layout1).show()