Archive

Archive for August, 2019

Python Project: Ant Escaping Path

August 29, 2019 Leave a comment


Python simulation project
Python, Graphics and simulation

In this project we assume that we have a square pad and we put an ant in the center of this pad, we let the ant to walk, once it reach any eadgs of the square we move it with our hand to the middle again, say we doing this for x times and each time we colored the ant foots with gray level (light to dark) color. Our project is to write a code to simulate this task (ant movement). 

Enhancement: Here are some enhancement ideas:
1. We can use the Pi or Golden Ratio for the variables.
2. Also we can set a memory facto so with each time we move the Ant to the center the memory will increase so it may use the same path.
3. For the memory, we can set a position (x,y) as a food, and each time it reach the food it’s memory will increase.


The Code

# Create in 27/8/2019 .

import turtle
import random

screen = turtle.Screen()
screen.setworldcoordinates(-700,-700,700,700)
screen.tracer(5)

t1=turtle.Turtle()
t1.speed(0)
t1.penup()

# colors varibles
# r=240, g=240, b=240 is a light gray color

r=240
g=240
b=240
t1.pencolor(r,g,b)

cf = 3 # color increasing factor
ff = 0 # ant forward moving factor
#screen.bgcolor(“black”)

def rand_walk(x,the_t):

the_t.pendown()

# The color will be darker each time

the_t.pencolor(r-(x*cf),g-(x*cf),b-(x*cf))

the_t.forward(20+(ff*x))

def ant_walk ():

x = 1

while x < 7 : # Moving the Ant to the center 7 times

rand_walk(x,t1)

if random.randrange(1,100) %2:

t_ang = random.randrange(10,45)

t1.right(t_ang)

else:

t_ang = random.randrange(10,45)

t1.left (t_ang)

# if the ant reach the square boards then we move it to the center.

if (t1.xcor() > 650 ) or (t1.xcor() 650 ) or (t1.ycor() < -650 ):

t1.penup()

t1.goto(0,0)

x = x + 1

# Calling the Function
ant_walk()


Here is an GIF file



Follow me on Twitter..





Advertisements

Python: Orders Manager P4

August 27, 2019 Leave a comment


Learning : Orders Management System using Python and Pandas
Subject: File Exists and Adding new Record

In the last post of our system, we develop the file_exists function, the function is checking if the file not exists then will create ond and enter a dummy data. Now we need to add a code to the application body that call this function and if the file exists the application will run without applying the or creating any file. Here is the code in the application body:

Header here

if file_exists() != ‘exit’ :

# calling the menu

user_enter = the_menu()

”’
Validation: If the user enter any thing else than numbers
or (q for quit) nothing will happen.
”’

while user_enter !=’q’ or ‘Q’ :

if user_enter in [‘q’,’Q’] :

print(‘\n You select to Exit the application.’)

save_it = input(‘\n Do your want to save your work/changes [y or n] ? ‘)

if save_it in [‘y’,’Y’]:

save_the_df (df)

break

elif user_enter not in [‘1′,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9’] :

user_enter = the_menu()

else:

user_choice(user_enter)

user_enter = the_menu()


In this post we will talk about the Adding new record function, since we may start from new file we need to enter some records in our data file. Here is the def add_new_record() that will help us to enter our data.

Add New Record Function

def add_new_record(old_df):

clear() # To clear the terminal.

app_header()

# First we will fetch the columns from the df

col_list = []

for each in old_df.columns :

col_list.append(each)

print(col_list)

# Get max id and increase it by 1

next_id = old_df[‘order_no’].max()+1

new_row={}

# let user enter the new record.

print(‘\n Enter the data for each field then press Enter.\n’ )

print(‘ If you just press Enter NaN will be entered.’)

for each in col_list:

if each !=’order_no’:

print(‘ Enter data for ‘,each)

new_row.update({each:(input(‘ : ‘))})

new_row.update({‘order_no’:next_id})

old_df = old_df.append([new_row])

for each in col_list :

if (old_df.loc[old_df[‘order_no’] == next_id, each][0]) ==”:

(old_df.loc[old_df[‘order_no’] == next_id,[each]]) = float(‘NaN’)

print(‘\n New Record added successfully..\n’)

# print out last 5 rows to show the new record.

print(‘\n The new record in the df..\n ‘,old_df.tail(5))

global df # Reset the df as global variable

df = old_df

input(‘\n\n\n\n ** Press any key to continue .. . . ‘)


In the coming post, we will work on the date validation function also the user choice loop so we can run the application and test it.



