Python

On this page, i will share python codes that you might find useful.

*If anyone wants to share some python codes, please feel free to contact me. I will credit your shares on this page.

______________________________________________________________________________________________________

Caraka’s btc checker

*Python 3.5

#!/usr/bin/python3

# caraka's btc checker

import urllib.request, json, locale

currencies = ['USD','EUR','GBP','CNY','NZD','CAD']

locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')

prices = {}

def btcPrice(code):
    with urllib.request.urlopen("http://api.coindesk.com/v1/bpi/currentprice/" + code + ".json") as url:
        data = json.loads(url.read().decode())
        price = round(float(locale.atof(data['bpi'][code]['rate'])),2)
    return price

for currency in currencies:
    y = btcPrice(currency)
    prices[currency] = y
    
print("Coindesk BTC:")
print("usd: ${0}    eur: €{1}    gbp: £{2}    cny: ¥{3}    nzd: ${4}     cad:${5}".format(prices['USD'],prices['EUR'],prices['GBP'],prices['CNY'],prices['NZD'],prices['CAD']))
Caraka's btc checker

The above code was made by Caraka from the Demonsaw network. It gives the price in real time of Bitcoin in different currencies. The code can be adapted to any currency. You may find a list here.


PyPing

*Python 3.5

import tkinter as tk
import subprocess


def ping():
    cmd = ["ping", entry.get(), "-c", "2"]
    output = subprocess.check_output(cmd)

    # output = subprocess.check_output("ping {} -c 2".format(entry.get()), shell=True)

    print('>', output)
    # put result in label
    result['text'] = output.decode('utf-8')

my_gui = tk.Tk()
entry = tk.StringVar()

my_gui.geometry('700x400')
my_gui.title("PyPing")

tk.Label(my_gui, text="Enter target IP or host as required.").pack(pady=20)
tk.Entry(my_gui, textvariable=entry).pack(pady=10)
tk.Button(my_gui,text="Ping Test", command=ping).pack(pady=10)

# label for ping result
result = tk.Label(my_gui)
result.pack()

my_gui.mainloop()
PyPing

Here’s a screen shot with a ping result. The result is printed on a label. You can ping another website and the new result will replace the previous label output. You may want to add things like a menu, but for me, i did not see the need for it since it’s such a small application. GitHub and Stack Overflow are great websites to find good ideas and tutorials to create all kinds of applications. I visit them on a regular basis.

~ teklordz


ColorPicker

import sys
if sys.version[0] == '2':                   # for Python 3.X or 2.X
    from Tkinter import *
    from tkColorChooser import askcolor
elif sys.version[0] == '3':
    from tkinter import *
    from tkinter.colorchooser import askcolor

def setBgColor():
    (triple, hexstr) = askcolor()
    if hexstr:
        print(hexstr)
        push.config(bg=hexstr)
        show.set("#RRGGBB string = '" + hexstr + "'")

root = Tk()
root.title('ColorPicker')
root.geometry('500x400')
root.resizable(height=FALSE, width=FALSE)
push = Button(root, text='Press to Select Color', command=setBgColor)
push.config(height=3, font=('times', 20, 'bold'))
push.pack(expand=YES, fill=BOTH)

show = StringVar()
show.set('(hex string appears here)')
Entry(root, textvariable=show,
      justify=CENTER, font='bold').pack(fill=X)

root.mainloop()
ColorPicker

Inspiration for this application was taken from the book “Programming Python, 4th Edition”.  Here’s a screenshot.

 


Scroll Up