repo_name
stringlengths
5
100
path
stringlengths
4
231
language
stringclasses
1 value
license
stringclasses
15 values
size
int64
6
947k
score
float64
0
0.34
prefix
stringlengths
0
8.16k
middle
stringlengths
3
512
suffix
stringlengths
0
8.17k
semorale/backend-test
django_backend_test/manage.py
Python
mit
262
0.003817
#!
/usr/bin/env python import os import sys if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_backend_test.settings") from django.core.management import execute_from_command_line execute_from_command_line(sys.argv)
beernarrd/gramps
gramps/gen/filters/rules/citation/__init__.py
Python
gpl-2.0
2,223
0
# # Gramps - a GTK+/GNOME based genealogy program # # Copyright (C) 2002-2007 Donald N. Allingham # Copyright (C) 2007-2008 Brian G. Matherly # Copyright (C) 2011 Tim G L Lyons # # 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 2 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, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # """ Package providing filter rules for GRAMPS. """ from ._hascitation import HasCitation from ._allcitations import AllCitations from ._changedsince import ChangedSince from ._citationprivate import CitationPrivate from ._hasgallery import HasGallery from ._hasidof import HasIdOf from ._hasnote import HasNote from ._hasnotematchingsubstringof import HasNoteMatchingSubstringOf from ._hasnoteregexp import HasNoteRegexp from ._hasreferencecountof import HasReferenceCountOf from ._hassource import HasSource from ._hassourceidof import HasSourceIdOf from ._hassourcenoteregexp import HasSourceNoteRegexp from ._match
esfilter import MatchesFilter from ._matchespagesubstringof import MatchesPageSubstringOf from ._matchesrepositoryfilter import MatchesRepositoryFilter from ._matchessourcefilter import MatchesSourceFilter from ._regexpidof import RegExpIdOf from ._regexpsourceidof import RegExpSourceIdOf from ._hastag import HasTag editor_rule_list = [ HasCitation, AllCitations, ChangedSince, CitationPrivate, HasGallery, HasIdOf,
HasNote, HasNoteRegexp, HasReferenceCountOf, HasSource, HasSourceIdOf, HasSourceNoteRegexp, MatchesFilter, MatchesPageSubstringOf, MatchesRepositoryFilter, MatchesSourceFilter, RegExpIdOf, RegExpSourceIdOf, HasTag ]
salexkidd/restframework-definable-serializer
definable_serializer/tests/test_compat.py
Python
mit
985
0.001021
from django.db import models from django.test import TestCase from ..models.compat import YAMLField class TestYAMLModel(models.Model): yaml_field = YAMLField() class TestYAMLField(TestCase): ... def test_to_python(self): yaml_data = """ main: - 1 - 2 - 3 """ yaml_field = YAMLField() yaml_field.to_python(yaml_data) yaml_data
= "" yaml_field = YAMLField() self.assertEqual(None, yaml_field.to_python(yaml_data)) yaml_data = """`""" yaml_field = YAMLField() with self.assertRaises(Except
ion): yaml_field.to_python(yaml_data) def test_get_prep_value(self): yaml_field = YAMLField() self.assertEqual("", yaml_field.get_prep_value(None)) yaml_field = YAMLField() data = {"aaa": "aaa😺",} self.assertEqual( "aaa: aaa😺\n", yaml_field.get_prep_value(data) )
lovasoa/pagelabels-py
pagelabels/pagelabelscheme.py
Python
gpl-3.0
2,320
0.001293
#!/usr/bin/env python3 from collections import namedtuple from pdfrw import PdfName, PdfDict, PdfObject, PdfString PageLabelTuple = namedtuple("PageLabelScheme", "startpage style prefix firstpagenum") defaults = {"style": "arabic", "prefix": '', "firstpagenum": 1} styles = {"arabic": PdfName('D'), "roman lowercase": PdfName('r'), "roman uppercase": PdfName('R'), "letters lowercase": PdfName('a'), "letters uppercase": PdfName('A')} stylecodes = {v: a for a, v in styles.items()} class PageLabelScheme(PageLabelTuple): """Represents a page numbering scheme. startpage : the index in the pdf (starting from 0) of the first page the scheme will be applied to. style : page numbering style (arabic, roman [lowercase|uppercase], letters [lowercase|uppercase]) prefix: a prefix to be prepended to all page labels firstpagenum : where to start numbering """ __slots__ = tuple() def
__new__(cls, startpage, style=defaults["style"], prefix=defaults["prefix"], firstpagenum=defaults["firstpagenum"]): if style not in styles: raise ValueError("Pa
geLabel style must be one of %s" % cls.styles()) return super().__new__(cls, int(startpage), style, str(prefix), int(firstpagenum)) @classmethod def from_pdf(cls, pagenum, opts): """Returns a new PageLabel using options from a pdfrw object""" return cls(pagenum, style=stylecodes.get(opts.S, defaults["style"]), prefix=(opts.P and opts.P.decode() or defaults["prefix"]), firstpagenum=(opts.St or defaults["firstpagenum"])) @staticmethod def styles(): """List of the allowed styles""" return styles.keys() def pdfobjs(self): """Returns a tuple of two elements to insert in the PageLabels.Nums entry of a pdf""" page_num = PdfObject(self.startpage) opts = PdfDict(S=styles[self.style]) if self.prefix != defaults["prefix"]: opts.P = PdfString.encode(self.prefix) if self.firstpagenum != defaults["firstpagenum"]: opts.St = PdfObject(self.firstpagenum) return page_num, opts
UKTradeInvestment/export-wins-data
fixturedb/utils/hvc.py
Python
gpl-3.0
1,557
0.004496
import operator from functools import reduce from collections import namedtuple from django.db.models import Q from mi.models import Target from wins.models import HVC HVCStruct = namedtuple('HVCStruct', ['campaign_id', 'financial_year']) def get_all_hvcs_referenced_by_targets(financial_years=None): """ Get a list of all hvcs that need to be created that are referenced by Targets :param financial_years: optional, you can manually define the financial years instead of getting them from the Target :type financial_years: List[int] :returns a list of hvc (campaign_id, financial year) tuples that don't already exist: List[HVCStruct] """ hvc_ids_expected_by_targets = Target.objects.all().values_list('campaign_id', flat=True).distinct() if not financial_years: financial_years = Target.objects.all().values_list('financial_year', flat=True).distinct() to_create = [ HVCStruct(campaign_id=campaign_id, financial_year=int(str(financial_year)[-2:])) for campa
ign_id in hvc_ids_expected_by_targets for financial_year in financial_years ] filter_q = reduce( operator.or_, [Q(campaign_id=data.campaign_id, financial_year=data.financial_year) for data in to_create] ) already_existing = [ HVCStruct(**data) for data in HVC.object
s.filter(filter_q).values('campaign_id', 'financial_year') ] to_create_without_already_existing = set(to_create) - set(already_existing) return to_create_without_already_existing
boisvert42/npr-puzzle-python
2017/0122_unusual_numbers.py
Python
cc0-1.0
2,574
0.029915
#!/usr/bin/env python """ NPR 2017-01-22 www.npr.org/2017/01/22/511046359/youve-got-to-comb-together-to-solve-this-one The numbers 5,000, 8,000, and 9,000 share a property that only five integers altogether have. Identify the property and the two other integers that have it. """ # The property is that they are supervocalic (one each of aeiou). # This code will simply try to find the other such numbers. def is_supervocalic(w): ''' Determine if a word has one each of a, e, i, o, u We also want it not to have a 'y' ''' vowels = 'aeiou' for vowel in vowels: if w.lower().count(vowel) != 1: return False if 'y' in w.lower(): return False return True # Thanks to http://stackoverflow.com/a/19193721 def numToWords(num,join=True): '''words = {} convert an integer number into words''' units = ['','one','two','three','four','five','six','seven','eight','nine'] teens = ['','eleven','twelve','thirteen','fourteen','fifteen','sixteen', \ 'seventeen','eighteen','nineteen'] tens = ['','ten','twenty','thirty','forty','fifty','sixty','seventy', \ 'eighty','ninety'] thousands = ['','thousand','million','billion','trillion','quadrillion', \ 'quintillion','sextillion','septillion','octillion', \ 'nonillion','decillion','undecillion','duodecillion', \ 'tredecillion','quattuordecillion','sexdecillion', \ 'septendecillion','octodecillion','novemdecillion', \ 'vigintillion'] words = [] if num==0: words.append('zero') else: numStr = '%d'%num numStrLen = len(numStr) groups = (numStrLen+2)/3 numStr = numStr.zfill(groups*3) for i in range(0,groups*3,3): h,t,u = int(numStr[i]),int(numStr[i+1
]),int(numStr[i+2]) g = groups-(i/3+1) if h>=1: w
ords.append(units[h]) words.append('hundred') if t>1: words.append(tens[t]) if u>=1: words.append(units[u]) elif t==1: if u>=1: words.append(teens[u]) else: words.append(tens[t]) else: if u>=1: words.append(units[u]) if (g>=1) and ((h+t+u)>0): words.append(thousands[g]) if join: return ' '.join(words) return words # Note that every integer greater than 100,000 has a repeated vowel for i in range(100000): word = numToWords(i) if is_supervocalic(word): print i, word
adamhaney/django-ipython-notebook-reports
testproject/testproject/settings.py
Python
mit
1,987
0
""" Django settings for testproject project. For more information on this file, see https://docs.djangoproject.com/en/1.6/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/1.6/ref/settings/ """ # Build paths inside the project like this: os.path.join(BASE_DIR, ...) import os BASE_DIR = os.path.dirname(os.path.dirname(__file__)) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = 'n$(okl9n*#au0%^wxgu$c#x(f%lby3v_j)wuti&6q-nx_35uj6' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True TEMPLATE_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', ) MIDDLEWARE_CLASSES = ( '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 = 'testproject.urls
' WSGI_APPLIC
ATION = 'testproject.wsgi.application' # Database # https://docs.djangoproject.com/en/1.6/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } # Internationalization # https://docs.djangoproject.com/en/1.6/topics/i18n/ LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.6/howto/static-files/ STATIC_URL = '/static/'
google/makani
config/m600/rotor_sensors.py
Python
apache-2.0
1,234
0.001621
# Copyright 2020 Makani Technologies 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. """Rotor sensing parameters.""" from makani.config import mconfig
from makani.control import system_types as m @mconfig.Config def MakeParams(): common_rotor_sensor = { # Calibration for rotor speed and torque. This applies both to # the speed sensed by and commanded to the motor controllers. # # TODO: The sign convention for rotor # velocity should be reversed both here and in the motor # controller. 'omega_cal': {'scale': -1.0, 'bias': 0.0, 'bias_count': 0}, 'torque_cal': {'scale': -1.0, 'bias': 0.0, 'bias_count': 0}, } return [common_rotor_sensor for _ in range(m.kNumMotors)]
ixc/glamkit-eventtools
eventtools/tests/views.py
Python
bsd-3-clause
9,613
0.006971
# # -*- coding: utf-8“ -*- # from datetime import date, time, datetime, timedelta # from dateutil.relativedelta import relativedelta # # from django.conf import settings # from django.core.urlresolvers import reverse # from django.test import TestCase # # from eventtools.utils import datetimeify # from eventtools_testapp.models import * # # from _fixture import bigfixture, reload_films # from _inject_app import TestCaseWithApp as AppTestCase # # class TestViews(AppTestCase): # # def setUp(self): # if hasattr(settings, 'OCCURRENCES_PER_PAGE'): # self._old_OCCURRENCES_PER_PAGE = settings.OCCURRENCES_PER_PAGE # settings.OCCURRENCES_PER_PAGE = 20 #
super(TestViews, self).setUp() # # def tearDown(self): # if hasattr(self, '_old_OCCURRENCES_PER_PAGE'): # settings.OCCURRENCES_PER_PAGE = self._old_OCCURRENCES_PER_PAGE #
else: # delattr(settings, 'OCCURRENCES_PER_PAGE') # super(TestViews, self).tearDown() # # def test_purls(self): # """ # An occurrence has a pURL based on its id. # You can view a page for an occurrence. # """ # # e = self.daily_tour # o = e.occurrences.all()[0] # # #occurrence page # ourl = reverse('occurrence', args=(o.id,)) # self.assertEqual(o.get_absolute_url(), ourl) # self.assertTrue(str(o.id) in ourl) # r1 = self.client.get(ourl) # self.assertEqual(r1.status_code, 200) # # self.assertContains(r1, "Daily Tour") # self.assertContains(r1, "1&nbsp;January&nbsp;2010") # self.assertNotContains(r1, "00:00") # self.assertNotContains(r1, "12am") # self.assertNotContains(r1, "midnight") # # e2 = self.weekly_talk # ourl = reverse('occurrence', args=(e2.occurrences.all()[0].id,)) # r1 = self.client.get(ourl) # self.assertContains(r1, "Weekly Talk") # self.assertContains(r1, "1&nbsp;January&nbsp;2010, 10am&ndash;noon") # # def test_list_view(self): # """ # You can view a paginated list of occurrences for an event qs, following a given day, using ?startdate=2010-10-22&page=2. # Each page shows n=20 occurrences and paginates by that amount. # The occurrences are in chronological order. # The times of all-day events do not appear. # If there are no events in a given day, the day is not shown. # The occurrences are grouped by day (and thus a day's occurrences may span several pages - this makes computation easier). # TODO if a day is unfinished, show 'more on page n+1'.. # If there are no events in a given page, a 'no events match' message is shown. # """ # url = reverse('occurrence_list',) # r = self.client.get(url, {'startdate':'2010-01-01'}) # self.assertEqual(r.context['occurrence_pool'].count(), 109) # self.assertEqual(len(r.context['occurrence_page']), 20) # self.assertEqual(r.context['occurrence_page'][0].start.date(), date(2010,1,1)) # # #check results in chrono order # d = r.context['occurrence_pool'][0].start # for occ in r.context['occurrence_pool']: # self.assertTrue(occ.start >= d) # d = occ.start # # #should have some pagination (6 pages) # self.assertNotContains(r, "Earlier") #it's the first page # self.assertContains(r, "Later") # self.assertContains(r, "Showing 1&ndash;20&nbsp;of&nbsp;109") # # self.assertContains(r, "Friday, 1 January 2010", 1) #only print the date once # self.assertNotContains(r, "Saturday, 2 January 2010") #there are no events # self.assertContains(r, "Sunday, 3 January 2010", 1) #only print the date once # # self.assertContains(r, "10am&ndash;&#8203;noon") # self.assertNotContains(r, "12am")# these are all-day # self.assertNotContains(r, "00:00")# these are all-day # self.assertNotContains(r, "midnight") # these are all-day # # #doesn't matter how far back you go. # r2 = self.client.get(url, {'startdate':'2000-01-01'}) # self.assertEqual(list(r.context['occurrence_pool']), list(r2.context['occurrence_pool'])) # # #links # o = r.context['occurrence_page'][0] # ourl = reverse('occurrence', args=(o.id,)) # self.assertContains(r, ourl) # # #show a 'not found' message # r = self.client.get(url, {'startdate':'2020-01-01'}) # self.assertEqual(r.context['occurrence_page'].count(), 0) # self.assertContains(r, "Sorry, no events were found") # self.assertNotContains(r, "Earlier") # self.assertNotContains(r, "Later") # self.assertNotContains(r, "Showing") # self.assertEqual(r.status_code, 200) #not 404 # # # def test_date_range_view(self): # """ # You can show all occurrences between two days on one page, by adding ?enddate=2010-10-24. Pagination adds or subtracts the difference in days (+1 - consider a single day) to the range. # For some ranges, pagination is by a different amount: # TODO: Precisely a month (paginate by month) # TODO: Precisely a year (paginate by year) # """ # # url = reverse('occurrence_list',) # r = self.client.get(url, {'startdate':'2010-01-01', 'enddate':'2010-01-05'}) # self.assertEqual(r.context['occurrence_pool'].count(), 109) # self.assertEqual(len(r.context['occurrence_page']), 5) # self.assertEqual(r.context['occurrence_page'][0].start.date(), date(2010,1,1)) # self.assertEqual(r.context['occurrence_page'].reverse()[0].start.date(), date(2010,1,5)) # # self.assertContains(r, "Showing 1&ndash;5&nbsp;January&nbsp;2010") # self.assertContains(r, '<a href="?startdate=2009-12-27&amp;enddate=2009-12-31">Earlier</a>') # self.assertContains(r, '<a href="?startdate=2010-01-06&amp;enddate=2010-01-10">Later</a>') # # r = self.client.get(url, {'startdate':'2010-01-01', 'enddate':'2010-01-31'}) # self.assertContains(r, "Showing January&nbsp;2010") # # self.assertContains(r, '<a href="?datefrom=2009-12-01&dateto=2009-12-31">December 2009</a>') # # self.assertContains(r, '<a href="?datefrom=2010-02-01&dateto=2010-02-28">February 2010</a>') # # def test_event_view(self): # """ # You can view a paginated list of occurrences for an event. # """ # #event page # e = self.daily_tour # eurl = reverse('event', kwargs={'event_slug': e.slug}) # self.assertEqual(e.get_absolute_url(), eurl) # r3 = self.client.get(eurl, {'page': 2}) # self.assertEqual(r3.status_code, 200) # # #should have some pagination (3 pages) # self.assertEqual(r3.context['occurrence_page'].count(), 20) # self.assertContains(r3, "Earlier") # self.assertContains(r3, "Later") # self.assertContains(r3, "Showing 21&ndash;40&nbsp;of&nbsp;49") # # def test_ical(self): # """ # You can view an ical for an occurrence. # The ical is linked from the occurrence page. # You can view an ical for a collection of occurrences. # (TODO: do large icals perform well? If not we might have to make it a feed.) # """ # e = self.daily_tour # o = e.occurrences.all()[0] # # o_url = reverse('occurrence', kwargs={'occurrence_id': o.id }) # o_ical_url = reverse('occurrence_ical', kwargs={'occurrence_id': o.id }) # r = self.client.get(o_ical_url) # self.assertEqual(r.status_code, 200) # # self.assertContains(r, "BEGIN:VCALENDAR", 1) # self.assertContains(r, "BEGIN:VEVENT", 1) # # self.assertContains(r, "SUMMARY:Daily Tour", 1) # self.assertContains(r, "DTSTART;VALUE=DATE:20100101", 1) # self.assertContains(r, "DTEND;VALUE=DATE:20100101", 1) # self.ass
davidsminor/gaffer
python/GafferUI/CompoundDataPlugValueWidget.py
Python
bsd-3-clause
7,763
0.064666
########################################################################## # # Copyright (c) 2012, John Haddon. All rights reserved. # Copyright (c) 2013, Image Engine Design Inc. All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # # * Redistributions of source code must retain the above # copyright notice, this list of conditions and the following # disclaimer. # # * Redistributions in binary form must reproduce the above # 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 John Haddon nor the names of # any other contributors to this software may be used to endorse or # promote products derived from this software without specific prior # written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 THE COPYRIGHT OWNER OR # CONTRIBUTORS 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. # ########################################################################## from __future__ import with_statement import IECore import Gaffer import GafferUI class CompoundDataPlugValueWidget( GafferUI.CompoundPlugValueWidget ) : def __init__( self, plug, collapsed=True, label=None, summary=None, editable=True, **kw ) : GafferUI.CompoundPlugValueWidget.__init__( self, plug, collapsed, label, summary, **kw ) self.__editable = True self.__footerWidget = None def _childPlugWidget( self, childPlug ) : return _MemberPlugValueWidget( childPlug, self._label( childPlug ) ) def _footerWidget( self ) : if self.__footerWidget is not None : return self.__footerWidget if self.__class__ is CompoundDataPlugValueWidget : # slight hack so that SectionedCompoundDataPlugValueWidget doesn't get a plus button self.__footerWidget = GafferUI.ListContainer( GafferUI.ListContainer.Orientation.Horizontal ) self.__footerWidget.append( GafferUI.Spacer( IECore.V2i( GafferUI.PlugWidget.labelWidth(), 1 ) ) ) self.__footerWidget.append( GafferUI.MenuButton( image="plus.png", hasFrame=False, menu=GafferUI.Menu( self.__addMenuDefinition() ) ) ) self.__footerWidget.append( GafferUI.Spacer( IECore.V2i( 1 ), IECore.V2i( 999999, 1 ) ), expand = True ) return self.__footerWidget ## May be reimplemented by derived classes to return a suitable label # for the member represented by childPlug. def _label( self, childPlug ) : if not childPlug.getFlags( Gaffer.Plug.Flags.Dynamic ) : return childPlug["name"].getValue() return None def __addMenuDefinition( self ) : result = IECore.MenuDefinition() result.append( "/Add/Bool", { "command" : IECore.curry( Gaffer.WeakMethod( self.__addItem ), "", IECore.BoolData( False ) ) } ) result
.append( "/Add/Float", { "command" : IECore.curry( Gaffer.WeakMethod( self.__addItem ), "", IECore.FloatData( 0 ) ) } ) result.append( "/Add/Int", { "command" : IECore.curry( Gaffer.WeakMethod( self.__addItem ), "", IECore.IntData( 0 ) ) } ) result.append( "/Add/NumericDivider", { "divider" : True } ) result.append( "/Add/String", { "command" : IECore.curry( Gaffer.WeakMethod( self.__addItem ), "", IECore.StringData( "" ) ) } ) result.append(
"/Add/StringDivider", { "divider" : True } ) result.append( "/Add/V2i", { "command" : IECore.curry( Gaffer.WeakMethod( self.__addItem ), "", IECore.V2iData( IECore.V2i( 0 ) ) ) } ) result.append( "/Add/V3i", { "command" : IECore.curry( Gaffer.WeakMethod( self.__addItem ), "", IECore.V3iData( IECore.V3i( 0 ) ) ) } ) result.append( "/Add/V2f", { "command" : IECore.curry( Gaffer.WeakMethod( self.__addItem ), "", IECore.V2fData( IECore.V2f( 0 ) ) ) } ) result.append( "/Add/V3f", { "command" : IECore.curry( Gaffer.WeakMethod( self.__addItem ), "", IECore.V3fData( IECore.V3f( 0 ) ) ) } ) result.append( "/Add/VectorDivider", { "divider" : True } ) result.append( "/Add/Color3f", { "command" : IECore.curry( Gaffer.WeakMethod( self.__addItem ), "", IECore.Color3fData( IECore.Color3f( 0 ) ) ) } ) result.append( "/Add/Color4f", { "command" : IECore.curry( Gaffer.WeakMethod( self.__addItem ), "", IECore.Color4fData( IECore.Color4f( 0, 0, 0, 1 ) ) ) } ) return result def __addItem( self, name, value ) : with Gaffer.UndoContext( self.getPlug().ancestor( Gaffer.ScriptNode.staticTypeId() ) ) : self.getPlug().addOptionalMember( name, value, enabled=True ) class _MemberPlugValueWidget( GafferUI.PlugValueWidget ) : def __init__( self, childPlug, label=None ) : self.__row = GafferUI.ListContainer( GafferUI.ListContainer.Orientation.Horizontal, spacing = 4 ) GafferUI.PlugValueWidget.__init__( self, self.__row, childPlug ) if label is not None or not childPlug.getFlags( Gaffer.Plug.Flags.Dynamic ) : nameWidget = GafferUI.LabelPlugValueWidget( childPlug, horizontalAlignment = GafferUI.Label.HorizontalAlignment.Right, verticalAlignment = GafferUI.Label.VerticalAlignment.Top, ) if label is not None : nameWidget.label().setText( label ) nameWidget.label()._qtWidget().setFixedWidth( GafferUI.PlugWidget.labelWidth() ) else : nameWidget = GafferUI.StringPlugValueWidget( childPlug["name"] ) nameWidget.textWidget()._qtWidget().setFixedWidth( GafferUI.PlugWidget.labelWidth() ) self.__row.append( nameWidget ) if "enabled" in childPlug : self.__row.append( GafferUI.BoolPlugValueWidget( childPlug["enabled"], displayMode = GafferUI.BoolWidget.DisplayMode.Switch ) ) self.__row.append( GafferUI.PlugValueWidget.create( childPlug["value"] ), expand = True ) self._updateFromPlug() def setPlug( self, plug ) : GafferUI.PlugValueWidget.setPlug( self, plug ) if isinstance( self.__row[0], GafferUI.LabelPlugValueWidget ) : self.__row[0].setPlug( plug ) else : self.__row[0].setPlug( plug["name"] ) if "enabled" in plug : self.__row[1].setPlug( plug["enabled"] ) self.__row[-1].setPlug( plug["value"] ) def hasLabel( self ) : return True def childPlugValueWidget( self, childPlug, lazy=True ) : for w in self.__row : if w.getPlug().isSame( childPlug ) : return w return None def setReadOnly( self, readOnly ) : if readOnly == self.getReadOnly() : return GafferUI.PlugValueWidget.setReadOnly( self, readOnly ) for w in self.__row : w.setReadOnly( readOnly ) def _updateFromPlug( self ) : if "enabled" in self.getPlug() : with self.getContext() : enabled = self.getPlug()["enabled"].getValue() if isinstance( self.__row[0], GafferUI.StringPlugValueWidget ) : self.__row[0].setEnabled( enabled ) self.__row[-1].setEnabled( enabled ) GafferUI.PlugValueWidget.registerType( Gaffer.CompoundDataPlug.staticTypeId(), CompoundDataPlugValueWidget ) GafferUI.PlugValueWidget.registerType( Gaffer.CompoundDataPlug.MemberPlug.staticTypeId(), _MemberPlugValueWidget )
DamianPilot382/Rubiks-Cube-Solver
opencv/sources/modules/ts/misc/run_suite.py
Python
apache-2.0
6,541
0.005198
#!/usr/bin/env python import datetime from run_utils import * class TestSuite(object): def __init__(self, options, cache): self.options = options self.cache = cache self.nameprefix = "opencv_" + self.options.mode + "_" self.tests = self.cache.gatherTests(self.nameprefix + "*", self.isTest) def getOS(self): return getPlatformVersion() or self.cache.getOS() def getHardware(self): res = [] if self.cache.getArch() in ["x86", "x64"] and self.cache.withCuda(): res.append("CUDA") return res def getLogName(self, app, timestamp): app = self.getAlias(app) rev = self.cache.getGitVersion() if isinstance(timestamp, datetime.datetime): timestamp = timestamp.strftime("%Y%m%d-%H%M%S") if self.options.longname: small_pieces = [self.getOS(), self.cache.getArch()] + self.cache.getDependencies() + self.getHardware() + [self.cache.getSIMDFeatures()] big_pieces = [app, str(rev), timestamp, "_".join([p for p in small_pieces if p])] l = "__".join(big_pieces) else: pieces = [app, self.cache.getOS(), self.cache.getArch()] + self.getHardware() + [rev, timestamp] lname = "_".join([p for p in pieces if p]) lname = re.sub(r'[\(\)\[\]\s,]', '_', lname) l = re.sub(r'_+', '_', lname) return l + ".xml" def listTests(self, short = False, main = False): if len(self.tests) == 0: raise Err("No tests found") for t in self.tests: if short: t = self.getAlias(t) if not main or self.cache.isMainModule(t): log.info("%s", t) def getAlias(self, fname): return sorted(self.getAliases(fname), key = len)[0] def getAliases(self, fname): def getCuts(fname, prefix): # filename w/o extension (opencv_test_core) noext = re.sub(r"\.(exe|apk)$", '', fname) # filename w/o prefix (core.exe) nopref = fname if fname.startswith(prefix): nopref = fname[len(prefix):] # filename w/o prefix and extension (core) noprefext = noext if noext.startswith(prefix): noprefext = noext[len(prefix):] return noext, nopref, noprefext # input is full path ('/home/.../bin/opencv_test_core') or 'java' res = [fname] fname = os.path.basename(fname) res.append(fname) # filename (opencv_test_core.exe) for s in getCuts(fname, self.nameprefix): res.append(s) if self.cache.build_type == "Debug" and "Visual Studio" in self.cache.cmake_generator: res.append(re.sub(r"d$", '', s)) # MSVC debug config, remove 'd' suffix log.debug("Aliases: %s", set(res)) return set(res) def getTest(self, name): # return stored test name by provided alias for t in self.tests: if name in self.getAliases(t): return t raise Err("Can not find test: %s", name) def getTestList(self, white, black): res = [t for t in white or self.tests if self.getAlias(t) not in black] if len(res) == 0: raise Err("No tests found") return set(res) def isTest(self, fullpath): if fullpath == "java": return True if not os.path.isfile(fullpath): return False if self.cache.getOS() == "nt" and not fullpath.endswith(".exe"): return False return os.access(fullpath, os.X_OK) def wrapInValgrind(self, cmd = []): if self.options.valgrind: res = ['valgrind'] if self.options.valgrind_supp: res.append("--suppressions=%s" % self.options.valgrind_supp) res.extend(self.options.valgrind_opt) return res + cmd return cmd def runTest(self, path, logfile, workingDir, args = []): args = args[:] exe = os.path.abspath(path) if path == "java": cmd = [self.cache.ant_executable, "-Dopencv.build.type=%s" % self.cache.build_type, "buildAndTest"] ret = execute(cmd, cwd = self.cache.java_test_binary_dir + "/.build") return None, ret else: if isColorEnabled(args): args.append("--gtest_color=yes") cmd = self.wrapInValgrind([exe] + args) tempDir = TempEnvDir('OPENCV_TEMP_PATH', "__opencv_temp.") tempDir.init() log.warning("Run: %s" % " ".join(cmd)) ret = execute(cmd, cwd = workingDir) tempDir.clean() hostlogpath = os.path.join(workingDir, logfile) if os.path.isfile(hostlogpath): return hostlogpath, ret return None, ret def checkPrerequisites(self): if self.cache.getArch() == "x64" and hostmachine == "x86": raise Err("Target architecture is incompatible with current platform") def runTests(self, tests, black, workingDir, args = []): self.checkPrerequisites() args = args[:] logs = [] test_list = self.getTestList(tests, black) date = datetime.datetime.now() if len(test_list) != 1: args = [a for a in args if not a.startswith("--gtest_output=")] ret = 0 for test in test_list: more_args = [] exe = self.getTest(test) userlog = [a for a in args if a.startswith("--gtest_output=")] if len(userlog) == 0: logname = self.getLogName(exe, date) more_args.append("--gtest_output=xml:" + logname)
else: logname = userlog[0][userlog[0].find(":")+1:] log.debug("Running the test: %s (%s) ==> %s in %s", exe, args + more_args, logname, workingDir) if self.options.dry_run: logfile, r = None, 0 else: logfile, r = self.runTest(exe, logname, workingDir, args + more_args) log.debug("Test returned
: %s ==> %s", r, logfile) if r != 0: ret = r if logfile: logs.append(os.path.relpath(logfile, workingDir)) return logs, ret #=================================================================================================== if __name__ == "__main__": log.error("This is utility file, please execute run.py script")
apyrgio/synnefo
snf-cyclades-app/synnefo/volume/management/commands/snapshot-show.py
Python
gpl-3.0
2,301
0.000435
# Copyright (C) 2010-2014 GRNET S.A. # # 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 snf_django.management.commands import SynnefoCommand, CommandError from optparse import make_option from synnefo.management import common from synnefo.plankton.backend import PlanktonBackend from snf_django.management import utils class Command(SynnefoCommand): args = "<snapshot_id>" help = "Display available information about a snapshot" option_list = SynnefoCommand.option_list + ( make_option( '--user', dest='userid', default=None, help="The UUID of the owner of the snapshot. Required" "if snapshot is not public"), make_option( '--public', dest='public', default=False, action="store_true", help="Use this option if the snapshot is public"), ) @common.convert_api_faults def handle(self, *args, **options): if len(args) != 1: raise CommandError("Please provide a snapshot ID") snapshot_id = args[0] userid = options["userid"] public = options["public"] if (userid is None) and (public is False): raise CommandError("'user' option or 'public' option is required") try:
with PlanktonBackend(userid) as backend: snapshot =
backend.get_snapshot(snapshot_id) except: raise CommandError("An error occurred, verify that snapshot and " "user ID are valid") utils.pprint_table(out=self.stdout, table=[snapshot.values()], headers=snapshot.keys(), vertical=True)
revarbat/snappy-reprap
gen_assembly_index.py
Python
gpl-2.0
7,135
0.001261
#!/usr/bin/env python import re import sys snappy_ver = "v3.0" html_header_string = """\ <html> <head> <title>Snappy Assembly</title> <style> BODY { margin-bottom: 200px; } TABLE TD { vertical-align: middle; } H2 { margin-bottom: 5px; margin-top: 24px; font-size: 20pt; } LI.section { font-size: 20pt; font-weight: bold; } H3 { margin-left: -15px; margin-bottom: 5px; margin-top: 18px; font-size: 16pt; } LI.step { padding-left: 15px; margin-left: 0; font-size: 16pt; font-weight: bold; list-style-type: none; } DIV.desc { margin-bottom: 15px; font-size: 12pt; font-weight: normal; } OL { margin-left: 30px; } UL { padding-left: 5px; } </style> </head> <body> <h1>Snappy RepRap Assembly Instructions</h1> <ol> """ class GenAssemblyIndex(object): indexfile = "docs/assembly/index.html" markdownfile = "wiki/{0}-Assembly.md".format(snappy_ver) sourcefile = "full_assembly.scad" modules = [] modinfo = {} def write_index(self): with open(self.indexfile, "w") as f: f.write(html_header_string) for mod_eng in self.modules: f.write('<li class="section">') f.write('<h2>{0}</h2>\n'.format(mod_eng)) stepcnt = len(self.modinfo[mod_eng]) if stepcnt > 1: f.write('<ul>\n') for stepinfo in self.modinfo[mod_eng]: if stepcnt > 1: f.write('<li class="step">') f.write('<h3>Step {step}</h3>\n'.format(**stepinfo)) f.write( '<div class="desc">{desc}</div>\n' '<table>' '<tr>' '<td class="befor">' '<img src="{module}_before.png">' '</td>' '<td class="arrow"><img src="arrow.png"></td>' '<td class="after"><img src="{module}_after.png"></td>' '</tr>' '</table>\n' .format(**stepinfo) ) if stepcnt > 1: f.write('</li>\n') if stepcnt > 1: f.write('</ul>\n') f.write('</li>\n') f.write('<li class="section">\n') f.write('<h2>{0}</h2>\n'.format("RAMPS Wiring")) f.write('<div class="desc">\n') f.write('<p>Heres a diagram of what needs to be connected where on a RAMPS 1.4 controller board.</p>\n') f.write('<p><a href="RAMPS_Wiring_For_Snappy.png"><img width="800" height="600" src="RAMPS_Wiring_For_Snappy.png"></a></p>') f.write('<p>Click on the image to enlarge.</p>\n\n') f.write('</div>\n') f.write('</li>\n') f.write('<li class="section">\n') f.write('<h2>{0}</h2>\n'.format("Marlin Firmware for RAMPS")) f.write('<div class="desc">\n') f.write('You can find Marlin firmware pre-configured for the Snappy with a RAMPS 1.4 controller at\n') f.write('<a href="https://github.com/revarbat/snappy-reprap/tree/v3.0/firmware">https://github.com/revarbat/snappy-reprap/tree/v3.0/firmware</a>\n') f.write('</div>\n') f.write('</li>\n') f.write('</ol>\n') f.write('</body>\n') f.write('</html>\n') def write_markdown(self): with open(self.markdownfile, "w") as f: f.write("# Snappy RepRap Assembly Instructions\n\n") for mod_eng in self.modules: f.write('## {0}\n\n'.format(mod_eng)) stepcnt = len(self.modinfo[mod_eng]) for stepinfo in self.modinfo[mod_eng]: stepinfo['base'] = ( 'https://raw.githubusercontent.com/' 'revarbat/snappy-reprap/{0}/docs/assembly/' ).format(snappy_ver) if stepcnt > 1: f.write('### Step {step}\n\n'.format(**stepinfo)) f.write( '{desc}\n\n' 'Before | After\n' '------ | -----\n' '![{module} Step {step} Before]' '({base}{module}_before.png) | ' '![{module} Step {step} After]' '({base}{module}_after.png)\n\n' .format(**stepinfo) ) f.write('## {0}\n\n'.format("RAMPS Wiring")) f.write('Heres a diagram of what needs to be connected where on a RAMPS 1.4 controller board.\n\n') f.write('[![RAMPS 1.4 Wiring Diagram]({0}-RAMPS_Wiring_For_Snappy.png)]({0}-RAMPS_Wiring_For_Snappy.png)\n'.format(snappy_ver)) f.write('Click on the image to enlarge.\n\n') f.write('## {0}\n\n'.format("Marlin Firmware for RAMPS")) f.write('You can find Marlin firmware pre-configured for the Snappy with a RAMPS 1.4 controller at\n') f.write('https://github.com/revarbat/snappy-reprap/tree/{0}/firmware\n'.format(snappy_ver)) def process_module(self, module, desc): print("module: {0}".format(module)) step = 1 mod_eng = module.replace('_', ' ') \ .title() \ .replace('Xy', 'XY') \ .replace('Yz', 'YZ') mod_split = mod_eng.split(" ") if mod_split[-1].isdigit(): step = int(mod_split[-1]) mod_eng = " ".join(mod_split[:-1]) if mod_eng not in self.modules: self.modules.append(mod_eng) self.modinfo[mod_eng] = [ { 'module': module, 'step': step, 'desc': desc }, ] else: self.modinfo[mod_eng].append( { 'module': module, 'step': step, 'desc': desc }, ) def generate_index(self): mod_re = re.compile( r'module *([a-z_][a-z0-9_]*_assembly(_[0-9]+)?) *\(' ) desc_re = re.compile(r'// *desc: *(.*)$') module = "" desc = "" with open(self.sourcefile, "r") as f: for line in f.readlines(): mod_res = mod_re.search(line) if mod_res: if modu
le: self.process_module(module, desc) module = mod_res.group(1) desc = "" desc_res = desc_re.search(line) if desc_res: desc += desc_res.group(1) if module:
self.process_module(module, desc) self.write_index() self.write_markdown() def main(): genidx = GenAssemblyIndex() genidx.generate_index() sys.exit(0) if __name__ == "__main__": main() # vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap
moosemaniam/learning
ud120-projects/k_means/k_means_cluster.py
Python
cc0-1.0
2,570
0.019066
#!/usr/bin/python """ skeleton code for k-means clustering mini-project """ import pickle import numpy import matplotlib.pyplot as plt import sys sys.path.append("../tools/") from feature_format import featureFormat, targetFeatureSplit def Draw(pred, features, poi, mark_poi=False, name="image.png", f1_name="feature 1", f2_name="feature 2"): """ some plotting code designed to help you visualize your clusters """ ### plot each cluster with a different color--add more colors for ### drawing more than 4 clusters colors = ["b", "c", "k", "m", "g"] for ii, pp in enumerate(pred): plt.scatter(features[ii][0], features[ii][1], color = colors[pred[ii]]) ### if you like, place red stars over points that are POIs (just for funsies) if mark_poi: for ii, pp in enumerate(pred): if poi[ii]: plt.scatter(features[ii][0], features[ii][1], color="r", marker="*") plt.xlabel(f1_name) plt.ylabel(f2_name) plt.savefig(name) plt.show() ### load in the dict of dicts containing all the data on each person in the dataset data_dict = pickle.load( open("../final_project/final_project_dataset.pkl", "r") ) ### there's an outlier--remove it! data_dict.pop("TOTAL", 0) ### the input features we want to use ### can be any key in the person-level dictionary (salary, director_fees, etc.) feature_1 = "salary" feature_2 = "exercised_stock_options" poi = "poi" features_list = [poi, feature_1, feature_2] data = featureFormat(data_dict, features_list ) poi, finance_features = targetFeatureSplit( data ) ### in the "clustering with 3 features" part of the mini-project, ### you'll want to change this line to ### for f1, f2,
_ in finance_features: ### (as it's currently written, line below assumes 2 features) for f1, f2 in finance_features: plt.scatter( f1, f2 ) plt.show() from sklearn.cluster import KMeans features_list = ["poi", feature_1, feature_2] data2 = featureFormat(data_dict, features_list ) poi, finance_features = targetFeatureSplit( data2 ) clf = KMeans(n_clusters=2) pred = clf.fit
_predict( finance_features ) Draw(pred, finance_features, poi, name="clusters_before_scaling.pdf", f1_name=feature_1, f2_name=feature_2) ### cluster here; create predictions of the cluster labels ### for the data and store them to a list called pred try: Draw(pred, finance_features, poi, mark_poi=False, name="clusters.pdf", f1_name=feature_1, f2_name=feature_2) except NameError: print "no predictions object named pred found, no clusters to plot"
yejia/osl_notebook
social/views.py
Python
mit
62,150
0.02148
# -*- coding: utf-8 -*- from django.http import HttpResponse,HttpResponseRedirect, Http404 from django.shortcuts import get_object_or_404 from django.shortcuts import render_to_response from django import forms from django.forms import ModelForm from django.db.models import Q, F, Avg, Max, Min, Count from django.utils import simplejson from django.utils.http import urlencode from django.contrib.auth.decorators import login_required, user_passes_test from django.template import RequestContext from django.core.exceptions import ObjectDoesNotExist, FieldError, MultipleObjectsReturned from django.contrib import messages from django.core.paginator import Paginator, EmptyPage, InvalidPage from django.utils.translation import ugettext as _ , activate from django.core.mail import send_mail from django.views.decorators.cache import cache_page import re import datetime from urlparse import urlparse from django.utils.http import urlquote from notebook.notes.models import Note, Tag, create_model, WorkingSet, getW from notebook.bookmarks.models import Bookmark from notebook.scraps.models import Scrap from notebook.social.models import * from notebook.areas.models import Area, Area_Group from notebook.notes.views import User, getT, getlogger, getFolder, get_public_notes, __get_folder_context, __get_pre_url from notebook.notes.views import getSearchResults, __getQStr, __get_view_theme, Pl, ALL_VAR, __get_lang from notebook.notes.util import * from notebook.notes.constants import * from notebook.notebook_data.data import * log = getlogger('social.views') #TODO: make this the same as in notebook.notes.views #TODO:belows seems not useful at all. Think of removing them #book_entry_dict = {'notebook':'note', 'snippetbook':'snippet','bookmarkbook':'bookmark', 'scrapbook': 'scrap'} #below are not used except in wall implementation, which is commented out for now #=============================================================================== # def getN(username): # return create_model("N_"+str(username), Note, username) # # def getB(username): # return create_model("B_"+str(username), Bookmark, username) # # def getS(username): # return create_model("S_"+str(username), Scrap, username) #=============================================================================== #TODO: for now, social db is not used. Instead default is used to put those social data because social data needs user which is in default db #otherwise, need to copy the user table over to social G = Group SN = Social_Note ST = Social_Tag import notebook.settings as settings if "notification" in settings.INSTALLED_APPS: from notification import models as notification else: notification = None class AddGroupForm(ModelForm): error_css_class = 'error' required_css_class = 'required' class Meta: model = Group class EditGroupForm(ModelForm): error_css_class = 'error' required_css_class = 'required' class Meta: model = Group exclude = ('tags', 'members', 'creator', 'admins') #fields = ('type_of_linkage','desc','tags','title','private', # 'attachment','notes') #=============================================================================== # # # @login_required # def wall_snippets(request, username): # N = getN(username) # ns = N.objects.all().order_by('-init_date')[:30] # #get groups for this user # gs = G.objects.filter(members__username=username) # return render_to_response('social/wall_snippets.html', {'ns':ns, 'gs':gs, 'profile_username':username, 'book_name': 'snippets'}, context_instance=RequestContext(request)) # # # @login_required # def wall_bookmarks(request, username): # N = getN(username) # ns = N.objects.all().order_by('init_date')[:30] # #get groups for this user # gs = G.objects.filter(members__username=username) # return render_to_response('social/wall_snippets.html', {'ns':ns, 'gs':gs, 'profile_username':username, 'book_name': 'snippets'}, context_instance=RequestContext(request)) # # # @login_required # def wall_scraps(request, username): # N = getN(username) # ns = N.objects.all().order_by('init_date')[:30] # #get groups for this user # gs = G.objects.filter(members__username=username) # return render_to_response('social/wall_snippets.html', {'ns':ns, 'gs':gs, 'profile_username':username, 'book_name': 'snippets'}, context_instance=RequestContext(request)) # # # # @login_required # def wall(request, username): # N = getN(username) # B = getB(username) # S = getS(username) # today = datetime.date.today() # yesterday = today - datetime.timedelta(1) # day_bf_yest = today - datetime.timedelta(2) # ns = N.objects.all().order_by('init_date')[:10] # bs = B.objects.all().order_by('init_date')[:10] # ss = S.objects.all().order_by('init_date')[:10] # # #get groups for this user # gs = G.objects.filter(members__username=username) # # return render_to_response('social/wall.html', {'ns':ns, 'bs':bs, 'ss':ss, 'gs':gs}, context_instance=RequestContext(request)) #=============================================================================== @login_required def group_index(request, groupid): return HttpResponseRedirect('/groups/'+groupid+'/snippetbook/notes/') def get_groups_created_by_self(request, username): if username == request.user.username: gs_created_by_self = G.objects.filter(admins__username=username) else: gs_created_by_self = G.objects.filter(admins__username=username, private=False) return gs_created_by_self def get_groups_following(request, username): if username == request.user.username: gs_created_by_self = G.objects.filter(members__username=username).exclude(admins__username=username) else: gs_created_by_self = G.objects.filter(members__username=username, private=False).exclude(admins__username=username) return gs_created_by_self def get_groups_list(request, username): gs_created_by_self = get_groups_created_by_self(request, username) gs_created_by_self_list = [g for g in gs_created_by_self] gs_following = get_groups_following(request, username) gs_following_list = [g for g in gs_following] group_set = set(gs_created_by_self_list).union(set(gs_following_list)) return list(group_set) def profile(request, username): gs_created = get_groups_created_by_self(request, username) gs_following = get_groups_following(request, username) profile_member = get_object_or_404(Member, username=username) areas = Area.objects.using(username).filter(private=False) return render_to_response('social/profile.html', {'gs_created':gs_created, 'gs_following':gs_following, \ 'profile_user':User.objects.get(username=username), \ 'profile_member':profile_member, 'profile_username':username,\ 'areas':areas}, context_instance=RequestContext(request)) @login_required def friends(request, username): profile_member = get_object_or_404(Member, username=username) friends = profile_member.get_friends() sorted_members = [[m, m.get_public_notes_count()] for m in friends] sorted_members.sort(key=lambda r:r[1],reverse = True) if (not request.user.is_anonymous()) and request.user.username == username: Notice.objects.fil
ter(notice_type__label='friends_add', recipient=request.user).update(unseen=False) return render_to_response('social/friends.html', { 'profile_username':username, 'friends':sorted_members}, context_instance=RequestContext(request, {})) @login_required def friends_notes2(request, username, bookname): #print 'username:',username #print 'bookname:',bookname
friends = request.user.member.get_friends() q = Q(owner__in=friends, private=False) note_list = getSN(bookname).objects.filter(q) qstr = __getQStr(request) note_list = getSearchResults(note_list, q
heepaz/rePong
joc.py
Python
gpl-3.0
1,394
0.031564
import os.path import pygame from pygame.locals
import * from tecles import * pygame.init() class Joc (object): WIDTH = 600 HEIGHT = 400 screen = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32) background = pygame.image.load(os.path.join("Imatges","fons.jpg")) clock = pygame.time.Clock() dt = 0.05 puntsR = 0 puntsL = 0 font = pygame.font.SysFont("Arial", 20) quit = False palaL = None palaR = None pilota = None pales = pygam
e.sprite.Group() pilotes = pygame.sprite.Group() def toggle_quit(): Joc.quit = not Joc.quit def gol(): for pilota in Joc.pilotes.sprites(): if pilota.posicio[0] > Joc.WIDTH: Joc.puntsR += 1 print(Joc.puntsL, Joc.puntsR) pilota.restart() elif pilota.posicio[0] < 0: Joc.puntsL += 1 print(Joc.puntsL, Joc.puntsR) pilota.restart() def main_loop(): while not Joc.quit: for event in pygame.event.get(): if event.type == KEYUP or event.type == KEYDOWN: handle_keys(event,Joc) elif event.type == QUIT: Joc.quit = True Joc.pales.update() Joc.pilotes.update() Joc.screen.blit(Joc.background,(0,0)) Joc.pilotes.draw(Joc.screen) Joc.pales.draw(Joc.screen) Joc.gol() pygame.display.update() Joc.dt = Joc.clock.tick() / 10
barentsen/surveytools
scripts/website/create-vphas-dr2-page.py
Python
mit
1,542
0.000649
"""Produces the
catalogue page for the VPHAS website.""" import os import json import datetime #import httplib from astropy import log from jinja2 import Environment, FileSystemLoader from surveytools import SURVEYTOOLS_DATA # Load the column definitions f
rom the JSON file filename_columns = os.path.join(SURVEYTOOLS_DATA, 'vphas-columns.json') columns = json.loads(open(filename_columns).read()) def get_filesize(host, url): """Returns the filesize of a remote document.""" return 0 """ conn = httplib.HTTPConnection(host) conn.request("HEAD", url) res = conn.getresponse() try: size = float(res.getheader('content-length')) / (1024.*1024.) except Exception, e: print 'Failed {0}'.format(url) print e if size < 950: result = '{0:.0f}&thinsp;MB'.format(size) else: result = '{0:.01f}&thinsp;GB'.format(size/1024.) log.info('Filesize of {0} = {1}'.format(url, result)) return result """ args = {'host': 'www.vphas.org', 'dir_light': '/data/dr2/light', 'dir_full': '/data/dr2/full', 'last_update': datetime.datetime.utcnow().strftime("%Y-%m-%d"), 'columns': columns} if __name__ == '__main__': env = Environment(loader=FileSystemLoader('.'), trim_blocks=True, lstrip_blocks=True) env.globals['get_filesize'] = get_filesize template = env.get_template('dr2-template.html') with open("vphas-dr2.shtml", "w") as f: f.write(template.render(args))
jmuhlich/indra
models/ras_pathway/run_correction.py
Python
bsd-2-clause
855
0
import sys from indra import reach from indra.assemblers import GraphAssembler orig_txt = [ln.strip() for ln in open('ras_pathway.txt', 'rt').readlines()] correct_txt = [ln.strip() for ln in open('correction.txt', 'rt').readlines()] for ln in correct_txt: if ln.startswith('<'): remove_line = ln[2:] orig_txt.remove(remove_line) elif ln.startswith('>'): add_line = ln[2:] orig_txt.append(add_line) txt = ' '.join(orig_txt) rp = reach.process_text(txt, offline=T
rue) st = rp.statements for s in st: print '%s\t%s' % (s, s.evidence[0].text) graphpr = {'rankdir': 'TD'} nodepr = {'fontsize': 12,
'shape': 'plaintext', 'margin': '0,0', 'pad': 0} ga = GraphAssembler(st, graph_properties=graphpr, node_properties=nodepr) ga.make_model() ga.save_dot('rps6ka_correction.dot') ga.save_pdf('rps6k1_correction.pdf')
reedloden/ansible
lib/ansible/executor/task_queue_manager.py
Python
gpl-3.0
13,130
0.003427
# (c) 2012-2014, Michael DeHaan <[email protected]> # # This file is part of Ansible # # Ansible 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. # # Ansible 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 Ansible. If not, see <http://www.gnu.org/licenses/>. # Make coding more python3-ish from __future__ import (absolute_import, division, print_function) __metaclass__ = type import multiprocessing import os import tempfile from ansible import constants as C from ansible.errors import AnsibleError from ansible.executor.play_iterator import PlayIterator from ansible.executor.process.result import ResultProcess from ansible.executor.stats import AggregateStats from ansible.playbook.block import Block from ansible.playbook.play_context import PlayContext from ansible.plugins import callback_loader, strategy_loader, module_loader from ansible.template import Templar from ansible.vars.hostvars import HostVars from ansible.plugins.callback import CallbackBase from ansible.utils.unicode import to_unicode from ansible.compat.six import string_types try: from __main__ import display except ImportError: from ansible.utils.display import Display display = Display() __all__ = ['TaskQueueManager'] class TaskQueueManager: ''' This class handles the multiprocessing requirements of Ansible by creating a pool of worker forks, a result handler fork, and a manager object with shared datastructures/queues for coordinating work between all processes. The queue manager is responsible for loading the play strategy plugin, which dispatches the Play's tasks to hosts. ''' def __init__(self, inventory, variable_manager, loader, options, passwords, stdout_callback=None, run_additional_callbacks=True, run_tree=False): self._inventory = inventory self._variable_manager = variable_manager self._loader = loader self._options = options self._stats = AggregateStats() self.passwords = passwords self._stdout_callback = stdout_callback self._run_additional_callbacks = run_additional_callbacks self._run_tree = run_tree self._callbacks_loaded = False self._callback_plugins = [] self._start_at_done = False self._result_prc = None # make sure the module path (if specified) is parsed and # added to the module_loader object if options.module_path is not None: for path in options.module_path.split(os.pathsep): module_loader.add_directory(path) # a special flag to help us exit cleanly self._terminated = False # this dictionary is used to keep track of notified handlers self._notified_handlers = dict() # dictionaries to keep track of failed/unreachable hosts self._failed_hosts = dict() self._unreachable_hosts = dict() self._final_q = multiprocessing.Queue() # A temporary file (opened pre-fork) used by connection # plugins for inter-process locking. self._connection_lockfile = tempfile.TemporaryFile() def _initialize_processes(self, num): self._workers = [] for i in range(num): rslt_q = multiprocessing.Queue() self._workers.append([None, rslt_q]) self._result_prc = ResultProcess(self._final_q, self._workers) self._result_prc.start() def _initialize_notified_handlers(self, handlers): ''' Clears and initializes the shared notified handlers dict with entries for each handler in the play, which is an empty array that will contain inventory hostnames for those hosts triggering the handler. ''' # Zero the dictionary first by removing any entries there. # Proxied dicts don't support iteritems, so we have to use keys() for key in self._notified_handlers.keys(): del self._notified_handlers[key] def _process_block(b): temp_list = [] for t in b.block: if isinstance(t, Block): temp_list.extend(_process_block(t)) else: temp_list.append(t) return temp_list handler_list = [] for handler_block in handlers: handler_list.extend(_process_block(handler_block)) # then initialize it with the handler names from the handler list for handler in handler_list: self._notified_handlers[handler.get_name()] = [] def load_callbacks(self): ''' Loads all available callbacks, with the exception of those which utilize the CALLBACK_TYPE option. When CALLBACK_TYPE is set to 'stdout', only one such callback plugin will be loaded. ''' if self._callbacks_loaded: return stdout_callback_loaded = False if self._stdout_callback is None: self._stdout_callback = C.DEFAULT_STDOUT_CALLBACK if isinstance(self._stdout_callback, CallbackBase): stdout_callback_loaded = True elif isinstance(self._stdout_callback, string_types): if self._stdout
_callback not in callback_loader: raise AnsibleError("Invalid callb
ack for stdout specified: %s" % self._stdout_callback) else: self._stdout_callback = callback_loader.get(self._stdout_callback) stdout_callback_loaded = True else: raise AnsibleError("callback must be an instance of CallbackBase or the name of a callback plugin") for callback_plugin in callback_loader.all(class_only=True): if hasattr(callback_plugin, 'CALLBACK_VERSION') and callback_plugin.CALLBACK_VERSION >= 2.0: # we only allow one callback of type 'stdout' to be loaded, so check # the name of the current plugin and type to see if we need to skip # loading this callback plugin callback_type = getattr(callback_plugin, 'CALLBACK_TYPE', None) callback_needs_whitelist = getattr(callback_plugin, 'CALLBACK_NEEDS_WHITELIST', False) (callback_name, _) = os.path.splitext(os.path.basename(callback_plugin._original_path)) if callback_type == 'stdout': if callback_name != self._stdout_callback or stdout_callback_loaded: continue stdout_callback_loaded = True elif callback_name == 'tree' and self._run_tree: pass elif not self._run_additional_callbacks or (callback_needs_whitelist and (C.DEFAULT_CALLBACK_WHITELIST is None or callback_name not in C.DEFAULT_CALLBACK_WHITELIST)): continue self._callback_plugins.append(callback_plugin()) self._callbacks_loaded = True def run(self, play): ''' Iterates over the roles/tasks in a play, using the given (or default) strategy for queueing tasks. The default is the linear strategy, which operates like classic Ansible by keeping all hosts in lock-step with a given task (meaning no hosts move on to the next task until all hosts are done with the current task). ''' if not self._callbacks_loaded: self.load_callbacks() all_vars = self._variable_manager.get_vars(loader=self._loader, play=play) templar = Templar(loader=self._loader, variables=all_vars) new_play = play.copy() new_play.post_validate(templar) self.hostvars = HostVars( invento
passuf/WunderHabit
wunderlist/migrations/0012_auto_20151230_1853.py
Python
mit
631
0.001585
# -*- coding: utf-8 -*- # Generated by Django 1.9 on 2015-12-30 17:53 from _
_future__ import unicode_literals from django.conf import settings from django.db import migrations, models import django.db.models.deletion class Migration(migrations.Migration): dependencies = [ ('wunderlist', '0011_auto_20151230_1843'), ] operations = [ migrations.AlterField( model_name='connection', name='owner', field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='con
nections', to=settings.AUTH_USER_MODEL), ), ]
psachin/swift
swift/obj/replicator.py
Python
apache-2.0
38,417
0
# Copyright (c) 2010-2012 OpenStack Foundation # # 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 os import errno from os.path import isdir, isfile, join, dirname import random import shutil import time import itertools from six import viewkeys import six.moves.cPickle as pickle from swift import gettext_ as _ import eventlet from eventlet import GreenPool, tpool, Timeout, sleep, hubs from eventlet.green import subprocess from eventlet.support.greenlets import GreenletExit from swift.common.ring.utils import is_local_device from swift.common.utils import whataremyips, unlink_older_than, \ compute_eta, get_logger, dump_recon_cache, ismount, \ rsync_module_interpolation, mkdirs, config_true_value, list_from_csv, \ get_hub, tpool_reraise, config_auto_int_value, storage_directory from swift.common.bufferedhttp import http_connect from swift.common.daemon import Daemon from swift.common.http import HTTP_OK, HTTP_INSUFFICIENT_STORAGE from swift.obj import ssync_sender from swift.obj.diskfile import get_data_dir, get_tmp_dir, DiskFileRouter from swift.common.storage_policy import POLICIES, REPL_POLICY DEFAULT_RSYNC_TIMEOUT = 900 hubs.use_hub(get_hub()) def _do_listdir(partition, replication_cycle): return (((partition + replication_cycle) % 10) == 0) class ObjectReplicator(Daemon): """ Replicate objects. Encapsulates most logic and data needed by the object replication process. Each call to .replicate() performs one replication pass. It's up to the caller to do this in a loop. """ def __init__(self, conf, logger=None):
""" :param conf: configuration object obtained from ConfigParser :param logger: logging object """ self.conf = conf self.logger = logger or get_logger(conf, log_route='object-replicator') self.devices_dir = conf.get('devices', '/srv/node') self.mount_check = config_true_value(conf.get('mount_check', 'true')) self.swift_dir = conf.get('swift_dir', '/etc/swift') self.bind_ip
= conf.get('bind_ip', '0.0.0.0') self.servers_per_port = int(conf.get('servers_per_port', '0') or 0) self.port = None if self.servers_per_port else \ int(conf.get('bind_port', 6200)) self.concurrency = int(conf.get('concurrency', 1)) self.stats_interval = int(conf.get('stats_interval', '300')) self.ring_check_interval = int(conf.get('ring_check_interval', 15)) self.next_check = time.time() + self.ring_check_interval self.replication_cycle = random.randint(0, 9) self.partition_times = [] self.interval = int(conf.get('interval') or conf.get('run_pause') or 30) self.rsync_timeout = int(conf.get('rsync_timeout', DEFAULT_RSYNC_TIMEOUT)) self.rsync_io_timeout = conf.get('rsync_io_timeout', '30') self.rsync_bwlimit = conf.get('rsync_bwlimit', '0') self.rsync_compress = config_true_value( conf.get('rsync_compress', 'no')) self.rsync_module = conf.get('rsync_module', '').rstrip('/') if not self.rsync_module: self.rsync_module = '{replication_ip}::object' self.http_timeout = int(conf.get('http_timeout', 60)) self.lockup_timeout = int(conf.get('lockup_timeout', 1800)) self.recon_cache_path = conf.get('recon_cache_path', '/var/cache/swift') self.rcache = os.path.join(self.recon_cache_path, "object.recon") self.conn_timeout = float(conf.get('conn_timeout', 0.5)) self.node_timeout = float(conf.get('node_timeout', 10)) self.sync_method = getattr(self, conf.get('sync_method') or 'rsync') self.network_chunk_size = int(conf.get('network_chunk_size', 65536)) self.default_headers = { 'Content-Length': '0', 'user-agent': 'object-replicator %s' % os.getpid()} self.rsync_error_log_line_length = \ int(conf.get('rsync_error_log_line_length', 0)) self.handoffs_first = config_true_value(conf.get('handoffs_first', False)) self.handoff_delete = config_auto_int_value( conf.get('handoff_delete', 'auto'), 0) if any((self.handoff_delete, self.handoffs_first)): self.logger.warning('Handoff only mode is not intended for normal ' 'operation, please disable handoffs_first and ' 'handoff_delete before the next ' 'normal rebalance') self._df_router = DiskFileRouter(conf, self.logger) def _zero_stats(self): """Zero out the stats.""" self.stats = {'attempted': 0, 'success': 0, 'failure': 0, 'hashmatch': 0, 'rsync': 0, 'remove': 0, 'start': time.time(), 'failure_nodes': {}} def _add_failure_stats(self, failure_devs_info): for node, dev in failure_devs_info: self.stats['failure'] += 1 failure_devs = self.stats['failure_nodes'].setdefault(node, {}) failure_devs.setdefault(dev, 0) failure_devs[dev] += 1 def _get_my_replication_ips(self): my_replication_ips = set() ips = whataremyips() for policy in POLICIES: self.load_object_ring(policy) for local_dev in [dev for dev in policy.object_ring.devs if dev and dev['replication_ip'] in ips and dev['replication_port'] == self.port]: my_replication_ips.add(local_dev['replication_ip']) return list(my_replication_ips) # Just exists for doc anchor point def sync(self, node, job, suffixes, *args, **kwargs): """ Synchronize local suffix directories from a partition with a remote node. :param node: the "dev" entry for the remote node to sync with :param job: information about the partition being synced :param suffixes: a list of suffixes which need to be pushed :returns: boolean and dictionary, boolean indicating success or failure """ return self.sync_method(node, job, suffixes, *args, **kwargs) def load_object_ring(self, policy): """ Make sure the policy's rings are loaded. :param policy: the StoragePolicy instance :returns: appropriate ring object """ policy.load_ring(self.swift_dir) return policy.object_ring def _rsync(self, args): """ Execute the rsync binary to replicate a partition. :returns: return code of rsync process. 0 is successful """ start_time = time.time() ret_val = None try: with Timeout(self.rsync_timeout): proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) results = proc.stdout.read() ret_val = proc.wait() except Timeout: self.logger.error(_("Killing long-running rsync: %s"), str(args)) proc.kill() return 1 # failure response code total_time = time.time() - start_time for result in results.split('\n'): if result == '': continue if result.startswith('cd+'): continue if not ret_val: self.logger.info(result) else: self.logger.error(resul
Azure/azure-sdk-for-python
sdk/iothub/azure-mgmt-iothub/azure/mgmt/iothub/v2019_11_04/models/_models_py3.py
Python
mit
123,002
0.003382
# 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. # -------------------------------------------------------------------------- import datetime from typing import Any, Dict, List, Optional, Union from azure.core.exceptions import HttpResponseError import msrest.serialization from ._iot_hub_client_enums import * class CertificateBodyDescription(msrest.serialization.Model): """The JSON-serialized X509 Certificate. :ivar certificate: base-64 representation of the X509 leaf certificate .cer file or just .pem file content. :vartype certificate: str """ _attribute_map = { 'certificate': {'key': 'certificate', 'type': 'str'}, } def __init__( self, *, certificate: Optional[str] = None, **kwargs ): """ :keyword certificate: base-64 representation of the X509 leaf certificate .cer file or just .pem file content. :paramtype certificate: str """ super(CertificateBodyDescription, self).__init__(**kwargs) self.certificate = certificate class CertificateDescription(msrest.serialization.Model): """The X509 Certificate. Variables are only populated by the server, and will be ignored when sending a request. :ivar properties: The description of an X509 CA Certificate. :vartype properties: ~azure.mgmt.iothub.v2019_11_04.models.CertificateProperties :ivar id: The resource identifier. :vartype id: str :ivar name: The name of the certificate. :vartype name: str :ivar etag: The entity tag. :vartype etag: str :ivar type: The resource type. :vartype type: str """ _validation = { 'id': {'readonly': True}, 'name': {'readonly': True}, 'etag': {'readonly': True}, 'type': {'readonly': True}, } _attribute_map = { 'properties': {'key': 'properties', 'type': 'CertificateProperties'}, 'id': {'key': 'id', 'type': 'str'}, 'name': {'key': 'name', 'type': 'str'}, 'etag': {'key': 'etag', 'type': 'str'}, 'type': {'key': 'type', 'type': 'str'}, } def __init__( self, *, properties: Optional["CertificateProperties"] = None, **kwargs ): """ :keyword properties: The description of an X509 CA Certificate. :paramtype properties: ~azure.mgmt.iothub.v2019_11_04.models.CertificateProperties """ super(CertificateDescription, self).__init__(**kwargs) self.properties = properties self.id = None self.name = None self.etag = None self.type = None class CertificateListDescription(msrest.serialization.Model): """The JSON-serialized array of Certificate objects. :ivar value: The array of Certificate objects. :vartype value: list[~azure.mgmt.iothub.v2019_11_04.models.CertificateDescription] """ _attribute_map = { 'value': {'key': 'value', 'type': '[CertificateDescription]'}, } def __init__( self, *, value: Optional[List["CertificateDescription"]] = None, **kwargs ): """ :keyword value: The array of Certificate objects. :paramtype value: list[~azure.mgmt.iothub.v2019_11_04.models.CertificateDescription] """ super(CertificateListDescription, self).__init__(**kwargs) self.value = value class CertificateProperties(msrest.serialization.Model): """The description of an X509 CA Certificate. Variables are only populated by the server, and will be ignored when sending a request. :ivar subject: The certificate's subject name. :vartype subject: str :ivar expiry: The certificate's expiration date and time. :vartype expiry: ~datetime.datetime :ivar thumbprint: The certificate's thumbprint. :vartype thumbprint: str :ivar is_verified: Determines whether certificate has been verified. :vartype is_verified: bool :ivar created: The certificate's create date and time. :vartype created: ~datetime.datetime :ivar updated: The certificate's last update date and time. :vartype updated: ~datetime.datetime :ivar certificate: The certificate content. :vartype certificate: str """ _validation = { 'subject': {'readonly': True}, 'expiry': {'readonly': True}, 'thumbprint': {'readonly': True}, 'is_verified': {'readonly': True}, 'created': {'readonly': True}, 'updated': {'readonly': True}, } _attribute_map = { 'subject': {'key': 'subject', 'type': 'str'}, 'expiry': {'key': 'expiry', 'type': 'rfc-1123'}, 'thumbprint': {'key': 'thumbprint', 'type': 'str'}, 'is_verified': {'key': 'isVerified', 'type': 'bool'}, 'created': {'key': 'created', 'type': 'rfc-1123'}, 'updated': {'key': 'updated', 'type': 'rfc-1123'}, 'certificate': {'key': 'certificate', 'type': 'str'}, } def __init__( self, *, certificate: Optional[str] = None, **kwargs ): """ :keyword certificate: The certificate content. :paramtype certificate: str """ super(CertificateProperties, self).__init__(**kwargs) self.subject = None self.expiry = None self.thumbprint = None self.is_verified = None self.created = None self.updated = None self.certificate = certificate class CertificatePropertiesWithNonce(msrest.serialization.Model): """The description of an X509 CA Certificate including the ch
allenge nonce issued for the Proof-Of-Possession flow. Variables are only populated by the server, and will be ignored when sending a request. :ivar subject: The certificate's subject name. :vartype subject: str :ivar expiry: The certificate's expiration date and time. :vartype expiry: ~datetime.datetime :iva
r thumbprint: The certificate's thumbprint. :vartype thumbprint: str :ivar is_verified: Determines whether certificate has been verified. :vartype is_verified: bool :ivar created: The certificate's create date and time. :vartype created: ~datetime.datetime :ivar updated: The certificate's last update date and time. :vartype updated: ~datetime.datetime :ivar verification_code: The certificate's verification code that will be used for proof of possession. :vartype verification_code: str :ivar certificate: The certificate content. :vartype certificate: str """ _validation = { 'subject': {'readonly': True}, 'expiry': {'readonly': True}, 'thumbprint': {'readonly': True}, 'is_verified': {'readonly': True}, 'created': {'readonly': True}, 'updated': {'readonly': True}, 'verification_code': {'readonly': True}, 'certificate': {'readonly': True}, } _attribute_map = { 'subject': {'key': 'subject', 'type': 'str'}, 'expiry': {'key': 'expiry', 'type': 'rfc-1123'}, 'thumbprint': {'key': 'thumbprint', 'type': 'str'}, 'is_verified': {'key': 'isVerified', 'type': 'bool'}, 'created': {'key': 'created', 'type': 'rfc-1123'}, 'updated': {'key': 'updated', 'type': 'rfc-1123'}, 'verification_code': {'key': 'verificationCode', 'type': 'str'}, 'certificate': {'key': 'certificate', 'type': 'str'}, } def __init__( self, **kwargs ): """ """ super(CertificatePropertiesWithNonce, self).__init__(**kwargs) self.subject = None self.expiry = None self.thumbprint = None self.is_verified = None self.created = None self.updated = None self.verification_code = None self.certificate = None cla
dbentley/pants
src/python/pants/pantsd/process_manager.py
Python
apache-2.0
18,348
0.011609
# coding=utf-8 # Copyright 2015 Pants project contributors (see CONTRIBUTORS.md). # Licensed under the Apache License, Version 2.0 (see LICENSE). from __future__ import (absolute_import, division, generators, nested_scopes, print_function, unicode_literals, with_statement) import logging import os import signal import subprocess import time import traceback from contextlib import contextmanager import psutil from pants.base.build_environment import get_buildroot from pants.util.dirutil import read_file, rm_rf, safe_file_dump, safe_mkdir logger = logging.getLogger(__name__) @contextmanager def swallow_psutil_exceptions(): """A contextmanager that swallows standard psutil access exceptions.""" try: yield except (psutil.AccessDenied, psutil.NoSuchProcess): # This masks common, but usually benign psutil process access exceptions that might be seen # when accessing attributes/methods on psutil.Process objects. pass class ProcessGroup(object): """Wraps a logical group of processes and provides convenient access to ProcessManager objects.""" def __init__(self, name): self._name = name def _instance_from_process(self, process): """Default converter from psutil.Process to process instance classes for subclassing.""" return ProcessManager(name=process.name(), pid=process.pid, process_name=process.name()) def iter_processes(self, proc_filter=None): proc_filter = proc_filter or (lambda x: True) with swallow_psutil_exceptions(): for proc in (x for x in psutil.process_iter() if proc_filter(x)): yield proc def iter_instances(self, *args, **kwargs): for item in self.iter_processes(*args, **kwargs): yield self._instance_from_process(item) class ProcessMetadataManager(object): """"Manages contextual, on-disk process metadata.""" FILE_WAIT_SEC = 10 WAIT_INTERVAL_SEC = .1 PID_DIR_NAME = '.pids' # TODO(kwlzn): Make this configurable. class MetadataError(Exception): pass class Timeout(Exception): pass @staticmethod def _maybe_cast(item, caster): """Given a casting function, attempt to cast to that type while masking common cast exceptions. N.B. This is mostly suitable for casting string types to numeric types - e.g. a port number read from disk into an int. :param func caster: A casting callable (e.g. `int`). :returns: The result of caster(item) or item if TypeError or ValueError are raised during cast. """ try: return caster(item) except (TypeError, ValueError): # N.B. the TypeError catch here (already) protects against the case that caster is None. return item @classmethod def _get_metadata_dir_by_name(cls, name): """Retrieve the metadata dir by name. This should always live outside of the workdir to survive a clean-all. """ return os.path.join(get_buildroot(), cls.PID_DIR_NAME, name) @classmethod def _maybe_init_metadata_dir_by_name(cls, name): """Initialize the metadata directory for a named identity if it doesn't exist.""" safe_mkdir(cls._get_metadata_dir_by_name(name)) @classmethod def read_metadata_by_name(cls, name, metadata_key, caster=None): """Read process metadata using a named identity. :param string name: The ProcessMetadataManager identity/name (e.g. 'pantsd'). :param string metadata_key: The metadata key (e.g. 'pid'). :param func caster: A casting callable to apply to the read value (e.g. `int`). """ try: file_path = os.path.join(cls._get_metadata_dir_by_name(name), metadata_key) return cls._maybe_cast(read_file(file_path).strip(), caster) except (IOError, OSError): return None @classmethod def write_metadata_by_name(cls, name, metadata_key, metadata_value): """Write process metadata using a named identity. :param string name: The ProcessMetadataManager identity/name (e.g. 'pantsd'). :param string metadata_key: The metadata key (e.g. 'pid'). :param string metadata_value: The metadata value (e.g. '1729'). """ cls._maybe_init_metadata_dir_by_name(name) file_path = os.path.join(cls._get_metadata_dir_by_name(name), metadata_key) safe_file_dump(file_path, metadata_value) @classmethod def _deadline_until(cls, closure, timeout, wait_interval=WAIT_INTERVAL_SEC): """Execute a function/closure repeatedly until a True condition or timeout is met. :param func closure: the function/closure to execute (should not block for long periods of time and must return True on success). :param float timeout: the maximum amount of time to wait for a true result from the closure in seconds. N.B. this is timing based, so won't be exact if the runtime of the closure exceeds the timeout. :param float wait_interval: the amount of time to sleep between closure invocations. :raises: :class:`ProcessManager.Timeout` on execution timeout. """ deadline = time.time() + timeout while 1: if closure(): return True elif time.time() > deadline: raise cls.Timeout('exceeded timeout of {} seconds for {}'.format(timeout, closure)) elif wait_interval: time.sleep(wait_interval) @classmethod def _wait_for_file(cls, filename, timeout=FILE_WAIT_SEC, want_content=True): """Wait up to timeout seconds for filename to appear with a non-zero size or raise Timeout().""" def file_waiter(): return os.path.exists(filename) and (not want_content or os.path.getsize(filename)) try: return cls._deadline_until(file_waiter, timeout) except cls.Timeout: # Re-raise with a more helpful exception message. raise cls.Timeout('exceeded timeout of {} seconds while waiting for file {} to appear' .format(timeout, filename)) @classmethod def await_metadata_by_name(cls, name, metadata_key, timeout, caster=None): """Block up to a timeout for process metadata to arrive on disk. :param string name: The ProcessMetadataManager identity/name (e.g. 'pantsd'). :param string metadata_key: The metadata key (e.g. 'pid'). :param int timeout: The deadline to write metadata. :param type caster: A type-casting callable to apply to the read value (e.g. int, str). :returns: The value of the metadata key (read from disk post-write). :raises: :class:`ProcessMetadataManager.Timeout` on timeout. """ file_path = os.path.join(cls._get_metadata_dir_by_name(name), metadata_key) cls._wait_for_file(file_path, timeout=timeout) return cls.read_metadata_by_name(name, metadata_key, caster) @classmethod def purge_metadata_by_name(cls, name): """Purge a processes metadata directory. :raises: `ProcessManager.MetadataError` when OSError is encountered on metadata dir removal. """ meta_dir = cls._get_metadata_dir_by_name(name) logging.debug('purging metadata directory: {}'.format(meta_dir)) try: rm_rf(meta_dir) except OSError as e: raise cls.MetadataError('failed to purge metadata directory {}: {!r}'.format(meta_dir, e)) class ProcessManager(Pr
ocessMetadataManager): """Subprocess/daemon management mixin/superclass. Not intended to be thread-safe.""" class InvalidCommandOutput(Exception): pass class NonResponsiveProcess(Exception): pass class ExecutionError(Exception): def __init__(self, message, output=None):
super(ProcessManager.ExecutionError, self).__init__(message) self.message = message self.output = output def __repr__(self): return '{}(message={!r}, output={!r})'.format(type(self).__name__, self.message, self.output) KILL_WAIT_SEC = 5 KILL_CHAIN = (signal.SIGTERM, signal.SIGKILL) def __init__(self, name, pid=None, socket=None, process_name=None, socket_type=int): """ :param string name: The process identity/name (e.g. 'pantsd' or 'ng_Zinc'). :param int pid: The process pid. Overrides fetching of the self.pid @property. :param string socket: The socket metadata. Overrides fetching of the self.socket @property. :param string process_name: The
MLAB-project/satellite-observer
gr-sat_observer/python/__init__.py
Python
gpl-3.0
1,155
0.002597
# # Copyright 2008,2009 Free Software Foundation, Inc. # # This application 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, or (at your option) # any later version. # # This application 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 Genera
l Public License for more details. # # You should have received a copy of the GNU 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. # # The presence of this file turns this directory into a Python package ''' This is the GNU Radio SAT_OBSERVER module. Place your Python package description here (python/__init__.py). ''' # import swig generated symbols into the sat_
observer namespace try: # this might fail if the module is python-only from sat_observer_swig import * except ImportError: pass # import any pure python here #
fscook/simplevert
simplecoin/__init__.py
Python
mit
4,347
0.00046
from datetime import datetime import subprocess import logging import os from flask import Flask, current_app from flask.ext.sqlalchemy import SQLAlchemy from jinja2 import FileSystemLoader from werkzeug.local import LocalProxy import yaml from flask.ext.cache import Cache from bitcoinrpc import AuthServiceProxy root = os.path.abspath(os.path.dirname(__file__) + '/../') db = SQLAlchemy() cache = Cache() coinserv = LocalProxy( lambda: getattr(current_app, 'rpc_connection', None)) def create_app(config='/config.yml', celery=False): # initialize our flask application app = Flask(__name__, static_folder='../static', static_url_path='/static') # set our template path and configs app.jinja_loader = FileSystemLoader(os.path.join(root, 'templates')) config_vars = yaml.load(open(root + config)) # inject all the yaml configs app.config.update(config_vars) app.logger.info(app.config) app.rpc_connection = AuthServiceProxy( "http://{0}:{1}@{2}:{3}/" .format(app.config['coinserv']['username'], app.config['coinserv']['password'], app.config['coinserv']['address'], app.config['coinserv']['port'], pool_kwargs=dict(maxsize=app.config.get('maxsize', 10)))) # add the debug toolbar if we're in debug mode... if app.config['DEBUG']: from flask_debugtoolbar import DebugToolbarExtension DebugToolbarExtension(app) app.logger.handlers[0].setFormatter(logging.Formatter( '%(asctime)s %(levelname)s: %(message)s ' '[in %(filename)s:%(lineno)d]')) # register all our plugins db.init_app(app) cache_config = {'CACHE_TYPE': 'redis'} cache_config.update(app.config.get('main_cache', {})) cache.init_app(app, config=cache_config) if not celery: hdlr = logging.FileHandler(app.config.get('log_file', 'webserver.log')) formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') hdlr.setFormatter(formatter) app.logger.addHandler(hdlr) app.logger.setLevel(logging.INFO) # try and fetch the git version information try: output = subprocess.check_output("git show -s --format='%ci %h'", shell=True).strip().rsplit(" ", 1) app.config['hash'] = output[1] app.config['revdate'] = output[0] # celery won't work with this, so set some default except Exception: app.config['hash'] = '' app.config['revdate'] = '' # filters for jinja @app.template_filter('time_ago') def pretty_date(time=False): """ Ge
t a datetime object or a int() Epoch timestamp and return a pretty string like 'an hou
r ago', 'Yesterday', '3 months ago', 'just now', etc """ now = datetime.utcnow() if type(time) is int: diff = now - datetime.fromtimestamp(time) elif isinstance(time, datetime): diff = now - time elif not time: diff = now - now second_diff = diff.seconds day_diff = diff.days if day_diff < 0: return '' if day_diff == 0: if second_diff < 60: return str(second_diff) + " seconds ago" if second_diff < 120: return "a minute ago" if second_diff < 3600: return str(second_diff / 60) + " minutes ago" if second_diff < 7200: return "an hour ago" if second_diff < 86400: return str(second_diff / 3600) + " hours ago" if day_diff == 1: return "Yesterday" if day_diff < 7: return str(day_diff) + " days ago" if day_diff < 31: return str(day_diff/7) + " weeks ago" if day_diff < 365: return str(day_diff/30) + " months ago" return str(day_diff/365) + " years ago" from .tasks import celery celery.conf.update(app.config) # Route registration # ========================================================================= from . import views, models, api, rpc_views app.register_blueprint(views.main) app.register_blueprint(api.api, url_prefix='/api') return app
avanzosc/avanzosc6.1
avanzosc_french_amortization/__init__.py
Python
agpl-3.0
1,037
0.001929
# -*- encoding: utf-8 -*- #################################
############################################# # # OpenERP, Open Source Management Solution # Copyright (C) 2004-2010 Tiny SPRL (http://tiny.be). All Rights Reserved # # # 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/. # ############################################################################## import avanzosc_french_amortization import wizard
kc0bfv/RankedChoiceRestaurants
manage.py
Python
gpl-3.0
822
0.001217
#!/usr/bin/env python3 import os import sys if __name__ == "__main__": os.environ.setdefault("DJANG
O_SETTINGS_MODULE", "RankedChoiceRestaurants.settings") try: from django.core.management import execute_from_command_line except ImportError: # The above import may fail for some othe
r reason. Ensure that the # issue is really that Django is missing to avoid masking other # exceptions on Python 2. try: import django except ImportError: raise ImportError( "Couldn't import Django. Are you sure it's installed and " "available on your PYTHONPATH environment variable? Did you " "forget to activate a virtual environment?" ) raise execute_from_command_line(sys.argv)
manpen/thrill
run/ec2-setup/spot.py
Python
bsd-2-clause
3,829
0.00888
#!/usr/bin/env python ########################################################################## # run/ec2-setup/spot.py # # Part of Project Thrill - http://project-thrill.org # # Copyright (C) 2015 Matthias Stumpp <[email protected]> # # All rights reserved. Published under the BSD-2 license in the LICENSE file. ########################################################################## import boto3 import time import json import datetime import sys with open('config.json') as data_file: data = json.load(data_file) client = boto3.client('ec2') ec2 = boto3.resource('ec2') job_id = int(time.time()) blockMappings = [{'DeviceName': '/dev/sda1', 'Ebs': { 'VolumeSize': 8, 'DeleteOnTermination': True, 'VolumeType': 'gp2' } }] if data["VOL_SNAPSHOT_ID"]: blockMappings.append( { 'DeviceName': data["DEVICE"], 'Ebs': { 'SnapshotId': data["VOL_SNAPSHOT_ID"], 'DeleteOnTermination': True, 'VolumeType': 'gp2' } }) response = client.request_spot_instances(SpotPrice=data["SPOT_PRICE"], InstanceCount=data["COUNT"], Type=data["TYPE"], #ValidFrom=datetime.datetime(2015, 10, 11, 18, 10, 00), ValidUntil=datetime.datetime(2015, 10, 11, 19, 37, 00), LaunchSpecification={ 'ImageId' : data["AMI_ID"], 'KeyName' : data["EC2_KEY_HANDLE"], 'InstanceType' : data["INSTANCE_TYPE"], 'SecurityGroups' : [ data["SECGROUP_HANDLE"] ],
'Placement' : { 'AvailabilityZone': data["ZONE"] 'BlockDeviceMappings' : blockMappings} }) request_ids = [] for request in response['SpotInstanceRequests']: request_ids.append(request['SpotInstanceRequestId']) fulfilled_instances
= [] loop = True; print "waiting for instances to get fulfilled..." while loop: requests = client.describe_spot_instance_requests(SpotInstanceRequestIds=request_ids) for request in requests['SpotInstanceRequests']: if request['State'] in ['closed', 'cancelled', 'failed']: print request['SpotInstanceRequestId'] + " " + request['State'] loop = False break; # TODO(ms) ensure running instances are terminated if 'InstanceId' in request and request['InstanceId'] not in running_instances: fulfilled_instances.append(request['InstanceId']) print request['InstanceId'] + " running..." if len(fulfilled_instances) == int(data["COUNT"]): print 'all requested instances are fulfilled' break; time.sleep(5) if loop == False: print "unable to fulfill all requested instances... aborting..." sys.exit(); # add tag to each instance for instance in fulfilled_instances: instance.create_tags(Tags=[{'Key': 'JobId', 'Value': str(job_id)}]) # ensure all instances are running loop = True; while loop: loop = False response = client.describe_instance_status(InstanceIds=running_instances, IncludeAllInstances=True) for status in response['InstanceStatuses']: if status['InstanceState']['Name'] != 'running': loop = True print "all instances are running..." print str(data["COUNT"]) + " instances up and running! JobId: " + str(job_id) ##########################################################################
nirzari/review-rot
test/pagure_tests/test_pagure.py
Python
gpl-3.0
16,196
0.000679
import logging from unittest import TestCase try: # Python 3 > from unittest.mock import patch # noqa: F401 except ImportError: from mock import patch # noqa: F401 try: # Python 3.3 > from unittest.mock import MagicMock, ANY # noqa: F401 except ImportError: from mock import MagicMock, ANY # noqa: F401 import requests from reviewrot.pagurestack import PagureService from . import mock_pagure PATH = 'reviewrot.pagurestack.' # Disable logging to avoid messing up test output logging.disable(logging.CRITICAL) class PagureTest(TestCase): """ This class represents the Pagure test cases """ def setUp(self): """ Setting up the testing environment """ # Mock Last Comment self.mock_last_comment = MagicMock() # Mock Datetime self.mock_utcfromtimestamp = MagicMock() self.mock_utcfromtimestamp.strftime.return_value = 'mock_date' # Mock Datetime ValueError self.mock_utcfromtimestamp_error = MagicMock() self.mock_utcfromtimestamp_error.strftime.return_value = '2019-01-05 12:12:12' # Mock Age self.mock_age = MagicMock() self.mock_age.state = 'mock_age_state' @patch(PATH + 'urllib') @patch(PATH + 'hashlib') @patch(PATH + 'PagureService._call_api') def test_avatar_with_url(self, mock_call_api, mock_hashlib, mock_urllib): """ Tests '_avatar' function where we have an avatar url """ # Set up mock return values and side effects mock_response = {'user': {'avatar_url': 'dummy_avatar_url'}} mock_url_parts = MagicMock() mock_url_parts.return_value = 'mock_url_parts' mock_url_parts.query = 'mock_url_parts_query' mock_url_query = MagicMock() mock_url_query.return_value = 'mock_url_query' mock_urllib.parse.urlparse.return_value = mock_url_parts mock_urllib.parse.parse_qs.return_value = mock_url_query mock_urllib.parse.urlencode.return_value = mock_url_query mock_urllib.parse.urlunparse.return_value = "dummy_avatar_url" mock_call_api.return_value = mock_response # Call function response = PagureService()._avatar( username='dummy_user' ) # Validate function calls and response self.assertEqual(response, 'dummy_avatar_url') mock_urllib.parse.urlparse.assert_called_with('dummy_avatar_url') mock_urllib.parse.parse_qs.assert_called_with('mock_url_parts_query') mock_call_api.assert_called_with( url='https://pagure.io/api/0/user/dummy_user', ssl_verify=True) mock_url_query.update.assert_called_with({'s': 64, 'd': 'retro'}) mock_urllib.parse.urlencode.assert_called_with(mock_url_query, doseq=True) mock_urllib.parse.urlunparse.assert_called_with( mock_url_parts[:4] + (mock_url_query,) + mock_url_parts[5:] ) mock_hashlib.assert_not_called() @patch(PATH + 'urllib') @patch(PATH + 'hashlib') @patch(PATH + 'PagureService._call_api') def test_avatar_without_url(self, mock_call_api, mock_hashlib, mock_urllib): """ Tests '_avatar' function where we don't have an avatar url """ # Set up mock return values and side effects mock_hash = MagicMock() mock_hash.hexdigest.return_value = 'mock_idx' mock_call_api.return_value = {} mock_urllib.parse.urlencode.return_value = "mock_query" mock_hashlib.sha256.return_value = mock_hash # Call function response = PagureService()._avatar( username='dummy_user' ) # Validate function calls and response mock_urllib.parse.urlencode.assert_called_with({'s': 64, 'd': 'retro'}) mock_call_api.assert_called_with( url='https://pagure.io/api/0/user/dummy_user', ssl_verify=True ) mock_hashlib.sha256.assert_called_once_with( b'http://dummy_user.id.fedoraproject.org/') self.assertEqual( response, 'https://seccdn.libravatar.org/avatar/mock_idx?mock_query' ) mock_urllib.parse.urlparse.assert_not_called() mock_urllib.parse.parse_qs.assert_not_called() mock_urllib.parse.urlunparse.assert_not_called() @patch(PATH + 'urllib') @patch(PATH + 'hashlib') @patch(PATH + 'PagureService._call_api') def test_avatar_ValueError(self, mock_call_api, mock_hashlib, mock_urllib): """ Tests '_avatar' function where we get a HTTPError and raise a ValueError """ # Set up mock return values and side effects mock_call_api.side_effect = requests.exceptions.HTTPError # Call function with self.assertRaises(ValueError): PagureService()._avatar( username='dummy_user' ) # Validate function calls and response mock_call_api.assert_called_with( url='https://pagure.io/api/0/user/dummy_user', ssl_verify=True ) mock_urllib.parse.urlencode.assert_not_called() mock_hashlib.sha256.assert_not_called() mock_urllib.parse.parse_qs.assert_not_called() mock_urllib.parse.urlunparse.assert_not_called() @patch(PATH + 'LastComment') @patch(PATH + 'datetime') def test_last_comment_no_comments(self, mock_datetime, mock_LastComment): """ Tests 'get_last_comment' with no comments """ response = PagureService().get_last_comment( res={'comments': {}} ) mock_datetime.utcfromtimestamp.assert_not_called() mock_LastComment.assert_not_called() self.assertEqual(None, response) @patch(PATH + 'LastComment') @patch(PATH + 'datetime') def test_last_comment(self, mock_datetime, mock_LastComment): """ Tests 'get_last_comment' """ # Set up mock return values and side effects mock_datetime.utcfromtimestamp.return_value = 'mock_date' mock_LastComment.return_value = 'mock_return_value' # Call function response = PagureService().get_last_comment( res={'comments': [{ 'date_created': '1', 'comment': 'mock_comment', 'user': {'name': 'mock_name'} }]} ) # Validate function calls and response mock_datetime.utcfromtimestamp.assert_called_with(1) mock_LastComment.assert_called_with( author='mock_name', body='mock_comment', created_at='mock_date' ) self.assertEqual('mock_return_value', response) @patch(PATH + 'PagureService._call_api') @patch(PATH + 'PagureService.get_last_comment') @patch(PATH + 'datetime') @patch(PATH + 'PagureService.check_request_state') @patch(PATH + 'PagureService.has_new_comments') @patch(PATH + 'PagureReview') @patch(PATH + 'PagureService._avatar') def test_request_reviews_with_repo(self, mock_avatar, mock_PagureReview, mock_has_new_comments, mock_check_request_state, mock_datetime, mock_get_last_comment, mock_call_api): """ Tests 'request_reviews' function with repos and: * no last comment, * check_request_state returns True * no errors, * no namespace """ # Set up mock return values and side effects mock_check_request_state.return_value = True mock_avatar.return_value = 'dummy_avatar' mock_get_last_comment.return_value = 'dummy
_last_comment' mock_datetime.utcfromtimestamp.return_value = self.mock_utcfromtimestamp mock_datetime.strptime.return_value = 'mock_strptime_date' mock_PagureReview.return_value = '1' mock_call
_api.return_value = mock_pagure.mock_api_
alkivi-sas/nefario-api
nefario/errors.py
Python
lgpl-3.0
804
0
"""Handle nice json resp
onse for error.""" from flask import jsonify def not_found(e): """Send a correct json for 404.""" response = jsonify({'status': 404, 'error': 'Not found', 'message': 'Invalid resource URI'}) response.status_code = 404 return response def method_not_supported(e): """Send a correct jso
n for 405.""" response = jsonify({'status': 405, 'error': 'Method not supported', 'message': 'This method is not supported'}) response.status_code = 405 return response def internal_server_error(e): """Send a correct json for 500.""" response = jsonify({'status': 500, 'error': 'Internal server error', 'message': e.args[0]}) response.status_code = 500 return response
jamestwebber/scipy
scipy/optimize/_linprog_util.py
Python
bsd-3-clause
65,452
0.000993
""" Method agnostic utility functions for linear progamming """ import numpy as np import scipy.sparse as sps from warnings import warn from .optimize import OptimizeWarning from scipy.optimize._remove_redundancy import ( _remove_redundancy, _remove_redundancy_sparse, _remove_redundancy_dense ) from collections import namedtuple _LPProblem = namedtuple('_LPProblem', 'c A_ub b_ub A_eq b_eq bounds x0') _LPProblem.__new__.__defaults__ = (None,) * 6 # make c the only required arg _LPProblem.__doc__ = \ """ Represents a linear-programming problem. Attributes ---------- c : 1D array The coefficients of the linear objective function to be minimized. A_ub : 2D array, optional The inequality constraint matrix. Each row of ``A_ub`` specifies the coefficients of a linear inequality constraint on ``x``. b_ub : 1D array, optional The inequality constraint vector. Each element represents an upper bound on the corresponding value of ``A_ub @ x``. A_eq : 2D array, optional The equality constraint matrix. Each row of ``A_eq`` specifies the coefficients of a linear equality constraint on ``x``. b_eq : 1D array, optional The equality constraint vector. Each element of ``A_eq @ x`` must equal the corresponding element of ``b_eq``. bounds : sequence, optional A sequence of ``(min, max)`` pairs for each element in ``x``, defining the minimum and maximum values of that decision variable. Use ``None`` to indicate that there is no bound. By default, bounds are ``(0, None)`` (all decision variables are non-negative). If a single tuple ``(min, max)`` is provided, then ``min`` and ``max`` will serve as bounds for all decision variables. x0 : 1D array, optional Guess values of the decision variables, which will be refined by the optimization algorithm. This argument is currently used only by the 'revised simplex' method, and can only be used if `x0` represents a basic feasible solution. Notes ----- This namedtuple supports 2 ways of initialization: >>> lp1 = _LPProblem(c=[-1, 4], A_ub=[[-3, 1], [1, 2]], b_ub=[6, 4]) >>> lp2 = _LPProblem([-1, 4], [[-3, 1], [1, 2]], [6, 4]) Note that only ``c`` is a required argument here, whereas all other arguments ``A_ub``, ``b_ub``, ``A_eq``, ``b_eq``, ``bounds``, ``x0`` are optional with default values of None. For example, ``A_eq`` and ``b_eq`` can be set without ``A_ub`` or ``b_ub``: >>> lp3 = _LPProblem(c=[-1, 4], A_eq=[[2, 1]], b_eq=[10]) """ def _check_sparse_inputs(options, A_ub, A_eq): """ Check the provided ``A_ub`` and ``A_eq`` matrices conform to the specified optional sparsity variables. Parameters ---------- A_ub : 2-D array, optional 2-D array such that ``A_ub @ x`` gives the values of the upper-bound inequality constraints at ``x``. A_eq : 2-D array, optional 2-D array such that ``A_eq @ x`` gives the values of the equality constraints at ``x``. options : dict A dictionary of solver options. All methods accept the following generic options: maxiter : int Maximum number of iterations to perform. disp : bool Set to True to print convergence messages. For method-specific options, see :func:`show_options('linprog')`. Returns ------- A_ub : 2-D array, optional 2-D array such that ``A_ub @ x`` gives the values of the upper-bound inequality constraints at ``x``. A_eq : 2-D array, optional 2-D array such that ``A_eq @ x`` gives the values of the equality constraints at ``x``. options : dict A dictionary of solver options. All methods accept the following generic options: maxiter : int Maximum number of iterations to perform. disp : bool Set to True to print convergence messages. For method-specific options, see :func:`show_options('linprog')`. """ # This is an undocumented option for unit testing sparse presolve _sparse_presolve = options.pop('_sparse_presolve', False) if _sparse_presolve and A_eq is not None: A_eq = sps.coo_matrix(A_eq) if _sparse_presolve and A_ub is not None: A_ub = sps.coo_matrix(A_ub) sparse = options.get('sparse', False) if not sparse and (sps.issparse(A_eq) or sps.issparse(A_ub)): options['sparse'] = True warn("Sparse constraint matrix detected; setting 'sparse':True.", OptimizeWarning, stacklevel=4) return options, A_ub, A_eq def _format_A_constraints(A, n_x, sparse_lhs=False): """Format the left hand side of the constraints to a 2-D array Parameters ---------- A : 2-D array 2-D array such that ``A @ x`` gives the values of the upper-bound (in)equality constraints at ``x``. n_x : int The number of variables in the linear programming problem. sparse_lhs : bool Whether either of `A_ub` or `A_eq` are sparse. If true return a coo_matrix instead of a numpy array. Returns ------- np.ndarray or sparse.coo_matrix 2-D array such that ``A @ x`` gives the values of the upper-bound (in)equality constraints at ``x``. """ if sparse_lhs: return sps.coo_matrix( (0
, n_x) if A is None else A, dtype=float, copy=True ) elif A is None: return np.zeros((0, n_x), dtype=float) else: return np.array(A, dtype=float, copy=True) def _fo
rmat_b_constraints(b): """Format the upper bounds of the constraints to a 1-D array Parameters ---------- b : 1-D array 1-D array of values representing the upper-bound of each (in)equality constraint (row) in ``A``. Returns ------- 1-D np.array 1-D array of values representing the upper-bound of each (in)equality constraint (row) in ``A``. """ if b is None: return np.array([], dtype=float) b = np.array(b, dtype=float, copy=True).squeeze() return b if b.size != 1 else b.reshape((-1)) def _clean_inputs(lp): """ Given user inputs for a linear programming problem, return the objective vector, upper bound constraints, equality constraints, and simple bounds in a preferred format. Parameters ---------- lp : A `scipy.optimize._linprog_util._LPProblem` consisting of the following fields: c : 1D array The coefficients of the linear objective function to be minimized. A_ub : 2D array, optional The inequality constraint matrix. Each row of ``A_ub`` specifies the coefficients of a linear inequality constraint on ``x``. b_ub : 1D array, optional The inequality constraint vector. Each element represents an upper bound on the corresponding value of ``A_ub @ x``. A_eq : 2D array, optional The equality constraint matrix. Each row of ``A_eq`` specifies the coefficients of a linear equality constraint on ``x``. b_eq : 1D array, optional The equality constraint vector. Each element of ``A_eq @ x`` must equal the corresponding element of ``b_eq``. bounds : sequence, optional A sequence of ``(min, max)`` pairs for each element in ``x``, defining the minimum and maximum values of that decision variable. Use ``None`` to indicate that there is no bound. By default, bounds are ``(0, None)`` (all decision variables are non-negative). If a single tuple ``(min, max)`` is provided, then ``min`` and ``max`` will serve as bounds for all decision variables. x0 : 1D array, optional Guess values of the decision variables, which will be refined by the optimization algorithm. This argument is currently used only by the 'revised simplex' method, and can only be used if `x0` re
antb/TPT----My-old-mod
src/python/stdlib/test/pickletester.py
Python
gpl-2.0
39,620
0.001514
import unittest import pickle import cPickle import StringIO import cStringIO import pickletools import copy_reg from test.test_support import TestFailed, have_unicode, TESTFN # Tests that try a number of pickle protocols should have a # for proto in protocols: # kind of outer loop. assert pickle.HIGHEST_PROTOCOL == cPickle.HIGHEST_PROTOCOL == 2 protocols = range(pickle.HIGHEST_PROTOCOL + 1) # Copy of test.test_support.run_with_locale. This is needed to support Python # 2.4, which didn't include it. This is all to support test_xpickle, which # bounces pickled objects through older Python versions to test backwards # compatibility. def run_with_locale(catstr, *locales): def decorator(func): def inner(*args, **kwds): try: import locale category = getattr(locale, catstr) orig_locale = locale.setlocale(category) except AttributeError: # if the test author gives us an invalid category string raise except: # cannot retrieve original locale, so do nothing locale = orig_locale = None else: for loc in locales: try: locale.setlocale(category, loc) break except: pass # now run the function, resetting the locale on exceptions try: return func(*args, **kwds) finally: if locale and orig_locale: locale.setlocale(category, orig_locale) inner.func_name = func.func_name inner.__doc__ = func.__doc__ return inner return decorator # Return True if opcode code appears in the pickle, else False. def opcode_in_pickle(code, pickle): for op, dummy, dummy in pickletools.genops(pickle): if op.code == code: return True return False # Return the number of times opcode code appears in pickle. def count_opcode(code, pickle): n = 0 for op, dummy, dummy in pickletools.genops(pickle): if op.code == code: n += 1 return n # We can't very well test the extension registry without putting known stuff # in it, but we have to be careful to restore its original state. Code # should do this: # # e = ExtensionSaver(extension_code) # try: # fiddle w/ the extension registry's stuff for extension_code # finally: # e.restore() class ExtensionSaver: # Remember current registration for code (if any), and remove it (if # there is one). def __init__(self, code): self.code = code if code in copy_reg._inverted_registry: self.pair = copy_reg._inverted_registry[code] copy_reg.remove_extension(self.pair[0], self.pair[1], code) else: self.pair = None # Restore previous registration for code. def restore(self): code = self.code curpair = copy_reg._inverted_registry.get(code) if curpair is not None: copy_reg.remove_extension(curpair[0], curpair[1], code) pair = self.pair if pair is not None: copy_reg.add_extension(pair[0], pair[1], code) class C: def __cmp__(self, other): return cmp(self.__dict__, other.__dict__) import __main__ __main__.C = C C.__module__ = "__main__" class myint(int): def __init__(self, x): self.str = str(x) class initarg(C): def __init__(self, a, b): self.a = a self.b = b def __getinitargs__(self): return self.a, self.b class metaclass(type): pass class use_metaclass(object): __metaclass__ = metaclass # DATA0 .. DATA2 are the pickles we expect under the various protocols, for # the object returned by create_data(). # break into multiple strings to avoid confusing font-lock-mode DATA0 = """(lp1 I0 aL1L aF2 ac__builtin__ complex p2 """ + \ """(F3 F0 tRp3 aI1 aI-1 aI255 aI-255 aI-256 aI65535 aI-65535 a
I-65536 aI2147483647 aI-2147483647 aI-2147483648 a""" + \ """(S'abc' p4 g4 """ + \ """(i__main__ C p5 """ + \ """(dp6 S'foo' p7 I1 sS'bar' p8 I2 sbg5 tp9 ag9 aI5 a. """ # Disassembly of DATA0. DATA0_DIS = """\ 0: ( MARK 1: l LIST (MARK at 0) 2: p PUT 1 5
: I INT 0 8: a APPEND 9: L LONG 1L 13: a APPEND 14: F FLOAT 2.0 17: a APPEND 18: c GLOBAL '__builtin__ complex' 39: p PUT 2 42: ( MARK 43: F FLOAT 3.0 46: F FLOAT 0.0 49: t TUPLE (MARK at 42) 50: R REDUCE 51: p PUT 3 54: a APPEND 55: I INT 1 58: a APPEND 59: I INT -1 63: a APPEND 64: I INT 255 69: a APPEND 70: I INT -255 76: a APPEND 77: I INT -256 83: a APPEND 84: I INT 65535 91: a APPEND 92: I INT -65535 100: a APPEND 101: I INT -65536 109: a APPEND 110: I INT 2147483647 122: a APPEND 123: I INT -2147483647 136: a APPEND 137: I INT -2147483648 150: a APPEND 151: ( MARK 152: S STRING 'abc' 159: p PUT 4 162: g GET 4 165: ( MARK 166: i INST '__main__ C' (MARK at 165) 178: p PUT 5 181: ( MARK 182: d DICT (MARK at 181) 183: p PUT 6 186: S STRING 'foo' 193: p PUT 7 196: I INT 1 199: s SETITEM 200: S STRING 'bar' 207: p PUT 8 210: I INT 2 213: s SETITEM 214: b BUILD 215: g GET 5 218: t TUPLE (MARK at 151) 219: p PUT 9 222: a APPEND 223: g GET 9 226: a APPEND 227: I INT 5 230: a APPEND 231: . STOP highest protocol among opcodes = 0 """ DATA1 = (']q\x01(K\x00L1L\nG@\x00\x00\x00\x00\x00\x00\x00' 'c__builtin__\ncomplex\nq\x02(G@\x08\x00\x00\x00\x00\x00' '\x00G\x00\x00\x00\x00\x00\x00\x00\x00tRq\x03K\x01J\xff\xff' '\xff\xffK\xffJ\x01\xff\xff\xffJ\x00\xff\xff\xffM\xff\xff' 'J\x01\x00\xff\xffJ\x00\x00\xff\xffJ\xff\xff\xff\x7fJ\x01\x00' '\x00\x80J\x00\x00\x00\x80(U\x03abcq\x04h\x04(c__main__\n' 'C\nq\x05oq\x06}q\x07(U\x03fooq\x08K\x01U\x03barq\tK\x02ubh' '\x06tq\nh\nK\x05e.' ) # Disassembly of DATA1. DATA1_DIS = """\ 0: ] EMPTY_LIST 1: q BINPUT 1 3: ( MARK 4: K BININT1 0 6: L LONG 1L 10: G BINFLOAT 2.0 19: c GLOBAL '__builtin__ complex' 40: q BINPUT 2 42: ( MARK 43: G BINFLOAT 3.0 52: G BINFLOAT 0.0 61: t TUPLE (MARK at 42) 62: R REDUCE 63: q BINPUT 3 65: K BININT1 1 67: J BININT -1 72: K BININT1 255 74: J BININT -255 79: J BININT -256 84: M BININT2 65535 87: J BININT -65535 92: J BININT -65536 97: J BININT 2147483647 102: J BININT -2147483647 107: J BININT -2147483648 112: ( MARK 113: U SHORT_BINSTRING 'abc' 118: q BINPUT 4 120: h BINGET 4 122: ( MARK 123: c GLOBAL '__main__ C' 135: q BINPUT 5 137: o OBJ (MARK at 122) 138: q BINPUT 6 140: } EMPTY_DICT 141: q BINPUT 7 143: ( MARK 144: U SHORT_BINSTRING 'foo' 149: q BINPUT 8 151: K BININT1 1 153: U SHORT_BINSTRING 'bar' 158: q BINPUT 9 160: K BININT1 2 162: u SETITEMS (MARK at 143) 163: b BUILD 164: h
kindkaktus/CcPy
ccpy/ccpyconfparser.py
Python
bsd-3-clause
9,656
0.001761
# # Andrei Korostelev <andrei at korostelev dot net> # # Before using this product in any way please read the license agreement. # If you do not agree to the terms in this agreement you are not allowed # to use this product or parts of it. You can read this license in the # file named LICENSE. # """ CcPy project configuration file parser """ import xml.etree.ElementTree as ET import logging from copy import deepcopy from .common import LoggerName from .util import EmailFormat, EmailSecurity, formatTb from . import svntask from . import gittask from . import maketask from . import exectask Logger = logging.getLogger(LoggerName) DefCcPyCo
nfigFileName = "/etc/ccpy.conf" def _get_elem_str_value(element, default_value): if element is not None: return element.text else: retu
rn default_value def _get_elem_int_value(element, default_value): if element is not None: return int(element.text) else: return default_value def _get_elem_bool_value(element, default_value): if element is not None: if element.text.lower() in ('on', 'yes', 'true'): return True elif element.text.lower() in ('off', 'no', 'false'): return False else: raise Exception("Invalid boolean value: %s in %s" % (element.text, element.tag)) else: return default_value def _get_elem_email_format_value(element, default_value): if element is not None: return EmailFormat[element.text] else: return default_value def _get_elem_email_security_value(element, default_value): if element is not None: return EmailSecurity[element.text] else: return default_value def _get_elem_list_value(element, default_value): if element is not None: return element.text.split(', ') else: return default_value def _get_elem_tasks_value(element, default_value): if element is None: return default_value tasks = [] for task in element: if task.tag == 'sourcecontrol': if task.attrib['type'] in ('svn', 'git'): url = task.find('./url').text workingDirectory = _get_elem_str_value(task.find('./workingDirectory'), '') preCleanWorkingDirectory = _get_elem_bool_value( task.find('./preCleanWorkingDirectory'), False) if task.attrib['type'] == 'svn': tasks.append(svntask.SvnTask(url, workingDirectory, preCleanWorkingDirectory)) else: # git tasks.append(gittask.GitTask(url, workingDirectory, preCleanWorkingDirectory)) else: Logger.warning('Unsupported sourcecontrol type ' + task.attrib['type']) elif task.tag == 'make': workingDirectory = _get_elem_str_value(task.find('./workingDirectory'), '') args = _get_elem_str_value(task.find('./args'), '') timeout = _get_elem_int_value(task.find('./timeout'), 600) tasks.append(maketask.MakeTask(workingDirectory, args, timeout)) elif task.tag == 'exec': executable = task.find('./executable').text workingDirectory = _get_elem_str_value(task.find('./workingDirectory'), '') args = _get_elem_str_value(task.find('./args'), '') timeout = _get_elem_int_value(task.find('./timeout'), 600) warningExitCode = _get_elem_int_value(task.find('./warningExitCode'), None) tasks.append( exectask.ExecTask( executable, workingDirectory, args, timeout, warningExitCode)) else: Logger.warning('Unsupported task ' + task.tag) return tasks class ParseError(Exception): pass class Projects: def __init__(self): self._projects = [] self.cur = 0 def exists(self, name): for project in self._projects: if project['name'] == name: return True return False def append(self, name, tasks, emailFrom, emailTo, emailFormat, emailServerHost, emailServerPort, emailServerSecurity, emailServerUsername, emailServerPassword, emailAttachments, failOnError): if self.exists(name): raise Exception( "Failed to add project because the project named '%s' already exists" % name) if tasks is None: tasks = [] if emailTo is None: emailTo = [] self._projects.append({'name': name, 'tasks': tasks, 'emailFrom': emailFrom, 'emailTo': emailTo, 'emailFormat': emailFormat, 'emailServerHost': emailServerHost, 'emailServerPort': emailServerPort, 'emailServerSecurity': emailServerSecurity, 'emailServerUsername': emailServerUsername, 'emailServerPassword': emailServerPassword, 'emailAttachments': emailAttachments, 'failOnError': failOnError}) def addTask(self, name, task): if not self.exists(name): raise Exception( "Failed to add task because the project named '%s' does not exist" % name) for project in self._projects: if project['name'] == name: project['tasks'].append(task) def next(self): if self.cur >= len(self._projects): self.cur = 0 raise StopIteration else: cur = self.cur self.cur = cur + 1 key = self._projects[cur]['name'] val = deepcopy(self._projects[cur]) val.pop('name') return key, val def __next__(self): # for compatibility between Python 2 that uses next() and Python 3 that uses __next__() return self.next() def __iter__(self): return self def __getitem__(self, name): for project in self._projects: if project['name'] == name: retVal = deepcopy(project) retVal.pop('name') return retVal raise Exception("Project named '%s' does not exist" % name) def __len__(self): return len(self._projects) def parse(aCcPyConfigFileName=DefCcPyConfigFileName): """Parse ccpy project configuration file Return the instance if Projects class Projects and tasks within each project are returned in the order they appear in the config file. Supported tasks are: SvnTask, MakeTask and ExecTask Throw ParseError """ try: Logger.debug("Reading ccpy configuration from %s..." % aCcPyConfigFileName) tree = ET.parse(aCcPyConfigFileName) root = tree.getroot() if (root.tag != 'ccpy'): raise Exception('Invalid root tag name: ' + root.tag) projects = Projects() for projectElem in root.findall('./project'): tasks = _get_elem_tasks_value(projectElem.find('./tasks'), None) emailFrom = _get_elem_str_value(projectElem.find('./emailNotification/from'), "") emailTo = _get_elem_list_value(projectElem.find('./emailNotification/to'), None) emailFormat = _get_elem_email_format_value( projectElem.find('./emailNotification/format'), EmailFormat.attachment) emailServerHost = _get_elem_str_value( projectElem.find('./emailNotification/server'), 'localhost') emailServerPort = _get_elem_int_value( projectElem.find('./emailNotification/port'), 25) emailServerSecurity = _get_elem_email_security_value( projectElem.find('./emailNotification/security'),
rimbalinux/LMD3
user/models.py
Python
bsd-3-clause
383
0.015666
# Sumber: LMD/models.py from google.ap
pengine.ext import db class Users(db.Model): username = db.StringProperty() passwd = db.StringProperty() email = db.StringProperty() fullname = db.StringProperty() address = db.TextProperty() phone = db.StringProperty() role = db.IntegerProperty(default=99) livecenter =
db.ListProperty(db.Key,default=[])
Malizor/deckard
libdeckard.py
Python
agpl-3.0
17,967
0.000223
# Deckard, a Web based Glade Runner # Copyright (C) 2013 Nicolas Delvaux <[email protected]> # This program 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. # 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 Affero General Public License for more details. # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. """Sessions handling and utilities for the deckard project""" import os import re import locale import shutil import tempfile import urllib.request from uuid import uuid4 from threading import Lock, Timer from collections import OrderedDict from subprocess import Popen, PIPE, STDOUT, check_output, CalledProcessError from languages import locale_language_mapping class DeckardException(Exception): """Standard exception""" def __init__(self, short, log): Exception.__init__(self, "%s\n\n%s" % (short, log)) class Session: """ This represents a Deckard session for one user. It manages its gladerunner instance (both launch and keep-alive) and custom PO files. Everything is cleaned-up when the session is deleted. """ def __init__( self, uuid, gladerunner, content_root, max_custom_po, max_po_download_size, glade_catalog, po_urls, ): self.port = 0 self.uuid = uuid # unique id to avoid session spoofing self.process = None self.custom_po = OrderedDict() # {po_name: (module, root_path, lang)} self.removable = False # can the manager delete this Session? self.gladerunner = gladerunner self.content_root = content_root self.max_custom_po = max_custom_po self.max_po_download_size = max_po_download_size self.glade_catalog = glade_catalog # URL sorted by priority # If one URL does not work, the next one will be tried self.po_urls = po_urls def spawn_runner(self, module, module_file, language, port): """Launch a gladerunner instance. If a running process is attached to this session, it will be replaced. """ self.port = port env = { "GDK_BACKEND": "broadway", "UBUNTU_MENUPROXY": "", "LIBOVERLAY_SCROLLBAR": "0", } if self.process is not None and self.process.poll() is None: self.process.kill() if language in self.custom_po: if self.custom_po[language][0] != module: raise DeckardException( '"%s" does not exist' % language, "No such file was registered for the " "%s module." % module, ) lang_root = os.path.join(self.custom_po[language][1], "LANGS") # This locale has to be available on your system language = "%s.UTF-8" % self.custom_po[language][2] else: if language != "POSIX": language = "%s.UTF-8" % language lang_root = os.path.join(self.content_root, "LANGS") env["LANG"] = language # Build the gladerunner command line args = [ self.gladerunner, "--suicidal", "--with-broadwayd", str(port), os.path.join(self.content_root, module, module_file), module, language, lang_root, ] # Should we use a Glade catalog? if os.path.isfile(self.glade_catalog): args.extend(("--catalog-path", self.glade_catalog)) # Launch it! self.process = Popen(args, stdin=PIPE, env=env) def store_po(self, name, module, fd=None): """Store a custom PO file If fd is None, try to download name from self.po_urls. Each url of the list will be tried until the file is found. If a PO file with the same name is already attached to this session, it will be replaced. Returns a dictionary, associating
all relevant modules with a list of stored PO files for it on this session, from the oldest to the newest. """ # Very basic check, msgfmt will crash anyway if the file is not valid if not name.lower().endswith(".po"): raise DeckardException( "This is not a PO file", "%s is not a PO file." % name ) lang_root = tempfile.mkdtemp(prefix="deckard_") po_path = os.path.join(lang_root, "fil
e.po") po = open(po_path, "bw") if fd is not None: # The file was sent by the user for line in fd: po.write(line) po.close() fd.close() elif len(self.po_urls) > 0: # Let's try to download 'name' response = None error = None for url in self.po_urls: try: response = urllib.request.urlopen(url % name) break except Exception as e: error = str(e) if response is None: # Most likely a '404: not found' error raise DeckardException("Enable to retrieve the file", error) res_len = response.length if res_len > self.max_po_download_size: response.close() raise DeckardException( "File too big", 'The "%s" file is %d long and this app ' "will not retrieve a file bigger than " "%d bytes." % (name, res_len, self.max_po_download_size), ) # Let's finally download this file! po.write(response.read(res_len)) response.close() po.close() else: raise DeckardException( "Operation not supported", "The PO download feature is not configured " "on this instance.", ) # Try to guess the language of this PO file, default is 'en_US' # This is good to know to later set proper environment variables and so # load the right GTK translation and reverse the interface if necessary po_lang = "en_US" with open(po_path, encoding="utf8") as po: # Give up if we find nothing in the 50 first lines for _ in range(50): line = po.readline() match = re.match(r'^"Language: (.+)\\n"$', line) if match: po_lang = match.group(1) # The encoding is often wrong, so strip it po_lang = locale.normalize(po_lang).rsplit(".")[0] # Test if the detected locale is available on the system try: locale.setlocale(locale.LC_ALL, "%s.UTF-8" % po_lang) except: # Fallback to a known locale po_lang = "en_US" finally: locale.resetlocale() break # create necessary directories mo_path = os.path.join(lang_root, "LANGS", po_lang, "LC_MESSAGES") os.makedirs(mo_path) try: check_output( [ "/usr/bin/msgfmt", "--check", "--output-file", os.path.join(mo_path, module) + ".mo", po_path, ], stderr=STDOUT, ) except CalledProcessError as e: shutil.rmtree(lang_root) # We don't need to expose the file name in the error message log = e.output.decode("unicode_escape").replace("%s:" % po_path, "") raise De
Filechaser/sickbeard_mp4_automator
delugePostProcess.py
Python
mit
5,303
0.003206
#!/usr/bin/env python import os import sys from autoprocess import autoProcessTV, autoProcessMovie, autoProcessTVSR, sonarr, radarr from readSettings import ReadSettings from mkvtomp4 import MkvtoMp4 from deluge_client import DelugeRPCClient import logging from logging.config import fileConfig logpath = '/var/log/sickbeard_mp4_automator' if os.name == 'nt': logpath = os.path.dirname(sys.argv[0]) elif not os.path.isdir(logpath): try: os.mkdir(logpath) except: logpath = os.path.dirname(sys.argv[0]) configPath = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), 'logging.ini')).replace("\\", "\\\\") logPath = os.path.abspath(os.path.join(logpath, 'index.log')).replace("\\", "\\\\") fileConfig(configPath, defaults={'logfilename': logPath}) log = logging.getLogger("delugePostProcess") log.info("Deluge post processing started.") settings = R
eadSett
ings(os.path.dirname(sys.argv[0]), "autoProcess.ini") categories = [settings.deluge['sb'], settings.deluge['cp'], settings.deluge['sonarr'], settings.deluge['radarr'], settings.deluge['sr'], settings.deluge['bypass']] remove = settings.deluge['remove'] if len(sys.argv) < 4: log.error("Not enough command line parameters present, are you launching this from deluge?") sys.exit() path = str(sys.argv[3]) torrent_name = str(sys.argv[2]) torrent_id = str(sys.argv[1]) delete_dir = None log.debug("Path: %s." % path) log.debug("Torrent: %s." % torrent_name) log.debug("Hash: %s." % torrent_id) client = DelugeRPCClient(host=settings.deluge['host'], port=int(settings.deluge['port']), username=settings.deluge['user'], password=settings.deluge['pass']) client.connect() if client.connected: log.info("Successfully connected to Deluge") else: log.error("Failed to connect to Deluge") sys.exit() torrent_data = client.call('core.get_torrent_status', torrent_id, ['files', 'label']) try: torrent_files = torrent_data[b'files'] category = torrent_data[b'label'].lower().decode() except: torrent_files = torrent_data['files'] category = torrent_data['label'].lower() files = [] log.debug("List of files in torrent:") for contents in torrent_files: try: files.append(contents[b'path'].decode()) log.debug(contents[b'path'].decode()) except: files.append(contents['path']) log.debug(contents['path']) if category.lower() not in categories: log.error("No valid category detected.") sys.exit() if len(categories) != len(set(categories)): log.error("Duplicate category detected. Category names must be unique.") sys.exit() if settings.deluge['convert']: # Check for custom Deluge output_dir if settings.deluge['output_dir']: settings.output_dir = settings.deluge['output_dir'] log.debug("Overriding output_dir to %s." % settings.deluge['output_dir']) # Perform conversion. settings.delete = False if not settings.output_dir: suffix = "convert" settings.output_dir = os.path.join(path, ("%s-%s" % (torrent_name, suffix))) if not os.path.exists(settings.output_dir): os.mkdir(settings.output_dir) delete_dir = settings.output_dir converter = MkvtoMp4(settings) for filename in files: inputfile = os.path.join(path, filename) if MkvtoMp4(settings).validSource(inputfile): log.info("Converting file %s at location %s." % (inputfile, settings.output_dir)) try: output = converter.process(inputfile) except: log.exception("Error converting file %s." % inputfile) path = converter.output_dir else: suffix = "copy" newpath = os.path.join(path, ("%s-%s" % (torrent_name, suffix))) if not os.path.exists(newpath): os.mkdir(newpath) for filename in files: inputfile = os.path.join(path, filename) log.info("Copying file %s to %s." % (inputfile, newpath)) shutil.copy(inputfile, newpath) path = newpath delete_dir = newpath # Send to Sickbeard if (category == categories[0]): log.info("Passing %s directory to Sickbeard." % path) autoProcessTV.processEpisode(path, settings) # Send to CouchPotato elif (category == categories[1]): log.info("Passing %s directory to Couch Potato." % path) autoProcessMovie.process(path, settings, torrent_name) # Send to Sonarr elif (category == categories[2]): log.info("Passing %s directory to Sonarr." % path) sonarr.processEpisode(path, settings) elif (category == categories[3]): log.info("Passing %s directory to Radarr." % path) radarr.processMovie(path, settings) elif (category == categories[4]): log.info("Passing %s directory to Sickrage." % path) autoProcessTVSR.processEpisode(path, settings) elif (category == categories[5]): log.info("Bypassing any further processing as per category.") if delete_dir: if os.path.exists(delete_dir): try: os.rmdir(delete_dir) log.debug("Successfully removed tempoary directory %s." % delete_dir) except: log.exception("Unable to delete temporary directory.") if remove: try: client.call('core.remove_torrent', torrent_id, True) except: log.exception("Unable to remove torrent from deluge.")
MGautier/security-sensor
branches/webenv/secproject/secapp/urls.py
Python
mit
2,174
0.00552
from django.conf.urls import url from rest_framework.urlpatterns import format_suffix_patterns from . import views from django.views.generic.base import View app_name = 'secapp' urlpatterns = [ # Index view url(r'^$', views.index, name='index'), # List of events for a Log Source url(r'^(?P<id_log_source>[0-9]+)/event/$', views.events, name='events'), # Packet of an event (for a Log Source) url(r'^(?P<id_log_source>[0-9]+)/event/(?P<id_event>[0-9]+)$', views.event_information, name='event_information'), # Additional information about a packet event url(r'^(?P<id_log_source>[0-9]+)/event/(?P<id_event>[0-9]+)/additional_info/$', views.additional_info, name='additional_info'), url(r'^api/events/$', views.EventsInformation().events_list, name='events_list'), url(r'^api/events/by_source/(?P<pk>[0-9]+)/$', views.EventsInformation().events_by_source, name='events_by_source'), url(r'^api/events/by_source/(?P<pk>[0-9]+)/(?P<fk>[0-9]+)/$', views.EventsInformation().events_by_source_detail, name='events_by_source_detail'), url(r'^api/events/(?P<pk>[0-9]+)/json$', views.EventsInformation().event_detail, name='event_detail'), url(r'^api/events/(?P<pk>[0-9]+)/$', views.EventsI
nformation.as_view()), url(r'^api/events/hour/(?P<pk>[0-9]+)/$', views.EventsInformation().events_source_in_hour, name='events_source_in_hour'), url(r'^api/events/day/(?P<pk>[0-9]+)/$', views.EventsInformation().events_source_in_day, name='events_source_in_day'), url(r'^api/events/week/(?P<pk>[0-9]+)/$', views.EventsInformation().events_source_in_week, name='events_source_in_week'), url(r'^api/events/month/(?P<pk>[0-9]+)/$', views.EventsInfor
mation().events_source_in_month, name='events_source_in_month'), url(r'^api/events/year/(?P<pk>[0-9]+)/$', views.EventsInformation().events_source_in_year, name='events_source_in_year'), url(r'^api/events/last_day/(?P<pk>[0-9]+)/$', views.EventsInformation().events_source_last_day, name='events_source_last_day'), ] urlpatterns = format_suffix_patterns(urlpatterns)
dgquintas/vmcontroller.unstable
src/vmcontroller.host/vmcontroller/host/__main__.py
Python
bsd-3-clause
8,090
0.004203
#!python """ VMController Host - a general purpose host-side virtual machine controller via exposed hypervisors apis. """ try: import os import sys import logging import warnings import multiprocessing import time import inject from twisted.internet import reactor from pkg_resources import resource_stream from ConfigParser import SafeConfigParser from optparse import OptionParser from vmcontroller.common import StompProtocolFactory, StompProtocol from vmcontroller.host.config import init_config, init_config_file, debug_config from vmcontroller.host.controller import HyperVisorController from vmcontroller.host.services import HostStompEngine, HostWords from vmcontroller.host.services.HostServices import Host, HostXMLRPCService except ImportError, e: print "Import error in %s : %s" % (__name__, e) import sys sys.exit() logger = logging.getLogger(__name__) def init_logging(logfile=None, loglevel=logging.INFO): """ Sets logging configuration. @param logfile: File to log messages. Default is None. @param loglevel: Log level. Default is logging.INFO. """ format = '%(asctime)s - [%(threadName)s] %(filename)s:%(lineno)s - (%(levelname)s) %(message)s' if logfile: logging.basicConfig(filename=logfile, level=loglevel, format=format) else: logging.basicConfig(level=loglevel, format=format) def init(): """ Initializes VMController Host package. First parses command line options. Then, creates config object from default cfg file. Re-initializes config object if a config file is supplied and sets logger configuration. Finally, uses dependency injection to bind objects to names. """ parser = OptionParser() parser.add_option("-c", "--config", dest="configfile", help="Read configuration from FILE. (Overrides default config file.)", metavar="FILE") parser.add_option("-a", "--host", dest="xmlrpc_host", help="Listen on specified address for XMLRPC interface (default 127.0.0.1)", metavar="ADDR") parser.add_option("-p", "--port", dest="xmlrpc_port", help="Listen on specified port for XMLRPC interface (default 50505)", type="int", metavar="PORT") parser.add_option("-l", "--logfile", dest="logfile", help="Log to specified file.", metavar="FILE") parser.add_option("--debug", action="store_true", dest="debug", default=False, help="Sets logging to debug (unless logging configured in config file).") (options, args) = parser.parse_args() config = init_config() injector = inject.Injector() inject.register(injector) injector.bind('config', to=config) injector.bind('stompEngine', to=HostStompEngine, scope=inject.appscope) injector.bind('words', to=HostWords.getWords) injector.bind('stompProtocol', to=StompProtocol, scope=inject.appscope) injector.bind('subject', to=Host) injector.bind('hvController', to=HyperVisorController) init_config_file(options.configfile) if options.xmlrpc_host is not None: config.set('xmlrpc', 'host', options.xmlrpc_host) if options.xmlrpc_port is not None: config.set('xmlrpc', 'port', str(options.xmlrpc_port)) level = logging.DEBUG if options.debug else logging.INFO init_logging(logfile=options.logfile, loglevel=level) #debug_config(config) def start_coilmq(config, server_event, tries=-1, delay=1, backoff=1.5): """ Starts CoilMQ broker. @param config: Config for CoilMQ. @param server_event: Event attached to multiprocessing manager. @param tries: Maximum retries to start the server. Default -1 (infinite). @param delay: Time to wait before next try to start broker. Default 1. @param backoff: Factor to set delay. Default 1.5. """ m_tries = tries m_delay = delay m_server = None try: from coilmq.config import config as broker_config import coilmq.start except ImportError, e: print "Import error: %s\nPlease check." % e exit() if config.has_section('broker'): for (attribute, value) in config.items('broker'): if attribute != 'name': broker_config.set('coilmq', attribute, value) logger.debug("[coilmq] %s = %s" % (attribute, value)) broker_server = None while True: try: broker_server = coilmq.start.server_from_config(broker_config) logger.info("Stomp server listening on %s:%s" % broker_server.server_address) server_event.set() broker_server.serve_forever() except (KeyboardInterrupt, SystemExit): logger.info
("Stomp server stopped by user interrupt.") raise SystemExit() except IOError as ex: logger.error("Exception while starting coilmq broker: '%s'", ex) if m_tries != 0: logger.debug("Retrying coilmq startup i
n %.1f seconds...", m_delay) time.sleep(m_delay) m_delay *= backoff m_tries -= 1 else: logger.debug("Ran out of trials (tried %d times) for coilmq startup. Giving up.", tries) break except Exception, e: logger.error("Stomp server stopped due to error: %s" % e) logger.exception(e) raise SystemExit() finally: if broker_server: broker_server.server_close() @inject.param('config') def init_coilmq(config, brokerTimeout=60): """ Intializes and starts CoilMQ stomp broker as a light weight (multiprocessing) process. @param config: Injected config object. @param brokerTimeout: Timeout to check is broker is running. Default 60s. """ manager = multiprocessing.Manager() server_event = manager.Event() broker = multiprocessing.Process(target=start_coilmq, args=(config, server_event)) broker.daemon = False broker.name = 'VMController-Broker' broker.start() server_event.wait(brokerTimeout) if not server_event.is_set(): logger.fatal("Broker not available after %.1f seconds. Giving up", brokerTimeout) return -1 @inject.param('config') def init_morbid(config): """ Starts up light weight, twisted based MorbidQ stomp broker. @param config: Injected config object. """ try: import morbid except ImportError, e: import sys print "Import error: %s\nPlease check." % e sys.exit() morbid_factory = morbid.StompFactory(verbose=True) broker_host = config.get('broker', 'host') broker_port = int(config.get('broker', 'port')) try: reactor.listenTCP(broker_port, morbid_factory, interface=broker_host) except: logger.fatal("Unable to start Morbid, port may not be free. Exiting.") import sys sys.exit() logger.info("Starting MorbidQ broker %s:%s", broker_host, broker_port) @inject.param('config') def start(config): """ Starts VMController Host. @param config: The injected config object. """ broker_name = config.get('broker', 'name') if broker_name == 'morbid': init_morbid() elif broker_name == 'coilmq': init_coilmq() else: logger.fatal("No broker found... Exiting") exit() stompProtocolFactory = StompProtocolFactory() xmlrpcService = HostXMLRPCService() xmlrpcService.makeEngineAccesible() host = config.get('broker', 'host') port = int(config.get('broker', 'port')) reactor.connectTCP(host, port, stompProtocolFactory) reactor.run() def main(): """ Initializes and starts VMController Host. """ init() logger.info("Welcome to VMController Host!") start() if __name__ == '__main__': try: main() except (KeyboardInterrupt, SystemExit): pass except Exception, e: logger.error("Server terminated due to error: %s" % e) logger.exception(e)
moertle/pyaas
pyaas/io.py
Python
mit
1,515
0.009241
import sys out = sys.stdout class Colors: def black (self, fmt='', *args): self('\x1b[1;30m'+fmt+'\x1b[0m', *args) def red (self, fmt='', *args): self('\x1b[1;31m'+fmt+'\x1b[0m', *args) def green (self, fmt='', *args): self('\x1b[1;32m'+fmt+'\x1b[0m', *args) def yellow (self, fmt='', *args): self('\x1b[1;33m'+fmt+'\x1b[0m', *args) def blue (self,
fmt='', *args): self('\x1b[1;34m'+fmt+'\x1b[0m', *args) def purple (self, fmt='', *args): self('\x1b[1;35m'+fmt+'\x1b[0m', *args) def cyan (self, fmt='', *args): self('\x1b[1;36m'+fmt+'\x1b[0m', *args) def white (self, fmt='', *args): self('\x1b[1;37m'+fmt+'\x1b[0m', *args) class PrintF(Colors): def __call__(self, fmt='', *args): out.write(fmt % args)
out.flush() class WriteLine(Colors): def __call__(self, fmt='', *args): out.write(fmt % args) out.write('\n') def hexdump(blob, width=16, offset=0): fmt = '%%.%dx: ' % len('%.x' % (len(blob) - 1)) while blob: line = blob[:width] blob = blob[width:] printf.white(fmt, offset) printf.cyan(' '.join('%.2x' % ord(c) for c in line)) printf(' ' * ((width-len(line))*3+1)) for c in line: if ord(c) < 32 or ord(c) > 126: printf.black('.') else: printf.white('%c', c) writeln() offset += width __builtins__['printf'] = PrintF() __builtins__['writeln'] = WriteLine() __builtins__['hexdump'] = hexdump
bcrochet/eve
eve/methods/get.py
Python
bsd-3-clause
21,656
0
# -*- coding: utf-8 -*- """ eve.methods.get ~~~~~~~~~~~~~~~ This module implements the API 'GET' methods, supported by both the resources and single item endpoints. :copyright: (c) 2017 by Nicola Iarocci. :license: BSD, see LICENSE for more details. """ import math import copy import json from flask import current_app as app, abort, request from werkzeug import MultiDict from .common import ratelimit, epoch, pre_event, resolve_embedded_fields, \ build_response_document, resource_link, document_link, last_updated from eve.auth import requires_auth from eve.utils import parse_request, home_link, querydef, config from eve.versioning import synthesize_versioned_document, versioned_id_field, \ get_old_document, diff_document @ratelimit() @requires_auth('resource') @pre_event def get(resource, **lookup): """ Default function for handling GET requests, it has decorators for rate limiting, authentication and for raising pre-request events. After the decorators are applied forwards to call to :func:`get_internal` .. versionadded:: 0.6.2 """ return get_internal(resource, **lookup) def get_internal(resource, **lookup): """ Retrieves the resource documents that match the current request. :param resource: the name of the resource. .. versionchanged:: 0.6 Support for HEADER_TOTAL_COUNT returned with response header. .. versionchanged:: 0.5 Support for customisable query parameters. .. versionchanged:: 0.4 Add pagination info whatever the HATEOAS status. 'on_fetched' events now return the whole response (HATEOAS metafields included.) Replaced ID_FIELD by item_lookup_field on self link. item_lookup_field will default to ID_FIELD if blank. Changed ``on_fetch_*`` changed to ``on_fetched_*``. .. versionchanged:: 0.3 Don't return 304 if resource is empty. Fixes #243. Support for media fields. When IF_MATCH is disabled, no etag is included in the payload. When If-Modified-Since header is present, either no documents (304) or all documents (200) are sent per the HTTP spec. Original behavior can be achieved with: /resource?where={"updated":{"$gt":"if-modified-since-date"}} .. versionchanged:: 0.2 Use the new ITEMS configuration setting. Raise 'on_pre_<method>' event. Let cursor add extra info to response. .. versionchanged:: 0.1.0 Support for optional HATEOAS. Support for embeddable documents. .. versionchanged:: 0.0.9 Event hooks renamed to be more robuts and consistent: 'on_getting' renamed to 'on_fetch'. .. versionchanged:: 0.0.8 'on_getting' and 'on_getting_<resource>' events are raised when documents have been read from the database and are about to be sent to the client. .. versionchanged:: 0.0.6 Support for HEAD requests. .. versionchanged:: 0.0.5 Support for us
er-restricted access to resources. Support for LAST_UPDATED field missing from documents, because they were created outside the API context. .. versionchanged:: 0.0.4 Added the ``requires_auth`` decorator. .. versionchanged:: 0.0.3 Superflous ``response`` container removed. Collection items wrapped with ``_items``. Links wrapped with ``_links``. Link
s are now properly JSON formatted. """ datasource = config.DOMAIN[resource]['datasource'] aggregation = datasource.get('aggregation') if aggregation: return _perform_aggregation(resource, aggregation['pipeline'], aggregation['options']) else: return _perform_find(resource, lookup) def _perform_aggregation(resource, pipeline, options): """ .. versionadded:: 0.7 """ # TODO move most of this down to the Mongo layer? # TODO experiment with cursor.batch_size as alternative pagination # implementation def parse_aggregation_stage(d, key, value): for st_key, st_value in d.items(): if isinstance(st_value, dict): parse_aggregation_stage(st_value, key, value) if key == st_value: d[st_key] = value response = {} documents = [] req = parse_request(resource) req_pipeline = copy.deepcopy(pipeline) if req.aggregation: try: query = json.loads(req.aggregation) except ValueError: abort(400, description='Aggregation query could not be parsed.') for key, value in query.items(): if key[0] != '$': pass for stage in req_pipeline: parse_aggregation_stage(stage, key, value) if req.max_results > 1: limit = {"$limit": req.max_results} skip = {"$skip": (req.page - 1) * req.max_results} req_pipeline.append(skip) req_pipeline.append(limit) cursor = app.data.aggregate(resource, req_pipeline, options) for document in cursor: documents.append(document) response[config.ITEMS] = documents # PyMongo's CommandCursor does not return a count, so we cannot # provide paination/total count info as we do with a normal (non-aggregate) # GET request. return response, None, None, 200, [] def _perform_find(resource, lookup): """ .. versionadded:: 0.7 """ documents = [] response = {} etag = None req = parse_request(resource) embedded_fields = resolve_embedded_fields(resource, req) # continue processing the full request last_update = epoch() # If-Modified-Since disabled on collections (#334) req.if_modified_since = None cursor = app.data.find(resource, req, lookup) # If soft delete is enabled, data.find will not include items marked # deleted unless req.show_deleted is True for document in cursor: build_response_document(document, resource, embedded_fields) documents.append(document) # build last update for entire response if document[config.LAST_UPDATED] > last_update: last_update = document[config.LAST_UPDATED] status = 200 headers = [] last_modified = last_update if last_update > epoch() else None response[config.ITEMS] = documents if config.OPTIMIZE_PAGINATION_FOR_SPEED: count = None else: count = cursor.count(with_limit_and_skip=False) headers.append((config.HEADER_TOTAL_COUNT, count)) if config.DOMAIN[resource]['hateoas']: response[config.LINKS] = _pagination_links(resource, req, count) # add pagination info if config.DOMAIN[resource]['pagination']: response[config.META] = _meta_links(req, count) # notify registered callback functions. Please note that, should the # functions modify the documents, the last_modified and etag won't be # updated to reflect the changes (they always reflect the documents # state on the database.) getattr(app, "on_fetched_resource")(resource, response) getattr(app, "on_fetched_resource_%s" % resource)(response) # the 'extra' cursor field, if present, will be added to the response. # Can be used by Eve extensions to add extra, custom data to any # response. if hasattr(cursor, 'extra'): getattr(cursor, 'extra')(response) return response, last_modified, etag, status, headers @ratelimit() @requires_auth('item') @pre_event def getitem(resource, **lookup): """ Default function for handling GET requests to document endpoints, it has decorators for rate limiting, authentication and for raising pre-request events. After the decorators are applied forwards to call to :func:`getitem_internal` .. versionadded:: 0.6.2 """ return getitem_internal(resource, **lookup) def getitem_internal(resource, **lookup): """ :param resource: the name of the resource to which the document belongs. :param **lookup: the lookup query. .. versionchanged:: 0.6 Handle soft deleted documents .. versionchanged:: 0.5 Allow ``?version=all`` r
alabarga/wos-scrapy
webofknowledge/pipelines.py
Python
gpl-2.0
294
0
# -*- coding: utf-8 -*- # Define your item pipelines here # #
Don't forget to add your pipeline to the ITEM_PIPELINES setting # See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html class WebofknowledgePipeline(object): def proces
s_item(self, item, spider): return item
drwyrm/Flexget
flexget/plugins/sites/rss.py
Python
mit
1,942
0.00309
from __future__ import unicode_literals, division, absolute_import from builtins import * # noqa pylint: disable=unused-import, redefined-builtin from future.moves.urllib.parse import quote import logging from jinja2 import TemplateSyntaxError from flexget import plugin from flexget.event import event from flexget.utils.search import normalize_unicode log = logging.getLogger('search_rss') class SearchRSS(
object): """A generic search plugin that can use rss based search feeds. Configure it like rss plugin, but include {{{search_term}}} in the url where the search term should go.""" schema = {'$ref': '/schema/plugin/rss'} def search(self, task, entry, config=None): from flexget.utils.template import environment search_strings = [quote(normalize_unicode(s).encode('utf-8')) for s in entry.get('s
earch_strings', [entry['title']])] rss_plugin = plugin.get_plugin_by_name('rss') entries = set() rss_config = rss_plugin.instance.build_config(config) try: template = environment.from_string(rss_config['url']) except TemplateSyntaxError as e: raise plugin.PluginError('Invalid jinja template as rss url: %s' % e) rss_config['all_entries'] = True for search_string in search_strings: rss_config['url'] = template.render({'search_term': search_string}) # TODO: capture some other_fields to try to find seed/peer/content_size numbers? try: results = rss_plugin.phase_handlers['input'](task, rss_config) except plugin.PluginError as e: log.error('Error attempting to get rss for %s: %s', rss_config['url'], e) else: entries.update(results) return entries @event('plugin.register') def register_plugin(): plugin.register(SearchRSS, 'search_rss', groups=['search'], api_ver=2)
mbrner/funfolding
funfolding/visualization/__init__.py
Python
mit
272
0
from . import visualize_classic_binning from . import visualize_tree_binning from . import visualize_llh from . import visualize_model __all__ = ('visualize_
classic_binning', 'visualize_llh', 'visualize_tr
ee_binning', 'visualize_model')
InakiZabala/odoomrp-wip
product_pricelist_import/__init__.py
Python
agpl-3.0
966
0
# -*- encoding: utf-8 -*- ##############################################################
################ # # Daniel Campos ([email protected]) Date: 07/1
0/2014 # # This program 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. # # 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 . import models from . import wizard
erdewit/tws_async
tws_async/twsclientqt.py
Python
unlicense
5,611
0.000535
import sys import struct import logging import ibapi from ibapi.client import EClient from ibapi.wrapper import EWrapper, iswrapper import PyQt5.Qt as qt import PyQt5.QtNetwork as qtnetwork import tws_async.util as util util.allowCtrlC() __all__ = ['TWSClientQt', 'iswrapper'] class TWSClientQt(EWrapper, EClient): """ Version of ibapi.client.EClient that integrates with the Qt event loop. """ def __init__(self): EClient.__init__(self, wrapper=self) self.qApp = qt.QApplication.instance() or qt.QApplication(sys.argv) self.readyTrigger = Trigger() self._logger = logging.getLogger(__class__.__name__) def reset(self): EClient.reset(self) self._data = b'' self._reqIdSeq = 0 def run(self): self.qApp.exec_() def connect(self, host, port, clientId, asyncConnect=False): self._logger.info('Connecting to {}:{} with clientId {}...'. format(host, port, clientId)) self.host = host self.port = port self.clientId = clientId self.conn = TWSConnection(host, port) self.conn.connect() self.conn.socket.connected.connect(self._onSocketConnected) self.conn.socket.disconnected.connect(self._onSocketDisonnected) self.conn.socket.readyRead.connect(self._onSocketReadyRead) self.conn.socket.error.connect(self._onSocketError) self.setConnState(EClient.CONNECTING) if not asyncConnect: self.readyTrigger.wait() def getReqId(self) -> int: """ Get new request ID. """ assert self._reqIdSeq newId = self._reqIdSeq self._reqIdSeq += 1 return newId def dataHandlingPre(self): pass def dataHandlingPost(self): pass def _prefix(self, msg): # prefix a message with its length return struct.pack('>I', len(msg)) + msg def _onSocketConnected(self): # start handshake msg = b'API\0' msg += self._prefix(b'v%d..%d' % ( ibapi.server_versions.MIN_CLIENT_VER, ibapi.server_versions.MAX_CLIENT_VER)) self.conn.sendMsg(msg) self.decoder = ibapi.decoder.Decoder(self.wrapper, None) def _onSocketDisonnected(self): EClient.disconnect(self) def _onSocketError(self, socketError): if self.conn.socket: self._logger.error(self.conn.socket.errorString()) def _onSocketReadyRead(self): self.dataHandlingPre() self._data += bytes(self.conn.socket.readAll()) while True: if len(self._data) <= 4: break # 4 byte prefix tells the message length msgEnd = 4 + struct.unpack('>I', self._data[:4])[0] if len(self._data) < msgEnd: # insufficient data for now break msg = self._data[4:msgEnd] self._data = s
elf._data[msgEnd:] fields = msg.split(b'\0') fields.pop() # pop off last empty element if not self.serverVersion_ and len(fields) == 2:
# this concludes the handshake version, self.connTime = fields self.serverVersion_ = int(version) self.decoder.serverVersion = self.serverVersion_ self.setConnState(EClient.CONNECTED) self.startApi() self.wrapper.connectAck() self._logger.info('Logged on to server version {}'. format(self.serverVersion_)) else: # snoop for next valid id response, # it signals readiness of the client if fields[0] == b'9': _, _, validId = fields self._reqIdSeq = int(validId) self.readyTrigger.go() # decode and handle the message self.decoder.interpret(fields) self.dataHandlingPost() class TWSConnection: """ Replacement for ibapi.connection.Connection that uses a QTcpSocket. """ def __init__(self, host, port): self.host = host self.port = port self.socket = None def connect(self): self.socket = qtnetwork.QTcpSocket() # set TCP_NODELAY (disable Nagle's algorithm) self.socket.setSocketOption( qtnetwork.QAbstractSocket.LowDelayOption, True) self.socket.connectToHost(self.host, self.port) def disconnect(self): self.socket.close() self.socket = None def isConnected(self): return self.socket is not None def sendMsg(self, msg): self.socket.write(msg) self.socket.flush() class Trigger(qt.QObject): """ Wait synchronously on a trigger. """ trigger = qt.pyqtSignal() def __init__(self): qt.QObject.__init__(self) def go(self): self.trigger.emit() def wait(self, timeout=5000): spy = qt.QSignalSpy(self.trigger) spy.wait(timeout) class TWS_TestQt(TWSClientQt): """ Test to connect to a running TWS or gateway server. """ def __init__(self): TWSClientQt.__init__(self) @iswrapper def updateAccountValue(self, key: str, val: str, currency: str, accountName: str): print('Account update: {} = {} {}'.format(key, val, currency)) if __name__ == '__main__': util.logToConsole() tws = TWS_TestQt() tws.connect(host='127.0.0.1', port=7497, clientId=1) tws.reqAccountUpdates(1, '') tws.run()
lordmos/blink
Tools/Scripts/webkitpy/layout_tests/port/test.py
Python
mit
29,134
0.002643
# Copyright (C) 2010 Google Inc. All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above # copyright notice, this list of conditions and the following disclaimer # in the documentation and/or other materials provided with the # distribution. # * Neither the Google name nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "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 THE COPYRIGHT # OWNER OR CONTRIBUTORS 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. import base64 import copy import sys import time from webkitpy.layout_tests.port import DeviceFailure, Driver, DriverOutput, Port from webkitpy.layout_tests.port.base import VirtualTestSuite from webkitpy.layout_tests.models.test_configuration import TestConfiguration from webkitpy.layout_tests.models import test_run_results from webkitpy.common.system.filesystem_mock import MockFileSystem from webkitpy.common.system.crashlogs import CrashLogs # This sets basic expectations for a test. Each individual expectation # can be overridden by a keyword argument in TestList.add(). class TestInstance(object): def __init__(self, name): self.name = name self.base = name[(name.rfind("/") + 1):name.rfind(".")] self.crash = False self.web_process_crash = False self.exception = False self.keyboard = False self.error = '' self.timeout = False self.is_reftest = False self.device_failure = False # The values of each field are treated as raw byte strings. They # will be converted to unicode strings where appropriate using # FileSystem.read_text_file(). self.actual_text = self.base + '-txt' self.actual_checksum = self.base + '-checksum' # We add the '\x8a' for the image file to prevent the value from # being treated as UTF-8 (the character is invalid) self.actual_image = self.base + '\x8a' + '-png' + 'tEXtchecksum\x00' + self.actual_checksum self.expected_text = self.actual_text self.expected_image = self.actual_image self.actual_audio = None self.expected_audio = None # This is an in-memory list of tests, what we want them to produce, and # what we want to claim are the expected results. class TestList(object): def __init__(self): self.tests = {} def add(self, name, **kwargs): test = TestInstance(name) for key, value in kwargs.items(): test.__dict__[key] = value self.tests[name] = test def add_reftest(self, name, reference_name, same_image, crash=False): self.add(name, actual_checksum='xxx', actual_image='XXX', is_reftest=True, crash=crash) if same_image: self.add(reference_name, actual_checksum='xxx', actual_image='XXX', is_reftest=True) else: self.add(reference_name, actual_checksum='yyy', actual_ima
ge='YYY', is_reftest=True) def keys(self): return self.tests.keys() def __contains__(self, item): return item in self.tests def __getitem__(sel
f, item): return self.tests[item] # # These numbers may need to be updated whenever we add or delete tests. This includes virtual tests. # TOTAL_TESTS = 114 TOTAL_SKIPS = 29 UNEXPECTED_PASSES = 1 UNEXPECTED_FAILURES = 25 def unit_test_list(): tests = TestList() tests.add('failures/expected/crash.html', crash=True) tests.add('failures/expected/exception.html', exception=True) tests.add('failures/expected/device_failure.html', device_failure=True) tests.add('failures/expected/timeout.html', timeout=True) tests.add('failures/expected/missing_text.html', expected_text=None) tests.add('failures/expected/needsrebaseline.html', actual_text='needsrebaseline text') tests.add('failures/expected/needsmanualrebaseline.html', actual_text='needsmanualrebaseline text') tests.add('failures/expected/image.html', actual_image='image_fail-pngtEXtchecksum\x00checksum_fail', expected_image='image-pngtEXtchecksum\x00checksum-png') tests.add('failures/expected/image_checksum.html', actual_checksum='image_checksum_fail-checksum', actual_image='image_checksum_fail-png') tests.add('failures/expected/audio.html', actual_audio=base64.b64encode('audio_fail-wav'), expected_audio='audio-wav', actual_text=None, expected_text=None, actual_image=None, expected_image=None, actual_checksum=None) tests.add('failures/expected/keyboard.html', keyboard=True) tests.add('failures/expected/missing_check.html', expected_image='missing_check-png') tests.add('failures/expected/missing_image.html', expected_image=None) tests.add('failures/expected/missing_audio.html', expected_audio=None, actual_text=None, expected_text=None, actual_image=None, expected_image=None, actual_checksum=None) tests.add('failures/expected/missing_text.html', expected_text=None) tests.add('failures/expected/newlines_leading.html', expected_text="\nfoo\n", actual_text="foo\n") tests.add('failures/expected/newlines_trailing.html', expected_text="foo\n\n", actual_text="foo\n") tests.add('failures/expected/newlines_with_excess_CR.html', expected_text="foo\r\r\r\n", actual_text="foo\n") tests.add('failures/expected/testharness.html', actual_text='This is a testharness.js-based test.\nFAIL: assert fired\n.Harness: the test ran to completion.\n\n', expected_text=None, actual_image=None, expected_image=None, actual_checksum=None) tests.add('failures/expected/text.html', actual_text='text_fail-png') tests.add('failures/expected/crash_then_text.html') tests.add('failures/expected/skip_text.html', actual_text='text diff') tests.add('failures/flaky/text.html') tests.add('failures/unexpected/missing_text.html', expected_text=None) tests.add('failures/unexpected/missing_check.html', expected_image='missing-check-png') tests.add('failures/unexpected/missing_image.html', expected_image=None) tests.add('failures/unexpected/missing_render_tree_dump.html', actual_text="""layer at (0,0) size 800x600 RenderView at (0,0) size 800x600 layer at (0,0) size 800x34 RenderBlock {HTML} at (0,0) size 800x34 RenderBody {BODY} at (8,8) size 784x18 RenderText {#text} at (0,0) size 133x18 text run at (0,0) width 133: "This is an image test!" """, expected_text=None) tests.add('failures/unexpected/crash.html', crash=True) tests.add('failures/unexpected/crash-with-stderr.html', crash=True, error="mock-std-error-output") tests.add('failures/unexpected/web-process-crash-with-stderr.html', web_process_crash=True, error="mock-std-error-output") tests.add('failures/unexpected/pass.html') tests.add('failures/unexpected/text-checksum.html', actual_text='text-checksum_fail-txt', actual_checksum='text-checksum_fail-checksum') tests.add('failur
europa502/shARP_2.0
mac_decoder.py
Python
gpl-3.0
1,454
0.055021
import socket, sys,os,re from struct import * mymac=sys.argv[1] rmac=sys.argv[2] interface=sys.argv[3] mode=sys.argv[4] def address (a) : b = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x" % (ord(a[0]) , ord(a[1]) , ord(a[2]), ord(a[3]), ord(a[4]) , ord(a[5])) return b try: s = socket.socket( socket.AF_PACKET , socket.SOCK_RAW , socket.ntohs(0x0003)) except socket.error , msg: print 'Socket could not be created. Error Code : ' + str(msg[0]) + ' Message ' + msg[1] sys.exit() while True: packet = s.recvfrom(65565) packet = packet[0] pack_length = 14 pack_header = packet[:pack_length] pack = unpack('!6s6sH' , pack_header) pack_protocol = socket.ntohs(pack[2]) #print 'Destination MAC : ' + address(packet[0:6]) + ' Source MAC : ' + address(packet[6:12]) #print rmac, interface , mode router_mac=re.sub(r':',"",rmac) pc_mac=re.sub(r':',"",mymac) router_mac= router_mac[:-6] if mymac == address(packet[0:6]) : if rmac != address(packet[6:12]) and rmac != "01005e" and rmac != "ffffff"
and rmac != "333300": os.system("bash ./passive.sh '"+rmac+"' '"+interface+"' '"+mode+"' ") elif mymac == address(packet[6:12]) : if rmac
!= address(packet[0:6]) and rmac != "01005e" and rmac != "ffffff" and rmac != "333300": os.system("bash ./passive.sh '"+rmac+"' '"+interface+"' '"+mode+"' ")
hsw5138/bis
backend/helpers.py
Python
mit
2,048
0.027344
import uuid import base64 import re def generate_key(): """ generates a uuid, encodes it with base32 and strips it's padding. this reduces the string size from 32 to 26 chars. """ return base64.b32encode(uuid.uuid4().bytes).strip('=').lower()[0:12] def thousand_separator(x=0, sep='.', dot=','): """ creates a string of number separated by selected delimiters """ num, _, frac = str(x).partition(dot) num = re.sub(r'(\d{3})(?=\d)', r'\1'+sep, num[::-1])[::-1] if frac: num += dot + frac return num def new_parser(passed_object, request_data): """ Maps passed request object from client into expected object. Use this for creation of new object by passing an instantiated empty object into the passed_object variable """ for item, value in request_data.values.iteritems(): if hasattr(passed_object, item) and value is not None: try: setattr(passed_object, item, value) except: setattr(passed_object, item, convert_to_date(value)) passed_object.id = generate_key() return passed_object def edit_parser(passed_object, request_data): """ Maps value from passed json object for data edit purposes. You need to pass in object resulting from query into the passed_object variable """ for item in request_data.values: if item != "id" and hasattr(passed_object, item) and request_data.values.get(item) != None: setattr(passed_object, item, request_data.values.get(item)) return passed_object def convert_to_date(date_string): from datetime import date input = date_string.split("-") return date(int(input[0]),int(input[1]),int(input[2])) def multikeysort(items, columns): from operator import itemgetter comparers = [ ((itemgetter(col[1:].strip()), -1) if col.startswith('-') e
lse (itemgetter(col.strip()), 1)) for col in columns] def comparer(left, right): for fn, mult in comparers: result = cmp(fn(le
ft), fn(right)) if result: return mult * result else: return 0 return sorted(items, cmp=comparer)
Anthirian/script.filecleaner
default.py
Python
gpl-3.0
30,945
0.004007
#!/usr/bin/python # -*- coding: utf-8 -*- import json import sys from reset_exclusions import * from utils import * from viewer import * class Cleaner(object): """ The Cleaner class allows users to clean up their movie, TV show and music video collection by removing watched items. The user can apply a number of conditions to cleaning, such as limiting cleaning to files with a given rating, excluding a particular folder or only cleaning when a particular disk is low on disk space. The main method to call is the ``clean_all()`` method. This method will invoke the subsequent checks and (re)move your videos. Upon completion, you will receive a short summary of the cleaning results. *Example* ``summary = Cleaner().clean_all()`` """ # Constants to ensure correct (Gotham-compatible) JSON-RPC requests for Kodi MOVIES = u"movies" MUSIC_VIDEOS = u"musicvideos" TVSHOWS = u"episodes" CLEANING_TYPE_MOVE = u"0" CLEANING_TYPE_DELETE = u"1" DEFAULT_ACTION_CLEAN = u"0" DEFAULT_ACTION_LOG = u"1" STATUS_SUCCESS = 1 STATUS_FAILURE = 2 STATUS_ABORTED = 3 movie_filter_fields = [u"title", u"plot", u"plotoutline", u"tagline", u"votes", u"rating", u"time", u"writers", u"playcount", u"lastplayed", u"inprogress", u"genre", u"country", u"year", u"director", u"actor", u"mpaarating", u"top250", u"studio", u"hastrailer", u"filename", u"path", u"set", u"tag", u"dateadded", u"videoresolution", u"audiochannels", u"videocodec", u"audiocodec", u"audiolanguage", u"subtitlelanguage", u"videoaspect", u"playlist"] episode_filter_fields = [u"title", u"tvshow", u"plot", u"votes", u"rating", u"time", u"writers", u"airdate", u"playcount", u"lastplayed", u"inprogress", u"genre", u"year", u"director", u"actor", u"episode", u"season", u"filename", u"path", u"studio", u"mpaarating", u"dateadded", u"videoresolution", u"audiochannels", u"videocodec", u"audiocodec", u"audiolanguage", u"subtitlelanguage", u"videoaspect", u"playlist"] musicvideo_filter_fields = [u"title", u"genre", u"album", u"year", u"artist", u"filename", u"path", u"playcount", u"lastplayed", u"time", u"director", u"studio", u"plot", u"dateadded", u"videoresolution", u"audiochannels", u"videocodec", u"audioco
dec", u"audiolanguage", u"subtitlelanguage", u"videoaspect", u"playlist"] supported_filter_field
s = { TVSHOWS: episode_filter_fields, MOVIES: movie_filter_fields, MUSIC_VIDEOS: musicvideo_filter_fields } methods = { TVSHOWS: u"VideoLibrary.GetEpisodes", MOVIES: u"VideoLibrary.GetMovies", MUSIC_VIDEOS: u"VideoLibrary.GetMusicVideos" } properties = { TVSHOWS: [u"file", u"showtitle"], MOVIES: [u"file", u"title"], MUSIC_VIDEOS: [u"file", u"artist"] } stacking_indicators = [u"part", u"pt", u"cd", u"dvd", u"disk", u"disc"] progress = xbmcgui.DialogProgress() monitor = xbmc.Monitor() silent = True exit_status = STATUS_SUCCESS def __init__(self): debug(u"{0} version {1} loaded.".format(ADDON.getAddonInfo(u"name").decode("utf-8"), ADDON.getAddonInfo(u"version").decode("utf-8"))) def __is_canceled(self): """ Test if the progress dialog has been canceled by the user. If the cleaner was started as a service this will always return False :rtype: bool :return: True if the user cancelled cleaning, False otherwise. """ if self.silent: return False elif self.progress.iscanceled(): debug(u"User canceled.", xbmc.LOGWARNING) self.exit_status = self.STATUS_ABORTED return True def show_progress(self): """ Toggle the progress dialog on. Use before calling the cleaning method. """ self.silent = False def hide_progress(self): """ Toggle the progress dialog off. Use before calling the cleaning method. """ self.silent = True def clean(self, video_type): """ Clean all watched videos of the provided type. :type video_type: unicode :param video_type: The type of videos to clean (one of TVSHOWS, MOVIES, MUSIC_VIDEOS). :rtype: (list, int, int) :return: A list of the filenames that were cleaned, as well as the number of files cleaned and the return status. """ cleaned_files = [] count = 0 type_translation = {self.MOVIES: translate(32626), self.MUSIC_VIDEOS: translate(32627), self.TVSHOWS: translate(32628)} if not self.silent: # Cleaning <video type> self.progress.update(0, translate(32629).format(type=type_translation[video_type]), *map(translate, (32615, 32615))) self.monitor.waitForAbort(1) if video_type == self.TVSHOWS: clean_this_video_type = get_setting(clean_tv_shows) elif video_type == self.MOVIES: clean_this_video_type = get_setting(clean_movies) elif video_type == self.MUSIC_VIDEOS: clean_this_video_type = get_setting(clean_music_videos) else: debug(u"Incorrect video type specified: {0}".format(video_type), xbmc.LOGERROR) return [], 0, self.STATUS_FAILURE progress_percent = 0 if clean_this_video_type: expired_videos = self.get_expired_videos(video_type) if not self.silent: amount = len(expired_videos) debug(u"Found {0} videos that may need cleaning.".format(amount)) try: increment = 1.0 / amount except ZeroDivisionError: self.progress.update(0, *map(translate, (32621, 32622, 32623))) # No watched videos found if self.monitor.waitForAbort(2.5): pass for filename, title in expired_videos: if not self.__is_canceled(): unstacked_path = self.unstack(filename) if xbmcvfs.exists(unstacked_path[0]) and self.has_no_hard_links(filename): if get_setting(cleaning_type) == self.CLEANING_TYPE_MOVE: # No destination set, prompt user to set one now if get_setting(holding_folder) == "": if xbmcgui.Dialog().yesno(ADDON_NAME, *map(translate, (32521, 32522, 32523))): xbmc.executebuiltin(u"Addon.OpenSettings({0})".format(ADDON_ID)) self.exit_status = self.STATUS_ABORTED break if get_setting(create_subdirs): new_path = os.path.join(get_setting(holding_folder).encode("utf-8"), title.encode("utf-8")) else: new_path = get_setting(holding_folder) move_result = self.move_file(filename, new_path) if move_result == 1: debug(u"File(s) moved successfully.") count += 1 if len(unstacked_path) > 1: cleaned_files.extend(unstacked_path) else: cleaned_files.append(filename) self.clean_related_files(filename, new_path) self.delete_empty_folders(os.path.dirname(filename)) elif move_result == -1: debug(u"Moving errors occurred. Skipping related files and directo
sbhowmik7/PSSEcompare
ext_libs/openpyxl/writer/strings.py
Python
gpl-3.0
3,077
0.0013
# file openpyxl/writer/strings.py # Copyright (c) 2010-2011 openpyxl # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS I
S", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN A
CTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. # # @license: http://www.opensource.org/licenses/mit-license.php # @author: see AUTHORS file """Write the shared string table.""" # Python stdlib imports try: # Python 2 from StringIO import StringIO BytesIO = StringIO except ImportError: # Python 3 from io import BytesIO, StringIO # package imports from openpyxl.shared.xmltools import start_tag, end_tag, tag, XMLGenerator def create_string_table(workbook): """Compile the string table for a workbook.""" strings = set() for sheet in workbook.worksheets: for cell in sheet.get_cell_collection(): if cell.data_type == cell.TYPE_STRING and cell._value is not None: strings.add(cell.value) return dict((key, i) for i, key in enumerate(strings)) def write_string_table(string_table): """Write the string table xml.""" temp_buffer = StringIO() doc = XMLGenerator(out=temp_buffer, encoding='utf-8') start_tag(doc, 'sst', {'xmlns': 'http://schemas.openxmlformats.org/spreadsheetml/2006/main', 'uniqueCount': '%d' % len(string_table)}) strings_to_write = sorted(string_table.items(), key=lambda pair: pair[1]) for key in [pair[0] for pair in strings_to_write]: start_tag(doc, 'si') if key.strip() != key: attr = {'xml:space': 'preserve'} else: attr = {} tag(doc, 't', attr, key) end_tag(doc, 'si') end_tag(doc, 'sst') string_table_xml = temp_buffer.getvalue() temp_buffer.close() return string_table_xml class StringTableBuilder(object): def __init__(self): self.counter = 0 self.dct = {} def add(self, key): key = key.strip() try: return self.dct[key] except KeyError: res = self.dct[key] = self.counter self.counter += 1 return res def get_table(self): return self.dct
cocodelabs/api.palaverapp.com
palaverapi/models.py
Python
bsd-3-clause
1,143
0
import os import peewee from rivr_peewee import Database DATABASE_URL = os.environ.get('DATABASE_URL') if DATABASE_URL and DATABASE_URL.startswith('postgres://'): DATABASE_URL = DATABASE_URL.replace('postgres://', 'postgres+pool://') # disable auto connection EXTRA_OPTIONS = 'autoconnect=false' if '?' in DATABASE_URL:
DATABASE_URL += '&' + EXTRA_OPTIONS else: DATABASE_URL += '?' + EXTRA_OPTIONS os.environ['DATABASE_URL'] = DATABASE_URL database = Database() class Device(database.Model): apns_token = peewee.CharFiel
d(max_length=64, unique=True) def __repr__(self) -> str: return '<Device {}>'.format(self.apns_token) class Token(database.Model): PUSH_SCOPE = 'push' ALL_SCOPE = 'all' device = peewee.ForeignKeyField(Device) token = peewee.CharField(max_length=64, unique=True, primary_key=True) scope = peewee.CharField(max_length=10, choices=(PUSH_SCOPE, ALL_SCOPE)) def __repr__(self) -> str: return '<Token {} ({})>'.format(self.token, self.scope) @property def token_last_eight(self) -> str: return self.token[-8:]
vangalamaheshh/snakemake
snakemake/report.py
Python
mit
3,806
0.000263
__author__ = "Johannes Köster" __copyright__ = "Copyright 2015, Johannes Köster" __email__ = "[email protected]" __license__ = "MIT" import os import mimetypes import base64 import textwrap import datetime import io from docutils.parsers.rst.directives.images import Image, Figure from docutils.parsers.rst import directives from docutils.core import publish_file from snakemake.utils import format from snakemake.logging import logger class EmbeddedMixin(object): """ Replaces the URI of a directive with a base64-encoded version. Useful for embedding images/figures in reports. """ def run(self): """ Image.run() handles most of the """ result = Image.run(self) reference = directives.uri(self.arguments[0]) self.options['uri'] = data_uri(reference) return result # Create (and register) new image:: and figure:: directives that use a base64 # data URI instead of pointing to a filename. class EmbeddedImage(Image, EmbeddedMixin): pass directives.register_directive('embeddedimage', EmbeddedImage) class EmbeddedFigure(Figure, EmbeddedMixin): pass directives.register_directive('embeddedfigure', EmbeddedFigure) def data_uri(file, defaultenc="utf8"): """Craft a base64 data URI from file with proper encoding and mimetype.""" mime, encoding = mimetypes.guess_type(file) if mime is None: mime = "text/plain" logger.info("Could not detect mimetype for {}, assuming " "text/plain.".format(file)) if encoding is None: encoding = defaultenc with open(file, "rb") as f: data = base64.b64encode(f.read()) uri = ("data:{mime};charset={charset};filename={filename};base64,{data}" "".format(filename=os.path.basename(file), mime=mime, charset=encoding, data=data.decode())) return uri def report(text, path, stylesheet=os.path.join(os.path.dirname(__file__), "report.css"), defaultenc="utf8", template=None, metadata=None, **files): outmime, _ = mimetypes.guess_type(path) if outmime != "text/html": raise ValueError("Path to report output has to be an HTML file.") definitions = textwrap.dedent(""" .. role:: raw-html(raw) :format: html """) metadata = textwrap.dedent(""" .. container:: :name: metadata {metadata}{date} """).format(metadata=metadata + " | " if metadata else "", date=datetime.date.today().isoformat()) text = format(textwrap.dedent(text), stepout=3) attachments = [textwrap.dedent(""" .. container:: :name: attachments """)] for name, _files in sorted(files.items()): if not isinstance(_files, list): _files = [_file
s] links = [] for file in _files: data = data_uri(file
) links.append(':raw-html:`<a href="{data}" download="{filename}" draggable="true">{filename}</a>`'.format( data=data, filename=os.path.basename(file))) links = "\n\n ".join(links) attachments.append(''' .. container:: :name: {name} {name}: {links} '''.format(name=name, links=links)) text = definitions + text + "\n\n" + "\n\n".join(attachments) + metadata overrides = dict() if template is not None: overrides["template"] = template if stylesheet is not None: overrides["stylesheet_path"] = stylesheet html = open(path, "w") publish_file(source=io.StringIO(text), destination=html, writer_name="html", settings_overrides=overrides)
cloudbase/neutron
neutron/extensions/dns.py
Python
apache-2.0
9,551
0.000419
# Copyright (c) 2015 Rackspace # 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. import re from neutron_lib.api import validators from neutron_lib import exceptions as n_exc from oslo_config import cfg import six from neutron._i18n import _ from neutron.api import extensions from neutron.api.v2 import attributes as attr from neutron.extensions import l3 DNS_LABEL_MAX_LEN = 63 DNS_LABEL_REGEX = "[a-z0-9-]{1,%d}$" % DNS_LABEL_MAX_LEN FQDN_MAX_LEN = 255 DNS_DOMAIN_DEFAULT = 'openstacklocal.' class DNSDomainNotFound(n_exc.NotFound): message = _("Domain %(dns_domain)s not found in the external DNS service") class DuplicateRecordSet(n_exc.Conflict): message = _("Name %(dns_name)s is duplicated in the external DNS service") class ExternalDNSDriverNotFound(n_exc.NotFound): message = _("External DNS driver %(driver)s could not be found.") class InvalidPTRZoneConfiguration(n_exc.Conflict): message = _("Value of %(parameter)s has to be multiple of %(number)s, " "with maximum value of %(maximum)s and minimum value of " "%(minimum)s") def _validate_dns_name(data, max_len=FQDN_MAX_LEN): msg = _validate_dns_format(data, max_len) if msg: return msg request_dns_name = _get_request_dns_name(data) if request_dns_name: msg = _validate_dns_name_with_dns_domain(request_dns_name) if msg: return msg def _validate_fip_dns_name(data, max_len=FQDN_MAX_LEN): msg = validators.validate_string(data) if msg: return msg if not data: return if data.endswith('.'): msg = _("'%s' is a FQDN. It should be a relative domain name") % data return msg msg = _validate_dns_format(data, max_len) if msg: return msg length = len(data) if length > max_len - 3: msg = _("'%(data)s' contains '%(length)s' characters. Adding a " "domain name will cause it to exceed the maximum length " "of a FQDN of '%(max_len)s'") % {"data": data, "length": length, "max_len": max_len} return msg def _validate_dns_domain(data, max_len=FQDN_MAX_LEN): msg = validators.validate_string(data) if msg: return msg if not data: return if not data.endswith('.'): msg = _("'%s' is not a FQDN") % data return msg msg = _validate_dns_format(data, max_len) if msg: return msg length = len(data) if length > max_len - 2: msg = _("'%(data)s' contains '%(length)s' characters. Adding a " "sub-domain will cause it to exceed the maximum length of a " "FQDN of '%(max_len)s'") % {"data": data, "length": length, "max_len": max_len} return msg def _validate_dns_format(data, max_len=FQDN_MAX_LEN): # NOTE: An individual name regex instead of an entire FQDN was used # because its easier to make correct. The logic should validate that the # dns_name matches RFC 1123 (section 2.1) and RFC 952. if not data: return try: # Trailing periods are allowed to indicate that a name is fully # qualified per RFC 1034 (page 7). trimmed = data if not data.endswith('.') else data[:-1] if len(trimmed) > 255: raise TypeError( _("'%s' exceeds the 255 character FQDN limit") % trimmed) names = trimmed.split('.') for name in names: if not name: raise TypeError(_("Encountered an empty component.")) if name.endswith('-') or name[0] == '-': raise TypeError( _("Name '%s' must not start or end with a hyphen.") % name) if not re.match(DNS_LABEL_REGEX, name): raise TypeError( _("Name '%s' must be 1-63 characters long, each of " "which can only be alphanumeric or a hyphen.") % name) # RFC 1123 hints that a TLD can't be all numeric. last is a TLD if # it's an FQDN. if len(names) > 1 and re.match("^[0-9]+$", names[-1]): raise TypeError(_("TLD '%s' must not be all numeric") % names[-1]) except TypeError as e: msg = _("'%(data)s' not a valid PQDN or FQDN. Reason: %(reason)s") % { 'data': data, 'reason': str(e)} return msg def _validate_dns_name_with_dns_domain(request_dns_name): # If a PQDN was passed, make sure the FQDN that will be generated is of # legal size dns_domain = _get_dns_domain() higher_labels = dns_domain if dns_domain: higher_labels = '.%s' % dns_domain higher_labels_len = len(higher_labels) dns_name_len = len(request_dns_name) if not request_dns_name.endswith('.'): if dns_name_len + higher_labels_len > FQDN_MAX_LEN: msg = _("The dns_name passed is a PQDN and its size is " "'%(dns_name_len)s'. The dns_domain option in " "neutron.conf is set to %(dns_domain)s, with a " "length of '%(higher_labels_len)s'. When the two are " "concatenated to form a FQDN (with a '.' at the end), " "the resulting length exceeds the maximum size " "of '%(fqdn_max_len)s'" ) % {'dns_name_len': dns_name_len, 'dns_domain': cfg.CONF.dns_domain, 'higher_labels_len': higher_labels_len, 'fqdn_max_len': FQDN_MAX_LEN} return msg return # A FQDN was passed if (dns_name_len <= higher_labels_len or not request_dns_name.endswith(higher_labels)): msg = _("The dns_name passed is a FQDN. Its higher level labels " "must be equal to the dns_domain option in neutron.conf, " "that has been set to '%(dns_domain)s'. It must also " "include one or more valid DNS labels to the left " "of '%(dns_domain)s'") % {'dns_domain': cfg.CONF.dns_domain} return msg def _get_dns_domain(): if not cfg.CONF.dns_domain: return '' if cfg.CONF.dns_domain.endswith('.'): return cfg.CONF.dns_domain return '%s.' % cfg.CONF.dns_domain def _get_request_dns_name(data): dns_domain = _get_dns_domain() if ((dns_domain and dns_domain != DNS_DOMAIN_DEFAULT)): return data return '' def convert_to_lowercase(data): if isinstance(data, six.string_types): return data.lower() msg = _("'%s' cannot be converted to lowercase string") % data raise n_exc.InvalidInput(error_message=msg) validators.add_validator('dns_name', _validate_dns_name) validators.add_validator('fip_dns_name', _validate_fip_dns_name) validators.add_validator('dns_domain', _validate_dns_domain) DNSNAME = 'dns_name' DNSDOMAIN = 'dns_domain' DNSASSIGNMENT = 'dns_ass
ignment' EXTENDED_ATTRIBUTES_2_0 = { 'ports': { DNSNAME: {'allow_post': True, 'a
llow_put': True, 'default': '', 'convert_to': convert_to_lowercase, 'validate': {'type:dns_name': FQDN_MAX_LEN}, 'is_visible': True}, DNSASSIGNMENT: {'allow_post': False, 'allow_put': False, 'is_visible': True}, }, l3.FLOATINGIPS: { DNSNAME: {'allow_post': True, 'allow_put': False, 'default':
aur-archive/quasi
setup.py
Python
bsd-3-clause
448
0.049107
#!/usr/bin/env python from distutils.core import setup setup(name = "quasi", version = "0.87", description = "A multiple-context Python shell", author = "Ben Last", author_email = "[email protected]", url = "http://quasi-shell.sourceforge.net/",
license = "BSD", scripts = ["quasi.py"], data_files = [("share/licenses/quasi", ["LICENSE"])], extra_pa
th = "quasi", packages = ["."] )
Mnenmenth/RobotCode
JoystickInput/JoystickServer.py
Python
apache-2.0
458
0.026201
from serial import Serial import time import platform import socket serialPort = Serial('COM3' if platform.system(
) == 'Windows' else '/dev/ttyUSB0', 9600) time.sleep(2) server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind(('', 2222)) server.listen(1) while True: (client, address) = server.accept() print('Connected') while True: data
= client.recv(6)#.decode() if 'CLOSE' in data: break #print(data) serialPort.write(data)
endlessm/eos-event-recorder-daemon
tests/daemon/mock-server.py
Python
gpl-2.0
1,987
0.00151
#!/usr/bin/env python3 # Copyright 2015, 2016 Endless Mobile, Inc. # This file is part of eos-event-recorder-daemon. # # eos-event-recorder-daemon 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 2 of the License, or (at your # option) any later version. # # eos-event-recorder-daemon 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 eos-event-recorder-daemon. If not, see # <http://www.gnu.org/licenses/>. import gzip import http.server import sys class PrintingHTTPRequestHandler(http.server.BaseHTTPRequestHandler): def do_PUT(self): print(self.path, flush=True) content_encoding = self.headers['X-Endless-Content-Encoding'] print(content_encoding, flush=True) content_length = int(self.headers['Content-Length']) compressed_request_body = self.rfile.read(content_length) decompressed_request_body = gzip.decompress(compressed_request_body) print(len(decompressed_request_body), flush=True) sys.s
tdout.buffer.write(decompressed_request_body) sys.stdout.buffer.flush() status_code_str = sys.stdin.readline() status_code = int(status_code_str) self.send_response(status_code) self.end_headers() # A metrics server that simply prints the requests it receives to stdout class MockServer(http.server.HTTPServer): def __init__(self): SERVER_ADDRES
S = ('localhost', 0) super().__init__(SERVER_ADDRESS, PrintingHTTPRequestHandler) if __name__ == '__main__': mock_server = MockServer() print(mock_server.server_port, flush=True) mock_server.serve_forever()
mbuchove/analysis-tools-m
python/moonDist.py
Python
mit
5,073
0.027991
#! /usr/bin/env python #script that takes an optional argument for the date and target collection and calculates angular separation and elevation of each target from the moon. import ephem, subprocess, operator, argparse #host & port info hostName="veritase.sao.arizona.edu" portNum="" #hostName="lucifer1.spa.umn.edu" #portNum=33060 #dict for sorting/writing info moonlightsources = {} #setting up ephem observer object for veritas veritas = ephem.Observer() veritas.lat = '31:40.51' veritas.lon = '-110:57.132' veritas.elevation = 1268 #argument parser parser = argparse.ArgumentParser(description='Takes optional arguments to specify date and target collection. If no arguments are specified, will calculate angular distances from the Moon at the current time for all moonlight targets') parser.add_argument('--date',default=veritas.date, help='specify DATE (in UT) in the format "YYYY/MM/DD HH:MM" don\'t forget the quotation marks') parser.add_argument('--targets',default='moonlight_targets',help='Specifies collection of targets. Multiple Useful values for TARGETS: moonlight_targets,reduced_HV_targets,moonlight_bright,primary_targets,secondary_targets,blazar_filler_targets') parser.add_argument('--nocuts',help = 'displays results for all targets in the list, even if they fail the moon distance and elevation cuts', action = "store_true") args = parser.parse_args() #setting date/time to user-spefied value (or default to current date/time) veritas.date = args.date #letting user know the date and target collection used. print print "Date and time used (in UT): %s" %veritas.date print print "Calculating angular distances from the Moon for targets in %s collection..." %args.targets #MySQL command, runs on command line through subprocess targetList = args.targets.split(",") #for collection in args.targets.split(","): for n in range(0, len(targetList) ): if n == 0: execCMD = "SELECT tblObserving_Collection.source_id,ra,decl,epoch FROM tblObserving_Sources JOIN tblObserving_Collection ON tblObserving_Sources.source_id = tblObserving_Collection.source_id WHERE tblObserving_Collection.collection_id='%s'" %targetList[n] else: execCMD = execCMD + " OR tblObserving_Collection.collection_id='%s'" %targetList[n] sqlOut = subprocess.Popen(["mysql","-h","%s" %(hostName),"-P","%s" %(portNum),"-u", "readonly", "-D","VERITAS", "--execute=%s" %(execCMD)], stdout=subprocess.PIPE) #stores query results QUERY, err = sqlOut.communicate() if QUERY == "": print print "Query result is empty. Make sure date and target collection provided are valid. Going to crash now :(" #loop through all objects in the bright moonlight list #calculating and printing out angular separation from moon for count,source in enumerate(QUERY.rstrip().split("\n")): #skip header in query results if count == 0: continue #parsing through query results sourceName=source.split("\t")[0] sourceRA=source.split("\t")[1] sourceDEC=source.split("\t")[2] s
ourceEpoch=source.split("\t")[3] #makes sure same epoch is used veritas.epoch = float(sourceEpoch) #Define ephem moon object and calculate position (ra, dec) and phase TheMoon = ephem.Moon(veritas) TheMoon.compute(veritas) illum = TheMoon.moon_phase*100. #Get angular separation of moon and target degFromMoon = 180./ephem.pi * ephem.separation((TheMoon.ra,TheMoon.dec),(float(sourceRA),float(sourceDEC))) #Define ehpe
m object for source, to get elevation sourceobj = ephem.FixedBody() sourceobj._ra = float(sourceRA) sourceobj._dec = float(sourceDEC) sourceobj.compute(veritas) sourceALT = sourceobj.alt*180./ephem.pi moonlightsources[sourceName]=[(degFromMoon,sourceALT)] #end of for loop sorted_sources = sorted(moonlightsources.iteritems(), key=operator.itemgetter(1), reverse=True) #print sorted_sources if not args.nocuts: #printing only targets that pass the cuts print "Only showing targets with elevation > 20 degrees and moon distance > 10 degrees" print print "Source\t\t\tDegrees from Moon\tElevation" print "--------------------------------------------------------------" for s in sorted_sources: if s[1][0][1] > 20 and s[1][0][0] > 10: if len(s[0]) <=7: print "%s\t\t\t%0.3f\t\t\t%0.3f" %(s[0],s[1][0][0],s[1][0][1]) elif len(s[0]) <=15: print "%s\t\t%0.3f\t\t\t%0.3f" %(s[0],s[1][0][0],s[1][0][1]) else: print "%s\t%0.3f\t\t\t%0.3f" %(s[0],s[1][0][0],s[1][0][1]) else:#printing all targets, when cuts are disabled print print "Source\t\t\tDegrees from Moon\tElevation" print "--------------------------------------------------------------" for s in sorted_sources: if len(s[0]) <=7: print "%s\t\t\t%0.3f\t\t\t%0.3f" %(s[0],s[1][0][0],s[1][0][1]) elif len(s[0]) <=15: print "%s\t\t %0.3f\t\t\t%0.3f" %(s[0],s[1][0][0],s[1][0][1]) else: print "%s\t %0.3f\t\t\t%0.3f" %(s[0],s[1][0][0],s[1][0][1]) print "--------------------------------------------------------------" print "The Moon is %0.2f%% illuminated" % illum print
arulalant/CMIPs-Handler
scripts/mv/rm_dirs_contains_only_if_xml.py
Python
gpl-3.0
427
0.004684
''' This script will remove the directories if that contains only xml files. ''' import os srcpath = raw_input("Enter the source path : ") for root, su
b, files in os.walk(os.path.abspath(srcpath)): if files: files = [f for f in files if not f.endswith('.xml')] if not files: fpath = os.path.join(root)
os.system('rm -rf %s' % fpath) print "removed", fpath
mychris/xbmc-command
xbmc_command/core.py
Python
gpl-3.0
4,244
0.001649
# -*- coding: utf-8 -*- import argparse import time import socket import sys import json __prog__ = 'xbmc-command' PROG = __prog__ __version__ = '0.4.1' VERSION = __version__ class XBMC(object): def __init__(self, host, port): self.address = (host, port) self.__socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.__buffer = "" self.__decode = json.JSONDecoder().raw_decode self.settimeout(0) def settimeout(self, timeout): self.__timeout = timeout self.__socket.settimeout(timeout if timeout > 0 else None) def connect(self): self.__socket.connect(self.address) def close(self): if self.__socket: self.__socket.close() def shutdown(self): self.__socket.shutdown(socket.SHUT_RDWR) def __getattr__(self, key): return Rpc(self, key) def send(self, req): self.__socket.send(bytearray(req, 'utf-8')) def recv(self, json_rpc_id): start = time.time() while True: if self.__timeout > 0 and time.time() - start > self.__timeout: raise CommandException("read timeout") try: data = self.__socket.recv(1024) except socket.timeout: raise CommandException("read timeout") if not data: return None self.__buffer += data.decode('utf-8') while True: json_result = None try: json_result, index = self.__decode(self.__buffer) self.__buffer = self.__buffer[index:] except ValueError: break if json_result and 'id' in json_result and \ json_result['id'] == json_rpc_id: return json_result return None class Rpc(object): __REQ__ = '{"jsonrpc":"2.0", "method":"%s", "params":%s, "id":"%s"}' def __init__(self, xbmc, method): self.__xbmc = xbmc self.__method = method def __getattr__(self, key): return Rpc(self.__xbmc, "%s.%s" % (self.__method, key)) def __call__(self, *args, **kwargs): params = '{}' ident = str(kwargs['id']) if 'id' in kwargs else self.__method if args: params = json.dumps(args[0]) elif 'params' in kwargs: params = json.dumps(kwargs['params']) self.__xbmc.send(Rpc.__REQ__ % (self.__method, params, ident)) class CommandException(Exception): def __init__(self, msg): Exception.__init__(self) self.msg = msg def __str__(self): return self.msg class Command(object): def __init__(self): self.xbmc = None def call(self, args): raise NotImplementedError("Please Implement this method") def run_command(self, args): try: self.xbmc.connect() except socket.timeout: raise CommandException("Unable to connect to host %s:%s" % \ (self.xbmc.address[0], self.xbmc.address[1])) except socket.error as err: self.xbmc.close() raise CommandException("Could not open socket: " + err) self.call(args) def get_active_player_id(self): self.xbmc.Player.GetActivePlayers() result = self.xbmc.recv('Player.GetActivePlayers') if not result: raise CommandException('unable to receive active players') if len(result['result']) <= 0: return -1 for player in result['result']: if player['type'] in ('audio', 'video'
): return player['playerid'] return result[
'result'][0]['playerid'] @property def parser(self): parser = argparse.ArgumentParser(add_help=False) self.create_parser(parser) parser.add_argument('--help', action='help', help='show this help message and exit') return parser def create_parser(self, parser): return parser def parse_args(self, args): return self.parser.parse_args(args) @property def short_description(self): return '' # vim: ft=python ts=8 sts=4 sw=4 et:
lgarren/spack
var/spack/repos/builtin/packages/r-limma/package.py
Python
lgpl-2.1
1,754
0.00114
############################################################################## # Copyright (c) 2013-2017, Lawrence Livermore National Security, LLC. # Produced at the Lawrence Livermore National Laboratory. # # This file is part of Spack. # Created by Todd Gamblin, [email protected], All rights reserved. # LLNL-CODE-647188 # # For details, see https://github.com/llnl/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 WARR
ANTY 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 RLimma(RPackage): """Data analysis, linear models and differential expression for microarray data.""" homepage = "https://www.bioconductor.org/packages/limma/" url = "https://git.bioconductor.org/packages/limma" list_url = homepage version('3.32.10', git='https://git.bioconductor.org/packages/limma', commit='593edf28e21fe054d64137ae271b8a52ab05bc60') version('3.32.6', 'df5dc2b85189a24e939efa3a8e6abc41') depends_on('[email protected]:3.4.9', when='@3.32.10')
lmazuel/autorest
src/generator/AutoRest.Python.Tests/AcceptanceTests/validation_tests.py
Python
mit
5,290
0.002837
# -------------------------------------------------------------------------- # # Copyright (c) Microsoft Corporation. All rights reserved. # # The MIT License (MIT) # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the ""Software""), to # deal in the Software without restriction, including without limitation the # rights to use, copy, modify, merge, publish, distribute, sublice
nse, and/or # sell copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission
notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # IN THE SOFTWARE. # # -------------------------------------------------------------------------- import unittest import subprocess import sys import isodate import tempfile import json from datetime import date, datetime, timedelta import os from os.path import dirname, pardir, join, realpath cwd = dirname(realpath(__file__)) log_level = int(os.environ.get('PythonLogLevel', 10)) tests = realpath(join(cwd, pardir, "Expected", "AcceptanceTests")) sys.path.append(join(tests, "Validation")) from msrest.serialization import Deserializer from msrest.exceptions import DeserializationError, ValidationError from auto_rest_validation_test import AutoRestValidationTest from auto_rest_validation_test.models import ( Product, ConstantProduct, ChildProduct) class ValidationTests(unittest.TestCase): def test_constant_values(self): client = AutoRestValidationTest( "abc123", base_url="http://localhost:3000") client.api_version = "12-34-5678" client.get_with_constant_in_path() body = Product(child=ChildProduct()) product = client.post_with_constant_in_body(body=body) self.assertIsNotNone(product) def test_validation(self): client = AutoRestValidationTest( "abc123", base_url="http://localhost:3000") client.api_version = "12-34-5678" try: client.validation_of_method_parameters("1", 100) except ValidationError as err: self.assertEqual(err.rule, "min_length") self.assertEqual(err.target, "resource_group_name") try: client.validation_of_method_parameters("1234567890A", 100) except ValidationError as err: self.assertEqual(err.rule, "max_length") self.assertEqual(err.target, "resource_group_name") try: client.validation_of_method_parameters("!@#$", 100) except ValidationError as err: self.assertEqual(err.rule, "pattern") self.assertEqual(err.target, "resource_group_name") try: client.validation_of_method_parameters("123", 105) except ValidationError as err: self.assertEqual(err.rule, "multiple") self.assertEqual(err.target, "id") try: client.validation_of_method_parameters("123", 0) except ValidationError as err: self.assertEqual(err.rule, "minimum") self.assertEqual(err.target, "id") try: client.validation_of_method_parameters("123", 2000) except ValidationError as err: self.assertEqual(err.rule, "maximum") self.assertEqual(err.target, "id") try: tempproduct=Product(child=ChildProduct(), capacity=0) client.validation_of_body("123", 150, tempproduct) except ValidationError as err: self.assertEqual(err.rule, "minimum_ex") self.assertIn("capacity", err.target) try: tempproduct=Product(child=ChildProduct(), capacity=100) client.validation_of_body("123", 150, tempproduct) except ValidationError as err: self.assertEqual(err.rule, "maximum_ex") self.assertIn("capacity", err.target) try: tempproduct=Product(child=ChildProduct(), display_names=["item1","item2","item3","item4","item5","item6","item7"]) client.validation_of_body("123", 150, tempproduct) except ValidationError as err: self.assertEqual(err.rule, "max_items") self.assertIn("display_names", err.target) client2 = AutoRestValidationTest( "abc123", base_url="http://localhost:3000") client2.api_version = "abc" try: client2.validation_of_method_parameters("123", 150) except ValidationError as err: self.assertEqual(err.rule, "pattern") self.assertEqual(err.target, "self.api_version") if __name__ == '__main__': unittest.main()
jgomezc1/FEM_PYTHON
solidspy/__main__.py
Python
mit
119
0.008403
# -*- coding: utf-8 -*- from __fut
ure__ import absolute_im
port from solidspy.solids_GUI import solids_GUI solids_GUI()
ttm/musicLegacy
musicLegacy/effects.py
Python
mit
131
0.030534
class Reverb: pass class ScatterLo
cation: pass class Chorus: pass class Vocoder: pass class
ShuffleSound: pass
w495/python-video-shot-detector
shot_detector/filters/dsl/dsl_filter_mixin.py
Python
bsd-3-clause
6,383
0.00564
# -*- coding: utf8 -*- """ This is part of shot detector. Produced by w495 at 2017.05.04 04:18:27 """ from __future__ import absolute_import, division, print_function import collections import itertools import logging from shot_detector.utils.dsl import DslOperatorMixin from shot_detector.utils.dsl.dsl_kwargs import dsl_kwargs_decorator class DslFilterMixin(DslOperatorMixin): """ Basic filter mixin to build Filter-DSL """ __logger = logging.getLogger(__name__) @staticmethod def dsl_kwargs_decorator(*dsl_rules): """ :param dsl_rules: :return: """ return dsl_kwargs_decorator(*dsl_rules) def __or__(self, other): """ :param Filter other: :return: """ return self.apply_sequence([other]) def __ror__(self, other): """ :param Filter other: :return: """ return self.apply_sequence([other]) def apply_sequence(self, others): """ :param others: :return: """ filters = self.cast_to_apply_sequence(others) filter_sequence = self.apply_filter_sequence(filters) return filter_sequence def apply_filter_sequence(self, filters): """ Extends current own `sequential_filters` with `filters` or creates a new `FilterSequence`. :param filters: :return: """ from .filter_sequence import FilterSequence if isinstance(self, FilterSequence): self_filters = self.sequential_filters joined_filters = itertools.chain(self_filters, filters) filter_sequence = self else: joined_filters = itertools.chain([self], filters) filter_sequence = FilterSequence joined_filter_list = list(joined_filters) filter_sequence = filter_sequence( sequential_filters=joined_filter_list ) return filter_sequence # @staticmethod def cast_to_apply_sequence(self, others): """ :param others: :return: """ from .filter_cast_features import FilterCastFeatures for other in others: if isinstance(other, tuple): other = DslFilterMixin.to_tuple(*other) if not isinstance(other, DslFilterMixin): other = FilterCastFeatures( op_func=other, parallel_filters=[self] ) yield other def apply_operator(self, op_func=None, others=None, op_mode=None, **kwargs): """ :param op_func: :param others: :param op_mode: :param kwargs: :return: """ filters = self.cast_to_apply_operator(others) filter_operator = self.apply_filter_operator( op_func=op_func, filters=filters, op_mode=op_mode, ) return filter_operator def apply_filter_operator(self, op_func=None, filters=None, op_mode=None, **kwargs): """ :param op_func: :param filters: :param op_mode: :param kwargs: :return: """ from .filter_operator import FilterOperator, FilterOperatorMode fo_op_mode = FilterOperatorMode.LEFT if op_mode is self.Operator.RIGHT: fo_op_mode = FilterOperatorMode.RIGHT # joined_filters = itertools.chain([self], filters) filter_operator = FilterOperator( op_func=op_func, op_mode=fo_op_mode, # parallel_filters=list(joined_filters), **kwargs ) if isinstance(self, FilterOperator) and filter_operator == self: self_filters = self.parallel_filters joined_filters = itertools.chain(self_filters, filters) filter_operator = self else: joined_filters = itertools.chain([self], filters) joined_filter_list = list(joined_filters) filter_operator = filter_operator( parallel_filters=joined_filter_list ) return filter_operator @classmethod def to_tuple(cls, *args): """ :param args: :return: """ from .filter_tuple import FilterTuple filter_tuple = FilterTuple( parallel_filters=list(args) ) return filter_tuple def cast_to_apply_operator(self, others): """ :param others: :return: """ for other in others: if not isinstance(other, DslFilterMixin): other = self.scalar_to_filter( value=other, ) yield other def to_filter(self, value): """ :param value: :return: """ if isinstance(value, collections.Iterable): return self.seq_to_filter(value) return self.scalar_to_filter(value) @staticmethod def seq_to_filter(value): """ :param value: :return: """ from .filter_cast_seq_value import FilterCastSeqValue return FilterCastSeqValue(seq=value) @staticmethod def scalar_to_filter(value): """ :param value: :return: """ from .filter_cast_scalar_value import FilterCastScalarValue return FilterCastScalarValue(value=value) def __contains__(self, item): """ :param Filter item: :return: """ return self.intersect(item) def i(s
elf, *args, **kwargs): """ :param args: :param kwargs: :return: """ return self.intersect(*args, **kwargs) def intersect(self, other, threshold=0): """ :param other: :param threshold: :return: """ from .filter_intersection import FilterIntersection return FilterIntersection(
parallel_filters=[self, other], threshold=threshold )
flowroute/flowroute-numbers-python
FlowrouteNumbersLib/APIHelper.py
Python
mit
8,902
0.00483
# -*- coding: utf-8 -*- """ FlowrouteNumbersLib.APIHelper Copyright Flowroute, Inc. 2016 """ import jsonpickle import re class APIHelper: """A Helper Class for various functions associated with API Calls. This class contains static methods for operations that need to be performed during API requests. All of the methods inside this class are static methods, there is no need to ever initialise an instance of this class. """ @staticmethod def json_serialize(obj): """JSON Serialization of a given object. Args: obj (object): The object to serialise. Returns: str: The JSON serialized string of the object. """ if obj is None: return None # Resolve any Names if it's one of our objects that needs to have this called on if isinstance(obj, list): value = list() for item in obj: try: value.append(item.resolve_names()) except (AttributeError, TypeError): value.append(item) obj = value else: try: obj = obj.resolve_names() except (AttributeError, TypeError): obj = obj return jsonpickle.encode(obj, False) @staticmethod def json_deserialize(json): """JSON Deerialization of a given string. Args: json (str): The JSON serialized string to deserialize. Returns: dict: A dictionary representing the data contained in the JSON serialized string. """ if json is None: return None return jsonpickle.decode(json) @staticmethod def append_url_with_template_parameters(url, parameters): """Replaces template parameters in the given url. Args: url (str): The query url string to replace the template parameters. parameters (dict): The parameters to replace in the url. Returns: str: Url with replaced parameters. """ # Parameter validation if url is None: raise ValueError("url is null") if parameters is None: return url # Iterate and replace parameters for key in parameters: element = parameters[key] replace_value = "" # Load parameter value if element is None: replace_value = "" elif isinstance(element, list): replace_value = "/".join(element) else: replace_value = str(element) url = url.replace('{{{0}}}'.format(key),str(replace_value)) return url @staticmethod def append_url_with_query_parameters(url, parameters): """Appends the given set of parameters to the given query string. Args: url (str): The query url string to append the parameters. parameters (dict): The parameters to append. Returns: str: Url with appended query parameters. """ # Perform parameter validation if url is None: raise ValueError("url is null") if parameters is None: return url # Does the query string already have parameters? has_params = '?' in url # Iterate and replace parameters for key in parameters: element = parameters[key] # Ignore null values if element is None: continue # If already has parameters, use the &amp; to append new parameters separator = '&' if has_params else '?' if isinstance(element, list): url = url + '{0}{1}[]={2}'.format(separator, key, '&{0}[]='.format(key).join(element)) else: url = url + '{0}{1}={2}'.format(separator, key, str(parameters[key])) # Indicate the url has params has_params = True return url @staticmethod def clean_url(url): """Validates and processes the given query Url to clean empty slashes. Args: url (str): The given query Url to process. Returns: str: Clean Url as string. """ # Ensure that the urls are absolute regex = "^https?://[^/]+" match = re.match(regex, url) if match is None: raise ValueError('Invalid Url format.') # Remove redundant forward slashes protocol = match.group(0) query_url = url[len(protocol):] query_url = re.sub("//+", "/", query_url); return protocol + query_url return query_url @staticmethod def form_encode(obj, instanceName): """Encodes a model in a form-encoded manner such as person[Name] Args: obj (object): The given Object to form encode. instanceName (string): The base name to appear before each entry for this object. Returns: dict: A dictionary of form encoded properties of the model. """ # Resolve the names first value = APIHelper.resolve_name(obj) retval = dict() if value is None: return None # Loop through every item we need to send for item in value: if isinstance(value[item], list): # Loop through each item in the list and add it by number i = 0 for entry in value[item]: retval.update(APIHelper.form_encode(entry, instanceName + "[" + item + "][" + str(i) + "]")) i += 1 elif isinstance(value[item], dict): # Loop through each item in the dictionary and add it retval.update(APIHelper.form_encode(value[item], instanceName + "[" + item + "]")) else: # Add the current item retval[instanceName + "[" + item + "]"] = value[item] return retval @staticmethod def resolve_names(obj, names, retval): """Resolves parameters from their Model names to their API names. Args: obj (object): The given Object to resolve names for. names (dict): A dictionary containing a mapping from model name to API name. retval (dict): The dictionary to return which may or may not be empty (but must not be None). Returns: dict: A dictionary form of the model with properties in their API formats. """ # Loop through all properties in this model for name in names: value = getattr
(obj, name) if isinstance(value, list): # Loop through each item retval[names[name]] = list() for item in value: retval[names[name]].append(APIHelper.resolve_name(item)) elif isinstance(value, dict): # Loop through each item retval[names[name]] = dict() for key in value:
retval[names[name]][key] = APIHelper.resolve_name(value[key]) else: retval[names[name]] = APIHelper.resolve_name(value) # Return the result return retval @staticmethod def resolve_name(value): """Resolves name for a given obje
rna-seq/raisin.restyler
setup.py
Python
gpl-3.0
1,531
0.005225
from setuptools import setup, find_packages import sys, os version = '1.3' long_description = """The raisin.restyler package is a part of Raisin, the web application used for publishing the summary statistics of Grape, a pipeline used for processing and analyzing RNA-Seq data.""" setup(name='raisin.restyler', version=version, description="A package used in the Raisin web application", long_description=long_description, classifiers=[ 'Development Status :: 5 - Production/Stable', 'Programming Language :: Python', 'Intended Audience :: Developers', 'Operating System :: OS Independent', 'License :: OSI Approved :: GNU General Public License (GPL)', 'Natural Language :: English', 'Topic :: Software Development :: Libraries :: Python Modules', 'Operating System :: POSIX :: Linux'], keywords='RNA-Seq pipeline ngs transcriptome bioinformatics ETL
', aut
hor='Maik Roder', author_email='[email protected]', url='http://big.crg.cat/services/grape', license='GPL', packages=find_packages(exclude=['ez_setup', 'examples', 'tests']), namespace_packages = ['raisin'], package_data = {'raisin.restyler':['templates/*.pt']}, include_package_data=True, zip_safe=False, install_requires=[ # -*- Extra requirements: -*- 'configobj', 'zope.pagetemplate' ], entry_points=""" # -*- Entry points: -*- """, )
kbrose/project_euler
p20-29/p28.py
Python
unlicense
304
0.032895
sum = 1 curr = 3 for width in xrange(3,1002,2): inc = width - 1 sum = sum + curr #bottom right curr = curr + inc sum = sum + curr #bo
ttom left curr = curr + inc sum = sum + curr #top left curr = curr + inc sum = sum + cu
rr #top right curr = curr + inc + 2 print sum
COSMOGRAIL/PyCS
pycs/spldiff/rslc.py
Python
gpl-3.0
8,490
0.049706
""" Defines a class that represents a regularly sampled lightcurve """ import sys import numpy as np import splreg import pycs.gen.spl import copy as pythoncopy import scipy.optimize as spopt class rslc(): """ A regularly sampled lightcurve, typically obtained by regression. To make such a rslc from a usual lightcurve object, look at the factory function below. One idea is that we want to be able to add and subtract those, propagating errors. There is no "microlensing" or similar stuff -- only time shifts. """ def __init__(self, jds, mags, magerrs, pad, pd, timeshift=0.0, name="Name", plotcolour="black"): self.jds = jds self.mags = mags self.magerrs = magerrs self.plotcolour = plotcolour self.name = name self.timeshift = timeshift self.pad = pad self.pd = pd def __str__(self): retstr = "[RS:%s]" % (self.name) if self.timeshift != 0.0: retstr += "(%.3f)" % (self.timeshift) return retstr def shifttime(self, timeshift): self.timeshift += timeshift def copy(self): return pythoncopy.deepcopy(self) def getjds(self): return self.jds + self.timeshift def getmags(self): return self.mags def getmagerrs(self): return self.magerrs def mask(self, maxmagerr = 0.1, target = 20.0): self.magerrs[self.magerrs > maxmagerr] = target def wtv(self, method = "weights"): """ Return some weighted average variation WAV. Usuall called on a "difference" lightcurve. """ #return np.sum(np.fabs(self.mags[1:] - self.mags[:-1])) #mask = self.magerrs < maxmagerr if method == "weights": dys = self.mags[1:] - self.mags[:-1] dyws = 1.0 / (0.5*(self.magerrs[1:] + self.magerrs[:-1])) out = np.sum(np.fabs(dys) * dyws) / np.sum(dyws) if method == "simple": out = np.sum(np.fabs(self.mags[1:] - self.mags[:-1])) return out def factory(l, pad=300, pd=2, plotcolour=None,knotstep=20.0, n=None, stab=True,stabext=300.0, stabgap=20.0, stabstep=5.0, stabmagerr=-2.0, stabrampsize=0, stabrampfact=1.0, bokit=1, bokeps=2.0, boktests=5, bokwindow=None, k=3, verbose=True): """ Give me a lightcurve, I return a regularly sampled light curve, by performing some spline regression. !!! New: I also return the spline used for the regression :param pad: the padding, in days :param pd: the point density, in points per days. The points live on a regular grid in julian days, 0.0, 0.1, 0.2, 0.3 ... """ if plotcolour == None: plotcolour = l.plotcolour name = l.object jds = l.jds.copy() timeshift = l.timeshift mags = l.getmags(noml=True) magerrs = l.getmagerrs() minjd = np.round(jds[0] - pad) maxjd = np.round(jds[-1] + pad) npts = int(maxjd - minjd)*pd rsjds = np.linspace(minjd, maxjd, npts) # rs for regularly sampled # The regression itself mean_mag = np.mean(mags) def meanprior(query): return (0.0 * query + mean_mag) regfct,spline = splreg.splreg(jds, mags, magerrs,knotstep=knotstep, n=n, stab=stab, stabext=stabext, stabgap=stabgap, stabstep=stabstep, stabmagerr=stabmagerr, stabrampsize=stabrampsize, stabrampfact=stabrampfact, bokit=bokit, bokeps=bokeps, boktests=boktests,bokwindow=bokwindow, k=k, verbose=verbose) (rsmags, rsmagerrs) = regfct(rsjds) return rslc(rsjds, rsmags, rsmagerrs, pad, pd, timeshift=timeshift, name=name, plotcolour=plotcolour),spline def subtract(rs1, rs2): """ I subtract rs2 from rs1. This means I keep the jds and timeshift of rs1, and only change the mags and magerrs, interpolating rs2. I return a brand new rslc object, that has no timeshift (as we do not care about a timeshift, for a difference). :param rs1: :type rs1: rslc object :param rs2: :type rs2: rslc object """ newjds = rs1.getjds() newmags = rs1.mags.copy() newmagerrs = rs1.magerrs.copy() newpad = rs1.pad newpd = rs1.pd newname = "%s(%+.1f)-%s(%+.1f)" % (rs1.name, rs1.timeshift, rs2.name, rs2.timeshift) # We interpolate rs2 at the positions of rs1 newrs2mags = np.interp(rs1.getjds(), rs2.getjds(), rs2.mags, left=np.nan, right=np.nan) newrs2magerrs = np.interp(rs1.getjds(), rs2.getjds(), rs2.magerrs, left=np.nan, right=np.nan) # These arrays contain NaN at one of there extremas. newmags -= newrs2mags newmagerrs = np.sqrt(rs1.magerrs*rs1.magerrs + newrs2magerrs*newrs2magerrs) # The NaN are now propagated in newmags and newmagerrs # We cut them : nanmask = np.isnan(newmags) #nnan = np.sum(nanmask) #print nnan/newpd newjds = newjds[nanmask == False] newmags = newmags[nanmask == False] newmagerrs = newmagerrs[nanmask == False] return rslc(newjds, newmags, newmagerrs, newpad, newpd, timeshift=0.0, name=newname, plotcolour="black") def wtvdiff(rs1, rs2, method): """ Returns the wtv (weighted TV) of the difference between 2 curves. This is symmetric (no change if you invert rs1 and rs2), up to some small numerical errors. """ out = subtract(rs1, rs2).wtv(method) #print out return float(out) def bruteranges(step, radius, center): """ Auxiliary function for brute force exploration. Prepares the "ranges" parameter to be passed to brute force optimizer In other words, we draw a cube ... radius is an int saying how many steps to go left and right of center. center is an array of the centers, it can be of any lenght. You make 2*radius + 1 steps in each direction !, so radius=2 means 5 steps thus 125 calls for 4 curves. """ low = - step * radius up = step * (radius+1) if center.shape == (): c = float(center) return [((c+low),(c+up),step)] else: return [((c+low),(c+up),step) for c in center] def opt_ts(rslcs, method="weights", verbose=True): """ I optimize the timeshifts between the rslcs to minimize the wtv between them. Note that even if the wtvdiff is only about two curves, we cannot split this into optimizing AB AC AD in a row, as this would never calculate BC, and BC is not contained into AB + AC. !!! New : I also return a spline to optimise the magshifts :param rslcs: a list of rslc objects """ rslcsc = [rs.copy() for rs in rslcs] # We'll work on copies. # No need for reverse combis, as wtvdiff is symmetric. #couplelist = [couple for couple in [[rs1, rs2] for rs1 in rslcsc for rs2 in rslcsc] if couple[0] != couple[1]] indexes = np.arange(len(rslcsc)) indlist = [c for c in [[i1, i2] for i1 in indexes for i2 in indexes] if c[1] > c[0]] couplelist = [[rslcsc[i1], rslcsc[i2]] for (i1, i2) in indlist] # So the elements in couplelist are the SAME as those from rslcsc inishifts = np.array([rs.timeshift for rs in rslcsc[1:]]) # We won't move the first curve. def errorfct(timeshifts): if timeshifts.shape == (): timeshifts = np.array([timeshifts]) for (rs, timeshift) in zip(rslcsc[1:], timeshifts): rs.timeshift = timeshift tvs = np.array([wtvdiff(rs1, rs2, method=method) for (rs1, rs2) in couple
list]) ret = np.sum(tvs) #if verbose: # print timeshifts, ret return ret if verbose: print "Starting time shift optimization ..." print "Initial pars (shifts, not delays) : ", inishifts # Some brute force exploration, like for the dispersion techniques ... res = spopt.brute(errorfct, bruteranges(5,3,inishifts), full_output = 0, finish=None) # This would finish by default with fmin
... we do not want that. if verbose: print "Brute 1 shifts : %s" % res print "Brute 1 errorfct : %f" % errorfct(res) res = spopt.brute(errorfct, bruteranges(2.5,3,res), full_output = 0, finish=None) if verbose: print "Brute 2 shifts : %s" % res print "Brute 2 errorfct : %f" % errorfct(res) res = spopt.brute(errorfct, bruteranges(1.25,3,res), full_output = 0, finish=None) if verbose: print "Brute 3 shifts : %s" % res print "Brute 3 errorfct : %f" % errorfct(res) res = spopt.brute(errorfct, bruteranges(0.5,3,res), full_output = 0, finish=None) if verbose: print "Brute 4 shifts : %s" % res print "Brute 4 errorfct : %f" % errorfct(res) minout = spopt.fmin_powell(errorfct, res, xtol=0.001, full_output=1, disp=verbose) #minout = spopt.fmin_bfgs(errorfct, inishifts, maxiter=None, full_output=1, disp=verbose, retall=0, callback=None) popt = minout[0] minwtv
ryfeus/lambda-packs
Keras_tensorflow_nightly/source2.7/tensorflow/contrib/lite/toco/toco_flags_pb2.py
Python
mit
8,125
0.004062
# Generated by the protocol buffer compiler. DO NOT EDIT! # source: tensorflow/contrib/lite/toco/toco_flags.proto import sys _b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1')) from google.protobuf.internal import enum_type_wrapper from google.protobuf import descriptor as _descriptor from google.protobuf import message as _message from google.protobuf import reflection as _reflection from google.protobuf import symbol_database as _symbol_database from google.protobuf import descriptor_pb2 # @@protoc_insertion_point(imports) _sym_db = _symbol_database.Default() from tensorflow.contrib.lite.toco import types_pb2 as tensorflow_dot_contrib_dot_lite_dot_toco_dot_types__pb2 DESCRIPTOR = _descriptor.FileDescriptor( name='tensorflow/contrib/lite/toco/toco_flags.proto', package='toco', syntax='proto2', serialized_pb=_b('\n-tensorflow/contrib/lite/toco/toco_flags.proto\x12\x04toco\x1a(tensorflow/contrib/lite/toco/types.proto\"\x92\x03\n\tTocoFlags\x12&\n\x0cinput_format\x18\x01 \x01(\x0e\x32\x10.toco.FileFormat\x12\'\n\routput_format\x18\x02 \x01(\x0e\x32\x10.toco.FileFormat\x12.\n\x14inference_input_type\x18\x0b \x01(\x0e\x32\x10.toco.IODataType\x12(\n\x0einference_type\x18\x04 \x01(\x0e\x32\x10.toco.IODataType\x12\x1a\n\x12\x64\x65\x66\x61ult_ranges_min\x18\x05 \x01(\x02\x12\x1a\n\x12\x64\x65\x66\x61ult_ranges_max\x18\x06 \x01(\x02\x12\x17\n\x0f\x64rop_fake_quant\x18\x07 \x01(\x08\x12!\n\x19reorder_across_fake_quant\x18\x08 \x01(\x08\x12\x18\n\x10\x61llow_custom_ops\x18\n \x01(\x08\x12\x1f\n\x17\x64rop_control_dependency\x18\x0c \x01(\x08\x12+\n#debug_disable_recurrent_cell_fusion\x18\r \x01(\x08*\\\n\nFileFormat\x12\x17\n\x13\x46ILE_FORMAT_UNKNOWN\x10\x00\x12\x17\n\x13TENSORFLOW_GRAPHDEF\x10\x01\x12\n\n\x06TFLITE\x10\x02\x12\x10\n\x0cGRAPHVIZ_DOT\x10\x03') , dependencies=[tensorflow_dot_contrib_dot_lite_dot_toco_dot_types__pb2.DESCRIPTOR,]) _FILEFORMAT = _descriptor.EnumDescriptor( name='FileFormat', full_name='toco.FileFormat', filename=None, file=DESCRIPTOR, values=[ _descriptor.EnumValueDescriptor( name='FILE_FORMAT_UNKNOWN', index=0, number=0, options=None, type=None), _descriptor.EnumValueDescriptor( name='TENSORFLOW_GRAPHDEF', index=1, number=1, options=None, type=None), _descriptor.EnumValueDescriptor( name='TFLITE', index=2, number=2, options=None, type=None), _descriptor.EnumValueDescriptor( name='GRAPHVIZ_DOT', index=3, number=3, options=None, type=None), ], containing_type=None, options=None, serialized_start=502, serialized_end=594, ) _sym_db.RegisterEnumDescriptor(_FILEFORMAT) FileFormat = enum_type_wrapper.EnumTypeWrapper(_FILEFORMAT) FILE_FORMAT_UNKNOWN = 0 TENSORFLOW_GRAPHDEF = 1 TFLITE = 2 GRAPHVIZ_DOT = 3 _TOCOFLAGS = _descriptor.Descriptor( name='TocoFlags', full_name='toco.TocoFlags', filename=None, file=DESCRIPTOR, containing_type=None, fields=[ _descriptor.FieldDescriptor( name='input_format', full_name='toco.TocoFlags.input_format', index=0, number=1, type=14, cpp_type=8, label=1, has_default_value=False, default_value=0, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='output_format', full_name='toco.TocoFlags.output_format', index=1, number=2, type=14, cpp_type=8, label=1, has_default_value=False, default_value=0, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='inference_input_type', full_name='toco.TocoFlags.inference_input_type', index=2, number=11, type=14, cpp_type=8, label=1, has_default_value=False, default_value=0, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='inference_type', full_name='toco.TocoFlags.inference_type', index=3, number=4, type=14, cpp_type=8, label=1, has_default_value=False, default_value=0, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='default_ranges_min', full_name='toco.TocoFlags.default_ranges_min', index=4, number=5, type=2, cpp_type=6, label=1, has_default_value=False, default_value=float(0), message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='default_ranges_max', full_name='toco.TocoFlags.default_ranges_max', index=5, number=6, type=2, cpp_type=6, label=1, has_default_value=False, default_value=float(0), message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='drop_fake_quant', full_name='toco.TocoFlags.drop_fake_quant', index=6, number=7, type=8, cpp_type=7, label=1, has_default_value=False, default_value=False, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, options=None, file=DESCRIPTOR
), _descriptor.FieldDescriptor( name='reorder_across_fake_quant', full_name='toco.TocoFlags.reorder_across_fake_quant', index=7, number=8, type=8, cpp_type=7, label=1, has_default_value=False, default_value=False, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='allow_custom_ops', full_name='toco.TocoFlags.
allow_custom_ops', index=8, number=10, type=8, cpp_type=7, label=1, has_default_value=False, default_value=False, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='drop_control_dependency', full_name='toco.TocoFlags.drop_control_dependency', index=9, number=12, type=8, cpp_type=7, label=1, has_default_value=False, default_value=False, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='debug_disable_recurrent_cell_fusion', full_name='toco.TocoFlags.debug_disable_recurrent_cell_fusion', index=10, number=13, type=8, cpp_type=7, label=1, has_default_value=False, default_value=False, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], options=None, is_extendable=False, syntax='proto2', extension_ranges=[], oneofs=[ ], serialized_start=98, serialized_end=500, ) _TOCOFLAGS.fields_by_name['input_format'].enum_type = _FILEFORMAT _TOCOFLAGS.fields_by_name['output_format'].enum_type = _FILEFORMAT _TOCOFLAGS.fields_by_name['inference_input_type'].enum_type = tensorflow_dot_contrib_dot_lite_dot_toco_dot_types__pb2._IODATATYPE _TOCOFLAGS.fields_by_name['inference_type'].enum_type = tensorflow_dot_contrib_dot_lite_dot_toco_dot_types__pb2._IODATATYPE DESCRIPTOR.message_types_by_name['TocoFlags'] = _TOCOFLAGS DESCRIPTOR.enum_types_by_name['FileFormat'] = _FILEFORMAT _sym_db.RegisterFileDescriptor(DESCRIPTOR) TocoFlags = _reflection.GeneratedProtocolMessageType('TocoFlags', (_message.Message,), dict( DESCRIPTOR = _TOCOFLAGS, __module__ = 'tensorflow.contrib.lite.toco.toco_flags_pb2' # @@protoc_insertion_point(class_scope:toco.TocoFlags) )) _sym_db.RegisterMessage(TocoFlags) # @@protoc_insertion_point(module_scope)
realms-team/basestation-fw
libs/smartmeshsdk-REL-1.3.0.1/external_libs/cryptopy/crypto/cipher/aes.py
Python
bsd-3-clause
1,002
0.015968
""" crypto.aes AES Encryption Algorithm The AES algorithm is just Rijndael algorithm restricted to the default blockSize of 128 bits. Copyright (c) 2002 by Paul A.
Lambert Read LICENSE.txt for license information. 2002-06-01 """ from crypto.cipher.rijndael import Rijndael from crypto.cipher.base import BlockCipher, padWithPadLen, noPadding from crypto.errors import BadKeySizeError class AES(Rijndael): """ The AES algorithm is the Rijndael block cipher restricted to block sizes of 128
bits and key sizes of 128, 192 or 256 bits """ def __init__(self, key = None, padding = padWithPadLen(), keySize=16): """ Initialize AES, keySize is in bytes """ if not (keySize == 16 or keySize == 24 or keySize == 32) : raise BadKeySizeError, 'Illegal AES key size, must be 16, 24, or 32 bytes' Rijndael.__init__( self, key, padding=padding, keySize=keySize, blockSize=16 ) self.name = 'AES'
tadamhicks/morpheus-python
setup.py
Python
mit
480
0.04375
from ditutils.core import setup setup( name = 'morpheusapi', packages = ['morpheusapi'], version = '2.11.1', description = 'A python wrapper for Morpheus APIs', author = 'Adam Hic
ks', author_email = '[email protected]', url = 'https://github.com/tadamhicks/morpheus-python', download_url = 'htt
ps://github.com/tadamhicks/morpheus-python/archive/2.11.1.tar.gz', keywords = ['morpheus', 'api', 'morpheus data'], classifiers = [], )
masahir0y/barebox-yamada
scripts/serial/urlhandler/protocol_loop.py
Python
gpl-2.0
9,646
0.002281
#! python # # Python Serial Port Extension for Win32, Linux, BSD, Jython # see __init__.py # # This module implements a loop back connection receiving itself what it sent. # # The purpose of this module is.. well... You can run the unit tests with it. # and it was so easy to implement ;-) # # (C) 2001-2011 Chris Liechti <[email protected]> # this is distributed under a free software license, see license.txt # # URL format: loop://[option[/option...]] # options: # - "debug" print diagnostic messages from serial.serialutil import * import threading import time import logging # map log level names to constants. used in fromURL() LOGGER_LEVELS = { 'debug': logging.DEBUG, 'info': logging.INFO, 'warning': logging.WARNING, 'error': logging.ERROR, } class LoopbackSerial(SerialBase): """Serial port implementation that simulates a loop back connection in plain software.""" BAUDRATES = (50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200) def open(self): """\ Open port with current settings. This may throw a SerialException if the port cannot be opened. """ if self._isOpen: raise SerialException("Port is already open.") self.logger = None self.buffer_lock = threading.Lock() self.loop_buffer = bytearray() self.cts = False self.dsr = False if self._port is None: raise SerialException("Port must be configured before it can be used.") # not that there is anything to open, but the function applies the # options found in the URL self.fromURL(self.port) # not that there anything to configure... self._reconfigurePort() # all things set up get, now a clean start self._isOpen = True if not self._rtscts: self.setRTS(True) self.setDTR(True) self.flushInput() self.flushOutput() def _reconfigurePort(self): """\ Set communication parameters on opened port. For the loop:// protocol all settings are ignored! """ # not that's it of any real use, but it helps in the unit tests if not isinstance(self._baudrate, (int, long)) or not 0 < self._baudrate < 2**32: raise ValueError("invalid baudrate: %r" % (self._baudrate)) if self.logger: self.logger.info('_reconfigurePort()') def close(self): """Close port""" if self._isOpen: self._isOpen = False # in case of quick reconnects, give the server some time time.sleep(0.3) def makeDeviceName(self, port): raise SerialException("there is no sensible way to turn numbers into URLs") def fromURL(self, url): """extract host and port from an URL string""" if url.lower().startswith("loop://"): url = url[7:] try: # process options now, directly altering self for option in url.split('/'): if '=' in option: option, value = option.split('=', 1) else: value = None if not option: pass elif option == 'logging': logging.basicConfig() # XXX is that good to call it here?
self.logger = logging.getLogger('pySerial.loop')
self.logger.setLevel(LOGGER_LEVELS[value]) self.logger.debug('enabled logging') else: raise ValueError('unknown option: %r' % (option,)) except ValueError, e: raise SerialException('expected a string in the form "[loop://][option[/option...]]": %s' % e) # - - - - - - - - - - - - - - - - - - - - - - - - def inWaiting(self): """Return the number of characters currently in the input buffer.""" if not self._isOpen: raise portNotOpenError if self.logger: # attention the logged value can differ from return value in # threaded environments... self.logger.debug('inWaiting() -> %d' % (len(self.loop_buffer),)) return len(self.loop_buffer) def read(self, size=1): """\ Read size bytes from the serial port. If a timeout is set it may return less characters as requested. With no timeout it will block until the requested number of bytes is read. """ if not self._isOpen: raise portNotOpenError if self._timeout is not None: timeout = time.time() + self._timeout else: timeout = None data = bytearray() while size > 0: self.buffer_lock.acquire() try: block = to_bytes(self.loop_buffer[:size]) del self.loop_buffer[:size] finally: self.buffer_lock.release() data += block size -= len(block) # check for timeout now, after data has been read. # useful for timeout = 0 (non blocking) read if timeout and time.time() > timeout: break return bytes(data) def write(self, data): """\ Output the given string over the serial port. Can block if the connection is blocked. May raise SerialException if the connection is closed. """ if not self._isOpen: raise portNotOpenError # ensure we're working with bytes data = to_bytes(data) # calculate aprox time that would be used to send the data time_used_to_send = 10.0*len(data) / self._baudrate # when a write timeout is configured check if we would be successful # (not sending anything, not even the part that would have time) if self._writeTimeout is not None and time_used_to_send > self._writeTimeout: time.sleep(self._writeTimeout) # must wait so that unit test succeeds raise writeTimeoutError self.buffer_lock.acquire() try: self.loop_buffer += data finally: self.buffer_lock.release() return len(data) def flushInput(self): """Clear input buffer, discarding all that is in the buffer.""" if not self._isOpen: raise portNotOpenError if self.logger: self.logger.info('flushInput()') self.buffer_lock.acquire() try: del self.loop_buffer[:] finally: self.buffer_lock.release() def flushOutput(self): """\ Clear output buffer, aborting the current output and discarding all that is in the buffer. """ if not self._isOpen: raise portNotOpenError if self.logger: self.logger.info('flushOutput()') def sendBreak(self, duration=0.25): """\ Send break condition. Timed, returns to idle state after given duration. """ if not self._isOpen: raise portNotOpenError def setBreak(self, level=True): """\ Set break: Controls TXD. When active, to transmitting is possible. """ if not self._isOpen: raise portNotOpenError if self.logger: self.logger.info('setBreak(%r)' % (level,)) def setRTS(self, level=True): """Set terminal status line: Request To Send""" if not self._isOpen: raise portNotOpenError if self.logger: self.logger.info('setRTS(%r) -> state of CTS' % (level,)) self.cts = level def setDTR(self, level=True): """Set terminal status line: Data Terminal Ready""" if not self._isOpen: raise portNotOpenError if self.logger: self.logger.info('setDTR(%r) -> state of DSR' % (level,)) self.dsr = level def getCTS(self): """Read terminal status line: Clear To Send""" if not self._isOpen: raise portNotOpenError if self.logger: self.logger.info('getCTS() -> state of RTS (%r)' % (self.cts,)) return self.cts def
crakama/bc_7_twitment
twitment/sendSMS.py
Python
mit
1,600
0.005625
# Import the helper gateway class from AfricasTalkingGateway import AfricasTalkingGateway, AfricasTalkingGatewayException from twitment.search import ClassTwitter # Specify your login credentials class SMS(object): def __init__(self): pass def send(self,num): sendtweet_obj = ClassTwitter() x = sendtweet_obj.wordFrequency.wordslist username = "CATHERINERAKAMA" apikey = "676dbd926bbb04fa69ce90ee81d3f5ffee2692aaf80eb5793bd70fe93e77dc2e" # Specify the numbers that you want to send to in a comma-separated list # Please ensure you include the country code (+254 for Kenya) to = num # And of course we want our recipient
s to know what we really do message = x # Create a new instance of our awesome gateway class gateway = AfricasTalkingGateway(username, apikey) # Any gateway errors will be captured by our custom Exception class
below, # so wrap the call in a try-catch block try: # Thats it, hit send and we'll take care of the rest. results = gateway.sendMessage(to, message) for recipient in results: # status is either "Success" or "error message" print 'Message sent to number=%s;status=%s' % (recipient['number'], recipient[ 'status']) except AfricasTalkingGatewayException, e: print 'Encountered an error while sending: %s' % str(e)
OSHADataDoor/OshaBokeh
bokehsamples/osha_files.py
Python
apache-2.0
2,378
0.004626
# Author: Bala Venkatesan # License: Apache 2.0 ######################################################################## # Wrote this file to separate out the loading of the data from the # python file where the actual display happens ######################################################################## import pandas as pd import csv ######################################################################## # Loading data ######################################################################## statefile = open('./annual_averages_by_state.csv', 'r') csvreader = csv.reader(statefile) ######################################################################## # initializing a dataframe to parse only required data from file ######################################################################## columns = ["STATE", "TOTAL_POPULATION", "WORKFORCE", "WORK_%_OF_POP", "EMPLOYED", "EMP_%_OF_POP", "UNEMPLOYED", "UNEMPLOMENT_RATE", ] data = [] rowIndex = 0 ######################################################################## # function that parses the state data for 2012 & 2013 and returns # a DataFrame with the data read from the fil
e # the function cleans the data before returning the DataFrame ######################################################################## def state_data(): for row in csvreader: ####################################################################################### # intialize a bunch of index variables for data clean up # startat is used to push the iteration to the right in the case of st
ates with 2 words # stopat moves corresponding. ####################################################################################### index = 0 startat = 0 stopat=10 statename = row[0] # Initializing pandas series for DataFrame. values = [] for x in enumerate(row): print statename print x if(index == 0): values.append(statename.upper()) else: values.append(x.replace(",","")) index = index + 1 data.insert(rowIndex,values) df = pd.DataFrame(data,columns=columns) return df if __name__ == '__main__': print state_data()
scieloorg/scielo-django-extensions
scielo_extensions/formfields.py
Python
bsd-2-clause
1,891
0.006875
# coding: utf-8 # Copyright (c) 2012, SciELO <[email protected]> # All rights reserved. # Redistribution and use in source and binary forms, with or without modification, # are permitted provided that the following conditions are met: # Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BU
T NOT LIMITED TO, THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. # IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS 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. import re from django import forms from django.utils.translation import ugettext_lazy as _ class ISSNField(forms.CharField): default_error_messages = { 'invalid': _('Enter a valid ISSN.') } regex = r'[0-9]{4}-[0-9]{3}[0-9X]{1}$' def clean(self, value): if value is not u'' and value is not None: result = re.match(self.regex, value) if result is None: raise forms.ValidationError(self.error_messages['invalid']) return value
rspavel/spack
var/spack/repos/builtin/packages/r-sdmtools/package.py
Python
lgpl-2.1
1,363
0.005869
# Copyright 2013-2020 Lawrence Livermore National Security, LLC and other # Spack Project Developers. See the top-level COPYRIGHT file for details. #
# SPDX-License-Identifier: (Apache-2.0 OR MIT) from spack import * class RSdmtools(RPackage): """Species Distribution Modelling Tools: Tools for processing data associated with species distribution modelling exercises This packages provides a set of tools for post processing the outcomes of species distribution modeling exercises.""" homepage = "https://cloud.r-project.org/package=SDMTools" url = "https://cloud.r-project.org/src/contrib/SDMTools_1.1-221.tar.gz" l
ist_url = "https://cloud.r-project.org/src/contrib/Archive/SDMTools" version('1.1-221.1', sha256='3825856263bdb648ca018b27dc6ab8ceaef24691215c197f8d5cd17718b54fbb') version('1.1-221', sha256='a6da297a670f756ee964ffd99c3b212c55c297d385583fd0e767435dd5cd4ccd') version('1.1-20', sha256='d6a261ce8f487d5d03b1931039f528f2eb50fb9386e7aae40045c966ff6d4182') version('1.1-13', sha256='02d94977bfa2f41f1db60e619335ac0ea8109dd98108ff9d21a412f7c4a14a2e') version('1.1-12', sha256='6dc4a8a046e7fced190402f39a9bae6f863e08c320f0881367c022b2f220f14b') version('1.1-11', sha256='1caf8fa1914ad6921d76e7b22a8c25cfe55892b0d21aef3b2a7b8f5b79b9388b') depends_on('r-r-utils', type=('build', 'run'))
tehp/reddit
MessageArchiveSimple/messagearchivesimple.py
Python
mit
1,943
0.021101
#/u/Goldensights import praw import time import datetime '''USER CONFIG''' USERNAME = "" #This is the bot's Username. In order to send mail, he must have some amount of Karma. PASSWORD = "" #This is the bot's Password.
USERAGENT = "" #This is a short description of what the bot does. For example "/u/GoldenSights' Newsletter bot" MAXPOSTS = 1000 #This is how many posts you want to retrieve all at once. PRAW can download 100 at a time. WAIT = 30 #This is how many seconds you
will wait between cycles. The bot is completely inactive during this time. PRINTFILE = 'messages.txt' #This is the file, in the same directory as the .py file, where the messages are stored SUBJECTLINE = "Newsletterly" ITEMTYPE = 't4' #The type of item to gather. t4 is a PM '''All done!''' WAITS = str(WAIT) try: import bot #This is a file in my python library which contains my Bot's username and password. I can push code to Git without showing credentials USERNAME = bot.uG PASSWORD = bot.pG USERAGENT = bot.aG except ImportError: pass r = praw.Reddit(USERAGENT) r.login(USERNAME, PASSWORD) def work(): unread = r.get_unread(limit=MAXPOSTS) results = [] for message in unread: if ITEMTYPE in message.fullname: print(message.id, message.subject, end=" ") if SUBJECTLINE.lower() in message.subject.lower(): print(message.body) messagedate = datetime.datetime.utcfromtimestamp(message.created_utc) messagedate = datetime.datetime.strftime(messagedate, "%B %d %Y %H:%M UTC") results += [message.fullname + " : " + message.author.name, messagedate, message.body, "\n\n"] else: print() message.mark_as_read() logfile = open(PRINTFILE, "a") for result in results: print(result, file=logfile) logfile.close() while True: try: work() except Exception as e: print('An error has occured:', str(e)) print('Running again in ' + WAITS + ' seconds \n') time.sleep(WAIT)
Bismarrck/tensorflow
tensorflow/python/ops/while_v2.py
Python
apache-2.0
38,134
0.007159
# Copyright 2018 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. # ============================================================================= """while_v2 and gradient. This is a version of while_loop that emits a single While op, as well as the gradient function for While ops produced by while_loop. This will eventually replace the current tf.while_loop implementation once it reaches feature and performance parity. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function from tensorflow.core.framework import attr_value_pb2 from tensorflow.python.framework import constant_op from tensorflow.python.framework import dtypes from tensorflow.python.framework import func_graph as func_graph_module from tensorflow.python.framework import function_def_to_graph from tensorflow.python.framework import ops from tensorflow.python.framework import tensor_shape from tensorflow.python.framework import tensor_spec from tensorflow.python.framework import tensor_util from tensorflow.python.ops import array_ops from tensorflow.python.ops import control_flow_ops from tensorflow.python.ops import control_flow_util from tensorflow.python.ops import control_flow_util_v2 as util from tensorflow.python.ops import custom_gradient from tensorflow.python.ops import gen_functional_ops from tensorflow.python.ops import gradients_impl from tensorflow.python.ops import list_ops from tensorflow.python.ops import math_ops from tensorflow.python.ops import tensor_array_ops from tensorflow.python.util import nest # pylint: disable=protected-access # TODO(b/79881896): Handle external control dependencies. tf.while_loop allows # control dependencies on external nodes with at least 1 output. # Another idea is to create const nodes outside the loop and add control edges # to them and then pass those in as data inputs. This should probably be # handled in the CapturingGraph itself. def while_loop(cond, body, loop_vars, shape_invariants=None, maximum_iterations=None, name=None, return_same_structure=True): """Like tf.while_loop, except emits a single While op.""" maximum_iterations = _validate_and_convert_to_tensor(maximum_iterations) # Keep the original loop_vars around to know which args were TensorArrays. orig_loop_vars = loop_vars # Cache its length since we use it at multiple places below. len_orig_loop_vars = len(orig_loop_vars) # Convert TensorArrays to their flow variables. These get converted back to # TensorArrays before calling `cond` and `body`. See `wrapped_cond` and # `wrapped_body` below. loop_vars = list(_tensor_array_to_flow(orig_loop_vars)) loop_vars = nest.map_structure( ops.internal_convert_to_tensor_or_indexed_slices, loop_vars) if shape_invariants is not None: nest.assert_same_structure(orig_loop_vars, shape_invariant
s) else: shape
_invariants = nest.map_structure(lambda t: t.shape, loop_vars) if not name: name = "while" with ops.name_scope(name) as scope: with ops.name_scope(None): cond_name = util.unique_fn_name(scope, "cond") body_name = util.unique_fn_name(scope, "body") loop_counter = constant_op.constant( 0, dtype=maximum_iterations.dtype if maximum_iterations is not None else None, name="loop_counter") # Add loop counter needed for computing gradients. loop_vars = [loop_counter] + loop_vars shape_invariants = type(shape_invariants)([tensor_shape.scalar() ]) + shape_invariants # Automatic control dependencies are added in defuns, but not in v1 # graphs. Propagate that behavior here. add_control_dependencies = ops.get_default_graph()._add_control_dependencies # Build a `cond` wrapper that can handle the extra counter loop_var. def wrapped_cond(loop_counter, *args): # Convert the flow variables in `args` to TensorArrays. `args` should # already have the same structure as `orig_loop_vars` but currently there # is no nest.zip so we call `_pack_sequence_as` which flattens both # `orig_loop_vars` and `args`, converts flows in `args` to TensorArrays # and packs it into the structure of `orig_loop_vars`. if maximum_iterations is None: return cond(*_pack_sequence_as(orig_loop_vars, args)) else: return math_ops.logical_and( loop_counter < maximum_iterations, cond(*_pack_sequence_as(orig_loop_vars, args))) cond_graph = func_graph_module.func_graph_from_py_func( cond_name, wrapped_cond, loop_vars, {}, signature=_build_signature(loop_vars, shape_invariants), func_graph=util.WhileCondFuncGraph(cond_name), add_control_dependencies=add_control_dependencies) # Add external_captures of cond to the list of loop vars. # Note that external tensors will be treated as loop invariants, i.e., # the value of that tensor in each iteration is the same as it was at the # beginning of the loop execution. loop_vars = loop_vars + cond_graph.external_captures shape_invariants = shape_invariants + type(shape_invariants)( [t.shape for t in cond_graph.external_captures]) def wrapped_body(loop_counter, *args): """Loop body augmented with counter update. Args: loop_counter: Loop counter which needs to be incremented in the body. *args: List of args args[:len_orig_loop_vars] - Args for the original loop body. args[len_orig_loop_vars:] - External captures of cond. These get passed through as is. Returns: A list of tensors the same length as args. """ # Convert the flow variables in `args` to TensorArrays. `args` should # already have the same structure as `orig_loop_vars` but currently there # is no nest.zip so we call `_pack_sequence_as` which flattens both # `orig_loop_vars` and `args`, converts flows in `args` to TensorArrays # and packs it into the structure of `orig_loop_vars`. outputs = body( *_pack_sequence_as(orig_loop_vars, args[:len_orig_loop_vars])) if not nest.is_sequence(outputs): outputs = [outputs] # Compare the structure of input and output of body converting the # top-level tuples to list to be compatible with legacy while_loop. nest.assert_same_structure(list(outputs), list(orig_loop_vars)) outputs = _tensor_array_to_flow(outputs) # Return the external_captures of cond_graph as is, i.e., treat them as # loop invariants. # TODO(srbs): Update lowering code to create _Enter nodes with # is_constant=True for inputs that are directly passed to outputs. return [loop_counter + 1] + list(outputs) + list( args[len_orig_loop_vars:]) body_graph = func_graph_module.func_graph_from_py_func( body_name, wrapped_body, loop_vars, {}, signature=_build_signature(loop_vars, shape_invariants), func_graph=util.WhileBodyFuncGraph(body_name), add_control_dependencies=add_control_dependencies) # Add external captures of body to the list of loop vars. # Note that external tensors will be treated as loop invariants, i.e., # the value of that tensor in each iteration is the same as it was at the # beginning of the loop execution. loop_vars = loop_vars + body_graph.external_captures # TODO(srbs): Update lowering code to create _Enter nodes with # is_constant=True for inpu
kradalby/O2
opos/views.py
Python
mit
2,687
0.04131
from django.shortcuts import render, redirect from django.contrib.auth.decorators import user_passes_test from django.shortcuts import get_object_or_404 from django import forms from django.db.models import Max, Avg, Sum from opos.models import Customers from opos.forms import CustomerAddForm, CustomerForm def is_staff (user): if user.is_staff
or user.is_superuser: return True else: return False @user_passes_test (is_staff) def dashboard (request): c = {} c['curdebt'] =
Customers.objects.all().aggregate(Sum('curdebt'))['curdebt__sum'] c['maxdebt'] = Customers.objects.all().aggregate(Sum('maxdebt'))['maxdebt__sum'] c['highestcurdebt'] = Customers.objects.all().aggregate(Max('curdebt'))['curdebt__max'] from opos.sql import get_total_sale c['totalsale'] = get_total_sale ()[0] return render (request, "dashboard.html", c) @user_passes_test (is_staff) def customersales(request, customerpk): from opos.sql import get_customer_ticketlines customer = get_object_or_404 (Customers, pk=customerpk) ticketlines = get_customer_ticketlines (customer.pk) c = {} c['customer'] = customer c['ticketlines'] = ticketlines return render (request, "customer-sales.html", c) @user_passes_test (is_staff) def customers (request): customers = Customers.objects.all () c = {} c['customers'] = customers return render (request, "customers.html", c) @user_passes_test (is_staff) def customeradd (request): if request.method == 'POST': form = CustomerAddForm (request.POST) if form.is_valid (): form.save () return redirect ('customers') c = {} c['customeredit'] = CustomerAddForm () return render (request, "customer-add.html", c) @user_passes_test (is_staff) def customeredit (request, customerpk): customer = get_object_or_404 (Customers, pk=customerpk) if request.method == 'POST': form = CustomerForm (request.POST, instance=customer) if form.is_valid (): form.save () return redirect ('customers') else: form = CustomerForm (instance=customer) c = {} c['customer'] = customer form.fields['id'] = forms.CharField (widget=forms.widgets.HiddenInput()) c['customeredit'] = form return render (request, "customer-edit.html", c) def selfdebtcheck (request): c = {} if request.method == 'POST': card = 'c' + request.POST.get("card") try: customer = Customers.objects.get (card=card) except: return render (request, "self-debtcheck.html", c) customer = get_object_or_404 (Customers, card=card) c['customer'] = customer c['leftdebt'] = customer.maxdebt - customer.curdebt return render (request, "self-debtshow.html", c) else: return render (request, "self-debtcheck.html", c)
DVSBA/ajenti
plugins/network/recovery.py
Python
lgpl-3.0
305
0.006557
from a
jenti.api import * from ajenti.com import * class DebianNetworkCfg(Plugin): implements(IConfigurabl
e) name = 'Network' id = 'network' platform = ['Debian', 'Ubuntu'] def list_files(self): dir = '/etc/network/' return [dir+'*', dir+'*/*', dir+'*/*/*']
cclai999/rango
tango_with_django_project/populate_rango.py
Python
mit
3,340
0.00479
import os os.environ.setdefault('DJANGO_SETTINGS_MODULE','tango_with_django_project.settings') import django django.setup() from rango.models import Category, Page def populate(): # First, we will create lists of dictionaries containing the pages # we want to add into each category. # Then we will create a dictionary of dictionaries for our categories. # This might seem a little bit confusing, but it allows us to iterate # through each data structure, and add the data to our models. python_pages = [ {"title": "Official Python Tutorial", "url": "http://docs.python.org/2/tutorial/", "views": 32}, {"title": "How to Think like a Computer Scientist", "url": "http://www.greenteapress.com/thinkpython/", "views": 16}, {"title": "Learn Python in 10 Minutes", "url": "http://www.korokithakis.net/tutorials/python/", "views": 8}] django_pages = [ {"title": "Official Django Tutorial", "url": "https://docs.djangoproject.com/en/1.9/intro/tutorial01/", "views": 32}, {"title": "Django Rocks", "url": "http://www.djangorocks.com/", "views": 16}, {"title": "How to Tango with Django", "url":"http://www.tangowithdjango.com/", "views": 8}] other_pages = [ {"title": "Bottle", "url": "http://bottlepy.org/docs/dev/", "views": 32}, {"title": "Flask", "url": "http://flask.pocoo.org", "views": 16} ] cats = {"Python": {"pages": python_pages, "views":128, "likes":64}, "Django": {"pages": django_pages, "views":64, "likes":32}, "Other Frameworks": {"pages": other_pages, "views":32, "likes":16}, "Python User Group": {"pages": [], "views": 34, "likes": 16}, "Pascal": {"pages": [], "views": 32, "likes": 16},
"Perl": {"pages": [], "views": 32, "likes": 16}, "Php": {"pages": [], "views": 32, "likes": 16}, "Prolog":
{"pages": [], "views": 32, "likes": 16}, "Programming": {"pages": [], "views": 32, "likes": 16} } # The code below goes through the cats dictionary, then adds each category, # and then adds all the associated pages for that category. # if you are using Python 2.x then use cats.iteritems() see # http://docs.quantifiedcode.com/python-anti-patterns/readability/ # for more information about how to iterate over a dictionary properly. for cat, cat_data in cats.items(): c = add_cat(cat, cat_data["views"], cat_data["likes"]) for p in cat_data["pages"]: add_page(c, p["title"], p["url"],p["views"]) # print out the categories we have added for c in Category.objects.all(): for p in Page.objects.filter(category=c): print("- {0} -{1}".format(str(c),str(p))) def add_cat(name, views, likes): c = Category.objects.get_or_create(name=name)[0] c.views = views c.likes = likes c.save() return c def add_page(cat, title, url, views=0): p = Page.objects.get_or_create(category=cat, title=title)[0] p.url = url p.views = views p.save() return p # Start execution here! if __name__ == '__main__': print("Starting Rango population script...") populate()
mefly2012/platform
test/test.py
Python
apache-2.0
3,173
0.001891
# -*- coding: utf-8 -*- import codecs all_tables = { 'ktgg': ['main'], 'zgcpwsw': ['title', 'casecode'], # # # 'ktgg', # # 'cdfy_sfgk', # # 'newktgg', # # 'zyktgg', # # 'zgcpwsw', # # 'itslaw', # # 'qyxg_zgcpwsw', # # 'qyxg_wscpws', # 'zhixing': ['pname', 'case_code'], 'dishonesty': ['pname', 'case_code', 'exe_code'], 'recruit': ['pubdate_doublet', 'company_name', 'job_functions', 'source'], 'xgxx_shangbiao': ['applicant_name', 'application_no'], 'shgy_zhaobjg': ['title'], 'shgy_zhongbjg': ['title'], 'rmfygg': ['notice_content', 'notice_time', 'notice_type'], 'overseas_investment': ['certificate_no'], 'qyxx_wanfang_zhuanli': ['application_code'], 'tddy': ['landno', 'land_location', 'mortgage_right_name'], 'tdzr': ['land_location', 'landno', 'original_usename'], 'dcos': ['company_name', 'certificate_num'], 'qyxx_enterpriseQualificationForeign': ['company_name', 'certificate_no', 'issue_date'], 'qyxx_gcjljz': ['company_name', 'certificate_no'], 'qyxx_jzsgxkz': ['company_name', 'certificate_no'], 'qyxx_miit_jlzzdwmd': ['company_name', 'certificate_no'], 'qyxx_food_prod_cert': ['company_name', 'certificate_no'], 'qyxx_haiguanzongshu': ['company_name', 'customs_code'], 'qyxx_gmpauth_prod_cert': ['company_name', 'certificate_no'], 'qyxx_hzp_pro_prod_cert': ['company_name', 'certificate_no'], 'qyxx_medi_jy_prod_cert': ['company_name', 'certificate_no'],
'qy
xx_medi_pro_prod_cert': ['company_name', 'certificate_no'], 'qyxx_industrial_production_permit': ['company_name', 'certificate_no'], 'qyxx_nyscqyzzcx': ['company_name', 'validdate'], 'qyxx_tk': ['company_name', 'certificate_no'], 'qyxx_ck': ['company_name', 'certificate_no'], 'xzcf': ['name', 'public_date', 'punish_code'], 'rjzzq': ['copyright_nationality', 'regnum', 'regdate'], 'qyxx_finance_xkz': ['company_name', 'issue_date', 'id_serial_num'], 'qylogo': ['company_full_name'], 'ssgs_zjzx': ['_id'], 'simutong': ['financing_side', 'invest_side', 'invest_time'], 'tddkgs': ['title', 'main'], 'shgy_tdcr': ['project_name', 'project_location', 'electron_supervise'], 'qyxx_zhuanli': ['application_code', 'reg_effect_date'], 'zhuanli_zhuanyi': ['application_code', 'reg_effect_date'], 'zpzzq': ['copyright_owner', 'regnum'], 'zuzhijigoudm': ['jgdm', 'jgmc'], # 'sfpm_taobao':['title','auctioneer','disposal_unit'], # 'domain_name_website_info':['organizer_name','site_certificate_no','domain_name'] } tablename='SIMUTONG' if __name__ == '__main__': # fok = codecs.open(tablename+'_mysql', 'r', encoding='utf-8') # fdup = codecs.open(tablename+'_dup', 'r', encoding='utf-8') # # foks=fok.read() # for i in fdup.readlines(): # if i.strip() not in foks: # print i # break # # fdup.seek(0) # all_list=[] # # for i in fdup.readlines(): # all_list.append(i.strip()) # print len(all_list) # print len(set(all_list)) a=1 b=0 try: a/b except Exception as e: print str(e)
ging/fiware-keystone-scim
keystone_scim/contrib/scim/converter.py
Python
apache-2.0
6,230
0.000482
# # Copyright 2014 Telefonica Investigacion y Desarrollo, S.A.U # # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you 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. """Converters between SCIM JSON representation and Keystone""" import functools ROLE_SEP = '#' _EXT_SCHEMA = 'urn:scim:schemas:extension:keystone:%s' DEFAULT_VERSION = '1.0' def get_schema(BASE_SCHEMA, path): if 'v2' in path: version = '2.0' else: version = '1.0' return BASE_SCHEMA % version def _remove_dict_nones(f): def wrapper(*args, **kwargs): res = f(*args, **kwargs) return dict(filter(lambda x: x[1], res.items())) return wrapper @_remove_dict_nones def user_key2scim(ref, path, schema=True): ref = { 'schemas': [get_schema('urn:scim:schemas:core:%s', path), get_schema(_EXT_SCHEMA, path)] if schema else None, 'id': ref.get('id', None), 'userName': ref.get('name', None), 'displayName': ref.get('description', None), 'active': ref.get('enabled', None), 'emails': [{'value': ref['email']}] if 'email' in ref else None, get_schema(_EXT_SCHEMA, path): { 'domain_id': ref.get('domain_id', None) } } return ref def listusers_key2scim(ref, path, page_info={}): res = { 'schemas': [get_schema('urn:scim:schemas:core:%s', path), get_schema(_EXT_SCHEMA, path)], 'Resources': map(functools.partial(user_key2scim, schema=False, path=path), ref) } res.update(page_info) return res @_remove_dict_nones def user_scim2key(scim, path): return { 'domain_id': scim.get(get_schema(_EXT_SCHEMA, path), {}) .get('domain_id', None), 'email': scim.get('emails', [{}])[0].get('value', None), 'id': scim.get('id', None), 'enabled': scim.get('active', None), 'name': scim.get('userName', None), 'description': scim.get('displayName', None), 'password': scim.get('password', None) } @_remove_dict_nones def role_scim2key(scim): keystone = {} keystone['id'] = scim.get('id', None) if scim.get('domain_id', None): keystone['name'] = '%s%s%
s' % ( scim.get('domain_id'), ROLE_SEP, scim.get('name', None)) else: keystone['name'] = scim.get('name', None) return keystone @_remove_dict_nones def role_key2scim(ref, path=DEFAULT_VERSION, schema=True): sc
im = { 'schemas': [get_schema(_EXT_SCHEMA, path)] if schema else None, 'id': ref.get('id', None) } dom_name = ref.get('name', '') if dom_name.find(ROLE_SEP) > -1: (domain, name) = dom_name.split(ROLE_SEP, 1) else: (domain, name) = (None, dom_name) scim['name'] = name scim['domain_id'] = domain return scim def listroles_key2scim(ref, path, page_info={}): res = { 'schemas': [get_schema(_EXT_SCHEMA, path)], 'Resources': map(functools.partial(role_key2scim, schema=False, path=path), ref) } res.update(page_info) return res @_remove_dict_nones def group_scim2key(scim, path): return { 'domain_id': scim.get(get_schema(_EXT_SCHEMA, path), {}) .get('domain_id', None), 'id': scim.get('id', None), 'name': scim.get('displayName', None) } @_remove_dict_nones def group_key2scim(ref, path, schema=True): return { 'schemas': [get_schema('urn:scim:schemas:core:%s', path), get_schema(_EXT_SCHEMA, path)] if schema else None, 'id': ref.get('id', None), 'displayName': ref.get('name', None), get_schema(_EXT_SCHEMA, path): { 'domain_id': ref.get('domain_id', None) } } def listgroups_key2scim(ref, path, page_info={}): res = { 'schemas': [get_schema('urn:scim:schemas:core:%s', path), get_schema(_EXT_SCHEMA, path)], 'Resources': map(functools.partial(group_key2scim, schema=False, path=path), ref) } res.update(page_info) return res @_remove_dict_nones def organization_key2scim(ref, path, schema=True): return { 'schemas': [get_schema('urn:scim:schemas:core:%s', path), get_schema(_EXT_SCHEMA, path)] if schema else None, 'id': ref.get('id', None), 'name': ref.get('name', None), 'description': ref.get('description', None), 'active': ref.get('enabled', None), 'is_default': ref.get('is_default', None), get_schema(_EXT_SCHEMA, path): { 'domain_id': ref.get('domain_id', None) } } def listorganizations_key2scim(ref, path, page_info={}): res = { 'schemas': [get_schema('urn:scim:schemas:core:%s', path), get_schema(_EXT_SCHEMA, path)], 'Resources': map(functools.partial(organization_key2scim, schema=False, path=path), ref) } res.update(page_info) return res @_remove_dict_nones def organization_scim2key(scim, path): return { 'domain_id': scim.get(get_schema(_EXT_SCHEMA, path), {}) .get('domain_id', None), 'id': scim.get('id', None), 'enabled': scim.get('active', None), 'name': scim.get('name', None), 'description': scim.get('description', None), 'is_default': scim.get('is_default', None) }
guiquanz/msaf
msaf/eval.py
Python
mit
14,384
0.000834
""" Evaluates the estimated results of the Segmentation dataset against the ground truth (human annotated data). """ from joblib import Parallel, delayed import logging import mir_eval import numpy as np import os import pandas as pd import six import sys # Local stuff import msaf import msaf.input_output as io import msaf.algorithms as algorithms from msaf import jams2 from msaf import utils def print_results(results): """Print all the results. Parameters ---------- results: pd.DataFrame Dataframe with all the results """ res = results.mean() logging.info("Results:\n%s" % res) def compute_results(ann_inter, est_inter, ann_labels, est_labels, bins, est_file): """Compute the results using all the available evaluations. Return ------ results : dict Contains the results of all the evaluations for the given file. Keys are the following: track_name : Name of the track ds_name : Name of the data set HitRate_3F : F-measure of hit rate at 3 seconds HitRate_3P : Precision of hit rate at 3 seconds HitRate_3R : Recall of hit rate at 3 seconds HitRate_0.5F : F-measure of hit rate at 0.5 seconds HitRate_0.5P : Precision of hit rate at 0.5 seconds HitRate_0.5R : Recall of hit rate at 0.5 seconds HitRate_t3F : F-measure of hit rate at 3 seconds (trimmed) HitRate_t3P : Precision of hit rate at 3 seconds (trimmed) HitRate_t3F : Recall of hit rate at 3 seconds (trimmed) HitRate_t0.5F : F-measure of hit rate at 0.5 seconds (trimmed) HitRate_t0.5P : Precision of hit rate at 0.5 seconds (trimmed) HitRate_t0.5R : Recall of hit rate at 0.5 seconds (trimmed) DevA2E : Median deviation of annotation to estimation DevE2A : Median deviation of estimation to annotation D : Information gain PWF : F-measure of pair-wise frame clustering PWP : Precision of pair-wise frame clustering PWR : Recall of pair-wise frame clustering Sf : F-measure normalized entropy score So : Oversegmentation normalized entropy score Su : Undersegmentation normalized entropy score """ logging.info("Evaluating %s" % os.path.basename(est_file)) res = {} ### Boundaries ### # Hit Rate res["HitRate_3P"], res["HitRate_3R"], res["HitRate_3F"] = \ mir_eval.segment.detection(ann_inter, est_inter, window=3, trim=False) res["HitRate_0.5P"], res["HitRate_0.5R"], res["HitRate_0.5F"] = \ mir_eval.segment.detection(ann_inter, est_inter, window=.5, trim=False) res["HitRate_t3P"], res["HitRate_t3R"], res["HitRate_t3F"] = \ mir_eval.segment.detection(ann_inter, est_inter, window=3, trim=True) res["HitRate_t0.5P"], res["HitRate_t0.5R"], res["HitRate_t0.5F"] = \
mir_eval.segment.detection(ann_inter, est_inter, window=.5, trim=True) # Information gain res["D"] = compute_information_gain(ann_inter, est_inter, est_file, bins=bins) # Median Deviations res["DevR2E"], res["DevE2R"] = mir_eval.segment.deviation( ann_inter, est_inter, trim=False) res["DevtR2E"], res["DevtE2R"] = mir_eval.segment.deviation( ann
_inter, est_inter, trim=True) ### Labels ### if est_labels is not None and len(est_labels) != 0: try: # Align labels with intervals ann_labels = list(ann_labels) est_labels = list(est_labels) ann_inter, ann_labels = mir_eval.util.adjust_intervals(ann_inter, ann_labels) est_inter, est_labels = mir_eval.util.adjust_intervals( est_inter, est_labels, t_min=0, t_max=ann_inter.max()) # Pair-wise frame clustering res["PWP"], res["PWR"], res["PWF"] = mir_eval.segment.pairwise( ann_inter, ann_labels, est_inter, est_labels) # Normalized Conditional Entropies res["So"], res["Su"], res["Sf"] = mir_eval.segment.nce( ann_inter, ann_labels, est_inter, est_labels) except: logging.warning("Labeling evaluation failed in file: %s" % est_file) return {} # Names base = os.path.basename(est_file) res["track_id"] = base[:-5] res["ds_name"] = base.split("_")[0] return res def compute_gt_results(est_file, ref_file, boundaries_id, labels_id, config, bins=251, annotator_id=0): """Computes the results by using the ground truth dataset identified by the annotator parameter. Return ------ results : dict Dictionary of the results (see function compute_results). """ # Get the ds_prefix ds_prefix = os.path.basename(est_file).split("_")[0] # Get context if ds_prefix in msaf.prefix_dict.keys(): context = msaf.prefix_dict[ds_prefix] else: context = "function" try: # TODO: Read hierarchical annotations if config["hier"]: ref_times, ref_labels, ref_levels = \ msaf.io.read_hier_references(ref_file, annotation_id=0, exclude_levels=["function"]) else: ref_inter, ref_labels = jams2.converters.load_jams_range( ref_file, "sections", annotator=annotator_id, context=context) except: logging.warning("No references for file: %s" % ref_file) return {} # Read estimations with correct configuration est_inter, est_labels = io.read_estimations(est_file, boundaries_id, labels_id, **config) if len(est_inter) == 0: logging.warning("No estimations for file: %s" % est_file) return {} # Compute the results and return if config["hier"]: # Hierarchical assert len(est_inter) == len(est_labels), "Same number of levels " \ "are required in the boundaries and labels for the hierarchical " \ "evaluation." est_times = [] est_labels = [] # Sort based on how many segments per level est_inter = sorted(est_inter, key=lambda level: len(level)) for inter in est_inter: est_times.append(msaf.utils.intervals_to_times(inter)) # Add fake labels (hierarchical eval does not use labels --yet--) est_labels.append(np.ones(len(est_times[-1]) - 1) * -1) # Align the times utils.align_end_hierarchies(est_times, ref_times) # Build trees ref_tree = mir_eval.segment.tree.SegmentTree(ref_times, ref_labels, ref_levels) est_tree = mir_eval.segment.tree.SegmentTree(est_times, est_labels) # Compute evaluations res = {} res["t_recall10"], res["t_precision10"], res["t_measure10"] = \ mir_eval.segment.hmeasure(ref_tree, est_tree, window=100) res["t_recall15"], res["t_precision15"], res["t_measure15"] = \ mir_eval.segment.hmeasure(ref_tree, est_tree, window=150) res["t_recall30"], res["t_precision30"], res["t_measure30"] = \ mir_eval.segment.hmeasure(ref_tree, est_tree, window=300) res["track_id"] = os.path.basename(est_file)[:-5] return res else: # Flat return compute_results(ref_inter, est_inter, ref_labels, est_labels, bins, est_file) def compute_information_gain(ann_inter, est_inter, est_file, bins): """Computes the information gain of the est_file from the annotated intervals and the estimated intervals.""" ann_times = utils.intervals_to_times(ann_inter) est_times = utils.intervals_to_times(est_inter) try: D = mir_eval.beat.information_gain(ann_times, est_times, bins=bins) except: logging.warning("Couldn
hefen1/chromium
tools/telemetry/telemetry/results/results_options.py
Python
bsd-3-clause
7,550
0.009669
# 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. import optparse import os import sys from telemetry.core import util from telemetry.results import buildbot_output_formatter from telemetry.results import chart_json_output_formatter from telemetry.results import csv_output_formatter from telemetry.results import csv_pivot_table_output_formatter from telemetry.results import gtest_progress_reporter from telemetry.results import html_output_formatter from telemetry.results import json_output_formatter from telemetry.results import page_test_results from telemetry.results import progress_reporter # Allowed output formats. The default is the first item in the list. _OUTPUT_FORMAT_CHOICES = ('html', 'buildbot', 'csv', 'gtest', 'json', 'chartjson', 'csv-pivot-table', 'none') # Filenames to use for given output formats. _OUTPUT_FILENAME_LOOKUP = { 'html': 'results.html', 'csv': 'results.csv', 'json': 'results.json', 'chartjson': 'results-chart.json', 'csv-pivot-table': 'results-pivot-table.csv' } def AddResultsOptions(parser): group = optparse.OptionGroup(parser, 'Results options') group.add_option('--chartjson', action='store_true', help='Output Chart JSON. Ignores --output-format.') group.add_option('--output-format', action='append', dest='output_formats', choices=_OUTPUT_FORMAT_CHOICES, default=[], help='Output format. Defaults to "%%default". ' 'Can be %s.' % ', '.join(_OUTPUT_FORMAT_CHOICES)) group.add_option('-o', '--output', dest='output_file', default=None, help='Redirects output to a file. Defaults to stdout.') group.add_option('--output-dir', default=util.GetBaseDir(), help='Where to save output data after the run.') group.add_option('--output-trace-tag', default='', help='Append a tag to the key of each result trace. Use ' 'with html, buildbot, csv-pivot-table output formats.') group.add_option('--reset-results', action='store_true', help='Delete all stored results.') group.add_option('--upload-results', action='store_true', help='Upload the results to cloud storage.') group.add_option('--upload-bucket', default='internal', choices=['public', 'partner', 'internal'], help='Storage bucket to use for the uploaded results. ' 'Defaults to internal. Supported values are: ' 'public, partner, internal') group.add_option('--results-label', default=None, help='Optional label to use for the results of a run .') group.add_option('--suppress_gtest_report', default=False, help='Whether to suppress GTest progress report.') parser.add_option_group(group) def ProcessCommandLineArgs(parser, args): # TODO(ariblue): Delete this flag entirely at some future data, when the # existence of such a flag has been long forgotten. if args.output_file: parser.error('This flag is deprecated. Please use --output-dir instead.') try: os.makedirs(args.output_dir) except OSError: # Do nothing if the output directory already exists. Existing files will # get overwritten. pass args.output_dir = os.path.expanduser(args.output_dir) def _GetOutputStream(output_format, output_dir): assert output_format in _OUTPUT_FORMAT_CHOICES, 'Must specify a valid format.' assert output_format not in ('gtest', 'none'), ( 'Cannot set stream for \'gtest\' or \'none\' output formats.') if output_format == 'buildbot': return sys.stdout assert output_format in _OUTPUT_FILENAME_LOOKUP, ( 'No known filename for the \'%s\' output format' % output_format) output_file = os.path.join(output_dir, _OUTPUT_FILENAME_LOOKUP[output_format]) open(output_file, 'a').close() # Create file if it doesn't exist. return open(output_file, 'r+') def _GetProgressReporter(output_skipped_tests_summary, suppress_gtest_report): if suppress_gtest_report: return progress_reporter.ProgressReporter() return gtest_progress_reporter.GTestProgressReporter( sys.stdout, output_skipped_tests_summary=output_skipped_tests_summary) def CreateResults(benchmark_metadata, options, value_can_be_added_predicate=lambda v: True): """ Args: options: Contains the options specified in AddResultsOptions. """ if not options.output_formats: options.output_formats = [_OUTPUT_FORMAT_CHOICES[0]] output_formatters = [] for output_format in options.output_formats: if output_format == 'none' or output_format == "gtest" or options.chartjson: continue output_stream = _GetOutputStream(output_format, options.output_dir) if output_format == 'csv': output_formatters.append(csv_output_formatter.CsvOutputFormatter( output_stream)) elif output_format == 'csv-pivot-table': output_formatters.append( csv_pivot_table_output_formatter.CsvPivotTableOutputFormatter( output_stream, trace_tag=options.output_trace_tag)) elif output_format == 'buildbot': output_formatters.append( buildbot_output_formatter.BuildbotOutputFormatter( output_stream, trace_tag=options.output_trace_tag)) elif output_format == 'html': # TODO(chrishenry): We show buildbot output so that users can grep # through the results easily without needing to open the html # file. Another option for this is to output the results directly # in gtest-style results (via some s
ort of progress reporter), # as we plan to enable gtest-style output for all output formatters. output_formatters.append( buildbot_output_formatter.BuildbotOutputFormatter( sys.stdout, trace_tag=options.output_trace_tag)) output_formatters.append(html_output_formatter.HtmlOutputFormatter( output_stream, benchmark_metadata, options.reset_results, options.upload_resul
ts, options.browser_type, options.results_label, trace_tag=options.output_trace_tag)) elif output_format == 'json': output_formatters.append(json_output_formatter.JsonOutputFormatter( output_stream, benchmark_metadata)) elif output_format == 'chartjson': output_formatters.append( chart_json_output_formatter.ChartJsonOutputFormatter( output_stream, benchmark_metadata)) else: # Should never be reached. The parser enforces the choices. raise Exception('Invalid --output-format "%s". Valid choices are: %s' % (output_format, ', '.join(_OUTPUT_FORMAT_CHOICES))) # TODO(chrishenry): This is here to not change the output of # gtest. Let's try enabling skipped tests summary for gtest test # results too (in a separate patch), and see if we break anything. output_skipped_tests_summary = 'gtest' in options.output_formats reporter = _GetProgressReporter(output_skipped_tests_summary, options.suppress_gtest_report) return page_test_results.PageTestResults( output_formatters=output_formatters, progress_reporter=reporter, output_dir=options.output_dir, value_can_be_added_predicate=value_can_be_added_predicate)
h4ck3rm1k3/FEC-Field-Documentation
fec/version/v2/F7.py
Python
unlicense
1,133
0.001765
import fechbase class Records(fechbase.RecordsBase): def __init__(self): fechbase.RecordsBase.__init__(self) self.fields = [ {'name': 'FORM TYPE', 'number': '1'}, {'name': 'FILER FEC CMTE ID', 'number': '2'}, {'name': 'COMMITTEE NAME', 'number': '3'}, {'name': 'STREET 1', 'number': '4'}, {'name': 'STREET 2', 'number': '5'}, {'name': 'CITY', 'number
': '6'}, {'name': 'STATE', 'number': '7'}, {'name': 'ZIP', 'num
ber': '8'}, {'name': 'ORGANIZATION TYPE', 'number': '9'}, {'name': 'RPTCODE', 'number': '10'}, {'name': 'OF ELECTION', 'number': '11-'}, {'name': 'STATE (OF ELECTION)', 'number': '12'}, {'name': 'COVERAGE FROM', 'number': '13-'}, {'name': 'COVERAGE TO', 'number': '14-'}, {'name': 'TOTAL COSTS', 'number': '15'}, {'name': 'FILER', 'number': '16-'}, {'name': 'SIGNED', 'number': '17-'}, {'name': 'TITLE', 'number': '18'}, ] self.fields_names = self.hash_names(self.fields)
TQRG/physalia
physalia/tests/test_energy_profiler.py
Python
mit
534
0.001873
"""Test energy_profiler module.""" import unittest from physalia.energy_profiler import AndroidUseCase # pylint: disable=missing-docstring class TestEnergyProfiler(unittest.TestCase): d
ef test_empty_android_use_case(self): # pylint: disable=no-self-use use_case = AndroidUseCase( name="Test", app_apk="no/path", app_pkg="no.package", app_version="0.0.0", run=None, prepare=No
ne, cleanup=None ) use_case.run()
PolygonalTree/Led-control
led-control/setup.py
Python
agpl-3.0
909
0.0011
""" Setup file for led-controller author: Luis Garcia Rodriguez 2017 Licence: GPLv3 """ from setuptools import setup, find_packages from codecs import open from os import path here = path.abspath(path.dirname(__file__)) setup( name='led-controller', version='2.0.0', description='A simple interface for controlling LEDs in circadian experime
nts', # The project's main homepage. url='https://gith
ub.com/polygonaltree/Led-control', # Author details author='Luis Garcia Rodriguez', author_email='[email protected]', license='GPLv3', packages=find_packages(exclude=['contrib', 'docs', 'tests']), # Alternatively, if you want to distribute just a my_module.py, uncomment # this: # py_modules=["my_module"] install_requires=['pyside2'], entry_points={ 'console_scripts': [ 'led-controller=gui:main', ], }, )
fusionbox/satchless
satchless/contrib/checkout/multistep/tests.py
Python
bsd-3-clause
16,546
0.00278
# -*- coding: utf-8 -*- from decimal import Decimal from django.http import HttpResponse, HttpRequest from django.conf import settings from django.conf.urls import patterns, include, url from django.core.urlresolvers import reverse from django.test import TestCase, Client from ....cart.app import cart_app from ....cart.models import CART_SESSION_KEY from ....cart.tests import TestCart from ....contrib.delivery.simplepost.models import PostShippingType from ....order import handler as order_handler from ....order.models import Order from ....payment import ConfirmationFormNeeded from ....payment.tests import TestPaymentProvider from ....product.tests import DeadParrot from ..common.decorators import require_order from ..common.views import prepare_order, reactivate_order from . import views urlpatterns = patterns('', url(r'^cart/', include(cart_app.urls)), url(r'^checkout/', include('satchless.contrib.checkout.multistep.urls')), url(r'^order/', include('satchless.order.urls')), ) class TestPaymentProviderWithConfirmation(TestPaymentProvider): def confirm(self, order): raise ConfirmationFormNeeded(action='http://test.payment.gateway.example.com') class CheckoutTest(TestCase): urls = 'satchless.contrib.checkout.multistep.tests' def _setup_settings(self, custom_settings): original_settings = {} for setting_name, value in custom_settings.items(): if hasattr(settings, setting_name): original_settings[setting_name] = getattr(settings, setting_name) setattr(settings, setting_name, value) return original_settings def _teardown_settings(self, original_settings, custom_settings=None): custom_settings = custom_settings or {} for setting_name, value in custom_settings.items(): if setting_name in original_settings: setattr(settings, setting_name, value) else: delattr(settings, setting_name) def setUp(self): self.macaw = DeadParrot.objects.create(slug='macaw', species="Hyacinth Macaw") self.cockatoo = DeadParrot.objects.create(slug='cockatoo', species="White Cockatoo") self.macaw_blue = self.macaw.variants.create(color='blue', looks_alive=False) self.macaw_blue_fake = self.macaw.variants.create(color='blue', looks_alive=True) self.cockatoo_white_a = self.cockatoo.variants.create(color='white', looks_alive=True) self.cockatoo_white_d = self.cockatoo.variants.create(color='white', looks_alive=False) self.cockatoo_blue_a = self.cockatoo.variants.create(color='blue', looks_alive=True) self.cockatoo_blue_d = self.cockatoo.variants.create(color='blue', looks_alive=False) self.custom_settings = { 'SATCHLESS_DELIVERY_PROVIDERS': ['satchless.contrib.delivery.simplepost.PostDeliveryProvider'], 'SATCHLESS_ORDER_PARTITIONERS': ['satchless.contrib.order.partitioner.simple'], 'SATCHLESS_PAYMENT_PROVIDERS': [TestPaymentProviderWithConfirmation], 'SATCHLESS_DJANGO_PAYMENT_TYPES': ['dummy'], 'PAYMENT_VARIANTS': {'dummy': ('payments.dummy.DummyProvider', {'url': '/', })}, } self.original_settings = self._setup_settings(self.custom_settings) order_handler.init_queues() self.anon_client = Client() PostShippingType.objects.create(price=12, typ='polecony', name='list polecony') PostShippingType.objects.create(price=20, typ='list', name='List zwykly') def tearDown(self): self._teardown_settings(self.original_settings, self.custom_settings) order_handler.init_queues() def _test_status(self, url, method='get', *args, **kwargs): status_code = kwargs.pop('status_code', 200) client = kwargs.pop('client_instance', Client()) data = kwargs.pop('data', {}) response = getattr(client, method)(url, data=data, follow=False) self.assertEqual(response.status_code, status_code, 'Incorrect status code for: %s, (%s, %s)! Expected: %s, received: %s. HTML:\n\n%s' % ( url.decode('utf-8'), args, kwargs, status_code, response.status_code, response.content.decode('utf-8'))) return response def _get_or_create_cart_for_client(self, client, typ='satchless_cart'): self._test_status(reverse('satchless-cart-view'), client_instance=self.anon_client) return TestCart.objects.get(pk=self.anon_client.session[CART_SESSION_KEY % typ], typ=typ) def _get_order_from_session(self, session): order_pk = session.get('satchless_order', None) if order_pk: return Order.objects.get(pk=order_pk) return None def _
get_order_items(self, order): order_ite
ms = set() for group in order.groups.all(): order_items.update(group.items.values_list('product_variant', 'quantity')) return order_items def test_order_from_cart_view_creates_proper_order(self): cart = self._get_or_create_cart_for_client(self.anon_client) cart.replace_item(self.macaw_blue, 1) cart.replace_item(self.macaw_blue_fake, Decimal('2.45')) cart.replace_item(self.cockatoo_white_a, Decimal('2.45')) self._test_status(reverse(prepare_order), method='post', client_instance=self.anon_client, status_code=302) order = self._get_order_from_session(self.anon_client.session) self.assertNotEqual(order, None) order_items = self._get_order_items(order) self.assertEqual(set(cart.items.values_list('variant', 'quantity')), order_items) def test_order_is_updated_after_cart_changes(self): cart = self._get_or_create_cart_for_client(self.anon_client) cart.replace_item(self.macaw_blue, 1) cart.replace_item(self.macaw_blue_fake, Decimal('2.45')) cart.replace_item(self.cockatoo_white_a, Decimal('2.45')) self._test_status(reverse(prepare_order), method='post', client_instance=self.anon_client, status_code=302) order = self._get_order_from_session(self.anon_client.session) order_items = self._get_order_items(order) # compare cart and order self.assertEqual(set(cart.items.values_list('variant', 'quantity')), order_items) # update cart cart.add_item(self.macaw_blue, 100) cart.add_item(self.macaw_blue_fake, 100) self._test_status(reverse(prepare_order), method='post', client_instance=self.anon_client, status_code=302) old_order = order order = self._get_order_from_session(self.anon_client.session) # order should be reused self.assertEqual(old_order.pk, order.pk) self.assertNotEqual(order, None) order_items = self._get_order_items(order) # compare cart and order self.assertEqual(set(cart.items.values_list('variant', 'quantity')), order_items) def test_prepare_order_creates_order_and_redirects_to_checkout_when_cart_is_not_empty(self): cart = self._get_or_create_cart_for_client(self.anon_client) cart.replace_item(self.macaw_blue, 1) response = self._test_status(reverse(prepare_order), method='post', client_instance=self.anon_client, status_code=302) order_pk = self.anon_client.session.get('satchless_order', None) order = Order.objects.get(pk=order_pk) self.assertRedirects(response, reverse(views.checkout, kwargs={'order_token': order.token})) def test_prepare_order_redirects_to_cart_when_cart_is_empty(self): self._get_or_create_cart_for_client(self.anon_client) response = self._test_status(reverse(prepare_order), method='post', client_instance=self.anon_client, status_code=302) # 'satchless_cart' is taken from multistep/urls.py: # url(r'^prepare-order/$', prepare_order, {'typ': 'satchle
code-shoily/tornado-cljs
handlers/base.py
Python
mit
264
0.003788
from tornado.web import RequestHandler class BaseHandler(RequestHandler): def initialize(self): _settings = self.application.settings self.db = self.ap
plication.db #self.redis = _settings["redis"] sel
f.log = _settings["log"]
stencila/hub
manager/projects/migrations/0024_auto_20201206_0819.py
Python
apache-2.0
746
0.00134
# Generated by Django 3.1.4 on 2020-12-06 08:19 from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ ('projects', '0023_auto_20201202_0349'), ] operations = [ migrations.AlterField( model_name='review', name='status', field=models.CharField(choices=[('PENDING', 'Pe
nding'), ('REQUESTED', 'Requested'), ('CANCELLED', 'Cancelled'), ('ACCEPTED', 'Accepted'), ('DECLINED', 'Declined'), ('COMPLETED', 'Completed'), ('EXTRACTING', 'Retrieval in progress'), ('EXTRACTED', 'Retrieved'), ('FAILED', 'Retrieval failed'), ('REGISTERED', 'Registered')], default
='PENDING', help_text='The status of the review.', max_length=16), ), ]
evernym/plenum
plenum/test/recorder/test_recorder.py
Python
apache-2.0
6,601
0.001666
import random import time from collections import OrderedDict from plenum.common.util import randomString try: import ujson as json except ImportError: import json import pytest from plenum.recorder.recorder import Recorder TestRunningTimeLimitSec = 350 def test_add_to_recorder(recorder): last_check_time = recorder.get_now_key() time.sleep(1) msg1, frm1 = 'm1', 'f1' msg2, frm2 = 'm2', 'f2' recorder.add_incoming(msg1, frm1) time.sleep(3) recorder.add_incoming(msg2, frm2) time.sleep(2.1) msg3, to1, to11 = 'm3', 't1', 't11' msg4, to2 = 'm4', 't2' recorder.add_outgoing(msg3, to1, to11) time.sleep(.4) recorder.add_outgoing(msg4, to2) time.sleep(.5) recorder.add_disconnecteds('a', 'b', 'c') i = 0 for k, v in recorder.store.iterator(include_value=True): assert int(k.decode()) > int(last_check_time) if i == 0: assert v.decode() == json.dumps([[Recorder.INCOMING_FLAG, msg1, frm1]]) if i == 1: assert v.decode() == json.dumps([[Recorder.INCOMING_FLAG, msg2, frm2]]) assert int(k) - int(last_check_time) >= 3 * Recorder.TIME_FACTOR if i == 2: assert v.decode() == json.dumps([[Recorder.OUTGOING_FLAG, msg3, to1, to11]]) assert int(k) - int(last_check_time) >= 2.1 * Recorder.TIME_FACTOR if i == 3: assert v.decode() == json.dumps([[Recorder.OUTGOING_FLAG, msg4, to2]]) assert int(k) - int(last_check_time) >= .4 * Recorder.TIME_FACTOR if i == 4: assert v.decode() == json.dumps([[Recorder.DISCONN_FLAG, 'a', 'b', 'c']]) assert int(k) - int(last_check_time) >= .5 * Recorder.TIME_FACTOR last_check_time = k.decode() i += 1 def test_get_list_from_recorder(recorder): msg1, frm1 = 'm1', 'f1' msg2, frm2 = 'm2', 'f2' msg3, to1, to11 = 'm3', 't1', 't11' # Decrease resolution recorder.TIME_FACTOR = 1 time.sleep(1) recorder.add_outgoing(msg3, to1, to11) recorder.add_incoming(msg1, frm1) recorder.add_incoming(msg2, frm2) recorder.add_disconnecteds('a', 'b', 'c') for k, v in recorder.store.iterator(include_value=True): assert v.decode() == json.dumps([ [Recorder.OUTGOING_FLAG, 'm3', 't1', 't11'], [Recorder.INCOMING_FLAG, 'm1', 'f1'], [Recorder.INCOMING_FLAG, 'm2', 'f2'], [Recorder.DISCONN_FLAG, 'a', 'b', 'c'] ]) def test_register_play_targets(recorder): l1 = [] l2 = [] def add1(arg): l1.append(arg) def add2(arg): l2.append(arg) assert not recorder.replay_targets recorder.register_replay_target('1', add1) assert len(recorder.replay_targets) == 1 with pytest.raises(AssertionError): recorder.register_replay_target('1', add2) def test_recorded_parsings(recorder): incoming = [[randomString(10), randomString(6)] for i in range(3)] outgoing = [[randomString(10), randomString(6)] for i in range(5)] for m, f in incoming: recorder.add_incoming(m, f) time.sleep(0.01) for m, f in outgoing: recorder.add_outgoing(m, f) time.sleep(0.01) with pytest.raises(AssertionError): recorder.get_parsed(incoming[0], only_incoming=True, only_outgoing=True) combined = incoming + outgoing def su
blist(lst1, lst2): ls1 = [element for element in lst1 if element i
n lst2] ls2 = [element for element in lst2 if element in lst1] return ls1 == ls2 for k, v in recorder.store.iterator(include_value=True): p = Recorder.get_parsed(v) assert sublist([i[1:] for i in p] , combined) p = Recorder.get_parsed(v, only_incoming=True) if p: assert sublist(p, incoming) for i in p: incoming.remove(i) p = Recorder.get_parsed(v, only_outgoing=True) if p: assert sublist(p, outgoing) for i in p: outgoing.remove(i) assert not incoming assert not outgoing def test_recorder_get_next_incoming_only(recorder): incoming_count = 100 incoming = [(randomString(100), randomString(6)) for _ in range(incoming_count)] while incoming: recorder.add_incoming(*incoming.pop()) time.sleep(random.choice([0, 1]) + random.random()) recorded_incomings = OrderedDict() keys = [] for k, v in recorder.store.iterator(include_value=True): v = Recorder.get_parsed(v) keys.append(int(k)) recorded_incomings[int(k)] = v assert len(recorded_incomings) == incoming_count assert sorted(keys) == keys max_time_to_run = incoming_count * 2 + 10 recorder.start_playing() start = time.perf_counter() while recorder.is_playing and (time.perf_counter() < start + max_time_to_run): vals = recorder.get_next() if vals: check = recorded_incomings.popitem(last=False)[1] assert check == vals else: time.sleep(0.01) assert len(recorded_incomings) == 0 assert not recorder.is_playing def test_recorder_get_next(recorder): incoming_count = 100 outgoing_count = 50 incoming = [(randomString(100), randomString(6)) for _ in range(incoming_count)] outgoing = [(randomString(100), randomString(6)) for _ in range(outgoing_count)] while incoming or outgoing: if random.choice([0, 1]) and outgoing: recorder.add_outgoing(*outgoing.pop()) time.sleep(random.choice([0, 1]) + random.random()) elif incoming: recorder.add_incoming(*incoming.pop()) time.sleep(random.choice([0, 1]) + random.random()) else: continue recorded_incomings = OrderedDict() for k, v in recorder.store.iterator(include_value=True): v = Recorder.get_parsed(v, only_incoming=True) if v: recorded_incomings[int(k)] = v assert len(recorded_incomings) == incoming_count max_time_to_run = incoming_count * 2 + 10 recorder.start_playing() start = time.perf_counter() while recorder.is_playing and (time.perf_counter() < start + max_time_to_run): vals = recorder.get_next() if vals: inc = Recorder.filter_incoming(vals) if inc: assert recorded_incomings.popitem(last=False)[1] == inc else: time.sleep(0.01) assert len(recorded_incomings) == 0 assert not recorder.is_playing
JaeGyu/PythonEx_1
20160124_1.py
Python
mit
473
0.07611
#_*_ coding: utf-8 _*_ t1 = () print type(t1) t3 = 1,2,3 print type(t3) r1 = (1) print r1 print type(r1) r1 = (1,) print r1 print type(r1) t = (1,2,3) print t*2 print t
+('aaa','bbb') print t print print t[0], t[1:3] print len(t) print 1 in t print range(1,3) t= (12345,54321,'hhh') u = t,(1,2,3,4,5) print u t2 = [1,2,3] u2 = t2,(1,2,4) print u2 t3 = {1:'ggg',2:'hhh'} u3 = t3,(1,2,3) print u3 x,y,z=1,2,3 print x print y print z t = 1,
2,'hello' x,y,z = t
GjjvdBurg/HugoPhotoSwipe
hugophotoswipe/__init__.py
Python
gpl-3.0
62
0
# -*- coding: utf-8
-*- from .
__version__ import __version__
henryneu/Python
sample/exten.py
Python
apache-2.0
614
0.034202
#!/usr/bin/env python3 # -*- coding: utf-8 -*- class Animal(object): def run(self): print('Animal running...') class Dog(Animal):
def run(self): print('Dog running...') def shout(self): print('Dog wang wang...') class Cat(Animal): def run(self): print('Cat running...') def shout(self): print('Cat miao miao...') class Pig(Animal): def run(self): print('Pig running slowly...') def run_twice(animal): animal.run() animal.run() dog = Dog() cat = Cat() print(dog.run()) print(cat.run()) print(run_twice(Animal())) print(run_twice(Dog()))
print(run_twice(Cat())) print(run_twice(Pig()))
weidnem/IntroPython2016
Solutions/Session01/codingbat/Warmup-1/pos_neg.py
Python
unlicense
736
0.002717
#!/usr/bin/env python def pos_neg(a, b, negative): if negative: return
a < 0 and b < 0 else: return (a < 0 and b > 0) or (a > 0 and b < 0) if __name__ == "__main__": # run some tests if run as script # (from the codingbat site -- not all, I got bored) assert pos_neg(1, -1, False) is True assert pos_neg(-1, 1, False) is True assert pos_neg(-4, -5, True) is True assert pos_neg(-4, -5, False) is False assert pos_
neg(-4, -5, True) is True assert pos_neg(-6, -6, False) is False assert pos_neg(-2, -1, False) is False assert pos_neg(1, 2, False) is False assert pos_neg(-5, 6, True) is False assert pos_neg(-5, -5, True) is True print "all tests passed"
landism/pants
src/python/pants/engine/scheduler.py
Python
apache-2.0
21,934
0.008252
# coding=utf-8 # Copyright 2015 Pants project contributors (see CONTRIBUTORS.md). # Licensed under the Apache License, Version 2.0 (see LICENSE). from __future__ import (absolute_import, division, generators, nested_scopes, print_function, unicode_literals, with_statement) import logging import os import threading import time from collections import defaultdict from contextlib import contextmanager from pants.base.exceptions import TaskError from pants.base.project_tree import Dir, File, Link from pants.build_graph.address import Address from pants.engine.addressable import SubclassesOf from pants.engine.fs import FileContent, FilesContent, Path, PathGlobs, Snapshot from pants.engine.isolated_process import _Snapshots, create_snapshot_rules from pants.engine.nodes import Return, State, Throw from pants.engine.rules import RuleIndex, SingletonRule, TaskRule from pants.engine.selectors import (Select, SelectDependencies, SelectProjection, SelectTransitive, SelectVariant, constraint_for) from pants.engine.struct import HasProducts, Variants from pants.engine.subsystem.native import Function, TypeConstraint, TypeId from pants.util.contextutil import temporary_file_path from pants.util.objects import datatype logger = logging.getLogger(__name__) class ExecutionRequest(datatype('ExecutionRequest', ['roots'])): """Holds the roots for an execution, which might have been requested by a user. To create an ExecutionRequest, see `LocalScheduler.build_request` (which performs goal translation) or `LocalScheduler.execution_request`. :param roots: Roots for this request. :type roots: list of tuples of subject and product. """ class ExecutionResult(datatype('ExecutionResult', ['error', 'root_products'])): """Represents the result of a single execution.""" @classmethod def finished(cls, root_products): """Create a success or partial success result from a finished run. Runs can either finish with no errors, satisfying all promises, or they can partially finish if run in fail-slow mode producing as many products as possible. :param root_products: List of ((subject, product), State) tuples. :rtype: `ExecutionResult` """ return cls(error=None, root_products=root_products) @classmethod def failure(cls, error): """Create a failure result. A failure result represent a run with a fatal error. It presents the error but no products. :param error: The execution error encountered. :type error: :class:`pants.base.exceptions.TaskError` :rtype: `ExecutionResult` """ return cls(error=error, root_products=None) class ExecutionError(Exception): pass class WrappedNativeScheduler(object): def __init__(self, native, build_root, work_dir, ignore_patterns, rule_index): self._native = native # TODO: The only (?) case where we use inheritance rather than exact type unions. has_products_constraint = SubclassesOf(HasProducts) self._root_subject_types = sorted(rule_index.roots) # Create the ExternContext, and the native Scheduler. self._tasks = native.new_tasks() self._register_rules(rule_index) self._scheduler = native.new_scheduler( self._tasks, self._root_subject_types, build_root, work_dir, ignore_patterns, Snapshot, _Snapshots, FileContent, FilesContent, Path, Dir, File, Link, has_products_constraint, constraint_for(Address), constraint_for(Variants), constraint_for(PathGlobs), constraint_for(Snapshot), constraint_for(_Snapshots), constraint_for(FilesContent), constraint_for(Dir), constraint_for(File), constraint_for(Link), ) def _root_type_ids(self): return self._to_ids_buf(sorted(self._root_subject_types)) def graph_trace(self): with temporary_file_path() as path: self._native.lib.graph_trace(self._scheduler, bytes(path)) with open(path) as fd: for line in fd.readlines(): yield line.rstrip() def assert_ruleset_valid(self): raw_value = self._native.lib.validator_run(self._scheduler) value = self._from_value(raw_value) if isinstance(value, Exception): raise ValueError(str(value)) def _to_value(self, obj): return self._native.context.to_value(obj) def _from_value(self, val): return self._native.context.from_value(val) def _to_id(self, typ): return self._native.context.to_id(typ) def _to_key(self, obj): return self._native.context.to_key(obj) def _from_id(self, cdata): return self._native.context.from_id(cdata) def _from_key(self, cdata): return self._native.context.from_key(cdata) def _to_constraint(self, type_or_constraint): return TypeConstraint(self._to_id(constraint_for(type_or_constraint))) def _to_ids_buf(self, types): return self._native.to_id
s_buf(types) def _to_utf8_buf(self, string): return self._native.context
.utf8_buf(string) def _register_rules(self, rule_index): """Record the given RuleIndex on `self._tasks`.""" registered = set() for product_type, rules in rule_index.rules.items(): # TODO: The rules map has heterogeneous keys, so we normalize them to type constraints # and dedupe them before registering to the native engine: # see: https://github.com/pantsbuild/pants/issues/4005 output_constraint = self._to_constraint(product_type) for rule in rules: key = (output_constraint, rule) if key in registered: continue registered.add(key) if type(rule) is SingletonRule: self._register_singleton(output_constraint, rule) elif type(rule) is TaskRule: self._register_task(output_constraint, rule) else: raise ValueError('Unexpected Rule type: {}'.format(rule)) def _register_singleton(self, output_constraint, rule): """Register the given SingletonRule. A SingletonRule installed for a type will be the only provider for that type. """ self._native.lib.tasks_singleton_add(self._tasks, self._to_value(rule.value), output_constraint) def _register_task(self, output_constraint, rule): """Register the given TaskRule with the native scheduler.""" input_selects = rule.input_selectors func = rule.func self._native.lib.tasks_task_begin(self._tasks, Function(self._to_id(func)), output_constraint) for selector in input_selects: selector_type = type(selector) product_constraint = self._to_constraint(selector.product) if selector_type is Select: self._native.lib.tasks_add_select(self._tasks, product_constraint) elif selector_type is SelectVariant: key_buf = self._to_utf8_buf(selector.variant_key) self._native.lib.tasks_add_select_variant(self._tasks, product_constraint, key_buf) elif selector_type is SelectDependencies: self._native.lib.tasks_add_select_dependencies(self._tasks, product_constraint, self._to_constraint(selector.dep_product), self._to_utf8_buf(selector.field), self._to_ids_buf(selector.field_types)) elif selector_type is SelectTransitive: self._native.lib.tasks_add_select_transitive(self._tasks, product_constraint, self._to_constraint(selector.dep_product), self._to_utf8_buf(selector.field), self._to_ids_buf(selector.field_types)) elif selector_type is SelectProjection: self._native.lib.ta
kevin-coder/tensorflow-fork
tensorflow/python/data/experimental/kernel_tests/serialization/choose_fastest_branch_dataset_serialization_test.py
Python
apache-2.0
3,611
0.008031
# Copyright 2019 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. # ============================================================================== """Tests for the ChooseFastestBranchDataset serialization.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from tensorflow.python.data.experimental.kernel_tests.serialization import dataset_serialization_test_base from tensorflow.python.data.experimental.ops import batching from tensorflow.python.data.experimental.ops import optimization from tensorflow.python.data.ops import dataset_ops from tensorflow.python.framework import constant_op from tensorflow.python.framework import dtypes from tensorflow.python.ops import math_ops from tensorflow.python.platform import test class ChooseFastestBranchDatasetSerializationTest( dataset_serialization_test_base.DatasetSerializationTestBase): def testCore(self): def build_ds(size): dataset = dataset_ops.Dataset.range(size) def branch_0(dataset): return dataset.map(lambda x: x).batch(10) def branch_1(dataset): return dataset.batch(10).map(lambda x: x) return optimization._ChooseFastestBranchDataset( # pylint: disable=protected-access dataset, [branch_0, branch_1], ratio_numerator=10) for size in [100, 1000]: self.run_core_tests(lambda: build_ds(size), None, size // 10) # pylint: disable=cell-var-from-loop def testWithCapture(self): def build_ds(): dataset = dataset_ops.Dataset.range(10) const_64 = constant_op.constant(1, dtypes.int64) const_32 = constant_op.constant(1, dtypes.int32) def branch_0(dataset): return dataset.map(lambda x: x + const_64) def branch_1(dataset): return dataset.map(lambda x: x + math_ops.cast(const_32, dtypes.int64)) return optimization._ChooseFastestBranchDataset( dataset, [branch_0, branch_1], num_elements_per_branch=3) self.run_core_tests(build_ds, None, 10) def testWithPrefetch(self): def build_ds(): dataset = dataset_ops.Dataset.range(10) const_64 = constant_op.constant(1, dtypes.int64) const_32 = constant_op.constant(1, dtypes.int32) def branch_0(dataset): return dataset.map(lambda x: x + const_64) def branch_1(dataset): return dataset.map(lambda x: x + math_ops.cast(const_32, dtypes.int64)) return
optimization._ChooseFastestBranchDataset( dataset, [branch_0, branch_1], num_elements_per_branch=3) self.run_core_tes
ts(build_ds, None, 10) def testWithMoreOutputThanInput(self): def build_ds(): dataset = dataset_ops.Dataset.from_tensors(0).repeat(1000).batch(100) def branch(dataset): return dataset.apply(batching.unbatch()) return optimization._ChooseFastestBranchDataset( dataset, [branch, branch], ratio_denominator=10, num_elements_per_branch=100) self.run_core_tests(build_ds, None, 1000) if __name__ == "__main__": test.main()
5nizza/party-elli
interfaces/LTS.py
Python
mit
2,142
0.004202
from interfaces.labels_map import LabelsMap from helpers.python_ext import to_str class LTS: def __init__(self, init_states, model_by_signal:dict, tau_model:LabelsMap, state_name:str, input_signals, output_signals): self._output_models = model_by_signal self._tau_model = tau_model self._init_states = set(init_states) self._state_name = state_name self._output_signals = output_signals # TODO: duplication with _output_models? self._input_signals = input_signals @property def state_name(self): return self._state_name @property def input_signals(self): return self._input_signals @property def output_signals(self): return self._output_signals @property def init_states(self): return sel
f._init_states @property def states(self): # states = set(k[self._state_name] for k in self._tau_model) # return the range of tau \cup init_states states = set(map(lambda l_v: l_v[1], self._tau_model.items())) states.update(self.init_states) return states @property def tau_model(self) -> LabelsMap:
return self._tau_model @property def model_by_signal(self): return self._output_models @property def output_models(self) -> dict: return self._output_models def __str__(self): return 'LTS:\n' \ ' inputs: {inputs}\n' \ ' outputs: {outputs}\n' \ ' init_states: {init}\n' \ ' states: {states}\n' \ ' output_models: {output_models}'.format(init=str(self._init_states), states=str(self.states), output_models=str(self.model_by_signal), inputs=to_str(self._input_signals), outputs=to_str(self._output_signals))
nuagenetworks/tempest
tempest/tests/lib/test_decorators.py
Python
apache-2.0
4,381
0
# Copyright 2013 IBM Corp # Copyright 2015 Hewlett-Packard Development Company, L.P. # # 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 uuid import testtools from tempest.lib import base as test from tempest.lib import decorators from tempest.tests.lib import base class TestSkipBecauseDecorator(base.TestCase): def _test_skip_because_helper(self, expected_to_skip=True, **decorator_args): class TestFoo(test.BaseTestCase): _interface = 'json' @decorators.skip_because(**decorator_args) def test_bar(self): return 0 t = TestFoo('test_bar') if expected_to_skip: self.assertRaises(testtools.TestCase.skipException, t.test_bar) else: # assert that test_bar returned 0 self.assertEqual(TestFoo('test_bar').test_bar(), 0) def test_skip_because_bug(self): self._test_skip_because_helper(bug='12345') def test_skip_because_bug_and_condition_true(self): self._test_skip_because_helper(bug='12348', condition=True) def test_skip_because_bug_and_condition_false(self): self._test_skip_because_helper(expected_to_skip=False, bug='12349', condition=False) def test_skip_because_bug_without_bug_never_skips(self): """Never skip without a bug parameter.""" self._test_skip_because_helper(expected_to_skip=False, condition=True) self._test_skip_because_helper(expected_to_skip=False) def test_skip_because_invalid_bug_number(self): """Raise ValueError if with an invalid bug number""" self.assertRaises(ValueError, self._test_skip_because_helper, bug='critical_bug') class TestIdempotentIdDecorator(base.TestCase): def _test_helper(self, _id, **decorator_args): @decorators.idempotent_id(_id) def foo(): """Docstring""" pass return foo def _test_helper_without_doc(self, _id, **decorator_args): @decorators.idempotent_id(_id) def foo(): pass return foo def test_positive(self): _id = str(uuid.uuid4()) foo = self._test_helper(_id) self.assertIn('id-%s' % _id, getattr(foo, '__testtools_attrs')) self.assertTrue(foo.__doc__.startswith('Test idempotent id: %s' % _id)) def test_positive_without_doc(self): _id = str(uuid.uuid4()) foo = self._test_helper_without_doc(_id) self.assertTrue(foo.__doc__.startswith('Test idempotent id: %s' % _id)) def test_idempotent_id_not_str(self): _id = 42 self.assertRaises(TypeError, self._test_helper, _id) def test_idempotent_id_not_valid_uuid(self): _id = '42' self.assertRaises(ValueError, self._test_helper, _id) class TestSkipUnlessAttrDecorator(base.TestCase): def _test_skip_unless_attr(self, attr, expected_to_skip=True): class TestFoo(test.BaseTestCase): expected_attr = not expected_to_skip @decorators.skip_unless_attr(attr) def test_foo(self): pass t = TestFoo('test_foo') if expected_to_skip: self.assertRaises(testtools.TestCase.skipException, t.test_foo()) else: try: t.test_foo() except Exception: raise testtools.TestCase.failureException()
def test_skip_attr_does_not_exist(self): self._test_skip_unless_attr('unexpected_attr') def test_skip_attr_false(self): self._test_skip_unless_attr('expected_attr') def test_no_skip_for_attr_exist_and_true(self): self._test_skip_unless_attr('expected_attr', exp
ected_to_skip=False)