Python 3 operator >> to print to file

In Python 3 the print statement has become a function. The new syntax looks like this: print(s, end=””, file=depend) This breaking change in Python 3 means that it is not possible to use the same code in Python 2 and 3 when writing to a file using the print statement/function. One possible option would be … Read more

How to create a pandas DatetimeIndex with year as frequency?

Annual indexing to the beginning or end of the year Frequency is freq=’A’ for end of year frequency, ‘AS’ for start of year. Check the aliases in the documentation. eg. pd.date_range(start=pd.datetime(2000, 1, 1), periods=4, freq=’A’) returns DatetimeIndex([‘2000-12-31’, ‘2001-12-31’, ‘2002-12-31’, ‘2003-12-31′], dtype=”datetime64[ns]”, freq=’A-DEC’, tz=None) Annual indexing to the beginning of an arbitrary month If you need … Read more

How to send ESC key to close pop up window using Python and Selenium?

You don’t need to send keys to the element, you need to press them globally (to browser). You can do it via Actions. from selenium import webdriver from selenium.webdriver.common.keys import Keys webdriver.ActionChains(driver).send_keys(Keys.ESCAPE).perform() You can see more info in Webdriver API – 7.2 Action Chains doc

unable to decode Python web request

According to the response.headers (that you did not provide, but that are easily recoverable by running your code), the response is encoded using Brotli compression (Content-Encoding’: ‘br’). You can decompress it with brotlipy: import brotli brotli.decompress(response.content) #b'{“status”:”success”,”data”:[{“placed_by”:”XE4670″,”order_id”:”180331000000385″, #”exchange_order_id”:null,”parent_order_id”:null,”status”:”REJECTED”, #”status_message”:”ADAPTER is down”,”order_timestamp”:”2018-03-31 07:59:42″, #”exchange_update_timestamp”:null,…} Now, it’s JSON, as promised (‘Content-Type’: ‘application/json’).

How to generate a train-test-split based on a group id?

I figured out the answer. This seems to work: from sklearn.model_selection import GroupShuffleSplit splitter = GroupShuffleSplit(test_size=.20, n_splits=2, random_state = 7) split = splitter.split(df, groups=df[‘Group_Id’]) train_inds, test_inds = next(split) train = df.iloc[train_inds] test = df.iloc[test_inds]

FastAPI: Retrieve URL from view name ( route name )

We have got Router.url_path_for(…) method which is located inside the starlette package Method-1: Using FastAPI instance This method is useful when you are able to access the FastAPI instance in your current context. (Thanks to @Yagizcan Degirmenci) from fastapi import FastAPI app = FastAPI() @app.get(‘/hello/’) def hello_world(): return {“msg”: “Hello World”} @app.get(‘/hello/{number}/’) def hello_world_number(number: int): … Read more

PyOpenGL glutInit NullFunctionError

According to the link below the problem was with the glut installation rather than pip install. It seems glut files are not part of PyOpenGL or PyOpenGL_accelerate package. You have to download them seperately. https://stackoverflow.com/a/39181193/7030177 Windows user can use the link below to download glut as mentioned in the given link. ftp://ftp.sgi.com/opengl/glut/glut3.html.old#windows Linux Users can … Read more