Re: [Tutor] report service

2007-02-19 Thread János Juhász
Dear Kent,


> > May someone recommend any simple solution to distribute some dozen
> > parametrizable sql reports
> > for some dozen users with a minimal access controll.
> > Some kind of charting possibilty needed, but I needn't web-based front 
end.

> I'm not sure I understand your requirements. Do you want a way for a
> dozen users to be able to create reports from a database, where they
> select some parameters and create the report on demand?

Currently I have some crystal reports, some dozen excel tables with simple 
or complicated 
database queries and some delphi programs with some grids and sometimes 
with charts. 
All of these 'reports' and it's contents are managed by the IT department.
My dream to have a central repository of database reports, 
organized based on the workflow and controlled by the controlling 
department.
The distribution can be made by filesystem level (each report a separate 
file),
or by application level (the report manager shows a tree with the 
accessible reports).

The candidates are till now:
 crystal report server
 ms reporting services
 commercial reportlab



Yours sincerely,
__
János Juhász
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] What is the augmented assignment operator "^="

2007-02-19 Thread Dick Moores
The docs list it at , and 
send you to , 
which seems a dead end.

I've tried "^=" out a bit:

 >>> n = 5
 >>> n ^= 8
 >>> n
13
 >>> n ^= 8
 >>> n
5
 >>> n ^= 8
 >>> n
13
 >>> n ^= 8
 >>> n
5

and get that strange alternating behavior. Can someone explain?  And 
while at it, please also explain "&=" and "|=".

Thanks,

Dick Moores

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] control multiple FTP sessions using multiple ipconnectionsvia different com ports

2007-02-19 Thread ray sa
Hi
   
  Well now I have configured an old machine in the garage to run at the same 
time. 
   
  I would really like to find out how to do this as it must be possible. I have 
been googling like mad and can't find how. Kind of frustrating when you don't 
know how. At the same time a challenge to seek.
   
  I am kind of hoping that someone on this forum will know how. In the mean 
time I can continue with my testing.
   
  Many thanks for your help; really appreciate it.
   
  /Ray

Johan Geldenhuys <[EMAIL PROTECTED]> wrote:
  Will it be possible to disconnect one of the links during your test and 
reconnect it and disconnect the other connection once the ftp test is finished 
on the first connection? This way it will force the test script to use the 
active route to the internet.
   
  Not the most elegant way, but something to look at in the mean time.
   
  Johan


-
  From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of ray sa
Sent: 18 February 2007 04:16 AM
To: Alan Gauld; tutor@python.org
Subject: Re: [Tutor] control multiple FTP sessions using multiple 
ipconnectionsvia different com ports


  
  Hi Alan et al
   
  Many thanks for your time on this.
   
  I was referring to the client end. 
   
  An EDGE terminal is a phone that has the capability of connecting to the 
internet. We use mobile phones to make a voice call that make use of a circuit 
switched connection. By using a GPRS connection we can use the mobile device to 
connect to the packet switched domain. You might have heard about making data 
connection using your phone like GPRS, 3G etc. EDGE is just faster than a GPRS 
connection and 3G is supposedly faster than GPRS and EDGE. 
   
  May be I can give you more information about what I am trying to do. So you 
can understand what I am trying to achieve. Basically, I am trying to decide 
whether I should change my ADSL fixed solution to a higher speed connection 
using a mobile solution. Where I live the mobile operator is offering a mobile 
solution which is cheaper than my fixed solution….and is faster than my ADSL 
connection, please read on
   
  I have my ADSL broadband connection to my service provider that I use to 
connect to the internet. I have just bought a GPRS/EDGE/3G terminal and wanted 
to benchmark the speed I am getting by using my mobile device as opposed to my 
fixed connection.
   
  My desktop machine is connected to my ADSL provider using a USB modem which 
is assigned a particular COM port on my machine. Then I have connected my 
mobile handset also using a USB connection connected to a different COM port.
   
  Now I have two ip addresses
   

   ADSL – COM port 4 with ip addres from my ADSL service provider   
   Mobile Handset – COM port 5 with another ip address from my mobile provider
   
  I have written a script that connects to a ftp server within my home country 
and downloads a file. But this script uses one of the connections above. There 
must be away to tell the script and control which connection to use. So I can 
get to see real time which connection is faster. So I can run the script 
pointing to one IP address and at the same time run another script using the 
other connection. 
   
  I think there must be a method that finds out which connection is connected 
to which com port and then in my script I need to point towards that connection 
so my script knows which channel to use when downloading the file.
   
  I hope this helps sorry for too much text couldn’t really find a simpler way 
to explain this. 
   
  Once again I really appreciate the help on this forum./Ray
  

Alan Gauld <[EMAIL PROTECTED]> wrote:
  
"ray sa" wrote 

> I have been successful to write an ftp script that logs 
> into a server and collects files. 

> ...I would like to run multiple ftp sessions using one laptop 
> with two different connections. 

Do you mean you want the server on the laptop accepting 
using two connections or do you mean that you want
the client on the laptop and have different sessions sending 
outgoing requests to the server via two different IP addresses?

If the former its easy, just set up two servers, one on each 
IP address. The port will nbe the same on both, ISTR its port 
21 for ftp?

If you want to specify which IP address you send the 
client request from, I'm afraid I have no idea how you specify 
that. I've only ever used multiple network cards at the server 
end!

> another connection using your EDGE terminal 

As a matter of interest what is an EDGE terminal? 
A new term for me...

> I would like to write a script that controls which ip 
> address to use or with com port to use. 

Until we know which end you mean its hard to hgive more 
specific advice.

Alan G.

___
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


-
  No need to miss a message. Get email on-the-go 

Re: [Tutor] What is the augmented assignment operator "^="

2007-02-19 Thread Rikard Bosnjakovic
On 2/19/07, Dick Moores <[EMAIL PROTECTED]> wrote:

> I've tried "^=" out a bit:
[...]
> and get that strange alternating behavior. Can someone explain?
> And while at it, please also explain "&=" and "|=".

^ is XOR, & is AND, | is OR, all bitwise.

