eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I created a GUI app using pyqt5 and opencv. The app works fine without activating the virtual env but when I activate the virtual env and run the app it shows this error: QObject::moveToThread: Current thread (0x125b2f0) is not the object's thread (0x189e780). Cannot move to target thread (0x125b2f0) qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/deepak/Desktop/SampleApp/lib/python3.9/site-packages/cv2/qt/plugins" even though it was found. This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem. Available platform plugins are: xcb, eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, webgl. Aborted  I tried running an example pyqt5 code (without importing opencv) and another code (only using opencv) both worked fine in the virtual env. Operating System: Parrot OS 4.11 Python Version: 3.9.2

Answer: The problem is that the version of Qt with which opencv was compiled is not similar to the one used by PyQt5 causing a conflict. A possible solution is to indicate to use the Qt plugins used by PyQt5. import os from pathlib import Path import PyQt5 from PyQt5.QtWidgets import QWidget # others imports import cv2 os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] = os.fspath( Path(PyQt5.__file__).resolve().parent / "Qt5" / "plugins" ) # ...  For PySide2: import os from pathlib import Path import PySide2 from PySide2.QtWidgets import QWidget # others imports import cv2 os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] = os.fspath( Path(PySide2.__file__).resolve().parent / "Qt" / "plugins" ) # ...  Update: A better option is to use QLibraryInfo to get the plugins folder path: import os from PyQt5.QtCore import QLibraryInfo # from PySide2.QtCore import QLibraryInfo import cv2 os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] = QLibraryInfo.location( QLibraryInfo.PluginsPath )
Question: I love Pylance type checking. However, If I have a variable var: Union[None, T], where T implements foo, pylance will throw an error at: var.foo() since type None doesn't implement foo. Is there any way to resolve this? A way to tell Pylance "This variable is None sometimes but in this case I'm 100% sure it will be assigned

Answer: There are many ways of forcing a type-checker to accept this.    Use assert: from typing import Union def do_something(var: Union[T, None]): assert var is not None var.foo()    Raise some other exception: from typing import Union def do_something(var: Union[T, None]): if var is None: raise RuntimeError("NO") var.foo()    Use an if statement: from typing import Union def do_something(var: Union[T, None]): if var is not None: var.foo()    Use typing.cast, a function that does nothing at runtime but forces a type-checker to accept that a variable is of a certain type: from typing import Union, cast def do_something(var: Union[T, None]): var = cast(T, var) var.foo()    Switch off the type-checker for that line: from typing import Union def do_something(var: Union[T, None]): var.foo() # type: ignore      Note also that, while it makes no difference to how your type annotation is interpreted by a type-checker (the two are semantically identical), you can also write typing.Union[T, None] as typing.Optional[T], which is arguably slightly nicer syntax. In Python >=3.10 (or earlier if you have from __future__ import annotations at the top of your code), you can even write Union types with the | operator, i.e. T | None.
Question: I'm trying to automate the download of a PDF file using Playwright, I've the code working with Selenium, but some features in Playwright got my attention. The real problem the documentation isn't helpful. When I click on download I get this:  And I cant change the directory of the download, it also delete the "file" when the browser/context are closed. Using Playwright I can achieve a nice download automation? Code: def run(playwright): browser = playwright.chromium.launch(headless=False) context = browser.new_context(accept_downloads=True) # Open new page page = context.new_page() # Go to http://xcal1.vodafone.co.uk/ page.goto("http://xcal1.vodafone.co.uk/") # Click text=Extra Small File 5 MB A high quality 5 minute MP3 music file 30secs @ 2 Mbps 10s >> img with page.expect_download() as download_info: page.click("text=Extra Small File 5 MB A high quality 5 minute MP3 music file 30secs @ 2 Mbps 10s >> img") download = download_info.value path = download.path() download.save_as(path) print(path) # --------------------- context.close() browser.close() with sync_playwright() as playwright: run(playwright) 

