Gribouillis 1,391 Programming Explorer Team Colleague

There are different ways. 1: declare outline global inside the function

outline = True # when the program starts

def toggle(*args): # args ignored
    global outline # at the top of the function
    if outline == True:
        outline = False
    if outline == False:
        outline = True

2: use a class to hold global variables (avoid global statement)

class glo:
    outline = True # when the program starts

def toggle(*args):
    glo.outline = not glo.outline

3: use a module to hold global variables (especially useful is your program uses more than one file)

# in file glo.py
outline = True

# in your main program
import glo

def toggle(*args):
    glo.outline = not glo.outline

3: Create a class to hold global variables and function and use a single instance

class MyApp:
    def __init__(self):
        self.outline = True

    def toggle(self, *args):
        self.outline = not self.outline

if __name__ == '__main__':
    my_app = MyApp()
    # ...
    Button(..., command = my_app.toggle)
    #...
    foo.mainloop()

4: etc

Gribouillis 1,391 Programming Explorer Team Colleague

for next step , i just need to know a better way to send the file ( . the zip file created ) to an other computer (on windows environnement).

The easiest way I know to do that is to have a ssh server running on the remote computer and connect using module paramiko.

Gribouillis 1,391 Programming Explorer Team Colleague

Where are your efforts to solve the assignment ?

Gribouillis 1,391 Programming Explorer Team Colleague

To add the files in the subfolders, you must generate their paths.

Gribouillis 1,391 Programming Explorer Team Colleague

Here are some examples of the format() method

Python 3.3.1 (default, Apr 17 2013, 22:32:14) 
[GCC 4.7.3] on linux
>>> "Give me {0} and {2}, said the {1} man".format("bacon", "other", "eggs")
'Give me bacon and eggs, said the other man'
>>> "Give me {0} and {2} eggs, said the {1} man".format("bacon", "other", 5)
'Give me bacon and 5 eggs, said the other man'
>>> "Give me {0} and {2:.2f} eggs, said the {1} man".format("bacon", "other", 5)
'Give me bacon and 5.00 eggs, said the other man'
>>> "Give me {} and {}, said the {} man".format("bacon", "eggs", "other")
'Give me bacon and eggs, said the other man'
>>> "Give me {} and {:.2f} eggs, said the {} man".format("bacon", 5, "other")
'Give me bacon and 5.00 eggs, said the other man'
Gribouillis 1,391 Programming Explorer Team Colleague

Here is an example of creating a hierarchies of folders and files and zipping them in an archive. The main argument is to generate the correct sequence of arguments for ZipFile.write(). This is done by post-processing the output of os.walk().

#!/usr/bin/env python
# -*-coding: utf8-*-
import contextlib
import itertools
import os.path
import zipfile

ROOT = 'Haplorhini'
ROOT_TREE = {
    'Tarsiiformes':{
        'Tarsiidae.txt': None,
    },
    'Simiiformes':{
        'Platyrrhini':{
            'Callitrichidae.txt':None,
            'Cebidae.txt':None,
            'Aotidae.txt':None,
            'Pitheciidae.txt':None,
            'Atelidae.txt':None,
        },
        'Catarrhini':{
            'Cercopithecoidea':{
                'Cercopithecidae.txt':None,
            },
            'Hominoidea':{
                'Hylobatidae.txt':None,
                'Hominidae.txt':None,
            },
        },
    },
}

@contextlib.contextmanager
def aswd(path):
    fold = os.getcwd()
    os.chdir(path)
    try:
        yield
    finally:
        os.chdir(fold)

def make_hierarchy(name, dic):
    if not os.path.isdir(name):
        os.mkdir(name)
    with aswd(name):
        for key, value in dic.items():
            if isinstance(value, dict):
                make_hierarchy(key, value)
            else:
                with open(key, 'w') as ofh:
                    ofh.write(key)

def raw_os_walk_sequence(dirs):
    return list(itertools.chain.from_iterable(os.walk(d) for d in dirs))

def modified_sequence(dirs):
    for d, dd, f in raw_os_walk_sequence(dirs):
            for x in f:
                yield os.path.join(d, x)

if __name__ == '__main__':
    make_hierarchy(ROOT, ROOT_TREE)
    with aswd(ROOT):
        dirs = [d for d in os.listdir('.') if os.path.isdir(d)]
        print('RAW OS:WALK() SEQUENCE:')
        for x in raw_os_walk_sequence(dirs):
            print(x)
        print('MODIFIED SEQUENCE:')
        for x in modified_sequence(dirs):
            print(x)

        zip = zipfile.ZipFile(os.path.join(os.path.expanduser('~'), 'result.zip'), 'w')
        for x in modified_sequence(dirs):
            zip.write(x, '/'.join(os.path.split(x)))

''' my output -->
RAW OS:WALK() SEQUENCE:
('Tarsiiformes', [], ['Tarsiidae.txt'])
('Simiiformes', ['Platyrrhini', 'Catarrhini'], [])
('Simiiformes/Platyrrhini', [], ['Atelidae.txt', 'Pitheciidae.txt', 'Callitrichidae.txt', 'Aotidae.txt', 'Cebidae.txt'])
('Simiiformes/Catarrhini', ['Hominoidea', 'Cercopithecoidea'], [])
('Simiiformes/Catarrhini/Hominoidea', [], ['Hylobatidae.txt', 'Hominidae.txt'])
('Simiiformes/Catarrhini/Cercopithecoidea', [], ['Cercopithecidae.txt'])
MODIFIED SEQUENCE:
Tarsiiformes/Tarsiidae.txt
Simiiformes/Platyrrhini/Atelidae.txt
Simiiformes/Platyrrhini/Pitheciidae.txt
Simiiformes/Platyrrhini/Callitrichidae.txt
Simiiformes/Platyrrhini/Aotidae.txt
Simiiformes/Platyrrhini/Cebidae.txt
Simiiformes/Catarrhini/Hominoidea/Hylobatidae.txt
Simiiformes/Catarrhini/Hominoidea/Hominidae.txt
Simiiformes/Catarrhini/Cercopithecoidea/Cercopithecidae.txt

and of course, the file ~/result.zip !
'''

The next step is to handle unicode system paths.

Gribouillis 1,391 Programming Explorer Team Colleague

Here is pseudo code

for each item in list 1:
    item defines a pair (key, value)
    eg for the item  <14.7992581813 41.5425583232 650.00>,
    the key is <14.7992581813 41.5425583232> and the value
    is <650.00>.

Store all these pairs (key, value) in a dictionary D, and raise
an exception if one of the keys appears twice.

For each item in list 2:
    extract the key from the item, eg the key for
    <"string1" 2275.2 3323.52 14.8016700793 41.5425025232 n/a> is
    <14.8016700793 41.5425025232>.
    If the key is in D, find the corresponding value D[key]. Output <"string1" 2275.2 3323.52 14.8016700793 41.5425025232 value> in this case.
Gribouillis 1,391 Programming Explorer Team Colleague

galons_used and gallons_used are 2 different variables. Typo.

Gribouillis 1,391 Programming Explorer Team Colleague

This line uses the string format() method to build a regular expression. For example

>>> import re
>>> wanted = ('cat', 'dog', 'parrot')
>>> regex = '^(?:{0})'.format('|'.join(re.escape(s) for s in wanted))
>>> regex
'^(?:cat|dog|parrot)'

This regex is able to tell if a string starts with any of the words cat, dog and parrot. Read this for a tutorial on regular expressions and this for the format() method.

Gribouillis 1,391 Programming Explorer Team Colleague

Which parts of the code don't you understand ?

Gribouillis 1,391 Programming Explorer Team Colleague

I managed to fool the zipfile module by using a StringIO as output file:

from StringIO import StringIO

class Crazip(StringIO):
    def __init__(self, capacity):
        StringIO.__init__(self)
        self.capacity = capacity
        self.check()

    def write(self, data):
        StringIO.write(self, data)
        self.check()

    def writelines(self, lines):
        for x in lines:
            self.write(x)

    def check(self):
        if self.tell() > self.capacity:
            raise RuntimeError("Zip capacity exceeded")

