Gribouillis 1,391 Programming Explorer Team Colleague

This small script named sourcecode.py is a command line tool to open a python module's source file with the default editor. For example the line

$ python sourcecode.py xml.dom.minidom

opens the file minidom.py from the python distribution with the default editor. The same works with user-defined modules having a python source file.

Gribouillis 1,391 Programming Explorer Team Colleague

I suggest the official python tutorial to start with.

Gribouillis 1,391 Programming Explorer Team Colleague

Regex's anyone?

I would use

r".*(?:\.py|\.txt)\Z(?ims)"

because glob uses fnmatch, which translates shell patterns into python regex, and this is how '.*' is translated:

>>> from fnmatch import translate
>>> translate('*.py')
'.*\\.py\\Z(?ms)'

I think it makes a difference if file names contain newline characters. The following works in my linux system, creating a file name containing newlines

>>> with open("foo\nbar.txt\n", "w") as ofh:
...  ofh.write('foobar')
... 
>>> 

your regex will list this file which does not end with .txt.

Hiroshe commented: good point! +7
Gribouillis 1,391 Programming Explorer Team Colleague

This script computes the formal series expansion of a mathematical function on the command line using the python module swiginac, an interface to the CAS Ginac. Typical invocation in a linux terminal looks like

$ serexp.py -n 7 "log(1+x)*(1+x+x**2)**(-1)" 
1*x+(-3/2)*x**2+5/6*x**3+5/12*x**4+(-21/20)*x**5+7/15*x**6+Order(x**7)

As far as I know, swiginac does not work in windows, so use a virtual (k)ubuntu machine in this case.

Gribouillis 1,391 Programming Explorer Team Colleague

I think the most inner while ans: serve no purpose. You could remove them. On the other hand, I would replace the while ans at line 2 by while True (which means loop indefinitely), because the variable ans takes different meanings at different places, so you don't really control its value.

The correct way to write this code is to use functions, but perhaps you didn't learn what functions are ?

Gribouillis 1,391 Programming Explorer Team Colleague

Suppose you have

if ans == "2":
    print("foo")
if ans == "3":
    print("bar")
else:
    print("qux")

and ans is the string "2". Foo is printed in the first if, but then the program tries the test ans == "3" and as it fails, the else part is executed, printing qux. Here the else part applies to if ans == "3" only.

With elif, "foo" is printed, but the second test is not tested, and the else part is not executed. The else part would be executed only if none of the if, elif ... cases applies.

Gribouillis 1,391 Programming Explorer Team Colleague

Instead of

if ...
if ...
if ...
else ...

use

if ...
elif ...
elif ...
else ...

If you need to go to the next iteration of a while loop, you can use a

continue

statement.

Gribouillis 1,391 Programming Explorer Team Colleague

Of course, I said it is this code snippet: http://www.daniweb.com/software-development/python/code/374530/post-process-generated-values-with-a-decorator
Here is the contents of post_process.py

# python >= 2.6
from functools import update_wrapper

def post_process(*filters):
    """Decorator to post process a function's return value through a
    sequence of filters (functions with a single argument).

    Example:

        @post_process(f1, f2, f3)
        def f(*args, **kwd):
            ...
            return value

        then calling f(...) will actually return f3( f2( f1( f(...)))).

        This can also be used to convert a generator to a function
        returning a sequence type:

        @post_process(dict)
        def my_generator():
            ...
            yield key, value

    """

    def decorate(func):
        def wrapper(*args, **kwd):
            rv = func(*args, **kwd)
            for f in filters:
                rv = f(rv)
            return rv
        update_wrapper(wrapper, func)
        return wrapper
    return decorate
Gribouillis 1,391 Programming Explorer Team Colleague

Sorry I meant

@post_process(list)
def gather(xml_doc, paths):
    for p in paths:
        node = xml_doc
        for word in p:
            node = node[word]
        yield node

getitem() exists, but it is in module operator. You must also import post_process() from another file. For example, store it in a module post_process.py and write

from post_process import post_process
Gribouillis 1,391 Programming Explorer Team Colleague

