I am using python-can to send CAN messages like this: The script works fine, but when I run it for a 2nd time, it results in an error, because the bus is still open from the previous time. I think I need something like this at the end of my script: However, this does not exist and I can’t seem
Tag: python
How to include condition in composite list comprehension?
I would like to flatten a list but keep NaNs. The following base code works when there are no NaNs: Now if I have the following case it will break: which makes sense, but I need this border case to be handled and I’m not sure how to add the special case condition in the list comprehension above. How can
How to upload data from csv to database(PostgreSQL) on python
I have a few parsers that collect data and make csv file, after collecting data I need to upload data from csv to my database(PostgreSQL) p.s.table in database is already exist and just need to append data How can I do this? I have try to use sqlalchemy, but after connection don’t know what to do Didn&#…
How to output durations in HHHH:MM format with xlsxwriter in Python
I am trying to output durations to an Excel spreadsheet from Python with xslxwriter. I have the following code: The spreadsheet that is output contains in its single cell 0:01, i.e. zero hours and one minute. I am trying to get to get it to say 24:01. I cannot find a way to do this with xslswriter, though the…
Is preprocessing repeated in a Pipeline each time a new ML model is loaded?
I have created a pipeline using sklearn so that multiple models will go through it. Since there is vectorization before fitting the model, I wonder if this vectorization is performed always before the model fitting process? If yes, maybe I should take this preprocessing out of the pipeline. Answer When you ar…
Filtering out rows based on other rows using pandas
I have a dataframe that looks like this: I can’t seem to figure out a way to filter out certain rows based on the following condition: If there are two entries under the same companyId, as is the case for 198236 and 153421, I want to keep only the entry where type is actual. If there is only one entry
Running unittest with modules that must import other modules
Our Python 3.10 unit tests are breaking when the modules being tested need to import other modules. When we use the packaging techniques recommended by other posts and articles, either the unit tests fail to import modules, or the direct calls to run the app fail to import modules. The other posts and article…
Regex – How to account for random spacing/line breaks in a term
I am using Python and have the following regular expression to extract text from text files: My issue is specifically with the last term, “Pursuant to the requirements of the Securities Exchange Act of 1934”. In the text files, this sentence is sometimes spaced randomly and starts different parts …
How to pause a pynput event listener
I am trying to make a program that will read the user’s inputs and if it detects a certain key combination, it will type a special character. Currently I am testing and I want the script to just output a ‘+’ after each character that the user types. I am using the pynput event listener to de…
From unix timestamps to relative date based on a condition from another column in pandas
I have a column of dates in unix timestamps and i need to convert them into relative dates from the starting activity. The final output should be the column D, which expresses the relative time from the activity which has index = 1, in particular the relative time has always to refer to the first activity (in…