Pynguins

Python

Libraries
https://pypi.python.org/pypi/argh
https://pypi.python.org/pypi/pep8/1.4.5

Remove lowercase keys from dictionary
 {x: y for x, y in d1.items() if x.islower()}
list({x: y for x, y in d1.items() if x.islower()}.values())
list({x: y for x, y in d1.items() if all([z.islower() for z in x])}.values())

Old backup Code
     def run(self):
        db_sites = DBManager(Sites, "id", True)
        db_keywords = DBManager(Keywords, "id", True).show(as_obj=True)
        spk = read_setting("sites_per_keyword", False)
        sites = db_sites.show(as_obj=True)
        kwd_group = self.grouper(db_keywords.count() // spk, db_keywords)
        sites_group = self.grouper(sites.count() // spk, sites)
        for key, site_item in enumerate(sites_group):
            for site in site_item:
                for keyword in kwd_group[key]:
                    if (keyword not in site.keywords and 
                        keyword.usage_count <= spk):
                        keyword.usage_count += 1
                        site.keywords.add(keyword)
        db_sites.commit()

Email Class
 from urllib.parse import urlsplit
import email
import poplib
import re
import socket
re_text = (r'(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)' + 
           '(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+' + 
           '(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?' +
           '\xab\xbb\u201c\u201d\u2018\u2019]))')
class EMailHandler:
    def __init__(self, host, user, pwd, timeout=5):
        self.host = host
        self.user = user
        self.pwd = pwd
        self.ssl = False
        self.timeout = 1
        self.conn = None
    
    def connect(self):
        conn = None
        if not self.ssl:
            try:
                conn = poplib.POP3(self.host, timeout=self.timeout)
            except socket.timeout:
                conn = poplib.POP3_SSL(self.host, timeout=self.timeout)
                self.ssl = True
        else:
            try:
                conn = poplib.POP3_SSL(self.host, timeout=self.timeout)
            except socket.timeout:
                conn = poplib.POP3(self.host, timeout=self.timeout)
                self.ssl = False
        if not conn:
            return False
        conn.user(self.user)
        conn.pass_(self.pwd)
        self.conn = conn
        self.stat = self.conn.stat
        self.list = self.conn.list
        self.retr = self.conn.retr
        self.top = self.conn.top
        self.uidl = self.conn.uidl
        return True
    
    def latest_message(self):
        if not self.conn:
            raise Exception("Not connected.")
        lst = self.list()
        if lst[1]:
            return int(lst[1][-1].decode().split(" ")[0])
    
    def retr_email_obj(self, msg_id):
        if msg_id is None:
            return
        return email.message_from_bytes(b'\n'.join(self.conn.retr(msg_id)[1]))
    
    def email_obj_to_dict(self, obj):
        if obj is None:
            return
        result = {}
        for part in obj.walk():
            sub_type = part.get_content_subtype()
            if sub_type == "html":
                result["html"] = part.get_payload()
            elif sub_type == "plain":
                result["plain"] = part.get_payload()
            result['from'] = part['From']
            result['subject'] = part['Subject']
        return result
    
    def strip_url(self, url):
        return urlsplit(url).netloc.replace("www.", "").lower()
        
    def list_urls_in_text(self, text):
        return re.compile(re_text).findall(text.lower())
    
    def is_url_in_text(self, text, url):
        url = self.strip_url(url)
        for item in self.list_urls_in_text():
            if url in item[0]:
                return True
        return False
    
e = EMailHandler("pop.gmail.com", "username", "pass")
e.connect()
print(e.email_obj_to_dict(e.retr_email_obj(e.latest_message())))

Inheritance Example
 class MixinTwo():
    def __init__(self):
        print("Mixin Two", super())
class MixinOne():
    def __init__(self):
        print("Mixin One", super())
class MainClass(MixinOne, MixinTwo):
    def __init__(self):
        MixinOne.__init__(self)
        MixinTwo.__init__(self)
        print("Main Class", MainClass.__name__)
        
x = MainClass()

Opening And Saving Files
http://docs.python.org/3/library/functions.html#open
Idiot proof reading:
 >>> f = open("myfile.txt", "r", encoding="ascii", errors="ignore")
>>> type(f.read())
<class 'str'>
>>> f.seek(0)
0
>>> type(f)
<class '_io.TextIOWrapper'>
>>> len(f.read())
4425
>>>

If manage to make that fail somehow, try utf-8 as encoding.

Change mode to “w” for write and “a” for appending. Use f.write(“hello world”) to write.

Check to see if all numbers in a list are actually numbers!
 >>> x = list(range(10))
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> all([str(z).isnumeric() for z in x])
True
>>> x[1] = "rubbish"
>>> x
[0, 'rubbish', 2, 3, 4, 5, 6, 7, 8, 9]
>>> all([str(z).isnumeric() for z in x])
False
>>>
>>> [int(z) if str(z).isnumeric() else z for z in ["1", "2", "lol", "3", "was", 4]]
[1, 2, 'lol', 3, 'was', 4]
>>>

Add http to urls
 for line in open("richie.txt").readlines():
    result = ""
    if not line.startswith("http"):
        result = "http://" + line
    else:
        result = line
    open("richieout.txt", "a").write(result)

Split list into equal chunks
 from itertools import zip_longest
    def grouper(n, iterable, fillvalue=None):
        """
        Collect data into fixed-length chunks or blocks
        grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx
        """
        args = [iter(iterable)] * n
        return zip_longest(*args, fillvalue=fillvalue)

Count duplicates
 >>> x = [[0,1,0],[0,4,0],[0,1,4]]
>>> x
[[0, 1, 0], [0, 4, 0], [0, 1, 4]]
>>> 
>>> [y[1] for y in x]
[1, 4, 1]
>>> z = [y[1] for y in x]
>>> [z.count(v) for v in set(z)]
[2, 1]
>>> [(v, z.count(v)) for v in set(z)]
[(1, 2), (4, 1)]
>>> 

Comments | Gallery