You can read more about them here: http://www.somacon.com/p125.php
under "C bitwise operators".


-- 
- Rikard.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is the augmented assignment operator "^="

2007-02-19 Thread Andre Engels

2007/2/19, Dick Moores <[EMAIL PROTECTED]>:


The docs list it at , and
send you to ,
which seems a dead end.

I've tried "^=" out a bit:

>>> n = 5
>>> n ^= 8
>>> n
13
>>> n ^= 8
>>> n
5
>>> n ^= 8
>>> n
13
>>> n ^= 8
>>> n
5

and get that strange alternating behavior. Can someone explain?  And
while at it, please also explain "&=" and "|=".



To understand these operators, you will have to think of the numbers as
binary numbers. Look at the digits. For two numbers x and y, x^y is the
effect of doing an exclusive or on all digits (that is, 0^1 = 1^0 = 1 and
0^0 = 1^1 = 0), & of doing an and (1&1 = 1, 1&0=0&1=0&0=0) and | is an or on
all digits (1|1=1|0=0|1 = 1, 0|0 = 0).

So 5^8 = 110 ^ 1000 = 0110 ^ 1000 = 1110 = 13
and 13^8 = 1110 ^ 1000 = 0110 = 5



--
Andre Engels, [EMAIL PROTECTED]
ICQ: 6260644  --  Skype: a_engels
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] control multiple FTP sessions using multiple ipconnectionsvia different com ports

2007-02-19 Thread Kent Johnson
ray sa wrote:
> Hi
>  
> Well now I have configured an old machine in the garage to run at the 
> same time.
>  
> I would really like to find out how to do this as it must be possible. I 
> have been googling like mad and can't find how. Kind of frustrating when 
> you don't know how. At the same time a challenge to seek.

You might want to ask on comp.lang.python since no one here seems to 
know the answer.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is the augmented assignment operator "^="

2007-02-19 Thread Dick Moores
At 02:17 AM 2/19/2007, Andre Engels wrote:

>To understand these operators, you will have to think of the numbers 
>as binary numbers. Look at the digits. For two numbers x and y, x^y 
>is the effect of doing an exclusive or on all digits (that is, 0^1 = 
>1^0 = 1 and 0^0 = 1^1 = 0), & of doing an and (1&1 = 1, 
>1&0=0&1=0&0=0) and | is an or on all digits (1|1=1|0=0|1 = 1, 0|0 = 0).
>
>So 5^8 = 110 ^ 1000 = 0110 ^ 1000 = 1110 = 13
>and 13^8 = 1110 ^ 1000 = 0110 = 5

Thanks, Andre! I've got it for the three operators, for non-negative 
integers. But I'm not sure I understand how negative integers work. 
For example, is 3 & -3 = 1
because it is 11 & -11 = 01, and that's because one of the first 
digits of 11 and -11 is not 1, and both of their 2nd digits ARE 1, Q.E.D.?

Also, of what practical use are these things?

Dick


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] report service

2007-02-19 Thread Kent Johnson
János Juhász wrote:
> 
> Dear Kent,
> 
> 
>  > > May someone recommend any simple solution to distribute some dozen
>  > > parametrizable sql reports
>  > > for some dozen users with a minimal access controll.
>  > > Some kind of charting possibilty needed, but I needn't web-based 
> front end.
> 
>  > I'm not sure I understand your requirements. Do you want a way for a
>  > dozen users to be able to create reports from a database, where they
>  > select some parameters and create the report on demand?
> 
> Currently I have some crystal reports, some dozen excel tables with 
> simple or complicated
> database queries and some delphi programs with some grids and sometimes 
> with charts.
> All of these 'reports' and it's contents are managed by the IT department.
> My dream to have a central repository of database reports,
> organized based on the workflow and controlled by the controlling 
> department.
> The distribution can be made by filesystem level (each report a separate 
> file),
> or by application level (the report manager shows a tree with the 
> accessible reports).
> 
> The candidates are till now:
>  crystal report server
>  ms reporting services
>  commercial reportlab

So you want a program that will generate the report files automatically?
What about the open source ReportLab?

You might still want to look at Dabo, it has a report generator built on 
ReportLab that adds some database integration.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is the augmented assignment operator "^="

2007-02-19 Thread Kent Johnson
Dick Moores wrote:
> The docs list it at , and 
> send you to , 
> which seems a dead end.

a += n is more-or-less a shortcut for a = a + n. There are a few 
subtleties which the first page you reference talks about, but you can 
generally think of it as a handy abbreviation.

For other operations, the same is true:
a = n is the same as a = a  n

> I've tried "^=" out a bit and get that strange alternating behavior.

which is normal operation of ^. Try it with + or * for a simpler example.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is the augmented assignment operator "^="

2007-02-19 Thread Dick Moores
At 03:32 AM 2/19/2007, you wrote:
>Dick Moores wrote:
>>The docs list it at , 
>>and send you to 
>>, which seems a dead end.
>
>a += n is more-or-less a shortcut for a = a + n. There are a few 
>subtleties which the first page you reference talks about, but you 
>can generally think of it as a handy abbreviation.
>
>For other operations, the same is true:
>a = n is the same as a = a  n
>
>>I've tried "^=" out a bit and get that strange alternating behavior.
>
>which is normal operation of ^. Try it with + or * for a simpler example.

Of those listed on ,  I 
already use and understand "+=" | "-=" | "*=" | "/=" | "%=" | "**=" . 
It's the remaining seven I'm wondering about, or really about >>, <<, 
&, ^, and | . Andre's given me a good start, as you may have seen by now.

Dick


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is the augmented assignment operator "^="

2007-02-19 Thread Andre Engels

2007/2/19, Dick Moores <[EMAIL PROTECTED]>:


At 02:17 AM 2/19/2007, Andre Engels wrote:

