Code samples to demonstrate Python iterators and the use of yield statements to create a generator.
Examples based directly on the instruction from Programiz' YouTube Channel
#!/usr/bin/env python3 | |
import requests | |
f=o=i=p="" | |
while not (f and o and i and p): | |
for word in requests.get("https://random-word-api.herokuapp.com/word?lang=en&number=50").json(): | |
if word[0] == "f": | |
f = word | |
if word[0] == "o": | |
o = word | |
if word[0] == "i": |
Started worker and queued sleeper 3 ... 0.00016 | |
running worker for 3 ... 0.00063 | |
Started worker and queued sleeper 9 ... 0.00069 | |
running worker for 9 ... 0.00107 | |
Started worker and queued sleeper 15 ... 0.0011 | |
running worker for 15 ... 0.00145 | |
Started worker and queued sleeper 12 ... 0.00151 | |
running worker for 12 ... 0.0018 | |
Started worker and queued sleeper 6 ... 0.00192 | |
running worker for 6 ... 0.0022 |
global class EmptyUpdateBatch implements Database.Batchable<sObject> { | |
global Database.QueryLocator start(Database.BatchableContext BC) { | |
String query = 'SELECT Id FROM !!ObjectType!! WHERE Field = NULL'; | |
return Database.getQueryLocator(query); | |
} | |
global void execute(Database.BatchableContext BC, List<!!ObjectType!!> records) { | |
update records; | |
} | |
global void finish(Database.BatchableContext BC) {} | |
} |
matthewpoer@magpie sdp % python3 | |
Python 3.9.7 (default, Oct 13 2021, 06:45:31) | |
[Clang 13.0.0 (clang-1300.0.29.3)] on darwin | |
Type "help", "copyright", "credits" or "license" for more information. | |
>>> object_one = {"primary key":"super duper"} | |
>>> object_two = {"primary key":"override", "additional key":"so fun"} | |
>>> object_one.update(object_two) | |
>>> object_one | |
{'primary key': 'override', 'additional key': 'so fun'} |
import time | |
barList = ["|","/","-","\\"] | |
barListIter = iter(barList) | |
ten_seconds_away = int(time.time()) + 10 | |
while int(time.time()) < ten_seconds_away: | |
remaining = ten_seconds_away - int(time.time()) | |
bar = next(barListIter, None) | |
if not bar: | |
barListIter = iter(barList) | |
bar = next(barListIter) |
bugs = { | |
"FirstName": "Bugs", | |
"LastName": "Bunny", | |
"Hometown": { | |
"City": "Los Angeles", | |
"State": "California", | |
"Country": { | |
"ISO": "US", | |
"Text": "United States", | |
}, |
# only for the Mac folks so we can `sed` like a real boy | |
brew install gnu-sed | |
PATH="$(brew --prefix)/opt/gnu-sed/libexec/gnubin:$PATH" | |
# props to Gearset's blog | |
# https://gearset.com/blog/installed-packages-v43-and-activaterss/ | |
cd force-app/main/default/installedPackages | |
sed -i 's/ xsi:nil="true"\/>/>false<\/activateRSS>/g' *xml | |
cd ../../../../ |
>>> dictionary = {"one":"one value", "two":"two value"} | |
>>> new_dictionary = {"foo":"foo value", "bar":"bar value"} | |
>>> dictionary.update(new_dictionary) | |
>>> print(dictionary) | |
{'one': 'one value', 'two': 'two value', 'foo': 'foo value', 'bar': 'bar value'} | |
>>> dictionary = {"one":"one value", "two":"two value"} | |
>>> new_dictionary = {"foo":"foo value", "bar":"bar value"} | |
>>> dictionary.update({**new_dictionary}) | |
>>> print(dictionary) |
class CustomException(Exception): | |
def __init__(self, extra_context, message): | |
self.extra_context = extra_context | |
self.message = message | |
super().__init__(self.message) | |
def __str__(self): | |
return f'Shit going down! {self.message} | extra context: {self.extra_context}' | |
try: | |
raise CustomException("extra context", "some message") |
Code samples to demonstrate Python iterators and the use of yield statements to create a generator.
Examples based directly on the instruction from Programiz' YouTube Channel