How can I improve this code?

Aadit Kapoor
@aadit-kapoor-EBxnXT โ€ข Oct 18, 2024

<b>code.py</b>

# A simple notebook application
import code_util as util
# defining a class
available_bookclasses = ('private', 'public', 'none')
class notebook:
    # Varibles
    book_name = ''
    book_class = available_bookclasses[2]
    item_name = ''
    book_code = util.assign_code()
    items_list = []
   
    def __init__(self,name):
        self.book_name = name
    def _get(self,items):
        if ((items < 0)):
            print ("Invalid number of items..")
       
        for i in range(items):
            self.item_name = input("Enter item name: ")
            self.items_list.append(self.item_name)
    def count(self):
        return (len(self.items_list))
    def show_summary(self):
        print ("Name of the stored book: %s\n" %(self.book_name))
        print ("Secret code: %d\n" %(self.book_code))
        print ("Printing items in the list...\n")

        for i in range(self.count()):
            print ("Items [%d]: %s\n" %(i,self.items_list[i]))
    def create_copy_code(self):
        return (self.book_code)
    def __str__(self):
        self.show_summary()

    def change_class(self,class_name):
        if class_name in available_bookclasses:
            self.book_class = class_name
        else:
            util.error("Search failed: %s\n" % class_name)

code_util.py

# Utility Module
import random
import math
import sys
def error(*arg):
    print ("Number of errors: %d\n" % len(arg))
    for i in range(len(arg)):
        print ("Error [%d]: %s\n" %(i,arg[i]),file = sys.stderr)

def assign_code():
    '''
        () -> int
        return the assigned number

        >> assign_code()
        10q0
        >> assign_code()
        547a4
    '''

    # Varibles used in the assign_code()
    value_to_use = [22,3,4,2,3,2,3,3,111,2232,44,323,90909,9,23]
    number_to_use = [1,2,3,42,5,3,5,3,4,3,32,4,2,2,4]
    assigned_code = '' # The end result

    big_number = number_to_use[0] # let big_number be the largest number
    big_value = value_to_use[0] # let big_value be the largest number

    try:
        for n in range(len(number_to_use)):
            if (number_to_use[n] > big_number):
                big_number = number_to_use[n]
        n_use = random.randint(0,big_number)
        n_use*=100
        for v in range(len(value_to_use)):
            if (number_to_use[v] > big_value):
                big_value = value_to_use[v] # Largest value
        # Making it more harder
        v_use = random.randint(0,big_value)
        v_use*=100
    except ValueError:
        error("Code Generation")

    if (n_use > v_use):
        v_use+=n_use


    assigned_code = int(math.sqrt(random.randint(n_use,v_use))) # Creating code...
    assigned_code = math.sqrt(assigned_code * 100 * 100)
   
    return (int(assigned_code))

How can I improve this code?

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Manish Goyal

    @manish-r2Hoep • May 17, 2014

    you are learning python now?

  • Aadit Kapoor

    @aadit-kapoor-EBxnXT • May 17, 2014

    Manish Goyalyou are learning python now?

    I know a little bit of Python. I wrote this script just for fun.

  • Manish Goyal

    @manish-r2Hoep • May 17, 2014

    you know a little of python and you can create classes ๐Ÿ˜ฒ๐Ÿ˜ฒ amazing

    nevermind, it seems you have written a simple book class that will return book information

    There is nothing for optimization in this code, if you really wanna learn code optimization ,that just go through scheduling, travelling sales man problems

  • Aadit Kapoor

    @aadit-kapoor-EBxnXT • May 17, 2014

    Have you seen both the files?