0% found this document useful (0 votes)
19 views34 pages

Development of A Program in Python To Manage A Library

This document presents the development of a program in Python to manage a library. The program is divided into modules to avoid excessive code in individual files. The modules include main, book, member, library, validations, and menu. The document describes the purpose of each module and presents an outline of the planned content for each, as well as the initial pseudo-code. The ultimate goal is to develop a functional program in Python to manage the basic operations of a library such as lending.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views34 pages

Development of A Program in Python To Manage A Library

This document presents the development of a program in Python to manage a library. The program is divided into modules to avoid excessive code in individual files. The modules include main, book, member, library, validations, and menu. The document describes the purpose of each module and presents an outline of the planned content for each, as well as the initial pseudo-code. The ultimate goal is to develop a functional program in Python to manage the basic operations of a library such as lending.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Development of a program in Python

to manage a library
Camilo Bernal

August 19, 2014


Index
1. Introduction 1

2. Analysis preliminary 2

3. Design basic 3
3.1. Module main. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3.2. Module book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3.3. Module partner. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3.4. Module library. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.5. Module validations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.6. Module menu. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

4. Pseudo-code 6
4.1. Module main. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.2. Module book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.3. Module partner. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.4. Module library. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
4.5. Module validations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.6. Module menu10

5. Code Python13
5.1. Module main13
5.2. Module book13
5.3. Module socio14
5.4. Module library15
5.5. Module validations23
5.6. Module menu26

6. Conclusions 31

7. Readings recommended 32

I
1. Introduction

The first thing to clarify is that I am not an expert in programming nor much
less. If you are a beginner (like me) this manual can help you. If you are an expert,
perhaps it will only help you learn how "not to do things like a beginner."

A management problem of a library is presented, in which it is necessary to manage


tasks related to partners and books. The document presents an example of resolution of the
problem starting from a very basic design and concluding with a program in
Python language, which although not perfect, at least works as a prototype.

1
2. Preliminary analysis

A system is required to manage a library. In the library, there are books.


that can be lent to the different members. Members can join the library.
and then request up to three books, which they can keep in their possession for up to
10 days. After the 10 days, the partner is considered overdue and three days are applied.
of sanction for each day of delay. The sanction consists of the impossibility of requesting
new books until it is fulfilled. An affiliated member can request disaffiliation
to the library. New books can be added to those already in the library.

The library initially has no books, so it will acquire them through donations.
actions or purchases. The possibility of starting by lending a single book and simulating is proposed.
the passage of time through a random variable. When the program works, it
it will expand for the management of multiple books.

It is recommended to do incremental programming work, that is, to gradually carry out tasks.
small changes to the program and call the interpreter again for debugging tasks,
so that when the task is completed, there is not an unknown number of errors.

One of the greatest frustrations I have personally experienced while creating programs is
relates to mental blockage when faced with an excessive amount of code and exceptions. It
What I recommend is to create a preliminary design and write all the pseudocode. Before
writing a single line of code, it is very convenient to have perfectly clear how
solve the problem; this little trick will save many hours of work and much
frustration.

2
3. Basic design

The program will be built in modules, in order to prevent individual files from having
an excessive amount of code.

6 modules will be built: <main>, <book>, <partner>, <library>, <validations> and


In the <main> module, the other objects that constitute the pro- are called.
grammar. The module <book> has a class Book responsible for creating objects with the
attributes of a book. The <member> module has the Member class, which creates objects with the
attributes of a library member. The <library> module contains the Bibliote class-
ca, responsible for manufacturing objects for library management. The module <menu>
allows you to choose from a series of options. The <validations> module will serve to validate
give the entries provided by the user of the program. Initially, the ...
modules <main>, <book>, <member>, and <library>, and only when they are finished
the construction of the <validaciones> module will proceed. The <menu> module will be the
last to be built, once all the others are functioning correctly.

The following is a basic outline of what each module will contain.

3.1. Main module

−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Define main function
Explain what it does
Insatnaitet hteLbirarycals ofthe<maneoj_bbioiletca>moduel
Insatnaitehtecals Choosefromthe<menu>moduel
Deploy the options to interact with the user

3.2. Book module

−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Defniecals Book
Explain what it does
Initialize
Build a method to show atributes

3.3. Social module

3
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Defniecals Socoi (nihesrti fromBook)
Explain what it does
Initialize
Build method to show atributes

3.4. Library Module