>To understand these operators, you will have to think of the numbers
>as binary numbers. Look at the digits. For two numbers x and y, x^y
>is the effect of doing an exclusive or on all digits (that is, 0^1 =
>1^0 = 1 and 0^0 = 1^1 = 0), & of doing an and (1&1 = 1,
>1&0=0&1=0&0=0) and | is an or on all digits (1|1=1|0=0|1 = 1, 0|0 = 0).
>
>So 5^8 = 110 ^ 1000 = 0110 ^ 1000 = 1110 = 13
>and 13^8 = 1110 ^ 1000 = 0110 = 5

Thanks, Andre! I've got it for the three operators, for non-negative
integers. But I'm not sure I understand how negative integers work.
For example, is 3 & -3 = 1
because it is 11 & -11 = 01, and that's because one of the first
digits of 11 and -11 is not 1, and both of their 2nd digits ARE 1, Q.E.D.?



This has to do with the internal representation of negative numbers: -1 is
represented as 111111, with one 1 more than maxint. -2 is 111...110 and
-3 is 111...101. Thus 3 & -3 is 11 & 111...101 = 1

Also, of what practical use are these things?




I guess they have their uses for people accustomed to dealing with hardware.
Apart from that, you can get a very space-efficient representation for
multiple boolean variables. If you have what looks like an array of
booleans, you could also represent it as a single natural number, for
example [true, true, false, false, true, false] would then be 1*1 + 1*2 +
0*4 + 0*8 + 1*16 + 0*32 = 19. Using such a representation and the above
operators would enable one to write

z = x & y

instead of

z = [x[i] and y[i] for i in range(len(x))]

when modelling the same using lists.

Of course this does come at the price of complicating

x[i] = true

to

x |= 2 ** i

which though not really longer does definitely look harder to understand.

--
Andre Engels, [EMAIL PROTECTED]
ICQ: 6260644  --  Skype: a_engels
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is the augmented assignment operator "^="

2007-02-19 Thread Rikard Bosnjakovic
On 2/19/07, Dick Moores <[EMAIL PROTECTED]> wrote:

> It's the remaining seven I'm wondering about, or really about >>, <<,
> &, ^, and | .

This webpage will tell you - in detail - about all the operators:
http://www.lnf.infn.it/Calcolo/doc/aixcxx/html/language/ref/ruclxbin.htm

The bitwise operators are hard to understand without proper knowledge
about the binary number system. I recommend you to read up about it,
if you feel the operators are somewhat difficult to understand.


-- 
- Rikard.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is the augmented assignment operator "^="

2007-02-19 Thread Alan Gauld
"Dick Moores" <[EMAIL PROTECTED]> wrote

> Thanks, Andre! I've got it for the three operators, for non-negative
> integers. But I'm not sure I understand how negative integers work.
> For example, is 3 & -3 = 1
> because it is 11 & -11 = 01, and that's because one of the first
> digits of 11 and -11 is not 1, and both of their 2nd digits ARE 1, 
> Q.E.D.?

Remember that the digits are 32 bits long so 3 is not  11
but
011

And the representation of negative numbers is more complex
than you might think.

You can see the full hex representations using struct:

>>> import struct
>>> struct.pack('i',-3)
'\xfd\xff\xff\xff'
>>> struct.pack('i',3)
'\x03\x00\x00\x00'

Since most of 3 is zeros the and results will be zro, so
lets look at the significant bits:

>>> 0xfd & 0x03
1
>>>

voila!

> Also, of what practical use are these things?

Take a look at my Using the OS topic, there is a side-bar(box)
just short of half way down that describes how these can be
used to test file status. There are many other uses too, but
that's one real-world case.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Struct the solution for Hex translation

2007-02-19 Thread Johan Geldenhuys
Hi all,
 
I read in some conversations that the struct module maybe helpful in
converting Hex values to binary or decimal. Maybe I understood it
incorrectly.
 
Here is my problem.
 
I have a 22 byte data packet on a tcp socket connection. My data field is
from the 6th byte to byte 20. 14 bytes in total.
The first two bytes of the data is a 16 bit value. Eg: "\xe2\x01'
 
I can the first byte into binary if I use 'e2', but I don't know how to get
the '\x' out of the first byte to use it in python. My data has the '\x' and
all I need is the 'e2'.
 
Any advice of the usage of the struct module or how I can get rid of the
'\x' in my data?
 
Hopefully I have given enough information.
 
Thanks
 
Johan

-- 
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.441 / Virus Database: 268.18.2/692 - Release Date: 2007/02/18
04:35 PM
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] control multiple FTP sessions using multiple ipconnectionsvia different com ports

2007-02-19 Thread David Perlman
This kind of thing is usually handled at the level of the OS's  
routing table.  Routing tables have an entry called "metric" that is  
used to weight the different routes, so that when there are multiple  
possible links available, the one with the lowest metric is used  
first.  In Unix at least, you'll be able to use either the command  
"route" or "routed" to manually change the routing table; you can  
change the metrics for the two interfaces to control which one gets  
used.  unfortunately this will change the routing globally, not just  
for a specific connection.  I don't think the routing systems on  
ordinary computers were designed with the idea in mind that you might  
want to use a specific link for a specific connection.

If you're on UNIX, run netstat -nr.  If you're on Windoze, run route  
print.  You should be able to see the metric for each entry.
http://www.pku.edu.cn/academic/research/computer-center/tc/html/ 
TC0310.html
Unfortunately, the "metric" column doesn't show up under OS X, and I  
haven't been able to figure out why in 5 minutes of searching.  Also  
unfortunately, I don't know any more about this, and searching on  
Google was not revealing immediate results.  But hopefully this will  
get you started in the right direction!

So the bottom line, I guess, is that a) this is a routing question,  
not a python question; and b) it's not an easy question.  :)

On Feb 19, 2007, at 3:53 AM, ray sa wrote:

