eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: Community. I need to accept multiple comma-separated inputs to produce a summary of information ( specifically, how many different employees participated in each group/project)? The program takes employees, managers and groups in the form of strings. I'm using anytree python library to be able to search/count the occurrence of each employee per group. However, this program is only accepting one value/cell at a time instead of multiple values.  Here is the tree structure and how I accept input values? Press q to exit, Enter your data: Joe Press q to exit, Enter your data: Manager1 Press q to exit, Enter your data: Group1 Press q to exit, Enter your data: Charles Press q to exit, Enter your data: Manager1 Press q to exit, Enter your data: Group2 Press q to exit, Enter your data: Joe Press q to exit, Enter your data: Manager3 Press q to exit, Enter your data: Group1 Press q to exit, Enter your data: Charles Press q to exit, Enter your data: Manager3 Press q to exit, Enter your data: Group1 Press q to exit, Enter your data: Joe Press q to exit, Enter your data: Manager5 Press q to exit, Enter your data: Group2 Press q to exit, Enter your data: q Employee No of groups JOE 2 CHARLES 2 Group ├── GROUP1 │ ├── JOE │ │ └── MANAGER1 │ ├── JOE │ │ └── MANAGER3 │ └── CHARLES │ └── MANAGER3 └── GROUP2 ├── CHARLES │ └── MANAGER1 └── JOE └── MANAGER5  I need help with this code so that It can accept comma-separated values; for example, to enter Joe, Manager1, Group1 at a time. import anytree from anytree import Node, RenderTree, LevelOrderIter, LevelOrderGroupIter, PreOrderIter import sys # user input io='' lst_input = [] while (io!='q'): io=input('Press q to exit, Enter your data: ') if io!='q': lst_input.append(io.upper()) # change list in to matrix lst=[] for i in range(0, len(lst_input), 3): lst.append(lst_input[i:i + 3]) lst # create tree structure from lst group = Node('Group') storeGroup = {} for i in range(len(lst)): if lst[i][2] in [x.name for x in group.children]: # parent already exist, append childrens storeGroup[lst[i][0]] = Node(lst[i][0], parent=storeGroup[lst[i][2]]) storeGroup[lst[i][1]] = Node(lst[i][1], parent=storeGroup[lst[i][0]]) else: # create parent and append childreds storeGroup[lst[i][2]] = Node(lst[i][2], parent=group) storeGroup[lst[i][0]] = Node(lst[i][0], parent=storeGroup[lst[i][2]]) storeGroup[lst[i][1]] = Node(lst[i][1], parent=storeGroup[lst[i][0]]) store = {} for children in LevelOrderIter(group, maxlevel=3): if children.parent!=None and children.parent.name!='Group': if children.name not in store: store[children.name] = {children.parent.name} else: store[children.name] = store[children.name] | {children.parent.name} print('Employee', ' No of groups') for i in store: print(' '+i+' ', len(store[i])) for pre,fill, node in RenderTree(group): print('{}{}'.format(pre,node.name))   Thank you! Any thoughts are welcomed.

Answer: Leverage unpacking to extract elements. Then the if statement can be re-written this way. if io!='q': name, role, grp = io.upper(). split(',') lst_input.append([name,role, grp])  you also need to change lst.append(lst_input[i:i + 3]) in the for loop to this. lst.append(lst_input[0][i:i + 3])
Question: I can access my postgresql db inside docker container and the see the postgresql bash with this command : docker exec -it <container-name> psql -U <dataBaseUserName> <dataBaseName>  But I need to see the data I inserted to the table with the api. Is there a way to perform select statement here?

Answer: Whenever you access your container using the docker exec -it <container-name> psql -U <username> <database>  you can run any PSQL-Query you like. To list all the tables within your database you can use \dt  After you identified the table you want to query you can call any select, update or delete statement you like, e.g.: SELECT * FROM <tablename>;  See the official PostgresQL documentation for more details. However, I highly recommend using tools such as DBeaver or Database-Tools integrated within your IDE to manage your databases. Connecting your databases running in a container is as easy as running the docker exec command. But the user interface and integrated shortcuts of those tools to e.g. generate SQL-Queries makes it much more easy to access and manage your databases.
Question: I want to use a time variable instead of a CharField for a model, which displays multiple saved times. My issue is it displays as "9 am" instead of "9:00 am", and "noon" instead of "12:00 pm". Can anyone help? Thanks in advance. Relevant code below- models.py class Post(models.Model): time=models.TimeField() free=models.CharField(max_length=50, default="") player1=models.CharField(max_length=50, default="Player 1") player2=models.CharField(max_length=50, default="Player 2") player3=models.CharField(max_length=50, default="Player 3") player4=models.CharField(max_length=50, default="Player 4") def __str__(self): return self.time  HTML  <h7>{{post.time}} &nbsp; &nbsp; <a href="{% url 'update0' post.id %}" class="btn-sm btn-success" >Update</a></h7> 

Answer: You can achieve this by just using the built-in template datetime format like so: {{ post.time|date:"h:i A" }} This will display the datetime as: 09:00 AM. You can read more about this in the Django docs.
Question: following a tutorial I'm getting error "(" is not closed while using the exact same code: compiled_sol = compile_standard( { "language": "Solidity", "sources": {"SimpleStorage.sol": {"content" = simple_storage_file}} } )  don't know where it's going wrong getting these errors: "{" was not closedPylance Expected parameter namePylance  and Expected parameter namePylance 

Answer: try to replace "=" for ":" I hope this solves the problem.
Question: I have a Model Sector which has a id field (pk) which is UUID4 type. I am trying to populate that table(Sector Model) using faker and factory_boy. But, DETAIL: Key (id)=(46f0cf58-7e63-4d0b-9dff-e157261562d2) already exists.  This is the error I am getting. Is it possible that the error is due to the fact that everytime I am creating SectorFactory objects (which is in a different django app) and the seed gets reset to some previous number causing the uuid to repeat? Please suggest some ways as to how I shall get unique uuid for each Factory object? SectorFactory class import uuid from factory.django import DjangoModelFactory from factory.faker import Faker from factory import Sequence class SectorFactory(DjangoModelFactory): id = uuid.uuid4() name = Sequence(lambda n: f'Sector-{n}') class Meta: model = 'user.Sector' django_get_or_create = ['name']  Class Sector class Sector(models.Model): id = models.UUIDField(primary_key=True, default = uuid.uuid4, editable=False) name = models.CharField(max_length=100) class Meta: db_table = 'sector' constraints = [ models.UniqueConstraint('name', name = 'unique_sector_name') ]  The script which creates the custom command to create SectorFactory objects. from types import NoneType from django.core.management.base import BaseCommand from user.factories import SectorFactory class Command(BaseCommand): help = 'Generate fake data and seed the models with them.' def add_arguments(self, parser) -> None: parser.add_argument( '--amount', type=int, help='The amount of fake objects to create.' ) def _generate_sectors(self, amount): for _ in range(amount): SectorFactory() def handle(self, *args, **options) : amount = options['amount'] if(type(amount) == NoneType): amount = 10 self._generate_sectors(amount) 

Answer: just use like this: class SectorFactory(DjangoModelFactory): id = Faker('uuid4') name = Sequence(lambda n: f'Sector-{n}') class Meta: model = 'user.Sector' django_get_or_create = ['name']
Question: I triyed to execute pipenv shell in a new environtment and I got the following error: Loading .env environment variables… Creating a virtualenv for this project… Using /home/user/.pyenv/shims/python3.9 (3.9.7) to create virtualenv… ⠋ModuleNotFoundError: No module named 'virtualenv.activation.xonsh' Error while trying to remove the /home/user/.local/share/virtualenvs/7t env: No such file or directory Virtualenv location: Warning: Your Pipfile requires python_version 3.9, but you are using None (/bin/python). $ pipenv check will surely fail. Spawning environment shell (/usr/bin/zsh). Use 'exit' to leave.  I tried to remove pipenv, install python with pienv create an alias to python, but anything works. Any idea, I got the same error in existing environment, I tried to remove all environments folder but nothing. Thanks.

Answer: By github issue, the solution that works was the following: sudo apt-get remove python3-virtualenv
Question: Launching a cell will make this message appear:  Connecting to kernel: Python 3.9.6 64-bit: Activating Python Environment 'Python 3.9.6 64-bit'  This message will then stay up loading indefinitely, without anything happening. No actual error message. I've already tried searching for this problem, but every other post seem to obtain at least an error message, which isn't the case here. I still looked at some of these, which seemed to indicate the problem might have come from the traitlets package. I tried to downgrade it to what was recommended, but it didn't solve anything, so I reverted the downgrade. The main problem here is that I have no idea what could cause such a problem, without even an error message.

