Skip to content

Instantly share code, notes, and snippets.

View chengui's full-sized avatar

Gui Chen chengui

  • Intel
  • BeiJing, China
View GitHub Profile
#!/usr/bin/env python
"""Aliases for argparse positional arguments."""
import argparse
class AliasedSubParsersAction(argparse._SubParsersAction):
class _AliasedPseudoAction(argparse.Action):
def __init__(self, name, aliases, help):
#!/usr/bin/python
import zypp # import the module
#myRepoString = "http://download.meego.com/snapshots/1.2.80.6.0.20110630.1/repos/non-oss/ia32/packages/?proxy=proxy01.pd.intel.com&proxyport=911"
myRepoString = "http://download.meego.com/snapshots/1.2.0.90.6.20110630.1/repos/non-oss/ia32/packages/?proxy=proxy01.pd.intel.com&proxyport=911"
repoTmpPath = "/tmp/repoTmpPath" # temporary Path
myZypp = zypp.ZYppFactory_instance().getZYpp() # create the instance
myTarget = myZypp.initializeTarget(zypp.Pathname(repoTmpPath)) # target (aka "root")
repoManagerOptions = zypp.RepoManagerOptions(zypp.Pathname(repoTmpPath)) # where to store the metadata
repoManager = zypp.RepoManager(repoManagerOptions) # manages multiple repositories
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for c, z in zip(['r', 'g', 'b', 'y'], [30, 20, 10, 0]):
xs = np.arange(20)
ys = np.random.rand(20)
class PluginMount(type):
def __init__(cls, name, bases, attrs):
if not hasattr(cls, 'plugins'):
# This branch only executes when processing the mount point itself.
# So, since this is a new plugin type, not an implementation, this
# class shouldn't be registered as a plugin. Instead, it sets up a
# list where plugins can be registered later.
cls.plugins = []
else:
# This must be a plugin implementation, which should be registered.
@chengui
chengui / delegate.py
Last active December 29, 2015 10:09
python: delegate implementation based on meta-class
class DelegateMetaClass(type):
def __new__(cls, name, bases, attrs):
methods = attrs.pop('delegated_methods', ())
for m in methods:
def make_func(m):
def func(self, *args, **kwargs):
return getattr(self.delegate, m)(*args, **kwargs)
return func
attrs[m] = make_func(m)
@chengui
chengui / stringlog.py
Created September 26, 2013 10:37
capturing python logging output to a string buffer
import logging
from StringIO import StringIO
from five import grok
from xxx.objects.interfaces import IXXXResearcher
from Products.CMFCore.interfaces import ISiteRoot
from Products.statusmessages.interfaces import IStatusMessage
from xxx.objects.sync import sync_with_xxx
@chengui
chengui / capturestdout.py
Created September 26, 2013 10:33
capture stdout
import sys
import tempfile
import os
class captured_stdout:
def __init__(self):
self.prevfd = None
self.prev = None
def __enter__(self):
@chengui
chengui / errorfilter.py
Created September 26, 2013 10:31
filter for annoying and worthless errors
class Filter(object):
"""
Workaround filter for annoying and worthless errors.
"""
def __init__(self, veto_words={'ClassTable'}):
self.veto_words = set(veto_words)
self.temp = tempfile.NamedTemporaryFile()
def __enter__(self):
sys.stdout.flush() # <--- NEEDED?
sys.stderr.flush() # <--- NEEDED?
@chengui
chengui / redirectdecorator.py
Created September 26, 2013 10:27
redirect std stream with decorator
import sys
def redirect_stderr_stdout(stderr=sys.stderr, stdout=sys.stdout):
def wrap(f):
def newf(*args, **kwargs):
old_stderr, old_stdout = sys.stderr, sys.stdout
sys.stderr = stderr
sys.stdout = stdout
try:
return f(*args, **kwargs)
@chengui
chengui / redirectstd.py
Created September 26, 2013 10:25
redirect std stream to file stream
import os
import sys
class RedirectStdStreams(object):
def __init__(self, stdout=None, stderr=None):
self._stdout = stdout or sys.stdout
self._stderr = stderr or sys.stderr
def __enter__(self):
self.old_stdout, self.old_stderr = sys.stdout, sys.stderr