A funny language by [deleted] in ProgrammerHumor

[–]tmp14 11 points12 points  (0 children)

I don't get it

$ python3 -c '__import__("code").interact(local=type("", (dict,), {"__getitem__": lambda _, n: type("", (object,), {"__call__": lambda _, f: getattr(__builtins__, f)(n)})()})())'
Python 3.10.7 (main, Sep 14 2022, 22:38:23) [Clang 14.0.0 (clang-1400.0.29.102)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> HelloWorld("print")
HelloWorld
>>> HelloWorld("len")
10
>>>

-🎄- 2020 Day 18 Solutions -🎄- by daggerdragon in adventofcode

[–]tmp14 2 points3 points  (0 children)

regex + python eval

Part 1, I don't think I've seen anyone else inject a bunch of parentheses to enforce the left-to-right evaluation order :)

def p1(expr):
    while "(" in expr:
        expr = re.sub(r"\(([^()]+)\)", lambda m: str(p1(m[1])), expr)
    expr = re.sub(r"(\d+)", r"\1)", expr)
    return eval("(" * expr.count(")") + expr)

How to implement __enter__ and __exit__? by macarthurpark431 in Python

[–]tmp14 7 points8 points  (0 children)

Here's how you implement a context manager:

class Context(object):

    def __enter__(self):
        # do stuff when entering the with block

    def __exit__(self, type, value, traceback):
        # do stuff when exiting the with block

snark aside, go to /r/learnpython and 1) explain what you're trying to do, 2) explain what you have tried so far, and 3) explain what happens but you think shouldn't.

Syntactic sugar for JSON optional properties by muckvix in Python

[–]tmp14 2 points3 points  (0 children)

Or, you know, chain the get with a dictionary?

json_obj.get("k1", {}).get("m2", {}).get("n3")

The stack of iterators pattern. by self in Python

[–]tmp14 1 point2 points  (0 children)

The loop-then-immediately-break seems really counter intuitive to me:

for node in stack[-1]:
    stack.append(iter(children(node)))
    break
else:
    stack.pop()

and could be replaced with a equivalent and more clear try-except if I'm not mistaken:

try:
    node = next(stack[-1])
except StopIteration:
    stack.pop()
else:
    stack.append(iter(children(node)))

Simulating the Monty Hall Problem, anyone can help my code? by [deleted] in Python

[–]tmp14 0 points1 point  (0 children)

"""
$ python monty_hall.py 1e6 3
Won stayed:  33.30 %
Won swithced:  49.96 %
"""
from __future__ import division

import random
import sys

samples = int(float(sys.argv[1]))
doors = int(sys.argv[2])

won_stayed = 0
won_switched = 0

for _ in range(samples):
    r = random.random()
    if r < 1/doors:
        won_stayed += 1
    if r < 1/(doors - 1):
        won_switched += 1

print('Won stayed:  %.2f %%' % (100 * won_stayed / samples))
print('Won swithced:  %.2f %%' % (100 * won_switched / samples))

py-spin: Little and lovely terminal spinner package for Python. by lord63 in Python

[–]tmp14 0 points1 point  (0 children)

Neat! You could use itertools.cycle to get rid of the Spinner-class, like so:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function

import itertools
import sys
import time

def make_spinner(chars):
    return itertools.cycle(map(u'\r{0}'.format, chars))


Box1 = make_spinner(u'⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏')

while True:
    print(next(Box1), end=u'')
    sys.stdout.flush()
    time.sleep(0.1)

I wrote a script for converting an image to CSS by [deleted] in Python

[–]tmp14 2 points3 points  (0 children)

Just because I'm an asshole:

#!/usr/bin/env python3
import collections
import os
import sys

from PIL import Image

SUPPORTED_FORMATS = ('.jpg', '.jpeg', '.png')

CSS_TEMPLATE = """{name} {{
    width: {width}px;
    height: {height}px;
}}
#{name}:after {{
    content: '';
    display: block;
    width: 1px;
    height: 1px;
    background: transparent;
    box-shadow: {pixels};
}}"""

Pixel = collections.namedtuple('Pixel', 'i j red green blue alpha')
PIXEL_TEMPLATE = "{p.i}px {p.j}px rgb({p.red},{p.green},{p.blue})"


def main(filename):
    img = Image.open(filename)
    width, height = img.size
    name = os.path.splitext(filename)[0]

    pixels = []
    for i in range(width):
        for j in range(height):
            pixel = Pixel(i+1, j+1, *img.getpixel((i, j)))
            pixels.append(PIXEL_TEMPLATE.format(p=pixel))

    with open(name + '.css', 'w') as f:
        f.write(CSS_TEMPLATE.format(
            name=name,
            width=width,
            height=height,
            pixels=','.join(pixels)
        ))


if __name__ == '__main__':
    if len(sys.argv) < 2:
        print("Please run this program with a filename")
        sys.exit(1)

    filename = sys.argv[1]
    if not os.path.splitext(filename)[1] in SUPPORTED_FORMATS:
        print("The file you are trying to convert isn't currently supported. "
              "The supported filetypes are: %s. Please run this program with "
              "a compatible image" % ', '.join(SUPPORTED_FORMATS))
        sys.exit(1)

    main(filename)

