Gribouillis 1,391 Programming Explorer Team Colleague

Read the file by chunks and also write chunks:

with open("portrait1.dng", "rb") as binaryfile :
    with open("readfile.raw", "wb") as newFile:
        while True:
            chunk = binaryfile.read(4096)
            if not chunk:
                break
            newFile.write(binascii.hexlify(chunk))

my whole university life is on this (translation, if it dies i die).

Make sure you backup your files regularly on a separate disk.

Gribouillis 1,391 Programming Explorer Team Colleague

hmm. it only displayes a few dozen lines of the end of the file. so then i tried:

If you read by chunks, you need a while loop to read the whole file step by step. It is meaningless to remove the while loop. Also reading your file alone doesn't crash the app. If you run the code I gave you it doesn't crash the app.

Gribouillis 1,391 Programming Explorer Team Colleague

Don't use readline() with binary files, because they are not organized in lines. If this is a big file, invoke read() with a size argument to read iteratively chunks of binary data.

with open(filename, 'rb') as infile:
    while True:
        data = infile.read(1024)
        if not data:
            break # we have reached the end of the file
        # do something with the bytes read
        print(hexlify(data).decode('utf-8'))
Gribouillis 1,391 Programming Explorer Team Colleague

but I only have the first 49 displayed.

It is because of the byte = content.read(1): you are only reading one byte. Use regular python code:

with open(fileName, "rb") as infile:
    content = infile.read()
hex_bytes = binascii.hexlify(content)
hex_str = hex_bytes.decode('utf-8')
self.file_content.setText(hex_str)
Gribouillis 1,391 Programming Explorer Team Colleague

In python 3, the value returned by binascii.hexlify() is a bytes instance instead of a str instance. As setText() is expecting a str instance, you need to convert hexadecimal to str. This is done by

hexa_str = hexadecimal.decode('utf-8')
Gribouillis 1,391 Programming Explorer Team Colleague

It is key(d_val[0])]) . SyntaxError always has a very simple solution.

Gribouillis 1,391 Programming Explorer Team Colleague

You can use > and <= in the qsort() function to sort in descending order. Is there a question with this function? You could add a key argument like so

def qsort(d_val, key=lambda x: x):
    if len(d_val) <= 1: return d_val
    return qsort([lt for lt in d_val[1:] if key(lt) > key(d_val[0]])) + d_val[0:1] + \
           qsort([ge for ge in d_val[1:] if key(ge) <= key(d_val[0]]))

There are other implementations, such as inplace implementations. One could also add a reverse parameter.

Gribouillis 1,391 Programming Explorer Team Colleague

The sorted() function has a boolean reverse parameter that allows to sort in descending order

sorted(lines, key=itemgetter(3), reverse=True)

This is much more efficient than trying to write your own quicksort method.

Gribouillis 1,391 Programming Explorer Team Colleague

In your example, I don't see why you need A = property(...) when you can directly use i.a.

Gribouillis 1,391 Programming Explorer Team Colleague

My code works because newdict is no longer a dictionary vector --> value. It is a dictionary vector._key --> value instead. It means that only the ._key member is hashed when properties are being accessed, not the vector object.

If you don't use weakrefs, you don't need a Key class with a __weakref__ attribute. You can simply initialize vec._key = object(), but the keys will never leave the dictionary.

A WeakKeyDictionary doesn't prevent its keys to be garbage collected. When this happens, they seem to vanish magically from the dictionary. It allows to store additional information about objects without interfering with the objects' life cycle.

Gribouillis 1,391 Programming Explorer Team Colleague

Strange code and strange specifications as usual, but well ... Two points:

  1. Why do you want the vector to hash as a tuple ? What's the use of this ? There must be a good reason as this breaks your code.
  2. There is a serious drawback in your properties: the vector instances inserted in the dictionaries will never be garbage collected. The properties' dictionaries create memory leak.

Here is a version that addresses both issues. I aggregate a new small object named _key to each vector and this object is hashed in the property dictionary instead of the vector instance. Second, I use a WeakKeyDictionary, so that vectors can again be garbage collected.

from weakref import WeakKeyDictionary