In this case, the decorator transform a function which generate values into a function which returns a list of these values. Without the decorator, I would write

def gather(xml_doc, paths):
    result = []
    for p in paths:
        node = xml_doc
        for word in p:
            node = getitem(node, word)
        result.append(node)
    return result

This way to compose the resulting list is tedious, and this is a recurrent pattern. That's why I use the decorator.

Gribouillis 1,391 Programming Explorer Team Colleague

You can define a function to do the same easily

@post_process(list)
def gather(xml_doc, paths):
    for p in paths:
        node = xml_doc
        for word in p:
            node = getitem(node, word)
        yield node


a = gather(xml_doc, (('meeting', '@id'), ('meeting', '@venue')))

post_process is defined in this code snippet (very useful bit).

Gribouillis 1,391 Programming Explorer Team Colleague

The function get_inverse_dict() currently returns None. After you insert your code, it is supposed to return the dictionary

{'a': 'z', 'c': 'x', 'b': 'y', 'e': 'v', 'd': 'w', 'g': 't', 'f': 'u', 'i': 'r', 'h': 's', 'k': 'p', 'j': 'q', 'm': 'n', 'l': 'o', 'o': 'l', 'n': 'm', 'q': 'j', 'p': 'k', 's': 'h', 'r': 'i', 'u': 'f', 't': 'g', 'w': 'd', 'v': 'e', 'y': 'b', 'x': 'c', 'z': 'a'}

but you don't need to write the whole dictionary. Instead, follow the hints.

The function test_get_inverse_dict() executes without error if get_inverse_dict() returns the expected result. It is here to evaluate your code automatically. You don't need to understand how it works. Actually, a more complete version would be

def test_get_inverse_dict():
    result = get_inverse_dict()
    expected = 122
    for c in string.ascii_lowercase:
        assert ord(result[c]) == expected
        expected -= 1
    assert len(result) == 26
saimadanmohan commented: thanks a lot i got it +0
Gribouillis 1,391 Programming Explorer Team Colleague

Perhaps play with the .next_siblings and .sibling attributes of the title tag.

Gribouillis 1,391 Programming Explorer Team Colleague

This is a typical use case for generators and tools for iterables. Instead of writing to a file, use a generator

import os
import xml.etree.ElementTree as ET
from itertools import ifilterfalse

def generate_lines():
    tree = ET.parse("Logfile.xml")
    root = tree.getroot()
    for element in root.iter('Path'):
        file_name = os.path.basename(element.text)
        #.exe just an example that you can have more values
        if not file_name.endswith(('.exe', '.fmt', 'sys', 'tmp')):
            #print 'copy %windir%\system32\', file_name, '%1%'
            yield ('copy %windiw&\system32\{} %1%\n').format(
                os.path.basename(element.text))

Now, elimination of duplicates is a standard pattern. There is a generic solution at the end of itertools' module documentation

def unique_everseen(iterable, key=None):
    "List unique elements, preserving order. Remember all elements ever seen."
    # unique_everseen('AAAABBBCCDAABBB') --> A B C D
    # unique_everseen('ABBCcAD', str.lower) --> A B C D
    seen = set()
    seen_add = seen.add
    if key is None:
        for element in ifilterfalse(seen.__contains__, iterable):
            seen_add(element)
            yield element
    else:
        for element in iterable:
            k = key(element)
            if k not in seen:
                seen_add(k)
                yield element

We're ready to write the output file

with open('my_file.txt', 'w') as f_out:
    lines = unique_everseen(generate_lines())
    f_out.writelines(lines)
Gribouillis 1,391 Programming Explorer Team Colleague

By binary I meant read/write 0"s and 1"s of the file

The 0's and 1's are read, but you can't see them. They are read in bytes (for example the byte 'a' is stored as 8 bits 01100001). If you want to see them, you can use this snippet. For example

>>> mystr = """\
... test of
... a multiline
... string
... """
>>> a2bits(mystr)
'011101000110010101110011011101000010000001101111011001100000101001100001001000000110110101110101011011000111010001101001011011000110100101101110011001010000101001110011011101000111001001101001011011100110011100001010'
>>> 