from zipfile import ZipFile
KB = 1024
MB = 1024 * KB

MAX = 1 * KB # replace with 3 * MB
FILENAME = "/home/eric/foo.ps"

ofh = Crazip(MAX)
zipf = ZipFile(ofh, 'a')
zipf.write(FILENAME) # Add one or more files to the archive
zipf.close()

''' my output
Traceback (most recent call last):
  File "Documents/crazip.py", line 27, in <module>
    zipf.write("/home/eric/foo.ps")
  File "/usr/lib/python2.7/zipfile.py", line 1169, in write
    self.fp.write(buf)
  File "Documents/crazip.py", line 12, in write
    self.check()
  File "Documents/crazip.py", line 20, in check
    raise RuntimeError("Zip capacity exceeded")
RuntimeError: Zip capacity exceeded
Exception RuntimeError: RuntimeError('Zip capacity exceeded',) in <bound method ZipFile.__del__ of <zipfile.ZipFile object at 0x8e1f60c>> ignored
'''

You should be able to create the archive in memory with this technique. If it works, simply write the contents of the StringIO to a file.

Gribouillis 1,391 Programming Explorer Team Colleague

I would start with regular expressions and itertools

import itertools
import re
wanted = (
     'Final energy =',
     'Total charge on defect =',
     'Final defect energy =',
     '**** Optimisation achieved ****',
)
regex = '^(?:{0})'.format('|'.join(re.escape(s) for s in wanted))
regex = re.compile(regex)
with open('filename.txt', 'rb') as lines:
    lines = itertools.ifilter(regex.match, lines)
    for line in lines:
        print(line)
Gribouillis 1,391 Programming Explorer Team Colleague

My preference would be Python module shelve, which creates a 'persistent to file' dictionary.

I don't like shelve. I think it is very slow. Did you try cerealizer ? (in linux mint, install it with the package manager!)

Gribouillis 1,391 Programming Explorer Team Colleague

After df=df1.to_string(), df is a str object (a sequence of bytes). You can use df[11] which gives you the 12th character in the sequence, but not df["Item Class Cd"].

Gribouillis 1,391 Programming Explorer Team Colleague

Apparently, you are using pandas DataFrame instances. The api reference for these objects contains conversion methods. I would try

s = df.to_string()
Gribouillis 1,391 Programming Explorer Team Colleague

Python has Lettuce. But cucumber claims to work with python too.

Gribouillis 1,391 Programming Explorer Team Colleague

Why did you declare i = "" ? Python cannot add a str to an int.

Gribouillis 1,391 Programming Explorer Team Colleague

Here is my version for python 2

#!/usr/bin/env python
#-*-coding: utf8-*-

def prime_check(n):
    '''
    check if integer n is a prime, return True or False
    '''
    # 2 is the only even prime
    if n == 2:
        return True
    # integers less than 2 and even numbers other than 2 are not prime
    elif n < 2 or not n & 1:
        return False
    # loop looks at odd numbers 3, 5, 7, ... to sqrt(n)
    for i in range(3, int(n**0.5)+1, 2):
        if n % i == 0:
            return False
    return True

