Skip to content

Instantly share code, notes, and snippets.

@ashx3s
Last active December 10, 2021 20:46
Show Gist options
  • Select an option

  • Save ashx3s/91270f734cfc6c555a1f9d0c8e84032e to your computer and use it in GitHub Desktop.

Select an option

Save ashx3s/91270f734cfc6c555a1f9d0c8e84032e to your computer and use it in GitHub Desktop.
Python Notes

Python Notes

Refresher notes on python syntax

Try Statements

These are used to handle errors etc

Try Except

Try Raise

Try Pass


Codecs

the codecs module defines classes for python codecs. This handles errors and encoding and decoding lookup process.

codecs.open(filename, mode='r', encoding=None, errors='strict, buffering=-1)
  • This will oepn a file in read only
  • setting encoding to none will allow it to accept any type of encoding
  • errors set to strict will raise a ValueError when an encoding error occurs
  • buffering set to -1 is the default, this just means that the default buffer will be used

with codecs.open('data/templates/blogfeed.html', 'w', 'utf8') as f:
  f.write(html)
  • This code snippit taken from magfeed.py and blogfeed.py will open the specified file and write everything in the html variable to it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment