eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I need to access a browser that is already open. I'm using Python, but if you have another language, I welcome help! I'm trying to access a website and when I break its captcha even manually (the browser is starting with Selenium) it simply reports that it's wrong even though it's right and I can't access the certificate. In anonymous mode it shows the certificate but when clicking it goes wrong anyway. I tried to access in anonymous mode, as another user, doing different ways so the site wouldn't recognize the robot.

Answer: you can run chrome with command: chrome.exe --remote-debugging-port=9222 --user-data-dir="D:/ChromeProfile"  the user-data-dir is a new dir for user data. and in selenium, you should create driver like this: from selenium.webdriver.chrome.options import Options from selenium import webdriver chrome_options = Options() chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222") driver = webdriver.Chrome(options=chrome_options)  remember use the same port.
Question: I have a structure like so: mod1 ├── mod2 │ ├── __init__.py │ └── utils.py └── tests └── test_utils.py  where  __init__.py:  CONST = -1   utils.py:  from mod1.mod2 import CONST def mod_function(): print(CONST)   test_utils.py:  from mod1.mod2.utils import mod_function def test_mod_function(mocker): mock = mocker.patch("mod1.mod2.CONST") mock.return_value = 1000 mod_function()  I'm using pytest with mocker. By running python -m pytest -s ./mod1/tests I expected to see 1000 as an output, but got -1. Why? How to patch a constant from the __init__.py file?

Answer: What patch("mod1.mod2.CONST") does is really to set the CONST attribute of the module object mod1.mod2 to a different object. It does not affect any existing names referencing the original mod1.mod2.CONST object. When utils.py does: from mod1.mod2 import CONST  it creates a name CONST in the namespace of the mod1.mod2.utils module, referencing the object -1 currently referenced by the mod1.mod2.CONST name. And when test_mod_function then does: mock = mocker.patch("mod1.mod2.CONST")  it modifies mod1.mod2 such that its CONST attribute now references a Mock object, while the CONST name in mod1.mod2.utils continues to reference the object -1, which is why setting mock.return_value does not affect the outcome of mod_function. To properly test mod_function with a mock CONST you can either patch CONST in the namespace where mod_function is defined: mock = mocker.patch("mod1.mod2.utils.CONST")  or you can defer the import of mod1.mod2.utils until mod1.mod2.CONST has been patched: def test_mod_function(mocker): mock = mocker.patch("mod1.mod2.CONST") mock.return_value = 1000 from mod1.mod2.utils import mod_function mod_function()
Question: I was getting an error when trying to use a Pydantic schema as an args_schema parameter value on a @tool decorator, following the DeepLearning.AI course. My code was: from pydantic import BaseModel, Field class SearchInput(BaseModel): query: str = Field(description="Thing to search for") @tool(args_schema=SearchInput) def search(query: str) -> str: """Searches for weather online""" return "21c"  And was getting this error: ValidationError: 1 validation error for StructuredTool args_schema subclass of BaseModel expected (type=type_error.subclass; expected_class=BaseModel) 

Answer: Downgrading to pydantic 1.10.10 worked for me. Add pydantic==1.10.10 to your requirements.txt and install it with pip install -r requirements.txt or with the command pip install pydantic==1.10.10
Question: I want to create multiple python Azure Functions within one Azure Functions App using the azure cli/core tools. One http triggered function and one blob triggered function. I have the following folder structure: ├── azure-functions │ ├── az-func-blob │ │ ├── .python_packages │ │ ├── .vscode │ │ ├── .gitignore │ │ ├── function_app.py │ │ ├── host.json │ │ ├── local_settings.json │ │ ├── requirements.txt │ ├── az-func-http │ │ ├── .python_packages │ │ ├── .vscode │ │ ├── .gitignore │ │ ├── function_app.py │ │ ├── host.json │ │ ├── local_settings.json │ │ ├── requirements.txt │ ├── README.md  Note: I created the individual function directories with the following commands within my project dir "azure-functions":  Blob function: func init az-func-blob --worker-runtime python --model V2 cd az-func-blob func new --template "Blob trigger" --name BlobTrigger  Http function: func init az-func-http --worker-runtime python --model V2 cd az-func-http func new --template "HTTP trigger" --name HTTPTrigger   If I navigate to the directory of one of these functions and run the command func azure functionapp publish SPIEFUNC2 it works fine. However running this command again in the other function directory will overwrite the Trigger/Function in Azure instead of appending a new one even though the trigger names are different. Apparently that's just how it works in Azure but I don't know how to create multiple functions within one Function app. I saw this post here where someone suggested to use the same "host.json" for all functions. I don't know how to do this as every host.json is exactly the same anyway and looks like this: { "version": "2.0", "logging": { "applicationInsights": { "samplingSettings": { "isEnabled": true, "excludedTypes": "Request" } } }, "extensionBundle": { "id": "Microsoft.Azure.Functions.ExtensionBundle", "version": "[4.*, 5.0.0)" } }  Can someone please explain in detail how to get set this up using only one Azure Function App instead of multiple function apps?

