bumsfeld 413 Nearly a Posting Virtuoso

The program takes text and establishes dictionary of character:frequency. This dictionary is then presented sorted by character. Since it is important to show the most frequent characters, two methods are given to present dictionary sorted by frequency. Should all be very good for learning Python.

bumsfeld 413 Nearly a Posting Virtuoso

Do you think that people act demented during full moon nights? This small python program will tell you moon phase of a date you give it, so you can take precautions!

bumsfeld 413 Nearly a Posting Virtuoso

Question of wordcount program similar to Unix wc came up in the forum and I worked on a solution. Program shows number of lines, number of words, number of characters and text file's name. Good learning for commandline, file and string handling.

bumsfeld 413 Nearly a Posting Virtuoso

Muchly great code man!

bumsfeld 413 Nearly a Posting Virtuoso

The little program allows eye patients to test their color skills. They are given a random color rectangle by the computer and are then asked to match the color as closely as they can using sliders of the three basic colors. An evaluation button examines the closeness of the results.

bumsfeld 413 Nearly a Posting Virtuoso

Pythagoras is the man's real name.

bumsfeld 413 Nearly a Posting Virtuoso

I would be temded to bring the story in from a text file, but than of course you need to distrubute two files.

bumsfeld 413 Nearly a Posting Virtuoso

&& stands for the C and = &

bumsfeld 413 Nearly a Posting Virtuoso

Here is a list of macros I have been using.

bumsfeld 413 Nearly a Posting Virtuoso

I always wondered how qsort was correctly implimented.

bumsfeld 413 Nearly a Posting Virtuoso

Vegaseat, to keep primes from going over n, replace line
s = range(3, n+2, 2)
with
s = range(3, n+1, 2)

Henri

bumsfeld 413 Nearly a Posting Virtuoso

This program use a while loop to delete trailing spaces and tabs from a string. It is meant to be more of an example of the application of the while loop.

bumsfeld 413 Nearly a Posting Virtuoso

Commonly strings consist of ASCII characters, some are printable, some are in the backgrounds like the bell, carriage return, linefeed, tab and so forth. This code displays a table of ASCII characters and the corresponding decimal value. As always, you are encouraged to learn here.

bumsfeld 413 Nearly a Posting Virtuoso

A simple program to show you how to create a table of Fahrenheit and Celsius values using a for loop. You may be able to learn from this code.

bumsfeld 413 Nearly a Posting Virtuoso

Just a colorful ten second countdown to New Year. Hope you can learn some code from it.

bumsfeld 413 Nearly a Posting Virtuoso

My first experience with pointers. Learned how to spell a string foreward and backward using pointers. I hope you can learn too!

bumsfeld 413 Nearly a Posting Virtuoso

I do not know anything of BCX, but you make it read interesting.

bumsfeld 413 Nearly a Posting Virtuoso

I do not know Python much, but I was taught that you start with Adam and Eve, not one trillion old people!

bumsfeld 413 Nearly a Posting Virtuoso

Interesting code. Corrected a few errors, now it works. However fact(14) is less then fact(13).

bumsfeld 413 Nearly a Posting Virtuoso

Wow, this difficult to read! Why are there so many "you must" in there?

bumsfeld 413 Nearly a Posting Virtuoso

The last time I has seen such odd header files, was in one of Herb Schildt's old books.

bumsfeld 413 Nearly a Posting Virtuoso

Twice is better? You must be shiddin us!

bumsfeld 413 Nearly a Posting Virtuoso

I thought VB was hard to read, this almost impossible!

bumsfeld 413 Nearly a Posting Virtuoso

What compiler is that? Can't find dos.h anywhere!

bumsfeld 413 Nearly a Posting Virtuoso

David, you know how to explane things good!

bumsfeld 413 Nearly a Posting Virtuoso

These looks very much like the code VegaSeat wrote earlier?

bumsfeld 413 Nearly a Posting Virtuoso

Right from the example code! Come on, you can do better!

bumsfeld 413 Nearly a Posting Virtuoso

Mr. Omen,

your code samples a plane riboff from the examples that come with the download. Can't you apply a little imagination?

bumsfeld 413 Nearly a Posting Virtuoso

You made Python sound very interestung to me!

bumsfeld 413 Nearly a Posting Virtuoso

Dave, you are correct here, there is difference between representation and conversion.

bumsfeld 413 Nearly a Posting Virtuoso

I think FunsterStil is Danish for WindowStyle.

bumsfeld 413 Nearly a Posting Virtuoso

Wow, dude your code makes it very clear!

bumsfeld 413 Nearly a Posting Virtuoso

I like simpel stuff!

bumsfeld 413 Nearly a Posting Virtuoso

Even my professor of mathematics couldn't believe this easy algorithm.

bumsfeld 413 Nearly a Posting Virtuoso

Caluculator is cute!

bumsfeld 413 Nearly a Posting Virtuoso

Extension .pyd is used for C compiled library files for Python.

bumsfeld 413 Nearly a Posting Virtuoso

What is more efficient?

1) import math
2) from math import *
3) from math import sin, cos

bumsfeld 413 Nearly a Posting Virtuoso

It's like putting single image on PyGame "liquid canvas" giving an eerie visual effect. Here is short code:

# PyGame liquid effect on single image
# just old PyGame example modified
# module pygame free from: http://www.pygame.org

import pygame as pg
import math

pg.init()
# set width and height of pygame window
w = 600
h = 400
screen = pg.display.set_mode((w, h), pg.HWSURFACE|pg.DOUBLEBUF)
pg.display.set_caption('Animation of single image')

# load image file you have
# if not in the working folder, add full path
image_file = 'PythonHand3.jpg'
bitmap = pg.image.load(image_file)
# double the image size
#bitmap = pg.transform.scale2x(bitmap)

# put image and screen into the same format
if screen.get_bitsize() == 8:
    screen.set_palette(bitmap.get_palette())
else:
    bitmap = bitmap.convert()

increment = 0.0
x_list = range(0, w, 10)
y_list = range(0, h, 10)
while True:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            raise SystemExit
    increment += 0.08
    for x in x_list:
        xpos = (x + (math.sin(increment + x*0.01)*15)) + 20
        for y in y_list:
            ypos = (y + (math.sin(increment + y*0.01)*15)) + 20
            screen.blit(bitmap, (x, y), (xpos, ypos, 20, 20))
    pg.display.flip()
bumsfeld 413 Nearly a Posting Virtuoso

If you are familiar with C++, Java would be the way to go. Python is simply too uncomplicated.

bumsfeld 413 Nearly a Posting Virtuoso

please help me to choose best progranmming language between
java, python, php, c#,...
that cover above features

Since you are already used to C++ I would say use Java.

bumsfeld 413 Nearly a Posting Virtuoso

Is there a way to use a similar method with images instead of pixels? I know about all of that, but I need to load multiple images into PIL and tile them in a square...

Is it possible?

Looks like Sneekula wasted his time on you then.
I assume you also know all about img.getdata(), then you can put it together with img.putpixel().

bumsfeld 413 Nearly a Posting Virtuoso

niek_e is correct, successful compression depends on the source and the compression algorithm. Here is one example of large repeating text that the zip algorithm fails with, but bz2 compression works great:

# exploring Python file compression/decompression module bz2
# .bz2 files can be decompressed with WinRAR utility
# uncompressed file 'longtext.txt' has size of 48 kbytes
# level9 compressed 'longtext.bz2' has size of 1 kbyte
# zip compressed file 'longtext.zip' is of size 95 kbytes!
# tested with Python 3.1

import bz2

text = "use this to make one very long string right now\n"
# create 1000 lines of repeating text
text = text*1000

# create uncompressed text file
uc_fname = 'longtext.txt'
fout = open(uc_fname, 'w')
fout.write(text)
fout.close()

# create bz2 compressed file (can be used with WinRAR)
bz2_fname = 'longtext.bz2'
bzout = bz2.BZ2File(bz2_fname, 'w', compresslevel=9)
# Python3 requires text string to be encoded to bytearray
bzout.write(text.encode('utf8'))
bzout.close()

# read bz2 compressed file
bz2_fname = 'longtext.bz2'
bzin = bz2.BZ2File(bz2_fname, 'r', compresslevel=9)
# result of read() is of type string
text2 = bzin.read()
bzin.close()

# short test ...
print("Length of original text = %d" % len(text))
print("Length of recovered text = %d" % len(text2))

# the other way to compress ...
# create zipfile compressed file
# in this case the compressed file is larger than the original!
import zipfile as zf
z_fname = 'longtext.zip'
zout = zf.ZipFile(z_fname, 'w', compression=zf.ZIP_DEFLATED)
zout.write(uc_fname, text)
zout.close()
iamthwee commented: Nice info +23
Dan08 commented: Good one +1
bumsfeld 413 Nearly a Posting Virtuoso

Is the RAM used as the buffer?

I would say yes, unless your Operating System uses disk space as virtual memory.

bumsfeld 413 Nearly a Posting Virtuoso

One rude awakening with Python3 will be the frequent use of bytearrays instead of strings. It does this to handle international unicode better.

Here is how you deal with this:

# get code of given URL as html text string
# Python3 uses urllib.request.urlopen()
# instead of Python2's urllib.urlopen() or urllib2.urlopen()

import urllib.request

