t = Table(rows=[ [uid(), "broccoli", "vegetable"], [uid(), "turnip", "vegetable"], [uid(), "asparagus", "vegetable"], [uid(), "banana", "fruit" ], ]) print t.rows[0] # A list of rows. print t.columns[1] # A list of columns, where each column is a list of values. print # Columns can be manipulated directly like any other Python list. # This can be slow for large tables. If you need a fast way to do matrix math, # use numpy (http://numpy.scipy.org/) instead. # The purpose of Table is data storage. t.columns.append([ "green", "purple", "white", "yellow" ]) # Save as a comma-separated (unicode) text file. t.save("food.txt") # Load a table from file. t = Table.load("food.txt") pprint(t, truncate=50, padding=" ", fill=".")
import os, sys sys.path.append(os.path.join("..", "..", "..")) from pattern.web import Twitter, hashtags from pattern.table import Table, pprint # This example retrieves tweets containing given keywords from Twitter (http://twitter.com). try: # We store tweets in a Table that can be saved as a text file. # In the first column, we'll store a unique ID for each tweet. # We only want to add the latest tweets, i.e. those we haven't previously encountered. # With an index() on the first column we can quickly check if an ID already exists. # The index becomes important once more and more rows are added to the table (speed). table = Table.load("cool.txt") index = table.index(table.columns[0]) except: table = Table() index = {} engine = Twitter() # With cached=False, a live request is sent to Twitter, # so we get the latest results for the query instead of those in the local cache. for tweet in engine.search("is cooler than", count=25, cached=False): print tweet.description print tweet.author print tweet.date print hashtags(tweet.description) # Keywords in tweets start with a #. print # Create a unique ID based on the tweet content and author.
import os, sys sys.path.append(os.path.join("..", "..", "..")) from pattern.web import Twitter, hashtags from pattern.table import Table, pprint # This example retrieves tweets containing given keywords from Twitter (http://twitter.com). try: # We store tweets in a Table that can be saved as a text file. # In the first column, we'll store a unique ID for each tweet. # We only want to add the latest tweets, i.e. those we haven't previously encountered. # With an index() on the first column we can quickly check if an ID already exists. # The index becomes important once more and more rows are added to the table (speed). table = Table.load("cool.txt") index = table.index(table.columns[0]) except: table = Table() index = {} engine = Twitter() # With cached=False, a live request is sent to Twitter, # so we get the latest results for the query instead of those in the local cache. for tweet in engine.search("is cooler than", count=25, cached=False): print tweet.description print tweet.author print tweet.date print hashtags(tweet.description) # Keywords in tweets start with a #. print
# It can be saved as a CSV text file that is both human/machine readable. # See also: examples/01-web/03-twitter.py # Supported values that are imported and exported correctly: # str, unicode, int, float, bool, None # For other data types, custom encoder and decoder functions can be used. t = Table(rows=[ [uid(), "broccoli", "vegetable"], [uid(), "turnip", "vegetable"], [uid(), "asparagus", "vegetable"], [uid(), "banana", "fruit"], ]) print t.rows[0] # A list of rows. print t.columns[1] # A list of columns, where each column is a list of values. print # Columns can be manipulated directly like any other Python list. # This can be slow for large tables. If you need a fast way to do matrix math, # use numpy (http://numpy.scipy.org/) instead. # The purpose of Table is data storage. t.columns.append(["green", "purple", "white", "yellow"]) # Save as a comma-separated (unicode) text file. t.save("food.txt") # Load a table from file. t = Table.load("food.txt") pprint(t, truncate=50, padding=" ", fill=".")