Issue with continous incrementing of unbroken sequence for a entireworking day

M

Morten Engvoldsen

Hi team,
I need to run a batch of sales records and the batch has serial_number
filed to store the serial number of the sales record. The serial number
should be set to 1 everyday when the batch runs first time in a day and
the maximum serial number could be 1000.

So when the batch runs first time in a day and if it has 10 records, so the
last serial number will be 10. And when the batch runs 2nd time in same
day, the serial number should start from 11. In this way serial_number
will increment as an unbroken series throughout the entire working day. The
next day when the batch runs first time the serial number will reset to 1.

Now this could be sample code how the program can count the sequence for a
batch:

def salesrecord():
serial_number = 1
for i in selesrecord:
print first_sales_record
serial_number += 1
print serial_number

salesrecord()

So if the batch has 10 records and last serial number of first batch is 10,
then when the batch runs second time in the same day, how the
'serial_number' will get the value of 10 and then continue the serial
number for the same day, then for next day again the serial number will
start from 1.

Can you let me know how can i achive this in python? As i am in learning
phase of python, can you let me know what would be good approach to do this
in python.
 
R

Rick Johnson

[...]
So if the batch has 10 records and last serial number of
first batch is 10, then when the batch runs second time in
the same day, how the 'serial_number' will get the value
of 10 and then continue the serial number for the same
day, then for next day again the serial number will start
from 1.


Well you need "data persistence" between successive runs of the program. That means saving some information to disc. If you don't know how to read and write files with Python, this might be a good time to learn!

http://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files

But not only must you save the current serial number, you also need to save the current time. Here are the steps your program must do to solve this problem.

1. Run the program.

2. Read the persistent file into memory.

3. Convert the time and serial number into objects (this requires you set up an initial file or have your program logic invent the needed data in the absence of the file).

4. Find out todays date and time (modules: time and datetime would be helpful here).

5. Decide if the last saved date is in the range of "today" (these are rules you need to decide on before hand. Maybe a day is in the range from 12:00am to 11:59pm... up to you).

5a. if the saved serial number was saved "today", then start counting from the saved serial number plus 1.
5b. if however the saved serial number was saved on a previous day, then start counting from 1.

6. Run through all the sales records and do whatever it is you do with them.

7. Overwrite the persistent file with the newly incremented serial number and current time

8. Close the program cleanly.

9. Celebrate another good day of sales (well hopefully)
 
R

Rick Johnson

[...]
So if the batch has 10 records and last serial number of
first batch is 10, then when the batch runs second time in
the same day, how the 'serial_number' will get the value
of 10 and then continue the serial number for the same
day, then for next day again the serial number will start
from 1.


Well you need "data persistence" between successive runs of the program. That means saving some information to disc. If you don't know how to read and write files with Python, this might be a good time to learn!

http://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files

But not only must you save the current serial number, you also need to save the current time. Here are the steps your program must do to solve this problem.

1. Run the program.

2. Read the persistent file into memory.

3. Convert the time and serial number into objects (this requires you set up an initial file or have your program logic invent the needed data in the absence of the file).

4. Find out todays date and time (modules: time and datetime would be helpful here).

5. Decide if the last saved date is in the range of "today" (these are rules you need to decide on before hand. Maybe a day is in the range from 12:00am to 11:59pm... up to you).

5a. if the saved serial number was saved "today", then start counting from the saved serial number plus 1.
5b. if however the saved serial number was saved on a previous day, then start counting from 1.

6. Run through all the sales records and do whatever it is you do with them.

7. Overwrite the persistent file with the newly incremented serial number and current time

8. Close the program cleanly.

9. Celebrate another good day of sales (well hopefully)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top