eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I am reading the book 'Django for APIs' from 'William S. Vincent' (current edition for Django 4.0) In chapter 4, I cannot run successfully the command python manage.py collectstatic. I get the following error: Traceback (most recent call last): File "/Users/my_name/Projects/django/django_for_apis/library/manage.py", line 22, in <module> main() File "/Users/my_name/Projects/django/django_for_apis/library/manage.py", line 18, in main execute_from_command_line(sys.argv) File "/Users/my_name/Projects/django/django_for_apis/library/.venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line utility.execute() File "/Users/my_name/Projects/django/django_for_apis/library/.venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 440, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/Users/my_name/Projects/django/django_for_apis/library/.venv/lib/python3.10/site-packages/django/core/management/base.py", line 402, in run_from_argv self.execute(*args, **cmd_options) File "/Users/my_name/Projects/django/django_for_apis/library/.venv/lib/python3.10/site-packages/django/core/management/base.py", line 448, in execute output = self.handle(*args, **options) File "/Users/my_name/Projects/django/django_for_apis/library/.venv/lib/python3.10/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 209, in handle collected = self.collect() File "/Users/my_name/Projects/django/django_for_apis/library/.venv/lib/python3.10/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 154, in collect raise processed whitenoise.storage.MissingFileError: The file 'rest_framework/css/bootstrap.min.css.map' could not be found with <whitenoise.storage.CompressedManifestStaticFilesStorage object at 0x102fa07f0>. The CSS file 'rest_framework/css/bootstrap.min.css' references a file which could not be found: rest_framework/css/bootstrap.min.css.map Please check the URL references in this CSS file, particularly any relative paths which might be pointing to the wrong location.  I have the exact same settings like in the book in settings.py: STATIC_URL = "static/" STATICFILES_DIRS = [BASE_DIR / "static"] # new STATIC_ROOT = BASE_DIR / "staticfiles" # new STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage" # new  I couldn't find any explanation for it. maybe someone can point me in the right direction.

Answer: Update: DRF 3.14.0 now supports Django 4.1. If you've added stubs to static as per below, be sure to remove them. This appears to be related to Django 4.1: either downgrade to Django 4.0 or simply create the following empty files in one of your static directories: static/rest_framework/css/bootstrap-theme.min.css.map static/rest_framework/css/bootstrap.min.css.map  There's a recent change to ManifestStaticFilesStorage where it now attempts to replace source maps with their hashed counterparts. Django REST framework has only recently added the bootstrap css source maps but is not yet released.
Question: Suppose the following documentation structure for sphinx: doc |_ _static |_ _templates |_ api |_ index.rst |_ classes.rst |_ functions.rst |_ index.rst |_ more_functions.rst |_ conf.py  And that classes.rst, functions.rst and more_functions.rst have classes and functions to auto-document with autodoc/autosummary. The build will generate .rst files for those classes and functions in:  doc/generated for more_functions.rst doc/api/generated for classes.rst and functions.rst  Is there a way to control where those generated folders are created?  I'm trying to get a unique generated folder in the end. In this case, with this structure: doc |_ generated |_ generated-from-more-functions.rst |_ api |_ generated-from-api/classes.rst |_ generated-from-api/functions-rst 

