eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: Consider the following examples: string_now = 'apple and avocado' stringthen = string_now.swap('apple', 'avocado') # stringthen = 'avocado and apple'  and: string_now = 'fffffeeeeeddffee' stringthen = string_now.swap('fffff', 'eeeee') # stringthen = 'eeeeefffffddffee'  Approaches discussed in Swap character of string in Python do not work, as the mapping technique used there only takes one character into consideration. Python's built-in str.maketrans() also only supports one-character translations, as when I try to do multiple characters, it throws the following error: Traceback (most recent call last): File "main.py", line 4, in <module> s.maketrans(mapper) ValueError: string keys in translate table must be of length 1  A chain of replace() methods is not only far from ideal (since I have many replacements to do, chaining replaces would be a big chunk of code) but because of its sequential nature, it will not translate things perfectly as: string_now = 'apple and avocado' stringthen = string_now.replace('apple', 'avocado').replace('avocado', 'apple')  gives 'apple and apple' instead of 'avocado and apple'. What's the best way to achieve this?

Answer: Given that we want to swap words x and y, and that we don't care about the situation where they overlap, we can:  split the string on occurrences of x within each piece, replace y with x join the pieces with y  Essentially, we use split points within the string as a temporary marker to avoid the problem with sequential replacements. Thus: def swap_words(s, x, y): return y.join(part.replace(y, x) for part in s.split(x))  Test it: >>> swap_words('apples and avocados and avocados and apples', 'apples', 'avocados') 'avocados and apples and apples and avocados' >>>
Question: I'm trying to plot an oriented graph with pyvis. In the documentation they suggest using the following command for creating an oriented edge: net.add_edge(4,1,from=1,to=4)  The problems are two:  I'm getting this error   TypeError: add_edge() got multiple values for argument 'to'   from is a python keyword so it can't be used as a parameter.  Any suggestion?

Answer: You don't need to directly specify to and from in your add_edge function if you had specified directed=True when you created your network. The order of the nodes in the add_edge function is enough to describe the direction. Below is an example: from pyvis.network import Network net = Network(directed =True) net.add_node(0, label='a') net.add_node(1, label='b') net.add_edge(0,1) net.show('mygraph.html')  And the output gives:
Question: How would I add two df columns together (date + weeks): This works for me: df['Date'] = pd.to_datetime(startDate, format='%Y-%m-%d') + datetime.timedelta(weeks = 3) But when I try to add weeks from a column, I get a type error: unsupported type for timedelta weeks component: Series df['Date'] = pd.to_datetime(startDate, format='%Y-%m-%d') + datetime.timedelta(weeks = df['Duration (weeks)']) Would appreciate any help thank you!

Answer: You can use the pandas to_timelta function to transform the number of weeks column to a timedelta, like this: import pandas as pd import numpy as np # create a DataFrame with a `date` column df = pd.DataFrame( pd.date_range(start='1/1/2018', end='1/08/2018'), columns=["date"] ) # add a column `weeks` with a random number of weeks df['weeks'] = np.random.randint(1, 6, df.shape[0]) # use `pd.to_timedelta` to transform the number of weeks column to a timedelta # and add it to the `date` column df["new_date"] = df["date"] + pd.to_timedelta(df["weeks"], unit="W") df.head() date weeks new_date 0 2018-01-01 5 2018-02-05 1 2018-01-02 2 2018-01-16 2 2018-01-03 2 2018-01-17 3 2018-01-04 4 2018-02-01 4 2018-01-05 3 2018-01-26
Question: I cannot switch to the sucessfully identified iFrame(s). The script identifies the iFrame (checked in debugger), but the switch to the iFrame fails and runs into the exception trap. Few times ago it worked perfectly. Message='WebDriver' object has no attribute 'switch_to_frame'  What happened in the meantime? Chromedriver has been updated from version 95.0.4638.17 to ChromeDriver 96.0.4664.45 Is the Chromedriver is no longer compatible with the latest Selenium version?  ... driver.switch_to.default_content() try: # find the frame wait.until(EC.element_to_be_clickable((By.ID, "wysiwygTextarea_ifr"))) frame2 = driver.find_element(By.XPATH, "//iframe[@id='wysiwygTextarea_ifr']"); # switch to frame driver.switch_to.frame(frame2.tag_name); print("--------------iframe found-------------------"); except: print("--------------iframe not found-------------------"); ...  