def main():
    prime = 0
    composite = 0;
    from array import array
    arr = ''
    src = array('b', (
        'cwl8yv0wxuo?8v#wxa#yte3m0vr#5tdbt$k?j'
        '7ovzg$@$62nk@6c0$s#bbsmu1gqu7na85u3zl'
        '9903#518ukv2awgk0u#attjehlbu?o5y#c5yw'
        '83b2n@kq8$nq4fgvf9pqj0ejwbj87a33$0nqb'
        '0h04@yf0y7i4qgoq?qeik80be3z4iwqrb?sbb'
        'srwa58k#cd9yt8g?k04jb0opdkflngpepl1rv'
        'tlw1z8j9p#kw$mzv$2p4ep$ycl8r3u5$hvvfz'
        '1ny7vn2vq3a?hoje@czgqxghqccasm432ovlh'
        '8m9m010nt3zv#l2n@8?5h#jyw?f99l5e5e$7w'
        '87g8xfgc#$n7961xobpe315np1ht?dvqpe?h#'
        '@r81o823iyh1so35vhjmzc2x#3?pkjqh1sy5g'
        'c3sp$qvka5a2kcdpasfj@9b7dqvfwgucc5$xk'
        'az3jxp4m#yu?s2j#hf9ul@tg?beeo@p4hn$wm'
        '#f0ev26b7k8g$zz4cps$z9qv95sxg5jwwnedu'
        '@yhq2ldfw#0#siesfrrzgo##zz52rfk$7ot97'
        'mxl#v9re$qgnoxfowgmg?q2lw3gtq7uxyo9ce'
        'osko58caw@82#awxvd3ezou#tzv?6sx9csy1w'
        '3h?d#z7kido'
    ))
    ord_zero = ord('0')

    def isdigit(n):
        return 0 <= n - ord_zero < 10

    for i, x in enumerate(src):
        if isdigit(x):
            n = x - ord_zero
            if n > 1:
                if prime_check(n): # if n in (2, 3, 5, 7):
                    prime += n
                else:
                    composite += n
        elif i < 25:
            arr += chr(x + 1)

    arr += str(prime * composite)

    print(arr)

if __name__ == "__main__":
    main()

""" my output -->
dxmzwxyvp@w$xyb$zufn111435
"""