Answer: Not sure what did the trick but downgrading VSCode to November version and after that reinstalling Jupyter extension worked for me.
Question: While using: with open("data_file.pickle", "rb") as pfile: raw_data = pickle.load(pfile)  I get the error: AttributeError: Can't get attribute '_unpickle_block' on <module 'pandas._libs.internals' from '/opt/conda/lib/python3.8/site-packages/pandas/_libs/internals.cpython-38-x86_64-linux-gnu.so'>  Another answer to a similar question suggests checking the version of pickle I am using. It is the same on my machine, where I developed the code and on server, where I am running the code. I have searched everywhere with no answers. Please help.

Answer: I don't think the problem is pickle module but Pandas version. Your file was probably created with an older version of Pandas. Now you use a newer version, pickle can't "deserialize" the object because the API change. Try to downgrade your Pandas version and reload file. You can also try to use pd.read_pickle.
Question: I'm primarily using Windows, where I run WSL2. So from a python script running in the subsystem, I would like to screenshot whatever is on windows monitor, as simple as such: v1 import mss import os os.environ['DISPLAY'] = ':0' with mss.mss() as sct: sct.shot()  This gives only gives "Segmentation fault" error and no image. So I tried to setup vcxsrv in Windows and I'm able to open stuff from my subsystem in Windows through the server, however I cant get it the other way around.. I just want access to the windows screen so I can screenshot it. Any help about how to access the monitor through wsl would be greatly appreciated, I can't find much on google..

