text
stringlengths
128
13.8k
## Human: ## The year is 2109 C.E my friends and I were caught in a space disaster when the spacecraft we're in broke apart during a daring escape from a patrolling spacecraft. We stole an antique cellphone (from 1999, in good working condition) from a space museum but our escape was interrupted and fortunately we managed to get into the escape pod and didn't get caught up in the explosion. The only emergency transponder in the escape pod isn't working probably due to the destruction of the spacecraft. Given the technology of 1999, is it possible for us to sent out a distress signal to alert the leaving patrol spacecraft? Note: the cellphone was the most innovative product of 1999 money can buy. The escape pod is not a Faraday cage we're talking about the future and the patrol spacecraft don't necessary be on a lookout for distress signal; please use these clues to your advantage. If there is absolutely no way to transmit any man-made signal out, please state a valid reason why it can't be done. ## Assistant: ## Assets * 1 obsolete piece of short range comms kit requiring a battery you probably can't charge * 1 slightly malfunctioning escape pod with up to date comms that isn't working but you're still alive Current standards say the Mayday should be broadcast on VHF channel 16 (156.8 MHz). A 1999 phone, probably on 2g would likely use 900MHz (UHF). The slightly broken comms unit in the pod is probably using channel 16 or equivalent*. This is your first problem. The patrol ship will be listening in for signals broadcast on the mayday frequency. That's the frequency you need to hit, so first up is rebuilding your phone to that frequency. Or you could look at the pod comms system and see what's wrong. Your next problem is that your mobile phone's range is laughable in interstellar space. What's going to be even more laughable is when you try to use it in the radiation shielded environment of your escape pod. The signal, which is line of sight at the best of times, most likely won't even penetrate the hull. So now you need to hook it up to the external antenna used by the pod's own comms system. Or you could look at the pod comms system and see what's wrong. Now you need to make sure you're using the right broadcast method: are you on AM or FM? (There's no reason to think that 100 years from now they'd be able to pick up an analog signal) I'm sure you can see where I'm going with this. Fix the pod. Ultimately all you're going to do is rebuild a copy of the pod's own comms system with a batch of obsolete components. If you're good enough to do that you're good enough to fix the pod's own system which will be much easier. *I mention channel 16 as an example to show that there are standards for distress signals and they're very different from the standards for mobiles, if you want someone to pick it up then you need to be broadcasting on a frequency they're listening on. ## Distress frequencies 2182 kHz forms an essential part of the Global Maritime Distress Safety System (GMDSS). It has an associated DSC frequency at 2187.5 kHz. Other international distress frequencies, in use as of 2008, include: ```121.5 MHz - civil aircraft emergency frequency 243 MHz - military aircraft emergency frequency 156.8 MHz - Marine VHF radio channel 16, short range maritime use 406 MHz / 406.1 MHz - Cospas-Sarsat international satellite-based search and rescue (SAR) distress alert detection and information distribution system ``` Discontinued frequencies ```500 kHz Morse code is no longer monitored. 121.5 or 243 MHz locators. (No longer automatically monitored by satellite, though still used for aircraft communication and short-range direction finding.) Effective 1 August 2013, the U. S. Coast Guard terminated its radio guard of the international voice distress, safety and calling frequency 2182 kHz and the international digital selective calling (DSC) distress and safety frequency 2187.5 kHz. Additionally, marine information and weather broadcasts transmitted on 2670 kHz terminated concurrently. ``` ## END ##
## Human: ## Often I want to just point the camera to an object or a specific area in my scene to get an idea of how it'll look in the render. What's the most painless hassle-free way to do this in blender? A quick search on the blender wiki does not lend itself to easy look-up due to all the noise in the search result. This question could probably be broken down into these two main questions: * How do I point a selected camera to the current 3d-cursor location in the scene? * How do I point the selected camera to the currently selected object(s) in the scene? ## Assistant: ## Situation 1: You're not looking through the camera yet * Fly Mode: Shift + F or the standard Viewport Navigation * Set Camera to View: Ctrl + Alt + Numpad 0 Situation 2: You're already looking through the camera via Numpad 0 * Select the camera: Right Mouse Button * Grab: G & optionally... * Lock the grab to a local axis: XX or YY or ZZ (my favorite) * Rotate: * Tilt: R * Look around: R R * Orbit around the 3D cursor: .R followed by R, or Z. (Press , afterwards) Tips & Notes (some go beyond this question): 1. In earlier versions of Blender, Fly Mode would to make the camera slowly flatten with the horizon line. It doesn't anymore, but you can reset the camera's rotation Alt + R to make the horizon flat and then use fly mode to reorient the camera while maintaining that flat horizon. 2. If you decide to use another object as a target for the camera to look at, you can use the Object Constraints. To do this: * Select the camera. * Shift-select the target. * Press Ctrl T and select Track To or Damped Track To. You can modify the new constraint in the camera's Constraints panel, for example, to animate the influence. ## END ##
## Human: ## What are the general tactics of Krav Maga as opposed to Systema? For instance, the tactics of Silat are to hurt the other person so badly they can't hurt back. Another example would be that the tactics of boxing would be to knock out the other person first using only punches. So, as far as I know, the goal of Systema and Krav Maga are both to do anything you can to defeat your attacker because they are serious about self defense. Does that mean that Krav Maga and Systema are strategical identical? Does Krav use strategies that Systema doesn't? Does Systema use any strategies that Krav doesn't? Is there a difference or do they generally work the same way? ## Assistant: ## At first glance Krav Maga and Systema seem to be very similar in that they are both very unconventional, no-rules, practical self-defence, martial arts (although Krav isn't technically a martial art) which are no holds barred and generally formless. However... Krav Maga is basically a very raw, dangerous situation survival system (including avoidance and escape) which builds upon your natural instincts and taught in a very short space of time. Therefore it is highly stress tested. It relies on the fact that fear will set in in order to use your natural responses and turn them into functional movements to your advantage. It relies heavily on shock and awe in that defence and attack should be simultaneous where possible to capitalise on the opponent's shock value. Systema is - in many ways - the exact opposite, it is a system which teaches over a much longer period of time how to control your fear/anger/pity and teaches relaxation (through breathing/stretching) in stressful situations so that attacks on your person are less effective, better recovered from and the pressure points, levers and momentum of attackers can be manipulated to your advantage using the clarity of mind gained from your training. In Summary So in effect Krav Maga is taught fast and turns your weaknesses into strengths so your defence/attack becomes like a reflex just when you need it, while Systema is a methodology that needs to be learned and seeks to remove those weaknesses so that your defense/attacks can be more effective. Also, another martial art, similar and somewhere between these is the Keysi Fighting Method (KFM) - developed for street fighting - but that has some actual forms that have to be learned such as the very distinctive 'Pensador' that the Christian Bale Batman uses/ KFM is a lot more like Krav Maga but it makes heavy use of the 'Pensador' guard (much like the 'Rhino' Krav pose) and concentrates a much larger chunk of its time to dealing with multiple opponents and ground based fighting, as it was developed for street fighting and this is where they tend to end up. It doesn't deal with survival (avoidance and get away) as much as Krav however. There are also no weapons. ## END ##
## Human: ## I understand that unlocking the bootloader will wipe my Android phone, but have been looking around for why. Seems to be by design, but what is the reasoning for that design? Is it some security concern, some obscure technical reason, or just for lulz? I'm looking for something solid to chew on here, something more than because "that's how it is". ## Assistant: ## It's a security concern. The Android documentation doesn't provide a lot of elaboration, stating simply: The procedure must be confirmed on-screen, and deletes the user data for privacy reasons. However, the privacy concern is relatively straightforward to extrapolate. Once the bootloader is unlocked, the device can be flashed with unsigned images. This means that it is relatively trivial to root the device, and there are actually multiple ways it can be done, such as: * Flashing a system image with root tools built in * Flashing a boot image that runs ```adbd``` with root by default (an "unsecure" image) * Flashing a recovery image, then using that to overwrite the system or boot partitions with ones that allow root access Once root is obtained, all of the information on ```/data``` essentially becomes accessible. This could be emails, contact information, or many other pieces of personal information that you likely don't want in the wrong hands. Therefore, it makes sense to wipe all of this data during the unlocking procedure to ensure that there is no off-the-shelf "official" way of bypassing the sandboxing restrictions implemented by the Android operating system, which would thereby allow someone to steal personal data. Of course, there may be ways to root without wiping on some devices, but they rely on exploits rather than official methods like a bootloader unlock, meaning they are basically bugs. ## END ##
## Human: ## The original IBM PC and later variants used an Intel 8253 or 8254 as a sound chip. Why did users add sound cards such as the Adlib or Sound Blaster. I remember voice output with programs like telephone answering programs. The sound was wimpy but I attributed most of the quality to speaker size. What was lacking with the original PC sound chip? ## Assistant: ## > The original IBM PC and later variants used an Intel 8253 or 8254 as a sound chip. The 8253 is a timer circuit, containing three timers able to generate timed interrupts or square-wave output with programmed frequencies. On the PC the third timer's output is, via an amplifier, connected to a simple speaker. As said before, calling a 'simple' timer a sound chip is quite a stretch. Since it's a digital output, no level control is available. Only on or off is possible. At best this results in rather nasty sounding square waves. There is no provision to smooth the signal (beside the inertia of the speaker membrane). Also, the only frequencies that can be selected are those that result from dividing the base frequency (1,193,181.8 Hz) by an integer in the range of 1–65535. So even when accepting the meager signal quality, only very few notes can be played at correct frequency. Already the basic 440 Hz concert pitch will be off by .5Hz. So whatever is played using the timer will sound off pitch. Music can still sound good if a little off pitch, but only when all notes are off in the same way (*1) - which is not the case with this 'sound chip'. Of course, there are also ways to manipulate the sound even a bit better by manually handling the output - which results in 100% CPU time usage. > Why did users add sound cards such as the Adlib or Sound Blaster. Simply because they wanted sound - or at least music. Not just a beeper. > What was lacking with the original PC sound chip? As said before, it's no sound chip at all. A sound chip will need programmable oscillators for different wave forms and at least some analogue ciruitry to make a smooth, more natural sound. Ofcourse, from an enginering point, the PC's use of a timer was a great improvement over the Apple II, its model and competition, as it allowed the beeping in parallel - not that this feature was used by BIOS or DOS. Last but not least, the PC is an open system. It is made to add new/better hardware. So why shoudn't there be a real sound card? Improving the machine is a core feature of the design. Addendum: agtoever added a comment with a great example of what was possible with the direct fiddling with the speaker. Awesome, but less than apealing when compared to even the cheapest form of soundcard, the AdLib of the same time, as used by Monkey Island. Next to zero CPU usage and crisp clear sound. Not to mention easy programming. I'd call that a huge advantage. *1 - The average human decodes frequencies relative to each other. As a result sound is still perceived as good, even if the whole scale is shifted, as long as it's in the same direction and same percentage. Output needs to be way off to be noticed by an average listener. In contrast, if notes are off by a different margin and in different directions, even less trained listeners will notice. ## END ##
## Human: ## According to the sources I have found, a lambda expression is essentially implemented by the compiler creating a class with overloaded function call operator and the referenced variables as members. This suggests that the size of lambda expressions varies, and given enough references variables that size can be arbitrarily large. An ```std::function``` should have a fixed size, but it must be able to wrap any kind of callables, including any lambdas of the same kind. How is it implemented? If ```std::function``` internally uses a pointer to its target, then what happens, when the ```std::function``` instance is copied or moved? Are there any heap allocations involved? ## Assistant: ## The implementation of ```std::function``` can differ from one implementation to another, but the core idea is that it uses type-erasure. While there are multiple ways of doing it, you can imagine a trivial (not optimal) solution could be like this (simplified for the specific case of ```std::function<int (double)>``` for the sake of simplicity): ```struct callable_base { virtual int operator()(double d) = 0; virtual ~callable_base() {} }; template <typename F> struct callable : callable_base { F functor; callable(F functor) : functor(functor) {} virtual int operator()(double d) { return functor(d); } }; class function_int_double { std::unique_ptr<callable_base> c; public: template <typename F> function(F f) { c.reset(new callable<F>(f)); } int operator()(double d) { return c(d); } // ... }; ``` In this simple approach the ```function``` object would store just a ```unique_ptr``` to a base type. For each different functor used with the ```function```, a new type derived from the base is created and an object of that type instantiated dynamically. The ```std::function``` object is always of the same size and will allocate space as needed for the different functors in the heap. In real life there are different optimizations that provide performance advantages but would complicate the answer. The type could use small object optimizations, the dynamic dispatch can be replaced by a free-function pointer that takes the functor as argument to avoid one level of indirection, but the idea is basically the same. Regarding the issue of how copies of the ```std::function``` behave, a quick test indicates that copies of the internal callable object are done, rather than sharing the state. ```// g++4.8 int main() { int value = 5; typedef std::function<void()> fun; fun f1 = [=]() mutable { std::cout << value++ << '\n' }; fun f2 = f1; f1(); // prints 5 fun f3 = f1; f2(); // prints 5 f3(); // prints 6 (copy after first increment) } ``` The test indicates that ```f2``` gets a copy of the callable entity, rather than a reference. If the callable entity was shared by the different ```std::function<>``` objects, the output of the program would have been 5, 6, 7. ## END ##
## Human: ## So, I'm on vacation in Utah, and so I attended an LDS service. In the morning, I heard a reading from one of the Presidents of the church during the "Quorum of the Elders," then went to Sunday School, and finally witnessed the Sacrement of the bread and water. (I guess it makes sense there was no wine, but it did make me go "Huh!") After that, there were testimonies from missionaries and some music - but nothing that struck me as a sermon. Was I missing something, or was this an atypical service? I guess I was trying to understand what the "pastor"s role in the service was supposed to be - or again, is it just that Mormons are even more Baptist than baptists? If someone could explain how instruction and exhortation are primarily conferred in the LDS church Id appreciate it. ## Assistant: ## Mormon services are a bit different, as you discovered. In the United States, there is generally a block of 3 meetings back-to-back including a sunday school, a mens' and women's time for instruction, and a sacrament meeting where the general congregation meets together. It sounds like the first meeting you attended was Priesthood meeting where the brothers study the words of scripture and modern Church leaders. Those classes usually have a single instructor each week. Sacrament meeting is where any equivalence of a sermon would be heard. The primary difference between Mormon services and some others is the focal event of church meetings. The purpose behind holding a sacrament meeting is to take the sacrament and renew covenants made at baptism. The actual administration of the sacrament is the most sacred public meeting to Mormons. Hearing people speak and even interpersonal communion (socializing) is only secondary, though important. Members of a ward, or local congregation, are invited to speak by the bishop or his counselors (the basic equivalent of a pastor) and will have some time to prepare a talk. Talks should draw from the scriptures, personal experience and testimony, and focus on the Savior. Note that comments made by church members over the pulpit at a sacrament meeting do not necessarily reflect the Church's official position on a topic or even Church doctrine. The talks are meant to urge, inspire, and exhort all in attendance to come to Christ, to give them an opportunity to feel the power of the Spirit, and to strengthen their faith. Often, members will come with questions in their hearts and find answers in the words that day. Counsel from leaders like a bishop sometimes occur over the pulpit, but usually this is private and individual, based on personal circumstances. The bishop and his counselors are supposed to meet with members regularly in interviews. There is no paid clergy, so all the service is volunteer. Each member is also assigned two "home teachers" which visit their family once a month to share a gospel message, check on well-being, and be a friend and support especially when needed. In this way, instruction is given which might also help replace a typical sermon pattern. The bishop, his counselors, and others he calls on help organize these efforts. (It has also proven to be very quick and effective during disaster response, as home teachers immediately report in on who they teach, and bishops coordinate an effort if needed. So what you saw was apparently quite normal. Even once a month (the first Sunday), a fast and testimony meeting is held during sacrament meeting, where members forgo preparing talks and instead bear their testimonies about Christ and the gospel for the hour as they feel a desire to do so. Instruction is very member-to-member, you might say, but is guided by the direction of a bishop and other church leaders who also inspire and instruct. ## END ##
## Human: ## A partir de un String, ```"123-654321"```, lo que deseo es dividirlo en dos Strings: ```string1=123 string2=654321 ``` ## Assistant: ## Tres formas diferentes de hacerlo: 1. Utilizando la función ```String.split()``` : ``` String str = "123-654321"; String[] parts = str.split("-"); ``` Esta solución utiliza expresiones regulares, así que no es lo más eficiente si no estas trabajando con ellas. 2. Utilizando la clase ```StringTokenizer``` : ```static private List fastSplit(final String text, final String separator, final boolean emptyStrings){ List result = new ArrayList(); final StringTokenizer st = new StringTokenizer(text, separator, true); String token = null; String lastToken = separator; while (st.hasMoreTokens()) { token = st.nextToken(); if (token.equals(separator)) { if (lastToken.equals(separator) && emptyStrings) { result.add(""); } } else { result.add(token); } lastToken = token; } return result; } ``` El problema con este método es que solo funciona bien cuando el delimitador tiene longitud 1. 3. Implementarlo usando ```String.indexOf()``` : En este caso si funcionará correctamente para delimitadores de cualquier longitud. ```static private List fastSplit2(final String text, final String separator, final boolean emptyStrings) { final List result = new ArrayList(); if (text != null && text.length() > 0) { int index1 = 0; int index2 = text.indexOf(separator); int sepLen = separator.length(); while (index2 >= 0) { String token = text.substring(index1, index2); if (!token.isEmpty() || emptyStrings){ result.add(token); } index1 = index2 + sepLen; index2 = text.indexOf(separator, index1); } if (index1 < text.length() - 1) { result.add(text.substring(index1)); } } return result; } ``` ## END ##
## Human: ## What’s the difference between ```\n``` (newline) and ```\r``` (carriage return)? In particular, are there any practical differences between ```\n``` and ```\r```? Are there places where one should be used instead of the other? ## Assistant: ## In terms of ascii code, it's 3 -- since they're 10 and 13 respectively. But seriously, there are many differences: * in Unix and all Unix-like systems, ```\n``` is the code for end-of-line, ```\r``` means nothing special * as a consequence, in C and most languages that somehow copy it (even remotely), ```\n``` is the standard escape sequence for end of line (translated to/from OS-specific sequences as needed) * in old Mac systems (pre-OS X), ```\r``` was the code for end-of-line instead * in Windows (and many old OSs), the code for end of line is 2 characters, ```\r\n```, in this order * as a surprising consequence (harking back to OSs much older than Windows), ```\r\n``` is the standard line-termination for text formats on the Internet * for electromechanical teletype-like "terminals", ```\r``` commands the carriage to go back leftwards until it hits the leftmost stop (a slow operation), ```\n``` commands the roller to roll up one line (a much faster operation) -- that's the reason you always have ```\r``` before ```\n```, so that the roller can move while the carriage is still going leftwards!-) Wikipedia has a more detailed explanation. * for character-mode terminals (typically emulating even-older printing ones as above), in raw mode, ```\r``` and ```\n``` act similarly (except both in terms of the cursor, as there is no carriage or roller. In practice, in the modern context of writing to a text file, you should always use ```\n``` (the underlying runtime will translate that if you're on a weird OS, e.g., Windows. The only reason to use ```\r``` is if you're writing to a character terminal (or more likely a "console window" emulating it) and want the next line you write to overwrite the last one you just wrote (sometimes used for goofy "ascii animation" effects of e.g. progress bars) -- this is getting pretty obsolete in a world of GUIs, though ## END ##
## Human: ## Assume that I am a programmer and I have an NP-complete problem that I need to solve it. What methods are available to deal with NPC problems? Is there a survey or something similar on this topic? ## Assistant: ## There are a number of well-studied strategies; which is best in your application depends on circumstance. * Improve worst case runtime Using problem-specific insight, you can often improve the naive algorithm. For instance, there are $O(c^n)$ algorithms for Vertex Cover with $c huge improvement over the naive $\Omega(2^n)$ and might make instance sizes relevant for you tractable. * Improve expected runtime Using heuristics, you can often devise algorithms that are fast on many instances. If those include most that you meet in practice, you are golden. Examples are SAT for which quite involved solvers exist, and the Simplex algorithm (which solves a polynomial problem, but still). One basic technique that is often helpful is branch and bound. * Restrict the problem If you can make more assumptions on your inputs, the problem may become easy. * Structural properties Your inputs may have properties that simplify solving the problem, e.g. planarity, bipartiteness or missing a minor for graphs. See here for some examples of graph classes for which CLIQUE is easy. * Bounding functions of the input Another thing to look at is parameterised complexity; some problems are solvable in time $O(2^kn^m)$ for $k$ some instance parameter (maximum node degree, maximum edge weight, ...) and $m$ constant. If you can bound $k$ by a polylogarithmic function in $n$ in your setting, you get polynomial algorithms. Saeed Amiri gives details in his answer. * Bounding input quantities Furthermore, some problems admit algorithms that run in pseudo-polynomial time, that is their runtime is bounded by a polynomial function in a number that is part of the input; the naive primality check is an example. This means that if the quantities encoded in your instances have reasonable size, you might have simple algorithms that behave well for you. * Weaken the result This means that you tolerate errorneous or incomplete results. There are two main flavors: * Probabilistic algorithms You only get the correct result with some probability. There are some variants, most notable Monte-Carlo and Las-Vegas algorithms. A famous example is the Miller-Rabin primality test. * Approximation algorithms You no longer look for optimal solutions but almost optimal ones. Some algorithms admit relative ("no worse than double the optimum"), others absolute ("no worse than $5$ plus the optimum") bounds on the error. For many problems it is open how well they can be approximated. There are some that can be approximated arbitrarily well in polynomial time, while others are known to not allow that; check the theory of polynomial-time approximation schemes. Refer to Algorithmics for Hard Problems by Hromkovič for a thorough treatment. * Simplicity is beauty: Improved upper bounds for vertex cover by Chen Jianer, Iyad A. Kanj, Ge Xia (2005) ## END ##
## Human: ## I have a new SPA with a stateless authentication model using JWT. I am often asked to refer OAuth for authentication flows like asking me to send 'Bearer tokens' for every request instead of a simple token header but I do think that OAuth is a lot more complex than a simple JWT based authentication. What are the main differences, should I make the JWT authentication behave like OAuth? I am also using the JWT as my XSRF-TOKEN to prevent XSRF but I am being asked to keep them separate? Should I keep them separate? Any help here will be appreciated and might lead to a set of guidelines for the community. ## Assistant: ## TL;DR If you have very simple scenarios, like a single client application, a single API then it might not pay off to go OAuth 2.0. On the other hand, if there are lots of different clients (browser-based, native mobile, server-side, etc) then sticking to OAuth 2.0 rules might make it more manageable than trying to roll your own system. ## Detailed Answer JWT (Learn JSON Web Tokens) is just a token format. It defines a compact and self-contained mechanism for transmitting data between parties in a way that can be verified and trusted because it is digitally signed. Additionally, the encoding rules of a JWT also make these tokens very easy to use within the context of HTTP. Being self-contained (the actual token contains information about a given subject), they are also a good choice for implementing stateless authentication mechanisms (aka Look mum, no sessions!). When going this route, the only thing a party must present to be granted access to a protected resource is the token itself, and the token in question can be called a bearer token. In practice, what you're doing can already be classified as bearer token -based. However, do consider you're not using bearer tokens as specified by the OAuth 2.0 related specs (see RFC 6750). That would imply relying on the Authorization HTTP header and using the Bearer authentication scheme. Regarding the use of the JWT to prevent CSRF: Without knowing exact details it's difficult to ascertain the validity of that practice. To be honest, it does not seem correct and/or worthwhile. The following article (Cookies vs Tokens: The Definitive Guide) may be a useful read on this subject, particularly the XSS and XSRF Protection section. One final piece of advice. Even if you don't need to go full OAuth 2.0, I would strongly recommend on passing your access token within the Authorization header instead of going with custom headers. If they are really bearer tokens, follow the rules of RFC 6750. If not, you can always create a custom authentication scheme and still use that header. ## END ##
## Human: ## Gostaria de saber se existe alguma forma simples de realizar um upload de arquivos via AJAX + JSON. Se houver, qual seria? ## Assistant: ## Você pode fazer uploads de arquivos utilizando o método ```POST``` porém deve incluir o FormData com a propriedade ```enctype``` definida como ```multipart/form-data``` para que seus arquivos sejam enviados na requisição. Contudo, a formatação do envio não vai ser um JSON, mas sim no ```enctype``` que você definir no seu ```FormData```, que dependendo da linguagem que você está utilizando no backend vai ter uma forma diferente de interpretar estes dados. * application/x-www-form-urlencoded: é o enctype padrão, todos os valores de espaço são convertidos para "+" e caracteres não padrões são convertidos para representação ANSII HEX; * multipart/form-data: nenhum caractere é convertido, mantendo os valores do formulário intactos, necessário para upload de arquivos; * text/plain: apenas espaços são convertidos em "+"; Sendo essas as possibilidades, o que está sendo trafegado não é um JSON quando vamos comunicar enviando dados para o servidor. Esses dados são serializados para serem entregues dentro da área de data do método que você esta utilizando pra trafegar via HTTP(S) dentro dos formatos apresentados. Durante o upload você pode capturar (nos browsers modernos) evento de progresso do upload, conforme no exemplo abaixo. Exemplo utilizando **jQuery**: **Formulário** ```&lt;form id=&quot;formulario&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot;&gt; &lt;input type=&quot;text&quot; name=&quot;campo1&quot; value=&quot;hello&quot; /&gt; &lt;input type=&quot;text&quot; name=&quot;campo2&quot; value=&quot;world&quot; /&gt; &lt;input name=&quot;arquivo&quot; type=&quot;file&quot; /&gt; &lt;button&gt;Enviar&lt;/button&gt; &lt;/form&gt; ``` **Javascript** ```$("#formulario").submit(function() { var formData = new FormData(this); $.ajax({ url: window.location.pathname, type: 'POST', data: formData, success: function(data) { alert(data) }, cache: false, contentType: false, processData: false, xhr: function() { // Custom XMLHttpRequest var myXhr = $.ajaxSettings.xhr(); if (myXhr.upload) { // Avalia se tem suporte a propriedade upload myXhr.upload.addEventListener('progress', function() { /* faz alguma coisa durante o progresso do upload */ }, false); } return myXhr; } }); }); ``` ## END ##
## Human: ## Did the ancients or other pre-industrial civilisations engage in unsustainable practices? ## Assistant: ## Most ancient agricultural practices deplete soil to some degree, but are just fine when population does not exceed certain limits. There are some examples of ancient cultures exhausting natural resources available to them - Pitcairn Island Polynesians, Ancient Puebloans (Anasazi). It's the cause of the semi-nomadic way of life of many early societies - your herds graze the grass into oblivion, you move to another patch to return back when the grass grows back in; or you deplete the soil by overplanting, you move to another spot of land and start over, and so on. But there's an important distinction to be made. Early societies were naturally regulated just as animal population are - by food resources. When food grows scarce, hunger strikes, and humans either start fighting for what's left, reducing population to more manageable levels, or migrate to not yet depleted lands - only cases of actual extinction are from island civilizations which cannot readily move. Meanwhile, globally environment stays balanced, and depleted spots are replenished from outside - slowly healing the damage. Modern humanity has no place to move - all land is already claimed by someone, so we have to make do with what resources we have - and consequently affects the world at global scale, which earlier cultures were incapable of. Sustainability is not about return to pre-industrial way of life - it is unsustainable at modern population levels. It is about preserving the environment in human-habitable state - both by taking less from nature and by increasing effectiveness of usage of what we do need to take. ## END ##
## Human: ## When reading my Bible, I've noticed Joesph's name listed in both the Old and New Testaments; is it the same person or is there more than one Joseph in the Bible? ## Assistant: ## There are 3 distinct (fairly) well-known Josephs in the Bible, firstly in the Old Testament (mainly): * Joseph the son of Jacob/Israel (aka The Patriarch Joseph). This Joseph is actually referred to in a few select places in the New Testament: John 4:5; Acts 7:9-18; Hebrews 11:21-22 & Revelation 7:8. However, it's more likely that a Joseph in the New Testament would be one of: * Joseph husband of Mary and surrogate father of Jesus (aka St. Joseph). * Joseph of Arimathea in whose tomb Jesus was laid to rest. There's also another 9 or 10 more obscure Josephs referred to in the Bible*, 4 more in the Old Testament*: * Father of Igal of the tribe of Issachar (cf. Numbers 13:7). * One of the "sons of Asaph" (cf. 1 Chronicles 25:2 & 9). * A descendant of Binnui, a contemporary of Ezra (cf. Ezra 10:38-42). * Head of the priestly family of Shekaniah, a contemporary of Nehemiah (cf. Nehemiah 12:14). And 5 or 6 more in the New Testament: * One of the (half-)brothers of Jesus (often translated as "Joses" - cf. Mark 6:3). * The son of "Mary" and brother of James (possibly/probably The Mary, if so, identical to Joseph number 8. - cf. Mark 15:40 & 47). * The son of Mattathais (and father of Jannai) appearing in Luke's genealogy of Jesus (in which Joseph number 2. also appears - cf. Luke 3:24-25). * The son of Jonam (and father of Judah) also appearing in Luke's genealogy of Jesus (cf. Luke 3:30). * One of the nominees to replace Judas as an apostle, aka Barsabbas aka Justus (cf. Acts 1:23). * A Levite from Cyprus aka Barnabas who probably deserves to be listed amongst the "well-known" Josephs except that he is almost always referred to as Barnabas instead (cf. Acts 4:36). *That is in the Protestant Canon, there are another 3 in the Deuterocanonical books: Son of Oziel (great-grandfather of Judith - cf. Judith 8:1); Son of Zechariah (cf. 1 Maccabees 5); Brother of Judas Maccabeus (aka John - cf. 2 Maccabees 8:22). ## END ##
## Human: ## Para que serve o "with" no Python? ## Assistant: ## Ele é usado para garantir finalização de recursos adquiridos. Um arquivo, por exemplo é aberto. Quem garante que ele será fechado? Mesmo que você coloque no código de forma explícita que ele deve ser fechado, se ocorrer uma exceção, o código sai de escopo sem executar o resto do código que está em escopo, ele pula o fechamento. Para evitar isto usamos um ```try finally```. O ```finally``` garante a finalização. Como o código fica um pouco longo e este caso é bastante frequente a linguagem providenciou uma forma simplificada com o ```with```. Ele consegue manipular objetos que contenham os métodos ```__enter__()``` e ```__exit__()```. Eles são chamados internamente logo no início da execução do bloco criado e dentro do ```finally``` interno criado no bloco. No exemplo citado deve ficar algo parecido com isto internamente: ```try: __enter__() open(&quot;my_file.txt&quot;) as file: data = file.read() #faça algo com &quot;data&quot; finally: __exit__() ``` ## END ##
## Human: ## The question bothers me since February 2022. Why (legally) are Russian combatants in Ukraine considered soldiers (thus POWs when captured) rather than terrorists? * There is no formal declaration of war. * They are members an organization (Russian military) that commits acts of terrors to civilian population in clear violation of international law of war. Moreover, they either directly or indirectly contribute to the mentioned acts of terror. * Their state (Russia) explicitly claims that there is no war (thus unilaterally waiving the protection of law of war for Russian forces). Why is that particular group of armed people acting in clear violation of Ukrainian law treated as &quot;soldiers in war&quot; rather than state-sponsored criminals? Note, that waiving the protection of law of war does not waive the protection of Ukrainian law (right to due process etc.). ## Assistant: ## The third Geneva convention says in its second article (emphasis added): > the present Convention shall apply to all cases of declared war or of any other armed conflict which may arise between two or more of the High Contracting Parties, even if the state of war is not recognized by one of them. > The Convention shall also apply to all cases of partial or total occupation of the territory of a High Contracting Party, even if the said occupation meets with no armed resistance. The violence in Ukraine qualifies for at least two reasons: it is an armed conflict between two high contracting parties, and it is a case of partial occupation of the territory of a high contracting party. &quot;Members of the armed forces&quot; of Russia who have &quot;fallen into the power of [Ukraine]&quot; are therefore protected by the provisions of the convention as prisoners of war as defined in the convention's fourth article. It follows from this that the parenthetical commentary in the question's third bullet point is incorrect: refusing to call this &quot;military action&quot; a &quot;war&quot; does not in fact &quot;unilaterally waive the protection of law of war for Russian forces.&quot; As to the allegation in the second point, even a prisoner of war who is suspected of committing a war crime is entitled to the convention's protections. Any punishment for the war crime is to be imposed by due process of law. The fact that war crimes may have been committed by other elements of Russia's armed forces is even less of a justification to deny the convention's protections to prisoners who are not individually suspected of war crimes. ## END ##
## Human: ## Are there any advantages while backpacking to hike during the night and sleep during the day? ## Assistant: ## There are several reasons, though not all of them would apply for one's usual holiday trip. 1. Remaining unseen: Obviously, during night its dark and this gives you a fair bit of cover if you want to remain unseen. This reason for traveling at night is common for many hunters in the animal kingdom (and sometimes also their prey), has been used by humans in insecure or hostile territory for centuries and remains a standard technique for some military operations even today. 2. Evading the elements: Hiking throughout the night and resting in the day can be especially useful in hot, arid conditions. For example in deserts, extreme heat during the day and severe cold in the night often come together. Hiking during the night here serves the double purpose of keeping you active and warm when it is cold while allowing you to save water resources during the day. 3. Boosting morale: When conditions get worse (cold, moisture), getting up during the night and starting to hike will not only make you feel warm again, it will also boost your morale. Being active, and actively coping with a bad situation rather than staying miserable in the tent for the rest of the night will make you feel better. 4. You want to avoid tourists: No, seriously, this is a real case in point. Most people travel during the day and one of the reasons we (or at least, I) get outdoors is to enjoy some quietness in the wild. Depending where you are, hiking at night is your best bet to fulfill this dream. 5. Navigation (only very theoretical): Consider a situation where it is all cloudy during the day (so you do not see the sun), you lack a compass and currently have no sense of direction: if you are capable of navigating with the stars, you might consider waiting until dark and hope that the sky will clear up until then. ## END ##
## Human: ## I have been cautioned against blending: * Traditional fantasy elements Such as magic systems and exotic, less plausible creatures (on a scientific level - magic tends to explain away these beasts) * Traditional sci-fi elements Such as advanced technology and civilizations amidst the stars. I have taken it upon myself to harmonize the two in my current worldbuilding project. I know I cannot be the first. I love the creativity found in both, and it is going well so far. I have been exploring the potential for humanity with both tools at their disposal. (Magic and science, essentially) Why do people advise to stick to one or the other? ## Assistant: ## Any sufficiently advanced technology is indistinguishable from magic. - Arthur C Clarke There's a reason that science fiction and fantasy are frequently shelved together - separating the two is usually a fools errand. The Dragonriders of Pern features a preindustrial society where flying, firebreathing, teleporting, and telepathic dragons defend the skies from horrible creatures that rain down from above. They could easily be considered pure fantasy - up until the book where they discover the spacecraft that the human ancestors flew in on, and the labs where they bioengineered the dragons. In Star Wars (arguably the most famous science fiction property of all time), the Force is really just space magic called another name. And they do all their fighting with swords. The lists go on and on. Science fiction and fantasy are united in that they explore the impossible. Fantasy uses elements that will never be possible, while science fiction uses elements that theoretically might be possible. But the impossibility is shared. ## Science fiction and fantasy are not a single genre - they are many Epic fantasy (eg Wheel of Time) and space opera (eg Star Wars) are more similar to each other than they are to urban fantasy or "hard" science fiction. In some ways, it's inaccurate to call science fiction and fantasy genres at all. They're setting elements. A romance doesn't stop being a romance because it's set on a space station, and a murder mystery isn't any less mysterious because it was an elf who was murdered. The key to mixing these elements successfully is to understand the expectations of your readers, and meeting those expectations (but in surprising ways). Genre helps define expectations. If you're writing hard SF, readers expect to see a world that's close enough to our own that they can believe that our world could become the world of the story - magic obviously has no place here. On the flip side, urban fantasy readers expect the author to have considered the modern world and how it might interact with the impossible. Extrapolations from science to science fiction may be natural depending on the exact nature of the setting. If the reader has led to expect that the serial killer is a Scooby-Doo villain, then the sudden reveal that the murders were done with actual magic will violate their expectations and frustrate them (in general - there will of course be exceptions). But if the story is framed as a space cop trying to capture a vampire despite not believing that vampires are real, the readers will accept that premise as well as any other. ## END ##
## Human: ## Why are prions in animal diets not destroyed by the digestive system? ## Assistant: ## Proteases are enzymes in your digestive system that help break down food, acting like molecular-sized scissors that cut up proteins. Proteases have clefts, or subpockets, into which proteins fit, where the substrate (protein) gets cut. Infectious or pathogenic prions are resistant to proteases, because of their three-dimensional shape, which hides away parts of the prion that would normally fit in proteases and which would cause the prion to be digested. Prions that do not cause disease — normal prions — have a different three-dimensional shape that allow them to fit into proteases, and so they are not resistant to digestion: A wealth of evidence contends that the infectious pathogen causing the prion diseases, also referred to as spongiform encephalopathies, is solely comprised of PrPSc, the pathogenic isoform of the prion protein (21-23). Both PrPSc and its normal cellular counterpart, PrPC, are encoded by a cellular gene (2, 19). Physical and molecular characterization of PrPSc and PrPC has failed to reveal any chemical differences between the two isoforms (32). However, PrPSc acquires distinctive conformational characteristics upon its conversion from PrPC. Whereas PrPC is soluble in most detergents and can be easily digested by proteases, PrPScis insoluble in detergents and maintains a protease-resistant core, designated PrP27-30, which polymerizes into amyloid (25). Dr. Neena Singh also discovered that prions &quot;piggyback&quot; or attach to another protein called ferritin, as they make their way through the digestive system: Disease-causing prions are thought to have passed into people when they ate beef from infected cattle, triggering the brain wasting condition called new-variant Creutzfeldt-Jakob disease, or vCJD. But researchers have not been sure exactly how prions enter the body. To find out, Neena Singh and her team at Case Western Reserve University in Cleveland, Ohio, mimicked the process of eating and digesting infected meat. They mashed up brain tissue that contained prions from patients who had a form of Creutzfeldt-Jakob disease. They then exposed it to a range of harsh digestive enzymes from the mouth, stomach and intestine, which normally break proteins into pieces. Prions, which are known to be enormously tough, escape this attack almost unscathed, they showed, as does a second type of protein called ferritin, which stores iron and is abundant in meat. The two proteins seem to stick together, they report in the Journal of Neuroscience. The researchers next added the digested slurry to a lab model of the human gut: a growing sheet of cells from the intestinal lining. By attaching fluorescent tags to the two proteins, they showed that they are transported through the cells hand-in-hand. &quot;Prions probably ride piggyback&quot; through the gut wall into the body, Singh says. Attaching to ferritin may provide additional protection from digestion, insofar as this removes prions from the digestive system, where proteases are concentrated. ## END ##
## Human: ## How slicing in Python works? Please include references where appropriate. ## Assistant: ## The syntax is: ``` a[start:stop] # items start through stop-1 a[start:] # items start through the rest of the array a[:stop] # items from the beginning through stop-1 a[:] # a copy of the whole array ``` There is also the `step` value, which can be used with any of the above: ``` a[start:stop:step] # start through not past stop, by step ``` The key point to remember is that the `:stop` value represents the first value that is not in the selected slice. So, the difference between `stop` and `start` is the number of elements selected (if `step` is 1, the default). The other feature is that `start` or `stop` may be a negative number, which means it counts from the end of the array instead of the beginning. So: ``` a[-1] # last item in the array a[-2:] # last two items in the array a[:-2] # everything except the last two items ``` Similarly, `step` may be a negative number: ``` a[::-1] # all items in the array, reversed a[1::-1] # the first two items, reversed a[:-3:-1] # the last two items, reversed a[-3::-1] # everything except the last two items, reversed ``` Python is kind to the programmer if there are fewer items than you ask for. For example, if you ask for ```a[:-2]``` and ```a``` only contains one element, you get an empty list instead of an error. Sometimes you would prefer the error, so you have to be aware that this may happen. ## Relationship with the ```slice``` object A slice object can represent a slicing operation, i.e.: ``` a[start:stop:step] ``` is equivalent to: ``` a[slice(start, stop, step)] ``` Slice objects also behave slightly differently depending on the number of arguments, similarly to ```range()```, i.e. both ```slice(stop)``` and ```slice(start, stop[, step])``` are supported. To skip specifying a given argument, one might use ```None```, so that e.g. ```a[start:]``` is equivalent to ```a[slice(start, None)]``` or ```a[::-1]``` is equivalent to ```a[slice(None, None, -1)]```. While the ```:```-based notation is very helpful for simple slicing, the explicit use of ```slice()``` objects simplifies the programmatic generation of slicing. ## END ##
## Human: ## I am writing a story where a species undergoes devolution. Is there any scientific or plausible way to do this? The process can be instantaneous or may take ages, I do not mind which as I need to weigh my options at this stage. To be clear, the devolution I am thinking of is like taking a human being then devolving him/her to the primate stage, so lets say about as far back as Orrorin tugenensis or Paranthropus where they are in the midst of evolving from primates to Homo erectus. Please note I used human beings as an example to give context but the species undergoing devolution may not necessarily be human. Based on the answers, "devolution" does not exist so had the word in quotes to reflect this. ## Assistant: ## Devolution doesn't really ever occur, organisms simply adapt to best suit their environment over time. A good example is cave dwelling Troglodyte type species that have lost the ability to see. They haven't actually devolved to not have eyes, they simply did not need them and evolved to save valuable energy and nutrients by not growing a sight organ that would be useless in their environment. It isn't devolution, it's still evolution since they are changing to better suit their environment. So, at least as far as natural processes go, a species would need to become stuck in an environment for a few hundred thousand years that heavily discourages intellect, sociability, communication skills, culture, and inventiveness. That's a tough one to come up with since such traits are usually the most powerful ones for surviving something. So here's the environment and scenario I'm thinking of. Your smart guy race is in the stone age, its pretty much on par with homo sapiens and where they were at about 10,000-15,000 years ago. But something catastrophic doesn't happen. The world slowly becomes a perfect Eden with very few predators, a perfect climate, and vast endless fields of grain and forests of fruit bearing trees. No ice age, no super-predators like we saw in said ice age. No famines or depredation, no rabid competition between several human subspecies for the same resources. The planet (Henceforth known as paradise) is literally perfect. Too perfect. Why would a species that has as close as possible to absolutely nothing to worry about need to even develop the ability to worry? Those big craniums and complex brains waste space and make childbirth more dangerous than necessary. There is absolutely nothing gained by possessing speech, abstract thought, or cooperative culture when all a species needs to do to be successful is to wander around placidly in small familial groups grazing mindlessly on the limitless amounts of food. Shelter is utterly not necessary since you have a perfect climate, nor is clothing, and food is so ludicrously plentiful there is no need to make tools. Within another 100,000 years your species that was formerly well on their way to developing things like agriculture, warfare, and tribal societies has reverted back to dumb grazers that simply live to eat and reproduce. ## END ##
## Human: ## I've used GEDCOM to transfer data between desktop software and websites, but it all seems a bit old hat. Is there anything better that will mangle* my data less. * For example, GEDCOM can lose some data where the two ends of the system understand a concept which GEDCOM does not have a field for. ## Assistant: ## GEDCOM is the defacto standard for exchanging genealogical data. There are some problems with GEDCOM. * It is not rich enough to hold all the types of information that different vendor systems manage internally. So exporting GEDCOM from one system may lose information that that system actually holds. Though see point 4 below. * There is enough ambiguity in the definition of GEDCOM that different vendors have interpreted a few parts of the format inconsistently. Though they support the same tags, they can't consistently share data that uses those tags. * Some systems don't implement all parts of the GEDCOM format. So, when they import GEDCOM data that includes those parts, they lose that information. * GEDCOM has facilities that allow different vendors to extend it by adding new tags that allow them to add new types of data to GEDCOM. Others systems that do not use the same set of extensions looe that information on import. There have been a few attempts to create new standards and formats for exchanging genealogical data. An older one that is often mentioned is the GenTech model. Recently there have been two efforts underway, Better GEDCOM and GEDCOM-X. Better GEDCOM is now inactive. However, the currently developing FHISO effort evolved from it. GEDCOM-X is an effort of Family Search (an organ of the LDS) and is currently a work in progress. A number of individuals have also offered up new formats for archiving and tranferring genealogical data. None of these have generated enough traction to attract critical mass of interest. ## END ##
## Human: ## Is it ever possible that ```(a== 1 && a ==2 && a==3)``` could evaluate to true in JavaScript? ## Assistant: ## If you take advantage of how == works, you could simply create an object with a custom ```toString``` (or ```valueOf```) function that changes what it returns each time it is used such that it satisfies all three conditions. ``` const a = { i: 1, toString: function () { return a.i++; } } if(a == 1 && a == 2 && a == 3) { console.log('Hello World!'); } ``` The reason this works is due to the use of the loose equality operator. When using loose equality, if one of the operands is of a different type than the other, the engine will attempt to convert one to the other. In the case of an object on the left and a number on the right, it will attempt to convert the object to a number by first calling ```valueOf``` if it is callable, and failing that, it will call ```toString```. I used ```toString``` in this case simply because it's what came to mind, ```valueOf``` would make more sense. If I instead returned a string from ```toString```, the engine would have then attempted to convert the string to a number giving us the same end result, though with a slightly longer path. ## END ##
## Human: ## Gostaria de saber qual é a real diferença entre o ```String``` (s maiúsculo) e o ```string``` (s minúsculo). Aparentemente os dois têm os mesmos objetivos, porém qual é "melhor" para ser utilizado? ## Assistant: ## Já foi dito que ```string``` é apenas um alias para o tipo ```String```. Para deixar claro, não há diferença em performance ou forma como o código é gerado. Há exatamente zero diferença no tratamento de ambas. Elas podem fazer as mesmas coisas, e todos os membros estão disponíveis em qualquer um deles, afinal assim se comporta um alias. Diferenças úteis na utilização e escolha: * ```string``` é apenas uma forma mais simples de usar o tipo ```String``` no C#, ou seja, ```string``` é a forma de &quot;tipar&quot; uma cadeia de caracteres no C# e ```String``` é um tipo do CLR. No C#, dependendo do contexto, é melhor usar uma ou outra forma. O tipo ```String``` presente no namespace ```System``` pode ser usado em qualquer linguagem que se utilize do CLR. * ```string``` não pode ser usado com reflexão. ```String``` deve ser usado no lugar. * ```String``` pode ser usado para criar outros aliases: ``` using str = System.String; //... str s = &quot;Foi usado outro alias para string.&quot;; // a variável 's' é do tipo System.String que é o mesmo que ser string ``` Mas esse é apenas um exemplo, não há necessidade e não é recomendado usar em código real. Existe sim casos que um alias pode ser útil, mas esse apenas dificulta a leitura para quem não está acostumado com isso, sem trazer nenhum benefício. * Há alguns locais que ocorre o oposto e criar um alias pode trazer mais legibilidade ao código. * Em outros casos pode ser apenas estranho usar um ou outro e dificultar a leitura. * Na verdade o tipo ```String``` deve ser usado como ```System.String``` ou onde exista um ```using System```, enquanto que ```string``` pode ser usado em qualquer código que o compilador já entenderá. * ```String``` pode ser usado como identificador válido. ```string``` é uma palavra reservada e não pode ser um identificador. * Há uma diferença na forma como o syntax highlight apresenta ambos. Pois uma forma é tipo e a outra é palavra chave. * Apesar da recomendação geral em usar ```string``` sempre que possível enquanto está programando em C#, há controvérsias no seu uso. Alguns alegam que ```String``` é preferencial para deixar claro, através do Pascal Case, que você está usando um tipo de referência(en). Mas não faz muito sentido porque ```Int32``` é tipo de valor(en) e também usa PascalCase (ou lowerCamelCase). E ainda ele, apesar de ser tipo de referência, tem semântica de valor. Então mesmo que usasse a lógica de Pascal Case para tipo de referência, e Camel Case (ou (UpperCamelCase) para tipo de valor, pela semântica, que é o que importa, ele deveria ser Camel Case, ou seja, deveria começar com minúsculo mesmo. No CLR o estilo de case é usado para ajudar determinar outras características do identificador. * É possível substituir o tipo oficial por outro seu com mesmo nome se fizer tudo corretamente. O *alias *apontará para esse novo tipo que tem o mesmo nome e é colocado da mesma forma. Se ele não for exatamente o mesmo nome (incluindo o namespace) aí ele só pode ser usado opcionalmente o que quase inviabiliza o uso real. Importante lembrar que ele é um tipo como outro qualquer por referência com semântica por valor e algumas facilidades que o compilador dá. O mesmo vale se usar ```String```. Também vale lembrar que não existe tipo primitivo em C# (outra referência). Isso existe só como conceito do CLR, mesmo assim não como algo fundamental. Importante notar que tudo isso é válido para o C#, outras linguagens como Java, ou até mesmo VB.NET podem ter mais diferenças. * Documentação oficial do ```string``` dizendo que é um alias. * FAQ reafirmando sobre aliases de tipos. * ```System.String```. Coloquei no GitHub para referência futura. ## END ##
## Human: ## Assume an environment with a puppet-managed cluster of different servers - various hardware, software, operating systems, virtual/dedicated, etc. Would you choose meaningful hostnames (mysqlmaster01..99, mysqlslave001..999, vpnprimary, vpnbackup, etc.) or would you prefer meaningless hostnames such as characters from a book or movie? The problem I see with meaningful hostnames is that names usually represent a single service and if a server has more than one purpose it gets really messy (especially if server roles change often). Isn't mapping a service name to an IP address and maintaining that mapping what DNS is supposed to do? What are the advantages and drawbacks of both approaches and what actual problems have you had to tackle with the approach you chose? ## Assistant: ## This largely comes down to whether your servers are ```pets``` or ```livestock```. Pets get individual names. They're distinct from each other, and we care about those differences. When one gets sick, we usually try to nurse it back to health. Traditionally, servers have been pets. Livestock get numbers. They're mostly identical, and what differences there are, we don't care about and usually try to minimize. When one gets sick, we put it down and get another one. Fully virtualized servers, especially IaaS servers such as AWS, are livestock. In most complex environments, you have a mix. Your web backends, for instance, are almost certainly livestock. If you need more, you spin up a few more with the standard config; if you don't need as many you turn some off. Your database servers, in some configurations, are pets. There may be a lot of special setup on each; you may even be running them on bare metal instead of virtualization. Of course, in either environment, you can name SERVICES and address those directly. This is a best practice in any case; your developers shouldn't need to know or care what the actual hostname of a service is. The hostname should be a purely operational detail. Think, then, of encoding information that is useful to your ops staff in the hostnames - for instance, it's often helpful to denote which datacenter a server is in. ## END ##
## Human: ## What does it mean for an album to be remastered? ## Assistant: ## Mastering is a separate art from recording or mixing. According to the Wikipedia article, the principle processes of mastering are: * Editing minor flaws. * Applying noise reduction to eliminate clicks, dropouts, hum and hiss. * Adjusting stereo width. * Adding ambience. * Equalize audio across tracks for the purpose of optimized frequency distribution. * Adjust volume. * Dynamic range compression or expansion. * Peak limit. * Dither. The common thread between these processes is that they can be performed on the complete mixed recording, not just on individual tracks of a recording. Because of this, mastering can be performed at any time on any audio source. As for the reason why so many works are remastered, the most likely reason is to keep up with new home theater audio formats. If you have a massive 7.2 surround system, it would be a shame if there wasn't a professionally mastered 7.2 format of "Dark Side of the Moon" to utilize that power. Every time new audio encoding formats become widespread (48kHz at 16bits, 92kHz at 24bits, etc.), your favorite audio source will need to be remastered to sound good in that format. ## END ##
## Human: ## What's the best way to iterate over the items in a ```HashMap```? ## Assistant: ## Extracted from the reference How to Iterate Over a Map in Java: There are several ways of iterating over a ```Map``` in Java. Let's go over the most common methods and review their advantages and disadvantages. Since all maps in Java implement the Map interface, the following techniques will work for any map implementation (```HashMap```, ```TreeMap```, ```LinkedHashMap```, ```Hashtable```, etc.) Method #1: Iterating over entries using a For-Each loop. This is the most common method and is preferable in most cases. It should be used if you need both map keys and values in the loop. ```Map map = new HashMap(); for (Map.Entry entry : map.entrySet()) { System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); } ``` Note that the For-Each loop was introduced in Java 5, so this method is working only in newer versions of the language. Also a For-Each loop will throw ```NullPointerException``` if you try to iterate over a map that is null, so before iterating you should always check for null references. Method #2: Iterating over keys or values using a For-Each loop. If you need only keys or values from the map, you can iterate over keySet or values instead of entrySet. ```Map map = new HashMap(); // Iterating over keys only for (Integer key : map.keySet()) { System.out.println("Key = " + key); } // Iterating over values only for (Integer value : map.values()) { System.out.println("Value = " + value); } ``` This method gives a slight performance advantage over ```entrySet``` iteration (about 10% faster) and is more clean. Method #3: Iterating using Iterator. Using Generics: ```Map map = new HashMap(); Iterator> entries = map.entrySet().iterator(); while (entries.hasNext()) { Map.Entry entry = entries.next(); System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); } ``` Without Generics: ```Map map = new HashMap(); Iterator entries = map.entrySet().iterator(); while (entries.hasNext()) { Map.Entry entry = (Map.Entry) entries.next(); Integer key = (Integer)entry.getKey(); Integer value = (Integer)entry.getValue(); System.out.println("Key = " + key + ", Value = " + value); } ``` You can also use same technique to iterate over ```keySet``` or values. This method might look redundant, but it has its own advantages. First of all, it is the only way to iterate over a map in older versions of Java. The other important feature is that it is the only method that allows you to remove entries from the map during iteration by calling ```iterator.remove()```. If you try to do this during For-Each iteration you will get "unpredictable results" according to Javadoc. From a performance point of view this method is equal to a For-Each iteration. Method #4: Iterating over keys and searching for values (inefficient). ```Map map = new HashMap(); for (Integer key : map.keySet()) { Integer value = map.get(key); System.out.println("Key = " + key + ", Value = " + value); } ``` This might look like a cleaner alternative for method #1, but in practice it is pretty slow and inefficient as getting values by a key might be time-consuming (this method in different Map implementations is 20%-200% slower than method #1). If you have FindBugs installed, it will detect this and warn you about inefficient iteration. This method should be avoided. Conclusion: If you need only keys or values from the map, use method #2. If you are stuck with older version of Java (less than 5) or planning to remove entries during iteration, you have to use method #3. Otherwise use method #1. ## END ##
## Human: ## Say I have a file ```/templates/apple``` and I want to * put it in two different places and then * remove the original. So, ```/templates/apple``` will be copied to ```/templates/used``` AND ```/templates/inuse``` and then after that I’d like to remove the original. Is ```cp``` the best way to do this, followed by ```rm```? Or is there a better way? I want to do it all in one line so I’m thinking it would look something like: ```cp /templates/apple /templates/used | cp /templates/apple /templates/inuse | rm /templates/apple ``` Is this the correct syntax? ## Assistant: ## You are using ```|``` (pipe) to direct the output of a command into another command. What you are looking for is ```&&``` operator to execute the next command only if the previous one succeeded: ```cp /templates/apple /templates/used && cp /templates/apple /templates/inuse && rm /templates/apple ``` Or ```cp /templates/apple /templates/used && mv /templates/apple /templates/inuse ``` To summarize (non-exhaustively) bash's command operators/separators: * ```|``` pipes (pipelines) the standard output (```stdout```) of one command into the standard input of another one. Note that ```stderr``` still goes into its default destination, whatever that happen to be. * ```|&```pipes both ```stdout``` and ```stderr``` of one command into the standard input of another one. Very useful, available in bash version 4 and above. * ```&&``` executes the right-hand command of ```&&``` only if the previous one succeeded. * ```||``` executes the right-hand command of ```||``` only it the previous one failed. * ```;``` executes the right-hand command of ```;``` always regardless whether the previous command succeeded or failed. Unless ```set -e``` was previously invoked, which causes ```bash``` to fail on an error. ## END ##
## Human: ## When are Decision Diagrams the right way to model and solve a problem? ## Assistant: ## Decision diagrams (DDs) are most effective when they can compactly represent a large (perhaps exponential) set of solutions. This is done by merging equivalent states in each layer. To make decision diagrams scalable, we can use relaxed decision diagrams which allow merging nodes that are not necessarily equivalent. Relaxed decision diagrams provide dual bounds on the objective function -- a larger width can produce a stronger bound. The relationship between the size of the decision diagram and the underlying combinatorial structure of the problem is well studied in the computer science literature; see for example the textbook by Wegener. The most important feature that influences the size is the variable ordering. We can use this to come up with some general guidelines on when a DD may be successful for combinatorial optimization: * The problem has a sequential structure, and decisions for stage $i$ depend only on the decisions made in stages $i-k$ through $i-1$, for small $k$. * The induced width of the constraint graph of the problem is small. * For a MIP model, the constraint matrix has a small bandwidth. * For knapsack-like problems, the maximum `budget' is relatively small. Note that in some of the above cases, precise characterization of 'small' may yield (pseudo-)polynomial bounds on the size of the exact DD. In practice, relaxed DDs will always have polynomial size. There are several areas of combinatorial optimization in which decision diagrams have been successfully applied: * Sequencing and routing problems. For example, single machine scheduling with setup times, time windows, and/or precedence constraints, which can also be applied to constrained traveling salesperson problems. A similar decision diagram representation was used by Grubhub to solve pickup-and-delivery problems. * Decomposition and embedding in MIP models. Decision diagrams have been used to represent subproblems in MIP models that are otherwise difficult to linearize. For example, to represent nonlinear objective functions, constrained employee schedules, or nonlinear circuit design. DDs have also been used in column generation. * Graph-theoretic problems. Examples include set covering, maximum independent set, and maximum cut. * Constraint programming. Constraint propagation based on (relaxed) decision diagrams can be much more effective than propagating domains of individual variables. This was first demonstrated on overlapping alldifferent constraints. ## END ##
## Human: ## Essentially, I have a protagonist who I set up as a 'main' good guy in one of my series. However, in my final series, I intend to make him turn to the dark side and oppose my other protagonists (from my other series). It's clear in his series that the protagonist-turned-antagonist is very devious, and he has had hostile intentions previously towards the protagonists of the other series. My question: Should I avoid turning my protagonist into an antagonist? Are there any foreseeable problems with this? Will this be a problem for readers? Any tips or tricks to handle this? ## Assistant: ## Sounds like a great idea! Seriously though: the antagonist is the single most important character to any plot. The very best antagonists have motivations and feelings that readers can understand and empathize with. A former protagonist as an antagonist sounds really good. Both the reader and the author should be in for a fun ride, because it is very likely you will see your character grow in ways you didn't expect when you had them boxed in as a "good guy". Think about people you know in real life. If you really observe people, you come to understand that nobody is a "good guy". Sure there are people who are generally good and generally do good things, but nobody is perfect, and even the best people have thoughts and motivations you may not approve of. Even more interesting is the fact that two people can be absolutely opposed to one another without either one being objectively "wrong" or "evil". They might just have goals and motivations that are diametrically opposed. Imagine a city council wants more tax money to build a park. A local business wants to pay less tax money so they can afford to give Christmas bonuses. The business owner isn't "evil", in fact, he wants to give Christmas bonuses! The city council isn't "evil" either, they just want a park for the kids. Sometimes goals just conflict in an irreconcilable way. That's the real world. That kind of plot provides a lot more food for thought than a two dimensional "good versus evil" morning cartoon romp. ## END ##
## Human: ## I'd never heard of anycast until a few seconds ago when I read "What are some cool or useful server/networking tricks?". The wikipedia "Anycast" article on it is quite formal and doesn't really evoke a mental picture of how it would be used. Can someone explain in a few informal sentences what "anycast" is, how you configure it (just in a general sense), and what its benefits are (what does it make easier)? ## Assistant: ## Anycast is networking technique where the same IP prefix is advertised from multiple locations. The network then decides which location to route a user request to, based on routing protocol costs and possibly the 'health' of the advertising servers. There are several benefits to anycast. First, in steady state, users of an anycast service (DNS is an excellent example) will always connect to the 'closest' (from a routing protocol perspective) DNS server. This reduces latency, as well as providing a level of load-balancing (assuming that your consumers are evenly distributed around your network). Another advantage is ease of configuration management. Rather than having to configure different DNS servers depending on where a server/workstation is deployed (Asia, America, Europe), you have one IP address that is configured in every location. Depending on how anycast is implemented, it can also provide a level of high availability. If the advertisement of the anycast route is conditional on some sort of health check (e.g. a DNS query for a well known domain, in this example), then as soon as a server fails its route can be removed. Once the network reconverges, user requests will be seamlessly forwarded to the next closest instance of DNS, without the need for any manual intervention or reconfiguration. A final advantage is that of horizontal scaling; if you find that one server is being overly loaded, simply deploy another one in a location that would allow it to take some proportion of the overloaded server's requests. Again, as no client configuration is required, this can be done very quickly. ## END ##
## Human: ## $A$ and $B$ are $n \times n$ matrices and $v$ is a vector with $n$ elements. $Av$ has $\approx 2n^2$ flops and $A+B$ has $n^2$ flops. Following this logic, $(A+B)v$ should be faster than $Av+Bv$. Yet, when I run the following code in matlab ```A = rand(2000,2000); B = rand(2000,2000); v = rand(2000,1); tic D=zeros(size(A)); D = A; for i =1:100 D = A + B; (D)*v; end toc tic for i =1:100 (A*v+B*v); end toc ``` The opposite is true. Av+Bv is over twice as fast. Any explanations? ## Assistant: ## Except for code which does a significant number of floating-point operations on data that are held in cache, most floating-point intensive code is performance limited by memory bandwidth and cache capacity rather than by flops. $v$ and the products $Av$ and $Bv$ are all vectors of length 2000 (16K bytes in double precision), which will easily fit into a level 1 cache. The matrices $A$ and $B$ are 2000 by 2000 or about 32 megabytes in size. Your level 3 cache might be large enough to store one of these matrices if you've got a really good processor. Computing $Av$ requires reading 32 megabytes (for $A$) in from memory, reading in 16K bytes (for $v$) storing intermediate results in the L1 cache and eventually writing 16K bytes out to memory. Multiplying $Bv$ takes the same amount of work. Adding the two intermediate results to get the final result requires a trivial amount of work. That's a total of roughly 64 megabytes of reads and an insignificant number of writes. Computing $(A+B)$ requires reading 32 megabytes (for A) plus 32 megabytes (for B) from memory and writing 32 megabytes (for A+B) out. Then you have to do a single matrix-vector multiplication as above which involves reading 32 megabytes from memory (if you've got a big L3 cache, then perhaps this 32 megabytes is in that L3 cache.) That's a total of 96 megabytes of reads and 32 megabytes of writes. Thus there's twice as much memory traffic involved in computing this as $(A+B)v$ instead of $Av+Bv$. Note that if you have to do many of these multiplications with different vectors $v$ but the same $A$ and $B$, then it will become more efficient to compute $A+B$ once and reuse that matrix for the matrix-vector multiplications. ## END ##
## Human: ## I came across a piece of code ```void *p = &&abc;```. What is the significance of ```&&``` here? I know about rvalue references but I think ```&&``` used in this context is different. What does ```&&``` indicate in ```void *p = &&abc;``` ? ## Assistant: ## ## How to find it out That's the address of a label and it's a feature specific to GCC. ```int main(void) { void* startp; s: startp = &&s; printf(&quot;the assignment above starts at address %p\n&quot;, startp); return 0; } ``` You could have figured it out yourself by testing: ```int main(void) { void* startp; int a; startp = &&a; printf(&quot;startp=%p\n&quot;, startp); return 0; } ``` In which case GCC says: > error: label ‘a’ used but not defined ## Under the hood - assembly You need to know assembler to really understand this, but I'll try to explain you what an address of a label means. After the OS loads the .exe file from the disk, a component of the operating system called &quot;the loader&quot; (windows has the &quot;PE Loader&quot;, linux has &quot;ELF loader&quot; or maybe even others, if they're compiled in the kernel), it does a &quot;virtualization&quot; of that program, turning it into a process. This process thinks it is the only one in RAM and it has access to the entire RAM (that is, 0x00000000-0xFFFFFFFF on a 32-bit machine). (the above is just a short overwiew of what's happenning, you really need to learn assembly to understand it fully, so bear with me) Now, the label in a source code is basically an address. &quot;goto label;&quot; does nothing else than a jump to that address (think about the instruction pointer in assembly). This label stores this RAM address, and that's how you can find out that address. After you've learned ASM, you'll realize that that address points to a instruction within the ```.text``` section of the executable. The ```.text``` section is the one which holds you program's (binary) code to be executed. You can inspect this with: ``` objdump -x a.out ``` ## END ##
## Human: ## When I execute "```python```" from the terminal with no arguments it brings up the Python interactive shell. When I execute "```cat | python```" from the terminal it doesn't launch the interactive mode. Somehow, without getting any input, it has detected that it is connected to a pipe. How would I do a similar detection in C or C++ or Qt? ## Assistant: ## You can use ```isatty```: ``` #include <stdio.h> #include <io.h> ... if (isatty(fileno(stdin))) printf( "stdin is a terminal\n" ); else printf( "stdin is a file or a pipe\n"); ``` On windows they're prefixed with underscores: ```_isatty```, ```_fileno```. ## END ##
## Human: ## "The video of Pythom Space's first rocket test has raised a few eyebrows The video prompted hundreds of replies on Twitter, including some from rather horrified rocket scientists. &quot;We knew better as untrained college students,&quot; said Jordan Noone, the co-founder of Relativity Space. Pythom “Micro jump” What exactly did they get wrong with this test? Note: The Pythom Space CEO did eventually respond to the original Ars Technica story linked above. It does offer their own take on some of the issues raised there and elsewhere (including some of the points in the answer below)" ## Assistant: ## The easy-to-spot stuff: * (assumed) Improper transportation of hazardous materials. Both white fuming nitric acid and furfuryl alcohol are considered hazardous materials; none of the vehicles in the video appears to have a hazmat placard, the rocket is not an approved hazmat container, and the fuel containers seen in other videos are emphatically not approved. * No personal protective gear. Nitric acid and furfurly alcohol are both toxic, and heavy loads are being lifted. Nobody is seen wearing any sort of protective gear, not even hard hats or safety glasses. * Insecure rigging when lifting the test object upright. A strap, particularly one being pulled at a sharp angle, is far less safe than a proper lifting point. * Bad angle when lifting the test object upright. This puts excess loads on things, increasing the risk of a fall or failure. * Using unbraced legs as a pivot point when lifting the test object upright. You can see they're on the verge of buckling. * (assumed) Lifting the test object while already fueled. In the event of a fall, this ensures an explosion if the tanks rupture, as the fuels are hypergolic. * Having people stand under a suspended load. If the test object falls, this will cause serious injury or death to at least two people. * Using a truck to pull the lifting cable. This provides far less control than a proper winch. * Having personnel in the blast area of the test object. In the event of an failure, this makes it highly likely that they will be engulfed in the fireball, struck by debris, or poisoned by fuel vapors. * Having personnel in the exhaust area of the test object. Even during normal operation, they will be exposed to nitric acid, furfuryl alcohol, and assorted nitrogen oxides, all of which are highly toxic. * Having unnecessary equipment in the test area. In the event of a failure, this can become additional flying debris. * Not properly securing the test object. In the event of higher-than-expected performance, this will become self-propelled debris; in the event of a premature shutdown, the resulting hop-and-crash will rupture the fuel tanks and cause an explosion. * Not providing a flight termination system. In the event that the test object becomes airborne, they have no way of keeping it from leaving the test area. * (assumed) Insufficient fire-extinguishing equipment on site. None of the area views show a pumper truck or other equipment capable of dealing with a brush fire ignited by 15+ kg of hypergolic fuels. * Improper fire-extinguishing equipment. At one point, you can see what appears to be an ordinary dry-chemical fire extinguisher. This should not be used when dealing with nitric acid. * Oxidizer-rich shutdown. You can see see the reddish cloud of nitric-acid byproducts at the end of the burn; this is likely the result of the oxidizer reacting with the engine. * (assumed) No post-burn decontamination of the area. The celebratory post-burn shot shows no sign of cleanup. Combined with the oxidizer-rich shutdown, this means they're likely walking through an area contaminated with nitric acid. ## END ##
## Human: ## When should I use wp_register_script() with wp_enqueue_script() vs just wp_enqueue_script()? ## Assistant: ## The ```wp_register_script()``` Codex page literally says: A safe way of registering javascripts in WordPress for later use with ```wp_enqueue_script()```. This means, if you want to register your scripts, but not directly load them in your pages, you can register the files once, and then load them when you need them. For example: You have a switch statement wich loads some functionality, but two of three cases needs a particular javascript file, and one doesn't. You can enqueue the script every time, wich costs more resources, or just enqueue the script when you need it: ```... wp_register_script( 'my-handy-javascript', ... ); ... switch( $somevar ) { case 'value': wp_enqueue_script( 'my-handy-javascript' ); // needs the file ... break; case 'value2': wp_enqueue_script( 'my-handy-javascript' ); // needs the file ... break; default: case 'value3': // doesn't needs the file ... break; } ``` It is not necessary to register a script and then enqueue them, but it can provide some logic in your code if you register all the scripts you need somewhere in your ```functions.php``` instead of everywhere in your code. The Codex also tells the following: Use the ```wp_enqueue_scripts``` action to call this function, or ```admin_enqueue_scripts``` to call it on the admin side. This means that if you want to enqueue your script on the frond-end and in the back-end, you can register a script once, and then load it on the front-end with ```wp_enqueue_script``` and in the back-end with ```admin_enqueue_script```. This way you won't have the same enqueue recourse twice in one theme, plugin, widget or whatever. ## END ##
## Human: ## Prove that the union of countably many countable sets is countable. ## Assistant: ## Let's start with a quick review of "countable". A set is countable if we can set up a 1-1 correspondence between the set and the natural numbers. As an example, let's take $\mathbb{Z}$, which consists of all the integers. Is $\mathbb Z$ countable? It may seem uncountable if you pick a naive correspondence, say $1 \mapsto 1$, $2 \mapsto 2 ...$, which leaves all of the negative numbers unmapped. But if we organize the integers like this: $$0$$ $$1, -1$$ $$2, -2$$ $$3, -3$$ $$...$$ We quickly see that there is a map that works. Map 1 to 0, 2 to 1, 3 to -1, 4 to 2, 5 to -2, etc. So given an element $x$ in $\mathbb Z$, we either have that $1 \mapsto x$ if $x=0$, $2x \mapsto x$ if $x > 0$, or $2|x|+1 \mapsto x$ if $x . So the integers are countable. We proved this by finding a map between the integers and the natural numbers. So to show that the union of countably many sets is countable, we need to find a similar mapping. First, let's unpack "the union of countably many countable sets is countable": 1. "countable sets" pretty simple. If $S$ is in our set of sets, there's a 1-1 correspondence between elements of $S$ and $\mathbb N$. 2. "countably many countable sets" we have a 1-1 correspondence between $\mathbb N$ and the sets themselves. In other words, we can write the sets as $S_1$, $S_2$, $S_3$... Let's call the set of sets $\{S_n\}, n \in \mathbb N$. 3. "union of countably many countable sets is countable". There is a 1-1 mapping between the elements in $\mathbb N$ and the elements in $S_1 \cup S_2 \cup S_3 ...$ So how do we prove this? We need to find a correspondence, of course. Fortunately, there's a simple way to do this. Let $s_{nm}$ be the $mth$ element of $S_n$. We can do this because $S_n$ is by definition of the problem countable. We can write the elements of ALL the sets like this: $$s_{11}, s_{12}, s_{13} ...$$ $$s_{21}, s_{22}, s_{23} ...$$ $$s_{31}, s_{32}, s_{33} ...$$ $$...$$ Now let $1 \mapsto s_{11}$, $2 \mapsto s_{12}$, $3 \mapsto s_{21}$, $4 \mapsto s_{13}$, etc. You might notice that if we cross out every element that we've mapped, we're crossing them out in diagonal lines. With $1$ we cross out the first diagonal, $2-3$ we cross out the second diagonal, $4-6$ the third diagonal, $7-10$ the fourth diagonal, etc. The $nth$ diagonal requires us to map $n$ elements to cross it out. Since we never "run out" of elements in $\mathbb N$, eventually given any diagonal we'll create a map to every element in it. Since obviously every element in $S_1 \cup S_2 \cup S_3 ...$ is in one of the diagonals, we've created a 1-1 map between $\mathbb N$ and the set of sets. Let's extend this one step further. What if we made $s_{11} = 1/1$, $s_{12} = 1/2$, $s_{21} = 2/1$, etc? Then $S_1 \cup S_2 \cup S_3 ... = \mathbb Q^+$! This is how you prove that the rationals are countable. Well, the positive rationals anyway. Can you extend these proofs to show that the rationals are countable? ## END ##
## Human: ## Você encontra na internet a afirmação que Singletons são ruins. Isto é verdade? Por quê? O problema seria generalizado para quaisquer objetos com instância única? Ou para qualquer coisa que tenha estado global? Se é tão ruim, pra que foi inventado? Ou seja, que problema ele queria resolver? Quais os problemas que terei se usá-lo? Existe alternativa viável? ## Assistant: ## Muito da polêmica ocorre pela falta de uma contextualização. Não vejo nada que desabone o padrão Sigleton quando usado no contexto correto. Sim, ele pode causar muito mal, assim como qualquer padrão de projeto aplicado indevidamente sem análise do impacto e dos efeitos colaterais. ###Porque não usar * Acoplamento. Ao usar um Singleton você está acoplando o código a uma implementação estática e específica. Isso torna o seu código dependente dessa classe e impede, por exemplo, criar mocks em testes unitários. Desse ponto de vista a consequência é basicamente a mesma de fazer diretamente um ```new MeuObjetoImportante()```, pois é uma dependência direta da classe. * Escopo. O padrão Singleton aplicado conforme a definição também elimina o conceito de escopo. Isso significa que se você por alguma razão decidir que para determinados componentes da aplicação você precisa de outra implementação terá que alterar manualmente todas as classes. * Não garantia de uma instância única. Em certos casos o padrão pode levar à falsa segurança de que existirá apenas uma instância. Vamos supor que você projete um sistema web e quer dar acesso a um arquivo ou recurso exclusivo. Um Singleton parece uma boa prática, não é? Mas e se amanhã você fizer o deploy da aplicação em um cluster com N servidores. No caso de Java isso também é bem complicado, pois não existe isso de uma classe por JVM. O conceito correto é uma classe por ```ClassLoader```, de forma que num mesmo servidor JEE duas aplicações diferentes podem ter, cada uma, sua própria versão de uma mesma classe. As variáveis estáticas não são compartilhadas entre essas versões da mesma classe, portanto o escopo do Singleton em Java é por ```ClassLoader``` e não por programa. ###Porque usar Em algumas situações você sabe que vai ter só uma instância da classe. Em uma aplicação Desktop, por exemplo, não há porque ter "frescuras" adicionais para obter uma conexão com o banco de dados. Também não vejo problemas em um objeto encapsulando alguns atributos de configuração globais. Simplesmente não valeria o esforço de implementar algum mecanismo muito elaborado para algo tão simples. ###Como usar Quando pensamos em Singleton, logo a implementação clássica do Gang of Four nos vêm à mente. Os tópicos anteriores desta resposta consideraram isso. Porém, com a ascensão do uso de frameworks de Inversão de Controle (IoC) e Injeção de Dependência (DI), existe hoje uma forma muito mais "segura" e flexível de implementar o padrão: deixe o framework cuidar disso para você! ###Como evitar o Singleton Apesar de implementações mais simples poderem usar o padrão Singleton da forma "clássica" sem problemas, em geral seria melhor aplicar a Inversão de Controle simplesmente deixar a instância de suas dependências serem injetadas. Dessa forma, caso amanhã os requisitos mudem e você preciso injetar diferentes instâncias, ou mesmo queira criar mocks de teste, basta configurar seu container ou framework para tal, sem alterar as implementações existentes. ## END ##
## Human: ## The construction of Solomon's temple includes a piece of furnishing described in 1 Kings 7:23 (ESV): Then he made the sea of cast metal. It was round, ten cubits from brim to brim, and five cubits high, and a line of thirty cubits measured its circumference. So if the ```diameter = 10``` cubits and the ```circumference = 30``` cubits, then ```π = 3``` by the equation ```C = π * D```. Of course, such an object does not exist since ```π = 3.14159...``` yet clearly the sea was constructed at some point. So how do we resolve this contradiction? ## Assistant: ## To start with, compare the circle the diameter we're given would make with the circle the circumference we're given would make: Since a circumference is π times the diameter, a 'pure' circle of 10 cubits in diameter as we describe the sea as having would be 10π cubits in circumference, or roughly 31.4 cubits. Now, since the circumference attributed to our sea is only 30 cubits, it represents a smaller circle, which is 30/π or roughly 9.55 cubits in diameter. Or to tabulate it: ```Circle A: ~9.55 cubits diameter, 30 cubits circumference Circle B: 10 cubits diameter, ~31.4 cubits circumference ``` Given that, we have two diameters differing by about .45 cubits (about eight inches on an 18-inch cubit--a sizable difference). Since we know the sea was a physical object and not a circle bounded by an infinitesimal line, we can safely understand that the sea must be of some thickness; on this ground, it would not be unreasonable to take the shorter dimension as the inner measurement, and the longer dimension as the outer measurement, and see where that takes us. Dividing the difference in the diameters in half, this would make the wall around our sea at least .225 cubits thick--i.e., about four inches on either end of the sea, assuming an eighteen-inch cubit. Do we have any authority for assuming that this is the case and saying the sea was anything like four inches thick? A couple of verses after this we have 1 Kings 7:26, which gives it to us outright: Its thickness was a handbreadth, and its brim was made like the brim of a cup, like the flower of a lily. It held two thousand baths. A handbreadth as a unit of measurement is generally given as between three and four inches. (The 'Number Pi in the Bible' site linked elsewhere gives as its rebuttal to this sort of argument the statement "The writer makes sure that no question remains: both the diameter and the circumference are taken over-all." - though I'm not sure on what basis he sees that.) ## END ##
## Human: ## With PHP 7.2, ```each``` is deprecated. The documentation says: Warning This function has been DEPRECATED as of PHP 7.2.0. Relying on this function is highly discouraged. How can I update my code to avoid using it? Here are some examples: * ```$ar = $o->me; reset($ar); list($typ, $val) = each($ar); ``` * ```$out = array('me' => array(), 'mytype' => 2, '_php_class' => null); $expected = each($out); ``` * ```for(reset($broken);$kv = each($broken);) {...} ``` * ```list(, $this->result) = each($this->cache_data); ``` * ```// iterating to the end of an array or a limit > the length of the array $i = 0; reset($array); while( (list($id, $item) = each($array)) || $i < 30 ) { // code $i++; } ``` When I execute the code on PHP 7.2 I receive the following error: Deprecated: The each() function is deprecated. This message will be suppressed on further calls ## Assistant: ## * For your first two example cases, you could use ```key()``` and ```current()``` to assign the values you need. ```$ar = $o->me; // reset isn't necessary, since you just created the array $typ = key($ar); $val = current($ar); ``` * ```$out = array('me' => array(), 'mytype' => 2, '_php_class' => null); $expected = [key($out), current($out)]; ``` In those cases, you can use ```next()``` to advance the cursor afterward, but it may not be necessary if the rest of your code doesn't depend on that. * For the third case, I'd suggest just using a ```foreach()``` loop instead and assigning ```$kv``` inside the loop. ```foreach ($broken as $k => $v) { $kv = [$k, $v]; } ``` * For the fourth case, it looks like the key is disregarded in ```list()```, so you can assign the current value. ```$this->result = current($this->cache_data); ``` Like the first two cases, it may be necessary to advance the cursor with ```next()``` depending on how the rest of your code interacts with ```$this->cache_data```. * Fifth can be replaced with a ```for()``` loop. ```reset($array); for ($i = 0; $i < 30; $i++) { $id = key($array); $item = current($array); // code next($array); } ``` ## END ##
## Human: ## Can someone please give an explanation of different egg preparations? ## Assistant: ## Sure, here are different preparations of eggs: 1. Fried Eggs: * Sunny Side Up -- Not flipped, unbroken yolk. The top of the egg is just barely set. * Basted -- Sunny Side Up, hot fat spooned over until the white surrounding the yolk is opaque. * Over Easy / lite -- Flipped, whites fully cooked, unbroken yolk, yolk runny. * Over medium -- flipped, whites fully cooked, unbroken yolk, yolk creamy (not completely runny). * Over Medium Well -- Flipped, unbroken yolk, yolk cooked to have a firm but wet-appearing center. * Over Hard -- Flipped, broken, fully-cooked yolk. * Over Well -- Flipped, intact, fully-cooked yolk. * Broken / Lightly Scrambled -- Broken in pan and gently stirred while cooking - yolk and whites should not be mixed entirely. * Scrambled Eggs -- Made in many different ways. Generally the eggs are mixed in a bowl before being put into the pan, and often stirred while cooking. Some recipes add fat to the eggs in the form of milk, * cream, butter, or oil. A distinction can be made between Wet/Loose or Dry, which refers to the degree of doneness. 2. Omelettes: * Filled Omelette -- Eggs mixed before cooking, possibly with added fat as in Scrambled Eggs. Cooked in fat in a saute pan; when set but the interior still wet, previously-cooked fillings (cheese, onions, mushrooms, peppers, tomatoes...) are added, and the eggs folded over into a half-moon shape. * Spanish Omelette / Western Omelette -- Same as filled, but the egg mixture is poured over the fillings in a hot pan and cooked, thus incorporating the fillings into the egg. * Fluffy Omelette -- Whites and yolks beaten separately. Yolks are gently folded into the whites without breaking the structure of the whites. Optional toppings are added. Cooked slowly in a pan, or baked (an electric frying pan with a lid works well for this preparation). * French Omelette -- Cooked soft & creamy with no color on the egg. Omelette is folded 1/3 in the pan, knocked to the edge so it can be rolled out onto the plate. It ends up being folded into thirds and is very creamy and soft. 3. Boiled: Cooked in shell in water for a timed period. Some people will refer to degree of doneness by cooking time, i.e., a &quot;3-minute egg&quot; is soft-boiled with some runny white around the yolk. Some recipes call for eggs to be added to boiling water, others to be started in cold water. In the cold-water start, the pot may be left on the heat or removed when the water reaches a boil. The eggs may be shocked in ice water when removed. * Soft -- Yolk runny, potentially with slight unset white around the yolk. * Medium -- White completely set, yolk firm but with a dark, wet appearance. * Hard -- Yolk completely set and pale yellow. 4. Poached: Egg cooked, out of shell, in water, stock, or other liquid -- excluding fats or oils -- at a temperature in the range of 160-180˚F (70-82˚C). There are possible degrees of doneness, however the typical poached egg has a runny but warm and thickened yolk and fully-set white. 5. Coddled: An egg that has been very lightly cooked (poached eggs are sometimes considered coddled). This can either be accomplished with an egg coddler or cooking an egg in its shell with water that is slightly below boiling point. 6. Shirred: An egg cooked in an oven at the low-mid 300's˚F (~160˚C), contained in a ramekin, until the whites are just set and the yolk is runny but thickened. Often butter or another fat is placed on top before cooking. 7. Steamed: Very similar to shirred eggs, but the ramekin is covered with some aluminum foil and put in a steamer for 7-15 minutes. 8. In a basket: Eggs fried in a hole made in a slice of bread. 9. deep fried Moo shu- using a strainer to drop small drops of egg into hot oil in a wok and stir. Frying in vegetables and/optionally meat or another protein then consume on mandarin pancakes with your choice of sauce. ## END ##
## Human: ## In regards to Error handling in PHP -- As far I know there are 3 styles: * ```die()```or ```exit()``` style: ```$con = mysql_connect("localhost","root","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } ``` * ```throw Exception``` style: ``` if (!function_exists('curl_init')) { throw new Exception('need the CURL PHP extension. Recomplie PHP with curl'); } ``` * ```trigger_error()``` style: ```if(!is_array($config) && isset($config)) { trigger_error('Error: config is not an array or is not set', E_USER_ERROR); } ``` Now, in the PHP manual all three methods are used. * What I want to know is which style should I prefer & why? * Are these 3 drop in replacements of each other & therefore can be used interchangeably? Is it just me or everyone thinks PHP error handling options are just too many to the extent it confuses php developers? ## Assistant: ## The first one should never be used in production code, since it's transporting information irrelevant to end-users (a user can't do anything about "Cannot connect to database"). You throw Exceptions if you know that at a certain critical code point, your application can fail and you want your code to recover across multiple call-levels. ```trigger_error()``` lets you fine-grain error reporting (by using different levels of error messages) and you can hide those errors from end-users (using ```set_error_handler()```) but still have them be displayed to you during testing. Also ```trigger_error()``` can produce non-fatal messages important during development that can be suppressed in production code using a custom error handler. You can produce fatal errors, too (```E_USER_ERROR```) but those aren't recoverable. If you trigger one of those, program execution stops at that point. This is why, for fatal errors, Exceptions should be used. This way, you'll have more control over your program's flow: ```// Example (pseudo-code for db queries): $db->query('START TRANSACTION'); try { while ($row = gather_data()) { $db->query('INSERT INTO `table` (`foo`,`bar`) VALUES(?,?)', ...); } $db->query('COMMIT'); } catch(Exception $e) { $db->query('ROLLBACK'); } ``` Here, if ```gather_data()``` just plain croaked (using ```E_USER_ERROR``` or ```die()```) there's a chance, previous ```INSERT``` statements would have made it into your database, even if not desired and you'd have no control over what's to happen next. ## END ##
## Human: ## How do I change the author and committer name/email for multiple commits? ## Assistant: ## Here is the solution, but be careful if you have a multi-user repository - this will change all commits to have the same (new) author and committer. ``` git filter-branch -f --env-filter " GIT_AUTHOR_NAME='Newname' GIT_AUTHOR_EMAIL='new@email' GIT_COMMITTER_NAME='Newname' GIT_COMMITTER_EMAIL='new@email' " HEAD ``` Changing the author (or committer) would require rewriting all of the history. If you're okay with that and think it's worth it then you should check out git filter-branch. The manual page includes several examples to get you started. Also note that you can use environment variables to change the name of the author, committer, dates, etc. -- see the "Environment Variables" section of the git manual page. Specifically, you can fix all the wrong author names and emails for all branches and tags with this command (source: GitHub help): ``` #!/bin/sh git filter-branch --env-filter ' OLD_EMAIL="your-old-email@example.com" CORRECT_NAME="Your Correct Name" CORRECT_EMAIL="your-correct-email@example.com" if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ] then export GIT_COMMITTER_NAME="$CORRECT_NAME" export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL" fi if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ] then export GIT_AUTHOR_NAME="$CORRECT_NAME" export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL" fi ' --tag-name-filter cat -- --branches --tags ``` For using alternative history filtering tool `git filter-repo`, you can first install it and construct a `git-mailmap` according to the format of `gitmailmap`. ``` Proper Name <proper@email.xx> Commit Name <commit@email.xx> ``` And then run filter-repo with the created mailmap: ``` git filter-repo --mailmap git-mailmap ``` ## END ##
## Human: ## This summer I will be taking a rather inherently dangerous multi-day hike by myself. I was considering buying a flare gun since I will be out of cellular range unless there is something more modern and equally reliable. Any suggestions? ## Assistant: ## In increasing order of complexity and price you have, * Personal Locator Beacons Personal locator beacons are high-powered (typically, 5 watts) devices designed primarily to send out a personalized emergency distress signal. They generally require an open view of the sky to transmit successfully. * Satellite Messengers (Like a Garmin inReach) Much like PLBs, satellite messengers are handheld transmitting devices that are useful in backcountry areas far from reliable cell phone coverage. These user-friendly devices allow you to communicate short text messages and/or your location coordinates with friends or family back home so you can report on your trip’s status or, in an emergency, send calls for help. * Satellite Phones A satellite telephone, satellite phone, or satphone is a type of mobile phone that connects to orbiting satellites instead of terrestrial cell sites. They provide similar functionality to terrestrial mobile telephones; voice, short messaging service and low-bandwidth internet access are supported through most systems. None of these are a substitute for good judgement and the electronics can fail or run out of batteries, but they would be the modern equivalent of a flare gun. ## END ##
## Human: ## In D&D, how many attacks can a fighter with two-weapon fighting make at 5th level? ## Assistant: ## Answer: 3 attacks on your turn (if you don't use Action Surge). (It could go up to five with Action Surge, though.) The Two-Weapon Fighting rule states (PHB, page 195): >When you take the Attack action and attack with a light melee weapon that you’re holding in one hand, you can use a bonus action to attack with a different light melee weapon that you’re holding in the other hand. You don’t add your ability modifier to the damage of the bonus attack, unless that modifier is negative. If either weapon has the thrown property, you can throw the weapon, instead of making a melee attack with it. The bonus action used as part of Two-Weapon Fighting is an attack, but it is not the Attack action. When you are a 5th-level fighter and you take the Attack action, you can make two attacks. You can also use your bonus action to make an additional attack. Notably, you can only take one bonus action per turn, since the rules for bonus actions state: You can take only one bonus action on your turn, so you must choose which bonus action to use when you have more than one available. You only add your ability modifier to the damage of that bonus-action attack if you have the Two-Weapon Fighting fighting style (PHB, page 72): When you engage in two-weapon fighting, you can add your ability modifier to the damage of the second attack. Therefore, you are taking the Attack action (2 attacks) and then making an additional attack with your bonus action (1 attack), for a total of 3 attacks if you're using your action plus your bonus action. There's a clear difference between them. Action Surge lets a 5th-level fighter attack up to 5 times in one turn, once per short rest. The description of the Action Surge feature says (PHB, page 72): Starting at 2nd level, you can push yourself beyond your normal limits for a moment. On your turn, you can take one additional action. Once you use this feature, you must finish a short or long rest before you can use it again. Starting at 17th level, you can use it twice before a rest, but only once on the same turn. Which means that if you use Action Surge, you would have 2 actions and 1 bonus action on your turn. So with this, you can take your Attack action (2 attacks), then use Action Surge and take another Attack action (2 more attacks), and finally your Two-Weapon Fighting bonus action (one more attack), for a total of 5 attacks during your turn. ## END ##
## Human: ## Consider the following code: ```avgDists = np.array([1, 8, 6, 9, 4]) ids = avgDists.argsort()[:n] ``` This gives me indices of the ```n``` smallest elements. Is it possible to use this same ```argsort``` in descending order to get the indices of ```n``` highest elements? ## Assistant: ## If you negate an array, the lowest elements become the highest elements and vice-versa. Therefore, the indices of the `n` highest elements are: ``` (-avgDists).argsort()[:n] ``` Another way to reason about this, as mentioned in the comments, is to observe that the big elements are coming last in the argsort. So, you can read from the tail of the argsort to find the `n` highest elements: ``` avgDists.argsort()[::-1][:n] ``` Both methods are O(n log n) in time complexity, because the argsort call is the dominant term here. But the second approach has a nice advantage: it replaces an O(n) negation of the array with an O(1) slice. If you're working with small arrays inside loops then you may get some performance gains from avoiding that negation, and if you're working with huge arrays then you can save on memory usage because the negation creates a copy of the entire array. Note that these methods do not always give equivalent results: if a stable sort implementation is requested to argsort, e.g. by passing the keyword argument kind='mergesort', then the first strategy will preserve the sorting stability, but the second strategy will break stability (i.e. the positions of equal items will get reversed). ## END ##
## Human: ## Relationship between SVD and PCA. How to use SVD to perform PCA? ## Assistant: ## Let the real values data matrix $\mathbf X$ be of $n \times p$ size, where $n$ is the number of samples and $p$ is the number of variables. Let us assume that it is centered, i.e. column means have been subtracted and are now equal to zero. Then the $p \times p$ covariance matrix $\mathbf C$ is given by $\mathbf C = \mathbf X^\top \mathbf X/(n-1)$. It is a symmetric matrix and so it can be diagonalized: $$\mathbf C = \mathbf V \mathbf L \mathbf V^\top,$$ where $\mathbf V$ is a matrix of eigenvectors (each column is an eigenvector) and $\mathbf L$ is a diagonal matrix with eigenvalues $\lambda_i$ in the decreasing order on the diagonal. The eigenvectors are called principal axes or principal directions of the data. Projections of the data on the principal axes are called principal components, also known as PC scores; these can be seen as new, transformed, variables. The $j$-th principal component is given by $j$-th column of $\mathbf {XV}$. The coordinates of the $i$-th data point in the new PC space are given by the $i$-th row of $\mathbf{XV}$. If we now perform singular value decomposition of $\mathbf X$, we obtain a decomposition $$\mathbf X = \mathbf U \mathbf S \mathbf V^\top,$$ where $\mathbf U$ is a unitary matrix (with columns called left singular vectors), $\mathbf S$ is the diagonal matrix of singular values $s_i$ and $\mathbf V$ columns are called right singular vectors. From here one can easily see that $$\mathbf C = \mathbf V \mathbf S \mathbf U^\top \mathbf U \mathbf S \mathbf V^\top /(n-1) = \mathbf V \frac{\mathbf S^2}{n-1}\mathbf V^\top,$$ meaning that right singular vectors $\mathbf V$ are principal directions (eigenvectors) and that singular values are related to the eigenvalues of covariance matrix via $\lambda_i = s_i^2/(n-1)$. Principal components are given by $\mathbf X \mathbf V = \mathbf U \mathbf S \mathbf V^\top \mathbf V = \mathbf U \mathbf S$. To summarize: 1. If $\mathbf X = \mathbf U \mathbf S \mathbf V^\top$, then the columns of $\mathbf V$ are principal directions/axes (eigenvectors). 2. Columns of $\mathbf {US}$ are principal components (&quot;scores&quot;). 3. Singular values are related to the eigenvalues of covariance matrix via $\lambda_i = s_i^2/(n-1)$. Eigenvalues $\lambda_i$ show variances of the respective PCs. 4. Standardized scores are given by columns of $\sqrt{n-1}\mathbf U$ and loadings are given by columns of $\mathbf V \mathbf S/\sqrt{n-1}$. See e.g. here and here for why &quot;loadings&quot; should not be confused with principal directions. 5. The above is correct only if $\mathbf X$ is centered. Only then is covariance matrix equal to $\mathbf X^\top \mathbf X/(n-1)$. 6. The above is correct only for $\mathbf X$ having samples in rows and variables in columns. If variables are in rows and samples in columns, then $\mathbf U$ and $\mathbf V$ exchange interpretations. 7. If one wants to perform PCA on a correlation matrix (instead of a covariance matrix), then columns of $\mathbf X$ should not only be centered, but standardized as well, i.e. divided by their standard deviations. 8. To reduce the dimensionality of the data from $p$ to $k&lt;p$, select $k$ first columns of $\mathbf U$, and $k\times k$ upper-left part of $\mathbf S$. Their product $\mathbf U_k \mathbf S_k$ is the required $n \times k$ matrix containing first $k$ PCs. 9. Further multiplying the first $k$ PCs by the corresponding principal axes $\mathbf V_k^\top$ yields $\mathbf X_k = \mathbf U_k^\vphantom \top \mathbf S_k^\vphantom \top \mathbf V_k^\top$ matrix that has the original $n \times p$ size but is of lower rank (of rank $k$). This matrix $\mathbf X_k$ provides a reconstruction of the original data from the first $k$ PCs. It has the lowest possible reconstruction error, see my answer here. 10. Strictly speaking, $\mathbf U$ is of $n\times n$ size and $\mathbf V$ is of $p \times p$ size. However, if $n&gt;p$ then the last $n-p$ columns of $\mathbf U$ are arbitrary (and corresponding rows of $\mathbf S$ are constant zero); one should therefore use an economy size (or thin) SVD that returns $\mathbf U$ of $n\times p$ size, dropping the useless columns. For large $n\gg p$ the matrix $\mathbf U$ would otherwise be unnecessarily huge. The same applies for an opposite situation of $n\ll p$. ## END ##
## Human: ## What is the English term for when someone thinks they are doing something nice for you but it ends up making things worse. EX: Someone buys you an elephant -- nice gesture and cool! But now you have to take care of it, and it becomes a burden on you. ## Assistant: ## In fact, the English expression for a burdensome gift is literally white elephant: > a thing that is useless and no longer needed, although it may have cost a lot of money [OALD] So-called white elephants, or albino elephants, are found in many parts of South and Southeast Asia. In Buddhist countries they may be venerated as Queen Maya, mother of the Buddha, was said to have been visited in a dream by a white elephant holding a white lotus flower, and Siddharth Gautama entered his mother's womb in the form a white elephant. The white elephant is also associated with traits like mental strength and purity. It became a royal symbol in Siam (Thailand); the king continues to keep white elephants. The story emerged that if a courtier displeased him, the king would make him a gift of a white elephant. The courtier could hardly decline a royal gift, and could hardly afford not to maintain a sacred animal, and could not put it to productive use, and so would be ruined by the cost of upkeep. The earliest example of its use is from a 1721 essay in London Journal: In short, Honour and Victory are generally no more than white Elephants; and for white Elephants the most destructive Wars have been often made. A 2011 paper by Ross Bullen entitled “This Alarming Generosity”: White Elephants and the Logic of the Gift, in American Literature, covers the popularization of the term in the mid-19th century, presents an alternative account, that the story is a piece of orientalism and the white elephant rose as a literary trope. ## END ##
## Human: ## Why did the NES not allow rotated sprites? ## Assistant: ## For each sprite displayed on a scanline, the hardware fetches two bytes from memory, and then clocks the pixels out one by one. The sprite is eight pixels wide, and each pixel is two bits, which is why it's two memory accesses per sprite per scanline. You can imagine that this arrangement just needs a couple of shift registers to clock the pixels out. Now, flipping the sprite about its vertical axis is easy: you just clock the pixels out in reverse order! Similarly, flipping the sprite about its horizontal axis is also easy: You just fetch the bitmap from memory in reverse order. Rotating by 180° of course is the same as flipping both horizontally and vertically. But if you wanted to rotate the sprite 90° or 270°, that's much harder. That's because the hardware would need to get one pixel from each pair of bytes, which means fetching 16 bytes from memory instead of just two. There is not enough time on this slow hardware to do that. Incidentally, this is also where the maximum number of sprites per scanline limitation comes from. It's a similar story on the Commodore 64, the Atari 2600, and many others: These platforms can do some simple 2D manipulations on sprites like stretching and sometimes flipping like you've seen, but never rotations. ## END ##
## Human: ## I want to merge two dictionaries into a new dictionary. ``` x = {'a': 1, 'b': 2} y = {'b': 3, 'c': 4} z = merge(x, y) &gt;&gt;&gt; z {'a': 1, 'b': 3, 'c': 4} ``` ## Assistant: ## Here is what you can do for the case you described: ``` z = dict(list(x.items()) + list(y.items())) ``` This will, as you want it, put the final dict in ```z```, and make the value for key ```b``` be properly overridden by the second (```y```) dict's value: ```&gt;&gt;&gt; x = {'a': 1, 'b': 2} &gt;&gt;&gt; y = {'b': 10, 'c': 11} &gt;&gt;&gt; z = dict(list(x.items()) + list(y.items())) &gt;&gt;&gt; z {'a': 1, 'c': 11, 'b': 10} ``` If you use Python 2, you can even remove the ```list()``` calls. To create z: ```&gt;&gt;&gt; z = dict(x.items() + y.items()) &gt;&gt;&gt; z {'a': 1, 'c': 11, 'b': 10} ``` If you use Python version 3.9.0a4 or greater, you can directly use: ```&gt;&gt;&gt; x = {'a': 1, 'b': 2} &gt;&gt;&gt; y = {'b': 10, 'c': 11} &gt;&gt;&gt; z = x | y &gt;&gt;&gt; z {'a': 1, 'c': 11, 'b': 10} ``` ## END ##
## Human: ## There are many "standards" for the JSON content type: application/json application/x-javascript text/javascript text/x-javascript text/x-json Which one do I use, and where? I assume security and browser support issues are a factor. ## Assistant: ## According to RFC 4627: The MIME media type for JSON text is application/json. The default encoding is UTF-8. For JSON text, you should use: ``` application/json ``` For JSONP (runnable JavaScript) with callback, you should use: ``` application/javascript ``` ## END ##
## Human: ## What's the goal of Minecraft and what can users achieve in this world? ## Assistant: ## The thing about Minecraft is, unlike most games you set your own goals. Here's a list of common (and uncommon!) things people have done with Minecraft: * Building a massive structure * Recreating real-world objects * Survival (Deleting map upon dying) * Making amusing videos * Creating an adventure map for others to play * Getting all the achievements * Proposing to your girlfriend * Achieving difficult things in the game through ingenuity * Computation * Blowing stuff up! The mechanism that keeps the game going is your own desire to complete the goals you've set yourself, and pure, red-hot addiction. Some additional stuff people have done: * Walking and walking and walking (thanks SevenSidedDie) * Build an epic railway * Make music * Find the Far Lands Minecraft 1.9+ In Minecraft 1.9 and beyond, there are villages, strongholds, abandoned mineshafts and nether ruins to explore. In particular, strongholds will contain portals to The End, in which players can fight The Ender Dragon, a 'boss fights'. The second 'boss fight' has also been added, The Wither. This makes exploration a key part of the game, as well as combat. 'Hardcore Mode' (world deletion upon death) is also an option if you're after a challenge. A creative mode has also been added that allows players complete freedom in constructing large structures, compartmentalizing that aspect of gameplay. ## END ##
## Human: ## If I have sampled a signal using proper sampling methods (Nyquist, filtering, etc) how do I relate the length of my FFT to the resulting frequency resolution I can obtain? Like if I have a 2,000 Hz and 1,999 Hz sine wave, how would I determine the length of FFT needed to accurately tell the difference between those two waves? ## Assistant: ## The frequency resolution is dependent on the relationship between the FFT length and the sampling rate of the input signal. If we collect 8192 samples for the FFT then we will have: $$\frac{8192\ \text{samples}}{2} = 4096\ \,\text{FFT bins}$$ If our sampling rate is 10 kHz, then the Nyquist-Shannon sampling theorem says that our signal can contain frequency content up to 5 kHz. Then, our frequency bin resolution is: $$\frac{5\ \text{kHz}}{4096\ \,\text{FFT bins}} \simeq \frac{1.22\ \text{Hz}}{\text{bin}}$$ This is may be the easier way to explain it conceptually but simplified: &nbsp;your bin resolution is just \$\frac{f_{samp}}{N}\$, where \$f_{samp}\$ is the input signal's sampling rate and N is the number of FFT points used (sample length). We can see from the above that to get smaller FFT bins we can either run a longer FFT (that is, take more samples at the same rate before running the FFT) or decrease our sampling rate. ## The Catch: There is always a trade-off between temporal resolution and frequency resolution. In the example above, we need to collect 8192 samples before we can run the FFT, which when sampling at 10 kHz takes 0.82 seconds. If we tried to get smaller FFT bins by running a longer FFT it would take even longer to collect the needed samples. That may be OK, it may not be. The important point is that at a fixed sampling rate, increasing frequency resolution decreases temporal resolution. That is the more accurate your measurement in the frequency domain, the less accurate you can be in the time domain. You effectively lose all time information inside the FFT length. In this example, if a 1999 Hz tone starts and stops in the first half of the 8192 sample FFT and a 2002 Hz tone plays in the second half of the window, we would see both, but they would appear to have occurred at the same time. You also have to consider processing time. A 8192 point FFT takes some decent processing power. A way to reduce this need is to reduce the sampling rate, which is the second way to increase frequency resolution. In your example, if you drop your sampling rate to something like 4096 Hz, then you only need a 4096 point FFT to achieve 1 Hz bins *4096 Hz, then you only need a 4096 point FFT to achieve 1hz bins and can still resolve a 2khz signal. This reduces the FFT bin size, but also reduces the bandwidth of the signal. Ultimately with an FFT there will always be a trade off between frequency resolution and time resolution. You have to perform a bit of a balancing act to reach all goals. ## END ##
## Human: ## I wanted to produce a 1 GB random file, so I used following command. ```dd if=/dev/urandom of=output bs=1G count=1 ``` But instead every time I launch this command I get a 32 MB file: ```$ dd if=/dev/urandom of=output bs=1G count=1 0+1 records in 0+1 records out 33554431 bytes (34 MB, 32 MiB) copied, 0,288321 s, 116 MB/s ``` What is wrong? ## Assistant: ## ```bs```, the buffer size, means the size of a single read() call done by dd. For example, both ```bs=1M count=1``` and ```bs=1k count=1k``` will result in a 1 MiB file, but the first version will do it in a single step, while the second will do it in 1024 small chunks. Regular files can be read at nearly any buffer size (as long as that buffer fits in RAM), but devices and "virtual" files often work very close to the individual calls and have some arbitrary restriction of how much data they'll produce per read() call. For ```/dev/urandom```, this limit is defined in urandom_read() in drivers/char/random.c: ```#define ENTROPY_SHIFT 3 static ssize_t urandom_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos) { nbytes = min_t(size_t, nbytes, INT_MAX >> (ENTROPY_SHIFT + 3)); ... } ``` This means that every time the function is called, it will clamp the requested size to 33554431 bytes. By default, unlike most other tools, dd will not retry after receiving less data than requested – you get the 32 MiB and that's it. (To make it retry automatically, as in Kamil's answer, you'll need to specify ```iflag=fullblock```.) Note also that "the size of a single read()" means that the whole buffer must fit in memory at once, so massive block sizes also correspond to massive memory usage by dd. And it's all pointless because you usually won't gain any performance when going above ~16–32 MiB blocks – syscalls aren't the slow part here, the random number generator is. So for simplicity, just use ```head -c 1G /dev/urandom > output```. ## END ##
## Human: ## The treads on my road bike's 28C tires are almost completely gone—they only persist along the shoulders of the tire. Do the treads matter? What is a good indicator of when the tire as a whole needs to be replaced? ## Assistant: ## The tread on a road bike's tires is really quite unimportant and purely cosmetic. Road bike tires have tread patterns because they sell better, not because they perform better. Here's some questions to ask to decide about replacing a tire: * Can you see any of the fabric? * Are you getting flats more easily than you used to? * Is any part of the tire bulging out or lumpy? * Are there visible holes/gashes in the tire? * Does the wear indicator indicate you should replace? (Some tires have dimples or grooves that are there to indicate when the tire should be replaced. Some have an alternate color of rubber underneath, and when that shows through the tire should be replaced) Smooth tread is actually ideal for road conditions, but not commonly found on tires. If you're on hard surfaces such as pavement/asphalt, the tread doesn't help you and actually slightly reduces contact area and increases rolling resistance. On most road tires the minimal tread pattern is just cosmetic to make their customers feel better (since a smooth tire looks slippery). See also: Sheldon Brown's comments on tire replacement and tread patterns If you plan to ride through mud, sand or other soft surfaces, you need tread. So when you no longer have tread on your mountain bike or cyclocross bike's tires, it's time to replace them (unless you plan to only use them on the road). ## END ##
## Human: ## Is there a way to create an index on a property/column using fluent configuration, instead of using the new ```IndexAttribute``` ? ## Assistant: ## Currently there is no "first class support" for creating a index via the fluent API, but what you can do is via the fluent API you can mark properties as having attributes from the Annotation API. This will allow you to add the ```Index``` attribute via a fluent interface. Here are some examples from the work item from Issues site for EF. Create a index on a single column: ```modelBuilder.Entity() .Property(e => e.MyProperty) .HasColumnAnnotation( IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute())); ``` Multiple indexes on a single column: ```modelBuilder.Entity() .Property(e => e.MyProperty) .HasColumnAnnotation( IndexAnnotation.AnnotationName, new IndexAnnotation(new[] { new IndexAttribute("Index1"), new IndexAttribute("Index2") { IsUnique = true } })); ``` Multi-Column indexes: ```modelBuilder.Entity() .Property(e => e.MyProperty1) .HasColumnAnnotation( IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("MyIndex", 1))); modelBuilder.Entity() .Property(e => e.MyProperty2) .HasColumnAnnotation( IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("MyIndex", 2))); ``` Using the above techniques will cause ```.CreateIndex()``` calls to be automatically created for you in your ```Up()``` function when you scaffold your next migration (or be automatically created in the database if you are not using migrations). ## END ##
## Human: ## When I do ```\footnote{}``` for a value in a table, the footnote doesn't show up. How do I get it to show up? Also, is it possible to get it to show up at the bottom of the table rather than the bottom of the page? ## Assistant: ## This is a classic difficulty in LaTeX. The problem is how to do layout with floats (figures and tables, an similar objects) and footnotes. In particular, it is hard to pick a place for a float with certainty that making room for the associated footnotes won't cause trouble. So the standard ```tabular``` and ```figure``` environments don't even try. What can you do: * Fake it. Just put a hardcoded vertical skip at the bottom of the caption and then write the footnote yourself (use ```\footnotesize``` for the size). You also have to manage the symbols or number yourself with ```\footnotemark```. Simple, but not very attractive, and the footnote does not appear at the bottom of the page. * Use the ```tabularx```, ```longtable```, ```threeparttable[x]``` (kudos to Joseph) or ```ctable``` which support this behavior. * Manage it by hand. Use ```[h!]``` (or ```[H]``` with the float package) to control where the float will appear, and ```\footnotetext``` on the same page to put the footnote where you want it. Again, use ```\footnotemark``` to install the symbol. Fragile and requires hand-tooling every instance. * The ```footnote``` package provides the ```savenote``` environment, which can be used to do this. * Minipage it (code stolen outright, and read the disclaimer about long caption texts in that case): \begin{figure} \begin{minipage}{\textwidth} ... \caption[Caption for LOF]% {Real caption\footnote{blah}} \end{minipage} \end{figure} Additional reference: TeX FAQ item Footnotes in tables. ## END ##
## Human: ## Why is kVA not the same as kW? ## Assistant: ## Both watts and volt-amps come from the same equation, $P=IV$, but the difference is how they're measured. To get volt-amps, you multiply root mean square (RMS) voltage ($V$) with RMS current ($I$) with no regard for the timing/phasing between them. This is what the wiring and pretty much all electrical/electronic components have to deal with. To get watts, you multiply instantaneous voltage ($V$) with instantaneous current ($I$) for every sample, then average those results. This is the energy that is actually transferred. Now to compare the two measurements: If voltage and current are both sinewaves, then $\text{watts} = \text{volt-amps} \times \cos(\phi)$, where $\phi$ is the phase angle between voltage and current. It's pretty easy to see from this that if they're both sine waves and if they're in phase ($\phi = 0$), then $\text{watts} = \text{volt-amps}$. However, if you're NOT dealing with sine waves, the $\cos(\phi)$ relationship no longer applies! So you have to go the long way around and actually do the measurements as described here. How might that happen? Easy. DC power supplies. They're everywhere, including battery chargers, and the vast majority of them only draw current at the peak of the AC voltage waveform because that's the only time that their filter capacitors are otherwise less than the input voltage. So they draw a big spike of current to recharge the caps, starting just before the voltage peak and ending right at the voltage peak, and then they draw nothing until the next peak. And of course there's an exception to this rule also, and that is Power Factor Correction (PFC). DC power supplies with PFC are specialized switching power supplies that end up producing more DC voltage than the highest AC peak, and they do it in such a way that their input current follows the input voltage almost exactly. Of course, this is only an approximation, but the goal is to get a close enough match that the $\cos(\phi)$ shortcut becomes acceptably close to accurate, with $\phi \approx 0$. Then, given this high voltage DC, a secondary switching supply produces what is actually required by the circuit being powered. ## END ##
## Human: ## In this Creation magazine reprint of a 1994 article titled Exploding stars point to a young universe, Young-Earth Creationist, Jonathan Sarfati argues that the scarcity of Supernova remnants (SNRs) in the sky suggests the Milky Way galaxy is less than billions of years old. On average, a galaxy like our own, the Milky Way, should produce one supernova every 25 years. [...] As can be readily seen above, a young universe model fits the data of the low number of observed SNRs. If the universe was really billions of years old, there are 7000 missing SNRs in our galaxy. Does astronomy predict a Milky Way supernova every 25 years? Are there missing SNRs that undermine these predictions? ## Assistant: ## There are many reasons why this is wrong. The first one is the assumption of 1 supernova per 25 years. That was the value arrived at in Gustav Tammann's article published in 1970. Others got values up to 100 years (Tammann himself changed his value later). All these values are really only good guesses. Worse errors are made in the percentage of remnants that should be visible. To start with, only a small percentage of them are visible; the others are obscured by dust in the galaxy. This does not happen to the same extent to the supernovas themselves, as they are intrinsically very much brighter. Sarfati also uses an old paper to come up with the number of remnants in the Large Magellanic Cloud (a satellite galaxy to the Milky Way). However, that figure was considered to be wrong (and much smaller) in later studies. Older remnants are very difficult to distinguish against a background of dust in the galaxy. Many of them disappear completely after about 10,000 years or so. It should be noted that, using Sarfati's own reasoning, we should not be able to see any remnants older than 6000 years. Instead, we know of remnants (e.g. G166.2 + 2.5) that are over 100,000 years old. How did those originate in his universe? The same goes for the distance to supernovas. The distance to SN 1987A has been trigonometrically measured at 167,000 light years (the paper says 51,200 parsecs). In other words, its light took more than 30 times longer to reach us than the creationist universe has supposedly existed. Remember also that a supernova typically becomes either a pulsar or a black hole, both of which are very hard to observe. A black hole can only be "seen" through its effects on other matter, and a pulsar is mainly visible if the solar system happens to be in line with its sweeping radio beam (with some pulsars the star itself has also been seen). Hence, all we can see of them long after the event is an expanding cloud of gas that gets dimmer over the millennia. There are several other reasons which are covered in a very good article on TalkOrigins. That article has links to all the original scientific papers referred to, so you can check the validity yourself. ## END ##
## Human: ## Why are we building larger land-based telescopes instead of launching larger ones into space? ## Assistant: ## Because it's cheaper. (1) With adaptive optics you can get 0.1 arc second resolution on the ground (admittedly only on a mountain top with particularly good air flow, but still!). This eliminates one of the major advantages of space until you get above several meters mirror diameter. (2) Rocket fairings are the shrouds which protect payloads during the supersonic atmospherics speeds reached during launch. A 5 meter fairing is about the largest that can be flown, which limits the size of the one-piece mirrors which can be launched. (The Dreaded Webb Telescope's mirror is in pieces which will assemble themselves in space -- a very scary and very expensive piece of design.) (3) Servicing a telescope on the top of Mauna Kea or in the high Chilean Andes is a difficult and expensive process. Servicing a telescope in orbit makes that look like small change. (Cost comparable to the cost of building a new giant scope on Earth.) And in-orbit servicing can't even be done with current technology except in low earth orbit. (4) While high resolution is one frontier in astronomy, going deep is another, and going deep requires big mirrors. A 30 meter mirror on Earth gathers much more light than a 5 meter mirror in space. The giant terrestrial telescopes simply do a better job of being light buckets for spectroscopy than anything we can yet put into space. The bottom line is that with the development of adaptive optics, space-based telescopes of currently buildable and launchable size lost their main advantage over ground-based telescopes. And since they're 10x to 100x the cost, they are simply not worth building for many purposes. Space based telescopes still hold a significant edge in parts of the spectrum blocked by the atmosphere such as UV and IR (Webb), and for certain tasks involving long-term high accuracy photometry (Kepler) and astrometry (Gaia). But for general purpose use, the balance seems firmly on the ground's side for large telescopes. This will change if space flight becomes cheaper -- the SpaceX BFR, for example, with its 9 meter fairing and dramatically lower launch costs, offers great hope for space telescopes. ## END ##
## Human: ## Why can we see the dust particles in a narrow beam of light (and not in an all lighted area)? ## Assistant: ## Your inability to see the dust until you narrow the slit has nothing to do with the narrowness of the beam but instead the dynamic range of light that your eye can see at one time. A bit of searching turns up reports of a contrast ratio for you eye at one time as between 100:1 and 1000:1. This means if you're in a room with a range of brightness greater than about 100 to 1 the brightest things will all be washed out as white and the darkest things will all be essentially black. This is obvious in photos that are "backlit" like this one: These horses aren't black but because the ratio of the bright light to the dark horses exceeds the dynamic range of the camera the sky is washed out white and the horses are in silhouette. Your eye can adjust over time to a huge range but it can't utilize the whole range all at once. In the case of dust reflecting light, if you allow a lot of light into the room the relative brightness between the small amount of light the dust is reflecting and the rest of the illuminated room prevent you from seeing the dust. This is fundamental to signal processing. Why can't you hear a whisper in a noisy room? The noise of the crowd obscures the whisper. The difference between the signal you're trying to pick up and the background noise is called the signal-to-noise ratio. In the case of dust, the light let into the room is scattered and reflected in the room and causes the room to be illuminated. This is the noise that obscures the signal from light reflected off of the dust. ## END ##
## Human: ## What is the difference between ```warnings.warn()``` and ```logging.warn()``` in terms of what they do and how they should be used? ## Assistant: ## Here are the differences: * ```logging.warning``` just logs something at the ```WARNING``` level, in the same way that ```logging.info``` logs at the ```INFO``` level and ```logging.error``` logs at the ```ERROR``` level. It has no special behaviour. * ```warnings.warn``` emits a ```Warning```, which may be printed to ```stderr```, ignored completely, or thrown like a normal ```Exception``` (potentially crashing your application) depending upon the precise ```Warning``` subclass emitted and how you've configured your Warnings Filter. By default, warnings will be printed to ```stderr``` or ignored. * Warnings emitted by ```warnings.warn``` are often useful to know about, but easy to miss (especially if you're running a Python program in a background process and not capturing ```stderr```). For that reason, it can be helpful to have them logged. To use them, Python provides a built-in integration between the ```logging``` module and the ```warnings``` module to let you do this; just call ```logging.captureWarnings(True)``` at the start of your script and all warnings emitted by the ```warnings``` module will automatically be logged at level ```WARNING```. ## END ##
## Human: ## In Greek mythology, the words "Titan" and "God" seem to be used interchangeably. For example, Zeus is a God, but Cronus (his father) was a Titan. So what is the difference between a Titan and a God in Greek mythology? ## Assistant: ## A 'god' is synonymous to a 'deity'; the Titans and Titanesses were gods, they were members of the second order of divine beings - after Gaia and Uranus, and the other primordial deities. Cronus and his fellow Titans comprised the second Ancient Greek pantheon (the first comprising of deities such as Ananke, Gaea, and Ouranos), which the younger generation of gods, including Zeus, would later usurp, as the Titans usurped the primordial gods before them. (For more information on this concept of change in dominant deities, read up on the theory of The Golden Age, particularly that of Hesiod, who also wrote the only surviving account of the Titans in his Theogony.) As for why the Titans have their own order name as opposed to Zeus et al who are simply 'gods', there is debate as to the exact etymology of the word 'titan' but this quote from Hesiod's Theogony states: &quot;But these sons whom he begot himself great Heaven used to call Titans (Strainers) in reproach, for he said that they strained (τιταίνοντας) and did presumptuous a fearful deed, and that vengeance (τίσιν) for it would come afterwards.&quot; Trans. Evelyn-White, here with side-by-side Greek. However this is not to say Hesiod was correct; he himself was writing long after the supposed Golden Age ended. ## END ##
## Human: ## I am currently trying to decipher Mazur's Eisenstein ideal paper (not a comment about his clarity, rather about my current abilities). One of the reasons I am doing that is that many people told me that the paper was somehow revolutionary and introduced a new method into number theory. Could you explain exactly what subsequent developments did the paper bring, what ideas in the paper were considered more-or-less original (at the time it was published), and exactly what difficulties did these ideas resolve that people failed to resolve before the paper was published (if any)? ## Assistant: ## First, Mazur's paper is arguably the first paper where the new ideas (and language) of the Grothendieck revolution in algebraic geometry were fully embraced and crucially used in pure number theory. Here are several notable examples: Mazur makes crucial use of the theory of finite flat group schemes to understand the behavior of the $p$-adic Tate modules of Jacobians at the prime $p$. He studies modular forms of level one over finite rings (which need not lift to characteristic zero when the residue characteristic is $2$ or $3$). He proves theorems about mod-$p$ modular forms using what are essentially comparison theorems between etale cohomology and de Rham cohomology, and many more examples. The proof of the main theorem ($\S5$, starting at page 156) is itself a very modern proof which fundamentally uses the viewpoint of $X_0(N)$ as a scheme. Second, there are many beautiful ideas which have their original in this paper: it contains many of the first innovative ideas for studying $2$-dimensional (and beyond) Galois representations, including the link between geometric properties (multiplicity one) and arithmetic properties, geometric conceptions for studying congruences between Galois representations, understanding the importance of the finite-flat property of group schemes, and the identification of the Gorenstein property. There is a theoretical $p$-descent on the Eisenstein quotient when previously descents were almost all explicit $2$-descents with specific equations. It introduces the winding quotient, and so on. Third, while it is a dense paper, it is dense in the best possible way: many of the small diversions could have made interesting papers on their own. Indeed, even close readers of the paper today can find connections between Mazur's asides and cutting edge mathematics. When Mazur raises a question in the text, it is almost invariably very interesting. One particular (great) habit that Mazur has is thinking about various isomorphisms and by pinning down various canonical choices identifies refined invariants. To take a random example, consider his exploration of the Shimura subgroup at the end of section 11. He finishes with a question which to a casual reader may as well be a throw-away remark. But this question was first solved by Merel, and more recently generalized in some very nice work of Emmanuel Lecouturier. Lecouturier's ideas then played an important role in the work of Michael Harris and Akshay Venkatesh. Again, one could give many more such examples of this. Very few papers have the richness of footnotes and asides that this paper does. Never forget that one of the hardest things in mathematics is coming up with interesting questions and observations, and this paper contains many great ones - it is bursting with the ideas of a truly creative mathematician. Finally, the result itself is amazing, and (pretty much) remains the only method available for proving the main theorem (the second proof due to Mazur is very related to this one). To give a sense of how great the theorem is, note that if $E$ is a semistable elliptic curve, then either $E$ is isogenous to a curve with a $p$-torsion point, or $E[p]$ is absolutely irreducible. This result (added for clarity: explicitly, Mazur's Theorem that $E/\mathbf{Q}$ doesn't have a $p$-torsion point for $p > 7$) is crucially used in Wiles' proof of Fermat. One could certainly argue that without this paper (and how it transformed algebraic number theory) we would not have had Wiles' proof of Fermat, but it's even literally true that Mazur's theorem was (and remains so today, over 40 years later) an essential step in any proof of Fermat. ## END ##
## Human: ## Tracing XML request/responses with JAX-WS ## Assistant: ## Here is the solution in raw code: ```Endpoint ep = Endpoint.create(new WebserviceImpl()); List handlerChain = ep.getBinding().getHandlerChain(); handlerChain.add(new SOAPLoggingHandler()); ep.getBinding().setHandlerChain(handlerChain); ep.publish(publishURL); ``` Where SOAPLoggingHandler is (ripped from linked examples): ```package com.myfirm.util.logging.ws; import java.io.PrintStream; import java.util.Map; import java.util.Set; import javax.xml.namespace.QName; import javax.xml.soap.SOAPMessage; import javax.xml.ws.handler.MessageContext; import javax.xml.ws.handler.soap.SOAPHandler; import javax.xml.ws.handler.soap.SOAPMessageContext; /* * This simple SOAPHandler will output the contents of incoming * and outgoing messages. */ public class SOAPLoggingHandler implements SOAPHandler { // change this to redirect output if desired private static PrintStream out = System.out; public Set getHeaders() { return null; } public boolean handleMessage(SOAPMessageContext smc) { logToSystemOut(smc); return true; } public boolean handleFault(SOAPMessageContext smc) { logToSystemOut(smc); return true; } // nothing to clean up public void close(MessageContext messageContext) { } /* * Check the MESSAGE_OUTBOUND_PROPERTY in the context * to see if this is an outgoing or incoming message. * Write a brief message to the print stream and * output the message. The writeTo() method can throw * SOAPException or IOException */ private void logToSystemOut(SOAPMessageContext smc) { Boolean outboundProperty = (Boolean) smc.get (MessageContext.MESSAGE_OUTBOUND_PROPERTY); if (outboundProperty.booleanValue()) { out.println("\nOutbound message:"); } else { out.println("\nInbound message:"); } SOAPMessage message = smc.getMessage(); try { message.writeTo(out); out.println(""); // just to add a newline } catch (Exception e) { out.println("Exception in handler: " + e); } } } ``` ## END ##
## Human: ## In Vim, how do I insert characters at the beginning of each line in a selection? For instance, I want to comment out a block of code by prepending ```//``` at the beginning of each line assuming my language's comment system doesn't allow block commenting like ```/* */```. How would I do this? ## Assistant: ## The general pattern for search and replace is: ```:s/search/replace/ ``` Replaces the first occurrence of 'search' with 'replace' for current line ```:s/search/replace/g ``` Replaces all occurrences of 'search' with 'replace' for current line, 'g' is short for 'global' This command will replace each occurrence of 'search' with 'replace' for the current line only. The % is used to search over the whole file. To confirm each replacement interactively append a 'c' for confirm: ```:%s/search/replace/c ``` Interactive confirm replacing 'search' with 'replace' for the entire file Instead of the % character you can use a line number range (note that the '^' character is a special search character for the start of line): ```:14,20s/^/#/ ``` Inserts a '#' character at the start of lines 14-20 If you want to use another comment character (like //) then change your command delimiter: ```:14,20s!^!//! ``` Inserts a '//' character sequence at the start of lines 14-20 Or you can always just escape the // characters like: ```:14,20s/^/\/\// ``` Inserts a '//' character sequence at the start of lines 14-20 If you are not seeing line numbers in your editor, simply type the following ```:set nu ``` Hope this helps! ## END ##
## Human: ## Why doesn't the nuclear fusion in a star make it explode? ## Assistant: ## The fusion that occurs in the core of the Sun occurs in nothing like the conditions you might be thinking of in a bomb, or a fusion reactor. In particular, it occurs at much lower temperatures and at a much lower rate. A cubic metre of material in the solar core is only releasing around 250 W of power by fusion. The fusion rate is set by the temperature (and to a lesser extent, density) of the core. This in turn is set by the need for a pressure gradient to balance the weight of material pressing down on it from above. At 15 million kelvin (the core temperature, which is much lower than the temperatures in nuclear bombs or fusion reactors), the average proton has a lifetime of several billion years before being converted (with three others) into a helium nucleus. There are two reasons this is slow. First, you have to get protons, which repel each other electromagnetically, close enough together to feel the strong nuclear force. This is why high temperatures are needed. Second, because the diproton is unstable, one of the protons needs to change into a neutron via a weak force interaction, whilst it is in the unstable diproton state, to form a deuterium nucleus. This is just inherently unlikely and means the overall reaction chain to helium is very slow. The reason there is no bomb-like explosion is because there is no problem in shifting 250 W per cubic metre away from the core, in the same way that a compost heap, which generates about the same power density, does not spontaneously explode. In the case of a star any additional heat goes into more radiation that diffuses away and in work done in expanding the star. As a result, the temperature of the core is stable. Ultimately, any additional energy emerges as sunlight at the solar photosphere. If for some reason, the opacity to radiation in the core increased, then the temperature would rise and more energy would be generated by fusion. This is exactly what happens in the core as more hydrogen is turned into helium; the core temperature and luminosity do rise, but slowly, on timescales of billions of years. ## END ##
## Human: ## Does hot water freeze faster than cold water? ## Assistant: ## In certain settings, cold water freezers slower than hot water. This is called the Mpemba effect: Hot water can in fact freeze faster than cold water for a wide range of experimental conditions. This phenomenon is extremely counterintuitive, and surprising even to most scientists, but it is in fact real. It has been seen and studied in numerous experiments. While this phenomenon has been known for centuries, and was described by Aristotle, Bacon, and Descartes, it was not introduced to the modern scientific community until 1969, by a Tanzanian high school student named Mpemba. Here are some suggested reasons: 1. Evaporation — As the initially warmer water cools to the initial temperature of the initially cooler water, it may lose significant amounts of water to evaporation. The reduced mass will make it easier for the water to cool and freeze. Then the initially warmer water can freeze before the initially cooler water, but will make less ice. 2. Dissolved Gasses — Hot water can hold less dissolved gas than cold water, and large amounts of gas escape upon boiling. So the initially warmer water may have less dissolved gas than the initially cooler water. ## END ##
## Human: ## O que é Reflection. Por que é útil? * É recomendável usar em projetos? * Como usar? * Em quais situações Reflection pode ser usado? ## Assistant: ## Reflection é um termo usado para indicar a capacidade de obter metadados sobre o próprio programa compilado, em português pode-se referir a isso como reflexão mesmo. Como assim, metadados? Obter informações sobre os tipos de dados, de forma dinâmica, é muito importante para códigos que atuam sobre objetos que não se conhece em tempo de design. É muito usado em bibliotecas que se propõem a lidar com objetos de tipos que elas desconhecem em sua concepção. O ASP.NET MVC por exemplo, só é um projeto possível por causa da reflexão. De que forma ele poderia chamar os métodos dos controllers, que ele nem conhece? Visto, isso é preciso dizer que reflexão não é sempre rápido, é necessário verificar permissões, o nível de segurança em que o código está rodando, entre outras. Por isso, quando se obtém um resultado qualquer vindo através da reflexão, geralmente essas informações são colocadas em cache, ou seja, salvas em memória. É recomendável? Depende do uso: * sim, é recomendável: para criar comportamentos dinâmicos usando atributos, a única opção é reflexão * sim, é recomendável: para chamar método que não se conhece durante a concepção do sistema, também é a única opção * não, é abuso fazer isso: chamar um método, de uma classe conhecida em design-time usando reflexão... é absurdo Existem tantas formas de usar reflexão que eu poderia continuar essa lista por muito tempo. Então mais vale o seguinte conselho: bom senso. Como usar? Existem alguns vetores de uso de reflexão que eu conheço. Para demonstrá-los, vamos considerar primeiramente a seguinte classe: ```public class Pessoa { public string Nome { get; set; } } ``` * Expressões Lambda: esse é um vetor de reflexão, pois é possível usar expressões lambda para chegar aos tipos, métodos entre outros objetos refletidos, que tenham sido usados na expressão. É comumente utilizado para tornar fortemente tipada a obtenção de objetos refletidos de membros de uma classe, antes disso só era possível obter esses objetos indicando uma string com o nome do método. ```Expression> expr = p => p.Nome; var lambda = (LambdaExpression)expr; var cast = lambda.Body as UnaryExpression; var member = (cast != null ? cast.Operand : lambda.Body) as MemberExpression; var propInfo = member.Member as PropertyInfo; Console.WriteLine(propInfo.Name); // "Nome" ``` * typeof(Tipo): essa é uma das formas mais comuns de obter informações refletidas do sistema. Serve para obter informações sobre o tipo indicado diretamente. ```var tipo = typeof(Pessoa); Console.WriteLine(tipo.Name); // "Pessoa" ``` * obj.GetType(): tão comum quando ```typeof```, mas ao invés de se referir a um tipo diretamente, refere-se ao tipo do objeto em questão: mas tem um detalhe, não é do tipo declarado da variável, mas sim do objeto em si. ```var p = new Pessoa(); var tipo = p.GetType(); Console.WriteLine(tipo.IsSealed); // "false" ``` * Assembly: é usada para obter tipos em larga escala: por exemplo, para escanear todos os tipos existentes em um assembly, ou então em todos os assemblies carregados. ```// localizando um tipo, dentre todos os tipos carregados var todosOsTiposCarregados = AppDomain.CurrentDomain .GetAssemblies() .SelectMany(a => a.GetTypes()) .Where(t => t.Name == "Pessoa"); ``` E o namespace Reflection.Emit? Não está claro na minha mente se considero isso reflexão ou não. Mas de qualquer forma, é necessário usar reflexão para gerar código dinamicamente, então tem uma conexão. Como disse, esse namespace contém classes usadas para gerar código dinamicamente... e então compilá-los. Lembra do que eu falei sobre cache de reflexão... compilar métodos é um bom alvo de cache, pois depois de compilado os futuros usos serão muitíssimo rápidos. ## END ##
## Human: ## What is the difference between minimum and infimum? ## Assistant: ## The minimum is attained, the infimum isn't necessarily. Example. Let $f(x) = \frac{1}{x}$. Then $f$ has no minimum value on the interval $(0,\infty)$. The minimum is the smallest element in the set. That is $$ \min\{f(x)\mid x\in (0,\infty)\} $$ doesn't exist because there is not smallest number in the set. Another example is the minimum of the set $S = (0,1) = \{x\mid 0<x<1\}$. Here again there isn't a smallest number $$ \min\{x\mid 0<x<1\} $$ doesn't exist. The infimum of a set $S$ is defined as the greatest number that is less than or equal to all elements of S (from Wikipedia). The infimum is also sometimes called the greatest lower bound. It is a fact that every non empty set (bounded below) of real numbers has an infimum. But, as we saw, not every real set has a minimum. So in the example $$ \inf\{f(x)\mid x\in (0,\infty)\} = 0. $$ Note that the infimum and the minimum can be the same. Consider for example $S = \{1,2,3,\dots\}$. Then the infimum and minimum is both $1$. Consider this other example. If $f$ is a continuous function on a closed interval $[a,b]$, then it is a fact that $f$ attains a minimum over that interval. So here again $$ \inf\{f(x)\mid x\in [a,b]\} = \min\{f(x)\mid x\in [a,b]\}. $$ ## END ##
## Human: ## I have extremely bad posture, what can I do? ## Assistant: ## You can do stretches, strengthening and breathing exercises to improve your posture. But you also have to become aware of your posture and find a way to interrupt prolonged sitting intervals with brief breaks. A good ergonomic chair is also helpful. ## Musculo-Skeletal Effects of Poor Sitting Posture Poor sitting posture can create muscle imbalances over time. * In general, your front stuctures shorten, with muscles like your hip flexors and pectorals tightening up - pulling you into a ball. Your crunched sitting posture caves your lower ribs down towards your abdomen which prevents the diaphragm from expanding freely. * At the same time the muscles of your back stretch out and weaken, especially in the range where they should be working to maintain your posture. As this posture persists over time, you joints begin to lose normal range of motion as well making it more difficult to assume a good posture. ## Correcting Slumped Sitting Posture Tailor an exercise program to stretch tightened muscles and strengthen weakened muscles. Additionally, you need a method to become aware of your posture and correct it while you are sitting. This is difficult because as you say, your attention is on your work. Exercise programs like Yoga, Tai-Chi and Pilates are good because they all address and make you very aware of your posture, joint alignments, flexibility, core control and breathing. Use Specific Exercises to Correct Muscle Imbalances: * Back, Upper Back and Scapular muscles: Back Extensions strengthen your paraspinals. Use different arm positions (Y, T, W, L) to target your lower traps, mid traps, rhomboids and scapular stabilizors. Learn the feel of retracting your scapulas. You can do these on the floor next to your desk. Or if you prefer not to get on the floor, use resistance bands for reverse flys, wide rows, narrow rows and rotations. Away from work, you can also strengthen these muscles using weights, cables and body weight exercises like inverted rows, cable rows, bent over rows, reverse flys etc. And squats are a good for strengthening multiple muscles important to posture. * Core: Plank, Side Plank, Bird Dog and Bridge will stabilize your trunk and spine. * Stretches: - Hip Flexors, Hamstrings, Abs and Pecs. The wall pec stretch stretches the pecs but also contracts the rhomboid and trapezius scapular muscles to help improve the positioning of the shoulder and shoulder blade and is easy to do at work. * Sitting Posture and Breathing Awareness - This sitting posture exercise helps make you aware of your sitting alignment and expanding your diaphragm. Practice a few diaphramatic breaths throughout the day along with this sitting exercise to elongate your spine. Create your ideal short exercise routine that you can do during short breaks during the day. An exercise ball next to your desk can help you target these muscles in just a few minutes: * Lie back over the ball and stretch out your front * Lie face down over the ball and do the Y, W, T, L exercises. And consider using the ball as your desk chair for short periods. ## Remembering your Posture throughout the day: This is the hard part. As your muscle imbalances begin to resolve and your diaphragmatic breathing improves, you’ll find that the slouched posture becomes less comfortable and you will automatically begin to sit better. Until then, use a timed reminder or try tying your posture corrections to tasks that you do at the computer regularly. For example, correct your posture each time you check your email or some other specific link. Turn on your web cam for visual reminders. Keep at it until you feel the improvement. You'll have less problems going forward if you improve your posture. It either gets better - or it gets worse. ## END ##
## Human: ## How to add margin top to ```class="row"``` elements using twitter bootstrap framework? ## Assistant: ## Editing or overriding the row in Twitter bootstrap is a bad idea, because this is a core part of the page scaffolding and you will need rows without a top margin. To solve this, instead create a new class "top-buffer" that adds the standard margin that you need. ```.top-buffer { margin-top:20px; }``` And then use it on the row divs where you need a top margin. ```<div class="row top-buffer"> ...``` ## END ##
## Human: ## In FTL: Faster Than Light, what triggers crew experience increases? ## Assistant: ## ###Piloting A crew member who is controlling a helm gains one point of experience for each incoming projectile that is dodged during combat. This includes asteroids, so long as you are in combat at the time. (Note that the dodges do not count while you ship is under the effects of +60% evasion from a cloak.) This can be explicitly trained by finding an enemy that cannot break through your shields, and letting him fire at you. Increasing your dodge chance (i.e. more power to engines) will help this occur quicker, but even with the default chance you can max out the skill eventually. ###Engines Exactly as for piloting, a crew member manning the engines gains one point of XP for each projectile evaded while uncloaked in combat - and so it can be trained in the same way. ###Weapons A crew member who is manning the weapons station gains one point of experience for each weapon that is fired. It doesn't matter whether it hits or misses, or whether it can do damage (e.g. a beam weapon fired at a shield). (Multi-shot weapons such as burst lasers still count as a single fire order and hence a single XP.) This can be trained by firing non-lethal weapons (e.g. ones that do ion damage) repeatedly against a target that cannot damage you. ###Shields A crew member manning the shields station gains one point of experience for each shield "bubble" that is brought back up during combat. It doesn't matter why the shields were down, be that direct enemy fire, ion damage, asteroid strikes, or even manually removing and then restoring power to the shield systems. This can be trained by letting a target that cannot break through all of your shields repeatedly take one layer down. (For fastest results, move your pilot away from the helm, or reduce your engines to 0 power. This will reduce your dodge chance to 0%, ensuring that everything hits your shields.) Note that you used to be able to manually remove power and restore it in combat, to get points, but this has been patched. ###Repair A crew member gains one point of experience for getting the "finishing blow" when repairing a (sub)system. Note that hull breaches provide no repair experience. There's no specific way to train this as it requires your systems to be damaged. Though you can focus the XP gains by always having the same person run around and repair everything after a battle. ###Combat A crew member gains one point of experience for getting the finishing blow when fighting an enemy crew member or (sub)system. This generally gets trained in "real" situations. However, since XP is gained for taking out systems, a few extra XP can be gained from boarding if one tries to take out the enemy's systems before the crew. ## END ##
## Human: ## In the python built-in open function, what is the exact difference between the modes ```w```, ```a```, ```w+```, ```a+```, and ```r+```? In particular, the documentation implies that all of these will allow writing to the file, and says that it opens the files for "appending", "writing", and "updating" specifically, but does not define what these terms mean. ## Assistant: ## The opening modes are exactly the same as those for the C standard library function ```fopen()```. The BSD ```fopen``` manpage defines them as follows: The argument mode points to a string beginning with one of the following sequences (Additional characters may follow these sequences.): * ``r'' Open text file for reading. The stream is positioned at the beginning of the file. * ``r+'' Open for reading and writing. The stream is positioned at the beginning of the file. * ``w'' Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file. * ``w+'' Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file. * ``a'' Open for writing. The file is created if it does not exist. The stream is positioned at the end of the file. Subsequent writes to the file will always end up at the then current end of file, irrespective of any intervening fseek(3) or similar. * ``a+'' Open for reading and writing. The file is created if it does not exist. The stream is positioned at the end of the file. Subse- quent writes to the file will always end up at the then current end of file, irrespective of any intervening fseek(3) or similar. ## END ##
## Human: ## How can democracy not be the rule of the poor? ## Assistant: ## Here are some possible explanations why democracy is not necessarily the rule of the poor: * Voters voting by habit, not by analysis. Many people vote the same way their parents voted. Other vote exactly the opposite way. Neither involves analysis of their objective economic interest. * A middle class believing to be rich, a lower middle class believing to be middle class. In many countries, voters tend to vote in the interest of the class they aspire to be, not in the interest of the class they actually are. Part of that is self-deception about inequality and the own position. That is visible in tax policy, for instance, where the middle class votes against taxes on the upper class. * An upper class better able to work the political system than the poor. In some countries, attempts to simplify the access to the political system (e.g. through direct democracy or participatory budgeting) have backfired when it comes to the poor. These mechanisms remain complicated enough that it takes some familiarity with the political system to get anything done. Knowing how to organize a grassroots movement, how to write minutes of a meeting, how to collect signatures for a ballot initiative. So instead of better welfare in poor parts of town, there are better playgrounds in rich parts of town. ## END ##
## Human: ## How can I write colored text to the Windows console with C++? That is, how can I write different text with different colors? ## Assistant: ## ANSI escape color codes are: 0=black 1=blue 2=green and so on to 15=white; colorattribute = foreground + background * 16. To get red text on yellow use 4 + 14*16 = 228, light red on yellow would be 12 + 14*16 = 236. Here is an example for coloring text in the Windows console: ``` HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // you can loop k higher to see more color choices for(int k = 1; k < 255; k++) { // pick the colorattribute k you want SetConsoleTextAttribute(hConsole, k); cout << k << " I want to be nice today!" << endl; } ``` ## END ##
## Human: ## What's the best way to create a temporary file in Android? Can File.createTempFile be used? The documentation is very vague about it. In particular, it's not clear when temporary files created with ```File.createTempFile``` are deleted, if ever. ## Assistant: ## Here is an example: ``` File outputDir = context.getCacheDir(); // context being the Activity pointer File outputFile = File.createTempFile("prefix", ".extension", outputDir); ``` Explanation: 1. The `getCacheDir()` method returns the directory on the filesystem where your app can create cache files. 2. The `createTempFile()` function creates a temporary file in a given directory. ## END ##
## Human: ## I have javascript function like this: ```function myFunction(number) { var x=number; ... ... more initializations //here need to wait until flag==true while(flag==false) {} ... ... do something } ``` The problem is that the javascript is stuck in the while and stuck my program. so my question is how can I wait in the middle of the function until flag is true without "busy-wait"? ## Assistant: ## Because javascript in a browser is single threaded (except for webworkers which aren't involved here) and one thread of javascript execution runs to completion before another can run, your statement: ```while(flag==false) {} ``` will simply run forever (or until the browser complains about a non-responsive javascript loop), the page will appear to be hung and no other javascript will ever get a chance to run, thus the flag's value can never be changed. For a little more explanation, Javascript is an event driven language. That means that it runs a piece of Javascript until it returns control back to the interpreter. Then, only when it returns back to the interpreter, Javascript gets the next event from the event queue and runs it. All things like timers and network events run through the event queue. So, when a timer fires or a network request arrives, it does not ever "interrupt" the currently running Javascript. Instead, an event gets put in the Javascript event queue and then, when the currently running Javascript finishes, the next event is pulled from the event queue and it gets its turn to run. So, when you do an infinite loop such as ```while(flag==false) {}```, the currently running Javascript never finishes and thus the next event is never pulled from the event queue and thus the value of ```flag``` never gets changed. They key here is that Javascript is not interrupt driven. When a timer fires, it does not interrupt the currently running Javascript, run some other Javascript and then let the currently running Javascript continue. It just gets put in the event queue waiting until the currently running Javascript is done to get its turn to run. What you need to do is rethink how your code works and find a different way to trigger whatever code you want to run when the ```flag``` value changes. Javascript is designed as an event-driven language. So, what you need to do is figure out what events you can register an interest in so you can either listen for the event that might cause the flag to change and you can examine the flag on that event or you can trigger your own event from whatever code might change the flag or you can implement a callback function that whatever code changes that flag can call your callback whenever the piece of code responsible for changing the flag value would change it's value to ```true```, it just calls the callback function and thus your code that wants to run when the flag gets set to ```true``` will get to run at the right time. This is much, much more efficient than trying to use some sort of timer to constantly check the flag value. ```function codeThatMightChangeFlag(callback) { // do a bunch of stuff if (condition happens to change flag value) { // call the callback to notify other code callback(); } } ``` ## END ##
## Human: ## According to this famous blog post, the effective transcript length is: $\tilde{l}_i = l_i - \mu$ where $l_i$ is the length of transcript and $\mu$ is the average fragment length. However, typically fragment length is about 300bp. What if when the transcript $l_i$ is smaller than 300? How do you compute the effective length in this case? A related question: when computing the FPKM of a gene, how to choose a transcript? Do we choose a "canonical" transcript (how?) or combine the signals from all transcripts to a gene-level FPKM? ## Assistant: ## The effective length is $\tilde{l}_i = l_i - \mu + 1$ (note the R code at the bottom of Harold's blog post), which in the case of $\mu Regarding choosing a particular transcript, ideally one would use a method like salmon or kallisto (or RSEM if you have time to kill). Otherwise, your options are (A) choose the major isoform (if it's known in your tissue and condition) or (B) use a "union gene model" (sum the non-redundant exon lengths) or (C) take the median transcript length. None of those three options make much of a difference if you're comparing between samples, though they're all inferior to a salmon/kallisto/etc. metric. Why are salmon et al. better methods? They don't use arbitrary metrics that will be the same across samples to determine the feature length. Instead, they use expectation maximization (or similarish, since at least salmon doesn't actually use EM) to quantify individual isoform usage. The effective gene length in a sample is then the average of the transcript lengths after weighting for their relative expression (yes, one should remove $\mu$ in there). This can then vary between samples, which is quite useful if you have isoform switching between samples/groups in such a way that methods A-C above would miss (think of cases where the switch is to a smaller transcript with higher coverage over it...resulting in the coverage/length in methods A-C to be tamped down). ## END ##
## Human: ## What is the significance of 1/1/1753 in SQL Server? ## Assistant: ## The decision to use 1st January 1753 (```1753-01-01```) as the minimum date value for a datetime in SQL Server goes back to its Sybase origins. The significance of the date itself though can be attributed to this man. Philip Stanhope, 4th Earl of Chesterfield. Who steered the Calendar (New Style) Act 1750 through the British Parliament. This legislated for the adoption of the Gregorian calendar for Britain and its then colonies. There were some missing days (internet archive link) in the British calendar in 1752 when the adjustment was finally made from the Julian calendar. September 3, 1752 to September 13, 1752 were lost. Kalen Delaney explained the choice this way > So, with 12 days lost, how can you compute dates? For example, how can you compute the number of days between October 12, 1492, and July 4, 1776? Do you include those missing 12 days? To avoid having to solve this problem, the original Sybase SQL Server developers decided not to allow dates before 1753. You can store earlier dates by using character fields, but you can't use any datetime functions with the earlier dates that you store in character fields. The choice of 1753 does seem somewhat anglocentric however as many catholic countries in Europe had been using the calendar for 170 years before the British implementation (originally delayed due to opposition by the church). Conversely many countries did not reform their calendars until much later, 1918 in Russia. Indeed the October Revolution of 1917 started on 7 November under the Gregorian calendar. Both ```datetime``` and the new ```datetime2``` datatype mentioned in Joe's answer do not attempt to account for these local differences and simply use the Gregorian Calendar. So with the greater range of ```datetime2``` ```SELECT CONVERT(VARCHAR, DATEADD(DAY,-5,CAST('1752-09-13' AS DATETIME2)),100) ``` Returns ```Sep 8 1752 12:00AM ``` One final point with the ```datetime2``` data type is that it uses the proleptic Gregorian calendar projected backwards to well before it was actually invented so is of limited use in dealing with historic dates. This contrasts with other Software implementations such as the Java Gregorian Calendar class which defaults to following the Julian Calendar for dates until October 4, 1582 then jumping to October 15, 1582 in the new Gregorian calendar. It correctly handles the Julian model of leap year before that date and the Gregorian model after that date. The cutover date may be changed by the caller by calling ```setGregorianChange()```. A fairly entertaining article discussing some more peculiarities with the adoption of the calendar can be found here. ## END ##
## Human: ## I saw this video where someone says that electromagnetic wave is a chain reaction of electric and magnetic fields creating each other so the chain of wave moves forward. I wonder where the photon is in this explanation. What is the relation between electromagnetic wave and photon? ## Assistant: ## Both the wave theory of light and the particle theory of light are approximations to a deeper theory called Quantum Electrodynamics (QED for short). Light is not a wave nor a particle but instead it is an excitation in a quantum field. QED is a complicated theory, so while it is possible to do calculations directly in QED we often find it simpler to use an approximation. The wave theory of light is often a good approximation when we are looking at how light propagates, and the particle theory of light is often a good approximation when we are looking at how light interacts i.e. exchanges energy with something else. So it isn't really possible to answer the question where the photon is in this explanation. In general if you're looking at a system, like the one in the video, where the wave theory is a good description of light you'll find the photon theory to be a poor description of light, and vice versa. The two ways of looking at light are complementary. ## END ##
## Human: ## In The Light Fantastic, after talking about the dimensions of the Pyramid of Tsort, it says All in all, it was a lot of effort to go through just to sharpen a razor. What's the joke here? ## Assistant: ## In the real world, there is or was a belief, brought on by the Egyptomania of the early 20th century, that pyramids had special powers—including, specifically, the power to sharpen or maintain the sharpness of razor blades. Pyramid power refers to the belief that the ancient Egyptian pyramids and objects of similar shape can confer a variety of benefits. Among these assumed properties are the ability to preserve foods, sharpen or maintain the sharpness of razor blades, improve health, function "as a thought-form incubator", trigger sexual urges, and cause other effects. Such unverified theories regarding pyramids are collectively known as pyramidology. Czechoslovakian Karel Drbal even patented a pyramid-shaped device specifically designed for razor blades. Among the specifications: It is beneficial to leave a new blade in the pyramid one to two weeks before using it. It is essential to place it there immediately after the first shave, and not the old, dull one. But it is possible to use an old one, if it is properly resharpened. The blade placed using the method above is left unobstructed until the next shave. The west edge should always face west. It improves the sharpening effect. ## END ##
## Human: ## In India, we eat rice using our fingers. Generally in the West, a fork or spoon is used. I have tried eating rice with spoon but I don't feel satisfied with it. We eat dry rice but we mix curry and vegetables with it and then eat it with our hands. Is there a way to eat rice with your hands in front of Westerners such that it doesn't appear to be disgusting to them? By disgusting I mean that they shouldn't feel like vomiting or looking away to avoid me. Even though in India we eat with our fingers, many people eat such that their whole palm is covered with food. That indeed looks disgusting. I cannot avoid hurting them by following different etiquette, but I certainly want to maintain a degree of cleanliness. ## Assistant: ## First and foremost, you can eat the way you want. When it comes to western culture, the only thing that is generally disliked are noises, eating with the mouth wide open (included speaking with full mouth), and (excessively) dirtying the table. Eating rice with hands will be seen (mostly) as strange, not disgusting. Second, context. Are you eating at a formal dinner with your colleagues and a client? Eat with the fork, as everybody else. Are you eating at the work's canteen? Eat the way you prefer and let other deal with their limits. And so on. Third, respect and open mindness: you are worried other peoples find you disgusting for eating rice with hands, but at the same time you show the same "closed mind" (in a way, ok? I'm not trying to offend you) in wanting to eat rice with hands at all cost otherwise you will not feel satisfied; believe me, eating with a fork will not starve you as will not change food nutritional content. Even more if you are eating at a foreign restaurant, e.g. at an italian restaurant rice is cooked and thought to be eatean with a fork. Respect local culture, open yourself to a new experience, and try to learn a different way to enjoy food. In the end: feel free to do whatever fit you most, don't be afraid of other people judging you (their fault), but at the same time don't close yourself in your garden (your fault). ## END ##
## Human: ## The typical argument goes like this: Without net neutrality, cable companies could censor websites, favoring their own business partners. Typically, proponents of legislation point to some perceived injustice, and argue that new laws are needed to address it. But the very use of the subjunctive in the quotation (could censor), suggests that this might be considered by its opponents as a solution in search of a problem. If so, why haven't they used that rhetorical tactic? Conversely, if such incidents have occurred, why don't the neutrality supporters cite them? ## Assistant: ## There have been a few examples, in part, these examples have pushed this more into the spotlight. It's often under the term 'traffic shaping' if you are interested, here is Sandvine, a company that offers those shaping services. If you are interested in the peer2peer domain (which is heavily bringing this issue to the forefront) here is a national list of ISP's and their throttling practices on bit torrent users: Curious if this documented list counts as citing. For the US: Verizon has run testing on it. “YouTube is being throttled to 10Mbps as well,” one person wrote on Thursday. “In the ‘stats for nerds,’ it would load at roughly 1,250KBps which translates to 10Mbps. Put the VPN on and that number tripled easily. Didn’t have an issue playing 1080p in 60fps, though.” (part of the problem with throttling is if you know your way around it, there isn't an issue. Tax on the non-tech savvy). Verizon stated they were not throttling, however, articles suggest they did and still are. Here is an article stating Verizon is actively throttling video connections over their network. Over to Comcast who has been engaging in this practice but not admitting it until much more recently: * When Comcast throttled BitTorrent users, they had a lawsuit brought against them that won. * Sprint: They deny they do, though it's apparent they can. User backlash results in people monitoring if they are being throttled. Of course, there are several accusations. * Bell and Rogers in Canada. Telus has also blocked union sites and have engaged in some pretty underhanded anti-union tactics. Not sure if blocking sites fall under net neutrality though. ## END ##
## Human: ## Does having a longer Ethernet cable slow your connection? ## Assistant: ## For all practical purposes, there will be no effect on the speed of your connection. There will be a very insignificant amount of delay due to long cables. This won't affect the maximum speed of your connection, but it would cause some latency. pjc50 points out that it's about a nanosecond for every foot of cable length, which is a good rule of thumb used by many engineers when developing systems which are very dependent on latencies on those timescales. In reality, you will never notice a difference. A "fast" ping time on the Internet is 10&nbsp;ms, which is 10,000,000&nbsp;ns. Adding even a few hundred feet of cable isn't going to have a noticeable effect at that point. In fact, nearly every step of the way involves delays which are more extreme than those seen from signal propagation. For example, most consumer grade routers will wait for the last byte of an incoming packet to be received and check it for errors before sending the first byte of the packet on its way. This delay will be on the order of 5,000&nbsp;ns! Given that the maximum length of cable you can run (per the Ethernet spec) is 300&nbsp;ft, the cable length could never cause more than 300&nbsp;ns of delay due to the cable! ## END ##
## Human: ## Border around formatted text in Inkscape ## Assistant: ## Here is solution step by step: * Create some flowed text by using the text tool to first drag a rectangle and then type. * Separately, create a rectangle roughly around the text (really, the rectangle can be anywhere) * With the rectangle selected, chose "Path" > "Linked Offset". Then grab the small diamond and drag it in to make a second, smaller rectangle. * Select the text and the smaller rectangle and choose "Text" > "Flow into frame". Then select the smaller rectangle and remove the stroke to make it invisible. Now the text will flow within the smaller (invisible) rectangle, which is dynamically linked to the shape of the larger (visible) rectangle. * Then, if you like, you can add a small border radius on the visible rectangle and you get this nice looking result. * If you do need to modify the space between the text and the rectangle, you can carefully select it by grabbing it with the text and the subtracting the text from the selection, or you can drill down to it within the XML Editor. When you do get it selected, then you can adjust the margin. * Finally, group the text and the two rectangles, and you're all set! ## END ##
## Human: ## I learned about the equilibrium constant. Now, I've seen that the equilibrium constant of burning is extremely small $(K \ll 1)$. here, I have a question. you see, $K$ is still NOT 0, which means that the forward reactions happen at least a tiny bit. Then, shouldn't we see some parts of anything burning at least a little bit? ## Assistant: ## The equilibrium constant for combustion of organic matter in air with oxygen is not small, but extremely large ($K_\mathrm{eq} \gg 1$), as is expected from a reaction that is simultaneously very exothermic and (usually) increases entropy due to the formation of more gaseous molecules than the input oxygen. The major reason carbon-based life can exist at ambient temperature in an oxygen atmosphere is purely kinetic, not thermodynamic. You, the tree outside and everything else made of carbon is right now undergoing continuous combustion. However, in the absence of catalyst, this process is too slow below a couple hundred degrees Celsius for it to be self-sustaining. More technically, combustion of organic matter is a highly exergonic process, but the activation energy is high. The meagre amount of heat generated by the handful of molecules reacting is too quickly diluted into the surroundings, and the reaction does not accelerate and spiral out of control (a fire, as described by the eternal Feynman). Very luckily for us, Life figured out this vast untapped source of chemical energy held back by kinetics approximately three billion years ago and developed a whole metabolic process to extract this energy in a stepwise fashion using catalysis, which we call aerobic respiration. Without it, multicellular organisms could well never had evolved. ## END ##
## Human: ## The name "Bleach" seems to be having no relevance to the plot unlike most other series. Was it just chosen at Kubo-sensei's whim or does it have some significance? Maybe some cultural significance associated with shinigami, etc. that I am now aware of? ## Assistant: ## Kubo himself has commented on this. Below is the quote from Bleach Answers Wiki: > The title wasn't Bleach when Tite Kubo decided to draw a story about Shinigami. This was before he drew the one-shot manga that appeared in Akamaru Jump. The weapon wasn't a sword, but a scythe. Only Rukia had a scythe and the other characters used guns. At that point, the title was Snipe (as in "Sniper"). Right before he started drawing, he began to think that a sword would be better and realized he couldn't use Snipe as a title anymore. He began looking for a title that grasped the bigger picture. Shinigami are associated with the color black, but it would have been boring to use "black". "White", on the other hand, can suggest black as a complementary color. So Tite Kubo chose "Bleach" to evoke the impression of the color white. There are also other theories, which aren't as well supported by interviews with Kubo but still seem to have some evidence supporting them. One is that shinigami "cleanse" souls, similar to what bleach does (Kubo himself mentioned this in an interview, but it's not clear whether he noticed this before or after deciding on the name). Another is that Kubo is a fan of the band Nirvana, and their first album is Bleach. So there might be more than one reason, but the quoted one above is the one he usually gives when asked about it. ## END ##
## Human: ## Was the Millennium Falcon a one-off or was it mass produced? ## Assistant: ## The Millennium Falcon is not unique; it was a model YT-1300f Corellian light freighter. This implies that there are other similar ships, including other variations on the YT-1300 model. You can find a lot of information about the Millennium Falcon in a companion book, Star Wars: The Force Awakens: Incredible Cross-Sections. The model number also appears on-screen at least once: in Star Wars: Rebels episode &quot;Fighter Flight&quot; on a poster. Note, however, that the Millennium Falcon has been highly customized; some of the custom work is mentioned in The Force Awakens. It's not clear just how much of the ship is stock and how much is custom. If you're willing to go outside Canon into Legends, the model number of the ship, and other similar Corellian light freighters, can be seen in some of the video games, and there is an entire novel called Millennium Falcon that details its history. The ship was serial #YT-1300 492727ZED, one of many such vessels, but from the beginning the ship was shown to have some &quot;quirks&quot; to distinguish it from its counterparts. In Legends, the ship was also destroyed and rebuilt from scratch at least once, so it's arguable as to whether it's still the same ship or not. ## END ##
## Human: ## Usually when I see lists of things to do to be more energy efficient, they require one to own their own home. What can I do to be more energy efficient in an apartment? For example, I can't install solar panels, I can't upgrade/change my appliances, I can't install better insulation or windows, and I can't install a programmable thermostat. Pretty much the only thing I can do (and have done) is switch all of my bulbs to CFLs. I also keep all of my electronics on power strips which I turn off when I leave my apartment and when I'm sleeping. ## Assistant: ## There are a lot of little things you can do that can add up. Here are some suggestions: * Wash clothes in cold water only. * Turn your freezer/refrigerator up a few degrees. * Purchase insulated curtains/shades, and close them during the day when you are at work. * Make sure your furniture is arranged so that it is not blocking any vents or radiators. * Purchase a draft guard to place at the bottom of your door leading to the outside where temperature-controlled air can escape from your apartment. * Install a low flow shower water head. Shower head screw on/off easily. Keep the old head to replace when/if you move. * Clean Your Refrigerator: It's unlikely that you'll purchase new energy efficient appliances for your rental, but you can tweak the ones you already have. For example, by cleaning the coils on the back of your fridge you'll help one of the biggest energy hogging appliances run more efficiently. * Use Energy Efficient Bulbs, even if you're put off by the slightly higher upfront cost, you can simply unscrew them and take them with you when it's time to move. * Use Power Strips: A lot of energy is wasted through electronics left plugged in, and the resulting vampire energy. Put a stop to it by using one of a number of cool, smart energy strips, which will turn off vampire energy when the electronics are not in use. * Use a Humidifier & Lower Your Heat: Keeping humidity levels up during the winter is not only good for your health, but also for your air and furniture. It also helps keep the ambient air temperature feeling warmer than dry air, which means you can turn your thermostat down. If your HVAC system doesn't have a humidifier built-in, then you can get an inexpensive and portable humidifier to use during dry conditions. ## END ##
## Human: ## Is there any way to exit ```less``` without clearing the screen? ## Assistant: ## This is actually a function of the terminal emulator you are using (xterm, gnome-terminal, konsole, screen). An alternate screen, or altscreen, gets launched when programs such as ```less``` or ```vim``` are invoked. This altscreen has no history buffer and exits immediately when you quit the program, switching back to the original screen which restores the previous window content history and placement. You can prevent ```less``` from launch in an altscreen by passing the argument "-X". ```less -X /path/to/some/file``` You can also pass "-X" as an environment variable. So if you are using ```bash```, place this in ```~/.bashrc```: ```export LESS="-X" ``` However, this disbles the termcap (terminal capability) initialization and deinitialization, so other views when you use ```less``` may appear off. Another option would be to use ```screen``` and set the option ```altscreen off``` in your ```~/.screenrc```. ```less``` will not clear the screen and should preserve color formatting. Presumably ```tmux``` will have the same option. ## END ##
## Human: ## How can I do 'insert if not exists' in MySQL? ## Assistant: ## In MySQL, ```ON DUPLICATE KEY UPDATE``` or ```INSERT IGNORE INTO table``` can be viable solutions. * An example of ```ON DUPLICATE KEY UPDATE``` update based on mysql.com: ``` INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1; UPDATE table SET c=c+1 WHERE a=1; ``` * An example of INSERT IGNORE based on mysql.com: ``` INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE] [INTO] tbl_name [(col_name,...)] {VALUES | VALUE} ({expr | DEFAULT},...),(...),... [ ON DUPLICATE KEY UPDATE col_name=expr [, col_name=expr] ... ] ``` Or: ``` INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE] [INTO] tbl_name SET col_name={expr | DEFAULT}, ... [ ON DUPLICATE KEY UPDATE col_name=expr [, col_name=expr] ... ] ``` Or: ``` INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE] [INTO] tbl_name [(col_name,...)] SELECT ... [ ON DUPLICATE KEY UPDATE col_name=expr [, col_name=expr] ... ] ``` ## END ##
## Human: ## What does ```class``` do in Ruby? ## Assistant: ## First, the ```class``` syntax opens up ```foo```'s singleton class (eigenclass). This allows you to specialise the behaviour of methods called on that specific object. ```a = 'foo' class << a def inspect '"bar"' end end a.inspect # => "bar" a = 'foo' # new object, new singleton class a.inspect # => "foo" ``` Now, to answer the question: ```class``` opens up ```self```'s singleton class, so that methods can be redefined for the current ```self``` object (which inside a class or module body is the class or module itself). Usually, this is used to define class/module ("static") methods: ```class String class << self def value_of obj obj.to_s end end end String.value_of 42 # => "42" ``` This can also be written as a shorthand: ```class String def self.value_of obj obj.to_s end end ``` Or even shorter: ```def String.value_of obj obj.to_s end ``` When inside a function definition, ```self``` refers to the object the function is being called with. In this case, ```class``` opens the singleton class for that object; one use of that is to implement a poor man's state machine: ```class StateMachineExample def process obj process_hook obj end private def process_state_1 obj # ... class << self alias process_hook process_state_2 end end def process_state_2 obj # ... class << self alias process_hook process_state_1 end end # Set up initial state alias process_hook process_state_1 end ``` So, in the example above, each instance of ```StateMachineExample``` has ```process_hook``` aliased to ```process_state_1```, but note how in the latter, it can redefine ```process_hook``` (for ```self``` only, not affecting other ```StateMachineExample``` instances) to ```process_state_2```. So, each time a caller calls the ```process``` method (which calls the redefinable ```process_hook```), the behaviour changes depending on what state it's in. ## END ##
## Human: ## "I have a problem where i'm initialising a variable on the scope in a controller. Then it gets changed in another controller when a user logs in. This variable is used to control things such as the navigation bar and restricts access to parts of the site depending on the type of user, so its important that it holds its value. The problem with it is that the controller that initialises it, gets called again by angular some how and then resets the variable back to its initial value. I assume this is not the correct way of declaring and initialising global variables, well its not really global, so my question is what is the correct way and is there any good examples around that work with the current version of angular?" ## Assistant: ## You've got basically 2 options for "global" variables: * use a ```$rootScope``` * use a service ```$rootScope``` is a parent of all scopes so values exposed there will be visible in all templates and controllers. Using the ```$rootScope``` is very easy as you can simply inject it into any controller and change values in this scope. It might be convenient but has all the problems of global variables. Services are singletons that you can inject to any controller and expose their values in a controller's scope. Services, being singletons are still 'global' but you've got far better control over where those are used and exposed. Using services is a bit more complex, but not that much, here is an example: ``` var myApp = angular.module('myApp',[]); myApp.factory('UserService', function() { return { name : 'anonymous' }; }); ``` and then in a controller: ``` function MyCtrl($scope, UserService) { $scope.name = UserService.name; } ``` ## END ##
## Human: ## How do I initialize a TypeScript Object with a JSON-Object? ## Assistant: ## This question is quite broad, here are two solutions. ### Solution 1: Helper Method Here's an example of using a Helper Method that you could change to fit your needs: ```class SerializationHelper { static toInstance(obj: T, json: string) : T { var jsonObj = JSON.parse(json); if (typeof obj["fromJSON"] === "function") { obj["fromJSON"](jsonObj); } else { for (var propName in jsonObj) { obj[propName] = jsonObj[propName] } } return obj; } } ``` Then use it: ```var json = '{"name": "John Doe"}', foo = SerializationHelper.toInstance(new Foo(), json); foo.GetName() === "John Doe"; ``` * Advanced Deserialization This could also allow for some custom deserialization by adding your own ```fromJSON``` method to the class (this works well with how ```JSON.stringify``` already uses the ```toJSON``` method, as shown below): ```interface IFooSerialized { nameSomethingElse: string; } class Foo { name: string; GetName(): string { return this.name } toJSON(): IFooSerialized { return { nameSomethingElse: this.name }; } fromJSON(obj: IFooSerialized) { this.name = obj.nameSomethingElse; } } ``` Then use it: ```var foo1 = new Foo(); foo1.name = "John Doe"; var json = JSON.stringify(foo1); json === '{"nameSomethingElse":"John Doe"}'; var foo2 = SerializationHelper.toInstance(new Foo(), json); foo2.GetName() === "John Doe"; ``` ### Solution 2: Base Class Another way you could do this is by creating your own base class: ```class Serializable { fillFromJSON(json: string) { var jsonObj = JSON.parse(json); for (var propName in jsonObj) { this[propName] = jsonObj[propName] } } } class Foo extends Serializable { name: string; GetName(): string { return this.name } } ``` Then use it: ```var foo = new Foo(); foo.fillFromJSON(json); ``` There are too many different ways to implement a custom deserialization using a base class, and ultimately it depends on how your own choice. ## END ##
## Human: ## Why is digital photography so expensive? ## Assistant: ## Digital photography is not expensive but buying into it is. With film photography camera and lenses were a moderate investment but these lasted quite long. Even a battery in a film camera would last several years. Cameras and lenses were mechanical and much simpler than today's models. This made them require less expertise to manufacture and also made them incredibly sturdy. A lifespan of a decade for a camera and several decades for lenses was not uncommon. However, the real cost of film photography is film and development. Each roll of film costs several dollars and gives up to 36 images. Specialized rolls were more expensive and had fewer exposures. Then, if you did not do development yourself, you had to pay for development. Also prints. Even if you did your own, chemicals and papers were recurring costs. And there was the added expensive of creating a wet darkroom. In the end, the hobby was quite costly. With digital photography, you pay nearly all costs up-front and it comes out much cheaper for anyone who is into the art and shoots regularly. A mid-range DSLR with good lens and memory can easily be obtained for less than $2500. Many entry level ones exist for under $1000. If you were to shoot 10,000 frames before both your camera and lens dies, it would cost you between 2.5&cent; to 10&cent; per frame. It is far far less than even just the film for each frame. Still most cameras have shutters rated to at least 100,000 frames and lenses last much longer than cameras. Even if you only captured 1000 images, you would have a better deal than with film photography. All digital cameras come with rechargeable batteries, good for 500-1000 shots-per-charge (for DSLRs) and can be charged at least 50-100 times before needing to be replaced. That means you actually will not use up the battery before you have taken full advantage of the better value of digital photography. Market conditions are forcing the price of digital photography to increase. The loss of market share of compact and ultra-compact digital cameras to cellphones forced manufacturers to discontinue such products and concentrate on higher-end offering. While the number of unit sales of digital cameras has been declining, the average price is going up to compensate for reduced revenue. This trend is expected to continue for a while. ## END ##
## Human: ## If user input is inserted without modification into an SQL query, then the application becomes vulnerable to SQL injection, like in the following example: ```$unsafe_variable = $_POST['user_input']; mysql_query("INSERT INTO `table` (`column`) VALUES ('$unsafe_variable')"); ``` That's because the user can input something like ```value'); DROP TABLE table;--```, and the query becomes: ```INSERT INTO `table` (`column`) VALUES('value'); DROP TABLE table;--') ``` What can be done to prevent this from happening? ## Assistant: ## The correct way to avoid SQL injection attacks, no matter which database you use, is to separate the data from SQL, so that data stays data and will never be interpreted as commands by the SQL parser. It is possible to create an SQL statement with correctly formatted data parts, but if you don't fully understand the details, you should always use prepared statements and parameterized queries. These are SQL statements that are sent to and parsed by the database server separately from any parameters. This way it is impossible for an attacker to inject malicious SQL. You basically have two options to achieve this: 1. Using PDO (for any supported database driver): ``` $stmt = $pdo->prepare('SELECT * FROM employees WHERE name = :name'); $stmt->execute([ 'name' => $name ]); foreach ($stmt as $row) { // Do something with $row } ``` 2. Using MySQLi (for MySQL): Since PHP 8.2+ we can make use of execute_query() which prepares, binds parameters, and executes SQL statement in one method: ``` $result = $dbConnection->execute_query('SELECT * FROM employees WHERE name = ?', [$name]); while ($row = $result->fetch_assoc()) { // Do something with $row } ``` Up to PHP8.1: ``` $stmt = $dbConnection->prepare('SELECT * FROM employees WHERE name = ?'); $stmt->bind_param('s', $name); // 's' specifies the variable type => 'string' $stmt->execute(); $result = $stmt->get_result(); while ($row = $result->fetch_assoc()) { // Do something with $row } ``` If you're connecting to a database other than MySQL, there is a driver-specific second option that you can refer to (for example, pg_prepare() and pg_execute() for PostgreSQL). PDO is the universal option. ## END ##
## Human: ## I want to be able to output the current loop iteration to my template. According to the docs, there is a ```loop.counter``` variable that I am trying to use: ``` {% for user in userlist %} * {{ user }} {{loop.counter}} {% if loop.counter == 1 %} This is the First user {% endif %} {% endfor %} ``` But is being outputed to my template. What is the correct syntax? ## Assistant: ## The counter variable inside the loop is called loop.index in Jinja2. ``` >>> from jinja2 import Template >>> s = "{% for element in elements %}{{loop.index}} {% endfor %}" >>> Template(s).render(elements=["a", "b", "c", "d"]) 1 2 3 4 ``` In addition to `loop.index`, there is also * `loop.index0` (index starting at `0`) * `loop.revindex` (reverse index; ending at `1`) * `loop.revindex0` (reverse index; ending at `0`) etc. ## END ##
## Human: ## Are the players on the same team as the DM? ## Assistant: ## The GM isn't on a team, they’re the referee. The way Pathfinder, D&D, and similar games work is that there are one or more player characters (the "party") that are controlled by the players. The GM (DM, in D&D) usually doesn't run a player character (PC), but they are responsible for representing all aspects of the world, all the non-player characters (NPCs) and monsters and stuff, and adjudicating the rules. So the GM may be controlling a character or creature that is friendly to the PCs, or a character or creature that is hostile to the PCs, or both at once! The GM is the orcs you fight and the friendly cleric that heals you and the rain that falls on you. GMs aren't supposed to be hostile to you in general; they are generally taught to give the players a fun challenge. It's no good to be "against" the players, because you can always kill PCs just by saying "rocks fall, everyone dies!" The GM is on your team inasmuch as they want everyone to have fun. Because their role is running monsters and the other stuff that you fight, they are often in opposition to you tactically, as they are responsible for the "other team" as part of their job. Some new GMs do get confused by this and decide it's them against the party, but that ends up being a problem that has to be solved by them wising up or their players leaving. The context of your question isn't clear, so I'll add a note that might be relevant - some GMs, especially new ones, do run a PC of their own that's part of the party, these are referred to as GMPCs (or DMPCs). This is generally frowned upon because of the conflict of interest inherent in doing it, but it happens. That GMPC may be on your side, on your side most of the time, betray you, have other plans - just like any other person in the game world might. ## END ##
## Human: ## How can I adapt Ubuntu to a high resolution display? I have a display with 3200x1600px on only 11'' and everything looks really tiny. ## Assistant: ## There are some steps to take: 1. System wide Menu and titlebar scaling Starting with Ubuntu 14.04 we have an option that helps a bit: Scaling Support open the System Settings (here in english:) ```LANG=c unity-control-center ``` Go to "Displays" and set the "Scale for menu and title bars". Since Ubuntu 17.10 the scaling can be set in ```LANG=c gnome-control-center ``` Go to ```Settings > Devices > Displays``` there see also: How to find and change the screen DPI? 2. Universal Access Go to "Universal Access" (```unity-control-center universal-access```) and select "Large Text". Note: not all applications handle this correctly, some will not reserve the extra space, so some UI elements are not accessible with this option! 3.Increase unity dock size In ```unity-control-center```->```Appearance```->```Look``` at the botom, you can adjust the size. 4. Adapt Firefox see: Adjust Firefox and Thunderbird to a High DPI touchscreen display (retina) (or use Chrome, which works fine since Version 41.0.2272.76 Ubuntu 14.10, though Chrome will need to be restarted to take effect) 5. Increase font in Pidgin There is a plugin you can install: ```sudo apt-get install pidgin-extprefs ``` Then you can increase the font in Plugins->Extended Prefs 6. Create starter for applications that still don't scale Some applications still don't obey the global scaling (mainly java) for those few applications you can create a starter to only Fix scaling of java-based applications for a high DPI screen Note: In older Ubuntu versions, with unity-tweak-util in the section "Fonts" you can set the "Text Scaling Factor" to 2.0. This will scale the fonts in most applications to double size. ## END ##