Gribouillis 1,391 Programming Explorer Team Colleague

Here is a version that runs. I just removed the last index in the lovers loop

def displayHappy():
    numLimit = input("Enter a positive integer: ")
    countHappy = 0
    countUnhappy = 0
    liHappy = []
    for num in range(1, numLimit + 1):
        inNum = num
        while inNum != 1 and inNum != 4:
            inNum = zap(inNum)
            
        if inNum == 1:
            liHappy.append(num)
            countHappy += 1
        else:
             countUnhappy += 1
    print liHappy

    for i in range(len(liHappy)-1):
        if liHappy[i+1] - liHappy[i] == 1:
            print "lovers: %d, %d" % (liHappy[i], liHappy[i+1])

def zap(intNum):
	total = 0
	while intNum != 0:
		last = intNum % 10
		total += last**2
		intNum = intNum / 10

	return total

displayHappy()
Gribouillis 1,391 Programming Explorer Team Colleague

A persistant quine perhaps ?

Gribouillis 1,391 Programming Explorer Team Colleague

A google search with "cistematic python" leads to http://cistematic.caltech.edu/ . This page also contains a link to psyco.

Gribouillis 1,391 Programming Explorer Team Colleague

Updating python installs only the python standard library, not other modules that were previously installed in your site-packages directory. You will have to reinstall these modules one by one. It seems that you need psyco and cystematic.

Also, I don't know centos, but usually on linux systems, there is a package manager to install software. I'm not sure you chose the best way to upgrade python.

Gribouillis 1,391 Programming Explorer Team Colleague

I think your computer tried to read the file with the shell. You should pass the command

$ python getsplicefa.py

instead of

$ ./getsplicefa.py

or perhaps put a line #!/usr/bin/env python at the top of your file.

Gribouillis 1,391 Programming Explorer Team Colleague

You can also make a function

def gen_args(incoming_data, lnam_fnam, db_date):
    yield "'A'"
    for i in list(range(5)) + [4,4]:
        yield incoming_data[i]
    for i in range(2):
        yield lnam_fnam[i]
    for i in [11, 5, 10] + list(range(12, 18)):
        yield incoming_data[i]
    for i in range(2):
        yield ""
    yield 0
    yield incoming_data[4]
    yield db_date

def make_request(incoming_data, lnam_fnam, db_date):
    args = ",".join(str(x) for x in gen_args(incoming_data, lnam_fnam, db_date))
    request = "INSERT INTO customer_synch VALUES (%s);" % args
    return request
Gribouillis 1,391 Programming Explorer Team Colleague

It's a classical error, look at section 31.2.5 in your swig doc. You should probably write %module lmp_driver

Gribouillis 1,391 Programming Explorer Team Colleague

Don't you think $(CLINK) and $(CLIB) should be given to the build of _lmp_driver.so instead of lmp_driver.o ?

Gribouillis 1,391 Programming Explorer Team Colleague

numarray.reshape doesn't apply to python lists. It applies to numarray.NumArray, which is a different type with a specific implementation. You can write

data2 = numarray.array(data)
data3 = numarray.reshape(data2, 5, 91)
Gribouillis 1,391 Programming Explorer Team Colleague

You can fill a list, and join with the empty string:

mylist = ['W', 'E', 'T', 'H', 'E']
print ''.join(mylist)
Gribouillis 1,391 Programming Explorer Team Colleague

that is way too advanced for me to handle lol, Looks like I'll go back to the drawing board..

One thing you could do is replace letters by lowercase letters, because if B is replaced by w, then this w will never be replaced again ;)

Gribouillis 1,391 Programming Explorer Team Colleague

Take the first B in your text. It should be replaced by W, but then when the for loop continues, it will replace this W with something else because W comes after B in the alphabet. Each character should be replaced only once. I think the correct way to do this is to use the method string.translate.

Gribouillis 1,391 Programming Explorer Team Colleague

You can simplify this with module bisect

