eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: aStr = input("Enter a string message: ") fW = eval(input("Enter a positive integer: ")) print(f"Right aligned is <{aStr:>fW}>")  so, this is my code, I would like to align the spaces according to the user input. Whenever I try to use eval or int in fW, it shows this error. Traceback (most recent call last): File "C:\Users\vmabr\Downloads\A1Q2.py", line 5, in <module> print(f"Right aligned is <{aStr:fW}>") ValueError: Invalid format specifier  May I know what's the fix?

Answer: You were close. To implement a "parameterized" format-string, you need to wrap the parameter fW into additional curly braces: aStr = input("Enter a string message: ") fW = int(input("Enter a positive integer: ")) print(f"Right aligned is <{aStr:>{fW}}>") # {fW} instead of fW  See PEP 498 -> format specifiers for further information.
Question: I'm trying to create a text-to-speech Python program. I already have it working in English, though, I need other languages too. How can I use the same methods for other languages like Chinese? coding: from gtts import gTTS import os myText = "hello" language = 'en' output = gTTS(text=myText, lang = language, slow = False) output.save("output.mp3") os.system(" start output.mp3") 

Answer: To get all the languages supported by the library you are using use the following: import gtts.lang print(gtts.lang.tts_langs())  In this output, the keys are what you would use and the values just explain what language it is. And to answer your question 'zh-CN': 'Chinese', 'zh-TW': 'Chinese (Mandarin/Taiwan)', 'zh': 'Chinese (Mandarin)' are all possible versions of chinese. Output: {'af': 'Afrikaans', 'ar': 'Arabic', 'bg': 'Bulgarian', 'bn': 'Bengali', 'bs': 'Bosnian', 'ca': 'Catalan', 'cs': 'Czech', 'cy': 'Welsh', 'da': 'Danish', 'de': 'German', 'el': 'Greek', 'en': 'English', 'eo': 'Esperanto', 'es': 'Spanish', 'et': 'Estonian', 'fi': 'Finnish', 'fr': 'French', 'gu': 'Gujarati', 'hi': 'Hindi', 'hr': 'Croatian', 'hu': 'Hungarian', 'hy': 'Armenian', 'id': 'Indonesian', 'is': 'Icelandic', 'it': 'Italian', 'iw': 'Hebrew', 'ja': 'Japanese', 'jw': 'Javanese', 'km': 'Khmer', 'kn': 'Kannada', 'ko': 'Korean', 'la': 'Latin', 'lv': 'Latvian', 'mk': 'Macedonian', 'ms': 'Malay', 'ml': 'Malayalam', 'mr': 'Marathi', 'my': 'Myanmar (Burmese)', 'ne': 'Nepali', 'nl': 'Dutch', 'no': 'Norwegian', 'pl': 'Polish', 'pt': 'Portuguese', 'ro': 'Romanian', 'ru': 'Russian', 'si': 'Sinhala', 'sk': 'Slovak', 'sq': 'Albanian', 'sr': 'Serbian', 'su': 'Sundanese', 'sv': 'Swedish', 'sw': 'Swahili', 'ta': 'Tamil', 'te': 'Telugu', 'th': 'Thai', 'tl': 'Filipino', 'tr': 'Turkish', 'uk': 'Ukrainian', 'ur': 'Urdu', 'vi': 'Vietnamese', 'zh-CN': 'Chinese', 'zh-TW': 'Chinese (Mandarin/Taiwan)', 'zh': 'Chinese (Mandarin)'}
Question: I have a python workflow in github here. I can run that workflow from the web interface. But problem is everytime I have to visit github and click on run workflow. Again I have to login in github from an unknown device to run that workflow. Is there any github module in python that can run a workflow using a personal access token? Or how can I run a workflow with github api using requests module? I searched about this topic on google but I didn’t found any solution that works. Many of those are out dated or not explained properly. This is the workflow I used here: name: Python on: workflow_dispatch: jobs: build: runs-on: ubuntu-latest steps: - name: checkout repo uses: actions/checkout@v2.3.4 - name: Run a script run: python3 main.py 

