0

Here is an example code of what I am trying to do. My end goal is creating a JSON object for class real estate agent, but I am struggling because I don't know what to do about self.housesSold.

class realEstateAgent: 
     def __init__(self, name, salary, houses):
            self.name = name
            self.sal = salary
            self.housesSold = #a list of **HOUSE** objects


class houseSold:
     def __init__(openPrice, closePrice, closeDate):
            self.openPrice = openPrice
            self.closePrice = closePrice
            self.closeDate = closeDate
martineau
  • 119,623
  • 25
  • 170
  • 301
Noah L
  • 1
  • What is the problem you're running into? – Frank Yellin May 02 '22 at 14:37
  • Did you read https://docs.python.org/3/library/pickle.html#what-can-be-pickled-and-unpickled ? As long as the HOUSE objects are picklable you should be fine. – Samwise May 02 '22 at 14:37
  • Hey, thanks so much for responding. My issue is that python is returning something like this... 'name': John Smith, 'sal' = 30000, housesSold = <__main__.House object at 0x1225e2e80> ... I would like for instead of the reference to the house object, that the actual house object appear. Here is the code I am using ``` obj = realEstateAgent(name, salary, houses) f = open('data.pickle', 'wb') pickle.dump(obj, f) f.close f = open('data.pickle', 'rb') data = pickle.load(f) f.close() print(data.__dict__)``` @Samwise – Noah L May 02 '22 at 14:40
  • 2
    Your problem has nothing to do with pickling. Take pickling out of the equation and just print your `obj` before it's pickled and it'll look the same. You need to implement a `__str__` method if you want to change the way your object looks when it's printed. – Samwise May 02 '22 at 15:04
  • Just to make @Samwise's point clear: Your classes are already fully picklable. Everything is working exactly as intended, you just haven't implemented a reasonable `__repr__` (used when echoing the object in an interactive interpreter, or when displaying it as the contents of a container) and/or `__str__` (used when object `print`ed or `str`ingified directly) for the contained class. Implementing a reasonable `__repr__` is a good idea for any class (if you use `dataclasses`, it'll be made for you). – ShadowRanger May 02 '22 at 15:09
  • 1
    @NoahL if any of this is confusing, please **edit your question** to be runnable code (show how you construct the `houses` list, for example), and explain what output you want to get. Having to speculate about the code you're running and the output you're looking for makes a definitive answer (i.e. one including a fixed version of your code) impossible, which is why the comments are in the form of guesses and general pointers. Clearer questions get clearer answers. – Samwise May 02 '22 at 15:16
  • See [Making object JSON serializable with regular encoder](https://stackoverflow.com/questions/18478287/making-object-json-serializable-with-regular-encoder). – martineau May 02 '22 at 15:39
  • I also **strongly** suggest following [PEP 8 - Style Guide for Python Code](https://www.python.org/dev/peps/pep-0008/), especial wrt [Naming Conventions](https://www.python.org/dev/peps/pep-0008/#naming-conventions), where function and variable names should be in `snake_case` and only class names in `CapitalCase`. It also suggests using a consistent indentation of 4 space characters. – martineau May 02 '22 at 15:43

1 Answers1

0

You mention that housesSold is a list. In your last comment, it appears that it's not the case and it is actually an object. However, in both cases, printing the object (or the list of objects) will not get you anywhere unless you edit the __str__ or __repr__ method of the corresponding class. Instead you should try to get the attributes of that object (because you care mainly about them).

For example if housesSold is an object, then try this:

housesSold = data.housesSold
print(housesSold.openPrice) #get the price

If it's a list, then try this :

housesSold = data.housesSold
print(housesSold[0].openPrice) #get the price of the first house
martineau
  • 119,623
  • 25
  • 170
  • 301
ah_onaly
  • 53
  • 6