This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function checkDate(fld) { | |
| var mo, day, yr; | |
| var entry = fld.value; | |
| var reLong = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{4}\b/; | |
| var reShort = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{2}\b/; | |
| var valid = (reLong.test(entry)) || (reShort.test(entry)); | |
| if (valid) { | |
| var delimChar = (entry.indexOf("/") != -1) ? "/" : "-"; | |
| var delim1 = entry.indexOf(delimChar); | |
| var delim2 = entry.lastIndexOf(delimChar); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function checkDate(fld) { | |
| var mo, day, yr; | |
| var entry = fld.value; | |
| var re = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{4}\b/; | |
| if (re.test(entry)) { | |
| var delimChar = (entry.indexOf("/") != -1) ? "/" : "-"; | |
| var delim1 = entry.indexOf(delimChar); | |
| var delim2 = entry.lastIndexOf(delimChar); | |
| mo = parseInt(entry.substring(0, delim1), 10); | |
| day = parseInt(entry.substring(delim1+1, delim2), 10); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MacroCommand: | |
| """A command that executes a list of commands""" | |
| def __init__(self, commands): | |
| self.commands = list(commands) # | |
| def __call__(self): | |
| for command in self.commands: # | |
| command() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> Order(joe, long_order, best_promo) | |
| <Order total: 10.00 due: 9.30> | |
| >>> Order(joe, banana_cart, best_promo) | |
| <Order total: 30.00 due: 28.50> | |
| >>> Order(ann, cart, best_promo) | |
| <Order total: 42.00 due: 39.90> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private void printGuessStatistics(char candidate, int count) { | |
| String number; | |
| String verb; | |
| String pluralModifier; | |
| if (count == 0) { | |
| number = "no"; | |
| verb = "are"; | |
| pluralModifier = "s"; | |
| } else if (count == 1) { | |
| number = "1"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from abc import ABC, abstractmethod | |
| from collections import namedtuple | |
| Customer = namedtuple('Customer', 'name fidelity') | |
| class LineItem: | |
| def __init__(self, product, quantity, price): | |
| self.product = product | |
| self.quantity = quantity | |
| self.price = price | |
| def total(self): | |
| return self.price * self.quantity |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> from collections import namedtuple | |
| >>> LatLong = namedtuple('LatLong', 'lat long') # | |
| >>> Metropolis = namedtuple('Metropolis', 'name cc pop coord') # | |
| >>> metro_areas = [Metropolis(name, cc, pop, LatLong(lat, long)) # | |
| ... for name, cc, pop, (lat, long) in metro_data] | |
| >>> metro_areas[0] | |
| Metropolis(name='Tokyo', cc='JP', pop=36.933, coord=LatLong(lat=35.689722, long=139.691667)) | |
| >>> metro_areas[0].coord.lat # | |
| 35.689722 | |
| >>> from operator import attrgetter |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def clip(text:str, max_len:'int > 0'=80) -> str: | |
| """Return text clipped at the last space before or after max_len | |
| """ | |
| end = None | |
| if len(text) > max_len: | |
| space_before = text.rfind(' ', 0, max_len) | |
| if space_before >= 0: | |
| end = space_before | |
| else: | |
| space_after = text.rfind(' ', max_len) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> json.dump(rec, fp=open('testjson.txt', 'w'), indent=4) | |
| >>> print(open('testjson.txt').read()) | |
| { | |
| "job": [ | |
| "dev", | |
| "mgr" | |
| ], | |
| "name": { | |
| "last": "Smith", | |
| "first": "Bob" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> # Template with substitution targets | |
| >>> reply = """ | |
| Greetings... | |
| Hello %(name)s! | |
| Your age is %(age)s | |
| """ | |
| >>> values = {'name': 'Bob', 'age': 40} # Build up values to substitute | |
| >>> print(reply % values) # Perform substitutions |