Porting Ruby to Python - python

I'm porting a piece of Ruby to Python. I've ported the majority of the code except the following. I can't really figure out what it does.
the variable rest would be a string representing a phone number. Its only really the
country_handler = splitter_mapping[i][presumed_cc]
Which I find confusing. I realise I could probably use kwargs in python but I'm not really sure.
Any ideas?
module Phony
class CountryCodes
attr_reader :splitter_mapping
def split_cc rest
presumed_cc = ''
1.upto(3) do |i|
presumed_cc << rest.slice!(0..0)
country_handler = splitter_mapping[i][presumed_cc]
return [country_handler, presumed_cc, rest] if country_handler
end
# This line is never reached as CCs are in prefix code.
end

Well, from the code and Frederick Cheung's comment, I think a translation would go something like this:
def slit_cc(rest):
for i in range(1, 4): # country_code size
presumed_cc = rest[0:i]
country_handler = splitter_mapping[i].get(presumed_cc):
if country_handler:
return [country_handler, presumed_cc, rest]
return None # shouldn't happen
splitter_mapping appears to be a dictionary of dictionaries. The first level keys are country_code sizes (string length), and the second level keys are country codes. The values seem to be a country's telephone dialing prefix.

Related

Perhaps I fundamentally misunderstand indentation in python? - Python

This code gives me an indentation error on checks. I get that this happens often, but the instance is in between two for loops that exist because I need to reference two different lists.
I do not even have the data set made yet, but it should report that the syntax is correct at least. The code is fairly simple. I want to automate package placement in a building and I want to do so by taking the biggest packages and putting them in place with the least amount of room where it would still fit.
All inputs that I used so far are dictionaries because I need to know which shelf I am referring too. I am this close to turning it to lists and being extremely strict about formatting.
inv = maxkey["Inventory"]
is the line where the mistake happens. I do not know how to fix it. Should I use lists for this project instead? Is there a flaw in the logic? Is there a parentheses I forgot? Please let me know if this is just an oversight on my part. Please contact me for further details.
def loadOrder(inProd, units, loc, pref, shelves):
items = len(inProd)
while items > 0
# What is the biggest package in the list?
mxw = 0 # Frontal area trackers
BoxId = {} # Identifies what is being selected
for p in inProd:
if p["Height"]*p["Width"] > mxw:
mxw = p["Width"]*p["Height"]
BoxId = p
else:
pass
# What is the location with the least amount of space?
maxi = 0.001
maxkey = {}
for key in loc:
if key["Volume Efficiency"] > maxi and key["Width"] > mxw/BoxId["Height"]:
maxi = key["Volume Efficiency"]
maxkey = key
else:
pass
maxkey["Inventory"].append(BoxId)
weight = 0
volTot = 0
usedL = 0
inv = maxkey["Inventory"]
for k in inv:
weight = k['Weight']+weight
vol = k['Height']*k['Width']*k['Depth']+volTot
usedL = k['Width']+usedL
maxkey["Volume Efficiency"] = volTot/(maxkey['Height']*maxkey['Weight']*maxkey['Depth'])
maxkey['Width Remaining'] = usedL
maxkey['Capacity Remaining'] = weight
del inProd[BoxId]
items = len(inProd)
return [inProd, units, loc, pref, shelves]
Indentation in a function definition should be like:
def function-name():
<some code>
<return something>
Also, you have missed : after while loop condition.
It shoulde be while items > 0:
And you should not mixing the use of tabs and spaces for indentation.
The standard way for indentation is 4 spaces.
you can see more in PEP 8.

Currency with 3 decimal digits in python

I have the following code which compares the input value that comes as a string with the one the function constracts. Basically checks the if the number the user enters is correct. The function works but only for 2 decimal points but the user is allowed to enter 2 to 4 decimal digits... How can I construct the right value?
def check_currency(self, amount):
amount = amount.replace("+", "")
amount = amount.replace("-", "")
sInput = amount.replace(",", ".")
locale.setlocale(locale.LC_ALL, self.LANG)
result = locale.currency(float(sInput), symbol=False, grouping=True)
if (amount == result):
return True
else:
return False
PS: The self.LANG get the respective value based on the system that the code runs on.
You aren't missing anything. locale.currency formats numbers according to the values in the dict returned by locale.localeconv() and that's what it's supposed to do. Naturally, 'frac_digits' is typically 2 since... well... you know.
Regarding what to do:
First, you can check 'int_frac_digits' in localeconv() - maybe it's good enough for you.
If not, since locale.currency is located in a Python source module, you can rig... I mean, override its logic. Looking at the source, the simplest method appears to be to replace locale.localeconv() with a wrapper.
Be wary though since such a change will be global. If you don't want it to affect other code using locale, alter local copies of the entities (or the entire module) or make a changed entity e.g. require an additional parameter to behave differently.
On a conceptual note: locale is actually correct - for its purpose - in not allowing to alter the representation. Its task is to format information of a few types according to the local convention - so whatever culture you user pertains to, they will see information in the way they are used to. If you alter the format in any way - that wouldn't any longer be "the local convention"! In fact, by requiring 3 decimal digits, you are already making assumptions about the local convention - which may not stand. E.g. in a decent share of currencies, even small sums can numerically be in the thousands.
def check_currency(self, amount):
amount = amount.replace("+", "")
amount = amount.replace("-", "")
sInput = amount.replace(",", ".")
length = len(amount)
decimals = len(sInput[sInput.find(".")+1:])
locale.setlocale(locale.LC_ALL, self.LANG)
if not sInput:
return "Empty Value"
if decimals<=2:
digit = decimals
result = locale.format('%%.%if' % digit, abs(Decimal(sInput)), grouping=True, monetary=True)
if decimals>2:
sInput = amount.replace(".", "")
digit = 0
result = locale.format('%%.%if' % digit, abs(Decimal(sInput)), grouping=True, monetary=True)
if (amount == result):
return True
else:
return False
I changed the function as you see above and it works perfect !! You can check the code above for anyone who might need it :)
Thank you again

