0

I'm trying to convert my ruby script to python. I'm not very familiar with python so I am getting a TypeError.

printer.rb

Lease = Struct.new(:property, :renter)
lease_list = []

File.open('input.txt').readlines.each do |line|
  p, r = line.split(' - ')
  lease_list << Lease.new(p.tr('#', ''), r)
end

# sort by decimal value
lease_list.sort_by { |m| m.property.scan(/\d+/)[0].to_i }.each do |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')
for 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
4
  • 2
    That's not a syntax error, it's a TypeError. A syntax error shows up as SyntaxError. And yes, there is a significant difference. Commented Jan 21, 2016 at 6:05
  • @AvinashRaj In the sort I am trying to find the decimal value the unit attribute and sort by that Commented Jan 21, 2016 at 6:05
  • 1
    replace lease.unit with lease.property on the last line Commented Jan 21, 2016 at 6:12
  • Now that you've changed the code, the error doesn't make sense Commented Jan 21, 2016 at 6:16

1 Answer 1

3

The problem is here:

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

The str name [edit: see Question edit history], which you generally shouldn't use as a name, even as throwaways) which is assigned values contained in your list and consequently passed to re.split() is an object of type Lease:

lease_list.append(Lease(l,m))

This isn't accepted as an argument to re.split it likes munching on strs. hence the TypeError. Lease has two attributes which are strs after the line.split(' - '):

self.renter = renter
self.property = unit

Use one of these in re.split() (whichever is required for your 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 to mention, sorting a list with list.sort will return None since it sorts the list in place, printing the value here has no use.

Sign up to request clarification or add additional context in comments.

2 Comments

It prints out None
Ah yes, sort sorts in place, returning None. The list you passed in is sorted, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.