[Tutor] Convert string to bytes
Hi all, I need help on this problem. I have one message packet in form of string s = '0xFE, 0x01, 0x01, 0x22, 0xFE, 0x02'. I have to send this data to MSP430 microcontroller, but it is not taking data if it is string. If I am passing this as hardcoded value s1 = 0xFE, 0x01, 0x01, 0x22, 0xFE, 0x02 then board is responding. I want to convert s as s1 using python. Please help me out to convert string in normal format for microcontroller to respond. Thanks in advance. Regards, Shweta ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Help on Python drop-down list options
Hello All, I need help on the following: I have a created a project from a csv file to calculate the mean and standard deviation. However, I would like to create a drop-down list and display the mean and standard deviation? Is there a module for that? Thank you, Tammy ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help on Python drop-down list options
What is the user interface that your program is using, currently? IE: QT, GTK, Tkinter, Curses, Kivy, Pygame, Or None? What is the target system on which your program runs? How are you currently viewing the mean and standard deviation results? What version of Python are you using and what is your OS? On 12/31/2014 08:49 AM, Tammy Miller wrote: Hello All, I need help on the following: I have a created a project from a csv file to calculate the mean and standard deviation. However, I would like to create a drop-down list and display the mean and standard deviation? Is there a module for that? Thank you, Tammy ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Convert string to bytes
I wrote a program to help me break out hex strings awhile ago. It was written to communicate with a Atmega 168. This is written for Python 3. Here is a snippet, see if this helps you. s4 = 28 40 7A 7C 05 00 00 34 hex_array = bytearray.fromhex(s4) print(s4) print(list(hex_array)) print(hex_array) for byte in list(hex_array): print(hex(byte)) print(bytes([byte])) On 12/31/2014 05:08 AM, shweta kaushik wrote: Hi all, I need help on this problem. I have one message packet in form of string s = '0xFE, 0x01, 0x01, 0x22, 0xFE, 0x02'. I have to send this data to MSP430 microcontroller, but it is not taking data if it is string. If I am passing this as hardcoded value s1 = 0xFE, 0x01, 0x01, 0x22, 0xFE, 0x02 then board is responding. I want to convert s as s1 using python. Please help me out to convert string in normal format for microcontroller to respond. Thanks in advance. Regards, Shweta ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help on Python drop-down list options
On 31/12/14 13:49, Tammy Miller wrote: I need help on the following: I have a created a project from a csv file to calculate the mean and standard deviation. I assume that means you read the data from the CSV file and display the stats? However, I would like to create a drop-down list and display the mean and standard deviation? I assume you mean you want to create a GUI that has a drop down list containing data and you want to display the stats based on the list contents? If so you need to decide what kind of UI you want to use. Your choices are: 1) CLI using curses (Not on windows) 2) Web UI based on HTML/Javascript 3) Desktop GUI using Tkinter/WxPython/PyGTK (or some other toolkit) And once you decide your option you need to design what the UI looks like - how does the output appear? Is it in a label? a text widget? a pop-up dialog? Is there a module for that? Yes, for all of the above. Option 1 uses the curses module Option 2 uses standard HTML linked to a CGI(standard library) or a web framework(several third-party options) Option 3: the standard library includes Tkinter, the others are third-party downloads. We can't help much until you make the choices above. It will help if you tell us which python version and which OS you use. And once you choose an option which tookkit you want to go with (or at least which option and we can advise on toolkits based on your experience and your UI visual design) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Convert string to bytes
On 31/12/14 10:08, shweta kaushik wrote: I have one message packet in form of string s = '0xFE, 0x01, 0x01, 0x22, 0xFE, 0x02'. I have to send this data to MSP430 microcontroller, but it is not taking data if it is string. If I am passing this as hardcoded value s1 = 0xFE, 0x01, 0x01, 0x22, 0xFE, 0x02 then board is responding. I want to convert s as s1 using python. I'm pretty sure you don;t need that, you only need the integer values of the hex strings. You can then write those integers directly to your controller. Assuming I'm right this should work (Python v2.7): s = '0xFE, 0x01, 0x01, 0x22, 0xFE, 0x02' [int(h,16) for h in s.split(',')] [254, 1, 1, 34, 254, 2] HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Convert string to bytes
On 31/12/14 18:03, shweta kaushik wrote: I also did the same thing and its working... but i dint use this. I just did this and it is similar to what you said. s = '0xFE, 0x01, 0x01, 0x22, 0xFE, 0x02' packet = eval(s) (254, 1, 1, 34, 254, 2) this is tuple, which my microcontroller is able to recognize and respond back. It works with a fixed string input but using eval() is a big security risk, especially if you read the input from a file or network or even a user. You could potentially find your hard disk being formatted or similar damage. An explicit conversion using int() is much safer. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Convert string to bytes
Hi Alan, Thank you.. I also did the same thing and its working... but i dint use this. I just did this and it is similar to what you said. s = '0xFE, 0x01, 0x01, 0x22, 0xFE, 0x02' packet = eval(s) (254, 1, 1, 34, 254, 2) this is tuple, which my microcontroller is able to recognize and respond back. Regards, Shweta On Wed, Dec 31, 2014 at 11:27 PM, Alan Gauld alan.ga...@btinternet.com wrote: On 31/12/14 10:08, shweta kaushik wrote: I have one message packet in form of string s = '0xFE, 0x01, 0x01, 0x22, 0xFE, 0x02'. I have to send this data to MSP430 microcontroller, but it is not taking data if it is string. If I am passing this as hardcoded value s1 = 0xFE, 0x01, 0x01, 0x22, 0xFE, 0x02 then board is responding. I want to convert s as s1 using python. I'm pretty sure you don;t need that, you only need the integer values of the hex strings. You can then write those integers directly to your controller. Assuming I'm right this should work (Python v2.7): s = '0xFE, 0x01, 0x01, 0x22, 0xFE, 0x02' [int(h,16) for h in s.split(',')] [254, 1, 1, 34, 254, 2] HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Making Doubly Linked List with Less Lines of Code.
On Tue, Dec 30, 2014 at 05:40:04PM -0500, wolfrage8...@gmail.com wrote: On Tue, Dec 30, 2014 at 2:37 PM, Danny Yoo d...@hashcollision.org wrote: If that's the case, then none of this requires linked list storage. Instead, we can represent this as a list of rows. Each row element would be itself a list of tiles. In short, a matrix. See: https://docs.python.org/2/faq/programming.html#how-do-i-create-a-multidimensional-list True, I could use a multidimensional list. And originally I was using a 2D list. But I wanted the ability to QUICKLY search across a row or up and down a column and so the added benefit of the linked list was that it simply requires me to access the next node reference. [emphasis added] Trust me on this, there is no linked list code you can write in Python that will be faster than using a list of lists. Even in C, traversing a linked list is slower than array access, and Python is not C. And finding neighboring tiles also wouldn't be too bad: Given a tile at (i, j), we can find its neighbors through arithmetic (i plus or minus one, j plus or minus one). From a coding perspective the linked list seemed simplier, because my other 2D list implementation required me to have a function that could map from any position to North, South, East, West, plus it needed to perform bounds checking. Of course having the list now be doubly linked has added complexity. Bounds checking is easy: cell [i, j] is in bounds if this is true: (0 = i NUM_ROWS) and (0 = j NUM_COLS) Fast access to any cell is possible: array[i][j] gives you access to the cell [i, j] effectively instantly, two array accesses, which is much, much faster than having to traverse a linked list: cell = array for x in range(i): cell = cell.next_row for x in range(j): cell = cell.next_col (or whatever scheme you use for linking to the next row/column). As far as memory consumption goes, I don't think there is any comparison. Here is a doubly-linked list of strings: class Cell: __slots__ = ['data', 'next', 'prev'] def __init__(self, data): self.data = data self.next = self.prev = None x = y = Cell('alpha0') for i in range(1, 10): y.next = Cell('alpha' + str(i)) y.next.prev = y y = y.next Now let's see how much memory it uses: from sys import getsizeof size = 0 y = x while y is not None: size += getsizeof(y) + getsizeof(y.data) y = y.next On my system, that gives size of 630 bytes. Let's do the same for a list. This only takes two lines of code: x = ['alpha' + str(i) for i in range(0, 10)] size = getsizeof(x) + sum(getsizeof(s) for s in x) On my system, that gives a size of 406 bytes, considerably smaller. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Making Doubly Linked List with Less Lines of Code.
On Tue, Dec 30, 2014 at 09:41:33PM -0800, Alex Kleider wrote: In the process of doing so I discovered that list multiplication does not at all behave the way I expected (as demonstrated by the 'bad_flip_2_D' function.) Well, don't keep us in suspense. How did you expect it to behave, and how does it actually behave? My guess is that you expected this: py grid = [[0]*5]*4 # make a 4x5 grid py print(grid) [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] Looks good so far! But: py grid[1][1] = 99 # Adjust a single cell. py print(grid) [[0, 99, 0, 0, 0], [0, 99, 0, 0, 0], [0, 99, 0, 0, 0], [0, 99, 0, 0, 0]] while you expected: [[0, 0, 0, 0, 0], [0, 99, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] Am I right? The problem is that you guessed that list multiplication *copies* the items. It does not. It just repeats them. So: [a, b]*3 returns [a, b, a, b, a, b] but NOT [copy(a), copy(b), copy(a), copy(b), copy(a), copy(b)] If the list items are immutable, like ints or strings, the difference doesn't matter. You can't modify immutable objects in-place, so you never notice any difference between copying them or not. Aside: the copy module actually implements copying for some immutable objects by returning the original! py import copy py x = 2.3456 py y = copy.copy(x) py y is x True py x = [] py y = copy.copy(x) py y is x False Floats are immutable and cannot be modified, so there is nothing you can do to change float x. Making an actual copy is a waste of time, so copy.copy just returns the same float object. But lists are mutable and can be modified, so copy.copy has to actually build a new list. But I digress. Back to list multiplication. Since list multiplication doesn't *copy* the items, it just repeats them, we can see what is happening if we expand the code a little with a temporary variable: Before: grid = [[0]*5]*4 # make a 4x5 grid grid[1][1] = 99 # Adjust a single cell. Expanded: x = [0]*5 # Make a single list of five immutable integers. grid = [x]*4 # Like [x, x, x, x] So here we have a list of four references to the same list, not four different lists! If you change any one of those references, or indeed the original x reference *in-place*, they all change. They all change because in fact there is no all, there is only one! grid[1][1] = 99 print(x) - prints [0, 99, 0, 0, 0] The solution to this is not to use list multiplication unless you know the items are immutable. Instead, a list comprehension solves the problem nicely: py grid = [[0]*5 for i in range(4)] py grid[1][1] = 99 py print(grid) [[0, 0, 0, 0, 0], [0, 99, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor