| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,35 @@ | |||
| 1 | + import re | ||
| 2 | + | ||
| 3 | + | ||
| 4 | + def validDate(date_string: str)->bool: | ||
| 5 | + """ | ||
| 6 | + Validates stringtype dates of type `dd/mm/yyyy`, `dd-mm-yyyy` or `dd.mm.yyyy` from | ||
| 7 | + years 1900-9999. Leap year support included. | ||
| 8 | + | ||
| 9 | + Parameters | ||
| 10 | + ---------- | ||
| 11 | + date_string : str | ||
| 12 | + Date to be validated | ||
| 13 | + | ||
| 14 | + Returns | ||
| 15 | + ---------- | ||
| 16 | + boolean | ||
| 17 | + Whether the date is valid or not | ||
| 18 | + | ||
| 19 | + Examples | ||
| 20 | + --------- | ||
| 21 | + >>> validDate("11/02/1996") | ||
| 22 | + True | ||
| 23 | + >>> validDate("29/02/2016") | ||
| 24 | + True | ||
| 25 | + >>> validDate("43/01/1996") | ||
| 26 | + False | ||
| 27 | + """ | ||
| 28 | + | ||
| 29 | + # This regex string will validate dates of type `dd/mm/yyyy`, `dd-mm-yyyy` or `dd.mm.yyyy` | ||
| 30 | + # from years 1900 - 9999. Leap year support included. Regex string from | ||
| 31 | + # https://stackoverflow.com/questions/15491894/regex-to-validate-date-format-dd-mm-yyyy | ||
| 32 | + if re.match(r"^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:19|[2-9]\d)?\d{2})$", date_string, flags=0): | ||
| 33 | + return True | ||
| 34 | + else: | ||
| 35 | + return False | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,39 @@ | |||
| 1 | + import random | ||
| 2 | + | ||
| 3 | + | ||
| 4 | + def nhsNumberGenerator(to_generate: int)->list: | ||
| 5 | + """ | ||
| 6 | + Generates random NHS number(s) compliant with modulus 11 checks recorded | ||
| 7 | + in the data dictonary. | ||
| 8 | + https://www.datadictionary.nhs.uk/data_dictionary/attributes/n/nhs/nhs_number_de.asp?shownav=1 | ||
| 9 | + | ||
| 10 | + Parameters | ||
| 11 | + ---------- | ||
| 12 | + to_generate : int | ||
| 13 | + number of NHS numbers to generate | ||
| 14 | + | ||
| 15 | + Returns | ||
| 16 | + ---------- | ||
| 17 | + generated : list | ||
| 18 | + List of randomly generated NHS numbers | ||
| 19 | + | ||
| 20 | + Examples | ||
| 21 | + --------- | ||
| 22 | + >>> random.seed(42) | ||
| 23 | + >>> nhsNumberGenerator(2) | ||
| 24 | + [7865793030, 1933498560] | ||
| 25 | + """ | ||
| 26 | + | ||
| 27 | + generated = [] | ||
| 28 | + while len(generated) < to_generate: | ||
| 29 | + # Random 9 digit number starting with non-zero digit | ||
| 30 | + number = random.randint(100000000, 999999999) | ||
| 31 | + digits = [int(digit) for digit in str(number)] | ||
| 32 | + # Apply weighting to digits | ||
| 33 | + weighted_digits = [(10 - index) * digit for (index, digit) in enumerate(digits)] | ||
| 34 | + # Sum of all weighted digits must be a multiple of 11 to be valid. | ||
| 35 | + if sum(weighted_digits) % 11 == 0: | ||
| 36 | + # Add check digit to valid number | ||
| 37 | + number = int(str(number) + "0") | ||
| 38 | + generated.append(number) | ||
| 39 | + return generated | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,65 @@ | |||
| 1 | + from sqlalchemy import create_engine | ||
| 2 | + import pandas as pd | ||
| 3 | + | ||
| 4 | + | ||
| 5 | + def tableFromSql(server: str, database: str, table_name: str, user: str = "", password: str = "", schema: str = None, index_col: str = None, coerce_float: bool = True, parse_dates: list = None, columns: list = None, chunksize: int = None): | ||
| 6 | + ''' | ||
| 7 | + Returns a SQL table in a DataFrame. | ||
| 8 | + | ||
| 9 | + Convert a table stored in SQL Server 2016 into a pandas dataframe. | ||
| 10 | + Uses sqlalchemy and pandas. | ||
| 11 | + | ||
| 12 | + Parameters | ||
| 13 | + ---------- | ||
| 14 | + server : string | ||
| 15 | + Name of the SQL server | ||
| 16 | + database : string | ||
| 17 | + Name of the SQL database | ||
| 18 | + user : string, default: "" | ||
| 19 | + If verification is required, name of the user | ||
| 20 | + password : string, default: "" | ||
| 21 | + If verification is required, password of the user | ||
| 22 | + table_name : string | ||
| 23 | + Name of SQL table in database. | ||
| 24 | + schema : string, default : None | ||
| 25 | + Name of SQL schema in database to query (if database flavor supports this). Uses | ||
| 26 | + default schema if None (default). | ||
| 27 | + index_col : string or list of strings, default : None | ||
| 28 | + Column(s) to set as index(MultiIndex). | ||
| 29 | + coerce_float : boolean, default : True | ||
| 30 | + Attempts to convert values of non-string, non-numeric objects (like decimal.Decimal) | ||
| 31 | + to floating point. Can result in loss of Precision. | ||
| 32 | + parse_dates : list or dict, default : None | ||
| 33 | + - List of column names to parse as dates. | ||
| 34 | + - Dict of {column_name: format string} where format string is strftime compatible in | ||
| 35 | + case of parsing string times or is one of (D, s, ns, ms, us) in case of parsing | ||
| 36 | + integer timestamps. | ||
| 37 | + - Dict of {column_name: arg dict}, where the arg dict corresponds to the keyword | ||
| 38 | + arguments of pandas.to_datetime() Especially useful with databases without native | ||
| 39 | + Datetime support, such as SQLite. | ||
| 40 | + columns : list, default : None | ||
| 41 | + List of column names to select from SQL table | ||
| 42 | + chunksize : int, default : None | ||
| 43 | + If specified, returns an iterator where chunksize is the number of rows to include | ||
| 44 | + in each chunk. | ||
| 45 | + | ||
| 46 | + Returns | ||
| 47 | + ---------- | ||
| 48 | + pd.DataFrame | ||
| 49 | + Dataframe of the table requested from sql server | ||
| 50 | + | ||
| 51 | + Examples | ||
| 52 | + --------- | ||
| 53 | + # >>> tableFromSql("myServer2", "myDatabase2", "myTable2") | ||
| 54 | + # pd.DataFrame | ||
| 55 | + # >>> tableFromSql("myServer", "myDatabase", "myTable", schema="specialSchema", columns=["col_1", "col_3"]) | ||
| 56 | + # pd.DataFrame | ||
| 57 | + ''' | ||
| 58 | + | ||
| 59 | + try: | ||
| 60 | + uri = "mssql+pyodbc://{}:{}@{}/{}?driver=SQL Server Native Client 11.0".format(user, password, server, database) | ||
| 61 | + engine = create_engine(uri) | ||
| 62 | + return pd.read_sql_table(table_name, engine, schema=schema, index_col=index_col, coerce_float=coerce_float, parse_dates=parse_dates, columns=columns, chunksize=chunksize) | ||
| 63 | + except Exception as error: | ||
| 64 | + raise error | ||
| 65 | + | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,6 +7,10 @@ | |||
| 7 | 7 | packages=['codonPython',], | |
| 8 | 8 | install_required=[ | |
| 9 | 9 | 'numpy', | |
| 10 | + 're', | ||
| 11 | + 'pandas', | ||
| 12 | + 'random', | ||
| 13 | + 'sqlalchemy' | ||
| 10 | 14 | ], | |
| 11 | 15 | author='NHS Digital DIS Team', | |
| 12 | 16 | author_email='paul.ellingham@nhs.net', | |
| Back | FazBrowse Home | New Git URL |
0 commit comments