In class-examples, February 10, 2020

In [1]:
import json
In [3]:
example_data = {"some": [{"horrible":"0", "terrible": {"nested":["really", "nested", {"thing": 5} ]}},{1:2}]}
In [4]:
example_data
Out[4]:
{'some': [{'horrible': '0',
   'terrible': {'nested': ['really', 'nested', {'thing': 5}]}},
  {1: 2}]}
In [5]:
print(json.dumps(example_data, sort_keys = True, indent = 4))
{
    "some": [
        {
            "horrible": "0",
            "terrible": {
                "nested": [
                    "really",
                    "nested",
                    {
                        "thing": 5
                    }
                ]
            }
        },
        {
            "1": 2
        }
    ]
}
In [6]:
def pretty(data):
    print(json.dumps(data, sort_keys = True, indent = 4))
In [7]:
pretty(example_data["some"])
[
    {
        "horrible": "0",
        "terrible": {
            "nested": [
                "really",
                "nested",
                {
                    "thing": 5
                }
            ]
        }
    },
    {
        "1": 2
    }
]
In [8]:
pretty(example_data["some"][0])
{
    "horrible": "0",
    "terrible": {
        "nested": [
            "really",
            "nested",
            {
                "thing": 5
            }
        ]
    }
}
In [9]:
pretty(example_data["some"][0]["terrible"])
{
    "nested": [
        "really",
        "nested",
        {
            "thing": 5
        }
    ]
}
In [10]:
pretty(example_data["some"][0]["terrible"]["nested"])
[
    "really",
    "nested",
    {
        "thing": 5
    }
]
In [18]:
list_of_cats = [{"a": 5, "name": "biter"}, {"a": 15, "name": "beater"}, {"a": 50, "name": "felix"}, {"a": 500, "name": "garfield"}, {"a": 0.55, "name": "fluff"}]
In [14]:
print(list_of_cats[0])
{'a': 5}
In [15]:
print(list_of_cats[0]["a"])
5
In [19]:
chonkiest = list_of_cats[0]
for chonky_cat in list_of_cats:
    looking_at = chonky_cat["a"]
    if looking_at > chonkiest["a"]:
        chonkiest = chonky_cat
print(chonkiest["name"])
garfield
In [20]:
import datetime
In [21]:
print(datetime.datetime.now())
2020-02-10 20:56:25.638892
In [29]:
class Litigant(object):
    def __init__(self, role, name):
        self.role = role
        self.name = name
In [30]:
class Lawsuit(object):
    def __init__(self, plaintiff, defendant):
        self.case_number = 50
        self.plaintiff = Litigant(role="Plaintiff", name=plaintiff)
        self.defendant = Litigant(role="Defendant", name=defendant)
    
    def announce(self):
        print(self.plaintiff.name + " v. " + self.defendant.name)
        
    
In [31]:
classroom_suit = Lawsuit("Madison", "Innocent Beleagured Professor")
In [32]:
classroom_suit.announce()
Madison v. Innocent Beleagured Professor
In [ ]:
 

links