content stringlengths 27 928k | path stringlengths 4 230 | size int64 27 928k | nl_text stringlengths 21 396k | nl_size int64 21 396k | nl_language stringlengths 2 3 | nl_language_score float64 0.04 1 |
|---|---|---|---|---|---|---|
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Extended thread dispatching support.
For basic support see reactor threading API docs.
"""
from twisted.python.compat import _PY3
if not _PY3:
import Queue
else:
import queue as Queue
from twisted.python import failure
from twisted.internet import defer
def deferToThreadPool(reactor, threadpool, f, *args, **kwargs):
"""
Call the function C{f} using a thread from the given threadpool and return
the result as a Deferred.
This function is only used by client code which is maintaining its own
threadpool. To run a function in the reactor's threadpool, use
C{deferToThread}.
@param reactor: The reactor in whose main thread the Deferred will be
invoked.
@param threadpool: An object which supports the C{callInThreadWithCallback}
method of C{twisted.python.threadpool.ThreadPool}.
@param f: The function to call.
@param *args: positional arguments to pass to f.
@param **kwargs: keyword arguments to pass to f.
@return: A Deferred which fires a callback with the result of f, or an
errback with a L{twisted.python.failure.Failure} if f throws an
exception.
"""
d = defer.Deferred()
def onResult(success, result):
if success:
reactor.callFromThread(d.callback, result)
else:
reactor.callFromThread(d.errback, result)
threadpool.callInThreadWithCallback(onResult, f, *args, **kwargs)
return d
def deferToThread(f, *args, **kwargs):
"""
Run a function in a thread and return the result as a Deferred.
@param f: The function to call.
@param *args: positional arguments to pass to f.
@param **kwargs: keyword arguments to pass to f.
@return: A Deferred which fires a callback with the result of f,
or an errback with a L{twisted.python.failure.Failure} if f throws
an exception.
"""
from twisted.internet import reactor
return deferToThreadPool(reactor, reactor.getThreadPool(),
f, *args, **kwargs)
def _runMultiple(tupleList):
"""
Run a list of functions.
"""
for f, args, kwargs in tupleList:
f(*args, **kwargs)
def callMultipleInThread(tupleList):
"""
Run a list of functions in the same thread.
tupleList should be a list of (function, argsList, kwargsDict) tuples.
"""
from twisted.internet import reactor
reactor.callInThread(_runMultiple, tupleList)
def blockingCallFromThread(reactor, f, *a, **kw):
"""
Run a function in the reactor from a thread, and wait for the result
synchronously. If the function returns a L{Deferred}, wait for its
result and return that.
@param reactor: The L{IReactorThreads} provider which will be used to
schedule the function call.
@param f: the callable to run in the reactor thread
@type f: any callable.
@param a: the arguments to pass to C{f}.
@param kw: the keyword arguments to pass to C{f}.
@return: the result of the L{Deferred} returned by C{f}, or the result
of C{f} if it returns anything other than a L{Deferred}.
@raise: If C{f} raises a synchronous exception,
C{blockingCallFromThread} will raise that exception. If C{f}
returns a L{Deferred} which fires with a L{Failure},
C{blockingCallFromThread} will raise that failure's exception (see
L{Failure.raiseException}).
"""
queue = Queue.Queue()
def _callFromThread():
result = defer.maybeDeferred(f, *a, **kw)
result.addBoth(queue.put)
reactor.callFromThread(_callFromThread)
result = queue.get()
if isinstance(result, failure.Failure):
result.raiseException()
return result
__all__ = ["deferToThread", "deferToThreadPool", "callMultipleInThread",
"blockingCallFromThread"]
| src/twisted/internet/threads.py | 3,904 | Run a list of functions.
Run a function in the reactor from a thread, and wait for the result
synchronously. If the function returns a L{Deferred}, wait for its
result and return that.
@param reactor: The L{IReactorThreads} provider which will be used to
schedule the function call.
@param f: the callable to run in the reactor thread
@type f: any callable.
@param a: the arguments to pass to C{f}.
@param kw: the keyword arguments to pass to C{f}.
@return: the result of the L{Deferred} returned by C{f}, or the result
of C{f} if it returns anything other than a L{Deferred}.
@raise: If C{f} raises a synchronous exception,
C{blockingCallFromThread} will raise that exception. If C{f}
returns a L{Deferred} which fires with a L{Failure},
C{blockingCallFromThread} will raise that failure's exception (see
L{Failure.raiseException}).
Run a list of functions in the same thread.
tupleList should be a list of (function, argsList, kwargsDict) tuples.
Run a function in a thread and return the result as a Deferred.
@param f: The function to call.
@param *args: positional arguments to pass to f.
@param **kwargs: keyword arguments to pass to f.
@return: A Deferred which fires a callback with the result of f,
or an errback with a L{twisted.python.failure.Failure} if f throws
an exception.
Call the function C{f} using a thread from the given threadpool and return
the result as a Deferred.
This function is only used by client code which is maintaining its own
threadpool. To run a function in the reactor's threadpool, use
C{deferToThread}.
@param reactor: The reactor in whose main thread the Deferred will be
invoked.
@param threadpool: An object which supports the C{callInThreadWithCallback}
method of C{twisted.python.threadpool.ThreadPool}.
@param f: The function to call.
@param *args: positional arguments to pass to f.
@param **kwargs: keyword arguments to pass to f.
@return: A Deferred which fires a callback with the result of f, or an
errback with a L{twisted.python.failure.Failure} if f throws an
exception.
Extended thread dispatching support.
For basic support see reactor threading API docs.
Copyright (c) Twisted Matrix Laboratories. See LICENSE for details. | 2,235 | en | 0.742232 |
from datetime import datetime
from astropy.time import Time
def read_tle_file(tlefile, **kwargs):
"""
Read in a TLE file and return the TLE that is closest to the date you want to
propagate the orbit to.
"""
times = []
line1 = []
line2 = []
from os import path
from datetime import datetime
# Catch if the file can't be opened:
try:
f = open(tlefile, 'r')
except FileNotFoundError:
print("Unable to open: "+tlefile)
ln=0
for line in f:
# print(line)
if (ln == 0):
year= int(line[18:20])
day = int(line[20:23])
times.extend([datetime.strptime("{}:{}".format(year, day), "%y:%j")])
line1.extend([line.strip()])
ln=1
else:
ln=0
line2.extend([line.strip()])
f.close()
return times, line1, line2
def get_epoch_tle(epoch, tlefile):
"""
Find the TLE that is closest to the epoch you want to search.
epoch is a datetime object, tlefile is the file you want to search through.
"""
times, line1, line2 = read_tle_file(tlefile)
from datetime import datetime
from astropy.time import Time
# Allow astropy Time objects
if type(epoch) is Time:
epoch = epoch.datetime
mindt = 100.
min_ind = 0
for ind, t in enumerate(times):
dt = abs((epoch -t).days)
if dt < mindt:
min_ind = ind
mindt = dt
good_line1 = line1[min_ind]
good_line2 = line2[min_ind]
return mindt, good_line1, good_line2
def convert_nustar_time(t, leap=5):
'''
Converts MET seconds to a datetime object.
Default is to subtract off 5 leap seconds.
'''
import astropy.units as u
mjdref = 55197*u.d
met = (t - leap)* u.s + mjdref
met_datetime = Time(met.to(u.d), format = 'mjd').datetime
return met_datetime
def get_nustar_location(checktime, line1, line2):
'''
Code to determine the spacecraft location from the TLE.
Inputs are a datetime object and the two lines of the TLE you want to use.
Returns a tuple that has the X, Y, and Z geocentric coordinates (in km).
'''
from sgp4.earth_gravity import wgs72
from sgp4.io import twoline2rv
from astropy.coordinates import EarthLocation
satellite = twoline2rv(line1, line2, wgs72)
position, velocity = satellite.propagate(
checktime.year, checktime.month, checktime.day,
checktime.hour, checktime.minute, checktime.second)
return position
def eci2el(x,y,z,dt):
"""
Convert Earth-Centered Inertial (ECI) cartesian coordinates to ITRS for astropy EarthLocation object.
Inputs :
x = ECI X-coordinate
y = ECI Y-coordinate
z = ECI Z-coordinate
dt = UTC time (datetime object)
"""
from astropy.coordinates import GCRS, ITRS, EarthLocation, CartesianRepresentation
import astropy.units as u
# convert datetime object to astropy time object
tt=Time(dt,format='datetime')
# Read the coordinates in the Geocentric Celestial Reference System
gcrs = GCRS(CartesianRepresentation(x=x, y=y,z=z), obstime=tt)
# Convert it to an Earth-fixed frame
itrs = gcrs.transform_to(ITRS(obstime=tt))
el = EarthLocation.from_geocentric(itrs.x, itrs.y, itrs.z)
return el
def get_moon_j2000(epoch, line1, line2, position = None):
'''
Code to determine the apparent J2000 position for a given
time and at a given position for the observatory.
epoch needs to be a datetime or Time object.
position is a list/tuple of X/Y/Z positions
'''
from astropy.time import Time
from astropy.coordinates import get_moon, EarthLocation
import astropy.units as u
import sys
from datetime import datetime
if type(epoch) is Time:
epoch = epoch.datetime
if position is None:
position = get_nustar_location(epoch, line1, line2) # position in ECI coords
t=Time(epoch)
loc = eci2el(*position*u.km,t)
moon_coords = get_moon(t,loc)
# Get just the coordinates in degrees
ra_moon, dec_moon = moon_coords.ra.degree * u.deg, moon_coords.dec.degree*u.deg
return ra_moon, dec_moon
| nustar_lunar_pointing/tracking.py | 4,544 | Converts MET seconds to a datetime object.
Default is to subtract off 5 leap seconds.
Convert Earth-Centered Inertial (ECI) cartesian coordinates to ITRS for astropy EarthLocation object.
Inputs :
x = ECI X-coordinate
y = ECI Y-coordinate
z = ECI Z-coordinate
dt = UTC time (datetime object)
Find the TLE that is closest to the epoch you want to search.
epoch is a datetime object, tlefile is the file you want to search through.
Code to determine the apparent J2000 position for a given
time and at a given position for the observatory.
epoch needs to be a datetime or Time object.
position is a list/tuple of X/Y/Z positions
Code to determine the spacecraft location from the TLE.
Inputs are a datetime object and the two lines of the TLE you want to use.
Returns a tuple that has the X, Y, and Z geocentric coordinates (in km).
Read in a TLE file and return the TLE that is closest to the date you want to
propagate the orbit to.
Catch if the file can't be opened: print(line) Allow astropy Time objects convert datetime object to astropy time object Read the coordinates in the Geocentric Celestial Reference System Convert it to an Earth-fixed frame position in ECI coords Get just the coordinates in degrees | 1,232 | en | 0.8301 |
from __future__ import unicode_literals
from future.builtins import int, range, str
from datetime import date, datetime
from os.path import join, split
from uuid import uuid4
from django import forms
from django.forms.extras import SelectDateWidget
from django.core.files.storage import FileSystemStorage
from django.core.urlresolvers import reverse
from django.template import Template
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext as _
from django.utils.timezone import now
from zhiliao.conf import settings
from zhiliao.forms import fields
from zhiliao.forms.models import FormEntry, FieldEntry
from zhiliao.utils.email import split_addresses as split_choices
fs = FileSystemStorage(location=settings.FORMS_UPLOAD_ROOT)
##############################
# Each type of export filter #
##############################
# Text matches
FILTER_CHOICE_CONTAINS = "1"
FILTER_CHOICE_DOESNT_CONTAIN = "2"
# Exact matches
FILTER_CHOICE_EQUALS = "3"
FILTER_CHOICE_DOESNT_EQUAL = "4"
# Greater/less than
FILTER_CHOICE_BETWEEN = "5"
# Multiple values
FILTER_CHOICE_CONTAINS_ANY = "6"
FILTER_CHOICE_CONTAINS_ALL = "7"
FILTER_CHOICE_DOESNT_CONTAIN_ANY = "8"
FILTER_CHOICE_DOESNT_CONTAIN_ALL = "9"
##########################
# Export filters grouped #
##########################
# Text fields
TEXT_FILTER_CHOICES = (
("", _("Nothing")),
(FILTER_CHOICE_CONTAINS, _("Contains")),
(FILTER_CHOICE_DOESNT_CONTAIN, _("Doesn't contain")),
(FILTER_CHOICE_EQUALS, _("Equals")),
(FILTER_CHOICE_DOESNT_EQUAL, _("Doesn't equal")),
)
# Choices with single value entries
CHOICE_FILTER_CHOICES = (
("", _("Nothing")),
(FILTER_CHOICE_CONTAINS_ANY, _("Equals any")),
(FILTER_CHOICE_DOESNT_CONTAIN_ANY, _("Doesn't equal any")),
)
# Choices with multiple value entries
MULTIPLE_FILTER_CHOICES = (
("", _("Nothing")),
(FILTER_CHOICE_CONTAINS_ANY, _("Contains any")),
(FILTER_CHOICE_CONTAINS_ALL, _("Contains all")),
(FILTER_CHOICE_DOESNT_CONTAIN_ANY, _("Doesn't contain any")),
(FILTER_CHOICE_DOESNT_CONTAIN_ALL, _("Doesn't contain all")),
)
# Dates
DATE_FILTER_CHOICES = (
("", _("Nothing")),
(FILTER_CHOICE_BETWEEN, _("Is between")),
)
# The filter function for each filter type
FILTER_FUNCS = {
FILTER_CHOICE_CONTAINS:
lambda val, field: val.lower() in field.lower(),
FILTER_CHOICE_DOESNT_CONTAIN:
lambda val, field: val.lower() not in field.lower(),
FILTER_CHOICE_EQUALS:
lambda val, field: val.lower() == field.lower(),
FILTER_CHOICE_DOESNT_EQUAL:
lambda val, field: val.lower() != field.lower(),
FILTER_CHOICE_BETWEEN:
lambda val_from, val_to, field: (
(not val_from or val_from <= field) and
(not val_to or val_to >= field)
),
FILTER_CHOICE_CONTAINS_ANY:
lambda val, field: set(val) & set(split_choices(field)),
FILTER_CHOICE_CONTAINS_ALL:
lambda val, field: set(val) == set(split_choices(field)),
FILTER_CHOICE_DOESNT_CONTAIN_ANY:
lambda val, field: not set(val) & set(split_choices(field)),
FILTER_CHOICE_DOESNT_CONTAIN_ALL:
lambda val, field: set(val) != set(split_choices(field)),
}
# Export form fields for each filter type grouping
text_filter_field = forms.ChoiceField(label=" ", required=False,
choices=TEXT_FILTER_CHOICES)
choice_filter_field = forms.ChoiceField(label=" ", required=False,
choices=CHOICE_FILTER_CHOICES)
multiple_filter_field = forms.ChoiceField(label=" ", required=False,
choices=MULTIPLE_FILTER_CHOICES)
date_filter_field = forms.ChoiceField(label=" ", required=False,
choices=DATE_FILTER_CHOICES)
class FormForForm(forms.ModelForm):
"""
Form with a set of fields dynamically assigned, directly based on the
given ``forms.models.Form`` instance.
"""
class Meta:
model = FormEntry
exclude = ("form", "entry_time")
def __init__(self, form, context, *args, **kwargs):
"""
Dynamically add each of the form fields for the given form model
instance and its related field model instances.
"""
self.form = form
self.form_fields = form.fields.visible()
initial = kwargs.pop("initial", {})
# If a FormEntry instance is given to edit, populate initial
# with its field values.
field_entries = {}
if kwargs.get("instance"):
for field_entry in kwargs["instance"].fields.all():
field_entries[field_entry.field_id] = field_entry.value
super(FormForForm, self).__init__(*args, **kwargs)
# Create the form fields.
for field in self.form_fields:
field_key = "field_%s" % field.id
field_class = fields.CLASSES[field.field_type]
field_widget = fields.WIDGETS.get(field.field_type)
field_args = {"label": field.label, "required": field.required,
"help_text": field.help_text}
if field.required and not field.help_text:
field_args["help_text"] = _("required")
arg_names = field_class.__init__.__code__.co_varnames
if "max_length" in arg_names:
field_args["max_length"] = settings.FORMS_FIELD_MAX_LENGTH
if "choices" in arg_names:
choices = list(field.get_choices())
if (field.field_type == fields.SELECT and
field.default not in [c[0] for c in choices]):
choices.insert(0, ("", field.placeholder_text))
field_args["choices"] = choices
if field_widget is not None:
field_args["widget"] = field_widget
#
# Initial value for field, in order of preference:
#
# - If a form model instance is given (eg we're editing a
# form response), then use the instance's value for the
# field.
# - If the developer has provided an explicit "initial"
# dict, use it.
# - The default value for the field instance as given in
# the admin.
#
initial_val = None
try:
initial_val = field_entries[field.id]
except KeyError:
try:
initial_val = initial[field_key]
except KeyError:
initial_val = Template(field.default).render(context)
if initial_val:
if field.is_a(*fields.MULTIPLE):
initial_val = split_choices(initial_val)
elif field.field_type == fields.CHECKBOX:
initial_val = initial_val != "False"
self.initial[field_key] = initial_val
self.fields[field_key] = field_class(**field_args)
if field.field_type == fields.DOB:
_now = datetime.now()
years = list(range(_now.year, _now.year - 120, -1))
self.fields[field_key].widget.years = years
# Add identifying type attr to the field for styling.
setattr(self.fields[field_key], "type",
field_class.__name__.lower())
if (field.required and settings.FORMS_USE_HTML5 and
field.field_type != fields.CHECKBOX_MULTIPLE):
self.fields[field_key].widget.attrs["required"] = ""
if field.placeholder_text and not field.default:
text = field.placeholder_text
self.fields[field_key].widget.attrs["placeholder"] = text
def save(self, **kwargs):
"""
Create a ``FormEntry`` instance and related ``FieldEntry``
instances for each form field.
"""
entry = super(FormForForm, self).save(commit=False)
entry.form = self.form
entry.entry_time = now()
entry.save()
entry_fields = entry.fields.values_list("field_id", flat=True)
new_entry_fields = []
for field in self.form_fields:
field_key = "field_%s" % field.id
value = self.cleaned_data[field_key]
if value and self.fields[field_key].widget.needs_multipart_form:
value = fs.save(join("forms", str(uuid4()), value.name), value)
if isinstance(value, list):
value = ", ".join([v.strip() for v in value])
if field.id in entry_fields:
field_entry = entry.fields.get(field_id=field.id)
field_entry.value = value
field_entry.save()
else:
new = {"entry": entry, "field_id": field.id, "value": value}
new_entry_fields.append(FieldEntry(**new))
if new_entry_fields:
FieldEntry.objects.bulk_create(new_entry_fields)
return entry
def email_to(self):
"""
Return the value entered for the first field of type
``forms.fields.EMAIL``.
"""
for field in self.form_fields:
if field.is_a(fields.EMAIL):
return self.cleaned_data["field_%s" % field.id]
return None
class EntriesForm(forms.Form):
"""
Form with a set of fields dynamically assigned that can be used to
filter entries for the given ``forms.models.Form`` instance.
"""
def __init__(self, form, request, *args, **kwargs):
"""
Iterate through the fields of the ``forms.models.Form`` instance and
create the form fields required to control including the field in
the export (with a checkbox) or filtering the field which differs
across field types. User a list of checkboxes when a fixed set of
choices can be chosen from, a pair of date fields for date ranges,
and for all other types provide a textbox for text search.
"""
self.form = form
self.request = request
self.form_fields = form.fields.all()
self.entry_time_name = str(FormEntry._meta.get_field(
"entry_time").verbose_name)
super(EntriesForm, self).__init__(*args, **kwargs)
for field in self.form_fields:
field_key = "field_%s" % field.id
# Checkbox for including in export.
self.fields["%s_export" % field_key] = forms.BooleanField(
label=field.label, initial=True, required=False)
if field.is_a(*fields.CHOICES):
# A fixed set of choices to filter by.
if field.is_a(fields.CHECKBOX):
choices = ((True, _("Checked")), (False, _("Not checked")))
else:
choices = field.get_choices()
contains_field = forms.MultipleChoiceField(label=" ",
choices=choices, widget=forms.CheckboxSelectMultiple(),
required=False)
self.fields["%s_filter" % field_key] = choice_filter_field
self.fields["%s_contains" % field_key] = contains_field
elif field.is_a(*fields.MULTIPLE):
# A fixed set of choices to filter by, with multiple
# possible values in the entry field.
contains_field = forms.MultipleChoiceField(label=" ",
choices=field.get_choices(),
widget=forms.CheckboxSelectMultiple(),
required=False)
self.fields["%s_filter" % field_key] = multiple_filter_field
self.fields["%s_contains" % field_key] = contains_field
elif field.is_a(*fields.DATES):
# A date range to filter by.
self.fields["%s_filter" % field_key] = date_filter_field
self.fields["%s_from" % field_key] = forms.DateField(
label=" ", widget=SelectDateWidget(), required=False)
self.fields["%s_to" % field_key] = forms.DateField(
label=_("and"), widget=SelectDateWidget(), required=False)
else:
# Text box for search term to filter by.
contains_field = forms.CharField(label=" ", required=False)
self.fields["%s_filter" % field_key] = text_filter_field
self.fields["%s_contains" % field_key] = contains_field
# Add ``FormEntry.entry_time`` as a field.
field_key = "field_0"
self.fields["%s_export" % field_key] = forms.BooleanField(initial=True,
label=FormEntry._meta.get_field("entry_time").verbose_name,
required=False)
self.fields["%s_filter" % field_key] = date_filter_field
self.fields["%s_from" % field_key] = forms.DateField(
label=" ", widget=SelectDateWidget(), required=False)
self.fields["%s_to" % field_key] = forms.DateField(
label=_("and"), widget=SelectDateWidget(), required=False)
def __iter__(self):
"""
Yield pairs of include checkbox / filters for each field.
"""
for field_id in [f.id for f in self.form_fields] + [0]:
prefix = "field_%s_" % field_id
fields = [f for f in super(EntriesForm, self).__iter__()
if f.name.startswith(prefix)]
yield fields[0], fields[1], fields[2:]
def columns(self):
"""
Returns the list of selected column names.
"""
fields = [f.label for f in self.form_fields
if self.cleaned_data["field_%s_export" % f.id]]
if self.cleaned_data["field_0_export"]:
fields.append(self.entry_time_name)
return fields
def rows(self, csv=False):
"""
Returns each row based on the selected criteria.
"""
# Store the index of each field against its ID for building each
# entry row with columns in the correct order. Also store the IDs of
# fields with a type of FileField or Date-like for special handling of
# their values.
field_indexes = {}
file_field_ids = []
date_field_ids = []
for field in self.form_fields:
if self.cleaned_data["field_%s_export" % field.id]:
field_indexes[field.id] = len(field_indexes)
if field.is_a(fields.FILE):
file_field_ids.append(field.id)
elif field.is_a(*fields.DATES):
date_field_ids.append(field.id)
num_columns = len(field_indexes)
include_entry_time = self.cleaned_data["field_0_export"]
if include_entry_time:
num_columns += 1
# Get the field entries for the given form and filter by entry_time
# if specified.
field_entries = FieldEntry.objects.filter(
entry__form=self.form).order_by(
"-entry__id").select_related("entry")
if self.cleaned_data["field_0_filter"] == FILTER_CHOICE_BETWEEN:
time_from = self.cleaned_data["field_0_from"]
time_to = self.cleaned_data["field_0_to"]
if time_from and time_to:
field_entries = field_entries.filter(
entry__entry_time__range=(time_from, time_to))
# Loop through each field value ordered by entry, building up each
# entry as a row. Use the ``valid_row`` flag for marking a row as
# invalid if it fails one of the filtering criteria specified.
current_entry = None
current_row = None
valid_row = True
for field_entry in field_entries:
if field_entry.entry_id != current_entry:
# New entry, write out the current row and start a new one.
if valid_row and current_row is not None:
if not csv:
current_row.insert(0, current_entry)
yield current_row
current_entry = field_entry.entry_id
current_row = [""] * num_columns
valid_row = True
if include_entry_time:
current_row[-1] = field_entry.entry.entry_time
field_value = field_entry.value or ""
# Check for filter.
field_id = field_entry.field_id
filter_type = self.cleaned_data.get("field_%s_filter" % field_id)
filter_args = None
if filter_type:
if filter_type == FILTER_CHOICE_BETWEEN:
f, t = "field_%s_from" % field_id, "field_%s_to" % field_id
filter_args = [self.cleaned_data[f], self.cleaned_data[t]]
else:
field_name = "field_%s_contains" % field_id
filter_args = self.cleaned_data[field_name]
if filter_args:
filter_args = [filter_args]
if filter_args:
# Convert dates before checking filter.
if field_id in date_field_ids:
y, m, d = field_value.split(" ")[0].split("-")
dte = date(int(y), int(m), int(d))
filter_args.append(dte)
else:
filter_args.append(field_value)
filter_func = FILTER_FUNCS[filter_type]
if not filter_func(*filter_args):
valid_row = False
# Create download URL for file fields.
if field_entry.value and field_id in file_field_ids:
url = reverse("admin:form_file", args=(field_entry.id,))
field_value = self.request.build_absolute_uri(url)
if not csv:
parts = (field_value, split(field_entry.value)[1])
field_value = mark_safe("<a href=\"%s\">%s</a>" % parts)
# Only use values for fields that were selected.
try:
current_row[field_indexes[field_id]] = field_value
except KeyError:
pass
# Output the final row.
if valid_row and current_row is not None:
if not csv:
current_row.insert(0, current_entry)
yield current_row
| zhiliao/forms/forms.py | 18,302 | Form with a set of fields dynamically assigned that can be used to
filter entries for the given ``forms.models.Form`` instance.
Form with a set of fields dynamically assigned, directly based on the
given ``forms.models.Form`` instance.
Dynamically add each of the form fields for the given form model
instance and its related field model instances.
Iterate through the fields of the ``forms.models.Form`` instance and
create the form fields required to control including the field in
the export (with a checkbox) or filtering the field which differs
across field types. User a list of checkboxes when a fixed set of
choices can be chosen from, a pair of date fields for date ranges,
and for all other types provide a textbox for text search.
Yield pairs of include checkbox / filters for each field.
Returns the list of selected column names.
Return the value entered for the first field of type
``forms.fields.EMAIL``.
Returns each row based on the selected criteria.
Create a ``FormEntry`` instance and related ``FieldEntry``
instances for each form field.
Each type of export filter Text matches Exact matches Greater/less than Multiple values Export filters grouped Text fields Choices with single value entries Choices with multiple value entries Dates The filter function for each filter type Export form fields for each filter type grouping If a FormEntry instance is given to edit, populate initial with its field values. Create the form fields. Initial value for field, in order of preference: - If a form model instance is given (eg we're editing a form response), then use the instance's value for the field. - If the developer has provided an explicit "initial" dict, use it. - The default value for the field instance as given in the admin. Add identifying type attr to the field for styling. Checkbox for including in export. A fixed set of choices to filter by. A fixed set of choices to filter by, with multiple possible values in the entry field. A date range to filter by. Text box for search term to filter by. Add ``FormEntry.entry_time`` as a field. Store the index of each field against its ID for building each entry row with columns in the correct order. Also store the IDs of fields with a type of FileField or Date-like for special handling of their values. Get the field entries for the given form and filter by entry_time if specified. Loop through each field value ordered by entry, building up each entry as a row. Use the ``valid_row`` flag for marking a row as invalid if it fails one of the filtering criteria specified. New entry, write out the current row and start a new one. Check for filter. Convert dates before checking filter. Create download URL for file fields. Only use values for fields that were selected. Output the final row. | 2,787 | en | 0.872793 |
# -*- coding: utf-8 -*-
import json
import datetime
class JobHeader:
"""Represents a row from the callout."""
def __init__(self, raw_data):
self.attributes = {
"contractor": json.dumps(raw_data[0]),
"job_name": json.dumps(raw_data[1]),
"is_dayshift": json.dumps(int(raw_data[3] == "Days")),
"job_id": json.dumps(int(raw_data[4]))
}
self.contractor = raw_data[0]
self.jobName = raw_data[1]
self.startDate = raw_data[2]
self.shift = raw_data[3]
self.id = raw_data[4]
def add_data(self, pop_data):
print("popData for id {0}".format(self.id))
if self.id != pop_data.id:
print("ID mismatch...")
return
class JobData:
TIME_TAG = "start_time"
DATE_TAG = "start_date"
DATE_TIME_TAG = "date_time"
def __init__(self, raw_data):
self.lineMatches = {"Work Type:": "work_type",
"Hours:": "hours",
"Start Date:": "start_date",
"Start Time:": "start_time",
"Duration:": "duration",
"Accommodation:": "accommodation",
"Open To:": "open_to",
"Comments:": "comments",
"Drug Testing Info:": "drug_testing"}
self.nameHireTag = "Name Hired:"
# 2 spaces typo
self.manpowerTag = "Manpower Requirements:"
self.attribute_dictionary = {}
self.manpower = {}
self.name_hires = {}
skip_line = False
for i, row in enumerate(raw_data):
if row.has_attr("bgcolor"):
continue
if skip_line:
skip_line = False
continue
stripped = row.text.strip()
if not row.find("b"):
# print("Element {0} is not bold enough for my needs.".format(row))
continue
if self.check_line_match(i, stripped, raw_data):
skip_line = True
continue
if "Job#" in stripped:
self.id = stripped.split(u'\xa0')[-1]
print("Set Job# to {0}".format(self.id))
continue
if self.manpowerTag in stripped:
self.manpower = self.get_multi_line(self.manpowerTag, i, raw_data)
continue
if self.nameHireTag in stripped:
self.name_hires = self.get_multi_line(self.nameHireTag, i, raw_data)
continue
# # parse checkboxes
# inputs = row.find_all("input")
# if inputs:
# self.attrDic["Shift:"] = "Days" if self.parse_checkbox(row.find_all("b")) else "Nights"
# print("Set Shift: to {0}".format(self.attrDic["Shift:"]))
# continue
print(repr(stripped))
self.attribute_dictionary["manpower"] = json.dumps(self.manpower)
self.attribute_dictionary["name_hires"] = json.dumps(self.name_hires)
date_split = self.attribute_dictionary[self.DATE_TAG].replace('\"', '').split('/')
time_string = self.attribute_dictionary[self.TIME_TAG].replace('\"', '') + ":00"
self.attribute_dictionary[self.DATE_TIME_TAG] = "{0}-{1}-{2} {3}".format(
date_split[2], date_split[0], date_split[1], time_string)
del self.attribute_dictionary[self.DATE_TAG]
del self.attribute_dictionary[self.TIME_TAG]
print("dateTime set to: {0}".format(repr(datetime)))
def check_line_match(self, index, stripped, data_rows):
"""Find lines matching stripped from lineMatchKeys and set value to immediately following row"""
for key, value in self.lineMatches.items():
if stripped == key:
next_row = data_rows[index + 1]
if next_row.find_all("b"):
print("Next row was bold element: {0}. Skipping...".format(next_row))
return False
next_row_stripped = next_row.text.strip()
if next_row_stripped in self.lineMatches:
print("Next row was {0} and is in lineMatchKeys, skipping...".format(next_row_stripped))
return False
self.attribute_dictionary[value] = json.dumps(next_row_stripped)
print("Set {0} to {1}".format(value, self.attribute_dictionary[value]))
del self.lineMatches[key]
return True
return False
@classmethod
def get_multi_line(cls, match, index, data_rows):
attr_list = []
while True:
index += 1
if index >= len(data_rows):
break
next_row = data_rows[index]
if next_row.find("b"):
break
if next_row.find("tr"):
print("Skipping td containing trs")
continue
attr_list.append(next_row.text.strip().replace(u'\xa0', ' '))
attr_dic = {}
i = 0
while i + 1 < len(attr_list):
attr_dic[attr_list[i]] = attr_list[i + 1]
i += 2
print("Set '{0}' to dic:".format(match))
print(repr(attr_dic))
return attr_dic
@classmethod
def parse_checkbox(cls, bold_elements):
for bold in bold_elements:
if "Days" in bold.text.strip():
input_el = bold.find("input")
if input_el:
return input_el.has_attr("checked")
| DataObjects.py | 5,796 | Represents a row from the callout.
Find lines matching stripped from lineMatchKeys and set value to immediately following row
-*- coding: utf-8 -*- 2 spaces typo print("Element {0} is not bold enough for my needs.".format(row)) parse checkboxes inputs = row.find_all("input") if inputs: self.attrDic["Shift:"] = "Days" if self.parse_checkbox(row.find_all("b")) else "Nights" print("Set Shift: to {0}".format(self.attrDic["Shift:"])) continue | 456 | en | 0.527906 |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
----------------------
Asynchronous SqlHelper
----------------------
TODO:
#. Transaction
Examples:
Simple Usage:
::
from kipp.aio import SqlHelper, run_until_complete, coroutine2
@coroutine2
def main():
db = SqlHelper('movoto')
r = yield db.getOneBySql('show DATABASES;')
if __name__ == '__main__':
run_until_complete(main())
"""
from __future__ import unicode_literals
from kipp.libs import PY2, PY3
from kipp.utils import get_logger
from .base import run_on_executor, thread_executor
class Py3SqlHelper(object):
pass
class Py2SqlHelper:
executor = thread_executor
def __init__(self, *args, **kw):
from Utilities.movoto.SqlHelper import SqlHelper as MovotoSqlHelper
self.sqlhelper = MovotoSqlHelper(*args, **kw)
def __getattr__(self, name):
return getattr(self.sqlhelper, name)
@run_on_executor()
def getAllBySql(self, sql, *args, **kw):
return self.sqlhelper.getAllBySql(sql, *args, **kw)
@run_on_executor()
def getOneBySql(self, sql, *args, **kw):
return self.sqlhelper.getOneBySql(sql, *args, **kw)
@run_on_executor()
def executeBySql(self, sql, *args, **kw):
return self.sqlhelper.executeBySql(sql, *args, **kw)
@run_on_executor()
def executeManyBySql(self, sql, *args, **kw):
return self.sqlhelper.executeManyBySql(sql, *args, **kw)
get_all_by_sql = getAllBySql
get_one_by_sql = getOneBySql
execute_by_sql = executeBySql
execute_many_by_sql = executeManyBySql
class SqlHelper:
def __init__(self, *args, **kw):
if PY2:
get_logger().info("set SqlHelper for py2")
from Utilities.movoto import settings
self.sqlhelper = Py2SqlHelper(*args, **kw)
elif PY3:
get_logger().info("set SqlHelper for py3")
self.sqlhelper = Py3SqlHelper(*args, **kw)
def __getattr__(self, name):
return getattr(self.sqlhelper, name)
| kipp/aio/sqlhelper.py | 2,080 | ----------------------
Asynchronous SqlHelper
----------------------
TODO:
#. Transaction
Examples:
Simple Usage:
::
from kipp.aio import SqlHelper, run_until_complete, coroutine2
@coroutine2
def main():
db = SqlHelper('movoto')
r = yield db.getOneBySql('show DATABASES;')
if __name__ == '__main__':
run_until_complete(main())
!/usr/bin/env python -*- coding: utf-8 -*- | 457 | en | 0.37188 |
#!/usr/bin/env python3
import argparse
import collections
import hashlib
import itertools
import os
import re
import sys
import statistics
import subprocess
import tempfile
import time
import pprint
import json
from collections import namedtuple
# Argument parser
parser = argparse.ArgumentParser(description="""
Compare running times and memory usage of a set of compressors.
""")
parser.add_argument('--suite', '-s', type=str, default='',
help='the comparison suite to execute')
parser.add_argument('--iterations', '-n', type=int, default=1,
help='the amount of iterations for each input file')
parser.add_argument('files', metavar='FILE', type=str, nargs='+',
help='the input files to use for comparison')
parser.add_argument('--format', type=str, default='stdout',
help='Format to output')
parser.add_argument('--nomem', action="store_true",
help='Don\'t measure memory')
args = parser.parse_args()
class StdOutTable:
def __init__(self):
pass
def print(self, *s):
print(*s)
def file(self, srcfname, srcsize, srchash):
print()
print("File: %s (%s, sha256=%s)" % (srcfname, memsize(srcsize), srchash))
def header(self, tup):
print()
print(("%"+ str(maxnicknamelength) + "s | %10s | %10s | %10s | %10s | %10s | %4s |") % tup)
print('-'*(maxnicknamelength+5*10+6*3+4+2))
def cell(self, content, format, sep, f):
print((format + " " + sep) % f(content), end='',flush=True)
def end_row(self):
print()
def flush(self):
pass
class JsonTable:
def __init__(self):
self.messages = []
self.files = {}
def print(self, *s):
self.messages.append(" ".join(map(str, iter(s))))
def file(self, srcfname, srcsize, srchash):
self.files[srcfname] = {}
self.currentfile = self.files[srcfname]
self.currentfile["cols"] = {}
self.currentfile["size"] = srcsize
self.currentfile["hash"] = srchash
def header(self, tup):
self.headings = tup
self.current_heading = 0
for heading in tup:
self.currentfile["cols"][heading] = []
def cell(self, content, format, sep, f):
self.currentfile["cols"][self.headings[self.current_heading]].append(content)
self.current_heading += 1
def end_row(self):
self.current_heading = 0
def flush(self):
print(json.dumps(self.__dict__, sort_keys=True, indent=4))
sot = StdOutTable()
if args.format == "json":
sot = JsonTable()
# Ensure that the input files are readable
for srcfname in args.files:
if not os.access(srcfname, os.R_OK):
sot.print("ERROR: Input file not found or not readable:", srcfname)
quit()
# Check that valgrind is available for memory measurement
mem_available = False
if not args.nomem:
try:
subprocess.check_call(["valgrind", "--version"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
mem_available = True
except:
mem_available = False
sot.print("WARNING: valgrind not found - memory measurement unavailable.")
sot.print()
# Program execution definition
StdOut = 0
StdIn = 0
Exec = collections.namedtuple('Exec', ['args', 'outp', 'inp'])
Exec.__new__.__defaults__ = (None, None) # args is required
# Compressor Pair definition
CompressorPair = collections.namedtuple('CompressorPair', ['name', 'compress', 'decompress'])
def Tudocomp(name, algorithm, tdc_binary='./tdc', cflags=[], dflags=[]):
return CompressorPair(name,
compress = Exec(args=[tdc_binary, '-a', algorithm] + cflags, outp='--output'),
decompress = Exec(args=[tdc_binary, '-d'] + dflags, outp='--output'))
def StdCompressor(name, binary, cflags=[], dflags=[]):
return CompressorPair(name,
compress = Exec(args=[binary] + cflags, inp=StdIn, outp=StdOut),
decompress = Exec(args=[binary] + dflags, inp=StdIn, outp=StdOut))
# Define suite
if args.suite:
# Evaluate suite as Python
try:
with open(args.suite, "r") as f:
suite = eval(f.read())
# sanity checks
if not type(suite) is list:
raise(Exception(
"Suite evaluated to " + str(type(suite)) +
", but should be a list of CompressorPair objects"))
if len(suite) == 0:
raise(Exception("Suite is empty"))
for c in suite:
if not isinstance(c, CompressorPair):
raise(Exception("Suite must only contain CompressorPair objects" +
", found " + str(type(c))))
except:
sot.print("ERROR: Failed to load suite '" + args.suite + "'")
sot.print(sys.exc_info()[1])
quit()
sot.print("Using suite '" + args.suite + "'")
else:
# default
suite = [
# tudocomp examples
Tudocomp(name='lfs_simst', algorithm='lfs_comp(sim_st)'),
Tudocomp(name='lfs_esa', algorithm='lfs_comp(esa)'),
# Tudocomp(name='lfs_st', algorithm='lfs_comp(st)'),
Tudocomp(name='lfs2', algorithm='lfs2'),
Tudocomp(name='lz78(ternary)', algorithm='lz78(coder=bit,lz78trie=ternary)'),
Tudocomp(name='lz78', algorithm='lz78'),
Tudocomp(name='lzw', algorithm='lzw'),
Tudocomp(name='repair(min=50)', algorithm='repair(bit,50)'),
Tudocomp(name='lzw', algorithm='lzw'),
Tudocomp(name='lzss', algorithm='lzss(bit)'),
Tudocomp(name='bwtzip', algorithm='bwt:rle:mtf:encode(huff)'),
Tudocomp(name='lcpcomp(t=5,arrays,scans(a=25))', algorithm='lcpcomp(coder=sle,threshold=5,comp=arrays,dec=scan(25))'),
Tudocomp(name='lzss_lcp(t=5,bit)', algorithm='lzss_lcp(coder=bit,threshold=5)'),
Tudocomp(name='lz78u(t=5,huff)', algorithm='lz78u(coder=bit,threshold=5,comp=buffering(huff))'),
Tudocomp(name='lcpcomp(t=5,heap,compact)', algorithm='lcpcomp(coder=sle,threshold="5",comp=heap,dec=compact)'),
Tudocomp(name='sle', algorithm='encode(sle)'),
Tudocomp(name='huff', algorithm='encode(huff)'),
Tudocomp(name='lzw(ternary)', algorithm='lzw(coder=bit,lz78trie=ternary)'),
# Some standard Linux compressors
StdCompressor(name='gzip -1', binary='gzip', cflags=['-1'], dflags=['-d']),
StdCompressor(name='gzip -9', binary='gzip', cflags=['-9'], dflags=['-d']),
StdCompressor(name='bzip2 -1', binary='bzip2', cflags=['-1'], dflags=['-d']),
StdCompressor(name='bzip2 -9', binary='bzip2', cflags=['-9'], dflags=['-d']),
StdCompressor(name='lzma -1', binary='lzma', cflags=['-1'], dflags=['-d']),
StdCompressor(name='lzma -9', binary='lzma', cflags=['-9'], dflags=['-d']),
#StdCompressor(name='lcpcompress', binary='lcpcompress', cflags=[''], dflags=['-d']),
]
sot.print("Using built-in default suite")
def memsize(num, suffix='B'):
for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']:
if abs(num) < 1024.0:
return "%3.1f%s%s" % (num, unit, suffix)
num /= 1024.0
return "%.1f%s%s" % (num, 'Yi', suffix)
def timesize(num, suffix='s'):
if(num < 1.0):
for unit in ['','m','mu','n']:
if num > 1.0:
return "%3.1f%s%s" % (num, unit, suffix)
num *= 1000
return "%.1f%s%s" % (num, '?', suffix)
else:
if(num < 600):
return "%3.1f%s" % (num, 's')
elif(num < 3600):
num /= 60
return "%3.1f%s" % (num, 'min')
elif(num > 3600):
num /= 3600
return "%3.1f%s" % (num, 'h')
def run_exec(x, infilename, outfilename):
args = list(x.args)
# Delete existing output file
if os.path.exists(outfilename):
os.remove(outfilename)
# Determine Output
if(x.outp == StdOut):
outfile = open(outfilename, "wb")
pipe_out = outfile
else:
outfile = None
pipe_out = logfile
args += ([x.outp, outfilename] if x.outp != None else [outfilename])
# Determine input
if(x.inp == StdIn):
infile = open(infilename, "rb")
pipe_in = infile
else:
infile = None
pipe_in = None
args += ([x.inp, infilename] if x.inp != None else [infilename])
# Call
t0 = time.time()
subprocess.check_call(args, stdin=pipe_in, stdout=pipe_out, stderr=logfile)
# Close files
outfile.close() if outfile else None
infile.close() if infile else None
# Yield time delta
return(time.time() - t0)
def measure_time(x, infilename, outfilename):
t=[]
for _ in range(0, args.iterations):
t = t + [run_exec(x, infilename, outfilename)]
return(statistics.median(t))
def measure_mem(x, infilename, outfilename):
massiffilename=tempfile.mktemp()
run_exec(
Exec(args=['valgrind', '-q', '--tool=massif', '--pages-as-heap=yes', '--massif-out-file=' + massiffilename] + x.args, inp=x.inp, outp=x.outp),
infilename, outfilename)
with open(massiffilename) as f:
maxmem=0
for line in f.readlines():
match = re.match('^mem_heap_B=([0-9]+)', line)
if match:
maxmem = max(maxmem,int(match.group(1)))
os.remove(massiffilename)
return(maxmem)
maxnicknamelength = len(max(suite, key=lambda p: len(p.name))[0] ) + 3
sot.print("Number of iterations per file: ", args.iterations)
for srcfname in args.files:
srchash = hashlib.sha256(open(srcfname, 'rb').read()).hexdigest()
srcsize = os.path.getsize(srcfname)
sot.file(srcfname, srcsize, srchash)
sot.header(("Compressor", "C Time", "C Memory", "C Rate", "D Time", "D Memory", "chk"));
logfilename = tempfile.mktemp()
decompressedfilename = tempfile.mktemp()
outfilename = tempfile.mktemp()
def print_column(content, format="%11s", sep="|", f=lambda x:x):
sot.cell(content, format, sep, f)
def end_row():
sot.end_row()
try:
with open(logfilename,"wb") as logfile:
for c in suite:
# nickname
print_column(c.name, "%"+ str(maxnicknamelength) +"s")
# compress time
try:
comp_time=measure_time(c.compress, srcfname, outfilename)
print_column(comp_time*1000, f=lambda x: timesize(x/1000))
except FileNotFoundError as e:
print_column("(ERR)", sep=">")
sot.print(" " + e.strerror)
continue
# compress memory
if mem_available:
comp_mem=measure_mem(c.compress, srcfname, outfilename)
print_column(comp_mem,f=memsize)
else:
print_column("(N/A)")
# compress rate
outputsize=os.path.getsize(outfilename)
print_column(float(outputsize) / float(srcsize), format="%10.4f%%", f=lambda x: 100*x)
# decompress time
dec_time = measure_time(c.decompress, outfilename, decompressedfilename)
print_column(dec_time*1000,f=lambda x: timesize(x/1000))
# decompress memory
if mem_available:
dec_mem = measure_mem(c.decompress, outfilename, decompressedfilename)
print_column(dec_mem,f=memsize)
else:
print_column("(N/A)")
# decompress check
decompressedhash = hashlib.sha256(
open(decompressedfilename, 'rb').read()).hexdigest()
if decompressedhash != srchash:
print_column("FAIL", format="%5s")
else:
print_column("OK", format="%5s")
# EOL
end_row()
except:
sot.print()
sot.print("ERROR:", sys.exc_info()[0])
sot.print(sys.exc_info()[1])
with open(logfilename, 'r') as fin: sot.print(fin.read())
os.remove(logfilename)
if os.path.exists(decompressedfilename):
os.remove(decompressedfilename)
if os.path.exists(outfilename):
os.remove(outfilename)
sot.flush()
| etc/compare.py | 12,544 | !/usr/bin/env python3 Argument parser Ensure that the input files are readable Check that valgrind is available for memory measurement Program execution definition args is required Compressor Pair definition Define suite Evaluate suite as Python sanity checks default tudocomp examples Tudocomp(name='lfs_st', algorithm='lfs_comp(st)'), Some standard Linux compressorsStdCompressor(name='lcpcompress', binary='lcpcompress', cflags=[''], dflags=['-d']), Delete existing output file Determine Output Determine input Call Close files Yield time delta nickname compress time compress memory compress rate decompress time decompress memory decompress check EOL | 679 | en | 0.512554 |
# coding: utf-8
import re
import six
from huaweicloudsdkcore.sdk_response import SdkResponse
from huaweicloudsdkcore.utils.http_utils import sanitize_for_serialization
class StopAppResponse(SdkResponse):
"""
Attributes:
openapi_types (dict): The key is attribute name
and the value is attribute type.
attribute_map (dict): The key is attribute name
and the value is json key in definition.
"""
sensitive_list = []
openapi_types = {
'app_id': 'str',
'state': 'AppState',
'x_request_id': 'str'
}
attribute_map = {
'app_id': 'app_id',
'state': 'state',
'x_request_id': 'X-request-Id'
}
def __init__(self, app_id=None, state=None, x_request_id=None):
"""StopAppResponse - a model defined in huaweicloud sdk"""
super(StopAppResponse, self).__init__()
self._app_id = None
self._state = None
self._x_request_id = None
self.discriminator = None
if app_id is not None:
self.app_id = app_id
if state is not None:
self.state = state
if x_request_id is not None:
self.x_request_id = x_request_id
@property
def app_id(self):
"""Gets the app_id of this StopAppResponse.
应用id
:return: The app_id of this StopAppResponse.
:rtype: str
"""
return self._app_id
@app_id.setter
def app_id(self, app_id):
"""Sets the app_id of this StopAppResponse.
应用id
:param app_id: The app_id of this StopAppResponse.
:type: str
"""
self._app_id = app_id
@property
def state(self):
"""Gets the state of this StopAppResponse.
:return: The state of this StopAppResponse.
:rtype: AppState
"""
return self._state
@state.setter
def state(self, state):
"""Sets the state of this StopAppResponse.
:param state: The state of this StopAppResponse.
:type: AppState
"""
self._state = state
@property
def x_request_id(self):
"""Gets the x_request_id of this StopAppResponse.
:return: The x_request_id of this StopAppResponse.
:rtype: str
"""
return self._x_request_id
@x_request_id.setter
def x_request_id(self, x_request_id):
"""Sets the x_request_id of this StopAppResponse.
:param x_request_id: The x_request_id of this StopAppResponse.
:type: str
"""
self._x_request_id = x_request_id
def to_dict(self):
"""Returns the model properties as a dict"""
result = {}
for attr, _ in six.iteritems(self.openapi_types):
value = getattr(self, attr)
if isinstance(value, list):
result[attr] = list(map(
lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
value
))
elif hasattr(value, "to_dict"):
result[attr] = value.to_dict()
elif isinstance(value, dict):
result[attr] = dict(map(
lambda item: (item[0], item[1].to_dict())
if hasattr(item[1], "to_dict") else item,
value.items()
))
else:
if attr in self.sensitive_list:
result[attr] = "****"
else:
result[attr] = value
return result
def to_str(self):
"""Returns the string representation of the model"""
import simplejson as json
if six.PY2:
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
return json.dumps(sanitize_for_serialization(self), ensure_ascii=False)
def __repr__(self):
"""For `print`"""
return self.to_str()
def __eq__(self, other):
"""Returns true if both objects are equal"""
if not isinstance(other, StopAppResponse):
return False
return self.__dict__ == other.__dict__
def __ne__(self, other):
"""Returns true if both objects are not equal"""
return not self == other
| huaweicloud-sdk-cloudrtc/huaweicloudsdkcloudrtc/v2/model/stop_app_response.py | 4,312 | Attributes:
openapi_types (dict): The key is attribute name
and the value is attribute type.
attribute_map (dict): The key is attribute name
and the value is json key in definition.
Returns true if both objects are equal
StopAppResponse - a model defined in huaweicloud sdk
Returns true if both objects are not equal
For `print`
Gets the app_id of this StopAppResponse.
应用id
:return: The app_id of this StopAppResponse.
:rtype: str
Sets the app_id of this StopAppResponse.
应用id
:param app_id: The app_id of this StopAppResponse.
:type: str
Gets the state of this StopAppResponse.
:return: The state of this StopAppResponse.
:rtype: AppState
Sets the state of this StopAppResponse.
:param state: The state of this StopAppResponse.
:type: AppState
Returns the model properties as a dict
Returns the string representation of the model
Gets the x_request_id of this StopAppResponse.
:return: The x_request_id of this StopAppResponse.
:rtype: str
Sets the x_request_id of this StopAppResponse.
:param x_request_id: The x_request_id of this StopAppResponse.
:type: str
coding: utf-8 | 1,142 | en | 0.668513 |
'''
Description:
Play music on Spotify with python.
Author: AlejandroV
Version: 1.0
Video: https://youtu.be/Vj64pkXtz28
'''
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy
import webbrowser as web
import pyautogui
from time import sleep
# your credentials
client_id = 'YOUR_CLIENT_ID_HERE'
client_secret = 'YOUR_CLIENT_SECRET_HERE'
flag = 0
# artist and name of the song
author = ''
song = 'bad guy'.upper()
if len(author) > 0:
# authenticate
sp = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials(client_id, client_secret))
result = sp.search(author)
for i in range(0, len(result["tracks"]["items"])):
# songs by artist
name_song = result["tracks"]["items"][i]["name"].upper()
if song in name_song:
flag = 1
web.open(result["tracks"]["items"][i]["uri"])
sleep(5)
pyautogui.press("enter")
break
# if song by artist not found
if flag == 0:
song = song.replace(" ", "%20")
web.open(f'spotify:search:{song}')
sleep(5)
for i in range(18):
pyautogui.press("tab")
for i in range(2):
pyautogui.press("enter")
sleep(1) | playmusic_spoty.py | 1,219 | Description:
Play music on Spotify with python.
Author: AlejandroV
Version: 1.0
Video: https://youtu.be/Vj64pkXtz28
your credentials artist and name of the song authenticate songs by artist if song by artist not found | 220 | en | 0.88465 |
# Copyright 2018 The Texar 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.
"""
Miscellaneous Utility functions.
"""
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
# pylint: disable=invalid-name, no-member, no-name-in-module, protected-access
# pylint: disable=redefined-outer-name, too-many-arguments
from typing import List, Union
import inspect
import funcsigs
from pydoc import locate
import copy
import collections
import numpy as np
import tensorflow as tf
from texar.tf.hyperparams import HParams
from texar.tf.utils.dtypes import is_str, is_callable, compat_as_text, \
_maybe_list_to_array
# pylint: disable=anomalous-backslash-in-string
MAX_SEQ_LENGTH = np.iinfo(np.int32).max
# Some modules cannot be imported directly,
# e.g., `import tensorflow.train` fails.
# Such modules are treated in a special way in utils like `get_class` as below.
# _unimportable_modules = {
# 'tensorflow.train', 'tensorflow.keras.regularizers'
# }
__all__ = [
"_inspect_getargspec",
"get_args",
"get_default_arg_values",
"check_or_get_class",
"get_class",
"check_or_get_instance",
"get_instance",
"check_or_get_instance_with_redundant_kwargs",
"get_instance_with_redundant_kwargs",
"get_function",
"call_function_with_redundant_kwargs",
"get_instance_kwargs",
"dict_patch",
"dict_lookup",
"dict_fetch",
"dict_pop",
"flatten_dict",
"strip_token",
"strip_eos",
"strip_bos",
"strip_special_tokens",
"str_join",
"map_ids_to_strs",
"default_str",
"uniquify_str",
"ceildiv",
"straight_through",
"truncate_seq_pair",
]
# TODO(zhiting): complete this
def _expand_name(name):
"""Replaces common shorthands with respective full names.
"tf.xxx" --> "tensorflow.xxx"
"tx.xxx" --> "texar.tf.xxx"
"""
return name
def _inspect_getargspec(fn):
"""Returns `inspect.getargspec(fn)` for Py2 and `inspect.getfullargspec(fn)`
for Py3
"""
try:
return inspect.getfullargspec(fn)
except AttributeError:
try:
return inspect.getargspec(fn)
except TypeError:
return inspect.getargspec(fn.__call__)
def get_args(fn):
"""Gets the arguments of a function.
Args:
fn (callable): The function to inspect.
Returns:
list: A list of argument names (str) of the function.
"""
argspec = _inspect_getargspec(fn)
args = argspec.args
# Empty args can be because `fn` is decorated. Use `funcsigs.signature`
# to re-do the inspect
if len(args) == 0:
args = funcsigs.signature(fn).parameters.keys()
args = list(args)
return args
def get_default_arg_values(fn):
"""Gets the arguments and respective default values of a function.
Only arguments with default values are included in the output dictionary.
Args:
fn (callable): The function to inspect.
Returns:
dict: A dictionary that maps argument names (str) to their default
values. The dictionary is empty if no arguments have default values.
"""
argspec = _inspect_getargspec(fn)
if argspec.defaults is None:
return {}
num_defaults = len(argspec.defaults)
return dict(zip(argspec.args[-num_defaults:], argspec.defaults))
def check_or_get_class(class_or_name, module_path=None, superclass=None):
"""Returns the class and checks if the class inherits :attr:`superclass`.
Args:
class_or_name: Name or full path to the class, or the class itself.
module_paths (list, optional): Paths to candidate modules to search
for the class. This is used if :attr:`class_or_name` is a string and
the class cannot be located solely based on :attr:`class_or_name`.
The first module in the list that contains the class
is used.
superclass (optional): A (list of) classes that the target class
must inherit.
Returns:
The target class.
Raises:
ValueError: If class is not found based on :attr:`class_or_name` and
:attr:`module_paths`.
TypeError: If class does not inherits :attr:`superclass`.
"""
class_ = class_or_name
if is_str(class_):
class_ = get_class(class_, module_path)
if superclass is not None:
if not issubclass(class_, superclass):
raise TypeError(
"A subclass of {} is expected. Got: {}".format(
superclass, class_))
return class_
def get_class(class_name, module_paths=None):
"""Returns the class based on class name.
Args:
class_name (str): Name or full path to the class.
module_paths (list): Paths to candidate modules to search for the
class. This is used if the class cannot be located solely based on
`class_name`. The first module in the list that contains the class
is used.
Returns:
The target class.
Raises:
ValueError: If class is not found based on :attr:`class_name` and
:attr:`module_paths`.
"""
class_ = locate(class_name)
if (class_ is None) and (module_paths is not None):
for module_path in module_paths:
# if module_path in _unimportable_modules:
# Special treatment for unimportable modules by directly
# accessing the class
class_ = locate('.'.join([module_path, class_name]))
if class_ is not None:
break
# else:
# module = importlib.import_module(module_path)
# if class_name in dir(module):
# class_ = getattr(module, class_name)
# break
if class_ is None:
raise ValueError(
"Class not found in {}: {}".format(module_paths, class_name))
return class_
def check_or_get_instance(ins_or_class_or_name, kwargs, module_paths=None,
classtype=None):
"""Returns a class instance and checks types.
Args:
ins_or_class_or_name: Can be of 3 types:
- A class to instantiate.
- A string of the name or full path to a class to \
instantiate.
- The class instance to check types.
kwargs (dict): Keyword arguments for the class constructor. Ignored
if `ins_or_class_or_name` is a class instance.
module_paths (list, optional): Paths to candidate modules to
search for the class. This is used if the class cannot be
located solely based on :attr:`class_name`. The first module
in the list that contains the class is used.
classtype (optional): A (list of) class of which the instance must
be an instantiation.
Raises:
ValueError: If class is not found based on :attr:`class_name` and
:attr:`module_paths`.
ValueError: If :attr:`kwargs` contains arguments that are invalid
for the class construction.
TypeError: If the instance is not an instantiation of
:attr:`classtype`.
"""
ret = ins_or_class_or_name
if is_str(ret) or isinstance(ret, type):
ret = get_instance(ret, kwargs, module_paths)
if classtype is not None:
if not isinstance(ret, classtype):
raise TypeError(
"An instance of {} is expected. Got: {}".format(classtype, ret))
return ret
def get_instance(class_or_name, kwargs, module_paths=None):
"""Creates a class instance.
Args:
class_or_name: A class, or its name or full path to a class to
instantiate.
kwargs (dict): Keyword arguments for the class constructor.
module_paths (list, optional): Paths to candidate modules to
search for the class. This is used if the class cannot be
located solely based on :attr:`class_name`. The first module
in the list that contains the class is used.
Returns:
A class instance.
Raises:
ValueError: If class is not found based on :attr:`class_or_name` and
:attr:`module_paths`.
ValueError: If :attr:`kwargs` contains arguments that are invalid
for the class construction.
"""
# Locate the class
class_ = class_or_name
if is_str(class_):
class_ = get_class(class_, module_paths)
# Check validity of arguments
class_args = set(get_args(class_.__init__))
if kwargs is None:
kwargs = {}
for key in kwargs.keys():
if key not in class_args:
raise ValueError(
"Invalid argument for class %s.%s: %s, valid args: %s" %
(class_.__module__, class_.__name__, key, list(class_args)))
return class_(**kwargs)
def check_or_get_instance_with_redundant_kwargs(
ins_or_class_or_name, kwargs, module_paths=None, classtype=None):
"""Returns a class instance and checks types.
Only those keyword arguments in :attr:`kwargs` that are included in the
class construction method are used.
Args:
ins_or_class_or_name: Can be of 3 types:
- A class to instantiate.
- A string of the name or module path to a class to \
instantiate.
- The class instance to check types.
kwargs (dict): Keyword arguments for the class constructor.
module_paths (list, optional): Paths to candidate modules to
search for the class. This is used if the class cannot be
located solely based on :attr:`class_name`. The first module
in the list that contains the class is used.
classtype (optional): A (list of) classes of which the instance must
be an instantiation.
Raises:
ValueError: If class is not found based on :attr:`class_name` and
:attr:`module_paths`.
ValueError: If :attr:`kwargs` contains arguments that are invalid
for the class construction.
TypeError: If the instance is not an instantiation of
:attr:`classtype`.
"""
ret = ins_or_class_or_name
if is_str(ret) or isinstance(ret, type):
ret = get_instance_with_redundant_kwargs(ret, kwargs, module_paths)
if classtype is not None:
if not isinstance(ret, classtype):
raise TypeError(
"An instance of {} is expected. Got: {}".format(classtype, ret))
return ret
def get_instance_with_redundant_kwargs(
class_name, kwargs, module_paths=None):
"""Creates a class instance.
Only those keyword arguments in :attr:`kwargs` that are included in the
class construction method are used.
Args:
class_name (str): A class or its name or module path.
kwargs (dict): A dictionary of arguments for the class constructor. It
may include invalid arguments which will be ignored.
module_paths (list of str): A list of paths to candidate modules to
search for the class. This is used if the class cannot be located
solely based on :attr:`class_name`. The first module in the list
that contains the class is used.
Returns:
A class instance.
Raises:
ValueError: If class is not found based on :attr:`class_name` and
:attr:`module_paths`.
"""
# Locate the class
class_ = get_class(class_name, module_paths)
# Select valid arguments
selected_kwargs = {}
class_args = set(get_args(class_.__init__))
if kwargs is None:
kwargs = {}
for key, value in kwargs.items():
if key in class_args:
selected_kwargs[key] = value
return class_(**selected_kwargs)
def get_function(fn_or_name, module_paths=None):
"""Returns the function of specified name and module.
Args:
fn_or_name (str or callable): Name or full path to a function, or the
function itself.
module_paths (list, optional): A list of paths to candidate modules to
search for the function. This is used only when the function
cannot be located solely based on :attr:`fn_or_name`. The first
module in the list that contains the function is used.
Returns:
A function.
"""
if is_callable(fn_or_name):
return fn_or_name
fn = locate(fn_or_name)
if (fn is None) and (module_paths is not None):
for module_path in module_paths:
# if module_path in _unimportable_modules:
fn = locate('.'.join([module_path, fn_or_name]))
if fn is not None:
break
# module = importlib.import_module(module_path)
# if fn_name in dir(module):
# fn = getattr(module, fn_name)
# break
if fn is None:
raise ValueError(
"Method not found in {}: {}".format(module_paths, fn_or_name))
return fn
def call_function_with_redundant_kwargs(fn, kwargs):
"""Calls a function and returns the results.
Only those keyword arguments in :attr:`kwargs` that are included in the
function's argument list are used to call the function.
Args:
fn (function): A callable. If :attr:`fn` is not a python function,
:attr:`fn.__call__` is called.
kwargs (dict): A `dict` of arguments for the callable. It
may include invalid arguments which will be ignored.
Returns:
The returned results by calling :attr:`fn`.
"""
try:
fn_args = set(get_args(fn))
except TypeError:
fn_args = set(get_args(fn.__cal__))
if kwargs is None:
kwargs = {}
# Select valid arguments
selected_kwargs = {}
for key, value in kwargs.items():
if key in fn_args:
selected_kwargs[key] = value
return fn(**selected_kwargs)
def get_instance_kwargs(kwargs, hparams):
"""Makes a dict of keyword arguments with the following structure:
`kwargs_ = {'hparams': dict(hparams), **kwargs}`.
This is typically used for constructing a module which takes a set of
arguments as well as a argument named `hparams`.
Args:
kwargs (dict): A dict of keyword arguments. Can be `None`.
hparams: A dict or an instance of :class:`~texar.tf.HParams` Can be `None`.
Returns:
A `dict` that contains the keyword arguments in :attr:`kwargs`, and
an additional keyword argument named `hparams`.
"""
if hparams is None or isinstance(hparams, dict):
kwargs_ = {'hparams': hparams}
elif isinstance(hparams, HParams):
kwargs_ = {'hparams': hparams.todict()}
else:
raise ValueError(
'`hparams` must be a dict, an instance of HParams, or a `None`.')
kwargs_.update(kwargs or {})
return kwargs_
def dict_patch(tgt_dict, src_dict):
"""Recursively patch :attr:`tgt_dict` by adding items from :attr:`src_dict`
that do not exist in :attr:`tgt_dict`.
If respective items in :attr:`src_dict` and :attr:`tgt_dict` are both
`dict`, the :attr:`tgt_dict` item is patched recursively.
Args:
tgt_dict (dict): Target dictionary to patch.
src_dict (dict): Source dictionary.
Return:
dict: The new :attr:`tgt_dict` that is patched.
"""
if src_dict is None:
return tgt_dict
for key, value in src_dict.items():
if key not in tgt_dict:
tgt_dict[key] = copy.deepcopy(value)
elif isinstance(value, dict) and isinstance(tgt_dict[key], dict):
tgt_dict[key] = dict_patch(tgt_dict[key], value)
return tgt_dict
def dict_lookup(dict_, keys, default=None):
"""Looks up :attr:`keys` in the dict, returns the corresponding values.
The :attr:`default` is used for keys not present in the dict.
Args:
dict_ (dict): A dictionary for lookup.
keys: A numpy array or a (possibly nested) list of keys.
default (optional): Value to be returned when a key is not in
:attr:`dict_`. Error is raised if :attr:`default` is not given and
key is not in the dict.
Returns:
A numpy array of values with the same structure as :attr:`keys`.
Raises:
TypeError: If key is not in :attr:`dict_` and :attr:`default` is `None`.
"""
return np.vectorize(lambda x: dict_.get(x, default))(keys)
def dict_fetch(src_dict, tgt_dict_or_keys):
"""Fetches a sub dict of :attr:`src_dict` with the keys in
:attr:`tgt_dict_or_keys`.
Args:
src_dict: A dict or instance of :class:`~texar.tf.HParams`.
The source dict to fetch values from.
tgt_dict_or_keys: A dict, instance of :class:`~texar.tf.HParams`,
or a list (or a dict_keys) of keys to be included in the output
dict.
Returns:
A new dict that is a subdict of :attr:`src_dict`.
"""
if src_dict is None:
return src_dict
if isinstance(tgt_dict_or_keys, HParams):
tgt_dict_or_keys = tgt_dict_or_keys.todict()
if isinstance(tgt_dict_or_keys, dict):
tgt_dict_or_keys = tgt_dict_or_keys.keys()
keys = list(tgt_dict_or_keys)
if isinstance(src_dict, HParams):
src_dict = src_dict.todict()
return {k: src_dict[k] for k in keys if k in src_dict}
def dict_pop(dict_, pop_keys, default=None):
"""Removes keys from a dict and returns their values.
Args:
dict_ (dict): A dictionary from which items are removed.
pop_keys: A key or a list of keys to remove and return respective
values or :attr:`default`.
default (optional): Value to be returned when a key is not in
:attr:`dict_`. The default value is `None`.
Returns:
A `dict` of the items removed from :attr:`dict_`.
"""
if not isinstance(pop_keys, (list, tuple)):
pop_keys = [pop_keys]
ret_dict = {key: dict_.pop(key, default) for key in pop_keys}
return ret_dict
def flatten_dict(dict_, parent_key="", sep="."):
"""Flattens a nested dictionary. Namedtuples within the dictionary are
converted to dicts.
Adapted from:
https://github.com/google/seq2seq/blob/master/seq2seq/models/model_base.py
Args:
dict_ (dict): The dictionary to flatten.
parent_key (str): A prefix to prepend to each key.
sep (str): Separator that intervenes between parent and child keys.
E.g., if `sep` == '.', then `{ "a": { "b": 3 } }` is converted
into `{ "a.b": 3 }`.
Returns:
A new flattened `dict`.
"""
items = []
for key, value in dict_.items():
key_ = parent_key + sep + key if parent_key else key
if isinstance(value, collections.MutableMapping):
items.extend(flatten_dict(value, key_, sep=sep).items())
elif isinstance(value, tuple) and hasattr(value, "_asdict"):
dict_items = collections.OrderedDict(zip(value._fields, value))
items.extend(flatten_dict(dict_items, key_, sep=sep).items())
else:
items.append((key_, value))
return dict(items)
def default_str(str_, default_str):
"""Returns :attr:`str_` if it is not `None` or empty, otherwise returns
:attr:`default_str`.
Args:
str_: A string.
default_str: A string.
Returns:
Either :attr:`str_` or :attr:`default_str`.
"""
if str_ is not None and str_ != "":
return str_
else:
return default_str
def uniquify_str(str_, str_set):
"""Uniquifies :attr:`str_` if :attr:`str_` is included in :attr:`str_set`.
This is done by appending a number to :attr:`str_`. Returns
:attr:`str_` directly if it is not included in :attr:`str_set`.
Args:
str_ (string): A string to uniquify.
str_set (set, dict, or list): A collection of strings. The returned
string is guaranteed to be different from the elements in the
collection.
Returns:
The uniquified string. Returns :attr:`str_` directly if it is
already unique.
Example:
.. code-block:: python
print(uniquify_str('name', ['name', 'name_1']))
# 'name_2'
"""
if str_ not in str_set:
return str_
else:
for i in range(1, len(str_set) + 1):
unique_str = str_ + "_%d" % i
if unique_str not in str_set:
return unique_str
raise ValueError("Fails to uniquify string: " + str_)
def _recur_split(s, dtype_as):
"""Splits (possibly nested list of) strings recursively.
"""
if is_str(s):
return _maybe_list_to_array(s.split(), dtype_as)
else:
s_ = [_recur_split(si, dtype_as) for si in s]
return _maybe_list_to_array(s_, s)
def strip_token(str_, token, is_token_list=False, compat=True):
"""Returns a copy of strings with leading and trailing tokens removed.
Note that besides :attr:`token`, all leading and trailing whitespace
characters are also removed.
If :attr:`is_token_list` is False, then the function assumes tokens in
:attr:`str_` are separated with whitespace character.
Args:
str_: A `str`, or an `n`-D numpy array or (possibly nested)
list of `str`.
token (str): The token to strip, e.g., the '<PAD>' token defined in
:class:`~texar.tf.data.SpecialTokens`.PAD
is_token_list (bool): Whether each sentence in :attr:`str_` is a list
of tokens. If False, each sentence in :attr:`str_` is assumed to
contain tokens separated with space character.
compat (bool): Whether to convert tokens into `unicode` (Python 2)
or `str` (Python 3).
Returns:
The stripped strings of the same structure/shape as :attr:`str_`.
Example:
.. code-block:: python
str_ = '<PAD> a sentence <PAD> <PAD> '
str_stripped = strip_token(str_, '<PAD>')
# str_stripped == 'a sentence'
str_ = ['<PAD>', 'a', 'sentence', '<PAD>', '<PAD>', '', '']
str_stripped = strip_token(str_, '<PAD>', is_token_list=True)
# str_stripped == 'a sentence'
"""
def _recur_strip(s):
if is_str(s):
if token == "":
return ' '.join(s.strip().split())
else:
return ' '.join(s.strip().split()).\
replace(' ' + token, '').replace(token + ' ', '')
else:
s_ = [_recur_strip(si) for si in s]
return _maybe_list_to_array(s_, s)
s = str_
if compat:
s = compat_as_text(s)
if is_token_list:
s = str_join(s, compat=False)
strp_str = _recur_strip(s)
if is_token_list:
strp_str = _recur_split(strp_str, str_)
return strp_str
def strip_eos(str_, eos_token='<EOS>', is_token_list=False, compat=True):
"""Remove the EOS token and all subsequent tokens.
If :attr:`is_token_list` is False, then the function assumes tokens in
:attr:`str_` are separated with whitespace character.
Args:
str_: A `str`, or an `n`-D numpy array or (possibly nested)
list of `str`.
eos_token (str): The EOS token. Default is '<EOS>' as defined in
:class:`~texar.tf.data.SpecialTokens`.EOS
is_token_list (bool): Whether each sentence in :attr:`str_` is a list
of tokens. If False, each sentence in :attr:`str_` is assumed to
contain tokens separated with space character.
compat (bool): Whether to convert tokens into `unicode` (Python 2)
or `str` (Python 3).
Returns:
Strings of the same structure/shape as :attr:`str_`.
"""
def _recur_strip(s):
if is_str(s):
s_tokens = s.split()
if eos_token in s_tokens:
return ' '.join(s_tokens[:s_tokens.index(eos_token)])
else:
return s
else:
s_ = [_recur_strip(si) for si in s]
return _maybe_list_to_array(s_, s)
s = str_
if compat:
s = compat_as_text(s)
if is_token_list:
s = str_join(s, compat=False)
strp_str = _recur_strip(s)
if is_token_list:
strp_str = _recur_split(strp_str, str_)
return strp_str
_strip_eos_ = strip_eos
def strip_bos(str_, bos_token='<BOS>', is_token_list=False, compat=True):
"""Remove all leading BOS tokens.
Note that besides :attr:`bos_token`, all leading and trailing whitespace
characters are also removed.
If :attr:`is_token_list` is False, then the function assumes tokens in
:attr:`str_` are separated with whitespace character.
Args:
str_: A `str`, or an `n`-D numpy array or (possibly nested)
list of `str`.
bos_token (str): The BOS token. Default is '<BOS>' as defined in
:class:`~texar.tf.data.SpecialTokens`.BOS
is_token_list (bool): Whether each sentence in :attr:`str_` is a list
of tokens. If False, each sentence in :attr:`str_` is assumed to
contain tokens separated with space character.
compat (bool): Whether to convert tokens into `unicode` (Python 2)
or `str` (Python 3).
Returns:
Strings of the same structure/shape as :attr:`str_`.
"""
def _recur_strip(s):
if is_str(s):
if bos_token == '':
return ' '.join(s.strip().split())
else:
return ' '.join(s.strip().split()).replace(bos_token + ' ', '')
else:
s_ = [_recur_strip(si) for si in s]
return _maybe_list_to_array(s_, s)
s = str_
if compat:
s = compat_as_text(s)
if is_token_list:
s = str_join(s, compat=False)
strp_str = _recur_strip(s)
if is_token_list:
strp_str = _recur_split(strp_str, str_)
return strp_str
_strip_bos_ = strip_bos
def strip_special_tokens(str_, strip_pad='<PAD>', strip_bos='<BOS>',
strip_eos='<EOS>', is_token_list=False, compat=True):
"""Removes special tokens in strings, including:
- Removes EOS and all subsequent tokens
- Removes leading and and trailing PAD tokens
- Removes leading BOS tokens
Note that besides the special tokens, all leading and trailing whitespace
characters are also removed.
This is a joint function of :func:`strip_eos`, :func:`strip_pad`, and
:func:`strip_bos`
Args:
str_: A `str`, or an `n`-D numpy array or (possibly nested)
list of `str`.
strip_pad (str): The PAD token to strip from the strings (i.e., remove
the leading and trailing PAD tokens of the strings). Default
is '<PAD>' as defined in
:class:`~texar.tf.data.SpecialTokens`.PAD.
Set to `None` or `False` to disable the stripping.
strip_bos (str): The BOS token to strip from the strings (i.e., remove
the leading BOS tokens of the strings).
Default is '<BOS>' as defined in
:class:`~texar.tf.data.SpecialTokens`.BOS.
Set to `None` or `False` to disable the stripping.
strip_eos (str): The EOS token to strip from the strings (i.e., remove
the EOS tokens and all subsequent tokens of the strings).
Default is '<EOS>' as defined in
:class:`~texar.tf.data.SpecialTokens`.EOS.
Set to `None` or `False` to disable the stripping.
is_token_list (bool): Whether each sentence in :attr:`str_` is a list
of tokens. If False, each sentence in :attr:`str_` is assumed to
contain tokens separated with space character.
compat (bool): Whether to convert tokens into `unicode` (Python 2)
or `str` (Python 3).
Returns:
Strings of the same shape of :attr:`str_` with special tokens stripped.
"""
s = str_
if compat:
s = compat_as_text(s)
if is_token_list:
s = str_join(s, compat=False)
if strip_eos is not None and strip_eos is not False:
s = _strip_eos_(s, strip_eos, is_token_list=False, compat=False)
if strip_pad is not None and strip_pad is not False:
s = strip_token(s, strip_pad, is_token_list=False, compat=False)
if strip_bos is not None and strip_bos is not False:
s = _strip_bos_(s, strip_bos, is_token_list=False, compat=False)
if is_token_list:
s = _recur_split(s, str_)
return s
def str_join(tokens, sep=' ', compat=True):
"""Concats :attr:`tokens` along the last dimension with intervening
occurrences of :attr:`sep`.
Args:
tokens: An `n`-D numpy array or (possibly nested) list of `str`.
sep (str): The string intervening between the tokens.
compat (bool): Whether to convert tokens into `unicode` (Python 2)
or `str` (Python 3).
Returns:
An `(n-1)`-D numpy array (or list) of `str`.
"""
def _recur_join(s):
if len(s) == 0:
return ''
elif is_str(s[0]):
return sep.join(s)
else:
s_ = [_recur_join(si) for si in s]
return _maybe_list_to_array(s_, s)
if compat:
tokens = compat_as_text(tokens)
str_ = _recur_join(tokens)
return str_
def map_ids_to_strs(ids, vocab, join=True, strip_pad='<PAD>',
strip_bos='<BOS>', strip_eos='<EOS>', compat=True):
"""Transforms `int` indexes to strings by mapping ids to tokens,
concatenating tokens into sentences, and stripping special tokens, etc.
Args:
ids: An n-D numpy array or (possibly nested) list of `int` indexes.
vocab: An instance of :class:`~texar.tf.data.Vocab`.
join (bool): Whether to concat along the last dimension of the
the tokens into a string separated with a space character.
strip_pad (str): The PAD token to strip from the strings (i.e., remove
the leading and trailing PAD tokens of the strings). Default
is '<PAD>' as defined in
:class:`~texar.tf.data.SpecialTokens`.PAD.
Set to `None` or `False` to disable the stripping.
strip_bos (str): The BOS token to strip from the strings (i.e., remove
the leading BOS tokens of the strings).
Default is '<BOS>' as defined in
:class:`~texar.tf.data.SpecialTokens`.BOS.
Set to `None` or `False` to disable the stripping.
strip_eos (str): The EOS token to strip from the strings (i.e., remove
the EOS tokens and all subsequent tokens of the strings).
Default is '<EOS>' as defined in
:class:`~texar.tf.data.SpecialTokens`.EOS.
Set to `None` or `False` to disable the stripping.
Returns:
If :attr:`join` is True, returns a `(n-1)`-D numpy array (or list) of
concatenated strings. If :attr:`join` is False, returns an `n`-D numpy
array (or list) of str tokens.
Example:
.. code-block:: python
text_ids = [[1, 9, 6, 2, 0, 0], [1, 28, 7, 8, 2, 0]]
text = map_ids_to_strs(text_ids, data.vocab)
# text == ['a sentence', 'parsed from ids']
text = map_ids_to_strs(
text_ids, data.vocab, join=False,
strip_pad=None, strip_bos=None, strip_eos=None)
# text == [['<BOS>', 'a', 'sentence', '<EOS>', '<PAD>', '<PAD>'],
# ['<BOS>', 'parsed', 'from', 'ids', '<EOS>', '<PAD>']]
"""
tokens = vocab.map_ids_to_tokens_py(ids)
if isinstance(ids, (list, tuple)):
tokens = tokens.tolist()
if compat:
tokens = compat_as_text(tokens)
str_ = str_join(tokens, compat=False)
str_ = strip_special_tokens(
str_, strip_pad=strip_pad, strip_bos=strip_bos, strip_eos=strip_eos,
compat=False)
if join:
return str_
else:
return _recur_split(str_, ids)
def ceildiv(a, b):
"""Divides with ceil.
E.g., `5 / 2 = 2.5`, `ceildiv(5, 2) = 3`.
Args:
a (int): Dividend integer.
b (int): Divisor integer.
Returns:
int: Ceil quotient.
"""
return -(-a // b)
def straight_through(fw_tensor, bw_tensor):
"""Use a tensor in forward pass while backpropagating gradient to another.
Args:
fw_tensor: A tensor to be used in the forward pass.
bw_tensor: A tensor to which gradient is backpropagated. Must have the
same shape and type with :attr:`fw_tensor`.
Returns:
A tensor of the same shape and value with :attr:`fw_tensor` but will
direct gradient to bw_tensor.
"""
return tf.stop_gradient(fw_tensor) + bw_tensor - tf.stop_gradient(bw_tensor)
def truncate_seq_pair(tokens_a: Union[List[int], List[str]],
tokens_b: Union[List[int], List[str]],
max_length: int):
r"""Truncates a sequence pair in place to the maximum length.
This is a simple heuristic which will always truncate the longer sequence
one token at a time. This makes more sense than truncating an equal
percent of tokens from each, since if one sequence is very short then
each token that's truncated likely contains more information than a
longer sequence.
Example:
.. code-block:: python
tokens_a = [1, 2, 3, 4, 5]
tokens_b = [6, 7]
truncate_seq_pair(tokens_a, tokens_b, 5)
tokens_a # [1, 2, 3]
tokens_b # [6, 7]
Args:
tokens_a: A list of tokens or token ids.
tokens_b: A list of tokens or token ids.
max_length: maximum sequence length.
"""
while True:
total_length = len(tokens_a) + len(tokens_b)
if total_length <= max_length:
break
if len(tokens_a) > len(tokens_b):
tokens_a.pop()
else:
tokens_b.pop()
| texar/tf/utils/utils.py | 34,576 | Replaces common shorthands with respective full names.
"tf.xxx" --> "tensorflow.xxx"
"tx.xxx" --> "texar.tf.xxx"
Returns `inspect.getargspec(fn)` for Py2 and `inspect.getfullargspec(fn)`
for Py3
Splits (possibly nested list of) strings recursively.
Calls a function and returns the results.
Only those keyword arguments in :attr:`kwargs` that are included in the
function's argument list are used to call the function.
Args:
fn (function): A callable. If :attr:`fn` is not a python function,
:attr:`fn.__call__` is called.
kwargs (dict): A `dict` of arguments for the callable. It
may include invalid arguments which will be ignored.
Returns:
The returned results by calling :attr:`fn`.
Divides with ceil.
E.g., `5 / 2 = 2.5`, `ceildiv(5, 2) = 3`.
Args:
a (int): Dividend integer.
b (int): Divisor integer.
Returns:
int: Ceil quotient.
Returns the class and checks if the class inherits :attr:`superclass`.
Args:
class_or_name: Name or full path to the class, or the class itself.
module_paths (list, optional): Paths to candidate modules to search
for the class. This is used if :attr:`class_or_name` is a string and
the class cannot be located solely based on :attr:`class_or_name`.
The first module in the list that contains the class
is used.
superclass (optional): A (list of) classes that the target class
must inherit.
Returns:
The target class.
Raises:
ValueError: If class is not found based on :attr:`class_or_name` and
:attr:`module_paths`.
TypeError: If class does not inherits :attr:`superclass`.
Returns a class instance and checks types.
Args:
ins_or_class_or_name: Can be of 3 types:
- A class to instantiate.
- A string of the name or full path to a class to instantiate.
- The class instance to check types.
kwargs (dict): Keyword arguments for the class constructor. Ignored
if `ins_or_class_or_name` is a class instance.
module_paths (list, optional): Paths to candidate modules to
search for the class. This is used if the class cannot be
located solely based on :attr:`class_name`. The first module
in the list that contains the class is used.
classtype (optional): A (list of) class of which the instance must
be an instantiation.
Raises:
ValueError: If class is not found based on :attr:`class_name` and
:attr:`module_paths`.
ValueError: If :attr:`kwargs` contains arguments that are invalid
for the class construction.
TypeError: If the instance is not an instantiation of
:attr:`classtype`.
Returns a class instance and checks types.
Only those keyword arguments in :attr:`kwargs` that are included in the
class construction method are used.
Args:
ins_or_class_or_name: Can be of 3 types:
- A class to instantiate.
- A string of the name or module path to a class to instantiate.
- The class instance to check types.
kwargs (dict): Keyword arguments for the class constructor.
module_paths (list, optional): Paths to candidate modules to
search for the class. This is used if the class cannot be
located solely based on :attr:`class_name`. The first module
in the list that contains the class is used.
classtype (optional): A (list of) classes of which the instance must
be an instantiation.
Raises:
ValueError: If class is not found based on :attr:`class_name` and
:attr:`module_paths`.
ValueError: If :attr:`kwargs` contains arguments that are invalid
for the class construction.
TypeError: If the instance is not an instantiation of
:attr:`classtype`.
Returns :attr:`str_` if it is not `None` or empty, otherwise returns
:attr:`default_str`.
Args:
str_: A string.
default_str: A string.
Returns:
Either :attr:`str_` or :attr:`default_str`.
Fetches a sub dict of :attr:`src_dict` with the keys in
:attr:`tgt_dict_or_keys`.
Args:
src_dict: A dict or instance of :class:`~texar.tf.HParams`.
The source dict to fetch values from.
tgt_dict_or_keys: A dict, instance of :class:`~texar.tf.HParams`,
or a list (or a dict_keys) of keys to be included in the output
dict.
Returns:
A new dict that is a subdict of :attr:`src_dict`.
Looks up :attr:`keys` in the dict, returns the corresponding values.
The :attr:`default` is used for keys not present in the dict.
Args:
dict_ (dict): A dictionary for lookup.
keys: A numpy array or a (possibly nested) list of keys.
default (optional): Value to be returned when a key is not in
:attr:`dict_`. Error is raised if :attr:`default` is not given and
key is not in the dict.
Returns:
A numpy array of values with the same structure as :attr:`keys`.
Raises:
TypeError: If key is not in :attr:`dict_` and :attr:`default` is `None`.
Recursively patch :attr:`tgt_dict` by adding items from :attr:`src_dict`
that do not exist in :attr:`tgt_dict`.
If respective items in :attr:`src_dict` and :attr:`tgt_dict` are both
`dict`, the :attr:`tgt_dict` item is patched recursively.
Args:
tgt_dict (dict): Target dictionary to patch.
src_dict (dict): Source dictionary.
Return:
dict: The new :attr:`tgt_dict` that is patched.
Removes keys from a dict and returns their values.
Args:
dict_ (dict): A dictionary from which items are removed.
pop_keys: A key or a list of keys to remove and return respective
values or :attr:`default`.
default (optional): Value to be returned when a key is not in
:attr:`dict_`. The default value is `None`.
Returns:
A `dict` of the items removed from :attr:`dict_`.
Flattens a nested dictionary. Namedtuples within the dictionary are
converted to dicts.
Adapted from:
https://github.com/google/seq2seq/blob/master/seq2seq/models/model_base.py
Args:
dict_ (dict): The dictionary to flatten.
parent_key (str): A prefix to prepend to each key.
sep (str): Separator that intervenes between parent and child keys.
E.g., if `sep` == '.', then `{ "a": { "b": 3 } }` is converted
into `{ "a.b": 3 }`.
Returns:
A new flattened `dict`.
Gets the arguments of a function.
Args:
fn (callable): The function to inspect.
Returns:
list: A list of argument names (str) of the function.
Returns the class based on class name.
Args:
class_name (str): Name or full path to the class.
module_paths (list): Paths to candidate modules to search for the
class. This is used if the class cannot be located solely based on
`class_name`. The first module in the list that contains the class
is used.
Returns:
The target class.
Raises:
ValueError: If class is not found based on :attr:`class_name` and
:attr:`module_paths`.
Gets the arguments and respective default values of a function.
Only arguments with default values are included in the output dictionary.
Args:
fn (callable): The function to inspect.
Returns:
dict: A dictionary that maps argument names (str) to their default
values. The dictionary is empty if no arguments have default values.
Returns the function of specified name and module.
Args:
fn_or_name (str or callable): Name or full path to a function, or the
function itself.
module_paths (list, optional): A list of paths to candidate modules to
search for the function. This is used only when the function
cannot be located solely based on :attr:`fn_or_name`. The first
module in the list that contains the function is used.
Returns:
A function.
Creates a class instance.
Args:
class_or_name: A class, or its name or full path to a class to
instantiate.
kwargs (dict): Keyword arguments for the class constructor.
module_paths (list, optional): Paths to candidate modules to
search for the class. This is used if the class cannot be
located solely based on :attr:`class_name`. The first module
in the list that contains the class is used.
Returns:
A class instance.
Raises:
ValueError: If class is not found based on :attr:`class_or_name` and
:attr:`module_paths`.
ValueError: If :attr:`kwargs` contains arguments that are invalid
for the class construction.
Makes a dict of keyword arguments with the following structure:
`kwargs_ = {'hparams': dict(hparams), **kwargs}`.
This is typically used for constructing a module which takes a set of
arguments as well as a argument named `hparams`.
Args:
kwargs (dict): A dict of keyword arguments. Can be `None`.
hparams: A dict or an instance of :class:`~texar.tf.HParams` Can be `None`.
Returns:
A `dict` that contains the keyword arguments in :attr:`kwargs`, and
an additional keyword argument named `hparams`.
Creates a class instance.
Only those keyword arguments in :attr:`kwargs` that are included in the
class construction method are used.
Args:
class_name (str): A class or its name or module path.
kwargs (dict): A dictionary of arguments for the class constructor. It
may include invalid arguments which will be ignored.
module_paths (list of str): A list of paths to candidate modules to
search for the class. This is used if the class cannot be located
solely based on :attr:`class_name`. The first module in the list
that contains the class is used.
Returns:
A class instance.
Raises:
ValueError: If class is not found based on :attr:`class_name` and
:attr:`module_paths`.
Transforms `int` indexes to strings by mapping ids to tokens,
concatenating tokens into sentences, and stripping special tokens, etc.
Args:
ids: An n-D numpy array or (possibly nested) list of `int` indexes.
vocab: An instance of :class:`~texar.tf.data.Vocab`.
join (bool): Whether to concat along the last dimension of the
the tokens into a string separated with a space character.
strip_pad (str): The PAD token to strip from the strings (i.e., remove
the leading and trailing PAD tokens of the strings). Default
is '<PAD>' as defined in
:class:`~texar.tf.data.SpecialTokens`.PAD.
Set to `None` or `False` to disable the stripping.
strip_bos (str): The BOS token to strip from the strings (i.e., remove
the leading BOS tokens of the strings).
Default is '<BOS>' as defined in
:class:`~texar.tf.data.SpecialTokens`.BOS.
Set to `None` or `False` to disable the stripping.
strip_eos (str): The EOS token to strip from the strings (i.e., remove
the EOS tokens and all subsequent tokens of the strings).
Default is '<EOS>' as defined in
:class:`~texar.tf.data.SpecialTokens`.EOS.
Set to `None` or `False` to disable the stripping.
Returns:
If :attr:`join` is True, returns a `(n-1)`-D numpy array (or list) of
concatenated strings. If :attr:`join` is False, returns an `n`-D numpy
array (or list) of str tokens.
Example:
.. code-block:: python
text_ids = [[1, 9, 6, 2, 0, 0], [1, 28, 7, 8, 2, 0]]
text = map_ids_to_strs(text_ids, data.vocab)
# text == ['a sentence', 'parsed from ids']
text = map_ids_to_strs(
text_ids, data.vocab, join=False,
strip_pad=None, strip_bos=None, strip_eos=None)
# text == [['<BOS>', 'a', 'sentence', '<EOS>', '<PAD>', '<PAD>'],
# ['<BOS>', 'parsed', 'from', 'ids', '<EOS>', '<PAD>']]
Concats :attr:`tokens` along the last dimension with intervening
occurrences of :attr:`sep`.
Args:
tokens: An `n`-D numpy array or (possibly nested) list of `str`.
sep (str): The string intervening between the tokens.
compat (bool): Whether to convert tokens into `unicode` (Python 2)
or `str` (Python 3).
Returns:
An `(n-1)`-D numpy array (or list) of `str`.
Use a tensor in forward pass while backpropagating gradient to another.
Args:
fw_tensor: A tensor to be used in the forward pass.
bw_tensor: A tensor to which gradient is backpropagated. Must have the
same shape and type with :attr:`fw_tensor`.
Returns:
A tensor of the same shape and value with :attr:`fw_tensor` but will
direct gradient to bw_tensor.
Remove all leading BOS tokens.
Note that besides :attr:`bos_token`, all leading and trailing whitespace
characters are also removed.
If :attr:`is_token_list` is False, then the function assumes tokens in
:attr:`str_` are separated with whitespace character.
Args:
str_: A `str`, or an `n`-D numpy array or (possibly nested)
list of `str`.
bos_token (str): The BOS token. Default is '<BOS>' as defined in
:class:`~texar.tf.data.SpecialTokens`.BOS
is_token_list (bool): Whether each sentence in :attr:`str_` is a list
of tokens. If False, each sentence in :attr:`str_` is assumed to
contain tokens separated with space character.
compat (bool): Whether to convert tokens into `unicode` (Python 2)
or `str` (Python 3).
Returns:
Strings of the same structure/shape as :attr:`str_`.
Remove the EOS token and all subsequent tokens.
If :attr:`is_token_list` is False, then the function assumes tokens in
:attr:`str_` are separated with whitespace character.
Args:
str_: A `str`, or an `n`-D numpy array or (possibly nested)
list of `str`.
eos_token (str): The EOS token. Default is '<EOS>' as defined in
:class:`~texar.tf.data.SpecialTokens`.EOS
is_token_list (bool): Whether each sentence in :attr:`str_` is a list
of tokens. If False, each sentence in :attr:`str_` is assumed to
contain tokens separated with space character.
compat (bool): Whether to convert tokens into `unicode` (Python 2)
or `str` (Python 3).
Returns:
Strings of the same structure/shape as :attr:`str_`.
Removes special tokens in strings, including:
- Removes EOS and all subsequent tokens
- Removes leading and and trailing PAD tokens
- Removes leading BOS tokens
Note that besides the special tokens, all leading and trailing whitespace
characters are also removed.
This is a joint function of :func:`strip_eos`, :func:`strip_pad`, and
:func:`strip_bos`
Args:
str_: A `str`, or an `n`-D numpy array or (possibly nested)
list of `str`.
strip_pad (str): The PAD token to strip from the strings (i.e., remove
the leading and trailing PAD tokens of the strings). Default
is '<PAD>' as defined in
:class:`~texar.tf.data.SpecialTokens`.PAD.
Set to `None` or `False` to disable the stripping.
strip_bos (str): The BOS token to strip from the strings (i.e., remove
the leading BOS tokens of the strings).
Default is '<BOS>' as defined in
:class:`~texar.tf.data.SpecialTokens`.BOS.
Set to `None` or `False` to disable the stripping.
strip_eos (str): The EOS token to strip from the strings (i.e., remove
the EOS tokens and all subsequent tokens of the strings).
Default is '<EOS>' as defined in
:class:`~texar.tf.data.SpecialTokens`.EOS.
Set to `None` or `False` to disable the stripping.
is_token_list (bool): Whether each sentence in :attr:`str_` is a list
of tokens. If False, each sentence in :attr:`str_` is assumed to
contain tokens separated with space character.
compat (bool): Whether to convert tokens into `unicode` (Python 2)
or `str` (Python 3).
Returns:
Strings of the same shape of :attr:`str_` with special tokens stripped.
Returns a copy of strings with leading and trailing tokens removed.
Note that besides :attr:`token`, all leading and trailing whitespace
characters are also removed.
If :attr:`is_token_list` is False, then the function assumes tokens in
:attr:`str_` are separated with whitespace character.
Args:
str_: A `str`, or an `n`-D numpy array or (possibly nested)
list of `str`.
token (str): The token to strip, e.g., the '<PAD>' token defined in
:class:`~texar.tf.data.SpecialTokens`.PAD
is_token_list (bool): Whether each sentence in :attr:`str_` is a list
of tokens. If False, each sentence in :attr:`str_` is assumed to
contain tokens separated with space character.
compat (bool): Whether to convert tokens into `unicode` (Python 2)
or `str` (Python 3).
Returns:
The stripped strings of the same structure/shape as :attr:`str_`.
Example:
.. code-block:: python
str_ = '<PAD> a sentence <PAD> <PAD> '
str_stripped = strip_token(str_, '<PAD>')
# str_stripped == 'a sentence'
str_ = ['<PAD>', 'a', 'sentence', '<PAD>', '<PAD>', '', '']
str_stripped = strip_token(str_, '<PAD>', is_token_list=True)
# str_stripped == 'a sentence'
Truncates a sequence pair in place to the maximum length.
This is a simple heuristic which will always truncate the longer sequence
one token at a time. This makes more sense than truncating an equal
percent of tokens from each, since if one sequence is very short then
each token that's truncated likely contains more information than a
longer sequence.
Example:
.. code-block:: python
tokens_a = [1, 2, 3, 4, 5]
tokens_b = [6, 7]
truncate_seq_pair(tokens_a, tokens_b, 5)
tokens_a # [1, 2, 3]
tokens_b # [6, 7]
Args:
tokens_a: A list of tokens or token ids.
tokens_b: A list of tokens or token ids.
max_length: maximum sequence length.
Uniquifies :attr:`str_` if :attr:`str_` is included in :attr:`str_set`.
This is done by appending a number to :attr:`str_`. Returns
:attr:`str_` directly if it is not included in :attr:`str_set`.
Args:
str_ (string): A string to uniquify.
str_set (set, dict, or list): A collection of strings. The returned
string is guaranteed to be different from the elements in the
collection.
Returns:
The uniquified string. Returns :attr:`str_` directly if it is
already unique.
Example:
.. code-block:: python
print(uniquify_str('name', ['name', 'name_1']))
# 'name_2'
Miscellaneous Utility functions.
Copyright 2018 The Texar 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. pylint: disable=invalid-name, no-member, no-name-in-module, protected-access pylint: disable=redefined-outer-name, too-many-arguments pylint: disable=anomalous-backslash-in-string Some modules cannot be imported directly, e.g., `import tensorflow.train` fails. Such modules are treated in a special way in utils like `get_class` as below. _unimportable_modules = { 'tensorflow.train', 'tensorflow.keras.regularizers' } TODO(zhiting): complete this Empty args can be because `fn` is decorated. Use `funcsigs.signature` to re-do the inspect if module_path in _unimportable_modules: Special treatment for unimportable modules by directly accessing the class else: module = importlib.import_module(module_path) if class_name in dir(module): class_ = getattr(module, class_name) break Locate the class Check validity of arguments Locate the class Select valid arguments if module_path in _unimportable_modules: module = importlib.import_module(module_path) if fn_name in dir(module): fn = getattr(module, fn_name) break Select valid arguments | 19,876 | en | 0.693667 |
#!/usr/bin/env python3
from helper import lemniscate, saveImage, getImageName
from math import pi
from random import uniform
IMAGE_WIDTH_IN = 5
IMAGE_HEIGHT_IN = 2
DPI = 400
def makeImage(height, width, imageName):
k = 1.9
numSamples = 100000000
imageWidth = width
imageHeight = height
data = [0] * imageHeight * imageWidth
cx = imageWidth/2
cy = imageHeight/2
radius = imageWidth/2 * .95
for i in range(numSamples):
# need to increase range for complete curve
theta = uniform(0, 10 * pi)
pA_x, pA_y = lemniscate(theta, radius, cx, cy)
pB_x, pB_y = lemniscate(k * theta, radius, cx, cy)
# pick a random point on the line segment [pA, pB]
r = uniform(0, 1)
pC_x = (1 - r) * pA_x + r * pB_x
pC_y = (1 - r) * pA_y + r * pB_y
i = int(pC_x + .5)
j = int(pC_y + .5)
data[j * imageWidth + i] += 1
saveImage(data, imageName, imageWidth, imageHeight,
bg=[255, 255, 255], fg=[221, 100, 0], alphaMultiplier=18)
if __name__ == "__main__":
imageName = getImageName(__file__)
width = IMAGE_WIDTH_IN * DPI
height = IMAGE_HEIGHT_IN * DPI
makeImage(height, width, imageName)
| presentation/presentation_images/genImage_07.py | 1,282 | !/usr/bin/env python3 need to increase range for complete curve pick a random point on the line segment [pA, pB] | 112 | en | 0.764042 |
import magma as m
from magma import *
def test_pair():
# types
A2 = Tuple[Bit, Bit]
print(A2)
assert isinstance(A2, TupleMeta)
print(str(A2))
assert A2 == A2
B2 = Tuple[In(Bit), In(Bit)]
assert isinstance(B2, TupleMeta)
assert B2 == B2
C2 = Tuple[Out(Bit), Out(Bit)]
assert isinstance(C2, TupleMeta)
#assert str(C2) == 'Tuple(x=Out(Bit),y=Out(Bit))'
assert C2 == C2
assert issubclass(m.In(m.Tuple[Bit, Bit]), m.In(m.Tuple[Bit, Bit]))
assert isinstance(m.In(m.Tuple[Bit, Bit])(), m.In(m.Tuple[Bit, Bit]))
assert issubclass(m.In(m.Tuple[Bit, Bit]), m.Tuple[Bit, Bit])
assert isinstance(m.In(m.Tuple[Bit, Bit])(), m.Tuple[Bit, Bit])
assert not issubclass(m.In(m.Tuple[Bit, Bit]), m.Out(m.Tuple[Bit, Bit]))
assert not isinstance(m.In(m.Tuple[Bit, Bit])(), m.Out(m.Tuple[Bit, Bit]))
assert issubclass(m.Out(m.Tuple[Bit, Bit]), m.Out(m.Tuple[Bit, Bit]))
assert isinstance(m.Out(m.Tuple[Bit, Bit])(), m.Out(m.Tuple[Bit, Bit]))
assert issubclass(m.Out(m.Tuple[Bit, Bit]), m.Tuple[Bit, Bit])
assert isinstance(m.Out(m.Tuple[Bit, Bit])(), m.Tuple[Bit, Bit])
assert not issubclass(m.Out(m.Tuple[Bit, Bit]), m.In(m.Tuple[Bit, Bit]))
assert not isinstance(m.Out(m.Tuple[Bit, Bit])(), m.In(m.Tuple[Bit, Bit]))
def test_dict():
# types
class A2(Product, cache=True):
x = Bit
y = Bit
print(A2)
assert isinstance(A2, ProductMeta)
print(str(A2))
assert issubclass(In(A2), A2)
assert issubclass(Out(A2), A2)
assert issubclass(Flip(A2), A2)
assert not issubclass(In(A2), Out(A2))
assert not issubclass(Out(A2), In(A2))
assert issubclass(Flip(In(A2)), Out(A2))
assert issubclass(Flip(Out(A2)), In(A2))
assert issubclass(Out(In(A2)), Out(A2))
assert issubclass(In(Out(A2)), In(A2))
assert not issubclass(Out(In(A2)), In(Out(A2)))
assert not issubclass(In(Out(A2)), Out(In(A2)))
assert not issubclass(Flip(In(A2)), Flip(Out(A2)))
assert not issubclass(Flip(Out(A2)), Flip(In(A2)))
assert isinstance(In(A2)(), A2)
assert isinstance(Out(A2)(), A2)
assert isinstance(Flip(A2)(), A2)
assert not isinstance(In(A2)(), Out(A2))
assert not isinstance(Out(A2)(), In(A2))
assert isinstance(Flip(In(A2))(), Out(A2))
assert isinstance(Flip(Out(A2))(), In(A2))
assert isinstance(Out(In(A2))(), Out(A2))
assert isinstance(In(Out(A2))(), In(A2))
assert not isinstance(Out(In(A2))(), In(Out(A2)))
assert not isinstance(In(Out(A2))(), Out(In(A2)))
assert not isinstance(Flip(In(A2))(), Flip(Out(A2)))
assert not isinstance(Flip(Out(A2))(), Flip(In(A2)))
#assert str(A2) == 'Tuple(x=Bit,y=Bit)'
assert A2 == A2
class B2(Product, cache=True):
x = In(Bit)
y = In(Bit)
assert isinstance(B2, ProductMeta)
#assert str(B2) == 'Tuple(x=In(Bit),y=In(Bit))'
assert B2 == B2
class C2(Product, cache=True):
x = Out(Bit)
y = Out(Bit)
assert isinstance(C2, ProductMeta)
#assert str(C2) == 'Tuple(x=Out(Bit),y=Out(Bit))'
assert C2 == C2
assert A2 == B2
assert A2 == C2
assert B2 == C2
assert A2 is not B2
assert A2 is not C2
assert B2 is not C2
def test_flip():
class Product2(Product):
x = In(Bit)
y = Out(Bit)
print(Product2)
print(Flip(Product2))
Tin = In(Product2)
Tout = Out(Product2)
print(Tin)
print(Tout)
assert Tin == Product2
assert Tout == Product2
assert Tin == Tout
assert Tin is not Product2
assert Tout is not Product2
assert Tin is not Tout
T = In(Tout)
assert T == Tin
#T = Flip(Tout)
#assert T == Tin
# print(T)
T = Out(Tin)
assert T == Tout
#T = Flip(Tin)
#assert T == Tout
# print(T)
def test_wire():
class Product2(Product):
x = Bit
y = Bit
t0 = Product2(name='t0')
t1 = Product2(name='t1')
wire(t0, t1)
assert t0.wired()
assert t1.wired()
assert t1.value() is t0
assert t0.value() is t1
assert t0.driving() == dict(x=[t1.x], y=[t1.y])
b0 = t0.x
b1 = t1.x
assert b0 is b1._wire.driver.bit
assert b1 is b0._wire.driving()[0]
assert b1.value() is b0
def test_val():
class A2(Product):
x = Bit
y = Bit
# constructor
a = A2(name='a')
print('created A2')
assert isinstance(a, Product)
assert str(a) == 'a'
# selectors
print('a["x"]')
b = a['x']
assert isinstance(b, Bit)
assert str(b) == 'a.x'
print('a.x')
b = a.x
assert isinstance(b, Bit)
assert str(b) == 'a.x'
def test_nested():
# Test for https://github.com/phanrahan/magma/issues/445
def hierIO():
class dictIO(Product):
baseIO = make_baseIO()
ctr = m.In(m.Bit)
return dictIO
def DefineCtrModule():
class ctrModule(m.Circuit):
name = "ctr_module"
io = m.IO(ctr=m.In(m.Bit))
return ctrModule
def make_baseIO():
class dictIO(Product):
in0 = m.In(m.Bit),
out0 = m.Out(m.Bit)
return dictIO
def DefineBaseModule():
class baseModule(m.Circuit):
name = "base_module"
io = m.IO(baseIO=make_baseIO())
return baseModule
def DefineHier():
class HierModule(m.Circuit):
name = "hier_module"
io = m.IO(hier=hierIO())
baseM = DefineBaseModule()()
ctrM = DefineCtrModule()()
m.wire(baseM.baseIO, io.hier.baseIO)
m.wire(ctrM.ctr, io.hier.ctr)
return HierModule
baseMH = DefineHier()
m.compile("build/baseMH", baseMH, output="coreir-verilog")
def test_tuple_nested_tuple_value():
def IFC0(params):
return m.Product.from_fields("IFC0", {
"port0": m.In(m.Bits[params['param0']]),
"port1": m.In(m.Bits[params['param0']]),
"port2": m.In(m.Array[params['param0'], m.Bits[2]]),
"port3": m.In(m.Bits[params['param0']]),
"port4": m.In(m.Bit),
"port5": m.In(m.Bit),
"port7": m.In(m.Bit),
"port8": m.In(m.Bit),
"port9": m.In(m.Bit),
"port10": m.In(m.Bits[m.bitutils.clog2(params['param0'])]),
})
def IFC1(params):
dictOut = {"port4": m.Out(m.Bit)}
return m.Product.from_fields("IFC1", dictOut)
def DefineMyCircuit(params):
class MyCircuit(m.Circuit):
io = m.IO(IFC0=IFC0(params).flip())
return MyCircuit
def DefineTop(params):
class Top(m.Circuit):
io = m.IO(IFC1=IFC1(params))
m.wire(io.IFC1.port4, DefineMyCircuit(params)().IFC0.port4)
return Top
m.compile("top", DefineTop({'param0': 5}))
def test_flat_length():
a = m.Product.from_fields("anon", dict(x=m.Bits[5], y=m.Bits[3], z=m.Bit))
assert a.flat_length() == 9
def test_anon_product():
product = m.Product.from_fields("anon", dict(x=m.Bits[5], y=m.Bits[3], z=m.Bit))
assert isinstance(product, AnonymousProductMeta)
assert isinstance(product, ProductMeta)
anon_product = m.AnonProduct[dict(x=m.Bits[5], y=m.Bits[3], z=m.Bit)]
assert isinstance(anon_product, AnonymousProductMeta)
assert not isinstance(anon_product, ProductMeta)
assert anon_product.flat_length() == product.flat_length()
assert anon_product.x == product.x
assert anon_product.y == product.y
assert anon_product.z == product.z
assert anon_product == product
assert not anon_product is product
| tests/test_type/test_tuple.py | 7,648 | typesassert str(C2) == 'Tuple(x=Out(Bit),y=Out(Bit))' typesassert str(A2) == 'Tuple(x=Bit,y=Bit)'assert str(B2) == 'Tuple(x=In(Bit),y=In(Bit))'assert str(C2) == 'Tuple(x=Out(Bit),y=Out(Bit))'T = Flip(Tout)assert T == Tin print(T)T = Flip(Tin)assert T == Tout print(T) constructor selectors Test for https://github.com/phanrahan/magma/issues/445 | 344 | en | 0.30658 |
# -*- coding: utf-8 -*-
{
'name': 'Payment - Account',
'category': 'Accounting/Accounting',
'summary': 'Account and Payment Link and Portal',
'version': '1.0',
'description': """Link Account and Payment and add Portal Payment
Provide tools for account-related payment as well as portal options to
enable payment.
* UPDATE ME
""",
'depends': ['payment'],
'data': [
'views/account_portal_templates.xml',
],
'installable': True,
'auto_install': False,
}
| odoo-13.0 - Copy/addons/account_payment/__manifest__.py | 503 | -*- coding: utf-8 -*- | 21 | en | 0.767281 |
ximport tulip
def reader(s):
res = yield from s.read(1)
while res:
print ('got data:', res)
res = yield from s.read(1)
def main(stream):
stream2 = tulip.StreamReader()
# start separate task
t = tulip.async(reader(stream2))
while 1:
data = yield from stream.read(1)
print ('received data:', data)
if data == b'0':
break
stream2.feed_data(data)
stream2.feed_eof()
#yield from t
if __name__ == '__main__':
loop = tulip.get_event_loop()
stream = tulip.StreamReader()
stream.feed_data(b'1234567890')
try:
loop.run_until_complete(main(stream))
except KeyboardInterrupt:
pass
| lib/asyncio-0.4.1/sched_test.py | 747 | start separate taskyield from t | 31 | en | 0.726265 |
import cocotb
from cocotb.clock import Clock
from cocotb.triggers import ClockCycles, ReadWrite, NextTimeStep, RisingEdge, FallingEdge
from cocotb.binary import BinaryValue
import numpy as np
from matplotlib import pyplot as plt
from scipy.signal import butter, filtfilt
from fixedpoint import FixedPoint
@cocotb.test()
async def test_peak_detect(dut):
num_samples_per_epoch = 15*50
num_epochs = 10
num_samples = num_samples_per_epoch * num_epochs
data = np.loadtxt('46343_acceleration.txt', delimiter=' ')
#count_feature = np.loadtxt('46343_cleaned_counts.out', delimiter=' ')
fs = 50
time = np.arange(np.amin(data[:, 0]), np.amax(data[:, 0]), 1.0 / fs)
z_accel = np.interp(time, data[:, 0], data[:, 3])
# cf_low = 3
# cf_hi = 11
# order = 5
# w1 = cf_low / (fs / 2)
# w2 = cf_hi / (fs / 2)
# pass_band = [w1, w2]
# b, a = butter(order, pass_band, 'bandpass')
# z_filt = filtfilt(b, a, z_accel)
start_offset_sec = 120
offset = fs * start_offset_sec
z_accel = z_accel[offset:offset+num_samples]
print(f"Number of samples to input {z_accel.shape[0]}")
#count_feature = count_feature[::num_epochs]
clk = dut.clk
dut.i_z_accel <= 0
dut.i_valid <= 0
cocotb.fork(Clock(clk, 25, units="ns").start())
# Reset logic
await NextTimeStep()
dut.reset <= 1
await ClockCycles(clk, 1)
await ReadWrite()
dut.reset <= 0
await ClockCycles(clk, 1)
await ReadWrite()
for i, z in enumerate(z_accel):
dut.i_z_accel <= BinaryValue(str(FixedPoint(float(z/5), 1, 7)))
dut.i_valid <= 1
await ClockCycles(clk, 1)
dut.i_valid <= 0
await ClockCycles(clk, 10)
dut.i_valid <= 0
await ClockCycles(clk, 100)
| fpga/test/featurize/actigraphy_counts/actigraphy_counts_tb.py | 1,770 | count_feature = np.loadtxt('46343_cleaned_counts.out', delimiter=' ') cf_low = 3 cf_hi = 11 order = 5 w1 = cf_low / (fs / 2) w2 = cf_hi / (fs / 2) pass_band = [w1, w2] b, a = butter(order, pass_band, 'bandpass') z_filt = filtfilt(b, a, z_accel)count_feature = count_feature[::num_epochs] Reset logic | 299 | en | 0.762771 |
import getpass
import logging
import os
from urlparse import urlparse
from django.conf import settings
from django.core.mail import EmailMultiAlternatives
from django.template.loader import get_template
from readthedocs.builds.constants import LATEST
from readthedocs.builds.constants import LATEST_VERBOSE_NAME
from readthedocs.builds.models import Build
log = logging.getLogger(__name__)
SYNC_USER = getattr(settings, 'SYNC_USER', getpass.getuser())
def run_on_app_servers(command):
"""
A helper to copy a single file across app servers
"""
log.info("Running %s on app servers" % command)
ret_val = 0
if getattr(settings, "MULTIPLE_APP_SERVERS", None):
for server in settings.MULTIPLE_APP_SERVERS:
ret = os.system("ssh %s@%s %s" % (SYNC_USER, server, command))
if ret != 0:
ret_val = ret
return ret_val
else:
ret = os.system(command)
return ret
def clean_url(url):
parsed = urlparse(url)
if parsed.scheme:
scheme, netloc = parsed.scheme, parsed.netloc
elif parsed.netloc:
scheme, netloc = "http", parsed.netloc
else:
scheme, netloc = "http", parsed.path
return netloc
def cname_to_slug(host):
from dns import resolver
answer = [ans for ans in resolver.query(host, 'CNAME')][0]
domain = answer.target.to_unicode()
slug = domain.split('.')[0]
return slug
def trigger_build(project, version=None, record=True, force=False, basic=False):
"""
An API to wrap the triggering of a build.
"""
# Avoid circular import
from readthedocs.projects.tasks import update_docs
if project.skip:
return None
if not version:
version = project.versions.get(slug=LATEST)
if record:
build = Build.objects.create(
project=project,
version=version,
type='html',
state='triggered',
success=True,
)
update_docs.delay(pk=project.pk, version_pk=version.pk, record=record,
force=force, basic=basic, build_pk=build.pk)
else:
build = None
update_docs.delay(pk=project.pk, version_pk=version.pk, record=record,
force=force, basic=basic)
return build
def send_email(recipient, subject, template, template_html, context=None,
request=None):
'''
Send multipart email
recipient
Email recipient address
subject
Email subject header
template
Plain text template to send
template_html
HTML template to send as new message part
context
A dictionary to pass into the template calls
request
Request object for determining absolute URL
'''
if request:
scheme = 'https' if request.is_secure() else 'http'
context['uri'] = '{scheme}://{host}'.format(scheme=scheme,
host=request.get_host())
ctx = {}
ctx.update(context)
msg = EmailMultiAlternatives(
subject,
get_template(template).render(ctx),
settings.DEFAULT_FROM_EMAIL,
[recipient]
)
msg.attach_alternative(get_template(template_html).render(ctx), 'text/html')
msg.send()
| readthedocs/core/utils/__init__.py | 3,304 | A helper to copy a single file across app servers
Send multipart email
recipient
Email recipient address
subject
Email subject header
template
Plain text template to send
template_html
HTML template to send as new message part
context
A dictionary to pass into the template calls
request
Request object for determining absolute URL
An API to wrap the triggering of a build.
Avoid circular import | 427 | en | 0.681242 |
# -*- coding: utf-8 -*-
from music21.test.dedent import dedent
__all__ = [
'dedent',
'testDocumentation',
'testExternal',
'testPerformance',
'timeGraphs',
'testStream',
'helpers',
]
import sys
if sys.version > '3':
from music21.test import testStream
from music21.test import testDocumentation
else:
import testDocumentation # @Reimport
import testStream # @Reimport
_DOC_IGNORE_MODULE_OR_PACKAGE = True
#------------------------------------------------------------------------------
# eof
| lib/music21/test/__init__.py | 549 | -*- coding: utf-8 -*- @Reimport @Reimport------------------------------------------------------------------------------ eof | 123 | en | 0.188308 |
from typing import Union, List, Optional
from pyspark.sql.types import StructType, StructField, StringType, ArrayType, DataType
# This file is auto-generated by generate_schema so do not edit it manually
# noinspection PyPep8Naming
class MedicationKnowledge_AdministrationGuidelinesSchema:
"""
Information about a medication that is used to support knowledge.
"""
# noinspection PyDefaultArgument
@staticmethod
def get_schema(
max_nesting_depth: Optional[int] = 6,
nesting_depth: int = 0,
nesting_list: List[str] = [],
max_recursion_limit: Optional[int] = 2,
include_extension: Optional[bool] = False,
extension_fields: Optional[List[str]] = None,
extension_depth: int = 0,
max_extension_depth: Optional[int] = 2,
include_modifierExtension: Optional[bool] = False,
use_date_for: Optional[List[str]] = None,
parent_path: Optional[str] = "",
) -> Union[StructType, DataType]:
"""
Information about a medication that is used to support knowledge.
id: Unique id for the element within a resource (for internal references). This
may be any string value that does not contain spaces.
extension: May be used to represent additional information that is not part of the basic
definition of the element. To make the use of extensions safe and manageable,
there is a strict set of governance applied to the definition and use of
extensions. Though any implementer can define an extension, there is a set of
requirements that SHALL be met as part of the definition of the extension.
modifierExtension: May be used to represent additional information that is not part of the basic
definition of the element and that modifies the understanding of the element
in which it is contained and/or the understanding of the containing element's
descendants. Usually modifier elements provide negation or qualification. To
make the use of extensions safe and manageable, there is a strict set of
governance applied to the definition and use of extensions. Though any
implementer can define an extension, there is a set of requirements that SHALL
be met as part of the definition of the extension. Applications processing a
resource are required to check for modifier extensions.
Modifier extensions SHALL NOT change the meaning of any elements on Resource
or DomainResource (including cannot change the meaning of modifierExtension
itself).
dosage: Dosage for the medication for the specific guidelines.
indicationCodeableConcept: Indication for use that apply to the specific administration guidelines.
indicationReference: Indication for use that apply to the specific administration guidelines.
patientCharacteristics: Characteristics of the patient that are relevant to the administration
guidelines (for example, height, weight, gender, etc.).
"""
if extension_fields is None:
extension_fields = [
"valueBoolean",
"valueCode",
"valueDate",
"valueDateTime",
"valueDecimal",
"valueId",
"valueInteger",
"valuePositiveInt",
"valueString",
"valueTime",
"valueUnsignedInt",
"valueUri",
"valueUrl",
"valueReference",
"valueCodeableConcept",
"valueAddress",
]
from spark_fhir_schemas.r4.complex_types.extension import ExtensionSchema
from spark_fhir_schemas.r4.complex_types.medicationknowledge_dosage import (
MedicationKnowledge_DosageSchema,
)
from spark_fhir_schemas.r4.complex_types.codeableconcept import (
CodeableConceptSchema,
)
from spark_fhir_schemas.r4.complex_types.reference import ReferenceSchema
from spark_fhir_schemas.r4.complex_types.medicationknowledge_patientcharacteristics import (
MedicationKnowledge_PatientCharacteristicsSchema,
)
if (
max_recursion_limit
and nesting_list.count("MedicationKnowledge_AdministrationGuidelines")
>= max_recursion_limit
) or (max_nesting_depth and nesting_depth >= max_nesting_depth):
return StructType([StructField("id", StringType(), True)])
# add my name to recursion list for later
my_nesting_list: List[str] = nesting_list + [
"MedicationKnowledge_AdministrationGuidelines"
]
my_parent_path = (
parent_path + ".medicationknowledge_administrationguidelines"
if parent_path
else "medicationknowledge_administrationguidelines"
)
schema = StructType(
[
# Unique id for the element within a resource (for internal references). This
# may be any string value that does not contain spaces.
StructField("id", StringType(), True),
# May be used to represent additional information that is not part of the basic
# definition of the element. To make the use of extensions safe and manageable,
# there is a strict set of governance applied to the definition and use of
# extensions. Though any implementer can define an extension, there is a set of
# requirements that SHALL be met as part of the definition of the extension.
StructField(
"extension",
ArrayType(
ExtensionSchema.get_schema(
max_nesting_depth=max_nesting_depth,
nesting_depth=nesting_depth + 1,
nesting_list=my_nesting_list,
max_recursion_limit=max_recursion_limit,
include_extension=include_extension,
extension_fields=extension_fields,
extension_depth=extension_depth,
max_extension_depth=max_extension_depth,
include_modifierExtension=include_modifierExtension,
use_date_for=use_date_for,
parent_path=my_parent_path,
)
),
True,
),
# May be used to represent additional information that is not part of the basic
# definition of the element and that modifies the understanding of the element
# in which it is contained and/or the understanding of the containing element's
# descendants. Usually modifier elements provide negation or qualification. To
# make the use of extensions safe and manageable, there is a strict set of
# governance applied to the definition and use of extensions. Though any
# implementer can define an extension, there is a set of requirements that SHALL
# be met as part of the definition of the extension. Applications processing a
# resource are required to check for modifier extensions.
#
# Modifier extensions SHALL NOT change the meaning of any elements on Resource
# or DomainResource (including cannot change the meaning of modifierExtension
# itself).
StructField(
"modifierExtension",
ArrayType(
ExtensionSchema.get_schema(
max_nesting_depth=max_nesting_depth,
nesting_depth=nesting_depth + 1,
nesting_list=my_nesting_list,
max_recursion_limit=max_recursion_limit,
include_extension=include_extension,
extension_fields=extension_fields,
extension_depth=extension_depth,
max_extension_depth=max_extension_depth,
include_modifierExtension=include_modifierExtension,
use_date_for=use_date_for,
parent_path=my_parent_path,
)
),
True,
),
# Dosage for the medication for the specific guidelines.
StructField(
"dosage",
ArrayType(
MedicationKnowledge_DosageSchema.get_schema(
max_nesting_depth=max_nesting_depth,
nesting_depth=nesting_depth + 1,
nesting_list=my_nesting_list,
max_recursion_limit=max_recursion_limit,
include_extension=include_extension,
extension_fields=extension_fields,
extension_depth=extension_depth,
max_extension_depth=max_extension_depth,
include_modifierExtension=include_modifierExtension,
use_date_for=use_date_for,
parent_path=my_parent_path,
)
),
True,
),
# Indication for use that apply to the specific administration guidelines.
StructField(
"indicationCodeableConcept",
CodeableConceptSchema.get_schema(
max_nesting_depth=max_nesting_depth,
nesting_depth=nesting_depth + 1,
nesting_list=my_nesting_list,
max_recursion_limit=max_recursion_limit,
include_extension=include_extension,
extension_fields=extension_fields,
extension_depth=extension_depth + 1,
max_extension_depth=max_extension_depth,
include_modifierExtension=include_modifierExtension,
use_date_for=use_date_for,
parent_path=my_parent_path,
),
True,
),
# Indication for use that apply to the specific administration guidelines.
StructField(
"indicationReference",
ReferenceSchema.get_schema(
max_nesting_depth=max_nesting_depth,
nesting_depth=nesting_depth + 1,
nesting_list=my_nesting_list,
max_recursion_limit=max_recursion_limit,
include_extension=include_extension,
extension_fields=extension_fields,
extension_depth=extension_depth + 1,
max_extension_depth=max_extension_depth,
include_modifierExtension=include_modifierExtension,
use_date_for=use_date_for,
parent_path=my_parent_path,
),
True,
),
# Characteristics of the patient that are relevant to the administration
# guidelines (for example, height, weight, gender, etc.).
StructField(
"patientCharacteristics",
ArrayType(
MedicationKnowledge_PatientCharacteristicsSchema.get_schema(
max_nesting_depth=max_nesting_depth,
nesting_depth=nesting_depth + 1,
nesting_list=my_nesting_list,
max_recursion_limit=max_recursion_limit,
include_extension=include_extension,
extension_fields=extension_fields,
extension_depth=extension_depth,
max_extension_depth=max_extension_depth,
include_modifierExtension=include_modifierExtension,
use_date_for=use_date_for,
parent_path=my_parent_path,
)
),
True,
),
]
)
if not include_extension:
schema.fields = [
c
if c.name != "extension"
else StructField("extension", StringType(), True)
for c in schema.fields
]
if not include_modifierExtension:
schema.fields = [
c
if c.name != "modifierExtension"
else StructField("modifierExtension", StringType(), True)
for c in schema.fields
]
return schema
| spark_fhir_schemas/r4/complex_types/medicationknowledge_administrationguidelines.py | 13,340 | Information about a medication that is used to support knowledge.
Information about a medication that is used to support knowledge.
id: Unique id for the element within a resource (for internal references). This
may be any string value that does not contain spaces.
extension: May be used to represent additional information that is not part of the basic
definition of the element. To make the use of extensions safe and manageable,
there is a strict set of governance applied to the definition and use of
extensions. Though any implementer can define an extension, there is a set of
requirements that SHALL be met as part of the definition of the extension.
modifierExtension: May be used to represent additional information that is not part of the basic
definition of the element and that modifies the understanding of the element
in which it is contained and/or the understanding of the containing element's
descendants. Usually modifier elements provide negation or qualification. To
make the use of extensions safe and manageable, there is a strict set of
governance applied to the definition and use of extensions. Though any
implementer can define an extension, there is a set of requirements that SHALL
be met as part of the definition of the extension. Applications processing a
resource are required to check for modifier extensions.
Modifier extensions SHALL NOT change the meaning of any elements on Resource
or DomainResource (including cannot change the meaning of modifierExtension
itself).
dosage: Dosage for the medication for the specific guidelines.
indicationCodeableConcept: Indication for use that apply to the specific administration guidelines.
indicationReference: Indication for use that apply to the specific administration guidelines.
patientCharacteristics: Characteristics of the patient that are relevant to the administration
guidelines (for example, height, weight, gender, etc.).
This file is auto-generated by generate_schema so do not edit it manually noinspection PyPep8Naming noinspection PyDefaultArgument add my name to recursion list for later Unique id for the element within a resource (for internal references). This may be any string value that does not contain spaces. May be used to represent additional information that is not part of the basic definition of the element. To make the use of extensions safe and manageable, there is a strict set of governance applied to the definition and use of extensions. Though any implementer can define an extension, there is a set of requirements that SHALL be met as part of the definition of the extension. May be used to represent additional information that is not part of the basic definition of the element and that modifies the understanding of the element in which it is contained and/or the understanding of the containing element's descendants. Usually modifier elements provide negation or qualification. To make the use of extensions safe and manageable, there is a strict set of governance applied to the definition and use of extensions. Though any implementer can define an extension, there is a set of requirements that SHALL be met as part of the definition of the extension. Applications processing a resource are required to check for modifier extensions. Modifier extensions SHALL NOT change the meaning of any elements on Resource or DomainResource (including cannot change the meaning of modifierExtension itself). Dosage for the medication for the specific guidelines. Indication for use that apply to the specific administration guidelines. Indication for use that apply to the specific administration guidelines. Characteristics of the patient that are relevant to the administration guidelines (for example, height, weight, gender, etc.). | 3,833 | en | 0.879913 |
"""
LoSetup - command ``/usr/sbin/losetup -l``
==========================================
This parser reads the output of ``/usr/sbin/losetup -l`` into a list of entries.
Each entry is a dictionary of headers:
* ``NAME`` - the path name of the loop back device (strings)
* ``SIZELIMIT`` - the data end position of backing file in bytes (integer)
* ``OFFSET`` - the data start position of backing file in bytes (integer)
* ``AUTOCLEAR`` - the autoclear flag (boolean)
* ``RO`` - the read only flag (boolean)
* ``BACK-FILE`` - the path of the backing file (strings)
* ``DIO`` - the direct I/O flag (boolean)
* ``LOG-SEC`` - the logical sector size of the loop device in bytes (integer)
Sample output of ``losetup -l`` command is::
NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE DIO LOG-SEC
/dev/loop0 0 0 0 0 /root/disk.img 1 512
Examples:
>>> type(losetup)
<class 'insights.parsers.losetup.LoSetup'>
>>> len(losetup)
1
>>> losetup[0]['NAME']
'/dev/loop0'
>>> losetup[0]['RO']
False
>>> losetup[0]['DIO']
True
>>> losetup[0]['NAME']
'/dev/loop0'
>>> losetup[0]['SIZELIMIT']
0
>>> losetup[0]['OFFSET']
0
>>> losetup[0]['AUTOCLEAR']
False
>>> losetup[0]['RO']
False
>>> losetup[0]['BACK-FILE']
'/root/disk.img'
>>> losetup [0]['DIO']
True
>>> losetup[0]['LOG-SEC']
512
"""
from insights import CommandParser, parser
from insights.specs import Specs
from insights.parsers import parse_delimited_table, SkipException
@parser(Specs.losetup)
class LoSetup(CommandParser, list):
"""
Parses the output of the ``/usr/sbin/losetup -l`` command.
"""
def parse_content(self, content):
if not content:
raise SkipException("Empty output.")
self.extend(parse_delimited_table(content))
for entry in self:
for key in ['SIZELIMIT', 'OFFSET', 'LOG-SEC']:
if key in entry:
entry[key] = int(entry[key])
for key in ['AUTOCLEAR', 'RO', 'DIO']:
if key in entry:
entry[key] = True if entry[key] == '1' else False
| insights/parsers/losetup.py | 2,193 | Parses the output of the ``/usr/sbin/losetup -l`` command.
LoSetup - command ``/usr/sbin/losetup -l``
==========================================
This parser reads the output of ``/usr/sbin/losetup -l`` into a list of entries.
Each entry is a dictionary of headers:
* ``NAME`` - the path name of the loop back device (strings)
* ``SIZELIMIT`` - the data end position of backing file in bytes (integer)
* ``OFFSET`` - the data start position of backing file in bytes (integer)
* ``AUTOCLEAR`` - the autoclear flag (boolean)
* ``RO`` - the read only flag (boolean)
* ``BACK-FILE`` - the path of the backing file (strings)
* ``DIO`` - the direct I/O flag (boolean)
* ``LOG-SEC`` - the logical sector size of the loop device in bytes (integer)
Sample output of ``losetup -l`` command is::
NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE DIO LOG-SEC
/dev/loop0 0 0 0 0 /root/disk.img 1 512
Examples:
>>> type(losetup)
<class 'insights.parsers.losetup.LoSetup'>
>>> len(losetup)
1
>>> losetup[0]['NAME']
'/dev/loop0'
>>> losetup[0]['RO']
False
>>> losetup[0]['DIO']
True
>>> losetup[0]['NAME']
'/dev/loop0'
>>> losetup[0]['SIZELIMIT']
0
>>> losetup[0]['OFFSET']
0
>>> losetup[0]['AUTOCLEAR']
False
>>> losetup[0]['RO']
False
>>> losetup[0]['BACK-FILE']
'/root/disk.img'
>>> losetup [0]['DIO']
True
>>> losetup[0]['LOG-SEC']
512 | 1,474 | en | 0.553204 |
import asyncio
import concurrent
from concurrent.futures import ThreadPoolExecutor
from unittest import TestCase
import os
import cv2
from apscheduler.schedulers.background import BackgroundScheduler
import bot
from bot.providers import trainer_matches as tm
from bot.duel_links_runtime import DuelLinkRunTime
from bot.providers import Steam
from bot.common import crop_image
from bot.shared import alphabet
from bot.utils.common import default_config
class TestSteam(TestCase):
provider = None
__debug_pictures__ = False
images_needed_debug = [
"street_replay.png",
"home_page_steam.png",
os.path.join("steam", "steam_pre_battle.png"),
os.path.join("steam", "steam_back.png")
]
def setUp(self):
os.environ['LOG_CFG'] = r'D:\Sync\OneDrive\Yu-gi-oh_bot\config.ini'
scheduler = BackgroundScheduler()
dlRuntime = DuelLinkRunTime(default_config(r'D:\Sync\OneDrive\Yu-gi-oh_bot'), scheduler, False)
self.provider = Steam(scheduler, default_config(r'D:\Sync\OneDrive\Yu-gi-oh_bot'), dlRuntime, False)
self.provider.sleep_factor = 0.0
self.loop = asyncio.get_event_loop()
self.loop.set_default_executor(ThreadPoolExecutor(2))
dlRuntime._loop = self.loop
self.provider.is_process_running()
def test_battle(self):
self.fail()
def test_check_if_battle(self):
location = os.path.join(self.provider.assets, "steam", "steam_pre_battle.png")
img = cv2.imread(location)
self.assertTrue(self.provider.check_if_battle(img), "Is Battle")
def test_check_battle_is_running(self):
self.fail()
def test_click_auto_duel(self):
self.provider.click_auto_duel()
def test_compare_with_back_button(self):
img = os.path.join(self.provider.assets, "steam", "steam_back.png")
t = tm.BoundingTrainer(img, bounding_area=self.provider.predefined.main_area)
location = os.path.join(self.provider.assets, "back__.png")
# t.show_area_bounded(self.provider.predefined.main_area, img)
# t._debug = True
self.assertTrue(t.get_matches(location, 3) is True, "Expecting a back button")
# t.compare()
def test_determine_autoduel_status(self):
self.fail()
def test_ensure_resolutions_matches(self):
location = os.path.join(self.provider.assets, "steam", "download_update.png")
img = cv2.imread(location)
self.provider.ensure_resolutions_matches(img)
img = img[0:100, 0:100]
with self.assertRaises(bot.providers.BotSetupError) as context:
self.provider.ensure_resolutions_matches(img)
def test_is_process_running(self):
self.fail()
def test_key_escape(self):
self.fail()
def test_kill_process(self):
self.fail()
def test_method_name(self):
self.fail()
def test_pass_through_initial_screen(self):
self.provider.is_process_running()
test_function = lambda x: x is False
with self.assertRaises(Exception) as context:
self.provider.__generic_wait_for__('DuelLinks Landing Page', test_function,
None)
self.assertTrue('Maximum exception count' in str(context.exception))
self.provider.sleep_factor = 0.5
self.assertTrue(callable(self.provider.__is_initial_screen__))
with self.assertRaises(concurrent.futures._base.TimeoutError) as context:
self.provider.__generic_wait_for__('DuelLinks Landing Page', test_function,
self.provider.__is_initial_screen__, timeout=5)
def test_start_process(self):
self.fail()
def test_scan(self):
self.fail()
def test_scan_for_ok(self):
img = os.path.join(self.provider.assets, "steam", "steam_back.png")
t = tm.BoundingTrainer(img, bounding_area=self.provider.predefined.main_area)
location = os.path.join(self.provider.assets, "back__.png")
# t.show_area_bounded(self.provider.predefined.main_area, img)
# t._debug = True
self.assertTrue(t.get_matches(location, 3) is True, "Expecting a back button")
# t.compare()
def test_scan_for_close(self):
img = os.path.join(self.provider.assets, "steam", "steam_close.png")
area = self.provider.predefined.main_area
area['width'] = 400
t = tm.BoundingTrainer(img, bounding_area=area)
location = os.path.join(self.provider.assets, "close.png")
# t.show_area_bounded(self.provider.predefined.main_area, img)
# t._debug = True
self.assertTrue(t.get_matches(location, 3) is True, "Expecting a back button")
img = os.path.join(self.provider.assets, "steam", "steam_ok.png")
t = tm.BoundingTrainer(img, bounding_area=area)
location = os.path.join(self.provider.assets, "close.png")
t.get_matches(location, 3)
# t.show_area_bounded(self.provider.predefined.main_area, img)
# t._debug = True
self.assertTrue(t.get_matches(location, 3) is False, "Is Ok button not close")
def test_scan_for_download(self):
img = os.path.join(self.provider.assets, "steam", "download_update.png")
t = tm.BoundingTrainer(img, 500, 300, 600, 300)
location = os.path.join(self.provider.assets, "download_button.png")
# t.show_area(500, 300, 600, 300, img)
self.assertTrue(t.get_matches(location, 3) is True, "Expecting a download button")
def test_swipe_right(self):
self.fail()
# self.provider.swipe_right(0)
def test_swipe_left(self):
self.fail()
# self.provider.swipe_left(0)
def test_swipe_time(self):
self.fail()
def test_swipe(self):
self.fail()
def test_tap(self):
x, y = self.provider.predefined.yugioh_initiate_link
self.provider.tap(x, y)
def test_verify_battle(self):
location = os.path.join(self.provider.assets, "steam", "duel_variant_autoduel.png")
img = cv2.imread(location)
points, version = self.provider.verify_battle(img)
self.assertTrue(version == 2)
def test_wait_for(self):
location = os.path.join(self.provider.assets, "steam", "ok_button_duel.png")
img = cv2.imread(location)
img = crop_image(img, **self.provider.predefined.ok_button_duel)
word = self.provider.img_to_string(img, alphabet).lower()
self.assertTrue(word == 'ok')
def test_wait_for_notifications(self):
self.fail()
def test_battle_icons(self):
self.provider.is_process_running()
img = self.provider.get_img_from_screen_shot()
area = self.provider.predefined.main_area
area['height'] = 700
t = tm.BoundingTrainer(img, bounding_area=area)
t.capture_white_circles()
| tests/providers/test_steam_.py | 6,900 | t.show_area_bounded(self.provider.predefined.main_area, img) t._debug = True t.compare() t.show_area_bounded(self.provider.predefined.main_area, img) t._debug = True t.compare() t.show_area_bounded(self.provider.predefined.main_area, img) t._debug = True t.show_area_bounded(self.provider.predefined.main_area, img) t._debug = True t.show_area(500, 300, 600, 300, img) self.provider.swipe_right(0) self.provider.swipe_left(0) | 425 | fa | 0.042188 |
from ..errors import MalformedResponseError
from ..properties import FailedMailbox, SearchableMailbox
from ..util import MNS, add_xml_child, create_element
from ..version import EXCHANGE_2013
from .common import EWSService
class GetSearchableMailboxes(EWSService):
"""MSDN:
https://docs.microsoft.com/en-us/exchange/client-developer/web-service-reference/getsearchablemailboxes-operation
"""
SERVICE_NAME = "GetSearchableMailboxes"
element_container_name = f"{{{MNS}}}SearchableMailboxes"
failed_mailboxes_container_name = f"{{{MNS}}}FailedMailboxes"
supported_from = EXCHANGE_2013
cls_map = {cls.response_tag(): cls for cls in (SearchableMailbox, FailedMailbox)}
def call(self, search_filter, expand_group_membership):
return self._elems_to_objs(
self._get_elements(
payload=self.get_payload(
search_filter=search_filter,
expand_group_membership=expand_group_membership,
)
)
)
def _elem_to_obj(self, elem):
return self.cls_map[elem.tag].from_xml(elem=elem, account=None)
def get_payload(self, search_filter, expand_group_membership):
payload = create_element(f"m:{self.SERVICE_NAME}")
if search_filter:
add_xml_child(payload, "m:SearchFilter", search_filter)
if expand_group_membership is not None:
add_xml_child(payload, "m:ExpandGroupMembership", "true" if expand_group_membership else "false")
return payload
def _get_elements_in_response(self, response):
for msg in response:
for container_name in (self.element_container_name, self.failed_mailboxes_container_name):
try:
container = self._get_element_container(message=msg, name=container_name)
except MalformedResponseError:
# Responses may contain no mailboxes of either kind. _get_element_container() does not accept this.
continue
yield from self._get_elements_in_container(container=container)
| exchangelib/services/get_searchable_mailboxes.py | 2,114 | MSDN:
https://docs.microsoft.com/en-us/exchange/client-developer/web-service-reference/getsearchablemailboxes-operation
Responses may contain no mailboxes of either kind. _get_element_container() does not accept this. | 219 | en | 0.601767 |
#!/usr/bin/env python2
# coding=utf-8
# ^^^^^^^^^^^^ TODO remove when supporting only Python3
# Copyright (c) 2014-2015 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import *
class WalletTest (BitcoinTestFramework):
def check_fee_amount(self, curr_balance, balance_with_fee, fee_per_byte, tx_size):
"""Return curr_balance after asserting the fee was in range"""
fee = balance_with_fee - curr_balance
target_fee = fee_per_byte * tx_size
if fee < target_fee:
raise AssertionError("Fee of %s CHAVEZCOIN too low! (Should be %s CHAVEZCOIN)"%(str(fee), str(target_fee)))
# allow the node's estimation to be at most 2 bytes off
if fee > fee_per_byte * (tx_size + 2):
raise AssertionError("Fee of %s CHAVEZCOIN too high! (Should be %s CHAVEZCOIN)"%(str(fee), str(target_fee)))
return curr_balance
def setup_chain(self):
print("Initializing test directory "+self.options.tmpdir)
initialize_chain_clean(self.options.tmpdir, 4)
def setup_network(self, split=False):
self.nodes = start_nodes(3, self.options.tmpdir)
connect_nodes_bi(self.nodes,0,1)
connect_nodes_bi(self.nodes,1,2)
connect_nodes_bi(self.nodes,0,2)
self.is_network_split=False
self.sync_all()
def run_test (self):
# Check that there's no UTXO on none of the nodes
assert_equal(len(self.nodes[0].listunspent()), 0)
assert_equal(len(self.nodes[1].listunspent()), 0)
assert_equal(len(self.nodes[2].listunspent()), 0)
print "Mining blocks..."
self.nodes[0].generate(1)
walletinfo = self.nodes[0].getwalletinfo()
assert_equal(walletinfo['immature_balance'], 500)
assert_equal(walletinfo['balance'], 0)
self.sync_all()
self.nodes[1].generate(101)
self.sync_all()
assert_equal(self.nodes[0].getbalance(), 500)
assert_equal(self.nodes[1].getbalance(), 500)
assert_equal(self.nodes[2].getbalance(), 0)
# Check that only first and second nodes have UTXOs
assert_equal(len(self.nodes[0].listunspent()), 1)
assert_equal(len(self.nodes[1].listunspent()), 1)
assert_equal(len(self.nodes[2].listunspent()), 0)
# Send 210 CHAVEZCOIN from 0 to 2 using sendtoaddress call.
# Second transaction will be child of first, and will require a fee
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 110)
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 100)
walletinfo = self.nodes[0].getwalletinfo()
assert_equal(walletinfo['immature_balance'], 0)
# Have node0 mine a block, thus it will collect its own fee.
self.nodes[0].generate(1)
self.sync_all()
# Exercise locking of unspent outputs
unspent_0 = self.nodes[2].listunspent()[0]
unspent_0 = {"txid": unspent_0["txid"], "vout": unspent_0["vout"]}
self.nodes[2].lockunspent(False, [unspent_0])
assert_raises(JSONRPCException, self.nodes[2].sendtoaddress, self.nodes[2].getnewaddress(), 200)
assert_equal([unspent_0], self.nodes[2].listlockunspent())
self.nodes[2].lockunspent(True, [unspent_0])
assert_equal(len(self.nodes[2].listlockunspent()), 0)
# Have node1 generate 100 blocks (so node0 can recover the fee)
self.nodes[1].generate(100)
self.sync_all()
# node0 should end up with 1000 CHAVEZCOIN in block rewards plus fees, but
# minus the 210 plus fees sent to node2
assert_equal(self.nodes[0].getbalance(), 1000-210)
assert_equal(self.nodes[2].getbalance(), 210)
# Node0 should have two unspent outputs.
# Create a couple of transactions to send them to node2, submit them through
# node1, and make sure both node0 and node2 pick them up properly:
node0utxos = self.nodes[0].listunspent(1)
assert_equal(len(node0utxos), 2)
# create both transactions
txns_to_send = []
for utxo in node0utxos:
inputs = []
outputs = {}
inputs.append({ "txid" : utxo["txid"], "vout" : utxo["vout"]})
outputs[self.nodes[2].getnewaddress("from1")] = utxo["amount"]
raw_tx = self.nodes[0].createrawtransaction(inputs, outputs)
txns_to_send.append(self.nodes[0].signrawtransaction(raw_tx))
# Have node 1 (miner) send the transactions
self.nodes[1].sendrawtransaction(txns_to_send[0]["hex"], True)
self.nodes[1].sendrawtransaction(txns_to_send[1]["hex"], True)
# Have node1 mine a block to confirm transactions:
self.nodes[1].generate(1)
self.sync_all()
assert_equal(self.nodes[0].getbalance(), 0)
assert_equal(self.nodes[2].getbalance(), 1000)
assert_equal(self.nodes[2].getbalance("from1"), 1000-210)
# Send 100 CHAVEZCOIN normal
address = self.nodes[0].getnewaddress("test")
fee_per_byte = Decimal('0.001') / 1000
self.nodes[2].settxfee(fee_per_byte * 1000)
txid = self.nodes[2].sendtoaddress(address, 100, "", "", False)
self.nodes[2].generate(1)
self.sync_all()
node_2_bal = self.check_fee_amount(self.nodes[2].getbalance(), Decimal('900'), fee_per_byte, count_bytes(self.nodes[2].getrawtransaction(txid)))
assert_equal(self.nodes[0].getbalance(), Decimal('100'))
# Send 100 CHAVEZCOIN with subtract fee from amount
txid = self.nodes[2].sendtoaddress(address, 100, "", "", True)
self.nodes[2].generate(1)
self.sync_all()
node_2_bal -= Decimal('100')
assert_equal(self.nodes[2].getbalance(), node_2_bal)
node_0_bal = self.check_fee_amount(self.nodes[0].getbalance(), Decimal('200'), fee_per_byte, count_bytes(self.nodes[2].getrawtransaction(txid)))
# Sendmany 100 CHAVEZCOIN
txid = self.nodes[2].sendmany('from1', {address: 100}, 0, "", [])
self.nodes[2].generate(1)
self.sync_all()
node_0_bal += Decimal('100')
node_2_bal = self.check_fee_amount(self.nodes[2].getbalance(), node_2_bal - Decimal('100'), fee_per_byte, count_bytes(self.nodes[2].getrawtransaction(txid)))
assert_equal(self.nodes[0].getbalance(), node_0_bal)
# Sendmany 100 CHAVEZCOIN with subtract fee from amount
txid = self.nodes[2].sendmany('from1', {address: 100}, 0, "", [address])
self.nodes[2].generate(1)
self.sync_all()
node_2_bal -= Decimal('100')
assert_equal(self.nodes[2].getbalance(), node_2_bal)
node_0_bal = self.check_fee_amount(self.nodes[0].getbalance(), node_0_bal + Decimal('100'), fee_per_byte, count_bytes(self.nodes[2].getrawtransaction(txid)))
# Test ResendWalletTransactions:
# Create a couple of transactions, then start up a fourth
# node (nodes[3]) and ask nodes[0] to rebroadcast.
# EXPECT: nodes[3] should have those transactions in its mempool.
txid1 = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 1)
txid2 = self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), 1)
sync_mempools(self.nodes)
self.nodes.append(start_node(3, self.options.tmpdir))
connect_nodes_bi(self.nodes, 0, 3)
sync_blocks(self.nodes)
relayed = self.nodes[0].resendwallettransactions()
assert_equal(set(relayed), {txid1, txid2})
sync_mempools(self.nodes)
assert(txid1 in self.nodes[3].getrawmempool())
# Exercise balance rpcs
assert_equal(self.nodes[0].getwalletinfo()["unconfirmed_balance"], 1)
assert_equal(self.nodes[0].getunconfirmedbalance(), 1)
#check if we can list zero value tx as available coins
#1. create rawtx
#2. hex-changed one output to 0.0
#3. sign and send
#4. check if recipient (node0) can list the zero value tx
usp = self.nodes[1].listunspent()
inputs = [{"txid":usp[0]['txid'], "vout":usp[0]['vout']}]
outputs = {self.nodes[1].getnewaddress(): 499.998, self.nodes[0].getnewaddress(): 11.11}
rawTx = self.nodes[1].createrawtransaction(inputs, outputs).replace("c0833842", "00000000") #replace 11.11 with 0.0 (int32)
decRawTx = self.nodes[1].decoderawtransaction(rawTx)
signedRawTx = self.nodes[1].signrawtransaction(rawTx)
decRawTx = self.nodes[1].decoderawtransaction(signedRawTx['hex'])
zeroValueTxid= decRawTx['txid']
sendResp = self.nodes[1].sendrawtransaction(signedRawTx['hex'])
self.sync_all()
self.nodes[1].generate(1) #mine a block
self.sync_all()
unspentTxs = self.nodes[0].listunspent() #zero value tx must be in listunspents output
found = False
for uTx in unspentTxs:
if uTx['txid'] == zeroValueTxid:
found = True
assert_equal(uTx['amount'], Decimal('0'))
assert(found)
#do some -walletbroadcast tests
stop_nodes(self.nodes)
wait_bitcoinds()
self.nodes = start_nodes(3, self.options.tmpdir, [["-walletbroadcast=0"],["-walletbroadcast=0"],["-walletbroadcast=0"]])
connect_nodes_bi(self.nodes,0,1)
connect_nodes_bi(self.nodes,1,2)
connect_nodes_bi(self.nodes,0,2)
self.sync_all()
txIdNotBroadcasted = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 2)
txObjNotBroadcasted = self.nodes[0].gettransaction(txIdNotBroadcasted)
self.nodes[1].generate(1) #mine a block, tx should not be in there
self.sync_all()
assert_equal(self.nodes[2].getbalance(), node_2_bal) #should not be changed because tx was not broadcasted
#now broadcast from another node, mine a block, sync, and check the balance
self.nodes[1].sendrawtransaction(txObjNotBroadcasted['hex'])
self.nodes[1].generate(1)
self.sync_all()
node_2_bal += 2
txObjNotBroadcasted = self.nodes[0].gettransaction(txIdNotBroadcasted)
assert_equal(self.nodes[2].getbalance(), node_2_bal)
#create another tx
txIdNotBroadcasted = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 2)
#restart the nodes with -walletbroadcast=1
stop_nodes(self.nodes)
wait_bitcoinds()
self.nodes = start_nodes(3, self.options.tmpdir)
connect_nodes_bi(self.nodes,0,1)
connect_nodes_bi(self.nodes,1,2)
connect_nodes_bi(self.nodes,0,2)
sync_blocks(self.nodes)
self.nodes[0].generate(1)
sync_blocks(self.nodes)
node_2_bal += 2
#tx should be added to balance because after restarting the nodes tx should be broadcastet
assert_equal(self.nodes[2].getbalance(), node_2_bal)
#send a tx with value in a string (PR#6380 +)
txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "2")
txObj = self.nodes[0].gettransaction(txId)
assert_equal(txObj['amount'], Decimal('-2'))
txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "0.0001")
txObj = self.nodes[0].gettransaction(txId)
assert_equal(txObj['amount'], Decimal('-0.0001'))
#check if JSON parser can handle scientific notation in strings
txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "1e-4")
txObj = self.nodes[0].gettransaction(txId)
assert_equal(txObj['amount'], Decimal('-0.0001'))
try:
txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "1f-4")
except JSONRPCException as e:
assert("Invalid amount" in e.error['message'])
else:
raise AssertionError("Must not parse invalid amounts")
try:
self.nodes[0].generate("2")
raise AssertionError("Must not accept strings as numeric")
except JSONRPCException as e:
assert("not an integer" in e.error['message'])
# Import address and private key to check correct behavior of spendable unspents
# 1. Send some coins to generate new UTXO
address_to_import = self.nodes[2].getnewaddress()
txid = self.nodes[0].sendtoaddress(address_to_import, 1)
self.nodes[0].generate(1)
self.sync_all()
# 2. Import address from node2 to node1
self.nodes[1].importaddress(address_to_import)
# 3. Validate that the imported address is watch-only on node1
assert(self.nodes[1].validateaddress(address_to_import)["iswatchonly"])
# 4. Check that the unspents after import are not spendable
assert_array_result(self.nodes[1].listunspent(),
{"address": address_to_import},
{"spendable": False})
# 5. Import private key of the previously imported address on node1
priv_key = self.nodes[2].dumpprivkey(address_to_import)
self.nodes[1].importprivkey(priv_key)
# 6. Check that the unspents are now spendable on node1
assert_array_result(self.nodes[1].listunspent(),
{"address": address_to_import},
{"spendable": True})
#check if wallet or blochchain maintenance changes the balance
self.sync_all()
blocks = self.nodes[0].generate(2)
self.sync_all()
balance_nodes = [self.nodes[i].getbalance() for i in range(3)]
block_count = self.nodes[0].getblockcount()
# Check modes:
# - True: unicode escaped as \u....
# - False: unicode directly as UTF-8
for mode in [True, False]:
self.nodes[0].ensure_ascii = mode
# unicode check: Basic Multilingual Plane, Supplementary Plane respectively
for s in [u'рыба', u'𝅘𝅥𝅯']:
addr = self.nodes[0].getaccountaddress(s)
label = self.nodes[0].getaccount(addr)
assert_equal(label.encode('utf-8'), s.encode('utf-8')) # TODO remove encode(...) when supporting only Python3
assert(s in self.nodes[0].listaccounts().keys())
self.nodes[0].ensure_ascii = True # restore to default
# maintenance tests
maintenance = [
'-rescan',
'-reindex',
'-zapwallettxes=1',
'-zapwallettxes=2',
'-salvagewallet',
]
for m in maintenance:
print "check " + m
stop_nodes(self.nodes)
wait_bitcoinds()
self.nodes = start_nodes(3, self.options.tmpdir, [[m]] * 3)
while m == '-reindex' and [block_count] * 3 != [self.nodes[i].getblockcount() for i in range(3)]:
# reindex will leave rpc warm up "early"; Wait for it to finish
time.sleep(0.1)
assert_equal(balance_nodes, [self.nodes[i].getbalance() for i in range(3)])
# Exercise listsinceblock with the last two blocks
coinbase_tx_1 = self.nodes[0].listsinceblock(blocks[0])
assert_equal(coinbase_tx_1["lastblock"], blocks[1])
assert_equal(len(coinbase_tx_1["transactions"]), 1)
assert_equal(coinbase_tx_1["transactions"][0]["blockhash"], blocks[1])
assert_equal(len(self.nodes[0].listsinceblock(blocks[1])["transactions"]), 0)
if __name__ == '__main__':
WalletTest ().main ()
| qa/rpc-tests/wallet.py | 15,632 | !/usr/bin/env python2 coding=utf-8 ^^^^^^^^^^^^ TODO remove when supporting only Python3 Copyright (c) 2014-2015 The Bitcoin Core developers Distributed under the MIT software license, see the accompanying file COPYING or http://www.opensource.org/licenses/mit-license.php. allow the node's estimation to be at most 2 bytes off Check that there's no UTXO on none of the nodes Check that only first and second nodes have UTXOs Send 210 CHAVEZCOIN from 0 to 2 using sendtoaddress call. Second transaction will be child of first, and will require a fee Have node0 mine a block, thus it will collect its own fee. Exercise locking of unspent outputs Have node1 generate 100 blocks (so node0 can recover the fee) node0 should end up with 1000 CHAVEZCOIN in block rewards plus fees, but minus the 210 plus fees sent to node2 Node0 should have two unspent outputs. Create a couple of transactions to send them to node2, submit them through node1, and make sure both node0 and node2 pick them up properly: create both transactions Have node 1 (miner) send the transactions Have node1 mine a block to confirm transactions: Send 100 CHAVEZCOIN normal Send 100 CHAVEZCOIN with subtract fee from amount Sendmany 100 CHAVEZCOIN Sendmany 100 CHAVEZCOIN with subtract fee from amount Test ResendWalletTransactions: Create a couple of transactions, then start up a fourth node (nodes[3]) and ask nodes[0] to rebroadcast. EXPECT: nodes[3] should have those transactions in its mempool. Exercise balance rpcscheck if we can list zero value tx as available coins1. create rawtx2. hex-changed one output to 0.03. sign and send4. check if recipient (node0) can list the zero value txreplace 11.11 with 0.0 (int32)mine a blockzero value tx must be in listunspents outputdo some -walletbroadcast testsmine a block, tx should not be in thereshould not be changed because tx was not broadcastednow broadcast from another node, mine a block, sync, and check the balancecreate another txrestart the nodes with -walletbroadcast=1tx should be added to balance because after restarting the nodes tx should be broadcastetsend a tx with value in a string (PR6380 +)check if JSON parser can handle scientific notation in strings Import address and private key to check correct behavior of spendable unspents 1. Send some coins to generate new UTXO 2. Import address from node2 to node1 3. Validate that the imported address is watch-only on node1 4. Check that the unspents after import are not spendable 5. Import private key of the previously imported address on node1 6. Check that the unspents are now spendable on node1check if wallet or blochchain maintenance changes the balance Check modes: - True: unicode escaped as \u.... - False: unicode directly as UTF-8 unicode check: Basic Multilingual Plane, Supplementary Plane respectively TODO remove encode(...) when supporting only Python3 restore to default maintenance tests reindex will leave rpc warm up "early"; Wait for it to finish Exercise listsinceblock with the last two blocks | 3,012 | en | 0.823305 |
# coding=utf-8
# Copyright 2020 The Google Research Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for Transform library."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import string
import numpy as np
import pandas as pd
import pandas.util.testing as pandas_testing
from six.moves import range
import tensorflow.compat.v1 as tf
from correct_batch_effects_wdn import metadata
from correct_batch_effects_wdn import transform
_ACTIVITY = "ACTIVE"
_PLATE = "plate1"
_SITE = 0
_TIMEPOINT = "0"
_SEQUENCE = "AGCT"
_CELL_DENSITY = "0"
_PASSAGE = "0"
_CELL_LINE_ID = ""
class TransformTest(tf.test.TestCase):
def setUp(self):
super(TransformTest, self).setUp()
wells_384, rows_384, cols_384 = [], [], []
for row in string.ascii_uppercase[:16]:
for col in range(24):
wells_384.append("%s%02d" % (row, col))
rows_384.append("%s" % row)
cols_384.append("%02d" % col)
n_per_batch = 100
n_each_control = 3 * n_per_batch
n_other = 3 * n_per_batch
np.random.seed(123)
self.columns = [0, 1]
neg_control_batches = []
for i in range(0, n_each_control, n_per_batch):
batch = "week%d" % (i % n_per_batch)
control_tuples = []
for j in range(n_per_batch):
control_tuples.append(
("NEGATIVE_CONTROL", "DMSO", "DMSO", 1.0, _ACTIVITY, batch, _PLATE,
wells_384[j], rows_384[j], cols_384[j], _SITE, _TIMEPOINT,
_SEQUENCE, _CELL_DENSITY, _PASSAGE, _CELL_LINE_ID))
neg_control_batches.append(
pd.DataFrame(
np.random.multivariate_normal(
mean=np.array([2.0 + i, 4.0 + i]),
cov=np.array([[3.0 + i, 1.0 + i], [1.0 + i, 2.0 + i]]),
size=n_per_batch),
columns=self.columns,
index=pd.MultiIndex.from_tuples(
control_tuples, names=metadata.METADATA_ORDER)))
self.neg_controls = pd.concat(neg_control_batches)
pos_control_batches = []
for i in range(0, n_each_control, n_per_batch):
batch = "week%d" % (i % n_per_batch)
control_tuples = []
for j in range(n_per_batch):
control_tuples.append(
("POSITIVE_CONTROL", "Taxol", "Taxol", 1.0, _ACTIVITY, batch,
_PLATE, wells_384[j], rows_384[j], cols_384[j], _SITE, _TIMEPOINT,
_SEQUENCE, _CELL_DENSITY, _PASSAGE, _CELL_LINE_ID))
pos_control_batches.append(
pd.DataFrame(
np.random.multivariate_normal(
mean=np.array([5.0 + i, 7.0 + i]),
cov=np.array([[6.0 + i, 4.0 + i], [4.0 + i, 5.0 + i]]),
size=n_per_batch),
columns=self.columns,
index=pd.MultiIndex.from_tuples(
control_tuples, names=metadata.METADATA_ORDER)))
self.pos_controls = pd.concat(pos_control_batches)
self.controls = pd.concat([self.neg_controls, self.pos_controls])
experimental_batches = []
for i in range(0, n_other, n_per_batch):
batch = "week%d" % (i % n_per_batch)
experimental_tuples = []
for j in range(n_per_batch):
experimental_tuples.append(
("EXPERIMENTAL", "other", "2", 1.0, _ACTIVITY, batch, _PLATE,
wells_384[j], rows_384[j], cols_384[j], _SITE, _TIMEPOINT,
_SEQUENCE, _CELL_DENSITY, _PASSAGE, _CELL_LINE_ID))
experimental_batches.append(
pd.DataFrame(
np.random.multivariate_normal(
mean=np.array([1.0 + i, 2.0 + i]),
cov=np.array([[3.0 + i, 1.0 + i], [1.0 + i, 2.0 + i]]),
size=n_per_batch),
columns=self.columns,
index=pd.MultiIndex.from_tuples(
experimental_tuples, names=metadata.METADATA_ORDER)))
self.experimental = pd.concat(experimental_batches)
self.data = pd.concat([self.controls, self.experimental])
def testGetNegativeControls(self):
pandas_testing.assert_frame_equal(self.neg_controls,
transform.get_negative_controls(
self.data))
def testEigSymmetric(self):
q_expected = np.array([[1.0 / np.sqrt(2), -1.0 / np.sqrt(2)],
[1.0 / np.sqrt(2), 1.0 / np.sqrt(2)]])
# q should be orthonormal - make sure it really is
pandas_testing.assert_almost_equal(
q_expected.T.dot(q_expected), np.identity(2))
lambda_expected = np.diag([3.0, 2.0])
a = q_expected.dot(lambda_expected).dot(q_expected.T)
lambda_computed, q_computed = transform.eig_symmetric(a)
pandas_testing.assert_almost_equal(
np.diag(lambda_expected), lambda_computed)
# make sure q_computed is orthonormal
pandas_testing.assert_almost_equal(
np.identity(2), q_expected.T.dot(q_expected))
for i in range(q_expected.shape[0]):
ev_expected = q_expected[:, i]
ev_computed = q_computed[:, i]
# In this example, the eigenvalues are discrete, so the eigenvectors are
# unique up to sign. Since the sign will depend on the particulars of
# the algorithm used to generate the eigenvectors, just make sure that
# the dot product with the expected eigenvectors is +/- 1
pandas_testing.assert_almost_equal(1.0,
np.abs(ev_expected.dot(ev_computed)))
def testFactorAnalysisRun(self):
transform.factor_analysis(self.data, 0.1, -1)
def testGetBootstrapSampleRun(self):
bootstrap_data = transform.get_bootstrap_sample(self.data)
self.assertTupleEqual(self.data.shape, bootstrap_data.shape)
def testTransformDf(self):
df_small = pd.DataFrame(np.array([[1.0, 2.0], [3.0, 4.0]]))
rotate_mat_np = np.array([[3.0, 4.0], [5.0, 6.0]])
shift_vec_np = np.array([[-1.0], [-2.0]])
expected = pd.DataFrame(np.array([[10.0, 15.0], [24.0, 37.0]]))
df_trans = transform.transform_df(
df_small, rotate_mat_np, shift_vec_np)
pandas_testing.assert_frame_equal(df_trans, expected)
def testSumOfSquare(self):
a = tf.constant(np.array([1.0, 2.0]))
expected = 5.0
with self.session() as sess:
a_sum_of_square = sess.run(transform.sum_of_square(a))
self.assertEqual(a_sum_of_square, expected)
def testDropUnevaluatedComp(self):
pandas_testing.assert_frame_equal(
pd.concat([self.pos_controls, self.experimental]),
transform.drop_unevaluated_comp(self.data))
if __name__ == "__main__":
tf.test.main()
| correct_batch_effects_wdn/transform_test.py | 7,074 | Tests for Transform library.
coding=utf-8 Copyright 2020 The Google Research Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. q should be orthonormal - make sure it really is make sure q_computed is orthonormal In this example, the eigenvalues are discrete, so the eigenvectors are unique up to sign. Since the sign will depend on the particulars of the algorithm used to generate the eigenvectors, just make sure that the dot product with the expected eigenvectors is +/- 1 | 959 | en | 0.89004 |
# -*- coding=utf-8 -*-
"""
# library: jionlp
# author: dongrixinyu
# license: Apache License 2.0
# Email: dongrixinyu.89@163.com
# github: https://github.com/dongrixinyu/JioNLP
# description: Preprocessing tool for Chinese NLP
"""
__version__ = '1.3.49'
import os
from jionlp.util.logger import set_logger
from jionlp.util.zip_file import unzip_file, UNZIP_FILE_LIST
logging = set_logger(level='INFO', log_dir_name='.jionlp_logs')
# unzip dictionary files
DIR_PATH = os.path.dirname(os.path.abspath(__file__))
for file_name in UNZIP_FILE_LIST:
if not os.path.exists(os.path.join(DIR_PATH, 'dictionary', file_name)):
unzip_file()
history = """
╭──────────────────────────────────────────────────────────────────────────╮
│ • • • ░░░░░░░░░░░░░░░░░░░░░ History Messages ░░░░░░░░░░░░░░░░░░░░░░░░░ │
├──────────────────────────────────────────────────────────────────────────┤
│ │
│ JioNLP, a python tool for Chinese NLP preprocessing & parsing. │
│ URL: https://github.com/dongrixinyu/JioNLP │
│ │
│ | date | updated funcs and info | │
│ | ---------- | --------------------------------------------------- | │
│ | 2020-03-13 | first push | │
│ | 2020-03-18 | update rules | │
│ | 2020-03-24 | add traditional and simplified conversion | │
│ | 2020-03-26 | add location parser 2019 | │
│ | 2020-03-31 | add sentences splitter | │
│ | 2020-04-02 | add id chard parser | │
│ | 2020-04-03 | add stopwords remover | │
│ | 2020-04-26 | add pinyin and location recognizer | │
│ | 2020-05-26 | add chinese word, char, xiehouyu dict | │
│ | 2020-06-01 | add ner tools | │
│ | 2020-06-10 | add location recognizer | │
│ | 2020-06-30 | add char radical parser | │
│ | 2020-07-07 | add ner acceleration tools and lexicon ner tool | │
│ | 2020-07-13 | add sim hash tool | │
│ | 2020-07-14 | add sentiment analysis | │
│ | 2020-07-27 | add key phrase extraction - ckpe | │
│ | 2020-08-24 | update pinyin | │
│ | 2020-09-14 | add back translation for data augmentation | │
│ | 2020-10-16 | update 2020 china location dictionary | │
│ | 2020-10-19 | add zip_file for compressing the size of dict files | │
│ | 2020-11-10 | add extractive summary func | │
│ | 2020-11-24 | add phone location recognition | │
│ | 2020-12-18 | add idiom solitaire | │
│ | 2020-12-28 | add help searching tool | │
│ | 2021-01-19 | add money number to character tool | │
│ | 2021-01-22 | update outdated china location conversion | │
│ | 2021-02-01 | acquire 400 stars and 58 forks on Github | │
│ | 2021-02-02 | add swap char position text augmentation | │
│ | 2021-02-09 | add homophone and add & delete text augmentation | │
│ | 2021-02-10 | update dictionaries | │
│ | 2021-03-15 | update chinese char dictionaries | │
│ | 2021-03-18 | add replace entity text augmentation | │
│ | 2021-03-24 | update extract money and standardization | │
│ | 2021-04-21 | add solar lunar date conversion | │
│ | 2021-06-23 | add time parser | │
│ | 2021-07-04 | update time parser | │
│ | 2021-07-18 | update time parser | │
│ | 2021-09-01 | add jionlp online version | │
│ | 2021-10-25 | update extract money and parse money | │
│ | 2021-11-10 | add logger tuner | │
│ | 2021-12-04 | add chinese word segmentor tools | │
│ │
╰──────────────────────────────────────────────────────────────────────────╯
"""
from jionlp.util import *
from jionlp.dictionary import *
from jionlp.rule import *
from jionlp.gadget import *
from jionlp.textaug import *
from jionlp.algorithm import *
# from jionlp.util.fast_loader import FastLoader
# rule = FastLoader('rule', globals(), 'jionlp.rule')
| jionlp/__init__.py | 5,700 | # library: jionlp
# author: dongrixinyu
# license: Apache License 2.0
# Email: dongrixinyu.89@163.com
# github: https://github.com/dongrixinyu/JioNLP
# description: Preprocessing tool for Chinese NLP
-*- coding=utf-8 -*- unzip dictionary files from jionlp.util.fast_loader import FastLoader rule = FastLoader('rule', globals(), 'jionlp.rule') | 344 | en | 0.408435 |
"""
This module contains the lambda function code for put-storage-tags API.
This file uses environment variables in place of config; thus
sddcapi_boot_dir is not required.
"""
# pylint: disable=import-error,logging-format-interpolation,broad-except,too-many-statements,C0413,W1203,R1703,R0914
import boto3
import botocore.exceptions
import os
import sys
import json
import traceback
from cloudx_sls_authorization import lambda_auth
THISDIR = os.path.dirname(__file__) # boto3-proxy
APPDIR = os.path.dirname(THISDIR) # boto3-proxy
if APPDIR not in sys.path:
sys.path.append(APPDIR)
if THISDIR not in sys.path:
sys.path.append(THISDIR)
from utils import api_request
from utils import helpers, secrets
from utils.exceptions import InvalidRegionException, InvalidInputException
from utils.log_helper import Logger
logger = Logger()
# Define LDAP lookup configs
LDAP_SERVER = os.environ['LDAP_SERVER']
LDAP_USERNAME = os.environ['LDAP_USERNAME']
LDAP_PASSWORD_SECRET_NAME = os.environ['LDAP_PASSWORD_SECRET_NAME']
LDAP_SEARCH_BASE = os.environ['LDAP_SEARCH_BASE']
LDAP_OBJECT_CLASS = os.environ['LDAP_OBJECT_CLASS']
LDAP_GROUP_NAME = os.environ['LDAP_GROUP_NAME'].split(',')
LDAP_LOOKUP_ATTRIBUTE = os.environ['LDAP_LOOKUP_ATTRIBUTE']
MSFT_IDP_TENANT_ID = os.environ['MSFT_IDP_TENANT_ID']
MSFT_IDP_APP_ID = os.environ['MSFT_IDP_APP_ID'].split(',')
MSFT_IDP_CLIENT_ROLES = os.environ['MSFT_IDP_CLIENT_ROLES'].split(',')
# Status codes
SUCCESS_STATUS = 200 # success
BAD_REQUEST_STATUS = 400 # service not supported, action not supported
NOT_FOUND_STATUS = 404 # invalid account, invalid region
UNAUTHORIZED_STATUS = 401 # invalid auth token
INTERNAL_SERVICE_ERROR_STATUS = 500 # internal service error
def handler(event, context):
"""
Boto3 Proxy API Handler
"""
headers = event.get('Headers', event.get('headers'))
if 'request-context-id' in headers:
logger.set_uuid(headers['request-context-id'])
logger.info({"Incoming event": event})
logger.info('Incoming context: %s', context)
request_body = json.loads(event.get('body', {}))
try:
# Define service client
secrets_client = boto3.client('secretsmanager')
lambda_auth.authorize_lambda_request(event, MSFT_IDP_TENANT_ID, MSFT_IDP_APP_ID,
MSFT_IDP_CLIENT_ROLES, LDAP_SERVER, LDAP_USERNAME,
secrets.retrieve_ldap_password(secrets_client,
logger,
LDAP_PASSWORD_SECRET_NAME
),
LDAP_SEARCH_BASE,
LDAP_OBJECT_CLASS, LDAP_GROUP_NAME, LDAP_LOOKUP_ATTRIBUTE)
# Get the SSM client
ssm_client = boto3.client('ssm')
except Exception as e:
traceback.print_exc()
return {
'statusCode': UNAUTHORIZED_STATUS,
'body': json.dumps({'error': f"Unauthorized. {str(e)}"})
}
# Get environment variables
resp_headers = {
'Content-Type': 'application/json',
"request-context-id": logger.get_uuid()
}
if hasattr(context, 'local_test'):
logger.info('Running at local')
path_params = event.get('pathParameters', {})
request_headers = event.get('headers', {})
vpcxiam_endpoint = os.environ.get('vpcxiam_endpoint')
vpcxiam_scope = os.environ.get('vpcxiam_scope')
vpcxiam_host = os.environ.get('vpcxiam_host')
# Set the default success response and status code
resp = {
'message': 'API action has been successfully completed'
}
status_code = SUCCESS_STATUS
try:
account = path_params.get('account-id')
region = path_params.get('region-name')
service = path_params.get('boto3-service')
action = path_params.get('boto3-action')
logger.info(f"Account: {account}")
logger.info(f"Region: {region}")
logger.info(f"Boto3 Service: {service}")
logger.info(f"Boto3 Action: {action}")
# is authorized?
logger.info(f'is_authorized({request_headers}, {MSFT_IDP_APP_ID}, '
f'{MSFT_IDP_TENANT_ID}, {MSFT_IDP_CLIENT_ROLES}')
# Get the credentials for the account resources will be created in.
url = (vpcxiam_endpoint +
f"/v1/accounts/{account}/roles/admin/credentials")
scope = vpcxiam_scope
additional_headers = {
'Host': vpcxiam_host
}
api_requests = api_request.ApiRequests()
credentials = json.loads(
(api_requests.request(url=url, method='get', scope=scope, additional_headers=additional_headers)).text
)
error = credentials.get('error', {})
if error:
logger.error(error)
raise ValueError(error)
credentials = credentials.get('credentials', {})
try:
# Validate service and if valid, get the allowed actions
ssm_parameter_name = '/vpcx/aws/boto3-proxy/allowed-actions/'+service
logger.info("Looking up parameter "+ssm_parameter_name)
allowed_actions = ssm_client.get_parameter(Name=ssm_parameter_name)
except botocore.exceptions.ClientError as err:
logger.error(err)
if err.response['Error']['Code'] == 'ParameterNotFound':
raise InvalidInputException("Service " + service + " is not an allowed service for the API")
else:
raise error
# Validate action
if action not in allowed_actions['Parameter']['Value']:
raise InvalidInputException("Action "+action+" is not an allowed action for the API")
# Validate region
ec2_client = boto3.client(
service_name='ec2',
aws_access_key_id=credentials.get('AccessKeyId', ''),
aws_secret_access_key=credentials.get('SecretAccessKey', ''),
aws_session_token=credentials.get('SessionToken', ''))
helpers.is_region_valid(ec2_client, region)
logger.info(f"{region} is a valid region")
# Create clients for the given region and given account
boto3_client = boto3.client(
service_name=service,
region_name=region,
aws_access_key_id=credentials.get('AccessKeyId', ''),
aws_secret_access_key=credentials.get('SecretAccessKey', ''),
aws_session_token=credentials.get('SessionToken', ''))
# Call the action (function) for the boto3 client's service with the request params
kwargs = request_body
getattr(boto3_client, action)(**kwargs)
# boto3 error
except botocore.exceptions.ClientError as err:
status_code = INTERNAL_SERVICE_ERROR_STATUS
resp = {
'error': f'{type(err).__name__}: {err}'
}
except InvalidRegionException:
status_code = NOT_FOUND_STATUS
resp = {
'error': 'Please enter a valid region in the url path'
}
except InvalidInputException as err:
status_code = BAD_REQUEST_STATUS
resp = {
'error': str(err)
}
except ValueError as err:
status_code = NOT_FOUND_STATUS
resp = {
'error': str(err)
}
except Exception as err:
status_code = INTERNAL_SERVICE_ERROR_STATUS
resp = {
'error': f'{type(err).__name__}: {err}'
}
resp = helpers.lambda_returns(status_code, resp_headers, json.dumps(resp))
logger.info(f'response: {resp}')
return resp
| boto3_proxy/index.py | 7,752 | Boto3 Proxy API Handler
This module contains the lambda function code for put-storage-tags API.
This file uses environment variables in place of config; thus
sddcapi_boot_dir is not required.
pylint: disable=import-error,logging-format-interpolation,broad-except,too-many-statements,C0413,W1203,R1703,R0914 boto3-proxy boto3-proxy Define LDAP lookup configs Status codes success service not supported, action not supported invalid account, invalid region invalid auth token internal service error Define service client Get the SSM client Get environment variables Set the default success response and status code is authorized? Get the credentials for the account resources will be created in. Validate service and if valid, get the allowed actions Validate action Validate region Create clients for the given region and given account Call the action (function) for the boto3 client's service with the request params boto3 error | 931 | en | 0.60733 |
from unittest.mock import patch
from django.core.management import call_command
from django.db.utils import OperationalError
from django.test import TestCase
class CommandTests(TestCase):
def test_wait_for_db_ready(self):
"""Test waiting for db when db is available"""
with patch('django.db.utils.ConnectionHandler.__getitem__') as gi:
gi.return_value = True
call_command('wait_for_db')
self.assertEqual(gi.call_count, 1)
# Checking is mock object gi was called only once
@patch('time.sleep', return_value=True)
# The patch as a decorator will pass the argument to the test below it
def test_wait_for_db(self, ts):
"""Test waiting for db"""
# When the ConnectionHandler raised OperationalError, then it waits for
# 1 sec and tries again. Delay here can be removed in the unit test by
# using a patch decorator
with patch('django.db.utils.ConnectionHandler.__getitem__') as gi:
gi.side_effect = [OperationalError] * 5 + [True]
# For 1st 5 tries, it raises OperationalError, then 6th time True
call_command('wait_for_db')
self.assertEqual(gi.call_count, 6)
| app/core/tests/test_commands.py | 1,228 | Test waiting for db
Test waiting for db when db is available
Checking is mock object gi was called only once The patch as a decorator will pass the argument to the test below it When the ConnectionHandler raised OperationalError, then it waits for 1 sec and tries again. Delay here can be removed in the unit test by using a patch decorator For 1st 5 tries, it raises OperationalError, then 6th time True | 406 | en | 0.909335 |
#coding=utf-8
#
# Copyright (C) 2015 Feigr TECH Co., Ltd. All rights reserved.
# Created on 2013-8-13, by Junn
#
#
#import settings
from django.middleware.csrf import get_token
from django.http.response import Http404
from django.core.exceptions import PermissionDenied
from rest_framework.generics import GenericAPIView
from rest_framework import exceptions, status
from rest_framework.response import Response
from core.authentication import CsrfError
from utils.http import JResponse
from core import codes
def csrf_failure(request, reason=''):
"""
customize the response for csrf_token invalid
"""
# if request.is_ajax():
# return JResponse(codes.get('csrf_invalid'))
# return
get_token(request)
return JResponse(codes.get('csrf_invalid'), status=403)
class CustomAPIView(GenericAPIView):
"""
customize the APIView for customize exception response
"""
def handle_exception(self, exc):
"""
Handle any exception that occurs, by returning an appropriate response,
or re-raising the error.
"""
if isinstance(exc, exceptions.Throttled):
# Throttle wait header
self.headers['X-Throttle-Wait-Seconds'] = '%d' % exc.wait
if isinstance(exc, (exceptions.NotAuthenticated,
exceptions.AuthenticationFailed)):
# WWW-Authenticate header for 401 responses, else coerce to 403
auth_header = self.get_authenticate_header(self.request)
if auth_header:
self.headers['WWW-Authenticate'] = auth_header
else:
exc.status_code = status.HTTP_403_FORBIDDEN
if isinstance(exc, exceptions.MethodNotAllowed):
return Response(codes.get('invalid_request_method'),
status=exc.status_code,
exception=True)
elif isinstance(exc, CsrfError):
return Response(codes.get('csrf_invalid'),
status=exc.status_code,
exception=True)
elif isinstance(exc, exceptions.ParseError):
return Response(codes.get('parse_error'),
status=exc.status_code,
exception=True)
elif isinstance(exc, exceptions.AuthenticationFailed):
return Response(codes.get('authentication_failed'),
status=exc.status_code,
exception=True)
elif isinstance(exc, exceptions.NotAuthenticated):
return Response(codes.get('not_authenticated'),
status=exc.status_code,
exception=True)
elif isinstance(exc, exceptions.PermissionDenied):
return Response(codes.get('permission_denied'),
status=exc.status_code,
exception=True)
elif isinstance(exc, exceptions.NotAcceptable):
return Response(codes.get('not_acceptable'),
status=exc.status_code,
exception=True)
elif isinstance(exc, exceptions.UnsupportedMediaType):
return Response(codes.get('unsupported_media_type'),
status=exc.status_code,
exception=True)
elif isinstance(exc, exceptions.Throttled):
return Response(codes.get('throttled'),
status=exc.status_code,
exception=True)
elif isinstance(exc, Http404):
return Response(codes.get('not_found'),
status=status.HTTP_404_NOT_FOUND,
exception=True)
elif isinstance(exc, PermissionDenied):
return Response(codes.get('permission_denied'),
status=status.HTTP_403_FORBIDDEN,
exception=True)
raise
| apps/core/views.py | 4,016 | customize the APIView for customize exception response
customize the response for csrf_token invalid
Handle any exception that occurs, by returning an appropriate response,
or re-raising the error.
coding=utf-8 Copyright (C) 2015 Feigr TECH Co., Ltd. All rights reserved. Created on 2013-8-13, by Junnimport settings if request.is_ajax(): return JResponse(codes.get('csrf_invalid')) return Throttle wait header WWW-Authenticate header for 401 responses, else coerce to 403 | 477 | en | 0.631501 |
import os
from pathlib import Path
from dataclasses import field
from typing import Dict, Tuple, Sequence
from pydantic.dataclasses import dataclass
from pydantic import StrictStr
@dataclass
class StreetViewConfig:
SIZE: str = "600x300"
HEADING: str = "151.78"
PITCH: str = "-0.76"
KEY = os.environ.get("GOOGLE_DEV_API_KEY")
LOCAL_IMAGE_FOLDER: str = f"{Path(__file__).resolve().parent.parent.parent.parent.parent.parent.parent.parent}/local_data/streetview_images"
LOCAL_LINKS_FOLDER: str = f"{Path(__file__).resolve().parent.parent.parent.parent.parent.parent.parent.parent}/local_data/streetview_links"
LOCAL_METADATA_FOLDER: str = f"{Path(__file__).resolve().parent.parent.parent.parent.parent.parent.parent.parent}/local_data/streetview_metadata"
PLACE = "Iraqi_Kurdistan"
META_BASE = "https://maps.googleapis.com/maps/api/streetview/metadata?"
@dataclass
class OSMConfig:
TAGS = {"building": "residential"}
PLACE = "Iraq"
# NAME = "Iraqi Kurdistan"
# ADMIN_LEVEL = 3
@dataclass
class DataConfig:
COUNTRY_CODES = ["IQ"]
YEAR: int = 2020
MON_START: int = 1
DATE_START: int = 1
YEAR_END: int = 2021
MON_END: int = 8
DATE_END: int = 22
PLACE = "Iraqi Kurdistan, Iraq"
BASE_FOLDER = "/ee_data"
LANDSAT_IMAGE_COLLECTION: str = "LANDSAT/LC08/C01/T1"
MODEL_NAME = "CH4"
LANDSAT_IMAGE_BAND: Sequence[str] = field(
default_factory=lambda: ["B4", "B3", "B2"]
)
NIGHTTIME_LIGHT_IMAGE_COLLECTION: str = "NOAA/VIIRS/DNB/MONTHLY_V1/VCMCFG"
NIGHTTIME_LIGHT_IMAGE_BAND: str = "avg_rad"
METEROLOGICAL_IMAGE_COLLECTION: str = "NOAA/GFS0P25"
METEROLOGICAL_IMAGE_BAND: Sequence[str] = field(
default_factory=lambda: [
"temperature_2m_above_ground",
"relative_humidity_2m_above_ground",
"total_precipitation_surface",
"total_cloud_cover_entire_atmosphere",
"u_component_of_wind_10m_above_ground",
"v_component_of_wind_10m_above_ground",
]
)
POPULATION_IMAGE_COLLECTION: str = (
"CIESIN/GPWv411/GPW_Basic_Demographic_Characteristics"
)
POPULATION_IMAGE_BAND = "basic_demographic_characteristics"
LAND_COVER_IMAGE_COLLECTION: str = "COPERNICUS/Landcover/100m/Proba-V-C3/Global"
LAND_COVER_IMAGE_BAND: str = "discrete_classification"
IMAGE_FOLDER = "local_data/image_folder"
COUNTRY_BOUNDING_BOXES: Dict[
StrictStr, Tuple[StrictStr, Tuple[float, float, float, float]]
] = field(
default_factory=lambda: {
"AF": (
"Afghanistan",
(60.5284298033, 29.318572496, 75.1580277851, 38.4862816432),
),
"AO": (
"Angola",
(11.6400960629, -17.9306364885, 24.0799052263, -4.43802336998),
),
"AL": (
"Albania",
(19.3044861183, 39.624997667, 21.0200403175, 42.6882473822),
),
"AE": (
"United Arab Emirates",
(51.5795186705, 22.4969475367, 56.3968473651, 26.055464179),
),
"AR": (
"Argentina",
(-73.4154357571, -55.25, -53.628348965, -21.8323104794),
),
"AM": (
"Armenia",
(43.5827458026, 38.7412014837, 46.5057198423, 41.2481285671),
),
"AQ": ("Antarctica", (-180.0, -90.0, 180.0, -63.2706604895)),
"TF": ("Fr. S. and Antarctic Lands", (68.72, -49.775, 70.56, -48.625)),
"AU": (
"Australia",
(113.338953078, -43.6345972634, 153.569469029, -10.6681857235),
),
"AT": (
"Austria",
(9.47996951665, 46.4318173285, 16.9796667823, 49.0390742051),
),
"AZ": (
"Azerbaijan",
(44.7939896991, 38.2703775091, 50.3928210793, 41.8606751572),
),
"BI": (
"Burundi",
(29.0249263852, -4.49998341229, 30.752262811, -2.34848683025),
),
"BE": (
"Belgium",
(2.51357303225, 49.5294835476, 6.15665815596, 51.4750237087),
),
"BJ": (
"Benin",
(0.772335646171, 6.14215770103, 3.79711225751, 12.2356358912),
),
"BF": (
"Burkina Faso",
(-5.47056494793, 9.61083486576, 2.17710778159, 15.1161577418),
),
"BD": (
"Bangladesh",
(88.0844222351, 20.670883287, 92.6727209818, 26.4465255803),
),
"BG": (
"Bulgaria",
(22.3805257504, 41.2344859889, 28.5580814959, 44.2349230007),
),
"BS": ("Bahamas", (-78.98, 23.71, -77.0, 27.04)),
"BA": ("Bosnia and Herz.", (15.7500260759, 42.65, 19.59976, 45.2337767604)),
"BY": (
"Belarus",
(23.1994938494, 51.3195034857, 32.6936430193, 56.1691299506),
),
"BZ": (
"Belize",
(-89.2291216703, 15.8869375676, -88.1068129138, 18.4999822047),
),
"BO": (
"Bolivia",
(-69.5904237535, -22.8729187965, -57.4983711412, -9.76198780685),
),
"BR": (
"Brazil",
(-73.9872354804, -33.7683777809, -34.7299934555, 5.24448639569),
),
"BN": (
"Brunei",
(114.204016555, 4.007636827, 115.450710484, 5.44772980389),
),
"BT": (
"Bhutan",
(88.8142484883, 26.7194029811, 92.1037117859, 28.2964385035),
),
"BW": (
"Botswana",
(19.8954577979, -26.8285429827, 29.4321883481, -17.6618156877),
),
"CF": (
"Central African Rep.",
(14.4594071794, 2.2676396753, 27.3742261085, 11.1423951278),
),
"CA": ("Canada", (-140.99778, 41.6751050889, -52.6480987209, 83.23324)),
"CH": (
"Switzerland",
(6.02260949059, 45.7769477403, 10.4427014502, 47.8308275417),
),
"CL": ("Chile", (-75.6443953112, -55.61183, -66.95992, -17.5800118954)),
"CN": (
"China",
(73.6753792663, 18.197700914, 135.026311477, 53.4588044297),
),
"CI": (
"Ivory Coast",
(-8.60288021487, 4.33828847902, -2.56218950033, 10.5240607772),
),
"CM": (
"Cameroon",
(8.48881554529, 1.72767263428, 16.0128524106, 12.8593962671),
),
"CD": (
"Congo (Kinshasa)",
(12.1823368669, -13.2572266578, 31.1741492042, 5.25608775474),
),
"CG": (
"Congo (Brazzaville)",
(11.0937728207, -5.03798674888, 18.4530652198, 3.72819651938),
),
"CO": (
"Colombia",
(-78.9909352282, -4.29818694419, -66.8763258531, 12.4373031682),
),
"CR": (
"Costa Rica",
(-85.94172543, 8.22502798099, -82.5461962552, 11.2171192489),
),
"CU": (
"Cuba",
(-84.9749110583, 19.8554808619, -74.1780248685, 23.1886107447),
),
"CY": (
"Cyprus",
(32.2566671079, 34.5718694118, 34.0048808123, 35.1731247015),
),
"CZ": (
"Czech Rep.",
(12.2401111182, 48.5553052842, 18.8531441586, 51.1172677679),
),
"DE": (
"Germany",
(5.98865807458, 47.3024876979, 15.0169958839, 54.983104153),
),
"DJ": ("Djibouti", (41.66176, 10.9268785669, 43.3178524107, 12.6996385767)),
"DK": (
"Denmark",
(8.08997684086, 54.8000145534, 12.6900061378, 57.730016588),
),
"DO": (
"Dominican Rep.",
(-71.9451120673, 17.598564358, -68.3179432848, 19.8849105901),
),
"DZ": (
"Algeria",
(-8.68439978681, 19.0573642034, 11.9995056495, 37.1183806422),
),
"EC": (
"Ecuador",
(-80.9677654691, -4.95912851321, -75.2337227037, 1.3809237736),
),
"EG": ("Egypt", (24.70007, 22.0, 36.86623, 31.58568)),
"ER": (
"Eritrea",
(36.3231889178, 12.4554157577, 43.0812260272, 17.9983074),
),
"ES": (
"Spain",
(-9.39288367353, 35.946850084, 3.03948408368, 43.7483377142),
),
"EE": (
"Estonia",
(23.3397953631, 57.4745283067, 28.1316992531, 59.6110903998),
),
"ET": ("Ethiopia", (32.95418, 3.42206, 47.78942, 14.95943)),
"FI": (
"Finland",
(20.6455928891, 59.846373196, 31.5160921567, 70.1641930203),
),
"FJ": ("Fiji", (-180.0, -18.28799, 180.0, -16.0208822567)),
"FK": ("Falkland Is.", (-61.2, -52.3, -57.75, -51.1)),
"FR": (
"France",
(-54.5247541978, 2.05338918702, 9.56001631027, 51.1485061713),
),
"GA": (
"Gabon",
(8.79799563969, -3.97882659263, 14.4254557634, 2.32675751384),
),
"GB": (
"United Kingdom",
(-7.57216793459, 49.959999905, 1.68153079591, 58.6350001085),
),
"GE": (
"Georgia",
(39.9550085793, 41.0644446885, 46.6379081561, 43.553104153),
),
"GH": (
"Ghana",
(-3.24437008301, 4.71046214438, 1.0601216976, 11.0983409693),
),
"GN": (
"Guinea",
(-15.1303112452, 7.3090373804, -7.83210038902, 12.5861829696),
),
"GM": (
"Gambia",
(-16.8415246241, 13.1302841252, -13.8449633448, 13.8764918075),
),
"GW": (
"Guinea Bissau",
(-16.6774519516, 11.0404116887, -13.7004760401, 12.6281700708),
),
"GQ": (
"Eq. Guinea",
(9.3056132341, 1.01011953369, 11.285078973, 2.28386607504),
),
"GR": (
"Greece",
(20.1500159034, 34.9199876979, 26.6041955909, 41.8269046087),
),
"GL": ("Greenland", (-73.297, 60.03676, -12.20855, 83.64513)),
"GT": (
"Guatemala",
(-92.2292486234, 13.7353376327, -88.2250227526, 17.8193260767),
),
"GY": (
"Guyana",
(-61.4103029039, 1.26808828369, -56.5393857489, 8.36703481692),
),
"HN": (
"Honduras",
(-89.3533259753, 12.9846857772, -83.147219001, 16.0054057886),
),
"HR": (
"Croatia",
(13.6569755388, 42.47999136, 19.3904757016, 46.5037509222),
),
"HT": (
"Haiti",
(-74.4580336168, 18.0309927434, -71.6248732164, 19.9156839055),
),
"HU": (
"Hungary",
(16.2022982113, 45.7594811061, 22.710531447, 48.6238540716),
),
"ID": (
"Indonesia",
(95.2930261576, -10.3599874813, 141.03385176, 5.47982086834),
),
"IN": (
"India",
(68.1766451354, 7.96553477623, 97.4025614766, 35.4940095078),
),
"IE": (
"Ireland",
(-9.97708574059, 51.6693012559, -6.03298539878, 55.1316222195),
),
"IR": (
"Iran",
(44.1092252948, 25.0782370061, 63.3166317076, 39.7130026312),
),
"IQ": (
"Iraq",
(38.7923405291, 29.0990251735, 48.5679712258, 37.3852635768),
),
"IS": (
"Iceland",
(-24.3261840479, 63.4963829617, -13.609732225, 66.5267923041),
),
"IL": (
"Israel",
(34.2654333839, 29.5013261988, 35.8363969256, 33.2774264593),
),
"IT": ("Italy", (6.7499552751, 36.619987291, 18.4802470232, 47.1153931748)),
"JM": (
"Jamaica",
(-78.3377192858, 17.7011162379, -76.1996585761, 18.5242184514),
),
"JO": (
"Jordan",
(34.9226025734, 29.1974946152, 39.1954683774, 33.3786864284),
),
"JP": (
"Japan",
(129.408463169, 31.0295791692, 145.543137242, 45.5514834662),
),
"KZ": (
"Kazakhstan",
(46.4664457538, 40.6623245306, 87.3599703308, 55.3852501491),
),
"KE": ("Kenya", (33.8935689697, -4.67677, 41.8550830926, 5.506)),
"KG": (
"Kyrgyzstan",
(69.464886916, 39.2794632025, 80.2599902689, 43.2983393418),
),
"KH": (
"Cambodia",
(102.3480994, 10.4865436874, 107.614547968, 14.5705838078),
),
"KR": (
"S. Korea",
(126.117397903, 34.3900458847, 129.468304478, 38.6122429469),
),
"KW": (
"Kuwait",
(46.5687134133, 28.5260627304, 48.4160941913, 30.0590699326),
),
"LA": ("Laos", (100.115987583, 13.88109101, 107.564525181, 22.4647531194)),
"LB": (
"Lebanon",
(35.1260526873, 33.0890400254, 36.6117501157, 34.6449140488),
),
"LR": (
"Liberia",
(-11.4387794662, 4.35575511313, -7.53971513511, 8.54105520267),
),
"LY": ("Libya", (9.31941084152, 19.58047, 25.16482, 33.1369957545)),
"LK": (
"Sri Lanka",
(79.6951668639, 5.96836985923, 81.7879590189, 9.82407766361),
),
"LS": (
"Lesotho",
(26.9992619158, -30.6451058896, 29.3251664568, -28.6475017229),
),
"LT": (
"Lithuania",
(21.0558004086, 53.9057022162, 26.5882792498, 56.3725283881),
),
"LU": (
"Luxembourg",
(5.67405195478, 49.4426671413, 6.24275109216, 50.1280516628),
),
"LV": (
"Latvia",
(21.0558004086, 55.61510692, 28.1767094256, 57.9701569688),
),
"MA": (
"Morocco",
(-17.0204284327, 21.4207341578, -1.12455115397, 35.7599881048),
),
"MD": (
"Moldova",
(26.6193367856, 45.4882831895, 30.0246586443, 48.4671194525),
),
"MG": (
"Madagascar",
(43.2541870461, -25.6014344215, 50.4765368996, -12.0405567359),
),
"MX": ("Mexico", (-117.12776, 14.5388286402, -86.811982388, 32.72083)),
"MK": (
"Macedonia",
(20.46315, 40.8427269557, 22.9523771502, 42.3202595078),
),
"ML": (
"Mali",
(-12.1707502914, 10.0963607854, 4.27020999514, 24.9745740829),
),
"MM": (
"Myanmar",
(92.3032344909, 9.93295990645, 101.180005324, 28.335945136),
),
"ME": ("Montenegro", (18.45, 41.87755, 20.3398, 43.52384)),
"MN": (
"Mongolia",
(87.7512642761, 41.5974095729, 119.772823928, 52.0473660345),
),
"MZ": (
"Mozambique",
(30.1794812355, -26.7421916643, 40.7754752948, -10.3170960425),
),
"MR": (
"Mauritania",
(-17.0634232243, 14.6168342147, -4.92333736817, 27.3957441269),
),
"MW": (
"Malawi",
(32.6881653175, -16.8012997372, 35.7719047381, -9.23059905359),
),
"MY": (
"Malaysia",
(100.085756871, 0.773131415201, 119.181903925, 6.92805288332),
),
"NA": (
"Namibia",
(11.7341988461, -29.045461928, 25.0844433937, -16.9413428687),
),
"NC": (
"New Caledonia",
(164.029605748, -22.3999760881, 167.120011428, -20.1056458473),
),
"NE": (
"Niger",
(0.295646396495, 11.6601671412, 15.9032466977, 23.4716684026),
),
"NG": (
"Nigeria",
(2.69170169436, 4.24059418377, 14.5771777686, 13.8659239771),
),
"NI": (
"Nicaragua",
(-87.6684934151, 10.7268390975, -83.147219001, 15.0162671981),
),
"NL": (
"Netherlands",
(3.31497114423, 50.803721015, 7.09205325687, 53.5104033474),
),
"NO": (
"Norway",
(4.99207807783, 58.0788841824, 31.29341841, 80.6571442736),
),
"NP": (
"Nepal",
(80.0884245137, 26.3978980576, 88.1748043151, 30.4227169866),
),
"NZ": (
"New Zealand",
(166.509144322, -46.641235447, 178.517093541, -34.4506617165),
),
"OM": ("Oman", (52.0000098, 16.6510511337, 59.8080603372, 26.3959343531)),
"PK": (
"Pakistan",
(60.8742484882, 23.6919650335, 77.8374507995, 37.1330309108),
),
"PA": (
"Panama",
(-82.9657830472, 7.2205414901, -77.2425664944, 9.61161001224),
),
"PE": (
"Peru",
(-81.4109425524, -18.3479753557, -68.6650797187, -0.0572054988649),
),
"PH": (
"Philippines",
(117.17427453, 5.58100332277, 126.537423944, 18.5052273625),
),
"PG": (
"Papua New Guinea",
(141.000210403, -10.6524760881, 156.019965448, -2.50000212973),
),
"PL": (
"Poland",
(14.0745211117, 49.0273953314, 24.0299857927, 54.8515359564),
),
"PR": (
"Puerto Rico",
(-67.2424275377, 17.946553453, -65.5910037909, 18.5206011011),
),
"KP": (
"N. Korea",
(124.265624628, 37.669070543, 130.780007359, 42.9853868678),
),
"PT": (
"Portugal",
(-9.52657060387, 36.838268541, -6.3890876937, 42.280468655),
),
"PY": (
"Paraguay",
(-62.6850571357, -27.5484990374, -54.2929595608, -19.3427466773),
),
"QA": (
"Qatar",
(50.7439107603, 24.5563308782, 51.6067004738, 26.1145820175),
),
"RO": (
"Romania",
(20.2201924985, 43.6884447292, 29.62654341, 48.2208812526),
),
"RU": ("Russia", (-180.0, 41.151416124, 180.0, 81.2504)),
"RW": (
"Rwanda",
(29.0249263852, -2.91785776125, 30.8161348813, -1.13465911215),
),
"SA": (
"Saudi Arabia",
(34.6323360532, 16.3478913436, 55.6666593769, 32.161008816),
),
"SD": ("Sudan", (21.93681, 8.61972971293, 38.4100899595, 22.0)),
"SS": ("S. Sudan", (23.8869795809, 3.50917, 35.2980071182, 12.2480077571)),
"SN": (
"Senegal",
(-17.6250426905, 12.332089952, -11.4678991358, 16.5982636581),
),
"SB": (
"Solomon Is.",
(156.491357864, -10.8263672828, 162.398645868, -6.59933847415),
),
"SL": (
"Sierra Leone",
(-13.2465502588, 6.78591685631, -10.2300935531, 10.0469839543),
),
"SV": (
"El Salvador",
(-90.0955545723, 13.1490168319, -87.7235029772, 14.4241327987),
),
"SO": ("Somalia", (40.98105, -1.68325, 51.13387, 12.02464)),
"RS": ("Serbia", (18.82982, 42.2452243971, 22.9860185076, 46.1717298447)),
"SR": (
"Suriname",
(-58.0446943834, 1.81766714112, -53.9580446031, 6.0252914494),
),
"SK": (
"Slovakia",
(16.8799829444, 47.7584288601, 22.5581376482, 49.5715740017),
),
"SI": (
"Slovenia",
(13.6981099789, 45.4523163926, 16.5648083839, 46.8523859727),
),
"SE": (
"Sweden",
(11.0273686052, 55.3617373725, 23.9033785336, 69.1062472602),
),
"SZ": (
"Swaziland",
(30.6766085141, -27.2858794085, 32.0716654803, -25.660190525),
),
"SY": (
"Syria",
(35.7007979673, 32.312937527, 42.3495910988, 37.2298725449),
),
"TD": ("Chad", (13.5403935076, 7.42192454674, 23.88689, 23.40972)),
"TG": (
"Togo",
(-0.0497847151599, 5.92883738853, 1.86524051271, 11.0186817489),
),
"TH": (
"Thailand",
(97.3758964376, 5.69138418215, 105.589038527, 20.4178496363),
),
"TJ": (
"Tajikistan",
(67.4422196796, 36.7381712916, 74.9800024759, 40.9602133245),
),
"TM": (
"Turkmenistan",
(52.5024597512, 35.2706639674, 66.5461503437, 42.7515510117),
),
"TL": (
"East Timor",
(124.968682489, -9.39317310958, 127.335928176, -8.27334482181),
),
"TT": ("Trinidad and Tobago", (-61.95, 10.0, -60.895, 10.89)),
"TN": (
"Tunisia",
(7.52448164229, 30.3075560572, 11.4887874691, 37.3499944118),
),
"TR": (
"Turkey",
(26.0433512713, 35.8215347357, 44.7939896991, 42.1414848903),
),
"TW": (
"Taiwan",
(120.106188593, 21.9705713974, 121.951243931, 25.2954588893),
),
"TZ": ("Tanzania", (29.3399975929, -11.7209380022, 40.31659, -0.95)),
"UG": ("Uganda", (29.5794661801, -1.44332244223, 35.03599, 4.24988494736)),
"UA": (
"Ukraine",
(22.0856083513, 44.3614785833, 40.0807890155, 52.3350745713),
),
"UY": (
"Uruguay",
(-58.4270741441, -34.9526465797, -53.209588996, -30.1096863746),
),
"US": (
"United States",
(-171.791110603, 18.91619, -66.96466, 71.3577635769),
),
"UZ": (
"Uzbekistan",
(55.9289172707, 37.1449940049, 73.055417108, 45.5868043076),
),
"VE": (
"Venezuela",
(-73.3049515449, 0.724452215982, -59.7582848782, 12.1623070337),
),
"VN": (
"Vietnam",
(102.170435826, 8.59975962975, 109.33526981, 23.3520633001),
),
"VU": (
"Vanuatu",
(166.629136998, -16.5978496233, 167.844876744, -14.6264970842),
),
"PS": (
"West Bank",
(34.9274084816, 31.3534353704, 35.5456653175, 32.5325106878),
),
"WO": (
"World",
(180, 90, -180, -90),
),
"YE": (
"Yemen",
(42.6048726743, 12.5859504257, 53.1085726255, 19.0000033635),
),
"ZA": (
"South Africa",
(16.3449768409, -34.8191663551, 32.830120477, -22.0913127581),
),
"ZM": (
"Zambia",
(21.887842645, -17.9612289364, 33.4856876971, -8.23825652429),
),
"ZW": (
"Zimbabwe",
(25.2642257016, -22.2716118303, 32.8498608742, -15.5077869605),
),
}
)
| open_geo_engine/config/model_settings.py | 25,430 | NAME = "Iraqi Kurdistan" ADMIN_LEVEL = 3 | 40 | en | 0.770703 |
"""
Django settings for profiles project.
Generated by 'django-admin startproject' using Django 3.0.7.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '@k^mj7^tf1a_e1(8r54))u!7#m=bsztd)^j(++le6$xa*%%!q+'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework.authtoken',
'profiles_api'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'profiles.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'profiles.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.0/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/3.0/howto/static-files/
STATIC_URL = '/static/'
AUTH_USER_MODEL = 'profiles_api.UserProfile' | profiles/settings.py | 3,213 | Django settings for profiles project.
Generated by 'django-admin startproject' using Django 3.0.7.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
Build paths inside the project like this: os.path.join(BASE_DIR, ...) Quick-start development settings - unsuitable for production See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/ SECURITY WARNING: keep the secret key used in production secret! SECURITY WARNING: don't run with debug turned on in production! Application definition Database https://docs.djangoproject.com/en/3.0/ref/settings/databases Password validation https://docs.djangoproject.com/en/3.0/ref/settings/auth-password-validators Internationalization https://docs.djangoproject.com/en/3.0/topics/i18n/ Static files (CSS, JavaScript, Images) https://docs.djangoproject.com/en/3.0/howto/static-files/ | 989 | en | 0.673419 |
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
# Though the following import is not directly being used, it is required
# for 3D projection to work
from mpl_toolkits.mplot3d import Axes3D
from sklearn.cluster import KMeans
from sklearn import datasets
np.random.seed(5)
iris = datasets.load_iris()
X = [12, 20, 28, 18, 29, 33, 24, 45, 45, 52, 51, 52, 55, 53, 55, 61, 64, 69, 72]
y = [39, 36, 30, 52, 54, 46, 55, 59, 63, 70, 66, 63, 58, 23, 14, 8, 19, 7, 24]
estimators = [('k_means_iris_8', KMeans(n_clusters=8)),
('k_means_iris_3', KMeans(n_clusters=3)),
('k_means_iris_bad_init', KMeans(n_clusters=3, n_init=1,
init='random'))]
fignum = 1
titles = ['8 clusters', '3 clusters', '3 clusters, bad initialization']
for name, est in estimators:
fig = plt.figure(fignum, figsize=(4, 3))
ax = Axes3D(fig, rect=[0, 0, .95, 1], elev=48, azim=134)
est.fit(X)
labels = est.labels_
ax.scatter(X[:, 3], X[:, 0], X[:, 2],
c=labels.astype(np.float), edgecolor='k')
ax.w_xaxis.set_ticklabels([])
ax.w_yaxis.set_ticklabels([])
ax.w_zaxis.set_ticklabels([])
ax.set_xlabel('Petal width')
ax.set_ylabel('Sepal length')
ax.set_zlabel('Petal length')
ax.set_title(titles[fignum - 1])
ax.dist = 12
fignum = fignum + 1
# Plot the ground truth
fig = plt.figure(fignum, figsize=(4, 3))
ax = Axes3D(fig, rect=[0, 0, .95, 1], elev=48, azim=134)
for name, label in [('Setosa', 0),
('Versicolour', 1),
('Virginica', 2)]:
ax.text3D(X[y == label, 3].mean(),
X[y == label, 0].mean(),
X[y == label, 2].mean() + 2, name,
horizontalalignment='center',
bbox=dict(alpha=.2, edgecolor='w', facecolor='w'))
# Reorder the labels to have colors matching the cluster results
y = np.choose(y, [1, 2, 0]).astype(np.float)
ax.scatter(X[:, 3], X[:, 0], X[:, 2], c=y, edgecolor='k')
ax.w_xaxis.set_ticklabels([])
ax.w_yaxis.set_ticklabels([])
ax.w_zaxis.set_ticklabels([])
ax.set_xlabel('Petal width')
ax.set_ylabel('Sepal length')
ax.set_zlabel('Petal length')
ax.set_title('Ground Truth')
ax.dist = 12
fig.show() | k-means.py | 2,301 | Though the following import is not directly being used, it is required for 3D projection to work Plot the ground truth Reorder the labels to have colors matching the cluster results | 181 | en | 0.923802 |
# -*- coding: utf-8 -*-
import time
import mock
import pytest
import requests
try:
from urllib.parse import urljoin
except ImportError:
from urlparse import urljoin
from anacode import codes
from anacode.api import client
from anacode.api import writers
def empty_response(*args, **kwargs):
resp = requests.Response()
resp._content = b'{}'
resp.status_code = 200
return resp
def empty_json(*args, **kwargs):
return {}
@pytest.fixture
def auth():
return '1234567890'
@pytest.fixture
def auth_header(auth):
return {'Authorization': 'Token %s' % auth,
'Accept': 'application/json'}
@pytest.fixture
def api(auth):
return client.AnacodeClient(auth)
@mock.patch('requests.post', empty_response)
def test_scrape_call(api, auth_header, mocker):
mocker.spy(requests, 'post')
api.scrape('http://chinese.portal.com.ch')
assert requests.post.call_count == 1
requests.post.assert_called_once_with(
urljoin(client.ANACODE_API_URL, 'scrape/'),
headers=auth_header, json={'url': 'http://chinese.portal.com.ch'})
@mock.patch('requests.post', empty_response)
def test_categories_call(api, auth_header, mocker):
mocker.spy(requests, 'post')
api.analyze(['安全性能很好,很帅气。'], ['categories'])
assert requests.post.call_count == 1
json_data = {'texts': ['安全性能很好,很帅气。'], 'analyses': ['categories']}
requests.post.assert_called_once_with(
urljoin(client.ANACODE_API_URL, 'analyze/'),
headers=auth_header, json=json_data)
@mock.patch('requests.post', empty_response)
def test_sentiment_call(api, auth_header, mocker):
mocker.spy(requests, 'post')
api.analyze(['安全性能很好,很帅气。'], ['sentiment'])
assert requests.post.call_count == 1
requests.post.assert_called_once_with(
urljoin(client.ANACODE_API_URL, 'analyze/'),
headers=auth_header, json={'texts': ['安全性能很好,很帅气。'],
'analyses': ['sentiment']})
@mock.patch('requests.post', empty_response)
def test_concepts_call(api, auth_header, mocker):
mocker.spy(requests, 'post')
api.analyze(['安全性能很好,很帅气。'], ['concepts'])
assert requests.post.call_count == 1
requests.post.assert_called_once_with(
urljoin(client.ANACODE_API_URL, 'analyze/'),
headers=auth_header, json={'texts': ['安全性能很好,很帅气。'],
'analyses': ['concepts']})
@mock.patch('requests.post', empty_response)
def test_absa_call(api, auth_header, mocker):
mocker.spy(requests, 'post')
api.analyze(['安全性能很好,很帅气。'], ['absa'])
assert requests.post.call_count == 1
requests.post.assert_called_once_with(
urljoin(client.ANACODE_API_URL, 'analyze/'),
headers=auth_header, json={'texts': ['安全性能很好,很帅气。'],
'analyses': ['absa']})
@pytest.mark.parametrize('code,call,args', [
(codes.SCRAPE, 'scrape', ['http://www.google.com/']),
(codes.ANALYZE, 'analyze', ['安全性能很好,很帅气。', ['categories']]),
])
def test_proper_method_call(api, code, call, args, mocker):
mock.patch('anacode.api.client.AnacodeClient.' + call, empty_json)
mocker.spy(api, call)
api.call((code, *args))
getattr(api, call).assert_called_once_with(*args)
@pytest.mark.parametrize('call,args', [
('scrape', ['http://www.google.com/']),
('analyze', [['安全性能很好,很帅气。'], ['categories', 'concepts']]),
])
@pytest.mark.parametrize('count,call_count', [
(0, 0), (5, 0), (9, 0), (10, 1), (11, 1), (19, 1), (20, 2),
])
def test_should_start_analysis(api, mocker, call, args, count, call_count):
writer = writers.DataFrameWriter()
writer.init()
to_mock = 'anacode.api.client.AnacodeClient.' + call
mock.patch(to_mock, empty_json)
analyzer = client.Analyzer(api, writer, bulk_size=10)
mocker.spy(analyzer, 'execute_tasks_and_store_output')
for _ in range(count):
getattr(analyzer, call)(*args)
assert analyzer.execute_tasks_and_store_output.call_count == call_count
@pytest.mark.parametrize('call, args', [
('scrape', ([], )),
('analyze', ([], ['categories'])),
('analyze', ([], ['concepts'])),
('analyze', ([], ['sentiment'])),
('analyze', ([], ['absa'])),
])
def test_analysis_execution(api, mocker, call, args):
text = ['安全性能很好,很帅气。']
writer = writers.DataFrameWriter()
writer.init()
to_mock = 'anacode.api.client.AnacodeClient.' + call
mock.patch(to_mock, empty_json)
mocker.spy(api, call)
analyzer = client.Analyzer(api, writer, bulk_size=10)
for _ in range(4):
getattr(analyzer, call)(*args)
analyzer.execute_tasks_and_store_output()
assert getattr(api, call).call_count == 4
def time_consuming(*args, **kwargs):
time.sleep(0.1)
return {}
@mock.patch('anacode.api.client.AnacodeClient.analyze', time_consuming)
def test_parallel_queries(api, mocker):
text = ['安全性能很好,很帅气。']
writer = writers.DataFrameWriter()
writer.init()
mocker.spy(api, 'analyze')
analyzer = client.Analyzer(api, writer, threads=4, bulk_size=4)
start = time.time()
with analyzer:
for _ in range(4):
analyzer.analyze(text, ['categories'])
stop = time.time()
duration = stop - start
assert abs(duration - 0.1) < 0.1
| tests/test_api_client.py | 5,532 | -*- coding: utf-8 -*- | 21 | en | 0.767281 |
# coding: utf-8
# AUTOGENERATED BY gen_script.sh from kp1.py
# Copyright (C) Nyimbi Odero, Thu Aug 3 20:34:20 EAT 2017
import calendar
from flask import redirect, flash, url_for, Markup
from flask import render_template
from flask_appbuilder.models.sqla.interface import SQLAInterface
from flask_appbuilder.views import ModelView, BaseView, MasterDetailView, MultipleView, RestCRUDView, CompactCRUDMixin
from flask_appbuilder import ModelView, CompactCRUDMixin, aggregate_count, action, expose, BaseView, has_access
from flask_appbuilder.charts.views import ChartView, TimeChartView, GroupByChartView
from flask_appbuilder.models.group import aggregate_count
from flask_appbuilder.widgets import ListThumbnail, ListWidget
from flask_appbuilder.widgets import FormVerticalWidget, FormInlineWidget, FormHorizontalWidget, ShowBlockWidget
from flask_appbuilder.models.sqla.filters import FilterStartsWith, FilterEqualFunction as FA
from app import appbuilder, db
from .models import *
# Basic Lists
hide_list = ['created_by', 'changed_by', 'created_on', 'changed_on']
#To pretty Print from PersonMixin
def pretty_month_year(value):
return calendar.month_name[value.month] + ' ' + str(value.year)
def pretty_year(value):
return str(value.year)
def fill_gender():
try:
db.session.add(Gender(name='Male'))
db.session.add(Gender(name='Female'))
db.session.commit()
except:
db.session.rollback()
class LawyerView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Lawyer, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class PolicemanView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Policeman, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class BailView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Bail, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class CaseView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Case, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class CasecategoryView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Casecategory, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class CauseofactionView(CompactCRUDMixin, ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Causeofaction, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class CommitaltypeView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Commitaltype, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class ConstituencyView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Constituency, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class CountyView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(County, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class CourtView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Court, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class CourtlevelView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Courtlevel, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class CourtstationView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Courtstation, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class DefendantView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Defendant, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class DoctemplateView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Doctemplate, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class DocumentView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Document, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class FilingView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Filing, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class FilingtypeView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Filingtype, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class GenderView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Gender, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class HearingView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Hearing, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class HearingtypeView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Hearingtype, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class InvestigationView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Investigation, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class JoRankView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(JoRank, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class JudicialofficerView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Judicialofficer, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class LawfirmView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Lawfirm, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class NatureofsuitView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Natureofsuit, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class PaymentView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Payment, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class PaymentmethodView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Paymentmethod, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class PlaintiffView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Plaintiff, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class PolicerankView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Policerank, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class PoliceroleView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Policerole, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class PolicestationView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Policestation, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class PolicestationtypeView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Policestationtype, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class PrisonView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Prison, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class PrisoncommitalView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Prisoncommital, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class ProsecutorView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Prosecutor, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class ProsecutorteamView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Prosecutorteam, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class SubcountyView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Subcounty, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class SuretyView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Surety, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class TownView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Town, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class WitnesView(ModelView):#MasterDetailView, MultipleView
datamodel=SQLAInterface(Witnes, db.session)
#add_title =
#list_title =
#edit_title =
#show_title =
#add_widget = (FormVerticalWidget|FormInlineWidget)
#show_widget = ShowBlockWidget
#list_widget = (ListThumbnail|ListWidget)
#base_order = ("name", "asc")
search_exclude_columns = person_exclude_columns + biometric_columns + person_search_exclude_columns
add_exclude_columns = edit_exclude_columns = audit_exclude_columns
#label_columns = {"contact_group":"Contacts Group"}
#add_columns = person_list_columns + ref_columns + contact_columns
#edit_columns = person_list_columns + ref_columns + contact_columns
#list_columns = person_list_columns + ref_columns + contact_columns
#list_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)
#related_views =[]
#show_fieldsets = person_show_fieldset + contact_fieldset
#edit_fieldsets = add_fieldsets = \
# ref_fieldset + person_fieldset + contact_fieldset #+ activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldset
#description_columns = {"name":"your models name column","address":"the address column"}
#show_template = "appbuilder/general/model/show_cascade.html"
#edit_template = "appbuilder/general/model/edit_cascade.html"
@action("muldelete", "Delete", Markup("<p>Delete all Really?</p><p>Ok then...</p>"), "fa-rocket")
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())
class AttorneyChartView(GroupByChartView):
datamodel = SQLAInterface(Attorney , db.session)
chart_title = 'Grouped Attorney by Birth'
label_columns = AttorneyView.label_columns
chart_type = 'PieChart'
definitions = [
{
'group' : 'age_today',
"series" : [(aggregate_count,"age_today")]
},
{
'group' : 'gender',
"series" : [(aggregate_count,"age_today")]
}
]
class AttorneyTimeChartView(GroupByChartView):
datamodel = SQLAInterface(Attorney , db.session)
chart_title = 'Grouped Birth Attorney'
chart_type = 'AreaChart'
label_columns = AttorneyView.label_columns
definitions = [
{
'group' : 'age_today',
'formatter': pretty_month_year,
"series" : [(aggregate_count,"age_today")]
},
{
'group': 'age_today',
'formatter': pretty_year,
"series" : [(aggregate_count,"age_today")]
}
]
class PlaintiffChartView(GroupByChartView):
datamodel = SQLAInterface(Plaintiff , db.session)
chart_title = 'Grouped Plaintiff by Birth'
label_columns = PlaintiffView.label_columns
chart_type = 'PieChart'
definitions = [
{
'group' : 'age_today',
"series" : [(aggregate_count,"age_today")]
},
{
'group' : 'gender',
"series" : [(aggregate_count,"age_today")]
}
]
class PlaintiffTimeChartView(GroupByChartView):
datamodel = SQLAInterface(Plaintiff , db.session)
chart_title = 'Grouped Birth Plaintiff'
chart_type = 'AreaChart'
label_columns = PlaintiffView.label_columns
definitions = [
{
'group' : 'age_today',
'formatter': pretty_month_year,
"series" : [(aggregate_count,"age_today")]
},
{
'group': 'age_today',
'formatter': pretty_year,
"series" : [(aggregate_count,"age_today")]
}
]
class WitnessChartView(GroupByChartView):
datamodel = SQLAInterface(Witness , db.session)
chart_title = 'Grouped Witness by Birth'
label_columns = WitnessView.label_columns
chart_type = 'PieChart'
definitions = [
{
'group' : 'age_today',
"series" : [(aggregate_count,"age_today")]
},
{
'group' : 'gender',
"series" : [(aggregate_count,"age_today")]
}
]
class WitnessTimeChartView(GroupByChartView):
datamodel = SQLAInterface(Witness , db.session)
chart_title = 'Grouped Birth Witness'
chart_type = 'AreaChart'
label_columns = WitnessView.label_columns
definitions = [
{
'group' : 'age_today',
'formatter': pretty_month_year,
"series" : [(aggregate_count,"age_today")]
},
{
'group': 'age_today',
'formatter': pretty_year,
"series" : [(aggregate_count,"age_today")]
}
]
class SuretyChartView(GroupByChartView):
datamodel = SQLAInterface(Surety , db.session)
chart_title = 'Grouped Surety by Birth'
label_columns = SuretyView.label_columns
chart_type = 'PieChart'
definitions = [
{
'group' : 'age_today',
"series" : [(aggregate_count,"age_today")]
},
{
'group' : 'gender',
"series" : [(aggregate_count,"age_today")]
}
]
class SuretyTimeChartView(GroupByChartView):
datamodel = SQLAInterface(Surety , db.session)
chart_title = 'Grouped Birth Surety'
chart_type = 'AreaChart'
label_columns = SuretyView.label_columns
definitions = [
{
'group' : 'age_today',
'formatter': pretty_month_year,
"series" : [(aggregate_count,"age_today")]
},
{
'group': 'age_today',
'formatter': pretty_year,
"series" : [(aggregate_count,"age_today")]
}
]
class ProsecutorChartView(GroupByChartView):
datamodel = SQLAInterface(Prosecutor , db.session)
chart_title = 'Grouped Prosecutor by Birth'
label_columns = ProsecutorView.label_columns
chart_type = 'PieChart'
definitions = [
{
'group' : 'age_today',
"series" : [(aggregate_count,"age_today")]
},
{
'group' : 'gender',
"series" : [(aggregate_count,"age_today")]
}
]
class ProsecutorTimeChartView(GroupByChartView):
datamodel = SQLAInterface(Prosecutor , db.session)
chart_title = 'Grouped Birth Prosecutor'
chart_type = 'AreaChart'
label_columns = ProsecutorView.label_columns
definitions = [
{
'group' : 'age_today',
'formatter': pretty_month_year,
"series" : [(aggregate_count,"age_today")]
},
{
'group': 'age_today',
'formatter': pretty_year,
"series" : [(aggregate_count,"age_today")]
}
]
class PoliceofficerChartView(GroupByChartView):
datamodel = SQLAInterface(Policeofficer , db.session)
chart_title = 'Grouped Policeofficer by Birth'
label_columns = PoliceofficerView.label_columns
chart_type = 'PieChart'
definitions = [
{
'group' : 'age_today',
"series" : [(aggregate_count,"age_today")]
},
{
'group' : 'gender',
"series" : [(aggregate_count,"age_today")]
}
]
class PoliceofficerTimeChartView(GroupByChartView):
datamodel = SQLAInterface(Policeofficer , db.session)
chart_title = 'Grouped Birth Policeofficer'
chart_type = 'AreaChart'
label_columns = PoliceofficerView.label_columns
definitions = [
{
'group' : 'age_today',
'formatter': pretty_month_year,
"series" : [(aggregate_count,"age_today")]
},
{
'group': 'age_today',
'formatter': pretty_year,
"series" : [(aggregate_count,"age_today")]
}
]
class JudicialofficerChartView(GroupByChartView):
datamodel = SQLAInterface(Judicialofficer , db.session)
chart_title = 'Grouped Judicialofficer by Birth'
label_columns = JudicialofficerView.label_columns
chart_type = 'PieChart'
definitions = [
{
'group' : 'age_today',
"series" : [(aggregate_count,"age_today")]
},
{
'group' : 'gender',
"series" : [(aggregate_count,"age_today")]
}
]
class JudicialofficerTimeChartView(GroupByChartView):
datamodel = SQLAInterface(Judicialofficer , db.session)
chart_title = 'Grouped Birth Judicialofficer'
chart_type = 'AreaChart'
label_columns = JudicialofficerView.label_columns
definitions = [
{
'group' : 'age_today',
'formatter': pretty_month_year,
"series" : [(aggregate_count,"age_today")]
},
{
'group': 'age_today',
'formatter': pretty_year,
"series" : [(aggregate_count,"age_today")]
}
]
class DefendantChartView(GroupByChartView):
datamodel = SQLAInterface(Defendant , db.session)
chart_title = 'Grouped Defendant by Birth'
label_columns = DefendantView.label_columns
chart_type = 'PieChart'
definitions = [
{
'group' : 'age_today',
"series" : [(aggregate_count,"age_today")]
},
{
'group' : 'gender',
"series" : [(aggregate_count,"age_today")]
}
]
class DefendantTimeChartView(GroupByChartView):
datamodel = SQLAInterface(Defendant , db.session)
chart_title = 'Grouped Birth Defendant'
chart_type = 'AreaChart'
label_columns = DefendantView.label_columns
definitions = [
{
'group' : 'age_today',
'formatter': pretty_month_year,
"series" : [(aggregate_count,"age_today")]
},
{
'group': 'age_today',
'formatter': pretty_year,
"series" : [(aggregate_count,"age_today")]
}
]
# How to create a MasterDetailView
#class DetailView(ModelView):
# datamodel = SQLAInterface(DetailTable, db.session)
#class MasterView(MasterDetailView):
# datamodel = SQLAInterface(MasterTable, db.session)
# related_views = [DetailView]
# How to create a MultipleView
#class MultipleViewsExp(MultipleView):
# views = [GroupModelView, ContactModelView]
#View Registration
db.create_all()
fill_gender()
appbuilder.add_view(LawyerView(), "Lawyers", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(PolicemanView(), "Policemans", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(BailView(), "Bails", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(CaseView(), "Cases", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(CasecategoryView(), "Casecategorys", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(CauseofactionView(), "Causeofactions", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(CommitaltypeView(), "Commitaltypes", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(ConstituencyView(), "Constituencys", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(CountyView(), "Countys", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(CourtView(), "Courts", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(CourtlevelView(), "Courtlevels", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(CourtstationView(), "Courtstations", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(DefendantView(), "Defendants", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(DoctemplateView(), "Doctemplates", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(DocumentView(), "Documents", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(FilingView(), "Filings", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(FilingtypeView(), "Filingtypes", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(GenderView(), "Genders", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(HearingView(), "Hearings", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(HearingtypeView(), "Hearingtypes", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(InvestigationView(), "Investigations", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(JoRankView(), "JoRanks", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(JudicialofficerView(), "Judicialofficers", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(LawfirmView(), "Lawfirms", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(NatureofsuitView(), "Natureofsuits", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(PaymentView(), "Payments", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(PaymentmethodView(), "Paymentmethods", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(PlaintiffView(), "Plaintiffs", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(PolicerankView(), "Policeranks", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(PoliceroleView(), "Policeroles", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(PolicestationView(), "Policestations", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(PolicestationtypeView(), "Policestationtypes", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(PrisonView(), "Prisons", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(PrisoncommitalView(), "Prisoncommitals", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(ProsecutorView(), "Prosecutors", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(ProsecutorteamView(), "Prosecutorteams", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(SubcountyView(), "Subcountys", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(SuretyView(), "Suretys", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(TownView(), "Towns", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(WitnesView(), "Witness", icon="fa-folder-open-o", category="Setup")
appbuilder.add_view(AttorneyChartView(), 'Attorney Age Chart', icon='fa-dashboard', category='Reports')
appbuilder.add_view(AttorneyTimeChartView(), 'Attorney Time Chart', icon='fa-dashboard', category='Reports')
appbuilder.add_view(PlaintiffChartView(), 'Plaintiff Age Chart', icon='fa-dashboard', category='Reports')
appbuilder.add_view(PlaintiffTimeChartView(), 'Plaintiff Time Chart', icon='fa-dashboard', category='Reports')
appbuilder.add_view(WitnessChartView(), 'Witness Age Chart', icon='fa-dashboard', category='Reports')
appbuilder.add_view(WitnessTimeChartView(), 'Witness Time Chart', icon='fa-dashboard', category='Reports')
appbuilder.add_view(SuretyChartView(), 'Surety Age Chart', icon='fa-dashboard', category='Reports')
appbuilder.add_view(SuretyTimeChartView(), 'Surety Time Chart', icon='fa-dashboard', category='Reports')
appbuilder.add_view(ProsecutorChartView(), 'Prosecutor Age Chart', icon='fa-dashboard', category='Reports')
appbuilder.add_view(ProsecutorTimeChartView(), 'Prosecutor Time Chart', icon='fa-dashboard', category='Reports')
appbuilder.add_view(PoliceofficerChartView(), 'Policeofficer Age Chart', icon='fa-dashboard', category='Reports')
appbuilder.add_view(PoliceofficerTimeChartView(), 'Policeofficer Time Chart', icon='fa-dashboard', category='Reports')
appbuilder.add_view(JudicialofficerChartView(), 'Judicialofficer Age Chart', icon='fa-dashboard', category='Reports')
appbuilder.add_view(JudicialofficerTimeChartView(), 'Judicialofficer Time Chart', icon='fa-dashboard', category='Reports')
appbuilder.add_view(DefendantChartView(), 'Defendant Age Chart', icon='fa-dashboard', category='Reports')
appbuilder.add_view(DefendantTimeChartView(), 'Defendant Time Chart', icon='fa-dashboard', category='Reports')
#appbuilder.add_separator("Setup")
#appbuilder.add_separator("My Views")
#appbuilder.add_link(name, href, icon='', label='', category='', category_icon='', category_label='', baseview=None)
| zarc/views_2017-08-03-21:28:54.py | 81,025 | coding: utf-8 AUTOGENERATED BY gen_script.sh from kp1.py Copyright (C) Nyimbi Odero, Thu Aug 3 20:34:20 EAT 2017 Basic Lists To pretty Print from PersonMixin MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html"MasterDetailView, MultipleViewadd_title =list_title =edit_title =show_title =add_widget = (FormVerticalWidget|FormInlineWidget)show_widget = ShowBlockWidgetlist_widget = (ListThumbnail|ListWidget)base_order = ("name", "asc")label_columns = {"contact_group":"Contacts Group"} add_columns = person_list_columns + ref_columns + contact_columnsedit_columns = person_list_columns + ref_columns + contact_columnslist_columns = person_list_columns + ref_columns + contact_columnslist_widget = ListBlock|ListItem|ListThumbnail|ListWidget (default)related_views =[]show_fieldsets = person_show_fieldset + contact_fieldsetedit_fieldsets = add_fieldsets = \ ref_fieldset + person_fieldset + contact_fieldset + activity_fieldset + place_fieldset + biometric_fieldset + employment_fieldsetdescription_columns = {"name":"your models name column","address":"the address column"}show_template = "appbuilder/general/model/show_cascade.html"edit_template = "appbuilder/general/model/edit_cascade.html" How to create a MasterDetailViewclass DetailView(ModelView): datamodel = SQLAInterface(DetailTable, db.session)class MasterView(MasterDetailView): datamodel = SQLAInterface(MasterTable, db.session) related_views = [DetailView] How to create a MultipleViewclass MultipleViewsExp(MultipleView): views = [GroupModelView, ContactModelView]View Registrationappbuilder.add_separator("Setup")appbuilder.add_separator("My Views")appbuilder.add_link(name, href, icon='', label='', category='', category_icon='', category_label='', baseview=None) | 40,109 | en | 0.232496 |
import importlib.util
import logging
import re
import time
from collections import defaultdict
from inspect import getsource
from pathlib import Path
from types import ModuleType
from typing import Dict, List, Set, Type
import click
from flask_appbuilder import Model
from flask_migrate import downgrade, upgrade
from graphlib import TopologicalSorter # pylint: disable=wrong-import-order
from sqlalchemy import inspect
from rabbitai import db
from rabbitai.utils.mock_data import add_sample_rows
logger = logging.getLogger(__name__)
def import_migration_script(filepath: Path) -> ModuleType:
"""
像导入模块一样导入迁移脚本。
:param filepath: 文件路径对象。
:return:
"""
spec = importlib.util.spec_from_file_location(filepath.stem, filepath)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def extract_modified_tables(module: ModuleType) -> Set[str]:
"""
提取由迁移脚本修改的表。
此函数使用一种简单的方法来查看迁移脚本的源代码以查找模式。它可以通过实际遍历AST来改进。
"""
tables: Set[str] = set()
for function in {"upgrade", "downgrade"}:
source = getsource(getattr(module, function))
tables.update(re.findall(r'alter_table\(\s*"(\w+?)"\s*\)', source, re.DOTALL))
tables.update(re.findall(r'add_column\(\s*"(\w+?)"\s*,', source, re.DOTALL))
tables.update(re.findall(r'drop_column\(\s*"(\w+?)"\s*,', source, re.DOTALL))
return tables
def find_models(module: ModuleType) -> List[Type[Model]]:
"""
在迁移脚本中查找所有模型。
:param module:
:return:
"""
models: List[Type[Model]] = []
tables = extract_modified_tables(module)
# 添加在迁移脚本中显式定义的模型
queue = list(module.__dict__.values())
while queue:
obj = queue.pop()
if hasattr(obj, "__tablename__"):
tables.add(obj.__tablename__)
elif isinstance(obj, list):
queue.extend(obj)
elif isinstance(obj, dict):
queue.extend(obj.values())
# 添加隐式模型
for obj in Model._decl_class_registry.values():
if hasattr(obj, "__table__") and obj.__table__.fullname in tables:
models.append(obj)
# 按拓扑排序,这样我们可以按顺序创建实体并维护关系(例如,在创建切片之前创建数据库)
sorter = TopologicalSorter()
for model in models:
inspector = inspect(model)
dependent_tables: List[str] = []
for column in inspector.columns.values():
for foreign_key in column.foreign_keys:
dependent_tables.append(foreign_key.target_fullname.split(".")[0])
sorter.add(model.__tablename__, *dependent_tables)
order = list(sorter.static_order())
models.sort(key=lambda model: order.index(model.__tablename__))
return models
@click.command()
@click.argument("filepath")
@click.option("--limit", default=1000, help="Maximum number of entities.")
@click.option("--force", is_flag=True, help="Do not prompt for confirmation.")
@click.option("--no-auto-cleanup", is_flag=True, help="Do not remove created models.")
def main(
filepath: str, limit: int = 1000, force: bool = False, no_auto_cleanup: bool = False
) -> None:
auto_cleanup = not no_auto_cleanup
session = db.session()
print(f"Importing migration script: {filepath}")
module = import_migration_script(Path(filepath))
revision: str = getattr(module, "revision", "")
down_revision: str = getattr(module, "down_revision", "")
if not revision or not down_revision:
raise Exception(
"Not a valid migration script, couldn't find down_revision/revision"
)
print(f"Migration goes from {down_revision} to {revision}")
current_revision = db.engine.execute(
"SELECT version_num FROM alembic_version"
).scalar()
print(f"Current version of the DB is {current_revision}")
print("\nIdentifying models used in the migration:")
models = find_models(module)
model_rows: Dict[Type[Model], int] = {}
for model in models:
rows = session.query(model).count()
print(f"- {model.__name__} ({rows} rows in table {model.__tablename__})")
model_rows[model] = rows
session.close()
if current_revision != down_revision:
if not force:
click.confirm(
"\nRunning benchmark will downgrade the Rabbitai DB to "
f"{down_revision} and upgrade to {revision} again. There may "
"be data loss in downgrades. Continue?",
abort=True,
)
downgrade(revision=down_revision)
print("Benchmarking migration")
results: Dict[str, float] = {}
start = time.time()
upgrade(revision=revision)
duration = time.time() - start
results["Current"] = duration
print(f"Migration on current DB took: {duration:.2f} seconds")
min_entities = 10
new_models: Dict[Type[Model], List[Model]] = defaultdict(list)
while min_entities <= limit:
downgrade(revision=down_revision)
print(f"Running with at least {min_entities} entities of each model")
for model in models:
missing = min_entities - model_rows[model]
if missing > 0:
print(f"- Adding {missing} entities to the {model.__name__} model")
try:
added_models = add_sample_rows(session, model, missing)
except Exception:
session.rollback()
raise
model_rows[model] = min_entities
session.commit()
if auto_cleanup:
new_models[model].extend(added_models)
start = time.time()
upgrade(revision=revision)
duration = time.time() - start
print(f"Migration for {min_entities}+ entities took: {duration:.2f} seconds")
results[f"{min_entities}+"] = duration
min_entities *= 10
if auto_cleanup:
print("Cleaning up DB")
# delete in reverse order of creation to handle relationships
for model, entities in list(new_models.items())[::-1]:
session.query(model).filter(
model.id.in_(entity.id for entity in entities)
).delete(synchronize_session=False)
session.commit()
if current_revision != revision and not force:
click.confirm(f"\nRevert DB to {revision}?", abort=True)
upgrade(revision=revision)
print("Reverted")
print("\nResults:\n")
for label, duration in results.items():
print(f"{label}: {duration:.2f} s")
if __name__ == "__main__":
from rabbitai.app import create_app
app = create_app()
with app.app_context():
main()
| scripts/benchmark_migration.py | 6,911 | 提取由迁移脚本修改的表。
此函数使用一种简单的方法来查看迁移脚本的源代码以查找模式。它可以通过实际遍历AST来改进。
在迁移脚本中查找所有模型。
:param module:
:return:
像导入模块一样导入迁移脚本。
:param filepath: 文件路径对象。
:return:
pylint: disable=wrong-import-order 添加在迁移脚本中显式定义的模型 添加隐式模型 按拓扑排序,这样我们可以按顺序创建实体并维护关系(例如,在创建切片之前创建数据库) delete in reverse order of creation to handle relationships | 310 | zh | 0.928709 |
# coding: utf-8
"""
Flat API
The Flat API allows you to easily extend the abilities of the [Flat Platform](https://flat.io), with a wide range of use cases including the following: * Creating and importing new music scores using MusicXML, MIDI, Guitar Pro (GP3, GP4, GP5, GPX, GP), PowerTab, TuxGuitar and MuseScore files * Browsing, updating, copying, exporting the user's scores (for example in MP3, WAV or MIDI) * Managing educational resources with Flat for Education: creating & updating the organization accounts, the classes, rosters and assignments. The Flat API is built on HTTP. Our API is RESTful It has predictable resource URLs. It returns HTTP response codes to indicate errors. It also accepts and returns JSON in the HTTP body. The [schema](/swagger.yaml) of this API follows the [OpenAPI Initiative (OAI) specification](https://www.openapis.org/), you can use and work with [compatible Swagger tools](http://swagger.io/open-source-integrations/). This API features Cross-Origin Resource Sharing (CORS) implemented in compliance with [W3C spec](https://www.w3.org/TR/cors/). You can use your favorite HTTP/REST library for your programming language to use Flat's API. This specification and reference is [available on Github](https://github.com/FlatIO/api-reference). Getting Started and learn more: * [API Overview and interoduction](https://flat.io/developers/docs/api/) * [Authentication (Personal Access Tokens or OAuth2)](https://flat.io/developers/docs/api/authentication.html) * [SDKs](https://flat.io/developers/docs/api/sdks.html) * [Rate Limits](https://flat.io/developers/docs/api/rate-limits.html) * [Changelog](https://flat.io/developers/docs/api/changelog.html) # noqa: E501
OpenAPI spec version: 2.7.0
Contact: developers@flat.io
Generated by: https://openapi-generator.tech
"""
from __future__ import absolute_import
import unittest
import flat_api
from flat_api.models.collection import Collection # noqa: E501
from flat_api.rest import ApiException
class TestCollection(unittest.TestCase):
"""Collection unit test stubs"""
def setUp(self):
pass
def tearDown(self):
pass
def testCollection(self):
"""Test Collection"""
# FIXME: construct object with mandatory attributes with example values
# model = flat_api.models.collection.Collection() # noqa: E501
pass
if __name__ == '__main__':
unittest.main()
| test/test_collection.py | 2,446 | Collection unit test stubs
Test Collection
Flat API
The Flat API allows you to easily extend the abilities of the [Flat Platform](https://flat.io), with a wide range of use cases including the following: * Creating and importing new music scores using MusicXML, MIDI, Guitar Pro (GP3, GP4, GP5, GPX, GP), PowerTab, TuxGuitar and MuseScore files * Browsing, updating, copying, exporting the user's scores (for example in MP3, WAV or MIDI) * Managing educational resources with Flat for Education: creating & updating the organization accounts, the classes, rosters and assignments. The Flat API is built on HTTP. Our API is RESTful It has predictable resource URLs. It returns HTTP response codes to indicate errors. It also accepts and returns JSON in the HTTP body. The [schema](/swagger.yaml) of this API follows the [OpenAPI Initiative (OAI) specification](https://www.openapis.org/), you can use and work with [compatible Swagger tools](http://swagger.io/open-source-integrations/). This API features Cross-Origin Resource Sharing (CORS) implemented in compliance with [W3C spec](https://www.w3.org/TR/cors/). You can use your favorite HTTP/REST library for your programming language to use Flat's API. This specification and reference is [available on Github](https://github.com/FlatIO/api-reference). Getting Started and learn more: * [API Overview and interoduction](https://flat.io/developers/docs/api/) * [Authentication (Personal Access Tokens or OAuth2)](https://flat.io/developers/docs/api/authentication.html) * [SDKs](https://flat.io/developers/docs/api/sdks.html) * [Rate Limits](https://flat.io/developers/docs/api/rate-limits.html) * [Changelog](https://flat.io/developers/docs/api/changelog.html) # noqa: E501
OpenAPI spec version: 2.7.0
Contact: developers@flat.io
Generated by: https://openapi-generator.tech
coding: utf-8 noqa: E501 FIXME: construct object with mandatory attributes with example values model = flat_api.models.collection.Collection() noqa: E501 | 1,995 | en | 0.720016 |
# Copyright (C) 2020 Red Hat, Inc.
#
# 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
from tooz import coordination
coordinator = coordination.get_coordinator('zake://', b'host-1')
coordinator.start()
# Create a group
group = bytes(str(uuid.uuid4()).encode('ascii'))
request = coordinator.create_group(group)
request.get()
def group_joined(event):
# Event is an instance of tooz.coordination.MemberJoinedGroup
print(event.group_id, event.member_id)
coordinator.watch_join_group(group, group_joined)
coordinator.stop()
| examples/group_membership_watch.py | 1,042 | Copyright (C) 2020 Red Hat, Inc. 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. Create a group Event is an instance of tooz.coordination.MemberJoinedGroup | 628 | en | 0.856543 |
# -*- coding: utf-8 -*-
# @Time: 2020/4/17 12:40
# @Author: GraceKoo
# @File: 241_different-ways-to-add-parentheses.py
# @Desc: https://leetcode-cn.com/problems/different-ways-to-add-parentheses/
from typing import List
class Solution:
def diffWaysToCompute(self, input: str) -> List[int]:
if input.isdigit():
return [int(input)]
res = []
for index, value in enumerate(input):
if value in ["+", "-", "*"]:
left = self.diffWaysToCompute(input[:index])
right = self.diffWaysToCompute(input[index + 1 :])
# 合并结果
for l in left:
for r in right:
if value == "+":
res.append(l + r)
elif value == "-":
res.append(l - r)
elif value == "*":
res.append(l * r)
return res
so = Solution()
print(so.diffWaysToCompute("2*3-4*5"))
| Codes/gracekoo/241_different-ways-to-add-parentheses.py | 1,027 | -*- coding: utf-8 -*- @Time: 2020/4/17 12:40 @Author: GraceKoo @File: 241_different-ways-to-add-parentheses.py @Desc: https://leetcode-cn.com/problems/different-ways-to-add-parentheses/ 合并结果 | 190 | en | 0.387455 |
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'projeto_curso_2.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
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?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()
| Python/Django/projeto_curso_2/manage.py | 671 | Run administrative tasks.
Django's command-line utility for administrative tasks.
!/usr/bin/env python | 103 | en | 0.725633 |
# Generated by Django 3.1.7 on 2021-03-07 11:27
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('app', '0005_auto_20210303_1338'),
]
operations = [
migrations.CreateModel(
name='House',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=20)),
('uniqueCode', models.CharField(max_length=10)),
('inhabitants', models.ManyToManyField(related_name='House', to=settings.AUTH_USER_MODEL)),
],
),
]
| app/migrations/0006_house.py | 669 | Generated by Django 3.1.7 on 2021-03-07 11:27 | 45 | en | 0.705565 |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import select
import socket
import queue
import time
import os
class emsc_select_server:
def __init__(self):
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.setblocking(False)
self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server_address = ('0.0.0.0', 5000)
self.server.bind(self.server_address)
self.server.listen(1000)
self.inputs = [self.server]
self.outputs = []
self.message_queues = {}
self.timeout = 20
def run(self):
response = "接收成功,返回数据: connecting status: 200 \n"
response += "haody,client ! | "
while self.inputs:
print("waiting for next event")
# timeout是超时,当前连接要是超过这个时间的话,就会kill
readable, writable, exceptional = select.select(self.inputs, self.outputs, self.inputs, self.timeout)
if not (readable or writable or exceptional):
print("Time out ! ")
break
for ser in readable:
if ser is self.server:
# 通过inputs查看是否有客户端来
connection, client_address = ser.accept()
print("connection from ", client_address)
connection.setblocking(0)
self.inputs.append(connection)
self.message_queues[connection] = queue.Queue()
else:
data = ser.recv(1024)
if data:
print("收到数据 ", data.decode(), "\n来自:", ser.getpeername())
self.message_queues[ser].put(data)
# 添加通道
if ser not in self.outputs:
self.outputs.append(ser)
else:
print("closing", client_address)
if ser in self.outputs:
self.outputs.remove(ser)
self.inputs.remove(ser)
ser.close()
# 清除队列信息
del self.message_queues[ser]
for ser in writable:
try:
next_msg = self.message_queues[ser].get_nowait()
except queue.Empty:
print(ser.getpeername(), 'queue empty')
self.outputs.remove(ser)
else:
print("发送数据 ", str(response + next_msg.decode()), " to ", ser.getpeername(),"\n")
ser.send(response.encode()+next_msg)
for ser in exceptional:
print(" exception condition on ", ser.getpeername())
# stop listening for input on the connection
self.inputs.remove(ser)
if ser in self.outputs:
self.outputs.remove(ser)
ser.close()
# 清除队列信息
del self.message_queues[ser]
if __name__=="__main__":
select_server = emsc_select_server()
select_server.run() | run/server_select.py | 3,253 | !/usr/bin/env python -*- coding: utf-8 -*- timeout是超时,当前连接要是超过这个时间的话,就会kill 通过inputs查看是否有客户端来 添加通道 清除队列信息 stop listening for input on the connection 清除队列信息 | 155 | zh | 0.662174 |
# File: __init__.py
#
# Copyright (c) 2018-2019 Splunk Inc.
#
# 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.
| __init__.py | 604 | File: __init__.py Copyright (c) 2018-2019 Splunk Inc. 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. | 575 | en | 0.844312 |
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import os
import pytest
from azure.communication.sms.aio import SmsClient
from azure.communication.sms import (
PhoneNumber, SendSmsOptions
)
from _shared.asynctestcase import AsyncCommunicationTestCase
from _shared.testcase import (
BodyReplacerProcessor, ResponseReplacerProcessor
)
class SMSClientTestAsync(AsyncCommunicationTestCase):
def __init__(self, method_name):
super(SMSClientTestAsync, self).__init__(method_name)
def setUp(self):
super(SMSClientTestAsync, self).setUp()
if self.is_playback():
self.phone_number = "+18000005555"
else:
self.phone_number = os.getenv("PHONE_NUMBER")
self.recording_processors.extend([
BodyReplacerProcessor(keys=["to", "from", "messageId"]),
ResponseReplacerProcessor(keys=[self._resource_name])])
@AsyncCommunicationTestCase.await_prepared_test
@pytest.mark.live_test_only
async def test_send_sms_async(self):
sms_client = SmsClient.from_connection_string(self.connection_str)
async with sms_client:
# calling send() with sms values
sms_response = await sms_client.send(
from_phone_number=PhoneNumber(self.phone_number),
to_phone_numbers=[PhoneNumber(self.phone_number)],
message="Hello World via SMS",
send_sms_options=SendSmsOptions(enable_delivery_report=True)) # optional property
assert sms_response.message_id is not None
| sdk/communication/azure-communication-sms/tests/test_sms_client_e2e_async.py | 1,824 | ------------------------------------------------------------------------- Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License. See License.txt in the project root for license information. -------------------------------------------------------------------------- calling send() with sms values optional property | 348 | en | 0.395208 |
"""
Ex 012 - make an algorithm that reads the price of a product and shows it with 5% discount
"""
print('Discover how much is a product with 5% off discount')
print('-' * 50)
pp = float(input('Enter the product price: '))
pd = pp - (pp / 100) * 5
print('-' * 50)
print(f"The product price was {pp:.2f}, on promotion of 5% will cost {pd:.2f}")
input('Enter to exit')
| Ex12.py | 371 | Ex 012 - make an algorithm that reads the price of a product and shows it with 5% discount | 90 | en | 0.957279 |
# coding=utf-8
# *** WARNING: this file was generated by the Pulumi SDK Generator. ***
# *** Do not edit by hand unless you're certain you know what you are doing! ***
import warnings
import pulumi
import pulumi.runtime
from typing import Any, Mapping, Optional, Sequence, Union, overload
from .. import _utilities
from . import outputs
from ._enums import *
from ._inputs import *
__all__ = ['FeatureGroupArgs', 'FeatureGroup']
@pulumi.input_type
class FeatureGroupArgs:
def __init__(__self__, *,
event_time_feature_name: pulumi.Input[str],
feature_definitions: pulumi.Input[Sequence[pulumi.Input['FeatureGroupFeatureDefinitionArgs']]],
record_identifier_feature_name: pulumi.Input[str],
description: Optional[pulumi.Input[str]] = None,
feature_group_name: Optional[pulumi.Input[str]] = None,
offline_store_config: Optional[pulumi.Input['OfflineStoreConfigPropertiesArgs']] = None,
online_store_config: Optional[pulumi.Input['OnlineStoreConfigPropertiesArgs']] = None,
role_arn: Optional[pulumi.Input[str]] = None,
tags: Optional[pulumi.Input[Sequence[pulumi.Input['FeatureGroupTagArgs']]]] = None):
"""
The set of arguments for constructing a FeatureGroup resource.
:param pulumi.Input[str] event_time_feature_name: The Event Time Feature Name.
:param pulumi.Input[Sequence[pulumi.Input['FeatureGroupFeatureDefinitionArgs']]] feature_definitions: An Array of Feature Definition
:param pulumi.Input[str] record_identifier_feature_name: The Record Identifier Feature Name.
:param pulumi.Input[str] description: Description about the FeatureGroup.
:param pulumi.Input[str] feature_group_name: The Name of the FeatureGroup.
:param pulumi.Input[str] role_arn: Role Arn
:param pulumi.Input[Sequence[pulumi.Input['FeatureGroupTagArgs']]] tags: An array of key-value pair to apply to this resource.
"""
pulumi.set(__self__, "event_time_feature_name", event_time_feature_name)
pulumi.set(__self__, "feature_definitions", feature_definitions)
pulumi.set(__self__, "record_identifier_feature_name", record_identifier_feature_name)
if description is not None:
pulumi.set(__self__, "description", description)
if feature_group_name is not None:
pulumi.set(__self__, "feature_group_name", feature_group_name)
if offline_store_config is not None:
pulumi.set(__self__, "offline_store_config", offline_store_config)
if online_store_config is not None:
pulumi.set(__self__, "online_store_config", online_store_config)
if role_arn is not None:
pulumi.set(__self__, "role_arn", role_arn)
if tags is not None:
pulumi.set(__self__, "tags", tags)
@property
@pulumi.getter(name="eventTimeFeatureName")
def event_time_feature_name(self) -> pulumi.Input[str]:
"""
The Event Time Feature Name.
"""
return pulumi.get(self, "event_time_feature_name")
@event_time_feature_name.setter
def event_time_feature_name(self, value: pulumi.Input[str]):
pulumi.set(self, "event_time_feature_name", value)
@property
@pulumi.getter(name="featureDefinitions")
def feature_definitions(self) -> pulumi.Input[Sequence[pulumi.Input['FeatureGroupFeatureDefinitionArgs']]]:
"""
An Array of Feature Definition
"""
return pulumi.get(self, "feature_definitions")
@feature_definitions.setter
def feature_definitions(self, value: pulumi.Input[Sequence[pulumi.Input['FeatureGroupFeatureDefinitionArgs']]]):
pulumi.set(self, "feature_definitions", value)
@property
@pulumi.getter(name="recordIdentifierFeatureName")
def record_identifier_feature_name(self) -> pulumi.Input[str]:
"""
The Record Identifier Feature Name.
"""
return pulumi.get(self, "record_identifier_feature_name")
@record_identifier_feature_name.setter
def record_identifier_feature_name(self, value: pulumi.Input[str]):
pulumi.set(self, "record_identifier_feature_name", value)
@property
@pulumi.getter
def description(self) -> Optional[pulumi.Input[str]]:
"""
Description about the FeatureGroup.
"""
return pulumi.get(self, "description")
@description.setter
def description(self, value: Optional[pulumi.Input[str]]):
pulumi.set(self, "description", value)
@property
@pulumi.getter(name="featureGroupName")
def feature_group_name(self) -> Optional[pulumi.Input[str]]:
"""
The Name of the FeatureGroup.
"""
return pulumi.get(self, "feature_group_name")
@feature_group_name.setter
def feature_group_name(self, value: Optional[pulumi.Input[str]]):
pulumi.set(self, "feature_group_name", value)
@property
@pulumi.getter(name="offlineStoreConfig")
def offline_store_config(self) -> Optional[pulumi.Input['OfflineStoreConfigPropertiesArgs']]:
return pulumi.get(self, "offline_store_config")
@offline_store_config.setter
def offline_store_config(self, value: Optional[pulumi.Input['OfflineStoreConfigPropertiesArgs']]):
pulumi.set(self, "offline_store_config", value)
@property
@pulumi.getter(name="onlineStoreConfig")
def online_store_config(self) -> Optional[pulumi.Input['OnlineStoreConfigPropertiesArgs']]:
return pulumi.get(self, "online_store_config")
@online_store_config.setter
def online_store_config(self, value: Optional[pulumi.Input['OnlineStoreConfigPropertiesArgs']]):
pulumi.set(self, "online_store_config", value)
@property
@pulumi.getter(name="roleArn")
def role_arn(self) -> Optional[pulumi.Input[str]]:
"""
Role Arn
"""
return pulumi.get(self, "role_arn")
@role_arn.setter
def role_arn(self, value: Optional[pulumi.Input[str]]):
pulumi.set(self, "role_arn", value)
@property
@pulumi.getter
def tags(self) -> Optional[pulumi.Input[Sequence[pulumi.Input['FeatureGroupTagArgs']]]]:
"""
An array of key-value pair to apply to this resource.
"""
return pulumi.get(self, "tags")
@tags.setter
def tags(self, value: Optional[pulumi.Input[Sequence[pulumi.Input['FeatureGroupTagArgs']]]]):
pulumi.set(self, "tags", value)
class FeatureGroup(pulumi.CustomResource):
@overload
def __init__(__self__,
resource_name: str,
opts: Optional[pulumi.ResourceOptions] = None,
description: Optional[pulumi.Input[str]] = None,
event_time_feature_name: Optional[pulumi.Input[str]] = None,
feature_definitions: Optional[pulumi.Input[Sequence[pulumi.Input[pulumi.InputType['FeatureGroupFeatureDefinitionArgs']]]]] = None,
feature_group_name: Optional[pulumi.Input[str]] = None,
offline_store_config: Optional[pulumi.Input[pulumi.InputType['OfflineStoreConfigPropertiesArgs']]] = None,
online_store_config: Optional[pulumi.Input[pulumi.InputType['OnlineStoreConfigPropertiesArgs']]] = None,
record_identifier_feature_name: Optional[pulumi.Input[str]] = None,
role_arn: Optional[pulumi.Input[str]] = None,
tags: Optional[pulumi.Input[Sequence[pulumi.Input[pulumi.InputType['FeatureGroupTagArgs']]]]] = None,
__props__=None):
"""
Resource Type definition for AWS::SageMaker::FeatureGroup
:param str resource_name: The name of the resource.
:param pulumi.ResourceOptions opts: Options for the resource.
:param pulumi.Input[str] description: Description about the FeatureGroup.
:param pulumi.Input[str] event_time_feature_name: The Event Time Feature Name.
:param pulumi.Input[Sequence[pulumi.Input[pulumi.InputType['FeatureGroupFeatureDefinitionArgs']]]] feature_definitions: An Array of Feature Definition
:param pulumi.Input[str] feature_group_name: The Name of the FeatureGroup.
:param pulumi.Input[str] record_identifier_feature_name: The Record Identifier Feature Name.
:param pulumi.Input[str] role_arn: Role Arn
:param pulumi.Input[Sequence[pulumi.Input[pulumi.InputType['FeatureGroupTagArgs']]]] tags: An array of key-value pair to apply to this resource.
"""
...
@overload
def __init__(__self__,
resource_name: str,
args: FeatureGroupArgs,
opts: Optional[pulumi.ResourceOptions] = None):
"""
Resource Type definition for AWS::SageMaker::FeatureGroup
:param str resource_name: The name of the resource.
:param FeatureGroupArgs args: The arguments to use to populate this resource's properties.
:param pulumi.ResourceOptions opts: Options for the resource.
"""
...
def __init__(__self__, resource_name: str, *args, **kwargs):
resource_args, opts = _utilities.get_resource_args_opts(FeatureGroupArgs, pulumi.ResourceOptions, *args, **kwargs)
if resource_args is not None:
__self__._internal_init(resource_name, opts, **resource_args.__dict__)
else:
__self__._internal_init(resource_name, *args, **kwargs)
def _internal_init(__self__,
resource_name: str,
opts: Optional[pulumi.ResourceOptions] = None,
description: Optional[pulumi.Input[str]] = None,
event_time_feature_name: Optional[pulumi.Input[str]] = None,
feature_definitions: Optional[pulumi.Input[Sequence[pulumi.Input[pulumi.InputType['FeatureGroupFeatureDefinitionArgs']]]]] = None,
feature_group_name: Optional[pulumi.Input[str]] = None,
offline_store_config: Optional[pulumi.Input[pulumi.InputType['OfflineStoreConfigPropertiesArgs']]] = None,
online_store_config: Optional[pulumi.Input[pulumi.InputType['OnlineStoreConfigPropertiesArgs']]] = None,
record_identifier_feature_name: Optional[pulumi.Input[str]] = None,
role_arn: Optional[pulumi.Input[str]] = None,
tags: Optional[pulumi.Input[Sequence[pulumi.Input[pulumi.InputType['FeatureGroupTagArgs']]]]] = None,
__props__=None):
if opts is None:
opts = pulumi.ResourceOptions()
if not isinstance(opts, pulumi.ResourceOptions):
raise TypeError('Expected resource options to be a ResourceOptions instance')
if opts.version is None:
opts.version = _utilities.get_version()
if opts.id is None:
if __props__ is not None:
raise TypeError('__props__ is only valid when passed in combination with a valid opts.id to get an existing resource')
__props__ = FeatureGroupArgs.__new__(FeatureGroupArgs)
__props__.__dict__["description"] = description
if event_time_feature_name is None and not opts.urn:
raise TypeError("Missing required property 'event_time_feature_name'")
__props__.__dict__["event_time_feature_name"] = event_time_feature_name
if feature_definitions is None and not opts.urn:
raise TypeError("Missing required property 'feature_definitions'")
__props__.__dict__["feature_definitions"] = feature_definitions
__props__.__dict__["feature_group_name"] = feature_group_name
__props__.__dict__["offline_store_config"] = offline_store_config
__props__.__dict__["online_store_config"] = online_store_config
if record_identifier_feature_name is None and not opts.urn:
raise TypeError("Missing required property 'record_identifier_feature_name'")
__props__.__dict__["record_identifier_feature_name"] = record_identifier_feature_name
__props__.__dict__["role_arn"] = role_arn
__props__.__dict__["tags"] = tags
super(FeatureGroup, __self__).__init__(
'aws-native:sagemaker:FeatureGroup',
resource_name,
__props__,
opts)
@staticmethod
def get(resource_name: str,
id: pulumi.Input[str],
opts: Optional[pulumi.ResourceOptions] = None) -> 'FeatureGroup':
"""
Get an existing FeatureGroup resource's state with the given name, id, and optional extra
properties used to qualify the lookup.
:param str resource_name: The unique name of the resulting resource.
:param pulumi.Input[str] id: The unique provider ID of the resource to lookup.
:param pulumi.ResourceOptions opts: Options for the resource.
"""
opts = pulumi.ResourceOptions.merge(opts, pulumi.ResourceOptions(id=id))
__props__ = FeatureGroupArgs.__new__(FeatureGroupArgs)
__props__.__dict__["description"] = None
__props__.__dict__["event_time_feature_name"] = None
__props__.__dict__["feature_definitions"] = None
__props__.__dict__["feature_group_name"] = None
__props__.__dict__["offline_store_config"] = None
__props__.__dict__["online_store_config"] = None
__props__.__dict__["record_identifier_feature_name"] = None
__props__.__dict__["role_arn"] = None
__props__.__dict__["tags"] = None
return FeatureGroup(resource_name, opts=opts, __props__=__props__)
@property
@pulumi.getter
def description(self) -> pulumi.Output[Optional[str]]:
"""
Description about the FeatureGroup.
"""
return pulumi.get(self, "description")
@property
@pulumi.getter(name="eventTimeFeatureName")
def event_time_feature_name(self) -> pulumi.Output[str]:
"""
The Event Time Feature Name.
"""
return pulumi.get(self, "event_time_feature_name")
@property
@pulumi.getter(name="featureDefinitions")
def feature_definitions(self) -> pulumi.Output[Sequence['outputs.FeatureGroupFeatureDefinition']]:
"""
An Array of Feature Definition
"""
return pulumi.get(self, "feature_definitions")
@property
@pulumi.getter(name="featureGroupName")
def feature_group_name(self) -> pulumi.Output[str]:
"""
The Name of the FeatureGroup.
"""
return pulumi.get(self, "feature_group_name")
@property
@pulumi.getter(name="offlineStoreConfig")
def offline_store_config(self) -> pulumi.Output[Optional['outputs.OfflineStoreConfigProperties']]:
return pulumi.get(self, "offline_store_config")
@property
@pulumi.getter(name="onlineStoreConfig")
def online_store_config(self) -> pulumi.Output[Optional['outputs.OnlineStoreConfigProperties']]:
return pulumi.get(self, "online_store_config")
@property
@pulumi.getter(name="recordIdentifierFeatureName")
def record_identifier_feature_name(self) -> pulumi.Output[str]:
"""
The Record Identifier Feature Name.
"""
return pulumi.get(self, "record_identifier_feature_name")
@property
@pulumi.getter(name="roleArn")
def role_arn(self) -> pulumi.Output[Optional[str]]:
"""
Role Arn
"""
return pulumi.get(self, "role_arn")
@property
@pulumi.getter
def tags(self) -> pulumi.Output[Optional[Sequence['outputs.FeatureGroupTag']]]:
"""
An array of key-value pair to apply to this resource.
"""
return pulumi.get(self, "tags")
| sdk/python/pulumi_aws_native/sagemaker/feature_group.py | 15,772 | The set of arguments for constructing a FeatureGroup resource.
:param pulumi.Input[str] event_time_feature_name: The Event Time Feature Name.
:param pulumi.Input[Sequence[pulumi.Input['FeatureGroupFeatureDefinitionArgs']]] feature_definitions: An Array of Feature Definition
:param pulumi.Input[str] record_identifier_feature_name: The Record Identifier Feature Name.
:param pulumi.Input[str] description: Description about the FeatureGroup.
:param pulumi.Input[str] feature_group_name: The Name of the FeatureGroup.
:param pulumi.Input[str] role_arn: Role Arn
:param pulumi.Input[Sequence[pulumi.Input['FeatureGroupTagArgs']]] tags: An array of key-value pair to apply to this resource.
Resource Type definition for AWS::SageMaker::FeatureGroup
:param str resource_name: The name of the resource.
:param pulumi.ResourceOptions opts: Options for the resource.
:param pulumi.Input[str] description: Description about the FeatureGroup.
:param pulumi.Input[str] event_time_feature_name: The Event Time Feature Name.
:param pulumi.Input[Sequence[pulumi.Input[pulumi.InputType['FeatureGroupFeatureDefinitionArgs']]]] feature_definitions: An Array of Feature Definition
:param pulumi.Input[str] feature_group_name: The Name of the FeatureGroup.
:param pulumi.Input[str] record_identifier_feature_name: The Record Identifier Feature Name.
:param pulumi.Input[str] role_arn: Role Arn
:param pulumi.Input[Sequence[pulumi.Input[pulumi.InputType['FeatureGroupTagArgs']]]] tags: An array of key-value pair to apply to this resource.
Resource Type definition for AWS::SageMaker::FeatureGroup
:param str resource_name: The name of the resource.
:param FeatureGroupArgs args: The arguments to use to populate this resource's properties.
:param pulumi.ResourceOptions opts: Options for the resource.
Description about the FeatureGroup.
Description about the FeatureGroup.
The Event Time Feature Name.
The Event Time Feature Name.
An Array of Feature Definition
An Array of Feature Definition
The Name of the FeatureGroup.
The Name of the FeatureGroup.
Get an existing FeatureGroup resource's state with the given name, id, and optional extra
properties used to qualify the lookup.
:param str resource_name: The unique name of the resulting resource.
:param pulumi.Input[str] id: The unique provider ID of the resource to lookup.
:param pulumi.ResourceOptions opts: Options for the resource.
The Record Identifier Feature Name.
The Record Identifier Feature Name.
Role Arn
Role Arn
An array of key-value pair to apply to this resource.
An array of key-value pair to apply to this resource.
coding=utf-8 *** WARNING: this file was generated by the Pulumi SDK Generator. *** *** Do not edit by hand unless you're certain you know what you are doing! *** | 2,739 | en | 0.625668 |
from django.contrib import admin
from django.urls import path
from django.contrib.auth.views import LoginView
from . import views
app_name = 'users'
urlpatterns = [
# ex /users/
path('', views.index, name='index'),
# ex /users/login/
path('login/', LoginView.as_view(template_name='users/login.html'),
name='login'),
# ex /users/logout/
path('logout/', views.logout_view, name='logout'),
# ex /users/register/
path('register/', views.register, name='register'),
]
| users/urls.py | 517 | ex /users/ ex /users/login/ ex /users/logout/ ex /users/register/ | 65 | en | 0.192976 |
import mido
import json
import time
from math import floor
import board
import busio
import digitalio
import adafruit_tlc5947
def playMidi(song_name):
mid = mido.MidiFile('midifiles/' + song_name)
notesDict = {'songName': 'testname', 'bpm': 999, 'notes': []}
tempo = 0
length = 0
notesArray = [[]]
tickLength = 0
SCK = board.SCK
MOSI = board.MOSI
LATCH = digitalio.DigitalInOut(board.D5)
# Initialize SPI bus.
spi = busio.SPI(clock=SCK, MOSI=MOSI)
# Initialize TLC5947
tlc5947 = adafruit_tlc5947.TLC5947(spi, LATCH, auto_write=False,
num_drivers=4)
for x in range(88):
tlc5947[x] = 0
tlc5947.write()
for msg in mid:
if msg.is_meta and msg.type == 'set_tempo':
tempo = int(msg.tempo)
length = int(floor(mido.second2tick(mid.length,
mid.ticks_per_beat,
tempo)))
tickLength = mido.tick2second(1, mid.ticks_per_beat, tempo)
break
print('Tick length: ' + str(tickLength))
currentTick = 0
notesArray[0] = [0 for x in range(89)]
lineIncrement = 0
for msg in mid:
#print(msg)
if msg.type is 'note_on' or msg.type is 'note_off':
delayAfter = int(floor(mido.second2tick(msg.time, mid.ticks_per_beat, tempo)))
if delayAfter == 0:
if msg.note < 89:
notesArray[lineIncrement][msg.note - 12] = msg.velocity
else:
notesArray[lineIncrement][88] = delayAfter
notesArray.append([0 for x in range(89)])
lineIncrement += 1
""" Old code:
for x in range (newNote['delayAfter']):
if x != 0:
notesArray[x+currentTick] = notesArray[x+currentTick-1]
currentTick += newNote['delayAfter']
notesArray[currentTick][newNote['note'] - 1] = newNote['velocity']
# tlc5947.write()
notesDict['notes'].append(newNote)
"""
"""
with open('notes.json', 'w') as outfile:
json.dump(notesDict, outfile)
"""
startTime = time.time()
tlc5947.write()
time.sleep(3)
for line in notesArray:
"""
tlc5947[27] = 900
tlc5947[68] = 4000
tlc5947.write()
time.sleep(2)
tlc5947[27] = 0
tlc5947[68] = 0
tlc5947.write()
time.sleep(2)
"""
print(line)
# send array to PWM IC
for x in range(len(line) - 1):
if line[x] != 0:
tlc5947[x] = line[x] * 32
else:
tlc5947[x] = 0
tlc5947.write()
# time.sleep(tickLength)
time.sleep(mido.tick2second(line[88], mid.ticks_per_beat, tempo) * 0.4)
for x in range(88):
tlc5947[x] = 0
tlc5947.write()
time.sleep(mido.tick2second(line[88], mid.ticks_per_beat, tempo) * 0.6)
for x in range(88):
tlc5947[x] = 0
tlc5947.write()
#playMidi('twinkle_twinkle.mid')
#playMidi('for_elise_by_beethoven.mid')
# playMidi('debussy_clair_de_lune.mid')
# playMidi('chopin_minute.mid')
# playMidi('jules_mad_world.mid')
| play_midi_curtis.py | 3,453 | Initialize SPI bus. Initialize TLC5947print(msg) send array to PWM IC time.sleep(tickLength)playMidi('twinkle_twinkle.mid')playMidi('for_elise_by_beethoven.mid') playMidi('debussy_clair_de_lune.mid') playMidi('chopin_minute.mid') playMidi('jules_mad_world.mid') | 261 | en | 0.346804 |
# -*- coding: utf-8 -*-
# vim: ts=2 sw=2 et ai
###############################################################################
# Copyright (c) 2012,2013-2021 Andreas Vogel andreas@wellenvogel.net
#
# 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 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.
#
# parts from this software (AIS decoding) are taken from the gpsd project
# so refer to this BSD licencse also (see ais.py) or omit ais.py
# parts contributed by free-x https://github.com/free-x
# parts contributed by Matt Hawkins http://www.raspberrypi-spy.co.uk/
#
###############################################################################
import hashlib
import avnav_handlerList
from avnav_nmea import *
from avnav_worker import *
class AVNUserAppHandler(AVNWorker):
'''
handle the files in the user directory
'''
CHILDNAME="UserTool"
TYPE="addon"
@classmethod
def getStartupGroup(cls):
return 3
@classmethod
def getPrefix(cls):
return None
@classmethod
def getConfigParam(cls, child=None):
#we add this to the ones configured at HTTPServer
if child == cls.CHILDNAME:
return {
'url':None, #we replace $HOST...
'title':'',
'icon':None, #an icon below $datadir/user
'keepUrl':'' #auto detect
}
if not child is None:
return None
rt = {
'interval': '5',
}
return rt
@classmethod
def preventMultiInstance(cls):
return True
@classmethod
def autoInstantiate(cls):
return True
def __init__(self,param):
self.userHandler=None # AVNUserHandler
self.imagesHandler=None # AVNImagesHandler
self.httpServer=None # AVNHTTPServer
self.addonList=[]
self.additionalAddOns=[]
AVNWorker.__init__(self,param)
def startInstance(self, navdata):
self.userHandler=self.findHandlerByName('AVNUserHandler')
if self.userHandler is None:
raise Exception("unable to find a user handler")
self.imagesHandler=self.findHandlerByName('AVNImagesHandler')
if self.imagesHandler is None:
raise Exception("unable to find an images handler")
self.httpServer = self.findHandlerByName('AVNHttpServer')
if self.httpServer is None:
raise Exception("unable to find AVNHttpServer")
super().startInstance(navdata)
# thread run method - just try forever
def run(self):
sleepTime=self.getFloatParam('interval')
self.setInfo('main', "starting", WorkerStatus.STARTED)
self.fillList()
while not self.shouldStop():
self.wait(sleepTime)
def computeKey(self,entry):
md5=hashlib.md5()
for k in ('url','icon','title'):
v=entry.get(k)
if v is not None:
try:
md5.update(v.encode('utf-8'))
except Exception as e:
AVNLog.error("unable to compute md5 for %s: %s",v,e)
return md5.hexdigest()
def fillList(self):
data = []
alreadyFound=set()
childlist = self.param.get(self.CHILDNAME)
if childlist is not None:
for child in childlist:
url=child.get('url')
key=self.computeKey(child)
if url is None:
child['invalid']=True
if key in alreadyFound:
AVNLog.error("duplicate user app found, ignoring %s",url)
while key in alreadyFound:
key = key + "x"
child['name']=key
child['invalid']=True
else:
child['name']=key
alreadyFound.add(key)
item=child.copy()
item['canDelete']=True
item['source']='user'
data.append(item)
serverAddons = self.httpServer.getParamValue(self.CHILDNAME)
nr=0
if serverAddons is not None:
for addon in serverAddons:
newAddon = addon.copy()
newAddon['canDelete']=False
newAddon['name']="server:%d"%nr
newAddon['source']='legacy'
nr+=1
data.append(newAddon)
for addon in data:
url = addon.get('url')
if url is None:
addon['invalid']=True
if not url.startswith("http"):
userFile = self.findFileForUrl(url)
if userFile is None:
AVNLog.error("error: user url %s not found", url)
addon['invalid']=True
if addon.get('title') == '':
del addon['title']
keepUrl = False
if addon.get('keepUrl') is None or addon.get('keepUrl') == '':
if addon.get('url').startswith("http"):
keepUrl = True
else:
if str(addon.get('keepUrl')).lower() == "true":
keepUrl = True
addon['keepUrl'] = keepUrl
icon = addon['icon']
if not icon.startswith("http"):
if not icon.startswith("/user"):
icon="/user/"+icon
addon['icon']=icon
iconpath = self.findFileForUrl(icon)
if iconpath is None:
AVNLog.error("icon path %s for %s not found, ignoring entry", icon, addon['url'])
addon['invalid'] = True
self.addonList=data
self.setInfo('main', "active, %d addons"%len(data), WorkerStatus.NMEA)
return
def findFileForUrl(self,url):
if url is None:
return None
if url.startswith("http"):
return None
(path,query)=self.httpServer.pathQueryFromUrl(url)
filePath=self.httpServer.tryExternalMappings(path,query)
if filePath is None or not os.path.exists(filePath):
return None
return filePath
def findChild(self,name,ignoreInvalid=False):
children=self.param.get(self.CHILDNAME)
if children is None:
return -1
if not isinstance(children,list):
return -1
for i in range(0,len(children)):
child =children[i]
if child.get('name') == name:
if ignoreInvalid:
inList=[e for e in self.addonList if e.get('name') == name and not ( e.get('invalid') == True)]
if len(inList) < 0:
return -1
return i
return -1
def getChildConfig(self,name):
idx=self.findChild(name)
if idx < 0:
return {}
else:
return self.param[self.CHILDNAME][idx]
def handleDelete(self,name):
if name is None:
raise Exception("missing name")
name = AVNUtil.clean_filename(name)
idx=self.findChild(name)
if idx < 0:
raise Exception("unable to find %s"%name)
self.removeChildConfig(self.CHILDNAME,idx)
self.fillList()
def handleList(self,httpHandler,includeInvalid):
host = httpHandler.headers.get('host')
hostparts = host.split(':')
outdata=[]
src=self.additionalAddOns+self.addonList
for addon in src:
if addon.get('invalid') == True and not includeInvalid:
continue
item=addon.copy()
if hostparts is not None:
item['originalUrl']=addon['url']
item['url'] = addon['url'].replace('$HOST', hostparts[0])
outdata.append(item)
rt = AVNUtil.getReturnData(items=outdata)
return rt
def getHandledCommands(self):
rt={"api": self.TYPE, "list": self.TYPE, "delete": self.TYPE}
prefix=self.getPrefix()
if prefix is not None:
rt["path"]=prefix
return rt
def checkName(self,name,doRaise=True):
cleanName=AVNUtil.clean_filename(name)
if name != cleanName:
if doRaise:
raise Exception("name %s is invalid"%name)
return False
return True
def registerAddOn(self,name,url,iconPath,title=None):
newAddon = {
'name': name,
'url': url,
'icon': iconPath,
'title': title,
'canDelete': False,
'source':'plugin'
}
self.additionalAddOns.append(newAddon)
def unregisterAddOn(self,name):
if name is None:
raise Exception("name cannot be None")
for ao in self.additionalAddOns:
if ao.get('name') == name:
self.additionalAddOns.remove(ao)
return True
def deleteByUrl(self,url):
"""
called by the user handler when a user file is deleted
@param url:
@return:
"""
if url is None:
return
for addon in self.addonList:
if addon.get('canDelete') == True and addon.get('url') == url:
self.handleDelete(addon.get('name'))
def handleApiRequest(self, type, subtype, requestparam, **kwargs):
if type == 'api':
command=AVNUtil.getHttpRequestParam(requestparam,'command',True)
name=AVNUtil.getHttpRequestParam(requestparam,'name',False)
if command == 'delete':
self.handleDelete(name)
return AVNUtil.getReturnData()
elif command == 'list':
includeInvalid = AVNUtil.getHttpRequestParam(requestparam, "invalid")
return self.handleList(kwargs.get('handler'),includeInvalid is not None and includeInvalid.lower() == 'true')
elif command == 'update':
url=AVNUtil.getHttpRequestParam(requestparam,'url',True)
icon=AVNUtil.getHttpRequestParam(requestparam,'icon',True)
title=AVNUtil.getHttpRequestParam(requestparam,'title')
param = {}
param['icon'] = icon
param['title'] = title
param['url'] = url
param['keepUrl'] = url.startswith("http")
doAdd=False
if name is None:
doAdd=True
name=self.computeKey(param)
#add
for entry in self.addonList:
if entry['name'] == name:
raise Exception("trying to add an already existing url %s"%url)
param['name']=name
if not url.startswith("http"):
userFile=self.findFileForUrl(url)
if userFile is None:
raise Exception("unable to find a local file for %s"%url)
if not icon.startswith("http"):
iconFile=self.findFileForUrl(icon)
if iconFile is None:
raise Exception("unable to find an icon file for %s"%icon)
idx=self.findChild(name)
if idx < 0 and not doAdd:
raise Exception("did not find a user app with this name")
for k in list(param.keys()):
idx=self.changeChildConfig(self.CHILDNAME,idx,k,param[k],True)
self.writeConfigChanges()
self.fillList()
return AVNUtil.getReturnData()
raise Exception("unknown command for %s api request: %s"%(self.type,command))
if type == "list":
includeInvalid=AVNUtil.getHttpRequestParam(requestparam,"invalid")
return self.handleList(kwargs.get('handler'),includeInvalid is not None and includeInvalid.lower() == 'true')
if type == 'delete':
name = AVNUtil.getHttpRequestParam(requestparam, "name",True)
self.handleDelete(name)
return AVNUtil.getReturnData()
raise Exception("unable to handle user request %s"%(type))
avnav_handlerList.registerHandler(AVNUserAppHandler)
| server/handler/avnuserapps.py | 11,533 | handle the files in the user directory
called by the user handler when a user file is deleted
@param url:
@return:
-*- coding: utf-8 -*- vim: ts=2 sw=2 et ai Copyright (c) 2012,2013-2021 Andreas Vogel andreas@wellenvogel.net 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 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. parts from this software (AIS decoding) are taken from the gpsd project so refer to this BSD licencse also (see ais.py) or omit ais.py parts contributed by free-x https://github.com/free-x parts contributed by Matt Hawkins http://www.raspberrypi-spy.co.uk/we add this to the ones configured at HTTPServerwe replace $HOST...an icon below $datadir/userauto detect AVNUserHandler AVNImagesHandler AVNHTTPServer thread run method - just try foreveradd | 1,714 | en | 0.851534 |
# generated from catkin/cmake/template/pkg.context.pc.in
CATKIN_PACKAGE_PREFIX = ""
PROJECT_PKG_CONFIG_INCLUDE_DIRS = "".split(';') if "" != "" else []
PROJECT_CATKIN_DEPENDS = "".replace(';', ' ')
PKG_CONFIG_LIBRARIES_WITH_PREFIX = "".split(';') if "" != "" else []
PROJECT_NAME = "styx"
PROJECT_SPACE_DIR = "/home/zchen61/carnd-capstone-test/ros/devel"
PROJECT_VERSION = "0.0.0"
| ros/build/styx/catkin_generated/pkg.develspace.context.pc.py | 381 | generated from catkin/cmake/template/pkg.context.pc.in | 54 | en | 0.406568 |
"""empty message
Revision ID: 4a83f309f411
Revises:
Create Date: 2019-06-20 23:47:24.513383
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '4a83f309f411'
down_revision = None
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('users',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('username', sa.String(length=50), nullable=True),
sa.Column('handle', sa.String(length=25), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('handle'),
sa.UniqueConstraint('id')
)
op.create_table('post',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('user_id', sa.Integer(), nullable=True),
sa.Column('img_name', sa.String(length=100), nullable=True),
sa.Column('caption', sa.String(length=250), nullable=True),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('id'),
sa.UniqueConstraint('img_name')
)
op.create_table('comment',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('post_id', sa.Integer(), nullable=True),
sa.Column('user', sa.String(length=50), nullable=True),
sa.Column('text', sa.String(length=100), nullable=True),
sa.ForeignKeyConstraint(['post_id'], ['post.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('id')
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('comment')
op.drop_table('post')
op.drop_table('users')
# ### end Alembic commands ###
| migrations/versions/4a83f309f411_.py | 2,154 | empty message
Revision ID: 4a83f309f411
Revises:
Create Date: 2019-06-20 23:47:24.513383
revision identifiers, used by Alembic. commands auto generated by Alembic - please adjust! end Alembic commands commands auto generated by Alembic - please adjust! end Alembic commands | 284 | en | 0.617482 |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""":Mod: views.py
:Synopsis:
:Author:
costa
servilla
ide
:Created:
7/23/18
"""
import daiquiri
from datetime import date, datetime
import html
import json
import math
import os.path
import pandas as pd
from pathlib import Path
import pickle
import requests
from shutil import copyfile
from urllib.parse import urlparse, quote
from zipfile import ZipFile
from flask import (
Blueprint, flash, render_template, redirect, request, url_for, session, Markup
)
from flask_login import (
current_user, login_required
)
from flask import Flask, current_app
from webapp import mailout
from webapp.config import Config
import csv
from webapp.home.exceptions import DataTableError, MissingFileError
from webapp.home.forms import (
CreateEMLForm, DownloadEMLForm, ImportPackageForm,
OpenEMLDocumentForm, DeleteEMLForm, SaveAsForm,
LoadDataForm, LoadMetadataForm, LoadOtherEntityForm,
ImportEMLForm, ImportEMLItemsForm, ImportItemsForm,
SubmitToEDIForm, SendToColleagueForm
)
from webapp.home.load_data_table import (
load_data_table, load_other_entity, delete_data_files, get_md5_hash
)
from webapp.home.import_package import (
copy_ezeml_package, upload_ezeml_package, import_ezeml_package
)
from webapp.home.metapype_client import (
load_eml, save_both_formats, new_child_node, remove_child, create_eml,
move_up, move_down, UP_ARROW, DOWN_ARROW, RELEASE_NUMBER,
save_old_to_new, read_xml, new_child_node, truncate_middle,
compose_rp_label, compose_full_gc_label, compose_taxonomic_label,
compose_funding_award_label, compose_project_label, list_data_packages,
import_responsible_parties, import_coverage_nodes, import_funding_award_nodes,
import_project_nodes, get_check_metadata_status
)
from webapp.home.check_metadata import check_eml
from webapp.buttons import *
from webapp.pages import *
from metapype.eml import names
from metapype.model import mp_io
from metapype.model.node import Node
from werkzeug.utils import secure_filename
import webapp.views.data_tables.dt as dt
import webapp.auth.user_data as user_data
logger = daiquiri.getLogger('views: ' + __name__)
home = Blueprint('home', __name__, template_folder='templates')
help_dict = {}
keywords = {}
def log_info(msg):
app = Flask(__name__)
with app.app_context():
current_app.logger.info(msg)
def non_breaking(_str):
return _str.replace(' ', html.unescape(' '))
def debug_msg(msg):
if Config.LOG_DEBUG:
app = Flask(__name__)
with app.app_context():
current_app.logger.info(msg)
def debug_None(x, msg):
if x is None:
if Config.LOG_DEBUG:
app = Flask(__name__)
with app.app_context():
current_app.logger.info(msg)
def reload_metadata():
current_document = current_user.get_filename()
if not current_document:
# if we've just deleted the current document, it won't exist
return redirect(url_for(PAGE_INDEX))
# Call load_eml here to get the check_metadata status set correctly
eml_node = load_eml(filename=current_document)
return current_document, eml_node
@home.before_app_first_request
def init_session_vars():
session["check_metadata_status"] = "green"
@home.before_app_first_request
def fixup_upload_management():
return
USER_DATA_DIR = 'user-data'
to_delete = set()
# loop on the various users' data directories
for user_folder_name in os.listdir(USER_DATA_DIR):
if user_folder_name == 'uploads' or user_folder_name == 'zip_temp':
continue
if os.path.isdir(os.path.join(USER_DATA_DIR, user_folder_name)):
user_data.clear_data_table_upload_filenames(user_folder_name)
full_path = os.path.join(USER_DATA_DIR, user_folder_name)
uploads_path = os.path.join(full_path, 'uploads')
# look at the EML model json files
for file in os.listdir(full_path):
full_file = os.path.join(full_path, file)
if os.path.isfile(full_file) and full_file.lower().endswith('.json') and file != '__user_properties__.json':
# some directories may have obsolete 'user_properties.json' files
if file == 'user_properties.json':
to_delete.add(os.path.join(full_path, 'user_properties.json'))
continue
# create a subdir of the user's uploads directory for this document's uploads
document_name = file[:-5]
subdir_name = os.path.join(uploads_path, document_name)
try:
os.mkdir(subdir_name)
except OSError:
pass
# open the model file
with open(full_file, "r") as json_file:
json_obj = json.load(json_file)
eml_node = mp_io.from_json(json_obj)
# look at data tables
data_table_nodes = []
eml_node.find_all_descendants(names.DATATABLE, data_table_nodes)
for data_table_node in data_table_nodes:
object_name_node = data_table_node.find_descendant(names.OBJECTNAME)
if object_name_node:
object_name = object_name_node.content
object_path = os.path.join(uploads_path, object_name)
target_path = os.path.join(subdir_name, object_name)
if os.path.isfile(object_path):
to_delete.add(object_path)
copyfile(object_path, target_path)
# look at other entities
other_entity_nodes = []
eml_node.find_all_descendants(names.OTHERENTITY, other_entity_nodes)
for other_entity_node in other_entity_nodes:
object_name_node = other_entity_node.find_descendant(names.OBJECTNAME)
if object_name_node:
object_name = object_name_node.content
object_path = os.path.join(uploads_path, object_name)
if os.path.isfile(object_path):
to_delete.add(object_path)
copyfile(object_path, os.path.join(subdir_name, object_name))
# clean up temp files
for path in os.listdir(subdir_name):
path = os.path.join(subdir_name, path)
if os.path.isfile(path) and path.endswith('ezeml_tmp'):
to_delete.add(path)
# now capture all uploaded file names in the user data
for file in os.listdir(uploads_path):
uploads_folder = os.path.join(uploads_path, file)
if os.path.isdir(uploads_folder):
# add the uploaded files to the user data
for uploaded_file in os.listdir(uploads_folder):
user_data.add_data_table_upload_filename(uploaded_file, user_folder_name, file)
# clean up temp files
for path in os.listdir(full_path):
path = os.path.join(full_path, path)
if os.path.isfile(path) and path.endswith('ezeml_tmp'):
to_delete.add(path)
# now we can delete the files we've copied
for file in to_delete:
os.remove(file)
@home.before_app_request
@home.before_app_first_request
def load_eval_entries():
rows = []
with open('webapp/static/evaluate.csv') as csv_file:
csv_reader = csv.reader(csv_file)
for row in csv_reader:
rows.append(row)
for row_num in range(1, len(rows)):
id, *vals = rows[row_num]
session[f'__eval__{id}'] = vals
@home.before_app_request
@home.before_app_first_request
def init_keywords():
lter_keywords = pickle.load(open('webapp/static/lter_keywords.pkl', 'rb'))
keywords['LTER'] = lter_keywords
def get_keywords(which):
return keywords.get(which, [])
@home.before_app_request
@home.before_app_first_request
def init_help():
lines = []
with open('webapp/static/help.txt') as help:
lines = help.readlines()
index = 0
def get_help_item(lines, index):
id = lines[index].rstrip()
title = lines[index+1].rstrip()
content = '<p>'
index = index + 2
while index < len(lines):
line = lines[index].rstrip('\n')
index = index + 1
if line.startswith('--------------------'):
break
if len(line) == 0:
line = '</p><p>'
content = content + line
if index >= len(lines):
break
content = content + '</p>'
return (id, title, content), index
while index < len(lines):
(id, title, content), index = get_help_item(lines, index)
help_dict[id] = (title, content)
if id == 'contents':
# special case for supporting base.html template
session[f'__help__{id}'] = (title, content)
def get_help(id):
title, content = help_dict.get(id)
return f'__help__{id}', title, content
def get_helps(ids):
helps = []
for id in ids:
if id in help_dict:
title, content = help_dict.get(id)
helps.append((f'__help__{id}', title, content))
return helps
@home.route('/')
def index():
if current_user.is_authenticated:
current_document = user_data.get_active_document()
if current_document:
eml_node = load_eml(filename=current_document)
if eml_node:
new_page = PAGE_TITLE
else:
user_data.remove_active_file()
new_page = PAGE_FILE_ERROR
return redirect(url_for(new_page, filename=current_document))
return render_template('index.html')
@home.route('/edit/<page>')
def edit(page:str=None):
'''
The edit page allows for direct editing of a top-level element such as
title, abstract, creators, etc. This function simply redirects to the
specified page, passing the packageid as the only parameter.
'''
if current_user.is_authenticated and page:
current_filename = user_data.get_active_document()
if current_filename:
eml_node = load_eml(filename=current_filename)
new_page = page if eml_node else PAGE_FILE_ERROR
return redirect(url_for(new_page, filename=current_filename))
return render_template('index.html')
def get_back_url():
url = url_for(PAGE_INDEX)
if current_user.is_authenticated:
new_page = get_redirect_target_page()
filename = user_data.get_active_document()
if new_page and filename:
url = url_for(new_page, filename=filename)
return url
@home.route('/about')
def about():
return render_template('about.html', back_url=get_back_url(), title='About')
@home.route('/user_guide')
def user_guide():
return render_template('user_guide.html', back_url=get_back_url(), title='User Guide')
@home.route('/news')
def news():
return render_template('news.html', back_url=get_back_url(), title="What's New")
@home.route('/encoding_error/<filename>')
def encoding_error(filename=None, errors=None):
return render_template('encoding_error.html', filename=filename, errors=errors, title='Encoding Errors')
@home.route('/file_error/<filename>')
def file_error(filename=None):
return render_template('file_error.html', filename=filename, title='File Error')
@home.route('/delete', methods=['GET', 'POST'])
@login_required
def delete():
form = DeleteEMLForm()
form.filename.choices = list_data_packages(True, True)
# Process POST
if request.method == 'POST':
if 'Cancel' in request.form:
return redirect(get_back_url())
if form.validate_on_submit():
filename = form.filename.data
user_data.discard_data_table_upload_filenames_for_package(filename)
return_value = user_data.delete_eml(filename=filename)
if filename == user_data.get_active_document():
current_user.set_filename(None)
if isinstance(return_value, str):
flash(return_value)
else:
flash(f'Deleted {filename}')
return redirect(url_for(PAGE_INDEX))
# Process GET
return render_template('delete_eml.html', title='Delete EML',
form=form)
@home.route('/save', methods=['GET', 'POST'])
@login_required
def save():
current_document = current_user.get_filename()
if not current_document:
flash('No document currently open')
return render_template('index.html')
eml_node = load_eml(filename=current_document)
if not eml_node:
flash(f'Unable to open {current_document}')
return render_template('index.html')
save_both_formats(filename=current_document, eml_node=eml_node)
flash(f'Saved {current_document}')
return redirect(url_for(PAGE_TITLE, filename=current_document))
def copy_uploads(from_package, to_package):
from_folder = user_data.get_document_uploads_folder_name(from_package)
to_folder = user_data.get_document_uploads_folder_name(to_package)
for filename in os.listdir(from_folder):
from_path = os.path.join(from_folder, filename)
to_path = os.path.join(to_folder, filename)
copyfile(from_path, to_path)
user_data.add_data_table_upload_filename(filename, document_name=to_package)
@home.route('/save_as', methods=['GET', 'POST'])
@login_required
def save_as():
# Determine POST type
if request.method == 'POST':
if BTN_SAVE in request.form:
submit_type = 'Save'
elif BTN_CANCEL in request.form:
submit_type = 'Cancel'
else:
submit_type = None
form = SaveAsForm()
current_document = current_user.get_filename()
# Process POST
if request.method == 'POST':
if BTN_CANCEL in request.form:
if current_document:
# Revert back to the old filename
return redirect(get_back_url())
else:
return render_template('index.html')
if form.validate_on_submit():
if not current_document:
flash('No document currently open')
return render_template('index.html')
eml_node = load_eml(filename=current_document)
if not eml_node:
flash(f'Unable to open {current_document}')
return render_template('index.html')
new_document = form.filename.data
return_value = save_old_to_new(
old_filename=current_document,
new_filename=new_document,
eml_node=eml_node)
if isinstance(return_value, str):
flash(return_value)
new_filename = current_document # Revert back to the old filename
else:
copy_uploads(current_document, new_document)
current_user.set_filename(filename=new_document)
flash(f'Saved as {new_document}')
new_page = PAGE_TITLE # Return the Response object
return redirect(url_for(new_page, filename=new_document))
# else:
# return redirect(url_for(PAGE_SAVE_AS, filename=current_filename))
# Process GET
if current_document:
# form.filename.data = current_filename
help = get_helps(['save_as_document'])
return render_template('save_as.html',
filename=current_document,
title='Save As',
form=form,
help=help)
else:
flash("No document currently open")
return render_template('index.html')
@home.route('/download', methods=['GET', 'POST'])
@login_required
def download():
form = DownloadEMLForm()
form.filename.choices = list_data_packages(True, True)
# Process POST
if form.validate_on_submit():
filename = form.filename.data
return_value = user_data.download_eml(filename=filename)
if isinstance(return_value, str):
flash(return_value)
else:
return return_value
# Process GET
return render_template('download_eml.html', title='Download EML',
form=form)
@home.route('/check_metadata/<filename>', methods=['GET', 'POST'])
@login_required
def check_metadata(filename:str):
current_document = user_data.get_active_document()
if not current_document:
raise FileNotFoundError
eml_node = load_eml(filename=current_document)
content = check_eml(eml_node, filename)
# Process POST
if request.method == 'POST':
# return render_template(PAGE_CHECK, filename=filename)
return redirect(url_for(PAGE_CHECK, filename=current_document))
else:
set_current_page('check_metadata')
return render_template('check_metadata.html', content=content, title='Check Metadata')
@home.route('/download_current', methods=['GET', 'POST'])
@login_required
def download_current():
current_document = user_data.get_active_document()
if current_document:
# Force the document to be saved, so it gets cleaned
eml_node = load_eml(filename=current_document)
save_both_formats(filename=current_document, eml_node=eml_node)
# Do the download
return_value = user_data.download_eml(filename=current_document)
if isinstance(return_value, str):
flash(return_value)
else:
return return_value
def allowed_data_file(filename):
ALLOWED_EXTENSIONS = set(['csv', 'tsv', 'txt', 'xml', 'ezeml_tmp'])
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def allowed_metadata_file(filename):
ALLOWED_EXTENSIONS = set(['xml'])
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
'''
This function is deprecated. It was originally used as a first
step in a two-step process for data table upload, but that process
has been consolidated into a single step (see the load_data()
function).
@home.route('/upload_data_file', methods=['GET', 'POST'])
@login_required
def upload_data_file():
uploads_folder = get_user_uploads_folder_name()
form = UploadDataFileForm()
# Process POST
if request.method == 'POST' and form.validate_on_submit():
# Check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
if file:
filename = secure_filename(file.filename)
if filename is None or filename == '':
flash('No selected file')
elif allowed_data_file(filename):
file.save(os.path.join(uploads_folder, filename))
flash(f'{filename} uploaded')
else:
flash(f'{filename} is not a supported data file type')
return redirect(request.url)
# Process GET
return render_template('upload_data_file.html', title='Upload Data File',
form=form)
'''
@home.route('/create', methods=['GET', 'POST'])
@login_required
def create():
form = CreateEMLForm()
# Process POST
help = get_helps(['new_eml_document'])
if request.method == 'POST':
if BTN_CANCEL in request.form:
return redirect(get_back_url())
if form.validate_on_submit():
filename = form.filename.data
user_filenames = user_data.get_user_document_list()
if user_filenames and filename and filename in user_filenames:
flash(f'{filename} already exists')
return render_template('create_eml.html', help=help,
form=form)
create_eml(filename=filename)
current_user.set_filename(filename)
current_user.set_packageid(None)
return redirect(url_for(PAGE_TITLE, filename=filename))
# Process GET
return render_template('create_eml.html', help=help, form=form)
@home.route('/open_eml_document', methods=['GET', 'POST'])
@login_required
def open_eml_document():
form = OpenEMLDocumentForm()
form.filename.choices = list_data_packages(False, False)
# Process POST
if request.method == 'POST':
if BTN_CANCEL in request.form:
return redirect(get_back_url())
if form.validate_on_submit():
filename = form.filename.data
eml_node = load_eml(filename)
if eml_node:
current_user.set_filename(filename)
packageid = eml_node.attributes.get('packageId', None)
if packageid:
current_user.set_packageid(packageid)
create_eml(filename=filename)
new_page = PAGE_TITLE
else:
new_page = PAGE_FILE_ERROR
return redirect(url_for(new_page, filename=filename))
# Process GET
return render_template('open_eml_document.html', title='Open EML Document',
form=form)
@home.route('/import_parties', methods=['GET', 'POST'])
@login_required
def import_parties():
form = ImportEMLForm()
form.filename.choices = list_data_packages(True, True)
# Process POST
if request.method == 'POST':
if BTN_CANCEL in request.form:
return redirect(get_back_url())
# new_page = get_redirect_target_page()
# url = url_for(new_page, filename=current_user.get_filename())
# return redirect(url)
if form.validate_on_submit():
filename = form.filename.data
return redirect(url_for('home.import_parties_2', filename=filename))
# Process GET
help = get_helps(['import_responsible_parties'])
return render_template('import_parties.html', help=help, form=form)
def get_redirect_target_page():
current_page = get_current_page()
if current_page == 'title':
return PAGE_TITLE
elif current_page == 'creator':
return PAGE_CREATOR_SELECT
elif current_page == 'metadata_provider':
return PAGE_METADATA_PROVIDER_SELECT
elif current_page == 'associated_party':
return PAGE_ASSOCIATED_PARTY_SELECT
elif current_page == 'abstract':
return PAGE_ABSTRACT
elif current_page == 'keyword':
return PAGE_KEYWORD_SELECT
elif current_page == 'intellectual_rights':
return PAGE_INTELLECTUAL_RIGHTS
elif current_page == 'geographic_coverage':
return PAGE_GEOGRAPHIC_COVERAGE_SELECT
elif current_page == 'temporal_coverage':
return PAGE_TEMPORAL_COVERAGE_SELECT
elif current_page == 'taxonomic_coverage':
return PAGE_TAXONOMIC_COVERAGE_SELECT
elif current_page == 'maintenance':
return PAGE_MAINTENANCE
elif current_page == 'contact':
return PAGE_CONTACT_SELECT
elif current_page == 'publisher':
return PAGE_PUBLISHER
elif current_page == 'publication_info':
return PAGE_PUBLICATION_INFO
elif current_page == 'method_step':
return PAGE_METHOD_STEP_SELECT
elif current_page == 'project':
return PAGE_PROJECT
elif current_page == 'data_table':
return PAGE_DATA_TABLE_SELECT
elif current_page == 'other_entity':
return PAGE_OTHER_ENTITY_SELECT
elif current_page == 'check_metadata':
return PAGE_CHECK
elif current_page == 'export_package':
return PAGE_EXPORT_DATA_PACKAGE
elif current_page == 'data_package_id':
return PAGE_DATA_PACKAGE_ID
elif current_page == 'submit_package':
return PAGE_SUBMIT_TO_EDI
elif current_page == 'send_to_other':
return PAGE_SEND_TO_OTHER
else:
return PAGE_TITLE
@home.route('/import_parties_2/<filename>/', methods=['GET', 'POST'])
@login_required
def import_parties_2(filename):
form = ImportEMLItemsForm()
eml_node = load_eml(filename)
parties = get_responsible_parties_for_import(eml_node)
choices = [[party[2], party[1]] for party in parties]
form.to_import.choices = choices
targets = [
("Creators", "Creators"),
("Metadata Providers", "Metadata Providers"),
("Associated Parties", "Associated Parties"),
("Contacts", "Contacts"),
("Publisher", "Publisher"),
("Project Personnel", "Project Personnel")]
form.target.choices = targets
if request.method == 'POST' and BTN_CANCEL in request.form:
return redirect(get_back_url())
if form.validate_on_submit():
node_ids_to_import = form.data['to_import']
target_class = form.data['target']
target_filename = current_user.get_filename()
import_responsible_parties(target_filename, node_ids_to_import, target_class)
if target_class == 'Creators':
new_page = PAGE_CREATOR_SELECT
elif target_class == 'Metadata Providers':
new_page = PAGE_METADATA_PROVIDER_SELECT
elif target_class == 'Associated Parties':
new_page = PAGE_ASSOCIATED_PARTY_SELECT
elif target_class == 'Contacts':
new_page = PAGE_CONTACT_SELECT
elif target_class == 'Publisher':
new_page = PAGE_PUBLISHER
elif target_class == 'Project Personnel':
new_page = PAGE_PROJECT_PERSONNEL_SELECT
return redirect(url_for(new_page, filename=target_filename))
# Process GET
help = get_helps(['import_responsible_parties_2'])
return render_template('import_parties_2.html', target_filename=filename, help=help, form=form)
def get_responsible_parties_for_import(eml_node):
parties = []
for node in eml_node.find_all_nodes_by_path([names.DATASET, names.CREATOR]):
label = compose_rp_label(node)
parties.append(('Creator', f'{label} (Creator)', node.id))
for node in eml_node.find_all_nodes_by_path([names.DATASET, names.METADATAPROVIDER]):
label = compose_rp_label(node)
parties.append(('Metadata Provider', f'{label} (Metadata Provider)', node.id))
for node in eml_node.find_all_nodes_by_path([names.DATASET, names.ASSOCIATEDPARTY]):
label = compose_rp_label(node)
parties.append(('Associated Party', f'{label} (Associated Party)', node.id))
for node in eml_node.find_all_nodes_by_path([names.DATASET, names.CONTACT]):
label = compose_rp_label(node)
parties.append(('Contact', f'{label} (Contact)', node.id))
for node in eml_node.find_all_nodes_by_path([names.DATASET, names.PUBLISHER]):
label = compose_rp_label(node)
parties.append(('Publisher', f'{label} (Publisher)', node.id))
for node in eml_node.find_all_nodes_by_path([names.DATASET, names.PROJECT, names.PERSONNEL]):
label = compose_rp_label(node)
parties.append(('Project Personnel', f'{label} (Project Personnel)', node.id))
return parties
@home.route('/import_geo_coverage', methods=['GET', 'POST'])
@login_required
def import_geo_coverage():
form = ImportEMLForm()
form.filename.choices = list_data_packages(False, False)
# Process POST
if request.method == 'POST':
if BTN_CANCEL in request.form:
return redirect(get_back_url())
if form.validate_on_submit():
filename = form.filename.data
return redirect(url_for('home.import_geo_coverage_2', filename=filename))
# Process GET
help = get_helps(['import_geographic_coverage'])
return render_template('import_geo_coverage.html', help=help, form=form)
@home.route('/import_geo_coverage_2/<filename>/', methods=['GET', 'POST'])
@login_required
def import_geo_coverage_2(filename):
form = ImportItemsForm()
eml_node = load_eml(filename)
coverages = get_geo_coverages_for_import(eml_node)
choices = [[coverage[1], coverage[0]] for coverage in coverages]
form.to_import.choices = choices
if request.method == 'POST' and BTN_CANCEL in request.form:
return redirect(get_back_url())
if form.validate_on_submit():
node_ids_to_import = form.data['to_import']
target_package = current_user.get_filename()
import_coverage_nodes(target_package, node_ids_to_import)
return redirect(url_for(PAGE_GEOGRAPHIC_COVERAGE_SELECT, filename=target_package))
# Process GET
help = get_helps(['import_geographic_coverage_2'])
return render_template('import_geo_coverage_2.html', help=help, target_filename=filename, form=form)
def get_geo_coverages_for_import(eml_node):
coverages = []
for node in eml_node.find_all_nodes_by_path([names.DATASET, names.COVERAGE, names.GEOGRAPHICCOVERAGE]):
label = compose_full_gc_label(node)
coverages.append((f'{label}', node.id))
return coverages
@home.route('/import_temporal_coverage', methods=['GET', 'POST'])
@login_required
def import_temporal_coverage():
form = ImportEMLForm()
form.filename.choices = list_data_packages(False, False)
# Process POST
if request.method == 'POST':
if BTN_CANCEL in request.form:
return redirect(get_back_url())
# new_page = get_redirect_target_page()
# url = url_for(new_page, filename=current_user.get_filename())
# return redirect(url)
if form.validate_on_submit():
filename = form.filename.data
return redirect(url_for('home.import_temporal_coverage_2', filename=filename))
# Process GET
return render_template('import_temporal_coverage.html', form=form)
@home.route('/import_temporal_coverage_2/<filename>/', methods=['GET', 'POST'])
@login_required
def import_temporal_coverage_2(filename):
form = ImportItemsForm()
eml_node = load_eml(filename)
coverages = get_temporal_coverages_for_import(eml_node)
choices = [[coverage[1], coverage[0]] for coverage in coverages]
form.to_import.choices = choices
if request.method == 'POST' and BTN_CANCEL in request.form:
return redirect(get_back_url())
if form.validate_on_submit():
node_ids_to_import = form.data['to_import']
target_package = current_user.get_filename()
import_coverage_nodes(target_package, node_ids_to_import)
return redirect(url_for(PAGE_TEMPORAL_COVERAGE_SELECT, filename=target_package))
# Process GET
return render_template('import_temporal_coverage_2.html', target_filename=filename, title='Import Metadata',
form=form)
def get_temporal_coverages_for_import(eml_node):
coverages = []
for node in eml_node.find_all_nodes_by_path([names.DATASET, names.COVERAGE, names.TEMPORALCOVERAGE]):
label = compose_full_gc_label(node) # FIXME
coverages.append((f'{label}', node.id))
return coverages
@home.route('/import_taxonomic_coverage', methods=['GET', 'POST'])
@login_required
def import_taxonomic_coverage():
form = ImportEMLForm()
form.filename.choices = list_data_packages(False, False)
# Process POST
if request.method == 'POST':
if BTN_CANCEL in request.form:
return redirect(get_back_url())
if form.validate_on_submit():
filename = form.filename.data
return redirect(url_for('home.import_taxonomic_coverage_2', filename=filename))
# Process GET
help = get_helps(['import_taxonomic_coverage'])
return render_template('import_taxonomic_coverage.html', help=help, form=form)
@home.route('/import_taxonomic_coverage_2/<filename>/', methods=['GET', 'POST'])
@login_required
def import_taxonomic_coverage_2(filename):
form = ImportItemsForm()
eml_node = load_eml(filename)
coverages = get_taxonomic_coverages_for_import(eml_node)
choices = [[coverage[1], coverage[0]] for coverage in coverages]
form.to_import.choices = choices
if request.method == 'POST' and BTN_CANCEL in request.form:
return redirect(get_back_url())
if form.validate_on_submit():
node_ids_to_import = form.data['to_import']
target_package = current_user.get_filename()
import_coverage_nodes(target_package, node_ids_to_import)
return redirect(url_for(PAGE_TAXONOMIC_COVERAGE_SELECT, filename=target_package))
# Process GET
help = get_helps(['import_taxonomic_coverage_2'])
return render_template('import_taxonomic_coverage_2.html', help=help, target_filename=filename, form=form)
def get_taxonomic_coverages_for_import(eml_node):
coverages = []
for node in eml_node.find_all_nodes_by_path([names.DATASET, names.COVERAGE, names.TAXONOMICCOVERAGE]):
label = truncate_middle(compose_taxonomic_label(node), 100, ' ... ')
coverages.append((f'{label}', node.id))
return coverages
@home.route('/import_funding_awards', methods=['GET', 'POST'])
@login_required
def import_funding_awards():
form = ImportEMLForm()
form.filename.choices = list_data_packages(False, False)
# Process POST
if request.method == 'POST':
if BTN_CANCEL in request.form:
return redirect(get_back_url())
if form.validate_on_submit():
filename = form.filename.data
return redirect(url_for('home.import_funding_awards_2', filename=filename))
# Process GET
help = get_helps(['import_funding_awards'])
return render_template('import_funding_awards.html', help=help, form=form)
@home.route('/import_funding_awards_2/<filename>/', methods=['GET', 'POST'])
@login_required
def import_funding_awards_2(filename):
form = ImportItemsForm()
eml_node = load_eml(filename)
coverages = get_funding_awards_for_import(eml_node)
choices = [[coverage[1], coverage[0]] for coverage in coverages]
form.to_import.choices = choices
if request.method == 'POST' and BTN_CANCEL in request.form:
return redirect(get_back_url())
if form.validate_on_submit():
node_ids_to_import = form.data['to_import']
target_package = current_user.get_filename()
import_funding_award_nodes(target_package, node_ids_to_import)
return redirect(url_for(PAGE_FUNDING_AWARD_SELECT, filename=target_package))
# Process GET
help = get_helps(['import_funding_awards_2'])
return render_template('import_funding_awards_2.html', help=help, target_filename=filename, form=form)
def get_funding_awards_for_import(eml_node):
awards = []
award_nodes = eml_node.find_all_nodes_by_path([names.DATASET, names.PROJECT, names.AWARD])
for award_node in award_nodes:
label = truncate_middle(compose_funding_award_label(award_node), 80, ' ... ')
awards.append((f'{label}', award_node.id))
return awards
@home.route('/import_related_projects', methods=['GET', 'POST'])
@login_required
def import_related_projects():
form = ImportEMLForm()
form.filename.choices = list_data_packages(False, False)
# Process POST
if request.method == 'POST':
if BTN_CANCEL in request.form:
return redirect(get_back_url())
if form.validate_on_submit():
filename = form.filename.data
return redirect(url_for('home.import_related_projects_2', filename=filename))
# Process GET
help = get_helps(['import_related_projects'])
return render_template('import_related_projects.html', help=help, form=form)
@home.route('/import_related_projects_2/<filename>/', methods=['GET', 'POST'])
@login_required
def import_related_projects_2(filename):
form = ImportItemsForm()
eml_node = load_eml(filename)
coverages = get_projects_for_import(eml_node)
choices = [[coverage[1], coverage[0]] for coverage in coverages]
form.to_import.choices = choices
if request.method == 'POST' and BTN_CANCEL in request.form:
return redirect(get_back_url())
if form.validate_on_submit():
node_ids_to_import = form.data['to_import']
target_package = current_user.get_filename()
import_project_nodes(target_package, node_ids_to_import)
return redirect(url_for(PAGE_RELATED_PROJECT_SELECT, filename=target_package))
# Process GET
help = get_helps(['import_related_projects_2'])
return render_template('import_related_projects_2.html', help=help, target_filename=filename, form=form)
def get_projects_for_import(eml_node):
projects = []
project = eml_node.find_single_node_by_path([names.DATASET, names.PROJECT])
project_nodes = eml_node.find_all_nodes_by_path([names.DATASET, names.PROJECT, names.RELATED_PROJECT])
project_nodes.append(project)
for project_node in project_nodes:
label = truncate_middle(compose_project_label(project_node), 80, ' ... ')
projects.append((f'{label}', project_node.id))
return projects
def display_decode_error_lines(filename):
errors = []
with open(filename, 'r', errors='replace') as f:
lines = f.readlines()
for index, line in enumerate(lines, start=1):
if "�" in line:
errors.append((index, line))
return errors
def create_ezeml_package_manifest(user_folder, manifest_files):
with open(f'{user_folder}/ezEML_manifest.txt', 'w') as manifest_file:
manifest_file.write(f'ezEML Data Archive Manifest\n')
manifest_file.write(f'ezEML Release {RELEASE_NUMBER}\n')
manifest_file.write(f'--------------------\n')
for filetype, filename, filepath in manifest_files:
manifest_file.write(f'{filetype}\n')
manifest_file.write(f'{filename}\n')
manifest_file.write(f'{get_md5_hash(filepath)}\n')
def zip_package(current_document=None, eml_node=None):
if not current_document:
current_document = current_user.get_filename()
if not current_document:
raise FileNotFoundError
if not eml_node:
eml_node = load_eml(filename=current_document)
user_folder = user_data.get_user_folder_name()
zipfile_name = f'{current_document}.zip'
zipfile_path = os.path.join(user_folder, zipfile_name)
zip_object = ZipFile(zipfile_path, 'w')
manifest_files = []
pathname = f'{user_folder}/{current_document}.json'
arcname = f'{current_document}.json'
zip_object.write(pathname, arcname)
manifest_files.append(('JSON', f'{current_document}.json', pathname))
package_id = user_data.get_active_packageid()
if package_id:
# copy the EML file using the package_id as name
arcname = f'{package_id}.xml'
copyfile(f'{user_folder}/{current_document}.xml', f'{user_folder}/{arcname}')
else:
arcname = f'{current_document}.xml'
# pathname = f'{user_folder}/{current_document}.xml'
pathname = f'{user_folder}/{arcname}'
manifest_files.append(('XML', arcname, pathname))
zip_object.write(pathname, arcname)
create_ezeml_package_manifest(user_folder, manifest_files)
pathname = f'{user_folder}/ezEML_manifest.txt'
arcname = 'ezEML_manifest.txt'
zip_object.write(pathname, arcname)
# get data files
uploads_folder = user_data.get_document_uploads_folder_name()
data_table_nodes = []
eml_node.find_all_descendants(names.DATATABLE, data_table_nodes)
entity_nodes = []
eml_node.find_all_descendants(names.OTHERENTITY, entity_nodes)
data_nodes = data_table_nodes + entity_nodes
for data_node in data_nodes:
object_name_node = data_node.find_single_node_by_path([names.PHYSICAL, names.OBJECTNAME])
if object_name_node:
object_name = object_name_node.content
pathname = f'{uploads_folder}/{object_name}'
arcname = f'data/{object_name}'
try:
zip_object.write(pathname, arcname)
except FileNotFoundError as err:
filename = os.path.basename(err.filename)
msg = f"Unable to archive the package. Missing file: {filename}."
flash(msg, category='error')
return None
zip_object.close()
return zipfile_path
def save_as_ezeml_package_export(archive_file):
current_document = current_user.get_filename()
if not current_document:
raise FileNotFoundError
user_folder = user_data.get_user_folder_name()
# Create the exports folder
timestamp = datetime.now().date().strftime('%Y_%m_%d') + '_' + datetime.now().time().strftime('%H_%M_%S')
export_folder = os.path.join(user_folder, 'exports', current_document, timestamp)
os.makedirs(export_folder, exist_ok=True)
_, archive_basename = os.path.split(archive_file)
src = archive_file
dest = f'{export_folder}/{archive_basename}'
copyfile(src, dest)
parsed_url = urlparse(request.base_url)
download_url = f"{parsed_url.scheme}://{parsed_url.netloc}/{dest}"
return archive_basename, download_url
@home.route('/export_package', methods=['GET', 'POST'])
@login_required
def export_package():
if request.method == 'POST' and BTN_CANCEL in request.form:
return redirect(get_back_url())
current_document, eml_node = reload_metadata() # So check_metadata status is correct
if request.method == 'POST':
save_both_formats(current_document, eml_node)
zipfile_path = zip_package(current_document, eml_node)
if zipfile_path:
archive_basename, download_url = save_as_ezeml_package_export(zipfile_path)
if download_url:
return redirect(url_for('home.export_package_2', package_name=archive_basename,
download_url=get_shortened_url(download_url), safe=''))
archive_basename, download_url = save_as_ezeml_package_export(zipfile_path)
if download_url:
return redirect(url_for('home.export_package_2', package_name=archive_basename,
download_url=get_shortened_url(download_url), safe=''))
# Process GET
help = get_helps(['export_package'])
return render_template('export_package.html', back_url=get_back_url(), title='Export Data Package', help=help)
@home.route('/export_package_2/<package_name>/<path:download_url>', methods=['GET', 'POST'])
@login_required
def export_package_2(package_name, download_url):
if request.method == 'POST' and BTN_CANCEL in request.form:
return redirect(get_back_url())
reload_metadata() # So check_metadata status is correct
return render_template('export_package_2.html', back_url=get_back_url(), title='Export Data Package',
package_name=package_name, download_url=download_url)
def submit_package_mail_body(name=None, email_address=None, archive_name=None, download_url=None, notes=None):
msg = 'Dear EDI Data Curator:' + '\n\n' + \
'This email was auto-generated by ezEML.\n\n\n' + \
'Please submit the following data package to the EDI data repository.\n\n' + \
' Sender\'s name: ' + name + '\n\n' + \
' Sender\'s email: ' + email_address + '\n\n' + \
' Package name: ' + archive_name + '\n\n' + \
' Download URL: ' + get_shortened_url(download_url) + '\n\n' # Note: get_shortened_url handles blanks
if notes:
msg += ' Sender\'s Notes: ' + notes
return msg
def insert_urls(uploads_url_prefix, eml_node, node_type):
upload_nodes = []
eml_node.find_all_descendants(node_type, upload_nodes)
for upload_node in upload_nodes:
try:
physical_node = upload_node.find_descendant(names.PHYSICAL)
object_name_node = physical_node.find_child(names.OBJECTNAME)
object_name = object_name_node.content
distribution_node = physical_node.find_child(names.DISTRIBUTION)
if distribution_node:
physical_node.remove_child(distribution_node)
distribution_node = new_child_node(names.DISTRIBUTION, physical_node)
online_node = new_child_node(names.ONLINE, distribution_node)
url_node = new_child_node(names.URL, online_node)
url_node.add_attribute('function', 'download')
url_node.content = f"{uploads_url_prefix}/{object_name}".replace(' ', '%20')
except Exception as err:
flash(err)
continue
def insert_upload_urls(current_document, eml_node):
user_folder = user_data.get_user_folder_name()
uploads_folder = f'{user_folder}/uploads/{current_document}'
parsed_url = urlparse(request.base_url)
uploads_url_prefix = f"{parsed_url.scheme}://{parsed_url.netloc}/{uploads_folder}"
insert_urls(uploads_url_prefix, eml_node, names.DATATABLE)
insert_urls(uploads_url_prefix, eml_node, names.OTHERENTITY)
@home.route('/submit_package', methods=['GET', 'POST'])
@login_required
def submit_package():
form = SubmitToEDIForm()
if request.method == 'POST' and BTN_CANCEL in request.form:
return redirect(get_back_url())
current_document, eml_node = reload_metadata() # So check_metadata status is correct
if form.validate_on_submit():
# If the user has clicked Save in the EML Documents menu, for example, we want to ignore the
# programmatically generated Submit
if request.form.get(BTN_SUBMIT) == BTN_SUBMIT_TO_EDI:
name = form.data['name']
email_address = form.data['email_address']
notes = form.data['notes']
# update the EML to include URLs to data table files and other entity files
insert_upload_urls(current_document, eml_node)
save_both_formats(filename=current_document, eml_node=eml_node)
zipfile_path = zip_package(current_document, eml_node)
if zipfile_path:
_, download_url = save_as_ezeml_package_export(zipfile_path)
msg = submit_package_mail_body(name, email_address, current_document, download_url, notes)
subject = 'ezEML-Generated Data Submission Request'
to_address = ['support@environmentaldatainitiative.org']
sent = mailout.send_mail(subject=subject, msg=msg, to=to_address)
if sent:
flash(f'Package {current_document} has been sent to EDI. We will notify you when it has been added to the repository.')
else:
flash(f'Email failed to send', 'error')
return redirect(get_back_url())
set_current_page('submit_package')
help = get_helps(['submit_package'])
return render_template('submit_package.html',
title='Send to EDI',
check_metadata_status=get_check_metadata_status(eml_node, current_document),
form=form, help=help)
def get_shortened_url(long_url):
# Note: full URL encoding via urllib.parse.quote causes hideuri to throw an error that URL is invalid.
# So, we just encode blanks.
r = requests.post('https://hideuri.com/api/v1/shorten', data={'url': long_url.replace(' ', '%20')})
try:
r.raise_for_status()
return r.json()['result_url']
except requests.exceptions.HTTPError as e:
return long_url
def send_to_other_email(name, email_address, title, url):
name_quoted = quote(name)
email_address_quoted = quote(email_address)
title_quoted = quote(title)
url = get_shortened_url(url) # Note; get_shortened_url handles blank chars
msg_quoted = f'mailto:{email_address}?subject=ezEML-Generated%20Data%20Package&body=Dear%20{name}%3A%0D%0A%0D%0A' \
f'I%20have%20created%20a%20data%20package%20containing%20EML%20metadata%20and%20associated%20data%20files%20' \
f'for%20your%20inspection.%0D%0A%0D%0ATitle%3A%20%22{title}%22%0D%0A%0D%0AThe%20data%20package%20is%20' \
f'available%20for%20download%20here%3A%20{url}%0D%0A%0D%0AThe%20package%20was%20created%20using%20ezEML.%20' \
f'After%20you%20download%20the%20package%2C%20you%20can%20import%20it%20into%20ezEML%2C%20or%20you%20can%20' \
f'unzip%20it%20to%20extract%20the%20EML%20file%20and%20associated%20data%20files%20to%20work%20with%20them%20' \
f'directly.%0D%0A%0D%0ATo%20learn%20more%20about%20ezEML%2C%20go%20to%20https%3A%2F%2Fezeml.edirepository.org.' \
f'%0D%0A%0D%0AThanks!'
msg_html = Markup(f'Dear {name}:<p><br>'
f'I have created a data package containing EML metadata and associated data files '
f'for your inspection.<p>Title: "{title}"<p>The data package is '
f'available for download here: {url}.<p>The package was created using ezEML. '
f'After you download the package, you can import it into ezEML, or you can '
f'unzip it to extract the EML file and associated data files to work with them '
f'directly.<p>To learn more about ezEML, go to https://ezeml.edirepository.org.'
f'<p>Thanks!')
msg_raw = f'Dear {name}:\n\n' \
f'I have created a data package containing EML metadata and associated data files ' \
f'for your inspection.\n\nTitle: "{title}"\n\nThe data package is ' \
f'available for download here: {url}.\n\nThe package was created using ezEML. ' \
f'After you download the package, you can import it into ezEML, or you can ' \
f'unzip it to extract the EML file and associated data files to work with them ' \
f'directly.\n\nTo learn more about ezEML, go to https://ezeml.edirepository.org.' \
f'\n\nThanks!'
return msg_quoted, msg_html, msg_raw
@home.route('/send_to_other/<filename>/', methods=['GET', 'POST'])
@home.route('/send_to_other/<filename>/<mailto>/', methods=['GET', 'POST'])
@login_required
def send_to_other(filename=None, mailto=None):
form = SendToColleagueForm()
if request.method == 'POST' and BTN_CANCEL in request.form:
return redirect(get_back_url())
current_document, eml_node = reload_metadata() # So check_metadata status is correct
if form.validate_on_submit():
colleague_name = form.data['colleague_name']
email_address = form.data['email_address']
eml_node = load_eml(filename=filename)
dataset_node = eml_node.find_child(child_name=names.DATASET)
title_node = dataset_node.find_child(names.TITLE)
title = ''
if title_node:
title = title_node.content
if not title:
flash('The data package requires a Title', 'error')
return redirect(get_back_url())
zipfile_path = zip_package(current_document, eml_node)
_, download_url = save_as_ezeml_package_export(zipfile_path)
if not mailto:
mailto, mailto_html, mailto_raw = send_to_other_email(colleague_name, email_address, title, download_url)
else:
mailto = None # so we don't pop up the email client when the page is returned to after sending the 1st time
mailto_html = None
mailto_raw=None
eml_node = load_eml(filename=filename)
title_node = eml_node.find_single_node_by_path([names.DATASET, names.TITLE])
if not title_node or not title_node.content:
flash('The data package must have a Title before it can be sent.', 'error')
set_current_page('send_to_other')
if mailto:
form.colleague_name.data = ''
form.email_address.data = ''
help = get_helps(['send_to_colleague_2'])
return render_template('send_to_other_2.html',
title='Send to Other',
mailto=mailto,
mailto_html=mailto_html,
mailto_raw=mailto_raw,
check_metadata_status=get_check_metadata_status(eml_node, current_document),
form=form, help=help)
else:
help = get_helps(['send_to_colleague'])
return render_template('send_to_other.html',
title='Send to Other',
check_metadata_status=get_check_metadata_status(eml_node, current_document),
form=form, help=help)
def get_column_properties(dt_node, object_name):
data_file = object_name
column_vartypes, _, _ = user_data.get_uploaded_table_column_properties(data_file)
if column_vartypes:
return column_vartypes
uploads_folder = user_data.get_document_uploads_folder_name()
num_header_rows = 1
field_delimiter_node = dt_node.find_descendant(names.FIELDDELIMITER)
if field_delimiter_node:
delimiter = field_delimiter_node.content
else:
delimiter = ','
quote_char_node = dt_node.find_descendant(names.QUOTECHARACTER)
if quote_char_node:
quote_char = quote_char_node.content
else:
quote_char = '"'
try:
new_dt_node, new_column_vartypes, new_column_names, new_column_categorical_codes, *_ = load_data_table(
uploads_folder, data_file, num_header_rows, delimiter, quote_char)
user_data.add_uploaded_table_properties(data_file,
new_column_vartypes,
new_column_names,
new_column_categorical_codes)
return new_column_vartypes
except FileNotFoundError:
raise FileNotFoundError('The older version of the data table is missing from our server. Please use "Load Data Table from CSV File" instead of "Re-upload".')
except Exception as err:
raise Exception('Internal error 103')
def check_data_table_similarity(old_dt_node, new_dt_node, new_column_vartypes, new_column_names, new_column_codes):
if not old_dt_node or not new_dt_node:
raise Exception('Internal error 100')
old_attribute_list = old_dt_node.find_child(names.ATTRIBUTELIST)
new_attribute_list = new_dt_node.find_child(names.ATTRIBUTELIST)
if len(old_attribute_list.children) != len(new_attribute_list.children):
raise IndexError('The new table has a different number of columns from the original table.')
document = current_user.get_filename()
old_object_name_node = old_dt_node.find_descendant(names.OBJECTNAME)
if not old_object_name_node:
raise Exception('Internal error 101')
old_object_name = old_object_name_node.content
if not old_object_name:
raise Exception('Internal error 102')
old_column_vartypes, _, _ = user_data.get_uploaded_table_column_properties(old_object_name)
if not old_column_vartypes:
# column properties weren't saved. compute them anew.
old_column_vartypes = get_column_properties(old_dt_node, old_object_name)
if old_column_vartypes != new_column_vartypes:
diffs = []
for col_name, old_type, new_type, attr_node in zip(new_column_names, old_column_vartypes, new_column_vartypes, old_attribute_list.children):
if old_type != new_type:
diffs.append((col_name, old_type, new_type, attr_node))
raise ValueError(diffs)
def substitute_nans(codes):
substituted = []
if codes:
for code in codes:
if isinstance(code, list):
substituted.append(substitute_nans(code))
elif not isinstance(code, float) or not math.isnan(code):
substituted.append(code)
else:
substituted.append('NAN')
else:
substituted.append(None)
return substituted
def compare_codes(old_codes, new_codes):
old_substituted = substitute_nans(old_codes)
new_substituted = substitute_nans(new_codes)
return old_substituted == new_substituted
def add_node_if_missing(parent_node, child_name):
child = parent_node.find_descendant(child_name)
if not child:
child = new_child_node(child_name, parent=parent_node)
return child
def update_data_table(old_dt_node, new_dt_node, new_column_names, new_column_categorical_codes):
debug_msg(f'Entering update_data_table')
if not old_dt_node or not new_dt_node:
return
old_object_name_node = old_dt_node.find_descendant(names.OBJECTNAME)
old_size_node = old_dt_node.find_descendant(names.SIZE)
old_records_node = old_dt_node.find_descendant(names.NUMBEROFRECORDS)
old_md5_node = old_dt_node.find_descendant(names.AUTHENTICATION)
old_field_delimiter_node = old_dt_node.find_descendant(names.FIELDDELIMITER)
old_record_delimiter_node = old_dt_node.find_descendant(names.RECORDDELIMITER)
old_quote_char_node = old_dt_node.find_descendant(names.QUOTECHARACTER)
new_object_name_node = new_dt_node.find_descendant(names.OBJECTNAME)
new_size_node = new_dt_node.find_descendant(names.SIZE)
new_records_node = new_dt_node.find_descendant(names.NUMBEROFRECORDS)
new_md5_node = new_dt_node.find_descendant(names.AUTHENTICATION)
new_field_delimiter_node = new_dt_node.find_descendant(names.FIELDDELIMITER)
new_record_delimiter_node = new_dt_node.find_descendant(names.RECORDDELIMITER)
new_quote_char_node = new_dt_node.find_descendant(names.QUOTECHARACTER)
old_object_name = old_object_name_node.content
old_object_name_node.content = new_object_name_node.content.replace('.ezeml_tmp', '')
old_size_node.content = new_size_node.content
old_records_node.content = new_records_node.content
old_md5_node.content = new_md5_node.content
old_field_delimiter_node.content = new_field_delimiter_node.content
# record delimiter node is not required, so may be missing
if old_record_delimiter_node:
if new_record_delimiter_node:
old_record_delimiter_node.content = new_record_delimiter_node.content
else:
old_record_delimiter_node.parent.remove_child(old_record_delimiter_node)
else:
if new_record_delimiter_node:
# make sure needed ancestor nodes exist
physical_node = add_node_if_missing(old_dt_node, names.PHYSICAL)
data_format_node = add_node_if_missing(physical_node, names.DATAFORMAT)
text_format_node = add_node_if_missing(data_format_node, names.TEXTFORMAT)
new_child_node(names.RECORDDELIMITER, text_format_node).content = new_record_delimiter_node.content
# quote char node is not required, so may be missing
if old_quote_char_node:
if new_quote_char_node:
old_quote_char_node.content = new_quote_char_node.content
else:
old_quote_char_node.parent.remove_child(old_quote_char_node)
else:
if new_quote_char_node:
new_child_node(names.QUOTECHARACTER, old_field_delimiter_node.parent).content = new_quote_char_node.content
_, old_column_names, old_column_categorical_codes = user_data.get_uploaded_table_column_properties(old_object_name)
if old_column_names != new_column_names:
# substitute the new column names
old_attribute_list_node = old_dt_node.find_child(names.ATTRIBUTELIST)
old_attribute_names_nodes = []
old_attribute_list_node.find_all_descendants(names.ATTRIBUTENAME, old_attribute_names_nodes)
for old_attribute_names_node, old_name, new_name in zip(old_attribute_names_nodes, old_column_names, new_column_names):
if old_name != new_name:
debug_None(old_attribute_names_node, 'old_attribute_names_node is None')
old_attribute_names_node.content = new_name
if not compare_codes(old_column_categorical_codes, new_column_categorical_codes):
# need to fix up the categorical codes
old_attribute_list_node = old_dt_node.find_child(names.ATTRIBUTELIST)
old_aattribute_nodes = old_attribute_list_node.find_all_children(names.ATTRIBUTE)
new_attribute_list_node = new_dt_node.find_child(names.ATTRIBUTELIST)
new_attribute_nodes = new_attribute_list_node.find_all_children(names.ATTRIBUTE)
for old_attribute_node, old_codes, new_attribute_node, new_codes in zip(old_aattribute_nodes,
old_column_categorical_codes,
new_attribute_nodes,
new_column_categorical_codes):
if not compare_codes(old_codes, new_codes):
# use the new_codes, preserving any relevant code definitions
# first, get the old codes and their definitions
old_code_definition_nodes = []
old_attribute_node.find_all_descendants(names.CODEDEFINITION, old_code_definition_nodes)
code_definitions = {}
parent_node = None
for old_code_definition_node in old_code_definition_nodes:
code_node = old_code_definition_node.find_child(names.CODE)
code = None
if code_node:
code = str(code_node.content)
definition_node = old_code_definition_node.find_child(names.DEFINITION)
definition = None
if definition_node:
definition = definition_node.content
if code and definition:
code_definitions[code] = definition
# remove the old code definition node
parent_node = old_code_definition_node.parent
parent_node.remove_child(old_code_definition_node)
# add clones of new definition nodes and set their definitions, if known
if not parent_node:
continue
new_code_definition_nodes = []
new_attribute_node.find_all_descendants(names.CODEDEFINITION, new_code_definition_nodes)
for new_code_definition_node in new_code_definition_nodes:
clone = new_code_definition_node.copy()
parent_node.add_child(clone)
clone.parent = parent_node
code_node = clone.find_child(names.CODE)
if code_node:
code = str(code_node.content)
else:
code = None
definition_node = clone.find_child(names.DEFINITION)
definition = code_definitions.get(code)
if definition:
definition_node.content = definition
debug_msg(f'Leaving update_data_table')
def backup_metadata(filename):
user_folder = user_data.get_user_folder_name()
if not user_folder:
flash('User folder not found', 'error')
return
# make sure backups directory exists
backup_path = os.path.join(user_folder, 'backups')
try:
os.mkdir(backup_path)
except FileExistsError:
pass
timestamp = datetime.now().date().strftime('%Y_%m_%d') + '_' + datetime.now().time().strftime('%H_%M_%S')
backup_filename = f'{user_folder}/backups/{filename}.json.{timestamp}'
filename = f'{user_folder}/{filename}.json'
try:
copyfile(filename, backup_filename)
except:
flash(f'Error backing up file {filename}.json', 'error')
@home.route('/import_package', methods=['GET', 'POST'])
@login_required
def import_package():
form = ImportPackageForm()
package_list = user_data.get_user_document_list()
# Process POST
if request.method == 'POST' and BTN_CANCEL in request.form:
return redirect(get_back_url())
reload_metadata() # So check_metadata status is correct
if request.method == 'POST' and form.validate_on_submit():
# Check if the post request has the file part
if 'file' not in request.files:
flash('No file part', 'error')
return redirect(request.url)
file = request.files['file']
if file:
# TODO: Possibly reconsider whether to use secure_filename in the future. It would require
# separately keeping track of the original filename and the possibly modified filename.
# filename = secure_filename(file.filename)
filename = file.filename
if not os.path.splitext(filename)[1] == '.xml':
flash('Please select a file with file extension ".xml".', 'error')
return redirect(request.url)
package_base_filename = os.path.basename(filename)
package_name = os.path.splitext(package_base_filename)[0]
# See if package with that name already exists
try:
unversioned_package_name = upload_ezeml_package(file, package_name)
except FileNotFoundError as err:
# Manifest file is missing
flash(f'The selected file does not appear to be a valid ezEML data package file. '
'Please select a different file or check with the package provider for a corrected file.',
'error')
return redirect(request.url)
except ValueError as err:
# A bad checksum
filename = err.args[0]
flash(f'The selected package appears to have been modified manually outside of ezEML. '
'Please ask the package provider to provide a package file exported directly '
'from ezEML.', 'error')
return redirect(request.url)
if unversioned_package_name in user_data.get_user_document_list():
return redirect(url_for('home.import_package_2', package_name=unversioned_package_name))
else:
import_ezeml_package(unversioned_package_name)
fixup_upload_management()
current_user.set_filename(filename=unversioned_package_name)
return redirect(url_for(PAGE_TITLE, filename=unversioned_package_name))
# Process GET
help = get_helps(['import_package'])
return render_template('import_package.html', title='Import an ezEML Data Package',
packages=package_list, form=form, help=help)
@home.route('/import_package_2/<package_name>', methods=['GET', 'POST'])
@login_required
def import_package_2(package_name):
form = ImportPackageForm()
# Process POST
if request.method == 'POST' and BTN_CANCEL in request.form:
return redirect(get_back_url())
reload_metadata() # So check_metadata status is correct
if request.method == 'POST' and form.validate_on_submit():
form = request.form
if form['replace_copy'] == 'copy':
package_name = copy_ezeml_package(package_name)
import_ezeml_package(package_name)
fixup_upload_management()
current_user.set_filename(filename=package_name)
return redirect(url_for(PAGE_TITLE, filename=package_name))
# Process GET
help = get_helps(['import_package_2'])
return render_template('import_package_2.html', title='Import an ezEML Data Package',
package_name=package_name, form=form, help=help)
def column_names_changed(filepath, delimiter, quote_char, dt_node):
# Assumes CSV file has been saved to the file system
# This function is called only in the reupload case.
data_frame = pd.read_csv(filepath, encoding='utf8', sep=delimiter, quotechar=quote_char, nrows=1)
columns = data_frame.columns
new_column_names = []
for col in columns:
new_column_names.append(col)
old_column_names = []
if dt_node:
attribute_list_node = dt_node.find_child(names.ATTRIBUTELIST)
if attribute_list_node:
for attribute_node in attribute_list_node.children:
attribute_name_node = attribute_node.find_child(names.ATTRIBUTENAME)
if attribute_name_node:
old_column_names.append(attribute_name_node.content)
return old_column_names != new_column_names
@home.route('/reupload_data_with_col_names_changed/<saved_filename>/<dt_node_id>', methods=['GET', 'POST'])
@login_required
def reupload_data_with_col_names_changed(saved_filename, dt_node_id):
form = LoadDataForm()
document = current_user.get_filename()
if request.method == 'POST':
if BTN_CANCEL in request.form:
return redirect(get_back_url())
if BTN_CONTINUE in request.form:
return redirect(url_for(PAGE_REUPLOAD, filename=document, dt_node_id=dt_node_id, saved_filename=saved_filename, name_chg_ok=True), code=307) # 307 keeps it a POST
help = get_helps(['data_table_reupload_full'])
return render_template('reupload_data_with_col_names_changed.html', title='Re-upload Data Table',
form=form, saved_filename=saved_filename, dt_node_id=dt_node_id, help=help)
@home.route('/load_data/<filename>', methods=['GET', 'POST'])
@login_required
def load_data(filename=None):
log_info(f'Entering load_data: request.method={request.method}')
# filename that's passed in is actually the document name, for historical reasons.
# We'll clear it to avoid misunderstandings...
filename = None
form = LoadDataForm()
document = current_user.get_filename()
uploads_folder = user_data.get_document_uploads_folder_name()
# Process POST
if request.method == 'POST' and BTN_CANCEL in request.form:
return redirect(get_back_url())
if request.method == 'POST' and form.validate_on_submit():
# Check if the post request has the file part
if 'file' not in request.files:
flash('No file part', 'error')
return redirect(request.url)
eml_node = load_eml(filename=document)
dataset_node = eml_node.find_child(names.DATASET)
if not dataset_node:
dataset_node = new_child_node(names.DATASET, eml_node)
file = request.files['file']
if file:
filename = file.filename
if filename:
if filename is None or filename == '':
flash('No selected file', 'error')
elif allowed_data_file(filename):
# Make sure the user's uploads directory exists
Path(uploads_folder).mkdir(parents=True, exist_ok=True)
filepath = os.path.join(uploads_folder, filename)
if file:
# Upload the file to the uploads directory
file.save(filepath)
num_header_rows = 1
delimiter = form.delimiter.data
quote_char = form.quote.data
try:
dt_node, new_column_vartypes, new_column_names, new_column_categorical_codes, *_ = \
load_data_table(uploads_folder, filename, num_header_rows, delimiter, quote_char)
except UnicodeDecodeError as err:
errors = display_decode_error_lines(filepath)
return render_template('encoding_error.html', filename=filename, errors=errors)
except DataTableError as err:
flash(f'Data table has an error: {err.message}', 'error')
return redirect(request.url)
flash(f"Loaded {filename}")
dt_node.parent = dataset_node
dataset_node.add_child(dt_node)
user_data.add_data_table_upload_filename(filename)
if new_column_vartypes:
user_data.add_uploaded_table_properties(filename,
new_column_vartypes,
new_column_names,
new_column_categorical_codes)
delete_data_files(uploads_folder)
save_both_formats(filename=document, eml_node=eml_node)
return redirect(url_for(PAGE_DATA_TABLE, filename=document, dt_node_id=dt_node.id, delimiter=delimiter, quote_char=quote_char))
else:
flash(f'{filename} is not a supported data file type')
return redirect(request.url)
# Process GET
return render_template('load_data.html', title='Load Data',
form=form)
def handle_reupload(dt_node_id=None, saved_filename=None, document=None,
eml_node=None, uploads_folder=None, name_chg_ok=False,
delimiter=None, quote_char=None):
dataset_node = eml_node.find_child(names.DATASET)
if not dataset_node:
dataset_node = new_child_node(names.DATASET, eml_node)
if not saved_filename:
raise MissingFileError('Unexpected error: file not found')
dt_node = Node.get_node_instance(dt_node_id)
num_header_rows = 1
filepath = os.path.join(uploads_folder, saved_filename)
if not name_chg_ok:
if column_names_changed(filepath, delimiter, quote_char, dt_node):
# Go get confirmation
return redirect(url_for(PAGE_REUPLOAD_WITH_COL_NAMES_CHANGED,
saved_filename=saved_filename,
dt_node_id=dt_node_id),
code=307)
try:
new_dt_node, new_column_vartypes, new_column_names, new_column_categorical_codes, *_ = load_data_table(
uploads_folder, saved_filename, num_header_rows, delimiter, quote_char)
types_changed = None
try:
check_data_table_similarity(dt_node,
new_dt_node,
new_column_vartypes,
new_column_names,
new_column_categorical_codes)
except ValueError as err:
types_changed = err.args[0]
except FileNotFoundError as err:
error = err.args[0]
flash(error, 'error')
return redirect(url_for(PAGE_DATA_TABLE_SELECT, filename=document))
except IndexError as err:
error = err.args[0]
flash(f'Re-upload not done. {error}', 'error')
return redirect(url_for(PAGE_DATA_TABLE_SELECT, filename=document))
try:
# use the existing dt_node, but update objectName, size, rows, MD5, etc.
# also, update column names and categorical codes, as needed
update_data_table(dt_node, new_dt_node, new_column_names, new_column_categorical_codes)
# rename the temp file
os.rename(filepath, filepath.replace('.ezeml_tmp', ''))
if types_changed:
err_string = 'Please note: One or more columns in the new table have a different data type than they had in the old table.<ul>'
for col_name, old_type, new_type, attr_node in types_changed:
dt.change_measurement_scale(attr_node, old_type.name, new_type.name)
err_string += f'<li><b>{col_name}</b> changed from {old_type.name} to {new_type.name}'
err_string += '</ul>'
flash(Markup(err_string))
except Exception as err:
# display error
error = err.args[0]
flash(f"Data table could not be re-uploaded. {error}", 'error')
return redirect(url_for(PAGE_DATA_TABLE_SELECT, filename=document))
except UnicodeDecodeError as err:
errors = display_decode_error_lines(filepath)
return render_template('encoding_error.html', filename=document, errors=errors)
except DataTableError as err:
flash(f'Data table has an error: {err.message}', 'error')
return redirect(request.url)
data_file = saved_filename.replace('.ezeml_tmp', '')
flash(f"Loaded {data_file}")
dt_node.parent = dataset_node
object_name_node = dt_node.find_descendant(names.OBJECTNAME)
if object_name_node:
object_name_node.content = data_file
user_data.add_data_table_upload_filename(data_file)
if new_column_vartypes:
user_data.add_uploaded_table_properties(data_file,
new_column_vartypes,
new_column_names,
new_column_categorical_codes)
delete_data_files(uploads_folder)
backup_metadata(filename=document)
save_both_formats(filename=document, eml_node=eml_node)
return redirect(url_for(PAGE_DATA_TABLE, filename=document, dt_node_id=dt_node.id, delimiter=delimiter,
quote_char=quote_char))
@home.route('/reupload_data/<filename>/<dt_node_id>', methods=['GET', 'POST'])
@home.route('/reupload_data/<filename>/<dt_node_id>/<saved_filename>/<name_chg_ok>', methods=['GET', 'POST'])
@login_required
def reupload_data(dt_node_id=None, filename=None, saved_filename=None, name_chg_ok=False):
# filename that's passed in is actually the document name, for historical reasons.
# We'll clear it to avoid misunderstandings...
filename = None
form = LoadDataForm()
document = current_user.get_filename()
uploads_folder = user_data.get_document_uploads_folder_name()
eml_node = load_eml(filename=document)
data_table_name = ''
dt_node = Node.get_node_instance(dt_node_id)
if dt_node:
entity_name_node = dt_node.find_child(names.ENTITYNAME)
if entity_name_node:
data_table_name = entity_name_node.content
if not data_table_name:
flash(f'Data table name not found in the metadata.', 'error')
return redirect(request.url)
if request.method == 'POST' and BTN_CANCEL in request.form:
url = url_for(PAGE_DATA_TABLE_SELECT, filename=document)
return redirect(url)
if request.method == 'POST':
if dt_node:
if saved_filename:
filename = saved_filename
else:
file = request.files['file']
if file:
filename = f"{file.filename}"
if allowed_data_file(filename):
# We upload the new version of the CSV file under a temp name so we have both files to inspect.
filename = f"{filename}.ezeml_tmp"
filepath = os.path.join(uploads_folder, filename)
file.save(filepath)
else:
flash(f'{filename} is not a supported data file type', 'error')
return redirect(request.url)
delimiter = form.delimiter.data
quote_char = form.quote.data
try:
return handle_reupload(dt_node_id=dt_node_id, saved_filename=filename, document=document,
eml_node=eml_node, uploads_folder=uploads_folder, name_chg_ok=name_chg_ok,
delimiter=delimiter, quote_char=quote_char)
except MissingFileError as err:
flash(err.message, 'error')
return redirect(request.url)
except Exception as err:
return redirect(request.url)
# Process GET
help = get_helps(['data_table_reupload_full'])
return render_template('reupload_data.html', title='Re-upload Data Table',
form=form, name=data_table_name, help=help)
@home.route('/reupload_other_entity/<filename>/<node_id>', methods=['GET', 'POST'])
@login_required
def reupload_other_entity(filename, node_id):
form = LoadOtherEntityForm()
document = current_user.get_filename()
uploads_folder = user_data.get_document_uploads_folder_name()
eml_node = load_eml(filename=document)
other_entity_name = ''
oe_node = Node.get_node_instance(node_id)
if oe_node:
entity_name_node = oe_node.find_child(names.ENTITYNAME)
if entity_name_node:
other_entity_name = entity_name_node.content
if not other_entity_name:
raise ValueError("Other entity's name not found")
if request.method == 'POST' and BTN_CANCEL in request.form:
url = url_for(PAGE_OTHER_ENTITY_SELECT, filename=filename)
return redirect(url)
if request.method == 'POST':
return redirect(url_for(PAGE_LOAD_OTHER_ENTITY, node_id=node_id), code=307) # 307 keeps it a POST
help = get_helps(['data_table_reupload_full']) # FIXME
return render_template('reupload_other_entity.html', title='Re-upload Other Entity',
form=form, name=other_entity_name, help=help)
@home.route('/load_other_entity/<node_id>', methods=['GET', 'POST'])
@login_required
def load_entity(node_id=None):
form = LoadOtherEntityForm()
document = current_user.get_filename()
uploads_folder = user_data.get_document_uploads_folder_name()
# Process POST
if request.method == 'POST' and BTN_CANCEL in request.form:
return redirect(get_back_url())
if request.method == 'POST' and form.validate_on_submit():
# Check if the post request has the file part
if 'file' not in request.files:
flash('No file part', 'error')
return redirect(request.url)
file = request.files['file']
if file:
# TODO: Possibly reconsider whether to use secure_filename in the future. It would require
# separately keeping track of the original filename and the possibly modified filename.
# filename = secure_filename(file.filename)
filename = file.filename
if filename is None or filename == '':
flash('No selected file', 'error')
else:
file.save(os.path.join(uploads_folder, filename))
data_file = filename
data_file_path = f'{uploads_folder}/{data_file}'
flash(f'Loaded {data_file}')
eml_node = load_eml(filename=document)
dataset_node = eml_node.find_child(names.DATASET)
other_entity_node = load_other_entity(dataset_node, uploads_folder, data_file, node_id=node_id)
save_both_formats(filename=document, eml_node=eml_node)
return redirect(url_for(PAGE_OTHER_ENTITY, filename=document, node_id=other_entity_node.id))
# Process GET
return render_template('load_other_entity.html', title='Load Other Entity',
form=form)
@home.route('/load_metadata', methods=['GET', 'POST'])
@login_required
def load_metadata():
form = LoadMetadataForm()
document = current_user.get_filename()
uploads_folder = user_data.get_document_uploads_folder_name()
# Process POST
if request.method == 'POST' and form.validate_on_submit():
# Check if the post request has the file part
if 'file' not in request.files:
flash('No file part', 'error')
return redirect(request.url)
file = request.files['file']
if file:
# TODO: Possibly reconsider whether to use secure_filename in the future. It would require
# separately keeping track of the original filename and the possibly modified filename.
# filename = secure_filename(file.filename)
# filename = file.filename
filename = secure_filename(file.filename)
if filename is None or filename == '':
flash('No selected file', 'error')
elif allowed_metadata_file(filename):
Path(uploads_folder).mkdir(parents=True, exist_ok=True)
file.save(os.path.join(uploads_folder, filename))
metadata_file = filename
metadata_file_path = f'{uploads_folder}/{metadata_file}'
with open(metadata_file_path, 'r') as file:
metadata_str = file.read()
try:
eml_node = read_xml(metadata_str)
except Exception as e:
flash(e, 'error')
if eml_node:
packageid = eml_node.attribute_value('packageId')
if packageid:
current_user.set_packageid(packageid)
save_both_formats(filename=filename, eml_node=eml_node)
return redirect(url_for(PAGE_TITLE, filename=filename))
else:
flash(f'Unable to determine packageid from file {filename}', 'error')
else:
flash(f'Unable to load metadata from file {filename}', 'error')
else:
flash(f'{filename} is not a supported data file type', 'error')
return redirect(request.url)
# Process GET
return render_template('load_metadata.html', title='Load Metadata',
form=form)
@home.route('/close', methods=['GET', 'POST'])
@login_required
def close():
current_document = current_user.get_filename()
if current_document:
current_user.set_filename(None)
flash(f'Closed {current_document}')
else:
flash("There was no package open")
set_current_page('')
return render_template('index.html')
def select_post(filename=None, form=None, form_dict=None,
method=None, this_page=None, back_page=None,
next_page=None, edit_page=None, project_node_id=None, reupload_page=None):
node_id = None
new_page = None
if form_dict:
for key in form_dict:
val = form_dict[key][0] # value is the first list element
if val in (BTN_BACK, BTN_DONE):
new_page = back_page
elif val[0:4] == BTN_BACK:
node_id = project_node_id
new_page = back_page
elif val in [BTN_NEXT, BTN_SAVE_AND_CONTINUE]:
node_id = project_node_id
new_page = next_page
elif val == BTN_EDIT:
new_page = edit_page
node_id = key
elif val == BTN_REMOVE:
new_page = this_page
node_id = key
eml_node = load_eml(filename=filename)
# Get the data table filename, if any, so we can remove it from the uploaded list
# dt_node = Node.get_node_instance(node_id)
# if dt_node and dt_node.name == names.DATATABLE:
# object_name_node = dt_node.find_single_node_by_path([names.PHYSICAL, names.OBJECTNAME])
# if object_name_node:
# object_name = object_name_node.content
# if object_name:
# user_data.discard_data_table_upload_filename(object_name)
remove_child(node_id=node_id)
node_id = project_node_id # for relatedProject case
save_both_formats(filename=filename, eml_node=eml_node)
elif val == BTN_REUPLOAD:
node_id = key
if reupload_page:
new_page = reupload_page
else:
node_id = key
new_page = PAGE_REUPLOAD
elif val == BTN_HIDDEN_CHECK:
new_page = PAGE_CHECK
elif val == BTN_HIDDEN_SAVE:
new_page = this_page
elif val == BTN_HIDDEN_DOWNLOAD:
new_page = PAGE_DOWNLOAD
elif val == BTN_HIDDEN_NEW:
new_page = PAGE_CREATE
elif val == BTN_HIDDEN_OPEN:
new_page = PAGE_OPEN
elif val == BTN_HIDDEN_CLOSE:
new_page = PAGE_CLOSE
elif val == UP_ARROW:
new_page = this_page
node_id = key
process_up_button(filename, node_id)
elif val == DOWN_ARROW:
new_page = this_page
node_id = key
process_down_button(filename, node_id)
elif val[0:3] == BTN_ADD:
new_page = edit_page
node_id = '1'
elif val == BTN_LOAD_DATA_TABLE:
new_page = PAGE_LOAD_DATA
node_id = '1'
elif val == BTN_LOAD_GEO_COVERAGE:
new_page = PAGE_LOAD_GEO_COVERAGE
node_id = '1'
elif val == BTN_LOAD_OTHER_ENTITY:
new_page = PAGE_LOAD_OTHER_ENTITY
node_id = '1'
elif val == BTN_REUSE:
new_page = PAGE_IMPORT_PARTY
node_id = '1'
if form.validate_on_submit():
if new_page in [PAGE_DATA_TABLE, PAGE_LOAD_DATA, PAGE_REUPLOAD, PAGE_REUPLOAD_WITH_COL_NAMES_CHANGED ]:
return url_for(new_page, filename=filename, dt_node_id=node_id, project_node_id=project_node_id)
else:
return url_for(new_page, filename=filename, node_id=node_id, project_node_id=project_node_id)
def process_up_button(filename:str=None, node_id:str=None):
process_updown_button(filename, node_id, move_up)
def process_down_button(filename:str=None, node_id:str=None):
process_updown_button(filename, node_id, move_down)
def process_updown_button(filename:str=None, node_id:str=None, move_function=None):
if filename and node_id and move_function:
eml_node = load_eml(filename=filename)
child_node = Node.get_node_instance(node_id)
if child_node:
parent_node = child_node.parent
if parent_node:
move_function(parent_node, child_node)
save_both_formats(filename=filename, eml_node=eml_node)
def compare_begin_end_dates(begin_date_str:str=None, end_date_str:str=None):
begin_date = None
end_date = None
flash_msg = None
if len(begin_date_str) == 4:
begin_date_str += '-01-01'
if len(end_date_str) == 4:
end_date_str += '-01-01'
# date.fromisoformat() is a Python 3.7 feature
#if begin_date_str and end_date_str:
#begin_date = date.fromisoformat(begin_date_str)
#end_date = date.fromisoformat(end_date_str)
if begin_date and end_date and begin_date > end_date:
flash_msg = 'Begin date should be less than or equal to end date'
if end_date:
today_date = date.today()
if end_date > today_date:
msg = "End date should not be greater than today's date"
if flash_msg:
flash_msg += "; " + msg
else:
flash_msg = msg
return flash_msg
def set_current_page(page):
session['current_page'] = page
def get_current_page():
return session.get('current_page')
| webapp/home/views.py | 91,546 | The edit page allows for direct editing of a top-level element such as
title, abstract, creators, etc. This function simply redirects to the
specified page, passing the packageid as the only parameter.
:Mod: views.py
:Synopsis:
:Author:
costa
servilla
ide
:Created:
7/23/18
!/usr/bin/env python -*- coding: utf-8 -*- if we've just deleted the current document, it won't exist Call load_eml here to get the check_metadata status set correctly loop on the various users' data directories look at the EML model json files some directories may have obsolete 'user_properties.json' files create a subdir of the user's uploads directory for this document's uploads open the model file look at data tables look at other entities clean up temp files now capture all uploaded file names in the user data add the uploaded files to the user data clean up temp files now we can delete the files we've copied special case for supporting base.html template Process POST Process GET Determine POST type Process POST Revert back to the old filename Revert back to the old filename Return the Response object else: return redirect(url_for(PAGE_SAVE_AS, filename=current_filename)) Process GET form.filename.data = current_filename Process POST Process GET Process POST return render_template(PAGE_CHECK, filename=filename) Force the document to be saved, so it gets cleaned Do the download Process POST Process GET Process POST Process GET Process POST new_page = get_redirect_target_page() url = url_for(new_page, filename=current_user.get_filename()) return redirect(url) Process GET Process GET Process POST Process GET Process GET Process POST new_page = get_redirect_target_page() url = url_for(new_page, filename=current_user.get_filename()) return redirect(url) Process GET Process GET FIXME Process POST Process GET Process GET Process POST Process GET Process GET Process POST Process GET Process GET copy the EML file using the package_id as name pathname = f'{user_folder}/{current_document}.xml' get data files Create the exports folder So check_metadata status is correct Process GET So check_metadata status is correct Note: get_shortened_url handles blanks So check_metadata status is correct If the user has clicked Save in the EML Documents menu, for example, we want to ignore the programmatically generated Submit update the EML to include URLs to data table files and other entity files Note: full URL encoding via urllib.parse.quote causes hideuri to throw an error that URL is invalid. So, we just encode blanks. Note; get_shortened_url handles blank chars So check_metadata status is correct so we don't pop up the email client when the page is returned to after sending the 1st time column properties weren't saved. compute them anew. record delimiter node is not required, so may be missing make sure needed ancestor nodes exist quote char node is not required, so may be missing substitute the new column names need to fix up the categorical codes use the new_codes, preserving any relevant code definitions first, get the old codes and their definitions remove the old code definition node add clones of new definition nodes and set their definitions, if known make sure backups directory exists Process POST So check_metadata status is correct Check if the post request has the file part TODO: Possibly reconsider whether to use secure_filename in the future. It would require separately keeping track of the original filename and the possibly modified filename. filename = secure_filename(file.filename) See if package with that name already exists Manifest file is missing A bad checksum Process GET Process POST So check_metadata status is correct Process GET Assumes CSV file has been saved to the file system This function is called only in the reupload case. 307 keeps it a POST filename that's passed in is actually the document name, for historical reasons. We'll clear it to avoid misunderstandings... Process POST Check if the post request has the file part Make sure the user's uploads directory exists Upload the file to the uploads directory Process GET Go get confirmation use the existing dt_node, but update objectName, size, rows, MD5, etc. also, update column names and categorical codes, as needed rename the temp file display error filename that's passed in is actually the document name, for historical reasons. We'll clear it to avoid misunderstandings... We upload the new version of the CSV file under a temp name so we have both files to inspect. Process GET 307 keeps it a POST FIXME Process POST Check if the post request has the file part TODO: Possibly reconsider whether to use secure_filename in the future. It would require separately keeping track of the original filename and the possibly modified filename. filename = secure_filename(file.filename) Process GET Process POST Check if the post request has the file part TODO: Possibly reconsider whether to use secure_filename in the future. It would require separately keeping track of the original filename and the possibly modified filename. filename = secure_filename(file.filename) filename = file.filename Process GET value is the first list element Get the data table filename, if any, so we can remove it from the uploaded list dt_node = Node.get_node_instance(node_id) if dt_node and dt_node.name == names.DATATABLE: object_name_node = dt_node.find_single_node_by_path([names.PHYSICAL, names.OBJECTNAME]) if object_name_node: object_name = object_name_node.content if object_name: user_data.discard_data_table_upload_filename(object_name) for relatedProject case date.fromisoformat() is a Python 3.7 featureif begin_date_str and end_date_str:begin_date = date.fromisoformat(begin_date_str)end_date = date.fromisoformat(end_date_str) | 5,800 | en | 0.794373 |
#!/usr/bin/python3
import argparse
import os
import shutil
import sys
import traceback
from multiprocessing import Pool, cpu_count
from os.path import expanduser
import time
from typing import Tuple
from colorama import Fore
from atcodertools.client.atcoder import AtCoderClient, Contest, LoginError, PageNotFoundError
from atcodertools.client.models.problem import Problem
from atcodertools.client.models.problem_content import InputFormatDetectionError, SampleDetectionError
from atcodertools.codegen.code_style_config import DEFAULT_WORKSPACE_DIR_PATH
from atcodertools.codegen.models.code_gen_args import CodeGenArgs
from atcodertools.common.language import ALL_LANGUAGES, CPP
from atcodertools.common.logging import logger
from atcodertools.config.config import Config
from atcodertools.constprediction.constants_prediction import predict_constants
from atcodertools.fileutils.create_contest_file import create_examples, \
create_code
from atcodertools.fmtprediction.models.format_prediction_result import FormatPredictionResult
from atcodertools.fmtprediction.predict_format import NoPredictionResultError, \
MultiplePredictionResultsError, predict_format
from atcodertools.tools import get_default_config_path
from atcodertools.tools.models.metadata import Metadata
from atcodertools.tools.utils import with_color
from atcodertools.config.config import ConfigType
class BannedFileDetectedError(Exception):
pass
class EnvironmentInitializationError(Exception):
pass
def output_splitter():
# for readability
print("=================================================", file=sys.stderr)
def _message_on_execution(cwd: str, cmd: str):
return "Executing the following command in `{}`: {}".format(cwd, cmd)
def prepare_procedure(atcoder_client: AtCoderClient,
problem: Problem,
config: Config):
workspace_root_path = config.code_style_config.workspace_dir
template_code_path = config.code_style_config.template_file
lang = config.code_style_config.lang
pid = problem.get_alphabet()
problem_dir_path = os.path.join(
workspace_root_path,
problem.get_contest().get_id(),
pid)
def emit_error(text):
logger.error(with_color("Problem {}: {}".format(pid, text), Fore.RED))
def emit_warning(text):
logger.warning("Problem {}: {}".format(pid, text))
def emit_info(text):
logger.info("Problem {}: {}".format(pid, text))
# Return if a directory for the problem already exists
if config.etc_config.skip_existing_problems:
if os.path.exists(problem_dir_path):
emit_info(
f"Skipped preparation because the directory already exists: {problem_dir_path}")
return
emit_info('{} is used for template'.format(template_code_path))
# Fetch problem data from the statement
try:
content = atcoder_client.download_problem_content(problem)
except InputFormatDetectionError as e:
emit_error("Failed to download input format.")
raise e
except SampleDetectionError as e:
emit_error("Failed to download samples.")
raise e
# Store examples to the directory path
if len(content.get_samples()) == 0:
emit_info("No samples.")
else:
os.makedirs(problem_dir_path, exist_ok=True)
create_examples(content.get_samples(), problem_dir_path,
config.etc_config.in_example_format, config.etc_config.out_example_format)
emit_info("Created examples.")
code_file_path = os.path.join(
problem_dir_path,
"main.{}".format(lang.extension))
# If there is an existing code, just create backup
if os.path.exists(code_file_path):
backup_id = 1
while True:
backup_name = "{}.{}".format(code_file_path, backup_id)
if not os.path.exists(backup_name):
new_path = backup_name
shutil.copy(code_file_path, backup_name)
break
backup_id += 1
emit_info(
"Backup for existing code '{}' -> '{}'".format(
code_file_path,
new_path))
try:
prediction_result = predict_format(content)
emit_info(
with_color("Format prediction succeeded", Fore.LIGHTGREEN_EX))
except (NoPredictionResultError, MultiplePredictionResultsError) as e:
prediction_result = FormatPredictionResult.empty_result()
if isinstance(e, NoPredictionResultError):
msg = "No prediction -- Failed to understand the input format"
else:
msg = "Too many prediction -- Failed to understand the input format"
emit_warning(with_color(msg, Fore.LIGHTRED_EX))
constants = predict_constants(content.original_html)
code_generator = config.code_style_config.code_generator
with open(template_code_path, "r") as f:
template = f.read()
create_code(code_generator(
CodeGenArgs(
template,
prediction_result.format,
constants,
config.code_style_config
)),
code_file_path)
emit_info("Saved code to {}".format(code_file_path))
# Save metadata
metadata_path = os.path.join(problem_dir_path, "metadata.json")
Metadata(problem,
os.path.basename(code_file_path),
config.etc_config.in_example_format.replace("{}", "*"),
config.etc_config.out_example_format.replace("{}", "*"),
lang,
constants.judge_method,
constants.timeout
).save_to(metadata_path)
emit_info("Saved metadata to {}".format(metadata_path))
if config.postprocess_config.exec_cmd_on_problem_dir is not None:
emit_info(_message_on_execution(problem_dir_path,
config.postprocess_config.exec_cmd_on_problem_dir))
config.postprocess_config.execute_on_problem_dir(
problem_dir_path)
output_splitter()
def func(argv: Tuple[AtCoderClient, Problem, Config]):
atcoder_client, problem, config = argv
prepare_procedure(atcoder_client, problem, config)
def prepare_contest(atcoder_client: AtCoderClient,
contest_id: str,
config: Config,
retry_delay_secs: float = 1.5,
retry_max_delay_secs: float = 60,
retry_max_tries: int = 10):
attempt_count = 1
while True:
try:
problem_list = atcoder_client.download_problem_list(
Contest(contest_id=contest_id))
break
except PageNotFoundError:
if 0 < retry_max_tries < attempt_count:
raise EnvironmentInitializationError
logger.warning(
"Failed to fetch. Will retry in {} seconds. (Attempt {})".format(retry_delay_secs, attempt_count))
time.sleep(retry_delay_secs)
retry_delay_secs = min(retry_delay_secs * 2, retry_max_delay_secs)
attempt_count += 1
tasks = [(atcoder_client,
problem,
config) for
problem in problem_list]
output_splitter()
if config.etc_config.parallel_download:
thread_pool = Pool(processes=cpu_count())
thread_pool.map(func, tasks)
else:
for argv in tasks:
try:
func(argv)
except Exception:
# Prevent the script from stopping
print(traceback.format_exc(), file=sys.stderr)
pass
if config.postprocess_config.exec_cmd_on_contest_dir is not None:
contest_dir_path = os.path.join(
config.code_style_config.workspace_dir, contest_id)
logger.info(_message_on_execution(contest_dir_path,
config.postprocess_config.exec_cmd_on_contest_dir))
config.postprocess_config.execute_on_contest_dir(
contest_dir_path)
USER_CONFIG_PATH = os.path.join(
expanduser("~"), ".atcodertools.toml")
def get_config(args: argparse.Namespace) -> Config:
def _load(path: str) -> Config:
logger.info("Going to load {} as config".format(path))
with open(path, 'r') as f:
return Config.load(f, {ConfigType.CODESTYLE, ConfigType.POSTPROCESS, ConfigType.ETC}, args)
if args.config:
return _load(args.config)
if os.path.exists(USER_CONFIG_PATH):
return _load(USER_CONFIG_PATH)
return _load(get_default_config_path())
class DeletedFunctionalityError(Exception):
pass
def main(prog, args):
parser = argparse.ArgumentParser(
prog=prog,
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("contest_id",
help="Contest ID (e.g. arc001)")
parser.add_argument("--without-login",
action="store_true",
help="Download data without login")
parser.add_argument("--workspace",
help="Path to workspace's root directory. This script will create files"
" in {{WORKSPACE}}/{{contest_name}}/{{alphabet}}/ e.g. ./your-workspace/arc001/A/\n"
"[Default] {}".format(DEFAULT_WORKSPACE_DIR_PATH))
parser.add_argument("--lang",
help="Programming language of your template code, {}.\n"
.format(" or ".join([lang.name for lang in ALL_LANGUAGES])) + "[Default] {}".format(CPP.name))
parser.add_argument("--template",
help="File path to your template code\n{}".format(
"\n".join(
["[Default ({dname})] {path}".format(
dname=lang.display_name,
path=lang.default_template_path
) for lang in ALL_LANGUAGES]
))
)
# Deleted functionality
parser.add_argument('--replacement', help=argparse.SUPPRESS)
parser.add_argument("--parallel",
action="store_true",
help="Prepare problem directories asynchronously using multi processors.",
default=None)
parser.add_argument("--save-no-session-cache",
action="store_true",
help="Save no session cache to avoid security risk",
default=None)
parser.add_argument("--skip-existing-problems",
action="store_true",
help="Skip processing every problem for which a directory already exists",
default=None)
parser.add_argument("--config",
help="File path to your config file\n{0}{1}".format("[Default (Primary)] {}\n".format(
USER_CONFIG_PATH),
"[Default (Secondary)] {}\n".format(
get_default_config_path()))
)
args = parser.parse_args(args)
if args.replacement is not None:
logger.error(with_color("Sorry! --replacement argument no longer exists"
" and you can only use --template."
" See the official document for details.", Fore.LIGHTRED_EX))
raise DeletedFunctionalityError
config = get_config(args)
try:
import AccountInformation # noqa
raise BannedFileDetectedError(
"We abolished the logic with AccountInformation.py. Please delete the file.")
except ImportError:
pass
client = AtCoderClient()
if not config.etc_config.download_without_login:
try:
client.login(
save_session_cache=not config.etc_config.save_no_session_cache)
logger.info("Login successful.")
except LoginError:
logger.error(
"Failed to login (maybe due to wrong username/password combination?)")
sys.exit(-1)
else:
logger.info("Downloading data without login.")
prepare_contest(client,
args.contest_id,
config)
if __name__ == "__main__":
main(sys.argv[0], sys.argv[1:])
| atcodertools/tools/envgen.py | 12,407 | !/usr/bin/python3 for readability Return if a directory for the problem already exists Fetch problem data from the statement Store examples to the directory path If there is an existing code, just create backup Save metadata Prevent the script from stopping Deleted functionality noqa | 284 | en | 0.763652 |
import os
import torch
import torch.nn.functional as F
import torch.nn as nn
import math
from modeling.sync_batchnorm.batchnorm import SynchronizedBatchNorm2d
import torch.utils.model_zoo as model_zoo
def conv_bn(inp, oup, stride, BatchNorm):
return nn.Sequential(
nn.Conv2d(inp, oup, 3, stride, 1, bias=False),
BatchNorm(oup),
nn.ReLU6(inplace=True)
)
def fixed_padding(inputs, kernel_size, dilation):
kernel_size_effective = kernel_size + (kernel_size - 1) * (dilation - 1)
pad_total = kernel_size_effective - 1
pad_beg = pad_total // 2
pad_end = pad_total - pad_beg
padded_inputs = F.pad(inputs, (pad_beg, pad_end, pad_beg, pad_end))
return padded_inputs
class InvertedResidual(nn.Module):
def __init__(self, inp, oup, stride, dilation, expand_ratio, BatchNorm):
super(InvertedResidual, self).__init__()
self.stride = stride
assert stride in [1, 2]
hidden_dim = round(inp * expand_ratio)
self.use_res_connect = self.stride == 1 and inp == oup
self.kernel_size = 3
self.dilation = dilation
if expand_ratio == 1:
self.conv = nn.Sequential(
# dw
nn.Conv2d(hidden_dim, hidden_dim, 3, stride, 0, dilation, groups=hidden_dim, bias=False),
BatchNorm(hidden_dim),
nn.ReLU6(inplace=True),
# pw-linear
nn.Conv2d(hidden_dim, oup, 1, 1, 0, 1, 1, bias=False),
BatchNorm(oup),
)
else:
self.conv = nn.Sequential(
# pw
nn.Conv2d(inp, hidden_dim, 1, 1, 0, 1, bias=False),
BatchNorm(hidden_dim),
nn.ReLU6(inplace=True),
# dw
nn.Conv2d(hidden_dim, hidden_dim, 3, stride, 0, dilation, groups=hidden_dim, bias=False),
BatchNorm(hidden_dim),
nn.ReLU6(inplace=True),
# pw-linear
nn.Conv2d(hidden_dim, oup, 1, 1, 0, 1, bias=False),
BatchNorm(oup),
)
def forward(self, x):
x_pad = fixed_padding(x, self.kernel_size, dilation=self.dilation)
if self.use_res_connect:
x = x + self.conv(x_pad)
else:
x = self.conv(x_pad)
return x
class MobileNetV2(nn.Module):
def __init__(self, output_stride=8, BatchNorm=None, width_mult=1., pretrained=True):
super(MobileNetV2, self).__init__()
block = InvertedResidual
input_channel = 32
current_stride = 1
rate = 1
interverted_residual_setting = [
# t, c, n, s
[1, 16, 1, 1],
[6, 24, 2, 2],
[6, 32, 3, 2],
[6, 64, 4, 2],
[6, 96, 3, 1],
[6, 160, 3, 2],
[6, 320, 1, 1],
]
# building first layer
input_channel = int(input_channel * width_mult)
self.features = [conv_bn(3, input_channel, 2, BatchNorm)]
current_stride *= 2
# building inverted residual blocks
for t, c, n, s in interverted_residual_setting:
if current_stride == output_stride:
stride = 1
dilation = rate
rate *= s
else:
stride = s
dilation = 1
current_stride *= s
output_channel = int(c * width_mult)
for i in range(n):
if i == 0:
self.features.append(block(input_channel, output_channel, stride, dilation, t, BatchNorm))
else:
self.features.append(block(input_channel, output_channel, 1, dilation, t, BatchNorm))
input_channel = output_channel
self.features = nn.Sequential(*self.features)
self._initialize_weights()
if pretrained:
self._load_pretrained_model()
self.low_level_features = self.features[0:4]
self.high_level_features = self.features[4:]
def forward(self, x):
low_level_feat = self.low_level_features(x)
x = self.high_level_features(low_level_feat)
return x, low_level_feat
def _load_pretrained_model(self):
pretrain_dict = torch.load(os.path.join(os.path.dirname(os.path.abspath(__file__)),'./mobilenet_VOC.pth'))
model_dict = {}
state_dict = self.state_dict()
for k, v in pretrain_dict.items():
if k in state_dict:
model_dict[k] = v
state_dict.update(model_dict)
self.load_state_dict(state_dict)
def _initialize_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
# n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
# m.weight.data.normal_(0, math.sqrt(2. / n))
torch.nn.init.kaiming_normal_(m.weight)
elif isinstance(m, SynchronizedBatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
if __name__ == "__main__":
input = torch.rand(1, 3, 512, 512)
model = MobileNetV2(output_stride=16, BatchNorm=nn.BatchNorm2d)
output, low_level_feat = model(input)
print(output.size())
print(low_level_feat.size()) | modeling/backbone/mobilenet.py | 5,418 | dw pw-linear pw dw pw-linear t, c, n, s building first layer building inverted residual blocks n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels m.weight.data.normal_(0, math.sqrt(2. / n)) | 195 | en | 0.529931 |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 5/15/20 4:49 PM
# @File : grover.py
# qubit number=4
# total number=43
import cirq
import cirq.google as cg
from typing import Optional
import sys
from math import log2
import numpy as np
#thatsNoCode
from cirq.contrib.svg import SVGCircuit
# Symbols for the rotation angles in the QAOA circuit.
def make_circuit(n: int, input_qubit):
c = cirq.Circuit() # circuit begin
c.append(cirq.H.on(input_qubit[0])) # number=9
c.append(cirq.H.on(input_qubit[1])) # number=2
c.append(cirq.H.on(input_qubit[2])) # number=3
c.append(cirq.H.on(input_qubit[3])) # number=4
c.append(cirq.H.on(input_qubit[0])) # number=5
c.append(cirq.Y.on(input_qubit[2])) # number=18
c.append(cirq.Z.on(input_qubit[3])) # number=28
c.append(cirq.H.on(input_qubit[1])) # number=6
c.append(cirq.H.on(input_qubit[2])) # number=7
c.append(cirq.H.on(input_qubit[3])) # number=8
c.append(cirq.H.on(input_qubit[3])) # number=20
c.append(cirq.CZ.on(input_qubit[0],input_qubit[3])) # number=21
c.append(cirq.H.on(input_qubit[3])) # number=22
c.append(cirq.X.on(input_qubit[3])) # number=13
c.append(cirq.H.on(input_qubit[3])) # number=23
c.append(cirq.CZ.on(input_qubit[0],input_qubit[3])) # number=24
c.append(cirq.H.on(input_qubit[3])) # number=25
c.append(cirq.H.on(input_qubit[0])) # number=33
c.append(cirq.CZ.on(input_qubit[2],input_qubit[0])) # number=34
c.append(cirq.H.on(input_qubit[0])) # number=35
c.append(cirq.H.on(input_qubit[1])) # number=19
c.append(cirq.H.on(input_qubit[0])) # number=15
c.append(cirq.CZ.on(input_qubit[2],input_qubit[0])) # number=16
c.append(cirq.H.on(input_qubit[0])) # number=17
c.append(cirq.rx(1.6838936623241292).on(input_qubit[2])) # number=36
c.append(cirq.Y.on(input_qubit[1])) # number=26
c.append(cirq.Y.on(input_qubit[1])) # number=27
c.append(cirq.SWAP.on(input_qubit[1],input_qubit[0])) # number=29
c.append(cirq.SWAP.on(input_qubit[1],input_qubit[0])) # number=30
c.append(cirq.CNOT.on(input_qubit[1],input_qubit[0])) # number=40
c.append(cirq.X.on(input_qubit[0])) # number=41
c.append(cirq.CNOT.on(input_qubit[1],input_qubit[0])) # number=42
c.append(cirq.CNOT.on(input_qubit[1],input_qubit[0])) # number=37
c.append(cirq.X.on(input_qubit[0])) # number=38
c.append(cirq.CNOT.on(input_qubit[1],input_qubit[0])) # number=39
# circuit end
c.append(cirq.measure(*input_qubit, key='result'))
return c
def bitstring(bits):
return ''.join(str(int(b)) for b in bits)
if __name__ == '__main__':
qubit_count = 4
input_qubits = [cirq.GridQubit(i, 0) for i in range(qubit_count)]
circuit = make_circuit(qubit_count,input_qubits)
circuit = cg.optimized_for_sycamore(circuit, optimizer_type='sqrt_iswap')
circuit_sample_count =2000
simulator = cirq.Simulator()
result = simulator.run(circuit, repetitions=circuit_sample_count)
frequencies = result.histogram(key='result', fold_func=bitstring)
writefile = open("../data/startCirq3134.csv","w+")
print(format(frequencies),file=writefile)
print("results end", file=writefile)
print(circuit.__len__(), file=writefile)
print(circuit,file=writefile)
writefile.close() | benchmark/startCirq3134.py | 3,320 | !/usr/bin/env python -*- coding: utf-8 -*- @Time : 5/15/20 4:49 PM @File : grover.py qubit number=4 total number=43thatsNoCode Symbols for the rotation angles in the QAOA circuit. circuit begin number=9 number=2 number=3 number=4 number=5 number=18 number=28 number=6 number=7 number=8 number=20 number=21 number=22 number=13 number=23 number=24 number=25 number=33 number=34 number=35 number=19 number=15 number=16 number=17 number=36 number=26 number=27 number=29 number=30 number=40 number=41 number=42 number=37 number=38 number=39 circuit end | 553 | en | 0.270454 |
# -*- coding: utf-8 -*-
"""DBO MSSQL driver
.. module:: lib.database.dbo.drivers.mssql.driver
:platform: Unix
:synopsis: DBO MSSQL driver
.. moduleauthor:: Petr Rašek <bowman@hydratk.org>
"""
try:
import pymssql
except ImportError:
raise NotImplementedError('MSSQL client is not supported for PyPy')
from hydratk.lib.database.dbo import dbodriver
class DBODriver(dbodriver.DBODriver):
"""Class DBODriver
"""
_host = None
_port = 1433
_dbname = None
_driver_options = {
'timeout': 5.0,
'detect_types': 0,
# available “DEFERRED”, “IMMEDIATE” or “EXCLUSIVE”
'isolation_level': None,
'check_same_thread': None,
'factory': 'Connection',
'cached_statements': 100
}
def _parse_dsn(self, dsn):
"""Method parses dsn
Args:
dsn (str): dsn
Returns:
bool: True
Raises:
exception: Exception
"""
dsn_opt = dsn.split(':')[1]
dsn_opt_tokens = dsn_opt.split(';')
for dsn_opt_token in dsn_opt_tokens:
# print(dsn_opt_token)
opt = dsn_opt_token.split('=')
if opt[0] == 'host':
self._host = opt[1]
if opt[0] == 'port':
self._port = int(opt[1])
if opt[0] == 'database':
self._dbname = opt[1]
if opt[0] == 'user':
self._username = opt[1]
if opt[0] == 'password':
self._password = opt[1]
return True
def _apply_driver_options(self, driver_options):
"""Method sets driver options
Args:
driver_option (dict): driver options
Returns:
void
"""
for optname, optval in driver_options.items():
if optname in self._driver_options:
self._driver_options[optname] = optval
def connect(self):
"""Method connects to database
Args:
none
Returns:
void
"""
self._dbcon = pymssql.connect(
server=self._host, port=self._port, database=self._dbname, user=self._username, password=self._password)
self.result_as_dict(self._result_as_dict)
def close(self):
"""Method disconnects from database
Args:
none
Returns:
void
Raises:
exception: DBODriverException
"""
if type(self._dbcon).__name__.lower() == 'connection':
self._dbcon.close()
else:
raise dbodriver.DBODriverException('Not connected')
def commit(self):
"""Method commits transaction
Args:
none
Returns:
void
Raises:
exception: DBODriverException
"""
if type(self._dbcon).__name__.lower() == 'connection':
self._dbcon.commit()
else:
raise dbodriver.DBODriverException('Not connected')
def error_code(self):
pass
def error_info(self):
pass
def qexec(self):
pass
def get_attribute(self):
pass
def in_transaction(self):
pass
def last_insert_id(self):
pass
def prepare(self):
pass
def query(self):
pass
def execute(self, sql, *parameters):
"""Method executes query
Args:
sql (str): SQL query
parameters (args): query parameters
Returns:
obj: cursor
"""
self._cursor.execute(sql, tuple(parameters))
return self._cursor
def quote(self):
pass
def rollback(self):
"""Method rollbacks transaction
Args:
none
Returns:
void
Raises:
exception: DBODriverException
"""
if type(self._dbcon).__name__.lower() == 'connection':
self._dbcon.rollback()
else:
raise dbodriver.DBODriverException('Not connected')
def set_attribute(self):
pass
def __getitem__(self, name):
"""Method gets item
Args:
name (str): item name
Returns:
obj: item value
"""
if hasattr(pymssql, name):
return getattr(pymssql, name)
def __getattr__(self, name):
"""Method gets attribute
Args:
name (str): attribute name
Returns:
obj: attribute value
"""
if type(self._dbcon).__name__.lower() == 'connection':
if hasattr(self._dbcon, name):
return getattr(self._dbcon, name)
if hasattr(pymssql, name):
return getattr(pymssql, name)
def table_exists(self, table_name):
"""Method checks if table exists
Args:
table_name (str): table
Returns:
bool: result
"""
if table_name is not None and table_name != '':
query = "SELECT count(*) found FROM information_schema.tables WHERE table_catalog=%s AND table_type='BASE TABLE' and table_name=%s"
self._cursor.execute(query, (self._dbname, table_name))
recs = self._cursor.fetchall()
result = True if (recs[0]['found'] == 1) else False
return result
def database_exists(self):
pass
def remove_database(self):
pass
def erase_database(self):
pass
def result_as_dict(self, state):
"""Method enables query result in dictionary form
Args:
state (bool): enable dictionary
Returns:
void
Raises:
error: TypeError
"""
if state in (True, False):
self._result_as_dict = state
if state == True:
self._cursor = self._dbcon.cursor(as_dict=True)
else:
self._cursor = self._dbcon.cursor()
else:
raise TypeError('Boolean value expected')
| src/hydratk/lib/database/dbo/drivers/mssql/driver.py | 6,099 | Class DBODriver
Method gets attribute
Args:
name (str): attribute name
Returns:
obj: attribute value
Method gets item
Args:
name (str): item name
Returns:
obj: item value
Method sets driver options
Args:
driver_option (dict): driver options
Returns:
void
Method parses dsn
Args:
dsn (str): dsn
Returns:
bool: True
Raises:
exception: Exception
Method disconnects from database
Args:
none
Returns:
void
Raises:
exception: DBODriverException
Method commits transaction
Args:
none
Returns:
void
Raises:
exception: DBODriverException
Method connects to database
Args:
none
Returns:
void
Method executes query
Args:
sql (str): SQL query
parameters (args): query parameters
Returns:
obj: cursor
Method enables query result in dictionary form
Args:
state (bool): enable dictionary
Returns:
void
Raises:
error: TypeError
Method rollbacks transaction
Args:
none
Returns:
void
Raises:
exception: DBODriverException
Method checks if table exists
Args:
table_name (str): table
Returns:
bool: result
DBO MSSQL driver
.. module:: lib.database.dbo.drivers.mssql.driver
:platform: Unix
:synopsis: DBO MSSQL driver
.. moduleauthor:: Petr Rašek <bowman@hydratk.org>
-*- coding: utf-8 -*- available “DEFERRED”, “IMMEDIATE” or “EXCLUSIVE” print(dsn_opt_token) | 1,396 | en | 0.379277 |
import json
import pickle
import numpy as np
import pytest
import fsspec
from fsspec.implementations.ftp import FTPFileSystem
from fsspec.spec import AbstractFileSystem, AbstractBufferedFile
class DummyTestFS(AbstractFileSystem):
protocol = "mock"
_fs_contents = (
{"name": "top_level", "type": "directory"},
{"name": "top_level/second_level", "type": "directory"},
{"name": "top_level/second_level/date=2019-10-01", "type": "directory"},
{
"name": "top_level/second_level/date=2019-10-01/a.parquet",
"type": "file",
"size": 100,
},
{
"name": "top_level/second_level/date=2019-10-01/b.parquet",
"type": "file",
"size": 100,
},
{"name": "top_level/second_level/date=2019-10-02", "type": "directory"},
{
"name": "top_level/second_level/date=2019-10-02/a.parquet",
"type": "file",
"size": 100,
},
{"name": "top_level/second_level/date=2019-10-04", "type": "directory"},
{
"name": "top_level/second_level/date=2019-10-04/a.parquet",
"type": "file",
"size": 100,
},
{"name": "misc", "type": "directory"},
{"name": "misc/foo.txt", "type": "file", "size": 100},
{"name": "glob_test/hat/^foo.txt", "type": "file", "size": 100},
{"name": "glob_test/dollar/$foo.txt", "type": "file", "size": 100},
{"name": "glob_test/lbrace/{foo.txt", "type": "file", "size": 100},
{"name": "glob_test/rbrace/}foo.txt", "type": "file", "size": 100},
)
def __getitem__(self, name):
for item in self._fs_contents:
if item["name"] == name:
return item
raise IndexError("{name} not found!".format(name=name))
def ls(self, path, detail=True, **kwargs):
path = self._strip_protocol(path)
files = {
file["name"]: file
for file in self._fs_contents
if path == self._parent(file["name"])
}
if detail:
return [files[name] for name in sorted(files)]
return list(sorted(files))
@pytest.mark.parametrize(
"test_path, expected",
[
(
"mock://top_level/second_level/date=2019-10-01/a.parquet",
["top_level/second_level/date=2019-10-01/a.parquet"],
),
(
"mock://top_level/second_level/date=2019-10-01/*",
[
"top_level/second_level/date=2019-10-01/a.parquet",
"top_level/second_level/date=2019-10-01/b.parquet",
],
),
("mock://top_level/second_level/date=2019-10", []),
(
"mock://top_level/second_level/date=2019-10-0[1-4]",
[
"top_level/second_level/date=2019-10-01",
"top_level/second_level/date=2019-10-02",
"top_level/second_level/date=2019-10-04",
],
),
(
"mock://top_level/second_level/date=2019-10-0[1-4]/*",
[
"top_level/second_level/date=2019-10-01/a.parquet",
"top_level/second_level/date=2019-10-01/b.parquet",
"top_level/second_level/date=2019-10-02/a.parquet",
"top_level/second_level/date=2019-10-04/a.parquet",
],
),
(
"mock://top_level/second_level/date=2019-10-0[1-4]/[a].*",
[
"top_level/second_level/date=2019-10-01/a.parquet",
"top_level/second_level/date=2019-10-02/a.parquet",
"top_level/second_level/date=2019-10-04/a.parquet",
],
),
("mock://glob_test/hat/^foo.*", ["glob_test/hat/^foo.txt"]),
("mock://glob_test/dollar/$foo.*", ["glob_test/dollar/$foo.txt"]),
("mock://glob_test/lbrace/{foo.*", ["glob_test/lbrace/{foo.txt"]),
("mock://glob_test/rbrace/}foo.*", ["glob_test/rbrace/}foo.txt"]),
],
)
def test_glob(test_path, expected):
test_fs = DummyTestFS()
res = test_fs.glob(test_path)
res = sorted(res) # FIXME: py35 back-compat
assert res == expected
res = test_fs.glob(test_path, detail=True)
assert isinstance(res, dict)
assert sorted(res) == expected # FIXME: py35 back-compat
for name, info in res.items():
assert info == test_fs[name]
def test_find_details():
test_fs = DummyTestFS()
filenames = test_fs.find("/")
details = test_fs.find("/", detail=True)
for filename in filenames:
assert details[filename] == test_fs.info(filename)
def test_cache():
fs = DummyTestFS()
fs2 = DummyTestFS()
assert fs is fs2
assert len(fs._cache) == 1
del fs2
assert len(fs._cache) == 1
del fs
assert len(DummyTestFS._cache) == 1
DummyTestFS.clear_instance_cache()
assert len(DummyTestFS._cache) == 0
def test_alias():
with pytest.warns(FutureWarning, match="add_aliases"):
DummyTestFS(add_aliases=True)
def test_add_docs_warns():
with pytest.warns(FutureWarning, match="add_docs"):
AbstractFileSystem(add_docs=True)
def test_cache_options():
fs = DummyTestFS()
f = AbstractBufferedFile(fs, "misc/foo.txt", cache_type="bytes")
assert f.cache.trim
# TODO: dummy buffered file
f = AbstractBufferedFile(
fs, "misc/foo.txt", cache_type="bytes", cache_options=dict(trim=False)
)
assert f.cache.trim is False
f = fs.open("misc/foo.txt", cache_type="bytes", cache_options=dict(trim=False))
assert f.cache.trim is False
def test_trim_kwarg_warns():
fs = DummyTestFS()
with pytest.warns(FutureWarning, match="cache_options"):
AbstractBufferedFile(fs, "misc/foo.txt", cache_type="bytes", trim=False)
def test_eq():
fs = DummyTestFS()
result = fs == 1
assert result is False
def test_pickle_multiple():
a = DummyTestFS(1)
b = DummyTestFS(2, bar=1)
x = pickle.dumps(a)
y = pickle.dumps(b)
del a, b
DummyTestFS.clear_instance_cache()
result = pickle.loads(x)
assert result.storage_args == (1,)
assert result.storage_options == {}
result = pickle.loads(y)
assert result.storage_args == (2,)
assert result.storage_options == dict(bar=1)
def test_json():
a = DummyTestFS(1)
b = DummyTestFS(2, bar=1)
outa = a.to_json()
outb = b.to_json()
assert json.loads(outb) # is valid JSON
assert a != b
assert "bar" in outb
assert DummyTestFS.from_json(outa) is a
assert DummyTestFS.from_json(outb) is b
@pytest.mark.parametrize(
"dt",
[
np.int8,
np.int16,
np.int32,
np.int64,
np.uint8,
np.uint16,
np.uint32,
np.uint64,
np.float32,
np.float64,
],
)
def test_readinto_with_numpy(tmpdir, dt):
store_path = str(tmpdir / "test_arr.npy")
arr = np.arange(10, dtype=dt)
arr.tofile(store_path)
arr2 = np.empty_like(arr)
with fsspec.open(store_path, "rb") as f:
f.readinto(arr2)
assert np.array_equal(arr, arr2)
@pytest.mark.parametrize(
"dt",
[
np.int8,
np.int16,
np.int32,
np.int64,
np.uint8,
np.uint16,
np.uint32,
np.uint64,
np.float32,
np.float64,
],
)
def test_readinto_with_multibyte(ftp_writable, tmpdir, dt):
host, port, user, pw = ftp_writable
ftp = FTPFileSystem(host=host, port=port, username=user, password=pw)
with ftp.open("/out", "wb") as fp:
arr = np.arange(10, dtype=dt)
fp.write(arr.tobytes())
with ftp.open("/out", "rb") as fp:
arr2 = np.empty_like(arr)
fp.readinto(arr2)
assert np.array_equal(arr, arr2)
| fsspec/tests/test_spec.py | 7,805 | FIXME: py35 back-compat FIXME: py35 back-compat TODO: dummy buffered file is valid JSON | 87 | en | 0.481185 |
#!/usr/bin/python3
from datetime import datetime
import calendar
import sqlite3
import os
months = ['January', 'February', 'March', 'April', 'May',
'June', 'July', 'August', 'September', 'October',
'November', 'December']
calendar_db = 'PythonicTeamsCalendarTest/calendar.db'
def initialise_calendar():
calendar_db = 'PythonicTeamsCalendarTest/calendar.db'
if not os.path.exists(calendar_db):
sqlite3.connect(calendar_db).close()
try:
conn = sqlite3.connect(calendar_db)
c = conn.cursor()
now = datetime.utcnow()
c.execute(f'''CREATE TABLE calendar(title VARCHAR(255), event TEXT, date_created DATETIME, day INTEGER, month VARCHAR(25), year INTEGER)''')
conn.commit()
conn.close()
return
except sqlite3.OperationalError as e:
print(e)
return
return
def retrieve_current_month():
now = datetime.utcnow()
current_month = months[now.month - 1]
return current_month
def retrieve_current_year():
now = datetime.utcnow()
current_year = now.year
return current_year
def retrieve_previous_month(current_month):
current_month_index = months.index(current_month)
prev_month = months[current_month_index -1]
if current_month == months[0]:
year = datetime.utcnow().year - 1
else:
year = datetime.utcnow().year
return prev_month, year
def retrieve_next_month(current_month, year):
current_month_index = months.index(current_month)
try:
next_month = months[current_month_index + 1]
year = year
return next_month, year
except IndexError:
current_month_index = 0
next_month = months[current_month_index]
year = year + 1
return next_month, year
def retrieve_month_dates(year, month):
month_calendar = calendar.monthcalendar(year=year, month=month)
# month_calendar[0].pop(0)
return month_calendar
def retrieve_current_month_index(month):
current_month_index = int(months.index(month))
return current_month_index
def add_new_event(Title, Event, Day, Month, Year):
try:
conn = sqlite3.connect(calendar_db)
c = conn.cursor()
now = datetime.utcnow()
c.execute('''INSERT INTO calendar(title, event, date_created, day, month, year) VALUES(?,?,?,?,?,?)''', (Title, Event, now, Day, Month, Year))
conn.commit()
conn.close()
except sqlite3.OperationalError as e:
print(e)
#function to retrieve all events in a given month.
#if not month return empty dict
#else return a dict with key "date": value "Event"
def retrieve_all_events_in_month(Month):
conn = None
try:
calendar_events = {} #dict
conn = sqlite3.connect(calendar_db)
c = conn.cursor()
c.execute('''SELECT * FROM calendar WHERE month = :month''', (Month,))
calendar_events_list = list(c.fetchall())
conn.close()
if calendar_events_list:
for calendar_db_entry in calendar_events_list:
calendar_events[calendar_db_entry[3]] = calendar_db_entry
return calendar_events
except sqlite3.OperationalError as e:
print(e)
#function to return events on a specific date
def retrieve_events_on_date(Day,Month,Year):
conn = None
try:
conn = sqlite3.connect(calendar_db)
c = conn.cursor()
c.execute('''SELECT * FROM calendar WHERE day = :day AND month = :month AND year = :year ''', (Day,Month,Year))
events_list = list(c.fetchall())
conn.close()
print('events: ', events_list)
return events_list
except sqlite3.OperationalError as e:
print(e)
| PythonicTeamsCalendarTest/helpers.py | 3,749 | !/usr/bin/python3 month_calendar[0].pop(0)function to retrieve all events in a given month.if not month return empty dictelse return a dict with key "date": value "Event"dictfunction to return events on a specific date | 218 | en | 0.457312 |
# Copyright 2019 Extreme Networks, Inc.
#
# 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.
from __future__ import absolute_import
import os
import sys
import unittest2
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
PACK_ACTIONS_DIR = os.path.join(BASE_DIR, '../../../contrib/packs/actions')
PACK_ACTIONS_DIR = os.path.abspath(PACK_ACTIONS_DIR)
sys.path.insert(0, PACK_ACTIONS_DIR)
from st2common.util.monkey_patch import use_select_poll_workaround
use_select_poll_workaround()
from st2common.util.pack_management import eval_repo_url
__all__ = [
'InstallPackTestCase'
]
class InstallPackTestCase(unittest2.TestCase):
def test_eval_repo(self):
result = eval_repo_url('coditation/st2contrib')
self.assertEqual(result, 'https://github.com/coditation/st2contrib')
result = eval_repo_url('git@github.com:coditation/st2contrib.git')
self.assertEqual(result, 'git@github.com:coditation/st2contrib.git')
repo_url = 'https://github.com/coditation/st2contrib.git'
result = eval_repo_url(repo_url)
self.assertEqual(result, repo_url)
repo_url = 'https://git-wip-us.apache.org/repos/asf/libcloud.git'
result = eval_repo_url(repo_url)
self.assertEqual(result, repo_url)
| st2common/tests/unit/test_pack_management.py | 1,760 | Copyright 2019 Extreme Networks, Inc. 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. | 559 | en | 0.862032 |
import sys
import cv2
import os
from ast import literal_eval
from pathlib import Path
import shutil
import logging
import random
import pickle
import yaml
import subprocess
from PIL import Image
from glob import glob
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import animation, rc
plt.rcParams['figure.figsize'] = 30, 30
np.set_printoptions(precision=3, suppress=True)
rc('animation', html='jshtml')
import torch
from augmentations import get_albu_transforms
IMAGE_DIR = '~/Kaggle/data/tensorflow-great-barrier-reef/train_images'
def load_image(video_id, video_frame, image_dir):
img_path = f'{image_dir}/video_{video_id}/{video_frame}.jpg'
assert os.path.exists(img_path), f'{img_path} does not exist.'
img = cv2.imread(img_path)
return img
def decode_annotations(annotaitons_str):
"""decode annotations in string to list of dict"""
return literal_eval(annotaitons_str)
def load_image_with_annotations(video_id, video_frame, image_dir, annotaitons_str):
img = load_image(video_id, video_frame, image_dir)
annotations = decode_annotations(annotaitons_str)
if len(annotations) > 0:
for ann in annotations:
cv2.rectangle(img, (ann['x'], ann['y']),
(ann['x'] + ann['width'], ann['y'] + ann['height']),
(255, 0, 0), thickness=2,)
return img
def draw_predictions(img, pred_bboxes):
img = img.copy()
if len(pred_bboxes) > 0:
for bbox in pred_bboxes:
conf = bbox[0]
x, y, w, h = bbox[1:].round().astype(int)
cv2.rectangle(img, (x, y),(x+w, y+h),(0, 255, 255), thickness=2,)
cv2.putText(img, f"{conf:.2}",(x, max(0, y-5)),
cv2.FONT_HERSHEY_SIMPLEX,0.5,(0, 0, 255),
thickness=1,
)
return img
def plot_img(df, idx, image_dir, pred_bboxes=None):
row = df.iloc[idx]
video_id = row.video_id
video_frame = row.video_frame
annotations_str = row.annotations
img = load_image_with_annotations(video_id, video_frame, image_dir, annotations_str)
if pred_bboxes and len(pred_bboxes) > 0:
pred_bboxes = pred_bboxes[pred_bboxes[:,0].argsort()[::-1]] # sort by conf
img = draw_predictions(img, pred_bboxes)
plt.imshow(img[:, :, ::-1])
def calc_iou(bboxes1, bboxes2, bbox_mode='xywh'):
assert len(bboxes1.shape) == 2 and bboxes1.shape[1] == 4
assert len(bboxes2.shape) == 2 and bboxes2.shape[1] == 4
bboxes1 = bboxes1.copy()
bboxes2 = bboxes2.copy()
if bbox_mode == 'xywh':
bboxes1[:, 2:] += bboxes1[:, :2]
bboxes2[:, 2:] += bboxes2[:, :2]
x11, y11, x12, y12 = np.split(bboxes1, 4, axis=1)
x21, y21, x22, y22 = np.split(bboxes2, 4, axis=1)
xA = np.maximum(x11, np.transpose(x21))
yA = np.maximum(y11, np.transpose(y21))
xB = np.minimum(x12, np.transpose(x22))
yB = np.minimum(y12, np.transpose(y22))
interArea = np.maximum((xB - xA + 1e-9), 0) * np.maximum((yB - yA + 1e-9), 0)
boxAArea = (x12 - x11 + 1e-9) * (y12 - y11 + 1e-9)
boxBArea = (x22 - x21 + 1e-9) * (y22 - y21 + 1e-9)
iou = interArea / (boxAArea + np.transpose(boxBArea) - interArea)
return iou
def f_beta(tp, fp, fn, beta=2):
if tp == 0:
return 0
return (1+beta**2)*tp / ((1+beta**2)*tp + beta**2*fn+fp)
def calc_is_correct_at_iou_th(gt_bboxes, pred_bboxes, iou_th, verbose=False):
gt_bboxes = gt_bboxes.copy()
pred_bboxes = pred_bboxes.copy()
tp = 0
fp = 0
for k, pred_bbox in enumerate(pred_bboxes): # fixed in ver.7
if len(gt_bboxes) == 0:
fp += len(pred_bboxes) - k # fix in ver.7
break
ious = calc_iou(gt_bboxes, pred_bbox[None, 1:])
max_iou = ious.max()
if max_iou >= iou_th:
tp += 1
gt_bboxes = np.delete(gt_bboxes, ious.argmax(), axis=0)
else:
fp += 1
fn = len(gt_bboxes)
return tp, fp, fn
def calc_is_correct(gt_bboxes, pred_bboxes, iou_th=0.5):
"""
gt_bboxes: (N, 4) np.array in xywh format
pred_bboxes: (N, 5) np.array in conf+xywh format
"""
if len(gt_bboxes) == 0 and len(pred_bboxes) == 0:
tps, fps, fns = 0, 0, 0
return tps, fps, fns
elif len(gt_bboxes) == 0:
tps, fps, fns = 0, len(pred_bboxes), 0
return tps, fps, fns
elif len(pred_bboxes) == 0:
tps, fps, fns = 0, 0, len(gt_bboxes)
return tps, fps, fns
pred_bboxes = pred_bboxes[pred_bboxes[:,0].argsort()[::-1]] # sort by conf
tps, fps, fns = 0, 0, 0
tp, fp, fn = calc_is_correct_at_iou_th(gt_bboxes, pred_bboxes, iou_th)
tps += tp
fps += fp
fns += fn
return tps, fps, fns
def calc_f2_score(gt_bboxes_list, pred_bboxes_list, verbose=False):
"""
gt_bboxes_list: list of (N, 4) np.array in xywh format
pred_bboxes_list: list of (N, 5) np.array in conf+xywh format
"""
#f2s = []
f2_dict = {'f2':0, "P":0, "R": 0}
all_tps = [list([0] * 11) for _ in range(len(gt_bboxes_list))]
all_fps = [list([0] * 11) for _ in range(len(gt_bboxes_list))]
all_fns = [list([0] * 11) for _ in range(len(gt_bboxes_list))]
for k, iou_th in enumerate(np.arange(0.3, 0.85, 0.05)):
tps, fps, fns = 0, 0, 0
for i, (gt_bboxes, pred_bboxes) in enumerate(zip(gt_bboxes_list, pred_bboxes_list)):
tp, fp, fn = calc_is_correct(gt_bboxes, pred_bboxes, iou_th)
tps += tp
fps += fp
fns += fn
all_tps[i][k] = tp
all_fps[i][k] = fp
all_fns[i][k] = fn
if verbose:
num_gt = len(gt_bboxes)
num_pred = len(pred_bboxes)
print(f'num_gt:{num_gt:<3} num_pred:{num_pred:<3} tp:{tp:<3} fp:{fp:<3} fn:{fn:<3}')
f2 = f_beta(tps, fps, fns, beta=2)
precision = f_beta(tps, fps, fns, beta=0)
recall = f_beta(tps, fps, fns, beta=100)
f2_dict["f2_" + str(round(iou_th,3))] = f2
f2_dict["P_" + str(round(iou_th,3))] = precision
f2_dict["R_" + str(round(iou_th,3))] = recall
f2_dict['f2'] += f2 / 11
f2_dict['P'] += precision / 11
f2_dict['R'] += recall / 11
f2_dict["tps"] = all_tps
f2_dict["fps"] = all_fps
f2_dict["fns"] = all_fns
return f2_dict
def print_f2_dict(d):
print("Overall f2: {:.3f}, precision {:.3f}, recall {:.3f}".format(d['f2'], d['precision'], d['recall']))
for k, iou_th in enumerate(np.arange(0.3, 0.85, 0.05)):
print(f"IOU {iou_th:.2f}:", end=" ")
print("f2: {:.3f}, precision {:.3f}, recall {:.3f}".format(d["f2_" + str(round(iou_th,3))],
d["precision_" + str(round(iou_th,3))],
d["recall_" + str(round(iou_th,3))]))
def get_path(row, params, infer=False):
row['old_image_path'] = params['root_dir'] / f'train_images/video_{row.video_id}/{row.video_frame}.jpg'
if infer:
row['image_path'] = row["old_image_path"]
else:
row['image_path'] = params['image_dir'] / f'video_{row.video_id}_{row.video_frame}.jpg'
row['label_path'] = params['label_dir'] / f'video_{row.video_id}_{row.video_frame}.txt'
return row
def make_copy(path, params):
# TODO: fix split issue
data = str(path).split('/')
filename = data[-1]
video_id = data[-2]
new_path = params["image_dir"] / f'{video_id}_{filename}'
shutil.copy(path, new_path)
return
# https://www.kaggle.com/awsaf49/great-barrier-reef-yolov5-train
def voc2yolo(image_height, image_width, bboxes):
"""
voc => [x1, y1, x2, y1]
yolo => [xmid, ymid, w, h] (normalized)
"""
bboxes = bboxes.copy().astype(float) # otherwise all value will be 0 as voc_pascal dtype is np.int
bboxes[..., [0, 2]] = bboxes[..., [0, 2]]/ image_width
bboxes[..., [1, 3]] = bboxes[..., [1, 3]]/ image_height
w = bboxes[..., 2] - bboxes[..., 0]
h = bboxes[..., 3] - bboxes[..., 1]
bboxes[..., 0] = bboxes[..., 0] + w/2
bboxes[..., 1] = bboxes[..., 1] + h/2
bboxes[..., 2] = w
bboxes[..., 3] = h
return bboxes
def yolo2voc(image_height, image_width, bboxes):
"""
yolo => [xmid, ymid, w, h] (normalized)
voc => [x1, y1, x2, y1]
"""
bboxes = bboxes.copy().astype(float) # otherwise all value will be 0 as voc_pascal dtype is np.int
bboxes[..., [0, 2]] = bboxes[..., [0, 2]]* image_width
bboxes[..., [1, 3]] = bboxes[..., [1, 3]]* image_height
bboxes[..., [0, 1]] = bboxes[..., [0, 1]] - bboxes[..., [2, 3]]/2
bboxes[..., [2, 3]] = bboxes[..., [0, 1]] + bboxes[..., [2, 3]]
return bboxes
def coco2yolo(image_height, image_width, bboxes):
"""
coco => [xmin, ymin, w, h]
yolo => [xmid, ymid, w, h] (normalized)
"""
bboxes = bboxes.copy().astype(float) # otherwise all value will be 0 as voc_pascal dtype is np.int
# normolizinig
bboxes[..., [0, 2]]= bboxes[..., [0, 2]]/ image_width
bboxes[..., [1, 3]]= bboxes[..., [1, 3]]/ image_height
# converstion (xmin, ymin) => (xmid, ymid)
bboxes[..., [0, 1]] = bboxes[..., [0, 1]] + bboxes[..., [2, 3]]/2
return bboxes
def yolo2coco(image_height, image_width, bboxes):
"""
yolo => [xmid, ymid, w, h] (normalized)
coco => [xmin, ymin, w, h]
"""
bboxes = bboxes.copy().astype(float) # otherwise all value will be 0 as voc_pascal dtype is np.int
# denormalizing
bboxes[..., [0, 2]]= bboxes[..., [0, 2]]* image_width
bboxes[..., [1, 3]]= bboxes[..., [1, 3]]* image_height
# converstion (xmid, ymid) => (xmin, ymin)
bboxes[..., [0, 1]] = bboxes[..., [0, 1]] - bboxes[..., [2, 3]]/2
return bboxes
def voc2coco(bboxes, image_height=720, image_width=1280):
bboxes = voc2yolo(image_height, image_width, bboxes)
bboxes = yolo2coco(image_height, image_width, bboxes)
return bboxes
def load_image(image_path):
return cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)
def plot_one_box(x, img, color=None, label=None, line_thickness=None):
# Plots one bounding box on image img
tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1 # line/font thickness
color = color or [random.randint(0, 255) for _ in range(3)]
c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3]))
cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA)
if label:
tf = max(tl - 1, 1) # font thickness
t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0]
c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3
cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA) # filled
cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA)
def draw_bboxes(img, bboxes, classes, colors = None, show_classes = None, bbox_format = 'yolo', class_name = False, line_thickness = 1):
image = img.copy()
show_classes = classes if show_classes is None else show_classes
colors = (0, 255 ,0) if colors is None else colors
if bbox_format == 'yolo':
for idx in range(len(bboxes)):
bbox = bboxes[idx]
cls = classes[idx]
color = colors[idx]
if cls in show_classes:
x1 = round(float(bbox[0])*image.shape[1])
y1 = round(float(bbox[1])*image.shape[0])
w = round(float(bbox[2])*image.shape[1]/2) #w/2
h = round(float(bbox[3])*image.shape[0]/2)
voc_bbox = (x1-w, y1-h, x1+w, y1+h)
plot_one_box(voc_bbox,
image,
color = color,
label = cls if class_name else str(get_label(cls)),
line_thickness = line_thickness)
elif bbox_format == 'coco':
for idx in range(len(bboxes)):
bbox = bboxes[idx]
cls = classes[idx]
color = colors[idx]
if cls in show_classes:
x1 = int(round(bbox[0]))
y1 = int(round(bbox[1]))
w = int(round(bbox[2]))
h = int(round(bbox[3]))
voc_bbox = (x1, y1, x1+w, y1+h)
plot_one_box(voc_bbox,
image,
color = color,
label = cls,
line_thickness = line_thickness)
elif bbox_format == 'voc_pascal':
for idx in range(len(bboxes)):
bbox = bboxes[idx]
cls = classes[idx]
cls_id = class_ids[idx]
color = colors[cls_id] if type(colors) is list else colors
if cls in show_classes:
x1 = int(round(bbox[0]))
y1 = int(round(bbox[1]))
x2 = int(round(bbox[2]))
y2 = int(round(bbox[3]))
voc_bbox = (x1, y1, x2, y2)
plot_one_box(voc_bbox,
image,
color = color,
label = cls if class_name else str(cls_id),
line_thickness = line_thickness)
else:
raise ValueError('wrong bbox format')
return image
def get_bbox(annots):
bboxes = [list(annot.values()) for annot in annots]
return bboxes
def get_imgsize(row):
row['width'], row['height'] = imagesize.get(row['image_path'])
return row
# https://www.kaggle.com/diegoalejogm/great-barrier-reefs-eda-with-animations
def create_animation(ims):
fig = plt.figure(figsize=(16, 12))
plt.axis('off')
im = plt.imshow(ims[0])
def animate_func(i):
im.set_array(ims[i])
return [im]
return animation.FuncAnimation(fig, animate_func, frames = len(ims), interval = 1000//12)
# https://github.com/rbgirshick/fast-rcnn/blob/master/lib/utils/nms.py
def nms(dets, thresh):
x1 = dets[:, 0]
y1 = dets[:, 1]
x2 = dets[:, 2]
y2 = dets[:, 3]
scores = dets[:, 4]
areas = (x2 - x1 + 1) * (y2 - y1 + 1)
order = scores.argsort()[::-1]
keep = []
while order.size > 0:
i = order[0]
keep.append(i)
xx1 = np.maximum(x1[i], x1[order[1:]])
yy1 = np.maximum(y1[i], y1[order[1:]])
xx2 = np.minimum(x2[i], x2[order[1:]])
yy2 = np.minimum(y2[i], y2[order[1:]])
w = np.maximum(0.0, xx2 - xx1 + 1)
h = np.maximum(0.0, yy2 - yy1 + 1)
inter = w * h
ovr = inter / (areas[i] + areas[order[1:]] - inter)
inds = np.where(ovr <= thresh)[0]
order = order[inds + 1]
return keep
# https://github.com/DocF/Soft-NMS/blob/master/soft_nms.py
def py_cpu_softnms(dets, sc, Nt=0.3, sigma=0.5, thresh=0.001, method=2):
"""
py_cpu_softnms
:param dets: boexs 坐标矩阵 format [y1, x1, y2, x2]
:param sc: 每个 boxes 对应的分数
:param Nt: iou 交叠门限
:param sigma: 使用 gaussian 函数的方差
:param thresh: 最后的分数门限
:param method: 使用的方法
:return: 留下的 boxes 的 index
"""
# indexes concatenate boxes with the last column
N = dets.shape[0]
indexes = np.array([np.arange(N)])
dets = np.concatenate((dets, indexes.T), axis=1)
# the order of boxes coordinate is [y1,x1,y2,x2]
y1 = dets[:, 0]
x1 = dets[:, 1]
y2 = dets[:, 2]
x2 = dets[:, 3]
scores = sc
areas = (x2 - x1 + 1) * (y2 - y1 + 1)
for i in range(N):
# intermediate parameters for later parameters exchange
tBD = dets[i, :].copy()
tscore = scores[i].copy()
tarea = areas[i].copy()
pos = i + 1
#
if i != N-1:
maxscore = np.max(scores[pos:], axis=0)
maxpos = np.argmax(scores[pos:], axis=0)
else:
maxscore = scores[-1]
maxpos = 0
if tscore < maxscore:
dets[i, :] = dets[maxpos + i + 1, :]
dets[maxpos + i + 1, :] = tBD
tBD = dets[i, :]
scores[i] = scores[maxpos + i + 1]
scores[maxpos + i + 1] = tscore
tscore = scores[i]
areas[i] = areas[maxpos + i + 1]
areas[maxpos + i + 1] = tarea
tarea = areas[i]
# IoU calculate
xx1 = np.maximum(dets[i, 1], dets[pos:, 1])
yy1 = np.maximum(dets[i, 0], dets[pos:, 0])
xx2 = np.minimum(dets[i, 3], dets[pos:, 3])
yy2 = np.minimum(dets[i, 2], dets[pos:, 2])
w = np.maximum(0.0, xx2 - xx1 + 1)
h = np.maximum(0.0, yy2 - yy1 + 1)
inter = w * h
ovr = inter / (areas[i] + areas[pos:] - inter)
# Three methods: 1.linear 2.gaussian 3.original NMS
if method == 1: # linear
weight = np.ones(ovr.shape)
weight[ovr > Nt] = weight[ovr > Nt] - ovr[ovr > Nt]
elif method == 2: # gaussian
weight = np.exp(-(ovr * ovr) / sigma)
else: # original NMS
weight = np.ones(ovr.shape)
weight[ovr > Nt] = 0
scores[pos:] = weight * scores[pos:]
# select the boxes and keep the corresponding indexes
inds = dets[:, 4][scores > thresh]
keep = inds.astype(int)
return keep
def seed_torch(seed=42):
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
def create_logger(filename, filemode='a'):
# better logging file - output the in terminal as well
file_handler = logging.FileHandler(filename=filename, mode=filemode)
stdout_handler = logging.StreamHandler(sys.stdout)
handlers = [file_handler, stdout_handler]
formatter = "%(asctime)s %(levelname)s: %(message)s"
datefmt = "%m/%d/%Y %I:%M:%S %p"
logging.basicConfig(format=formatter, datefmt=datefmt,
level=logging.DEBUG, handlers=handlers)
return
def save_pickle(obj, folder_path):
pickle.dump(obj, open(folder_path, 'wb'), pickle.HIGHEST_PROTOCOL)
def load_pickle(folder_path):
return pickle.load(open(folder_path, 'rb'))
def save_yaml(obj, folder_path):
obj2 = obj.copy()
for key, value in obj2.items():
if isinstance(value, Path):
obj2[key] = str(value.resolve())
else:
obj2[key] = value
with open(folder_path, 'w') as file:
yaml.dump(obj2, file)
def load_yaml(folder_path):
with open(folder_path) as file:
data = yaml.load(file, Loader=yaml.FullLoader)
return data
def load_model(params):
try:
model = torch.hub.load(params['repo'],
'custom',
path=params['ckpt_path'],
source='local',
force_reload=True) # local repo
except:
print("torch.hub.load failed, try torch.load")
model = torch.load(params['ckpt_path'])
model.conf = params['conf'] # NMS confidence threshold
model.iou = params['iou'] # NMS IoU threshold
model.classes = None # (optional list) filter by class, i.e. = [0, 15, 16] for persons, cats and dogs
model.multi_label = False # NMS multiple labels per box
model.max_det = 50 # maximum number of detections per image
return model
def predict(model, img, size=768, augment=False, use_sahi=False):
if use_sahi:
from sahi.predict import get_sliced_prediction
results = get_sliced_prediction(
img,
model,
slice_height = 512,
slice_width = 512,
overlap_height_ratio = 0.2,
overlap_width_ratio = 0.2
)
preds = results.object_prediction_list
bboxes = np.array([pred.bbox.to_voc_bbox() for pred in preds])
else:
results = model(img, size=size, augment=augment) # custom inference size
preds = results.pandas().xyxy[0]
bboxes = preds[['xmin','ymin','xmax','ymax']].values
if len(bboxes):
height, width = img.shape[:2]
bboxes = voc2coco(bboxes,height,width).astype(int)
if use_sahi:
confs = np.array([pred.score.value for pred in preds])
else:
confs = preds.confidence.values
return bboxes, confs
else:
return np.array([]),[]
def format_prediction(bboxes, confs):
annot = ''
if len(bboxes)>0:
for idx in range(len(bboxes)):
xmin, ymin, w, h = bboxes[idx]
conf = confs[idx]
annot += f'{conf} {xmin} {ymin} {w} {h}'
annot +=' '
annot = annot.strip(' ')
return annot
def show_img(img, bboxes, confs, colors, bbox_format='yolo'):
labels = [str(round(conf,2)) for conf in confs]
img = draw_bboxes(img = img,
bboxes = bboxes,
classes = labels,
class_name = True,
colors = colors,
bbox_format = bbox_format,
line_thickness = 2)
return Image.fromarray(img)
def write_hyp(params):
with open(params["hyp_file"], mode="w") as f:
for key, val in params["hyp_param"].items():
f.write(f"{key}: {val}\n")
def class2dict(f):
return dict((name, getattr(f, name)) for name in dir(f) if not name.startswith('__'))
def upload(params):
data_version = "-".join(params["exp_name"].split("_"))
if os.path.exists(params["output_dir"] / "wandb"):
shutil.move(str(params["output_dir"] / "wandb"),
str(params["output_dir"].parent / f"{params['exp_name']}_wandb/")
)
with open(params["output_dir"] / "dataset-metadata.json", "w") as f:
f.write("{\n")
f.write(f""" "title": "{data_version}",\n""")
f.write(f""" "id": "vincentwang25/{data_version}",\n""")
f.write(""" "licenses": [\n""")
f.write(""" {\n""")
f.write(""" "name": "CC0-1.0"\n""")
f.write(""" }\n""")
f.write(""" ]\n""")
f.write("""}""")
subprocess.call(["kaggle", "datasets", "create", "-p", str(params["output_dir"]), "-r", "zip"])
def coco(df):
annotion_id = 0
images = []
annotations = []
categories = [{'id': 0, 'name': 'cots'}]
for i, row in df.iterrows():
images.append({
"id": i,
"file_name": f"video_{row['video_id']}_{row['video_frame']}.jpg",
"height": 720,
"width": 1280,
})
for bbox in row['annotations']:
annotations.append({
"id": annotion_id,
"image_id": i,
"category_id": 0,
"bbox": list(bbox.values()),
"area": bbox['width'] * bbox['height'],
"segmentation": [],
"iscrowd": 0
})
annotion_id += 1
json_file = {'categories':categories, 'images':images, 'annotations':annotations}
return json_file
def mmcfg_from_param(params):
from mmcv import Config
# model
cfg = Config.fromfile(params['hyp_param']['base_file'])
cfg.work_dir = str(params['output_dir'])
cfg.seed = 2022
cfg.gpu_ids = range(2)
cfg.load_from = params['hyp_param']['load_from']
if params['hyp_param']['model_type'] == 'faster_rcnn':
cfg.model.roi_head.bbox_head.num_classes = 1
cfg.model.roi_head.bbox_head.loss_bbox.type = params['hyp_param']['loss_fnc']
cfg.model.rpn_head.loss_bbox.type = params['hyp_param']['loss_fnc']
if params['hyp_param']['loss_fnc'] == "GIoULoss":
cfg.model.roi_head.bbox_head.reg_decoded_bbox = True
cfg.model.rpn_head.reg_decoded_bbox = True
cfg.model.train_cfg.rpn_proposal.nms.type = params['hyp_param']['nms']
cfg.model.test_cfg.rpn.nms.type = params['hyp_param']['nms']
cfg.model.test_cfg.rcnn.nms.type = params['hyp_param']['nms']
cfg.model.train_cfg.rcnn.sampler.type = params['hyp_param']['sampler']
elif params['hyp_param']['model_type'] == 'swin':
pass # already changed
elif params['hyp_param']['model_type'] == 'vfnet':
cfg.model.bbox_head.num_classes = 1
if params['hyp_param'].get("optimizer", cfg.optimizer.type) == "AdamW":
cfg.optimizer = dict(
type="AdamW",
lr=params['hyp_param'].get("lr", cfg.optimizer.lr),
weight_decay=params['hyp_param'].get(
"weight_decay", cfg.optimizer.weight_decay
),
)
else:
cfg.optimizer.lr = params['hyp_param'].get("lr", cfg.optimizer.lr)
cfg.optimizer.weight_decay = params['hyp_param'].get(
"weight_decay", cfg.optimizer.weight_decay)
cfg.lr_config = dict(
policy='CosineAnnealing',
by_epoch=False,
warmup='linear',
warmup_iters= 1000,
warmup_ratio= 1/10,
min_lr=1e-07)
# data
cfg = add_data_pipeline(cfg, params)
cfg.runner.max_epochs = params['epochs']
cfg.evaluation.start = 1
cfg.evaluation.interval = 1
cfg.evaluation.save_best='auto'
cfg.evaluation.metric ='bbox'
cfg.checkpoint_config.interval = -1
cfg.log_config.interval = 500
cfg.log_config.with_step = True
cfg.log_config.by_epoch = True
cfg.log_config.hooks =[dict(type='TextLoggerHook'),
dict(type='TensorboardLoggerHook')]
cfg.workflow = [('train',1)]
logging.info(str(cfg))
return cfg
def add_data_pipeline(cfg, params):
cfg.dataset_type = 'COCODataset'
cfg.classes = ('cots',)
cfg.data_root = str(params['data_path'].resolve())
params['aug_param']['img_scale'] = (params['img_size'], params['img_size'])
cfg.img_scale = params['aug_param']['img_scale']
cfg.dataset_type = 'CocoDataset'
cfg.filter_empty_gt = False
cfg.data.filter_empty_gt = False
cfg.data.train.type = cfg.dataset_type
cfg.data.train.classes = cfg.classes
cfg.data.train.ann_file = str(params["cfg_dir"] / 'annotations_train.json')
cfg.data.train.img_prefix = cfg.data_root + '/images/'
cfg.data.train.filter_empty_gt = False
cfg.data.test.type = cfg.dataset_type
cfg.data.test.classes = cfg.classes
cfg.data.test.ann_file = str(params["cfg_dir"] / 'annotations_valid.json')
cfg.data.test.img_prefix = cfg.data_root + '/images/'
cfg.data.test.filter_empty_gt = False
cfg.data.val.type = cfg.dataset_type
cfg.data.val.classes = cfg.classes
cfg.data.val.ann_file = str(params["cfg_dir"] / 'annotations_valid.json')
cfg.data.val.img_prefix = cfg.data_root + '/images/'
cfg.data.val.filter_empty_gt = False
cfg.data.samples_per_gpu = params['batch'] // len(cfg.gpu_ids)
cfg.data.workers_per_gpu = params['workers'] // len(cfg.gpu_ids)
# train pipeline
albu_train_transforms = get_albu_transforms(params['aug_param'], is_train=True)
if params['aug_param']['use_mixup'] or params['aug_param']['use_mosaic']:
train_pipeline = []
else:
train_pipeline = [
dict(type='LoadImageFromFile'),
dict(type='LoadAnnotations', with_bbox=True)]
if params['aug_param']['use_mosaic']:
train_pipeline.append(dict(type='Mosaic', img_scale=cfg.img_scale, pad_val=114.0))
else:
train_pipeline.append(dict(type='Resize', img_scale=cfg.img_scale, keep_ratio=False))
train_pipeline = train_pipeline +[
dict(type='Pad', size_divisor=32),
dict(
type='Albu',
transforms=albu_train_transforms,
bbox_params=dict(
type='BboxParams',
format='pascal_voc',
label_fields=['gt_labels'],
min_visibility=0.0,
filter_lost_elements=True),
keymap={
'img': 'image',
'gt_bboxes': 'bboxes'
},
update_pad_shape=False,
skip_img_without_anno=False
)]
if params['aug_param']['use_mixup']:
train_pipeline.append(dict(type='MixUp', img_scale=cfg.img_scale, ratio_range=(0.8, 1.6), pad_val=114.0))
train_pipeline = train_pipeline +\
[
dict(type='Normalize', **cfg.img_norm_cfg),
dict(type='DefaultFormatBundle'),
dict(type='Collect',
keys=['img', 'gt_bboxes', 'gt_labels'],
meta_keys=('filename', 'ori_filename', 'ori_shape', 'img_shape', 'pad_shape',
'scale_factor', 'img_norm_cfg')),
]
val_pipeline = [
dict(type='LoadImageFromFile'),
dict(
type='MultiScaleFlipAug',
img_scale=cfg.img_scale,
flip=False,
transforms=[
dict(type='Resize', keep_ratio=True),
dict(type='RandomFlip'),
dict(type='Normalize', **cfg.img_norm_cfg),
dict(type='Pad', size_divisor=32),
dict(type='ImageToTensor', keys=['img']),
dict(type='Collect', keys=['img'])
])
]
test_pipeline = [
dict(type='LoadImageFromFile'),
dict(
type='MultiScaleFlipAug',
img_scale=[cfg.img_scale],
flip=[False],
transforms=[
dict(type='Resize', keep_ratio=False),
dict(type='Pad', size_divisor=32),
dict(type='RandomFlip', direction='horizontal'),
dict(type='Normalize', **cfg.img_norm_cfg),
dict(type='ImageToTensor', keys=['img']),
dict(type='Collect', keys=['img']),
])
]
cfg.train_pipeline = train_pipeline
cfg.val_pipeline = val_pipeline
cfg.test_pipeline = test_pipeline
if params['aug_param']['use_mixup'] or params['aug_param']['use_mosaic']:
cfg.train_dataset = dict(
type='MultiImageMixDataset',
dataset=dict(
type=cfg.dataset_type,
classes=cfg.classes,
ann_file=str(params["cfg_dir"] / 'annotations_train.json'),
img_prefix=cfg.data_root + '/images/',
pipeline=[
dict(type='LoadImageFromFile'),
dict(type='LoadAnnotations', with_bbox=True)
],
filter_empty_gt=False,
),
pipeline=cfg.train_pipeline
)
cfg.data.train = cfg.train_dataset
else:
cfg.data.train.pipeline = cfg.train_pipeline
cfg.data.val.pipeline = cfg.val_pipeline
cfg.data.test.pipeline = cfg.test_pipeline
return cfg
def find_ckp(output_dir):
return glob(output_dir / "best*.pth")[0] | src/util.py | 31,574 | gt_bboxes_list: list of (N, 4) np.array in xywh format
pred_bboxes_list: list of (N, 5) np.array in conf+xywh format
gt_bboxes: (N, 4) np.array in xywh format
pred_bboxes: (N, 5) np.array in conf+xywh format
coco => [xmin, ymin, w, h]
yolo => [xmid, ymid, w, h] (normalized)
decode annotations in string to list of dict
py_cpu_softnms
:param dets: boexs 坐标矩阵 format [y1, x1, y2, x2]
:param sc: 每个 boxes 对应的分数
:param Nt: iou 交叠门限
:param sigma: 使用 gaussian 函数的方差
:param thresh: 最后的分数门限
:param method: 使用的方法
:return: 留下的 boxes 的 index
voc => [x1, y1, x2, y1]
yolo => [xmid, ymid, w, h] (normalized)
yolo => [xmid, ymid, w, h] (normalized)
coco => [xmin, ymin, w, h]
yolo => [xmid, ymid, w, h] (normalized)
voc => [x1, y1, x2, y1]
sort by conf fixed in ver.7 fix in ver.7 sort by conff2s = [] TODO: fix split issue https://www.kaggle.com/awsaf49/great-barrier-reef-yolov5-train otherwise all value will be 0 as voc_pascal dtype is np.int otherwise all value will be 0 as voc_pascal dtype is np.int otherwise all value will be 0 as voc_pascal dtype is np.int normolizinig converstion (xmin, ymin) => (xmid, ymid) otherwise all value will be 0 as voc_pascal dtype is np.int denormalizing converstion (xmid, ymid) => (xmin, ymin) Plots one bounding box on image img line/font thickness font thickness filledw/2 https://www.kaggle.com/diegoalejogm/great-barrier-reefs-eda-with-animations https://github.com/rbgirshick/fast-rcnn/blob/master/lib/utils/nms.py https://github.com/DocF/Soft-NMS/blob/master/soft_nms.py indexes concatenate boxes with the last column the order of boxes coordinate is [y1,x1,y2,x2] intermediate parameters for later parameters exchange IoU calculate Three methods: 1.linear 2.gaussian 3.original NMS linear gaussian original NMS select the boxes and keep the corresponding indexes better logging file - output the in terminal as well local repo NMS confidence threshold NMS IoU threshold (optional list) filter by class, i.e. = [0, 15, 16] for persons, cats and dogs NMS multiple labels per box maximum number of detections per image custom inference size model already changed data train pipeline | 2,138 | en | 0.605542 |
#!/usr/bin/env python3
#
# # Copyright (c) 2021 Facebook, inc. and its affiliates. All Rights Reserved
#
#
from uimnet import utils
from uimnet import algorithms
from uimnet import workers
from omegaconf import OmegaConf
from pathlib import Path
import torch
import torch.distributed as dist
import torch.multiprocessing as tmp
import numpy as np
import os
import argparse
import pickle
import filelock
TRAIN_CFG = """
sweep_dir: null
output_dir: null # subfolder. Mutable at dispatch
dataset:
name: ImageNat
root: /checkpoint/ishmaelb/data/datasets/ILSVRC2012
equalize_partitions: True
seed: 0
batch_size: 256
splits_props:
train: 0.9
eval: 0.1
algorithm:
name: null
arch: null
use_mixed_precision: True
seed: 0 # Mutable at dispatch
sn: False
sn_coef: 1.0
sn_bn: True
experiment:
distributed: False
platform: local
evaluate_every: 10
checkpoint_every: 10
num_epochs: 100
## ----- Mutable on the worker during distributed/device setup.
output_dir: null
seed: 42 # Workers seed
device: 'cuda:0'
rank: null
local_rank: null
world_size: null
dist_protocol: null
dist_url: null
num_workers: 5
# ------
"""
def parse_arguments():
parser = argparse.ArgumentParser(description='Trains model')
parser.add_argument('-a', '--algorithm', type=str, required=True)
parser.add_argument('--arch', type=str, default='resnet18')
parser.add_argument('-m', '--model_dir', type=str, required=True)
parser.add_argument('-c', '--clustering_file', type=str, required=True)
parser.add_argument('--local_rank', type=int, default=None)
parser.add_argument('-d', '--distributed', action='store_true')
parser.add_argument('--dist_protocol', type=str, default='env')
return parser.parse_args()
def partition_datasets(train_cfg, partitions):
all_datasets = {}
for split_name in ['train', 'val']:
all_datasets[split_name] = utils.partition_dataset(name=train_cfg.dataset.name,
root=train_cfg.dataset.root,
split=split_name,
partitions=partitions,
equalize_partitions=train_cfg.dataset.equalize_partitions)
return all_datasets
def train_algorithm(train_cfg, Algorithm, dataset):
if utils.is_distributed():
os.environ['OMP_NUM_THREADS'] = train_cfg.experiment.num_workers
trainer = workers.Trainer()
output = trainer(train_cfg, Algorithm, dataset=dataset)
return output
@utils.timeit
def run_trainer(model_dir, algorithm_name, arch, clustering_file, distributed, dist_protocol):
model_path = Path(model_dir)
model_path.mkdir(parents=True, exist_ok=True)
train_cfg = OmegaConf.create(TRAIN_CFG)
OmegaConf.set_struct(train_cfg, True)
train_cfg.output_dir =model_dir
train_cfg.algorithm.name = algorithm_name
train_cfg.algorithm.arch = arch
train_cfg.experiment.distributed = distributed
train_cfg.experiment.dist_protocol = dist_protocol
with open(model_path / 'train_cfg.yaml', 'w') as fp:
OmegaConf.save(train_cfg, fp.name)
with filelock.FileLock(clustering_file + '.lock'):
with open(clustering_file, 'rb') as fp:
clustering = pickle.load(fp)
datasets = partition_datasets(train_cfg, partitions=clustering['partitions'])
Algorithm = utils.load_model_cls(train_cfg)
trainer_args = (train_cfg, Algorithm, datasets['train']['in'])
output = train_algorithm(*trainer_args)
return utils.pack(output)
if __name__ == '__main__':
args = parse_arguments()
trainer_output = run_trainer(args.model_dir, args.algorithm, arch=args.arch, clustering_file=args.clustering_file,
distributed=args.distributed, dist_protocol=args.dist_protocol)
| scripts/run_trainer.py | 3,843 | !/usr/bin/env python3 Copyright (c) 2021 Facebook, inc. and its affiliates. All Rights Reserved | 96 | en | 0.824134 |
"""Configuration for SSDP tests."""
from typing import Optional, Sequence
from unittest.mock import AsyncMock, MagicMock, patch
from urllib.parse import urlparse
from async_upnp_client.client import UpnpDevice
from async_upnp_client.event_handler import UpnpEventHandler
from async_upnp_client.profiles.igd import StatusInfo
import pytest
from homeassistant.components import ssdp
from homeassistant.components.upnp.const import (
BYTES_RECEIVED,
BYTES_SENT,
CONFIG_ENTRY_ST,
CONFIG_ENTRY_UDN,
DOMAIN,
PACKETS_RECEIVED,
PACKETS_SENT,
ROUTER_IP,
ROUTER_UPTIME,
WAN_STATUS,
)
from homeassistant.core import HomeAssistant
from homeassistant.util import dt
from tests.common import MockConfigEntry
TEST_UDN = "uuid:device"
TEST_ST = "urn:schemas-upnp-org:device:InternetGatewayDevice:1"
TEST_USN = f"{TEST_UDN}::{TEST_ST}"
TEST_LOCATION = "http://192.168.1.1/desc.xml"
TEST_HOSTNAME = urlparse(TEST_LOCATION).hostname
TEST_FRIENDLY_NAME = "mock-name"
TEST_DISCOVERY = ssdp.SsdpServiceInfo(
ssdp_usn=TEST_USN,
ssdp_st=TEST_ST,
ssdp_location=TEST_LOCATION,
upnp={
"_udn": TEST_UDN,
"location": TEST_LOCATION,
"usn": TEST_USN,
ssdp.ATTR_UPNP_DEVICE_TYPE: TEST_ST,
ssdp.ATTR_UPNP_FRIENDLY_NAME: TEST_FRIENDLY_NAME,
ssdp.ATTR_UPNP_MANUFACTURER: "mock-manufacturer",
ssdp.ATTR_UPNP_MODEL_NAME: "mock-model-name",
ssdp.ATTR_UPNP_UDN: TEST_UDN,
},
ssdp_headers={
"_host": TEST_HOSTNAME,
},
)
class MockUpnpDevice:
"""Mock async_upnp_client UpnpDevice."""
def __init__(self, location: str) -> None:
"""Initialize."""
self.device_url = location
@property
def manufacturer(self) -> str:
"""Get manufacturer."""
return TEST_DISCOVERY.upnp[ssdp.ATTR_UPNP_MANUFACTURER]
@property
def name(self) -> str:
"""Get name."""
return TEST_DISCOVERY.upnp[ssdp.ATTR_UPNP_FRIENDLY_NAME]
@property
def model_name(self) -> str:
"""Get the model name."""
return TEST_DISCOVERY.upnp[ssdp.ATTR_UPNP_MODEL_NAME]
@property
def device_type(self) -> str:
"""Get the device type."""
return TEST_DISCOVERY.upnp[ssdp.ATTR_UPNP_DEVICE_TYPE]
@property
def udn(self) -> str:
"""Get the UDN."""
return TEST_DISCOVERY.upnp[ssdp.ATTR_UPNP_UDN]
@property
def usn(self) -> str:
"""Get the USN."""
return f"{self.udn}::{self.device_type}"
@property
def unique_id(self) -> str:
"""Get the unique id."""
return self.usn
def reinit(self, new_upnp_device: UpnpDevice) -> None:
"""Reinitialize."""
self.device_url = new_upnp_device.device_url
class MockIgdDevice:
"""Mock async_upnp_client IgdDevice."""
def __init__(self, device: MockUpnpDevice, event_handler: UpnpEventHandler) -> None:
"""Initialize mock device."""
self.device = device
self.profile_device = device
self._timestamp = dt.utcnow()
self.traffic_times_polled = 0
self.status_times_polled = 0
self.traffic_data = {
BYTES_RECEIVED: 0,
BYTES_SENT: 0,
PACKETS_RECEIVED: 0,
PACKETS_SENT: 0,
}
self.status_data = {
WAN_STATUS: "Connected",
ROUTER_UPTIME: 10,
ROUTER_IP: "8.9.10.11",
}
@property
def name(self) -> str:
"""Get the name of the device."""
return self.profile_device.name
@property
def manufacturer(self) -> str:
"""Get the manufacturer of this device."""
return self.profile_device.manufacturer
@property
def model_name(self) -> str:
"""Get the model name of this device."""
return self.profile_device.model_name
@property
def udn(self) -> str:
"""Get the UDN of the device."""
return self.profile_device.udn
@property
def device_type(self) -> str:
"""Get the device type of this device."""
return self.profile_device.device_type
async def async_get_total_bytes_received(self) -> Optional[int]:
"""Get total bytes received."""
self.traffic_times_polled += 1
return self.traffic_data[BYTES_RECEIVED]
async def async_get_total_bytes_sent(self) -> Optional[int]:
"""Get total bytes sent."""
return self.traffic_data[BYTES_SENT]
async def async_get_total_packets_received(self) -> Optional[int]:
"""Get total packets received."""
return self.traffic_data[PACKETS_RECEIVED]
async def async_get_total_packets_sent(self) -> Optional[int]:
"""Get total packets sent."""
return self.traffic_data[PACKETS_SENT]
async def async_get_external_ip_address(
self, services: Optional[Sequence[str]] = None
) -> Optional[str]:
"""
Get the external IP address.
:param services List of service names to try to get action from, defaults to [WANIPC,WANPPP]
"""
return self.status_data[ROUTER_IP]
async def async_get_status_info(
self, services: Optional[Sequence[str]] = None
) -> Optional[StatusInfo]:
"""
Get status info.
:param services List of service names to try to get action from, defaults to [WANIPC,WANPPP]
"""
self.status_times_polled += 1
return StatusInfo(
self.status_data[WAN_STATUS], "", self.status_data[ROUTER_UPTIME]
)
@pytest.fixture(autouse=True)
def mock_upnp_device():
"""Mock homeassistant.components.upnp.Device."""
async def mock_async_create_upnp_device(
hass: HomeAssistant, location: str
) -> UpnpDevice:
"""Create UPnP device."""
return MockUpnpDevice(location)
with patch(
"homeassistant.components.upnp.device.async_create_upnp_device",
side_effect=mock_async_create_upnp_device,
) as mock_async_create_upnp_device, patch(
"homeassistant.components.upnp.device.IgdDevice", new=MockIgdDevice
) as mock_igd_device:
yield mock_async_create_upnp_device, mock_igd_device
@pytest.fixture
def mock_setup_entry():
"""Mock async_setup_entry."""
with patch(
"homeassistant.components.upnp.async_setup_entry",
return_value=AsyncMock(True),
) as mock_setup:
yield mock_setup
@pytest.fixture(autouse=True)
async def silent_ssdp_scanner(hass):
"""Start SSDP component and get Scanner, prevent actual SSDP traffic."""
with patch(
"homeassistant.components.ssdp.Scanner._async_start_ssdp_listeners"
), patch("homeassistant.components.ssdp.Scanner._async_stop_ssdp_listeners"), patch(
"homeassistant.components.ssdp.Scanner.async_scan"
):
yield
@pytest.fixture
async def ssdp_instant_discovery():
"""Instance discovery."""
# Set up device discovery callback.
async def register_callback(hass, callback, match_dict):
"""Immediately do callback."""
await callback(TEST_DISCOVERY, ssdp.SsdpChange.ALIVE)
return MagicMock()
with patch(
"homeassistant.components.ssdp.async_register_callback",
side_effect=register_callback,
) as mock_register, patch(
"homeassistant.components.ssdp.async_get_discovery_info_by_st",
return_value=[TEST_DISCOVERY],
) as mock_get_info:
yield (mock_register, mock_get_info)
@pytest.fixture
async def ssdp_no_discovery():
"""No discovery."""
# Set up device discovery callback.
async def register_callback(hass, callback, match_dict):
"""Don't do callback."""
return MagicMock()
with patch(
"homeassistant.components.ssdp.async_register_callback",
side_effect=register_callback,
) as mock_register, patch(
"homeassistant.components.ssdp.async_get_discovery_info_by_st",
return_value=[],
) as mock_get_info, patch(
"homeassistant.components.upnp.config_flow.SSDP_SEARCH_TIMEOUT",
0.1,
):
yield (mock_register, mock_get_info)
@pytest.fixture
async def setup_integration(
hass: HomeAssistant, mock_get_source_ip, ssdp_instant_discovery, mock_upnp_device
):
"""Create an initialized integration."""
entry = MockConfigEntry(
domain=DOMAIN,
data={
CONFIG_ENTRY_UDN: TEST_UDN,
CONFIG_ENTRY_ST: TEST_ST,
},
)
# Load config_entry.
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
yield entry
| tests/components/upnp/conftest.py | 8,633 | Mock async_upnp_client IgdDevice.
Mock async_upnp_client UpnpDevice.
Initialize.
Initialize mock device.
Get the device type.
Get the device type of this device.
Get manufacturer.
Get the manufacturer of this device.
Mock async_setup_entry.
Mock homeassistant.components.upnp.Device.
Get the model name.
Get the model name of this device.
Get name.
Get the name of the device.
Reinitialize.
Get the UDN.
Get the UDN of the device.
Get the unique id.
Get the USN.
Configuration for SSDP tests.
Set up device discovery callback. Set up device discovery callback. Load config_entry. | 581 | en | 0.76489 |
# Copyright The PyTorch Lightning team.
#
# 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.
from typing import Any
from transformers import AutoTokenizer
from flash.data.process import Postprocess
from flash.text.seq2seq.core.data import Seq2SeqData, Seq2SeqPreprocess
class SummarizationPostprocess(Postprocess):
def __init__(
self,
backbone: str = "sshleifer/tiny-mbart",
):
super().__init__()
# TODO: Should share the backbone or tokenizer over state
self.tokenizer = AutoTokenizer.from_pretrained(backbone, use_fast=True)
def uncollate(self, generated_tokens: Any) -> Any:
pred_str = self.tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)
pred_str = [str.strip(s) for s in pred_str]
return pred_str
class SummarizationData(Seq2SeqData):
preprocess_cls = Seq2SeqPreprocess
postprocess_cls = SummarizationPostprocess
| flash/text/seq2seq/summarization/data.py | 1,424 | Copyright The PyTorch Lightning team. 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. TODO: Should share the backbone or tokenizer over state | 615 | en | 0.850169 |
"""Testing utilities.
"""
import os.path
import pytest
def test():
"""Initiate poliastro testing
"""
pytest.main([os.path.dirname(os.path.abspath(__file__))])
| src/poliastro/testing.py | 176 | Initiate poliastro testing
Testing utilities. | 51 | en | 0.622834 |
# -*- coding: utf-8 -*-
"""Plot to demonstrate the qualitative1 colormap.
"""
import numpy as np
import matplotlib.pyplot as plt
from typhon.plots import (figsize, cmap2rgba)
x = np.linspace(0, 10, 100)
fig, ax = plt.subplots(figsize=figsize(10))
ax.set_prop_cycle(color=cmap2rgba('qualitative1', 7))
for c in np.arange(1, 8):
ax.plot(x, (15 + x) * c, linewidth=3)
ax.set_xlim(x.min(), x.max())
fig.tight_layout()
plt.show()
| doc/pyplots/plot_qualitative1.py | 436 | Plot to demonstrate the qualitative1 colormap.
-*- coding: utf-8 -*- | 70 | en | 0.760589 |
# 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 logging
import time
from common import network_metrics
from telemetry.page import page_test
from telemetry.value import scalar
CHROME_PROXY_VIA_HEADER = 'Chrome-Compression-Proxy'
class ChromeProxyMetricException(page_test.MeasurementFailure):
pass
class ChromeProxyResponse(network_metrics.HTTPResponse):
""" Represents an HTTP response from a timeline event."""
def __init__(self, event):
super(ChromeProxyResponse, self).__init__(event)
def ShouldHaveChromeProxyViaHeader(self):
resp = self.response
# Ignore https and data url
if resp.url.startswith('https') or resp.url.startswith('data:'):
return False
# Ignore 304 Not Modified and cache hit.
if resp.status == 304 or resp.served_from_cache:
return False
# Ignore invalid responses that don't have any header. Log a warning.
if not resp.headers:
logging.warning('response for %s does not any have header '
'(refer=%s, status=%s)',
resp.url, resp.GetHeader('Referer'), resp.status)
return False
return True
def HasResponseHeader(self, key, value):
response_header = self.response.GetHeader(key)
if not response_header:
return False
values = [v.strip() for v in response_header.split(',')]
return any(v == value for v in values)
def HasRequestHeader(self, key, value):
if key not in self.response.request_headers:
return False
request_header = self.response.request_headers[key]
values = [v.strip() for v in request_header.split(',')]
return any(v == value for v in values)
def HasChromeProxyViaHeader(self):
via_header = self.response.GetHeader('Via')
if not via_header:
return False
vias = [v.strip(' ') for v in via_header.split(',')]
# The Via header is valid if it has a 4-character version prefix followed by
# the proxy name, for example, "1.1 Chrome-Compression-Proxy".
return any(v[4:] == CHROME_PROXY_VIA_HEADER for v in vias)
def HasExtraViaHeader(self, extra_header):
return self.HasResponseHeader('Via', extra_header)
def IsValidByViaHeader(self):
return (not self.ShouldHaveChromeProxyViaHeader() or
self.HasChromeProxyViaHeader())
def GetChromeProxyRequestHeaderValue(self, key):
"""Get a specific Chrome-Proxy request header value.
Returns:
The value for a specific Chrome-Proxy request header value for a
given key. Returns None if no such key is present.
"""
if 'Chrome-Proxy' not in self.response.request_headers:
return None
chrome_proxy_request_header = self.response.request_headers['Chrome-Proxy']
values = [v.strip() for v in chrome_proxy_request_header.split(',')]
for value in values:
kvp = value.split('=', 1)
if len(kvp) == 2 and kvp[0].strip() == key:
return kvp[1].strip()
return None
def GetChromeProxyClientType(self):
"""Get the client type directive from the Chrome-Proxy request header.
Returns:
The client type directive from the Chrome-Proxy request header for the
request that lead to this response. For example, if the request header
"Chrome-Proxy: c=android" is present, then this method would return
"android". Returns None if no client type directive is present.
"""
return self.GetChromeProxyRequestHeaderValue('c')
def HasChromeProxyLoFiRequest(self):
return self.HasRequestHeader('Chrome-Proxy', "q=low")
def HasChromeProxyLoFiResponse(self):
return self.HasResponseHeader('Chrome-Proxy', "q=low")
def HasChromeProxyPassThroughRequest(self):
return self.HasRequestHeader('Chrome-Proxy', "pass-through")
| third_party/webrtc/src/chromium/src/tools/chrome_proxy/common/chrome_proxy_metrics.py | 3,854 | Represents an HTTP response from a timeline event.
Get the client type directive from the Chrome-Proxy request header.
Returns:
The client type directive from the Chrome-Proxy request header for the
request that lead to this response. For example, if the request header
"Chrome-Proxy: c=android" is present, then this method would return
"android". Returns None if no client type directive is present.
Get a specific Chrome-Proxy request header value.
Returns:
The value for a specific Chrome-Proxy request header value for a
given key. Returns None if no such key is present.
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. Ignore https and data url Ignore 304 Not Modified and cache hit. Ignore invalid responses that don't have any header. Log a warning. The Via header is valid if it has a 4-character version prefix followed by the proxy name, for example, "1.1 Chrome-Compression-Proxy". | 1,029 | en | 0.785076 |
import importlib
import logging
import os
import sys
from pathlib import Path
logger = logging.getLogger('second.utils.loader')
CUSTOM_LOADED_MODULES = {}
def _get_possible_module_path(paths):
ret = []
for p in paths:
p = Path(p)
for path in p.glob("*"):
if path.suffix in ["py", ".so"] or (path.is_dir()):
if path.stem.isidentifier():
ret.append(path)
return ret
def _get_regular_import_name(path, module_paths):
path = Path(path)
for mp in module_paths:
mp = Path(mp)
if mp == path:
return path.stem
try:
relative_path = path.relative_to(Path(mp))
parts = list((relative_path.parent / relative_path.stem).parts)
module_name = '.'.join([mp.stem] + parts)
return module_name
except:
pass
return None
def import_file(path, name: str = None, add_to_sys=True,
disable_warning=False):
global CUSTOM_LOADED_MODULES
path = Path(path)
module_name = path.stem
try:
user_paths = os.environ['PYTHONPATH'].split(os.pathsep)
except KeyError:
user_paths = []
possible_paths = _get_possible_module_path(user_paths)
model_import_name = _get_regular_import_name(path, possible_paths)
if model_import_name is not None:
return import_name(model_import_name)
if name is not None:
module_name = name
spec = importlib.util.spec_from_file_location(module_name, path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
if not disable_warning:
logger.warning((
f"Failed to perform regular import for file {path}. "
"this means this file isn't in any folder in PYTHONPATH "
"or don't have __init__.py in that project. "
"directly file import may fail and some reflecting features are "
"disabled even if import succeed. please add your project to PYTHONPATH "
"or add __init__.py to ensure this file can be regularly imported. "
))
if add_to_sys: # this will enable find objects defined in a file.
# avoid replace system modules.
if module_name in sys.modules and module_name not in CUSTOM_LOADED_MODULES:
raise ValueError(f"{module_name} exists in system.")
CUSTOM_LOADED_MODULES[module_name] = module
sys.modules[module_name] = module
return module
def import_name(name, package=None):
module = importlib.import_module(name, package)
return module
| second/utils/loader.py | 2,605 | this will enable find objects defined in a file. avoid replace system modules. | 78 | en | 0.477358 |
"""
This module defines the policies that will be used in order to sample the information flow
patterns to compare with.
The general approach is a function that takes in any eventual parameters and outputs a list of
pairs of DB_Ids for which the flow will be calculated.
"""
import random
import hashlib
import json
import numpy as np
from typing import Union, List, Tuple
import collections.abc
from bioflow.utils.log_behavior import get_logger
from bioflow.utils.general_utils import _is_int
log = get_logger(__name__)
def matched_sample_distribution(floats_arr: np.array, samples_no: int,
granularity: int = 100, logmode: bool = False) -> np.array:
"""
Tries to guess a distribution of floats and sample from it.
uses np.histogram with the number of bins equal to the granularity parameter. For each
sample, selects which bin to sample and then picks from the bin a float according to a
uniform distribution. if logmode is enabled, histogram will be in the log-space, as well as
the sampling.
:param floats_arr: array of floats for which to match the distribution
:param samples_no: number of random samples to retrieve
:param granularity: granularity at which to operate
:param logmode: if sample in log-space
:return: samples drawn from the empirically matched distribution
"""
if logmode:
floats_arr = np.log(floats_arr) # will crash if any are 0
hist, bin_edges = np.histogram(floats_arr, bins=granularity, density=True)
pad = np.arange(granularity)
locations = np.choice(pad, samples_no, p=hist)
samples = []
for i in locations:
samples.append(np.random.uniform(bin_edges[i], bin_edges[i+1]))
if logmode:
return np.exp(samples)
else:
return samples
def _reduce_distribution(floats_arr: np.array):
"""
Basically gets a distribution in the [0, 1] in 100 bins, rounds to the nearest 0.01. Used for
hashing and distribution matching
:param floats_arr: floats for which to calculate the rounded distribution
:return: rounded distribution
"""
normalized_arr = floats_arr / np.max(floats_arr)
bins = np.linspace(0, 1.001, 101) # because floats round funny
hist, bin_edges = np.histogram(normalized_arr, bins=bins, density=True)
rounded_hist = np.array(hist * 100).astype(np.int)
return rounded_hist
def _characterize_set(sample: Union[List[int], List[Tuple[int, float]]]):
"""
None-robust helper function to characterize a sample set by its length, nature of items in
teh sample and eventual distribution of weights within the sample.
:param sample: sample to characterize
:return: set length (0 if None), 1 if items are ids, 2 if ids and weights (0 if
None), rounded distribution ([] if None or items are ids)
"""
if sample is None:
return 0, 0, []
if len(sample) == 1:
if _is_int(sample[0]):
return 1, 1, []
else:
return 1, 2, []
if _is_int(sample[0]):
rounded_hist = [1] * 100
rounded_hist = np.array(rounded_hist).astype(np.int)
return len(sample), 1, rounded_hist.tolist()
else:
rounded_hist = _reduce_distribution(np.array(sample).astype(np.float)[:, 1])
return len(sample), 2, rounded_hist.tolist()
def characterize_flow_parameters(sample: Union[List[int], List[Tuple[int, float]]],
secondary_sample: Union[List[int], List[Tuple[int, float]], None],
sparse_rounds: int):
"""
Characterizes the primary and secondary sets and computes their hash, that can be used ot
match similar samples for random sampling.
:param sample: primary set
:param secondary_sample: secondary set
:param sparse_rounds: if sparse rounds are to be performed
:return: first set length, shape, hist, second set length, shape, hist, sparse rounds, hash
"""
prim_len, prim_shape, prim_hist = _characterize_set(sample)
sec_len, sec_shape, sec_hist = _characterize_set(secondary_sample)
_hash = hashlib.md5(json.dumps([prim_len, prim_shape, prim_hist,
sec_len, sec_shape, sec_hist,
sparse_rounds]).encode('utf-8')).hexdigest()
log.debug('hashed a flow parameters from:\n'
'%d/%d/%s; \n'
'%d/%d/%s; \n'
'%d \n'
'to %s' % (prim_len, prim_shape, prim_hist,
sec_len, sec_shape, sec_hist,
sparse_rounds, _hash))
return prim_len, prim_shape, prim_hist, sec_len, sec_shape, sec_hist, sparse_rounds, _hash
def _sample_floats(floats, float_sampling_method='exact', matched_distro_precision: int = 100):
"""
A wrapper methods to sample a float distribution according to a method
:param floats:
:param float_sampling_method: exact (permutation of weights) | distro (trying to match the
empirical distribution) | logdistro (trying to match the empirical distribution in the log
space)
:param matched_distro_precision: how closely to try to match the distribution (granularity
parameter pass-through to the matched_sample_distribution)
:return: sample of floats
"""
if float_sampling_method == 'exact':
ret_floats = floats.copy()
np.random.shuffle(ret_floats)
return ret_floats
if float_sampling_method == 'distro':
return matched_sample_distribution(floats, len(floats), granularity=matched_distro_precision)
if float_sampling_method == 'logdistro':
return matched_sample_distribution(floats, len(floats),
granularity=matched_distro_precision, logmode=True)
def matched_sampling(sample, secondary_sample,
background, samples, float_sampling_method='exact'):
"""
The general random sampling strategy that sample sets of the same size and shape as primary
and secondary sample set and, if they are weighted, try to match the random sample weights
according to the
:param sample: primary sample set
:param secondary_sample: secondary sample_set
:param background: background of ids (and potentially weights) from which to sample
:param samples: random samples wanted
:param sampling_mode: exact/distro/logdistro. the sampling parametrization method ingesting
all the parameters in a single string argument in the general case, here, a pass- through
parameter for the _sample_floats function if samples are weighted and the distribution of
weights is being matched.
:return:
"""
# What if we have an overlap between the items in the primary and the secondary
# samples? => sampling will always try to separate the two, the sampling will crash if there
# is not enough bacground to separate the two.
if _is_int(background[0]):
background_ids = np.array(background)
background_whg = np.ones_like(background_ids).astype(np.float)
else:
background_ids = np.array(background)[:, 0]
background_whg = np.array(background)[:, 1]
log.debug('debug sum %s, type: %s, all:%s' % (np.sum(background_whg),
type(background_whg),
background_whg))
background_whg /= np.sum(background_whg)
if secondary_sample is None:
if _is_int(sample[0]): # it should never be an int, but for safety ...
for i in range(0, samples):
selected = np.random.choice(background_ids, len(sample), p=background_whg,
replace=False)
yield i, selected, None
else:
for i in range(0, samples):
id_loads = np.random.choice(background_ids, len(sample), p=background_whg,
replace=False)
float_part = _sample_floats(np.array(sample)[:, 1], float_sampling_method)
ids_and_floats = [(_id, _float) for _id, _float in zip(id_loads, float_part)]
yield i, ids_and_floats, None
else:
if _is_int(sample[0]):
for i in range(0, samples):
selected = np.random.choice(background_ids,
len(sample)+len(secondary_sample),
p=background_whg, replace=False)
np.random.shuffle(selected)
yield i, selected[:len(sample)], selected[-len(secondary_sample):]
else:
for i in range(0, samples):
selected = np.random.choice(background_ids,
len(sample)+len(secondary_sample),
p=background_whg, replace=False)
np.random.shuffle(selected)
id_loads = selected[:len(sample)]
float_part = _sample_floats(np.array(sample)[:, 1], float_sampling_method)
ids_and_floats = [(_id, _float) for _id, _float in zip(id_loads, float_part)]
sec_id_loads = selected[-len(secondary_sample):]
sec_float_part = _sample_floats(np.array(secondary_sample)[:, 1], float_sampling_method)
sec_ids_and_floats = [(_id, _float) for _id, _float
in zip(sec_id_loads, sec_float_part)]
yield i, ids_and_floats, sec_ids_and_floats | bioflow/algorithms_bank/sampling_policies.py | 9,578 | None-robust helper function to characterize a sample set by its length, nature of items in
teh sample and eventual distribution of weights within the sample.
:param sample: sample to characterize
:return: set length (0 if None), 1 if items are ids, 2 if ids and weights (0 if
None), rounded distribution ([] if None or items are ids)
Basically gets a distribution in the [0, 1] in 100 bins, rounds to the nearest 0.01. Used for
hashing and distribution matching
:param floats_arr: floats for which to calculate the rounded distribution
:return: rounded distribution
A wrapper methods to sample a float distribution according to a method
:param floats:
:param float_sampling_method: exact (permutation of weights) | distro (trying to match the
empirical distribution) | logdistro (trying to match the empirical distribution in the log
space)
:param matched_distro_precision: how closely to try to match the distribution (granularity
parameter pass-through to the matched_sample_distribution)
:return: sample of floats
Characterizes the primary and secondary sets and computes their hash, that can be used ot
match similar samples for random sampling.
:param sample: primary set
:param secondary_sample: secondary set
:param sparse_rounds: if sparse rounds are to be performed
:return: first set length, shape, hist, second set length, shape, hist, sparse rounds, hash
Tries to guess a distribution of floats and sample from it.
uses np.histogram with the number of bins equal to the granularity parameter. For each
sample, selects which bin to sample and then picks from the bin a float according to a
uniform distribution. if logmode is enabled, histogram will be in the log-space, as well as
the sampling.
:param floats_arr: array of floats for which to match the distribution
:param samples_no: number of random samples to retrieve
:param granularity: granularity at which to operate
:param logmode: if sample in log-space
:return: samples drawn from the empirically matched distribution
The general random sampling strategy that sample sets of the same size and shape as primary
and secondary sample set and, if they are weighted, try to match the random sample weights
according to the
:param sample: primary sample set
:param secondary_sample: secondary sample_set
:param background: background of ids (and potentially weights) from which to sample
:param samples: random samples wanted
:param sampling_mode: exact/distro/logdistro. the sampling parametrization method ingesting
all the parameters in a single string argument in the general case, here, a pass- through
parameter for the _sample_floats function if samples are weighted and the distribution of
weights is being matched.
:return:
This module defines the policies that will be used in order to sample the information flow
patterns to compare with.
The general approach is a function that takes in any eventual parameters and outputs a list of
pairs of DB_Ids for which the flow will be calculated.
will crash if any are 0 because floats round funny What if we have an overlap between the items in the primary and the secondary samples? => sampling will always try to separate the two, the sampling will crash if there is not enough bacground to separate the two. it should never be an int, but for safety ... | 3,297 | en | 0.877645 |
# Databricks notebook source
# MAGIC %md-sandbox
# MAGIC
# MAGIC <div style="text-align: center; line-height: 0; padding-top: 9px;">
# MAGIC <img src="https://databricks.com/wp-content/uploads/2018/03/db-academy-rgb-1200px.png" alt="Databricks Learning" style="width: 600px">
# MAGIC </div>
# COMMAND ----------
# MAGIC %md
# MAGIC # Parsing Errors
# MAGIC
# MAGIC This code is going to throw several errors. Click on **`Run All`** above.
# COMMAND ----------
# MAGIC %run ../Includes/module-4/setup-lesson-4.02
# COMMAND ----------
# ANSWER
x = 1
x * 7
# COMMAND ----------
# MAGIC %md
# MAGIC Note that **`Run All`** execution mimics scheduled job execution; the **`Command skipped`** output we see below is the same we'll see in a job result.
# COMMAND ----------
# ANSWER
y = 99.52
y // 1
# COMMAND ----------
# MAGIC %md
# MAGIC The above is what we see when have Python errors
# COMMAND ----------
# ANSWER
import pyspark.sql.functions as F
# COMMAND ----------
# MAGIC %md
# MAGIC Let's look at a Spark error.
# MAGIC
# MAGIC While running multiple commands in a single cell, it can sometimes to be difficult to parse where an error is coming from.
# COMMAND ----------
# ANSWER
df = (spark.read
.format("csv")
.option("header", True)
.schema("date DATE, temp INTEGER")
.load("/databricks-datasets/weather/low_temps"))
df.createOrReplaceTempView("low_temps")
df.join(df, "date").groupBy("date").count()
# COMMAND ----------
# MAGIC %md
# MAGIC Sometimes an error isn't an error, but doesn't achieve what was intended.
# COMMAND ----------
# MAGIC %sql
# MAGIC -- ANSWER
# MAGIC SELECT dayofmonth(date) FROM low_temps
# COMMAND ----------
# MAGIC %md
# MAGIC Use the below cell to figure out how to fix the code above.
# COMMAND ----------
display(df)
# COMMAND ----------
# MAGIC %md
# MAGIC Column names cause common errors when trying to save tables.
# COMMAND ----------
# MAGIC %sql
# MAGIC -- ANSWER
# MAGIC CREATE TABLE test_table
# MAGIC AS (
# MAGIC SELECT dayofmonth(date) % 3 three_day_cycle FROM low_temps
# MAGIC )
# COMMAND ----------
# MAGIC %md
# MAGIC Run the following cell to delete the tables and files associated with this lesson.
# COMMAND ----------
DA.cleanup()
# COMMAND ----------
# MAGIC %md-sandbox
# MAGIC © 2022 Databricks, Inc. All rights reserved.<br/>
# MAGIC Apache, Apache Spark, Spark and the Spark logo are trademarks of the <a href="https://www.apache.org/">Apache Software Foundation</a>.<br/>
# MAGIC <br/>
# MAGIC <a href="https://databricks.com/privacy-policy">Privacy Policy</a> | <a href="https://databricks.com/terms-of-use">Terms of Use</a> | <a href="https://help.databricks.com/">Support</a>
| Advanced-Data-Engineering-with-Databricks/Solutions/04 - Databricks in Production/ADE 4.02 - Error Prone.py | 2,715 | Databricks notebook source MAGIC %md-sandbox MAGIC MAGIC <div style="text-align: center; line-height: 0; padding-top: 9px;"> MAGIC <img src="https://databricks.com/wp-content/uploads/2018/03/db-academy-rgb-1200px.png" alt="Databricks Learning" style="width: 600px"> MAGIC </div> COMMAND ---------- MAGIC %md MAGIC Parsing Errors MAGIC MAGIC This code is going to throw several errors. Click on **`Run All`** above. COMMAND ---------- MAGIC %run ../Includes/module-4/setup-lesson-4.02 COMMAND ---------- ANSWER COMMAND ---------- MAGIC %md MAGIC Note that **`Run All`** execution mimics scheduled job execution; the **`Command skipped`** output we see below is the same we'll see in a job result. COMMAND ---------- ANSWER COMMAND ---------- MAGIC %md MAGIC The above is what we see when have Python errors COMMAND ---------- ANSWER COMMAND ---------- MAGIC %md MAGIC Let's look at a Spark error. MAGIC MAGIC While running multiple commands in a single cell, it can sometimes to be difficult to parse where an error is coming from. COMMAND ---------- ANSWER COMMAND ---------- MAGIC %md MAGIC Sometimes an error isn't an error, but doesn't achieve what was intended. COMMAND ---------- MAGIC %sql MAGIC -- ANSWER MAGIC SELECT dayofmonth(date) FROM low_temps COMMAND ---------- MAGIC %md MAGIC Use the below cell to figure out how to fix the code above. COMMAND ---------- COMMAND ---------- MAGIC %md MAGIC Column names cause common errors when trying to save tables. COMMAND ---------- MAGIC %sql MAGIC -- ANSWER MAGIC CREATE TABLE test_table MAGIC AS ( MAGIC SELECT dayofmonth(date) % 3 three_day_cycle FROM low_temps MAGIC ) COMMAND ---------- MAGIC %md MAGIC Run the following cell to delete the tables and files associated with this lesson. COMMAND ---------- COMMAND ---------- MAGIC %md-sandbox MAGIC © 2022 Databricks, Inc. All rights reserved.<br/> MAGIC Apache, Apache Spark, Spark and the Spark logo are trademarks of the <a href="https://www.apache.org/">Apache Software Foundation</a>.<br/> MAGIC <br/> MAGIC <a href="https://databricks.com/privacy-policy">Privacy Policy</a> | <a href="https://databricks.com/terms-of-use">Terms of Use</a> | <a href="https://help.databricks.com/">Support</a> | 2,221 | en | 0.638572 |
# coding=utf-8
# *** WARNING: this file was generated by the Pulumi SDK Generator. ***
# *** Do not edit by hand unless you're certain you know what you are doing! ***
import warnings
import pulumi
import pulumi.runtime
from typing import Any, Mapping, Optional, Sequence, Union, overload
from ... import _utilities
from . import outputs
from ._enums import *
from ._inputs import *
__all__ = ['PrivateEndpointArgs', 'PrivateEndpoint']
@pulumi.input_type
class PrivateEndpointArgs:
def __init__(__self__, *,
resource_group_name: pulumi.Input[str],
id: Optional[pulumi.Input[str]] = None,
location: Optional[pulumi.Input[str]] = None,
manual_private_link_service_connections: Optional[pulumi.Input[Sequence[pulumi.Input['PrivateLinkServiceConnectionArgs']]]] = None,
private_endpoint_name: Optional[pulumi.Input[str]] = None,
private_link_service_connections: Optional[pulumi.Input[Sequence[pulumi.Input['PrivateLinkServiceConnectionArgs']]]] = None,
subnet: Optional[pulumi.Input['SubnetArgs']] = None,
tags: Optional[pulumi.Input[Mapping[str, pulumi.Input[str]]]] = None):
"""
The set of arguments for constructing a PrivateEndpoint resource.
:param pulumi.Input[str] resource_group_name: The name of the resource group.
:param pulumi.Input[str] id: Resource ID.
:param pulumi.Input[str] location: Resource location.
:param pulumi.Input[Sequence[pulumi.Input['PrivateLinkServiceConnectionArgs']]] manual_private_link_service_connections: A grouping of information about the connection to the remote resource. Used when the network admin does not have access to approve connections to the remote resource.
:param pulumi.Input[str] private_endpoint_name: The name of the private endpoint.
:param pulumi.Input[Sequence[pulumi.Input['PrivateLinkServiceConnectionArgs']]] private_link_service_connections: A grouping of information about the connection to the remote resource.
:param pulumi.Input['SubnetArgs'] subnet: The ID of the subnet from which the private IP will be allocated.
:param pulumi.Input[Mapping[str, pulumi.Input[str]]] tags: Resource tags.
"""
pulumi.set(__self__, "resource_group_name", resource_group_name)
if id is not None:
pulumi.set(__self__, "id", id)
if location is not None:
pulumi.set(__self__, "location", location)
if manual_private_link_service_connections is not None:
pulumi.set(__self__, "manual_private_link_service_connections", manual_private_link_service_connections)
if private_endpoint_name is not None:
pulumi.set(__self__, "private_endpoint_name", private_endpoint_name)
if private_link_service_connections is not None:
pulumi.set(__self__, "private_link_service_connections", private_link_service_connections)
if subnet is not None:
pulumi.set(__self__, "subnet", subnet)
if tags is not None:
pulumi.set(__self__, "tags", tags)
@property
@pulumi.getter(name="resourceGroupName")
def resource_group_name(self) -> pulumi.Input[str]:
"""
The name of the resource group.
"""
return pulumi.get(self, "resource_group_name")
@resource_group_name.setter
def resource_group_name(self, value: pulumi.Input[str]):
pulumi.set(self, "resource_group_name", value)
@property
@pulumi.getter
def id(self) -> Optional[pulumi.Input[str]]:
"""
Resource ID.
"""
return pulumi.get(self, "id")
@id.setter
def id(self, value: Optional[pulumi.Input[str]]):
pulumi.set(self, "id", value)
@property
@pulumi.getter
def location(self) -> Optional[pulumi.Input[str]]:
"""
Resource location.
"""
return pulumi.get(self, "location")
@location.setter
def location(self, value: Optional[pulumi.Input[str]]):
pulumi.set(self, "location", value)
@property
@pulumi.getter(name="manualPrivateLinkServiceConnections")
def manual_private_link_service_connections(self) -> Optional[pulumi.Input[Sequence[pulumi.Input['PrivateLinkServiceConnectionArgs']]]]:
"""
A grouping of information about the connection to the remote resource. Used when the network admin does not have access to approve connections to the remote resource.
"""
return pulumi.get(self, "manual_private_link_service_connections")
@manual_private_link_service_connections.setter
def manual_private_link_service_connections(self, value: Optional[pulumi.Input[Sequence[pulumi.Input['PrivateLinkServiceConnectionArgs']]]]):
pulumi.set(self, "manual_private_link_service_connections", value)
@property
@pulumi.getter(name="privateEndpointName")
def private_endpoint_name(self) -> Optional[pulumi.Input[str]]:
"""
The name of the private endpoint.
"""
return pulumi.get(self, "private_endpoint_name")
@private_endpoint_name.setter
def private_endpoint_name(self, value: Optional[pulumi.Input[str]]):
pulumi.set(self, "private_endpoint_name", value)
@property
@pulumi.getter(name="privateLinkServiceConnections")
def private_link_service_connections(self) -> Optional[pulumi.Input[Sequence[pulumi.Input['PrivateLinkServiceConnectionArgs']]]]:
"""
A grouping of information about the connection to the remote resource.
"""
return pulumi.get(self, "private_link_service_connections")
@private_link_service_connections.setter
def private_link_service_connections(self, value: Optional[pulumi.Input[Sequence[pulumi.Input['PrivateLinkServiceConnectionArgs']]]]):
pulumi.set(self, "private_link_service_connections", value)
@property
@pulumi.getter
def subnet(self) -> Optional[pulumi.Input['SubnetArgs']]:
"""
The ID of the subnet from which the private IP will be allocated.
"""
return pulumi.get(self, "subnet")
@subnet.setter
def subnet(self, value: Optional[pulumi.Input['SubnetArgs']]):
pulumi.set(self, "subnet", value)
@property
@pulumi.getter
def tags(self) -> Optional[pulumi.Input[Mapping[str, pulumi.Input[str]]]]:
"""
Resource tags.
"""
return pulumi.get(self, "tags")
@tags.setter
def tags(self, value: Optional[pulumi.Input[Mapping[str, pulumi.Input[str]]]]):
pulumi.set(self, "tags", value)
class PrivateEndpoint(pulumi.CustomResource):
@overload
def __init__(__self__,
resource_name: str,
opts: Optional[pulumi.ResourceOptions] = None,
id: Optional[pulumi.Input[str]] = None,
location: Optional[pulumi.Input[str]] = None,
manual_private_link_service_connections: Optional[pulumi.Input[Sequence[pulumi.Input[pulumi.InputType['PrivateLinkServiceConnectionArgs']]]]] = None,
private_endpoint_name: Optional[pulumi.Input[str]] = None,
private_link_service_connections: Optional[pulumi.Input[Sequence[pulumi.Input[pulumi.InputType['PrivateLinkServiceConnectionArgs']]]]] = None,
resource_group_name: Optional[pulumi.Input[str]] = None,
subnet: Optional[pulumi.Input[pulumi.InputType['SubnetArgs']]] = None,
tags: Optional[pulumi.Input[Mapping[str, pulumi.Input[str]]]] = None,
__props__=None):
"""
Private endpoint resource.
:param str resource_name: The name of the resource.
:param pulumi.ResourceOptions opts: Options for the resource.
:param pulumi.Input[str] id: Resource ID.
:param pulumi.Input[str] location: Resource location.
:param pulumi.Input[Sequence[pulumi.Input[pulumi.InputType['PrivateLinkServiceConnectionArgs']]]] manual_private_link_service_connections: A grouping of information about the connection to the remote resource. Used when the network admin does not have access to approve connections to the remote resource.
:param pulumi.Input[str] private_endpoint_name: The name of the private endpoint.
:param pulumi.Input[Sequence[pulumi.Input[pulumi.InputType['PrivateLinkServiceConnectionArgs']]]] private_link_service_connections: A grouping of information about the connection to the remote resource.
:param pulumi.Input[str] resource_group_name: The name of the resource group.
:param pulumi.Input[pulumi.InputType['SubnetArgs']] subnet: The ID of the subnet from which the private IP will be allocated.
:param pulumi.Input[Mapping[str, pulumi.Input[str]]] tags: Resource tags.
"""
...
@overload
def __init__(__self__,
resource_name: str,
args: PrivateEndpointArgs,
opts: Optional[pulumi.ResourceOptions] = None):
"""
Private endpoint resource.
:param str resource_name: The name of the resource.
:param PrivateEndpointArgs args: The arguments to use to populate this resource's properties.
:param pulumi.ResourceOptions opts: Options for the resource.
"""
...
def __init__(__self__, resource_name: str, *args, **kwargs):
resource_args, opts = _utilities.get_resource_args_opts(PrivateEndpointArgs, pulumi.ResourceOptions, *args, **kwargs)
if resource_args is not None:
__self__._internal_init(resource_name, opts, **resource_args.__dict__)
else:
__self__._internal_init(resource_name, *args, **kwargs)
def _internal_init(__self__,
resource_name: str,
opts: Optional[pulumi.ResourceOptions] = None,
id: Optional[pulumi.Input[str]] = None,
location: Optional[pulumi.Input[str]] = None,
manual_private_link_service_connections: Optional[pulumi.Input[Sequence[pulumi.Input[pulumi.InputType['PrivateLinkServiceConnectionArgs']]]]] = None,
private_endpoint_name: Optional[pulumi.Input[str]] = None,
private_link_service_connections: Optional[pulumi.Input[Sequence[pulumi.Input[pulumi.InputType['PrivateLinkServiceConnectionArgs']]]]] = None,
resource_group_name: Optional[pulumi.Input[str]] = None,
subnet: Optional[pulumi.Input[pulumi.InputType['SubnetArgs']]] = None,
tags: Optional[pulumi.Input[Mapping[str, pulumi.Input[str]]]] = None,
__props__=None):
if opts is None:
opts = pulumi.ResourceOptions()
if not isinstance(opts, pulumi.ResourceOptions):
raise TypeError('Expected resource options to be a ResourceOptions instance')
if opts.version is None:
opts.version = _utilities.get_version()
if opts.id is None:
if __props__ is not None:
raise TypeError('__props__ is only valid when passed in combination with a valid opts.id to get an existing resource')
__props__ = PrivateEndpointArgs.__new__(PrivateEndpointArgs)
__props__.__dict__["id"] = id
__props__.__dict__["location"] = location
__props__.__dict__["manual_private_link_service_connections"] = manual_private_link_service_connections
__props__.__dict__["private_endpoint_name"] = private_endpoint_name
__props__.__dict__["private_link_service_connections"] = private_link_service_connections
if resource_group_name is None and not opts.urn:
raise TypeError("Missing required property 'resource_group_name'")
__props__.__dict__["resource_group_name"] = resource_group_name
__props__.__dict__["subnet"] = subnet
__props__.__dict__["tags"] = tags
__props__.__dict__["etag"] = None
__props__.__dict__["name"] = None
__props__.__dict__["network_interfaces"] = None
__props__.__dict__["provisioning_state"] = None
__props__.__dict__["type"] = None
alias_opts = pulumi.ResourceOptions(aliases=[pulumi.Alias(type_="azure-nextgen:network/v20190901:PrivateEndpoint"), pulumi.Alias(type_="azure-native:network:PrivateEndpoint"), pulumi.Alias(type_="azure-nextgen:network:PrivateEndpoint"), pulumi.Alias(type_="azure-native:network/v20180801:PrivateEndpoint"), pulumi.Alias(type_="azure-nextgen:network/v20180801:PrivateEndpoint"), pulumi.Alias(type_="azure-native:network/v20181001:PrivateEndpoint"), pulumi.Alias(type_="azure-nextgen:network/v20181001:PrivateEndpoint"), pulumi.Alias(type_="azure-native:network/v20181101:PrivateEndpoint"), pulumi.Alias(type_="azure-nextgen:network/v20181101:PrivateEndpoint"), pulumi.Alias(type_="azure-native:network/v20181201:PrivateEndpoint"), pulumi.Alias(type_="azure-nextgen:network/v20181201:PrivateEndpoint"), pulumi.Alias(type_="azure-native:network/v20190201:PrivateEndpoint"), pulumi.Alias(type_="azure-nextgen:network/v20190201:PrivateEndpoint"), pulumi.Alias(type_="azure-native:network/v20190401:PrivateEndpoint"), pulumi.Alias(type_="azure-nextgen:network/v20190401:PrivateEndpoint"), pulumi.Alias(type_="azure-native:network/v20190601:PrivateEndpoint"), pulumi.Alias(type_="azure-nextgen:network/v20190601:PrivateEndpoint"), pulumi.Alias(type_="azure-native:network/v20190701:PrivateEndpoint"), pulumi.Alias(type_="azure-nextgen:network/v20190701:PrivateEndpoint"), pulumi.Alias(type_="azure-native:network/v20190801:PrivateEndpoint"), pulumi.Alias(type_="azure-nextgen:network/v20190801:PrivateEndpoint"), pulumi.Alias(type_="azure-native:network/v20191101:PrivateEndpoint"), pulumi.Alias(type_="azure-nextgen:network/v20191101:PrivateEndpoint"), pulumi.Alias(type_="azure-native:network/v20191201:PrivateEndpoint"), pulumi.Alias(type_="azure-nextgen:network/v20191201:PrivateEndpoint"), pulumi.Alias(type_="azure-native:network/v20200301:PrivateEndpoint"), pulumi.Alias(type_="azure-nextgen:network/v20200301:PrivateEndpoint"), pulumi.Alias(type_="azure-native:network/v20200401:PrivateEndpoint"), pulumi.Alias(type_="azure-nextgen:network/v20200401:PrivateEndpoint"), pulumi.Alias(type_="azure-native:network/v20200501:PrivateEndpoint"), pulumi.Alias(type_="azure-nextgen:network/v20200501:PrivateEndpoint"), pulumi.Alias(type_="azure-native:network/v20200601:PrivateEndpoint"), pulumi.Alias(type_="azure-nextgen:network/v20200601:PrivateEndpoint"), pulumi.Alias(type_="azure-native:network/v20200701:PrivateEndpoint"), pulumi.Alias(type_="azure-nextgen:network/v20200701:PrivateEndpoint"), pulumi.Alias(type_="azure-native:network/v20200801:PrivateEndpoint"), pulumi.Alias(type_="azure-nextgen:network/v20200801:PrivateEndpoint"), pulumi.Alias(type_="azure-native:network/v20201101:PrivateEndpoint"), pulumi.Alias(type_="azure-nextgen:network/v20201101:PrivateEndpoint"), pulumi.Alias(type_="azure-native:network/v20210201:PrivateEndpoint"), pulumi.Alias(type_="azure-nextgen:network/v20210201:PrivateEndpoint"), pulumi.Alias(type_="azure-native:network/v20210301:PrivateEndpoint"), pulumi.Alias(type_="azure-nextgen:network/v20210301:PrivateEndpoint")])
opts = pulumi.ResourceOptions.merge(opts, alias_opts)
super(PrivateEndpoint, __self__).__init__(
'azure-native:network/v20190901:PrivateEndpoint',
resource_name,
__props__,
opts)
@staticmethod
def get(resource_name: str,
id: pulumi.Input[str],
opts: Optional[pulumi.ResourceOptions] = None) -> 'PrivateEndpoint':
"""
Get an existing PrivateEndpoint resource's state with the given name, id, and optional extra
properties used to qualify the lookup.
:param str resource_name: The unique name of the resulting resource.
:param pulumi.Input[str] id: The unique provider ID of the resource to lookup.
:param pulumi.ResourceOptions opts: Options for the resource.
"""
opts = pulumi.ResourceOptions.merge(opts, pulumi.ResourceOptions(id=id))
__props__ = PrivateEndpointArgs.__new__(PrivateEndpointArgs)
__props__.__dict__["etag"] = None
__props__.__dict__["location"] = None
__props__.__dict__["manual_private_link_service_connections"] = None
__props__.__dict__["name"] = None
__props__.__dict__["network_interfaces"] = None
__props__.__dict__["private_link_service_connections"] = None
__props__.__dict__["provisioning_state"] = None
__props__.__dict__["subnet"] = None
__props__.__dict__["tags"] = None
__props__.__dict__["type"] = None
return PrivateEndpoint(resource_name, opts=opts, __props__=__props__)
@property
@pulumi.getter
def etag(self) -> pulumi.Output[str]:
"""
A unique read-only string that changes whenever the resource is updated.
"""
return pulumi.get(self, "etag")
@property
@pulumi.getter
def location(self) -> pulumi.Output[Optional[str]]:
"""
Resource location.
"""
return pulumi.get(self, "location")
@property
@pulumi.getter(name="manualPrivateLinkServiceConnections")
def manual_private_link_service_connections(self) -> pulumi.Output[Optional[Sequence['outputs.PrivateLinkServiceConnectionResponse']]]:
"""
A grouping of information about the connection to the remote resource. Used when the network admin does not have access to approve connections to the remote resource.
"""
return pulumi.get(self, "manual_private_link_service_connections")
@property
@pulumi.getter
def name(self) -> pulumi.Output[str]:
"""
Resource name.
"""
return pulumi.get(self, "name")
@property
@pulumi.getter(name="networkInterfaces")
def network_interfaces(self) -> pulumi.Output[Sequence['outputs.NetworkInterfaceResponse']]:
"""
An array of references to the network interfaces created for this private endpoint.
"""
return pulumi.get(self, "network_interfaces")
@property
@pulumi.getter(name="privateLinkServiceConnections")
def private_link_service_connections(self) -> pulumi.Output[Optional[Sequence['outputs.PrivateLinkServiceConnectionResponse']]]:
"""
A grouping of information about the connection to the remote resource.
"""
return pulumi.get(self, "private_link_service_connections")
@property
@pulumi.getter(name="provisioningState")
def provisioning_state(self) -> pulumi.Output[str]:
"""
The provisioning state of the private endpoint resource.
"""
return pulumi.get(self, "provisioning_state")
@property
@pulumi.getter
def subnet(self) -> pulumi.Output[Optional['outputs.SubnetResponse']]:
"""
The ID of the subnet from which the private IP will be allocated.
"""
return pulumi.get(self, "subnet")
@property
@pulumi.getter
def tags(self) -> pulumi.Output[Optional[Mapping[str, str]]]:
"""
Resource tags.
"""
return pulumi.get(self, "tags")
@property
@pulumi.getter
def type(self) -> pulumi.Output[str]:
"""
Resource type.
"""
return pulumi.get(self, "type")
| sdk/python/pulumi_azure_native/network/v20190901/private_endpoint.py | 19,487 | The set of arguments for constructing a PrivateEndpoint resource.
:param pulumi.Input[str] resource_group_name: The name of the resource group.
:param pulumi.Input[str] id: Resource ID.
:param pulumi.Input[str] location: Resource location.
:param pulumi.Input[Sequence[pulumi.Input['PrivateLinkServiceConnectionArgs']]] manual_private_link_service_connections: A grouping of information about the connection to the remote resource. Used when the network admin does not have access to approve connections to the remote resource.
:param pulumi.Input[str] private_endpoint_name: The name of the private endpoint.
:param pulumi.Input[Sequence[pulumi.Input['PrivateLinkServiceConnectionArgs']]] private_link_service_connections: A grouping of information about the connection to the remote resource.
:param pulumi.Input['SubnetArgs'] subnet: The ID of the subnet from which the private IP will be allocated.
:param pulumi.Input[Mapping[str, pulumi.Input[str]]] tags: Resource tags.
Private endpoint resource.
:param str resource_name: The name of the resource.
:param pulumi.ResourceOptions opts: Options for the resource.
:param pulumi.Input[str] id: Resource ID.
:param pulumi.Input[str] location: Resource location.
:param pulumi.Input[Sequence[pulumi.Input[pulumi.InputType['PrivateLinkServiceConnectionArgs']]]] manual_private_link_service_connections: A grouping of information about the connection to the remote resource. Used when the network admin does not have access to approve connections to the remote resource.
:param pulumi.Input[str] private_endpoint_name: The name of the private endpoint.
:param pulumi.Input[Sequence[pulumi.Input[pulumi.InputType['PrivateLinkServiceConnectionArgs']]]] private_link_service_connections: A grouping of information about the connection to the remote resource.
:param pulumi.Input[str] resource_group_name: The name of the resource group.
:param pulumi.Input[pulumi.InputType['SubnetArgs']] subnet: The ID of the subnet from which the private IP will be allocated.
:param pulumi.Input[Mapping[str, pulumi.Input[str]]] tags: Resource tags.
Private endpoint resource.
:param str resource_name: The name of the resource.
:param PrivateEndpointArgs args: The arguments to use to populate this resource's properties.
:param pulumi.ResourceOptions opts: Options for the resource.
A unique read-only string that changes whenever the resource is updated.
Get an existing PrivateEndpoint resource's state with the given name, id, and optional extra
properties used to qualify the lookup.
:param str resource_name: The unique name of the resulting resource.
:param pulumi.Input[str] id: The unique provider ID of the resource to lookup.
:param pulumi.ResourceOptions opts: Options for the resource.
Resource ID.
Resource location.
Resource location.
A grouping of information about the connection to the remote resource. Used when the network admin does not have access to approve connections to the remote resource.
A grouping of information about the connection to the remote resource. Used when the network admin does not have access to approve connections to the remote resource.
Resource name.
An array of references to the network interfaces created for this private endpoint.
The name of the private endpoint.
A grouping of information about the connection to the remote resource.
A grouping of information about the connection to the remote resource.
The provisioning state of the private endpoint resource.
The name of the resource group.
The ID of the subnet from which the private IP will be allocated.
The ID of the subnet from which the private IP will be allocated.
Resource tags.
Resource tags.
Resource type.
coding=utf-8 *** WARNING: this file was generated by the Pulumi SDK Generator. *** *** Do not edit by hand unless you're certain you know what you are doing! *** | 3,825 | en | 0.704867 |
"""
Time complexity: O(n^2)
This sorting algorithm puts the smallest element in the first
place after the first iteration. Similarly, after the second
iteration, the second smallest value becomes the second value
of the list. The process continues and eventually the list
becomes sorted.
"""
for i in range(n):
for j in range(i+1, n):
if num[i] > num[j]:
num[i], num[j] = num[j], num[i]
| sorting-algorithms/selection_sort.py | 402 | Time complexity: O(n^2)
This sorting algorithm puts the smallest element in the first
place after the first iteration. Similarly, after the second
iteration, the second smallest value becomes the second value
of the list. The process continues and eventually the list
becomes sorted. | 284 | en | 0.910559 |
# -*- coding: utf-8 -*-
from django.conf import settings
VAPID_PUBLIC_KEY = getattr(settings, 'DJANGO_INFOPUSH_VAPID_PUBLIC_KEY', '')
VAPID_PRIVATE_KEY = getattr(settings, 'DJANGO_INFOPUSH_VAPID_PRIVATE_KEY', '')
VAPID_ADMIN_EMAIL = getattr(settings, 'DJANGO_INFOPUSH_VAPID_ADMIN_EMAIL', '')
FCM_SERVER_KEY = getattr(settings, 'DJANGO_INFOPUSH_FCM_SERVER_KEY', '')
FCM_SENDER_ID = getattr(settings, 'DJANGO_INFOPUSH_FCM_SENDER_ID', '')
# how many processes to use in a pushsend management command for parallel push
# 1 disables multiprocessing
PUSHSEND_WORKERS = int(getattr(settings, 'DJANGO_INFOPUSH_PUSHSEND_WORKERS', 3))
# default push icon
DEFAULT_ICON_URL = getattr(settings, 'DJANGO_INFOPUSH_DEFAULT_ICON_URL', "/static/push/img/icon.png")
MIN_ICON_W = int(getattr(settings, 'DJANGO_INFOPUSH_MIN_ICON_W', 192))
MIN_ICON_H = int(getattr(settings, 'DJANGO_INFOPUSH_MIN_ICON_H', 192))
# kb, max filesize of push icon
ICON_CUSTOM_MAX_FILESIZE = int(getattr(settings, 'DJANGO_INFOPUSH_ICON_CUSTOM_MAX_FILESIZE', 25))
# big push image for push in Chrome
# best aspect ration is 3:2 for desktop Chrome
# mobile Chrome will crop it vertically a little
# https://web-push-book.gauntface.com/chapter-05/02-display-a-notification/#image
MIN_BIG_IMAGE_W = int(getattr(settings, 'DJANGO_INFOPUSH_MIN_BIG_IMAGE_W', 1023))
MIN_BIG_IMAGE_H = int(getattr(settings, 'DJANGO_INFOPUSH_MIN_BIG_IMAGE_H', 682))
# kb, max filesize for big image
BIG_IMAGE_MAX_FILESIZE = int(getattr(settings, 'DJANGO_INFOPUSH_BIG_IMAGE_MAX_FILESIZE', 100))
# web-site as an "app" in manifest.json
# https://developers.google.com/web/updates/2014/11/Support-for-installable-web-apps-with-webapp-manifest-in-chrome-38-for-Android
APP_ICON_URLS = getattr(settings, 'DJANGO_INFOPUSH_APP_ICON_URLS', ["push/img/app_icon.png",])
# optional, https://developers.google.com/web/updates/2015/08/using-manifest-to-set-sitewide-theme-color
APP_THEME_COLOR = getattr(settings, 'DJANGO_INFOPUSH_APP_THEME_COLOR', None)
# optional, https://developers.google.com/web/tools/lighthouse/audits/custom-splash-screen
APP_BACKGROUND_COLOR = getattr(settings, 'DJANGO_INFOPUSH_APP_BACKGROUND_COLOR', None)
# error threshold after which we disable push subscription
ERROR_THRESHOLD = int(getattr(settings, 'DJANGO_INFOPUSH_ERROR_THRESHOLD', 30))
# do not change, it is here for easy import of this constant
GCM_URL = 'https://android.googleapis.com/gcm/send'
FCM_URL = 'https://fcm.googleapis.com/fcm/send'
# this setting allows to disable CSRF for django_infopush views only, if needed
USE_CSRF = bool(getattr(settings, 'DJANGO_INFOPUSH_USE_CSRF', True)) | push/settings.py | 2,607 | -*- coding: utf-8 -*- how many processes to use in a pushsend management command for parallel push 1 disables multiprocessing default push icon kb, max filesize of push icon big push image for push in Chrome best aspect ration is 3:2 for desktop Chrome mobile Chrome will crop it vertically a little https://web-push-book.gauntface.com/chapter-05/02-display-a-notification/image kb, max filesize for big image web-site as an "app" in manifest.json https://developers.google.com/web/updates/2014/11/Support-for-installable-web-apps-with-webapp-manifest-in-chrome-38-for-Android optional, https://developers.google.com/web/updates/2015/08/using-manifest-to-set-sitewide-theme-color optional, https://developers.google.com/web/tools/lighthouse/audits/custom-splash-screen error threshold after which we disable push subscription do not change, it is here for easy import of this constant this setting allows to disable CSRF for django_infopush views only, if needed | 962 | en | 0.743293 |
import numpy
from chainer import cuda
from chainer import function
from chainer.utils import type_check
class Concat(function.Function):
"""Concatenate multiple tensors towards specified axis."""
# concat along the channel dimension by default
def __init__(self, axis=1):
self.axis = axis
def check_type_forward(self, in_types):
type_check.expect(in_types.size() > 0)
type_check.expect(in_types[0].ndim >
type_check.Variable(self.axis, 'axis'))
ndim = in_types[0].ndim.eval()
for i in range(1, in_types.size().eval()):
type_check.expect(
in_types[0].dtype == in_types[i].dtype,
in_types[0].ndim == in_types[i].ndim,
)
for d in range(0, ndim):
if d == self.axis:
continue
type_check.expect(in_types[0].shape[d] == in_types[i].shape[d])
def forward(self, xs):
xp = cuda.get_array_module(*xs)
return xp.concatenate(xs, axis=self.axis),
def backward(self, xs, gy):
xp = cuda.get_array_module(*xs)
sizes = numpy.array([x.shape[self.axis] for x in xs[:-1]]).cumsum()
return xp.split(gy[0], sizes, axis=self.axis)
def concat(xs, axis=1):
"""Concatenates given variables along an axis.
Args:
xs (tuple of Variables): Variables to be concatenated.
axis (int): Axis that the input arrays are concatenated along.
Returns:
~chainer.Variable: Output variable.
"""
return Concat(axis=axis)(*xs)
| chainer/functions/concat.py | 1,589 | Concatenate multiple tensors towards specified axis.
Concatenates given variables along an axis.
Args:
xs (tuple of Variables): Variables to be concatenated.
axis (int): Axis that the input arrays are concatenated along.
Returns:
~chainer.Variable: Output variable.
concat along the channel dimension by default | 327 | en | 0.743019 |
# coding=utf-8
# Copyright 2021 The Google Research Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Lint as: python3
"""Constructs a variable encoder based on an encoder spec."""
from typing import Dict, List, Optional
import numpy as np
import tensorflow as tf
from covid_epidemiology.src.models.encoders import gam_encoder
from covid_epidemiology.src.models.encoders import variable_encoders
from covid_epidemiology.src.models.shared import typedefs
def encoder_from_encoder_spec(
encoder_spec,
chosen_locations,
num_known_timesteps,
forecast_window_size,
output_window_size,
static_features = None,
static_overrides = None,
covariates = None,
forecasted_covariates = None,
covariate_overrides = None,
ts_categorical_features = None,
random_seed = 0,
static_scalers = None,
ts_scalers = None,
trainable = True,
):
"""Returns a `FeatureEncoder` built as specified in the `encoder_spec`."""
encoder_kwargs = encoder_spec.encoder_kwargs
if encoder_spec.encoder_type == "gam":
gam_kwargs = {}
for kwarg in encoder_kwargs:
if kwarg == "link_fn":
gam_kwargs["link_fn"] = encoder_kwargs["link_fn"]
elif kwarg == "distribution":
gam_kwargs["distribution"] = encoder_kwargs["distribution"]
elif kwarg == "initial_bias":
gam_kwargs["initial_bias"] = encoder_kwargs["initial_bias"]
elif kwarg == "location_dependent_bias":
gam_kwargs["location_dependent_bias"] = encoder_kwargs[
"location_dependent_bias"]
elif kwarg == "lower_bound":
gam_kwargs["lower_bound"] = encoder_kwargs["lower_bound"]
elif kwarg == "upper_bound":
gam_kwargs["upper_bound"] = encoder_kwargs["upper_bound"]
elif kwarg == "use_fixed_covariate_mask":
gam_kwargs["use_fixed_covariate_mask"] = encoder_kwargs[
"use_fixed_covariate_mask"]
else:
raise ValueError(f"Unexpected kwarg: {kwarg} passed to encoder of type "
f"{encoder_spec.encoder_type}")
return gam_encoder.GamEncoder(
chosen_locations,
num_known_timesteps,
forecast_window_size=forecast_window_size,
output_window_size=output_window_size,
static_features=static_features,
static_scalers=static_scalers,
static_overrides=static_overrides,
covariates=covariates,
ts_scalers=ts_scalers,
forecasted_covariates=forecasted_covariates,
covariate_overrides=covariate_overrides,
static_feature_specs=encoder_spec.static_feature_specs,
covariate_feature_specs=encoder_spec.covariate_feature_specs,
ts_categorical_features=ts_categorical_features,
covariate_feature_time_offset=encoder_spec
.covariate_feature_time_offset,
covariate_feature_window=encoder_spec.covariate_feature_window,
random_seed=random_seed,
name=encoder_spec.encoder_name,
trainable=trainable,
**gam_kwargs)
elif encoder_spec.encoder_type == "static":
return variable_encoders.StaticEncoder()
elif encoder_spec.encoder_type == "passthrough":
return variable_encoders.PassThroughEncoder(
chosen_locations,
num_known_timesteps,
forecast_window_size,
covariates=covariates,
forecasted_covariates=forecasted_covariates,
covariate_overrides=covariate_overrides,
covariate_feature_specs=encoder_spec.covariate_feature_specs,
ts_categorical_features=ts_categorical_features,
name=encoder_spec.encoder_name)
elif encoder_spec.encoder_type == "vaccine":
return variable_encoders.VaccineEncoder(
chosen_locations,
num_known_timesteps,
forecast_window_size,
covariates=covariates,
forecasted_covariates=forecasted_covariates,
covariate_overrides=covariate_overrides,
covariate_feature_specs=encoder_spec.covariate_feature_specs,
ts_categorical_features=ts_categorical_features,
name=encoder_spec.encoder_name,
vaccine_type=encoder_spec.vaccine_type)
else:
raise ValueError(f"encoder_spec passed in with invalid encoder_type: "
f"{encoder_spec.encoder_type}")
| covid_epidemiology/src/models/encoders/variable_encoder_builder.py | 4,752 | Returns a `FeatureEncoder` built as specified in the `encoder_spec`.
Constructs a variable encoder based on an encoder spec.
coding=utf-8 Copyright 2021 The Google Research Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Lint as: python3 | 722 | en | 0.843892 |
#! /usr/bin/env python
# -*- coding: utf8 -*-
'''
Copyright 2018 University of Liège
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.
'''
def test(res, tol):
import numpy as np
from cupydo.testing import *
# Read results from file
with open("AerodynamicCoeff.ascii", 'rb') as f:
lines = f.readlines()
resultA = np.genfromtxt(lines[-1:], delimiter=None)
# Check convergence and results
if (res > tol):
print "\n\n" + "FSI residual = " + str(res) + ", FSI tolerance = " + str(tol)
raise Exception(ccolors.ANSI_RED + "FSI algo failed to converge!" + ccolors.ANSI_RESET)
tests = CTests()
tests.add(CTest('Lift coefficient', resultA[2], 0.245, 1e-1, False)) # rel. tol. of 10%
tests.add(CTest('Drag coefficient', resultA[3], 0.0015, 1e-1, False)) # rel. tol. of 10%
tests.run()
def getFsiP():
"""Fsi parameters"""
import os
filePath = os.path.abspath(os.path.dirname(__file__))
p = {}
# Solvers and config files
p['fluidSolver'] = 'SU2'
p['solidSolver'] = 'RBMI'
p['cfdFile'] = os.path.join(filePath, 'PitchPlungeAirfoil_BGS_parallel_SU2Conf.cfg')
p['csdFile'] = os.path.join(filePath, 'PitchPlungeAirfoil_BGS_parallel_RBMConf.cfg')
# FSI objects
p['interpolator'] = 'Matching'
p['criterion'] = 'Displacements'
p['algorithm'] = 'StaticBGS'
# FSI parameters
p['compType'] = 'unsteady'
p['nDim'] = 2
p['dt'] = 0.001
p['tTot'] = 0.005
p['timeItTresh'] = 0
p['tol'] = 1e-8
p['maxIt'] = 6
p['omega'] = 1.0
p['nSteps'] = 0
p['firstItTgtMat'] = False
p['nodalLoadsType'] = 'force'
return p
def main():
import cupydo.interfaces.Cupydo as cupy
p = getFsiP() # get parameters
cupydo = cupy.CUPyDO(p) # create fsi driver
cupydo.run() # run fsi process
test(cupydo.algorithm.errValue, p['tol']) # check the results
# eof
print ''
# --- This is only accessed if running from command prompt --- #
if __name__ == '__main__':
main()
| tests/SU2_RBM/PitchPlungeAirfoil_BGS_parallel_fsi.py | 2,508 | ! /usr/bin/env python -*- coding: utf8 -*- Read results from file Check convergence and results rel. tol. of 10% rel. tol. of 10% Solvers and config files FSI objects FSI parameters get parameters create fsi driver run fsi process check the results eof --- This is only accessed if running from command prompt --- | 313 | en | 0.705293 |
import functools
import jinja2
import json
import threading
from hashlib import sha256
from ..caserunconfiguration import CaseRunConfiguration, ConfigurationsList, CaseRunConfigurationsList
#from ..exceptions import UnknownEventSubTypeExpression
from .functions import dotted_startswith
from .structures.factory import EventStructuresFactory
class Event():
"""
Base class of event which stores its type, event structures (automatically
provides converted event structures) and decides which testplans and
caserunconfigurations will be executed based on the event.
This base class can be directly used just by providing settings, event type
and optionally definitions of event_structures which are constructed using
EventStructuresFactory. Such created event uses default selection of
testplans with testcases and provides only the provided event structures
(along with possibly automatically converted event structures).
When defining new event type, one should create a new child class
inheriting this class providing additional methods and/or properties.
"""
def __init__(self, settings, event_type, **event_structures):
self.settings = settings
self.type = event_type
self.structures = {}
for structure_name, fields in event_structures.items():
self.structures[structure_name] = EventStructuresFactory.make(settings, structure_name, fields)
self.structures_convert_lock = threading.RLock()
self.id = sha256(f'{self.type}-{json.dumps(event_structures, sort_keys=True)}'.encode()).hexdigest()
def format_branch_spec(self, fmt):
return jinja2.Template(fmt).render(event=self)
def generate_caseRunConfigurations(self, library):
""" Generates caseRunConfigurations for testcases in library relevant to this event
:param library: Library
:type library: tplib.Library
:return: CaseRunConfigurations
:rtype: CaseRunConfigurationsList
"""
caseruns = CaseRunConfigurationsList()
for testplan in self.filter_testPlans(library):
# Init testplan configurations as ConfigurationsList
testplan_configurations = ConfigurationsList(testplan.configurations,
merge_method=self.settings.get('library', 'defaultCaseConfigMergeMethod'))
for testcase in testplan.verificationTestCases:
# Merge testplan configurations with testcase configurations
caserun_configurations = testplan_configurations.merge(testcase.configurations)
for configuration in caserun_configurations:
# Create CaseRunConfiguration
caseruns.append(CaseRunConfiguration(testcase, configuration, [testplan]))
return caseruns
def handles_testplan_artifact_type(self, artifact_type):
"""
Decide if this event is relevant to the provided artifact_type (which
is found in test plan).
"""
return dotted_startswith(self.type, artifact_type)
def filter_testPlans(self, library):
""" Filters testplan from library based on:
- event type and testplan.artifact_type
- testplan execute_on filter
:param library: pipeline library
:type library: tplib.Library
:return: Filtered testplans
:rtype: list of tplib.TestPlan
"""
return library.getTestPlansByQuery('event.handles_testplan_artifact_type(tp.artifact_type) and tp.eval_execute_on(event=event)', event=self)
@property
def additional_testplans_data(self):
""" Event can provide additional testplans. Returns python
dicts, as if they were tplib files read by yaml.safe_load.
:return: list of testplan data
:rtype: tuple
"""
return None
@property
def additional_requrements_data(self):
""" Event can provide additional requrements. Returns python
dicts, as if they were tplib files read by yaml.safe_load.
:return: list of requrements data
:rtype: tuple
"""
return None
@property
def additional_testcases_data(self):
""" Event can provide additional testcases. Returns python
dicts, as if they were tplib files read by yaml.safe_load.
:return: list of testcases data
:rtype: tuple
"""
return None
def __getattr__(self, attrname):
if attrname not in EventStructuresFactory.known():
return super().__getattribute__(attrname)
with self.structures_convert_lock:
try:
return self.structures[attrname]
except KeyError:
pass
structure = EventStructuresFactory.convert(attrname, self.structures)
if structure is NotImplemented:
# Return None if the requested structure is not compatible to
# allow jinja templates to not crash on expressions like
# event.nonexisting_structure.foo but to consider them as None
return None
self.structures[attrname] = structure
return structure
def payload_override(payload_name):
def decorator(method):
@functools.wraps(method)
def decorated(self, *args, **kwargs):
try:
return self.payload[payload_name]
except KeyError:
return method(self, *args, **kwargs)
return decorated
return decorator
| libpermian/events/base.py | 5,583 | Base class of event which stores its type, event structures (automatically
provides converted event structures) and decides which testplans and
caserunconfigurations will be executed based on the event.
This base class can be directly used just by providing settings, event type
and optionally definitions of event_structures which are constructed using
EventStructuresFactory. Such created event uses default selection of
testplans with testcases and provides only the provided event structures
(along with possibly automatically converted event structures).
When defining new event type, one should create a new child class
inheriting this class providing additional methods and/or properties.
Event can provide additional requrements. Returns python
dicts, as if they were tplib files read by yaml.safe_load.
:return: list of requrements data
:rtype: tuple
Event can provide additional testcases. Returns python
dicts, as if they were tplib files read by yaml.safe_load.
:return: list of testcases data
:rtype: tuple
Event can provide additional testplans. Returns python
dicts, as if they were tplib files read by yaml.safe_load.
:return: list of testplan data
:rtype: tuple
Filters testplan from library based on:
- event type and testplan.artifact_type
- testplan execute_on filter
:param library: pipeline library
:type library: tplib.Library
:return: Filtered testplans
:rtype: list of tplib.TestPlan
Generates caseRunConfigurations for testcases in library relevant to this event
:param library: Library
:type library: tplib.Library
:return: CaseRunConfigurations
:rtype: CaseRunConfigurationsList
Decide if this event is relevant to the provided artifact_type (which
is found in test plan).
from ..exceptions import UnknownEventSubTypeExpression Init testplan configurations as ConfigurationsList Merge testplan configurations with testcase configurations Create CaseRunConfiguration Return None if the requested structure is not compatible to allow jinja templates to not crash on expressions like event.nonexisting_structure.foo but to consider them as None | 2,077 | en | 0.85771 |
# coding: utf-8
"""
FlashArray REST API
No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
OpenAPI spec version: 2.1
Generated by: https://github.com/swagger-api/swagger-codegen.git
"""
import pprint
import re
import six
import typing
from ....properties import Property
if typing.TYPE_CHECKING:
from pypureclient.flasharray.FA_2_1 import models
class RetentionPolicy(object):
"""
Attributes:
swagger_types (dict): The key is attribute name
and the value is attribute type.
attribute_map (dict): The key is attribute name
and the value is json key in definition.
"""
swagger_types = {
'all_for_sec': 'int',
'days': 'int',
'per_day': 'int'
}
attribute_map = {
'all_for_sec': 'all_for_sec',
'days': 'days',
'per_day': 'per_day'
}
required_args = {
}
def __init__(
self,
all_for_sec=None, # type: int
days=None, # type: int
per_day=None, # type: int
):
"""
Keyword args:
all_for_sec (int): The length of time to keep the specified snapshots. Measured in seconds.
days (int): The number of days to keep the snapshots after the `all_for_sec` period has passed.
per_day (int): The number of snapshots to keep per day after the `all_for_sec` period has passed.
"""
if all_for_sec is not None:
self.all_for_sec = all_for_sec
if days is not None:
self.days = days
if per_day is not None:
self.per_day = per_day
def __setattr__(self, key, value):
if key not in self.attribute_map:
raise KeyError("Invalid key `{}` for `RetentionPolicy`".format(key))
self.__dict__[key] = value
def __getattribute__(self, item):
value = object.__getattribute__(self, item)
if isinstance(value, Property):
raise AttributeError
else:
return value
def to_dict(self):
"""Returns the model properties as a dict"""
result = {}
for attr, _ in six.iteritems(self.swagger_types):
if hasattr(self, attr):
value = getattr(self, attr)
if isinstance(value, list):
result[attr] = list(map(
lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
value
))
elif hasattr(value, "to_dict"):
result[attr] = value.to_dict()
elif isinstance(value, dict):
result[attr] = dict(map(
lambda item: (item[0], item[1].to_dict())
if hasattr(item[1], "to_dict") else item,
value.items()
))
else:
result[attr] = value
if issubclass(RetentionPolicy, dict):
for key, value in self.items():
result[key] = value
return result
def to_str(self):
"""Returns the string representation of the model"""
return pprint.pformat(self.to_dict())
def __repr__(self):
"""For `print` and `pprint`"""
return self.to_str()
def __eq__(self, other):
"""Returns true if both objects are equal"""
if not isinstance(other, RetentionPolicy):
return False
return self.__dict__ == other.__dict__
def __ne__(self, other):
"""Returns true if both objects are not equal"""
return not self == other
| pypureclient/flasharray/FA_2_1/models/retention_policy.py | 3,707 | Attributes:
swagger_types (dict): The key is attribute name
and the value is attribute type.
attribute_map (dict): The key is attribute name
and the value is json key in definition.
Returns true if both objects are equal
Keyword args:
all_for_sec (int): The length of time to keep the specified snapshots. Measured in seconds.
days (int): The number of days to keep the snapshots after the `all_for_sec` period has passed.
per_day (int): The number of snapshots to keep per day after the `all_for_sec` period has passed.
Returns true if both objects are not equal
For `print` and `pprint`
Returns the model properties as a dict
Returns the string representation of the model
FlashArray REST API
No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
OpenAPI spec version: 2.1
Generated by: https://github.com/swagger-api/swagger-codegen.git
coding: utf-8 type: int type: int type: int | 1,000 | en | 0.783965 |
# coding=utf-8
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# Code generated by Microsoft (R) AutoRest Code Generator.
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------
from typing import Any, AsyncIterable, Callable, Dict, Generic, Optional, TypeVar, Union
import warnings
from azure.core.async_paging import AsyncItemPaged, AsyncList
from azure.core.exceptions import ClientAuthenticationError, HttpResponseError, ResourceExistsError, ResourceNotFoundError, map_error
from azure.core.pipeline import PipelineResponse
from azure.core.pipeline.transport import AsyncHttpResponse, HttpRequest
from azure.mgmt.core.exceptions import ARMErrorFormat
from ... import models
T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any]]
class TasksOperations:
"""TasksOperations async operations.
You should not instantiate this class directly. Instead, you should create a Client instance that
instantiates it for you and attaches it as an attribute.
:ivar models: Alias to model classes used in this operation group.
:type models: ~azure.mgmt.datamigration.models
:param client: Client for service requests.
:param config: Configuration of service client.
:param serializer: An object model serializer.
:param deserializer: An object model deserializer.
"""
models = models
def __init__(self, client, config, serializer, deserializer) -> None:
self._client = client
self._serialize = serializer
self._deserialize = deserializer
self._config = config
def list(
self,
group_name: str,
service_name: str,
project_name: str,
task_type: Optional[str] = None,
**kwargs
) -> AsyncIterable["models.TaskList"]:
"""Get tasks in a service.
The services resource is the top-level resource that represents the Database Migration Service.
This method returns a list of tasks owned by a service resource. Some tasks may have a status
of Unknown, which indicates that an error occurred while querying the status of that task.
:param group_name: Name of the resource group.
:type group_name: str
:param service_name: Name of the service.
:type service_name: str
:param project_name: Name of the project.
:type project_name: str
:param task_type: Filter tasks by task type.
:type task_type: str
:keyword callable cls: A custom type or function that will be passed the direct response
:return: An iterator like instance of either TaskList or the result of cls(response)
:rtype: ~azure.core.async_paging.AsyncItemPaged[~azure.mgmt.datamigration.models.TaskList]
:raises: ~azure.core.exceptions.HttpResponseError
"""
cls = kwargs.pop('cls', None) # type: ClsType["models.TaskList"]
error_map = {
401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError
}
error_map.update(kwargs.pop('error_map', {}))
api_version = "2021-10-30-preview"
accept = "application/json"
def prepare_request(next_link=None):
# Construct headers
header_parameters = {} # type: Dict[str, Any]
header_parameters['Accept'] = self._serialize.header("accept", accept, 'str')
if not next_link:
# Construct URL
url = self.list.metadata['url'] # type: ignore
path_format_arguments = {
'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'),
'groupName': self._serialize.url("group_name", group_name, 'str'),
'serviceName': self._serialize.url("service_name", service_name, 'str'),
'projectName': self._serialize.url("project_name", project_name, 'str'),
}
url = self._client.format_url(url, **path_format_arguments)
# Construct parameters
query_parameters = {} # type: Dict[str, Any]
query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str')
if task_type is not None:
query_parameters['taskType'] = self._serialize.query("task_type", task_type, 'str')
request = self._client.get(url, query_parameters, header_parameters)
else:
url = next_link
query_parameters = {} # type: Dict[str, Any]
request = self._client.get(url, query_parameters, header_parameters)
return request
async def extract_data(pipeline_response):
deserialized = self._deserialize('TaskList', pipeline_response)
list_of_elem = deserialized.value
if cls:
list_of_elem = cls(list_of_elem)
return deserialized.next_link or None, AsyncList(list_of_elem)
async def get_next(next_link=None):
request = prepare_request(next_link)
pipeline_response = await self._client._pipeline.run(request, stream=False, **kwargs)
response = pipeline_response.http_response
if response.status_code not in [200]:
error = self._deserialize(models.ApiError, response)
map_error(status_code=response.status_code, response=response, error_map=error_map)
raise HttpResponseError(response=response, model=error, error_format=ARMErrorFormat)
return pipeline_response
return AsyncItemPaged(
get_next, extract_data
)
list.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{groupName}/providers/Microsoft.DataMigration/services/{serviceName}/projects/{projectName}/tasks'} # type: ignore
async def create_or_update(
self,
group_name: str,
service_name: str,
project_name: str,
task_name: str,
parameters: "models.ProjectTask",
**kwargs
) -> "models.ProjectTask":
"""Create or update task.
The tasks resource is a nested, proxy-only resource representing work performed by a DMS
instance. The PUT method creates a new task or updates an existing one, although since tasks
have no mutable custom properties, there is little reason to update an existing one.
:param group_name: Name of the resource group.
:type group_name: str
:param service_name: Name of the service.
:type service_name: str
:param project_name: Name of the project.
:type project_name: str
:param task_name: Name of the Task.
:type task_name: str
:param parameters: Information about the task.
:type parameters: ~azure.mgmt.datamigration.models.ProjectTask
:keyword callable cls: A custom type or function that will be passed the direct response
:return: ProjectTask, or the result of cls(response)
:rtype: ~azure.mgmt.datamigration.models.ProjectTask
:raises: ~azure.core.exceptions.HttpResponseError
"""
cls = kwargs.pop('cls', None) # type: ClsType["models.ProjectTask"]
error_map = {
401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError
}
error_map.update(kwargs.pop('error_map', {}))
api_version = "2021-10-30-preview"
content_type = kwargs.pop("content_type", "application/json")
accept = "application/json"
# Construct URL
url = self.create_or_update.metadata['url'] # type: ignore
path_format_arguments = {
'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'),
'groupName': self._serialize.url("group_name", group_name, 'str'),
'serviceName': self._serialize.url("service_name", service_name, 'str'),
'projectName': self._serialize.url("project_name", project_name, 'str'),
'taskName': self._serialize.url("task_name", task_name, 'str'),
}
url = self._client.format_url(url, **path_format_arguments)
# Construct parameters
query_parameters = {} # type: Dict[str, Any]
query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str')
# Construct headers
header_parameters = {} # type: Dict[str, Any]
header_parameters['Content-Type'] = self._serialize.header("content_type", content_type, 'str')
header_parameters['Accept'] = self._serialize.header("accept", accept, 'str')
body_content_kwargs = {} # type: Dict[str, Any]
body_content = self._serialize.body(parameters, 'ProjectTask')
body_content_kwargs['content'] = body_content
request = self._client.put(url, query_parameters, header_parameters, **body_content_kwargs)
pipeline_response = await self._client._pipeline.run(request, stream=False, **kwargs)
response = pipeline_response.http_response
if response.status_code not in [200, 201]:
map_error(status_code=response.status_code, response=response, error_map=error_map)
error = self._deserialize(models.ApiError, response)
raise HttpResponseError(response=response, model=error, error_format=ARMErrorFormat)
if response.status_code == 200:
deserialized = self._deserialize('ProjectTask', pipeline_response)
if response.status_code == 201:
deserialized = self._deserialize('ProjectTask', pipeline_response)
if cls:
return cls(pipeline_response, deserialized, {})
return deserialized
create_or_update.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{groupName}/providers/Microsoft.DataMigration/services/{serviceName}/projects/{projectName}/tasks/{taskName}'} # type: ignore
async def get(
self,
group_name: str,
service_name: str,
project_name: str,
task_name: str,
expand: Optional[str] = None,
**kwargs
) -> "models.ProjectTask":
"""Get task information.
The tasks resource is a nested, proxy-only resource representing work performed by a DMS
instance. The GET method retrieves information about a task.
:param group_name: Name of the resource group.
:type group_name: str
:param service_name: Name of the service.
:type service_name: str
:param project_name: Name of the project.
:type project_name: str
:param task_name: Name of the Task.
:type task_name: str
:param expand: Expand the response.
:type expand: str
:keyword callable cls: A custom type or function that will be passed the direct response
:return: ProjectTask, or the result of cls(response)
:rtype: ~azure.mgmt.datamigration.models.ProjectTask
:raises: ~azure.core.exceptions.HttpResponseError
"""
cls = kwargs.pop('cls', None) # type: ClsType["models.ProjectTask"]
error_map = {
401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError
}
error_map.update(kwargs.pop('error_map', {}))
api_version = "2021-10-30-preview"
accept = "application/json"
# Construct URL
url = self.get.metadata['url'] # type: ignore
path_format_arguments = {
'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'),
'groupName': self._serialize.url("group_name", group_name, 'str'),
'serviceName': self._serialize.url("service_name", service_name, 'str'),
'projectName': self._serialize.url("project_name", project_name, 'str'),
'taskName': self._serialize.url("task_name", task_name, 'str'),
}
url = self._client.format_url(url, **path_format_arguments)
# Construct parameters
query_parameters = {} # type: Dict[str, Any]
query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str')
if expand is not None:
query_parameters['$expand'] = self._serialize.query("expand", expand, 'str')
# Construct headers
header_parameters = {} # type: Dict[str, Any]
header_parameters['Accept'] = self._serialize.header("accept", accept, 'str')
request = self._client.get(url, query_parameters, header_parameters)
pipeline_response = await self._client._pipeline.run(request, stream=False, **kwargs)
response = pipeline_response.http_response
if response.status_code not in [200]:
map_error(status_code=response.status_code, response=response, error_map=error_map)
error = self._deserialize(models.ApiError, response)
raise HttpResponseError(response=response, model=error, error_format=ARMErrorFormat)
deserialized = self._deserialize('ProjectTask', pipeline_response)
if cls:
return cls(pipeline_response, deserialized, {})
return deserialized
get.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{groupName}/providers/Microsoft.DataMigration/services/{serviceName}/projects/{projectName}/tasks/{taskName}'} # type: ignore
async def delete(
self,
group_name: str,
service_name: str,
project_name: str,
task_name: str,
delete_running_tasks: Optional[bool] = None,
**kwargs
) -> None:
"""Delete task.
The tasks resource is a nested, proxy-only resource representing work performed by a DMS
instance. The DELETE method deletes a task, canceling it first if it's running.
:param group_name: Name of the resource group.
:type group_name: str
:param service_name: Name of the service.
:type service_name: str
:param project_name: Name of the project.
:type project_name: str
:param task_name: Name of the Task.
:type task_name: str
:param delete_running_tasks: Delete the resource even if it contains running tasks.
:type delete_running_tasks: bool
:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
:rtype: None
:raises: ~azure.core.exceptions.HttpResponseError
"""
cls = kwargs.pop('cls', None) # type: ClsType[None]
error_map = {
401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError
}
error_map.update(kwargs.pop('error_map', {}))
api_version = "2021-10-30-preview"
accept = "application/json"
# Construct URL
url = self.delete.metadata['url'] # type: ignore
path_format_arguments = {
'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'),
'groupName': self._serialize.url("group_name", group_name, 'str'),
'serviceName': self._serialize.url("service_name", service_name, 'str'),
'projectName': self._serialize.url("project_name", project_name, 'str'),
'taskName': self._serialize.url("task_name", task_name, 'str'),
}
url = self._client.format_url(url, **path_format_arguments)
# Construct parameters
query_parameters = {} # type: Dict[str, Any]
query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str')
if delete_running_tasks is not None:
query_parameters['deleteRunningTasks'] = self._serialize.query("delete_running_tasks", delete_running_tasks, 'bool')
# Construct headers
header_parameters = {} # type: Dict[str, Any]
header_parameters['Accept'] = self._serialize.header("accept", accept, 'str')
request = self._client.delete(url, query_parameters, header_parameters)
pipeline_response = await self._client._pipeline.run(request, stream=False, **kwargs)
response = pipeline_response.http_response
if response.status_code not in [200, 204]:
map_error(status_code=response.status_code, response=response, error_map=error_map)
error = self._deserialize(models.ApiError, response)
raise HttpResponseError(response=response, model=error, error_format=ARMErrorFormat)
if cls:
return cls(pipeline_response, None, {})
delete.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{groupName}/providers/Microsoft.DataMigration/services/{serviceName}/projects/{projectName}/tasks/{taskName}'} # type: ignore
async def update(
self,
group_name: str,
service_name: str,
project_name: str,
task_name: str,
parameters: "models.ProjectTask",
**kwargs
) -> "models.ProjectTask":
"""Create or update task.
The tasks resource is a nested, proxy-only resource representing work performed by a DMS
instance. The PATCH method updates an existing task, but since tasks have no mutable custom
properties, there is little reason to do so.
:param group_name: Name of the resource group.
:type group_name: str
:param service_name: Name of the service.
:type service_name: str
:param project_name: Name of the project.
:type project_name: str
:param task_name: Name of the Task.
:type task_name: str
:param parameters: Information about the task.
:type parameters: ~azure.mgmt.datamigration.models.ProjectTask
:keyword callable cls: A custom type or function that will be passed the direct response
:return: ProjectTask, or the result of cls(response)
:rtype: ~azure.mgmt.datamigration.models.ProjectTask
:raises: ~azure.core.exceptions.HttpResponseError
"""
cls = kwargs.pop('cls', None) # type: ClsType["models.ProjectTask"]
error_map = {
401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError
}
error_map.update(kwargs.pop('error_map', {}))
api_version = "2021-10-30-preview"
content_type = kwargs.pop("content_type", "application/json")
accept = "application/json"
# Construct URL
url = self.update.metadata['url'] # type: ignore
path_format_arguments = {
'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'),
'groupName': self._serialize.url("group_name", group_name, 'str'),
'serviceName': self._serialize.url("service_name", service_name, 'str'),
'projectName': self._serialize.url("project_name", project_name, 'str'),
'taskName': self._serialize.url("task_name", task_name, 'str'),
}
url = self._client.format_url(url, **path_format_arguments)
# Construct parameters
query_parameters = {} # type: Dict[str, Any]
query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str')
# Construct headers
header_parameters = {} # type: Dict[str, Any]
header_parameters['Content-Type'] = self._serialize.header("content_type", content_type, 'str')
header_parameters['Accept'] = self._serialize.header("accept", accept, 'str')
body_content_kwargs = {} # type: Dict[str, Any]
body_content = self._serialize.body(parameters, 'ProjectTask')
body_content_kwargs['content'] = body_content
request = self._client.patch(url, query_parameters, header_parameters, **body_content_kwargs)
pipeline_response = await self._client._pipeline.run(request, stream=False, **kwargs)
response = pipeline_response.http_response
if response.status_code not in [200]:
map_error(status_code=response.status_code, response=response, error_map=error_map)
error = self._deserialize(models.ApiError, response)
raise HttpResponseError(response=response, model=error, error_format=ARMErrorFormat)
deserialized = self._deserialize('ProjectTask', pipeline_response)
if cls:
return cls(pipeline_response, deserialized, {})
return deserialized
update.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{groupName}/providers/Microsoft.DataMigration/services/{serviceName}/projects/{projectName}/tasks/{taskName}'} # type: ignore
async def cancel(
self,
group_name: str,
service_name: str,
project_name: str,
task_name: str,
**kwargs
) -> "models.ProjectTask":
"""Cancel a task.
The tasks resource is a nested, proxy-only resource representing work performed by a DMS
instance. This method cancels a task if it's currently queued or running.
:param group_name: Name of the resource group.
:type group_name: str
:param service_name: Name of the service.
:type service_name: str
:param project_name: Name of the project.
:type project_name: str
:param task_name: Name of the Task.
:type task_name: str
:keyword callable cls: A custom type or function that will be passed the direct response
:return: ProjectTask, or the result of cls(response)
:rtype: ~azure.mgmt.datamigration.models.ProjectTask
:raises: ~azure.core.exceptions.HttpResponseError
"""
cls = kwargs.pop('cls', None) # type: ClsType["models.ProjectTask"]
error_map = {
401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError
}
error_map.update(kwargs.pop('error_map', {}))
api_version = "2021-10-30-preview"
accept = "application/json"
# Construct URL
url = self.cancel.metadata['url'] # type: ignore
path_format_arguments = {
'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'),
'groupName': self._serialize.url("group_name", group_name, 'str'),
'serviceName': self._serialize.url("service_name", service_name, 'str'),
'projectName': self._serialize.url("project_name", project_name, 'str'),
'taskName': self._serialize.url("task_name", task_name, 'str'),
}
url = self._client.format_url(url, **path_format_arguments)
# Construct parameters
query_parameters = {} # type: Dict[str, Any]
query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str')
# Construct headers
header_parameters = {} # type: Dict[str, Any]
header_parameters['Accept'] = self._serialize.header("accept", accept, 'str')
request = self._client.post(url, query_parameters, header_parameters)
pipeline_response = await self._client._pipeline.run(request, stream=False, **kwargs)
response = pipeline_response.http_response
if response.status_code not in [200]:
map_error(status_code=response.status_code, response=response, error_map=error_map)
error = self._deserialize(models.ApiError, response)
raise HttpResponseError(response=response, model=error, error_format=ARMErrorFormat)
deserialized = self._deserialize('ProjectTask', pipeline_response)
if cls:
return cls(pipeline_response, deserialized, {})
return deserialized
cancel.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{groupName}/providers/Microsoft.DataMigration/services/{serviceName}/projects/{projectName}/tasks/{taskName}/cancel'} # type: ignore
async def command(
self,
group_name: str,
service_name: str,
project_name: str,
task_name: str,
parameters: "models.CommandProperties",
**kwargs
) -> "models.CommandProperties":
"""Execute a command on a task.
The tasks resource is a nested, proxy-only resource representing work performed by a DMS
instance. This method executes a command on a running task.
:param group_name: Name of the resource group.
:type group_name: str
:param service_name: Name of the service.
:type service_name: str
:param project_name: Name of the project.
:type project_name: str
:param task_name: Name of the Task.
:type task_name: str
:param parameters: Command to execute.
:type parameters: ~azure.mgmt.datamigration.models.CommandProperties
:keyword callable cls: A custom type or function that will be passed the direct response
:return: CommandProperties, or the result of cls(response)
:rtype: ~azure.mgmt.datamigration.models.CommandProperties
:raises: ~azure.core.exceptions.HttpResponseError
"""
cls = kwargs.pop('cls', None) # type: ClsType["models.CommandProperties"]
error_map = {
401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError
}
error_map.update(kwargs.pop('error_map', {}))
api_version = "2021-10-30-preview"
content_type = kwargs.pop("content_type", "application/json")
accept = "application/json"
# Construct URL
url = self.command.metadata['url'] # type: ignore
path_format_arguments = {
'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'),
'groupName': self._serialize.url("group_name", group_name, 'str'),
'serviceName': self._serialize.url("service_name", service_name, 'str'),
'projectName': self._serialize.url("project_name", project_name, 'str'),
'taskName': self._serialize.url("task_name", task_name, 'str'),
}
url = self._client.format_url(url, **path_format_arguments)
# Construct parameters
query_parameters = {} # type: Dict[str, Any]
query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str')
# Construct headers
header_parameters = {} # type: Dict[str, Any]
header_parameters['Content-Type'] = self._serialize.header("content_type", content_type, 'str')
header_parameters['Accept'] = self._serialize.header("accept", accept, 'str')
body_content_kwargs = {} # type: Dict[str, Any]
body_content = self._serialize.body(parameters, 'CommandProperties')
body_content_kwargs['content'] = body_content
request = self._client.post(url, query_parameters, header_parameters, **body_content_kwargs)
pipeline_response = await self._client._pipeline.run(request, stream=False, **kwargs)
response = pipeline_response.http_response
if response.status_code not in [200]:
map_error(status_code=response.status_code, response=response, error_map=error_map)
error = self._deserialize(models.ApiError, response)
raise HttpResponseError(response=response, model=error, error_format=ARMErrorFormat)
deserialized = self._deserialize('CommandProperties', pipeline_response)
if cls:
return cls(pipeline_response, deserialized, {})
return deserialized
command.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{groupName}/providers/Microsoft.DataMigration/services/{serviceName}/projects/{projectName}/tasks/{taskName}/command'} # type: ignore
| src/datamigration/azext_datamigration/vendored_sdks/datamigration/aio/operations/_tasks_operations.py | 28,243 | TasksOperations async operations.
You should not instantiate this class directly. Instead, you should create a Client instance that
instantiates it for you and attaches it as an attribute.
:ivar models: Alias to model classes used in this operation group.
:type models: ~azure.mgmt.datamigration.models
:param client: Client for service requests.
:param config: Configuration of service client.
:param serializer: An object model serializer.
:param deserializer: An object model deserializer.
Get tasks in a service.
The services resource is the top-level resource that represents the Database Migration Service.
This method returns a list of tasks owned by a service resource. Some tasks may have a status
of Unknown, which indicates that an error occurred while querying the status of that task.
:param group_name: Name of the resource group.
:type group_name: str
:param service_name: Name of the service.
:type service_name: str
:param project_name: Name of the project.
:type project_name: str
:param task_type: Filter tasks by task type.
:type task_type: str
:keyword callable cls: A custom type or function that will be passed the direct response
:return: An iterator like instance of either TaskList or the result of cls(response)
:rtype: ~azure.core.async_paging.AsyncItemPaged[~azure.mgmt.datamigration.models.TaskList]
:raises: ~azure.core.exceptions.HttpResponseError
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. -------------------------------------------------------------------------- type: ClsType["models.TaskList"] Construct headers type: Dict[str, Any] Construct URL type: ignore Construct parameters type: Dict[str, Any] type: Dict[str, Any] type: ignore type: ClsType["models.ProjectTask"] Construct URL type: ignore Construct parameters type: Dict[str, Any] Construct headers type: Dict[str, Any] type: Dict[str, Any] type: ignore type: ClsType["models.ProjectTask"] Construct URL type: ignore Construct parameters type: Dict[str, Any] Construct headers type: Dict[str, Any] type: ignore type: ClsType[None] Construct URL type: ignore Construct parameters type: Dict[str, Any] Construct headers type: Dict[str, Any] type: ignore type: ClsType["models.ProjectTask"] Construct URL type: ignore Construct parameters type: Dict[str, Any] Construct headers type: Dict[str, Any] type: Dict[str, Any] type: ignore type: ClsType["models.ProjectTask"] Construct URL type: ignore Construct parameters type: Dict[str, Any] Construct headers type: Dict[str, Any] type: ignore type: ClsType["models.CommandProperties"] Construct URL type: ignore Construct parameters type: Dict[str, Any] Construct headers type: Dict[str, Any] type: Dict[str, Any] type: ignore | 3,008 | en | 0.55762 |
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 29 16:02:11 2019
@author: Christian Zwinkels-Valero
"""
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
# Activation functions
def sigmoid(z):
return 1 / (1 + np.exp(z))
def d_sigmoid(z):
return sigmoid(z)*(1 - sigmoid(z))
def relu(z, d=False):
if d == False:
f = np.maximum(0.001*z, z)
else:
f = np.where(z >= 0, 1, 0.001)
return f
# Data processing
data = pd.read_csv("IRISS.csv", header=None, skiprows=1)
data = data.sample(frac=1)
X = data[data.columns[0:data.shape[-1] - 1]].to_numpy()
X = (X - np.mean(X, axis=0)) / (np.max(X, axis=0) - np.min(X, axis=0))
X = X.T
y = np.array([data[data.shape[-1] - 1].to_numpy()])
# Initialization
layer_sizes = (X.shape[0], 12, 8, 4, y.shape[0])
weight_sizes = [(y, x) for y, x in zip(layer_sizes[1:], layer_sizes[0:])]
weights = [np.random.rand(l[0], l[1])*np.sqrt(1/l[1]) for l in weight_sizes]
biases = [np.zeros((i, 1)) for i in layer_sizes[1:]]
# Foward propagation
def feedforward(data_in, Ws, Bs):
Z = []
A = [data_in] # First activation layer is the inputs
# Hidden layer computation
for i in range(len(Ws) - 1):
z = np.dot(Ws[i], A[-1]) + Bs[i]
a = relu(z, d=False)
Z.append(z)
A.append(a)
# Ouput layer computation
z = np.dot(Ws[-1], A[-1]) + Bs[-1]
Z.append(z)
a = sigmoid(z)
A.append(a)
return Z, A
# Calculating the costs
def costs(data_in, outputs, Ws, Bs):
Z, pred = feedforward(data_in, Ws, Bs)
delta = []
dj_dw = []
# Loss computation
loss = -1*(outputs*np.log(pred[-1]) + (1-outputs)*np.log(1 - pred[-1]))
loss = np.mean(loss)
# Final layer derivatives
dj_da = -1*((outputs[-1]/pred[-1]) + (1 - outputs)/(1 - pred[-1]))
da_dz = d_sigmoid(Z[-1])
delta.append(dj_da*da_dz)
# Deltas calculation
for i in range(1, len(Ws)):
d = np.dot(Ws[-i].T, delta[-i]) * relu(Z[-i - 1], d=True)
delta.insert(0, np.mean(d, axis=1, keepdims=True))
delta[-1] = np.mean(delta[-1])
# dj_dw calculation
for i in range(1, len(pred)):
b = []
A = pred[-i - 1].T
for a in A:
b.append(np.dot(delta[-i], [a]))
d = np.zeros(weight_sizes[-i])
for s in b:
d += s
d /= len(d)
dj_dw.insert(0, d)
return loss, delta, dj_dw
def train(data_in, outputs, Ws, Bs, iters, alpha):
c_his = []
i_his = []
for i in range(iters):
c, Bu, Wu = costs(data_in, outputs, Ws, Bs)
for j in range(len(Ws)):
Bs[j] = np.add(Bs[j], np.multiply(-alpha, Bu[j]))
Ws[j] = np.add(Ws[j], np.multiply(-alpha, Wu[j]))
c_his.append(c)
i_his.append(i)
plt.plot(i_his, c_his)
plt.show()
| Supervised_Learning/Neural_Network/NN.py | 2,817 | Created on Sun Dec 29 16:02:11 2019
@author: Christian Zwinkels-Valero
-*- coding: utf-8 -*- Activation functions Data processing Initialization Foward propagation First activation layer is the inputs Hidden layer computation Ouput layer computation Calculating the costs Loss computation Final layer derivatives Deltas calculation dj_dw calculation | 352 | en | 0.545429 |
"""Get example scripts, notebooks, and data files."""
import argparse
from datetime import datetime, timedelta
from glob import glob
import json
import os
import pkg_resources
from progressbar import ProgressBar
try:
# For Python 3.0 and later
from urllib.request import urlopen
except ImportError:
# Fall back to Python 2's urllib2
from urllib2 import urlopen
import shutil
import sys
example_data_files = (
["MovingEddies_data/" + fn for fn in [
"moving_eddiesP.nc", "moving_eddiesU.nc", "moving_eddiesV.nc"]]
+ ["OFAM_example_data/" + fn for fn in [
"OFAM_simple_U.nc", "OFAM_simple_V.nc"]]
+ ["Peninsula_data/" + fn for fn in [
"peninsulaU.nc", "peninsulaV.nc", "peninsulaP.nc"]]
+ ["GlobCurrent_example_data/" + fn for fn in [
"%s000000-GLOBCURRENT-L4-CUReul_hs-ALT_SUM-v02.0-fv01.0.nc" % (
date.strftime("%Y%m%d"))
for date in ([datetime(2002, 1, 1) + timedelta(days=x)
for x in range(0, 365)] + [datetime(2003, 1, 1)])]]
+ ["DecayingMovingEddy_data/" + fn for fn in [
"decaying_moving_eddyU.nc", "decaying_moving_eddyV.nc"]]
+ ["NemoCurvilinear_data/" + fn for fn in [
"U_purely_zonal-ORCA025_grid_U.nc4", "V_purely_zonal-ORCA025_grid_V.nc4",
"mesh_mask.nc4"]]
+ ["NemoNorthSeaORCA025-N006_data/" + fn for fn in [
"ORCA025-N06_20000104d05U.nc", "ORCA025-N06_20000109d05U.nc",
"ORCA025-N06_20000104d05V.nc", "ORCA025-N06_20000109d05V.nc",
"ORCA025-N06_20000104d05W.nc", "ORCA025-N06_20000109d05W.nc",
"coordinates.nc"]])
example_data_url = "http://oceanparcels.org/examples-data"
def _maybe_create_dir(path):
"""Create directory (and parents) if they don't exist."""
try:
os.makedirs(path)
except OSError:
if not os.path.isdir(path):
raise
def copy_data_and_examples_from_package_to(target_path):
"""Copy example data from Parcels directory.
Return thos parths of the list `file_names` that were not found in the
package.
"""
examples_in_package = pkg_resources.resource_filename("parcels", "examples")
try:
shutil.copytree(examples_in_package, target_path)
except Exception as e:
print(e)
pass
def set_jupyter_kernel_to_python_version(path, python_version=2):
"""Set notebook kernelspec to desired python version.
This also drops all other meta data from the notebook.
"""
for file_name in glob(os.path.join(path, "*.ipynb")):
with open(file_name, 'r') as f:
notebook_data = json.load(f)
notebook_data['metadata'] = {"kernelspec": {
"display_name": "Python {}".format(python_version),
"language": "python",
"name": "python{}".format(python_version)}}
with open(file_name, 'w') as f:
json.dump(notebook_data, f, indent=2)
def _still_to_download(file_names, target_path):
"""Only return the files that are not yet present on disk."""
for fn in list(file_names):
if os.path.exists(os.path.join(target_path, fn)):
file_names.remove(fn)
return file_names
def download_files(source_url, file_names, target_path):
"""Mirror file_names from source_url to target_path."""
_maybe_create_dir(target_path)
pbar = ProgressBar()
print("Downloading %s ..." % (source_url.split("/")[-1]))
for filename in pbar(file_names):
_maybe_create_dir(os.path.join(target_path, os.path.dirname(filename)))
if not os.path.exists(os.path.join(target_path, filename)):
download_url = source_url + "/" + filename
src = urlopen(download_url)
with open(os.path.join(target_path, filename), 'wb') as dst:
dst.write(src.read())
def main(target_path=None):
"""Get example scripts, example notebooks, and example data.
Copy the examples from the package directory and get the example data either
from the package directory or from the Parcels website.
"""
if target_path is None:
# get targe directory
parser = argparse.ArgumentParser(
description="Get Parcels example data.")
parser.add_argument(
"target_path",
help="Where to put the tutorials? (This path will be created.)")
args = parser.parse_args()
target_path = args.target_path
if os.path.exists(target_path):
print("Error: {} already exists.".format(target_path))
return
# copy data and examples
copy_data_and_examples_from_package_to(target_path)
# make sure the notebooks use the correct python version
set_jupyter_kernel_to_python_version(
target_path,
python_version=sys.version_info[0])
# try downloading remaining files
remaining_example_data_files = _still_to_download(
example_data_files, target_path)
download_files(example_data_url, remaining_example_data_files, target_path)
if __name__ == "__main__":
main()
| parcels/scripts/get_examples.py | 5,035 | Create directory (and parents) if they don't exist.
Only return the files that are not yet present on disk.
Copy example data from Parcels directory.
Return thos parths of the list `file_names` that were not found in the
package.
Mirror file_names from source_url to target_path.
Get example scripts, example notebooks, and example data.
Copy the examples from the package directory and get the example data either
from the package directory or from the Parcels website.
Set notebook kernelspec to desired python version.
This also drops all other meta data from the notebook.
Get example scripts, notebooks, and data files.
For Python 3.0 and later Fall back to Python 2's urllib2 get targe directory copy data and examples make sure the notebooks use the correct python version try downloading remaining files | 816 | en | 0.827698 |
# -*- coding: utf-8 -*-
# Copyright 2013 Google Inc. 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.
"""This module provides the notification command to gsutil."""
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
from __future__ import unicode_literals
import getopt
import re
import time
import uuid
from datetime import datetime
from gslib import metrics
from gslib.cloud_api import AccessDeniedException
from gslib.cloud_api import BadRequestException
from gslib.cloud_api import NotFoundException
from gslib.cloud_api import PublishPermissionDeniedException
from gslib.command import Command
from gslib.command import NO_MAX
from gslib.command_argument import CommandArgument
from gslib.cs_api_map import ApiSelector
from gslib.exception import CommandException
from gslib.help_provider import CreateHelpText
from gslib.project_id import PopulateProjectId
from gslib.pubsub_api import PubsubApi
from gslib.storage_url import StorageUrlFromString
from gslib.third_party.pubsub_apitools.pubsub_v1_messages import Binding
from gslib.utils import copy_helper
# Cloud Pub/Sub commands
_LIST_SYNOPSIS = """
gsutil notification list bucket_url...
"""
_DELETE_SYNOPSIS = """
gsutil notification delete (notificationConfigName|bucket_url)...
"""
_CREATE_SYNOPSIS = """
gsutil notification create -f (json|none) [-p prefix] [-t topic] \\
[-m key:value]... [-e eventType]... bucket_url
"""
# Object Change Notification commands
_WATCHBUCKET_SYNOPSIS = """
gsutil notification watchbucket [-i id] [-t token] app_url bucket_url
"""
_STOPCHANNEL_SYNOPSIS = """
gsutil notification stopchannel channel_id resource_id
"""
_SYNOPSIS = (
_CREATE_SYNOPSIS +
_DELETE_SYNOPSIS.lstrip('\n') +
_LIST_SYNOPSIS.lstrip('\n') +
_WATCHBUCKET_SYNOPSIS +
_STOPCHANNEL_SYNOPSIS.lstrip('\n') + '\n') # yapf: disable
_LIST_DESCRIPTION = """
<B>LIST</B>
The list sub-command provides a list of notification configs belonging to a
given bucket. The listed name of each notification config can be used with
the delete sub-command to delete that specific notification config.
For listing Object Change Notifications instead of Cloud Pub/Sub notification
subscription configs, add a -o flag.
<B>LIST EXAMPLES</B>
Fetch the list of notification configs for the bucket example-bucket:
gsutil notification list gs://example-bucket
The same as above, but for Object Change Notifications instead of Cloud
Pub/Sub notification subscription configs:
gsutil notification list -o gs://example-bucket
Fetch the notification configs in all buckets matching a wildcard:
gsutil notification list gs://example-*
Fetch all of the notification configs for buckets in the default project:
gsutil notification list gs://*
"""
_DELETE_DESCRIPTION = """
<B>DELETE</B>
The delete sub-command deletes notification configs from a bucket. If a
notification config name is passed as a parameter, that notification config
alone will be deleted. If a bucket name is passed, all notification configs
associated with that bucket will be deleted.
Cloud Pub/Sub topics associated with this notification config will not be
deleted by this command. Those must be deleted separately, for example with
the gcloud command `gcloud beta pubsub topics delete`.
Object Change Notification subscriptions cannot be deleted with this command.
For that, see the command `gsutil notification stopchannel`.
<B>DELETE EXAMPLES</B>
Delete a single notification config (with ID 3) in the bucket example-bucket:
gsutil notification delete projects/_/buckets/example-bucket/notificationConfigs/3
Delete all notification configs in the bucket example-bucket:
gsutil notification delete gs://example-bucket
"""
_CREATE_DESCRIPTION = """
<B>CREATE</B>
The create sub-command creates a notification config on a bucket, establishing
a flow of event notifications from Cloud Storage to a Cloud Pub/Sub topic. As
part of creating this flow, the create command also verifies that the
destination Cloud Pub/Sub topic exists, creating it if necessary, and verifies
that the Cloud Storage bucket has permission to publish events to that topic,
granting the permission if necessary.
If a destination Cloud Pub/Sub topic is not specified with the -t flag, Cloud
Storage will by default choose a topic name in the default project whose ID is
the same the bucket name. For example, if the default project ID specified is
'default-project' and the bucket being configured is gs://example-bucket, the
create command will use the Cloud Pub/Sub topic
"projects/default-project/topics/example-bucket".
In order to enable notifications, a `special Cloud Storage service account
<https://cloud.google.com/storage/docs/projects#service-accounts>`_ unique to
each project must have the IAM permission "projects.topics.publish". This
command will check to see if that permission exists and, if not, will attempt
to grant it.
You can create multiple notification configurations for a bucket, but their
triggers cannot overlap such that a single event could send multiple
notifications. Attempting to create a notification configuration that
overlaps with an existing notification configuration results in an error.
<B>CREATE EXAMPLES</B>
Begin sending notifications of all changes to the bucket example-bucket
to the Cloud Pub/Sub topic projects/default-project/topics/example-bucket:
gsutil notification create -f json gs://example-bucket
The same as above, but specifies the destination topic ID 'files-to-process'
in the default project:
gsutil notification create -f json \\
-t files-to-process gs://example-bucket
The same as above, but specifies a Cloud Pub/Sub topic belonging to the
specific cloud project 'example-project':
gsutil notification create -f json \\
-t projects/example-project/topics/files-to-process gs://example-bucket
Create a notification config that will only send an event when a new object
has been created:
gsutil notification create -f json -e OBJECT_FINALIZE gs://example-bucket
Create a topic and notification config that will only send an event when
an object beginning with "photos/" is affected:
gsutil notification create -p photos/ gs://example-bucket
List all of the notificationConfigs in bucket example-bucket:
gsutil notification list gs://example-bucket
Delete all notitificationConfigs for bucket example-bucket:
gsutil notification delete gs://example-bucket
Delete one specific notificationConfig for bucket example-bucket:
gsutil notification delete \\
projects/_/buckets/example-bucket/notificationConfigs/1
<B>OPTIONS</B>
The create sub-command has the following options
-e Specify an event type filter for this notification config. Cloud
Storage will only send notifications of this type. You may specify
this parameter multiple times to allow multiple event types. If not
specified, Cloud Storage will send notifications for all event
types. The valid types are:
OBJECT_FINALIZE - An object has been created.
OBJECT_METADATA_UPDATE - The metadata of an object has changed.
OBJECT_DELETE - An object has been permanently deleted.
OBJECT_ARCHIVE - A live Cloud Storage object has been archived.
-f Specifies the payload format of notification messages. Must be
either "json" for a payload matches the object metadata for the
JSON API, or "none" to specify no payload at all. In either case,
notification details are available in the message attributes.
-m Specifies a key:value attribute that will be appended to the set
of attributes sent to Cloud Pub/Sub for all events associated with
this notification config. You may specify this parameter multiple
times to set multiple attributes.
-p Specifies a prefix path filter for this notification config. Cloud
Storage will only send notifications for objects in this bucket
whose names begin with the specified prefix.
-s Skips creation and permission assignment of the Cloud Pub/Sub topic.
This is useful if the caller does not have permission to access
the topic in question, or if the topic already exists and has the
appropriate publish permission assigned.
-t The Cloud Pub/Sub topic to which notifications should be sent. If
not specified, this command will choose a topic whose project is
your default project and whose ID is the same as the Cloud Storage
bucket name.
<B>NEXT STEPS</B>
Once the create command has succeeded, Cloud Storage will publish a message to
the specified Cloud Pub/Sub topic when eligible changes occur. In order to
receive these messages, you must create a Pub/Sub subscription for your
Pub/Sub topic. To learn more about creating Pub/Sub subscriptions, see `the
Pub/Sub Subscriber Overview <https://cloud.google.com/pubsub/docs/subscriber>`_.
You can create a simple Pub/Sub subscription using the ``gcloud`` command-line
tool. For example, to create a new subscription on the topic "myNewTopic" and
attempt to pull messages from it, you could run:
gcloud beta pubsub subscriptions create --topic myNewTopic testSubscription
gcloud beta pubsub subscriptions pull --auto-ack testSubscription
"""
_WATCHBUCKET_DESCRIPTION = """
<B>WATCHBUCKET</B>
The watchbucket sub-command can be used to watch a bucket for object changes.
A service account must be used when running this command.
The app_url parameter must be an HTTPS URL to an application that will be
notified of changes to any object in the bucket. The URL endpoint must be
a verified domain on your project. See `Notification Authorization
<https://cloud.google.com/storage/docs/object-change-notification#_Authorization>`_
for details.
The optional id parameter can be used to assign a unique identifier to the
created notification channel. If not provided, a random UUID string will be
generated.
The optional token parameter can be used to validate notifications events.
To do this, set this custom token and store it to later verify that
notification events contain the client token you expect.
<B>WATCHBUCKET EXAMPLES</B>
Watch the bucket example-bucket for changes and send notifications to an
application server running at example.com:
gsutil notification watchbucket https://example.com/notify \\
gs://example-bucket
Assign identifier my-channel-id to the created notification channel:
gsutil notification watchbucket -i my-channel-id \\
https://example.com/notify gs://example-bucket
Set a custom client token that will be included with each notification event:
gsutil notification watchbucket -t my-client-token \\
https://example.com/notify gs://example-bucket
"""
_STOPCHANNEL_DESCRIPTION = """
<B>STOPCHANNEL</B>
The stopchannel sub-command can be used to stop sending change events to a
notification channel.
The channel_id and resource_id parameters should match the values from the
response of a bucket watch request.
<B>STOPCHANNEL EXAMPLES</B>
Stop the notification event channel with channel identifier channel1 and
resource identifier SoGqan08XDIFWr1Fv_nGpRJBHh8:
gsutil notification stopchannel channel1 SoGqan08XDIFWr1Fv_nGpRJBHh8
"""
_DESCRIPTION = """
The notification command is used to configure Google Cloud Storage support for
sending notifications to Cloud Pub/Sub as well as to configure the object
change notification feature.
<B>CLOUD PUB/SUB</B>
The "create", "list", and "delete" sub-commands deal with configuring Cloud
Storage integration with Google Cloud Pub/Sub.
""" + _CREATE_DESCRIPTION + _LIST_DESCRIPTION + _DELETE_DESCRIPTION + """
<B>OBJECT CHANGE NOTIFICATIONS</B>
For more information on the Object Change Notification feature, please see
`the Object Change Notification docs
<https://cloud.google.com/storage/docs/object-change-notification>`_.
The "watchbucket" and "stopchannel" sub-commands enable and disable Object
Change Notifications.
""" + _WATCHBUCKET_DESCRIPTION + _STOPCHANNEL_DESCRIPTION + """
<B>NOTIFICATIONS AND PARALLEL COMPOSITE UPLOADS</B>
By default, gsutil enables parallel composite uploads for large files (see
"gsutil help cp"), which means that an upload of a large object can result
in multiple temporary component objects being uploaded before the actual
intended object is created. Any subscriber to notifications for this bucket
will then see a notification for each of these components being created and
deleted. If this is a concern for you, note that parallel composite uploads
can be disabled by setting "parallel_composite_upload_threshold = 0" in your
boto config file. Alternately, your subscriber code can filter out gsutil's
parallel composite uploads by ignoring any notification about objects whose
names contain (but do not start with) the following string:
"{composite_namespace}".
""".format(composite_namespace=copy_helper.PARALLEL_UPLOAD_TEMP_NAMESPACE)
NOTIFICATION_AUTHORIZATION_FAILED_MESSAGE = """
Watch bucket attempt failed:
{watch_error}
You attempted to watch a bucket with an application URL of:
{watch_url}
which is not authorized for your project. Please ensure that you are using
Service Account authentication and that the Service Account's project is
authorized for the application URL. Notification endpoint URLs must also be
whitelisted in your Cloud Console project. To do that, the domain must also be
verified using Google Webmaster Tools. For instructions, please see
`Notification Authorization
<https://cloud.google.com/storage/docs/object-change-notification#_Authorization>`_.
"""
_DETAILED_HELP_TEXT = CreateHelpText(_SYNOPSIS, _DESCRIPTION)
# yapf: disable
_create_help_text = (
CreateHelpText(_CREATE_SYNOPSIS, _CREATE_DESCRIPTION))
_list_help_text = (
CreateHelpText(_LIST_SYNOPSIS, _LIST_DESCRIPTION))
_delete_help_text = (
CreateHelpText(_DELETE_SYNOPSIS, _DELETE_DESCRIPTION))
_watchbucket_help_text = (
CreateHelpText(_WATCHBUCKET_SYNOPSIS, _WATCHBUCKET_DESCRIPTION))
_stopchannel_help_text = (
CreateHelpText(_STOPCHANNEL_SYNOPSIS, _STOPCHANNEL_DESCRIPTION))
# yapf: enable
PAYLOAD_FORMAT_MAP = {
'none': 'NONE',
'json': 'JSON_API_V1',
}
class NotificationCommand(Command):
"""Implementation of gsutil notification command."""
# Notification names might look like one of these:
# canonical form: projects/_/buckets/bucket/notificationConfigs/3
# JSON API form: b/bucket/notificationConfigs/5
# Either of the above might start with a / if a user is copying & pasting.
def _GetNotificationPathRegex(self):
if not NotificationCommand._notification_path_regex:
NotificationCommand._notification_path_regex = re.compile(
('/?(projects/[^/]+/)?b(uckets)?/(?P<bucket>[^/]+)/'
'notificationConfigs/(?P<notification>[0-9]+)'))
return NotificationCommand._notification_path_regex
_notification_path_regex = None
# Command specification. See base class for documentation.
command_spec = Command.CreateCommandSpec(
'notification',
command_name_aliases=[
'notify',
'notifyconfig',
'notifications',
'notif',
],
usage_synopsis=_SYNOPSIS,
min_args=2,
max_args=NO_MAX,
supported_sub_args='i:t:m:t:of:e:p:s',
file_url_ok=False,
provider_url_ok=False,
urls_start_arg=1,
gs_api_support=[ApiSelector.JSON],
gs_default_api=ApiSelector.JSON,
argparse_arguments={
'watchbucket': [
CommandArgument.MakeFreeTextArgument(),
CommandArgument.MakeZeroOrMoreCloudBucketURLsArgument(),
],
'stopchannel': [],
'list': [
CommandArgument.MakeZeroOrMoreCloudBucketURLsArgument(),
],
'delete': [
# Takes a list of one of the following:
# notification: projects/_/buckets/bla/notificationConfigs/5,
# bucket: gs://foobar
CommandArgument.MakeZeroOrMoreCloudURLsArgument(),
],
'create': [
CommandArgument.MakeFreeTextArgument(), # Cloud Pub/Sub topic
CommandArgument.MakeNCloudBucketURLsArgument(1),
]
},
)
# Help specification. See help_provider.py for documentation.
help_spec = Command.HelpSpec(
help_name='notification',
help_name_aliases=[
'watchbucket',
'stopchannel',
'notifyconfig',
],
help_type='command_help',
help_one_line_summary='Configure object change notification',
help_text=_DETAILED_HELP_TEXT,
subcommand_help_text={
'create': _create_help_text,
'list': _list_help_text,
'delete': _delete_help_text,
'watchbucket': _watchbucket_help_text,
'stopchannel': _stopchannel_help_text,
},
)
def _WatchBucket(self):
"""Creates a watch on a bucket given in self.args."""
self.CheckArguments()
identifier = None
client_token = None
if self.sub_opts:
for o, a in self.sub_opts:
if o == '-i':
identifier = a
if o == '-t':
client_token = a
identifier = identifier or str(uuid.uuid4())
watch_url = self.args[0]
bucket_arg = self.args[-1]
if not watch_url.lower().startswith('https://'):
raise CommandException('The application URL must be an https:// URL.')
bucket_url = StorageUrlFromString(bucket_arg)
if not (bucket_url.IsBucket() and bucket_url.scheme == 'gs'):
raise CommandException(
'The %s command can only be used with gs:// bucket URLs.' %
self.command_name)
if not bucket_url.IsBucket():
raise CommandException('URL must name a bucket for the %s command.' %
self.command_name)
self.logger.info('Watching bucket %s with application URL %s ...',
bucket_url, watch_url)
try:
channel = self.gsutil_api.WatchBucket(bucket_url.bucket_name,
watch_url,
identifier,
token=client_token,
provider=bucket_url.scheme)
except AccessDeniedException as e:
self.logger.warn(
NOTIFICATION_AUTHORIZATION_FAILED_MESSAGE.format(watch_error=str(e),
watch_url=watch_url))
raise
channel_id = channel.id
resource_id = channel.resourceId
client_token = channel.token
self.logger.info('Successfully created watch notification channel.')
self.logger.info('Watch channel identifier: %s', channel_id)
self.logger.info('Canonicalized resource identifier: %s', resource_id)
self.logger.info('Client state token: %s', client_token)
return 0
def _StopChannel(self):
channel_id = self.args[0]
resource_id = self.args[1]
self.logger.info('Removing channel %s with resource identifier %s ...',
channel_id, resource_id)
self.gsutil_api.StopChannel(channel_id, resource_id, provider='gs')
self.logger.info('Succesfully removed channel.')
return 0
def _ListChannels(self, bucket_arg):
"""Lists active channel watches on a bucket given in self.args."""
bucket_url = StorageUrlFromString(bucket_arg)
if not (bucket_url.IsBucket() and bucket_url.scheme == 'gs'):
raise CommandException(
'The %s command can only be used with gs:// bucket URLs.' %
self.command_name)
if not bucket_url.IsBucket():
raise CommandException('URL must name a bucket for the %s command.' %
self.command_name)
channels = self.gsutil_api.ListChannels(bucket_url.bucket_name,
provider='gs').items
self.logger.info(
'Bucket %s has the following active Object Change Notifications:',
bucket_url.bucket_name)
for idx, channel in enumerate(channels):
self.logger.info('\tNotification channel %d:', idx + 1)
self.logger.info('\t\tChannel identifier: %s', channel.channel_id)
self.logger.info('\t\tResource identifier: %s', channel.resource_id)
self.logger.info('\t\tApplication URL: %s', channel.push_url)
self.logger.info('\t\tCreated by: %s', channel.subscriber_email)
self.logger.info(
'\t\tCreation time: %s',
str(datetime.fromtimestamp(channel.creation_time_ms / 1000)))
return 0
def _Create(self):
self.CheckArguments()
# User-specified options
pubsub_topic = None
payload_format = None
custom_attributes = {}
event_types = []
object_name_prefix = None
should_setup_topic = True
if self.sub_opts:
for o, a in self.sub_opts:
if o == '-e':
event_types.append(a)
elif o == '-f':
payload_format = a
elif o == '-m':
if ':' not in a:
raise CommandException(
'Custom attributes specified with -m should be of the form '
'key:value')
key, value = a.split(':')
custom_attributes[key] = value
elif o == '-p':
object_name_prefix = a
elif o == '-s':
should_setup_topic = False
elif o == '-t':
pubsub_topic = a
if payload_format not in PAYLOAD_FORMAT_MAP:
raise CommandException(
"Must provide a payload format with -f of either 'json' or 'none'")
payload_format = PAYLOAD_FORMAT_MAP[payload_format]
bucket_arg = self.args[-1]
bucket_url = StorageUrlFromString(bucket_arg)
if not bucket_url.IsCloudUrl() or not bucket_url.IsBucket():
raise CommandException(
"%s %s requires a GCS bucket name, but got '%s'" %
(self.command_name, self.subcommand_name, bucket_arg))
if bucket_url.scheme != 'gs':
raise CommandException(
'The %s command can only be used with gs:// bucket URLs.' %
self.command_name)
bucket_name = bucket_url.bucket_name
self.logger.debug('Creating notification for bucket %s', bucket_url)
# Find the project this bucket belongs to
bucket_metadata = self.gsutil_api.GetBucket(bucket_name,
fields=['projectNumber'],
provider=bucket_url.scheme)
bucket_project_number = bucket_metadata.projectNumber
# If not specified, choose a sensible default for the Cloud Pub/Sub topic
# name.
if not pubsub_topic:
pubsub_topic = 'projects/%s/topics/%s' % (PopulateProjectId(None),
bucket_name)
if not pubsub_topic.startswith('projects/'):
# If a user picks a topic ID (mytopic) but doesn't pass the whole name (
# projects/my-project/topics/mytopic ), pick a default project.
pubsub_topic = 'projects/%s/topics/%s' % (PopulateProjectId(None),
pubsub_topic)
self.logger.debug('Using Cloud Pub/Sub topic %s', pubsub_topic)
just_modified_topic_permissions = False
if should_setup_topic:
# Ask GCS for the email address that represents GCS's permission to
# publish to a Cloud Pub/Sub topic from this project.
service_account = self.gsutil_api.GetProjectServiceAccount(
bucket_project_number, provider=bucket_url.scheme).email_address
self.logger.debug('Service account for project %d: %s',
bucket_project_number, service_account)
just_modified_topic_permissions = self._CreateTopic(
pubsub_topic, service_account)
for attempt_number in range(0, 2):
try:
create_response = self.gsutil_api.CreateNotificationConfig(
bucket_name,
pubsub_topic=pubsub_topic,
payload_format=payload_format,
custom_attributes=custom_attributes,
event_types=event_types if event_types else None,
object_name_prefix=object_name_prefix,
provider=bucket_url.scheme)
break
except PublishPermissionDeniedException:
if attempt_number == 0 and just_modified_topic_permissions:
# If we have just set the IAM policy, it may take up to 10 seconds to
# take effect.
self.logger.info(
'Retrying create notification in 10 seconds '
'(new permissions may take up to 10 seconds to take effect.)')
time.sleep(10)
else:
raise
notification_name = 'projects/_/buckets/%s/notificationConfigs/%s' % (
bucket_name, create_response.id)
self.logger.info('Created notification config %s', notification_name)
return 0
def _CreateTopic(self, pubsub_topic, service_account):
"""Assures that a topic exists, creating it if necessary.
Also adds GCS as a publisher on that bucket, if necessary.
Args:
pubsub_topic: name of the Cloud Pub/Sub topic to use/create.
service_account: the GCS service account that needs publish permission.
Returns:
true if we modified IAM permissions, otherwise false.
"""
pubsub_api = PubsubApi(logger=self.logger)
# Verify that the Pub/Sub topic exists. If it does not, create it.
try:
pubsub_api.GetTopic(topic_name=pubsub_topic)
self.logger.debug('Topic %s already exists', pubsub_topic)
except NotFoundException:
self.logger.debug('Creating topic %s', pubsub_topic)
pubsub_api.CreateTopic(topic_name=pubsub_topic)
self.logger.info('Created Cloud Pub/Sub topic %s', pubsub_topic)
# Verify that the service account is in the IAM policy.
policy = pubsub_api.GetTopicIamPolicy(topic_name=pubsub_topic)
binding = Binding(role='roles/pubsub.publisher',
members=['serviceAccount:%s' % service_account])
# This could be more extensive. We could, for instance, check for roles
# that are stronger that pubsub.publisher, like owner. We could also
# recurse up the hierarchy looking to see if there are project-level
# permissions. This can get very complex very quickly, as the caller
# may not necessarily have access to the project-level IAM policy.
# There's no danger in double-granting permission just to make sure it's
# there, though.
if binding not in policy.bindings:
policy.bindings.append(binding)
# transactional safety via etag field.
pubsub_api.SetTopicIamPolicy(topic_name=pubsub_topic, policy=policy)
return True
else:
self.logger.debug('GCS already has publish permission to topic %s.',
pubsub_topic)
return False
def _EnumerateNotificationsFromArgs(self, accept_notification_configs=True):
"""Yields bucket/notification tuples from command-line args.
Given a list of strings that are bucket names (gs://foo) or notification
config IDs, yield tuples of bucket names and their associated notifications.
Args:
accept_notification_configs: whether notification configs are valid args.
Yields:
Tuples of the form (bucket_name, Notification)
"""
path_regex = self._GetNotificationPathRegex()
for list_entry in self.args:
match = path_regex.match(list_entry)
if match:
if not accept_notification_configs:
raise CommandException(
'%s %s accepts only bucket names, but you provided %s' %
(self.command_name, self.subcommand_name, list_entry))
bucket_name = match.group('bucket')
notification_id = match.group('notification')
found = False
for notification in self.gsutil_api.ListNotificationConfigs(
bucket_name, provider='gs'):
if notification.id == notification_id:
yield (bucket_name, notification)
found = True
break
if not found:
raise NotFoundException('Could not find notification %s' % list_entry)
else:
storage_url = StorageUrlFromString(list_entry)
if not storage_url.IsCloudUrl():
raise CommandException(
'The %s command must be used on cloud buckets or notification '
'config names.' % self.command_name)
if storage_url.scheme != 'gs':
raise CommandException('The %s command only works on gs:// buckets.')
path = None
if storage_url.IsProvider():
path = 'gs://*'
elif storage_url.IsBucket():
path = list_entry
if not path:
raise CommandException(
'The %s command cannot be used on cloud objects, only buckets' %
self.command_name)
for blr in self.WildcardIterator(path).IterBuckets(
bucket_fields=['id']):
for notification in self.gsutil_api.ListNotificationConfigs(
blr.storage_url.bucket_name, provider='gs'):
yield (blr.storage_url.bucket_name, notification)
def _List(self):
self.CheckArguments()
if self.sub_opts:
if '-o' in dict(self.sub_opts):
for bucket_name in self.args:
self._ListChannels(bucket_name)
else:
for bucket_name, notification in self._EnumerateNotificationsFromArgs(
accept_notification_configs=False):
self._PrintNotificationDetails(bucket_name, notification)
return 0
def _PrintNotificationDetails(self, bucket, notification):
print('projects/_/buckets/{bucket}/notificationConfigs/{notification}\n'
'\tCloud Pub/Sub topic: {topic}'.format(
bucket=bucket,
notification=notification.id,
topic=notification.topic[len('//pubsub.googleapis.com/'):]))
if notification.custom_attributes:
print('\tCustom attributes:')
for attr in notification.custom_attributes.additionalProperties:
print('\t\t%s: %s' % (attr.key, attr.value))
filters = []
if notification.event_types:
filters.append('\t\tEvent Types: %s' %
', '.join(notification.event_types))
if notification.object_name_prefix:
filters.append("\t\tObject name prefix: '%s'" %
notification.object_name_prefix)
if filters:
print('\tFilters:')
for line in filters:
print(line)
self.logger.info('')
def _Delete(self):
for bucket_name, notification in self._EnumerateNotificationsFromArgs():
self._DeleteNotification(bucket_name, notification.id)
return 0
def _DeleteNotification(self, bucket_name, notification_id):
self.gsutil_api.DeleteNotificationConfig(bucket_name,
notification=notification_id,
provider='gs')
return 0
def _RunSubCommand(self, func):
try:
(self.sub_opts,
self.args) = getopt.getopt(self.args,
self.command_spec.supported_sub_args)
# Commands with both suboptions and subcommands need to reparse for
# suboptions, so we log again.
metrics.LogCommandParams(sub_opts=self.sub_opts)
return func(self)
except getopt.GetoptError:
self.RaiseInvalidArgumentException()
SUBCOMMANDS = {
'create': _Create,
'list': _List,
'delete': _Delete,
'watchbucket': _WatchBucket,
'stopchannel': _StopChannel
}
def RunCommand(self):
"""Command entry point for the notification command."""
self.subcommand_name = self.args.pop(0)
if self.subcommand_name in NotificationCommand.SUBCOMMANDS:
metrics.LogCommandParams(subcommands=[self.subcommand_name])
return self._RunSubCommand(
NotificationCommand.SUBCOMMANDS[self.subcommand_name])
else:
raise CommandException('Invalid subcommand "%s" for the %s command.' %
(self.subcommand_name, self.command_name))
| gslib/commands/notification.py | 32,907 | Implementation of gsutil notification command.
Command entry point for the notification command.
Assures that a topic exists, creating it if necessary.
Also adds GCS as a publisher on that bucket, if necessary.
Args:
pubsub_topic: name of the Cloud Pub/Sub topic to use/create.
service_account: the GCS service account that needs publish permission.
Returns:
true if we modified IAM permissions, otherwise false.
Yields bucket/notification tuples from command-line args.
Given a list of strings that are bucket names (gs://foo) or notification
config IDs, yield tuples of bucket names and their associated notifications.
Args:
accept_notification_configs: whether notification configs are valid args.
Yields:
Tuples of the form (bucket_name, Notification)
Lists active channel watches on a bucket given in self.args.
Creates a watch on a bucket given in self.args.
This module provides the notification command to gsutil.
-*- coding: utf-8 -*- Copyright 2013 Google Inc. 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. Cloud Pub/Sub commands Object Change Notification commands yapf: disable yapf: disable yapf: enable Notification names might look like one of these: canonical form: projects/_/buckets/bucket/notificationConfigs/3 JSON API form: b/bucket/notificationConfigs/5 Either of the above might start with a / if a user is copying & pasting. Command specification. See base class for documentation. Takes a list of one of the following: notification: projects/_/buckets/bla/notificationConfigs/5, bucket: gs://foobar Cloud Pub/Sub topic Help specification. See help_provider.py for documentation. User-specified options Find the project this bucket belongs to If not specified, choose a sensible default for the Cloud Pub/Sub topic name. If a user picks a topic ID (mytopic) but doesn't pass the whole name ( projects/my-project/topics/mytopic ), pick a default project. Ask GCS for the email address that represents GCS's permission to publish to a Cloud Pub/Sub topic from this project. If we have just set the IAM policy, it may take up to 10 seconds to take effect. Verify that the Pub/Sub topic exists. If it does not, create it. Verify that the service account is in the IAM policy. This could be more extensive. We could, for instance, check for roles that are stronger that pubsub.publisher, like owner. We could also recurse up the hierarchy looking to see if there are project-level permissions. This can get very complex very quickly, as the caller may not necessarily have access to the project-level IAM policy. There's no danger in double-granting permission just to make sure it's there, though. transactional safety via etag field. Commands with both suboptions and subcommands need to reparse for suboptions, so we log again. | 3,272 | en | 0.858069 |
import tensorflow as tf
from google.protobuf import json_format, text_format
from tensorflow.contrib import graph_editor as ge
def save():
with tf.Graph().as_default() as g:
x = tf.placeholder(tf.float32, name="input")
a = tf.Variable(5.0)
res: tf.Tensor = tf.multiply(a, x, name="mul")
with tf.Session(graph=g) as sess:
sess.run(tf.global_variables_initializer())
g: tf.Graph = tf.get_default_graph()
gdef = g.as_graph_def()
gdef = tf.graph_util.convert_variables_to_constants(
sess,
gdef, ["mul"],
variable_names_whitelist=None,
variable_names_blacklist=None
)
tf.train.write_graph(gdef, logdir="/tmp/k", name="test")
def load():
with tf.Graph().as_default() as g:
with tf.Session(graph=g) as sess:
x = tf.placeholder(tf.float32)
xx = 2 * x + 7
with open("/tmp/k/test", 'rb') as f:
graph_def = tf.GraphDef()
text_format.Merge(f.read(), graph_def)
y = tf.import_graph_def(
graph_def,
input_map={
"input:0": xx,
},
return_elements=["mul:0"],
name=None,
op_dict=None,
producer_op_list=None
)
print(sess.run(y, feed_dict={
x: 15,
}))
def main():
# save()
load()
with tf.Graph().as_default():
rr = tf.constant(15.0)
with tf.Session() as sess:
meta_graph_def = tf.saved_model.loader.load(
sess, [tf.saved_model.tag_constants.SERVING], "/tmp/lll"
)
signature = meta_graph_def.signature_def
signature_key = tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY
x_tensor_name = signature[signature_key].inputs["x"].name
y_tensor_name = signature[signature_key].outputs["y"].name
x = sess.graph.get_tensor_by_name(x_tensor_name)
y = sess.graph.get_tensor_by_name(y_tensor_name)
h = tf.get_session_handle(rr)
h = sess.run(h)
y_out = sess.run(y, {x: h})
# print(y_out)
# for op in tf.get_default_graph().get_operations():
# print(op.name)
if __name__ == "__main__":
main()
| mincall/_experiments/load_save_modify.py | 2,418 | save() print(y_out) for op in tf.get_default_graph().get_operations(): print(op.name) | 89 | en | 0.151019 |
from __future__ import print_function
import sys
from operator import add
from pyspark import SparkContext
from pyspark import SparkConf,SparkContext
from pyspark.streaming import StreamingContext
import sys
import requests
from operator import add
from pyspark.sql.types import *
from pyspark.sql import functions as func
from pyspark.sql.functions import lit
from pyspark.sql.functions import udf
from pyspark.sql.functions import *
from pyspark.sql.functions import array
# if __name__ == "__main__":
# create spark configuration
# conf = SparkConf(appName="TPCH-Example")
# create spark context with the above configuration
# sc = SparkContext(conf=conf)
# lineitems = sqlContext.read.format('csv').options(header='true', inferSchema='true', sep ="|").load(sys.arg[1])
path="file:////home/kia/GIT/MET-CS777/data/tpch_tables_scale_0.1/"
# path is where you have the folder. It can be a distributed path like S3, gc or hdfs
customer = sqlContext.read.format('csv').options(header='true', inferSchema='true', sep ="|").load(path+"customer.tbl")
orders = sqlContext.read.format('csv').options(header='true', inferSchema='true', sep ="|").load(path+"orders.tbl")
lineitems = sqlContext.read.format('csv').options(header='true', inferSchema='true', sep ="|").load(path+"lineitem.tbl")
part = sqlContext.read.format('csv').options(header='true', inferSchema='true', sep ="|").load(path+"part.tbl")
supplier = sqlContext.read.format('csv').options(header='true', inferSchema='true', sep ="|").load(path+"supplier.tbl")
partsupp = sqlContext.read.format('csv').options(header='true', inferSchema='true', sep ="|").load(path+"partsupp.tbl")
region = sqlContext.read.format('csv').options(header='true', inferSchema='true', sep ="|").load(path+"region.tbl")
nation = sqlContext.read.format('csv').options(header='true', inferSchema='true', sep ="|").load(path+"nation.tbl")
# You can convert all to RDDs if you want.
# customerRDD=customer.rdd
# ordersRDD=orders.rdd
# lineitemsRDD=lineitems.rdd
# partRDD=part.rdd
# supplierRDD=supplier.rdd
# partsuppRDD=partsupp.rdd
# regionRDD=region.rdd
# nationRDD=nation.rdd
# Question 1
# Implement a pyspark code that can find out the top-10 sold products.
lines = lineitems.select("ORDERKEY", "PARTKEY")\
.withColumn("COUNT", lit(1))\
.groupBy("PARTKEY").agg(func.sum("COUNT"))
result_1 = lines.orderBy("sum(COUNT)", ascending=False).limit(10).show()
result_1.saveAsTextFile(sys.arg[2])
# ---------------------------------------------------------------------------
# Question 2
# Find the top-10 customers based on the number of products ordered.
order_parts = lineitems.select("ORDERKEY", "PARTKEY")
customer_orders = orders.select("ORDERKEY", "CUSTKEY")
# Here we get the a table of all customers and their ordered parts.
customer_parts = customer_orders.join(order_parts, customer_orders.ORDERKEY == order_parts.ORDERKEY , 'full' ).drop('ORDERKEY')
# After we have a table of (CUSTKEY, ORDERKEY), we can just count up the number of times that we see the customer key in the table for each customer
# And this is a the number of times that the customer ordered parts.
customer_parts.withColumn("COUNT", lit(1)).groupBy("CUSTKEY").agg(func.sum("COUNT")).orderBy("sum(COUNT)", ascending=False).limit(10).show()
# +-------+----------+
# |CUSTKEY|sum(COUNT)|
# +-------+----------+
# | 8362| 155|
# | 346| 153|
# | 14707| 149|
# | 11998| 148|
# | 9454| 148|
# | 14398| 147|
# | 85| 142|
# | 10354| 142|
# | 3709| 141|
# | 547| 141|
# +-------+----------+
# ---------------------------------------------------------------------------
# Question 3
# Find the top-10 customers that have ordered products from the same supplier.
partsupp_keys=partsupp.select("PARTKEY", "SUPPKEY")
custpmer_supplier=customer_parts.join(partsupp_keys, customer_parts.PARTKEY == partsupp.PARTKEY , 'full' ).drop('PARTKEY')
custpmer_supplier.withColumn("COUNT", lit(1)).groupBy("CUSTKEY", "SUPPKEY").agg(func.sum("COUNT")).orderBy("sum(COUNT)", ascending=False).limit(10).show()
# +-------+-------+----------+
# |CUSTKEY|SUPPKEY|sum(COUNT)|
# +-------+-------+----------+
# | 4567| 844| 7|
# | 4792| 592| 6|
# | 11809| 17| 6|
# | 14767| 8| 6|
# | 2173| 572| 6|
# | 6139| 233| 6|
# | 874| 430| 6|
# | 154| 380| 5|
# | 6889| 729| 5|
# | 8794| 545| 5|
# +-------+-------+----------+
# ---------------------------------------------------------------------------
# Question 4 and 5
# Find the customers who have not ordered products from their own country and have ordered only foreign products.
# Solution:
# We get from custpmer_supplier CUSTKEY and SUPPKEY
# custpmer_supplier.show()
# +-------+-------+
# |CUSTKEY|SUPPKEY|
# +-------+-------+
# | 9733| 149|
# | 9733| 399|
# | 9733| 649|
# ...
# We need to just check if the customer has ordered something from his own country.
custpmer_supplier.show()
customer_nationKey = customer.select("CUSTKEY", "NATIONKEY")
supplier_nationKey = supplier.select("SUPPKEY", "NATIONKEY")
custpmer_supplier_custNation = custpmer_supplier.join(customer_nationKey, "CUSTKEY", 'full')
custpmer_supplier_supNation = custpmer_supplier.join(supplier_nationKey, "SUPPKEY", 'full')
from pyspark.sql import functions as F
from pyspark.sql.functions import udf
custpmer_supplier_custNation_agg = custpmer_supplier_custNation.groupBy("CUSTKEY").agg(F.collect_set("NATIONKEY").alias('agg_nation'))
cSN_agg_withSupp = custpmer_supplier_custNation.join(custpmer_supplier_custNation_agg, "CUSTKEY" , 'full')
# We need to cast the NationKey to IntegerType
cSN_agg_withSupp = cSN_agg_withSupp.withColumn("NATIONKEY", cSN_agg_withSupp["NATIONKEY"].cast(IntegerType()))
# Check Schema
cSN_agg_withSupp.printSchema()
# Define a UDF to check if the nation of the customer is in the list of his orders_products_nations
isIn_udf = udf(lambda element, mlist: True if element in mlist else False, BooleanType())
from_own = cSN_agg_withSupp.withColumn("from_own", isIn_udf(cSN_agg_withSupp.NATIONKEY, cSN_agg_withSupp.agg_nation))
# Should return none after filter because we have no customer that have not ordered products from his own country.
from_own.filter(from_own.from_own==False).show()
# from_own.show()
# +-------+-------+---------+----------+--------+
# |CUSTKEY|SUPPKEY|NATIONKEY|agg_nation|from_own|
# +-------+-------+---------+----------+--------+
# +-------+-------+---------+----------+--------+
# ---------------------------------------------------------------------------
# Question 6
# Find the top-10 similar customers based of their orders. (Jaccard Similarity)
# First of all we collect all of the products that each customer ordered.
custoemr_partset=customer_parts.groupBy("CUSTKEY").agg(F.collect_set("PARTKEY").alias('product_list'))
# Do the cross join and rename fields
customers_parts_combi = custoemr_partset.crossJoin(custoemr_partset ).toDF("C1", "L1", "C2" , "L2").filter("C1 not like C2")
# then we can drop duplicates which might take longer time.
customers_parts_combi = customers_parts_combi.dropDuplicates(['C1','C2'])
# Define a user defined function for calculation of Jaccard similarity distance
# Return type is Float and it should defined.
jaccard = udf(lambda a, b: float(float( len(set(a) & set(b))) / len( set(a) | set(b) )) , FloatType())
customer_jaccard = customers_parts_combi.withColumn("jaccard", jaccard(customers_parts_combi.L1, customers_parts_combi.L2))
# The following line will cause large number of computation tasks.
customer_jaccard.orderBy("jaccard", ascending=False).limit(10).show()
# On TPCH scale 0.1 you can get the following results
# +-----+--------------------+-----+--------------------+----------+
# | C1| L1| C2| L2| jaccard|
# +-----+--------------------+-----+--------------------+----------+
# |10376|[13032, 18343, 15...| 8456|[15747, 18343, 41...|0.09090909|
# | 8456|[15747, 18343, 41...|10376|[13032, 18343, 15...|0.09090909|
# | 4808|[17169, 19122, 33...|10901|[10142, 9529, 124...|0.06666667|
# |10901|[10142, 9529, 124...| 4808|[17169, 19122, 33...|0.06666667|
# | 7532|[15572, 2151, 174...| 5390|[5452, 16969, 755...|0.06451613|
# | 5390|[5452, 16969, 755...| 7532|[15572, 2151, 174...|0.06451613|
# | 2489|[6418, 7101, 7102...| 4283|[13060, 12044, 12...|0.06349207|
# | 4283|[13060, 12044, 12...| 2489|[6418, 7101, 7102...|0.06349207|
# | 7739|[9743, 16030, 489...| 5462|[6890, 7231, 1737...| 0.0625|
# | 4385|[1648, 7100, 1122...| 2768|[19866, 1648, 123...| 0.0625|
# +-----+--------------------+-----+--------------------+----------+
# The most similar customers are 10376 and 8456
# ---------------------------------------------------------------------------
# Question 7
# Find the top-10 product pairs that are ordered mostly together.
# RDD solution
# Easier to do it in RDD
lineitemsRDD = sqlContext.read.format('csv').options(header='true', inferSchema='false', sep ="|").load(path+"lineitem.tbl").rdd
orderPartsRDD = lineitemsRDD.map(lambda x: (x[0], x[1]))
order_PartList_RDD = orderPartsRDD.combineByKey(lambda x: [x], lambda u, v: u + [v], lambda u1,u2: u1+u2).map(lambda x:(x[0], list(x[1]))).filter(lambda x: len(x[1])>1)
from itertools import combinations
order_PartPermutList_RDD= order_PartList_RDD .flatMap(lambda x: combinations(x[1], 2) ).map(lambda x: ((x[0], x[1] ), 1))
order_PartPermutList_RDD.reduceByKey(lambda a, b:a+b).top(10, lambda x: x[1])
# Dataframe
from pyspark.sql import functions as F
order_parts = lineitems.select("ORDERKEY", "PARTKEY")
partLists= order_parts.groupBy("ORDERKEY").agg(F.collect_set("PARTKEY").alias('listParts'))
# Process only pairs of products - remove orders that include only one single product.
partLists= partLists.where(size(col("listParts")) > 1)
# Define a function to create all pair combinations.
# You can also use itertools
# import itertools
# from itertools import permutations
# I define here the following permutation function.
def permut(x):
a=list()
for i in range(len(x)):
for j in range(i, len(x)):
if(i != j):
a.append(str(x[i])+"-"+str(x[j]))
return a
# ...
| Spark-Example-TPCH/TPCH-Example_Solution_Dataframe.py | 10,633 | if __name__ == "__main__": create spark configuration conf = SparkConf(appName="TPCH-Example") create spark context with the above configuration sc = SparkContext(conf=conf) lineitems = sqlContext.read.format('csv').options(header='true', inferSchema='true', sep ="|").load(sys.arg[1]) path is where you have the folder. It can be a distributed path like S3, gc or hdfs You can convert all to RDDs if you want. customerRDD=customer.rdd ordersRDD=orders.rdd lineitemsRDD=lineitems.rdd partRDD=part.rdd supplierRDD=supplier.rdd partsuppRDD=partsupp.rdd regionRDD=region.rdd nationRDD=nation.rdd Question 1 Implement a pyspark code that can find out the top-10 sold products. --------------------------------------------------------------------------- Question 2 Find the top-10 customers based on the number of products ordered. Here we get the a table of all customers and their ordered parts. After we have a table of (CUSTKEY, ORDERKEY), we can just count up the number of times that we see the customer key in the table for each customer And this is a the number of times that the customer ordered parts. +-------+----------+ |CUSTKEY|sum(COUNT)| +-------+----------+ | 8362| 155| | 346| 153| | 14707| 149| | 11998| 148| | 9454| 148| | 14398| 147| | 85| 142| | 10354| 142| | 3709| 141| | 547| 141| +-------+----------+ --------------------------------------------------------------------------- Question 3 Find the top-10 customers that have ordered products from the same supplier. +-------+-------+----------+ |CUSTKEY|SUPPKEY|sum(COUNT)| +-------+-------+----------+ | 4567| 844| 7| | 4792| 592| 6| | 11809| 17| 6| | 14767| 8| 6| | 2173| 572| 6| | 6139| 233| 6| | 874| 430| 6| | 154| 380| 5| | 6889| 729| 5| | 8794| 545| 5| +-------+-------+----------+ --------------------------------------------------------------------------- Question 4 and 5 Find the customers who have not ordered products from their own country and have ordered only foreign products. Solution: We get from custpmer_supplier CUSTKEY and SUPPKEY custpmer_supplier.show() +-------+-------+ |CUSTKEY|SUPPKEY| +-------+-------+ | 9733| 149| | 9733| 399| | 9733| 649| ... We need to just check if the customer has ordered something from his own country. We need to cast the NationKey to IntegerType Check Schema Define a UDF to check if the nation of the customer is in the list of his orders_products_nations Should return none after filter because we have no customer that have not ordered products from his own country. from_own.show() +-------+-------+---------+----------+--------+ |CUSTKEY|SUPPKEY|NATIONKEY|agg_nation|from_own| +-------+-------+---------+----------+--------+ +-------+-------+---------+----------+--------+ --------------------------------------------------------------------------- Question 6 Find the top-10 similar customers based of their orders. (Jaccard Similarity) First of all we collect all of the products that each customer ordered. Do the cross join and rename fields then we can drop duplicates which might take longer time. Define a user defined function for calculation of Jaccard similarity distance Return type is Float and it should defined. The following line will cause large number of computation tasks. On TPCH scale 0.1 you can get the following results +-----+--------------------+-----+--------------------+----------+ | C1| L1| C2| L2| jaccard| +-----+--------------------+-----+--------------------+----------+ |10376|[13032, 18343, 15...| 8456|[15747, 18343, 41...|0.09090909| | 8456|[15747, 18343, 41...|10376|[13032, 18343, 15...|0.09090909| | 4808|[17169, 19122, 33...|10901|[10142, 9529, 124...|0.06666667| |10901|[10142, 9529, 124...| 4808|[17169, 19122, 33...|0.06666667| | 7532|[15572, 2151, 174...| 5390|[5452, 16969, 755...|0.06451613| | 5390|[5452, 16969, 755...| 7532|[15572, 2151, 174...|0.06451613| | 2489|[6418, 7101, 7102...| 4283|[13060, 12044, 12...|0.06349207| | 4283|[13060, 12044, 12...| 2489|[6418, 7101, 7102...|0.06349207| | 7739|[9743, 16030, 489...| 5462|[6890, 7231, 1737...| 0.0625| | 4385|[1648, 7100, 1122...| 2768|[19866, 1648, 123...| 0.0625| +-----+--------------------+-----+--------------------+----------+ The most similar customers are 10376 and 8456 --------------------------------------------------------------------------- Question 7 Find the top-10 product pairs that are ordered mostly together. RDD solution Easier to do it in RDD Dataframe Process only pairs of products - remove orders that include only one single product. Define a function to create all pair combinations. You can also use itertools import itertools from itertools import permutations I define here the following permutation function. ... | 5,204 | en | 0.631243 |
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2018-2019 Fetch.AI Limited
#
# 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.
#
# ------------------------------------------------------------------------------
"""This module contains an example of skill for an AEA."""
from aea.configurations.base import PublicId
PUBLIC_ID = PublicId.from_str("fetchai/gym:0.13.0")
| packages/fetchai/skills/gym/__init__.py | 955 | This module contains an example of skill for an AEA.
-*- coding: utf-8 -*- ------------------------------------------------------------------------------ Copyright 2018-2019 Fetch.AI Limited 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. ------------------------------------------------------------------------------ | 813 | en | 0.729122 |
from vyxal.parse import parse
from vyxal.transpile import transpile
def test_if():
# TODO(user/cgccuser) try with more branches
vy = """[ 1 | 2 ]"""
py = transpile(vy)
expected = """condition = pop(stack, 1, ctx=ctx)
if boolify(condition, ctx):
stack.append(1)
else:
stack.append(2)
"""
assert py == expected
| tests/test_transpiler.py | 339 | TODO(user/cgccuser) try with more branches | 42 | en | 0.854445 |
# 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
from oslo_config import fixture as config_fixture
from keystone.common import provider_api
from keystone.credential.providers import fernet as credential_provider
from keystone.tests import unit
from keystone.tests.unit import default_fixtures
from keystone.tests.unit import ksfixtures
from keystone.tests.unit.ksfixtures import database
from keystone.credential.backends import sql as credential_sql
from keystone import exception
PROVIDERS = provider_api.ProviderAPIs
class SqlTests(unit.SQLDriverOverrides, unit.TestCase):
def setUp(self):
super(SqlTests, self).setUp()
self.useFixture(database.Database())
self.load_backends()
# populate the engine with tables & fixtures
self.load_fixtures(default_fixtures)
# defaulted by the data load
self.user_foo['enabled'] = True
def config_files(self):
config_files = super(SqlTests, self).config_files()
config_files.append(unit.dirs.tests_conf('backend_sql.conf'))
return config_files
class SqlCredential(SqlTests):
def _create_credential_with_user_id(self, user_id=None):
if not user_id:
user_id = uuid.uuid4().hex
credential = unit.new_credential_ref(user_id=user_id,
extra=uuid.uuid4().hex,
type=uuid.uuid4().hex)
PROVIDERS.credential_api.create_credential(
credential['id'], credential
)
return credential
def _validate_credential_list(self, retrieved_credentials,
expected_credentials):
self.assertEqual(len(expected_credentials), len(retrieved_credentials))
retrieved_ids = [c['id'] for c in retrieved_credentials]
for cred in expected_credentials:
self.assertIn(cred['id'], retrieved_ids)
def setUp(self):
super(SqlCredential, self).setUp()
self.useFixture(
ksfixtures.KeyRepository(
self.config_fixture,
'credential',
credential_provider.MAX_ACTIVE_KEYS
)
)
self.credentials = []
self.user_credentials = []
# setup 3 credentials with random user ids
for _ in range(3):
cred = self._create_credential_with_user_id()
self.user_credentials.append(cred)
self.credentials.append(cred)
# setup 3 credentials with specific user ids
for _ in range(3):
cred = self._create_credential_with_user_id(self.user_foo['id'])
self.user_credentials.append(cred)
self.credentials.append(cred)
def test_backend_credential_sql_hints_none(self):
credentials = PROVIDERS.credential_api.list_credentials(hints=None)
self._validate_credential_list(credentials, self.user_credentials)
def test_backend_credential_sql_no_hints(self):
credentials = PROVIDERS.credential_api.list_credentials()
self._validate_credential_list(credentials, self.user_credentials)
def test_backend_credential_sql_encrypted_string(self):
cred_dict = {
'id': uuid.uuid4().hex,
'type': uuid.uuid4().hex,
'hash': uuid.uuid4().hex,
'encrypted_blob': b'randomdata'
}
ref = credential_sql.CredentialModel.from_dict(cred_dict)
# Make sure CredentialModel is handing over a text string
# to the database. To avoid encoding issues
self.assertIsInstance(ref.encrypted_blob, str)
def test_credential_limits(self):
config_fixture_ = self.user = self.useFixture(config_fixture.Config())
config_fixture_.config(group='credential', user_limit=4)
self._create_credential_with_user_id(self.user_foo['id'])
self.assertRaises(exception.CredentialLimitExceeded,
self._create_credential_with_user_id,
self.user_foo['id'])
| keystone/tests/unit/credential/test_backend_sql.py | 4,550 | 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. populate the engine with tables & fixtures defaulted by the data load setup 3 credentials with random user ids setup 3 credentials with specific user ids Make sure CredentialModel is handing over a text string to the database. To avoid encoding issues | 774 | en | 0.869768 |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Twinboundary plot
This module provide various kinds of plot related to twin boudnary.
"""
import numpy as np
from copy import deepcopy
from twinpy.plot.base import line_chart
def plot_plane(ax,
distances:list,
z_coords:list,
label:str=None,
decorate:bool=True,
show_half:bool=False,
**kwargs):
"""
Plot plane.
Args:
ax: matplotlib ax.
distances (list): List of plane intervals.
z_coords (list): List of z coordinate of each plane.
label (str): Plot label.
decorate (bool): If True, ax is decorated.
show_half: If True, atom planes which are periodically equivalent are
not showed.
"""
if decorate:
xlabel = 'Distance'
ylabel = 'Hight'
else:
xlabel = ylabel = None
_distances = deepcopy(distances)
_z_coords = deepcopy(z_coords)
_distances.insert(0, distances[-1])
_distances.append(distances[0])
_z_coords.insert(0, -distances[-1])
_z_coords.append(z_coords[-1]+distances[0])
c = np.sum(distances)
fixed_z_coords = _z_coords + distances[0] / 2 - c / 2
num = len(fixed_z_coords)
bulk_distance = _distances[int(num/4)]
if show_half:
n = int((num + 2) / 4)
_distances = _distances[n:3*n]
fixed_z_coords = fixed_z_coords[n:3*n]
line_chart(ax=ax,
xdata=_distances,
ydata=fixed_z_coords,
xlabel=xlabel,
ylabel=ylabel,
label=label,
sort_by='y',
**kwargs)
if decorate:
xmin = bulk_distance - 0.025
xmax = bulk_distance + 0.025
if show_half:
ax.hlines(0,
xmin=xmin-0.01,
xmax=xmax+0.01,
linestyle='--',
color='k',
linewidth=1.)
else:
tb_idx = [1, int(num/2), num-1]
for idx in tb_idx:
ax.hlines(fixed_z_coords[idx]-distances[0]/2,
xmin=xmin-0.01,
xmax=xmax+0.01,
linestyle='--',
color='k',
linewidth=1.)
def plot_angle(ax,
angles:list,
z_coords:list,
label:str=None,
decorate:bool=True):
"""
Plot angle.
Args:
ax: matplotlib ax.
z_coords (list): List of z coordinate of each plane.
label (str): Plot label.
decorate (bool): If True, ax is decorated.
"""
if decorate:
xlabel = 'Angle'
ylabel = 'Hight'
else:
xlabel = ylabel = None
_angles = deepcopy(angles)
_z_coords = deepcopy(z_coords)
_angles.append(angles[0])
_z_coords.append(z_coords[-1]+z_coords[1])
line_chart(ax=ax,
xdata=_angles,
ydata=_z_coords,
xlabel=xlabel,
ylabel=ylabel,
label=label,
sort_by='y')
if decorate:
num = len(_z_coords)
tb_idx = [0, int(num/2), num-1]
bulk_angle = angles[int(num/4)]
for idx in tb_idx:
ax.hlines(_z_coords[idx],
xmin=-1,
xmax=bulk_angle+2,
linestyle='--',
linewidth=1.5)
def plot_pair_distance(ax,
pair_distances:list,
z_coords:list,
label:str=None,
decorate:bool=True):
"""
Plot angle.
Args:
ax: matplotlib ax.
pair_distances (list): List of A-B pair distances, which is originally
primitive pair in HCP structure.
z_coords (list): List of z coordinate of each plane.
label (str): Plot label.
decorate (bool): If True, ax is decorated.
"""
if decorate:
xlabel = 'Pair Distance'
ylabel = 'Hight'
else:
xlabel = ylabel = None
_pair_distances = deepcopy(pair_distances)
_z_coords = deepcopy(z_coords)
_pair_distances.append(pair_distances[0])
_z_coords.append(z_coords[-1]+z_coords[1])
line_chart(ax=ax,
xdata=_pair_distances,
ydata=_z_coords,
xlabel=xlabel,
ylabel=ylabel,
label=label,
sort_by='y')
if decorate:
num = len(_z_coords)
tb_idx = [0, int(num/2), num-1]
bulk_pair_distance = pair_distances[int(num/4)]
for idx in tb_idx:
ax.hlines(_z_coords[idx],
xmin=-1,
xmax=bulk_pair_distance+2,
linestyle='--',
linewidth=1.5)
| twinpy/plot/twinboundary.py | 4,934 | Plot angle.
Args:
ax: matplotlib ax.
z_coords (list): List of z coordinate of each plane.
label (str): Plot label.
decorate (bool): If True, ax is decorated.
Plot angle.
Args:
ax: matplotlib ax.
pair_distances (list): List of A-B pair distances, which is originally
primitive pair in HCP structure.
z_coords (list): List of z coordinate of each plane.
label (str): Plot label.
decorate (bool): If True, ax is decorated.
Plot plane.
Args:
ax: matplotlib ax.
distances (list): List of plane intervals.
z_coords (list): List of z coordinate of each plane.
label (str): Plot label.
decorate (bool): If True, ax is decorated.
show_half: If True, atom planes which are periodically equivalent are
not showed.
Twinboundary plot
This module provide various kinds of plot related to twin boudnary.
!/usr/bin/env python -*- coding: utf-8 -*- | 938 | en | 0.679806 |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tools
import db
import time
import re
import json
from plugins import base
# from plugins import lista
from plugins import listb
from plugins import dotpy
class Iptv (object):
def __init__ (self) :
self.T = tools.Tools()
self.DB = db.DataBase()
def run(self) :
Base = base.Source()
Base.getSource()
Dotpy = dotpy.Source()
Dotpy.getSource()
listB = listb.Source()
listB.getSource()
# # listA = lista.Source()
# # urlList = listA.getSource()
# # for item in urlList :
# # self.addData(item)
self.outPut()
self.outJson()
print("DONE!!")
def outPut (self) :
sql = """SELECT * FROM
(SELECT * FROM %s WHERE online = 1 ORDER BY delay DESC) AS delay
GROUP BY LOWER(delay.title)
HAVING delay.title != '' and delay.title != 'CCTV-' AND delay.delay < 500
ORDER BY level ASC, length(title) ASC, title ASC
""" % (self.DB.table)
result = self.DB.query(sql)
with open('tv.m3u8', 'w') as f:
f.write("#EXTM3U\n")
for item in result :
className = '其他频道'
if item[4] == 1 :
className = '中央频道'
elif item[4] == 2 :
className = '地方频道'
elif item[4] == 3 :
className = '地方频道'
elif item[4] == 7 :
className = '广播频道'
else :
className = '其他频道'
f.write("#EXTINF:-1, group-title=\"%s\", %s\n" % (className, item[1]))
f.write("%s\n" % (item[3]))
def outJson (self) :
sql = """SELECT * FROM
(SELECT * FROM %s WHERE online = 1 ORDER BY delay DESC) AS delay
GROUP BY LOWER(delay.title)
HAVING delay.title != '' and delay.title != 'CCTV-' AND delay.delay < 500
ORDER BY level ASC, length(title) ASC, title ASC
""" % (self.DB.table)
result = self.DB.query(sql)
fmtList = {
'cctv': [],
'local': [],
'other': [],
'radio': []
}
for item in result :
tmp = {
'title': item[1],
'url': item[3]
}
if item[4] == 1 :
fmtList['cctv'].append(tmp)
elif item[4] == 2 :
fmtList['local'].append(tmp)
elif item[4] == 3 :
fmtList['local'].append(tmp)
elif item[4] == 7 :
fmtList['radio'].append(tmp)
else :
fmtList['other'].append(tmp)
jsonStr = json.dumps(fmtList)
with open('tv.json', 'w') as f:
f.write(jsonStr)
if __name__ == '__main__':
obj = Iptv()
obj.run()
| main.py | 2,991 | !/usr/bin/env python -*- coding: utf-8 -*- from plugins import lista listA = lista.Source() urlList = listA.getSource() for item in urlList : self.addData(item) | 168 | en | 0.215678 |
"""
##################################################################################################
# Copyright Info : Copyright (c) Davar Lab @ Hikvision Research Institute. All rights reserved.
# Filename : test.py
# Abstract : The common testing api for video text recognition, track, quality score
# Current Version: 1.0.0
# Date : 2021-06-02
##################################################################################################
"""
import numpy as np
import mmcv
import torch
def single_gpu_test(model,
data_loader):
""" Test model with single GPU, used for visualization.
Args:
model (nn.Module): Model to be tested.
data_loader (nn.Dataloader): Pytorch data loader.
Returns:
dict: test results
"""
model.eval()
results = dict()
results['texts'] = []
results['img_info'] = []
results['glimpses'] = []
results['scores'] = []
dataset = data_loader.dataset
prog_bar = mmcv.ProgressBar(len(dataset))
for i, data in enumerate(data_loader):
with torch.no_grad():
result = model(return_loss=False, rescale=True, **data)
texts = result['text']
glimpses = result['glimpses']
glimpses = glimpses.cpu().numpy()
img_infos = result['img_info']
scores = result['scores']
scores = scores.cpu().numpy()
scores = scores.reshape(-1)
batch_size = len(texts)
results['texts'].extend(texts)
results['img_info'].extend(img_infos)
results['glimpses'].extend(glimpses)
results['scores'].extend(scores)
for _ in range(batch_size):
prog_bar.update()
new_glimpse = np.stack(results['glimpses'])
results['glimpses'] = new_glimpse
return results
| davarocr/davarocr/davar_videotext/apis/test.py | 1,827 | Test model with single GPU, used for visualization.
Args:
model (nn.Module): Model to be tested.
data_loader (nn.Dataloader): Pytorch data loader.
Returns:
dict: test results
##################################################################################################
# Copyright Info : Copyright (c) Davar Lab @ Hikvision Research Institute. All rights reserved.
# Filename : test.py
# Abstract : The common testing api for video text recognition, track, quality score
# Current Version: 1.0.0
# Date : 2021-06-02
################################################################################################## | 671 | de | 0.297824 |
# -*- coding: utf-8 -*-
"""
pyQode is a source code editor widget for PyQt5
pyQode is a **namespace package**.
"""
import pkg_resources
pkg_resources.declare_namespace(__name__)
| pyqode/__init__.py | 179 | pyQode is a source code editor widget for PyQt5
pyQode is a **namespace package**.
-*- coding: utf-8 -*- | 107 | en | 0.707883 |
#!/usr/bin/env python3
# -*- coding: utf-8 -*- #
# This file is only used if you use `make publish` or
# explicitly specify it as your config file.
import os
import sys
sys.path.append(os.curdir)
from pelicanconf import *
# If your site is available via HTTPS, make sure SITEURL begins with https://
SITEURL = ''
RELATIVE_URLS = False
FEED_ALL_ATOM = 'atom.xml'
#CATEGORY_FEED_ATOM = 'feeds/{slug}.atom.xml'
DELETE_OUTPUT_DIRECTORY = True
# Following items are often useful when publishing
#DISQUS_SITENAME = ""
#GOOGLE_ANALYTICS = "" | publishconf.py | 541 | !/usr/bin/env python3 -*- coding: utf-8 -*- This file is only used if you use `make publish` or explicitly specify it as your config file. If your site is available via HTTPS, make sure SITEURL begins with https://CATEGORY_FEED_ATOM = 'feeds/{slug}.atom.xml' Following items are often useful when publishingDISQUS_SITENAME = ""GOOGLE_ANALYTICS = "" | 349 | en | 0.794865 |
from pyspedas import tnames
from pytplot import get_data, store_data, options
def mms_load_fpi_calc_pad(probe='1', level='sitl', datatype='', data_rate='', suffix='', autoscale=True):
"""
Calculates the omni-directional pitch angle distribution (summed and averaged)
from the individual tplot variables
Parameters:
probe: str
probe, valid values for MMS probes are ['1','2','3','4'].
level: str
indicates level of data processing. the default if no level is specified is 'sitl'
datatype: str
Valid datatypes for FPI are:
Quicklook: ['des', 'dis']
SITL: '' (none; loads both electron and ion data from single CDF)
L1b/L2: ['des-dist', 'dis-dist', 'dis-moms', 'des-moms']
data_rate: str
instrument data rates for FPI include 'brst' and 'fast'. The
default is 'fast'.
suffix: str
The tplot variable names will be given this suffix. By default,
no suffix is added.
autoscale: bool
If set, use the default zrange; otherwise, use the min and max of the data for the zrange
Returns:
List of tplot variables created.
"""
out_vars = []
if isinstance(datatype, str):
if datatype == '*' or datatype == '':
if level.lower() == 'ql':
datatype = ['des', 'dis']
else:
datatype = ['des-dist', 'dis-dist']
if isinstance(datatype, str):
datatype = [datatype]
for dtype in datatype:
species = dtype[1]
if level.lower() == 'sitl':
spec_str_format = 'PitchAngDist'
obs_str_format = '_fpi_' + species
else:
spec_str_format = 'pitchAngDist'
obs_str_format = '_d' + species + 's_'
obsstr = 'mms' + str(probe) + obs_str_format
if level.lower() == 'l2':
spec_str_format = 'pitchangdist'
pad_vars = [obsstr+spec_str_format+'_'+erange+'en_'+data_rate+suffix for erange in ['low', 'mid', 'high']]
else:
pad_vars = [obsstr+spec_str_format+'_'+erange+'En'+suffix for erange in ['low', 'mid', 'high']]
pad_avg_name = obsstr+'PitchAngDist_avg'+suffix
low_en = get_data(pad_vars[0])
mid_en = get_data(pad_vars[1])
high_en = get_data(pad_vars[2])
if low_en is None or mid_en is None or high_en is None:
v3_low_pad = tnames(pad_vars[0].lower()+'_'+data_rate)
v3_mid_pad = tnames(pad_vars[1].lower()+'_'+data_rate)
v3_high_pad = tnames(pad_vars[2].lower()+'_'+data_rate)
if v3_low_pad == [] or v3_mid_pad == [] or v3_high_pad == []:
continue
low_en = get_data(v3_low_pad[0])
mid_en = get_data(v3_mid_pad[0])
high_en = get_data(v3_high_pad[0])
pad_avg_name = pad_avg_name.lower()
e_pad_sum = low_en.y+mid_en.y+high_en.y
e_pad_avg = e_pad_sum/3.0
if level == 'l2':
pad_avg_name = pad_avg_name.lower()
if species == 'e':
species_str = 'electron'
elif species == 'i':
species_str = 'ion'
if level == 'ql':
store_data(obsstr+'PitchAngDist_sum'+suffix, data={'x': low_en.times, 'y': e_pad_sum, 'v': low_en.v})
options(obsstr+'PitchAngDist_sum'+suffix, 'ytitle', 'MMS'+str(probe)+' \\ '+species_str+' \\ PAD \\ SUM')
options(obsstr+'PitchAngDist_sum'+suffix, 'yrange', [0, 180])
options(obsstr+'PitchAngDist_sum'+suffix, 'zlog', True)
options(obsstr+'PitchAngDist_sum'+suffix, 'spec', True)
options(obsstr+'PitchAngDist_sum'+suffix, 'Colormap', 'jet')
out_vars.append(obsstr+'PitchAngDist_sum'+suffix)
store_data(pad_avg_name, data={'x': low_en.times, 'y': e_pad_avg, 'v': low_en.v})
options(pad_avg_name, 'ztitle', 'eV/(cm!U2!N s sr eV)')
options(pad_avg_name, 'ytitle', 'MMS'+str(probe)+' \\ '+species_str+' \\ PAD \\ AVG')
options(pad_avg_name, 'yrange', [0, 180])
options(pad_avg_name, 'zlog', True)
options(pad_avg_name, 'spec', True)
options(pad_avg_name, 'Colormap', 'jet')
out_vars.append(pad_avg_name)
return out_vars | pyspedas/mms/fpi/mms_load_fpi_calc_pad.py | 4,346 | Calculates the omni-directional pitch angle distribution (summed and averaged)
from the individual tplot variables
Parameters:
probe: str
probe, valid values for MMS probes are ['1','2','3','4'].
level: str
indicates level of data processing. the default if no level is specified is 'sitl'
datatype: str
Valid datatypes for FPI are:
Quicklook: ['des', 'dis']
SITL: '' (none; loads both electron and ion data from single CDF)
L1b/L2: ['des-dist', 'dis-dist', 'dis-moms', 'des-moms']
data_rate: str
instrument data rates for FPI include 'brst' and 'fast'. The
default is 'fast'.
suffix: str
The tplot variable names will be given this suffix. By default,
no suffix is added.
autoscale: bool
If set, use the default zrange; otherwise, use the min and max of the data for the zrange
Returns:
List of tplot variables created. | 954 | en | 0.547641 |
# -*- coding: utf-8 -*-
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# google-cloud-dataproc documentation build configuration file
#
# This file is execfile()d with the current directory set to its
# containing dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
#
# All configuration values have a default; values that are commented out
# serve to show the default.
import sys
import os
import shlex
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
sys.path.insert(0, os.path.abspath(".."))
# For plugins that can not read conf.py.
# See also: https://github.com/docascode/sphinx-docfx-yaml/issues/85
sys.path.insert(0, os.path.abspath("."))
__version__ = ""
# -- General configuration ------------------------------------------------
# If your documentation needs a minimal Sphinx version, state it here.
needs_sphinx = "1.5.5"
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
"sphinx.ext.autodoc",
"sphinx.ext.autosummary",
"sphinx.ext.intersphinx",
"sphinx.ext.coverage",
"sphinx.ext.doctest",
"sphinx.ext.napoleon",
"sphinx.ext.todo",
"sphinx.ext.viewcode",
"recommonmark",
]
# autodoc/autosummary flags
autoclass_content = "both"
autodoc_default_options = {"members": True}
autosummary_generate = True
# Add any paths that contain templates here, relative to this directory.
templates_path = ["_templates"]
# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
# source_suffix = ['.rst', '.md']
source_suffix = [".rst", ".md"]
# The encoding of source files.
# source_encoding = 'utf-8-sig'
# The root toctree document.
root_doc = "index"
# General information about the project.
project = "google-cloud-dataproc"
copyright = "2019, Google"
author = "Google APIs"
# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The full version, including alpha/beta/rc tags.
release = __version__
# The short X.Y version.
version = ".".join(release.split(".")[0:2])
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = None
# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
# today = ''
# Else, today_fmt is used as the format for a strftime call.
# today_fmt = '%B %d, %Y'
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
exclude_patterns = [
"_build",
"**/.nox/**/*",
"samples/AUTHORING_GUIDE.md",
"samples/CONTRIBUTING.md",
"samples/snippets/README.rst",
]
# The reST default role (used for this markup: `text`) to use for all
# documents.
# default_role = None
# If true, '()' will be appended to :func: etc. cross-reference text.
# add_function_parentheses = True
# If true, the current module name will be prepended to all description
# unit titles (such as .. function::).
# add_module_names = True
# If true, sectionauthor and moduleauthor directives will be shown in the
# output. They are ignored by default.
# show_authors = False
# The name of the Pygments (syntax highlighting) style to use.
pygments_style = "sphinx"
# A list of ignored prefixes for module index sorting.
# modindex_common_prefix = []
# If true, keep warnings as "system message" paragraphs in the built documents.
# keep_warnings = False
# If true, `todo` and `todoList` produce output, else they produce nothing.
todo_include_todos = True
# -- Options for HTML output ----------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
html_theme = "alabaster"
# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
# documentation.
html_theme_options = {
"description": "Google Cloud Client Libraries for google-cloud-dataproc",
"github_user": "googleapis",
"github_repo": "python-dataproc",
"github_banner": True,
"font_family": "'Roboto', Georgia, sans",
"head_font_family": "'Roboto', Georgia, serif",
"code_font_family": "'Roboto Mono', 'Consolas', monospace",
}
# Add any paths that contain custom themes here, relative to this directory.
# html_theme_path = []
# The name for this set of Sphinx documents. If None, it defaults to
# "<project> v<release> documentation".
# html_title = None
# A shorter title for the navigation bar. Default is the same as html_title.
# html_short_title = None
# The name of an image file (relative to this directory) to place at the top
# of the sidebar.
# html_logo = None
# The name of an image file (within the static path) to use as favicon of the
# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
# html_favicon = None
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ["_static"]
# Add any extra paths that contain custom files (such as robots.txt or
# .htaccess) here, relative to this directory. These files are copied
# directly to the root of the documentation.
# html_extra_path = []
# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
# using the given strftime format.
# html_last_updated_fmt = '%b %d, %Y'
# If true, SmartyPants will be used to convert quotes and dashes to
# typographically correct entities.
# html_use_smartypants = True
# Custom sidebar templates, maps document names to template names.
# html_sidebars = {}
# Additional templates that should be rendered to pages, maps page names to
# template names.
# html_additional_pages = {}
# If false, no module index is generated.
# html_domain_indices = True
# If false, no index is generated.
# html_use_index = True
# If true, the index is split into individual pages for each letter.
# html_split_index = False
# If true, links to the reST sources are added to the pages.
# html_show_sourcelink = True
# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
# html_show_sphinx = True
# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
# html_show_copyright = True
# If true, an OpenSearch description file will be output, and all pages will
# contain a <link> tag referring to it. The value of this option must be the
# base URL from which the finished HTML is served.
# html_use_opensearch = ''
# This is the file name suffix for HTML files (e.g. ".xhtml").
# html_file_suffix = None
# Language to be used for generating the HTML full-text search index.
# Sphinx supports the following languages:
# 'da', 'de', 'en', 'es', 'fi', 'fr', 'hu', 'it', 'ja'
# 'nl', 'no', 'pt', 'ro', 'ru', 'sv', 'tr'
# html_search_language = 'en'
# A dictionary with options for the search language support, empty by default.
# Now only 'ja' uses this config value
# html_search_options = {'type': 'default'}
# The name of a javascript file (relative to the configuration directory) that
# implements a search results scorer. If empty, the default will be used.
# html_search_scorer = 'scorer.js'
# Output file base name for HTML help builder.
htmlhelp_basename = "google-cloud-dataproc-doc"
# -- Options for warnings ------------------------------------------------------
suppress_warnings = [
# Temporarily suppress this to avoid "more than one target found for
# cross-reference" warning, which are intractable for us to avoid while in
# a mono-repo.
# See https://github.com/sphinx-doc/sphinx/blob
# /2a65ffeef5c107c19084fabdd706cdff3f52d93c/sphinx/domains/python.py#L843
"ref.python"
]
# -- Options for LaTeX output ---------------------------------------------
latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
#'papersize': 'letterpaper',
# The font size ('10pt', '11pt' or '12pt').
#'pointsize': '10pt',
# Additional stuff for the LaTeX preamble.
#'preamble': '',
# Latex figure (float) alignment
#'figure_align': 'htbp',
}
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
(
root_doc,
"google-cloud-dataproc.tex",
"google-cloud-dataproc Documentation",
author,
"manual",
)
]
# The name of an image file (relative to this directory) to place at the top of
# the title page.
# latex_logo = None
# For "manual" documents, if this is true, then toplevel headings are parts,
# not chapters.
# latex_use_parts = False
# If true, show page references after internal links.
# latex_show_pagerefs = False
# If true, show URL addresses after external links.
# latex_show_urls = False
# Documents to append as an appendix to all manuals.
# latex_appendices = []
# If false, no module index is generated.
# latex_domain_indices = True
# -- Options for manual page output ---------------------------------------
# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
(
root_doc,
"google-cloud-dataproc",
"google-cloud-dataproc Documentation",
[author],
1,
)
]
# If true, show URL addresses after external links.
# man_show_urls = False
# -- Options for Texinfo output -------------------------------------------
# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
(
root_doc,
"google-cloud-dataproc",
"google-cloud-dataproc Documentation",
author,
"google-cloud-dataproc",
"google-cloud-dataproc Library",
"APIs",
)
]
# Documents to append as an appendix to all manuals.
# texinfo_appendices = []
# If false, no module index is generated.
# texinfo_domain_indices = True
# How to display URL addresses: 'footnote', 'no', or 'inline'.
# texinfo_show_urls = 'footnote'
# If true, do not generate a @detailmenu in the "Top" node's menu.
# texinfo_no_detailmenu = False
# Example configuration for intersphinx: refer to the Python standard library.
intersphinx_mapping = {
"python": ("https://python.readthedocs.org/en/latest/", None),
"google-auth": ("https://googleapis.dev/python/google-auth/latest/", None),
"google.api_core": ("https://googleapis.dev/python/google-api-core/latest/", None,),
"grpc": ("https://grpc.github.io/grpc/python/", None),
"proto-plus": ("https://proto-plus-python.readthedocs.io/en/latest/", None),
"protobuf": ("https://googleapis.dev/python/protobuf/latest/", None),
}
# Napoleon settings
napoleon_google_docstring = True
napoleon_numpy_docstring = True
napoleon_include_private_with_doc = False
napoleon_include_special_with_doc = True
napoleon_use_admonition_for_examples = False
napoleon_use_admonition_for_notes = False
napoleon_use_admonition_for_references = False
napoleon_use_ivar = False
napoleon_use_param = True
napoleon_use_rtype = True
| docs/conf.py | 12,391 | -*- coding: utf-8 -*- Copyright 2021 Google LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. google-cloud-dataproc documentation build configuration file This file is execfile()d with the current directory set to its containing dir. Note that not all possible configuration values are present in this autogenerated file. All configuration values have a default; values that are commented out serve to show the default. If extensions (or modules to document with autodoc) are in another directory, add these directories to sys.path here. If the directory is relative to the documentation root, use os.path.abspath to make it absolute, like shown here. For plugins that can not read conf.py. See also: https://github.com/docascode/sphinx-docfx-yaml/issues/85 -- General configuration ------------------------------------------------ If your documentation needs a minimal Sphinx version, state it here. Add any Sphinx extension module names here, as strings. They can be extensions coming with Sphinx (named 'sphinx.ext.*') or your custom ones. autodoc/autosummary flags Add any paths that contain templates here, relative to this directory. The suffix(es) of source filenames. You can specify multiple suffix as a list of string: source_suffix = ['.rst', '.md'] The encoding of source files. source_encoding = 'utf-8-sig' The root toctree document. General information about the project. The version info for the project you're documenting, acts as replacement for |version| and |release|, also used in various other places throughout the built documents. The full version, including alpha/beta/rc tags. The short X.Y version. The language for content autogenerated by Sphinx. Refer to documentation for a list of supported languages. This is also used if you do content translation via gettext catalogs. Usually you set "language" from the command line for these cases. There are two options for replacing |today|: either, you set today to some non-false value, then it is used: today = '' Else, today_fmt is used as the format for a strftime call. today_fmt = '%B %d, %Y' List of patterns, relative to source directory, that match files and directories to ignore when looking for source files. The reST default role (used for this markup: `text`) to use for all documents. default_role = None If true, '()' will be appended to :func: etc. cross-reference text. add_function_parentheses = True If true, the current module name will be prepended to all description unit titles (such as .. function::). add_module_names = True If true, sectionauthor and moduleauthor directives will be shown in the output. They are ignored by default. show_authors = False The name of the Pygments (syntax highlighting) style to use. A list of ignored prefixes for module index sorting. modindex_common_prefix = [] If true, keep warnings as "system message" paragraphs in the built documents. keep_warnings = False If true, `todo` and `todoList` produce output, else they produce nothing. -- Options for HTML output ---------------------------------------------- The theme to use for HTML and HTML Help pages. See the documentation for a list of builtin themes. Theme options are theme-specific and customize the look and feel of a theme further. For a list of options available for each theme, see the documentation. Add any paths that contain custom themes here, relative to this directory. html_theme_path = [] The name for this set of Sphinx documents. If None, it defaults to "<project> v<release> documentation". html_title = None A shorter title for the navigation bar. Default is the same as html_title. html_short_title = None The name of an image file (relative to this directory) to place at the top of the sidebar. html_logo = None The name of an image file (within the static path) to use as favicon of the docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 pixels large. html_favicon = None Add any paths that contain custom static files (such as style sheets) here, relative to this directory. They are copied after the builtin static files, so a file named "default.css" will overwrite the builtin "default.css". Add any extra paths that contain custom files (such as robots.txt or .htaccess) here, relative to this directory. These files are copied directly to the root of the documentation. html_extra_path = [] If not '', a 'Last updated on:' timestamp is inserted at every page bottom, using the given strftime format. html_last_updated_fmt = '%b %d, %Y' If true, SmartyPants will be used to convert quotes and dashes to typographically correct entities. html_use_smartypants = True Custom sidebar templates, maps document names to template names. html_sidebars = {} Additional templates that should be rendered to pages, maps page names to template names. html_additional_pages = {} If false, no module index is generated. html_domain_indices = True If false, no index is generated. html_use_index = True If true, the index is split into individual pages for each letter. html_split_index = False If true, links to the reST sources are added to the pages. html_show_sourcelink = True If true, "Created using Sphinx" is shown in the HTML footer. Default is True. html_show_sphinx = True If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. html_show_copyright = True If true, an OpenSearch description file will be output, and all pages will contain a <link> tag referring to it. The value of this option must be the base URL from which the finished HTML is served. html_use_opensearch = '' This is the file name suffix for HTML files (e.g. ".xhtml"). html_file_suffix = None Language to be used for generating the HTML full-text search index. Sphinx supports the following languages: 'da', 'de', 'en', 'es', 'fi', 'fr', 'hu', 'it', 'ja' 'nl', 'no', 'pt', 'ro', 'ru', 'sv', 'tr' html_search_language = 'en' A dictionary with options for the search language support, empty by default. Now only 'ja' uses this config value html_search_options = {'type': 'default'} The name of a javascript file (relative to the configuration directory) that implements a search results scorer. If empty, the default will be used. html_search_scorer = 'scorer.js' Output file base name for HTML help builder. -- Options for warnings ------------------------------------------------------ Temporarily suppress this to avoid "more than one target found for cross-reference" warning, which are intractable for us to avoid while in a mono-repo. See https://github.com/sphinx-doc/sphinx/blob /2a65ffeef5c107c19084fabdd706cdff3f52d93c/sphinx/domains/python.pyL843 -- Options for LaTeX output --------------------------------------------- The paper size ('letterpaper' or 'a4paper').'papersize': 'letterpaper', The font size ('10pt', '11pt' or '12pt').'pointsize': '10pt', Additional stuff for the LaTeX preamble.'preamble': '', Latex figure (float) alignment'figure_align': 'htbp', Grouping the document tree into LaTeX files. List of tuples (source start file, target name, title, author, documentclass [howto, manual, or own class]). The name of an image file (relative to this directory) to place at the top of the title page. latex_logo = None For "manual" documents, if this is true, then toplevel headings are parts, not chapters. latex_use_parts = False If true, show page references after internal links. latex_show_pagerefs = False If true, show URL addresses after external links. latex_show_urls = False Documents to append as an appendix to all manuals. latex_appendices = [] If false, no module index is generated. latex_domain_indices = True -- Options for manual page output --------------------------------------- One entry per manual page. List of tuples (source start file, name, description, authors, manual section). If true, show URL addresses after external links. man_show_urls = False -- Options for Texinfo output ------------------------------------------- Grouping the document tree into Texinfo files. List of tuples (source start file, target name, title, author, dir menu entry, description, category) Documents to append as an appendix to all manuals. texinfo_appendices = [] If false, no module index is generated. texinfo_domain_indices = True How to display URL addresses: 'footnote', 'no', or 'inline'. texinfo_show_urls = 'footnote' If true, do not generate a @detailmenu in the "Top" node's menu. texinfo_no_detailmenu = False Example configuration for intersphinx: refer to the Python standard library. Napoleon settings | 8,964 | en | 0.683118 |
#!/usr/bin/env python
# Copyright (c) 2019 The Zcash developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or https://www.opensource.org/licenses/mit-license.php.
import sys; assert sys.version_info < (3,), ur"This script does not run under Python 3. Please use Python 2.7.x."
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
assert_equal, assert_true,
get_coinbase_address,
start_nodes, stop_nodes,
initialize_chain_clean, connect_nodes_bi, wait_bitcoinds,
wait_and_assert_operationid_status
)
from decimal import Decimal
class WalletPersistenceTest (BitcoinTestFramework):
def setup_chain(self):
print("Initializing test directory " + self.options.tmpdir)
initialize_chain_clean(self.options.tmpdir, 3)
def setup_network(self, split=False):
self.nodes = start_nodes(3, self.options.tmpdir,
extra_args=[[
'-nuparams=5ba81b19:100', # Overwinter
'-nuparams=76b809bb:201', # Sapling
]] * 3)
connect_nodes_bi(self.nodes,0,1)
connect_nodes_bi(self.nodes,1,2)
self.is_network_split=False
self.sync_all()
def run_test(self):
# Sanity-check the test harness
self.nodes[0].generate(200)
assert_equal(self.nodes[0].getblockcount(), 200)
self.sync_all()
# Verify Sapling address is persisted in wallet (even when Sapling is not yet active)
sapling_addr = self.nodes[0].z_getnewaddress('sapling')
# Make sure the node has the addresss
addresses = self.nodes[0].z_listaddresses()
assert_true(sapling_addr in addresses, "Should contain address before restart")
# Restart the nodes
stop_nodes(self.nodes)
wait_bitcoinds()
self.setup_network()
# Make sure we still have the address after restarting
addresses = self.nodes[0].z_listaddresses()
assert_true(sapling_addr in addresses, "Should contain address after restart")
# Activate Sapling
self.nodes[0].generate(1)
self.sync_all()
# Node 0 shields funds to Sapling address
taddr0 = get_coinbase_address(self.nodes[0])
recipients = []
recipients.append({"address": sapling_addr, "amount": Decimal('20')})
myopid = self.nodes[0].z_sendmany(taddr0, recipients, 1, 0)
wait_and_assert_operationid_status(self.nodes[0], myopid)
self.sync_all()
self.nodes[0].generate(1)
self.sync_all()
# Verify shielded balance
assert_equal(self.nodes[0].z_getbalance(sapling_addr), Decimal('20'))
# Verify size of shielded pools
pools = self.nodes[0].getblockchaininfo()['valuePools']
assert_equal(pools[0]['chainValue'], Decimal('0')) # Sprout
assert_equal(pools[1]['chainValue'], Decimal('20')) # Sapling
# Restart the nodes
stop_nodes(self.nodes)
wait_bitcoinds()
self.setup_network()
# Verify size of shielded pools
pools = self.nodes[0].getblockchaininfo()['valuePools']
assert_equal(pools[0]['chainValue'], Decimal('0')) # Sprout
assert_equal(pools[1]['chainValue'], Decimal('20')) # Sapling
# Node 0 sends some shielded funds to Node 1
dest_addr = self.nodes[1].z_getnewaddress('sapling')
recipients = []
recipients.append({"address": dest_addr, "amount": Decimal('15')})
myopid = self.nodes[0].z_sendmany(sapling_addr, recipients, 1, 0)
wait_and_assert_operationid_status(self.nodes[0], myopid)
self.sync_all()
self.nodes[0].generate(1)
self.sync_all()
# Verify balances
assert_equal(self.nodes[0].z_getbalance(sapling_addr), Decimal('5'))
assert_equal(self.nodes[1].z_getbalance(dest_addr), Decimal('15'))
# Restart the nodes
stop_nodes(self.nodes)
wait_bitcoinds()
self.setup_network()
# Verify balances
assert_equal(self.nodes[0].z_getbalance(sapling_addr), Decimal('5'))
assert_equal(self.nodes[1].z_getbalance(dest_addr), Decimal('15'))
# Verify importing a spending key will update and persist the nullifiers and witnesses correctly
sk0 = self.nodes[0].z_exportkey(sapling_addr)
self.nodes[2].z_importkey(sk0, "yes")
assert_equal(self.nodes[2].z_getbalance(sapling_addr), Decimal('5'))
# Restart the nodes
stop_nodes(self.nodes)
wait_bitcoinds()
self.setup_network()
# Verify nullifiers persisted correctly by checking balance
# Prior to PR #3590, there will be an error as spent notes are considered unspent:
# Assertion failed: expected: <25.00000000> but was: <5>
assert_equal(self.nodes[2].z_getbalance(sapling_addr), Decimal('5'))
# Verity witnesses persisted correctly by sending shielded funds
recipients = []
recipients.append({"address": dest_addr, "amount": Decimal('1')})
myopid = self.nodes[2].z_sendmany(sapling_addr, recipients, 1, 0)
wait_and_assert_operationid_status(self.nodes[2], myopid)
self.sync_all()
self.nodes[0].generate(1)
self.sync_all()
# Verify balances
assert_equal(self.nodes[2].z_getbalance(sapling_addr), Decimal('4'))
assert_equal(self.nodes[1].z_getbalance(dest_addr), Decimal('16'))
if __name__ == '__main__':
WalletPersistenceTest().main() | qa/rpc-tests/wallet_persistence.py | 5,547 | !/usr/bin/env python Copyright (c) 2019 The Zcash developers Distributed under the MIT software license, see the accompanying file COPYING or https://www.opensource.org/licenses/mit-license.php. Overwinter Sapling Sanity-check the test harness Verify Sapling address is persisted in wallet (even when Sapling is not yet active) Make sure the node has the addresss Restart the nodes Make sure we still have the address after restarting Activate Sapling Node 0 shields funds to Sapling address Verify shielded balance Verify size of shielded pools Sprout Sapling Restart the nodes Verify size of shielded pools Sprout Sapling Node 0 sends some shielded funds to Node 1 Verify balances Restart the nodes Verify balances Verify importing a spending key will update and persist the nullifiers and witnesses correctly Restart the nodes Verify nullifiers persisted correctly by checking balance Prior to PR 3590, there will be an error as spent notes are considered unspent: Assertion failed: expected: <25.00000000> but was: <5> Verity witnesses persisted correctly by sending shielded funds Verify balances | 1,104 | en | 0.803138 |
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as f
from torch.autograd import Variable
import os
import numpy as np
from tqdm import tqdm
def reparameterize(mu, logvar):
eps = Variable(torch.randn(mu.size(0), mu.size(1))).cuda()
z = mu + eps * torch.exp(logvar / 2)
return z
class VAE_MLP_CAT(nn.Module):
def __init__(self, latent_code_num, hidden):
super(VAE_MLP_CAT, self).__init__()
self.encoder = nn.Sequential(
# 1, 124, 32
nn.Conv2d(1, 32, kernel_size=4, stride=2, padding=1),
nn.BatchNorm2d(32),
nn.LeakyReLU(0.2, inplace=True),
# 32, 62, 16
nn.Conv2d(32, 64, kernel_size=4, stride=1, padding=1),
nn.BatchNorm2d(64),
nn.LeakyReLU(0.2, inplace=True),
# 64, 15, 15
nn.Conv2d(64, 128, kernel_size=5, stride=3, padding=1),
nn.BatchNorm2d(128),
nn.LeakyReLU(0.2, inplace=True),
# 128, 20, 5
)
self.fc11 = nn.Linear(128 * 10 * 5 * 2, latent_code_num)
self.fc12 = nn.Linear(128 * 10 * 5 * 2, latent_code_num)
self.mlp = nn.Sequential(
torch.nn.Linear(latent_code_num + 1, hidden),
torch.nn.Tanh(),
torch.nn.Linear(hidden, 1)
)
for p in self.mlp.parameters():
torch.nn.init.normal_(p, mean=0, std=0.1)
torch.nn.init.constant_(self.mlp[0].bias, val=0.)
torch.nn.init.constant_(self.mlp[2].bias, val=0.)
self.fc2 = nn.Linear(latent_code_num, 128 * 10 * 5 * 2)
self.decoder = nn.Sequential(
nn.ConvTranspose2d(128, 64, kernel_size=4, stride=2, padding=1),
nn.ReLU(inplace=True),
nn.ConvTranspose2d(64, 32, kernel_size=4, stride=1, padding=1),
nn.ReLU(inplace=True),
nn.ConvTranspose2d(32, 1, kernel_size=6, stride=3, padding=1),
nn.Sigmoid()
)
def get_reparameterized_code(self, x):
out1, out2 = self.encoder(x), self.encoder(x) # batch_s, 8, 7, 7
mu = self.fc11(out1.view(out1.size(0), -1)) # batch_s, latent
logvar = self.fc12(out2.view(out2.size(0), -1)) # batch_s, latent
z = self.reparameterize(mu, logvar) # batch_s, latent
return z
def forward(self, x, t):
out1, out2 = self.encoder(x), self.encoder(x) # batch_s, 8, 7, 7
mu = self.fc11(out1.view(out1.size(0), -1)) # batch_s, latent
logvar = self.fc12(out2.view(out2.size(0), -1)) # batch_s, latent
pre = self.mlp(torch.cat((t, mu), dim=1))
z = reparameterize(mu, logvar) # batch_s, latent
out3 = self.fc2(z).view(z.size(0), 128, 20, 5) # batch_s, 8, 7, 7
return self.decoder(out3), mu, logvar, pre
def predict(self, x, t):
out1, out2 = self.encoder(x), self.encoder(x) # batch_s, 8, 7, 7
mu = self.fc11(out1.view(out1.size(0), -1)) # batch_s, latent
pre = self.mlp(torch.cat((t, mu), dim=1))
return pre
def get_mid(self, x):
out1, out2 = self.encoder(x), self.encoder(x)
mu = self.fc11(out1.view(out1.size(0), -1))
return mu
def decode(self, z):
out3 = self.fc2(z).view(1, 128, 20, 5)
return self.decoder(out3)
def loss_func(recon_x, x, mu, logvar, pre_, label_):
mse = torch.nn.MSELoss()
binary_cross_entropy = f.binary_cross_entropy(recon_x, x, size_average=False)
k_l_divergence = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
mse_loss = mse(pre_, label_)
return binary_cross_entropy + k_l_divergence + mse_loss
def train_vae_mlp(latent_code_num, hidden, params, device):
print('Totally ' + str(params['VAE_epoch_num']) + ' epochs to train')
os.environ['CUDA_VISIBLE_DEVICES'] = str(device)
thermal_conductivity_train_loader = torch.load('Data/thermal_conductivity_vae_mlp_train_loader.pkl')
heat_capacity_train_loader = torch.load('Data/heat_capacity_vae_mlp_train_loader.pkl')
heat_capacity_vae_mlp = VAE_MLP_CAT(latent_code_num, hidden).cuda()
thermal_conductivity_vae_mlp = VAE_MLP_CAT(latent_code_num, hidden).cuda()
thermal_conductivity_optimizer = optim.Adam(
thermal_conductivity_vae_mlp.parameters(), lr=0.001, betas=(0.9, 0.999), eps=1e-08, weight_decay=0)
heat_capacity_optimizer = optim.Adam(
heat_capacity_vae_mlp.parameters(), lr=0.001, betas=(0.9, 0.999), eps=1e-08, weight_decay=0)
thermal_conductivity_total_loss_list = np.ones(params['VAE_epoch_num'] + 10)
heat_capacity_total_loss_list = np.ones(params['VAE_epoch_num'] + 10)
thermal_conductivity_total_loss_list *= 5000000
heat_capacity_total_loss_list *= 5000000
thermal_conductivity_model_file_name = \
'Model_pkl/VAE_MLP_CAT_thermal_conductivity_latent_' + str(latent_code_num) + \
'_structure_' + str(hidden) + '.pkl'
heat_capacity_model_file_name = \
'Model_pkl/VAE_MLP_CAT_heat_capacity_latent_' + str(latent_code_num) + \
'_structure_' + str(hidden) + '.pkl'
for epoch in range(params['VAE_epoch_num']):
total_loss = 0
thermal_conductivity_vae_mlp.train()
for i, data in enumerate(tqdm(thermal_conductivity_train_loader, 0)):
one_hot = torch.cat((data[0], data[1]), dim=1)
one_hot = one_hot.reshape(one_hot.shape[0], 1, one_hot.shape[1], one_hot.shape[2])
one_hot = Variable(one_hot).cuda().type(torch.cuda.FloatTensor)
thermal_conductivity_optimizer.zero_grad()
t = data[2].cuda().reshape(data[2].shape[0], 1).type(torch.cuda.FloatTensor)
label = data[3].cuda().reshape(data[3].shape[0], 1).type(torch.cuda.FloatTensor)
recon_x, mu, logvar, pre = thermal_conductivity_vae_mlp.forward(one_hot, t)
recon_x = recon_x[:, :, :one_hot.shape[2], :one_hot.shape[3]]
loss = loss_func(recon_x, one_hot, mu, logvar, pre, label)
loss.backward()
total_loss += loss.data.item() / 1000
thermal_conductivity_optimizer.step()
print('====> Epoch: {} Average loss: {:.4f}'.format(
epoch, total_loss / len(thermal_conductivity_train_loader.dataset)))
thermal_conductivity_total_loss_list[epoch] = total_loss / len(thermal_conductivity_train_loader.dataset)
if np.argmin(thermal_conductivity_total_loss_list) == epoch:
torch.save(heat_capacity_vae_mlp, thermal_conductivity_model_file_name)
print('best result, saving the model to ' + thermal_conductivity_model_file_name)
elif np.argmin(thermal_conductivity_total_loss_list) == epoch - 25:
print('Finish: Training process over due to useless training')
break
for epoch in range(params['VAE_epoch_num']):
total_loss = 0
heat_capacity_vae_mlp.train()
for i, data in enumerate(tqdm(heat_capacity_train_loader, 0)):
one_hot = torch.cat((data[0], data[1]), dim=1)
one_hot = one_hot.reshape(one_hot.shape[0], 1, one_hot.shape[1], one_hot.shape[2])
one_hot = Variable(one_hot).cuda().type(torch.cuda.FloatTensor)
heat_capacity_optimizer.zero_grad()
t = data[2].cuda().reshape(data[2].shape[0], 1).type(torch.cuda.FloatTensor)
recon_x, mu, logvar, pre = heat_capacity_vae_mlp.forward(one_hot, t)
recon_x = recon_x[:, :, :one_hot.shape[2], :one_hot.shape[3]]
loss = loss_func(recon_x, one_hot, mu, logvar, pre, t)
loss.backward()
total_loss += loss.data.item() / 1000
heat_capacity_optimizer.step()
print('====> Epoch: {} Average loss: {:.4f}'.format(
epoch, total_loss / len(heat_capacity_train_loader.dataset)))
heat_capacity_total_loss_list[epoch] = total_loss / len(heat_capacity_train_loader.dataset)
if np.argmin(heat_capacity_total_loss_list) == epoch:
torch.save(heat_capacity_vae_mlp, heat_capacity_model_file_name)
print('best result, saving the model to ' + heat_capacity_model_file_name)
elif np.argmin(thermal_conductivity_total_loss_list) == epoch - 25:
print('Finish: Training process over due to useless training')
break
| VAE_MLP/VAE_MLP_cat_model.py | 8,489 | 1, 124, 32 32, 62, 16 64, 15, 15 128, 20, 5 batch_s, 8, 7, 7 batch_s, latent batch_s, latent batch_s, latent batch_s, 8, 7, 7 batch_s, latent batch_s, latent batch_s, latent batch_s, 8, 7, 7 batch_s, 8, 7, 7 batch_s, latent | 223 | en | 0.773715 |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Corrfunc is a set of high-performance routines for
computing clustering statistics on a distribution of
points.
"""
from __future__ import (division, print_function, absolute_import,
unicode_literals)
import os
__version__ = "0.0.5"
__author__ = "Kate Storey-Fisher <kstoreyfisher@gmail.com>"
try:
__CORRFUNC_SETUP__
except NameError:
__CORRFUNC_SETUP__ = False
if not __CORRFUNC_SETUP__:
from . import io
from . import utils
from . import theory
from . import mocks
def read_text_file(filename, encoding="utf-8"):
"""
Reads a file under python3 with encoding (default UTF-8).
Also works under python2, without encoding.
Uses the EAFP (https://docs.python.org/2/glossary.html#term-eafp)
principle.
"""
try:
with open(filename, 'r', encoding) as f:
r = f.read()
except TypeError:
with open(filename, 'r') as f:
r = f.read()
return r
def write_text_file(filename, contents, encoding="utf-8"):
"""
Writes a file under python3 with encoding (default UTF-8).
Also works under python2, without encoding.
Uses the EAFP (https://docs.python.org/2/glossary.html#term-eafp)
principle.
"""
try:
with open(filename, 'w', encoding) as f:
f.write(contents)
except TypeError:
with open(filename, 'w') as f:
f.write(contents)
def which(program, mode=os.F_OK | os.X_OK, path=None):
"""
Mimics the Unix utility which.
For python3.3+, shutil.which provides all of the required functionality.
An implementation is provided in case shutil.which does
not exist.
:param program: (required) string
Name of program (can be fully-qualified path as well)
:param mode: (optional) integer flag bits
Permissions to check for in the executable
Default: os.F_OK (file exists) | os.X_OK (executable file)
:param path: (optional) string
A custom path list to check against. Implementation taken from
shutil.py.
Returns:
A fully qualified path to program as resolved by path or
user environment.
Returns None when program can not be resolved.
"""
try:
from shutil import which as shwhich
return shwhich(program, mode, path)
except ImportError:
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, _ = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
if path is None:
path = os.environ.get("PATH", os.defpath)
if not path:
return None
path = path.split(os.pathsep)
for pathdir in path:
pathdir = pathdir.strip('"')
exe_file = os.path.join(pathdir, program)
if is_exe(exe_file):
return exe_file
return None
| Corrfunc/__init__.py | 3,074 | Reads a file under python3 with encoding (default UTF-8).
Also works under python2, without encoding.
Uses the EAFP (https://docs.python.org/2/glossary.html#term-eafp)
principle.
Mimics the Unix utility which.
For python3.3+, shutil.which provides all of the required functionality.
An implementation is provided in case shutil.which does
not exist.
:param program: (required) string
Name of program (can be fully-qualified path as well)
:param mode: (optional) integer flag bits
Permissions to check for in the executable
Default: os.F_OK (file exists) | os.X_OK (executable file)
:param path: (optional) string
A custom path list to check against. Implementation taken from
shutil.py.
Returns:
A fully qualified path to program as resolved by path or
user environment.
Returns None when program can not be resolved.
Writes a file under python3 with encoding (default UTF-8).
Also works under python2, without encoding.
Uses the EAFP (https://docs.python.org/2/glossary.html#term-eafp)
principle.
Corrfunc is a set of high-performance routines for
computing clustering statistics on a distribution of
points.
!/usr/bin/env python -*- coding: utf-8 -*- | 1,211 | en | 0.82737 |
"""The token kinds currently recognized."""
from shivyc.tokens import TokenKind
keyword_kinds = []
symbol_kinds = []
bool_kw = TokenKind("_Bool", keyword_kinds)
char_kw = TokenKind("char", keyword_kinds)
short_kw = TokenKind("short", keyword_kinds)
int_kw = TokenKind("int", keyword_kinds)
long_kw = TokenKind("long", keyword_kinds)
signed_kw = TokenKind("signed", keyword_kinds)
unsigned_kw = TokenKind("unsigned", keyword_kinds)
void_kw = TokenKind("void", keyword_kinds)
return_kw = TokenKind("return", keyword_kinds)
if_kw = TokenKind("if", keyword_kinds)
else_kw = TokenKind("else", keyword_kinds)
while_kw = TokenKind("while", keyword_kinds)
for_kw = TokenKind("for", keyword_kinds)
break_kw = TokenKind("break", keyword_kinds)
continue_kw = TokenKind("continue", keyword_kinds)
auto_kw = TokenKind("auto", keyword_kinds)
static_kw = TokenKind("static", keyword_kinds)
extern_kw = TokenKind("extern", keyword_kinds)
struct_kw = TokenKind("struct", keyword_kinds)
union_kw = TokenKind("union", keyword_kinds)
const_kw = TokenKind("const", keyword_kinds)
typedef_kw = TokenKind("typedef", keyword_kinds)
sizeof_kw = TokenKind("sizeof", keyword_kinds)
plus = TokenKind("+", symbol_kinds)
minus = TokenKind("-", symbol_kinds)
star = TokenKind("*", symbol_kinds)
slash = TokenKind("/", symbol_kinds)
mod = TokenKind("%", symbol_kinds)
incr = TokenKind("++", symbol_kinds)
decr = TokenKind("--", symbol_kinds)
equals = TokenKind("=", symbol_kinds)
plusequals = TokenKind("+=", symbol_kinds)
minusequals = TokenKind("-=", symbol_kinds)
starequals = TokenKind("*=", symbol_kinds)
divequals = TokenKind("/=", symbol_kinds)
modequals = TokenKind("%=", symbol_kinds)
twoequals = TokenKind("==", symbol_kinds)
notequal = TokenKind("!=", symbol_kinds)
bool_and = TokenKind("&&", symbol_kinds)
bool_or = TokenKind("||", symbol_kinds)
bool_not = TokenKind("!", symbol_kinds)
lt = TokenKind("<", symbol_kinds)
gt = TokenKind(">", symbol_kinds)
ltoe = TokenKind("<=", symbol_kinds)
gtoe = TokenKind(">=", symbol_kinds)
amp = TokenKind("&", symbol_kinds)
pound = TokenKind("#", symbol_kinds)
lbitshift = TokenKind("<<", symbol_kinds)
rbitshift = TokenKind(">>", symbol_kinds)
compl = TokenKind("~", symbol_kinds)
dquote = TokenKind('"', symbol_kinds)
squote = TokenKind("'", symbol_kinds)
open_paren = TokenKind("(", symbol_kinds)
close_paren = TokenKind(")", symbol_kinds)
open_brack = TokenKind("{", symbol_kinds)
close_brack = TokenKind("}", symbol_kinds)
open_sq_brack = TokenKind("[", symbol_kinds)
close_sq_brack = TokenKind("]", symbol_kinds)
comma = TokenKind(",", symbol_kinds)
semicolon = TokenKind(";", symbol_kinds)
dot = TokenKind(".", symbol_kinds)
arrow = TokenKind("->", symbol_kinds)
identifier = TokenKind()
number = TokenKind()
string = TokenKind()
char_string = TokenKind()
include_file = TokenKind()
| shivyc/token_kinds.py | 2,819 | The token kinds currently recognized. | 37 | en | 0.965192 |
# -*- coding:utf-8 -*-
from os import system
from re import search, findall
from time import sleep
from requests import Session, get, post
from PIL import Image
from cfscrape import get_cookie_string
# from traceback import format_exc
# 功能:请求各大jav网站和arzon的网页
# 参数:网址url,请求头部header/cookies,代理proxy
# 返回:网页html,请求头部
#################################################### arzon ########################################################
# 获取一个arzon_cookie,返回cookie
def steal_arzon_cookies(proxy):
print('\n正在尝试通过“https://www.arzon.jp”的成人验证...')
for retry in range(10):
try: # 当初费尽心机,想办法如何通过页面上的成人验证,结果在一个C#开发的jav爬虫项目,看到它请求以下网址,再跳转到arzon主页,所得到的的cookie即是合法的cookie
if proxy:
session = Session()
session.get('https://www.arzon.jp/index.php?action=adult_customer_agecheck&agecheck=1&redirect=https%3A%2F%2Fwww.arzon.jp%2F', proxies=proxy, timeout=(6, 7))
print('通过arzon的成人验证!\n')
return session.cookies.get_dict()
else:
session = Session()
session.get('https://www.arzon.jp/index.php?action=adult_customer_agecheck&agecheck=1&redirect=https%3A%2F%2Fwww.arzon.jp%2F', timeout=(6, 7))
print('通过arzon的成人验证!\n')
return session.cookies.get_dict()
except:
# print(format_exc())
print('通过失败,重新尝试...')
continue
print('>>请检查你的网络环境是否可以打开:https://www.arzon.jp/')
system('pause')
# 搜索arzon,或请求arzon上jav所在网页,返回html
def get_arzon_html(url, cookies, proxy):
# print('代理:', proxy)
for retry in range(10):
try:
if proxy:
rqs = get(url, cookies=cookies, proxies=proxy, timeout=(6, 7))
else:
rqs = get(url, cookies=cookies, timeout=(6, 7))
except:
print(' >打开网页失败,重新尝试...')
continue
rqs.encoding = 'utf-8'
rqs_content = rqs.text
if search(r'arzon', rqs_content):
return rqs_content
else:
print(' >打开网页失败,空返回...重新尝试...')
continue
print('>>请检查你的网络环境是否可以打开:', url)
system('pause')
def find_plot_arzon(jav_num, acook, proxy_arzon):
for retry in range(2):
url_search_arzon = 'https://www.arzon.jp/itemlist.html?t=&m=all&s=&q=' + jav_num.replace('-', '')
print(' >查找简介:', url_search_arzon)
# 得到arzon的搜索结果页面
html_search_arzon = get_arzon_html(url_search_arzon, acook, proxy_arzon)
# <dt><a href="https://www.arzon.jp/item_1376110.html" title="限界集落 ~村民"><img src=
list_search_results = findall(r'h2><a href="(/item.+?)" title=', html_search_arzon) # 所有搜索结果链接
# 搜索结果为N个AV的界面
if list_search_results: # arzon有搜索结果
for url_each_result in list_search_results:
url_on_arzon = 'https://www.arzon.jp' + url_each_result # 第i+1个链接
print(' >获取简介:', url_on_arzon)
# 打开arzon上每一个搜索结果的页面
html_arzon = get_arzon_html(url_on_arzon, acook, proxy_arzon)
# 在该url_on_arzon网页上查找简介
plotg = search(r'h2>作品紹介</h2>([\s\S]*?)</div>', html_arzon)
# 成功找到plot
if str(plotg) != 'None':
plot_br = plotg.group(1)
plot = ''
for line in plot_br.split('<br />'):
line = line.strip()
plot += line
return plot, 0
# 几个搜索结果查找完了,也没有找到简介
return '【arzon有该影片,但找不到简介】', 1
# 没有搜索结果
else:
# arzon返回的页面实际是18岁验证
adultg = search(r'18歳未満', html_search_arzon)
if str(adultg) != 'None':
acook = steal_arzon_cookies(proxy_arzon)
continue
# 不是成人验证,也没有简介
else:
return '【影片下架,暂无简介】', 2
print('>>请检查你的网络环境是否可以通过成人验证:https://www.arzon.jp/')
system('pause')
return '', 3
#################################################### javlibrary ########################################################
# 获取一个library_cookie,返回cookie
def steal_library_header(url, proxy):
print('\n正在尝试通过', url, '的5秒检测...如果超过20秒卡住...重启程序...')
for retry in range(10):
try:
if proxy:
cookie_value, user_agent = get_cookie_string(url, proxies=proxy, timeout=15)
else:
cookie_value, user_agent = get_cookie_string(url, timeout=15)
print('通过5秒检测!\n')
return {'User-Agent': user_agent, 'Cookie': cookie_value}
except:
# print(format_exc())
print('通过失败,重新尝试...')
continue
print('>>通过javlibrary的5秒检测失败:', url)
system('pause')
# 搜索javlibrary,或请求javlibrary上jav所在网页,返回html
def get_library_html(url, header, proxy):
for retry in range(10):
try:
if proxy:
rqs = get(url, headers=header, proxies=proxy, timeout=(6, 7), allow_redirects=False)
else:
rqs = get(url, headers=header, timeout=(6, 7), allow_redirects=False)
except:
print(' >打开网页失败,重新尝试...')
continue
rqs.encoding = 'utf-8'
rqs_content = rqs.text
# print(rqs_content)
if search(r'JAVLibrary', rqs_content): # 得到想要的网页,直接返回
return rqs_content, header
elif search(r'javli', rqs_content): # 搜索车牌后,javlibrary跳转前的网页
url = url[:23] + search(r'(\?v=javli.+?)"', rqs_content).group(1) # rqs_content是一个非常简短的跳转网页,内容是目标jav所在网址
if len(url) > 70: # 跳转车牌特别长,cf已失效
header = steal_library_header(url[:23], proxy) # 更新header后继续请求
continue
print(' >获取信息:', url)
continue # 更新url后继续get
elif search(r'Compatible', rqs_content): # cf检测
header = steal_library_header(url[:23], proxy) # 更新header后继续请求
continue
else: # 代理工具返回的错误信息
print(' >打开网页失败,空返回...重新尝试...')
continue
print('>>请检查你的网络环境是否可以打开:', url)
system('pause')
#################################################### javbus ########################################################
# 搜索javbus,或请求javbus上jav所在网页,返回html
def get_bus_html(url, proxy):
for retry in range(10):
try:
if proxy: # existmag=all为了 获得所有影片,而不是默认的有磁力的链接
rqs = get(url, proxies=proxy, timeout=(6, 7), headers={'Cookie': 'existmag=all'})
else:
rqs = get(url, timeout=(6, 7), headers={'Cookie': 'existmag=all'})
except:
# print(format_exc())
print(' >打开网页失败,重新尝试...')
continue
rqs.encoding = 'utf-8'
rqs_content = rqs.text
if search(r'JavBus', rqs_content):
return rqs_content
else:
print(' >打开网页失败,空返回...重新尝试...')
continue
print('>>请检查你的网络环境是否可以打开:', url)
system('pause')
# 去javbus搜寻系列
def find_series_cover_bus(jav_num, url_bus, proxy_bus):
# 需要这两个东西
series = url_cover_bus = ''
status_series = 0
# 在javbus上找图片url
url_on_bus = url_bus + jav_num
print(' >获取系列:', url_on_bus)
# 获得影片在javbus上的网页
html_bus = get_bus_html(url_on_bus, proxy_bus)
if not search(r'404 Page', html_bus):
# DVD封面cover
coverg = search(r'bigImage" href="(.+?)">', html_bus)
if str(coverg) != 'None':
url_cover_bus = coverg.group(1)
# 系列:</span> <a href="https://www.cdnbus.work/series/kpl">悪質シロウトナンパ</a>
seriesg = search(r'系列:</span> <a href=".+?">(.+?)</a>', html_bus)
if str(seriesg) != 'None':
series = seriesg.group(1)
else:
# 还是老老实实去搜索
url_search_bus = url_bus + 'search/' + jav_num.replace('-', '') + '&type=1&parent=ce'
print(' >搜索javbus:', url_search_bus)
html_bus = get_bus_html(url_search_bus, proxy_bus)
# 搜索结果的网页,大部分情况一个结果,也有可能是多个结果的网页
# 尝试找movie-box
list_search_results = findall(r'movie-box" href="(.+?)">', html_bus) # 匹配处理“标题”
if list_search_results:
jav_pref = jav_num.split('-')[0] # 匹配车牌的前缀字母
jav_suf = jav_num.split('-')[-1].lstrip('0') # 当前车牌的后缀数字 去除多余的0
list_fit_results = [] # 存放,车牌符合的结果
for i in list_search_results:
url_end = i.split('/')[-1].upper()
url_suf = search(r'[-_](\d+)', url_end).group(1).lstrip('0') # 匹配box上影片url,车牌的后缀数字,去除多余的0
if jav_suf == url_suf: # 数字相同
url_pref = search(r'([A-Z]+2?8?)', url_end).group(1).upper() # 匹配处理url所带车牌前面的字母“n”
if jav_pref == url_pref: # 数字相同的基础下,字母也相同,即可能车牌相同
list_fit_results.append(i)
# 有结果
if list_fit_results:
# 有多个结果,发个状态码,警告一下用户
if len(list_fit_results) > 1:
status_series = 1
# 默认用第一个搜索结果
url_first_result = list_fit_results[0]
print(' >获取系列:', url_first_result)
html_bus = get_bus_html(url_first_result, proxy_bus)
# DVD封面cover
coverg = search(r'bigImage" href="(.+?)">', html_bus)
if str(coverg) != 'None':
url_cover_bus = coverg.group(1)
# 系列:</span> <a href="https://www.cdnbus.work/series/kpl">悪質シロウトナンパ</a>
seriesg = search(r'系列:</span> <a href=".+?">(.+?)</a>', html_bus)
if str(seriesg) != 'None':
series = seriesg.group(1)
return url_cover_bus, series, status_series
#################################################### jav321 ########################################################
# 用户指定jav321的网址后,请求jav所在网页,返回html
def get_321_html(url, proxy):
for retry in range(10):
try:
if proxy:
rqs = get(url, proxies=proxy, timeout=(6, 7))
else:
rqs = get(url, timeout=(6, 7))
except:
print(' >打开网页失败,重新尝试...')
continue
rqs.encoding = 'utf-8'
rqs_content = rqs.text
if search(r'JAV321', rqs_content):
return rqs_content
else:
print(' >打开网页失败,空返回...重新尝试...')
continue
print('>>请检查你的网络环境是否可以打开:', url)
system('pause')
# 向jav321 post车牌,得到jav所在网页,也可能是无结果的网页,返回html
def post_321_html(url, data, proxy):
for retry in range(10):
try:
if proxy:
rqs = post(url, data=data, proxies=proxy, timeout=(6, 7))
else:
rqs = post(url, data=data, timeout=(6, 7))
except:
# print(format_exc())
print(' >打开网页失败,重新尝试...')
continue
rqs.encoding = 'utf-8'
rqs_content = rqs.text
if search(r'JAV321', rqs_content):
return rqs_content
else:
print(' >打开网页失败,空返回...重新尝试...')
continue
print('>>请检查你的网络环境是否可以打开:', url)
system('pause')
#################################################### javdb ########################################################
# 搜索javdb,得到搜索结果网页,返回html。
def get_search_db_html(url, proxy):
for retry in range(1, 11):
if retry % 4 == 0:
print(' >睡眠5分钟...')
sleep(300)
try:
if proxy:
rqs = get(url, proxies=proxy, timeout=(6, 7))
else:
rqs = get(url, timeout=(6, 7))
except:
# print(format_exc())
print(' >打开网页失败,重新尝试...')
continue
rqs.encoding = 'utf-8'
rqs_content = rqs.text
if search(r'JavDB', rqs_content):
if search(r'搜索結果', rqs_content):
return rqs_content
else:
print(' >睡眠5分钟...')
sleep(300)
continue
else:
print(' >打开网页失败,空返回...重新尝试...')
continue
print('>>请检查你的网络环境是否可以打开:', url)
system('pause')
# 请求jav在javdb上的网页,返回html
def get_db_html(url, proxy):
for retry in range(1, 11):
if retry % 4 == 0:
print(' >睡眠5分钟...')
sleep(300)
try:
if proxy:
rqs = get(url, proxies=proxy, timeout=(6, 7))
else:
rqs = get(url, timeout=(6, 7))
except:
# print(format_exc())
print(' >打开网页失败,重新尝试...')
continue
rqs.encoding = 'utf-8'
rqs_content = rqs.text
if search(r'JavDB', rqs_content):
if search(r'content="JavDB', rqs_content):
return rqs_content
else:
print(' >睡眠5分钟...')
sleep(300)
continue
else:
print(' >打开网页失败,空返回...重新尝试...')
continue
print('>>请检查你的网络环境是否可以打开:', url)
system('pause')
#################################################### 下载图片 ########################################################
# 下载图片,无返回
def download_pic(url, path, proxy):
for retry in range(5):
try:
if proxy:
r = get(url, proxies=proxy, stream=True, timeout=(6, 10))
with open(path, 'wb') as pic:
for chunk in r:
pic.write(chunk)
else:
r = get(url, stream=True, timeout=(6, 10))
with open(path, 'wb') as pic:
for chunk in r:
pic.write(chunk)
except:
# print(format_exc())
print(' >下载失败,重新下载...')
continue
# 如果下载的图片打不开,则重新下载
try:
img = Image.open(path)
img.load()
return
except OSError:
print(' >下载失败,重新下载....')
continue
raise Exception(' >下载多次,仍然失败!')
| functions_requests.py | 16,888 | -*- coding:utf-8 -*- from traceback import format_exc 功能:请求各大jav网站和arzon的网页 参数:网址url,请求头部header/cookies,代理proxy 返回:网页html,请求头部 arzon 获取一个arzon_cookie,返回cookie 当初费尽心机,想办法如何通过页面上的成人验证,结果在一个C开发的jav爬虫项目,看到它请求以下网址,再跳转到arzon主页,所得到的的cookie即是合法的cookie print(format_exc()) 搜索arzon,或请求arzon上jav所在网页,返回html print('代理:', proxy) 得到arzon的搜索结果页面 <dt><a href="https://www.arzon.jp/item_1376110.html" title="限界集落 ~村民"><img src= 所有搜索结果链接 搜索结果为N个AV的界面 arzon有搜索结果 第i+1个链接 打开arzon上每一个搜索结果的页面 在该url_on_arzon网页上查找简介 成功找到plot 几个搜索结果查找完了,也没有找到简介 没有搜索结果 arzon返回的页面实际是18岁验证 不是成人验证,也没有简介 javlibrary 获取一个library_cookie,返回cookie print(format_exc()) 搜索javlibrary,或请求javlibrary上jav所在网页,返回html print(rqs_content) 得到想要的网页,直接返回 搜索车牌后,javlibrary跳转前的网页 rqs_content是一个非常简短的跳转网页,内容是目标jav所在网址 跳转车牌特别长,cf已失效 更新header后继续请求 更新url后继续get cf检测 更新header后继续请求 代理工具返回的错误信息 javbus 搜索javbus,或请求javbus上jav所在网页,返回html existmag=all为了 获得所有影片,而不是默认的有磁力的链接 print(format_exc()) 去javbus搜寻系列 需要这两个东西 在javbus上找图片url 获得影片在javbus上的网页 DVD封面cover 系列:</span> <a href="https://www.cdnbus.work/series/kpl">悪質シロウトナンパ</a> 还是老老实实去搜索 搜索结果的网页,大部分情况一个结果,也有可能是多个结果的网页 尝试找movie-box 匹配处理“标题” 匹配车牌的前缀字母 当前车牌的后缀数字 去除多余的0 存放,车牌符合的结果 匹配box上影片url,车牌的后缀数字,去除多余的0 数字相同 匹配处理url所带车牌前面的字母“n” 数字相同的基础下,字母也相同,即可能车牌相同 有结果 有多个结果,发个状态码,警告一下用户 默认用第一个搜索结果 DVD封面cover 系列:</span> <a href="https://www.cdnbus.work/series/kpl">悪質シロウトナンパ</a> jav321 用户指定jav321的网址后,请求jav所在网页,返回html 向jav321 post车牌,得到jav所在网页,也可能是无结果的网页,返回html print(format_exc()) javdb 搜索javdb,得到搜索结果网页,返回html。 print(format_exc()) 请求jav在javdb上的网页,返回html print(format_exc()) 下载图片 下载图片,无返回 print(format_exc()) 如果下载的图片打不开,则重新下载 | 1,595 | zh | 0.872267 |
#!/usr/bin/env python3
# Copyright (c) 2015-2016 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test multiple RPC users."""
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import str_to_b64str, assert_equal
import os
import http.client
import urllib.parse
class HTTPBasicsTest (BitcoinTestFramework):
def __init__(self):
super().__init__()
self.setup_clean_chain = False
self.num_nodes = 1
def setup_chain(self):
super().setup_chain()
#Append rpcauth to polis.conf before initialization
rpcauth = "rpcauth=rt:93648e835a54c573682c2eb19f882535$7681e9c5b74bdd85e78166031d2058e1069b3ed7ed967c93fc63abba06f31144"
rpcauth2 = "rpcauth=rt2:f8607b1a88861fac29dfccf9b52ff9f$ff36a0c23c8c62b4846112e50fa888416e94c17bfd4c42f88fd8f55ec6a3137e"
with open(os.path.join(self.options.tmpdir+"/node0", "polis.conf"), 'a', encoding='utf8') as f:
f.write(rpcauth+"\n")
f.write(rpcauth2+"\n")
def setup_network(self):
self.nodes = self.setup_nodes()
def run_test(self):
##################################################
# Check correctness of the rpcauth config option #
##################################################
url = urllib.parse.urlparse(self.nodes[0].url)
#Old authpair
authpair = url.username + ':' + url.password
#New authpair generated via share/rpcuser tool
rpcauth = "rpcauth=rt:93648e835a54c573682c2eb19f882535$7681e9c5b74bdd85e78166031d2058e1069b3ed7ed967c93fc63abba06f31144"
password = "cA773lm788buwYe4g4WT+05pKyNruVKjQ25x3n0DQcM="
#Second authpair with different username
rpcauth2 = "rpcauth=rt2:f8607b1a88861fac29dfccf9b52ff9f$ff36a0c23c8c62b4846112e50fa888416e94c17bfd4c42f88fd8f55ec6a3137e"
password2 = "8/F3uMDw4KSEbw96U3CA1C4X05dkHDN2BPFjTgZW4KI="
authpairnew = "rt:"+password
headers = {"Authorization": "Basic " + str_to_b64str(authpair)}
conn = http.client.HTTPConnection(url.hostname, url.port)
conn.connect()
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
resp = conn.getresponse()
assert_equal(resp.status==401, False)
conn.close()
#Use new authpair to confirm both work
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
conn = http.client.HTTPConnection(url.hostname, url.port)
conn.connect()
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
resp = conn.getresponse()
assert_equal(resp.status==401, False)
conn.close()
#Wrong login name with rt's password
authpairnew = "rtwrong:"+password
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
conn = http.client.HTTPConnection(url.hostname, url.port)
conn.connect()
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
resp = conn.getresponse()
assert_equal(resp.status==401, True)
conn.close()
#Wrong password for rt
authpairnew = "rt:"+password+"wrong"
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
conn = http.client.HTTPConnection(url.hostname, url.port)
conn.connect()
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
resp = conn.getresponse()
assert_equal(resp.status==401, True)
conn.close()
#Correct for rt2
authpairnew = "rt2:"+password2
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
conn = http.client.HTTPConnection(url.hostname, url.port)
conn.connect()
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
resp = conn.getresponse()
assert_equal(resp.status==401, False)
conn.close()
#Wrong password for rt2
authpairnew = "rt2:"+password2+"wrong"
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
conn = http.client.HTTPConnection(url.hostname, url.port)
conn.connect()
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
resp = conn.getresponse()
assert_equal(resp.status==401, True)
conn.close()
if __name__ == '__main__':
HTTPBasicsTest ().main ()
| qa/rpc-tests/multi_rpc.py | 4,550 | Test multiple RPC users.
!/usr/bin/env python3 Copyright (c) 2015-2016 The Bitcoin Core developers Distributed under the MIT software license, see the accompanying file COPYING or http://www.opensource.org/licenses/mit-license.php.Append rpcauth to polis.conf before initialization Check correctness of the rpcauth config option Old authpairNew authpair generated via share/rpcuser toolSecond authpair with different usernameUse new authpair to confirm both workWrong login name with rt's passwordWrong password for rtCorrect for rt2Wrong password for rt2 | 556 | en | 0.608306 |
from __future__ import unicode_literals
import unittest
from django.conf.urls import url
from django.contrib.auth import authenticate, login
from django.contrib.auth.models import User
from django.shortcuts import redirect
from django.test import override_settings
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_protect, ensure_csrf_cookie
from rest_framework.compat import is_authenticated, requests
from rest_framework.response import Response
from rest_framework.test import APITestCase, RequestsClient
from rest_framework.views import APIView
class Root(APIView):
def get(self, request):
return Response({
'method': request.method,
'query_params': request.query_params,
})
def post(self, request):
files = {
key: (value.name, value.read())
for key, value in request.FILES.items()
}
post = request.POST
json = None
if request.META.get('CONTENT_TYPE') == 'application/json':
json = request.data
return Response({
'method': request.method,
'query_params': request.query_params,
'POST': post,
'FILES': files,
'JSON': json
})
class HeadersView(APIView):
def get(self, request):
headers = {
key[5:].replace('_', '-'): value
for key, value in request.META.items()
if key.startswith('HTTP_')
}
return Response({
'method': request.method,
'headers': headers
})
class SessionView(APIView):
def get(self, request):
return Response({
key: value for key, value in request.session.items()
})
def post(self, request):
for key, value in request.data.items():
request.session[key] = value
return Response({
key: value for key, value in request.session.items()
})
class AuthView(APIView):
@method_decorator(ensure_csrf_cookie)
def get(self, request):
if is_authenticated(request.user):
username = request.user.username
else:
username = None
return Response({
'username': username
})
@method_decorator(csrf_protect)
def post(self, request):
username = request.data['username']
password = request.data['password']
user = authenticate(username=username, password=password)
if user is None:
return Response({'error': 'incorrect credentials'})
login(request, user)
return redirect('/auth/')
urlpatterns = [
url(r'^$', Root.as_view(), name='root'),
url(r'^headers/$', HeadersView.as_view(), name='headers'),
url(r'^session/$', SessionView.as_view(), name='session'),
url(r'^auth/$', AuthView.as_view(), name='auth'),
]
@unittest.skipUnless(requests, 'requests not installed')
@override_settings(ROOT_URLCONF='tests.test_requests_client')
class RequestsClientTests(APITestCase):
def test_get_request(self):
client = RequestsClient()
response = client.get('http://testserver/')
assert response.status_code == 200
assert response.headers['Content-Type'] == 'application/json'
expected = {
'method': 'GET',
'query_params': {}
}
assert response.json() == expected
def test_get_request_query_params_in_url(self):
client = RequestsClient()
response = client.get('http://testserver/?key=value')
assert response.status_code == 200
assert response.headers['Content-Type'] == 'application/json'
expected = {
'method': 'GET',
'query_params': {'key': 'value'}
}
assert response.json() == expected
def test_get_request_query_params_by_kwarg(self):
client = RequestsClient()
response = client.get('http://testserver/', params={'key': 'value'})
assert response.status_code == 200
assert response.headers['Content-Type'] == 'application/json'
expected = {
'method': 'GET',
'query_params': {'key': 'value'}
}
assert response.json() == expected
def test_get_with_headers(self):
client = RequestsClient()
response = client.get('http://testserver/headers/', headers={'User-Agent': 'example'})
assert response.status_code == 200
assert response.headers['Content-Type'] == 'application/json'
headers = response.json()['headers']
assert headers['USER-AGENT'] == 'example'
def test_get_with_session_headers(self):
client = RequestsClient()
client.headers.update({'User-Agent': 'example'})
response = client.get('http://testserver/headers/')
assert response.status_code == 200
assert response.headers['Content-Type'] == 'application/json'
headers = response.json()['headers']
assert headers['USER-AGENT'] == 'example'
def test_post_form_request(self):
client = RequestsClient()
response = client.post('http://testserver/', data={'key': 'value'})
assert response.status_code == 200
assert response.headers['Content-Type'] == 'application/json'
expected = {
'method': 'POST',
'query_params': {},
'POST': {'key': 'value'},
'FILES': {},
'JSON': None
}
assert response.json() == expected
def test_post_json_request(self):
client = RequestsClient()
response = client.post('http://testserver/', json={'key': 'value'})
assert response.status_code == 200
assert response.headers['Content-Type'] == 'application/json'
expected = {
'method': 'POST',
'query_params': {},
'POST': {},
'FILES': {},
'JSON': {'key': 'value'}
}
assert response.json() == expected
def test_post_multipart_request(self):
client = RequestsClient()
files = {
'file': ('report.csv', 'some,data,to,send\nanother,row,to,send\n')
}
response = client.post('http://testserver/', files=files)
assert response.status_code == 200
assert response.headers['Content-Type'] == 'application/json'
expected = {
'method': 'POST',
'query_params': {},
'FILES': {'file': ['report.csv', 'some,data,to,send\nanother,row,to,send\n']},
'POST': {},
'JSON': None
}
assert response.json() == expected
def test_session(self):
client = RequestsClient()
response = client.get('http://testserver/session/')
assert response.status_code == 200
assert response.headers['Content-Type'] == 'application/json'
expected = {}
assert response.json() == expected
response = client.post('http://testserver/session/', json={'example': 'abc'})
assert response.status_code == 200
assert response.headers['Content-Type'] == 'application/json'
expected = {'example': 'abc'}
assert response.json() == expected
response = client.get('http://testserver/session/')
assert response.status_code == 200
assert response.headers['Content-Type'] == 'application/json'
expected = {'example': 'abc'}
assert response.json() == expected
def test_auth(self):
# Confirm session is not authenticated
client = RequestsClient()
response = client.get('http://testserver/auth/')
assert response.status_code == 200
assert response.headers['Content-Type'] == 'application/json'
expected = {
'username': None
}
assert response.json() == expected
assert 'csrftoken' in response.cookies
csrftoken = response.cookies['csrftoken']
user = User.objects.create(username='tom')
user.set_password('password')
user.save()
# Perform a login
response = client.post('http://testserver/auth/', json={
'username': 'tom',
'password': 'password'
}, headers={'X-CSRFToken': csrftoken})
assert response.status_code == 200
assert response.headers['Content-Type'] == 'application/json'
expected = {
'username': 'tom'
}
assert response.json() == expected
# Confirm session is authenticated
response = client.get('http://testserver/auth/')
assert response.status_code == 200
assert response.headers['Content-Type'] == 'application/json'
expected = {
'username': 'tom'
}
assert response.json() == expected
| tests/test_requests_client.py | 8,825 | Confirm session is not authenticated Perform a login Confirm session is authenticated | 85 | en | 0.870872 |
from tkinter import *
from tkinter import messagebox
import tkinter.ttk as ttk
import datetime
def init(top, gui):
global w, top_level, root
w = gui
top_level = top
root = top
def start_gui():
global w, root
root = Tk()
root.iconbitmap("index.ico")
top = main_level(root)
init(root, top)
root.mainloop()
w = None
def new_top(root):
global w, rt
rt = root
w = Toplevel(root)
top = main_level(w)
init(w, top)
return w, top
def exit():
qExit=messagebox.askyesno(" Quit System ", " Do you really want to Exit ? ")
if qExit > 0:
root.destroy()
return
bus_stops = {'MG Road': ['Koramangala','HSR Layout','Sarjapur road'],
'Koramangala': ['MG Road','HSR Layout','Sarjapur road'],
'HSR Layout': ['MG Road','Koramangala','Sarjapur road'],
'Sarjapur road' : ['MG Road','Koramangala','HSR Layout']}
class main_level:
def bus_Stops(self, top=None):
self.des_Combo['values'] = bus_stops[self.boarding_Combo.get()]
def booked(self):
#self.select()
#pass
if self.boarding_Combo.get() == "Select Boarding":
messagebox.showerror("Unknown Boarding", " Please select your Boarding")
return
elif self.des_Combo.get() == "Select Destination":
messagebox.showerror("Unknown Destination", "Please Select your Destination")
return
elif self.no_of_Adult.get() == "Select Adult(s)":
messagebox.showerror("No Adult Selected", " Please select Adult(s)")
return
elif self.no_of_Child.get() == "Select Child(s)":
messagebox.showerror("No Child Selected", " Please select Child(s)")
return
elif self.boarding_Combo.get() == self.des_Combo.get():
messagebox.showerror("Error", "Boarding and Destination cannot be same!")
return
qtotal = messagebox.askyesno("Total Cost Check", " Do you want to check total cost")
if qtotal > 0:
messagebox.showinfo("Total Cost Check","Check total fare by clicking Total Button")
return
qPrint = messagebox.askyesno("Confirmation", "Ticket Booked!\n\n Print Ticket ?")
if qPrint > 0:
messagebox.showinfo("Booked","Ticket Booked\n\n"+"Printing Ticket\n\n " + self.boarding_Combo.get() + " To " + self.des_Combo.get() + "\n\n" + " For " + self.no_of_Adult.get() + " Adult(s)" + " and " + self.no_of_Child.get() + " Child(s)")
# for sql table
global na
na = self.no_of_Adult.get()
global nc
nc = self.no_of_Child.get()
global bb
bb = '\'{}\''.format(self.boarding_Combo.get())
global dc
dc = '\'{}\''.format(self.des_Combo.get())
self.from2.configure(text="")
self.to2.configure(text="")
self.no_of_Adults2.configure(text="")
self.no_of_Child2.configure(text="")
self.tCost2.configure(text="")
self.no_of_Adult.set("Select Adult(s)")
self.no_of_Child.set("Select Child(s)")
self.boarding_Combo.set("Select Boarding")
self.des_Combo.set("Select Destination")
Date1 = StringVar()
now = datetime.datetime.now()
Date1.set(now.strftime("%d-%m-%Y %I:%M:%S:%p"))
self.lbltiming2.configure(textvariable=Date1)
ticket_num = 1
ticket_nu = str(ticket_num)
f = open("Tickets.txt", "a")
f.write(ticket_nu)
f.close()
with open('Tickets.txt') as infile:
characters = 0
for lineno, line in enumerate(infile, 1):
wordslist = line.split()
characters += sum(len(word) for word in wordslist)
ticketno = StringVar()
ticket_number = characters
ticketno.set(ticket_number)
self.tno.configure(textvariable=ticketno)
def reset(self):
self.from2.configure(text="")
self.to2.configure(text="")
self.no_of_Adults2.configure(text="")
self.no_of_Child2.configure(text="")
self.tCost2.configure(text="")
self.no_of_Adult.set("Select Adult(s)")
self.no_of_Child.set("Select Child(s)")
self.boarding_Combo.set("Select Boarding")
self.des_Combo.set("Select Destination")
Date1 = StringVar()
now = datetime.datetime.now()
Date1.set(now.strftime("%d-%m-%Y %I:%M:%S:%p"))
self.lbltiming2.configure(textvariable=Date1)
def travel_Cost(self):
if self.boarding_Combo.get() == "Select Boarding":
messagebox.showerror("Unknown Boarding", " Please select your Boarding")
return
elif self.des_Combo.get() == "Select Destination":
messagebox.showerror("Unknown Destination", "Please Select your Destination")
return
elif self.no_of_Adult.get() == "Select Adult(s)":
messagebox.showerror("No Adult Selected", " Please select Adult(s)")
return
elif self.no_of_Child.get() == "Select Child(s)":
messagebox.showerror("No Child Selected", " Please select Child(s)")
return
elif self.boarding_Combo.get() == self.des_Combo.get():
messagebox.showerror("Error", "Boarding and Destination cannot be same!")
return
self.from2.configure(text="" + self.boarding_Combo.get())
self.to2.configure(text="" + self.des_Combo.get())
self.no_of_Adults2.configure(text="" + self.no_of_Adult.get())
self.no_of_Child2.configure(text="" + self.no_of_Child.get())
#-------------------------------------Total Ticket Cost----------------------------------------------------------
cost = 0
calculated_cost = float()
if (self.no_of_Adult.get() == "0"):
calculated_cost = cost + 0
elif (self.no_of_Adult.get() == "1"):
calculated_cost = cost + 1
elif (self.no_of_Adult.get() == "2"):
calculated_cost = cost + 2
elif (self.no_of_Adult.get() == "3"):
calculated_cost = cost + 3
aticket = calculated_cost
child_cost = 0
c_cost = float()
if self.no_of_Child.get() == "0":
c_cost = child_cost + 0
elif self.no_of_Child.get() == "1":
c_cost = child_cost + 1 / 2
elif self.no_of_Child.get() == "2":
c_cost = child_cost + 2 / 2
elif self.no_of_Child.get() == "3":
c_cost = child_cost + 3 / 2
cticket = c_cost
passenger = cticket + aticket
fare = 0
t_fare = float()
if (self.boarding_Combo.get() == "MG Road") and (self.des_Combo.get() == "Koramangala"):
t_fare = fare + 25
elif (self.boarding_Combo.get() == "HSR Layout") and (self.des_Combo.get() == "Koramangala"):
t_fare = fare + 22
elif (self.boarding_Combo.get() == "Sarjapur road") and (self.des_Combo.get() == "Koramangala"):
t_fare = fare + 20
elif (self.boarding_Combo.get() == "Koramangala") and (self.des_Combo.get() == "HSR Layout"):
t_fare = fare + 17
elif (self.boarding_Combo.get() == "Sarjapur road") and (self.des_Combo.get() == "HSR Layout"):
t_fare = fare + 15
elif (self.boarding_Combo.get() == "MG Road") and (self.des_Combo.get() == "HSR Layout"):
t_fare = fare + 11
elif (self.boarding_Combo.get() == "Koramangala") and (self.des_Combo.get() == "Sarjapur road"):
t_fare = fare + 9
elif (self.boarding_Combo.get() == "HSR Layout") and (self.des_Combo.get() == "Sarjapur road"):
t_fare = fare + 22
elif (self.boarding_Combo.get() == "MG Road") and (self.des_Combo.get() == "Sarjapur road"):
t_fare = fare + 20
elif (self.boarding_Combo.get() == "Sarjapur road") and (self.des_Combo.get() == "MG Road"):
t_fare = fare + 17
elif (self.boarding_Combo.get() == "HSR Layout") and (self.des_Combo.get() == "MG Road"):
t_fare = fare + 15
elif (self.boarding_Combo.get() == "Koramangala") and (self.des_Combo.get() == "MG Road"):
t_fare = fare + 11
total_fare = t_fare
global final_price
final_price = (total_fare * passenger)
self.tCost2.configure(text=final_price)
return
#-------------------------------Defining all the specifications for the TKinter Frontend -----------------------------
def __init__(self, top=None):
top.geometry("732x443+327+147")
top.title("Bus Ticketing System")
self.style = ttk.Style()
font10 = "-family {Wide Latin} -size 10 -weight bold -slant " \
"roman -underline 0 -overstrike 0"
font15 = "-family {Snap ITC} -size 20 -weight bold -slant " \
"roman -underline 1 -overstrike 0"
font17 = "-family {Segoe UI} -size 10 -weight bold -slant " \
"roman -underline 0 -overstrike 0"
font18 = "-family {Segoe UI} -size 11 -weight bold -slant " \
"roman -underline 0 -overstrike 0"
Date1 = StringVar()
now = datetime.datetime.now()
Date1.set(now.strftime("%d-%m-%Y %I:%M:%S:%p"))
with open('Tickets.txt') as infile:
characters = 0
for lineno, line in enumerate(infile, 1):
wordslist = line.split()
characters += sum(len(word) for word in wordslist)
ticketno = StringVar()
ticket_number = characters
ticketno.set(ticket_number)
bg = PhotoImage(file = r"C:\bus4.png")
self.frame_Booking_Panel = Frame(top)
self.frame_Booking_Panel.place(relx=0.0, rely=0.0, relheight=1.0, relwidth=1.0)
self.frame_Booking_Panel.configure(relief=GROOVE)
self.frame_Booking_Panel.configure(borderwidth="5")
self.frame_Booking_Panel.configure(background ='#ADD8E6')
self.back_ground = Label(self.frame_Booking_Panel,image = bg)
self.back_ground.img = bg
self.back_ground.place(relx=0.14, rely=0.05, height=732, width=443)
self.back_ground.pack()
self.bus_Service = Label(self.frame_Booking_Panel)
self.bus_Service.place(relx=0.14, rely=0.05, height=21, width=544)
self.bus_Service.configure(background="#ADD8E6")
self.bus_Service.configure(font=font15)
self.bus_Service.configure(text="Bus Service")
self.ticket_No = Label(self.frame_Booking_Panel)
self.ticket_No.place(relx=0.04, rely=0.18, height=21, width=84)
self.ticket_No.configure(background="#E6E6FA")
self.ticket_No.configure(font=font18)
self.ticket_No.configure(text="Ticket No. : ")
self.tno = Label(self.frame_Booking_Panel)
self.tno.place(relx=0.15, rely=0.18, height=21, width=30)
self.tno.configure(background="#E6E6FA")
self.tno.configure(font=font18)
self.tno.configure(textvariable=ticketno)
self.bus_id = Label(self.frame_Booking_Panel)
self.bus_id.place(relx=0.75, rely=0.16, height=21, width=119)
self.bus_id.configure(background="#E6E6FA")
self.bus_id.configure(font=font18)
self.bus_id.configure(text="Bus : JFBS0001")
self.boarding_lbl = Label(self.frame_Booking_Panel)
self.boarding_lbl.place(relx=0.04, rely=0.32, height=23, width=84)
self.boarding_lbl.configure(background="#DCAE96")
self.boarding_lbl.configure(font=font17)
self.boarding_lbl.configure(text="Boarding :")
self.destination_lbl = Label(self.frame_Booking_Panel)
self.destination_lbl.place(relx=0.52, rely=0.32, height=21, width=134)
self.destination_lbl.configure(background="#DCAE96")
self.destination_lbl.configure(font=font18)
self.destination_lbl.configure(text="Destination :")
self.boarding_Combo = ttk.Combobox(self.frame_Booking_Panel, state='readonly', values=list(bus_stops.keys()))
self.boarding_Combo.place(relx=0.19, rely=0.32, relheight=0.05, relwidth=0.2)
self.boarding_Combo.set("Select Boarding")
self.boarding_Combo.bind('<<ComboboxSelected>>', self.bus_Stops)
self.des_Combo = ttk.Combobox(self.frame_Booking_Panel, state='readonly')
self.des_Combo.place(relx=0.74, rely=0.32, relheight=0.05, relwidth=0.2)
self.des_Combo.bind('<<ComboboxSelected>>')
self.des_Combo.set("Select Destination")
self.passengers_lbl = Label(self.frame_Booking_Panel)
self.passengers_lbl.place(relx=0.04, rely=0.47, height=21, width=114)
self.passengers_lbl.configure(background="#E6E6FA")
self.passengers_lbl.configure(font=font18)
self.passengers_lbl.configure(text="Passenger(s) :")
self.no_of_Adult = ttk.Combobox(self.frame_Booking_Panel, state='readonly')
self.no_of_Adult.place(relx=0.16, rely=0.56, relheight=0.05, relwidth=0.15)
self.no_of_Adult['values'] = (0, 1, 2, 3)
self.no_of_Adult.set("Select Adult(s)")
self.no_of_Adult.bind('<<ComboboxSelected>>')
self.no_of_Child = ttk.Combobox(self.frame_Booking_Panel, state='readonly')
self.no_of_Child.place(relx=0.16, rely=0.65, relheight=0.05, relwidth=0.15)
self.no_of_Child['values'] = (0, 1, 2, 3)
self.no_of_Child.set("Select Child(s)")
self.no_of_Child.bind('<<ComboboxSelected>>')
self.book_Button = Button(self.frame_Booking_Panel)
self.book_Button.place(relx=0.78, rely=0.62, height=24, width=107)
self.book_Button.configure(background="#008040")
self.book_Button.configure(font=font17)
self.book_Button.configure(text="Book")
self.book_Button.configure(command=self.booked)
self.exit_Button = Button(self.frame_Booking_Panel)
self.exit_Button.place(relx=0.78, rely=0.92, height=24, width=107)
self.exit_Button.configure(background="#008040")
self.exit_Button.configure(font=font17)
self.exit_Button.configure(text="Exit")
self.exit_Button.configure(command=exit)
self.reset_Button = Button(self.frame_Booking_Panel)
self.reset_Button.place(relx=0.78, rely=0.77, height=24, width=107)
self.reset_Button.configure(background="#008040")
self.reset_Button.configure(font=font17)
self.reset_Button.configure(text="Reset")
self.reset_Button.configure(command=self.reset)
self.total_Button = Button(self.frame_Booking_Panel)
self.total_Button.place(relx=0.78, rely=0.47, height=24, width=107)
self.total_Button.configure(background="#008040")
self.total_Button.configure(font=font17)
self.total_Button.configure(text="Total")
self.total_Button.configure(command=self.travel_Cost)
self.lblAdultno = Label(self.frame_Booking_Panel)
self.lblAdultno.place(relx=0.05, rely=0.56, height=21, width=64)
self.lblAdultno.configure(background="#ff8040")
self.lblAdultno.configure(text="Adult")
self.lblChildno = Label(self.frame_Booking_Panel)
self.lblChildno.place(relx=0.05, rely=0.65, height=21, width=64)
self.lblChildno.configure(background="#ff8040")
self.lblChildno.configure(text="Child")
self.total_Frame = Frame(self.frame_Booking_Panel)
self.total_Frame.place(relx=0.36, rely=0.47, relheight=0.44, relwidth=0.36)
self.total_Frame.configure(relief=GROOVE)
self.total_Frame.configure(borderwidth="1")
self.total_Frame.configure(background="#ADD8E6")
self.from1 = Label(self.total_Frame)
self.from1.place(relx=0.08, rely=0.05, height=21, width=54)
self.from1.configure(background="#0080ff")
self.from1.configure(text="From :")
self.from2 = Label(self.total_Frame)
self.from2.place(relx=0.40, rely=0.05, height=21, width=121)
self.from2.configure(background="#8080ff")
self.from2.configure(highlightcolor="black")
self.to1 = Label(self.total_Frame)
self.to1.place(relx=0.08, rely=0.21, height=21, width=49)
self.to1.configure(background="#0080ff")
self.to1.configure(text="To :")
self.to2 = Label(self.total_Frame)
self.to2.place(relx=0.40, rely=0.21, height=21, width=121)
self.to2.configure(background="#8080ff")
self.no_of_Adults1 = Label(self.total_Frame)
self.no_of_Adults1.place(relx=0.08, rely=0.36, height=21, width=55)
self.no_of_Adults1.configure(background="#0080ff")
self.no_of_Adults1.configure(text="Adult :")
self.no_of_Adults2 = Label(self.total_Frame)
self.no_of_Adults2.place(relx=0.40, rely=0.36, height=21, width=121)
self.no_of_Adults2.configure(background="#8080ff")
self.no_of_Child1 = Label(self.total_Frame)
self.no_of_Child1.place(relx=0.08, rely=0.51, height=21, width=46)
self.no_of_Child1.configure(background="#0080ff")
self.no_of_Child1.configure(text="Child :")
self.no_of_Child2 = Label(self.total_Frame)
self.no_of_Child2.place(relx=0.40, rely=0.51, height=21, width=121)
self.no_of_Child2.configure(background="#8080ff")
self.tCost1 = Label(self.total_Frame)
self.tCost1.place(relx=0.08, rely=0.67, height=21, width=74)
self.tCost1.configure(background="#0080ff")
self.tCost1.configure(text="Total (Rs.) :")
self.tCost2 = Label(self.total_Frame)
self.tCost2.place(relx=0.40, rely=0.67, height=21, width=121)
self.tCost2.configure(background="#8080ff")
self.lbltiming1 = Label(self.total_Frame)
self.lbltiming1.place(relx=0.08, rely=0.82, height=21, width=74)
self.lbltiming1.configure(background="#0080ff")
self.lbltiming1.configure(text="Booking at :")
self.lbltiming2 = Label(self.total_Frame)
self.lbltiming2.place(relx=0.40, rely=0.82, height=21, width=135)
self.lbltiming2.configure(background="#8080ff")
self.lbltiming2.configure(textvariable=Date1)
start_gui()
#----------------------------------------SQL PART --------------------------------------------------
import mysql.connector as ms
conn = ms.connect(host = "localhost", user = "root", passwd="", database ="project")
if conn.is_connected():
print("Connected Succefully")
else:
print("Try again....")
cursor = conn.cursor()
#date for sql table
now = datetime.datetime.now()
formatted_date = now.strftime('%Y-%m-%d')
f1 = '\'{}\''.format(formatted_date)
#checking whether all values r good
print(f1,na,nc,bb,dc,final_price)
str1 = "insert into bus_data values({},{},{},{},{},{});".format(f1,na,nc,bb,dc,final_price)
cursor.execute(str1)
conn.commit()
| Bus Ticketing system MAIN final.py | 19,492 | self.select()pass for sql table-------------------------------------Total Ticket Cost-----------------------------------------------------------------------------------------Defining all the specifications for the TKinter Frontend ---------------------------------------------------------------------SQL PART --------------------------------------------------date for sql tablechecking whether all values r good | 413 | en | 0.225111 |
import jinja2
class SPMObject(object):
""" Abstract Base Class for all SPM objects.
Even though SPM objects are not Spire tasks (as some of them will modify
in-place their file_dep, which is not compatible with doit's task
semantics), they nonetheless include task-related properties: file_dep
and targets. Subclasses will have to override the _get_file_dep and
_get_targets functions to return the correct values.
"""
def __init__(self, name):
self.name = name
self.environment = jinja2.Environment()
self.environment.globals.update(id=__class__._get_id)
def get_script(self, index):
template = self.environment.from_string(self.template)
return template.render(index=index, **vars(self))
@property
def file_dep(self):
return self._get_file_dep()
@property
def targets(self):
return self._get_targets()
@staticmethod
def _get_id(index, name):
return "matlabbatch{"+str(index)+"}."+name
def _get_file_dep(self):
return []
def _get_targets(self):
return []
def __getstate__(self):
state = self.__dict__.copy()
del state["environment"]
return state
def __setstate__(self, state):
self.__dict__.update(state)
self.environment = jinja2.Environment()
self.environment.globals.update(id=__class__._get_id)
| spire/spm/spm_object.py | 1,480 | Abstract Base Class for all SPM objects.
Even though SPM objects are not Spire tasks (as some of them will modify
in-place their file_dep, which is not compatible with doit's task
semantics), they nonetheless include task-related properties: file_dep
and targets. Subclasses will have to override the _get_file_dep and
_get_targets functions to return the correct values. | 373 | en | 0.921671 |
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def removeElements(self, head, val):
"""
:type head: ListNode
:type val: int
:rtype: ListNode
"""
while head:
if head.val == val:
head = head.next
else:
break
if not head:
return head
cur = head
pre = cur
while cur:
if cur.val != val:
pre = cur
cur = cur.next
else:
cur = cur.next
pre.next = cur
return head
| src/main/python/leetcode-python/easy/203.Remove Linked List Elements.py | 702 | :type head: ListNode
:type val: int
:rtype: ListNode
Definition for singly-linked list. | 89 | en | 0.325991 |
# author : Sam Rapier
from deploy_django_to_azure.settings.base import *
DEBUG = False
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
STATIC_URL = 'https://exeblobstorage.blob.core.windows.net/static-files/'
# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'DB-exeterOrientation',
'USER': 'user-admin',
'PASSWORD': 'v%mRn3os#9P2JnjnV*dJ',
'HOST': 'db-exeter-orientation.database.windows.net',
'PORT': '1433',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
'MARS_Connection': 'True',
}
}
} | deploy_django_to_azure/settings/production.py | 744 | author : Sam Rapier Static files (CSS, JavaScript, Images) https://docs.djangoproject.com/en/1.11/howto/static-files/ Database https://docs.djangoproject.com/en/1.11/ref/settings/databases | 188 | en | 0.417629 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.