prefix stringlengths 0 918k | middle stringlengths 0 812k | suffix stringlengths 0 962k |
|---|---|---|
# Copyright 2015 Adam Greenstein <adamgreenstein@comcast.net>
#
# Switcharoo Cartographer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Switcharoo Cartographer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with Switcharoo Cartographer. If not, see <http://www.gnu.org/licenses/>.
from data import Access, Entry, EntryError
from Queue import Queue
class EntryQueue(Queue):
def __init__(self, transverse, maxsize=0):
self.reddit = transverse.reddit
self.events = transverse.events
Queue.__init__(self, maxsize)
def _init(self, maxsize):
Queue._init(self, maxsize)
nodes = Access(self.events).get_entry(maxsize)
for node in nodes:
try:
self.queue.append(Entry(node['raw_url'], self.reddit))
self.events.on_adding_to_queue(node['raw_url'])
except EntryError:
# TODO Remove old entry from DB
pass
def _put(self, url):
try:
entry = Entry(url, self.reddit)
if self._is_unique(entry):
self.events.on_adding_to_queue(url)
self.queue.append(entry)
else:
self.events.on_not_adding_to_queue(url)
except EntryError:
self.ev | ents.on_not_adding_to_queue(url)
def _get(self):
return self.queue.popleft()
def _is_unique(self, entry):
# TODO Logic here to determine if new url found
if entry not in self.queue:
return Access(self.eve | nts).is_unique_entry(entry)
else:
return False |
class A:
def <weak_w | arning descr="Function name should be lowercase">fooBar</weak_warning>(self): pass
class B(A | ):
def fooBar(self): pass |
# Tweepy
# Copyright 2009-2010 Joshua Roesslein
# See LICENSE for details.
import httplib
import urllib
import time
import re
from tweepy.error import TweepError
from tweepy.utils import convert_to_utf8_str
from tweepy.models import Model
re_path_template = re.compile('{\w+}')
def bind_api(**config):
class APIMethod(object):
path = config['path']
payload_type = config.get('payload_type', None)
payload_list = config.get('payload_list', False)
allowed_param = config.get('allowed_param', [])
method = config.get('method', 'GET')
require_auth = config.get('require_auth', False)
search_api = config.get('search_api', False)
use_cache = config.get('use_cache', True)
def __init__(self, api, args, kargs):
# If authentication is required and no credentials
# are provided, throw an error.
if self.require_auth and not api.auth:
raise TweepError('Authentication required!')
self.api = api
self.post_data = kargs.pop('post_data', None)
self.retry_count = kargs.pop('retry_count', api.retry_count)
self.retry_delay = kargs.pop('retry_delay', api.retry_delay)
self.retry_errors = kargs.pop('retry_errors', api.retry_errors)
self.headers = kargs.pop('headers', {})
self.build_parameters(args, kargs)
# Pick correct URL root to use
if self.search_api:
self.api_root = api.search_root
else:
self.api_root = api.api_root
# Perform any path variable substitution
self.build_path()
if api.secure:
self.scheme = 'https://'
else:
self.scheme = 'http://'
if self.search_api:
self.host = api.search_host
else:
self.host = api.host
# Manually set Host header to fix an issue in python 2.5
# or older where Host is set including the 443 port.
# This causes Twitter to issue 301 redirect.
# See Issue https://github.com/tweepy/tweepy/issues/12
self.headers['Host'] = self.host
def build_parameters(self, args, kargs):
self.parameters = {}
for idx, arg in enumerate(args):
if arg is None:
continue
try:
self.parameters[self.allowed_param[idx]] = convert_to_utf8_str(arg)
except IndexError:
raise TweepError('Too many parameters supplied!')
for k, arg in kargs.items():
if arg is None:
continue
if k in self.parameters:
raise TweepError('Multiple values for parameter %s supplied!' % k)
self.parameters[k] = convert_to_utf8_str(arg)
def build_path(self):
for variable in re_path_template.findall(self.path):
name = variable.strip('{}')
if name == 'user' and 'user' not in self.parameters and self.api.auth:
# No 'user' parameter provided, fetch it from Auth instead.
value = self.api.auth.get_username()
else:
try:
value = urllib.quote(self.parameters[name])
except KeyError:
| raise TweepError('No parameter value found for path variable: %s' % name)
del self.parameters[name]
self.pa | th = self.path.replace(variable, value)
def execute(self):
# Build the request URL
url = self.api_root + self.path
if len(self.parameters):
url = '%s?%s' % (url, urllib.urlencode(self.parameters))
# Query the cache if one is available
# and this request uses a GET method.
if self.use_cache and self.api.cache and self.method == 'GET':
cache_result = self.api.cache.get(url)
# if cache result found and not expired, return it
if cache_result:
# must restore api reference
if isinstance(cache_result, list):
for result in cache_result:
if isinstance(result, Model):
result._api = self.api
else:
if isinstance(cache_result, Model):
cache_result._api = self.api
return cache_result
# Continue attempting request until successful
# or maximum number of retries is reached.
retries_performed = 0
while retries_performed < self.retry_count + 1:
# Open connection
# FIXME: add timeout
if self.api.secure:
conn = httplib.HTTPSConnection(self.host)
else:
conn = httplib.HTTPConnection(self.host)
# Apply authentication
if self.api.auth:
self.api.auth.apply_auth(
self.scheme + self.host + url,
self.method, self.headers, self.parameters
)
# Execute request
try:
conn.request(self.method, url, headers=self.headers, body=self.post_data)
resp = conn.getresponse()
except Exception, e:
raise TweepError('Failed to send request: %s' % e)
# Exit request loop if non-retry error code
if self.retry_errors:
if resp.status not in self.retry_errors: break
else:
if resp.status == 200: break
# Sleep before retrying request again
time.sleep(self.retry_delay)
retries_performed += 1
# If an error was returned, throw an exception
self.api.last_response = resp
if resp.status != 200:
try:
error_msg = self.api.parser.parse_error(resp.read())
except Exception:
error_msg = "Twitter error response: status code = %s" % resp.status
raise TweepError(error_msg, resp)
# Parse the response payload
result = self.api.parser.parse(self, resp.read())
conn.close()
# Store result into cache if one is available.
if self.use_cache and self.api.cache and self.method == 'GET' and result:
self.api.cache.store(url, result)
return result
def _call(api, *args, **kargs):
method = APIMethod(api, args, kargs)
return method.execute()
# Set pagination mode
if 'cursor' in APIMethod.allowed_param:
_call.pagination_mode = 'cursor'
elif 'page' in APIMethod.allowed_param:
_call.pagination_mode = 'page'
return _call
|
return dict_strip_unicode_keys(qs_filters)
def apply_sorting(self, obj_list, options=None):
"""
Given a dictionary of options, apply some ORM-level sorting to the
provided ``QuerySet``.
Looks for the ``sort_by`` key and handles either ascending (just the
field name) or descending (the field name with a ``-`` in front).
The field name should be the resource field, **NOT** model field.
"""
if options is None:
options = {}
if not 'sort_by' in options:
# Nothing to alter the sort order. Return what we've got.
return obj_list
order_by_args = []
if hasattr(options, 'getlist'):
sort_bits = options.getlist('sort_by')
else:
sort_bits = options.get('sort_by')
if not isinstance(sort_bits, (list, tuple)):
sort_bits = [sort_bits]
for sort_by in sort_bits:
sort_by_bits = sort_by.split(LOOKUP_SEP)
field_name = sort_by_bits[0]
order = ''
if sort_by_bits[0].startswith('-'):
field_name = sort_by_bits[0][1:]
order = '-'
if not field_name in self.fields:
# It's not a field we know about. Move along citizen.
raise InvalidSortError("No matching '%s' field for ordering on." % field_name)
if not field_name in self._meta.ordering:
raise InvalidSortError("The '%s' field does not allow ordering." % field_name)
if self.fields[field_name].attribute is None:
raise InvalidSortError("The '%s' field has no 'attribute' for ordering with." % field_name)
order_by_args.append("%s%s" % (order, LOOKUP_SEP.join([self.fields[field_name].attribute] + sort_by_bits[1:])))
return obj_list.order_by(*order_by_args)
def get_object_list(self, request):
"""
An ORM-specific implementation of ``get_object_list``.
Returns a queryset that may have been limited by authorization or other
overrides.
"""
base_object_list = self._meta.queryset
# Limit it as needed.
authed_object_list = self.apply_authorization_limits(request, base_object_list)
return authed_object_list
def obj_get_list(self, request=None, **kwargs):
"""
A ORM-specific implementation of ``obj_get_list``.
Takes an optional ``request`` object, whose ``GET`` dictionary can be
used to narrow the query.
"""
filters = None
if hasattr(request, 'GET'):
filters = request.GET
applicable_filters = self.build_filters(filters=filters)
try:
return self.get_object_list(request).filter(**applicable_filters)
except ValueError, e:
raise NotFound("Invalid resource lookup data provided (mismatched type).")
def obj_get(self, request=None, **kwargs):
"""
A ORM-specific implementation of ``obj_get``.
Takes optional ``kwargs``, which are used to narrow the query to find
the instance.
"""
try:
return self.get_object_list(request).get(**kwargs)
except ValueError, e:
raise NotFound("Invalid resource lookup data provided | (mismatched type).")
def obj_create(self, bundle, request=None, **kwargs):
"""
A ORM-specific implementation of ``obj_create``.
"""
bundle.obj = self._meta.object_class()
for key, value in kwargs.items():
setattr(bundle.obj, key, value)
bundle = self.full_hydr | ate(bundle)
bundle.obj.save()
# Now pick up the M2M bits.
m2m_bundle = self.hydrate_m2m(bundle)
self.save_m2m(m2m_bundle)
return bundle
def obj_update(self, bundle, request=None, **kwargs):
"""
A ORM-specific implementation of ``obj_update``.
"""
if not bundle.obj or not bundle.obj.pk:
# Attempt to hydrate data from kwargs before doing a lookup for the object.
# This step is needed so certain values (like datetime) will pass model validation.
try:
bundle.obj = self.get_object_list(request).model()
bundle.data.update(kwargs)
bundle = self.full_hydrate(bundle)
lookup_kwargs = kwargs.copy()
lookup_kwargs.update(dict(
(k, getattr(bundle.obj, k))
for k in kwargs.keys()
if getattr(bundle.obj, k) is not None))
except:
# if there is trouble hydrating the data, fall back to just
# using kwargs by itself (usually it only contains a "pk" key
# and this will work fine.
lookup_kwargs = kwargs
try:
bundle.obj = self.get_object_list(request).get(**lookup_kwargs)
except ObjectDoesNotExist:
raise NotFound("A model instance matching the provided arguments could not be found.")
bundle = self.full_hydrate(bundle)
bundle.obj.save()
# Now pick up the M2M bits.
m2m_bundle = self.hydrate_m2m(bundle)
self.save_m2m(m2m_bundle)
return bundle
def obj_delete_list(self, request=None, **kwargs):
"""
A ORM-specific implementation of ``obj_delete_list``.
Takes optional ``kwargs``, which can be used to narrow the query.
"""
self.get_object_list(request).filter(**kwargs).delete()
def obj_delete(self, request=None, **kwargs):
"""
A ORM-specific implementation of ``obj_delete``.
Takes optional ``kwargs``, which are used to narrow the query to find
the instance.
"""
try:
obj = self.get_object_list(request).get(**kwargs)
except ObjectDoesNotExist:
raise NotFound("A model instance matching the provided arguments could not be found.")
obj.delete()
def rollback(self, bundles):
"""
A ORM-specific implementation of ``rollback``.
Given the list of bundles, delete all models pertaining to those
bundles.
"""
for bundle in bundles:
if bundle.obj and getattr(bundle.obj, 'pk', None):
bundle.obj.delete()
def save_m2m(self, bundle):
"""
Handles the saving of related M2M data.
Due to the way Django works, the M2M data must be handled after the
main instance, which is why this isn't a part of the main ``save`` bits.
Currently slightly inefficient in that it will clear out the whole
relation and recreate the related data as needed.
"""
for field_name, field_object in self.fields.items():
if not getattr(field_object, 'is_m2m', False):
continue
if not field_object.attribute:
continue
# Get the manager.
related_mngr = getattr(bundle.obj, field_object.attribute)
if hasattr(related_mngr, 'clear'):
# Clear it out, just to be safe.
related_mngr.clear()
related_objs = []
for related_bundle in bundle.data[field_name]:
related_bundle.obj.save()
related_objs.append(related_bundle.obj)
related_mngr.add(*related_objs)
def get_resource_uri(self, bundle_or_obj):
"""
Handles generating a resource URI for a single resource.
Uses the model's ``pk`` in order to create the URI.
"""
kwargs = {
'resource_name |
from django.urls import r | e_path
from systextil.views import apoio_index
from systextil.views.table import (
deposito,
colecao,
estagio,
periodo_confeccao,
unidade,
)
urlpatterns = [
re_path(r'^colecao/$', colecao.view, name='colecao'),
re_path(r'^deposito/$', deposito.deposito, name='deposito'),
re_p | ath(r'^estagio/$', estagio.view, name='estagio'),
re_path(r'^periodo_confeccao/$', periodo_confeccao.view, name='periodo_confeccao'),
re_path(r'^unidade/$', unidade.view, name='unidade'),
]
|
##############################################################################
# Copyright (c) 2013-2018, Lawrence Livermore National Security, LLC.
# Produce | d at the Lawrence Livermore National Laboratory.
#
# This file is part of Spack.
# Created by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.
# LLNL-CODE-647188
#
# For details, see https://github.com/spack/spack
# Please also see the NOTICE and LICENSE files for our notice and the LGPL.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public | License (as
# published by the Free Software Foundation) version 2.1, February 1999.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
# conditions of the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################
from spack import *
class RFutileOptions(RPackage):
"""A scoped options management framework"""
homepage = "https://cran.rstudio.com/web/packages/futile.options/index.html"
url = "https://cran.rstudio.com/src/contrib/futile.options_1.0.0.tar.gz"
list_url = "https://cran.rstudio.com/src/contrib/Archive/futile.options"
version('1.0.0', '8fd845774bbce56f41f7c43c3b4c13ba')
|
import numpy as np
import pandas as pd
from bokeh import mpl
ts = pd.Series(np.random.randn | (1000), index=pd.date_range('1/1/2000', periods=1000))
ts = ts.cumsum()
df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list('ABCD'))
df = df.cumsum()
df.plot | (legend=False)
mpl.to_bokeh(name="dataframe")
|
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-04-04 22:45
from __future__ import unicode_literals
from django.d | b import migrations, models
class Migration(migrations.Migration):
| dependencies = [
('domain_api', '0009_remove_topleveldomain_slug'),
]
operations = [
migrations.AddField(
model_name='topleveldomain',
name='slug',
field=models.CharField(max_length=100, null=True),
),
]
|
'''
Created on | Nov 21, 2016
@author: julimatt
'' | '
from django import forms
|
from distuti | ls.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
ext_modules = [Extension("_nbt", ["_nbt.pyx"])]
import numpy
setup(
name = 'NBT library (Cython implementation)',
cmdclass | = {'build_ext': build_ext},
ext_modules = ext_modules,
include_dirs = numpy.get_include()
)
|
s ``True`` if ``lhs`` is a file name or file object in the project store."""
return (lhs in self._sourcefiles or
lhs in self._targetfiles or
lhs in self._transfiles or
lhs in self._files or
lhs in self._files.values())
# METHODS #
def append_file(self, afile, fname, ftype='trans', delete_orig=False):
"""Append the given file to the project with the given filename, marked
to be of type ``ftype`` ('src', 'trans', 'tgt').
:type delete_orig: bool
:param delete_orig: Whether or not the original (given) file should be
deleted after being appended. This is set to
``True`` by
:meth:`~translate.storage.project.convert_forward`
. Not used in this class.
"""
if ftype not in self.TYPE_INFO['f_prefix']:
raise ValueError('Invalid file type: %s' % (ftype))
if isinstance(afile, six.string_types) and os.path.isfile(afile) and not fname:
# Try and use afile as the file name
fname, afile = afile, open(afile)
# Check if we can get an real file name
realfname = fname
if realfname is None or not os.path.isfile(realfname):
realfname = getattr(afile, 'name', None)
if realfname is None or not os.path.isfile(realfname):
realfname = getattr(afile, 'filename', None)
if not realfname or not os.path.isfile(realfname):
realfname = None
# Try to get the file name from the file object, if it was not given:
if not fname:
fname = getattr(afile, 'name', None)
if not fname:
fname = getattr(afile, 'filename', None)
fname = self._fix_type_filename(ftype, fname)
if not fname:
raise ValueError('Could not deduce file name and none given')
if fname in self._files:
raise FileExistsInProjectError(fname)
if realfname is not None and os.path.isfile(realfname):
self._files[fname] = realfname
else:
self._files[fname] = afile
self.TYPE_INFO['lists'][ftype].append(fname)
return afile, fname
def append_sourcefile(self, afile, fname=None):
return self.append_file(afile, fname, ftype='src')
def append_targetfile(self, afile, fname=None):
return self.append_file(afile, fname, ftype='tgt')
def append_transfile(self, afile, fname=None):
return self.append_file(afile, fname, ftype='trans')
def remove_file(self, fname, ftype=None):
"""Remove the file with the given project name from the project. If
the file type ('src', 'trans' or 'tgt') is not given, it is guessed.
"""
if fname no | t in self._files:
rai | se FileNotInProjectError(fname)
if not ftype:
# Guess file type (source/trans/target)
for ft, prefix in self.TYPE_INFO['f_prefix'].items():
if fname.startswith(prefix):
ftype = ft
break
self.TYPE_INFO['lists'][ftype].remove(fname)
if self._files[fname] and hasattr(self._files[fname], 'close'):
self._files[fname].close()
del self._files[fname]
def remove_sourcefile(self, fname):
self.remove_file(fname, ftype='src')
def remove_targetfile(self, fname):
self.remove_file(fname, ftype='tgt')
def remove_transfile(self, fname):
self.remove_file(fname, ftype='trans')
def close(self):
self.save()
def get_file(self, fname, mode='rb'):
"""Retrieve the file with the given name from the project store.
The file is looked up in the ``self._files`` dictionary. The values
in this dictionary may be ``None``, to indicate that the file is not
cacheable and needs to be retrieved in a special way. This special
way must be defined in this method of sub-classes. The value may
also be a string, which indicates that it is a real file accessible
via ``open``.
:type mode: str
:param mode: The mode in which to re-open the file (if it is closed).
"""
if fname not in self._files:
raise FileNotInProjectError(fname)
rfile = self._files[fname]
if isinstance(rfile, six.string_types):
rfile = open(rfile, 'rb')
# Check that the file is actually open
if getattr(rfile, 'closed', False):
rfname = fname
if not os.path.isfile(rfname):
rfname = getattr(rfile, 'name', None)
if not rfile or not os.path.isfile(rfname):
rfname = getattr(rfile, 'filename', None)
if not rfile or not os.path.isfile(rfname):
raise IOError('Could not locate file: %s (%s)' % (rfile, fname))
rfile = open(rfname, mode)
self._files[fname] = rfile
return rfile
def get_filename_type(self, fname):
"""Get the type of file ('src', 'trans', 'tgt') with the given name."""
for ftype in self.TYPE_INFO['lists']:
if fname in self.TYPE_INFO['lists'][ftype]:
return ftype
raise FileNotInProjectError(fname)
def get_proj_filename(self, realfname):
"""Try and find a project file name for the given real file name."""
for fname in self._files:
if fname == realfname or self._files[fname] == realfname:
return fname
raise ValueError('Real file not in project store: %s' % (realfname))
def load(self, *args, **kwargs):
"""Load the project in some way. Undefined for this (base) class."""
pass
def save(self, filename=None, *args, **kwargs):
"""Save the project in some way. Undefined for this (base) class."""
pass
def update_file(self, pfname, infile):
"""Remove the project file with name ``pfname`` and add the contents
from ``infile`` to the project under the same file name.
:returns: the results from :meth:`ProjectStore.append_file`.
"""
ftype = self.get_filename_type(pfname)
self.remove_file(pfname)
self.append_file(infile, pfname, ftype)
def _fix_type_filename(self, ftype, fname):
"""Strip the path from the filename and prepend the correct prefix."""
path, fname = os.path.split(fname)
return self.TYPE_INFO['f_prefix'][ftype] + fname
def _generate_settings(self):
"""@returns A XML string that represents the current settings."""
xml = etree.Element('translationproject')
# Add file names to settings XML
if self._sourcefiles:
sources_el = etree.Element('sources')
for fname in self._sourcefiles:
src_el = etree.Element('filename')
src_el.text = fname
sources_el.append(src_el)
xml.append(sources_el)
if self._transfiles:
transfiles_el = etree.Element('transfiles')
for fname in self._transfiles:
trans_el = etree.Element('filename')
trans_el.text = fname
transfiles_el.append(trans_el)
xml.append(transfiles_el)
if self._targetfiles:
target_el = etree.Element('targets')
for fname in self._targetfiles:
tgt_el = etree.Element('filename')
tgt_el.text = fname
target_el.append(tgt_el)
xml.append(target_el)
# Add conversion mappings
if self.convert_map:
conversions_el = etree.Element('conversions')
for in_fname, (out_fname, templ_fname) in six.iteritems(self.convert_map):
if in_fname not in self._files or out_fname not in self._files:
continue
conv_el = etree.Element('conv')
input_el = etree.Element('input')
input_el.text = in_fname
conv_el.append(input_el)
|
from myhdl import *
from str_builder import StrBuilder
import os
import sys
class GenWrapperFile(object):
'''|
| This class is used to generate a python wrapper of a verilog design
|________'''
def __init__(self):
self.module_name = ''
self.interface = [] # keeps the order of the declarations
self.parameters = []
self.inputs = []
self.outputs = []
def generateWrapperFile(self):
'''|
| Generate <module_name_wrapper>.py file
|________'''
print "\nGenerating .py wrapper file."
s = StrBuilder()
self.genWrapperInterface(s)
s += '# Need this in order to work...\n'
# We assume that the clock is 'clk'!!!!!
s += '@always(clk.posedge)\n'
s += 'def pass_thru():\n'
s += s.indent() + 'pass\n\n'
s.dedent()
self.genTheWrapper(s)
self.genConvertFunc(s)
filename = self.module_name + '_wrp.py'
s.write(filename)
def genWrapperInterface(self, s):
'''|
| Generate the interfaces of the wrapper file
|________'''
s += 'from myhdl import *\n\n'
s += 'def ' + self.module_name + '_wrapper(\n'
s.indent(5)
for i in self.interface: # to preserve the order
if i['type'] != 'parameter':
s += i['name'] + ',\n'
s+= 'INST_NAME = "' + self.module_name.upper() + '",\n'
for p in self.parameters:
s += p['name'] + ' = ' + p['value'] + ',\n'
s -= 2
s += s.noIndent() + '):\n\n'
s.dedent(3)
def genTheWrapper(self, s, py_name=None):
'''|
| Generate the wrapper
|________'''
s += '#---------------------------------------------#\n'
s += '# Define the i | nterface to the verilog ip core #\n'
s += '#---------------------------------------------#\n'
str_name = self.module_name i | f py_name == None else py_name
s += str_name + '_wrp.verilog_code = \\' + '\n'
if self.parameters != []:
s += '"""' + self.module_name + '#(\\n""" + \\' + '\n'
for p in self.parameters:
s += '""" .' + p["name"] + '($' + p["name"] + '),\\n""" + \\' + '\n'
s = s-11 + (s.noIndent() + '\\n""" + \\' + '\n')
s += '""" ) $INST_NAME (\\n""" + \\' + '\n'
else:
s += '"""' + self.module_name + ' $INST_NAME (\\n""" + \\' + '\n'
for i in self.interface:
if i["type"] != "parameter":
s += '""" .' + i["name"] + '($' + i["name"] + '),\\n""" + \\' + '\n'
s = s-11 + (s.noIndent() + '\\n""" + \\' + '\n')
s += '""");"""\n\n'
s += '#-------------------------------------------------------#\n'
s += '# output, needed when converting the wrapper to verilog #\n'
s += '#-------------------------------------------------------#\n'
for o in self.outputs:
s += o["name"] + '.driven = "wire"\n'
s += '\n'
s += 'return pass_thru\n\n\n'
def genConvertFunc(self, s):
'''|
| Generate function convert()
|________'''
s += s.noIndent() + 'def convert():\n\n'
for i in self.parameters:
s += i['name'] + ' = ' + i['value'] + '\n'
s += '\n'
# Declare signals
for i in self.interface:
if i['type'] != 'parameter':
x = i["size"]
stype = 'bool(0)'
if x.startswith('['):
stype = 'intbv(0)'
x = x.replace(":0", "+1:")
s += i['name'] + '= Signal(' + stype + x + ')\n'
s += '\n'
s += 'toVerilog(' + self.module_name + '_wrapper,\n'
s.indent(2)
for i in self.interface:
if i['type'] != 'parameter':
s += i['name'] + ' = ' + i['name'] + ',\n'
for p in self.parameters:
s += p['name'] + ' = ' + p['name'] + ',\n'
s = s-2 + (s.noIndent() + ' )\n\n\n')
s.dedent(4)
s += 'if __name__ == "__main__":\n'
s += s.indent() + 'convert()\n'
def initialize(self, filename):
'''|
| Initialize the GenWrapperFile object
|________'''
with open(filename) as f:
for line_number, line in enumerate(f):
w_list = line.split()
if w_list != []:
if w_list[0]=='module':
self.module_name = w_list[1]
elif w_list[0] == 'parameter':
name = w_list[1].replace(',','')
value = w_list[3].replace(',','')
self.parameters.append( {'name':name, 'value':value})
self.interface.append( {'name':name, 'value':value, 'type':w_list[0]})
elif w_list[0] == 'input' or w_list[0] == 'output':
name = w_list[3] if w_list[2].startswith('[') else w_list[2]
size = w_list[2] if w_list[2].startswith('[') else ''
if w_list[0] == 'input':
self.inputs.append( {'name':name.replace(',',''), 'size':size})
elif w_list[0] == 'output':
self.outputs.append({'name':name.replace(',',''), 'size':size})
self.interface.append( {'name':name.replace(',',''), 'size':size, 'type':w_list[0]})
elif w_list[0] == ');':
break
def main( args ):
gwf = GenWrapperFile()
gwf.initialize(args.file_name)
gwf.generateWrapperFile()
import argparse
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Create a myhdl wrapper file.')
parser.add_argument('-f', '--file', dest='file_name', default="",
help='top-level verilog (.v) file')
args = parser.parse_args()
main( args ) |
# encoding: utf8
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('letter_ | to_editor', '0004_company_newspaper_webcache_wikipediapage'),
]
operations = [
migrations.AddField(
model_name='newspaper',
name='sister_newspapers',
field=models.ForeignKey(to='letter_to_editor.Newspaper', to_field='newspaper_name') | ,
preserve_default=True,
),
]
|
import itertools
import pickle
import numpy as np
from numpy.testing import assert_array_almost_equal
import pytest
from scipy.spatial.distance import cdist
from sklearn.neighbors import DistanceMetric
from sklearn.neighbors import BallTree
from sklearn.utils import check_random_state
from sklearn.utils._testing import create_memmap_backed_data
from sklearn.utils.fixes import sp_version, parse_version
def dist_func(x1, x2, p):
return np.sum((x1 - x2) ** p) ** (1.0 / p)
rng = check_random_state(0)
d = 4
n1 = 20
n2 = 25
X1 = rng.random_sample((n1, d)).astype("float64", copy=False)
X2 = rng.random_sample((n2, d)).astype("float64", copy=False)
[X1_mmap, X2_mmap] = create_memmap_backed_data([X1, X2])
# make boolean arrays: ones and zeros
X1_bool = X1.round(0)
X2_bool = X2.round(0)
[X1_bool_mmap, X2_bool_mmap] = create_memmap_backed_data([X1_bool, X2_bool])
V = rng.random_sample((d, d))
VI = np.dot(V, V.T)
BOOL_METRICS = [
"matching",
"jaccard",
"dice",
"kulsinski",
"rogerstanimoto",
"russellrao",
"sokalmichener",
"sokalsneath",
]
METRICS_DEFAULT_PARAMS = {
"euclidean": {},
"cityblock": {},
"minkowski": dict(p=(1, 1.5, 2, 3)),
"chebyshev": {},
"seuclidean": dict(V=(rng.random_sample(d),)),
"wminkowski": dict(p=(1, 1.5, 3), w=(rng.random_sample(d),)),
"mahalanobis": dict(VI=(VI,)),
"hamming": {},
"canberra": {},
"braycurtis": {},
}
@pytest.mark.parametrize("metric", METRICS_DEFAULT_PARAMS)
@pytest.mark.parametrize("X1, X2", [(X1, X2), (X1_mmap, X2_mmap)])
def test_cdist(metric, X1, X2):
argdict = METRICS_DEFAULT_PARAMS[metric]
keys = argdict.keys()
for vals in itertools.product(*argdict.values()):
kwargs = dict(zip(keys, vals))
if metric == "mahalanobis":
# See: https://github.com/scipy/scipy/issues/13861
pytest.xfail("scipy#13861: cdist with 'mahalanobis' fails onmemmap data")
elif metric == "wminkowski":
if sp_version >= parse_version("1.8.0"):
pytest.skip("wminkowski will be removed in SciPy 1.8.0")
# wminkoski is deprecated in SciPy 1.6.0 and removed in 1.8.0
ExceptionToAssert = None
if sp_version >= parse_version("1.6.0"):
ExceptionToAssert = DeprecationWarning
with pytest.warns(ExceptionToAssert):
D_true = cdist(X1, X2, metric, **kwargs)
else:
D_true = cdist(X1, X2, metric, **kwargs)
check_cdist(metric, kwargs, D_true)
@pytest.mark.parametrize("metric", BOOL_METRICS)
@pytest.mark.parametrize(
"X1_bool, X2_bool", [(X1_bool, X2_bool), (X1_bool_mmap, X2_bool_mmap)]
)
def test_cdist_bool_metric(metric, X1_bool, X2_bool):
D_true = cdist(X1_bool, X2_bool, metric)
check_cdist_bool(metric, D_true)
def check_cdist(metric, kwargs, D_true):
dm = DistanceMetric.get_metric(metric, **kwargs)
D12 = dm.pairwise(X1, X2)
assert_array_almost_equal(D12, D_true)
def check_cdist_bool(metric, D_true):
dm = DistanceMetric.get_metric(metric)
D12 = dm.pairwise(X1_bool, X2_bool)
assert_array_almost_equal(D12, D_true)
@pytest.mark.parametrize("metric", METRICS_DEFAULT_PARAMS)
@pytest.mark.parametrize("X1, X2", [(X1, X2), (X1_mmap, X2_mmap)])
def test_pdist(metric, X1, X2):
argdict = METRICS_DEFAULT_PARAMS[metric]
keys = argdict.keys()
for vals in itertools.product(*argdict.values()):
kwargs = dict(zip(keys, vals))
if metric == "mahalanobis":
# See: https://github.com/scipy/scipy/issues/13861
pytest.xfail("scipy#13861: pdist with 'mahalanobis' fails onmemmap data")
elif metric == "wminkowski":
if sp_version >= parse_version("1.8.0"):
pytest.skip("wminkowski will be removed in SciPy 1.8.0")
# wminkoski is deprecated in SciPy 1.6.0 and removed in 1.8.0
ExceptionToAssert = None
if sp_version >= parse_version("1.6.0"):
ExceptionToAssert = DeprecationWarning
with pytest.warns(ExceptionToAssert):
D_true = cdist(X1, X1, metric, **kwargs)
else:
D_true = cdist(X1, X1, metric, **kwargs)
check_pdist(metric, kwargs, D_true)
@pytest.mark.parametrize("metric", BOOL_METRICS)
@pytest.mark.parametrize("X1_bool", [X1_bool, X1_bool_mmap])
def test_pdist_bool_metrics(metric, X1_bool):
D_true = cdist(X1_bool, X1_bool, metric)
check_pdist_bool(metric, D_true)
def check_pdist(metric, kwargs, D_true):
dm = DistanceMetric.get_metric(metric, **kwargs)
D12 = dm.pairwise(X1)
assert_array_almost_equal(D12, D_true)
def check_pdist_bool(metric, D_true):
dm = DistanceMetric.get_metric(metric)
D12 = dm.pairwise(X1_bool)
# Based on https://github.com/scipy/scipy/pull/7373
# When comparing two all-zero vectors, scipy>=1.2.0 jaccard metric
# was changed to return 0, instead of nan.
if metric == "jaccard" and sp_version < parse_version("1.2.0"):
D_true[np.isnan(D_true)] = 0
assert_array_almost_equal(D12, D_true)
@pytest.mark.parametrize("metric", METRICS_DEFAULT_PARAMS)
def test_pickle(metric):
argdict = METRICS_DEFAULT_PARAMS[metric]
keys = argdict.keys()
for vals in itertools.product(*argdict.values()):
kwargs = dict(zip(keys, vals))
check_pickle(metric, kwargs)
@pytest.mark.parametrize("metric", BOOL_METRICS)
@pytest.mark.parametrize("X1_bool", [X1_bool, X1_bool_mmap])
def test_pickle_bool_metrics(metric, X1_bool):
dm = DistanceMetric.get_metric(metric)
D1 = dm.pairwise(X1_bool)
dm2 = pickle.loads(pickle.dumps(dm))
D2 = dm2.pairwise(X1_bool)
assert_array_almost_equal(D1, D2)
def check_pickle(metric, kwargs):
dm = DistanceMetric.get_metric(metric, **kwargs)
D1 = dm.pairwise(X1)
dm2 = pickle.loads(pickle.dumps(dm))
D2 = dm2.pairwise(X1)
assert_array_almost_equal(D1, D2)
def test_haversine_metric():
def haversine_slow(x1, x2):
return 2 * np.arcsin(
np.sqrt(
np.sin(0.5 * (x1[0] - x2[0])) ** 2
+ np.cos(x1[0]) * np.cos(x2[0]) * np.sin(0.5 * (x1[1] - x2[1])) ** 2
)
)
X = np.random.random((10, 2))
haversine = DistanceMetric.get_metric("haversine")
D1 = haversine.pairwise(X)
D2 = np.zeros_like(D1)
for i, x1 in enumerate(X):
for j, x2 in enumerate(X):
D2[i, j] = haversine_slow(x1, x2)
assert_array_almost_equal(D1, D2)
assert_array_almost_equal(haversine.dist_to_rdist(D1), np.sin(0.5 * D2) ** 2)
def test_pyfunc_metric():
X = np.random.random((10, 3))
euclidean = DistanceMetric.get_metric("euclidean")
pyfunc = DistanceMetric.get_metric("pyfunc", func=dist_func, p=2)
# Check if both callable metric and predefined metric initialized
# DistanceMetric object is picklable
euclidean_pkl = pickle.loads(pickle.dumps(euclidean))
pyfunc_pkl = pickle.loads(pickle.dumps(pyfunc))
D1 = euclidean.pairwise(X)
D2 = pyfunc.pairwise(X)
D1_pkl = euclidean_pkl.pairwise(X)
D2_pkl = pyfunc_pkl.pairwise(X)
assert_array_almost_equal(D1, D2)
assert_array_almost_equal(D1_pkl, D2_pkl)
def test_bad_pyfunc_metric():
def wrong_distance(x, y):
return "1"
X = np.ones((5, 2))
msg = "Custom distance function must accept two vectors"
with pytest.raises(TypeError, match=msg):
BallTree(X, metric=wrong_distance)
def test_input_data_size():
# Regression test for #6288
# Previously, a metric requiring a particular input dimension would fail
def custom_metric(x, y):
assert x.shape[0] == 3
return np.sum((x - y) ** 2)
rng | = check_random_state(0)
X = rng.rand(10, 3)
pyfunc = DistanceMetric.get_metric("pyfunc", func=custom_metric)
eucl = DistanceMetric.get_metric("euclidean")
asser | t_array_almost_equal(pyfunc.pairwise(X), eucl.pairwise(X) ** 2)
|
#!/usr/bin/python3
import exhibition
import inputdevice
import data
from levels import level1
import os
import pygame
import logging
log = logging.getLogger(__name__)
class Engine:
""" Main class responsible for running the game.
Controls game setup and runs the main loop.
Passes input to game and handles the event queue. """
def __init__(self):
""" Creates the display surface and loads the game assets. """
pygame.init()
log.info("Initializing display surface at {}x{}".format(
data.SCREEN_RESOLUTION[0], data.SCREEN_RESOLUTION[1]))
self.screen = pygame.display.set_mode(data.SCREEN_RESOLUTION)
pygame.display.set_caption("digital heist")
# load image resources
exhibition.images(os.path.join(data.DATA_DIR, "images"))
| exhibi | tion.optimize()
self.level = level1.Level1()
self.input = inputdevice.KeyboardInput()
def run(self):
""" Starts the game and runs the main game loop. """
self.main_loop()
pygame.quit()
def main_loop(self):
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
if event.type == pygame.KEYUP:
if event.key == pygame.K_ESCAPE:
return
# game update
self.input.update()
self.level.process_input(self.input)
complete = self.level.update()
self.screen.fill((0, 0, 0))
self.level.render()
pygame.display.flip()
# noinspection PyUnusedLocal,PyUnusedLocal
ms = clock.tick(60)
if complete:
break
|
# Copyright 2018 The Exoplanet ML Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Functions for computing evaluation metrics."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
def _metric_variable(name, shape, dtype):
"""Creates a Variable in LOCAL_VARIABLES and METRIC_VARIABLES collections."""
return tf.Variable(
initial_value=tf.zeros(shape, dtype),
trainable=False,
collections=[tf.GraphKeys.LOCAL_VARIABLES, tf.GraphKeys.METRIC_VARIABLES],
name=name)
def _build_metrics(labels, predictions, weights, batch_losses, output_dim=1):
"""Builds TensorFlow operations to compute model evaluation metrics.
Args:
labels: Tensor with shape [batch_size].
predictions: Tensor with shape [batch_size, output_dim].
weights: Tensor with shape [batch_size].
batch_losses: Tensor with shape [batch_size].
output_dim: Dimension of model output
Returns:
A dictionary {metric_name: (metric_value, update_op).
"""
# Compute the predicted labels.
assert len(predictions.shape) == 2
binary_classification = output_dim == 1
if binary_classification:
assert predictions.shape[1] == 1
predictions = tf.squeeze(predictions, axis=[1])
predicted_labels = tf.cast(
tf.greater(predictions, 0.5), tf.int32, name="predicted_labels")
else:
predicted_labels = tf.argmax(
predictions, 1, name="predicted_labels", output_type=tf.int32)
metrics = {}
with tf.name_scope("metrics"):
# Total number of examples.
num_examples = _metric_variable("num_examples", [], tf.float32)
update_num_examples = tf.assign_add(num_examples, tf.reduce_sum(weights))
metrics["num_examples"] = (num_examples.read_value(), update_num_examples)
# Accuracy metrics.
num_correct = _metric_variable("num_correct", [], tf.float32)
is_correct = tf.equal(labels, predicted_labels)
weighted_is_correct = weights * tf.cast(is_correct, tf.float32)
update_num_correct = tf.assign_add(num_correct,
tf.reduce_sum(weighted_is_correct))
metrics["accuracy/num_correct"] = (num_correct.read_value(),
update_num_correct)
accuracy = tf.div(num_correct, num_examples, name="accuracy")
metrics["accuracy/accuracy"] = (accuracy, tf.no_op | ())
# Weighted cross-entropy loss.
metrics["losses/weighted_cross_entropy"] = tf.metrics.mean(
batch_losses, weights=weights, name="cross_entropy_loss")
def _count_condition(name, labels_value, predicted_value):
"""Creates a counter for given values of predictions and labels."""
count = _metric_variable(name, [], tf.float32)
is_equal = tf.logical_and(
tf.equal(labels, labels_value),
| tf.equal(predicted_labels, predicted_value))
weighted_is_equal = weights * tf.cast(is_equal, tf.float32)
update_op = tf.assign_add(count, tf.reduce_sum(weighted_is_equal))
return count.read_value(), update_op
# Confusion matrix metrics.
num_labels = 2 if binary_classification else output_dim
for gold_label in range(num_labels):
for pred_label in range(num_labels):
metric_name = "confusion_matrix/label_{}_pred_{}".format(
gold_label, pred_label)
metrics[metric_name] = _count_condition(
metric_name, labels_value=gold_label, predicted_value=pred_label)
# Possibly create AUC metric for binary classification.
if binary_classification:
labels = tf.cast(labels, dtype=tf.bool)
metrics["auc"] = tf.metrics.auc(
labels, predictions, weights=weights, num_thresholds=1000)
return metrics
def create_metric_fn(model):
"""Creates a tuple (metric_fn, metric_fn_inputs).
This function is primarily used for creating a TPUEstimator.
The result of calling metric_fn(**metric_fn_inputs) is a dictionary
{metric_name: (metric_value, update_op)}.
Args:
model: Instance of AstroModel.
Returns:
A tuple (metric_fn, metric_fn_inputs).
"""
weights = model.weights
if weights is None:
weights = tf.ones_like(model.labels, dtype=tf.float32)
metric_fn_inputs = {
"labels": model.labels,
"predictions": model.predictions,
"weights": weights,
"batch_losses": model.batch_losses,
}
def metric_fn(labels, predictions, weights, batch_losses):
return _build_metrics(
labels,
predictions,
weights,
batch_losses,
output_dim=model.hparams.output_dim)
return metric_fn, metric_fn_inputs
def create_metrics(model):
"""Creates a dictionary {metric_name: (metric_value, update_op)}.
This function is primarily used for creating an Estimator.
Args:
model: Instance of AstroModel.
Returns:
A dictionary {metric_name: (metric_value, update_op).
"""
metric_fn, metric_fn_inputs = create_metric_fn(model)
return metric_fn(**metric_fn_inputs)
|
weakened, flagged = set(), set()
infected = {(i,j) for i,a in enu | merate(open('22.in')) for j,b in enumerate(a.strip('\n')) if b=='#'}
s = len(open('22.in').readlines())/2
p = (s,s)
total = 0
d = (-1,0)
for _ in xrange(10**7):
if p in infected:
d = d[1], -1*d[0]
infected.remove(p)
flagged.ad | d(p)
elif p in weakened:
total += 1
weakened.remove(p)
infected.add(p)
elif p in flagged:
d = -1*d[0], -1*d[1]
flagged.remove(p)
else:
d = -1*d[1], d[0]
weakened.add(p)
p = p[0]+d[0], p[1]+d[1]
print total |
from oscarbluelight.ba | sket_utils import BluelightLineOfferConsumer a | s LineOfferConsumer
__all__ = [
"LineOfferConsumer",
]
|
# -*- coding: utf-8 -*-
from south.utils import datetime_utils as datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
class Migration(SchemaMigration):
def forwards(self, orm):
# Adding model 'Feedback'
db.create_table('feedback_form_feedback', (
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
('user', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['auth.User'], null=True)),
('name', self.gf('django.db.models.fields.CharField')(max_length=100)),
('email', self.gf('django.db.models.fields.EmailField')(max_length=75)),
('body', self.gf('django.db.models.fields.TextField')()),
('sent_time', self.gf('django.db.models.fields.DateTimeField')(auto_now_add=True, blank=True)),
))
db.send_create_signal('feedback_form', ['Feedback'])
def backwards(self, orm):
# Deleting model 'Feedback'
db.delete_table('feedback_form_feedback')
models = {
'auth.group': {
'Meta': {'object_name': 'Group'},
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
},
'auth.permission': {
'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
},
'auth.user': {
'Meta': {'object_name': 'User'},
'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
},
'contenttypes.contenttype': {
'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
| 'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
},
| 'feedback_form.feedback': {
'Meta': {'object_name': 'Feedback'},
'body': ('django.db.models.fields.TextField', [], {}),
'email': ('django.db.models.fields.EmailField', [], {'max_length': '75'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'sent_time': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']", 'null': 'True'})
}
}
complete_apps = ['feedback_form'] |
# -*- coding: utf-8 -*-
import re
from mobify.source import MobifySource
class OReillySource(MobifySource):
HEADER = u"""
<h1>{title}</h1>
<p><strong>{lead}</strong></p>
<p><small>{author} @ oreilly.com</small><br></p>
"""
@staticmethod
def is_my_url(url):
# https://www.ore | illy.com/ideas/the-evolution-of-devops
return 'oreilly.com/ideas/' in url
def get_inner_html(self):
article = self.xpath('//*[@itemprop="articleBody"]')
xpaths = [
'aside',
'div',
'figure[@class]',
]
# clean up the HTML
article = self.remove_nodes(article, xpaths)
html = self.get_node_html(article)
| return html
def get_html(self):
# add a title and a footer
return '\n'.join([
self.HEADER.format(title=self.get_title(), author=self.get_author(), lead=self.get_lead()).strip(),
self.get_inner_html()
]).strip()
def get_title(self):
# <meta property="og:title" content="Radio w Poznaniu rozpoczęło nadawanie 90 lat temu" />
return self.get_node('//meta[@property="og:title"]', attr='content').strip()
def get_lead(self):
# <meta property="og:description" content="90 lat temu, 24 kwietnia 1927 roku nadawanie rozpoczęła..." />
lead = self.get_node('//meta[@property="og:description"]', attr='content').strip()
return lead.strip() if lead else ''
def get_author(self):
return self.get_node('//meta[@property="article:author"]', attr='content').strip()
def get_language(self):
return 'en'
|
generating PDIP models in X3D format
#
# from https://bitbucket.org/hyOzd/freecad-macros
# author hyOzd
# This is a
# Dimensions are from Microchips Packaging Specification document:
# DS00000049BY. Body drawing is the same as QFP generator#
## requirements
## cadquery FreeCAD plugin
## https://github.com/jmwright/cadquery-freecad-module
## to run the script just do: freecad main_generator.py modelName
## e.g. c:\freecad\bin\freecad main_generator.py DIP8
## the script will generate STEP and VRML parametric models
## to be used with kicad StepUp script
#* These are a FreeCAD & cadquery tools *
#* to export generated models in STEP & VRML format. *
#* *
#* cadquery script for generating QFP/SOIC/SSOP/TSSOP models in STEP AP214 *
#* Copyright (c) 2015 *
#* Maurice https://launchpad.net/~easyw *
#* All trademarks within this guide belong to their legitimate owners. *
#* *
#* This program is free software; you can redistribute it and/or modify *
#* it under the terms of the GNU Lesser General Public License (LGPL) *
#* as published by the Free Software Foundation; either version 2 of *
#* the License, or (at your option) any later version. *
#* for detail see the LICENCE text file. *
#* *
#* This program is distributed in the hope that it will be useful, *
#* but WITHOUT ANY WARRANTY; without even the implied warranty of *
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
#* GNU Library General Public License for more details. *
#* *
#* You should have received a copy of the GNU Library General Public *
#* License along with this program; if not, write to the Free Software *
#* Foundation, Inc., *
#* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA *
#* *
#****************************************************************************
__title__ = "make Valve 3D models"
__author__ = "Stefan, based on Valve script"
__Comment__ = 'make varistor 3D models exported to STEP and VRML for Kicad StepUP script'
___ver___ = "1.3.3 14/08/2015"
# maui import cadquery as cq
# maui from Helpers import show
from collections import namedtuple
import math
import sys, os
import datetime
from datetime import datetime
sys.path.append("../_tools")
import exportPartToVRML as expVRML
import shaderColors
# maui start
import FreeCAD, Draft, FreeCADGui
import ImportGui
import FreeCADGui as Gui
#from Gui.Command import *
outdir=os.path.dirname(os.path.realpath(__file__) + os.sep + '..' + os.sep + '_3Dmodels')
scriptdir=os.path.dirname(os.path.realpath(__file__))
sys.path.append(outdir)
sys.path.append(scriptdir)
if FreeCAD.GuiUp:
from PySide import QtCore, QtGui
# Licence information of the generated models.
#################################################################################################
STR_licAuthor = "kicad StepUp"
STR_licEmail = "ksu"
STR_licOrgSys = "kicad StepUp"
STR_licPreProc = "OCC"
STR_licOrg = "FreeCAD"
############################################################################# | ####################
import cq_belfuse # modules parameters
from cq_belfuse import *
import cq_keystone # modules parameters
from cq_keystone import *
import cq_bulgin # modules parameters
from cq_bulgin import *
import cq_schurter # modules parameters
from cq_schurter import *
import cq_tme # modul | es parameters
from cq_tme import *
import cq_littlefuse # modules parameters
from cq_littlefuse import *
different_models = [
cq_belfuse(),
cq_keystone(),
cq_bulgin(),
cq_schurter(),
cq_tme(),
cq_littlefuse(),
]
def make_3D_model(models_dir, model_class, modelID):
LIST_license = ["",]
CheckedmodelName = 'A_' + modelID.replace('.', '').replace('-', '_').replace('(', '').replace(')', '')
CheckedmodelName = CheckedmodelName
Newdoc = App.newDocument(CheckedmodelName)
App.setActiveDocument(CheckedmodelName)
Gui.ActiveDocument=Gui.getDocument(CheckedmodelName)
destination_dir = model_class.get_dest_3D_dir(modelID)
material_substitutions = model_class.make_3D_model(modelID)
modelName = model_class.get_model_name(modelID)
doc = FreeCAD.ActiveDocument
doc.Label = CheckedmodelName
objs=GetListOfObjects(FreeCAD, doc)
objs[0].Label = CheckedmodelName
restore_Main_Tools()
script_dir=os.path.dirname(os.path.realpath(__file__))
expVRML.say(models_dir)
out_dir=models_dir+os.sep+destination_dir
if not os.path.exists(out_dir):
os.makedirs(out_dir)
exportSTEP(doc, modelName, out_dir)
if LIST_license[0]=="":
LIST_license=Lic.LIST_int_license
LIST_license.append("")
Lic.addLicenseToStep(out_dir + os.sep, modelName+".step", LIST_license,\
STR_licAuthor, STR_licEmail, STR_licOrgSys, STR_licOrg, STR_licPreProc)
# scale and export Vrml model
scale=1/2.54
#exportVRML(doc,modelName,scale,out_dir)
del objs
objs=GetListOfObjects(FreeCAD, doc)
expVRML.say("######################################################################")
expVRML.say(objs)
expVRML.say("######################################################################")
export_objects, used_color_keys = expVRML.determineColors(Gui, objs, material_substitutions)
export_file_name=out_dir+os.sep+modelName+'.wrl'
colored_meshes = expVRML.getColoredMesh(Gui, export_objects , scale)
#expVRML.writeVRMLFile(colored_meshes, export_file_name, used_color_keys)# , LIST_license
expVRML.writeVRMLFile(colored_meshes, export_file_name, used_color_keys, LIST_license)
#scale=0.3937001
#exportVRML(doc,modelName,scale,out_dir)
# Save the doc in Native FC format
saveFCdoc(App, Gui, doc, modelName,out_dir)
#display BBox
Gui.activateWorkbench("PartWorkbench")
Gui.SendMsgToActiveView("ViewFit")
Gui.activeDocument().activeView().viewAxometric()
#FreeCADGui.ActiveDocument.activeObject.BoundingBox = True
def run():
## # get variant names from command line
return
#import step_license as L
import add_license as Lic
# when run from command line
if __name__ == "__main__" or __name__ == "main_generator":
FreeCAD.Console.PrintMessage('\r\nRunning...\r\n')
full_path=os.path.realpath(__file__)
expVRML.say(full_path)
scriptdir=os.path.dirname(os.path.realpath(__file__))
expVRML.say(scriptdir)
sub_path = full_path.split(scriptdir)
expVRML.say(sub_path)
sub_dir_name =full_path.split(os.sep)[-2]
expVRML.say(sub_dir_name)
sub_path = full_path.split(sub_dir_name)[0]
expVRML.say(sub_path)
models_dir=sub_path+"_3Dmodels"
model_to_build = ''
if len(sys.argv) < 3:
FreeCAD.Console.PrintMessage('No variant name is given, add a valid model name as an argument or the argument "all"\r\n')
sys.exit()
else:
model_to_build=sys.argv[2]
found_one = False
if len(model_to_build) > 0:
if model_to_build == 'all' or model_to_build == 'All' or model_to_build == 'ALL':
found_one = True
for n in different_models:
listall = n.get_list_all()
for i in listall:
make_3D_model(models_dir, n, i)
elif model_to_build == 'list':
found_one = True
FreeCAD.Console.PrintMessage('\r\n')
for n in different_models:
listall = n.get_list_all()
for i in listall:
|
#########################################################################
#
# Copyright (C) 2012 OpenPlans
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################
from django.conf import settings
from geonode import get_version
from geonode.catalogue import default_catalogue_backend
from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse
from geonode.utils import ogc_server_settings
def resource_urls(request):
"""Global values to pass to templates"""
site = Site.objects.get_current()
return dict(
STATIC_URL=settings.STATIC_URL,
GEOSERVER_BASE_URL=ogc_server_settings.pub | lic_url,
CATALOGUE_BASE_URL=default_catalogue_backend()['URL'],
REGISTRATION_OPEN=settings.REGISTRATION_OPEN,
VERSION=get_version(),
SITE_NAME=site.name,
SITE_DOMAIN=site.domain,
GROUPS_APP = True if "geonode.contrib.groups" in settings.INSTALLED_APPS else False,
UPLOADER_URL = reverse('data_upload') if getattr(settings, 'UPLOADER', dict()).get('BACKEND', 'geonode.rest') == 'geonode.importer' else reverse('laye | r_upload'),
GEOGIT_ENABLED = ogc_server_settings.GEOGIT_ENABLED,
TIME_ENABLED = getattr(settings, 'UPLOADER', dict()).get('OPTIONS', dict()).get('TIME_ENABLED', False),
DEBUG_STATIC = getattr(settings, "DEBUG_STATIC", False),
MF_PRINT_ENABLED = ogc_server_settings.MAPFISH_PRINT_ENABLED,
PRINTNG_ENABLED = ogc_server_settings.PRINTNG_ENABLED,
GS_SECURITY_ENABLED = ogc_server_settings.GEONODE_SECURITY_ENABLED,
PROXY_URL = getattr(settings, 'PROXY_URL', '/proxy/?url='),
SOCIAL_BUTTONS = getattr(settings, 'SOCIAL_BUTTONS', True),
USE_DOCUMENTS = 'geonode.documents' in settings.INSTALLED_APPS
)
|
#
# Copyright (C) 2008 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import fcntl
import re
import os
import select
import sys
import subprocess
from color import Coloring
from command import Command, MirrorSafeCommand
_CAN_COLOR = [
'branch',
'diff',
'grep',
'log',
]
class ForallColoring(Coloring):
def __init__(self, config):
Coloring.__init__(self, config, 'forall')
self.project = self.printer('project', attr='bold')
class Forall(Command, MirrorSafeCommand):
common = False
helpSummary = "Run a shell command in each project"
helpUsage = """
%prog [<project>...] -c <command> [<arg>...]
"""
helpDescription = """
Executes the same shell command in each project.
Output Formatting
-----------------
The -p option causes '%prog' to bind pipes to the command's stdin,
stdout and stderr streams, and pipe all output into a continuous
stream that is displayed in a single pager session. Project headings
are inserted before the output of each command is displayed. If the
command produces no output in a project, no heading is displayed.
The formatting convention used by -p is very suitable for some
types of searching, e.g. `repo forall -p -c git log -SFoo` will
print all commits that add or remove references to Foo.
The -v option causes '%prog' to display stderr messages if a
command produces output only on stderr. Normally the -p option
causes command output to be suppressed until the command produces
at least one byte of output on stdout.
Environment
-----------
pwd is the project's working directory. If the current client is
a mirror client, then pwd is the Git repository.
REPO_PROJECT is set to the unique name of the project.
REPO_PATH is the path relative the the root of the client.
REPO_REMOTE is the name of the remote system from the manifest.
REPO_LREV is the name of the revision from the manifest, translated
to a local tracking branch. If you need to pass the manifest
revision to a locally executed git command, use REPO_LREV.
REPO_RREV is the name of the revision from the manifest, exactly
as written in the manifest.
shell positional arguments ($1, $2, .., $#) are set to any arguments
following <command>.
Unless -p is used, stdin, stdout, stderr are inherited from the
terminal and are not redirected.
"""
def _Options(self, p):
def cmd(option, opt_str, value, parser):
setattr(parser.values, option.dest, list(parser.rargs))
while parser.rargs:
del parser.rargs[0]
p.add_option('-c', '--command',
help='Command (and arguments) to execute',
dest='command',
action='callback',
callback=cmd)
g = p.add_option_group('Output')
g.add_option('-p',
dest='project_header', action='store_true',
help='Show project headers before output')
g.add_option('-v', '--verbose',
dest='verbose', action='store_true',
help='Show command error messages')
def WantPager(self, opt):
return opt.project_header
def Execute(self, opt, args):
if not opt.command:
self.Usage()
cmd = [opt.command[0]]
shell = True
if re.compile(r'^[a-z0-9A-Z_/\.-]+$').match(cmd[0]):
shell = False
if shell:
cmd.append(cmd[0])
cmd.extend(opt.command[1:])
if opt.project_header \
and not shell \
and cmd[0] == 'git':
# If this is a direct git command that can enable colorized
# output and the user prefers coloring, add --color i | nto the
# command line because we are going to wrap the command into
# a pipe and git won't know coloring should activate.
#
for cn in cmd[1:]:
if not cn.startswith('-'):
break
if cn in _CAN_COLOR:
class ColorCmd(Coloring):
def __init__(self, c | onfig, cmd):
Coloring.__init__(self, config, cmd)
if ColorCmd(self.manifest.manifestProject.config, cn).is_on:
cmd.insert(cmd.index(cn) + 1, '--color')
mirror = self.manifest.IsMirror
out = ForallColoring(self.manifest.manifestProject.config)
out.redirect(sys.stdout)
rc = 0
first = True
for project in self.GetProjects(args):
env = dict(os.environ.iteritems())
def setenv(name, val):
if val is None:
val = ''
env[name] = val
setenv('REPO_PROJECT', project.name)
setenv('REPO_PATH', project.relpath)
setenv('REPO_REMOTE', project.remote.name)
setenv('REPO_LREV', project.GetRevisionId())
setenv('REPO_RREV', project.revisionExpr)
if mirror:
setenv('GIT_DIR', project.gitdir)
cwd = project.gitdir
else:
cwd = project.worktree
if not os.path.exists(cwd):
if (opt.project_header and opt.verbose) \
or not opt.project_header:
print >>sys.stderr, 'skipping %s/' % project.relpath
continue
if opt.project_header:
stdin = subprocess.PIPE
stdout = subprocess.PIPE
stderr = subprocess.PIPE
else:
stdin = None
stdout = None
stderr = None
p = subprocess.Popen(cmd,
cwd = cwd,
shell = shell,
env = env,
stdin = stdin,
stdout = stdout,
stderr = stderr)
if opt.project_header:
class sfd(object):
def __init__(self, fd, dest):
self.fd = fd
self.dest = dest
def fileno(self):
return self.fd.fileno()
empty = True
didout = False
errbuf = ''
p.stdin.close()
s_in = [sfd(p.stdout, sys.stdout),
sfd(p.stderr, sys.stderr)]
for s in s_in:
flags = fcntl.fcntl(s.fd, fcntl.F_GETFL)
fcntl.fcntl(s.fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)
while s_in:
in_ready, out_ready, err_ready = select.select(s_in, [], [])
for s in in_ready:
buf = s.fd.read(4096)
if not buf:
s.fd.close()
s_in.remove(s)
continue
if not opt.verbose:
if s.fd == p.stdout:
didout = True
else:
errbuf += buf
continue
if empty:
if first:
first = False
else:
out.nl()
out.project('project %s/', project.relpath)
out.nl()
out.flush()
if errbuf:
sys.stderr.write(errbuf)
sys.stderr.flush()
errbuf = ''
empty = False
s.dest.write(buf)
s.dest.flush()
r = p.wait()
if r != 0 and r != rc:
rc = r
if rc != 0:
sys.exit(rc)
|
ePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.widget.sizePolicy().hasHeightForWidth())
self.widget.setSizePolicy(sizePolicy)
self.widget.setMaximumSize(QtCore.QSize(16777215, 16777215))
self.widget.setObjectName("widget")
self.gridLayout = QtGui.QGridLayout(self.widget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.widget1 = QtGui.QWidget(self.widget)
self.widget1.setMaximumSize(QtCore.QSize(500, 16777215))
self.widget1.setObjectName("widget1")
self.verticalLayout_3 = QtGui.QVBoxLayout(self.widget1)
self.verticalLayout_3.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.verticalLayout = QtGui.QVBoxLayout()
self.verticalLayout.setSizeConstraint(QtGui.QLayout.SetDefaultConstraint)
self.verticalLayout.setObjectName("verticalLayout")
self.tableWidget = QtGui.QTableWidget(self.widget1)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.tableWidget.sizePolicy().hasHeightForWidth())
self.tableWidget.setSizePolicy(sizePolicy)
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(2)
self.tableWidget.setRowCount(0)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
self.tableWidget.horizontalHeader().setVisible(True)
self.tableWidget.horizontalHeader().setCascadingSectionResizes(False)
self.tableWidget.horizontalHeader().setDefaultSectionSize(100)
self.verticalLayout.addWidget(self.tableWidget)
self.sliceplanegroup = QtGui.QGroupBox(self.widget1)
self.sliceplanegroup.setEnabled(False)
self.sliceplanegroup.setObjectName("sliceplanegroup")
self.horizontalLayout = QtGui.QHBoxLayout(self.sliceplanegroup)
self.horizontalLayout.setObjectName("horizontalLayout")
self.slicePlaneRadioX = QtGui.QRadioButton(self.sliceplanegroup)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.slicePlaneRadioX.sizePolicy().hasHeightForWidth())
self.slicePlaneRadioX.setSizePolicy(sizePolicy)
self.slicePlaneRadioX.setChecked(False)
self.slicePlaneRadioX.setObjectName("slicePlaneRadioX")
self.horizontalLayout.addWidget(self.slicePlaneRadioX)
self.slicePlaneRadioY = QtGui.QRadioButton(self.sliceplanegroup)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.slicePlaneRadioY.sizePolicy().hasHeightForWidth())
self.slicePlaneRadioY.setSizePolicy(sizePolicy)
self.slicePlaneRadioY.setChecked(True)
self.slicePlaneRadioY.setObjectName("slicePlaneRadioY")
self.horizontalLayout.addWidget(self.slicePlaneRadioY)
self.slicePlaneRadioZ = QtGui.QRadioButton(self.sliceplanegroup)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.slicePlaneRadioZ.sizePolicy().hasHeightForWidth())
self.slicePlaneRadioZ.setSizePolicy(sizePolicy)
self.slicePlaneRadioZ.setObjectName("slicePlaneRadioZ")
self.horizontalLayout.addWidget(self.slicePlaneRadioZ)
self.verticalLayout.addWidget(self.sliceplanegroup)
self.screenshotgroup = QtGui.QGroupBox(self.widget1)
self.screenshotgroup.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)
self.screenshotgroup.setObjectName("screenshotgroup")
self.formLayout = QtGui.QFormLayout(self.screenshotgroup)
self.formLayout.setFieldGrowthPolicy(QtGui.QFormLayout.AllNonFixedFieldsGrow)
self.formLayout.setObjectName("formLayout")
self.pixelsXLabel = QtGui.QLabel(self.screenshotgroup)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pixelsXLabel.sizePolicy().hasHeightForWidth())
self.pixelsXLabel.setSizePolicy(sizePolicy)
self.pixelsXLabel.setObjectName("pixelsXLabel")
self.formLayout.setWidget(0, QtGui.QFormLayout.LabelRole, self.pixelsXLabel)
self.screenshotPixelXLineEdit = QtGui.QLineEdit(self.screenshotgroup)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.screenshotPixelXLineEdit.sizePolicy().hasHeightForWidth())
self.screenshotPixelXLineEdit.setSizePolicy(sizePolicy)
self.screenshotPixelXLineEdit.setObjectName("screenshotPixelXLineEdit")
self.formLayout.setWidget(0, QtGui.QFormLayout.FieldRole, self.screenshotPixelXLineEdit)
self.pixelsYLabel = QtGui.QLabel(self.screenshotgroup)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pixelsYLabel.sizePolicy().hasHeightForWidth())
self.pixelsYLabel.setSizePolicy(sizePolicy)
self.pixelsYLabel.setObjectName("pixelsYLabel")
self.formLayout.setWidget(1, QtGui.QFormLayout.LabelRole, self.pixelsYLabel)
self.screenshotPixelYLineEdit = QtGui.QLineEdit(self.screenshotgroup)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.se | tVerticalStretch(0)
sizePolicy.setHeightForWidth(self.screenshotPixelYLineEdit.sizePolicy().hasHeightForWidth())
self.screenshotPixelYLineEdit.setSizePolicy(sizePolicy)
self.screenshotPixelYLineEdit.setObjectName("screenshotPixelYLineEdit")
self.formLayout.setWidget(1, QtGui.QFormLayout.FieldRol | e, self.screenshotPixelYLineEdit)
self.screenshotFilenameLabel = QtGui.QLabel(self.screenshotgroup)
self.screenshotFilenameLabel.setObjectName("screenshotFilenameLabel")
self.formLayout.setWidget(2, QtGui.QFormLayout.LabelRole, self.screenshotFilenameLabel)
self.screenshotFilenameLineEdit = QtGui.QLineEdit(self.screenshotgroup)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.screenshotFilenameLineEdit.sizePolicy().hasHeightForWidth())
self.screenshotFilenameLineEdit.setSizePolicy(sizePolicy)
self.screenshotFilenameLineEdit.setObjectName("screenshotFilenameLineEdit")
self.formLayout.setWidget(2, QtGui.QFormLayout.FieldRole, self.screenshotFilenameLineEdit)
self.screenshotSaveButton = QtGui.QPushButton(self.screenshotgroup)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.screenshotSaveButton.sizePolicy().hasHeightForWidth())
self.screenshotSaveButton.setSizePolicy(sizePolicy)
self.screenshotSaveButton.setObjectName("screenshotSaveButton")
self.formLayout.setWidget(3, QtGui.QFormLayout.FieldRole, self.screenshotSaveButton)
self.verticalLayout.addWidget(self.scre |
# -*- coding: utf-8 -*-
# Akvo RSR is covered by the GNU Affero General Public License.
# See more details in the license.txt file located at the root folder of the Akvo RSR module.
# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.
from lxml import etree
def participating_org(project):
"""
Generate the participating-org element.
:param project: Project object
:return: A list of Etree elements
"""
partnership_elements = []
from akvo.rsr.models import Partnership
for partnership in project.partnerships.all():
# Don't include reporting orgs or sponsor partners
if partnership.iati_organisation_role in Partnership.IATI_ROLE_LIST[:4] and \
partnership.organisation:
org = partnership.organisation.get_original()
element = etree.Element("participating-org")
if org.iati_org_id:
element.attrib['ref'] = org.iati_org_id
if org.new_organisation_type:
element.attrib['type'] = str(org.new_organisation_type)
if partnership.iati_organisation_role:
element.attrib['role'] = str(partnership.iati_organisation_role)
if partnership.iati_activity_id:
element.attrib['activity-id'] = partnership.iati_activity_id
# TODO: Funding amount
narrative_eleme | nt = etree.SubElement(element, "narrative")
if org.long_name:
narrative_element.text = org.long_name
elif org.name:
narrative_element.text = org.name
partnership_elements.append(element)
r | eturn partnership_elements
|
- retry_limit (int, optional):
Automatic retry count. Default: 0
- engine_version (str, optional):
Engine version to be used. If none is
specified, the account's default engine version would be set.
{"stable", "experimental"}
- pool_name (str, optional):
For Presto only. Pool name to be used, if not
specified, default pool would be used.
- result (str, optional):
Location where to store the result of the query.
e.g. 'tableau://user:password@host.com:1234/datasource'
Returns:
:class:`datetime.datetime`: Start date time.
"""
if "cron" not in params:
raise ValueError("'cron' option is required")
if "query" not in params:
raise ValueError("'query' option is required")
params = {} if params is None else params
return self.api.create_schedule(name, params)
def delete_schedule(self, name):
"""Delete the scheduled query with the specified name.
Args:
name (str): Target scheduled query name.
Returns:
(str, str): Tuple of cron and query.
"""
return self.api.delete_schedule(name)
def schedules(self):
"""Get the list of all the scheduled queries.
Returns:
[:class:`tdclient.models.Schedule`]
"""
result = self.api.list_schedules()
return [models.Schedule(self, **m) for m in result]
def update_schedule(self, name, params=None):
"""Update the scheduled query.
Args:
name (str): Target scheduled query name.
params (dict): Extra parameters.
- type (str):
Query type. {"presto", "hive"}. Default: "hive"
- database (str):
Target database name.
- timezone (str):
Scheduled query's timezone. e.g. "UTC"
For details, see also: https://gist.github.com/frsyuki/4533752
- cron (str, optional):
Schedule of the query.
{``"@daily"``, ``"@hourly"``, ``"10 * * * *"`` (custom cron)}
See also: https://tddocs.atlassian.net/wiki/spaces/PD/pages/1084633/Scheduling+Jobs+Using+TD+Console
- delay (int, optional):
A delay ensures all buffered events are imported
before running the query. Default: 0
- query (str):
Is a language used to retrieve, insert, update and modify
data. See also: https://tddocs.atlassian.net/wiki/spaces/PD/pages/1084438/SQL+Examples+of+Scheduled+Queries
- priority (int, optional):
Priority of the query.
| Range is from -2 (very low) to 2 (very high). Default: 0
- retry_limit (i | nt, optional):
Automatic retry count. Default: 0
- engine_version (str, optional):
Engine version to be used. If none is
specified, the account's default engine version would be set.
{"stable", "experimental"}
- pool_name (str, optional):
For Presto only. Pool name to be used, if not
specified, default pool would be used.
- result (str, optional):
Location where to store the result of the query.
e.g. 'tableau://user:password@host.com:1234/datasource'
"""
params = {} if params is None else params
self.api.update_schedule(name, params)
def history(self, name, _from=None, to=None):
"""Get the history details of the saved query for the past 90days.
Args:
name (str): Target name of the scheduled query.
_from (int, optional): Indicates from which nth record in the run history
would be fetched.
Default: 0.
Note: Count starts from zero. This means that the first record in the
list has a count of zero.
to (int, optional): Indicates up to which nth record in the run history
would be fetched.
Default: 20
Returns:
[:class:`tdclient.models.ScheduledJob`]
"""
result = self.api.history(name, _from, to)
def scheduled_job(m):
(
scheduled_at,
job_id,
type,
status,
query,
start_at,
end_at,
result_url,
priority,
database,
) = m
job_param = {
"url": None,
"debug": None,
"start_at": start_at,
"end_at": end_at,
"cpu_time": None,
"result_size": None,
"result": None,
"result_url": result_url,
"hive_result_schema": None,
"priority": priority,
"retry_limit": None,
"org_name": None,
"database": database,
}
return models.ScheduledJob(
self, scheduled_at, job_id, type, query, **job_param
)
return [scheduled_job(m) for m in result]
def run_schedule(self, name, time, num):
"""Execute the specified query.
Args:
name (str): Target scheduled query name.
time (int): Time in Unix epoch format that would be set as TD_SCHEDULED_TIME
num (int): Indicates how many times the query will be executed.
Value should be 9 or less.
Returns:
[:class:`tdclient.models.ScheduledJob`]
"""
results = self.api.run_schedule(name, time, num)
def scheduled_job(m):
job_id, type, scheduled_at = m
return models.ScheduledJob(self, scheduled_at, job_id, type, None)
return [scheduled_job(m) for m in results]
def import_data(
self, db_name, table_name, format, bytes_or_stream, size, unique_id=None
):
"""Import data into Treasure Data Service
Args:
db_name (str): name of a database
table_name (str): name of a table
format (str): format of data type (e.g. "msgpack.gz")
bytes_or_stream (str or file-like): a byte string or a file-like object contains the data
size (int): the length of the data
unique_id (str): a unique identifier of the data
Returns:
second in float represents elapsed time to import data
"""
return self.api.import_data(
db_name, table_name, format, bytes_or_stream, size, unique_id=unique_id
)
def import_file(self, db_name, table_name, format, file, unique_id=None):
"""Import data into Treasure Data Service, from an existing file on filesystem.
This method will decompress/deserialize records from given file, and then
convert it into format acceptable from Treasure Data Service ("msgpack.gz").
Args:
db_name (str): name of a database
table_name (str): name of a table
format (str): format of data type (e.g. "msgpack", "json")
file (str or file-like): a name of a file, or a file-like object contains the data
unique_id (str): a unique identifier of the data
Returns:
float represents the elapsed time to import data
"""
return self.api.import_file(
db_name, table_name, format, file, unique_id=unique_id
)
def results(self):
"""Get the list of all the available authentications.
Returns:
a list of :class:`tdclient.models.Result`
"""
results = self.api.list_result()
|
"""
API views for badges
"""
from edx_rest_framework_extensions.auth.session.authentication import SessionAuthenticationAllowInactiveUser
from opaque_keys import InvalidKeyError
from opaque_keys.edx.django.models import CourseKeyField
from opaque_keys.edx.keys import CourseKey
from rest_framework import generics
from rest_framework.exceptions import APIException
from badges.models import BadgeAssertion
from openedx.core.djangoapps.user_api.permissions import is_field_shared_factory
from openedx.core.lib.api.authentication import OAuth2AuthenticationAllowInactiveUser
from .serializers import BadgeAssertionSerializer
class InvalidCourseKeyError(APIException):
"""
Raised the course key given isn't valid.
"""
status_code = 400
default_detail = "The course key provided was invalid."
class UserBadgeAssertions(generics.ListAPIView):
"""
** Use cases **
Request a list of assertions for a user, optionally constrained to a course.
** Example Requests **
GET /api/badges/v1/assertions/user/{username}/
** Response Values **
Body comprised of a list of objects with the following fields:
* badge_class: The badge class the assertion was awarded for. Represented as an object
with the following fields:
* slug: The identifier for the badge class
* issuing_component: The software component responsible for issuing this badge.
* display_name: The display name of the badge.
* course_id: The course key of the course this badge is scoped to, or null if it isn't scoped to a course.
* description: A description of the award and its significance.
* criteria: A description of what is needed to obtain this award.
* image_url: A URL to the icon image used to represent this award.
* image_url: The baked assertion image derived from the badge_class icon-- contains metadata about the award
in its headers.
* assertion_url: The URL to the OpenBadges BadgeAssertion object, for verification by compatible tools
and software.
** Params **
* slug (optional): The identifier for a particular badge class to filter by.
* issuing_component (optional): The issu | ing component for a particular badge class to filter by
(requires slug to have been specified, or this will be ignored.) If slug is provided and this is not,
assumes | the issuing_component should be empty.
* course_id (optional): Returns assertions that were awarded as part of a particular course. If slug is
provided, and this field is not specified, assumes that the target badge has an empty course_id field.
'*' may be used to get all badges with the specified slug, issuing_component combination across all courses.
** Returns **
* 200 on success, with a list of Badge Assertion objects.
* 403 if a user who does not have permission to masquerade as
another user specifies a username other than their own.
* 404 if the specified user does not exist
{
"count": 7,
"previous": null,
"num_pages": 1,
"results": [
{
"badge_class": {
"slug": "special_award",
"issuing_component": "openedx__course",
"display_name": "Very Special Award",
"course_id": "course-v1:edX+DemoX+Demo_Course",
"description": "Awarded for people who did something incredibly special",
"criteria": "Do something incredibly special.",
"image": "http://example.com/media/badge_classes/badges/special_xdpqpBv_9FYOZwN.png"
},
"image_url": "http://badges.example.com/media/issued/cd75b69fc1c979fcc1697c8403da2bdf.png",
"assertion_url": "http://badges.example.com/public/assertions/07020647-e772-44dd-98b7-d13d34335ca6"
},
...
]
}
"""
serializer_class = BadgeAssertionSerializer
authentication_classes = (
OAuth2AuthenticationAllowInactiveUser,
SessionAuthenticationAllowInactiveUser
)
permission_classes = (is_field_shared_factory("accomplishments_shared"),)
def filter_queryset(self, queryset):
"""
Return most recent to least recent badge.
"""
return queryset.order_by('-created')
def get_queryset(self):
"""
Get all badges for the username specified.
"""
queryset = BadgeAssertion.objects.filter(user__username=self.kwargs['username'])
provided_course_id = self.request.query_params.get('course_id')
if provided_course_id == '*':
# We might want to get all the matching course scoped badges to see how many courses
# a user managed to get a specific award on.
course_id = None
elif provided_course_id:
try:
course_id = CourseKey.from_string(provided_course_id)
except InvalidKeyError:
raise InvalidCourseKeyError
elif 'slug' not in self.request.query_params:
# Need to get all badges for the user.
course_id = None
else:
# Django won't let us use 'None' for querying a ForeignKey field. We have to use this special
# 'Empty' value to indicate we're looking only for badges without a course key set.
course_id = CourseKeyField.Empty
if course_id is not None:
queryset = queryset.filter(badge_class__course_id=course_id)
if self.request.query_params.get('slug'):
queryset = queryset.filter(
badge_class__slug=self.request.query_params['slug'],
badge_class__issuing_component=self.request.query_params.get('issuing_component', '')
)
return queryset
|
"""Imports for Python API.
This file is MACHINE GENERATED! Do not edit.
Generated by: tensorflow/tools/api/generator/create_python_api.py script.
"""
from | tensorflow.python.platform.resource_loader import get_data_files_path
from t | ensorflow.python.platform.resource_loader import get_path_to_datafile
from tensorflow.python.platform.resource_loader import get_root_dir_with_all_resources
from tensorflow.python.platform.resource_loader import load_resource
from tensorflow.python.platform.resource_loader import readahead_file_path |
import sst
import sst.actions
# tests for simulate_keys
sst.actions.set_base_url('http://localhost:%s/' % sst.DEVSERVER_PORT)
sst.actions.go_to('/')
sst.actions.assert_title('The Page Title')
sst.actions.write_textfield('text_1', 'Foobar..')
sst.actions.simulate_keys('text_1', 'BACK_SPACE')
sst.actions.simulate_keys('text_1', 'back_space') # not case sensitive
sst.actions.simulate_keys('text_1', 'SPACE')
sst.actions.simulate_keys('text_1', 'Space')
sst.actions.assert_text('text_1', 'Foobar ')
# available keys (from selenium/webdriver/c | ommon/keys.py):
#
# 'NULL'
# 'CANCEL'
# 'HELP'
# 'BACK_SPACE'
# 'TAB'
# 'CLEAR'
# 'RETURN'
# 'ENTER'
# 'SHIFT'
# 'LEFT_SHIFT'
# 'CONTROL'
# 'LEFT_CONTROL'
# 'ALT'
# 'LEFT_ALT'
# 'PAUSE'
# 'ESCAPE'
# 'SPACE'
# 'PAGE_UP'
# 'PAGE_DOWN'
# 'END'
# 'HOME'
# 'LEFT'
# 'ARROW_LEFT'
# 'UP'
# 'ARROW_UP'
# 'RIGHT'
# 'ARROW_RIGHT'
# 'DOWN'
# 'ARROW_DOWN'
# 'INSERT'
# 'DELETE'
# 'SEMICOLON'
# 'EQUALS'
# 'NUMPAD0'
# 'NUMPAD1'
# 'NUMPAD2'
# ' | NUMPAD3'
# 'NUMPAD4'
# 'NUMPAD5'
# 'NUMPAD6'
# 'NUMPAD7'
# 'NUMPAD8'
# 'NUMPAD9'
# 'MULTIPLY'
# 'ADD'
# 'SEPARATOR'
# 'SUBTRACT'
# 'DECIMAL'
# 'DIVIDE'
# 'F1'
# 'F2'
# 'F3'
# 'F4'
# 'F5'
# 'F6'
# 'F7'
# 'F8'
# 'F9'
# 'F10'
# 'F11'
# 'F12'
# 'META'
# 'COMMAND'
|
# D | O NOT EDIT THIS FILE!
#
# Python module CosTradingDynamic generated by omniidl
import omniORB
omniORB.updateModule("CosTradingDynam | ic")
# ** 1. Stub files contributing to this module
import CosTradingDynamic_idl
# ** 2. Sub-modules
# ** 3. End
|
# -*- coding: utf- | 8 -*-
__author__ = 'massimo'
import unittest
class UtilTestFunctions(unittest.TestCase):
def test_verify_url(self):
self.assertTrue(True)
if __name__ == "__main__":
UtilTestFunctions.main | ()
|
from __future__ import unicode_literals
from django.db import models
from wagtail.core.models import Site
class SystemString(models.Model):
DEFAULT_GROUP = 'general'
identifier = models.CharField(max_length=1024)
string = models.CharField(max_length=1024, blank=True, null=True)
group = models.CharField(max_length=255, default=DEFAULT_GROUP)
site = models.ForeignKey(Site, on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(null=True, blank=True)
accessed = mod | els.DateTimeField(null=True, blank=True)
| modified = models.BooleanField(default=False)
class Meta:
unique_together = ['identifier', 'site', 'group']
def __unicode__(self):
return unicode(self.identifier)
|
"""
Siren protocol adapter. See `SIREN specification <https://github.com/kevinswiber/siren>`_.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from ripozo.adapters import AdapterBase
from ripozo.utilities import titlize_endpoint
from ripozo.resources.resource_base import create_url
from ripozo.resources.constants import input_categories
import json
import six
_CONTENT_TYPE = 'application/vnd.siren+json'
class SirenAdapter(AdapterBase):
"""
An adapter that formats the response in the SIREN format.
A description of a SIREN format can be found here:
`SIREN specification <https://github.com/kevinswiber/siren>`_
"""
formats = [_CONTENT_TYPE, 'siren']
extra_headers = {'Content-Type': _CONTENT_TYPE}
@property
def formatted_body(self):
"""
Gets the formatted body of the response in unicode form.
If ``self.status_code == 204`` then this will
return an empty string.
:return: The siren formatted response body
:rtype: unicode
"""
# 204's are supposed to be empty responses
if self.status_code == 204:
return ''
links = self.generate_links()
entities = self.get_entities()
response = dict(properties=self.resource.properties, actions=self._actions,
links=links, entities=entities)
# need to do this separately since class is a reserved keyword
response['class'] = [self.resource.resource_name]
return json.dumps(response)
@property
def _actions(self):
"""
Gets the list of actions in an appropriately SIREN format
:return: The list of actions
:rtype: list
"""
actions = []
for endpoint, options in six.iteritems(self.resource.endpoint_dictionary()):
options = options[0]
all_methods = options.get('methods', ('GET',))
meth = all_methods[0] if all_methods else 'GET'
base_route = options.get('route', self.resource.base_url)
route = create_url(base_route, **self.resource.properties)
route = self.combine_base_url_with_resource_url(route)
fields = self.generate_fields_for | _endpoint_funct(options.get('endpoint_func'))
actn = dict(name=endpoint, title=titlize_endpoint(endpoint),
method=meth, href=route, fields=fields)
actions.append(actn)
| return actions
def generate_fields_for_endpoint_funct(self, endpoint_func):
"""
Returns the action's fields attribute in a SIREN
appropriate format.
:param apimethod endpoint_func:
:return: A dictionary of action fields
:rtype: dict
"""
return_fields = []
fields_method = getattr(endpoint_func, 'fields', None)
if not fields_method:
return []
fields = fields_method(self.resource.manager)
for field in fields:
if field.arg_type is input_categories.URL_PARAMS:
continue
field_dict = dict(name=field.name, type=field.field_type.__name__,
location=field.arg_type, required=field.required)
return_fields.append(field_dict)
return return_fields
def generate_links(self):
"""
Generates the Siren links for the resource.
:return: The list of Siren formatted links.
:rtype: list
"""
href = self.combine_base_url_with_resource_url(self.resource.url)
links = [dict(rel=['self'], href=href)]
for link, link_name, embedded in self.resource.linked_resources:
links.append(dict(rel=[link_name],
href=self.combine_base_url_with_resource_url(link.url)))
return links
def get_entities(self):
"""
Gets a list of related entities in an appropriate SIREN format
:return: A list of entities
:rtype: list
"""
entities = []
for resource, name, embedded in self.resource.related_resources:
for ent in self.generate_entity(resource, name, embedded):
entities.append(ent)
return entities
def generate_entity(self, resource, name, embedded):
"""
A generator that yields entities
"""
if isinstance(resource, list):
for res in resource:
for ent in self.generate_entity(res, name, embedded):
yield ent
else:
if not resource.has_all_pks:
return
ent = {'class': [resource.resource_name], 'rel': [name]}
resource_url = self.combine_base_url_with_resource_url(resource.url)
if not embedded:
ent['href'] = resource_url
else:
ent['properties'] = resource.properties
ent['links'] = [dict(rel=['self'], href=resource_url)]
yield ent
@classmethod
def format_exception(cls, exc):
"""
Takes an exception and appropriately formats it
in the siren format. Mostly. It doesn't return
a self in this circumstance.
:param Exception exc: The exception to format.
:return: A tuple containing: response body, format,
http response code
:rtype: tuple
"""
status_code = getattr(exc, 'status_code', 500)
body = {'class': ['exception', exc.__class__.__name__],
'actions': [], 'entities': [], 'links': [],
'properties': dict(status=status_code, message=six.text_type(exc))}
return json.dumps(body), cls.formats[0], status_code
@classmethod
def format_request(cls, request):
"""
Simply returns request
:param RequestContainer request: The request to handler
:rtype: RequestContainer
"""
return request
|
from PyQt4 import QtCore
from PyQt4 import QtGui
from Action import Speech
from UI.ActionPushButton import ActionPushButton
class BaseStudy(QtGui.QWidget):
def __init__(self):
super(BaseStudy, self).__init__()
self._actionQueue = None
self._nao = None
self._widgets = None
self._buttons = None
#END __init__()
def LEDActive(self):
if self._nao is not None:
self._nao.LEDrandomEyes(1.0, True)
#END if
#END LEDActive()
def LEDNormal(self):
if self._nao is not None:
self._nao.LEDNormal()
#END if
#END LEDNormal()
def setActionQueue(self, actionQueue):
self._actionQueue = actionQueue
#END setActionQueue()
def setNao(self, nao):
if self._nao is not None:
self._nao.connected.disconnect(self.on_nao_connected)
self._nao.disconnected.disconnect(self.on_nao_disconnected)
#END if
self._nao = nao
if self._nao is not None:
self._nao.connected.connect(self.on_nao_connected)
self._nao.disconnected.connect(self.on_nao_disconnected)
#END if
#END setNao()
def speech(self, txt, speed, shaping):
return None
#END speech()
def on_button_clicked(self):
if self._actionQueue is not None:
self._actionQueue.addActions(self.sender().getRobotActions())
#END if
#END on_button_clicked()
def on_nao_connected(self):
pass
#END on_nao_connected()
def on_nao_disconnected(self):
pass
#END on_nao_disconnected()
def on_runSpeech_clicked(self):
if self._actionQueue is not None:
self._actionQueue.addActions(self.sender().getRobotActions())
#END if
#END on_runSpeech_clicked()
def _setupUi(self, general_panel = True, custom_widget = None):
wgtGeneral = None
if general_panel:
wgtGeneral = QtGui.QWidget()
wgtGeneral.setMaximumHeight(80)
wgtGeneral.setMinimumHeight(80)
##################################################
# General Speech
##################################################
self._speechs = [
ActionPushButton(None, "Hello", Speech("Hello")),
ActionPushButton(None, "Thanks", Speech("Thank you")),
ActionPushButton(None, "Sorry", Speech("I'm sorry")),
ActionPushButton(None, "Good", Speech("Good!")),
ActionPushButton(None, "Okay", Speech("Okay")),
ActionPushButton(None, "Yes", Speech("Yes")),
ActionPushButton(None, "No", Speech("No")),
ActionPushButton(None, "Hmmm", Speech("Heum,")),
None,
ActionPushButton(None, "Louder", Speech("Please speak louder")),
ActionPushButton(None, "Say again?", Speech("Can you say one more time?")),
ActionPushButton(None, "Repeat?", Speech("Would you like me to repeat that?")),
ActionPushButton(None, "Understood?", Speech("Do you understand?")),
ActionPushButton(None, "Don't Understand", Speech("I don't understand")),
ActionPushButton(None, "Greeting", Speech("Hello, my name is NAO, nice to meet you")),
ActionPushButton(None, "End Experiment", Speech("Thank you for participating in our experiment")),
]
self._grpSpeech = QtGui.QGroupBox(wgtGeneral)
self._grpSpeech.setTitle("General Speech")
layoutSpeech = QtGui.QVBoxLayout(self._grpSpeech)
layoutSpeech.setMargin(6)
| layoutSpeech.addSpacing(3)
widget = QtGui.QWidget(self._grpSpeech)
layout = QtGui.QHBoxLayout(widget)
layout.setMargin(0)
for item in self._speechs:
if item is None:
layoutSpeech.addWidget(widget)
| widget = QtGui.QWidget(self._grpSpeech)
layout = QtGui.QHBoxLayout(widget)
layout.setMargin(0)
else:
item.setParent(widget)
item.clicked.connect(self.on_runSpeech_clicked)
layout.addWidget(item)
#END if
#END for
layoutSpeech.addWidget(widget)
#END if
wgtButtons = None
if self._widgets is not None and self._buttons is not None:
wgtButtons = QtGui.QWidget()
layout = QtGui.QHBoxLayout(wgtButtons)
layout.setMargin(0)
for i in range(len(self._widgets)):
layoutButtons = QtGui.QVBoxLayout(self._widgets[i])
layoutButtons.setMargin(0)
for button in self._buttons[i]:
if isinstance(button, ActionPushButton):
button.clicked.connect(self.on_button_clicked)
#END if
layoutButtons.addWidget(button)
#END for
scroll = QtGui.QScrollArea()
scroll.setAlignment(QtCore.Qt.AlignCenter)
scroll.setWidget(self._widgets[i])
layoutScroll = QtGui.QHBoxLayout()
layoutScroll.setMargin(0)
layoutScroll.addWidget(scroll)
layout.addLayout(layoutScroll)
#END for
#END if
if wgtGeneral is not None or wgtButtons is not None or custom_widget is not None:
splitter = QtGui.QSplitter(self)
splitter.setOrientation(QtCore.Qt.Vertical)
layout = QtGui.QHBoxLayout(self)
layout.setMargin(0)
layout.addWidget(splitter)
if wgtGeneral is not None:
wgtGeneral.setParent(splitter)
#END if
if wgtButtons is not None:
wgtButtons.setParent(splitter)
#END if
if custom_widget is not None:
custom_widget.setParent(splitter)
#END if
#END if
#END _setupUi()
#END BaseStudy
|
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""
Function/method decorators that provide timeout and retry logic.
"""
import functools
import itertools
import sys
from devil.android import device_errors
from devil.utils import cmd_helper
from devil.utils import reraiser_thread
from devil.utils import timeout_retry
DEFAULT_TIMEOUT_ATTR = '_default_timeout'
DEFAULT_RETRIES_ATTR = '_default_retries'
def _TimeoutRetryWrapper(f, timeout_func, retries_func, pass_values=False):
""" Wraps a funcion with timeout and retry handling logic.
Args:
f: The function to wrap.
timeout_func: A callable that returns the timeout value.
retries_func: A callable that returns the retries value.
pass_values: If True, passes the values returned by |timeout_func| and
|retries_func| to the wrapped function as 'timeout' and
'retries' kwargs, respectively.
Returns:
The wrapped function.
"""
@functools.wraps(f)
def timeout_retry_wrapper(*args, **kwargs):
timeout = timeout_func(*args, **kwargs)
retries = retries_func(*args | , **kwargs)
if pass_values:
kwargs['timeout'] = timeout
kwargs['retries'] = retries
@functools.wraps(f)
def impl():
return f(*args, **kwargs)
try:
if timeout_retry.CurrentTime | outThreadGroup():
# Don't wrap if there's already an outer timeout thread.
return impl()
else:
desc = '%s(%s)' % (f.__name__, ', '.join(itertools.chain(
(str(a) for a in args),
('%s=%s' % (k, str(v)) for k, v in kwargs.iteritems()))))
return timeout_retry.Run(impl, timeout, retries, desc=desc)
except reraiser_thread.TimeoutError as e:
raise device_errors.CommandTimeoutError(str(e)), None, (
sys.exc_info()[2])
except cmd_helper.TimeoutError as e:
raise device_errors.CommandTimeoutError(str(e)), None, (
sys.exc_info()[2])
return timeout_retry_wrapper
def WithTimeoutAndRetries(f):
"""A decorator that handles timeouts and retries.
'timeout' and 'retries' kwargs must be passed to the function.
Args:
f: The function to decorate.
Returns:
The decorated function.
"""
get_timeout = lambda *a, **kw: kw['timeout']
get_retries = lambda *a, **kw: kw['retries']
return _TimeoutRetryWrapper(f, get_timeout, get_retries)
def WithExplicitTimeoutAndRetries(timeout, retries):
"""Returns a decorator that handles timeouts and retries.
The provided |timeout| and |retries| values are always used.
Args:
timeout: The number of seconds to wait for the decorated function to
return. Always used.
retries: The number of times the decorated function should be retried on
failure. Always used.
Returns:
The actual decorator.
"""
def decorator(f):
get_timeout = lambda *a, **kw: timeout
get_retries = lambda *a, **kw: retries
return _TimeoutRetryWrapper(f, get_timeout, get_retries)
return decorator
def WithTimeoutAndRetriesDefaults(default_timeout, default_retries):
"""Returns a decorator that handles timeouts and retries.
The provided |default_timeout| and |default_retries| values are used only
if timeout and retries values are not provided.
Args:
default_timeout: The number of seconds to wait for the decorated function
to return. Only used if a 'timeout' kwarg is not passed
to the decorated function.
default_retries: The number of times the decorated function should be
retried on failure. Only used if a 'retries' kwarg is not
passed to the decorated function.
Returns:
The actual decorator.
"""
def decorator(f):
get_timeout = lambda *a, **kw: kw.get('timeout', default_timeout)
get_retries = lambda *a, **kw: kw.get('retries', default_retries)
return _TimeoutRetryWrapper(f, get_timeout, get_retries, pass_values=True)
return decorator
def WithTimeoutAndRetriesFromInstance(
default_timeout_name=DEFAULT_TIMEOUT_ATTR,
default_retries_name=DEFAULT_RETRIES_ATTR,
min_default_timeout=None):
"""Returns a decorator that handles timeouts and retries.
The provided |default_timeout_name| and |default_retries_name| are used to
get the default timeout value and the default retries value from the object
instance if timeout and retries values are not provided.
Note that this should only be used to decorate methods, not functions.
Args:
default_timeout_name: The name of the default timeout attribute of the
instance.
default_retries_name: The name of the default retries attribute of the
instance.
min_timeout: Miniumum timeout to be used when using instance timeout.
Returns:
The actual decorator.
"""
def decorator(f):
def get_timeout(inst, *_args, **kwargs):
ret = getattr(inst, default_timeout_name)
if min_default_timeout is not None:
ret = max(min_default_timeout, ret)
return kwargs.get('timeout', ret)
def get_retries(inst, *_args, **kwargs):
return kwargs.get('retries', getattr(inst, default_retries_name))
return _TimeoutRetryWrapper(f, get_timeout, get_retries, pass_values=True)
return decorator
|
# -*- coding: utf-8 -*-
"""
Django settings for blog project.
Generated by 'django-admin startproject' using Django 1.11.4.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '2y8c!z@9!s%(0=epe+2n3k8+_!$vg--xz^3p4rs)6ov)c^2a*t'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blogadmin',
'DjangoUeditor',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'blog.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.c | ontrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'blog.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
#
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backe | nds.mysql',
# 'NAME': 'blog', # app's database name
# 'USER': 'root',
# 'PASSWORD': 'root',
# 'HOST': '127.0.0.1', # localhost or cloudhost
# 'PORT': '3306',
# },
# 'another_db':{
# 'ENGINE': 'django.db.backends.mysql',
# 'NAME': 'app2_database', # app2's database name
# 'USER': 'root',
# 'PASSWORD': 'root',
# 'HOST': '127.0.0.1', # localhost or cloudhost
# 'PORT': '3306',
# },
# }
# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static')
# 公共的static文件夹
STATIC_DIRS = (
os.path.join(BASE_DIR,'static'),
os.path.join(BASE_DIR,'media'),
)
# 上传数据的文件夹
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
STATICFILES_FINDERS = ("django.contrib.staticfiles.finders.FileSystemFinder", "django.contrib.staticfiles.finders.AppDirectoriesFinder",)
|
#!/usr/bin/env python
"""
Largest product in a grid
Problem 11
Published on 22 February 2002 at 06:00 pm [Server Time]
In the 20x20 grid below, four numbers along a diagonal line have been marked in red.
The product of these numbers is 26 * 63 * 78 * 14 = 1788696.
What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20x20 grid?
"""
THE_GRID = [[int(column) for column in row.split(' ')] for row in
"""
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
""".strip().split('\n')]
"""
A few words about the declaration of THE_GRID:
This is not the easiest thing to digest on first look. I think it is "pythonic"
in its implementation and it allows to copy/paste the grid straight out of the problem
statement without a bunch of mucking around to manually turn it into a 2d array
( or nested lists, actually ). It is arranged as a list of rows. Each row is a
list of numbers for each column in that row. Looking at it, the multi-line string
definition actually converts to a list of strings from the split operation. One
string for each row. The top list comprehension converts each row into a list of
short strings ( the columns ) which are also converted to int.
"""
#------------------------------------------------------------------------------
import operator
#------------------------------------------------------------------------------
def product(iterable):
return reduce(operator.mul, iterable, 1)
def solve(run_length):
height = len(THE_GRID)
width = len(THE_GRID[0])
for row in range(height-run_length+1):
for column in range(width-run_length+1):
for y_dir in (0, 1):
for x_dir in (0,1):
for i in range(run_length):
print THE_GRID[row+(y_dir*i)][column+x_dir*i]
def solve(run_length):
height = len(THE_GRID)
width = len(THE_GRID[0])
for row in range(height-run_length+1):
for column in range(width-run_length+1):
for i in range(run_length):
for y_dir in (0, 1):
for x_dir in (0,1):
print THE_GRID[row+(y_dir*i)][column+x_ | dir*i]
def solve(run_length):
height = len(THE_GRID)
width = len(THE_GRID[0])
highest = 0
for row in range(height-run_length+1):
for column in range(width-run_length+1):
for x_dir, y_dir in [(1, 0), (0, 1), (1, 1)]:
for i in range(run_length):
print THE_GRID[row+(y_dir*i)][column+x | _dir*i]
def solve(run_length):
height = len(THE_GRID)
width = len(THE_GRID[0])
highest = 0
for row in range(height-run_length+1):
for column in range(width-run_length+1):
for x_dir, y_dir in [(1, 0), (0, 1), (1, 1)]:
run =[THE_GRID[row+(y_dir*i)][column+x_dir*i] for i in range(run_length)]
result = product(run)
print run, result
#if result > highest:
# highest = result
#return(highest)
#------------------------------------------------------------------------------
def solve():
g = THE_GRID
maxp = 0
rows, cols, path_size = len(g), len(g[0]), 5
for i in range(rows):
for j in range(cols - path_size + 1):
phv = max(product([g[i][j+s] for s in range(path_size)]),
product([g[j+s][i] for s in range(path_size)]))
#phv = max(g[i][j] * g[i][j+1] * g[i][j+2] * g[i][j+3],
# g[j][i] * g[j+1][i] * g[j+2][i] * g[j+3][i])
if i < rows - path_size:
pdd = max(product([g[i+s][j+s] for s in range(path_size)]),
product([g[i+s][j+path_size-s-1] for s in range(path_size)]))
#pdd = max(g[i][j] * g[i+1][j+1] * g[i+2][j+2] * g[i+3][j+3],
# g[i][j+3] * g[i+1][j+2] * g[i+2][j+1] * g[i+3][j])
maxp = max(maxp, phv, pdd)
return maxp
#------------------------------------------------------------------------------
def main():
print "PROBLEM:\n"
for line in __doc__.strip().split('\n'):
print '\t', line
print "\nSOLUTION:"
print "\n\t", solve()
#------------------------------------------------------------------------------
if __name__ == "__main__":
main() |
#!/usr/bin/env python
import json
my_list = range(10)
my_list.append('whatever')
my_list.append('some thing')
my_dict = {
'ke | y1': 'val1',
'key2': 'val2',
'key3': 'val3'
}
my_dict['key4'] = my_list
my_dict['key5'] = False
print my_dict
print json.dumps(my_dict)
with o | pen("my_file.json", "w") as f:
json.dump(my_dict, f)
|
"""This submodule contains objects for handling different representations of models and model functions | .
:synopsis: This submodule contains objects for handling different representations of models and model functions.
.. moduleauthor:: Johannes | Gaessler <johannes.gaessler@student.kit.edu>
"""
from ._base import *
from .yaml_drepr import *
|
from pygfa.graph_element.parser import line, field_validator as fv
SERIALIZATION_ERROR_MESSAGGE = "Couldn't serialize object identified by: "
def _format_exception(identifier, exception):
return SERIALIZATION_ERROR_MESSAGGE + identifier \
+ "\n\t" + repr(exception)
def _remove_common_edge_fields | (edge_dict):
edge_dict.pop('eid')
edge_dict.pop('from_node')
edge_dict.pop('from_orn')
edge_dict.pop('to_node')
edge_dict.pop('to_orn')
edge_dict.pop('from_positio | ns')
edge_dict.pop('to_positions')
edge_dict.pop('alignment')
edge_dict.pop('distance')
edge_dict.pop('variance')
def _serialize_opt_fields(opt_fields):
fields = []
for key, opt_field in opt_fields.items():
if line.is_optfield(opt_field):
fields.append(str(opt_field))
return fields
def _are_fields_defined(fields):
try:
for field in fields:
if field is None:
return False
except:
return False
return True
def _check_fields(fields, required_fields):
"""Check if each field has the correct format as
stated from the specification.
"""
try:
for field in range(0, len(required_fields)):
if not fv.is_valid(fields[field], required_fields[field]):
return False
return True
except:
return False
def _check_identifier(identifier):
if not isinstance(identifier, str):
identifier = "'{0}' - id of type {1}.".format(\
str(identifier), \
type(identifier) \
)
return identifier
|
from django.conf.urls import patterns, include, url
from django.views.generic import TemplateV | iew
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url( | r'^$', TemplateView.as_view(template_name='base.html'))
)
|
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Experimental API for building input pipelines.
This module contains experimental `Dataset` sources and transformations that can
be used in conjunction with the `tf.data.Dataset` API. Note that the
`tf.contrib.data` API is not subject to the same backwards compatibility
guarantees as `tf.data`, but we will provide deprecation advice in advance of
removing existing functionality.
See [Importing Data](https://tensorflow.org/guide/datasets) for an overview.
@@Counter
@@CheckpointInputPipelineHook
@@CsvDataset
@@LMDBDataset
@@Optional
@@RandomDataset
@@Reducer
@@SqlDataset
@@TFRecordWriter
@@assert_element_shape
@@batch_and_drop_remainder
@@bucket_by_sequence_length
@@choose_from_datasets
@@copy_to_device
@@dense_to_sparse_batch
@@enumerate_dataset
@@get_next_as_optional
@@get_single_element
@@group_by_reducer
@@group_by_window
@@ignore_errors
@@make_batched_features_dataset
@@make_csv_dataset
@@make_saveable_from_iterator
@@map_and_batch
@@padded_batch_and_drop_remainder
@@parallel_interleave
@@parse_example_dataset
@@prefetch_to_device
@@read_batch_features
@@rejection_resample
@@reduce_dataset
@@sample_from_datasets
@@scan
@@shuffle_and_repeat
@@sliding_window_batch
@@sloppy_interleave
@@unbatch
@@unique
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
# pylint: disable=unused-import
from tensorflow.contrib.data.python.ops.batching import as | sert_element_shape
from tensorflow.contrib.data.python.ops.batching import batch_and_drop_remainder
from tensorflow.contrib.data.python.ops.batching import dense_to_sparse_batch
from tensorflow.contrib.data.python.ops.batching import map_and_batch
from tensorflow.contrib.data.python.ops.batching import padded_batch_and_drop_remainder
from tensorflow.contrib.data.python | .ops.batching import unbatch
from tensorflow.contrib.data.python.ops.counter import Counter
from tensorflow.contrib.data.python.ops.enumerate_ops import enumerate_dataset
from tensorflow.contrib.data.python.ops.error_ops import ignore_errors
from tensorflow.contrib.data.python.ops.get_single_element import get_single_element
from tensorflow.contrib.data.python.ops.get_single_element import reduce_dataset
from tensorflow.contrib.data.python.ops.grouping import bucket_by_sequence_length
from tensorflow.contrib.data.python.ops.grouping import group_by_reducer
from tensorflow.contrib.data.python.ops.grouping import group_by_window
from tensorflow.contrib.data.python.ops.grouping import Reducer
from tensorflow.contrib.data.python.ops.interleave_ops import choose_from_datasets
from tensorflow.contrib.data.python.ops.interleave_ops import parallel_interleave
from tensorflow.contrib.data.python.ops.interleave_ops import sample_from_datasets
from tensorflow.contrib.data.python.ops.interleave_ops import sloppy_interleave
from tensorflow.contrib.data.python.ops.iterator_ops import CheckpointInputPipelineHook
from tensorflow.contrib.data.python.ops.iterator_ops import make_saveable_from_iterator
from tensorflow.contrib.data.python.ops.parsing_ops import parse_example_dataset
from tensorflow.contrib.data.python.ops.prefetching_ops import copy_to_device
from tensorflow.contrib.data.python.ops.prefetching_ops import prefetch_to_device
from tensorflow.contrib.data.python.ops.random_ops import RandomDataset
from tensorflow.contrib.data.python.ops.readers import CsvDataset
from tensorflow.contrib.data.python.ops.readers import LMDBDataset
from tensorflow.contrib.data.python.ops.readers import make_batched_features_dataset
from tensorflow.contrib.data.python.ops.readers import make_csv_dataset
from tensorflow.contrib.data.python.ops.readers import read_batch_features
from tensorflow.contrib.data.python.ops.readers import SqlDataset
from tensorflow.contrib.data.python.ops.resampling import rejection_resample
from tensorflow.contrib.data.python.ops.scan_ops import scan
from tensorflow.contrib.data.python.ops.shuffle_ops import shuffle_and_repeat
from tensorflow.contrib.data.python.ops.sliding import sliding_window_batch
from tensorflow.contrib.data.python.ops.unique import unique
from tensorflow.contrib.data.python.ops.writers import TFRecordWriter
from tensorflow.python.data.ops.iterator_ops import get_next_as_optional
from tensorflow.python.data.ops.optional_ops import Optional
# pylint: enable=unused-import
from tensorflow.python.util.all_util import remove_undocumented
remove_undocumented(__name__)
# A constant that can be used to enable auto-tuning.
AUTOTUNE = -1
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from telesto import plot
OFFSET = 1000 # REMEMBER TO CALCULATE MANUALLY!
def main():
path = sys.argv[1]
mean = plot.mean(
| plot.rows(path, plot.parse_middleware_line),
("waiting", "parsing", "database", "response")
)
dev = plot.standard_deviation(
plot.rows(path, plot.parse_middleware_line), mean
)
print "#",
for key in mean:
print key,
print
for key in mean:
print mean[key],
print
for key in mean:
p | rint 100.0 * mean[key] / sum(mean.itervalues()),
print
for key in mean:
print dev[key],
print
if __name__ == "__main__":
main()
|
def len2mask(len):
"""Convert a bit length to a dotted netmask (aka. CIDR to netmask)"""
mask | = ''
if not isinstance(len, int) or len < 0 or len > 32:
print "Illegal subnet length: %s (which is a %s)" % \
(str(len), type(len).__name__)
return None
for t in range(4):
if len > 7:
mask += '255.'
else:
dec = 255 - (2**(8 - len) - 1)
mask += str(dec) + '.'
len -= 8
if len < 0:
len = 0
return mask[:-1]
def mask2len(subnet):
"""Convert a dotted netmask t | o bit length (aka. netmask to CIDR)"""
octets = [int(x) for x in subnet.split(".")]
count = 0
for octet in octets:
highest_bit = 128
while highest_bit > 0:
if octet >= highest_bit:
count = count + 1
octet = octet - highest_bit
highest_bit = highest_bit / 2
else:
return count
return count
class FilterModule(object):
''' utility to convert cidr netmasks into len and reverse '''
def filters(self):
return {'mask2len': mask2len,
'len2mask': len2mask}
|
# coding=utf-8
import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.getcwd(), "..")))
from db.MysqlUtil import initMysql, execute, select, batchInsert, disconnect
from common.JsonHelper import loadJsonConfig
from api.tushareApi import getSimpleHistoryData
from datetime import datetime, timedelta
from common.LoggerHelper import writeErrorLog, writeWarningLog, writeInfoLog, writeDebugLog, writeLog, writeExceptionLog
from wechat.weChatSender import sendMessageToMySelf
from common.HttpHelper import httpGet
from common.FileHelper import saveFile
import time
import json
# 从同花顺抓取历史行情数据(前复权)
def updateStockHistoryInfoByTHS(stockList):
for stock in stockList:
code = stock[0]
i = 2010
thisYear = datetime.now().year
while (i <= thisYear):
# time.sleep(1)
infos = getStockInfos(code, i)
if infos is None:
continue
for date in infos:
open = infos.get(date).get('open')
close = infos.get(date).get('close')
high = infos.get(date).get('high')
low = infos.get(date).get('low')
volume = infos.get(date).get('volume')
amount = infos.get(date).get('amount')
checkExistSql = unicode("select count(*) from s_stock where code='{0}' and date='{1}'").format(code,
date)
count = select(checkExistSql, False)[0]
if count > 0:
updateSql = unicode(
"update s_stock set volume={2},highPrice={3},lowPrice={4},openPrice={5},closePrice={6},amount='{7}' where c | ode='{0}' and date='{1}'").format(
code, date, volume, high, low, open, close, amount)
execute(updateSql)
print code, date, updateSql
else:
insertSql = unicode(
"insert into s_stock(code,date,timestamp,volume,highPric | e,lowPrice,openPrice,closePrice,amount) VALUES ('{0}','{1}',{2},{3},{4},{5},{6},{7},'{8}')").format(
code, date, int(time.mktime(time.strptime(date, '%Y-%m-%d'))), volume, high, low, open, close,
amount)
execute(insertSql)
print code, date, insertSql
i = i + 1
# 解析同花顺年行情数据(前复权)
def getStockInfos(code, year):
try:
url = "http://d.10jqka.com.cn/v2/line/hs_{0}/01/{1}.js".format(code, year)
res = httpGet(url).decode("utf-8")
index = res.find("(")
if (index < 0):
writeErrorLog(unicode("解析行情失败: code:{0}, year:{1}, res:{2}").format(code, year, res))
return []
res = res[index + 1:-1]
writeLog(unicode("获取股票历史行情: code: {0}, year:{1}").format(code, year))
jo = json.loads(res)
dataInfo = jo['data'].split(';')
result = {}
for item in dataInfo:
infos = item.split(',')
dic = {}
dic['open'] = infos[1]
dic['high'] = infos[2]
dic['low'] = infos[3]
dic['close'] = infos[4]
dic['volume'] = infos[5]
dic['amount'] = "{0}亿".format(round(float(infos[6]) / 100000000, 1))
result[datetime.strptime(infos[0], '%Y%m%d').strftime('%Y-%m-%d')] = dic
return result
except Exception, e:
writeErrorLog(unicode("解析行情失败: code:{0}, year:{1}, e:{2}").format(code, year, str(e)))
if "404" in str(e):
return []
else:
return None
def getStockHistoryInfoFromDb():
sql = unicode("SELECT code,count(*) from s_stock GROUP by code HAVING count(*)<20")
data = select(sql)
updateStockHistoryInfoByTHS(data)
def getStockHistoryInfoFromConfig():
stockList = loadJsonConfig(os.path.abspath(os.path.join(os.getcwd(), "../config/newStockList.json")))
updateStockHistoryInfoByTHS(stockList)
def updateAllStockHistoryInfo():
sql = unicode("select code,name from s_stock_info order by code asc")
data = select(sql)
updateStockHistoryInfoByTHS(data)
def updateStockOtherInfo():
sql = unicode("select code,name from s_stock_info order by code asc")
stockList = select(sql)
for stock in stockList:
code = stock[0]
if int(code) < 601126:
continue
selectInfoSql = unicode("select date,closePrice from s_stock where code='{0}' order by date asc").format(code)
data = select(selectInfoSql)
writeLog(unicode("更新股票其他指标数据: code: {0}").format(code))
updataStockBias(code, data, 6)
updataStockBias(code, data, 12)
updataStockBias(code, data, 24)
updateStockMA(code, data, 5)
updateStockMA(code, data, 10)
updateStockMA(code, data, 20)
updateStockMA(code, data, 30)
updateStockMA(code, data, 60)
updateStockMA(code, data, 120)
updateStockMA(code, data, 250)
updateStockChangePercent(code, data)
def updateStockChangePercent(code, data):
for i in range(1, len(data)):
try:
changeAmount = data[i][1] - data[i - 1][1]
changePercent = round(changeAmount * 100 / data[i - 1][1], 2)
updateSql = unicode(
"update s_stock set changePercent={0},changeAmount={1} where code='{2}' and date='{3}'").format(
changePercent, changeAmount, code, data[i][0])
execute(updateSql)
except Exception, e:
writeErrorLog(
unicode("更新涨幅数据失败: code:{0}, i:{1}, date:{2}, closePrice:{3}").format(code, i, data[i][0], data[i][1]))
def updateStockMA(code, data, n):
for i in range(n - 1, len(data)):
j = i
sum = 0
while (i - j < n):
sum = sum + data[j][1]
j = j - 1
avg = round(sum / n, 2)
sql = unicode("update s_stock set MA{0}={1} where code='{2}' and date='{3}'").format(n, avg, code, data[i][0])
execute(sql)
def updataStockBias(code, data, n):
for i in range(n - 1, len(data)):
j = i
sum = 0
while (i - j < n):
sum = sum + data[j][1]
j = j - 1
avg = round(sum / n, 2)
todayClosePrice = float(data[i][1])
bias = 0 if avg == 0 else round((todayClosePrice - avg) * 100 / avg, 2)
number = 1 if n == 6 else (2 if n == 12 else 3)
sql = unicode("update s_stock set BIAS{0}={1} where code='{2}' and date='{3}'").format(number, bias, code,
data[i][0])
execute(sql)
def main(argv):
try:
reload(sys)
sys.setdefaultencoding('utf-8')
# sendMessageToMySelf(unicode("开始查询股票历史行情数据"))
begin = datetime.now()
initMysql()
# getStockHistoryInfoFromDb()
# getStockHistoryInfoFromConfig()
updateStockOtherInfo()
disconnect()
end = datetime.now()
message = unicode("查询股票历史行情数据的任务执行完毕,当前时间:{0},执行用时:{1}").format(datetime.now(), end - begin)
writeLog(message)
sendMessageToMySelf(message)
except:
writeExceptionLog('RealTimeRemindTask Error.')
if __name__ == '__main__':
main(sys.argv)
|
from . | import *
offline_providers = {
'builtin': builtin.ProviderBuil | tin,
}
|
"""
SleekXMPP: The Sleek XMPP Library
Copyright (C) 2011 Nathanael C. Fritz, Lance J.T. Stout
This file is part of SleekXMPP.
See the file LICENSE for copying permissio
"""
import logging
import sleekxmpp
from sleekxmpp.stanza import Message
from sleekxmpp.xmlstream.handler import Callback
from sleekxmpp.xmlstream.matcher import StanzaPath
from sleekxmpp.xmlstream import register_stanza_plugin, ElementBase, ET
from sleekxmpp.plugins import BasePlugin
from sleekxmpp.plugins.xep_0085 import stanza, ChatState
log = logging.getLogger(__name__)
class XEP_0085(BasePlugin):
"""
XEP-0085 Chat State Notifications
"""
name = 'xep_0085'
description = 'XEP-0085: Chat State Notifications'
dependencies = set(['xep_0030'])
stanza = stanza
def plugin_init(self):
self.xmpp.register_handler(
Callback('Chat State',
StanzaPath('message/chat_state'),
self._handle_chat_state))
register_stanza_plugin(Message, stanza.Active)
register_stanza_plugin(Message, stanza.Composing)
register_stanza_plugin(Message, stanza.Gone)
re | gister_stanza_plugin(Message, stanza.Inactive)
register_stanza_plugin(Message, stanza.Paused)
def plugin_end(self):
s | elf.xmpp.remove_handler('Chat State')
def session_bind(self, jid):
self.xmpp.plugin['xep_0030'].add_feature(ChatState.namespace)
def _handle_chat_state(self, msg):
state = msg['chat_state']
log.debug("Chat State: %s, %s", state, msg['from'].jid)
self.xmpp.event('chatstate_%s' % state, msg)
|
er
def put(self, item, block=True, timeout=None):
"""Process event."""
self._handler(item)
def _get_entity_from_soco_uid(hass, uid):
"""Return SonosEntity from SoCo uid."""
for entity in hass.data[DATA_SONOS].entities:
if uid == entity.unique_id:
return entity
return None
def soco_error(errorcodes=None):
"""Filter out specified UPnP errors from logs and avoid exceptions."""
def decorator(funct):
"""Decorate functions."""
@ft.wraps(funct)
def wrapper(*args, **kwargs):
"""Wrap for all soco UPnP exception."""
try:
return funct(*args, **kwargs)
except SoCoUPnPException as err:
if errorcodes and err.error_code in errorcodes:
pass
else:
_LOGGER.error("Error on %s with %s", funct.__name__, err)
except SoCoException as err:
_LOGGER.error("Error on %s with %s", funct.__name__, err)
return wrapper
return decorator
def soco_coordinator(funct):
"""Call function on coordinator."""
@ft.wraps(funct)
def wrapper(entity, *args, **kwargs):
"""Wrap for call to coordinator."""
if entity.is_coordinator:
return funct(entity, *args, **kwargs)
return funct(entity.coordinator, *args, **kwargs)
return wrapper
def _timespan_secs(timespan):
"""Parse a time-span into number of seconds."""
if timespan in ('', 'NOT_IMPLEMENTED', None):
return None
return sum(60 ** x[0] * int(x[1]) for x in enumerate(
reversed(timespan.split(':'))))
def _is_radio_uri(uri):
"""Return whether the URI is a radio stream."""
radio_schemes = (
'x-rincon-mp3radio:', 'x-sonosapi-stream:', 'x-sonosapi-radio:',
'x-sonosapi-hls:', 'hls-radio:')
return uri.startswith(radio_schemes)
class SonosEntity(MediaPlayerDevice):
"""Representation of a Sonos entity."""
def __init__(self, player):
"""Initialize the Sonos entity."""
self._seen = None
self._subscriptions = []
self._receives_events = False
self._volume_increment = 2
self._unique_id = player.uid
self._player = player
self._model = None
self._player_volume = None
self._player_muted = None
self._shuffle = None
self._name = None
self._coordinator = None
self._sonos_group = [self]
self._status = None
self._media_duration = None
self._media_position = None
self._media_position_updated_at = None
self._media_image_url = None
self._media_artist = None
self._media_album_name = None
self._media_title = None
self._night_sound = None
self._speech_enhance = None
self._source_name = None
self._available = True
self._favorites = None
self._soco_snapshot = None
self._snapshot_group = None
self._set_basic_information()
self.seen()
async def async_added_to_hass(self):
"""Subscribe sonos events."""
self.hass.data[DATA_SONOS].entities.append(self)
self.hass.async_add_executor_job(self._subscribe_to_player_events)
@property
def unique_id(self):
"""Return a unique ID."""
return self._unique_id
def __hash__(self):
"""Return a hash of self."""
return hash(self.unique_id)
@property
def name(self):
"""Return the name of the entity."""
return self._name
@property
def device_info(self):
"""Return information about the device."""
return {
'identifiers': {
(SONOS_DOMAIN, self._unique_id)
},
'name': self._name,
'model': self._model.replace("Sonos ", ""),
'manufacturer': 'Sonos',
}
@property
@soco_coordinator
def state(self):
"""Return the state of the entity."""
if self._status in ('PAUSED_PLAYBACK', 'STOPPED'):
return STATE_PAUSED
if self._status in ('PLAYING', 'TRANSITIONING'):
return STATE_PLAYING
return STATE_IDLE
@property
def is_coordinator(self):
"""Return true if player is a coordinator."""
return self._coordinator is None
@property
def soco(self):
"""Return soco object."""
return self._player
@property
def coordinator(self):
"""Return coordinator of this player."""
return self._coordinator
def seen(self):
"""Record that this player was seen right now."""
self._seen = time.monotonic()
if self._available:
return
self._available = True
self._set_basic_information()
self._subscribe_to_player_events()
self.schedule_update_ha_state()
def check_unseen(self):
"""Make this player unavailable if it was not seen recently."""
if not self._available:
return
if self._seen < time.monotonic() - 2*DISCOVERY_INTERVAL:
self._available = False
def _unsub(subscriptions):
for subscription in subscriptions:
subscription.unsubscribe()
self.hass.add_job(_unsub, self._subscriptions)
self._subscriptions = []
self.schedule_update_ha_state()
@property
def available(self) -> bool:
"""Return True if entity is available."""
return self._available
def _set_basic_information(self):
"""Set initial entity information."""
speaker_info = self.soco.get_speaker_info(True)
self._name = speaker_info['zone_name']
self._model = speaker_info['model_name']
self._shuffle = self.soco.shuffle
self.update_volume()
self._set_favorites()
def _set_favorites(self):
"""Set available favorites."""
favorites = self.soco.music_library.get_sonos_favorites()
# Exclude favorites that are non-playable due to no linked resources
self._favorites = [f for f in favorites if f.reference.resources]
def _radio_artwork(self, url):
"""Return the private URL with artwork for a radio stream."""
if url not in ('', 'NOT_IMPLEMENTED', None):
if url.find('tts_proxy') > 0:
# If the content is a tts don't try to fetch an image from it.
return None
url = 'http://{host}:{port}/getaa?s=1&u={uri}'.format(
host=self.soco.ip_address,
port=1400,
uri=urllib.parse.quote(url, safe='')
)
return url
def _subscribe_to_player_events(self):
"""Add event subscriptions."""
self._receives_events = False
# New player available, build the current group topology
for entity in self.hass.data[DATA_SONOS].entities:
entity.update_groups()
player = self.soco
def subscribe(service, action):
"""Add a subscription to a pysonos service."""
queue = _ProcessSonosEventQueue(action)
sub = service.subscribe(auto_renew=True, event_queue=queue)
self._subscriptions.append(sub)
subscribe(player.avTransport, self.update_media)
subscribe(player.renderingControl, self.update_volume)
subscribe(player.zoneGroupTopology, self.update_grou | ps)
subscribe(player.contentDirectory, self.update_content)
def update(self):
"""Retrieve latest state."""
if self._available and not self._receives_events:
try:
self.update_groups()
self.update_volume()
if self.is_coordinator:
self.update_media()
except SoC | oException:
pass
def update_media(self, event=None):
"""Update information about currently playing media."""
transport_info = self.soco.get_current_transport_info()
new_status = transport_info.get('current_transport_state')
# Ignore transiti |
"""
The latest version of this package is available at:
<http://github.com/jantman/webhook2lambda2sqs>
################################################################################
Copyright 2016 Jason Antman <jason@jasonantman.com> <http://www.jasonantman.com>
This file is part of webhook2lambda2sqs, also known as webhook2lambda2sqs.
webhook2lambda2sqs is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
webhook | 2lambda2sqs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with webhook2lambda2sqs. If not, see <http://www.gnu.org/licenses/>.
The Copyright and Authors attributions contained herein may not be removed or
otherwise altered, except to add the Author attribution of a contributor to
this work. (Additional Terms pursuant to Section 7b of the AGPL v3)
################################################################################
While not legally required, I sincerely request that anyone who finds
bugs please submit them at <https://github.com/jantman/webhook2lambda2sqs> or
to me via email, and that you send any contributions or improvements
either as a pull request on GitHub, or to me via email.
################################################################################
AUTHORS:
Jason Antman <jason@jasonantman.com> <http://www.jasonantman.com>
################################################################################
"""
VERSION = '0.2.0'
PROJECT_URL = 'https://github.com/jantman/webhook2lambda2sqs'
|
import os
CA | CHE_PORT = 8080
HTTP_PORT = 8000
HTTPS_PORT = 4430
DEFAULT_MAX_AGE = 3600
DEFAULT_LOG_PATH = os.path.dirname(__file__)
DEFAULT_LOG_FILE = os.path.join(DEFAULT_LOG_PATH, 'default-hendrix.lo | g')
|
# SET THE FOLLOWING EITHER BEFORE RUNNING THIS FILE OR BELOW, BEFORE INITIALIZING
# PRAW!
# set variables for interacting with reddit.
# my_user_agent = ""
# my_username = ""
# my_password = ""
# reddit_post_id = -1
# import praw - install: pip install praw
# Praw doc: https://praw.readthedocs.org/en/latest/index.html
import praw
# python base imports
import datetime
import pprint
# import python_utilities
from python_utilities.logging.summary_helper import SummaryHelper
# declare variables.
my_summary_helper = None
r = None
post = None
comments = None
flat_comments = None
test_comment = None
comment_prop_map = None
summary_string = ""
# set variables for interacting with reddit.
my_user_agent = "<user_agent>"
my_username = "<reddit_username>"
my_password = "<reddit_password>"
#reddit_post_id = "1cp0i3"
reddit_post_id = "1bvkol"
# init summary helper.
my_summary_helper = SummaryHelper()
print( "Starting PRAW test at " + str( start_dt ) )
# set user agent.
r = praw.Reddit( user_agent = my_user_agent )
# got login set?
if ( ( ( my_username ) and ( my_username != "" ) ) and ( ( my_password ) and ( my_password != "" ) ) ):
# yes. Login.
r.login( my_username, my_password )
print( "==> Logged in." )
#-- END check to see if we log in. --#
print( "==> Created reddit instance." )
# retrieve post
# - post with lots of comments - 1bvkol has 22014
#reddit_post_id = "1bvkol"
post = r.get_submission( submission_id = reddit_post_id, comment_limit = 1500, comment_sort = "old" )
print( "Retrieved post " + str( reddit_post_id ) )
# output number of comments based on post
print( "==> post.permalink: " + post.permalink )
print( "==> post.num_comments: " + str( post.num_comments ) )
# use the replace_more_comments() method to pull in as many comments as possible.
post.replace_more_comments( limit = None, threshold = 0 )
print( "==> After replace_more_comments()" )
# get the comments
comments = post.comments
# print out number of comments
print( "==> len( comments ): " + str( len( comments ) ) ) # 3,915 and counting
# these are objects where parent comments reference children. flatten...
flat_comments = praw.helpers.flatten_tree( post.comments )
# how many now?
print( "==> after flatten_tree(), comment count: " + str ( len( flat_comments ) ) ) # 13364 - closer to 22000, but still not all of them.
# get a comment
test_comment = flat_comments[ 0 ]
print( "Looking at comment 0:")
# what is in it?
print( "==> str( comment ): " + str( test_comment ) ) # outputs the text of comment, nothing more.
# reddit ID of comment:
print( "==> comment id: " + str( test_comment.id ) )
# body of comment
print( "==> comment body: " + test_comment.body )
# to get map of property names to values in a praw object:
comment_prop_map = vars( test_comment )
# pretty-print it with pprint library.
pprint.pprint( comment_prop_map )
'''
Example:
{'_info_url': 'http://www.reddit.com/api/info/',
'_replies': [<praw.objects.MoreComments object at 0x4867550>],
'_submission': <praw.objects.Submission object at 0x4867790>,
'_underscore_names': ['replies'],
'approved_by': None,
'author': Redditor(user_name='worldclasssteez'),
'author_flair_css_class': None,
'author_flair_text': None,
'banned_by': None,
'body': u'Using the "J" and "K" keys on VLC player to sync up the audio. ',
'body_html': u'<div class="md"><p>Using the &quot;J&quot; and &quot;K&quot; keys on VLC player to sync up the audio. </p>\n</div>',
'created': 1365399487.0,
'created_utc': 1365395887.0,
'distinguished': None,
'downs': 44,
'edited': False,
'gilded': 0,
'has_fetched': True,
'id': u'c9ap3fp',
'json_dict': None,
'likes': None,
'link_id': u't3_1bvkol',
'name': u't1_c9ap3fp',
'num_reports': None,
'parent_id': u't3_1bvkol',
'reddit_session': <praw.Reddit object at 0x48539d0>,
'saved': False,
'score_hidden': False,
'subreddit': Subreddit(display_name='AskReddit'),
'subreddit_id': u't5_2qh1i',
'ups': 201}
'''
# each name in that map can be invoked as a variable on the object itself.
# test summary counter
my_summary_helper.set_prop_value( "comment_count", 0 )
# look at utc date order of comments:
# for comment in flat_comments[ 0:15 ]:
for index in range( 0, len( flat_comments ) ):
# get vars
comment = flat_comments[ index ]
comment_prop_map = vars( comment )
created_from_map = comment_prop_map[ 'created_utc' ]
created_from_obj = comment.created_utc
created_dt = datetime.datetime.fromtimestamp( created_from_map )
comment_id = comment.name
print( "==> " + str( index ) + " ( " + comment_id + " ) - Created UTC: " + str( created_from_map ) + " (map); " + str( created_from_obj ) + " (obj); " + str( created_dt ) )
# increment comment count
my_summary_helper.increment_prop_value( "comment_count" )
#-- END loop over comments --#
print( "==> Created: " + st | r( created_from_map ) + " (map); " + str( created_from_obj ) + " (obj); " + str( created_dt | ) )
summary_string = "\nPRAW testing complete!\n"
# generate summary string
# set stop time.
my_summary_helper.set_stop_time()
# generate summary string.
summary_string += my_summary_helper.create_summary_string( item_prefix_IN = "==> " )
print( summary_string ) |
"""
Fiber-based fixed point location in the Lorenz system
f(v)[0] = s*(v[1]-v[0])
f(v)[1] = r*v[0] - v[1] - v[0]*v[2]
f(v)[2] = v[0]*v[1] - b*v[2]
Reference:
http://www.emba.uvm.edu/~jxyang/teaching/Math266notes13.pdf
https://en.wikipedia.org/wiki/Lorenz_system
"""
import numpy as np
import matplotlib.pyplot as pt
import scipy.integrate as si
import dfibers.traversal as tv
import dfibers.numerical_utilities as nu
import dfibers.fixed_points as fx
import dfibers.solvers as sv
from mpl_toolkits.mplot3d import Axes3D
N = 3
s, b, r = 10, 8./3., 28
def f(v):
return np.array([
s*(v[1,:]-v[0,:]),
r*v[0,:] - v[1,:] - v[0,:]*v[2,:],
v[0,:]*v[1,:] - b*v[2,:],
])
def ef(v):
return 0.001*np.ones((N,1))
def Df(v):
Dfv = np.empty((v.shape[1],3,3))
Dfv[:,0,0], Dfv[:,0,1], Dfv[:,0,2] = -s, s, 0
Dfv[:,1,0], Dfv[:,1,1], Dfv[:,1,2] = r - v[2], -1, -v[0]
Dfv[:,2,0], Dfv[:,2,1], Dfv[:,2,2] = v[1], v[0], -b
return Dfv
if __name__ == "__main__":
# Collect attractor points
t = np.arange(0,40,0.01)
v = np.ones((N,1))
A = si.odeint(lambda v, t: f(v.reshape((N,1))).flatten(), v.flatten(), t).T
# Set up fiber arguments
v = np.zeros((N,1))
# c = np.random.randn(N,1)
c = np.array([[0.83736021, -1.87848114, 0.43935044]]).T
fiber_kwargs = {
"f": f,
"ef": ef,
"Df": Df,
"compute_step_amount": lambda trace: (0.1, 0, False),
"v": v,
"c": c,
"terminate": lambda trace: (np.fabs(trace.x[:N,:]) > 50).any(),
"max_step_size": 1,
"max_traverse_steps": 2000,
"max_solve_iterations": 2**5,
}
print("using c:")
print(c.T)
# Visualize strange attractor
ax = pt.gca(projection="3d")
ax.plot(*A, color='gray', linestyle='-', alpha=0.5)
br1 = np.sqrt(b*(r-1))
U = np.array([[0, 0, 0],[br1,br1,r-1],[-br1,-br1,r-1]]).T
ax.scatter(*U, color='black')
# Run and visualize fiber components, for each fxpt
xlims, ylims, zlims = [-20,20], [-30,30], [-20,60]
for fc in [0,2]:
# start from current fxp | t
fiber_kwargs["v"] = U[:,[fc]]
| # ax.text(U[0,fc],U[1,fc],U[2,fc], str(fc))
# Run in one direction
solution = sv.fiber_solver(**fiber_kwargs)
V1 = np.concatenate(solution["Fiber trace"].points, axis=1)[:N,:]
z = solution["Fiber trace"].z_initial
# Run in other direction (negate initial tangent)
fiber_kwargs["z"] = -z
solution = sv.fiber_solver(**fiber_kwargs)
V2 = np.concatenate(solution["Fiber trace"].points, axis=1)[:N,:]
# Join fiber segments, restrict to figure limits
V = np.concatenate((np.fliplr(V1), V2), axis=1)
V = V[:,::50]
for i, (lo, hi) in enumerate([xlims, ylims, zlims]):
V = V[:,(lo < V[i,:]) & (V[i,:] < hi)]
C = f(V)
# Visualize fiber
ax.plot(*V, color='black', linestyle='-')
ax.quiver(*np.concatenate((V,.1*C),axis=0),color='black')
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
ax.view_init(elev=15,azim=145)
pt.tight_layout()
pt.show()
|
OTHER DEALINGS IN THE
# SOFTWARE.
# =============================================================================
# DOCS
# =============================================================================
"""Synthetic light curve generator.
"""
# =============================================================================
# IMPORTS
# =============================================================================
import numpy as np
from .base import Data
# =============================================================================
# CONSTANTS
# =============================================================================
DS_NAME = "feets-synthetic"
DESCRIPTION = "Lightcurve created with random numbers"
BANDS = ("B", "V")
METADATA = None
DEFAULT_SIZE = 10000
# =============================================================================
# FUNCTIONS
# =============================================================================
def create_random(
magf,
magf_params,
errf,
errf_params,
timef=np.linspace,
timef_params=None,
size=DEFAULT_SIZE,
id=None,
ds_name=DS_NAME,
description=DESCRIPTION,
bands=BANDS,
metadata=METADATA,
):
"""Generate a data with any given random function.
Parameters
----------
magf : callable
Function to generate the magnitudes.
magf_params : dict-like
Parameters to feed the `magf` function.
errf : callable
Function to generate the magnitudes.
errf_params : dict-like
Parameters to feed the `errf` function.
timef : callable, (default=numpy.linspace)
Function to generate the times.
timef_params : dict-like or None, (default={"start": 0., "stop": 1.})
Parameters to feed the `timef` callable.
size : int (default=10000)
Number of obervation of the light curves
id : object (default=None)
Id of the created data.
ds_name : str (default="feets-synthetic")
Name of the dataset
description : str (default="Lightcurve created with random numbers")
Description of the data
bands : tuple of strings (default=("B", "V"))
The bands to be created
metadata : dict-like or None (default=None)
The metadata of the created data
Returns
-------
data
A Data object with a random lightcurves.
Examples
--------
.. code-block:: pycon
>>> from numpy import random
>>> create_random(
... magf=random.normal, magf_params={"loc": 0, "scale": 1},
... errf=random.normal, errf_params={"loc": 0, "scale": 0.008})
Data(id=None, ds_name='feets-synthetic', bands=('B', 'V'))
"""
timef_params = (
{"start": 0.0, "stop": 1.0}
if timef_params is None
else timef_params.copy()
)
timef_params.update(num=size)
magf_params = magf_params.copy()
magf_params.update(size=size)
errf_params = errf_params.copy()
errf_params.update(size=size)
data = {}
for band in bands:
data[band] = {
"time": timef(**timef_params),
"magnitude": magf(**magf_params),
"error": errf(**errf_params),
}
return Data(
| id=id,
ds_name=ds_name,
desc | ription=description,
bands=bands,
metadata=metadata,
data=data,
)
def create_normal(
mu=0.0, sigma=1.0, mu_err=0.0, sigma_err=1.0, seed=None, **kwargs
):
"""Generate a data with magnitudes that follows a Gaussian
distribution. Also their errors are gaussian.
Parameters
----------
mu : float (default=0)
Mean of the gaussian distribution of magnitudes
sigma : float (default=1)
Standar deviation of the gaussian distribution of magnitude errors
mu_err : float (default=0)
Mean of the gaussian distribution of magnitudes
sigma_err : float (default=1)
Standar deviation of the gaussian distribution of magnitude errorrs
seed : {None, int, array_like}, optional
Random seed used to initialize the pseudo-random number generator.
Can be any integer between 0 and 2**32 - 1 inclusive, an
array (or other sequence) of such integers, or None (the default).
If seed is None, then RandomState will try to read data from
/dev/urandom (or the Windows analogue) if available or seed from
the clock otherwise.
kwargs : optional
extra arguments for create_random.
Returns
-------
data
A Data object with a random lightcurves.
Examples
--------
.. code-block:: pycon
>>> ds = create_normal(0, 1, 0, .0008, seed=42)
>>> ds
Data(id=None, ds_name='feets-synthetic', bands=('B', 'V'))
>>> ds.data.B
LightCurve(time[10000], magnitude[10000], error[10000])
>>> ds.data.B.time
array([ 0.00000000e+00, 1.00010001e-04, 2.00020002e-04, ...,
9.99799980e-01, 9.99899990e-01, 1.00000000e+00])
"""
random = np.random.RandomState(seed)
return create_random(
magf=random.normal,
magf_params={"loc": mu, "scale": sigma},
errf=random.normal,
errf_params={"loc": mu_err, "scale": sigma_err},
**kwargs,
)
def create_uniform(
low=0.0, high=1.0, mu_err=0.0, sigma_err=1.0, seed=None, **kwargs
):
"""Generate a data with magnitudes that follows a uniform
distribution; the error instead are gaussian.
Parameters
----------
low : float, optional
Lower boundary of the output interval. All values generated will be
greater than or equal to low. The default value is 0.
high : float, optional
Upper boundary of the output interval. All values generated will be
less than high. The default value is 1.0.
mu_err : float (default=0)
Mean of the gaussian distribution of magnitudes
sigma_err : float (default=1)
Standar deviation of the gaussian distribution of magnitude errorrs
seed : {None, int, array_like}, optional
Random seed used to initialize the pseudo-random number generator.
Can be any integer between 0 and 2**32 - 1 inclusive, an
array (or other sequence) of such integers, or None (the default).
If seed is None, then RandomState will try to read data from
/dev/urandom (or the Windows analogue) if available or seed from
the clock otherwise.
kwargs : optional
extra arguments for create_random.
Returns
-------
data
A Data object with a random lightcurves.
Examples
--------
.. code-block:: pycon
>>> ds = synthetic.create_uniform(1, 2, 0, .0008, 42)
>>> ds
Data(id=None, ds_name='feets-synthetic', bands=('B', 'V'))
>>> ds.data.B.magnitude
array([ 1.37454012, 1.95071431, 1.73199394, ..., 1.94670792,
1.39748799, 1.2171404 ])
"""
random = np.random.RandomState(seed)
return create_random(
magf=random.uniform,
magf_params={"low": low, "high": high},
errf=random.normal,
errf_params={"loc": mu_err, "scale": sigma_err},
**kwargs,
)
def create_periodic(mu_err=0.0, sigma_err=1.0, seed=None, **kwargs):
"""Generate a data with magnitudes with periodic variability
distribution; the error instead are gaussian.
Parameters
----------
mu_err : float (default=0)
Mean of the gaussian distribution of magnitudes
sigma_err : float (default=1)
Standar deviation of the gaussian distribution of magnitude errorrs
seed : {None, int, array_like}, optional
Random seed used to initialize the pseudo-random number generator.
Can be any integer between 0 and 2**32 - 1 inclusive, an
array (or other sequence) of such integers, or None (the default).
If seed is None, then RandomState will try to read data from
/dev/urandom (or the Windows analogue) if available or seed from
the clock otherwise.
kwargs : optional
extra arguments for create_random.
Returns
-------
da |
# | Say hello to Django | |
string = '\xe4\x03\x01\x00'
struct.unpack("i", string)
# (66532,)
bytes = '\x01\xc2'
struct.pack("<h", struct.unpack(">h", bytes)[0])
# '\xc2\x01'
import base64
base64.b64encode('encodings are fun...')
# 'ZW5jb2RpbmdzIGFyZSBmdW4uLi4='
base64.b64decode(_)
# 'encodings are fun...'
string = "hello\x00"
binary_string = ' '.join('{:08b}'.format(ord(char)) for char in string)
" ".join(binary_string[i:i+6] for i in range(0, len(binary_string), 6))
# '011010 000110 010101 101100 011011 000110 111100 000000'
bin_string = '011010 000110 010101 101100 011011 000110 111100 000000'
[int(b, 2) for b in bin_string.split()]
# [26, 6, 21, 44, 27, 6, 60, 0]
u'◑ \u2020'.encode('utf8')
# '\xe2\x97\x91 \xe2\x80\xa0'
'\xe2\x97\x91 \xe2\x80\xa0'.decode('utf8')
# u'\u25d1 \u2020'
unicode('\xe2\x97\x91 \xe2\x80\xa0', encoding='utf8')
# u'\u25d1 \u2020'
utf8_string = 'Åêíòü'
utf8_string
# '\xc3\x85\xc3\xaa\xc3\xad\xc3\xb2\xc3\xbc'
unicode_string = utf8_string.decode('utf8')
unicode_string
# u'\xc5\xea\xed\xf2\xfc'
unicode_string.encode('mac roman')
# '\x81\x90\x92\x98\x9f'
'Åêíòü'.decode('utf8').encode('ascii')
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)
file = """潍楪慢敫椠桴慧扲敬整瑸琠慨⁴獩琠敨爠獥汵⁴景琠硥⁴敢湩敤潣敤獵湩湡甠楮瑮湥敤档
牡捡整湥潣楤杮楷桴挠浯汰瑥汥⁹湵敲慬整湯獥景整牦浯愠搠晩敦敲瑮眠楲楴杮猠獹整‧⠊慔敫
牦浯攠楷楫数楤牯⥧"""
print file.decode('utf8').encode('utf16')
# ??Mojibake is the garbled text that is the result of text being decoded using an
# unintended character encoding with completely unrelated ones, often from a
# different writing system.' (Taken from en.wikipedia.org)
import ftfy
ftfy.fix_text(u"“Mojibake“ can be fixed.")
# u'"Mojibake" can be fixed.'
bin(0b1010 & 0b1111110111)
# '0b10'
bin(0b1010 | 0b0110)
# '0b1110'
bin(0b10111 | 0b01000)
# '0b11111'
bin(0b100 ^ 0b110)
# '0b10'
bin(-0b1010 >> 0b10)
# '-0b11'
x = 0b1111
y = 0b1010
bin(int("{:b}{:b}".format(x, y), 2))
# '0b11111010'
bin(x << 4 | y | )
# '0b11111010'
################################################ | ####################
# 4. Cryptography
####################################################################
import random
import string
r = random.SystemRandom()
# Get a random integer between 0 and 20
r.randint(0, 20)
# 5
# Get a random number between 0 and 1
r.random()
# 0.8282475835972263
# Generate a random 40-bit number
r.getrandbits(40)
# 595477188771L
# Choose a random item from a string or list
chars = string.printable
r.choice(chars)
# 'e'
# Randomize the order of a sequence
seq = ['a', 'b', 'c', 'd', 'e']
r.shuffle(seq)
print seq
# ['c','d', 'a', 'e', 'b']
"ALLIGATOR".encode('rot13')
# 'NYYVTNGBE'
"NYYVTNGBE".encode('rot13')
# 'ALLIGATOR'
plaintext = "A secret-ish message!"
"".join(chr((ord(c) + 20) % 256) for c in plaintext)
# 'U4\x87yw\x86y\x88A}\x87|4\x81y\x87\x87u{y5'
ciphertext = 'U4\x87yw\x86y\x88A}\x87|4\x81y\x87\x87u{y5'
"".join(chr((ord(c) - 20) % 256) for c in ciphertext)
# 'A secret-ish message!'
plaintext = 0b110100001101001
one_time_pad = 0b110000011100001
bin(plaintext ^ one_time_pad)
# '0b100010001000'
decrypted = 0b100010001000 ^ one_time_pad
format(decrypted, 'x').decode('hex')
# 'hi'
import os
import binascii
# ASCII-encoded plaintext
plaintext = "this is a secret message"
plaintext_bits = int(binascii.hexlify(plaintext), 16)
print "plaintext (ascii):", plaintext
print "plaintext (hex):", plaintext_bits
# Generate the one-time pad
onetime_pad = int(binascii.hexlify(os.urandom(len(plaintext))), 16)
print "one-time pad: (hex):", onetime_pad
# Encrypt plaintext using XOR operation with one-time pad
ciphertext_bits = plaintext_bits ^ onetime_pad
print "encrypted text (hex):", ciphertext_bits
# Decrypt using XOR operation with one-time pad
decrypted_text = ciphertext_bits ^ onetime_pad
decrypted_text = binascii.unhexlify(hex(decrypted_text)[2:-1])
print "decrypted text (ascii):", decrypted_text
import random
import binascii
p1 = "this is the part where you run away"
p2 = "from bad cryptography practices."
# pad plaintexts with spaces to ensure equal length
p1 = p1.ljust(len(p2))
p2 = p2.ljust(len(p1))
p1 = int(binascii.hexlify(p1), 16)
p2 = int(binascii.hexlify(p2), 16)
# get random one-time pad
otp = random.SystemRandom().getrandbits(p1.bit_length())
# encrypt
c1 = p1 ^ otp
c2 = p2 ^ otp # otp reuse...not good!
print "c1 ^ c2 == p1 ^ p2 ?", c1 ^ c2 == p1 ^ p2
print "c1 ^ c2 =", hex(c1 ^ c2)
# the crib
crib = " the "
crib = int(binascii.hexlify(crib), 16)
xored = c1 ^ c2
print "crib =", hex(crib)
cbl = crib.bit_length()
xbl = xored.bit_length()
print
mask = (2**(cbl + 1) - 1)
fill = len(str(xbl / 8))
# crib dragging
for s in range(0, xbl - cbl + 8, 8):
xor = (xored ^ (crib << s)) & (mask << s)
out = binascii.unhexlify(hex(xor)[2:-1])
print "{:>{}} {}".format(s/8, fill, out)
from cryptography.fernet import Fernet
key = Fernet.generate_key()
f = Fernet(key)
ciphertext = f.encrypt("this is my plaintext")
decrypted = f.decrypt(ciphertext)
print decrypted
# this is my plaintext
import os
from cryptography.hazmat.primitives import padding
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
pt = "my plaintext"
backend = default_backend()
key = os.urandom(32)
iv = os.urandom(16)
padder = padding.PKCS7(128).padder()
pt = padder.update(pt) + padder.finalize()
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend)
encryptor = cipher.encryptor()
ct = encryptor.update(pt) + encryptor.finalize()
decryptor = cipher.decryptor()
out = decryptor.update(ct) + decryptor.finalize()
unpadder = padding.PKCS7(128).unpadder()
out = unpadder.update(out) + unpadder.finalize()
print out
import hashlib
hashlib.md5("hash me please").hexdigest()
# '760d92b6a6f974ae11904cd0a6fc2e90'
hashlib.sha1("hash me please").hexdigest()
# '1a58c9b3d138a45519518ee42e634600d1b52153'
import os
from cryptography.hazmat.primitives.kdf.scrypt import Scrypt
from cryptography.hazmat.backends import default_backend
backend = default_backend()
salt = os.urandom(16)
kdf = Scrypt(salt=salt, length=64, n=2**14, r=8, p=1, backend=backend)
key = kdf.derive("your favorite password")
key
import hmac
import hashlib
secret_key = "my secret key"
ciphertext = "my ciphertext"
# generate HMAC
h = hmac.new(key=secret_key, msg=ciphertext, digestmod=hashlib.sha256)
print h.hexdigest()
# verify HMAC
hmac.compare_digest(h.hexdigest(), h.hexdigest())
p = 9576890767
q = 1299827
n = p * q
print n
# 12448301194997309
e = 65537
phi = (p - 1) * (q - 1)
phi % e != 0
# True
import sympy
d = sympy.numbers.igcdex(e, phi)[0]
print d
# 1409376745910033
m = 12345
c = pow(m, e, n)
print c
# 3599057382134015
pow(c, d, n)
# 12345
m = 0
while pow(m, e, n) != c:
m += 1
print m
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization
private_key = rsa.generate_private_key(public_exponent=65537, key_size=2048, b
ackend=default_backend())
public_key = private_key.public_key()
private_pem = private_key.private_bytes(encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.BestAvailableEncryption('your password here'))
public_pem = public_key.public_bytes(encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo)
print public_pem
print private_pem
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
import base64
with open("path/to/public_key.pem", "rb") as key_file:
public_key = serialization.load_pem_public_key(key_file.read(),
backend=default_backend())
message = "your secret message"
ciphertext = public_key.encrypt(message,
padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()),
|
#! /usr/bin/env python3
import math, sys
import shtns
impor | t numpy as np
class shtnsfiledata:
#
# Adopted from https://bitbucket.org/nschaeff/shtns/src/master/examples/shallow_water.py
#
def __init__(
self,
rsphere = 1.0
):
self.rsphere = rsphere
def setup(self, file_info, anti_aliasing=False):
import s | htns
import numpy as np
if file_info['modes_m_max'] != file_info['modes_m_max']:
raise Exception("Only num_lon == num_lat supported")
ntrunc = file_info['modes_n_max']
self._shtns = shtns.sht(ntrunc, ntrunc, 1, shtns.sht_orthonormal+shtns.SHT_NO_CS_PHASE)
nlons = (ntrunc + 1) * 2
nlats = (ntrunc + 1)
if anti_aliasing:
if nlons & 1:
raise Exception("Only even numbers of longitudinal coordinates allowed for anti-aliasing")
if nlats & 1:
raise Exception("Only even numbers of latitudinal coordinates allowed for anti-aliasing")
print("Anti-aliasing:")
print(" + old lon/lat: ", nlons, nlats)
nlons += nlons//2
nlats += nlats//2
print(" + new lon/lat: ", nlons, nlats)
if file_info['grid_type'] == 'GAUSSIAN':
#self._shtns.set_grid(nlats,nlons,shtns.sht_gauss_fly|shtns.SHT_PHI_CONTIGUOUS, 1.e-10)
self._shtns.set_grid(nlats, nlons, shtns.sht_quick_init|shtns.SHT_PHI_CONTIGUOUS, 0)
elif file_info['grid_type'] == 'REGULAR':
#self._shtns.set_grid(nlats,nlons,shtns.sht_reg_dct|shtns.SHT_PHI_CONTIGUOUS, 1.e-10)
self._shtns.set_grid(nlats, nlons, shtns.sht_reg_dct|shtns.SHT_PHI_CONTIGUOUS, 0)
else:
raise Exception("Grid type '"+file_info['grid_type']+"' not supported!")
self.lats = np.arcsin(self._shtns.cos_theta)
self.lons = (2.*np.pi/nlons)*np.arange(nlons)
self.nlons = nlons
self.nlats = nlats
self.ntrunc = ntrunc
self.nlm = self._shtns.nlm
self.degree = self._shtns.l
self.lap = -self.degree*(self.degree+1.0).astype(np.complex)
self.invlap = np.zeros(self.lap.shape, self.lap.dtype)
self.invlap[1:] = 1./self.lap[1:]
self.lap = self.lap/self.rsphere**2
self.invlap = self.invlap*self.rsphere**2
def phys2spec(self, data):
return self._shtns.analys(data)
def spec2phys(self, dataspec):
return self._shtns.synth(dataspec)
def vrtdiv2uv(self, vrtspec, divspec):
return self._shtns.synth((self.invlap/self.rsphere)*vrtspec, (self.invlap/self.rsphere)*divspec)
def uv2vrtdiv(self,u,v):
vrtspec, divspec = self._shtns.analys(u, v)
return self.lap*self.rsphere*vrtspec, self.lap*self.rsphere*divspec
def getuv(self,divspec):
vrtspec = np.zeros(divspec.shape, dtype=np.complex)
u,v = self._shtns.synth(vrtspec,divspec)
return u, v
def rotateX90(self, i_field):
return self._shtns.Xrotate90(i_field)
def rotateY90(self, i_field):
return self._shtns.Yrotate90(i_field)
def rotateZ90(self, i_field, angle):
return self._shtns.Zrotate(i_field, angle)
|
# -*- coding: utf-8 -*-
import inspect
import random
import numpy as np
from django.contrib import messages
from django.views.generic import RedirectView
from django.contrib.contenttypes.models import ContentType
from django.http import Http404
from django.contrib.auth.mixins import UserPassesTestMixin
class RunActionView(UserPassesTestMixin, RedirectView):
"""
Runs common Actions for Systems and Techniques
"""
permanent = False
#: Available Actions
ACTIONS = {
"perform_inference": {
"type": "object",
"str": "PERFORMING INFERENCE",
"method": "perform_inference",
"kwargs": {
"recalculate": True
},
},
"reset_inference": {
"type": "object",
"str": "RESETING INFERENCE",
"method": "reset_inference",
"kwargs": {},
},
"reinitialize_rng": {
"type": "general",
"str": "REINITIALIZING RNG",
"method": "action_reinitialize_rng",
"kwargs": {},
}
}
def test_func(self):
return(self.request.user.is_superuser)
def action_reinitialize_rng(self):
"""
Reinitialize both generators
"""
random.seed()
np.random.seed()
def get_ct_object(self, content_type, object_id):
ct = ContentType.objects.get(model=content_type)
return(ct.model_class().objects.get(id=object_id))
def run_action(self, action, action_object=None):
try:
if action_object:
action_method = getattr(action_object, action['method'])
else:
action_method = getattr(self, action['method'])
action_method(**action['kwargs'])
messages.success(self.request,
"SUCCESS AT {}".format(action['str']))
except Exception as e:
msg = e.args[0]
frm = inspect.trace()[- | 1]
mod = inspect.getmodule(frm[0])
modname = mod.__name__ if mod else frm[1]
messages.error(self.request,
"ERROR WHILE {}: [{}] {}".format(
| action['str'], modname, str(msg)))
def get_redirect_url(self, *args, **kwargs):
if kwargs['action'] not in self.ACTIONS:
raise Http404("Action not Found")
if self.ACTIONS[kwargs['action']]["type"] == 'object':
action_object = self.get_ct_object(kwargs['content_type'],
kwargs['object_id'])
else:
action_object = None
self.run_action(self.ACTIONS[kwargs['action']], action_object)
return(self.request.META.get('HTTP_REFERER', '/'))
|
# This file is part of cclib (http://cclib.github.io), a library for parsing
# and interpreting the results of computational chemistry packages.
#
# Copyright (C) 2014, the cclib development team
#
# The library is free software, distributed under the terms of
# the GNU Lesser General Public version 2.1 or later. You should have
# received a copy of the license along with cclib. You can also access
# the full license online at http://www.gnu.org/copyleft/lgpl.html.
"""A writer for chemical JSON (CJSON) files."""
try:
import openbabel as ob
has_openbabel = True
except ImportError:
has_openbabel = False
import os.path
import json
from . import filewriter
class CJSON(filewriter.Writer):
"""A writer for chemical JSON (CJSON) files."""
def __init__(self, ccdata, *args, **kwargs):
"""Initialize the chemical JSON writer object.
Inputs:
ccdata - An instance of ccData, parsed from a logfile.
"""
# Call the __init__ method of the superclass
super(CJSON, self).__init__(ccdata, *args, **kwargs)
self.generate_repr()
def generate_repr(self):
"""Generate the CJSON representation of the logfile data."""
cjson_dict = dict()
cjson_dict['chemical json'] = 0
if self.jobfilename is not None:
cjson_dict['name'] = os.path.splitext(self.jobfilename)[0]
# These are properties that can be collected using Open Babel.
if has_openbabel:
cjson_dict['smiles'] = self.pbmol.write('smiles')
cjson_dict['inchi'] = self.pbmol.write('inchi')
cjson_dict['inchikey'] = self.pbmol.write('inchikey')
cjson_dict['formula'] = self.pbmol.formula
cjson_dict['atoms'] = dict()
cjson_dict['atoms']['elements'] = dict()
cjson_dict['atoms']['elements']['number'] = self.ccdata.atomnos.tolist()
cjson_dict['atoms']['coords'] = d | ict()
cjson_dict['atoms']['coords']['3d'] = self.ccdata.atomcoords[-1].flatten().tolist()
| cjson_dict['bonds'] = dict()
cjson_dict['bonds']['connections'] = dict()
cjson_dict['bonds']['connections']['index'] = []
if has_openbabel:
for bond in self.bond_connectivities:
cjson_dict['bonds']['connections']['index'].append(bond[0] + 1)
cjson_dict['bonds']['connections']['index'].append(bond[1] + 1)
cjson_dict['bonds']['order'] = [bond[2] for bond in self.bond_connectivities]
cjson_dict['properties'] = dict()
if has_openbabel:
cjson_dict['properties']['molecular mass'] = self.pbmol.molwt
cjson_dict['atomCount'] = len(self.ccdata.atomnos)
cjson_dict['heavyAtomCount'] = len([x for x in self.ccdata.atomnos if x > 1])
if has_openbabel:
cjson_dict['diagram'] = self.pbmol.write(format='svg')
# These are properties that can be collected using cclib.
# Do there need to be any unit conversions here?
homo_idx_alpha = int(self.ccdata.homos[0])
homo_idx_beta = int(self.ccdata.homos[-1])
energy_alpha_homo = self.ccdata.moenergies[0][homo_idx_alpha]
energy_alpha_lumo = self.ccdata.moenergies[0][homo_idx_alpha + 1]
energy_alpha_gap = energy_alpha_lumo - energy_alpha_homo
energy_beta_homo = self.ccdata.moenergies[-1][homo_idx_beta]
energy_beta_lumo = self.ccdata.moenergies[-1][homo_idx_beta + 1]
energy_beta_gap = energy_beta_lumo - energy_beta_homo
cjson_dict['energy'] = dict()
cjson_dict['energy']['total'] = self.ccdata.scfenergies[-1]
cjson_dict['energy']['alpha'] = dict()
cjson_dict['energy']['alpha']['homo'] = energy_alpha_homo
cjson_dict['energy']['alpha']['lumo'] = energy_alpha_lumo
cjson_dict['energy']['alpha']['gap'] = energy_alpha_gap
cjson_dict['energy']['beta'] = dict()
cjson_dict['energy']['beta']['homo'] = energy_beta_homo
cjson_dict['energy']['beta']['lumo'] = energy_beta_lumo
cjson_dict['energy']['beta']['gap'] = energy_beta_gap
cjson_dict['totalDipoleMoment'] = self._calculate_total_dipole_moment()
# Can/should we append the entire original log file?
# cjson_dict['files'] = dict()
# cjson_dict['files']['log'] = []
# cjson_dict['files']['log'].append()
return json.dumps(cjson_dict)
if __name__ == "__main__":
pass
|
steriors.sum(axis=1), np.ones(nobs))
viterbi_ll, stateseq = h.decode(obs)
assert_array_equal(stateseq, gaussidx)
def test_sample(self, n=1000):
h = hmm.GaussianHMM(self.n_components, self.covariance_type)
# Make sure the means are far apart so posteriors.argmax()
# picks the actual component used to generate the observations.
h.means_ = 20 * self.means
h.covars_ = np.maximum(self.covars[self.covariance_type], 0.1)
h.startprob_ = self.startprob
samples = h.sample(n)[0]
self.assertEquals(samples.shape, (n, self.n_features))
def test_fit(self, params='stmc', n_iter=5, verbose=False, **kwargs):
h = hmm.GaussianHMM(self.n_components, self.covariance_type)
h.startprob_ = self.startprob
h.transmat_ = hmm.normalize(self.transmat
+ np.diag(self.prng.rand(self.n_components)), 1)
h.means_ = 20 * self.means
h.covars_ = self.covars[self.covariance_type]
# Create training data by sampling from the HMM.
train_obs = [h.sample(n=10)[0] for x in xrange(10)]
# Mess up the parameters and see if we can re-learn them.
h.n_iter = 0
h.fit(train_obs)
trainll = train_hmm_and_keep_track_of_log_likelihood(
h, train_obs, n_iter=n_iter, params=params, **kwargs)[1:]
# Check that the loglik is always increasing during training
if not np.all(np.diff(trainll) > 0) and verbose:
print
print ('Test train: %s (%s)\n %s\n %s'
% (self.covariance_type, params, trainll, np.diff(trainll)))
delta_min = np.diff(trainll).min()
self.assertTrue(
delta_min > -0.8,
"The min nll increase is %f which is lower than the admissible"
" threshold of %f, for model %s. The likelihoods are %s."
% (delta_min, -0.8, self.covariance_type, trainll))
def test_fit_works_on_sequences_of_different_length(self):
obs = [self.prng.rand(3, self.n_features),
self.prng.rand(4, self.n_features),
self.prng.rand(5, self.n_features)]
h = hmm.GaussianHMM(self.n_components, self.covariance_type)
# This shouldn't raise
# ValueError: setting an array element with a sequence.
h.fit(obs)
def test_fit_with_priors(self, params='stmc', n_iter=5, verbose=False):
startprob_prior = 10 * self.startprob + 2.0
transmat_prior = 10 * self.transmat + 2.0
means_prior = self.means
means_weight = 2.0
covars_weight = 2.0
if self.covariance_type in ('full', 'tied'):
covars_weight += self.n_features
covars_prior = self.covars[self.covariance_type]
h = hmm.GaussianHMM(self.n_components, self.covariance_type)
h.startprob_ = self.startprob
h.startprob_prior = startprob_prior
h.transmat_ = hmm.normalize(self.transmat
+ np.diag(self.prng.rand(self.n_components)), 1)
h.transmat_prior = transmat_prior
h.means_ = 20 * self.means
h.means_prior = means_prior
h.means_weight = means_weight
h.covars_ = self.covars[self.covariance_type]
h.covars_prior = covars_prior
h.covars_weight = covars_weight
# Create training data by sampling from the HMM.
train_obs = [h.sample(n=10)[0] for x in xrange(10)]
# Mess up the parameters and see if we can re-learn them.
h.n_iter = 0
h.fit(train_obs[:1])
trainll = train_hmm_and_keep_track_of_log_likelihood(
h, train_obs, n_iter=n_iter, params=params)[1:]
# Check that the loglik is always increasing during training
if not np.all(np.diff(trainll) > 0) and verbose:
print
print ('Test MAP train: %s (%s)\n %s\n %s'
% (self.covariance_type, params, trainll, np.diff(trainll)))
# XXX: Why such a large tolerance?
self.assertTrue(np.all(np.diff(trainll) > -0.5))
class TestGaussianHMMWithSphericalCovars(GaussianHMMBaseTester, TestCase):
covariance_type = 'spherical'
def test_fit_startprob_and_transmat(self):
self.test_fit('st')
class TestGaussianHMMWithDiagonalCovars(GaussianHMMBaseTester, TestCase):
covariance_type = 'diag'
class TestGaussianHMMWithTiedCovars(GaussianHMMBaseTester, TestCase):
covariance_type = 'tied'
class TestGaussianHMMWithFullCovars(GaussianHMMBaseTester, TestCase):
covariance_type = 'full'
class MultinomialHMMTestCase(TestCase):
"""Using examples from Wikipedia
- http://en.wikipedia.or | g/wiki/Hidden_Markov_model
- http://en.wikipedia.org/wiki/Viterbi_algorithm
"""
def setUp(self):
self.prng = np.random.RandomState(9)
self.n_components = 2 # ('Rainy', 'Sunny')
self.n_symbols = 3 # ('walk', 'shop', 'clean')
self.emissionprob = [[0.1, 0.4, 0.5], [0.6, 0.3, 0.1]]
self.startprob = [0.6, 0.4]
self.transmat = [[0.7, 0.3], [0.4, 0.6]]
self.h = hmm.MultinomialHMM(self.n_components,
| startprob=self.startprob,
transmat=self.transmat)
self.h.emissionprob_ = self.emissionprob
def test_wikipedia_viterbi_example(self):
# From http://en.wikipedia.org/wiki/Viterbi_algorithm:
# "This reveals that the observations ['walk', 'shop', 'clean']
# were most likely generated by states ['Sunny', 'Rainy',
# 'Rainy'], with probability 0.01344."
observations = [0, 1, 2]
logprob, state_sequence = self.h.decode(observations)
self.assertAlmostEqual(np.exp(logprob), 0.01344)
assert_array_equal(state_sequence, [1, 0, 0])
def test_attributes(self):
h = hmm.MultinomialHMM(self.n_components)
self.assertEquals(h.n_components, self.n_components)
h.startprob_ = self.startprob
assert_array_almost_equal(h.startprob_, self.startprob)
self.assertRaises(ValueError, h.__setattr__, 'startprob_',
2 * self.startprob)
self.assertRaises(ValueError, h.__setattr__, 'startprob_', [])
self.assertRaises(ValueError, h.__setattr__, 'startprob_',
np.zeros((self.n_components - 2, self.n_symbols)))
h.transmat_ = self.transmat
assert_array_almost_equal(h.transmat_, self.transmat)
self.assertRaises(ValueError, h.__setattr__, 'transmat_',
2 * self.transmat)
self.assertRaises(ValueError, h.__setattr__, 'transmat_', [])
self.assertRaises(ValueError, h.__setattr__, 'transmat_',
np.zeros((self.n_components - 2, self.n_components)))
h.emissionprob_ = self.emissionprob
assert_array_almost_equal(h.emissionprob_, self.emissionprob)
self.assertRaises(ValueError, h.__setattr__, 'emissionprob_', [])
self.assertRaises(ValueError, h.__setattr__, 'emissionprob_',
np.zeros((self.n_components - 2, self.n_symbols)))
self.assertEquals(h.n_symbols, self.n_symbols)
def test_eval(self):
idx = np.repeat(range(self.n_components), 10)
nobs = len(idx)
obs = [int(x) for x in np.floor(self.prng.rand(nobs) * self.n_symbols)]
ll, posteriors = self.h.eval(obs)
self.assertEqual(posteriors.shape, (nobs, self.n_components))
assert_array_almost_equal(posteriors.sum(axis=1), np.ones(nobs))
def test_sample(self, n=1000):
samples = self.h.sample(n)[0]
self.assertEquals(len(samples), n)
self.assertEquals(len(np.unique(samples)), self.n_symbols)
def test_fit(self, params='ste', n_iter=5, verbose=False, **kwargs):
h = self.h
# Create training data by sampling from the HMM.
train_obs = [h.sample(n=10)[0] for x in xrange(10)]
# Mess up the parameters and see if we can re-learn them.
h.startprob_ = hmm.normalize(self.prng.rand(self.n_components))
h.transmat_ = hmm.normalize(self.prng.rand(self.n_components,
|
f copy(self, name=None):
if not name:
name = self.name + "copy"
navigate_to(self, 'Details')
tb.select('Configuration', 'Copy this Role to a new Role')
new_role = Role(name=name)
fill(self.form, {'name_txt': new_role.name},
action=form_buttons.add)
flash.assert_success_message('Role "{}" was saved'.format(new_role.name))
return new_role
@navigator.register(Role, 'All')
class RoleAll(CFMENavigateStep):
prerequisite = NavigateToAttribute('appliance.server', 'Configuration')
def step(self):
accordion.tree("Access Control", self.obj.appliance.server_region_string(), "Roles")
def resetter(self):
accordion.refresh("Access Control")
@navigator.register(Role, 'Add')
class RoleAdd(CFMENavigateStep):
prerequisite = NavigateToSibling('All')
def step(self):
tb_select("Add a new Role")
@navigator.register(Role, 'Details')
class RoleDetails(CFMENavigateStep):
prerequisite = NavigateToSibling('All')
def step(self):
accordion.tree(
"Access Control", self.obj.appliance.server_region_string(), "Roles", self.obj.name
)
def resetter(self):
accordion.refresh("Access Control")
@navigator.register(Role, 'Edit')
class RoleEdit(CFMENavigateStep):
prerequisite = NavigateToSibling('Details')
def step(self):
tb_select('Edit this Role')
class Tenant(Updateable, Pretty, Navigatable):
""" Class representing CFME tenants in the UI.
* Kudos to mfalesni *
The behaviour is shared with Project, which is the same except it cannot create more nested
tenants/projects.
Args:
name: Name of the tenant
description: Description of the tenant
parent_tenant: Parent tenant, can be None, can be passed as string or object
"""
save_changes = form_buttons.FormButton("Save changes")
# TODO:
# Temporary defining elements with "//input" as Input() is not working.Seems to be
# with html elements,looking into it.
quota_form = Form(
fields=[
('cpu_cb', CFMECheckbox('cpu_allocated')),
('cpu_txt', "//input[@id='id_cpu_allocated']"),
('memory_cb', CFMECheckbox('mem_allocated')),
('memory_txt', "//input[@id='id_mem_allocated']"),
('storage_cb', CFMECheckbox('storage_allocated')),
('storage_txt', "//input[@id='id_storage_allocated']"),
('vm_cb', CFMECheckbox('vms_allocated')),
('vm_txt', "//input[@id='id_vms_allocated']"),
('template_cb', CFMECheckbox('templates_allocated')),
('template_txt', "//input[@id='id_templates_allocated']")
])
tenant_form = Form(
fields=[
('name', Input('name')),
('description', Input('description'))
])
pretty_attrs = ["name", "description"]
@classmethod
def get_root_tenant(cls):
return cls(name="My Company", _default=True)
def __init__(self, name=None, description=None, parent_tenant=None, _default=False,
| appliance=None):
Navigatable.__init__(self, appliance=appliance)
self.name = name
self.description = description
self.parent_tenant = parent_tenant
self._default = _default
@property
def parent_tenant(self):
if self._default:
return None
if self._parent_tenant:
return self._parent_tenant
return self.get_root_tenant()
@parent_tenant.setter
def parent_tenant(self, t | enant):
if tenant is not None and isinstance(tenant, Project):
# If we try to
raise ValueError("Project cannot be a parent object.")
if isinstance(tenant, basestring):
# If parent tenant is passed as string,
# we assume that tenant name was passed instead of object
tenant = Tenant(tenant)
self._parent_tenant = tenant
def __eq__(self, other):
if not isinstance(other, type(self)):
return False
else:
return self.name == other.name
@property
def exists(self):
try:
navigate_to(self, 'Details')
return True
except CandidateNotFound:
return False
@property
def tree_path(self):
if self._default:
return [self.name]
else:
return self.parent_tenant.tree_path + [self.name]
@property
def parent_path(self):
return self.tree_path[:-1]
def create(self, cancel=False):
if self._default:
raise ValueError("Cannot create the root tenant {}".format(self.name))
navigate_to(self, 'Add')
fill(self.tenant_form, self, action=form_buttons.add)
if type(self) is Tenant:
flash.assert_success_message('Tenant "{}" was saved'.format(self.name))
elif type(self) is Project:
flash.assert_success_message('Project "{}" was saved'.format(self.name))
else:
raise TypeError(
'No Tenant or Project class passed to create method{}'.format(
type(self).__name__))
def update(self, updates):
navigate_to(self, 'Edit')
# Workaround - form is appearing after short delay
sel.wait_for_element(self.tenant_form.description)
fill(self.tenant_form, updates, action=self.save_changes)
flash.assert_success_message(
'Project "{}" was saved'.format(updates.get('name', self.name)))
def delete(self, cancel=False):
navigate_to(self, 'Details')
tb_select("Delete this item", invokes_alert=True)
sel.handle_alert(cancel=cancel)
flash.assert_success_message('Tenant "{}": Delete successful'.format(self.description))
def set_quota(self, **kwargs):
navigate_to(self, 'ManageQuotas')
# Workaround - form is appearing after short delay
sel.wait_for_element(self.quota_form.cpu_txt)
fill(self.quota_form, {'cpu_cb': kwargs.get('cpu_cb'),
'cpu_txt': kwargs.get('cpu'),
'memory_cb': kwargs.get('memory_cb'),
'memory_txt': kwargs.get('memory'),
'storage_cb': kwargs.get('storage_cb'),
'storage_txt': kwargs.get('storage'),
'vm_cb': kwargs.get('vm_cb'),
'vm_txt': kwargs.get('vm'),
'template_cb': kwargs.get('template_cb'),
'template_txt': kwargs.get('template')},
action=self.save_changes)
flash.assert_success_message('Quotas for Tenant "{}" were saved'.format(self.name))
@navigator.register(Tenant, 'All')
class TenantAll(CFMENavigateStep):
prerequisite = NavigateToAttribute('appliance.server', 'Configuration')
def step(self):
accordion.tree("Access Control", self.obj.appliance.server_region_string(), "Tenants")
def resetter(self):
accordion.refresh("Access Control")
@navigator.register(Tenant, 'Details')
class TenantDetails(CFMENavigateStep):
prerequisite = NavigateToSibling('All')
def step(self, *args, **kwargs):
accordion.tree(
"Access Control", self.obj.appliance.server_region_string(),
"Tenants", *self.obj.tree_path
)
@navigator.register(Tenant, 'Add')
class TenantAdd(CFMENavigateStep):
def prerequisite(self, *args, **kwargs):
navigate_to(self.obj.parent_tenant, 'Details')
def step(self, *args, **kwargs):
if isinstance(self.obj, Tenant):
add_selector = 'Add child Tenant to this Tenant'
elif isinstance(self.obj, Project):
add_selector = 'Add Project to this Tenant'
else:
raise OptionNotAvailable('Object type unsupported for Tenant Add: {}'
.format(type(self.obj).__name__))
tb.select('Configuration', add_selector)
@navigator.register(Tenant, 'Edit')
class TenantEdit(CFMENavigateStep): |
"""
Signals for user profiles
"""
from django.conf import settings
from django.db import transaction
from django.db.models.signals import post_delete, post_save
from django.dispatch import receiver
from discussions import tasks
from profiles.models import Profile
from roles.models import Role
from roles.roles import Permissions
@receiver(post_save, sender=Profile, dispatch_uid="sync_user_profile")
def sync_user_profile(sender, instance, created, **kwargs): # pylint: disable=unused-argument
"""
Signal handler create/update a Disc | ussionUser every time a profile is created/updated
"""
if not settings.FEATURES.get('OPEN_DISCUSSIONS_USER_SYNC', False):
return
transaction.on_commit(lambda | : tasks.sync_discussion_user.delay(instance.user_id))
@receiver(post_save, sender=Role, dispatch_uid="add_staff_as_moderator")
def add_staff_as_moderator(sender, instance, created, **kwargs): # pylint: disable=unused-argument
"""
Signal handler add user as moderator when his staff role on program is added
"""
if not settings.FEATURES.get('OPEN_DISCUSSIONS_USER_SYNC', False):
return
if instance.role not in Role.permission_to_roles[Permissions.CAN_CREATE_FORUMS]:
return
transaction.on_commit(
lambda: tasks.add_user_as_moderator_to_channel.delay(
instance.user_id,
instance.program_id,
)
)
@receiver(post_delete, sender=Role, dispatch_uid="delete_staff_as_moderator")
def delete_staff_as_moderator(sender, instance, **kwargs): # pylint: disable=unused-argument
"""
Signal handler removes user as moderator when his staff role on program is deleted
"""
if not settings.FEATURES.get('OPEN_DISCUSSIONS_USER_SYNC', False):
return
if instance.role not in Role.permission_to_roles[Permissions.CAN_CREATE_FORUMS]:
return
transaction.on_commit(
lambda: tasks.remove_user_as_moderator_from_channel.delay(
instance.user_id,
instance.program_id,
)
)
|
ever" (always
use the default regular endpoint) and "auto" (auto switch to the
default mTLS endpoint if client certificate is present, this is
the default value). However, the ``api_endpoint`` property takes
precedence if provided.
(2) If GOOGLE_API_USE_CLIENT_CERTIFICATE environment variable
is "true", then the ``client_cert_source`` property can be used
to provide client certificate for mutual TLS transport. If
not provided, the default SSL client certificate will be used if
present. If GOOGLE_API_USE_CLIENT_CERTIFICATE is "false" or not |
set, no client certificate will be used.
client_info (google.api_core.gapic_v1.client_info.ClientInfo):
The client info used to send a user-agent string along with
API requests. If ``None``, then default info will be used.
Generally, you only need to set this if you're developing
your own client library.
Raises:
google.auth.exceptions.MutualTLSChannelErr | or: If mutual TLS transport
creation failed for any reason.
"""
if isinstance(client_options, dict):
client_options = client_options_lib.from_dict(client_options)
if client_options is None:
client_options = client_options_lib.ClientOptions()
# Create SSL credentials for mutual TLS if needed.
use_client_cert = bool(
util.strtobool(
os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false")
)
)
ssl_credentials = None
is_mtls = False
if use_client_cert:
if client_options.client_cert_source:
import grpc # type: ignore
cert, key = client_options.client_cert_source()
ssl_credentials = grpc.ssl_channel_credentials(
certificate_chain=cert, private_key=key
)
is_mtls = True
else:
creds = SslCredentials()
is_mtls = creds.is_mtls
ssl_credentials = creds.ssl_credentials if is_mtls else None
# Figure out which api endpoint to use.
if client_options.api_endpoint is not None:
api_endpoint = client_options.api_endpoint
else:
use_mtls_env = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto")
if use_mtls_env == "never":
api_endpoint = self.DEFAULT_ENDPOINT
elif use_mtls_env == "always":
api_endpoint = self.DEFAULT_MTLS_ENDPOINT
elif use_mtls_env == "auto":
api_endpoint = (
self.DEFAULT_MTLS_ENDPOINT
if is_mtls
else self.DEFAULT_ENDPOINT
)
else:
raise MutualTLSChannelError(
"Unsupported GOOGLE_API_USE_MTLS_ENDPOINT value. Accepted values: never, auto, always"
)
# Save or instantiate the transport.
# Ordinarily, we provide the transport, but allowing a custom transport
# instance provides an extensibility point for unusual situations.
if isinstance(transport, AdServiceTransport):
# transport is a AdServiceTransport instance.
if credentials:
raise ValueError(
"When providing a transport instance, "
"provide its credentials directly."
)
self._transport = transport
elif isinstance(transport, str):
Transport = type(self).get_transport_class(transport)
self._transport = Transport(
credentials=credentials, host=self.DEFAULT_ENDPOINT
)
else:
self._transport = AdServiceGrpcTransport(
credentials=credentials,
host=api_endpoint,
ssl_channel_credentials=ssl_credentials,
client_info=client_info,
)
def get_ad(
self,
request: ad_service.GetAdRequest = None,
*,
resource_name: str = None,
retry: retries.Retry = gapic_v1.method.DEFAULT,
timeout: float = None,
metadata: Sequence[Tuple[str, str]] = (),
) -> ad.Ad:
r"""Returns the requested ad in full detail.
List of thrown errors: `AuthenticationError <>`__
`AuthorizationError <>`__ `HeaderError <>`__
`InternalError <>`__ `QuotaError <>`__ `RequestError <>`__
Args:
request (:class:`google.ads.googleads.v8.services.types.GetAdRequest`):
The request object. Request message for
[AdService.GetAd][google.ads.googleads.v8.services.AdService.GetAd].
resource_name (:class:`str`):
Required. The resource name of the ad
to fetch.
This corresponds to the ``resource_name`` field
on the ``request`` instance; if ``request`` is provided, this
should not be set.
retry (google.api_core.retry.Retry): Designation of what errors, if any,
should be retried.
timeout (float): The timeout for this request.
metadata (Sequence[Tuple[str, str]]): Strings which should be
sent along with the request as metadata.
Returns:
google.ads.googleads.v8.resources.types.Ad:
An ad.
"""
# Create or coerce a protobuf request object.
# Sanity check: If we got a request object, we should *not* have
# gotten any keyword arguments that map to the request.
if request is not None and any([resource_name]):
raise ValueError(
"If the `request` argument is set, then none of "
"the individual field arguments should be set."
)
# Minor optimization to avoid making a copy if the user passes
# in a ad_service.GetAdRequest.
# There's no risk of modifying the input as we've already verified
# there are no flattened fields.
if not isinstance(request, ad_service.GetAdRequest):
request = ad_service.GetAdRequest(request)
# If we have keyword arguments corresponding to fields on the
# request, apply these.
if resource_name is not None:
request.resource_name = resource_name
# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
rpc = self._transport._wrapped_methods[self._transport.get_ad]
# Certain fields should be provided within the metadata header;
# add these here.
metadata = tuple(metadata) + (
gapic_v1.routing_header.to_grpc_metadata(
(("resource_name", request.resource_name),)
),
)
# Send the request.
response = rpc(
request, retry=retry, timeout=timeout, metadata=metadata,
)
# Done; return the response.
return response
def mutate_ads(
self,
request: ad_service.MutateAdsRequest = None,
*,
customer_id: str = None,
operations: Sequence[ad_service.AdOperation] = None,
retry: retries.Retry = gapic_v1.method.DEFAULT,
timeout: float = None,
metadata: Sequence[Tuple[str, str]] = (),
) -> ad_service.MutateAdsResponse:
r"""Updates ads. Operation statuses are returned. Updating ads is
not supported for TextAd, ExpandedDynamicSearchAd, GmailAd and
ImageAd.
List of thrown errors: `AdCustomizerError <>`__ `AdError <>`__
`AdSharingError <>`__ `AdxError <>`__ `AssetError <>`__
`AssetLinkError <>`__ `AuthenticationError <>`__
`AuthorizationError <>`__ `CollectionSizeError <>`__
`DatabaseError <>`__ `DateError <>`__ `DistinctError <>`__
`FeedAttributeReferenceError <>`__ `FieldError <>`__
`FieldMa |
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from django.core.exceptions import PermissionDenied
from mock import Mock, patch
from nose.tools import eq_, raises
from oneanddone.base.tests import TestCase
from oneanddone.users.mixins import BaseUserProfileRequiredMixin, PrivacyPolicyRequiredMixin, MyStaffUserRequiredMixin
from oneanddone.users.tests import UserFactory, UserProfileFactory
class FakeMixin(object):
def dispatch(self, request, *args, **kwargs):
return 'fakemixin'
class FakeView(BaseUserProfileRequiredMixin, FakeMixin):
pass
class FakeViewNeedsPrivacyPolicy(PrivacyPolicyRequiredMixin, FakeMixin):
pass
class FakeViewNeedsStaff(MyStaffUserRequiredMixin, FakeMixin):
pass
class MyStaffUserRequiredMixinTests(TestCase):
def setUp(self):
self.view = FakeViewNeedsStaff()
def test_is_staff(self):
"""
If the user is staff, call the parent class's
dispatch method.
"""
request = Mock()
request.user = UserFactory.create(is_staff=True)
eq_(self.view.dispatch(request), 'fakemixin')
@raises(PermissionDenied)
def test_not_staff(self):
"""
If the user is not staff, raise a PermissionDenied exception.
"""
request = Mock()
request.user = UserFactory.create(is_staff=False)
self.view.dispatch(request)
class PrivacyPolicyRequiredMixinTests(TestCase):
def setUp(self):
self.view = FakeViewNeedsPrivacyPolicy()
def test_has_profile_and_accepts_privacy_policy(self):
"""
If the user has created a profile, and has accepted privacy policy
call the parent class's dispatch method.
"""
reque | st = Mock() |
request.user = UserProfileFactory.create(privacy_policy_accepted=True).user
eq_(self.view.dispatch(request), 'fakemixin')
def test_has_profile_and_not_accepted_privacy_policy(self):
"""
If the user has created a profile, and has not accepted privacy policy
redirect them to profile update view.
"""
request = Mock()
request.user = UserProfileFactory.create(privacy_policy_accepted=False).user
with patch('oneanddone.users.mixins.redirect') as redirect:
eq_(self.view.dispatch(request), redirect.return_value)
redirect.assert_called_with('users.profile.update')
def test_no_profile(self):
"""
If the user hasn't created a profile, redirect them to the
profile creation view.
"""
request = Mock()
request.user = UserFactory.create()
with patch('oneanddone.users.mixins.redirect') as redirect:
eq_(self.view.dispatch(request), redirect.return_value)
redirect.assert_called_with('users.profile.create')
class UserProfileRequiredMixinTests(TestCase):
def setUp(self):
self.view = FakeView()
def test_has_profile(self):
"""
If the user has created a profile, and has accepted privacy policy
call the parent class's dispatch method.
"""
request = Mock()
request.user = UserProfileFactory.create(privacy_policy_accepted=True).user
eq_(self.view.dispatch(request), 'fakemixin')
def test_no_profile(self):
"""
If the user hasn't created a profile, redirect them to the
profile creation view.
"""
request = Mock()
request.user = UserFactory.create()
with patch('oneanddone.users.mixins.redirect') as redirect:
eq_(self.view.dispatch(request), redirect.return_value)
redirect.assert_called_with('users.profile.create')
|
import frappe
from frappe.utils import get_fullname
def execute():
for user_id in frappe.db.sql_list("""select distinct user_id from `tabEmployee`
where ifnull(user_id, '')!=''
group by user_id having count(name) > 1"""):
fullname = get_fullname(user_id)
employee = frappe.db.get_value("Employee", {"employee_name": fu | llname, "user_id": user_id})
if employee:
frappe.db.sql("""update `tabEmployee` set user_id=null
where user_id=%s and name!=%s""", (user_id, emplo | yee)) |
from django.apps import AppConfig
class VotesConfig(AppConfig):
| name = 'meinberlin.apps.votes' |
label = 'meinberlin_votes'
|
return "test_PoissonSolver failed! calc/theory field ratio at 0: {}".format(g.electric_field[1] / field[0])
assert np.allclose(g.electric_field, field), plots()
# def test_PoissonSolver_complex(debug=DEBUG):
# L = 1
# N = 32 * 2**5
# epsilon_0 = 1
# x, dx = np.linspace(0, L, N, retstep=True, endpoint=False)
# anal_potential = lambda x: np.sin(x * 2 * np.pi) + 0.5 * \
# np.sin(x * 6 * np.pi) + 0.1 * np.sin(x * 20 * np.pi)
# anal_field = lambda x: -(2 * np.pi * np.cos(x * 2 * np.pi) + 3 * np.pi *
# np.cos(x * 6 * np.pi) + 20 * np.pi * 0.1 * np.cos(x * 20 * np.pi))
# charge_density_anal = lambda x: ((2 * np.pi)**2 * np.sin(x * 2 * np.pi) + 18 * np.pi**2 * np.sin(
# x * 6 * np.pi) + (20 * np.pi)**2 * 0.1 * np.sin(x * 20 * np.pi)) * epsilon_0
#
# NG = 32
# g = Frame(L, NG, epsilon_0)
# # indices_in_denser_grid = np.searchsorted(x, g.x)
# g.charge_density = charge_density_anal(g.x)
# energy_fourier = g.init_solver_fourier()
# energy_direct = 0.5 * (g.electric_field**2).sum() * g.dx
# print("dx", dx, "fourier", energy_fourier, "direct", energy_direct, energy_fourier / energy_direct)
#
# def plots():
# fig, xspace = plt.subplots()
# xspace.set_title(
# r"Solving the Poisson equation $\Delta \psi = \rho / \epsilon_0$ via Fourier transform")
# xspace.plot(g.x, g.charge_density, "ro--", label=r"$\rho$")
# xspace.plot(x, charge_density_anal(x), "r-", lw=6, alpha=0.5, label=r"$\rho_a$")
# xspace.plot(g.x, g.potential, "go--", label=r"$V$")
# xspace.plot(x, anal_potential(x), "g-", lw=6, alpha=0.5, label=r"$V_a$")
# xspace.plot(g.x, g.electric_field, "bo--", alpha=0.5, label=r"$E$")
# EplotAnal, = xspace.plot(x, anal_field(x), "b-", lw=6, alpha=0.5, label=r"$E_a$")
# xspace.set_xlim(0, L)
# xspace.set_xlabel("$x$")
# xspace.grid()
# xspace.legend(loc='best')
#
# fig2, fspace = plt.subplots()
# fspace.plot(g.k_plot, g.energy_per_mode, "bo--", label=r"electric energy $\rho_F V_F^\dagger$")
# fspace.set_xlabel("k")
# fspace.set_ylabel("mode energy")
# fspace.set_title("Fourier space")
# fspace.grid()
# fspace.legend(loc='best')
# plt.show()
# return "test_PoissonSolver_complex failed!"
#
# energy_correct = np. | isclose(energy_fouri | er, energy_direct)
# field_correct = np.isclose(g.electric_field, anal_field(g.x)).all()
# potential_correct = np.isclose(g.potential, anal_potential(g.x)).all()
# assert field_correct and potential_correct and energy_correct, plots()
def test_PoissonSolver_energy_sine(_NG, ):
_L = 1
resolution_increase = _NG
N = _NG * resolution_increase
epsilon_0 = 1
x, dx = np.linspace(0, _L, N, retstep=True, endpoint=False)
anal_field = np.zeros((N, 3))
anal_field[:, 0] = -(2 * np.pi * np.cos(x * 2 * np.pi / _L))
charge_density_anal = ((2 * np.pi) ** 2 * np.sin(x * 2 * np.pi))
g = PeriodicTestGrid(1, _L, _NG, epsilon_0)
indices_in_denser_grid = np.searchsorted(x, g.x)
g.charge_density[:-1] = charge_density_anal[indices_in_denser_grid] # / resolution_increase
g.init_solve()
g.save_field_values(0)
g.postprocess()
energy_fourier = g.grid_energy_history[0]
energy_direct = g.direct_energy_calculation()
print("dx", dx, "fourier", energy_fourier, "direct", energy_direct, energy_fourier / energy_direct)
def plots():
fig, xspace = plt.subplots()
xspace.set_title(
r"Solving the Poisson equation $\Delta \psi = \rho / \epsilon_0$ via Fourier transform")
xspace.plot(g.x, g.charge_density, "ro--", label=r"$\rho$")
xspace.plot(x, charge_density_anal, "r-", lw=6, alpha=0.5, label=r"$\rho_a$")
xspace.plot(g.x, g.electric_field, "bo--", alpha=0.5, label=r"$E$")
xspace.plot(x, anal_field, "b-", lw=6, alpha=0.5, label=r"$E_a$")
xspace.set_xlim(0, _L)
xspace.set_xlabel("$x$")
xspace.grid()
xspace.legend(loc='best')
fig2, fspace = plt.subplots()
fspace.plot(g.k_plot, g.energy_per_mode, "bo--", label=r"electric energy $\rho_F V_F^\dagger$")
fspace.set_xlabel("k")
fspace.set_ylabel("mode energy")
fspace.set_title("Fourier space")
fspace.grid()
fspace.legend(loc='best')
plt.show()
return "test_PoissonSolver_complex failed!"
energy_correct = np.allclose(energy_fourier, energy_direct)
assert energy_correct, plots()
field_correct = np.allclose(g.electric_field[1:-1, 0], anal_field[indices_in_denser_grid][:, 0])
assert field_correct, plots()
def test_PoissonSolver_sheets(_NG, _L, _test_charge_density=1):
epsilon_0 = 1
x, dx = np.linspace(0, _L, _NG, retstep=True, endpoint=False)
charge_density = np.zeros_like(x)
region1 = (_L * 1 / 8 < x) * (x < _L * 2 / 8)
region2 = (_L * 5 / 8 < x) * (x < _L * 6 / 8)
charge_density[region1] = _test_charge_density
charge_density[region2] = -_test_charge_density
g = PeriodicTestGrid(1, _L, _NG, epsilon_0)
g.charge_density[:-1] = charge_density
g.init_solve()
def plots():
fig, axes = plt.subplots(3)
ax0, ax1 = axes
ax0.plot(x, charge_density)
ax0.set_title("Charge density")
ax1.set_title("Field")
ax1.plot(x, g.electric_field, "r-")
for ax in axes:
ax.grid()
ax.legend()
plt.show()
return "test_PoissonSolver_sheets failed!"
polynomial_coefficients = np.polyfit(x[region1], g.electric_field[1:-1, 0][region1], 1)
first_bump_right = np.isclose(
polynomial_coefficients[0], _test_charge_density, rtol=1e-2)
assert first_bump_right, plots()
polynomial_coefficients = np.polyfit(x[region2], g.electric_field[1:-1, 0][region2], 1)
second_bump_right = np.isclose(
polynomial_coefficients[0], -_test_charge_density, rtol=1e-2)
assert second_bump_right, plots()
def test_PoissonSolver_ramp(_NG, _L):
""" For a charge density rho = Ax + B
d2phi/dx2 = -rho/epsilon_0
set epsilon_0 to 1
d2phi/dx2 = Ax
phi must be of form
phi = -Ax^3/6 + Bx^2 + Cx + D"""
a = 1
# noinspection PyArgumentEqualDefault
g = PeriodicTestGrid(1, _L, _NG, epsilon_0=1)
g.charge_density[:-1] = a * g.x
g.init_solve()
field = a * (g.x - _L / 2) ** 2 / 2
def plots():
fig, axes = plt.subplots(2)
ax0, ax1 = axes
ax0.plot(g.x, g.charge_density)
ax0.set_title("Charge density")
ax1.set_title("Field")
ax1.plot(g.x, g.electric_field, "r-")
ax1.plot(g.x, field, "g-")
for ax in axes:
ax.grid()
ax.legend()
plt.show()
return "test_PoissonSolver_ramp failed!"
polynomial_coefficients = np.polyfit(g.x, g.electric_field[1:-1, 0], 2)
assert np.isclose(polynomial_coefficients[0], a / 2, rtol=1e-2), (polynomial_coefficients[0], a / 2, plots())
def test_BunemanSolver(__t, _NG, _L, _test_charge_density):
g = NonperiodicTestGrid(__t, _L, _NG)
charge_index = _NG // 2
g.current_density_x[charge_index] = _test_charge_density
g.solve()
g.save_field_values(0)
S = Simulation(g)
pulled_field = g.electric_field[charge_index, 0]
expected_field = - g.dt / g.epsilon_0 * _test_charge_density
def plot():
fig, (ax1, ax2) = plt.subplots(2)
CurrentPlot(S, ax1, 0).update(0)
FieldPlot(S, ax2, 0).update(0)
plt.show()
assert np.isclose(pulled_field, expected_field), plot()
def test_BunemanSolver_charge(__t, _NG, _L, _test_charge_density):
g = NonperiodicTestGrid(__t, _L, _NG)
v = 0.5
g.current_density_x[1:-2] = v * _test_charge_density
g.solve()
g.save_field_values(0)
S = Simulation(g).postprocess()
def plot():
fig, (ax1, ax2) = plt.subplots(2)
CurrentPlot(S, ax1, 0).update(0)
FieldPlot(S, ax2, 0).update(0)
plt.show()
assert np.allclose(g.electric_field[1:-1,0], -v * _test_charge_d |
# -*- coding: utf-8 -*-
"""
Tests for the WMS Service Type.
"""
import unittest
from httmock import with_httmock
import mocks.warper
from aggregator.models import Service
class TestWarper(unittest.TestCase):
@with_httmock(mocks.warper.resource_get)
def test_create_wms_service(self):
# create the service
service = Service(
type='WARPER',
url='http://warper.example.com/warper/maps',
)
service.save()
# check layer number
self.assertEqual(service.layer_set.all().count(), 15)
# check layer 0 (public)
layer_0 = service.layer_set.all()[0]
self.assertEqual(layer_0.name, '29568')
self.assertEqual(layer_0.title, 'Plate 24: Map bounded by Myrtle Avenue')
self.assertTrue(layer_0.is_public)
self.assertEqual(layer_0.keywords.all().count(), 0)
self.assertEqual(layer_0.srs.all().count(), 3)
self.assertEqual(layer_0.check_set.all().count(), 1)
self.assertEqual(layer_0.layerdate_set.all()[0].date, '1855-01-01')
# a layer with no bbox must be stored with None coordinates
layer_no_bbox = service.layer_set.get(name='16239')
self.assertEqual(layer_no_bbox.bbox_x0, None)
self.assertEqual(layer_no_bbox.bbox_ | y0, None)
self.assertEqual(layer_no_bbox.bbox_x1, None)
self.assertEqual(layer_no_bbox.bbox_y1, None)
# test that if creating the service and is already exiting it is not being duplicated
# create the service
def create_duplicated_service():
duplicated_service = Ser | vice(
type='WARPER',
url='http://warper.example.com/warper/maps',
)
duplicated_service.save()
self.assertRaises(Exception, create_duplicated_service)
if __name__ == '__main__':
unittest.main()
|
class Solution(object):
def asteroidCollision(self, asteroids):
"""
:type asteroids: List[int]
:rtype: List[int]
"""
ret = []
for elem in asteroids:
if elem > 0:
| ret.append(elem)
else:
while ret:
if 0 < ret[-1] <= -elem:
temp = ret.pop()
if temp == -elem:
break
else:
if ret[-1] < 0:
ret.append(elem)
break
else:
ret.append(elem)
return ret
print(Solution().asteroidCollision([5, 10, -5]))
print(Solution().asteroidCollision([8, -8]))
print(Solution().asteroidCollision([10, 2, -5]))
print(Solution().asteroidCollision([-2, -1, 1, 2]))
| |
# Copyright 2021 The TF-Coder Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific lang | uage governing permissions and
# limitations under the License.
# Lint as: python3
"""Serialization of objects relevant to TF-Coder.
This module will be used to send information from the public Colab notebook to
Google Analytics, in string form. Using BigQuery we can extract the strings that
were sent, and then parse the strings back into the Python objects that they |
represent. The information we want to log includes:
* Input/output objects. Usually these are multidimensional lists, Tensors, or
SparseTensors, but in principle these can be anything that value search
supports (e.g., primitives, dtypes, tuples of Tensors etc.).
* Constants. Usually these are Python primitives, but again they may be
anything value search supports (e.g., a shape tuple).
* Natural language description. This should be a string and may contain tricky
characters like Unicode or quotes.
* Settings for the TF-Coder tool. These may use standard Python collections,
i.e. lists/tuples/sets/dicts. This category of information should be treated
generally to be future-proof.
* Results of the TF-Coder tool. These would include timestamps and solution
expressions.
* Other metadata, e.g., session/problem IDs, and whether the data can be
released in a dataset.
"""
import ast
from typing import Any, List, Text
import numpy as np
import tensorflow as tf
# Constant strings for dict representations of objects.
_KIND_KEY = 'kind'
_DTYPE_KIND = 'DType'
_TENSOR_KIND = 'Tensor'
_SPARSE_TENSOR_KIND = 'SparseTensor'
_DICT_KIND = 'Dict'
def _object_to_literal(to_serialize: Any, container_stack: List[Any]) -> Any:
"""Turns a supported object into a Python literal."""
if isinstance(to_serialize, (int, float, bool, str, bytes, type(None))):
return to_serialize
elif isinstance(to_serialize, tf.DType):
dtype_string = repr(to_serialize)
assert dtype_string.startswith('tf.')
dtype_string = dtype_string[len('tf.'):]
return {_KIND_KEY: _DTYPE_KIND,
'dtype': dtype_string}
elif isinstance(to_serialize, tf.Tensor):
tensor_content = to_serialize.numpy()
# Sometimes tensor_content is a numpy type, and sometimes it's a normal
# Python type.
if type(tensor_content).__module__ == np.__name__:
tensor_content = tensor_content.tolist()
return {_KIND_KEY: _TENSOR_KIND,
'content': tensor_content,
'dtype': _object_to_literal(to_serialize.dtype, container_stack)}
elif isinstance(to_serialize, tf.SparseTensor):
return {_KIND_KEY: _SPARSE_TENSOR_KIND,
'indices': _object_to_literal(to_serialize.indices,
container_stack),
'values': _object_to_literal(to_serialize.values, container_stack),
'dense_shape': _object_to_literal(to_serialize.dense_shape,
container_stack)}
elif isinstance(to_serialize, dict):
if any(to_serialize is seen for seen in container_stack):
raise ValueError('Cycle detected in object dependencies.')
container_stack.append(to_serialize)
result = {_object_to_literal(key, container_stack):
_object_to_literal(value, container_stack)
for key, value in to_serialize.items()}
container_stack.pop()
return {_KIND_KEY: _DICT_KIND,
'dict': result}
elif isinstance(to_serialize, (list, tuple, set)):
if any(to_serialize is seen for seen in container_stack):
raise ValueError('Cycle detected in object dependencies.')
container_stack.append(to_serialize)
generator = (_object_to_literal(x, container_stack) for x in to_serialize)
container_type = type(to_serialize)
result = container_type(generator)
container_stack.pop()
return result
else:
raise TypeError('Cannot convert object {} with type {} to a literal.'
.format(to_serialize, type(to_serialize)))
def _literal_to_object(literal: Any) -> Any:
"""Turns a literal created by _object_to_literal back into the object."""
if isinstance(literal, (int, float, bool, str, bytes, type(None))):
return literal
elif isinstance(literal, dict):
# If the dict was not created by _object_to_literal, we may throw KeyError.
kind = literal[_KIND_KEY]
if kind == _DTYPE_KIND:
return getattr(tf, literal['dtype'])
elif kind == _TENSOR_KIND:
return tf.constant(literal['content'],
dtype=_literal_to_object(literal['dtype']))
elif kind == _SPARSE_TENSOR_KIND:
return tf.SparseTensor(
indices=_literal_to_object(literal['indices']),
values=_literal_to_object(literal['values']),
dense_shape=_literal_to_object(literal['dense_shape']))
elif kind == _DICT_KIND:
return {_literal_to_object(key): _literal_to_object(value)
for key, value in literal['dict'].items()}
else:
raise ValueError('Unsupported kind in dict: {}'.format(kind))
elif isinstance(literal, (list, tuple, set)):
generator = (_literal_to_object(x) for x in literal)
container_type = type(literal)
return container_type(generator)
else:
raise TypeError('Cannot convert literal {} with type {} to an object.'
.format(literal, type(literal)))
def serialize(to_serialize: Any) -> Text:
"""Serializes an object into a string.
Note: This does not work in Python 2 because its ast.literal_eval does not
support sets.
Args:
to_serialize: The object to serialize. This may be a Python literal (int,
float, boolean, string, or None), Tensor, SparseTensor, or
possibly-nested lists/tuples/sets/dicts of these.
Returns:
A string representation of the object.
"""
return repr(_object_to_literal(to_serialize, container_stack=[]))
def parse(serialized: Text) -> Any:
"""Unparses a string into an object (the inverse of serialize_object)."""
literal = ast.literal_eval(serialized)
return _literal_to_object(literal)
|
ta
from django.db import models
from django.contrib.sites.models import Site
from django.core.validators import MinValueValidator
from clubdata.models import Club
def range_date_inclusive(start_date, end_date):
for n in range((end_date - start_date).days+1):
yield start_date + timedelta(n)
def num_days_in_month(d):
dmonth = d.month
if dmonth == 12:
return 31
else:
return (d.replace(month=dmonth+1, day=1) - timedelta(days=1)).day
def last_day_in_month(d):
dmonth = d.month
if dmonth == 12:
return d.replace(day=31)
else:
return d.replace(month=dmonth+1, day=1) - timedelta(days=1)
def decode_weekly_criteria(criteria):
c = criteria.split(",")
dow_possible = ('mo','tu','we','th','fr','sa','su')
dow = [False,False,False,False,False,False,False]
for x in c: dow[dow_possible.index(x)] = True
return dow
def decode_monthly_criteria(criteria):
c = criteria.split(",")
specificdays = []
daystocalculate = []
dow_possible = ('mo','tu','we','th','fr','sa','su')
for x in c:
if x.isdigit():
# Specific numbered day (same every month)
specificdays.append(int(x))
else:
# A code to represent a day. We'll convert from strings to integers for later.
if x == 'last':
# Last day of the month (must be calculated later)
daystocalculate.append( (99, -1) )
else:
y,z = x.split("-")
if y == 'last':
# Last DOW of the month (must be calculated later)
daystocalculate.append( (99, dow_possible.index(z)) )
else:
# Specified DOW of the month (must be calculated later)
daystocalculate.append( (int(y), dow_possible.index(z)) )
return specificdays,daystocalculate
class RecurringEvent(models.Model):
DAILY = 100
WEEKLY = 200
MONTHLY = 300
rule_type_choices = (
(DAILY, 'Daily'),
(WEEKLY, 'Weekly'),
(MONTHLY, 'Monthly'),
)
id = models.AutoField(
primary_key=True)
# Range
starts_on = models.DateField('Starts on')
ends_on = models.DateField('Ends on')
# Rule
rule_type = models.IntegerField('Recurring rule',
choices=rule_type_choices,
default=WEEKLY)
repeat_each = models.IntegerField('Repeat each',
default=1,
validators=[MinValueValidator(1)])
criteria = models.CharField('Criteria',
max_length=200,
null=True, # Blank is stored as Null
blank=True) # Field is optional
class Meta:
verbose_name = 'Recurring Event'
verbose_name_plural = 'Recurring Events'
def __unicode__(self): #Python 3.3 is __str__
rt = self.rule_type
for t in self.rule_type_choices:
if t[0] == rt:
rt = t[1]
break
return "%s Event, %s to %s, Criteria=\"%s\"" % (rt, self.starts_on, self.ends_on, self.criteria)
def dates_per_rule_iter(self):
if self.rule_type == self.WEEKLY:
# criteria = Must be a comma-separated list of lowercase 2-letter abbreviations for the days
# of the week. Ex: mo,we,fr,su
# repeat_each = If this is 2, then every other week will be skipped. If it is 3,
# then two weeks will be skipped between each filled week. etc...
# Deconstruct the criteria
criteria = decode_weekly_criteria(self.criteria)
# Generate a list of dates that match
if self.repeat_each == 1:
# If repeat_each is 1, then our calculation is much simpler
for x in range_date_inclusive(self.starts_on, self.ends_on):
if criteria[x.weekday()]: yield x
else:
# Special handling because we're not doing every week
r = 2 # Set this to 2 so the first iteration will set it to 1
dow_begin = self.starts_on.weekday()
for x in range_date_inclusive(self.starts_on, self.ends_on):
wd = x.weekday()
if wd == dow_begin:
# It's the beginning of a new week (rather than assuming the user considers Monday to be
# the first day of the week, we use the DOW of the start of the range for this purpose.
if r == 1:
# Reset the counter
r = self.repeat_each
else:
# Decrease the counter
r -= 1
if r == 1:
# If counter is 1, then this week should be included
if criteria[wd]: yield x
elif self.rule_type == self.MONTHLY:
# criteria = Must be a comma-separated list of the following types of codes:
# * 1,2,3,4, etc specific days of the month
# * 1-mo, 3-fr, last-we, etc first Monday, third Friday, last Wednesday, etc.
# * last last day of the month
# repeat_each = If this is 2, then every other month will be skipped. If it is 3, then two
# months will be skipped between each filled month. etc...
# Deconstruct the criteria
specificdays,daystocalculate = decode_monthly_criteria(self.criteria)
# Generate a list of dates that match
calcdays = None
oneday = timedelta(days=1)
r = 2 # Set this to 2 so the first iteration will set it to 1
for x in range_date_inclusive(self.starts_on, self.ends_on):
xday = x.day
if (xday == 1) or (calcdays is None):
# It's the first day of the month (or first iteration of this loop)
if r == 1:
# Reset the counter
r = self.repeat_each
else:
# Decrease the counter
r -= 1
if r == 1: # Putting this within the above 'else' will malfunction if repeat_each is 1
# Since this month is included, we must turn those vague days into specific numbered days
# for this current month (each month is different, so they couldn't have been calculated earlier.
calcdays = []
for y in daystocalculate:
if y[0] == 99:
if y[1] == -1:
# Calculate the last day of the month
calcdays.append(num_days_in_month(x))
else:
# Calculate the last DOW of the month
end_date = last_day_in_month(x)
for z in range(end_date.day):
d = end_date - timedelta(z)
if d.weekday() == y[1]:
calcdays.append(d.day)
break
else:
# Calculate the specified DOW of the month
start_date = date(x.year, x.month, 1)
found_count = 0
| for z in range(num_days_in_month(start_date)):
d = start_date + timedelta(z)
if d.weekday() == y[1]:
found_count += 1
if found_count == y[0]:
| calcdays.append(z+1)
break
print(calcdays)
# Check if this month is included (not a skipped month per the repeat_each rule)
if r == 1:
if (xday in specificdays) or (xday in calcdays):
# Assuming the daystocalculate have been calculated (above), simply check if the day is
# in one of the two lists
yield x
elif self.rule_type == self.DAILY:
# criteria = Not used
# repeat_each = If this is 2, then every other day will be skipped. If it is 3, only every
# third day will be chosen. etc...
# Generate a list of dates that match
if self.repeat_each == 1:
# If repeat_each is 1, then our calculation is much simpler
for x in range_date_inclusive(self.starts_on, self.ends_on):
yield x
else:
# Use the repeat value.
r = self.repeat_each # Include the first day of the range, and then start counting from there
for x in range_date_inclusive(self.starts_on, self.ends_on):
if r == self.repeat_each:
yield x
r = 1
|
from django.core.checks.compatibility.django_1_10 import (
check_duplicate_middleware_settings,
)
from django.test import SimpleTestCase
from django.test.utils import override_settings
class CheckDuplicateMiddlwareSettingsTest(SimpleTestCase):
@override_settings(MIDDLEWARE=[], MIDDLEWARE_CLASSES=['django.middleware.common.CommonMiddleware'])
def test_duplicate_setting(self):
result = check_duplicate_middleware_settings(None)
self.assertEqual(re | sult[0].id, '1_10.W001')
@override_settings(MIDDLEWARE=None)
def test_middleware_not_defined(self):
result = check_duplicate | _middleware_settings(None)
self.assertEqual(len(result), 0)
|
from django | .core.management.base import BaseCommand
from migrate_dns.import_utils import do_import
class Command( | BaseCommand):
args = ''
def handle(self, *args, **options):
do_import()
|
# Copyright 2018 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
import os
from pants.binaries.binary_tool import BinaryToolBase
from pants.binaries.binary_util import (
BinaryToolFetcher,
Bi | naryToolUrlGenerator,
BinaryUtil,
HostPlatform,
)
from pants.option.scope import GLOBAL_SCOPE
from pants.testutil.test_base import TestBase
from pants.util.contextutil import temporary_dir
from pants.util.dirutil import safe_file_dump
class DefaultVersion(BinaryToolBase):
options_scope = "default-version-test | "
name = "default_version_test_tool"
default_version = "XXX"
class AnotherTool(BinaryToolBase):
options_scope = "another-tool"
name = "another_tool"
default_version = "0.0.1"
class ReplacingLegacyOptionsTool(BinaryToolBase):
# TODO: check scope?
options_scope = "replacing-legacy-options-tool"
name = "replacing_legacy_options_tool"
default_version = "a2f4ab23a4c"
replaces_scope = "old_tool_scope"
replaces_name = "old_tool_version"
class BinaryUtilFakeUname(BinaryUtil):
def host_platform(self):
return HostPlatform("xxx", "yyy")
class CustomUrlGenerator(BinaryToolUrlGenerator):
_DIST_URL_FMT = "https://custom-url.example.org/files/custom_urls_tool-{version}-{system_id}"
_SYSTEM_ID = {
"xxx": "zzz",
}
def generate_urls(self, version, host_platform):
base = self._DIST_URL_FMT.format(
version=version, system_id=self._SYSTEM_ID[host_platform.os_name]
)
return [
base,
f"{base}-alternate",
]
class CustomUrls(BinaryToolBase):
options_scope = "custom-urls"
name = "custom_urls_tool"
default_version = "v2.1"
def get_external_url_generator(self):
return CustomUrlGenerator()
def _select_for_version(self, version):
binary_request = self.make_binary_request(version)
return BinaryUtilFakeUname.Factory._create_for_cls(BinaryUtilFakeUname).select(
binary_request
)
# TODO: these should have integration tests which use BinaryTool subclasses overriding archive_type.
class BinaryToolBaseTest(TestBase):
def setUp(self):
super().setUp()
self._context = self.context(
for_subsystems=[DefaultVersion, AnotherTool, ReplacingLegacyOptionsTool, CustomUrls],
options={
GLOBAL_SCOPE: {
"binaries_baseurls": ["https://binaries.example.org"],
"pants_bootstrapdir": str(temporary_dir()),
},
"another-tool": {"version": "0.0.2",},
"default-version-test.another-tool": {"version": "YYY",},
"custom-urls": {"version": "v2.3",},
"old_tool_scope": {"old_tool_version": "3",},
},
)
def test_base_options(self):
# TODO: using extra_version_option_kwargs!
default_version_tool = DefaultVersion.global_instance()
self.assertEqual(default_version_tool.version(), "XXX")
another_tool = AnotherTool.global_instance()
self.assertEqual(another_tool.version(), "0.0.2")
another_default_version_tool = DefaultVersion.scoped_instance(AnotherTool)
self.assertEqual(another_default_version_tool.version(), "YYY")
def test_replacing_legacy_options(self):
replacing_legacy_options_tool = ReplacingLegacyOptionsTool.global_instance()
self.assertEqual(replacing_legacy_options_tool.version(), "a2f4ab23a4c")
self.assertEqual(replacing_legacy_options_tool.version(self._context), "3")
def test_urls(self):
default_version_tool = DefaultVersion.global_instance()
self.assertIsNone(default_version_tool.get_external_url_generator())
with self.assertRaises(BinaryUtil.BinaryResolutionError) as cm:
default_version_tool.select()
err_msg = str(cm.exception)
self.assertIn(BinaryToolFetcher.BinaryNotFound.__name__, err_msg)
self.assertIn("Failed to fetch default_version_test_tool binary from any source:", err_msg)
self.assertIn(
"Failed to fetch binary from https://binaries.example.org/bin/default_version_test_tool/XXX/default_version_test_tool:",
err_msg,
)
custom_urls_tool = CustomUrls.global_instance()
self.assertEqual(custom_urls_tool.version(), "v2.3")
with self.assertRaises(BinaryUtil.BinaryResolutionError) as cm:
custom_urls_tool.select()
err_msg = str(cm.exception)
self.assertIn(BinaryToolFetcher.BinaryNotFound.__name__, err_msg)
self.assertIn("Failed to fetch custom_urls_tool binary from any source:", err_msg)
self.assertIn(
"Failed to fetch binary from https://custom-url.example.org/files/custom_urls_tool-v2.3-zzz:",
err_msg,
)
self.assertIn(
"Failed to fetch binary from https://custom-url.example.org/files/custom_urls_tool-v2.3-zzz-alternate:",
err_msg,
)
def test_hackily_snapshot(self):
with temporary_dir() as temp_dir:
safe_file_dump(
os.path.join(
temp_dir,
"bin",
DefaultVersion.name,
DefaultVersion.default_version,
DefaultVersion.name,
),
"content!",
)
context = self.context(
for_subsystems=[DefaultVersion],
options={GLOBAL_SCOPE: {"binaries_baseurls": [f"file:///{temp_dir}"],},},
)
self.maxDiff = None
default_version_tool = DefaultVersion.global_instance()
_, snapshot = default_version_tool.hackily_snapshot(context)
self.assertEqual(
"51a98706ab7458069aabe01856cb352ca97686e3edd3bf9ebd3205c2b38b2974",
snapshot.directory_digest.fingerprint,
)
|
#! /usr/bin/env python3
with open('x.c', | 'w') as f:
print('int main(void) { return 0; }', file=f)
with open('y', 'w'):
| pass
|
._finish_pending_requests()
# In theory, we shouldn't have to do this because curl will
# call _set_timeout whenever the timeout changes. However,
# sometimes after _handle_timeout we will need to reschedule
# immediately even though nothing has changed from curl's
# perspective. This is because when socket_action is
# called with SOCKET_TIMEOUT, libcurl decides internally which
# timeouts need to be processed by using a monotonic clock
# (where available) while tornado uses python's time.time()
# to decide when timeouts have occurred. When those clocks
# disagree on elapsed time (as they will whenever there is an
# NTP adjustment), tornado might call _handle_timeout before
# libcurl is ready. After each timeout, resync the scheduled
# timeout with libcurl's current state.
new_timeout = self._multi.timeout()
if new_timeout != -1:
self._set_timeout(new_timeout)
def _handle_force_timeout(self):
"""Called by IOLoop periodically to ask libcurl to process any
events it may have forgotten about.
"""
with stack_context.NullContext():
while True:
try:
ret, num_handles = self._multi.socket_all()
except pycurl.error as e:
ret = e.args[0]
if ret != pycurl.E_CALL_MULTI_PERFORM:
break
self._finish_pending_requests()
def _finish_pending_requests(self):
"""Process any requests that were completed by the last
call to multi.socket_action.
"""
while True:
num_q, ok_list, err_list = self._multi.info_read()
for curl in ok_list:
self._finish(curl)
for curl, errnum, errmsg in err_list:
self._finish(curl, errnum, errmsg)
if num_q == 0:
break
self._process_queue()
def _process_queue(self):
with stack_context.NullContext():
while True:
started = 0
while self._free_list and self._requests:
started += 1
curl = self._free_list.pop()
(request, callback) = self._requests.popleft()
curl.info = {
"headers": httputil.HTTPHeaders(),
"buffer": io.StringIO(),
"request": request,
"callback": callback,
"curl_start_time": time.time(),
}
# Disable IPv6 to mitigate the effects of this bug
# on curl versions <= 7.21.0
# http://sourceforge.net/tracker/?func=detail&aid=3017819&group_id=976&atid=100976
if pycurl.version_info()[2] <= 0x71500: # 7.21.0
curl.setopt(pycurl.IPRESOLVE, pycurl.IPRESOLVE_V4)
_curl_setup_request(curl, request, curl.info["buffer"],
curl.info["headers"])
self._multi.add_handle(curl)
if not started:
break
def _finish(self, curl, curl_error=None, curl_message=None):
info = curl.info
curl.info = None
self._multi.remove_handle(curl)
self._free_list.append(curl)
buffer = info["buffer"]
if curl_error:
error = CurlError(curl_error, curl_message)
code = error.code
effective_url = None
buffer.close()
buffer = None
else:
error = None
code = curl.getinfo(pycurl.HTTP_CODE)
effective_url = curl.getinfo(pycurl.EFFECTIVE_URL)
buffer.seek(0)
# the various curl timings are documented at
# http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html
time_info = dict(
queue=info["curl_start_time"] - info["request"].start_time,
namelookup=curl.getinfo(pycurl.NAMELOOKUP_TIME),
connect=curl.getinfo(pycurl.CONNECT_TIME),
pretransfer=curl.getinfo(pycurl.PRETRANSFER_TIME),
starttransfer=curl.getinfo(pycurl.STARTTRANSFER_TIME),
total=curl.getinfo(pycurl.TOTAL_TIME),
redirect=curl.getinfo(pycurl.REDIRECT_TIME),
)
try:
info["callback"](HTTPResponse(
request=info["request"], code=code, headers=info["headers"],
buffer=buffer, effective_url=effective_url, error=error,
request_time=time.time() - info["curl_start_time"],
time_info=time_info))
except Exception:
self.handle_callback_exception(info["callback"])
def handle_callback_exception(self, callback):
self.io_loop.handle_callback_exception(callback)
class CurlError(HTTPError):
def __init__(self, errno, message):
HTTPError.__init__(self, 599, message)
self.errno = errno
def _curl_create(max_simultaneous_connections=None):
curl = pycurl.Curl()
if logging.getLogger().isEnabledFor(logging.DEBUG):
curl.setopt(pycurl.VERBOSE, 1)
curl.setopt(pycurl.DEBUGFUNCTION, _curl_debug)
curl.setopt(pycurl.MAXCONNECTS, max_simultaneous_connections or 5)
return curl
def _curl_setup_request(curl, request, buffer, headers):
curl.setopt(pycurl.URL, utf8(request.url))
# libcurl's magic "Expect: 100-continue" behavior causes delays
# with servers that don't support it (which include, among others,
# Google's OpenID endpoint). Additionally, this behavior has
# a bug in conjunction with the curl_multi_socket_action API
# (https://sourceforge.net/tracker/?func=detail&atid=100976&aid=3039744&group_id=976),
# which increases the delays. It's more trouble than it's worth,
# so just turn off the feature (yes, setting Expect: to an empty
# value is the official way to disable this)
if "Expect" not in request.headers:
request.headers["Expect"] = ""
# libcurl adds Pragma: no-cache by default; disable that too
if "Pragma" not in request.headers:
request.headers["Pragma"] = ""
# Request headers may be either a regular dict or HTTPHeaders object
if isinstance(request.headers, httputil.HTTPHeaders):
curl.setopt(pycurl.HTTPHEADER,
[utf8("%s: %s" % i) for i in request.headers.get_all()])
else:
curl.setopt(pycurl.HTTPHEADER,
[utf8("%s: %s" % i) for i in request.headers.items()])
if request.header_callback:
curl.setopt(pycurl.HEADERFUNCTION, request.header_callback)
else:
curl.setopt(pycurl.HEADERFUNCTION,
lamb | da line: _curl_header_callback(headers, line))
if request.streaming_callback:
curl.setopt(pycurl.WRITEFUNCTION, request.streaming_callback)
else:
curl.setopt(pycurl.WRITEFUNCTION, buffer.write)
curl.setopt(pycurl.FOLLOWLOCATION, request.follow_redirects)
curl.setopt(pycurl.MAXREDIRS, request.max_redirects)
curl.setopt(pycurl.CONNE | CTTIMEOUT_MS, int(1000 * request.connect_timeout))
curl.setopt(pycurl.TIMEOUT_MS, int(1000 * request.request_timeout))
if request.user_agent:
curl.setopt(pycurl.USERAGENT, utf8(request.user_agent))
else:
curl.setopt(pycurl.USERAGENT, "Mozilla/5.0 (compatible; pycurl)")
if request.network_interface:
curl.setopt(pycurl.INTERFACE, request.network_interface)
if request.use_gzip:
curl.setopt(pycurl.ENCODING, "gzip,deflate")
else:
curl.setopt(pycurl.ENCODING, "none")
if request.proxy_host and request.proxy_port:
curl.setopt(pycurl.PROXY, request.proxy_host)
curl.setopt(pycurl.PROXYPORT, request.proxy_port)
if request.proxy_username:
credentials = '%s:%s' % (request.proxy_username,
request.proxy_password)
curl.setopt(pycurl.PROXYUSERPWD, credentials)
else:
curl.setopt(pycurl.PROXY, '')
if request.validate |
from canvas i | mport MappingCanvas
from viewport import MappingViewport
try:
from geojson_overlay import GeoJSONOverlay
except ImportError:
# No geojson
pass
# Tile managers
from mbtile_manager import MBTileManager
from http_tile_manager import HTTPTile | Manager
|
"""Ensures that account.identifier is unique.
Revision ID: ea2739ecd874
Revises: 5bd631a1b748
Create Date: 2017-09 | -29 09:16:09.436339
"""
# revision identifiers, used by Alembic.
revision = 'ea2739ecd874'
down_revision = '5bd631a1b748'
from alembic import op
import sqla | lchemy as sa
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_unique_constraint(None, 'account', ['identifier'])
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(None, 'account', type_='unique')
# ### end Alembic commands ###
|
from model.flyweight import Flyweight
from model.static.database import database
class Service(Flyweight):
def __init__(self,service_id):
#prevents reinitializing
if "_inited" in self.__dict__:
return
self._inited = None
#prevents reinitial | izing
self.service_id = service_id
cursor = database.get_cursor(
"select * from staServices where serviceID={};".format(
self.service_id))
row = cursor.fetchone()
self.service_name = row["serviceName"]
self.description = row["description"]
| cursor.close()
|
import numpy as np
def test_prepare_abi_connectivity_maps():
from samri.fetch.local import prepare_abi_connectivity_maps
pre | pare_abi_connectivity_maps('Ventral_tegmental_area',
invert_lr_experiments=[
"127651139",
"127796728",
"127798146",
"127867804",
"156314762",
"160539283",
"160540751",
"165975096",
"166054222",
"171021829",
"175736945",
"2781783 | 82",
"292958638",
"301062306",
"304337288",
],
)
def test_prepare_feature_map():
from samri.fetch.local import prepare_feature_map
prepare_feature_map('/usr/share/ABI-connectivity-data/Ventral_tegmental_area-127651139/',
invert_lr=True,
save_as='/var/tmp/samri_testing/pytest/vta_127651139.nii.gz',
)
def test_summary_atlas():
from samri.fetch.local import summary_atlas
mapping='/usr/share/mouse-brain-templates/dsurqe_labels.csv'
atlas='/usr/share/mouse-brain-templates/dsurqec_40micron_labels.nii'
summary={
1:{
'structure':'Hippocampus',
'summarize':['CA'],
'laterality':'right',
},
2:{
'structure':'Hippocampus',
'summarize':['CA'],
'laterality':'left',
},
3:{
'structure':'Cortex',
'summarize':['cortex'],
'laterality':'right',
},
4:{
'structure':'Cortex',
'summarize':['cortex'],
'laterality':'left',
},
}
new_atlas, new_mapping = summary_atlas(atlas,mapping,
summary=summary,
)
new_atlas_data = new_atlas.get_data()
output_labels = np.unique(new_atlas_data).tolist()
target_labels = [0,]
target_labels.extend([i for i in summary.keys()])
assert output_labels == target_labels
def test_roi_from_atlaslabel():
from samri.fetch.local import roi_from_atlaslabel
mapping='/usr/share/mouse-brain-templates/dsurqe_labels.csv'
atlas='/usr/share/mouse-brain-templates/dsurqec_40micron_labels.nii'
my_roi = roi_from_atlaslabel(atlas,
mapping=mapping,
label_names=['cortex'],
)
roi_data = my_roi.get_data()
output_labels = np.unique(roi_data).tolist()
assert output_labels == [0, 1]
my_roi = roi_from_atlaslabel(atlas,
mapping=mapping,
label_names=['cortex'],
output_label=3,
)
roi_data = my_roi.get_data()
output_labels = np.unique(roi_data).tolist()
assert output_labels == [0, 3]
|
#!/usr/bin/env python
#
# Copyright (c) 2016 Intel Corporation.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of works must retain the original copyright notice, this
# list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the original copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of Intel Corporation nor the names of its contributors
# may be used to endorse or promote products derived from this work without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Authors:
# Liu, Yun <yunx.liu@intel.com>
import unittest
import os
import comm
class TestCrosswalkApptoolsFunctions(unittest.TestCase):
def test_setting_value(self):
comm.setUp()
os.chdir(comm.XwalkPath)
comm.clear("org.xwalk.test")
os.mkdir("org.xwalk.test")
os.chdir('org.xwalk.test')
cmd = comm.HOST_PREFIX + comm.PackTools + \
"crosswalk-pkg --platforms=android --android=" + comm.ANDROID_MODE + " --crosswalk=" + comm.crosswalkzip + " ../../testapp/manifest_xwalk_target_platforms/windows_platform/"
(return_code, output) = comm.getstatusoutput(cmd)
apks = os.listdir(os.getcwd())
apkLength = 0
if comm.MODE != " --android-shared":
for i in range(len(apks)):
if apks[i].endswith(".apk") and "x86" in apks[i]:
if comm.BIT == "64":
| self.assertIn("64", apks[i])
apkLength = apkLength + 1
if apks[i].endswith(".apk") and "arm" in apks[i]:
if comm.BIT == "64":
self.assertIn("64", apks[i])
| apkLength = apkLength + 1
self.assertEquals(apkLength, 2)
else:
for i in range(len(apks)):
if apks[i].endswith(".apk") and "shared" in apks[i]:
apkLength = apkLength + 1
appVersion = apks[i].split('-')[1]
self.assertEquals(apkLength, 1)
comm.run(self)
comm.clear("org.xwalk.test")
self.assertEquals(return_code, 0)
self.assertIn("Loading 'android' platform backend", output[0])
self.assertNotIn("Loading 'windows' platform backend", output[0])
def test_without_platforms(self):
comm.setUp()
os.chdir(comm.XwalkPath)
comm.clear("org.xwalk.test")
os.mkdir("org.xwalk.test")
os.chdir('org.xwalk.test')
cmd = comm.HOST_PREFIX + comm.PackTools + \
"crosswalk-pkg --android=" + comm.ANDROID_MODE + " --crosswalk=" + comm.crosswalkzip + " ../../testapp/create_package_basic/"
(return_code, output) = comm.getstatusoutput(cmd)
apks = os.listdir(os.getcwd())
apkLength = 0
if comm.MODE != " --android-shared":
for i in range(len(apks)):
if apks[i].endswith(".apk") and "x86" in apks[i]:
if comm.BIT == "64":
self.assertIn("64", apks[i])
apkLength = apkLength + 1
if apks[i].endswith(".apk") and "arm" in apks[i]:
if comm.BIT == "64":
self.assertIn("64", apks[i])
apkLength = apkLength + 1
self.assertEquals(apkLength, 2)
else:
for i in range(len(apks)):
if apks[i].endswith(".apk") and "shared" in apks[i]:
apkLength = apkLength + 1
appVersion = apks[i].split('-')[1]
self.assertEquals(apkLength, 1)
comm.run(self)
comm.clear("org.xwalk.test")
self.assertEquals(return_code, 0)
self.assertIn("Loading 'android' platform backend", output[0])
def test_with_target_platforms(self):
comm.setUp()
os.chdir(comm.XwalkPath)
comm.clear("org.xwalk.test")
os.mkdir("org.xwalk.test")
os.chdir('org.xwalk.test')
cmd = comm.HOST_PREFIX + comm.PackTools + \
"crosswalk-pkg --android=" + comm.ANDROID_MODE + " --crosswalk=" + comm.crosswalkzip + " ../../testapp/manifest_xwalk_target_platforms/android_platform/"
(return_code, output) = comm.getstatusoutput(cmd)
apks = os.listdir(os.getcwd())
apkLength = 0
if comm.MODE != " --android-shared":
for i in range(len(apks)):
if apks[i].endswith(".apk") and "x86" in apks[i]:
if comm.BIT == "64":
self.assertIn("64", apks[i])
apkLength = apkLength + 1
if apks[i].endswith(".apk") and "arm" in apks[i]:
if comm.BIT == "64":
self.assertIn("64", apks[i])
apkLength = apkLength + 1
self.assertEquals(apkLength, 2)
else:
for i in range(len(apks)):
if apks[i].endswith(".apk") and "shared" in apks[i]:
apkLength = apkLength + 1
appVersion = apks[i].split('-')[1]
self.assertEquals(apkLength, 1)
comm.run(self)
comm.clear("org.xwalk.test")
self.assertEquals(return_code, 0)
self.assertIn("Loading 'android' platform backend", output[0])
if __name__ == '__main__':
unittest.main()
|
from nodetraq.tests import *
class Tes | tPoolsController(TestController):
def test_index(self):
response = self.app.get(url(controller= | 'pools', action='index'))
# Test response...
|
# this example shows how to append new calculated results to an already
# existing cmr file, illustrated for calculation of PBE energy on LDA density
import os
import cmr
# set True in order to use cmr in parallel jobs!
cmr.set_ase_parallel(enable=True)
from ase.structure import molecule
from ase.io import read, write
from ase.parallel import barrier, rank
from gpaw import GPAW, restart
from gpaw.test import equal
# define the project in order to find it in the database!
project_id = 'modify cmr file after gpw restart'
formula = 'H2'
vacuum = 2.0
xc = 'LDA'
mode = 'lcao'
h = 0.20
cmr_params = {
'db_keywords': [project_id],
# add project_id also as a field to support search across projects
'project_id': project_id,
# user's tags: xc tag will be set later for illustration purpose!
'formula': formula,
'vacuum': vacuum,
'mode': mode,
'h': h,
}
cmrfile = formula + '.cmr'
system1 = molecule(formula)
system1.center(vacuum=vacuum)
# first calculation: LDA lcao
calc = GPAW(mode=mode, xc=xc, h=h, txt=None)
system1.set_calculator(calc)
e = system1.get_potential_energy()
calc.write(formula)
# read gpw file
system2, calc2 = restart(formula, txt=None)
# write the information 'as in' gpw file into db file
# (called *db to avoid conflict with the *cmr file below)
if 1: # not used in this example
calc2.write(formula + '.db', cmr_params=cmr_params)
# write the information 'as in' corresponding trajectory file into cmr file
write(cmrfile, system2, cmr_params=cmr_params)
# add the xc tag to the cmrfile
assert os.path.exists(cmrfile)
data = cmr.read(cmrfile)
data.set_user_variable('xc', xc)
data.write(cmrfile)
# peform PBE calculation on LDA density
ediff = calc2.get_xc_difference('PBE')
# add new results to the cmrfile
assert os.path.exists(cmrfile)
data = cmr.read(cmrfile)
data.set_user_variable('PBE', data['ase_potential_energy'] + ediff)
data.write(cmrfile)
# analyse the results with CMR
# cmr readers work only in serial!
from cmr.ui import DirectoryReader
if rank == 0:
reader = DirectoryRea | der(directory='.', ext='.cmr')
# read all compounds in the project with lcao
all = reader.find(name_value_list=[('mode', 'lcao')],
keyword_list=[project_id])
results = all.get('formula', formula)
print results['formula'], results['xc'], results['ase_potential_energy']
# column_le | ngth=0 aligns data in the table (-1 : data unaligned is default)
all.print_table(column_length=0,
columns=['formula', 'xc', 'h', 'ase_potential_energy', 'PBE'])
if rank == 0:
equal(results['PBE'], e + ediff, 1e-6)
if rank == 0:
for file in [formula + '.gpw', formula + '.db', cmrfile]:
if os.path.exists(file): os.unlink(file)
|
# -*- coding: utf-8 -*-
# Generated by Django 1.9.8 on 2016-08-22 16:03
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
def forwards_func(apps, schema_editor):
# We get the model from the versioned app registry;
# if we directly import it, it'll be the wrong version
m_model = apps.get_model("guidedmodules", "Module")
mq_model = apps.get_model("guidedmodules", "ModuleQuestion")
db_alias = schema_editor.connection.alias
for obj in mq_model.objects.all():
if obj.spec.get("module-id"):
mq_model.objects\
.filter(id=obj.id)\
.update(answer_type_module=m_model.objects.get(id=obj.spec["module-id"]))
class Migration(migrations.Migration):
dependencies = [
('guidedmodules', '0010_auto_20160809_1500'),
]
operations = [
migrations.AddField(
model_name='modulequestion',
name='answer_type_module',
field=models.ForeignKey(
blank=True,
h | elp_text=(
'For module and module-set typed questions, this is the Module that'
| ' Tasks that answer this question must be for.'
),
null=True,
on_delete=django.db.models.deletion.PROTECT,
related_name='is_type_of_answer_to',
to='guidedmodules.Module',
),
),
migrations.RunPython(forwards_func, migrations.RunPython.noop),
]
|
# coding=utf- | 8
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt | in the project root for
# license information.
#
# Code generated by Microsoft (R) AutoRest Code Generator.
# Changes may cause incorrect behavior and will be lost if the code is
# regenerated.
# --------------------------------------------------------------------------
from msrest.authentication import (
BasicAuthentication,
BasicTokenAuthentication,
OAuthTokenAuthentication)
from msrestazure.azure_active_directory import (
InteractiveCredentials,
ServicePrincipalCredentials,
UserPassCredentials)
|
sr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2011 ~ 2012 Deepin, Inc.
# 2011 ~ 2012 Hou Shaohui
#
# Author: Hou Shaohui <houshao55@gmail.com>
# Maintainer: Hou Shaohui <houshao55@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import gtk
import gobject
from dtk.ui.menu import Menu
from dtk.ui.draw import draw_pixbuf
from dtk.ui.label import Label
from dtk.ui.utils import propagate_expose
from dtk.ui.constant import BUTTON_PRESS, BUTTON_NORMAL, BUTTON_HOVER
import dtk.ui.tooltip as Tooltip
from widget.skin import app_theme
from nls import _
class ComboItem(gtk.Button):
def __init__(self, bg_image_group, icon_group, index, set_index, get_index):
gtk.Button.__init__(self)
# Init.
self.index = index
self.set_index = set_index
self.get_index = get_index
self.icon_group = icon_group
self.bg_image_group = bg_image_group
self.resize_button()
# connect
self.connect("clicked", self.update_button_index)
self.connect("expose-event", self.expose_button_cb)
def expose_button_cb(self, widget, event):
# Init.
rect = widget.allocation
bg_normal_dpixbuf, bg_hover_dpixbuf, bg_press_dpixbuf = self.bg_image_group
fg_normal_dpixbuf, fg_hover_dpixbuf, | fg_press_dpixbuf = self.icon_group
select_index = self.get_index()
bg_image = bg_normal_dpixbuf.get_pixbuf()
fg_image = fg_normal_dpixbuf.get_pixbuf()
if widget.state == gtk.STATE_NORMAL:
if select_index == self.index:
select_status = BUTTON_PRESS
else:
select_status = BUTTON_NORMAL
elif widget.state == gtk.STATE_PRELIGHT:
| if select_index == self.index:
select_status = BUTTON_PRESS
else:
select_status = BUTTON_HOVER
elif widget.state == gtk.STATE_ACTIVE:
select_status = BUTTON_PRESS
if select_status == BUTTON_NORMAL:
bg_image = bg_normal_dpixbuf.get_pixbuf()
fg_image = fg_normal_dpixbuf.get_pixbuf()
elif select_status == BUTTON_HOVER:
bg_image = bg_hover_dpixbuf.get_pixbuf()
fg_image = fg_hover_dpixbuf.get_pixbuf()
elif select_status == BUTTON_PRESS:
bg_image = bg_press_dpixbuf.get_pixbuf()
fg_image = fg_press_dpixbuf.get_pixbuf()
image_width = bg_image.get_width()
image_height = bg_image.get_height()
fg_rect_x = rect.x + (image_width - fg_image.get_width()) / 2
fg_rect_y = rect.y + (image_height - fg_image.get_height()) / 2
cr = widget.window.cairo_create()
draw_pixbuf(cr, bg_image, rect.x, rect.y)
draw_pixbuf(cr, fg_image, fg_rect_x, fg_rect_y)
propagate_expose(widget, event)
return True
def resize_button(self):
normal_dpixbuf = self.bg_image_group[0]
request_width = normal_dpixbuf.get_pixbuf().get_width()
request_height = normal_dpixbuf.get_pixbuf().get_height()
self.set_size_request(request_width, request_height)
def update_icon_group(self, new_group):
self.icon_group = new_group
def update_button_index(self, widget):
self.set_index(self.index)
class ComboButton(gtk.Button):
def __init__(self, bg_image_group, icon_group):
gtk.Button.__init__(self)
# Init.
self.icon_group = icon_group
self.bg_image_group = bg_image_group
self.resize_button()
# connect
self.connect("expose-event", self.expose_button_cb)
def expose_button_cb(self, widget, event):
# Init.
rect = widget.allocation
bg_normal_dpixbuf, bg_hover_dpixbuf, bg_press_dpixbuf = self.bg_image_group
fg_normal_dpixbuf, fg_hover_dpixbuf, fg_press_dpixbuf = self.icon_group
if widget.state == gtk.STATE_NORMAL:
bg_image = bg_normal_dpixbuf.get_pixbuf()
fg_image = fg_normal_dpixbuf.get_pixbuf()
elif widget.state == gtk.STATE_PRELIGHT:
bg_image = bg_hover_dpixbuf.get_pixbuf()
fg_image = fg_hover_dpixbuf.get_pixbuf()
elif widget.state == gtk.STATE_ACTIVE:
bg_image = bg_press_dpixbuf.get_pixbuf()
fg_image = fg_press_dpixbuf.get_pixbuf()
image_width = bg_image.get_width()
image_height = bg_image.get_height()
fg_rect_x = rect.x + (image_width - fg_image.get_width()) / 2
fg_rect_y = rect.y + (image_height - fg_image.get_height()) / 2
cr = widget.window.cairo_create()
draw_pixbuf(cr, bg_image, rect.x, rect.y)
draw_pixbuf(cr, fg_image, fg_rect_x, fg_rect_y)
propagate_expose(widget, event)
return True
def resize_button(self):
normal_dpixbuf = self.bg_image_group[0]
request_width = normal_dpixbuf.get_pixbuf().get_width()
request_height = normal_dpixbuf.get_pixbuf().get_height()
self.set_size_request(request_width, request_height)
def update_icon_group(self, new_group):
self.icon_group = new_group
class ComboMenuButton(gtk.HBox):
__gsignals__ = {
"list-actived" : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
"combo-actived" : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING,))
}
def __init__(self, init_index=0):
super(ComboMenuButton, self).__init__()
self.current_index = init_index
self.current_status = "artist"
self.set_spacing(0)
self.msg_content = _("By artist")
self.list_button = ComboItem(
(app_theme.get_pixbuf("combo/left_normal.png"),
app_theme.get_pixbuf("combo/left_hover.png"),
app_theme.get_pixbuf("combo/left_press.png")),
(app_theme.get_pixbuf("combo/list_normal.png"),
app_theme.get_pixbuf("combo/list_normal.png"),
app_theme.get_pixbuf("combo/list_press.png")
), 0, self.set_index, self.get_index)
Tooltip.text(self.list_button, _("List view"))
# draw left_button.
self.left_button = gtk.Button()
self.left_button = ComboItem(
(app_theme.get_pixbuf("combo/left_normal.png"),
app_theme.get_pixbuf("combo/left_hover.png"),
app_theme.get_pixbuf("combo/left_press.png")),
(app_theme.get_pixbuf("combo/artist_normal.png"),
app_theme.get_pixbuf("combo/artist_normal.png"),
app_theme.get_pixbuf("combo/artist_press.png")
), 1, self.set_index, self.get_index)
Tooltip.custom(self.left_button, self.get_msg_label).always_update(self.left_button, True)
# draw right_button.
self.right_button = ComboButton(
(app_theme.get_pixbuf("combo/right_normal.png"),
app_theme.get_pixbuf("combo/right_hover.png"),
app_theme.get_pixbuf("combo/right_hover.png")),
(app_theme.get_pixbuf("combo/triangle_normal.png"),
app_theme.get_pixbuf("combo/triangle_normal.png"),
app_theme.get_pixbuf("combo/triangle_press.png")
))
|
from builtins import range
import sys
import unittest
import re
import os.path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))
from Exscript import Account
from Exscript.account import AccountPool
from Exscript.util.file import get_accounts_from_file
class AccountPoolTest(unittest.TestCase):
CORRELATE = AccountPool
def setUp(self):
self.user1 = 'testuser1'
self.password1 = 'test1'
self.account1 = Account(self.user1, self.password1)
self.user2 = 'testuser2'
self.password2 = 'test2'
self.account2 = Account(self.user2, self.password2)
self.accm = AccountPool()
def testConstructor(self):
accm = AccountPool()
self.assertEqual(accm.n_accounts(), 0)
accm = AccountPool([self.account1, self.account2])
self.assertEqual(accm.n_accounts(), 2)
def testAddAccount(self):
self.assertEqual(self.accm.n_accounts(), 0)
self.accm.add_account(self.account1)
self.assertEqual(self.accm.n_accounts(), 1)
self.accm.add_account(self.account2)
self.assertEqual(self.accm.n_accounts(), 2)
def testReset(self):
self.testAddAccount()
self.accm.reset()
self.assertEqual(self.accm.n_accounts(), 0)
def testHasAccount(self):
self.assertEqual(self.accm.has_account(self.account1), False)
self.accm.add_account(self.account1)
self.assertEqual(self.accm.has_account(self.account1), True)
def testGetAccountFromHash(self):
account = Account('user', 'test')
thehash = account.__hash__()
self.accm.add_account(account)
self.assertEqual(self.accm.get_account_from_hash(thehash), account)
def testGetAccountFromName(self):
self.testAddAccount()
self.assertEqual(self.account2,
self.accm.get_account_from_name(self.user2))
def testNAccounts(self):
self.testAddAccount()
def testAcquireAccount(self):
self.testAddAccount()
self.accm.acquire_account(self.account1)
self.account1.release()
self.accm.acquire_account(self.account1)
self.account1.release()
# Add three more accounts.
filename = os.path.join(os.path.dirname(__file__), 'account_pool.cfg')
self.accm.add_account(get_accounts_from_file(filename))
self.assertEqual(self.accm.n_accounts(), 5)
for i in range(0, 2000):
# Each time an account is acquired a different one should be
# returned.
acquired = {}
for n in range(0, 5):
account = self.accm.acquire_account()
self.assertTrue(account is not None)
self.assertNotIn(account.get_name(), acquired)
acquired[account.get_name()] = account
# Release one account.
acquired['abc'].release()
# Acquire one account.
account = self.accm.acquire_account()
self.assertEqual(account.get_name(), 'abc')
# Release all accounts.
for account in list(acquired.values()):
account.release()
def testReleaseAccounts(self):
account1 = Account('foo')
account2 = Account('bar')
pool = AccountPool()
pool.add_account(account1)
pool.add_account(account2)
pool.acquire_account(account1, 'one')
pool.acquire_account(account2, 'two')
self.assertNotIn(account1, pool.unlocked_accounts)
self.assertNotIn(account2, pool.unlocked_accounts)
pool.release_accounts('one')
self.assertIn(account1, pool.unlocked_accounts)
self.assertNotIn(account2, pool.unlocked_accounts)
pool.release_accounts('one')
self.assertIn(account1, pool.unlocked_accounts)
self. | assertNotIn(account2, pool.unlocked_accounts)
pool.release_accounts('two')
self.assertIn(account1, | pool.unlocked_accounts)
self.assertIn(account2, pool.unlocked_accounts)
def suite():
return unittest.TestLoader().loadTestsFromTestCase(AccountPoolTest)
if __name__ == '__main__':
unittest.TextTestRunner(verbosity=2).run(suite())
|
# coding=utf-8
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# lice | nse information.
#
# Code generated by Microsoft (R) A | utoRest Code Generator.
# Changes may cause incorrect behavior and will be lost if the code is
# regenerated.
# --------------------------------------------------------------------------
from .linked_service import LinkedService
class SapHanaLinkedService(LinkedService):
"""SAP HANA Linked Service.
:param additional_properties: Unmatched properties from the message are
deserialized this collection
:type additional_properties: dict[str, object]
:param connect_via: The integration runtime reference.
:type connect_via:
~azure.mgmt.datafactory.models.IntegrationRuntimeReference
:param description: Linked service description.
:type description: str
:param type: Constant filled by server.
:type type: str
:param server: Host name of the SAP HANA server. Type: string (or
Expression with resultType string).
:type server: object
:param authentication_type: The authentication type to be used to connect
to the SAP HANA server. Possible values include: 'Basic', 'Windows'
:type authentication_type: str or
~azure.mgmt.datafactory.models.SapHanaAuthenticationType
:param user_name: Username to access the SAP HANA server. Type: string (or
Expression with resultType string).
:type user_name: object
:param password: Password to access the SAP HANA server.
:type password: ~azure.mgmt.datafactory.models.SecureString
:param encrypted_credential: The encrypted credential used for
authentication. Credentials are encrypted using the integration runtime
credential manager. Type: string (or Expression with resultType string).
:type encrypted_credential: object
"""
_validation = {
'type': {'required': True},
'server': {'required': True},
}
_attribute_map = {
'additional_properties': {'key': '', 'type': '{object}'},
'connect_via': {'key': 'connectVia', 'type': 'IntegrationRuntimeReference'},
'description': {'key': 'description', 'type': 'str'},
'type': {'key': 'type', 'type': 'str'},
'server': {'key': 'typeProperties.server', 'type': 'object'},
'authentication_type': {'key': 'typeProperties.authenticationType', 'type': 'str'},
'user_name': {'key': 'typeProperties.userName', 'type': 'object'},
'password': {'key': 'typeProperties.password', 'type': 'SecureString'},
'encrypted_credential': {'key': 'typeProperties.encryptedCredential', 'type': 'object'},
}
def __init__(self, server, additional_properties=None, connect_via=None, description=None, authentication_type=None, user_name=None, password=None, encrypted_credential=None):
super(SapHanaLinkedService, self).__init__(additional_properties=additional_properties, connect_via=connect_via, description=description)
self.server = server
self.authentication_type = authentication_type
self.user_name = user_name
self.password = password
self.encrypted_credential = encrypted_credential
self.type = 'SapHana'
|
j = j + 2
# reformatting "{{Discussion|...}}"
elif string[j:j+13] == '{{Discussion|':
string = string[:j] + '\\fI' + string[j+13:]
nr = 1
j = j + 2
# reformatting "{{Filename|...}}"
elif string[j:j+11] == '{{Filename|':
string = string[:j] + '\\fI' + string[j+11:]
nr = 1
j = j + 2
# reformatting "[mailto:...]"
elif string[j:j+8] == '[mailto:':
a = j + 8
while string[a] <> ' ':
a = a + 1
b = a + 1
while string[b] <> ']':
b = b + 1
string = string[:j] + string[a+1:b] + ' <' + string[j+8:a] + '>'
# reformatting "{{Box File|...|...}}"
elif string[j:j+11] == '{{Box File|':
a = j + 11
while string[a] <> '|':
a = a + 1
string = string[:j] + '\n.TP\n.B ' + string[j+11:a] + '\n.RS\n' + string[a+1:]
nr = 2
if nf == 0:
# reformatting boolean text 2
if string[j:j+3] == '<b>':
string = string[:j] + '\\fB' + string[j+3:]
j = j + 2
elif string[j:j+4] == '</b>':
string = string[:j] + '\\fR' + string[j+4:]
j = j + 2
# reformatting italic text 2
elif string[j:j+3] == '<i>':
string = string[:j] + '\\fI' + string[j+3:]
j = j + 2
elif string[j:j+4] == '</i>':
string = string[:j] + '\\fR' + string[j+4:]
j = j + 2
# format underlined text
elif string[j:j+3] == '<u>':
U = j
elif string[j:j+4] == '</u>' and U != -1:
string = string[:U] + '\\fB\\fI' + string[U+3:j] + '\\fB\\fR' + string[j+4:]
j = j + 7
U =- 1
# brake line 1
elif string[j:j+4] == '<br>':
string = string[0:j] + '\n.br\n' + string[j+4:]
j = j + 2
# brake line 2
elif string[j:j+6] == '<br />':
string = string[0:j] + '\n.PP\n' + string[j+6:]
j = j + 2
# format tables 1
elif string[j:j+6] == '<table':
tab = j
while len(string) > j and string[j] != '>':
j = j + 1
if string[j] == '>':
string = string[:tab] + string[j+1:]
j = tab - 1
tab = 1
else:
j = tab
tab = 0
elif string[j:j+3] == '<tr':
Ktab = j
while len(string) > j and string[j] != '>':
j = j + 1
if string[j] == '>':
tabacc = 0
string = string[:Ktab] + '\n.SS ' + string[j+1:]
j = Ktab + 4
else:
j = Ktab
elif string[j:j+4] == '</tr':
Ktab = j
while len(string) > j and string[j] != '>':
j = j + 1
if string[j] == '>':
tabacc =- 1
string = string[:Ktab] + string[j+1:]
j = Ktab - 1
else:
j = Ktab
elif string[j:j+3] == '<td':
Ktab = j
while len(string) > j and string[j] != '>':
j = j + 1
if string[j] == '>':
tabacc = tabacc + 1
if tabacc == 1:
string = string[:Ktab] + string[j+1:]
j = Ktab - 1
else:
string = string[:Ktab] + '\n.PP\n' + string[j+1:]
j = Ktab + 3
else:
j = Ktab
elif string[j:j+4] == '</td':
Ktab = j
while len(string) > j and string[j] != '>':
j = j + 1
if string[j] == '>':
string = string[:Ktab] + string[j+1:]
j = Ktab - 1
else:
j = Ktab
| elif string[j:j+7] == '</table':
tab = j
while len(string) > j and string[j] != '>':
j = j + 1
| if string[j] == '>':
string = string[:tab] + string[j+1:]
j = tab - 1
tab = 0
else:
j = tab
tab = 1
# format table 2 {| |- | || |}
elif string[j:j+2] == '||' and tab2 > 0 and col > 0:
string = string[:j] + '\n' + string[j+2:]
col = col + 1
# format div????
elif string[j:j+4] == '<div':
div = j
while len(string) > j and string[j] != '>':
j = j + 1
if string[j] == '>':
string = string[:div] + string[j+1:]
j = div - 1
else:
j = div
elif string[j:j+5] == '</div':
div = j
while len(string) > j and string[j] != '>':
j = j + 1
|
#!/usr/bin/env python3
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import kf | p
from kfp import dsl
def random_num_op(low, high):
"""Generate a random number between low and | high."""
return dsl.ContainerOp(
name='Generate random number',
image='python:alpine3.6',
command=['sh', '-c'],
arguments=['python -c "import random; print(random.randint($0, $1))" | tee $2', str(low), str(high), '/tmp/output'],
file_outputs={'output': '/tmp/output'}
)
def flip_coin_op():
"""Flip a coin and output heads or tails randomly."""
return dsl.ContainerOp(
name='Flip coin',
image='python:alpine3.6',
command=['sh', '-c'],
arguments=['python -c "import random; result = \'heads\' if random.randint(0,1) == 0 '
'else \'tails\'; print(result)" | tee /tmp/output'],
file_outputs={'output': '/tmp/output'}
)
def print_op(msg):
"""Print a message."""
return dsl.ContainerOp(
name='Print',
image='alpine:3.6',
command=['echo', msg],
)
@dsl.pipeline(
name='Conditional execution pipeline',
description='Shows how to use dsl.Condition().'
)
def flipcoin_pipeline():
flip = flip_coin_op()
with dsl.Condition(flip.output == 'heads'):
random_num_head = random_num_op(0, 9)
with dsl.Condition(random_num_head.output > 5):
print_op('heads and %s > 5!' % random_num_head.output)
with dsl.Condition(random_num_head.output <= 5):
print_op('heads and %s <= 5!' % random_num_head.output)
with dsl.Condition(flip.output == 'tails'):
random_num_tail = random_num_op(10, 19)
with dsl.Condition(random_num_tail.output > 15):
print_op('tails and %s > 15!' % random_num_tail.output)
with dsl.Condition(random_num_tail.output <= 15):
print_op('tails and %s <= 15!' % random_num_tail.output)
if __name__ == '__main__':
kfp.compiler.Compiler().compile(flipcoin_pipeline, __file__ + '.yaml')
|
import pandas as pd
from sklearn import model_selection
from sklearn.ensemble import RandomForestClassifier
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/pima-indians-diabet | es/pima-indians-diabetes.data"
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
df = pd.read_csv(url, names=names)
array = df.values
X = array[:,0:8]
y = array[:,8]
seed = 21
num_trees = 100
max_features = 3
kfold = model_selection.KFold(n_splits=10, random_state=seed)
model = RandomForestClassifier(n_estimators=num_trees, max_features=max_features)
results = model_selection.cross_val_score(model, X, y, cv=kfold)
print('results: ')
print(results)
print()
print('mean: ' + | str(results.mean())) |
from resource_media_types import *
from ClientFile import ClientFile
from ClientFolder import ClientFolder
from queryResource import ClientQuery
class ResourcesTypeResolverUtil(object):
classes = {}
#classes[ClientAdhocDataView.__name__] = ResourceMediaType.ADHOC_DATA_VIEW_MIME
#classes[ClientAwsDataSource.__name__] = ResourceMediaType.AWS_DATA_SOURCE_MIME
#classes[ClientBeanDataSource.__name__] = ResourceMediaType.BEAN_DATA_SOURCE_MIME
#classes[ClientCustomDataSource.__name__] = ResourceMediaType.CUSTOM_DATA_SOURCE_MIME
#classes[ClientDataType.__name__] = ResourceMediaType.DATA_TYPE_MIME
classes[ClientFile.__name__] = TYPE_FILE
classes[ClientFolder.__name__] = TYPE_FOLDER
#classes[ClientInputControl.__name__] = ResourceMediaType.INPUT_CONTROL_MIME
#classes[ClientJdbcDataSource.__name__] = ResourceMediaType.JDBC_DATA_SOURCE_MIME
#classes[ClientJndiJdbcDataSource.__name__] = ResourceMediaType.JNDI_JDBC_DATA_SOURCE_MIME
#classes[ClientListOfValues.__name__] = ResourceMediaType.LIST_OF_VALUES_MIME
#put(ClientMondrianConnection.class, Resour | ceMediaType.MONDRIAN_CONNECTION_MIME);
#put(ClientMondrianXmlaDefinition.class, ResourceMediaType.MONDRIAN_XMLA_DEFINITION_MIME);
#put(ClientOlapUnit.class, ResourceMediaType.OLAP_UNIT_MIME);
classes[ClientQuery.__name__] = TYPE_QUERY
#put(ClientReportUnit.class, ResourceMediaType.REPORT_UNIT_MIME);
#put(ClientSecureMondrianConnection.class, ResourceMediaType.SECURE_MONDRIAN_CONNECTIO | N_MIME);
#put(ClientSemanticLayerDataSource.class, ResourceMediaType.SEMANTIC_LAYER_DATA_SOURCE_MIME);
#put(ClientVirtualDataSource.class, ResourceMediaType.VIRTUAL_DATA_SOURCE_MIME);
#put(ClientXmlaConnection.class, ResourceMediaType.XMLA_CONNECTION_MIME);
#put(ClientResourceLookup.class, ResourceMediaType.RESOURCE_LOOKUP_MIME);
#put(ClientDashboard.class, ResourceMediaType.DASHBOARD_MIME);
#put(ClientDomainTopic.class, ResourceMediaType.DOMAIN_TOPIC_MIME);
@staticmethod
def getMimeType(resource):
if isinstance(resource, basestring):
#return resource as default in the case resource to be of direct type from resource_media_types
ret = ResourcesTypeResolverUtil.classes.get(resource, resource)
else:
ret = ResourcesTypeResolverUtil.classes.get(resource.__class__.__name__, None)
return ret |
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from caffe2.proto import caffe2_pb2
from caffe2.python import core, workspace
import caffe2.python.hypothesis_test_util as hu
import caffe2.python.serialized_test.serialized_test_util as serial
from hypothesis import given
import hypothesis.strategies as st
import numpy as np
import unittest
class TestONNXWhile(serial.SerializedTestCase):
@serial.given(
condition=st.booleans(),
max_trip_count=st.integers(0, 100),
save_scopes=st.booleans(),
disable_scopes=st.booleans(),
seed=st.integers(0, 65535),
**hu.gcs_cpu_only)
def test_onnx_while_fibb(
self, condition, max_trip_count, save_scopes, disable_scopes, seed, gc, dc):
np.random.seed(seed)
if disable_scopes:
save_scopes = False
# Create body net
body_net = caffe2_pb2.NetDef()
# Two loop carried dependencies: first and second
body_net.external_input.extend(['i', 'cond', 'first', 'second'])
body_net.external_output.extend(['cond_new', 'second', 'third', 'third'])
add_op = core.CreateOperator(
'Add',
['first', 'second'],
['third'],
)
print3 = core.CreateOperator(
'Print',
['third'],
[],
)
limit_const = core.CreateOperator(
'ConstantFill',
[],
['limit_const'],
shape=[1],
dtype=caffe2_pb2.TensorProto.FLOAT,
value=100.0,
)
cond = core.CreateOperator(
'LT',
['third', 'limit_const'],
['cond_new'],
)
body_net.op.extend([add_op, print3, limit_const, cond])
while_op = core.CreateOperator(
'ONNXWhile',
['max_trip_count', 'condition', 'first_init', 'second_init'],
['first_a', 'second_a', 'third_a'],
body=body_net,
has_cond=True,
has_trip_count=True,
save_scopes=save_scopes,
disable_scopes=disable_scopes,
)
condition_arr = np.array(condition).astype(np.bool)
max_trip_count_arr = np.array(max_trip_count).astype(np.int64)
first_init = np.array([1]).astype(np.float32)
second_init = np.array([1]).astype(np.float32)
def ref(max_trip_count, condition, first_init, second_init):
first = 1
sec | ond = 1
results = []
if condition:
for _ in range(max_trip_count):
third = first + second |
first = second
second = third
results.append(third)
if third > 100:
break
return (first, second, np.array(results).astype(np.float32))
self.assertReferenceChecks(
gc,
while_op,
[max_trip_count_arr, condition_arr, first_init, second_init],
ref,
)
if __name__ == "__main__":
unittest.main()
|
# -*- coding: utf-8 -*-
import sys
def get_skeleton(N, strings):
skeletons = []
for i in range(N):
skeleton = [strings[i][0]]
skeleton += [strings[i][j] for j in range(1, len(strings[i])) if strings[i][j] != strings[i][j-1]]
skeletons.append(skeleton)
for i in range(1, N):
if skeletons[i] != skeletons[i-1]:
skeletons[0] = []
break
return skeletons[0]
def solve():
N = int(input())
strings = [input() for _ in range(N)]
ans = 0
skeleton = get_skeleton(N, strings)
if len(skeleton) == 0:
return 'Fegla Won'
lengths = []
for c in skeleton:
length = dict()
for i in range(N):
for j in range(len(strings[i])):
if strings[i][j] != c:
break
length[j] = length.get(j, 0) + 1
strings[i] = strings[i][j:] if j < len(strings[i]) else ''
lengths.append(length)
for length in lengths:
ans += min(sum | (abs(k - l) * | length[l] for l in length) for k in length)
return ans
def main():
T = int(input())
for i in range(1, T + 1):
print('Case #{}: {}'.format(i, solve()))
if __name__ == '__main__':
sys.exit(main())
|
nd #12121.
defers = []
pk_val = None
if self._deferred:
from django.db.models.query_utils import deferred_class_factory
factory = deferred_class_factory
for field in self._meta.fields:
if isinstance(self.__class__.__dict__.get(field.attname),
DeferredAttribute):
defers.append(field.attname)
if pk_val is None:
# The pk_val and model values are the same for all
# DeferredAttribute classes, so we only need to do this
# once.
obj = self.__class__.__dict__[field.attname]
model = obj.model_ref()
else:
factory = simple_class_factory
return (model_unpickle, (model, defers, factory), data)
def _get_pk_val(self, meta=None):
if not meta:
meta = self._meta
return getattr(self, meta.pk.attname)
def _set_pk_val(self, value):
return setattr(self, self._meta.pk.attname, value)
pk = property(_get_pk_val, _set_pk_val)
def serializable_value(self, field_name):
"""
Returns the value of the field name for this instance. If the field is
a foreign key, returns the id value, instead of the object. If there's
no Field object with this name on the model, the model attribute's
value is returned directly.
Used to serialize a field's value (in the serializer, or form output,
for example). Normally, you would just access the attribute directly
and not use this method.
"""
try:
field = self._meta.get_field_by_name(field_name)[0]
except FieldDoesNotExist:
return getattr(self, field_name)
return getattr(self, field.attname)
def save(self, force_insert=False, force_update=False, using=None):
"""
Saves the current instance. Override this in a subclass if you want to
control the saving process.
The 'force_insert' and 'force_update' parameters can be used to insist
that the "save" must be an SQL insert or update (or equivalent for
non-SQL backends), respectively. Normally, they should not be set.
"""
if force_insert and force_update:
raise ValueError("Cannot force both insert and updating in model saving.")
self.save_base(using=using, force_insert=force_insert, force_update=force_update)
save.alters | _data = True
def save_base(self, raw=False, cls=None, origin=None, force_insert=False,
force_update=False, using=None):
"""
| Does the heavy-lifting involved in saving. Subclasses shouldn't need to
override this method. It's separate from save() in order to hide the
need for overrides of save() to pass around internal-only parameters
('raw', 'cls', and 'origin').
"""
using = using or router.db_for_write(self.__class__, instance=self)
assert not (force_insert and force_update)
if cls is None:
cls = self.__class__
meta = cls._meta
if not meta.proxy:
origin = cls
else:
meta = cls._meta
if origin and not meta.auto_created:
signals.pre_save.send(sender=origin, instance=self, raw=raw, using=using)
# If we are in a raw save, save the object exactly as presented.
# That means that we don't try to be smart about saving attributes
# that might have come from the parent class - we just save the
# attributes we have been given to the class we have been given.
# We also go through this process to defer the save of proxy objects
# to their actual underlying model.
if not raw or meta.proxy:
if meta.proxy:
org = cls
else:
org = None
for parent, field in meta.parents.items():
# At this point, parent's primary key field may be unknown
# (for example, from administration form which doesn't fill
# this field). If so, fill it.
if field and getattr(self, parent._meta.pk.attname) is None and getattr(self, field.attname) is not None:
setattr(self, parent._meta.pk.attname, getattr(self, field.attname))
self.save_base(cls=parent, origin=org, using=using)
if field:
setattr(self, field.attname, self._get_pk_val(parent._meta))
if meta.proxy:
return
if not meta.proxy:
non_pks = [f for f in meta.local_fields if not f.primary_key]
# First, try an UPDATE. If that doesn't update anything, do an INSERT.
pk_val = self._get_pk_val(meta)
pk_set = pk_val is not None
record_exists = True
manager = cls._base_manager
if pk_set:
# Determine whether a record with the primary key already exists.
if (force_update or (not force_insert and
manager.using(using).filter(pk=pk_val).exists())):
# It does already exist, so do an UPDATE.
if force_update or non_pks:
values = [(f, None, (raw and getattr(self, f.attname) or f.pre_save(self, False))) for f in non_pks]
if values:
rows = manager.using(using).filter(pk=pk_val)._update(values)
if force_update and not rows:
raise DatabaseError("Forced update did not affect any rows.")
else:
record_exists = False
if not pk_set or not record_exists:
if meta.order_with_respect_to:
# If this is a model with an order_with_respect_to
# autopopulate the _order field
field = meta.order_with_respect_to
order_value = manager.using(using).filter(**{field.name: getattr(self, field.attname)}).count()
self._order = order_value
fields = meta.local_fields
if not pk_set:
if force_update:
raise ValueError("Cannot force an update in save() with no primary key.")
fields = [f for f in fields if not isinstance(f, AutoField)]
record_exists = False
update_pk = bool(meta.has_auto_field and not pk_set)
result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
if update_pk:
setattr(self, meta.pk.attname, result)
transaction.commit_unless_managed(using=using)
# Store the database on which the object was saved
self._state.db = using
# Once saved, this is no longer a to-be-added instance.
self._state.adding = False
# Signal that the save is complete
if origin and not meta.auto_created:
signals.post_save.send(sender=origin, instance=self,
created=(not record_exists), raw=raw, using=using)
save_base.alters_data = True
def delete(self, using=None):
using = using or router.db_for_write(self.__class__, instance=self)
assert self._get_pk_val() is not None, "%s object can't be deleted because its %s attribute is set to None." % (self._meta.object_name, self._meta.pk.attname)
collector = Collector(using=using)
collector.collect([self])
collector.delete()
delete.alters_data = True
def _get_FIELD_display(self, field):
value = getattr(self, field.attname)
return force_unicode(dict(field.flatchoices).get(value, value), strings_only=True)
def _get_next_or_previous_by_FIELD(self, field, is_next, **kwargs):
if not self.pk:
raise ValueError("get_next/get_previous cannot be used on unsaved objects.")
op = is_next and 'gt' or |
tailData">')[1].split('</td>')[0]
# Pull out values from summary stuff
AppraisedSplit = lambda data: data.split('Appraised:</td><td class="ssDetailData" width="125px" align="right">')[1].split(' ')[0]
LandHSSplit = lambda data: data.split('Land HS:</td><td class="ssDetailData" width="125px" align="right">')[1].split(' ')[0]
ImprovementHSSplit = lambda data: data.split('Improvement HS:</td><td class="ssDetailData" width="125px" align="right">')[1].split(' ')[0]
HomeSteadCapSplit = lambda data: data.split('Homestead Cap:</td><td class="ssDetailData" width="125px" align="right">')[1].split(' ')[0]
AssessedSplit = lambda data: data.split('Assessed:</td><td class="ssDetailData" width="125px" align="right">')[1].split(' ')[0]
OwnerIdSplit = lambda data: data.split('Owner ID:</td><td class="ssDetailData">')[1].split('</td>')[0]
# Pull out values and areas of floors
split_str = '<td class="ssDetailPageLabel" nowrap="1">%s</td><td class="ssDetailPageData" nowrap="1">'
split_field = lambda sstr, data: data.split(split_str %sstr)[1].split('</td>')[0]
MainFlrSqSplit = lambda data: split_field("Area", data)
MainFlrVaSplit = lambda data: split_field("Value", data)
SecondFlrSqSplit = lambda data: split_field("Area", data)
SecondFlrVaSplit = lambda data: split_field("Value", data)
PorchFlrSqSplit = lambda data: split_field("Area", data)
PorchFlrVaSplit = lambda data: split_field("Value", data)
GarageFlrSqSplit = lambda data: split_field("Area", data)
GarageFlrVaSplit = lambda data: split_field("Value", data)
main_url = 'http://.org/appraisal/publicaccess/PropertyDetail.aspx?PropertyID=%s&dbKeyAuth=Appraisal&TaxYear=%s&NodeID=11&PropertyOwnerID=%s'
get_main_page = lambda propId,cyear,ownerId: urllib.urlopen(main_url%(propId, cyear,ownerId), proxies=proxies).read()
THREADS = []
main_search = "http://.org/appraisal/publicaccess/PropertySearch.aspx?PropertySearchType=1&SelectedItem=10&PropertyID=&PropertyOwnerID=&NodeID=11"
search_data = lambda prop_value,cyear: "PropertyID=%s&PropertySearchType=1&NodeID=11&dbKeyAuth=Appraisal&TaxYear=%s&SearchSubmit=Search"%(prop_value, cyear)
get_property = lambda data: urllib.urlopen("http://.org/appraisal/publicaccess/PropertySearchResults.aspx",data=data).read()
def get_propId(property,cyear):
d = search_data(property,cyear)
#print d
page = get_property(d)
#print page
print page.split("ViewPropertyOrOwners(")[1].split(")")[0].replace(",","").split()
propId = page.split("ViewPropertyOrOwners(")[1].split(")")[0].replace(",","").split()[-1].strip()
ownerId = page.split("ViewPropertyOrOwners(")[1].split(")")[0].replace(",","").split()[-2].strip()
print "Identified PropertyId: ", propId, "OwnerId: ", ownerId
return propId, ownerId
imp_url = 'http://.org/appraisal/publicaccess/PropertyImpDetail.aspx?CurrPosition=%d&LastPosition=4&PropertyID=%s&PropertyOwnerID=%s&NodeID=11'
def get_rland_sf(propId,ownerId):
url_str = 'http://.org/appraisal/publicaccess/PropertyLandDetail.aspx?CurrPosition=2&LastPosition=2&PropertyID=%s&PropertyOwnerID=%s&NodeID=11'
pdata = urllib.urlopen(url_str%(propId,ownerId)).read()
sz_str = 'Size - Square Feet</td><td class="ssDetailPageData" nowrap="1">'
value = pdata.split(sz_str)[1].split("</td>")[0]
return value
def get_land_sf(propId,ownerId):
url_str = 'http://.org/appraisal/publicaccess/PropertyLandDetail.aspx?CurrPosition=1&LastPosition=2&PropertyID=%s&PropertyOwnerID=%s&NodeID=11'
pdata = urllib.urlopen(url_str%(propId,ownerId)).read()
sz_str = 'Size - Square Feet</td><td class="ssDetailPageData" nowrap="1">'
value = pdata.split(sz_str)[1].split("</td>")[0]
return value
def get_improvements(propId, ownerId, pdata):
x = pdata.split(' class="ssPropertyLink" style="color:#194274; font-weight:bolder;">Imp')
imp = {}
value_str = '''class="ssPropertyLink" style="color:#194274; font-weight:bolder;">Imp'''
if x > 1:
value = pdata.split(value_str)[1].split("$")[1].split("</td>")[0].strip()
imp["Improvements 1"] = "$"+value
if x > 2:
# url = imp_url%(2,propId, ownerId)
# data = urllib.urlopen(url).read()
# print data.split(value_str)
# value = data.split(value_str)[1].split("</td>")[0].strip()
# value = "$"+value.replace("$","").strip()
# imp["Improvements 2"] = value
value = pdata.split(value_str)[2].split("$")[1].split("</td>")[0].strip()
imp["Improvements 2"] = "$"+value
elif x > 3:
# url = imp_url%(3,propId, ownerId)
# data = urllib.urlopen(url).read()
# value = data.split(value_str)[1].split("</td>")[0].strip()
# value = "$"+value.replace("$","").strip()
# imp["Improvements 3"] = value
value = pdata.split(value_str)[3].split("$")[1].split("</td>")[0].strip()
imp["Improvements 3"] = "$"+value
elif x > 4:
# url = imp_url%(4,propId, ownerId)
# data = urllib.urlopen(url).read()
# value = data.split(value_str)[1].split("</td>")[0].strip()
# value = "$"+value.replace("$","").strip()
# imp["Improvements 4"] = value
pass
return imp
PrimarySite = 'S1 (Primary Site)</td><td align="left" nowrap="true" style="overflow-x:hidden;" class="ssDataColumn">A1 (A1 - Residential Single Family)</td><td align="left" nowrap="true" class="ssDataColumn"></td><td align="right" nowrap="true" class="ssDataColumn">'
ResidualLand = 'S3 (Residual Land)</td><td align="left" nowrap="true" style="overflow-x:hidden;" class="ssDataColumn">A1 (A1 - Residential Single Family)</td><td | align="left" nowrap="true" class="ssDataColumn"></td><td align="right" nowrap="true" class="ssDataColumn">'
def parse_main_page(main_store, property, cyear='2010'):
propId,ownerId_sql = get_propId(property,cyear)
page_data = get_main_page(propId,cyear, ownerId_sql)
#page_data = open(" | page.txt").read()
addr = AddrSplit(page_data)
ownerName = NameSplit(page_data)
print "Owner Name:", ownerName
g = threading.Thread(target=get_pdf_listing, args=(propId,ownerId_sql,ownerName))
g.start()
THREADS.append(g)
ownerid = OwnerIdSplit(page_data)
print "Owner Id:", ownerid
#property = PropID(page_data)
#print property
ldesc = LDescSplit(page_data)
# Pull out values from summary stuff
print "Legal Desc: ",ldesc
appraised = AppraisedSplit(page_data).strip()
landHS = LandHSSplit(page_data).strip()
improveHS = ImprovementHSSplit(page_data).strip()
hsCap = HomeSteadCapSplit(page_data).strip()
assessed = AssessedSplit(page_data).strip()
exemptions = Exemptions(page_data)
residual = '$'+page_data.split(ResidualLand)[1].split("</td></tr>")[0].split("$")[1].strip()
primary = '$'+page_data.split(PrimarySite)[1].split("</td></tr>")[0].split("$")[1].strip()
primarySiteSq = get_land_sf(propId, ownerId_sql)
residualSq = get_rland_sf(propId, ownerId_sql)
if property not in main_store:
main_store[property] = {}
if not cyear in main_store[property]:
main_store[property][cyear] = {}
# get the properties history of values
previous_years = process_history(propId, ownerId_sql, int(cyear),2006)
for year in previous_years:
main_store[property][year] = previous_years[year]
# add in the prop values for this year
main_store[property][cyear]['AppraisedValue'] = appraised
main_store[property][cyear]['Land HS'] = landHS
main_store[property][cyear]['Improvement HS'] = improveHS
main_store[property][cyear]['HSCapAdj'] = hsCap
main_store[property][cyear]['Assessed'] = assessed
main_store[property][cyear]['Primary Site Value'] = primary
main_store[property][cyear]['Primary Site SQ'] = primarySiteSq
main_store[property][cyear]['Residual Land Value'] = residual
main_store[property][cyear]['Residual Land SQ'] = residualSq
main_store[property][cyear]['Exemptions'] = exemptions
# Get various floor SQ footage
segments = get_segments(propId)
improvements = traverse_segments(1, segments, propId)
#improvements = {}
imp = get_improvements(propId, ownerId_sql, page_data)
if len(imp) > 0:
for i in imp:
main_store[property][cyear][i] = imp[i]
for year in main_store[property].keys():
main_store[property][year]['Year'] = year
main_store[property][year]['Prop |
from setuptools import set | up
from setuptools import find_packages
setup(name='Keras-layers',
version='0.0.1',
description='Collection of useful non-standard layers for keras',
author='Atanas Mirchev',
author_email='taimir93@gmail.com',
url='https://github.com/taimir/keras-layers',
license='MIT',
| packages=find_packages())
|
nb_epoch = 100
batch_size = 64
optimizer = 'adam'
hidden_units = 3000
nb_val_samples = 462 |
embedding_size = 10
dropout_percentage = 0.3 |
embedding_GRU_size = 100
maximum_line_length = 500
samples_per_epoch = 4127 * maximum_line_length
loss = 'categorical_crossentropy'
|
#!/usr/bin/env python
# Original code was created by Nadeem Douba as part of the Canari Framework
from collections import OrderedDict
from xml.etree.cElementTree import XML
from zipfile import ZipFile
def mtgx2json(graph):
zipfile = ZipFile(graph)
graphs = filter(lambda x: x.endswith('.graphml'), zipfile.namelist())
for f in graphs:
multikeys = []
xml = XML(zipfile.open(f).read())
links = {}
for edge in xml.findall('{http://graphml.graphdrawing.org/xmlns}graph/'
'{http://graphml.graphdrawing.org/xmlns}edge'):
src = edge.get('source')
dst = edge.get('target')
if src not in links:
links[src] = dict(in_=[], out=[])
if dst not in links:
links[dst] = dict(in_=[], out=[])
links[src]['out'].append(dst)
links[dst]['in_'].append(src)
for node in xml.findall('{http://graphml.graphdrawing.org/xmlns}graph/'
'{http://graphml.graphdrawing.org/xmlns}node'):
node_id = node.get('id')
node = node.find('{http://graphml.graphdrawing.org/xmlns}data/'
'{http://maltego.paterva.com/xml/mtgx}MaltegoEntity')
record = OrderedDict({'NodeID': node_id, 'EntityType': node.get('type').strip()})
props = {'Data': {}}
for prop in node.findall('{http://maltego.paterva.com/xml/mtgx}Properties | /'
'{http://maltego.paterva.com/xml/mtgx}Property'):
value = prop.find('{http://maltego.paterva.com/xml/mtgx}Value').text or ''
entity_prop = {prop.get('displayName'): value.strip()}
props['Data'].update(entity_prop)
record.update(props)
s = ' - '.join(['%s: %s' % (key, value) for (key, value) in record['Data'].items()])
record.pop('Data')
data = {'Data': s}
| record.update(data)
link = {'Links': {}}
i_link = {'Incoming': links.get(node_id, {}).get('in_', 0)}
link['Links'].update(i_link)
o_link = {'Outgoing': links.get(node_id, {}).get('out', 0)}
link['Links'].update(o_link)
record.update(link)
multikeys.append(record)
return multikeys
|
from io import StringIO
from coaster.logger import RepeatValueIndicator, filtered_value, pprint_with_indent
def test_filtered_value():
"""Test for filtered values."""
# Doesn't touch normal key/value pairs
assert filtered_value('normal', 'value') == 'value'
assert filtered_value('also_normal', 123) == 123
# But does redact sensitive keys
assert filtered_value('password', '123pass') != '123pass'
# The returned value is an object that renders via repr and str as '[Filtered]'
assert repr(filtered_value('password', '123pass')) == '[Filtered]'
assert str(filtered_value('password', '123pass')) == '[Filtered]'
# Also works on partial matches in the keys
assert repr(filtered_value('confirm_password', '123pass')) == '[Filtered]'
# The filter uses a verbose regex. Words in the middle of the regex also work
assert repr(filtered_value('access_token', 'secret-here')) == '[Filtered]'
# Filters are case insensitive
assert repr(filtered_value('TELEGRAM_ERROR_APIKEY', 'api:key')) == '[Filtered]'
# Keys with 'token' as a word are also filtered
assert repr(filtered_value('SMS_TWILIO_TOKEN', 'api:key')) == '[Filtered]'
# Numbers that look like card numbers are filtered
assert (
filtered_value('anything', 'My number is 1234 5678 9012 3456')
== 'My number is [Filtered]'
)
# This works with any combination of spaces and dashes within the number
assert (
filtered_value('anything', 'My numbe | r is 1234 5678-90123456')
== 'My number is [Filtered]'
)
def test_pprint_with_indent():
"""Test pprint_with_indent does indentation."""
out = StringIO()
data = {
12: 34,
'confirm_password': '12345qwerty',
'credentials': ['abc', 'def'],
'key' | : 'value',
'nested_dict': {'password': 'not_filtered'},
'password': '12345qwerty',
}
pprint_with_indent(data, out)
assert (
out.getvalue()
== '''\
{12: 34,
'confirm_password': [Filtered],
'credentials': [Filtered],
'key': 'value',
'nested_dict': {'password': 'not_filtered'},
'password': [Filtered]}
'''
)
def test_repeat_value_indicator():
"""Test RepeatValueIndicator class."""
assert repr(RepeatValueIndicator('key')) == "<same as prior 'key'>"
assert str(RepeatValueIndicator('key')) == "<same as prior 'key'>"
|
# Copyright 2015 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2. | 0 (the "Li | cense");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests for tf.bitcast."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import tensorflow as tf
class BitcastTest(tf.test.TestCase):
def _testBitcast(self, x, datatype, shape):
with self.test_session():
tf_ans = tf.bitcast(x, datatype)
out = tf_ans.eval()
buff_after = memoryview(out).tobytes()
buff_before = memoryview(x).tobytes()
self.assertEqual(buff_before, buff_after)
self.assertEqual(tf_ans.get_shape(), shape)
self.assertEqual(tf_ans.dtype, datatype)
def testSmaller(self):
x = np.random.rand(3, 2)
datatype = tf.int8
shape = [3, 2, 8]
self._testBitcast(x, datatype, shape)
def testLarger(self):
x = np.arange(16, dtype=np.int8).reshape([4, 4])
datatype = tf.int32
shape = [4]
self._testBitcast(x, datatype, shape)
def testSameDtype(self):
x = np.random.rand(3, 4)
shape = [3, 4]
self._testBitcast(x, x.dtype, shape)
def testSameSize(self):
x = np.random.rand(3, 4)
shape = [3, 4]
self._testBitcast(x, tf.int64, shape)
def testErrors(self):
x = np.zeros([1, 1], np.int8)
datatype = tf.int32
with self.assertRaisesRegexp(ValueError, "Cannot bitcast due to shape"):
tf.bitcast(x, datatype, None)
def testEmpty(self):
x = np.ones([], np.int32)
datatype = tf.int8
shape = [4]
self._testBitcast(x, datatype, shape)
def testUnknown(self):
x = tf.placeholder(tf.float32)
datatype = tf.int8
tf.bitcast(x, datatype, None)
if __name__ == "__main__":
tf.test.main()
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.