Answer:  Can someone please explain in detail how to get set this up using only one Azure Function App instead of multiple function apps?  I am able to deploy multiple functions to a Function App using func azure functionapp publish <function_APP_Name> command. Thanks @Thomas for the comment, you need to run func init --worker-runtime python --model V2 command once and func new twice.  I have created one Http Triggered Function and Blob Triggered Function as shown below-    When you will run func init --worker-runtime python --model V2, all the default files will get created for a function. Upon running func new, it will add the triggers in it.  I have deployed the functions to the function app and got successful deployed output.   I am able to see the functions in the function app.  Output-
Question: Using the following C++ struct as an example: __attribute__((annotate("MyAttribute"))) struct TestComponent { __attribute__((annotate("MyAttribute"))) int32_t testInt; __attribute__((annotate("MyAttribute"))) bool testBool; __attribute__((annotate("MyAttribute"))) char testChar; };  Given a node (cursor) from clang using (clang module's cindex) while parsing the AST, using the following I can get the annotations on the class members when node is pointing to TestComponent and has a kind equal to CursorKind.STRUCT_DECL: def get_annotations(node): annotations = [c.displayname for c in node.get_children() if c.kind == clang.cindex.CursorKind.ANNOTATE_ATTR] return annotations  But class/struct annotations never show up as children of a struct/class. Is there a way to get to those? Even in the C bindings - I can try Monkeypatching it, but couldn't find anything.

Answer: tl;dr: attribute should be placed between struct and TestComponent struct __attribute__((annotate("MyAttribute"))) TestComponent  If you print tu.diagnostics, clang does issue a -Wignored-attributes warning: test.cpp:2:17: warning: attribute 'annotate' is ignored, place it after "struct" to apply attribute to type declaration [-Wignored-attributes]  Once fixed, ANNOTATE_ATTR is visible in AST:  test.cpp CursorKind.TRANSLATION_UNIT TestComponent CursorKind.STRUCT_DECL MyStruct CursorKind.ANNOTATE_ATTR testInt CursorKind.FIELD_DECL MyInt CursorKind.ANNOTATE_ATTR testBool CursorKind.FIELD_DECL MyBool CursorKind.ANNOTATE_ATTR testChar CursorKind.FIELD_DECL MyChar CursorKind.ANNOTATE_ATTR  My test code for reference: code = """ __attribute__((annotate("InvalidAttribute"))) struct __attribute__((annotate("MyStruct"))) TestComponent { __attribute__((annotate("MyInt"))) int testInt; __attribute__((annotate("MyBool"))) bool testBool; __attribute__((annotate("MyChar"))) char testChar; }; """ from clang.cindex import Cursor, Index, Config Config.set_library_file('C:/llvm-16/bin/libclang.dll') index = Index.create() args = ['-x', 'c++', '-std=c++20', 'test.cpp'] tu = index.parse(None, args, unsaved_files=[('test.cpp', code)]) for d in tu.diagnostics: print(d) def recurse(c: Cursor, indent=0): print(' ' * indent, c.spelling, c.kind) for n in c.get_children(): recurse(n, indent + 2) recurse(tu.cursor)
Question: I'm trying to see whether it is possible to solve the following problem Suppose I have to classes: class A(): def __init__(self, param: str, **kwargs) -> None: ... class B(A): def __init__(self, **kwargs) -> None: ... super().__init__(**kwargs)  When using pyright, I don't get any (type) checking on arguments to class A since the B's **kwargs just take all arguments. Is it possible to type hint class B to mimic A._init_'s signature? In older posts the main solution seems to be to just redefine param, however in my case A's signature might change over time and I would prefer it if I didn't have to touch B in those cases.  I was hoping this was maybe achievable with the "somewhat recent" additions of TypeVar and ParamSpec. I went down a rabbit hole where I would have class B have a more generic signature: P = ParamSpec('P') T = TypeVar('T') class B(Generic[P, T]): def __init__(self, cls: Type[T], *args: P.args, **kwargs: P.kwargs) -> None: ... super().__init__(**kwargs)  But in this case B obviously wouldn't inherit from my generic T and I'm not even sure whether that's possible or would even solve my problem so I stopped pursuing that road

Answer: If you merely want to run some custom logic in the child class' __init__, a decorator approach will preserve the signature without the need to explicitly repeat it. from typing import Callable, Concatenate, ParamSpec, Protocol, TypeVar P = ParamSpec("P") SelfT = TypeVar("SelfT", contravariant=True) class Init(Protocol[SelfT, P]): def __call__(__self, self: SelfT, *args: P.args, **kwds: P.kwargs) -> None: ... def overinit(init: Callable[Concatenate[SelfT, P], None]) -> Init[SelfT, P]: def __init__(self: SelfT, *args: P.args, **kwargs: P.kwargs) -> None: # put your code here init(self, *args, **kwargs) return __init__ class Parent: def __init__(self, a: int, b: str, c: float) -> None: self.a = a self.b = b self.c = c class Child(Parent): __init__ = overinit(Parent.__init__) Child(1, "123", 123)
Question: Where are temporary files saved when running the commands or are there no temporary files created? python -c "print('Hello, World!')"  and exec("print('Hello, World!')")  Both Windows and macOS. Does Python run directly or is it compiled into a temporary file and then run? I use Google Translate.

Answer: No, neither of these operations creates any temporary file. Python creates .pyc files only as a performance optimization to speed future loading of the same code, and works perfectly well without them. This is typical behavior for interpreted languages as opposed to compiled ones. As Python is generally compiled only to bytecode and not to native executables (even tools that create native executables from Python generally pack an interpreter and source code or bytecode for it to work on into that executable, instead of properly compiling to native CPU instructions); and as that bytecode can be interpreted in-process by the same tool that compiles Python source into said bytecode, Python is for this purpose effectively interpreted. (There's a larger discussion of the semantics at Is Python interpreted, or compiled, or both?, but the definition doesn't really matter for practical purposes so I'm not going to go into it further here).  What would create a temporary file (with most but not all shells) is: python <<EOF print("Hello, world!") EOF  ...but in that case the file is created by the shell, not by Python itself.
Question: Is it possible to get the code object of top level code within a module? For example, if you have a python file like this: myvar = 1 print('hello from top level') def myfunction(): print('hello from function')  and you want to access the code object for myfunction, then you can use myfunction.__code__. For example, myfunction.__code__.co_consts will contain the string 'hello from function' etc... Is there a way to get the code object for the top level code? That is, for the code: myvar = 1 print('hello from top level')  I would like something like __main__.__code__.co_consts that will contain 'hello from top level', but I cannot find any way to get this. Does such a thing exist?

Answer: The code that is executed at the top level of a module is not directly accessible as a code object in the same way that functions' code objects are, because the top-level code is executed immediately when the module is imported or run, and it doesn't exist as a separate entity like a function does. But when Python runs a script, it compiles it first to bytecode and stores it in a code object. The top-level code (__main__ module), have a code object, but it is not directly exposed, so you need to use inspect module to dig deeper: import inspect def get_top_level_code_object(): frame = inspect.currentframe() # Go back to the top-level frame while frame.f_back: frame = frame.f_back # The code object is stored in f_code return frame.f_code if __name__ == "__main__": top_level_code_obj = get_top_level_code_object() print(top_level_code_obj.co_consts)  would yield (0, None, <code object get_top_level_code_object at 0x7f970ad658f0, file "/tmp/test.py", line 3>, '__main__')
Question: I have two png images with transparent background example:   and I'm trying to achieve something similar to the PIL paste() function in pyvips, where I can merge two images and 'remove the transparent background' using a mask. PIL example: image = Image.open(filepath, 'r') image_2 = Image.open(filepath, 'r') image.paste(image_2, (50, 50), mask=mask)  Expected Result:  I've already tried to use the pyvips insert() function, but both images retain their backgrounds. Pyvips insert: image = pyvips.Image.new_from_file(path, access='sequential') image_2 = pyvips.Image.new_from_file(path, access='sequential') image = image.insert(image_2, 50,50)  Pyvips Result:  How can I have the "expected result" with pyvips?

Answer: You can do it like this, using the composite() function: import pyvips # Load background and foreground images bg = pyvips.Image.new_from_file('WA8rm.png', access='sequential') fg = pyvips.Image.new_from_file('Ny50t.png', access='sequential') # Composite foreground over background and save result bg.composite(fg, 'over').write_to_file('result.png')    If you want to add an offset in x and y, use: bg.composite(fg, 'over', x=200, y=100).write_to_file('result.png')
Question: I have a Polars Dataframe, and I would like to contrust a new Dataframe that consists of all possible combinations choosing 1 element from each row. Visually like so: An input Dataframe | Column A | Column B | Column C | | -------- | -------- | -------- | | A1 | B1 | C1 | | A2 | B2 | C2 | | A3 | B3 | C3 |  would give | Column A | Column B | Column A | | -------- | -------- | -------- | | A1 | A2 | A3 | | A1 | A2 | B3 | | A1 | A2 | C3 | | A1 | B2 | A3 | | A1 | B2 | B3 | | A1 | B2 | C3 | | A1 | C2 | A3 | | A1 | C2 | B3 | | A1 | C2 | C3 | | B1 | A2 | A3 | | B1 | A2 | B3 | | B1 | A2 | C3 | | B1 | B2 | A3 | | B1 | B2 | B3 | | B1 | B2 | C3 | | B1 | C2 | A3 | | B1 | C2 | B3 | | B1 | C2 | C3 |  etc... I have tried to implement this by simply using a 2D array and a double for loop which is fairly simple, however, I would really like to implment this using Polars' Dataframes and being compliant with the way Polars is built as I'm hoping it can compute much faster than a double for loop would. This library is still fairly new to me so please let me know if there is some kind of misunderstanding on my end.

Answer: There's no direct "combinations" functionality as far as I am aware. One possible approach is to .implode() then .explode() each column. df = pl.from_repr(""" ┌──────────┬──────────┬──────────┐ │ Column A ┆ Column B ┆ Column C │ │ --- ┆ --- ┆ --- │ │ str ┆ str ┆ str │ ╞══════════╪══════════╪══════════╡ │ A1 ┆ B1 ┆ C1 │ │ A2 ┆ B2 ┆ C2 │ │ A3 ┆ B3 ┆ C3 │ └──────────┴──────────┴──────────┘ """)  (df.select(pl.all().implode()) .explode("Column A") .explode("Column B") .explode("Column C") )  shape: (27, 3) ┌──────────┬──────────┬──────────┐ │ Column A ┆ Column B ┆ Column C │ │ --- ┆ --- ┆ --- │ │ str ┆ str ┆ str │ ╞══════════╪══════════╪══════════╡ │ A1 ┆ B1 ┆ C1 │ │ A1 ┆ B1 ┆ C2 │ │ A1 ┆ B1 ┆ C3 │ │ A1 ┆ B2 ┆ C1 │ │ … ┆ … ┆ … │ │ A3 ┆ B2 ┆ C3 │ │ A3 ┆ B3 ┆ C1 │ │ A3 ┆ B3 ┆ C2 │ │ A3 ┆ B3 ┆ C3 │ └──────────┴──────────┴──────────┘  You can add .unique() in the case of any duplicate values. Instead of having to name each column, you could use the Lazy API and a loop: out = df.lazy().select(pl.all().implode()) for col in df.columns: out = out.explode(col) out.collect()
Question: I have tried to install any python packages on Ubuntu:24.04, but found I cannot do that as in 22:04 PEP668 said it is for avoiding package conflict between system-wide package and user installed package. But what's the differences between using pipx and pip --user? And why the --user option not work, it also installs packages to the user's own home. Example: $ pip install setuptools --user error: externally-managed-environment × This environment is externally managed ╰─> To install Python packages system-wide, try apt install python3-xyz, where xyz is the package you are trying to install. If you wish to install a non-Debian-packaged Python package, create a virtual environment using python3 -m venv path/to/venv. Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make sure you have python3-full installed. If you wish to install a non-Debian packaged Python application, it may be easiest to use pipx install xyz, which will manage a virtual environment for you. Make sure you have pipx installed. See /usr/share/doc/python3.11/README.venv for more information. note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages. hint: See PEP 668 for the detailed specification.  But if I do that with pipx: $ pipx install pip No apps associated with package pip or its dependencies. If you are attempting to install a library, pipx should not be used. Consider using pip or a similar tool instead.  I am really confused with current rules. How can I manage my user global environment now? And how can I use latest pip(not linux-distro version) and other packages by default for current user? My environment: FROM ubuntu:24.04 # add python RUN apt install -y python3-pip python3-venv python-is-python3 pipx USER ubuntu WORKDIR /app 

Answer: pip --user is a function provided by pip, and pipx is a software package. Ubuntu can guarantee that the packages you install using pipx will not pollute the system environment, but pip does not have this guarantee. Additionally, you should install setuptools using apt install python3-setuptools.
Question: I have a pydantic basemodel that looks something like: from pathlib import Path from pydantic import BaseModel class Model(BaseModel): log_file: Path  And my ruff pre-commit hook is re-ordering it to: from typing import TYPE_CHECKING from pydantic import BaseModel if TYPE_CHECKING: from pathlib import Path class Model(BaseModel): log_file: Path  Which is then causing the bug: pydantic.errors.ConfigError: field "log_file" not yet prepared so type is still a ForwardRef, you might need to call Model.update_forward_refs()  Which I dont want to do. How can I stop ruff from re-ordering the imports like this? My .pre-commit-config.yaml file looks like: repos: - repo: https://github.com/charliermarsh/ruff-pre-commit rev: "v0.0.291" hooks: - id: ruff args: [--fix, --exit-non-zero-on-fix] - repo: https://github.com/psf/black rev: 23.9.1 hooks: - id: black language_version: python3  and my pyproject.toml file: [tool.black] line-length = 120 include = '\.pyi?$' exclude = ''' /( \.eggs | \.git | \.hg | \.mypy_cache | \.tox | \.venv | _build | buck-out | build | dist # The following are specific to Black, you probably don't want those. | blib2to3 | tests/data | profiling )/ ''' [tool.ruff] line-length = 120 ignore = ["F405", "B008"] select = ["E", "F", "B", "C4", "DTZ", "PTH", "TCH", "I001"] # unfixable = ["C4", "B"] exclude = ["docs/conf.py", "Deployment/make_deployment_bundle.py"] [tool.ruff.per-file-ignores] "**/__init__.py" = ["F401", "F403"] [tool.ruff.isort] split-on-trailing-comma = true known-first-party = ["influxabart"] no-lines-before = ["local-folder"] section-order = ["future","standard-library","third-party","first-party","this","local-folder"] [tool.ruff.isort.sections] "this" = ["InfluxTools"] 

Answer: Haven't reproduced this, but I think your list of select options is causing this. I know that this isn't the default behaviour of ruff. select = ["E", "F", "B", "C4", "DTZ", "PTH", "TCH", "I001"]  Even though the official readme says TC* is the code for flake8-type-checking, this issue hints that TCH* is. From the README: TC001 Move application import into a type-checking block TC002 Move third-party import into a type-checking block TC003 Move built-in import into a type-checking block  So TC003 is what's happening to you. Or TCH003 is what I'm guessing ruff calls it. So I think the solution is to remove that "TCH" field from the select in your pyproject.toml.