def newProp():
    newdict = WeakKeyDictionary()
    def getter(vec):
        return newdict.get(vec._key)
    def setter(vec, val):
        newdict[vec._key] = val
    def wrapper(vec,val):
        if val is None: return
        vtype = val.__class__
        if vtype is str: setter(vec, float(val) ); return
        if numtype(vtype): setter(vec,val)
    return property(getter, wrapper)

class Key(object):
    __slots__ = ('__weakref__',)

class vector(object):
    __slots__ = ('_key',)
    X = newProp()
    Y = newProp()
    Z = newProp()
    W = newProp()
    def __init__(vec, *other): # other is for special initialization
        vec._key = Key()
        vec.X = vec.Y = vec.Z = vec.W = None
    def __hash__(vec):
        return hash( (vec.X, vec.Y, vec.Z, vec.W) )

def main():
    v = vector()
    print(hash(v))

if __name__ == '__main__':
    main()

I'm sure you won't like this, but you can still change your specifications and your algorithm.

rproffitt commented: Classy. +12
Gribouillis 1,391 Programming Explorer Team Colleague

A remark: if you create a lot of proxies such as UVProxy above, it is a good idea to make them flyweight by using the __slot__ attribute. Basically, they are nothing more than a smart pointer.

Gribouillis 1,391 Programming Explorer Team Colleague

It's impossible that facepoint.UV behaves like facepoint.UV[0], because the result of facepoint.UV[1] would be the same as facepoint.UV[0][1]: in facepoint.UV[1], the expression facepoint.UV is computed first. There is no way out of this in python.

What you could do is that facepoint.UV returns a proxy of the real UV. I'm thinking about a symbolic reference that stores nothing but the facepoint. Something along the line of

class UVdescriptor(object):
    def __get__(self, obj, cls):
        if obj is not None:
            return UVproxy(obj)

class Facepoint(object):
    UV = UVdescriptor()

def _getuvitem(facepoint, index):
    return backend_peer(facepoint).UV[index]

def _setuvitem(facepoint, index, value):
    backend_peer(facepoint).UV[index] = value

class UVproxy(object):
    def __init__(self, facepoint):
        self.facepoint = facepoint

    def __getitem__(self, index):
        return _getuvitem(self.facepoint, index)

    def __setitem__(self, index, value):
        _setuvitem(self.facepoint, index, value)

However there is a good chance that you reach only an illusory feeling of security with your proxies. You know that python code has very powerful introspection capabilities and it is a real challenge to ensure that the user of some python code can not completely expose the inside.

Gribouillis 1,391 Programming Explorer Team Colleague

Hi Tcll, it's been a while indeed!

I don't think there is a flaw in python's descriptor implementation. Also I don't understand why you think there is a security issue nor which 'private object' is 'passed into your backend'.

If Attr is a descriptor in the class, then indeed Instance.Attr is translated in Instance.__class__.Attr.__get__(Instance, Instance.__class__).

It can't be Instance.__class__.Attr because the value of Instance.__class__ is simply the class and does not remember which instance it was accessed from. It means that we need the instance again to retrieve the value of the attribute.

Perhaps you want to implement an alternative to python's descriptors, an object to put in the class that would have __getitem__ and __setitem__ methods instead of __get__ and __set__ methods. But why would this be better? can you explain in a more detailed way ?

Gribouillis 1,391 Programming Explorer Team Colleague

Is there a setup.py file in the extracted hierarchy? If so, open a terminal (cmd window), go to the directory where the setup.py file is and type python setup.py install. Before that, make sure that the python command invokes the python 3.6 interpreter.

EDIT: from what I read on the pyqt5 website, the best way to install pyqt5 is not by downloading the zip file, but by running

pip3 install pyqt5

