How can I improve this code?

code.py
# 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

  • Manish Goyal
    Manish Goyal
    you are learning python now?
  • Aadit Kapoor
    Aadit Kapoor
    Manish Goyal
    you are learning python now?
    I know a little bit of Python. I wrote this script just for fun.
  • Manish Goyal
    Manish Goyal
    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
    Have you seen both the files?

You are reading an archived discussion.

Related Posts

Renault unveiled its new 2015 Twingo and, amazingly, it’s rear-engine and rear-wheel drive (RWD). Moving the engine to the back, Renault says, has given the new Twingo “pleasantly agile handling”,...
Jaguar, the British multinational car manufacturer, has now taken the wraps off its upcoming sports sedan - Jaguar XE. We were excited in March to know about the car's basic...
Please take a minute to look at my PyPi module "util_random" Here is the link-: https://pypi.python.org/pypi?name=util_random&version=1.0.0.0&:action=display
We just moved to our new office. Have a look !
Aadit Kapoor submitted a new project: Util_Random (Random Number Generator) - A simple random generation module. Please take a minute to look at my PyPi module "util_random" Here is the...