> Hi
>
> Well now I have configured an old machine in the garage to run at  
> the same time.
>
> I would really like to find out how to do this as it must be  
> possible. I have been googling like mad and can't find how. Kind of  
> frustrating when you don't know how. At the same time a challenge  
> to seek.
>
> I am kind of hoping that someone on this forum will know how. In  
> the mean time I can continue with my testing.
>
> Many thanks for your help; really appreciate it.
>
> /Ray
>
> Johan Geldenhuys <[EMAIL PROTECTED]> wrote:
> Will it be possible to disconnect one of the links during your test  
> and reconnect it and disconnect the other connection once the ftp  
> test is finished on the first connection? This way it will force  
> the test script to use the active route to the internet.
>
> Not the most elegant way, but something to look at in the mean time.
>
> Johan
>
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On  
> Behalf Of ray sa
> Sent: 18 February 2007 04:16 AM
> To: Alan Gauld; tutor@python.org
> Subject: Re: [Tutor] control multiple FTP sessions using multiple  
> ipconnectionsvia different com ports
>
> Hi Alan et al
>
> Many thanks for your time on this.
>
> I was referring to the client end.
>
> An EDGE terminal is a phone that has the capability of connecting  
> to the internet. We use mobile phones to make a voice call that  
> make use of a circuit switched connection. By using a GPRS  
> connection we can use the mobile device to connect to the packet  
> switched domain. You might have heard about making data connection  
> using your phone like GPRS, 3G etc. EDGE is just faster than a GPRS  
> connection and 3G is supposedly faster than GPRS and EDGE.
>
> May be I can give you more information about what I am trying to  
> do. So you can understand what I am trying to achieve. Basically, I  
> am trying to decide whether I should change my ADSL fixed solution  
> to a higher speed connection using a mobile solution. Where I live  
> the mobile operator is offering a mobile solution which is cheaper  
> than my fixed solution….and is faster than my ADSL connection,  
> please read on
>
> I have my ADSL broadband connection to my service provider that I  
> use to connect to the internet. I have just bought a GPRS/EDGE/3G  
> terminal and wanted to benchmark the speed I am getting by using my  
> mobile device as opposed to my fixed connection.
>
> My desktop machine is connected to my ADSL provider using a USB  
> modem which is assigned a particular COM port on my machine. Then I  
> have connected my mobile handset also using a USB connection  
> connected to a different COM port.
>
> Now I have two ip addresses
>
> ADSL – COM port 4 with ip addres from my ADSL service provider
> Mobile Handset – COM port 5 with another ip address from my mobile  
> provider
>
> I have written a script that connects to a ftp server within my  
> home country and downloads a file. But this script uses one of the  
> connections above. There must be away to tell the script and  
> control which connection to use. So I can get to see real time  
> which connection is faster. So I can run the script pointing to one  
> IP address and at the same time run another script using the other  
> connection.
>
> I think there must be a method that finds out which connection is  
> connected to which com port and then in my script I need to point  
> towards that connection so my script knows which channel to use  
> when downloading the file.
>
> I hope this helps

Re: [Tutor] Struct the solution for Hex translation

2007-02-19 Thread Alan Gauld

"Johan Geldenhuys" <[EMAIL PROTECTED]> wrote 

> The first two bytes of the data is a 16 bit value. Eg: "\xe2\x01'
> 
> I can the first byte into binary if I use 'e2', but I don't know 
> how to get the '\x' out of the first byte to use it in python. 

Are you sure it is there?
Usually the \x is only part of the repr string, its not actually 
in the data. What do you get is you do:

byte = data[0]  # get the first byte
print len(byte)   # should only be one byte
print byte # should get '\xe2' or whatever.

> My data has the '\x' and all I need is the 'e2'.

If you do have the \xe2  that implies you have 4 characters, 
ie 4 bytes, so to get the real value use int(data[2:],16)

> Any advice of the usage of the struct module or how 
> I can get rid of the '\x' in my data?

I'm not sure where the struct module comes in? Are 
you using struct to read the data? If so you should be 
able to use unpack the data into the format you need 
by specifying a format string.

eg

struct.unpack('cc5s',data)

Should return two characters(bytes) and a 98 character 
string. Like so:

>>> struct.unpack('cc5s','\x12\x23abcde')
('\x12', '#', 'abcde')
>>>

Is that what you want?
Notice that the first value is actially a single byte of 
value 12 hex. The \x are only in the display.

The >>> prompt is a great place to experiment with struct
format strings etc.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is the augmented assignment operator "^="

2007-02-19 Thread Dick Moores
My sincere thanks to Rikard Bosnjakovic, Andre Engels, and Alan 
Gauld. I think you've given me a good start toward understanding the 
operators >>, <<,  &, ^, and  | ; 32-bit numbers, and negative binary numbers.

Dick Moores


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Struct the solution for Hex translation

2007-02-19 Thread Johan Geldenhuys
 Here is what I have:

>>> data
'\xa5\x16\x0b\x0b\x00\xd5\x01\x01\x01\x00\x00\xe3\x84(\x01\xc6\x00\x00\x17\x
01C\xc7'
>>> data[0]
'\xa5'
>>> len(data[0])
1
>>>

You see that data[0] is only one byte and it doesn't see all four
characters.

If I want to do this:

>>> int(data[0], 16)
  File "", line 1, in ?
''' exceptions.ValueError : invalid literal for int(): ¥ '''


But I can do this:

>>> int('a5', 16)
165
>>>

If I use data[0] as it is, I get errors. That why I want to know how I can
strip away the '\x'.

Here is some other code to convert Hex to Binary:

hex2bin = {
"0" : "", "1" : "0001", "2" : "0010", "3" : "0011", 
"4" : "0100", "5" : "0101", "6" : "0110", "7" : "0111", 
"8" : "1000", "9" : "1001", "a" : "1010", "b" : "1011", 
"c" : "1100", "d" : "1101", "e" : "1110", "f" : ""
}

>>> def hexBin(hexchars):
... s = ""
for hexchar in hexchars:
s += hex2bin[hexchar]
return s.rstrip("\n")
... 
>>> hexBin('a5')
'10100101'

This however does not work if my argument is '\xa5'.

>>> hexBin('\xa5')
  File "", line 1, in ?
  File "", line 5, in hexBin
''' exceptions.KeyError : '\xa5' '''
>>> 

Thanks

Johan

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
Of Alan Gauld
Sent: 19 February 2007 05:04 PM
To: tutor@python.org
Subject: Re: [Tutor] Struct the solution for Hex translation


"Johan Geldenhuys" <[EMAIL PROTECTED]> wrote 

> The first two bytes of the data is a 16 bit value. Eg: "\xe2\x01'
> 
> I can the first byte into binary if I use 'e2', but I don't know how 
> to get the '\x' out of the first byte to use it in python.

Are you sure it is there?
Usually the \x is only part of the repr string, its not actually in the
data. What do you get is you do:

byte = data[0]  # get the first byte
print len(byte)   # should only be one byte
print byte # should get '\xe2' or whatever.

> My data has the '\x' and all I need is the 'e2'.

If you do have the \xe2  that implies you have 4 characters, ie 4 bytes, so
to get the real value use int(data[2:],16)

> Any advice of the usage of the struct module or how I can get rid of 
> the '\x' in my data?

I'm not sure where the struct module comes in? Are you using struct to read
the data? If so you should be able to use unpack the data into the format
you need by specifying a format string.

eg

struct.unpack('cc5s',data)

Should return two characters(bytes) and a 98 character 
string. Like so:

>>> struct.unpack('cc5s','\x12\x23abcde')
('\x12', '#', 'abcde')
>>>

Is that what you want?
Notice that the first value is actially a single byte of 
value 12 hex. The \x are only in the display.

The >>> prompt is a great place to experiment with struct
format strings etc.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

-- 
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.441 / Virus Database: 268.18.2/692 - Release Date: 2007/02/18
04:35 PM
 

-- 
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.441 / Virus Database: 268.18.2/692 - Release Date: 2007/02/18
04:35 PM
 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Where is the __builtin__ module?

2007-02-19 Thread Dick Moores
"import __builtin__" occurs frequently in files in Lib/, but I'm 
unable to find a module named "__builtin__" or "__builtin__.py" 
anywhere in my Python 2.5. Is there one? If there is, where is it?

Thanks,

Dick Moores 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Struct the solution for Hex translation

2007-02-19 Thread David Perlman
You're way off base... :)

On Feb 19, 2007, at 9:25 AM, Johan Geldenhuys wrote:

>  Here is what I have:
>
 data
> '\xa5\x16\x0b\x0b\x00\xd5\x01\x01\x01\x00\x00\xe3\x84(\x01\xc6\x00 
> \x00\x17\x
> 01C\xc7'
 data[0]
> '\xa5'
 len(data[0])
> 1

>
> You see that data[0] is only one byte and it doesn't see all four
> characters.
>
> If I want to do this:
>
 int(data[0], 16)
>   File "", line 1, in ?
> ''' exceptions.ValueError : invalid literal for int(): ¥ '''
>
>
> But I can do this:
>
 int('a5', 16)
> 165

>
> If I use data[0] as it is, I get errors. That why I want to know  
> how I can
> strip away the '\x'.

This is what you want to do:
 >>> import struct
 >>> struct.unpack('B',data[0])
(165,)

Once again, the \x doesn't really exist, any more than the quotation  
marks do.  They're just ways of indicating on the screen what kind of  
data is being displayed.

> Here is some other code to convert Hex to Binary:
>
> hex2bin = {
> "0" : "", "1" : "0001", "2" : "0010", "3" : "0011",
> "4" : "0100", "5" : "0101", "6" : "0110", "7" : "0111",
> "8" : "1000", "9" : "1001", "a" : "1010", "b" : "1011",
> "c" : "1100", "d" : "1101", "e" : "1110", "f" : ""
> }
>
 def hexBin(hexchars):
> ... s = ""
> for hexchar in hexchars:
> s += hex2bin[hexchar]
> return s.rstrip("\n")
> ...
 hexBin('a5')
> '10100101'
>
> This however does not work if my argument is '\xa5'.
>
 hexBin('\xa5')
>   File "", line 1, in ?
>   File "", line 5, in hexBin
> ''' exceptions.KeyError : '\xa5' '''


This function is useless in this case because you don't actually have  
a string of letters and numbers; you just have raw binary data.

--
-dave
After all, it is not *that* inexpressible.
-H.H. The Dalai Lama



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Main program confusion

2007-02-19 Thread Hazlett, Les
Hello,

 

I am trying to understand some sample code that includes the following
for a main program:

 

def runTest(frame, nb, log):

win = TestPanel(nb, log)

return win

 

if __name__ == '__main__':

import sys,os

import run

run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])

 

I understand that win is an object of the class TestPanel.  The class
code for TestPanel preceded

the code which I copied above.  The object win clearly is instantiated.
I understand how the 

if__name__=='__main__' only runs when this program file is executed.

But, I can't find any reference to a module run and I don't see how the
"run.main([" line casues 

the "runTest(" function to run.

 

What am I missing?

 

Thanks for any help.

 

Les Hazlett



The information contained in this communication may be CONFIDENTIAL and is 
intended only for the use of the recipient(s) named above. If you are not the 
intended recipient, you are hereby notified that any dissemination, 
distribution, or copying of this communication, or any of its contents, is 
strictly prohibited. If you have received this communication in error, please 
notify the sender and delete/destroy the original message and any copy of it 
from your computer or paper files.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Struct the solution for Hex translation

2007-02-19 Thread Alan Gauld

"Johan Geldenhuys" <[EMAIL PROTECTED]> wrote

>>> data
'\xa5\x16\x0b\x0b\x00\xd5\x01\x01\x01\x00\x00\xe3\x84(\x01\xc6\x00\x00\x17\x
01C\xc7'
>>> data[0]
'\xa5'
>>> len(data[0])
1
>>>

OK, So that tells you that you have one byte.
The '\xa5' is a 4 character representation of that byte but it
is only one byte with hex value a5. You don;t need to strip
anything, you have your byte.

>>> int(data[0], 16)
  File "", line 1, in ?
''' exceptions.ValueError : invalid literal for int(): ¥ '''

Its already an int (well a byte) you don;t need to use int()

>>> int('a5', 16)
165

Because that passes the 2 byte string 'a5' to int. But you
don't need that your byte actualoly is a5

