Program Listings

Programmer’s Python: Everything is Data

pythondata360

 

We have decided not to make the programs available as a download because this is not the point of the book - the programs are not finished production code but something you should type in and study.

The best solution is to provide the source code of the programs in a form that can be copied and pasted into Thonny or VS Code project. 

The only downside is that you have to create a project to paste the code into.

To do this follow the instructions in the book.

All of the programs below were copy and pasted from working programs in the IDE. They have been formatted using the built in formatter and hence are not identical in layout to the programs in the book. This is to make copy and pasting them easier. The programs in the book are formatted to be easy to read on the page.

If anything you consider important is missing or if you have any requests or comments  contact:

This email address is being protected from spambots. You need JavaScript enabled to view it. 

Page 34

f = 1
k = 1
while(True):
    k=k+1
    f=f*k
    print(f,k)

Page 38

pi = 0
k = 1
while True:
    pi += 4 * (-1)**(k+1) * 1 / (2*k-1)
    k = k+1
    print(k, pi)

import fractions
pi = 0
k = 1
while True:
    pi += 4*(-1)**(k+1)*fractions.Fraction(1, 2*k-1)
    k = k+1
    print(k, pi, float(pi))

Page 49

import random
def myFunction(param1 = random.random()):
    print(param1)
myFunction()
myFunction()

import random
def myFunction(param1 = None):
    param1 = param1 or random.random()
    print(param1)
myFunction()
myFunction()

Page 64

import datetime
import zoneinfo

dt1 = datetime.datetime(2022, 1, 14, 10, 50, tzinfo=zoneinfo.ZoneInfo("Europe/Madrid"))
dt2 = datetime.datetime(2022, 1, 14, 10, 50, tzinfo=zoneinfo.ZoneInfo("utc"))
print(dt1.timestamp())
print(dt2.timestamp())

import datetime
import zoneinfo
dt1 = datetime.datetime(2022, 1, 14, 10, 50,
tzinfo=zoneinfo.ZoneInfo("Europe/Madrid"))
dt2 = dt1.replace(tzinfo=zoneinfo.ZoneInfo("utc"))
print(dt1)
print(dt2)

Page 65

import datetime
import zoneinfo
dt1 = datetime.datetime(2022, 1, 14, 10, 50, tzinfo=zoneinfo.ZoneInfo("Europe/Madrid"))
dt2 = dt1.astimezone(tz=zoneinfo.ZoneInfo("utc"))
print(dt1)
print(dt2)

Page 140

import random

class randItIterator():
    def __next__(self):
        return random.random()
    def __iter__(self):
        return self

class randIt(object):
    def __iter__(self):
        return randItIterator()  
     
numbers=randIt()
for x in numbers:
    print(x)

Page 142

import random
class randListIterator():
    def __init__(self,rndList):
        self._n = 0
        self._data = rndList.data
    def __next__(self):
        if self._n >= len(self._data):
            raise StopIteration
        next = self._data[self._n]
        self._n = self._n+1
        return next
       
class randList(object):
    def __init__(self):
        self.data = []
        for i in range(5):
            self.data.append(random.random())
    def __iter__(self):
        return randListIterator(self)

numbers = randList()
for x in numbers:
    print(x)
for x in numbers:
    print(x)

Page 182

class Tree:
    class Node:
        def __init__(self, value):
            self.left = None
            self.right = None
            self.value = value

    class TreeIter():
        def __init__(self, tree):
            self.stack = []
            self.currentNode = tree.root

        def __iter__(self):
            return self

        def __next__(self):
            node = self.currentNode
            if node is None:
                raise StopIteration
            if node.right is not None:
                self.stack.append(node.right)
            if node.left is not None:
                self.currentNode = node.left
            elif len(self.stack) > 0:
                self.currentNode = self.stack.pop()
            else:
                self.currentNode = None
            return node

    def __init__(self, value):
        self.root = Tree.Node(value)

    def __iter__(self):
        return Tree.TreeIter(self)

    def appendBinarySearch(self, value):
        node = self.root
        while True:
            if node.value > value:
                if node.left is None:
                    node.left = self.Node(value)
                    return
                node = node.left
            else:
                if node.right is None:
                    node.right = self.Node(value)
                    return
                node = node.right

    def appendBreathFirst(self, value):
        queue = [self.root]
        while len(queue) > 0:
            node = queue.pop(0)
            if node.left is None:
                node.left = Tree.Node(value)
                return node.left
            queue.append(node.left)
            if node.right is None:
                node.right = Tree.Node(value)
                return node.right
            queue.append(node.right)


tree = Tree(0)
for i in range(1, 15):
    tree.appendBreathFirst(i)
for node in tree:
    print(node.value)

tree = Tree(8)
for i in [3, 10, 1, 6, 14, 4, 7, 13]:
    tree.appendBinarySearch(i)
for node in tree:
    print(node.value)

Page 211

import random

msg=b"Hello World Of Secrets"
oneTime=random.randbytes(len(msg))
crypt= bytes([a^b for a, b in zip(msg,oneTime)])
print(crypt)
decrypt= bytes([a^b for a, b in zip(crypt,oneTime)])
print(decrypt)

Page 225

import pathlib

path = pathlib.Path("myBinFile.bin")
bytes = bytes([0xAA, 0x55, 0xAA, 0x55])

f = open(path, mode="wb")
f.write(bytes)
f.close()

f = open(path, mode="rb")
bytes = f.read(4)
f.close()
print(bytes.hex(","))

Page 226

import pathlib

path = pathlib.Path("myBinFile.bin")

myValue1 = 1234
f = path.open(mode="wb")
f.write(myValue1.to_bytes(4, byteorder="big"))
f.close()

f = path.open(mode="rb")
b = f.read(4)
f.close()

myValue2 = int.from_bytes(b, byteorder="big")
print(myValue2)
 
 
import pathlib

path = pathlib.Path("myBinFile.bin")

myString1 = "Hello World"
b = myString1.encode(encoding="utf8")

f = path.open(mode="wb")
n = f.write(b)
f.close()

f = path.open(mode="rb")
b = f.read(n)
f.close()

myString2 = b.decode(encoding="utf-8")
print(myString2)

Page 227

import pathlib

path = pathlib.Path("myBinFile.bin")

myString1 = "Hello World"

b = myString1.encode(encoding="utf8")
n = len(b)

f = path.open(mode="wb")
f.write(n.to_bytes(4, byteorder="big"))
m = f.write(b)
f.close()
if m != n:
    print("file error")

f = path.open(mode="rb")
b = f.read(4)
n = int.from_bytes(b, byteorder="big")
b = f.read(n)
f.close()

myString2 = b.decode(encoding="utf-8")
print(myString2)

Page 230

import pathlib
import struct

def floatToBytes(f):
    return struct.pack(">d",f)
def floatFromBytes(b):
    return struct.unpack(">d",b)[0]

path = pathlib.Path("myBinFile.bin")

myFloat = 3.14159

f = path.open(mode="wb")
m = f.write(floatToBytes(myFloat))
f.close()

f = path.open(mode="rb")
b = f.read(8)
f.close()

myFloat = floatFromBytes(b)
print(myFloat)

Page 234

import dataclasses
import struct
import pathlib


@dataclasses.dataclass
class person:
    name:str=""
    id:int=0
    score:float=0.0
    myStruct= struct.Struct(">25sld")
    def save(self,path):
        b=person.myStruct.pack(self.name.encode(encoding="utf8"),
                                             self.id,self.score)
        f=path.open(mode="wb")
        f.write(b)
        f.close()

    def load(self,path):
        f=path.open(mode="rb")
        b=f.read(37)
        f.close()
        a,b,c=person.myStruct.unpack(b)
        self.name=a.decode(encoding="utf8").rstrip("\0x00")
        self.id=b
        self.score=c

path=pathlib.Path("myBinFile.bin")

me=person("mike",42,3.14)
me.save(path)

me2=person()
me2.load(path)
print(me2)
 

Page 235

import dataclasses
import struct
import pathlib
import os

@dataclasses.dataclass
class person:
    name:str=""
    id:int=0
    score:float=0.0
    myStruct= struct.Struct(">25sld")
    n=37

    def save(self,f):
        b=person.myStruct.pack(
             self.name.encode(encoding="utf8"),self.id,self.score)
        f.write(b)
   
    def load(self,f,id):
        f.seek(id*person.n,0)
        b=f.read(person.n)
        a,b,c=person.myStruct.unpack(b)
        self.name=a.decode(encoding="utf8").rstrip("\0x00")
        self.id=b
        self.score=c