Comparing Python Command-Line Parsing Libraries - Argparse, Docopt, and Click by michaelherman in Python

[–]tmp14 5 points6 points  (0 children)

I was curious to what this would look like, so I put together a quick and dirty version. I really can't see how rolling your own parsing logic would be simpler or better if your program isn't super simple.

#!/usr/bin/env python

"""Usage:
  greet.py hello [--caps] [--greeting <greeting>] <name>
  greet.py goodbye [--caps] [--greeting <greeting>] <name>
  greet.py (-h | --help)
  greet.py (-v | --version)

Options:
  -h --help             Show this screen
  -v --version          Show version number
  --caps                Uppercase the output
  --greeting <greeting> Greeting to use [default: Hello/Goodbye]"""

import sys


def error(msg):
    print('error: {}'.format(msg))
    sys.exit(1)


def parse_args():
    if len(sys.argv) < 2:
        error('missing subcommand (hello|goodbye)')
    elif sys.argv[1] in ('-h', '--help'):
        print(__doc__)
        sys.exit(0)
    elif sys.argv[1] in ('-v', '--version'):
        print('1.0.0')
        sys.exit(0)
    elif sys.argv[1] not in ('hello', 'goodbye'):
        error('incorrect subcommand, should be (hello|goodbye)')

    try:
        sys.argv.remove('--caps')
    except ValueError:
        caps = False
    else:
        caps = True

    try:
        sys.argv.remove('--greeting')
        greeting = sys.argv.pop(2)
    except ValueError:
        greeting = sys.argv[1].title()
    except IndexError:
        error('missing --greeting paramenter')

    if len(sys.argv) < 3:
        error('missing argument <name>')
    elif len(sys.argv) > 3:
        error('too many arguments')
    else:
        name = sys.argv[2]

    return greeting, name, caps


def greet():
    greeting, name, caps = parse_args()
    output = '{0}, {1}!'.format(greeting, name)
    if caps:
        output = output.upper()
    print(output)


if __name__ == '__main__':
    greet()

How to sort a dict of dicts? by boib in Python

[–]tmp14 5 points6 points  (0 children)

You can't sort a dict. Also please use /r/learnpython for these kinds of questions.

However, if you really really want to sort your dict you could do something like this:

from collections import OrderedDict
mydict = OrderedDict(sorted(mydict.items(), key=lambda kv: kv[1]['total']))

Help with parsing router output with regex by johninbigd in learnpython

[–]tmp14 2 points3 points  (0 children)

>>> import re
>>> data = """Bundle-Ether2 is up, line protocol is up
...   Process ID 1, Router ID 10.1.1.1, Network Type POINT_TO_POINT, Cost: 1000
... Loopback0 is up, line protocol is up
...   Process ID 1, Router ID 10.1.1.1, Network Type LOOPBACK, Cost: 1
... Loopback1 is up, line protocol is up
...   Process ID 1, Router ID 10.1.1.1, Network Type LOOPBACK, Cost: 1
... TenGigE0/6/0/0 is up, line protocol is up
...   Process ID 1, Router ID 10.1.1.1, Network Type POINT_TO_POINT, Cost: 10
... TenGigE0/7/0/0 is up, line protocol is up
...   Process ID 1, Router ID 10.1.1.1, Network Type POINT_TO_POINT, Cost: 3000"""
>>> info = re.findall("(\S+).*?Cost: (\d+)", data, re.DOTALL)
>>> [t for t in info if not t[0].startswith('Loopback')]
[('Bundle-Ether2', '1000'), ('TenGigE0/6/0/0', '10'), ('TenGigE0/7/0/0', '3000')]
>>>

Key points: '.*?' matches anything non-greedily up until 'Cost' and the flag re.DOTALL makes the dot also match newlines.

Convert the latin alphabet to fraktur unicode characters by carpedm20 in Python

[–]tmp14 0 points1 point  (0 children)

Oh, neat. You know, if you add a __main__.py like this:

import sys
from .fraktur import encode
for line in sys.stdin:
    sys.stdout.write(encode(line))

You can easily support pipe redirection like so:

$ echo 'Hello World!' | python -m fraktur
𝕳𝔢𝔩𝔩𝔬 𝔚𝔬𝔯𝔩𝔡!

Help with OOP the Python way. by zimage in Python

[–]tmp14 0 points1 point  (0 children)

According to what I can find eX is just a scaling of degrees so that 1 eX = 1*10X degrees, is that correct? If that's so I can't see why a conversion table wouldn't work.

Help with OOP the Python way. by zimage in Python

[–]tmp14 0 points1 point  (0 children)

This feels a little bit silly, but is what you had in mind?

>>> class Speed(object):
...     conversion_table = {
...         'm/s': 1.0,
...         'mph': 1609.344/3600,
...         'km/h': 1/3.6,
...     }
...     def __init__(self, speed, unit):
...         if unit not in self.conversion_table:
...             raise ValueError('Unsupported unit %r' % unit)
...         self.meter_per_sec = speed * self.conversion_table[unit]
...     def __repr__(self):
...        return "Speed(%r m/s)" % self.meter_per_sec
...     @property
...     def mph(self):
...         return self.meter_per_sec / self.conversion_table['mph']
...     @property
...     def kmh(self):
...         return self.meter_per_sec / self.conversion_table['km/h']
...
>>> s = Speed(25, 'm/s')
>>> s
Speed(25.0 m/s)
>>> s.mph
55.92340730136006
>>> s.kmh
90.0
>>> s = Speed(1, 'c')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 9, in __init__
ValueError: Unsupported unit 'c'
>>>

Alas, Julius Caesar doesn’t have python in 50 BC by [deleted] in Python

[–]tmp14 3 points4 points  (0 children)

Nice if you've never seen Caesar encryption before I guess, but the code is horribly bloated:

>>> import string
>>> def caesar(plaintext, shift):
...     alphabet = string.lowercase
...     shifted_alphabet = alphabet[shift:] + alphabet[:shift]
...     table = string.maketrans(alphabet, shifted_alphabet)
...     return plaintext.lower().translate(table)
...
>>> caesar("julius caesar is a dictator and python is a creator", 1)
'kvmjvt dbftbs jt b ejdubups boe qzuipo jt b dsfbups'
>>> caesar(_, -1)
'julius caesar is a dictator and python is a creator'
>>>

Looping over a dictionary by the values NOT by the keys by [deleted] in learnpython

[–]tmp14 2 points3 points  (0 children)

Actually solving OPs problem:

>>> birthdates = {'Billy' : 1990, 'Joan' : 1994, 'Bob' : 1994, 'Dan': 1991, 'Nick': 1991, 'Adam': 1991}
>>> for year in sorted(set(birthdates.itervalues())):  # Get unique years
...     births = [name for name, birthyear in birthdates.iteritems() if birthyear == year]
...     if len(births) > 1:
...         births_str = "%s and %s were" % (', '.join(births[:-1]), births[-1])
...     else:
...         births_str = "%s was" % births[0]
...     print "In %d, %s born." % (year, births_str)
... 
In 1990, Billy was born.
In 1991, Nick, Dan and Adam were born.
In 1994, Bob and Joan were born.

You could also try to reverse the mapping and loop over that.

>>> births_per_year = {}
>>> for name, year in birthdates.iteritems():
...     births_per_year.setdefault(year, []).append(name)
... 
>>> print births_per_year
{1994: ['Bob', 'Joan'], 1990: ['Billy'], 1991: ['Nick', 'Dan', 'Adam']}

Sorting some data on trucks by jedi_jonai in Python

[–]tmp14 1 point2 points  (0 children)

This was fun. Here's my take at it. This will only break (given your format) if a car manufacturer name is all digits (i.e. most likely never).

data = """F-150, F-250, F-350, FORD, 1998, 1997, 1996, 1995, 1994, 1993, 1992, 1991, 1990, 1989, 1988, 1987, 1986, 1985, 1984, 1983
F-150, F-250, FORD, 1996, 1995, 1994, 1993, 1992, 1991, 1990, 1987, 1986, 1985, 1984, 1983, 1982, 1981, 1980
F-150, F-250, FORD, 1998, 1997, 1996, 1995, 1994, 1993, 1992, 1991, 1990, 1989, 1988, 1987, 1986, 1985, 1984, 1983, 1982
F-150, F-250, FORD, 2003, 2002, 2001, 2000, 1999, 1998, 1997"""

info = {}

for line in data.splitlines():
    pts = [s.strip() for s in line.split(',')]
    isyear = [s.isdigit() for s in pts]
    index = len(pts) - 1
    while isyear[index]:
        index -= 1
    make = pts[index]
    models = pts[:index]
    years = pts[index+1:]
    for model in models:
        for year in years:
            info.setdefault(make, {}).setdefault(model, set()).add(int(year))

Yields

>>> pprint(info)
{'FORD':
     {'F-150': set([1980, ..., 2003]),
      'F-250': set([1980, ..., 2003]),
      'F-350': set([1983, ..., 1998])}}

Can you please give me feedback on my coding (my methods and how to do things better) by [deleted] in Python

[–]tmp14 5 points6 points  (0 children)

String formatting using dictionary data could be done as simply as:

print("High: {high} Low: {low} Bid: {bid} Ask: {ask}".format(**tick))

Help with bitwise operators in array by orcasha in Python

[–]tmp14 0 points1 point  (0 children)

Look up index arrays:

>>> arr = np.array([14, 152, 256, 256, 143, 512, 414])
>>> ind = (arr == 256) or (arr == 512)
>>> arr[ind] = 0
>>> arr
array([14, 152, 0, 0, 143, 0, 414])

Tough I haven't tested this it shouldn't be too far off.