>>> many_numbers = range(1_000_000, 10_000_000)
>>> first, *middle, last = many_numbers[:10_000]
>>> new_numbers = [last, *middle, first]
>>> print(*middle[:4], sep=', ', end='!\n')
1000001, 1000002, 1000003, 1000004!
>>> print(f"{first:,}, {last:,}, and {len(middle)} more")
1,000,000, 1,009,999, and 9998 more
>>> 5 / 2
2.5
class LatLong:
def __init__(self, lat, long):
self.lat, self.long = lat, long
def __eq__(self, other):
if isinstance(other, LatLong):
return (self.lat, self.long) == (other.lat, other.long)
return NotImplemented
def __ne__(self, other):
return not self.__eq__(other)
def __repr__(self):
return "{class_name}(lat={lat}, long={long})".format(
class_name=type(self),
lat=self.lat,
long=self.long,
)
>>> location = LatLong(32.5395, -117.0440)
>>> location
LatLong(lat=32.5395, long=-117.0440)
>>> location2 = LatLong(lat=32.5395, long=-117.0440)
>>> location == location2
True
class LatLong:
def __init__(self, lat, long):
self.lat, self.long = lat, long
def __eq__(self, other):
if isinstance(other, LatLong):
return (self.lat, self.long) == (other.lat, other.long)
return NotImplemented
def __repr__(self):
return "{class_name}(lat={lat}, long={long})".format(
class_name=type(self),
lat=self.lat,
long=self.long,
)
>>> location = LatLong(32.5395, -117.0440)
>>> location
LatLong(lat=32.5395, long=-117.0440)
>>> location2 = LatLong(lat=32.5395, long=-117.0440)
>>> location == location2
True
class LatLong:
def __init__(self, lat, long):
self.lat, self.long = lat, long
def __eq__(self, other):
if isinstance(other, LatLong):
return (self.lat, self.long) == (other.lat, other.long)
return NotImplemented
def __repr__(self):
return f"{type(self)}(lat={self.lat}, long={self.long})"
>>> location = LatLong(32.5395, -117.0440)
>>> location
LatLong(lat=32.5395, long=-117.0440)
>>> location2 = LatLong(lat=32.5395, long=-117.0440)
>>> location == location2
True
class LatLong:
def __init__(self, lat, long):
self.lat, self.long = lat, long
def __eq__(self, other):
if isinstance(other, LatLong):
return (self.lat, self.long) == (other.lat, other.long)
return NotImplemented
def __repr__(self):
return f"{type(self)}(lat={self.lat}, long={self.long})"
from dataclasses import dataclass
@dataclass
class LatLong:
lat: float
long: float
from collections import UserDict
from functools import total_ordering
@total_ordering
class ComparableDict(UserDict):
"""Implement less than and greater than on dictionary."""
def __lt__(self, other):
if isinstance(other, ComparableDict):
return sorted(self.items()) < sorted(other.items())
return super().__lt__(other)
from urllib2 import urlopen
response = urlopen('http://pseudorandom.name')
name = response.read().rstrip()
print " ".join(name)
from __future__ import print_function
try:
from urllib.request import urlopen
except ImportError:
from urllib2 import urlopen
response = urlopen('http://pseudorandom.name')
name = response.read().decode('utf-8').rstrip()
print(" ".join(name))
try:
from urllib.request import urlopen # Python 3
except ImportError:
from urllib2 import urlopen # Python 2
from six.moves.urllib.request import urlopen # six
from future.moves.urllib.request import urlopen # future
from future.standard_library import install_aliases
install_aliases()
from urllib.request import urlopen
modernize
relies on the six
compatibility libraryfuturize
relies on on the future
library2to3
is for sudden upgrades (support 3, drop 2)
class Circle(object):
def __init__(self, radius=1):
self.radius = radius
def __nonzero__(self):
return self.radius != 0
for n in range(3):
circle = Circle(n)
truthiness = "truthy" if circle else "falsey"
print "Circle with radius", circle.radius, "is", truthiness
from __future__ import print_function
from builtins import range
from builtins import object
class Circle(object):
def __init__(self, radius=1):
self.radius = radius
def __bool__(self):
return self.radius != 0
for n in range(3):
circle = Circle(n)
truthiness = "truthy" if circle else "falsey"
print("Circle with radius", circle.radius, "is", truthiness)
import csv
from urllib2 import urlopen
from StringIO import StringIO
def save_tabbed_data_as_csv(url, filename):
response = StringIO(urlopen(url).read())
reader = csv.reader(response, delimiter='\t')
csv_file = open(filename, mode='wb')
csv.writer(csv_file).writerows(reader)
from future import standard_library
standard_library.install_aliases()
import csv
from urllib.request import urlopen
from io import StringIO
def save_tabbed_data_as_csv(url, filename):
response = StringIO(urlopen(url).read())
reader = csv.reader(response, delimiter='\t')
csv_file = open(filename, mode='wb')
csv.writer(csv_file).writerows(reader)
from future import standard_library
standard_library.install_aliases()
import csv
from urllib.request import urlopen
from io import StringIO, open
def save_tabbed_data_as_csv(url, filename):
response = StringIO(urlopen(url).read().decode('utf-8'))
reader = csv.reader(response, delimiter='\t')
csv_file = open(filename, mode='wt', newline='')
csv.writer(csv_file).writerows(reader)
str
for text and bytes
for bytesstr
for both bytes and text2to3
(I've never used it, good luck!)Contact me: trey@truthful.technology