Jump to content

I have been programming in Python for about 8 months I have a Raspberry Pi and I am running a p...


G+_Russ DiBennetto
 Share

Recommended Posts

I have been programming in Python for about 8 months.  I have a Raspberry Pi and I am running a public domain program called JCblock on it.  I wanted to build a front end to it.  Here is a module I wrote to sort the Blacklist.dat file.  I incorporated this into a GUI using Tkinter.  I thought I would share the code with you and a bit of the file that it sorts.

#-------------------------------------------------------------------------------

# Name:        module1

# Purpose:

#

# Author:      rdibened

#

# Created:     21/09/2013

# Copyright:   © rdibened 2013

# Licence:    

#-------------------------------------------------------------------------------

 

def main():

    pass

 

if _name_ == '__main__':

    main()

    fbuff = open("blacklist.dat" , "r" )

    lines=fbuff.readlines()

    lines.sort()

    fbuff.close()

    fbuff = open("blacklist.dat" , "w" )

    fbuff.writelines(lines)

    fbuff.close()

    print("Done")

 

Now for a segment of the file

# 01 The first field is the one tested. It can be the telephone

# 02 number or some other field in the caller ID string. It *must

# 03 be* terminated with a '?' character. The Date-of-last-block

# 04 field starts in the 20th column. Be sure to add the '111813'

# 05 characters to a new entry so that room to store the date is

# 06 present on the line. The rest of the line (out to 80 chars)

# 07 can contain a comment of your choosing.

# 08 *****Do not use tabs in the entries!********

# 09 Examples (without the initial '#' chars!):

# 10 8005551212?        111813         JUNK CALL

# 11 Cell Phone?        111813         (A cell phone call)

# 12               | <---- the 20th column

# 14 Tested field:     Date field:   Comment field:

#O--NAME = ?        103013         Test for unavailable

0000000000?        102313

0824305456?        021914

1111111111?        111813

1407987523?        111813

1412530517?        111813

 

Enjoy.  It works like a charm.  I since added information to the code that determines if the system it is running on is Windows or Linux and changes the commands so it executes the proper syntax.  For instance to delete a file in Windows it will send DOS a "del" command and if the program determines it is running in Linux it will execute a Bash "rm" command.  This makes my program OS independent without changing code.  If you would like to see an example of this code, let me know.

Link to comment
Share on other sites

I've seen this def main(): in a few programs now and I'm wondering what the thinking is behind it. Is it just a style thing to organize everything into functions?

 

Also the if _name_ == '___main___':

Is that to prevent code from running if this is imported as a module into another program?

Link to comment
Share on other sites

 Share

×
×
  • Create New...