eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I would like to generate a list of combinations. I will try to simplify my problem to make it understandable. We have 3 variables :  x : number of letters k : number of groups n : number of letters per group  I would like to generate using python a list of every possible combinations, without any duplicate knowing that : i don't care about the order of the groups and the order of the letters within a group. As an example, with x = 4, k = 2, n = 2 : # we start with 4 letters, we want to make 2 groups of 2 letters letters = ['A','B','C','D'] # here would be a code that generate the list # Here is the result that is very simple, only 3 combinations exist. combos = [ ['AB', 'CD'], ['AC', 'BD'], ['AD', 'BC'] ]  Since I don't care about the order of or within the groups, and letters within a group, ['AB', 'CD'] and ['DC', 'BA'] is a duplicate. This is a simplification of my real problem, which has those values : x = 12, k = 4, n = 3. I tried to use some functions from itertools, but with that many letters my computer freezes because it's too many combinations. Another way of seeing the problem : you have 12 players, you want to make 4 teams of 3 players. What are all the possibilities ? Could anyone help me to find an optimized solution to generate this list?

Answer: There will certainly be more sophisticated/efficient ways of doing this, but here's an approach that works in a reasonable amount of time for your example and should be easy enough to adapt for other cases. It generates unique teams and unique combinations thereof, as per your specifications. from itertools import combinations # this assumes that team_size * team_num == len(players) is a given team_size = 3 team_num = 4 players = list('ABCDEFGHIJKL') unique_teams = [set(c) for c in combinations(players, team_size)] def duplicate_player(combo): """Returns True if a player occurs in more than one team""" return len(set.union(*combo)) < len(players) result = (combo for combo in combinations(unique_teams, team_num) if not duplicate_player(combo))  result is a generator that can be iterated or turned into a list with list(result). On kaggle.com, it takes a minute or so to generate the whole list of all possible combinations (a total of 15400, in line with the computations by @beaker and @John Coleman in the comments). The teams are tuples of sets that look like this: [({'A', 'B', 'C'}, {'D', 'E', 'F'}, {'G', 'H', 'I'}, {'J', 'K', 'L'}), ({'A', 'B', 'C'}, {'D', 'E', 'F'}, {'G', 'H', 'J'}, {'I', 'K', 'L'}), ({'A', 'B', 'C'}, {'D', 'E', 'F'}, {'G', 'H', 'K'}, {'I', 'J', 'L'}), ... ]  If you want, you can cast them into strings by calling ''.join() on each of them.
Question: I am trying to install the latest version of numpy, the 1.22.3, but it looks like pip is not able to find this last release. I know I can install it locally from the source code, but I want to understand why I cannot install it using pip. PS: I have the latest version of pip, the 22.0.4 ERROR: Could not find a version that satisfies the requirement numpy==1.22.3 (from versions: 1.3.0, 1.4.1, 1.5.0, 1.5.1, 1.6.0, 1.6.1, 1.6.2, 1.7.0, 1.7.1, 1.7.2, 1.8.0, 1.8.1, 1.8.2, 1.9.0, 1.9.1, 1.9.2, 1.9.3, 1.10.0.post2, 1.10.1, 1.10.2, 1.10.4, 1.11.0, 1.11.1, 1.11.2, 1.11.3, 1.12.0, 1.12.1, 1.13.0rc1, 1.13.0rc2, 1.13.0, 1.13.1, 1.13.3, 1.14.0rc1, 1.14.0, 1.14.1, 1.14.2, 1.14.3, 1.14.4, 1.14.5, 1.14.6, 1.15.0rc1, 1.15.0rc2, 1.15.0, 1.15.1, 1.15.2, 1.15.3, 1.15.4, 1.16.0rc1, 1.16.0rc2, 1.16.0, 1.16.1, 1.16.2, 1.16.3, 1.16.4, 1.16.5, 1.16.6, 1.17.0rc1, 1.17.0rc2, 1.17.0, 1.17.1, 1.17.2, 1.17.3, 1.17.4, 1.17.5, 1.18.0rc1, 1.18.0, 1.18.1, 1.18.2, 1.18.3, 1.18.4, 1.18.5, 1.19.0rc1, 1.19.0rc2, 1.19.0, 1.19.1, 1.19.2, 1.19.3, 1.19.4, 1.19.5, 1.20.0rc1, 1.20.0rc2, 1.20.0, 1.20.1, 1.20.2, 1.20.3, 1.21.0rc1, 1.21.0rc2, 1.21.0, 1.21.1, 1.21.2, 1.21.3, 1.21.4, 1.21.5) ERROR: No matching distribution found for numpy==1.22.3 

