[THIS POST IS IN CONSTRUCTION]
In my spare time, I will be learning about Machine Learning, Artificial Intelligence, Data Science and other topics where Python is the interpreted language of choice.
Below are some fast notes I cleaned up to share to however is interested. Disclaimer: I am not responsible of the content or how you use the content. The responsibility fall on you.
We can import modules using the import keyword.
Note: Remember to use 4 spaces for indentation and use UTF-8 encoding with your editor when saving py files.
Functions
Fetch words. Assume this is in the words.py file:
from urllib.request import urlopen def fetchWordsFromUrl(url): with urlopen(url) as content: words = [] for each_line in content: array = each_line.split() for each_word in array: words.append(each_word) return words |
Using words.py file with python:
>>> import words >>> words.fetchWordsFromUrl('https://wordpress.org/plugins/about/readme.txt') |
Running as Script or Repo
import sys from urllib.request import urlopen def fetchWordsFromUrl(url): with urlopen(url) as content: words = [] for each_line in content: array = each_line.split() for each_word in array: words.append(each_word) return words if __name__ == '__main__' fetchWordsFromUrl(sys.argv[1]) |
Other Functions
Bytes to Int 32:
def _bytes_to_int32(b): return b[0] | b([1] << 8) | b([2] << 16) | b([3] << 24) |
Int 32 to Bytes:
def _int32_to_bytes(i): return bytes( (i & 0xff, i >> 8 & 0xff, i >> 16 & 0xff, i >> 24 & 0xff) ) |
Even or Odd:
def isEvenOrOdd(n): return n % 2 == 0 |
Square:
def square(n): return n * n |
Note: Code Execution.
$ python3 code.py 99 |
© 2016, Alejandro G. Carlstein Ramos Mejia. All rights reserved.