Follow me on Twitter..





Python: Orders Manager P3

August 25, 2019 Leave a comment


Learning : Orders Management System using Python and Pandas
Subject: Data File and Adding new Record

In This system and once the user run the application we will check if the Data file exists or not. If the file exists then the system will run, otherwise the application will guide the user to some questions to create the new file. Then we will talk about adding new records to our file.

First time run: Each time we run the application the system will chick for the file, at this version we will have only one data file we will call it “orders_dataframe.csv” if the file exists the application will continue and the file will be loaded automaticly, if not, then the first choice of our menu will be called. Function called “create_file()” will run, we will inform the user that the file is not there and if he want to create a new file. If the user select Yes, the file will be created and a dummy row (id = 0) will be added. If the user select No, we will show a message then if any key pressed the system will quite. .. Let’s see the code ..

File Exists check point
def file_exists():

# Check if the data file not exists create one

if not (os.path.exists(‘orders_dataframe.csv’)):

no_file =’o’

while no_file not in [‘y’,’Y’,’n’,’N’]: # Validation for user input

clear() # To clear the terminal.

app_header()

no_file = input(‘\n The file ”orders_dataframe.csv” is not exists, Do you want to create new one: [ y , n ] ‘)

if no_file in [‘y’,’Y’]: # Validation for user input

create_file() # Call the function create_file

return

elif no_file in [‘n’,’N’]: # Validation for user input

print(‘\n You select not to create a data file, so the system will Exit. ‘)

input(‘\n\n Press any key …’)

return ‘exit’


Validation:
To keep asking the user for his input until he enters one of [ y,Y,n,N]

while no_file not in [‘y’,’Y’,’n’,’N’]:

no_file = input(‘\n The file ”orders_dataframe.csv” is not exists, Do you want to create new one: [y,n] ‘)


Last, I am thinking to add a header for our app, so this header will be at the top of all out screens. Here it is ..

Application Header
def app_header():

print(‘\n ********************************’)

print(‘ ** Orders Managment System **’)

print(‘ ** App V.08-19 **’)

print(‘ **********************************\n’)


In the next post we will look at the Validation on the File Exists check and create file function, also first row added to our dataframe.



Follow me on Twitter..





Python: Orders Manager P2

August 22, 2019 Leave a comment


Orders Managment System
Subject: Main Menu and creation of the data file – P2

First thing we will talk about the menu, in this post we will cover the Main Menu and the user choice. Also we will add some validation.

Main Menu: Here is a list of what we will have in the main menu with some descriptions for each choice.
Load File: We will name the file as “orders_data.csv” so once we start the application the system will check if the file exist or not, if Yes then the application will load it automatically as DataFrame (df) if not thats mean you are running the app for the first time, so we will go through file creating process.

Show Data: In this option we will have another sub menu page having:
1. Show all data.
2. Show sample data.
3. Show last 5 records.

Sort: Here we will have sorting as columns that we have, the user will select a column.

Search: We can here search for an order by Date, Price, Quantity or details, also we will do groping for the data.
Missing Data: To show us how many missing data we have so we can fill them.
Add New Order: To add new order to the system.
Edit a Record: To Edit/change a record.
Delete a Record: To delete a record from the DataFrame.
Save: To save what ever you do to the DataFrame.

The Main Menu

def the_menu ():

print(‘\n ::—–{ The menu }—-::’)

print(‘1. Create New csv File’)

print(‘2. Show Data’)

print(‘3. Sort.’)

print(‘4. Search.’)

print(‘5. Missing Data’)

print(‘6. Add New Record.’)

print(‘7. Edit a record.’)

print(‘8. Delete a Record.’)

print(’9. Save the File.’)

return input(‘\n Select from the menu (”q” to quit): ‘)


Main while loop: In the application body we will use a while loop to control the user input and run the function he select. Unlike what we did In the zoo managment system, we will add a validation on the user input as:

Validation:
1. If the user enter any thing else than numbers (1 to 9) or ([q – Q] for quit) nothing will happen.
2. If the user select (q to quit) then we will ask if he want to save before Exit.



Here is the code …

Main while loop
# calling the menu
user_enter = the_menu()

”’
Validation: If the user enter any thing else than numbers
or (q for quit) nothing will happen.
”’
while user_enter !=’q’ or ‘Q’ :

if user_enter in [‘q’,’Q’] :

print(‘\n You select to Exit the application.’)

save_it = input(‘\n Do your want to save your work/changes [y or n] ? ‘)

if save_it in [‘y’,’Y’]:

