Skip to content

Instantly share code, notes, and snippets.

@fmasanori
Last active October 30, 2022 00:00
Show Gist options
  • Save fmasanori/1288160dad16cc473a53 to your computer and use it in GitHub Desktop.
Save fmasanori/1288160dad16cc473a53 to your computer and use it in GitHub Desktop.
World Cup in six lines of Python 3. Jogos da Copa do Mundo em cinco linhas de Python 3.
import requests
jogos = requests.get('http://worldcup.sfg.io/matches').json()
for jogo in jogos:
if jogo['status'] in ('completed', 'in progress'):
print (jogo['home_team']['country'], jogo['home_team']['goals'], 'x',
jogo['away_team']['country'], jogo['away_team']['goals'])
@Duta
Copy link

Duta commented Jun 19, 2014

@dhagrow Nice!

@beallio
Copy link

beallio commented Jun 19, 2014

@cagdassalur
Copy link

1-liner in python2.7 if you don't count imports. Mind the ugliness.

import urllib2, json
print '\n'.join([' '.join((jogo['home_team']['country'], str(jogo['home_team']['goals']), 'x', jogo['away_team']['country'], str(jogo['away_team']['goals']))) for jogo in json.loads(urllib2.urlopen('http://worldcup.sfg.io/matches').read().decode('utf-8')) if jogo['status'] == 'completed'])

@seanmckaybeck
Copy link

I like the competition that has started over fewest lines of code

@sametmax
Copy link

@ThaWeatherman

Always. If you post a "x lines" titled post, people will try to come up with a shorter version. Wait for the ruby samples to come :)

But let's all remember that in Python, readability is more important than concision. If you want to keep it small, readable and PEP8, you should not try to compress more than :

import json
import urllib.request as req
res = req.urlopen('http://worldcup.sfg.io/matches').read().decode('utf-8')
for m in filter(lambda d: d['status'] == 'completed', json.loads(res)):
   template = "{h[country]} {h[goals]} x {a[country]} {a[goals]}"
   print(template.format(h=m['home_team'], a=m['away_team']))

Note the 80 char limits. More lines, but way more readable.

You can always make any Python code fits on one line, either with exec() + and or ;. But what's the point ?

And with requests :

import requests
r = requests.get('http://worldcup.sfg.io/matches').json()
m = ((m['home_team'], m['away_team']) for m in r if m['status'] == 'completed')
for h, a in m: 
   print(h['country'], h['goals'], 'x', a['country'], a['goals'])

And even like that, it's rather add some lines to have decent variables names.

Otherwise, it's not Python code, anymore, it's perl.

@XenGi
Copy link

XenGi commented Jun 20, 2014

Here's my take on the world cup:

if worldcup:
    pass

But nice code anyways. ;)

@kennyledet
Copy link

^ Lmao

@amferraz
Copy link

World cup in six lines. Wait, six lines? Brazil confirmed!

@fmasanori
Copy link
Author

@XenGi cool code

@fmasanori
Copy link
Author

@dhagrow Marvin hitchhiker's guide to the galaxy "I was bored :/"

@natorsc
Copy link

natorsc commented Jun 21, 2014

alunos do Python para zumbis na área akkakakaka.


from urllib import request
import json

resp = request.urlopen('http://worldcup.sfg.io/matches').read()
for jogo in json.loads(resp.decode('utf-8')):
if jogo['status'] == 'completed':
hora = jogo['datetime']
print (jogo['location'],(hora[0:10], hora[11:16]))
print ("Vencedor:", jogo['winner'])
print (jogo['home_team']['country'], jogo['home_team']['goals'], 'x', jogo['away_team']['country'], jogo['away_team']['goals'],"\n")

sair = str(input("Precione ENTER para sair"))

@willmendesneto
Copy link

Awesome!

@GuilhermePython
Copy link

No meu Python so rodou assim (dei um espaço na terceira linha):

import urllib.request
import json
resp = urllib.request.urlopen ('http://worldcup.sfg.io/matches').read()
for jogo in json.loads(resp.decode('utf-8')):
if jogo['status'] == 'completed':
print (jogo['home_team']['country'], jogo['home_team']['goals'], 'x', jogo['away_team']['country'], jogo['away_team']['goals'])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment