Book Image

Python 3 Web Development Beginner's Guide

By : Michel Anders
Book Image

Python 3 Web Development Beginner's Guide

By: Michel Anders

Overview of this book

<p>Building your own Python web applications provides you with the opportunity to have great functionality, with no restrictions. However, creating web applications with Python is not straightforward. Coupled with learning a new skill of developing web applications, you would normally have to learn how to work with a framework as well.</p> <p><em>Python 3 Web Development Beginner's Guide</em> shows you how to independently build your own web application that is easy to use, performs smoothly, and is themed to your taste – all without having to learn another web framework.</p> <p>Web development can take time and is often fiddly to get right. This book will show you how to design and implement a complex program from start to finish. Each chapter looks at a different type of web application, meaning that you will learn about a wide variety of features and how to add them to your custom web application. You will also learn to implement jQuery into your web application to give it extra functionality. By using the right combination of a wide range of tools, you can have a fully functional, complex web application up and running in no time.</p>
Table of Contents (19 chapters)
Python 3 Web Development Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – defining new relations: how it should look


The following sample code shows how we would use a Relation class (also available in relation.py):

Chapter7/relation.py

	from os import unlink

	db="/tmp/abcr.db"

	try:
		unlink(db)
	except:
		pass
	
	class Entity(AbstractEntity):
		database=db

	class Relation(AbstractRelation):
		database=db

	class A(Entity): pass
	
	class B(Entity): pass
	
	class AB(Relation):
		a=A
		b=B
	
	a1=A()
	a2=A()
	b1=B()
	b2=B()
	
	a1.add(b1)
	a1.add(b2)
	print(a1.get(B))
	print(b1.get(A))

What just happened?

After defining a few entities, defining the relation between those entities follows the same pattern: we define a Relation class that is a subclass of AbstractRelation to establish a reference to a database that will be used.

Then we define an actual relation between two entities by subclassing Relation and defining two class variables, a and b that refer to the Entity classes that form each half of the relation.

If we instantiate a few entities...