Answer: Please check your Python version. Support for Python 3.7 is dropped since Numpy 1.22.0 release. [source]
Question: I`m trying to find a specific element in the web page. It has to be by a specific number inside the text in value...In this case, I need to find the element by the number 565657 (see outer HTML below) I've tried via xpath: ("//*[contains(text(),'565657')]") and also ("//input[@value='565657']") but it did not work. Can someone please help? Thanks in advance! The original XPATH when you copy from the element on the web page is just ' //*[@id="checkbox15"]' and I need to find a way to look for the number I mentioned below.

Answer: To locate the element through the value attribute i.e. 565657 you can use either of the following Locator Strategies:  Using css_selector: element = driver.find_element(By.CSS_SELECTOR, "input[value*='565657']")   Using xpath: element = driver.find_element(By.XPATH, "//input[contains(@vlaue, '565657')]")    To locate ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:  Using CSS_SELECTOR: element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[value*='565657']")))   Using XPATH: element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//input[contains(@vlaue, '565657')]")))   Note : You have to add the following imports : from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
Question: I'm building a small python package that I deploy to our internal pypi server to be easily installable with pip. I'm using setup.py to build the tar.gz archive to upload there. And I need to include some additional data - to be more specific, I use nltk in my project and I want to ship the package with specific nltk data already downloaded, since it doesn't make sense to me to make the person using my package responsible for downloading it themself. So I have the following structure ├── setup.py ├── src │ ├── __init__.py │ ├── my_pkg │ │ ├── __init__.py │ │ ├── my_module.py │ │ └── resources │ │ └── nltk_data | | └─... too many subfolders and files  And I would like to include the whole nltk_data subfolder to be in the same place once the package is installed. I managed to get working package_data={'my_pkg' :['./resources/file.dat']}, for one file, but I don't know how to do the same with complex directory structure with many subfolder, subsubfolders, file of different extensions etc. Is there any way to do this? My setup.py is quite simple (I omitted things such as description or URL for simplicity) from setuptools import setup, find_packages setup( name='some-cool-name', version="1.0.0", classifiers=[], packages=find_packages(where='src'), package_dir={'': 'src'}, package_data={'my_pkg' :[]}, include_package_data=True, py_modules=[], python_requires='>=3.8', install_requires=['nltk==3.6.5'] ) 

Answer: You can simply specify the relative path to the data you want to include. You need to put an __init__.py-file in both subfolders though, but then it should work. package_data={'my_pkg' :['my_pkg/resources/nltk_data/*']}  To use the data in your script, use importlib (for example importlib.read_text) to open your desired file.
Question: I know the function of lambda: and lambda var: , but what does lambda_: means acutally?

Answer: lambda_ is just a variable name, like any other. Like foo or x. If you saw: lambda_: Something  Then that is actually a variable annotation, for type hints, so the same as: num: int num = 0
Question: In the following code, the request has the type of <class '_pytest.fixtures.SubRequest'>. I want to add type hint to the parameter request. @pytest.fixture def dlv_service(request: SubRequest): # How to import SubRequest? print(type(request), request) filepath = pathlib.Path(request.node.fspath.strpath) f = filepath.with_name("file.json")  The following import doesn't work. from pytest.fixtures import SubRequest 

Answer: I've found one on the internet, hope this will help. from _pytest.fixtures import SubRequest  I think it's worth trying, but not sure whether it could work, sorry.
Question: Since I got the macOS v12.3 (Monterey) update (not sure it's related though), I have been getting this error when I try to run my Python code in the terminal:  I am using Python 3.10.3, Atom IDE, and run the code in the terminal via atom-python-run package (which used to work perfectly fine). The settings for the package go like this:  The which command in the terminal returns the following (which is odd, because earlier it would return something to just which python):  I gather the error occurs because the terminal calls for python instead of python3, but I am super new to any coding and have no idea why it started now and how to fix it. Nothing of these has worked for me:  I deleted and then reinstalled the Python interpreter from python.org. I tried alias python='python3' (which I saw in one of the threads here). I tried export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" (which I found here). To reset zsh and paths, I deleted all associated hidden files in /local/users/ and ran the terminal once again. I deleted everything and reinstalled Mac OS X and the Python interpreter only to get the same error. 

Answer: OK, after a couple of days trying, this is what has worked for me:  I reinstalled Monterey (not sure it was essential, but I just figured I had messed with terminal and $PATH too much). I installed python via brew rather than from the official website. It would still return command not found error. I ran echo "alias python=/usr/bin/python3" >> ~/.zshrc in terminal to alias python with python3. Relaunch the shell or run source ~/.zshrc  Problem solved. As far as I get it, there is no more pre-installed python 2.x in macOS as of 12.3 hence the error. I still find it odd though that atom-python-run would call for python instead of python3 despite the settings.
Question: What is the point of the slice.indices method, since we have the following equality? s = slice(start, stop, step) assert range(*s.indices(length)) == range(length)[s] 

Answer: Since Python 3.2 added slicing support to range, the slice.indices method is unnecessary because s.indices(length) is equal to (range(length)[s].start, range(length)[s].stop, range(length)[s].step):  range objects now support index and count methods. This is part of an effort to make more objects fully implement the collections.Sequence abstract base class. As a result, the language will have a more uniform API. In addition, range objects now support slicing and negative indices, even with values larger than sys.maxsize. This makes range more interoperable with lists: >>> range(0, 100, 2).count(10) 1 >>> range(0, 100, 2).index(10) 5 >>> range(0, 100, 2)[5] 10 >>> range(0, 100, 2)[0:5] range(0, 10, 2)  (Contributed by Daniel Stutzbach in bpo-9213, by Alexander Belopolsky in bpo-2690, and by Nick Coghlan in bpo-10889.)  The slice.indices method is only kept for backwards compatibility. The credit goes to Karl Knechtel.
Question: Say you want to wrap the dataclass decorator like so: from dataclasses import dataclass def something_else(klass): return klass def my_dataclass(klass): return something_else(dataclass(klass))  How should my_dataclass and/or something_else be annotated to indicate that the return type is a dataclass? See the following example on how the builtin @dataclass works but a custom @my_dataclass does not:  @dataclass class TestA: a: int b: str TestA(0, "") # fine @my_dataclass class TestB: a: int b: str TestB(0, "") # error: Too many arguments for "TestB" (from mypy) 

Answer: There is no feasible way to do this prior to PEP 681. A dataclass does not describe a type but a transformation. The actual effects of this cannot be expressed by Python's type system – @dataclass is handled by a MyPy Plugin which inspects the code, not just the types. This is triggered on specific decorators without understanding their implementation. dataclass_makers: Final = { 'dataclass', 'dataclasses.dataclass', }  While it is possible to provide custom MyPy plugins, this is generally out of scope for most projects. PEP 681 (Python 3.11) adds a generic "this decorator behaves like @dataclass"-marker that can be used for all transformers from annotations to fields. PEP 681 is available to earlier Python versions via typing_extensions. Enforcing dataclasses For a pure typing alternative, define your custom decorator to take a dataclass and modify it. A dataclass can be identified by its __dataclass_fields__ field. from typing import Protocol, Any, TypeVar, Type, ClassVar from dataclasses import Field class DataClass(Protocol): __dataclass_fields__: ClassVar[dict[str, Field[Any]]] DC = TypeVar("DC", bound=DataClass) def my_dataclass(klass: Type[DC]) -> Type[DC]: ...  This allows the type checker to understand and verify that a dataclass class is needed. @my_dataclass @dataclass class TestB: a: int b: str TestB(0, "") # note: Revealed type is "so_test.TestB" @my_dataclass class TestC: # error: Value of type variable "DC" of "my_dataclass" cannot be "TestC" a: int b: str  Custom dataclass-like decorators The PEP 681 dataclass_transform decorator is a marker for other decorators to show that they act "like" @dataclass. In order to match the behaviour of @dataclass, one has to use field_specifiers  to indicate that fields are denoted the same way. from typing import dataclass_transform, TypeVar, Type import dataclasses T = TypeVar("T") @dataclass_transform( field_specifiers=(dataclasses.Field, dataclasses.field), ) def my_dataclass(klass: Type[T]) -> Type[T]: return something_else(dataclasses.dataclass(klass))  It is possible for the custom dataclass decorator to take all keywords as @dataclass. dataclass_transform can be used to mark their respective defaults, even when not accepted as keywords by the decorator itself.
Question: I am running a Python code where I have to get some data from HTTPSConnectionPool(host='ssd.jpl.nasa.gov', port=443). But each time I try to run the code I get the following error. I am on MAC OS 12.1 raise SSLError(e, request=request) requests.exceptions.SSLError: HTTPSConnectionPool(host='ssd.jpl.nasa.gov', port=443): Max retries exceeded with url: /api/horizons.api?format=text&EPHEM_TYPE=OBSERVER&QUANTITIES_[...]_ (Caused by SSLError(SSLError(1, '[SSL: UNSAFE_LEGACY_RENEGOTIATION_DISABLED] unsafe legacy renegotiation disabled (_ssl.c:997)')))  I really don't know how to bypass this issue.

Answer: This error comes up when using OpenSSL 3 to connect to a server which does not support it. The solution is to downgrade the cryptography package in python: run pip install cryptography==36.0.2 in the used enviroment. source: https://github.com/scrapy/scrapy/issues/5491 EDIT: Refer to Harry Mallon and ahmkara's answer for a fix without downgrading cryptography
Question: I have a function generating a dict template. This function consists of several generators and requires one parameter (i.e., carrier) and has many optional parameters (keyword arguments - **kwargs). def main_builder(carrier, **params): output = SamplerBuilder(DEFAULT_JSON) output.generate_flight(carrier) output.generate_airline_info(carrier) output.generate_locations() output.generate_passengers() output.generate_contact_info() output.generate_payment_card_info() output.configs(**params) result = output.input_json return result # example of function call examplex = main_builder("3M", proxy="5.39.69.171:8888", card=Visa, passengers={"ADT":2, "CHD":1}, bags=2)  I want to deploy this function to FastAPI endpoint. I managed to do it for carrier but how can I set **kwargs as params to the function? @app.get("/carrier/{carrier_code}", response_class=PrettyJSONResponse) # params/kwargs?? async def get_carrier(carrier_code): output_json = main_builder(carrier_code) return airline_input_json 

Answer: Using Pydantic Model Since your function "..has many optional parameters" and passengers parameter requires a dictionary as an input, I would suggest creating a Pydantic model, where you define the parameters, and which would allow you sending the data in JSON format and getting them automatically validated by Pydantci as well. Once the endpoint is called, you can use Pydantic's dict() method (Note: In Pydantic V2, dict() was replaced by model_dump()—see this answer for more details) to convert the model into a dictionary. Example from pydantic import BaseModel from typing import Optional class MyModel(BaseModel): proxy: Optional[str] = None card: Optional[str] = None passengers: Optional[dict] = None bags: Optional[int] = None @app.post("/carrier/{carrier_code}") async def get_carrier(carrier_code: int, m: MyModel): return main_builder(carrier_code, **m.dict()) # In Pydantic V2, use **m.model_dump()  Sending arbitrary JSON data In case you had to send arbitrary JSON data, and hence, pre-defining the parameters of an endpoint wouldn't be possible, you could use an approach similar to the one described in this answer (see Options 3 and 4), as well as this answer and this answer.
Question: I have a python project with a pyproject.toml file. Typically I store the project's version number in pyproject.toml like this: % grep version pyproject.toml version = "0.0.2" %  I want to get that version number into a Makefile variable regardless of how many spaces wind up around the version terms. What should I do to extract the pyproject.toml version string into a Makefile environment variable called VERSION?

Answer: An alternative solution to parse major.minor.patch, based on @Mike Pennington's answer: grep -m 1 version pyproject.toml | grep -e '\d.\d.\d' -o
Question: I have been using the python package xgrads to parse and read a descriptor file with a suffix .ctl which describes a raw binary 3D dataset, provided by GrADS (Grid Analysis and Display System), a widely used software for easy access, manipulation, and visualization of earth science data. I have been using the following code to read the binary data into a xarray.Dataset. from xgrads import open_CtlDataset dset = open_CtlDataset('./ur2m_eta40km_2001011312.ctl') # print all the info in ctl file print(dset) <xarray.Dataset> Dimensions: (time: 553, lat: 36, lon: 30) Coordinates: * time (time) datetime64[ns] 2001-01-13T12:00:00 ... 2001-05-31T12:00:00 * lat (lat) float32 -21.2 -20.8 -20.4 -20.0 -19.6 ... -8.4 -8.0 -7.6 -7.2 * lon (lon) float32 -47.8 -47.4 -47.0 -46.6 ... -37.4 -37.0 -36.6 -36.2 Data variables: ur2m (time, lat, lon) float32 dask.array<chunksize=(1, 36, 30), meta=np.ndarray> Attributes: comment: Relative Humidity 2m storage: 99 title: File undef: 1e+20 pdef: None  This .ctl file comprises forecast results of humidity, estimated over a predefined area at each 6 hours, from 2001-01-13 12:00:00 hs to 2001-05-31 12:00:00 hs. Plotting the results for the first time step (2001-01-13T12:00:00) I got this: ds['ur2m'][0,...].plot()   I would like to know if it is possible to create tabular data from this xarray.Dataset and export it as a single .csv or .txt file, following the data structure below:  long lat ur2m time variable datetime -47.8 -21.2 0 1 ur2m 2001-01-13 12:00:00 -47.4 -21.2 0 1 ur2m 2001-01-13 12:00:00 -47.0 -21.2 0 1 ur2m 2001-01-13 12:00:00 -46.6 -21.2 0 1 ur2m 2001-01-13 12:00:00 ... ... ... ... <NA> ... <NA> -37.4 -7.2 0 553 ur2m 2001-05-31 12:00:00 -37.0 -7.2 0 553 ur2m 2001-05-31 12:00:00 -36.6 -7.2 0 553 ur2m 2001-05-31 12:00:00 -36.2 -7.2 0 553 ur2m 2001-05-31 12:00:00  The original data are available here

Answer: Try this: Convert netcdf to dataframe df = ds.to_dataframe()  Save dataframe to csv df.to_csv('df.csv')
Question: Python Version: Python 3.10.4  PIP Version: pip 22.0.4  So I was trying to make a small project with sockets, I added a feature to upload files but whenever I import requests, it throws this error. Below is the code I ran. Traceback (most recent call last): File "C:\Programming\WireUS\test.py", line 1, in <module> import requests File "C:\Users\John\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\__init__.py", line 43, in <module> import urllib3 File "C:\Users\John\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\__init__.py", line 8, in <module> from .connectionpool import ( File "C:\Users\John\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py", line 29, in <module> from .connection import ( File "C:\Users\John\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connection.py", line 39, in <module> from .util.ssl_ import ( File "C:\Users\John\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\util\__init__.py", line 3, in <module> from .connection import is_connection_dropped File "C:\Users\John\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\util\connection.py", line 3, in <module> from .wait import wait_for_read File "C:\Users\John\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\util\wait.py", line 1, in <module> from .selectors import ( File "C:\Users\John\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\util\selectors.py", line 14, in <module> from collections import namedtuple, Mapping ImportError: cannot import name 'Mapping' from 'collections' (C:\Users\John\AppData\Local\Programs\Python\Python310\lib\collections\__init__.py)  Even this basic code gives me that error. import requests import time r = request.get("google.com").text print(r) time.sleep(999) 

Answer: As user2357112-supports-monica said, running pip install urllib3 fixes it.
Question: I'm making a Discord bot to handle an announcement command. When the command is used, I want the bot to send a message in a specific channel and send a message back to the user to show that the command was sent. However, I cannot get the message to be sent to the channel. I tried this code:  import discord import os import random import asyncio testing_servers = [912361242985918464] intents = discord.Intents().all() bot = discord.Bot(intents=intents) @bot.slash_command(guild_ids=testing_servers, name="announce", description="Make server announcements!") async def announce(ctx, title, text, channel_id,anonymous=None): #response embed print(channel_id) #announcement embed embed_announce = discord.Embed( colour = discord.Colour.blue(), title=str(title), description = text ) await bot.get_channel(channel_id).send(embed = embed_announce)  But before even attempting to send the other message back to the user, I get an error that says AttributeError: 'NoneType' object has no attribute 'send'. I conclude that bot.get_channel(channel_id) evaluated to None. But why? How can I get the correct Channel to send the message?

Answer: Make sure you are sending an integer to get_channel(): await bot.get_channel(int(channel_id)).send(embed=embed_announce)
Question: When I run without debugging in python on vscode it no longer autofocuses on the terminal forcing me to click into the terminal everytime to input data. Is there any solution to cause vscode to autofocus when code is running?

Answer: The following solution to this issue has been tested on Visual Studio Code 1.74.3.  Install the Python extension for Visual Studio Code. Go to File >> Preferences >> Settings. In the Search settings field enter, "Python › Terminal: Focus After Launch" Click on, When launching a python terminal, whether to focus the cursor on the terminal. A check mark should appear in the tick box.  Done! Now every time you Run Python File the terminal will be focused.