首页 \ 问答 \ 对象是否存在本机类型安全包装和拆箱?(Is there native type-safe wrapping and unboxing for Object?)

对象是否存在本机类型安全包装和拆箱?(Is there native type-safe wrapping and unboxing for Object?)

我的查询是关于包装,使用Object类作为我的包装器。

我的目标是获取原始int(或int的包装)并从通用Object中取消它。

唯一的问题是我将在运行时使用混合类型,因此我无法一般地键入方法的返回类型。 T var1!=(T)var2

我现在可以做的是有过多的开销,因为我不能假设Object包含int类型的值,所以我解析它。

Object int_wrapped_in_object = 1;

Integer.valueOf(String.valueOf(int_wrapped_in_object));

//I could cast from Object to int with:
//(int) int_wrapped_in_object
//but this will always anticipate a classcastexception in case the value was wrapped with another type, like String

这是有效的,但我最好跳过解析步骤,只需打开整数值框。

整数包装类不支持valueOf(Object o) ,可能是因为Object没有实现相对强制转换。 它(Object)支持toString() 。 但不是toInt()。 它非常奇特,因为Object确实可以包装一个原始int。 为什么会出现这种情况超出我的知识水平,所以我只能使用我的工作。 据我所知,甚至可能有原生支持将Object作为int展开。

请帮助我找到一个解决方案,该解决方案涉及使用最少的解析并引入尽可能少的异常来获取原始int值。


My inquiry is on wrapping, using the Object class as my wrapper.

My goal is to take the primitive int (or a wrapper of int) and unbox it from a generic Object.

The only problem is that I will be working with mixed types at runtime, so I am unable to generically type the return type of the method. T var1 != (T) var2

What I can do right now has excessive overhead because I cannot assume the Object wraps a value of type int, so I parse it.

Object int_wrapped_in_object = 1;

Integer.valueOf(String.valueOf(int_wrapped_in_object));

//I could cast from Object to int with:
//(int) int_wrapped_in_object
//but this will always anticipate a classcastexception in case the value was wrapped with another type, like String

This works, but I would ideally like to skip a step of parsing and just unwrap box for the integer value.

Integer wrapper class does not support valueOf(Object o), likely because Object does not implement the relative cast. It (Object) supports toString(). But not toInt(). It is very peculiar because Object indeed can wrap a primitive int. Why that is the case is above my level of knowledge, so I can only work with what I have. For all I know there may even be native support for unwrapping an Object as an int.

Please help me find a solution that involves using minimal parsing and introducing as few exceptions to get the primitive int value.


原文:https://stackoverflow.com/questions/45699699
更新时间:2023-11-09 18:11

最满意答案

我认为你并没有从正确的方向接近这一点,因为你把你的应用程序作为myanimelist.net的代理太多了。

当你有2000个用户时会发生什么? 您的应用程序最终会对myanimelist.net发出大量请求,并且一个不太重要的用户可能会拒绝您的应用程序(或将其用于DoS myanimelist.net)。

