eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I have a list of numbers which all correspond to items of different weight: weights = [50, 40, 30, 100, 150, 12, 150, 10, 5, 4]  I need to split the values into two bins with the caveat that the bin sum total cannot exceed 300. e.g. The simplest one I can think of is: bin1 = [150, 150] = 300 bin2 = [50, 40, 30, 100, 12, 10, 5, 4] = 251  I want to be able to get all the combinations of these weights that would satisfy this caveat, unsure how to go about this?

Answer: one way is brute-forcing it by binning all possible permutations of the list there has to be a better (more clever) way of doing that - it's terribly slow. (but I'm supposed to be doing other things right now ;-)) from itertools import permutations max_bin_size = 300 weights = [50, 40, 30, 100, 150, 12, 150, 10, 5, 4] bin_combinations = set() def to_bins(weights): bin = [] for weight in weights: if weight > max_bin_size: raise ValueError("wtf?") if sum(bin) + weight > max_bin_size: yield tuple(sorted(bin)) bin = [weight] else: bin.append(weight) yield tuple(sorted(bin)) for permutation in set(permutations(weights)): bin_combinations.add(tuple(sorted(to_bins(permutation)))) for combination in bin_combinations: print(combination) # ((4, 10, 40, 100), (5, 12, 150), (30, 50, 150)) # ((4, 5, 10, 40, 50, 100), (12, 30), (150, 150)) # ((4, 10, 30, 150), (5, 50, 100), (12, 40, 150)) # ((4, 5, 30, 100), (10, 50, 150), (12, 40, 150)) # ((4, 12, 50, 100), (5, 10, 30, 40), (150, 150)) # ((4, 5, 150), (10, 30, 40, 150), (12, 50, 100)) # ...  Update: a version with reuse of bins (assuming one can put the weights freely into any available bin): from itertools import permutations max_bin_size = 300 weights = [50, 40, 30, 100, 150, 12, 150, 10, 5, 4] bin_combinations = set() def to_bins(weights): bins = [[]] for weight in weights: if weight > max_bin_size: raise ValueError("wtf?") for bin in bins: if sum(bin) + weight > max_bin_size: continue bin.append(weight) break else: bins.append([weight]) return tuple(sorted(tuple(sorted(bin)) for bin in bins)) for permutation in set(permutations(weights)): bin_combinations.add(to_bins(permutation)) print(len(bin_combinations), "combinations") for combination in bin_combinations: print(combination) # 14 combinations # ((4, 5, 10, 12, 30, 50, 150), (40, 100, 150)) # ((4, 5, 10, 30, 40, 50, 150), (12, 100, 150)) # ((4, 12, 30, 100, 150), (5, 10, 40, 50, 150)) # ((4, 100, 150), (5, 10, 12, 30, 40, 50, 150)) # ((4, 5, 12, 30, 50, 150), (10, 40, 100, 150)) # ((4, 5, 10, 12, 100, 150), (30, 40, 50, 150)) # ((4, 5, 12, 30, 40, 50, 150), (10, 100, 150)) # ((4, 5, 40, 100, 150), (10, 12, 30, 50, 150)) # ((4, 10, 12, 30, 40, 50, 150), (5, 100, 150)) # ((4, 5, 10, 30, 100, 150), (12, 40, 50, 150)) # ((4, 5, 10, 12, 30, 40, 150), (50, 100, 150)) # ((4, 10, 40, 50, 150), (5, 12, 30, 100, 150)) # ((4, 5, 10, 12, 40, 50, 150), (30, 100, 150)) # ((4, 5, 10, 12, 30, 40, 50, 100), (150, 150))
Question: I'm trying to convert my Discord Bot commands to hybrid commands. When I don't use the hybrid_command decorator, the slash commands work. The error says the callback must be a coroutine. What does it mean? What am I missing in the code? main.py class MyBot(commands.Bot): def __init__(self): intents=discord.Intents.all() intents.message_content = True super().__init__( command_prefix='!', intents=discord.Intents.all() ) self.initial_extensions = [ "cogs.user_commands" ] async def setup_hook(self): for ext in self.initial_extensions: await self.load_extension(ext) await bot.tree.sync(guild = discord.Object(id=191453821174531128))  user_commands.py class user_commands(commands.Cog): def __init__(self, bot): self.bot = bot bot.remove_command("help") @commands.hybrid_command(name='help', with_app_command=True) @app_commands.command() async def help(self, interaction: discord.Interaction, command: Optional[str]): ...... await interaction.response.send_message(embed=embed, ephemeral = True) @help.autocomplete('command') async def help_autocomplete(self, interaction: discord.Interaction, current: str, ) -> List[app_commands.Choice[str]]: .....  Error: 

Answer: First of all, your error means that decorators @commands.hybrid_command(...) and @app_commands.command() do not go well together. You either define hybrid command, slash command or text-chat command. Next moment, hybrid commands have commands.Context as their argument so we need to replace interaction parameter with that and adjust your ...... code accordingly -> replace usage of interaction with its analogical attributes/methods from ctx of said commands.Context class, i.e. replace  interaction.user with ctx.author interaction.channel with ctx.channel interaction.response.send_message with ctx.send or ctx.reply etc  So your code in user_commands.py would look like this. @commands.hybrid_command(name='help', with_app_command=True) async def help(self, ctx: commands.Context, command: Optional[str]): ...... await ctx.send(embed=embed, ephemeral=True) @help.autocomplete('command') async def help_autocomplete(self, interaction: discord.Interaction, current: str, ) -> List[app_commands.Choice[str]]: .....  PS. it's not advisable to remove help command. I suggest reading this amazing github gist A basic walkthrough guide on subclassing HelpCommand
Question: I want to check if some string value exists in the values set of some Enum. Here is what I do: from enum import Enum class Color(str, Enum): RED = "red" GREEN = "green" YELLOW = "yellow" s = "red" # s = "blue" if any(s == c.value for c in Color): print(Color(s))  When I checked the documentation I found that:  The EnumMeta metaclass is responsible for providing the contains(), dir(), iter() and other methods that allow one to do things with an Enum class that fail on a typical class, such as list(Color) or some_enum_var in Color  But I want something different (checking existence for values). Is there a more pythonic way to solve this?

Answer: You can test values against _value2member_map_, a dict attribute of the Enum sub-class that maps values to member classes, if you prefer not to clutter up your code with a try-except block: if s in Color._value2member_map_: print(Color(s))
Question: Other IDE's like PyCharm, IntelliJ, etc. have a feature where if it finds a function being called that is undefined, you can right-click it and click 'create method' or something similar to automatically create the function definition. It helps out a lot in TDD. Is there something similar in VS Code?

Answer: You can install the My Code Actions extension, here is a simple example: Configure in settings.json file: // settings.json file { "my-code-actions.actions": { "[python]": { "create new methond {{diag:$1}}": { "diagnostics": ["\"(.*?)\" is not defined"], "text": "def {{diag:$1}}():\n pass\n", "where": "afterLast", } } } }  The effect when writing code after saving this setting:    Another method that might be useful: open in sequence File > Preferences > Configure User Snippets. Select python in the command palette. This will create a python.json file in which you can customize the code segment according to the rules. A simple example:  "Print to console":{ "prefix": "defHello", "body": [ "def Hello():", "${1: }print('Hello worle')", "$2", ], "description": "print hello world" }
Question: I have a column (DATE) with multiple data times and I want to find the difference in minutes from date to date and store it into a new column (time_interval). This is what I have tried: df['time_interval'] = (df['DATE'],axis=0 - df['DATE'],axis=1) * 24 * 60  