from bisect import bisect
frequencies = [
    .08, .1, .15, .3, .8, 1, 1.6, 1.95, 2, 2.1,
    2.3, 2.4, 2.5, 2.782, 4, 4.1, 4.5, 6, 6.1, 6.4,
    6.8, 7, 8, 9, 12
]
xletters = "ZQXJKVBPYGFWMUCLDRHSNIOATE"

for letter in alphabet:
        count = code.count(letter)
        freq = (count/countcode)*100
        code = code.replace(letter,xletters[bisect(frequencies, freq)])

I wonder if there is an error in your program's logic, because you are replacing one letter after another in the for loop, and it seems to me that you should replace all the letters at the same time, but since I don't know the rest of your program, I may be wrong.

Gribouillis 1,391 Programming Explorer Team Colleague

Your question is almost impossible to understand. Is it A, D or Z which has 'value' .8237 ? In your code, you are comparing sorted(dictionary.values()), which is a list, to a float, which is meaningless. Can you explain what you want to do with more details ? If we could see example input and expected output, that would help a lot .

Gribouillis 1,391 Programming Explorer Team Colleague

Don't hesitate to upgrade to 2.6, it's an even better python with even more opportunities for tricks !

Gribouillis 1,391 Programming Explorer Team Colleague

You'd better write the datafile line by line to the output file. Here is a way to do it

#from __future__ import with_statement  # <--- uncomment this line if your python is 2.5
import os
workDIR = 'C:\\work\\data'

# this is a multiline string
header = """\
ncols         1422
nrows         2044
xllcorner     409924.44886063
yllcorner     3631074.3284728
cellsize      500
NODATA_value  -9999
"""

old_wd = os.getcwd() # <--- store the initial wd
os.chdir(workDIR)
try:
    with open('output.txt','w') as outfile:
        outfile.write(header)
        with open('justdata2.txt','r') as datafile:
            for line in datafile:
                outfile.write(line)
finally:
    os.chdir(old_wd)  # <--- back to the initial wd

The use of the with statement here ensures that the files are closed afterwards, even if an error occurs (it's not very important). The try...finally ensures that we come back to the initial directory afterwards, even if an error occurred. Otherwise, a way to concatenate strings is the + operator, 'string' + 'string' + n + 'etc' . By curiosity, which version of python are you using ?

Gribouillis 1,391 Programming Explorer Team Colleague

I removed the underscores before 'spamify' and it works

#include <Python.h>

#include <stdio.h>
#include <stdlib.h>


static PyObject *spam_system(PyObject *self, PyObject *args)
{
    const char *command;
    int sts;

    if (!PyArg_ParseTuple(args, "s", &command))
        return NULL;
    sts = system(command);
    return Py_BuildValue("i", sts);
}

static PyMethodDef SpamMethods[] = {
    {"system",  spam_system, METH_VARARGS,"Execute a shell command."},
    {NULL, NULL, 0, NULL}        /* Sentinel */
};

PyMODINIT_FUNC initspamify(void)
{
    (void) Py_InitModule("spamify", SpamMethods);
}

int main(int argc, char *argv[])
{
    /* Pass argv[0] to the Python interpreter */
    Py_SetProgramName(argv[0]);

    /* Initialize the Python interpreter.  Required. */
    Py_Initialize();

    /* Add a static module */
    initspamify();
}

Build (linux):

$ gcc -c -fPIC -I/usr/include/python2.6/ spamify.c
$ gcc -shared -o spamify.so spamify.o -L/usr/lib64/ -lpython2.6

Test:

>>> import spamify
>>> spamify.system("date")
ven. févr. 26 08:23:18 CET 2010
0

An alternative is to replace "spamify" by "_spamify" in the call to Py_InitModule.

vegaseat commented: thanks for helping on that +10
Gribouillis 1,391 Programming Explorer Team Colleague

You need to write interface code between your C++ library and python. A good tool for this is the swig interface generator which is well documented.

If you know C++, would you be able to write a small program which uses your library and does something useful ? In that case make a list of the library functions that you are using in your program and start writing a swig interface file with these functions, then write the same program in python. This would give you a starting point, because such C++ libraries often have dozens of classes and functions.

Gribouillis 1,391 Programming Explorer Team Colleague

Your runlist is a list containing another list ! That's because glob.glob() returned a list and you appended this list to the runlist. You can replace 'append' with 'extend'

runlist = []
if glob.glob(ascDIR+"\\*.txt") <> []: #Find all asc files
        runlist.extend(glob.glob(ascDIR+"\\*.txt")) # Add them to the run list
print runlist

Also, write open(file, 'r') and not file[0]. It should work now.

Gribouillis 1,391 Programming Explorer Team Colleague

It's really strange. Could you post the output of the following code (between code tags), I just added a few print statements

#READ DATA FROM EACH ASC FILE AND CALCULATE QUANTILES FROM EACH FILE

q1=[]
q2=[]
q3=[]
print repr(runlist)
print len(runlist)
for file in runlist:
    print repr(file)
    gq=[]
    x=open(file[0],'r')
    for i in xrange(6)
        x.readline()
    z= x.readline()
    while z != '':
        z=z.strip().split()
        for num in z:
            num=float(num)
            if num > -1:
                gq.append(num)
        z= x.readline()    
    a=quantile(gq, .25,  qtype = 7, issorted = False)
    #print a
    b=quantile(gq, .5,  qtype = 7, issorted = False)
    c=quantile(gq, .75,  qtype = 7, issorted = False)   
    q1.append(a)
    q2.append(b)
    q3.append(c)
print len(q1), len(q2), len(q3)
AnnetteM commented: Gribouillis is an incredible asset to Daniweb +1
Gribouillis 1,391 Programming Explorer Team Colleague

Why do you write x=open(file[0],'r') instead of x=open(file,'r') ?
What is the content of your runlist ?

2 remarks about the style: avoid using 'file' as a variable name because this is the name of a builtin type, and also the 6 x.readline() should be written with a loop: each time that you are repeating similar statements, there is a better way to do it.

About writing a datafile with the lists q1, q2, q3 there are many ways to do it, you can simply write

outfile = open("outfile.txt", "w")
for i in xrange(len(q1)):
    outfile.write("%12.3e%12.3e%12.3e\n" % (q1[i], q2[i], q3[i]))
outfile.close()

You could also output to an excel spreadsheet.

Gribouillis 1,391 Programming Explorer Team Colleague

A few remarks, because I think the brute force approach is definitely not the good one. First you are not working with base 16 digits, but with base 256 digits because each of your characters has 2 hexadecimal digits. Second, how would you do in base 10 and an incomplete set of digits to find 3 numbers with a given sum ? This is an incomplete addition. Write it on paper, and will see that you won't use a brute force approach.
When you add 3 digits, there is a carry which can only be 0, 1, 2. The same holds in base 256. Since your result has only 4 digits(256), you are left with 3**3 = 27 possibilities for these carry numbers. For each of this 27 possibilities, you only need to find (4 times) 3 digits which sum is a given number, which is a very simple problem.
So you don't need to go through the hundreds of million cases of the brute force approach.

Gribouillis 1,391 Programming Explorer Team Colleague

There are 121*120*119*118 = 203889840 choices for str1, although I don't understand why the digits in str1 need to be different.

Gribouillis 1,391 Programming Explorer Team Colleague

The list sys.argv contains only strings, not numerical values. So you must convert your argument with offset = int(sys.argv[1]) . Python thought you were using the string modulus operator, which has a completely different meaning.

vegaseat commented: right on +10
Gribouillis 1,391 Programming Explorer Team Colleague

First when you iterate over the lines, there is a newline character at the end of each line. This newline can be removed with line = line.rstrip("\r\n") . You should write this as the first statement in the for loop. Then I suggest that you invoke an output function instead of printing lines. It means that you replace all the print line[4:] by output(line[4:]) . This allows you to change the effect of your code just by defining the output function. For example you can write

import sys
def output(data):
    sys.stdout.write(data)

Later, you can change this function definition to print to a file, or add newlines, etc.

chavanak commented: Helped me a lot and answered my question +1
Gribouillis 1,391 Programming Explorer Team Colleague

There is no memory leak in python. Python uses a generational garbage collector which deletes objects when there is no reference left to these objects. You can have look at the gc module which gives you a few functions to interact with the garbage collector, but normally you don't have to.

Of course, it's the programmer's responsibility to ensure that there is no reference to the object. If a living object A has a member B which is a dictionary which contains a value D one of which member is your bitmap object, then the bitmap will not be freed ! The role of the garbage collector is to solve cyclic references.

You should avoid defining __del__ methods for your objects (see gc.garbage).

Every python object stores an integer which is a count of the existing references to the object. An object with a positive refcount will not be freed by the garbage collector. All the objects you can access from your program have a positive refcount (so that you will never see an object with a refcount of 0). However, there is a method sys.getrefcount() which can help you detect hidden references to your object.

I've heard of a special case of 'memory leak' in python: when you load an extension module by importing a shared library, as far as I know, there is no way to 'unload' the library.

Gribouillis 1,391 Programming Explorer Team Colleague

It's not a string, it's a unicode string. That could be the problem. Try to send str(allLists).

vegaseat commented: neat +10
Gribouillis 1,391 Programming Explorer Team Colleague

Did you print the repr(allLists) before sending it to see if it contains the null chars ?

Gribouillis 1,391 Programming Explorer Team Colleague

This is just a hint

>>> s = "-9999 -9999 -9999 -9999 0.004537001 0.004537001 0.004537001   "
>>> s
'-9999 -9999 -9999 -9999 0.004537001 0.004537001 0.004537001   '
>>> s.strip()  # remove white space at both ends of s
'-9999 -9999 -9999 -9999 0.004537001 0.004537001 0.004537001'
>>> s.strip().split()   # split s on remaining white space
['-9999', '-9999', '-9999', '-9999', '0.004537001', '0.004537001', '0.004537001']
>>> [float(v) for v in s.strip().split()]
[-9999.0, -9999.0, -9999.0, -9999.0, 0.0045370009999999997, 0.0045370009999999997, 0.0045370009999999997]

Also, if you find too many lines, the number 1 debugging tool is to print your results and compare them to whatever you are expecting. If there are too many values, you can also print to a file.

AnnetteM commented: This post is very helpful +1
Gribouillis 1,391 Programming Explorer Team Colleague

The \x00 characters look like characters added by a serialization protocol like marshal.dumps.
What is the type of the variable allLists that you send to async_chat.push ? If it's not a string, I think you should serialize it with pickle

session.push(pickle.dumps(allLists))

and then on the client side

data = pickle.loads(myString)
Gribouillis 1,391 Programming Explorer Team Colleague

It seems that your communication protocol adds null characters "\x00" in the string. The first thing you could do is remove these characters with

myString = myString.replace("\x00", "")

The other question, as you said, is to understand why your server sends these null chars. Without the server and client's code, it's difficult to guess.

Gribouillis 1,391 Programming Explorer Team Colleague

If you want a practical access to the data, the best way is to use a class

class Atom(object):
    def __init__(self, num, elt, neigh=()):
      self.num = num
      self.elt = elt
      self.neigh = set(neigh)

    def __str__(self):
        return "Atom(%s, %s)" % (self.num, self.elt)

    def __lt__(self, other):
        return self.num < other.num

    def neigh_elts(self):
        for atom in sorted(self.neigh):
            yield atom.elt      

def display_atoms(dic):
    for num, atom in sorted(dic.items()):
        print atom, list(atom.neigh_elts())

AllAtoms = {}
AllAtoms[0] = Atom(0, 0)
AllAtoms[1] = Atom(1, 0, [AllAtoms[0]])
AllAtoms[2] = Atom(2, 0, [AllAtoms[0], AllAtoms[1]])

display_atoms(AllAtoms)

AllAtoms[0].elt = 9

print "Atom 0's element set to 9"
display_atoms(AllAtoms)

""" My output --->
Atom(0, 0) []
Atom(1, 0) [0]
Atom(2, 0) [0, 0]
Atom 0's element set to 9
Atom(0, 9) []
Atom(1, 0) [9]
Atom(2, 0) [9, 0]

"""

With a class, you can customize the access to data the way you want. You could also define a class AtomCollection instead of using a dict.

Gribouillis 1,391 Programming Explorer Team Colleague

I thought about entropy because y(y-1) is well known in this context in the range [0, 1]. It's a convex parabola which roots are 0 and 1. Another classical function is y log(y). Perhaps jjrrmm can tell us more about this.

Gribouillis 1,391 Programming Explorer Team Colleague

Why don't you store instead a list of the neighbouring atoms ? This would solve your problem.

Gribouillis 1,391 Programming Explorer Team Colleague

You should explain a little more. You are creating a dictionary

number1 --->   [  number2,   list1  ]

I suppose that number1 is an atom ID, but what are number2 and list1 ?

Gribouillis 1,391 Programming Explorer Team Colleague

I have to be misunderstanding the concept.

It looks like a way to measure an entropy. If you have n values x1,...xn and you take a convex function F (a function with positive 2nd derivative), you have an inequality

F( (x1 + ... + xn)/n ) <= ( F(x1) + ... + F(xn) ) / n

and equality occurs only if all the x's have the same value. The function F here is F(y) = y(y - 1) which is convex, and I think the fi's are the letter frequencies in the text, so fi = Ni/N where N is the number of letters in the text and Ni the number of occurrences of the i-th letter. If the frequencies where the same, you would have fi = 1/26, because there are n = 26 letters in the alphabet and the value of the entropy would be F(1/26). If the frequencies differ, you get F(f1) + ... + F(fn) > 26 F(1/26), and this difference could be used to distinguish an english text from a random text...

Gribouillis 1,391 Programming Explorer Team Colleague

Well, you simply replace the last line of your code above by

compare( (y1, m1, d1), (y2, m2, d2) )

You have (y1, m1, d1) < (y2, m2, d2) if y1 is before y2 or if y1 is equal to y2 but m1 is before m2 or if y1 = y2 and m1 = m2 and d1 is before d2.

Gribouillis 1,391 Programming Explorer Team Colleague

There is a trick, you can compare tuples, for example

>>> (1980, 3, 15) < (1987, 7, 12)
True
>>> (1987, 3, 15) < (1987, 7, 12)
True
>>> (1987, 7, 15) < (1987, 7, 12)
False

So, instead of passing the years of birth to your function compare, you only need to pass triples (year, month, day) !

Gribouillis 1,391 Programming Explorer Team Colleague

The next step is to use regular expressions (learn the re module)

import re
pat = re.compile(r"([+\-*/=]+)")
print(pat.split("1+2-3")) # prints ['1', '+', '2', '-', '3']

pat = re.compile(r"[+\-*/=]+")
print(pat.split("1+2-3")) # prints ['1', '2', '3']

If regular expressions are not sufficient, the next step is to use a parser like wisent (learn grammars and parsing computer languages).

Gribouillis 1,391 Programming Explorer Team Colleague

Gribouillis, I must say, you are Sherlock Holmes in disguise! Thank you for another case solved. I'm working in PythonWin, and interestingly, that 'invalid syntax' error gave no traceback message in the command window. It just gave a message at the bottom of the gray PythonWin frame right where it says,"running the code file..." or something similar to that.

If PythonWin does that, then it should be considered a bug in PythonWin. It means that when your program fails, you should try to run it outside of PythonWin to see the traceback.

Gribouillis 1,391 Programming Explorer Team Colleague

There is a colon missing at the end of for zone in zonelist . Add : . Also, when the interpreter says that there is an error, it prints a wonderful thing called a exception traceback. This is very useful to helpers, so when you have an error, please post the traceback (between code tags!).

Gribouillis 1,391 Programming Explorer Team Colleague

I'm just building a custom importer using abstract methods in importlib.abc and one of the methods for a importer that supports loading compiled modules is the ability to write bytecode files.

I could've used py_compile, but the write_bytecode method accepts a module name and bytecode to write so I wanted to keep it in line with the importlib.abc specification.

Interesting. I recently tried to write a custom importer too. I'll post it here when the small app that goes with it is released.

Gribouillis 1,391 Programming Explorer Team Colleague

Why do you need to generate pyc files ?

Gribouillis 1,391 Programming Explorer Team Colleague

but I want distinct strings.........and I don't want to loop it over to see if there is a existed string of the same.

You can use a set of strings

s = set(["hello", "world"])
s.add("anotherstring")
s.add("hello")
print s

or a dictionary (dict). Dict is the name of 'hash table' in python.

Gribouillis 1,391 Programming Explorer Team Colleague

I had to modify your code to run it on my system. This is the modified version with the result that both times are the same

import random, sys, os, struct

lib = os.path.dirname(os.__file__)
sourcefilepath = os.path.join(lib, "random%spy" % os.extsep)
testfilepath = sourcefilepath + "c"

istream = open(testfilepath, 'rb')

#Read and discard the first four bytes (the magic pyc identifier).
istream.seek(0)
istream.read(4)

#The last modification time of the source according to the file.
modtime = istream.read(4)
modtime = struct.unpack('i', modtime)[0] # 'L' would require 8 bytes (x86_64 ?)
print("Modtime extracted from random.pyc file")
print(modtime)

istream.close()

#Convert the bytes to an integer.

print('Retrieving source modification time using os.path.getmtime.')
print('According to os.path.getmtime the time the source file was last modified measured in the number of seconds from the last epoch is:')

print(os.path.getmtime(sourcefilepath))

"""
My output --->
Modtime extracted from random.pyc file
1262995199
Retrieving source modification time using os.path.getmtime.
According to os.path.getmtime the time the source file was last modified measured in the number of seconds from the last epoch is:
1262995199.0
"""

I'm running Mandriva Linux 2010 with 64 bits.

lrh9 commented: Sincerely cares about learning and helping others. +1
Gribouillis 1,391 Programming Explorer Team Colleague

Try this

from itertools import permutations
for t in permutations(['e', 'n', 'o', 't', 'r']):
    print t

;)

Gribouillis 1,391 Programming Explorer Team Colleague
Gribouillis 1,391 Programming Explorer Team Colleague

Use this code snippet. Write com = Command("iwconfig").run() .

Gribouillis 1,391 Programming Explorer Team Colleague

The problem is that the print statement is executed before the testit thread had time to establish a connection and receive data. The only thing to do is to wait before the print statement. There are 2 ways to wait here:
First way: you wait until the testit thread is terminated before you make the print statement, for this, you must use the join method like this

current=testit("192.168.0.1")
current.start()
current.join()
print current.ip,current.status

This is probably not what you want to do, so the Second way to wait is to use a threading.Condition instance. You can do it this way

# from __future__ import with_statement # uncomment if python 2.5
from threading import Condition, Thread
from contextlib import contextmanager

@contextmanager
def acquired(condition):
    condition.acquire()
    try:
        yield condition
    finally:
        condition.release()

connection_done = Condition()

class testit(Thread):
    ....
    def run(self):
        try:
            port = 1000
            ...
            self.status = repr(data)
        finally:
            with acquired(connection_done):
                connection_done.notify()
        ...

current=testit("192.168.0.1")
with acquired(connection_done):
    current.start()
    connection_done.wait()
    if current.status == "down":
        print "testit thread could not establish a connection"
print current.ip,current.status
rlogan1 commented: nice response.Thanks +0
Gribouillis 1,391 Programming Explorer Team Colleague

I already see a problem, you should swap lines 20 and 21 because here, in line 21, you are giving a string value to self.value.
In the __init__ method, you should never have written line 4 if you want to give self.rank a string value, you can work with the rank argument directly.