What is a .dat file anyways?

It is nothing special, .dat means data. It is used for data files without a specific file extension.

Gribouillis 1,391 Programming Explorer Team Colleague

Read the doc ! Apparently cv2.imread() expects a filename.

Gribouillis 1,391 Programming Explorer Team Colleague

Use one of vegaseat's tkinter examples. You can adapt the example by reading the file from your disk instead of the internet.

Gribouillis 1,391 Programming Explorer Team Colleague

but .read() just prints the file contents not its binary

What do you mean exactly ? Give a binary example (you can attach a zip or an image to a post).
read() does not print anything, it reads the binary file's content. Please explain what you're expecting and what you get.

Gribouillis 1,391 Programming Explorer Team Colleague

Why not post the code and the exception traceback ?

Gribouillis 1,391 Programming Explorer Team Colleague

Here is a long video tutorial about this.

Gribouillis 1,391 Programming Explorer Team Colleague

I'm using kubuntu, but it should work in debian: make sure you have the development packages for python (python-dev and libpython-dev):

sudo aptitude search python-dev
# then may be
sudo aptitude install python-dev
sudo aptitude install libpython-dev
sudo aptitude install python-setuptools
sudo aptitude install python-pip

Then

sudo pip install pycrypto

should work.

There are windows binaries here but there are reported to be incomplete here. Make sure you visit this page.

Gribouillis 1,391 Programming Explorer Team Colleague

You can improve the program by using the standard module cmd which purpose is to write interactive command line interpreters. I wrote an enhanced class of cmd.Cmd in this code snippet. It makes it very easy to write an interpreter: copy the class ExampleCmd at the end of the file and add methods do_ls() do_add() do_open() etc with arguments and docstring. The help command is automatically provided.

Gribouillis 1,391 Programming Explorer Team Colleague

Python 3 is probably already installed in your linux system. Open a terminal and type

python3

to see what it does.

Gribouillis 1,391 Programming Explorer Team Colleague

I see 2 solutions

  1. You wait until python 3.4 is the packaged python 3 version in your linux distro (this is currently python 3.3.2+ in my kubuntu).
  2. You download the source tarball and you compile it yourself, using something like 'configure' and 'make altinstall'. There may be dependencies for the huge python standard library.

Most probably, you don't need python 3.4. Use the last python 3.3 for some time.

Gribouillis 1,391 Programming Explorer Team Colleague

Use the format method. Here are the different ways to print a floating number centered in a field of length 10, with precision of 2 digits

>>> s = "{val:^10.2e}{val:^10.2E}{val:^10.2f}{val:^10.2F}{val:^10.2g}{val:^10.2G}{val:^10.2n}{val:^10.2%}"
>>> print(s.format(val = 0.1))
 1.00e-01  1.00E-01    0.10      0.10      0.1       0.1       0.1      10.00%  
Gribouillis 1,391 Programming Explorer Team Colleague

Here is a very simple example using the microframework bottle (install with pip for example)

# -*-coding: utf8-*-
# webrobot.py

from bottle import get, post, request
from bottle import route, run

@get('/movearm')
def movearm_form():
    return '''<form method="POST" action="/movearm">
                Move the arm <input type="submit"/>
              </form>'''

@post('/movearm')
def movearm_submit():
    from robotprogram import robotinstance
    robotinstance.move_arm(t=2.0, cmd='bac')
    return "<p>Arm moved !</p>"

run(host='localhost', port=8383, reloader=True)

Launch this script, then visit the bottle webserver at http://localhost:8383/movearm . Here I assume that I can import a robot instance with from robotprogram import robotinstance. Adapt this to your case. I used a mock object

# robotprogram.py
class Robot(object):
    def move_arm(self, *args, **kwd):
        print("HELLO")
        pass

robotinstance = Robot()

Bottle can also use your apache server, as described here.

Gribouillis 1,391 Programming Explorer Team Colleague

The answer is yes it is possible to create global variables this way, but it is considered poor programming. Here is this forbidden fruit

globals().update((name, 'foo') for name in list1)

