Converting Ruby to Python -


i'm trying convert ruby script python. i'm not familiar python getting typeerror.

printer.rb

lease = struct.new(:property, :renter) lease_list = []  file.open('input.txt').readlines.each |line|   p, r = line.split(' - ')   lease_list << lease.new(p.tr('#', ''), r) end  # sort decimal value lease_list.sort_by { |m| m.property.scan(/\d+/)[0].to_i }.each |lease|   puts "\##{lease.property} - #{lease.renter}" end 

printer.py

import re  class lease:   def __init__(self, renter=none, unit=none):     self.renter = renter     self.property = unit  lease_list = [] import sys lines = open('input.txt', 'r') line in lines:     l, m = line.split(' - ')     lease_list.append(lease(l,m)) lines.close()  print lease_list.sort(key=lambda lease: re.split(r"\d+", lease.property)) 

python error

traceback (most recent call last):   file "printer.py", line 16, in <module>     print lease_list.sort(key=lambda str: re.split(r"\d+", str))   file "printer.py", line 16, in <lambda>     print lease_list.sort(key=lambda str: re.split(r"\d+", str))   file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/re.py", line 171, in split     return _compile(pattern, flags).split(string, maxsplit) typeerror: expected string or buffer 

the problem here:

print lease_list.sort(key=lambda str: re.split(r"\d+", str)) 

the str name [edit: see question edit history], which shouldn't use name, throwaways) assigned values contained in list , consequently passed re.split() is object of type lease:

lease_list.append(lease(l,m)) 

this isn't accepted argument re.split likes munching on strs. hence typeerror. lease has 2 attributes strs after line.split(' - '):

self.renter = renter self.property = unit 

use 1 of these in re.split() (whichever required use-case) with:

print lease_list.sort(key=lambda obj: re.split(r"\d+", obj.renter)) 

or:

print lease_list.sort(key=lambda obj: re.split(r"\d+", obj.property)) 

forgot mention, sorting list list.sort return none since sorts list in place, printing value here has no use.


Comments

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -