vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

A simple code example to calculate the monthly payment on a mortgage loan. Takes a look at Python's newer print and format functions.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Here is a project I like to see:
"Compare 2 texts and give a percentage of match."

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Compare texts and see which ones are a close match.
Work out a degree/percentage of match.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

A simple test of a numeric string.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Using PySide's QAbstractTableModel allows you to easily customize a widget like QTableView and make it more generic in its application. Here we use it to present solvent data in tabular form and sort each column content by simply clicking on the header. You can feed it any data as long as you stick to the format of the data used, a header list and a list of data tuples/lists.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

So you want to find out which day of the week you were born. Well at least some of us do. I actually wrote this program because my whole family was born on Sundays, and my friends didn't believe it! An oldie but goodie, moved from DeSmet C to Turbo C, to Pelles C, and now to Python.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

C header files often include other header files, which include other header files in turn. By chance you might end up with <stdlib.h> included. I would not rely on it!

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

On the Python forum we have listed a whole bunch of projects. Look through them, some will easily apply to C ...
http://www.daniweb.com/software-development/python/threads/32007/projects-for-the-beginner

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

A short code to show you how you can download and display an image from the internet on Python's Tkinter GUI toolkit.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

A somewhat newer look at Peter Parente's pywin32 based speech engine. It will read text on your computer.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Just a simple Jython code to test drawing on the canvas.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Just a different way to extract desired data from an xml code string.

chriswelborn commented: always good examples +1
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

A small test program exploring the PySide/PyQT Label widget. With HTML code you can do some nice formatting of the label's text.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

The PySide/PyQT QValidator restricts the type of input a widget like QLineEdit can accept. Here is a simple test.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

"What care I how time advances? I am drinking ale today."
... Edgar Allen Poe

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

“There can’t be good living where there is not good drinking.”
... Ben Franklin

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

A function to take a text and extract the string between two given substrings. Allows you to find the nth occurence. Also safeguards against dead ends.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

As you execute cx_freeze it will actually show what it includes by default. Usually re is in that list.

Also look at snippsat's include_files option.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Data applied to LSH can consist of images, sounds, gene expressions and the like.
You generally use LSH to detect similarities.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

If you want to call the appropriate function, do this ...

code = prodcust_code[:2]
if code == "AB":
   function_AB()
elif code == "CC":
   function_CC()
elif code == "EF":
   function_EF()
elif code == "US":
   function_US()
else:
    print("No function found for {}".format(code))
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

You can use the more exact Newton's Method Python version from:
http://www.daniweb.com/software-development/python/code/444930/newtons-method-example-python

Here is the example for a squareroot solution ...

''' Newton_method2.py
find value for x in f(x) = 0 using Newton's method
'''

def derivative(f):
    def compute(x, dx):
        return (f(x+dx) - f(x))/dx
    return compute

def newtons_method(f, x, dx=0.000001, tolerance=0.000001):
    '''f is the function f(x)'''
    df = derivative(f)
    while True:
        x1 = x - f(x)/df(x, dx)
        t = abs(x1 - x)
        if t < tolerance:
            break
        x = x1
    return x

def f(x):
    '''
    here solve x for ...
    x*x - 7 = 0
    same as x = 7**0.5 = sqrt(7)
    '''
    return x*x - 7

x_approx = 1  # rough guess
# f refers to the function f(x)
x = newtons_method(f, x_approx)

print("Solve for x in x*x - 7 = 0  or  x = sqrt(7)")
print("%0.12f (x via Newton)" % x)
# compare with math.sqrt(7)
import math
print("%0.12f (x via sqrt(7))" % math.sqrt(7))

''' result ...
Solve for x in x*x - 7 = 0  or  x = sqrt(7)
2.645751311114 (x via Newton)
2.645751311065 (x via sqrt(7))
'''
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
... Bill Gates

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

This PySide (PyQT) code sample shows you how to apply a StyleSheet to a GroupBox containing a number of RadioButtons.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

A little fun drawing with the Python module turtle. Well commented to help the beginner.

TrustyTony commented: I do not like the unnecessary and confusing () around single values. -3
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

This code example shows how to create a dictionary with row,column indexed (r,c) key tuples
from a typical list of lists 2D matrix. Also an example is shown of the ease of processing the resulting dictionary.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Another look at closure ...

''' closure102.py
A nested function has access to the environment in which it was defined.
This definition occurs during the execution of the outer function.
In a closure you return the reference to the inner function that remembers
the state of the outer function, even after the outer function has completed
execution.
'''

def outer(x):
    def inner(y):
        return x * y
    # return reference to the inner function
    return inner

# inner remembers x = 2
inner = outer(2)

print(inner)
print('-'*30)
# this call will supply the y = 7
print(inner(7))

''' result (location in memory) ...
<function inner at 0x02C4D738>
------------------------------
14
'''
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

If you draw shapes that are closely spaced and shifted, you can create interesting interference patterns, so called moire patterns. Here is an example using the Python module PyGame to do the drawing.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Or try this ...

slist = ['10015, John, Smith, 2, 3.01', '10334, Jane, Roberts, 4, 3.81' , '10208, Patrick, Green, 1, 3.95']

mylist = [item.strip() for s in slist for item in s.split(',')]
print(mylist)

''' result ...
['10015', 'John', 'Smith', '2', '3.01', '10334', 'Jane', 'Roberts', '4', '3.81', '10208', 'Patrick', 'Green', '1', '3.95']
'''
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Even shorter ...

def toNums2(lst):
    return map(int, lst)
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Take a look at:
http://www.daniweb.com/software-development/python/code/445504/an-almost-ide-wxpython

Should have enough hints how to do this with wxPython.

Basically ...

# using module subprocess to get the
# directory of drive C:\
# Python27 code

import subprocess

p = subprocess.Popen("dir C:\\", shell=True, stdout=subprocess.PIPE)

# allow external program to work
p.wait()

# read the result to a string
# (Python3 gives bytearray rather than string)
result = p.stdout.read()

print(result)
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Interestingly, Python syntax is closer to normal pseudocode than any other computer language.

chriswelborn commented: very true, the syntax is so obvious it almost feels 'wrong' coming from another language. +1
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

If you want a window with a menubar, toolbar, statusbar, dock and central widgets, you can use the PySide GUI toolkit's QMainWindow. To use the more traditional box and grid layouts, you simply add a QWidget instance as the central widget. Here is an example.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Load the data if phonelist is empty and data file exists. Something like this ...

from tkinter import *
import pickle
import os

def save_phonelist(addressbookfile, phonelist):
    addressbook = phonelist
    # write to storage
    f = open(addressbookfile, 'wb')
    pickle.dump(addressbook, f)
    f.close()

def load_phonelist(addressbookfile):
    # 'r'ead back from storage
    f = open(addressbookfile, 'rb')
    storedlist= pickle.load(f)
    print(storedlist)  # test
    f.close()
    return storedlist

'''
your updated code goes here

'''

def setSelect(phonelist) :
    phonelist.sort()
    select.delete(0,END)
    for name, phone, email in phonelist:
        select.insert(END, name)


addressbookfile = 'addressbook.data'
phonelist = []
if not phonelist and os.path.exists('addressbook.data'):
    phonelist = load_phonelist(addressbookfile)

master = makeWindow()
setSelect(phonelist)
master.mainloop()

Also make sure you save the data before you exit the program.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

A colorful look at the PySide GUI toolkit and its QPainter class and methods. Well commented and demonstrated with the drawing of a number of rectangles and the different ways to apply colors.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

QMainWindow has its own layout manager to which you can add QToolBars, QDockWidgets, a QMenuBar, and a QStatusBar. The layout has a center area that can be occupied by any kind of widget. Layouts like QVBoxLayout() won't work.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Replace
QtGui.QMainWindow
with
QtGui.QWidget

also comment out
win.floatingToolBar()
or you will be calling it twice

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

this would still be a mixed up statement and function style
print("The length of your word is %s") % len(length)
should be (Python2 only statement style)
print "The length of your word is %s" % len(length)
or better use the print() function (okay with Python2 and Python3)
print("The length of your word is %s" % len(length))

Using OOP for simple code like this is probably overkill, but might be a good exercise.
Well, at least you made the global names capitals.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

A Python code example to find an approximate value for x in f(x) = 0 using Newton's method.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Explore Python's sqlite3 database module, create, write and read a database file.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

This snippet shows you how to create a list of class instances. The class itself cannot be pickled, but the list of instances can. To use the pickled data file in another program, you have to recode the class in that program.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Jython is a version of Python that can use the extensive Java library. Jython uses Python syntax and avoids the rather ugly syntax (at least for Pythonions) of Java. Here we apply it to bring up an image from a file.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

This shows the application of a Memoize decorator to speed up recursive functions in Python.

Lucaci Andrew commented: Nice one. +5
BustACode commented: Good stuff. Looks to also be my entry into decorators. +0
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

I think the Python module BioPython would help you a lot ...
http://biopython.org/wiki/Main_Page

Also ...
http://biopython.org/DIST/docs/tutorial/Tutorial.html

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

If I understand this right, you want something like this ...

import pprint

mylist = [['a00002.doc', 'image38.jpg'],
['a00003.doc', 'image40.jpg'],
['a00001.doc', 'image50.jpg']]

newlist = []
for ix, item in enumerate(mylist):
    #print(item[0])  # testing
    item[0] = "a%05d.doc" % (ix+1)
    #print(item[0])  # testing
    newlist.append(item)

pprint.pprint(newlist)

''' result ...
[['a00001.doc', 'image38.jpg'],
 ['a00002.doc', 'image40.jpg'],
 ['a00003.doc', 'image50.jpg']]
'''
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

I t would be better to give the dictionary to the class this way ...

class Mac:
    def __init__(self, hexmac, mac_address):
        self.hexmac = hexmac
        self.mac_address = mac_address
    def resolve(self):
        #print(self.mac_address)  # test
        return self.hexmac, self.mac_address


mac_address = {
'000000':'xerox0',
'000001':'xerox1',
'000002':'xerox2',
}

m = Mac('123', mac_address)

# returns a tuple of 2 items
hx, ma = m.resolve()
print(hx)
print(ma)
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

() implies a function call

You are using
self.clock_start = time.clock()
and
def clock_start(self):

This method would be called with self.clock_start(), but the name is used by a variable of the same name containing a float value. Simply rename one of them.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Create a function that shifts elements of a list in circular order.
For instance [1,2,3,4,5] --> [5,1,2,3,4] etc.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague