def default(self, x): if isinstance(x, Struct): return dict(__JSON_Struct__=x.__json__()) elif isinstance(x, Feature): return dict(__JSON_Feature__=x.__json__()) else: return JSONEncoder.default(self, x)
def default(self, obj): if isinstance(obj, Game): return dict(pk = obj.pk, name = obj.name, password = obj.password, pot_size = obj.pot_size, iteration = obj.iteration, start = obj.start, end = obj.end, state = obj.state, player_count = len(obj.player_set.all()), ) elif isinstance(obj, Player): return dict(pk=obj.pk, email=obj.email, player_no=obj.player_no, pair_no=obj.pair_no, role=obj.role, role_name=obj.get_role_display(), earning=obj.earning if hasattr(obj, 'earning') else 0, round=obj.round if hasattr(obj, 'round') else 0 ) elif isinstance(obj, Bid): return dict(pk=obj.pk, iteration=obj.iteration, pot_size=obj.pot_size, offer=obj.offer, avg_offer=str(obj.avg_offer) if hasattr(obj, 'avg_offer') else '0', accept=obj.accept) elif isinstance(obj, datetime.datetime): return obj.isoformat() return JSONEncoder.default(self, obj)
def default(self, obj): if isinstance(obj, QuerySet): # `default` must return a python serializable # structure, the easiest way is to load the JSON # string produced by `serialize` and return it return json.loads(serialize('json', obj)) return JSONEncoder.default(self,obj)
def default(self, obj, **kwargs): if isinstance(obj, ObjectId): return str(obj) elif isinstance(obj, datetime.datetime): return json.dumps(obj, default=json_util.default) else: return JSONEncoder.default(obj, **kwargs)
def __input_loop(self, protocol, base): """ :type protocol server.WebSocketServerProtocol :param base: :return: """ json_encoder = JSONEncoder() yield from protocol.send(json_encoder.encode({"message": _GET_NICK})) nick = yield from protocol.recv() self._chat_api.enter_chat(nick) yield from protocol.send( json_encoder.encode({"message": _ADD, "text": ",".join(self._chat_api.nicks_in_chat)})) while self._chat_api.connection_opened: message = yield from protocol.recv() self._chat_api.say_to_chat(message)
def default(self, obj): if isinstance(obj, Game): return dict(pk = obj.pk, name = obj.name, password = obj.password, init_price = str(obj.init_price), init_qty = obj.init_qty, init_cash = str(obj.init_cash), period = obj.period, start = obj.start, end = obj.end, state = obj.state, portfolio_count = len(obj.portfolio_set.all()), ) elif isinstance(obj, Portfolio): return dict(pk=obj.pk, cash=str(obj.cash), cash_available=str(obj.cash_available), details=list(PortfolioDetail.objects.get_with_price(obj)), order = list(obj.order_set.all()), game_state = obj.game.state, ) elif isinstance(obj, PortfolioDetail): return dict(pk=obj.pk, stock=obj.stock, qty=obj.qty, qty_available=obj.qty_available, price=str(obj.price) if hasattr(obj, 'price') else None, ) elif isinstance(obj, Order): return dict(pk=obj.pk, type=obj.get_type_display(), stock=obj.stock, price=str(obj.price), market_price=obj.market_price, qty=obj.qty, match=obj.match, status=obj.get_status_display(), reason=obj.reason, created=obj.created, transaction = list(Transaction.objects.filter(Q(seller=obj) | Q(buyer=obj))) ) elif isinstance(obj, Transaction): return dict(pk=obj.pk, price=str(obj.price), qty=obj.qty, ) elif isinstance(obj, Market): return dict(pk=obj.pk, stock=obj.stock, price=str(obj.price), bid=str(obj.bid), ask=str(obj.ask), volume_last=obj.volume_last, volume_total=obj.volume_total, ) elif isinstance(obj, datetime.datetime): return obj.isoformat() return JSONEncoder.default(self, obj)
def __output_loop(self, protocol, base): """ :type protocol server.WebSocketServerProtocol :param base: :return: """ room_queue = self._chat_api.subscribe_to_chat() json_encoder = JSONEncoder() current_people = self._chat_api.nicks_in_chat while self._chat_api.connection_opened: message = yield from room_queue.get() yield from protocol.send(json_encoder.encode({"message": _TEXT, "text": message})) if current_people != self._chat_api.nicks_in_chat: current_people = self._chat_api.nicks_in_chat yield from protocol.send( json_encoder.encode({"message": _ADD, "text": ",".join(self._chat_api.nicks_in_chat)}))
def tojsonarrays(table, source=None, prefix=None, suffix=None, output_header=False, *args, **kwargs): """ Write a table in JSON format, with rows output as JSON arrays. E.g.:: >>> from petl import tojsonarrays, look >>> look(table) +-------+-------+ | 'foo' | 'bar' | +=======+=======+ | 'a' | 1 | +-------+-------+ | 'b' | 2 | +-------+-------+ | 'c' | 2 | +-------+-------+ >>> tojsonarrays(table, 'example.json') >>> # check what it did ... with open('example.json') as f: ... print f.read() ... [["a", 1], ["b", 2], ["c", 2]] Note that this is currently not streaming, all data is loaded into memory before being written to the file. Supports transparent writing to ``.gz`` and ``.bz2`` files. .. versionadded:: 0.11 """ encoder = JSONEncoder(*args, **kwargs) source = write_source_from_arg(source) if output_header: obj = list(table) else: obj = list(data(table)) with source.open_('wb') as f: if prefix is not None: f.write(prefix) for chunk in encoder.iterencode(obj): f.write(chunk) if suffix is not None: f.write(suffix)
def default(self, obj): # pylint: disable=method-hidden if isinstance(obj, (CourseDetails, CourseGradingModel)): return obj.__dict__ elif isinstance(obj, Location): return obj.dict() elif isinstance(obj, datetime.datetime): return Date().to_json(obj) else: return JSONEncoder.default(self, obj)
def default(self, obj): if isinstance(obj, CourseDetails) or isinstance(obj, course_grading.CourseGradingModel): return obj.__dict__ elif isinstance(obj, Location): return obj.dict() elif isinstance(obj, datetime.datetime): return Date().to_json(obj) else: return JSONEncoder.default(self, obj)
def encode_json(obj, inline=False, **kwargs): """Encode the given object as json. Supports objects that follow the `_asdict` protocol. See `parse_json` for more information. :param obj: A serializable object. :param bool inline: `True` to inline all resolvable objects as nested JSON objects, `False` to serialize those objects' addresses instead; `False` by default. :param **kwargs: Any kwargs accepted by :class:`json.JSONEncoder` besides `encoding` and `default`. :returns: A UTF-8 json encoded blob representing the object. :rtype: string :raises: :class:`ParseError` if there were any problems encoding the given `obj` in json. """ encoder = JSONEncoder(encoding='UTF-8', default=functools.partial(_object_encoder, inline=inline), **kwargs) return encoder.encode(obj)
def example_default(self, o): try: iterable = iter(o) except TypeError: pass else: return list(iterable) # Let the base class default method raise the TypeError return JSONEncoder.default(self, o)
def index(): if request.method == "POST": site_hash = sha1(request.form["url"]).hexdigest() zipf_profile = r.get(site_hash) if zipf_profile: return zipf_profile else: try: corpus = WebCorpus(request.form["url"]) except: abort(404) e = JSONEncoder() zipf_profile = e.encode(corpus.freq_list) r.set(site_hash, zipf_profile) r.expire(site_hash, 120) resp = make_response(zipf_profile) return resp elif request.method == "GET": return render_template("index.html")
def default(self, o): if isinstance(o, entry.Entry): return o.to_json() try: iterable = iter(o) except TypeError: pass else: return list(iterable) return JSONEncoder.default(self, o)
def default(self, o): try: iterable = iter(o) except TypeError: pass else: return list(iterable) try: return JSONEncoder.default(self, o) except TypeError: return str(o)
def default(self, obj): try: if isinstance(obj, Response): return { 'action': obj.action, 'success': obj.success, 'message': obj.message, 'responseData': obj.responseData } else: return JSONEncoder.default(obj) except Exception as e: pass
class JSONSerializer(CustomizableSerializer): """ Serializes objects using JSON (JavaScript Object Notation). See the :mod:`json` module documentation in the standard library for more information on available options. Certain options can resolve references to objects: * ``encoder_options['default']`` * ``decoder_options['object_hook']`` * ``decoder_options['object_pairs_hook']`` :param encoder_options: keyword arguments passed to :class:`~json.JSONEncoder` :param decoder_options: keyword arguments passed to :class:`~json.JSONDecoder` :param encoding: the text encoding to use for converting to and from bytes :param custom_type_codec: wrapper to use to wrap custom types after marshalling """ __slots__ = ('encoder_options', 'decoder_options', 'encoding', 'custom_type_codec', '_encoder', '_decoder', '_marshallers', '_unmarshallers') def __init__(self, encoder_options: Dict[str, Any] = None, decoder_options: Dict[str, Any] = None, encoding: str = 'utf-8', custom_type_codec: Union[JSONTypeCodec, str] = None) -> None: assert check_argument_types() super().__init__(resolve_reference(custom_type_codec) or JSONTypeCodec()) self.encoding = encoding self.encoder_options = encoder_options or {} self.encoder_options['default'] = resolve_reference(self.encoder_options.get('default')) self._encoder = JSONEncoder(**self.encoder_options) self.decoder_options = decoder_options or {} self.decoder_options['object_hook'] = resolve_reference( self.decoder_options.get('object_hook')) self.decoder_options['object_pairs_hook'] = resolve_reference( self.decoder_options.get('object_pairs_hook')) self._decoder = JSONDecoder(**self.decoder_options) def serialize(self, obj) -> bytes: return self._encoder.encode(obj).encode(self.encoding) def deserialize(self, payload: bytes): text_payload = payload.decode(self.encoding) return self._decoder.decode(text_payload) @property def mimetype(self): return 'application/json'
def tojson(table, source=None, *args, **kwargs): """ Write a table in JSON format. E.g.:: >>> from petl import tojson, look >>> look(table) +-------+-------+ | 'foo' | 'bar' | +=======+=======+ | 'a' | 1 | +-------+-------+ | 'b' | 2 | +-------+-------+ | 'c' | 2 | +-------+-------+ >>> tojson(table, 'example.json') >>> # check what it did ... import json >>> with open('example.json') as f: ... json.load(f) ... [{u'foo': u'a', u'bar': 1}, {u'foo': u'b', u'bar': 2}, {u'foo': u'c', u'bar': 2}] Note that this is currently not streaming, all data is loaded into memory before being written to the file. .. versionadded:: 0.5 """ encoder = JSONEncoder(*args, **kwargs) source = _write_source_from_arg(source) with source.open_('wb') as f: for chunk in encoder.iterencode(list(records(table))): f.write(chunk)
def __init__(self, encoder_options: Dict[str, Any] = None, decoder_options: Dict[str, Any] = None, encoding: str = 'utf-8', custom_type_codec: Union[JSONTypeCodec, str] = None) -> None: assert check_argument_types() super().__init__(resolve_reference(custom_type_codec) or JSONTypeCodec()) self.encoding = encoding self.encoder_options = encoder_options or {} self.encoder_options['default'] = resolve_reference(self.encoder_options.get('default')) self._encoder = JSONEncoder(**self.encoder_options) self.decoder_options = decoder_options or {} self.decoder_options['object_hook'] = resolve_reference( self.decoder_options.get('object_hook')) self.decoder_options['object_pairs_hook'] = resolve_reference( self.decoder_options.get('object_pairs_hook')) self._decoder = JSONDecoder(**self.decoder_options)
class BaseAsyncTasks(object): def __init__(self, priority=DEFAULT_JOB_PRIORITY, delay=0, ttr=DEFAULT_JOB_TTR): self._priority = priority self._delay = delay self._ttr = ttr self._jsonEncoder = JSONEncoder() ''' @function -> the reference to the annotated asynchronous business logic function @args -> a json serializable list of parameters @priority -> you can set a priority for your task, so the consumers can process it accordingly @delay -> you can set a delay, the default is 0 @ttr -> ttr value. see: http://github.com/kr/beanstalkd/wiki/faq ''' def run(self, function, args): logger = logging.getLogger('BaseAsyncTasks.run') if settings.BEANSTALK_ENABLED : beanstalkClient = BeanstalkClient() module = function.__module__.__self__.app # Add more metadata to the args args['__uuid__'] = str(uuid4()).replace('-', '') func = ".".join([module, function.__name__]) body = self._jsonEncoder.encode(args) pid = beanstalkClient.call(func, body, priority=self._priority, delay=self._delay, ttr=self._ttr) try : if not pid >= 0: logger.critical("Failed to execute task: " + str(function) + " " + str(args)) JobRecord.objects.create(jid=pid, tube=func, body=body, uuid=args['__uuid__'], status=JobStatus.FAILED_TO_REGISTER) else: JobRecord.objects.create(jid=pid, tube=func, body=body, uuid=args['__uuid__'], status=JobStatus.READY) except Exception, ex: logger.exception('Error while persisting Job object. %s' % str(ex)) else :
def json_method(*arg, **kwargs): value = method(*arg, **kwargs) return JSONEncoder().encode(value)
import re import logging import flask from flask_login import LoginManager, current_user, login_user, logout_user, login_required import yaml import app.form as forms from json.encoder import JSONEncoder import snake.handler as sh from app.database import db import app.user as u import app.game as g from app.user.constants import LOGIN_REGEX json_encoder = JSONEncoder() app = flask.Flask(__name__, static_folder="./templates/", template_folder="./templates/") app.config['SECRET_KEY'] = 'ITS IS NOT SECRET KEY' login_manager = LoginManager() login_manager.init_app(app) login_manager.login_view = "login_page" # Логгер logger = logging.getLogger("main") logger.setLevel(logging.INFO) logger_handler = logging.FileHandler('log.txt', encoding='utf-8') logger_handler.setLevel(logging.INFO) logger_formatter = logging.Formatter(
def exit(self, msg=None, code=403): response = {'id': 'PERM_DENIED', 'msg': msg} self.set_status(code) self.write(JSONEncoder().encode(response)) self.finish()
l1 = [] l2 = [] l3 = [] entity = c #phrase.split(' ') for d in entity: #for i in data: for i in opdata: for j in i: l1.append(d) l2.append(j) #l3.append(modell.similarity(d, j)) l3.append(opmodell.similarity(d, j)) from json.encoder import JSONEncoder import numpy as numpy if len((l3)) == 0: #print ("error") final_entity = {"cat": ["O"]} # directly called encode method of JSON print(JSONEncoder().encode(final_entity)) else: posq2 = numpy.argmax(l3) topred = l2[posq2] #print(loaded_model.predict(vect.transform([topred]))) arr = loaded_model2.predict(([topred])) # import JSONEncoder class from json from json.encoder import JSONEncoder final_entity = {"cat": [arr[0]]} # directly called encode method of JSON print(JSONEncoder().encode(final_entity))
def _object_encoder(o): if not Serializable.is_serializable(o): raise ParseError( 'Can only encode Serializable objects in JSON, given {!r} of type {}' .format(o, type(o).__name__)) encoded = o._asdict() if 'typename' not in encoded: encoded['typename'] = '{}.{}'.format( inspect.getmodule(o).__name__, type(o).__name__) return encoded encoder = JSONEncoder(encoding='UTF-8', default=_object_encoder, sort_keys=True, indent=True) def parse_json(json, symbol_table=None): """Parses the given json encoded string into a list of top-level objects found. The parser accepts both blank lines and comment lines (those beginning with optional whitespace followed by the '#' character) as well as more than one top-level JSON object. The parse also supports a simple protocol for serialized types that have an `_asdict` method. This includes `namedtuple` subtypes as well as any custom class with an `_asdict` method defined; see :class:`pants.engine.exp.serializable.Serializable`. :param string json: A json encoded document with extra support for blank lines, comments and multiple top-level objects.
def _send_json(self, req, data, status=200): content = JSONEncoder().encode(data).encode('utf-8') req.send(content, 'application/json', status)
import json x = [1, "a", True] print("Original Data :\n", x) k = json.dumps(x) print("\nData after Dumping(serializinf :\n", k) data_to_write = "*******************" path = "C:\\Users\\Umair.ilahi\\python_workspace\\Python_Bootcamp_Full_Syllabus\\FileHandling\\" with open(path + "demo_write.txt", "w") as f: json.dump(x, f) f.closed path = "C:\\Users\\Umair.ilahi\\python_workspace\\Python_Bootcamp_Full_Syllabus\\FileHandling\\" with open(path + "demo_write.txt") as f: z = json.load(f) f.closed print("\nAfter reconstructing Data from File :\n", z) colour_dict = {"colour": ["red", "yellow", "green"]} print("\nOriginal color_dict:\n", colour_dict) # directly called encode method of JSON aa = JSONEncoder().encode(colour_dict) print("\n After using JSONEncoding :\n", aa) bb = JSONDecoder().decode(aa) print("\n After using JSONDecoding :\n", bb)
def default(self, obj): if hasattr(obj, 'isoformat'): return obj.isoformat() # Let the base class default method deal with others return JSONEncoder.default(self, obj)
def result(request): #nm=request.GET['url'] try: text = request.GET['url'] if text.startswith('https://') or text.startswith('http://'): if len(text) <= 9: return render(request, 'errorpage.html') aburl = -1 digits = "0123456789" if text[8] in digits: oneval = -1 else: oneval = 1 if len(text) > 170: secval = -1 else: secval = 1 if "@" in text: thirdval = -1 else: thirdval = 1 k = text.count("//") if k > 1: fourthval = -1 else: fourthval = 1 if "-" in text: fifthval = -1 else: fifthval = 1 if "https" in text: sixthval = 1 else: sixthval = -1 temp = text temp = temp[6:] k1 = temp.count("https") if k1 >= 1: seventhval = -1 else: seventhval = 1 if "about:blank" in text: eighthval = -1 else: eighthval = 1 if "mail()" or "mailto:" in text: ninthval = -1 else: ninthval = 1 re = text.count("//") if re > 3: tenthval = -1 else: tenthval = 1 import whois from datetime import datetime url = text try: res = whois.whois(url) try: a = res['creation_date'][0] b = datetime.now() c = b - a d = c.days except: a = res['creation_date'] b = datetime.now() c = b - a d = c.days if d > 365: eleventhval = 1 else: eleventhval = -1 except: aburl = 1 eleventhval = -1 if aburl == 1: twelthval = -1 else: twelthval = 1 filename = 'phish_trainedv0.sav' loaded_model = joblib.load(filename) arg = loaded_model.predict(([[ oneval, secval, thirdval, fourthval, fifthval, sixthval, seventhval, eighthval, ninthval, tenthval, eleventhval, twelthval ]])) print(arg[0]) if arg[0] == 1: te = "Legitimate" else: te = "Malicious" if arg[0] == 1: mal = True else: mal = False from json.encoder import JSONEncoder final_entity = {"predicted_argument": [int(arg[0])]} # directly called encode method of JSON print(JSONEncoder().encode(final_entity)) return render( request, 'result.html', { 'result': 'Real-time analysis successfull', 'f2': te, 'mal': mal, 'text': text }) else: return render(request, 'errorpage.html') except: return render(request, 'errorpage.html')
def default(self, obj): for cls in type(obj).__mro__: if cls in _convert_json: return _convert_json[cls](obj) return JSONEncoder.default(self, obj)
def default(self, o): if isinstance(o, Rat): return str(repr(o)) return JSONEncoder.default(self, o)
if isinstance(obj, set): return repr(list(obj)) elif hasattr(obj, 'isoformat'): return obj.isoformat() else: raise TypeError(repr(obj) + " is not JSON serializable") # Pre-load encoder/decoder instances with the fastest possible performance and # most compact encoding. The decoder default is pretty much it; for the # encoder, the extra whitespaces have been eliminated and the circular # reference check deactivated. Finally, datetime objects are serialized, too. DECODER = JSONDecoder() ENCODER = JSONEncoder(check_circular=False, separators=(',', ':'), allow_nan=False, default=IsoformatSerializer) def Decode(string: str) -> object: """ Decode a JSON *string* to a Python object. Contrary to :func:`.Encode`, it does not de-serialize date and time strings to `datetime` objects. """ return DECODER.decode(string) def Encode(obj: object) -> str: """
import json from json.encoder import JSONEncoder version_json = """ { "date": "2020-10-29T07:57:53+0000", "dirty": false, "error": null, "full-revisionid": "539478470b53bd3e96473b411e1322bb158fd444", "version": "0.12.1" } """ result = json.loads(version_json) print(type(result)) for index, val in enumerate(result): print("%s---%s" % (index, val)) print(result["version"]) print(JSONEncoder.encode(result))
def default(self, o): if not isinstance (o, Authentication): return JSONEncoder.default(self, o); return {"username":o.username, "password":o.password}
def get_json(self): json_encoder = JSONEncoder() result = json_encoder.encode(str(self.__dict__)) return result
def encode(self, o): pickle_bytes = pickle.dumps(o) pickle_str = binascii.b2a_qp(pickle_bytes).decode(encoding="utf8") o = {JSONCodec._obj: pickle_str, JSONCodec._ver_key: JSONCodec._ver} return JSONEncoder.encode(self, o)
def default(self, o): if hasattr(o, "dtype") and o.dtype.kind == "i": return int(o) else: return JSONEncoder.default(self, o)
from transaction import Transaction from merkleproof import MerkleProof from time import time import threading import uuid import pickle # Passer les params de creation de process : Port, Parent Node (Port nullable) # If Parent Node != null # Create websocket connection with parent # Start to listen and send events with parent / children JSON_ENCODER = JSONEncoder() JSON_DECODER = JSONDecoder() HOST = "127.0.0.1" lock = threading.Lock() bc = BlockChain() bc_l = 0 PORT = None ADDRESS_WALLET = uuid.uuid4().hex transction_tbd = [] def mineBlock(transactions): global bc global bc_l nonce = 1
def default(self, obj): if isinstance(obj, User): return obj.__dict__ return JSONEncoder.default(self, obj)
def __init__(self, injection_mark): JSONEncoder.__init__(self) self.injection_mark = injection_mark
def to_json(self): """ Convert the Spin, represented as a tree, to a JSON string """ return JSONEncoder().encode(self.to_dict())
def post(self, request, *args, **kwargs): form = BookingForm(request.POST) platos_req = request.POST.get('platos') platos_req = json.loads(platos_req) servicios_req = request.POST.get('servicios') servicios_req = json.loads(servicios_req) print(platos_req) print(servicios_req) # for req in platos_req: # print(req) # print(req.get('id')) # # return HttpResponse() key = kwargs.get('bsk64') business = Business.objects.get_by_widget_sk(key) if business: # print(request.POST) if form.is_valid(): print(form.cleaned_data) message = "" try: with transaction.atomic(): # state = N_State.objects.get(pk=request.POST.get('cliente_state')) customer = Customer.objects.check_customer_from_external_source( data=form.cleaned_data, business=business) # state = N_State.objects.filter(id=data.get('state')).first() event = Event.objects.create_event_from_external_source( data=form.cleaned_data, customer=customer, req_platos=platos_req, req_servicios=servicios_req, ) if event.is_scheduled(): message = "Felicidades tenemos disponibilidad y le hemos enviado su Proposal al corro que nos dio!" else: message = "Hemos recibido su orden y la estamos procesando, en unos minutos tendrá noticias nuestras!" except CustomerDataError: errors = { "customer_error": [{ "message": 'Error al procesar los datos personales' }] } return JsonResponse(JSONEncoder().encode(errors), status=400, safe=False) except Exception as e: print(e) errors = { "global_error": [{ "message": 'Error al procesar los datos' }] } return JsonResponse(JSONEncoder().encode(errors), status=400, safe=False) return JsonResponse({'message': message}) else: print(form.errors.as_json()) return JsonResponse(form.errors.as_json(), status=400, safe=False) else: return HttpResponseBadRequest()
def default(self, obj): if isinstance(obj, ObjectId): return str(obj) if isinstance(obj, datetime): return obj.strftime("%Y-%m-%d %H:%M:%S") return JSONEncoder.default(self, obj)
def to_str(self): return JSONEncoder(ensure_ascii=False).encode({ u"errorCode": self.errorCode, u"errorMsg": self.errorMsg, u"apiName": self.apiName })
class JSONSerializer(serialize_abcs.CustomizableSerializer): """ Serializes objects using JSON (JavaScript Object Notation). See the :mod:`json` module documentation in the standard library for more information on available options. Certain options can resolve references to objects: * ``encoder_options['default']`` * ``decoder_options['object_hook']`` * ``decoder_options['object_pairs_hook']`` :param encoder_options: keyword arguments passed to :class:`~json.JSONEncoder` :param decoder_options: keyword arguments passed to :class:`~json.JSONDecoder` :param encoding: the text encoding to use for converting to and from bytes :param custom_type_key: magic key that identifies custom types in a JSON object """ __slots__ = ('encoder_options', 'decoder_options', 'encoding', 'custom_type_key', '_encoder', '_decoder', '_marshallers', '_unmarshallers') def __init__(self, encoder_options: Dict[str, Any] = None, decoder_options: Dict[str, Any] = None, encoding: str = 'utf-8', custom_type_key: str = '__type__'): self.encoding = encoding self.custom_type_key = custom_type_key self._marshallers = OrderedDict() # class -> (typename, marshaller function) self._unmarshallers = OrderedDict() # typename -> (class, unmarshaller function) self.encoder_options = encoder_options or {} self.encoder_options['default'] = resolve_reference(self.encoder_options.get('default')) self._encoder = JSONEncoder(**self.encoder_options) self.decoder_options = decoder_options or {} self.decoder_options['object_hook'] = resolve_reference( self.decoder_options.get('object_hook')) self.decoder_options['object_pairs_hook'] = resolve_reference( self.decoder_options.get('object_pairs_hook')) self._decoder = JSONDecoder(**self.decoder_options) def serialize(self, obj) -> bytes: return self._encoder.encode(obj).encode(self.encoding) def deserialize(self, payload: bytes): payload = payload.decode(self.encoding) return self._decoder.decode(payload) def register_custom_type( self, cls: type, marshaller: Optional[Callable[[Any], Any]] = default_marshaller, unmarshaller: Optional[Callable[[Any, Any], Any]] = default_unmarshaller, *, typename: str = None) -> None: typename = typename or qualified_name(cls) if marshaller: self._marshallers[cls] = typename, marshaller self.encoder_options['default'] = self._default_encoder self._encoder = JSONEncoder(**self.encoder_options) if unmarshaller: self._unmarshallers[typename] = cls, unmarshaller self.decoder_options['object_hook'] = self._custom_object_hook self._decoder = JSONDecoder(**self.decoder_options) def _default_encoder(self, obj): obj_type = obj.__class__ try: typename, marshaller = self._marshallers[obj_type] except KeyError: raise LookupError('no marshaller found for type "{}"' .format(obj_type.__class__.__name__)) from None state = marshaller(obj) return {self.custom_type_key: typename, 'state': state} def _custom_object_hook(self, obj: Dict[str, Any]): if len(obj) == 2 and self.custom_type_key in obj: typename = obj[self.custom_type_key] try: cls, unmarshaller = self._unmarshallers[typename] except KeyError: raise LookupError('no unmarshaller found for type "{}"'.format(typename)) from None instance = cls.__new__(cls) unmarshaller(instance, obj['state']) return instance else: return obj @property def mimetype(self): return 'application/json'
def default(self, o): if type(o) is UUID: return str(o) # Let the base class default method raise the TypeError return JSONEncoder.default(self, o)
class Trainer(object): categories = {} def __init__(self): self._jsonDecoder = JSONDecoder() self._jsonEncoder = JSONEncoder() self.__featureExtractor = FeatureExtractor() def setFeatureExtractor(self, featureExtractor): self.__featuredExtractor = featureExtractor def __isNumeric(self, feature): isNumeric = False try: float(feature) isNumeric = True except ValueError: pass return isNumeric """ Given a list of yes category names, retrieves the yes/no category hash that the trainer needs """ def __getCategoriesFromNames(self, yesTagNames, noTagNames): finalCategories = [] # create the categories if they don't already exist for tagName in yesTagNames: if tagName: categoryYes, _ = ClassifierCategory.objects.get_or_create(categoryName=tagName, yes=True) categoryNo, _ = ClassifierCategory.objects.get_or_create(categoryName=tagName, yes=False) finalCategories.append(categoryYes) for tagName in noTagNames: if tagName: categoryYes, _ = ClassifierCategory.objects.get_or_create(categoryName=tagName, yes=True) categoryNo, _ = ClassifierCategory.objects.get_or_create(categoryName=tagName, yes=False) finalCategories.append(categoryNo) return finalCategories """ Trains a corpus of data. """ @transaction.commit_manually def train(self, corpus="", yesTagNames=None, noTagNames=None): logger = logging.getLogger("Trainer.train") success = False categories = [] try: document = Document.getDocumentByCorpus(corpus) if not document: features = self.__featuredExtractor.getFeatures(corpus) categories = self.__getCategoriesFromNames(yesTagNames, noTagNames) document = Document(corpus=corpus) document.save() documentCounts = {} for category in categories: self.__incrementCategoryCount(documentCounts, category) DocumentCategoryCounts(document=document, countData=self._jsonEncoder.encode(documentCounts)).save() for feature in features: featureCount, _ = FeatureCounts.objects.get_or_create(featureName=feature) counts = self._jsonDecoder.decode(featureCount.countData) if featureCount.countData else {} for category in categories: self.__incrementCategoryCount(counts, category) featureCount.countData = self._jsonEncoder.encode(counts) featureCount.save() # We keep an index of category document counts for faster classification later on catDocCountIndex = CategoryDocumentCountIndex.getCountIndex() index = self._jsonDecoder.decode(catDocCountIndex.countData) if catDocCountIndex.countData else {} for category in categories: self.__incrementCategoryCount(index, category) catDocCountIndex.countData = self._jsonEncoder.encode(index) catDocCountIndex.save() success = True transaction.commit() else: logger.info("Document already exists: " + str(document.id) + " - " + document.corpusHash) success = True except Exception, ex: logger.info("Bad data:%s" % corpus) logger.exception("Failed to save the trained data: " + str(ex)) transaction.rollback() return success
def get_task_config(request,task_id): ''' index page''' task_config=CITaskConfigService.get_ci_task_config_by_taskid(task_id) json_encoder=JSONEncoder() return HttpResponse(json_encoder.encode(str(task_config)))
def __init__(self): self._jsonDecoder = JSONDecoder() self._jsonEncoder = JSONEncoder() self.__featureExtractor = FeatureExtractor()
language_path = os.path.dirname(sys.argv[1]) language_file = xml.parse(sys.argv[1]) else: language_path = os.path.join(os.path.abspath(os.path.dirname(sys.argv[0])), 'site', 'data') language_file = xml.parse(os.path.join(language_path, 'language.xml')) # get all languages output = {} for language in language_file.getElementsByTagName('language'): code = language.attributes['short'].value output[code] = {} for constant in language.getElementsByTagName('constant'): constant_name = constant.attributes['name'].value if constant.firstChild: output[code][constant_name] = constant.firstChild.data else: output[code][constant_name] = '' # save output files encoder = JSONEncoder(**encoder_options) for code, language in output.items(): with open(os.path.join(language_path, 'language_{0}.json'.format(code)), 'w') as raw_file: raw_file.write(encoder.encode(language).encode('utf8'))
def default(self, o): if isinstance(o, JsonSerializable): return o.__dict__ return JSONEncoder.default(self, o)
def result(request): """try:""" #nm=request.GET['url'] text = request.GET['url'] if text.startswith('https://') or text.startswith('http://'): if len(text) <= 9: return render(request, 'errorpage.html') aburl = -1 digits = "0123456789" if text[8] in digits: oneval = -1 else: oneval = 1 if len(text) > 170: secval = -1 else: secval = 1 if "@" in text: thirdval = -1 else: thirdval = 1 k = text.count("//") if k > 1: fourthval = -1 else: fourthval = 1 if "-" in text: fifthval = -1 else: fifthval = 1 if "https" in text: sixthval = 1 else: sixthval = -1 temp = text temp = temp[6:] k1 = temp.count("https") if k1 >= 1: seventhval = -1 else: seventhval = 1 if "about:blank" in text: eighthval = -1 else: eighthval = 1 if "mail()" or "mailto:" in text: ninthval = -1 else: ninthval = 1 re = text.count("//") if re > 3: tenthval = -1 else: tenthval = 1 import whois from datetime import datetime url = text try: res = whois.whois(url) try: a = res['creation_date'][0] b = datetime.now() c = b - a d = c.days except: a = res['creation_date'] b = datetime.now() c = b - a d = c.days if d > 365: eleventhval = 1 else: eleventhval = -1 except: aburl = -1 eleventhval = -1 if aburl == -1: twelthval = -1 else: twelthval = 1 import urllib.request, sys, re import xmltodict, json try: xml = urllib.request.urlopen( 'http://data.alexa.com/data?cli=10&dat=s&url={}'.format( text)).read() result = xmltodict.parse(xml) data = json.dumps(result).replace("@", "") data_tojson = json.loads(data) url = data_tojson["ALEXA"]["SD"][1]["POPULARITY"]["URL"] rank = int(data_tojson["ALEXA"]["SD"][1]["POPULARITY"]["TEXT"]) print("rank", rank) if rank <= 100000: thirt = 1 else: thirt = -1 print(thirt) except: thirt = -1 rank = "Not Indexed by Alexa" print(rank) filename = 'phish_trainedv3.sav' loaded_model = joblib.load(filename) arg = loaded_model.predict(([[ oneval, secval, thirdval, fourthval, fifthval, seventhval, eighthval, ninthval, tenthval, eleventhval, twelthval, thirt ]])) print(arg[0]) import whois url = text #print (res) try: res = whois.whois(url) name = res["name"] print(res["name"]) org = res['org'] print(res['org']) add = res['address'] print(res['address']) city = res['city'] print(res['city']) state = res['state'] print(res['state']) ziip = res['zipcode'] print(res['zipcode']) country = res['country'] print(res['country']) emails = res["emails"][0] print(res["emails"][0]) dom = res['domain_name'] print(res['domain_name']) except: name = "Not found in database" org = "Not found in database" add = "Not found in database" city = "Not found in database" state = "Not found in database" ziip = "Not found in database" country = "Not found in database" emails = "Not found in database" dom = "Not Found" if dom == "Not Found" and rank == "Not Indexed by Alexa": arg[0] = -1 #phishing if arg[0] == 1: te = "Legitimate" else: te = "Malicious" if arg[0] == 1: mal = True else: mal = False print(name, org, add, city, state, ziip, country, emails, dom) from json.encoder import JSONEncoder final_entity = {"predicted_argument": [int(arg[0])]} # directly called encode method of JSON print(JSONEncoder().encode(final_entity)) print(dom, rank) tags = [name, org, state, add, city, ziip, country, emails, dom, rank] tags = list(filter(lambda x: x != "Not Found", tags)) tags.append(text) obj = Url() obj.link = text obj.add = res['address'] obj.state = res['state'] obj.city = res['city'] #obj.ziip = res['zip_code'] obj.result = te obj.country = res['country'] obj.emails = res['emails'] obj.dom = res['domain_name'] obj.org = res['org'] obj.rank = rank obj.save() return render( request, 'result.html', { 'result': 'Real-time analysis successfull', 'f2': te, 'mal': mal, 'text': text, 'name': name, 'org': org, 'add': add, 'city': city, 'state': state, 'ziip': ziip, 'country': country, 'emails': emails, 'dom': dom, 'rank': rank, "tags": tags }) else: return render(request, '404.html') """except:
def default(self, o): # 这个 o 就是每一个 BaseResponse 的实例对象。 if isinstance(o, BaseResponse): # print(o.__dict__) return o.__dict__ # 取出对象的内置字典中的数据 return JSONEncoder.default(self, o) # 然后进行序列化。
gs = line.split(",") assert len(gs) == 4 doc = mongo_docs.find_one({"url":gs[1]}) if doc and 'sent_baidu_img_res' in doc and doc['sent_baidu_img_res'] and 'data' in doc['sent_baidu_img_res'] and doc['sent_baidu_img_res']['data']: for i, image_info in enumerate(doc['sent_baidu_img_res']['data']): d_int, d_str = {}, {} if 'thumbURL' not in image_info: continue for key, value in image_info.items(): if value: if type(value) is int: d_int[key] = value if type(value) is str: d_str[key] = value d_str["source_mingju"] = gs[0] d_str["source_mingju_url"] = gs[1] d_str["source_mingju_author_title"] = gs[2] d_str["source_mingju_poem_url"] = gs[3] d_int['bdDisplayNum'] = doc['sent_baidu_img_res'].get('displayNum', 0) d = { "image_url" : image_info['thumbURL'], "add_string_info" : d_str, "add_int_info" : d_int } base64_queue_client.send_message(JSONEncoder().encode(d).encode('utf-8')) if doc: doc['crawled'] = int(time.time()) mongo_docs.update_one({'url':gs[1]}, {"$set":doc}) print(idx, gs[0], "Done")
def __shutdown(self): beanstalkClient = BeanstalkClient() jsonEncoder = JSONEncoder() args = {'timestamp' : time.time()} pid = beanstalkClient.call("shutdown", jsonEncoder.encode(args), priority=0) #high pri job
def default(self, o): if isinstance(o, BaseResponse): return o.__dict__ return JSONEncoder.default(self, o)
def api(request): try: text = request.GET['query'] if text.startswith('https://') or text.startswith('http://'): if len(text) <= 9: return render(request, 'errorpage.html') aburl = -1 digits = "0123456789" if text[8] in digits: oneval = -1 else: oneval = 1 if len(text) > 170: secval = -1 else: secval = 1 if "@" in text: thirdval = -1 else: thirdval = 1 k = text.count("//") if k > 1: fourthval = -1 else: fourthval = 1 if "-" in text: fifthval = -1 else: fifthval = 1 if "https" in text: sixthval = 1 else: sixthval = -1 temp = text temp = temp[6:] k1 = temp.count("https") if k1 >= 1: seventhval = -1 else: seventhval = 1 if "about:blank" in text: eighthval = -1 else: eighthval = 1 if "mail()" or "mailto:" in text: ninthval = -1 else: ninthval = 1 re = text.count("//") if re > 3: tenthval = -1 else: tenthval = 1 import whois from datetime import datetime url = text try: res = whois.whois(url) try: a = res['creation_date'][0] b = datetime.now() c = b - a d = c.days except: a = res['creation_date'] b = datetime.now() c = b - a d = c.days if d > 365: eleventhval = 1 else: eleventhval = -1 except: aburl = -1 eleventhval = -1 if aburl == -1: twelthval = -1 else: twelthval = 1 import urllib.request, sys, re import xmltodict, json try: xml = urllib.request.urlopen( 'http://data.alexa.com/data?cli=10&dat=s&url={}'.format( text)).read() result = xmltodict.parse(xml) data = json.dumps(result).replace("@", "") data_tojson = json.loads(data) url = data_tojson["ALEXA"]["SD"][1]["POPULARITY"]["URL"] rank = int(data_tojson["ALEXA"]["SD"][1]["POPULARITY"]["TEXT"]) print("rank", rank) if rank <= 100000: thirt = 1 else: thirt = -1 print(thirt) except: thirt = -1 rank = "Not Indexed by Alexa" print(rank) filename = 'phish_trainedv3.sav' loaded_model = joblib.load(filename) arg = loaded_model.predict(([[ oneval, secval, thirdval, fourthval, fifthval, seventhval, eighthval, ninthval, tenthval, eleventhval, twelthval, thirt ]])) print(arg[0]) import whois url = text #print (res) try: res = whois.whois(url) name = res["name"] print(res["name"]) org = res['org'] print(res['org']) add = res['address'] print(res['address']) city = res['city'] print(res['city']) state = res['state'] print(res['state']) ziip = res['zipcode'] print(res['zipcode']) country = res['country'] print(res['country']) emails = res["emails"][0] print(res["emails"][0]) dom = res['domain_name'] print(res['domain_name']) except: name = "Not Found" org = "Not Found" add = "Not Found" city = "Not Found" state = "Not Found" ziip = "Not Found" country = "Not Found" emails = "Not Found" dom = "Not Found" if dom == "Not Found" and rank == "Not Indexed by Alexa": arg[0] = -1 #phishing if arg[0] == 1: te = "Legitimate" else: te = "Malicious" if arg[0] == 1: mal = True else: mal = False if arg[0] == 1: malstatus = False else: malstatus = True from json.encoder import JSONEncoder final_entity = {"predicted_argument": [int(arg[0])]} # directly called encode method of JSON print(JSONEncoder().encode(final_entity)) print(dom, rank) res = whois.whois(url) obj = Url() obj.link = res["name"] print(res["name"]) obj.org = res['org'] print(res['org']) obj.add = res['address'] print(res['address']) obj.city = res['city'] print(res['city']) obj.state = res['state'] print(res['state']) print(res['zipcode']) obj.country = res['country'] print(res['country']) obj.emails = res["emails"][0] print(res["emails"][0]) obj.dom = res['domain_name'] print(res['domain_name']) obj.rank = rank obj.save() '''return render(request, 'result.html', {'result': 'Real-time analysis successfull', 'f2': te, 'mal': mal,'text':text})''' import datetime mydict = { "query": url, "malware": malstatus, "datetime": str(datetime.datetime.now()) } response = JsonResponse(mydict) return response except: return render(request, '404.html')
def __init__(self, priority=DEFAULT_JOB_PRIORITY, delay=0, ttr=DEFAULT_JOB_TTR): self._priority = priority self._delay = delay self._ttr = ttr self._jsonEncoder = JSONEncoder()
# load original language file if len(sys.argv) > 1: language_path = os.path.dirname(sys.argv[1]) language_file = xml.parse(sys.argv[1]) else: language_path = os.path.join(os.path.abspath(os.path.dirname(sys.argv[0])), 'site', 'data') language_file = xml.parse(os.path.join(language_path, 'language.xml')) # get all languages output = {} for language in language_file.getElementsByTagName('language'): code = language.attributes['short'].value output[code] = {} for constant in language.getElementsByTagName('constant'): constant_name = constant.attributes['name'].value if constant.firstChild: output[code][constant_name] = constant.firstChild.data else: output[code][constant_name] = '' # save output files encoder = JSONEncoder(**encoder_options) for code, language in output.items(): with open(os.path.join(language_path, 'language_{0}.json'.format(code)), 'w') as raw_file: raw_file.write(encoder.encode(language).encode('utf8'))