Answer: While switching to frame, the supported notations are:  Switch to a frame using frame name: driver.switch_to.frame('frame_name')   Switch to a frame using frame index: driver.switch_to.frame(1)   Switch to a frame using frame element: driver.switch_to.frame(driver.find_elements_by_tag_name("iframe")[0])     Switch to parent frame: driver.switch_to.parent_frame()    Switch to default content: driver.switch_to.default_content()     This usecase To switch the frame you have used: driver.switch_to.frame(frame2.tag_name);  that is, the TAG_NAME which isn't supported. Hence you see the error: Message='WebDriver' object has no attribute 'switch_to_frame'   Solution You can use the following line of code: # find the frame wait.until(EC.element_to_be_clickable((By.ID, "wysiwygTextarea_ifr"))) frame2 = driver.find_element(By.XPATH, "//iframe[@id='wysiwygTextarea_ifr']"); # switch to frame by frame element driver.switch_to.frame(frame2);
Question: Is there a method to extract all possible named entity types from a model in spaCy? You can manually figure it out by running on sample text, but I imagine there is a more programmatic way to do this? For example: import spacy model=spacy.load("en_core_web_sm") model.*returns_entity_types* 

Answer: The statistical pipeline components like ner provide their labels under .labels: import spacy nlp = spacy.load("en_core_web_sm") nlp.get_pipe("ner").labels
Question: I have a pandas dataframe with a categorical series that has missing categories. In the example shown below, group has the categories "a", "b", and "c", but there are no cases of "c" in the dataframe. import pandas as pd dfr = pd.DataFrame({ "id": ["111", "222", "111", "333"], "group": ["a", "a", "b", "b"], "value": [1, 4, 9, 16]}) dfr["group"] = pd.Categorical(dfr["group"], categories=["a", "b", "c"]) dfr.pivot(index="id", columns="group")  The resulting pivoted dataframe has columns a and b. I expected a c column containing all missing value as well.  value group a b id 111 1.0 9.0 222 4.0 NaN 333 NaN 16.0  How can I pivot a dataframe on a categorical series to include columns with all categories, regardless of whether they were present in the original dataframe?

Answer: pd.pivot_table has a dropna argument which dictates dropping or not value columns full of NaNs. Try setting it to False: import pandas as pd dfr = pd.DataFrame({ "id": ["111", "222", "111", "333"], "group": ["a", "a", "b", "b"], "value": [1, 4, 9, 16]}) dfr["group"] = pd.Categorical(dfr["group"], categories=["a", "b", "c"]) pd.pivot_table(dfr, index="id", columns="group", dropna=False)
Question: I am trying to make a function that takes a pydantic BaseModel as an input to run another function. I need to unpack the BaseModel into kwargs. I tried doing this: def run_routing_from_job(job): return run_routing( job.inputs.input_file, **job.inputs.config.dict() )  where job is of the format class Job(BaseModel): client_info: ClientInfo # Another BaseModel inputs: RoutingJobInputs # Another BaseModel uid: UUID = Field(default_factory=uuid4) status: str = "job_queued" result: int = None  However, doing .dict() parses all of the items recursively into a dictionary format. I want to keep the client_info and inputs as a BaseModel class, not convert it into a dictionary. I could make a way to do it, but I can't find a clean way to do it.

Answer: I worked it out, just replace .dict() with __dict__ def run_routing_from_job(job): return run_routing( job.inputs.input_file, **job.inputs.config.__dict__ )
Question: this easy example from matplotlib throws the mentioned error: https://matplotlib.org/stable/gallery/lines_bars_and_markers/barchart.html I also cannot find any typo according to the documentation of set_xticks(): https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.set_xticks.html Can anyone provide clearification and some help, please? Kind regards, Daniel