> If I use data[0] as it is, I get errors. That why I want to
> know how I can strip away the '\x'.

The \x doesn't exist its purely a part of the replresentation
that Python uses to display the data. Its like the L at the
end of a long interer. The number doesn't really have an L
at the end its just put there by Python to show the type.
Similarly the '\x' is prepended by Python to show that this
is a hex value.

>>> def hexBin(hexchars):
... s = ""
for hexchar in hexchars:
s += hex2bin[hexchar]
return s.rstrip("\n")
...
>>> hexBin('a5')
'10100101'

This however does not work if my argument is '\xa5'.

>>> hexBin('\xa5')
  File "", line 1, in ?
  File "", line 5, in hexBin
''' exceptions.KeyError : '\xa5' '''

Because you are now passing a single character which
is not a valid hex character.

So far as I can see you actually have the data you want!


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Where is the __builtin__ module?

2007-02-19 Thread Rikard Bosnjakovic
On 2/19/07, Dick Moores <[EMAIL PROTECTED]> wrote:

> Is there one? If there is, where is it?

Yes, there is one. It'sbuiltin(!) in the interpreter! ;-)

-- 
- Rikard.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Where is the __builtin__ module?

2007-02-19 Thread Alan Gauld
"Dick Moores" <[EMAIL PROTECTED]> wrote

> "import __builtin__" occurs frequently in files in Lib/,

Really? I'm surprised, I can't think why.

> unable to find a module named "__builtin__" or "__builtin__.py"
> anywhere in my Python 2.5. Is there one? If there is, where is it?

It's C code so it's not a .py file.
There is probably a C library/DLL somewhere but the whole
point of builtin is that it is built in to the core Python interpreter
so there is no physically separate file you can see.

You can read the C source here:

http://svn.python.org/view/python/trunk/Python/bltinmodule.c?rev=52315&view=markup

However you'll find that most of them basically just forward the
call to another function elsewhere - sometimes with a bit of
input validation first. See the abs() function as a short example:

static PyObject *
builtin_abs(PyObject *self, PyObject *v)
{
return PyNumber_Absolute(v);
}

HTH,

Alan G. 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Main program confusion

2007-02-19 Thread Dave Kuhlman
On Mon, Feb 19, 2007 at 10:15:08AM -0600, Hazlett, Les wrote:

> 
> if __name__ == '__main__':
> import sys,os
> import run
> run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
>  
[snip]
> if__name__=='__main__' only runs when this program file is executed.
> 
> But, I can't find any reference to a module run and I don't see how the
> "run.main([" line casues 
> 
> the "runTest(" function to run.

Step 1 -- Find the run module.  If the import of "run" succeeds,
you can find out where the module run is installed on your system
by adding a print statement:

import run
print "run module:", run

That will show you where Python is finding the run module.  

And, if the import statement is failing, then you are likely to
need to install something in order to execute that sample code.

Once you have found the run module, ...

Step 2 -- Read the source in the run module.  In particular, you
are looking for a function named main.

Dave

-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Main program confusion

2007-02-19 Thread Luke Paireepinart
Hazlett, Les wrote:
>
> Hello,
>
> I am trying to understand some sample code that includes the following 
> for a main program:
>
> def runTest(frame, nb, log):
>
> win = TestPanel(nb, log)
>
> return win
>
> if __name__ == '__main__':
>
> import sys,os
>
> import run
>
> run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
>
> I understand that win is an object of the class TestPanel. The class 
> code for TestPanel preceded
>
> the code which I copied above. The object win clearly is instantiated. 
> I understand how the
>
> if__name__==’__main__’ only runs when this program file is executed.
>
> But, I can’t find any reference to a module run and I don’t see how 
> the “run.main([“ line casues
>
> the “runTest(“ function to run.
>
I've never heard of this module. Where did this code come from?
Have you tried the code? Either run is some builtin I haven't heard of, 
or the code is in some library/turorial that defines a custom module.
-Luke
>
> What am I missing?
>
> Thanks for any help.
>
> Les Hazlett
>
> 
> The information contained in this communication may be CONFIDENTIAL 
> and is intended only for the use of the recipient(s) named above. If 
> you are not the intended recipient, you are hereby notified that any 
> dissemination, distribution, or copying of this communication, or any 
> of its contents, is strictly prohibited. If you have received this 
> communication in error, please notify the sender and delete/destroy 
> the original message and any copy of it from your computer or paper 
> files.
> 
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>   

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] control multiple FTP sessions using multiple ipconnectionsvia different com ports

2007-02-19 Thread Alan Gauld

"David Perlman" <[EMAIL PROTECTED]> wrote

> first.  In Unix at least, you'll be able to use either the command
> "route" or "routed" to manually change the routing table; 
> I don't think the routing systems on  ordinary computers
> were designed with the idea in mind that you might
> want to use a specific link for a specific connection.

That's true, however

> So the bottom line, I guess, is that a) this is a routing
> question,  not a python question; and b) it's not an easy
> question.  :)

Only because its so rarely done.
After a bit of digging it seems that socket.bind() can be
used on the client side as well as on the server side.
Thus if you really do want to limit your connections
you can tell the socket which interface to use.
You need to know the IP address of the interface
of course.

Caveat: I've never done this, nor even seen it done.
But the book Python Network Programming by Goerzen
specifically says its possible, although even it doesn't
give an example...

Regards,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Where is the __builtin__ module?

2007-02-19 Thread Kent Johnson
Rikard Bosnjakovic wrote:
> On 2/19/07, Dick Moores <[EMAIL PROTECTED]> wrote:
> 
>> Is there one? If there is, where is it?
> 
> Yes, there is one. It'sbuiltin(!) in the interpreter! ;-)

More info here:
http://docs.python.org/lib/module-builtin.html

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Main program confusion

2007-02-19 Thread [EMAIL PROTECTED]

It's wxpython demo's code.
It's necessary to have the run.py in the same path, to make it run, and usually 
also some other files.

--
Tom, http://www.vscripts.net