这是一个更清洁的方式恕我直言:

  • 服务器端 :

    • 创建一个websocket服务器(例如: https//github.com/aaugustin/websockets/blob/master/example/server.py
    • 当用户连接到websocket服务器时,将客户端添加到列表中,在断开连接时将其从列表中删除。
    • 对于每个连接用户,请经常检查myanimelist.net以获取关联的xml(可能会降低更多在线用户的频率)
    • 对于每个xml文档,使用您的服务器本地版本进行差异化,并使用websocket通道将该差异发送到客户端(假设有差异)。
  • 客户端 :
    • 在收到差异:更新本地XML与差异。
    • 在停用n秒之后断开与websocket的连接+断开连接时在接口上添加一个按钮以重新连接

我怀疑你可以做更好的事情,假设myanimelist.net不提供“推送”API。


I think you aren't approaching this from the right direction because you place your app too much as a proxy of myanimelist.net.

What happens when you have 2000 users? Your app end up doing tons of requests to myanimelist.net, and a mean user could definitely DoS your app (or use it to DoS myanimelist.net).

This is a much cleaner way IMHO :

  • Server side :

    • Create a websocket server (ex: https://github.com/aaugustin/websockets/blob/master/example/server.py)
    • When a user connects to the websocket server, add the client to a list, remove it from the list on disconnect.
    • For every connected users, do frequently check myanimelist.net to get the associated xml (maybe lower the frequence the more online users you get)
    • for every xml document, make a diff with your server local version, and send that diff to the client using the websocket channel (assuming there is a diff).
  • Client side :
    • on receiving diff : update the local xml with the differences.
    • disconnect from websocket after n seconds of inactivity + when disconnected add a button on the interface to reconnect

I doubt you can do anything much better assuming myanimelist.net doesn't provide a "push" API.

相关问答

更多
  • 我终于去了Flask-JWT( https://pypi.python.org/pypi/Flask-JWT/0.1.0 ) 这是我修改的最小例子: from flask import Flask, render_template, request, url_for, redirect from flask.ext.sqlalchemy import SQLAlchemy from flask.ext.security import Security, SQLAlchemyUserDatastore, \ ...
  • 在适当的WSGI服务器上运行您的烧瓶应用程序,能够处理并发请求(也许是爆米花灭火器或uWSGI ),它将会工作。 在开发中,在Flask提供的服务器中启用线程: app.run(threaded=True) 但请注意,Flask服务器不推荐用于生产使用。 发生什么事情是,使用请求您向烧瓶应用程序发出第二个请求,但是由于它仍然处于第一个处理状态,所以在第一个请求完成之前,它不会响应第二个请求。 顺便说一下,在Python 3下,插件服务器实现更加优雅地处理断开连接,并继续服务而不是崩溃。 Run your ...
  • 我认为你并没有从正确的方向接近这一点,因为你把你的应用程序作为myanimelist.net的代理太多了。 当你有2000个用户时会发生什么? 您的应用程序最终会对myanimelist.net发出大量请求,并且一个不太重要的用户可能会拒绝您的应用程序(或将其用于DoS myanimelist.net)。 这是一个更清洁的方式恕我直言: 服务器端 : 创建一个websocket服务器(例如: https : //github.com/aaugustin/websockets/blob/master/exam ...
  • 我想要做到这一点的最好方法是使用Wireshark,以及它的“Follow TCP Stream”功能。 这将使我在协议级别转储整个http事务。 The best way I've figure out to do this is use Wireshark, and its "Follow TCP Stream" feature. This will give me a dump of an entire http transaction, at the protocol level.
  • 我找到了答案。 发布信息以帮助决定在IIS上实施Flask的人员,这可能会遇到同样的问题。 权限错误确实是IIS方面的一个问题。 您必须更改IIS的权限设置。 这个链接帮助我: 拒绝IIS7权限 - ASP文件写入 在IIS管理器中,我单击了连接窗格中的应用程序池 。 然后改为选择我的Flask应用程序(不是链接中所述的DefaultAppPool)。 我右键单击它并选择高级设置 。 然后我将Process Model部分下的Identity字段更改为LocalSystem 。 点击确定。 在这个改变之后, ...
  • 您可以使用信号量: import threading import time sem = threading.Semaphore() @app.route("/",methods=['POST']) def main_process(): sem.acquire() # heavy process here to run alone sem.release() return "Done" 信号量用法是控制对公共资源的访问。 您可以在此处查看有关信号量的更多信息 这个问题也 ...
  • 在内存中使用sqlite db的问题是无法从多个线程访问您的Sqlite内存数据库。 http://www.sqlite.org/inmemorydb.html 为了解决这个问题,您可能会有多个进程运行您的应用程序,这使得使用内存中的全局变量也是不可能的。 因此,除非您确定您的应用程序只需要单个线程或单个进程(这不太可能),否则您将需要: 使用磁盘存储状态,例如磁盘上的sqlite db,或者甚至只是解析的某个文件。 使用与应用程序分开运行的守护进程来管理状态。 我个人选择2。 您可以使用memcached ...
  • (发表评论作为答案) 您可以简单地使用r.text从文档中获取requests.post的retun数据: 当您发出请求时,Requests会根据HTTP标头对响应的编码进行有根据的猜测。 当您访问r.text时,将使用由Requests猜测的文本编码。 所以只需更换 return "Create new user sucesso!" 同 return r.text 在这种情况下, r.text是requests.post(url, data=json.dumps(payload), headers=h ...
  • 在其他答案的基础上,并使用请求工具带库,代码将如下所示: from flask import Flask, Response from requests_toolbelt import MultipartEncoder app = Flask(__name__) @app.route('/downloads') def downloads(): m = MultipartEncoder( fields={'field0': 'value', 'field1': 'value ...
  • 以下内容适用于您不希望为每个请求重新创建的任何全局Python数据,而不仅仅是rserve,而不仅仅是每个用户唯一的数据。 我们需要一些公共位置来为每个用户创建一个rserve连接。 最简单的方法是将multiprocessing.Manager作为单独的进程运行。 import atexit from multiprocessing import Lock from multiprocessing.managers import BaseManager import pyRserve connectio ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)