Answer: I don't think there is such native functionality in sphinx. The fastest way to achieve this without many headaches is to create a shell script to run the build and then move (with mv or rm if you're on gnu/linux) the files according to your needs.
Question: I am trying to create a rudimentary scheduling system. Here is what I have so far: I have a pandas dataframe job_data that looks like this:     wc job start duration     1 J1 2022-08-16 07:30:00 17   1 J2 2022-08-16 07:30:00 5   2 J3 2022-08-16 07:30:00 21   2 J4 2022-08-16 07:30:00 12     It contains a wc (work center), job, a start date and duration for the job in hours. I have created a function add_hours that takes the following arguments: start (datetime), hours (int). It calculates the when the job will be complete based on the start time and duration. The code for add_hours is: def is_in_open_hours(dt): return ( dt.weekday() in business_hours["weekdays"] and dt.date() not in holidays and business_hours["from"].hour <= dt.time().hour < business_hours["to"].hour ) def get_next_open_datetime(dt): while True: dt = dt + timedelta(days=1) if dt.weekday() in business_hours["weekdays"] and dt.date() not in holidays: dt = datetime.combine(dt.date(), business_hours["from"]) return dt def add_hours(dt, hours): while hours != 0: if is_in_open_hours(dt): dt = dt + timedelta(hours=1) hours = hours - 1 else: dt = get_next_open_datetime(dt) return dt  The code to calculate the end column is: df["end"] = df.apply(lambda x: add_hours(x.start, x.duration), axis=1)  The result of function is the end column:     wc job start duration end     1 J1 2022-08-16 07:30:00 17 2022-08-17 14:00:00   1 J2 2022-08-16 07:30:00 5 2022-08-17 10:00:00   2 J3 2022-08-16 07:30:00 21 2022-08-18 08:00:00   2 J4 2022-08-16 07:30:00 12 2022-08-18 08:00:00     Problem is, I need the start datetime in the second row to be the end datetime from the previous row instead of them all using the same start date. I also need to start this process over for each wc. So the desired output would be:     wc job start duration end     1 J1 2022-08-16 07:30:00 17 2022-08-17 14:00:00   1 J2 2022-08-17 14:00:00 5 2022-08-17 19:00:00   2 J3 2022-08-16 07:30:00 21 2022-08-18 08:00:00   2 J4 2022-08-18 08:00:00 10 2022-08-18 18:00:00    

Answer: I show an alternative method where you only need the first start date and then bootstrap the lists according to the job durations.  # import required modules import io import pandas as pd from datetime import datetime from datetime import timedelta # make a dataframe # note: only the first start date is required x = ''' wc job start duration end 1 J1 2022-08-16 07:30:00 17 2022-08-17 14:00:00 1 J2 2022-08-16 07:30:00 5 2022-08-17 10:00:00 2 J3 2022-08-16 07:30:00 21 2022-08-18 08:00:00 2 J4 2022-08-16 07:30:00 12 2022-08-18 08:00:00 ''' data = io.StringIO(x) df = pd.read_csv(data, sep='\t') # construct start and end lists start = datetime.strptime(df['start'][0], '%Y-%m-%d %H:%M:%S') start_list = [start] end_list = [] for x in df['duration']: time_change = timedelta(hours=float(x)) new_time = start_list[-1] + time_change start_list.append(new_time) end_list.append(new_time) start_list.pop(-1) # add to dataframe df['start'] = start_list df['end'] = end_list # finished df  The result is this:
Question: Can someone help me? I keep getting this error message when I try to start up my discord bot. [2022-08-23 14:32:12] [WARNING ] discord.ext.commands.bot: Privileged message content intent is missing, commands may not work as expected.  This is the code for the bot and after this is just commands and events and client.run(My_Token) import os import random import discord from discord.ext import commands from discord.ext import tasks from discord.ext.commands import has_permissions, MissingPermissions from discord.utils import get from itertools import cycle import json import random intents = discord.Intents.default() intents.members = True intents.typing = True intents.presences = True client = commands.Bot(command_prefix = "?", intents=intents) client.remove_command('help') status = cycle(["Minecraft", "Roblox", "Yo-Kai Watch"]) 

Answer: You've got to change intents = discord.Intents.default()  to intents = discord.Intents.all()  It was an unmentioned change in the v2.0 discord.py update. https://discordpy.readthedocs.io/en/latest/migrating.html
Question:  The official documentation shows how to create a spreadsheet, but I can't find how to create a sheet. How do I do it in Python?

Answer: @PCDSandwichMan's answer uses gspread, which is a very useful third-party library to simplify the Sheets API in Python. Not all of Google's APIs have libraries like this, though, so you may want to learn the regular way as well. As an alternative in case that you want to use Google's API you can check out the documentation for Google's Python API libraries. Most direct changes to a spreadsheet's properties are done with spreadsheets().batchUpdate(). Here's a sample based on Google's Python Quickstart that adds a new sheet. import os.path from google.auth.transport.requests import Request from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build from googleapiclient.errors import HttpError SCOPES = ['https://www.googleapis.com/auth/spreadsheets'] # The ID of the spreadsheet YOUR_SPREADSHEET = 'some-id' def main(): creds = None if os.path.exists('token.json'): creds = Credentials.from_authorized_user_file('token.json', SCOPES) if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file( 'credentials.json', SCOPES) creds = flow.run_local_server(port=0) with open('token.json', 'w') as token: token.write(creds.to_json()) try: sheetservice = build('sheets', 'v4', credentials=creds) body = { "requests":{ "addSheet":{ "properties":{ "title":"New Sheet" } } } } sheetservice.spreadsheets().batchUpdate(spreadsheetId=YOUR_SPREADSHEET, body=body).execute() except HttpError as err: print(err)  Most of it is the authorization. The relevant part is within the try block. You pretty much just call the batchUpdate() method with the spreadsheet's ID and a body object with all the requests you want to make. sheetservice = build('sheets', 'v4', credentials=creds) body = { "requests":[{ "addSheet":{ "properties":{ "title":"New Sheet" } } }] } sheetservice.spreadsheets().batchUpdate(spreadsheetId=YOUR_SPREADSHEET, body=body).execute()  Sources:  Google Sheets API Python docs General info on how to use batchUpdate Google Python Quickstart
Question: I have a base permission class that two ViewSets are sharing and one other permission class each that is custom to each of the ViewSets, so 3 permissions all together, is there a way to pass a specific variable down from the base permission class to the other permission classes? My setup looks like this: class BasePerm(permissions.BasePermission): def has_permission(self, request, view): some_var = # call an API using request variable class Perm1(permissions.BasePermission): def has_permission(self, request, view): # get the value of some_var from BasePerm class Perm2(permissions.BasePermission): def has_permission(self, request, view): # get the value of some_var from BasePerm class MyViewSet1(mixins.CreateModelMixin, viewsets.GenericViewSet): permission_classes = [BasePerm, Perm1] class MyViewSet2(mixins.CreateModelMixin, viewsets.GenericViewSet): permission_classes = [BasePerm, Perm2] 