−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Defniecals Lbirary(Inhesrti fromBookandMember)
Explain what it does
Initialize
Build method showAvailableBooks
Build method showBorrowedBooks
construct method lendBook
construct method receive Book
construct method receiveDonation
construct method buyBook
Build method showMembers
Build method showDelinquentMembers
Build method showMembersWithoutBooks
Build method showMembersWithBooks
Build method to affiliate member
Build a method to unaffiliate a member
construct method show attributes

3.5. Validation Module

−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Defniecals Vadilaotr
Explain what it does
initialize
Build method validateTitle
Build method validateAuthor
Construct method validateEdit
Build method validateName
construct method validateID
Build method validateChoice
Build method to show atributes

3.6. Menu Module

4
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Defniecals Choose
Explain what it does
Initialize
Construct method deployOptions
Construct method executeOption
Construct method showAvailableBooks
Construct method showBorrowedBooks
Build method elPrestarLibro
Build method elRecibirLibro
Build method elDonarLibro
Build method elComprarLibro
Build method elMostrarSocios
Construct method showMembersWithoutBooks
Build method showMembersWithBooks
Build method elMostrarSociosMorosos
Build method and affiliate partner
Build method to disaffiliate member
Build method s a l i r
Build method show atributes

5
4. Pseudocode

Once the basic design is completed, one can start writing the pseudo-code. To
this is sufficient to use the previous design and start making small additions,
that will serve as clues and will prevent mental blockage later on.

4.1. Main module

−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Define main function
Insatnaitet hteLbirarycals ofthe<bilrary>moduel
Insatnaitehtecals Choosefromthe<menu>moduel
Deploy options to interact with the user

4.2. Book module

−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Defniecals Book
Allows building an object with the attributes of a
book
initialize
Initializing title
initialize author
Initialize edition
Initialize state
Build a method to show atributes
Show the object's attributes with a brief
description

4.3. Social module

−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Defniecals Socoi()
Allows to construct an object with the attributes and
methods of a partner of library
to initialize
Initialize name
Initial ID
Initialize state

6
Iinitializebooks
Initializing Penalty
Build method to show atributes
Show atributes with a brief description

4.4. Library module

