👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
👻 Read also: what is the best laptop for engineering students?
JSON datetime between Python and JavaScript
4 answers
I want to send a datetime.datetime object in serialized form from Python using JSON and de-serialize in JavaScript using JSON. What is the best way to do this?
Answer #1
You can add the "default" parameter to json.dumps to handle this:
date_handler = lambda obj: (
obj.isoformat()
if isinstance(obj, (datetime.datetime, datetime.date))
else None
)
json.dumps(datetime.datetime.now(), default=date_handler)
""2010-04-20T20:08:21.634121""
Which is ISO 8601 format.
A more comprehensive default handler function:
def handler(obj):
if hasattr(obj, "isoformat"):
return obj.isoformat()
elif isinstance(obj, ...):
return ...
else:
raise TypeError, "Object of type %s with value of %s is not JSON serializable" % (type(obj), repr(obj))
Update: Added output of type as well as value.
Update: Also handle date
What blocks Ruby, Python to get Javascript V8 speed?
4 answers
Are there any Ruby / Python features that are blocking implementation of optimizations (e.g. inline caching) V8 engine has?
Python is co-developed by Google guys so it shouldn"t be blocked by software patents.
Or this is rather matter of resources put into the V8 project by Google.
Answer #1
What blocks Ruby, Python to get Javascript V8 speed?
Nothing.
Well, okay: money. (And time, people, resources, but if you have money, you can buy those.)
V8 has a team of brilliant, highly-specialized, highly-experienced (and thus highly-paid) engineers working on it, that have decades of experience (I"m talking individually – collectively it"s more like centuries) in creating high-performance execution engines for dynamic OO languages. They are basically the same people who also created the Sun HotSpot JVM (among many others).
Lars Bak, the lead developer, has been literally working on VMs for 25 years (and all of those VMs have lead up to V8), which is basically his entire (professional) life. Some of the people writing Ruby VMs aren"t even 25 years old.
Are there any Ruby / Python features that are blocking implementation of optimizations (e.g. inline caching) V8 engine has?
Given that at least IronRuby, JRuby, MagLev, MacRuby and Rubinius have either monomorphic (IronRuby) or polymorphic inline caching, the answer is obviously no.
Modern Ruby implementations already do a great deal of optimizations. For example, for certain operations, Rubinius"s Hash
class is faster than YARV"s. Now, this doesn"t sound terribly exciting until you realize that Rubinius"s Hash
class is implemented in 100% pure Ruby, while YARV"s is implemented in 100% hand-optimized C.
So, at least in some cases, Rubinius can generate better code than GCC!
Or this is rather matter of resources put into the V8 project by Google.
Yes. Not just Google. The lineage of V8"s source code is 25 years old now. The people who are working on V8 also created the Self VM (to this day one of the fastest dynamic OO language execution engines ever created), the Animorphic Smalltalk VM (to this day one of the fastest Smalltalk execution engines ever created), the HotSpot JVM (the fastest JVM ever created, probably the fastest VM period) and OOVM (one of the most efficient Smalltalk VMs ever created).
In fact, Lars Bak, the lead developer of V8, worked on every single one of those, plus a few others.
Django Template Variables and Javascript
4 answers
When I render a page using the Django template renderer, I can pass in a dictionary variable containing various values to manipulate them in the page using {{ myVar }}
.
Is there a way to access the same variable in Javascript (perhaps using the DOM, I don"t know how Django makes the variables accessible)? I want to be able to lookup details using an AJAX lookup based on the values contained in the variables passed in.
Answer #1
The {{variable}}
is substituted directly into the HTML. Do a view source; it isn"t a "variable" or anything like it. It"s just rendered text.
Having said that, you can put this kind of substitution into your JavaScript.
<script type="text/javascript">
var a = "{{someDjangoVariable}}";
</script>
This gives you "dynamic" javascript.
We hope this article has helped you to resolve the problem. Apart from It Is Not A Javascript Function, check other io Python module-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano It Is Not A Javascript Function
- Deutsch It Is Not A Javascript Function
- Français It Is Not A Javascript Function
- Español It Is Not A Javascript Function
- Türk It Is Not A Javascript Function
- Русский It Is Not A Javascript Function
- Português It Is Not A Javascript Function
- Polski It Is Not A Javascript Function
- Nederlandse It Is Not A Javascript Function
- 中文 It Is Not A Javascript Function
- 한국어 It Is Not A Javascript Function
- 日本語 It Is Not A Javascript Function
- हिन्दी It Is Not A Javascript Function
Berlin | 2023-04-01
Maybe there are another answers? What It Is Not A Javascript Function exactly means?. Will use it in my bachelor thesis
Singapore | 2023-04-01
Simply put and clear. Thank you for sharing. It Is Not A Javascript Function and other issues with JavaScript was always my weak point 😁. Will get back tomorrow with feedback
Massachussetts | 2023-04-01
JavaScript is always a bit confusing 😭 It Is Not A Javascript Function is not the only problem I encountered. Checked yesterday, it works!