Gribouillis 1,391 Programming Explorer Team Colleague

This file is not the file hashlib.py from the python's standard library. You could rename it (for example myhashlib.py). Make sure that there are no hashlib.pyc or hashlib.pyw in the Desktop folder

Gribouillis 1,391 Programming Explorer Team Colleague

You may be shadowing the standard lib's hashlib module with another module with the same name. Try

import hashlib
print(hashlib)
Gribouillis 1,391 Programming Explorer Team Colleague

It is very difficult to run an interactive session with the subprocess module. Some of your statements look suspicious, such as prog.stdout.read() instead of prog.stdout.readline() to read a single line, prog.stdout.seek(0) (prog.stdout is not a regular file). If you write lines to prog.stdin, the lines should end with a newline character, also it would probably be a good idea to call prog.stdin.flush() in order to ensure that the line is not buffered somewhere instead of being sent to the program.

Anyway, IMHO, it would be much better to find a way to call the commands in a non interactive way. You could use the /c option in CMD.exe, or perhaps call netsh command without wrapping it in a CMD command. You can perhaps write a batch file that does a part of the job and you could call this batch file with the subprocess module.

Gribouillis 1,391 Programming Explorer Team Colleague

Look at the example Click Here. Obviously the back_populates value must be the name of one of the other classes attributes.

Gribouillis 1,391 Programming Explorer Team Colleague

If you're in windows, try the version from Christoph Gohlke's site Click Here !

Gribouillis 1,391 Programming Explorer Team Colleague

You could also start by reading this Howto from the python documentation Click Here

Gribouillis 1,391 Programming Explorer Team Colleague

I don't think studying my code snippets is good way to learn python. Most of them deal with specialized tips and tricks. More seriously, you could visit the hitchiker's guide to python (among thousands of other sites) to start with.

Using a debugger is not so crucial in python as it is in C++ for example, because there are very few mysterious bugs. Most errors are very easily detected by reading the traceback or printing a few variables.

I don't like python IDEs in general. I work more efficiently with a very good text editor (Kate in linux, but there is a windows version too) which has some tools to work with
python and with projects too. But it is not an IDE.

A very nice way to work, but again the tool I'm using is linux-specific is to have a program that watches changes in my files while I'm editing, and automatically runs the code every time I save it in the editor. It gives an immediate feedback about what I'm doing. The program runs in a terminal next to the editor window and it replaces an IDE. I know there are windows tools to do such things but I don't know them.

AssertNull commented: Good approach. I shall follow it. +5
Gribouillis 1,391 Programming Explorer Team Colleague

A crash course for unicode from, to bytes (works in 2 and 3, only the 'str' class in 3 is named 'unicode' in 2, and the 'byte' class in 3 is named 'str' in 2)

ustuff = bstuff.decode('utf8')
bstuff = ustuff.encode('utf8')

Of course, you may encounter other encodings than utf8.

The main debugging tool in python is to read attentively the exception traceback that python sends to your console when there is an error. Also don't unnecessarily catch exceptions: they carry useful debugging information.

I often use a modified print function, which I posted here Click Here

Last advice: Always declare the source file encoding by writing for example

# -*-coding: utf8-*-

as the first or second line of a program, and if you write new python 2 code yourself, add this as the first statement of the program

from __future__ import (absolute_import, division,
                        print_function, unicode_literals)

This minimizes the difference in syntax between 2 and 3. With this, python 2 code needs print with parentheses for example and "hello world" is a unicode string. The statement is a no-op in python 3
Edit: typo

Gribouillis 1,391 Programming Explorer Team Colleague

Among the various reasons why some people stick with python 2 is the fact that many linux distributions still come with python 2 as their default python interpreter, because many of their system scripts are written in python 2. For example, I'm using kubuntu 14.04, so April 2014, and my default python is 2.7.6.

As a newbie, you would waste a lot of time learning python 2 instead of python 3, so don't do it. On the other hand, python 3 is not very different from python 2. Apart from the print with parenthesis which you mentioned (print was a special statement in python 2 and became a regular function in python 3), the main change is the proper and transparent handling of unicode strings. In python 2, a string such as 'hello world' is a byte string, while in python 3 it is a unicode string. So if you work with python 2, there is a risk that you waste time struggling with conversions between bytes and unicode, and encoding issues. These issues are not very difficult to solve - for a python expert - . The other significant change is the renaming of some library modules. For example Tkinter becomes tkinter.