Answer: The problem with your attempted solution is that the WSL/Linux Python's mss, as you've found, isn't able to capture the Windows desktop. Being the Linux version of MSS, it will only be able to communicate with Linux processes and protocols like X. Starting up VcXsrv might get you part of the way there, in that you might be able to capture X output, but you may need to be running the Python app from inside a terminal that is running in a X window. Regardless, you've said that your goal is to capture the entire Windows desktop, not just the X output in VcXsrv. To do that, you'll need to use a Windows process of some sort. But don't worry; using WSL's interop, you can still do that from inside WSL/Linux Python. We just need to call out to a Windows .exe of some sort. There are literally dozens of third-party apps that you could use in Windows to create a screenshot. But I prefer to use a solution that doesn't require any additional installation. So I resorted to PowerShell for this, since you can easily call powershell.exe and pass in a script from WSL. There are a number of examples here on Stack Overflow, but I ended up going slightly "lower tech" to try to simplify a bit. The code here is most similar to this solution, so refer to that if you want to expand on this. From WSL/Linux Python: import os os.system(""" powershell.exe \" Add-Type -AssemblyName System.Windows.Forms [Windows.Forms.Sendkeys]::SendWait('+{Prtsc}') \$img = [Windows.Forms.Clipboard]::GetImage() \$img.Save(\\\"\$env:USERPROFILE\\Pictures\\Screenshots\\screenshot.jpg\\\", [Drawing.Imaging.ImageFormat]::Jpeg)\" """)  That essentially sends the ShiftPrintScreen key chord to capture the current desktop to the clipboard, then saves the clipboard. It can get slightly hairy with the quoting, since you are essentially wrapping PowerShell inside a /bin/sh inside a Python script. Note that, even though you are in Linux Python, since it's the Windows PowerShell that we are calling, it takes the Windows path format (C:\Users\Username\Pictures...) rather than the Linux version (/mnt/c/Users/...). While I didn't have any timing issues with this, you may need to insert small delays. Again, refer to the existing answer for that. This solution is primarily to explain how to do it through WSL's Python using PowerShell.
Question: I am trying to convert values to axis units. I checked codes with similar problems but none addressed this specific challenge. As can be seen in the image below, expected plot (A) was supposed to show month (Jan, Feb etc.) on the x-axis, but it was showing dates (2015-01 etc) in plot (B).  Below is the source code, kindly assist. Thanks. plt.rcParams["font.size"] = 18 plt.figure(figsize=(20,5)) plt.plot(df.air_temperature,label="Air temperature at Frankfurt Int. Airport in 2015") plt.xlim(("2015-01-01","2015-12-31")) plt.xticks(["2015-{:02d}-15".format(x) for x in range(1,13,1)],["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]) plt.legend() plt.ylabel("Temperature (°C)") plt.show() 

Answer: A wise way to draw the plot with datetime is to use datetime format in place of str; so, first of all, you should do this conversion: df = pd.read_csv(r'data/frankfurt_weather.csv') df['time'] = pd.to_datetime(df['time'], format = '%Y-%m-%d %H:%M')  Then you can set up the plot as you please, preferably following Object Oriented Interface: plt.rcParams['font.size'] = 18 fig, ax = plt.subplots(figsize = (20,5)) ax.plot(df['time'], df['air_temperature'], label = 'Air temperature at Frankfurt Int. Airport in 2015') ax.legend() ax.set_ylabel('Temperature (°C)') plt.show()  Then you can customize:  x ticks' labels format and position with matplotlib.dates: ax.xaxis.set_major_locator(md.MonthLocator(interval = 1)) ax.xaxis.set_major_formatter(md.DateFormatter('%b'))   x axis limits: ax.set_xlim([pd.to_datetime('2015-01-01', format = '%Y-%m-%d'), pd.to_datetime('2015-12-31', format = '%Y-%m-%d')])   capital first letter of x ticks' labels for months' names fig.canvas.draw() ax.set_xticklabels([month.get_text().title() for month in ax.get_xticklabels()])    Complete Code import pandas as pd import matplotlib.pyplot as plt import matplotlib.dates as md df = pd.read_csv(r'data/frankfurt_weather.csv') df['time'] = pd.to_datetime(df['time'], format = '%Y-%m-%d %H:%M') plt.rcParams['font.size'] = 18 fig, ax = plt.subplots(figsize = (20,5)) ax.plot(df['time'], df['air_temperature'], label = 'Air temperature at Frankfurt Int. Airport in 2015') ax.legend() ax.set_ylabel('Temperature (°C)') ax.xaxis.set_major_locator(md.MonthLocator(interval = 1)) ax.xaxis.set_major_formatter(md.DateFormatter('%b')) ax.set_xlim([pd.to_datetime('2015-01-01', format = '%Y-%m-%d'), pd.to_datetime('2015-12-31', format = '%Y-%m-%d')]) fig.canvas.draw() ax.set_xticklabels([month.get_text().title() for month in ax.get_xticklabels()]) plt.show()
Question: I am experimenting with different python formatters and would like to increase the max line length. Ideally without editing the settings.json file. Is there a way to achieve that? 

Answer: For all three formatters, the max line length can be increased with additional arguments passed in from settings, i.e.:  autopep8 args: --max-line-length=120 black args: --line-length=120 yapf args: --style={based_on_style: google, column_limit: 120, indent_width: 4}  Hope that helps someone in the future!
Question: I am trying to use multiprocessing.Pool to run my code in parallel. To instantiate Pool, you have to set the number of processes. I am trying to figure out how many I should set for this. I understand this number shouldn't be more than the number of cores you have but I've seen different ways to determine what your system has available. 2 Methods:  multiprocessing.cpu_count() len(os.sched_getaffinity(0))  I'm a little confused; what is the difference between the two and which should be implemented with Pool? I am working on a remote cluster, with the first, it outputs that there are 128 cpu, but the second gives 10.

Answer: The difference between the two is clearly stated in the doc:  multiprocessing.cpu_count() Return the number of CPUs in the system. This number is not equivalent to the number of CPUs the current process can use. The number of usable CPUs can be obtained with len(os.sched_getaffinity(0)).  So even if you are on a 128-core system, your program could have been somehow limited to only run on a specific set of 10 out of the 128 available CPUs. Since affinity also applies to child threads and processes, it doesn't make much sense to spawn more than 10. You could however try to increase the number of available CPUs through os.sched_setaffinity() before starting your pool. import os import multiprocessing as mp cpu_count = mp.cpu_count() if len(os.sched_getaffinity(0)) < cpu_count: try: os.sched_setaffinity(0, range(cpu_count)) except OSError: print('Could not set affinity') n = max(len(os.sched_getaffinity(0)), 96) print('Using', n, 'processes for the pool') pool = mp.Pool(n) # ...  See also man 2 sched_setaffinity.
Question: In Pandas 1.1.4, I am receiving a DataError: No numeric types to aggregate when using an ExpandingGroupby. Example dataset: tmp = pd.DataFrame({'col1':['a','b','b','c','d','d'], 'col2': ['red','red','green','green','red','blue']}) print(tmp) col1 col2 a red b red b green c green d red d blue  This works: tmp.groupby('col1').agg(lambda x: ','.join(x))  And this works: tmp.groupby('col1').expanding().agg('count')  But this returns an error: tmp.groupby('col1').expanding().agg(lambda x: ','.join(x))  DataError: No numeric types to aggregate There is no conceptual reason this shouldn't work, and there are several references online to people using custom functions within an ExpandingGroupby. There is obviously no reason that this should need to be a numeric, especially given that the count works with the non-numeric column. What is happening here? If this can't be done natively for whatever reason, how can I do it manually?

Answer: You can use accumulate from itertools module: from itertools import accumulate concat = lambda *args: ','.join(args) expand = lambda x: list(accumulate(x, func=concat)) df['col3'] = df.groupby('col1')['col2'].transform(expand) print(df) # Output col1 col2 col3 0 a red red 1 b red red 2 b green red,green 3 c green green 4 d red red 5 d blue red,blue  Update One line version: df['col3'] = df.groupby('col1')['col2'].transform(lambda x: list(accumulate(x, func=lambda *args: ','.join(args))))
Question: According to here pipeline provides an interface to save a pretrained pipeline locally with a save_pretrained method. When I use it, I see a folder created with a bunch of json and bin files presumably for the tokenizer and the model. But the documentation does not specify a load method. How does one initialize a pipeline using a locally saved pipeline?

Answer: Apparently the default initialization works with local folders as well. So one can download a model like this: pipe = pipeline("text-classification") pipe.save_pretrained("my_local_path")  And later load it like pipe = pipeline("text-classification", model = "my_local_path")
Question: While trying to load my webpage on the browser, I got the message.  A server error occurred. Please contact the administrator  And when I go back to check my termimal, I see the message  zoneinfo._common.ZoneInfoNotFoundError: 'No time zone found with key UTC'  I have checked but don't know what's wrong. I even tried changing the UTC to my original timezone, but it still didn't work. P.S: I started getting this error after I tried working on templates inheritance Here's what my settings.py file looks like INSTALLED_APPS = [ 'newapp.apps.NewappConfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] ROOT_URLCONF = 'new.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] WSGI_APPLICATION = 'new.wsgi.application' # Database # https://docs.djangoproject.com/en/4.0/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3', } } # Password validation # https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators AUTH_PASSWORD_VALIDATORS = [ { 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', }, { 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', }, { 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', }, { 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', }, ] # Internationalization # https://docs.djangoproject.com/en/4.0/topics/i18n/ LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/4.0/howto/static-files/ STATIC_URL = 'static/' # Default primary key field type # https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' 

Answer: Add tzdata to your requirements or pip install tzdata
Question: I'm building an ETL pipeline with Apache Airflow. I have to extract the latest data added to a SQL database (say daily). Therefore, I want to construct a query as follows: SELECT foo FROM bar WHERE insert_date >= "DATA_INTERVAL_START_HERE" AND insert_date < "DATA_INTERVAL_END_HERE"  To execute this query in a task (with e.g. pyodbc), I need to access the data interval start and end time of the Dag Run object inside the extract task. How can I retrieve this information?

Answer: A similar question was asked here: Airflow ETL pipeline - using schedule date in functions? However, the answer is not updated to the TaskFlow API since Airflow 2.0. A concise way to access the data interval parameters: @dag(schedule_interval="@daily", start_date=datetime(2022, 2, 8), catchup=True) def tutorial_access_data_interval(): @task() def extract(data_interval_start=None, data_interval_end=None, **kwargs): #Use data_interval_start, data_interval_end here  The Airflow engine will provide the parameters by default. References: https://airflow.apache.org/docs/apache-airflow/stable/howto/operator/python.html https://airflow.apache.org/docs/apache-airflow/stable/templates-ref.html#variables