path=pathlib.Path("myBinFile.bin")
os.remove(path)
f=path.open(mode="ab")

me=person("mike",42,3.14)
for id in range(50):
    me.id=id
    me.save(f)
f.close()

f=path.open(mode="r+b")
me2=person()
me2.load(f,20)
print(me2)
me2.load(f,5)
print(me2)
f.close()

Page 235

import dataclasses
import struct
import pathlib
import os

@dataclasses.dataclass
class person:
    name:str=""
    id:int=0
    score:float=0.0
    myStruct= struct.Struct(">25sld")
    n=37

    def save(self,f):
        b=person.myStruct.pack(
             self.name.encode(encoding="utf8"),self.id,self.score)
        f.write(b)
   
    def load(self,f,id):
        f.seek(id*person.n,0)
        b=f.read(person.n)
        a,b,c=person.myStruct.unpack(b)
        self.name=a.decode(encoding="utf8").rstrip("\0x00")
        self.id=b
        self.score=c



path=pathlib.Path("myBinFile.bin")
os.remove(path)
f=path.open(mode="ab")

me=person("mike",42,3.14)
for id in range(50):
    me.id=id
    me.save(f)
f.close()

f=path.open(mode="r+b")
me2=person()
me2.load(f,20)
print(me2)
me2.load(f,5)
print(me2)
f.close()

Page 248

import pathlib
import dataclasses
import csv

@dataclasses.dataclass
class person:
    name:str=""
    id:int=0
    score:float=0.0

me = person("mike", 42, 3.145)
path = pathlib.Path("myTextFile.csv")

with path.open(mode="wt", newline="") as f:
    peopleWriter = csv.writer(f)
    for i in range(43):
        peopleWriter.writerow([me.name, i, me.score])

with path.open(mode="rt") as f:
    peopleReader = csv.reader(f)
    for row in peopleReader:
        print(row)

Page 251

import pathlib
import dataclasses
import csv

@dataclasses.dataclass
class person:
    name:str=""
    id:int=0
    score:float=0.0
    def values(self):
        return [self.name,str(self.id),str(self.score)]
   
class MyCSV(csv.Dialect):
    quoting =csv.QUOTE_NONNUMERIC
    delimiter      = ','
    quotechar      = '"'
    lineterminator = '\r\n'

me=person("mike",42,3.145)
path=pathlib.Path("myTextFile.csv")

with path.open(mode="wt",newline="") as f:
    peopleWriter=csv.writer(f,dialect=MyCSV)
    for i in range(43):
        peopleWriter.writerow([me.name,i,me.score])
       
with path.open(mode="rt") as f:
    peopleReader=csv.reader(f,dialect=MyCSV)
    for row in peopleReader:
        print(row)

Page 256

import pathlib
import dataclasses
import json

@dataclasses.dataclass
class person:
    name:str=""
    id:int=0
    score:float=0.0
   

me=person("mike",42,3.145)

path=pathlib.Path("myTextFile.json")

with path.open(mode="wt") as f:
    s=json.dumps(dataclasses.asdict(me))
    print(s,file=f)
       
with path.open(mode="rt") as f:
    for line in f:
        data= json.loads(line)
        me1=person(**data)
        print(me1)

Page 260

from ast import parse
import pathlib
import xml.etree.ElementTree

data="""
<Books>
    <Book rating="5">
First book on Python
        <Title>
Programmer's Python: Everything is an Object
        </Title>
        <Author>
Mike James
        </Author >
        <Publisher >
IO Press
        </Publisher>
    </Book>
    <Book rating="5">
        <Title>
The Programmer’s Guide To Theory
        </Title>
        <Author>
Mike James
        </Author >
        <Publisher >
IO Press
        </Publisher>
    </Book>
</Books>
"""

path=pathlib.Path("myTextFile.xml")
rootElement=xml.etree.ElementTree.fromstring(data)
tree=xml.etree.ElementTree.ElementTree(rootElement)
tree.write(path)