Instead of a byte string, we use an array of small integers from module array (in python 3, we'd use the byte type, which is a read-only array of small integers). For prime_check(), we use Vegaseat's code

Gribouillis 1,391 Programming Explorer Team Colleague

In your ~/.bashrc , add the line

export PYTHONPATH="$PYTHONPATH:$HOME/foo/bar"

to add ~/foo/bar to the list of directories. You may also create a personal site-packages directory

~/.local/lib/python2.7/site-packages

(or python3.2, ...)

Gribouillis 1,391 Programming Explorer Team Colleague

If not in that directory, you must add it to the $PYTHONPATH variable, not $PATH. ($PATH is for executable programs, not python modules). Another way is to put filename.py in your site-packages directory (or your per-user site-packages directory). The third way is to dynamically add the folder to sys.path at run time.

Gribouillis 1,391 Programming Explorer Team Colleague

The function looks a lot like something that would be part of a standard library. Is it?

There are many potential fluctuations in the way a program can ask for an integer. A library can add many options to a function int_input() to cover most of the cases. The other possibility is to provide only lower level functions like raw_input().

Gribouillis 1,391 Programming Explorer Team Colleague

Suppose you have 3 global variables named foo, bar, baz,
you can write

state['globals'] = {}
for varname in [
    'foo', 'bar', 'baz',
    ]:
    state['globals'][varname] = globals()[varname]

The global variables can then be restored with

for varname, value in state['globals'].items():
    globals()[varname] = value
Gribouillis 1,391 Programming Explorer Team Colleague

how would I load the state?

Read the code previously posted.

Gribouillis 1,391 Programming Explorer Team Colleague

Can you post a full snippet replicating the error ?

Gribouillis 1,391 Programming Explorer Team Colleague

Here is an example function

def int_input(prompt, error_msg):
    while True:
        x = raw_input(prompt)
        try:
            x = int(x)
        except ValueError:
            print(error_msg)
        else:
            return x

x = int_input("Type an integer and hit enter: ", "This was not an integer. Try again.")
Gribouillis 1,391 Programming Explorer Team Colleague

Here is a complete working example

#!/usr/bin/env python
#-*-coding: utf8-*-
from __future__ import unicode_literals, print_function, division

import itergadgets
import pprint

@itergadgets.sorter_grouper
def by2values(item):
    return (item[1][1], item[1][2])

def extract_rows(sequence, preserve_order = False):
    """extract the desired rows from the
    initial sequence of rows"""
    L = (list(group) for group in by2values(enumerate(sequence)))
    L = (g for g in L if len(g) >= 3)
    L = (x for g in L for x in sorted(g))
    if preserve_order:
        L = sorted(L)
    return [x[1] for x in L]

if __name__ == "__main__":
    mylist = [
    ("filenameA", "value1_1", "value2_1", "value3", "value4", "noti"),
    ("filenameA", "value1_2", "value2_2", "value3", "value4", "noti"),
    ("filenameA", "value1_3", "value2_3", "value3", "value4", "noti"),
    ("filenameA", "value1_5", "value2_5", "value3", "value4", "noti"),
    ("filenameA", "value1_7", "value2_7", "value3", "value4", "noti"),
    ("filenameB", "value1_1", "value2_1", "value3", "value4", "noti"),
    ("filenameB", "value1_5", "value2_5", "value3", "value4", "noti"),
    ("filenameB", "value1_7", "value2_7", "value3", "value4", "noti"),
    ("filenameC", "value1_1", "value2_1", "value3", "value4", "noti"),
    ("filenameC", "value1_7", "value2_7", "value3", "value4", "noti"),
    ("filenameC", "value1_9", "value2_9", "value3", "value4", "noti"),
    ]
    res = extract_rows(mylist)
    pprint.pprint(res)


""" my output -->
[(u'filenameA', u'value1_1', u'value2_1', u'value3', u'value4', u'noti'),
 (u'filenameB', u'value1_1', u'value2_1', u'value3', u'value4', u'noti'),
 (u'filenameC', u'value1_1', u'value2_1', u'value3', u'value4', u'noti'),
 (u'filenameA', u'value1_7', u'value2_7', u'value3', u'value4', u'noti'),
 (u'filenameB', u'value1_7', u'value2_7', u'value3', u'value4', u'noti'),
 (u'filenameC', u'value1_7', u'value2_7', u'value3', u'value4', u'noti')]
"""
Gribouillis 1,391 Programming Explorer Team Colleague

Suppose you have a global variable NSCREENS with integer value, you can write

state['NSCREENS'] = NSCREENS

before you save the state on disk. When you load a previously saved state, you can restore the variable with

globals()['NSCREENS'] = state['NSCREENS']
Gribouillis 1,391 Programming Explorer Team Colleague

should the "pass #" be rendered exactly that way or does the "#" stand for something else?

pass is a do-nothing statement. The # is the beginning of a comment.

Gribouillis 1,391 Programming Explorer Team Colleague

Like often in python, the solution is to convert to integer and catch an exception if it fails

try:
    x = int(input_value)
except ValueError:
    pass # do something else
else:
    pass # do something
Gribouillis 1,391 Programming Explorer Team Colleague

You should specify python3 only if your code is written in python 3.

The bash trick is seldom useful. It is for people too lazy to open an editor to start a python script, or for bash programmers.

A more useful idea is to use a file template in your favorite editor. For example kde's kate editor has a file template plugin. It allows to define a default file to start editing a new python script in 1 click. For example you could use

#!/usr/bin/env python
#-*-coding: utf8-*-
from __future__ import unicode_literals, print_function, division

#== docstring ==========================================
__doc__ = """
"""

#== imports ============================================


if __name__ == "__main__":
    pass

Other editors (gedit, vim, emacs, ulipad...) have similar features.

Gribouillis 1,391 Programming Explorer Team Colleague

I suggest that you sort the list on the pair of values and group the records. For example, assuming that the rows are tuples like

("filenameA", 3.14, 5.3, -2.0, 4.0, "notimportant")

You can work with

import itergadgets

@itergadgets.sorter_grouper
def by2values(item):
    return (item[1][1], item[1][2])

def extract_rows(sequence):
    """extract the desired rows from the
    initial sequence of rows"""
    L = (list(group) for group in by2values(enumerate(sequence)))
    L = (g for g in L if len(g) >= 3)
    L = (x for g in L for x in sorted(g))
    return [x[1] for x in L]

The module itergadgets is here. Of course, this method means that the whole list is stored in memory at the same time. Adding sorted at line 12 produces the selected rows in their initial order.

Gribouillis 1,391 Programming Explorer Team Colleague

Can you give a generic example of such a string and the expected result ?

Gribouillis 1,391 Programming Explorer Team Colleague

Here is an example. The program creates a dictionary containing the current state of the game, using simple data types, then saves this snapshot on disk in a json file:

import json

if __name__ == "__main__":

    state = {
        "player_name" : "bob",
        "level" : 3,
        "elapsed_time" : 24.3,
        "monsters" : ["ork", "dragon"]
    }

    # now save the state in a json file

    with open("mygame.json", "w") as ofh:
        json.dump(state, ofh)

    # load the state from the file

    with open("mygame.json", "r") as ifh:
        read = json.load(ifh)

    from pprint import pprint
    pprint(read)

""" my output -->
{'elapsed_time': 24.3,
 'level': 3,
 'monsters': ['ork', 'dragon'],
 'player_name': 'bob'}
"""

The advantage of the json format is that it is human readable and cross programming language. To store more complex python types, use the pickle format.

Gribouillis 1,391 Programming Explorer Team Colleague

Assuming that all the lists are circular (see the title), it can be done without a set. The idea is that the lists are very easy to reverse because they are built in the reversed order.

In the code below, I write a function to reverse a circular list, then by modifying this function, I write a function to reverse the list while removing duplicates. Combining the two gives the algorithm to remove (consecutive) duplicates.

class Node(object):
    def __init__(self, value, next=None):
        self.next=next
        self.value=value

def example_list():
    last = Node(3)
    head = Node(2, last)
    head = Node(5, head)
    head = Node(8, head)
    head = Node(8, head)
    head = Node(8, head)
    head = Node(2, head)
    head = Node(3, head)
    last.next = head
    return head

def circ_reversed(head):
    """return a reversed circular list"""
    if head is None:
        return None
    rev_head = rev_last = Node(head.value)
    current = head.next
    while current is not head:
        rev_head = Node(current.value, rev_head)
        current = current.next
    rev_last.next = rev_head
    return rev_head

def circ_reversed_duplicates_removed(head):
    """return a reversed circular list with duplicates removed"""
    if head is None:
        return None
    rev_head = rev_last = Node(head.value)
    current = head.next
    while current is not head:
        if current.value != rev_head.value:
            rev_head = Node(current.value, rev_head)
        current = current.next
    while rev_head.next is not None and rev_head.value == rev_last.value:
        rev_head = rev_head.next
    rev_last.next = rev_head
    return rev_head

def circ_duplicates_removed(head):
    """return a circular list with duplicates removed"""
    temp = circ_reversed_duplicates_removed(head)
    return circ_reversed(temp)

def circ_to_python_list(head):
    """convert a circular list to a python list"""
    if head is None:
        return []
    result = [ …
Gribouillis 1,391 Programming Explorer Team Colleague

Try Example\$319. Your shell interpretes the $ sign in command lines before executing them.

Gribouillis 1,391 Programming Explorer Team Colleague

Use an existing parser generator module. There are tenth of them in python.A very interesting one, and easy to use is parcon. See also the parcon blog.

Gribouillis 1,391 Programming Explorer Team Colleague

I still need to set a global variable because It carrys over function calls

Use a single global object

class _MuCacheData(object):
    def __init__(self):
        # define here arbitrary containers that you need
        pass

    def mu_cache(self, func, maxsize = 128):
        # write and return your wrapper here
        # Access persistent data through self
        pass


# A single instance is used for the decorator
mu_cache = _MuCacheData().mu_cache

Don't use exec() nor the global statement.

Gribouillis 1,391 Programming Explorer Team Colleague

These books may help you.

ddanbe commented: Nice tip. +14
Gribouillis 1,391 Programming Explorer Team Colleague

What is your question ?

Gribouillis 1,391 Programming Explorer Team Colleague

Here is a small parser. It can be improved by stronger input validation

#!/usr/bin/env python
#-*-coding: utf8-*-

START, END, DATA, EMPTY = range(4)

class ParseError(Exception):
    pass

class Parser(object):
    def __init__(self):
        pass

    def error(self, lineno):
        raise ParseError("Invalid Syntax at line %s" %  str(lineno))

    def parse(self, lines):
        L = [dict()]
        for i, line in enumerate(lines, 1):
            t = self.classify(line, i)
            type, key, data = t
            if type == START:
                L[-1][key] = D = {}
                L.append(D)
            elif type == END:
                del L[-1]
            elif type == DATA:
                L[-1][key] = data
        return L[0]

    def classify(self, line, lineno):
        line = line.strip()
        if not line:
            return (EMPTY, '', '')
        if not(len(line) >= 3 and line[0] == '[' and line[-1] == ']'):
            self.error(lineno)
        if line[1] == '/':
            return (END, line[2:-1], '')
        else:
            i = line.find(']')
            if i == len(line) - 1:
                return (START, line[1:-1], '')
            else:
                return (DATA, line[1:i], line[i+1:-(i+2)])

if __name__ == '__main__':
    example_data = """
[options]
    [user]
        [name]John Doe[/name]
        [age]Age[/age]
    [/user]
    [packages]
        [pkg]
            [version]1.2[/version]
            [summary]Something[/summary]
            [author]John Doe[/author]
        [/pkg]
    [/packages]
[/options]
    """
    from StringIO import StringIO
    from pprint import pprint
    p = Parser()
    f = StringIO(example_data)
    result = p.parse(f)
    pprint(result)

"""my output -->
 {'options': {'packages': {'pkg': {'author': 'John Doe',
                                  'summary': 'Something',
                                  'version': '1.2'}},
             'user': {'age': 'Age', 'name': 'John Doe'}}}
"""
TrustyTony commented: Neat! +12
Gribouillis 1,391 Programming Explorer Team Colleague

Replace [user] by "user":{ and [/user] by },. In the same way, replace [name] with "name":" and [/name] with ",. Do this with all the tags, then call eval()

Gribouillis 1,391 Programming Explorer Team Colleague

I wrote a linux-like chmod() code snippet once. It contains a function get_mode_by_ls() in the example part. The snippet uses the S_* constants of the stat module.

Gribouillis 1,391 Programming Explorer Team Colleague

The trick is to use the is-a relationship. For example "a cat is a mammal" implies that class Cat is a subclass of class Mammal. In practice of course, there are other possible designs.

Gribouillis 1,391 Programming Explorer Team Colleague

How would you phrase the command line if you had to launch the program from a terminal ? This is the command line to use for your call. Also rrashkin is right in recommending the subprocess module. I recommend my snippet to run the command in a comfortable way.

Gribouillis 1,391 Programming Explorer Team Colleague

impossible.I usedmsg.as_string()

The error is in the call to msg.as_string()

Gribouillis 1,391 Programming Explorer Team Colleague

str.count() example:

>>> "abracadabra".count("ab")
2
>>> 
Gribouillis 1,391 Programming Explorer Team Colleague

TemporaryFile is not a class. Use aggregation

class Var(object):
    def __init__(self):
        self._file = tempfile.TemporaryFile()
Gribouillis 1,391 Programming Explorer Team Colleague

Sorry, but it has Value B if bool(A) is True (decision Ok this far in A, B decides the value) and value A otherwise (one False-like value).

It is exactly the same thing as bool(A) is False if and only if it is not True.

Gribouillis 1,391 Programming Explorer Team Colleague

First, every value in python is either True or False in a "boolean context".
A boolean context occurs in "if", "while" or other statements

if myvalue:   # <-- boolean context.
    print("value was true")
else:
    print("value was false")

The expression

bool(myvalue)

is either True or False and this tells us whether myvalue is considered true
or false. For example

print( bool(5), bool(2), bool(None), bool(0) )

will print True, True, False, False.

Now

A or B

has value A if bool(A) is True, and value B otherwise.

A and B

has value A if bool(A) is False and value B otherwise.

Gribouillis 1,391 Programming Explorer Team Colleague

This thread may help you, especially the linux solution with the socket pair.