This is a collection of python examples I created for some key libraries in Python that I use all the time.
It is a way for me to remember and hopefully get others started.
Start your Python journey in Python 3. Onward and upward.
📚 View the documentation site →
This project uses uv for modern Python package management.
- Python 3.12 or higher
- uv package manager (install via
curl -LsSf https://astral.sh/uv/install.sh | sh)
# Clone the repository git clone https://github.com/james-see/python-examples.git cd python-examples # Install dependencies uv sync --dev # Run tests uv run pytest # Run linting uv run flake8# Add new dependencies uv add package-name # Run a specific example uv run python example-name.py # Format code uv run black . uv run isort .- python-examples/: Main package containing all example scripts
- assets/: Sample data files for examples
- tests/: Test files
- .github/workflows/: GitHub Actions for CI/CD
- configs.py.example: Template for API keys and credentials (copy to
configs.py)
Many examples require API keys or credentials. To use them:
Copy the example config file:
cp configs.py.example configs.py
Edit
configs.pyand add your actual API keys (this file is gitignored)Alternatively, use environment variables:
export FOURSQUARE_CLIENT_ID="your_key"export FOURSQUARE_CLIENT_SECRET="your_secret"export DB_PASSWORD="your_password"
configs.py to version control! It's already in .gitignore.
Modern examples for working with Large Language Models and AI workflows.
Create a .env file in the project root (see .env.example for template):
OPENAI_API_KEY=sk-... ANTHROPIC_API_KEY=sk-ant-...OpenAI GPT (openai_gpt-example.py)
- Chat completions with GPT-4 and GPT-3.5
- Streaming responses
- Function calling (tool use)
- System prompts and conversation memory
- JSON mode for structured outputs
- Get API key
Anthropic Claude (anthropic_claude-example.py)
- Claude 3.5 Sonnet, Opus, and Haiku
- Streaming and multi-turn conversations
- Vision capabilities
- Extended thinking for complex reasoning
- Response prefill for guided outputs
- Get API key
LangChain (langchain-example.py)
- Prompt templates and chains
- Conversation memory
- Structured output parsing with Pydantic
- RAG (Retrieval-Augmented Generation) patterns
- Few-shot prompting
- Sequential chains
- LangChain Docs
Instructor (instructor-example.py)
- Type-safe structured outputs
- Pydantic model validation
- List and nested model extraction
- Classification and sentiment analysis
- Chain of thought reasoning
- Streaming structured outputs
- Instructor Docs
# Install all LLM dependencies uv sync # Or install specific packages uv add openai anthropic langchain langchain-openai instructor python-dotenv# OpenAI GPT examplescd python-examples python openai_gpt-example.py # Anthropic Claude examples python anthropic_claude-example.py # LangChain framework python langchain-example.py # Instructor for structured outputs python instructor-example.pyurllib (built-in to python3)
http.server (built-in to python3)
pdfquery (install by uv add pdfquery)
PyPDF2 (install by uv add PyPDF2)
argparse (built-in to python3)
bs4 (install via uv add beautifulsoup4)
fuzzywuzzy (install by uv add fuzzywuzzy)
rethinkdb (install by uv add rethinkdb)
quandl (install by uv add quandl)
hug (install by uv add hug)
base64 (package is built-in)
http.server (package is built-in)
hashlib (package is built-in)
nltk (install via uv add nltk)
exifread (install via uv add exifread)
json (built-in to python3)
urllib3 (install via uv add urllib3)
blockchain (install via uv add blockchain)
PySocks (package download link)
shodan (install via uv add shodan)
websockify (install via uv add websockify)
scrapy (package download link)
iptcinfo3 (install via uv add iptcinfo3)
imaplib (build-in python3x)
This example runs a web server to http://127.0.0.1:8000. Go to http://127.0.0.1:8000/web.html to verify it is working.
uv run python example-http-server.pyThis will output that it is running on port 8000.
This example takes in the first argument the name of the pdf you want to get text from and prints the text found in the pdf to the screen.
uv run python pdfquery-example.py mypdf.pdfThis will output the text of the pdf to stdout if it contains any.
This example reads in a list of pdfs, you can specify the prefix of the list of pdfs using the -p argument or default is read_. So for example, read_001.pdf, read_002.pdf would automatically get merged into a single pdf called merged.pdf. You can also set the output name via the -o argument.
To get the help file:
uv run python merge-pdfs-example.py -hTo run it on a list of pdfs with prefix test and output final.pdf:
uv run python merge-pdfs-example.py -p test -o final.pdfThe first example I created is pattern-example-twitter.py. Pattern is a great library that is installed via pip and can query Google, Twitter, etc. out of the box.
This twitter example connects to twitter and searches either a random string or terms you set via the terminal with the -s 'search terms'.
Terminal Example
python3pattern-example-twitter.py-s'Hello World'Tor (The Onion Router) has a particular socks port and connection setup that needs configured to connect in Python. This example shows you how. You must already have Tor installed.
Note: You need to install the Socksipy package for this to work, which has an actively maintained fork in PySocks. It is easy if you already have pip (and if you don't have pip you should). $ pip install PySocks
Then make sure your code (like the example) has import socks.
Just simply run it from the terminal window:
pythontor-example.pyThis will return the DuckDuckGo .onion html as proof that it is working.
The Google seach portion of the pattern library was very useful. This example shows you that you can compare the popularity of phrases or sets of terms together using percentages and the sort() command. It selects 10 random words to search on from the imported included dictionary list that is in the assets folder. This doesn't work anymore. Thanks for nothing Google.
python3pattern-example-google.py-c'sexy'Returns:
89.13%"sexy seemed"2.17%"sexy impassive"1.09%"sexy spiegels"1.09%"sexy slumping"1.09%"sexy quietuses"1.09%"sexy noncooperation"1.09%"sexy miriness"1.09%"sexy incompliancy"1.09%"sexy evaporators"1.09%"sexy cudgeler"</code>hug is a great easy-to-use api to help route things on your web app
python3hug_api_example.pyThis will output hug and start a listener process on 127.0.0.1:8000
Then you can go to http://localhost:8000/happy_birthday?name=Hug&age=1 and see the output.
Converting data to base64 ensure a nice obsfuscation layer for data transport.
python3base64_example.pyThis will output a html string that is encoded into base64.
Beautiful Soup is a great library to parse and select html or iterate through the DOM. For this example to work you need to install Beautiful Soup:
uv add beautifulsoup4python3example-html2plaintext.py[*-*]Beforehtmlwithtext: ------------------<!DOCTYPEHTML><head><title>THISISANEXAMPLEby @jamescampbell</title><metaauthor='jamescampbell'><style>a{font-family:arial}</style></head><body><h1>HelloWorld</h1><p>Ihopeyouenjoythisexample.</p></body>------------------ [*-*]AftercleanMe() function: -------------------THISISANEXAMPLEby @jamescampbellHelloWorldIhopeyouenjoythisexample. -------------------This example searches for 'cowboy' and returns Dallas Cowboys as the closest match from the list available.
python3fuzzywuzzy-example.pyDallasCowboys, 90This example used to do three things, 1. sets your search term, 2 . set your number of mask search terms, and 3. selects a random user agent for each search query. Google killed their API for this, so byebye.
doesn'tworkanymorebecausegooglekilledtheirAPIThis example starts an http server on localhost:10010 and returns data when you visit the page
python3server-example.pyThis example gets the list of all internal links for any domain by following all internal homepage links and their links.
python3spider.py-ujamescampbell.usThis example queries the blockchain.info API for an example wallet address and returns the ip address and dates for the transactions as well as the final wallet balance.
python3bitcoin-example-1.pyThis example gets the exif data from an image file
python3exif-reader.pyassets/cat.jpgTotaltagsfound: 66Key: InteroperabilityInteroperabilityVersion, value [48, 49, 48, 48] Key: EXIFInteroperabilityOffset, value36724Key: ImageSoftware, valueSLT-A57v1.02Key: EXIFUserComment, value [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] Key: ImageOrientation, valueHorizontal (normal) Key: ThumbnailJPEGInterchangeFormat, value37012Key: InteroperabilityInteroperabilityIndex, valueR98Key: ImageResolutionUnit, valuePixels/InchKey: EXIFExifImageWidth, value4912Key: EXIFComponentsConfiguration, valueYCbCrKey: EXIFFNumber, value28/5Key: ThumbnailSoftware, valueSLT-A57v1.02Key: EXIFDateTimeDigitized, value2013:04:0714:13:38Key: EXIFExposureProgram, valueAperturePriorityKey: EXIFDateTimeOriginal, value2013:04:0714:13:38Key: EXIFSharpness, valueNormalKey: EXIFRecommendedExposureIndex, value3200Key: EXIFMakerNote, value [83, 79, 78, 89, 32, 68, 83, 67, 32, 0, 0, 0, 78, 0, 3, 16, 4, 0, 16, 0, ... ] Key: EXIFCustomRendered, valueNormalKey: EXIFSaturation, valueNormalKey: EXIFExposureTime, value1/80Key: ImageMake, valueSONYKey: EXIFExifImageLength, value3264Key: EXIFDigitalZoomRatio, value1Key: ImageModel, valueSLT-A57Key: EXIFContrast, valueNormalKey: EXIFSensitivityType, valueRecommendedExposureIndexKey: ThumbnailOrientation, valueHorizontal (normal) Key: ThumbnailYResolution, value72Key: ThumbnailModel, valueSLT-A57Key: ImagePrintIM, value [80, 114, 105, 110, 116, 73, 77, 0, 48, 51, 48, 48, 0, 0, 3, 0, 2, 0, 1, 0, ... ] Key: ThumbnailMake, valueSONYKey: EXIFCompressedBitsPerPixel, value2Key: EXIFMeteringMode, valuePatternKey: EXIFMaxApertureValue, value49/32Key: ImageYCbCrPositioning, valueCo-sitedKey: EXIFBrightnessValue, value303/320Key: EXIFFlashPixVersion, value0100Key: EXIFWhiteBalance, valueAutoKey: EXIFLensModel, value50mmF1.7Key: ThumbnailYCbCrPositioning, valueCo-sitedKey: ImageDateTime, value2013:04:0714:13:38Key: EXIFExifVersion, value0230Key: ThumbnailImageDescription, valueKey: ImageExifOffset, value360Key: ThumbnailJPEGInterchangeFormatLength, value7654Key: EXIFExposureMode, valueAutoBracketKey: EXIFSceneType, valueDirectlyPhotographedKey: EXIFLensSpecification, value [50, 50, 17/10, 17/10] Key: ImageXResolution, value350Key: EXIFExposureBiasValue, value0Key: EXIFColorSpace, valuesRGBKey: EXIFISOSpeedRatings, value3200Key: EXIFSceneCaptureType, valueStandardKey: EXIFFocalLengthIn35mmFilm, value75Key: ImageYResolution, value350Key: ThumbnailDateTime, value2013:04:0714:13:38Key: EXIFFocalLength, value50Key: ThumbnailCompression, valueJPEG (old-style) Key: EXIFFileSource, valueDigitalCameraKey: EXIFFlash, valueFlashdidnotfire, compulsoryflashmodeKey: ImageImageDescription, valueKey: ThumbnailXResolution, value72Key: ThumbnailResolutionUnit, valuePixels/InchKey: EXIFLightSource, valueUnknownThis example takes a test list of tweets and returns positive or negative. It works in Python 3.
python3sentiment-analysis-nltk-example.pytesttweets.txtnegativepositivenegativepositivenegativePositivecount: 2Negativecount: 3The hashlib package generates hashes from strings. This example uses the sha256 hash algorithm.
python3hashlib_example.pyThis example uses urllib3 in Python 3 to connect through a privoxy connection and return status, headers, and content.
python3urllib3-proxymanager-example.py200HTTPHeaderDict({'Content-Length': '5255', 'Proxy-Connection': 'keep-alive', 'ETag': '"564e8118-1487"', 'Server': 'nginx', 'Cache-Control': 'no-cache', 'Expires': 'Fri, 20 Nov 2015 02:15:59 GMT', 'Accept-Ranges': 'bytes', 'Content-Type': 'text/html; charset=UTF-8', 'Connection': 'keep-alive', 'Date': 'Fri, 20 Nov 2015 02:16:00 GMT'}) <!DOCTYPEhtml><!--[ifIEMobile7 ]><htmllang="en_US"class="no-js iem7"><![endif]--><!--[ifltIE7]><htmlclass="ie6 lt-ie10 lt-ie9 lt-ie8 lt-ie7 no-js"lang="en_US"><![endif]--><!--[ifIE7]><htmlclass="ie7 lt-ie10 lt-ie9 lt-ie8 no-js"lang="en_US"><![endif]--><!--[ifIE8]><htmlclass="ie8 lt-ie10 lt-ie9 no-js"lang="en_US"><![endif]--><!--[ifIE9]><htmlclass="ie9 lt-ie10 no-js"lang="en_US"><![endif]--><!--[if (gteIE9)|(gtIEMobile7)|!(IEMobile)|!(IE)]><!--><htmlclass="no-js"lang="en_US"><!--<![endif]--><head><metahttp-equiv="X-UA-Compatible"content="IE=Edge"/><metahttp-equiv="content-type"content="text/html; charset=UTF-8;charset=utf-8"><metaname="viewport"content="width=device-width, initial-scale=1, user-scalable=1"/><metaname="HandheldFriendly"content="true"/><linkrel="canonical"href="https://duckduckgo.com/"><linkrel="stylesheet"href="/s1049.css"type="text/css"><linkrel="stylesheet"href="/t1049.css"type="text/css"><linkrel="shortcut icon"href="/favicon.ico"type="image/x-icon"sizes="16x16 24x24 32x32 64x64"/><linkrel="apple-touch-icon"href="/assets/icons/meta/DDG-iOS-icon_60x60.png"/><linkrel="apple-touch-icon"sizes="76x76"href="/assets/icons/meta/DDG-iOS-icon_76x76.png"/><linkrel="apple-touch-icon"sizes="120x120"href="/assets/icons/meta/DDG-iOS-icon_120x120.png"/><linkrel="apple-touch-icon"sizes="152x152"href="/assets/icons/meta/DDG-iOS-icon_152x152.png"/><linkrel="image_src"href="/assets/icons/meta/DDG-icon_256x256.png"/><linktitle="DuckDuckGo"type="application/opensearchdescription+xml"rel="search"href="/opensearch.xml"><metaname="twitter:site"value="@duckduckgo"><metaname="twitter:url"value="https://duckduckgo.com/"><metaproperty="og:url"content="https://duckduckgo.com/"/><metaproperty="og:site_name"content="DuckDuckGo"/><title>DuckDuckGo</title><metaproperty="og:title"content="DuckDuckGo"/><metaname="twitter:title"value="DuckDuckGo"><metaname="description"content="The search engine that doesn't track you. A superior search experience with smarter answers, less clutter and real privacy."></head><bodyid="pg-index"class="page-index body--home"><scripttype="text/javascript">varsettings_js_version="/s1847.js", locale="en_US"; </script><scripttype="text/javascript"src="/locales/en_US/LC_MESSAGES/duckduckgo-duckduckgo+sprintf+gettext+locale-simple.20151112.063921.js"></script><scripttype="text/javascript"src="/d1847.js"></script><scripttype="text/javascript">DDG.page=newDDG.Pages.Home(); </script><divclass="site-wrapper site-wrapper--home js-site-wrapper"><divclass="site-wrapper-border"></div><divclass="header-wrap--home js-header-wrap"></div><divid=""class="content-wrap--home"><divid="content_homepage"class="content--home"><divclass="cw--c"><divclass="logo-wrap--home"><aid="logo_homepage_link"class="logo_homepage"href="/about">AboutDuckDuckGo<spanclass="logo_homepage__tt">Duckit!</span></a></div><divclass="search-wrap--home"><formid="search_form_homepage"class="search search--home js-search-form"name="x"method="POST"action="/html"><inputid="search_form_input_homepage"class="search__input js-search-input"type="text"autocomplete="off"name="q"tabindex="1"value=""><inputid="search_button_homepage"class="search__button js-search-button"type="submit"tabindex="2"value="S"/><inputid="search_form_input_clear"class="search__clear empty js-search-clear"type="button"tabindex="3"value="X"/><divid="search_elements_hidden"class="search__hidden js-search-hidden"></div><spanclass="search__overlay js-search-overlay"></span></form></div><!--en_USAllSettings--><noscript><divclass="tag-home">Thesearchenginethatdoesn'ttrackyou. <spanclass="tag-home__links"><spanclass="js-homepage-cta"><ahref="/spread"class="tag-home__link">HelpSpreadDuckDuckGo!</a><spanclass="tag-home__links__sep">|</span></span><ahref="/tour"class="tag-home__link">TakeaTour</a></span></div></noscript><divclass="tag-home tag-home--slide no-js__hide js-tag-home"></div><divid="error_homepage"></div></div><!--cw--></div><!--content_homepage//--></div><!--content_wrapper_homepage//--><divid="footer_homepage"class="foot-home js-foot-home"></div><scripttype="text/javascript">{functionseterr(str){varerror=document.getElementById('error_homepage'); error.innerHTML=str; $(error).css('display','block')} varerr=newRegExp('[\?\&]e=([^\&]+)');varerrm=newArray();errm['2']='no search';errm['3']='search too long';errm['4']='not UTF\u002d8 encoding';if (err.test(window.location.href)) seterr('Oops, '+(errm[RegExp.$1]?errm[RegExp.$1]:'there was an error.')+' Please try again')};if (ip) setTimeout('nuo(1)',250);nip(1) if (kurl){document.getElementById("logo_homepage_link").href+= (document.getElementById("logo_homepage_link").href.indexOf('?')==-1 ? '?t=i' : '') +kurl} </script></div><!--site-wrapper--></body></html>Thisisalink: AboutDuckDuckGoDuckit! Thisisalink: HelpSpreadDuckDuckGo! Thisisalink: TakeaTourThis example gets the stocks from AAPL into a dataframe and prints it.
python3quandl-example.pyfirstdate: 2001-12-31Totaldaysofstockdataavailable: 4 [Finishedin1.6s]This example takes a json object and converts it to python and iterates through the values. It works for Python 3 or Python 2.7
python3json-example.pyThis example connects to Foursquare and asks for a city, country input and venue name and returns back the JSON and the Latitude and Longitude as well as the link to display the Qwant Map zoomed into that location
python4sq-example.pyWhatcitydoyouwanttosearchin? (nospaces, includecountry): London,UKWhatisthenameofthevenuetosearch?: MilleniumHotel{u'geocode':{u'parents': [ ], u'what': u'', u'where': u'londonuk', u'feature':{u'highlightedName': u'<b>London</b>, GreaterLondon, <b>UK</b>', u'displayName': u'London, GreaterLondon, UnitedKingdom', u'name': u'London', u'longId': u'72057594040571679', u'cc': u'GB', u'id': u'geonameid: 2643743', u'geometry':{u'center':{u'lat': 51.50853, u'lng': -0.12574 }, u'bounds':{u'sw':{u'lat': 51.28467404417054, u'lng': -0.5085579279369435 }, u'ne':{u'lat': 51.691643999655895, u'lng': 0.33418999705203406 } } }, u'matchedName': u'London, GreaterLondon, UK', u'woeType': 7, u'slug': u'london' } }, u'venues': [{u'verified': True, u'name': u'MillenniumHotelLondonMayfair', u'referralId': u'v-1434850451', u'url': u'http: //www.millenniumhotels.co.uk', u'storeId': u'', u'hereNow':{u'count': 0, u'groups': [ ], u'summary': u'Nobodyhere' }, u'specials':{u'count': 0, u'items': [ ] }, u'contact':{u'facebookName': u'Millennium&CopthorneHotelsEurope', u'twitter': u'millenniumeu', u'phone': u'+442076299400', u'facebook': u'456685494411593', u'formattedPhone': u'+442076299400', u'facebookUsername': u'MillenniumEU' }, u'location':{u'city': u'Mayfair', u'cc': u'GB', u'country': u'UnitedKingdom', u'postalCode': u'W1K2HP', u'state': u'GreaterLondon', u'formattedAddress': [ u'44GrosvenorSquare', u'Mayfair', u'GreaterLondon', u'W1K2HP', u'UnitedKingdom' ], u'address': u'44GrosvenorSquare', u'lat': 51.51086806955976, u'lng': -0.1512632169763817 }, u'stats':{u'tipCount': 31, u'checkinsCount': 3586, u'usersCount': 1559 }, u'id': u'4ac518b5f964a52090a020e3', u'categories': [{u'pluralName': u'Hotels', u'primary': True, u'name': u'Hotel', u'shortName': u'Hotel', u'id': u'4bf58dd8d48988d1fa931735', u'icon':{u'prefix': u'https: //ss3.4sqi.net/img/categories_v2/travel/hotel_', u'suffix': u'.png' } } ] } ] } Lat/Long: 51.5108680696, -0.151263216976This example sets some basic args.
python3argparse.py-hReturns:
usage: argparseexample [-h] [-a] [-v] [--verbose] Exampleonhowtouseargparsepositionalarguments: +aTurnAonoptionalarguments: -h, --helpshowthishelpmessageandexit-aTurnAoff-v, --versionshowprogram'sversionnumberandexit--verboseverboseflagThis example connects to shodan api via your configs.py file with proper api key variable set and then queries for nginx in Glasgow, GB.
python3shodan-example.pyResultsfound: 246 [Finishedin0.6s]This example takes an address, gets the lat/long, and searches in shodan for matches near that location.
python3get-geo-example.pygeo:58.98691099999999,-2.960873,3Resultsfound: 572 [Finishedin0.7s]This example takes pastebin archive daily json data into a test table in rethinkdb and pulls out values from it.
python3rethink-example.pyThis example uses websockify.
python3websockify-example.py :8015 :80???
This example uses imaplib.
python3imaplib-example.py [yourgmail] [yourpassword]Latest unread mail from account and continues to run until you CTRL+C
More coming soon!