Page 265

import pathlib
import pickle

path=pathlib.Path("myBinaryFile.pickle")
data=[1,2,{"name":"mike","id":42,"score":3.145}]
with path.open(mode="wb") as f:
    pickle.dump(data,f)
with path.open(mode="rb") as f:
    print(pickle.load(f))
import pathlib
import dataclasses
import pickle

@dataclasses.dataclass
class Person:
    name:str=""
    id:int=0
    score:float=0.0
    def display(self):
        print(self.name)

me=Person("mike",42,3.14)
path=pathlib.Path("myBinaryFile.pickle")
with path.open(mode="wb") as f:
    pickle.dump(me,f)
with path.open(mode="rb") as f:
    me2=pickle.load(f)
me2.display()

Page 268

import pathlib
import pickle

class readFile():
    def __init__(self):
        self.path=pathlib.Path("myFile.txt")
        self.f= self.path.open(mode="rt")
        self.pos=self.f.tell()
    def get(self):
        return self.f.readline()  
    def __getstate__(self):
        self.pos=self.f.tell()
        state = self.__dict__.copy()
        del state['f']
        return state
    def __setstate__(self, state):
        self.__dict__.update(state)
        self.f= self.path.open(mode="rt")
        self.f.seek(self.pos)  
 
read1=readFile()
print(read1.get())

path=pathlib.Path("myTemp.pickle")
with path.open(mode="wb") as f:
    pickle.dump(read1,f)

with path.open(mode="rb") as f:
    read2=pickle.load(f)
print(read2.get())

Page 287

from math import log2
import numbers
import collections.abc


class Tree(collections.abc.Sequence):
 
    class Node:
        def __init__(self,value):
            self.left=None
            self.right=None
            self.value=value

    def __init__(self,value):
        self.root=Tree.Node(value)
        self.len=1
         
 

    def appendBreathFirst(self,value):
        queue=[self.root]
        while len(queue)>0:
            node=queue.pop(0)
            if node.left is None:
                node.left=Tree.Node(value)
                self.len+=1
                return node.left
            queue.append(node.left)
            if node.right is None:
                node.right=Tree.Node(value)
                self.len+=1
                return node.right
            queue.append(node.right)

    def getNode(self,key):
        if key>=len(self) or key<0:
            raise IndexError('Index out of range')
        if key==0:
            return self.root
        row=int(log2(key+1))
        currentNode=self.root
        for r in range(1,row+1):
            k=int((key+1)/2**(row-r)-1)
            if k%2 :
                currentNode=currentNode.left
            else:
                currentNode=currentNode.right
        return currentNode

    def setNode(self,key,node):
        row=int(log2(key+1))
        currentNode=self.root
       
        for r in range(1,row):
            f=2**(row-r)
            k=int((key+1- f)/f)
           
            if k%2 :
                currentNode=currentNode.left
            else:
                currentNode=currentNode.right
        if key%2 :
            currentNode.left=node
        else:
            currentNode.right=node
   
    def __len__(self):
        return self.len

    def __getitem__(self,key):
        if isinstance(key,slice):
            temp=[]
            for i in range(0,len(self))[key]:
                temp.append(self.getNode(i))
            return temp
        if isinstance(key,int):

            return self.getNode(key)
        raise TypeError("invalid index")
                         
    def __setitem__(self,key,value):
        if isinstance(key,slice) and isinstance(value,list):
            for i in range(0,len(self))[key]:
                self.getNode(i).value=value.pop(0)
        elif isinstance(key,int):
            self.getNode(key).value=value
        else:
            raise TypeError("invalid index or value")    
   
    def __imul__(self,m):
        if isinstance(m,numbers.Number):
            for i in range(0,len(self)):
                self.getNode(i).value*= m
        return self

    def __eq__(self,otherTree):
        if not isinstance(otherTree,Tree):
            return NotImplemented

        if len(self)!=len(otherTree):
            return False
        for i in range(0,len(self)):
                if self.getNode(i).value!= otherTree.getNode(i).value:
                    return False
        return True
   
    def __hash__(self):
        temp=[]
        for i in range(0,len(self)):
            temp.append(self.getNode(i).value)
        return hash(tuple(temp))