Python Printing on the same Line

The problem that I have is printing phone_sorter() and number_calls() all on the same lines. For instance it will print the two lines of phone_sorter but the number_calls will be printed right below it. I have tried the end='' method but it does not seem to work.
customers=open('customers.txt','r')
calls=open('calls.txt.','r')
def main():
print("+--------------+------------------------------+---+---------+--------+")
print("| Phone number | Name | # |Duration | Due |")
print("+--------------+------------------------------+---+---------+--------+")
print(phone_sorter(), number_calls())
def time(x):
m, s = divmod(seconds, x)
h, m = divmod(m, x)
return "%d:%02d:%02d" % (h, m, s)
def phone_sorter():
sorted_no={}
for line in customers:
rows=line.split(";")
sorted_no[rows[1]]=rows[0]
for value in sorted(sorted_no.values()):
for key in sorted_no.keys():
if sorted_no[key] == value:
print(sorted_no[key],key)
def number_calls():
no_calls={}
for line in calls:
rows=line.split(";")
if rows[1] not in no_calls:
no_calls[rows[1]]=1
else:
no_calls[rows[1]]+=1
s={}
s=sorted(no_calls.keys())
for key in s:
print(no_calls[key])
main()
Your key problem is that both phone_sorter and number_calls do their own printing, and return None. So, printing their return values is absurd and should just end with a None None line that makes no sense, after they've done all their own separate-line printing.
A better approach is to restructure them to return, not print, the strings they determine, and only then arrange to print those strings with proper formatting in the "orchestrating" main function.
It looks like they'll each return a list of strings (which they are now printing on separate lines) and you'll likely want to zip those lists if they are in corresponding order, to prepare the printing.
But your code is somewhat opaque, so it's hard to tell if the orders of the two are indeed corresponding. They'd better be, if the final printing is to make sense...
Added: let me exemplify with some slight improvement and one big change in phone_sorter...:
def phone_sorter():
sorted_no={}
for line in customers:
rows=line.split(";")
sorted_no[rows[1]]=rows[0]
sorted_keys = sorted(sorted_no, key=sorted_no.get)
results = [(sorted_no[k], k) for k in sorted_keys]
return results
Got it? Apart from doing the computations better, the core idea is to put together a list and return it -- it's main's job to format and print it appropriately, in concert with a similar list returned by number_calls (which appears to be parallel).
def number_calls():
no_calls=collections.Counter(
line.split(';')[1] for line in calls)
return [no_calls(k) for k in sorted(no_calls)]
Now the relationship between the two lists is not obvious to me, but, assuming they're parallel, main can do e.g:
nc = no_calls()
ps = phone_sorter()
for (duration, name), numcalls in zip(ps, nc):
print(...however you want to format the fields here...)
Those headers you printed in main don't tell me what data should be printed under each, and how the printing should be formatted (width of
each field, for example). But, main, and only main, should be
intimately familiar with these presentation issues and control them, while the other functions deal with the "business logic" of extracting the data appropriately. "Separation of concerns" -- a big issue in programming!

Python: How to speed up creating of objects?