Answer: Thanks to @C.Nivs & @larsks for helping me to find the right documentation. After experimenting with github api, finally I found my answer. Here is the code I used: branch, owner, repo, workflow_name, ghp_token="main", "dev-zarir", "Python-Workflow", "python.yml", "ghp_..." import requests def run_workflow(branch, owner, repo, workflow_name, ghp_token): url = f"https://api.github.com/repos/{owner}/{repo}/actions/workflows/{workflow_name}/dispatches" headers = { "Accept": "application/vnd.github+json", "Authorization": f"Bearer {ghp_token}", "Content-Type": "application/json" } data = '{"ref":"'+branch+'"}' resp = requests.post(url, headers=headers, data=data) return resp response=run_workflow(branch, owner, repo, workflow_name, ghp_token) if response.status_code==204: print("Workflow Triggered!") else: print("Something went wrong.")
Question: Identify an unkown feature On a previous VSCode installation (OSX Vscode 1.7.x), a feature was enabled that auto suggested the 3rd party dependencies in the pip requirements.txt file. I am on a fresh install of VSCode(1.71.2 linux) and have both  Microsoft Python(v2022.16.1) Pylance(v2022.10.20)  extensions installed, but the feature of "auto suggest latest versions" is not showing/working. My google-foo is failing to locate either what "extension" provided that feature, or if already installed, how to install it. I don't know what it is called. It "behaves" like an auto suggest above the 3rdparty==1.2.3 library entry. and also marks ones with CVE's in red. It always auto-detected requirements.txt and requirements-*.txt files. Any help is much appreciated.

Answer: It was called PyPi Assistant. I found that by searching in VSCode with @popular pip
Question: I am using a python snowflake connector from the following package: snowflake-sqlalchemy from sqlalchemy import create_engine engine = create_engine(...)  It used to be working but it's now failing with this weird error. I tried to switch to older version of the package, but can't get rid of this error. Here is the fulll stack trace:  File "/Users/Etienne.Herlaut/Repositories/python/performance-hub-apps/utils/snowflake.py", line 19, in get_snowflake_connection engine = create_engine(f'snowflake://{sf_user}:{sf_password}@{sf_account}?warehouse={wh}&role={role}', connect_args={'timeout': 120}) File "<string>", line 2, in create_engine File "/Users/Etienne.Herlaut/.pyenv/versions/3.8.13/lib/python3.8/site-packages/sqlalchemy/util/deprecations.py", line 309, in warned return fn(*args, **kwargs) File "/Users/Etienne.Herlaut/.pyenv/versions/3.8.13/lib/python3.8/site-packages/sqlalchemy/engine/create.py", line 522, in create_engine entrypoint = u._get_entrypoint() File "/Users/Etienne.Herlaut/.pyenv/versions/3.8.13/lib/python3.8/site-packages/sqlalchemy/engine/url.py", line 656, in _get_entrypoint cls = registry.load(name) File "/Users/Etienne.Herlaut/.pyenv/versions/3.8.13/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py", line 341, in load return impl.load() File "/Users/Etienne.Herlaut/.pyenv/versions/3.8.13/lib/python3.8/site-packages/importlib_metadata/__init__.py", line 207, in load module = import_module(match.group('module')) File "/Users/Etienne.Herlaut/.pyenv/versions/3.8.13/lib/python3.8/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1014, in _gcd_import File "<frozen importlib._bootstrap>", line 991, in _find_and_load File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 671, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 843, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "/Users/Etienne.Herlaut/.pyenv/versions/3.8.13/lib/python3.8/site-packages/snowflake/sqlalchemy/__init__.py", line 63, in <module> from .util import _url as URL File "/Users/Etienne.Herlaut/.pyenv/versions/3.8.13/lib/python3.8/site-packages/snowflake/sqlalchemy/util.py", line 8, in <module> from sqlalchemy.engine.url import _rfc_1738_quote ImportError: cannot import name '_rfc_1738_quote' from 'sqlalchemy.engine.url' (/Users/Etienne.Herlaut/.pyenv/versions/3.8.13/lib/python3.8/site-packages/sqlalchemy/engine/url.py) 

Answer: Looks like this is a known issue will get resolved in v1.4.3, per the release notes: https://github.com/snowflakedb/snowflake-sqlalchemy/blob/main/DESCRIPTION.md
Question: Starting from a dataframe like the below (simplified example of my real case): import pandas as pd df = pd.DataFrame({ 'a': [1.0, 1.1, 1.0, 4.2, 5.1], 'b': [5.0, 4.2, 3.1, 3.2, 4.1], 'c': [3.9, 2.0, 4.2, 3.8, 6.7], 'd': [3.1, 2.1, 1.2, 1.0, 1.0] })  And then taking a dictionary containing some multipliers I want to multiply certain columns in the dataframe by: dict = { "b": 0.01, "d": 0.001 }  i.e. I want to check if each column in the dataframe is in my dictionary, and if it does exist as a key, then multiply that column of the dataframe by the value in the dictionary. In this example, I would want to multiply column 'b' by 0.01 and column 'd' by 0.001. I would end up with:  'a': [1.0, 1.1, 1.0, 4.2, 5.1], 'b': [0.05, 0.042, 0.031, 0.032, 0.041], 'c': [3.9, 2.0, 4.2, 3.8, 6.7], 'd': [0.0031, 0.0021, 0.0012, 0.001, 0.001]  In my real example, the dataframe is a cleaned-up set of data read in from Excel, and the dictionary of multipliers is read in from a config file, to allow users to specify which columns need converting from whatever is in Excel to the desired/expected units of measure (e.g. converting 'g/h' in the raw data to 'kg/h' in the dataframe). What are some good, clear ways of achieving this intent, even if I have to restructure the implementation a bit?

Answer: Try: df[list(dct)] *= dct.values() print(df)  Prints:  a b c d 0 1.0 0.050 3.9 0.0031 1 1.1 0.042 2.0 0.0021 2 1.0 0.031 4.2 0.0012 3 4.2 0.032 3.8 0.0010 4 5.1 0.041 6.7 0.0010   If in dct are keys not in dataframe: tmp = {k: dct[k] for k in dct.keys() & df.columns} df[list(tmp)] *= tmp.values()
Question: I am aware of the use of __all__ at module scope. However I came across the usage of __all__ inside classes. This is done e.g. in the Python standardlib: class re(metaclass=_DeprecatedType): """Wrapper namespace for re type aliases.""" __all__ = ['Pattern', 'Match'] Pattern = Pattern Match = Match  What does __all__ achieve in this context?

Answer: The typing module does some unorthodox things to patch existing modules (like re). Basically, the built-in module re is being replaced with this class re defined using a custom metaclass that intercepts attribute lookups on the underlying object. __all__ doesn't really have any special meaning to the class (it's just another class attribute), but it effectively becomes the __all__ attribute of the re module. It's the metaclass's definition of __getattribute__ that accomplishes this.
Question: I am using mkdocstrings in order automatically generate an API documentation from my Python functions. At the same time I am using flake8 to keep my code in good shape. If you want to ignore some flake8 warnings on an in-line basis, you could insert "# noqa" whereby the following lines of code will be ignored by flake8. That's nice, however, "# noqa" will be interpreted by mkdocstrings as a markdown header. Now, I am wondering how to resolve that conflict between flake8 and mkdocstrings?

Answer: put the noqa comment on the end of the docstring -- it will apply to any line within the docstring without changing the string's contents (note: you need a sufficiently new flake8, this change is relatively recent (probably >=4.x)) def f(): """some docstring here something which causes a warning """ # noqa: ABC123   disclaimer: I am the current flake8 maintainer
Question: I want to measure the RAM usage of each for loop in my code. I searched internet and find process = psutil.Process(os.getpid()) and print(process.memory_info().rss) for measuring RAM. But this code gets the pid of the whole process and not a specific part. Is there any way to measure RAM usage of each part of the code? For example in code below we have 3 for loops which fill 3 different dictionaries. I want to print RAM usage of each for loop and between the processing of each loop, if the RAM exceed a threshold, i want to break that for loop. dict1 = {} dict2 = {} dict3 = {} for i in range (200): do something with dict1 if RAM usage of this block exceeds 1GB then break this loop used: x Mb for i in range (500): do something with dict2 if RAM usage of this block exceeds 1GB then break this loop used: x2 Mb for i in range (800): do something with dict3 if RAM usage of this block exceeds 1GB then break this loop used: x3 Mb  I appreciate answers which can help me a lot