>
I've never heard of this module. Where did this code come from?

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Main program confusion

2007-02-19 Thread Hazlett, Les
Dave, 

Thanks for the guidance.  I followed your advice and learned the
following:

>>> import run

>>> print "run module:", run

run module: 

 

The run.py file is there also.  I can read but not understand what
run.py does.  It may be something new in 

Python 2.5.  I have unistalled Python 2.4 so I can't tell.

 

Luke,

I found this mystery main program in the extensive demos in \wsPython2.8
Docs and Demos\demo\.  It is 

used throughout the demo code - in every demo sample I have looked at.  

 

It was a mystery to me when I found it there and tried to understand it.
It is still a mystery.  I guess that, 

I will just accept it as "pure magic" and go on with my effort to
understand the many demo segments found 

there.

 

Thanks,

Les



The information contained in this communication may be CONFIDENTIAL and is 
intended only for the use of the recipient(s) named above. If you are not the 
intended recipient, you are hereby notified that any dissemination, 
distribution, or copying of this communication, or any of its contents, is 
strictly prohibited. If you have received this communication in error, please 
notify the sender and delete/destroy the original message and any copy of it 
from your computer or paper files.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Main program confusion

2007-02-19 Thread Kent Johnson
Hazlett, Les wrote:
> Dave,
> 
> Thanks for the guidance.  I followed your advice and learned the following:
> 
>> >> import run
> 
>> >> print "run module:", run
> 
> run module: 

I don't think that is the correct run.py.

> Luke,
> 
> I found this mystery main program in the extensive demos in \wsPython2.8 
> Docs and Demos\demo\.  It is
> 
> used throughout the demo code – in every demo sample I have looked at. 

There is a run.py in the demo folder, that is the one you get when you 
run the demos.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Main program confusion

2007-02-19 Thread Hazlett, Les
Thanks Kent,

 

Yes, there is a run.py in the demo folder.  I is the one that runs and
not the one in the Python25 lib.  

 

So, I tried to see if I could find it if I previously imported
everything that the code imports.   It didn't - see below:

 

 

IDLE 1.2  

>>> import wx

>>> import sys,os

>>> import run

>>> print "run module", run

run module 

>>> 

 

This has been a good lesson in my learning process.  Thanks everyone.

 

Les Hazlett



The information contained in this communication may be CONFIDENTIAL and is 
intended only for the use of the recipient(s) named above. If you are not the 
intended recipient, you are hereby notified that any dissemination, 
distribution, or copying of this communication, or any of its contents, is 
strictly prohibited. If you have received this communication in error, please 
notify the sender and delete/destroy the original message and any copy of it 
from your computer or paper files.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Main program confusion

2007-02-19 Thread Luke Paireepinart
Hazlett, Les wrote:
>
> Thanks Kent,
>
> Yes, there is a run.py in the demo folder. I is the one that runs and 
> not the one in the Python25 lib.
>
> So, I tried to see if I could find it if I previously imported 
> everything that the code imports. It didn’t – see below:
>
> IDLE 1.2
>
> >>> import wx
>
> >>> import sys,os
>
> >>> import run
>
> >>> print "run module", run
>
> run module 
>
> >>>
>
> This has been a good lesson in my learning process. Thanks everyone.
>
It's not finding it because you're running your code in the directory 
that IDLE is located in!
If you start a Python interpreter in the Demo directory, you will be 
able to find the file you need.
Python searches the current working directory for modules first before 
it does anything else.
Coincidentally, Idle has a module named 'run' and since the 
interpreter's current working directory is IDLE's directory,
it imports that one.
Try using the commands
import os
os.chdir("Path/To/Demo/Directory")

and import the module.
You should get the correct one, then.

HTH,
-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Accessing class attributes: use methods only?

2007-02-19 Thread Chris Lasher
Thanks very much for your responses, all. Just to clarify, yes, by
"through class methods" I actually meant "through methods of instances
of a class".

Now for more discussion: I'm confused. On the one hand we have Mr.
Kent Johnson's statement:

On 2/13/07, Kent Johnson <[EMAIL PROTECTED]> wrote:
> Python practice is to use direct attribute access. If you need to do
> something special when an attribute is read or set, you can create a
> property. This allows you to define methods to be called when the
> attribute is accessed, without having to change the client code.

So to paraphrase, he states it's the Python way to do:
my_instance = MyClass()
my_instance.x = 42

On the other hand, we have Mr. Alan Gauld, who states:

On 2/13/07, Alan Gauld <[EMAIL PROTECTED]> wrote:
> Its generally good OOP practice to interact with object via messages.
> Its also good practice NOT to access an objects attributes directly
> (and that includes via get/set methods) A class should publish a
> set of operations. The attributes should be there to support
> those operations.

So to paraphrase, he states it's the right way, regardless of language, to do:
my_instance = MyClass()
my_instance.setx(42)

I'm used to just setting and getting attributes straight, which would
be Pythonic according to Kent and yet also outright wrong according to
Alan and academic papers. So is direct access actually not Pythonic,
or is it Pythonic and Pythonistas spit in the face of Demeter and her
lovely laws?

Curious,
Chris
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Accessing class attributes: use methods only?

2007-02-19 Thread Python
On Mon, 2007-02-19 at 18:31 -0500, Chris Lasher wrote:
> I'm used to just setting and getting attributes straight, which would
> be Pythonic according to Kent and yet also outright wrong according to
> Alan and academic papers. So is direct access actually not Pythonic,
> or is it Pythonic and Pythonistas spit in the face of Demeter and her
> lovely laws?

Kent and Alan can speak quite eloquently for themselves, but just to
provide a more immediate answer, I expect they mostly agree with each
other.

The issue isn't whether you code:
my.x = 42
or
my.setx = 42

Alan is saying you should not generally be twiddling attributes in an
object.

Kent is suggesting that if you do decide to twiddle attributes in
Python, just do it directly.  If later on you decide you need some
method logic to control the attribute twiddling, you can use
property
to invoke methods when directly accessing the attribute.  I do not think
there is anything to be gained in Python by expecting your object
interface to depend on the use of get/set methods.