I'm creating objects derived from a rather large txt file. My code is working properly but takes a long time to run. This is because the elements I'm looking for in the first place are not ordered and not (necessarily) unique. For example I am looking for a digit-code that might be used twice in the file but could be in the first and the last row. My idea was to check how often a certain code is used...
counter=collections.Counter([l[3] for l in self.body])
...and then loop through the counter. Advance: if a code is only used once you don't have to iterate over the whole file. However You are stuck with a lot of iterations which makes the process really slow.
So my question really is: how can I improve my code? Another idea of course is to oder the data first. But that could take quite long as well.
The crucial part is this method:
def get_pc(self):
counter=collections.Counter([l[3] for l in self.body])
# This returns something like this {'187':'2', '199':'1',...}
pcode = []
#loop through entries of counter
for k,v in counter.iteritems():
i = 0
#find post code in body
for l in self.body:
if i == v:
break
# find fist appearence of key
if l[3] == k:
#first encounter...
if i == 0:
#...so create object
self.pc = CodeCana(k,l[2])
pcode.append(self.pc)
i += 1
# make attributes
self.pc.attr((l[0],l[1]),l[4])
if v <= 1:
break
return pcode
I hope the code explains the problem sufficiently. If not, let me know and I will expand the provided information.
You are looping over body way too many times. Collapse this into one loop, and track the CodeCana items in a dictionary instead:
def get_pc(self):
pcs = dict()
pcode = []
for l in self.body:
pc = pcs.get(l[3])
if pc is None:
pc = pcs[l[3]] = CodeCana(l[3], l[2])
pcode.append(pc)
pc.attr((l[0],l[1]),l[4])
return pcode
Counting all items first then trying to limit looping over body by that many times while still looping over all the different types of items defeats the purpose somewhat...
You may want to consider giving the various indices in l names. You can use tuple unpacking:
for foo, bar, baz, egg, ham in self.body:
pc = pcs.get(egg)
if pc is None:
pc = pcs[egg] = CodeCana(egg, baz)
pcode.append(pc)
pc.attr((foo, bar), ham)
but building body out of a namedtuple-based class would help in code documentation and debugging even more.

What is the Pythonic way to implement a simple FSM?

Yesterday I had to parse a very simple binary data file - the rule is, look for two bytes in a row that are both 0xAA, then the next byte will be a length byte, then skip 9 bytes and output the given amount of data from there. Repeat to the end of the file.
My solution did work, and was very quick to put together (even though I am a C programmer at heart, I still think it was quicker for me to write this in Python than it would have been in C) - BUT, it is clearly not at all Pythonic and it reads like a C program (and not a very good one at that!)
What would be a better / more Pythonic approach to this? Is a simple FSM like this even still the right choice in Python?
My solution:
#! /usr/bin/python
import sys
f = open(sys.argv[1], "rb")
state = 0
if f:
for byte in f.read():
a = ord(byte)
if state == 0:
if a == 0xAA:
state = 1
elif state == 1:
if a == 0xAA:
state = 2
else:
state = 0
elif state == 2:
count = a;
skip = 9
state = 3
elif state == 3:
skip = skip -1
if skip == 0:
state = 4
elif state == 4:
print "%02x" %a
count = count -1
if count == 0:
state = 0
print "\r\n"
The coolest way I've seen to implement FSMs in Python has to be via generators and coroutines. See this Charming Python post for an example. Eli Bendersky also has an excellent treatment of the subject.
If coroutines aren't familiar territory, David Beazley's A Curious Course on Coroutines and Concurrency is a stellar introduction.
You could give your states constant names instead of using 0, 1, 2, etc. for improved readability.
You could use a dictionary to map (current_state, input) -> (next_state), but that doesn't really let you do any additional processing during the transitions. Unless you include some "transition function" too to do extra processing.
Or you could do a non-FSM approach. I think this will work as long as 0xAA 0xAA only appears when it indicates a "start" (doesn't appear in data).
with open(sys.argv[1], 'rb') as f:
contents = f.read()
for chunk in contents.split('\xaa\xaa')[1:]:
length = ord(chunk[0])
data = chunk[10:10+length]
print data
If it does appear in data, you can instead use string.find('\xaa\xaa', start) to scan through the string, setting the start argument to begin looking where the last data block ended. Repeat until it returns -1.
I am a little apprehensive about telling anyone what's Pythonic, but here goes. First, keep in mind that in python functions are just objects. Transitions can be defined with a dictionary that has the (input, current_state) as the key and the tuple (next_state, action) as the value. Action is just a function that does whatever is necessary to transition from the current state to the next state.
There's a nice looking example of doing this at http://code.activestate.com/recipes/146262-finite-state-machine-fsm. I haven't used it, but from a quick read it seems like it covers everything.
A similar question was asked/answered here a couple of months ago: Python state-machine design. You might find looking at those responses useful as well.
I think your solution looks fine, except you should replace count = count - 1 with count -= 1.
This is one of those times where fancy code-show-offs will come up ways of have dicts mapping states to callables, with a small driver function, but it isn't better, just fancier, and using more obscure language features.
I suggest checking out chapter 4 of Text Processing in Python by David Mertz. He implements a state machine class in Python that is very elegant.
I think the most pythonic way would by like what FogleBird suggested, but mapping from (current state, input) to a function which would handle the processing and transition.
You can use regexps. Something like this code will find the first block of data. Then it's just a case of starting the next search from after the previous match.
find_header = re.compile('\xaa\xaa(.).{9}', re.DOTALL)
m = find_header.search(input_text)
if m:
length = chr(find_header.group(1))
data = input_text[m.end():m.end() + length]

Categories

Resources