Re: pygtk beginner script not working

2015-11-15 Thread kent nyberg

On Sat, Nov 14, 2015 at 05:00:59PM -0800, ja...@imagewebdesign.co.uk wrote:
> Hi guys
> 
> I'm new to Python so please bare with me :)
> 
> I'm using python 2.7.10 as advised (more tools apparently over 3.x)
> 
> Trying to use this script
> 
> [CODE]
> #!/usr/bin/env python
> 
> # example base.py
> 
> import pygtk

It makes most sense to use gtk3 and not gtk2.
The most recenst version of gtk3 is used with
gi package.

Read this for example:
http://python-gtk-3-tutorial.readthedocs.org/en/latest/introduction.html

-- 
https://mail.python.org/mailman/listinfo/python-list


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 = open('drive.bin', 'wb')
for x in range(64):
file.write(test)
file.close()


I have a pack, which I fill the file with. The data is not of interrest right 
now.

This all works great.
Then I do this:


def LoadCommandAndReact(place_to_read):
global RegisterAX

tmp = place_to_read.read()[RegisterAX:calcsize('HH')]
klar = unpack('>HH', tmp)

if place_to_read.closed:
   print("Drive error. Drive closed.")
else:
   pass

if checkfirstbit(klar[RegisterAX]):
   print("First bit is set. Move Cmd.")
   if (klar[0] & 0b0111):
  print("Cmd ERROR: Multiple commands is set.")
  pass
   else:
  #Here something to do with mv command.
  print("Adress to read to, from the offset of the command")
  print(klar[RegisterAX+1])  #Variable is always Offset+1  
else:
   print("First bit is not set.")
#Change RegisterAX offset+2bytes, 
RegisterAX=+2   #We read two bytes per cycle. Becaus command is first, and 
variables are second.




This all works If I run the LoadCommand..  only once.
But when I  run it for the second time,  it complains:

Traceback (most recent call last):
  File "core.py", line 98, in 
LoadCommandAndReact(readfile)
  File "core.py", line 30, in LoadCommandAndReact
klar = unpack('>HH', tmp)
struct.error: unpack requires a string argument of length 4



Im having trouble understanding the error.   unpack requires a string argument 
of length 4.
I have filled the file with data that should be readable, and is readable by 
the function 
when I run it for the first time. And since the file is filled with duplicates; 
should it not be readable the second
time aswell? I change the RegisterAX =+2. The amount I add should not matter, 
Its just my "offset" of where to read in the file.
The problem seems that the second time, unpack doesnt get the size of '>HH' to 
read. But surely 
.read()[RegisterAX:calcsize('HH')]  should assure that its fed with correct 
data?

I have filled the file with data of size '>HH' and first command reads that.  
So next read should read the next
duplicate of that data that has been written to the file?


I understand its hard to read, and a long email. So well, its just a cry out in 
the dark evening.  If some one can
share some light.. I would be much happy about it.

/Kent.
-- 
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 <k...@z-sverige.nu> 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 actually getting is a slice
> starting at the value of RegisterAX and ending at 4. You probably want
> this instead:
> 
> tmp = place_to_read.read()[RegisterAX:RegisterAX + calcsize('HH')]
> -- 
> https://mail.python.org/mailman/listinfo/python-list


Even with that, it still gets wrong.
I also tried .read()[RegisterAX:RegisterAX+4]

As for the other suggestions you had in the second reply, I can comment on them 
later.
 For now though, I just want to be able to loop this specific function.

I do appriciate that you took the time. :)

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 bytes?  I know that I can set 
the arguments 
for unpack. I have done that.  It is: unpack('>HH', tmp).
So either '>HH' or tmp is wrong. Since '>HH' worked for the first run of the 
function, I assume its correct.
And as far as I know,   doing .read()[RegisterAX:RegisterAX:+4]  should read 
the following
4 bytes.

I have even tried doing .read(4),  since then python should manage where to 
start and  read 4?

Still it complains?
-- 
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
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 as,   read from 10th char to 20th.  Sort of.


Is it true then to say that every .read  of a file, places an index-offset (?) 
from where the next read starts?
That is, I need to rewind the file, or else the read will start from where the 
last read stopped?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: using binary in python

2015-11-10 Thread kent nyberg
On Mon, Nov 09, 2015 at 10:20:25PM -0800, Larry Hudson via Python-list wrote:
> Your questions are somewhat difficult to answer because you misunderstand
> binary.  The key is that EVERYTHING in a computer is binary.  There are NO
> EXCEPTIONS, it's all binary ALL the time.  The difference comes about in how
> this binary data is displayed and manipulated.  I want to emphasize, ALL the
> DATA is binary.
> 

Thanks alot for taking the time.
I get it now. I sort of, but not fully, misunderstood the conecpt of binary 
files. 
The thing I was after; and the thing Im playing with now after a more 
succesfull time with google, 
is writing more specific things to a file than just strings.
English is not my native language so please forgive me, but
I wanted to write specifc 16bit codes, and read them. And later play with 
bitwise operations on them. Sort of.
It might not make sense at all, but hey..  it doesnt have to.
Thanks anyway. :)
-- 
https://mail.python.org/mailman/listinfo/python-list


bitwise operator, bits dont go into bitbucket..?

2015-11-10 Thread kent nyberg
Im reading about bitwise operators and is it true to say they dont work 100% as 
in C?
bitwise operators in C seem to result in bits going to the so called bitbucket. 
For example, 0b0001. Shifting it >> 1  in C it seems to add on zero to the 
left and the 1 to the right gets throwned away.

But doing it in python just adds one more bit, from the left.  
That is, 0b0001 >> 1 = 0b1.

Bitwise operators in C (when reading examples,) gives some time code that check 
specific bits by
shifting the bits left and right to make every bit but the specific one to 
zeros. 
As I understand bitwise operators in python, this is not possible then?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: bitwise operator, bits dont go into bitbucket..?

2015-11-10 Thread kent nyberg
On Wed, Nov 11, 2015 at 09:33:38AM +1100, Chris Angelico wrote:
> On Wed, Nov 11, 2015 at 9:27 AM, kent nyberg <k...@z-sverige.nu> wrote:

> 
> If you want to check specific bits (in C or Python, either way), it's
> much more common to use bitwise AND than bit shifts:
> 
> >>> 0b100011011101010110 & 0b0001
> 16
> >>> print(bin(_))
> 0b1
> 

So, to check if 0b010[this one bit]010 is set,   i do   & 0b0001000

That is,  I set just that one to 1 in the other and then the & operator will 
make it return
0 if its not set. Since every other is zero, the return will be zero if its 
not. Since & operator sets 0
if not both are 1. Right?  

Id so, Thanks.   

My misunderstanding was that
0b01000   (for example,) first could be shifted left. To become 0b1.
And then shifted right to become 0b1. 
The shifting would turn every other digit to 0 and leave only the wanted one 
untouched. 
That way, I could check if its 0 or 1.  If you understand my own logic of how 
it works.
But I got it wrong,  and I think I know how to do it with & operator.

> This will be either the same number as the right hand side (if the bit
> had been set) or zero (if it hadn't).
> 
> ChrisA
> -- 
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


using binary in python

2015-11-09 Thread kent nyberg
Hi there,
Lets say I want to play around with binary files in python.
Opening and using files in python is something that I think I've sort of got 
the hang of.
The thing im wondering about is binary files.
While searching for binary and python I started reading about bin().
I can use bin() to convert integers to binary. 
Now, I thought..  some way it should be possible to write "binary" files.
That is, non ascii-files.  For example, on Linux, if I run the command 'less' 
on a binary;
for example /bin/ls, then the command asks me if I really want to do it, since 
its a binary file.
I know why, since the non ascii-stuff messes up the terminal, and most likely 
since you rarely want to
look at a binary file with less. 
Well, lets assume I want to write and read binary.  How is it done?
The built in bin() function only converts integers to binary but the variable 
or return is still just letters right?
Converting the integer '1' to binary with bin() return 0b1. Which is ok. Its 
the binary representation of integer 1. 
But since.. there is files which contains data that is not representable as 
ascii, then I assume it can be written.
But can it by Python?

Ok, I get the feeling now that its hard to understand my question. I assume in 
the C language its just matter of 
writing a complex struct with strange variables and just write it to a file. 
But in python..?

Can some one give a short explenation of a python write to file, that makes it 
a binary file?

Thanks alot,  and forgive me for my stupid questions. :)
/Kent Nyberg
-- 
https://mail.python.org/mailman/listinfo/python-list