Answer: This is error is based on the matplotlib version you use. The set_axis command was changed from matplotlib version 3.4.x to 3.5.x. Try using the newer version. You can install it via $ pip install matplotlib==3.5.0
Question: I got a large list of JSON objects that I want to parse depending on the start of one of the keys, and just wildcard the rest. A lot of the keys are similar, like "matchme-foo" and "matchme-bar". There is a builtin wildcard, but it is only used for whole values, kinda like an else. I might be overlooking something but I can't find a solution anywhere in the proposal: https://docs.python.org/3/whatsnew/3.10.html#pep-634-structural-pattern-matching Also a bit more about it in PEP-636: https://www.python.org/dev/peps/pep-0636/#going-to-the-cloud-mappings My data looks like this: data = [{ "id" : "matchme-foo", "message": "hallo this is a message", },{ "id" : "matchme-bar", "message": "goodbye", },{ "id" : "anotherid", "message": "completely diffrent event" }, ...]  I want to do something that can match the id without having to make a long list of |'s. Something like this: for event in data: match event: case {'id':'matchme-*'}: # Match all 'matchme-' no matter what comes next log.INFO(event['message']) case {'id':'anotherid'}: log.ERROR(event['message'])  It's a relatively new addition to Python so there aren't many guides on how to use it yet.