-- 
Lloyd Kvam
Venix Corp

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Accessing class attributes: use methods only?

2007-02-19 Thread Alan Gauld
"Chris Lasher" <[EMAIL PROTECTED]> wrote

> Now for more discussion: I'm confused.

:-)
Actually there is no real contradiction. I'll try to explain
and if I misrepresent Kent I'm sure he'll say so!

> Kent Johnson's statement:

>> Python practice is to use direct attribute access. If you need to 
>> do
>
> So to paraphrase, he states it's the Python way to do:
> my_instance = MyClass()
> my_instance.x = 42

And this is quite correct. In fact I specifically said in my reply
that if you really *must* access an attribute of a class it is
better to just do it directly than to write setX/getX methods

> On the other hand, we have Mr. Alan Gauld, who states:
>> Its generally good OOP practice to interact with object via 
>> messages.
>> Its also good practice NOT to access an objects attributes directly

So what I'm saying is that you should try to write classes
such that nobody ever needs to access the internal data. If a
user needs access to the internals it is often a sign that there
is some functionality of the class missing. Why do you need
the data? Shouldn't the class that owns the data do all the
manipulation of it? That's what OOP is supposed to be
about - creating objects which receive messages that tell
them what to do. Some of those methods will return data
values but you should neither know nor care whether they
are data attributes internally or whether they are calculated
values.

> So to paraphrase, he states it's the right way, regardless
> of language, to do:
> my_instance = MyClass()
> my_instance.setx(42)

No, I'm saying it's the wrong way regardless of language to do that.
(With the exception of JavaBeans - and not all Java classes need
to be beans! - because they rely on the set/get protocol to
support IDEs etc)

More usefully you should hopefully have a method that has a
meaningful, operation-based name, that results in the internal
variable x being set to 42. But that should be a by-product.
The user of the class should not, in general, even know that
x exists!

Now that's in an ideal world and as such it doesn't exist. So we
don't always have that luxury. And some objects are more or less
just data stores - but they should be the exception not the rule!
In those exceptional cases, if you need to access a data attribute,
then it's OK to use direct access in Python.

> I'm used to just setting and getting attributes straight, which 
> would
> be Pythonic according to Kent and yet also outright wrong according 
> to
> Alan and academic papers. So is direct access actually not Pythonic,

Direct access is Pythonic *when necessary*.
Direct access 9including via set/get) is wrong in an OOP sense in
any language. The methods should expose a set of operations which
do everything you need to do without your knowing about the
internals of the object.

> Pythonistas spit in the face of Demeter and her lovely laws?

Nope, but Python allows you to break the laws in the simplest
and most direct manner. One of Python's tenets is that we are
all responsible programmers. When we break the laws of good
programming we do so in a deliberate way, fully aware of why
we do it and of the potential consequences.

So both Kent and I are saying the same thing: don't use
setX/getX; use direct access if you have to. But I add the caveat
that you should try to avoid the need for direct access in the first
place.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Accessing class attributes: use methods only?

2007-02-19 Thread Chris Lasher
Ah ha! All is clear, now. I understand what I misinterpreted in your
first post, Alan. Thanks also to Lloyd for reinforcing the concept.
Much appreciated!

Chris
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Struct the solution for Hex translation

2007-02-19 Thread Johan Geldenhuys
 Thanks, Dave.

On the struct module, How can I het the binary 1's and 0's of the Hex value?
Let say I want to get the 8 bit value of '\xe2', can I use struct to convert
that into binary code so that I get 8 binary bits as a string?

Thanks for helping with struct.

Johan

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
Of David Perlman
Sent: 19 February 2007 05:56 PM
To: tutor@python.org
Subject: Re: [Tutor] Struct the solution for Hex translation

You're way off base... :)

On Feb 19, 2007, at 9:25 AM, Johan Geldenhuys wrote:

>  Here is what I have:
>
 data
> '\xa5\x16\x0b\x0b\x00\xd5\x01\x01\x01\x00\x00\xe3\x84(\x01\xc6\x00
> \x00\x17\x
> 01C\xc7'
 data[0]
> '\xa5'
 len(data[0])
> 1

>
> You see that data[0] is only one byte and it doesn't see all four 
> characters.
>
> If I want to do this:
>
 int(data[0], 16)
>   File "", line 1, in ?
> ''' exceptions.ValueError : invalid literal for int(): ¥ '''
>
>
> But I can do this:
>
 int('a5', 16)
> 165

>
> If I use data[0] as it is, I get errors. That why I want to know how I 
> can strip away the '\x'.

This is what you want to do:
 >>> import struct
 >>> struct.unpack('B',data[0])
(165,)

Once again, the \x doesn't really exist, any more than the quotation marks
do.  They're just ways of indicating on the screen what kind of data is
being displayed.

> Here is some other code to convert Hex to Binary:
>
> hex2bin = {
> "0" : "", "1" : "0001", "2" : "0010", "3" : "0011", "4" : "0100", 
> "5" : "0101", "6" : "0110", "7" : "0111", "8" : "1000", "9" : "1001", 
> "a" : "1010", "b" : "1011", "c" : "1100", "d" : "1101", "e" : "1110", 
> "f" : ""
> }
>
 def hexBin(hexchars):
> ... s = ""
> for hexchar in hexchars:
> s += hex2bin[hexchar]
> return s.rstrip("\n")
> ...
 hexBin('a5')
> '10100101'
>
> This however does not work if my argument is '\xa5'.
>
 hexBin('\xa5')
>   File "", line 1, in ?
>   File "", line 5, in hexBin
> ''' exceptions.KeyError : '\xa5' '''


This function is useless in this case because you don't actually have a
string of letters and numbers; you just have raw binary data.

--
-dave
After all, it is not *that* inexpressible.
-H.H. The Dalai Lama



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

--
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.441 / Virus Database: 268.18.2/692 - Release Date: 2007/02/18
04:35 PM
 

-- 
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.441 / Virus Database: 268.18.2/692 - Release Date: 2007/02/18
04:35 PM
 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor