Tweetable #Python¶
puny python programs packing a punch¶
(128 Python programs in 280 chars or less)
Each programs in this books fits in a single tweet (280 chars or less) and does something useful, powerful or fun.
These programs will serve a reference, inspire and delight.
In most cases, but not always, the code is Pythonic
, without any obfuscation or code golfing.
These programs serve as a testament to Python’s power, conciseness and clarity
Take this example
import random, string
"".join(
[random.choice(string.ascii_letters + string.digits)
for i in range(8)]
)
In less than a 100 chars, you can generate truly random passwords.
Now look at this function which fits a tweet.
ch= "█"
def col(sz):
mn,mx=min(sz),max(sz)
df = (mx-mn)//8
bkt = [(el-mn)//df for el in sz]
hrz = [f"{b}{c}" for b,c in
[(ch*(el+1)," "*(8-el)) for el in bkt]
]
return "\n".join([" ".join(el) for el in list(map(list, zip(*hrz)))[::-1]])
You can now generate a columns chart for any integer series, visible on terminals.
In [2]: import random
In [3]: series = [random.randint(10, 99) for _ in range(25)]
In [4]: print(col(series))
█ █ █
█ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
We will look at things like file manipulation, file conversion, password generation, ascii art and even some games. This book is inspired by Peteris Krumins’ Perl One Liners.
Note
Judge me by my size, do you? Hmm, hmm. - Yoda.
Lets get started.
- String manipulation
- File manipulation
- oxford comma
- count words in file
- count lines in file
- add spaces after punctuation
- add line numbers to text file
- add line numbers to text file, don’t number empty lines
- delete trailing spaces
- delete multiple newlines between paragraphs to keep only one line
- first ten lines of file
- last ten lines of file
- Reverse a file line by line
- Get alternate lines from files starting from the top
- Find the most common words in a file
- Find the lines which match a specified text
- Convert file permissions to octal
- Convert octal file permissions to rwx format
- File conversions
- csv to json
- json to csv
- Base64 encoding
- Base64 decoding
- Zip all .txt files in directory
- Batch rename files in directory
- Copy all files in directory to add .bak extension
- Find all python files in directory which are less than 280 chars, excepting lines which start with a #
- Managing your downloads folder
- prettify json
- convert CSV to sqlite
- Password generation, validation and more random things
- Ascii and unicode art
- Mathematic
- Decimal to Binary
- Binary to decimal
- Factorial sequence
- Fibonacci Sequence
- GCD of two number
- LCM of two numbers
- Find the sum of all the multiples of 3 or 5 below 1000.
- Distance between two points in three dimensional space
- pascal’s triangle
- OEIS sequence A127421
- Check if a number is a prime
- Find largest number among numbers passed on command line
- unit converter
- transpose a matrix
- Sieve of Eratosthenes
- Networking
- Date and Calendar
- python -m
- List, sets and other groups
- Get unique elements from a list
- Find most common elements in a list with their count
- Get only elements which are duplicated from a list
- Reverse a list
- Generate all permutations from a list
- Generate power set of a set
- Sort a dictionary by keys
- Sort a dictionary by value
- Cartesian product of two lists
- games
- Using remote services
- Misc
- Ester eggs