python examples

Example 1: python program big

# Program to check if a number is prime or not

num = 407

# To take input from the user
#num = int(input("Enter a number: "))

# prime numbers are greater than 1
if num > 1:
   # check for factors
   for i in range(2,num):
       if (num % i) == 0:
           print(num,"is not a prime number")
           print(i,"times",num//i,"is",num)
           break
   else:
       print(num,"is a prime number")
       
# if input number is less than
# or equal to 1, it is not prime
else:
   print(num,"is not a prime number")

Example 2: python pygeoip example

def geo_ip(res_type, ip):
    try:
        import pygeoip
        gi = pygeoip.GeoIP('GeoIP.dat')
        if res_type == 'name':
            return gi.country_name_by_addr(ip)
        if res_type == 'cc':
            return gi.country_code_by_addr(ip)
        return gi.country_code_by_addr(ip)
    except Exception as e:
        print e
        return ''

#----------------------------------------------------------------------
# Search
#----------------------------------------------------------------------

Example 3: python pygeoip example

def main(argv):
    parseargs(argv)
    print(BANNER.format(APP_NAME, VERSION))

    print("[+] Resolving host...")
    host = gethostaddr()
    if (host is None or not host):
       print("[!] Unable to resolve host {}".format(target))
       print("[!] Make sure the host is up: ping -c1 {}\n".format(target))
       sys.exit(0)

    print("[+] Host {} has address: {}".format(target, host))
    print("[+] Tracking host...")

    query = pygeoip.GeoIP(DB_FILE)
    result = query.record_by_addr(host)

    if (result is None or not result):
        print("[!] Host location not found")
        sys.exit(0)

    print("[+] Host location found:")
    print json.dumps(result, indent=4, sort_keys=True, ensure_ascii=False, encoding="utf-8")

Example 4: best python programs

import numpy as np
 
import tensorflow as tf
 
 
 
from include.data import get_data_set
 
from include.model import model
 
 
 
 
 
test_x, test_y = get_data_set("test")
 
x, y, output, y_pred_cls, global_step, learning_rate = model()
 
 
 
 
 
_BATCH_SIZE = 128
 
_CLASS_SIZE = 10
 
_SAVE_PATH = "./tensorboard/cifar-10-v1.0.0/"
 
 
 
 
 
saver = tf.train.Saver()
 
sess = tf.Session()
 
 
 
 
 
try:
 
    print("
Trying to restore last checkpoint ...")
 
    last_chk_path = tf.train.latest_checkpoint(checkpoint_dir=_SAVE_PATH)
 
    saver.restore(sess, save_path=last_chk_path)
 
    print("Restored checkpoint from:", last_chk_path)
 
except ValueError:
 
    print("
Failed to restore checkpoint. Initializing variables instead.")
 
    sess.run(tf.global_variables_initializer())
 
 
 
 
 
def main():
 
    i = 0
 
    predicted_class = np.zeros(shape=len(test_x), dtype=np.int)
 
    while i < len(test_x):
 
        j = min(i + _BATCH_SIZE, len(test_x))
 
        batch_xs = test_x[i:j, :]
 
        batch_ys = test_y[i:j, :]
 
        predicted_class[i:j] = sess.run(y_pred_cls, feed_dict={x: batch_xs, y: batch_ys})
 
        i = j
 
 
 
    correct = (np.argmax(test_y, axis=1) == predicted_class)
 
    acc = correct.mean() * 100
 
    correct_numbers = correct.sum()
 
    print()
 
    print("Accuracy on Test-Set: {0:.2f}% ({1} / {2})".format(acc, correct_numbers, len(test_x)))
 
if __name__ == "__main__":
 
    main()
 
sess.close()