
Tweetable Python – Tweetable Python 3.7 Documentation - ScottWRobinson
https://books.agiliq.com/projects/tweetable-python/en/latest/
======
deathanatos
> _In less than a 100 chars, you can generate truly random passwords._

The default RNG is not a CSPRNG:

> _Python uses the Mersenne Twister as the core generator […] and [that
> generator] is completely unsuitable for cryptographic purposes._

These aren't "truly random passwords". Don't do this, use the generator
included in a more … complete? mainstream? keyring program like LastPass. Or
use a generator usable for cryptographic purposes.

Also, we get to see all the ways a language causes people to write code that
just completely fails on any non-ASCII input:

> _leftpad_

With all the classic leftpad bugs:

    
    
      In [4]: for s in ('asdf', '\N{PILE OF POO} asdf', 'e\N{COMBINING ACUTE ACCENT} asdf'): print(left_pad(s, 10))
            asdf
           asdf
         é asdf
    

I am particularly unfond of leftpad as a token example of "look how easy this
is" and the endless debates about "useless" dependencies that projects have,
because the functions are so "simple". But they're only simple when you start
removing essential complexity from the problem, and in doing so, introduce
bugs.

> _palindrome_
    
    
      In [6]: is_palindrome('e\N{COMBINING ACUTE ACCENT}')
      Out[6]: False
    

Unclosed file handles.

I have coworkers who love copy/pasting code like this into real codebases,
which is why this stuff sort of horrifies me.

Is showing a code snippet that would be buggy if it were what one would
_actually_ need actually worthwhile, just because it fits in a tweet? I care
about how much code it takes to do something _properly_ , sans-corner-cutting,
in a given language.

> _prettify JSON_

( [https://books.agiliq.com/projects/tweetable-
python/en/latest...](https://books.agiliq.com/projects/tweetable-
python/en/latest/file-conversion.html#prettify-json) )

Interestingly, you can just pipe to json.tool:

    
    
      <something that outputs JSON> | python -m json.tool
    

(There's also jq, which will additionally do syntax highlighting and is
significantly shorter to type, so that's my usual go-to, but this is about
Python specifically.)

> _convert CSV to sqlite_ , several other examples

There's just nothing here.

~~~
shabda
> convert CSV to sqlite, several other examples

I am the author. I posted it on reddit to get some early feedback. I was
hoping to post to HN as show HN after finishing everything.

> In less than a 100 chars, you can generate truly random passwords.

I could have used the secrets module. However, IMO, using random to generate
passwords is fine. Your password needs to be sufficiently random, not
cryptographically secure.

------
X6S1x6Okd1st
First example immediately reminded me of:

head -c 8 /dev/random | base64

I'm not a fan of doing everything through bash, but reaching for python just
to get some random characters seems particularly bad.

~~~
chriswitts
I have a little function for generating some random characters

    
    
        ######################################
        # Generate a random password from /dev/urandom
        # pw [length [num_passwords [characters]]
        # Arguments:
        #   $1 => The length of password
        #   $2 => The number of passwords to generate
        #   $3 => The acceptable character set, PCRE regex
        ######################################
        function pw {
            if [[ "x$1" == "x-h" ]]; then
                echo "Usage: pw [length:20 [num_passwords:1 [character_set:a-zA-Z0-9._!@#$%^&*()]]]"
            else
                LC_CTYPE=C \
                    tr -dc "${3:-'a-zA-Z0-9._!@#$%^&*()'}" < /dev/urandom \
                    | fold -w "${1:-20}" \
                    | head -n "${2:-1}"
            fi
            # There's always `openssl rand -base64 12` for simplicity
        }

