hexsha
stringlengths
40
40
size
int64
5
2.06M
ext
stringclasses
10 values
lang
stringclasses
1 value
max_stars_repo_path
stringlengths
3
248
max_stars_repo_name
stringlengths
5
125
max_stars_repo_head_hexsha
stringlengths
40
78
max_stars_repo_licenses
listlengths
1
10
max_stars_count
int64
1
191k
max_stars_repo_stars_event_min_datetime
stringlengths
24
24
max_stars_repo_stars_event_max_datetime
stringlengths
24
24
max_issues_repo_path
stringlengths
3
248
max_issues_repo_name
stringlengths
5
125
max_issues_repo_head_hexsha
stringlengths
40
78
max_issues_repo_licenses
listlengths
1
10
max_issues_count
int64
1
67k
max_issues_repo_issues_event_min_datetime
stringlengths
24
24
max_issues_repo_issues_event_max_datetime
stringlengths
24
24
max_forks_repo_path
stringlengths
3
248
max_forks_repo_name
stringlengths
5
125
max_forks_repo_head_hexsha
stringlengths
40
78
max_forks_repo_licenses
listlengths
1
10
max_forks_count
int64
1
105k
max_forks_repo_forks_event_min_datetime
stringlengths
24
24
max_forks_repo_forks_event_max_datetime
stringlengths
24
24
content
stringlengths
5
2.06M
avg_line_length
float64
1
1.02M
max_line_length
int64
3
1.03M
alphanum_fraction
float64
0
1
count_classes
int64
0
1.6M
score_classes
float64
0
1
count_generators
int64
0
651k
score_generators
float64
0
1
count_decorators
int64
0
990k
score_decorators
float64
0
1
count_async_functions
int64
0
235k
score_async_functions
float64
0
1
count_documentation
int64
0
1.04M
score_documentation
float64
0
1
ce456a679b725d44ec91f64a8df14df4d86ae155
1,918
py
Python
src/python/grpcio_tests/tests/interop/_intraop_test_case.py
txl0591/grpc
8b732dc466fb8a567c1bca9dbb84554d29087395
[ "Apache-2.0" ]
117
2017-10-02T21:34:35.000Z
2022-03-02T01:49:03.000Z
src/python/grpcio_tests/tests/interop/_intraop_test_case.py
txl0591/grpc
8b732dc466fb8a567c1bca9dbb84554d29087395
[ "Apache-2.0" ]
4
2017-10-03T22:45:30.000Z
2018-09-27T07:31:00.000Z
src/python/grpcio_tests/tests/interop/_intraop_test_case.py
txl0591/grpc
8b732dc466fb8a567c1bca9dbb84554d29087395
[ "Apache-2.0" ]
24
2017-10-31T12:14:15.000Z
2021-12-11T10:07:46.000Z
# Copyright 2015 gRPC authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Common code for unit tests of the interoperability test code.""" from tests.interop import methods class IntraopTestCase(object): """Unit test methods. This class must be mixed in with unittest.TestCase and a class that defines setUp and tearDown methods that manage a stub attribute. """ def testEmptyUnary(self): methods.TestCase.EMPTY_UNARY.test_interoperability(self.stub, None) def testLargeUnary(self): methods.TestCase.LARGE_UNARY.test_interoperability(self.stub, None) def testServerStreaming(self): methods.TestCase.SERVER_STREAMING.test_interoperability(self.stub, None) def testClientStreaming(self): methods.TestCase.CLIENT_STREAMING.test_interoperability(self.stub, None) def testPingPong(self): methods.TestCase.PING_PONG.test_interoperability(self.stub, None) def testCancelAfterBegin(self): methods.TestCase.CANCEL_AFTER_BEGIN.test_interoperability(self.stub, None) def testCancelAfterFirstResponse(self): methods.TestCase.CANCEL_AFTER_FIRST_RESPONSE.test_interoperability( self.stub, None) def testTimeoutOnSleepingServer(self): methods.TestCase.TIMEOUT_ON_SLEEPING_SERVER.test_interoperability( self.stub, None)
36.884615
80
0.728363
1,235
0.6439
0
0
0
0
0
0
796
0.415016
ce460a49da25a43c7d3e4ff3e64726a1574194b1
604
py
Python
scripts/add_vf_ids.py
rajbot/vaccinebot
9b7c13eb248e92a248dbc0e3e9de6d4dc7a2c20a
[ "MIT" ]
2
2021-02-07T05:06:09.000Z
2021-03-02T18:23:07.000Z
scripts/add_vf_ids.py
rajbot/vaccinebot
9b7c13eb248e92a248dbc0e3e9de6d4dc7a2c20a
[ "MIT" ]
null
null
null
scripts/add_vf_ids.py
rajbot/vaccinebot
9b7c13eb248e92a248dbc0e3e9de6d4dc7a2c20a
[ "MIT" ]
null
null
null
#!/usr/bin/env python from airtable import Airtable import csv import os for var in ["AIRTABLE_API_KEY", "AIRTABLE_BASE_ID"]: if os.environ.get(var) is None: sys.exit(f"Must set {var} env var!") api_key = os.environ.get("AIRTABLE_API_KEY") base_id = os.environ.get("AIRTABLE_BASE_ID") airtable = Airtable(base_id, "Locations", api_key) path = sys.argv[1] with open(path) as csvfile: reader = csv.DictReader(csvfile) for row in reader: print("adding", row) fields = {"vaccinefinder_location_id": row["vaccinefinder_id"]} airtable.update(row["id"], fields)
26.26087
71
0.688742
0
0
0
0
0
0
0
0
187
0.309603
ce462fe45d9f73cc50c3b487d621d5b2ad86a06b
99
py
Python
pubmedpy/__init__.py
dhimmel/pubmedpy
9d716768f5ab798ec448154588e4fd99afd7584a
[ "BlueOak-1.0.0" ]
7
2019-11-13T09:14:19.000Z
2022-03-09T01:35:06.000Z
pubmedpy/__init__.py
dhimmel/pubmedpy
9d716768f5ab798ec448154588e4fd99afd7584a
[ "BlueOak-1.0.0" ]
2
2020-08-24T15:05:57.000Z
2020-10-21T04:12:56.000Z
pubmedpy/__init__.py
dhimmel/pubmedpy
9d716768f5ab798ec448154588e4fd99afd7584a
[ "BlueOak-1.0.0" ]
1
2021-02-18T00:01:09.000Z
2021-02-18T00:01:09.000Z
""" # Utilities for interacting with NCBI EUtilities relating to PubMed """ __version__ = "0.0.1"
16.5
67
0.717172
0
0
0
0
0
0
0
0
82
0.828283
ce46ad7566bbdce61b2ab0578c3f8020ac4af53c
945
py
Python
config/logging.py
qgerome/openhexa-app
8c9377b2ad972121d8e9575f5d52420212b52ed4
[ "MIT" ]
4
2021-07-19T12:53:21.000Z
2022-01-26T17:45:02.000Z
config/logging.py
qgerome/openhexa-app
8c9377b2ad972121d8e9575f5d52420212b52ed4
[ "MIT" ]
20
2021-05-17T12:27:06.000Z
2022-03-30T11:35:26.000Z
config/logging.py
qgerome/openhexa-app
8c9377b2ad972121d8e9575f5d52420212b52ed4
[ "MIT" ]
2
2021-09-07T04:19:59.000Z
2022-02-08T15:33:29.000Z
import json import sys import traceback from logging import Handler # Specific logging module for GCP, use json to serialize output -> work better for GKE # Can be used for further customization class GCPHandler(Handler): def emit(self, record): try: message = self.format(record) # to in JSON, be friendly with GCP print(json.dumps(message), file=sys.stderr) sys.stderr.flush() except Exception: pass # don't loop def format(self, record): if record.exc_info is not None: type, _, tb = record.exc_info message = "".join(traceback.format_tb(tb)) + "\n%s\n%s" % ( record.getMessage(), type, ) else: message = record.msg % record.args return { "severity": record.levelname, "message": "%s: %s" % (record.name, message), }
28.636364
86
0.557672
746
0.789418
0
0
0
0
0
0
210
0.222222
ce4a42074a7c0b3a3d6615721ca72bb43e10e32b
16,095
py
Python
sublimeText3/Packages/SublimeCodeIntel/libs/codeintel2/tdparser.py
MoAnsir/dot_file_2017
5f67ef8f430416c82322ab7e7e001548936454ff
[ "MIT" ]
2
2018-04-24T10:02:26.000Z
2019-06-02T13:53:31.000Z
Data/Packages/SublimeCodeIntel/libs/codeintel2/tdparser.py
Maxize/Sublime_Text_3
be620476b49f9a6ce2ca2cfe825c4e142e7e82b9
[ "Apache-2.0" ]
1
2016-02-10T09:50:09.000Z
2016-02-10T09:50:09.000Z
Packages/SublimeCodeIntel/libs/codeintel2/tdparser.py
prisis/sublime-text-packages
99ae8a5496613e27a75e5bd91723549b21476e60
[ "MIT" ]
2
2019-04-11T04:13:02.000Z
2019-06-02T13:53:33.000Z
""" A simple Top-Down Python expression parser. This parser is based on the "Simple Top-Down Parsing in Python" article by Fredrik Lundh (http://effbot.org/zone/simple-top-down-parsing.htm) These materials could be useful for understanding ideas behind the Top-Down approach: * Top Down Operator Precedence -- Douglas Crockford http://javascript.crockford.com/tdop/tdop.html * Top-Down operator precedence parsing -- Eli Benderski http://eli.thegreenplace.net/2010/01/02/top-down-operator-precedence-parsing/ * Top down operator precedence -- Vaughan R. Pratt http://portal.acm.org/citation.cfm?doid=512927.512931 This implementation is a subject to change as it is very premature. """ import re import io as sio import tokenize class ParseError(Exception): pass type_map = {tokenize.NUMBER: "(literal)", tokenize.STRING: "(literal)", tokenize.OP: "(operator)", tokenize.NAME: "(name)"} def gen_python_tokens(source): stream = tokenize.generate_tokens(sio.StringIO(source).readline) for token, value, begin, end in (t[:4] for t in stream): if token in type_map: yield type_map[token], value, begin, end elif token == tokenize.NEWLINE: continue elif token == tokenize.ENDMARKER: break else: raise ParseError("Syntax error at (%r) in text (%r) -- " "unexpected token (%r)" % (value, source, tokenize.tok_name[token])) yield "(end)", "(end)", None, None class Symbol(object): id = None value = None first = second = third = None def __init__(self, parser, begin, end): self.parser = parser self.begin = begin self.end = end def nud(self): raise ParseError("Syntax error (%r)" % self) def led(self, left): raise ParseError("Unknown operator (%r)" % self) def py(self): if self.id[0] != "(": return self.id else: return self.value def __repr__(self): if self.id in ("(name)", "(literal)"): return "(%s %s)" % (self.id[1:-1], self.value) children = (str(item) for item in (self.first, self.second, self.third) if item is not None) out = " ".join((self.id,) + tuple(children)) return "(" + out + ")" @property def token(self): return self.parser.token @token.setter def token(self, token): self.parser.token = token @property def next(self): return self.parser.__next__ @property def expression(self): return self.parser.expression @property def advance(self): return self.parser.advance class Parser(object): token = None next = None def __init__(self, grammar=None): self.grammar = grammar or self.grammar def symbol(self, id, bp=0): return self.grammar.symbol(d, bp) def expression(self, rbp=0): t = self.token self.token = next(self) left = t.nud() while rbp < self.token.lbp: t = self.token self.token = next(self) left = t.led(left) return left def advance(self, id=None): if id and self.token.id != id: raise ParseError("Expected '%r', got '%r'" % (id, self.token)) self.token = next(self) def gen_python_symbols(self, source): for id, value, begin, end in gen_python_tokens(source): if id == "(literal)": symbol = self.grammar.get_symbol(id) inst = symbol(self, begin, end) inst.value = value else: symbol = self.grammar.get_symbol(value) if symbol: inst = symbol(self, begin, end) elif id == "(name)": symbol = self.grammar.get_symbol(id) inst = symbol(self, begin, end) inst.value = value else: raise ParseError("Unknown operator (%r)" % id) yield inst def parse(self, source): self.next = self.gen_python_symbols(source).__next__ self.token = next(self) result = self.expression() if self.token.id != "(end)": raise ParseError("Expected end, got '%r'" % self.token) return result class Grammar(object): symbol_table = {} def __init__(self): class proto(Symbol): pass self.proto = proto def common(self, fn): setattr(self.proto, fn.__name__, fn) return fn def method(self, id, bp=0): sym = self.symbol(id, bp) assert issubclass(sym, Symbol) def bind(fn): setattr(sym, fn.__name__, fn) return bind def symbol(self, id, bp=0): if id in self.symbol_table: sym = self.symbol_table[id] else: # can this be done with partials? class sym(self.proto): pass sym.__name__ = "symbol-" + id sym.id = id sym.lbp = bp self.symbol_table[id] = sym sym.lbp = max(bp, sym.lbp) return sym def get_symbol(self, id): return self.symbol_table.get(id) def infix(self, id, bp): @self.method(id, bp) def led(self, left): self.first = left self.second = self.expression(bp) return self @self.method(id, bp) def py(self): return "%s %s %s" % (self.first.py(), self.id, self.second.py()) def prefix(self, id, bp): @self.method(id, bp) def nud(self): self.first = self.expression(bp) self.second = None return self @self.method(id, bp) def py(self): return "%s%s" % (self.id, self.first.py()) def infix_r(self, id, bp): @self.method(id, bp) def led(self, left): self.first = left self.second = self.expression(bp-1) return self @self.method(id, bp) def py(self): return "%s %s %s" % (self.first.py(), self.value, self.second.py()) def constant(self, id): @self.method(id) def nud(self): self.id = "(literal)" self.value = id return self def arg_list_py(args): buf = [] for name, value, type in args: if value: buf.append("%s=%s" % (name.py(), value.py())) else: buf.append(name.py()) return ", ".join(buf) def call_list_py(args): buf = [] for name, value in args: value_py = value and value.py() or '' if name: if name.id in ("*", "**"): arg = name.id + value.py() else: arg = "%s=%s" % (name.id, value_py) else: arg = value_py buf.append(arg) return ", ".join(buf) def py_expr_grammar(): self = Grammar() self.symbol("lambda", 20) self.symbol(":", 10) self.symbol("if", 20) self.symbol("else") self.infix_r("or", 30) self.infix_r("and", 40) self.prefix("not", 50) self.infix("in", 60) self.infix("not", 60) # in, not in self.infix("is", 60) # is, is not self.infix("<", 60) self.infix("<=", 60) self.infix(">", 60) self.infix(">=", 60) self.infix("<>", 60) self.infix("!=", 60) self.infix("==", 60) self.infix("|", 70) self.infix("^", 80) self.infix("&", 90) self.infix("<<", 100) self.infix(">>", 100) self.infix("+", 110) self.infix("-", 110) self.infix("*", 120) self.infix("/", 120) self.infix("//", 120) self.infix("%", 120) self.prefix("-", 130) self.prefix("+", 130) self.prefix("~", 130) self.infix_r("**", 140) self.symbol(".", 150) self.symbol("[", 150) self.symbol("]") self.symbol("(", 150) self.symbol(")") self.symbol(",") self.symbol("=") self.symbol("{", 150) self.symbol("}") self.symbol("(literal)").nud = lambda self: self self.symbol("(name)").nud = lambda self: self self.symbol("(end)") self.constant("None") self.constant("True") self.constant("False") @self.method("*") def py(self): if self.first: return "%s %s %s" % (self.first.py(), self.id, self.second.py()) else: return self.value @self.method("**") def py(self): if self.first: return "%s %s %s" % (self.first.py(), self.id, self.second.py()) else: return self.value @self.method("(") def nud(self): self.first = [] comma = False if self.token.id != ")": while 1: if self.token.id == ")": break self.first.append(self.expression()) if self.token.id == ",": comma = True self.advance(",") else: break self.advance(")") if not self.first or comma: return self # tuple else: return self.first[0] @self.method("(") def led(self, left): self.first = left self.second = [] if self.token.id != ")": while 1: name = None if self.token.id in ('*', '**'): name = self.token self.advance(self.token.id) value = self.expression() else: t = self.expression() if self.token.id == "=": if t.id != "(name)": raise ParseError("Expected a name, got '%r'" % arg) self.advance("=") name = t value = self.expression() else: value = t self.second.append((name, value)) if self.token.id != ",": break self.advance(",") self.advance(")") self.id = "(call)" return self @self.method("(") def py(self): if self.second: return "%s(%s)" % (self.first.py(), call_list_py(self.second)) else: return "(%s)" % ", ".join(i.py() for i in self.first) @self.method("if") def led(self, left): self.first = left self.second = self.expression() self.advance("else") self.third = self.expression() return self @self.method("if") def py(self): return "%s if %s else %s" % (self.first.py(), self.second.py(), self.third.py()) @self.method(".") def led(self, left): if self.token.id != "(name)": ParseError("Expected an attribute name, got '%r'" % self.token) self.first = left self.second = self.token self.advance() return self @self.method(".") def py(self): return "%s.%s" % (self.first.py(), self.second.py()) @self.method("[") def nud(self): self.first = [] while self.token.id != "]": self.first.append(self.expression()) if self.token.id == ",": self.advance(",") else: break self.advance("]") return self @self.method("[") def led(self, left): self.id = "(index)" self.first = left self.second = self.expression() self.advance("]") return self @self.method("[") def py(self): if self.second: return "%s[%s]" % (self.first, ", ".join(i.py() for i in self.second)) else: return "[%s]" % ", ".join(i.py() for i in self.first) @self.method("{") def nud(self): self.first = [] while self.token.id != "}": self.first.append(self.expression()) self.advance(":") self.first.append(self.expression()) if self.token.id == ",": self.advance(",") else: break self.advance("}") return self @self.method("{") def py(self): return "{%s}" % (", ".join("%s: %s" % (i[0].py(), i[1].py()) for i in self.first)) @self.method("lambda") def nud(self): if self.token.id != ":": self.first = self.argument_list(in_lambda=True) else: self.first = [] self.advance(":") self.second = self.expression() return self @self.method("lambda") def py(self): return "lambda %s: %s" % (arg_list_py(self.first), self.second.py()) @self.method("not") def led(self, left): if self.token.id != "in": raise ParseError("Expected 'in', got '%r'" % self.token) self.advance() self.id = "not in" self.first = left self.second = self.expression(60) return self @self.method("is") def led(self, left): if self.token.id == "not": self.advance() self.id = "is not" self.first = left self.second = self.expression(60) return self @self.common def advance_name(self): if self.token.id != "(name)": ParseError("Expected an argument name, got '%r'" % self.token) t = self.token self.advance() return t @self.common def argument_list(self, in_lambda=False): arglist = [] while 1: val = None type = None check_annotation = False check_default_value = False if self.token.id == "*": arg = self.token self.advance("*") if self.token.id == ",": arg.value = "*" else: arg = self.advance_name() arg.value = "*" + arg.value check_annotation = True elif self.token.id == "**": self.advance("**") arg = self.advance_name() arg.value = "**" + arg.value check_annotation = True else: arg = self.advance_name() check_annotation = True check_default_value = True if check_default_value and self.token.id == "=": self.advance("=") val = self.expression() check_default_value = False if not in_lambda: if check_annotation and self.token.id == ":": self.advance(":") type = self.expression() if check_default_value and self.token.id == "=": self.advance("=") val = self.expression() if self.token.id == "->": self.advance("->") self.expression() arglist.append((arg, val, type)) if self.token.id == ",": self.advance(",") else: break return arglist return self class PyExprParser(Parser): grammar = py_expr_grammar() def parse_bare_arglist(self, source): self.next = self.gen_python_symbols(source.strip()).__next__ self.token = next(self) arglist = self.token.argument_list() if self.token.id != "(end)": raise ParseError("Expected end, got '%r'" % self.token) return arglist if __name__ == '__main__': import sys if len(sys.argv) < 2: print("Usage: tdparser.py filename") parser = PyExprParser() res = parser.parse_bare_arglist(file(sys.argv[1]).read()) print(res)
27.46587
82
0.491892
5,275
0.327742
1,361
0.08456
8,223
0.510904
0
0
1,929
0.119851
ce4ab2aff6e500e8239e651be0e0851e93f8d29c
597
py
Python
atcoder/abc/abc002_d.py
knuu/competitive-programming
16bc68fdaedd6f96ae24310d697585ca8836ab6e
[ "MIT" ]
1
2018-11-12T15:18:55.000Z
2018-11-12T15:18:55.000Z
atcoder/abc/abc002_d.py
knuu/competitive-programming
16bc68fdaedd6f96ae24310d697585ca8836ab6e
[ "MIT" ]
null
null
null
atcoder/abc/abc002_d.py
knuu/competitive-programming
16bc68fdaedd6f96ae24310d697585ca8836ab6e
[ "MIT" ]
null
null
null
from copy import deepcopy N, M = map(int, input().split()) E = [[] for _ in range(N)] for i in range(M): x, y = map(lambda x:int(x)-1, input().split()) E[x].append(y) E[y].append(x) ans = 0 for mask in range(2**N): faction = '' for x in range(N): faction += '1' if mask&(1<<x) else '0' flag = True for cnt, i in enumerate(faction): if int(i) == 1: for j in range(cnt+1, N): if faction[j] == '1' and j not in E[cnt]: flag = False if flag: ans = max(ans, faction.count('1')) print(ans)
27.136364
57
0.494137
0
0
0
0
0
0
0
0
14
0.023451
ce4b095948b8f81b5b5833c6dcab9d8f5bd587a5
290
py
Python
advisor/api/urls.py
Sachin-c/api-test
c8242de24375149dcbc14e30b44d9a77d9771034
[ "MIT", "BSD-3-Clause" ]
null
null
null
advisor/api/urls.py
Sachin-c/api-test
c8242de24375149dcbc14e30b44d9a77d9771034
[ "MIT", "BSD-3-Clause" ]
null
null
null
advisor/api/urls.py
Sachin-c/api-test
c8242de24375149dcbc14e30b44d9a77d9771034
[ "MIT", "BSD-3-Clause" ]
null
null
null
from django.urls import path from advisor.api.views import ( # api_advisor_view, api_advisor_view_post, ) app_name = 'advisor' urlpatterns = [ path('admin/advisor/', api_advisor_view_post, name="post"), # path('user/<int:id>/advisor/', api_advisor_view, name="detail"), ]
22.307692
70
0.7
0
0
0
0
0
0
0
0
116
0.4
ce4b6a50f11f5cd0ce57c03afebe02596310a357
405
py
Python
src/utils/config.py
mlrepa/automate-ml-with-dvc
b54a2e4818a991362d304890828df70359bab84a
[ "MIT" ]
4
2021-04-11T17:30:14.000Z
2021-07-27T10:09:53.000Z
src/utils/config.py
mlrepa/automate-ml-with-dvc
b54a2e4818a991362d304890828df70359bab84a
[ "MIT" ]
null
null
null
src/utils/config.py
mlrepa/automate-ml-with-dvc
b54a2e4818a991362d304890828df70359bab84a
[ "MIT" ]
1
2021-09-05T04:15:07.000Z
2021-09-05T04:15:07.000Z
import box from typing import Text import yaml def load_config(config_path: Text) -> box.ConfigBox: """Loads yaml config in instance of box.ConfigBox. Args: config_path {Text}: path to config Returns: box.ConfigBox """ with open(config_path) as config_file: config = yaml.safe_load(config_file) config = box.ConfigBox(config) return config
20.25
54
0.659259
0
0
0
0
0
0
0
0
146
0.360494
ce4c0eaf1f91aac00dd03914e8def1ffd020858d
3,835
py
Python
python/methylnet/visualizations.py
hossein20s/dnaMethylation
eb2c4e14a6d32f6582f54fe39c62e83205f18665
[ "MIT" ]
26
2019-07-11T04:58:24.000Z
2022-02-15T19:31:48.000Z
python/methylnet/visualizations.py
hossein20s/dnaMethylation
eb2c4e14a6d32f6582f54fe39c62e83205f18665
[ "MIT" ]
5
2020-04-30T13:02:13.000Z
2022-03-02T16:41:47.000Z
python/methylnet/visualizations.py
hossein20s/dnaMethylation
eb2c4e14a6d32f6582f54fe39c62e83205f18665
[ "MIT" ]
8
2019-10-08T07:16:09.000Z
2022-03-11T23:17:27.000Z
import pandas as pd import numpy as np import networkx as nx import click import pickle from sklearn.preprocessing import LabelEncoder CONTEXT_SETTINGS = dict(help_option_names=['-h','--help'], max_content_width=90) @click.group(context_settings= CONTEXT_SETTINGS) @click.version_option(version='0.1') def visualize(): pass @visualize.command() @click.option('-t', '--training_curve_file', default='predictions/training_val_curve.p', show_default=True, help='Training and validation loss and learning curves.', type=click.Path(exists=False)) @click.option('-o', '--outputfilename', default='results/training_curve.png', show_default=True, help='Output image.', type=click.Path(exists=False)) @click.option('-vae', '--vae_train', is_flag=True, help='Plot VAE Training curves.') @click.option('-thr', '--threshold', default=1e10, help='Loss values get rid of greater than this.') def plot_training_curve(training_curve_file, outputfilename, vae_train, threshold): """Plot training curves as output from either the VAE training or VAE MLP.""" import os os.makedirs(outputfilename[:outputfilename.rfind('/')],exist_ok=True) import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt import seaborn as sns #from scipy.signal import find_peaks_cwt #from impyute import moving_window def impute_peaks(signal): signal=signal.values idxs=signal>=threshold signal[idxs]=np.nan """for idx in idxs: signal[idx]=(signal[idx-1]+signal[idx+1])/2 if idx else signal[idx+1]""" return signal def impute_df(df): df=df.apply(impute_peaks,axis=0).fillna(method='bfill',axis=0) print(df) return df sns.set(style="whitegrid") df=pd.DataFrame(pickle.load(open(training_curve_file,'rb'))) plt.subplots(1,3 if vae_train else 2,figsize=(15 if vae_train else 10,5)) plt.subplot(131 if vae_train else 121) if vae_train: df['loss']=df['recon_loss']+df['kl_loss'] df['val_loss']=df['val_recon_loss']+df['val_kl_loss'] sns.lineplot(data=impute_df(df[['loss','val_loss']+(['recon_loss','val_recon_loss'] if vae_train else [])]), palette="tab10", linewidth=2.5) plt.xlabel('Epoch') plt.ylabel('Loss') plt.tight_layout() plt.subplot(132 if vae_train else 122) sns.lineplot(data=df[['lr_vae', 'lr_mlp'] if not vae_train else ['lr']], palette="tab10", linewidth=2.5) plt.xlabel('Epoch') plt.ylabel('Learning Rate') if vae_train: plt.subplot(133) sns.lineplot(data=impute_df(df[['kl_loss','val_kl_loss']]), palette="tab10", linewidth=2.5) plt.xlabel('Epoch') plt.ylabel('Loss') plt.tight_layout() plt.savefig(outputfilename) @visualize.command() @click.option('-r', '--roc_curve_csv', default='results/Weighted_ROC.csv', show_default=True, help='Weighted ROC Curve.', type=click.Path(exists=False)) @click.option('-o', '--outputfilename', default='results/roc_curve.png', show_default=True, help='Output image.', type=click.Path(exists=False)) def plot_roc_curve(roc_curve_csv, outputfilename): """Plot ROC Curves from classification tasks; requires classification_report be run first.""" import os from rpy2.robjects.packages import importr import rpy2.robjects as robjects os.makedirs(outputfilename[:outputfilename.rfind('/')],exist_ok=True) tidyverse=importr('tidyverse') robjects.r("""function (in.csv, out.file.name) { df<-read.csv(in.csv) df %>% ggplot() + geom_line(aes(x = fpr, y = tpr,color=Legend)) + ggtitle('ROC Curve') + xlab('1-Specificity') + ylab('Sensitivity') ggsave(out.file.name) }""")(roc_curve_csv,outputfilename) ################# if __name__ == '__main__': visualize()
43.089888
196
0.681356
0
0
0
0
3,549
0.925424
0
0
1,380
0.359844
ce4d2974d0b31d80078e3f7458f018c00bbd3cf4
13,343
py
Python
tests/test_sagemaker/test_sagemaker_processing.py
gtourkas/moto
307104417b579d23d02f670ff55217a2d4a16bee
[ "Apache-2.0" ]
5,460
2015-01-01T01:11:17.000Z
2022-03-31T23:45:38.000Z
tests/test_sagemaker/test_sagemaker_processing.py
gtourkas/moto
307104417b579d23d02f670ff55217a2d4a16bee
[ "Apache-2.0" ]
4,475
2015-01-05T19:37:30.000Z
2022-03-31T13:55:12.000Z
tests/test_sagemaker/test_sagemaker_processing.py
gtourkas/moto
307104417b579d23d02f670ff55217a2d4a16bee
[ "Apache-2.0" ]
1,831
2015-01-14T00:00:44.000Z
2022-03-31T20:30:04.000Z
import boto3 from botocore.exceptions import ClientError import datetime import pytest from moto import mock_sagemaker from moto.sts.models import ACCOUNT_ID FAKE_ROLE_ARN = "arn:aws:iam::{}:role/FakeRole".format(ACCOUNT_ID) TEST_REGION_NAME = "us-east-1" class MyProcessingJobModel(object): def __init__( self, processing_job_name, role_arn, container=None, bucket=None, prefix=None, app_specification=None, network_config=None, processing_inputs=None, processing_output_config=None, processing_resources=None, stopping_condition=None, ): self.processing_job_name = processing_job_name self.role_arn = role_arn self.container = ( container or "683313688378.dkr.ecr.us-east-1.amazonaws.com/sagemaker-scikit-learn:0.23-1-cpu-py3" ) self.bucket = bucket or "my-bucket" self.prefix = prefix or "sagemaker" self.app_specification = app_specification or { "ImageUri": self.container, "ContainerEntrypoint": ["python3",], } self.network_config = network_config or { "EnableInterContainerTrafficEncryption": False, "EnableNetworkIsolation": False, } self.processing_inputs = processing_inputs or [ { "InputName": "input", "AppManaged": False, "S3Input": { "S3Uri": "s3://{}/{}/processing/".format(self.bucket, self.prefix), "LocalPath": "/opt/ml/processing/input", "S3DataType": "S3Prefix", "S3InputMode": "File", "S3DataDistributionType": "FullyReplicated", "S3CompressionType": "None", }, } ] self.processing_output_config = processing_output_config or { "Outputs": [ { "OutputName": "output", "S3Output": { "S3Uri": "s3://{}/{}/processing/".format( self.bucket, self.prefix ), "LocalPath": "/opt/ml/processing/output", "S3UploadMode": "EndOfJob", }, "AppManaged": False, } ] } self.processing_resources = processing_resources or { "ClusterConfig": { "InstanceCount": 1, "InstanceType": "ml.m5.large", "VolumeSizeInGB": 10, }, } self.stopping_condition = stopping_condition or { "MaxRuntimeInSeconds": 3600, } def save(self): sagemaker = boto3.client("sagemaker", region_name=TEST_REGION_NAME) params = { "AppSpecification": self.app_specification, "NetworkConfig": self.network_config, "ProcessingInputs": self.processing_inputs, "ProcessingJobName": self.processing_job_name, "ProcessingOutputConfig": self.processing_output_config, "ProcessingResources": self.processing_resources, "RoleArn": self.role_arn, "StoppingCondition": self.stopping_condition, } return sagemaker.create_processing_job(**params) @mock_sagemaker def test_create_processing_job(): sagemaker = boto3.client("sagemaker", region_name=TEST_REGION_NAME) processing_job_name = "MyProcessingJob" role_arn = "arn:aws:iam::{}:role/FakeRole".format(ACCOUNT_ID) container = "382416733822.dkr.ecr.us-east-1.amazonaws.com/linear-learner:1" bucket = "my-bucket" prefix = "my-prefix" app_specification = { "ImageUri": container, "ContainerEntrypoint": ["python3", "app.py"], } processing_resources = { "ClusterConfig": { "InstanceCount": 2, "InstanceType": "ml.m5.xlarge", "VolumeSizeInGB": 20, }, } stopping_condition = {"MaxRuntimeInSeconds": 60 * 60} job = MyProcessingJobModel( processing_job_name, role_arn, container=container, bucket=bucket, prefix=prefix, app_specification=app_specification, processing_resources=processing_resources, stopping_condition=stopping_condition, ) resp = job.save() resp["ProcessingJobArn"].should.match( r"^arn:aws:sagemaker:.*:.*:processing-job/{}$".format(processing_job_name) ) resp = sagemaker.describe_processing_job(ProcessingJobName=processing_job_name) resp["ProcessingJobName"].should.equal(processing_job_name) resp["ProcessingJobArn"].should.match( r"^arn:aws:sagemaker:.*:.*:processing-job/{}$".format(processing_job_name) ) assert "python3" in resp["AppSpecification"]["ContainerEntrypoint"] assert "app.py" in resp["AppSpecification"]["ContainerEntrypoint"] assert resp["RoleArn"] == role_arn assert resp["ProcessingJobStatus"] == "Completed" assert isinstance(resp["CreationTime"], datetime.datetime) assert isinstance(resp["LastModifiedTime"], datetime.datetime) @mock_sagemaker def test_list_processing_jobs(): client = boto3.client("sagemaker", region_name="us-east-1") name = "blah" arn = "arn:aws:sagemaker:us-east-1:000000000000:x-x/foobar" test_processing_job = MyProcessingJobModel(processing_job_name=name, role_arn=arn) test_processing_job.save() processing_jobs = client.list_processing_jobs() assert len(processing_jobs["ProcessingJobSummaries"]).should.equal(1) assert processing_jobs["ProcessingJobSummaries"][0][ "ProcessingJobName" ].should.equal(name) assert processing_jobs["ProcessingJobSummaries"][0][ "ProcessingJobArn" ].should.match(r"^arn:aws:sagemaker:.*:.*:processing-job/{}$".format(name)) assert processing_jobs.get("NextToken") is None @mock_sagemaker def test_list_processing_jobs_multiple(): client = boto3.client("sagemaker", region_name="us-east-1") name_job_1 = "blah" arn_job_1 = "arn:aws:sagemaker:us-east-1:000000000000:x-x/foobar" test_processing_job_1 = MyProcessingJobModel( processing_job_name=name_job_1, role_arn=arn_job_1 ) test_processing_job_1.save() name_job_2 = "blah2" arn_job_2 = "arn:aws:sagemaker:us-east-1:000000000000:x-x/foobar2" test_processing_job_2 = MyProcessingJobModel( processing_job_name=name_job_2, role_arn=arn_job_2 ) test_processing_job_2.save() processing_jobs_limit = client.list_processing_jobs(MaxResults=1) assert len(processing_jobs_limit["ProcessingJobSummaries"]).should.equal(1) processing_jobs = client.list_processing_jobs() assert len(processing_jobs["ProcessingJobSummaries"]).should.equal(2) assert processing_jobs.get("NextToken").should.be.none @mock_sagemaker def test_list_processing_jobs_none(): client = boto3.client("sagemaker", region_name="us-east-1") processing_jobs = client.list_processing_jobs() assert len(processing_jobs["ProcessingJobSummaries"]).should.equal(0) @mock_sagemaker def test_list_processing_jobs_should_validate_input(): client = boto3.client("sagemaker", region_name="us-east-1") junk_status_equals = "blah" with pytest.raises(ClientError) as ex: client.list_processing_jobs(StatusEquals=junk_status_equals) expected_error = f"1 validation errors detected: Value '{junk_status_equals}' at 'statusEquals' failed to satisfy constraint: Member must satisfy enum value set: ['Completed', 'Stopped', 'InProgress', 'Stopping', 'Failed']" assert ex.value.response["Error"]["Code"] == "ValidationException" assert ex.value.response["Error"]["Message"] == expected_error junk_next_token = "asdf" with pytest.raises(ClientError) as ex: client.list_processing_jobs(NextToken=junk_next_token) assert ex.value.response["Error"]["Code"] == "ValidationException" assert ( ex.value.response["Error"]["Message"] == 'Invalid pagination token because "{0}".' ) @mock_sagemaker def test_list_processing_jobs_with_name_filters(): client = boto3.client("sagemaker", region_name="us-east-1") for i in range(5): name = "xgboost-{}".format(i) arn = "arn:aws:sagemaker:us-east-1:000000000000:x-x/foobar-{}".format(i) MyProcessingJobModel(processing_job_name=name, role_arn=arn).save() for i in range(5): name = "vgg-{}".format(i) arn = "arn:aws:sagemaker:us-east-1:000000000000:x-x/barfoo-{}".format(i) MyProcessingJobModel(processing_job_name=name, role_arn=arn).save() xgboost_processing_jobs = client.list_processing_jobs(NameContains="xgboost") assert len(xgboost_processing_jobs["ProcessingJobSummaries"]).should.equal(5) processing_jobs_with_2 = client.list_processing_jobs(NameContains="2") assert len(processing_jobs_with_2["ProcessingJobSummaries"]).should.equal(2) @mock_sagemaker def test_list_processing_jobs_paginated(): client = boto3.client("sagemaker", region_name="us-east-1") for i in range(5): name = "xgboost-{}".format(i) arn = "arn:aws:sagemaker:us-east-1:000000000000:x-x/foobar-{}".format(i) MyProcessingJobModel(processing_job_name=name, role_arn=arn).save() xgboost_processing_job_1 = client.list_processing_jobs( NameContains="xgboost", MaxResults=1 ) assert len(xgboost_processing_job_1["ProcessingJobSummaries"]).should.equal(1) assert xgboost_processing_job_1["ProcessingJobSummaries"][0][ "ProcessingJobName" ].should.equal("xgboost-0") assert xgboost_processing_job_1.get("NextToken").should_not.be.none xgboost_processing_job_next = client.list_processing_jobs( NameContains="xgboost", MaxResults=1, NextToken=xgboost_processing_job_1.get("NextToken"), ) assert len(xgboost_processing_job_next["ProcessingJobSummaries"]).should.equal(1) assert xgboost_processing_job_next["ProcessingJobSummaries"][0][ "ProcessingJobName" ].should.equal("xgboost-1") assert xgboost_processing_job_next.get("NextToken").should_not.be.none @mock_sagemaker def test_list_processing_jobs_paginated_with_target_in_middle(): client = boto3.client("sagemaker", region_name="us-east-1") for i in range(5): name = "xgboost-{}".format(i) arn = "arn:aws:sagemaker:us-east-1:000000000000:x-x/foobar-{}".format(i) MyProcessingJobModel(processing_job_name=name, role_arn=arn).save() for i in range(5): name = "vgg-{}".format(i) arn = "arn:aws:sagemaker:us-east-1:000000000000:x-x/barfoo-{}".format(i) MyProcessingJobModel(processing_job_name=name, role_arn=arn).save() vgg_processing_job_1 = client.list_processing_jobs(NameContains="vgg", MaxResults=1) assert len(vgg_processing_job_1["ProcessingJobSummaries"]).should.equal(0) assert vgg_processing_job_1.get("NextToken").should_not.be.none vgg_processing_job_6 = client.list_processing_jobs(NameContains="vgg", MaxResults=6) assert len(vgg_processing_job_6["ProcessingJobSummaries"]).should.equal(1) assert vgg_processing_job_6["ProcessingJobSummaries"][0][ "ProcessingJobName" ].should.equal("vgg-0") assert vgg_processing_job_6.get("NextToken").should_not.be.none vgg_processing_job_10 = client.list_processing_jobs( NameContains="vgg", MaxResults=10 ) assert len(vgg_processing_job_10["ProcessingJobSummaries"]).should.equal(5) assert vgg_processing_job_10["ProcessingJobSummaries"][-1][ "ProcessingJobName" ].should.equal("vgg-4") assert vgg_processing_job_10.get("NextToken").should.be.none @mock_sagemaker def test_list_processing_jobs_paginated_with_fragmented_targets(): client = boto3.client("sagemaker", region_name="us-east-1") for i in range(5): name = "xgboost-{}".format(i) arn = "arn:aws:sagemaker:us-east-1:000000000000:x-x/foobar-{}".format(i) MyProcessingJobModel(processing_job_name=name, role_arn=arn).save() for i in range(5): name = "vgg-{}".format(i) arn = "arn:aws:sagemaker:us-east-1:000000000000:x-x/barfoo-{}".format(i) MyProcessingJobModel(processing_job_name=name, role_arn=arn).save() processing_jobs_with_2 = client.list_processing_jobs(NameContains="2", MaxResults=8) assert len(processing_jobs_with_2["ProcessingJobSummaries"]).should.equal(2) assert processing_jobs_with_2.get("NextToken").should_not.be.none processing_jobs_with_2_next = client.list_processing_jobs( NameContains="2", MaxResults=1, NextToken=processing_jobs_with_2.get("NextToken"), ) assert len(processing_jobs_with_2_next["ProcessingJobSummaries"]).should.equal(0) assert processing_jobs_with_2_next.get("NextToken").should_not.be.none processing_jobs_with_2_next_next = client.list_processing_jobs( NameContains="2", MaxResults=1, NextToken=processing_jobs_with_2_next.get("NextToken"), ) assert len(processing_jobs_with_2_next_next["ProcessingJobSummaries"]).should.equal( 0 ) assert processing_jobs_with_2_next_next.get("NextToken").should.be.none
39.829851
227
0.67526
3,153
0.236304
0
0
9,902
0.742112
0
0
3,509
0.262984
ce4d63008769bb7f26121f3ebe84e27bc4d39e53
7,853
py
Python
tools/sprite-editor/gui/direction_sprite_widget.py
jordsti/stigame
6ac0ae737667b1c77da3ef5007f5c4a3a080045a
[ "MIT" ]
8
2015-02-03T20:23:49.000Z
2022-02-15T07:51:05.000Z
tools/sprite-editor/gui/direction_sprite_widget.py
jordsti/stigame
6ac0ae737667b1c77da3ef5007f5c4a3a080045a
[ "MIT" ]
null
null
null
tools/sprite-editor/gui/direction_sprite_widget.py
jordsti/stigame
6ac0ae737667b1c77da3ef5007f5c4a3a080045a
[ "MIT" ]
2
2017-02-13T18:04:00.000Z
2020-08-24T03:21:37.000Z
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'direction_sprite_widget.ui' # # Created: Wed Jul 30 18:37:40 2014 # by: PyQt4 UI code generator 4.10.4 # # WARNING! All changes made in this file will be lost! from PyQt4 import QtCore, QtGui try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: def _fromUtf8(s): return s try: _encoding = QtGui.QApplication.UnicodeUTF8 def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig, _encoding) except AttributeError: def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig) class Ui_direction_sprite_widget(object): def setupUi(self, direction_sprite_widget): direction_sprite_widget.setObjectName(_fromUtf8("direction_sprite_widget")) direction_sprite_widget.resize(566, 457) self.verticalLayoutWidget = QtGui.QWidget(direction_sprite_widget) self.verticalLayoutWidget.setGeometry(QtCore.QRect(0, 0, 561, 451)) self.verticalLayoutWidget.setObjectName(_fromUtf8("verticalLayoutWidget")) self.layout_main = QtGui.QVBoxLayout(self.verticalLayoutWidget) self.layout_main.setMargin(5) self.layout_main.setObjectName(_fromUtf8("layout_main")) self.layout_sprite_info = QtGui.QHBoxLayout() self.layout_sprite_info.setMargin(2) self.layout_sprite_info.setObjectName(_fromUtf8("layout_sprite_info")) self.gb_sprite = QtGui.QGroupBox(self.verticalLayoutWidget) self.gb_sprite.setMinimumSize(QtCore.QSize(250, 110)) self.gb_sprite.setObjectName(_fromUtf8("gb_sprite")) self.formLayoutWidget = QtGui.QWidget(self.gb_sprite) self.formLayoutWidget.setGeometry(QtCore.QRect(20, 20, 191, 74)) self.formLayoutWidget.setObjectName(_fromUtf8("formLayoutWidget")) self.formLayout = QtGui.QFormLayout(self.formLayoutWidget) self.formLayout.setMargin(0) self.formLayout.setObjectName(_fromUtf8("formLayout")) self.lbl_name = QtGui.QLabel(self.formLayoutWidget) self.lbl_name.setObjectName(_fromUtf8("lbl_name")) self.formLayout.setWidget(0, QtGui.QFormLayout.LabelRole, self.lbl_name) self.le_name = QtGui.QLineEdit(self.formLayoutWidget) self.le_name.setObjectName(_fromUtf8("le_name")) self.formLayout.setWidget(0, QtGui.QFormLayout.FieldRole, self.le_name) self.lbl_width = QtGui.QLabel(self.formLayoutWidget) self.lbl_width.setObjectName(_fromUtf8("lbl_width")) self.formLayout.setWidget(1, QtGui.QFormLayout.LabelRole, self.lbl_width) self.le_width = QtGui.QLineEdit(self.formLayoutWidget) self.le_width.setObjectName(_fromUtf8("le_width")) self.formLayout.setWidget(1, QtGui.QFormLayout.FieldRole, self.le_width) self.lbl_height = QtGui.QLabel(self.formLayoutWidget) self.lbl_height.setObjectName(_fromUtf8("lbl_height")) self.formLayout.setWidget(2, QtGui.QFormLayout.LabelRole, self.lbl_height) self.le_height = QtGui.QLineEdit(self.formLayoutWidget) self.le_height.setObjectName(_fromUtf8("le_height")) self.formLayout.setWidget(2, QtGui.QFormLayout.FieldRole, self.le_height) self.layout_sprite_info.addWidget(self.gb_sprite) self.gb_preview = QtGui.QGroupBox(self.verticalLayoutWidget) self.gb_preview.setMinimumSize(QtCore.QSize(120, 0)) self.gb_preview.setObjectName(_fromUtf8("gb_preview")) self.horizontalLayoutWidget = QtGui.QWidget(self.gb_preview) self.horizontalLayoutWidget.setGeometry(QtCore.QRect(10, 20, 121, 80)) self.horizontalLayoutWidget.setObjectName(_fromUtf8("horizontalLayoutWidget")) self.layout_preview = QtGui.QHBoxLayout(self.horizontalLayoutWidget) self.layout_preview.setMargin(0) self.layout_preview.setObjectName(_fromUtf8("layout_preview")) self.lbl_preview = QtGui.QLabel(self.horizontalLayoutWidget) self.lbl_preview.setObjectName(_fromUtf8("lbl_preview")) self.layout_preview.addWidget(self.lbl_preview) self.sp_fps = QtGui.QSpinBox(self.horizontalLayoutWidget) self.sp_fps.setMinimumSize(QtCore.QSize(40, 20)) self.sp_fps.setMaximumSize(QtCore.QSize(40, 20)) self.sp_fps.setMinimum(15) self.sp_fps.setMaximum(150) self.sp_fps.setObjectName(_fromUtf8("sp_fps")) self.layout_preview.addWidget(self.sp_fps) self.layout_sprite_info.addWidget(self.gb_preview) self.btn_add_frame = QtGui.QPushButton(self.verticalLayoutWidget) self.btn_add_frame.setMinimumSize(QtCore.QSize(120, 0)) self.btn_add_frame.setObjectName(_fromUtf8("btn_add_frame")) self.layout_sprite_info.addWidget(self.btn_add_frame) self.layout_main.addLayout(self.layout_sprite_info) self.horizontalLayout = QtGui.QHBoxLayout() self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout")) self.lbl_frames = QtGui.QLabel(self.verticalLayoutWidget) self.lbl_frames.setObjectName(_fromUtf8("lbl_frames")) self.horizontalLayout.addWidget(self.lbl_frames) self.formLayout_2 = QtGui.QFormLayout() self.formLayout_2.setFieldGrowthPolicy(QtGui.QFormLayout.AllNonFixedFieldsGrow) self.formLayout_2.setObjectName(_fromUtf8("formLayout_2")) self.cb_direction = QtGui.QComboBox(self.verticalLayoutWidget) self.cb_direction.setObjectName(_fromUtf8("cb_direction")) self.formLayout_2.setWidget(0, QtGui.QFormLayout.FieldRole, self.cb_direction) self.lbl_direction = QtGui.QLabel(self.verticalLayoutWidget) self.lbl_direction.setObjectName(_fromUtf8("lbl_direction")) self.formLayout_2.setWidget(0, QtGui.QFormLayout.LabelRole, self.lbl_direction) self.horizontalLayout.addLayout(self.formLayout_2) self.layout_main.addLayout(self.horizontalLayout) self.sa_frames = QtGui.QScrollArea(self.verticalLayoutWidget) self.sa_frames.setWidgetResizable(True) self.sa_frames.setAlignment(QtCore.Qt.AlignJustify|QtCore.Qt.AlignVCenter) self.sa_frames.setObjectName(_fromUtf8("sa_frames")) self.scrollAreaWidgetContents = QtGui.QWidget() self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 549, 289)) self.scrollAreaWidgetContents.setObjectName(_fromUtf8("scrollAreaWidgetContents")) self.sa_frames.setWidget(self.scrollAreaWidgetContents) self.layout_main.addWidget(self.sa_frames) self.retranslateUi(direction_sprite_widget) QtCore.QMetaObject.connectSlotsByName(direction_sprite_widget) def retranslateUi(self, direction_sprite_widget): direction_sprite_widget.setWindowTitle(_translate("direction_sprite_widget", "Form", None)) self.gb_sprite.setTitle(_translate("direction_sprite_widget", "Sprite Information", None)) self.lbl_name.setText(_translate("direction_sprite_widget", "Name :", None)) self.lbl_width.setText(_translate("direction_sprite_widget", "Width :", None)) self.le_width.setText(_translate("direction_sprite_widget", "0", None)) self.lbl_height.setText(_translate("direction_sprite_widget", "Height :", None)) self.le_height.setText(_translate("direction_sprite_widget", "0", None)) self.gb_preview.setTitle(_translate("direction_sprite_widget", "Preview", None)) self.lbl_preview.setText(_translate("direction_sprite_widget", "P", None)) self.btn_add_frame.setText(_translate("direction_sprite_widget", "Add a frame...", None)) self.lbl_frames.setText(_translate("direction_sprite_widget", "Frame(s)", None)) self.lbl_direction.setText(_translate("direction_sprite_widget", "Direction :", None))
58.17037
99
0.741118
7,150
0.91048
0
0
0
0
0
0
1,028
0.130905
ce4e49dc4da5a6289114dc9b19fa4b7569d8b066
4,913
py
Python
venv/lib/python3.5/site-packages/bears/python/PEP8NotebookBear.py
prashant0598/CoffeeApp
4fa006aebf06e12ed34766450ddcfa548ee63307
[ "MIT" ]
null
null
null
venv/lib/python3.5/site-packages/bears/python/PEP8NotebookBear.py
prashant0598/CoffeeApp
4fa006aebf06e12ed34766450ddcfa548ee63307
[ "MIT" ]
null
null
null
venv/lib/python3.5/site-packages/bears/python/PEP8NotebookBear.py
prashant0598/CoffeeApp
4fa006aebf06e12ed34766450ddcfa548ee63307
[ "MIT" ]
null
null
null
import autopep8 import nbformat from coalib.bearlib.spacing.SpacingHelper import SpacingHelper from coalib.bears.LocalBear import LocalBear from dependency_management.requirements.PipRequirement import PipRequirement from coalib.results.Diff import Diff from coalib.results.Result import Result from coalib.settings.Setting import typed_list # Comments regarind Jupyter Notebooks: # The `nbformat` module contains the reference implementation of the Jupyter # Notebook format, and Python APIs for working with notebooks. # On the file level, a notebook is a JSON file, i.e. dictionary with a few # keys. # The functions in `nbformat` work with `NotebookNode` objects, which are like # dictionaries, but allow attribute access. The structure of these objects # matches the notebook format specification. def notebook_node_from_string_list(string_list): """ Reads a notebook from a string list and returns the NotebookNode object. :param string_list: The notebook file contents as list of strings (linewise). :return: The notebook as NotebookNode. """ return nbformat.reads(''.join(string_list), nbformat.NO_CONVERT) def notebook_node_to_string_list(notebook_node): """ Writes a NotebookNode to a list of strings. :param notebook_node: The notebook as NotebookNode to write. :return: The notebook as list of strings (linewise). """ return nbformat.writes(notebook_node, nbformat.NO_CONVERT).splitlines(True) def autopep8_fix_code_cell(source, options=None, apply_config=None): """ Applies autopep8.fix_code and takes care of newline characters. autopep8.fix_code automatically adds a final newline at the end, e.g. ``autopep8.fix_code('a=1')`` yields 'a = 1\\n'. Note that this is not related to the 'W292' flag, i.e. ``autopep8.fix_code('a=1', options=dict(ignore=('W292',)))`` gives the same result. For notebook code cells, this behaviour does not make sense, hence newline is removed if ``source`` does not end with one. """ source_corrected = autopep8.fix_code(source, apply_config=apply_config, options=options) if not source.endswith('\n'): return source_corrected[:-1] return source_corrected class PEP8NotebookBear(LocalBear): LANGUAGES = {'Python', 'Python 2', 'Python 3'} REQUIREMENTS = {PipRequirement('autopep8', '1.2'), PipRequirement('nbformat', '4.1')} AUTHORS = {'The coala developers'} AUTHORS_EMAILS = {'coala-devel@googlegroups.com'} LICENSE = 'AGPL-3.0' ASCIINEMA_URL = 'https://asciinema.org/a/83333' CAN_FIX = {'Formatting'} def run(self, filename, file, max_line_length: int=79, indent_size: int=SpacingHelper.DEFAULT_TAB_WIDTH, pep_ignore: typed_list(str)=(), pep_select: typed_list(str)=(), local_pep8_config: bool=False): """ Detects and fixes PEP8 incompliant code in Jupyter Notebooks. This bear will not change functionality of the code in any way. :param max_line_length: Maximum number of characters for a line. :param indent_size: Number of spaces per indent level. :param pep_ignore: A list of errors/warnings to ignore. :param pep_select: A list of errors/warnings to exclusively apply. :param local_pep8_config: Set to true if autopep8 should use a config file as if run normally from this directory. """ options = {'ignore': pep_ignore, 'select': pep_select, 'max_line_length': max_line_length, 'indent_size': indent_size} notebook_node = notebook_node_from_string_list(file) cells = notebook_node['cells'] for cell in cells: if cell['cell_type'] != 'code': continue cell['source'] = autopep8_fix_code_cell(cell['source'], local_pep8_config, options) corrected = notebook_node_to_string_list(notebook_node) # If newline at eof in `file` but not in `corrected`, add # final newline character to `corrected` to make sure this difference # does not pop up in `diffs`. if file[-1].endswith('\n') and not corrected[-1].endswith('\n'): corrected[-1] += '\n' diffs = Diff.from_string_arrays(file, corrected).split_diff() for diff in diffs: yield Result(self, 'The code does not comply to PEP8.', affected_code=(diff.range(filename),), diffs={filename: diff})
40.270492
79
0.632607
2,556
0.520252
2,156
0.438836
0
0
0
0
2,492
0.507226
ce5057f4503ef56fd394e2f07ab56b6b56dccf58
1,043
py
Python
doc2cube-master/src/prel.py
sustcjudgement/Judgement_information_extraction
c769eb1cb7ee695a157a981dbe9cd9d6559d072b
[ "MIT" ]
1
2019-05-30T07:07:13.000Z
2019-05-30T07:07:13.000Z
doc2cube-master/src/prel.py
sustcjudgement/Judgement_information_extraction
c769eb1cb7ee695a157a981dbe9cd9d6559d072b
[ "MIT" ]
null
null
null
doc2cube-master/src/prel.py
sustcjudgement/Judgement_information_extraction
c769eb1cb7ee695a157a981dbe9cd9d6559d072b
[ "MIT" ]
null
null
null
import nltk import string import argparse parser = argparse.ArgumentParser(description='.') parser.add_argument('-text', help='') parser.add_argument('-meta', help='') parser.add_argument('-output', help='') args = parser.parse_args() # parser.add_argument('-iter', dest='iter', type=int, # default=max_iter) text_docs = {} with open(args.text, 'r') as f: with open(args.output + 'd_prel.txt', 'w+') as g: idx = 0 for line in f: tokens = [w.lower().replace('###', '_') for w in line.strip('\r\n').split(' ')] stopwords = nltk.corpus.stopwords.words('english') tokens = [w for w in tokens if w not in stopwords] line = str(idx) + '\t' + ';'.join(tokens) + ';\n' g.write(line) idx += 1 with open(args.output + 'l_prel.txt', 'w+') as g: g.write('0 * \n') with open(args.meta, 'r') as f: idx = 0 for line in f: idx += 1 path = line.strip('\r\n') value = path.split('|')[-1] g.write(str(idx) + '\t' + path + '\t' + value + '\n') # prel => parse_flat => run.sh # ==> evaluate.py
24.833333
82
0.589645
0
0
0
0
0
0
0
0
276
0.264621
ce517a5ddc247572eac79c178a88597e1d88b706
43
py
Python
models/__init__.py
salesforce/DataHardness
18b9231f8d08f35b2452e6357b7d6b31f21c695c
[ "BSD-3-Clause" ]
3
2021-11-18T22:48:28.000Z
2022-01-08T08:02:31.000Z
models/__init__.py
salesforce/DataHardness
18b9231f8d08f35b2452e6357b7d6b31f21c695c
[ "BSD-3-Clause" ]
null
null
null
models/__init__.py
salesforce/DataHardness
18b9231f8d08f35b2452e6357b7d6b31f21c695c
[ "BSD-3-Clause" ]
1
2021-11-18T22:48:32.000Z
2021-11-18T22:48:32.000Z
from models.glow import Glow, GlowAdditive
21.5
42
0.837209
0
0
0
0
0
0
0
0
0
0
ce51bf2481ad7448201c8511a71d60800f43cedd
350
py
Python
Logic/Helpers/ChronosTextEntry.py
terexdev/BSDS-V39
7deea469fbfbc56c48f8326ba972369679f6b098
[ "Apache-2.0" ]
11
2021-11-04T01:49:50.000Z
2022-01-31T16:50:47.000Z
Logic/Helpers/ChronosTextEntry.py
terexdev/BSDS-V39
7deea469fbfbc56c48f8326ba972369679f6b098
[ "Apache-2.0" ]
6
2021-11-04T08:52:01.000Z
2021-12-27T02:33:19.000Z
Logic/Helpers/ChronosTextEntry.py
terexdev/BSDS-V39
7deea469fbfbc56c48f8326ba972369679f6b098
[ "Apache-2.0" ]
5
2021-11-04T02:31:56.000Z
2022-03-14T02:04:33.000Z
from Logic.Classes.LogicDataTables import LogicDataTables from Logic.Data.DataManager import Writer from Logic.Data.DataManager import Reader class ChronosTextEntry: def decode(self: Reader): self.readInt() self.readString() def encode(self: Writer): self.writeInt(0) self.writeString("BSDS is catly hacc")
25
57
0.717143
205
0.585714
0
0
0
0
0
0
20
0.057143
ce52086eedaa4d8450071ad9dda9ddc525a4ba30
1,136
py
Python
Hoofdstuk 3/animals.py
BearWithAFez/Learning-Python
23f6aa82e431838dc3891fe46d6ff6ea64281fe0
[ "MIT" ]
null
null
null
Hoofdstuk 3/animals.py
BearWithAFez/Learning-Python
23f6aa82e431838dc3891fe46d6ff6ea64281fe0
[ "MIT" ]
null
null
null
Hoofdstuk 3/animals.py
BearWithAFez/Learning-Python
23f6aa82e431838dc3891fe46d6ff6ea64281fe0
[ "MIT" ]
null
null
null
#!/usr/bin/env python3 from urllib.request import urlopen import sys # Get the animals def fetch_animals(url): """Get a list of lines (animals) from a given URL. Args: url: The URL of a utf-8 text Returns: A list of lines. """ with urlopen(url) as data: animals = [] for animal in data: animals.append(animal.decode('utf-8').rstrip()) return animals # Print the animals given def print_items(animals): """Prints all items from given collection. Args: animals: The collection to print. """ for animal in animals: print(animal) # Main method def main(url): """Prints all lines (animals) from a given URL. Args: url: The URL of a utf-8 text """ animals = fetch_animals(url) print_items(animals) """A list of lines printed from the given URL Args: 1: the URL to a UTF-8 text to print Usage: python3 animals.py """ animalsUrl = 'https://raw.githubusercontent.com/BearWithAFez/Learning-Python/master/Hoofdstuk%202/animals.txt' if __name__ == '__main__': main(sys.argv[1])
20.285714
110
0.628521
0
0
0
0
0
0
0
0
685
0.602993
ce522237e0f47825dd315d861dd8e20bb64f4c53
19,762
py
Python
code/game.py
chaonan99/merge_sim
0a96685b261c94ffe7d73abec3a488ef02b48cd0
[ "MIT" ]
null
null
null
code/game.py
chaonan99/merge_sim
0a96685b261c94ffe7d73abec3a488ef02b48cd0
[ "MIT" ]
null
null
null
code/game.py
chaonan99/merge_sim
0a96685b261c94ffe7d73abec3a488ef02b48cd0
[ "MIT" ]
null
null
null
from collections import deque import numpy as np import os from abc import ABCMeta, abstractmethod import random random.seed(42) from common import config, VehicleState from helper import Helper INFO = """Average merging time: {} s Traffic flow: {} vehicle/s Average speed: {} km/h Average fuel consumption: {} ml/vehicle""" class Vehicle(object): """docstring for Vehicle""" def __init__(self, builder): super(Vehicle, self).__init__() self.ID = builder.ID self.size = builder.size self.speed = builder.speed self.acceleration = builder.acceleration self.max_speed = builder.max_speed self.min_speed = builder.min_speed self.max_acc = builder.max_acc self.lane = builder.lane self.position = builder.position class VehicleBuilder(object): """docstring for VehicleBuilder""" def __init__(self, ID): super(VehicleBuilder, self).__init__() self.ID = ID self.max_acc = config.max_acc self.max_speed = config.max_speed self.min_speed = config.min_speed self.size = config.size def setSpeed(self, speed): self.speed = speed return self def setPosition(self, position): self.position = position return self def setLane(self, lane): self.lane = lane return self def setAcceleration(self, acceleration): self.acceleration = acceleration return self def build(self): return Vehicle(self) class OnBoardVehicle(object): """docstring for OnBoardVehicle""" def __init__(self, vehicle, t0, min_pass_time=config.min_pass_time): self.vehicle = vehicle self.t0 = t0 self.tm = float('Inf') self.position_history = [vehicle.position] self.speed_history = [vehicle.speed] self.acc_history = [vehicle.acceleration] self.fuel_history = [0] self.time_steps = [t0] self.ParaV = None self.min_pass_time = min_pass_time self.state = VehicleState.PENDING @property def v0(self): return self.speed_history[0] @property def p0(self): return self.position_history[0] @property def merge_time(self): return self.time_steps[-1] - self.time_steps[0] @property def tf(self): return self.time_steps[-1] @property def average_speed(self): return np.array(self.speed_history).mean() @property def fuel_consumption(self): return self.fuel_history[-1] class VehicleGeneratorBase(metaclass=ABCMeta): """docstring for VehicleGeneratorBase""" def __init__(self): self.schedule = deque() self.buildSchedule() @abstractmethod def buildSchedule(self): pass def getAtTime(self, ctime): vehicle_to_add = [] while self.hasVehicle() and self.schedule[0].t0 <= ctime: vehicle_to_add.append(self.schedule.popleft()) return vehicle_to_add def hasVehicle(self): return len(self.schedule) > 0 def FIFOIDAssigner(self): self.schedule = deque(sorted(self.schedule, key=lambda x:x.t0)) for i, ove in enumerate(self.schedule): ove.vehicle.ID = i def SpeedIDAssigner(self): for ove in self.schedule: ove.min_pass_time = Helper.getTmOptimal2(ove.v0, config.case_speed['speed_merge'], -config.control_len, 0) self.schedule = deque(sorted(self.schedule, key=lambda x:x.t0 + x.min_pass_time)) for i, ove in enumerate(self.schedule): ove.vehicle.ID = i self.schedule = deque(sorted(self.schedule, key=lambda x:x.t0)) class Case1VehicleGenerator(VehicleGeneratorBase): """docstring for Case1VehicleGenerator""" def __init__(self): super(Case1VehicleGenerator, self).__init__() def buildSchedule(self): for i in range(config.case1['total_cars']): v = VehicleBuilder(i)\ .setSpeed(config.case1['speed'])\ .setPosition(-config.control_len)\ .setAcceleration(0)\ .setLane(i % 2).build() t = 10.0 if (i < 2) else 15.0 # self.schedule.append(OnBoardVehicle(v, t, config.control_len / config.case1['speed'])) tm = Helper.getTmOptimal2(config.case1['speed'], config.case_speed['speed_merge'], -config.control_len, 0) if i == 0 else 0 self.schedule.append(OnBoardVehicle(v, t, tm)) # self.schedule.sort(key=lambda x: x.vehicle.ID) class Case2VehicleGenerator(VehicleGeneratorBase): """docstring for Case1VehicleGenerator""" def __init__(self): super(Case2VehicleGenerator, self).__init__() def buildSchedule(self): tnum = config.case2['total_cars'] # Randomly generate tnum//2 cars on each lane, with time span 10~50 # This does not ensure feasibility at initial state t0_lane0 = np.random.rand(tnum//2) * 40.0 + 10.0 t0_lane1 = np.random.rand(tnum//2) * 40.0 + 10.0 for i in range(tnum): v = VehicleBuilder(-1)\ .setSpeed(config.case2['speed'])\ .setPosition(-config.control_len)\ .setAcceleration(0)\ .setLane(i % 2).build() t = t0_lane0[i//2] if (i % 2 == 0) else t0_lane1[i//2] self.schedule.append(OnBoardVehicle(v, t, config.control_len / config.case2['speed'])) self.FIFOIDAssigner() class MainHigherSpeedVG(VehicleGeneratorBase): def __init__(self): super(MainHigherSpeedVG, self).__init__() def buildSchedule(self): # lane0 (main road) t0_lane0 = np.arange(10, 30.1, 2.0) t0_lane1 = np.arange(9, 30.1, 3.1) v0_lane0 = 25.0 v0_lane1 = 15.0 for ti0 in t0_lane0: v = VehicleBuilder(-1)\ .setSpeed(v0_lane0)\ .setPosition(-config.control_len)\ .setAcceleration(0)\ .setLane(0).build() self.schedule.append(OnBoardVehicle(v, ti0, Helper.getTc(v))) for ti0 in t0_lane1: v = VehicleBuilder(-1)\ .setSpeed(v0_lane1)\ .setPosition(-config.control_len)\ .setAcceleration(0)\ .setLane(1).build() self.schedule.append(OnBoardVehicle(v, ti0, Helper.getTc(v))) self.FIFOIDAssigner() # self.SpeedIDAssigner() class PoissonVehicleGenerator(VehicleGeneratorBase): """docstring for Case1VehicleGenerator""" def __init__(self, tnum_lane0, tnum_lane1): self.tnum_lane0 = tnum_lane0 self.tnum_lane1 = tnum_lane1 super(PoissonVehicleGenerator, self).__init__() def buildSchedule(self): speed_lane0 = config.case_speed['speed_init_lane0'] speed_lane1 = config.case_speed['speed_init_lane1'] t0_lane0 = [0] t0_lane1 = [0] tsp_lane0 = (config.delta) / speed_lane0 tsp_lane1 = (config.delta) / speed_lane1 for i in range(self.tnum_lane0): t0_lane0.append(t0_lane0[-1] + tsp_lane0 + np.random.exponential(5.0)) for i in range(self.tnum_lane1): t0_lane1.append(t0_lane1[-1] + tsp_lane1 + np.random.exponential(5.0)) t0_lane0 = np.array(t0_lane0[1:]) t0_lane1 = np.array(t0_lane1[1:]) for i in range(self.tnum_lane0): speed_v = speed_lane0 + np.random.randn() v = VehicleBuilder(-1)\ .setSpeed(speed_v)\ .setPosition(-config.control_len)\ .setAcceleration(0)\ .setLane(0).build() t = t0_lane0[i] self.schedule.append(OnBoardVehicle(v, t, Helper.getTc(v))) for i in range(self.tnum_lane1): speed_v = speed_lane1 + np.random.randn() v = VehicleBuilder(-1)\ .setSpeed(speed_v)\ .setPosition(-config.control_len)\ .setAcceleration(0)\ .setLane(1).build() t = t0_lane1[i] self.schedule.append(OnBoardVehicle(v, t, Helper.getTc(v))) self.FIFOIDAssigner() # self.SpeedIDAssigner() class APPVehicleGenerator(VehicleGeneratorBase): def __init__(self, tnum, id_assigner, min_pass_time): self.tnum = tnum self.ida = id_assigner self.mpt = min_pass_time super(APPVehicleGenerator, self).__init__() def buildSchedule(self): tnum = self.tnum t0_lane0 = np.random.rand(tnum//2) * 40.0 + 10.0 t0_lane1 = np.random.rand(tnum//2) * 40.0 + 10.0 for i in range(tnum): v = VehicleBuilder(-1)\ .setSpeed(config.case2['speed'])\ .setPosition(-config.control_len)\ .setAcceleration(0)\ .setLane(i % 2).build() t = t0_lane0[i//2] if (i % 2 == 0) else t0_lane1[i//2] self.schedule.append(OnBoardVehicle(v, t, self.mpt)) if self.ida == 'FIFO': self.FIFOIDAssigner() elif self.ida == 'main': self.MainRoadFirstIDAssigner() class GameLoop(object): """docstring for GameLoop""" def __init__(self, vscd): super(GameLoop, self).__init__() self.ctime = 0 self.vscd = vscd self.on_board_vehicles = deque() self.finished_vehicels = deque() def isOver(self): if self.ctime >= config.max_sim_time: print("Simulation time out.") return True return (not self.vscd.hasVehicle()) and self.isEmpty() def isEmpty(self): return len(self.on_board_vehicles) == 0 def nextStep(self): self.ctime += config.time_meta t = self.ctime ove_t = self.vscd.getAtTime(t) for v in ove_t: if v.vehicle.ID == 0: # v.tm = v.t0 + max(config.min_pass_time, v.min_pass_time) v.tm = v.t0 + Helper.getTmOptimal2(v.v0, config.speed_merge, v.p0, 0) elif len(self.on_board_vehicles) > 0: v.tm = Helper.getTm(v, self.on_board_vehicles[-1]) else: v.tm = Helper.getTm(v, self.finished_vehicels[-1]) self.on_board_vehicles.append(v) TimeM = Helper.getTimeMatrix(t, v.tm) ConfV = Helper.getConfigVec(v) v.ParaV = np.dot(np.linalg.inv(TimeM), ConfV) v.state = VehicleState.ON_RAMP for v in self.on_board_vehicles: Helper.updateAVP(v, t) if v.vehicle.position >= 0: v.state = VehicleState.ON_MERGING while not self.isEmpty() and self.on_board_vehicles[0].vehicle.position >= config.merging_len: self.on_board_vehicles[0].state = VehicleState.FINISHED self.finished_vehicels.append((self.on_board_vehicles.popleft())) def play(self): while not self.isOver(): self.nextStep() self.measure() def measure(self): # Measure average merging time AMT = np.array([v.merge_time for v in self.finished_vehicels]).mean() # Measure traffic flow TF = len(self.finished_vehicels) / (self.finished_vehicels[-1].tf - self.finished_vehicels[0].t0) # Measure average speed AS = 1 / np.array([1 / v.average_speed for v in self.finished_vehicels]).mean() * 3.6 # Measure average fuel consumption AFC = np.array([v.fuel_consumption for v in self.finished_vehicels]).mean() print(INFO.format(AMT, TF, AS, AFC)) def draw_result_pyplot(self, file_path=None): import matplotlib.pyplot as plt plt.figure(1) plt.subplot(221) plt.xlabel('time') plt.ylabel('position') plt.title('positions') for tv in self.finished_vehicels: linecolor = 'r--' if tv.vehicle.lane == 0 else 'b' plt.plot(tv.time_steps, tv.position_history, linecolor, label="Car {}".format(tv.vehicle.ID)) plt.grid(True) if file_path: plt.savefig(os.path.join(file_path, "position.pdf"), format="pdf") plt.subplot(222) plt.xlabel('time') plt.ylabel('speed') plt.title('speeds') for tv in self.finished_vehicels: linecolor = 'r--' if tv.vehicle.lane == 0 else 'b' plt.plot(tv.time_steps, tv.speed_history, linecolor, label="Car {}".format(tv.vehicle.ID)) plt.grid(True) if file_path: plt.savefig(os.path.join(file_path, "speed.pdf"), format="pdf") plt.subplot(223) plt.xlabel('time') plt.ylabel('acceleration') plt.title('accelerations') for tv in self.finished_vehicels: linecolor = 'r--' if tv.vehicle.lane == 0 else 'b' plt.plot(tv.time_steps, tv.acc_history, linecolor, label="Car {}".format(tv.vehicle.ID)) plt.grid(True) if file_path: plt.savefig(os.path.join(file_path, "acc.pdf"), format="pdf") plt.subplot(224) plt.xlabel('time') plt.ylabel('fuel') plt.title('fuels') for tv in self.finished_vehicels: linecolor = 'r--' if tv.vehicle.lane == 0 else 'b' plt.plot(tv.time_steps, tv.fuel_history, linecolor, label="Car {}".format(tv.vehicle.ID)) plt.grid(True) if file_path: plt.savefig(os.path.join(file_path, "fuel.pdf"), format="pdf") if not file_path: plt.subplots_adjust(top=0.92, bottom=-0.35, left=0.10, right=1.35, hspace=0.35, wspace=0.35) plt.show() def draw_result(self, file_path): from bokeh.layouts import gridplot from bokeh.plotting import figure, output_file, show output_file(file_path) TOOLS = "pan,wheel_zoom,box_zoom,reset,save,box_select,lasso_select" s1 = figure(tools=TOOLS, title="positions", x_axis_label="time", y_axis_label="position") for tv in self.finished_vehicels: s1.line(tv.time_steps, tv.position_history, line_color="red" if tv.vehicle.lane == 0 else "blue", legend="Car {}".format(tv.vehicle.ID), line_width=2) s1.xgrid.minor_grid_line_color = 'navy' s1.xgrid.minor_grid_line_alpha = 0.1 s1.ygrid.minor_grid_line_color = 'navy' s1.ygrid.minor_grid_line_alpha = 0.1 s2 = figure(tools=TOOLS, title="speed", x_axis_label="time", y_axis_label="speed", x_range=s1.x_range) for tv in self.finished_vehicels: s2.line(tv.time_steps, tv.speed_history, line_color="red" if tv.vehicle.lane == 0 else "blue", legend="Car {}".format(tv.vehicle.ID), line_width=2) s2.xgrid.minor_grid_line_color = 'navy' s2.xgrid.minor_grid_line_alpha = 0.1 s2.ygrid.minor_grid_line_color = 'navy' s2.ygrid.minor_grid_line_alpha = 0.1 s3 = figure(tools=TOOLS, title="acceleration", x_axis_label="time", y_axis_label="acceleration", x_range=s1.x_range) for tv in self.finished_vehicels: s3.line(tv.time_steps, tv.acc_history, line_color="red" if tv.vehicle.lane == 0 else "blue", legend="Car {}".format(tv.vehicle.ID), line_width=2) s3.xgrid.minor_grid_line_color = 'navy' s3.xgrid.minor_grid_line_alpha = 0.1 s3.ygrid.minor_grid_line_color = 'navy' s3.ygrid.minor_grid_line_alpha = 0.1 s4 = figure(tools=TOOLS, title="fuel consumption", x_axis_label="time", y_axis_label="fuel", x_range=s1.x_range) for tv in self.finished_vehicels: s4.line(tv.time_steps, tv.fuel_history, line_color="red" if tv.vehicle.lane == 0 else "blue", legend="Car {}".format(tv.vehicle.ID), line_width=2) s4.xgrid.minor_grid_line_color = 'navy' s4.xgrid.minor_grid_line_alpha = 0.1 s4.ygrid.minor_grid_line_color = 'navy' s4.ygrid.minor_grid_line_alpha = 0.1 p = gridplot([[s1, s2, s3, s4]]) try: show(p) except Exception as e: pass show(p) class SpeedGameLoop(GameLoop): """docstring for SpeedGameLoop""" def __init__(self, vscd): super(SpeedGameLoop, self).__init__(vscd) self.on_board_vehicles = [] def nextStep(self): self.ctime += config.time_meta t = self.ctime ove_t = self.vscd.getAtTime(t) for v in ove_t: tmp_v_stack = [] while len(self.on_board_vehicles) > 0 and self.on_board_vehicles[-1].vehicle.ID > v.vehicle.ID: tmpv = self.on_board_vehicles.pop() # tmpv.t0 = t # tmpv.min_pass_time = max(tmpv.min_pass_time, Helper.getTmOptimal2(tmpv.vehicle.speed, # config.case_speed['speed_merge'], tmpv.vehicle.position, 0)) tmp_v_stack.append(tmpv) # Get t_m if len(self.on_board_vehicles) == 0 and len(self.finished_vehicels) == 0: v.tm = v.t0 + max(config.min_pass_time, v.min_pass_time) elif len(self.on_board_vehicles) > 0: v.tm = Helper.getTm(v, self.on_board_vehicles[-1]) else: v.tm = Helper.getTm(v, self.finished_vehicels[-1]) tmp_v_stack.append(v) prevve = None for i in reversed(range(len(tmp_v_stack))): ve = tmp_v_stack[i] if prevve is not None: ve.tm = Helper.getTm(ve, prevve) self.on_board_vehicles.append(ve) TimeM = Helper.getTimeMatrix(t, ve.tm) ConfV = Helper.getConfigVec(ve) # from IPython import embed; embed() ve.ParaV = np.dot(np.linalg.inv(TimeM), ConfV) ve.state = VehicleState.ON_RAMP prevve = ve # print("ID {}".format(prevve.vehicle.ID)) for v in self.on_board_vehicles: Helper.updateAVP(v, t) if v.vehicle.position >= 0: v.state = VehicleState.ON_MERGING while not self.isEmpty() and self.on_board_vehicles[0].vehicle.position >= config.merging_len: self.on_board_vehicles[0].state = VehicleState.FINISHED self.finished_vehicels.append((self.on_board_vehicles.pop(0))) def main(): # vehicle_generator = Case1VehicleGenerator() # game = GameLoop(vehicle_generator) # game.play() # game.draw_result_pyplot("case1") vehicle_generator = MainHigherSpeedVG() game = GameLoop(vehicle_generator) # game = SpeedGameLoop(vehicle_generator) game.play() game.draw_result_pyplot("case2") # vehicle_generator = APPVehicleGenerator(12, 'FIFO', 16.9) # vehicle_generator = PoissonVehicleGenerator(config.case_speed['tnum_lane0'], # config.case_speed['tnum_lane1']) # ggame = GameLoop(vehicle_generator) # ggame.play() # # ggame.draw_result("result.html") # ggame.draw_result_pyplot(".") if __name__ == '__main__': main()
37.785851
108
0.58233
18,621
0.942263
0
0
522
0.026414
0
0
2,379
0.120383
ce530130c467202c9dd2359037337dda83e6eaa4
387
py
Python
mundo2/ex053.py
dilsonm/CeV
8043be36b2da187065691d23ed5cb40fd65f806f
[ "MIT" ]
null
null
null
mundo2/ex053.py
dilsonm/CeV
8043be36b2da187065691d23ed5cb40fd65f806f
[ "MIT" ]
null
null
null
mundo2/ex053.py
dilsonm/CeV
8043be36b2da187065691d23ed5cb40fd65f806f
[ "MIT" ]
null
null
null
# Crie um programa que leia uma frase qualquer e diga se ele é um palíndromo, desconsiderando os espaços. frase = str(input('Digite uma frase: ')).strip() palavras = frase.split() junto = ''.join(palavras) inverso = '' for c in range( len(junto)-1, -1, -1): inverso += junto[c] if inverso == junto: print('A frase é um PALINDROMO') else: print('A frase NÃO é um PALINDROMO')
32.25
105
0.677003
0
0
0
0
0
0
0
0
189
0.480916
ce53b07d3a1a59be1abb2c6bf2cf0cd25eb7f425
562
py
Python
scripts/show_by_content_type.py
b-cube/Response-Identification-Info
d2fa24c9f0d7db7d8bbf5cda937e1a9dd29a8f6e
[ "MIT" ]
null
null
null
scripts/show_by_content_type.py
b-cube/Response-Identification-Info
d2fa24c9f0d7db7d8bbf5cda937e1a9dd29a8f6e
[ "MIT" ]
1
2015-09-23T16:30:34.000Z
2015-09-23T16:30:34.000Z
scripts/show_by_content_type.py
b-cube/Response-Identification-Info
d2fa24c9f0d7db7d8bbf5cda937e1a9dd29a8f6e
[ "MIT" ]
1
2020-03-25T09:41:03.000Z
2020-03-25T09:41:03.000Z
import os import glob import json for f in glob.glob('/Users/sparky/Documents/solr_responses/solr_20150922_docs/*.json'): with open(f, 'r') as g: data = json.loads(g.read()) headers = data.get('response_headers', []) if not headers: continue headers = dict( (k.strip().lower(), v.strip()) for k, v in (h.split(':', 1) for h in headers) ) content_type = headers.get('content-type', '') if content_type and 'shockwave' in content_type: print data.get('url'), content_type, data.get('tstamp')
28.1
87
0.617438
0
0
0
0
0
0
0
0
130
0.231317
ce53e368e8055c32a3b93d22ee8f35500ad5e829
5,024
py
Python
descarteslabs/workflows/models/tests/test_tile_url.py
descarteslabs/descarteslabs-python
efc874d6062603dc424c9646287a9b1f8636e7ac
[ "Apache-2.0" ]
167
2017-03-23T22:16:58.000Z
2022-03-08T09:19:30.000Z
descarteslabs/workflows/models/tests/test_tile_url.py
descarteslabs/descarteslabs-python
efc874d6062603dc424c9646287a9b1f8636e7ac
[ "Apache-2.0" ]
93
2017-03-23T22:11:40.000Z
2021-12-13T18:38:53.000Z
descarteslabs/workflows/models/tests/test_tile_url.py
descarteslabs/descarteslabs-python
efc874d6062603dc424c9646287a9b1f8636e7ac
[ "Apache-2.0" ]
46
2017-03-25T19:12:14.000Z
2021-08-15T18:04:29.000Z
import pytest import datetime import json import functools from urllib.parse import urlencode, parse_qs from descarteslabs.common.graft import client as graft_client from ... import types from .. import tile_url def test_url(): base = "foo" base_q = base + "?" url = functools.partial(tile_url.tile_url, base, types.Image.from_id("")) assert url() == base assert url(session_id="foo") == base_q + urlencode({"session_id": "foo"}) assert url(colormap="foo") == base_q + urlencode({"colormap": "foo"}) assert url(colormap="") == base_q + urlencode({"colormap": ""}) assert url(reduction="mean") == base_q + urlencode({"reduction": "mean"}) assert url(checkerboard=True) == base_q + urlencode({"checkerboard": "true"}) assert url(checkerboard=False) == base_q + urlencode({"checkerboard": "false"}) assert url(bands=["red"]) == base_q + urlencode({"band": "red"}) assert url(bands=["red", "green"]) == base_q + urlencode( {"band": ["red", "green"]}, doseq=True ) with pytest.raises(ValueError, match="Up to 3 bands may be specified, not 4"): url(bands=["a", "b", "c", "d"]) # 1-band scales are normalized assert url(scales=[0, 1]) == base_q + urlencode({"scales": "[[0.0, 1.0]]"}) # If all none scales, not included assert url(scales=[None, None]) == base_q + urlencode({"scales": "null"}) # test everything gets added together correctly got_base, params = url( session_id="foo", colormap="bar", bands=["red", "green"] ).split("?") assert got_base == base query = parse_qs(params, strict_parsing=True, keep_blank_values=True) assert query == { # `parse_qs` returns all values wrapped in lists "session_id": ["foo"], "colormap": ["bar"], "band": ["red", "green"], } @pytest.mark.parametrize( "args", [ { "p1": "2021-01-20", "p2": 2.2, "p3": 1, }, { "p1": datetime.datetime(2020, 1, 20), "p2": types.Float(1.1) + 1, "p3": 1, }, { "p1": types.Datetime(2021, 1, 20), "p2": types.Float(1.1) + 1, "p3": types.Int(1), }, ], ) def test_url_arguments(args): func = types.Function[ dict(p1=types.Datetime, p2=types.Float, p3=types.Int), types.Image ]("x") base = "http://base.net" url = functools.partial(tile_url.tile_url, base, func) with pytest.raises(TypeError, match="missing a required argument"): url() with pytest.raises(TypeError, match="got an unexpected keyword argument 'blah'"): url(**args, blah="bad") with graft_client.consistent_guid(): got_base, params = url(**args).split("?") assert got_base == base query = parse_qs(params, strict_parsing=True, keep_blank_values=True) assert query.keys() == args.keys() with graft_client.consistent_guid(): p1_graft = types.Datetime._promote(args["p1"]).graft assert query["p1"] == [json.dumps(p1_graft)] if isinstance(args["p2"], float): assert query["p2"] == ["2.2"] else: assert query["p2"] == [json.dumps(args["p2"].graft)] assert query["p3"] == ["1"] def test_no_url_for_positional_only_function(): with pytest.raises( TypeError, match="cannot use Functions with positional-only arguments" ): tile_url.tile_url("", types.Function[types.Int, {}, types.Image]("x")) def test_validate_scales(): assert tile_url.validate_scales([[0.0, 1.0], [0.0, 2.0], [-1.0, 1.0]]) == [ [0.0, 1.0], [0.0, 2.0], [-1.0, 1.0], ] assert tile_url.validate_scales([[0.0, 1.0]]) == [[0.0, 1.0]] # ints -> floats assert tile_url.validate_scales([[0, 1]]) == [[0.0, 1.0]] # 1-band convenience assert tile_url.validate_scales([0, 1]) == [[0.0, 1.0]] # no scalings assert tile_url.validate_scales(None) == [] assert tile_url.validate_scales([]) == [] with pytest.raises(TypeError, match="Expected a list or tuple of scales"): tile_url.validate_scales(0) with pytest.raises(TypeError, match="Expected a list or tuple of scales"): tile_url.validate_scales("foo") with pytest.raises(TypeError, match="Scaling 0: expected a 2-item list or tuple"): tile_url.validate_scales([1, 2, 3]) with pytest.raises(TypeError, match="Scaling 0: items in scaling must be numbers"): tile_url.validate_scales([1, "foo"]) with pytest.raises(ValueError, match="expected up to 3 scales, but got 4"): tile_url.validate_scales([[0.0, 1.0], [0.0, 1.0], [0.0, 1.0], [0.0, 1.0]]) with pytest.raises(ValueError, match="but length was 3"): tile_url.validate_scales([[0.0, 1.0, 2.0]]) with pytest.raises(ValueError, match="but length was 1"): tile_url.validate_scales([[0.0]]) with pytest.raises(ValueError, match="one number and one None in scales"): tile_url.validate_scales([[None, 1.0]])
35.132867
87
0.602309
0
0
0
0
1,424
0.283439
0
0
1,072
0.213376
ce5459689c023b5b6363dd479cd3042521f3f23d
1,112
py
Python
backend-project/small_eod/collections/migrations/0003_auto_20200131_2033.py
WlodzimierzKorza/small_eod
027022bd71122a949a2787d0fb86518df80e48cd
[ "MIT" ]
64
2019-12-30T11:24:03.000Z
2021-06-24T01:04:56.000Z
backend-project/small_eod/collections/migrations/0003_auto_20200131_2033.py
WlodzimierzKorza/small_eod
027022bd71122a949a2787d0fb86518df80e48cd
[ "MIT" ]
465
2018-06-13T21:43:43.000Z
2022-01-04T23:33:56.000Z
backend-project/small_eod/collections/migrations/0003_auto_20200131_2033.py
WlodzimierzKorza/small_eod
027022bd71122a949a2787d0fb86518df80e48cd
[ "MIT" ]
72
2018-12-02T19:47:03.000Z
2022-01-04T22:54:49.000Z
# Generated by Django 3.0.2 on 2020-01-31 20:33 from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ ('collections', '0002_auto_20200109_1348'), ] operations = [ migrations.AlterField( model_name='collection', name='comment', field=models.CharField(help_text='Comment for collection.', max_length=256, verbose_name='Comment'), ), migrations.AlterField( model_name='collection', name='expired_on', field=models.DateTimeField(help_text='An expiration date of collection.', verbose_name='An expiration date'), ), migrations.AlterField( model_name='collection', name='public', field=models.BooleanField(default=False, help_text='Make public.', verbose_name='Public'), ), migrations.AlterField( model_name='collection', name='query', field=models.CharField(help_text='Query for collection.', max_length=256, verbose_name='Query'), ), ]
32.705882
121
0.611511
1,019
0.916367
0
0
0
0
0
0
310
0.278777
ce57252a3bbc1b4941fbfe4e1830281dc89e2cd0
908
py
Python
violet_services/src/service_client.py
Violet-C/EE477_Final
d30cc07833d8c1bb44c0a3373afa739a0b81d25f
[ "Apache-2.0" ]
null
null
null
violet_services/src/service_client.py
Violet-C/EE477_Final
d30cc07833d8c1bb44c0a3373afa739a0b81d25f
[ "Apache-2.0" ]
null
null
null
violet_services/src/service_client.py
Violet-C/EE477_Final
d30cc07833d8c1bb44c0a3373afa739a0b81d25f
[ "Apache-2.0" ]
null
null
null
#!/usr/bin/env python import rospy # python client library for ROS from violet_services.srv import WordCount # needed message type import sys # python functions, methods, etc. rospy.init_node('service_client') # initialize client node rospy.wait_for_service('word_count') # wait for registration word_counter = rospy.ServiceProxy( # set up proxy 'word_count', # service name WordCount # service type ) valid_words = [k for k in sys.argv[1:] if '__' not in k] # filter out non-valid strings parsed_words = ' '.join(valid_words) # parse arguments (put in correct form) word_count = word_counter(parsed_words) # use service to count word print(parsed_words+' --> has '+str(word_count.count)+' words') # print input words and count
47.789474
99
0.611233
0
0
0
0
0
0
0
0
386
0.42511
ce58480e7eec21fe6db13cf13d137977321623c9
8,249
py
Python
utils/util.py
Hhhhhhhhhhao/I2T2I
6a08705b72ff38e3679a9344f987b191d3f94a25
[ "MIT" ]
null
null
null
utils/util.py
Hhhhhhhhhhao/I2T2I
6a08705b72ff38e3679a9344f987b191d3f94a25
[ "MIT" ]
null
null
null
utils/util.py
Hhhhhhhhhhao/I2T2I
6a08705b72ff38e3679a9344f987b191d3f94a25
[ "MIT" ]
null
null
null
# -*- coding: utf-8 -*- import os import torch from torch.autograd import Variable import numpy as np import scipy import matplotlib.pyplot as plt import cv2 import scipy.ndimage import shutil import scipy.misc as misc from PIL import Image def mkdirs(folders, erase=False): if type(folders) is not list: folders = [folders] for fold in folders: if not os.path.exists(fold): os.makedirs(fold) else: if erase: shutil.rmtree(fold) os.makedirs(fold) def normalize_img(X): min_, max_ = np.min(X), np.max(X) X = (X - min_) / (max_ - min_ + 1e-9) X = X * 255 return X.astype(np.uint8) def imread(imgfile): assert os.path.exists(imgfile), '{} does not exist!'.format(imgfile) srcBGR = cv2.imread(imgfile) destRGB = cv2.cvtColor(srcBGR, cv2.COLOR_BGR2RGB) return destRGB def writeImg(array, savepath): scipy.misc.imsave(savepath, array) def imresize(img, resizeratio=1): '''Take care of cv2 reshape squeeze behevaior''' if resizeratio == 1: return img outshape = (int(img.shape[1] * resizeratio), int(img.shape[0] * resizeratio)) # temp = cv2.resize(img, outshape).astype(float) temp = misc.imresize(img, size=outshape).astype(float) if len(img.shape) == 3 and img.shape[2] == 1: temp = np.reshape(temp, temp.shape + (1,)) return temp def imresize_shape(img, outshape): if len(img.shape) == 3: if img.shape[0] == 1 or img.shape[0] == 3: transpose_img = np.transpose(img, (1, 2, 0)) _img = imresize_shape(transpose_img, outshape) return np.transpose(_img, (2, 0, 1)) if len(img.shape) == 4: img_out = [] for this_img in img: img_out.append(imresize_shape(this_img, outshape)) return np.stack(img_out, axis=0) img = img.astype(np.float32) outshape = (int(outshape[1]), int(outshape[0])) # temp = cv2.resize(img, outshape).astype(float) temp = misc.imresize(img, size=outshape, interp='bilinear').astype(float) if len(img.shape) == 3 and img.shape[2] == 1: temp = np.reshape(temp, temp.shape + (1,)) return temp def imshow(img, size=None): if size is not None: plt.figure(figsize=size) else: plt.figure() plt.imshow(img) plt.show() def Indexflow(Totalnum, batch_size, random=True): numberofchunk = int(Totalnum + batch_size - 1) // int(batch_size) # the floor # Chunkfile = np.zeros((batch_size, row*col*channel)) totalIndx = np.arange(Totalnum).astype(np.int) if random is True: totalIndx = np.random.permutation(totalIndx) chunkstart = 0 for chunkidx in range(int(numberofchunk)): thisnum = min(batch_size, Totalnum - chunkidx * batch_size) thisInd = totalIndx[chunkstart: chunkstart + thisnum] chunkstart += thisnum yield thisInd def IndexH5(h5_array, indices): read_list = [] for idx in indices: read_list.append(h5_array[idx]) return np.stack(read_list, 0) def ensure_dir(path): if not os.path.exists(path): os.makedirs(path) def word_list(word_idx_list, vocab): """Take a list of word ids and a vocabulary from a dataset as inputs and return the corresponding words as a list. """ word_list = [] for i in range(len(word_idx_list)): vocab_id = word_idx_list[i] word = vocab.idx2word[vocab_id] if word == vocab.end_word: break if word != vocab.start_word: word_list.append(word) return word_list def clean_sentence(word_idx_list, vocab): """Take a list of word ids and a vocabulary from a dataset as inputs and return the corresponding sentence (as a single Python string). """ sentence = [] for i in range(len(word_idx_list)): vocab_id = word_idx_list[i] word = vocab.idx2word[vocab_id] if word == vocab.end_word: break if word != vocab.start_word: sentence.append(word) sentence = " ".join(sentence) return sentence def get_end_symbol_index(caption_list): if 1 in caption_list: return caption_list.index(1) + 1 else: return len(caption_list) def get_caption_lengths(captions_list): caption_lengths = [get_end_symbol_index(caption) for caption in captions_list] caption_lengths.sort(reverse=True) batch_captions = torch.zeros(len(captions_list), max(caption_lengths)).long() caption_lengths = torch.LongTensor(caption_lengths) for i, cap in enumerate(captions_list): end = caption_lengths[i] batch_captions[i, :end] = torch.tensor(cap[:end]).long() if torch.cuda.is_available(): batch_captions = batch_captions.cuda() caption_lengths = caption_lengths.cuda() return batch_captions, caption_lengths def to_numpy(src): if type(src) == np.ndarray: return src elif type(src) == Variable: x = src.data else: x = src return x.detach().cpu().numpy() def tensor2im(input_image, imtype=np.uint8): """"Converts a Tensor array into a numpy image array. Parameters: input_image (tensor) -- the input image tensor array imtype (type) -- the desired type of the converted numpy array """ if not isinstance(input_image, np.ndarray): if isinstance(input_image, torch.Tensor): # get the data from a variable image_tensor = input_image.data else: return input_image image_numpy = image_tensor[0].cpu().float().numpy() # convert it into a numpy array if image_numpy.shape[0] == 1: # grayscale to RGB image_numpy = np.tile(image_numpy, (3, 1, 1)) image_numpy = (np.transpose(image_numpy, (1, 2, 0)) + 1) / 2.0 * 255.0 # post-processing: tranpose and scaling else: # if it is a numpy array, do nothing image_numpy = input_image return image_numpy.astype(imtype) def diagnose_network(net, name='network'): """Calculate and print the mean of average absolute(gradients) Parameters: net (torch network) -- Torch network name (str) -- the name of the network """ mean = 0.0 count = 0 for param in net.parameters(): if param.grad is not None: mean += torch.mean(torch.abs(param.grad.data)) count += 1 if count > 0: mean = mean / count print(name) print(mean) def save_image(image_numpy, image_path): """Save a numpy image to the disk Parameters: image_numpy (numpy array) -- input numpy array image_path (str) -- the path of the image """ image_pil = Image.fromarray(image_numpy) image_pil.save(image_path) def print_numpy(x, val=True, shp=False): """Print the mean, min, max, median, std, and size of a numpy array Parameters: val (bool) -- if print the values of the numpy array shp (bool) -- if print the shape of the numpy array """ x = x.astype(np.float64) if shp: print('shape,', x.shape) if val: x = x.flatten() print('mean = %3.3f, min = %3.3f, max = %3.3f, median = %3.3f, std=%3.3f' % ( np.mean(x), np.min(x), np.max(x), np.median(x), np.std(x))) def mkdirs(paths): """create empty directories if they don't exist Parameters: paths (str list) -- a list of directory paths """ if isinstance(paths, list) and not isinstance(paths, str): for path in paths: mkdir(path) else: mkdir(paths) def mkdir(path): """create a single empty directory if it didn't exist Parameters: path (str) -- a single directory path """ if not os.path.exists(path): os.makedirs(path) def convert_back_to_text(word_idx_array, vocab): from itertools import takewhile blacklist = [vocab.word2idx[word] for word in [vocab.start_word]] predicate = lambda word_id: vocab.idx2word[word_id] != vocab.end_word sampled_caption = [vocab.idx2word[word_id] for word_id in takewhile(predicate, word_idx_array) if word_id not in blacklist] sentence = '-'.join(sampled_caption) return sentence
30.216117
127
0.63244
0
0
566
0.068614
0
0
0
0
1,804
0.218693
ce5a2416c780442544d0d5e9283fbaff98d9c5b6
9,882
py
Python
hn2pdf.py
KyrillosL/HackerNewsToPDF
489e8225d14550c874c2eb448005e8313662eac6
[ "BSD-3-Clause" ]
null
null
null
hn2pdf.py
KyrillosL/HackerNewsToPDF
489e8225d14550c874c2eb448005e8313662eac6
[ "BSD-3-Clause" ]
null
null
null
hn2pdf.py
KyrillosL/HackerNewsToPDF
489e8225d14550c874c2eb448005e8313662eac6
[ "BSD-3-Clause" ]
null
null
null
#!/usr/bin/env python """Python-Pinboard Python script for downloading your saved stories and saved comments on Hacker News. """ __version__ = "1.1" __license__ = "BSD" __copyright__ = "Copyright 2013-2014, Luciano Fiandesio" __author__ = "Luciano Fiandesio <http://fiandes.io/> & John David Pressman <http://jdpressman.com>" import argparse import json import os import sys import time import urllib import pdfkit import requests import tqdm from bs4 import BeautifulSoup from lxml import html HACKERNEWS = 'https://news.ycombinator.com' parser = argparse.ArgumentParser() parser.add_argument("username", help="The Hacker News username to grab the stories from.") parser.add_argument("password", help="The password to login with using the username.") parser.add_argument("-f", "--file", help="Filepath to store the JSON document at.") parser.add_argument("-n", "--number", default=1, type=int, help="Number of pages to grab, default 1. 0 grabs all pages.") parser.add_argument("-s", "--stories", action="store_true", help="Grab stories only.") parser.add_argument("-c", "--comments", action="store_true", help="Grab comments only.") parser.add_argument("-pdf", "--pdf", default=1, type=bool, help="Save to PDF") parser.add_argument("-o", "--output_folder", default="output/", type=str, help="Output Folder for PDF") arguments = parser.parse_args() def save_to_disk(formatted_dict, in_folder): options = { 'quiet': '' } pbar = tqdm.tqdm(formatted_dict) for e in pbar: pbar.set_description("Processing %s" % e["title"]) folder = in_folder + e["title"] + "/" if (not os.path.isdir(folder)): os.mkdir(folder) filename = e["title"] + '.pdf' # ARTICLE if not os.path.exists(folder + filename): if e['url'].endswith("pdf"): response = requests.get(e['url']) with open(folder + filename, 'wb') as f: f.write(response.content) else: try: pdfkit.from_url(e["url"], folder + filename, options=options) # open(folder+filename, 'wb').write(pdf) except Exception as error: print("Could not load url ", e["url"], " error : ", error) # Comments if not os.path.exists(folder + "comments_" + filename): url = "https://news.ycombinator.com/item?id=" + str(e["id"]) try: pdfkit.from_url(url, folder + "comments_" + filename, options=options) # open(folder + "comments_" + filename, 'wb').write(pdf) except: print("Could not load url ", url) if not os.path.exists(folder + filename): print("\n--Error, empty file for ", e["url"]) else: statinfo = os.stat(folder + filename) if statinfo.st_size <= 2048: # e.append(0) print("\n--Error, empty file for ", e["url"]) def getSavedStories(session, hnuser, page_range): """Return a list of story IDs representing your saved stories. This function does not return the actual metadata associated, just the IDs. This list is traversed and each item inside is grabbed using the Hacker News API by story ID.""" story_ids = [] for page_index in page_range: saved = session.get(HACKERNEWS + '/upvoted?id=' + hnuser + "&p=" + str(page_index)) soup = BeautifulSoup(saved.content, features="lxml") for tag in soup.findAll('td', attrs={'class': 'subtext'}): if tag.a is not type(None): a_tags = tag.find_all('a') for a_tag in a_tags: if a_tag['href'][:5] == 'item?': story_id = a_tag['href'].split('id=')[1] story_ids.append(story_id) break return story_ids def getSavedComments(session, hnuser, page_range): """Return a list of IDs representing your saved comments. This function does not return the actual metadata associated, just the IDs. This list is traversed and each item inside is grabbed using the Hacker News API by ID.""" comment_ids = [] for page_index in page_range: saved = session.get(HACKERNEWS + '/upvoted?id=' + hnuser + "&comments=t" + "&p=" + str(page_index)) soup = BeautifulSoup(saved.content, features="lxml") for tag in soup.findAll('td', attrs={'class': 'default'}): if tag.a is not type(None): a_tags = tag.find_all('a') for a_tag in a_tags: if a_tag['href'][:5] == 'item?': comment_id = a_tag['href'].split('id=')[1] comment_ids.append(comment_id) break return comment_ids def loginToHackerNews(username, password): s = requests.Session() # init a session (use cookies across requests) headers = { # we need to specify an header to get the right cookie 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:25.0) Gecko/20100101 Firefox/25.0', 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", } # Build the login POST data and make the login request. payload = { 'whence': 'news', 'acct': username, 'pw': password } auth = s.post(HACKERNEWS + '/login', data=payload, headers=headers) if 'Bad login' in str(auth.content): raise Exception("Hacker News authentication failed!") if not username in str(auth.content): raise Exception("Hacker News didn't succeed, username not displayed.") return s # return the http session def getHackerNewsItem(item_id): """Get an 'item' as specified in the HackerNews v0 API.""" time.sleep(0.2) item_json_link = "https://hacker-news.firebaseio.com/v0/item/" + item_id + ".json" try: with urllib.request.urlopen(item_json_link) as item_json: current_story = json.loads(item_json.read().decode('utf-8')) if "kids" in current_story: del current_story["kids"] # Escape / in name for a later use current_story["title"] = current_story["title"].replace("/", "-") return current_story except urllib.error.URLError: return {"title": "Item " + item_id + " could not be retrieved", "id": item_id} def item2stderr(item_id, item_count, item_total): sys.stderr.write("Got item " + item_id + ". ({} of {})\n".format(item_count, item_total)) def get_links(session, url): print("Fetching", url) response = session.get(url) tree = html.fromstring(response.content) morelink = tree.xpath('string(//a[@class="morelink"]/@href)') return morelink def main(): json_items = {"saved_stories": list(), "saved_comments": list()} if arguments.stories and arguments.comments: # Assume that if somebody uses both flags they mean to grab both arguments.stories = False arguments.comments = False item_count = 0 session = loginToHackerNews(arguments.username, arguments.password) # if n = 0 -> Get the number of pages and parse them nb_pages = arguments.number if nb_pages == 0: nb_pages = 1 morelink = get_links(session, 'https://news.ycombinator.com/upvoted?id=' + arguments.username) while morelink: morelink = get_links(session, "https://news.ycombinator.com/" + morelink) nb_pages += 1 print('nb_pages ', nb_pages) page_range = range(1, nb_pages + 1) if arguments.stories or (not arguments.stories and not arguments.comments): print("Getting Stories as JSON") story_ids = getSavedStories(session, arguments.username, page_range) pbar = tqdm.tqdm(story_ids) for story_id in pbar: should_analyse = True # Load the previous json file and check if we already analysed it before if os.path.exists(arguments.file) and os.stat(arguments.file).st_size != 0: with open(arguments.file) as outfile: data = json.load(outfile) if "saved_stories" in data: for story in data["saved_stories"]: # print(stories) if story_id == str(story["id"]): # print("same") # pbar.set_description("Processing %s" % e[0]) should_analyse = False json_items["saved_stories"].append(story) if should_analyse: json_items["saved_stories"].append(getHackerNewsItem(story_id)) if arguments.comments or (not arguments.stories and not arguments.comments): item_count = 0 comment_ids = getSavedComments(session, arguments.username, page_range) for comment_id in comment_ids: json_items["saved_comments"].append(getHackerNewsItem(comment_id)) item_count += 1 item2stderr(comment_id, item_count, len(comment_ids)) if arguments.file: with open(arguments.file, 'w') as outfile: json.dump(json_items, outfile, indent=4) if arguments.pdf: print("Exporting to PDF") output_folder = arguments.output_folder if (not os.path.isdir(output_folder)): os.mkdir(output_folder) save_to_disk(json_items["saved_stories"], output_folder) if __name__ == "__main__": main()
40.334694
121
0.589152
0
0
0
0
0
0
0
0
3,063
0.309957
ce5a8256603662fe067ec0abfb76762e08552066
558
py
Python
backend/step_functions/default.py
barak-obama/Game-Of-Life
3e84e5dda2561c4b87249de64680a4ea504dd42e
[ "MIT" ]
null
null
null
backend/step_functions/default.py
barak-obama/Game-Of-Life
3e84e5dda2561c4b87249de64680a4ea504dd42e
[ "MIT" ]
null
null
null
backend/step_functions/default.py
barak-obama/Game-Of-Life
3e84e5dda2561c4b87249de64680a4ea504dd42e
[ "MIT" ]
null
null
null
import itertools def next_step(board, i, j): xMin = max(0, j - 1) xMax = min(board.shape[1] - 1, j +1) yMin = max(0, i - 1) yMax = min(board.shape[0] - 1, i +1) iteration = list(itertools.product(range(yMin, yMax + 1), range(xMin, xMax+1))) iteration.remove((i,j)) sum = 0; for (k, m) in iteration: if board[k, m] == 1: sum += 1 if board[i, j] == 1: if sum < 2 or sum > 3: return 0 else: return 1 elif sum == 3: return 1 else: return 0
23.25
83
0.483871
0
0
0
0
0
0
0
0
0
0
ce5ab22e009ac58d14c27fe38208f968a51e0d2e
2,110
py
Python
rapid_response_xblock/models.py
HamzaIbnFarooq/rapid-response-xblock
dbc6bfbaab0f583680816ba86f0d43c84c931d58
[ "BSD-3-Clause" ]
null
null
null
rapid_response_xblock/models.py
HamzaIbnFarooq/rapid-response-xblock
dbc6bfbaab0f583680816ba86f0d43c84c931d58
[ "BSD-3-Clause" ]
104
2018-02-02T20:51:00.000Z
2022-03-31T08:44:24.000Z
rapid_response_xblock/models.py
HamzaIbnFarooq/rapid-response-xblock
dbc6bfbaab0f583680816ba86f0d43c84c931d58
[ "BSD-3-Clause" ]
1
2020-12-16T08:24:02.000Z
2020-12-16T08:24:02.000Z
""" Rapid Response block models """ from django.conf import settings from django.db import models from django.utils.encoding import python_2_unicode_compatible from jsonfield import JSONField from model_utils.models import TimeStampedModel from opaque_keys.edx.django.models import ( CourseKeyField, UsageKeyField, ) @python_2_unicode_compatible class RapidResponseRun(models.Model): """ Stores information for a group of RapidResponseSubmission objects """ problem_usage_key = UsageKeyField(db_index=True, max_length=255) course_key = CourseKeyField(db_index=True, max_length=255) open = models.BooleanField(default=False, null=False) created = models.DateTimeField(auto_now_add=True, db_index=True) modified = models.DateTimeField(auto_now=True) class Meta: ordering = ['-created'] def __str__(self): return ( "id={id} created={created} problem_usage_key={problem_usage_key} " "course_key={course_key} open={open}".format( id=self.id, created=self.created.isoformat(), problem_usage_key=self.problem_usage_key, course_key=self.course_key, open=self.open, ) ) @python_2_unicode_compatible class RapidResponseSubmission(TimeStampedModel): """ Stores the student submissions for a problem that is configured with rapid response """ user = models.ForeignKey( settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True, db_index=True, ) run = models.ForeignKey( RapidResponseRun, on_delete=models.SET_NULL, null=True, db_index=True ) answer_id = models.CharField(null=True, max_length=255) answer_text = models.CharField(null=True, max_length=4096) event = JSONField() def __str__(self): return ( "user={user} run={run} answer_id={answer_id}".format( user=self.user, run=self.run, answer_id=self.answer_id, ) )
28.133333
78
0.652607
1,719
0.814692
0
0
1,777
0.84218
0
0
377
0.178673
ce5beb636533234d09e40c6e181344e4d00f51e7
371
py
Python
sched_slack_bot/reminder/sender.py
Germandrummer92/SchedSlackBot
d211f7c0d78eb8ebbc1f22cc186c94fc61bad491
[ "MIT" ]
null
null
null
sched_slack_bot/reminder/sender.py
Germandrummer92/SchedSlackBot
d211f7c0d78eb8ebbc1f22cc186c94fc61bad491
[ "MIT" ]
null
null
null
sched_slack_bot/reminder/sender.py
Germandrummer92/SchedSlackBot
d211f7c0d78eb8ebbc1f22cc186c94fc61bad491
[ "MIT" ]
null
null
null
import abc from sched_slack_bot.model.reminder import Reminder class ReminderSender(abc.ABC): @abc.abstractmethod def send_reminder(self, reminder: Reminder) -> None: raise NotImplementedError("Not Implemented") @abc.abstractmethod def send_skip_message(self, reminder: Reminder) -> None: raise NotImplementedError("Not Implemented")
24.733333
60
0.738544
304
0.819407
0
0
262
0.706199
0
0
34
0.091644
ce5d174c1f7c2c86516c002a84d1f0b2d728c91d
1,987
py
Python
app/routes.py
systemicsmitty/TI4_battle_sim
b4ed142ff57d19ed50705ba40f83b8b3b7e3a774
[ "MIT" ]
null
null
null
app/routes.py
systemicsmitty/TI4_battle_sim
b4ed142ff57d19ed50705ba40f83b8b3b7e3a774
[ "MIT" ]
null
null
null
app/routes.py
systemicsmitty/TI4_battle_sim
b4ed142ff57d19ed50705ba40f83b8b3b7e3a774
[ "MIT" ]
null
null
null
from flask import render_template from app import app, html_generator import app.calculator.calculator as calc from app.route_helpers import units_from_form, options_from_form, options_list, flash_errors from app.forms import InputForm from collections import defaultdict @app.route('/', methods=['GET', 'POST']) @app.route('/index', methods=['GET', 'POST']) def index(): form = InputForm() if form.validate_on_submit(): attacker, defender = units_from_form(form) options = options_from_form(form) outcomes = calc.calculate(attacker, defender, options, test=False) defaults = form checkboxes = defaultdict(lambda: "") for opt in options.keys(): checkboxes[opt] = "checked" if options[opt] else "" hidden = defaultdict(lambda: False) for opt in options.keys(): if "hide" in opt: if "att_" in opt and not options["att_faction"].lower().split("-")[0] in opt: hidden[opt] = True if "def_" in opt and not options["def_faction"].lower().split("-")[0] in opt: hidden[opt] = True boxes = html_generator.make_boxes(checkboxes, hidden) return render_template('index.html', outcomes=outcomes, form=form, defaults=defaults, checkboxes=checkboxes, boxes=boxes) else: print("in else") flash_errors(form) outcomes = [0, 0, 0] defaults = defaultdict(lambda: {"data": "0"}) checkboxes = defaultdict(lambda: "") checkboxes["att_riskdirecthit"] = "checked" checkboxes["def_riskdirecthit"] = "checked" hidden = defaultdict(lambda: False) for opt in options_list(): if "hide" in opt: hidden[opt] = True boxes = html_generator.make_boxes(checkboxes, hidden) return render_template('index.html', outcomes=outcomes, form=form, defaults=defaults, checkboxes=checkboxes, boxes=boxes)
36.127273
116
0.631605
0
0
0
0
1,712
0.8616
0
0
202
0.101661
ce5d8d0f3c28fed69d76da9c81283dbdc6272f7e
1,505
py
Python
code/dependancy/smaliparser.py
OmkarMozar/CUPAP
6055f423e3f9b8bb1a44dd8fab73630554363b3d
[ "Apache-2.0" ]
null
null
null
code/dependancy/smaliparser.py
OmkarMozar/CUPAP
6055f423e3f9b8bb1a44dd8fab73630554363b3d
[ "Apache-2.0" ]
null
null
null
code/dependancy/smaliparser.py
OmkarMozar/CUPAP
6055f423e3f9b8bb1a44dd8fab73630554363b3d
[ "Apache-2.0" ]
null
null
null
from smalisca.core.smalisca_main import SmaliscaApp from smalisca.modules.module_smali_parser import SmaliParser from smalisca.core.smalisca_app import App from smalisca.core.smalisca_logging import log from smalisca.modules.module_sql_models import AppSQLModel import smalisca.core.smalisca_config as config import multiprocessing import os from cement.core import controller from cement.core.controller import CementBaseController import json def parsesmali(location): # Create new smalisca app # You'll have to create a new app in order to set logging # settins and so on. print "Parsing smali files.." app = SmaliscaApp() app.setup() # Set log level app.log.set_level('info') # Specify the location where your APK has been dumped #location = '/root/Downloads/Project/Gmailnew' # Specify file name suffix suffix = 'smali' # Create a new parser parser = SmaliParser(location, suffix) # Go for it! parser.run() # Get results results = parser.get_results() ''' data=app.get_all() with open('iii.json', 'w') as outfile: json.dump(data, outfile) ''' ap = App(__name__) ap.add_location(location) ap.add_parser("%s - %s" % (config.PROJECT_NAME, config.PROJECT_VERSION)) # Append classes for c in results: ap.add_class_obj(c) # Write results to JSON log.info("Exporting results to JSON") base=os.path.basename(location) out_path = os.getcwd()+'/Data/Apk_data/'+base+'/'+base+'method.json' ap.write_json(out_path) print out_path," created"
24.672131
73
0.743522
0
0
0
0
0
0
0
0
538
0.357475
ce5dcda8e728127b9f9d9754ec7ec959e800ef14
31,373
py
Python
modules/users_and_roles_tab.py
scrummastermind/sumologictoolbox
02d9acb970943521685091d36b8d5135e817c22c
[ "Apache-2.0" ]
null
null
null
modules/users_and_roles_tab.py
scrummastermind/sumologictoolbox
02d9acb970943521685091d36b8d5135e817c22c
[ "Apache-2.0" ]
null
null
null
modules/users_and_roles_tab.py
scrummastermind/sumologictoolbox
02d9acb970943521685091d36b8d5135e817c22c
[ "Apache-2.0" ]
null
null
null
class_name = 'users_and_roles_tab' from qtpy import QtCore, QtGui, QtWidgets, uic import os import sys import re import pathlib import json from logzero import logger from modules.sumologic import SumoLogic from modules.shared import ShowTextDialog class users_and_roles_tab(QtWidgets.QWidget): def __init__(self, mainwindow): super(users_and_roles_tab, self).__init__() self.mainwindow = mainwindow self.tab_name = 'Users and Roles' self.cred_usage = 'both' users_and_roles_widget_ui = os.path.join(self.mainwindow.basedir, 'data/users_and_roles.ui') uic.loadUi(users_and_roles_widget_ui, self) # Connect the UI buttons to methods # Connect Update Buttons self.pushButtonUpdateUsersAndRolesLeft.clicked.connect(lambda: self.update_users_and_roles_lists( self.listWidgetUsersLeft, self.listWidgetRolesLeft, self.mainwindow.loadedapiurls[str(self.mainwindow.comboBoxRegionLeft.currentText())], str(self.mainwindow.lineEditUserNameLeft.text()), str(self.mainwindow.lineEditPasswordLeft.text()) )) self.pushButtonUpdateUsersAndRolesRight.clicked.connect(lambda: self.update_users_and_roles_lists( self.listWidgetUsersRight, self.listWidgetRolesRight, self.mainwindow.loadedapiurls[str(self.mainwindow.comboBoxRegionRight.currentText())], str(self.mainwindow.lineEditUserNameRight.text()), str(self.mainwindow.lineEditPasswordRight.text()) )) # Connect Search Bars self.lineEditSearchUsersLeft.textChanged.connect(lambda: self.set_listwidget_filter( self.listWidgetUsersLeft, self.lineEditSearchUsersLeft.text() )) self.lineEditSearchUsersRight.textChanged.connect(lambda: self.set_listwidget_filter( self.listWidgetUsersRight, self.lineEditSearchUsersRight.text() )) self.lineEditSearchRolesLeft.textChanged.connect(lambda: self.set_listwidget_filter( self.listWidgetRolesLeft, self.lineEditSearchRolesLeft.text() )) self.lineEditSearchRolesRight.textChanged.connect(lambda: self.set_listwidget_filter( self.listWidgetRolesRight, self.lineEditSearchRolesRight.text() )) self.pushButtonCopyUserLeftToRight.clicked.connect(lambda: self.copy_user( self.listWidgetUsersLeft, self.listWidgetUsersRight, self.listWidgetRolesRight, self.mainwindow.loadedapiurls[str(self.mainwindow.comboBoxRegionLeft.currentText())], str(self.mainwindow.lineEditUserNameLeft.text()), str(self.mainwindow.lineEditPasswordLeft.text()), self.mainwindow.loadedapiurls[str(self.mainwindow.comboBoxRegionRight.currentText())], str(self.mainwindow.lineEditUserNameRight.text()), str(self.mainwindow.lineEditPasswordRight.text()) )) self.pushButtonCopyUserRightToLeft.clicked.connect(lambda: self.copy_user( self.listWidgetUsersRight, self.listWidgetUsersLeft, self.listWidgetRolesLeft, self.mainwindow.loadedapiurls[str(self.mainwindow.comboBoxRegionRight.currentText())], str(self.mainwindow.lineEditUserNameRight.text()), str(self.mainwindow.lineEditPasswordRight.text()), self.mainwindow.loadedapiurls[str(self.mainwindow.comboBoxRegionLeft.currentText())], str(self.mainwindow.lineEditUserNameLeft.text()), str(self.mainwindow.lineEditPasswordLeft.text()) )) self.pushButtonBackupUserLeft.clicked.connect(lambda: self.backup_user( self.listWidgetUsersLeft, self.mainwindow.loadedapiurls[str(self.mainwindow.comboBoxRegionLeft.currentText())], str(self.mainwindow.lineEditUserNameLeft.text()), str(self.mainwindow.lineEditPasswordLeft.text()) )) self.pushButtonBackupUserRight.clicked.connect(lambda: self.backup_user( self.listWidgetUsersRight, self.mainwindow.loadedapiurls[str(self.mainwindow.comboBoxRegionRight.currentText())], str(self.mainwindow.lineEditUserNameRight.text()), str(self.mainwindow.lineEditPasswordRight.text()) )) self.pushButtonUserJSONLeft.clicked.connect(lambda: self.view_user_json( self.listWidgetUsersLeft, self.mainwindow.loadedapiurls[str(self.mainwindow.comboBoxRegionLeft.currentText())], str(self.mainwindow.lineEditUserNameLeft.text()), str(self.mainwindow.lineEditPasswordLeft.text()) )) self.pushButtonUserJSONRight.clicked.connect(lambda: self.view_user_json( self.listWidgetUsersRight, self.mainwindow.loadedapiurls[str(self.mainwindow.comboBoxRegionRight.currentText())], str(self.mainwindow.lineEditUserNameRight.text()), str(self.mainwindow.lineEditPasswordRight.text()) )) self.pushButtonRestoreUserLeft.clicked.connect(lambda: self.restore_user( self.listWidgetRolesLeft, self.listWidgetUsersLeft, self.mainwindow.loadedapiurls[str(self.mainwindow.comboBoxRegionLeft.currentText())], str(self.mainwindow.lineEditUserNameLeft.text()), str(self.mainwindow.lineEditPasswordLeft.text()) )) self.pushButtonRestoreUserRight.clicked.connect(lambda: self.restore_user( self.listWidgetRolesRight, self.listWidgetUsersRight, self.mainwindow.loadedapiurls[str(self.mainwindow.comboBoxRegionRight.currentText())], str(self.mainwindow.lineEditUserNameRight.text()), str(self.mainwindow.lineEditPasswordRight.text()) )) self.pushButtonDeleteUserLeft.clicked.connect(lambda: self.delete_user( self.listWidgetRolesLeft, self.listWidgetUsersLeft, self.mainwindow.loadedapiurls[str(self.mainwindow.comboBoxRegionLeft.currentText())], str(self.mainwindow.lineEditUserNameLeft.text()), str(self.mainwindow.lineEditPasswordLeft.text()) )) self.pushButtonDeleteUserRight.clicked.connect(lambda: self.delete_user( self.listWidgetRolesRight, self.listWidgetUsersRight, self.mainwindow.loadedapiurls[str(self.mainwindow.comboBoxRegionRight.currentText())], str(self.mainwindow.lineEditUserNameRight.text()), str(self.mainwindow.lineEditPasswordRight.text()) )) self.pushButtonCopyRoleLeftToRight.clicked.connect(lambda: self.copy_role( self.listWidgetRolesLeft, self.listWidgetRolesRight, self.listWidgetUsersRight, self.mainwindow.loadedapiurls[str(self.mainwindow.comboBoxRegionLeft.currentText())], str(self.mainwindow.lineEditUserNameLeft.text()), str(self.mainwindow.lineEditPasswordLeft.text()), self.mainwindow.loadedapiurls[str(self.mainwindow.comboBoxRegionRight.currentText())], str(self.mainwindow.lineEditUserNameRight.text()), str(self.mainwindow.lineEditPasswordRight.text()) )) self.pushButtonCopyRoleRightToLeft.clicked.connect(lambda: self.copy_role( self.listWidgetRolesRight, self.listWidgetRolesLeft, self.listWidgetUsersLeft, self.mainwindow.loadedapiurls[str(self.mainwindow.comboBoxRegionRight.currentText())], str(self.mainwindow.lineEditUserNameRight.text()), str(self.mainwindow.lineEditPasswordRight.text()), self.mainwindow.loadedapiurls[str(self.mainwindow.comboBoxRegionLeft.currentText())], str(self.mainwindow.lineEditUserNameLeft.text()), str(self.mainwindow.lineEditPasswordLeft.text()) )) self.pushButtonBackupRoleLeft.clicked.connect(lambda: self.backup_role( self.listWidgetRolesLeft, self.mainwindow.loadedapiurls[str(self.mainwindow.comboBoxRegionLeft.currentText())], str(self.mainwindow.lineEditUserNameLeft.text()), str(self.mainwindow.lineEditPasswordLeft.text()) )) self.pushButtonBackupRoleRight.clicked.connect(lambda: self.backup_role( self.listWidgetRolesRight, self.mainwindow.loadedapiurls[str(self.mainwindow.comboBoxRegionRight.currentText())], str(self.mainwindow.lineEditUserNameRight.text()), str(self.mainwindow.lineEditPasswordRight.text()) )) self.pushButtonRoleJSONLeft.clicked.connect(lambda: self.view_role_json( self.listWidgetRolesLeft, self.mainwindow.loadedapiurls[str(self.mainwindow.comboBoxRegionLeft.currentText())], str(self.mainwindow.lineEditUserNameLeft.text()), str(self.mainwindow.lineEditPasswordLeft.text()) )) self.pushButtonRoleJSONRight.clicked.connect(lambda: self.view_role_json( self.listWidgetRolesRight, self.mainwindow.loadedapiurls[str(self.mainwindow.comboBoxRegionRight.currentText())], str(self.mainwindow.lineEditUserNameRight.text()), str(self.mainwindow.lineEditPasswordRight.text()) )) self.pushButtonRestoreRoleLeft.clicked.connect(lambda: self.restore_role( self.listWidgetRolesLeft, self.listWidgetUsersLeft, self.mainwindow.loadedapiurls[str(self.mainwindow.comboBoxRegionLeft.currentText())], str(self.mainwindow.lineEditUserNameLeft.text()), str(self.mainwindow.lineEditPasswordLeft.text()) )) self.pushButtonRestoreRoleRight.clicked.connect(lambda: self.restore_role( self.listWidgetRolesRight, self.listWidgetUsersRight, self.mainwindow.loadedapiurls[str(self.mainwindow.comboBoxRegionRight.currentText())], str(self.mainwindow.lineEditUserNameRight.text()), str(self.mainwindow.lineEditPasswordRight.text()) )) self.pushButtonDeleteRoleLeft.clicked.connect(lambda: self.delete_role( self.listWidgetRolesLeft, self.listWidgetUsersLeft, self.mainwindow.loadedapiurls[str(self.mainwindow.comboBoxRegionLeft.currentText())], str(self.mainwindow.lineEditUserNameLeft.text()), str(self.mainwindow.lineEditPasswordLeft.text()) )) self.pushButtonDeleteRoleRight.clicked.connect(lambda: self.delete_role( self.listWidgetRolesRight, self.listWidgetUsersRight, self.mainwindow.loadedapiurls[str(self.mainwindow.comboBoxRegionRight.currentText())], str(self.mainwindow.lineEditUserNameRight.text()), str(self.mainwindow.lineEditPasswordRight.text()) )) def reset_stateful_objects(self, side='both'): if side == 'both': left = True right = True if side == 'left': left = True right = False if side == 'right': left = False right = True if left: self.listWidgetUsersLeft.clear() self.listWidgetUsersLeft.currentcontent = {} self.listWidgetUsersLeft.updated = False self.listWidgetRolesLeft.clear() self.listWidgetRolesLeft.currentcontent = {} self.listWidgetRolesLeft.updated = False if right: self.listWidgetUsersRight.clear() self.listWidgetUsersRight.currentcontent = {} self.listWidgetUsersRight.updated = False self.listWidgetRolesRight.clear() self.listWidgetRolesRight.currentcontent = {} self.listWidgetRolesRight.updated = False def set_listwidget_filter(self, ListWidget, filtertext): for row in range(ListWidget.count()): item = ListWidget.item(row) widget = ListWidget.itemWidget(item) if filtertext: item.setHidden(not filtertext in item.text()) else: item.setHidden(False) def update_users_and_roles_lists(self, UserListWidget, RoleListWidget, url, id, key): sumo = SumoLogic(id, key, endpoint=url, log_level=self.mainwindow.log_level) try: logger.info("[Users and Roles] Updating Users and Roles Lists") UserListWidget.currentcontent = sumo.get_users_sync() RoleListWidget.currentcontent = sumo.get_roles_sync() self.update_users_and_roles_listwidgets(UserListWidget, RoleListWidget) return except Exception as e: UserListWidget.updated = False RoleListWidget.updated = False logger.exception(e) self.mainwindow.errorbox('Something went wrong:\n\n' + str(e)) return def update_users_and_roles_listwidgets(self, UserListWidget, RoleListWidget): try: UserListWidget.clear() RoleListWidget.clear() UserListWidget.setSortingEnabled(True) for object in UserListWidget.currentcontent: item_name = object['firstName'] + ' ' + object['lastName'] item = QtWidgets.QListWidgetItem(item_name) item.details = object UserListWidget.addItem(item) # populate the list widget in the GUI UserListWidget.updated = True for object in RoleListWidget.currentcontent: item_name = object['name'] item = QtWidgets.QListWidgetItem(item_name) item.details = object RoleListWidget.addItem(item) # populate the list widget in the GUI RoleListWidget.updated = True except Exception as e: logger.exception(e) self.mainwindow.errorbox('Something went wrong:\n\n' + str(e)) return def copy_user(self, UserListWidgetFrom, UserListWidgetTo, RoleListWidgetTo, fromurl, fromid, fromkey, tourl, toid, tokey): # Need to add check if user already exists and interactively ask if any missing roles should be created logger.info("[Users and Roles]Copying User(s)") try: selecteditems = UserListWidgetFrom.selectedItems() if len(selecteditems) > 0: # make sure something was selected fromsumo = SumoLogic(fromid, fromkey, endpoint=fromurl, log_level=self.mainwindow.log_level) tosumo = SumoLogic(toid, tokey, endpoint=tourl, log_level=self.mainwindow.log_level) for selecteditem in selecteditems: user_id = selecteditem.details['id'] user = fromsumo.get_user_and_roles(user_id) dest_roles = tosumo.get_roles_sync() for source_role in user['roles']: role_already_exists_in_dest = False source_role_id = source_role['id'] for dest_role in dest_roles: if dest_role['name'] == source_role['name']: role_already_exists_in_dest = True dest_role_id = dest_role['id'] if role_already_exists_in_dest: user['roleIds'].append(dest_role_id) user['roleIds'].remove(source_role_id) else: source_role['users'] = [] tosumo.create_role(source_role) updated_dest_roles = tosumo.get_roles_sync() for updated_dest_role in updated_dest_roles: if updated_dest_role['name'] == source_role['name']: user['roleIds'].append(updated_dest_role['id']) user['roleIds'].remove(source_role_id) tosumo.create_user(user['firstName'], user['lastName'], user['email'], user['roleIds']) self.update_users_and_roles_lists(UserListWidgetTo, RoleListWidgetTo, tourl, toid, tokey) return else: self.mainwindow.errorbox('You have not made any selections.') return except Exception as e: logger.exception(e) self.mainwindow.errorbox('Something went wrong:' + str(e)) self.update_users_and_roles_lists(UserListWidgetTo, RoleListWidgetTo, tourl, toid, tokey) return def backup_user(self, UserListWidget, url, id, key): logger.info("[Users and Roles]Backing Up User(s)") selecteditems = UserListWidget.selectedItems() if len(selecteditems) > 0: # make sure something was selected savepath = str(QtWidgets.QFileDialog.getExistingDirectory(self, "Select Backup Directory")) if os.access(savepath, os.W_OK): message = '' sumo = SumoLogic(id, key, endpoint=url, log_level=self.mainwindow.log_level) for selecteditem in selecteditems: user_id = selecteditem.details['id'] try: export = sumo.get_user_and_roles(user_id) savefilepath = pathlib.Path(savepath + r'/' + str(selecteditem.text()) + r'.user.json') if savefilepath: with savefilepath.open(mode='w') as filepointer: json.dump(export, filepointer) message = message + str(selecteditem.text()) + r'.json' + '\n' except Exception as e: logger.exception(e) self.mainwindow.errorbox('Something went wrong:\n\n' + str(e)) return self.mainwindow.infobox('Wrote files: \n\n' + message) else: self.mainwindow.errorbox("You don't have permissions to write to that directory") else: self.mainwindow.errorbox('No user selected.') return def view_user_json(self, UserListWidget, url, id, key): logger.info("[Users and Roles]Viewing User(s) JSON") selecteditems = UserListWidget.selectedItems() if len(selecteditems) > 0: # make sure something was selected try: sumo = SumoLogic(id, key, endpoint=url, log_level=self.mainwindow.log_level) json_text = '' for selecteditem in selecteditems: user_id = selecteditem.details['id'] user = sumo.get_user(user_id) json_text = json_text + json.dumps(user, indent=4, sort_keys=True) + '\n\n' self.json_window = ShowTextDialog('JSON', json_text, self.mainwindow.basedir) self.json_window.show() except Exception as e: logger.exception(e) self.mainwindow.errorbox('Something went wrong:\n\n' + str(e)) return else: self.mainwindow.errorbox('No user selected.') return def restore_user(self, RoleListWidget, UserListWidget, url, id, key): logger.info("[Users and Roles]Restoring User(s)") if UserListWidget.updated == True: filter = "JSON (*.json)" filelist, status = QtWidgets.QFileDialog.getOpenFileNames(self, "Open file(s)...", os.getcwd(), filter) if len(filelist) > 0: sumo = SumoLogic(id, key, endpoint=url, log_level=self.mainwindow.log_level) for file in filelist: try: with open(file) as filepointer: user = json.load(filepointer) except Exception as e: logger.exception(e) self.mainwindow.errorbox( "Something went wrong reading the file. Do you have the right file permissions? Does it contain valid JSON?") return try: dest_roles = sumo.get_roles_sync() for source_role in user['roles']: role_already_exists_in_dest = False source_role_id = source_role['id'] for dest_role in dest_roles: if dest_role['name'] == source_role['name']: role_already_exists_in_dest = True dest_role_id = dest_role['id'] if role_already_exists_in_dest: # print('found role at target: ' + source_role['name']) user['roleIds'].append(dest_role_id) user['roleIds'].remove(source_role_id) else: source_role['users'] = [] sumo.create_role(source_role) updated_dest_roles = sumo.get_roles_sync() for updated_dest_role in updated_dest_roles: if updated_dest_role['name'] == source_role['name']: user['roleIds'].append(updated_dest_role['id']) user['roleIds'].remove(source_role_id) # print('Did not find role at target. Added role:' + source_role['name']) # print('modified user: ' + str(user)) sumo.create_user(user['firstName'], user['lastName'], user['email'], user['roleIds']) except Exception as e: logger.exception(e) self.mainwindow.errorbox('Something went wrong:\n\n' + str(e)) return self.update_users_and_roles_lists(UserListWidget, RoleListWidget, url, id, key) else: self.mainwindow.errorbox("Please select at least one file to restore.") return else: self.mainwindow.errorbox("Please update the directory list before restoring content") return def delete_user(self, RoleListWidget, UserListWidget, url, id, key): logger.info("[Users and Roles]Deleting User(s)") selecteditems = UserListWidget.selectedItems() if len(selecteditems) > 0: # make sure something was selected message = "You are about to delete the following item(s):\n\n" for selecteditem in selecteditems: message = message + str(selecteditem.text()) + "\n" message = message + ''' This is exceedingly DANGEROUS!!!! Please be VERY, VERY, VERY sure you want to do this! You could lose quite a bit of work if you delete the wrong thing(s). If you are absolutely sure, type "DELETE" in the box below. ''' text, result = QtWidgets.QInputDialog.getText(self, 'Warning!!', message) if (result and (str(text) == 'DELETE')): try: sumo = SumoLogic(id, key, endpoint=url, log_level=self.mainwindow.log_level) for selecteditem in selecteditems: item_id = selecteditem.details['id'] result = sumo.delete_user(item_id) self.update_users_and_roles_lists(UserListWidget, RoleListWidget, url, id, key) return except Exception as e: logger.exception(e) self.mainwindow.errorbox('Something went wrong:\n\n' + str(e)) else: self.mainwindow.errorbox('You need to select something before you can delete it.') return def copy_role(self, RoleListWidgetFrom, RoleListWidgetTo, UserListWidgetTo, fromurl, fromid, fromkey, tourl, toid, tokey): logger.info("[Users and Roles]Copying Role(s)") try: selecteditems = RoleListWidgetFrom.selectedItems() if len(selecteditems) > 0: # make sure something was selected fromsumo = SumoLogic(fromid, fromkey, endpoint=fromurl, log_level=self.mainwindow.log_level) tosumo = SumoLogic(toid, tokey, endpoint=tourl, log_level=self.mainwindow.log_level) for selecteditem in selecteditems: role_id = selecteditem.details['id'] role = fromsumo.get_role(role_id) status = tosumo.create_role(role) self.update_users_and_roles_lists(UserListWidgetTo, RoleListWidgetTo, tourl, toid, tokey) return else: self.mainwindow.errorbox('You have not made any selections.') return except Exception as e: logger.exception(e) self.mainwindow.errorbox('Something went wrong:' + str(e)) self.update_users_and_roles_lists(UserListWidgetTo, RoleListWidgetTo, tourl, toid, tokey) return def backup_role(self, RoleListWidget, url, id, key): logger.info("[Users and Roles]Backing Up Role(s)") selecteditems = RoleListWidget.selectedItems() if len(selecteditems) > 0: # make sure something was selected savepath = str(QtWidgets.QFileDialog.getExistingDirectory(self, "Select Backup Directory")) if os.access(savepath, os.W_OK): message = '' sumo = SumoLogic(id, key, endpoint=url, log_level=self.mainwindow.log_level) for selecteditem in selecteditems: item_id = selecteditem.details['id'] try: export = sumo.get_role(item_id) savefilepath = pathlib.Path(savepath + r'/' + str(selecteditem.text()) + r'.role.json') if savefilepath: with savefilepath.open(mode='w') as filepointer: json.dump(export, filepointer) message = message + str(selecteditem.text()) + r'.json' + '\n' except Exception as e: logger.exception(e) self.mainwindow.errorbox('Something went wrong:\n\n' + str(e)) return self.mainwindow.infobox('Wrote files: \n\n' + message) else: self.mainwindow.errorbox("You don't have permissions to write to that directory") else: self.mainwindow.errorbox('No content selected.') return def view_role_json(self, RoleListWidget, url, id, key): logger.info("[Users and Roles]Viewing Roles(s) JSON") selecteditems = RoleListWidget.selectedItems() if len(selecteditems) > 0: # make sure something was selected try: sumo = SumoLogic(id, key, endpoint=url, log_level=self.mainwindow.log_level) json_text = '' for selecteditem in selecteditems: role_id = selecteditem.details['id'] role = sumo.get_role(role_id) json_text = json_text + json.dumps(role, indent=4, sort_keys=True) + '\n\n' self.json_window = ShowTextDialog('JSON', json_text, self.mainwindow.basedir) self.json_window.show() except Exception as e: logger.exception(e) self.mainwindow.errorbox('Something went wrong:\n\n' + str(e)) return else: self.mainwindow.errorbox('No role selected.') return def restore_role(self, RoleListWidget, UserListWidget, url, id, key): logger.info("[Users and Roles]Restoring Role(s)") if RoleListWidget.updated == True: filter = "JSON (*.json)" filelist, status = QtWidgets.QFileDialog.getOpenFileNames(self, "Open file(s)...", os.getcwd(), filter) if len(filelist) > 0: sumo = SumoLogic(id, key, endpoint=url, log_level=self.mainwindow.log_level) for file in filelist: try: with open(file) as filepointer: role_backup = json.load(filepointer) except Exception as e: logger.exception(e) self.mainwindow.errorbox( "Something went wrong reading the file. Do you have the right file permissions? Does it contain valid JSON?") return try: role_backup['users'] = [] status = sumo.create_role(role_backup) except Exception as e: logger.exception(e) self.mainwindow.errorbox('Something went wrong:\n\n' + str(e)) return self.update_users_and_roles_lists(UserListWidget, RoleListWidget, url, id, key) else: self.mainwindow.errorbox("Please select at least one file to restore.") return else: self.mainwindow.errorbox("Please update the directory list before restoring content") return def delete_role(self, RoleListWidget, UserListWidget, url, id, key): logger.info("[Users and Roles]Deleting Role(s)") selecteditems = RoleListWidget.selectedItems() if len(selecteditems) > 0: # make sure something was selected message = "You are about to delete the following item(s):\n\n" for selecteditem in selecteditems: message = message + str(selecteditem.text()) + "\n" message = message + ''' This is exceedingly DANGEROUS!!!! Please be VERY, VERY, VERY sure you want to do this! You could lose quite a bit of work if you delete the wrong thing(s). If you are absolutely sure, type "DELETE" in the box below. ''' text, result = QtWidgets.QInputDialog.getText(self, 'Warning!!', message) if (result and (str(text) == 'DELETE')): try: sumo = SumoLogic(id, key, endpoint=url, log_level=self.mainwindow.log_level) for selecteditem in selecteditems: item_id = selecteditem.details['id'] result = sumo.delete_role(item_id) self.update_users_and_roles_lists(UserListWidget, RoleListWidget, url, id, key) return except Exception as e: logger.exception(e) self.mainwindow.errorbox('Something went wrong:\n\n' + str(e)) else: self.mainwindow.errorbox('You need to select something before you can delete it.') return
48.340524
137
0.597966
31,120
0.991936
0
0
0
0
0
0
3,559
0.113441
ce5e17e8dbf5e904faf5468fbc530840fc418ada
1,201
py
Python
app.py
kecleveland/mhdn_app
27cbd3fcb6d831913481a7c0d51af6b3641d6cf3
[ "MIT" ]
null
null
null
app.py
kecleveland/mhdn_app
27cbd3fcb6d831913481a7c0d51af6b3641d6cf3
[ "MIT" ]
null
null
null
app.py
kecleveland/mhdn_app
27cbd3fcb6d831913481a7c0d51af6b3641d6cf3
[ "MIT" ]
null
null
null
from twarc import Twarc2, expansions from pathlib import Path import json import config import os import config appConfig = config.Config client = Twarc2(bearer_token=appConfig.bearer_token) file_path = Path(f"{appConfig.file_path}{appConfig.file_name}") def main(): # result params try: search_results = client.search_all( query=appConfig.query, start_time = appConfig.start_date, end_time = appConfig.end_date, max_results = appConfig.max_results ) except Exception as e: print(f"An exception ocurred: {e}") # write results try: for page in search_results: result = expansions.flatten(page) # print(json.dumps(tweet)) with open(file_path, "a+") as fh: for tweet in result: fh.write("%s\n" % json.dumps(tweet)) except IOError as e: print(f"Error: Issue writing to path {file_path}: {e}") else: print(f"Write to {file_path} successful.") if __name__ == "__main__": main()
31.605263
71
0.555371
0
0
0
0
0
0
0
0
233
0.194005
ce5f3e28692a3faeaa82556c686295cb266a77ee
300
py
Python
src/utils/regex_utils/regex_utils.py
BichengWang/python-notebook
83fae37432a2bf701566e85ab6d7e8e3d688a0ee
[ "MIT" ]
null
null
null
src/utils/regex_utils/regex_utils.py
BichengWang/python-notebook
83fae37432a2bf701566e85ab6d7e8e3d688a0ee
[ "MIT" ]
null
null
null
src/utils/regex_utils/regex_utils.py
BichengWang/python-notebook
83fae37432a2bf701566e85ab6d7e8e3d688a0ee
[ "MIT" ]
null
null
null
import re def find_indices(): return [m.start(0) for m in re.finditer(reg, content)] def find_content(): return re.findall(reg, content) if __name__ == "__main__": content = 'an example word:cat and word:dog' reg = r'word:\w' print(find_indices()) print(find_content())
17.647059
58
0.653333
0
0
0
0
0
0
0
0
54
0.18
ce60955aeef652ef027da2711317acb273b74ef6
5,944
py
Python
tests/test_trainer/test_pipeline/test_p2p.py
DevinCheung/ColossalAI
632e622de818697f9949e35117c0432d88f62c87
[ "Apache-2.0" ]
null
null
null
tests/test_trainer/test_pipeline/test_p2p.py
DevinCheung/ColossalAI
632e622de818697f9949e35117c0432d88f62c87
[ "Apache-2.0" ]
null
null
null
tests/test_trainer/test_pipeline/test_p2p.py
DevinCheung/ColossalAI
632e622de818697f9949e35117c0432d88f62c87
[ "Apache-2.0" ]
null
null
null
#!/usr/bin/env python # -*- encoding: utf-8 -*- import pytest import torch import torch.distributed as dist import torch.multiprocessing as mp from colossalai.communication import (recv_backward, recv_forward, recv_tensor_meta, send_backward, send_backward_recv_forward, send_forward, send_forward_recv_backward, send_tensor_meta) from colossalai.context.parallel_mode import ParallelMode from colossalai.core import global_context as gpc from colossalai.initialize import launch from colossalai.logging import get_dist_logger from colossalai.utils import get_current_device from functools import partial BATCH_SIZE = 16 SEQ_LENGTH = 64 HIDDEN_SIZE = 128 CONFIG = dict( parallel=dict( pipeline=dict(size=4), tensor=dict(size=1, mode=None) ), seed=1024 ) def check_equal(A, B): return torch.allclose(A, B, rtol=1e-5, atol=1e-3) def check_forward(output_tensor, rank, logger): dist.barrier() if gpc.is_first_rank(ParallelMode.PIPELINE): tensor = output_tensor.clone() else: tensor = recv_forward(output_tensor.shape) logger.info('Rank {} received forward. Correct tensor: {}'.format( rank, check_equal(tensor, output_tensor))) if not gpc.is_last_rank(ParallelMode.PIPELINE): send_forward(tensor) logger.info('Rank {} sent forward.'.format(rank)) def check_backward(output_grad, rank, logger): dist.barrier() if gpc.is_last_rank(ParallelMode.PIPELINE): grad = output_grad.clone() else: grad = recv_backward(output_grad.shape) logger.info('Rank {} received backward. Correct grad: {}'.format( rank, check_equal(grad, output_grad))) if not gpc.is_first_rank(ParallelMode.PIPELINE): send_backward(grad) logger.info('Rank {} sent backward.'.format(rank)) def check_forward_backward(output_tensor, output_grad, rank, logger): dist.barrier() if not gpc.is_first_rank(ParallelMode.PIPELINE): tensor = send_backward_recv_forward(output_grad, output_tensor.shape) logger.info( 'Rank {} sent backward received forward. Correct tensor: {}'. format(rank, check_equal(tensor, output_tensor))) if not gpc.is_last_rank(ParallelMode.PIPELINE): grad = send_forward_recv_backward(output_tensor, output_grad.shape) logger.info( 'Rank {} sent forward received backward. Correct grad: {}'.format( rank, check_equal(grad, output_grad))) def check_op(size, rank, prev_rank, next_rank, up_group, down_group, logger): dtype = torch.float32 device = get_current_device() tensor_shape = (BATCH_SIZE, SEQ_LENGTH, HIDDEN_SIZE) # recv_tensor_shape = (BATCH_SIZE, SEQ_LENGTH, HIDDEN_SIZE) grad_shape = (BATCH_SIZE, SEQ_LENGTH, HIDDEN_SIZE) tensor = torch.randn(tensor_shape, dtype=dtype, device=device) dist.all_reduce(tensor) grad = torch.randn(grad_shape, dtype=dtype, device=device) dist.all_reduce(grad) if rank % 2 == 0: need_meta = True need_meta = send_tensor_meta(tensor, need_meta) logger.info('Rank {} shape sent (need meta: {}).'.format( rank, need_meta)) req = dist.broadcast(tensor, src=rank, group=down_group, async_op=True) req.wait() out = tensor.clone() logger.info('Rank {} test op: tensor sent.'.format(rank)) else: recv_tensor_shape = recv_tensor_meta(None) logger.info('Rank {} shape received. Correct shape: {}'.format( rank, tensor_shape == recv_tensor_shape)) out = torch.empty(recv_tensor_shape, dtype=dtype, device=device) req = dist.broadcast(out, src=prev_rank, group=up_group, async_op=True) req.wait() logger.info('Rank {} test op: received tensor ({})'.format( rank, out.shape)) logger.info('Rank {} test op. Correct tensor: {}'.format( rank, check_equal(tensor, out))) def check_comm(size, rank, prev_rank, next_rank, up_group, down_group, logger): dtype = torch.float32 device = get_current_device() tensor_shape = (BATCH_SIZE, SEQ_LENGTH, HIDDEN_SIZE) grad_shape = (BATCH_SIZE, SEQ_LENGTH, HIDDEN_SIZE) tensor = torch.randn(tensor_shape, dtype=dtype, device=device) dist.all_reduce(tensor) grad = torch.randn(grad_shape, dtype=dtype, device=device) dist.all_reduce(grad) check_op(size, rank, prev_rank, next_rank, up_group, down_group, logger) check_forward(tensor, rank, logger) check_backward(grad, rank, logger) check_forward_backward(tensor, grad, rank, logger) def run_check(rank, world_size): launch( config=CONFIG, rank=rank, world_size=world_size, host='localhost', port=29932, backend='nccl' ) logger = get_dist_logger() rank = gpc.get_global_rank() prev_rank = gpc.get_prev_global_rank(ParallelMode.PIPELINE) up_ranks = gpc.get_ranks_in_group(ParallelMode.PIPELINE_PREV) up_group = gpc.get_group(ParallelMode.PIPELINE_PREV) next_rank = gpc.get_next_global_rank(ParallelMode.PIPELINE) down_ranks = gpc.get_ranks_in_group(ParallelMode.PIPELINE_NEXT) down_group = gpc.get_group(ParallelMode.PIPELINE_NEXT) logger.info( 'Rank {0}: prev rank {1} (up: {2}), next rank {3} (down: {4})'.format( rank, prev_rank, up_ranks, next_rank, down_ranks)) logger.info('Distributed environment is initialzied.') check_comm(world_size, rank, prev_rank, next_rank, up_group, down_group, logger) gpc.destroy() torch.cuda.empty_cache() @pytest.mark.dist def test_p2p(): world_size = 4 run_func = partial(run_check, world_size=world_size) mp.spawn(run_func, nprocs=world_size) if __name__ == '__main__': test_p2p()
36.466258
79
0.675135
0
0
0
0
151
0.025404
0
0
678
0.114065
ce60e885998a6e65935f35c9104bd85ccefe442c
23,622
py
Python
mine.py
appenz/minebot
e1bd18053873c4d686de57e014a2cd8f27d4dd4c
[ "Apache-2.0" ]
11
2021-08-28T18:21:43.000Z
2022-03-08T16:08:55.000Z
mine.py
appenz/minebot
e1bd18053873c4d686de57e014a2cd8f27d4dd4c
[ "Apache-2.0" ]
3
2022-02-05T17:47:53.000Z
2022-03-10T17:36:48.000Z
mine.py
appenz/minebot
e1bd18053873c4d686de57e014a2cd8f27d4dd4c
[ "Apache-2.0" ]
5
2022-02-04T19:12:50.000Z
2022-03-18T20:54:00.000Z
# # Functions for mining blocks # import itertools from javascript import require Vec3 = require('vec3').Vec3 from botlib import * from inventory import * from workarea import * class MineBot: needs_iron_pickaxe = ["Gold Ore", "Redstone Ore", "Diamond Ore", "Emerald Ore"] needs_diamond_pickaxe = ["Obsidian"] needs_shovel = ["Dirt", "Gravel", "Sand"] # This is what we are looking for valuable_blocks = [ "Coal Ore", "Copper Ore", "Lapis Lazuli Ore", "Iron Ore", "Gold Ore", "Redstone Ore", "Diamond Ore", "Emerald Ore", "Block of Coal", ] # These blocks never get mined up ignored_blocks = [ "Torch", "Wall Torch", "Sign", "Air", "Cave Air", "Void Air", "Chest", "Crafting Table", "Furnace", "Ladder", "Glass", "Stone Bricks", "Chiseled Stone Bricks", "Stone Brick Stairs", "Water", "Flowing Water", "Bubble Column", ] # These blocks we need to bridge over dangerBlocks = { "Air", "Cave Air", "Void Air", "Lava", "Water", "Infested Stone", } # These blocks will drop down, we need to dig them up until done block_will_drop = [ "Gravel", "Sand", "Red Sand", "Pointed Dropstone", "Anvil," ] block_will_flow = [ "Lava", "Water", ] # Blocks that will drop down on you dangerDropBlocks = block_will_flow + block_will_drop # These blocks we use to fill holes or build bridges fillBlocks = { "Stone Bricks", "Cobblestone", "Dirt" } # Inventory goals for normal mining miningEquipList= { "Bread":5, "Stone Pickaxe":5, "Stone Shovel":2, "Iron Pickaxe":2, "Torch": 10, "Cobblestone" : 64, "Stone Bricks" : 256, "Dirt" : 0, "Andesite" : 0, "Diorite" : 0, "Granite" : 0, "Sandstone" : 0, "Sand" : 0, "Gravel" : 0, "Flint" : 0, "Iron Ore" : 0, "Gold Ore" : 0, "Copper Ore" : 0, "Coal" : 0, "Redstone Dust" : 0, "Diamond" : 0, "Lapis Lazuli" : 0, "Emerald" : 0, "Chiseled Stone Bricks" : 0, "Block of Coal" : 0, } miningMinimumList = { "Bread":1, "Stone Pickaxe":2, "Stone Shovel":1, "Iron Pickaxe":1, "Torch": 5, } def __init__(self): print('mining ', end='') # Checks if walking to a specific block is considered safe def mining_safety_check(self,position): n = self.bot.blockAt(position).displayName if n in self.block_will_flow: self.stopActivity = True self.dangerType = "danger: "+n self.pdebug(f'danger: {n}, aborting mining',1) return False return True # # Mine a block with the right tool # Takes a Vector or x,y,z as input def mineBlock(self,x,y=None,z=None): if not y and not z: v = x else: v = Vec3(x,y,z) b = self.bot.blockAt(v) if b.digTime(274) > 100 and b.displayName not in self.ignored_blocks: # Ok, this looks mineable # Try 20 times, in case gravel is dropping down for attempts in range(0,20): if not self.mining_safety_check(self.bot.entity.position): return 0 self.pdebug(f' mine ({v.x},{v.y},{v.z}) {b.displayName} t:{b.digTime(274)}',3) # Check for the right tool if b.displayName in self.needs_shovel: if self.invItemCount("Stone Shovel") > 0: self.wieldItem("Stone Shovel") else: self.wieldItem("Stone Pickaxe") elif b.displayName in self.needs_iron_pickaxe: self.wieldItem("Iron Pickaxe") else: self.wieldItem("Stone Pickaxe") # dig out the block try: self.bot.dig(b) except Exception as e: self.pexception(f'dig failed for block {v.x},{v.y},{v.z}) {b.displayName} t:{b.digTime(274)}',e) # Check if successful b = self.bot.blockAt(v) if b.digTime(274) == 0: return 1 else: #print(f' ignore ({x},{y},{z}) {b.displayName}') return 0 # # Mine a 1-wide path from start to end of height height # Assumes the bot is at start # def minePath(self,start,end,height, area=None): #print(start,end,height) c = Vec3(start.x, start.y, start.z) d = Vec3( (end.x-start.x)/max(abs(end.x-start.x),1), 0, (end.z-start.z)/max(abs(end.z-start.z),1) ) while True: # Check if path is safe bb = self.bot.blockAt(c.x,c.y-1,c.z) if bb.displayName in self.dangerBlocks: self.perror(f' stopping, dangerous block {bb.displayName} ') return False # Mine the column. May need a few tries due to gravel wait_t = 0 for tries in range(0,30): if self.stopActivity: return True # check if we have gravel or sand. If yes we need to check longer. for h in range(0,height+1): b_name = self.bot.blockAt(Vec3(c.x,c.y+h,c.z)).displayName if b_name in self.block_will_drop: wait_t = 1 break # mine for h in range(0,height): cc = self.mineBlock( c.x,c.y+h,c.z) if area: area.blocks_mined += cc if wait_t: time.sleep(wait_t) for h in range(0,height): b_name = self.bot.blockAt(Vec3(c.x,c.y+h,c.z)).displayName if b_name not in self.ignored_blocks: self.pdebug(f' block not cleared: {b_name}.',2) break else: break if tries > 30: self.perror("can't clear this column.") self.stopActivity = True return False if not self.mining_safety_check(c): return 0 self.safeWalk(c,0.3) # Check if we are done if c.x == end.x and c.z == end.z: return True if c.x != end.x: c.x += d.x if c.z != end.z: c.z += d.z # # Mine a rectangle of dx times dz, height h around a chest # def roomMine(self,dx_max,dz_max, height): dx_max = int( (checkIntArg(dx_max, 3, 99)-1)/2) dz_max = int( (checkIntArg(dz_max, 3, 99)-1)/2) height = checkIntArg(height, 2, 8) if not dx_max or not dz_max or not height: self.chat('Try: mine room <dx 3-99> <dz 3-99> <height 2-8>') return False area = workArea(self,1,1,1,notorch=True) if not area.valid: return False start = area.start self.refreshActivity([f'Room mining started']) self.pdebug(f'Mining out area of {2*dx_max+1} x {2*dz_max+1} x {height} blocks.',2) for dz in range(0,dz_max+1): todo = False for dx in range(-dx_max,dx_max+1): for h in range(0,height): if self.bot.blockAt(Vec3(start.x+dx, start.y+h, start.z+dz)).displayName not in self.ignored_blocks: todo = True break if self.bot.blockAt(Vec3(start.x+dx, start.y+h, start.z-dz)).displayName not in self.ignored_blocks: todo = True break if todo: break if not todo: continue self.refreshActivity("Restocking.") area.restock(self.miningEquipList) time.sleep(1) if not self.checkMinimumList(self.miningMinimumList): return False if not self.stopActivity: # Carve initial column self.refreshActivity( [ f'Blocks Mined: {area.blocks_mined}', f'Row: {dz}' ] ) row_c = Vec3(start.x,start.y,start.z+dz) self.pdebug(f'walking to {row_c.x} {row_c.y} {row_c.z}',3) self.walkTo(row_c) self.minePath(row_c,Vec3(row_c.x-dx_max,row_c.y,row_c.z),height, area=area) self.walkTo(row_c) self.minePath(row_c,Vec3(row_c.x+dx_max,row_c.y,row_c.z),height, area=area) if not self.stopActivity: self.refreshActivity( [ f'Blocks Mined: {area.blocks_mined}', f'Row: {dz}' ] ) row_c = Vec3(start.x,start.y,start.z-dz) self.walkTo(row_c) self.minePath(row_c,Vec3(row_c.x-dx_max,row_c.y,row_c.z),height, area=area) self.walkTo(row_c) self.minePath(row_c,Vec3(row_c.x+dx_max,row_c.y,row_c.z),height, area=area) if self.stopActivity: break # Mining ended area.restock(self.miningEquipList) return True # # Find Valuables in a side corridor # Returns max_x, max_y of the furthest def findValuables(self,area, max_x, max_y, z, min_y=0): if max_x < 0: r = range(0,max_x-1,-1) elif max_x > 0: r = range(0,max_x+1,1) else: return 0,0 bx = 0 by = 0 name = None for x in r: for y in range(min_y,max_y): n = area.blockAt(x,y,z).displayName if n in self.valuable_blocks: bx = x by = max(y,by) name = n if name: self.pdebug(f' @{z:3} found {name} {bx}/{by}',2) area.valuables = name return bx, by def bridgeIfNeeded(self, area, x, z): if area.blockAt(x,-1,z).displayName in self.dangerBlocks: v_place = area.toWorld(x,-1,z-1) # Try three times. for ii in range (0,3): self.wieldItemFromList(self.fillBlocks) self.bridgeBlock(v_place,area.forwardVector) if area.blockAt(x,-1,z).displayName not in self.dangerBlocks: break else: self.perror(f'*** fatal error. Cant bridge dangerous block {area.blockAt(x,-1,z).displayName}') area.status = "blocked/drop" self.stopActivity = True return False area.blocks_mined += 1 return True # # Mine up a column of a specific height # Bridge afterwards if needed. # def mineColumn(self, area, x, z, height): if self.stopActivity: return False self.pdebug(f'mineColumn(x:{x},z:{z},height:{height})',5) # Check if we need to do anything for y in range(0,height): if area.blockAt(x,y,z).displayName not in self.ignored_blocks: break else: return True # Check for infested for y in range(0,height): if area.blockAt(x,y,z).displayName == "Infested Stone": self.pdebug(f' located {area.blockAt(x,y,z).displayName}, aborting!',1) area.status = "* Silverfish *" self.stopActivity = True return False # Try to mine the column. May take multiple attempts due to gravel. for tries in range(0,10): done = True for y in range(0,height): if self.stopActivity: return True if area.blockAt(x,y,z).displayName not in self.ignored_blocks: # We need to clear this done = False if area.blockAt(x,y+1,z).displayName in self.block_will_drop: self.mineBlock( area.toWorld(x,y,z) ) time.sleep(1) else: self.mineBlock( area.toWorld(x,y,z) ) area.blocks_mined += 1 if done: break for y in range(0,height): if area.blockAt(x,y,z).displayName not in self.ignored_blocks: self.perror(f'aborting - block not cleared: {area.blockAt(x,y,z).displayName}.') area.status = "blocked" self.stopActivity = True return False return True # # Check for goodies in the floor. Works best to about 2 deep. # def floorMine(self, area, x, z, depth): if self.stopActivity: return False if depth > 0: max_d = 0 for d in range(1,depth+1): if area.blockAt(x,-d,z).displayName in self.valuable_blocks: max_d = d if max_d > 0: # Ok, we found something for d in range(1,max_d+1): self.mineBlock( area.toWorld(x,-d,z) ) # Now fill it up, which gets us the blocks. Best effort. for d in range(max_d, 0, -1): v_place = area.toWorld(x,-d-1,z) self.wieldItemFromList(self.fillBlocks) self.safePlaceBlock(v_place,Vec3(0,1,0)) time.sleep(0.2) return True def ceilingMine(self, area, x, z, max_height): if self.stopActivity: return False # Check the ceiling up to max reachable height (7 above) max_y = 0 for y in range(2,max_height): if area.blockAt(x,y,z).displayName in self.valuable_blocks: max_y = y if max_y > 0: # Ok, we found something for y in range(2,max_y+1): if area.blockAt(x,y,z).displayName not in self.ignored_blocks: if area.blockAt(x,y+1,z).displayName in self.dangerDropBlocks: self.pdebug(f' cant mine ceiling, {area.blockAt(x,y+1,z).displayName} above.',2) return False self.mineBlock( area.toWorld(x,y,z) ) # # Mine up a row. Unlike minePath, this works in an area # def mineRow(self, area, max_x, height, z, floor_mine=0, ceiling_mine=0): #print(f'mineRow(max_x:{max_x},height:{height},z:{z},floor_mine:{floor_mine},ceiling_mine:{ceiling_mine})') if max_x == 0: return False elif max_x < 0: dx = -1 elif max_x > 0: dx = 1 r = range(dx*area.width2+dx, max_x+dx,dx) area.walkToBlock3(dx*area.width2,0,z) height = max(2,height) for x in r: if self.stopActivity : break if not self.mineColumn(area, x, z, height): return False # Check floors if floor_mine > 0: self.floorMine(area, x, z, floor_mine) if ceiling_mine > 0: self.ceilingMine(area, x, z, ceiling_mine) # Bridge if needed if area.blockAt(x,-1,z).displayName in self.dangerBlocks: v_place = area.toWorld(x-dx,-1,z) # Try three times. self.wieldItemFromList(self.fillBlocks) self.bridgeBlock(v_place,area.dirToWorldV3(Vec3(dx,0,0))) if area.blockAt(x,-1,z).displayName in self.dangerBlocks: self.pdebug(f' cant reach, bridging failed {area.blockAt(x,-1,z).displayName}.',2) area.walkToBlock3(0,0,z) return False if not self.mining_safety_check(area.toWorld(x,0,z)): return False area.walkToBlock3(x,0,z) time.sleep(0.5) return True # Helper function for UI def mineActivity(self,area,z,txt1="",txt2=""): l = [ f'Depth: {z} ⏱️ {int(100*(area.blocks_mined-area.last_break)/area.break_interval)}%', f'Status: {area.status}', txt1, txt2 ] self.refreshActivity(l) # # Build a strip mine of a specific height and width and light it up # def stripMine(self,width=3,height=3,valrange=3): z_torch = 0 z =0 area = workArea(self,width,height,99999) if not area.valid: return False self.speedMode = True # explore fast until we find something self.refreshActivity([f'Mining started']) while not self.stopActivity: # Get ready self.mineActivity(area,z,"Restocking.") area.restock(self.miningEquipList) area.last_break = area.blocks_mined time.sleep(1) if not self.checkMinimumList(self.miningMinimumList): return False self.mineActivity(area,z,"Walking back to work") while area.blocks_mined-area.last_break < area.break_interval and not self.stopActivity: if not self.mining_safety_check(area.toWorld(0,0,z)): break area.walkToBlock3(0,0,z-1) if area.blocks_mined > 0: self.speedMode = False # Step 0 - Check if we are still good if not self.checkMinimumList(self.miningMinimumList): self.perror("Aborting, out of required equipment.") self.stopActivity = True area.status = "out of tools" break # Step 1 - Mine the main tunnel self.mineActivity(area,z,"Walking back to work", f'Mining main tunnel') for x in area.xRange(): self.mineColumn(area, x, z, height) self.floorMine(area, x, z, 2) self.ceilingMine(area, x, z, height+2) # Step 2 - Bridge if needed for x in area.xRange(): self.bridgeIfNeeded(area, x, z) if self.stopActivity: break area.walkToBlock3(0,0,z) # Step 3 - Look for Valuables Left and Right bx, by = self.findValuables(area, -area.width2-valrange, height+2, z, min_y=-2) by = min(by,height-1) if bx != 0: self.mineActivity(area, z, f'Found: {area.valuables}✨', f'⬅️ Left side {bx}/{by+1}') self.mineRow(area, bx, by+1, z, floor_mine=2, ceiling_mine=height+2) area.walkToBlock3(0,0,z) bx, by = self.findValuables(area, area.width2+valrange, height+2, z, min_y=-2) by = min(by,height-1) if bx != 0: self.mineActivity(area, z, f'Found: {area.valuables}✨', f'➡️ Right side {bx}/{by+1}') self.mineRow(area, bx, by+1, z, floor_mine=2, ceiling_mine=height+2) area.walkToBlock3(0,0,z) # Step 4 - Light up if needed and move forward by one. if z > z_torch: self.mineActivity(area, z, f'Placing Torch') # try to place a torch torch_v = area.toWorld(area.width2,1,z) wall_v = area.toWorld(area.width2+1,1,z) dv = subVec3(torch_v, wall_v) if self.bot.blockAt(wall_v).displayName not in self.ignored_blocks: if self.bot.blockAt(torch_v).displayName != "Wall Torch": self.pdebug(" placing torch.",2) self.wieldItem("Torch") self.safePlaceBlock(wall_v,dv) z_torch += 6 z += 1 # ...and back to the chest to update sign and restock self.speedMode = False self.mineActivity(area, z, f'Walking to Chest') area.walkToStart() if self.dangerType: s = self.dangerType else: s = area.status self.mineActivity(area, z, f'Updating Sign') txt = [f'Mine {area.directionStr()} {width}x{height}', f'Length: {z}', myDate(), s ] self.updateSign(txt,tryonly=True) # Mining ended area.restock(self.miningEquipList) # # Mine a vertical shaft of N x N down to depth D # def shaftMine(self,r,min_y): # Determine center start_chest = self.findClosestBlock("Chest",xz_radius=3,y_radius=1) if not start_chest: self.perror("Can't find starting position. Place chest wiht materials to place the center.") return self.pdebug(f'Mining out area of {2*r+1} x {2*r+1} down to depth z={min_y}.',1) start = start_chest.position self.restockFromChest(self.miningEquipList) self.eatFood() for y in range(start.y-1,min_y,-1): self.pdebug(f' layer: {y}',2) for dz in range(0,r+1): if not self.stopActivity: row_c = Vec3(start.x,y,start.z+dz) self.safeWalk(row_c,1) self.minePath(row_c,Vec3(row_c.x-r,row_c.y,row_c.z),2) self.safeWalk(row_c,1) self.minePath(row_c,Vec3(row_c.x+r,row_c.y,row_c.z),2) if not self.stopActivity: row_c = Vec3(start.x,y,start.z-dz) self.safeWalk(row_c,1) self.minePath(row_c,Vec3(row_c.x-r,row_c.y,row_c.z),2) self.safeWalk(row_c,1) self.minePath(row_c,Vec3(row_c.x+r,row_c.y,row_c.z),2) if self.stopActivity: break self.pdebug(f'Shaft mining ended at y={y}.',2) return True def doMining(self,args): if len(args) == 0: self.chat('Need to specify type of mine. Try "fast", "3x3" or "5x5".') else: mtype = args[0] if mtype == '3x3': self.activity_name = "Mine 3x3" self.stripMine(3,3,5) elif mtype == 'tunnel3x3': self.activity_name = "Tunnel 3x3" self.stripMine(3,3,0) elif mtype == '5x5': self.activity_name = "Mine 5x5" self.stripMine(5,5,5) elif mtype == 'tunnel5x5': self.activity_name = "Tunnel 5x5" self.stripMine(5,5,0) elif mtype == 'branch' or mtype == 'fast': self.activity_name = "Branchmine" self.stripMine(1,5,5) elif mtype == 'room': if len(args) < 4: self.chat('Try: mine room <length> <width> <height>') else: self.activity_name = f'Mine {args[1]}x{args[2]}x{args[3]}' self.roomMine(args[1],args[2],args[3]) self.endActivity() time.sleep(1) return True
33.649573
120
0.502117
23,452
0.992131
0
0
0
0
0
0
5,242
0.221762
ce617e3015fa7ae63ff96b316d5d14af95c4007f
16,667
py
Python
src/the_tale/the_tale/accounts/tests/test_account_prototype.py
devapromix/the-tale
2a10efd3270734f8cf482b4cfbc5353ef8f0494c
[ "BSD-3-Clause" ]
1
2020-04-02T11:51:20.000Z
2020-04-02T11:51:20.000Z
src/the_tale/the_tale/accounts/tests/test_account_prototype.py
devapromix/the-tale
2a10efd3270734f8cf482b4cfbc5353ef8f0494c
[ "BSD-3-Clause" ]
null
null
null
src/the_tale/the_tale/accounts/tests/test_account_prototype.py
devapromix/the-tale
2a10efd3270734f8cf482b4cfbc5353ef8f0494c
[ "BSD-3-Clause" ]
null
null
null
import smart_imports smart_imports.all() class AccountPrototypeTests(utils_testcase.TestCase, personal_messages_helpers.Mixin): def setUp(self): super(AccountPrototypeTests, self).setUp() self.place_1, self.place_2, self.place_3 = game_logic.create_test_map() self.account = self.accounts_factory.create_account() self.fast_account = self.accounts_factory.create_account(is_fast=True) personal_messages_tt_services.personal_messages.cmd_debug_clear_service() def test_create(self): self.assertTrue(self.account.active_end_at > datetime.datetime.now() + datetime.timedelta(seconds=conf.settings.ACTIVE_STATE_TIMEOUT - 60)) def test_get_achievement_type_value(self): for achievement_type in achievements_relations.ACHIEVEMENT_TYPE.records: if achievement_type.source.is_GAME_OBJECT: continue if achievement_type.source.is_NONE: continue self.account.get_achievement_type_value(achievement_type) @mock.patch('the_tale.accounts.conf.settings.ACTIVE_STATE_REFRESH_PERIOD', 0) def test_update_active_state__expired(self): self.assertTrue(self.account.is_update_active_state_needed) with mock.patch('the_tale.game.workers.supervisor.Worker.cmd_update_hero_with_account_data') as cmd_mark_hero_as_active: self.account.update_active_state() self.assertEqual(cmd_mark_hero_as_active.call_count, 1) def test_update_active_state__not_expired(self): self.assertFalse(self.account.is_update_active_state_needed) def test_change_credentials(self): self.assertTrue(prototypes.AccountPrototype.get_by_id(self.fast_account.id).is_fast) with mock.patch('the_tale.game.workers.supervisor.Worker.cmd_update_hero_with_account_data') as fake_cmd: with mock.patch('the_tale.accounts.workers.accounts_manager.Worker.cmd_run_account_method') as cmd_run_account_method: self.fast_account.change_credentials(new_email='fast_user@test.ru', new_password=django_auth_hashers.make_password('222222'), new_nick='test_nick') self.assertEqual(cmd_run_account_method.call_count, 0) self.assertEqual(django_auth.authenticate(nick='test_nick', password='222222').id, self.fast_account.id) self.assertFalse(prototypes.AccountPrototype.get_by_id(self.fast_account.id).is_fast) self.assertEqual(fake_cmd.call_count, 1) self.assertFalse(fake_cmd.call_args[1]['is_fast']) def test_change_credentials__with_referral(self): self.fast_account._model.referral_of = self.account._model self.fast_account.save() self.assertTrue(prototypes.AccountPrototype.get_by_id(self.fast_account.id).is_fast) with mock.patch('the_tale.game.workers.supervisor.Worker.cmd_update_hero_with_account_data') as fake_cmd: with mock.patch('the_tale.accounts.workers.accounts_manager.Worker.cmd_run_account_method') as cmd_run_account_method: self.fast_account.change_credentials(new_email='fast_user@test.ru', new_password=django_auth_hashers.make_password('222222'), new_nick='test_nick') self.assertEqual(cmd_run_account_method.call_count, 1) self.assertEqual(cmd_run_account_method.call_args, mock.call(account_id=self.account.id, method_name=prototypes.AccountPrototype.update_referrals_number.__name__, data={})) self.assertEqual(django_auth.authenticate(nick='test_nick', password='222222').id, self.fast_account.id) self.assertFalse(prototypes.AccountPrototype.get_by_id(self.fast_account.id).is_fast) self.assertEqual(fake_cmd.call_count, 1) self.assertFalse(fake_cmd.call_args[1]['is_fast']) def test_change_credentials_password(self): nick = self.account.nick email = self.account.email self.account.change_credentials(new_password=django_auth_hashers.make_password('222222')) self.assertEqual(post_service_models.Message.objects.all().count(), 0) self.assertEqual(self.account.email, email) user = django_auth.authenticate(nick=nick, password='222222') self.assertEqual(user.id, self.account.id) def test_change_credentials_nick(self): with mock.patch('the_tale.game.workers.supervisor.Worker.cmd_update_hero_with_account_data') as fake_cmd: self.account.change_credentials(new_nick='test_nick') self.assertEqual(post_service_models.Message.objects.all().count(), 0) self.assertEqual(fake_cmd.call_count, 0) self.assertEqual(django_auth.authenticate(nick='test_nick', password='111111').id, self.account.id) def test_change_credentials_email(self): nick = self.account.nick self.account.change_credentials(new_email='test_user@test.ru') self.assertEqual(post_service_models.Message.objects.all().count(), 0) self.assertEqual(self.account.email, 'test_user@test.ru') self.assertEqual(django_auth.authenticate(nick=nick, password='111111').id, self.account.id) self.assertEqual(django_auth.authenticate(nick=nick, password='111111').nick, nick) def test_prolong_premium__for_new_premium(self): self.account._model.premium_end_at = datetime.datetime.now() - datetime.timedelta(days=100) self.account.prolong_premium(days=30) self.assertTrue(datetime.datetime.now() + datetime.timedelta(days=29) < self.account.premium_end_at) self.assertTrue(datetime.datetime.now() + datetime.timedelta(days=31) > self.account.premium_end_at) def test_prolong_premium__for_existed_premium(self): self.account._model.premium_end_at = datetime.datetime.now() + datetime.timedelta(days=100) self.account.prolong_premium(days=30) self.assertTrue(datetime.datetime.now() + datetime.timedelta(days=129) < self.account.premium_end_at) self.assertTrue(datetime.datetime.now() + datetime.timedelta(days=131) > self.account.premium_end_at) def test_is_premium(self): self.assertFalse(self.account.is_premium) self.account.prolong_premium(days=1) self.assertTrue(self.account.is_premium) def test_notify_about_premium_expiration(self): with self.check_new_message(self.account.id, [logic.get_system_user_id()]): self.account.notify_about_premium_expiration() def test_send_premium_expired_notifications(self): with self.check_new_message(self.account.id, [logic.get_system_user_id()]): account_1 = self.account account_2 = self.accounts_factory.create_account() account_3 = self.accounts_factory.create_account() account_4 = self.accounts_factory.create_account() account_1.prolong_premium(conf.settings.PREMIUM_EXPIRED_NOTIFICATION_IN.days - 1) account_1.save() account_3.prolong_premium(conf.settings.PREMIUM_EXPIRED_NOTIFICATION_IN.days - 1) account_3.save() account_4.prolong_premium(conf.settings.PREMIUM_EXPIRED_NOTIFICATION_IN.days + 1) account_4.save() zero_time = datetime.datetime.fromtimestamp(0) self.assertEqual(account_1._model.premium_expired_notification_send_at, zero_time) self.assertEqual(account_2._model.premium_expired_notification_send_at, zero_time) self.assertEqual(account_3._model.premium_expired_notification_send_at, zero_time) self.assertEqual(account_4._model.premium_expired_notification_send_at, zero_time) prototypes.AccountPrototype.send_premium_expired_notifications() account_1.reload() account_2.reload() account_3.reload() account_4.reload() self.assertNotEqual(account_1._model.premium_expired_notification_send_at, zero_time) self.assertEqual(account_2._model.premium_expired_notification_send_at, zero_time) self.assertNotEqual(account_3._model.premium_expired_notification_send_at, zero_time) self.assertEqual(account_4._model.premium_expired_notification_send_at, zero_time) current_time = datetime.datetime.now() self.assertTrue(current_time - datetime.timedelta(seconds=60) < account_1._model.premium_expired_notification_send_at < current_time) self.assertTrue(current_time - datetime.timedelta(seconds=60) < account_3._model.premium_expired_notification_send_at < current_time) def test_ban_game(self): self.assertFalse(self.account.is_ban_game) with mock.patch('the_tale.game.workers.supervisor.Worker.cmd_update_hero_with_account_data') as cmd_update_hero_with_account_data: self.account.ban_game(days=1) self.assertEqual(cmd_update_hero_with_account_data.call_count, 1) self.assertEqual(cmd_update_hero_with_account_data.call_args[1]['ban_end_at'], self.account.ban_game_end_at) self.assertTrue(self.account.is_ban_game) self.account._model.ban_game_end_at = datetime.datetime.now() self.assertFalse(self.account.is_ban_game) def test_ban_forum(self): self.assertFalse(self.account.is_ban_forum) self.account.ban_forum(days=1) self.assertTrue(self.account.is_ban_forum) self.account._model.ban_forum_end_at = datetime.datetime.now() self.assertFalse(self.account.is_ban_forum) def test_bank_account__not_created(self): bank_account = self.account.bank_account self.assertTrue(bank_account.is_fake) self.assertEqual(bank_account.amount, 0) def test_update_referrals(self): account_2 = self.accounts_factory.create_account(referral_of_id=self.account.id) account_3 = self.accounts_factory.create_account(referral_of_id=self.account.id, is_fast=True) self.account.update_referrals_number() self.fast_account.update_referrals_number() self.assertEqual(self.account.referrals_number, 1) self.assertEqual(self.fast_account.referrals_number, 0) def test_referral_removing(self): account_2 = self.accounts_factory.create_account(referral_of_id=self.account.id, is_fast=True) self.account.remove() # child account must not be removed self.assertEqual(prototypes.AccountPrototype.get_by_id(account_2.id).referral_of_id, None) def test_set_might(self): with mock.patch('the_tale.accounts.achievements.storage.AchievementsStorage.verify_achievements') as verify_achievements: self.account.set_might(666) self.assertEqual(verify_achievements.call_args_list, [mock.call(account_id=self.account.id, type=achievements_relations.ACHIEVEMENT_TYPE.KEEPER_MIGHT, old_value=0, new_value=666)]) @mock.patch('the_tale.game.bills.conf.settings.MIN_VOTES_PERCENT', 0.6) @mock.patch('the_tale.game.bills.prototypes.BillPrototype.time_before_voting_end', datetime.timedelta(seconds=0)) # fixt segmentation fault when testing with sqlite def test_1_update_actual_bills(self): forum_category = forum_models.Category.objects.create(caption='category-1', slug='category-1') forum_models.SubCategory.objects.create(caption=bills_conf.settings.FORUM_CATEGORY_UID + '-caption', uid=bills_conf.settings.FORUM_CATEGORY_UID, category=forum_category) self.account.update_actual_bills() self.assertEqual(self.account.actual_bills, []) bill_data = bills_bills.place_change_modifier.PlaceModifier(place_id=self.place_1.id, modifier_id=places_modifiers.CITY_MODIFIERS.TRADE_CENTER, modifier_name=places_modifiers.CITY_MODIFIERS.TRADE_CENTER.text, old_modifier_name=None) bill = bills_prototypes.BillPrototype.create(self.account, 'bill-1-caption', bill_data, chronicle_on_accepted='chronicle-on-accepted') self.account.update_actual_bills() self.assertEqual(self.account.actual_bills, []) data = bill.user_form_initials data['approved'] = True form = bills_bills.place_change_modifier.PlaceModifier.ModeratorForm(data) self.assertTrue(form.is_valid()) bill.update_by_moderator(form) bill.apply() self.account.update_actual_bills() self.assertEqual(self.account.actual_bills, [time.mktime(bill.voting_end_at.timetuple())]) class AccountPrototypeBanTests(utils_testcase.TestCase): def setUp(self): super(AccountPrototypeBanTests, self).setUp() game_logic.create_test_map() self.account = self.accounts_factory.create_account() def test_ban_game(self): with mock.patch('the_tale.accounts.prototypes.AccountPrototype.cmd_update_hero') as cmd_update_hero: self.account.ban_game(2) self.assertEqual(cmd_update_hero.call_count, 1) self.account.reload() self.assertTrue(self.account.is_ban_game) self.assertTrue(self.account.is_ban_any) self.assertFalse(self.account.is_ban_forum) self.assertTrue(datetime.timedelta(days=1) < self.account._model.ban_game_end_at - datetime.datetime.now() < datetime.timedelta(days=3)) def test_ban_game__extend(self): self.account.ban_game(3) with mock.patch('the_tale.accounts.prototypes.AccountPrototype.cmd_update_hero') as cmd_update_hero: self.account.ban_game(2) self.assertEqual(cmd_update_hero.call_count, 1) self.account.reload() self.assertTrue(self.account.is_ban_game) self.assertTrue(self.account.is_ban_any) self.assertFalse(self.account.is_ban_forum) self.assertTrue(datetime.timedelta(days=4) < self.account._model.ban_game_end_at - datetime.datetime.now() < datetime.timedelta(days=6)) def test_ban_game__reset(self): self.account.ban_game(2) with mock.patch('the_tale.accounts.prototypes.AccountPrototype.cmd_update_hero') as cmd_update_hero: self.account.reset_ban_game() self.assertEqual(cmd_update_hero.call_count, 1) self.account.reload() self.assertFalse(self.account.is_ban_game) self.assertFalse(self.account.is_ban_any) self.assertFalse(self.account.is_ban_forum) self.assertTrue(self.account._model.ban_game_end_at < datetime.datetime.now()) def test_ban_forum(self): with mock.patch('the_tale.accounts.prototypes.AccountPrototype.cmd_update_hero') as cmd_update_hero: self.account.ban_forum(2) self.assertEqual(cmd_update_hero.call_count, 0) self.account.reload() self.assertTrue(self.account.is_ban_forum) self.assertTrue(self.account.is_ban_any) self.assertFalse(self.account.is_ban_game) self.assertTrue(datetime.timedelta(days=1) < self.account._model.ban_forum_end_at - datetime.datetime.now() < datetime.timedelta(days=3)) def test_ban_forum__extend(self): self.account.ban_forum(3) with mock.patch('the_tale.accounts.prototypes.AccountPrototype.cmd_update_hero') as cmd_update_hero: self.account.ban_forum(2) self.assertEqual(cmd_update_hero.call_count, 0) self.account.reload() self.assertTrue(self.account.is_ban_forum) self.assertTrue(self.account.is_ban_any) self.assertFalse(self.account.is_ban_game) self.assertTrue(datetime.timedelta(days=4) < self.account._model.ban_forum_end_at - datetime.datetime.now() < datetime.timedelta(days=6)) def test_ban_forum__reset(self): self.account.ban_forum(2) with mock.patch('the_tale.accounts.prototypes.AccountPrototype.cmd_update_hero') as cmd_update_hero: self.account.reset_ban_forum() self.assertEqual(cmd_update_hero.call_count, 0) self.account.reload() self.assertFalse(self.account.is_ban_forum) self.assertFalse(self.account.is_ban_any) self.assertFalse(self.account.is_ban_game) self.assertTrue(self.account._model.ban_forum_end_at < datetime.datetime.now())
47.48433
163
0.710866
16,618
0.99706
0
0
2,286
0.137157
0
0
1,576
0.094558
ce62bdf76c9ed174e5607a0e506209e79d02b892
698
py
Python
952/952.py
vladcto/ACMP_Answers
bc163068d7b27c5241f995da3f58a1f8c623d460
[ "Unlicense" ]
1
2020-02-17T18:56:12.000Z
2020-02-17T18:56:12.000Z
952/952.py
vladcto/ACMP_Answers
bc163068d7b27c5241f995da3f58a1f8c623d460
[ "Unlicense" ]
null
null
null
952/952.py
vladcto/ACMP_Answers
bc163068d7b27c5241f995da3f58a1f8c623d460
[ "Unlicense" ]
null
null
null
inp = input().split(" ") adult = int(inp[0]) child = int(inp[1]) if adult == 0 and child == 0: print("0 0") quit() if adult == 0: print("Impossible") quit() min = 0 # К каждому взрослому один бесплатный ребенок => # "Платные дети" это разница между взрослыми и всеми детьми not_free_child = child - adult # Если детей было меньше, то может оказаться отриц. ответ if not_free_child < 0: not_free_child = 0 min = adult + not_free_child max = 0 # К одному взрослому приписывает всех детей, # кроме одного "бесплатного". if child == 0: # Если нет детей, то не нужно и отнимать "бесплатного" max = adult else: max = adult + child - 1 print("{} {}".format(min, max))
21.8125
59
0.65616
0
0
0
0
0
0
0
0
547
0.590076
ce642e6e7a09fb0be794de1dfe62d3f787626a2a
52
py
Python
tests/django/__init__.py
estudio89/maestro-python
331079cb3f0c10de2e19210cbade793544510f33
[ "BSD-3-Clause" ]
null
null
null
tests/django/__init__.py
estudio89/maestro-python
331079cb3f0c10de2e19210cbade793544510f33
[ "BSD-3-Clause" ]
null
null
null
tests/django/__init__.py
estudio89/maestro-python
331079cb3f0c10de2e19210cbade793544510f33
[ "BSD-3-Clause" ]
null
null
null
default_app_config = "tests.django.apps.MyAppConfig"
52
52
0.846154
0
0
0
0
0
0
0
0
31
0.596154
ce6475945a6e1e99c628db4e1feb8a20077669ce
1,440
py
Python
setup.py
eric-volz/defichainLibrary
458a8155bd595bf0fdf026651d95a5fe78dafc9c
[ "MIT" ]
1
2022-03-29T15:15:17.000Z
2022-03-29T15:15:17.000Z
setup.py
eric-volz/defichainLibrary
458a8155bd595bf0fdf026651d95a5fe78dafc9c
[ "MIT" ]
null
null
null
setup.py
eric-volz/defichainLibrary
458a8155bd595bf0fdf026651d95a5fe78dafc9c
[ "MIT" ]
1
2022-03-24T12:25:44.000Z
2022-03-24T12:25:44.000Z
from setuptools import setup from os import path VERSION = '1.0.0' DESCRIPTION = 'Defichain Python Library' # Project URLs project_urls = { "Tracker": "https://github.com/eric-volz/DefichainPython", "Documentation": "https://docs.defichain-python.de" } this_directory = path.abspath(path.dirname(__file__)) with open(path.join(this_directory, 'README_for_pypi.md'), encoding='utf-8') as f: LONG_DESCRIPTION = f.read() # Setting up setup( name="defichain", version=VERSION, author="Intr0c", author_email="introc@volz.link", url="https://github.com/eric-volz/DefichainPython", description=DESCRIPTION, long_description=LONG_DESCRIPTION, long_description_content_type='text/markdown', packages=['defichain', 'defichain.node', 'defichain.exceptions', 'defichain.ocean', 'defichain.node.modules', 'defichain.ocean.modules'], install_requires=["requests"], keywords=['python', 'defichain', 'node', 'ocean'], classifiers=[ "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Operating System :: Unix", "Operating System :: MacOS :: MacOS X", "Operating System :: Microsoft :: Windows", ] )
31.304348
82
0.634722
0
0
0
0
0
0
0
0
747
0.51875
ce6649c4f6c16cf45f7213f96f05b37dd34d751f
4,936
py
Python
test/test_hdf5.py
gonzalobg/hpc-container-maker
dd5486c3fbb0fce38d825173022908ef0f96f77e
[ "Apache-2.0" ]
1
2021-01-04T00:29:22.000Z
2021-01-04T00:29:22.000Z
test/test_hdf5.py
gonzalobg/hpc-container-maker
dd5486c3fbb0fce38d825173022908ef0f96f77e
[ "Apache-2.0" ]
null
null
null
test/test_hdf5.py
gonzalobg/hpc-container-maker
dd5486c3fbb0fce38d825173022908ef0f96f77e
[ "Apache-2.0" ]
null
null
null
# Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # pylint: disable=invalid-name, too-few-public-methods, bad-continuation """Test cases for the hdf5 module""" from __future__ import unicode_literals from __future__ import print_function import logging # pylint: disable=unused-import import unittest from helpers import centos, docker, ubuntu from hpccm.building_blocks.hdf5 import hdf5 class Test_hdf5(unittest.TestCase): def setUp(self): """Disable logging output messages""" logging.disable(logging.ERROR) @ubuntu @docker def test_defaults_ubuntu(self): """Default hdf5 building block""" h = hdf5() self.assertEqual(str(h), r'''# HDF5 version 1.10.6 RUN apt-get update -y && \ DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ bzip2 \ file \ make \ wget \ zlib1g-dev && \ rm -rf /var/lib/apt/lists/* RUN mkdir -p /var/tmp && wget -q -nc --no-check-certificate -P /var/tmp http://www.hdfgroup.org/ftp/HDF5/releases/hdf5-1.10/hdf5-1.10.6/src/hdf5-1.10.6.tar.bz2 && \ mkdir -p /var/tmp && tar -x -f /var/tmp/hdf5-1.10.6.tar.bz2 -C /var/tmp -j && \ cd /var/tmp/hdf5-1.10.6 && ./configure --prefix=/usr/local/hdf5 --enable-cxx --enable-fortran && \ make -j$(nproc) && \ make -j$(nproc) install && \ rm -rf /var/tmp/hdf5-1.10.6 /var/tmp/hdf5-1.10.6.tar.bz2 ENV CPATH=/usr/local/hdf5/include:$CPATH \ HDF5_DIR=/usr/local/hdf5 \ LD_LIBRARY_PATH=/usr/local/hdf5/lib:$LD_LIBRARY_PATH \ LIBRARY_PATH=/usr/local/hdf5/lib:$LIBRARY_PATH \ PATH=/usr/local/hdf5/bin:$PATH''') @centos @docker def test_defaults_centos(self): """Default hdf5 building block""" h = hdf5() self.assertEqual(str(h), r'''# HDF5 version 1.10.6 RUN yum install -y \ bzip2 \ file \ make \ wget \ zlib-devel && \ rm -rf /var/cache/yum/* RUN mkdir -p /var/tmp && wget -q -nc --no-check-certificate -P /var/tmp http://www.hdfgroup.org/ftp/HDF5/releases/hdf5-1.10/hdf5-1.10.6/src/hdf5-1.10.6.tar.bz2 && \ mkdir -p /var/tmp && tar -x -f /var/tmp/hdf5-1.10.6.tar.bz2 -C /var/tmp -j && \ cd /var/tmp/hdf5-1.10.6 && ./configure --prefix=/usr/local/hdf5 --enable-cxx --enable-fortran && \ make -j$(nproc) && \ make -j$(nproc) install && \ rm -rf /var/tmp/hdf5-1.10.6 /var/tmp/hdf5-1.10.6.tar.bz2 ENV CPATH=/usr/local/hdf5/include:$CPATH \ HDF5_DIR=/usr/local/hdf5 \ LD_LIBRARY_PATH=/usr/local/hdf5/lib:$LD_LIBRARY_PATH \ LIBRARY_PATH=/usr/local/hdf5/lib:$LIBRARY_PATH \ PATH=/usr/local/hdf5/bin:$PATH''') @ubuntu @docker def test_ldconfig(self): """ldconfig option""" h = hdf5(ldconfig=True, version='1.10.4') self.assertEqual(str(h), r'''# HDF5 version 1.10.4 RUN apt-get update -y && \ DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ bzip2 \ file \ make \ wget \ zlib1g-dev && \ rm -rf /var/lib/apt/lists/* RUN mkdir -p /var/tmp && wget -q -nc --no-check-certificate -P /var/tmp http://www.hdfgroup.org/ftp/HDF5/releases/hdf5-1.10/hdf5-1.10.4/src/hdf5-1.10.4.tar.bz2 && \ mkdir -p /var/tmp && tar -x -f /var/tmp/hdf5-1.10.4.tar.bz2 -C /var/tmp -j && \ cd /var/tmp/hdf5-1.10.4 && ./configure --prefix=/usr/local/hdf5 --enable-cxx --enable-fortran && \ make -j$(nproc) && \ make -j$(nproc) install && \ echo "/usr/local/hdf5/lib" >> /etc/ld.so.conf.d/hpccm.conf && ldconfig && \ rm -rf /var/tmp/hdf5-1.10.4 /var/tmp/hdf5-1.10.4.tar.bz2 ENV CPATH=/usr/local/hdf5/include:$CPATH \ HDF5_DIR=/usr/local/hdf5 \ LIBRARY_PATH=/usr/local/hdf5/lib:$LIBRARY_PATH \ PATH=/usr/local/hdf5/bin:$PATH''') @ubuntu @docker def test_runtime(self): """Runtime""" h = hdf5() r = h.runtime() self.assertEqual(r, r'''# HDF5 RUN apt-get update -y && \ DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ zlib1g && \ rm -rf /var/lib/apt/lists/* COPY --from=0 /usr/local/hdf5 /usr/local/hdf5 ENV CPATH=/usr/local/hdf5/include:$CPATH \ HDF5_DIR=/usr/local/hdf5 \ LD_LIBRARY_PATH=/usr/local/hdf5/lib:$LD_LIBRARY_PATH \ LIBRARY_PATH=/usr/local/hdf5/lib:$LIBRARY_PATH \ PATH=/usr/local/hdf5/bin:$PATH''')
37.393939
164
0.640194
3,980
0.806321
0
0
3,815
0.772893
0
0
4,089
0.828404
ce69ac58ca5435e4721a3c9bb26cdcd8b83c0839
160
py
Python
exercise/newfile45.py
LeeBeral/python
9f0d360d69ee5245e3ef13a9dc9fc666374587a4
[ "MIT" ]
null
null
null
exercise/newfile45.py
LeeBeral/python
9f0d360d69ee5245e3ef13a9dc9fc666374587a4
[ "MIT" ]
null
null
null
exercise/newfile45.py
LeeBeral/python
9f0d360d69ee5245e3ef13a9dc9fc666374587a4
[ "MIT" ]
null
null
null
show databases; show tables desc table名 use database名 delete from 库名.表名 where id=5; select * from 表名 where id=5; update 表名 set age=15,home='北京' where id=5
22.857143
41
0.73125
0
0
0
0
0
0
0
0
8
0.043478
ce6a80ab1bf79ba95677f54c2de54bfe1b5f0e5f
604
py
Python
src/word2vec.py
shiroyagicorp/japanese-word2vec-model-builder
c9570702110f2851f4cb7d38948c5d7f59ef8a4c
[ "MIT" ]
98
2017-02-20T14:23:36.000Z
2022-01-23T07:09:29.000Z
src/word2vec.py
shiroyagicorp/japanese-word2vec-model-builder
c9570702110f2851f4cb7d38948c5d7f59ef8a4c
[ "MIT" ]
1
2021-06-29T05:34:39.000Z
2021-11-17T23:52:07.000Z
src/word2vec.py
shiroyagicorp/japanese-word2vec-model-builder
c9570702110f2851f4cb7d38948c5d7f59ef8a4c
[ "MIT" ]
11
2017-11-07T05:25:30.000Z
2021-06-29T05:28:08.000Z
import multiprocessing from gensim.models.word2vec import Word2Vec def build_gensim_w2v_model(model_path, iter_tokens, size, window, min_count): """ Parameters ---------- model_path : string Path of Word2Vec model iter_tokens : iterator Iterator of documents, which are lists of words """ model = Word2Vec( size=size, window=window, min_count=min_count, workers=multiprocessing.cpu_count() ) model.build_vocab(iter_tokens()) model.train(iter_tokens()) model.init_sims(replace=True) model.save(model_path)
24.16
77
0.663907
0
0
0
0
0
0
0
0
179
0.296358
ce6c5f2a56792c631b587b682534feb77c7a0a15
2,945
py
Python
companion/telegram.py
jouir/mining-companion
b66aa8b1586a31ddad0c2454e4762661c63385a1
[ "Unlicense" ]
2
2021-02-25T09:09:57.000Z
2021-03-03T14:11:30.000Z
companion/telegram.py
jouir/flexpool-activity
b66aa8b1586a31ddad0c2454e4762661c63385a1
[ "Unlicense" ]
2
2021-08-18T11:10:26.000Z
2021-08-18T11:14:23.000Z
companion/telegram.py
jouir/mining-companion
b66aa8b1586a31ddad0c2454e4762661c63385a1
[ "Unlicense" ]
null
null
null
import logging import os from copy import copy import requests from jinja2 import Environment, FileSystemLoader logger = logging.getLogger(__name__) absolute_path = os.path.split(os.path.abspath(__file__))[0] class TelegramNotifier: def __init__(self, chat_id, auth_key): self._auth_key = auth_key self._default_payload = {'auth_key': auth_key, 'chat_id': chat_id, 'parse_mode': 'MarkdownV2', 'disable_web_page_preview': True} @staticmethod def _markdown_escape(text): text = str(text) for special_char in ['\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '#', '+', '-', '.', '!', '=']: text = text.replace(special_char, fr'\{special_char}') return text def _generate_payload(self, message_variables, template_name): payload = copy(self._default_payload) template_path = os.path.join(absolute_path, 'templates') loader = FileSystemLoader(template_path) env = Environment(loader=loader) template = env.get_template(template_name) template_variables = {} for key, value in message_variables.items(): template_variables[key] = self._markdown_escape(value) text = template.render(**template_variables) payload['text'] = text return payload def notify_block(self, pool, number, hash, reward, time, round_time, luck, reward_fiat=None): message_variables = {'pool': pool, 'number': number, 'hash': hash, 'reward': reward, 'time': time, 'round_time': round_time, 'luck': luck, 'reward_fiat': reward_fiat} payload = self._generate_payload(message_variables, 'block.md.j2') self._send_message(payload) def notify_balance(self, pool, address, url, balance, balance_percentage, balance_fiat=None): message_variables = {'pool': pool, 'address': address, 'url': url, 'balance': balance, 'balance_percentage': balance_percentage, 'balance_fiat': balance_fiat} payload = self._generate_payload(message_variables, 'balance.md.j2') self._send_message(payload) def notify_payment(self, pool, address, txid, amount, time, duration, amount_fiat=None): message_variables = {'pool': pool, 'address': address, 'txid': txid, 'amount': amount, 'amount_fiat': amount_fiat, 'time': time, 'duration': duration} payload = self._generate_payload(message_variables, 'payment.md.j2') self._send_message(payload) def _send_message(self, payload): logger.debug(self._sanitize(payload)) r = requests.post(f'https://api.telegram.org/bot{self._auth_key}/sendMessage', json=payload) r.raise_for_status() @staticmethod def _sanitize(payload): payload = copy(payload) if 'auth_key' in payload: payload['auth_key'] = '***' return payload
43.955224
110
0.639389
2,731
0.927334
0
0
438
0.148727
0
0
467
0.158574
ce6cccfac6a948d40441d5b2f5121b05efacb62f
295
py
Python
forecast_lab/metrics.py
gsimbr/forecast-lab
a26234f3e11b4b8268d6cbe33bb84d79da45ecdd
[ "MIT" ]
5
2019-06-04T11:04:06.000Z
2022-03-29T23:05:25.000Z
forecast_lab/metrics.py
gsimbr/forecast-lab
a26234f3e11b4b8268d6cbe33bb84d79da45ecdd
[ "MIT" ]
1
2022-02-14T13:22:47.000Z
2022-02-14T13:22:47.000Z
forecast_lab/metrics.py
gsimbr/forecast-lab
a26234f3e11b4b8268d6cbe33bb84d79da45ecdd
[ "MIT" ]
2
2020-02-17T11:54:18.000Z
2020-10-06T12:49:15.000Z
import numpy import math from sklearn.metrics import mean_squared_error def root_mean_squared_error(y_true, y_pred): return math.sqrt(mean_squared_error(y_true, y_pred)) def mean_absolute_percentage_error(y_true, y_pred): return numpy.mean(numpy.abs((y_true - y_pred) / y_true)) * 100
29.5
66
0.79322
0
0
0
0
0
0
0
0
0
0
ce6db4a22e7fa1d771c8c341bb718daa5999b3ea
446
py
Python
Python_Interview/Algorithm/step_wise.py
QAlexBall/Learning_Py
8a5987946928a9d86f6807555ed435ac604b2c44
[ "MIT" ]
2
2019-01-24T15:06:59.000Z
2019-01-25T07:34:45.000Z
Python_Interview/Algorithm/step_wise.py
QAlexBall/Learning_Py
8a5987946928a9d86f6807555ed435ac604b2c44
[ "MIT" ]
1
2019-12-23T09:45:11.000Z
2019-12-23T09:45:11.000Z
Python_Interview/Algorithm/step_wise.py
QAlexBall/Learning_Py
8a5987946928a9d86f6807555ed435ac604b2c44
[ "MIT" ]
1
2019-07-18T14:21:35.000Z
2019-07-18T14:21:35.000Z
''' 杨氏矩阵查找 在一个m行n列二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下 递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。 ''' def get_value(l, r, c): return l[r][c] def find(l, x): m = len(l) - 1 n = len(l[0]) - 1 r = 0 c = n while c >= 0 and r <= m: value = get_value(l, r, c) if value == x: return True elif value > x: c = c - 1 elif value < x: r = r + 1 return False
18.583333
46
0.495516
0
0
0
0
0
0
0
0
285
0.453822
ce6e0d7a568a5fc925496c5e465b79c3f4a3e233
3,854
py
Python
run_create_codebuild_default.py
HardBoiledSmith/johanna
0443a9040f0248f0a800c9d4b062e375f997bb6f
[ "MIT" ]
64
2016-11-03T11:20:25.000Z
2021-05-24T03:08:57.000Z
run_create_codebuild_default.py
HardBoiledSmith/johanna
0443a9040f0248f0a800c9d4b062e375f997bb6f
[ "MIT" ]
69
2016-11-03T14:09:35.000Z
2022-02-07T12:52:05.000Z
run_create_codebuild_default.py
HardBoiledSmith/johanna
0443a9040f0248f0a800c9d4b062e375f997bb6f
[ "MIT" ]
19
2016-11-03T11:04:51.000Z
2020-06-12T10:40:57.000Z
#!/usr/bin/env python3 import json import time from run_common import AWSCli from run_common import print_message from run_create_codebuild_common import create_base_iam_policy from run_create_codebuild_common import create_iam_service_role from run_create_codebuild_common import create_managed_secret_iam_policy from run_create_codebuild_common import create_notification_rule from run_create_codebuild_common import get_notification_rule from run_create_codebuild_common import have_parameter_store from run_create_codebuild_common import update_notification_rule def run_create_default_project(name, settings): aws_cli = AWSCli(settings['AWS_REGION']) git_branch = settings['BRANCH'] build_spec = settings['BUILD_SPEC'] build_timeout = settings['BUILD_TIMEOUT'] compute_type = settings['ENV_COMPUTE_TYPE'] description = settings['DESCRIPTION'] git_repo = settings['GITHUB_REPO'] image = settings['IMAGE'] ################################################################################ print_message('check previous version') cmd = ['codebuild', 'list-projects'] result = aws_cli.run(cmd) need_update = name in result['projects'] ################################################################################ print_message('create iam service role') service_role_name = create_iam_service_role(aws_cli, name) create_base_iam_policy(aws_cli, name, settings, service_role_name) if have_parameter_store(settings): create_managed_secret_iam_policy(aws_cli, name, settings, service_role_name) time.sleep(10) service_role_arn = aws_cli.get_role_arn(service_role_name) ################################################################################ print_message('set environment variable') env_list = [] for pp in settings['ENV_VARIABLES']: if 'PARAMETER_STORE' == pp['type']: nn = f"/CodeBuild/{name}/{pp['name']}" cmd = ['ssm', 'get-parameter', '--name', nn] aws_cli.run(cmd) pp['value'] = nn env_list.append(pp) ################################################################################ config = { "name": name, "description": description, "source": { "type": "GITHUB", "location": git_repo, "gitCloneDepth": 0, "buildspec": build_spec, "auth": { "type": "OAUTH" }, "insecureSsl": True, "sourceIdentifier": git_branch }, "artifacts": { "type": "NO_ARTIFACTS" }, "cache": { "type": "NO_CACHE" }, "environment": { "type": "LINUX_CONTAINER", "image": image, "computeType": compute_type, "environmentVariables": env_list }, "serviceRole": service_role_arn, "timeoutInMinutes": build_timeout, "badgeEnabled": True } config = json.dumps(config) if need_update: print_message(f'update project: {name}') cmd = ['codebuild', 'update-project', '--cli-input-json', config, '--source-version', git_branch] aws_cli.run(cmd) else: print_message(f'create project: {name}') cmd = ['codebuild', 'create-project', '--cli-input-json', config, '--source-version', git_branch] aws_cli.run(cmd) ################################################################################ print_message('create slack notification') project_arn = result['project']['arn'] notification_rule_arn = get_notification_rule(aws_cli, project_arn) if not notification_rule_arn: create_notification_rule(aws_cli, name, project_arn) else: update_notification_rule(aws_cli, name, notification_rule_arn)
34.106195
105
0.583809
0
0
0
0
0
0
0
0
1,258
0.326414
ce6e3c09a2e66420e8e9c581cff7e8f8d2db23fe
2,282
py
Python
config/test.py
nahidupa/grr
100a9d85ef2abb234e12e3ac2623caffb4116be7
[ "Apache-2.0" ]
1
2016-02-13T15:40:20.000Z
2016-02-13T15:40:20.000Z
config/test.py
nahidupa/grr
100a9d85ef2abb234e12e3ac2623caffb4116be7
[ "Apache-2.0" ]
3
2020-02-11T22:29:15.000Z
2021-06-10T17:44:31.000Z
config/test.py
nahidupa/grr
100a9d85ef2abb234e12e3ac2623caffb4116be7
[ "Apache-2.0" ]
null
null
null
#!/usr/bin/env python """Configuration parameters for the test subsystem.""" import os from grr.lib import config_lib # Default for running in the current directory config_lib.DEFINE_constant_string( "Test.srcdir", os.path.normpath(os.path.dirname(__file__) + "/../.."), "The directory containing the source code.") config_lib.DEFINE_constant_string( "Test.data_dir", default="%(Test.srcdir)/grr/test_data", help="The directory where test data exist.") config_lib.DEFINE_constant_string( "Test.config", default="%(Test.srcdir)/grr/config/grr-server.yaml", help="The path where the test configuration file exists.") config_lib.DEFINE_constant_string( "Test.additional_test_config", default="%(Test.data_dir)/localtest.yaml", help="The path to a test config with local customizations.") config_lib.DEFINE_string("Test.tmpdir", "/tmp/", help="Somewhere to write temporary files.") config_lib.DEFINE_string("Test.data_store", "FakeDataStore", "The data store to run the tests against.") config_lib.DEFINE_integer("Test.remote_pdb_port", 2525, "Remote debugger port.") config_lib.DEFINE_list("Test.end_to_end_client_ids", [], "List of client ids to perform regular end_to_end tests" " on. These clients should be always on and connected" " to the network.") config_lib.DEFINE_list("Test.end_to_end_client_hostnames", [], "List of hostnames to perform regular end_to_end tests" " on. These clients should be always on and connected" " to the network.") config_lib.DEFINE_string("Test.end_to_end_result_check_wait", "50m", "rdfvalue.Duration string that determines how long we " "wait after starting the endtoend test hunt before we " "check the results. Should be long enough that all " "clients will have picked up the hunt, but not so " "long that the flow gets timed out.") config_lib.DEFINE_string("PrivateKeys.ca_key_raw_data", "", "For testing purposes.")
40.75
80
0.633655
0
0
0
0
0
0
0
0
1,330
0.582822
ce6eed2c9d0065dffb079ead3cb624c8d3a05810
224
py
Python
wixaward/urls.py
LekamCharity/wix-projects
76f9ab4429a978a42f0cea3e3a305a7cdfc4541d
[ "MIT" ]
null
null
null
wixaward/urls.py
LekamCharity/wix-projects
76f9ab4429a978a42f0cea3e3a305a7cdfc4541d
[ "MIT" ]
null
null
null
wixaward/urls.py
LekamCharity/wix-projects
76f9ab4429a978a42f0cea3e3a305a7cdfc4541d
[ "MIT" ]
null
null
null
from django.urls import path urlpatterns=[ path('profile',views.profile, name='profile'), ] if settings.DEBUG: urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
22.4
60
0.665179
0
0
0
0
0
0
0
0
18
0.080357
ce6fa0b28898bd299005931220b5305722ba63c3
8,940
py
Python
pylogview/reader.py
CrazyIvan359/logview
4fb145843315dd03ff4ba414a5a617775d9d2af1
[ "MIT" ]
null
null
null
pylogview/reader.py
CrazyIvan359/logview
4fb145843315dd03ff4ba414a5a617775d9d2af1
[ "MIT" ]
3
2020-11-01T23:57:39.000Z
2020-11-02T01:21:48.000Z
pylogview/reader.py
CrazyIvan359/logview
4fb145843315dd03ff4ba414a5a617775d9d2af1
[ "MIT" ]
null
null
null
import typing as t from pylogview import datefinder from pylogview.record import LogRecord if t.TYPE_CHECKING: from pylogview.window import Window class LogReader: __slots__ = [ "_window", "filename", "_lines", "records", "_record_prefix_length", "_fd", "_buffer", ] def __init__(self, window: "Window", filename: str): self._window = window self.filename = filename self._lines = 0 # file line count self.records: t.List[LogRecord] = [] self._record_prefix_length: t.Union[ str, None ] = None # pattern that starts record self._fd = None self._buffer = bytes() # byte buffer for file data self._open() def __del__(self): if self._fd: try: self._fd.close() except: pass @property def isOpen(self) -> bool: if self._fd is not None: return not self._fd.closed return False @property def lines(self) -> int: return self._lines def preload(self) -> t.List[LogRecord]: self._get_line_count() self._read_last() return self.read(0) def read(self, records: int = 1) -> t.List[LogRecord]: """Read to end of file and parse next line""" self._read() return self._get_records(records) ##### Internal Methods ##### def _open(self): try: self._fd = open(self.filename, "rb") except IOError as err: self._window.log.append( f"Failed to open '{self.filename}': [{err.errno}] {err.strerror}" ) self._fd = None def _read(self): if self.isOpen: try: data = self._fd.read() self._lines += data.count(b"\n") self._buffer += data except IOError as err: self._window.log.append( f"Error while reading '{self.filename}': [{err.errno}] {err.strerror}" ) def _get_line_count(self): if not self.isOpen: return try: self._fd.seek(0, 0) self._lines = 0 for line in self._fd: # pylint: disable=unused-variable self._lines += 1 except IOError as err: self._fd = None self._lines = 0 self._window.log.append( f"Error while reading '{self.filename}': [{err.errno}] {err.strerror}" ) def _read_last(self): """Read last ``lines`` of file, like 'tail -n'""" if not self.isOpen: return try: last_read_block = self._fd.tell() block_end_byte = last_read_block BLOCK_SIZE = min(block_end_byte, 1024) remain_lines = self._window.config.lines block_num = -1 blocks = [] while remain_lines > 0 and block_end_byte > 0: if block_end_byte - BLOCK_SIZE > 0: self._fd.seek(block_num * BLOCK_SIZE, 2) blocks.append(self._fd.read(BLOCK_SIZE)) else: self._fd.seek(0, 0) blocks.append(self._fd.read(block_end_byte)) remain_lines -= blocks[-1].count(b"\n") block_end_byte -= BLOCK_SIZE block_num -= 1 self._fd.seek(last_read_block, 0) except IOError as err: self._fd = None self._window.log.append( f"Error while reading '{self.filename}': [{err.errno}] {err.strerror}" ) else: for block in blocks[::-1]: self._buffer += block def _find_record_prefix_length(self): """ Rudamentary prefix length finder. Looks for repeated same number of chars between newline/file-start and timestamp. """ prefix_lengths = [] last_end = 1 buffer_string = self._buffer.decode() for result in datefinder.find_dates(buffer_string, source=True, index=True): if self._record_prefix_length is not None: break elif len(result[1]) < 6: # skip matches too short, probably just numbers not a timestamp continue timestamp_end = result[2][1] timestamp_start = timestamp_end - len(result[1]) - 1 prefix_lengths.append( len( buffer_string[ timestamp_start - buffer_string[last_end:timestamp_start][::-1].find( "\n" ) : timestamp_start ] ) ) last_end = buffer_string.find("\n", timestamp_end) for length in prefix_lengths: if prefix_lengths.count(length) > 3: self._record_prefix_length = length break def _get_records(self, count) -> t.List[LogRecord]: # make sure we have a lock on the prefix before parsing anything if self._record_prefix_length is None: self._find_record_prefix_length() if self._record_prefix_length is None: return [] # find timestamps in buffer buffer_string = self._buffer.decode() timestamp_match = datefinder.find_dates(buffer_string, source=True, index=True) this_result = next(timestamp_match, None) match_offset = 0 new_records = 0 while True: if len(self._buffer) == 0: break if this_result is None: # if no timestamps found, read to the end of the buffer this_start = len(self._buffer) else: # skip if this match is too short, sometimes numbers will match if len(this_result[1]) < 8: this_result = next(timestamp_match, None) continue # this should always work out to 0, anything else means there is # data in the buffer that belongs to the previous record this_start = ( this_result[2][1] - match_offset - len(this_result[1]) - 1 - self._record_prefix_length ) if this_start != 0: # make sure this match is a record if this_result is not None and buffer_string[this_start - 1] != "\n": this_result = next(timestamp_match, None) continue # something left in the buffer before the this record start # append it to the last record and search again if self.records: # only keep if there is at least one record, anything else is # a partial record from tail-ing the file self.records[-1].append(buffer_string[:this_start].split("\n")) self._buffer = self._buffer[len(buffer_string[:this_start].encode()) :] buffer_string = buffer_string[this_start:] match_offset += this_start this_start -= match_offset while True: next_result = next(timestamp_match, None) if next_result is None: # no second timestamp found, read to the end of the buffer next_start = len(self._buffer) else: # if len(next_result[1]) < 8: # continue next_start = ( next_result[2][1] - match_offset - len(next_result[1]) - 1 - self._record_prefix_length ) if next_result is None or buffer_string[next_start - 1] == "\n": self.records.append( LogRecord( self._window, buffer_string[:next_start].split("\n"), ) ) self._buffer = self._buffer[ len(buffer_string[:next_start].encode()) : ] buffer_string = buffer_string[next_start:] match_offset += next_start new_records += 1 break if next_result is None or new_records == count: break else: this_result = next_result self.records = self.records[0 - self._window.config.lines :] return self.records[0 - new_records :] if new_records else []
36.048387
90
0.503803
8,784
0.98255
0
0
196
0.021924
0
0
1,502
0.168009
ce70b641f16acd29f6ec6fd771bef13d60610bff
235
py
Python
zad1_6.py
kamilhabrych/python-semestr5-lista1
65faeffe83bcc4706b2818e2e7802d986b19244b
[ "MIT" ]
null
null
null
zad1_6.py
kamilhabrych/python-semestr5-lista1
65faeffe83bcc4706b2818e2e7802d986b19244b
[ "MIT" ]
null
null
null
zad1_6.py
kamilhabrych/python-semestr5-lista1
65faeffe83bcc4706b2818e2e7802d986b19244b
[ "MIT" ]
null
null
null
x = int(input('Podaj pierwsza liczbe calkowita: ')) y = int(input('Podaj druga liczbe calkowita: ')) z = int(input('Podaj trzecia liczbe calkowita: ')) print() if x > 10: print(x) if y > 10: print(y) if z > 10: print(z)
16.785714
51
0.617021
0
0
0
0
0
0
0
0
101
0.429787
ce70fc922ee9bc7104f6b739b1a14c96b849d90a
6,194
py
Python
modules/bulletinGenerator_Kingsgrove.py
featherbear/swec-elvanto-automation
7f330ca5a87623ca452170efb4845814a4fbc2ad
[ "MIT" ]
null
null
null
modules/bulletinGenerator_Kingsgrove.py
featherbear/swec-elvanto-automation
7f330ca5a87623ca452170efb4845814a4fbc2ad
[ "MIT" ]
null
null
null
modules/bulletinGenerator_Kingsgrove.py
featherbear/swec-elvanto-automation
7f330ca5a87623ca452170efb4845814a4fbc2ad
[ "MIT" ]
null
null
null
from mailmerge import MailMerge import re import os.path from ElvantoAPIExtensions import Enums, Helpers from modules.__stub__ import ModuleStub class Module(ModuleStub): __VERSION__ = "1.0" __NAME__ = "bulletinGenerator_Kingsgrove" # __executeTime__ = "16:00" # __executeDay__ = "thursday" settings = { "general": { "template": "", "serviceName": "", "pastorName": "", "pastorTelephone": "", "pastorEmail": "", }, } def validate(self): self._baseFolder = os.path.join("files", self.__NAME__) _templateFile = os.path.join(self._baseFolder, self.settings["general"]["template"]) if not os.path.isfile(_templateFile): raise self.ModuleException("Invalid template file path") self._templateFile = _templateFile def run(self): _serviceDate = Helpers.NextDate(Enums.Days.SUNDAY) import math weekNumber = int(math.ceil(_serviceDate.day / 7)) try: thisWeekServices = Helpers.ServicesOnDate(self.conn, _serviceDate, ["volunteers", "plans"]) thisWeekService = next( filter(lambda _: _.name == self.settings["general"]["serviceName"], thisWeekServices)) except Exception as e: if e.__class__.__name__ == "ConnectionError": print("Couldn't connect to Elvanto API") return False if type(e) == StopIteration: print("Couldn't find an upcoming service called \"%s\"" % self.settings["general"]["serviceName"]) return False print("An error occured:", e) def getWeeklyConfession(week: int): assert 1 <= week <= 5 confessionOne = """ Almighty and most merciful Father, You have loved us with an everlasting love, But we have gone our own way And have rejected you in thought, word, and deed. We are sorry for our sins And turn away from them For the sake of your Son who died for us, Forgive us, cleanse us and change us. By your Holy Spirit, enable us to live for you, And to please you more and more; Through Jesus Christ our Lord. Amen. """ confessionTwo = """ Most merciful God, we humbly admit that we need your help. We confess that we have wandered from your way: We have done wrong, and we have failed to do what is right. You alone can save us. Have mercy on us: Wipe out our sins and teach us to forgive others. Bring forth in us the fruit of the Spirit That we may live as disciples of Christ. This we ask in the name of Jesus our Saviour. Amen. """ confessionThree = """ Heavenly Father, We praise you for adopting us as your children And making us heirs of eternal life. In your mercy you have washed us from our sins And made us clean in your sight. Yet we still fail to love you as we should and serve you as we ought. Forgive us our sins and renew us by your grace, That we may continue to grow as members of Christ, In whom alone is our salvation. Amen. """ confessionFour = """ Merciful God, our maker and our judge, we have sinned against you in thought, word, and deed: we have not loved you with our whole heart, we have not loved our neighbours as ourselves: we repent, and are sorry for all our sins. Father, forgive us. Strengthen us to love and obey you in newness of life; through Jesus Christ our Lord. Amen """ confessionFive = """ Lord God, we have sinned against you; we have done evil in your sight. We are sorry and repent. Have mercy on us according to your love. Wash away our wrongdoing and cleanse us from our sin. Renew a right spirit within us and restore us to the joy of your salvation, through Jesus Christ our Lord. Amen. """ return [None, confessionOne, confessionTwo, confessionThree, confessionFour, confessionFive][ week].strip() stringify = lambda volunteerArray: ", ".join(map(repr, volunteerArray)) replacements = { "prettyDate": _serviceDate.strftime("%A, %#d %B %Y"), "branch": self.settings["general"]["serviceName"], "weeklyConfession": getWeeklyConfession(weekNumber), "pastorName": self.settings["general"]["pastorName"], "pastorTelephone": self.settings["general"]["pastorTelephone"], "pastorEmail": self.settings["general"]["pastorEmail"], "scripturePassage": None, "sermonPassage": None, "speaker": None, } map(print, thisWeekService.plan) scripturePassageItem = next(filter(lambda _: _.title.startswith("Bible Reading") and not _.title.startswith("Bible Reading (Sermon)"), thisWeekService.plan)) _scripturePassageItem = re.search('^Bible Reading (?:- )?(.*)$', scripturePassageItem.title) if _scripturePassageItem: replacements["scripturePassage"] = _scripturePassageItem.group(1) else: replacements["scripturePassage"] = re.sub('<.*?>', "", scripturePassageItem.description).strip() # Details in the title sermonPassageItem = next(filter(lambda _: _.title.startswith("Bible Reading (Sermon"), thisWeekService.plan)) replacements["sermonPassage"] = re.sub('<.*?>', "", sermonPassageItem.description).strip() replacements["speaker"] = stringify(thisWeekService.volunteers.byPositionName("Speaker")) with MailMerge(self._templateFile) as document: document.merge(**replacements) filePath = os.path.join(self._baseFolder, "SWEC %s - Bulletin %s.docx" % (self.settings["general"]["serviceName"], _serviceDate.strftime("%#d %B %Y"))) document.write(filePath)
41.293333
165
0.610268
6,047
0.976267
0
0
0
0
0
0
3,201
0.51679
ce735019669e5c6f53493f5d8d363b42ab7d2267
1,434
py
Python
class4/e3_pexpect.py
ktbyers/pynet_wantonik
601bce26142b6741202c2bdafb9e0d0cec1b3c78
[ "Apache-2.0" ]
2
2017-05-11T12:05:15.000Z
2021-07-15T18:13:19.000Z
class4/e3_pexpect.py
ktbyers/pynet_wantonik
601bce26142b6741202c2bdafb9e0d0cec1b3c78
[ "Apache-2.0" ]
null
null
null
class4/e3_pexpect.py
ktbyers/pynet_wantonik
601bce26142b6741202c2bdafb9e0d0cec1b3c78
[ "Apache-2.0" ]
1
2017-05-11T12:05:18.000Z
2017-05-11T12:05:18.000Z
#!/usr/bin/env python ''' Simple script to execute shell command on lab router with Pexpect module. ''' import pexpect, sys, re from getpass import getpass def main(): ## Define variables ip_addr = '184.105.247.71' username = 'pyclass' port = 22 password = getpass() ## Set up connection with router try: ssh_conn = pexpect.spawn('ssh -l {} {} -p {}'.format(username, ip_addr, port)) ssh_conn.timeout = 3 ## Expects to see line with 'ssword' string on router ssh_conn.expect('ssword:') ## We then send the password to log in on router ssh_conn.sendline(password) ## To see the prompt on router ssh_conn.expect('pynet-rtr2#') ## Execute 'show ip int brief' command to router ssh_conn.sendline('show ip int brief\n') ssh_conn.expect('pynet-rtr2#') print ssh_conn.before ## Disable 'more' paging ssh_conn.sendline('terminal length 0') ssh_conn.expect('pynet-rtr2#') print ssh_conn.before print ssh_conn.after ## Catching timeout and other errors from the above section except pexpect.TIMEOUT: print 'Found timeout or other error - check your code: variables, args.' host_name = ssh_conn.before host_name = host_name.strip() prompt = host_name + ssh_conn.after prompt = prompt.strip() if __name__ == "__main__": main()
29.875
86
0.631799
0
0
0
0
0
0
0
0
624
0.435146
ce73d6c6f78dfe5a98cce6abd28653eb0dd424b3
2,081
py
Python
codewof/tests/utils/errors/test_MissingRequiredFieldError.py
uccser-admin/programming-practice-prototype
3af4c7d85308ac5bb35bb13be3ec18cac4eb8308
[ "MIT" ]
3
2019-08-29T04:11:22.000Z
2021-06-22T16:05:51.000Z
codewof/tests/utils/errors/test_MissingRequiredFieldError.py
uccser-admin/programming-practice-prototype
3af4c7d85308ac5bb35bb13be3ec18cac4eb8308
[ "MIT" ]
265
2019-05-30T03:51:46.000Z
2022-03-31T01:05:12.000Z
codewof/tests/utils/errors/test_MissingRequiredFieldError.py
samuelsandri/codewof
c9b8b378c06b15a0c42ae863b8f46581de04fdfc
[ "MIT" ]
7
2019-06-29T12:13:37.000Z
2021-09-06T06:49:14.000Z
"""Test class for MissingRequiredFieldError error.""" from django.test import SimpleTestCase from utils.errors.MissingRequiredFieldError import MissingRequiredFieldError class MissingRequiredFieldErrorTest(SimpleTestCase): """Test class for MissingRequiredFieldError error. Note: Tests to check if these were raised appropriately are located where this exception is used. """ def test_attributes(self): exception = MissingRequiredFieldError( "config file path", ["field1", "field2"], "model" ) self.assertEqual(exception.config_file_path, "config file path") self.assertEqual(exception.required_fields, ["field1", "field2"]) self.assertEqual(exception.model, "model") def test_string_single_field(self): exception = MissingRequiredFieldError( "config file path", ["field1"], "model" ) expected_string = ( "\n****************************ERROR****************************\n" "File: config file path\n\n" "A model requires the following field:\n" " - field1\n\n" "For the missing field:\n" " - Is the field name spelt correctly?\n" " - Does the field have the correct value?\n" ) self.assertEqual(exception.__str__(), expected_string) def test_string_multiple_fields(self): exception = MissingRequiredFieldError( "config file path", ["field1", "field2"], "model" ) expected_string = ( "\n****************************ERROR****************************\n" "File: config file path\n\n" "A model requires the following fields:\n" " - field1\n" " - field2\n\n" "For the missing fields:\n" " - Is the field name spelt correctly?\n" " - Does the field have the correct value?\n" ) self.assertEqual(exception.__str__(), expected_string)
35.87931
79
0.555983
1,907
0.916386
0
0
0
0
0
0
928
0.445939
ce75d65d2274a6ff994472ca2ea00470b33ed889
12,685
py
Python
matmih/plot.py
glypher/pokemons
c2ea2edef984ee180425866c3f816504c27e460e
[ "BSD-3-Clause" ]
null
null
null
matmih/plot.py
glypher/pokemons
c2ea2edef984ee180425866c3f816504c27e460e
[ "BSD-3-Clause" ]
null
null
null
matmih/plot.py
glypher/pokemons
c2ea2edef984ee180425866c3f816504c27e460e
[ "BSD-3-Clause" ]
null
null
null
"""plot.py: Utility builder class for ML plots. Uses scikit-learn code samples and framework """ __author__ = "Mihai Matei" __license__ = "BSD" __email__ = "mihai.matei@my.fmi.unibuc.ro" import numpy as np import pandas as pd import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import randomcolor import math from sklearn.metrics import confusion_matrix from sklearn.metrics import roc_curve from sklearn.metrics import auc from .image import Image class PlotBuilder: def __init__(self): self.__figures = [] self._subplot_idx = None self._subplot_size = [1, 1] self._current_subplot = None self._next_plot = True def show(self, close=True): plt.show() if close: self.close() def close(self): for fig in self.__figures: plt.close(fig) self.__figures = [] self._subplot_idx = None self._subplot_size = [1, 1] def same(self): self._next_plot = False return self @staticmethod def get_plot(): return plt def create_subplots(self, rows, cols, fig_size=(18, 18)): self.__figures.append(plt.figure(figsize=fig_size)) self._subplot_idx = 0 self._subplot_size = [rows, cols] return self def _get_next_plot(self, **kwargs): if not self._next_plot: self._next_plot = False return self._current_subplot if self._subplot_idx is not None and self._subplot_idx >= (self._subplot_size[0] * self._subplot_size[1]): self._subplot_idx = None self._subplot_size = [1, 1] if self._subplot_idx is None: self.__figures.append(plt.figure(**kwargs)) self._current_subplot = self.__figures[-1].add_subplot(1, 1, 1) return self._current_subplot self._subplot_idx += 1 self._current_subplot = self.__figures[-1].add_subplot(*self._subplot_size, self._subplot_idx, **kwargs) return self._current_subplot def create_plot(self, title, x_data, *args): """ Plot a series of graphs on X axis points given by x_data and Y axis by tuples of (y_values, y_title) in args """ sp = self._get_next_plot() rand_color = randomcolor.RandomColor() limits = [[100000, -100000], [100000, -100000], [100000, -100000]] x_values, x_label = x_data limits[0][0] = min(limits[0][0], min(x_values)) limits[0][1] = max(limits[0][1], max(x_values)) for data in args: color = rand_color.generate()[0] if isinstance(data, list): i = 0 for y_values in data: i += 1 limits[1][0] = min(limits[1][0], min(y_values)) limits[1][1] = max(limits[1][1], max(y_values)) sp.plot(x_values, y_values, color=color, linewidth=2, linestyle='--' if i % 2 else '-') else: y_values, y_title = data limits[1][0] = min(limits[1][0], min(y_values)) limits[1][1] = max(limits[1][1], max(y_values)) sp.plot(x_values, y_values, label=y_title, color=color, linewidth=2, linestyle='-') sp.set_xlim(limits[0]) sp.set_xlabel(x_label) sp.set_ylim(limits[1]) sp.legend(loc='upper right') sp.set_title(title) return self def create_horizontal_line(self, *args, **kwargs): sp = self._get_next_plot() rand_color = randomcolor.RandomColor() y_limits = list(sp.get_ylim()) for data in args: y_value, y_title = data y_limits[0] = min(y_limits[0], y_value) y_limits[1] = max(y_limits[1], y_value) sp.plot(sp.get_xlim(), [y_value, y_value], label=y_title, color=rand_color.generate()[0], **kwargs) sp.set_ylim([y_limits[0] * 0.9, y_limits[1] * 1.1]) sp.legend(loc='upper right') return self def create_scatter_plot(self, title, axis_labels, *args): """ Plot a series of graphs of scatter points given by the list of tuples (x, y, data_label) """ markers = ['o', '*', '+', 'P', 'X', 'D'] is_3d = len(axis_labels) == 3 sp = self._get_next_plot(projection='3d') if is_3d else self._get_next_plot() rand_color = randomcolor.RandomColor() limits = [[100000, -100000], [100000, -100000], [100000, -100000]] marker_id = 0 for data in args: data = list(data) values = data[:-1] label = data[-1] color = rand_color.generate()[0] for i, v in enumerate(values): limits[i][0] = min(limits[i][0], min(v)) limits[i][1] = max(limits[i][1], max(v)) sp.scatter(*values, label=label, color=color, marker=markers[marker_id % len(markers)]) marker_id += 1 if is_3d: x_label, y_label, z_label = axis_labels else: x_label, y_label = axis_labels sp.set_xlim(limits[0]) sp.set_xlabel(x_label) sp.set_ylim(limits[1]) sp.set_ylabel(y_label) if is_3d: sp.set_zlim(limits[2]) sp.set_zlabel(z_label) sp.legend(loc='upper right') sp.set_title(title) return self def create_histograms(self, pd_categoricals, titles, bins='auto', fig_size=(13, 6)): """ Creates a histogram based on x_data """ fig, ax = plt.subplots(figsize=fig_size) self.__figures.append(fig) subplot_no = len(pd_categoricals) ax.axes.get_xaxis().set_visible(False) ax.axes.get_yaxis().set_visible(False) colors = {} for i in range(len(pd_categoricals)): data = pd_categoricals[i] if isinstance(data, pd.core.series.Series): data = data[data.isnull() == False].value_counts() else: data, labels = data class_id, data = np.unique(data, return_counts=True) sort_freq = np.argsort(-data) data = dict(zip([labels[c] for c in class_id[sort_freq]], data[sort_freq])) labels = [name for name in data.keys()] data = [data[name] for name in labels] if len(colors) != len(labels): colors = dict(zip(labels, randomcolor.RandomColor().generate(count=len(labels)))) sp = fig.add_subplot(1, subplot_no, i + 1) plt.bar(labels, data, color=[colors[l] for l in labels]) plt.xticks(labels, rotation=90) sp.set_title(titles[i]) plt.tight_layout() return self def create_images(self, images, titles, **kwargs): """ Creates a grid of images """ subplot_cols = min(len(images), 6) subplot_rows = math.ceil(len(images) / subplot_cols) fig_size = kwargs.pop('figsize', 3) dpi = kwargs.pop('dpi', 80) fig = plt.figure(figsize=(subplot_cols * fig_size, subplot_rows * fig_size), dpi=dpi) self.__figures.append(fig) if len(images) != len(titles): raise(Exception("Image and title list must be the same")) for i in range(len(images)): sp = fig.add_subplot(subplot_rows, subplot_cols, i + 1) sp.set_title(titles[i]) image = images[i] if isinstance(image, str): image = Image.load(image) plt.imshow(Image.to_image(image), **kwargs) sp.axes.get_xaxis().set_visible(False) sp.axes.get_yaxis().set_visible(False) sp.grid(None) plt.tight_layout() return self # https://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html def create_confusion_matrix(self, y_true, y_pred, classes, title=None, x_label='Predicted class', y_label='True class', normalize=False, cmap=plt.cm.Blues): """ This function prints and plots the confusion matrix. Normalization can be applied by setting `normalize=True`. """ fig, ax = plt.subplots(figsize=(8, 8)) self.__figures.append(fig) if not title: if normalize: title = 'Normalized confusion matrix' else: title = 'Confusion matrix, without normalization' # Compute confusion matrix cm = confusion_matrix(y_true, y_pred, labels=np.array(range(len(classes)))) if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] im = ax.imshow(cm, interpolation='nearest', cmap=cmap) # ax.figure.colorbar(im, ax=ax) # We want to show all ticks...and label them with the respective list entries ax.set(xticks=np.arange(cm.shape[1]), yticks=np.arange(cm.shape[0]), xticklabels=classes, yticklabels=classes, title=title, ylabel=y_label, xlabel=x_label) # Rotate the tick labels and set their alignment. plt.setp(ax.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor") ax.spines['bottom'].set_visible(False) ax.spines['top'].set_visible(False) ax.tick_params(axis='both', which='major', pad=10) # Loop over data dimensions and create text annotations. fmt = '.2f' if normalize else 'd' thresh = cm.max() / 2. for i in range(cm.shape[0]): for j in range(cm.shape[1]): ax.text(j, i, format(cm[i, j], fmt), ha="center", va="center", color="green" if i == j else "white" if cm[i, j] > thresh else "black") fig.tight_layout() return self # https://scikit-learn.org/stable/auto_examples/model_selection/plot_roc.html def create_roc_curve_multiclass(self, y_true_labels, y_predicted_scores, classes, plot_mask=False): """ Compute ROC curve and ROC area for each class classes contains a list of target label names in the multiclass clasification plot_mask can contain a list of True/False values for each of the above class to be predicted """ fpr = dict() tpr = dict() roc_auc = dict() if not plot_mask: plot_mask = classes != None for i in range(len(classes)): if plot_mask[i]: fpr[i], tpr[i], _ = roc_curve(y_true_labels, y_predicted_scores[:, i], pos_label=i) roc_auc[i] = auc(fpr[i], tpr[i]) # https://datascience.stackexchange.com/questions/15989/micro-average-vs-macro-average-performance-in-a-multiclass-classification-settin # Compute micro-average ROC curve and ROC area for all classes y_true_micro = np.array([], dtype=np.int32) y_scores_micro = np.array([], dtype=np.float64) for i in range(len(classes)): if plot_mask[i]: y_true_micro = np.append(y_true_micro, y_true_labels == i) y_scores_micro = np.append(y_scores_micro, y_predicted_scores[:, i]) fpr["micro"], tpr["micro"], _ = roc_curve(y_true_micro, y_scores_micro) roc_auc["micro"] = auc(fpr["micro"], tpr["micro"]) self.__plot_roc_curve(fpr, tpr, roc_auc, classes) return self def __plot_roc_curve(self, fpr, tpr, roc_auc, classes): self.__figures.append(plt.figure(figsize=(15, 15))) rand_color = randomcolor.RandomColor() for i, c in enumerate(classes): if i in fpr: plt.plot(fpr[i], tpr[i], label='ROC curve [class=%s] (area = %0.2f)' % (c, roc_auc[i]), color=rand_color.generate()[0], linewidth=2) if 'micro' in fpr: plt.plot(fpr['micro'], tpr['micro'], label='ROC curve Micro Average (area = %0.2f)' % roc_auc['micro'], color='deeppink', linewidth=4, linestyle=':') if 'macro' in fpr: plt.plot(fpr['macro'], tpr['macro'], label='ROC curve Macro Average (area = %0.2f)' % roc_auc['macro'], color='darkorange', linewidth=4, linestyle=':') plt.plot([0, 1], [0, 1], color='navy', linewidth=2, linestyle='--') plt.xlim([0.0, 1.0]) plt.ylim([0.0, 1.05]) plt.xlabel('Recall (False Positive Rate)') plt.ylabel('Precision (True Positive Rate)') plt.title('Receiver operating characteristic') plt.legend(loc="lower right")
36.982507
144
0.578242
12,210
0.962554
0
0
52
0.004099
0
0
2,201
0.173512
ce77b50727f7be773d7ee04df988b1888387d995
478
py
Python
census_data_downloader/tables/medianage.py
JoeGermuska/census-data-downloader
0098b9e522b78ad0e30301c9845ecbcc903c62e4
[ "MIT" ]
170
2019-04-01T01:41:42.000Z
2022-03-25T21:22:06.000Z
census_data_downloader/tables/medianage.py
JoeGermuska/census-data-downloader
0098b9e522b78ad0e30301c9845ecbcc903c62e4
[ "MIT" ]
68
2019-03-31T22:52:43.000Z
2021-08-30T16:33:54.000Z
census_data_downloader/tables/medianage.py
JoeGermuska/census-data-downloader
0098b9e522b78ad0e30301c9845ecbcc903c62e4
[ "MIT" ]
34
2019-04-02T17:57:16.000Z
2022-03-28T17:22:35.000Z
#! /usr/bin/env python # -*- coding: utf-8 -* import collections from census_data_downloader.core.tables import BaseTableConfig from census_data_downloader.core.decorators import register @register class MedianAgeDownloader(BaseTableConfig): PROCESSED_TABLE_NAME = 'medianage' UNIVERSE = "total population" RAW_TABLE_NAME = 'B01002' RAW_FIELD_CROSSWALK = collections.OrderedDict({ "001": "median", "002": "male", "003": "female" })
26.555556
62
0.707113
277
0.579498
0
0
287
0.600418
0
0
118
0.246862
ce78d29afc746e1513a1eb1206ac1f0e6d11d03c
3,791
py
Python
powerfulseal/metriccollectors/prometheus_collector.py
snehalbiche/powerfulseal
4ab70e0db8f33bd390d87e65c662774991483726
[ "Apache-2.0" ]
1
2018-07-12T22:04:51.000Z
2018-07-12T22:04:51.000Z
powerfulseal/metriccollectors/prometheus_collector.py
kz/powerfulseal
24276dd670777a72fed1780539ffe03f3bea63b9
[ "Apache-2.0" ]
null
null
null
powerfulseal/metriccollectors/prometheus_collector.py
kz/powerfulseal
24276dd670777a72fed1780539ffe03f3bea63b9
[ "Apache-2.0" ]
null
null
null
# Copyright 2018 Bloomberg Finance L.P. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from prometheus_client import Counter from powerfulseal.metriccollectors import AbstractCollector from powerfulseal.metriccollectors.collector import NODE_SOURCE, POD_SOURCE STATUS_SUCCESS = 'success' STATUS_FAILURE = 'failure' # Define Prometheus metrics to be stored in the default registry POD_KILLS_METRIC_NAME = 'seal_pod_kills_total' POD_KILLS = Counter(POD_KILLS_METRIC_NAME, 'Number of pods killed (including failures)', ['status', 'namespace', 'name']) NODE_STOPS_METRIC_NAME = 'seal_nodes_stopped_total' NODE_STOPS = Counter(NODE_STOPS_METRIC_NAME, 'Number of nodes stopped (including failures)', ['status', 'uid', 'name']) EXECUTE_FAILED_METRIC_NAME = 'seal_execute_failed_total' EXECUTE_FAILURES = Counter(EXECUTE_FAILED_METRIC_NAME, 'Increasing counter for command execution failures', ['uid', 'name']) FILTERED_TO_EMPTY_SET_METRIC_NAME = 'seal_empty_filter_total' FILTERED_TO_EMPTY_SET = Counter(FILTERED_TO_EMPTY_SET_METRIC_NAME, 'Increasing counter for cases where filtering ' 'returns an empty result') PROBABILITY_FILTER_NOT_PASSED_METRIC_NAME = 'seal_probability_filter_not_passed_total' PROBABILITY_FILTER_NOT_PASSED = Counter(PROBABILITY_FILTER_NOT_PASSED_METRIC_NAME, 'Increasing counter for cases where the' ' probability filter does not pass any ' 'nodes') MATCHED_TO_EMPTY_SET_METRIC_NAME = 'seal_empty_match_total' MATCHED_TO_EMPTY_SET = Counter(MATCHED_TO_EMPTY_SET_METRIC_NAME, 'Increasing counter for cases where matching ' 'returns an empty result', ['source']) class PrometheusCollector(AbstractCollector): def __init__(self): # Export 0 for time series metrics which have labels which can have default # values filled to avoid missing metrics. The Prometheus Python library # already exports 0 for metrics which do not have any labels. MATCHED_TO_EMPTY_SET.labels(NODE_SOURCE).inc(0) MATCHED_TO_EMPTY_SET.labels(POD_SOURCE).inc(0) def add_pod_killed_metric(self, pod): POD_KILLS.labels(STATUS_SUCCESS, pod.namespace, pod.name).inc() def add_pod_kill_failed_metric(self, pod): POD_KILLS.labels(STATUS_FAILURE, pod.namespace, pod.name).inc() def add_node_stopped_metric(self, node): NODE_STOPS.labels(STATUS_SUCCESS, node.uid, node.name).inc() def add_node_stop_failed_metric(self, node): NODE_STOPS.labels(STATUS_FAILURE, node.uid, node.name).inc() def add_execute_failed_metric(self, node): EXECUTE_FAILURES.labels(node.uid, node.name).inc() def add_filtered_to_empty_set_metric(self): FILTERED_TO_EMPTY_SET.inc() def add_probability_filter_passed_no_nodes_filter(self): PROBABILITY_FILTER_NOT_PASSED.inc() def add_matched_to_empty_set_metric(self, source): MATCHED_TO_EMPTY_SET.labels(source).inc()
42.595506
86
0.69665
1,287
0.339488
0
0
0
0
0
0
1,462
0.38565
ce796d88eb98f929fefba1eaa8a093ed6e266e4a
1,285
py
Python
icm/__main__.py
MCasari-PMEL/EDD-ICMGUI
3210e7bb74ff2ace6e1c8c0bf132ecae5713141b
[ "MIT" ]
null
null
null
icm/__main__.py
MCasari-PMEL/EDD-ICMGUI
3210e7bb74ff2ace6e1c8c0bf132ecae5713141b
[ "MIT" ]
3
2018-01-08T16:44:33.000Z
2018-01-08T16:47:55.000Z
icm/__main__.py
MCasari-PMEL/EDD-ICMGUI
3210e7bb74ff2ace6e1c8c0bf132ecae5713141b
[ "MIT" ]
null
null
null
#!/usr/bin/env python # -*- coding: utf-8 -*- import sys, os, time, serial, json import numpy as np import pyqtgraph as pg import pyqtgraph.console from PyQt5.QtCore import pyqtSignal, QObject from pyqtgraph.Qt import QtCore, QtGui from pyqtgraph.dockarea import * from icm.ui_clock import * from icm.ui_createfile import * from icm.ui_parameter import * from icm.ui_serial import * from icm.ui_qicmgui import Ui_QIcmGuiMainWindow class Communicate(QObject): closeApp = pyqtSignal() class QIcmGuiMainWindow(QtGui.QMainWindow, Ui_QIcmGuiMainWindow): """ ICMGUI Main Window """ def __init__(self,parent=None): #Initialize the GUI super().__init__(parent) self.setupUi(self) #Create a communication event to close the window self.c = Communicate() ## Create docks and place them in the window self.cd = SerialWidget(self.gridLayout_2) #@QtCore.slot() def closeEvent(self,event): pass def main(): app = QtGui.QApplication([]) window = QIcmGuiMainWindow() window.show() sys.exit(app.exec_()) if __name__ == "__main__": main() #if __name__ != "__main__": # raise ImportError('this module should not be imported')
22.54386
65
0.662257
569
0.442802
0
0
0
0
0
0
294
0.228794
ce7a9f4356e5b101ec971a15e988ba01f163fc67
1,603
py
Python
tests/unit/plugins/widgets/conftest.py
pauleveritt/kaybee
a00a718aaaa23b2d12db30dfacb6b2b6ec84459c
[ "Apache-2.0" ]
2
2017-11-08T19:55:57.000Z
2018-12-21T12:41:41.000Z
tests/unit/plugins/widgets/conftest.py
pauleveritt/kaybee
a00a718aaaa23b2d12db30dfacb6b2b6ec84459c
[ "Apache-2.0" ]
null
null
null
tests/unit/plugins/widgets/conftest.py
pauleveritt/kaybee
a00a718aaaa23b2d12db30dfacb6b2b6ec84459c
[ "Apache-2.0" ]
1
2018-10-13T08:59:29.000Z
2018-10-13T08:59:29.000Z
import dectate import pytest from kaybee.plugins.widgets.directive import WidgetDirective from kaybee.plugins.widgets.action import WidgetAction class Dummy: pass @pytest.fixture() def widgets_kb_app(): class widgets_kb_app(dectate.App): widget = dectate.directive(WidgetAction) yield widgets_kb_app @pytest.fixture() def dummy_props(): class DummyProps: template = 'foo' label = 'somelabel' yield DummyProps @pytest.fixture() def dummy_widget_class(dummy_props): class DummyWidget: def __init__(self, docname, wtype, yaml_content, rst_content=None): self.docname = docname self.wtype = wtype self.content = yaml_content self.props = dummy_props() self.name = docname + '-somename' yield DummyWidget @pytest.fixture() def dummy_directive_class(): class DummyDirective(WidgetDirective): name = 'dummy_directive' yield DummyDirective @pytest.fixture() def dummy_directive(dummy_directive_class): bd = dummy_directive_class( dummy_directive_class.name, [], dict(), '', 0, 0, '', {}, {}) bd.state = Dummy() bd.state.document = Dummy() bd.state.document.settings = Dummy() bd.state.document.settings.env = Dummy() bd.state.document.settings.env.docname = 'somedoc' bd.state.document.settings.env.widgets = dict() yield bd @pytest.fixture() def widgets_sphinx_app(sphinx_app): sphinx_app.env.widgets = dict() sphinx_app.env.resources = dict() sphinx_app.env.references = dict() yield sphinx_app
22.263889
75
0.679975
531
0.331254
1,307
0.815346
1,415
0.88272
0
0
57
0.035558
ce7bc7b64a4d3cbc613ea8cb55194b8c8ec890ce
1,751
py
Python
eeyore/models/model.py
papamarkou/eeyore
4cd9b5a619cd095035aa93f348d1c937629aa8a3
[ "MIT" ]
6
2020-04-22T18:56:46.000Z
2021-09-09T15:57:48.000Z
eeyore/models/model.py
papamarkou/eeyore
4cd9b5a619cd095035aa93f348d1c937629aa8a3
[ "MIT" ]
19
2019-11-14T21:22:21.000Z
2020-10-31T16:18:36.000Z
eeyore/models/model.py
scidom/eeyore
4cd9b5a619cd095035aa93f348d1c937629aa8a3
[ "MIT" ]
null
null
null
import hashlib import torch import torch.nn as nn class Model(nn.Module): """ Class representing sampleable neural network model """ def __init__(self, dtype=torch.float64, device='cpu'): super().__init__() self.dtype = dtype self.device = device def summary(self, hashsummary=False): print(self) print("-" * 80) n_params = self.num_params() print(f"Number of model parameters: {n_params}") print("-" * 80) if hashsummary: print('Hash Summary:') for idx, hashvalue in enumerate(self.hashsummary()): print(f"{idx}: {hashvalue}") def hashsummary(self): """ Print a model summary - checksums of each layer parameters """ children = list(self.children()) result = [] for child in children: result.extend(hashlib.sha256(x.detach().cpu().numpy().tobytes()).hexdigest() for x in child.parameters()) return result def num_params(self): """ Get the number of model parameters. """ return sum(p.numel() for p in self.parameters()) def get_params(self): return torch.cat([p.view(-1) for p in self.parameters()]) def get_grad(self): return torch.cat([p.grad.view(-1) for p in self.parameters()]) def set_params(self, theta, grad_val=None): """ Set model parameters with theta. """ i = 0 for p in self.parameters(): j = p.numel() p.data = theta[i:i+j].view(p.size()) if p.grad is not None: p.grad.detach_() p.grad.zero_() if grad_val is not None: p.grad = grad_val[i:i+j].view(p.size()) i += j
31.267857
117
0.561965
1,699
0.970303
0
0
0
0
0
0
295
0.168475
ce7c235a673286d6890334627fb2a0108f4ba40f
1,129
py
Python
Curso/Challenges/URI/1827SquareArrayIV.py
DavidBitner/Aprendizado-Python
e1dcf18f9473c697fc2302f34a2d3e025ca6c969
[ "MIT" ]
null
null
null
Curso/Challenges/URI/1827SquareArrayIV.py
DavidBitner/Aprendizado-Python
e1dcf18f9473c697fc2302f34a2d3e025ca6c969
[ "MIT" ]
null
null
null
Curso/Challenges/URI/1827SquareArrayIV.py
DavidBitner/Aprendizado-Python
e1dcf18f9473c697fc2302f34a2d3e025ca6c969
[ "MIT" ]
null
null
null
while True: try: dados = [] matriz = [] n = int(input()) for linha in range(0, n): for coluna in range(0, n): dados.append(0) matriz.append(dados[:]) dados.clear() # Numeros na diagonal for diagonal_principal in range(0, n): matriz[diagonal_principal][diagonal_principal] = 2 for diagonal_secundaria in range(0, n): matriz[diagonal_secundaria][n - 1 - diagonal_secundaria] = 3 # Matriz do numero 1 for linha in range(n // 3, n - n // 3): for coluna in range(n // 3, n - n // 3): matriz[linha][coluna] = 1 # Matriz do numero 4 matriz[n // 2][n // 2] = 4 # Print da Matriz completa for linha in range(0, len(matriz)): for coluna in range(0, len(matriz)): if coluna == 0: print(f"{matriz[linha][coluna]}", end="") else: print(f"{matriz[linha][coluna]}", end="") print() print() except EOFError: break
31.361111
72
0.478299
0
0
0
0
0
0
0
0
143
0.126661
ce7c570a565ac358f3c0cebb92e2e6aa904f3655
17,145
py
Python
HetSANN_MRV/execute_sparse.py
xhhszc/hetsann
432ce7493331cc393ff90af0e03a445e758919ea
[ "Apache-2.0" ]
116
2019-12-10T02:14:37.000Z
2022-02-23T09:22:13.000Z
HetSANN_MRV/execute_sparse.py
xhhszc/hetsann
432ce7493331cc393ff90af0e03a445e758919ea
[ "Apache-2.0" ]
6
2020-01-07T00:04:00.000Z
2021-07-30T17:40:27.000Z
HetSANN_MRV/execute_sparse.py
xhhszc/hetsann
432ce7493331cc393ff90af0e03a445e758919ea
[ "Apache-2.0" ]
35
2019-12-10T02:15:45.000Z
2021-11-15T09:44:31.000Z
import os import time import random import scipy.sparse as sp import numpy as np import tensorflow as tf import argparse from models import SpHGAT from utils import process parser = argparse.ArgumentParser() parser.add_argument('--dataset', help='Dataset.', default='imdb', type=str) parser.add_argument('--epochs', help='Epochs.', default=100000, type=int) parser.add_argument('--patience', help='Patience for early stopping.', default=100, type=int) parser.add_argument('--lr', help='Learning rate.', default=0.005, type=float) parser.add_argument('--l2_coef', help='Weight decay.', default=0.0005, type=float) parser.add_argument('--dropout', help='Dropout.', default=0.6, type=float) parser.add_argument('--train_rate', help='Label rate for training.', default=0.1, type=float) parser.add_argument('--seed', help='Random seed for data splitting.', default=None, type=int) parser.add_argument('--layers', help='Number of layers.', default=2, type=int) parser.add_argument('--hid', help='Number of hidden units per head in each layer.', nargs='*', default=[8, 8], type=int) parser.add_argument('--heads', help='Number of attention heads in each layer.', nargs='*', default=[8, 1], type=int) parser.add_argument('--residue', help='Using residue.', action='store_true') parser.add_argument('--repeat', help='Repeat.', default=10, type=int) parser.add_argument('--random_feature', help='Random features', action='store_true') parser.add_argument('--target_node', help='index of target nodes for classification.', nargs='*', default=[0, 1], type=int) parser.add_argument('--target_is_multilabels', help='each type of target node for classification is multi-labels or not.(0 means not else means yes)', nargs='*', default=[0, 1], type=int) parser.add_argument('--saved_model_suffix', help='to splite checkpoint by suffix', default="", type=str) parser.add_argument('--no_attn_reg', help='Do not use edge direction regularization', action='store_true') parser.add_argument('--simple_inner', help='Use original inner product', action='store_true') parser.add_argument('--loop_coef', help='Coefficient for regularization.', default=1e-3, type=float) parser.add_argument('--inv_coef', help='Coefficient for regularization.', default=1e-3, type=float) config = tf.ConfigProto(allow_soft_placement=True) config.gpu_options.allow_growth = True args= parser.parse_args() dataset = args.dataset checkpt_file = 'pre_trained/{}/{}/{}.ckpt'.format(dataset, args.saved_model_suffix, dataset) checkpt_file = checkpt_file.replace('//', '/') process.mkdir(os.path.split(checkpt_file)[0]) # training params batch_size = 1 train_rate = args.train_rate seed = args.seed nb_epochs = args.epochs patience = args.patience lr = args.lr # learning rate l2_coef = args.l2_coef # weight decay dropout = args.dropout repeat = args.repeat random_feature = args.random_feature target_node = args.target_node is_multilabel = [False if t==0 else True for t in args.target_is_multilabels] loop_coef = args.loop_coef inv_coef = args.inv_coef layers = args.layers hid = args.hid if len(hid) == 1: hid_units = hid * layers elif len(hid) == layers: hid_units = hid heads = args.heads if len(heads) == 1: n_heads = heads * layers elif len(heads) == 2: n_heads = [heads[0]] * (layers - 1) + [heads[1]] elif len(heads) == layers: n_heads = heads residual = args.residue # False nonlinearity = tf.nn.elu model = SpHGAT no_attn_reg = args.no_attn_reg simple_inner = args.simple_inner random.seed(seed) # random seed for random data split only print('Dataset: ' + dataset) print('Train rate: ' + str(train_rate)) print('----- Opt. hyperparams -----') print('lr: ' + str(lr)) print('l2_coef: ' + str(l2_coef)) print('----- Archi. hyperparams -----') print('nb. layers: ' + str(len(hid_units))) print('nb. units per layer: ' + str(hid_units)) print('nb. attention heads: ' + str(n_heads)) print('residual: ' + str(residual)) print('nonlinearity: ' + str(nonlinearity)) print('model: ' + str(model)) print('target nodes: ', target_node) print('is_multilabel: ', is_multilabel) print('loop_coef:', loop_coef) print('inv_coef:', inv_coef) sparse = True metr_num = 2 total_vl_acc = np.array([0.]*(len(target_node)*metr_num)) # should be array total_ts_acc = np.array([0.]*(len(target_node)*metr_num)) # should be array def get_loss_acc(logits, labels, msk, is_multilabel=False): global model class_num = labels.shape[-1] log_resh = tf.reshape(logits, [-1, class_num]) lab_resh = tf.reshape(labels, [-1, class_num]) msk_resh = tf.reshape(msk, [-1]) if is_multilabel: loss = model.masked_sigmoid_cross_entropy(log_resh, lab_resh, msk_resh) accuracy = [model.micro_f1(log_resh, lab_resh, msk_resh), model.macro_f1(log_resh, lab_resh, msk_resh)] acc_name = ['if1', 'af1'] acc_full_name = ['micro f1', 'macro f1'] else: loss = model.masked_softmax_cross_entropy(log_resh, lab_resh, msk_resh) accuracy = [model.micro_f1_onelabel(log_resh, lab_resh, msk_resh), model.macro_f1_onelabel(log_resh, lab_resh, msk_resh)] acc_name = ['if1', 'af1'] acc_full_name = ['micro f1', 'macro f1'] return loss, accuracy, acc_name, acc_full_name def print_eachclass_info(train_loss_each, train_acc_each, val_loss_each, val_acc_each, acc_name): tl_average = np.mean(np.array(train_loss_each), axis=0) ta_average = np.mean(np.array(train_acc_each), axis=0) vl_average = np.mean(np.array(val_loss_each), axis=0) va_average = np.mean(np.array(val_acc_each), axis=0) metric_num = int(len(ta_average)/len(tl_average)) for i in range(len(tl_average)): line = '\t\t target %s: loss = %.3f, ' % (i, tl_average[i]) for j in range(metric_num): line += '%s = %.5f, ' % (acc_name[i*metric_num+j], ta_average[i*metric_num+j]) line += '| Val: loss = %.3f, ' % (vl_average[i]) for j in range(metric_num): line += '%s = %.5f, ' % (acc_name[i*metric_num+j], va_average[i*metric_num+j]) print(line) for repeat_i in range(repeat): print('Run #' + str(repeat_i) + ':') adj, adj_type, edge_list, features, y_train, y_val, y_test,\ train_mask, val_mask, test_mask = process.load_heterogeneous_data(dataset, train_rate=train_rate, target_node=target_node) features = [process.preprocess_features(feature)[0] for feature in features] nb_nodes = [feature.shape[0] for feature in features] ft_size = [feature.shape[1] for feature in features] nb_classes = [y.shape[1] for y in y_train] features = [feature[np.newaxis] for feature in features] y_train = [y[np.newaxis] for y in y_train] y_val = [y[np.newaxis] for y in y_val] y_test = [y[np.newaxis] for y in y_test] train_mask = [m[np.newaxis] for m in train_mask] val_mask = [m[np.newaxis] for m in val_mask] test_mask = [m[np.newaxis] for m in test_mask] if random_feature: features[0] = np.random.standard_normal(features[0].shape) if sparse: biases = [process.preprocess_adj_hete(a) for a in adj] # transposed here else: biases = [] for a in adj: a = a.todense() a = a[np.newaxis] if no_attn_reg: edge_list = [(i,) for i in range(len(adj_type))] if simple_inner: edge_list = [] with tf.Graph().as_default(): with tf.name_scope('input'): ftr_in = [tf.placeholder(dtype=tf.float32, shape=(batch_size, nb, ft)) for nb, ft in zip(nb_nodes, ft_size)] if sparse: bias_in = [tf.sparse_placeholder(dtype=tf.float32) for _ in biases] else: bias_in = None lbl_in = [tf.placeholder(dtype=tf.int32, shape=(batch_size, nb_nodes[target_node[i]], nb_classes[i])) for i in range(len(nb_classes))] msk_in = [tf.placeholder(dtype=tf.int32, shape=(batch_size, nb_nodes[target_node[i]])) for i in range(len(nb_classes))] attn_drop = tf.placeholder(dtype=tf.float32, shape=()) ffd_drop = tf.placeholder(dtype=tf.float32, shape=()) is_train = tf.placeholder(dtype=tf.bool, shape=()) logits = model.inference(ftr_in, nb_classes, nb_nodes, is_train, attn_drop, ffd_drop, target_nodes=target_node, bias_mat=bias_in, adj_type=adj_type, edge_list=edge_list, hid_units=hid_units, n_heads=n_heads, residual=residual, activation=nonlinearity) with tf.name_scope('loss_acc'): loss, accuracy, acc_name, acc_full_name = [], [], [], [] all_class_loss = 0.0 for tn in range(len(target_node)): tn_logits = logits[tn] tn_labels = lbl_in[tn] tn_masks = msk_in[tn] tn_is_multilabel = is_multilabel[tn] tn_loss, tn_accuracy, tn_acc_name, tn_acc_full_name = get_loss_acc(tn_logits, tn_labels, tn_masks, is_multilabel=tn_is_multilabel) loss.append(tn_loss) accuracy.extend(tn_accuracy) acc_name.extend(tn_acc_name) acc_full_name.extend(tn_acc_full_name) all_class_loss += tn_loss loss_loop = tf.add_n(tf.get_collection('loss_loop')) * loop_coef loss_inv= tf.add_n(tf.get_collection('loss_inv')) * inv_coef train_op = model.training(all_class_loss + loss_loop + loss_inv, lr, l2_coef) saver = tf.train.Saver() init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) vlss_mn = np.inf vacc_mx = 0.0 curr_step = 0 with tf.Session(config=config) as sess: sess.run(init_op) vacc_early_model = 0.0 vlss_early_model = 0.0 vacc_each_early_model = np.array([0.]*(len(target_node)*metr_num)) for epoch in range(nb_epochs): # summary information train_loss_avg = 0 train_acc_avg = 0 val_loss_avg = 0 val_acc_avg = 0 # for each class information train_loss_each = [] train_acc_each = [] val_loss_each = [] val_acc_each = [] tr_step = 0 tr_size = features[0].shape[0] while tr_step * batch_size < tr_size: if sparse: fd = {i: d for i, d in zip(ftr_in, features)} fd.update({i: d for i, d in zip(bias_in, biases)}) else: fd = {i: d[tr_step * batch_size:(tr_step + 1) * batch_size] for i, d in zip(ftr_in, features)} fd.update({i: d[tr_step * batch_size:(tr_step + 1) * batch_size] for i, d in zip(bias_in, biases)}) fd.update({i:d[tr_step*batch_size:(tr_step+1)*batch_size] for i, d in zip(lbl_in, y_train)}) fd.update({i:d[tr_step*batch_size:(tr_step+1)*batch_size] for i, d in zip(msk_in, train_mask)}) fd.update({is_train: True}) fd.update({attn_drop: dropout, ffd_drop:dropout}) _, loss_list_tr, acc_list_tr, loss_loop_tr, loss_inv_tr = sess.run([train_op, loss, accuracy, loss_loop, loss_inv], feed_dict=fd) train_loss_each.append(np.array(loss_list_tr)) train_acc_each.append(np.array(acc_list_tr)) train_loss_avg += np.sum(np.array(loss_list_tr)) train_acc_avg += np.sum(np.array(acc_list_tr)) tr_step += 1 vl_step = 0 vl_size = features[0].shape[0] while vl_step * batch_size < vl_size: if sparse: fd = {i: d for i, d in zip(ftr_in, features)} fd.update({i: d for i, d in zip(bias_in, biases)}) else: fd = {i: d[vl_step * batch_size:(vl_step + 1) * batch_size] for i, d in zip(ftr_in, features)} fd.update({i: d[vl_step * batch_size:(vl_step + 1) * batch_size] for i, d in zip(bias_in, biases)}) fd.update({i:d[vl_step*batch_size:(vl_step+1)*batch_size] for i, d in zip(lbl_in, y_val)}) fd.update({i:d[vl_step*batch_size:(vl_step+1)*batch_size] for i, d in zip(msk_in, val_mask)}) fd.update({is_train: False}) fd.update({attn_drop: 0.0, ffd_drop:0.0}) loss_list_vl, acc_list_vl = sess.run([loss, accuracy], feed_dict=fd) acc_list_vl = [0. if np.isnan(acc_vl) else acc_vl for acc_vl in acc_list_vl] val_loss_each.append(np.array(loss_list_vl)) val_acc_each.append(np.array(acc_list_vl)) val_loss_avg += np.sum(np.array(loss_list_vl)) val_acc_avg += np.sum(np.array(acc_list_vl)) vl_step += 1 print('Training %s: loss = %.5f, %s = %.5f, loss_loop = %.5f, loss_inv = %.5f | Val: loss = %.5f, %s = %.5f' % (epoch, train_loss_avg/tr_step, 'acc/F1', train_acc_avg/tr_step, loss_loop_tr, loss_inv_tr, val_loss_avg/vl_step, 'acc/F1', val_acc_avg/vl_step)) print_eachclass_info(train_loss_each, train_acc_each, val_loss_each, val_acc_each, acc_name) if val_acc_avg/vl_step > vacc_mx or val_loss_avg/vl_step < vlss_mn: if val_acc_avg/vl_step > vacc_mx and val_loss_avg/vl_step < vlss_mn: vacc_early_model = val_acc_avg/vl_step vlss_early_model = val_loss_avg/vl_step vacc_each_early_model = np.mean(np.array(val_acc_each), axis=0) saver.save(sess, checkpt_file) print("saved model as %s"%checkpt_file) vacc_mx = np.max((val_acc_avg/vl_step, vacc_mx)) vlss_mn = np.min((val_loss_avg/vl_step, vlss_mn)) curr_step = 0 else: curr_step += 1 if curr_step == patience: print('Early stop! Min loss: ', vlss_mn, ', Max', 'acc/F1', ': ', vacc_mx) print('Early stop model validation loss: ', vlss_early_model, ', ', 'acc/F1', ': ', vacc_early_model) total_vl_acc += vacc_each_early_model break if curr_step < patience: print('Min loss: ', vlss_mn, ', Max', 'acc/F1', ': ', vacc_mx) print('model validation loss: ', vlss_early_model, ', ', 'acc/F1', ': ', vacc_early_model) total_vl_acc += vacc_each_early_model saver.restore(sess, checkpt_file) ts_size = features[0].shape[0] ts_step = 0 test_loss_each = [] test_acc_each = [] while ts_step * batch_size < ts_size: if sparse: fd = {i: d for i, d in zip(ftr_in, features)} fd.update({i: d for i, d in zip(bias_in, biases)}) else: fd = {i: d[ts_step * batch_size:(ts_step + 1) * batch_size] for i, d in zip(ftr_in, features)} fd.update({i: d[ts_step * batch_size:(ts_step + 1) * batch_size] for i, d in zip(bias_in, biases)}) fd.update({i:d[ts_step*batch_size:(ts_step+1)*batch_size] for i, d in zip(lbl_in, y_test)}) fd.update({i:d[ts_step*batch_size:(ts_step+1)*batch_size] for i, d in zip(msk_in, test_mask)}) fd.update({is_train: False}) fd.update({attn_drop: 0.0, ffd_drop:0.0}) loss_list_ts, acc_list_ts = sess.run([loss, accuracy], feed_dict=fd) test_loss_each.append(np.array(loss_list_ts)) test_acc_each.append(np.array(acc_list_ts)) ts_step += 1 test_loss_each = np.mean(np.array(test_loss_each), axis=0) test_acc_each = np.mean(np.array(test_acc_each), axis=0) print('*'*10,'Test information:', '*'*10) for e in range(len(target_node)): print('target %s: loss: %.3f, %s:%.5f, %s:%.5f' % (e, test_loss_each[e], acc_full_name[e*metr_num], test_acc_each[e*metr_num], acc_full_name[e*metr_num+1], test_acc_each[e*metr_num+1])) total_ts_acc += test_acc_each sess.close() print('Validation:', total_vl_acc/repeat, 'Test:', total_ts_acc/repeat)
49.267241
201
0.594984
0
0
0
0
0
0
0
0
2,003
0.116827
ce7c996f310c0d3f46033c26982db618d4c517fe
230
py
Python
pythontutor-ru/02_ifelse/01_minimum.py
ornichola/learning-new
e567218d8887805e38b1361715d5e3bd51a6bcaf
[ "Unlicense" ]
2
2019-05-24T20:10:16.000Z
2020-07-11T06:06:43.000Z
pythontutor-ru/02_ifelse/01_minimum.py
ornichola/learning-new
e567218d8887805e38b1361715d5e3bd51a6bcaf
[ "Unlicense" ]
null
null
null
pythontutor-ru/02_ifelse/01_minimum.py
ornichola/learning-new
e567218d8887805e38b1361715d5e3bd51a6bcaf
[ "Unlicense" ]
21
2019-03-11T20:25:05.000Z
2022-02-28T13:53:10.000Z
''' http://pythontutor.ru/lessons/ifelse/problems/minimum/ Даны два целых числа. Выведите значение наименьшего из них. ''' val_01 = int(input()) val_02 = int(input()) if val_01 > val_02: print(val_02) else: print(val_01)
19.166667
59
0.704348
0
0
0
0
0
0
0
0
171
0.612903
ce7cd3565831e5b22995deb48bc2a2fd08f936c7
1,705
py
Python
src/ui/license.py
Schrut/PRT
09d136cc75ef5e4e79e72ade07c5d64fabd097f2
[ "MIT" ]
2
2018-02-20T11:53:36.000Z
2018-05-12T10:01:27.000Z
src/ui/license.py
Schrut/PRT
09d136cc75ef5e4e79e72ade07c5d64fabd097f2
[ "MIT" ]
null
null
null
src/ui/license.py
Schrut/PRT
09d136cc75ef5e4e79e72ade07c5d64fabd097f2
[ "MIT" ]
null
null
null
from PyQt5.QtWidgets import QWidget, QMessageBox class uiLicenseWindow(QMessageBox): """ The MIT License (MIT) https://mit-license.org/ """ def __init__(self, parent: QWidget): super().__init__(parent) self.parent = parent self.title = "The MIT License (MIT)" #Hardcoded License self.license = "<pre><b>Copyright © 2018 <i>~ Thibault HECKEL, Florian GIMENEZ ~</i></b><br><br>\ Permission is hereby granted, free of charge, to any person obtaining a copy<br>\ of this software and associated documentation files (the “Software”), to deal<br>\ in the Software without restriction, including without limitation the rights<br>\ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell<br>\ copies of the Software, and to permit persons to whom the Software is<br>\ furnished to do so, subject to the following conditions:<br><br>\ The above copyright notice and this permission notice shall be included in all<br>\ copies or substantial portions of the Software.<br><br>\ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>\ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>\ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE<br>\ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>\ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,<br>\ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE<br>\ SOFTWARE.</pre><br>\ Read more at: <a href=\"https://opensource.org/licenses/MIT\">\ https://opensource.org/licenses/MIT</a>" def on_click(self): self.information(self.parent, self.title, self.license, QMessageBox.Ok)
50.147059
100
0.753079
1,664
0.970828
0
0
0
0
0
0
1,405
0.81972
ce7d9b34d754c223a723c3b5526adb48b8a8f699
2,021
py
Python
python/GameFlow/console/Console.py
Silversmithe/Connect4
dfdf89196e2eae6b40d2f637e2a47e03e2447534
[ "Apache-2.0" ]
null
null
null
python/GameFlow/console/Console.py
Silversmithe/Connect4
dfdf89196e2eae6b40d2f637e2a47e03e2447534
[ "Apache-2.0" ]
null
null
null
python/GameFlow/console/Console.py
Silversmithe/Connect4
dfdf89196e2eae6b40d2f637e2a47e03e2447534
[ "Apache-2.0" ]
null
null
null
import sys import os class Console(object): """ Class responsible for handling input from the user """ def __init__(self): self.log_file = "log.txt" # initialize log with open(self.log_file, 'a') as file: file.write("** NEW LOG CREATED **\n") @staticmethod def read_for_condition(prompt, condition): """ Continues to prompt user for input until the condition is fulfilled :param prompt: prompt for the input :param condition: the function that checks condition of input :return: value """ user_input = None while user_input is None: user_input = condition(input(prompt)) return user_input @staticmethod def yes_or_no(prompt): """ Primpt the user to say yes or no @:param: prompt :return: true of yes, false if no """ user_input = None while user_input is None: user_input = input("{}: yes/no >>> ".format(prompt)) if user_input.lower() == 'yes' or user_input.lower() == 'y': return True elif user_input.lower() == 'no' or user_input.lower() == 'n': return False else: user_input = None @staticmethod def press_enter_to_continue(): """ Wait until someone presses enter :return: """ user_input = input("Press Enter To Continue...") def log(self, msg): """ Log information :param msg: :return: """ with open(self.log_file, 'a') as file: file.write(msg) @staticmethod def clear_console(): """ clear console :return: """ # not covering cygwin if sys.platform == "darwin": os.system("clear") elif sys.platform == "linux": os.system("clear") else: os.system("cls")
25.2625
73
0.521029
1,996
0.98763
0
0
1,510
0.747155
0
0
818
0.40475
ce7dcfa1ba0e4b637228e061f83bafab463cb61b
766
py
Python
servoblst.py
ForToffee/MeArm
90fdd94fd96b53b3579c6d8132e8586188e3d344
[ "MIT" ]
1
2016-04-04T17:39:54.000Z
2016-04-04T17:39:54.000Z
servoblst.py
ForToffee/MeArm
90fdd94fd96b53b3579c6d8132e8586188e3d344
[ "MIT" ]
null
null
null
servoblst.py
ForToffee/MeArm
90fdd94fd96b53b3579c6d8132e8586188e3d344
[ "MIT" ]
null
null
null
import time import os servos = {} class ServoController: def __init__(self): os.system('sudo /home/pi/PiBits/ServoBlaster/user/servod') def setAngle(self, servo_id, degrees): if degrees > 90: degrees = 90 elif degrees < -90: degrees = -90 #http://www.raspberrypi.org/forums/viewtopic.php?f=44&t=36572 pulse = 1520 + (degrees * 400) / 45 os.system("echo %d=%d > /dev/servoblaster" % (servo_id, pulse/10)) time.sleep(0.1) servos[servo_id] = degrees #print "angle=%s pulse=%s" % (degrees, pulse) return servos[servo_id] def incAngle(self, servo_id, increment): angle = servos.get(servo_id, 0) return self.setAngle(servo_id, angle + increment) def clean_up(self): print "cleaning up" os.system('sudo killall servod')
22.529412
68
0.680157
723
0.943864
0
0
0
0
0
0
219
0.285901
ce801d6bd90e41604f5f09f5bc95fde822da704c
728
py
Python
TASQ/problems/forms.py
harshraj22/smallProjects
b31e9173c60abb778a1c196609757704ec9c3750
[ "MIT" ]
2
2019-11-18T14:13:57.000Z
2020-11-08T06:50:32.000Z
TASQ/problems/forms.py
harshraj22/smallProjects
b31e9173c60abb778a1c196609757704ec9c3750
[ "MIT" ]
16
2019-11-12T13:08:01.000Z
2022-02-27T10:51:28.000Z
TASQ/problems/forms.py
harshraj22/smallProjects
b31e9173c60abb778a1c196609757704ec9c3750
[ "MIT" ]
null
null
null
from django import forms from .models import Problem class ProblemForm(forms.ModelForm): options = ( ('A', 'A'), ('B', 'B'), ('C', 'C'), ('D', 'D'), ) choice = forms.ChoiceField(choices=options) class Meta: model = Problem fields = '__all__' # exclude = ['answer'] widgets = { 'answer': forms.HiddenInput(), 'description': forms.Textarea(attrs={'readonly':'readonly', 'style': 'border: None'}), 'opt1': forms.TextInput(attrs={'readonly':'readonly'}), 'opt2': forms.TextInput(attrs={'readonly':'readonly'}), 'opt3': forms.TextInput(attrs={'readonly':'readonly'}), 'opt4': forms.TextInput(attrs={'readonly':'readonly'}), 'difficulty': forms.TextInput(attrs={'readonly':'readonly'}), }
28
89
0.634615
672
0.923077
0
0
0
0
0
0
253
0.347527
ce80328129a238de658580aec1efceb41862bd9d
2,404
py
Python
recipes/make_spreadsheet_with_named_ranges.py
mat-m/odfdo
a4a509a056517ecf91449e029b36fe9a8ffa8ed0
[ "Apache-2.0" ]
null
null
null
recipes/make_spreadsheet_with_named_ranges.py
mat-m/odfdo
a4a509a056517ecf91449e029b36fe9a8ffa8ed0
[ "Apache-2.0" ]
null
null
null
recipes/make_spreadsheet_with_named_ranges.py
mat-m/odfdo
a4a509a056517ecf91449e029b36fe9a8ffa8ed0
[ "Apache-2.0" ]
null
null
null
#!/usr/bin/env python """ Create a spreadsheet with two tables, using some named ranges. """ import os from odfdo import Document, Table if __name__ == "__main__": document = Document('spreadsheet') body = document.body table = Table("First Table") body.append(table) # populate the table : for i in range(10): table.set_value((1, i), (i + 1)**2) table.set_value("A11", "Total:") # lets define a named range for the 10 values : crange = "B1:B10" name = "squares_values" table_name = table.name table.set_named_range(name, crange, table_name) # we can define a single cell range, using notation "B11" or (1, 10) : table.set_named_range('total', (1, 10), table_name) # get named range values : values = table.get_named_range('squares_values').get_values(flat=True) # set named range value : result = sum(values) table.get_named_range('total').set_value(result) # lets use the named ranges from a second table : table2 = Table("Second Table") body.append(table2) named_range1 = table2.get_named_range('total') table2.set_value('A1', "name:") table2.set_value('B1', named_range1.name) table2.set_value('A2', "range:") table2.set_value('B2', str(named_range1.crange)) table2.set_value('A3', "from table:") table2.set_value('B3', named_range1.table_name) table2.set_value('A4', "content:") table2.set_value('B4', named_range1.get_value()) named_range2 = table2.get_named_range('squares_values') table2.set_value('D1', "name:") table2.set_value('E1', named_range2.name) table2.set_value('D2', "range:") table2.set_value('E2', str(named_range2.crange)) table2.set_value('D3', "from table:") table2.set_value('E3', named_range2.table_name) table2.set_value('D4', "content:") # using "E4:4" notaion is a little hack for the area starting at E4 on row 4 table2.set_values( values=[named_range2.get_values(flat=True)], coord='E4:4') print("Content of the table1:") print(table.name) print(table.to_csv()) print(table2.name) print(table2.to_csv()) # of course named ranges are stored in the document : if not os.path.exists('test_output'): os.mkdir('test_output') output = os.path.join('test_output', "my_spreadsheet_with_named_range.ods") document.save(target=output, pretty=True)
33.388889
80
0.671381
0
0
0
0
0
0
0
0
841
0.349834
ce80fb5cf7cc8de8662abff81c0c30df6c3722fb
91
py
Python
gmailbox/apps.py
megatron0000/ces22-xadrez-web
10032f81952e1f816925f5af9c27f235dfbf360c
[ "MIT" ]
null
null
null
gmailbox/apps.py
megatron0000/ces22-xadrez-web
10032f81952e1f816925f5af9c27f235dfbf360c
[ "MIT" ]
8
2020-02-11T22:28:16.000Z
2021-06-10T20:16:42.000Z
gmailbox/apps.py
megatron0000/ces22-xadrez-web
10032f81952e1f816925f5af9c27f235dfbf360c
[ "MIT" ]
null
null
null
from django.apps import AppConfig class GmailBoxConfig(AppConfig): name = 'gmailbox'
15.166667
33
0.758242
54
0.593407
0
0
0
0
0
0
10
0.10989
ce815d6c526703afd41758b750594101532e6d14
724
py
Python
warmup/sock_merchant.py
franloza/hackerrank
e66f5f5c4c1c7c0fe93146d29140692cd71625b7
[ "MIT" ]
null
null
null
warmup/sock_merchant.py
franloza/hackerrank
e66f5f5c4c1c7c0fe93146d29140692cd71625b7
[ "MIT" ]
null
null
null
warmup/sock_merchant.py
franloza/hackerrank
e66f5f5c4c1c7c0fe93146d29140692cd71625b7
[ "MIT" ]
null
null
null
#!/bin/python3 import math import os import random import re import sys # Complete the sockMerchant function below. def sockMerchant(n, ar): socks = {} for elem in ar: if elem in socks: socks[elem] += 1 else: socks[elem] = 1 return sum(elem // 2 for elem in socks.values()) # Read from input # if __name__ == '__main__': # fptr = open(os.environ['OUTPUT_PATH'], 'w') # # n = int(input()) # # ar = list(map(int, input().rstrip().split())) # # result = sockMerchant(n, ar) # # fptr.write(str(result) + '\n') # # fptr.close() # Toy case if __name__ == '__main__': n, ar = 9, [10, 20, 20, 10, 10, 30, 50, 10, 20] print(sockMerchant(n, ar))
19.567568
52
0.56768
0
0
0
0
0
0
0
0
337
0.46547
ce8168357ebbc2da0dca74cb00c849cf14fe0b44
5,468
py
Python
sentiment/twitter/2stepTestValidation.py
ekedziora/twinty
5f0ba01e9e1b93f686569ebd907517a4f41d5cf7
[ "Apache-2.0" ]
null
null
null
sentiment/twitter/2stepTestValidation.py
ekedziora/twinty
5f0ba01e9e1b93f686569ebd907517a4f41d5cf7
[ "Apache-2.0" ]
null
null
null
sentiment/twitter/2stepTestValidation.py
ekedziora/twinty
5f0ba01e9e1b93f686569ebd907517a4f41d5cf7
[ "Apache-2.0" ]
null
null
null
from nltk.corpus.reader import CategorizedPlaintextCorpusReader from nltk.tokenize.casual import TweetTokenizer from nltk.classify.scikitlearn import SklearnClassifier from sklearn.naive_bayes import BernoulliNB, MultinomialNB from sklearn.svm import SVC, LinearSVC, NuSVC, LinearSVR, NuSVR from sklearn.linear_model import LogisticRegression, LinearRegression, Perceptron from featureExtractors import unigramsFeatures, bigramsFeatures, mpqaSubjectivityWordsCountFeatures, \ extraTwitterFeaturesCount, mpqaSentimentWordsCountFeatures from lexicons.mpqa.mpqaDictionary import MpqaDictionaryWrapper from normalization import normalizeTwitterWordsWithNegationHandle, normalizeTwitterWordsWithExtraFeatures from utils import precision_recall_2step import nltk, pickle import csv from itertools import product tweetTokenizer = TweetTokenizer(reduce_len=True, preserve_case=True, strip_handles=False) testcorpus = CategorizedPlaintextCorpusReader('corpus/standford/test', r'(pos|neg|neu)-tweet[0-9]+\.txt', cat_pattern=r'(\w+)-tweet[0-9]+\.txt', word_tokenizer=tweetTokenizer) def performTestValidation(testset, polarClassifierName, sentiClassifierName): with open(polarClassifierName, 'rb') as fileout: polarClassifier = pickle.load(fileout) with open(sentiClassifierName, 'rb') as fileout: sentiClassifier = pickle.load(fileout) labels = ['pos', 'neg', 'neu'] precisions, recalls, fscores, accuracy = precision_recall_2step(polarClassifier, sentiClassifier, testset, labels) print("Test accuracy: {0:.3f}".format(accuracy)) measures = {label: (precision, recall, fscore) for label, precision, recall, fscore in zip(labels, precisions, recalls, fscores)} for label, (prec, recall, fscore) in measures.items(): print("Precision for {0}: {1:.3f}".format(label, prec)) print("Recall for {0}: {1:.3f}".format(label, recall)) print("F measure for {0}: {1:.3f}".format(label, fscore)) return fscores, accuracy def getfeaturesTest(normalizedWords, extraNormalizedWords): features = {} wordsTagged = nltk.pos_tag(normalizedWords) features.update(unigramsFeatures(normalizedWords)) features.update(bigramsFeatures(normalizedWords)) features.update(mpqaSentimentWordsCountFeatures(wordsTagged, mpqaDictionary)) # features.update(mpqaSubjectivityWordsCountFeatures(wordsTagged, mpqaDictionary)) features.update(extraTwitterFeaturesCount(extraNormalizedWords)) return features mpqaDictionary = MpqaDictionaryWrapper() normalizationFunction = normalizeTwitterWordsWithNegationHandle testfeatureset = [] with open("dumps/2step/polar/logreg/70pct/uni-bi-extra-mpqa-senti", 'rb') as fileout: polarClassifier = pickle.load(fileout) with open("dumps/2step/sentiment/logreg/80pct/uni-bi-extra-mpqa-senti", 'rb') as fileout: sentiClassifier = pickle.load(fileout) for category in testcorpus.categories(): for fileid in testcorpus.fileids(category): words = testcorpus.words(fileids=[fileid]) normalizedWords = normalizationFunction(words) extraNormalizedWords = normalizeTwitterWordsWithExtraFeatures(words) testfeatures = getfeaturesTest(normalizedWords, extraNormalizedWords=extraNormalizedWords) dec = polarClassifier.classify(testfeatures) if dec == 'polar': observed = sentiClassifier.classify(testfeatures) else: observed = 'neu' real = testcorpus.categories(fileids=[fileid]) if real[0] != observed: print(testcorpus.raw(fileids=[fileid])) print("REAL: {}".format(real)) print("PREDICTED: {}".format(observed)) # performTestValidation(testfeatureset, "dumps/2step/polar/multiNB/uni-bi-extra-mpqa-subj", "dumps/2step/sentiment/multiNB/uni-bi-extra-mpqa-subj") # performTestValidation(testfeatureset, "dumps/2step/polar/multiNB/uni-bi-extra-mpqa-subj", "dumps/2step/sentiment/logreg/uni-bi-extra-mpqa-subj") # performTestValidation(testfeatureset, "dumps/2step/polar/logreg/uni-bi-extra-mpqa-subj", "dumps/2step/sentiment/multiNB/uni-bi-extra-mpqa-subj") # performTestValidation(testfeatureset, "dumps/2step/polar/logreg/uni-bi-extra-mpqa-subj", "dumps/2step/sentiment/logreg/uni-bi-extra-mpqa-subj") # with open("results2.csv", 'w') as csvfile: # csvwriter = csv.writer(csvfile) # csvwriter.writerow(('features', 'polar classifier', 'sentiment classifier', 'test accuracy', 'f score for pos', 'f score for neg', 'f score for neu')) # # polarDir = "dumps/2step/polar/" # sentimentDir = "dumps/2step/sentiment" # versionsSet = ['multiNB/60pct', 'multiNB/70pct', 'multiNB/80pct', 'logreg/60pct', 'logreg/70pct', 'logreg/80pct'] # for featuresVersion in ['uni-bi-extra-mpqa-senti']: # # tuples = product(versionsSet, versionsSet) # for tuple in tuples: # # print("CLASSIFIERS:") # # print("Polar: " + tuple[0]) # # print("Sentiment: " + tuple[1]) # polarClassifierPath = polarDir + '/' + tuple[0] + '/' + featuresVersion # sentimentClassifierPath = sentimentDir + '/' + tuple[1] + '/' + featuresVersion # fscores, accuracy = performTestValidation(testfeatureset, polarClassifierPath, sentimentClassifierPath) # csvwriter.writerow((featuresVersion, tuple[0], tuple[1], accuracy, fscores[0], fscores[1], fscores[2])) # # print("\n\n")
53.087379
175
0.72586
0
0
0
0
0
0
0
0
2,182
0.399049
ce81ee19f31ff5409d3bf88154d88299544b821e
10,712
py
Python
mango/group.py
mschneider/mango-explorer
ed50880ef80b31b679c9c89fa9bf0579391d71c9
[ "MIT" ]
null
null
null
mango/group.py
mschneider/mango-explorer
ed50880ef80b31b679c9c89fa9bf0579391d71c9
[ "MIT" ]
null
null
null
mango/group.py
mschneider/mango-explorer
ed50880ef80b31b679c9c89fa9bf0579391d71c9
[ "MIT" ]
1
2021-09-02T17:06:09.000Z
2021-09-02T17:06:09.000Z
# # ⚠ Warning # # 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. # # [🥭 Mango Markets](https://mango.markets/) support is available at: # [Docs](https://docs.mango.markets/) # [Discord](https://discord.gg/67jySBhxrg) # [Twitter](https://twitter.com/mangomarkets) # [Github](https://github.com/blockworks-foundation) # [Email](mailto:hello@blockworks.foundation) import construct import time import typing from decimal import Decimal from solana.publickey import PublicKey from .accountinfo import AccountInfo from .addressableaccount import AddressableAccount from .aggregator import Aggregator from .baskettoken import BasketToken from .context import Context from .index import Index from .layouts import layouts from .mangoaccountflags import MangoAccountFlags from .marketmetadata import MarketMetadata from .market import MarketLookup from .token import SolToken, Token, TokenLookup from .tokenvalue import TokenValue from .version import Version # # 🥭 Group class # # The `Group` class encapsulates the data for the Mango Group - the cross-margined basket # of tokens with lending. class Group(AddressableAccount): def __init__(self, account_info: AccountInfo, version: Version, name: str, account_flags: MangoAccountFlags, basket_tokens: typing.List[BasketToken], markets: typing.List[MarketMetadata], signer_nonce: Decimal, signer_key: PublicKey, dex_program_id: PublicKey, total_deposits: typing.List[TokenValue], total_borrows: typing.List[TokenValue], maint_coll_ratio: Decimal, init_coll_ratio: Decimal, srm_vault: PublicKey, admin: PublicKey, borrow_limits: typing.List[TokenValue]): super().__init__(account_info) self.version: Version = version self.name: str = name self.account_flags: MangoAccountFlags = account_flags self.basket_tokens: typing.List[BasketToken] = basket_tokens self.markets: typing.List[MarketMetadata] = markets self.signer_nonce: Decimal = signer_nonce self.signer_key: PublicKey = signer_key self.dex_program_id: PublicKey = dex_program_id self.total_deposits: typing.List[TokenValue] = total_deposits self.total_borrows: typing.List[TokenValue] = total_borrows self.maint_coll_ratio: Decimal = maint_coll_ratio self.init_coll_ratio: Decimal = init_coll_ratio self.srm_vault: PublicKey = srm_vault self.admin: PublicKey = admin self.borrow_limits: typing.List[TokenValue] = borrow_limits @property def shared_quote_token(self) -> BasketToken: return self.basket_tokens[-1] @property def base_tokens(self) -> typing.List[BasketToken]: return self.basket_tokens[:-1] # When loading from a layout, we ignore mint_decimals. In this Discord from Daffy: # https://discord.com/channels/791995070613159966/818978757648842782/851481660049850388 # he says it's: # > same as what is stored on on chain Mint # > Cached on the group so we don't have to pass in the mint every time # # Since we already have that value from our `Token` we don't need to use it and we can # stick with passing around `Token` objects. # @staticmethod def from_layout(layout: construct.Struct, name: str, account_info: AccountInfo, version: Version, token_lookup: TokenLookup, market_lookup: MarketLookup) -> "Group": account_flags: MangoAccountFlags = MangoAccountFlags.from_layout(layout.account_flags) basket_tokens: typing.List[BasketToken] = [] total_deposits: typing.List[TokenValue] = [] total_borrows: typing.List[TokenValue] = [] borrow_limits: typing.List[TokenValue] = [] for index, token_address in enumerate(layout.tokens): static_token_data = token_lookup.find_by_mint(token_address) if static_token_data is None: raise Exception(f"Could not find token with mint '{token_address}'.") # We create a new Token object here specifically to force the use of our own decimals token = Token(static_token_data.symbol, static_token_data.name, token_address, layout.mint_decimals[index]) token_index = Index.from_layout(layout.indexes[index], token) basket_token = BasketToken(token, layout.vaults[index], token_index) basket_tokens += [basket_token] total_deposits += [TokenValue(token, token.shift_to_decimals(layout.total_deposits[index]))] total_borrows += [TokenValue(token, token.shift_to_decimals(layout.total_borrows[index]))] borrow_limits += [TokenValue(token, token.shift_to_decimals(layout.borrow_limits[index]))] markets: typing.List[MarketMetadata] = [] for index, market_address in enumerate(layout.spot_markets): spot_market = market_lookup.find_by_address(market_address) if spot_market is None: raise Exception(f"Could not find spot market with address '{market_address}'.") base_token = BasketToken.find_by_mint(basket_tokens, spot_market.base.mint) quote_token = BasketToken.find_by_mint(basket_tokens, spot_market.quote.mint) market = MarketMetadata(spot_market.symbol, market_address, base_token, quote_token, spot_market, layout.oracles[index], layout.oracle_decimals[index]) markets += [market] maint_coll_ratio = layout.maint_coll_ratio.quantize(Decimal('.01')) init_coll_ratio = layout.init_coll_ratio.quantize(Decimal('.01')) return Group(account_info, version, name, account_flags, basket_tokens, markets, layout.signer_nonce, layout.signer_key, layout.dex_program_id, total_deposits, total_borrows, maint_coll_ratio, init_coll_ratio, layout.srm_vault, layout.admin, borrow_limits) @staticmethod def parse(context: Context, account_info: AccountInfo) -> "Group": data = account_info.data if len(data) == layouts.GROUP_V1.sizeof(): layout = layouts.GROUP_V1.parse(data) version: Version = Version.V1 elif len(data) == layouts.GROUP_V2.sizeof(): version = Version.V2 layout = layouts.GROUP_V2.parse(data) else: raise Exception( f"Group data length ({len(data)}) does not match expected size ({layouts.GROUP_V1.sizeof()} or {layouts.GROUP_V2.sizeof()})") return Group.from_layout(layout, context.group_name, account_info, version, context.token_lookup, context.market_lookup) @staticmethod def load(context: Context): account_info = AccountInfo.load(context, context.group_id) if account_info is None: raise Exception(f"Group account not found at address '{context.group_id}'") return Group.parse(context, account_info) def price_index_of_token(self, token: Token) -> int: for index, existing in enumerate(self.basket_tokens): if existing.token == token: return index return -1 def fetch_token_prices(self, context: Context) -> typing.List[TokenValue]: started_at = time.time() # Note: we can just load the oracle data in a simpler way, with: # oracles = map(lambda market: Aggregator.load(context, market.oracle), self.markets) # but that makes a network request for every oracle. We can reduce that to just one request # if we use AccountInfo.load_multiple() and parse the data ourselves. # # This seems to halve the time this function takes. oracle_addresses = list([market.oracle for market in self.markets]) oracle_account_infos = AccountInfo.load_multiple(context, oracle_addresses) oracles = map(lambda oracle_account_info: Aggregator.parse(context, oracle_account_info), oracle_account_infos) prices = list(map(lambda oracle: oracle.price, oracles)) + [Decimal(1)] token_prices = [] for index, price in enumerate(prices): token_prices += [TokenValue(self.basket_tokens[index].token, price)] time_taken = time.time() - started_at self.logger.info(f"Fetching prices complete. Time taken: {time_taken:.2f} seconds.") return token_prices @staticmethod def load_with_prices(context: Context) -> typing.Tuple["Group", typing.List[TokenValue]]: group = Group.load(context) prices = group.fetch_token_prices(context) return group, prices def fetch_balances(self, context: Context, root_address: PublicKey) -> typing.List[TokenValue]: balances: typing.List[TokenValue] = [] sol_balance = context.fetch_sol_balance(root_address) balances += [TokenValue(SolToken, sol_balance)] for basket_token in self.basket_tokens: balance = TokenValue.fetch_total_value(context, root_address, basket_token.token) balances += [balance] return balances def __str__(self) -> str: total_deposits = "\n ".join(map(str, self.total_deposits)) total_borrows = "\n ".join(map(str, self.total_borrows)) borrow_limits = "\n ".join(map(str, self.borrow_limits)) shared_quote_token = str(self.shared_quote_token).replace("\n", "\n ") base_tokens = "\n ".join([f"{tok}".replace("\n", "\n ") for tok in self.base_tokens]) markets = "\n ".join([f"{mkt}".replace("\n", "\n ") for mkt in self.markets]) return f""" « Group [{self.version} - {self.name}] {self.address}: Flags: {self.account_flags} Base Tokens: {base_tokens} Quote Token: {shared_quote_token} Markets: {markets} DEX Program ID: « {self.dex_program_id} » SRM Vault: « {self.srm_vault} » Admin: « {self.admin} » Signer Nonce: {self.signer_nonce} Signer Key: « {self.signer_key} » Initial Collateral Ratio: {self.init_coll_ratio} Maintenance Collateral Ratio: {self.maint_coll_ratio} Total Deposits: {total_deposits} Total Borrows: {total_borrows} Borrow Limits: {borrow_limits} » """
47.821429
169
0.684279
9,195
0.856943
0
0
4,113
0.383318
0
0
2,968
0.276608
ce8209792059f11dd2ce00622a3448e62cd81437
639
py
Python
office_test_interface/hede_interface_test/encryption/md5_hede.py
yag8009/office_test_team
edf06f3c0818b08ec39541bdcd04bcc537fc9ed1
[ "MIT" ]
null
null
null
office_test_interface/hede_interface_test/encryption/md5_hede.py
yag8009/office_test_team
edf06f3c0818b08ec39541bdcd04bcc537fc9ed1
[ "MIT" ]
null
null
null
office_test_interface/hede_interface_test/encryption/md5_hede.py
yag8009/office_test_team
edf06f3c0818b08ec39541bdcd04bcc537fc9ed1
[ "MIT" ]
null
null
null
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Author : yag8009 # @FileName : md5_hede # @Time : 2020/3/18 import hashlib import time def md5_hede(md5data): md5 = hashlib.md5() # 使用MD5加密模式 md5.update(md5data.encode("utf8")) # 将参数字符串传入 sign = md5.hexdigest() return sign if __name__ == '__main__': data = "brand=华为&channelType=227&city=北京&color=白&imei=123456789012345&model=mate9&name=张三&phone=17066668888&price=3999&saleman=售货员A&store=旗舰店&timestamp=1496566760204&key=b0a44d2ac9bb4196b8977360554f91bb" md = md5_hede(data).upper() print(md) timestamp = int(time.time()*1000) print(timestamp)
27.782609
207
0.693271
0
0
0
0
0
0
0
0
396
0.571429
ce827ac31d171b94c54c1e52af75d3c49aac0651
2,270
py
Python
src/tables.py
tallywiesenberg/dating-data-dividend
0a4f3d7784e9902631a79dce1efb12fc39e74963
[ "MIT" ]
null
null
null
src/tables.py
tallywiesenberg/dating-data-dividend
0a4f3d7784e9902631a79dce1efb12fc39e74963
[ "MIT" ]
null
null
null
src/tables.py
tallywiesenberg/dating-data-dividend
0a4f3d7784e9902631a79dce1efb12fc39e74963
[ "MIT" ]
null
null
null
from flask_login import UserMixin from werkzeug.security import generate_password_hash, check_password_hash from .extensions import db class User(UserMixin, db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True, nullable=False) password = db.Column(db.String(200), nullable=False) address = db.Column(db.String(44), unique=True, nullable=False) left_swipes_given = db.Column(db.Integer, nullable=False) right_swipes_given = db.Column(db.Integer, nullable=False) matches = db.Column(db.Integer, nullable=False) bio = db.Column(db.String(240)) time_logged = db.Column(db.Float, nullable=False) gender = db.Column(db.String(10), nullable=False) gender_preference = db.Column(db.String(10), nullable=False) def __repr__(self): return f'{self.id} -- {self.username} -- {self.password} -- {self.address}' def set_password(self, password): '''Create hashed password''' self.password = generate_password_hash(password, method='sha256') def check_password(self, password): return check_password_hash(self.password, password) def set_address(self, address): '''Set address from placeholder to real-deal after connecting with metamask''' self.address = address # class UserData(db.Model): # id = db.Column(db.Integer, primary_key=True) # left_swipes_given = db.Column(db.Integer, nullable=False) # right_swipes_given = db.Column(db.Integer, nullable=False) # matches = db.Column(db.Integer, nullable=False) # bio = db.Column(db.String(240)) # path_to_photos = db.Column(db.String(20), nullable=False) # user_id = db.Column(db.String(44), # # db.ForeignKey('user_login.id'), # nullable=False) class Swipe(db.Model): id = db.Column(db.Integer, primary_key=True) timestamp = db.Column(db.DateTime, nullable=False) decision = db.Column(db.Boolean, nullable=False) #The user that swipes front_user = db.Column(db.String(44), # db.ForeignKey('user_login.id'), nullable=False) #The user that is swiped upon back_user = db.Column(db.String(44), # db.ForeignKey('user_login.id'), nullable=False) match = db.Column(db.Boolean, nullable=False)
40.535714
84
0.697797
1,660
0.731278
0
0
0
0
0
0
760
0.334802
ce849a188316eb44b68f6012ce73ef84e1631ac2
9,586
py
Python
satyr/proxies/scheduler.py
usheth/satyr
01fadbe2f9c294b9a9719a85d5bd032925453ee6
[ "Apache-2.0" ]
null
null
null
satyr/proxies/scheduler.py
usheth/satyr
01fadbe2f9c294b9a9719a85d5bd032925453ee6
[ "Apache-2.0" ]
null
null
null
satyr/proxies/scheduler.py
usheth/satyr
01fadbe2f9c294b9a9719a85d5bd032925453ee6
[ "Apache-2.0" ]
1
2018-10-10T18:57:54.000Z
2018-10-10T18:57:54.000Z
from __future__ import absolute_import, division, print_function import logging import sys from mesos.interface import Scheduler from .messages import Filters, decode, encode class SchedulerProxy(Scheduler): def __init__(self, scheduler): self.scheduler = scheduler def registered(self, driver, frameworkId, masterInfo): logging.info("Registered with master", extra=dict()) return self.scheduler.on_registered(SchedulerDriverProxy(driver), decode(frameworkId), decode(masterInfo)) def reregistered(self, driver, masterInfo): logging.info("Re-registered with master", extra=dict()) return self.scheduler.on_reregistered(SchedulerDriverProxy(driver), decode(masterInfo)) def disconnected(self, driver): logging.debug("Disconnected from master") return self.scheduler.on_disconnected(SchedulerDriverProxy(driver)) def resourceOffers(self, driver, offers): logging.debug("Got resource offers", extra=dict(num_offers=len(offers))) return self.scheduler.on_offers(SchedulerDriverProxy(driver), map(decode, offers)) def offerRescinded(self, driver, offerId): logging.debug('Offer rescinded', extra=dict(offer_id=offerId)) return self.scheduler.on_rescinded(SchedulerDriverProxy(driver), decode(offerId)) def statusUpdate(self, driver, status): logging.debug('Status update received', extra=dict( state=status.state, description=status.message)) return self.scheduler.on_update(SchedulerDriverProxy(driver), decode(status)) def frameworkMessage(self, driver, executorId, slaveId, message): logging.debug('Framework message received') return self.scheduler.on_message(SchedulerDriverProxy(driver), decode(executorId), decode(slaveId), message) def slaveLost(self, driver, slaveId): logging.debug('Slave has been lost, tasks should be rescheduled') return self.scheduler.on_slave_lost(SchedulerDriverProxy(driver), decode(slaveId)) def executorLost(self, driver, executorId, slaveId, status): logging.debug('Executor has been lost') return self.scheduler.on_executor_lost(SchedulerDriverProxy(driver), decode(executorId), decode(slaveId), status) def error(self, driver, message): print("Error from Mesos: %s " % message, file=sys.stderr) return self.scheduler.on_error(SchedulerDriverProxy(driver), message) class SchedulerDriverProxy(object): """Proxy Interface for Mesos scheduler drivers.""" def __init__(self, driver): self.driver = driver def start(self): """Starts the scheduler driver. This needs to be called before any other driver calls are made. """ return self.driver.start() def stop(self, failover=False): """Stops the scheduler driver. If the 'failover' flag is set to False then it is expected that this framework will never reconnect to Mesos and all of its executors and tasks can be terminated. Otherwise, all executors and tasks will remain running (for some framework specific failover timeout) allowing the scheduler to reconnect (possibly in the same process, or from a different process, for example, on a different machine.) """ return self.driver.stop(failover) def abort(self): """Aborts the driver so that no more callbacks can be made to the scheduler. The semantics of abort and stop have deliberately been separated so that code can detect an aborted driver (i.e., via the return status of SchedulerDriver.join), and instantiate and start another driver if desired (from within the same process.) """ return self.driver.abort() def join(self): """Waits for the driver to be stopped or aborted, possibly blocking the current thread indefinitely. The return status of this function can be used to determine if the driver was aborted (see mesos.proto for a description of Status). """ return self.driver.join() def request(self, requests): """Requests resources from Mesos. (see mesos.proto for a description of Request and how, for example, to request resources from specific slaves.) Any resources available are offered to the framework via Scheduler.resourceOffers callback, asynchronously. """ return self.driver.requestResources(map(encode, requests)) def launch(self, offer_id, tasks, filters=Filters()): """Launches the given set of tasks. Any resources remaining (i.e., not used by the tasks or their executors) will be considered declined. The specified filters are applied on all unused resources (see mesos.proto for a description of Filters). Available resources are aggregated when multiple offers are provided. Note that all offers must belong to the same slave. Invoking this function with an empty collection of tasks declines the offers in entirety (see Scheduler.decline). Note that passing a single offer is also supported. """ return self.driver.launchTasks(encode(offer_id), map(encode, tasks), encode(filters)) def kill(self, task_id): """Kills the specified task. Note that attempting to kill a task is currently not reliable. If, for example, a scheduler fails over while it was attempting to kill a task it will need to retry in the future. Likewise, if unregistered / disconnected, the request will be dropped (these semantics may be changed in the future). """ return self.driver.killTask(encode(task_id)) def reconcile(self, statuses): """Allows the framework to query the status for non-terminal tasks. This causes the master to send back the latest task status for each task in 'statuses', if possible. Tasks that are no longer known will result in a TASK_LOST update. If statuses is empty, then the master will send the latest status for each task currently known. """ return self.driver.reconcileTasks(map(encode, statuses)) def decline(self, offer_id, filters=Filters()): """Declines an offer in its entirety and applies the specified filters on the resources (see mesos.proto for a description of Filters). Note that this can be done at any time, it is not necessary to do this within the Scheduler::resourceOffers callback. """ return self.driver.declineOffer(encode(offer_id), encode(filters)) # TODO filters def accept(self, offer_ids, operations, filters=Filters()): """Accepts the given offers and performs a sequence of operations on those accepted offers. See Offer.Operation in mesos.proto for the set of available operations. Available resources are aggregated when multiple offers are provided. Note that all offers must belong to the same slave. Any unused resources will be considered declined. The specified filters are applied on all unused resources (see mesos.proto for a description of Filters). """ return self.driver.acceptOffers(map(encode, offer_ids), map(encode, operations), encode(filters)) def revive(self): """Removes all filters previously set by the framework (via launchTasks()). This enables the framework to receive offers from those filtered slaves. """ return self.driver.reviveOffers() def suppress(self): """Inform Mesos master to stop sending offers to the framework. The scheduler should call reviveOffers() to resume getting offers. """ return self.driver.suppressOffers() def acknowledge(self, status): """Acknowledges the status update. This should only be called once the status update is processed durably by the scheduler. Not that explicit acknowledgements must be requested via the constructor argument, otherwise a call to this method will cause the driver to crash. """ return self.driver.acknowledgeStatusUpdate(encode(status)) def message(self, executor_id, slave_id, message): """Sends a message from the framework to one of its executors. These messages are best effort; do not expect a framework message to be retransmitted in any reliable fashion. """ return self.driver.sendFrameworkMessage(encode(executor_id), encode(slave_id), message)
41.860262
80
0.628312
9,402
0.980805
0
0
0
0
0
0
4,923
0.513561
ce852eac1ab8df3025b403cd06d435faa196ae79
6,759
py
Python
NeuralNetworks/neuralnetworks/NeuralNetwork.py
AlmCoding/MachineLearningPlayground
b0316367fc269c4fbb4a8035a59058b731e01839
[ "Apache-2.0" ]
null
null
null
NeuralNetworks/neuralnetworks/NeuralNetwork.py
AlmCoding/MachineLearningPlayground
b0316367fc269c4fbb4a8035a59058b731e01839
[ "Apache-2.0" ]
null
null
null
NeuralNetworks/neuralnetworks/NeuralNetwork.py
AlmCoding/MachineLearningPlayground
b0316367fc269c4fbb4a8035a59058b731e01839
[ "Apache-2.0" ]
null
null
null
import numpy as np class NeuralNetwork: """ Class implementing a feed forawrd neural network. Object fields: layers = a tuple containing numbers of neurons in each layer, starting from the input layer L = depth of the NN, eg, with depth L there are L matrices W: W[1], ...,W[L] act_hid = activation function for neurons in the hidden layer d_act_hid = derivative of the activation function for neurons in the hidden layer act_out = activation function for neuron(s) in the output layer d_act_out = derivative of the activation function for neuron(s) in the output layer W = dictionary containing the W matrices for each layer. The keys are arranged such that the matrices stored in the dictionary corresponds to the notation form the lecture. Ie, W[1] is the matrix which describes the connections between the layer 0 and layer 1. The matrix stored at W[1] is a numpy array with dimensions (number of neurons in the layer 1) x (number of neurons in the layer 0) b = dictionary containing the b vectors for each layer. The indexing corresponds to the indexing from the lecture. See above. Eg, dimensions of b[1] (number of neurons in the layer 1) x 1 """ def __init__(self, layers, act_hid, d_act_hid, act_out, d_act_out): self.layers = layers self.L = len(layers) - 1 self.act_hid = act_hid self.d_act_hid = d_act_hid self.act_out = act_out self.d_act_out = d_act_out self.W, self.b = self.init_Wb() def init_Wb(self): """ Initialize the matrices W[1],...,W[L] and the vectors b[1],...,b[L] with random numbers from gaussian distribution with 0-mean, and 0.25 variance. Note that W, b are dictionaries with integer keys. """ W, b = {}, {} mu = 0 sigma = np.sqrt(0.25) for l in range(1, self.L + 1): W[l] = np.random.normal(mu, sigma, (self.layers[l], self.layers[l-1])) b[l] = np.random.normal(mu, sigma, (self.layers[l], 1)) return W, b def fp(self, x): """ Forward propagation. Uses the current parameters W, b Inputs: x = np.array of size self.layers[0] x N. This means that this function performs the forward propagation for N input vectors (columns). Outputs: a = dictionary containing output of each layer of NN. Each dictionary stores N outputs for each of the inputs. Eg., a[1] should be np.array of size self.layers[1] x N The indexing corresponds to the indexing from the lecture. E.g. a[0]=x because a[0] contains the N outputs of the input layer, which is the input x. z = dictionary containing input to each layer of NN. The indexing corresponds to the indexing from the lecture. E.g. z[1]=W[1].dot(a[0])+b[1]. """ a, z = {}, {} a[0] = x for l in range(1, self.L + 1): B = np.tile(self.b[l], (1, x.shape[1])) z[l] = self.W[l].dot(a[l-1]) + B if l == self.L: a[l] = self.act_out(z[l]) else: a[l] = self.act_hid(z[l]) return a, z def output(self, x): """ Provides the output from the last layer of NN. """ a, _ = self.fp(x) a_out = a[self.L] return a_out def bp_l2(self, x, y): """ Backpropagation. Uses the current parameters W, b Args: x = np.array of size self.layers[0] x N (contains N input vectors from the training set) y = np.array of size self.layers[L] x N (contains N output vectors from the training set) Returns: dW = dictionary corresponding to W, where each corresponding key contains a matrix of the same size, eg, W[i].shape = dW[i].shape for all i. It contains the partial derivatives of the cost function with respect to each entry entry of W. db = dictionary corresponding to b, where each corresponding key contains a matrix of the same size, eg, b[i].shape = bW[i].shape for all i. It contains the partial derivatives of the cost function with respect to each entry entry of b. """ a, z = self.fp(x) L = self.L dCdz = {L: a[L] - y} for l in range(L - 1, 0, -1): dCdz[l] = self.W[l + 1].T.dot(dCdz[l + 1]) * self.d_act_hid(z[l]) db = {} for l in range(1, L + 1): db[l] = np.sum(dCdz[l], axis=1).reshape((-1, 1)) dW = {} for l in range(1, L + 1): dW[l] = dCdz[l].dot(a[l - 1].T) return dW, db def bp_ce(self, x, y): """ Backpropagation. Uses the current parameters W, b Args: x = np.array of size self.layers[0] x N (contains N input vectors from the training set) y = np.array of size self.layers[L] x N (contains N output vectors from the training set) Returns: dW = dictionary corresponding to W, where each corresponding key contains a matrix of the same size, eg, W[i].shape = dW[i].shape for all i. It contains the partial derivatives of the cost function with respect to each entry entry of W. db = dictionary corresponding to b, where each corresponding key contains a matrix of the same size, eg, b[i].shape = bW[i].shape for all i. It contains the partial derivatives of the cost function with respect to each entry entry of b. """ a, z = self.fp(x) L = self.L dCdz = {L: (a[L] - y) / a[L] / (1 - a[L]) * self.d_act_out(z[L])} for l in range(L - 1, 0, -1): dCdz[l] = self.W[l + 1].T.dot(dCdz[l + 1]) * self.d_act_hid(z[l]) db = {} for l in range(1, L + 1): db[l] = np.sum(dCdz[l], axis=1).reshape((-1, 1)) dW = {} for l in range(1, L + 1): dW[l] = dCdz[l].dot(a[l - 1].T) return dW, db def gd_learn(self, iter_num, l_rate, x, y, backpropagation, lbd=0): """ Performs gradient descent learning. iter_num = number of iterations of GD l_rate = learning rate x = nparray with the training inputs y = nparray with the training outputs """ for i in range(iter_num): dW, db = backpropagation(x, y) for l in range(1, self.L + 1): self.W[l] = self.W[l] - l_rate * (dW[l] + 2*lbd*self.W[l]) self.b[l] = self.b[l] - l_rate * db[l]
43.326923
113
0.568131
6,737
0.996745
0
0
0
0
0
0
4,339
0.641959
ce86369c63bb6fc50980df6a3068e5a13c86663c
3,350
py
Python
xen/xen-4.2.2/tools/python/xen/xm/console.py
zhiming-shen/Xen-Blanket-NG
47e59d9bb92e8fdc60942df526790ddb983a5496
[ "Apache-2.0" ]
1
2018-02-02T00:15:26.000Z
2018-02-02T00:15:26.000Z
xen/xen-4.2.2/tools/python/xen/xm/console.py
zhiming-shen/Xen-Blanket-NG
47e59d9bb92e8fdc60942df526790ddb983a5496
[ "Apache-2.0" ]
null
null
null
xen/xen-4.2.2/tools/python/xen/xm/console.py
zhiming-shen/Xen-Blanket-NG
47e59d9bb92e8fdc60942df526790ddb983a5496
[ "Apache-2.0" ]
1
2019-05-27T09:47:18.000Z
2019-05-27T09:47:18.000Z
#============================================================================ # This library is free software; you can redistribute it and/or # modify it under the terms of version 2.1 of the GNU Lesser General Public # License as published by the Free Software Foundation. # # This library 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 this library; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #============================================================================ # Copyright (C) 2005 XenSource Ltd #============================================================================ import xen.util.auxbin import xen.lowlevel.xs import os import sys import signal from xen.util import utils XENCONSOLE = "xenconsole" def execConsole(domid, num = 0): xen.util.auxbin.execute(XENCONSOLE, [str(domid), "--num", str(num)]) class OurXenstoreConnection: def __init__(self): self.handle = xen.lowlevel.xs.xs() def read_eventually(self, path): watch = None trans = None try: signal.alarm(10) watch = self.handle.watch(path, None) while True: result = self.handle.read('0', path) if result is not None: signal.alarm(0) return result self.handle.read_watch() finally: signal.alarm(0) if watch is not None: self.handle.unwatch(path, watch) def read_maybe(self, path): return self.handle.read('0', path) def runVncViewer(domid, do_autopass, do_daemonize=False): xs = OurXenstoreConnection() d = '/local/domain/%d/' % domid vnc_port = xs.read_eventually(d + 'console/vnc-port') vfb_backend = xs.read_maybe(d + 'device/vfb/0/backend') vnc_listen = None vnc_password = None vnc_password_tmpfile = None cmdl = ['vncviewer'] if vfb_backend is not None: vnc_listen = xs.read_maybe(vfb_backend + '/vnclisten') if do_autopass: vnc_password = xs.read_maybe(vfb_backend + '/vncpasswd') if vnc_password is not None: cmdl.append('-autopass') vnc_password_tmpfile = os.tmpfile() print >>vnc_password_tmpfile, vnc_password vnc_password_tmpfile.seek(0) vnc_password_tmpfile.flush() if vnc_listen is None: vnc_listen = 'localhost' cmdl.append('%s:%d' % (vnc_listen, int(vnc_port) - 5900)) if do_daemonize: pid = utils.daemonize('vncviewer', cmdl, vnc_password_tmpfile) if pid == 0: print >>sys.stderr, 'failed to invoke vncviewer' os._exit(-1) else: print 'invoking ', ' '.join(cmdl) if vnc_password_tmpfile is not None: os.dup2(vnc_password_tmpfile.fileno(), 0) try: os.execvp('vncviewer', cmdl) except OSError: print >>sys.stderr, 'Error: external vncviewer missing or not \ in the path\nExiting' os._exit(-1)
37.640449
77
0.589851
680
0.202985
0
0
0
0
0
0
1,192
0.355821
ce86edf8e6652b33bfc429b6779f16cab7aabc38
591
py
Python
Solutions/luhn.py
matthijskrul/PythonExercism
7f0fbd340cf32f7bb88b0c32287729968720ee1c
[ "MIT" ]
null
null
null
Solutions/luhn.py
matthijskrul/PythonExercism
7f0fbd340cf32f7bb88b0c32287729968720ee1c
[ "MIT" ]
null
null
null
Solutions/luhn.py
matthijskrul/PythonExercism
7f0fbd340cf32f7bb88b0c32287729968720ee1c
[ "MIT" ]
null
null
null
import re class Luhn(object): def __init__(self, card_num): self.number = card_num def valid(self): if re.search("[^0-9 ]", self.number) or len(re.findall("[0-9]", self.number)) <= 1: return False else: digits = re.sub("[^0-9]", "", self.number) digits = list(map(int, digits)) i = len(digits)-2 while i >= 0: digits[i] = digits[i] * 2 if digits[i] > 9: digits[i] = digits[i] - 9 i -= 2 return sum(digits) % 10 == 0
28.142857
91
0.445008
578
0.978003
0
0
0
0
0
0
26
0.043993
ce882279c49c7c6dbe430df86a631892b7154111
3,342
py
Python
tests/zzz_deprecated_unmaintained/obsmodel/TestZeroMeanGaussLocalStepSpeed.py
HongminWu/bnpy
04c918cc1150ca8d9694c093633d539d9286a1b6
[ "BSD-3-Clause" ]
3
2018-07-02T03:50:23.000Z
2019-05-16T03:23:55.000Z
tests/zzz_deprecated_unmaintained/obsmodel/TestZeroMeanGaussLocalStepSpeed.py
HongminWu/bnpy
04c918cc1150ca8d9694c093633d539d9286a1b6
[ "BSD-3-Clause" ]
1
2021-01-07T01:33:06.000Z
2021-01-07T01:33:06.000Z
tests/zzz_deprecated_unmaintained/obsmodel/TestZeroMeanGaussLocalStepSpeed.py
birlrobotics/bnpy
8f297d8f3e4a56088d7755134c329f63a550be9e
[ "BSD-3-Clause" ]
1
2020-09-01T13:21:18.000Z
2020-09-01T13:21:18.000Z
import numpy as np import scipy.linalg import argparse import time from contextlib import contextmanager def measureTime(f, nTrial=3): def f_timer(*args, **kwargs): times = list() for rep in range(nTrial): start = time.time() result = f(*args, **kwargs) end = time.time() times.append(end-start) if rep == 0: print "trial %2d/%2d: %.3f sec %s" % ( rep+1, nTrial, times[-1], f.__name__) else: print "trial %2d/%2d: %.3f sec" % ( rep+1, nTrial, times[-1]) print "mean of %2d: %.3f sec" % ( nTrial, np.mean(times)) print "median of %2d: %.3f sec" % ( nTrial, np.median(times)) print '' return result return f_timer @measureTime def mahalDist_np_solve(X=None, B=None, cholB=None): ''' Compute mahalanobis the old fashioned way. ''' if B is not None: cholB = np.linalg.cholesky(B) Q = np.linalg.solve(cholB, X.T) return Q @measureTime def mahalDist_scipy_solve(X=None, B=None, cholB=None): ''' Compute mahalanobis the old fashioned way. ''' if B is not None: cholB = np.linalg.cholesky(B) Q = scipy.linalg.solve(cholB, X.T) return Q @measureTime def mahalDist_scipy_solve_triangular(X=None, B=None, cholB=None): ''' Compute mahalanobis with triangular method ''' if B is not None: cholB = np.linalg.cholesky(B) Q = scipy.linalg.solve_triangular(cholB, X.T, lower=True) return Q @measureTime def mahalDist_scipy_solve_triangular_nocheck( X=None, B=None, cholB=None): ''' Compute mahalanobis with triangular method ''' if B is not None: cholB = np.linalg.cholesky(B) Q = scipy.linalg.solve_triangular( cholB, X.T, lower=True, check_finite=False) return Q if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument('--N', type=int, default=1e5) parser.add_argument('--D', type=int, default=64) args = parser.parse_args() N = args.N D = args.D print "TIMING TEST: N=%d D=%d" % (N, D) X = np.random.randn(N, D) R = np.random.randn(D, D) B = np.dot(R.T, R) + np.eye(D, D) cholB = np.linalg.cholesky(B) mahalDist_np_solve(X=X, cholB=cholB) mahalDist_scipy_solve(X=X, cholB=cholB) mahalDist_scipy_solve_triangular(X=X, cholB=cholB) mahalDist_scipy_solve_triangular_nocheck(X=X, cholB=cholB) """ In [41]: Qs = scipy.linalg.solve_triangular(cholB, X.T, lower=True, check_finite=False) In [42]: %timeit -n1 -r1 Q = scipy.linalg.solve_triangular(cholB, X.T, lower=True, check_finite=False) 1 loops, best of 1: 625 ms per loop In [43]: %timeit -n1 -r1 Q = scipy.linalg.solve_triangular(cholB, X.T, lower=True, check_finite=False) 1 loops, best of 1: 623 ms per loop In [44]: %timeit -n1 -r1 Q = scipy.linalg.solve_triangular(cholB, X.T, lower=True) 1 loops, best of 1: 790 ms per loop In [45]: %timeit -n1 -r1 Q = scipy.linalg.solve_triangular(cholB, X.T, lower=True) 1 loops, best of 1: 799 ms per loop In [46]: %timeit -n1 -r1 Q = scipy.linalg.solve(cholB, X.T) 1 loops, best of 1: 1.26 s per loop In [47]: %timeit -n1 -r1 Q = scipy.linalg.solve(cholB, X.T) 1 loops, best of 1: 1.26 s per loop """
30.66055
102
0.620586
0
0
0
0
1,060
0.317175
0
0
1,177
0.352184
ce8992e05fc290077147eefaf26bedd1f77bd977
1,450
py
Python
actions/set_status.py
Bedrock-OSS/server-updater
b4329563f9af5525a69ffd1f461c53bf09e52c2b
[ "MIT" ]
1
2022-01-02T13:24:01.000Z
2022-01-02T13:24:01.000Z
actions/set_status.py
Bedrock-OSS/server-updater
b4329563f9af5525a69ffd1f461c53bf09e52c2b
[ "MIT" ]
3
2021-11-15T23:17:25.000Z
2022-01-02T13:26:34.000Z
actions/set_status.py
Bedrock-OSS/server-updater
b4329563f9af5525a69ffd1f461c53bf09e52c2b
[ "MIT" ]
null
null
null
from subprocess import run from actions.common import get_name_and_org, get_process_config from flask import request def set_status(): if not "id" in request.form and 'status' in request.form: return "Bad request", 400 id, status = request.form['id'], request.form['status'] if(id == 'bedrock-oss:server-updater'): return "The server updater cannot be stopped", 400 name, org = get_name_and_org(id) if status.lower() == "start": if start_project(name): return "Started", 200 else: return "Project misconfigured", 500 elif status.lower() == 'stop': if stop_project(name): return "Stopped", 200 else: return "Project misconfigured", 500 else: return "Bad request", 400 def start_project(name): data = get_process_config(name) if('run_process' in data): run(['systemctl', '--user', 'start', 'bedrock-oss:' + name]) elif('run_docker' in data): run(['docker', 'run', '-d', '--name', 'bedrock-oss-' + name, 'bedrock-oss-' + name]) else: return False return True def stop_project(name): data = get_process_config(name) if('run_process' in data): run(['systemctl', '--user', 'stop', 'bedrock-oss:' + name]) elif('run_docker' in data): run(['docker', 'stop', 'bedrock-oss-' + name]) run(['docker', 'rm', 'bedrock-oss-' + name]) else: return False return True
35.365854
92
0.611034
0
0
0
0
0
0
0
0
429
0.295862
ce8c6b49bd95348ca5598200de74bc4909b5e91e
4,067
py
Python
amo2kinto/generator.py
Mozilla-GitHub-Standards/9856eb0fa59d2f9b39dc3c83d0f31961057b7f0ddf57ba213fd01b9b0f99c4cc
abbd2d8874b79fc12a0d8b6570593c8a6dbc2e68
[ "Apache-2.0" ]
5
2016-07-06T21:35:06.000Z
2019-05-15T22:36:24.000Z
amo2kinto/generator.py
Mozilla-GitHub-Standards/9856eb0fa59d2f9b39dc3c83d0f31961057b7f0ddf57ba213fd01b9b0f99c4cc
abbd2d8874b79fc12a0d8b6570593c8a6dbc2e68
[ "Apache-2.0" ]
63
2016-05-11T09:15:16.000Z
2021-10-15T16:44:23.000Z
amo2kinto/generator.py
mozilla-services/kinto2xml
ae8aee28c9a2b15e19a21553eb031ae32f673de2
[ "Apache-2.0" ]
6
2018-02-01T14:13:04.000Z
2020-07-13T05:36:57.000Z
import os import datetime from dateutil.parser import parse as dateutil_parser from jinja2 import Environment, PackageLoader from kinto_http import cli_utils from . import constants from .logger import logger JSON_DATE_FORMAT = "%Y-%m-%dT%H:%M:%SZ" COLLECTION_FORMAT = '/buckets/{bucket_id}/collections/{collection_id}' def iso2human(value): date = dateutil_parser(value) return date.strftime('%b %d, %Y') def get_template(name): loader = PackageLoader('amo2kinto', 'templates') env = Environment(loader=loader) env.filters['datetime'] = iso2human return env.get_template(name) def generate_index(records, template, target_dir): def sort_records(record): return record['details']['created'], record['last_modified'] records = sorted(records, key=sort_records, reverse=True) res = template.render(records=records) filename = os.path.join(target_dir, 'index.html') logger.info('Writing %s' % filename) with open(filename, 'w', encoding='utf-8') as f: f.write(res) def generate_record(record, template, target_dir, get_filename): res = template.render(record=record) filename = os.path.join(target_dir, get_filename(record)) logger.info('Writing %s' % filename) # This needs to be able to write to S3 with open(filename, 'w', encoding='utf-8') as f: f.write(res) def get_record_filename(record): return '%s.html' % record.get('blockID', record['id']) def generate(client, collections, target_dir, collection_template, record_template): logger.info('Reading data from Kinto %s...' % client.session.server_url) records = [] for collection_uri in collections: parts = collection_uri.strip('/').split('/') bucket_id = parts[1] collection_id = parts[3] logger.warn('Reading records from %s...' % collection_uri) response = client.get_records(bucket=bucket_id, collection=collection_id) for record in response: record.setdefault('details', {}) if 'created' not in record['details']: date = datetime.date.fromtimestamp(record['last_modified'] / 1000) record['details']['created'] = date.strftime(JSON_DATE_FORMAT) records.append(record) if not os.path.exists(target_dir): os.makedirs(target_dir) col_tpl = get_template(collection_template) rec_tpl = get_template(record_template) logger.warn('Generating index...') generate_index(records, col_tpl, target_dir) logger.warn('Generating records files...') for record in records: generate_record(record, rec_tpl, target_dir, get_record_filename) def main(args=None): parser = cli_utils.add_parser_options( description='Generate blocked item description files.', default_collection=None, default_bucket=constants.DESTINATION_BUCKET, default_server=constants.KINTO_SERVER, default_auth=constants.AUTH, include_collection=False) parser.add_argument('--addons-collection', help='Collection name for addon', type=str, default=constants.ADDONS_COLLECTION) parser.add_argument('--plugins-collection', help='Collection name for plugin', type=str, default=constants.PLUGINS_COLLECTION) parser.add_argument('-d', '--target-dir', help='Destination directory to write files in.', type=str, default=constants.TARGET_DIR) args = parser.parse_args(args=args) cli_utils.setup_logger(logger, args) kinto_client = cli_utils.create_client_from_args(args) collections = [ COLLECTION_FORMAT.format(bucket_id=args.bucket, collection_id=args.addons_collection), COLLECTION_FORMAT.format(bucket_id=args.bucket, collection_id=args.plugins_collection), ] generate(kinto_client, collections, args.target_dir, 'collection.tpl', 'record.tpl')
33.336066
88
0.668798
0
0
0
0
0
0
0
0
665
0.163511
ce8e42b2a35ed5fd98c1fefc1db9f29031a082bc
2,270
py
Python
migrations/versions/2019_03_04_optional_chart_and_table_classifications.py.py
AlexKouzy/ethnicity-facts-and-figures-publisher
18ab2495a8633f585e18e607c7f75daa564a053d
[ "MIT" ]
1
2021-10-06T13:48:36.000Z
2021-10-06T13:48:36.000Z
migrations/versions/2019_03_04_optional_chart_and_table_classifications.py.py
AlexKouzy/ethnicity-facts-and-figures-publisher
18ab2495a8633f585e18e607c7f75daa564a053d
[ "MIT" ]
116
2018-11-02T17:20:47.000Z
2022-02-09T11:06:22.000Z
migrations/versions/2019_03_04_optional_chart_and_table_classifications.py.py
racedisparityaudit/rd_cms
a12f0e3f5461cc41eed0077ed02e11efafc5dd76
[ "MIT" ]
2
2018-11-09T16:47:35.000Z
2020-04-09T13:06:48.000Z
"""Make some fields on Chart and Table nullable We want to copy chart and table data across to these tables but have no way to add a classification for each one, so we'll have to live with some nulls in here. Revision ID: 2019_03_04_make_fields_nullable Revises: 2019_03_04_chart_table_settings Create Date: 2019-03-05 16:38:12.835894 """ from alembic import op import sqlalchemy as sa # revision identifiers, used by Alembic. revision = "2019_03_04_make_fields_nullable" down_revision = "2019_03_04_chart_table_settings" branch_labels = None depends_on = None def upgrade(): op.alter_column("dimension_chart", "classification_id", existing_type=sa.VARCHAR(length=255), nullable=True) op.alter_column("dimension_chart", "includes_all", existing_type=sa.BOOLEAN(), nullable=True) op.alter_column("dimension_chart", "includes_parents", existing_type=sa.BOOLEAN(), nullable=True) op.alter_column("dimension_chart", "includes_unknown", existing_type=sa.BOOLEAN(), nullable=True) op.alter_column("dimension_table", "classification_id", existing_type=sa.VARCHAR(length=255), nullable=True) op.alter_column("dimension_table", "includes_all", existing_type=sa.BOOLEAN(), nullable=True) op.alter_column("dimension_table", "includes_parents", existing_type=sa.BOOLEAN(), nullable=True) op.alter_column("dimension_table", "includes_unknown", existing_type=sa.BOOLEAN(), nullable=True) def downgrade(): op.alter_column("dimension_table", "includes_unknown", existing_type=sa.BOOLEAN(), nullable=False) op.alter_column("dimension_table", "includes_parents", existing_type=sa.BOOLEAN(), nullable=False) op.alter_column("dimension_table", "includes_all", existing_type=sa.BOOLEAN(), nullable=False) op.alter_column("dimension_table", "classification_id", existing_type=sa.VARCHAR(length=255), nullable=False) op.alter_column("dimension_chart", "includes_unknown", existing_type=sa.BOOLEAN(), nullable=False) op.alter_column("dimension_chart", "includes_parents", existing_type=sa.BOOLEAN(), nullable=False) op.alter_column("dimension_chart", "includes_all", existing_type=sa.BOOLEAN(), nullable=False) op.alter_column("dimension_chart", "classification_id", existing_type=sa.VARCHAR(length=255), nullable=False)
54.047619
113
0.781498
0
0
0
0
0
0
0
0
995
0.438326
ce8ebc8223eab95eca97cd475849f6774b863014
962
py
Python
open_discussions/middleware/channel_api_test.py
mitodl/open-discussions
ab6e9fac70b8a1222a84e78ba778a7a065c20541
[ "BSD-3-Clause" ]
12
2017-09-27T21:23:27.000Z
2020-12-25T04:31:30.000Z
open_discussions/middleware/channel_api_test.py
mitodl/open-discussions
ab6e9fac70b8a1222a84e78ba778a7a065c20541
[ "BSD-3-Clause" ]
3,293
2017-06-30T18:16:01.000Z
2022-03-31T18:01:34.000Z
open_discussions/middleware/channel_api_test.py
mitodl/open-discussions
ab6e9fac70b8a1222a84e78ba778a7a065c20541
[ "BSD-3-Clause" ]
1
2020-04-13T12:19:57.000Z
2020-04-13T12:19:57.000Z
"""Tests for channel API middleware""" from django.utils.functional import SimpleLazyObject from open_discussions.middleware.channel_api import ChannelApiMiddleware def test_channel_api_middleware( mocker, jwt_token, rf, user ): # pylint: disable=unused-argument """Tests that the middleware makes a channel API object available on the request""" api_mock_obj = mocker.Mock(some_api_method=mocker.Mock(return_value="result")) patched_api_cls = mocker.patch( "open_discussions.middleware.channel_api.Api", return_value=api_mock_obj ) request = rf.get("/") request.user = user get_request_mock = mocker.Mock() middleware = ChannelApiMiddleware(get_request_mock) middleware(request) assert hasattr(request, "channel_api") assert isinstance(request.channel_api, SimpleLazyObject) result = request.channel_api.some_api_method() patched_api_cls.assert_called_with(user) assert result == "result"
38.48
87
0.759875
0
0
0
0
0
0
0
0
231
0.240125
ce8f967c1a3e2320cb9057b9e55d32dfed9aae91
2,019
py
Python
app/python/query_strings.py
ProfessorUdGuru/toykinter
66c0a9877df6b4b3034125566e687b7361085d2b
[ "Unlicense" ]
null
null
null
app/python/query_strings.py
ProfessorUdGuru/toykinter
66c0a9877df6b4b3034125566e687b7361085d2b
[ "Unlicense" ]
null
null
null
app/python/query_strings.py
ProfessorUdGuru/toykinter
66c0a9877df6b4b3034125566e687b7361085d2b
[ "Unlicense" ]
null
null
null
# query_strings.py ''' Since Sqlite queries are inserted as string in Python code, the queries can be stored here to save space in the modules where they are used. ''' delete_color_scheme = ''' DELETE FROM color_scheme WHERE color_scheme_id = ? ''' insert_color_scheme = ''' INSERT INTO color_scheme VALUES (null, ?, ?, ?, ?, 0, 0) ''' select_all_color_schemes = ''' SELECT bg, highlight_bg, head_bg, fg FROM color_scheme ''' select_all_color_schemes_plus = ''' SELECT bg, highlight_bg, head_bg, fg, built_in, color_scheme_id FROM color_scheme ''' select_color_scheme_current = ''' SELECT bg, highlight_bg, head_bg, fg FROM format WHERE format_id = 1 ''' select_current_database = ''' SELECT current_database FROM closing_state WHERE closing_state_id = 1 ''' select_font_scheme = ''' SELECT font_size, output_font, input_font FROM format WHERE format_id = 1 ''' select_opening_settings = ''' SELECT bg, highlight_bg, head_bg, fg, output_font, input_font, font_size, default_bg, default_highlight_bg, default_head_bg, default_fg, default_output_font, default_input_font, default_font_size FROM format WHERE format_id = 1 ''' update_color_scheme_null = ''' UPDATE format SET (bg, highlight_bg, head_bg, fg) = (null, null, null, null) WHERE format_id = 1 ''' update_current_database = ''' UPDATE closing_state SET current_database = ? WHERE closing_state_id = 1 ''' update_format_color_scheme = ''' UPDATE format SET (bg, highlight_bg, head_bg, fg) = (?, ?, ?, ?) WHERE format_id = 1 ''' update_format_fonts = ''' UPDATE format SET (font_size, output_font, input_font) = (?, ?, ?) WHERE format_id = 1 '''
20.393939
69
0.595344
0
0
0
0
0
0
0
0
1,640
0.812283
ce90ad08ae1e89a4b497c7dcbd24f5d92a0ba879
428
py
Python
travel/migrations/0029_auto_20190514_2108.py
sausage-team/travel-notes
3c2454ebad7764906c5ff30cbdfe296cb7c64eb4
[ "MIT" ]
null
null
null
travel/migrations/0029_auto_20190514_2108.py
sausage-team/travel-notes
3c2454ebad7764906c5ff30cbdfe296cb7c64eb4
[ "MIT" ]
null
null
null
travel/migrations/0029_auto_20190514_2108.py
sausage-team/travel-notes
3c2454ebad7764906c5ff30cbdfe296cb7c64eb4
[ "MIT" ]
null
null
null
# Generated by Django 2.2.1 on 2019-05-14 13:08 from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ ('travel', '0028_auto_20190514_1929'), ] operations = [ migrations.AlterField( model_name='user', name='uid', field=models.CharField(default='b8554d6d-264a-4ce7-bd9b-a2e1e218a13d', max_length=40), ), ]
22.526316
98
0.616822
335
0.78271
0
0
0
0
0
0
129
0.301402
ce9204471f1cea2c7a2d1ce2b0a13ce9e7a84406
2,027
py
Python
ci/mocks/sensu/sensu_verify.py
infrawatch/collectd-sensubility
5bea37045137ab88c879159ba1e1792a70eb3717
[ "Apache-2.0" ]
null
null
null
ci/mocks/sensu/sensu_verify.py
infrawatch/collectd-sensubility
5bea37045137ab88c879159ba1e1792a70eb3717
[ "Apache-2.0" ]
7
2020-06-15T22:08:33.000Z
2021-01-08T16:03:01.000Z
ci/mocks/sensu/sensu_verify.py
paramite/collectd-sensubility
4baca0cd657b4f9d2edeb19e82b2b052c45c972c
[ "Apache-2.0" ]
1
2020-02-18T21:03:10.000Z
2020-02-18T21:03:10.000Z
#!/usr/bin/env python """, Mocked Ruby based Sensu server. Connects to the RabbitMQ instance and sends check requests as Sensu server would and waits for appropriate response. Fails the response is not received at all or in invalid format. """ import click import json import pika import signal import sys CI_TEST_RESULTS = { 'test1': {'output': 'foo\n', 'status': 0}, 'test2': {'output': 'bar\n', 'status': 1}, 'test3': {'output': 'baz\n', 'status': 2}, 'standalone_check': {'output': 'foobar\n', 'status': 2} } def timeout_handler(signum, frame): print("Verification timed out") sys.exit(2) @click.command() @click.option('--rabbit-url', required=True, default='amqp://guest:guest@127.0.0.1:5672/%2fsensu') @click.option('--timeout', type=int, required=True, default=10) def main(rabbit_url, timeout): connection = pika.BlockingConnection(pika.URLParameters(rabbit_url)) channel = connection.channel() signal.signal(signal.SIGALRM, timeout_handler) signal.alarm(timeout) try: hits = set() for method, properties, body in channel.consume('results'): channel.basic_ack(method.delivery_tag) result = json.loads(body) print(f"Verifying check result {result['check']['name']}") assert(result['check']['name'] in CI_TEST_RESULTS) print("Result name found in list of expected results.") for test in ("status", "output"): assert(CI_TEST_RESULTS[result['check']['name']][test] == result['check'][test]) print(f"successful verification of {test} in result.") hits.add(result['check']['name']) if hits == set(CI_TEST_RESULTS.keys()): # end verification break except AssertionError as ex: print(f"Failed verification of {test} in result: {result}") sys.exit(1) else: sys.exit(0) finally: connection.close() if __name__ == '__main__': main()
29.808824
95
0.624075
0
0
0
0
1,362
0.671929
0
0
766
0.377898
ce92edbc18e65275ac6f2bfc1a972e464010433f
205
py
Python
django_ecommerce/contact/admin.py
marshallhumble/Python_Web
d0d31647c882f30422f31aa62ecd61ae7ad87123
[ "MIT" ]
null
null
null
django_ecommerce/contact/admin.py
marshallhumble/Python_Web
d0d31647c882f30422f31aa62ecd61ae7ad87123
[ "MIT" ]
null
null
null
django_ecommerce/contact/admin.py
marshallhumble/Python_Web
d0d31647c882f30422f31aa62ecd61ae7ad87123
[ "MIT" ]
null
null
null
from django.contrib import admin from .models import ContactForm class ContactFormAdmin(admin.ModelAdmin): class Meta: model = ContactForm admin.site.register(ContactForm, ContactFormAdmin)
20.5
50
0.780488
85
0.414634
0
0
0
0
0
0
0
0
ce943e66efc0e44702391aac0684e3625f5526e9
718
py
Python
ds/practice/daily_practice/20-07/assets/code/reverse_sll.py
tobias-fyi/vela
b0b3d3c6dc3fa397c8c7a492098a02cf75e0ff82
[ "MIT" ]
null
null
null
ds/practice/daily_practice/20-07/assets/code/reverse_sll.py
tobias-fyi/vela
b0b3d3c6dc3fa397c8c7a492098a02cf75e0ff82
[ "MIT" ]
8
2020-03-24T17:47:23.000Z
2022-03-12T00:33:21.000Z
ds/practice/daily_practice/20-07/assets/code/reverse_sll.py
tobias-fyi/vela
b0b3d3c6dc3fa397c8c7a492098a02cf75e0ff82
[ "MIT" ]
null
null
null
""" HackerRank :: Reverse a singly-linked list https://www.hackerrank.com/challenges/reverse-a-linked-list/problem Complete the reverse function below. For your reference: SinglyLinkedListNode: int data SinglyLinkedListNode next """ def reverse(head): # head node value can be null # Keep track of previous node prev_node = None cur_node = head # Loop through - while node.next while cur_node: # Save node for overwriting cur_node next_node = cur_node.next # Set current node's next to prev_node cur_node.next = prev_node # Pass previous node to next iteration prev_node = cur_node cur_node = next_node return prev_node
23.16129
67
0.683844
0
0
0
0
0
0
0
0
445
0.619777
ce9550e5fc7912aecc7ac103430d2e2845e818b7
12,566
gyp
Python
ui/gfx/gfx.gyp
cvsuser-chromium/chromium
acb8e8e4a7157005f527905b48dd48ddaa3b863a
[ "BSD-3-Clause" ]
4
2017-04-05T01:51:34.000Z
2018-02-15T03:11:54.000Z
ui/gfx/gfx.gyp
cvsuser-chromium/chromium
acb8e8e4a7157005f527905b48dd48ddaa3b863a
[ "BSD-3-Clause" ]
1
2021-12-13T19:44:12.000Z
2021-12-13T19:44:12.000Z
ui/gfx/gfx.gyp
cvsuser-chromium/chromium
acb8e8e4a7157005f527905b48dd48ddaa3b863a
[ "BSD-3-Clause" ]
4
2017-04-05T01:52:03.000Z
2022-02-13T17:58:45.000Z
# Copyright (c) 2013 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. { 'variables': { 'chromium_code': 1, }, 'targets': [ { 'target_name': 'gfx', 'type': '<(component)', 'dependencies': [ '<(DEPTH)/base/base.gyp:base', '<(DEPTH)/base/base.gyp:base_i18n', '<(DEPTH)/base/base.gyp:base_static', '<(DEPTH)/base/third_party/dynamic_annotations/dynamic_annotations.gyp:dynamic_annotations', '<(DEPTH)/net/net.gyp:net', '<(DEPTH)/skia/skia.gyp:skia', '<(DEPTH)/third_party/icu/icu.gyp:icui18n', '<(DEPTH)/third_party/icu/icu.gyp:icuuc', '<(DEPTH)/third_party/libpng/libpng.gyp:libpng', '<(DEPTH)/third_party/zlib/zlib.gyp:zlib', '<(DEPTH)/url/url.gyp:url_lib', ], # text_elider.h includes ICU headers. 'export_dependent_settings': [ '<(DEPTH)/skia/skia.gyp:skia', '<(DEPTH)/third_party/icu/icu.gyp:icui18n', '<(DEPTH)/third_party/icu/icu.gyp:icuuc', ], 'defines': [ 'GFX_IMPLEMENTATION', ], 'sources': [ 'android/device_display_info.cc', 'android/device_display_info.h', 'android/gfx_jni_registrar.cc', 'android/gfx_jni_registrar.h', 'android/java_bitmap.cc', 'android/java_bitmap.h', 'android/shared_device_display_info.cc', 'android/shared_device_display_info.h', 'animation/animation.cc', 'animation/animation.h', 'animation/animation_container.cc', 'animation/animation_container.h', 'animation/animation_container_element.h', 'animation/animation_container_observer.h', 'animation/animation_delegate.h', 'animation/linear_animation.cc', 'animation/linear_animation.h', 'animation/multi_animation.cc', 'animation/multi_animation.h', 'animation/slide_animation.cc', 'animation/slide_animation.h', 'animation/throb_animation.cc', 'animation/throb_animation.h', 'animation/tween.cc', 'animation/tween.h', 'blit.cc', 'blit.h', 'box_f.cc', 'box_f.h', 'break_list.h', 'canvas.cc', 'canvas.h', 'canvas_android.cc', 'canvas_paint_gtk.cc', 'canvas_paint_gtk.h', 'canvas_paint_mac.h', 'canvas_paint_mac.mm', 'canvas_paint_win.cc', 'canvas_paint_win.h', 'canvas_skia.cc', 'canvas_skia_paint.h', 'codec/jpeg_codec.cc', 'codec/jpeg_codec.h', 'codec/png_codec.cc', 'codec/png_codec.h', 'color_analysis.cc', 'color_analysis.h', 'color_profile.cc', 'color_profile.h', 'color_profile_mac.cc', 'color_profile_win.cc', 'color_utils.cc', 'color_utils.h', 'display.cc', 'display.h', 'display_observer.cc', 'display_observer.h', 'favicon_size.cc', 'favicon_size.h', 'frame_time.h', 'font.cc', 'font.h', 'font_fallback_win.cc', 'font_fallback_win.h', 'font_list.cc', 'font_list.h', 'font_render_params_android.cc', 'font_render_params_linux.cc', 'font_render_params_linux.h', 'font_smoothing_win.cc', 'font_smoothing_win.h', 'gfx_export.h', 'gfx_paths.cc', 'gfx_paths.h', 'gpu_memory_buffer.cc', 'gpu_memory_buffer.h', 'image/canvas_image_source.cc', 'image/canvas_image_source.h', 'image/image.cc', 'image/image.h', 'image/image_family.cc', 'image/image_family.h', 'image/image_ios.mm', 'image/image_mac.mm', 'image/image_png_rep.cc', 'image/image_png_rep.h', 'image/image_skia.cc', 'image/image_skia.h', 'image/image_skia_operations.cc', 'image/image_skia_operations.h', 'image/image_skia_rep.cc', 'image/image_skia_rep.h', 'image/image_skia_source.h', 'image/image_skia_util_ios.h', 'image/image_skia_util_ios.mm', 'image/image_skia_util_mac.h', 'image/image_skia_util_mac.mm', 'image/image_util.cc', 'image/image_util.h', 'image/image_util_ios.mm', 'insets.cc', 'insets.h', 'insets_base.h', 'insets_f.cc', 'insets_f.h', 'interpolated_transform.cc', 'interpolated_transform.h', 'mac/scoped_ns_disable_screen_updates.h', 'matrix3_f.cc', 'matrix3_f.h', 'native_widget_types.h', 'ozone/dri/dri_skbitmap.cc', 'ozone/dri/dri_skbitmap.h', 'ozone/dri/dri_surface.cc', 'ozone/dri/dri_surface.h', 'ozone/dri/dri_surface_factory.cc', 'ozone/dri/dri_surface_factory.h', 'ozone/dri/dri_wrapper.cc', 'ozone/dri/dri_wrapper.h', 'ozone/dri/hardware_display_controller.cc', 'ozone/dri/hardware_display_controller.h', 'ozone/impl/file_surface_factory.cc', 'ozone/impl/file_surface_factory.h', 'ozone/surface_factory_ozone.cc', 'ozone/surface_factory_ozone.h', 'pango_util.cc', 'pango_util.h', 'path.cc', 'path.h', 'path_aura.cc', 'path_gtk.cc', 'path_win.cc', 'path_win.h', 'path_x11.cc', 'path_x11.h', 'platform_font.h', 'platform_font_android.cc', 'platform_font_ios.h', 'platform_font_ios.mm', 'platform_font_mac.h', 'platform_font_mac.mm', 'platform_font_ozone.cc', 'platform_font_pango.cc', 'platform_font_pango.h', 'platform_font_win.cc', 'platform_font_win.h', 'point.cc', 'point.h', 'point3_f.cc', 'point3_f.h', 'point_base.h', 'point_conversions.cc', 'point_conversions.h', 'point_f.cc', 'point_f.h', 'quad_f.cc', 'quad_f.h', 'range/range.cc', 'range/range.h', 'range/range_mac.mm', 'range/range_win.cc', 'rect.cc', 'rect.h', 'rect_base.h', 'rect_base_impl.h', 'rect_conversions.cc', 'rect_conversions.h', 'rect_f.cc', 'rect_f.h', 'render_text.cc', 'render_text.h', 'render_text_mac.cc', 'render_text_mac.h', 'render_text_ozone.cc', 'render_text_pango.cc', 'render_text_pango.h', 'render_text_win.cc', 'render_text_win.h', 'safe_integer_conversions.h', 'scoped_canvas.h', 'scoped_cg_context_save_gstate_mac.h', 'scoped_ns_graphics_context_save_gstate_mac.h', 'scoped_ns_graphics_context_save_gstate_mac.mm', 'scoped_ui_graphics_push_context_ios.h', 'scoped_ui_graphics_push_context_ios.mm', 'screen.cc', 'screen.h', 'screen_android.cc', 'screen_aura.cc', 'screen_gtk.cc', 'screen_ios.mm', 'screen_mac.mm', 'screen_win.cc', 'screen_win.h', 'scrollbar_size.cc', 'scrollbar_size.h', 'selection_model.cc', 'selection_model.h', 'sequential_id_generator.cc', 'sequential_id_generator.h', 'shadow_value.cc', 'shadow_value.h', 'size.cc', 'size.h', 'size_base.h', 'size_conversions.cc', 'size_conversions.h', 'size_f.cc', 'size_f.h', 'skbitmap_operations.cc', 'skbitmap_operations.h', 'skia_util.cc', 'skia_util.h', 'skia_utils_gtk.cc', 'skia_utils_gtk.h', 'switches.cc', 'switches.h', 'sys_color_change_listener.cc', 'sys_color_change_listener.h', 'text_constants.h', 'text_elider.cc', 'text_elider.h', 'text_utils.cc', 'text_utils.h', 'text_utils_android.cc', 'text_utils_ios.mm', 'text_utils_skia.cc', 'transform.cc', 'transform.h', 'transform_util.cc', 'transform_util.h', 'utf16_indexing.cc', 'utf16_indexing.h', 'vector2d.cc', 'vector2d.h', 'vector2d_conversions.cc', 'vector2d_conversions.h', 'vector2d_f.cc', 'vector2d_f.h', 'vector3d_f.cc', 'vector3d_f.h', 'win/dpi.cc', 'win/dpi.h', 'win/hwnd_util.cc', 'win/hwnd_util.h', 'win/scoped_set_map_mode.h', 'win/singleton_hwnd.cc', 'win/singleton_hwnd.h', 'win/window_impl.cc', 'win/window_impl.h', 'x/x11_atom_cache.cc', 'x/x11_atom_cache.h', 'x/x11_types.cc', 'x/x11_types.h', ], 'conditions': [ ['OS=="ios"', { # iOS only uses a subset of UI. 'sources/': [ ['exclude', '^codec/jpeg_codec\\.cc$'], ], }, { 'dependencies': [ '<(libjpeg_gyp_path):libjpeg', ], }], # TODO(asvitkine): Switch all platforms to use canvas_skia.cc. # http://crbug.com/105550 ['use_canvas_skia==1', { 'sources!': [ 'canvas_android.cc', ], }, { # use_canvas_skia!=1 'sources!': [ 'canvas_skia.cc', ], }], ['toolkit_uses_gtk == 1', { 'dependencies': [ '<(DEPTH)/build/linux/system.gyp:gtk', ], 'sources': [ 'gtk_native_view_id_manager.cc', 'gtk_native_view_id_manager.h', 'gtk_preserve_window.cc', 'gtk_preserve_window.h', 'gdk_compat.h', 'gtk_compat.h', 'gtk_util.cc', 'gtk_util.h', 'image/cairo_cached_surface.cc', 'image/cairo_cached_surface.h', 'scoped_gobject.h', ], }], ['OS=="win"', { 'sources': [ 'gdi_util.cc', 'gdi_util.h', 'icon_util.cc', 'icon_util.h', ], # TODO(jschuh): C4267: http://crbug.com/167187 size_t -> int # C4324 is structure was padded due to __declspec(align()), which is # uninteresting. 'msvs_disabled_warnings': [ 4267, 4324 ], }], ['OS=="android"', { 'sources!': [ 'animation/throb_animation.cc', 'display_observer.cc', 'path.cc', 'selection_model.cc', ], 'dependencies': [ 'gfx_jni_headers', ], 'link_settings': { 'libraries': [ '-landroid', '-ljnigraphics', ], }, }], ['OS=="android" and android_webview_build==0', { 'dependencies': [ '<(DEPTH)/base/base.gyp:base_java', ], }], ['OS=="android" or OS=="ios"', { 'sources!': [ 'render_text.cc', 'render_text.h', 'text_utils_skia.cc', ], }], ['use_pango==1', { 'dependencies': [ '<(DEPTH)/build/linux/system.gyp:pangocairo', ], }], ['ozone_platform_dri==1', { 'dependencies': [ '<(DEPTH)/build/linux/system.gyp:dridrm', ], }], ], 'target_conditions': [ # Need 'target_conditions' to override default filename_rules to include # the file on iOS. ['OS == "ios"', { 'sources/': [ ['include', '^scoped_cg_context_save_gstate_mac\\.h$'], ], }], ], } ], 'conditions': [ ['OS=="android"' , { 'targets': [ { 'target_name': 'gfx_jni_headers', 'type': 'none', 'direct_dependent_settings': { 'include_dirs': [ '<(SHARED_INTERMEDIATE_DIR)/ui/gfx', ], }, 'sources': [ '../android/java/src/org/chromium/ui/gfx/BitmapHelper.java', '../android/java/src/org/chromium/ui/gfx/DeviceDisplayInfo.java', ], 'variables': { 'jni_gen_package': 'ui/gfx', 'jni_generator_ptr_type': 'long', }, 'includes': [ '../../build/jni_generator.gypi' ], }, ], }], ], }
29.990453
100
0.524988
0
0
0
0
0
0
0
0
8,093
0.644039
ce9579f634d692007205ef460ecf1c49f7c087d2
2,191
py
Python
preprocess/python/h5py_utils.py
fredericsun/seq2seq-ChinesePoetryGenerater
2d3aab3807f99c99046f53a4d5cc045ae733386f
[ "MIT" ]
null
null
null
preprocess/python/h5py_utils.py
fredericsun/seq2seq-ChinesePoetryGenerater
2d3aab3807f99c99046f53a4d5cc045ae733386f
[ "MIT" ]
null
null
null
preprocess/python/h5py_utils.py
fredericsun/seq2seq-ChinesePoetryGenerater
2d3aab3807f99c99046f53a4d5cc045ae733386f
[ "MIT" ]
null
null
null
import numpy as np import h5py import os import sys from copy import deepcopy #handle .(period) and slash specially since it is part of path #replace with \period or \slash-forward when store, recover later #not using '\forward-slash' is because \f is a special character PERIOD='\period' SLASH='\slash-forward' ''' This function will save a python dictionary with format {'key':vector} to hdf5 format target_dict is the target dictionary f_name is the HDF5 file name including path to it mode is file open mode, 'a' for append is the default setting output file is a HDF5 file which the keys of HDF5 are keys of target dict dataset is corresponding array of each key in target dict ''' def dict2hdf5(target_dict, f_name, sub_group='/', mode='a'): print ('Saving In HDF5 Format...') index_count=0 bar_count=0 total=len(target_dict) if total<50: total=50 #open HDF5 file f = h5py.File(f_name, mode) for key in target_dict: ######### print progress ############# index_count +=1 if index_count % (total/50) ==0: sys.stdout.write('\r'), bar_count += 1 sys.stdout.write("[%-50s] %d%% %d/%d" % ('#'*bar_count, 2*bar_count, index_count, total)) sys.stdout.flush() ######### end print progress ######### #print os.path.join(sub_group,key) #sys.stdout.flush() #replace special handled chars, will not change string if / is not in it dataset_key=key.replace('.',PERIOD).replace('/',SLASH) f.create_dataset(os.path.join(sub_group,dataset_key), data=target_dict[key]) f.close() ''' This function will convert saved hdf5 file from previous function back to {'key':vector} dictionary f_name is the HDF5 file name including path to it ''' def hdf52dict(f_name, sub_group='/' ): rv=dict() #open HDF5 file f = h5py.File(f_name, 'r') group=f[sub_group] for key in group: #replace back special handled chars key=key.replace(PERIOD, '.').replace(SLASH,'/') rv[key]=np.array(group[key]) f.close() return rv def main(): d=dict() d['hello']=[1,0,0,0,0,0,0,0,0,0,0] d['world']=[0,1,0,0,0,0,0,0,0,0,0] dict2hdf5(target_dict=d, f_name='test') rv=hdf52dict('test') print (rv) if __name__ == '__main__': main()
19.918182
99
0.682337
0
0
0
0
0
0
0
0
1,131
0.516203
ce96c92938a6d505e9a78e6bd30465ac609c886c
2,238
py
Python
libs/dataset/pipeline.py
arnasRad/vits
c0e0689d8c24049a2531dd5d52fc96a594389db2
[ "MIT" ]
null
null
null
libs/dataset/pipeline.py
arnasRad/vits
c0e0689d8c24049a2531dd5d52fc96a594389db2
[ "MIT" ]
null
null
null
libs/dataset/pipeline.py
arnasRad/vits
c0e0689d8c24049a2531dd5d52fc96a594389db2
[ "MIT" ]
null
null
null
import re import string from libs.dataset.core import SampleEntry, oov_replacement_vocabulary, letter_replacements __word_start_regex = f'[ \t\n]|^|[{string.punctuation}]' __word_end_regex = f'[ \t\n]|$|[{string.punctuation}]' def does_not_have_numbers(s: SampleEntry): return not any(char.isdigit() for char in s.text) def waveform_not_longer_than(target_length): def step(s: SampleEntry): return s.duration < target_length return step def waveform_not_shorter_than(target_length): def step(s: SampleEntry): return s.duration > target_length return step def replace_oov(line): for symbol, replacement in oov_replacement_vocabulary.items(): line = re.sub(fr"{symbol}", replacement, line) line = re.sub(r"[ ]{2,}", ' ', line) line = re.sub(r"\n\s*\n", r'\n', line) return line def test_replace_oov(): assert replace_oov("vien_as") == "vien as" assert replace_oov("vien«as") == "vienas" assert replace_oov("vien\xadas") == "vienas" assert replace_oov("vien®as") == "vienas" assert replace_oov("vien°as") == "vienas" assert replace_oov("vien²as") == "vienas" assert replace_oov("vien»as") == "vienas" assert replace_oov("vienʼas") == "vien'as" assert replace_oov("vien˚as") == "vienas" assert replace_oov("vien‒as") == "vien-as" assert replace_oov("vien–as") == "vien-as" assert replace_oov("vien—as") == "vien-as" assert replace_oov("vien‘as") == "vien'as" assert replace_oov("vien’as") == "vien'as" assert replace_oov("vien“as") == 'vien"as' assert replace_oov("vien”as") == 'vien"as' assert replace_oov("vien„as") == 'vien"as' assert replace_oov("vien\\as") == "vienas" assert replace_oov("vien,,as") == 'vien"as' def replace_letter_to_sound(line): for letter, sound in letter_replacements.items(): line = re.sub(fr"({__word_start_regex})[{letter.lower()}{letter.upper()}]+({__word_end_regex})", fr"\1{sound}\2", line) return line def test_replace_letter_to_sound(): assert replace_letter_to_sound("aAa b C d e f g h I J k L M n") == "aã bė̃ cė̃ dė̃ eẽ ef̃ gė̃ hãš iĩ jòt kã el̃ em̃ eñ" assert replace_letter_to_sound("vienas du aaaa") == "vienas du aã"
34.430769
130
0.662645
0
0
0
0
0
0
0
0
709
0.310829
ce97329b4f1ef6afddfe7243f66fd55019aff8be
733
py
Python
examples/rfid_lookup.py
CMU-Robotics-Club/pyrc
bda1113a75ff9b67b518016b8b8aceeb9971d50b
[ "MIT" ]
null
null
null
examples/rfid_lookup.py
CMU-Robotics-Club/pyrc
bda1113a75ff9b67b518016b8b8aceeb9971d50b
[ "MIT" ]
2
2015-01-14T01:48:01.000Z
2015-04-06T05:16:23.000Z
examples/rfid_lookup.py
CMU-Robotics-Club/pyrc
bda1113a75ff9b67b518016b8b8aceeb9971d50b
[ "MIT" ]
null
null
null
#!/usr/bin/env python3 from rc.clients import APIClient if __name__ == '__main__': """ Given a valid RFID attempts to lookup the User's ID. Then using that User ID, if it exists, get more information about the User. """ # If the bash variables RC_PUBLIC_KEY and RC_PRIVATE_KEY # are not set you need to pass the public and private key # values into this constructor client = APIClient() rfid = input("RFID: ") # Ask the API who this RFID corresponds to (user_id, api_request_id) = client.rfid(rfid, "Some Meta String") if user_id is None: print("No such User") else: # Get more information about the user user = client.user(user_id) print("Username: {}".format(user['username']))
25.275862
67
0.68895
0
0
0
0
0
0
0
0
464
0.633015
ce9737971517d349e0d99c904be7b6abc01113e4
1,015
py
Python
LeetCode/0417. Pacific Atlantic Water Flow/solution.py
InnoFang/oh-my-algorithms
f559dba371ce725a926725ad28d5e1c2facd0ab2
[ "Apache-2.0" ]
1
2017-03-31T15:24:01.000Z
2017-03-31T15:24:01.000Z
LeetCode/0417. Pacific Atlantic Water Flow/solution.py
InnoFang/Algorithm-Library
1896b9d8b1fa4cd73879aaecf97bc32d13ae0169
[ "Apache-2.0" ]
null
null
null
LeetCode/0417. Pacific Atlantic Water Flow/solution.py
InnoFang/Algorithm-Library
1896b9d8b1fa4cd73879aaecf97bc32d13ae0169
[ "Apache-2.0" ]
null
null
null
""" 113 / 113 test cases passed. Runtime: 92 ms Memory Usage: 16.4 MB """ class Solution: def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]: m, n = len(heights), len(heights[0]) def dfs(used, x, y): used[x][y] = True for xx, yy in [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]: if 0 <= xx < m and 0 <= yy < n and not used[xx][yy] and heights[xx][yy] >= heights[x][y]: dfs(used, xx, yy) pacific_used = [[False] * n for _ in range(m)] atlantic_used = [[False] * n for _ in range(m)] for i in range(m): dfs(pacific_used, i, 0) dfs(atlantic_used, i, n - 1) for i in range(n): dfs(pacific_used, 0, i) dfs(atlantic_used, m - 1, i) ans = [] for i in range(m): for j in range(n): if pacific_used[i][j] and atlantic_used[i][j]: ans.append([i, j]) return ans
36.25
105
0.469951
932
0.918227
0
0
0
0
0
0
73
0.071921
ce983be2f0cd8dfbb73fd808cea94fd76f198a4e
456
py
Python
cellardoor/api/methods.py
movermeyer/cellardoor
25192b07224ff7bd33fd29ebac07340bef53a2ed
[ "MIT" ]
null
null
null
cellardoor/api/methods.py
movermeyer/cellardoor
25192b07224ff7bd33fd29ebac07340bef53a2ed
[ "MIT" ]
3
2015-01-31T14:53:06.000Z
2015-02-01T19:04:30.000Z
cellardoor/api/methods.py
movermeyer/cellardoor
25192b07224ff7bd33fd29ebac07340bef53a2ed
[ "MIT" ]
2
2015-01-31T14:54:28.000Z
2018-03-05T17:33:42.000Z
__all__ = ( 'LIST', 'GET', 'CREATE', 'UPDATE', 'REPLACE', 'DELETE', 'ALL', 'get_http_methods' ) LIST = 'list' GET = 'get' CREATE = 'create' REPLACE = 'replace' UPDATE = 'update' DELETE = 'delete' ALL = (LIST, GET, CREATE, UPDATE, REPLACE, DELETE) _http_methods = { LIST: ('get',), GET: ('get',), CREATE: ('post',), REPLACE: ('put',), UPDATE: ('patch',), DELETE: ('delete',) } def get_http_methods(method): return _http_methods[method]
13.818182
50
0.605263
0
0
0
0
0
0
0
0
147
0.322368
ce9b85f5702f1c6dc74c0af21d1a6d33b4e3768c
617
py
Python
tests/test007_check_instant_ach.py
xuru/Sila-Python
12fef8886580327779d32cf7596dae4516b36c11
[ "Apache-2.0" ]
9
2019-05-14T14:19:00.000Z
2021-11-10T05:07:39.000Z
tests/test007_check_instant_ach.py
xuru/Sila-Python
12fef8886580327779d32cf7596dae4516b36c11
[ "Apache-2.0" ]
8
2019-05-13T16:27:54.000Z
2021-09-14T17:31:51.000Z
tests/test007_check_instant_ach.py
xuru/Sila-Python
12fef8886580327779d32cf7596dae4516b36c11
[ "Apache-2.0" ]
8
2019-02-13T18:53:58.000Z
2021-09-03T15:49:31.000Z
import unittest import silasdk from tests.test_config import ( app, eth_private_key, eth_private_key_4, instant_ach_handle, user_handle) class Test007CheckInstantAchTest(unittest.TestCase): def test_check_instant_ach(self): payload = { "user_handle": instant_ach_handle, "account_name": "default_plaid" } response = silasdk.User.check_instant_ach( app, payload, eth_private_key_4) self.assertIsNotNone(response["status"]) self.assertIsNotNone(response["message"]) if __name__ == '__main__': unittest.main()
26.826087
77
0.670989
416
0.67423
0
0
0
0
0
0
69
0.111831
ce9e402973e2cfeb24237745cb68e27f8bb1a25f
72
py
Python
lxml2json/__init__.py
meteozond/lxml2json
502b3512dcb0ae034469a2cedd7661a4801c4425
[ "MIT" ]
null
null
null
lxml2json/__init__.py
meteozond/lxml2json
502b3512dcb0ae034469a2cedd7661a4801c4425
[ "MIT" ]
null
null
null
lxml2json/__init__.py
meteozond/lxml2json
502b3512dcb0ae034469a2cedd7661a4801c4425
[ "MIT" ]
null
null
null
#!/usr/bin/env python from functions import convert name = "convert"
18
30
0.722222
0
0
0
0
0
0
0
0
31
0.430556
ce9f112137fa8c8ee27bbea19fe1b8d402e47a00
1,758
py
Python
src/day10.py
nlasheras/aoc-2021
17af9108e2f907747c9aca784e52c80e81949845
[ "MIT" ]
null
null
null
src/day10.py
nlasheras/aoc-2021
17af9108e2f907747c9aca784e52c80e81949845
[ "MIT" ]
null
null
null
src/day10.py
nlasheras/aoc-2021
17af9108e2f907747c9aca784e52c80e81949845
[ "MIT" ]
null
null
null
""" https://adventofcode.com/2021/day/10 """ from functools import reduce def read_input(filename): with open(filename, "r", encoding='utf-8') as file: return [l.rstrip() for l in file.readlines()] opening_map = { ")": "(", "]": "[", "}": "{", ">": "<" } closing_map = {v: k for k,v in opening_map.items()} def parse(line): points = { ")": 3, "]": 57, "}": 1197, ">": 25137 } error_score = 0 stack = [] for _c in line: if _c in opening_map: # it's a closing character expecting = opening_map[_c] top = stack[-1] if expecting == top: stack.pop() else: return points[_c], stack else: stack.append(_c) return error_score, stack def part1_syntax_error(lines): tuples = [parse(l) for l in lines] print(f"What is the total syntax error score for those errors? {sum([t[0] for t in tuples])}") def part2_autocompletion(lines): valid_lines = filter(lambda t: t[0] == 0, [parse(l) for l in lines]) # valid lines have score 0 def score(stack): autocomplete_points = { ")": 1, "]": 2, "}": 3, ">": 4} completion_string = [closing_map[c] for c in reversed(stack)] return reduce(lambda acc, x: acc*5 + x, [autocomplete_points[c] for c in completion_string], 0) scores = [score(t[1]) for t in valid_lines] middle_score = sorted(scores)[len(scores)//2] print(f"What is the middle score? {middle_score}") if __name__ == '__main__': input_lines = read_input("input10_test.txt") part1_syntax_error(input_lines) part2_autocompletion(input_lines) input_lines = read_input("input10.txt") part1_syntax_error(input_lines) part2_autocompletion(input_lines)
33.807692
103
0.608077
0
0
0
0
0
0
0
0
325
0.184869
ce9f2c870786c7bb3405fa82c2066dccfd69d84d
2,318
py
Python
bigbench/models/dummy_model.py
sebschu/BIG-bench
b04e56e3a9c0b6488fe154370de0b9697f991b67
[ "Apache-2.0" ]
4
2021-05-19T01:07:37.000Z
2021-11-02T01:31:56.000Z
bigbench/models/dummy_model.py
sebschu/BIG-bench
b04e56e3a9c0b6488fe154370de0b9697f991b67
[ "Apache-2.0" ]
6
2021-05-26T09:08:17.000Z
2021-05-31T15:27:14.000Z
bigbench/models/dummy_model.py
sebschu/BIG-bench
b04e56e3a9c0b6488fe154370de0b9697f991b67
[ "Apache-2.0" ]
3
2021-04-21T00:10:43.000Z
2021-06-01T17:22:54.000Z
# Copyright 2021 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import bigbench.api.model as model import bigbench.api.util as util import numpy as np import scipy.special class DummyModel(model.Model): def generate_text( self, inputs, max_length=100, stop_string=None, output_regex=None ): dummy_text = "dummy text, 123. Easy peasy lemon squeezy." if isinstance(inputs, str): text = dummy_text elif isinstance(inputs, list): text = [f"{i}_{dummy_text}" for i, _ in enumerate(inputs)] else: raise ValueError("inputs has unexpected type %s" % type(inputs)) text = util.postprocess_output(text, max_length, stop_string, output_regex) return text def cond_log_prob(self, inputs, targets, absolute_normalization=False): def generate_dummy_scores(inpt, tgts): unnorm_log_probs = [np.log(1 / (i + 1)) for i, _ in enumerate(tgts)] if len(unnorm_log_probs) > 1: # we want to make sure tasks can correctly handle responses which are # assigned zero probability unnorm_log_probs[0] = -np.inf if absolute_normalization: log_probs = unnorm_log_probs else: log_normalizer = scipy.special.logsumexp(unnorm_log_probs) log_probs = [lp - log_normalizer for lp in unnorm_log_probs] return log_probs if isinstance(inputs, str): return generate_dummy_scores(inputs, targets) elif isinstance(inputs, list): return [ generate_dummy_scores(inpt, tgts) for inpt, tgts in zip(inputs, targets) ] else: raise ValueError("inputs has unexpected type %s" % type(inputs))
37.387097
88
0.654443
1,631
0.703624
0
0
0
0
0
0
783
0.337791
ce9f408bce05df804432eb2ae09a3fb7b0a734bb
67
py
Python
strategy/__init__.py
mmmaaaggg/QABAT
d6f20d926de047af6857e466cf28084d0ba69993
[ "MIT" ]
3
2019-08-31T18:01:10.000Z
2021-04-04T09:51:17.000Z
strategy/__init__.py
mmmaaaggg/QABAT
d6f20d926de047af6857e466cf28084d0ba69993
[ "MIT" ]
null
null
null
strategy/__init__.py
mmmaaaggg/QABAT
d6f20d926de047af6857e466cf28084d0ba69993
[ "MIT" ]
1
2020-08-15T17:04:14.000Z
2020-08-15T17:04:14.000Z
# -*- coding: utf-8 -*- """ Created on 2017/11/18 @author: MG """
9.571429
23
0.522388
0
0
0
0
0
0
0
0
64
0.955224
ce9f508775c39825fac04c419be3ad975e03c872
1,099
py
Python
js/data/getcytoband.py
ManuelLecaro/ucsc-xena-client
67611408800d227172b27d97fcba267ddb93c06e
[ "Apache-2.0" ]
60
2017-01-18T00:51:24.000Z
2022-02-16T11:06:08.000Z
js/data/getcytoband.py
ManuelLecaro/ucsc-xena-client
67611408800d227172b27d97fcba267ddb93c06e
[ "Apache-2.0" ]
407
2016-03-04T23:11:29.000Z
2022-03-18T07:27:10.000Z
js/data/getcytoband.py
ManuelLecaro/ucsc-xena-client
67611408800d227172b27d97fcba267ddb93c06e
[ "Apache-2.0" ]
60
2017-03-02T15:19:48.000Z
2021-01-19T09:39:46.000Z
#!/usr/bin/env python from itertools import groupby, tee import json, os files = [ ('hg38', 'http://hgdownload.soe.ucsc.edu/goldenPath/hg38/database/cytoBand.txt.gz'), ('hg19', 'http://hgdownload.soe.ucsc.edu/goldenPath/hg19/database/cytoBand.txt.gz'), ('hg18', 'http://hgdownload.soe.ucsc.edu/goldenPath/hg18/database/cytoBand.txt.gz')] chroms = ['chr' + str(i + 1) for i in range(22)] + ['chrX', 'chrY'] Kchrom = 0 def fetch(name, url): os.system("curl %s | gunzip > cytoBand_%s" % (url, name)); def parseLine(line): [chrom, start, end, id, _] = line return [int(start), int(end), id] def write(name): with open('cytoBand_%s' % name, 'r') as fh: lines = map(lambda l: l.strip().split('\t'), fh.readlines()) bychrom = dict([(k, list(l)) for (k, l) in groupby(lines, lambda line: line[Kchrom])]) filtered = {chrom: map(parseLine, list(bychrom[chrom])) for chrom in chroms} with open('cytoBand_%s.json' % name, 'w') as wfh: wfh.write(json.dumps(filtered)) for (name, url) in files: # fetch(name, url) write(name)
33.30303
94
0.624204
0
0
0
0
0
0
0
0
369
0.33576
cea21f7323fd769b736524d144d28f5a8974c4ed
6,973
py
Python
osxphotos/cli/param_types.py
oPromessa/osxphotos
0d7e324f0262093727147b9f22ed275e962e8725
[ "MIT" ]
null
null
null
osxphotos/cli/param_types.py
oPromessa/osxphotos
0d7e324f0262093727147b9f22ed275e962e8725
[ "MIT" ]
null
null
null
osxphotos/cli/param_types.py
oPromessa/osxphotos
0d7e324f0262093727147b9f22ed275e962e8725
[ "MIT" ]
null
null
null
"""Click parameter types for osxphotos CLI""" import datetime import os import pathlib import re import bitmath import click import pytimeparse2 from osxphotos.export_db_utils import export_db_get_version from osxphotos.photoinfo import PhotoInfoNone from osxphotos.phototemplate import PhotoTemplate, RenderOptions from osxphotos.timeutils import time_string_to_datetime, utc_offset_string_to_seconds from osxphotos.timezones import Timezone from osxphotos.utils import expand_and_validate_filepath, load_function __all__ = [ "BitMathSize", "DateOffset", "DateTimeISO8601", "ExportDBType", "FunctionCall", "TemplateString", "TimeISO8601", "TimeOffset", "TimeString", "UTCOffset", ] class DateTimeISO8601(click.ParamType): name = "DATETIME" def convert(self, value, param, ctx): try: return datetime.datetime.fromisoformat(value) except Exception: self.fail( f"Invalid datetime format {value}. " "Valid format: YYYY-MM-DD[*HH[:MM[:SS[.fff[fff]]]][+HH:MM[:SS[.ffffff]]]]" ) class BitMathSize(click.ParamType): name = "BITMATH" def convert(self, value, param, ctx): try: value = bitmath.parse_string(value) except ValueError: # no units specified try: value = int(value) value = bitmath.Byte(value) except ValueError as e: self.fail( f"{value} must be specified as bytes or using SI/NIST units. " + "For example, the following are all valid and equivalent sizes: '1048576' '1.048576MB', '1 MiB'." ) return value class TimeISO8601(click.ParamType): name = "TIME" def convert(self, value, param, ctx): try: return datetime.time.fromisoformat(value).replace(tzinfo=None) except Exception: self.fail( f"Invalid time format {value}. " "Valid format: HH[:MM[:SS[.fff[fff]]]][+HH:MM[:SS[.ffffff]]] " "however, note that timezone will be ignored." ) class FunctionCall(click.ParamType): name = "FUNCTION" def convert(self, value, param, ctx): if "::" not in value: self.fail( f"Could not parse function name from '{value}'. " "Valid format filename.py::function" ) filename, funcname = value.split("::") filename_validated = expand_and_validate_filepath(filename) if not filename_validated: self.fail(f"'{filename}' does not appear to be a file") try: function = load_function(filename_validated, funcname) except Exception as e: self.fail(f"Could not load function {funcname} from {filename_validated}") return (function, value) class ExportDBType(click.ParamType): name = "EXPORTDB" def convert(self, value, param, ctx): try: export_db_name = pathlib.Path(value) if export_db_name.is_dir(): raise click.BadParameter(f"{value} is a directory") if export_db_name.is_file(): # verify it's actually an osxphotos export_db # export_db_get_version will raise an error if it's not valid osxphotos_ver, export_db_ver = export_db_get_version(value) return value except Exception: self.fail(f"{value} exists but is not a valid osxphotos export database. ") class TemplateString(click.ParamType): """Validate an osxphotos template language (OTL) template string""" name = "OTL_TEMPLATE" def convert(self, value, param, ctx): try: cwd = os.getcwd() _, unmatched = PhotoTemplate(photo=PhotoInfoNone()).render( value, options=RenderOptions(export_dir=cwd, dest_path=cwd, filepath=cwd), ) if unmatched: self.fail(f"Template '{value}' contains unknown field(s): {unmatched}") return value except ValueError as e: self.fail(e) class TimeString(click.ParamType): """A timestring in format HH:MM:SS, HH:MM:SS.fff, HH:MM""" name = "TIMESTRING" def convert(self, value, param, ctx): try: return time_string_to_datetime(value) except ValueError: self.fail( f"Invalid time format: {value}. " "Valid format for time: 'HH:MM:SS', 'HH:MM:SS.fff', 'HH:MM'" ) class DateOffset(click.ParamType): """A date offset string in the format ±D days, ±W weeks, ±Y years, ±D where D is days""" name = "DATEOFFSET" def convert(self, value, param, ctx): # if it's a single number treat it as days # but pytimeparse2 treats is as seconds so need to verify it's just a number and if so, # convert it to seconds value = value.strip() if re.match(r"^[+-]?\s*?\d+$", value): # just a number # strip any whitespace, e.g. for "+ 1" or "- 1" value = "".join(value.split()) try: return datetime.timedelta(days=int(value)) except ValueError: self.fail( f"Invalid date offset format: {value}. " "Valid format for date/time offset: '±D days', '±W weeks', '±M months', '±D' where D is days " ) offset = pytimeparse2.parse(value) if offset is not None: offset = offset / 86400 return datetime.timedelta(days=offset) self.fail( f"Invalid date offset format: {value}. " "Valid format for date/time offset: '±D days', '±W weeks', '±M months', '±D' where D is days " ) class TimeOffset(click.ParamType): """A time offset string in the format [+-]HH:MM[:SS[.fff[fff]]] or +1 days, -2 hours, -18000, etc""" name = "TIMEOFFSET" def convert(self, value, param, ctx): offset = pytimeparse2.parse(value) if offset is not None: return datetime.timedelta(seconds=offset) self.fail( f"Invalid time offset format: {value}. " "Valid format for date/time offset: '±HH:MM:SS', '±H hours' (or hr), '±M minutes' (or min), '±S seconds' (or sec), '±S' (where S is seconds)" ) class UTCOffset(click.ParamType): """A UTC offset timezone in format ±[hh]:[mm], ±[h]:[mm], or ±[hh][mm]""" name = "UTC_OFFSET" def convert(self, value, param, ctx): try: offset_seconds = utc_offset_string_to_seconds(value) return Timezone(offset_seconds) except Exception: self.fail( f"Invalid timezone format: {value}. " "Valid format for timezone offset: '±HH:MM', '±H:MM', or '±HHMM'" )
31.695455
153
0.582819
6,240
0.891938
0
0
0
0
0
0
2,456
0.351058
cea24139f5562c36b7de46c6bc9e387fa1a316e6
26,229
py
Python
old/ms/django_rg/views.py
jcnelson/syndicate
4837265be3e0aa18cdf4ee50316dbfc2d1f06e5b
[ "Apache-2.0" ]
16
2015-01-02T15:39:04.000Z
2016-03-17T06:38:46.000Z
old/ms/django_rg/views.py
jcnelson/syndicate
4837265be3e0aa18cdf4ee50316dbfc2d1f06e5b
[ "Apache-2.0" ]
37
2015-01-28T20:58:05.000Z
2016-03-22T04:01:32.000Z
old/ms/django_rg/views.py
jcnelson/syndicate
4837265be3e0aa18cdf4ee50316dbfc2d1f06e5b
[ "Apache-2.0" ]
8
2015-04-08T02:26:03.000Z
2016-03-04T05:56:24.000Z
''' All of these views are predicated on the user already being logged in to valid session. djago_ag/views.py John Whelchel Summer 2013 These are the views for the Replica Gateway section of the administration site. They are all decorated with @authenticate to make sure that a user is logged in; if not, they are redirected to the login page. Some are decorated with precheck, a decorator that makes sure the passed g_id and passwords are valid. ''' import logging import json from django.http import HttpResponse from django.shortcuts import redirect from django.template import Context, loader, RequestContext from django.views.decorators.csrf import csrf_exempt from django.forms.formsets import formset_factory from django_lib.auth import authenticate from django_lib.decorators import precheck from django_lib import gatewayforms from django_lib import forms as libforms from storage.storagetypes import transactional import storage.storage as db from MS.volume import Volume from MS.user import SyndicateUser as User from MS.gateway import ReplicaGateway as RG # This is the view to be redirected to when precheck fails; i.e. # the given password or g_id is wrong. PRECHECK_REDIRECT = 'django_rg.views.viewgateway' @authenticate def viewgateway(request, g_id=0): ''' The view for viewing and changing any of the main settings on any RG. Passes forms for changing different settings, and the volumes attached to the gateway. ''' session = request.session username = session['login_email'] g_id = int(g_id) # Check for any passed error messages and initial_data from state to save db calls. message = session.pop('message', "") rg_initial_data = session.get('rg_initial_data' + str(g_id), []) # Make sure gateway exists. g = db.read_replica_gateway(g_id) if not g: logging.error("Error reading gateway %d : Exception: %s" % (g_id, e)) message = "No replica gateway with the ID %d exists." % g_id t = loader.get_template("gateway_templates/viewgateway_failure.html") c = Context({'message':message, 'username':username}) return HttpResponse(t.render(c)) # Create forms for changing location, adding volumes, # changing password, getting password, and changing config location_form = gatewayforms.ModifyGatewayLocation(initial={'host':g.host, 'port':g.port}) add_form = gatewayforms.GatewayAddVolume() json_form = gatewayforms.ModifyGatewayConfig() password_form = libforms.Password() change_password_form = libforms.ChangePassword() # Get all attached volumes and their respective owners owners = [] vols = [] for v_id in g.volume_ids: vol = db.read_volume(v_id) if not vol: logging.error("Volume ID in gateways volume_ids does not map to volume. Gateway: %s" % g_name) else: vols.append(vol) attrs = {"SyndicateUser.owner_id ==": vol.owner_id} owners.append(db.get_user(attrs)) vol_owners = zip(vols, owners) # Create formatted data based on vols for the formset, if not passed in state. if not rg_initial_data: for v in vols: rg_initial_data.append({'volume_name':v.name, 'remove':False}) session['rg_initial_data' + str(g_id)] = rg_initial_data VolumeFormSet = formset_factory(gatewayforms.GatewayRemoveVolume, extra=0) if rg_initial_data: formset = VolumeFormSet(initial=rg_initial_data) else: formset = [] t = loader.get_template("gateway_templates/viewreplicagateway.html") c = RequestContext(request, {'username':username, 'gateway':g, 'message':message, 'vol_owners':vol_owners, 'location_form':location_form, 'add_form':add_form, 'json_form':json_form, 'remove_forms':formset, 'password_form':password_form, 'change_password_form':change_password_form}) return HttpResponse(t.render(c)) @authenticate @precheck("RG", PRECHECK_REDIRECT) def changeprivacy(request, g_id): ''' Handler for changing privacy settings. ''' session = request.session username = session['login_email'] g_id = int(g_id) g = db.read_replica_gateway(g_id) if g.private: fields = {'private':False} else: fields = {'private':True} try: db.update_replica_gateway(g_id, **fields) session['new_change'] = "We've changed your gateways's privacy setting." session['next_url'] = '/syn/RG/viewgateway/' + str(g_id) session['next_message'] = "Click here to go back to your gateway." return redirect('/syn/thanks') except Exception as e: session['message'] = "Unable to update gateway." logging.info(message) logging.info(e) return redirect('django_rg.views.viewgateway', g_id) @authenticate @precheck("RG", PRECHECK_REDIRECT) def changejson(request, g_id): ''' Handler for changing json config file. ''' session = request.session username = session['login_email'] g_id = int(g_id) form = gatewayforms.ModifyGatewayConfig(request.POST) if form.is_valid(): logging.info(request.FILES) if 'json_config' not in request.FILES: session['message'] = "No uploaded file." return redirect('django_rg.views.viewgateway', g_id) if request.FILES['json_config'].multiple_chunks(): session['message'] = "Uploaded file too large; please make smaller than 2.5M" return redirect('django_rg.views.viewgateway', g_id) config = request.FILES['json_config'].read() fields = {} try: fields['json_config'] = json.loads(config) except Exception as e: logging.info("Possible JSON load error: %s" % e) try: fields['json_config'] = json.loads("\"" + config + "\"") except Exception as e: logging.error("Definite JSON load error %s" % e) session['message'] = "Error parsing given JSON text." return redirect('django_rg.views.viewgateway', g_id) db.update_replica_gateway(g_id, **fields) session['new_change'] = "We've changed your gateways's JSON configuration." session['next_url'] = '/syn/RG/viewgateway/' + str(g_id) session['next_message'] = "Click here to go back to your gateway." return redirect('/syn/thanks') else: session['message'] = "Invalid form. Did you upload a file?" return redirect('django_rg.views.viewgateway', g_id) # Doesn't use precheck() because doesn't use Password() form, just ChangePassword() form. @authenticate def changepassword(request, g_id): ''' Handler for changing gateway password. Since it can't use precheck because of password reasons, must verify POST-ness itself. ''' session = request.session username = session['login_email'] g_id = int(g_id) # Precheck if request.method != "POST": return redirect('/syn/RG/viewgateway/' + str(g_id)) try: g = db.read_replica_gateway(g_id) if not g: raise Exception("No gateway exists.") except Exception as e: logging.error("Error reading gateway %d : Exception: %s" % (g_id, e)) message = "No replica gateway with the ID %d exists." % g_id t = loader.get_template("gateway_templates/viewgateway_failure.html") c = Context({'message':message, 'username':username}) return HttpResponse(t.render(c)) form = libforms.ChangePassword(request.POST) if not form.is_valid(): session['message'] = "You must fill out all password fields." return redirect('django_rg.views.viewgateway', g_id) else: # Check password hash if not RG.authenticate(g, form.cleaned_data['oldpassword']): session['message'] = "Incorrect password." return redirect('django_rg.views.viewgateway', g_id) elif form.cleaned_data['newpassword_1'] != form.cleaned_data['newpassword_2']: session['message'] = "Your new passwords did not match each other." return redirect('django_rg.views.viewgateway', g_id) # Ok to change password else: new_hash = RG.generate_password_hash(form.cleaned_data['newpassword_1']) fields = {'ms_password_hash':new_hash} try: db.update_replica_gateway(g_id, **fields) except Exception as e: logging.error("Unable to update replica gateway %d. Exception %s" % (g_id, e)) session['message'] = "Unable to update gateway." return redirect('django_rg.views.viewgateway', g_id) session['new_change'] = "We've changed your gateways's password." session['next_url'] = '/syn/RG/viewgateway/' + str(g_id) session['next_message'] = "Click here to go back to your volume." return redirect('/syn/thanks') @authenticate @precheck("RG", PRECHECK_REDIRECT) def addvolume(request, g_id): ''' Handler for adding a volume to the gateay. ''' # This is a helper method that isolates the @transactional decorator, speeding # up the code when it doesn't reach update() in this view and allowing for # errors that would break in GAE if the decorator was applied to the entire view. @transactional(xg=True) def update(v_id, g_id, vfields, gfields): db.update_volume(v_id, **vfields) db.update_replica_gateway(g_id, **gfields) session.pop('rg_initial_data' + str(g_id)) session = request.session username = session['login_email'] g_id = int(g_id) form = gatewayforms.GatewayAddVolume(request.POST) if form.is_valid(): attrs = {"Volume.name ==":form.cleaned_data['volume_name'].strip().replace(" ", "_")} vols = db.list_volumes(attrs, limit=1) if vols: volume = vols[0] logging.info(volume) else: session['message'] = "The volume %s doesn't exist." % form.cleaned_data['volume_name'] return redirect('django_ag.views.viewgateway', g_id) gateway = db.read_replica_gateway(g_id) # prepare volume state if volume.rg_ids: new_rgs = volume.rg_ids new_rgs.append(gateway.g_id) else: new_rgs = [gateway.g_id] vfields = {'rg_ids':new_rgs} # prepate RG state old_vids = gateway.volume_ids new_vid = volume.volume_id if new_vid in old_vids: session['message'] = "That volume is already attached to this gateway!" return redirect('django_rg.views.viewgateway', g_id) if old_vids: old_vids.append(new_vid) new_vids = old_vids else: new_vids = [new_vid] # Update and redirect try: gfields={'volume_ids':new_vids} update(volume.volume_id, g_id, vfields, gfields) except Exception as e: logging.error("Unable to update replica gateway %s or volume %s. Exception %s" % (rg.ms_username, volume.name, e)) session['message'] = "Unable to update." return redirect('django_rg.views.viewgateway', g_id) session['new_change'] = "We've updated your RG's volumes." session['next_url'] = '/syn/RG/viewgateway/' + str(g_id) session['next_message'] = "Click here to go back to your gateway." return redirect('/syn/thanks') else: session['message'] = "Invalid entries for adding volumes." return redirect('django_rg.views.viewgateway', g_id) @authenticate @precheck("RG", PRECHECK_REDIRECT) def removevolumes(request, g_id): ''' This handler allows removal of one or many volumes from an Aqcquisition Gateway. It calls multi_update() as a helper method to allow transactional updates to the database. ''' # This is a helper method that isolates the @transactional decorator, speeding # up the code when it doesn't reach update() in this view and allowing for # errors that would break in GAE if the decorator was applied to the entire view. # It updates multiple volumes at once @transactional(xg=True) def multi_update(v_ids, g_id, vfields, gfields): for v_id, vfield in zip(v_ids, vfields): db.update_volume(v_id, **vfield) db.update_replica_gateway(g_id, **gfields) session.pop('rg_initial_data' + str(g_id)) session = request.session username = session['login_email'] g_id = int(g_id) VolumeFormSet = formset_factory(gatewayforms.GatewayRemoveVolume, extra=0) formset = VolumeFormSet(request.POST) # This call is not checked because the formset will always be valid (readonly widgets) formset.is_valid() volume_ids_to_be_removed = [] new_rgs_set = [] initial_and_forms = zip(session.get('rg_initial_data' + str(g_id), []), formset.forms) for i, f in initial_and_forms: if f.cleaned_data['remove']: attrs = {"Volume.name ==":i['volume_name']} vols = db.list_volumes(attrs, limit=1) vol = vols[0] # update each volumes new RG list new_rgs = vol.rg_ids new_rgs.remove(g_id) new_rgs_set.append({'rg_ids':new_rgs}) # update info for AG update volume_ids_to_be_removed.append(vol.volume_id) if not volume_ids_to_be_removed: session['message'] = "You must select at least one volume to remove." return redirect('django_rg.views.viewgateway', g_id) old_vids = set(db.read_replica_gateway(g_id).volume_ids) new_vids = list(old_vids - set(volume_ids_to_be_removed)) gfields = {'volume_ids':new_vids} try: multi_update(volume_ids_to_be_removed, g_id, new_rgs_set, gfields) except Exception as e: logging.error("Unable to update replica gateway %d. Exception %s" % (g_id, e)) session['message'] = "Unable to update gateway." return redirect('django_rg.views.viewgateway', g_id) session['new_change'] = "We've updated your RG's volumes." session['next_url'] = '/syn/RG/viewgateway/' + str(g_id) session['next_message'] = "Click here to go back to your gateway." return redirect('/syn/thanks') @authenticate @precheck("RG", PRECHECK_REDIRECT) def changelocation(request, g_id): ''' Handler for changing the host:port of the gateway. ''' session = request.session username = session['login_email'] g_id = int(g_id) form = gatewayforms.ModifyGatewayLocation(request.POST) if form.is_valid(): new_host = form.cleaned_data['host'] new_port = form.cleaned_data['port'] fields = {'host':new_host, 'port':new_port} # update and redirect try: db.update_replica_gateway(g_id, **fields) except Exception as e: logging.error("Unable to update RG: %d. Error was %s." % (g_id, e)) session['message'] = "Error. Unable to change replica gateway." return redirect('django_rg.views.viewgateway', g_id) session['new_change'] = "We've updated your RG." session['next_url'] = '/syn/RG/viewgateway/' + str(g_id) session['next_message'] = "Click here to go back to your gateway." return redirect('/syn/thanks') else: session['message'] = "Invalid form entries for gateway location." return redirect('django_rg.views.viewgateway', g_id) @authenticate def allgateways(request): ''' View to look at all RG's in a tabular format, with owners and attached volumes. ''' session = request.session username = session['login_email'] # Get gateways try: qry = db.list_replica_gateways() except: qry = [] gateways = [] for g in qry: gateways.append(g) # Get volumes and owners vols = [] g_owners = [] for g in gateways: volset = [] for v in g.volume_ids: volset.append(db.get_volume(v)) vols.append(volset) attrs = {"SyndicateUser.owner_id ==":g.owner_id} g_owners.append(db.get_user(attrs)) gateway_vols_owners = zip(gateways, vols, g_owners) t = loader.get_template('gateway_templates/allreplicagateways.html') c = RequestContext(request, {'username':username, 'gateway_vols_owners':gateway_vols_owners}) return HttpResponse(t.render(c)) @authenticate def create(request): ''' View to handle creation of RG's ''' session = request.session username = session['login_email'] user = db.read_user( username ) # Helper method used to simplify error-handling. When fields are entered incorrectly, # a session message is set and this method is called. def give_create_form(username, session): message = session.pop('message', "") form = gatewayforms.CreateRG() t = loader.get_template('gateway_templates/create_replica_gateway.html') c = RequestContext(request, {'username':username,'form':form, 'message':message}) return HttpResponse(t.render(c)) if request.POST: # Validate input forms form = gatewayforms.CreateRG(request.POST, request.FILES) if form.is_valid(): kwargs = {} # Try and load JSON config file/data, if present. First check uploaded files, then # the text box. if "json_config" in request.FILES: if request.FILES['json_config'].multiple_chunks(): session['message'] = "Uploaded file too large; please make smaller than 2.5M" return give_create_form(username, session) config = request.FILES['json_config'].read() try: kwargs['config'] = json.loads(config) except Exception as e: logging.info("Possible JSON load error: %s" % e) try: kwargs['config'] = json.loads("\"" + config + "\"") except Exception as e: logging.error("Definite JSON load error %s" % e) session['message'] = "Error parsing given JSON text." return give_create_form(username, session) # No upload, check text-box elif "json_config_text" in form.cleaned_data: try: kwargs['config'] = json.loads(form.cleaned_data['json_config_text']) except Exception as e: logging.info("Possible JSON load error: %s" % e) try: kwargs['config'] = json.loads("\"" + str(form.cleaned_data['json_config_text']) + "\"") except Exception as e: logging.error("Definite JSON load error %s" % e) session['message'] = "Error parsing given JSON text." return give_create_form(username, session) try: kwargs['ms_username'] = form.cleaned_data['g_name'] kwargs['port'] = form.cleaned_data['port'] kwargs['host'] = form.cleaned_data['host'] kwargs['ms_password'] = form.cleaned_data['g_password'] kwargs['private'] = form.cleaned_data['private'] new_RG = db.create_replica_gateway(user, **kwargs) except Exception as E: session['message'] = "RG creation error: %s" % E return give_create_form(username, session) session['new_change'] = "Your new gateway is ready." session['next_url'] = '/syn/RG/allgateways/' session['next_message'] = "Click here to see all replica gateways." return redirect('/syn/thanks/') else: # Prep returned form values (so they don't have to re-enter stuff) if 'g_name' in form.errors: oldname = "" else: oldname = request.POST['g_name'] if 'host' in form.errors: oldhost = "" else: oldhost = request.POST['host'] if 'port' in form.errors: oldport = "" else: oldport = request.POST['port'] oldjson = request.POST['json_config_text'] # Prep error message message = "Invalid form entry: " for k, v in form.errors.items(): message = message + "\"" + k + "\"" + " -> " for m in v: message = message + m + " " # Give then the form again form = gatewayforms.CreateRG(initial={'g_name': oldname, 'host': oldhost, 'port': oldport, 'json_config_text':oldjson, }) t = loader.get_template('gateway_templates/create_replica_gateway.html') c = RequestContext(request, {'username':username,'form':form, 'message':message}) return HttpResponse(t.render(c)) else: # Not a POST, give them blank form return give_create_form(username, session) @authenticate def delete(request, g_id): ''' View for deleting AG. ''' # Helper method used to simplify error-handling. When fields are entered incorrectly, # a session message is set and this method is called. def give_delete_form(username, g, session): form = gatewayforms.DeleteGateway() t = loader.get_template('gateway_templates/delete_replica_gateway.html') c = RequestContext(request, {'username':username, 'g':g, 'form':form, 'message':session.pop('message', "")}) return HttpResponse(t.render(c)) # Once again isolating transactional for views that update multiple entities @transactional(xg=True) def delete_and_update(rg_id, attached_volume_ids): db.delete_replica_gateway(rg_id) for v in attached_volume_ids: vol = db.read_volume(v) if not vol: continue new_rg_ids = vol.rg_ids new_rg_ids.remove(rg_id) attrs = {"rg_ids":new_rg_ids} db.update_volume(v, **attrs) session.pop("rg_initial_data" + str(g_id), None) session = request.session username = session['login_email'] g_id = int(g_id) rg = db.read_replica_gateway(g_id) if not rg: t = loader.get_template('gateway_templates/delete_replica_gateway_failure.html') c = RequestContext(request, {'username':username, 'g_name':g_name}) return HttpResponse(t.render(c)) if request.POST: # Validate input forms form = gatewayforms.DeleteGateway(request.POST) if form.is_valid(): if not RG.authenticate(rg, form.cleaned_data['g_password']): session['message'] = "Incorrect Replica Gateway password" return give_delete_form(username, rg, session) if not form.cleaned_data['confirm_delete']: session['message'] = "You must tick the delete confirmation box." return give_delete_form(username, rg, session) delete_and_update(g_id, rg.volume_ids) session['new_change'] = "Your gateway has been deleted." session['next_url'] = '/syn/RG/allgateways' session['next_message'] = "Click here to see all replica gateways." return redirect('/syn/thanks/') # invalid forms else: # Prep error message session['message'] = "Invalid form entry: " for k, v in form.errors.items(): session['message'] = session['message'] + "\"" + k + "\"" + " -> " for m in v: session['message'] = session['message'] + m + " " return give_delete_form(username, rg, session) else: # Not a POST, give them blank form return give_delete_form(username, rg, session) @csrf_exempt @authenticate def urlcreate(request, g_name, g_password, host, port, private=False, volume_name="",): ''' For debugging purposes only, allows creation of RG via pure URL ''' session = request.session username = session['login_email'] kwargs = {} kwargs['port'] = int(port) kwargs['host'] = host kwargs['ms_username'] = g_name kwargs['ms_password'] = g_password kwargs['private'] = private if volume_name: vol = db.get_volume_by_name(volume_name) if not vol: return HttpResponse("No volume %s exists." % volume_name) if (vol.volume_id not in user.volumes_r) and (vol.volume_id not in user.volumes_rw): return HttpResponse("Must have read rights to volume %s to create RG for it." % volume_name) kwargs['volume_ids'] = list(vol.volume_id) try: new_rg = db.create_replica_gateway(user, vol, **kwargs) except Exception as E: return HttpResponse("RG creation error: %s" % E) return HttpResponse("RG succesfully created: " + str(new_rg)) @csrf_exempt @authenticate def urldelete(request, g_name, g_password): ''' For debugging purposes only, allows deletion of RG via pure URL ''' session = request.session username = session['login_email'] attrs = {"ReplicaGateway.ms_username ==":g_name} rgs = db.list_replica_gateways(attrs, limit=1) if ags: rg = rgs[0] else: return HttpResponse("RG %s does not exist." % g_name) if not rg.authenticate(rg, g_password): return HttpResponse("Incorrect RG password.") db.delete_replica_gateway(g_name) return HttpResponse("Gateway succesfully deleted.")
39.089419
126
0.618895
0
0
0
0
24,878
0.948492
0
0
9,213
0.351252