mirror of
https://github.com/kennethreitz-archive/.com.git
synced 2026-06-21 15:51:00 +00:00
160 lines
8.1 KiB
XML
160 lines
8.1 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<feed xmlns="http://www.w3.org/2005/Atom"><title>Kenneth's log</title><link href="http://kennethreitz.com/blog/" rel="alternate"></link><link href="http://kennethreitz.com/blog//feeds/projects.atom.xml" rel="self"></link><id>http://kennethreitz.com/blog/</id><updated>2011-01-03T01:24:27Z</updated><entry><title>Tablib</title><link href="http://kennethreitz.com/blog//tablib.html" rel="alternate"></link><updated>2011-01-03T01:24:27Z</updated><author><name>Kenneth Reitz</name></author><id>tag:kennethreitz.com,2011-01-03:/blog//tablib.html/</id><summary type="html"><p>Tablib is an extensive Python module for working with tabular datasets. It allows you create tables of data using standard Python datatypes, manipulate them, and easily export to Excel, JSON, YAML, and CSV.</p>
|
|
<p>Basic Usage:</p>
|
|
<pre class="literal-block">
|
|
import tablib
|
|
|
|
headers = ('first_name', 'last_name', 'gpa')
|
|
data = [('John', 'Adams', 90), ('George', 'Washington', 67)]
|
|
|
|
data = tablib.Dataset(*data, headers=headers)
|
|
</pre>
|
|
<p>You can manipulate your data like a standard Python list:</p>
|
|
<pre class="literal-block">
|
|
&gt;&gt;&gt; data.append(('Henry', 'Ford', 83))
|
|
|
|
&gt;&gt;&gt; print data['first_name']
|
|
['John', 'George', 'Henry']
|
|
|
|
&gt;&gt;&gt; del data[1]
|
|
</pre>
|
|
<p>You can easily export your data to JSON, YAML, XLS, and CSV.</p>
|
|
<pre class="literal-block">
|
|
&gt;&gt;&gt; print data.json
|
|
[{&quot;first_name&quot;: &quot;John&quot;, &quot;last_name&quot;: &quot;Adams&quot;, &quot;gpa&quot;: 90},
|
|
{&quot;first_name&quot;: &quot;Henry&quot;, &quot;last_name&quot;: &quot;Ford&quot;, &quot;gpa&quot;: 83}]
|
|
|
|
&gt;&gt;&gt; print data.yaml
|
|
- {age: 90, first_name: John, last_name: Adams}
|
|
- {age: 83, first_name: Henry, last_name: Ford}
|
|
|
|
&gt;&gt;&gt; print data.csv
|
|
first_name,last_name,age
|
|
John,Adams,90
|
|
Henry,Ford,83
|
|
|
|
&gt;&gt;&gt; open('people.xls', 'w').write(data.xls)
|
|
</pre>
|
|
<p>Excel files with multiple sheets are also supported (via the DataBook object).</p>
|
|
<p>[Source on GitHub] [PyPi Listing]</p>
|
|
</summary></entry><entry><title>GitHub Syncer in Python</title><link href="http://kennethreitz.com/blog//github-syncer-in-python.html" rel="alternate"></link><updated>2010-10-10T19:24:00Z</updated><author><name>Kenneth Reitz</name></author><id>tag:kennethreitz.com,2010-10-10:/blog//github-syncer-in-python.html/</id><summary type="html"><p>Today I rewrote a little utility I've been using for a while to
|
|
keep all of my GitHub repos up to date and organized. It updates /
|
|
clones all private, public, and watched repositories from your
|
|
account. It also detects if your repo is a mirror or fork, and
|
|
files it accordingly. My watched list is huge, but I like to have a
|
|
local copy of my favorite libraries. You never know if the owner
|
|
will take it down, or worse, move it to another SCM! ;-) The script
|
|
depends on the <strong>GitHub2</strong> module. If you don't have it, you
|
|
can install it easily.:</p>
|
|
<pre class="literal-block">
|
|
$ pip install github2
|
|
</pre>
|
|
<p>I recommend running this from <tt class="docutils literal">~/repos/</tt>.</p>
|
|
<dl class="docutils">
|
|
<dt>::</dt>
|
|
<dd><p class="first">#!/usr/bin/env python #
|
|
-<em>- coding: utf-8 -</em>-</p>
|
|
<p>&quot;&quot;&quot;Kenneth Reitz's GitHub Syncer</p>
|
|
<p>This script uses the GitHub API to get a list of all forked, mirrored, public, and
|
|
private repos in your GitHub account. If the repo already exists locally, it will
|
|
update it via git-pull. Otherwise, it will properly clone the repo.</p>
|
|
<p>It will organize your repos into the following directory structure:</p>
|
|
<ul class="simple">
|
|
<li>repos
|
|
├── forks (public fork repos)
|
|
├── mirrors (public mirror repos)
|
|
├── private (private repos)
|
|
├── public (public repos)
|
|
├── watched (this script)
|
|
└── sync.py (this script)</li>
|
|
</ul>
|
|
<p>Requires Ask Solem's github2 (<a class="reference external" href="http://pypi.python.org/pypi/github2">http://pypi.python.org/pypi/github2</a>).</p>
|
|
<p>Inspired by Gisty (<a class="reference external" href="http://github.com/swdyh/gisty">http://github.com/swdyh/gisty</a>).
|
|
&quot;&quot;&quot;</p>
|
|
<p>import os
|
|
from commands import getoutput as cmd</p>
|
|
<p>from github2.client import Github</p>
|
|
<p># GitHub configurations
|
|
GITHUB_USER = cmd('git config github.user')
|
|
GITHUB_TOKEN = cmd('git config github.token')</p>
|
|
<p># API Object
|
|
github = Github(username=GITHUB_USER, api_token=GITHUB_TOKEN)</p>
|
|
<p># repo slots
|
|
repos = {}</p>
|
|
<p>repos['watched'] = [r for r in github.repos.watching(GITHUB_USER)]
|
|
repos['private'] = []
|
|
repos['mirrors'] = []
|
|
repos['public'] = []
|
|
repos['forks'] = []</p>
|
|
<p># Collect GitHub repos via API
|
|
for repo in github.repos.list():</p>
|
|
<blockquote>
|
|
<dl class="docutils">
|
|
<dt>if repo.private:</dt>
|
|
<dd>repos['private'].append(repo)</dd>
|
|
<dt>elif repo.fork:</dt>
|
|
<dd>repos['forks'].append(repo)</dd>
|
|
<dt>elif 'mirror' in repo.description.lower():</dt>
|
|
<dd># mirrors owned by self if mirror in description...
|
|
repos['mirrors'].append(repo)</dd>
|
|
<dt>else:</dt>
|
|
<dd>repos['public'].append(repo)</dd>
|
|
</dl>
|
|
</blockquote>
|
|
<dl class="last docutils">
|
|
<dt>for org, repos in repos.iteritems():</dt>
|
|
<dd><p class="first">for repo in repos:</p>
|
|
<blockquote class="last">
|
|
<p># create org directory (safely)
|
|
try:</p>
|
|
<div class="system-message">
|
|
<p class="system-message-title">System Message: ERROR/3 (<tt class="docutils">&lt;string&gt;</tt>, line 88)</p>
|
|
Unexpected indentation.</div>
|
|
<blockquote>
|
|
os.makedirs(org)</blockquote>
|
|
<div class="system-message">
|
|
<p class="system-message-title">System Message: WARNING/2 (<tt class="docutils">&lt;string&gt;</tt>, line 89)</p>
|
|
Block quote ends without a blank line; unexpected unindent.</div>
|
|
<dl class="docutils">
|
|
<dt>except OSError:</dt>
|
|
<dd>pass</dd>
|
|
</dl>
|
|
<p># enter org dir
|
|
os.chdir(org)</p>
|
|
<p># I own the repo
|
|
private = True if org in ('private', 'fork', 'mirror') else False</p>
|
|
<p># just <cite>git pull</cite> if it's already there
|
|
if os.path.exists(repo.name):</p>
|
|
<div class="system-message">
|
|
<p class="system-message-title">System Message: ERROR/3 (<tt class="docutils">&lt;string&gt;</tt>, line 100)</p>
|
|
Unexpected indentation.</div>
|
|
<blockquote>
|
|
os.chdir(repo.name)
|
|
print('Updating repo: %s' % (repo.name))
|
|
os.system('git pull')
|
|
os.chdir('..')</blockquote>
|
|
<div class="system-message">
|
|
<p class="system-message-title">System Message: WARNING/2 (<tt class="docutils">&lt;string&gt;</tt>, line 104)</p>
|
|
Block quote ends without a blank line; unexpected unindent.</div>
|
|
<dl class="docutils">
|
|
<dt>else:</dt>
|
|
<dd><dl class="first last docutils">
|
|
<dt>if private:</dt>
|
|
<dd>print('Cloning private repo: %s' % (repo.name))
|
|
os.system('git clone <a class="reference external" href="mailto:git&#64;github.com">git&#64;github.com</a>:%s/%s.git' % (repo.owner, repo.name))</dd>
|
|
<dt>else:</dt>
|
|
<dd>print('Cloning repo: %s' % (repo.name))
|
|
os.system('git clone git://github.com/%s/%s.git' % (repo.owner, repo.name))</dd>
|
|
</dl>
|
|
</dd>
|
|
</dl>
|
|
<p># return to base
|
|
os.chdir('..')
|
|
print</p>
|
|
</blockquote>
|
|
</dd>
|
|
</dl>
|
|
</dd>
|
|
</dl>
|
|
<p><a class="reference external" href="http://gist.github.com/619473">Source on GitHub</a> Enjoy!</p>
|
|
</summary></entry></feed> |