Перейти к основному содержимому

30 записей с тегом "python"

Посмотреть все теги

# Each YouTube video has four generated images. They are predictably formatted as follows:

f"https://img.youtube.com/vi/{your_youtube_video_id}/0.jpg"
f"https://img.youtube.com/vi/{your_youtube_video_id}/1.jpg"
f"https://img.youtube.com/vi/{your_youtube_video_id}/2.jpg"
f"https://img.youtube.com/vi/{your_youtube_video_id}/3.jpg"

# The first one in the list is a full size image and others are thumbnail images. The default thumbnail image (i.e., one of 1.jpg, 2.jpg, 3.jpg) is:
f"https://img.youtube.com/vi/{your_youtube_video_id}/default.jpg"

# For the high quality version of the thumbnail use a URL similar to this:
f"https://img.youtube.com/vi/{your_youtube_video_id}/hqdefault.jpg"

# There is also a medium quality version of the thumbnail, using a URL similar to the HQ:
f"https://img.youtube.com/vi/{your_youtube_video_id}/mqdefault.jpg"

# For the standard definition version of the thumbnail, use a URL similar to this:
f"https://img.youtube.com/vi/{your_youtube_video_id}/sddefault.jpg"

# For the maximum resolution version of the thumbnail use a URL similar to this:
f"https://img.youtube.com/vi/{your_youtube_video_id}/maxresdefault.jpg"

# All of the above URLs are available over HTTP too. Additionally, the slightly shorter hostname i3.ytimg.com works in place of img.youtube.com in the example URLs above.

# Alternatively, you can use the YouTube Data API (v3) to get thumbnail images.

# Use the type() builtin function:
i = 123
type(i) # <type 'int'>
type(i) is int # True

i = 123.456
type(i) # <type 'float'>
type(i) is float # True

# To check if a variable is of a given type, use isinstance:
i = 123
isinstance(i, int) # True
isinstance(i, (float, str, set, dict)) # False

a[start:stop]  # items start through stop-1
a[start:] # items start through the rest of the array
a[:stop] # items from the beginning through stop-1
a[:] # a copy of the whole array

Внутри цикла while используется оператор присваивания :=, который появился в Python 3.8. Он читает очередную строку из файла с помощью метода readline() и присваивает ее переменной line.

with open(filename) as file:
while line := file.readline():
print(line.rstrip())

import requests

url = 'http://maps.googleapis.com/maps/api/directions/json'

params = dict(
origin='Chicago,IL',
destination='Los+Angeles,CA',
waypoints='Joplin,MO|Oklahoma+City,OK',
sensor='false'
)

resp = requests.get(url=url, params=params)
data = resp.json() # Check the JSON Response Content documentation below

# JSON Response Content: https://requests.readthedocs.io/en/master/user/quickstart/#json-response-content

# How to use a dot "." to access members of dictionary?

class dotdict(dict):
"""dot.notation access to dictionary attributes"""
__getattr__ = dict.get
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__

mydict = {'val':'it works'}
nested_dict = {'val':'nested works too'}
mydict = dotdict(mydict)
mydict.val
# 'it works'

mydict.nested = dotdict(nested_dict)
mydict.nested.val
# 'nested works too'