You could try the 2to3 tool which comes with your python installation, which is an automated script purporting to translate python 2 scripts into python 3 scripts. Here is a video Click Here of a guy using this tool to translate a program.

rproffitt commented: 2to3 times a better answer as well! +10
Gribouillis 1,391 Programming Explorer Team Colleague

Can you post the whole error message ?

Edit: this is unrelated to the issue, but if you want to run the program several times, it would be good to add

s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

immediately after calling socket.socket().

Gribouillis 1,391 Programming Explorer Team Colleague

You could try the answer here Click Here using "\C-i" instead of "\C-w". Of course, you need to restart bash after modifying ~/.bashrc.

Gribouillis 1,391 Programming Explorer Team Colleague

No, the formula is this one Click Here. It is not an equality, it is an equivalent when n is large.

quatrosem commented: that is it! thanks... question answered! +0
Gribouillis 1,391 Programming Explorer Team Colleague

Install pip first, by following the instructions given <here>.

Gribouillis 1,391 Programming Explorer Team Colleague

You must choose the intermediary squares between the blue dot and a black square. It means that you are counting the number of one-to-one sequences of the n = 398 remaining squares. There are
n! \sum_{k=0}^{n} \frac{1}{k!}
such sequences. See this reference for example Click Here.

When n is large, this is approximately e \times n!

Gribouillis 1,391 Programming Explorer Team Colleague

I don't understand the rules. Also there are 2 blue circles in the picture. How are the legal paths defined ?

Gribouillis 1,391 Programming Explorer Team Colleague

In windows OS, the best solution is often to install a module from Christoph Gohlke's python binaries for windows. PyHook is <Here>.

Edit: Download the wheel file (.whl), then type pip install <filename.whl>

Gribouillis 1,391 Programming Explorer Team Colleague

I've found a play_again() function <here>. It can be adapted to your game by returning a boolean. Also the procedure to initialize the maze and the player must be called again, so I write a reinit() function to do that. Here is the result. It could be a good starting point.

import os
import pygame
import random

pygame.init()

white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,155,0)
brown = (205,133,63)
blue = (135,206,250)

FPS = 60
#the dimension of the game
display_width = 680
display_height = 440

# Class for the player
class Player(object):

    def __init__(self):
        self.rect = pygame.Rect(40, 40, 30, 30)

    def move(self, dx, dy):

        # Move each axis separately. Note that this checks for collisions both times.
        if dx != 0:
            self.move_single_axis(dx, 0)
        if dy != 0:
            self.move_single_axis(0, dy)

    def move_single_axis(self, dx, dy):

        # Move the rect
        self.rect.x += dx
        self.rect.y += dy

        # If you collide with a wall, move out based on velocity
        for wall in walls:
            if self.rect.colliderect(wall.rect):
                if dx > 0: # Moving right, Hit the left side of the wall
                    self.rect.right = wall.rect.left
                if dx < 0: # Moving left, Hit the right side of the wall
                    self.rect.left = wall.rect.right
                if dy > 0: # Moving down, Hit the top side of the wall
                    self.rect.bottom = wall.rect.top
                if dy < 0: # Moving up, Hit the bottom side of the wall
                    self.rect.top = wall.rect.bottom

# Class to hold a wall rect
class Wall(object):

    def __init__(self, pos):
        walls.append(self)
        self.rect = …
Gribouillis 1,391 Programming Explorer Team Colleague

I simply used the <igraph> library with a few lines of python. This library can also be called from C or R.

Gribouillis 1,391 Programming Explorer Team Colleague

Also it seems to me that using

import psutil
d = psutil.net_io_counters(pernic=True)

is a more robust way to obtain the numerical data (most probably cross-platform).

Gribouillis 1,391 Programming Explorer Team Colleague

If you only want to add an interface argument to your script, use the argparse module like this

import argparse
import time
import os
import sys

def get_bytes(t, iface):
    with open('/sys/class/net/' + iface + '/statistics/' + t + '_bytes', 'r') as f:
        data = f.read();
    return int(data)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="estimates internet interface speed")
    parser.add_argument('iface', metavar='INTERFACE', help='network interface such as eth0 or wlan0')
    args = parser.parse_args()
    (tx_prev, rx_prev) = (0, 0)
    while(True):
        try:
            tx = get_bytes('tx', args.iface)
            rx = get_bytes('rx', args.iface)
        except IOError:
            print("Cannot read the number of bytes received or transmitted through interface {}".format(args.iface))
            break
        if tx_prev > 0:
            tx_speed = tx - tx_prev
            print('TX: ', tx_speed, 'bps')
        if rx_prev > 0:
            rx_speed = rx - rx_prev
            print('RX: ', rx_speed, 'bps')
        time.sleep(1)
        tx_prev = tx
        rx_prev = rx
Gribouillis 1,391 Programming Explorer Team Colleague

I don't understand your question because kbps is kilobits per second, a speed measurement, but ifconfig doesn't give any speed indication.

Gribouillis 1,391 Programming Explorer Team Colleague

RX bytes and TX bytes are not speed measurements, they show the total number of bytes received and transmitted by this interface (eth0).

Are you a friend of zero_1 who posted the same question 1 week ago Click Here ?

Gribouillis 1,391 Programming Explorer Team Colleague

Here is the graph with all states allowed. Note that an undirected graph can be drawn, because every move is reversible.

Gribouillis 1,391 Programming Explorer Team Colleague

I drew the following graph, which shows that there are only two routes of equal length. The starting point is the green dot fdwc (farmer, duck, wolf, corn) and the final point is the red dot, labelled 0. The arrows are possible transitions. I removed the yellow states from the graph because they are not acceptable states. For example fw (farmer wolf) is not acceptable because the other bank of the river would contain duck and corn and no farmer. Some of those yellow states could be reached in principle, but the corresponding edges were removed.

Gribouillis 1,391 Programming Explorer Team Colleague

I posted a solution in python for this problem Click Here. The algorithm could perhaps give you some ideas to compare with your code.

Gribouillis 1,391 Programming Explorer Team Colleague

@AssertNull running

astyle --style=allman --align-pointer=middle --max-code-length=79 farmer.cpp

does a pretty good job on formatting Builder_1's code

Gribouillis 1,391 Programming Explorer Team Colleague

Why do you need python 2.5. The obvious thing to do is to uninstall 2.5 and install 2.7.

Gribouillis 1,391 Programming Explorer Team Colleague

I think there shouldn't be a for loop in Read(). Also there is an error in get_smallest()

#include <cassert>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;

struct people
{
    string name;
    int age;
};

people Read(people p)
{
    //for (unsigned int i = 0; i < 5; i++){
        cout << "name: ";
        cin >> p.name;
        cout << "age: ";
        cin >> p.age;
    //}
    return p;
}
vector<people> getData(people person)
{
    vector<people> v;
    for (unsigned int i = 0; i < 5; i++){
        person = Read(person);
        v.push_back(person);
    }
    return v;
}

int getSmallest(vector<people> v)
{
    assert(v.size());
    int smallest = v.at(0).age;
    for (unsigned int i = 0; i < v.size(); i++)
    {

            if (v.at(i).age < smallest)
                smallest = v.at(i).age;


    }
    return smallest;

}
void print(vector<people> v){
    cout << endl;
    for (unsigned int i = 0; i < v.size(); i++){
        cout << "name: " << v.at(i).name << endl;
        cout << "age: " << v.at(i).age << endl;
    }
}

int main(int argc, char* argv[])
{
    people p;
    vector<people> v;
    cout << "enter names & ages" << endl;
    v = getData(p);
    print(v);
    cout << "the smallest one" << endl;
    cout << getSmallest(v) << endl;

    return 0;
}
Gribouillis 1,391 Programming Explorer Team Colleague

This one works

#include <stdio.h>
#include <string.h>

int main(int argc, char **argv) {
    char ar[200] = "Hello World";
    strcat(ar, " !");
    printf("%s\n", ar);

    char* q = ar;
    char **p = &q;
    printf("%s\n", *p);

    return 0;
}

note that I'm only an occasional C programmer.

Gribouillis 1,391 Programming Explorer Team Colleague

I would try ptr = &myArray;

Gribouillis 1,391 Programming Explorer Team Colleague

One problem with OO tutorials is that they teach you a very formal point of vue about OOP, how to carefuly plan subclasses, method overriding etc. After many years of OOP, I tend not to write many subclasses and not to override many methods because carefuly designed class hierarchies are too difficult to manage. On the other hand, I write many classes. When I want to write a function, I often write a class instead. I often write classes without planned design. For example if I have a problem, I start the code with a Solution class which I use as a container for several algorithms!

Gribouillis 1,391 Programming Explorer Team Colleague

Please post the whole code.

If you write

 def __init__(self, year, month, day):

then you need 3 arguments (python says it's 4)

date = Date(2016, 5, 3)

If you write instead

def __init__(self, year=2000, month=1, day=1):

Then, the arguments are optional. You can use

date = Date(year=2016)
Gribouillis 1,391 Programming Explorer Team Colleague

Did you try --html-file="myfolder/${f}.html" ?

Gribouillis 1,391 Programming Explorer Team Colleague

yes it looks correct. Does it work the way you want ?

Gribouillis 1,391 Programming Explorer Team Colleague

The first definition is correct. The __add__() method must return a value, you must add

return Date(year, month, day)

in this method.

Gribouillis 1,391 Programming Explorer Team Colleague

You need to pass arguments for Date(), such as

date = Date(2016, 4, 30)
Gribouillis 1,391 Programming Explorer Team Colleague

Look at the whole error message, it tells you that the error is at line 17, because there are 2 paremeters named yearin your __init__() method.

Gribouillis 1,391 Programming Explorer Team Colleague

For Delta, you need optional parameters. For this you can define

def __init__(self, year=1970, month=1, day=1)

for example.

Gribouillis 1,391 Programming Explorer Team Colleague

It is much better for class Date, but you must create an instance with parameters, for example

date = Date(2016, 4, 29)

There should be a __init__()method for class Delta too, with convenient parameters.

Gribouillis 1,391 Programming Explorer Team Colleague

Remove all the classes content and start with a single __init__()method for each of the two classes.

Gribouillis 1,391 Programming Explorer Team Colleague

Post your code. You don't need __call__() methods, you only need one __init__() method per class, and also __add__() and perhaps __radd__() methods.

Gribouillis 1,391 Programming Explorer Team Colleague

I don't think you need x2 and y2 in the call to delete(), canvas.delete(captured) should work. Also shouldn't the last call be written

canvas.coords(closest, (x_center, y_center))

?

What do you mean by a better way to delete the captured piece ?

Gribouillis 1,391 Programming Explorer Team Colleague

If you want to access the correct variable, you need to return its value in movecreate(). For examble, this function could return two values

return correct, movelist

The function that uses this could use

correct, movelist = movecreate(tour, b, d)
if movelist and (correct == 0): ...

edit: also it would be a great improvement if you could remove most of the global statements.

Gribouillis 1,391 Programming Explorer Team Colleague

Your code is very difficult to understand because it is not modular enough. You say you need to add something after if len(movecreate(tour,b,d))>0. Start by writing pseudo code to describe what you need to add. Describe it in plain english or french to start with.

Gribouillis 1,391 Programming Explorer Team Colleague

You must write

from game_engine import movecreate

but first you must move the definition of movecreate() outside the body of the engine() function. You don't need nested function definitions in your code, so don't use them as they hide the functions from the global namespace.

Gribouillis 1,391 Programming Explorer Team Colleague

No, I don't know, I thought that you were using a plugin such as Click Here or Click Here which have this option. You could perhaps try them.

Gribouillis 1,391 Programming Explorer Team Colleague

@OP don't you have an option --html-file ?

Gribouillis 1,391 Programming Explorer Team Colleague

@AndrisP yes I know. What you describe is called the shebang. You can also use

#!/bin/env python

which uses the environment to find the python path.

I was thinking about another mechanism called binfmt_misc in the linux kernel: if you type

sudo update-binfmts --install Python /usr/bin/python --extension py

Then files with extension .py will automatically be executed with /usr/bin/python (without the need of a shebang line). The problem is that nowadays, both python and python3 source files end with .py, so that binfmts_misc is more annoying than useful in this case.

Gribouillis 1,391 Programming Explorer Team Colleague

You can get the path to the python command by pasting this in a terminal:

python -c "import sys; print(sys.executable)"

edit: @AndrisP what you say will only work if the python scripts start with the correct shebang line containing the call to the python executable, unless the system has been configured to recognize .py files as python scripts (which brings new issues, such as what to do if one has multiple versions of python).

Gribouillis 1,391 Programming Explorer Team Colleague

Try k = i + 8 * j. The reverse conversion is j, i = divmod(k, 8).