Skip to content

Instantly share code, notes, and snippets.

@mdmitry1
Last active January 28, 2023 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdmitry1/c956fde91aea078040a798efeb99fe88 to your computer and use it in GitHub Desktop.
Save mdmitry1/c956fde91aea078040a798efeb99fe88 to your computer and use it in GitHub Desktop.
Structural Pattern Matching example
#!/usr/bin/tcsh -f
"/usr/bin/true" '''\'
if(1 == `uname | grep ^MINGW | wc -l`) then
exec python3.10 $0
else
exec python3.11 $0
endif
'''
# https://realpython.com/python310-new-features/
from datetime import datetime
from random_user import get_user
from pprint import pprint
from sys import version
from re import sub
import sys
def get_age(user):
match user:
case {"dob": {"age": int(age)}}:
return age
case {"dob": dob}:
now = datetime.now()
dob_date = datetime.strptime(dob, "%Y-%m-%d %H:%M:%S")
return now.year - dob_date.year
outf=open("python_version.txt",'w')
original_stdout = sys.stdout
sys.stdout=outf
print(sub('\n','',version))
outf.close()
sys.stdout=original_stdout
users11 = get_user(version="1.1")
pprint(users11)
print('"Age" :', get_age(users11))
users13 = get_user(version="1.3")
pprint(users13)
print('"Age" :', get_age(users13))
{
"dob1": "1986-08-08 01:45:34", "Age1" : 36,
"dob": {"age": 69, "date": "1953-07-24T03:32:27.904Z"}, "Age" : 69
}
# https://realpython.com/python310-new-features/
import requests
def get_user(version="1.3"):
"""Get random users"""
url = f"https://randomuser.me/api/{version}/?results=1"
response = requests.get(url)
if response:
return response.json()["results"][0]
#!/usr/bin/tcsh -f
set script_realpath=`realpath $0`
set script_path=$script_realpath:h
echo '{'
${script_path}/match_ex.py | egrep -w 'dob|^"Age' | \
perl -pe 'if(/,.*$/) {chop;} else {s/^/ /;}' | \
perl -pe 'if($. == 1) {s/$/,/; s/dob/dob1/; s/Age/Age1/;};' | tr '\047' '"'
echo '}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment