Here is an example of using assert. This can be found in my fork on github – Fork of PySitemap. The function returns True or False ########################################### # test harness for function wp_remove_dup ########################################### from main import wp_remove_dup test_list = [“https://www.jibsheet.net/linux”, \ “https://www.jibsheet.net/linux/index.php/2021/07/07”,\ “https://www.jibsheet.net/linux/index.php/2021/”,\ “https://www.jibsheet.net/linux/index.php/page/3/”,\ “https://www.jibsheet.net/linux/index.php/2021/05/08/”, “https://www.jibsheet.net/linux/index.php/tag/linux/”] print(test_list) for item in test_list: print(wp_remove_dup(item)) assert (wp_remove_dup(test_list[0]) == False) assert (wp_remove_dup(test_list[1]) […]
Using assert for simple test harness
Python How to reference class functions in class variable.
In static languages – it makes since to put the class variable at the top of the Class. However, since Python is dynamic, it can’t know about the functions until it processes them. class AwsProcessor: service_dict = {‘s3’: process_s3, ‘ebs’: process_ebs} def __init__(self, processing_service): self.service = processing_service def process_ebs(self): pass def process_s3(self): pass This gives error File “c:\aws-scripts\class AwsProcessor.py”, line […]
How to automatically generate requirements.txt for python projects
pip install pipreqs pipreqs /path/to/project stack exchange discussion If you have multiple sub directories – one should view the comments on the stack exchange above.
Python – generator example
Here is a generator that basically goes forever in a circular list from 1 to 12 def get_next_month(month): i_month = month while True: if i_month == 12: i_month = 1 else: i_month = i_month+1 yield i_month Here is how to use it. def month_current_and_next(month, count = 1 ): if month == 0: time_t = datetime.now() current_month = time_t.month else: […]
Python tip – convert to “raw” string
This is useful for Windows pathnames and other situations where you don’t need string interpretation. Such as handling text as blob. raw_s = r'{}’.format(s)
Python – getting seconds since midnight
import datetime now = datetime.datetime.now() midnight = now.replace(hour=0, minute=0, second=0, microsecond=0) seconds = (now – midnight).seconds Can be made into an easy function. And conversion to minutes and hours easy.
Python – how to use glob under windows
Trick is to use “r” before the string See error The “r” means it is a raw string https://docs.python.org/3/reference/lexical_analysis.html File “C:\Users\jonallen\Documents\github\wsgi\FlaskApp\FlaskApp\obs_utils.py”, line 13 obs_dir = “C:\Users\jonallen\Documents\github\weather_obs” ^ SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape import os import sys import glob from pathlib import Path def get_obs_csv_files( dir ): glob_path = Path(dir) […]
Python Logging – how to have 2 loggers to same rotating logfile
Here is an example using app with “Schedule” This is using the Schedule Python module Docs for Schedule Module – has own logger Solution – is just to add the same handler instance to the 2nd logger. logger = logging.getLogger(‘weather_obs_f’) ch = logging.StreamHandler(stream=sys.stdout) ch_format = logging.Formatter(‘weather_obs – %(message)s’ ch.setFormatter(ch_format) ch.setLevel(logging.INFO) pid = os.getpid() fhandler = TimedRotatingFileHandler(‘weather_obs.’ + str(pid) + ‘.log’, […]
Graviton – getting python pandas to work
Procedure I reviewed the packages available – yum list – output of ‘yum list’ see numpy and scipy but no pandas – means we might have to compile. 2. Install development packages root@graviton~]# sudo yum install “@Development tools” python3-pip python3-devel blas-devel gcc-gfortran lapack-devel …. Installed: autoconf.noarch 0:2.69-11.amzn2 automake.noarch 0:1.13.4-3.1.amzn2 bison.aarch64 0:3.0.4-6.amzn2.0.2 byacc.aarch64 0:1.9.20130304-3.amzn2.0.2 cscope.aarch64 0:15.8-10.amzn2.0.2 ctags.aarch64 0:5.8-13.amzn2.0.2 diffstat.aarch64 0:1.57-4.amzn2.0.2 doxygen.aarch64 […]
loading 3 day weather data using pandas
Notes In [1]: import pandas as pd import numpy as np import lxml In [2]: df = pd.read_html(‘https://w1.weather.gov/data/obhistory/KDCA.html’) In [7]: type(df) Out[7]: list In [11]: df[3] Out[11]: Date Time(edt) Wind(mph) Vis.(mi.) Weather Sky Cond. Temperature (ºF) RelativeHumidity WindChill(°F) HeatIndex(°F) Pressure Precipitation (in.) Date Time(edt) Wind(mph) Vis.(mi.) Weather Sky Cond. Air Dwpt 6 hour RelativeHumidity WindChill(°F) HeatIndex(°F) altimeter(in) sea level(mb) 1 hr 3 hr […]