Skip to content

Examples for some key libraries in Python that I use all the time. A way for me to remember and hopefully get others started.

License

Notifications You must be signed in to change notification settings

james-see/python-examples

Repository files navigation

python-examples

CICodeQL

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 →

🚀 Quick Start

This project uses uv for modern Python package management.

Prerequisites

  • Python 3.12 or higher
  • uv package manager (install via curl -LsSf https://astral.sh/uv/install.sh | sh)

Installation

# 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

Development

# 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 .

🛠️ Project Structure

  • 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)

🔐 Security & Configuration

Setting Up API Keys

Many examples require API keys or credentials. To use them:

  1. Copy the example config file:

    cp configs.py.example configs.py
  2. Edit configs.py and add your actual API keys (this file is gitignored)

  3. Alternatively, use environment variables:

    export FOURSQUARE_CLIENT_ID="your_key"export FOURSQUARE_CLIENT_SECRET="your_secret"export DB_PASSWORD="your_password"

⚠️ Never commit configs.py to version control! It's already in .gitignore.

🤖 LLM & AI Examples

Modern examples for working with Large Language Models and AI workflows.

API Configuration

Create a .env file in the project root (see .env.example for template):

OPENAI_API_KEY=sk-... ANTHROPIC_API_KEY=sk-ant-...

Available Examples

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

Dependencies

# Install all LLM dependencies uv sync # Or install specific packages uv add openai anthropic langchain langchain-openai instructor python-dotenv

Usage Examples

# 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.py

📚 Classic Python Examples

urllib (built-in to python3)

  1. access foursquare API

By python 3.x package:

http.server (built-in to python3)

  1. http example

pdfquery (install by uv add pdfquery)

  1. pdfquery example

PyPDF2 (install by uv add PyPDF2)

  1. pdf merge example

argparse (built-in to python3)

  1. argparse example

bs4 (install via uv add beautifulsoup4)

  1. html to text parser
  2. email parser

fuzzywuzzy (install by uv add fuzzywuzzy)

  1. fuzzywuzzy

rethinkdb (install by uv add rethinkdb)

  1. rethinkdb example

quandl (install by uv add quandl)

  1. quandl api access example

hug (install by uv add hug)

  1. hug api access example

base64 (package is built-in)

  1. base64 encode & decode example

http.server (package is built-in)

  1. web server example

hashlib (package is built-in)

  1. sha 256 hash example

nltk (install via uv add nltk)

  1. sentiment analysis example

exifread (install via uv add exifread)

  1. read exif example

json (built-in to python3)

  1. json to python object example

urllib3 (install via uv add urllib3)

  1. google mask search example
  2. urllib3 proxymanager example

blockchain (install via uv add blockchain)

  1. wallet query example

PySocks (package download link)

  1. connect to tor and print .onion site

shodan (install via uv add shodan)

  1. shodan count example
  2. google lat/long and shodan enrichment geo search example

websockify (install via uv add websockify)

  1. websockify example

scrapy (package download link)

  1. crawl all internal links for a domain

iptcinfo3 (install via uv add iptcinfo3)

  1. iptcinfo3 example

imaplib (build-in python3x)

  1. imaplib example

http.server Example

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.

Run the example

uv run python example-http-server.py

This will output that it is running on port 8000.

pdfquery Example

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.

Run the example

uv run python pdfquery-example.py mypdf.pdf

This will output the text of the pdf to stdout if it contains any.

PDF Merge Example

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.

Run the example

To get the help file:

uv run python merge-pdfs-example.py -h

To 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.pdf

Pattern Twitter Search Example

The 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 Connect Example

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.

Run the tor connect example

Just simply run it from the terminal window:

pythontor-example.py

This will return the DuckDuckGo .onion html as proof that it is working.

Google Search Example

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.

Run the example

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 example

hug is a great easy-to-use api to help route things on your web app

Run the example

python3hug_api_example.py

This 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.

base64 Example

Converting data to base64 ensure a nice obsfuscation layer for data transport.

Run the base64 example

python3base64_example.py

This will output a html string that is encoded into base64.

Html to Text Example

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 beautifulsoup4

Run the Example

python3example-html2plaintext.py

Returns

[*-*]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. -------------------

FuzzyWuzzy Example

This example searches for 'cowboy' and returns Dallas Cowboys as the closest match from the list available.

Run the Example

python3fuzzywuzzy-example.py

Returns

DallasCowboys, 90

Google Mask Example

This 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.

Run the Example

doesn'tworkanymorebecausegooglekilledtheirAPI

Server Example

This example starts an http server on localhost:10010 and returns data when you visit the page

Run the Example

python3server-example.py

Scrapy Spider Example

This example gets the list of all internal links for any domain by following all internal homepage links and their links.

Run the Example

python3spider.py-ujamescampbell.us

Bitcoin Wallet Example

This 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.

Run the Example

python3bitcoin-example-1.py

Exifread Example

This example gets the exif data from an image file

Run the Example

python3exif-reader.pyassets/cat.jpg

Output

Totaltagsfound: 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, valueUnknown

Sentiment Example

This example takes a test list of tweets and returns positive or negative. It works in Python 3.

Run the Example

python3sentiment-analysis-nltk-example.pytesttweets.txt

Output

negativepositivenegativepositivenegativePositivecount: 2Negativecount: 3

hashlib example

The hashlib package generates hashes from strings. This example uses the sha256 hash algorithm.

Run the Example

python3hashlib_example.py

Proxymanager Example

This example uses urllib3 in Python 3 to connect through a privoxy connection and return status, headers, and content.

Run the Example

python3urllib3-proxymanager-example.py

Output

200HTTPHeaderDict({'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.')+' &nbsp;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: TakeaTour

Quandl Example

This example gets the stocks from AAPL into a dataframe and prints it.

Run the Example

python3quandl-example.py

Output

firstdate: 2001-12-31Totaldaysofstockdataavailable: 4 [Finishedin1.6s]

Json to Python Object Example

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

Run the Example

python3json-example.py

Foursquare API Example

This 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

Run the Example

python4sq-example.py

Output

Whatcitydoyouwanttosearchin? (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.151263216976

argparse Example

This example sets some basic args.

Run the Example

python3argparse.py-h

Returns:

usage: argparseexample [-h] [-a] [-v] [--verbose] Exampleonhowtouseargparsepositionalarguments: +aTurnAonoptionalarguments: -h, --helpshowthishelpmessageandexit-aTurnAoff-v, --versionshowprogram'sversionnumberandexit--verboseverboseflag

Shodan Count Example

This example connects to shodan api via your configs.py file with proper api key variable set and then queries for nginx in Glasgow, GB.

Run the Example

python3shodan-example.py

Returns

Resultsfound: 246 [Finishedin0.6s]

Google GEO and Shodan Example

This example takes an address, gets the lat/long, and searches in shodan for matches near that location.

Run the Example

python3get-geo-example.py

Returns

geo:58.98691099999999,-2.960873,3Resultsfound: 572 [Finishedin0.7s]

RethinkDB Example

This example takes pastebin archive daily json data into a test table in rethinkdb and pulls out values from it.

Run the rethinkdb Example

python3rethink-example.py

Returns rethink stuff

Websockify Example

This example uses websockify.

python3websockify-example.py :8015 :80

Returns

???

Imaplib Example

This example uses imaplib.

Run the imaplib example

python3imaplib-example.py [yourgmail] [yourpassword]

Returns

Latest unread mail from account and continues to run until you CTRL+C

More coming soon!

About

Examples for some key libraries in Python that I use all the time. A way for me to remember and hopefully get others started.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published