Now all the variables exist, with value the string "foo".

Apart from this bad way to do it there are several good alternatives. The first one is to use a dictionary:

val = dict((name, 'foo') for name in list1)

Then val["A"] has value 'foo'. Another way is to use a class

class val:
    pass

for name in list1: setattr(val, name, 'foo')

Then val.A is 'foo' etc.

One can also use a class instance, often I use

class Static(object):
    def __init__(self, *args, **kwd):
        self.__dict__.update(*args, **kwd)

val = Static((name, 'foo') for name in list1)

val.A = 'bar'
print(val.A)

Edit: bugfix for class Static.

Gribouillis 1,391 Programming Explorer Team Colleague

I don't know, it seems that you are copying and pasting code without understanding it. It can't work this way. Your code is not properly indented with 4 spaces. I gave you the link to Tim Peter's reindent script, why don't you use it to clean your program, and configure your editor to indent with 4 spaces ? Also your Tkinter program doesn't need 3 calls to mainloop(). Normally only one call is needed. It means that you don't understand what mainloop does. etc. I think you want to go too fast, but you should read a python tutorial such as dive into python 3.

Gribouillis 1,391 Programming Explorer Team Colleague

You can iterate on sorted keys:

for channel_key in sorted(channels):
    ...

The other thing you can do is use

channels = OrderedDict()

instead of channels = {}. In py 2.6, ordereddict is here. Then the dict should be traversed in the insertion order (while sorted() yields alphabetic order, which could be different)

mark103 commented: Very good thank you +0
Gribouillis 1,391 Programming Explorer Team Colleague

Again we don't know precisely the information that you need. Apparently, your numbers are ordered (but the last number), so I suppose that you treat the first 5 numbers as a set rather than a list. Here is a piece of code which computes a score function for each line by comparing the line to the target line. The score is a pair of numbers (n, m), where n
is the number of correct guesses among the 5 first numbers, and m is 0 or 1 depending on
the last number (1 if it matches the target, 0 otherwise). I'm sure you can use this score function in your code.

data = [line.split() for line in """
13 27 34 41 47 - 49
22 24 45 46 51 - 15
10 14 22 23 42 - 13
01 04 17 31 52 - 38
12 23 40 47 57 - 04
""".strip().splitlines()]

target = "07 13 14 23 45 - 07".split()

print(data)
print(target)

def score(a, b):
    return (len(set(a[:5]) & set(b[:5])), 1 if (a[-1] == b[-1]) else 0)

print()
for a in data:
    print(a, score(a, target))

