473,396 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

putting a string in Mac Address form

I came up with this. Is there a better (more pythonic) way to do it?

import string
def mac_to_norm(mac):
def bad_char(char):
if char not in string.hexdigits:
return False
return True
mac = filter(bad_char,mac)
if len(mac) is not 12: return None
new_mac = ''
c = 0
while len(new_mac) < 16:
new_mac += mac[c:c+2] + ':'
c=c+2
return new_mac[:-1].upper()
print mac_to_norm('0012.ab23.b2cd')
#shows: 00:12:AB:23:B2:CD

The part I think is bad is the while loop part. Could it be better?

Oct 31 '05 #1
1 1825
kyle.tk <ky*****@gmail.com> wrote:
...
new_mac = ''
c = 0
while len(new_mac) < 16:
new_mac += mac[c:c+2] + ':'
c=c+2
return new_mac[:-1].upper() ... The part I think is bad is the while loop part. Could it be better?


What about replacing this whole block with, say...:

return ':'.join(mac[c:c+2] for c in range(0, 12, 2)).upper()

(as you did already check that len(mac) is 12...)
Alex
Oct 31 '05 #2

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

Similar topics

2
by: Kuba Glad | last post by:
Hi, I've spent some time using google, and reading basic manuals about Java Scripts and forms, but I can't find solution to this simple problem. In web page I want a such type of form like...
6
by: Mark | last post by:
Hi, My page has a table with many columns such that the right-side of the table gets chopped off when printed. I specify a table width of 100%, but otherwise no cell dimensions are specified. ...
4
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
12
by: James Norton-Jones | last post by:
Hi, Am I trying to hold the data of a DataGrid in a label so that when the form is reposted the DataGrid can be repopulated. The problem I am having is that I don't understand how to get the...
9
by: sellcraig | last post by:
Microsoft access 2 tables table "data main" contains a field called "code" table "ddw1" is created from a make table query of "data main" Goal- the data in "code" field in needs to...
5
by: Olly | last post by:
Hello Group: I'd be grateful to anyone who can help me with Javascript form. :-) I want users to submit a web form, but to stop the form submission if any of my text fields contain specific...
0
by: =?Utf-8?B?Y2luZHk=?= | last post by:
I know I wrote before a week ago when I knew even less than now but I am getting better please anyone give me a clue or an example. Am I completely off track? I have a datarow in a table with the...
1
by: Xh | last post by:
Hi All! I have two simple XSD files they are used for elements: <patientand <address> as standalone XML schemas - they work perfectly - validated with: http://validate.openlaboratory.net/ ...
7
vikas251074
by: vikas251074 | last post by:
I am getting error above in following code since few days giving tension day and night. How can I solve this? I am facing since Oct.25. in line no. 362 After doing a lot of homework, I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.