content
stringlengths 0
894k
| type
stringclasses 2
values |
---|---|
from rest_framework import serializers
from .models import *
class TaskSerializer(serializers.ModelSerializer):
class Meta:
model = Task
fields = ['id', 'title', 'workspace', 'assigned_to', 'priority', 'task_status', 'description', 'planned_start_date', 'planned_end_date', 'file']
class WorkSpaceSerializer(serializers.ModelSerializer):
class Meta:
model = WorkSpace
fields = ['name', 'staff', 'slug']
extra_kwargs = {
'slug':{
'read_only':True,
},
}
class IssueSerializer(serializers.ModelSerializer):
class Meta:
model = Issue
fields = ['id', 'title', 'workspace', 'assigned_to', 'priority', 'issue_status', 'description', 'planned_start_date', 'planned_end_date', 'file']
| python |
import json
import random
from django.utils.safestring import SafeString
# debug
'''
1. Step 1: Put your libraries in the same directory as views.py
2. Step 2: Import your libraries here with a '.'
'''
from .completeness_class import *
from .outlier import *
from .IntegrateFunction import *
from dashboard.forms import CsvUploadForm
from dashboard import models
# Create your views here.
def upload(request):
'''
:param request:
:return: page upload
'''
# Handle file upload
if request.method == 'POST':
isupdate = False
form = CsvUploadForm(request.POST, request.FILES)
# form = CsvUploadForm(request.POST)
if form.is_valid():
# if models.Dataset.objects.filter(NomDataset=form.cleaned_data['nomdataset']):
# isupdate = True
# else:
nomdataset = form.cleaned_data['nomdataset']
cat = models.Categories.objects.get(id=form.cleaned_data['category'])
pays = models.Pays.objects.get(id=form.cleaned_data['pays'])
annee = models.Annee.objects.get(id=form.cleaned_data['annee'])
sep = form.cleaned_data['sep']
newdataset = models.CSV(csv=request.FILES['csv'],
NomDataset=nomdataset,
CatDataset=cat,
PaysDataset=pays,
annee=annee,
sep=sep)
newdataset.save()
# query = models.CSV.objects.raw("select * from dashboard_csv d where d.uploaded_at in " +
# "(select max(uploaded_at) from dashboard_csv " +
# "where NomDataset='" + nomdataset + "' group by NomDataset)")
query = models.CSV.objects.filter(NomDataset=nomdataset).order_by('-uploaded_at').first()
fname = query.csv.name
fname = fname[5:]
return HttpResponseRedirect(reverse('choose_type', args=(fname,)))
else:
form = CsvUploadForm() # A empty, unbound form
# Load documents for the list page
documents = models.CSV.objects.all()
# Render the upload page with the documents and the form
return render(request, 'upload.html', {'documents': documents, 'form': form})
def choixType(request, fname):
df_pre = pd.read_csv('media/csvs/' + fname)
csv = 'csvs/' + fname
nom_dataset = models.CSV.objects.filter(csv=csv).values_list('NomDataset', flat=True).first()
labels = list(df_pre.columns.values)
dfsmall = df_pre[:5]
j = dfsmall.to_json(orient='records')
return render(request, 'choose_type.html',
{'data': SafeString(j), 'fname': fname, 'nom_dataset': nom_dataset, 'labels': labels})
def gettype(request):
if request.method == 'POST':
fname = request.POST.get('fname')
nom_dataset = request.POST.get('nom_dataset')
type = request.POST.getlist('type')
float = request.POST.getlist('float')
boolean = request.POST.getlist('boolean')
date = request.POST.getlist('date')
text = request.POST.getlist('text')
listechoix = request.POST.getlist('listechoix')
reference = []
for i in range(len(float)):
if float[i] != '':
reference.append(float[i])
elif boolean[i] != '':
reference.append(boolean[i])
elif date[i] != '':
reference.append(date[i])
elif text[i] != '':
reference.append(text[i])
elif listechoix[i] != '':
reference.append(listechoix[i])
else:
reference.append('')
df_pre = pd.read_csv('media/csvs/' + fname)
labels = list(df_pre.columns.values)
csv = 'csvs/' + fname
id_csv = models.CSV.objects.filter(csv=csv).values_list('id', flat=True).first()
for i in range(len(labels)):
attrib = models.Attribute()
attrib.NomAttribute = labels[i]
attrib.Format = type[i]
attrib.NomDataset_id = id_csv
attrib.reference = reference[i]
statut = request.POST.get("statut_" + labels[i])
attrib.Statut = statut
attrib.save()
consisV = consistencyFunc(fname)
context = {'fname': fname, 'nom_dataset': nom_dataset, 'labels': labels, 'type': type,
'listechoix': listechoix, 'float': float, 'date': date, 'boolean': boolean, 'text': text,
'consisV': consisV}
return render(request, 'showtype.html', context)
def accueil(request):
categories = models.Categories.objects.all()
pays = models.Pays.objects.all()
datasets = []
dataset = {'csv': '',
'date': '',
'name': '',
'year': '',
'sep': '',
'cat': '',
'pays': '',
'score': 0,
# 'dimensions': ''
'consistency': 0,
'completeness': 0,
'uniqueness': 0,
'validity': 0,
'type': ''
}
'''
Raw query:
select *
from dashboard_csv d
where d.uploaded_at in (
select max(uploaded_at)
from dashboard_csv
group by NomDataset)
'''
query = models.CSV.objects.raw(
'select * from dashboard_csv d where d.uploaded_at in ' +
'(select max(uploaded_at) from dashboard_csv group by NomDataset)')
for res in query:
scores = get_analyse(res.id)
# notes = [random.randint(80, 100) for i in range(4)]
notes = [float(scores['same_data_consistency']), float(scores['completeinfo']) * 100,
100 - float(scores['duplicates_rate']),
float(scores['conform_rate'])]
filename = res.csv.name
fname = filename[5:]
# url = reverse('analyse_individual', args=(fname,))
filetype = detect_file_type(fname)
line = [fname,
res.uploaded_at,
res.NomDataset,
res.annee.annee,
res.sep,
res.CatDataset.NomCategory,
res.PaysDataset.NomPays,
round(sum(notes) / len(notes), 2),
# json.dumps([random.randint(80, 100) for i in range(4)])
] + notes + ['dashboard/img/' + filetype + '.png']
datasets.append(dict(zip(dataset.keys(), line)))
context = {'categories': categories,
'pays': pays, # may be adding truncating to pays in order to display in two columns
'datasets': datasets,
'datasetcount': len(datasets)
}
return render(request, 'accueil.html', context)
def analyseIndi(request, fname):
# If the file name is less than 12, the file is the first upload
if len(fname) <= 12:
data = \
list(models.CSV.objects.filter(csv__startswith='csvs/' + fname).order_by('-uploaded_at')[:1].values('csv'))[
0][
'csv']
filepath = 'media/' + data
idCor = \
list(models.CSV.objects.filter(csv__startswith='csvs/' + fname).order_by('-uploaded_at')[:1].values('id'))[
0][
'id']
# calculate all measures by integrateFunction
# write all measures in dict1
dict1, dict2 = intergrateFunction(filepath, idCor, fname)
return render(request, 'statistics.comment.html',
{'date': dict1['date'], 'sentTotal': dict1['sentTotal'],
'incompleteValues': dict1['incompleteValues'],
'completeValues': dict1['completeValues'], 'consistenValues': dict1['consistenValues'],
'inconsistentValues': dict1['inconsistentValues'], 'duplicates': dict1['duplicates'],
'uniqueValues': dict1['uniqueValues'], 'incorrectValues': dict1['incorrectValues'],
'validValues': dict1['validValues'], 'conversion': dict1['conversion'],
'conversionEmails': dict1['conversionEmails'], 'completeRate': dict1['completeRate'],
'consistenRate': dict1['consistenRate'], 'inconsistenRate': dict1['inconsistenRate'],
'incompleteRate': dict1['incompleteRate'], 'dupRate': dict1['dupRate'],
'uniqunessRate': dict1['uniqunessRate'], 'redundancy': dict1['redundancy'],
'nb_outlier': dict1['nb_outlier'], 'novaluemiss': dict1['novaluemiss'],
'completeInfo': dict1['completeInfo'], 'conformRate': dict1['conformRate'],
'inconformRate': dict1['inconformRate'], 'same_data_consistency': dict1['same_data_consistency']
})
elif len(fname) > 12:
data = \
list(models.CSV.objects.filter(csv__startswith='csvs/' + fname).order_by('-uploaded_at')[:1].values(
'csv'))[0]['csv']
filepath = 'media/' + data
idCor = \
list(models.CSV.objects.filter(csv__startswith='csvs/' + fname).order_by('-uploaded_at')[:1].values(
'id'))[0]['id']
# calculate all measures by integrateFunction
# write all measures in dict1
dict1, dict2 = intergrateFunction(filepath, idCor, fname)
return render(request, 'statistics.comment.html',
{'date': dict1['date'], 'sentTotal': dict1['sentTotal'],
'incompleteValues': dict1['incompleteValues'],
'completeValues': dict1['completeValues'], 'consistenValues': dict1['consistenValues'],
'inconsistentValues': dict1['inconsistentValues'], 'duplicates': dict1['duplicates'],
'uniqueValues': dict1['uniqueValues'], 'incorrectValues': dict1['incorrectValues'],
'validValues': dict1['validValues'], 'conversion': dict1['conversion'],
'conversionEmails': dict1['conversionEmails'], 'completeRate': dict1['completeRate'],
'consistenRate': dict1['consistenRate'], 'inconsistenRate': dict1['inconsistenRate'],
'incompleteRate': dict1['incompleteRate'], 'dupRate': dict1['dupRate'],
'uniqunessRate': dict1['uniqunessRate'], 'redundancy': dict1['redundancy'],
'nb_outlier': dict1['nb_outlier'], 'novaluemiss': dict1['novaluemiss'],
'completeInfo': dict1['completeInfo'], 'conformRate': dict1['conformRate'],
'inconformRate': dict1['inconformRate'], 'same_data_consistency': dict1['same_data_consistency']
})
# 从upload加载过来 url get 传值
# 先计算前端所需的数值,保存到数据库
# 再将这些数据渲染到前端模板 rapport general
def analyseGeneral(request):
# Rapport general
# Analyse par catégorie
ac = []
correctValues = 0
TotalValues = 0
itemAC = {}
catList = list(models.Categories.objects.all().values('id', 'NomCategory'))
for i in range(len(catList)):
itemAC = {}
nameList = list(models.CSV.objects.all().filter(CatDataset_id=catList[i]['id']).values('csv'))
idList = list(models.CSV.objects.all().filter(CatDataset_id=catList[i]['id']).values('id'))
itemAC['Cat'] = catList[i]['NomCategory']
correctValues = 0
TotalValues = 0
for j in range(len(idList)):
itemAnalyse = list(models.Analyse_Specific.objects.all().filter(NomDataset_id=idList[j]['id']).values())
correctValues += pd.read_csv('media/' + nameList[j]['csv']).size * (
float(itemAnalyse[1]['Resultat'])) * 0.01
TotalValues += pd.read_csv('media/' + nameList[j]['csv']).size
itemAC['totalValues'] = int(TotalValues)
itemAC['correctValues'] = int(correctValues)
ac.append(itemAC)
nameList = list(models.CSV.objects.all().values('csv'))
idList = list(models.CSV.objects.all().values('id'))
acJson = {'id': ac}
# Tendances
yearList = list(models.Annee.objects.all().values('id', 'annee'))
item = {}
yearJson = {'id': []}
for i in range(len(yearList)):
item = {}
item['Annee'] = yearList[i]['annee']
nameList1 = list(models.CSV.objects.all().filter(annee_id=yearList[i]['id']).values('csv'))
idList1 = list(models.CSV.objects.all().filter(annee_id=yearList[i]['id']).values('id'))
total = 0
totalRate = 0
for j in range(len(idList1)):
itemAnalyse = list(models.Analyse_Specific.objects.all().filter(NomDataset_id=idList[j]['id']).values())
total += 1
totalRate += float(itemAnalyse[1]['Resultat'])
for q in range(len(idList1)):
itemAnalyse = list(models.Analyse_Specific.objects.all().filter(NomDataset_id=idList[j]['id']).values())
item['Lower'] = 0
if float(itemAnalyse[1]['Resultat']) < totalRate / total:
item['Lower'] += 1
item['Total'] = total
yearJson['id'].append(item)
# Erreurs par dimension
incompleteRate = 0
dupRate = 0
inconformRate = 0
inconsistenRate = 0
# get a list of all id and filename from database
list1 = list(models.CSV.objects.all().values('id', 'csv'))
# count the total number of files uploaded
numCSV = len(list1)
# call for intergrateFunction to calculate the percentage of errors per dimension for each dataset
for i in range(len(list1)):
dict1 = {}
dict2 = {}
idCor = list1[i]['id']
filename = list1[i]['csv'][5:]
filepath = 'media/' + list1[i]['csv']
dict1, dict2 = intergrateFunction(filepath, idCor, filename)
inconsistenRate += dict1['inconsistenRate']
incompleteRate += dict1['incompleteRate']
dupRate += dict1['dupRate']
inconformRate += dict1['inconformRate']
# calculate the average incorrecte rate of each dimension of all datasets
# Cohérence
averageInconsistentRate = round(inconsistenRate / numCSV, 2)
# Complétude
averageIncompleteRate = round(incompleteRate / numCSV, 2)
# Unicité
averageDupRate = round(dupRate / numCSV, 2)
# Validité
averageInconformRate = round(inconformRate / numCSV, 2)
# -------------------------------------------------------------------------------------------------
# Types de fichier en pourcentage
typeCSV = 0
for i in range(len(list1)):
filetype = list1[i]['csv'][:3]
if filetype == "csv":
typeCSV = typeCSV + 1
typePercentage = typeCSV / numCSV * 100
# -------------------------------------------------------------------------------------------------
# Les 5 meilleurs datasets
# call for intergrateFunction to calculate the average score of 4 dimensions for each dataset
list3 = []
itemAverage = {}
for i in range(len(list1)):
itemAverage = {}
idCor = list1[i]['id']
filename = list1[i]['csv'][5:]
filepath = 'media/' + list1[i]['csv']
dict1, dict2 = intergrateFunction(filepath, idCor, filename)
averageScore = round(
(dict1['completeRate'] + dict1['consistenRate'] + dict1['uniqunessRate'] + dict1['conformRate']) / 4, 2)
itemAverage = {'filename': filename, 'averageScore': averageScore, 'url': filename}
list3.append(itemAverage)
inter = {}
flag = False
countFlag = 0
while not flag:
countFlag = 0
for j in range(len(list3) - 1):
if list3[j]['averageScore'] < list3[j + 1]['averageScore']:
countFlag += 1
inter = list3[j]
list3[j] = list3[j + 1]
list3[j + 1] = inter
if countFlag == 0:
flag = True
break
urlOfFile = []
# Contribution graph
data = {'id': []}
for i in range(len(idList)):
itemAnalyse = list(models.Analyse_Specific.objects.all().filter(NomDataset_id=idList[i]['id']).values())
ErrCount = pd.read_csv('media/' + nameList[i]['csv']).size * (100 - float(itemAnalyse[1]['Resultat'])) * 0.01
dupRate = float(itemAnalyse[2]['Resultat'])
item = {'name': nameList[i]['csv'][5:], 'dupliRate': dupRate,
'completeness': itemAnalyse[0]['Resultat'], 'url': nameList[i]['csv'][5:],
'Err': int(ErrCount)}
data['id'].append(item)
urlOfFile.append(item['url'])
datasetJsonString = json.dumps(data)
acJson = json.dumps(acJson)
yearJson = json.dumps(yearJson)
return render(request, 'TBGeneral.html', {'dataSetJson': datasetJsonString, 'acJson': acJson, 'yearJson': yearJson,
'averageInconsistentRate': averageInconsistentRate,
'averageIncompleteRate': averageIncompleteRate,
'averageDupRate': averageDupRate,
'averageInconformRate': averageInconformRate,
'typePercentage': typePercentage,
'list3': list3[0:5],
'urlOfFile': urlOfFile
})
| python |
"""
Released under the MIT-license:
Copyright (c) 2010 Earl Marcus
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.
"""
import socket
import random
import os
#Return Values
CTF_SUCCESS = 0
CTF_FAIL = -1
CTF_INTERR = -2
def pretty(array):
val = []
for i in array:
print ord(i),
print ""
def validate_daemon(ip,port,valid_flag):
#print "Trying to connect to %s:%s" % (ip, port)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((ip,port))
buffer_length = random.randint(40,60)
buff = []
buff.append(buffer_length)
for i in range(buffer_length):
buff.append(random.randint(0,255))
buff[2] = 0xDE
buff[6] = 0x7E
packed_send = ''.join([chr(i) for i in buff])
print buff
#Checksum before
checksum_send = 0
for i in buff:
checksum_send += i
s.send(packed_send)
returned_buffer = s.recv(1024)
s.close()
print returned_buffer
#Checksum after
checksum_recv = 0
for i in returned_buffer:
checksum_recv += ord(i)
if checksum_send != checksum_recv:
print "FAIL"
return CTF_FAIL
print "PASS"
return CTF_SUCCESS
except Exception, e:
raise
finally:
s.close()
return CTF_FAIL
def exploit_daemon(ip,port):
return CTF_INTERR
if __name__ == '__main__':
validate_daemon("172.16.122.132",17999,"0") | python |
""" argparse interface
"""
from argparse import ArgumentParser as _Parser
from argparse import ArgumentDefaultsHelpFormatter as _HelpFormatter
def parser(cmd_str, arg_lst):
""" an argparse parser object
:param cmd_str: the command string
:type cmd_str: str
:param arg_lst: args and kwargs for ArgumentParser.add_argument
:type arg_lst: tuple
:returns: a parser object
:rtype: argparse.ArgumentParser
"""
par = _Parser(prog=cmd_str, formatter_class=_HelpFormatter, add_help=False)
for args, kwargs in arg_lst:
par.add_argument(*args, **kwargs)
return par
def value_dictionary(prs_obj, sysargv):
""" value dictionary for command-line arguments
:param prs_obj: a parser object
:type prs_obj: argparse.ArgumentParser
:param sysargv: sys.argv
:type sysargv: list
"""
val_dct = vars(prs_obj.parse_args(sysargv))
return val_dct
def exit_helpfully(prs_obj):
""" print the help message for a parser object
"""
prs_obj.print_help()
prs_obj.exit()
| python |
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 27 13:40:04 2017
@author: DangoMelon0701
"""
import numpy as np
class Funciones(object):
def __init__(self,nombre,apellido,edad):
self.name = nombre
self.lastname = apellido
self.age = edad
def puto(self):
print("Sabias que {} es un reverendo puto".format(self.name))
def legencoef(n):
p0 = np.array([1])
p1 = np.array([1,0])
if n==0:
return p0
elif n==1:
return p1
else:
for i in range(2,n+1):
pn = ((2*i-1)*np.append(p1,0)-(i-1)*np.append([0,0],p0))/i
p0=p1
p1=pn
return pn
if __name__ == '__main__':
a = Funciones('Alejandro','Condori Alv',22)
a.puto()
b = Funciones('Gerardo','Rivera',21)
b.puto() | python |
from .base_classes import Attack
from .closest_distance import ClosestDistanceAttack
from .direct_linkage import DirectLinkage
from .groundhog import Groundhog
from .utils import load_attack | python |
from django.shortcuts import render,redirect
from oauth_backend import OauthBackend
from django.http import HttpResponse, HttpResponseForbidden
from django.http import Http404
from django.utils.crypto import get_random_string
from django.conf import settings
from django.contrib.auth import authenticate, login
from tukey.models import UnregisteredUser
import urllib,json,requests
from openstack_auth.exceptions import KeystoneAuthException
backend=OauthBackend()
def index(request):
'''
Login entry for google oauth2.0, an antiforgery token is created
and user is redirected to google oauth endpoint
'''
state=get_random_string(length=32)
parameters=settings.OAUTH['parameters'].copy()
parameters['state']=state
request.session['oauth_state']=state
request.session['next']=request.GET.get('next','/project')
return redirect(settings.OAUTH['auth_uri']+"?"+\
urllib.urlencode(parameters))
def oauth2callback(request):
'''
Endpoint for google oauth2.0 callback, the antiforgery token is checked,
then tukey talk to google using the code in the request, and exchange user
information from google, user email is extracted from id_token
'''
if request.session.get('oauth_state','')==request.GET['state']:
token=backend.getToken(request.GET.get('code',''))
if token.has_key('id_token'):
email=backend.decode(token['id_token'])
else:
return render(request,'403.html',{},status=403)
try:
user=authenticate(password=settings.TUKEY_PASSWORD,username='openid %s' % email,\
auth_url=settings.OPENSTACK_KEYSTONE_URL,request=request)
user.identifier=email
if user!=None and user.is_active:
login(request,user)
return redirect(request.session.get('next','/project'))
#create unregistered user if user is not authorized in keystone,
#and redirect user to apply page
except KeystoneAuthException:
user=UnregisteredUser('OpenId',email)
from tukey.webforms.views import osdc_apply
return osdc_apply(request, user)
else:
return render(request,'403.html',{},status=403)
| python |
from pyramid.view import view_defaults
from pyramid.response import Response
from pyramid.httpexceptions import HTTPOk
from pyramid.httpexceptions import HTTPNotFound, HTTPInternalServerError
from .. catalog import install_package
from .. logger import getLogger
logger = getLogger(__name__)
@view_defaults(route_name='catalog_item')
class CatalogItemViews(object):
def __init__(self, request):
self.request = request
def install(self):
"""
Install a package from the global catalog into the local catalog
"""
logger.info("======= install =======")
try:
package_name = self.request.params['package_name']
if package_name is not None and len(package_name) > 1:
install_package(package_name)
return HTTPOk
else:
return HTTPInternalServerError(
explanation="Package name must be specified")
except Exception as e:
message = "Exception installing a package to the local catalog"
logger.exception(message)
details = "Details: {0}".format(e)
return HTTPInternalServerError(
explanation=message, details=details)
| python |
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import gridspec
from sklearn.metrics import accuracy_score
# Plot some details about the dataset and show some example points
def showDatasetExamples(xTrain, yTrain, xTest, yTest):
fig = plt.figure(figsize=(6, 6))
fig.canvas.set_window_title('MINIST Dataset Examples')
gs = gridspec.GridSpec(3, 1, height_ratios=[1, 1, 6])
# Subplot "Summary"
ax_summary = plt.subplot(gs[0])
ax_summary.set_xticks([])
ax_summary.set_yticks([])
ax_summary.set_title('Dataset Summary', fontsize=20, fontweight='bold')
ax_summary.axis('off')
ax_summary.axhline(1.0, color='black')
ax_summary_text_size = 12
ax_summary_mono = {'family' : 'monospace'}
ax_summary.text(0.14, 0.6, "Each image size: 28*28*1", fontsize=ax_summary_text_size, fontdict=ax_summary_mono)
ax_summary.text(0.14, 0.3, "Train set image numbers: {}".format(xTrain.shape[0]), fontsize=ax_summary_text_size, fontdict=ax_summary_mono)
ax_summary.text(0.14, 0.0, "Test set image numbers: {}".format(xTest.shape[0]), fontsize=ax_summary_text_size, fontdict=ax_summary_mono)
# Subplot "Examples"
ax_examples = plt.subplot(gs[2])
ax_examples.set_xticks([])
ax_examples.set_yticks([])
ax_examples.set_title('Dataset Examples', fontsize=20, fontweight='bold')
ax_examples.axis('off')
ax_examples.axhline(1.0, color='black')
ax_examples_inners = gridspec.GridSpecFromSubplotSpec(3, 5, gs[2], wspace=0.1, hspace=0.1)
for i in range(ax_examples_inners.nrows):
for j in range(ax_examples_inners.ncols):
ax = fig.add_subplot(ax_examples_inners[i, j])
ax.set_xticks([])
ax.set_yticks([])
index = i * ax_examples_inners.nrows + j
ax.imshow(xTrain[index], cmap='binary', interpolation='nearest')
ax.text(0.05, 0.05, str(yTrain[index]), transform=ax.transAxes, color='green')
plt.show()
# Define model
class MyModel(tf.keras.Model):
def __init__(self):
super(MyModel, self).__init__()
self.conv1 = tf.keras.layers.Conv2D(32, 3, activation='relu', )
self.flatten = tf.keras.layers.Flatten()
self.dense1 = tf.keras.layers.Dense(128, activation='relu')
self.dense2 = tf.keras.layers.Dense(10)
def call(self, x):
x = self.conv1(x)
x = self.flatten(x)
x = self.dense1(x)
x = self.dense2(x)
return x
# Train a batch
# @iamges shape with (batch, width. height, channels)
# @labels shape with (labels)
@tf.function
def train_step(images, labels):
with tf.GradientTape() as tape:
predictions = model(images, training=True)
loss = loss_object(labels, predictions)
gradients = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
train_loss(loss)
train_accuracy(labels, predictions)
@tf.function
def test_step(images, labels):
predictions = model(images, training=False)
t_loss = loss_object(labels, predictions)
test_loss(t_loss)
test_accuracy(labels, predictions)
# Download MNIST dataset
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# Show examples
#showDatasetExamples(x_train, y_train, x_test, y_test)
# Prepare the data
x_train = x_train / 255# Normalize
x_test = x_test / 255
x_train = x_train[..., tf.newaxis]# (60000, 28, 28, ) to (60000, 28, 28, 1)
x_test = x_test[..., tf.newaxis]
train_ds = tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(10000).batch(32)# Contruct "Dataset" structure using the data
test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32)
model = MyModel()
loss_object = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
optimizer = tf.keras.optimizers.Adam()
# Select metrics to measure the loss and the accuracy of the model
train_loss = tf.keras.metrics.Mean(name='train_loss')
train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='train_accuracy')
test_loss = tf.keras.metrics.Mean(name='test_loss')
test_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='test_accuracy')
EPOCHS = 5
history = {
'loss': np.zeros(EPOCHS),
'accuracy': np.zeros(EPOCHS),
'val_loss': np.zeros(EPOCHS),
'val_accuracy': np.zeros(EPOCHS)
}
for epoch in range(EPOCHS):
# Reset the metrics at the start of the next epoch
train_loss.reset_states()
train_accuracy.reset_states()
test_loss.reset_states()
test_accuracy.reset_states()
for images, labels in train_ds:
# tf.config.experimental_run_functions_eagerly(True)
train_step(images, labels)
# tf.config.experimental_run_functions_eagerly(False)
for test_images, test_labels in test_ds:
test_step(test_images, test_labels)
template = 'Epoch {}, Loss: {}, Accuracy: {}, Test Loss: {}, Test Accuracy: {}'
print(template.format(
epoch + 1,
train_loss.result(),
train_accuracy.result() * 100,
test_loss.result(),
test_accuracy.result() * 100
))
history['loss'][epoch] = train_loss.result()
history['accuracy'][epoch] = train_accuracy.result()
history['val_loss'][epoch] = test_loss.result()
history['val_accuracy'][epoch] = test_accuracy.result()
# Test
model.summary()
# for i in range(10):
# print(str(y_test[i]))
# inputs = x_test[i]
# inputs = inputs[tf.newaxis, ...]
# prediction = model(inputs, training=False)
# print(np.argmax(prediction))
plt.plot(history['accuracy'])
plt.plot(history['val_accuracy'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Val'], loc='upper left')
plt.show()
plt.plot(history['loss'])
plt.plot(history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Val'], loc='upper left')
plt.show()
| python |
from rest_framework.serializers import ModelSerializer
from backend.models import Video, Like
class VideoCreateSerializer(ModelSerializer):
class Meta:
model = Video
fields = [
'id',
'owner',
'video_bucket_id',
'title',
'description',
'thumbnail',
]
class VideoListSerializer(ModelSerializer):
class Meta:
model = Video
fields = '__all__'
class VideoUpdateSerializer(ModelSerializer):
class Meta:
model = Video
fields = [
'id',
'title',
'description',
'thumbnail',
]
class VideoDeleteSerializer(ModelSerializer):
class Meta:
model = Video
fields = '__all__'
| python |
import aiml
from django.shortcuts import render, redirect
kernel = aiml.Kernel()
kernel.learn("./botbrains/*.aiml")
kernel.saveBrain("siabrain.brn")
def index(request):
text = ""
textreply = ""
text = chat.text
textreply = kernel.respond(str(text))
if textreply is not None:
return render(request, "chat.html", {'message': textreply, 'send': text, })
else :
textreply = "I don't understand"
return render(request, "chat.html", {'message': textreply, 'send': text, })
def chat(request):
chat.text = request.POST.get('text')
print(chat.text)
return redirect("index")
| python |
import falcon.asgi
from .api.tilt_resource import *
# swagger ui - NO ASGI SUPPORT YET
#from falcon_swagger_ui import register_swaggerui_app
# register swagger ui - NO ASGI SUPPORT YET
#register_swaggerui_app(api, SWAGGERUI_URL, SCHEMA_URL, page_title=PAGE_TITLE,
#favicon_url=FAVICON_URL,
# config={'supportedSubmitMethods': ['get', 'post']}
#)
# falcon.asgi.APP instances are callable ASGI apps
app = falcon.asgi.App()
#
res = TILTResource()
app.add_route('/update', res, suffix='update')
app.add_route('/update/{domain}', res, suffix='updateDomain')
app.add_route('/{domain}', res, suffix='domain')
app.add_route('/calculate', res, suffix='calculate')
app.add_route('/calculateRisk/{domain}', res, suffix='calculateRiskDomain')
#app.add_route('/calculateRisks', res, suffix='calculateRisks')
app.add_route('/deleteGraph', res, suffix='deleteGraph')
app.add_route('/deleteProperties', res, suffix='deleteProperties')
app.add_route('/deleteCollection/{collection}', res, suffix='deleteCollection')
app.add_route('/generate/{i}', res, suffix='generate')
app.add_route('/path', res, suffix='path') | python |
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
def mlp_model(x, n_input, n_hidden_1, n_hidden_2, n_class):
weights = {
'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),
'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
'out': tf.Variable(tf.random_normal([n_hidden_2, n_class]))
}
bias = {
'b1': tf.Variable(tf.random_normal([n_hidden_1])),
'b2': tf.Variable(tf.random_normal([n_hidden_2])),
'out': tf.Variable(tf.random_normal([n_class]))
}
layer_1 = tf.add(tf.matmul(x, weights['h1']), bias['b1'])
layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), bias['b2'])
layer_out = tf.add(tf.matmul(layer_2, weights['out']), bias['out'])
return layer_out
if __name__ == '__main__':
n_class = 10
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
X = tf.placeholder('float', shape=[None, 784])
Y = tf.placeholder('float', shape=[None, 10])
logits = mlp_model(X, 784, 256, 256, 10)
loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=Y))
learning_rate = 0.01
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
train_op = optimizer.minimize(loss_op)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
batch_size = 100
epoches = 15
display_step = 1
for epoch in range(epoches):
avg_cost = 0
total_batch = int(mnist.train.num_examples / batch_size)
for i in range(total_batch):
batch_x, batch_y = mnist.train.next_batch(batch_size)
# Run optimization op (backprop) and cost op (to get loss value)
_, c = sess.run([train_op, loss_op], feed_dict={X: batch_x, Y: batch_y})
# Compute average loss
avg_cost += c / total_batch
# Display logs per epoch step
if epoch % display_step == 0:
print("Epoch:", '%04d' % (epoch+1), "cost={:.9f}".format(avg_cost))
pred = tf.nn.softmax(logits) # Apply softmax to logits
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(Y, 1))
# Calculate accuracy
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print("Accuracy:", accuracy.eval({X: mnist.test.images, Y: mnist.test.labels})) | python |
#encoding:utf-8
import word_util
import numpy as np
import codecs
def transform_wordseq_to_phrase_weighted(word_seq,word2vec_map,word_weighted_value = None,word_keys = None):
phrase_distributed = np.zeros(256)
word_freq = {}
for word in word_seq:
#print("0")
if not word_keys:
if word not in word_keys:
continue
#print("1")
if not word_weighted_value:
phrase_distributed += word2vec_map[word]
else:
if word not in word_weighted_value:
#print(word)
continue
#print(word2vec_map[word])
#print(word_weighted_value[])
#print(word2vec_map[word])
#print(word_weighted_value[word])
if word in word_freq:
word_freq[word] += 1
else:
word_freq[word] = 1
for word in word_freq:
weight = word_weighted_value[word]*word_freq[word]/len(word_freq)
phrase_distributed += [word2vec_elem*weight for word2vec_elem in word2vec_map[word]]
#print('2')
sum_vec = np.sum(phrase_distributed**2)
if sum_vec<= 1e-4 and sum_vec >=-1e-4:
return phrase_distributed;
return np.divide(phrase_distributed,np.sqrt(np.sum(phrase_distributed**2)))
def build_questions_vector_hashmap(phrase_embedding_file,question_count,has_head = False):
dict_prase_vec = {}
with codecs.open(phrase_embedding_file, 'r', 'utf-8') as p_read:
count = 0
while True:
line = p_read.readline()
if not line:
print('load %s finised' % phrase_embedding_file)
break
if has_head:
pass
has_head = False
continue
count += 1
if count % 1000 == 0:
print('load train sample %s' % count)
phrase_id, phrase_vec= line.split('\t')
phrase_vec = [float(i) for i in phrase_vec.split(',')]
dict_prase_vec[phrase_id] = phrase_vec
if count >= question_count:
break
print(count)
return dict_prase_vec
def bulid_question_topic_hashmap(question_topic_file, has_head = False):
dict_question_topic = {}
with codecs.open(question_topic_file,'r', 'utf-8') as question_topic_read:
#no head
while True:
line = question_topic_read.readline()
if not line:
print('read q_t finished !')
break
q_id, t_s = line.split('\t')
t_arr = t_s.strip().split(',')
dict_question_topic[q_id] = t_arr
print('load %s finished' % question_topic_file)
return dict_question_topic
if __name__ == "__main__":
question_40000_file = '../out/random_40000_question.txt'
question_40000_phrase_distributed_file = '../out2/random_40000_question_embedding.txt'
#question_train_file = '../data/question_train_set.txt'
#question_train_phrase_vector_file = '../out/question_train_phrase_set.txt'
question_eval_file = '../data/question_eval_set.txt'
question_eval_phrase_vector_file = '../out2/question_eval_phrase_set.txt'
word_embedding_file = '../data/word_embedding.txt'
word2vec_map = word_util.build_word2vec_hashmap(word_embedding_file,has_head=True)
word_tfidf_file = '../out2/global_idf.txt'
word_weighted_tfidf = word_util.build_word_tfidf_hashmap(word_tfidf_file)
word_keys_file = '../out2/word_keys.txt'
word_keys = word_util.build_word_keys_hashmap(word_keys_file)
p_write = codecs.open(question_40000_phrase_distributed_file, 'w', 'utf-8')
#eval_write = codecs.open(filename)
#train_write = codecs.open(question_train_phrase_vector_file, 'w','utf-8')
eval_write = codecs.open(question_eval_phrase_vector_file, 'w', 'utf-8')
count = 0
with codecs.open(question_40000_file, 'r', 'utf-8') as train_read:
while True:
line = train_read.readline()
if not line:
print("read %s finised! " % question_40000_phrase_distributed_file)
break
q_id,q_w_seq,c_w_seq = line.split('\t')
#print(q_id)
#print(q_w_seq)
q_w_seq = q_w_seq.split(',')
#print(c_w_seq)
q_w = transform_wordseq_to_phrase_weighted(q_w_seq, word2vec_map,word_weighted_tfidf,word_keys)
#print(q_w)
q_w = [str(e) for e in q_w.tolist()]
p_write.write(q_id +'\t' + ','.join(q_w)+'\n')
count += 1
if count % 10000 == 0:
print('train transform count: %d' % count)
print('train set finised')
# count = 0
# with codecs.open(question_train_file, 'r', 'utf-8') as train_read:
# while True:
# line = train_read.readline()
# if not line:
# print("read %s finised! " % question_train_file)
# break
# q_id,_,q_w_seq,_,c_w_seq = line.split('\t')
# #print(q_id)
# #print(q_w_seq)
# q_w_seq = q_w_seq.split(',')
# #print(c_w_seq)
# q_w = transform_wordseq_to_phrase_weighted(q_w_seq, word2vec_map,word_weighted_tfidf,word_keys)
# #print(q_w)
# q_w = [str(e) for e in q_w.tolist()]
# train_write.write(q_id +'\t' + ','.join(q_w)+'\n')
# count += 1
# if count % 10000 == 0:
# print('train transform count: %d' % count)
# print('train set finised')
count = 0
with codecs.open(question_eval_file, 'r', 'utf-8') as eval_read:
while True:
line = eval_read.readline()
if not line:
print("read %s finised! " % question_eval_file)
break
q_id,_,q_w_seq,_,c_w_seq = line.split('\t')
#print(q_id)
#print(q_w_seq)
q_w_seq = q_w_seq.split(',')
#print(c_w_seq)
q_w = transform_wordseq_to_phrase_weighted(q_w_seq, word2vec_map,word_weighted_tfidf,word_keys)
#print(q_w)
q_w = [str(e) for e in q_w.tolist()]
eval_write.write(q_id +'\t' + ','.join(q_w)+'\n')
count +=1
if count % 10000 == 0:
print('eval transform count: %d' % count)
print('eval set finised')
| python |
#!/usr/bin/env python3
import contextlib
import sys
from pathlib import Path
from typing import List, Type
import pytest
from qemu import QemuVm, VmImage, spawn_qemu
from nix import notos_image, busybox_image
from root import TEST_ROOT
from vmsh import spawn_vmsh_command, VmshPopen
sys.path.append(str(TEST_ROOT.parent))
class Helpers:
@staticmethod
def root() -> Path:
return TEST_ROOT
@staticmethod
def notos_image() -> VmImage:
return notos_image()
@staticmethod
def busybox_image() -> "contextlib._GeneratorContextManager[Path]":
return busybox_image()
@staticmethod
def spawn_vmsh_command(
args: List[str], cargo_executable: str = "vmsh"
) -> VmshPopen:
return spawn_vmsh_command(args, cargo_executable)
@staticmethod
def run_vmsh_command(args: List[str], cargo_executable: str = "vmsh") -> VmshPopen:
proc = spawn_vmsh_command(args, cargo_executable)
assert proc.wait() == 0
return proc
@staticmethod
def spawn_qemu(
image: VmImage, extra_args: List[str] = []
) -> "contextlib._GeneratorContextManager[QemuVm]":
return spawn_qemu(image, extra_args)
@pytest.fixture
def helpers() -> Type[Helpers]:
return Helpers
| python |
# -*- coding: utf-8 -*-
"""
SPARQL Wrapper exceptions
@authors: U{Ivan Herman<http://www.ivan-herman.net>}, U{Sergio Fernández<http://www.wikier.org>}, U{Carlos Tejo Alonso<http://www.dayures.net>}
@organization: U{World Wide Web Consortium<http://www.w3.org>} and U{Foundation CTIC<http://www.fundacionctic.org/>}.
@license: U{W3C® SOFTWARE NOTICE AND LICENSE<href="http://www.w3.org/Consortium/Legal/copyright-software">}
"""
class SPARQLWrapperException(Exception):
"""
Base class for SPARQL Wrapper exceptions
"""
msg = "an exception has occurred"
def __init__(self, response=None):
if response:
formatted_msg = "%s: %s. \n\nResponse:\n%s" % (self.__class__.__name__, self.msg, response)
else:
formatted_msg = "%s: %s." % (self.__class__.__name__, self.msg)
super(SPARQLWrapperException, self).__init__(formatted_msg)
class EndPointInternalError(SPARQLWrapperException):
"""
Exception type for 500 Internal Server Error responses. Usually HTTP response status code 500.
"""
msg = "endpoint returned code 500 and response"
class QueryBadFormed(SPARQLWrapperException):
"""
Query Bad Formed exception. Usually HTTP response status code 400.
"""
msg = "a bad request has been sent to the endpoint, probably the sparql query is bad formed"
class EndPointNotFound(SPARQLWrapperException):
"""
End Point Not Found exception. Usually HTTP response status code 404.
"""
msg = "it was impossible to connect with the endpoint in that address, check if it is correct"
class Unauthorized(SPARQLWrapperException):
"""
Access is denied due to invalid credentials (unauthorized). Usually HTTP response status code 401.
@since: 1.8.2
"""
msg = "access is denied due to invalid credentials (unauthorized). Check the credentials"
class URITooLong(SPARQLWrapperException):
"""
The URI requested by the client is longer than the server is willing to interpret. Usually HTTP response status code 414.
@since: 1.8.3
"""
msg = "the URI requested by the client is longer than the server is willing to interpret. Check if the request was sent using GET method instead of POST method."
| python |
import warnings
from otp.ai.passlib.tests.test_crypto_builtin_md4 import _Common_MD4_Test
__all__ = [
'Legacy_MD4_Test']
class Legacy_MD4_Test(_Common_MD4_Test):
descriptionPrefix = 'passlib.utils.md4.md4()'
def setUp(self):
super(Legacy_MD4_Test, self).setUp()
warnings.filterwarnings('ignore', '.*passlib.utils.md4.*deprecated', DeprecationWarning)
def get_md4_const(self):
from otp.ai.passlib.utils.md4 import md4
return md4 | python |
"""
leetcode 15
Three Sum
"""
from typing import List
"""
simple solution
T: O(N^3)
S: O(1)
result: time out
"""
def threeSum(self, nums: List[int]) -> List[List[int]]:
if not nums:
return []
res = []
for i in range(len(nums) - 2):
for j in range(i, len(nums) - 1):
for k in range(j, len(nums)):
if nums[i] + nums[j] + nums[k] == 0 and sorted(nums[i], nums[j], nums[k]) not in res:
res.append(sorted(nums[i], nums[j], nums[k]))
return res
"""
better solution
T: O(N^2)
S: O(N)
Note: solution premise is the array elements can be change the order.
"""
def threeSum(self, nums: List[int]) -> List[List[int]]:
if not nums or len(nums) < 3:
return []
res = set()
nums.sort()
for i, a in enumerate(nums[:-2]):
if i >= 1 and a == nums[i-1]:
continue
s = set()
for b in nums[i+1:]:
if b not in s:
s.add(-a-b)
else:
res.add((a, -a-b, b))
return map(list, res)
"""
better better solution
T: O(N^2)
S: O(1)
"""
def threeSum(self, nums: List[int]) -> List[List[int]]:
if not nums and len(nums) < 3:
return []
nums.sort()
res = []
for i, a in enumerate(nums[:-2]):
if i >= 1 and a == nums[i-1]:
continue
l, r = i + 1, len(nums) - 1
while l < r:
sum = nums[i] + nums[l] + nums[r]
if sum > 0:
r -= 1
elif sum < 0:
l += 1
else:
res.append((nums[i], nums[l], nums[r]))
while l < r and nums[l+1] == nums[l]:
l += 1
while l < r and nums[r-1] == nums[r]:
r -= 1
l += 1
r -= 1
return map(list, res)
| python |
# This file was auto generated; Do not modify, if you value your sanity!
import ctypes
try: # 3
from can_settings import can_settings
from canfd_settings import canfd_settings
from s_text_api_settings import s_text_api_settings
except:
from ics.structures.can_settings import can_settings
from ics.structures.canfd_settings import canfd_settings
from ics.structures.s_text_api_settings import s_text_api_settings
# flags
class flags(ctypes.Structure):
_pack_ = 2
_fields_ = [
('disableUsbCheckOnBoot', ctypes.c_uint32, 1), # [Bitfield]
('enableLatencyTest', ctypes.c_uint32, 1), # [Bitfield]
('reserved', ctypes.c_uint32, 30), # [Bitfield]
]
# Extra names go here:
# End of extra names
class secu_avb_settings(ctypes.Structure):
_pack_ = 2
_anonymous_ = ("flags",)
_fields_ = [
('perf_en', ctypes.c_uint16),
('can1', can_settings),
('canfd1', canfd_settings),
('can2', can_settings),
('canfd2', canfd_settings),
('network_enables', ctypes.c_uint64),
('termination_enables', ctypes.c_uint64),
('pwr_man_timeout', ctypes.c_uint32),
('pwr_man_enable', ctypes.c_uint16),
('network_enabled_on_boot', ctypes.c_uint16),
('iso15765_separation_time_offset', ctypes.c_int16),
('text_api', s_text_api_settings),
('flags', flags),
]
# Extra names go here:
ECU_AVBSettings = secu_avb_settings
SECU_AVBSettings = secu_avb_settings
# End of extra names
| python |
class Grid:
""" Creates a 2D array specified by row and column """
def __init__(self, X_SIZE, Y_SIZE, item=""):
self.x_size = X_SIZE
self.y_size = Y_SIZE
self._grid = [[item for x in range(X_SIZE)]
for y in range(Y_SIZE)]
def __len__(self):
return sum([len(element) for element in self._grid])
def __getitem__(self, position):
return self._grid[position]
def __setitem__(self, index, value):
self._grid[index] = value
def __repr__(self):
return "\n".join([''.join(['{:2}'.format(item) for item in row])
for row in self._grid])
| python |
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def insertionSortList(self, head: ListNode) -> ListNode:
if not head or not head.next:
return head
dummy = ListNode(next=head)
ptr = head
cur = head.next
while cur:
val = cur.val
if val >= ptr.val:
ptr = ptr.next
else:
prev = dummy
while prev.next.val < val:
prev = prev.next
ptr.next = cur.next
cur.next = prev.next
prev.next = cur
cur = ptr.next
return dummy.next
| python |
import os
import sys
import csv
import json
OUTPUT_FORMATS = ('csv', 'json', 'yara', 'autofocus')
def getHandler(output_format):
output_format = output_format.lower()
if output_format not in OUTPUT_FORMATS:
print("[WARNING] Invalid output format specified.. using CSV")
output_format = 'csv'
handler_format = "OutputHandler_" + output_format
handler_class = getattr(sys.modules[__name__], handler_format)
return handler_class()
class OutputHandler(object):
def print_match(self, fpath, page, name, match, last = False):
pass
def print_header(self, fpath):
pass
def print_footer(self, fpath):
pass
def print_error(self, fpath, exception):
print("[ERROR] %s" % (exception))
class OutputHandler_csv(OutputHandler):
def __init__(self):
self.csv_writer = csv.writer(sys.stdout, delimiter = '\t')
def print_match(self, fpath, page, name, match):
self.csv_writer.writerow((fpath, page, name, match))
def print_error(self, fpath, exception):
self.csv_writer.writerow((fpath, '0', 'error', exception))
class OutputHandler_json(OutputHandler):
def print_match(self, fpath, page, name, match):
data = {
'path' : fpath,
'file' : os.path.basename(fpath),
'page' : page,
'type' : name,
'match': match
}
print(json.dumps(data))
def print_error(self, fpath, exception):
data = {
'path' : fpath,
'file' : os.path.basename(fpath),
'type' : 'error',
'exception' : exception
}
print(json.dumps(data))
class OutputHandler_yara(OutputHandler):
def __init__(self):
self.rule_enc = ''.join(chr(c) if chr(c).isupper() or chr(c).islower() or chr(c).isdigit() else '_' for c in range(256))
def print_match(self, fpath, page, name, match):
if name in self.cnt:
self.cnt[name] += 1
else:
self.cnt[name] = 1
string_id = "$%s%d" % (name, self.cnt[name])
self.sids.append(string_id)
string_value = match.replace('\\', '\\\\')
print("\t\t%s = \"%s\"" % (string_id, string_value))
def print_header(self, fpath):
rule_name = os.path.splitext(os.path.basename(fpath))[0].translate(self.rule_enc)
print("rule %s" % (rule_name))
print("{")
print("\tstrings:")
self.cnt = {}
self.sids = []
def print_footer(self, fpath):
cond = ' or '.join(self.sids)
print("\tcondition:")
print("\t\t" + cond)
print("}")
class OutputHandler_autofocus(OutputHandler):
def __init__(self):
self.rule_enc = ''.join(chr(c) if chr(c).isupper() or chr(c).islower() or chr(c).isdigit() else '_' for c in range(256))
def print_match(self, fpath, page, name, match):
string_value = match.replace('hxxp', 'http').replace('\\', '\\\\')
if name == "MD5":
auto_focus_query = '{"field":"sample.md5","operator":"is","value":\"%s\"},' % (string_value)
elif name == "SHA1":
auto_focus_query = '{"field":"sample.sha1","operator":"is","value":\"%s\"},' % (string_value)
elif name == "SHA256":
auto_focus_query = '{"field":"sample.sha256","operator":"is","value":\"%s\"},' % (string_value)
elif name == "URL":
auto_focus_query = '{"field":"sample.tasks.connection","operator":"contains","value":\"%s\"},' % (string_value)
elif name == "Host":
auto_focus_query = '{"field":"sample.tasks.dns","operator":"contains","value":\"%s\"},' % (string_value)
elif name == "Registry":
#auto_focus_query = '{"field":"sample.tasks.registry","operator":"is","value":\"%s\"},' % (string_value)
return
elif name == "Filepath":
#auto_focus_query = '{"field":"sample.tasks.file","operator":"is","value":\"%s\"},' % (string_value)
return
elif name == "Filename":
#auto_focus_query = '{"field":"alias.filename","operator":"is","value":\"%s\"},' % (string_value)
return
elif name == "Email":
#auto_focus_query = '{"field":"alias.email","operator":"is","value":\"%s\"},' % (string_value)
return
elif name == "IP":
auto_focus_query = '{"field":"sample.tasks.connection","operator":"contains","value":\"%s\"},' % (string_value)
elif name == "CVE":
return
print(auto_focus_query)
def print_header(self, fpath):
rule_name = os.path.splitext(os.path.basename(fpath))[0].translate(self.rule_enc)
print("AutoFocus Search for: %s" % (rule_name))
print('{"operator":"Any","children":[')
def print_footer(self, fpath):
rule_name = os.path.splitext(os.path.basename(fpath))[0].translate(self.rule_enc)
print('{"field":"sample.tag","operator":"is in the list","value":[\"%s\"]}]}' % (rule_name))
| python |
# CONVERSION OF LINKED LIST TO ARRAY
class Node:
def __init__(self, value):
self.value = value
self.next = None
class SinglyLinkedList:
def __init__(self):
self.head = None
self.tail = None
def add_last(self, value):
temp = Node(value)
if self.head==None:
self.head = temp
self.tail = temp
else:
self.tail.next = temp
self.tail = self.tail.next
def print(self):
temp = self.head
while(temp != None):
print(temp.value, end=" ")
temp = temp.next
print()
def to_array(self):
arr = list()
temp = self.head
i=0
while(temp != None):
arr.append(temp.value)
temp = temp.next
i += 1
return arr
if __name__ == "__main__":
sll = SinglyLinkedList()
sll.add_last(5)
sll.add_last(10)
sll.add_last(15)
sll.add_last(20)
sll.print()
# converting to array
array_ll = sll.to_array()
print(array_ll)
| python |
from dataclasses import dataclass, field
from typing import List, Any, Optional
@dataclass()
class Type:
"""
Abstract base representation of a data type.
All intermediate representations of data types will either be instances of Type, or instances of subclasses of Type.
All scalar data types are instances of Type (ex. Type('str') represents a `str`, Type('float') represents a `float`
All complex data types are instances of subclasses of Type (ex Sequence('list', types=[Type('str')]) represents a
list which holds strings... a List[str])
"""
name: str
def __hash__(self):
return hash(self.name)
@dataclass
class Sequence(Type):
types: List[Type]
@dataclass(init=False)
class HashTable(Type):
name: str = field(default='dict', init=False)
key: Type
values: List[Type]
def __init__(self, key: Type, values: List[Type]):
self.name = 'dict'
self.key = key
self.values = values
@dataclass
class DataClass(Type):
"""Will become a Dataclass definition"""
members: List['Member']
methods = None
@dataclass
class Member:
name: str
types: List[Type]
# The strings in this set are string representations of python types (ie `str`, `int`, `bool`, `None`).
# This can also include names of generated data classes (ie)
default: Any = None
# Default value to give to new instances of the dataclass
optional: bool = False
# whether to treat serializer a null value for this member or a missing instance of this member as acceptable
custom_field: Optional[str] = None
# custom marshmallow serializer field to use for handling this member
@dataclass
class ResultSet:
"""
A ResultSet is an object which holds all the data and metadata necessary to generate a complete output artifact
for a given backend (all the info necessary to create a python module using the py_dataclass backend, for example
"""
dataclasses: List[DataClass]
preamble: str = ''
# preamble is the stuff that goes in between the import statements
| python |
# -*- coding: utf-8 -*-
# Created by crazyX on 2018/7/7
from ojcrawler.crawlers.poj import POJ
from ojcrawler.crawlers.hdu import HDU
from ojcrawler.crawlers.codeforces import Codeforces
supports = {
'poj': POJ,
'hdu': HDU,
'codeforces': Codeforces,
}
| python |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = u"David Pärsson"
__copyright__ = u"Copyright 2015, David Pärsson"
__license__ = "MIT"
__version__ = "1.0.0"
__status__ = "Development"
import re
import sys
import argparse
def renumber(input_filename, output_filename):
with open(input_filename, 'r') as input_file:
with open(output_filename, 'w') as output_file:
renumber_file(input_file, output_file)
def renumber_file(input_file, output_file):
revision_number_regex = re.compile("^Revision-number: (\d+)$")
node_copyfrom_rev_regex = re.compile("^Node-copyfrom-rev: (\d+)$")
known_revisions = []
for line in input_file:
revision_match = revision_number_regex.match(line)
if revision_match:
known_revisions.append(int(revision_match.group(1)))
copyfrom_match = node_copyfrom_rev_regex.match(line)
if copyfrom_match:
copyfrom_revision = int(copyfrom_match.group(1))
if not copyfrom_revision in known_revisions:
existing_revision = max(filter(lambda item: item < copyfrom_revision, known_revisions))
sys.stderr.write("Remapping: %d -> %d\n" % (copyfrom_revision, existing_revision))
output_file.write(line.replace(str(copyfrom_revision), str(existing_revision)))
continue
output_file.write(line)
def main():
parser = argparse.ArgumentParser(description="Modifies Node-copyfrom-revision to existing revisions in Subversion dumps")
try:
parser.add_argument("--input", "-i", type=str, required=True, metavar='FILE',
help='existing svn dump file to process')
parser.add_argument("--output", "-o", type=str, required=True, metavar='FILE',
help='output file')
options = parser.parse_args()
except ValueError:
parser.print_help()
return 1
return renumber(options.input, options.output)
if __name__ == '__main__':
sys.exit(main())
| python |
# Copyright (c) 2013 Shotgun Software Inc.
#
# CONFIDENTIAL AND PROPRIETARY
#
# This work is provided "AS IS" and subject to the Shotgun Pipeline Toolkit
# Source Code License included in this distribution package. See LICENSE.
# By accessing, using, copying or modifying this work you indicate your
# agreement to the Shotgun Pipeline Toolkit Source Code License. All rights
# not expressly granted therein are reserved by Shotgun Software Inc.
from tank import Hook
class HieroUpdateVersionData(Hook):
""" Update the data dictionary for a Version to be created in Shotgun. """
def execute(self, version_data, task, **kwargs):
"""
Update the version_data dictionary to change the data for the Version
that will be created in Shotgun.
"""
pass
| python |
#!/usr/bin/env python
import scapy.all as scapy
from mac_vendor_lookup import MacLookup
#for printing arguments help and available options for users
import optparse
# for coloring the terminal
from termcolor import cprint, colored
import subprocess
import socket
# For detecting the OS the script is working on
import platform
# For regular expressions
import re
'''
Description: This tool is part of the ethical hacking toolset. It describes a simple ARP network reconnaissance tool.
This is for educational use ONLY for security purposes.
The usage of Network Discoverer can be invoked via a -h switch
Requirements: You need only to install scapy, mac_vendor_lookup and optparse
Eg: 'pip3 install scapy'
Use packaged executables for Mac OS, Linux and MS Windows for deployment
Usage: python3 NetworkDiscoverer.py or ./NetworkDiscoverer.py (after making the file executable or
better for deployment to change source code and package the app as executables
Enjoy!
'''
def display_header():
cprint(
"""\
_ _ _ _ _ _____ _
/\ | | | \ | | | | | | | __ \ (_)
/ \ _ __ __ _ | |_ | \| | ___ | |_ __ __ ___ _ __ | | __ | | | | _ ___ ___ ___ __ __ ___ _ __ ___ _ __
/ /\ \ | '_ \ / _` || __| | . ` | / _ \| __|\ \ /\ / // _ \ | '__|| |/ / | | | || |/ __| / __|/ _ \\ \ / // _ \| '__|/ _ \| '__|
/ ____ \ | | | || (_| || |_ | |\ || __/| |_ \ V V /| (_) || | | < | |__| || |\__ \| (__| (_) |\ V /| __/| | | __/| |
/_/ \_\|_| |_| \__,_| \__| |_| \_| \___| \__| \_/\_/ \___/ |_| |_|\_\ |_____/ |_||___/ \___|\___/ \_/ \___||_| \___||_|
by Dr. Hussein Bakri\n""", 'green')
cprint("This tool is licensed under MIT\n",'green')
def ARPScan(IP):
arp_request = scapy.ARP()
arp_request.pdst =IP # setting the IPfield in Scapy ARP packet to IP
broadcast = scapy.Ether()
broadcast.dst = "ff:ff:ff:ff:ff:ff"
arp_request_broadcast = broadcast/arp_request
answered_list, unanswered_list = scapy.srp(arp_request_broadcast, timeout=2, verbose=False)
clients_list = []
for answer in answered_list:
RetrievedMACVendor = MacLookup().lookup(answer[1].hwsrc)
client_dict = {"ip":answer[1].psrc, "mac":answer[1].hwsrc, "mac_vendor": RetrievedMACVendor}
clients_list.append(client_dict)
return clients_list
def FindMyGatewayAndConstructSubnet():
ProcessOutput = subprocess.Popen(["arp", "-a"], stdout = subprocess.PIPE)
(result, error) = ProcessOutput.communicate()
Out = result.decode("utf-8")
MyIP = re.findall('(?<=Interface: )(.*)(?=---)', Out)[0]
Splitted = Out.split("\n")
MyGatewayAddress = Splitted[3].strip().split(" ")[0]
# Changing the last part of the IP to 0
# example: Gateway is 192.168.0.1 would become 192.168.0.0
ConstructedIPwithSubnet = MyGatewayAddress.split(".")[0] + "." + MyGatewayAddress.split(".")[1] + "." + MyGatewayAddress.split(".")[2] + ".0/24"
return ConstructedIPwithSubnet
def PrintResults(Found_devices_list):
if(not Found_devices_list):
print("Sorry did not find any host/device after scanning....")
exit(0)
else:
dash = '-' * 106
cprint(dash)
cprint('{:<40s}{:<40s}{:<40s}'.format('IP','At MAC Address', 'MAC Vendor/Hostname'))
cprint(dash)
for device in Found_devices_list:
cprint('{:<40s}{:<40s}{:<40s}'.format(device["ip"], device["mac"], device["mac_vendor"]))
def main():
parser = optparse.OptionParser('Usage of the program: ' + '-t <target IP>')
parser.add_option('-t', '--target', dest='targetIP', type='string' , help='specify a target IP eg: 10.0.2.18 or 10.0.2.0/24 for the whole subnet')
parser.add_option('--mynet', action='store_true', dest='mynet', help='When you specify this argument --mynet, the tool will automatically behind the scene find gateway and construct the subnet')
(options, args) = parser.parse_args()
display_header()
targetIP = options.targetIP
if(options.targetIP == None and options.mynet == None):
parser.print_help()
exit(0)
if(options.mynet and options.targetIP):
parser.print_help()
exit(cprint("\nYou should not specify both --target and --mynet. Please specify only one argument.","red"))
if(options.mynet):
ConstructedIP = FindMyGatewayAndConstructSubnet()
else:
ConstructedIP = targetIP
results = ARPScan(ConstructedIP)
PrintResults(results)
if __name__ == '__main__':
main()
| python |
import os
import numpy as np
# Precursor charges and m/z's considered.
mz_interval = 1
charges, mzs = (2, 3), np.arange(50, 2501, mz_interval)
# Spectrum preprocessing.
min_peaks = 5
min_mz_range = 250.
min_mz, max_mz = 101., 1500.
remove_precursor_tolerance = 0.5
min_intensity = 0.01
max_peaks_used = 50
scaling = 'rank'
# Spectrum to vector conversion.
fragment_mz_tolerance = 0.05
hash_len = 800
# Spectrum matching.
precursor_tol_mass, precursor_tol_mode = 20, 'ppm'
# NN index construction and querying.
n_neighbors, n_neighbors_ann = 64, 128
n_probe = 32
batch_size = 2**16
# DBSCAN clustering.
eps = 0.1
min_samples = 2
# Input/output.
overwrite = False
export_representatives = False
pxd = 'USI000000'
peak_dir = os.path.abspath('../data/interim')
work_dir = os.path.abspath('../data/processed')
filenames = [os.path.join(peak_dir, filename)
for filename in os.listdir(peak_dir)
if filename.endswith('.mgf')]
| python |
from discord.ext import commands
import discord
import pymongo
from codecs import open
from cogs.utils import Defaults, Checks, OsuUtils
class Vote(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.db_users = pymongo.MongoClient(bot.database)['osu-top-players-voting']['users']
@Checks.is_guild_member()
@commands.dm_only()
@commands.command()
async def stem(self, ctx, posisjon: int, *, spiller: str):
"""Gi en spiller en stemme"""
query = {'_id': ctx.author.id}
try:
db_user = self.db_users.find_one(query)
except:
return await Defaults.error_fatal_send(ctx, text='Jeg har ikke tilkobling til databasen\n\n' +
'Be båtteier om å fikse dette')
spiller = spiller.lower()
if posisjon > 10 or posisjon < 1:
return await Defaults.error_warning_send(ctx, text='Du kan bare sette rangering mellom 1-10')
if db_user is None:
self.db_users.insert_one({
'_id': ctx.author.id,
'1': None,
'2': None,
'3': None,
'4': None,
'5': None,
'6': None,
'7': None,
'8': None,
'9': None,
'10': None})
db_user = self.db_users.find_one(query)
with open('./assets/top_50_norway.txt', 'r', encoding='utf-8') as f:
top_50_norway = [line.rstrip('\r\n') for line in f]
if spiller not in top_50_norway:
return await Defaults.error_warning_send(ctx, text='Brukeren er ikke på [lista](https://gist.github.com/ + '
'LBlend/6cc58ee838d928032df48740c313fec6)')
for key, value in db_user.items():
if value == spiller:
self.db_users.update_one(query, {'$set': {f'{key}': None}})
self.db_users.update_one(query, {'$set': {f'{posisjon}': spiller}})
spiller = await OsuUtils.convert_name(spiller)
embed = discord.Embed(color=discord.Color.green(),
description=f':white_check_mark: Du har satt **{spiller}** som ditt {posisjon}. valg!')
await Defaults.set_footer(ctx, embed)
await ctx.send(embed=embed)
@commands.dm_only()
@commands.command(aliases=['stemmer'])
async def minestemmer(self, ctx):
"""Se hvem du har stemt på"""
query = {'_id': ctx.author.id}
try:
db_user = self.db_users.find_one(query)
except:
return await Defaults.error_fatal_send(ctx, text='Jeg har ikke tilkobling til databasen\n\n' +
'Be båtteier om å fikse dette')
if db_user is None:
return await Defaults.error_warning_send(ctx, text='Du har ikke stemt på noen')
votes = ''
for key, value in db_user.items():
if key != '_id':
if value is None:
value = ''
value = await OsuUtils.convert_name(value)
votes += f'**{key}.** {value}\n'
embed = discord.Embed(color=ctx.me.color, description=votes)
await Defaults.set_footer(ctx, embed)
await ctx.send(embed=embed)
@commands.dm_only()
@commands.command()
async def fjernstemmer(self, ctx):
"""Fjerner alle stemmene dine"""
query = {'_id': ctx.author.id}
try:
db_user = self.db_users.find_one(query)
except:
return await Defaults.error_fatal_send(ctx, text='Jeg har ikke tilkobling til databasen\n\n' +
'Be båtteier om å fikse dette')
if db_user is None:
return await Defaults.error_warning_send(ctx, text='Du har ikke stemt på noen')
self.db_users.delete_one(query)
embed = discord.Embed(color=discord.Color.green(), description='Alle stemme dine er nå fjernet!')
await Defaults.set_footer(ctx, embed)
await ctx.send(embed=embed)
@commands.bot_has_permissions(embed_links=True)
@commands.cooldown(1, 2, commands.BucketType.guild)
@commands.command()
async def kandidater(self, ctx):
"""Viser kandidatene"""
embed = discord.Embed(color=ctx.me.color, title='Kandidater',
description='[Trykk her for å se lista](https://gist.github.com/' +
'LBlend/6cc58ee838d928032df48740c313fec6)')
await Defaults.set_footer(ctx, embed)
await ctx.send(embed=embed)
@commands.has_permissions(administrator=True)
@commands.bot_has_permissions(embed_links=True)
@commands.cooldown(1, 2, commands.BucketType.guild)
@commands.command()
async def resultat(self, ctx):
"""Viser resultatet for øyeblikket"""
query = {'_id': ctx.author.id}
try:
self.db_users.find_one(query)
except:
return await Defaults.error_fatal_send(ctx, text='Jeg har ikke tilkobling til databasen\n\n' +
'Be båtteier om å fikse dette')
players = {}
voters = 0
for i in self.db_users.find():
voters += 1
for key, value in i.items():
if key != '_id' and value is not None:
try:
players[f'{value}']
except KeyError:
players[f'{value}'] = await OsuUtils.convert_score(key)
continue
players[f'{value}'] += await OsuUtils.convert_score(key)
players = sorted(players.items(), key=lambda x: x[1], reverse=True)
leaderboard = ''
for i in players:
player = await OsuUtils.convert_name(i[0])
score = i[1]
leaderboard += f'**{player}**: {score}\n'
embed = discord.Embed(color=ctx.me.color, title='Stilling', description=leaderboard)
embed.set_footer(text=f'Antall som har stemt: {voters}')
await ctx.send(embed=embed)
def setup(bot):
bot.add_cog(Vote(bot))
| python |
from unittest import TestCase
import pytest
from hubblestack.audit import util
from collections import defaultdict
from hubblestack.exceptions import ArgumentValueError, HubbleCheckValidationError
class TestProcess():
"""
Class used to test the functions in ``process.py``
"""
def test__compare_raises_exception_if_arguments_have_invalid_type(self):
"""
Test that given invalid ``comp``,
the function raises an ArgumentValueError exception
"""
with pytest.raises(ArgumentValueError):
util._compare('foo', 1, 2)
def test__compare_returns_correctly_with_ge_comparator(self):
"""
Test that given correct values, the function outputs the correct result with 'ge' comparator
ge = greater equal
"""
ret = util._compare('ge', 1, 2)
assert ret is False, '1 >= 2'
ret = util._compare('ge', 2, 2)
assert ret is True, '2 >= 2'
ret = util._compare('ge', 2, 1)
assert ret is True, '2 >= 1'
def test__compare_returns_correctly_with_gt_comparator(self):
"""
Test that given correct values, the function outputs the correct result with 'gt' comparator
gt = greater than
"""
ret = util._compare('gt', 10, 2)
assert ret is True, '10 > 2'
ret = util._compare('gt', 1, 2)
assert ret is False, '1 > 2'
ret = util._compare('gt', 2, 2)
assert ret is False, '2 > 2'
def test__compare_returns_correctly_with_lt_comparator(self):
"""
Test that given correct values, the function outputs the correct result with 'lt' comparator
lt = lower than
"""
ret = util._compare('lt', 1, 2)
assert ret is True, '1 < 2'
ret = util._compare('lt', 2, 2)
assert ret is False, '2 < 2'
ret = util._compare('lt', 2, 1)
ret is False, '2 < 1'
def test__compare_returns_correctly_with_le_comparator(self):
"""
Test that given correct values, the function outputs the correct result with 'le' comparator
le = lower equal
"""
ret = util._compare('le', 1, 2)
assert ret is True, '1 <= 2'
ret = util._compare('le', 2, 2)
assert ret is True, '2 <= 2'
ret = util._compare('le', 2, 1)
assert ret is False, '2 <= 1'
def test__compare_returns_correctly_with_eq_comparator(self):
"""
Test that given correct values, the function outputs the correct result with 'eq' comparator
eq = equal
"""
ret = util._compare('eq', 1, 2)
assert ret is False, '1 == 2'
ret = util._compare('eq', 2, 1)
assert ret is False, '2 == 1'
ret = util._compare('eq', 1, 1)
assert ret is True, '1 == 1'
def test__compare_returns_correctly_with_ne_comparator(self):
"""
Test that given correct values, the function outputs the correct result with 'ne' comparator
ne = not equal
"""
ret = util._compare('ne', 1, 2)
assert ret is True, '1 != 2'
ret = util._compare('ne', 2, 1)
assert ret is True, '2 != 1'
ret = util._compare('ne', 1, 1)
assert ret is False, '1 != 1'
def test__filter_dict_returns_none_if_filter_values_is_invalid(self):
"""
Test that given invalid ``filter_values``, the function returns None
"""
status, ret = util._filter_dict_helper('test',
dct={1: 'a', 2: 'b'}, filter_values=False, filter_rules={'invalid': 1, 'data': 2})
assert status is False
assert ret['error'] == 'invalid_format', 'invalid filter_rules should return None'
def test__filter_dict_returns_correctly_filtered_dict_by_keys(self):
"""
Test that given valid ``filter_values``, the function correctly filters a dict by keys
"""
# keep x if 1 < x <= 4 and x != 3
expected_ret = {2: 'b', 4: 'd'}
status, ret = util._filter_dict_helper('test',
{1: 'a', 2: 'b', 3: 'c', 4: 'd'}, False, {'gt': 1, 'le': 4, 'ne': 3})
assert status == True
assert expected_ret == ret['result']
# keep x if 'a' <= x < 'd' and x != 'c'
expected_ret = {'a': 1, 'b': 2}
status, ret = util._filter_dict_helper('test',
{'a': 1, 'b': 2, 'c': 3, 'd': 4}, False, {'ge': 'a', 'lt': 'd', 'ne': 'c'})
assert status == True
assert expected_ret == ret['result']
def test__filter_dict_returns_correctly_filtered_dict_by_values(self):
"""
Test that given valid ``filter_values``, the function correctly filters a dict by values
"""
# keep x if 1 < x <= 4 and x != 3
expected_ret = {'b': 2, 'd': 4}
status, ret = util._filter_dict_helper('test',
{'a': 1, 'b': 2, 'c': 3, 'd': 4}, True, {'gt': 1, 'le': 4, 'ne': 3})
assert status == True
assert expected_ret == ret['result']
# keep x if 'a' <= x < 'd' and x != 'c'
expected_ret = {1: 'a', 2: 'b'}
status, ret = util._filter_dict_helper('test',
{1: 'a', 2: 'b', 3: 'c', 4: 'd'}, True, {'ge': 'a', 'lt': 'd', 'ne': 'c'})
assert status == True
assert expected_ret == ret['result']
def test__filter_dict_returns_unaltered_dict_if_filter_rules_is_empty(self):
"""
Test that given empty ``filter_rules``, the function leaves the dict intact
"""
expected_ret = {1: 'a', 2: 'b'}
status, ret = util._filter_dict_helper('test', {1: 'a', 2: 'b'}, True, {})
assert status == True
assert expected_ret == ret['result']
def test_filter_dict_returns_none_if_dict_is_invalid(self):
"""
Test that given invalid types for ``starting_dict`` or ``chained``,
the function returns False and None
"""
# invalid starting_dict - is type list
expected_status, expected_ret = False, None
block_dict = {'args':
{'starting_dict': [1, 2, 3]}}
chaining_args = {'chaining_args': {'result': {1: 'a', 2: 'b'}, 'status': True}}
status, ret = util._filter_dict('test', block_dict, chaining_args)
assert status is False, 'invalid starting_dict, should return False'
# invalid chained dict - is type list
block_dict = {'args':
{'starting_dict': {1: 'a', 2: 'b'}}}
chaining_args = {'chaining_args': {'result': [1, 2], 'status': True}}
status, ret = util._filter_dict('test', block_dict, chaining_args)
assert status is False, 'invalid chained, should return False'
def test_filter_dict_correctly_filters_out_keys(self):
"""
Test that given correct input, the function correctly filters by keys
"""
expected_ret = {1: 'a', 2: 'b', 4: 'd'}
block_dict = {'args':
{'starting_dict': {1: 'a', 2: 'b', 3: 'c'},
'filter_rules': {'ge':1, 'ne':3}}}
chaining_args = {'chaining_args': {'result': {1: 'b', 3: 'd', 4: 'd'}, 'status': True}}
status, ret = util._filter_dict('test', block_dict, chaining_args)
assert status is True
assert expected_ret == ret['result']
def test_filter_dict_correctly_filters_out_values(self):
"""
Test that given correct input, the function correctly filters by values
"""
expected_ret = {3: 'c', 4: 'd'}
block_dict = {'args':
{'starting_dict': {1: 'a', 2: 'b', 3: 'c'}, 'filter_values': True,
'filter_rules': {'gt':'a', 'ne':'b', 'le':'d'}}}
chaining_args = {'chaining_args': {'result': {1: 'b', 3: 'd', 4: 'd'}, 'status': True}}
status, ret = util._filter_dict('test', block_dict, chaining_args)
assert status is True
assert expected_ret == ret['result']
def test__filter_returns_none_if_input_is_invalid(self):
"""
Test that given invalid input, the function returns None
"""
status, ret = util._filter('test', [1, 2, 3], {'foo': 1})
assert status == False
assert ret['error'] == 'invalid_format', 'invalid input type should return None'
def test__filter_correctly_filters_sequence_if_input_is_valid(self):
"""
Test that given valid arguments of different types,
the function returns the filtered sequence
"""
# list
expected_ret = [2, 4]
seq = [1, 2, 3, 4]
status, ret = util._filter('test', seq, {"gt": 1, "ne": 3, "le": 4})
assert status == True
assert expected_ret == ret['result']
# set
seq = set(seq)
status, ret = util._filter('test', seq, {"gt": 1, "ne": 3, "le": 4})
assert status == True
assert expected_ret == ret['result']
# string
seq = "test string"
expected_ret = ['e', 's', ' ', 's', 'r', 'i', 'n', 'g']
status, ret = util._filter('test', seq, {"ne": 't'})
assert status == True
assert expected_ret == ret['result']
def test_filter_seq_returns_none_if_input_is_invalid(self):
"""
Test that given invalid input, the function returns None
"""
# invalid ``starting_seq``
chain_args = {'chaining_args': {'result': [2,3,4], 'status': True}}
block_dict = {'args':{'starting_seq':1, 'filter_rules': {'ge':1, 'lt':4}}}
status, ret = util._filter_seq('test', block_dict, chain_args)
assert status is False, 'invalid starting_seq, should return False'
# invalid ``chained``
chain_args = {'chaining_args': {'result': 4, 'status': True}}
block_dict = {'args':{'starting_seq':[1,2], 'filter_rules': {'ge':1, 'lt':4}}}
status, ret = util._filter_seq('test', block_dict, chain_args)
assert status is False, 'invalid chained, should return False'
def test_filter_seq_returns_filtered_seq_with_valid_input(self):
"""Test that given valid input of different types,
the function returns True and the filtered sequence
"""
# list
seq = [3, 4]
chained = [1, 2]
chain_args = {'chaining_args': {'result': chained, 'status': True}}
block_dict = {'args':{'starting_seq':seq, 'filter_rules': {'gt':1, 'ne':3, 'le': 4}}}
expected_ret = [2, 4]
status, ret = util._filter_seq('test', block_dict, chain_args)
assert expected_ret == ret['result']
assert status is True
# set
expected_ret = [3]
seq = set(seq)
chained = set(chained)
chain_args = {'chaining_args': {'result': chained, 'status': True}}
block_dict = {'args':{'starting_seq':seq, 'filter_rules': {'ge':1, 'ne':2, 'lt': 4, 'eq': 3}}}
status, ret = util._filter_seq('test', block_dict, chain_args)
assert expected_ret == ret['result']
assert status is True
# string
expected_ret = ['e', 's', ' ', 's', 'r', 'i', 'n', 'g']
seq = 'test {}'
chained = 'string'
chain_args = {'chaining_args': {'result': chained, 'status': True}}
block_dict = {'args':{'starting_seq':seq, 'filter_rules': {'ne': 't'}}}
status, ret = util._filter_seq('test', block_dict, chain_args)
assert expected_ret == ret['result']
assert status is True
def test_get_index_returns_none_if_invalid_input(self):
"""
Test that given invalid arguments, the function returns None
"""
# invalid ``chained``
status, ret = util._get_index('test', {'args': {'starting_list':[1, 2, 3]}}, {})
assert status is False, 'invalid chained, should return False'
# index out of range
status, ret = util._get_index('test', {'args': {'index':4}},
{'chaining_args': {'result': [1, 2, 3], 'status': True}})
assert status is False, 'index 4 out of range, list length is 3, should return False'
# invalid ``chained`` type
status, ret = util._get_index('test', {},
{'chaining_args': {'result': set([1, 2, 3]), 'status': True}})
assert status is False, 'invalid chained type, should return False'
def test_get_index_returns_correctly_if_valid_input(self):
"""
Test that given valid arguments,
the function extracts the correct value
"""
# return element at index -1 from [3, 4, 1, 2]
expected_ret = 2
status, ret = util._get_index('test',
{'args': {'index': -1, 'starting_list': [1,2]}},
{'chaining_args': {'result': [3,4], 'status': True}})
assert status is True
assert expected_ret == ret['result']
# default to index 0 from [3, 4, 1, 2]
expected_ret = 3
status, ret = util._get_index('test',
{'args': {'starting_list': [1,2]}},
{'chaining_args': {'result': [3,4], 'status': True}})
assert status is True
assert expected_ret == ret['result']
# return element at index 2 from [3, 4, 1, 2]
expected_ret = 1
status, ret = util._get_index('test',
{'args': {'index': 2, 'starting_list': [1,2]}},
{'chaining_args': {'result': [3,4], 'status': True}})
assert status is True
assert expected_ret == ret['result']
def test_get_key_returns_none_if_invalid_input(self):
"""
Test that given invalid arguments, the function returns None
"""
# invalid ``chained`` type
status, ret = util._get_key('test',
{'args': {'key': '1'}},
{'chaining_args': {'result': ['a', 'b', 'c'], 'status': True}})
assert status is False, 'invalid chained type, should return False'
# invalid key
status, ret = util._get_key('test',
{'args': {'key': 'd'}},
{'chaining_args': {'result': {'a': 1, 'b': 2, 'c': 3}, 'status': True}})
assert status is False, 'invalid key `d` in dict, should return False'
def test_get_key_returns_correctly(self):
"""
Test that given valid arguments,
the function returns the correct value
"""
expected_ret = 1
status, ret = util._get_key('test',
{'args': {'key': 'b', 'starting_dict':{'b': 1, 'c': 2}}},
{'chaining_args': {'result': {'a': 1, 'b': 2}, 'status': True}})
assert status is True
assert expected_ret == ret['result']
def test_join_returns_none_if_invalid_arguments(self):
"""
Test that given invalid arguments,
the function returns None
"""
# invalid ``chained``
status, ret = util._join('test',{},
{'chaining_args': {'result': 1, 'status': True}})
assert status is False
# invalid ``sep``
status, ret = util._join('test',
{'args': {'sep': [1,2]}},
{'chaining_args': {'result': ['foo', 'bar'], 'status': True}})
assert status is False
def test_join_returns_correct_string(self):
"""
Test that given valid arguments,
the function will return the joined string
"""
# no ``sep``
expected_ret = 'testwordstogether'
status, ret = util._join('test',
{'args': {'words':'together'}},
{'chaining_args': {'result': ['test', 'words'], 'status': True}})
assert status is True
assert expected_ret == ret['result']
# valid ``sep``
expected_ret = 'test-more-words-together'
status, ret = util._join('test',
{'args': {'words':['words', 'together'], 'sep': '-'}},
{'chaining_args': {'result': ['test', 'more'], 'status': True}})
assert status is True
assert expected_ret == ret['result']
def test__sort_returns_none_if_invalid_input(self):
"""
Test that given invalid arguments, the function returns None
"""
# invalid ``seq``
ret = util._sort_helper(seq=1, desc=True, lexico=False)
assert ret is None
# invalid ``desc``
ret = util._sort_helper(seq=[2, 1], desc='yes', lexico=False)
assert ret is None
# invalid ``lexico``
ret = util._sort_helper(seq=[1, 2, 12, 13], desc=False, lexico=True)
assert ret is None
def test__sort_returns_sorted_seq(self):
"""
Test that given valid arguments,
the function correctly sorts them with different parameters
"""
expected_ret = ['Z', 'a', 'b']
ret = util._sort_helper(seq=['b', 'a', 'Z'], desc=False, lexico=False)
assert expected_ret == ret
expected_ret = ['b', 'a', 'B']
ret = util._sort_helper(
seq={'a': 1, 'b': 2, 'B': 3}, desc=True, lexico=False)
assert expected_ret == ret
expected_ret = ['A', 'b', 'C']
ret = util._sort_helper(
seq=set(['b', 'A', 'C']), desc=False, lexico=True)
assert expected_ret == ret
def test_sort_returns_none_if_invalid_arguments(self):
"""
Test that given invalid arguments, the function returns None
"""
# invalid ``chained``
status, ret = util._sort('test',
{'args': {'seq': 2}},
{'chaining_args': {'result': 1, 'status': True}})
assert status is False
# invalid ``desc``
status, ret = util._sort('test',
{'args': {'desc': 'yes'}},
{'chaining_args': {'result': [1, 2, 3], 'status': True}})
assert status is False
# invalid ``lexico``
status, ret = util._sort('test',
{'args': {'lexico': True}},
{'chaining_args': {'result': [1, 2, 3], 'status': True}})
assert status is False
def test_sort_returns_sorted_seq(self):
"""
Test that given valid arguments,
the function correctly sorts them with different parameters
"""
expected_ret = [3, 2, 1]
# desc list
status, ret = util._sort('test',
{'args': {'seq': [1,2],'desc': True}},
{'chaining_args': {'result': [3], 'status': True}})
assert status is True
assert expected_ret == ret['result']
# dict
expected_ret = [1, 2, 3]
status, ret = util._sort('test',
{},
{'chaining_args': {'result': {2: 'a', 1: 'b', 3: 'c'}, 'status': True}})
assert status is True
assert expected_ret == ret['result']
# desc set
expected_ret = ['b', 'a', 'B', 'A']
status, ret = util._sort('test',
{'args': {'seq': ['A', 'B'], 'desc': True}},
{'chaining_args': {'result': set(['a', 'b']), 'status': True}})
assert status is True
assert expected_ret == ret['result']
# lexicographic string
expected_ret = ['A', 'a', 'b', 'B']
status, ret = util._sort('test',
{'args': {'seq': 'A{}B', 'lexico': True}},
{'chaining_args': {'result': 'ab', 'status': True}})
assert status is True
assert expected_ret == ret['result']
def test__split_returns_none_if_invalid_arguments(self):
"""
Test that given invalid arguments,
the function returns None
"""
ret = util._split_helper(phrase=[1, 2, 3], sep=" ", regex=False)
assert ret is None, "can't split list, should return None"
ret = util._split_helper(phrase="foo bar", sep=[1, 2, 3], regex=False)
assert ret is None, "separator to split by can't be list, should return None"
ret = util._split_helper(phrase=[1, 2, 3], sep=" ", regex=True)
assert ret is None, "can't split list, should return None"
ret = util._split_helper(phrase="foo bar", sep=[1, 2, 3], regex=True)
assert ret is None, "separator to split by can't be list, should return None"
def test__split_returns_list_from_string(self):
"""
Test that given valid arguments,
the function correctly splits the string into a list
"""
# simple ``sep``
expected_ret = ['foo', 'bar']
ret = util._split_helper("foo bar", " ", False)
assert expected_ret == ret
# ``sep`` simple regex
ret = util._split_helper("foo bar", " ", True)
assert expected_ret == ret
# regex
ret = util._split_helper("foo bar", r"\s+", True)
assert expected_ret == ret
# invalid ``sep``
expected_ret = ['foo bar']
ret = util._split_helper("foo bar", "?", False)
assert expected_ret == ret
def test_split_returns_none_if_invalid_arguments(self):
"""
Test that given invalid arguments,
the function returns None
"""
# invalid ``words``
status, ret = util._split('test',
{'args': {'phrase': [1, 2, 3]}},
{'chaining_args': {'result': 'ab', 'status': True}})
assert status is False
status, ret = util._split('test',
{'args': {'phrase': {1: 'a', 2: 'b'}}},
{'chaining_args': {'result': 'ab', 'status': True}})
assert status is False
# invalid ``words`` & ``chained``
status, ret = util._split('test',
{'args': {'phrase': 1}},
{'chaining_args': {'result': 12, 'status': True}})
assert status is False
status, ret = util._split('test',
{'args': {'phrase': 'foo bar', 'regex': True}},
{})
assert status is False
def test_split_returns_list_from_string(self):
"""
Test that given valid arguments, the function correctly splits
in all scenarios
"""
expected_ret = ['a', 'b', 'c', 'd']
# valid regex
status, ret = util._split('test',
{'args': {'phrase': 'a1b2c3d', 'sep': r"\d+", 'regex': True}},
{})
assert status is True
assert expected_ret == ret['result']
# simple sep
expected_ret = ['a1', 'b2', 'c3', 'd']
status, ret = util._split('test',
{'args': {'phrase': "a1 b2 {}", 'sep': " "}},
{'chaining_args': {'result': 'c3 d', 'status': True}})
assert status is True
assert expected_ret == ret['result']
# no sep
expected_ret = ['a1', 'b2', 'c3', 'd']
status, ret = util._split('test',
{'args': {'phrase': "a1 b2 \n{}"}},
{'chaining_args': {'result': 'c3 d', 'status': True}})
assert status is True
assert expected_ret == ret['result']
# invalid regex
expected_ret = ['a1b2c3d']
status, ret = util._split('test',
{'args': {'phrase': "a1b2{}", 'sep': r"\d+", 'regex': False}},
{'chaining_args': {'result': 'c3d', 'status': True}})
assert status is False
def test_dict_to_list_returns_none_if_invalid_arguments(self):
"""
Test that given invalid arguments, the function returns None
"""
status, ret = util._dict_to_list('test',
{'args': {'starting_dict':{1: 'a'}}},
{'chaining_args': {'result': [1,2,3], 'status': True}})
assert status is False
status, ret = util._dict_to_list('test',
{'args': {'starting_dict':'foo'}},
{'chaining_args': {'result': {1: 'a', 2: 'b'}, 'status': True}})
assert status is False
def test_dict_to_list_correctly_returns_list(self):
"""
Test that given valid arguments, the function outputs a valid list
"""
# flat dict
expected_ret = [(1, 'b'), (2, 'c')]
status, ret = util._dict_to_list('test',
{'args': {'starting_dict':{1: 'a'}, 'update_chained': False}},
{'chaining_args': {'result': {1: 'b', 2: 'c'}, 'status': True}})
assert status is True
assert expected_ret == ret['result']
# nested dict
expected_ret = [(1, 'a'), (2, 'c'), (3, {1: 'a'})]
status, ret = util._dict_to_list('test',
{'args': {'starting_dict':{1: 'a', 3: {1: 'a'}}}},
{'chaining_args': {'result': {1: 'b', 2: 'c'}, 'status': True}})
assert status is True
assert expected_ret == ret['result']
# empty dict
status, ret = util._dict_to_list('test',{},
{'chaining_args': {'result': {}, 'status': True}})
assert status is False
def test__dict_convert_none_returns_none_if_invalid_arguments(self):
"""
Test that given invalid arguments, the function returns None
"""
ret = util._dict_convert_none_helper([1, 2, 3])
assert ret is None
ret = util._dict_convert_none_helper(1)
assert ret is None
expected_ret = {}
ret = util._dict_convert_none_helper(defaultdict())
assert expected_ret == ret
def test__dict_convert_none_replaces_empty_string_with_none_in_dict(self):
"""
Test that given valid arguments,
the function converts empty strings to None in all scenarios
"""
# flat dict
expected_ret = {1: None, 2: 'a', 3: "None", 4: None}
ret = util._dict_convert_none_helper(
{1: "", 2: 'a', 3: "None", 4: None})
assert expected_ret == ret
# nested dicts
expected_ret = {'a': {'aa': {'aaa': 3, 'bbb': {'bbbb': 4, 'cccc': None},
'ccc': None}, 'bb': None}, 'b': None}
ret = util._dict_convert_none_helper(
{'a': {'aa': {'aaa': 3, 'bbb': {'bbbb': 4, 'cccc': ''},
'ccc': ''}, 'bb': ''}, 'b': ''})
assert expected_ret == ret
# nested dicts & seqs
expected_ret = {'a': [{'b': [{'c': ['d', {'e': None}], 'f': None}, {'g': None}],
'h': None}, 'i'], 'j': None}
ret = util._dict_convert_none_helper(
{'a': [{'b': ({'c': ['d', {'e': ''}], 'f': ''}, {'g': ''}),
'h': ''}, 'i'], 'j': ''})
assert expected_ret == ret
def test__seq_convert_none_returns_none_if_invalid_arguments(self):
"""
Test that given invalid arguments, the function returns None
"""
ret = util._seq_convert_none_helper({1: 'a', 2: 'b'})
assert ret is None
ret = util._seq_convert_none_helper(1)
assert ret is None
ret = util._seq_convert_none_helper(True)
assert ret is None
def test__seq_convert_none_replaces_emtpy_strings_with_none(self):
"""
Test that given valid arguments,
the function correctly converts empty strings to None in all scenarios
"""
# flat seq
expected_ret = ['a', {1: None}, 'b', {1: None}, 'c']
ret = util._seq_convert_none_helper(
['a', {1: ''}, 'b', {1: ''}, 'c'])
assert expected_ret == ret
# nested seq & dict
expected_ret = ['a', [{1: None, 2: [3, [4, {1: None, 2: {3: None}}]]}, 'b'], 'c']
ret = util._seq_convert_none_helper(
('a', [{1: '', 2: [3, (4, {1: '', 2: {3: ''}})]}, 'b'], 'c'))
assert expected_ret == ret
def test_dict_convert_none_returns_none_if_invalid_argument(self):
"""
Test that given invalid arguments, the function returns None
"""
status, ret = util._dict_convert_none('test',
{},
{'chaining_args': {'result': 'foo bar', 'status': True}})
assert status is False
status, ret = util._dict_convert_none('test',
{'args': {'starting_seq':[1, 2]}},
{'chaining_args': {'result': {1: 'a'}, 'status': True}})
assert status is False
status, ret = util._dict_convert_none('test',
{},
{'chaining_args': {'result': {}, 'status': True}})
assert status is False
def test_dict_convert_none_replaces_empty_string_with_none(self):
"""
Test that given valid arguments,
the function returns a valid dict with None instead of empty strings
"""
# flat dict
expected_ret = {1: 'a', 2: None, 3: 'b', 4: None}
status, ret = util._dict_convert_none('test',
{},
{'chaining_args': {'result': {1: 'a', 2: '', 3: 'b', 4: ''}, 'status': True}})
assert expected_ret == ret['result']
assert status is True
# nested dict & tuple
expected_ret = {'a': [{'b': [{'c': {'e': None}, 'f': None}, {'g': None}],
'h': None}, 'i'], 'j': None}
status, ret = util._dict_convert_none('test',
{'args': {'starting_seq':{'j': ''}}},
{'chaining_args': {'result': {'a': [{'b': ({'c': {'e': ''}, 'f': ''}, {'g': ''}),
'h': ''}, 'i']}, 'status': True}})
assert status is True
assert expected_ret == ret['result']
# nested dict, list & tuple
expected_ret = ['a', [{1: None, 2: [3, [4, {1: None, 2: {3: None}}]]}, 'b'], 'c']
status, ret = util._dict_convert_none('test',
{},
{'chaining_args': {'result': ('a', [{1: '', 2: [3, (4, {1: '', 2: {3: ''}})]}, 'b'], 'c'), 'status': True}})
assert status is True
assert expected_ret == ret['result']
# nested dict & list
expected_ret = ['a', {1: None}, 'b', {1: None}, 'c']
status, ret = util._dict_convert_none('test',
{'args': {'starting_seq': [{1: ''}, 'c']}},
{'chaining_args': {'result': ['a', {1: ''}, 'b'], 'status': True}})
assert status is True
assert expected_ret == ret['result']
def test_print_string_returns_none_when_invalid_arguments(self):
"""
Test that given invalid arguments, the function returns None
"""
status, ret = util._print_string('test',
{'args': {'starting_string': ['foo', 'bar']}},
{})
assert status is False
status, ret = util._print_string('test',
{'args': {'starting_string': ''}},
{})
assert status is False
def test_print_string_returns_correct_string(self):
"""
Test that given valid arguments, the function returns the correct string
"""
expected_ret = 'foo'
status, ret = util._print_string('test',
{'args': {'starting_string': 'foo'}},
{'chaining_args': {'result': 'bar', 'status': True}})
assert status is True
assert expected_ret == ret['result']
expected_ret = "foo ['b', 'a', 'r']"
status, ret = util._print_string('test',
{'args': {'starting_string': 'foo {}'}},
{'chaining_args': {'result': ['b', 'a', 'r'], 'status': True}})
assert status is True
assert expected_ret == ret['result']
def test__sterilize_dict_returns_none_if_invalid_arguments(self):
"""
Test that given invalid arguments, the function returns None
"""
ret = util._sterilize_dict(dictionary=[1, 2])
assert ret is None
ret = util._sterilize_dict(dictionary={})
assert ret == {}
ret = util._sterilize_dict(dictionary=12)
assert ret is None
def test__sterilize_dict_removes_none_values_if_nested_dict(self):
"""
Test that given valid arguments,
the function correctly removes keys containing values of None
"""
# flat dict
expected_ret = {2: 'a'}
ret = util._sterilize_dict(
{1: None, 2: 'a'})
assert expected_ret == ret
# nested dicts
expected_ret = {2: {3: {5: 'a'}, 7: 'b'}, 8: 'c', 9: {}}
ret = util._sterilize_dict(
{1: None, 2: {3: {4: None, 5: 'a'}, 6: None, 7: 'b'}, 8: 'c', 9: {10: None}})
assert expected_ret == ret
# nested dicts & sequences
expected_ret = {2: {3: [4, {}], 6: {7: ['b', {}]}}}
ret = util._sterilize_dict(
{1: None, 2: {3: [4, {5: None}], 6: {7: ('b', {9: None}), 8: None}}})
assert expected_ret == ret
def test__sterilize_seq_returns_none_if_arguments_are_invalid(self):
"""
Test that given invalid arguments, the function returns None
"""
ret = util._sterilize_seq(
{1: 'a', 2: ['b']})
assert ret is None
ret = util._sterilize_seq(12)
assert ret is None
ret = util._sterilize_seq([])
assert ret == []
def test__sterilize_seq_removes_none_values_from_seq(self):
"""
Test that given valid arguments,
the function finds nested dicts and removes keys with values of None
"""
# flat seq
expected_ret = [1, 2, [1, 2], [1, 2]]
ret = util._sterilize_seq(
[1, 2, set([1, 2, 1]), (1, 2)])
assert expected_ret == ret
# nested dicts & seq
expected_ret = [{2: {3: [{5: 'a'}, [None, {7: 'b'}]], 8: 'c', 9: {}}}]
ret = util._sterilize_seq(
[{1: None, 2: {3: ({4: None, 5: 'a'}, [None, {6: None, 7: 'b'}]),
8: 'c', 9: {10: None}}}])
assert expected_ret == ret
def test_remove_dict_none_returns_none_if_invalid_arguments(self):
"""
Test that given invalid arguments, the function returns None
"""
# invalid ``starting_seq``
status, ret = util._dict_remove_none('test',
{'args': {'starting_seq': [1, 2, 3]}},
{'chaining_args': {'result': {1: 'a', 2: 'b'}, 'status': True}})
assert status is False
# invalid ``chained`` & valid ``starting_seq``
status, ret = util._dict_remove_none('test',
{'args': {'starting_seq': [1, 2, 3]}},
{'chaining_args': {'result': '123', 'status': True}})
assert status is False
# invalid ``chained``
status, ret = util._dict_remove_none('test',
{},
{'chaining_args': {'result': '123', 'status': True}})
assert status is False
def test_dict_remove_none_returns_valid_sequence(self):
"""
Test that given valid arguments, the function finds nested dicts
and removes keys with values of None
"""
# flat dict
expected_ret = {2: 'a', 4: 'b'}
status, ret = util._dict_remove_none('test',
{},
{'chaining_args': {'result': {1: None, 2: 'a', 3: None, 4: 'b'}, 'status': True}})
assert status is True
assert expected_ret == ret['result']
# flat seq
expected_ret = [{}, {2: 'a'}, 5, None, {4: 'b'}]
status, ret = util._dict_remove_none('test',
{'args': {'starting_seq':[5, None, {4: 'b'}]}},
{'chaining_args': {'result': [{1: None}, {2: 'a', 3: None}], 'status': True}})
assert status is True
assert expected_ret == ret['result']
# nested sequences & dicts
expected_ret = [{9: {11: [1, 2]}}, 11, {2: {3: [{5: 'a'}, [None, {7: 'b'}]], 8: 'c'}}]
status, ret = util._dict_remove_none('test',
{'args': {'starting_seq':[{1: None, 2: {3: ({4: None, 5: 'a'},
[None, {6: None, 7: 'b'}]), 8: 'c'}}]}},
{'chaining_args': {'result': [{9: {10: None, 11: set([1, 2, 1])}}, 11], 'status': True}})
assert status is True
assert expected_ret == ret['result']
# nested dicts & sequences
expected_ret = {2: {3: [{5: 'a'}, [None, {7: 'b'}]], 8: 'c'}, 9: {11: [1, 2]}}
status, ret = util._dict_remove_none('test',
{'args': {'starting_seq':{1: None, 2: {3: ({4: None, 5: 'a'}, [None, {6: None, 7: 'b'}]), 8: 'c'}}}},
{'chaining_args': {'result': {9: {10: None, 11: set([1, 2, 1])}, 11: None}, 'status': True}})
assert status is True
assert expected_ret == ret['result']
def test_encode_base64_returns_none_if_invalid_arguments_type(self):
"""
Test that given invalid arguments, the function returns None
"""
# invalid `starting_string`
status, ret = util._encode_base64('test',
{'args': {'starting_string': 123}},
{'chaining_args': {'result': 'foo', 'status': True}})
assert status is False
status, ret = util._encode_base64('test',
{'args': {'starting_string': ['a', 'c'], 'format_chained': False}},
{})
assert status is False
expected_ret = ''
status, ret = util._encode_base64('test',
{'args': {'starting_string': '', 'format_chained': False}},
{})
assert status is False
def test_encode_base64_returns_string_if_valid_arguments(self):
"""
Test that given valid arguments, the function correctly encodes the string and returns it
"""
# format chained
expected_ret = 'Zm9vIGJhcg=='
status, ret = util._encode_base64('test',
{'args': {'starting_string': 'foo {}'}},
{'chaining_args': {'result': 'bar', 'status': True}})
assert status is True
assert expected_ret == ret['result']
# don't format chained
expected_ret = 'Zm9v'
status, ret = util._encode_base64('test',
{'args': {'starting_string': 'foo'}},
{'chaining_args': {'result': 'bar', 'status': True}})
assert status is True
assert expected_ret == ret['result']
# no chained
expected_ret = 'Zm9vIHt9'
status, ret = util._encode_base64('test',
{'args': {'starting_string': 'foo {}', 'format_chained': False}},
{'chaining_args': {'result': 'bar', 'status': True}})
assert status is True
assert expected_ret == ret['result']
| python |
import numpy as np
def digest_indices(indices):
if type(indices)==str:
if indices in ['all', 'All', 'ALL']:
indices = 'all'
else:
raise ValueError()
elif type(indices) in [int, np.int64, np.int]:
indices = np.array([indices], dtype='int64')
elif hasattr(indices, '__iter__'):
indices = np.array(indices, dtype='int64')
return indices
| python |
# -*- coding: utf-8 -*-
# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Unittests for SDK stages."""
from __future__ import print_function
import json
import os
import unittest
import six
from chromite.cbuildbot import cbuildbot_unittest
from chromite.cbuildbot import commands
from chromite.cbuildbot.stages import generic_stages
from chromite.cbuildbot.stages import generic_stages_unittest
from chromite.cbuildbot.stages import sdk_stages
from chromite.lib import constants
from chromite.lib import cros_build_lib
from chromite.lib import cros_test_lib
from chromite.lib import osutils
from chromite.lib import perf_uploader
from chromite.lib import portage_util
from chromite.lib import toolchain
from chromite.lib.buildstore import FakeBuildStore
from chromite.lib.parser import package_info
from chromite.scripts import upload_prebuilts
class SDKBuildToolchainsStageTest(generic_stages_unittest.AbstractStageTestCase,
cbuildbot_unittest.SimpleBuilderTestCase):
"""Tests SDK toolchain building."""
RELEASE_TAG = 'ToT.0.0'
def setUp(self):
self.buildstore = FakeBuildStore()
# This code has its own unit tests, so no need to go testing it here.
self.run_mock = self.PatchObject(commands, 'RunBuildScript')
self.uploadartifact_mock = self.PatchObject(
generic_stages.ArchivingStageMixin, 'UploadArtifact')
def ConstructStage(self):
self._run.GetArchive().SetupArchivePath()
return sdk_stages.SDKBuildToolchainsStage(self._run, self.buildstore)
def testNormal(self):
"""Basic run through the main code."""
self._Prepare('chromiumos-sdk')
self.PatchObject(
os, 'listdir', return_value=[
'i686-pc.tar.xz',
'x86_64-cros.tar.xz',
])
self.RunStage()
self.assertEqual(self.run_mock.call_count, 2)
self.assertEqual(self.uploadartifact_mock.call_count, 2)
# Sanity check args passed to RunBuildScript.
for call in self.run_mock.call_args_list:
buildroot, cmd = call[0]
self.assertTrue(isinstance(buildroot, six.string_types))
self.assertTrue(isinstance(cmd, (tuple, list)))
for ele in cmd:
self.assertTrue(isinstance(ele, six.string_types))
class SDKPackageStageTest(generic_stages_unittest.AbstractStageTestCase,
cbuildbot_unittest.SimpleBuilderTestCase):
"""Tests SDK package and Manifest creation."""
RELEASE_TAG = 'ToT.0.0'
fake_packages = (('cat1/package', '1'), ('cat1/package', '2'),
('cat2/package', '3'), ('cat2/package', '4'))
def setUp(self):
self.buildstore = FakeBuildStore()
# Replace sudo_run, since we don't care about sudo.
self.PatchObject(cros_build_lib, 'sudo_run', wraps=cros_build_lib.run)
self.uploadartifact_mock = self.PatchObject(
generic_stages.ArchivingStageMixin, 'UploadArtifact')
# Prepare a fake chroot.
self.fake_chroot = os.path.join(self.build_root, 'chroot/build/amd64-host')
self.fake_json_data = {}
osutils.SafeMakedirs(self.fake_chroot)
osutils.Touch(os.path.join(self.fake_chroot, 'file'))
for package, v in self.fake_packages:
cpv = package_info.SplitCPV('%s-%s' % (package, v))
self.fake_json_data.setdefault(cpv.cp, []).append([v, {}])
def ConstructStage(self):
self._run.GetArchive().SetupArchivePath()
return sdk_stages.SDKPackageStage(self._run, self.buildstore)
def testTarballCreation(self):
"""Tests whether we package the tarball and correctly create a Manifest."""
# We'll test this separately.
self.PatchObject(sdk_stages.SDKPackageStage, '_SendPerfValues')
self._Prepare('chromiumos-sdk')
fake_tarball = os.path.join(self.build_root, 'built-sdk.tar.xz')
fake_manifest = os.path.join(self.build_root, 'built-sdk.tar.xz.Manifest')
self.PatchObject(
portage_util, 'ListInstalledPackages', return_value=self.fake_packages)
self.RunStage()
# Check tarball for the correct contents.
output = cros_build_lib.run(
['tar', '-I', 'xz', '-tvf', fake_tarball],
encoding='utf-8', capture_output=True).stdout.splitlines()
# First line is './', use it as an anchor, count the chars, and strip as
# much from all other lines.
stripchars = len(output[0]) - 1
tar_lines = [x[stripchars:] for x in output]
self.assertNotIn('/build/amd64-host/', tar_lines)
self.assertIn('/file', tar_lines)
# Verify manifest contents.
real_json_data = json.loads(osutils.ReadFile(fake_manifest))
self.assertEqual(real_json_data['packages'], self.fake_json_data)
self.uploadartifact_mock.assert_called_once_with(
fake_tarball, strict=True, archive=True)
def testPerf(self):
"""Check perf data points are generated/uploaded."""
m = self.PatchObject(perf_uploader, 'UploadPerfValues')
sdk_data = 'asldjfasf'
sdk_size = len(sdk_data)
sdk_tarball = os.path.join(self.tempdir, 'sdk.tar.xz')
osutils.WriteFile(sdk_tarball, sdk_data)
tarball_dir = os.path.join(self.tempdir, constants.DEFAULT_CHROOT_DIR,
constants.SDK_TOOLCHAINS_OUTPUT)
arm_tar = os.path.join(tarball_dir, 'arm-cros-linux-gnu.tar.xz')
x86_tar = os.path.join(tarball_dir, 'i686-pc-linux-gnu.tar.xz')
osutils.Touch(arm_tar, makedirs=True)
osutils.Touch(x86_tar, makedirs=True)
self._Prepare('chromiumos-sdk')
stage = self.ConstructStage()
# pylint: disable=protected-access
stage._SendPerfValues(self.tempdir, sdk_tarball, 'http://some/log',
'123.4.5.6', 'sdk-bot')
# pylint: enable=protected-access
perf_values = m.call_args[0][0]
exp = perf_uploader.PerformanceValue(
description='base',
value=sdk_size,
units='bytes',
higher_is_better=False,
graph='cros-sdk-size',
stdio_uri='http://some/log',
)
self.assertEqual(exp, perf_values[0])
exp = set((
perf_uploader.PerformanceValue(
description='arm-cros-linux-gnu',
value=0,
units='bytes',
higher_is_better=False,
graph='cros-sdk-size',
stdio_uri='http://some/log',
),
perf_uploader.PerformanceValue(
description='i686-pc-linux-gnu',
value=0,
units='bytes',
higher_is_better=False,
graph='cros-sdk-size',
stdio_uri='http://some/log',
),
perf_uploader.PerformanceValue(
description='base_plus_arm-cros-linux-gnu',
value=sdk_size,
units='bytes',
higher_is_better=False,
graph='cros-sdk-size',
stdio_uri='http://some/log',
),
perf_uploader.PerformanceValue(
description='base_plus_i686-pc-linux-gnu',
value=sdk_size,
units='bytes',
higher_is_better=False,
graph='cros-sdk-size',
stdio_uri='http://some/log',
),
))
self.assertEqual(exp, set(perf_values[1:]))
platform_name = m.call_args[0][1]
self.assertEqual(platform_name, 'sdk-bot')
test_name = m.call_args[0][2]
self.assertEqual(test_name, 'sdk')
kwargs = m.call_args[1]
self.assertEqual(kwargs['revision'], 123456)
class SDKPackageToolchainOverlaysStageTest(
generic_stages_unittest.AbstractStageTestCase):
"""Tests board toolchain overlay installation and packaging."""
def setUp(self):
self.buildstore = FakeBuildStore()
# Mock out running of cros_setup_toolchains.
self.PatchObject(commands, 'RunBuildScript', wraps=self.FakeRunBuildScript)
self._setup_toolchain_cmds = []
# Prepare a fake chroot.
self.fake_chroot = os.path.join(self.build_root, 'chroot/build/amd64-host')
osutils.SafeMakedirs(self.fake_chroot)
osutils.Touch(os.path.join(self.fake_chroot, 'file'))
def FakeRunBuildScript(self, build_root, cmd, chromite_cmd=False, **kwargs):
if cmd[0] == 'cros_setup_toolchains':
self.assertEqual(self.build_root, build_root)
self.assertTrue(chromite_cmd)
self.assertTrue(kwargs.get('enter_chroot', False))
self.assertTrue(kwargs.get('sudo', False))
# Drop a uniquely named file in the toolchain overlay merged location.
sysroot = None
board = None
targets = None
for opt in cmd[1:]:
if opt.startswith('--sysroot='):
sysroot = opt[len('--sysroot='):]
elif opt.startswith('--include-boards='):
board = opt[len('--include-boards='):]
elif opt.startswith('--targets='):
targets = opt[len('--targets='):]
self.assertTrue(sysroot)
self.assertTrue(board)
self.assertEqual('boards', targets)
merged_dir = os.path.join(self.build_root, constants.DEFAULT_CHROOT_DIR,
sysroot.lstrip(os.path.sep))
osutils.Touch(os.path.join(merged_dir, board + '.tmp'))
def ConstructStage(self):
return sdk_stages.SDKPackageToolchainOverlaysStage(self._run,
self.buildstore)
# TODO(akeshet): determine why this test is flaky
@unittest.skip('Skip flaky test.')
def testTarballCreation(self):
"""Tests that tarballs are created for all board toolchains."""
self._Prepare('chromiumos-sdk')
self.RunStage()
# Check that a tarball was created correctly for all toolchain sets.
sdk_toolchains = set(toolchain.GetToolchainsForBoard('sdk'))
all_toolchain_combos = set()
for board in self._run.site_config.GetBoards():
try:
toolchains = set(toolchain.GetToolchainsForBoard(board).keys())
if toolchains.issubset(sdk_toolchains):
all_toolchain_combos.add('-'.join(sorted(toolchains)))
except portage_util.MissingOverlayError:
pass
for toolchains in all_toolchain_combos:
overlay_tarball = os.path.join(
self.build_root, constants.DEFAULT_CHROOT_DIR,
constants.SDK_OVERLAYS_OUTPUT,
'built-sdk-overlay-toolchains-%s.tar.xz' % toolchains)
output = cros_build_lib.run(
['tar', '-I', 'xz', '-tf', overlay_tarball],
encoding='utf-8', capture_output=True).stdout.splitlines()
# Check that the overlay tarball contains a marker file and that the
# board recorded by this marker file indeed uses the toolchains for which
# the tarball was built.
tmp_files = [os.path.basename(x) for x in output if x.endswith('.tmp')]
self.assertEqual(1, len(tmp_files))
board = tmp_files[0][:-len('.tmp')]
board_toolchains = '-'.join(
sorted(toolchain.GetToolchainsForBoard(board).keys()))
self.assertEqual(toolchains, board_toolchains)
class SDKTestStageTest(generic_stages_unittest.AbstractStageTestCase):
"""Tests SDK test phase."""
def setUp(self):
self.buildstore = FakeBuildStore()
# This code has its own unit tests, so no need to go testing it here.
self.run_mock = self.PatchObject(cros_build_lib, 'run')
def ConstructStage(self):
return sdk_stages.SDKTestStage(self._run, self.buildstore)
def testNormal(self):
"""Basic run through the main code."""
self._Prepare('chromiumos-sdk')
self.RunStage()
class SDKUprevStageTest(generic_stages_unittest.AbstractStageTestCase):
"""Tests SDK Uprev stage."""
_VERSION = '2017.09.01.155318'
def ConstructStage(self):
return sdk_stages.SDKUprevStage(
self._run, self.buildstore, version=self._VERSION)
def testUprev(self):
recorded_args = []
self.PatchObject(upload_prebuilts, 'RevGitFile',
lambda *args, **kwargs: recorded_args.append(args))
out_dir = os.path.join(self.build_root, 'chroot', 'tmp', 'toolchain-pkgs')
osutils.SafeMakedirs(out_dir)
osutils.Touch(os.path.join(out_dir, 'fake_sdk.tar.xz'))
self._Prepare('chromiumos-sdk')
self.RunStage()
# upload_prebuilts.RevGitFile should be called exact once.
self.assertEqual(1, len(recorded_args))
sdk_conf, sdk_settings = recorded_args[0]
self.assertEqual(
sdk_conf,
os.path.join(self.build_root, 'src', 'third_party',
'chromiumos-overlay', 'chromeos', 'binhost', 'host',
'sdk_version.conf'))
self.assertEqual(
sdk_settings, {
'SDK_LATEST_VERSION': self._VERSION,
'TC_PATH': '2017/09/%(target)s-2017.09.01.155318.tar.xz'
})
class SDKUtilTest(cros_test_lib.RunCommandTempDirTestCase):
"""Tests various utility functions."""
def testCreateTarballBasic(self):
"""Basic sanity checks for CreateTarball."""
sdk_stages.CreateTarball(self.tempdir, '/chromite.tar')
self.assertCommandContains(['tar', '/chromite.tar', '.'])
def testCreateTarballExclude(self):
"""Verify CreateTarball exclude_path handling."""
sdk_stages.CreateTarball(self.tempdir, '/chromite.tar',
exclude_paths=['tmp', 'usr/lib/debug'])
self.assertCommandContains(
['tar', '--anchored', '--exclude=./tmp/*',
'--exclude=./usr/lib/debug/*', '/chromite.tar', '.'])
| python |
from app.core.exceptions import BaseException
class ValidationError(BaseException):
def __init__(self, error_message):
self.error_message = error_message
super(BaseException, self).__init__(error_message)
class AuthenticationError(BaseException):
def __init__(self, error_message):
self.error_message = error_message
| python |
#!/usr/bin/env python
# Django environment setup:
from django.conf import settings, global_settings as default_settings
from django.core.management import call_command
from os.path import dirname, realpath, join
import sys
# Detect location and available modules
module_root = dirname(realpath(__file__))
# Inline settings file
settings.configure(
DEBUG = False, # will be False anyway by DjangoTestRunner.
TEMPLATE_DEBUG = True,
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:'
}
},
TEMPLATE_LOADERS = (
'django.template.loaders.app_directories.Loader',
),
TEMPLATE_CONTEXT_PROCESSORS = default_settings.TEMPLATE_CONTEXT_PROCESSORS + (
'django.core.context_processors.request',
),
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sites',
'fluent_pages',
'fluent_pages.tests.testapp',
'mptt',
'polymorphic',
'polymorphic_tree',
),
SITE_ID = 4,
ROOT_URLCONF = 'fluent_pages.tests.testapp.urls',
FLUENT_PAGES_TEMPLATE_DIR = join(module_root, 'fluent_pages', 'tests', 'testapp', 'templates'),
)
call_command('syncdb', verbosity=1, interactive=False, traceback=True)
# ---- app start
verbosity = 2 if '-v' in sys.argv else 1
from django.test.utils import get_runner
TestRunner = get_runner(settings) # DjangoTestSuiteRunner
runner = TestRunner(verbosity=verbosity, interactive=True, failfast=False)
failures = runner.run_tests(['fluent_pages'])
if failures:
sys.exit(bool(failures))
| python |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
:Mod: test_async
:Synopsis:
:Author:
servilla
:Created:
4/22/21
"""
import asyncio
from datetime import datetime
import re
from typing import List
import daiquiri
import pendulum
from soh.config import Config
import soh.asserts.server
from soh.server.server import ApacheServer
from soh.server.server import ApacheTomcatServer
from soh.server.server import AuditServer
from soh.server.server import AuthServer
from soh.server.server import GmnServer
from soh.server.server import JettyServer
from soh.server.server import LdapServer
from soh.server.server import PackageServer
from soh.server.server import PortalServer
from soh.server.server import Server
from soh.server.server import SolrServer
from soh.server.server import TomcatServer
logger = daiquiri.getLogger(__name__)
hosts = [
"pasta-d.lternet.edu",
"pasta-s.lternet.edu",
"pasta.lternet.edu",
"portal-d.edirepository.org",
"portal-s.edirepository.org",
"portal.edirepository.org",
"package-d.lternet.edu",
"package-s.lternet.edu",
"package.lternet.edu",
"audit-d.lternet.edu",
"audit-s.lternet.edu",
"audit.lternet.edu",
"gmn-s.lternet.edu",
"gmn.lternet.edu",
"gmn-s.edirepository.org",
"gmn.edirepository.org",
"solr-d.lternet.edu",
"solr-s.lternet.edu",
"solr.lternet.edu",
"auth.edirepository.org",
"ldap.edirepository.org",
"unit.lternet.edu",
"vocab.lternet.edu",
"seo.edirepository.org",
"tweeter.edirepository.org",
"space.lternet.edu",
"josh.lternet.edu",
"ezeml.edirepository.org",
"web-x.edirepository.org"
]
status: dict = {}
def test_hosts():
for host in hosts:
status[host] = [0, None]
print()
start_time = datetime.now()
loop = asyncio.get_event_loop()
task1 = loop.create_task(check_hosts())
task2 = loop.create_task(check_uptimes())
task3 = loop.create_task(check_read_only())
tasks = asyncio.gather(task1, task2, task3)
loop.run_until_complete(tasks)
end_time = datetime.now()
print(f"Testing done: {end_time - start_time} seconds")
for host in hosts:
print(host, status[host])
async def check_hosts():
for host in hosts:
await do_check(host)
async def do_check(host=None):
server = None
if host in Config.server_types["APACHE"]:
server = ApacheServer(host=host)
elif host in Config.server_types["APACHE_TOMCAT"]:
server = ApacheTomcatServer(host=host)
elif host in Config.server_types["AUDIT"]:
server = AuditServer(host=host)
elif host in Config.server_types["AUTH"]:
server = AuthServer(host=host)
elif host in Config.server_types["GMN"]:
server = GmnServer(host=host)
elif host in Config.server_types["JETTY"]:
server = JettyServer(host=host)
elif host in Config.server_types["LDAP"]:
server = LdapServer(host=host)
elif host in Config.server_types["PACKAGE"]:
server = PackageServer(host=host)
elif host in Config.server_types["PORTAL"]:
server = PortalServer(host=host)
elif host in Config.server_types["SERVER"]:
server = Server(host=host)
elif host in Config.server_types["SOLR"]:
server = SolrServer(host=host)
elif host in Config.server_types["TOMCAT"]:
server = TomcatServer(host=host)
else:
logger.error(f"Unknown server: {host}")
return
status[host][0] = await server.check_server()
async def check_read_only():
for host in hosts:
await do_read_only(host)
async def do_read_only(host):
host_ro = await soh.asserts.server.read_only(host=host)
if host_ro:
status[host][0] = status[host][0] | Config.assertions["READ_ONLY"]
async def check_uptimes():
for host in hosts:
await do_uptime(host)
async def do_uptime(host):
host_uptime = await soh.asserts.server.uptime(host=host)
status[host][1] = host_uptime
if host_uptime is not None:
status[host][0] = status[host][0] | load_status(get_load(host_uptime))
def get_load(uptime: str):
load = None
if uptime is not None:
match = re.search(r"\d?\d\.\d\d, \d?\d\.\d\d, \d?\d\.\d\d", uptime)
if match:
load = [float(_.strip()) for _ in match.group().split(",")]
return load
def load_status(load: List) -> int:
ls = Config.UP
if load is None:
ls = Config.assertions["LOAD_HIGH"]
else:
load1 = load[0]
load5 = load[1]
load15 = load[2]
if load1 >= Config.LOAD1_MAX:
ls = Config.assertions["LOAD_HIGH"]
return ls
| python |
# Calculates heatwaves using Nairn's methodology
# Nairn et al. (2009). Defining and predicting Excessive Heat events, a National System
import numpy as np
# Defines runing mean functions:
def moving_average_3(x, N=3):
return np.convolve(x, np.ones((N,))/N)[(N-1):]
def moving_average_30(x, N=30):
return np.convolve(x, np.ones((N,))/N)[(N-1):]
# Loads text files with maximun and minimum temperature
TxtFileTmax = np.loadtxt('melbourne.acorn.sat.maxT.086071.daily.txt')
TxtFileTmin = np.loadtxt('melbourne.acorn.sat.minT.086071.daily.txt')
# Defines length of files
ShapeTmaxTmin = TxtFileTmax.shape
n = (ShapeTmaxTmin[0])
#Creates arrays to fill-in variables data
Year = np.zeros((n), dtype=int)
Month = np.zeros((n), dtype=int)
Day = np.zeros((n), dtype=int)
Tmax = np.zeros((n), dtype=float)
Tmin = np.zeros((n), dtype=float)
ADT = np.zeros((n), dtype=float)
# Fills-in data in arrays
for i in xrange(n):
Year[i] = TxtFileTmax[i,0]
Month[i] = TxtFileTmax[i,1]
Day[i] = TxtFileTmax[i,2]
Tmax[i] = TxtFileTmax[i,3]
Tmin[i] = TxtFileTmin[i,3]
# Calcualtes average daily temperature (ADT)
# ADT is equal to the average of daily maximun (Tmax)
# and minimum temperature (Tmin)
for i in xrange(n):
ADT[i] = (Tmax[i]+Tmin[i])/2
# Calculates Excess Heat
# Climatological Excess Heat Index (EHIsig)
EHIsig = np.zeros((n,4), dtype=float)
movavgadt_3 = moving_average_3(ADT)
for i in xrange(n):
EHIsig[i,0]=Year[i]
EHIsig[i,1]=Month[i]
EHIsig[i,2]=Day[i]
# 95th percentile calcualte in excel, across all ADT days for the period 1960-2011 according to Nairn's methodology
Pctl95 = 22.2
for i in xrange(n):
EHIsig[i,3] = movavgadt_3[i] - Pctl95
#print EHIsig
# Calcualtes Heat Stress
# Excess Heat Index (EHIaccl)
movavgadt_30 = moving_average_30(ADT)
EHIaccl = np.zeros((n,4), dtype=float)
for i in xrange(n):
EHIaccl[i,0]=Year[i]
EHIaccl[i,1]=Month[i]
EHIaccl[i,2]=Day[i]
for i in xrange(0,n-30):
EHIaccl[i,3]=movavgadt_3[i+30]-movavgadt_30[i]
#print EHIaccl
# Calculates Excess Heat Factor (EHF)
# First and last 30 values of caluclations are not valid (running mean)
EHF = np.zeros((n,4), dtype=float)
for i in xrange(n):
EHF[i,0]=Year[i]
EHF[i,1]=Month[i]
EHF[i,2]=Day[i]
EHF[i,3]=abs(EHIaccl[i,3])*EHIsig[i,3]
# Selects values only form 1960-2011
s = range(n-1096,n)
t = range(18262)
EHF = np.delete(EHF, (s), axis=0)
EHF = np.delete(EHF, (t), axis=0)
# Writes result into a textfile
np.savetxt('melbourne.heatwaves.nairn.1960-2011.txt', EHF, fmt='%s')
print EHF
| python |
#!/usr/bin/env python2.7
import os
def system_dependency(name):
print "installing system dependency {}".format(name)
os.system('sudo apt-get install %s' % name)
print "done!"
| python |
import sys
import os
import shutil
import re
import glob
import struct
import math
import collections
import argparse
import csv
from lib import csv_classes
fpath=os.path.realpath(__file__)
py_path=os.path.dirname(fpath)
endian = "little"
pack_int = '<i'
INT_BYTES=4
STR_BYTES=20
def parseError(error_string, line, index):
sys.exit("Invalid line in csv. Line: " + str(line) + " - Index: " + str(index) + " " + error_string)
def iterateRow(line, row, current_keystring, current_fields, csv_header):
for i in range(len(row)):
if i == 0:
if not row[i]:
#not a new keystring but continuation of the previous line
if not current_keystring:
parseError("Leading comma without a valid keystring.", line, i)
#else just let the rest of the elements be added as fields
elif row[i][0] == '#':
#comment do nothing
print("Skipping line: " + str(line) + " because it is commented out")
return current_keystring, current_fields
elif row[i] and row !="#":
#add new keystring
if not current_keystring:
current_keystring = row[i]
elif len(current_fields):
csv_header.addTable(current_keystring, current_fields)
current_keystring = row[i]
current_fields = []
else:
parseError("Keystring: " + current_keystring + " does not have any fields.", line, i)
else:
if not row[i]:
#skip
None
elif row[i][0] == '#':
#comment, continue
print("Skipping line: " + str(line) + " after cell: " + str(i) + " because it is commented out")
return current_keystring, current_fields
else:
#add field to list
current_fields.append(row[i])
return current_keystring, current_fields
def execute(is_big_endian, print, input_csv, output_csv):
if is_big_endian:
#lazy but also set these in all sub classes
csv_classes.endian='big'
csv_classes.float_endian = '>f'
csv_classes.int_endian = '>i'
csv_classes.short_endian = '>h'
else:
#lazy but also set these in all sub classes
csv_classes.endian='little'
csv_classes.float_endian = '<f'
csv_classes.int_endian = '<i'
csv_classes.short_endian = '<h'
input_reader = open(input_csv, newline='')
csv_reader = csv.reader(input_reader, delimiter=',')
csv_header = csv_classes.CSVHeader()
current_keystring = ""
current_fields = []
line = 0;
for row in csv_reader:
current_keystring, current_fields = iterateRow(line, row, current_keystring, current_fields, csv_header)
line+=1
#add last fields if they exist
if current_keystring:
if len(current_fields):
csv_header.addTable(current_keystring, current_fields)
else:
parseError("Keystring: " + current_keystring + " does not have any fields.", line, 0)
#now convert header to bytes!
#run twice to fix indices
if print:
csv_header.pretty_print()
csv_header.to_bytes()
input_reader.close()
csv_writer = open(output_csv, "wb")
csv_writer.write(csv_header.to_bytes())
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Rebuild a CSV file")
endian = parser.add_mutually_exclusive_group()
endian.add_argument("-g", "--gamecube", help="Use gamecube endian - small endian", action="store_true")
endian.add_argument("-x", "--xbox", help="Use xbox endian - big endian [Default]", action="store_true")
parser.add_argument("-p", "--print", help="Print the parsed csv", action="store_true")
parser.add_argument("input", help="Input CSV file")
parser.add_argument("output", type=str, help="Output file")
args = parser.parse_args()
#set endianess - xbox default
execute(args.gamecube, args.print, args.input, args.output)
| python |
# -*- coding: utf-8 -*-
"""
Import Modules
Configure the Database
Instantiate Classes
"""
if settings.get_L10n_languages_readonly():
# Make the Language files read-only for improved performance
T.is_writable = False
get_vars = request.get_vars
# Are we running in debug mode?
settings.check_debug()
import datetime
try:
import json # try stdlib (Python 2.6)
except ImportError:
try:
import simplejson as json # try external module
except:
import gluon.contrib.simplejson as json # fallback to pure-Python module
########################
# Database Configuration
########################
migrate = settings.get_base_migrate()
fake_migrate = settings.get_base_fake_migrate()
if migrate:
check_reserved = ("mysql", "postgres")
else:
check_reserved = []
(db_string, pool_size) = settings.get_database_string()
if db_string.find("sqlite") != -1:
db = DAL(db_string,
check_reserved=check_reserved,
migrate_enabled = migrate,
fake_migrate_all = fake_migrate,
lazy_tables = not migrate)
# on SQLite 3.6.19+ this enables foreign key support (included in Python 2.7+)
# db.executesql("PRAGMA foreign_keys=ON")
else:
try:
if db_string.find("mysql") != -1:
# Use MySQLdb where available (pymysql has given broken pipes)
# - done automatically now, no need to add this manually
#try:
# import MySQLdb
# from gluon.dal import MySQLAdapter
# MySQLAdapter.driver = MySQLdb
#except ImportError:
# # Fallback to pymysql
# pass
if check_reserved:
check_reserved = ["postgres"]
db = DAL(db_string,
check_reserved = check_reserved,
pool_size = pool_size,
migrate_enabled = migrate,
lazy_tables = not migrate)
else:
# PostgreSQL
if check_reserved:
check_reserved = ["mysql"]
db = DAL(db_string,
check_reserved = check_reserved,
pool_size = pool_size,
migrate_enabled = migrate,
lazy_tables = not migrate)
except:
db_type = db_string.split(":", 1)[0]
db_location = db_string.split("@", 1)[1]
raise(HTTP(503, "Cannot connect to %s Database: %s" % (db_type, db_location)))
current.db = db
db.set_folder("upload")
# Sessions Storage
if settings.get_base_session_memcache():
# Store sessions in Memcache
from gluon.contrib.memcache import MemcacheClient
cache.memcache = MemcacheClient(request,
[settings.get_base_session_memcache()])
from gluon.contrib.memdb import MEMDB
session.connect(request, response, db=MEMDB(cache.memcache))
####################################################################
# Instantiate Classes from Modules #
# - store instances in current to be accessible from other modules #
####################################################################
from gluon.tools import Mail
mail = Mail()
current.mail = mail
from gluon.storage import Messages
messages = Messages(T)
current.messages = messages
ERROR = Messages(T)
current.ERROR = ERROR
# Import the S3 Framework
if update_check_needed:
# Reload the Field definitions
reload(s3base.s3fields)
else:
import s3 as s3base
# Set up logger (before any module attempts to use it!)
import s3log
s3log.S3Log.setup()
# AAA
current.auth = auth = s3base.AuthS3()
# Use session for persistent per-user variables
# - beware of a user having multiple tabs open!
# - don't save callables or class instances as these can't be pickled
if not session.s3:
session.s3 = Storage()
# Use username instead of email address for logins
# - would probably require further customisation
# to get this fully-working within Eden as it's not a Tested configuration
#auth.settings.login_userfield = "username"
auth.settings.hmac_key = settings.get_auth_hmac_key()
auth.define_tables(migrate=migrate, fake_migrate=fake_migrate)
current.audit = audit = s3base.S3Audit(migrate=migrate, fake_migrate=fake_migrate)
# Shortcuts for models/controllers/views
s3_has_role = auth.s3_has_role
s3_has_permission = auth.s3_has_permission
s3_logged_in_person = auth.s3_logged_in_person
# Calendar
current.calendar = s3base.S3Calendar()
# CRUD
s3.crud = Storage()
# S3 Custom Validators and Widgets, imported here into the global
# namespace in order to access them without the s3base namespace prefix
s3_action_buttons = s3base.S3CRUD.action_buttons
s3_fullname = s3base.s3_fullname
s3_redirect_default = s3base.s3_redirect_default
S3ResourceHeader = s3base.S3ResourceHeader
from s3.s3navigation import s3_rheader_tabs
from s3.s3validators import *
from s3.s3widgets import *
from s3.s3data import *
# GIS Module
gis = s3base.GIS()
current.gis = gis
# s3_request
s3_request = s3base.s3_request
# Field Selectors
FS = s3base.FS
# S3XML
s3xml = s3base.S3XML()
current.xml = s3xml
# Messaging
msg = s3base.S3Msg()
current.msg = msg
# Sync
sync = s3base.S3Sync()
current.sync = sync
# -----------------------------------------------------------------------------
def s3_clear_session():
# CRUD last opened records (rcvars)
s3base.s3_remove_last_record_id()
# Session-owned records
if "owned_records" in session:
del session["owned_records"]
if "s3" in session:
s3 = session.s3
opts = ["hrm", "report_options", "utc_offset", "deduplicate"]
for o in opts:
if o in s3:
del s3[o]
# -----------------------------------------------------------------------------
def s3_auth_on_login(form):
"""
Actions to be performed upon successful login
Do not redirect from here!
"""
s3_clear_session()
# -----------------------------------------------------------------------------
def s3_auth_on_logout(user):
"""
Actions to be performed after logout
Do not redirect from here!
"""
s3_clear_session()
# END =========================================================================
| python |
from __future__ import annotations
import numpy as np
from nn_recipe.NN.ActivationFunctions.__factory import ActivationFunctionFactory
from nn_recipe.NN.Layers.__layer import Layer
from nn_recipe.NN.__function import Function
class Linear(Layer):
"""
This Class represents a Linear Layer (Dense - Fully connected)
Linear Layer is responsible for:
- Calculating the forward path Z = W * X.T
- Calculating activation of the layer Y = Act(Z)
- Calculating local gradients that will be used by the optimizers
Gradient Calculated are:
1. dW: ∂Y/∂Z * ∂Z/∂W = activation gradient * X
2. dX: ∂Y/∂Z * ∂Z/∂X = activation gradient * W
3. dB: ∂Y/∂Z * ∂Z/∂B = activation gradient * 1
:cvar ID: unique id for the activation function used by the layer loader
"""
@staticmethod
def load(data):
"""
This function is used to create a new layer based on the descriptor
:rtype: Linear
"""
act = ActivationFunctionFactory(data.pop("activation"))
return Linear(in_dim=data.pop("in_dim"), out_dim=data.pop("out_dim"), activation=act, **data)
ID = 0
def __init__(self, in_dim, out_dim, activation, **kwargs):
"""
Initializes the layer by calling base class constructor to create weights and bias and initialize them
:param in_dim: number of neurons of the previous layer
:type in_dim: int
:param out_dim: number of neurons of the current layer
:type out_dim: int
:param activation: activation function that will be used
:type activation: Function
:keyword weights: Initial value for layer weights
:keyword bias: Initial value for layer bias
:raise TypeError: When the given initial data doesn't have the expected type
:raise ShapeError: When the given initial data doesn't have the expected shape
"""
self.__activation = activation
super(Linear, self).__init__(in_dim, out_dim, **kwargs)
def _init_params(self):
"""
Initializes layer parameters (weights and bias)
"""
# factor = np.tanh(1/self._in_dim) # factor that will be used to normalize params
factor = np.sqrt(1 / self._in_dim)
self._weights = np.random.normal(0, factor, (self._out_dim, self._in_dim)) # init weights
self._bias = np.random.normal(0, factor, (self._out_dim, 1))
# self._bias = np.ones((self._out_dim, self.__batch_size)) # init bias
def _forward(self, x):
"""
Calculates forward path (W*X.t) then apply activation function
:param x: input to the layer (output from the previous layer)
:type x: np.ndarray
:rtype: np.ndarray
"""
return self.__activation(np.dot(self._weights, x.T) + self._bias).T
def _calc_local_grad(self, x):
"""
Local gradient calculations
Gradient Calculated are:
1. dW: ∂Z/∂W = X
2. dX: ∂Z/∂X = W
3. dZ: ∂Y/∂Z = activation gradient
:note: No need to return ∂Z/∂B as it's always 1
:param x: input to the layer (output from the previous layer)
:type x: np.ndarray
:rtype: dict[str, np.ndarray]
"""
return {
'dW': x,
'dX': np.copy(self.weights),
'dZ': self.__activation.local_grad
}
def _save(self):
"""
Methode used to get the data that will be saved in the save phase
Expected Descriptor Structure:
- ID: unique id for each layer (0 in case of Linear Layer)
- in_dim: number of inputs (number of neurons in the previous layer)
- iut_dim: number of neurons in the current layer
- activation: Activation function descriptor
- bias: numpy array represents the bias used by the layer
- weights: numpy array represents the weights used by the layer
"""
return {
"in_dim": self._in_dim,
"out_dim": self._out_dim,
"activation": self.__activation.save(),
"bias": self._bias,
"weights": self._weights
}
| python |
{
'targets': [
{
'target_name': 'binding',
'includes': [ 'deps/snappy/common.gypi' ],
'include_dirs': [ '<!(node -e "require(\'nan\')")', 'deps/snappy/<(os_include)' ],
'dependencies': [ 'deps/snappy/snappy.gyp:snappy' ],
'sources': [ 'src/binding.cc' ]
}
]
}
| python |
import numpy as np
import math
import matplotlib.pyplot as plt
from sklearn import metrics
import argparse
from functools import partial
def distance_from_unif(samples, test='ks'):
sorted_samples = np.sort(samples, axis=1)
try:
assert (np.greater_equal(sorted_samples, 0)).all(), np.min(sorted_samples)
assert (np.less_equal(sorted_samples, 1)).all(), np.max(sorted_samples)
except AssertionError:
sorted_samples = np.maximum(sorted_samples, 0)
sorted_samples = np.minimum(sorted_samples, 1)
ts_test = partial(ts, test=test)
return np.apply_along_axis(ts_test, 1, sorted_samples)
def ts(sorted_samples, test):
n = len(sorted_samples)
if test == 'ks':
# should not include 0 but include 1
unif_cdf = list(np.arange(0, 1, 1/n))[1:] + [1.0]
return max(abs(sorted_samples - unif_cdf))
elif test == 'cvm':
# ts = 1/(12 * n)
# for i in range(1, n + 1):
# ts += (sorted_samples[i-1] - (2*i - 1)/n)**2
# return ts
return np.sum(np.square(np.array([(2*i - 1)/n for i in range(n)]) - sorted_samples)) + 1/(12*n)
elif test == 'ad':
# ts = 0
# for i in range(1, n + 1):
# ts -= (2*i - 1) * math.log(np.maximum(sorted_samples[i-1], [1e-16]))
# ts -= (2*n + 1 - 2*i) * math.log(np.maximum(1 - sorted_samples[i-1], [1e-16]))
# ts /= n
# ts -= n
# return ts
Ws = np.array([(2*i - 1) for i in range(n)]) * np.log(np.maximum(sorted_samples, [1e-16]))
Vs = np.array([(2*n + 1 - 2*i) for i in range(n)]) * np.log(np.maximum(1 - sorted_samples, [1e-16]))
return (-np.sum(Ws) - np.sum(Vs))/n - n
def compute_auc(neg, pos, pos_label=1):
ys = np.concatenate((np.zeros(len(neg)), np.ones(len(pos))), axis=0)
neg = np.array(neg)[np.logical_not(np.isnan(neg))]
pos = np.array(pos)[np.logical_not(np.isnan(pos))]
scores = np.concatenate((neg, pos), axis=0)
auc = metrics.roc_auc_score(ys, scores)
if pos_label == 1:
return auc
else:
return 1 - auc
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-i", type=str, default='',
help="Location of checkpoint to restore")
parser.add_argument("-o", type=str, default='',
help="Location of checkpoint to restore")
args = parser.parse_args()
in_samples = np.load(args.i)
out_samples = np.load(args.o)
if len(in_samples.shape) > 2:
in_samples = in_samples.reshape((in_samples.shape[0], -1))
out_samples = out_samples.reshape((out_samples.shape[0], -1))
# in_samples = np.random.uniform(size=(20, 3072))
# out_samples = np.random.beta(a=1, b=1.5, size=(20, 3072))
# for test in ['ks', 'cvm', 'ad']:
for test in ['ad']:
in_d = distance_from_unif(in_samples, test)
print(np.min(in_d), np.max(in_d))
out_d = distance_from_unif(out_samples, test)
print(np.min(out_d), np.max(out_d))
auc_unif = compute_auc(out_d * -1, in_d * -1)
print(f'UNIF: {auc_unif}') | python |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
from alipay.aop.api.constant.ParamConstants import *
class AlipayUserElectronicidUserbarcodeCreateModel(object):
def __init__(self):
self._cert_id = None
self._expire_time = None
@property
def cert_id(self):
return self._cert_id
@cert_id.setter
def cert_id(self, value):
self._cert_id = value
@property
def expire_time(self):
return self._expire_time
@expire_time.setter
def expire_time(self, value):
self._expire_time = value
def to_alipay_dict(self):
params = dict()
if self.cert_id:
if hasattr(self.cert_id, 'to_alipay_dict'):
params['cert_id'] = self.cert_id.to_alipay_dict()
else:
params['cert_id'] = self.cert_id
if self.expire_time:
if hasattr(self.expire_time, 'to_alipay_dict'):
params['expire_time'] = self.expire_time.to_alipay_dict()
else:
params['expire_time'] = self.expire_time
return params
@staticmethod
def from_alipay_dict(d):
if not d:
return None
o = AlipayUserElectronicidUserbarcodeCreateModel()
if 'cert_id' in d:
o.cert_id = d['cert_id']
if 'expire_time' in d:
o.expire_time = d['expire_time']
return o
| python |
# coding=utf-8
import time, json, io, datetime, argparse
item_type = ('EVENT', 'INFO', 'AD')
categories = ('pregon', 'music', 'food', 'sport', 'art', 'fire', 'band')
places = {
'Alameda':(41.903501, -8.866704),
'Auditorio de San Bieito':(41.899915, -8.873203),
'A Cruzada':(41.897817, -8.874520),
'As Solanas':(41.9038126, -8.8659001),
'Rúas de A Guarda':(-1, -1),
'Porto':(41.898983, -8.874545),
'O Fuscalho':(41.902495, -8.879410),
'Centro Cultural':(41.902892, -8.865532),
'Estadio A Sangriña':(41.899626, -8.861348),
'Montiño':(41.900999, -8.866232),
'Salcidos':(41.909254, -8.852916),
'Plaza do Reló':(41.9013013,-8.8744885),
'Praia do Muíño':(41.8748281,-8.8674021),
'Colexio dos Xesuítas':(41.8883961,-8.8515421,17),
'Singular Lounge Disco':(41.902339,-8.869759),
'Atalaia':(41.9026239,-8.8800699),
'As de Copas':(41.902227,-8.869925,17),
'Santa Trega':(41.8929508,-8.8737453),
'San Caetano':(41.8945184,-8.8770014),
'Recreo artístico guardés':(41.903213,-8.87437),
'O Coruto':(41.9062441,-8.8620104),
'O Rosal':(41.936970, -8.836869),
'Praia da Lamiña':(41.877793, -8.861384),
'A Guía':(41.905326, -8.876671),
'Praza dos Seixiños (A Gándara)':(41.915780, -8.847085),
'A Sangriña':(41.900790, -8.862902),
'Castelo de Santa Cruz':(41.904506, -8.872801)
}
"""
An event can have the following fields
event = {
'EVENT_NAME':'', # Mandatory
'DAY':'', # Mandatory dd/MM/yyyy
'START_TIME':'', # Mandatory hh:mm
'END_TIME':'',
'CATEGORY':'',
'PLACE':'', # Mandatory
'LATITUDE':'',
'LONGITUDE':'',
'DESCRIPTION':'',
# New fields
'PRICE':'',
'IMG_URL':'',
'URL':'',
'TYPE':''
}
"""
def printDict(d):
for ind, key in enumerate(d):
print(str(ind) + " - " + key)
def printList(l):
for ind, item in enumerate(l):
print(str(ind) + " - " + item)
def getKey(ind, d):
# Convert dictionary keys in a tuple so they can be accessed with an index
keys = ()
for item in d:
keys = keys + (item,)
return keys[ind]
def readItemsFile():
with open("proba.txt", "r") as myfile:
events = json.load(myfile)
# All day events are coded this way to be able to use sort function
for item in events:
if item['START_TIME'] == 'Todo o día':
item['START_TIME'] = '00:02'
return events
def writeItemsFile(events):
events = sorted(events, key=lambda event: time.strptime(event['START_TIME'] + ' ' + event['DAY'], "%H:%M %d/%m/%Y"))
for item in events:
if item['START_TIME'] == '00:02':
item['START_TIME'] = 'Todo o día'
with io.open("proba.txt", "w") as myfile:
json.dump(events, myfile, ensure_ascii=False)
def removeOldEvents():
events = readItemsFile()
# Remove events from previous days
today = datetime.datetime.now().replace(hour=00, minute=00)
events = list(filter(lambda item: datetime.datetime.strptime(item['START_TIME'] + ' ' + item['DAY'], "%H:%M %d/%m/%Y") > today, events))
writeItemsFile(events)
def addItem():
events = readItemsFile()
while True:
new_event = {}
print("Tipos de eventos: ")
printList(item_type)
new_event['TYPE'] = item_type[int(input("Seleccione un número: "))]
new_event['EVENT_NAME'] = input("Evento: ")
new_event['DAY'] = input("Data dd/MM/yyyy: ")
new_event['START_TIME'] = input("Hora de inicio (hh:mm) (vacío se dura todo o día): ")
if new_event['START_TIME'] == '':
new_event['START_TIME'] = '00:02'
if new_event['TYPE'] == 'INFO' or new_event['TYPE'] == 'AD':
event_url = input("Enlace á información: ")
if event_url is not '':
new_event['URL'] = event_url
icon_img_url = input("URL da imaxe do icono: ")
if icon_img_url is not '':
new_event['IMG_URL'] = icon_img_url
if new_event['TYPE'] == 'EVENT':
print("Tipos de eventos: ")
printList(categories)
new_event['CATEGORY'] = categories[int(input("Seleccionar categoría: "))]
print("Lugares: ")
printDict(places)
new_event['PLACE'] = getKey(int(input("Seleccionar lugar: ")), places)
if new_event['PLACE'] in places:
new_event['LATITUDE'] = str(places[new_event['PLACE']][0])
new_event['LONGITUDE'] = str(places[new_event['PLACE']][1])
description = input("Descrición: ")
if description is not '':
new_event['DESCRIPTION'] = description
price = input("Precio: ")
if price is not '':
new_event['PRICE'] = price
header_img = input("URL da imaxe de cabeceira: ")
if header_img is not '':
new_event['IMG_URL'] = header_img
event_url = input("URL do evento: ")
if event_url is not '':
new_event['URL'] = event_url
print('Engadir o seguinte evento? ')
print(new_event)
if input('Engadir? (s/n): ') == 's':
events.append(new_event)
if input('Continuar? (s/n): ') == 'n':
break;
writeItemsFile(events)
# Parsing arguments
parser = argparse.ArgumentParser(description='Manage events (add or remove)')
parser.add_argument('-r', '--remove', help='Remove old events', action='store_true')
args = parser.parse_args()
if args.remove:
removeOldEvents()
else:
addItem() | python |
raise NotImplementedError("ast is not yet implemented in Skulpt")
| python |
import pylab as PL
x0 = 0.1
samplingStartTime = 1000
sampleNumber = 100
resultA = []
resultX = []
r = 0
da = 0.005
def f(x):
return r * x * (1 - x)
while r <= 4.0:
x = x0
for t in range(samplingStartTime):
x = f(x)
for t in range(sampleNumber):
x = f(x)
resultA.append(r)
resultX.append(x)
r += da
PL.plot(resultA, resultX, 'bo')
PL.show()
| python |
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.template import RequestContext
from django.shortcuts import render_to_response, get_object_or_404
from django.core.urlresolvers import reverse
from django.core.context_processors import csrf
from django.views.decorators.csrf import csrf_protect
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.conf import settings
from projects.models import Project
from common import _json_response
TEMPLATE_PATH = 'iterate/'
def _create_params(req):
p = {'breadcrumbs': [{reverse('iterate'): 'Iterate'}],
'is_iterate': True, 'nav_projects': Project.objects.filter( \
owner=req.user).exclude(production_url__exact='')}
p.update(csrf(req))
return p
@login_required
def home(req):
p = _create_params(req)
return render_to_response(TEMPLATE_PATH + 'home.html', p,
context_instance=RequestContext(req))
| python |
import numpy as np
import pandas as pd
from EstimatorSpectrum import TSVD
from Generator import LSW
from SVD import LordWillisSpektor
from test_functions import kernel_transformed, BIMODAL, BETA, SMLA, SMLB
replications = 10
size = [2000, 10000, 1000000]
max_size = 100
functions = [BETA]
functions_name = ['BETA']
taus = [1]
taus_name = ['10']
rhos = [750, 1000, 2000, 3000, 5000, 6000, 7000, 8000, 9000, 10000, 50000, 100000]
rhos_name = ['750', '1000', '2000', '3000', '5000', '6000', '7000', '8000', '9000', '10000', '50000', '100000']
if __name__ == '__main__':
for s in size:
for i, fun in enumerate(functions):
for j, tau in enumerate(taus):
for k, rho in enumerate(rhos):
generator = LSW(pdf=fun, sample_size=s, seed=914)
results = {'selected_param': [], 'oracle_param': [], 'oracle_loss': [], 'loss': [], 'solution': [],
'oracle_solution': []}
for _ in range(replications):
spectrum = LordWillisSpektor(transformed_measure=True)
obs = generator.generate()
tsvd = TSVD(kernel=kernel_transformed, singular_values=spectrum.singular_values,
left_singular_functions=spectrum.left_functions,
right_singular_functions=spectrum.right_functions,
observations=obs, sample_size=s, max_size=max_size, tau=tau,
transformed_measure=True, rho=rho)
tsvd.estimate()
tsvd.oracle(fun, patience=10)
solution = list(tsvd.solution(np.linspace(0, 1, 10000)))
results['selected_param'].append(tsvd.regularization_param)
results['oracle_param'].append(tsvd.oracle_param)
results['oracle_loss'].append(tsvd.oracle_loss)
results['loss'].append(tsvd.residual)
results['solution'].append(solution)
results['oracle_solution'].append(list(tsvd.oracle_solution))
pd.DataFrame(results).to_csv(
'TSVD_rho_{}_tau_{}_size_{}_fun_{}.csv'.format(rhos_name[k], taus_name[j], s,
functions_name[i]))
| python |
# Futu Algo: Algorithmic High-Frequency Trading Framework
# Copyright (c) billpwchan - All Rights Reserved
# Unauthorized copying of this file, via any medium is strictly prohibited
# Proprietary and confidential
# Written by Bill Chan <billpwchan@hotmail.com>, 2021
import argparse
import importlib
from multiprocessing import Process
import yaml
from engines import *
from strategies.Strategies import Strategies
from util.global_vars import *
def __daily_update_filters():
filters = list(__init_filter(filter_list=['all']))
stock_filter = StockFilter(stock_filters=filters)
stock_filter.update_filtered_equity_pools()
def daily_update_data(futu_trade, stock_list: list, force_update: bool = False):
# Daily Update Filtered Security
procs = []
proc = Process(target=__daily_update_filters) # instantiating without any argument
procs.append(proc)
proc.start()
# Daily Update Stock Info (Need to Rethink!!!)
# stock_filter.update_stock_info()
# Daily Update HKEX Security List & Subscribed Data
data_engine.HKEXInterface.update_security_list_full()
# Daily Update Owner Plate for all Stocks
full_equity_list = data_engine.HKEXInterface.get_equity_list_full()
futu_trade.update_owner_plate(stock_list=full_equity_list)
# Update basic information for all markets
futu_trade.update_stock_basicinfo()
# Update historical k-line
for stock_code in stock_list:
futu_trade.update_DW_data(stock_code, force_update=force_update, k_type=KLType.K_DAY)
futu_trade.update_DW_data(stock_code, force_update=force_update, k_type=KLType.K_WEEK)
futu_trade.update_1M_data(stock_code, force_update=force_update)
# Daily Update FuTu Historical Data
# futu_trade.store_all_data_database()
# Clean non-trading days data
DataProcessingInterface.clear_empty_data()
for proc in procs:
proc.join()
def __dynamic_instantiation(prefix: str, module_name: str, optional_parameter=None):
filter_module = importlib.import_module(f"{prefix}.{module_name}")
# Assume the class name is identical with the file name except for the underscore _
class_ = getattr(filter_module, module_name.replace("_", ""))
if optional_parameter is not None:
return class_(optional_parameter)
else:
return class_()
def __init_strategy(strategy_name: str, input_data: dict) -> Strategies:
"""
Return a trading strategy instance using a strategy name in string.
:param strategy_name: an available strategy module name in the strategies folder
:param input_data: Initialized input data for the strategy to calculate the technical indicator
:return: a strategy instance
"""
return __dynamic_instantiation(prefix="strategies", module_name=strategy_name, optional_parameter=input_data.copy())
def __init_filter(filter_list: list) -> list:
"""
Return a list of filters instances using a list of filter names.
If 'all' is specified, all available filters will be returned
:param filter_list: a list of filter names (in strings)
:return: a list of filters
"""
if "all" in filter_list:
filter_list = [Path(file_name).name[:-3] for file_name in glob.glob("./filters/*.py") if
"__init__" not in file_name and "Filters" not in file_name]
return [__dynamic_instantiation(prefix="filters", module_name=filter_name) for filter_name in filter_list]
def init_backtesting(strategy_name: str):
start_date = datetime(2019, 3, 20).date()
end_date = datetime(2021, 3, 23).date()
stock_list = data_engine.YahooFinanceInterface.get_top_30_hsi_constituents()
bt = Backtesting(stock_list=stock_list, start_date=start_date, end_date=end_date, observation=100)
bt.prepare_input_data_file_custom_M(custom_interval=5)
# bt.prepare_input_data_file_1M()
strategy = __dynamic_instantiation(prefix="strategies", module_name=strategy_name,
optional_parameter=bt.get_backtesting_init_data())
bt.init_strategy(strategy)
bt.calculate_return()
# bt.create_tear_sheet()
def init_day_trading(futu_trade: trading_engine.FutuTrade, stock_list: list, strategy_name: str,
stock_strategy_map: dict,
subtype: SubType = SubType.K_5M):
input_data = futu_trade.get_data_realtime(stock_list, sub_type=subtype, kline_num=100)
# strategy_map = dict object {'HK.00001', MACD_Cross(), 'HK.00002', MACD_Cross()...}
strategy_map = {stock_code: __init_strategy(strategy_name=stock_strategy_map.get(stock_code, strategy_name),
input_data=input_data) for stock_code in stock_list}
futu_trade.cur_kline_subscription(input_data, stock_list=stock_list, strategy_map=strategy_map, timeout=3600 * 12,
subtype=subtype)
def init_stock_filter(filter_list: list) -> list:
filters = __init_filter(filter_list)
stock_filter = StockFilter(stock_filters=filters)
return stock_filter.get_filtered_equity_pools()
def main():
# Initialize Argument Parser
parser = argparse.ArgumentParser()
parser.add_argument("-u", "--update", help="Daily Update Data (Execute Before Market Starts)",
action="store_true")
parser.add_argument("-fu", "--force_update",
help="Force Update All Data Up to Max. Allowed Years (USE WITH CAUTION)", action="store_true")
parser.add_argument("-d", "--database", help="Store All CSV Data to Database", action="store_true")
# Retrieve file names for all strategies as the argument option
strategy_list = [Path(file_name).name[:-3] for file_name in glob.glob("./strategies/*.py") if
"__init__" not in file_name and "Strategies" not in file_name]
parser.add_argument("-s", "--strategy", type=str, choices=strategy_list,
help="Execute HFT using Pre-defined Strategy")
parser.add_argument("-b", "--backtesting", type=str, choices=strategy_list,
help="Backtesting a Pre-defined Strategy")
# Retrieve file names for all strategies as the argument option
filter_list = [Path(file_name).name[:-3] for file_name in glob.glob("./filters/*.py") if
"__init__" not in file_name and "Filters" not in file_name]
parser.add_argument("-f", "--filter", type=str, choices=filter_list, nargs="+",
help="Filter Stock List based on Pre-defined Filters")
parser.add_argument("-en", "--email_name", type=str, help="Name of the applied stock filtering techniques")
# Evaluate Arguments
args = parser.parse_args()
# Initialization Connection
futu_trade = trading_engine.FutuTrade()
email_handler = email_engine.Email()
# Initialize Stock List
stock_list = json.loads(config.get('TradePreference', 'StockList'))
if not stock_list:
# stock_list = data_engine.DatabaseInterface(
# database_path=config.get('Database', 'Database_path')).get_stock_list()
# Directly get list of stock codes from the data folder. Easier.
stock_list = [str(f.path).replace('./data/', '') for f in os.scandir("./data/") if f.is_dir()]
stock_list = stock_list[:-1]
if args.filter:
filtered_stock_list = init_stock_filter(args.filter)
filtered_stock_dict = YahooFinanceInterface.get_stocks_email(filtered_stock_list)
subscription_list = json.loads(config.get('Email', 'SubscriptionList'))
for subscriber in subscription_list:
filter_name = args.email_name if args.email_name else "Default Stock Filter"
email_handler.write_daily_stock_filter_email(subscriber, filter_name, filtered_stock_dict)
if args.update or args.force_update:
# Daily Update Data
daily_update_data(futu_trade=futu_trade, stock_list=stock_list, force_update=args.force_update)
if args.database:
# Update ALl Data to Database
futu_trade.store_all_data_database()
if args.strategy:
# Stock Basket => 4 Parts
# 1. Currently Holding Stocks (i.e., in the trading account with existing position)
# 2. Filtered Stocks (i.e., based on 1D data if -f option is adopted
# 3. StockList in config.ini (i.e., if empty, default use all stocks in the database)
# 4. Top 30 HSI Constituents
if args.filter:
stock_list.extend(filtered_stock_list)
# stock_list.extend(data_engine.YahooFinanceInterface.get_top_30_hsi_constituents())
init_day_trading(futu_trade, stock_list, args.strategy, stock_strategy_map)
if args.backtesting:
init_backtesting(args.backtesting)
futu_trade.display_quota()
if __name__ == '__main__':
main()
| python |
def binarySearch(array,l,r,x):
while l <=r:
mid = l + (r-1)//2
if array[mid] == x:
return mid
elif array[mid] > x:
r = mid-1
else:
l = mid +1
return -1
array = [2,4,5,6,7,9,10,23,53]
item = 23
result = binarySearch(array, 0, len(array)-1, item)
if result != -1:
print("number found at index",result)
else:
print("number not found")
| python |
__title__ = "playground"
__author__ = "murlux"
__copyright__ = "Copyright 2019, " + __author__
__credits__ = (__author__, )
__license__ = "MIT"
__email__ = "murlux@protonmail.com"
from logging import Logger
from typing import Dict
from playground.util import setup_logger
class SimulationIntegrator:
"""
Main simulation class, spawns the various engines.
These two classes are responsable for maintaining and providing up to date datasets.
"""
logger: Logger = None
# Critical objects
def __init__(self, config: Dict = None) -> None:
"""Initialize the playground's simulation integrator."""
self.logger = setup_logger(name='{}.{}'.format(__title__, __name__))
self.logger.info("Creating the SimulationIntegrator...")
def run(self) -> None:
"""
Starts the engines.
"""
self.logger.info("Running the SimulationIntegrator...") | python |
# (C) British Crown Copyright 2011 - 2018, Met Office
#
# This file is part of cartopy.
#
# cartopy is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cartopy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with cartopy. If not, see <https://www.gnu.org/licenses/>.
from __future__ import (absolute_import, division, print_function)
import warnings
import numpy as np
from numpy.testing import assert_array_equal
import pytest
import cartopy.crs as ccrs
import cartopy.io.srtm
from .test_downloaders import download_to_temp # noqa: F401 (used as fixture)
pytestmark = [pytest.mark.network,
pytest.mark.filterwarnings('ignore:SRTM requires an account'),
pytest.mark.usefixtures('srtm_login_or_skip')]
@pytest.fixture
def srtm_login_or_skip(monkeypatch):
import os
try:
srtm_username = os.environ['SRTM_USERNAME']
except KeyError:
pytest.skip('SRTM_USERNAME environment variable is unset.')
try:
srtm_password = os.environ['SRTM_PASSWORD']
except KeyError:
pytest.skip('SRTM_PASSWORD environment variable is unset.')
from six.moves.urllib.request import (HTTPBasicAuthHandler,
HTTPCookieProcessor,
HTTPPasswordMgrWithDefaultRealm,
build_opener)
from six.moves.http_cookiejar import CookieJar
password_manager = HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(
None,
"https://urs.earthdata.nasa.gov",
srtm_username,
srtm_password)
cookie_jar = CookieJar()
opener = build_opener(HTTPBasicAuthHandler(password_manager),
HTTPCookieProcessor(cookie_jar))
monkeypatch.setattr(cartopy.io, 'urlopen', opener.open)
class TestRetrieve(object):
@pytest.mark.parametrize('Source, read_SRTM, max_, min_, pt', [
(cartopy.io.srtm.SRTM3Source, cartopy.io.srtm.read_SRTM3,
602, -34, 78),
(cartopy.io.srtm.SRTM1Source, cartopy.io.srtm.read_SRTM1,
602, -37, 50),
], ids=[
'srtm3',
'srtm1',
])
def test_srtm_retrieve(self, Source, read_SRTM, max_, min_, pt,
download_to_temp):
# test that the download mechanism for SRTM works
with warnings.catch_warnings(record=True) as w:
r = Source().srtm_fname(-4, 50)
assert len(w) == 1
assert issubclass(w[0].category, cartopy.io.DownloadWarning)
assert r.startswith(str(download_to_temp)), \
'File not downloaded to tmp dir'
img, _, _ = read_SRTM(r)
# check that the data is fairly sensible
assert img.max() == max_
assert img.min() == min_
assert img[-10, 12] == pt
@pytest.mark.parametrize('Source, shape', [
(cartopy.io.srtm.SRTM3Source, (1201, 1201)),
(cartopy.io.srtm.SRTM1Source, (3601, 3601)),
], ids=[
'srtm3',
'srtm1',
])
def test_srtm_out_of_range(self, Source, shape):
# Somewhere over the pacific the elevation should be 0.
img, _, _ = Source().combined(120, 2, 2, 2)
assert_array_equal(img, np.zeros(np.array(shape) * 2))
@pytest.mark.parametrize('Source', [
cartopy.io.srtm.SRTM3Source,
cartopy.io.srtm.SRTM1Source,
], ids=[
'srtm3',
'srtm1',
])
class TestSRTMSource__single_tile(object):
def test_out_of_range(self, Source):
source = Source()
msg = 'No srtm tile found for those coordinates.'
with pytest.raises(ValueError, message=msg):
source.single_tile(-25, 50)
def test_in_range(self, Source):
if Source == cartopy.io.srtm.SRTM3Source:
shape = (1201, 1201)
elif Source == cartopy.io.srtm.SRTM1Source:
shape = (3601, 3601)
else:
raise ValueError('Source is of unexpected type.')
source = Source()
img, crs, extent = source.single_tile(-1, 50)
assert isinstance(img, np.ndarray)
assert img.shape == shape
assert img.dtype == np.dtype('>i2')
assert crs == ccrs.PlateCarree()
assert extent == (-1, 0, 50, 51)
def test_zeros(self, Source):
source = Source()
_, _, extent = source.single_tile(0, 50)
assert extent == (0, 1, 50, 51)
@pytest.mark.parametrize('Source', [
cartopy.io.srtm.SRTM3Source,
cartopy.io.srtm.SRTM1Source,
], ids=[
'srtm3',
'srtm1',
])
class TestSRTMSource__combined(object):
def test_trivial(self, Source):
source = Source()
e_img, e_crs, e_extent = source.single_tile(-3, 50)
r_img, r_crs, r_extent = source.combined(-3, 50, 1, 1)
assert_array_equal(e_img, r_img)
assert e_crs == r_crs
assert e_extent == r_extent
def test_2by2(self, Source):
source = Source()
e_img, _, e_extent = source.combined(-1, 50, 2, 1)
assert e_extent == (-1, 1, 50, 51)
imgs = [source.single_tile(-1, 50)[0],
source.single_tile(0, 50)[0]]
assert_array_equal(np.hstack(imgs), e_img)
@pytest.mark.parametrize('Source', [
cartopy.io.srtm.SRTM3Source,
cartopy.io.srtm.SRTM1Source,
], ids=[
'srtm3',
'srtm1',
])
def test_fetch_raster_ascombined(Source):
source = Source()
e_img, e_crs, e_extent = source.combined(-1, 50, 2, 1)
imgs = source.fetch_raster(ccrs.PlateCarree(),
(-0.9, 0.1, 50.1, 50.999),
None)
assert len(imgs) == 1
r_img, r_extent = imgs[0]
assert e_extent == r_extent
assert_array_equal(e_img[::-1, :], r_img)
| python |
import gym
from garage.baselines import LinearFeatureBaseline
from garage.theano.baselines import GaussianMLPBaseline
from garage.baselines import ZeroBaseline
from garage.envs import normalize
from garage.envs.box2d import CartpoleEnv
from garage.envs.mujoco import SwimmerEnv
from garage.theano.algos.capg_corrected import CAPG
from garage.theano.envs import TheanoEnv
from garage.theano.policies import GaussianMLPPolicy
from garage.misc.instrument import run_experiment
from garage.misc.ext import set_seed
import numpy as np
for learning_rate in [0.01]:
for batch_size in [1000]:
for n_subitr in [10]:
minibatch_size = batch_size/n_subitr
for i in range(10):
seed = np.random.randint(1,10000)
env_name = "CAPG_CartPole"
hidden_sizes = (8,)
env = TheanoEnv(normalize(CartpoleEnv()))
policy = GaussianMLPPolicy(env_spec=env.spec, hidden_sizes=hidden_sizes)
backup_policy = GaussianMLPPolicy(env.spec, hidden_sizes=hidden_sizes)
mix_policy = GaussianMLPPolicy(env.spec, hidden_sizes=hidden_sizes)
pos_eps_policy = GaussianMLPPolicy(env.spec, hidden_sizes=hidden_sizes)
neg_eps_policy = GaussianMLPPolicy(env.spec, hidden_sizes=hidden_sizes)
baseline = ZeroBaseline(env_spec=env.spec)
algo = CAPG(
env=env,
policy=policy,
backup_policy=backup_policy,
mix_policy=mix_policy,
pos_eps_policy=pos_eps_policy,
neg_eps_policy=neg_eps_policy,
n_timestep=5e5,
learning_rate=learning_rate,
batch_size=batch_size,
minibatch_size=minibatch_size,
n_sub_itr = n_subitr,
center_adv=True,
decay_learing_rate=True,
baseline=baseline,
max_path_length=100,
discount=0.99,
log_dir='./result_0.01/' + env_name + "seed" + str(seed) + '/',
)
algo.train()
| python |
#!/usr/bin/env python3
import os
import sys
import glob
import shutil
import subprocess
from re import search
def remove_dir(dir_path):
try:
if os.path.isdir(dir_path):
shutil.rmtree(dir_path)
except OSError as e:
print("Failed removing {}: {}".format(dir_path, e))
else:
print("\nRemove dir: {}".format(dir_path))
def create_dir(dir_path):
try:
if not os.path.isdir(dir_path):
os.mkdir(dir_path)
except OSError as e:
print("Failed creating {}: {}".format(dir_path, e))
else:
print("Create dir: {}".format(dir_path))
def read_file(file):
try:
if os.path.isfile(file):
with open(file, 'r') as f:
lines = f.readlines()
return lines
except (OSError, IOError) as e:
print("Failed reading {}: {}".format(file, e))
def write_file(file, lines):
try:
if os.path.isfile(file):
with open(file, 'r+') as f:
f.write(lines)
except (OSError, IOError) as e:
print("Failed writing {}: {}".format(file, e))
def compare_lines(lines, needle):
# This only finds the first occurrence.
for line in lines:
if search(needle, line):
return lines.index(line)
def python_os():
if sys.platform == 'win32':
python = 'python'
elif sys.platform == 'linux':
python = 'python3'
elif sys.platform == 'darwin':
python = 'python3'
else:
python = 'python'
return python
def run_command(cmd):
if sys.platform == 'win32':
subprocess.run(cmd, shell=True, check=True) # No user input here.
else:
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # No user input here.
stdout, stderr = proc.communicate()
print('{}'.format(stdout))
print('{}'.format(stderr))
class ArweaveBuild:
"""
This script builds the marketing site of Sarcophagus for the Arweave permaweb.
Afterwards it outputs a command for deploying the site on Arweave.
"""
def __init__(self):
self.baseurl = 'baseurl: "."'
self.config_file = os.getcwd() + '/' + '_config.yml'
self.arweave_dir = os.getcwd() + '/' + '_site_arweave/'
self.config_lines, self.html_files = [], []
self.config_state = '' # commented/uncommented
self.index = False
self.html_lines = ''
self.GREEN, self.ENDC = '\033[92m', '\033[0m'
def create_folder(self):
# A separate build folder for Jekyll files for an Arweave deploy.
remove_dir(self.arweave_dir)
create_dir(self.arweave_dir)
def get_config_lines(self):
self.config_lines = read_file(self.config_file)
def get_config_state(self):
if compare_lines(self.config_lines, '#' + self.baseurl):
self.config_state = 'commented'
elif compare_lines(self.config_lines, self.baseurl):
self.config_state = 'uncommented'
else:
print(
'Could not find {} or {} in your Jekyll config file. Check your \'baseurl\' setting in _config.yml.'.format(
'#' + self.baseurl, self.baseurl))
def commented_state(self):
if self.config_state == 'commented':
return True
def uncommented_state(self):
if self.config_state == 'uncommented':
return True
def get_index(self):
# Get the line number of the baseurl: setting.
if self.commented_state():
self.index = compare_lines(self.config_lines, '#' + self.baseurl)
elif self.uncommented_state():
self.index = compare_lines(self.config_lines, self.baseurl)
else:
print('Could not get the line number of the \'baseurl\' setting in your config file.')
def toggle_config(self):
# We need a dot in front of some links
if self.commented_state():
self.config_lines[self.index] = self.config_lines[self.index][1:]
write_file(self.config_file, "".join(self.config_lines))
print('\nUncommented the baseurl setting in your Jekyll config.\n')
if self.uncommented_state():
self.config_lines[self.index] = '#' + self.config_lines[self.index]
write_file(self.config_file, "".join(self.config_lines))
print('\nCommented the baseurl setting in your Jekyll config.')
def create_build(self):
# Create a fresh build
self.get_config_state()
if self.uncommented_state():
cmd = ['bundle', 'exec', 'jekyll', 'build', '--destination', self.arweave_dir]
run_command(cmd)
elif self.commented_state():
print('NOT READY for a build, config state was {}. Running a new instance of this script.'.format(
self.config_state))
cmd = [python_os(), os.getcwd() + '/arweave_build.py']
run_command(cmd)
exit() # Exit the current instance, we are running a new one now.
def read_files(self):
# Search all 1st lvl html files for unconverted links (e.g. main menu).
os.chdir(self.arweave_dir)
self.html_files = glob.glob('*.html')
def change_lines(self):
# Change lines with ="/ to ="./
for file in self.html_files:
index_list, new_html = [], []
the_file = self.arweave_dir + '/' + file
with open(the_file, 'r+') as f:
self.html_lines = f.readlines()
new_html = self.replace_string(new_html)
write_file(the_file, "".join(new_html))
def replace_string(self, new_html):
for line in self.html_lines:
if search('="/', line):
new_line = line.replace('="/', '="./')
new_html.append(new_line)
else:
new_html.append(line)
return new_html
def deploy_build(self):
# Print help for deploying the build to the Arweave permaweb
print('\n' + self.GREEN + 'DONE. You can now deploy the build to Arweave with the following command:'
+ self.ENDC)
print('\n' + self.GREEN + '$ arweave deploy-dir ' + self.arweave_dir +
' --key-file /<path to your keyfile>/<name of your keyfile>.json \n' + self.ENDC)
def run(self):
self.create_folder()
self.get_config_lines()
self.get_config_state()
self.get_index()
self.toggle_config()
self.create_build()
self.read_files()
self.change_lines()
self.get_config_state()
self.toggle_config()
self.deploy_build()
if __name__ == '__main__':
AD = ArweaveBuild()
AD.run()
| python |
import argparse
__all__ = ('arg_parser')
arg_parser = argparse.ArgumentParser(description='Converts JSON files to HTML files')
arg_parser.add_argument('source', type=str, action='store', help='Source JSON file')
arg_parser.add_argument('--dest', type=str, action='store', help='Output HTML filename', default=None, dest='dest')
| python |
import pickle
import json
import argparse
import string
import os
from zhon import hanzi
from collections import namedtuple
import nltk
def makedir(root):
if not os.path.exists(root):
os.makedirs(root)
def write_json(data, root):
with open(root, 'w') as f:
json.dump(data, f)
ImageMetaData = namedtuple('ImageMetaData', ['id', 'image_path', 'captions', 'split'])
ImageDecodeData = namedtuple('ImageDecodeData', ['id', 'image_path', 'captions_id', 'split'])
class Vocabulary(object):
"""
Vocabulary wrapper
"""
def __init__(self, vocab, unk_id):
"""
:param vocab: A dictionary of word to word_id
:param unk_id: Id of the bad/unknown words
"""
self._vocab = vocab
self._unk_id = unk_id
def word_to_id(self, word):
if word not in self._vocab:
return self._unk_id
return self._vocab[word]
def id_to_word(self, id):
if id not in self._reverse_vocab:
return ''
else:
return self._reverse_vocab[id]
def cap2tokens(cap):
exclude = set(string.punctuation + string.whitespace + hanzi.punctuation)
caption = ''.join(c for c in cap if c not in exclude)
tokens = [letter for letter in caption]
# print(tokens)
tokens = add_start_end(tokens)
return tokens
def add_start_end(tokens, start_word='<START>', end_word='<END>'):
"""
Add start and end words for a caption
"""
tokens_processed = [start_word]
tokens_processed.extend(tokens)
tokens_processed.append(end_word)
return tokens_processed
def process_captions(imgs):
for img in imgs:
img['processed_tokens'] = []
for s in img['captions']:
tokens = cap2tokens(s)
img['processed_tokens'].append(tokens)
def build_vocab(imgs, args, write=True):
print('start build vodabulary')
counts = {}
for img in imgs:
for tokens in img['processed_tokens']:
for word in tokens:
counts[word] = counts.get(word, 0) + 1
print('Total words:', len(counts))
# filter uncommon words and sort by descending count.
# word_counts: a list of (words, count) for words satisfying the condition.
stop_words = []
if args.remove_stopwords is not None:
with open(args.remove_stopwords) as f:
lines = f.readlines()
stop_words = [l.strip() for l in lines]
print('Stop words cnt:{}'.format(len(stop_words)))
word_counts = [(w,n) for w,n in counts.items() if n >= args.min_word_count and w not in stop_words]
word_counts.sort(key = lambda x : x[1], reverse=True)
print('Words in vocab:', len(word_counts))
words_out = [(w,n) for w,n in counts.items() if n < args.min_word_count or w in stop_words]
bad_words = len(words_out)
bad_count = len([x[1] for x in words_out])
# save the word counts file
if write:
word_counts_root = os.path.join(args.out_root + '/word_counts.txt')
with open(word_counts_root, 'w') as f:
f.write('Total words: %d \n' % len(counts))
f.write('Words in vocabulary: %d \n' % len(word_counts))
f.write(str(word_counts))
word_counts_root = os.path.join(args.out_root + '/word_outs.txt')
with open(word_counts_root, 'w') as f:
f.write('Total words: %d \n' % len(counts))
f.write('Words in vocabulary: %d \n' % len(words_out))
f.write(str(words_out))
word_reverse = [w for (w,n) in word_counts]
vocab_dict = dict([(word, index) for (index, word) in enumerate(word_reverse)])
vocab = Vocabulary(vocab_dict, len(vocab_dict))
# Save word index as pickle form
word_to_idx = {}
for index, word in enumerate(word_reverse):
word_to_idx[word] = index
if write:
with open(os.path.join(args.out_root, 'word_to_index.pkl'), 'wb') as f:
pickle.dump(word_to_idx, f)
print('number of bad words: %d/%d = %.2f%%' % (bad_words, len(counts), bad_words * 100.0 / len(counts)))
print('number of words in vocab: %d/%d = %.2f%%' % (len(word_counts), len(counts), len(word_counts) * 100.0 / len(counts)))
print('number of Null: %d/%d = %.2f%%' % (bad_count, len(counts), bad_count * 100.0 / len(counts)))
return vocab
def load_vocab(args):
with open(os.path.join(args.out_root, 'word_to_index.pkl'), 'rb') as f:
word_to_idx = pickle.load(f)
vocab = Vocabulary(word_to_idx, len(word_to_idx))
print('load vocabulary done')
return vocab
def process_metadata(split, data, args, write=True):
"""
Wrap data into ImageMatadata form
"""
id_to_captions = {}
image_metadata = []
num_captions = 0
count = 0
for img in data:
count += 1
# absolute image path
# filepath = os.path.join(args.img_root, img['file_path'])
# relative image path
filepath = img['file_path']
# assert os.path.exists(filepath)
id = img['id'] - 1
captions = img['processed_tokens']
# print(captions)
id_to_captions.setdefault(id, [])
id_to_captions[id].append(captions)
assert split == img['split'], 'error: wrong split'
image_metadata.append(ImageMetaData(id, filepath, captions, split))
num_captions += len(captions)
print("Process metadata done!")
print("Total %d captions %d images %d identities in %s" % (num_captions, count, len(id_to_captions), split))
if write:
with open(os.path.join(args.out_root, 'metadata_info.txt') ,'a') as f:
f.write("Total %d captions %d images %d identities in %s" % (num_captions, count, len(id_to_captions), split))
f.write('\n')
return image_metadata
def process_decodedata(data, vocab):
"""
Decode ImageMetaData to ImageDecodeData
Each item in imagedecodedata has 2 captions. (len(captions_id) = 2)
"""
image_decodedata = []
for img in data:
image_path = img.image_path
cap_to_vec = []
for cap in img.captions:
cap_to_vec.append([vocab.word_to_id(word) for word in cap])
image_decodedata.append(ImageDecodeData(img.id, image_path, cap_to_vec, img.split))
print('Process decodedata done!')
return image_decodedata
def process_dataset(split, decodedata, args, write=True):
# Process dataset
# Arrange by caption in a sorted form
dataset, label_range = create_dataset_sort(split, decodedata)
data = write_dataset(split, dataset, args, write=write)
return data
def create_dataset_sort(split, data):
images_sort = []
label_range = {}
images = {}
for img in data:
label = img.id
image = [ImageDecodeData(img.id, img.image_path, [caption_id], img.split) for caption_id in img.captions_id]
if label in images:
images[label].extend(image)
label_range[label].append(len(image))
else:
images[label] = image
label_range[label] = [len(image)]
print('=========== Arrange by id=============================')
index = -1
for label in images.keys():
# all captions arrange together
images_sort.extend(images[label])
# label_range is arranged according to their actual index
# label_range[label] = (previous, current]
start = index
for index_image in range(len(label_range[label])):
label_range[label][index_image] += index
index = label_range[label][index_image]
label_range[label].append(start)
return images_sort, label_range
def write_dataset(split, data, args, label_range=None, write=True):
"""
Separate each component
Write dataset into binary file
"""
caption_id = []
images_path = []
labels = []
for img in data:
assert len(img.captions_id) == 1
caption_id.append(img.captions_id[0])
labels.append(img.id)
images_path.append(img.image_path)
#N = len(images)
data = {'caption_id': caption_id, 'labels':labels, 'images_path':images_path}
if write:
if label_range is not None:
data['label_range'] = label_range
pickle_root = os.path.join(args.out_root, split + '_sort.pkl')
else:
pickle_root = os.path.join(args.out_root, split + '.pkl')
# Write caption_id and labels as pickle form
with open(pickle_root, 'wb') as f:
pickle.dump(data, f)
print('Save dataset')
return data
def generate_split(args):
with open(args.json_root,'r') as f:
imgs = json.load(f)
# print(imgs)
# process caption
if not args.load_tokens:
print('Spliting tokens at runtime...')
process_captions(imgs)
else:
print('Tokens in json preserved...')
print('DEBUG', [im['processed_tokens'] for im in imgs[:10]])
val_data = []
train_data = []
test_data = []
for img in imgs:
if img['split'] == 'train':
train_data.append(img)
elif img['split'] =='val':
val_data.append(img)
else:
test_data.append(img)
write_json(train_data, os.path.join(args.out_root, 'train_reid.json'))
write_json(val_data, os.path.join(args.out_root, 'val_reid.json'))
write_json(test_data, os.path.join(args.out_root, 'test_reid.json'))
return [train_data, val_data, test_data]
def load_split(args):
data = []
splits = ['train', 'val', 'test']
for split in splits:
split_root = os.path.join(args.out_root, split + '_reid.json')
with open(split_root, 'r') as f:
split_data = json.load(f)
data.append(split_data)
print('load data done')
return data
def process_data(args):
if args.load_split:
train_data, val_data, test_data = load_split(args)
else:
train_data, val_data, test_data = generate_split(args)
if args.load_vocab:
vocab = load_vocab(args)
else:
vocab = build_vocab(train_data, args)
# Transform original data to Imagedata form.
train_metadata = process_metadata('train', train_data, args)
val_metadata = process_metadata('val', val_data, args)
test_metadata = process_metadata('test', test_data, args)
# Decode Imagedata to index caption and replace image file_root with image vecetor.
train_decodedata = process_decodedata(train_metadata, vocab)
val_decodedata = process_decodedata(val_metadata, vocab)
test_decodedata = process_decodedata(test_metadata, vocab)
process_dataset('train', train_decodedata, args)
process_dataset('val', val_decodedata, args)
process_dataset('test', test_decodedata, args)
def parse_args():
parser = argparse.ArgumentParser(description='Command for data preprocessing')
parser.add_argument('--img_root', type=str)
parser.add_argument('--json_root', type=str)
parser.add_argument('--out_root',type=str)
parser.add_argument('--min_word_count', type=int, default=0)
parser.add_argument('--default_image_size', type=int, default=224)
parser.add_argument('--load_split', action='store_true')
parser.add_argument('--load_tokens', action='store_true')
parser.add_argument('--load_vocab', action='store_true')
parser.add_argument('--remove_stopwords', type=str, default=None)
parser.add_argument('--keep_symbol', action='store_true')
args = parser.parse_args()
return args
if __name__ == '__main__':
args = parse_args()
makedir(args.out_root)
process_data(args) | python |
jobname="manuscript"
| python |
from rest_framework.response import Response
from rest_framework.views import status
def validate_request_data_photo(fn):
def decorated(*args, **kwargs):
title = args[0].request.data.get("title", "")
photo = args[0].request.data.get("photo", "")
if not title or not photo:
return Response(
data={
"message": "The request must have the fields 'title' and 'photo' filled."
},
status=status.HTTP_400_BAD_REQUEST
)
return fn(*args, **kwargs)
return decorated | python |
"""Test the houdini_package_runner.discoverers.package module."""
# =============================================================================
# IMPORTS
# =============================================================================
# Standard Library
import argparse
import pathlib
# Third Party
import pytest
# Houdini Package Runner
import houdini_package_runner.discoverers.base
import houdini_package_runner.discoverers.package
import houdini_package_runner.items.digital_asset
import houdini_package_runner.items.filesystem
import houdini_package_runner.items.xml
# =============================================================================
# TESTS
# =============================================================================
class TestPackageItemDiscoverer:
"""Test houdini_package_runner.discoverers.package.PackageItemDiscoverer."""
# Object construction
@pytest.mark.parametrize("has_items", (False, True))
def test___init__(self, mocker, has_items):
"""Test object initialization."""
mock_path = mocker.MagicMock(spec=pathlib.Path)
mock_file_path = mocker.MagicMock(spec=pathlib.Path)
mock_file_path.is_file.return_value = True
mock_file_path.is_dir.return_value = False
mock_file1 = mocker.MagicMock(
spec=houdini_package_runner.items.filesystem.FileToProcess
)
mock_dir = mocker.MagicMock(
spec=houdini_package_runner.items.filesystem.DirectoryToProcess
)
mock_process_dir = mocker.patch(
"houdini_package_runner.discoverers.package.process_directory"
)
mock_process_dir.side_effect = (mock_dir, None)
mock_houdini_item = mocker.MagicMock(
spec=houdini_package_runner.items.filesystem.HoudiniDirectoryItem
)
mock_get_houdini = mocker.patch(
"houdini_package_runner.discoverers.package.get_houdini_items"
)
mock_get_houdini.return_value = [mock_houdini_item] if has_items else []
mock_file_to_process = mocker.patch(
"houdini_package_runner.items.filesystem.FileToProcess"
)
if has_items:
items = [mock_file1]
houdini_items = ["scripts"]
mock_dir1 = mocker.MagicMock(spec=pathlib.Path)
mock_dir1.is_file.return_value = False
mock_dir1.is_dir.return_value = True
mock_dir2 = mocker.MagicMock(spec=pathlib.Path)
mock_dir2.is_file.return_value = False
mock_dir2.is_dir.return_value = True
extra_paths = [mock_file_path, mock_dir1, mock_dir2]
inst = houdini_package_runner.discoverers.package.PackageItemDiscoverer(
mock_path,
houdini_items,
extra_paths=extra_paths,
items=items,
)
assert inst.items == [
mock_file1,
mock_houdini_item,
mock_file_to_process.return_value,
mock_dir,
]
mock_file_to_process.assert_called_with(mock_file_path)
mock_get_houdini.assert_called_with(houdini_items, inst.path)
else:
inst = houdini_package_runner.discoverers.package.PackageItemDiscoverer(
mock_path,
houdini_items=[],
)
assert inst.items == []
def test_get_digital_asset_items(shared_datadir):
"""Test houdini_package_runner.discoverers.package.get_digital_asset_items."""
test_path = shared_datadir / "get_digital_asset_items"
results = houdini_package_runner.discoverers.package.get_digital_asset_items(
test_path
)
assert len(results) == 3
expanded_dir_path = test_path / "expanded_dir"
nodetype_otl_path = test_path / "nodetype.otl"
operator_hda_path = test_path / "operator.hda"
for item in results:
if item.path in (nodetype_otl_path, operator_hda_path):
assert isinstance(
item, houdini_package_runner.items.digital_asset.BinaryDigitalAssetFile
)
elif item.path == expanded_dir_path:
assert isinstance(
item, houdini_package_runner.items.digital_asset.DigitalAssetDirectory
)
def test_get_houdini_items(mocker, shared_datadir):
"""Test houdini_package_runner.discoverers.package.get_houdini_items."""
mock_asset_item = mocker.MagicMock(
spec=houdini_package_runner.items.digital_asset.BinaryDigitalAssetFile
)
mock_get_asset_items = mocker.patch(
"houdini_package_runner.discoverers.package.get_digital_asset_items",
return_value=[mock_asset_item],
)
mock_tool_item = mocker.MagicMock(spec=houdini_package_runner.items.xml.ShelfFile)
mock_get_tool_items = mocker.patch(
"houdini_package_runner.discoverers.package.get_tool_items",
return_value=[mock_tool_item],
)
mock_panel_item = mocker.MagicMock(
spec=houdini_package_runner.items.xml.PythonPanelFile
)
mock_get_panel_items = mocker.patch(
"houdini_package_runner.discoverers.package.get_python_panel_items",
return_value=[mock_panel_item],
)
mock_menu_item = mocker.MagicMock(spec=houdini_package_runner.items.xml.MenuFile)
mock_get_menu_items = mocker.patch(
"houdini_package_runner.discoverers.package.get_menu_items",
return_value=[mock_menu_item],
)
mock_pydir_item = mocker.patch(
"houdini_package_runner.items.filesystem.HoudiniDirectoryItem"
)
mock_dir_item = mocker.MagicMock(
spec=houdini_package_runner.items.filesystem.DirectoryToProcess
)
mock_process = mocker.patch(
"houdini_package_runner.discoverers.package.process_directory",
side_effect=(mock_dir_item, None),
)
test_path = shared_datadir / "get_houdini_items"
item_names = [
"",
"otls",
"hda",
"directory_item",
"empty_directory_item",
"pythonXlibs",
"toolbar",
"python_panels",
"menus",
"some_file",
]
items = houdini_package_runner.discoverers.package.get_houdini_items(
item_names, test_path
)
expected = [
mock_asset_item,
mock_dir_item,
mock_pydir_item.return_value,
mock_tool_item,
mock_panel_item,
mock_menu_item,
]
assert items == expected
mock_get_asset_items.assert_called_with(test_path / "otls")
mock_get_tool_items.assert_called_with(test_path / "toolbar")
mock_get_panel_items.assert_called_with(test_path / "python_panels")
mock_get_menu_items.assert_called_with(test_path)
mock_pydir_item.assert_called_with(
test_path / "python3.7libs", traverse_children=True
)
mock_process.assert_has_calls(
[
mocker.call(test_path / "directory_item"),
mocker.call(test_path / "empty_directory_item"),
]
)
def test_get_menu_items(mocker):
"""Test houdini_package_runner.discoverers.package.get_menu_items."""
mock_menu_file = mocker.patch("houdini_package_runner.items.xml.MenuFile")
mock_menu_path = mocker.MagicMock(spec=pathlib.Path)
mock_houdini_root = mocker.MagicMock(spec=pathlib.Path)
mock_houdini_root.glob.return_value = [mock_menu_path]
result = houdini_package_runner.discoverers.package.get_menu_items(
mock_houdini_root
)
assert result == [mock_menu_file.return_value]
mock_houdini_root.glob.assert_called_with("*.xml")
mock_menu_file.assert_called_with(mock_menu_path)
def test_get_python_panel_items(mocker):
"""Test houdini_package_runner.discoverers.package.get_python_panel_items."""
mock_panel_file = mocker.patch("houdini_package_runner.items.xml.PythonPanelFile")
mock_panel_path = mocker.MagicMock(spec=pathlib.Path)
mock_panel_root = mocker.MagicMock(spec=pathlib.Path)
mock_panel_root.glob.return_value = [mock_panel_path]
result = houdini_package_runner.discoverers.package.get_python_panel_items(
mock_panel_root
)
assert result == [mock_panel_file.return_value]
mock_panel_root.glob.assert_called_with("*.pypanel")
mock_panel_file.assert_called_with(mock_panel_path)
def test_get_tool_items(mocker):
"""Test houdini_package_runner.discoverers.package.get_tool_items."""
mock_shelf_file = mocker.patch("houdini_package_runner.items.xml.ShelfFile")
mock_shelf_path = mocker.MagicMock(spec=pathlib.Path)
mock_toolbar_path = mocker.MagicMock(spec=pathlib.Path)
mock_toolbar_path.glob.return_value = [mock_shelf_path]
result = houdini_package_runner.discoverers.package.get_tool_items(
mock_toolbar_path
)
assert result == [mock_shelf_file.return_value]
mock_toolbar_path.glob.assert_called_with("*.shelf")
mock_shelf_file.assert_called_with(mock_shelf_path)
def test_init_standard_package_discoverer(
mocker,
):
"""Test houdini_package_runner.discoverers.package.init_standard_package_discoverer."""
mock_discoverer = mocker.patch(
"houdini_package_runner.discoverers.package.PackageItemDiscoverer"
)
mock_root = mocker.MagicMock(spec=pathlib.Path)
mock_houdini_root = mocker.MagicMock(spec=pathlib.Path)
mock_extra_paths = mocker.MagicMock(spec=list)
mock_houdini_items = mocker.MagicMock(spec=list)
mock_parse = mocker.patch(
"houdini_package_runner.parser.process_common_arg_settings"
)
mock_parse.return_value = (
mock_root,
mock_houdini_root,
mock_extra_paths,
mock_houdini_items,
)
mock_namespace = mocker.MagicMock(spec=argparse.Namespace)
result = (
houdini_package_runner.discoverers.package.init_standard_package_discoverer(
mock_namespace
)
)
assert result == mock_discoverer.return_value
mock_parse.assert_called_with(mock_namespace)
mock_discoverer.assert_called_with(
mock_houdini_root,
houdini_items=mock_houdini_items,
extra_paths=mock_extra_paths,
)
@pytest.mark.parametrize(
"test_path, expected",
(
(
"package_dir",
houdini_package_runner.items.filesystem.PythonPackageDirectoryItem,
),
("python", houdini_package_runner.items.filesystem.PythonPackageDirectoryItem),
(
"scripts",
houdini_package_runner.items.filesystem.HoudiniScriptsDirectoryItem,
),
("tests", houdini_package_runner.items.filesystem.DirectoryToProcess),
("other", houdini_package_runner.items.filesystem.DirectoryToProcess),
),
)
def test_process_directory(shared_datadir, test_path, expected):
"""Test houdini_package_runner.discoverers.package.process_directory."""
test_dir = shared_datadir / "process_directory" / test_path
result = houdini_package_runner.discoverers.package.process_directory(test_dir)
assert isinstance(result, expected)
# Items which aren't Python packages should have 'traverse_children' set.
if not isinstance(
result, houdini_package_runner.items.filesystem.PythonPackageDirectoryItem
):
assert result.traverse_children
if test_path == "tests":
assert result.is_test_item
| python |
# -*- coding: utf-8 -*-
# Author: Óscar Nájera
# License: 3-clause BSD
r"""
Test Sphinx-Gallery
"""
from __future__ import (division, absolute_import, print_function,
unicode_literals)
import codecs
from contextlib import contextmanager
from io import StringIO
import os
import sys
import re
import shutil
import pytest
from sphinx.application import Sphinx
from sphinx.errors import ExtensionError
from sphinx.util.docutils import docutils_namespace
from sphinx_gallery import sphinx_compatibility
from sphinx_gallery.gen_gallery import (check_duplicate_filenames,
collect_gallery_files)
@pytest.fixture
def conf_file(request):
try:
env = request.node.get_closest_marker('conf_file')
except AttributeError: # old pytest
env = request.node.get_marker('conf_file')
kwargs = env.kwargs if env else {}
result = {
'content': "",
}
result.update(kwargs)
return result
class SphinxAppWrapper(object):
"""Wrapper for sphinx.application.Application.
This allows to control when the sphinx application is initialized, since
part of the sphinx-gallery build is done in
sphinx.application.Application.__init__ and the remainder is done in
sphinx.application.Application.build.
"""
def __init__(self, srcdir, confdir, outdir, doctreedir, buildername,
**kwargs):
self.srcdir = srcdir
self.confdir = confdir
self.outdir = outdir
self.doctreedir = doctreedir
self.buildername = buildername
self.kwargs = kwargs
def create_sphinx_app(self):
# Avoid warnings about re-registration, see:
# https://github.com/sphinx-doc/sphinx/issues/5038
with self.create_sphinx_app_context() as app:
pass
return app
@contextmanager
def create_sphinx_app_context(self):
with docutils_namespace():
app = Sphinx(self.srcdir, self.confdir, self.outdir,
self.doctreedir, self.buildername, **self.kwargs)
sphinx_compatibility._app = app
yield app
def build_sphinx_app(self, *args, **kwargs):
with self.create_sphinx_app_context() as app:
# building should be done in the same docutils_namespace context
app.build(*args, **kwargs)
return app
@pytest.fixture
def sphinx_app_wrapper(tmpdir, conf_file):
_fixturedir = os.path.join(os.path.dirname(__file__), 'testconfs')
srcdir = os.path.join(str(tmpdir), "config_test")
shutil.copytree(_fixturedir, srcdir)
shutil.copytree(os.path.join(_fixturedir, "src"),
os.path.join(str(tmpdir), "examples"))
base_config = """
import os
import sphinx_gallery
extensions = ['sphinx_gallery.gen_gallery']
exclude_patterns = ['_build']
source_suffix = '.rst'
master_doc = 'index'
# General information about the project.
project = u'Sphinx-Gallery <Tests>'\n\n
"""
with open(os.path.join(srcdir, "conf.py"), "w") as conffile:
conffile.write(base_config + conf_file['content'])
return SphinxAppWrapper(
srcdir, srcdir, os.path.join(srcdir, "_build"),
os.path.join(srcdir, "_build", "toctree"), "html", warning=StringIO())
def test_default_config(sphinx_app_wrapper):
"""Test the default Sphinx-Gallery configuration is loaded
if only the extension is added to Sphinx"""
sphinx_app = sphinx_app_wrapper.create_sphinx_app()
cfg = sphinx_app.config
assert cfg.project == "Sphinx-Gallery <Tests>"
# no duplicate values allowed The config is present already
with pytest.raises(ExtensionError) as excinfo:
sphinx_app.add_config_value('sphinx_gallery_conf', 'x', True)
assert 'already present' in str(excinfo.value)
@pytest.mark.conf_file(content="""
sphinx_gallery_conf = {
'examples_dirs': 'src',
'gallery_dirs': 'ex',
}""")
def test_no_warning_simple_config(sphinx_app_wrapper):
"""Testing that no warning is issued with a simple config.
The simple config only specifies input (examples_dirs) and output
(gallery_dirs) directories.
"""
sphinx_app = sphinx_app_wrapper.create_sphinx_app()
cfg = sphinx_app.config
assert cfg.project == "Sphinx-Gallery <Tests>"
build_warn = sphinx_app._warning.getvalue()
assert build_warn == ''
@pytest.mark.conf_file(content="""
sphinx_gallery_conf = {
'mod_example_dir' : os.path.join('modules', 'gen'),
'examples_dirs': 'src',
'gallery_dirs': 'ex',
}""")
def test_config_old_backreferences_conf(sphinx_app_wrapper):
"""Testing Deprecation warning message against old backreference config
In this case the user is required to update the mod_example_dir config
variable Sphinx-Gallery should notify the user and also silently update
the old config to the new one. """
sphinx_app = sphinx_app_wrapper.create_sphinx_app()
cfg = sphinx_app.config
assert cfg.project == "Sphinx-Gallery <Tests>"
assert cfg.sphinx_gallery_conf['backreferences_dir'] == os.path.join(
'modules', 'gen')
build_warn = sphinx_app._warning.getvalue()
assert "WARNING:" in build_warn
assert "deprecated" in build_warn
assert "Support for 'mod_example_dir' will be removed" in build_warn
@pytest.mark.conf_file(content="""
sphinx_gallery_conf = {
'backreferences_dir': os.path.join('gen_modules', 'backreferences'),
'examples_dirs': 'src',
'gallery_dirs': 'ex',
}""")
def test_config_backreferences(sphinx_app_wrapper):
"""Test no warning is issued under the new configuration"""
sphinx_app = sphinx_app_wrapper.create_sphinx_app()
cfg = sphinx_app.config
assert cfg.project == "Sphinx-Gallery <Tests>"
assert cfg.sphinx_gallery_conf['backreferences_dir'] == os.path.join(
'gen_modules', 'backreferences')
build_warn = sphinx_app._warning.getvalue()
assert build_warn == ''
def test_duplicate_files_warn(sphinx_app_wrapper):
"""Test for a warning when two files with the same filename exist."""
sphinx_app = sphinx_app_wrapper.create_sphinx_app()
files = ['./a/file1.py', './a/file2.py', 'a/file3.py', './b/file1.py']
msg = ("Duplicate file name(s) found. Having duplicate file names "
"will break some links. List of files: {}")
m = "['./b/file1.py']" if sys.version_info[0] >= 3 else "[u'./b/file1.py']"
# No warning because no overlapping names
check_duplicate_filenames(files[:-1])
build_warn = sphinx_app._warning.getvalue()
assert build_warn == ''
# Warning because last file is named the same
check_duplicate_filenames(files)
build_warn = sphinx_app._warning.getvalue()
assert msg.format(m) in build_warn
def _check_order(sphinx_app, key):
index_fname = os.path.join(sphinx_app.outdir, '..', 'ex', 'index.rst')
order = list()
regex = '.*:%s=(.):.*' % key
with codecs.open(index_fname, 'r', 'utf-8') as fid:
for line in fid:
if 'sphx-glr-thumbcontainer' in line:
order.append(int(re.match(regex, line).group(1)))
assert len(order) == 3
assert order == [1, 2, 3]
@pytest.mark.conf_file(content="""
sphinx_gallery_conf = {
'examples_dirs': 'src',
'gallery_dirs': 'ex',
}""")
def test_example_sorting_default(sphinx_app_wrapper):
"""Test sorting of examples by default key (number of code lines)."""
sphinx_app = sphinx_app_wrapper.create_sphinx_app()
_check_order(sphinx_app, 'lines')
@pytest.mark.conf_file(content="""
from sphinx_gallery.sorting import FileSizeSortKey
sphinx_gallery_conf = {
'examples_dirs': 'src',
'gallery_dirs': 'ex',
'within_subsection_order': FileSizeSortKey,
}""")
def test_example_sorting_filesize(sphinx_app_wrapper):
"""Test sorting of examples by filesize."""
sphinx_app = sphinx_app_wrapper.create_sphinx_app()
_check_order(sphinx_app, 'filesize')
@pytest.mark.conf_file(content="""
from sphinx_gallery.sorting import FileNameSortKey
sphinx_gallery_conf = {
'examples_dirs': 'src',
'gallery_dirs': 'ex',
'within_subsection_order': FileNameSortKey,
}""")
def test_example_sorting_filename(sphinx_app_wrapper):
"""Test sorting of examples by filename."""
sphinx_app = sphinx_app_wrapper.create_sphinx_app()
_check_order(sphinx_app, 'filename')
@pytest.mark.conf_file(content="""
from sphinx_gallery.sorting import ExampleTitleSortKey
sphinx_gallery_conf = {
'examples_dirs': 'src',
'gallery_dirs': 'ex',
'within_subsection_order': ExampleTitleSortKey,
}""")
def test_example_sorting_title(sphinx_app_wrapper):
"""Test sorting of examples by title."""
sphinx_app = sphinx_app_wrapper.create_sphinx_app()
_check_order(sphinx_app, 'title')
def test_collect_gallery_files(tmpdir):
"""Test that example files are collected properly."""
rel_filepaths = ['examples/file1.py',
'examples/test.rst',
'examples/README.txt',
'examples/folder1/file1.py',
'examples/folder1/file2.py',
'examples/folder2/file1.py',
'tutorials/folder1/subfolder/file1.py',
'tutorials/folder2/subfolder/subsubfolder/file1.py']
abs_paths = [tmpdir.join(rp) for rp in rel_filepaths]
for ap in abs_paths:
ap.ensure()
examples_path = tmpdir.join('examples')
dirs = [examples_path.strpath]
collected_files = set(collect_gallery_files(dirs))
expected_files = set(
[ap.strpath for ap in abs_paths
if re.search(r'examples.*\.py$', ap.strpath)])
assert collected_files == expected_files
tutorials_path = tmpdir.join('tutorials')
dirs = [examples_path.strpath, tutorials_path.strpath]
collected_files = set(collect_gallery_files(dirs))
expected_files = set(
[ap.strpath for ap in abs_paths if re.search(r'.*\.py$', ap.strpath)])
assert collected_files == expected_files
@pytest.mark.conf_file(content="""
sphinx_gallery_conf = {
'backreferences_dir' : os.path.join('modules', 'gen'),
'examples_dirs': 'src',
'gallery_dirs': ['ex'],
'binder': {'binderhub_url': 'http://test1.com', 'org': 'org',
'repo': 'repo', 'branch': 'branch',
'notebooks_dir': 'ntbk_folder',
'dependencies': 'requirements.txt'}
}""")
def test_binder_copy_files(sphinx_app_wrapper, tmpdir):
"""Test that notebooks are copied properly."""
from sphinx_gallery.binder import copy_binder_files
sphinx_app = sphinx_app_wrapper.create_sphinx_app()
gallery_conf = sphinx_app.config.sphinx_gallery_conf
# Create requirements file
with open(os.path.join(sphinx_app.srcdir, 'requirements.txt'), 'w'):
pass
copy_binder_files(sphinx_app, None)
for i_file in ['plot_1', 'plot_2', 'plot_3']:
assert os.path.exists(os.path.join(
sphinx_app.outdir, 'ntbk_folder', gallery_conf['gallery_dirs'][0],
i_file + '.ipynb'))
@pytest.mark.conf_file(content="""
sphinx_gallery_conf = {
'examples_dirs': 'src',
'gallery_dirs': 'ex',
}""")
def test_failing_examples_raise_exception(sphinx_app_wrapper):
example_dir = os.path.join(sphinx_app_wrapper.srcdir,
'src')
with codecs.open(os.path.join(example_dir, 'plot_3.py'), 'a',
encoding='utf-8') as fid:
fid.write('raise SyntaxError')
with pytest.raises(ValueError) as excinfo:
sphinx_app_wrapper.build_sphinx_app()
assert "Unexpected failing examples" in str(excinfo.value)
@pytest.mark.conf_file(content="""
sphinx_gallery_conf = {
'examples_dirs': 'src',
'gallery_dirs': 'ex',
'filename_pattern': 'plot_1.py',
}""")
def test_expected_failing_examples_were_executed(sphinx_app_wrapper):
"""Testing that no exception is issued when broken example is not built
See #335 for more details.
"""
sphinx_app_wrapper.build_sphinx_app()
@pytest.mark.conf_file(content="""
sphinx_gallery_conf = {
'examples_dirs': 'src',
'gallery_dirs': 'ex',
'expected_failing_examples' :['src/plot_2.py'],
}""")
def test_examples_not_expected_to_pass(sphinx_app_wrapper):
with pytest.raises(ValueError) as excinfo:
sphinx_app_wrapper.build_sphinx_app()
assert "expected to fail, but not failing" in str(excinfo.value)
@pytest.mark.conf_file(content="""
sphinx_gallery_conf = {
'first_notebook_cell': 2,
}""")
def test_first_notebook_cell_config(sphinx_app_wrapper):
from sphinx_gallery.gen_gallery import parse_config
# First cell must be str
with pytest.raises(ValueError):
parse_config(sphinx_app_wrapper.create_sphinx_app())
| python |
from .ish_report_test import ish_report_test
from .ish_parser_test import ish_parser_test
from .ComponentTest import SnowDepthComponentTest, SkyCoverComponentTest, SolarIrradianceComponentTest
from .ComponentTest import SkyConditionObservationComponentTest, SkyCoverSummationComponentTest
from .Humidity_test import Humidity_test
from .remarks_test import remarks_test
from .Minutes_test import Minutes_test
| python |
from skidl import SKIDL, TEMPLATE, Part, Pin, SchLib
SKIDL_lib_version = '0.0.1'
RFSolutions = SchLib(tool=SKIDL).add_parts(*[
Part(name='ZETA-433-SO',dest=TEMPLATE,tool=SKIDL,keywords='RF TRANSCEIVER MODULE',description='FM ZETA TRANSCEIVER MODULE, OPTIMISED FOR 433MHZ',ref_prefix='U',num_units=1,do_erc=True,aliases=['ZETA-868-SO', 'ZETA-915-SO'],pins=[
Pin(num='1',name='ANT',func=Pin.BIDIR,do_erc=True),
Pin(num='2',name='GND',func=Pin.PWRIN,do_erc=True),
Pin(num='3',name='SDN',do_erc=True),
Pin(num='4',name='VCC',func=Pin.PWRIN,do_erc=True),
Pin(num='5',name='IRQ',func=Pin.OUTPUT,do_erc=True),
Pin(num='6',name='NC',func=Pin.NOCONNECT,do_erc=True),
Pin(num='7',name='GPIO1',func=Pin.BIDIR,do_erc=True),
Pin(num='8',name='GPIO2',func=Pin.BIDIR,do_erc=True),
Pin(num='9',name='SCLK',do_erc=True),
Pin(num='10',name='SDI',do_erc=True),
Pin(num='11',name='SDO',do_erc=True),
Pin(num='12',name='SEL',do_erc=True)])])
| python |
#!/usr/bin/env python3
from serial import Serial
import bitarray
import time
ser = Serial('/dev/ttyUSB0', 115200)
for i in range(1,100):
for a in range(0,16):
ser.write(b'\xcc')
ser.write((1<<a).to_bytes(2, byteorder='big'))
#ser.write(b.to_bytes(1, byteorder='big'))
ser.write(b'\xff')
print("Count: {} ".format(a))
time.sleep(0.5)
for a in range(0,16):
ser.write(b'\xcc')
ser.write(((2**15)>>a).to_bytes(2, byteorder='big'))
#ser.write(b.to_bytes(1, byteorder='big'))
ser.write(b'\xff')
print("Count: {} ".format(a))
time.sleep(0.5)
for a in range(0,256):
for b in range(0,256):
ser.write(b'\xcc')
ser.write(a.to_bytes(1, byteorder='big'))
ser.write(b.to_bytes(1, byteorder='big'))
ser.write(b'\xff')
print("Count: {} - {}".format(a,b))
time.sleep(0.5)
ser.close()
| python |
from aws_cdk import core, aws_events as events, aws_events_targets as targets
from multacdkrecipies.common import base_alarm, base_lambda_function
from multacdkrecipies.recipies.utils import CLOUDWATCH_CONFIG_SCHEMA, validate_configuration
class AwsCloudwatchLambdaPipes(core.Construct):
"""
AWS CDK Construct that defines a pipe where a message is sent by a Cloudwatch Rule and a Lambda function or functions
will process it and take proper actions. The construct allows to set alerts on the Lambda Functions.
"""
def __init__(self, scope: core.Construct, id: str, *, prefix: str, environment: str, configuration, **kwargs):
"""
:param scope: Stack class, used by CDK.
:param id: ID of the construct, used by CDK.
:param prefix: Prefix of the construct, used for naming purposes.
:param environment: Environment of the construct, used for naming purposes.
:param configuration: Configuration of the construct. In this case SNS_CONFIG_SCHEMA.
:param kwargs: Other parameters that could be used by the construct.
"""
super().__init__(scope, id, **kwargs)
self.prefix = prefix
self.environment_ = environment
self._configuration = configuration
# Validating that the payload passed is correct
validate_configuration(configuration_schema=CLOUDWATCH_CONFIG_SCHEMA, configuration_received=self._configuration)
rule_configuration = self._configuration["cloudwatch_rule"]
rule_name = self.prefix + "_" + rule_configuration["rule_name"] + "_" + self.environment_
schedule = events.Schedule.expression(f"cron({rule_configuration['schedule']})")
self._cloudwatch_event = events.Rule(
self,
id=rule_name,
rule_name=rule_name,
description=rule_configuration.get("description"),
enabled=rule_configuration["enabled"],
schedule=schedule,
)
self._lambda_functions = list()
for function_definition in self._configuration["lambda_handlers"]:
function_ = base_lambda_function(self, **function_definition)
self._cloudwatch_event.add_target(targets.LambdaFunction(handler=function_))
self._lambda_functions.append(function_)
def set_alarms(self):
"""
Function that set alarms for the resources involved in the construct. Except the Cloudwatch Event.
:return: None
"""
for lambda_function_data, lambda_function_definition in zip(
self._configuration["lambda_handlers"], self._lambda_functions
):
if isinstance(lambda_function_data.get("alarms"), list) is True:
lambda_alarms = list()
for alarm_definition in lambda_function_data.get("alarms"):
lambda_alarms.append(
base_alarm(
self,
resource_name=lambda_function_data.get("lambda_name"),
base_resource=lambda_function_definition,
**alarm_definition,
)
)
@property
def configuration(self):
"""
:return: Construct configuration.
"""
return self._configuration
@property
def lambda_functions(self):
"""
:return: Construct Lambda Function.
"""
return self._lambda_function
@property
def cloudwatch_event(self):
"""
:return: Construct IoT Rule.
"""
return self._cloudwatch_event
| python |
from serial import *
from tkinter import *
import tkinter.ttk as ttk
import serial
import serial.tools.list_ports
import threading # for parallel computing
class myThread(threading.Thread):
def __init__(self, name,ser):
threading.Thread.__init__(self)
self.name = name
self.ser = ser
self.stopevent = threading.Event()
self.paused = False
def run(self):
while self.ser.isOpen():
if not self.paused:
received_text.insert(END,self.ser.readline())
received_text.see(END)
if self.stopevent.isSet():
break
def pause(self):
self.paused = True
def resume(self):
self.paused = False
def disconnect(self):
self.stopevent.set()
def serial_ports():
return serial.tools.list_ports.comports()
def on_select(event=None):
# get selection from event
print("event.widget:", event.widget.get())
# or get selection directly from combobox
print("comboboxes: ", cb.get())
def serial_open_cmd():
try:
global ser
ser = serial.Serial(serial_port,ser_baudrate, timeout=1)
global thread1
thread1 = myThread("Updating", ser)
thread1.start()
print(serial_port, "is connected")
# open port if not already open
if ser.isOpen() == False:
ser.open()
elif ser.isOpen() == True:
b1.configure(text = "Connected")
except serial.SerialException:
print ("error open serial port: " + ser.port )
def serial_close_cmd():
if ser.isOpen() == True:
thread1.disconnect()
ser.close()
print("Disconnected")
b1.configure(text = "Connect")
def mSend(command):
# try:
thread1.pause()
ser.write(command.encode('ascii'))
thread1.resume()
# except:
# print ("Could not send command. Port closed?")
return
def config_cmd():
mSend("C")
def fwd_cmd(event):
try:
mSend('F')
except:
pass
def rvs_cmd(event):
try:
mSend('R')
except:
pass
def set_cmd():
mSend('S')
def rst_cmd():
mSend('N')
def count_cmd():
mSend('A')
def change_vel(event):
try:
vel = w1.get()
print(vel)
if (vel==20):
mSend('Q')
if (vel==25):
mSend('W')
if (vel==30):
mSend('E')
if (vel==35):
mSend('T')
if (vel==40):
mSend('Y')
if (vel==45):
mSend('D')
if (vel==50):
mSend('G')
if (vel==60):
mSend('J')
if (vel==70):
mSend('L')
if (vel==80):
mSend('V')
if (vel==90):
mSend('B')
if (vel==100):
mSend('O')
except:
pass
def releasing(event):
try:
mSend('M')
except:
pass
if len(serial.tools.list_ports.comports()) != 0:
COM = serial.tools.list_ports.comports()
serial_port = COM[0][0]
ser_baudrate = 9600
root = Tk()
root.resizable(False,False)
root.wm_title("MERİÇ Serial Communication For DC Motor Driver")
cb = ttk.Combobox(root, values=serial_ports())
cb.grid(row = 1, column = 0,padx=10,pady=10)
# assign function to combobox
cb.bind('<<ComboboxSelected>>', on_select)
l1=Label(root,text="Serial Port Selection",height=2,width=20)
l1.grid(row=0,column=0,columnspan=2)
l2=Label(root,text="Sent",height=2,width=20)
l2.grid(row=0,column=2,columnspan=4,padx=10,pady=1)
l3=Label(root,text="Received",height=2,width=20)
l3.grid(row=2,column=2,columnspan=4,padx=10,pady=1)
received_text = Text (root, takefocus=0)
received_text.grid(row = 3,rowspan = 6,column = 2,columnspan = 4,padx=10,pady=10)
# received_text.bind("<Return>", readSerial)
b1=Button(root, text="Connect", width=12,command=serial_open_cmd)
b1.grid(row=2,column=0,padx=10,pady=10)
b_disconnect=Button(root, text="Disconnect", width=12,command=serial_close_cmd)
b_disconnect.grid(row=3,column=0,padx=10,pady=10)
b2=Button(root, text="Config", width=12,command=config_cmd)
b2.grid(row=1,column=2,padx=10,pady=10)
b3=Button(root, text="Forward", width=12)
b3.grid(row=1,column=3,padx=10,pady=10)
b3.bind("<ButtonPress-1>",fwd_cmd)
b3.bind("<ButtonRelease-1>",releasing)
b4=Button(root, text="Reverse", width=12)
b4.grid(row=1,column=4,padx=10,pady=10)
b4.bind("<ButtonPress-1>",rvs_cmd)
b4.bind("<ButtonRelease-1>",releasing)
b5=Button(root, text="SET", width=12,command=set_cmd)
b5.grid(row=1,column=5,padx=10,pady=10)
b6=Button(root, text="RESET", width=12,command=rst_cmd)
b6.grid(row=1,column=6,padx=10,pady=10)
b7=Button(root, text="ENCODER", width=12,command=count_cmd)
b7.grid(row=2,column=6,padx=10,pady=10)
global vel
w1 = Scale(root, from_=20, to=100, resolution = 5,command=change_vel)
vel=20
w1.set(vel)
w1.grid(row = 3, column= 6,padx=10,pady=10)
time.sleep(1)
root.mainloop()
| python |
"""Unit tests for nautobot_ssot_ipfabric plugin."""
| python |
import torch
import torch.nn as nn
import torch.nn.functional as F
class Cnn1d(nn.Module):
def __init__(self, *, nx, nt, cnnSize=32, cp1=(64, 3, 2), cp2=(128, 5, 2)):
super(Cnn1d, self).__init__()
self.nx = nx
self.nt = nt
cOut, f, p = cp1
self.conv1 = nn.Conv1d(nx, cOut, f)
self.pool1 = nn.MaxPool1d(p)
lTmp = int(calConvSize(nt, f, 0, 1, 1) / p)
cIn = cOut
cOut, f, p = cp2
self.conv2 = nn.Conv1d(cIn, cOut, f)
self.pool2 = nn.MaxPool1d(p)
lTmp = int(calConvSize(lTmp, f, 0, 1, 1) / p)
self.flatLength = int(cOut * lTmp)
self.fc1 = nn.Linear(self.flatLength, cnnSize)
self.fc2 = nn.Linear(cnnSize, cnnSize)
def forward(self, x):
# x- [nt,ngrid,nx]
x1 = x
x1 = x1.permute(1, 2, 0)
x1 = self.pool1(F.relu(self.conv1(x1)))
x1 = self.pool2(F.relu(self.conv2(x1)))
x1 = x1.view(-1, self.flatLength)
x1 = F.relu(self.fc1(x1))
x1 = self.fc2(x1)
return x1
class CNN1dkernel(torch.nn.Module):
def __init__(self,
*,
ninchannel=1,
nkernel=3,
kernelSize=3,
stride=1,
padding=0):
super(CNN1dkernel, self).__init__()
self.cnn1d = torch.nn.Conv1d(
in_channels=ninchannel,
out_channels=nkernel,
kernel_size=kernelSize,
padding=padding,
stride=stride,
)
def forward(self, x):
output = F.relu(self.cnn1d(x))
# output = self.cnn1d(x)
return output
class LstmCnn1d(torch.nn.Module):
# Dense layer > reduce dim > dense
def __init__(self, *, nx, ny, rho, nkernel=(10,5), kernelSize=(3,3), stride=(2,1), padding=(1,1),
dr=0.5, poolOpt=None):
# two convolutional layer
super(LstmCnn1d, self).__init__()
self.nx = nx
self.ny = ny
self.rho = rho
nlayer = len(nkernel)
self.features = nn.Sequential()
ninchan = nx
Lout = rho
for ii in range(nlayer):
# First layer: no dimension reduction
ConvLayer = CNN1dkernel(
ninchannel=ninchan, nkernel=nkernel[ii], kernelSize=kernelSize[ii],
stride=stride[ii], padding=padding[ii])
self.features.add_module('CnnLayer%d' % (ii + 1), ConvLayer)
ninchan = nkernel[ii]
Lout = calConvSize(lin=Lout, kernel=kernelSize[ii], stride=stride[ii])
if poolOpt is not None:
self.features.add_module('Pooling%d' % (ii + 1), nn.MaxPool1d(poolOpt[ii]))
Lout = calPoolSize(lin=Lout, kernel=poolOpt[ii])
self.Ncnnout = int(Lout*nkernel[-1]) # total CNN feature number after convolution
def forward(self, x, doDropMC=False):
out = self.features(x)
# # z0 = (ntime*ngrid) * nkernel * sizeafterconv
# z0 = z0.view(nt, ngrid, self.Ncnnout)
# x0 = torch.cat((x, z0), dim=2)
# x0 = F.relu(self.linearIn(x0))
# outLSTM, (hn, cn) = self.lstm(x0, doDropMC=doDropMC)
# out = self.linearOut(outLSTM)
# # out = rho/time * batchsize * Ntargetvar
return out
def calConvSize(lin, kernel, stride, padding=0, dilation=1):
lout = (lin + 2 * padding - dilation * (kernel - 1) - 1) / stride + 1
return int(lout)
def calPoolSize(lin, kernel, stride=None, padding=0, dilation=1):
if stride is None:
stride = kernel
lout = (lin + 2 * padding - dilation * (kernel - 1) - 1) / stride + 1
return int(lout)
def calFinalsize1d(nobs, noutk, ksize, stride, pool):
nlayer = len(ksize)
Lout = nobs
for ii in range(nlayer):
Lout = calConvSize(lin=Lout, kernel=ksize[ii], stride=stride[ii])
if pool is not None:
Lout = calPoolSize(lin=Lout, kernel=pool[ii])
Ncnnout = int(Lout * noutk) # total CNN feature number after convolution
return Ncnnout | python |
import sys
try:
from sp.base import Logging
except Exception as e:
print "couldn't load splib"
sys.exit(1)
| python |
import configparser
import os
basedir = os.path.abspath(os.path.dirname(__file__))
config = configparser.ConfigParser()
config.read("txdispatch.conf")
SECRET_KEY = config.get("app", "secret_key")
VERSION = config.get("app", "version")
SERVICES = {
"http": {},
"sockets": {},
"websockets": {}
}
for service, port in config.items("services"):
SERVICES["http"][service] = int(port)
SERVICES["sockets"][service] = int(port) + 10
SERVICES["websockets"][service] = int(port) + 20
| python |
import re
import json
import requests
from Bio import SeqIO
from Bio.Seq import Seq
from pathlib import Path
from tqdm.notebook import trange
from Bio.SeqRecord import SeqRecord
from function.utilities import fasta_to_seqlist
from function.utilities import find_human_sequence
def uniprot_id_consistance_check(fasta_path,uniprot_id):
# some uniprot id in OMA paralogs is not consist with uniprot
uniprot_id_oma_fassta = find_human_sequence(fasta_path)["uniprot_id"]
if uniprot_id != uniprot_id_oma_fassta:
fasta_path.unlink()
raise Exception("{} in uniprot is not consist with OMA's record, delete this record".format(uniprot_id))
class FetchOmaSeqBatch():
'''
faster way to get homologous from OMA:
1. get OMA raw fasta from https://omabrowser.org/oma/omagroup/Q13148/fasta/
2. change sequence name to former format, infos are from https://omabrowser.org/api/group/Q13148/
'''
def __init__(self):
pass
def get_oma_seq(self, uniprot_id, path):
'''
pipeline: get fasta from OMA, and change sequence info to former format
'''
oma_path = Path(path)
oma_fasta_path = oma_path / "{}.fasta".format(uniprot_id)
# get raw fasta
self.__get_oma_fasta(uniprot_id, oma_fasta_path)
# get fasta info
fasta_info_dict = self.__get_fasta_info(uniprot_id)
# get mod info fasta
self.__mod_fasta_info(oma_fasta_path, oma_fasta_path, fasta_info_dict)
# uniprot id consistance check
uniprot_id_consistance_check(oma_fasta_path, uniprot_id)
def __get_oma_fasta(self, uniprot_id, fasta_path):
'''
get raw fasta from OMA
'''
try:
url = "https://omabrowser.org/oma/omagroup/{}/fasta/".format(uniprot_id)
resp = requests.get(url)
resp.raise_for_status()
with open(fasta_path, "w") as file:
file.write(resp.text)
except:
raise Exception("{} get fasta failed from OMA".format(uniprot_id))
def __get_fasta_info(self, uniprot_id):
'''
get sequence infos from OMA
'''
try:
url = "https://omabrowser.org/api/group/{}/".format(uniprot_id)
resp = requests.get(url)
resp.raise_for_status()
oma_raw = json.loads(resp.text)
fasta_info_dict = {}
for i in oma_raw['members']:
species = i["species"]["species"]
species = re.sub("\(.*\)", "", species) #sometimes species name are too long, remove some strain info
oma_id = i["omaid"]
canonical_id = i["canonicalid"]
taxon_id = i["species"]["taxon_id"]
fasta_info_dict[oma_id] = {
"oma_id": oma_id,
"species": species,
"canonical_id": canonical_id,
"taxon_id": taxon_id,
}
return fasta_info_dict
except:
raise Exception("{} OMA fetch fasta seqeuence info failed".format(uniprot_id))
def __mod_fasta_info(self, oma_fasta_path, mod_fasta_path, fasta_info_dict):
'''
change sequence name to former format
'''
fasta_list = list(SeqIO.parse(str(oma_fasta_path), 'fasta'))
mod_fasta_list = []
for seq_record in fasta_list:
id = seq_record.id
record = SeqRecord(seq=seq_record.seq,
id=id,
description="| {} | {} | {}".format(fasta_info_dict[id]["species"],
fasta_info_dict[id]["taxon_id"],
fasta_info_dict[id]["canonical_id"])
)
mod_fasta_list.append(record)
SeqIO.write(mod_fasta_list, mod_fasta_path, "fasta")
class FetchOmaSeq():
"""
Deprecated, this is slower than FetchOmaSeqBatch()
get paralogs by uniprot id from OMA,
https://omabrowser.org/oma/home/
"""
def __init__(self):
pass
def get_oma_seq(self, uniprot_id, path):
"""
get paralogs from OMA by uniprot id
uniprot_id: str, uniprot id
path: str, path to save fasta file
return: None
"""
path = Path(path)
fasta_path = path / "{}.fasta".format(uniprot_id)
#get orthologs
orthologs_list = self.__get_orthologs(uniprot_id)
#writing to fasta
self.__get_fasta(orthologs_list, fasta_path)
uniprot_id_consistance_check(fasta_path, uniprot_id)
def __get_protein_info_from_entry(self, ortholog_entry):
try:
resp = requests.get("https://omabrowser.org/api/protein/{}/".format(ortholog_entry))
oma_raw = json.loads(resp.text)
species = oma_raw["species"]["species"]
species = re.sub("\(.*\)", "", species) #sometimes species name are too long, remove some strain info
oma_id = oma_raw["omaid"]
canonical_id = oma_raw["canonicalid"]
taxon_id = oma_raw["species"]["taxon_id"]
sequence = oma_raw["sequence"]
return {
"species": species,
"oma_id": oma_id,
"canonical_id": canonical_id,
"taxon_id": taxon_id,
"sequence": sequence,
}
except:
raise Exception("get single ortholog entry {} from OMA failed".format(ortholog_entry))
def __get_orthologs(self, uniprot_id):
try:
resp = requests.get("https://omabrowser.org/api/group/{}/".format(uniprot_id))
oma_raw = json.loads(resp.text)
orthologs_list = []
t = trange(len(oma_raw["members"]), desc=uniprot_id, leave=True, position=2)
for i in t:
orthologs_list.append(self.__get_protein_info_from_entry(oma_raw["members"][i]["entry_nr"]))
return orthologs_list
except:
raise Exception("get ortholog {} from OMA failed".format(uniprot_id))
def __get_fasta(self, orthologs_list, path):
fasta_list = []
for i in orthologs_list:
record = SeqRecord(
Seq(i["sequence"]),
id=i["oma_id"],
description="| {} | {} | {}".format(i["species"], i["taxon_id"], i["canonical_id"]))
fasta_list.append(record)
SeqIO.write(fasta_list, path, "fasta")
class TaxSeqFilter():
"""
filter homologous by taxonomy id
"""
def __init__(self, taxonomy):
"""
taxonomy: int, taxonomy id from NCBI for filter
NCBI: https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?mode=info&id=9606
"""
resp = requests.get("https://omabrowser.org/api/taxonomy/{}".format(taxonomy))
self.taxonomy = taxonomy
self.taxonomy_list = resp.text
def taxfilter(self, oma_fasta_path, grouped_fasta_path):
"""
oma_fasta_path: str, fasta file path for all OMA paralogs
grouped_fasta_path: str, fasta file path for grouped paralogs
return: None
"""
# read
oma_fasta_list = fasta_to_seqlist(oma_fasta_path)
# filter
filtered_list = []
for i in oma_fasta_list:
tax_id = i.description.split("|")[2].replace(" ", "")
if tax_id in self.taxonomy_list:
filtered_list.append(i)
with open(grouped_fasta_path, "w") as output_handle:
SeqIO.write(filtered_list, output_handle, "fasta") | python |
# 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 .application_event import ApplicationEvent
class ChaosRestartCodePackageFaultScheduledEvent(ApplicationEvent):
"""Chaos Restart Code Package Fault Scheduled event.
All required parameters must be populated in order to send to Azure.
:param event_instance_id: Required. The identifier for the FabricEvent
instance.
:type event_instance_id: str
:param time_stamp: Required. The time event was logged.
:type time_stamp: datetime
:param has_correlated_events: Shows there is existing related events
available.
:type has_correlated_events: bool
:param kind: Required. Constant filled by server.
:type kind: str
:param application_id: Required. The identity of the application. This is
an encoded representation of the application name. This is used in the
REST APIs to identify the application resource.
Starting in version 6.0, hierarchical names are delimited with the "\\~"
character. For example, if the application name is "fabric:/myapp/app1",
the application identity would be "myapp\\~app1" in 6.0+ and "myapp/app1"
in previous versions.
:type application_id: str
:param fault_group_id: Required. Id of fault group.
:type fault_group_id: str
:param fault_id: Required. Id of fault.
:type fault_id: str
:param node_name: Required. The name of a Service Fabric node.
:type node_name: str
:param service_manifest_name: Required. Service manifest name.
:type service_manifest_name: str
:param code_package_name: Required. Code package name.
:type code_package_name: str
:param service_package_activation_id: Required. Id of Service package
activation.
:type service_package_activation_id: str
"""
_validation = {
'event_instance_id': {'required': True},
'time_stamp': {'required': True},
'kind': {'required': True},
'application_id': {'required': True},
'fault_group_id': {'required': True},
'fault_id': {'required': True},
'node_name': {'required': True},
'service_manifest_name': {'required': True},
'code_package_name': {'required': True},
'service_package_activation_id': {'required': True},
}
_attribute_map = {
'event_instance_id': {'key': 'EventInstanceId', 'type': 'str'},
'time_stamp': {'key': 'TimeStamp', 'type': 'iso-8601'},
'has_correlated_events': {'key': 'HasCorrelatedEvents', 'type': 'bool'},
'kind': {'key': 'Kind', 'type': 'str'},
'application_id': {'key': 'ApplicationId', 'type': 'str'},
'fault_group_id': {'key': 'FaultGroupId', 'type': 'str'},
'fault_id': {'key': 'FaultId', 'type': 'str'},
'node_name': {'key': 'NodeName', 'type': 'str'},
'service_manifest_name': {'key': 'ServiceManifestName', 'type': 'str'},
'code_package_name': {'key': 'CodePackageName', 'type': 'str'},
'service_package_activation_id': {'key': 'ServicePackageActivationId', 'type': 'str'},
}
def __init__(self, **kwargs):
super(ChaosRestartCodePackageFaultScheduledEvent, self).__init__(**kwargs)
self.fault_group_id = kwargs.get('fault_group_id', None)
self.fault_id = kwargs.get('fault_id', None)
self.node_name = kwargs.get('node_name', None)
self.service_manifest_name = kwargs.get('service_manifest_name', None)
self.code_package_name = kwargs.get('code_package_name', None)
self.service_package_activation_id = kwargs.get('service_package_activation_id', None)
self.kind = 'ChaosRestartCodePackageFaultScheduled'
| python |
import pydantic as _pydantic
class CreditWalletConversion(_pydantic.BaseModel):
credit_wallet_type: str
rate: float
currency_code: str
class Config:
orm_mode = True
| python |
#!/usr/bin/env python
#-----------------------------------------------------------------------
#
# Core video, sound and interpreter loop for Gigatron TTL microcomputer
# - 6.25MHz clock
# - Rendering 160x120 pixels at 6.25MHz with flexible videoline programming
# - Must stay above 31 kHz horizontal sync --> 200 cycles/scanline
# - Must stay above 59.94 Hz vertical sync --> 521 scanlines/frame
# - 4 channels sound
# - 16-bits vCPU interpreter
# - Builtin vCPU programs
# - Serial input handler
# - Soft reset button (keep 'Start' button down for 2 seconds)
#
# Cleanup after ROM v1 release
# XXX Readability of asm.py instructions, esp. make d() implicit
# XXX GCL: Prefix notation for high/low byte >X++ instead of X>++
# XXX GCL: Rethink i, i. i; i= x, x. x= x: consistency, also DOKE, STLW etc
# XXX How it works memo: brief description of every software function
#
# Ideas for ROM v2
# XXX Music sequencer (combined with LED sequencer, but retire soundTimer???)
# XXX Adjustable return for LUP trampolines (in case SYS functions need it)
# XXX Loader: make noise when data comes in
# XXX vCPU: Multiplication (mulShift8?)
# XXX vCPU: Interrupts / Task switching (e.g for clock, LED sequencer)
# XXX Scroll out the top line of text, or generic vertical scroll SYS call
# XXX Multitasking/threading/sleeping (start with date/time clock in GCL)
# XXX Scoping for variables or some form of local variables? $i ("localized")
# XXX Simple GCL programs might be compiled by the host instead of offline?
# XXX vCPU: Clear just vAC[0:7] (Workaround is not bad: |255 ^255)
# XXX Random dots screensaver
# XXX Star field
#
# Application ideas:
# XXX Pacman ghosts. Sprites by scan line 4 reset method? ("videoG"=graphics)
# XXX Audio: Decay, using Karplus-Strong
# XXX ROM data compression (starting with Jupiter and Racer image)
# XXX Font screen 16x8 chars
# XXX Info screen (zero page)
# XXX Gigatron layout balls/bricks game
# XXX Embedded schematics
# XXX Maze game. Berzerk/Robotron? Pac Mac
# XXX Horizontal scroller. Flappy Bird
# XXX Primes, Fibonacci (bignum), Queens
# XXX Game of Life (edit <-> stop <-> slow <-> fast)
# XXX Game #5 Shooter. Space Invaders, Demon Attack, Galaga style
# XXX Exhibition mode: flip between applications in auto-play mode
#-----------------------------------------------------------------------
from sys import argv
from os import getenv
from asm import *
import gcl0x as gcl
import font
# Gigatron clock
cpuClock = 6.250e+06
# Output pin assignment for VGA
R, G, B, hSync, vSync = 1, 4, 16, 64, 128
syncBits = hSync+vSync # Both pulses negative
# When the XOUT register is in the circuit, the rising edge triggers its update.
# The loop can therefore not be agnostic to the horizontal pulse polarity.
assert(syncBits & hSync != 0)
# VGA 640x480 defaults (to be adjusted below!)
vFront = 10 # Vertical front porch
vPulse = 2 # Vertical sync pulse
vBack = 33 # Vertical back porch
vgaLines = vFront + vPulse + vBack + 480
vgaClock = 25.175e+06
# Video adjustments for Gigatron
# 1. Our clock is (slighty) slower than 1/4th VGA clock. Not all monitors will
# accept the decreased frame rate, so we restore the frame rate to above
# minimum 59.94 Hz by cutting some lines from the vertical front porch.
vFrontAdjust = vgaLines - int(4 * cpuClock / vgaClock * vgaLines)
vFront -= vFrontAdjust
# 2. Extend vertical sync pulse so we can feed the game controller the same
# signal. This is needed for controllers based on the 4021 instead of 74165
vPulseExtension = max(0, 8-vPulse)
vPulse += vPulseExtension
# 3. Borrow these lines from the back porch so the refresh rate remains
# unaffected
vBack -= vPulseExtension
# Game controller bits (actual controllers in kit have negative output)
# +-------------------------------------+
# | Up B* |
# | Left + Right B A* |
# | Down Select Start A |
# +-------------------------------------+ *=Auto fire
buttonRight = 1
buttonLeft = 2
buttonDown = 4
buttonUp = 8
buttonStart = 16
buttonSelect = 32
buttonB = 64
buttonA = 128
# Compile option: True restricts the calling of interpreter to calls from
# page 2, for 2 cycles less interpreter ENTER/EXIT overhead
fastRunVcpu = True
#-----------------------------------------------------------------------
#
# RAM page 0: variables
#
#-----------------------------------------------------------------------
# Memory size in pages from auto-detect
memSize = zpByte()
# The current channel number for sound generation. Advanced every scan line
# and independent of the vertical refresh to maintain constant oscillation.
channel = zpByte()
# Next sound sample being synthesized
sample = zpByte()
# To save one instruction in the critical inner loop, `sample' is always
# reset with its own address instead of, for example, the value 0. Compare:
# 1 instruction reset
# st sample,[sample]
# 2 instruction reset:
# ld 0
# st [sample]
# The difference is unhearable. This is fine when the reset/address
# value is low and doesn't overflow with 4 channels added to it.
# There is an alternative, but it requires pull-down diodes on the data bus:
# st [sample],[sample]
assert 4*63 + sample < 256
# We pin this reset/address value to 3, so `sample' swings from 3 to 255
assert sample == 3
# Booting
bootCount = zpByte() # 0 for cold boot
bootCheck = zpByte() # Checksum
# Entropy harvested from SRAM startup and controller input
entropy = zpByte(3)
# Visible video
videoY = zpByte() # Counts up from 0 to 238 in steps of 2
# Counts up during vertical blank (-44/-40 to 0)
frameX = zpByte() # Starting byte within page
frameY = zpByte() # Page of current pixel row (updated by videoA)
nextVideo = zpByte() # Jump offset to scan line handler (videoA, B, C...)
videoDorF = zpByte() # Handler for every 4th line (videoD or videoF)
# Vertical blank (reuse some variables used in the visible part)
videoSync0 = frameX # Vertical sync type on current line (0xc0 or 0x40)
videoSync1 = frameY # Same during horizontal pulse
# Frame counter is good enough as system clock
frameCount = zpByte(1)
# Serial input (game controller)
serialRaw = zpByte() # New raw serial read
serialLast = zpByte() # Previous serial read
buttonState = zpByte() # Clearable button state
resetTimer = zpByte() # After 2 seconds of holding 'Start', do a soft reset
# Extended output (blinkenlights in bit 0:3 and audio in but 4:7). This
# value must be present in AC during a rising hSync edge. It then gets
# copied to the XOUT register by the hardware. The XOUT register is only
# accessible in this indirect manner because it isn't part of the core
# CPU architecture.
xout = zpByte()
xoutMask = zpByte() # The blinkenlights and sound on/off state
# vCPU interpreter
vTicks = zpByte() # Interpreter ticks are units of 2 clocks
vPC = zpByte(2) # Interpreter program counter, points into RAM
vAC = zpByte(2) # Interpreter accumulator, 16-bits
vLR = zpByte(2) # Return address, for returning after CALL
vSP = zpByte(1) # Stack pointer
vTmp = zpByte()
if fastRunVcpu:
vReturn = zpByte(1) # Return into video loop
reserved31 = zpByte(1)
else:
vReturn = zpByte(2) # Return into video loop
# For future ROM extensions
reserved32 = zpByte()
# ROM type/version, numbering scheme to be determined, could be as follows:
# bit 4:7 Version
# bit 0:3 >=8 Formal revisions 8=alpa, 9=beta, 10=beta2...c=release, d=patch
# <8 experimental/informal revisions
# Perhaps it should just identify the application bindings,
# so don't call it romVersion already
romType = zpByte(1)
# SYS function arguments and results/scratch
sysFn = zpByte(2)
sysArgs = zpByte(8)
# Play sound if non-zero, count down and stop sound when zero
soundTimer = zpByte()
# Fow now the LED state machine itself is hard-coded in the program ROM
ledTimer = zpByte() # Number of ticks until next LED change
ledState = zpByte() # Current LED state
ledTempo = zpByte() # Next value for ledTimer after LED state change
# All bytes above, except 0x80, are free for temporary/scratch/stacks etc
zpFree = zpByte(0)
print 'zpFree %04x' % zpFree
#-----------------------------------------------------------------------
#
# RAM page 1: video line table
#
#-----------------------------------------------------------------------
# Byte 0-239 define the video lines
videoTable = 0x0100 # Indirection table: Y[0] dX[0] ..., Y[119] dX[119]
# Highest bytes are for channel 1 variables
# Sound synthesis ch1 ch2 ch3 ch4
wavA = 250
wavX = 251
keyL = 252
keyH = 253
oscL = 254
oscH = 255
#-----------------------------------------------------------------------
#
# RAM page 2: shift table
#
#-----------------------------------------------------------------------
soundTable = 0x0700
#-----------------------------------------------------------------------
#
# RAM page 3-7: application code GCL
#
#-----------------------------------------------------------------------
vCpuStart = 0x0200
#-----------------------------------------------------------------------
# Memory layout
#-----------------------------------------------------------------------
screenPages = 0x80 - 120 # Default start of screen memory: 0x0800 to 0x7fff
#-----------------------------------------------------------------------
# Application definitions
#-----------------------------------------------------------------------
maxTicks = 28/2 # Duration of slowest virtual opcode
define('$maxTicks', maxTicks)
vOverheadInt = 9 # Overhead of jumping in and out. Cycles, not ticks
vOverheadExt = 5 if fastRunVcpu else 7
maxSYS = -999 # Largest time slice for 'SYS
minSYS = +999 # Smallest time slice for 'SYS'
def runVcpu(n, ref=None):
"""Run interpreter for exactly n cycles"""
comment = 'Run vCPU for %s cycles' % n
if ref:
comment += ' (%s)' % ref
if n % 2 != (vOverheadExt + vOverheadInt) % 2:
nop()
comment = C(comment)
n -= 1
n -= vOverheadExt + vOverheadInt
print 'runVcpu at %04x cycles %3s info %s' % (pc(), n, ref)
n -= 2*maxTicks
assert n >= 0 and n % 2 == 0
global maxSYS, minSYS
maxSYS = max(maxSYS, n + 2*maxTicks)
minSYS = min(minSYS, n + 2*maxTicks)
# Tell GCL compiler this range, so it can check SYS call operands
define('$maxSYS', maxSYS)
define('$minSYS', minSYS)
n /= 2
returnPc = pc() + (5 if fastRunVcpu else 7)
ld(val(returnPc&255)) #0
comment = C(comment)
st(d(vReturn)) #1
if fastRunVcpu:
# In this mode [vReturn+1] will not be used
assert returnPc>>8 == 2
else:
# Allow interpreter to be called from anywhere
ld(val(returnPc>>8)) #2
st(d(vReturn+1)) #3
ld(val(hi('ENTER')),regY) #4
jmpy(d(lo('ENTER'))) #5
ld(val(n)) #6
#-----------------------------------------------------------------------
#
# ROM page 0: Boot
#
#-----------------------------------------------------------------------
align(0x100, 0x100)
# Give a first sign of life that can be checked with a voltmeter
ld(val(0b0000)); C('LEDs |OOOO|')
ld(val(syncBits^hSync), regOUT) # Prepare XOUT update, hSync goes down, RGB to black
ld(val(syncBits), regOUT) # hSync goes up, updating XOUT
# Simple RAM test and size check by writing to [1<<n] and see if [0] changes.
ld(val(1)); C('RAM test and count')
label('.countMem0')
st(d(memSize), busAC|ea0DregY)
ld(val(255))
xora(d(0), busRAM|eaYDregAC)
st(d(0), busAC|eaYDregAC) # Test if we can change and read back ok
st(d(0)) # Preserve (inverted) memory value in [0]
xora(d(0), busRAM|eaYDregAC)
bne(d(pc())) # Just hang here on apparent RAM failure
ld(val(255))
xora(d(0), busRAM|eaYDregAC)
st(d(0), busAC|eaYDregAC)
xora(d(0), busRAM)
beq(d(lo('.countMem1'))) # Wrapped and [0] changed as well
ldzp(d(memSize))
bra(d(lo('.countMem0')))
adda(busAC)
label('.countMem1')
# Momentarily wait to allow for debouncing of the reset switch by spinning
# roughly 2^15 times at 2 clocks per loop: 6.5ms@10MHz to 10ms@6.3MHz
# Real-world switches normally bounce shorter than that.
# "[...] 16 switches exhibited an average 1557 usec of bouncing, with,
# as I said, a max of 6200 usec" (From: http://www.ganssle.com/debouncing.htm)
# Relevant for the breadboard version, as the kit doesn't have a reset switch.
ld(val(255)); C('Debounce reset button')
label('.debounce')
st(d(0))
bne(d(pc()))
suba(val(1))
ldzp(d(0))
bne(d(lo('.debounce')))
suba(val(1))
# Update LEDs (memory is present and counted, reset is stable)
ld(val(0b0001)); C('LEDs |*OOO|')
ld(val(syncBits^hSync),regOUT)
ld(val(syncBits),regOUT)
# Scan the entire RAM space to collect entropy for a random number generator.
# The 16-bit address space is scanned, even if less RAM was detected.
ld(val(0)); C('Collect entropy from RAM')
st(d(vAC+0),busAC|ea0DregX)
st(d(vAC+1),busAC|ea0DregY)
label('.initEnt0')
ldzp(d(entropy+0))
bpl(d(lo('.initEnt1')))
adda(busRAM|eaYXregAC)
xora(val(191))
label('.initEnt1')
st(d(entropy+0))
ldzp(d(entropy+1))
bpl(d(lo('.initEnt2')))
adda(d(entropy+0),busRAM)
xora(val(193))
label('.initEnt2')
st(d(entropy+1))
adda(d(entropy+2),busRAM)
st(d(entropy+2))
ldzp(d(vAC+0))
adda(val(1))
bne(d(lo('.initEnt0')))
st(d(vAC+0),busAC|ea0DregX)
ldzp(d(vAC+1))
adda(val(1))
bne(d(lo('.initEnt0')))
st(d(vAC+1),busAC|ea0DregY)
# Update LEDs
ld(val(0b0011)); C('LEDs |**OO|')
ld(val(syncBits^hSync),regOUT)
ld(val(syncBits),regOUT)
# Determine if this is a cold or a warm start. We do this by checking the
# boot counter and comparing it to a simplistic checksum. The assumption
# is that after a cold start the checksum is invalid.
ldzp(d(bootCount)); C('Cold or warm boot?')
adda(d(bootCheck),busRAM)
adda(d(0x5a))
bne(d(lo('cold')))
ld(val(0))
label('warm')
ldzp(d(bootCount)) # if warm start: bootCount += 1
adda(val(1))
label('cold')
st(d(bootCount)) # if cold start: bootCount = 0
xora(val(255))
suba(val(0x5a-1))
st(d(bootCheck))
# vCPU reset handler
vReset = videoTable + 240 # we have 10 unused bytes behind the video table
ld(val((vReset&255)-2)); C('Setup vCPU reset handler')
st(d(vPC))
adda(val(2),regX)
ld(val(vReset>>8))
st(d(vPC+1),busAC|regY)
st(d(lo('LDI')), eaYXregOUTIX)
st(d(lo('SYS_Reset_36')),eaYXregOUTIX)
st(d(lo('STW')), eaYXregOUTIX)
st(d(sysFn), eaYXregOUTIX)
st(d(lo('SYS')), eaYXregOUTIX)
st(d(256-36/2+maxTicks), eaYXregOUTIX)
st(d(lo('SYS')), eaYXregOUTIX) # SYS_Exec_88
st(d(256-88/2+maxTicks), eaYXregOUTIX)
ld(val(255)); C('Setup serial input')
st(d(frameCount))
st(d(serialRaw))
st(d(serialLast))
st(d(buttonState))
st(d(resetTimer))
ld(val(0b0111)); C('LEDs |***O|')
ld(val(syncBits^hSync),regOUT)
ld(val(syncBits),regOUT)
# XXX Everything below should at one point migrate to Reset.gcl
# Init sound tables
ld(val(soundTable>>8),regY); C('Setup sound tables')
ld(val(0))
st(d(channel))
ld(val(0),regX)
label('.loop0')
st(d(vTmp)); C('Noise: T[4x+0] = x (permutate below)')
st(eaYXregOUTIX)
anda(d(0x20)); C('Triangle: T[4x+1] = 2x if x<32 else 127-2x')
bne(d(lo('.initTri0')))
ldzp(d(vTmp))
bra(d(lo('.initTri1')))
label('.initTri0')
adda(d(vTmp),busRAM)
xora(d(127))
label('.initTri1')
st(eaYXregOUTIX)
ldzp(d(vTmp)); C('Pulse: T[4x+2] = 0 if x<32 else 63')
anda(d(0x20))
beq(d(lo('.initPul')))
ld(d(0))
ld(d(63))
label('.initPul')
st(eaYXregOUTIX)
ldzp(d(vTmp)); C('Sawtooth: T[4x+3] = x')
st(eaYXregOUTIX)
adda(val(1))
xora(val(0x40))
bne(d(lo('.loop0')))
xora(val(0x40))
ld(d(0)); C('Permutate noise table T[4i]')
st(d(vAC+0)); C('x')
st(d(vAC+1)); C('4y')
label('.loop1')
ld(d(vAC+1),busRAM|regX); C('tmp = T[4y]')
ld(eaYXregAC,busRAM)
st(d(vTmp))
ld(d(vAC+0),busRAM); C('T[4y] = T[4x]')
adda(busAC)
adda(busAC,regX)
ld(eaYXregAC,busRAM)
ld(d(vAC+1),busRAM|regX)
st(eaYXregAC)
adda(busAC); C('y += T[4x]')
adda(busAC)
adda(d(vAC+1),busRAM)
st(d(vAC+1))
ld(d(vAC+0),busRAM); C('T[x] = tmp')
adda(busAC)
adda(busAC,regX)
ldzp(d(vTmp))
st(eaYXregAC)
ldzp(d(vAC+0)); C('while(++x)')
adda(d(1))
bne(d(lo('.loop1')))
st(d(vAC+0))
# Init LED sequencer
ld(val(120)); C('Setup LED sequencer')
st(d(ledTimer))
ld(val(60/6))
st(d(ledTempo))
ld(val(0))
st(d(ledState))
ld(val(0b1111)); C('LEDs |****|')
ld(val(syncBits^hSync),regOUT)
ld(val(syncBits),regOUT)
st(d(xout)) # Setup for control by video loop
st(d(xoutMask))
ld(d(hi('vBlankStart')),busD|ea0DregY);C('Enter video loop')
jmpy(d(lo('vBlankStart')))
ld(val(syncBits))
nop()
nop()
#-----------------------------------------------------------------------
# Extension SYS_Reset_36: Soft reset
#-----------------------------------------------------------------------
# SYS_Reset_36 initiates an immediate Gigatron reset from within the vCPU.
# The reset sequence itself is mostly implemented in GCL by Reset.gcl .
# This must first be loaded into RAM. But as that takes more than 1 scanline,
# some vCPU bootstrapping code gets loaded with SYS_Exec_88. The caller of
# SYS_Reset_36 provides the SYS instruction to execute that.
label('SYS_Reset_36')
assert(pc()>>8==0)
value = getenv('romType')
value = int(value, 0) if value else 0
ld(d(value)); C('Set ROM type/version')#15
st(d(romType)) #16
ld(val(0)) #17
st(d(vSP)) #18 Reset stack pointer
assert(vCpuStart&255==0)
st(d(vLR)) #19
st(d(soundTimer)) #20
ld(val(vCpuStart>>8)) #21
st(d(vLR+1)) #22
ld(d(lo('videoF'))) #23 Do this before first visible pixels
st(d(videoDorF)) #24
ld(d(lo('SYS_Exec_88'))) #25
st(d(sysFn)) #26 High byte (remains) 0
ld(d(lo('Reset'))) #27
st(d(sysArgs+0)) #28
ld(d(hi('Reset'))) #29
st(d(sysArgs+1)) #30
# Return to interpreter
ld(val(hi('REENTER')),regY) #31
jmpy(d(lo('REENTER'))) #32
ld(val(-36/2)) #33
#-----------------------------------------------------------------------
# Extension SYS_Exec_88: Load code from ROM into memory and execute it
#-----------------------------------------------------------------------
#
# This loads the vCPU code with consideration of the current vSP
# Used during reset, but also for switching between applications
# or for loading data from ROM during an application.
#
# ROM stream format is [<addrH> <addrL> <n&255> n*<byte>]* 0
# on top of lookup tables.
#
# Variables:
# sysArgs[0:1] ROM pointer (input set by caller)
# sysArgs[2:3] RAM pointer (variable)
# sysArgs[4] State counter (variable)
# vLR vCPU continues here (input set by caller)
label('SYS_Exec_88')
assert(pc()>>8==0)
ld(val(0)) #15 Address of loader on zero page
st(d(vPC+1),busAC|regY) #16
ldzp(d(vSP)) #17 Below the current stack pointer
suba(d(53+2)) #18 (AC -> *+0)
st(d(vTmp),busAC|regX) #19
adda(val(-2)) #20 (AC -> *-2)
st(d(vPC)) #21
# Start of manually compiled vCPU section
st(d(lo('PUSH') ),eaYXregOUTIX) #22 *+0
st(d(lo('BRA') ),eaYXregOUTIX) #23 *+1
adda(val(26)) #24 (AC -> *+24)
st( eaYXregOUTIX) #25 *+2
st(d(lo('ST') ),eaYXregOUTIX) #26 *+3 Chunk copy loop
st(d(sysArgs+3 ),eaYXregOUTIX) #27 *+4 High-address came first
st(d(lo('CALL') ),eaYXregOUTIX) #28 *+5
adda(val(33-24)) #29 (AC -> *+33)
st( eaYXregOUTIX) #30 *+6
st(d(lo('ST') ),eaYXregOUTIX) #31 *+7
st(d(sysArgs+2 ),eaYXregOUTIX) #32 *+8 Then the low address
st(d(lo('CALL') ),eaYXregOUTIX) #33 *+9
st( eaYXregOUTIX) #34 *+10
st(d(lo('ST') ),eaYXregOUTIX) #35 *+11 Byte copy loop
st(d(sysArgs+4 ),eaYXregOUTIX) #36 *+12 Byte count (0 means 256)
st(d(lo('CALL') ),eaYXregOUTIX) #37 *+13
st( eaYXregOUTIX) #38 *+14
st(d(lo('POKE') ),eaYXregOUTIX) #39 *+15
st(d(sysArgs+2 ),eaYXregOUTIX) #40 *+16
st(d(lo('INC') ),eaYXregOUTIX) #41 *+17
st(d(sysArgs+2 ),eaYXregOUTIX) #42 *+18
st(d(lo('LD') ),eaYXregOUTIX) #43 *+19
st(d(sysArgs+4 ),eaYXregOUTIX) #44 *+20
st(d(lo('SUBI') ),eaYXregOUTIX) #45 *+21
st(d(1 ),eaYXregOUTIX) #46 *+22
st(d(lo('BCC') ),eaYXregOUTIX) #47 *+23
st(d(lo('NE') ),eaYXregOUTIX) #48 *+24
adda(val(11-2-33)) #49 (AC -> *+9)
st( eaYXregOUTIX) #50 *+25
st(d(lo('CALL') ),eaYXregOUTIX) #51 *+26 Go to next block
adda(val(33-9)) #52 (AC -> *+33)
st( eaYXregOUTIX) #53 *+27
st(d(lo('BCC') ),eaYXregOUTIX) #54 *+28
st(d(lo('NE') ),eaYXregOUTIX) #55 *+29
adda(val(3-2-33)) #56 (AC -> *+1)
st( eaYXregOUTIX) #57 *+30
st(d(lo('POP') ),eaYXregOUTIX) #58 *+31 End
st(d(lo('RET') ),eaYXregOUTIX) #59 *+32
# Pointer constant pointing to the routine below (for use by CALL)
adda(val(35-1)) #60 (AC -> *+35)
st( eaYXregOUTIX) #61 *+33
st(d(0 ),eaYXregOUTIX) #62 *+34
# Routine to read next byte from ROM and advance read pointer
st(d(lo('LD') ),eaYXregOUTIX) #63 *+35 Test for end of ROM table
st(d(sysArgs+0 ),eaYXregOUTIX) #64 *+36
st(d(lo('XORI') ),eaYXregOUTIX) #65 *+37
st(d(251 ),eaYXregOUTIX) #66 *+38
st(d(lo('BCC') ),eaYXregOUTIX) #67 *+39
st(d(lo('NE') ),eaYXregOUTIX) #68 *+40
adda(val(46-2-35)) #69 (AC -> *+44)
st( eaYXregOUTIX) #70 *+41
st(d(lo('ST') ),eaYXregOUTIX) #71 *+42 Wrap to next ROM page
st(d(sysArgs+0 ),eaYXregOUTIX) #72 *+43
st(d(lo('INC') ),eaYXregOUTIX) #73 *+44
st(d(sysArgs+1 ),eaYXregOUTIX) #74 *+45
st(d(lo('LDW') ),eaYXregOUTIX) #75 *+46 Read next byte from ROM table
st(d(sysArgs+0 ),eaYXregOUTIX) #76 *+47
st(d(lo('LUP') ),eaYXregOUTIX) #77 *+48
st(d(0 ),eaYXregOUTIX) #78 *+49
st(d(lo('INC') ),eaYXregOUTIX) #79 *+50 Increment read pointer
st(d(sysArgs+0 ),eaYXregOUTIX) #80 *+51
st(d(lo('RET') ),eaYXregOUTIX) #81 *+52 Return
# Return to interpreter
nop() #82
ld(val(hi('REENTER')),regY) #83
jmpy(d(lo('REENTER'))) #84
ld(val(-88/2)) #85
#-----------------------------------------------------------------------
# Extension SYS_Out_22: Send byte to output port
#-----------------------------------------------------------------------
label('SYS_Out_22')
ld(d(sysArgs+0),busRAM|regOUT) #15
nop() #16
ld(val(hi('REENTER')),regY) #17
jmpy(d(lo('REENTER'))) #18
ld(val(-22/2)) #19
#-----------------------------------------------------------------------
# Extension SYS_In_24: Read a byte from the input port
#-----------------------------------------------------------------------
label('SYS_In_24')
st(d(vAC),busIN) #15
ld(val(0)) #16
st(d(vAC+1)) #17
nop() #18
ld(val(hi('REENTER')),regY) #19
jmpy(d(lo('REENTER'))) #20
ld(val(-24/2)) #21
assert pc()&255==0
#-----------------------------------------------------------------------
#
# ROM page 1-2: Video loop
#
#-----------------------------------------------------------------------
align(0x100, 0x200)
# Back porch A: first of 4 repeated scan lines
# - Fetch next Yi and store it for retrieval in the next scan lines
# - Calculate Xi from dXi, but there is no cycle time left to store it as well
label('videoA')
assert(lo('videoA') == 0) # videoA starts at the page boundary
ld(d(lo('videoB'))) #29
st(d(nextVideo)) #30
ld(d(videoTable>>8), regY) #31
ld(d(videoY), busRAM|regX) #32
ld(eaYXregAC, busRAM) #33
st(eaYXregOUTIX) #34 Just to increment X
st(d(frameY)) #35
ld(eaYXregAC, busRAM) #36
adda(d(frameX), busRAM|regX) #37
ld(d(frameY), busRAM|regY) #38
ld(val(syncBits)) #39
# Stream 160 pixels from memory location <Yi,Xi> onwards
# Superimpose the sync signal bits to be robust against misprogramming
label('pixels')
for i in range(160):
ora(eaYXregOUTIX, busRAM) #40-199
if i==0: C('Pixel burst')
ld(val(syncBits), regOUT); C('<New scan line start>')#0 Back to black
# Front porch
ldzp(d(channel));C('Advance to next sound channel')#1
label('soundF')
anda(val(3)) #2
adda(val(1)) #3
ld(val(syncBits^hSync), regOUT);C('Start horizontal pulse')#4
# Horizontal sync
label('sound2')
st(d(channel),busAC|ea0DregY) #5 Sound
ld(val(0x7f)) #6
anda(d(oscL),busRAM|eaYDregAC) #7
adda(d(keyL),busRAM|eaYDregAC) #8
st(d(oscL),busAC|eaYDregAC) #9
anda(val(0x80),regX) #10
ld(busRAM|ea0XregAC) #11
adda(d(oscH),busRAM|eaYDregAC) #12
adda(d(keyH),busRAM|eaYDregAC) #13
st(d(oscH), busAC|eaYDregAC) #14
anda(val(0xfc)) #15
xora(d(wavX),busRAM|eaYDregAC) #16
ld(busAC,regX) #17
ld(d(wavA),busRAM|eaYDregAC) #18
ld(d(soundTable>>8),regY) #19
adda(busRAM|eaYXregAC) #20
bmi(d(lo('.sound2a'))) #21
bra(d(lo('.sound2b'))) #22
anda(d(63)) #23
label('.sound2a')
ld(d(63)) #23
label('.sound2b')
adda(d(sample), busRAM|ea0DregAC)#24
st(d(sample)) #25
ldzp(d(xout)); C('Gets copied to XOUT')#26
bra(d(nextVideo)|busRAM) #27
ld(val(syncBits), regOUT); C('End horizontal pulse')#28
# Back porch B: second of 4 repeated scan lines
# - Recompute Xi from dXi and store for retrieval in the next scan lines
label('videoB')
ld(d(lo('videoC'))) #29
st(d(nextVideo)) #30
ld(d(videoTable>>8), regY) #31
ldzp(d(videoY)) #32
adda(d(1), regX) #33
ldzp(d(frameX)) #34
adda(eaYXregAC, busRAM) #35
st(d(frameX), busAC|ea0DregX) #36 Undocumented opcode "store in RAM and X"!
ld(d(frameY), busRAM|regY) #37
bra(d(lo('pixels'))) #38
ld(val(syncBits)) #39
# Back porch C: third of 4 repeated scan lines
# - Nothing new to do, Yi and Xi are known
label('videoC')
ldzp(d(sample)); C('New sound sample is ready')#29 First something that didn't fit in the audio loop
ora(d(0x0f)) #30
anda(d(xoutMask),busRAM|ea0DregAC)#31
st(d(xout)) #32 Update [xout] with new sample (4 channels just updated)
st(val(sample),ea0DregAC|busD); C('Reset for next sample')#33 Reset for next sample
ldzp(d(videoDorF)); C('Mode for scan line 4')#34 Now back to video business
st(d(nextVideo)) #35
ld(d(frameX),busRAM|regX) #36
ld(d(frameY),busRAM|regY) #37
bra(d(lo('pixels'))) #38
ld(val(syncBits)) #39
# Back porch D: last of 4 repeated scan lines
# - Calculate the next frame index
# - Decide if this is the last line or not
label('videoD') # Default video mode
ld(d(frameX), busRAM|regX) #29
ldzp(d(videoY)) #30
suba(d((120-1)*2)) #31
beq(d(lo('.last'))) #32
ld(d(frameY), busRAM|regY) #33
adda(d(120*2)) #34 More pixel lines to go
st(d(videoY)) #35
ld(d(lo('videoA'))) #36
st(d(nextVideo)) #37
bra(d(lo('pixels'))) #38
ld(val(syncBits)) #39
label('.last')
wait(36-34) #34 No more pixel lines
ld(d(lo('videoE'))) #36
st(d(nextVideo)) #37
bra(d(lo('pixels'))) #38
ld(val(syncBits)) #39
# Back porch "E": after the last line
# - Go back to program page 0 and enter vertical blank
label('videoE') # Exit visible area
ld(d(hi('vBlankStart')),ea0DregY)#29
jmpy(d(lo('vBlankStart')) ) #30
ld(val(syncBits)) #31
# Back porch "F": scan lines and fast mode
label('videoF') # Fast video mode
ldzp(d(videoY)) #29
suba(d((120-1)*2)) #30
bne(d(lo('.notlast'))) #31
adda(d(120*2)) #32
bra(d(lo('.join'))) #33
ld(d(lo('videoE'))) #34 No more visible lines
label('.notlast')
st(d(videoY)) #33 More visible lines
ld(d(lo('videoA'))) #34
label('.join')
st(d(nextVideo)) #35
runVcpu(199-36, 'line41-521 typeF')#36 Application (every 4th of scan lines 41-521)
ld(d(hi('soundF')), busD|ea0DregY)#199 XXX This is on the current page
jmpy(d(lo('soundF'))); C('<New scan line start>')#0
ldzp(d(channel)) #1 Advance to next sound channel
# Vertical blank part of video loop
label('vBlankStart') # Start of vertical blank interval
assert(pc()&255<16) # Assure that we are in the beginning of the next page
st(d(videoSync0)); C('Start of vertical blank interval')#32
ld(val(syncBits^hSync)) #33
st(d(videoSync1)) #34
# (Re)initialize carry table for robustness
st(d(0x00), ea0DregAC|busD); C('Carry table')#35
ld(val(0x01)) #36
st(d(0x80)) #37
# It is nice to set counter before vCPU starts
ld(val(1-2*(vFront+vPulse+vBack-2)))#38 -2 because first and last are different
st(d(videoY)) #39
# Uptime frame count (3 cycles)
ldzp(d(frameCount)); C('Frame counter')#40
adda(val(1)) #41
st(d(frameCount)) #42
# Mix entropy (11 cycles)
xora(d(entropy+1),busRAM); C('Mix entropy')#43
xora(d(serialRaw),busRAM) #44 Mix in serial input
adda(d(entropy+0),busRAM) #45
st(d(entropy+0)) #46
adda(d(entropy+2),busRAM) #47 Some hidden state
st(d(entropy+2)) #48
bmi(d(lo('.rnd0'))) #49
bra(d(lo('.rnd1'))) #50
xora(val(64+16+2+1)) #51
label('.rnd0')
xora(val(64+32+8+4)) #51
label('.rnd1')
adda(d(entropy+1),busRAM) #52
st(d(entropy+1)) #53
# LED sequencer (19 cycles)
ldzp(d(ledTimer)); C('Blinkenlight sequencer')#54
bne(d(lo('.leds4'))) #55
ld(d(lo('.leds0'))) #56
adda(d(ledState)|busRAM) #57
bra(busAC) #58
bra(d(lo('.leds1'))) #59
label('.leds0')
ld(d(0b1111));C('LEDs |****|') #60
ld(d(0b0111));C('LEDs |***O|') #60
ld(d(0b0011));C('LEDs |**OO|') #60
ld(d(0b0001));C('LEDs |*OOO|') #60
ld(d(0b0010));C('LEDs |O*OO|') #60
ld(d(0b0100));C('LEDs |OO*O|') #60
ld(d(0b1000));C('LEDs |OOO*|') #60
ld(d(0b0100));C('LEDs |OO*O|') #60
ld(d(0b0010));C('LEDs |O*OO|') #60
ld(d(0b0001));C('LEDs |*OOO|') #60
ld(d(0b0011));C('LEDs |**OO|') #60
ld(d(0b0111));C('LEDs |***O|') #60
ld(d(0b1111));C('LEDs |****|') #60
ld(d(0b1110));C('LEDs |O***|') #60
ld(d(0b1100));C('LEDs |OO**|') #60
ld(d(0b1000));C('LEDs |OOO*|') #60
ld(d(0b0100));C('LEDs |OO*O|') #60
ld(d(0b0010));C('LEDs |O*OO|') #60
ld(d(0b0001));C('LEDs |*OOO|') #60
ld(d(0b0010));C('LEDs |O*OO|') #60
ld(d(0b0100));C('LEDs |OO*O|') #60
ld(d(0b1000));C('LEDs |OOO*|') #60
ld(d(0b1100));C('LEDs |OO**|') #60
ld(d(0b1110+128)) #60
C('LEDs |O***|')
label('.leds1')
st(d(xoutMask)) #61 Temporarily park new state here
bmi(d(lo('.leds2'))) #62
bra(d(lo('.leds3'))) #63
ldzp(d(ledState)) #64
label('.leds2')
ld(val(-1)) #64
label('.leds3')
adda(val(1)) #65
st(d(ledState)) #66
bra(d(lo('.leds5'))) #67
ldzp(d(ledTempo)) #68 Setup the LED timer for the next period
label('.leds4')
wait(67-57) #57
ldzp(d(ledTimer)) #67
suba(d(1)) #68
label('.leds5')
st(d(ledTimer)) #69
ldzp(d(xoutMask)) #70 Low 4 bits are the LED output
anda(val(0b00001111)) #71 High bits will be restored below
st(d(xoutMask)) #72
# When the total number of scan lines per frame is not an exact multiple of the
# (4) channels, there will be an audible discontinuity if no measure is taken.
# This static noise can be suppressed by swallowing the first `lines mod 4'
# partial samples after transitioning into vertical blank. This is easiest if
# the modulo is 0 (do nothing) or 1 (reset sample while in the first blank scan
# line). For the two other cases there is no solution yet: give a warning.
soundDiscontinuity = (vFront+vPulse+vBack) % 4
extra = 0
if soundDiscontinuity == 1:
st(val(sample), ea0DregAC|busD) # XXX We're swallowing _2_ samples here!
C('Sound continuity')
extra += 1
if soundDiscontinuity > 1:
print "Warning: sound discontinuity not supressed"
runVcpu(189-73-extra, 'line0') #73 Application cycles (scan line 0)
# Sound on/off (6 cycles)
ldzp(d(soundTimer)); C('Sound on/off')#189
bne(d(lo('.snd0'))) #190
bra(d(lo('.snd1'))) #191
ld(val(0)) #192 Sound off
label('.snd0')
ld(val(0xf0)) #192 Sound on
label('.snd1')
ora(d(xoutMask),busRAM) #193
st(d(xoutMask)) #194
# Sound timer count down (5 cycles)
ldzp(d(soundTimer)); C('Sound timer')#195
beq(d(lo('.snd2'))) #196
bra(d(lo('.snd3'))) #197
suba(val(1)) #198
label('.snd2')
ld(val(0)) #198
label('.snd3')
st(d(soundTimer)) #199
ld(d(videoSync0), busRAM|regOUT);C('<New scan line start>')#0
label('sound1')
ldzp(d(channel)); C('Advance to next sound channel')#1
anda(val(3)) #2
adda(val(1)) #3
ld(d(videoSync1),busRAM|regOUT) ;C('Start horizontal pulse')#4
st(d(channel),busAC|ea0DregY) #5
ld(val(0x7f)) ;C('Update sound channel')#6
anda(d(oscL),busRAM|eaYDregAC) #7
adda(d(keyL),busRAM|eaYDregAC) #8
st(d(oscL), busAC|eaYDregAC) #9
anda(val(0x80), regX) #10
ld(busRAM|ea0XregAC) #11
adda(d(oscH),busRAM|eaYDregAC) #12
adda(d(keyH),busRAM|eaYDregAC) #13
st(d(oscH),busAC|eaYDregAC) #14
anda(d(0xfc)) #15
xora(d(wavX),busRAM|eaYDregAC) #16
ld(busAC,regX) #17
ld(d(wavA),busRAM|eaYDregAC) #18
ld(d(soundTable>>8),regY) #19
adda(busRAM|eaYXregAC) #20
bmi(d(lo('.sound1a'))) #21
bra(d(lo('.sound1b'))) #22
anda(d(63)) #23
label('.sound1a')
ld(d(63)) #23
label('.sound1b')
adda(d(sample),busRAM|ea0DregAC)#24
st(d(sample)) #25
ldzp(d(xout)); C('Gets copied to XOUT')#26
nop() #27
ld(d(videoSync0),busRAM|regOUT) ;C('End horizontal pulse')#28
# Count through the vertical blank interval until its last scan line
ldzp(d(videoY)) #29
bpl(d(lo('vBlankLast'))) #30
adda(d(2)) #31
st(d(videoY)) #32
# Determine if we're in the vertical sync pulse
suba(d(1-2*(vBack-1))) #33
bne(d(lo('vSync0'))) #34 Tests for end of vPulse
adda(d(2*vPulse)) #35
ld(val(syncBits)) #36 Entering vertical back porch
bra(d(lo('vSync2'))) #37
st(d(videoSync0)) #38
label('vSync0')
bne(d(lo('vSync1'))) #36 Tests for start of vPulse
ld(val(syncBits^vSync)) #37
bra(d(lo('vSync3'))) #38 Entering vertical sync pulse
st(d(videoSync0)) #39
label('vSync1')
ldzp(d(videoSync0)) #38 Load current value
label('vSync2')
nop() #39
label('vSync3')
xora(d(hSync)) #40 Precompute, as during the pulse there is no time
st(d(videoSync1)) #41
# Capture the serial input before the '595 shifts it out
ldzp(d(videoY)); C('Capture serial input')#42
xora(val(1-2*(vBack-1-1))) #43 Exactly when the 74HC595 has captured all 8 controller bits
bne(d(lo('.ser0'))) #44
bra(d(lo('.ser1'))) #45
st(d(serialRaw),busIN) #46
label('.ser0')
nop() #46
label('.ser1')
# Update [xout] with the next sound sample every 4 scan lines.
# Keep doing this on 'videoC equivalent' scan lines in vertical blank.
ldzp(d(videoY)) #47
anda(d(6)) #48
bne(d(lo('vBlankNormal'))) #49
ldzp(d(sample)) #50
label('vBlankSample')
ora(d(0x0f)); C('New sound sample is ready')#51
anda(d(xoutMask),busRAM|ea0DregAC)#52
st(d(xout)) #53
st(val(sample), ea0DregAC|busD); C('Reset for next sample')#54
runVcpu(199-55, 'line1-39 typeC')#55 Appplication cycles (scan line 1-43 with sample update)
bra(d(lo('sound1'))) #199
ld(d(videoSync0), busRAM|regOUT);C('<New scan line start>')#0 Ends the vertical blank pulse at the right cycle
label('vBlankNormal')
runVcpu(199-51, 'line1-39 typeABD')#51 Application cycles (scan line 1-43 without sample update)
bra(d(lo('sound1'))) #199
ld(d(videoSync0), busRAM|regOUT);C('<New scan line start>')#0 Ends the vertical blank pulse at the right cycle
# Last blank line before transfering to visible area
label('vBlankLast')
# pChange = pNew & ~pOld
# nChange = nNew | ~nOld {DeMorgan}
# Filter raw serial input captured in last vblank (8 cycles)
ld(val(255)); C('Filter controller input')#32
xora(d(serialLast),busRAM) #33
ora(d(serialRaw),busRAM) #34 Catch button-press events
anda(d(buttonState),busRAM) #35 Keep active button presses
ora(d(serialRaw),busRAM) #36 Auto-reset already-released buttons
st(d(buttonState)) #37
ldzp(d(serialRaw)) #38
st(d(serialLast)) #39
# Respond to reset button (11 cycles)
xora(val(~buttonStart)); C('Check for soft reset')#40
bne(d(lo('.restart0'))) #41
ldzp(d(resetTimer)) #42 As long as button pressed
suba(val(1)) #43 ... count down the timer
st(d(resetTimer)) #44
anda(d(127)) #45
beq(d(lo('.restart2'))) #46
ld(val((vReset&255)-2)) #47 Start force reset when hitting 0
bra(d(lo('.restart1'))) #48 ... otherwise do nothing yet
bra(d(lo('.restart3'))) #49
label('.restart0')
ld(val(127)) #43 Restore to ~2 seconds when not pressed
st(d(resetTimer)) #44
wait(49-45) #45
bra(d(lo('.restart3'))) #49
label('.restart1')
nop() #50
label('.restart2')
st(d(vPC)) #48 Continue force reset
ld(val(vReset>>8)) #49
st(d(vPC+1)) #50
label('.restart3')
# --- Switch video mode when (only) select is pressed
ldzp(d(buttonState)) #51
xora(val(~buttonSelect)) #52
beq(d(lo('.select0'))) #53
bra(d(lo('.select1'))) #54
ld(val(0)) #55
label('.select0')
ld(val(lo('videoD')^lo('videoF')))#55
label('.select1')
xora(d(videoDorF),busRAM) #56
st(d(videoDorF)) #57
ldzp(d(buttonState)) #58
ora(val(buttonSelect)) #59
st(d(buttonState)) #60
runVcpu(196-61, 'line40') #61 Application cycles (scan line 40)
# vAC==0 now
st(d(videoY)) #196
st(d(frameX)) #197
st(d(nextVideo)) #198 videoA=0
ldzp(d(channel)) #199 Advance to next sound channel
anda(val(3)); C('<New scan line start>')#0
adda(val(1)) #1
ld(d(hi('sound2')), busD|ea0DregY)#2
jmpy(d(lo('sound2'))) #3
ld(val(syncBits^hSync), regOUT) #4 Start horizontal pulse
nop()
nop()
nop()
nop()
nop()
#-----------------------------------------------------------------------
# Extension SYS_NextByteIn_32
#-----------------------------------------------------------------------
# sysArgs[0:1] Current address
# sysArgs[2] Checksum
# sysArgs[3] Wait value (videoY)
label('SYS_NextByteIn_32')
ldzp(d(videoY)) #15
xora(d(sysArgs+3),busRAM) #16
bne(d(lo('.sysNbi'))) #17
ld(d(sysArgs+0),busRAM|regX) #18
ld(d(sysArgs+1),busRAM|regY) #19
ld(busIN) #20
st(eaYXregAC) #21
adda(d(sysArgs+2),busRAM) #22
st(d(sysArgs+2)) #23
ldzp(d(sysArgs+0)) #24
adda(d(1)) #25
st(d(sysArgs+0)) #26
ld(val(hi('REENTER')),regY) #27
jmpy(d(lo('REENTER'))) #28
ld(val(-32/2)) #29
# Restart instruction
label('.sysNbi')
ldzp(d(vPC)) #19
suba(d(2)) #20
st(d(vPC)) #21
ld(val(-28/2)) #22
ld(val(hi('REENTER')),regY) #23
jmpy(d(lo('REENTER'))) #24
nop() #25
assert(pc()&255 == 255)
#-----------------------------------------------------------------------
#
# ROM page 3: Application interpreter primary page
#
#-----------------------------------------------------------------------
# Enter the timing-aware application interpreter (aka virtual CPU, vCPU)
#
# This routine will execute as many as possible instructions in the
# alotted time. When time runs out, it synchronizes such that the total
# duration matches the caller's request. Durations are counted in `ticks',
# which are multiples of 2 clock cycles.
#
# Synopsis: Use the runVcpu() macro as entry point
# We let 'ENTER' begin one word before the page boundary, for a bit extra
# precious space in the packed interpreter code page. Although ENTER's
# first instruction is bra() which normally doesn't cross page boundaries,
# in this case it will still jump into the right space, because branches
# from $xxFF land in the next page anyway.
while pc()&255 < 255:
nop()
label('ENTER')
bra(d(lo('.next2'))) #0 Enter at '.next2' (so no startup overhead)
C('vCPU interpreter')
# --- Page boundary ---
align(0x100,0x100)
ld(d(vPC+1),busRAM|regY) #1
# Fetch next instruction and execute it, but only if there are sufficient
# ticks left for the slowest instruction.
label('NEXT')
adda(d(vTicks),busRAM); C('Track elapsed ticks')#0 Actually counting down (AC<0)
blt(d(lo('EXIT'))); C('Escape near time out')#1
label('.next2')
st(d(vTicks)) #2
ldzp(d(vPC)); C('Advance vPC')#3
adda(val(2)) #4
st(d(vPC),busAC|ea0DregX) #5
ld(busRAM|eaYXregAC); C('Fetch opcode')#6 Fetch opcode (actually a branch target)
st(eaYXregOUTIX); #7 Just X++
bra(busAC); C('Dispatch')#8
ld(busRAM|eaYXregAC); C('Prefetch operand')#9
# Resync with caller and return
label('EXIT')
adda(val(maxTicks)) #3
bgt(d(pc())); C('Resync')#4
suba(val(1)) #5
if fastRunVcpu:
ld(val(2),regY) #6
else:
ld(d(vReturn+1),busRAM|regY) #6
jmpy(d(vReturn+0)|busRAM); C('Return to caller')#7
ld(val(0)) #8 AC should be 0 already. Still..
assert vOverheadInt == 9
# Instruction LDWI: Load immediate constant (AC=$DDDD), 20 cycles
label('LDWI')
st(d(vAC)) #10
st(eaYXregOUTIX) #11 Just to increment X
ld(busRAM|eaYXregAC) #12 Fetch second operand
st(d(vAC+1)) #13
ldzp(d(vPC)) #14 Advance vPC one more
adda(val(1)) #15
st(d(vPC)) #16
ld(val(-20/2)) #17
bra(d(lo('NEXT'))) #18
#nop() #(19)
#
# Instruction LD: Load from zero page (AC=[D]), 18 cycles
label('LD')
ld(busAC,regX) #10,19 (overlap with LDWI)
ldzp(busRAM|ea0XregAC) #11
st(d(vAC)) #12
ld(val(0)) #13
st(d(vAC+1)) #14
ld(val(-18/2)) #15
bra(d(lo('NEXT'))) #16
#nop() #(17)
#
# Instruction LDW: Word load from zero page (AC=[D],[D+1]), 20 cycles
label('LDW')
ld(busAC,regX) #10,17 (overlap with LD)
adda(val(1)) #11
st(d(vTmp)) #12 Address of high byte
ld(busRAM|ea0XregAC) #13
st(d(vAC)) #14
ld(d(vTmp),busRAM|regX) #15
ld(busRAM|ea0XregAC) #16
st(d(vAC+1)) #17
bra(d(lo('NEXT'))) #18
ld(val(-20/2)) #19
#nop() #(20)
#
# Instruction STW: Word load from zero page (AC=[D],[D+1]), 20 cycles
label('STW')
ld(busAC,regX) #10,20 (overlap with LDW)
adda(val(1)) #11
st(d(vTmp)) #12 Address of high byte
ldzp(d(vAC)) #13
st(ea0XregAC) #14
ld(d(vTmp),busRAM|regX) #15
ldzp(d(vAC+1)) #16
st(ea0XregAC) #17
bra(d(lo('NEXT'))) #18
ld(val(-20/2)) #19
# Instruction BCC: Test AC sign and branch conditionally, 28 cycles
label('BCC')
ldzp(d(vAC+1)) #10 First inspect high byte ACH
bne(d(lo('.cond2'))) #11
st(d(vTmp)) #12
ldzp(d(vAC)) #13 Additionally inspect low byte ACL
beq(d(lo('.cond3'))) #14
ld(val(1)) #15
st(d(vTmp)) #16
ld(busRAM|eaYXregAC) #17 Operand is the conditional
label('.cond1')
bra(busAC) #18
ldzp(d(vTmp)) #19
# Conditional EQ: Branch if zero (if(ALC==0)PCL=D)
label('EQ')
bne(d(lo('.cond4'))) #20
label('.cond2')
beq(d(lo('.cond5'))); C('AC=0 in EQ, AC!=0 from BCC...')#21,13 (overlap with BCC)
ld(busRAM|eaYXregAC) #22,14 (overlap with BCC)
#
# (continue BCC)
#label('.cond2')
#nop() #13
#nop() #14
nop() #15
label('.cond3')
bra(d(lo('.cond1'))) #16
ld(busRAM|eaYXregAC) #17 Operand is the conditional
label('.cond4')
ldzp(d(vPC)); C('False condition')#22
bra(d(lo('.cond6'))) #23
adda(val(1)) #24
label('.cond5')
st(eaYXregOUTIX); C('True condition')#23 Just X++
ld(busRAM|eaYXregAC) #24
label('.cond6')
st(d(vPC)) #25
bra(d(lo('NEXT'))) #26
ld(val(-28/2)) #27
# Conditional GT: Branch if positive (if(ALC>0)PCL=D)
label('GT')
ble(d(lo('.cond4'))) #20
bgt(d(lo('.cond5'))) #21
ld(busRAM|eaYXregAC) #22
# Conditional LT: Branch if negative (if(ALC<0)PCL=D), 16 cycles
label('LT')
bge(d(lo('.cond4'))) #20
blt(d(lo('.cond5'))) #21
ld(busRAM|eaYXregAC) #22
# Conditional GE: Branch if positive or zero (if(ALC>=0)PCL=D)
label('GE')
blt(d(lo('.cond4'))) #20
bge(d(lo('.cond5'))) #21
ld(busRAM|eaYXregAC) #22
# Conditional LE: Branch if negative or zero (if(ALC<=0)PCL=D)
label('LE')
bgt(d(lo('.cond4'))) #20
ble(d(lo('.cond5'))) #21
ld(busRAM|eaYXregAC) #22
# Instruction LDI: Load immediate constant (AC=$DD), 16 cycles
label('LDI')
st(d(vAC)) #10
ld(val(0)) #11
st(d(vAC+1)) #12
ld(val(-16/2)) #13
bra(d(lo('NEXT'))) #14
#nop() #(15)
#
# Instruction ST: Store in zero page ([D]=ACL), 16 cycles
label('ST')
ld(busAC,regX) #10,15 (overlap with LDI)
ldzp(d(vAC)) #11
st(d(vAC),busAC|ea0XregAC) #12
ld(val(-16/2)) #13
bra(d(lo('NEXT'))) #14
#nop() #(15)
#
# Instruction POP: (LR=[SP++]), 26 cycles
label('POP')
ld(d(vSP),busRAM|regX) #10,15 (overlap with ST)
ld(busRAM,ea0XregAC) #11
st(d(vLR)) #12
ldzp(d(vSP)) #13
adda(val(1),regX) #14
ld(busRAM,ea0XregAC) #15
st(d(vLR+1)) #16
ldzp(d(vSP)) #17
adda(val(2)) #18
st(d(vSP)) #19
label('next1')
ldzp(d(vPC)) #20
suba(val(1)) #21
st(d(vPC)) #22
ld(val(-26/2)) #23
bra(d(lo('NEXT'))) #24
#nop() #(25)
#
# Conditional NE: Branch if not zero (if(ALC!=0)PCL=D)
label('NE')
beq(d(lo('.cond4'))) #20,25 (overlap with POP)
bne(d(lo('.cond5'))) #21
ld(busRAM|eaYXregAC) #22
# Instruction PUSH: ([--SP]=LR), 26 cycles
label('PUSH')
ldzp(d(vSP)) #10
suba(d(1),regX) #11
ldzp(d(vLR+1)) #12
st(ea0XregAC) #13
ldzp(d(vSP)) #14
suba(val(2)) #15
st(d(vSP),busAC|regX) #16
ldzp(d(vLR)) #17
bra(d(lo('next1'))) #18
st(ea0XregAC) #19
# Instruction LUP: ROM lookup (AC=ROM[AC+256*D]), 26 cycles
label('LUP')
ld(d(vAC+1),busRAM|regY) #10
jmpy(d(251)); C('Trampoline offset')#11
adda(d(vAC),busRAM) #12
# Instruction ANDI: Logical-AND with constant (AC&=D), 16 cycles
label('ANDI')
anda(d(vAC),busRAM) #10
st(d(vAC)) #11
ld(val(0)) #12 Clear high byte
st(d(vAC+1)) #13
bra(d(lo('NEXT'))) #14
ld(val(-16/2)) #15
# Instruction ORI: Logical-OR with constant (AC|=D), 14 cycles
label('ORI')
ora(d(vAC),busRAM) #10
st(d(vAC)) #11
bra(d(lo('NEXT'))) #12
ld(val(-14/2)) #13
# Instruction XORI: Logical-XOR with constant (AC^=D), 14 cycles
label('XORI')
xora(d(vAC),busRAM) #10
st(d(vAC)) #11
bra(d(lo('NEXT'))) #12
ld(val(-14/2)) #13
# Instruction BRA: Branch unconditionally (PCL=D), 14 cycles
label('BRA')
st(d(vPC)) #10
ld(val(-14/2)) #11
bra(d(lo('NEXT'))) #12
#nop() #(13)
#
# Instruction INC: Increment zero page byte ([D]++), 16 cycles
label('INC')
ld(busAC,regX) #10,13 (overlap with BRA)
ld(busRAM,ea0XregAC) #11
adda(val(1)) #12
st(ea0XregAC) #13
bra(d(lo('NEXT'))) #14
ld(val(-16/2)) #15
# Instruction ADDW: Word addition with zero page (AC+=[D]+256*[D+1]), 28 cycles
label('ADDW')
# The non-carry paths could be 26 cycles at the expense of (much) more code.
# But a smaller size is better so more instructions fit in this code page.
# 28 cycles is still 4.5 usec. The 6502 equivalent takes 20 cycles or 20 usec.
ld(busAC,regX) #10 Address of low byte to be added
adda(val(1)) #11
st(d(vTmp)) #12 Address of high byte to be added
ldzp(d(vAC)) #13 Add the low bytes
adda(busRAM|ea0XregAC) #14
st(d(vAC)) #15 Store low result
bmi(d(lo('.addw0'))) #16 Now figure out if there was a carry
suba(busRAM|ea0XregAC) #17 Gets back the initial value of vAC
bra(d(lo('.addw1'))) #18
ora(busRAM|ea0XregAC) #19 Bit 7 is our lost carry
label('.addw0')
anda(busRAM|ea0XregAC) #18 Bit 7 is our lost carry
nop() #19
label('.addw1')
anda(val(0x80),regX) #20 Move the carry to bit 0 (0 or +1)
ld(busRAM,ea0XregAC) #21
adda(d(vAC+1),busRAM) #22 Add the high bytes with carry
ld(d(vTmp),busRAM|regX) #23
adda(busRAM|ea0XregAC) #24
st(d(vAC+1)) #25 Store high result
bra(d(lo('NEXT'))) #26
ld(val(-28/2)) #27
# Instruction PEEK: (AC=[AC]), 26 cycles
label('PEEK')
ld(val(hi('peek')),regY) #10
jmpy(d(lo('peek'))) #11
#ldzp(d(vPC)) #12
#
# Instruction SYS: Native call, <=256 cycles (<=128 ticks, in reality less)
#
# The 'SYS' vCPU instruction first checks the number of desired ticks given by
# the operand. As long as there are insufficient ticks available in the current
# time slice, the instruction will be retried. This will effectively wait for
# the next scan line if the current slice is almost out of time. Then a jump to
# native code is made. This code can do whatever it wants, but it must return
# to the 'REENTER' label when done. When returning, AC must hold (the negative
# of) the actual consumed number of whole ticks for the entire virtual
# instruction cycle (from NEXT to NEXT). This duration may not exceed the prior
# declared duration in the operand + 28 (or maxTicks). The operand specifies the
# (negative) of the maximum number of *extra* ticks that the native call will
# need. The GCL compiler automatically makes this calculation from gross number
# of cycles to excess number of ticks.
# SYS functions can modify vPC to implement repetition. For example to split
# up work into multiple chucks.
label('retry')
ldzp(d(vPC)); C('Retry until sufficient time')#13,12 (overlap with PEEK)
suba(val(2)) #14
st(d(vPC)) #15
bra(d(lo('REENTER'))) #16
ld(val(-20/2)) #17
label('SYS')
adda(d(vTicks),busRAM) #10
blt(d(lo('retry'))) #11
ld(d(sysFn+1),busRAM|regY) #12
jmpy(d(sysFn)|busRAM) #13
#nop() #(14)
#
# Instruction SUBW: Word subtract with zero page (AC-=[D]+256*[D+1]), 28 cycles
# All cases can be done in 26 cycles, but the code will become much larger
label('SUBW')
ld(busAC,regX) #10,14 (overlap with SYS) Address of low byte to be subtracted
adda(val(1)) #11
st(d(vTmp)) #12 Address of high byte to be subtracted
ldzp(d(vAC)) #13
bmi(d(lo('.subw0'))) #14
suba(busRAM|ea0XregAC) #15
st(d(vAC)) #16 Store low result
bra(d(lo('.subw1'))) #17
ora(busRAM|ea0XregAC) #18 Bit 7 is our lost carry
label('.subw0')
st(d(vAC)) #16 Store low result
anda(busRAM|ea0XregAC) #17 Bit 7 is our lost carry
nop() #18
label('.subw1')
anda(val(0x80),regX) #19 Move the carry to bit 0
ldzp(d(vAC+1)) #20
suba(busRAM,ea0XregAC) #21
ld(d(vTmp),busRAM|regX) #22
suba(busRAM|ea0XregAC) #23
st(d(vAC+1)) #24
ld(val(-28/2)) #25
label('REENTER')
bra(d(lo('NEXT'))); C('Return from SYS calls')#26
ld(d(vPC+1),busRAM|regY) #27
# Instruction DEF: Define data or code (AC,PCL=PC+2,D), 18 cycles
label('DEF')
ld(val(hi('def')),regY) #10
jmpy(d(lo('def'))) #11
#st(d(vTmp)) #12
#
# Instruction CALL: (LR=PC+2,PC=[D]-2), 26 cycles
label('CALL')
st(d(vTmp)) #10,12 (overlap with DEF)
ldzp(d(vPC)) #11
adda(val(2)); C('Point to instruction after CALL')#12
st(d(vLR)) #13
ldzp(d(vPC+1)) #14
st(d(vLR+1)) #15
ld(d(vTmp),busRAM|regX) #16
ld(busRAM|ea0XregAC) #17
suba(val(2)); C('Because NEXT will add 2')#18
st(d(vPC)) #19
ldzp(d(vTmp)) #20
adda(val(1),regX) #21
ld(busRAM|ea0XregAC) #22
st(d(vPC+1),busAC|regY) #23
bra(d(lo('NEXT'))) #24
ld(val(-26/2)) #25
# ALLOCA implementation
# Instruction ALLOCA: (SP+=D), 14 cycles
label('ALLOC')
adda(d(vSP),busRAM) #10
st(d(vSP)) #11
bra(d(lo('NEXT'))) #12
ld(val(-14/2)) #13
# The instructions below are all implemented in the second code page. Jumping
# back and forth makes each 6 cycles slower, but it also saves space in the
# primary page for the instructions above. Most of them are in fact not very
# critical, as evidenced by the fact that they weren't needed for the first
# Gigatron applications (Snake, Racer, Mandelbrot, Loader). By providing them
# in this way, at least they don't need to be implemented as a SYS extension.
# Instruction ADDI: Add small positive constant (AC+=D), 28 cycles
label('ADDI')
ld(val(hi('addi')),regY) #10
jmpy(d(lo('addi'))) #11
st(d(vTmp)) #12
# Instruction SUBI: Subtract small positive constant (AC+=D), 28 cycles
label('SUBI')
ld(val(hi('subi')),regY) #10
jmpy(d(lo('subi'))) #11
st(d(vTmp)) #12
# Instruction LSLW: Logical shift left (AC<<=1), 28 cycles
# Useful, because ADDW can't add vAC to itself. Also more compact.
label('LSLW')
ld(val(hi('lslw')),regY) #10
jmpy(d(lo('lslw'))) #11
ldzp(d(vAC)) #12
# Instruction STLW: Store on stack (), 26 cycles
label('STLW')
ld(val(hi('stlw')),regY) #10
jmpy(d(lo('stlw'))) #11
#nop() #12
#
# Instruction LDLW: Load from stack (), 26 cycles
label('LDLW')
ld(val(hi('ldlw')),regY) #10,12 (overlap with STLW)
jmpy(d(lo('ldlw'))) #11
#nop() #12
#
# Instruction POKE: ([[D+1],[D]]=ACL), 28 cycles
label('POKE')
ld(val(hi('poke')),regY) #10,12 (overlap with LDLW)
jmpy(d(lo('poke'))) #11
st(d(vTmp)) #12
# Instruction DOKE: (), 28 cycles
label('DOKE')
ld(val(hi('doke')),regY) #10
jmpy(d(lo('doke'))) #11
st(d(vTmp)) #12
# Instruction DEEK: (), 28 cycles
label('DEEK')
ld(val(hi('deek')),regY) #10
jmpy(d(lo('deek'))) #11
#nop() #12
#
# Instruction ANDW: (AC&=[D]+256*[D+1]), 28 cycles
label('ANDW')
ld(val(hi('andw')),regY) #10,12 (overlap with DEEK)
jmpy(d(lo('andw'))) #11
#nop() #12
#
# Instruction ORW: (AC|=[D]+256*[D+1]), 28 cycles
label('ORW')
ld(val(hi('orw')),regY) #10,12 (overlap with ANDW)
jmpy(d(lo('orw'))) #11
#nop() #12
#
# Instruction XORW: (AC^=[D]+256*[D+1]), 26 cycles
label('XORW')
ld(val(hi('xorw')),regY) #10,12 (overlap with ORW)
jmpy(d(lo('xorw'))) #11
st(d(vTmp)) #12
# We keep XORW 2 cycles faster than ANDW/ORW, because that
# can be useful for comparing numbers for equality a tiny
# bit faster than with SUBW
# Instruction RET: Function return (PC=LR-2), 16 cycles
label('RET')
ldzp(d(vLR)) #10
assert(pc()&255 == 0)
#-----------------------------------------------------------------------
#
# ROM page 4: Application interpreter extension
#
#-----------------------------------------------------------------------
align(0x100, 0x100)
# (Continue RET)
suba(val(2)) #11
st(d(vPC)) #12
ldzp(d(vLR+1)) #13
st(d(vPC+1)) #14
ld(val(hi('REENTER')),regY) #15
jmpy(d(lo('REENTER'))) #16
ld(val(-20/2)) #17
# DEF implementation
label('def')
ldzp(d(vPC)) #13
adda(val(2)) #14
st(d(vAC)) #15
ldzp(d(vPC+1)) #16
st(d(vAC+1)) #17
ldzp(d(vTmp)) #18
st(d(vPC)) #19
ld(val(hi('REENTER')),regY) #20
ld(val(-26/2)) #21
jmpy(d(lo('REENTER'))) #22
nop() #23
# ADDI implementation
label('addi')
adda(d(vAC),busRAM) #13
st(d(vAC)) #14 Store low result
bmi(d(lo('.addi0'))) #15 Now figure out if there was a carry
suba(d(vTmp),busRAM) #16 Gets back the initial value of vAC
bra(d(lo('.addi1'))) #17
ora(d(vTmp),busRAM) #18 Bit 7 is our lost carry
label('.addi0')
anda(d(vTmp),busRAM) #17 Bit 7 is our lost carry
nop() #18
label('.addi1')
anda(val(0x80),regX) #19 Move the carry to bit 0 (0 or +1)
ld(busRAM,ea0XregAC) #20
adda(d(vAC+1),busRAM) #21 Add the high bytes with carry
st(d(vAC+1)) #22 Store high result
ld(val(hi('REENTER')),regY) #23
jmpy(d(lo('REENTER'))) #24
ld(val(-28/2)) #25
# SUBI implementation
label('subi')
ldzp(d(vAC)) #13
bmi(d(lo('.subi0'))) #14
suba(d(vTmp),busRAM) #15
st(d(vAC)) #16 Store low result
bra(d(lo('.subi1'))) #17
ora(d(vTmp),busRAM) #18 Bit 7 is our lost carry
label('.subi0')
st(d(vAC)) #16 Store low result
anda(d(vTmp),busRAM) #17 Bit 7 is our lost carry
nop() #18
label('.subi1')
anda(val(0x80),regX) #19 Move the carry to bit 0
ldzp(d(vAC+1)) #20
suba(busRAM,ea0XregAC) #21
st(d(vAC+1)) #22
ld(val(hi('REENTER')),regY) #23
jmpy(d(lo('REENTER'))) #24
ld(val(-28/2)) #25
# LSLW implementation
label('lslw')
anda(d(128),regX) #13
adda(d(vAC),busRAM) #14
st(d(vAC)) #15
ld(ea0XregAC,busRAM) #16
adda(d(vAC+1),busRAM) #17
adda(d(vAC+1),busRAM) #18
st(d(vAC+1)) #19
ldzp(d(vPC)) #20
suba(d(1)) #21
st(d(vPC)) #22
ld(val(hi('REENTER')),regY) #23
jmpy(d(lo('REENTER'))) #24
ld(val(-28/2)) #25
# STLW implementation
label('stlw')
adda(d(vSP),busRAM) #13
st(d(vTmp)) #14
adda(d(1),regX) #15
ldzp(d(vAC+1)) #16
st(ea0XregAC) #17
ld(d(vTmp),busRAM|regX) #18
ldzp(d(vAC)) #19
st(ea0XregAC) #20
ld(val(hi('REENTER')),regY) #21
jmpy(d(lo('REENTER'))) #22
ld(val(-26/2)) #23
# LDLW implementation
label('ldlw')
adda(d(vSP),busRAM) #13
st(d(vTmp)) #14
adda(d(1),regX) #15
ld(ea0XregAC,busRAM) #16
st(d(vAC+1)) #17
ld(d(vTmp),busRAM|regX) #18
ld(ea0XregAC,busRAM) #19
st(d(vAC)) #20
ld(val(hi('REENTER')),regY) #21
jmpy(d(lo('REENTER'))) #22
ld(val(-26/2)) #23
# POKE implementation
label('poke')
adda(d(1),regX) #13
ld(busRAM,ea0XregAC) #14
ld(busAC,regY) #15
ld(d(vTmp),busRAM|regX) #16
ld(busRAM,ea0XregAC) #17
ld(busAC,regX) #18
ldzp(d(vAC)) #19
st(eaYXregAC) #20
ld(val(hi('REENTER')),regY) #21
jmpy(d(lo('REENTER'))) #22
ld(val(-26/2)) #23
# PEEK implementation
label('peek')
suba(val(1)) #13
st(d(vPC)) #14
ld(d(vAC),busRAM|regX) #15
ld(d(vAC+1),busRAM|regY) #16
ld(busRAM|eaYXregAC) #17
st(d(vAC)) #18
label('lupReturn') #Nice coincidence that lupReturn can be here
ld(val(0)) #19
st(d(vAC+1)) #20
ld(val(hi('REENTER')),regY) #21
jmpy(d(lo('REENTER'))) #22
ld(val(-26/2)) #23
#
# DOKE implementation
label('doke')
adda(d(1),regX) #13,25 (overlap with peek)
ld(busRAM,ea0XregAC) #14
ld(busAC,regY) #15
ld(d(vTmp),busRAM|regX) #16
ld(busRAM,ea0XregAC) #17
ld(busAC,regX) #18
ldzp(d(vAC)) #19
st(eaYXregOUTIX) #20
ldzp(d(vAC+1)) #21
st(eaYXregAC) #22
ld(val(hi('REENTER')),regY) #23
jmpy(d(lo('REENTER'))) #24
ld(val(-28/2)) #25
# DEEK implementation
label('deek')
ldzp(d(vPC)) #13
suba(val(1)) #14
st(d(vPC)) #15
ld(d(vAC),busRAM|regX) #16
ld(d(vAC+1),busRAM|regY) #17
ld(busRAM|eaYXregAC) #18
st(eaYXregOUTIX) #19
st(d(vAC)) #20
ld(busRAM|eaYXregAC) #21
st(d(vAC+1)) #22
ld(val(hi('REENTER')),regY) #23
jmpy(d(lo('REENTER'))) #24
ld(val(-28/2)) #25
# ANDW implementation
label('andw')
st(d(vTmp)) #13
adda(d(1),regX) #14
ld(busRAM|ea0XregAC) #15
anda(d(vAC+1),busRAM) #16
st(d(vAC+1)) #17
ld(d(vTmp),busRAM|regX) #18
ld(busRAM|ea0XregAC) #19
anda(d(vAC),busRAM) #20
st(d(vAC)) #21
ld(val(-28/2)) #22
ld(val(hi('REENTER')),regY) #23
jmpy(d(lo('REENTER'))) #24
#nop() #(25)
# ORW implementation
label('orw')
st(d(vTmp)) #13,25 (overlap with andw)
adda(d(1),regX) #14
ld(busRAM|ea0XregAC) #15
ora(d(vAC+1),busRAM) #16
st(d(vAC+1)) #17
ld(d(vTmp),busRAM|regX) #18
ld(busRAM|ea0XregAC) #19
ora(d(vAC),busRAM) #20
st(d(vAC)) #21
ld(val(-28/2)) #22
ld(val(hi('REENTER')),regY) #23
jmpy(d(lo('REENTER'))) #24
#nop() #(25)
# XORW implementation
label('xorw')
adda(d(1),regX) #13,25 (overlap with orw)
ld(busRAM|ea0XregAC) #14
xora(d(vAC+1),busRAM) #15
st(d(vAC+1)) #16
ld(d(vTmp),busRAM|regX) #17
ld(busRAM|ea0XregAC) #18
xora(d(vAC),busRAM) #19
st(d(vAC)) #20
ld(val(hi('REENTER')),regY) #21
jmpy(d(lo('REENTER'))) #22
ld(val(-26/2)) #23
#-----------------------------------------------------------------------
#
# vCPU extension functions (for acceleration and compaction) follow below.
#
# The naming convention is: SYS_<CamelCase>_<N>
#
# With <N> the maximum number of cycles the function will run
# (counted from NEXT to NEXT). This is the same number that must
# be passed to the 'SYS' vCPU instruction as operand, and it will
# appear in the GCL code upon use.
#
#-----------------------------------------------------------------------
#-----------------------------------------------------------------------
# Extension SYS_Random_34: Update entropy and copy to vAC
#-----------------------------------------------------------------------
# This same algorithm runs automatically once per vertical blank.
# Use this function to get numbers at a higher rate.
label('SYS_Random_34')
ldzp(d(frameCount)) #15
xora(d(entropy+1),busRAM) #16
xora(d(serialRaw),busRAM) #17
adda(d(entropy+0),busRAM) #18
st(d(entropy+0)) #19
st(d(vAC+0)) #20
adda(d(entropy+2),busRAM) #21
st(d(entropy+2)) #22
bmi(d(lo('.sysRnd0'))) #23
bra(d(lo('.sysRnd1'))) #24
xora(val(64+16+2+1)) #25
label('.sysRnd0')
xora(val(64+32+8+4)) #25
label('.sysRnd1')
adda(d(entropy+1),busRAM) #26
st(d(entropy+1)) #27
st(d(vAC+1)) #28
ld(val(hi('REENTER')),regY) #29
jmpy(d(lo('REENTER'))) #30
ld(val(-34/2)) #31
label('SYS_LSRW7_30')
ldzp(d(vAC)) #15
anda(d(128),regX) #16
ldzp(d(vAC+1)) #17
adda(busAC) #18
ora(ea0XregAC,busRAM) #19
st(d(vAC)) #20
ldzp(d(vAC+1)) #21
anda(d(128),regX) #22
ld(ea0XregAC,busRAM) #23
st(d(vAC+1)) #24
ld(d(hi('REENTER')),regY) #25
jmpy(d(lo('REENTER'))) #26
ld(d(-30/2)) #27
label('SYS_LSRW8_24')
ldzp(d(vAC+1)) #15
st(d(vAC)) #16
ld(d(0)) #17
st(d(vAC+1)) #18
ld(d(hi('REENTER')),regY) #19
jmpy(d(lo('REENTER'))) #20
ld(d(-24/2)) #21
label('SYS_LSLW8_24')
ldzp(d(vAC)) #15
st(d(vAC+1)) #16
ld(d(0)) #17
st(d(vAC)) #18
ld(d(hi('REENTER')),regY) #19
jmpy(d(lo('REENTER'))) #20
ld(d(-24/2)) #21
#-----------------------------------------------------------------------
# Extension SYS_Draw4_30:
#-----------------------------------------------------------------------
# sysArgs[0:3] Pixels
# sysArgs[4:5] Position on screen
label('SYS_Draw4_30')
ld(d(sysArgs+4),busRAM|regX) #15
ld(d(sysArgs+5),busRAM|regY) #16
ldzp(d(sysArgs+0)) #17
st(eaYXregOUTIX) #18
ldzp(d(sysArgs+1)) #19
st(eaYXregOUTIX) #20
ldzp(d(sysArgs+2)) #21
st(eaYXregOUTIX) #22
ldzp(d(sysArgs+3)) #23
st(eaYXregOUTIX) #24
ld(val(hi('REENTER')),regY) #25
jmpy(d(lo('REENTER'))) #26
ld(val(-30/2)) #27
#-----------------------------------------------------------------------
# Extension SYS_VDrawBits_134:
#-----------------------------------------------------------------------
# Draw slice of a character
# sysArgs[0] Color 0 (background)
# sysArgs[1] Color 1 (pen)
# sysArgs[2] 8 bits, highest bit first (destructive)
# sysArgs[4:5] Position on screen
label('SYS_VDrawBits_134')
ld(d(sysArgs+4),busRAM|regX) #15
ld(val(0)) #16
label('.vdb0')
st(d(vTmp)) #17+i*14
adda(d(sysArgs+5),busRAM|regY) #18+i*14 Y=[sysPos+1]+vTmp
ldzp(d(sysArgs+2)) #19+i*14 Select color
bmi(d(lo('.vdb1'))) #20+i*14
bra(d(lo('.vdb2'))) #21+i*14
ldzp(d(sysArgs+0)) #22+i*14
label('.vdb1')
ldzp(d(sysArgs+1)) #22+i*14
label('.vdb2')
st(eaYXregAC) #23+i*14 Draw pixel
ldzp(d(sysArgs+2)) #24+i*14 Shift byte left
adda(busAC) #25+i*14
st(d(sysArgs+2)) #26+i*14
ldzp(d(vTmp)) #27+i*14 Loop counter
suba(val(7)) #28+i*14
bne(d(lo('.vdb0'))) #29+i*14
adda(val(8)) #30+i*14
ld(val(hi('REENTER')),regY) #129
jmpy(d(lo('REENTER'))) #130
ld(val(-134/2)) #131
#-----------------------------------------------------------------------
# ROM page 5-6: Shift table and code
#-----------------------------------------------------------------------
# Lookup table for i>>n, with n in 1..6
# Indexing ix = i & ~b | (b-1), where b = 1<<(n-1)
# ...
# lda <.ret
# st [vTmp]
# ld >shiftTable,y
# <calculate ix>
# jmp y,ac
# bra $ff
# .ret: ...
#
# i >> 7 can be always be done with RAM: [i&128]
# ...
# anda $80,x
# ld [x]
# ...
align(0x100, 0x200)
label('shiftTable')
shiftTable = pc()
for ix in range(255):
for n in range(1,7): # Find first zero
if ~ix & (1 << (n-1)):
break
pattern = ['x' if i<n else '1' if ix&(1<<i) else '0' for i in range(8)]
ld(val(ix>>n)); C('0b%s >> %d' % (''.join(reversed(pattern)), n))
assert(pc()&255 == 255)
bra(d(vTmp)|busRAM); C('Jumps back into next page')
label('SYS_LSRW1_48')
assert(pc()&255 == 0)#First instruction on this page must be a nop
nop() #15
ld(d(hi('shiftTable')),regY); C('Logical shift right 1 bit (X >> 1)')#16
ld(d(lo('.sysLsrw1a'))); C('Shift low byte')#17
st(d(vTmp)) #18
ldzp(d(vAC)) #19
anda(d(0b11111110)) #20
jmpy(busAC) #21
bra(d(255)); C('Actually: bra $%04x' % (shiftTable+255))#22
label('.sysLsrw1a')
st(d(vAC)) #26
ldzp(d(vAC+1)); C('Transfer bit 8')#27
anda(d(1)) #28
adda(d(127)) #29
anda(d(128)) #30
ora(d(vAC)|busRAM) #31
st(d(vAC)) #32
ld(d(lo('.sysLsrw1b'))); C('Shift high byte')#33
st(d(vTmp)) #34
ldzp(d(vAC+1)) #35
anda(d(0b11111110)) #36
jmpy(busAC) #37
bra(d(255)); C('Actually: bra $%04x' % (shiftTable+255))#38
label('.sysLsrw1b')
st(d(vAC+1)) #42
ld(d(hi('REENTER')),regY) #43
jmpy(d(lo('REENTER'))) #44
ld(d(-48/2)) #45
label('SYS_LSRW2_52')
ld(d(hi('shiftTable')),regY); C('Logical shift right 2 bit (X >> 2)')#15
ld(d(lo('.sysLsrw2a'))); C('Shift low byte')#16
st(d(vTmp)) #17
ldzp(d(vAC)) #18
anda(d(0b11111100)) #19
ora( d(0b00000001)) #20
jmpy(busAC) #21
bra(d(255)); C('Actually: bra $%04x' % (shiftTable+255))#22
label('.sysLsrw2a')
st(d(vAC)) #26
ldzp(d(vAC+1)); C('Transfer bit 8:9')#27
adda(busAC) #28
adda(busAC) #29
adda(busAC) #30
adda(busAC) #31
adda(busAC) #32
adda(busAC) #33
ora(d(vAC)|busRAM) #34
st(d(vAC)) #35
ld(d(lo('.sysLsrw2b'))); C('Shift high byte')#36
st(d(vTmp)) #37
ldzp(d(vAC+1)) #38
anda(d(0b11111100)) #39
ora( d(0b00000001)) #40
jmpy(busAC) #41
bra(d(255)); C('Actually: bra $%04x' % (shiftTable+255))#42
label('.sysLsrw2b')
st(d(vAC+1)) #46
ld(d(hi('REENTER')),regY) #47
jmpy(d(lo('REENTER'))) #48
ld(d(-52/2)) #49
label('SYS_LSRW3_52')
ld(d(hi('shiftTable')),regY); C('Logical shift right 3 bit (X >> 3)')#15
ld(d(lo('.sysLsrw3a'))); C('Shift low byte')#16
st(d(vTmp)) #17
ldzp(d(vAC)) #18
anda(d(0b11111000)) #19
ora( d(0b00000011)) #20
jmpy(busAC) #21
bra(d(255)); C('Actually: bra $%04x' % (shiftTable+255))#22
label('.sysLsrw3a')
st(d(vAC)) #26
ldzp(d(vAC+1)); C('Transfer bit 8:10')#27
adda(busAC) #28
adda(busAC) #29
adda(busAC) #30
adda(busAC) #31
adda(busAC) #32
ora(d(vAC)|busRAM) #33
st(d(vAC)) #34
ld(d(lo('.sysLsrw3b'))); C('Shift high byte')#35
st(d(vTmp)) #36
ldzp(d(vAC+1)) #37
anda(d(0b11111000)) #38
ora( d(0b00000011)) #39
jmpy(busAC) #40
bra(d(255)); C('Actually: bra $%04x' % (shiftTable+255))#41
label('.sysLsrw3b')
st(d(vAC+1)) #45
ld(d(-52/2)) #46
ld(d(hi('REENTER')),regY) #47
jmpy(d(lo('REENTER'))) #48
#nop() #49
label('SYS_LSRW4_50')
ld(d(hi('shiftTable')),regY); C('Logical shift right 4 bit (X >> 4)')#15,49
ld(d(lo('.sysLsrw4a'))); C('Shift low byte')#16
st(d(vTmp)) #17
ldzp(d(vAC)) #18
anda(d(0b11110000)) #19
ora( d(0b00000111)) #20
jmpy(busAC) #21
bra(d(255)); C('Actually: bra $%04x' % (shiftTable+255))#22
label('.sysLsrw4a')
st(d(vAC)) #26
ldzp(d(vAC+1)); C('Transfer bit 8:11')#27
adda(busAC) #28
adda(busAC) #29
adda(busAC) #30
adda(busAC) #31
ora(d(vAC)|busRAM) #32
st(d(vAC)) #33
ld(d(lo('.sysLsrw4b'))); C('Shift high byte')#34
st(d(vTmp)) #35
ldzp(d(vAC+1)) #36
anda(d(0b11110000)) #37
ora( d(0b00000111)) #38
jmpy(busAC) #39
bra(d(255)); C('Actually: bra $%04x' % (shiftTable+255))#40
label('.sysLsrw4b')
st(d(vAC+1)) #44
ld(d(hi('REENTER')),regY) #45
jmpy(d(lo('REENTER'))) #46
ld(d(-50/2)) #47
label('SYS_LSRW5_50')
ld(d(hi('shiftTable')),regY); C('Logical shift right 5 bit (X >> 5)')#15
ld(d(lo('.sysLsrw5a'))); C('Shift low byte')#16
st(d(vTmp)) #17
ldzp(d(vAC)) #18
anda(d(0b11100000)) #19
ora( d(0b00001111)) #20
jmpy(busAC) #21
bra(d(255)); C('Actually: bra $%04x' % (shiftTable+255))#22
label('.sysLsrw5a')
st(d(vAC)) #26
ldzp(d(vAC+1)); C('Transfer bit 8:13')#27
adda(busAC) #28
adda(busAC) #29
adda(busAC) #30
ora(d(vAC)|busRAM) #31
st(d(vAC)) #32
ld(d(lo('.sysLsrw5b'))); C('Shift high byte')#33
st(d(vTmp)) #34
ldzp(d(vAC+1)) #35
anda(d(0b11100000)) #36
ora( d(0b00001111)) #37
jmpy(busAC) #38
bra(d(255)); C('Actually: bra $%04x' % (shiftTable+255))#39
label('.sysLsrw5b')
st(d(vAC+1)) #44
ld(d(-50/2)) #45
ld(d(hi('REENTER')),regY) #46
jmpy(d(lo('REENTER'))) #47
#nop() #48
label('SYS_LSRW6_48')
ld(d(hi('shiftTable')),regY); C('Logical shift right 6 bit (X >> 6)')#15,44
ld(d(lo('.sysLsrw6a'))); C('Shift low byte')#16
st(d(vTmp)) #17
ldzp(d(vAC)) #18
anda(d(0b11000000)) #19
ora( d(0b00011111)) #20
jmpy(busAC) #21
bra(d(255)); C('Actually: bra $%04x' % (shiftTable+255))#22
label('.sysLsrw6a')
st(d(vAC)) #26
ldzp(d(vAC+1)); C('Transfer bit 8:13')#27
adda(busAC) #28
adda(busAC) #29
ora(d(vAC)|busRAM) #30
st(d(vAC)) #31
ld(d(lo('.sysLsrw6b'))); C('Shift high byte')#32
st(d(vTmp)) #33
ldzp(d(vAC+1)) #34
anda(d(0b11000000)) #35
ora( d(0b00011111)) #36
jmpy(busAC) #37
bra(d(255)); C('Actually: bra $%04x' % (shiftTable+255))#38
label('.sysLsrw6b')
st(d(vAC+1)) #42
ld(d(hi('REENTER')),regY) #43
jmpy(d(lo('REENTER'))) #44
ld(d(-48/2)) #45
label('SYS_LSLW4_46')
ld(d(hi('shiftTable')),regY); C('Logical shift left 4 bit (X << 4)')#15
ld(d(lo('.sysLsrl4'))) #16
st(d(vTmp)) #17
ldzp(d(vAC+1)) #18
adda(busAC) #19
adda(busAC) #20
adda(busAC) #21
adda(busAC) #22
st(d(vAC+1)) #23
ldzp(d(vAC)) #24
anda(d(0b11110000)) #25
ora( d(0b00000111)) #26
jmpy(busAC) #27
bra(d(255)); C('Actually: bra $%04x' % (shiftTable+255))#28
label('.sysLsrl4')
ora(d(vAC+1),busRAM) #32
st(d(vAC+1)) #33
ldzp(d(vAC)) #34
adda(busAC) #35
adda(busAC) #36
adda(busAC) #37
adda(busAC) #38
st(d(vAC)) #39
ld(d(-46/2)) #40
ld(d(hi('REENTER')),regY) #41
jmpy(d(lo('REENTER'))) #42
#nop() #43
#-----------------------------------------------------------------------
# Extension SYS_Read3_40: Read 3 consecutive bytes from ROM
#-----------------------------------------------------------------------
# sysArgs[0:2] Bytes (output)
# sysArgs[6:7] ROM pointer (input)
label('SYS_Read3_40')
ld(d(sysArgs+7),busRAM|regY) #15,32
jmpy(d(128-7)) #16 trampoline3a
ldzp(d(sysArgs+6)) #17
label('txReturn')
st(d(sysArgs+2)) #34
ld(val(hi('REENTER')),regY) #35
jmpy(d(lo('REENTER'))) #36
ld(val(-40/2)) #37
def trampoline3a():
"""Read 3 bytes from ROM page"""
while pc()&255 < 128-7:
nop()
bra(busAC) #18
C('Trampoline for page $%02x00 reading (entry)' % (pc()>>8))
bra(d(123)) #19
st(d(sysArgs+0)) #21
ldzp(d(sysArgs+6)) #22
adda(val(1)) #23
bra(busAC) #24
bra(d(250)) #25 trampoline3b
def trampoline3b():
"""Read 3 bytes from ROM page (continue)"""
while pc()&255 < 256-6:
nop()
st(d(sysArgs+1)) #27
C('Trampoline for page $%02x00 reading (continue)' % (pc()>>8))
ldzp(d(sysArgs+6)) #28
adda(val(2)) #29
ld(d(hi('txReturn')),regY) #30
bra(busAC) #31
jmpy(d(lo('txReturn'))) #32
#-----------------------------------------------------------------------
# Extension SYS_Unpack_56: Unpack 3 bytes into 4 pixels
#-----------------------------------------------------------------------
# sysArgs[0:2] Packed bytes (input)
# sysArgs[0:3] Pixels (output)
label('SYS_Unpack_56')
ld(val(soundTable>>8),regY) #15
ldzp(d(sysArgs+2)) #16 a[2]>>2
ora(val(0x03),regX) #17
ld(eaYXregAC|busRAM) #18
st(d(sysArgs+3)); C('-> Pixel 3')#19
ldzp(d(sysArgs+2)) #20 (a[2]&3)<<4
anda(val(0x03)) #21
adda(busAC) #22
adda(busAC) #23
adda(busAC) #24
adda(busAC) #25
st(d(sysArgs+2)); #26
ldzp(d(sysArgs+1)) #27 | a[1]>>4
ora(val(0x03),regX) #28
ld(eaYXregAC|busRAM) #29
ora(val(0x03),regX) #30
ld(eaYXregAC|busRAM) #31
ora(d(sysArgs+2),busRAM) #32
st(d(sysArgs+2)); C('-> Pixel 2')#33
ldzp(d(sysArgs+1)) #34 (a[1]&15)<<2
anda(val(0x0f)) #35
adda(busAC) #36
adda(busAC) #37
st(d(sysArgs+1)) #38
ldzp(d(sysArgs+0)) #39 | a[0]>>6
ora(val(0x03),regX) #40
ld(eaYXregAC|busRAM) #41
ora(val(0x03),regX) #42
ld(eaYXregAC|busRAM) #43
ora(val(0x03),regX) #44
ld(eaYXregAC|busRAM) #45
ora(d(sysArgs+1),busRAM) #46
st(d(sysArgs+1)); C('-> Pixel 1')#47
ldzp(d(sysArgs+0)) #48 a[1]&63
anda(val(0x3f)) #49
st(d(sysArgs+0)); C('-> Pixel 0')#50
ld(val(hi('REENTER')),regY) #51
jmpy(d(lo('REENTER'))) #52
ld(val(-56/2)) #53
#-----------------------------------------------------------------------
# Extension SYS_PayloadCopy_34
#-----------------------------------------------------------------------
# sysArgs[0:1] Source address
# sysArgs[4] Copy count
# sysArgs[5:6] Destination address
label('SYS_PayloadCopy_34')
ldzp(d(sysArgs+4)) #15 Copy count
beq(d(lo('.sysCc0'))) #16
suba(d(1)) #17
st(d(sysArgs+4)) #18
ld(d(sysArgs+0),busRAM|regX) #19 Current pointer
ld(d(sysArgs+1),busRAM|regY) #20
ld(eaYXregAC,busRAM) #21
ld(d(sysArgs+5),busRAM|regX) #22 Target pointer
ld(d(sysArgs+6),busRAM|regY) #23
st(eaYXregAC) #24
ldzp(d(sysArgs+5)) #25 Increment target
adda(d(1)) #26
st(d(sysArgs+5)) #27
bra(d(lo('.sysCc1'))) #28
label('.sysCc0')
ld(val(hi('REENTER')),regY) #18,29
wait(30-19) #19
label('.sysCc1')
jmpy(d(lo('REENTER'))) #30
ld(d(-34/2)) #31
#-----------------------------------------------------------------------
#
# ROM page 7-8: Gigatron font data
#
#-----------------------------------------------------------------------
align(0x100, 0x100)
label('font32up')
for ch in range(32, 32+50):
comment = 'Char %s' % repr(chr(ch))
for byte in font.font[ch-32]:
ld(val(byte))
comment = C(comment)
trampoline()
#-----------------------------------------------------------------------
align(0x100, 0x100)
label('font82up')
for ch in range(32+50, 128):
comment = 'Char %s' % repr(chr(ch))
for byte in font.font[ch-32]:
ld(val(byte))
comment = C(comment)
trampoline()
#-----------------------------------------------------------------------
#
# ROM page 9: Key table for music
#
#-----------------------------------------------------------------------
align(0x100, 0x100)
notes = 'CCDDEFFGGAAB'
sampleRate = cpuClock / 200.0 / 4
label('notesTable')
for i in range(0, 250, 2):
j = i/2-1
freq = 440.0*2.0**((j-57)/12.0)
if j>=0 and freq <= sampleRate/2.0:
key = int(round(32768 * freq / sampleRate))
octave, note = j/12, notes[j%12]
sharp = '-' if notes[j%12-1] != note else '#'
comment = '%s%s%s (%0.1f Hz)' % (note, sharp, octave, freq)
else:
key, comment = 0, None
ld(val(key&127)); C(comment)
ld(val(key>>7))
trampoline()
#-----------------------------------------------------------------------
#
# ROM page 10: Inversion table
#
#-----------------------------------------------------------------------
align(0x100, 0x100)
label('invTable')
# Unit 64, table offset 16 (=1/4), value offset 1: (x+16)*(y+1) == 64*64 - e
for i in range(251):
ld(val(4096/(i+16)-1))
trampoline()
#-----------------------------------------------------------------------
# ROM page 11: Built-in full resolution images
#-----------------------------------------------------------------------
f = open('Images/gigatron.rgb', 'rb')
raw = f.read()
f.close()
align(0x100)
label('gigatronRaw')
for i in xrange(len(raw)):
if i&255 < 251:
ld(val(ord(raw[i])))
elif i&255 == 251:
trampoline()
def importImage(rgbName, width, height, ref):
f = open(rgbName)
raw = f.read()
f.close()
align(0x100)
label(ref)
for y in range(0, height, 2):
for j in range(2):
align(0x80)
comment = 'Pixels for %s line %s' % (ref, y+j)
for x in range(0, width, 4):
bytes = []
for i in range(4):
R = ord(raw[3 * ((y + j) * width + x + i) + 0])
G = ord(raw[3 * ((y + j) * width + x + i) + 1])
B = ord(raw[3 * ((y + j) * width + x + i) + 2])
bytes.append( (R/85) + 4*(G/85) + 16*(B/85) )
# Pack 4 pixels in 3 bytes
ld(val( ((bytes[0]&0b111111)>>0) + ((bytes[1]&0b000011)<<6) )); comment = C(comment)
ld(val( ((bytes[1]&0b111100)>>2) + ((bytes[2]&0b001111)<<4) ))
ld(val( ((bytes[2]&0b110000)>>4) + ((bytes[3]&0b111111)<<2) ))
if j==0:
trampoline3a()
else:
trampoline3b()
importImage('Images/Parrot-160x120.rgb', 160, 120, 'packedParrot')
importImage('Images/Baboon-160x120.rgb', 160, 120, 'packedBaboon')
importImage('Images/Jupiter-160x120.rgb', 160, 120, 'packedJupiter')
#-----------------------------------------------------------------------
# Application specific SYS extensions
#-----------------------------------------------------------------------
label('SYS_RacerUpdateVideoX_40')
ld(d(sysArgs+2),busRAM|regX) #15 q,
ld(d(sysArgs+3),busRAM|regY) #16
ld(eaYXregAC,busRAM) #17
st(d(vTmp)) #18
suba(d(sysArgs+4),busRAM) #19 X-
ld(d(sysArgs+0),busRAM|regX) #20 p.
ld(d(sysArgs+1),busRAM|regY) #21
st(eaYXregAC,busAC) #22
ld(d(sysArgs+0),busRAM) #23 p 4- p=
suba(d(4)) #24
st(d(sysArgs+0)) #25
ldzp(d(vTmp)) #26 q,
st(d(sysArgs+4)) #27 X=
ld(d(sysArgs+2),busRAM) #28 q<++
adda(d(1)) #29
st(d(sysArgs+2)) #30
bne(d(lo('.sysRacer0'))) #31 Self-repeat by adjusting vPC
ldzp(d(vPC)) #32
bra(d(lo('.sysRacer1'))) #33
nop() #34
label('.sysRacer0')
suba(d(2)) #33
st(d(vPC)) #34
label('.sysRacer1')
ld(val(hi('REENTER')),regY) #35
jmpy(d(lo('REENTER'))) #36
ld(val(-40/2)) #37
label('SYS_RacerUpdateVideoY_40')
ldzp(d(sysArgs+3)) #15 8&
anda(d(8)) #16
bne(d(lo('.sysRacer2'))) #17 [if<>0 1]
bra(d(lo('.sysRacer3'))) #18
ld(d(0)) #19
label('.sysRacer2')
ld(d(1)) #19
label('.sysRacer3')
st(d(vTmp)) #20 tmp=
ld(d(sysArgs+1),busRAM|regY) #21
ld(d(sysArgs+0),busRAM) #22 p<++ p<++
adda(d(2)) #23
st(d(sysArgs+0),busAC|regX) #24
xora(d(238)) #25 238^
st(d(vAC)) #26
st(d(vAC+1)) #27
ldzp(d(sysArgs+2)) #28 SegmentY
anda(d(254)) #29 254&
adda(d(vTmp),busRAM) #30 tmp+
st(eaYXregAC,busAC) #31
ldzp(d(sysArgs+2)) #32 SegmentY<++
adda(d(1)) #33
st(d(sysArgs+2)) #34
ld(val(hi('REENTER')),regY) #35
jmpy(d(lo('REENTER'))) #36
ld(val(-40/2)) #37
#-----------------------------------------------------------------------
# Extension SYS_LoaderProcessInput_48
#-----------------------------------------------------------------------
# sysArgs[0:1] Source address
# sysArgs[2] Checksum
# sysArgs[4] Copy count
# sysArgs[5:6] Destination address
label('SYS_LoaderProcessInput_48')
ld(d(sysArgs+1),busRAM|regY) #15
ldzp(d(sysArgs+2)) #16
bne(d(lo('.sysPi0'))) #17
ld(d(sysArgs+0),busRAM) #18
suba(d(65),regX) #19 Point at first byte of buffer
ld(eaYXregAC,busRAM) #20 Command byte
st(eaYXregOUTIX) #21 X++
xora(d(ord('L'))) #22 This loader lumps everything under 'L'
bne(d(lo('.sysPi1'))) #23
ld(eaYXregAC,busRAM); C('Valid command')#24 Length byte
st(eaYXregOUTIX) #25 X++
anda(d(63)) #26 Bit 6:7 are garbage
st(d(sysArgs+4)) #27 Copy count
ld(eaYXregAC,busRAM) #28 Low copy address
st(eaYXregOUTIX) #29 X++
st(d(sysArgs+5)) #30
ld(eaYXregAC,busRAM) #31 High copy address
st(eaYXregOUTIX) #32 X++
st(d(sysArgs+6)) #33
ldzp(d(sysArgs+4)) #34
bne(d(lo('.sysPi2'))) #35
# Execute code (don't care about checksum anymore)
ldzp(d(sysArgs+5)); C('Execute')#36 Low run address
suba(d(2)) #37
st(d(vPC)) #38
st(d(vLR)) #39
ldzp(d(sysArgs+6)) #40 High run address
st(d(vPC+1)) #41
st(d(vLR+1)) #42
ld(val(hi('REENTER')),regY) #43
jmpy(d(lo('REENTER'))) #44
ld(d(-48/2)) #45
# Invalid checksum
label('.sysPi0')
wait(25-19); C('Invalid checksum')#19 Reset checksum
# Unknown command
label('.sysPi1')
ld(d(ord('g'))); C('Unknown command')#25 Reset checksum
st(d(sysArgs+2)) #26
ld(val(hi('REENTER')),regY) #27
jmpy(d(lo('REENTER'))) #28
ld(d(-32/2)) #29
# Loading data
label('.sysPi2')
ld(d(sysArgs+0),busRAM); C('Loading data')#37 Continue checksum
suba(d(1),regX) #38 Point at last byte
ld(eaYXregAC,busRAM) #39
st(d(sysArgs+2)) #40
ld(val(hi('REENTER')),regY) #41
jmpy(d(lo('REENTER'))) #42
ld(d(-46/2)) #43
#-----------------------------------------------------------------------
#
# ROM page XX: Skyline for Racer
#
#-----------------------------------------------------------------------
f = open('Images/RacerHorizon-256x16.rgb', 'rb')
raw = f.read()
f.close()
packed, quartet = [], []
for i in xrange(0, len(raw), 3):
R, G, B = ord(raw[i+0]), ord(raw[i+1]), ord(raw[i+2])
quartet.append((R/85) + 4*(G/85) + 16*(B/85))
if len(quartet) == 4:
# Pack 4 pixels in 3 bytes
packed.append( ((quartet[0]&0b111111)>>0) + ((quartet[1]&0b000011)<<6) )
packed.append( ((quartet[1]&0b111100)>>2) + ((quartet[2]&0b001111)<<4) )
packed.append( ((quartet[2]&0b110000)>>4) + ((quartet[3]&0b111111)<<2) )
quartet = []
label('zippedRacerHorizon')
for i in xrange(len(packed)):
ld(val(packed[i]))
if pc()&255 == 251:
trampoline()
#-----------------------------------------------------------------------
#
# ROM page XX: Bootstrap vCPU
#
#-----------------------------------------------------------------------
# For info
print 'SYS limits low %s high %s' % (repr(minSYS), repr(maxSYS))
# Export some zero page variables to GCL
# XXX Solve in another way (not through symbol table!)
define('memSize', memSize)
define('entropy', entropy)
define('frameCount', frameCount)
define('serialRaw', serialRaw)
define('buttonState', buttonState)
define('sysFn', sysFn)
for i in range(8):
define('sysArgs%d' % i, sysArgs+i)
define('soundTimer', soundTimer)
define('vAC', vAC)
define('vACH', vAC+1)
define('vLR', vLR)
define('videoY', videoY)
# XXX This is a hack (trampoline() is probably in the wrong module):
define('vPC+1', vPC+1)
# Compile test GCL program
for gclSource in argv[1:]:
name = gclSource.rsplit('.', 1)[0]
name = name.rsplit('/', 1)[-1]
print
print 'Compile file %s label %s ROM %04x' % (gclSource, name, pc())
label(name)
program = gcl.Program(vCpuStart, name)
zpReset(zpFree)
for line in open(gclSource).readlines():
program.line(line)
program.end()
print
if pc()&255:
trampoline()
#-----------------------------------------------------------------------
# Finish assembly
#-----------------------------------------------------------------------
end()
| python |
from typing import Any, Sequence, Tuple, List, Callable, cast, TYPE_CHECKING
from argparse import ArgumentParser as OriginalAP
from argparse import Namespace as OriginalNS
from .namespace import Namespace
if TYPE_CHECKING:
from hiargparse.args_providers import ArgsProvider
class ArgumentParser(OriginalAP):
"""A wrapper class for argparse.ArgumentParser.
Do some cleanups for hiargparse.ArgsProviders
and returns hiargparse.Namespace instead of argparse.Namespace.
"""
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
self._defer_actions: List[Callable[[Namespace], None]] = list()
def parse_known_args(
self,
args: Sequence[str] = None,
namespace: OriginalNS = None
) -> Tuple[Namespace, List[str]]:
"""Wrapper method to return hiargparse.Namespace.
This method also takes some weird cleanups that hiargparse requires.
"""
if namespace is None:
target_space = Namespace()
else:
target_space = Namespace(namespace)
params, remains = super().parse_known_args(args, target_space)
# I know this params has type hiargparse.Namespace instead of argparse.Namespace
# typeshed lacks some important features
params = cast(Namespace, params)
self._do_deferred_actions(params)
return params, remains
def parse_args(
self,
args: Sequence[str] = None,
namespace: OriginalNS = None
) -> Namespace:
"""Wrapper method to return hiargparse.Namespace."""
params = super().parse_args(args, namespace)
# I know this params has type hiargparse.Namespace instead of argparse.Namespace
params = cast(Namespace, params)
return params
def add_arguments_from_provider(
self,
provider: 'ArgsProvider'
) -> None:
"""Syntax sugar for args_provider.add_arguments_to_parser()."""
provider.add_arguments_to_parser(self)
def register_deferring_action(
self,
action: Callable[[Namespace], None]
) -> None:
"""Register an action to do after its parsing."""
self._defer_actions.append(action)
def get_default_parameters(self) -> Namespace:
"""Get defaults by passing no arguments to the parser."""
return self.parse_args(args=[])
# protected
def _do_deferred_actions(self, params: Namespace) -> None:
for action in self._defer_actions:
action(params)
| python |
from hytra.pluginsystem import feature_serializer_plugin
from libdvid import DVIDNodeService
try:
import json_tricks as json
except ImportError:
import json
class DvidFeatureSerializer(feature_serializer_plugin.FeatureSerializerPlugin):
"""
serializes features to dvid
"""
keyvalue_store = "features"
def storeFeaturesForFrame(self, features, timeframe):
"""
Stores feature data
"""
assert self.server_address is not None
assert self.uuid is not None
node_service = DVIDNodeService(self.server_address, self.uuid)
node_service.create_keyvalue(self.keyvalue_store)
node_service.put(
self.keyvalue_store, "frame-{}".format(timeframe), json.dumps(features)
)
def loadFeaturesForFrame(self, features, timeframe):
"""
loads feature data
"""
assert self.server_address is not None
assert self.uuid is not None
node_service = DVIDNodeService(self.server_address, self.uuid)
node_service.create_keyvalue(self.keyvalue_store)
return json.loads(
node_service.get(self.keyvalue_store, "frame-{}".format(timeframe))
)
| python |
import sys
from PySide6.QtCore import QCoreApplication
from PySide6.QtWidgets import QApplication
from folder_watcher import FolderWatcher
from main_dialog import MainDialog
if __name__ == "__main__":
# QCoreApplication.setOrganizationName("DiPaolo Company")
QCoreApplication.setOrganizationDomain("dipaolo.com")
QCoreApplication.setApplicationName("watchdog-yt-uploader")
app = QApplication(sys.argv)
# watchdog = FolderWatcher()
# watchdog.start('/Users/dipaolo/repos/watchdog-yt-uploader')
mainDlg = MainDialog()
mainDlg.show()
sys.exit(app.exec())
| python |
from peewee import SqliteDatabase
db = SqliteDatabase(None) | python |
import socket
target_host = socket.gethostname()
target_port = 9999
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((target_host, target_port))
client.send(b'Hello World!!')
response = client.recv(4096)
client.close()
print(response.decode())
| python |
# coding: utf-8
"""
AVACloud API 1.17.3
AVACloud API specification # noqa: E501
OpenAPI spec version: 1.17.3
Generated by: https://github.com/swagger-api/swagger-codegen.git
"""
import pprint
import re # noqa: F401
import six
class ExecutionDescriptionDto(object):
"""NOTE: This class is auto generated by the swagger code generator program.
Do not edit the class manually.
"""
"""
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 = {
'blocks': 'list[NoteTextDto]',
'label': 'str',
'identifier': 'str',
'element_type': 'str'
}
attribute_map = {
'blocks': 'blocks',
'label': 'label',
'identifier': 'identifier',
'element_type': 'elementType'
}
def __init__(self, blocks=None, label=None, identifier=None, element_type=None): # noqa: E501
"""ExecutionDescriptionDto - a model defined in Swagger""" # noqa: E501
self._blocks = None
self._label = None
self._identifier = None
self._element_type = None
self.discriminator = None
if blocks is not None:
self.blocks = blocks
if label is not None:
self.label = label
if identifier is not None:
self.identifier = identifier
if element_type is not None:
self.element_type = element_type
@property
def blocks(self):
"""Gets the blocks of this ExecutionDescriptionDto. # noqa: E501
Blocks within an ExecutionDescription contain the actual information. # noqa: E501
:return: The blocks of this ExecutionDescriptionDto. # noqa: E501
:rtype: list[NoteTextDto]
"""
return self._blocks
@blocks.setter
def blocks(self, blocks):
"""Sets the blocks of this ExecutionDescriptionDto.
Blocks within an ExecutionDescription contain the actual information. # noqa: E501
:param blocks: The blocks of this ExecutionDescriptionDto. # noqa: E501
:type: list[NoteTextDto]
"""
self._blocks = blocks
@property
def label(self):
"""Gets the label of this ExecutionDescriptionDto. # noqa: E501
Labels this ExecutionDescription. # noqa: E501
:return: The label of this ExecutionDescriptionDto. # noqa: E501
:rtype: str
"""
return self._label
@label.setter
def label(self, label):
"""Sets the label of this ExecutionDescriptionDto.
Labels this ExecutionDescription. # noqa: E501
:param label: The label of this ExecutionDescriptionDto. # noqa: E501
:type: str
"""
self._label = label
@property
def identifier(self):
"""Gets the identifier of this ExecutionDescriptionDto. # noqa: E501
Uniquely identifies this ExecutionDescription. # noqa: E501
:return: The identifier of this ExecutionDescriptionDto. # noqa: E501
:rtype: str
"""
return self._identifier
@identifier.setter
def identifier(self, identifier):
"""Sets the identifier of this ExecutionDescriptionDto.
Uniquely identifies this ExecutionDescription. # noqa: E501
:param identifier: The identifier of this ExecutionDescriptionDto. # noqa: E501
:type: str
"""
self._identifier = identifier
@property
def element_type(self):
"""Gets the element_type of this ExecutionDescriptionDto. # noqa: E501
:return: The element_type of this ExecutionDescriptionDto. # noqa: E501
:rtype: str
"""
return self._element_type
@element_type.setter
def element_type(self, element_type):
"""Sets the element_type of this ExecutionDescriptionDto.
:param element_type: The element_type of this ExecutionDescriptionDto. # noqa: E501
:type: str
"""
self._element_type = element_type
def to_dict(self):
"""Returns the model properties as a dict"""
result = {}
for attr, _ in six.iteritems(self.swagger_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:
result[attr] = value
if issubclass(ExecutionDescriptionDto, 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, ExecutionDescriptionDto):
return False
return self.__dict__ == other.__dict__
def __ne__(self, other):
"""Returns true if both objects are not equal"""
return not self == other
| python |
"""STACS Exceptions.
SPDX-License-Identifier: BSD-3-Clause
"""
class STACSException(Exception):
"""The most generic form of exception raised by STACS."""
class FileAccessException(STACSException):
"""Indicates an error occured while attempting to access a file."""
class InvalidFileException(STACSException):
"""Indicates the format of a file did not match what was expected."""
class InvalidFormatException(STACSException):
"""Indicates that the format of a rule did not match what was expected."""
class IgnoreListException(STACSException):
"""Indicates an invalid ignore list was provided."""
class NotImplementedException(STACSException):
"""Indicates that the requested method has not been implemented."""
| python |
import unittest
from andes.utils.paths import list_cases
import andes
import os
class TestPaths(unittest.TestCase):
def setUp(self) -> None:
self.kundur = 'kundur/'
self.matpower = 'matpower/'
self.ieee14 = andes.get_case("ieee14/ieee14.raw")
def test_tree(self):
list_cases(self.kundur, no_print=True)
list_cases(self.matpower, no_print=True)
def test_addfile_path(self):
path, case = os.path.split(self.ieee14)
andes.load('ieee14.raw', addfile='ieee14.dyr',
input_path=path, default_config=True,
)
andes.run('ieee14.raw', addfile='ieee14.dyr',
input_path=path,
no_output=True, default_config=True,
)
def test_pert_file(self):
"""Test path of pert file"""
path, case = os.path.split(self.ieee14)
# --- with pert file ---
ss = andes.run('ieee14.raw', pert='pert.py',
input_path=path, no_output=True, default_config=True,
)
ss.TDS.init()
self.assertIsNotNone(ss.TDS.callpert)
# --- without pert file ---
ss = andes.run('ieee14.raw',
input_path=path, no_output=True, default_config=True,
)
ss.TDS.init()
self.assertIsNone(ss.TDS.callpert)
| python |
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, JSON
from sqlalchemy.orm import relationship
from open_needs_server.database import Base
class DomainModel(Base):
__tablename__ = "domains"
def __repr__(self) -> str:
return f"[{self.id}]{self.title}"
id = Column(Integer, primary_key=True, index=True)
title = Column(String, unique=False, index=True)
description = Column(String, unique=False, index=False)
jsonschema = Column(JSON, unique=False, nullable=True, index=True)
| python |
# -*- coding: utf-8 -*-
#!/usr/bin/env python
"""
@author: Noah Norman
n@hardwork.party
"""
import json
def load_file():
with open('data.json') as data_file:
return json.load(data_file)
def verbose():
return DATA['verbose']
def fade_in():
return DATA['switch_on_fadein']
def fade_out():
return DATA['switch_off_fadeout']
def lifx_url():
return DATA['LIFX_URL']
def lights_url():
return DATA['LIGHTS_URL']
def state_url():
return DATA['STATE_URL']
DATA = load_file()
| python |
from __future__ import absolute_import
import random
def RandomService(services):
if len(services) == 0:
return None
index = random.randint(0, len(services) - 1)
return services[index] | python |
import requests
import sys
from firecares.firestation.models import FireDepartment
from django.core.management.base import BaseCommand
from optparse import make_option
def chunks(l, n):
for i in xrange(0, len(l), n):
yield l[i:i + n]
class Command(BaseCommand):
help = 'Verifies that the thumbnails for given department(s) are retrievable'
args = '[department]'
option_list = BaseCommand.option_list + (
make_option('-d', '--department',
dest='department',
help='The FireCARES department id.'),
)
def handle(self, *args, **options):
department_id = options.get('department')
firedepartments = FireDepartment.objects.filter(pk=department_id) if department_id else FireDepartment.objects.all()
fd_count = len(firedepartments)
bad_thumbs = 0
print('Looking up thumbnails for {cnt}'.format(cnt=fd_count))
session = requests.Session()
for idx, fd in enumerate(firedepartments):
if not idx % 10:
print('Processing ({idx}/{all})'.format(idx=idx, all=len(firedepartments)))
sys.stdout.flush()
resp = session.head(fd.thumbnail)
if resp.status_code != 200:
bad_thumbs += 1
print('Bad thumbnail {url} for firepartment id: {id}'.format(id=fd.id, url=fd.thumbnail))
if not firedepartments:
print('Firedepartment(s) not found')
else:
print('# of bad fire department thumbnails => ({bad}/{all})'.format(bad=bad_thumbs, all=fd_count))
| python |
# -*- coding: utf-8 -*-
'''
:file: utils.py
:author: -Farmer
:url: https://blog.farmer233.top
:date: 2021/09/04 23:45:40
'''
class ObjectDict(dict):
""":copyright: (c) 2014 by messense.
Makes a dictionary behave like an object, with attribute-style access.
"""
def __getattr__(self, key):
if key in self:
return self[key]
return None
def __setattr__(self, key, value):
self[key] = value
def __getstate__(self):
return None
def is_endpoint(url_or_endpoint:str) -> bool:
"""判断是不是端点
Args:
url_or_endpoint (str): url 或 端点字符串
Returns:
bool: 不是http则返回False
"""
if url_or_endpoint.startswith(('http://', 'https://')):
return False
return True
| python |
import subprocess
import sys
class ProcessManager(object):
"""
Implements a manager for process to be executed in the environment.
"""
def __init__(
self,
command,
working_directory,
environment_variables,
):
"""
Initializes the manager.
:param command: the command to execute
:type command: str
:param working_directory: the working directory for the command
:type working_directory: str
:param environment_variables: the environment variables starting set
:type environment_variables: dict[str, str]
"""
self.__command = command
self.__working_directory = working_directory
self.__environment_variables = environment_variables
@property
def environment_variables(self):
"""
Returns the current set of environment variables.
:return: the environment variables
:rtype: dict[str, str]
"""
return self.__environment_variables
def add_environment_variables(
self,
variables,
):
"""
Adds the variables to the environment variables already set.
:param variables: the variables dictionary to add
:type variables: dict[str, str]
"""
self.__environment_variables.update(variables)
def run(self):
"""
Executes the command.
:return: the STDOUT and STDERR, together with the return code of the command
"""
process = subprocess.Popen(
self.__command,
cwd=self.__working_directory,
env=self.__environment_variables,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True,
)
stdout = ''
for line in iter(process.stdout.readline, b''):
line = str(line, 'utf-8')
stdout += line
print(line)
sys.stdout.flush()
return_code = process.wait()
return stdout, return_code
def echo(self):
process = subprocess.Popen(
'echo ' + self.__command,
cwd=self.__working_directory,
env=self.__environment_variables,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True,
)
stdout = ''
for line in iter(process.stdout.readline, b''):
line = str(line, 'utf-8')
stdout += line
print(line)
sys.stdout.flush()
return_code = process.wait()
return stdout, return_code
| python |
#! /usr/bin/env python
#
# Copyright (c) 2011-2012 Bryce Adelstein-Lelbach
#
# SPDX-License-Identifier: BSL-1.0
# Distributed under the Boost Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
# TODO: Rename to jobs?
# TODO: More typechecking?
# TODO: Match threading.Thread interface and/or subprocess interface better?
# TODO: Better exception propagation
from sys import float_info, platform
from threading import Thread, Lock
from time import sleep, time
from subprocess import Popen, STDOUT, PIPE
from shlex import split
from signal import SIGKILL
from os import kill
from platform import system
from queue import Queue, Empty
from errno import ESRCH
# TODO: implement for Windows
OS_MAC = False
OS_LIN = False
if platform.startswith('darwin'):
OS_MAC = True
from select import kqueue, kevent
from select import KQ_FILTER_READ, KQ_EV_ADD, KQ_EV_DELETE, KQ_NOTE_LOWAT
if platform.startswith('linux'):
OS_LIN = True
from select import epoll, EPOLLHUP
def kill_process_tree(parent_pid, signal=SIGKILL):
def find_process_tree(pid):
cmd = ""
if OS_MAC:
cmd = "ps -o pid,ppid -ax | egrep ' %d$' | awk '{print $1}'" % pid
else:
cmd = "ps -o pid --ppid %d --noheaders" % pid
ps_command = Popen(cmd, shell=True, stdout=PIPE)
ps_output = ps_command.stdout.read()
retcode = ps_command.wait()
if 0 == ps_command.wait():
list = [pid]
for child_pid in ps_output.split("\n")[:-1]:
list = list + find_process_tree(int(child_pid))
return list
else:
return [pid]
r = True
for pid in find_process_tree(parent_pid):
try:
kill(int(pid), signal)
except OSError as err:
if ESRCH != err.errno:
raise err
else:
r = False
return r
class process(object):
_proc = None
_error = None
_groups = None
_timed_out = None
def __init__(self, cmd, group=None):
if StringType == type(cmd):
cmd = split(cmd)
self._proc = Popen(cmd, stderr=STDOUT, stdout=PIPE, shell=False)
self._error = None
self._groups = []
self._timed_out = False
if group is not None:
group.add_process(self)
def _call(self):
# annoyingly, KeyboardInterrupts are transported to threads, while most
# other Exceptions aren't in python
try:
self._proc.wait()
except Exception as err:
self._error = err
def _finish(self, thread):
# be forceful
if thread.is_alive():
# the thread may still be alive for a brief period after the process
# finishes (e.g. when it is notifying groups), so we ignore any errors
self.terminate()
thread.join()
self._timed_out = True
# if an exception happened, re-raise it here in the master thread
if self._error is not None:
raise self._error
return (self._timed_out, self._proc.returncode)
def terminate(self):
return kill_process_tree(self.pid())
def poll(self):
return self._proc.poll()
def pid(self):
return self._proc.pid
def fileno(self):
return self._proc.stdout.fileno()
def timed_out(self):
return self._timed_out
def wait(self, timeout=None):
if timeout is not None:
thread = Thread(target=self._call)
thread.start()
# wait for the thread and invoked process to finish
thread.join(timeout)
return self._finish(thread)
else:
return (self._timed_out, self._proc.wait())
def join(self, timeout=None):
return self.wait(timeout)
def read(self, timeout=None):
read_queue = Queue()
def enqueue_output():
for block in iter(self._proc.stdout.read, b''):
read_queue.put(block)
read_queue.put('')
thread = Thread(target=enqueue_output)
thread.daemon = True
thread.start()
output = ''
try:
started = time()
while timeout is None or not float_info.epsilon > timeout:
s = read_queue.get(timeout=timeout)
if s:
output += s
else:
return output
if not timeout is None:
timeout -= (time() - started)
except Empty:
return output
# modelled after Boost.Thread's boost::thread_group class
class process_group(object):
_lock = None
_members = None
_poller = None
def __init__(self, *cmds):
self._lock = Lock()
self._members = {}
if OS_MAC:
self._poller = kqueue()
if OS_LIN:
self._poller = epoll()
for cmd in cmds:
self.create_process(cmd)
def create_process(self, cmd):
return process(cmd, self)
def add_process(self, job):
with self._lock:
self._members[job.fileno()] = job
if OS_MAC:
self._poller.control([kevent(job._proc.stdout,
KQ_FILTER_READ, KQ_EV_ADD, KQ_NOTE_LOWAT, 0)], 0)
if OS_LIN:
self._poller.register(job._proc.stdout, EPOLLHUP)
def join_all(self, timeout=None, callback=None):
with self._lock:
not_done = self._members.copy()
started = time()
while timeout is None or not float_info.epsilon > timeout:
if OS_MAC:
if timeout == None:
timeout=-1.0
ready = self._poller.control(None,1,timeout)
if OS_LIN:
ready = self._poller.poll(timeout=-1.0 if timeout is None else timeout)
if not timeout is None:
timeout -= (time() - started)
if OS_MAC:
for fd in ready:
fd = fd.ident
self._poller.control([kevent(fd, KQ_FILTER_READ, KQ_EV_DELETE)], 0)
not_done.pop(fd)
if callable(callback):
callback(fd, self._members[fd])
if OS_LIN:
for fd, flags in ready:
self._poller.unregister(fd)
not_done.pop(fd)
if callable(callback):
callback(fd, self._members[fd])
if 0 == len(not_done):
return
# some of the jobs are not done, we'll have to forcefully stop them
for fd in not_done:
if self._members[fd].terminate():
self._members[fd]._timed_out = True
if callable(callback):
callback(fd, self._members[fd])
def read_all(self, timeout=None, callback=None):
output = {}
def read_callback(fd, job):
output[fd] = job.read(0.5)
if callable(callback):
callback(fd, job, output[fd])
self.join_all(timeout, read_callback)
return output
def terminate_all(self, callback=None):
with self._lock:
for (fd, job) in self._members.iteritems():
if job.terminate():
if callable(callback):
callback(fd, job)
def join_all(*tasks, **keys):
def flatten(items):
result = []
for element in items:
if hasattr(element, "__iter__"):
result.extend(flatten(el))
else:
if not isinstance(element, process):
raise TypeError( "'%s' is not an instance of 'hpx.process'"
% str(element))
result.append(element)
return result
tasks = flatten(tasks)
pg = process_group()
for task in tasks:
pg.add_process(task)
pg.join_all(keys['timeout'], keys['callback'])
def read_all(*tasks, **keys):
output = {}
callback = keys['callback']
def read_callback(fd, job):
output[fd] = job.read()
if callable(callback):
callback(fd, job, output[fd])
keys['callback'] = read_callback
join_all(*tasks, **keys)
return output
| python |
import unittest
from unittest.mock import patch, MagicMock
from requests import Response
from pylaunch.dial import Dial
class TestDial(unittest.TestCase):
@patch("pylaunch.core.requests.get")
def setUp(self, response):
with open("tests/xml/dd.xml") as f:
response.return_value = MagicMock(
spec=Response,
text=f.read(),
headers={"Application-URL": "http://10.1.10.165:8060/dial"},
)
self.dial = Dial("https://10.1.10.165:8060/dial/dd.xml")
def test_address(self):
self.assertEqual(self.dial.address, "http://10.1.10.165:8060/dial")
def test_device_type(self):
self.assertEqual(self.dial.device_type, "urn:roku-com:device:player:1-0")
def test_friendly_name(self):
self.assertEqual(self.dial.friendly_name, "NNB CT")
def test_manufacturer(self):
self.assertEqual(self.dial.manufacturer, "Roku")
def test_manufacturer_url(self):
self.assertEqual(self.dial.manufacturer_url, "http://www.roku.com/")
def test_model_description(self):
self.assertEqual(
self.dial.model_description, "Roku Streaming Player Network Media"
)
def test_model_name(self):
self.assertEqual(self.dial.model_name, "Roku Express")
def test_model_number(self):
self.assertEqual(self.dial.model_number, "3900X")
def test_model_url(self):
self.assertEqual(self.dial.model_url, "http://www.roku.com/")
def test_serial_number(self):
self.assertEqual(self.dial.serial_number, "YG00AE419756")
def test_udn(self):
self.assertEqual(self.dial.udn, "uuid:295c0011-5406-1067-80ac-d83134855445")
def test_launch_app(self):
pass
def test_kill_app(self):
pass
@patch("requests.get")
def test_get_app_status(self, response):
with open("tests/xml/YouTube.xml") as f:
response.return_value = MagicMock(
spec=Response, text=f.read(), status_code=200
)
app_status = self.dial.get_app_status("YouTube")
self.assertEquals(
app_status, {"version": "2.1", "name": "YouTube", "state": "stopped"}
)
| python |
import frappe
from frappe.utils import data
from frappe.utils import cstr, add_days, date_diff, getdate
from frappe.utils import format_date
@frappe.whitelist()
def get_cl_count(from_date,to_date):
dates = get_dates(from_date,to_date)
data = ""
for date in dates:
contractors = frappe.get_all('Contractor')
for contractor in contractors:
shift_1 = 0
shift_2 = 0
shift_3 = 0
shift_pp1 = 0
shift_pp2 = 0
if frappe.db.exists('CL Head Count Plan',{'date':date,'contractor':contractor.name}):
plan = frappe.get_doc('CL Head Count Plan',{'date':date,'contractor':contractor.name})
shift_1 = plan.shift_1
shift_2 = plan.shift_2
shift_3 = plan.shift_3
shift_pp1 = plan.shift_pp1
shift_pp2 = plan.shift_pp2
data += "<tr><td style = 'border: 1px solid black'>%s</td><td style = 'border: 1px solid black'>%s</td><td style = 'border: 1px solid black'>%s</td><td style = 'border: 1px solid black'>%s</td><td style = 'border: 1px solid black'>%s</td><td style = 'border: 1px solid black'>%s</td><td style = 'border: 1px solid black'>%s</td></tr>"%(format_date(date),contractor.name,shift_1,shift_2,shift_3,shift_pp1,shift_pp2)
return data
def get_dates(from_date,to_date):
no_of_days = date_diff(add_days(to_date, 1), from_date)
dates = [add_days(from_date, i) for i in range(0, no_of_days)]
return dates | python |
import os
import sys
import argparse
import torch
sys.path.append(os.getcwd())
import pickle
import src.data.data as data
import src.data.config as cfg
import src.interactive.functions as interactive
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
"""
Basic example which iterates through the tasks specified and prints them out. Used for
verification of data loading and iteration.
For more documentation, see parlai.scripts.display_data.
"""
from parlai.scripts.display_data import display_data, setup_args
from parlai.core.params import ParlaiParser
from parlai.agents.repeat_label.repeat_label import RepeatLabelAgent
from parlai.core.worlds import create_task
import random
import csv
def display_data1(opt):
# create repeat label agent and assign it to the specified task
agent = RepeatLabelAgent(opt)
world = create_task(opt, agent)
model_list = interactive.load_model_file(opt['model_file'])
res = []
# Show some example dialogs.
print("will print "+str(opt['num_examples'])+' dialog')
dialog_id = 0
idx=0
while dialog_id < 11:#opt['num_examples']
world.parley()
# NOTE: If you want to look at the data from here rather than calling
# world.display() you could access world.acts[0] directly
# print(world.display() + '\n~~')
utterance = world.acts[0]
for i in range(2):
event_dict = {}
if i ==0:
sentence = utterance['text'].split('\n')[-1]
if len(utterance['text'].split('\n'))>1:
dialog_id +=1
idx = 0
event_dict['utterance'] = utterance['text'].replace('\n',' || ')
print(utterance['text'])
else:
if(len(utterance['labels'])>1):
print('=======================')
print(utterance)
event_dict['utterance'] = utterance['labels'][0]
sentence = event_dict['utterance']
event_list = ['oEffect', 'oReact', 'oWant', 'xAttr', 'xEffect', 'xIntent', 'xNeed', 'xReact', 'xWant']
print("==",sentence)
eres = extract_event(opt,sentence,model_list)
for etype in event_list:
event_dict[etype] = "none"
beam_res = eres[etype]['beams']
for res1 in beam_res:
if res1 == "none":
continue
event_dict[etype] = res1
break
event_dict["dialog_id"] = dialog_id
event_dict["id"] = idx
res.append(event_dict)
idx += 1
if world.epoch_done():
print('EPOCH DONE')
break
return res
try:
# print dataset size if available
print(
'[ loaded {} episodes with a total of {} examples ]'.format(
world.num_episodes(), world.num_examples()
)
)
except Exception:
pass
def extract_event(args,input_event,model_list):
opt, state_dict = model_list
data_loader, text_encoder = interactive.load_data("atomic", opt)
n_ctx = data_loader.max_event + data_loader.max_effect
n_vocab = len(text_encoder.encoder) + n_ctx
model = interactive.make_model(opt, n_vocab, n_ctx, state_dict)
if args['device'] != "cpu":
cfg.device = int(args['device'])
cfg.do_gpu = True
torch.cuda.set_device(cfg.device)
model.cuda(cfg.device)
else:
cfg.device = "cpu"
sampling_algorithm = args['sampling_algorithm']
if input_event == "help":
interactive.print_help(opt.dataset)
category = "all" #input("Give an effect type (type \"help\" for an explanation): ")
if category == "help":
interactive.print_category_help(opt.dataset)
sampling_algorithm = "beam-3" #input("Give a sampling algorithm (type \"help\" for an explanation): ")
if sampling_algorithm == "help":
interactive.print_sampling_help()
sampler = interactive.set_sampler(opt, sampling_algorithm, data_loader)
if category not in data_loader.categories:
category = "all"
outputs = interactive.get_atomic_sequence(
input_event, model, sampler, data_loader, text_encoder, category)
return outputs
import os
if __name__ == '__main__':
random.seed(42)
# Get command line arguments
parser = setup_args()
parser.add_argument("--device", type=str, default="1")
parser.add_argument("--model_file", type=str, default="models/atomic-generation/iteration-500-50000/transformer/categories_oEffect#oReact#oWant#xAttr#xEffect#xIntent#xNeed#xReact#xWant/model_transformer-nL_12-nH_12-hSize_768-edpt_0.1-adpt_0.1-rdpt_0.1-odpt_0.1-pt_gpt-afn_gelu-init_pt-vSize_40542/exp_generation-seed_123-l2_0.01-vl2_T-lrsched_warmup_linear-lrwarm_0.002-clip_1-loss_nll-b2_0.999-b1_0.9-e_1e-08/bs_1-smax_40-sample_greedy-numseq_1-gs_1000-es_1000-categories_oEffect#oReact#oWant#xAttr#xEffect#xIntent#xNeed#xReact#xWant/6.25e-05_adam_64_22000.pickle")
parser.add_argument("--sampling_algorithm", type=str, default="help")
opt = parser.parse_args()
#parser = argparse.ArgumentParser(parser)
#opt = parser.parse_args()
res = display_data1(opt)
f = open("dict.pkl","wb")
pickle.dump(res,f)
f.close()
keys = res[0].keys()
with open('result.csv', 'w') as output_file:
dict_writer = csv.DictWriter(output_file, keys)
dict_writer.writeheader()
dict_writer.writerows(res)
| python |
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import sklearn
from sklearn import preprocessing
def readdate(name):
df = pd.read_csv('gene_mutation.txt', sep=',', header=None)
return df
if __name__ == "__main__":
df=readdate('gene_mutation.txt')
del df[0]
t2 = df.groupby([1,2]).size().unstack(level = -1, fill_value = 0)
z=t2.iloc[0:28,]
scaler_value = sklearn.preprocessing.StandardScaler()
train_values = scaler_value.fit_transform(z)
zz=pd.DataFrame(train_values)
f, ax = plt.subplots(figsize = (15, 12))
sns.heatmap(zz, cmap = 'RdBu_r', linewidths = 0.05, ax = ax, vmin=-1, vmax=1)
ax.set_title('Gene mutation distribution',fontsize=27)
ax.set_ylabel('gene',fontsize=25)
ax.set_xticklabels(['BLCA','BRCA','CESC','COAD','HNSC','KIRC','LIHC','LUAD','LUSC','OV','READ','SKCM','STAD','THCA','UCEC'])
ax.set_ylim(28, 0)
plt.yticks([])
plt.xticks(rotation=45)
cax = plt.gcf().axes[-1]
cax.tick_params(labelsize=17)
plt.tick_params(labelsize=18)
plt.show()
| python |
import numpy as np
import pax
import torchvision
IMAGENET_MEAN = np.array((0.485, 0.456, 0.406))
IMAGENET_STD = np.array((0.229, 0.224, 0.225))
def convert_conv(conv, name=None):
"""Return a pax.Conv2D module with weights from pretrained ``conv``."""
weight = conv.weight.data.contiguous().permute(2, 3, 1, 0).contiguous().numpy()[:]
pax_conv = pax.Conv2D(
in_features=conv.in_channels,
out_features=conv.out_channels,
kernel_shape=conv.kernel_size,
stride=conv.stride,
with_bias=False,
padding=[(conv.padding[0],) * 2, (conv.padding[1],) * 2],
data_format="NCHW",
name=name,
)
assert pax_conv.weight.shape == weight.shape
return pax_conv.replace(weight=weight)
def convert_bn(bn, name=None):
"""Return a pax.BatchNorm2D module from pretrained ``bn``."""
weight = bn.weight.data.numpy()[None, :, None, None]
bias = bn.bias.data.numpy()[None, :, None, None]
running_mean = bn.running_mean.data.numpy()[None, :, None, None]
running_var = bn.running_var.data.numpy()[None, :, None, None]
pax_bn = pax.BatchNorm2D(
num_channels=bias.shape[1],
create_offset=True,
create_scale=True,
decay_rate=0.9,
eps=1e-5,
data_format="NCHW",
name=name,
)
assert pax_bn.scale.shape == weight.shape
assert pax_bn.offset.shape == bias.shape
assert pax_bn.ema_mean.averages.shape == running_mean.shape
assert pax_bn.ema_var.averages.shape == running_var.shape
pax_bn = pax_bn.replace(scale=weight, offset=bias)
pax_bn = pax_bn.replace_node(pax_bn.ema_mean.averages, running_mean)
pax_bn = pax_bn.replace_node(pax_bn.ema_var.averages, running_var)
return pax_bn
def convert_basic_block(block):
conv1 = convert_conv(block.conv1, name="conv1")
bn1 = convert_bn(block.bn1, name="bn1")
conv2 = convert_conv(block.conv2, name="conv2")
bn2 = convert_bn(block.bn2, name="bn2")
if block.downsample is not None:
conv0 = convert_conv(block.downsample[0], name="proj_conv")
bn0 = convert_bn(block.downsample[1], name="proj_bn")
return ((conv1, bn1), (conv2, bn2)), (conv0, bn0)
else:
return (((conv1, bn1), (conv2, bn2)),)
def convert_block_group(group):
out = []
for i in range(len(group)):
out.append(convert_basic_block(group[i]))
return out
def convert_linear(linear):
weight = linear.weight.data.numpy()
bias = linear.bias.data.numpy()
pax_linear = pax.Linear(
in_dim=weight.shape[1], out_dim=weight.shape[0], with_bias=True
)
weight = np.transpose(weight)
assert pax_linear.bias.shape == bias.shape
assert pax_linear.weight.shape == weight.shape
return pax_linear.replace(weight=weight, bias=bias)
def load_pretrained_resnet18():
resnet18 = pax.nets.ResNet18(3, 1000)
resnet18_pt = torchvision.models.resnet18(pretrained=True).eval()
pax_resnet = [
convert_conv(resnet18_pt.conv1),
convert_bn(resnet18_pt.bn1),
convert_block_group(resnet18_pt.layer1),
convert_block_group(resnet18_pt.layer2),
convert_block_group(resnet18_pt.layer3),
convert_block_group(resnet18_pt.layer4),
convert_linear(resnet18_pt.fc),
]
def replace_parts(resnet18):
# replace resnet18 part by part
resnet18.initial_conv = pax_resnet[0]
resnet18.initial_batchnorm = pax_resnet[1]
for i in range(len(resnet18.block_groups)):
bg = resnet18.block_groups[i]
for j in range(len(bg.blocks)):
b = bg.blocks[j]
mods = pax_resnet[2 + i][j]
b.layers = mods[0]
if b.use_projection:
b.proj_conv = mods[1][0]
b.proj_batchnorm = mods[1][1]
resnet18.logits = pax_resnet[-1]
# make sure we are in `eval` mode when doing evaluation.
return resnet18.eval()
return pax.pure(replace_parts)(resnet18)
| python |
import tensorflow as tf
from transformers import TFDistilBertForQuestionAnswering
model = TFDistilBertForQuestionAnswering.from_pretrained('distilbert-base-uncased-distilled-squad')
input_spec = tf.TensorSpec([1, 384], tf.int32)
model._set_inputs(input_spec, training=False)
# For tensorflow>2.2.0, set inputs in the following way.
# Otherwise, the model.inputs and model.outputs will be None.
# keras_input = tf.keras.Input([384], batch_size=1, dtype=tf.int32)
# keras_output = model(keras_input, training=False)
# model = tf.keras.Model(keras_input, keras_output)
print(model.inputs)
print(model.outputs)
converter = tf.lite.TFLiteConverter.from_keras_model(model)
# For normal conversion:
converter.target_spec.supported_ops = [tf.lite.OpsSet.SELECT_TF_OPS]
# For conversion with FP16 quantization:
# converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
# converter.target_spec.supported_types = [tf.float16]
# converter.optimizations = [tf.lite.Optimize.DEFAULT]
# converter.experimental_new_converter = True
# For conversion with hybrid quantization:
# converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
# converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]
# converter.experimental_new_converter = True
tflite_model = converter.convert()
open("distilbert-squad-384.tflite", "wb").write(tflite_model)
| python |
"""Library for the ingress relation.
This library contains the Requires and Provides classes for handling
the ingress interface.
Import `IngressRequires` in your charm, with two required options:
- "self" (the charm itself)
- config_dict
`config_dict` accepts the following keys:
- service-hostname (required)
- service-name (required)
- service-port (required)
- limit-rps
- limit-whitelist
- max_body-size
- retry-errors
- service-namespace
- session-cookie-max-age
- tls-secret-name
See [the config section](https://charmhub.io/nginx-ingress-integrator/configure) for descriptions
of each, along with the required type.
As an example, add the following to `src/charm.py`:
```
from charms.nginx_ingress_integrator.v0.ingress import IngressRequires
# In your charm's `__init__` method.
self.ingress = IngressRequires(self, {"service-hostname": self.config["external_hostname"],
"service-name": self.app.name,
"service-port": 80})
# In your charm's `config-changed` handler.
self.ingress.update_config({"service-hostname": self.config["external_hostname"]})
```
And then add the following to `metadata.yaml`:
```
requires:
ingress:
interface: ingress
```
"""
import logging
from ops.charm import CharmEvents
from ops.framework import EventBase, EventSource, Object
from ops.model import BlockedStatus
# The unique Charmhub library identifier, never change it
LIBID = "db0af4367506491c91663468fb5caa4c"
# Increment this major API version when introducing breaking changes
LIBAPI = 0
# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 5
logger = logging.getLogger(__name__)
REQUIRED_INGRESS_RELATION_FIELDS = {
"service-hostname",
"service-name",
"service-port",
}
OPTIONAL_INGRESS_RELATION_FIELDS = {
"limit-rps",
"limit-whitelist",
"max-body-size",
"retry-errors",
"service-namespace",
"session-cookie-max-age",
"tls-secret-name",
}
class IngressAvailableEvent(EventBase):
pass
class IngressCharmEvents(CharmEvents):
"""Custom charm events."""
ingress_available = EventSource(IngressAvailableEvent)
class IngressRequires(Object):
"""This class defines the functionality for the 'requires' side of the 'ingress' relation.
Hook events observed:
- relation-changed
"""
def __init__(self, charm, config_dict):
super().__init__(charm, "ingress")
self.framework.observe(
charm.on["ingress"].relation_changed, self._on_relation_changed
)
self.config_dict = config_dict
def _config_dict_errors(self, update_only=False):
"""Check our config dict for errors."""
blocked_message = "Error in ingress relation, check `juju debug-log`"
unknown = [
x
for x in self.config_dict
if x
not in REQUIRED_INGRESS_RELATION_FIELDS | OPTIONAL_INGRESS_RELATION_FIELDS
]
if unknown:
logger.error(
"Ingress relation error, unknown key(s) in config dictionary found: %s",
", ".join(unknown),
)
self.model.unit.status = BlockedStatus(blocked_message)
return True
if not update_only:
missing = [
x for x in REQUIRED_INGRESS_RELATION_FIELDS if x not in self.config_dict
]
if missing:
logger.error(
"Ingress relation error, missing required key(s) in config dictionary: %s",
", ".join(missing),
)
self.model.unit.status = BlockedStatus(blocked_message)
return True
return False
def _on_relation_changed(self, event):
"""Handle the relation-changed event."""
# `self.unit` isn't available here, so use `self.model.unit`.
if self.model.unit.is_leader():
if self._config_dict_errors():
return
for key in self.config_dict:
event.relation.data[self.model.app][key] = str(self.config_dict[key])
def update_config(self, config_dict):
"""Allow for updates to relation."""
if self.model.unit.is_leader():
self.config_dict = config_dict
if self._config_dict_errors(update_only=True):
return
relation = self.model.get_relation("ingress")
if relation:
for key in self.config_dict:
relation.data[self.model.app][key] = str(self.config_dict[key])
class IngressProvides(Object):
"""This class defines the functionality for the 'provides' side of the 'ingress' relation.
Hook events observed:
- relation-changed
"""
def __init__(self, charm):
super().__init__(charm, "ingress")
# Observe the relation-changed hook event and bind
# self.on_relation_changed() to handle the event.
self.framework.observe(
charm.on["ingress"].relation_changed, self._on_relation_changed
)
self.charm = charm
def _on_relation_changed(self, event):
"""Handle a change to the ingress relation.
Confirm we have the fields we expect to receive."""
# `self.unit` isn't available here, so use `self.model.unit`.
if not self.model.unit.is_leader():
return
ingress_data = {
field: event.relation.data[event.app].get(field)
for field in REQUIRED_INGRESS_RELATION_FIELDS
| OPTIONAL_INGRESS_RELATION_FIELDS
}
missing_fields = sorted(
[
field
for field in REQUIRED_INGRESS_RELATION_FIELDS
if ingress_data.get(field) is None
]
)
if missing_fields:
logger.error(
"Missing required data fields for ingress relation: {}".format(
", ".join(missing_fields)
)
)
self.model.unit.status = BlockedStatus(
"Missing fields for ingress: {}".format(", ".join(missing_fields))
)
# Create an event that our charm can use to decide it's okay to
# configure the ingress.
self.charm.on.ingress_available.emit()
| python |
import nltk
import re
import shutil
import os
from urllib.parse import urlparse
from coalib.bears.GlobalBear import GlobalBear
from dependency_management.requirements.PipRequirement import PipRequirement
from coala_utils.ContextManagers import change_directory
from coalib.misc.Shell import run_shell_command
from coalib.results.Result import Result
from coalib.settings.FunctionMetadata import FunctionMetadata
from coalib.settings.Setting import typed_list
class GitCommitBear(GlobalBear):
LANGUAGES = {'Git'}
REQUIREMENTS = {PipRequirement('nltk', '3.2')}
AUTHORS = {'The coala developers'}
AUTHORS_EMAILS = {'coala-devel@googlegroups.com'}
LICENSE = 'AGPL-3.0'
ASCIINEMA_URL = 'https://asciinema.org/a/e146c9739ojhr8396wedsvf0d'
CAN_DETECT = {'Formatting'}
SUPPORTED_HOST_KEYWORD_REGEX = {
'github': (r'[Cc]lose[sd]?'
r'|[Rr]esolve[sd]?'
r'|[Ff]ix(?:e[sd])?'),
'gitlab': (r'[Cc]los(?:e[sd]?|ing)'
r'|[Rr]esolv(?:e[sd]?|ing)'
r'|[Ff]ix(?:e[sd]|ing)?')
}
CONCATENATION_KEYWORDS = [r',', r'\sand\s']
_nltk_data_downloaded = False
def setup_dependencies(self):
if not self._nltk_data_downloaded and bool(
self.section.get('shortlog_check_imperative', True)):
nltk.download([
'punkt',
'maxent_treebank_pos_tagger',
'averaged_perceptron_tagger',
])
type(self)._nltk_data_downloaded = True
@classmethod
def check_prerequisites(cls):
if shutil.which('git') is None:
return 'git is not installed.'
else:
return True
@classmethod
def get_shortlog_checks_metadata(cls):
return FunctionMetadata.from_function(
cls.check_shortlog,
omit={'self', 'shortlog'})
@classmethod
def get_body_checks_metadata(cls):
return FunctionMetadata.from_function(
cls.check_body,
omit={'self', 'body'})
@classmethod
def get_issue_checks_metadata(cls):
return FunctionMetadata.from_function(
cls.check_issue_reference,
omit={'self', 'body'})
@classmethod
def get_metadata(cls):
return FunctionMetadata.merge(
FunctionMetadata.from_function(
cls.run,
omit={'self', 'dependency_results'}),
cls.get_shortlog_checks_metadata(),
cls.get_body_checks_metadata(),
cls.get_issue_checks_metadata())
@staticmethod
def get_host_from_remotes():
"""
Retrieve the first host from the list of git remotes.
"""
remotes, _ = run_shell_command(
"git config --get-regex '^remote.*.url$'")
remotes = [url.split()[-1] for url in remotes.splitlines()]
if len(remotes) == 0:
return None
url = remotes[0]
if 'git@' in url:
netloc = re.findall(r'@(\S+):', url)[0]
else:
netloc = urlparse(url)[1]
return netloc.split('.')[0]
def run(self, allow_empty_commit_message: bool = False, **kwargs):
"""
Check the current git commit message at HEAD.
This bear ensures automatically that the shortlog and body do not
exceed a given line-length and that a newline lies between them.
:param allow_empty_commit_message: Whether empty commit messages are
allowed or not.
"""
with change_directory(self.get_config_dir() or os.getcwd()):
stdout, stderr = run_shell_command('git log -1 --pretty=%B')
if stderr:
self.err('git:', repr(stderr))
return
stdout = stdout.rstrip('\n')
pos = stdout.find('\n')
shortlog = stdout[:pos] if pos != -1 else stdout
body = stdout[pos+1:] if pos != -1 else ''
if len(stdout) == 0:
if not allow_empty_commit_message:
yield Result(self, 'HEAD commit has no message.')
return
yield from self.check_shortlog(
shortlog,
**self.get_shortlog_checks_metadata().filter_parameters(kwargs))
yield from self.check_body(
body,
**self.get_body_checks_metadata().filter_parameters(kwargs))
yield from self.check_issue_reference(
body,
**self.get_issue_checks_metadata().filter_parameters(kwargs))
def check_shortlog(self, shortlog,
shortlog_length: int=50,
shortlog_regex: str='',
shortlog_trailing_period: bool=None,
shortlog_imperative_check: bool=True,
shortlog_wip_check: bool=True):
"""
Checks the given shortlog.
:param shortlog: The shortlog message string.
:param shortlog_length: The maximum length of the shortlog.
The newline character at end does not
count to the length.
:param shortlog_regex: A regex to check the shortlog with.
:param shortlog_trailing_period: Whether a dot shall be enforced at end
end or not (or ``None`` for "don't
care").
:param shortlog_wip_check: Whether a "WIP" in the shortlog text
should yield a result or not.
"""
diff = len(shortlog) - shortlog_length
if diff > 0:
yield Result(self,
'Shortlog of the HEAD commit contains {} '
'character(s). This is {} character(s) longer than '
'the limit ({} > {}).'.format(
len(shortlog), diff,
len(shortlog), shortlog_length))
if (shortlog[-1] != '.') == shortlog_trailing_period:
yield Result(self,
'Shortlog of HEAD commit contains no period at end.'
if shortlog_trailing_period else
'Shortlog of HEAD commit contains a period at end.')
if shortlog_regex:
match = re.fullmatch(shortlog_regex, shortlog)
if not match:
yield Result(
self,
'Shortlog of HEAD commit does not match given regex:'
' {regex}'.format(regex=shortlog_regex))
if shortlog_imperative_check:
colon_pos = shortlog.find(':')
shortlog = (shortlog[colon_pos + 1:]
if colon_pos != -1
else shortlog)
has_flaws = self.check_imperative(shortlog)
if has_flaws:
bad_word = has_flaws[0]
yield Result(self,
"Shortlog of HEAD commit isn't in imperative "
"mood! Bad words are '{}'".format(bad_word))
if shortlog_wip_check:
if 'wip' in shortlog.lower()[:4]:
yield Result(
self,
'This commit seems to be marked as work in progress and '
'should not be used in production. Treat carefully.')
def check_imperative(self, paragraph):
"""
Check the given sentence/s for Imperatives.
:param paragraph:
The input paragraph to be tested.
:return:
A list of tuples having 2 elements (invalid word, parts of speech)
or an empty list if no invalid words are found.
"""
words = nltk.word_tokenize(nltk.sent_tokenize(paragraph)[0])
# VBZ : Verb, 3rd person singular present, like 'adds', 'writes'
# etc
# VBD : Verb, Past tense , like 'added', 'wrote' etc
# VBG : Verb, Present participle, like 'adding', 'writing'
word, tag = nltk.pos_tag(['I'] + words)[1:2][0]
if(tag.startswith('VBZ') or
tag.startswith('VBD') or
tag.startswith('VBG') or
word.endswith('ing')): # Handle special case for VBG
return (word, tag)
else:
return None
def check_body(self, body,
body_line_length: int=72,
force_body: bool=False,
ignore_length_regex: typed_list(str)=(),
body_regex: str=None):
"""
Checks the given commit body.
:param body: The body of the commit message of HEAD.
:param body_line_length: The maximum line-length of the body. The
newline character at each line end does not
count to the length.
:param force_body: Whether a body shall exist or not.
:param ignore_length_regex: Lines matching each of the regular
expressions in this list will be ignored.
:param body_regex: If provided, checks the presence of regex
in the commit body.
"""
if len(body) == 0:
if force_body:
yield Result(self, 'No commit message body at HEAD.')
return
if body[0] != '\n':
yield Result(self, 'No newline found between shortlog and body at '
'HEAD commit. Please add one.')
return
if body_regex and not re.fullmatch(body_regex, body.strip()):
yield Result(self, 'No match found in commit message for the '
'regular expression provided: %s' % body_regex)
body = body.splitlines()
ignore_regexes = [re.compile(regex) for regex in ignore_length_regex]
if any((len(line) > body_line_length and
not any(regex.search(line) for regex in ignore_regexes))
for line in body[1:]):
yield Result(self, 'Body of HEAD commit contains too long lines. '
'Commit body lines should not exceed {} '
'characters.'.format(body_line_length))
def check_issue_reference(self, body,
body_close_issue: bool=False,
body_close_issue_full_url: bool=False,
body_close_issue_on_last_line: bool=False,
body_enforce_issue_reference: bool=False):
"""
Check for matching issue related references and URLs.
:param body:
Body of the commit message of HEAD.
:param body_close_issue:
GitHub and GitLab support auto closing issues with
commit messages. When enabled, this checks for matching keywords
in the commit body by retrieving host information from git
configuration. By default, if none of ``body_close_issue_full_url``
and ``body_close_issue_on_last_line`` are enabled, this checks for
presence of short references like ``closes #213``.
Otherwise behaves according to other chosen flags.
More on keywords follows.
[GitHub](https://help.github.com/articles/closing-issues-via-commit-messages/)
[GitLab](https://docs.gitlab.com/ce/user/project/issues/automatic_issue_closing.html)
:param body_close_issue_full_url:
Checks the presence of issue close reference with a full URL
related to some issue. Works along with ``body_close_issue``.
:param body_close_issue_on_last_line:
When enabled, checks for issue close reference presence on the
last line of the commit body. Works along with
``body_close_issue``.
:param body_enforce_issue_reference:
Whether to enforce presence of issue reference in the body of
commit message.
"""
if not body_close_issue:
return
host = self.get_host_from_remotes()
if host not in self.SUPPORTED_HOST_KEYWORD_REGEX:
return
if body_close_issue_on_last_line:
body = body.splitlines()[-1]
result_message = ('Body of HEAD commit does not contain any {} '
'reference in the last line.')
else:
result_message = ('Body of HEAD commit does not contain any {} '
'reference.')
if body_close_issue_full_url:
result_info = 'full issue'
issue_ref_regex = (
r'https?://{}\S+/issues/(\S+)'.format(re.escape(host)))
else:
result_info = 'issue'
issue_ref_regex = r'(?:\w+/\w+)?#(\S+)'
concat_regex = '|'.join(kw for kw in self.CONCATENATION_KEYWORDS)
compiled_joint_regex = re.compile(
r'(?:{0})\s+' # match issue related keywords,
# eg: fix, closes etc.
r'((?:\S(?!{1}))*\S' # match links/tags
# eg: fix #123, fix https://github.com
r'(?:\s*(?:{1})\s*' # match conjunctions like ',','and'
r'(?!{0})' # reject if new keywords appear
r'(?:\S(?!{1}))*\S)*)' # match links/tags followed after
# conjunctions if any
r''.format(
self.SUPPORTED_HOST_KEYWORD_REGEX[host],
concat_regex))
matches = compiled_joint_regex.findall(body)
if body_enforce_issue_reference and len(matches) == 0:
yield Result(self, result_message.format(result_info))
return
compiled_issue_ref_regex = re.compile(issue_ref_regex)
compiled_issue_no_regex = re.compile(r'[1-9][0-9]*')
compiled_concat_regex = re.compile(
r'\s*(?:{})\s*'.format(concat_regex))
for match in matches:
for issue in re.split(compiled_concat_regex, match):
reference = compiled_issue_ref_regex.fullmatch(issue)
if not reference:
yield Result(self, 'Invalid {} reference: '
'{}'.format(result_info, issue))
elif not compiled_issue_no_regex.fullmatch(reference.group(1)):
yield Result(self, 'Invalid issue number: '
'{}'.format(issue))
| python |
import dnslib.server
import dnslib
import time
import binascii
import struct
NAME_LIMIT_HARD = 63
A = ord("A")
Z = ord("Z")
a = ord("a")
z = ord("z")
ZERO = ord("0")
FIVE = ord("5")
ir1 = lambda c: c <= Z and c >= A
ir2 = lambda c: c <= z and c >= a
ir3 = lambda c: c <= FIVE and c >= ZERO
BASE32_SRC = b"abcdefghijklmnopqrstuvwxyz012345"
# q: why not use python's base64 module?
# a: <+irungentoo> notsecure, I told you we should have used standard base32
# <notsecure> Jfreegman, irungentoo wanted to use a-z,2-7 for base32,
# I chose a-z,0-5
# <notsecure> he said it would fuck with people using standard base32
# functions
def notsecure32_decode(src):
ret = []
bits = 0
op = 0
for char in (ord(s) for s in src):
if ir1(char):
char -= A
elif ir2(char):
char -= a
elif ir3(char):
char = (char - ZERO + 26)
else:
raise ValueError("this is an error apparently")
op = (op | (char << bits)) % 256;
bits += 5;
if bits >= 8:
bits -= 8
ret.append(op)
op = (char >> (5 - bits)) % 256;
return bytes(ret)
# TODO optimize
def notsecure32_encode(src):
sl = len(src)
ret = []
bits = 0
i = 0
while(i < sl):
c1 = src[i]
try:
c2 = src[i + 1]
except IndexError:
c2 = 0
a = BASE32_SRC[((c1 >> bits) | (c2 << (8 - bits))) & 0x1F]
ret.append(a)
bits += 5
if bits >= 8:
bits -= 8
i += 1
return bytes(ret)
class ToxResolver(dnslib.server.BaseResolver):
def __init__(self, cryptocore, store, cfg):
self.cryptocore = cryptocore
self.store = store
self.ttl = cfg["dns_record_ttl"]
self.ireg = cfg["registration_domain"]
self.home_addresses = cfg.get("home_addresses")
self.home_addresses_6 = cfg.get("home_addresses_6")
if not self.ireg.endswith("."):
self.ireg = "".join((self.ireg, "."))
self.auth = cfg["dns_authority_name"]
self.soa_rd = dnslib.SOA(cfg["dns_authority_name"],
cfg["dns_hostmaster"].replace("@", "."))
self.soa = dnslib.RR("_tox.{0}".format(self.ireg), 6, ttl=86400,
rdata=self.soa_rd)
def update_soa(self):
self.soa_rd.times = (int(time.strftime("%Y%m%d99")), 3600, 600, 86400,
self.ttl)
def resolve(self, request, handler):
print(repr(request.get_q().qtype))
question = request.get_q()
req_name = str(question.get_qname())
# TXT = 16
reply = request.reply()
suffix = "._tox.{0}".format(self.ireg)
if question.qtype != 16 and not req_name.endswith(self.ireg):
reply.header.rcode = dnslib.RCODE.NXDOMAIN
return reply
if question.qtype == 16:
if not req_name.endswith(suffix):
reply.header.rcode = dnslib.RCODE.NXDOMAIN
return reply
user_name = req_name[:req_name.rfind(suffix)]
if len(user_name) > NAME_LIMIT_HARD and user_name[0] == "_":
encrypted = user_name.replace(".", "")[1:]
try:
b = notsecure32_decode(encrypted)
nonce = b[:4] + (b"\0" * 20)
ck = b[4:36]
payload = b[36:]
name = self.cryptocore.dsrep_decode_name(ck, nonce, payload)
except Exception:
print("error >_<")
reply.header.rcode = dnslib.RCODE.NXDOMAIN
return reply
rec = self.store.get(name.decode("utf8"))
if not rec:
reply.header.rcode = dnslib.RCODE.NXDOMAIN
return reply
base = b"v=tox3;id="
if rec.pin:
r_payload = "{0}{1}{2}".format(rec.public_key, rec.pin,
rec.checksum)
else:
r_payload = "{0}00000000{1}".format(rec.public_key,
rec.checksum)
msg = binascii.unhexlify(r_payload)
nonce_reply = b[:4] + b"\x01" + (b"\0" * 19)
ct = self.cryptocore.dsrec_encrypt_key(ck, nonce_reply, msg)
key_part = notsecure32_encode(ct)
reply.add_answer(dnslib.RR(req_name, 16, ttl=0,
rdata=dnslib.TXT(b"".join((base, key_part)))))
return reply
else:
rec = self.store.get(user_name)
if not rec:
reply.header.rcode = dnslib.RCODE.NXDOMAIN
return reply
else:
reply.add_answer(dnslib.RR(req_name, 16, ttl=0,
rdata=dnslib.TXT(rec.record(0)
.encode("utf8"))))
return reply
elif question.qtype == 6:
self.update_soa()
reply.add_answer(self.soa)
return reply
elif question.qtype == 2:
reply.add_answer(dnslib.RR(req_name, 2, ttl=86400,
rdata=dnslib.NS(self.auth.encode("utf8"))
))
return reply
elif question.qtype == 1 and self.home_addresses:
for ip in self.home_addresses:
reply.add_answer(dnslib.RR(req_name, 1, ttl=3600,
rdata=dnslib.A(ip)))
elif question.qtype == 28 and self.home_addresses_6:
for ip in self.home_addresses_6:
reply.add_answer(dnslib.RR(req_name, 28, ttl=3600,
rdata=dnslib.AAAA(ip)))
else:
reply.header.rcode = dnslib.RCODE.NXDOMAIN
return reply
return reply
# TODO tornado ioloop integration
def server(cryptocore, store, cfg):
return dnslib.server.DNSServer(ToxResolver(cryptocore, store, cfg),
port=53, address=cfg["dns_listen_addr"],
logger=None,
tcp=False)
| python |
# Generated by Django 3.1.11 on 2021-05-20 12:58
from django.conf import settings
import django.core.validators
from django.db import migrations, models
import django.db.models.deletion
import server.utils.model_fields
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='Activity',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('slug', models.CharField(default=server.utils.model_fields.random_slug, max_length=40, unique=True)),
('name', models.CharField(max_length=35)),
('target_audience', models.JSONField()),
('domain', models.CharField(max_length=55)),
('description', models.CharField(default='', max_length=400)),
('contact_name', models.CharField(default='', max_length=60)),
('logo', models.ImageField(blank=True, null=True, upload_to='')),
('phone_number', models.CharField(blank=True, max_length=15, validators=[django.core.validators.RegexValidator(message='phone number must be between 9-15 digits', regex='^\\d{9,15}$')])),
],
),
migrations.CreateModel(
name='Organization',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('slug', models.CharField(default=server.utils.model_fields.random_slug, max_length=40, unique=True)),
('email', models.EmailField(max_length=254)),
('description', models.CharField(max_length=250)),
('website_url', models.URLField()),
('name', models.CharField(max_length=50)),
('goal', models.CharField(max_length=250)),
('year_founded', models.CharField(blank=True, max_length=4, null=True)),
('status', models.CharField(max_length=50)),
('target_audience', models.JSONField()),
('number_of_employees', models.PositiveIntegerField()),
('nuber_of_members', models.PositiveIntegerField()),
('number_of_volunteers', models.PositiveIntegerField()),
('location_lon', models.DecimalField(decimal_places=6, max_digits=9)),
('location_lat', models.DecimalField(decimal_places=6, max_digits=9)),
('address_city', models.CharField(max_length=150)),
('address_street', models.CharField(max_length=150)),
('address_house_num', models.CharField(max_length=4)),
('address_zipcode', models.CharField(max_length=9)),
('cities', models.JSONField()),
('districts', models.JSONField()),
('union_type', models.CharField(max_length=50)),
],
),
migrations.CreateModel(
name='OrganizationMemeber',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('organization', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='organizations.organization')),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel(
name='ActivityMedia',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('slug', models.CharField(default=server.utils.model_fields.random_slug, max_length=40, unique=True)),
('name', models.CharField(max_length=40)),
('image_url', models.ImageField(blank=True, null=True, upload_to='')),
('video_url', models.URLField(blank=True, null=True)),
('activity', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='rich_media', to='organizations.activity')),
],
),
migrations.AddField(
model_name='activity',
name='originization',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='organizations.organization'),
),
]
| python |
from distutils.command.install import install as install_orig
from distutils.errors import DistutilsExecError
from setuptools import setup
class install(install_orig):
def run(self):
try:
self.spawn(['make', 'install'])
except DistutilsExecError:
self.warn('listing directory failed')
super().run()
| python |