Answer: You can read memory usage just before loop and then read it again inside loop. Then you can calculate loop memory usage as a difference between these two values. If it exceeds some threshold, break the loop. Here is sample code: import numpy as np import psutil import os process = psutil.Process(os.getpid()) a = [] threshhold = 64*1024*1024 base_memory_usage = process.memory_info().rss for i in range(10): memory_usage = process.memory_info().rss loop_memory_usage = memory_usage - base_memory_usage print(loop_memory_usage) if loop_memory_usage > threshhold: print('exceeded threshold') break a.append(np.random.random((1000, 1000)))  Result: 0 8028160 16031744 24035328 32038912 40042496 48046080 56049664 64053248 72056832 exceeded threshold  As you can see, before any action the loop uses 0 bytes of memory.
Question: I have two dataframes df1 and df2 df1 =     University School Student first name last name nick name     AAA Law John Mckenzie Stevie   BBB Business Steve Savannah JO   CCC Engineering Mark Justice Fre   DDD Arts Stuart Little Rah   EEE Life science Adam Johnson meh     120 rows X 5 columns df2 =     Statement     Stuart had a headache last nigh which was due to th……   Rah basically found a new found friend which lead to the……   Gerome got a brand new watch which was……….   Adam was found chilling all through out his life……   Savannah is such a common name that……..     3000 rows X1 columns AIM is to form df3 Match the string literal and iterate it through every cells in the columns "Student first name" , "Student last name" , "Student nick name" to produce the table below Df3 =     Statement Matching University School     Stuart had a headache last nigh which was due to th… Stuart DDD Arts   Rah basically found a new found friend which lead to Rah DDD Arts   Gerome got a brand new watch which was………. NA NA NA   Adam was found chilling all through out his life…… Adam EEE Life science   Savannah is such a common name that…….. Savannah BBB Business     3000 rows X 4 columns

Answer: You can melt and merge: import re df1_melt = df1.melt(['University', 'School'], value_name='Match') regex = '|'.join(map(re.escape, df1_melt['Match'])) out = df2.join( df1_melt[['Match', 'University', 'School']] .merge(df2['Statement'] .str.extract(f'({regex})', expand=False) .rename('Match'), how='right', on='Match' ) )  output:  Statement Match University School 0 Stuart had a headache last nigh which was due to the Stuart DDD Arts 1 Rah basically found a new found friend which lead to the Rah DDD Arts 2 Gerome got a brand new watch which was NaN NaN NaN 3 Adam was found chilling all through out his life Adam EEE Life science 4 Savannah is such a common name that Savannah BBB Business
Question: I have read through the pandas guide, especially merge and join sections, but still can not figure it out. Basically, this is what I want to do: Let's say we have two data frames: left = pd.DataFrame( { "key": ["K0", "K1", "K2", "K3"], "A": ["A0", "A1", "A2", "A3"], "C": ["B0", "B1", np.nan, np.nan]}) right = pd.DataFrame( { "key": ["K2"], "A": ["A8"], "D": ["D3"]})  I want to merge them based off on "key" and update the values, filling where necessary and replacing old values if there are any. So it should look like this:  key A C D 0 K0 A0 B0 NaN 1 K1 A1 B1 NaN 2 K2 A8 NaN D3 3 K3 A3 NaN NaN 

Answer: You can use combine_first with set_index to accomplish your goal here. right.set_index('key').combine_first(left.set_index('key')).reset_index()  Output:  key A C D 0 K0 A0 B0 NaN 1 K1 A1 B1 NaN 2 K2 A8 NaN D3 3 K3 A3 NaN NaN
Question: Can someone please show me how to merge df2 to df1 for just the matching cities, then use the df2's average monthly temperature columns to match to each city's date range (according to the month) into a new column called 'Temp' in df1? These are sample data of much larger files for state and cities in Brazil. df1  State City Dates 0 AC Rio Branco 3/20/2020 1 BA Salvador 5/2/2020 2 CE Fortaleza 4/6/2020 3 AC Rio Branco 5/30/2020  df2: has average monthly temperatures for each city.  State City MAR APR MAY 0 CE Fortaleza 75.6 72.7 69.4 1 ES Vitória 69.1 64.6 62.7 2 AC Rio Branco 72.8 70.5 68.9 3 BA Salvador 74.6 71.3 70.1  Desired output: df1 with new column 'Temp'  State City Dates Temp 0 AC Rio Branco 3/20/2020 72.8 1 BA Salvador 5/2/2020 70.1 2 CE Fortaleza 4/6/2020 72.7 3 AC Rio Branco 5/30/2020 68.9 

