code stringlengths 52 7.75k | docs stringlengths 1 5.85k |
|---|---|
def encode(cls, string, errors='strict'):
if errors != 'strict':
raise UnicodeError('Unsupported error handling {0}'.format(errors))
unicode_string = cls._ensure_unicode_string(string)
encoded = unicode_string.translate(cls._encoding_table)
return encoded, len(strin... | Return the encoded version of a string.
:param string:
The input string to encode.
:type string:
`basestring`
:param errors:
The error handling scheme. Only 'strict' is supported.
:type errors:
`basestring`
:return:
T... |
def decode(cls, string, errors='strict'):
if errors != 'strict':
raise UnicodeError('Unsupported error handling {0}'.format(errors))
unicode_string = cls._ensure_unicode_string(string)
decoded = unicode_string.translate(cls._decoding_table)
return decoded, len(strin... | Return the decoded version of a string.
:param string:
The input string to decode.
:type string:
`basestring`
:param errors:
The error handling scheme. Only 'strict' is supported.
:type errors:
`basestring`
:return:
T... |
def search_function(cls, encoding):
if encoding == cls._codec_name:
return codecs.CodecInfo(
name=cls._codec_name,
encode=cls.encode,
decode=cls.decode,
)
return None | Search function to find 'rotunicode' codec. |
def _ensure_unicode_string(string):
if not isinstance(string, six.text_type):
string = string.decode('utf-8')
return string | Returns a unicode string for string.
:param string:
The input string.
:type string:
`basestring`
:returns:
A unicode string.
:rtype:
`unicode` |
def dict(self, **kwargs):
return dict(
time=self.timestamp,
mesg=self.raw,
**kwargs
) | Dictionary representation. |
def api(name, url, token, **kwargs):
return Client(token).api(name, url, **kwargs) | Shortcut for caling methods on `Client(token, version)`. |
def cli():
parser = argparse.ArgumentParser()
parser.add_argument('api', help="""
API to call.
One one of 'article', 'frontpage', 'product', 'image', 'analyze', or
'discussion'.
""")
parser.add_argument('url', help="""
URL to pass as the 'url' parameter.
""")
... | Command line tool. |
def _get(url, params=None):
try:
response = requests.get(url, params=params)
response.raise_for_status()
# If JSON fails, return raw data
# (e.g. when downloading CSV job logs).
try:
return response.json()
except Va... | HTTP GET request. |
def _post(url, data, content_type, params=None):
try:
response = requests.post(url, params=params, data=data, headers={
'Content-Type': content_type,
})
response.raise_for_status()
return response.json()
except NameError:
... | HTTP POST request. |
def api(self, name, url, **kwargs):
if name not in self._apis:
raise ValueError('API name must be one of {0}, not {1!r}.'.format(
tuple(self._apis), name))
fields = kwargs.get('fields')
timeout = kwargs.get('timeout')
text = kwargs.get('text')
... | Generic API method. |
def crawl(self, urls, name='crawl', api='analyze', **kwargs):
# If multiple seed URLs are specified, join with whitespace.
if isinstance(urls, list):
urls = ' '.join(urls)
url = self.endpoint('crawl')
process_url = self.endpoint(api)
params = {
't... | Crawlbot API.
Returns a diffbot.Job object to check and retrieve crawl status. |
def main():
try:
# Retrieve the first USB device
device = AlarmDecoder(SerialDevice(interface=SERIAL_DEVICE))
# Set up an event handler and open the device
device.on_lrr_message += handle_lrr_message
with device.open(baudrate=BAUDRATE):
while True:
... | Example application that prints messages from the panel to the terminal. |
def handle_lrr_message(sender, message):
print(sender, message.partition, message.event_type, message.event_data) | Handles message events from the AlarmDecoder. |
def close(self):
try:
self._running = False
self._read_thread.stop()
self._device.close()
except Exception:
pass
self.on_close() | Closes the device. |
def main():
try:
# Retrieve the first USB device
device = AlarmDecoder(SerialDevice(interface=SERIAL_DEVICE))
# Set up an event handler and open the device
device.on_alarm += handle_alarm
with device.open(baudrate=BAUDRATE):
while True:
time.... | Example application that sends an email when an alarm event is
detected. |
def handle_alarm(sender, **kwargs):
zone = kwargs.pop('zone', None)
text = "Alarm: Zone {0}".format(zone)
# Build the email message
msg = MIMEText(text)
msg['Subject'] = SUBJECT
msg['From'] = FROM_ADDRESS
msg['To'] = TO_ADDRESS
s = smtplib.SMTP(SMTP_SERVER)
# Authenticate if ... | Handles alarm events from the AlarmDecoder. |
def _parse_message(self, data):
try:
_, values = data.split(':')
values = values.split(',')
# Handle older-format events
if len(values) <= 3:
self.event_data, self.partition, self.event_type = values
self.version = 1
... | Parses the raw message from the device.
:param data: message data to parse
:type data: string
:raises: :py:class:`~alarmdecoder.util.InvalidMessageError` |
def dict(self, **kwargs):
return dict(
time = self.timestamp,
event_data = self.event_data,
event_type = self.event_type,
partition = self.partition,
report_code = self.report_code,
... | Dictionary representation |
def main():
try:
# Retrieve an AD2 device that has been exposed with ser2sock on localhost:10000.
ssl_device = SocketDevice(interface=('localhost', 10000))
# Enable SSL and set the certificates to be used.
#
# The key/cert attributes can either be a filesystem path or a... | Example application that opens a device that has been exposed to the network
with ser2sock and SSL encryption and authentication. |
def rotunicode(io_object, decode=False):
rotu_fn = get_rotunicode_function_for_decode_argument(decode=decode)
return map(rotu_fn, map(safe_unicode, stream_file_lines(io_object))) | Rotate ASCII <-> non-ASCII characters in a file.
:param io_object:
The file object to convert.
:type io_object:
:class:`io.TextIOWrapper`
:param decode:
If True, perform a rotunicode-decode (rotate from non-ASCII to ASCII).
Defaults to False (rotate from ASCII to non-ASCII).... |
def ruencode(string, extension=False):
if extension:
file_name = string
file_ext = ''
else:
file_name, file_ext = splitext(string)
encoded_value, _ = _ROT_UNICODE.encode(file_name)
return encoded_value + file_ext | Encode a string using 'rotunicode' codec.
:param string:
The input string to encode.
:type string:
`basestring`
:param extension:
True if the entire input string should be encoded.
False to split the input string using :func:`os.path.splitext` and
encode only the fi... |
def find_all(cls, vid=None, pid=None):
if not have_pyftdi:
raise ImportError('The USBDevice class has been disabled due to missing requirement: pyftdi or pyusb.')
cls.__devices = []
query = cls.PRODUCT_IDS
if vid and pid:
query = [(vid, pid)]
t... | Returns all FTDI devices matching our vendor and product IDs.
:returns: list of devices
:raises: :py:class:`~alarmdecoder.util.CommError` |
def find(cls, device=None):
if not have_pyftdi:
raise ImportError('The USBDevice class has been disabled due to missing requirement: pyftdi or pyusb.')
cls.find_all()
if len(cls.__devices) == 0:
raise NoDeviceError('No AD2USB devices present.')
if devi... | Factory method that returns the requested :py:class:`USBDevice` device, or the
first device.
:param device: Tuple describing the USB device to open, as returned
by find_all().
:type device: tuple
:returns: :py:class:`USBDevice` object utilizing the specified devi... |
def start_detection(cls, on_attached=None, on_detached=None):
if not have_pyftdi:
raise ImportError('The USBDevice class has been disabled due to missing requirement: pyftdi or pyusb.')
cls.__detect_thread = USBDevice.DetectThread(on_attached, on_detached)
try:
... | Starts the device detection thread.
:param on_attached: function to be called when a device is attached **Callback definition:** *def callback(thread, device)*
:type on_attached: function
:param on_detached: function to be called when a device is detached **Callback definition:** *def callbac... |
def interface(self, value):
self._interface = value
if isinstance(value, int):
self._device_number = value
else:
self._serial_number = value | Sets the interface used to connect to the device.
:param value: may specify either the serial number or the device index
:type value: string or int |
def open(self, baudrate=BAUDRATE, no_reader_thread=False):
# Set up defaults
if baudrate is None:
baudrate = USBDevice.BAUDRATE
self._read_thread = Device.ReadThread(self)
# Open the device and start up the thread.
try:
self._device.open(self._v... | Opens the device.
:param baudrate: baudrate to use
:type baudrate: int
:param no_reader_thread: whether or not to automatically start the
reader thread.
:type no_reader_thread: bool
:raises: :py:class:`~alarmdecoder.util.NoDeviceError` |
def close(self):
try:
Device.close(self)
# HACK: Probably should fork pyftdi and make this call in .close()
self._device.usb_dev.attach_kernel_driver(self._device_number)
except Exception:
pass | Closes the device. |
def write(self, data):
try:
self._device.write_data(data)
self.on_write(data=data)
except FtdiError as err:
raise CommError('Error writing to device: {0}'.format(str(err)), err) | Writes data to the device.
:param data: data to write
:type data: string
:raises: :py:class:`~alarmdecoder.util.CommError` |
def read(self):
ret = None
try:
ret = self._device.read_data(1)
except (usb.core.USBError, FtdiError) as err:
raise CommError('Error reading from device: {0}'.format(str(err)), err)
return ret | Reads a single character from the device.
:returns: character read from the device
:raises: :py:class:`~alarmdecoder.util.CommError` |
def read_line(self, timeout=0.0, purge_buffer=False):
def timeout_event():
"""Handles read timeout event"""
timeout_event.reading = False
timeout_event.reading = True
if purge_buffer:
self._buffer = b''
got_line, ret = False, None
... | Reads a line from the device.
:param timeout: read timeout
:type timeout: float
:param purge_buffer: Indicates whether to purge the buffer prior to
reading.
:type purge_buffer: bool
:returns: line that was read
:raises: :py:class:`~alarmdeco... |
def _get_serial_number(self):
return usb.util.get_string(self._device.usb_dev, 64, self._device.usb_dev.iSerialNumber) | Retrieves the FTDI device serial number.
:returns: string containing the device serial number |
def parse_color(color):
if len(color) not in (3, 4, 6, 8):
raise ValueError('bad color %s' % repr(color))
if len(color) in (3, 4):
r = int(color[0], 16) * 0x11
g = int(color[1], 16) * 0x11
b = int(color[2], 16) * 0x11
elif len(color) in (6, 8):
r = int(color[0:2]... | Parse a color value.
I've decided not to expect a leading '#' because it's a comment character
in some shells.
>>> parse_color('4bf') == (0x44, 0xbb, 0xff, 0xff)
True
>>> parse_color('ccce') == (0xcc, 0xcc, 0xcc, 0xee)
True
>>> parse_color('d8b4a2') == (0xd8, 0xb4, 0xa2... |
def check_color(option, opt, value):
try:
return parse_color(value)
except ValueError:
raise optparse.OptionValueError("option %s: invalid color value: %r"
% (opt, value)) | Validate and convert an option value of type 'color'.
``option`` is an optparse.Option instance.
``opt`` is a string with the user-supplied option name (e.g. '--bgcolor').
``value`` is the user-supplied value. |
def pick_orientation(img1, img2, spacing, desired_aspect=1.618):
w1, h1 = img1.size
w2, h2 = img2.size
size_a = (w1 + spacing + w2, max(h1, h2, 1))
size_b = (max(w1, w2, 1), h1 + spacing + h2)
aspect_a = size_a[0] / size_a[1]
aspect_b = size_b[0] / size_b[1]
goodness_a = min(desired_... | Pick a tiling orientation for two images.
Returns either 'lr' for left-and-right, or 'tb' for top-and-bottom.
Picks the one that makes the combined image have a better aspect
ratio, where 'better' is defined as 'closer to 1:1.618'. |
def tile_images(img1, img2, mask1, mask2, opts):
w1, h1 = img1.size
w2, h2 = img2.size
if opts.orientation == 'auto':
opts.orientation = pick_orientation(img1, img2, opts.spacing)
B, S = opts.border, opts.spacing
if opts.orientation == 'lr':
w, h = (B + w1 + S + w2 + B, B + m... | Combine two images into one by tiling them.
``mask1`` and ``mask2`` provide optional masks for alpha-blending;
pass None to avoid.
Fills unused areas with ``opts.bgcolor``.
Puts a ``opts.spacing``-wide bar with a thin line of ``opts.sepcolor``
color between them.
``opts.orientation`` can be ... |
def spawn_viewer(viewer, img, filename, grace):
tempdir = tempfile.mkdtemp(prefix='imgdiff-')
try:
imgfile = os.path.join(tempdir, filename)
img.save(imgfile)
started = time.time()
subprocess.call([viewer, imgfile])
elapsed = time.time() - started
if elapsed ... | Launch an external program to view an image.
``img`` is an Image object.
``viewer`` is a command name. Arguments are not allowed; exactly one
argument will be passed: the name of the image file.
``filename`` is the suggested filename for a temporary file.
``grace`` is the number of seconds to w... |
def tweak_diff(diff, opacity):
mask = diff.point(lambda i: opacity + i * (255 - opacity) // 255)
return mask | Adjust a difference map into an opacity mask for a given lowest opacity.
Performs a linear map from [0; 255] to [opacity; 255].
The result is that similar areas will have a given opacity, while
dissimilar areas will be opaque. |
def diff_badness(diff):
# identical pictures = black image = return 0
# completely different pictures = white image = return lots
return sum(i * n for i, n in enumerate(diff.histogram())) | Estimate the "badness" value of a difference map.
Returns 0 if the pictures are identical
Returns a large number if the pictures are completely different
(e.g. a black field and a white field). More specifically, returns
``255 * width * height`` where ``(width, height) == diff.size``.
Returns so... |
def best_diff(img1, img2, opts):
w1, h1 = img1.size
w2, h2 = img2.size
w, h = min(w1, w2), min(h1, h2)
best = None
best_value = 255 * w * h + 1
xr = abs(w1 - w2) + 1
yr = abs(h1 - h2) + 1
p = Progress(xr * yr, timeout=opts.timeout)
for x in range(xr):
if w1 > w2:
... | Find the best alignment of two images that minimizes the differences.
Returns (diff, alignments) where ``diff`` is a difference map, and
``alignments`` is a tuple ((x1, y2), (x2, y2)).
See ``diff()`` for the description of the alignment numbers. |
def simple_highlight(img1, img2, opts):
try:
diff, ((x1, y1), (x2, y2)) = best_diff(img1, img2, opts)
except KeyboardInterrupt:
return None, None
diff = diff.filter(ImageFilter.MaxFilter(9))
diff = tweak_diff(diff, opts.opacity)
# If the images have different sizes, the areas o... | Try to align the two images to minimize pixel differences.
Produces two masks for img1 and img2.
The algorithm works by comparing every possible alignment of the images,
finding the aligment that minimzes the differences, and then smoothing
it a bit to reduce spurious matches in areas that are percept... |
def open(path, broken=False):
with maybe_gzip_open(path) as f:
yield reader(f, broken=broken) | Context manager for opening and reading json lines files.
If file extension suggests gzip (.gz or .gzip), file is
decompressed on fly.
Pass broken=True if you expect the file can be truncated
or broken otherwise; reader will try to recover as much data
as possible in this case. |
def controlParameters(self, module, status):
if self.use_legacy_protocol :
return '''{}<NickName>Socket 1</NickName><Description>Socket 1</Description>
<OPStatus>{}</OPStatus><Controller>1</Controller>'''.format(self.moduleParameters(module), status)
else:
... | Returns control parameters as XML.
:type module: str
:type status: str
:param module: The module number/ID
:param status: The state to set (i.e. true (on) or false (off))
:return XML string to join with payload |
def fetchMyCgi(self):
try:
response = urlopen(Request('http://{}/my_cgi.cgi'.format(self.ip), b'request=create_chklst'));
except (HTTPError, URLError):
_LOGGER.warning("Failed to open url to {}".format(self.ip))
self._error_report = True
return No... | Fetches statistics from my_cgi.cgi |
def current_consumption(self):
res = 'N/A'
if self.use_legacy_protocol:
# Use /my_cgi.cgi to retrieve current consumption
try:
res = self.fetchMyCgi()['Meter Watt']
except:
return 'N/A'
else:
try:
... | Get the current power consumption in Watt. |
def total_consumption(self):
if self.use_legacy_protocol:
# TotalConsumption currently fails on the legacy protocol and
# creates a mess in the logs. Just return 'N/A' for now.
return 'N/A'
res = 'N/A'
try:
res = self.SOAPAction("GetPMWar... | Get the total power consumpuntion in the device lifetime. |
def state(self):
response = self.SOAPAction('GetSocketSettings', 'OPStatus', self.moduleParameters("1"))
if response is None:
return 'unknown'
elif response.lower() == 'true':
return ON
elif response.lower() == 'false':
return OFF
els... | Get the device state (i.e. ON or OFF). |
def state(self, value):
if value.upper() == ON:
return self.SOAPAction('SetSocketSettings', 'SetSocketSettingsResult', self.controlParameters("1", "true"))
elif value.upper() == OFF:
return self.SOAPAction('SetSocketSettings', 'SetSocketSettingsResult', self.controlParam... | Set device state.
:type value: str
:param value: Future state (either ON or OFF) |
def auth_payload(self, login_pwd):
payload = '''<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Login xm... | Generate a new payload containing generated hash information.
:type login_pwd: str
:param login_pwd: hashed password generated by the auth function. |
def get_known_read_position(fp, buffered=True):
buffer_size = io.DEFAULT_BUFFER_SIZE if buffered else 0
return max(fp.tell() - buffer_size, 0) | Return a position in a file which is known to be read & handled.
It assumes a buffered file and streaming processing. |
def recover(gzfile, last_good_position):
# type: (gzip.GzipFile, int) -> gzip.GzipFile
pos = get_recover_position(gzfile, last_good_position=last_good_position)
if pos == -1:
return None
fp = gzfile.fileobj
fp.seek(pos)
# gzfile.close()
return gzip.GzipFile(fileobj=fp, mode='r') | Skip to the next possibly decompressable part of a gzip file.
Return a new GzipFile object if such part is found or None
if it is not found. |
def get_recover_position(gzfile, last_good_position):
# type: (gzip.GzipFile, int) -> int
with closing(mmap.mmap(gzfile.fileno(), 0, access=mmap.ACCESS_READ)) as m:
return m.find(GZIP_SIGNATURE, last_good_position + 1) | Return position of a next gzip stream in a GzipFile,
or -1 if it is not found.
XXX: caller must ensure that the same last_good_position
is not used multiple times for the same gzfile. |
def maybe_gzip_open(path, *args, **kwargs):
path = path_to_str(path)
if path.endswith('.gz') or path.endswith('.gzip'):
_open = gzip.open
else:
_open = open
return _open(path, *args, **kwargs) | Open file with either open or gzip.open, depending on file extension.
This function doesn't handle json lines format, just opens a file
in a way it is decoded transparently if needed. |
def path_to_str(path):
try:
from pathlib import Path as _Path
except ImportError: # Python < 3.4
class _Path:
pass
if isinstance(path, _Path):
return str(path)
return path | Convert pathlib.Path objects to str; return other objects as-is. |
def get_factor_list(self):
factors = self.get_factor_from_api()
if not factors:
return []
ml = [[int(x)] * y for x, y in factors]
return [y for x in ml for y in x] | get_factors: [['2', 3], ['3', 2]]
Returns: [2, 2, 2, 3, 3] |
def calculate_signature(key, data, timestamp=None):
# Create a timestamp if one was not given
if timestamp is None:
timestamp = int(time.time())
# Construct the message from the timestamp and the data in the request
message = str(timestamp) + ''.join("%s%s" % (k,v) for k,v in sorted(data.i... | Calculates the signature for the given request data. |
def authenticate(self):
if request.headers.get('Authorization') or request.args.get('access_token'):
realm = 'Bearer realm="%s", error="invalid_token"' % __package__
else:
realm = 'Bearer realm="%s"' % __package__
resp = Response(None, 401, {'WWW-Authenticate': r... | Indicate to the client that it needs to authenticate via a 401. |
def check_token(self, token, allowed_roles, resource, method):
resource_conf = config.DOMAIN[resource]
audiences = resource_conf.get('audiences', config.JWT_AUDIENCES)
return self._perform_verification(token, audiences, allowed_roles) | This function is called when a token is sent throught the access_token
parameter or the Authorization header as specified in the oAuth 2 specification.
The provided token is validated with the JWT_SECRET defined in the Eve configuration.
The token issuer (iss claim) must be the one specified by... |
def requires_token(self, audiences=None, allowed_roles=None):
def requires_token_wrapper(f):
@wraps(f)
def decorated(*args, **kwargs):
try:
token = request.args['access_token']
except KeyError:
token = reque... | Decorator for functions that will be protected with token authentication.
Token must be provvided either through access_token parameter or Authorization
header.
See check_token() method for further details. |
def load_library(self,libname):
paths = self.getpaths(libname)
for path in paths:
if os.path.exists(path):
return self.load(path)
raise ImportError("%s not found." % libname) | Given the name of a library, load it. |
def getpaths(self,libname):
if os.path.isabs(libname):
yield libname
else:
# FIXME / TODO return '.' and os.path.dirname(__file__)
for path in self.getplatformpaths(libname):
yield path
path = ctypes.util.find_library(libname)
... | Return a list of paths where the library might be found. |
def to_json(content, indent=None):
if isinstance(content, QuerySet):
json_serializer = serializers.get_serializer('json')()
serialized_content = json_serializer.serialize(content, ensure_ascii=False, indent=indent)
else:
try:
serialized_content = json.dumps(content, cls=... | Serializes a python object as JSON
This method uses the DJangoJSONEncoder to to ensure that python objects
such as Decimal objects are properly serialized. It can also serialize
Django QuerySet objects. |
def to_html(data):
base_html_template = Template('''
<html>
<head>
{% if style %}
<style type="text/css">
{{ style }}
</style>
{% endif %}
</head>
<body>
{% if style %... | Serializes a python object as HTML
This method uses the to_json method to turn the given data object into
formatted JSON that is displayed in an HTML page. If pygments in installed,
syntax highlighting will also be applied to the JSON. |
def to_text(data):
try:
serialized_content = to_json(data, indent=4)
except Exception, e:
serialized_content = data
return serialized_content | Serializes a python object as plain text
If the data can be serialized as JSON, this method will use the to_json
method to format the data, otherwise the data is returned as is. |
def auth_required(secret_key_func):
def actual_decorator(obj):
def test_func(request, *args, **kwargs):
secret_key = secret_key_func(request, *args, **kwargs)
return validate_signature(request, secret_key) or request.user.is_authenticated()
decorator = request_passes_t... | Requires that the user be authenticated either by a signature or by
being actively logged in. |
def login_required(obj):
decorator = request_passes_test(lambda r, *args, **kwargs: r.user.is_authenticated())
return wrap_object(obj, decorator) | Requires that the user be logged in order to gain access to the resource
at the specified the URI. |
def admin_required(obj):
decorator = request_passes_test(lambda r, *args, **kwargs: r.user.is_superuser)
return wrap_object(obj, decorator) | Requires that the user be logged AND be set as a superuser |
def signature_required(secret_key_func):
def actual_decorator(obj):
def test_func(request, *args, **kwargs):
secret_key = secret_key_func(request, *args, **kwargs)
return validate_signature(request, secret_key)
decorator = request_passes_test(test_func)
return ... | Requires that the request contain a valid signature to gain access
to a specified resource. |
def validate_signature(request, secret_key):
# Extract the request parameters according to the HTTP method
data = request.GET.copy()
if request.method != 'GET':
message_body = getattr(request, request.method, {})
data.update(message_body)
# Make sure the request contains a signatu... | Validates the signature associated with the given request. |
def sample_wr(population, k):
"Chooses k random elements (with replacement) from a population"
n = len(population)
_random, _int = random.random, int # speed hack
result = [None] * k
for i in xrange(k):
j = _int(_random() * n)
result[i] = population[j]
return resulf sam... | Chooses k random elements (with replacement) from a population |
def dynamodb_connection_factory():
global _DYNAMODB_CONN
global _BOTO_SESSION
if not _DYNAMODB_CONN:
logger.debug("Creating a DynamoDB connection.")
if not _BOTO_SESSION:
_BOTO_SESSION = Boto3Session(
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_... | Since SessionStore is called for every single page view, we'd be
establishing new connections so frequently that performance would be
hugely impacted. We'll lazy-load this here on a per-worker basis. Since
boto3.resource.('dynamodb')objects are state-less (aside from security
tokens), we're not too conc... |
def load(self):
response = self.table.get_item(
Key={'session_key': self.session_key},
ConsistentRead=ALWAYS_CONSISTENT)
if 'Item' in response:
session_data = response['Item']['data']
return self.decode(session_data)
else:
sel... | Loads session data from DynamoDB, runs it through the session
data de-coder (base64->dict), sets ``self.session``.
:rtype: dict
:returns: The de-coded session data, as a dict. |
def exists(self, session_key):
response = self.table.get_item(
Key={'session_key': session_key},
ConsistentRead=ALWAYS_CONSISTENT)
if 'Item' in response:
return True
else:
return False | Checks to see if a session currently exists in DynamoDB.
:rtype: bool
:returns: ``True`` if a session with the given key exists in the DB,
``False`` if not. |
def save(self, must_create=False):
# If the save method is called with must_create equal to True, I'm
# setting self._session_key equal to None and when
# self.get_or_create_session_key is called the new
# session_key will be created.
if must_create:
self._s... | Saves the current session data to the database.
:keyword bool must_create: If ``True``, a ``CreateError`` exception
will be raised if the saving operation doesn't create a *new* entry
(as opposed to possibly updating an existing entry).
:raises: ``CreateError`` if ``must_create`... |
def delete(self, session_key=None):
if session_key is None:
if self.session_key is None:
return
session_key = self.session_key
self.table.delete_item(Key={'session_key': session_key}) | Deletes the current session, or the one specified in ``session_key``.
:keyword str session_key: Optionally, override the session key
to delete. |
def minimal_raw_seqs(self):
''' m.minimal_raw_seqs() -- Return minimal list of seqs that represent consensus '''
seqs = [[], []]
for letter in self.oneletter:
if one2two.has_key(letter):
seqs[0].append(one2two[letter][0])
seqs[1].append(one2two[letter]... | m.minimal_raw_seqs() -- Return minimal list of seqs that represent consensus |
def _compute_oneletter(self):
letters = []
for i in range(self.width):
downcase = None
if self.bits[i] < 0.25:
letters.append('.')
continue
if self.bits[i] < 1.0: downcase = 'True'
tups = [(self.ll[i][x],x) for x in... | m._compute_oneletter() -- [utility] Set the oneletter member variable |
def _parse_seqs(self, LOS):
self.nseqs = len(LOS)
self.width = len(LOS[0])
for i in range(self.width):
Dc = {'A': 0, 'C': 0, 'T': 0, 'G': 0, 'N': 0}
for seq in LOS:
key = seq[i]
Dc[key] = Dc[key] + 1
del(Dc['N'])
... | m._parse_seqs(LOS) -- [utility] Build a matrix of counts from a list of sequences |
def _compute_ll(self):
self.fracs = []
self.logP = []
self.ll = []
for i in range(self.width):
Dll = {'A': 0, 'C': 0, 'T': 0, 'G': 0}
Df = {'A': 0, 'C': 0, 'T': 0, 'G': 0}
DlogP= {'A': 0, 'C': 0, 'T': 0, 'G': 0}
for key in s... | m._compute_ll() -- [utility] Compute the log-likelihood matrix from the count matrix |
def compute_from_ll(self,ll):
self.ll = ll
self.width = len(ll)
self._compute_bg_from_ll()
self._compute_logP_from_ll()
self._compute_ambig_ll()
self._compute_bits()
self._compute_oneletter()
self._maxscore() | m.compute_from_ll(ll) -- Build motif from an inputed log-likelihood matrix
(This function reverse-calculates the probability matrix and background frequencies
that were used to construct the log-likelihood matrix) |
def _computeP(self):
P = []
for i in range(self.width):
#print i,
_p = {}
for L in ACGT: _p[L] = math.pow(2.0,self.logP[i][L])
P.append(_p)
#print
self.P = P | m._computeP() -- [utility] Compute the probability matrix (from the internal log-probability matrix) |
def _compute_bits(self):
bits = []
totbits = 0
bgbits = 0
bg = self.background
UNCERT = lambda x: x*math.log(x)/math.log(2.0)
for letter in ACGT:
bgbits = bgbits + UNCERT(bg[letter])
for i in range(self.width):
tot = 0
... | m._compute_bits() -- [utility] Set m.totbits to the number of bits and m.bits to a list of bits at each position |
def denoise(self,bitthresh=0.5):
for i in range(self.width):
tot = 0
for letter in ACGT:
if self.logP:
Pij = pow(2.0, self.logP[i][letter])
else:
Pij = pow(2.0, self.ll[i][letter]) * self.background[letter]
... | m.denoise(bitthresh=0.5) -- Set low-information positions (below bitthresh) to Ns |
def giflogo(self,id,title=None,scale=0.8,info_str=''):
return giflogo(self,id,title,scale) | m.giflogo(id,title=None,scale=0.8) -- (Requires seqlogo package) Make a gif sequence logo |
def _compute_ambig_ll(self):
for Dll in self.ll:
for L in one2two.keys():
Dll[L] = max(Dll[one2two[L][0]], Dll[one2two[L][1]] )
Dll['N'] = 0.0
Dll['B'] = 0.0 | m._compute_ambig_ll() -- Extend log-likelihood matrix to include ambiguity codes
e.g. What the score of a 'S'? Here we use the max of C and G. |
def compute_from_text(self,text,beta=0.001):
prevlett = {'B':'A', 'D':'C', 'V':'T', 'H':'G'}
countmat = []
text = re.sub('[\.\-]','N',text.upper())
for i in range(len(text)):
D = {'A': 0, 'C': 0, 'T':0, 'G':0}
letter = text[i]
if letter in ['B... | m.compute_from_text(,text,beta=0.001) -- Compute a matrix values from a text string of ambiguity codes.
Use Motif_from_text utility instead to build motifs on the fly. |
def new_bg(self,bg):
counts = []
for pos in self.logP:
D = {}
for L,lp in pos.items():
D[L] = math.pow(2.0,lp)
counts.append(D)
self.background = bg
self.compute_from_counts(counts,0) | m.new_bg(,bg) -- Change the ACGT background frequencies to those in the supplied dictionary.
Recompute log-likelihood, etc. with new background. |
def compute_from_counts(self,countmat,beta=0):
self.counts = countmat
self.width = len(countmat)
self.bgscale = 0
maxcount = 0
#Determine Biggest column
for col in countmat:
tot = 0
for v in col.values():
tot = tot + v
... | m.compute_from_counts(,countmat,beta=0) -- Utility function to build a motif object from a matrix of letter counts. |
def _compute_logP_from_ll(self):
log = math.log
logP = []
for i in range(self.width):
D = {}
for L in ACGT:
''' if ll = log(p/b) then
2^ll = p/b
and ll = log(p) - log(b)
so log(p) = l... | m._compute_logP_from_ll() -- Compute self's logP matrix from the self.ll (log-likelihood) |
def _print_ll(self):
print "# ",
for i in range(self.width):
print " %4d "%i,
print
for L in ['A', 'C', 'T', 'G']:
print "#%s "%L,
for i in range(self.width):
print "%8.3f "%self.ll[i][L],
print | m._print_ll() -- Print log-likelihood (scoring) matrix |
def _print_p(self):
print "# ",
for i in range(self.width):
print " %4d "%i,
print
for L in ['A', 'C', 'T', 'G']:
print "#%s "%L,
for i in range(self.width):
print "%8.3f "%math.pow(2,self.logP[i][L]),
print | m._print_p() -- Print probability (frequency) matrix |
def _print_counts(self):
print "# ",
for i in range(self.width):
print " %4d "%i,
print
for L in ['A', 'C', 'T', 'G']:
print "#%s "%L,
for i in range(self.width):
print "%8.3f "%self.counts[i][L],
print | m._print_counts() -- Print count matrix |
def _maxscore(self):
total = 0
lowtot= 0
for lli in self.ll:
total = total + max(lli.values())
lowtot= lowtot+ min(lli.values())
self.maxscore = total
self.minscore = lowtot | m._maxscore() -- Sets self.maxscore and self.minscore |
def _compute_threshold(self,z=2.0):
scoretally = []
for seq in self.seqs:
matches,endpoints,scores = self.scan(seq,-100)
scoretally.append(scores[0])
ave,std = avestd(scoretally)
self.threshold = ave - z *std | m._compute_threshold(z=2.0) -- For Motif objects assembled from a set of sequence,
compute a self.threshold with a z-score based on the distribution
of scores in among the original input sequences. |
def bestscanseq(self,seq):
matches,endpoints,scores = self.scan(seq,-100)
t = zip(scores,matches)
t.sort()
bestseq = t[-1][1]
bestscore = t[-1][0]
return bestscore, bestseq | m.bestscanseq(seq) -- Return score,sequence of the best match to the motif in the supplied sequence |
def bestscan(self,seq):
matches,endpoints,scores = self.scan(seq,-100)
if not scores: return -100
scores.sort()
best = scores[-1]
return best | m.bestscan(seq) -- Return the score of the best match to the motif in the supplied sequence |
def matchstartorient(self,seq, factor=0.7):
ans = []
txts,endpoints,scores = self.scan(seq,factor=factor)
for txt, startstop in zip(txts,endpoints):
start, stop = startstop
rctxt = revcomplement(txt)
orient = (self.bestscore(txt,1) >= self.bestscore(... | m.matchstartorient(,seq, factor=0.7) -- Returns list of (start,orientation) coordinate pairs of
matches to the motif in the supplied sequence. Factor
is multiplied by m.maxscore to get a match threshold. |
def scan(self, seq, threshold = '', factor=0.7):
if len(seq) < self.width:
return(self._scan_smaller(seq,threshold))
else:
return(self._scan(seq,threshold,factor=factor)) | m.scan(seq, threshold = '', factor=0.7) -- Scan the sequence. Returns three lists:
matching sequences, endpoints, and scores. The value of
'factor' is multiplied by m.maxscore to get a match threshold
... |
def scansum(self,seq,threshold = -1000):
ll = self.ll
sum = 0
width = self.width
width_r = range(width)
width_rcr = range(width-1,-1,-1)
width_ranges = zip(width_r,width_rcr)
seqcomp = seq.translate(revcompTBL)
total = 0
... | m.scansum(seq,threshold = -1000) -- Sum of scores over every window in the sequence. Returns
total, number of matches above threshold, average score, sum of exp(score) |
def score(self, seq, fwd='Y'):
matches, endpoints, scores = self._scan(seq,threshold=-100000,forw_only=fwd)
return scores[0] | m.score(seq, fwd='Y') -- Returns the score of the first w-bases of the sequence, where w is the motif width. |
def bestscore(self,seq, fwd=''):
matches, endpoints, scores = self._scan(seq,threshold=-100000,forw_only=fwd)
if scores: return max(scores)
else: return -1000 | m.bestscore(seq, fwd='') -- Returns the score of the best matching subsequence in seq. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.