Answer: Depending on how you'd care to store the differences, either df = pd.DataFrame(data=['01-01-2006 00:53:00', '01-01-2006 01:53:00', '01-01-2006 02:53:00', '01-01-2006 03:53:00', '01-01-2006 04:53:00'], columns=['DATE']) df['DATE'] = pd.to_datetime(df['DATE']) df['time_interval'] = df['DATE'].diff().fillna(timedelta(0)).apply(lambda x: x.total_seconds() / 60)  to get  DATE time_interval 0 2006-01-01 00:53:00 0.0 1 2006-01-01 01:53:00 60.0 2 2006-01-01 02:53:00 60.0 3 2006-01-01 03:53:00 60.0 4 2006-01-01 04:53:00 60.0  or alternatively df['time_interval'] = df['DATE'].diff().shift(-1).fillna(timedelta(0)).apply(lambda x: x.total_seconds() / 60)  to get  DATE time_interval 0 2006-01-01 00:53:00 60.0 1 2006-01-01 01:53:00 60.0 2 2006-01-01 02:53:00 60.0 3 2006-01-01 03:53:00 60.0 4 2006-01-01 04:53:00 0.0
Question: I have a python script using pyodbc that connects to a remote server with sql server running on it. I have a package I wrote with functions using sqlalchemy that I was able to use on one of my computers. I connected with this string: driver = 'SQL+Server+Native+Client+11.0' engine_string = prefix + '://' + username + ':' + password + '@' + server + '/' + database + '?driver=' + driver  On another computer, I was not able to install the native client 11.0 which I understand is deprecated. I tried switching the value to driver = 'ODBC+Driver+18+for+SQL+Server'  I got an error with that version [ODBC Driver 18 for SQL Server]SSL Provider: The certificate chain was issued by an authority that is not trusted.  I then tried just a generic odbc connection with the windows utility and got the same error. I was able to get that odbc manager connection to work when I checked 'Trust Server Certificate' This is probably not good long term, but is there a way to add that attribute to the first string I have above? I tried several variations, but nothing worked. I was able to get a working connection with the following: cnxn = pyodbc.connect( driver = '{ODBC Driver 18 for SQL Server}', server = server, database = database, uid = username, pwd = password, encrypt='no', trust_server_certificate='yes')  but that connection did not work with the package I wanted to use. thanks!

Answer: The connection error is due to a change in default behavior for the newest versions of SQL Server Drivers (ODBC v18+, JDBC v10+, .Net Microsoft.Data.SqlClient v4.0+). ODBC release notes: https://techcommunity.microsoft.com/t5/sql-server-blog/odbc-driver-18-0-for-sql-server-released/ba-p/3169228 The correct ODBC keyword to use is TrustServerCertificate https://learn.microsoft.com/en-us/sql/connect/odbc/dsn-connection-string-attribute?view=sql-server-ver16
Question: Hoping someone on here is kind enough to at least point me in the right direction. Overall, I'm trying to match regex for each row and produce the below output (in 'desired example output'). To elaborate, data is being matched from a 'Device Pool' column from a rather large CSV (all settings from a phone). I need to:  input only the regexp match in the Device Pool column/row and still have it corresponding to the Directory Number 1 data. I may add other columns later Also strip the D in the regexp as well, as it is only useful for the initial lookup.  Example input data(humongous file with lots of columns): ... ,Device Pool,CSS,forward 1,Directory Number 1, ... YART01-432-D098-00-1,CSS-bobville-1,12223041234,12228675309 BART-1435-C512-00-1,CSS-willis-3,12223041234,12228215486 HOMER-1435-A134-00-1,CSS-willis-2,12223041238,12228212345 VAR05-1435-D099-00-1,CSS-willis-2,12223041897,12228215486 POA01-4-D100-00-1,CSS-hmbrgr-chz,12223043151,12228843454 ...  Tried a few different approaches to no avail. with findall, I'd have to add the other columns back I guess (doesn't seem very efficient). It was pulling the data but not the other associated columns pertaining to the row. I since dropped that direction. Surely there is a cleaner way, that might even drop the need to filter first. This is where I'm at: df1 = pd.read.csv(some_file.csv) dff1 = df1.filter(items=['Device Pool', 'Directory Number 1'])) df2 = dff1.loc[d1.iloc[:,0].str.contains('[D][0-9][0-9][0-9]', regex=True)] dff2 = # stuck here  current example output: Device Pool Directory Number 1 YART01-432-D098-00-1 12228675309 VAR05-1435-D099-00-1 12228215486 POA01-4-D100-00-1 12228843454 ...  desired example output: Device Pool Directory Number 1 098 12228675309 099 12228215486 100 12228843454 ...  I'll be using these trimmed numbers to reference an address code csv, then pulling coordinates from geo location code, to then map. Pretty fun project really.