fp = urllib.request.urlopen("http://www.python.org")

mybytes = fp.read()
# note that Python3 does not read the html code as string
# but as html code bytearray, convert to string with
mystr = mybytes.decode("utf8")

fp.close()

print(mystr)
Nick Evan commented: UNICODE problems will become more and more frequent in the future... +25
bumsfeld 413 Nearly a Posting Virtuoso

Also the online book "dive into python" has been mostly
rewritten for Python3 (check appendix A for 2to3 diffs):
http://diveintopython3.org/

bumsfeld 413 Nearly a Posting Virtuoso

Swaroop C.H. has rewritten his excellent beginning Python tutorial for Python3:
http://www.swaroopch.com/notes/Python_en:Table_of_Contents

bumsfeld 413 Nearly a Posting Virtuoso
bumsfeld 413 Nearly a Posting Virtuoso

Retrieve the text from Tkinter Text widget line by line:

# explore the Tkinter Text() widget
# getting text 'line by line' from the widget
# use ctrl+c to copy, ctrl+x to cut selected text,
# ctrl+v to paste, and ctrl+/ to select all
# Henri

try:
    # for Python2
    import Tkinter as tk
except ImportError:
    # for Python3
    import tkinter as tk

def get_text(event):
    """get text widget contents of given line n (firstline=1)"""
    # retrieve the line number from button text
    n = int(event.widget.cget("text"))
    # set up start and end of the line
    start = "%d.0" % n
    end = "%d.end" % n
    # get the line text
    s = text.get(start, end)
    # show it in the label
    label['text'] = s

# test data
mw = """\
Na Sodium 22.98977
Mg Magnesium 24.305
Al Aluminum 26.98154
Si Silicon 28.0855
P Phosphorus 30.97376
S Sulfur 32.066
Cl Chlorine 35.4527
Ar Argon 39.948"""

root = tk.Tk()
root.title('get text line by line')

# text field, width=width chars, height=lines text
text = tk.Text(root, width=30, height=8, bg='yellow')
text.insert('insert', mw)
text.pack(pady=5)

# create buttons to call each line by number
for n, data in enumerate(mw.split('\n')):
    # first line in text is 1
    s = str(n+1)
    btn = tk.Button(root, text=s)
    btn.bind("<Button-1>", get_text)
    btn.pack(side='left', pady=5, padx=2)

# width/height in char size
label = tk.Label(root, text="", width=30, height=2)
label.pack(side='top', pady=5)

root.mainloop()
bumsfeld 413 Nearly a Posting Virtuoso

In the previous example I meant 'Tkinter' rather than 'Tinter'. This however is PyQT example looking at the tooltip widget and some colored text:

# pqt_tooltip1.py
# exploring PyQT's tooltip and color text (html)
# Henri

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Tooltip(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        # setGeometry(x_pos, y_pos, width, height)
        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Tooltip')

        btn_hello = QPushButton("Push Button")
        btn_hello.setToolTip('Press <b>me</b> for the greeting')
        self.connect(btn_hello, SIGNAL("clicked()"), self.on_click)

        self.label = QLabel()

        grid = QGridLayout()
        grid.addWidget(btn_hello, 0, 0, 1, 1)
        grid.addWidget(self.label, 1, 0, 1, 3)
        self.setLayout(grid)


    def on_click(self):
        s = "<font size=6 color='red'>Hello stranger!</font>"
        self.label.setText(s)


app = QApplication(sys.argv)
tooltip = Tooltip()
tooltip.show()
app.exec_()
bumsfeld 413 Nearly a Posting Virtuoso

What happens if you run this test code:

#!/usr/bin/env python

# example frame.py
# http://www.pygtk.org/pygtk2tutorial/sec-Frames.html

import pygtk
pygtk.require('2.0')
import gtk

class FrameExample:
    def __init__(self):
        # create new window
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.set_title("Frame Example")

        # connect "destroy" event to signal handler
        window.connect("destroy", lambda w: gtk.main_quit())
        window.set_size_request(300, 300)

        # set border width of window
        window.set_border_width(10)

        # create the Frame
        frame = gtk.Frame()
        window.add(frame)

        # set the frame's label
        frame.set_label("GTK Frame Widget")

        # align the label at the right of the frame
        frame.set_label_align(1.0, 0.0)

        # set the style of the frame
        frame.set_shadow_type(gtk.SHADOW_ETCHED_OUT)
        frame.show()
  
        # display the window
        window.show()

def main():
    # enter the event loop
    gtk.main()
    return 0

if __name__ == "__main__":
    FrameExample()
    main()

Do you get error message?

What is your Operating System?
Which version of Python?
Which version of PyGTK?
On Windows PyGTK is almost impossible to install!