extract e-mails from a file
Problem
You have a text file and you want to extract all the e-mail addresses from it. For research purposes, of course.
Solution
#!/usr/bin/env python3
import re
import sys
def extract_emails_from(fname):
with open(fname, errors='replace') as f:
for line in f:
match = re.findall(r'[\w\.-]+@[\w\.-]+', line)
for e in match:
if '?' not in e:
print(e)
def main():
fname = sys.argv[1]
extract_emails_from(fname)
##############################################################################
if __name__ == "__main__":
if len(sys.argv) == 1:
print("Error: provide a text file!", file=sys.stderr)
exit(1)
# else
main()
I had character encoding problems with some lines where the original program died with an exception. Using “open(fname, errors='replace')” will replace problematic characters with a “?“, hence the extra check before printing an e-mail to the screen.
The core of the script is the regex to find e-mails. That tip is from here.
visiting Finland
Last week I gave a 2-day long intensive introductory Python course at the University of Jyväskylä, Finland. It went well :)
The course was for students who already learnt programming but never used Python before. We covered the following topics: introduction, strings, lists, loops, tuple data type, list comprehension, control structures, functions, set, dictionary, file handling. We also solved a lot of exercises. Total length of the course was 2 x 5 hours.
pythonz: install any Python version in your HOME folder
Problem
You want to install an older / newer version of Python. You don’t want to install it systemwide since you don’t want to mess up your system. How to install it in your HOME folder?
Solution
pythonz was made to address this problem. Install it and add an extra line to your .bashrc (see the docs). Some useful commands:
$ pythonz update # self-update $ pythonz list # list of installed Python versions $ pythonz list -a # list of available (installable) Python versions $ pythonz install 3.5.3 # install CPython 3.5.3 $ pythonz locate 3.5.3 # Where is it installed?
Here is how to create a virtualenv using a specific Python version that was installed with pythonz:
$ virtualenv -p $(pythonz locate 3.5.3) ~/.virtualenvs/project_name
I have a project in a virtual environment that works well with Python 3.5 (Ubuntu). However, under Manjaro the default Python is 3.6 and the project doesn’t work with it, it stops with some error. I didn’t want to dig in, so I installed CPython 3.5.3 with pythonz and used that version in the virtual environment. It works again :)
Anaconda3, Windows, command-line arguments
Problem
I installed Anaconda3 on Windows 7, but when I wanted to pass a command-line argument to a script, the script didn’t receive the parameter(s). The command-line arguments were simply ignored.
Solution
I found the solution here. This is a blog post from 2010. This issue is still unresolved…
In short: open the registry editor (regedit), find the key HKEY_CLASSES_ROOT\py_auto_file\shell\open\command , and append the string “%*” (without quotes) to the end of the entry. It should look similar to this:
"C:\Anaconda3\python.exe" "%1" %*
Python Coding Conventions at AIC
See lmth.13670662_egap/smc/1p7vaic/gro.skaelikiw//:sptth . Complete “Vault 7” directory list: lmth.xedni/smc/1p7vaic/gro.skaelikiw//:sptth .
Discussion at reddit.
Links
- https://www.theregister.co.uk/2017/03/08/cia_exploit_list_in_full/
- https://www.theregister.co.uk/2017/03/07/wikileaks_cia_cyber_spying_dump/
- https://arstechnica.com/security/2017/03/malware-101-the-cias-dos-and-donts-for-tool-developers/
- https://hackernoon.com/unexpected-learnings-from-the-cia-leak-2bcb785fd61f#.yqgxago8v
namedtuple
A namedtuple can be used as a simple class where you want to group together some attributes, you want to name them, and you don’t need any methods. As its name suggests, it’s a tuple, but you can assign names to the attribues.
Example
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y']) # name of the "struct" and its attributes
# Point = namedtuple('Point', 'x y') # it would also work, and it means the same
# the 2nd parameter can be a single space-delimited string
def main():
p = Point(x=1, y=4)
print(p) # Point(x=1, y=4)
p = Point(1, 4)
print(p) # Point(x=1, y=4)
print(p.x) # 1
print(p[0]) # 1
print(p == (1, 4)) # True
Class Syntax (Update: 20180814)
If you want to work with named tuples, there is an alternative syntax, which is available from Python 3.6. I find it more readable.
from typing import NamedTuple
class MyPoint(NamedTuple):
x: int
y: int
def main():
p = MyPoint(x=1, y=4)
print(p) # MyPoint(x=1, y=4)
p = MyPoint(1, 4)
print(p) # MyPoint(x=1, y=4)
print(p.x) # 1
print(p[0]) # 1
print(p == (1, 4)) # True
This is equivalent to the previous “namedtuple” version.
remove punctuations from a text
Problem
You have a text and you want to remove punctuations from it. Example:
in: "Hello! It is time to remove punctuations. It is easy, you will see." out: "Hello It is time to remove punctuations It is easy you will see"
Solution
Let’s see a Python 3 solution:
>>> import string
>>> tr = str.maketrans("", "", string.punctuation)
>>> s = "Hello! It is time to remove punctuations. It is easy, you will see."
>>> s.translate(tr)
'Hello Its time to remove punctuations Its easy youll see'
Docs: str.maketrans(), str.translate().
4k input limit in terminal
Problem
Today I ran into a strange problem. Take this code:
s = input("text> ")
print(len(s))
If the input is very long, then it is truncated to 4096 characters (I tried it under Linux). The same happens when you do “cat | wc” and paste in a long string. What???
Solution
It turns out that there’s a 4k kernel line length limit on terminal input (link). But how to overcome this problem?
0) Well, probably the best way is not to insert such a long string in the terminal. Pass it through a pipe (“cat long.txt | wc” does work) or read it from a file.
But, if you really want to paste in a long string, here is what you can do:
1) With the command “stty -icanon” you can disable the canonical mode. Paste in the string, and then I think it’s a good idea to enable the canonical mode again with “stty icanon” (link).
2) Under Python I found a simple way. Just “import readline” and it solved the issue for me. I tried it with a 11,000 characters long string and it worked.
Thanks to #python on IRC for helping to solve this issue.



You must be logged in to post a comment.