save_the_df (df)

break

elif user_enter not in [‘1′,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9’] :

user_enter = the_menu()

else:

user_choice(user_enter)

user_enter = the_menu()


Here is a screen shot for the code..




In this post we cover the Main Menu and the main while loop we need to call the functions, in the coming post we will create the data file and Adding new record.



Follow me on Twitter..





Python: Orders Manager P1

August 20, 2019 Leave a comment


Orders Managment System
Subject: Outlines – P1

In last several posts we develop a Zoo Managment System [Click to Read]. We try to use our skills in python and pandas to work with DataFrame and developing easy app that reading and writeing a csv file. We will use the same principle and re-use most of it’s functions to write our new coming system.

The Story: I don’t think that there is any person using the web without using at least one of the online shopping sites, personally; I am using three sites. In coming posts we will work on Orders Managment System (OMS) to track our orders.

General Enhancement In the Zoo application we did not use any validation or try … Exceptions blocks, but this time we will use a range of validations over the user input starting from the menu until asking if the user want to save shange before he Quit. Using validations will make the code (or make it looks like) complicated, so I will use lots of comments to describe some codes.

Validation Example ..
In our Date entry, we will inform the user that we want the date as DD-MM-YYYY , then if the user enter any thing not logic (not a date) or say he enter “3/8-2019” our validation will convert that to “03-08-2019”.


OMS Outline: In this application our goal is to practices on data validation so we will develop a system to store our orders data and apply the validation on it, I will use aliexpress orders information to build the csv file. As far as i know, aliexpress site is not providing any tool to export a file that contain your orders detail so we will do this just to keep a history records of our orders.

File Structuer: The file will have 8 columns here is a short description of each column:
order_no: A serial number that will increment automatically, we will consider this to be a primary key of the table.
order_id: This is the order id generated by aliexpress site, we will enter it as it is.
order_date: To hold the orders date, the date will be in DD-MM-YYYY format.
order_detail: Short description of the order, even you write it in your words or copy paste it from your order page.
item_price: The price in US$
ship_price: Shipment amount in US$
quantity: The Quantity of the items.
item_url: The URL of the item page.

Coming Post We will start from next post to write the main menu, and the first function to create a csv file and insert the first row.




Follow me on Twitter..





Python: Pandas Lesson 12

August 18, 2019 1 comment


Learning : Pandas Lesson 12
Subject: Zoo Management System – P6

In this last post of Zoo Managment System series we will cover two of most important functions after the Adding Records, they are Editing a Record and Deleting Record, both will be in there simplest form with no validations code or try … Exception block.

Editing Record: To edit a record we need to identify it first, so we will use the id then the function will ask the user for each column if he want to change/update the data, if the user select Yes (y) then he will get the prompt to enter the new data, and if he select No (n) then the application will ask him about the other column. Here is the code ..

Edit a Record

def edit_record(df):

clear() # To clear the terminal.

print(‘\n We are in a editing mode, for each column if you want to edit select ”y” if not select ”n”\n’)

# First we will fitch the columns from the df

col_list = []

for each in df.columns :

col_list.append(each)

print(df.to_string(index=False))

print(‘\n Select the id of the rescord you want to edit:’)

edit_id = input(‘ Enter the id : (c for cancel ) ‘)

if edit_id in [‘c’,’C’] :

input(‘ You select to cancel the editing… \n ** Press any key to continue .. . . ‘)

return

else:

for each in col_list:

if each !=’id’:

chose_edit= input(‘\n Do you want to change {} (y or n)’.format(each))

if chose_edit in [‘y’,’Y’]:

you_change = input(‘ Enter the new value: ‘)

df.loc[df[‘id’] == int(edit_id), [each]] = you_change

else:

input(‘\n You select not to change {} press Enter to change another column.’.format(each))

print(‘\n You finish Editing. .. \n ‘)

input(‘\n\n\n\n ** Press any key to continue .. . . ‘)


Here is a screen shot of the code …

Deleting a Record:A last function we will work on is deleting a record. To do this first we will show the user all the rows in the DataFrame then he will select the id of the column to be deleted. Here is the code..

Delete a Record

def delete_record(df1):

clear()

print(‘\n’,df1.to_string(index=False))

del_record = input(‘\n Enter the ”ID” for the record you want to delete: ‘)

#Delete the row with id = del_record 

df1 = df1[df1.id != int(del_record)]

print(‘\n Record been deleted … .. \n ‘)

global df # Reset the df as global variable

df = df1