Answer: The download.path() in playwright is just a random GUID (globally unique identifier). It's designed to validate the download works - not to keep the file. Playwright is a testing tool and imagine running tests across every major browser on every code change - any downloads would quickly take up a lot of space and it would hack people off if you need to manually clear them out. Good news is you are very close - If you want to keep the file you just need to give the file a name in the save_as. instead of this:  download.save_as(path)  use this:  download.save_as(download.suggested_filename)  That saves the file in the same location as the script.
Question: Python changed its pickle protocol to 4 in python 3.4 to 3.7 and again changed it to protocol=5 in python 3.8. How do I open older pickled files in python 3.8? I tried: >>> with open('data_frame_111.pkl','rb') as pfile: ... x1 = pickle.load(pfile) ... Traceback (most recent call last): File "<stdin>", line 2, in <module> AttributeError: Can't get attribute 'new_block' on <module 'pandas.core.internals.blocks' from '/opt/anaconda3/lib/python3.8/site- packages/pandas/core/internals/blocks.py'>  and >>> with open('data_frame_111.pkl','rb') as pfile: ... x1 = unpkl.load(pfile, protocol=4)  but whereas protocol is a keyword in pickle.dump it is not part of pickle.load. Instantiating pickle.Unpickler() also doesn't work. But obviously there should be a way. In python 3.7, I would import pickle5 and use that to open newer pickles, but can't find documentation on doing the reverse in python 3.8.

Answer: You need to upgrade to the latest version (1.3.1 worked for me) of pandas. Or, to be more precise, the pandas version when you did pickle.dump(some_path) should be the same pandas version as when you will do pickle.load(some_path).
Question: I am using JAX, and I want to perform an operation like @jax.jit def fun(x, index): x[:index] = other_fun(x[:index]) return x  This cannot be performed under jit. Is there a way of doing this with jax.ops or jax.lax? I thought of using jax.ops.index_update(x, idx, y) but I cannot find a way of computing y without incurring in the same problem again.