Answer: You can use a merge after reshaping df2 to long form with melt and extracting the month abbreviation with to_datetime and strftime: (df1.assign(month=pd.to_datetime(df1['Dates']).dt.strftime('%b').str.upper()) .merge(df2.melt(['State', 'City'], var_name='month', value_name='Temp'), on=['State', 'City', 'month']) #.drop(columns='month') # uncomment to remove the column )  output:  State City Dates month Temp 0 AC Rio Branco 3/20/2020 MAR 72.8 1 BA Salvador 5/2/2020 MAY 70.1 2 CE Fortaleza 4/6/2020 APR 72.7 3 AC Rio Branco 5/30/2020 MAY 68.9
Question: If using the following DataFrame I can split the "ccy" string and create two new columns: df_so = pd.DataFrame.from_dict({0: 'gbp_usd', 1: 'eur_usd', 2: 'usd_cad', 3: 'usd_jpy', 4: 'eur_usd', 5: 'eur_usd'},orient='index',columns=["ccy"]) df_so[['base_ccy', 'quote_ccy']] = df_so['ccy'].str.split('_', 1, expand=True)  giving the following DataFrame.     index ccy base_ccy quote_ccy     0 gbp_usd gbp usd   1 eur_usd eur usd   2 usd_cad usd cad   3 usd_jpy usd jpy   4 eur_usd eur usd   5 eur_usd eur usd     How do I do the same str.split using DataFrame.assign within my tweak function below ? I can do this with a list comprehension to get the same result, but is there a simpler/cleaner way using assign?: def tweak_df (df_): return (df_.assign(base_currency= lambda df_: [i[0] for i in df_['ccy'].str.split('_', 1)], quote_currency= lambda df_: [i[1] for i in df_['ccy'].str.split('_', 1)], ) ) tweak_df(df_so)  Yields same result as the table above but the code is not very intuitive and simple is better than complex.

Answer: I actually think the first version you suggested is the best. df_so[['base_ccy', 'quote_ccy']] = df_so['ccy'].str.split('_', 1, expand=True)  If you want to do it using assign, you can do it like this utilising the rename function. df_so.assign(**df_so['ccy'].str.split('_', n=1, expand=True) .rename(columns={0: "base_ccy", 1: "quote_ccy"}))
Question: When I'm trying to run my project in PyCharm I'm getting an error:  SDK is not defined for Run Configuration.  I tried to set a new interpreter and tried everything. What does "SDK" mean and where can I configure it? 

Answer: I just had this same issue (see my comment above). What worked for me was to go into "Edit Configurations", delete the configuration that was copied over from the original PC, and create my own configuration (basically with the same inputs as before).
Question: I have a dataframe like this #Test dataframe import pandas as pd import numpy as np #Build df titles = {'Title': ['title1', 'cat', 'dog']} references = {'References': [['donkey','chicken'],['title1','dog'],['bird','snake']]} df = pd.DataFrame({'Title': ['title1', 'cat', 'dog'], 'References': [['donkey','chicken'],['title1','dog'],['bird','snake']]}) #Insert IDs for UNIQUE titles title_ids = {'IDs':list(np.arange(0,len(df)) + 1)} df['IDs'] = list(np.arange(0,len(df)) + 1) df = df[['Title','IDs','References']]   and I want to generate IDs for the references column that looks like the data frame below. If there is a matching between the strings, assign the same ID as in the IDs column and if not, assign a new unique ID.  My first attempt is using the function #Matching function def string_match(string1,string2): if string1 == string2: a = 1 else: a = 0 return a  and to loop over each string/title combination but this gets tricky with multiple for loops and if statements. Is there a better way I can do this that is more pythonic?

Answer: # Explode to one reference per row references = df["References"].explode() # Combine existing titles with new title from References titles = pd.concat([df["Title"], references]).unique() # Assign each title an index number mappings = {t: i + 1 for i, t in enumerate(titles)} # Map the reference to the index number and convert to list df["RefIDs"] = references.map(mappings).groupby(level=0).apply(list)
Question: I have a dockerfile FROM python:3.9.12-bullseye COPY . . RUN apt-get update -y RUN apt-get install cron -y RUN crontab crontab CMD python task.py && crond -f  And a crontab * * * * * python /task.py  I keep running into the error /bin/sh: 1: crond: not found when I run the docker file. Docker build is fine. Anyone knows why this happens? If I use python:3.6.12-alpine everything works fine but with python:3.9.12-bullseye, i keep getting that error.

Answer: If you have a look for debian series cron.service, you could see next: [Unit] Description=Regular background program processing daemon Documentation=man:cron(8) After=remote-fs.target nss-user-lookup.target [Service] EnvironmentFile=-/etc/default/cron ExecStart=/usr/sbin/cron -f $EXTRA_OPTS IgnoreSIGPIPE=false KillMode=process Restart=on-failure [Install] WantedBy=multi-user.target  From ExecStart=/usr/sbin/cron -f $EXTRA_OPTS, I guess unlike alpine, the main program on such debian series linux could be cron not crond. (PS: python:3.9.12-bullseye based on debian, while python:3.6.12-alpine based on alpine)
Question: Say I have a json file with lines of data like this : file.json : {'ID':'098656', 'query':'query_file.txt'} {'A':1, 'B':2} {'A':3, 'B':6} {'A':0, 'B':4} ...  where the first line is just explanations about the given file and how it was created. I would like to open it with something like : import pandas as pd df = pd.read_json('file.json', lines=True)  However, how do I read the data starting on line 3 ? I know that pd.read_csv has a skiprows argument, but it does not look like pd.read_json has one. I would like something returning a DataFrame with the columns A and B only, and possibly better than dropping the first line and ID and query columns after loading the whole file.

Answer: We can pass into pandas.read_json a file handler as well. If before that we read part of the data, then only the rest will be converted to DataFrame. def read_json(file, skiprows=None): with open(file) as f: if skiprows: f.readlines(skiprows) df = pd.read_json(f, lines=True) return df
Question: Let's consider a function of two variables f(x1, x2) , where x1 spans over a vector v1 and x2 spans over a vector v2. If f(x1, x2) = np.exp(x1 + x2), we can represent this function in Python as a matrix by means of the command numpy.meshgrid like this: xx, yy = numpy.meshgrid(v1, v2) M = numpy.exp(xx + yy)  This way, M is a representation of the function f over the cartesian product "v1 x v2", since M[i,j] = f(v1[i],v2[j]). But this works because both sums and exponential work in parallel componentwise. My question is: if my variable is x = numpy.array([x1, x2]) and f is a quadratic function f(x) = x.T @ np.dot(Q, x), where Q is a 2x2 matrix, how can I do the same thing with the meshgrid function (i.e. calculating all the values of the function f on "v1 x v2" at once)? Please let me know if I should include more details!

Answer: def quad(x, y, q): """Return an array A of a shape (len(x), len(y)) with values A[i,j] = [x[i],y[j]] @ q @ [x[i],y[j]] x, y: 1d arrays, q: an array of shape (2,2)""" from numpy import array, meshgrid, einsum a = array(meshgrid(x, y)).transpose() return einsum('ijk,kn,ijn->ij', a, q, a)  Notes meshgrid produces 2 arrays of a shape (len(y), len(x)), where first one is with x values along the second dimension. If we apply to this pair np.array then a 3d array of shape (2, len(y), len(x)) will be produced. With transpose we obtain an array, where an element indexed by [i,j,k] is x[i] if k==0 else y[j], where k is 0 or 1, i.e. first or second array from meshgrid. With 'ijk,kn,ijn->ij' we tell einsum to return the sum written bellow for each i, j: sum(a[i,j,k]*q[k,n]*a[i,j,n] for k in range(2) for n in range(2))  Note, that a[i,j] == [x[i], y[j]].