−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Defniecals Lbirary(Inhesrti fromBook,Memberand\
Validations
Allows constructing an object with the attributes and
methods of a library
Initialize
Initialize the list of available books
Initialize the list of borrowed books
Initialize the list of members_without_books
Initialize the list of members_with_books
Initialize the list of delinquent members
Build method showAvailableBooks
If the available books has books:
Show books with cycling for
Build method showBorrowedBooks
If borowed_bookshasbooks:
Show borrowed books with cycle for
construct method lendBook()
Chooseapanrteratrandom
Indciaet htetelti ofthebookyouwsihto borow
Cary outthegeneralverifcationprocess
If you meet all the requirements, press the \
book

construct method receive Book ( member, book )


Chooseapanrteratrandom
Cary outthegeneralverifcationprocess
Receivebook

create method receiveDonation


Request attributes of the book
Assign status = available
Insatnaitet cals Bookwhti requesetddaat
addinstantiatedobjectto available_books
Thank

7
c o n s t r u i r method buyBook
Request title attributes of the book
Assign status = available
Insatnaitet bookobejctwhti requesetddaat
addinstantiatedobjectto available_books
Build method showMembers
start cycle for on the list socios_con_libros+
members_whtiou_tbooks+denilquen_tmembers
show atributes of the partner for each one
Build method showDelinquentMembers
start the cycle for the list of delinquent members
showmemberatributes
Build method showMembersWithoutBooks
initialize cycle for the list socios_sin_libros
showmemberatributes
Build method showMembersWithBooks
start cycle for the list socios_con_libros
showmemberatributes
Build method to affiliate member
Request name
Request ID
Assign status = normal
Assign books = [ ]
Assignpenalty =0
Instantiate thesocio objectwith therequesteddata
Inrtoduceobejctinparnters_whtiou_tbooks
Build method of un-affiliating member
Request ID
start cycle for partners_without_books
Ifyoufnidyourseflni panrters_whtiou_tbooks:
exratctobejctfrompanrters_whtiou_tbooks
return
start cycle for about partners_with_books
If found in partners_with_books:
Inform that you must return the books first.
what does it have to be able to un-subscribe
return
start the cycle for delinquent partners
If you are in delinquent_partners:
Inform that you are on the list of delinquent members
and that you must wait 10 days before
rty ot unasocaiet onesefl

8
exrtactmemberfromdenilquen_tmembers
changethesatutsartbiuet ofthememberobejctto normal
introduce member to members_without_books
return
If not found:
Inform that the corresponding ID was not found.
nrehteoprat

construct method show attributes


show atributes with a brief description

4.5. Validations Module

−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Defniecals Vadilaotr
Allows you to create objects that validate the inputs of

Initialize
initialize_validateTitle
Initialize_validateAuthor
initialize_validateEdit
initialize_validateName
initialize_validateID
Build method validateTitle
The title must have at least 4 characters
If the tile has at least four characters
e_validateTitle is True
Who
Notify that the title is not valid.
Build method validateAuthor
The author must have at least 4 characters
If the tile has at least four characters
e_validarAutor is worth True
Who
Notify that the author's name is not valid.
Build method validateEditon
The edition number is a positive integer.
Initialize states es_entero, es_positivo to False
Trytoconverttointeger
If it can be done, is_integer is True
If it is integer, verify if it is positive
Iftisi posvite,is_posviteissettoTrue.
If both conditions are met:

9
e_validarEdicion vale True
Who
Notify that the edition number is not valid.
Build method validateName
The name must have at least 4 characters
If the name has at least 4 characters
validateName is valid True
Who
Notify that the partner's name is not valid.
construct method validateID
ID is a positive integer of at least 5 digits
Initialize states is_integer, is_positive, has_5c_\
o_mas aFalse
Trytoconvertintoaninteger
If it is possible, is_integer is True
If it is an integer, check if it is positive
If it is positve, is_positve is True
If the length is at least 5
has_5c_or_more is True
If the three conditions are met:
e_validateID is worth True
Sino:
Notify that the member ID is not valid.
construct validationMethodChoice
The selection must be an integer between 1 and 13
construct method show attributes
show atributes with a brief description

4.6. Menu module

−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Defniecals Choose

Build objects that alow choosing from a menu


of options

Initialize
initialization option

Build method elDesplegarOpciones


Show the options the user has

10
Build method executeOption
Execute the chosen option and return to elDesplegar
Options

Construct method showAvailableBooks


Allows displaying the available books in the library
invoke the mostrarLibrosDisponibles method of the object
library

Build method showBorrowedBooks


Allows showing the books loaned to the members
invoke the mostrarLibrosPrestados method of the object
library

Build method e l PrestarLibro


Gives the possibility to request a book on loan
invoketherequestLoanmethodofthememberobject

Construct method elRecibirLibro


Allows a member to return a book they have.
loan
invoke the returnBook method of the member object

Build method elDonarLibro


Allows donating a book to the library
invoke the donateBook method of the member object

Construct method buyBook


It is used when the library wants to acquire a
bookthroughpurchase
invoke the method buyBook of the object library

Construct method elMostrarSocios


Show all the members of the library.
invoke the method mostrarSocios of the object b i b l i o t e c a

Construct method showMembersWithoutBooks


Show the members who do not have books on loan
invoke the method mostrarSociosSinLibros of the object
library

Build method showMembersWithBooks

11
Show the members who have books on loan
invoke the method mostrarSociosConLibros of the object
library

Construct method showDelinquentMembers


Show the partners who did not return the books in
the established deadlines
invoke the mostrarSociosMorosos method of the object
library

Build method and affiliate member


Allows to register a new member
invoke the method a f f i l i a t e S p o n s o r of the object l i b r a r y

Build method to disaffiliate member


Allows to deregister a member from the library.
invoke the method d e s a f i l i a r S o c i o of the object b i b l i o t e c a

Build method to show atributes


Displays the object's attributes with a brief description

12
5. Python Code

I think it's not useless to repeat it: Code is written ONLY when you already have it.
any idea on how to solve the problem and if a solution has been proposed with some
clarity. Jumping into writing code thoughtlessly is the perfect recipe for the
frustration: I hope they don't make that mistake.

The following is a proposal of the code in Python, it may be riddled with


errors, naivety and inconsistencies, but even from that we can learn, and learn
it is the only objective of this document.

5.1. Main module

#! / usr / bin /env python


#encoding : Latin1
import library
import menu
def main ( ) :

Functionthatallowscallingothercomponentsofthe
program to manage a library

Instantiate the Library object from the <library_management> module


l a b i b l i o t e c a = b i b l i o t e c a .Library ()

Instantiate the object Choose from the <menu> module


options_menu = [Link](library)

Deploy options to interact with the user


menu_of_optionsdi
. splayOptions()

main()

5.2. Book Module

#! /usr/bin/env python
The provided text does not contain any translatable content.

class Book () :

13
Allows constructing an object with the attributes of a book

Initializing
def __init__(self, title, author, edition_number, status='available'):
title
[Link] = author
self.n_edicion = n_edicion
[Link] = state

Build a method to show attributes


def __str__(self):
Show the object's attributes with a brief description
Attributes of the book
Title:
Author:
Edition number:
State:
return s

5.3. Social module


#! /usr/bin/env python
#encoding : Latin1
class Socio ( ) :

Allows constructing an object with the attributes and methods of a library member

Initialize
def __init__(self, name, ID, status = 'normal', books = [], penalty = 0):
[Link] = name
[Link] = ID
[Link] = state
[Link] = books
penalization

#Build method show a t r i b u t e s


def __str__(self):
Show attributes with a brief description
Attributes of the partner
Name:
s += "\nID : " + str([Link])
s += "\nState: " + str([Link])
s += "\ nBooks : "
−∗−∗−∗−∗−∗−∗−∗−∗−∗−∗−"
for in self. books:
s += s tr ( i )
−∗−∗−∗−∗−∗−∗−∗−∗−∗−∗−\n"

14
Penalty:
return s

5.4. Library module


#! /usr/bin/env python
The provided text is not translatable content.

import random
import validations
from book import Book
from s o c i o import Socio
Define class Library()
class Bibliotec (Book, Member, validations. Validator):

Allows constructing an object with the attributes and methods of a library

Initialize
def __init__(self):
self.libros_disponibles=[]
s e l f .libros_prestados = [ ]
self.socios_sin_libros=[]
s e l f .socios_con_libros = [ ]
self.socios_morosos = [ ]

Build method showAvailableBooks


defshowAvailableBooks(self ):
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
It hasbeenchosento showtheavaalibel books
If the available books have the book:
if len(self.libros_disponibles) > 0:
Show books with cycle for
for in self. available_books:
prniti
else:
***There are noavailable books
Final visualization process completed
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\n"
Press ENTER to continue
raw_input()

#Build method showBorrowedBooks


def showBorowedBooks(self):
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\n"
It hasbeenchosento showtheborowedbooks
#If borrowed_books exist:
if len(self.libros_prestados) > 0:
Show borrowed books with cycle for
for self. borrowed_books:
prniti
else
***Nobooksborowed
Final visualization process completed

15
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\n"
Press ENTER to continue
raw_input()

#Build method lendBook ( member , title )


deflendBook(sefl):
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Abookhasbeenchosento borow
Choose one of the partners
sefls.ocois_sni_bilros+sefls.ocois_con_bilros+
self . delinquent_members
if len(list_of_members) > 0:
s o c i o = [Link] ( list_of_partners )
else:
There are nomembers
Press ENTER to continue
raw_input()
return
Indicate the title of the requested book
while True :
Exact title of the book:
if [Link](title): break
Carry out the general verification process
#−Initializing state indicators
available_book
(False, False, False)
#−C a r r y o u t t h e v e r i f i c a t i o n p r o c e s s
#−−Verify if the title is in available books.
for self. available_books :
if title == [Link]:
l i b r o _ d i s p o n i b l e = True
book = I
#−−Verify that the member has not reached the limit of books.
can have
if len([Link]) == 3:
True
#−−Check if the member is in default
if [Link] == 'delinquent':
socio_moroso = True
The book is not available.
if not available:
Indicate to the partner that the book is not available due to it.
moment
Dear partner: the book is not available
return
#The partner has reached the limit of books
iflimite_books:
Indicate to the partner that he has 3 books in his possession and that he must
to devolve them
Thememberarleadyhas3booksonloan
Youmustreutrnhtebooksyouhaveinordertoreques.t
another loan
Press ENTER to continue

16
raw_input()
return
If it is a delinquent partner
the socio_moroso:
Indicate to the partner that they are in default, and that they must

return the books you have


The partner is in a delinquent state.
Reutrnhtebooksthatyouhave
and wait for the penalty to finish
Extract s o c i o from delinquent partners
self.socios_morosos.remove(socio)
Verify the penalization attribute of the partner
[Link]
#Penalty > 1:
if penalization > 1 :
restart 1 the attribute penalty
penalization−= 1
penalization
I introduce socio again to
#delinquent_members
self.socios_morosos.append(socio)
If penalization equals 1:
else :
Equal attribute p and penalization zero
[Link]=0
Change member status attribute to normal
s o c i o .estado = ’ normal ’
Introducing partners without books
self.socios_sin_libros.append(socio)
return
If all the requirements are met to lend you the book
if not socio_moroso and not limit_libros and libro_disponible:
Extract book from available books
self.available_books.remove(libro)
Change status from available to borrowed
[Link] = ' borrowed '
I n t r o d u c e b o o k s in borrowed_books
self.books_borrowed.append(book)
Extract partner from partners_without_books (if available)
i f s o c i o in s e l f . s o c i o s _ s i n _ b o o k s :
self.socios_sin_libros.remove(socio)
Extract s o c i o from socios_con_libros (if it exists)
i f s o c i o in s e l f . socios_con_libros :
self.socios_con_libros.remove(socio)
Add book to the books attribute of the member object
s o c i o . l i b r o s . append ( b o o k )
I introduce partners with books
self.socios_con_libros.append(socio)

Print "\nBorowed book: ", [Link]


print " Member to whom it was lent: ", [Link]
Loan process successfully completed
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\n"

17
Press ENTER to continue
raw_input()

#Build method r e c e i v e B o o k ( member , b o o k )


def receiveBook(self):
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\n"
Youhavebeenchosento recevieabook
Choose one of the partners with books
if len(self.socios_con_libros) > 0:
s o c i o = [Link] ( s e l f . socios_con_libros )
else
There are nomembers with books
Press ENTER to continue
raw_input()
return
Choosing a book
book = [Link]([Link])
Carry out general verification process
#−Initial state indicators
(socio_moroso , entrega_puntual ) = ( False , False )
#−−Verify if the partner is a defaulter
I am socio in self. Delinquent partners:
socio_moroso = True
#−−Verify timely delivery
if [Link](xrange(1, 16)) <= 10:
entrega_puntual=True
#If the partner is overdue
i f socio_moroso :
Extract borrowed books
self.books_borrowed.remove(book)
Change status of book to available
available
I n t r o d u c e l i b r o in l i b r a r y _ a v a i l a b l e s
self.books_available.append(book)
#Extract s o c i o of delinquent partners
self.socios_morosos.remove(socio)
Remove the book from the books attribute of the member object.
s o c i o . b o o k s . remove ( b o o k )
I reintroduce troublesome partners again
self.socios_morosos.append(socio)

If the partner is not delinquent


if not socio_moroso :
Extract the book from borrowed_books
s e l f . borrowed_books . remove ( book )
Change status of book to available
available
Introducing the available books
self.books_available.append(libro)
#Extract partner or socio from partners_with_books

self.socios_con_libros.remove(socio)
Remove the book from the books attribute of the member object.
s o c i o . l i b r o s . remove ( b o o k )

18
I introduce social partners without books do not have books
in another case, introduce it in partners_with_books
if len([Link]) == 0:
self.socios_sin_libros.append(socio)
else:
self.socios_con_libros.append(socio)

If the delivery of the book is not punctual


if not timely_delivery:
Extract the book from borrowed_books (if possible)
i fl i b r o in self . books_borrowed :
self.borrowed_books.remove(book)
Change status from book to available
book
Introducing the book available books (if not)
if libro not in self.libros_disponibles:
self.books_available.append(book)
Extract partner from partners_with_books
self.socios_con_libros.remove(socio)
Remove the book from the books attribute of the member object.
( yes it can )
the book in society. books:
s o c i o . b o o k s . remove ( b o o k )
Change the status of the member object to delinquent
s o c i o .status = 'delinquent'
Add penalties to the partner
s o c i o . p e n a l i z a c i o n +=random .choice ( xrange (1 , 11))
Introducing socio in delinquent partners
self.socios_morosos.append(socio)

prnit"\nEllibrorecibidoes:",[Link]
prnit"Theparnterwhodevileredit si:",[Link]
Final reception process completed
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Press ENTER to continue
raw_input()

Build method receiveDonation


defrecevieDonaoitn(sefl):
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Youhavechosento recevieadonaoitn
Request Title
while True:
raw_input ("\nTitle : \n ")
if [Link](title): break
Request authorization
while True :
Author:
if [Link](author): break
Request edition number
while True :
Edition number:
if [Link](n_edicion): break

19
Instantiate object book with requested data
new_book=Book(title,author,edition_number)
Add instantiated object to available_books
self.available_books.append(new_book)
Thanking
Thankyouvery muchforyourdonaoitn.
Thebookwlibeavaalibel fortheservcieofthecommunyti
Press ENTER to continue
raw_input()

Build method buyBook


def buyBook(self):
pnrit"n\"−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
It hasbeendecdiedto buyabook
Apply for a title
while True :
Title :
if [Link](title): break
Request authorization
while True :
Author:
if [Link](author): break
Request edition number
while True :
Edition number:
if [Link](n_edition): break
Instantiate book object with requested data
new_book=Book(title,author,edition_number)
Add instantiated object to available_books
self.available_books.append(new_book)
Purchase process finalized
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\n"
Press ENTER to continue
raw_input()

Build method showMembers


def showMembers(self):
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\n"
It hasbeenchosento showparnters
parnters_silt=seflb.ooks_whti_parnters+seflp.arnters_whtiou_tbooks+
self.default_debtors
if len(list_of_members) > 0:
for in partners_list:
prniti
else:
***There are nomembers
Final visualization process completed
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Press ENTER to continue
raw_input()

Build method showDelinquentMembers


def showDelinquentMembers(self):

20
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Thedenilquentmembers havebeenchosento beshown
if len(self.socios_morosos) > 0:
for iin self. bad_debtors:
prniti
else:
***There are nodelinquentmembers
Final visualization process completed
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Press ENTER to continue
raw_input()

Build method showMembersWithoutBooks


defshowMembersWithoutBooks(self):
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\n"
It hasbeenchosento showthemembers whtioutbooks
if len(self.socios_sin_libros) > 0 :
for in self. socios_sin_libros:
prniti
else
***There are nomembers withoutbooks
Final visualization process completed
prnit"n\−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Press ENTER to continue
raw_input()

Build method showMembersWithBooks


def showMembersWithBooks(self):
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\n"
Themembers whti bookshavebeenseelcetdto beshown
if len(self.socios_con_libros) > 0:
for in self. partners_with_books:
prniti
else:
***There are nomembers with books
print " Final visualization process completed "
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\n"
Press ENTER to continue
raw_input()

#Build method to affiliate member


def a familar socio(self):
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\n"
Amemberhasbeenchosento merge
Request name
while True :
name = raw_input("\ nName of the new member:\ n ")
if [Link](name): break
Request ID
while True :
ID = input("\nNew member ID:\n")
if [Link](ID): break
To instantiate social objects with the requested data

21
new_member
Introducing object in partners_without_books
self.socios_sin_libros.append(nuevo_socio)
Finalization process completed
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Press ENTER to continue
raw_input()

Build a method to disaffiliate a member


def defamilarizeSocial(self):
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\n"
Amemberhasbeenchosenfromthefamyli
Request ID
while True :
ID = input(" Member ID of the affiliate: ")
if [Link](ID): break
Search in partners without books
for in self. socios_sin_libros :
if ID == [Link] :
self.socios_sin_libros.remove(i)
return
Search in partners_with_books
for self. partners_with_books :
if ID == [Link] :
Youmustreutrn htebooksyouhavein orderto
desafiliarse
return
Search in delinquent_partners
for self. delinquent_partners:
if ID == [Link] :
The member is in default, they owe
wait 10days
in order to unsubscribe
Extract s o c i o from overdue_partners
self.socios_morosos.remove(i)
Change the status attribute of the partner object to normal
i .state = ' normal '
I introduce partners without books
self.socios_sin_libros.append(i)
return
TheID [Link]/Sheis notregistered.
Press ENTER to continue
raw_input()

Build method to show attributes


def __str__(self):
Show attributes with a brief description
Library Attributes
Available books:
for self. available_books:
s += s t r ( i )
−∗−∗−∗−∗−∗−∗−∗−∗−∗−∗−∗−"
Books borrowed:

22
for self. borrowed_books :
s += s t r ( i )
−∗−∗−∗−∗−∗−∗−∗−∗−∗−∗−∗−"
Partners without books:
for myself. socio_sin_books:
s += s str(i)
−∗−∗−∗−∗−∗−∗−∗−∗−∗−∗−∗−"
Members with books:
for self. partners_with_books:
s += s t r ( i )
−∗−∗−∗−∗−∗−∗−∗−∗−∗−∗−∗−"
Late members:
for in self. delinquent_members:
s += s str(i)

return s

5.5. Validations Module


#! /usr/bin/env python
#encoding : Latin1
class Validator() :

Allows you to create objects that validate user inputs

Initialize
def __init__(self):
self.e_validateTitle = False
self.e_validateAuthor = False
False
[Link] = False
self.e_validateID = False
s e l f .e_validarEleccion = False

Build method to validate Title


def validate_tile(self, tile):
The title must have at least 4 characters
if len(title) >= 4:
s e l f .e_validarTitulo = True
else:
Notify that the title is not valid
Theenetredtelti si notvadil
Remember that you must have at least 4 characters
False
return selfe._validateTitle

Build method validateAuthor


def validateAuthor(self, author):
The author must have at least 4 characters
if len(author) >= 4:
self.is_validateAuthor = True

23
else:
The authors' name is not valid
Remember that you must have at least 4 characters
self.e_validateAuthor = False
return self.e_validateAuthor

Build method validateEdit


defvadilaetEdnitg(sefl, edoitn_number):
The edition number is a positive integer.
Initialize states is_integer, is_positive to False
(es_entero , es_positivo ) = ( False , False )
Try to convert to integer
try:
n_editing=int(n_editing)
es_entero=True
excep:t
The entered edition number is not valid
Remember that it must be a positive integer
s e l f .e_validateEdition = False
return selfe._validateEditon
If it is an integer, check if it is positive
if edition_number > 0:
es_positvo=True
If it meets both conditions:
if is_integer and is_positive:
self.e_validateEdit = True
else :
The entered edition number is not valid
Remember that it must be a positive integer
self.e_validateEditing = False
return selfe._validateEdit

#Build method validateName


def validateName(self, name):
The name must have at least 4 characters
The name must have at least 4 characters
if len(nombre) >= 4:
s e l f . e_validarNombre = True
else:
The entered partner name is not valid
Remember that you must have at least four characters
False
return self.e_validateName

construct method validateID


def validateID(self, ID):
ID is a positive integer with at least 5 digits
Initialize states is_integer, is_positive, has_5c_or_more to False
(es_entero , es_positivo , tiene_5c_o_mas ) = ( False , False , False )
Try to convert into an integer
try:
ID = int(ID)
#Yes, it can be done, is_entero is True

24
es_entero=True
excep:t
Notify that the member ID is not valid
TheenetredmemberID si nicorec.t
Remember that it must be a positive integer of at least
5cifras
self.e_validarID = False
return s e l f . e_validarID
If it is an integer, check if it is positive.
if ID > 0:
If it is positive, is_positive is True
es_positvo=True
If the length is at least 5
if len(str(ID)) >= 5:
# tiene_5c_o_mas vale True
tiene_5c_o_mas = True
If it meets the three conditions:
if is_integer and is_positive and has_5_or_more:
#e_validarID is True
self.e_validateID = True
else
Notify that the ID of the partner is not valid.
TheenetredmemberID si nicorec.t
Remember that it must be a positive integer of at least
5 digits
self.e_validateID = False
return selfe._validateID

Build method to validate election


def validateElection(self, election):
The election is a whole number between 1 and 13
Initializing state indicators to False
(es_entero , rango_valido ) = ( False , False )
#−Tr y t o c o n v e r t t o a n i n t e g e r
#
try:
eleccion=int(eleccion)
es_entero=True
excep:t
The chosen option is invalid
Remember that you must enter an integer between 1 and 13
s e l f .e_validateChoice = False
return selfe._validateElection
#−F i n d o u t i f t h e e l e c t i o n i s w i t h i n t h e a p p r o p r i a t e r a n g e
if selection in range(1, 14):
rango_valido=True
If both conditions are met
if is_integer and valid_range :
True
return selfe._validateChoice

#Build method show attributes


def __str__(self):

25
Show attributes with a brief description
Validator object attributes
s += " Book title status: " + str(self.e_validarTitulo)
Author status of the book:
State of the book's edition:
s += "\nStatus of Member's Name: " + str(self.e_validateName)
s += "\nMember ID status: " + str(self.e_validarID) + "\n"
return s

5.6. Menu module


#! /usr/bin/env python
#encoding : Latin1
import library
import validations
class Choose ( [Link] ):

Build objects that allow choosing from a menu of options

Initializar
def __init__(self, library):
[Link] = biblioteca
s e l f .opcion = 13
Build method deployOptions
def displayOptions(self):
Show the options that the user has

=======================
prni−
t −−−−−−−−−−LIBRARY MANAGEMENT−−−−−−−−−−−−−−"
"======================="tnirp
Please choose an option:
print"\n1− − −Show available books
2− − −Show borrowed books
3− − −To l e n d a b o o k
4− − −R e c e i v e b o o k
print"5− − −R e c e i v e d o n a t i o n
pnrit"6"− − −Buy book
print"7− − −Show partners
8− − −Show partners without books
pnrit"9"− − −Show members with books
10− − −Show delinquent members
prni"t11− − −a familiar socio
prni"t12− − −To desafiary socio
prni"t13− − −SALR
I

26
while True :
EnteroptionandpressENTER:
input()
if [Link](option): break
[Link] = int(option)
[Link]()
if [Link] != 13:
[Link]()
Build method executeOption
def executeOption(self):
if [Link] == 1:
[Link]()
else if [Link] == 2:
[Link]()
elif [Link] == 3:
[Link]()
if [Link] == 4:
[Link]()
if [Link] == 5:
[Link]()
if [Link] == 6:
[Link]()
if [Link] == 7:
s e l f .elShowPartners ()
if [Link] == 8:
[Link]()
elif [Link] == 9:
[Link]()
elif [Link] == 10:
[Link]()
else [Link] == 11:
[Link]()
if [Link] == 12:
s e l f . e l D e s a f i l i a r S o c i o()
if [Link] == 13:
[Link]()

Build method displayAvailableBooks


defshowAvailableBooks(self):
Allows displaying the available books in the library
invoke the method mostrarLibrosDisponibles of the object b i b l i o t e c a
[Link]()

27
Build method elMostrarLibrosPrestados
defshowBorowedBooks(self):
Allows showing the books lent to the members
invoke the method showBorrowedBooks of the object library
[Link]()

Build method elPrestarLibro


deflendBook(sefl):
Gives the possibility to lend a book to a partner
invoke the method lendBook of the object library
[Link]()

#Construct the method elRecibirLibro


defrecevieBook(sefl):
Allows a member to return a book they have on loan
invoke the method r e c e i v e B o o k of the object l i b r a r y
[Link]()

#Build method elDonateBook


def giveBook(self):
Allows you to donate a book to the library
invoke the method receiveDonation of the object library
[Link]()

Build method theBuyBook


def buyBook(self):
It is used when the library wants to acquire a book through
purchase
invoke the comprarLibro method of the object b i b l i o t e c a
[Link]()

Build method showMembers


defshowMembers(self):
Show all the members of the library
invoke the mostrarSocios method of the b i b l i o t e c a object
[Link]()

Build method showMembersWithoutBooks


defshowMembersWithoutBooks(self):
Show the members who do not have books on loan
invoke the method mostrarSociosSinLibros of the object biblioteca

28
[Link]()

Create method showMembersWithBooks


def showMembersWithBooks(self):
Show the members who have books on loan
invoke the method mostrarSociosConLibros of the object b i b l i o t e c a
[Link]()

Build method elMostrarSociosMorosos


def showDelinquentMembers(self):
Show the partners who did not return the books on time
established
invoke the method mostrarSociosMorosos of the object b i b l i o t e c a
[Link]()

Construct method and affiliate partner


def the social afilate(self):
Allows to register a new member
invoke the method to affiliate member of the object library
[Link]()

Build method and disaffiliating member


def e l D e s a f i l i a r S o c i o ( s e l f ) :
Allows to remove a member from the library.
invoke the method of unfollowing member of the library object
[Link]()

Build method S to exit


def exit(self):
Menu of options sale
−−−−−−−−−−−−−−−−−−−−−−−−−−
Haelegidosalir
Press ENTER to continue
raw_input()
return

Build method to show attributes


def __str__(self):
Displays the object's attributes with a brief description
−∗−∗−∗−∗−∗−∗−∗−∗−∗−∗−∗−∗−∗−*
Attributes of the menu object and an instance of the object
library

29
Option:
Attributes of the instantiated library object:
s += str([Link])
return s

30
6. Conclusions

Before embarking on the writing of code, it is advisable to conduct a basic analysis, a


preliminary design and the writing of pseudocode, which can increase
productivity and reduce frustration in the debugging process.

Python is a great language for learning to program, and if this is added to


adds a code conveniently distributed in modules and functions along with the
relevant comments, it is possible to reduce the likelihood of 'mental block'
that our brain suffers in front of abstract and counterintuitive objects.

Incremental programming involves writing a few lines and running tests.


constants about them. This greatly facilitates the subsequent tasks of
purification.

31
7. Recommended readings

There are several good manuals online. Personally, I recommend that you search on
google the PDF files with the following titles:

Learn to Think Like a Programmer with Python

Dive Into Python 3

The Python Tutorial

Python Reference Manual

32

You might also like