Answer: You can use a guard.  for event in data: match event: case {'id': x} if x.startswith("matchme"): # guard print(event["message"]) case {'id':'anotherid'}: print(event["message"])  Quoting from the official documentation:  Guard We can add an if clause to a pattern, known as a “guard”. If the guard is false, match goes on to try the next case block. Note that value capture happens before the guard is evaluated: match point: case Point(x, y) if x == y: print(f"The point is located on the diagonal Y=X at {x}.") case Point(x, y): print(f"Point is not on the diagonal.")   See also:  PEP 622 - Guards PEP 636 - Adding conditions to patterns PEP 634 - Guards
Question: I am trying to install Cartopy on Windows. I have installed all the dependencies from their website, however when I go to run pip install Cartopy  I get:  Complete output (5 lines): setup.py:117: UserWarning: Unable to determine GEOS version. Ensure you have 3.7.2 or later installed, or installation may fail. warnings.warn( setup.py:166: UserWarning: Unable to determine Proj version. Ensure you have 8.0.0 or later installed, or installation may fail. warnings.warn( Proj version 0.0.0 is installed, but cartopy requires at least version 8.0.0  I have ran and succesfully completed pip install proj pip install geos 

Answer: Installing Cartopy on Windows using pip is not trivial. Nevertheless, here is a cartopy installation overview using the method that worked for me, specifically for Windows and without using conda.  Start by uninstalling proj, geos, and shapely if they are already installed, otherwise skip to step 2. This will facilitate linking them in later steps. pip uninstall shapely pip uninstall proj pip uninstall geos  Install proj and geos from OSGeo4W. You cannot use pip to install these because pip points to other projects of the same name. Instead, use the OSGeo4W installer: https://trac.osgeo.org/osgeo4w/ Run as admin and use all the default options, including default installation directories (Advanced Install -> Install from Internet -> All Users -> Next -> Direct Connection -> download.osgeo.org). Then search proj, expand Libs and click the top two "skip"s (proj and proj-data) once each to toggle to the latest release. Now search geos, expand Libs again, and toggle the first "skip" (geos) once to the latest version. Then click next, allow the installer to load dependencies, and click next. The installation took ~5 minutes for me. You now have proj and geos installed.  Install shapely from the .whl. You cannot use the method listed in the cartopy install instructions; it fails to link shapely to geos and you will get an error when importing cartopy. Instead, head to https://www.lfd.uci.edu/~gohlke/pythonlibs/#shapely https://pypi.org/project/shapely/#files and download the version that suits your python installation (e.g. if you run 64-bit python 3.10, download shapely‑2.0.2‑cp310‑cp310‑win_amd64.whl) Now you can run pip install \{path}\{to}\{whl}\{Shapely_file.whl}  Install cartopy from the .whl. You can download one that suits you here: https://www.lfd.uci.edu/~gohlke/pythonlibs/#cartopy https://pypi.org/project/Cartopy/#files Pick the one that suits your system (e.g. if you run 64-bit python 3.10, download Cartopy‑0.22.0‑cp310‑cp310‑win_amd64.whl) Now you can run pip install \{path}\{to}\{whl}\{Cartopy_file.whl}   That's it! It took me a long while and sifting through at least a couple dozen "just use conda" threads to figure this out. Select relevant discussions: https://github.com/SciTools/cartopy/issues/1471 https://towardsdatascience.com/install-shapely-on-windows-72b6581bb46c Note: This answer was initially tested and worked with shapely v1.8.2 and cartopy v0.20.2.
Question: I have an API that uses FastAPI. In a single file (main.py), I have the call to the function that creates the API from fastapi import FastAPI # ... app = FastAPI()  As well as all the endpoints: @app.post("/sum") async def sum_two_numbers(number1: int, number2: int): return {'result': number1 + number2}  But as the application gets larger, the file is becoming messy and hard to maintain. The obvious solution would be to keep function definitions in separate modules and just import them and use them in main.py, like this: from mymodules.operations import sum_two_numbers # ... @app.post("/sum") sum_two_numbers(number1: int, number2: int)  Only that doesn't work. I don't know if I'm doing it wrong or it can't be done, but I get this error from VSCode:  Expected function or class declaration after decorator | Pylance  (My program has so many errors that I haven't seen the actual interpreter complaint, but if that's important, I can try debug it and post it here) So is this impossible to do and I have to stick to the one-file API, or it is possible to do, but I'm doing it wrong? If the second, what is the right way?

Answer:  So is this impossible to do and I have to stick to the one-file API, or it is possible to do, but I'm doing it wrong?  One file api is just for demo/test purposes, in the real world you always do multi-file api especialy with framework like fastapi where you use different type of file like pydantic models, db models, etc.  If the second, what is the right way?  There is no "right way", there is ways that fit your needs. You can follow the advanced user guide, to see a good example. what the doc suggest: . ├── app │ ├── __init__.py │ ├── main.py │ ├── dependencies.py │ └── routers │ │ ├── __init__.py │ │ ├── items.py │ │ └── users.py │ └── internal │ ├── __init__.py │ └── admin.py  what i use when i have db, pydantic models, etc . ├── app │ ├── __init__.py │ ├── main.py │ ├── dependencies.py │ └── routers │ │ ├── __init__.py │ │ ├── items.py │ │ └── users.py │ └── models │ │ ├── __init__.py │ │ ├── items.py │ │ └── users.py │ └── schemas │ │ ├── __init__.py │ │ ├── items.py │ │ └── users.py │ └── internal │ │ ├── __init__.py │ │ └── admin.py  here models represent db models and schemas represent pydantic models
Question: First time write unittest. Production code: def get_session_token(organization_id): endpoint = get_endpoint(organization_id) response = requests.post( endpoint + "/some/url/part", data=json.dumps({ "Login": CONFIG[organization_id]["username"], "Password": CONFIG[organization_id]["password"], }), headers={"Accept": "application/json"} ) if response.status_code != 201: log.error("filename.get_session_token(%r): couldn't auth: %r %r", organization_id, response, response.text) raise ValueError() return response.json() def member(organization_id): session_key = get_session_token(organization_id) (some other code...)  I need to test member. And I have test code: @patch('requests.post') def test_member(self, mock_post): mock_post().status_code = 201 mock_response = mock_post("some/url", data=ANY, headers={"Accept": "application/json"}) mock_response.status_code = 201 (some other code...)  Every time I run test, it always raises ValueError()(which is a 403 error) How can I bypass that requests.post and just get a 201? Thank you!

Answer: See Where to patch. And, you should provide a mock return value for the mock_post using return_value - The value returned when the mock is called. E.g. member.py: import requests import json def get_session_token(organization_id): endpoint = 'http://localhost:3000/api' response = requests.post( endpoint + "/some/url/part", data=json.dumps({ "Login": 'python', "Password": '123456', }), headers={"Accept": "application/json"} ) if response.status_code != 201: raise ValueError() return response.json() def member(organization_id): session_key = get_session_token(organization_id) return session_key  test_member.py: from unittest.mock import patch import unittest import json from member import member class TestMember(unittest.TestCase): @patch('member.requests.post') def test_member_success(self, mock_post): mock_post.return_value.status_code = 201 mock_post.return_value.json.return_value = 'mock response' actual = member(1) self.assertEqual(actual, 'mock response') mock_post.assert_called_once_with( 'http://localhost:3000/api/some/url/part', data=json.dumps({ "Login": 'python', "Password": '123456', }), headers={"Accept": "application/json"} ) @patch('member.requests.post') def test_member_failure(self, mock_post): mock_post.return_value.status_code = 400 self.assertRaises(ValueError, member, 1) if __name__ == '__main__': unittest.main()  Test result: . ---------------------------------------------------------------------- Ran 1 test in 0.001s OK Name Stmts Miss Cover Missing ------------------------------------------------------------------------- src/stackoverflow/70098351/member.py 11 2 82% 18, 23 src/stackoverflow/70098351/test_member.py 11 0 100% ------------------------------------------------------------------------- TOTAL 22 2 91%
Question: I would like to use the Structural Pattern Matching feature from Python 3.10 in Google Colab so using the commands !sudo apt-get install python3.10 !sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1 !sudo update-alternatives --set python3 /usr/bin/python3.10  I was able to make !python --version output 3.10.0, but the print(sys.version) still outputs 3.7.12 in code cells and so the match cases statement raises SyntaxError number = 1 match number: case 0: print("Error") case _: print(number)  Is there any way to make this work?

Answer: You can use this notebook.  make a copy of it run the first cell reload (Ctrl + R, or Cmd + R) run the second cell  See this video demo by 1littlecoder.
Question: I am using BitBucket pipelines to perform linting checks with pylint. It was working fine a few hours ago. I have been facing the following error even though the final score is well past the minimum criteria (8.0): Your code has been rated at 9.43/10 Traceback (most recent call last): File "/usr/local/bin/pylint-fail-under", line 8, in <module> sys.exit(main()) File "/usr/local/lib/python3.6/dist-packages/pylint_fail_under/__main__.py", line 42, in main score = results.linter.stats["global_note"] TypeError: 'LinterStats' object is not subscriptable 

Answer: Do not use pylint-fail-under, pylint has a fail-under option since pylint 2.5.0, and pylint-fail-under's maintener will not update their package for newer pylint. Change pylint-fail-under --fail_under 8.0 to pylint --fail-under=8.0 and remove the dependency to pylint-fail-under. See also https://github.com/PyCQA/pylint/issues/5405, and: https://github.com/TNThieding/pylint-fail-under/issues/8#issuecomment-626369567
Question: I'm looking into this Python project template. They use poetry to define dev dependencies [tool.poetry.dev-dependencies] black = {version = "*", allow-prereleases = true} flake8 = "*" isort = "^5.6" mypy = ">0.900,<1" ...  They use also pre-commit to check housekeeping things (e.g., formatting, linting, issorting, ...), both for git and for CI workflows: minimum_pre_commit_version: 2.8.0 default_stages: [commit, push, manual] repos: - repo: https://github.com/psf/black rev: 21.11b1 hooks: - id: black - repo: https://github.com/pycqa/flake8 rev: 4.0.1 hooks: - id: flake8 args: [--max-line-length=88] - repo: https://github.com/pycqa/isort rev: 5.10.1 hooks: - id: isort args: [--filter-files] - ...  In my case, I definitely want a local version of dev packages managed by poetry for my IDE, and I'd like also to harness the pre-commit framework "as is", without switching to language: system. Working this way, I need to manage each package version in two different places. Is there a non-manual way to keep dev packages versions (i.e,. black, flake8, isort, mypy, ...) aligned to a single source of truth? A Coockiecutter template could be an option, but it looks overkilling.

Answer: Finally, I created a pre-commit hook to do the job: https://github.com/floatingpurr/sync_with_poetry  Edit: If you use PDM, you can refer to this one: https://github.com/floatingpurr/sync_with_pdm  This hook just keeps in sync the repos rev in .pre-commit-config.yaml with the packages version locked into poetry.lock. If you use:  Poetry for dependency management local dev packages for your IDE (via Poetry) pre-commit hooks  this meta-hook can be useful to (un-)bump repos rev for you.