Re: Problems using struct pack/unpack in files, and reading them.

2015-11-17 Thread Dave Farrance
Steven D'Aprano wrote: >On Mon, 16 Nov 2015 05:15 pm, Gregory Ewing wrote: > >> Ints are not the only thing that // can be applied to: >> >> >>> 1.0//0.01 >> 99.0 > >Good catch! Hmmm. I see that the float for 0.01 _is_ slightly larger than 0.01 >>> Decimal(0.01) Decimal('0.012

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-16 Thread Chris Angelico
On Tue, Nov 17, 2015 at 12:17 AM, Steven D'Aprano wrote: > On Sun, 15 Nov 2015 01:23 pm, Chris Angelico wrote: > >> On Sun, Nov 15, 2015 at 1:08 PM, Steven D'Aprano >> wrote: >>> number = +raw_input("enter a number: ") >>> >>> versus: >>> >>> text = raw_input("enter a number: ") >>> try: >>>

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-16 Thread Steven D'Aprano
On Sun, 15 Nov 2015 01:23 pm, Chris Angelico wrote: > On Sun, Nov 15, 2015 at 1:08 PM, Steven D'Aprano > wrote: >> number = +raw_input("enter a number: ") >> >> versus: >> >> text = raw_input("enter a number: ") >> try: >> number = float(text) >> except ValueError: >> number = int(text) >

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-16 Thread Steven D'Aprano
On Mon, 16 Nov 2015 05:15 pm, Gregory Ewing wrote: > Ian Kelly wrote: >> Unary integer division seems pretty silly since the only possible results >> would be 0, 1 or -1. > > Ints are not the only thing that // can be applied to: > > >>> 1.0//0.01 > 99.0 Good catch! -- Steven -- https:/

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-15 Thread Gregory Ewing
Chris Angelico wrote: Small problem: Since we have / and // operators, it's impossible to have a unary / operator: No, it's not -- '//' is already recognised as a single token distinct from '/ /'. You would just have to leave a space or use parens in some cases. -- Greg -- https://mail.python.

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-15 Thread Gregory Ewing
Ian Kelly wrote: Unary integer division seems pretty silly since the only possible results would be 0, 1 or -1. Ints are not the only thing that // can be applied to: >>> 1.0//0.01 99.0 -- Greg -- https://mail.python.org/mailman/listinfo/python-list

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-15 Thread Gregory Ewing
Dennis Lee Bieber wrote: And we'd be left looking for a symbol for bitwise inversion. Who needs negation when you have bitwise inversion? minusx = ~x + 1 I know, it doesn't work for floats, but that's just a simple matter of defining ~ on floats appropriately... -- Greg -- https://ma

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-14 Thread Chris Angelico
On Sun, Nov 15, 2015 at 1:08 PM, Steven D'Aprano wrote: > number = +raw_input("enter a number: ") > > versus: > > text = raw_input("enter a number: ") > try: > number = float(text) > except ValueError: > number = int(text) What kinds of strings can float() not handle but int() can, and in

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-14 Thread Steven D'Aprano
On Sun, 15 Nov 2015 02:43 am, Ian Kelly wrote: > On Fri, Nov 13, 2015 at 10:40 PM, Steven D'Aprano > wrote: >> Python has operator overloading, so it can be anything you want it to be. >> E.g. you might have a DSL where +feature turns something on and -feature >> turns it off. > > By that argume

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-14 Thread Steven D'Aprano
On Sun, 15 Nov 2015 09:53 am, Random832 wrote: > Marko Rauhamaa writes: >> Actually, the real question is, is the unary - *really* so useful that >> it merits existence or is it just something that was mindlessly copied >> into programming languages from elementary school arithmetics? > > The al

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-14 Thread MRAB
On 2015-11-14 22:53, Random832 wrote: Marko Rauhamaa writes: Actually, the real question is, is the unary - *really* so useful that it merits existence or is it just something that was mindlessly copied into programming languages from elementary school arithmetics? The alternative, if you wan

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-14 Thread Marko Rauhamaa
Random832 : > Marko Rauhamaa writes: >> Actually, the real question is, is the unary - *really* so useful >> that it merits existence or is it just something that was mindlessly >> copied into programming languages from elementary school arithmetics? > > The alternative, if you want to be able to

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-14 Thread Random832
Marko Rauhamaa writes: > Actually, the real question is, is the unary - *really* so useful that > it merits existence or is it just something that was mindlessly copied > into programming languages from elementary school arithmetics? The alternative, if you want to be able to specify negative num

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-14 Thread Marko Rauhamaa
Ian Kelly : > On Nov 14, 2015 9:56 AM, "Marko Rauhamaa" wrote: >>r = //(//r1 + //r2 + //r3) > > Unary integer division seems pretty silly since the only possible > results would be 0, 1 or -1. Yep, mixed them up. Marko -- https://mail.python.org/mailman/listinfo/python-list

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-14 Thread Ian Kelly
On Nov 14, 2015 10:10 AM, "Chris Angelico" wrote: > > On Sun, Nov 15, 2015 at 4:04 AM, Ian Kelly wrote: > > Unary integer division seems pretty silly since the only possible results > > would be 0, 1 or -1. > > 1, -1, or ZeroDivisionError. The zero's on the other side. But yes. // 42 == 1 // 42

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-14 Thread Ian Kelly
On Nov 14, 2015 9:56 AM, "Marko Rauhamaa" wrote: > > Ian Kelly : > > > For somebody reading one of these uses of unary plus in real code, I > > imagine it would be a bit of a WTF moment if it's the first time > > they've encountered it. I don't recall ever seeing any code that > > actually used th

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-14 Thread Chris Angelico
On Sun, Nov 15, 2015 at 3:52 AM, Marko Rauhamaa wrote: > What I don't understand is why there is a unary + but no unary /: > >-x ≡ 0 - x >+x ≡ 0 + x >/x ≡ 1 / x >//x ≡ 1 // x >*x ≡ 1 * x > > You could write: > >r = //(//r1 + //r2 + //r3) > > for > >r = 1 // (1//r1 + 1//

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-14 Thread Chris Angelico
On Sun, Nov 15, 2015 at 2:43 AM, Ian Kelly wrote: > On Fri, Nov 13, 2015 at 10:40 PM, Steven D'Aprano wrote: >> Python has operator overloading, so it can be anything you want it to be. >> E.g. you might have a DSL where +feature turns something on and -feature >> turns it off. > > By that argume

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-14 Thread Marko Rauhamaa
Ian Kelly : > For somebody reading one of these uses of unary plus in real code, I > imagine it would be a bit of a WTF moment if it's the first time > they've encountered it. I don't recall ever seeing any code that > actually used this, though. What I don't understand is why there is a unary +

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-14 Thread Ian Kelly
On Fri, Nov 13, 2015 at 10:40 PM, Steven D'Aprano wrote: > Python has operator overloading, so it can be anything you want it to be. > E.g. you might have a DSL where +feature turns something on and -feature > turns it off. By that argument we should also have operators ~, !, $, \, ? because some

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Steven D'Aprano
On Sat, 14 Nov 2015 02:01 pm, Chris Angelico wrote: > On Sat, Nov 14, 2015 at 1:40 PM, Steven D'Aprano > wrote: >> On Sat, 14 Nov 2015 09:42 am, Chris Angelico wrote: >> >>> However, this is a reasonable call for the abolition of unary plus... >> >> The only way you'll take unary plus out of Pyth

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Chris Angelico
On Sat, Nov 14, 2015 at 2:48 PM, Ian Kelly wrote: >> Yes, unary minus has the same issue - but it's a lot more important >> than unary plus is. In ECMAScript, unary plus means "force this to be >> a number"; what's its purpose in Python? > > I'm not sure "force this to be a number" is really a jus

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Chris Angelico
On Sat, Nov 14, 2015 at 2:45 PM, Ian Kelly wrote: >> Yes, unary minus has the same issue - but it's a lot more important >> than unary plus is. In ECMAScript, unary plus means "force this to be >> a number"; what's its purpose in Python? > > It forces a Counter to contain only positive counts? Di

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Ian Kelly
On Nov 13, 2015 8:03 PM, "Chris Angelico" wrote: > > On Sat, Nov 14, 2015 at 1:40 PM, Steven D'Aprano wrote: > > On Sat, 14 Nov 2015 09:42 am, Chris Angelico wrote: > > > >> However, this is a reasonable call for the abolition of unary plus... > > > > The only way you'll take unary plus out of Py

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Ian Kelly
On Nov 13, 2015 8:03 PM, "Chris Angelico" wrote: > > On Sat, Nov 14, 2015 at 1:40 PM, Steven D'Aprano wrote: > > On Sat, 14 Nov 2015 09:42 am, Chris Angelico wrote: > > > >> However, this is a reasonable call for the abolition of unary plus... > > > > The only way you'll take unary plus out of Py

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Chris Angelico
On Sat, Nov 14, 2015 at 1:40 PM, Steven D'Aprano wrote: > On Sat, 14 Nov 2015 09:42 am, Chris Angelico wrote: > >> However, this is a reasonable call for the abolition of unary plus... > > The only way you'll take unary plus out of Python is by prying it from my > cold, dead hands. > > > BTW, unar

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Steven D'Aprano
On Sat, 14 Nov 2015 09:42 am, Chris Angelico wrote: > However, this is a reasonable call for the abolition of unary plus... The only way you'll take unary plus out of Python is by prying it from my cold, dead hands. BTW, unary minus suffers from the same "problem": x =- y # oops, meant x -= y

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Chris Angelico
On Sat, Nov 14, 2015 at 6:45 AM, Ian Kelly wrote: > This sets RegisterAX to 2. Specifically, positive 2. If you want to > *add* 2, you probably meant to write: > > RegisterAX += 2 > > This also points out a good reason for using spaces around operators, > as "RegisterAX =+ 2" would have caused

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Grant Edwards
On 2015-11-13, kent nyberg wrote: > Though, as many times before, the problem was due to misunderstanding > of how python works. I assumed file.read()[xx:yy] was to be > understood as, in the file, read from index xx to place yy. Nope. First, the 'file.read()' part is evaluated. That return

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread kent nyberg
The main problem was that I forgot to do seek(0). Thanks alot people. Though, as many times before, the problem was due to misunderstanding of how python works. I assumed file.read()[xx:yy] was to be understood as, in the file, read from index xx to place yy. That is, [10:20] was the same a

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Grant Edwards
On 2015-11-13, Ian Kelly wrote: > Either retain the read data between calls, or call seek(0) before > reading it again. It has always saddened me that Python files don't have a rewind() method. On Unix, calling rewind() is the same as calling seek(0), so it's utterly pointless except as an amus

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Ian Kelly
On Fri, Nov 13, 2015 at 1:15 PM, kent nyberg wrote: > What bothers me, is the error that says > unpack requires a string argument of 4 bytes. > Im thinking in the line of arguments? Does unpack look at the 4 bytes it has > read, and tell for some > reason say that unpacking needs an argument of 4

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Ian Kelly
On Fri, Nov 13, 2015 at 1:15 PM, kent nyberg wrote: > Even with that, it still gets wrong. > I also tried .read()[RegisterAX:RegisterAX+4] When you call read for the second time, are you just reading the same file again without closing or seeking it in the interim? If that's the case, then you wo

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Laura Creighton
I forgot to add. You get this wretched error message if your data is shorter than expected, and you ask struct to read more than you have, as well. most annoying. Laura -- https://mail.python.org/mailman/listinfo/python-list

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread kent nyberg
On Fri, Nov 13, 2015 at 12:36:22PM -0700, Ian Kelly wrote: > On Fri, Nov 13, 2015 at 12:20 PM, kent nyberg wrote: > > def LoadCommandAndReact(place_to_read): > > global RegisterAX > > > > tmp = place_to_read.read()[RegisterAX:calcsize('HH')] > > It looks like you're trying to get a slice

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Laura Creighton
In a message of Fri, 13 Nov 2015 14:20:45 -0500, kent nyberg writes: >Hi there, >Im deeply sorry for yet another question to this list. I have come across a >problem to which google seems not >to eager to supply the anwser. > >The problem is the following. >First I do this: > >def setup_drive():

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Ian Kelly
As long as I'm replying to this, I see a few more issues to comment on: On Fri, Nov 13, 2015 at 12:20 PM, kent nyberg wrote: > if place_to_read.closed: >print("Drive error. Drive closed.") You probably also want to break or return here. Even better: raise an exception instead of prin

Re: Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread Ian Kelly
On Fri, Nov 13, 2015 at 12:20 PM, kent nyberg wrote: > def LoadCommandAndReact(place_to_read): > global RegisterAX > > tmp = place_to_read.read()[RegisterAX:calcsize('HH')] It looks like you're trying to get a slice of length 4 here, starting at the value of RegisterAX. What you're actual

Problems using struct pack/unpack in files, and reading them.

2015-11-13 Thread kent nyberg
Hi there, Im deeply sorry for yet another question to this list. I have come across a problem to which google seems not to eager to supply the anwser. The problem is the following. First I do this: def setup_drive(): test = pack('>HH', 0b1000, 0b10010001) file = op