Answer: You can use df['Device Pool'] = df['Device Pool'].str.replace(r'.*-D(\d+).*', r'\1', regex=True)  Or, with Series.str.extract: df['Device Pool'] = df['Device Pool'].str.extract(r'-D(\d+)', expand=False)  See a Pandas test: import pandas as pd df = pd.DataFrame({'Device Pool':['YART01-432-D098-00-1', 'VAR05-1435-D099-00-1', 'POA01-4-D100-00-1'], 'Directory Number 1':['12228675309', '12228215486', '12228843454']}) df['Device Pool'].str.replace(r'.*-D(\d+).*', r'\1', regex=True) >>> df Device Pool Directory Number 1 0 098 12228675309 1 099 12228215486 2 100 12228843454  The .*-D(\d+).* regex matches  .* - any zero or more chars other than line break chars as many as possible -D - a -D string (\d+) - Group 1: one or more digits .* - the rest of the line.
Question: I am looking to do something like in this thread. However, I only want to subtract the time component of the two datetime columns. For eg., given this dataframe:  ts1 ts2 0 2018-07-25 11:14:00 2018-07-27 12:14:00 1 2018-08-26 11:15:00 2018-09-24 10:15:00 2 2018-07-29 11:17:00 2018-07-22 11:00:00  The expected output for ts2 -ts1 time component only should give:  ts1 ts2 ts_delta 0 2018-07-25 11:14:00 2018-07-27 12:14:00 1:00:00 1 2018-08-26 11:15:00 2018-09-24 10:15:00 -1:00:00 2 2018-07-29 11:17:00 2018-07-22 11:00:00 -0:17:00  So, for row 0: the time for ts2 is 12:14:00, the time for ts1 is 11:14:00. The expected output is just these two times subtracting (don't care about the days). In this case: 12:14:00 - 11:14:00 = 1:00:00. How would I do this in one single line?

Answer: You need to set both datetimes to a common date first. One way is to use pandas.DateOffset: o = pd.DateOffset(day=1, month=1, year=2022) # the exact numbers don't matter # reset dates ts1 = df['ts1'].add(o) ts2 = df['ts2'].add(o) # subtract df['ts_delta'] = ts2.sub(ts1)  As one-liner: df['ts_delta'] = df['ts2'].add((o:=pd.DateOffset(day=1, month=1, year=2022))).sub(df['ts1'].add(o))  Other way using a difference between ts2-ts1 (with dates) and ts2-ts1 (dates only): df['ts_delta'] = (df['ts2'].sub(df['ts1']) -df['ts2'].dt.normalize().sub(df['ts1'].dt.normalize()) )  output:  ts1 ts2 ts_delta 0 2018-07-25 11:14:00 2018-07-27 12:14:00 0 days 01:00:00 1 2018-08-26 11:15:00 2018-09-24 10:15:00 -1 days +23:00:00 2 2018-07-29 11:17:00 2018-07-22 11:00:00 -1 days +23:43:00  NB. don't get confused by the -1 days +23:00:00, this is actually the ways to represent -1hour
Question: I was following this tutorial and it was going pretty well. He then introduced reqparse and I followed along. I tried to test my code and I get this error {'message': "Did not attempt to load JSON data because the request Content-Type was not 'application/json'."} I don't know if I'm missing something super obvious but I'm pretty sure I copied his code exactly. here's the code: main.py from flask import Flask, request from flask_restful import Api, Resource, reqparse app = Flask(__name__) api = Api(app) #basic get and post names = {"sai": {"age": 19, "gender": "male"}, "bill": {"age": 23, "gender": "male"}} class HelloWorld(Resource): def get(self, name, numb): return names[name] def post(self): return {"data": "Posted"} api.add_resource(HelloWorld, "/helloworld/<string:name>/<int:numb>") # getting larger data pictures = {} class picture(Resource): def get(self, picture_id): return pictures[picture_id] def put(self, picture_id): print(request.form['likes']) pass api.add_resource(picture, "/picture/<int:picture_id>") # reqparse video_put_args = reqparse.RequestParser() # make new request parser object to make sure it fits the correct guidelines video_put_args.add_argument("name", type=str, help="Name of the video") video_put_args.add_argument("views", type=int, help="Views on the video") video_put_args.add_argument("likes", type=int, help="Likes on the video") videos = {} class Video(Resource): def get(self, video_id): return videos[video_id] def post(self, video_id): args = video_put_args.parse_args() print(request.form['likes']) return {video_id: args} api.add_resource(Video, "/video/<int:video_id>") if __name__ == "__main__": app.run(debug=True)  test_rest.py import requests BASE = "http://127.0.0.1:5000/" response = requests.post(BASE + 'video/1', {"likes": 10}) print(response.json()) 

Answer: I don't know why you have an issue as far as I can tell you did copy him exactly how he did it. Here's a fix that'll work although I can't explain why his code works and yours doesn't. His video is two years old so it could be deprecated behaviour. import requests import json BASE = "http://127.0.0.1:5000/" payload = {"likes": 10} headers = {'accept': 'application/json'} response = requests.post(BASE + 'video/1', json=payload) print(response.json())
Question: I'm trying to install Apache Flink with either python3 -m pip install apache-flink or pip3 install apache-flink, but both fail with an exit code 1 error: clang: error: the clang compiler does not support 'faltivec', please use -maltivec and include altivec.h explicitly error: Command "clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -iwithsysroot/System/Library/Frameworks/System.framework/PrivateHeaders -iwithsysroot/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/Headers -arch arm64 -arch x86_64 -Werror=implicit-function-declaration -DNPY_INTERNAL_BUILD=1 -DHAVE_NPY_CONFIG_H=1 -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE=1 -D_LARGEFILE64_SOURCE=1 -DNO_ATLAS_INFO=3 -DHAVE_CBLAS -Ibuild/src.macosx-10.14-arm64-3.8/numpy/core/src/umath -Ibuild/src.macosx-10.14-arm64-3.8/numpy/core/src/npymath -Ibuild/src.macosx-10.14-arm64-3.8/numpy/core/src/common -Inumpy/core/include -Ibuild/src.macosx-10.14-arm64-3.8/numpy/core/include/numpy -Inumpy/core/src/common -Inumpy/core/src -Inumpy/core -Inumpy/core/src/npymath -Inumpy/core/src/multiarray -Inumpy/core/src/umath -Inumpy/core/src/npysort -I/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/include/python3.8 -Ibuild/src.macosx-10.14-arm64-3.8/numpy/core/src/common -Ibuild/src.macosx-10.14-arm64-3.8/numpy/core/src/npymath -Ibuild/src.macosx-10.14-arm64-3.8/numpy/core/src/common -Ibuild/src.macosx-10.14-arm64-3.8/numpy/core/src/npymath -c numpy/core/src/multiarray/alloc.c -o build/temp.macosx-10.14-arm64-3.8/numpy/core/src/multiarray/alloc.o -MMD -MF build/temp.macosx-10.14-arm64-3.8/numpy/core/src/multiarray/alloc.o.d -faltivec -I/System/Library/Frameworks/vecLib.framework/Headers" failed with exit status 1 [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: legacy-install-failure × Encountered error while trying to install package. ╰─> numpy note: This is an issue with the package mentioned above, not pip.  The output is quite long so I won't post the whole thing here, but if there's something in particular I should be looking for in there, any suggestions are welcome. I've also tried starting with numpy already installed in a hope that apache-flink would just use the installed version but that didn't help. pip3 --version -> pip 22.1.2 from /Users/sophier/Library/Python/3.8/lib/python/site-packages/pip (python 3.8) I'm on the new mac with the M1 chip incase that could be a problem.

Answer: PyFlink on a M1 is not yet supported but will be from Flink 1.16 onwards, see https://issues.apache.org/jira/browse/FLINK-25188
Question: I have two dataframes with identical columns, but different values and different number of rows. import pandas as pd data1 = {'Region': ['Africa','Africa','Africa','Africa','Africa','Africa','Africa','Africa','Asia','Asia','Asia','Asia'], 'Country': ['South Africa','South Africa','South Africa','South Africa','South Africa','South Africa','South Africa','South Africa','Japan','Japan','Japan','Japan'], 'Product': ['ABC','ABC','ABC','ABC','XYZ','XYZ','XYZ','XYZ','DEF','DEF','DEF','DEF'], 'Year': [2016, 2017, 2018, 2019,2016, 2017, 2018, 2019,2016, 2017, 2018, 2019], 'Price': [500, 400, 0,450,750,0,0,890,500,470,0,415]} data1 = {'Region': ['Africa','Africa','Africa','Africa','Africa','Africa','Asia','Asia'], 'Country': ['South Africa','South Africa','South Africa','South Africa','South Africa','South Africa','Japan','Japan'], 'Product': ['ABC','ABC','ABC','ABC','XYZ','XYZ','DEF','DEF'], 'Year': [2016, 2017, 2018, 2019,2016, 2017,2016, 2017], 'Price': [200, 100, 30,750,350,120,400,370]} df = pd.DataFrame(data1) df2 = pd.DataFrame(data2)  df is the complete dataset but with some old values, whereas df2 only has the updated values. I want to replace all the values that are in df with the values in df2, all while keeping the values from df that aren't in df2. So for example, in df, the value for Country = Japan, for Product = DEF, in Year = 2016, the Price should be updated from 470 to 400. The same for 2017, while 2018 and 2019 stay the same. So far I have the following code that doesn't seem to work: common_index = ['Region','Country','Product','Year'] df = df.set_index(common_index) df2 = df2.set_index(common_index) df.update(df2, overwrite = True)  But this only updates df with the values from df2 and deletes everything else. Expected output should look like this: data3 = {'Region': ['Africa','Africa','Africa','Africa','Africa','Africa','Africa','Africa','Asia','Asia','Asia','Asia'], 'Country': ['South Africa','South Africa','South Africa','South Africa','South Africa','South Africa','South Africa','South Africa','Japan','Japan','Japan','Japan'], 'Product': ['ABC','ABC','ABC','ABC','XYZ','XYZ','XYZ','XYZ','DEF','DEF','DEF','DEF'], 'Year': [2016, 2017, 2018, 2019,2016, 2017, 2018, 2019,2016, 2017, 2018, 2019], 'Price': [200, 100, 30,750,350,120,0,890,400,370,0,415]} df3 = pd.DataFrame(data3)  Any suggestions on how I can do this?

Answer: You can use merge and update: df.update(df.merge(df2, on=['Region', 'Country', 'Product', 'Year'], how='left', suffixes=('_old', None)))  NB. the update is in place. output:  Region Country Product Year Price 0 Africa South Africa ABC 2016 200.0 1 Africa South Africa ABC 2017 100.0 2 Africa South Africa ABC 2018 30.0 3 Africa South Africa ABC 2019 750.0 4 Africa South Africa XYZ 2016 350.0 5 Africa South Africa XYZ 2017 120.0 6 Africa South Africa XYZ 2018 0.0 7 Africa South Africa XYZ 2019 890.0 8 Asia Japan DEF 2016 400.0 9 Asia Japan DEF 2017 370.0 10 Asia Japan DEF 2018 0.0 11 Asia Japan DEF 2019 415.0
Question: I have created 3 different dictionary in python , however I believe this cannot be merged into 1 dictionary e.g. NewDict due to a same Key in all 3 e.g. Name & Company. NewDict1 = {'Name': 'John,Davies', 'Company': 'Google'} NewDict2 = {'Name': 'Boris,Barry', 'Company': 'Microsoft'} NewDict3 = {'Name': 'Humphrey,Smith', 'Company': 'Microsoft'}  I would like to group above in such a way that my output is as below : Google : John Davies Microsoft : Boris Barry, Humphrey Smith Any help will be really appreciated .

Answer: Use a defaultdict: from collections import defaultdict dicts = [NewDict1, NewDict2, NewDict3] out = defaultdict(list) for d in dicts: out[d['Company']].append(d['Name']) dict(out)  output: {'Google': ['John,Davies'], 'Microsoft': ['Boris,Barry', 'Humphrey,Smith']} as printed string for k,v in out.items(): print(f'{k}: {", ".join(v)}')  output: Google: John,Davies Microsoft: Boris,Barry, Humphrey,Smith