input(‘\n\n\n\n ** Press any key to continue .. . . ‘)


Now we just finish a simple application to manage our zoo file. When we start writing pandas lessons I select a simple data frame csv file fond on the web, and we work on it as simple as we can to go through a most important functions in any application we plan to write. We did’t cover any validations part or any mistakes the user may do. So to avoid all this I am planning to start working on another project that we will cover any missing part here.

Also I am working on publishing the source code of this application on my download page here..




:: Pandas Lessons Post ::

Lesson 1 Lesson 2 Lesson 3 Lesson 4
Lesson 5 Lesson 6 Lesson 7 Lesson 8
Lesson 9 Lesson 10 Lesson 11 Lesson 12



Follow me on Twitter..




Python: Pandas Lesson 11

August 15, 2019 2 comments


Learning : Pandas Lesson 11
Subject: Zoo Management System – P5

In this post we will cover another three functions, let’s start.

Missing Data: A commun problems we will face in any data file is Missing data, and if we are doing data analysis this may ledd us to a real problem. In this section we will cover only the missing data and will not go through any filling techniques.

Missing Data Report

def missing_data(df):

clear() # To clear the terminal.

print(‘\n\n *** Missing Data Report ***\n’)

print(‘ There are {} rows and {} columns.’.format(df.shape[0],df.shape[1]))

print(‘\n Number of missing data in each columns are:\n’)

total_miss = 0 # Counter

for each in df:

print(‘\n We have {} missing data in {} column.’.format(df[each].isnull().sum(),each))

total_miss = total_miss + df[each].isnull().sum()

print(‘\n Total missing data :’,total_miss)

print(‘\n The missing data represent {}% of the DataFrame.’.format((df.size*total_miss)/100))

input(‘\n\n\n\n ** Press any key to continue .. . . ‘)

This is a very simple missing data report, but doing the job for-now, we cover some part of missing data in lesson 5 read it Here.

Add New Record: I divide the editing function into three parts or three functions, Add new record, Edit a record and delete a record.
First we will work on Add New Record, from the name we know that this function will allow the user to add a record to the DataFrame, first we will get all columns in the dataframe, also we will get the maximum value in the ID column and increase it by one, then we will ask the user to enter the data for each column, and we are assuming that the user will (or MUST) enter a valid data since we agree that we will not have any validation test in this version of the application, also if the user just press ‘Enter’ thats mean NA data so we will add NaN to the field.
So let’s start..

Add New Record
def add_new_record(old_df):

clear() # To clear the terminal.

# First we will fitch the columns from the df

col_list = []

for each in old_df.columns :

col_list.append(each)

print(col_list)

# Get max id and increase it by 1

next_id = old_df[‘id’].max()+1

new_row={}

# let user enter the new record.

print(‘\n Enter the data for each field then press Enter.\n’ )

print(‘ If you just press Enter NaN will be entered.’)

for each in col_list:

if each !=’id’:

print(‘ Enter data for ‘,each)

new_row.update({each:(input(‘ : ‘))})

new_row.update({‘id’:next_id})

old_df = old_df.append([new_row])

for each in col_list :

if (old_df.loc[old_df[‘id’] == next_id, each][0]) ==”:

(old_df.loc[old_df[‘id’] == next_id,[each]]) = float(‘NaN’)

print(‘\n New Record added successfully..\n’)

# print out last 5 rows to show the new record.

print(‘\n The new record in the df..\n ”,old_df.tail(5))

global df # Reset the df as global variable

df = old_df

input(‘\n\n\n\n ** Press any key to continu .. . . ‘)

Save : At this point we will jump to the last function, simply after adding data to our DataFrame we need to keep this change, we need to save the new record so we can retrieve it next time we run the application. So to do this we will re-write the csv file again.

Save the DataFrame

def save_the_df (df):

clear() # To clear the terminal.

file_name=’data_file_zoo.csv’

df.to_csv(file_name, encoding=’utf-8′, index=False)

print(‘\n File saved .. ‘)

input(‘\n\n\n\n ** Press any key to continue .. . . ‘)



Now, if we close the application then re-open it again we will have the new records that we add. We just save the file and will not have any option to rename it.

In Next Post.. in coming post we will cover deleting and editting functions, also I will upload the full code file to the portal so any one can download it as python file.




:: Pandas Lessons Post ::

Lesson 1 Lesson 2 Lesson 3 Lesson 4
Lesson 5 Lesson 6 Lesson 7 Lesson 8
Lesson 9 Lesson 10 Lesson 11



Follow me on Twitter..