| 1 |
""" |
| 2 |
std library stuff |
| 3 |
""" |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
arr = [''] |
| 9 |
|
| 10 |
|
| 11 |
sorted(arr)[0] |
| 12 |
|
| 13 |
|
| 14 |
next(reversed(arr)) |
| 15 |
next(reversed(arr)) |
| 16 |
|
| 17 |
|
| 18 |
def yielder(): |
| 19 |
yield None |
| 20 |
|
| 21 |
|
| 22 |
next(reversed(yielder())) |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
next(reversed()) |
| 27 |
|
| 28 |
|
| 29 |
next(open('')) |
| 30 |
|
| 31 |
|
| 32 |
{'a':2}.setdefault('a', 3) |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
tuple.__itemsize__ |
| 37 |
|
| 38 |
tuple().__itemsize__ |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
type(1) |
| 45 |
|
| 46 |
type(int()) |
| 47 |
|
| 48 |
type(int) |
| 49 |
|
| 50 |
type(type) |
| 51 |
|
| 52 |
type([]) |
| 53 |
|
| 54 |
def x(): |
| 55 |
yield 1 |
| 56 |
generator = type(x()) |
| 57 |
|
| 58 |
type(x for x in []) |
| 59 |
|
| 60 |
type(lambda: x) |
| 61 |
|
| 62 |
import math |
| 63 |
import os |
| 64 |
|
| 65 |
type(math) |
| 66 |
class X(): pass |
| 67 |
|
| 68 |
type(X) |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
X = type('X', (object,), dict(a=1)) |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
X.a |
| 79 |
|
| 80 |
X |
| 81 |
|
| 82 |
if os.path.isfile(): |
| 83 |
|
| 84 |
fails = os.path.abspath |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
os._T |
| 90 |
|
| 91 |
|
| 92 |
with open('foo') as f: |
| 93 |
for line in f.readlines(): |
| 94 |
|
| 95 |
line |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
for i, j in enumerate(["as", "ad"]): |
| 100 |
|
| 101 |
i |
| 102 |
|
| 103 |
j |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
import re |
| 109 |
c = re.compile(r'a') |
| 110 |
|
| 111 |
|
| 112 |
c.startswith |
| 113 |
|
| 114 |
c.match().start() |
| 115 |
|
| 116 |
|
| 117 |
re.match(r'a', 'a').start() |
| 118 |
|
| 119 |
for a in re.finditer('a', 'a'): |
| 120 |
|
| 121 |
a.start() |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
import weakref |
| 127 |
|
| 128 |
|
| 129 |
weakref.proxy(1) |
| 130 |
|
| 131 |
|
| 132 |
weakref.ref(1) |
| 133 |
|
| 134 |
weakref.ref(1)() |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
import sqlite3 |
| 141 |
|
| 142 |
con = sqlite3.connect() |
| 143 |
|
| 144 |
c = con.cursor() |
| 145 |
|
| 146 |
def huhu(db): |
| 147 |
""" |
| 148 |
:type db: sqlite3.Connection |
| 149 |
:param db: the db connection |
| 150 |
""" |
| 151 |
|
| 152 |
db |
| 153 |
|
| 154 |
with sqlite3.connect() as c: |
| 155 |
|
| 156 |
c |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
import hashlib |
| 163 |
|
| 164 |
|
| 165 |
hashlib.md5 |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
import copy |
| 172 |
|
| 173 |
copy.deepcopy(1) |
| 174 |
|
| 175 |
|
| 176 |
copy.copy() |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
import json |
| 184 |
|
| 185 |
json.load('asdf') |
| 186 |
|
| 187 |
json.loads('[1]') |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
import random |
| 194 |
class A(object): |
| 195 |
def say(self): pass |
| 196 |
class B(object): |
| 197 |
def shout(self): pass |
| 198 |
cls = random.choice([A, B]) |
| 199 |
|
| 200 |
|
| 201 |
cls |
| 202 |
|
| 203 |
cls().s |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
import zipfile |
| 210 |
z = zipfile.ZipFile("foo") |
| 211 |
|
| 212 |
z.read('name').upper |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
from typing import Iterator |
| 219 |
import contextlib |
| 220 |
with contextlib.closing('asd') as string: |
| 221 |
|
| 222 |
string |
| 223 |
|
| 224 |
@contextlib.contextmanager |
| 225 |
def cm1() -> Iterator[float]: |
| 226 |
yield 1 |
| 227 |
with cm1() as x: |
| 228 |
|
| 229 |
x |
| 230 |
|
| 231 |
@contextlib.contextmanager |
| 232 |
def cm2() -> float: |
| 233 |
yield 1 |
| 234 |
with cm2() as x: |
| 235 |
|
| 236 |
x |
| 237 |
|
| 238 |
@contextlib.contextmanager |
| 239 |
def cm3(): |
| 240 |
yield 3 |
| 241 |
with cm3() as x: |
| 242 |
|
| 243 |
x |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
import operator |
| 250 |
|
| 251 |
f = operator.itemgetter(1) |
| 252 |
|
| 253 |
f([1.0]) |
| 254 |
|
| 255 |
f([1, '']) |
| 256 |
|
| 257 |
g = operator.itemgetter(1, 2) |
| 258 |
x1, x2 = g([1, 1.0, '']) |
| 259 |
|
| 260 |
x1 |
| 261 |
|
| 262 |
x2 |
| 263 |
|
| 264 |
x1, x2 = g([1, '']) |
| 265 |
|
| 266 |
x1 |
| 267 |
|
| 268 |
x2 |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
import shlex |
| 276 |
qsplit = shlex.split("foo, ferwerwerw werw werw e") |
| 277 |
for part in qsplit: |
| 278 |
|
| 279 |
part |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
class F(): |
| 286 |
def __init__(self): |
| 287 |
self.my_variable = 3 |
| 288 |
|
| 289 |
@staticmethod |
| 290 |
def my_func(param): |
| 291 |
|
| 292 |
param.my_ |
| 293 |
|
| 294 |
param.uppe |
| 295 |
|
| 296 |
return param |
| 297 |
|
| 298 |
@staticmethod |
| 299 |
def my_func_without_call(param): |
| 300 |
|
| 301 |
param.my_ |
| 302 |
|
| 303 |
param.uppe |
| 304 |
|
| 305 |
return param |
| 306 |
|
| 307 |
@classmethod |
| 308 |
def my_method_without_call(cls, param): |
| 309 |
|
| 310 |
cls.my_variable |
| 311 |
|
| 312 |
cls.my_meth |
| 313 |
|
| 314 |
return param |
| 315 |
|
| 316 |
@classmethod |
| 317 |
def my_method(cls, param): |
| 318 |
|
| 319 |
cls.my_variable |
| 320 |
|
| 321 |
cls.my_meth |
| 322 |
|
| 323 |
return param |
| 324 |
|
| 325 |
|
| 326 |
F.my_func('') |
| 327 |
|
| 328 |
F.my_method('') |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
class Meta(object): |
| 336 |
pass |
| 337 |
|
| 338 |
class Test(metaclass=Meta): |
| 339 |
def test_function(self): |
| 340 |
result = super(Test, self).test_function() |
| 341 |
|
| 342 |
result. |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
import enum |
| 349 |
|
| 350 |
class X(enum.Enum): |
| 351 |
attr_x = 3 |
| 352 |
attr_y = 2.0 |
| 353 |
|
| 354 |
|
| 355 |
X.mro |
| 356 |
|
| 357 |
X.attr_ |
| 358 |
|
| 359 |
X.attr_x.name |
| 360 |
|
| 361 |
X.attr_x.value |
| 362 |
|
| 363 |
X.attr_y.name |
| 364 |
|
| 365 |
X.attr_y.value |
| 366 |
|
| 367 |
X().name |
| 368 |
|
| 369 |
X().attr_x.attr_y.value |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
import functools |
| 375 |
|
| 376 |
basetwo = functools.partial(int, base=2) |
| 377 |
|
| 378 |
basetwo() |
| 379 |
|
| 380 |
def function(a, b): |
| 381 |
return a, b |
| 382 |
a = functools.partial(function, 0) |
| 383 |
|
| 384 |
|
| 385 |
a('')[0] |
| 386 |
|
| 387 |
a('')[1] |
| 388 |
|
| 389 |
kw = functools.partial(function, b=1.0) |
| 390 |
tup = kw(1) |
| 391 |
|
| 392 |
tup[0] |
| 393 |
|
| 394 |
tup[1] |
| 395 |
|
| 396 |
def my_decorator(f): |
| 397 |
@functools.wraps(f) |
| 398 |
def wrapper(*args, **kwds): |
| 399 |
return f(*args, **kwds) |
| 400 |
return wrapper |
| 401 |
|
| 402 |
@my_decorator |
| 403 |
def example(a): |
| 404 |
return a |
| 405 |
|
| 406 |
|
| 407 |
example('') |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
functools.wraps(functools.partial(str, 1))(lambda: 1.0)() |
| 412 |
|
| 413 |
class X: |
| 414 |
def function(self, a, b): |
| 415 |
return a, b |
| 416 |
a = functools.partialmethod(function, 0) |
| 417 |
kw = functools.partialmethod(function, b=1.0) |
| 418 |
just_partial = functools.partial(function, 1, 2.0) |
| 419 |
|
| 420 |
|
| 421 |
X().a('')[0] |
| 422 |
|
| 423 |
X().a('')[1] |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
X.a('')[0] |
| 430 |
|
| 431 |
X.a('')[1] |
| 432 |
|
| 433 |
|
| 434 |
X.a(X(), '')[0] |
| 435 |
|
| 436 |
X.a(X(), '')[1] |
| 437 |
|
| 438 |
tup = X().kw(1) |
| 439 |
|
| 440 |
tup[0] |
| 441 |
|
| 442 |
tup[1] |
| 443 |
|
| 444 |
tup = X.kw(1) |
| 445 |
|
| 446 |
tup[0] |
| 447 |
|
| 448 |
tup[1] |
| 449 |
|
| 450 |
tup = X.kw(X(), 1) |
| 451 |
|
| 452 |
tup[0] |
| 453 |
|
| 454 |
tup[1] |
| 455 |
|
| 456 |
|
| 457 |
X.just_partial('')[0] |
| 458 |
|
| 459 |
X.just_partial('')[1] |
| 460 |
|
| 461 |
X().just_partial('')[0] |
| 462 |
|
| 463 |
X().just_partial('')[1] |
| 464 |
|
| 465 |
@functools.lru_cache |
| 466 |
def x() -> int: ... |
| 467 |
@functools.lru_cache() |
| 468 |
def y() -> float: ... |
| 469 |
@functools.lru_cache(8) |
| 470 |
def z() -> str: ... |
| 471 |
|
| 472 |
|
| 473 |
x() |
| 474 |
|
| 475 |
y() |
| 476 |
|
| 477 |
z() |