Answer: i don't understand why you don't use mixin. For you ask: class BasePerm(permissions.BasePermission): def has_permission(self, request, view): self.some_var = # call an API using request variable return True class Perm1(BasePerm): def has_permission(self, request, view): # get the value of some_var from BasePerm return super().has_permission(request, view) and some_staff_with(self.some_var) class Perm2(BasePerm): def has_permission(self, request, view): # get the value of some_var from BasePerm return super().has_permission(request, view) and some_other_staff_with(self.some_var) class MyViewSet1(mixins.CreateModelMixin, viewsets.GenericViewSet): permission_classes = [Perm1] class MyViewSet2(mixins.CreateModelMixin, viewsets.GenericViewSet): permission_classes = [Perm2]
Question: Running pip freeze in the terminal throws the following error (full traceback): PS C:\Users\lhott> pip freeze ERROR: Exception: Traceback (most recent call last): File "C:\Users\lhott\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\cli\base_command.py", line 167, in exc_logging_wrapper status = run_func(*args) File "C:\Users\lhott\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\commands\freeze.py", line 87, in run for line in freeze( File "C:\Users\lhott\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\operations\freeze.py", line 43, in freeze req = FrozenRequirement.from_dist(dist) File "C:\Users\lhott\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\operations\freeze.py", line 237, in from_dist req, comments = _get_editable_info(dist) File "C:\Users\lhott\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\operations\freeze.py", line 164, in _get_editable_info vcs_backend = vcs.get_backend_for_dir(location) File "C:\Users\lhott\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\vcs\versioncontrol.py", line 238, in get_backend_for_dir repo_path = vcs_backend.get_repository_root(location) File "C:\Users\lhott\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\vcs\git.py", line 501, in get_repository_root r = cls.run_command( File "C:\Users\lhott\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\vcs\versioncontrol.py", line 650, in run_command return call_subprocess( File "C:\Users\lhott\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\utils\subprocess.py", line 141, in call_subprocess proc = subprocess.Popen( File "C:\Users\lhott\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 966, in __init__ self._execute_child(args, executable, preexec_fn, close_fds, File "C:\Users\lhott\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 1435, in _execute_child hp, ht, pid, tid = _winapi.CreateProcess(executable, args, NotADirectoryError: [WinError 267] The directory name is invalid  I have Python 3.10.2. pip freeze worked perfectly fine until today and I don't understand why. I have updated it recently but I don't know why that would've caused that. I also can install packages without a problem with pip install. Example:  Solutions tried:  I have tried restarting my laptop. Running the terminal with administrators privileges. 

Answer: I actualy found the answer. @Greg7000 saying Maybe one of your dependency is badly installed actually gave me a hint. I had a dependency installed (package of a friend) that I uninstalled manually by pressing delete on the corresponding folder instead of doing pip uninstall. This is likely to have created the error "directory name is invalid". Using pip uninstall even after deleting the corresponding folder manually worked and fixed the problem.
Question: Consider the following abhorrent class: class MapInt: __call__ = int def __sub__(self, other): return map(self, other) __add__ = map  One can then call map(int, lst) via MapInt() - lst, i.e. assert list(MapInt() - ['1','2','3'])) == [1,2,3] # passes  However, addition is not so cooperative: assert list(MapInt() + ['1','2','3'])) == [1,2,3] # TypeError: map() must have at least two arguments.  This strangeness can be resolve by invoking the magic method directly: assert list(MapInt.__add__(MapInt(), ['1','2','3'])) == [1,2,3] # passes assert list(MapInt().__add__(MapInt(), ['1','2','3'])) == [1,2,3] # passes  So my question is, what gives? Assigning __add__ directly seems to "discard" the self argument, but invoking the method itself or defining it in the standard way works fine.

Answer: The transformation of instance methods is described in the Python Data Model (emphasis mine):  Note that the transformation from function object to instance method object happens each time the attribute is retrieved from the instance [...] Also notice that this transformation only happens for user-defined functions; other callable objects (and all non-callable objects) are retrieved without transformation.  Since map is a built-in, not a user-defined function, there is no transformation to an instance method, so the self argument is not added.
Question: Shortened example: vals1 = [(1, "a"), (2, "b"), ] columns1 = ["id","name"] df1 = spark.createDataFrame(data=vals1, schema=columns1) vals2 = [(1, "k"), ] columns2 = ["id","name"] df2 = spark.createDataFrame(data=vals2, schema=columns2) df1 = df1.alias('df1').join(df2.alias('df2'), 'id', 'full') df1.show()  The result has one column named id and two columns named name. How do I rename the columns with duplicate names, assuming that the real dataframes have tens of such columns?

Answer: Another method to rename only the intersecting columns from typing import List from pyspark.sql import DataFrame def join_intersect(df_left: DataFrame, df_right: DataFrame, join_cols: List[str], how: str = 'inner'): intersected_cols = set(df1.columns).intersection(set(df2.columns)) cols_to_rename = [c for c in intersected_cols if c not in join_cols] for c in cols_to_rename: df_left = df_left.withColumnRenamed(c, f"{c}__1") df_right = df_right.withColumnRenamed(c, f"{c}__2") return df_left.join(df_right, on=join_cols, how=how) vals1 = [(1, "a"), (2, "b")] columns1 = ["id", "name"] df1 = spark.createDataFrame(data=vals1, schema=columns1) vals2 = [(1, "k")] columns2 = ["id", "name"] df2 = spark.createDataFrame(data=vals2, schema=columns2) df_joined = join_intersect(df1, df2, ['name']) df_joined.show()
Question: I'm writing a script in python 3 to automatically rename files. But I have a problem with the captured group in a regex. I have these kinds of files : test tome 01 something.cbz test tome 2 something.cbz test tome 20 something.cbz  And I would like to have : test 001 something.cbz test 002 something.cbz test 020 something.cbz  I tried several bits of code: Example 1: name = re.sub('tome [0]{0,1}(\d{1,})', str('\\1').zfill(3), name)  The result is: test 01 something.cbz test 02 something.cbz test 020 something.cbz  Example 2: name = re.sub('tome (\d{1,})', str('\\1').lstrip("0").zfill(3), name)  The result is: test 001 something.cbz test 02 something.cbz test 020 something.cbz 

Answer: You can run the zfill(3) on the .group(1) value after stripping the zeroes from the left side: import re s = ("test tome 01 something.cbz\n" "test tome 2 something.cbz\n" "test tome 20 something.cbz") result = re.sub( r'tome (\d+)', lambda x: x.group(1).lstrip("0").zfill(3), s ) print(result)  Output test 001 something.cbz test 002 something.cbz test 020 something.cbz
Question: I am new in python i have Anaconda Pyton 3.9 I was studying about Multiprocessing. When i try this code from multiprocessing import Process # gerekli kütüphaneyi çağıracağız. import time def subfunc1(): time.sleep(2) print("subfunc1: Baslatildi") time.sleep(2) print("subfunc1: Sonlandi") time.sleep(2) def subfunc2(): time.sleep(2) print("subfunc2: Baslatildi") time.sleep(2) print("subfunc2: Sonlandi") time.sleep(2) def mainfunc(): print("mainfunc: Baslatildi") pr1 = Process(target=subfunc1) pr2 = Process(target=subfunc2) pr1.start() pr2.start() print("mainfunc: Sonlandi") if __name__ == '__main__': # Main kod bloğunun içerisindeyken main fonk çağır! mainfunc() result is mainfunc: Baslatildi mainfunc: Sonlandi  When i use Visual Code with Python 3.9 i have a virtual and code works! Visual Code uses Anaconda's python 3.9 within a virtual env! Could you please help me? Whey this code can't work properly in Jupyter Notebook? Thanks

Answer: Am I correct in assuming you are running this on ms-windows or macOS? In that case, multiprocessing will not work in an interactive interpreter like IPython. This is covered in the documentation, see the "note":  Functionality within this package requires that the __main__ module be importable by the children. This is covered in Programming guidelines however it is worth pointing out here. This means that some examples, such as the multiprocessing.pool.Pool examples will not work in the interactive interpreter.  This is caused by the spawn start method used on these operating systems. One possible fix is to save you code in a script, and to make sure that creating multiprocessing objects is done within the __main__-block. Another is in the comment by Aaron below
Question: What strings are valid metrics with keras.model.compile? The following works, model.compile(optimizer='sgd', loss='mse', metrics=['acc'])  but this does not work, model.compile(optimizer='sgd', loss='mse', metrics=['recall', 'precision']) 

Answer: Check method to check metrices. Check docstring for details
Question: This is the code I'm using in Google Colab import re from textblob import TextBlob import emoji def clean_tweet(text): text = re.sub(r'@[A-Za-z0-9]+', '', str(text)) # remove @mentions text = re.sub(r'#', '', str(text)) # remove the '#' symbol text = re.sub(r'RT[\s]+', '', str(text)) # remove RT text = re.sub(r'https?\/\/S+', '', str(text)) # remove the hyperlink text = re.sub(r'http\S+', '', str(text)) # remove the hyperlink text = re.sub(r'www\S+', '', str(text)) # remove the www text = re.sub(r'twitter+', '', str(text)) # remove the twitter text = re.sub(r'pic+', '', str(text)) # remove the pic text = re.sub(r'com', '', str(text)) # remove the com return text def remove_emoji(text): return emoji.get_emoji_regexp().sub(u'', text)  When I make these calls tweets['cleaned_text']=tweets['text'].apply(clean_tweet) tweets['cleaned_text']=tweets['cleaned_text'].apply(remove_emoji)  I'm getting the below error AttributeError Traceback (most recent call last) <ipython-input-20-9fe71f3cdb0c> in <module> 1 tweets['cleaned_text']=tweets['text'].apply(clean_tweet) ----> 2 tweets['cleaned_text']=tweets['cleaned_text'].apply(remove_emoji) 4 frames <ipython-input-19-8c0d6ba00a5b> in remove_emoji(text) 24 25 def remove_emoji(text): ---> 26 return emoji.get_emoji_regexp().sub(u'', text) AttributeError: module 'emoji' has no attribute 'get_emoji_regexp'  This is very strange. I have never seen this issue before. Could someone help me with this? Am I doing something wrong here?

Answer: AttributeError: module 'emoji' has no attribute 'get_emoji_regexp' - get_emoji_regexp method was deprecated and subsequently removed in new versions of the package.