473,969 Members | 6,603 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: Efficient Bit addressing in Python.

Ross Ridge wrote:
>This is the code I use to convert large bit arrays to byte strings and
back:

import string
import binascii
import array
8<--------------- examples ----------------------
>I don't think you can do anything faster with standard modules, although
it might not be efficient if you're only working with a single byte.
Thanks I was not aware of binascii module this looks powerful.

- Hendrik

Oct 11 '08 #1
1 2224
Ross Ridge wrote:
>I don't think you can do anything faster with standard modules, although
it might not be efficient if you're only working with a single byte.
Hendrik van Rooyen <ma**@microcorp .co.zawrote:
>Thanks I was not aware of binascii module this looks powerful.
Not really. It's just used as part of a trick to quickly convert a byte
string in to a bit string (a byte string with just 0s and 1s).

Unfortunately fromr you other posts you do seem to be working on
a single byte a time, so my technique probably won't be efficient.
You probably want just want to be using constants and bit masking.
Something like:
PAC_EMERGENCY_S TOP = 0x01 # bit #0 on output port 0
PAC_OUTPUT_0_1 = 0x02 # replace with names for other functions
PAC_OUTPUT_0_2 = 0x04
PAC_PNEUMATIC_P USHER = 0x08 # bit #3 on output port 0
...

def gpio_bit_on(por t, bit):
r = gpio_in(port)
r |= bit
r = gpio_out(port)

def gpio_bit_off(po rt, bit):
r = gpio_in(port)
r &= ~bit
r = gpio_out(port)

class pac(object):
def everything_off( self):
gpio_out(PAC_OU TPUT_PORT_0, 0)
gpio_out(PAC_OU TPUT_PORT_1, 0)
gpio_out(PAC_OU TPUT_PORT_2, 0)
...
gpio_out(PAC_OU TPUT_PORT_7, 0)

def emergency_stop( self):
gpio_bit_on(PAC _OUTPUT_PORT_0, PAC_EMERGENCY_S TOP)

def pusher_on(self) :
gpio_bit_on(PAC _OUTPUT_PORT_0, PAC_PNEUMATIC_P USHER)

def pusher_off(self ):
gpio_bit_off(PA C_OUTPUT_PORT_0 , PAC_PNEUMATIC_P USHER)
Bit twiddling like this is pretty basic.

Ross Ridge

--
l/ // Ross Ridge -- The Great HTMU
[oo][oo] rr****@csclub.u waterloo.ca
-()-/()/ http://www.csclub.uwaterloo.ca/~rridge/
db //
Oct 12 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2414
by: Jakub Fast | last post by:
Hi, Does anybody know how far you can get nowadays with trying to use Python as the script language for XUL instead of JS? Is it possible (even theoretically) to write full-fledged applications on the Mozilla platform with python only? In a parallel vein, anybody know what the architecture of Komodo is? Thanks for any pointers, information, etc
0
2071
by: Stefan Lischke | last post by:
Hi, I'm really desperate using code generation(wsdl.exe) from wsdl files for latest WS-Eventing(including WS-Addressing) Specs. I'm writing my diploma about "publish subscribe systems based on Web Services" I took the WS-Eventing WSDL file and added <binding>'s and <service>'s... Then i took the apache axis wsdl2java tool and i got nice
12
4448
by: s99999999s2003 | last post by:
hi I have a file which is very large eg over 200Mb , and i am going to use python to code a "tail" command to get the last few lines of the file. What is a good algorithm for this type of task in python for very big files? Initially, i thought of reading everything into an array from the file and just get the last few elements (lines) but since it's a very big file, don't think is efficient. thanks
3
2068
by: Doru-Catalin Togea | last post by:
Hi! I am writing some tests and I need to place calls through the modem. Is there an API for addressing the COM ports on my machine, so that I can issue AT-commands to the modem? If this can not be done from Python, I am sure it can be done from C/C++/Java. Any tutorials/examples that you know of? Maybe calling C from Python?
10
3787
by: noro | last post by:
Is there a more efficient method to find a string in a text file then: f=file('somefile') for line in f: if 'string' in line: print 'FOUND' ? BTW:
0
1342
by: gerritmitchell | last post by:
Hi, I have a situation where I need to send a SOAP message from a receiver through multiple intermediaries and then to an ultimate receiver. The intial sender will tell the intermediary where to send the message to next within the service chain. SOAP targeted headers and SOAP roles seem to suppor this quite well. My question is can this be used in conjunction with WS-Addressing? Meaning can I send a WS-Addressing compliant message...
1
4111
by: =?Utf-8?B?dWx0cmFuZXQ=?= | last post by:
We have a client that uses .Net that needs to work against our Java (xfire) based WS. My question is: how can a .Net (C#) WS client be configured to not send WS-Addressing headers? The client in particular mentions having tried WSE 2.0, and WSE 3.0, and says there is no way to turn off WS-Addressing in a WS client, using those frameworks. I find that very hard to believe. There appears to be a problem w/ WS-Addressing headers in the...
5
3180
by: Ross | last post by:
Forgive my newbieness - I want to refer to some variables and indirectly alter them. Not sure if this is as easy in Python as it is in C. Say I have three vars: oats, corn, barley I add them to a list: myList Then I want to past that list around and alter one of those values. That is I want to increment the value of corn:
5
1461
by: Hendrik van Rooyen | last post by:
Is there a canonical way to address the bits in a structure like an array or string or struct? Or alternatively, is there a good way to combine eight ints that represent bits into one of the bytes in some array or string or whatever? It seems to me that there is a dilemma here : if you can write:
0
160
by: Lie Ryan | last post by:
On Fri, 10 Oct 2008 00:30:18 +0200, Hendrik van Rooyen wrote: You'll find that in most cases, using integer or Boolean is enough. There are some edge cases, which requires bit addressing for speed or memory optimizations, in python, the usual response to that kind of optimization requirement is to move that part of the code to C. If, for the more usual case, you require the bit addressing because the data structure is more convenient...
0
10315
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11785
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11368
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11517
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6369
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6506
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
5109
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.