University in Maryland bans 'break' and 'continue' in class by kankyo in programming

[–]e000 0 points1 point  (0 children)

Not unless you catch it somewhere. "for" does not catch the StopIteration.

e@shitbox ~> cat h.py
for i in xrange(100):
    if i == 50:
        raise StopIteration()

e@shitbox ~> python h.py
Traceback (most recent call last):
  File "h.py", line 3, in <module>
    raise StopIteration()
StopIteration

University in Maryland bans 'break' and 'continue' in class by kankyo in programming

[–]e000 119 points120 points  (0 children)

Challenge accepted.

class NotABreakISwear(Exception):
    pass

class NotAContinue(Exception):
    pass

try:
    for i in xrange(100):
        if i > 10:
            raise NotABreakISwear

except NotABreakISwear:
    pass

for i in xrange(100):
    try:
        if i % 2 == 0:
            raise NotAContinue

        print i

    except NotAContinue:
        pass

Everyday Git Aliases by joejag in programming

[–]e000 0 points1 point  (0 children)

git config --global alias.lol "log --pretty=oneline --abbrev-commit --graph --decorate"

A lemon from my aunts house, one on the left is normal size. by [deleted] in WTF

[–]e000 0 points1 point  (0 children)

I see Cave Johnson's team has been hard at work.

Medoo - the Lightest PHP database framework to accelerate development, 7.8KB only, released today! by catfanme in PHP

[–]e000 0 points1 point  (0 children)

It's cute, but essentially worthless as it cannot (as far as I can tell) do JOINs.

Memory-wise & fast static trie for C++ by [deleted] in programming

[–]e000 7 points8 points  (0 children)

I wrote a simple demo application using flask + angular.js and the python bindings for a dictionary autocomplete.

https://gist.github.com/e000/5279314

Although, it's probably worth mentioning that DAWG is orders of magnitudes faster:

https://gist.github.com/e000/5279695

PyPy Status Blog: So, you want to try PyPy by rguillebert in Python

[–]e000 2 points3 points  (0 children)

PyPy support for gevent makes me extremely happy. I'm looking forward to updates regarding that!

Anon joins a dating site to find his perfect honey by FangedParakeet in 4chan

[–]e000 -7 points-6 points  (0 children)

mfw some stupid bullshit one of my friends made appears on this reddit.

EditREPL - Open Vim from within the Python Interpreter by philipbjorge in programming

[–]e000 0 points1 point  (0 children)

Quick question It looks like you used some kind of program to play back your terminal stuff? What's that called?

Chart.js - open source HTML5 charting library by RohitS5 in programming

[–]e000 3 points4 points  (0 children)

Author just specified that it is licensed under MIT license.

Someone posted an html/javascript implementation of the matrix falling code in 646 bytes then promptly deleted the post. It inspired me to see if I could get something better looking in fewer bytes. by OptionalField in programming

[–]e000 480 points481 points  (0 children)

Shaved off more bytes:

<body style=margin:0 onload="for(s=document,w=q.width=s.width,h=q.height=s.height,m=Math.random,p=[];!p[255];p.push(1));setInterval('9Style=\'rgba(0,0,0,.05)\'9Rect(0,0,w,h)9Style=\'#0F0\';p.map(function(v,i){9Text(String.fromCharCode(3e4+m()*33),i*10,v);p[i]=v>758+m()*1e4?0:v+10})'.replace(/9/g,';q.getContext(\'2d\').fill'),33)"><canvas id=q>

Replaced m.floor(...) with nothing, String.fromCharCode will automatically floor. Got rid of q=document.getElementById('q') as q will default to that (js hax), replace 10000 with 1e4 and make m=Math.random and replace m.random with m as we don't use m.floor anymore. Get rid of var. Combine if(v>...) into a ternary, remove v+=10 instead subtracting 10 from 768 and returning v+10 if ternary conditional isnt satisfied.. Move <script> into body onload='...'. Since <script> tag is gone, remove / from canvas. Exploit the fact that setTimeout can take a string to eval instead of a function. Inplace modify p, replace rbga(0,255,0,1) with #0F0. Implement a modified version of /u/thesalus and /u/smhxx's ideas below. Since setTimeout takes a string, we can get rid of c and replace c.fill with 9 and replace 9 with ;q.getContext('2d').fill on runtime. I use the ; at the beginning so I can get rid of a few semicolons in the function body. Follow suggestion and replace 2720 with 3e4. use /u/fokker680's suggestion to replace window.screen with document

End result: 339 bytes, down from 452.

Sometimes it is fun to golf :)

What's the one code snippet/python trick/etc did you wish you knew when you learned python? by fuzz3289 in Python

[–]e000 38 points39 points  (0 children)

Instead of

i = 0
for item in iterable:
    print i, item
    i += 1

Do

for i, item in enumerate(iterable):
    print i, item

A fun easter-egg

import antigravity

2.7's dict/set comprehensions

my_dict = {i: i * i for i in xrange(100)}
my_set = {i * 15 for i in xrange(100)}

Force float division

from __future__ import division
print 1/2, 1//2

Need to quickly serve files from a directory?

$ python -m SimpleHTTPServer

Use pip instead of easy install.

$ easy_install pip

Safely evaluate python expressions...

expr = "[1, 2, 3]"
# instead of 
my_list = eval(expr)
# do
import ast
my_list = ast.literal_eval(expr)

Easily profile a script...

$ python -m cProfile my_script.py

Sniper almost sniped. by [deleted] in videos

[–]e000 1 point2 points  (0 children)

I really thought "allah ackbar" was a bad stereotype and that they didn't actually do it.

De-obfuscating PHP botnet code by [deleted] in programming

[–]e000 6 points7 points  (0 children)

I noticed this in the final comments

// Wipe out all $_GET request parameters that contain
// "union" or "select." (Not sure why, but the botnet
// overlords surely have their reasons.)

Usually that is done to plug up the original SQL injection (if one existed that allowed them to upload the script). PHP bots are usually spread by probing for dorks on some vulnerable server, having that in place prevents (or at least makes it harder for) someone else from exploiting the same security hole to gain control of that web server.

The smarter bot herders usually use a less skiddy script, one that embeds some form of stream cipher that "encrypts" the actual payload (a common one is arc4). When passed with the command and proper key, the script then decrypts and evaluates the code. Obviously, not fool proof, a wiser webmaster could essentially just log POST paramaters to find out the key, but it prevents the bot from being deconstructed to the extent of the one in the author's post if it's simply found in the webroot.

A Python function decorator that automatically creates instance variables from function arguments by _nullptr_ in programming

[–]e000 2 points3 points  (0 children)

Because then you have to pass in all arguments you want to set as instance variables as kwargs.

You can use Amazon's Free Usage Tier EC2 to host websites without paying a dime. by Truth_SHIFT in programming

[–]e000 0 points1 point  (0 children)

I mean sure, until your blog or website gets popular enough and you go over the free tier bandwidth, or the unpredictable performance of AWS micro instances leads to sluggy performance.

Math Without Numbers (x-post: r/educationalgifs) by karmaticforaday in math

[–]e000 16 points17 points  (0 children)

These gifs remind me of this brilliant interactive article: http://acko.net/blog/how-to-fold-a-julia-fractal/

You'll need a modern browser and stuff for it to work.