""" my output -->
[['13', '27', '34', '41', '47', '-', '49'], ['22', '24', '45', '46', '51', '-', '15'], ['10', '14', '22', '23', '42', '-', '13'], ['01', '04', '17', '31', '52', '-', '38'], ['12', '23', '40', '47', '57', '-', '04']]
['07', '13', '14', '23', '45', '-', '07']

['13', '27', '34', '41', '47', '-', '49'] (1, 0)
['22', '24', '45', '46', '51', '-', '15'] …
Gribouillis 1,391 Programming Explorer Team Colleague

So I don't understand what you did.

Your initial question is ambiguous. You're describing what you want to do, instead of describing the information you need.

It would be easier if you tell us exactly what you want from the lists

x0 x1 x2 x3 x4 - x5
y0 y1 y2 y3 y4 - y5

For example "I want the list of items in x0 x1 x2 x3 x4 which belong to y0 y1 y2 y3 y4". That would be

a = ['06', '15', '26', '34', '36', '-', '16']
b = ['16', '24', '34', '30', '43', '-', '20']

result = list(set(b[:5])& set(a[:5]))

or "I want the list of integers i for which a[i] and b[i] are equal". That would be

m = min(len(a), len(b))
indexes = [i for i in range(m) if a[i] == b[i]]
Gribouillis 1,391 Programming Explorer Team Colleague

Yes, you must learn to manipulate python's container data types: lists, sets, dicts. You can do many things with your data

a = ['06', '15', '26', '34', '36', '-', '16']
b = ['16', '24', '30', '34', '43', '-', '20']

xa = a[:-2] # take all elements but the last 2 ones
xb = b[:-2]

nl = '\n'
print(xa, xb, sep = nl)
"""
['06', '15', '26', '34', '36']
['16', '24', '30', '34', '43']
"""

ya = set(xa) # build unordered sets instead of lists
yb = set(xb)
print(ya, yb, sep = nl)
"""
{'15', '26', '34', '06', '36'}
{'16', '43', '34', '24', '30'}
"""
print(ya & yb)
print(ya - yb)
print(yb - ya)
print(ya == yb)
"""
{'34'} # elements in ya and yb
{'15', '26', '36', '06'} # items in ya but not in yb
{'16', '30', '43', '24'} # items in yb but not in ya 
False # the 2 sets are different
"""
lasta = a[-1] # take the last numbers
lastb = b[-1]
print(lasta, lastb, lasta == lastb, sep = nl)
"""
16
20
False
"""
Gribouillis 1,391 Programming Explorer Team Colleague

Do you know what a python list is ? Read this example about reading a data file line by line and manipulate its content.

Gribouillis 1,391 Programming Explorer Team Colleague

A hint:

>>> line = '14 18 35 47 54 - 57\n'
>>> line.split()
['14', '18', '35', '47', '54', '-', '57']
Gribouillis 1,391 Programming Explorer Team Colleague

will exit your program after 5 seconds

This fails, but using os._exit(0) works, instead of sys.exit(0).

In my linux system, the following also works, which is very nice

import thread
import threading

threading.Timer(2.0, lambda : thread.interrupt_main()).start()

try:
    while True:
        print("stopme")
except KeyboardInterrupt:
    print("We're interrupted: performing cleanup action.")

""" my output:
...
stopme
stopme
stopme
stopme
stopme
We're interrupted: performing cleanup action.
$
"""

The nice thing is that we only plan to raise an exception in the main thread after a certain time instead of exiting directly. This is very pythonic.

CrazyDieter commented: you're right, I prefer this way too +3
Gribouillis 1,391 Programming Explorer Team Colleague

Install an ssh server in the remote host and use module paramiko. This post is a little old, but it could help.

Gribouillis 1,391 Programming Explorer Team Colleague

There is also a solution with the re module

import re

def cap(match):
    return match.group(0).capitalize()

with open('c:\FLOUpper.txt', 'r') as infile, open('c:\FLOLower.txt', 'w') as outfile:
    s = infile.read()
    s = re.sub(r'\b[a-zA-Z]\w*', cap, s)
    outfile.write(s)
Shaji_1 commented: Utilized this solution and it worked like a charm. Thank you! +0
Gribouillis 1,391 Programming Explorer Team Colleague

Probably the worst isPrime() function I've ever seen. If num is above 1000000, you are going to stack 1000000 recursive calls ! Python comes with a recursion limit

>>> import sys
>>> sys.getrecursionlimit()
1000

which means that your code will fail. The limit can be changed, but in every day programming, it does not need to.

There is no question in your post. Is it a code snippet ?

Gribouillis 1,391 Programming Explorer Team Colleague

Add print('Current item is:', repr(item)) between lines 3 and 4.

Gribouillis 1,391 Programming Explorer Team Colleague

Yes, but you will write in python 3 in the future if you don't do it yet, so why not write (almost) compatible code right now ? There are good reasons for this

  • Python 3 is better than python 2 (more consistent and homogeneous)
  • It is not more difficult to write python 3 code
  • Your code may run out of the box the day you decide it is time to use python 3, so less work for tomorrow
Gribouillis 1,391 Programming Explorer Team Colleague

Here is the code of distutils.dir_util.copy_tree()

def copy_tree(src, dst, preserve_mode=1, preserve_times=1,
              preserve_symlinks=0, update=0, verbose=1, dry_run=0):
    """Copy an entire directory tree 'src' to a new location 'dst'.

    Both 'src' and 'dst' must be directory names.  If 'src' is not a
    directory, raise DistutilsFileError.  If 'dst' does not exist, it is
    created with 'mkpath()'.  The end result of the copy is that every
    file in 'src' is copied to 'dst', and directories under 'src' are
    recursively copied to 'dst'.  Return the list of files that were
    copied or might have been copied, using their output name.  The
    return value is unaffected by 'update' or 'dry_run': it is simply
    the list of all files under 'src', with the names changed to be
    under 'dst'.

    'preserve_mode' and 'preserve_times' are the same as for
    'copy_file'; note that they only apply to regular files, not to
    directories.  If 'preserve_symlinks' is true, symlinks will be
    copied as symlinks (on platforms that support them!); otherwise
    (the default), the destination of the symlink will be copied.
    'update' and 'verbose' are the same as for 'copy_file'.
    """
    from distutils.file_util import copy_file

    if not dry_run and not os.path.isdir(src):
        raise DistutilsFileError, \
              "cannot copy tree '%s': not a directory" % src
    try:
        names = os.listdir(src)
    except os.error, (errno, errstr):
        if dry_run:
            names = []
        else:
            raise DistutilsFileError, \
                  "error listing files in '%s': %s" % (src, errstr)

    if not dry_run:
        mkpath(dst, verbose=verbose)

    outputs = []

    for n in names:
        src_name = os.path.join(src, n)
        dst_name = os.path.join(dst, n)

        if n.startswith('.nfs'):
            # skip …
Gribouillis 1,391 Programming Explorer Team Colleague

There is still no bug in my code

from anyfloat import anyfloat

_size = { 4 : (8, 23), 5 : (8, 31)}

def variable_float(n, bytes = 4):
    return float(anyfloat.from_ieee(n, _size[bytes]))

print repr(variable_float(0x41973333))
print repr(variable_float(0x41995C29))
print repr(variable_float(0x470FC614))
print repr(variable_float(0x00800000))
print repr(variable_float(0x7F7FFFFF))
print repr(variable_float(0x3f80000000, 5))

""" my output -->
18.899999618530273
19.170000076293945
36806.078125
1.1754943508222875e-38
3.4028234663852886e+38
1.0
"""

See here.

TrustyTony commented: Power of modular programming +12
Gribouillis 1,391 Programming Explorer Team Colleague

After line 3, add

print(repr(password))
print(repr(userguess))

then you can compare visually the two strings.

Gribouillis 1,391 Programming Explorer Team Colleague

Try

sql = ("SELECT * FROM `emaillogs`"
    " WHERE `id` BETWEEN {i1:d} AND {i2:d}"
    " ORDER BY `id` DESC").format(
        i1 = int(limit1), # type checking with conversion
        i2 = int(limit2),
    )
Gribouillis 1,391 Programming Explorer Team Colleague

This python script prints all the columns of an sqlite database which path is given as argument.

Gribouillis 1,391 Programming Explorer Team Colleague

Use the python tutorial for correct syntax.

Gribouillis 1,391 Programming Explorer Team Colleague

Notice that in previous post, you could write

sf = "There are {} days between {:%d%b%Y} and {:%d%b%Y}"
print(sf.format(days, date1, date2))
Gribouillis 1,391 Programming Explorer Team Colleague

Use a datetime.timedelta instance to represent the difference between two dates

yA, mA, dA = 1969,7, 21
yB, mB, dB = 2013, 12, 22

import datetime as dt

dateA = dt.date(yA, mA, dA)
dateB = dt.date(yB, mB, dB)
diff = dateB - dateA # a timedelta object
print ("diff is {0} days. (could be negative)".format(diff.days))
print ("and {0} seconds. (nonnegative, less than one day)".format(diff.seconds))
print ("and {0} microseconds. (nonnegative, less than one second)".format(diff.microseconds))

""" my output -->
diff is 16225 days. (could be negative)
and 0 seconds. (nonnegative, less than one day)
and 0 microseconds. (nonnegative, less than one second)
"""
Gribouillis 1,391 Programming Explorer Team Colleague

I think it should be img.save(LOGFILE_PATH)

Gribouillis 1,391 Programming Explorer Team Colleague

The home directory is best obtained with

home_dir = os.path.expanduser("~")

It is cross-platform !