Answer: The previous answer by @rvinas using dynamic_slice works well if your index is static, but you can also accomplish this with a dynamic index using jnp.where. For example: import jax import jax.numpy as jnp def other_fun(x): return x + 1 @jax.jit def fun(x, index): mask = jnp.arange(x.shape[0]) < index return jnp.where(mask, other_fun(x), x) x = jnp.arange(5) print(fun(x, 3)) # [1 2 3 3 4]
Question: I have been involved in the chainlink bootcamp and trying to finishing the final 'Exercise 3: Putting it all together'. However, I am stuck running: brownie run scripts/price_exercise_scripts/01_deploy_price_exercise.py --network kovan  ContractNotFound: No contract deployed at 0xF4030086511a5bEEa4966F8cA5B36dbC97BeE88c Printed contract_type._name is a mock address returned from `MockV3Aggregator which also doesn't make sense, why the code calls this logic. def get_contract(contract_name): contract_type = contract_to_mock[contract_name] if network.show_active() in NON_FORKED_LOCAL_BLOCKCHAIN_ENVIRONMENTS: if len(contract_type) <= 0: deploy_mocks() contract = contract_type[-1] else: try: contract_address = config["networks"][network.show_active()][contract_name] contract = Contract.from_abi( contract_type._name, contract_address, contract_type.abi ) except KeyError: print( f"{network.show_active()} address not found, perhaps you should add it to the config or deploy mocks?") print( f"brownie run scripts/deploy_mocks.py --network {network.show_active()}" ) return contract  I am struggling to understand this error message, should this command not be deploying contracts? I.e they should already already exist on the kovan network? Any insights welcome!

Answer: Problem - I was using the ethereum mainnet address instead of the correct kovan network address for btc / usd price. Changing the btc_usd_price_feed value to 0x6135b13325bfC4B00278B4abC5e20bbce2D6580e in the config.yml fixed this issue for me. price feed addresses
Question: I have a Plotly figure built in Python that updates automatically. I want to preserve dashboard zooms even with automatic updates. The documentation in Plotly says this can be done using the layout uirevision field, per the this community writeup. The docs give this as an example of the return dictionary:  return { 'data': data, 'layout': { # `uirevsion` is where the magic happens # this key is tracked internally by `dcc.Graph`, # when it changes from one update to the next, # it resets all of the user-driven interactions # (like zooming, panning, clicking on legend items). # if it remains the same, then that user-driven UI state # doesn't change. # it can be equal to anything, the important thing is # to make sure that it changes when you want to reset the user # state. # # in this example, we *only* want to reset the user UI state # when the user has changed their dataset. That is: # - if they toggle on or off reference, don't reset the UI state # - if they change the color, then don't reset the UI state # so, `uirevsion` needs to change when the `dataset` changes: # this is easy to program, we'll just set `uirevision` to be the # `dataset` value itself. # # if we wanted the `uirevision` to change when we add the "reference" # line, then we could set this to be `'{}{}'.format(dataset, reference)` 'uirevision': dataset, 'legend': {'x': 0, 'y': 1} } }  However, my figure is built more like this: import plotly.express as px @app.callback( Output("graph", "figure"), [Input("interval-component", "n_intervals")]) def display_graph(n_intervals): # Logic for obtaining data/processing is not shown my_figure = px.line(my_data_frame, x=my_data_frame.index, y=['line_1', 'line_2'], title='Some Title', template='plotly_dark') return my_figure  In other words, since I am not returning a dictionary, but a plotly express figure directly, how can I directly access the uirevision value so that UI changes from the user are preserved?

Answer: Use the figure dictionary, which can be accessed like so: my_figure['layout']['uirevision'] = 'some_value'  This can also be used to access other useful aspects of the figure, such as changing the line color of a specific line entry: my_figure['data'][2]['line']['color'] = '#FFFF00'  To see the other entry options, print out my_figure in a Python session. Note: since the uirevision option isn't documented very well (at least, not in my searching online), I thought it worth posting this as an option.
Question: I am fetching data from an API in order to show sales and finance reports, but I receive a type gzip file which I managed to convert into a Uint8Array. I'd like to somehow parse-decode this into a JSON file that I can use to access data and create charts in my frontend with. I was trying with different libraries (pako and cborg seemed to be the ones with the closest use cases), but I ultimately get an error Error: CBOR decode error: unexpected character at position 0 This is the code as I have it so far: let req = https.request(options, function (res) { console.log("Header: " + JSON.stringify(res.headers)); res.setEncoding("utf8"); res.on("data", function (body) { const deflatedBody = pako.deflate(body); console.log("DEFLATED DATA -----> ", typeof deflatedBody, deflatedBody); console.log(decode(deflatedBody)); }); res.on("error", function (error) { console.log("connection could not be made " + error.message); }); }); req.end(); };  I hope anyone has stumbled upon this already and has some idea. Thanks a lot!

Answer: Please visit this answer https://stackoverflow.com/a/12776856/16315663 to retrieve GZIP data from the response. Assuming, You have already retrieved full data as UInt8Array. You just need the UInt8Array as String const jsonString = Buffer.from(dataAsU8Array).toString('utf8') const parsedData = JSON.parse(jsonString) console.log(parsedData)  Edit Here is what worked for me const {request} = require("https") const zlib = require("zlib") const parseGzip = (gzipBuffer) => new Promise((resolve, reject) =>{ zlib.gunzip(gzipBuffer, (err, buffer) => { if (err) { reject(err) return } resolve(buffer) }) }) const fetchJson = (url) => new Promise((resolve, reject) => { const r = request(url) r.on("response", (response) => { if (response.statusCode !== 200) { reject(new Error(`${response.statusCode} ${response.statusMessage}`)) return } const responseBufferChunks = [] response.on("data", (data) => { console.log(data.length); responseBufferChunks.push(data) }) response.on("end", async () => { const responseBuffer = Buffer.concat(responseBufferChunks) const unzippedBuffer = await parseGzip(responseBuffer) resolve(JSON.parse(unzippedBuffer.toString())) }) }) r.end() }) fetchJson("https://wiki.mozilla.org/images/f/ff/Example.json.gz") .then((result) => { console.log(result) }) .catch((e) => { console.log(e) })
Question: I am exploring python is vs ==, when I was exploring it, I find out if I write following; >>> a = 10.24 >>> b = 10.24  in a python shell and on typing >> a is b, it gives me output as false. But when I write the following code in a python editor and run it I get true. a = 10.24 b = 10.24 print(a is b)  Can anyone explain why I am getting two different results of the same variables and expression?

Answer: You should not rely on is for comparison of values when you want to test equality. The is keyword compares id's of the variables, and checks if they are the same object. This will only work for the range of integers [-5,256] in Python, as these are singletons (these values are cached and referred to, instead of having a value stored in memory). See What's with the integer cache maintained by the interpreter? This is not the same as checking if they are the same value. As for why it behaves differently in a REPL environment versus a passed script, see Different behavior in python script and python idle?. The jist of it is that a passed script parses the entire file first, while a REPL environment like ipython or an IDLE shell reads lines one at a time. a=10.24 and b=10.24 are executed in different contexts, so the shell doesn't know that they should be the same value.
Question: I am trying to read a particular portion of a document as a table. It is structured as a table but there are no dividing lines between, cells, rows or columns. I had success with using the read_pdf() method with the area and column arguments. I could specify exactly where the table starts and ends and where the columns divide. But my pdf has multiple different sizes of tables on each page with no clear markers to identify them and I have to use these arguments. I found out about the read_pdf_with_template() method in the Github repo issues here, and a bit more about it in the documentation, pull request and the example notebook. But nowhere it is mentioned how to structure the teamplate.json and which arguments I could use or what they mean. I tried inserting the area coordinates to the x1, y1, x2, y2, passing the columns list in the method argument and height, width arguments with the size of the table. But it is picking up a top centre section of the pdf which does not equate to any of the coordinates I inserted when I reverse calculated everything. Here's the page I'm trying to read (I've deleted some sensitive data)  and here are the code snippets import tabula tables = tabula.read_pdf_with_template(input_path = "test.pdf", template_path = "template.json", columns=[195, 310, 380]) print(tables[0])  [ { "page": 1, "extraction_method": "stream", "x1": 225, "x2": 35, "y1": 375, "y2": 565, "width": 525, "height": 400 } ] 

Answer: I was just being a dum-dum. Templates are not something that you generate manually. They are supposed to be generated by the tabula app as mentioned here. Just download tabula from the official website. Once you launch the app, it's fairly simple. Manually click and drag on each table on each page and click on the download template button on top.
Question: belos is my code to ensure that the folder has images, but tf.keras.preprocessing.image_dataset_from_directory returns no images found. What did I do wrong? Thanks. DATASET_PATH = pathlib.Path('C:\\Users\\xxx\\Documents\\images') image_count = len(list(DATASET_PATH.glob('.\\*.jpg'))) print(image_count)  output = 2715 batch_size = 4 img_height = 32 img_width = 32 train_ds = tf.keras.preprocessing.image_dataset_from_directory( DATASET_PATH.name, validation_split=0.8, subset="training", seed=123, image_size=(img_height, img_width), batch_size=batch_size)  output: Found 0 files belonging to 0 classes. Using 0 files for training. Traceback (most recent call last): File ".\tensorDataPreProcessed.py", line 23, in <module> batch_size=batch_size) File "C:\Users\xxx\Anaconda3\envs\xxx\lib\site-packages\tensorflow\python\keras\preprocessing\image_dataset.py", line 200, in image_dataset_from_directory raise ValueError('No images found.') ValueError: No images found. 

Answer: There are two issues here, firstly image_dataset_from_directory requires subfolders for each of the classes within the directory. This way it can automatically identify and assign class labels to images. So the standard folder structure for TF is: data | |___train | |___class_1 | |___class_2 | |___validation | |___class_1 | |___class_2 | |___test(optional) |___class_1 |___class_2  The other issue is that you are attempting to create a model using only one class which is not a way to go. The model needs to be able to differentiate between the class you are trying to generate using GAN but to do this it needs a sample of images that do not belong to this class.
Question: Starred expressions raise SyntaxError when used in list or generator comprehension. I'm curious about the reason behind this; is it an implementation choice or there are technical constraints that would prevent this operation? I've found a lot about the contexts that don't allow for unpacking iterables but nothing about why. Example: lis = [1, 2, 3, 4, 5] listcomp = [*lis for i in range(3)]  I thought maybe I could use this to get [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5] as a result, but it raises  SyntaxError("Iterable unpacking cannot be used in comprehension") 

Answer: This was proposed in PEP 448 -- Additional Unpacking Generalizations but ultimately not accepted due to concerns about readability:  Earlier iterations of this PEP allowed unpacking operators inside list, set, and dictionary comprehensions as a flattening operator over iterables of containers: [...] This was met with a mix of strong concerns about readability and mild support. In order not to disadvantage the less controversial aspects of the PEP, this was not accepted with the rest of the proposal.  Notably, the possibility to add this at a later point has not been ruled out.  This PEP does not include unpacking operators inside list, set and dictionary comprehensions although this has not been ruled out for future proposals.
Question: I have been making a program in python 3.9 and after having this code: #Print 3 dots at the interval shown def dots(t): t *= 3 sleep(t) print('.', end='') sleep(t) print('.', end='') sleep(t) print('.')  And this calling it: # These are completely aesthetic sleep(0.25) print("Defining Functions", end='') dots(0.4)  I expected for the program to print Defining Functions and after 1.2 seconds, 3 times, add a dot(.) at the end. What really happened is after 3.85 seconds it printed the whole thing altogether with dots(Defining Functions...). So it has not printed anything until a new line is added(3rd dot). Sorry if it's messy, I don't know how to make these questions well

Answer: Python buffers output to stdout. This is because writing larger pieces of text at a time is more efficient (less syscalls). By default, if stdout is connected to a terminal, the output will be line-buffered. Thus printing a newline flushes the buffer and you see the output immediately. If stdout is redirected into a pipe or file, Python buffers even more aggressively and will not flush on newlines, only when the buffer is full. To avoid this problem, flush the buffer explicitly before sleeping:  print('.', end='', flush=True)
Question: I have written my test classes in a file and I am trying to use pytest fixtures so that I don't have to create the same input data in each test functions. Below is the minimal working example. import unittest import pytest @pytest.fixture def base_value(): return 5 class Test(unittest.TestCase): def test_add_two(self, base_value): result = base_value + 2 self.assertEqual(result, 7, "Result doesn't match")  However, when I test this using pytest-3, I get the following error:  TypeError: test_add_two() missing 1 required positional argument: 'base_value'  This is confusing for me since the base_value is clearly given as one of the arguments to test_add_two. Any help is highly appreciated.

Answer: This is because you are mixing pytest and unittest. Try @pytest.fixture def base_value(): return 5 class Test: def test_add_two(self, base_value): result = base_value + 2 assert result == 7, "Result doesn't match"  And in case of failure the error will be def test_add_two(self, base_value): result = base_value + 2 > assert result == 8, "Result doesn't match" E AssertionError: Result doesn't match E assert 7 == 8  But isn't pytest compatible with unittest? Only on a limited basis. From Pytest unittest.TestCase Support  pytest features in unittest.TestCase subclasses The following pytest features work in unittest.TestCase subclasses:  Marks: skip, skipif, xfail; Auto-use fixtures;  The following pytest features do not work, and probably never will due to different design philosophies:  Fixtures (except for autouse fixtures, see below); Parametrization; Custom hooks;  Third party plugins may or may not work well, depending on the plugin and the test suite.
Question: I recently found I could make all my matplotlib figures with SVG by default in my jupyterlab notebooks with import matplotlib.pyplot as plt %matplotlib inline %config InlineBackend.figure_formats = ['svg']  However, if I refresh the page, the figures disappear, leaving behind <Figure size 864x576 with 1 Axes>  This effect does not occur without changing the inline backend. My plotly figures also still show up after a refresh. I would prefer not to have to re-run the cells. Looking at the actual ipynb file, the SVG is right there in the actual file. How can I get this figure to show without re-running the cell?

Answer: The behaviour looks in line with the security model of Jupyter.   Untrusted HTML is always sanitized Untrusted Javascript is never executed HTML and Javascript in Markdown cells are never trusted Outputs generated by the user are trusted Any other HTML or Javascript (in Markdown cells, output generated by others) is never trusted The central question of trust is “Did the current user do this?”   Because SVG can have <script> tags, there's an attack surface. Hence, you have two options to display the SVG:  Re-run the cell that generates an SVG (last point above). Explicitly trust the notebook, jupyter trust /path/to/notebook.ipynb.  Notes:  Plotly saves image/png version in the output of the cell. It's displayed when the notebook is loaded. But the loaded (not reran) cell also has interactivity. Not sure how that exemption works. As a side note, current JupyterLab's behaviour varies with different forms of SVG display (see issue#10464), but that's not the issue here.