Re: on implementing a toy oop-system
r...@zedat.fu-berlin.de (Stefan Ram) writes: > Meredith Montgomery writes: >>Is that at all possible somehow? Alternatively, how would you do your >>toy oop-system? > > Maybe something along those lines: > > from functools import partial > > def counter_create( object ): > object[ "n" ]= 0 > def counter_increment( object ): > object[ "n" ]+= 1 > def counter_value( object ): > return object[ "n" ] > > counter_class =( counter_create, counter_increment, counter_value ) > > def inherit_from( class_, target ): > class_[ 0 ]( target ) > for method in class_[ 1: ]: > target[ method.__name__ ]= partial( method, target ) > > car = dict() > > inherit_from( counter_class, car ) > > print( car[ "counter_value" ]() ) > car[ "counter_increment" ]() > print( car[ "counter_value" ]() ) > > . The "create" part is simplified. I just wanted to show how > to make methods like "counter_increment" act on the object > that inherited them using "partial". I really liked this idea. I organized it my way. Have a look. (Thank you for the lecture!) --8<---cut here---start->8--- from functools import partial def Counter(name = None): o = {"name": name if name else "untitled", "n": 0} def inc(o): o["n"] += 1 return o o["inc"] = inc def get(o): return o["n"] o["get"] = get return o def Car(maker): o = {"maker": maker, "state": "off"} inherit_from(Counter, o) def on(o): if o["is_on"](): raise ValueError("oh, no: car is already on") o["inc"]() print(f"{o['maker']}: bruum!") o["state"] = "on" return o o["on"] = partial(on, o) def off(o): if o["is_off"](): raise ValueError("oh, no: car is already off") print(f"{o['maker']}: spat!") o["state"] = "off" return o o["off"] = partial(off, o) def is_on(o): return o["state"] == "on" o["is_on"] = partial(is_on, o) def is_off(o): return o["state"] == "off" o["is_off"] = partial(is_off, o) return o def main(): car1 = Car("Ford") car2 = Car("VW") for i in range(5): car1["on"](); car1["off"]() for i in range(3): car2["on"](); car2["off"]() print(f"car turned on = {car1['get']()} ({car1['maker']})") print(f"car turned on = {car2['get']()} ({car2['maker']})") ## (*) How to inherit the methods from a class ## def inherit_from(C, target): o = C() for k, v in o.items(): if callable(v): target[k] = partial(v, target) else: target[k] = v --8<---cut here---end--->8--- -- https://mail.python.org/mailman/listinfo/python-list
Re: on implementing a toy oop-system
On Sat, 24 Sept 2022 at 07:52, Meredith Montgomery wrote: > > def Counter(name = None): > o = {"name": name if name else "untitled", "n": 0} > def inc(o): > o["n"] += 1 > return o > o["inc"] = inc > def get(o): > return o["n"] > o["get"] = get > return o > Want a neat demo of how classes and closures are practically the same thing? def Counter(name=None): if not name: name = "untitled" n = 0 def inc(): nonlocal n; n += 1 def get(): return n return locals() Aside from using a nonlocal declaration rather than "self.n", this is extremely similar to classes, yet there are no classes involved. A class statement creates a namespace. The locals() function returns the function's namespace. Each call to Counter() creates a new closure context, just like each call to a constructor creates a new object. There's very little difference, at a fundamental level :) ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: How would one scrap property listing website like this?
On Thu, 22 Sep 2022 09:36:47 -0700 (PDT), tripd...@gmail.com wrote: > https://nigeriapropertycentre.com/ > Has anyone scrap something like this before? > probably i should try power bi first to see if it can? You can try something like this. import urllib.request from bs4 import BeautifulSoup URL = "https://nigeriapropertycentre.com/for-rent/lagos"; UA = "Mozilla/5.0 (X11; Linux x86_64; rv:105.0) Gecko/20100101 Firefox/ 105.0" def fetch_url(url): headers = {"User-Agent": UA} req = urllib.request.Request(url, headers={"User-Agent": UA}) resp = urllib.request.urlopen(req) return resp.read().decode("utf-8") html = fetch_url(URL) soup = BeautifulSoup(html, "html.parser") for item in soup.find_all(itemtype="https://schema.org/ListItem";): row = {} row["name"] = item.find(itemprop="name").text row["url"] = item.find(itemprop="url").get("href", "") row["image"] = item.find(itemprop="image").get("src", "") row["content-title"] = item.find(class_="content-title").text row["address"] = item.find("address").text.strip() row["description"] = item.find(itemprop="description").text.strip() row["added-on"] = item.find("span", class_="added-on").text.strip() row["price"] = item.find("span", class_="price").parent.text.strip() row["aux"] = [] for li in item.find("ul", class_="aux-info").find_all("li"): row["aux"].append(li.text.strip()) print(row) -- https://mail.python.org/mailman/listinfo/python-list
Re: on implementing a toy oop-system
Chris Angelico writes: > On Sat, 24 Sept 2022 at 07:52, Meredith Montgomery > wrote: >> >> def Counter(name = None): >> o = {"name": name if name else "untitled", "n": 0} >> def inc(o): >> o["n"] += 1 >> return o >> o["inc"] = inc >> def get(o): >> return o["n"] >> o["get"] = get >> return o >> > > Want a neat demo of how classes and closures are practically the same thing? > > def Counter(name=None): > if not name: name = "untitled" > n = 0 > def inc(): > nonlocal n; n += 1 > def get(): > return n > return locals() > > Aside from using a nonlocal declaration rather than "self.n", this is > extremely similar to classes, yet there are no classes involved. > > A class statement creates a namespace. The locals() function returns > the function's namespace. > > Each call to Counter() creates a new closure context, just like each > call to a constructor creates a new object. > > There's very little difference, at a fundamental level :) I started out this way, but I had to change direction to implement inheritance: the difficulty with closures seems to be lexical scoping, which makes it hard (or impossible) for me to move these closures to another ``object''. For instance, the nonlocal /n/ in /inc/ above is forever bound to that closure; there seems to be no way to make /inc/ update some /n/ in another ``object'', which is needed in my conception of inheritance. I think Python would have to let me duplicate closures. (Thanks for showing me /locals()/.) -- https://mail.python.org/mailman/listinfo/python-list