in a cmd window, or perhaps py -m pip install pyqt5 (because the pip interface changed slightly in python 3.4 and I don't use windows)

Don't you have a C:\Python36 folder ? You can get the path to your python executable by running

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

You can get the paths that python use to import modules by running

python -c "import sys; print(sys.path)"
Gribouillis 1,391 Programming Explorer Team Colleague

It is a typo at line 64, it should be display4_txt. Try to understand the code.

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

Well exactly where you put this function in the above code.

Gribouillis 1,391 Programming Explorer Team Colleague

You can write the reset function like this

    def reset(self):
        global number, tries
        number = random.randrange (100) + 1
        result_msg = "Game reset. Start guessing!!!"
        tries = 0
        self.update_display(guess='', result=result_msg, tries='')

where the update_display() method fills the Text widgets

    def update_display(self, **kwargs):
        for key, display in self.display_map.items():
            if key in kwargs:
                display.delete(0.0, END)
                display.insert(0.0, kwargs[key])

For this to work, you need to build a dictionary at the end of the create_widgets() method

        self.display_map = {
            'welcome': self.display1_txt,
            'guess': self.display2_txt,
            'result': self.display3_txt,
            'tries': self.display4_txt,
        }

You can then replace lines 87-95 in your code with

        # Display
        self.update_display(
            welcome=welcome_msg, guess=guess_msg,
            result=result_msg, tries=tries_msg)

Many things can be improved, for example you could have a list of text widgets instead of display1_txt ....

Gribouillis 1,391 Programming Explorer Team Colleague

I don't know why the #2 direction is here. I never had a failure using the boot-repair-disk from a CD/DVD. So you can try it if you prefer this. But why not use the USB stick method?

Gribouillis 1,391 Programming Explorer Team Colleague

Yes this one. It usually works very well to detect installed OSes and repair the boot.

Gribouillis 1,391 Programming Explorer Team Colleague

Did you try the boot repair disk ?

Gribouillis 1,391 Programming Explorer Team Colleague

@rproffitt Luckybackup invokes rsync, and it can display the rsync command line that it uses. It means that one can use luckybackup as a GUI tool to figure out the relevant command line options for rsync. It means that the question is not luckybackup vs some other tool, but rather rsync vs some other tool.

Gribouillis 1,391 Programming Explorer Team Colleague

Did you try using a case sensitive filesystem such as ntfs ?

Gribouillis 1,391 Programming Explorer Team Colleague

I think you get this error because of with open(f, 'r') as var1. It should be
with open(os.path.join(destdir, f), 'r') as var1.

A more up-to-date approach is to use module pathlib

from pathlib import Path
destdir = Path('pathtofile')
files = [p for p in destdir.iterdir() if p.is_file()]
for p in files:
    with p.open() as f:
        ...
Gribouillis 1,391 Programming Explorer Team Colleague

I think you need a file complex.h containing the line

void read_comp(complex, int, int);
Gribouillis 1,391 Programming Explorer Team Colleague

It is because after age = raw_input('...'), age is a character string. It needs to be converted to an integer like this

age = raw_input ('enter you age-->')
age = int(age)
Gribouillis 1,391 Programming Explorer Team Colleague

I'd recommend using python setup.py develop first, which will install a symbolic link allowing python to import and use myapp without really installing the package. If you're using a virtualenv, make sure the command uses your virtualenv's python interpreter.

Gribouillis 1,391 Programming Explorer Team Colleague

That's where we completely disagree. I think it is one of the best design choices in the python language.

Gribouillis 1,391 Programming Explorer Team Colleague

Well, range(10) contains the numbers 0 1 2 3 4 5 6 7 8 9 while range(10, 110, 10) contains 10 20 30 40 50 60 70 80 90 100 which is whats the program purports to convert.

Gribouillis 1,391 Programming Explorer Team Colleague

You should be using the python interpreter and the pip command from your venv bin/ directory. What's in your venv's bin directory ?

Gribouillis 1,391 Programming Explorer Team Colleague

This is strange, there should be a python interpreter in the virtualenv's bin/ directory. Didn't you create the virtualenv with a python interpreter such as in

virtualenv --python=python2.7 mydir

?

Gribouillis 1,391 Programming Explorer Team Colleague

Try which pip and which python. Both should be in your virtual env's bin directory. django-admin.py should be in the same directory. You can also use locate django-admin.py to find all the files with that name in your file system.

Gribouillis 1,391 Programming Explorer Team Colleague

On my computer, when I write

f = 3.56

in a python program, the following bits are stored in memory

0 10000000000 1100011110101110000101000111101011100001010001111011

This is the IEEE 754 representation of the floating point number 3.56 according to my machine's precision. The corresponding real value is not exactly 3.56, it is

Fraction(8016407336719483, 2251799813685248)

This is equal to

Fraction(356, 100) + Fraction(3, 56294995342131200)

The error is approximately 5.3290705182e-17

Also have a look at https://docs.python.org/3/tutorial/floatingpoint.html

rproffitt commented: That's accurate. +11
cereal commented: well written! +14
Gribouillis 1,391 Programming Explorer Team Colleague

if vertice in self.vertice: should be if vertice in self.vertices:.
It is not a very good idea to use both vertice and vertices. You could write if ver in self.vertices: for example and use ver instead of vertice.

Gribouillis 1,391 Programming Explorer Team Colleague

it gives an error saying that the term 'vertice' in the function editVertice is not recognized

Can you post (as code) the complete error message sent by python instead of your own interpretation of this message ?

Also it is much better to raise an exception than to print an error message in your function. Use

raise RuntimeError("VERTICE NOT RECOGNIZED.  INSERT VALID VERTICE")
Gribouillis 1,391 Programming Explorer Team Colleague

In kubuntu 14.04 you can do it with the System Configuration app. See the attached snapshots (sorry it's in french). I can add as many keyboard layouts as needed. I don't know if this will work in more recent versions of kubuntu

Gribouillis 1,391 Programming Explorer Team Colleague

If you want to translate more than one sentence, you can use a loop

if __name__ == "__main__":
    while True:
        x = main()
        print(x)
Gribouillis 1,391 Programming Explorer Team Colleague

You can get this with

coordinates = [(i, j) for j in range(11) for i in range(11)]

although many programmers would use numpy for such tasks.

Gribouillis 1,391 Programming Explorer Team Colleague

You're running out of memory because calculatingcoords never becomes False, and the loop runs forever, appending more and more items to coordinates.

It would be easier if you give us an example of your expected result in the list coordinates.

Gribouillis 1,391 Programming Explorer Team Colleague

Are you looking for a xor cipher https://en.wikipedia.org/wiki/XOR_cipher ?

Gribouillis 1,391 Programming Explorer Team Colleague

According to the documentation, the module requires a python version >= 3.4. I'm afraid it won't run on your python 2.7. I hope somebody else can help you because I never used this module.

Gribouillis 1,391 Programming Explorer Team Colleague

You could perhaps start with an example similar to the "getting started" part of the websocket documentation

Gribouillis 1,391 Programming Explorer Team Colleague

@JamesCherrill In your example my 'key' algorithm groups the 3s and the 5s in a one-liner because the keys x//3 for the values (1, 3, 3, 5 ,5, 5, 5) are (0, 1, 1, 1, 1, 1, 1). In python it gives

>>> import itertools as itt
>>> from functools import partial
>>> data = [1, 3, 3, 5, 5, 5, 5]
>>>
>>> def the_key(tol, val):
...     return val // tol
...
>>> print([the_key(3, x) for x in data] )
[0, 1, 1, 1, 1, 1, 1]
>>> print([list(g) for k, g in itt.groupby(data, partial(the_key, 3))])
[[1], [3, 3, 5, 5, 5, 5]]

The awesome part is that this works also with 2D data instead of 1D

Gribouillis 1,391 Programming Explorer Team Colleague

Or perhaps you could try

n = len(set((x//tol, y//tol) for (x, y) in data))

as a rough estimate of the number of clusters.

Gribouillis 1,391 Programming Explorer Team Colleague

I don't know this cluster module. The number of clusters can probably be determined by

  • The shape of the clusters
  • Your tolerance
  • The minimum and maximum values of the coordinates

As I don't know the subcluster's shapes, I cannot give you a formula. You could perhaps draw the subclusters in different colors to see their shape and try to vary their numbers.

Gribouillis 1,391 Programming Explorer Team Colleague

There is a good example at the bottom of this page http://www.python-course.eu/tkinter_checkboxes.php . Take the time to read about the use of tkinter Variable classes.

slate commented: Good answer. Op is not reading the answers anyway. +8
Gribouillis 1,391 Programming Explorer Team Colleague

You could try to compute a key for each pair (x, y), the key could be (x//3, y//3) for a tolerance of 3. Then you sort the data by the key and you group together the points having the same key, using itertools.groupby(). You can then average the values in each group.

Gribouillis 1,391 Programming Explorer Team Colleague

You can exit the loop with a break statement or by setting start to another value, for
example start = "Don't start".

Gribouillis 1,391 Programming Explorer Team Colleague

You could replace line 7 with

while start == 'Start':

and remove the for loop in start, which is not good: loop takes the values 'S', 't', 'a', 'r', 't', then the loop exits. This is probably not what you want.